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