Mercurial > projects > ddmd
annotate dmd/TemplateInstance.d @ 62:6557375aff35
InExp.opId_r
WhileStatement.syntaxCopy
author | korDen |
---|---|
date | Mon, 23 Aug 2010 03:21:32 +0400 |
parents | 460959608115 |
children | 2e2a5c3f943a |
rev | line source |
---|---|
0 | 1 module dmd.TemplateInstance; |
2 | |
3 import dmd.ScopeDsymbol; | |
4 import dmd.IntegerExp; | |
5 import dmd.Identifier; | |
6 import dmd.ArrayTypes; | |
7 import dmd.TupleDeclaration; | |
8 import dmd.TemplateParameter; | |
9 import dmd.AliasDeclaration; | |
10 import dmd.TemplateDeclaration; | |
11 import dmd.TupleExp; | |
12 import dmd.WithScopeSymbol; | |
13 import dmd.Dsymbol; | |
14 import dmd.Module; | |
15 import dmd.ArrayTypes; | |
16 import dmd.Loc; | |
17 import dmd.Global; | |
18 import dmd.Util; | |
19 import dmd.Type; | |
20 import dmd.Expression; | |
21 import dmd.Tuple; | |
22 import dmd.STC; | |
23 import dmd.TOK; | |
24 import dmd.TY; | |
25 import dmd.TypeTuple; | |
26 import dmd.Argument; | |
27 import dmd.WANT; | |
28 import dmd.ExpInitializer; | |
29 import dmd.Array; | |
30 import dmd.DsymbolTable; | |
31 import dmd.Scope; | |
32 import dmd.OutBuffer; | |
33 import dmd.HdrGenState; | |
34 import dmd.VarDeclaration; | |
35 import dmd.VarExp; | |
36 import dmd.FuncExp; | |
37 import dmd.Declaration; | |
38 import dmd.MATCH; | |
20
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
39 import dmd.templates.Util; |
0 | 40 |
41 import dmd.backend.glue; | |
42 | |
43 Tuple isTuple(Object o) | |
44 { | |
45 //return dynamic_cast<Tuple *>(o); | |
46 ///if (!o || o.dyncast() != DYNCAST_TUPLE) | |
47 /// return null; | |
48 return cast(Tuple)o; | |
49 } | |
50 | |
51 /****************************** | |
52 * If o1 matches o2, return 1. | |
53 * Else, return 0. | |
54 */ | |
55 | |
56 bool match(Object o1, Object o2, TemplateDeclaration tempdecl, Scope sc) | |
57 { | |
58 Type t1 = isType(o1); | |
59 Type t2 = isType(o2); | |
60 Expression e1 = isExpression(o1); | |
61 Expression e2 = isExpression(o2); | |
62 Dsymbol s1 = isDsymbol(o1); | |
63 Dsymbol s2 = isDsymbol(o2); | |
64 Tuple v1 = isTuple(o1); | |
65 Tuple v2 = isTuple(o2); | |
66 | |
67 //printf("\t match t1 %p t2 %p, e1 %p e2 %p, s1 %p s2 %p, v1 %p v2 %p\n", t1,t2,e1,e2,s1,s2,v1,v2); | |
68 | |
69 /* A proper implementation of the various equals() overrides | |
70 * should make it possible to just do o1.equals(o2), but | |
71 * we'll do that another day. | |
72 */ | |
73 | |
74 if (t1) | |
75 { | |
76 /* if t1 is an instance of ti, then give error | |
77 * about recursive expansions. | |
78 */ | |
79 Dsymbol s = t1.toDsymbol(sc); | |
80 if (s && s.parent) | |
81 { | |
82 TemplateInstance ti1 = s.parent.isTemplateInstance(); | |
83 if (ti1 && ti1.tempdecl == tempdecl) | |
84 { | |
85 for (Scope sc1 = sc; sc1; sc1 = sc1.enclosing) | |
86 { | |
87 if (sc1.scopesym == ti1) | |
88 { | |
89 error("recursive template expansion for template argument %s", t1.toChars()); | |
90 return true; // fake a match | |
91 } | |
92 } | |
93 } | |
94 } | |
95 | |
96 //printf("t1 = %s\n", t1.toChars()); | |
97 //printf("t2 = %s\n", t2.toChars()); | |
98 if (!t2 || !t1.equals(t2)) | |
99 goto Lnomatch; | |
100 } | |
101 else if (e1) | |
102 { | |
103 static if (false) { | |
104 if (e1 && e2) | |
105 { | |
106 printf("match %d\n", e1.equals(e2)); | |
107 e1.print(); | |
108 e2.print(); | |
109 e1.type.print(); | |
110 e2.type.print(); | |
111 } | |
112 } | |
113 if (!e2) | |
114 goto Lnomatch; | |
115 if (!e1.equals(e2)) | |
116 goto Lnomatch; | |
117 } | |
118 else if (s1) | |
119 { | |
120 //printf("%p %s, %p %s\n", s1, s1.toChars(), s2, s2.toChars()); | |
121 if (!s2 || !s1.equals(s2) || s1.parent != s2.parent) | |
122 { | |
123 goto Lnomatch; | |
124 } | |
125 version (DMDV2) { | |
126 VarDeclaration vv1 = s1.isVarDeclaration(); | |
127 VarDeclaration vv2 = s2.isVarDeclaration(); | |
128 if (vv1 && vv2 && vv1.storage_class & vv2.storage_class & STCmanifest) | |
129 { | |
130 ExpInitializer ei1 = vv1.init.isExpInitializer(); | |
131 ExpInitializer ei2 = vv2.init.isExpInitializer(); | |
132 if (ei1 && ei2 && !ei1.exp.equals(ei2.exp)) | |
133 goto Lnomatch; | |
134 } | |
135 } | |
136 } | |
137 else if (v1) | |
138 { | |
139 if (!v2) | |
140 goto Lnomatch; | |
141 | |
142 if (v1.objects.dim != v2.objects.dim) | |
143 goto Lnomatch; | |
144 | |
145 for (size_t i = 0; i < v1.objects.dim; i++) | |
146 { | |
147 if (!match(cast(Object)v1.objects.data[i], cast(Object)v2.objects.data[i], tempdecl, sc)) | |
148 goto Lnomatch; | |
149 } | |
150 } | |
151 //printf("match\n"); | |
152 return true; // match | |
153 | |
154 Lnomatch: | |
155 //printf("nomatch\n"); | |
156 return false; // nomatch; | |
157 } | |
158 | |
159 class TemplateInstance : ScopeDsymbol | |
160 { | |
161 /* Given: | |
162 * foo!(args) => | |
163 * name = foo | |
164 * tiargs = args | |
165 */ | |
166 Identifier name; | |
167 //Array idents; | |
168 Objects tiargs; // Array of Types/Expressions of template | |
169 // instance arguments [int*, char, 10*10] | |
170 | |
171 Objects tdtypes; // Array of Types/Expressions corresponding | |
172 // to TemplateDeclaration.parameters | |
173 // [int, char, 100] | |
174 | |
175 TemplateDeclaration tempdecl; // referenced by foo.bar.abc | |
176 TemplateInstance inst; // refer to existing instance | |
177 TemplateInstance tinst; // enclosing template instance | |
178 ScopeDsymbol argsym; // argument symbol table | |
179 AliasDeclaration aliasdecl; // !=null if instance is an alias for its | |
180 // sole member | |
181 WithScopeSymbol withsym; // if a member of a with statement | |
182 int semanticRun; // has semantic() been done? | |
183 int semantictiargsdone; // has semanticTiargs() been done? | |
184 int nest; // for recursion detection | |
185 int havetempdecl; // 1 if used second constructor | |
186 Dsymbol isnested; // if referencing local symbols, this is the context | |
187 int errors; // 1 if compiled with errors | |
188 version (IN_GCC) { | |
189 /* On some targets, it is necessary to know whether a symbol | |
190 will be emitted in the output or not before the symbol | |
191 is used. This can be different from getModule(). */ | |
192 Module objFileModule; | |
193 } | |
194 | |
195 this(Loc loc, Identifier ident) | |
196 { | |
197 super(null); | |
198 | |
199 version (LOG) { | |
200 printf("TemplateInstance(this = %p, ident = '%s')\n", this, ident ? ident.toChars() : "null"); | |
201 } | |
202 this.loc = loc; | |
203 this.name = ident; | |
204 | |
205 tdtypes = new Objects(); | |
206 } | |
207 | |
208 /***************** | |
209 * This constructor is only called when we figured out which function | |
210 * template to instantiate. | |
211 */ | |
212 this(Loc loc, TemplateDeclaration td, Objects tiargs) | |
213 { | |
214 super(null); | |
215 | |
216 version (LOG) { | |
217 printf("TemplateInstance(this = %p, tempdecl = '%s')\n", this, td.toChars()); | |
218 } | |
219 this.loc = loc; | |
220 this.name = td.ident; | |
221 this.tiargs = tiargs; | |
222 this.tempdecl = td; | |
223 this.semantictiargsdone = 1; | |
224 this.havetempdecl = 1; | |
225 | |
226 assert(cast(size_t)cast(void*)tempdecl.scope_ > 0x10000); | |
227 | |
228 tdtypes = new Objects(); | |
229 } | |
230 | |
231 static Objects arraySyntaxCopy(Objects objs) | |
232 { | |
20
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
233 Objects a = null; |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
234 if (objs) |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
235 { a = new Objects(); |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
236 a.setDim(objs.dim); |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
237 for (size_t i = 0; i < objs.dim; i++) |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
238 { |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
239 a.data[i] = cast(void*)objectSyntaxCopy(cast(Object)objs.data[i]); |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
240 } |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
241 } |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
242 return a; |
0 | 243 } |
244 | |
20
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
245 Dsymbol syntaxCopy(Dsymbol s) |
0 | 246 { |
20
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
247 TemplateInstance ti; |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
248 |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
249 if (s) |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
250 ti = cast(TemplateInstance)s; |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
251 else |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
252 ti = new TemplateInstance(loc, name); |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
253 |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
254 ti.tiargs = arraySyntaxCopy(tiargs); |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
255 |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
256 ScopeDsymbol.syntaxCopy(ti); |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
257 return ti; |
0 | 258 } |
259 | |
260 void semantic(Scope sc) | |
261 { | |
262 if (global.errors) | |
263 { | |
264 if (!global.gag) | |
265 { | |
266 /* Trying to soldier on rarely generates useful messages | |
267 * at this point. | |
268 */ | |
269 fatal(); | |
270 } | |
271 return; | |
272 } | |
273 | |
274 version (LOG) { | |
275 printf("\n+TemplateInstance.semantic('%s', this=%p)\n", toChars(), this); | |
276 } | |
277 | |
278 if (inst) // if semantic() was already run | |
279 { | |
280 version (LOG) { | |
281 printf("-TemplateInstance.semantic('%s', this=%p) already run\n", inst.toChars(), inst); | |
282 } | |
283 return; | |
284 } | |
285 | |
286 // get the enclosing template instance from the scope tinst | |
287 tinst = sc.tinst; | |
288 | |
289 if (semanticRun != 0) | |
290 { | |
291 error(loc, "recursive template expansion"); | |
292 // inst = this; | |
293 return; | |
294 } | |
295 | |
296 semanticRun = 1; | |
297 | |
298 version (LOG) { | |
299 printf("\tdo semantic\n"); | |
300 } | |
301 if (havetempdecl) | |
302 { | |
303 assert(cast(size_t)cast(void*)tempdecl.scope_ > 0x10000); | |
304 | |
305 // Deduce tdtypes | |
306 tdtypes.setDim(tempdecl.parameters.dim); | |
307 if (!tempdecl.matchWithInstance(this, tdtypes, 2)) | |
308 { | |
309 error("incompatible arguments for template instantiation"); | |
310 inst = this; | |
311 return; | |
312 } | |
313 } | |
314 else | |
315 { | |
316 /* Run semantic on each argument, place results in tiargs[] | |
317 * (if we havetempdecl, then tiargs is already evaluated) | |
318 */ | |
319 semanticTiargs(sc); | |
320 | |
321 tempdecl = findTemplateDeclaration(sc); | |
322 if (tempdecl) | |
323 tempdecl = findBestMatch(sc); | |
324 | |
325 if (!tempdecl || global.errors) | |
326 { | |
327 inst = this; | |
328 //printf("error return %p, %d\n", tempdecl, global.errors); | |
329 return; // error recovery | |
330 } | |
331 } | |
332 | |
333 hasNestedArgs(tiargs); | |
334 | |
335 /* See if there is an existing TemplateInstantiation that already | |
336 * implements the typeargs. If so, just refer to that one instead. | |
337 */ | |
338 | |
339 for (size_t i = 0; i < tempdecl.instances.dim; i++) | |
340 { | |
341 TemplateInstance ti = cast(TemplateInstance)tempdecl.instances.data[i]; | |
342 version (LOG) { | |
343 printf("\t%s: checking for match with instance %d (%p): '%s'\n", toChars(), i, ti, ti.toChars()); | |
344 } | |
345 assert(tdtypes.dim == ti.tdtypes.dim); | |
346 | |
347 // Nesting must match | |
348 if (isnested !is ti.isnested) | |
349 { | |
350 //printf("test2 isnested %s ti.isnested %s\n", isnested ? isnested.toChars() : "", ti.isnested ? ti.isnested.toChars() : ""); | |
351 continue; | |
352 } | |
353 static if (false) { | |
354 if (isnested && sc.parent != ti.parent) | |
355 continue; | |
356 } | |
357 for (size_t j = 0; j < tdtypes.dim; j++) | |
358 { | |
359 Object o1 = cast(Object)tdtypes.data[j]; | |
360 Object o2 = cast(Object)ti.tdtypes.data[j]; | |
361 if (!match(o1, o2, tempdecl, sc)) | |
362 { | |
363 goto L1; | |
364 } | |
365 } | |
366 | |
367 // It's a match | |
368 inst = ti; | |
369 parent = ti.parent; | |
370 version (LOG) { | |
371 printf("\tit's a match with instance %p\n", inst); | |
372 } | |
373 return; | |
374 | |
375 L1: | |
376 ; | |
377 } | |
378 | |
379 /* So, we need to implement 'this' instance. | |
380 */ | |
381 version (LOG) { | |
382 printf("\timplement template instance '%s'\n", toChars()); | |
383 } | |
384 uint errorsave = global.errors; | |
385 inst = this; | |
386 int tempdecl_instance_idx = tempdecl.instances.dim; | |
387 tempdecl.instances.push(cast(void*)this); | |
388 parent = tempdecl.parent; | |
389 //printf("parent = '%s'\n", parent.kind()); | |
390 | |
391 ident = genIdent(); // need an identifier for name mangling purposes. | |
392 | |
393 static if (true) { | |
394 if (isnested) | |
395 parent = isnested; | |
396 } | |
397 //printf("parent = '%s'\n", parent.kind()); | |
398 | |
399 // Add 'this' to the enclosing scope's members[] so the semantic routines | |
400 // will get called on the instance members | |
401 static if (true) { | |
402 int dosemantic3 = 0; | |
403 { | |
404 Array a; | |
405 | |
406 Scope scx = sc; | |
407 static if (false) { | |
408 for (scx = sc; scx; scx = scx.enclosing) | |
409 if (scx.scopesym) | |
410 break; | |
411 } | |
412 | |
413 //if (scx && scx.scopesym) printf("3: scx is %s %s\n", scx.scopesym.kind(), scx.scopesym.toChars()); | |
414 if (scx && scx.scopesym && | |
415 scx.scopesym.members && !scx.scopesym.isTemplateMixin() | |
416 | |
417 /// static if (false) { // removed because it bloated compile times | |
418 /// /* The problem is if A imports B, and B imports A, and both A | |
419 /// * and B instantiate the same template, does the compilation of A | |
420 /// * or the compilation of B do the actual instantiation? | |
421 /// * | |
422 /// * see bugzilla 2500. | |
423 /// */ | |
424 /// && !scx.module.selfImports() | |
425 /// } | |
426 ) | |
427 { | |
428 //printf("\t1: adding to %s %s\n", scx.scopesym.kind(), scx.scopesym.toChars()); | |
429 a = scx.scopesym.members; | |
430 } | |
431 else | |
432 { Module m = sc.module_.importedFrom; | |
433 //printf("\t2: adding to module %s instead of module %s\n", m.toChars(), sc.module.toChars()); | |
434 a = m.members; | |
435 if (m.semanticRun >= 3) | |
436 dosemantic3 = 1; | |
437 } | |
438 | |
439 for (int i = 0; 1; i++) | |
440 { | |
441 if (i == a.dim) | |
442 { | |
443 a.push(cast(void*)this); | |
444 break; | |
445 } | |
446 | |
447 if (this is cast(Dsymbol)a.data[i]) // if already in Array | |
448 break; | |
449 } | |
450 } | |
451 } | |
452 | |
453 // Copy the syntax trees from the TemplateDeclaration | |
454 members = Dsymbol.arraySyntaxCopy(tempdecl.members); | |
455 | |
456 // Create our own scope for the template parameters | |
457 Scope scope_ = tempdecl.scope_; | |
458 if (!tempdecl.semanticRun) | |
459 { | |
460 error("template instantiation %s forward references template declaration %s\n", toChars(), tempdecl.toChars()); | |
461 return; | |
462 } | |
463 | |
464 version (LOG) { | |
465 printf("\tcreate scope for template parameters '%s'\n", toChars()); | |
466 } | |
467 argsym = new ScopeDsymbol(); | |
468 argsym.parent = scope_.parent; | |
469 scope_ = scope_.push(argsym); | |
470 // scope.stc = 0; | |
471 | |
472 // Declare each template parameter as an alias for the argument type | |
473 Scope paramscope = scope_.push(); | |
474 paramscope.stc = STCundefined; | |
475 declareParameters(paramscope); | |
476 paramscope.pop(); | |
477 | |
478 // Add members of template instance to template instance symbol table | |
479 // parent = scope.scopesym; | |
480 symtab = new DsymbolTable(); | |
13 | 481 bool memnum = false; |
0 | 482 for (int i = 0; i < members.dim; i++) |
483 { | |
484 Dsymbol s = cast(Dsymbol)members.data[i]; | |
485 version (LOG) { | |
486 printf("\t[%d] adding member '%s' %p kind %s to '%s', memnum = %d\n", i, s.toChars(), s, s.kind(), this.toChars(), memnum); | |
487 } | |
488 memnum |= s.addMember(scope_, this, memnum); | |
489 } | |
490 | |
491 version (LOG) { | |
492 printf("adding members done\n"); | |
493 } | |
494 | |
495 /* See if there is only one member of template instance, and that | |
496 * member has the same name as the template instance. | |
497 * If so, this template instance becomes an alias for that member. | |
498 */ | |
499 //printf("members.dim = %d\n", members.dim); | |
500 if (members.dim) | |
501 { | |
502 Dsymbol s; | |
503 if (Dsymbol.oneMembers(members, &s) && s) | |
504 { | |
505 //printf("s.kind = '%s'\n", s.kind()); | |
506 //s.print(); | |
507 //printf("'%s', '%s'\n", s.ident.toChars(), tempdecl.ident.toChars()); | |
508 if (s.ident && s.ident.equals(tempdecl.ident)) | |
509 { | |
510 //printf("setting aliasdecl\n"); | |
511 aliasdecl = new AliasDeclaration(loc, s.ident, s); | |
512 } | |
513 } | |
514 } | |
515 | |
516 // Do semantic() analysis on template instance members | |
517 version (LOG) { | |
518 printf("\tdo semantic() on template instance members '%s'\n", toChars()); | |
519 } | |
520 Scope sc2; | |
521 sc2 = scope_.push(this); | |
522 //printf("isnested = %d, sc.parent = %s\n", isnested, sc.parent.toChars()); | |
523 sc2.parent = /*isnested ? sc.parent :*/ this; | |
524 sc2.tinst = this; | |
525 | |
526 try | |
527 { | |
528 static int nest; | |
529 //printf("%d\n", nest); | |
530 if (++nest > 500) | |
531 { | |
532 global.gag = 0; // ensure error message gets printed | |
533 error("recursive expansion"); | |
534 fatal(); | |
535 } | |
536 for (int i = 0; i < members.dim; i++) | |
537 { | |
538 Dsymbol s = cast(Dsymbol)members.data[i]; | |
539 //printf("\t[%d] semantic on '%s' %p kind %s in '%s'\n", i, s.toChars(), s, s.kind(), this.toChars()); | |
540 //printf("test: isnested = %d, sc2.parent = %s\n", isnested, sc2.parent.toChars()); | |
541 // if (isnested) | |
542 // s.parent = sc.parent; | |
543 //printf("test3: isnested = %d, s.parent = %s\n", isnested, s.parent.toChars()); | |
544 s.semantic(sc2); | |
545 //printf("test4: isnested = %d, s.parent = %s\n", isnested, s.parent.toChars()); | |
546 sc2.module_.runDeferredSemantic(); | |
547 } | |
548 --nest; | |
549 } | |
20
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
550 catch (Exception e) |
0 | 551 { |
552 global.gag = 0; // ensure error message gets printed | |
553 error("recursive expansion"); | |
554 fatal(); | |
555 } | |
556 | |
557 /* If any of the instantiation members didn't get semantic() run | |
558 * on them due to forward references, we cannot run semantic2() | |
559 * or semantic3() yet. | |
560 */ | |
561 for (size_t i = 0; i < Module.deferred.dim; i++) | |
562 { | |
563 Dsymbol sd = cast(Dsymbol)Module.deferred.data[i]; | |
564 | |
565 if (sd.parent is this) | |
566 goto Laftersemantic; | |
567 } | |
568 | |
569 /* The problem is when to parse the initializer for a variable. | |
570 * Perhaps VarDeclaration.semantic() should do it like it does | |
571 * for initializers inside a function. | |
572 */ | |
573 // if (sc.parent.isFuncDeclaration()) | |
574 | |
575 /* BUG 782: this has problems if the classes this depends on | |
576 * are forward referenced. Find a way to defer semantic() | |
577 * on this template. | |
578 */ | |
579 semantic2(sc2); | |
580 | |
581 if (sc.func || dosemantic3) | |
582 { | |
583 semantic3(sc2); | |
584 } | |
585 | |
586 Laftersemantic: | |
587 sc2.pop(); | |
588 | |
589 scope_.pop(); | |
590 | |
591 // Give additional context info if error occurred during instantiation | |
592 if (global.errors != errorsave) | |
593 { | |
594 error("error instantiating"); | |
595 if (tinst && !global.gag) | |
596 { | |
597 tinst.printInstantiationTrace(); | |
598 fatal(); | |
599 } | |
600 errors = 1; | |
601 if (global.gag) | |
602 tempdecl.instances.remove(tempdecl_instance_idx); | |
603 } | |
604 | |
605 version (LOG) { | |
606 printf("-TemplateInstance.semantic('%s', this=%p)\n", toChars(), this); | |
607 } | |
608 } | |
609 | |
610 void semantic2(Scope sc) | |
611 { | |
612 int i; | |
613 | |
614 if (semanticRun >= 2) | |
615 return; | |
616 | |
617 semanticRun = 2; | |
618 version (LOG) { | |
20
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
619 printf("+TemplateInstance.semantic2('%s')\n", toChars()); |
0 | 620 } |
621 | |
622 if (!errors && members) | |
623 { | |
624 sc = tempdecl.scope_; | |
625 assert(sc); | |
626 sc = sc.push(argsym); | |
627 sc = sc.push(this); | |
628 sc.tinst = this; | |
629 | |
630 for (i = 0; i < members.dim; i++) | |
631 { | |
632 Dsymbol s = cast(Dsymbol)members.data[i]; | |
633 version (LOG) { | |
634 printf("\tmember '%s', kind = '%s'\n", s.toChars(), s.kind()); | |
635 } | |
636 s.semantic2(sc); | |
637 } | |
638 | |
639 sc = sc.pop(); | |
640 sc.pop(); | |
641 } | |
642 | |
643 version (LOG) { | |
20
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
644 printf("-TemplateInstance.semantic2('%s')\n", toChars()); |
0 | 645 } |
646 } | |
647 | |
648 void semantic3(Scope sc) | |
649 { | |
650 version (LOG) { | |
651 printf("TemplateInstance.semantic3('%s'), semanticRun = %d\n", toChars(), semanticRun); | |
652 } | |
653 //if (toChars()[0] == 'D') *(char*)0=0; | |
654 if (semanticRun >= 3) | |
655 return; | |
656 semanticRun = 3; | |
657 if (!errors && members) | |
658 { | |
659 sc = tempdecl.scope_; | |
660 sc = sc.push(argsym); | |
661 sc = sc.push(this); | |
662 sc.tinst = this; | |
663 for (int i = 0; i < members.dim; i++) | |
664 { | |
665 Dsymbol s = cast(Dsymbol)members.data[i]; | |
666 s.semantic3(sc); | |
667 } | |
668 sc = sc.pop(); | |
669 sc.pop(); | |
670 } | |
671 } | |
672 | |
673 void inlineScan() | |
674 { | |
675 version (LOG) { | |
676 printf("TemplateInstance.inlineScan('%s')\n", toChars()); | |
677 } | |
678 if (!errors && members) | |
679 { | |
680 for (int i = 0; i < members.dim; i++) | |
681 { | |
682 Dsymbol s = cast(Dsymbol)members.data[i]; | |
683 s.inlineScan(); | |
684 } | |
685 } | |
686 } | |
687 | |
688 void toCBuffer(OutBuffer buf, HdrGenState* hgs) | |
689 { | |
690 int i; | |
691 | |
692 Identifier id = name; | |
693 buf.writestring(id.toChars()); | |
694 buf.writestring("!("); | |
695 if (nest) | |
696 buf.writestring("..."); | |
697 else | |
698 { | |
699 nest++; | |
700 Objects args = tiargs; | |
701 for (i = 0; i < args.dim; i++) | |
702 { | |
703 if (i) | |
704 buf.writeByte(','); | |
705 Object oarg = cast(Object)args.data[i]; | |
706 ObjectToCBuffer(buf, hgs, oarg); | |
707 } | |
708 nest--; | |
709 } | |
710 buf.writeByte(')'); | |
711 } | |
712 | |
713 Dsymbol toAlias() // resolve real symbol | |
714 { | |
715 version (LOG) { | |
20
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
716 printf("TemplateInstance.toAlias()\n"); |
0 | 717 } |
718 if (!inst) | |
719 { | |
720 error("cannot resolve forward reference"); | |
721 return this; | |
722 } | |
723 | |
724 if (inst !is this) | |
725 return inst.toAlias(); | |
726 | |
727 if (aliasdecl) | |
728 { | |
729 return aliasdecl.toAlias(); | |
730 } | |
731 | |
732 return inst; | |
733 } | |
734 | |
735 string kind() | |
736 { | |
20
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
737 return "template instance"; |
0 | 738 } |
739 | |
740 bool oneMember(Dsymbol* ps) | |
741 { | |
20
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
742 *ps = null; |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
743 return true; |
0 | 744 } |
745 | |
746 string toChars() | |
747 { | |
748 scope OutBuffer buf = new OutBuffer(); | |
749 HdrGenState hgs; | |
750 | |
751 toCBuffer(buf, &hgs); | |
752 return buf.extractString(); | |
753 } | |
754 | |
755 string mangle() | |
756 { | |
20
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
757 OutBuffer buf = new OutBuffer(); |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
758 string id; |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
759 |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
760 static if (0) { |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
761 printf("TemplateInstance.mangle() %s", toChars()); |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
762 if (parent) |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
763 printf(" parent = %s %s", parent.kind(), parent.toChars()); |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
764 printf("\n"); |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
765 } |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
766 id = ident ? ident.toChars() : toChars(); |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
767 if (!tempdecl) |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
768 error("is not defined"); |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
769 else if (tempdecl.parent) |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
770 { |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
771 string p = tempdecl.parent.mangle(); |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
772 if (p[0] == '_' && p[1] == 'D') |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
773 p = p[2..$]; |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
774 buf.writestring(p); |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
775 } |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
776 buf.printf("%d%s", id.length, id); |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
777 id = buf.toChars(); |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
778 buf.data = null; |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
779 //printf("TemplateInstance.mangle() %s = %s\n", toChars(), id); |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
780 return id; |
0 | 781 } |
782 | |
783 void printInstantiationTrace() | |
784 { | |
20
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
785 if (global.gag) |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
786 return; |
0 | 787 } |
788 | |
789 void toObjFile(int multiobj) // compile to .obj file | |
790 { | |
791 version (LOG) { | |
792 printf("TemplateInstance.toObjFile('%s', this = %p)\n", toChars(), this); | |
793 } | |
794 if (!errors && members) | |
795 { | |
796 if (multiobj) | |
797 // Append to list of object files to be written later | |
798 obj_append(this); | |
799 else | |
800 { | |
801 for (int i = 0; i < members.dim; i++) | |
802 { | |
803 Dsymbol s = cast(Dsymbol)members.data[i]; | |
804 s.toObjFile(multiobj); | |
805 } | |
806 } | |
807 } | |
808 } | |
809 | |
810 // Internal | |
811 /********************************** | |
812 * Input: | |
813 * flags 1: replace const variables with their initializers | |
814 */ | |
815 static void semanticTiargs(Loc loc, Scope sc, Objects tiargs, int flags) | |
816 { | |
817 // Run semantic on each argument, place results in tiargs[] | |
818 //printf("+TemplateInstance.semanticTiargs() %s\n", toChars()); | |
819 if (!tiargs) | |
820 return; | |
821 for (size_t j = 0; j < tiargs.dim; j++) | |
822 { | |
823 Object o = cast(Object)tiargs.data[j]; | |
824 Type ta = isType(o); | |
825 Expression ea = isExpression(o); | |
826 Dsymbol sa = isDsymbol(o); | |
827 | |
828 //printf("1: tiargs.data[%d] = %p, %p, %p, ea=%p, ta=%p\n", j, o, isDsymbol(o), isTuple(o), ea, ta); | |
829 if (ta) | |
830 { | |
831 //printf("type %s\n", ta.toChars()); | |
832 // It might really be an Expression or an Alias | |
833 ta.resolve(loc, sc, &ea, &ta, &sa); | |
834 if (ea) | |
835 { | |
836 ea = ea.semantic(sc); | |
837 /* This test is to skip substituting a const var with | |
838 * its initializer. The problem is the initializer won't | |
839 * match with an 'alias' parameter. Instead, do the | |
840 * const substitution in TemplateValueParameter.matchArg(). | |
841 */ | |
842 if (ea.op != TOKvar || flags & 1) | |
843 ea = ea.optimize(WANTvalue | WANTinterpret); | |
844 | |
845 tiargs.data[j] = cast(void*)ea; | |
846 } | |
847 else if (sa) | |
848 { | |
849 tiargs.data[j] = cast(void*)sa; | |
850 TupleDeclaration d = sa.toAlias().isTupleDeclaration(); | |
851 if (d) | |
852 { | |
853 size_t dim = d.objects.dim; | |
854 tiargs.remove(j); | |
855 tiargs.insert(j, d.objects); | |
856 j--; | |
857 } | |
858 } | |
859 else if (ta) | |
860 { | |
861 Ltype: | |
862 if (ta.ty == Ttuple) | |
863 { | |
864 // Expand tuple | |
865 TypeTuple tt = cast(TypeTuple)ta; | |
866 size_t dim = tt.arguments.dim; | |
867 tiargs.remove(j); | |
868 if (dim) | |
869 { | |
870 tiargs.reserve(dim); | |
871 for (size_t i = 0; i < dim; i++) | |
872 { | |
873 Argument arg = cast(Argument)tt.arguments.data[i]; | |
874 tiargs.insert(j + i, cast(void*)arg.type); | |
875 } | |
876 } | |
877 j--; | |
878 } | |
879 else | |
880 tiargs.data[j] = cast(void*)ta; | |
881 } | |
882 else | |
883 { | |
884 assert(global.errors); | |
885 tiargs.data[j] = cast(void*)Type.terror; | |
886 } | |
887 } | |
888 else if (ea) | |
889 { | |
890 if (!ea) | |
891 { | |
892 assert(global.errors); | |
893 ea = new IntegerExp(0); | |
894 } | |
895 assert(ea); | |
896 ea = ea.semantic(sc); | |
897 if (ea.op != TOKvar || flags & 1) | |
898 ea = ea.optimize(WANTvalue | WANTinterpret); | |
899 tiargs.data[j] = cast(void*)ea; | |
900 if (ea.op == TOKtype) | |
901 { | |
902 ta = ea.type; | |
903 goto Ltype; | |
904 } | |
905 if (ea.op == TOKtuple) | |
906 { | |
907 // Expand tuple | |
908 TupleExp te = cast(TupleExp)ea; | |
909 size_t dim = te.exps.dim; | |
910 tiargs.remove(j); | |
911 if (dim) | |
912 { | |
913 tiargs.reserve(dim); | |
914 for (size_t i = 0; i < dim; i++) | |
915 tiargs.insert(j + i, te.exps.data[i]); | |
916 } | |
917 j--; | |
918 } | |
919 } | |
920 else if (sa) | |
921 { | |
922 TemplateDeclaration td = sa.isTemplateDeclaration(); | |
923 if (td && !td.semanticRun && td.literal) | |
924 td.semantic(sc); | |
925 } | |
926 else | |
927 { | |
928 assert(0); | |
929 } | |
930 //printf("1: tiargs.data[%d] = %p\n", j, tiargs.data[j]); | |
931 } | |
932 | |
933 static if (false) { | |
934 printf("-TemplateInstance.semanticTiargs('%s', this=%p)\n", toChars(), this); | |
935 for (size_t j = 0; j < tiargs.dim; j++) | |
936 { | |
937 Object o = cast(Object)tiargs.data[j]; | |
938 Type ta = isType(o); | |
939 Expression ea = isExpression(o); | |
940 Dsymbol sa = isDsymbol(o); | |
941 Tuple va = isTuple(o); | |
942 | |
943 printf("\ttiargs[%d] = ta %p, ea %p, sa %p, va %p\n", j, ta, ea, sa, va); | |
944 } | |
945 } | |
946 } | |
947 | |
948 void semanticTiargs(Scope sc) | |
949 { | |
950 //printf("+TemplateInstance.semanticTiargs() %s\n", toChars()); | |
951 if (semantictiargsdone) | |
952 return; | |
953 | |
954 semantictiargsdone = 1; | |
955 semanticTiargs(loc, sc, tiargs, 0); | |
956 } | |
957 | |
958 /********************************************** | |
959 * Find template declaration corresponding to template instance. | |
960 */ | |
961 TemplateDeclaration findTemplateDeclaration(Scope sc) | |
962 { | |
963 //printf("TemplateInstance.findTemplateDeclaration() %s\n", toChars()); | |
964 if (!tempdecl) | |
965 { | |
966 /* Given: | |
967 * foo!( ... ) | |
968 * figure out which TemplateDeclaration foo refers to. | |
969 */ | |
970 Dsymbol s; | |
971 Dsymbol scopesym; | |
972 int i; | |
973 | |
974 Identifier id = name; | |
975 s = sc.search(loc, id, &scopesym); | |
976 if (!s) | |
977 { | |
978 error("identifier '%s' is not defined", id.toChars()); | |
979 return null; | |
980 } | |
981 version (LOG) { | |
982 printf("It's an instance of '%s' kind '%s'\n", s.toChars(), s.kind()); | |
983 if (s.parent) | |
984 printf("s.parent = '%s'\n", s.parent.toChars()); | |
985 } | |
986 withsym = scopesym.isWithScopeSymbol(); | |
987 | |
988 /* We might have found an alias within a template when | |
989 * we really want the template. | |
990 */ | |
991 TemplateInstance ti; | |
992 if (s.parent && | |
993 (ti = s.parent.isTemplateInstance()) !is null) | |
994 { | |
995 if ( | |
996 (ti.name == id || | |
997 ti.toAlias().ident == id) | |
998 && | |
999 ti.tempdecl | |
1000 ) | |
1001 { | |
1002 /* This is so that one can refer to the enclosing | |
1003 * template, even if it has the same name as a member | |
1004 * of the template, if it has a !(arguments) | |
1005 */ | |
1006 tempdecl = ti.tempdecl; | |
1007 if (tempdecl.overroot) // if not start of overloaded list of TemplateDeclaration's | |
1008 tempdecl = tempdecl.overroot; // then get the start | |
1009 | |
1010 s = tempdecl; | |
1011 } | |
1012 } | |
1013 | |
1014 s = s.toAlias(); | |
1015 | |
1016 /* It should be a TemplateDeclaration, not some other symbol | |
1017 */ | |
1018 tempdecl = s.isTemplateDeclaration(); | |
1019 if (!tempdecl) | |
1020 { | |
1021 if (!s.parent && global.errors) | |
1022 return null; | |
1023 if (!s.parent && s.getType()) | |
1024 { | |
1025 Dsymbol s2 = s.getType().toDsymbol(sc); | |
1026 if (!s2) | |
1027 { | |
1028 error("%s is not a template declaration, it is a %s", id.toChars(), s.kind()); | |
1029 return null; | |
1030 } | |
1031 s = s2; | |
1032 } | |
1033 debug { | |
1034 //if (!s.parent) printf("s = %s %s\n", s.kind(), s.toChars()); | |
1035 } | |
1036 //assert(s.parent); | |
1037 TemplateInstance ti2 = s.parent ? s.parent.isTemplateInstance() : null; | |
1038 if (ti2 && | |
1039 (ti2.name == id || | |
1040 ti2.toAlias().ident == id) | |
1041 && | |
1042 ti2.tempdecl | |
1043 ) | |
1044 { | |
1045 /* This is so that one can refer to the enclosing | |
1046 * template, even if it has the same name as a member | |
1047 * of the template, if it has a !(arguments) | |
1048 */ | |
1049 tempdecl = ti2.tempdecl; | |
1050 if (tempdecl.overroot) // if not start of overloaded list of TemplateDeclaration's | |
1051 tempdecl = tempdecl.overroot; // then get the start | |
1052 } | |
1053 else | |
1054 { | |
1055 error("%s is not a template declaration, it is a %s", id.toChars(), s.kind()); | |
1056 return null; | |
1057 } | |
1058 } | |
1059 } | |
1060 else | |
1061 assert(tempdecl.isTemplateDeclaration()); | |
1062 | |
1063 return tempdecl; | |
1064 } | |
1065 | |
1066 TemplateDeclaration findBestMatch(Scope sc) | |
1067 { | |
1068 /* Since there can be multiple TemplateDeclaration's with the same | |
1069 * name, look for the best match. | |
1070 */ | |
1071 TemplateDeclaration td_ambig = null; | |
1072 TemplateDeclaration td_best = null; | |
1073 MATCH m_best = MATCHnomatch; | |
1074 scope Objects dedtypes = new Objects(); | |
1075 | |
1076 version (LOG) { | |
1077 printf("TemplateInstance.findBestMatch()\n"); | |
1078 } | |
1079 // First look for forward references | |
1080 for (TemplateDeclaration td = tempdecl; td; td = td.overnext) | |
1081 { | |
1082 if (!td.semanticRun) | |
1083 { | |
1084 if (td.scope_) | |
1085 { | |
1086 // Try to fix forward reference | |
1087 td.semantic(td.scope_); | |
1088 } | |
1089 if (!td.semanticRun) | |
1090 { | |
1091 error("%s forward references template declaration %s\n", toChars(), td.toChars()); | |
1092 return null; | |
1093 } | |
1094 } | |
1095 } | |
1096 | |
1097 for (TemplateDeclaration td = tempdecl; td; td = td.overnext) | |
1098 { | |
1099 MATCH m; | |
1100 | |
1101 //if (tiargs.dim) printf("2: tiargs.dim = %d, data[0] = %p\n", tiargs.dim, tiargs.data[0]); | |
1102 | |
1103 // If more arguments than parameters, | |
1104 // then this is no match. | |
1105 if (td.parameters.dim < tiargs.dim) | |
1106 { | |
1107 if (!td.isVariadic()) | |
1108 continue; | |
1109 } | |
1110 | |
1111 dedtypes.setDim(td.parameters.dim); | |
1112 dedtypes.zero(); | |
1113 assert(td.semanticRun); | |
1114 m = td.matchWithInstance(this, dedtypes, 0); | |
1115 //printf("matchWithInstance = %d\n", m); | |
1116 if (!m) // no match at all | |
1117 continue; | |
1118 | |
1119 if (m < m_best) | |
1120 goto Ltd_best; | |
1121 if (m > m_best) | |
1122 goto Ltd; | |
1123 | |
1124 { | |
1125 // Disambiguate by picking the most specialized TemplateDeclaration | |
1126 MATCH c1 = td.leastAsSpecialized(td_best); | |
1127 MATCH c2 = td_best.leastAsSpecialized(td); | |
1128 //printf("c1 = %d, c2 = %d\n", c1, c2); | |
1129 | |
1130 if (c1 > c2) | |
1131 goto Ltd; | |
1132 else if (c1 < c2) | |
1133 goto Ltd_best; | |
1134 else | |
1135 goto Lambig; | |
1136 } | |
1137 | |
1138 Lambig: // td_best and td are ambiguous | |
1139 td_ambig = td; | |
1140 continue; | |
1141 | |
1142 Ltd_best: // td_best is the best match so far | |
1143 td_ambig = null; | |
1144 continue; | |
1145 | |
1146 Ltd: // td is the new best match | |
1147 td_ambig = null; | |
1148 td_best = td; | |
1149 m_best = m; | |
1150 tdtypes.setDim(dedtypes.dim); | |
1151 memcpy(tdtypes.data, dedtypes.data, tdtypes.dim * (void*).sizeof); | |
1152 continue; | |
1153 } | |
1154 | |
1155 if (!td_best) | |
1156 { | |
1157 if (tempdecl && !tempdecl.overnext) | |
1158 // Only one template, so we can give better error message | |
1159 error("%s does not match template declaration %s", toChars(), tempdecl.toChars()); | |
1160 else | |
1161 error("%s does not match any template declaration", toChars()); | |
1162 return null; | |
1163 } | |
1164 | |
1165 if (td_ambig) | |
1166 { | |
1167 error("%s matches more than one template declaration, %s and %s", | |
1168 toChars(), td_best.toChars(), td_ambig.toChars()); | |
1169 } | |
1170 | |
1171 /* The best match is td_best | |
1172 */ | |
1173 tempdecl = td_best; | |
1174 | |
1175 static if (false) { | |
1176 /* Cast any value arguments to be same type as value parameter | |
1177 */ | |
1178 for (size_t i = 0; i < tiargs.dim; i++) | |
1179 { | |
1180 Object o = cast(Object)tiargs.data[i]; | |
1181 Expression ea = isExpression(o); // value argument | |
1182 TemplateParameter tp = cast(TemplateParameter)tempdecl.parameters.data[i]; | |
1183 assert(tp); | |
1184 TemplateValueParameter tvp = tp.isTemplateValueParameter(); | |
1185 if (tvp) | |
1186 { | |
1187 assert(ea); | |
1188 ea = ea.castTo(tvp.valType); | |
1189 ea = ea.optimize(WANTvalue | WANTinterpret); | |
1190 tiargs.data[i] = cast(Object)ea; | |
1191 } | |
1192 } | |
1193 } | |
1194 | |
1195 version (LOG) { | |
1196 printf("\tIt's a match with template declaration '%s'\n", tempdecl.toChars()); | |
1197 } | |
1198 return tempdecl; | |
1199 } | |
1200 | |
1201 /**************************************************** | |
1202 * Declare parameters of template instance, initialize them with the | |
1203 * template instance arguments. | |
1204 */ | |
1205 void declareParameters(Scope sc) | |
1206 { | |
20
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
1207 //printf("TemplateInstance.declareParameters()\n"); |
0 | 1208 for (int i = 0; i < tdtypes.dim; i++) |
1209 { | |
1210 TemplateParameter tp = cast(TemplateParameter)tempdecl.parameters.data[i]; | |
1211 //Object o = cast(Object)tiargs.data[i]; | |
1212 Object o = cast(Object)tdtypes.data[i]; // initializer for tp | |
1213 | |
1214 //printf("\ttdtypes[%d] = %p\n", i, o); | |
1215 tempdecl.declareParameter(sc, tp, o); | |
1216 } | |
1217 } | |
1218 | |
1219 /***************************************** | |
1220 * Determines if a TemplateInstance will need a nested | |
1221 * generation of the TemplateDeclaration. | |
1222 */ | |
1223 bool hasNestedArgs(Objects args) | |
1224 { | |
1225 bool nested = false; | |
20
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
1226 //printf("TemplateInstance.hasNestedArgs('%s')\n", tempdecl.ident.toChars()); |
0 | 1227 |
1228 /* A nested instance happens when an argument references a local | |
1229 * symbol that is on the stack. | |
1230 */ | |
1231 for (size_t i = 0; i < args.dim; i++) | |
1232 { | |
1233 Object o = cast(Object)args.data[i]; | |
1234 Expression ea = isExpression(o); | |
1235 Dsymbol sa = isDsymbol(o); | |
1236 Tuple va = isTuple(o); | |
1237 if (ea) | |
1238 { | |
1239 if (ea.op == TOKvar) | |
1240 { | |
1241 sa = (cast(VarExp)ea).var; | |
1242 goto Lsa; | |
1243 } | |
1244 if (ea.op == TOKfunction) | |
1245 { | |
1246 sa = (cast(FuncExp)ea).fd; | |
1247 goto Lsa; | |
1248 } | |
1249 } | |
1250 else if (sa) | |
1251 { | |
1252 Lsa: | |
1253 Declaration d = null; | |
1254 TemplateDeclaration td = sa.isTemplateDeclaration(); | |
1255 if (td && td.literal) | |
1256 { | |
1257 goto L2; | |
1258 } | |
1259 d = sa.isDeclaration(); | |
1260 if (d && !d.isDataseg() && | |
1261 /// version (DMDV2) { | |
1262 !(d.storage_class & STCmanifest) && | |
1263 /// } | |
1264 (!d.isFuncDeclaration() || d.isFuncDeclaration().isNested()) && | |
1265 !isTemplateMixin()) | |
1266 { | |
1267 L2: | |
1268 // if module level template | |
1269 if (tempdecl.toParent().isModule()) | |
1270 { | |
1271 Dsymbol dparent = sa.toParent(); | |
1272 if (!isnested) | |
1273 isnested = dparent; | |
1274 else if (isnested != dparent) | |
1275 { | |
1276 /* Select the more deeply nested of the two. | |
1277 * Error if one is not nested inside the other. | |
1278 */ | |
1279 for (Dsymbol p = isnested; p; p = p.parent) | |
1280 { | |
1281 if (p == dparent) | |
1282 goto L1; // isnested is most nested | |
1283 } | |
1284 for (Dsymbol p = dparent; p; p = p.parent) | |
1285 { | |
1286 if (p == isnested) | |
1287 { | |
1288 isnested = dparent; | |
1289 goto L1; // dparent is most nested | |
1290 } | |
1291 } | |
1292 error("%s is nested in both %s and %s", | |
1293 toChars(), isnested.toChars(), dparent.toChars()); | |
1294 } | |
1295 L1: | |
1296 //printf("\tnested inside %s\n", isnested.toChars()); | |
1297 nested |= 1; | |
1298 } | |
1299 else | |
1300 error("cannot use local '%s' as parameter to non-global template %s", d.toChars(), tempdecl.toChars()); | |
1301 } | |
1302 } | |
1303 else if (va) | |
1304 { | |
1305 nested |= hasNestedArgs(va.objects); | |
1306 } | |
1307 } | |
1308 return nested; | |
1309 } | |
1310 | |
1311 /**************************************** | |
1312 * This instance needs an identifier for name mangling purposes. | |
1313 * Create one by taking the template declaration name and adding | |
1314 * the type signature for it. | |
1315 */ | |
1316 Identifier genIdent() | |
1317 { | |
1318 scope OutBuffer buf = new OutBuffer(); | |
1319 string id; | |
1320 Objects args; | |
1321 | |
20
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
1322 //printf("TemplateInstance.genIdent('%s')\n", tempdecl.ident.toChars()); |
0 | 1323 id = tempdecl.ident.toChars(); |
1324 buf.printf("__T%d%s", id.length, id); ///! | |
1325 args = tiargs; | |
1326 for (int i = 0; i < args.dim; i++) | |
1327 { | |
1328 Object o = cast(Object)args.data[i]; | |
1329 Type ta = isType(o); | |
1330 Expression ea = isExpression(o); | |
1331 Dsymbol sa = isDsymbol(o); | |
1332 Tuple va = isTuple(o); | |
1333 //printf("\to [%d] %p ta %p ea %p sa %p va %p\n", i, o, ta, ea, sa, va); | |
1334 if (ta) | |
1335 { | |
1336 buf.writeByte('T'); | |
1337 if (ta.deco) | |
1338 buf.writestring(ta.deco); | |
1339 else | |
1340 { | |
1341 debug writef("ta = %d, %s\n", ta.ty, ta.toChars()); | |
1342 assert(global.errors); | |
1343 } | |
1344 } | |
1345 else if (ea) | |
1346 { | |
1347 Lea: | |
1348 long v; | |
1349 real r; | |
1350 | |
1351 ea = ea.optimize(WANTvalue | WANTinterpret); | |
1352 if (ea.op == TOKvar) | |
1353 { | |
1354 sa = (cast(VarExp)ea).var; | |
1355 ea = null; | |
1356 goto Lsa; | |
1357 } | |
1358 if (ea.op == TOKfunction) | |
1359 { | |
1360 sa = (cast(FuncExp)ea).fd; | |
1361 ea = null; | |
1362 goto Lsa; | |
1363 } | |
1364 buf.writeByte('V'); | |
1365 if (ea.op == TOKtuple) | |
1366 { | |
1367 ea.error("tuple is not a valid template value argument"); | |
1368 continue; | |
1369 } | |
1370 static if (true) { | |
1371 /* Use deco that matches what it would be for a function parameter | |
1372 */ | |
1373 buf.writestring(ea.type.deco); | |
1374 } else { | |
1375 // Use type of parameter, not type of argument | |
1376 TemplateParameter tp = cast(TemplateParameter)tempdecl.parameters.data[i]; | |
1377 assert(tp); | |
1378 TemplateValueParameter tvp = tp.isTemplateValueParameter(); | |
1379 assert(tvp); | |
1380 buf.writestring(tvp.valType.deco); | |
1381 } | |
1382 ea.toMangleBuffer(buf); | |
1383 } | |
1384 else if (sa) | |
1385 { | |
1386 Lsa: | |
1387 buf.writeByte('S'); | |
1388 Declaration d = sa.isDeclaration(); | |
1389 if (d && (!d.type || !d.type.deco)) | |
1390 { | |
1391 error("forward reference of %s", d.toChars()); | |
1392 continue; | |
1393 } | |
1394 static if (false) { | |
1395 VarDeclaration v = sa.isVarDeclaration(); | |
1396 if (v && v.storage_class & STCmanifest) | |
1397 { | |
1398 ExpInitializer ei = v.init.isExpInitializer(); | |
1399 if (ei) | |
1400 { | |
1401 ea = ei.exp; | |
1402 goto Lea; | |
1403 } | |
1404 } | |
1405 } | |
1406 string p = sa.mangle(); | |
62 | 1407 ///buf.printf("%zu%s", p.length, p); |
1408 buf.printf("%su%s", p.length, p); | |
0 | 1409 } |
1410 else if (va) | |
1411 { | |
1412 assert(i + 1 == args.dim); // must be last one | |
1413 args = va.objects; | |
1414 i = -1; | |
1415 } | |
1416 else | |
1417 assert(0); | |
1418 } | |
1419 buf.writeByte('Z'); | |
1420 id = buf.toChars(); | |
1421 buf.data = null; | |
1422 //printf("\tgenIdent = %s\n", id); | |
1423 return new Identifier(id, TOKidentifier); | |
1424 } | |
1425 | |
1426 TemplateInstance isTemplateInstance() { return this; } | |
1427 | |
1428 AliasDeclaration isAliasDeclaration() | |
1429 { | |
1430 assert(false); | |
1431 } | |
20
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
1432 } |