Mercurial > projects > ddmd
annotate dmd/TemplateInstance.d @ 128:e6e542f37b94
Some more Array -> Vector conversions
author | Eldar Insafutdinov <e.insafutdinov@gmail.com> |
---|---|
date | Sat, 04 Sep 2010 01:33:05 +0100 |
parents | 1765f3ef917d |
children | 60bb0fe4563e |
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 | |
128
e6e542f37b94
Some more Array -> Vector conversions
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
340 foreach (ti; tempdecl.instances) |
0 | 341 { |
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 { | |
113
3482c73a991b
More cleanup for arrays
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
110
diff
changeset
|
359 Object o1 = tdtypes[j]; |
3482c73a991b
More cleanup for arrays
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
110
diff
changeset
|
360 Object o2 = ti.tdtypes[j]; |
0 | 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; | |
128
e6e542f37b94
Some more Array -> Vector conversions
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
387 tempdecl.instances.push(this); |
0 | 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 { | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
72
diff
changeset
|
404 Dsymbols a; |
0 | 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 { | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
72
diff
changeset
|
443 a.push(this); |
0 | 444 break; |
445 } | |
446 | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
72
diff
changeset
|
447 if (this is a[i]) // if already in Array |
0 | 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; |
77
ad4792a1cfd6
more D-ification container accessing
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
74
diff
changeset
|
482 foreach(Dsymbol s; members) |
0 | 483 { |
484 version (LOG) { | |
485 printf("\t[%d] adding member '%s' %p kind %s to '%s', memnum = %d\n", i, s.toChars(), s, s.kind(), this.toChars(), memnum); | |
486 } | |
487 memnum |= s.addMember(scope_, this, memnum); | |
488 } | |
489 | |
490 version (LOG) { | |
491 printf("adding members done\n"); | |
492 } | |
493 | |
494 /* See if there is only one member of template instance, and that | |
495 * member has the same name as the template instance. | |
496 * If so, this template instance becomes an alias for that member. | |
497 */ | |
498 //printf("members.dim = %d\n", members.dim); | |
499 if (members.dim) | |
500 { | |
501 Dsymbol s; | |
502 if (Dsymbol.oneMembers(members, &s) && s) | |
503 { | |
504 //printf("s.kind = '%s'\n", s.kind()); | |
505 //s.print(); | |
506 //printf("'%s', '%s'\n", s.ident.toChars(), tempdecl.ident.toChars()); | |
507 if (s.ident && s.ident.equals(tempdecl.ident)) | |
508 { | |
509 //printf("setting aliasdecl\n"); | |
510 aliasdecl = new AliasDeclaration(loc, s.ident, s); | |
511 } | |
512 } | |
513 } | |
514 | |
515 // Do semantic() analysis on template instance members | |
516 version (LOG) { | |
517 printf("\tdo semantic() on template instance members '%s'\n", toChars()); | |
518 } | |
519 Scope sc2; | |
520 sc2 = scope_.push(this); | |
521 //printf("isnested = %d, sc.parent = %s\n", isnested, sc.parent.toChars()); | |
522 sc2.parent = /*isnested ? sc.parent :*/ this; | |
523 sc2.tinst = this; | |
524 | |
525 try | |
526 { | |
79 | 527 // static int nest; |
0 | 528 //printf("%d\n", nest); |
529 if (++nest > 500) | |
530 { | |
531 global.gag = 0; // ensure error message gets printed | |
532 error("recursive expansion"); | |
533 fatal(); | |
534 } | |
77
ad4792a1cfd6
more D-ification container accessing
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
74
diff
changeset
|
535 foreach(Dsymbol s; members) |
0 | 536 { |
537 //printf("\t[%d] semantic on '%s' %p kind %s in '%s'\n", i, s.toChars(), s, s.kind(), this.toChars()); | |
538 //printf("test: isnested = %d, sc2.parent = %s\n", isnested, sc2.parent.toChars()); | |
539 // if (isnested) | |
540 // s.parent = sc.parent; | |
541 //printf("test3: isnested = %d, s.parent = %s\n", isnested, s.parent.toChars()); | |
542 s.semantic(sc2); | |
543 //printf("test4: isnested = %d, s.parent = %s\n", isnested, s.parent.toChars()); | |
544 sc2.module_.runDeferredSemantic(); | |
545 } | |
546 --nest; | |
547 } | |
20
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
548 catch (Exception e) |
0 | 549 { |
550 global.gag = 0; // ensure error message gets printed | |
551 error("recursive expansion"); | |
552 fatal(); | |
553 } | |
554 | |
555 /* If any of the instantiation members didn't get semantic() run | |
556 * on them due to forward references, we cannot run semantic2() | |
557 * or semantic3() yet. | |
558 */ | |
559 for (size_t i = 0; i < Module.deferred.dim; i++) | |
560 { | |
561 Dsymbol sd = cast(Dsymbol)Module.deferred.data[i]; | |
562 | |
563 if (sd.parent is this) | |
564 goto Laftersemantic; | |
565 } | |
566 | |
567 /* The problem is when to parse the initializer for a variable. | |
568 * Perhaps VarDeclaration.semantic() should do it like it does | |
569 * for initializers inside a function. | |
570 */ | |
571 // if (sc.parent.isFuncDeclaration()) | |
572 | |
573 /* BUG 782: this has problems if the classes this depends on | |
574 * are forward referenced. Find a way to defer semantic() | |
575 * on this template. | |
576 */ | |
577 semantic2(sc2); | |
578 | |
579 if (sc.func || dosemantic3) | |
580 { | |
79 | 581 try |
582 { | |
583 // static int nest; // TODO: | |
584 if (++nest > 300) | |
585 { | |
586 global.gag = 0; // ensure error message gets printed | |
587 error("recursive expansion"); | |
588 fatal(); | |
589 } | |
590 semantic3(sc2); | |
591 --nest; | |
592 } | |
593 catch (Exception e) | |
594 { | |
595 global.gag = 0; // ensure error message gets printed | |
596 error("recursive expansion"); | |
597 fatal(); | |
598 } | |
0 | 599 } |
600 | |
601 Laftersemantic: | |
602 sc2.pop(); | |
603 | |
604 scope_.pop(); | |
605 | |
606 // Give additional context info if error occurred during instantiation | |
607 if (global.errors != errorsave) | |
608 { | |
609 error("error instantiating"); | |
610 if (tinst && !global.gag) | |
611 { | |
612 tinst.printInstantiationTrace(); | |
613 fatal(); | |
614 } | |
615 errors = 1; | |
616 if (global.gag) | |
617 tempdecl.instances.remove(tempdecl_instance_idx); | |
618 } | |
619 | |
620 version (LOG) { | |
621 printf("-TemplateInstance.semantic('%s', this=%p)\n", toChars(), this); | |
622 } | |
623 } | |
624 | |
72 | 625 override void semantic2(Scope sc) |
0 | 626 { |
627 if (semanticRun >= 2) | |
628 return; | |
629 | |
630 semanticRun = 2; | |
631 version (LOG) { | |
20
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
632 printf("+TemplateInstance.semantic2('%s')\n", toChars()); |
0 | 633 } |
634 | |
635 if (!errors && members) | |
636 { | |
637 sc = tempdecl.scope_; | |
638 assert(sc); | |
639 sc = sc.push(argsym); | |
640 sc = sc.push(this); | |
641 sc.tinst = this; | |
642 | |
77
ad4792a1cfd6
more D-ification container accessing
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
74
diff
changeset
|
643 foreach(Dsymbol s; members) |
0 | 644 { |
645 version (LOG) { | |
646 printf("\tmember '%s', kind = '%s'\n", s.toChars(), s.kind()); | |
647 } | |
648 s.semantic2(sc); | |
649 } | |
650 | |
651 sc = sc.pop(); | |
652 sc.pop(); | |
653 } | |
654 | |
655 version (LOG) { | |
20
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
656 printf("-TemplateInstance.semantic2('%s')\n", toChars()); |
0 | 657 } |
658 } | |
659 | |
72 | 660 override void semantic3(Scope sc) |
0 | 661 { |
662 version (LOG) { | |
663 printf("TemplateInstance.semantic3('%s'), semanticRun = %d\n", toChars(), semanticRun); | |
664 } | |
665 //if (toChars()[0] == 'D') *(char*)0=0; | |
666 if (semanticRun >= 3) | |
667 return; | |
668 semanticRun = 3; | |
669 if (!errors && members) | |
670 { | |
671 sc = tempdecl.scope_; | |
672 sc = sc.push(argsym); | |
673 sc = sc.push(this); | |
674 sc.tinst = this; | |
77
ad4792a1cfd6
more D-ification container accessing
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
74
diff
changeset
|
675 foreach(Dsymbol s; members) |
0 | 676 s.semantic3(sc); |
677 sc = sc.pop(); | |
678 sc.pop(); | |
679 } | |
680 } | |
681 | |
72 | 682 override void inlineScan() |
0 | 683 { |
684 version (LOG) { | |
685 printf("TemplateInstance.inlineScan('%s')\n", toChars()); | |
686 } | |
687 if (!errors && members) | |
688 { | |
77
ad4792a1cfd6
more D-ification container accessing
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
74
diff
changeset
|
689 foreach(Dsymbol s; members) |
0 | 690 s.inlineScan(); |
691 } | |
692 } | |
693 | |
72 | 694 override void toCBuffer(OutBuffer buf, HdrGenState* hgs) |
0 | 695 { |
696 int i; | |
697 | |
698 Identifier id = name; | |
699 buf.writestring(id.toChars()); | |
700 buf.writestring("!("); | |
701 if (nest) | |
702 buf.writestring("..."); | |
703 else | |
704 { | |
705 nest++; | |
706 Objects args = tiargs; | |
707 for (i = 0; i < args.dim; i++) | |
708 { | |
709 if (i) | |
710 buf.writeByte(','); | |
113
3482c73a991b
More cleanup for arrays
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
110
diff
changeset
|
711 Object oarg = args[i]; |
0 | 712 ObjectToCBuffer(buf, hgs, oarg); |
713 } | |
714 nest--; | |
715 } | |
716 buf.writeByte(')'); | |
717 } | |
718 | |
72 | 719 override Dsymbol toAlias() // resolve real symbol |
0 | 720 { |
79 | 721 version (LOG) |
722 { | |
723 writef("TemplateInstance.toAlias()\n"); | |
0 | 724 } |
725 if (!inst) | |
726 { | |
727 error("cannot resolve forward reference"); | |
79 | 728 errors = 1; |
0 | 729 return this; |
730 } | |
731 | |
732 if (inst !is this) | |
733 return inst.toAlias(); | |
734 | |
735 if (aliasdecl) | |
736 { | |
737 return aliasdecl.toAlias(); | |
738 } | |
739 | |
740 return inst; | |
741 } | |
742 | |
72 | 743 override string kind() |
0 | 744 { |
20
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
745 return "template instance"; |
0 | 746 } |
747 | |
72 | 748 override bool oneMember(Dsymbol* ps) |
0 | 749 { |
20
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
750 *ps = null; |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
751 return true; |
0 | 752 } |
753 | |
72 | 754 override string toChars() |
0 | 755 { |
756 scope OutBuffer buf = new OutBuffer(); | |
757 HdrGenState hgs; | |
758 | |
759 toCBuffer(buf, &hgs); | |
760 return buf.extractString(); | |
761 } | |
762 | |
72 | 763 override string mangle() |
0 | 764 { |
20
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
765 OutBuffer buf = new OutBuffer(); |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
766 string id; |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
767 |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
768 static if (0) { |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
769 printf("TemplateInstance.mangle() %s", toChars()); |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
770 if (parent) |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
771 printf(" parent = %s %s", parent.kind(), parent.toChars()); |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
772 printf("\n"); |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
773 } |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
774 id = ident ? ident.toChars() : toChars(); |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
775 if (!tempdecl) |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
776 error("is not defined"); |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
777 else if (tempdecl.parent) |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
778 { |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
779 string p = tempdecl.parent.mangle(); |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
780 if (p[0] == '_' && p[1] == 'D') |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
781 p = p[2..$]; |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
782 buf.writestring(p); |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
783 } |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
784 buf.printf("%d%s", id.length, id); |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
785 id = buf.toChars(); |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
786 buf.data = null; |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
787 //printf("TemplateInstance.mangle() %s = %s\n", toChars(), id); |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
788 return id; |
0 | 789 } |
790 | |
791 void printInstantiationTrace() | |
792 { | |
20
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
793 if (global.gag) |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
794 return; |
0 | 795 } |
796 | |
72 | 797 override void toObjFile(int multiobj) // compile to .obj file |
0 | 798 { |
799 version (LOG) { | |
800 printf("TemplateInstance.toObjFile('%s', this = %p)\n", toChars(), this); | |
801 } | |
802 if (!errors && members) | |
803 { | |
804 if (multiobj) | |
805 // Append to list of object files to be written later | |
806 obj_append(this); | |
807 else | |
808 { | |
77
ad4792a1cfd6
more D-ification container accessing
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
74
diff
changeset
|
809 foreach(Dsymbol s; members) |
0 | 810 s.toObjFile(multiobj); |
811 } | |
812 } | |
813 } | |
814 | |
815 // Internal | |
816 /********************************** | |
817 * Input: | |
818 * flags 1: replace const variables with their initializers | |
819 */ | |
820 static void semanticTiargs(Loc loc, Scope sc, Objects tiargs, int flags) | |
821 { | |
822 // Run semantic on each argument, place results in tiargs[] | |
823 //printf("+TemplateInstance.semanticTiargs() %s\n", toChars()); | |
824 if (!tiargs) | |
825 return; | |
826 for (size_t j = 0; j < tiargs.dim; j++) | |
827 { | |
113
3482c73a991b
More cleanup for arrays
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
110
diff
changeset
|
828 Object o = tiargs[j]; |
0 | 829 Type ta = isType(o); |
830 Expression ea = isExpression(o); | |
831 Dsymbol sa = isDsymbol(o); | |
832 | |
833 //printf("1: tiargs.data[%d] = %p, %p, %p, ea=%p, ta=%p\n", j, o, isDsymbol(o), isTuple(o), ea, ta); | |
834 if (ta) | |
835 { | |
836 //printf("type %s\n", ta.toChars()); | |
837 // It might really be an Expression or an Alias | |
838 ta.resolve(loc, sc, &ea, &ta, &sa); | |
839 if (ea) | |
840 { | |
841 ea = ea.semantic(sc); | |
842 /* This test is to skip substituting a const var with | |
843 * its initializer. The problem is the initializer won't | |
844 * match with an 'alias' parameter. Instead, do the | |
845 * const substitution in TemplateValueParameter.matchArg(). | |
846 */ | |
847 if (ea.op != TOKvar || flags & 1) | |
848 ea = ea.optimize(WANTvalue | WANTinterpret); | |
849 | |
94
3a0b150c9841
Objects -> Vector!Object iteration 1
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
91
diff
changeset
|
850 tiargs[j] = ea; |
0 | 851 } |
852 else if (sa) | |
853 { | |
94
3a0b150c9841
Objects -> Vector!Object iteration 1
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
91
diff
changeset
|
854 tiargs[j] = sa; |
0 | 855 TupleDeclaration d = sa.toAlias().isTupleDeclaration(); |
856 if (d) | |
857 { | |
858 size_t dim = d.objects.dim; | |
859 tiargs.remove(j); | |
860 tiargs.insert(j, d.objects); | |
861 j--; | |
862 } | |
863 } | |
864 else if (ta) | |
865 { | |
866 Ltype: | |
867 if (ta.ty == Ttuple) | |
868 { | |
869 // Expand tuple | |
126
1765f3ef917d
ClassDeclarations, Arguments -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
121
diff
changeset
|
870 auto tt = cast(TypeTuple)ta; |
0 | 871 size_t dim = tt.arguments.dim; |
872 tiargs.remove(j); | |
873 if (dim) | |
874 { | |
875 tiargs.reserve(dim); | |
876 for (size_t i = 0; i < dim; i++) | |
877 { | |
126
1765f3ef917d
ClassDeclarations, Arguments -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
121
diff
changeset
|
878 auto arg = tt.arguments[i]; |
94
3a0b150c9841
Objects -> Vector!Object iteration 1
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
91
diff
changeset
|
879 tiargs.insert(j + i, arg.type); |
0 | 880 } |
881 } | |
882 j--; | |
883 } | |
884 else | |
94
3a0b150c9841
Objects -> Vector!Object iteration 1
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
91
diff
changeset
|
885 tiargs[j] = ta; |
0 | 886 } |
887 else | |
888 { | |
889 assert(global.errors); | |
94
3a0b150c9841
Objects -> Vector!Object iteration 1
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
91
diff
changeset
|
890 tiargs[j] = Type.terror; |
0 | 891 } |
892 } | |
893 else if (ea) | |
894 { | |
895 if (!ea) | |
896 { | |
897 assert(global.errors); | |
898 ea = new IntegerExp(0); | |
899 } | |
900 assert(ea); | |
901 ea = ea.semantic(sc); | |
902 if (ea.op != TOKvar || flags & 1) | |
903 ea = ea.optimize(WANTvalue | WANTinterpret); | |
94
3a0b150c9841
Objects -> Vector!Object iteration 1
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
91
diff
changeset
|
904 tiargs[j] = ea; |
0 | 905 if (ea.op == TOKtype) |
906 { | |
907 ta = ea.type; | |
908 goto Ltype; | |
909 } | |
910 if (ea.op == TOKtuple) | |
911 { | |
912 // Expand tuple | |
90
39648eb578f6
more Expressions work
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
84
diff
changeset
|
913 auto te = cast(TupleExp)ea; |
0 | 914 size_t dim = te.exps.dim; |
915 tiargs.remove(j); | |
916 if (dim) | |
917 { | |
918 tiargs.reserve(dim); | |
919 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
|
920 tiargs.insert(j + i, te.exps[i]); |
0 | 921 } |
922 j--; | |
923 } | |
924 } | |
925 else if (sa) | |
926 { | |
927 TemplateDeclaration td = sa.isTemplateDeclaration(); | |
928 if (td && !td.semanticRun && td.literal) | |
929 td.semantic(sc); | |
930 } | |
931 else | |
932 { | |
933 assert(0); | |
934 } | |
935 //printf("1: tiargs.data[%d] = %p\n", j, tiargs.data[j]); | |
936 } | |
937 | |
938 static if (false) { | |
939 printf("-TemplateInstance.semanticTiargs('%s', this=%p)\n", toChars(), this); | |
940 for (size_t j = 0; j < tiargs.dim; j++) | |
941 { | |
942 Object o = cast(Object)tiargs.data[j]; | |
943 Type ta = isType(o); | |
944 Expression ea = isExpression(o); | |
945 Dsymbol sa = isDsymbol(o); | |
946 Tuple va = isTuple(o); | |
947 | |
948 printf("\ttiargs[%d] = ta %p, ea %p, sa %p, va %p\n", j, ta, ea, sa, va); | |
949 } | |
950 } | |
951 } | |
952 | |
953 void semanticTiargs(Scope sc) | |
954 { | |
955 //printf("+TemplateInstance.semanticTiargs() %s\n", toChars()); | |
956 if (semantictiargsdone) | |
957 return; | |
958 | |
959 semantictiargsdone = 1; | |
960 semanticTiargs(loc, sc, tiargs, 0); | |
961 } | |
962 | |
963 /********************************************** | |
964 * Find template declaration corresponding to template instance. | |
965 */ | |
966 TemplateDeclaration findTemplateDeclaration(Scope sc) | |
967 { | |
968 //printf("TemplateInstance.findTemplateDeclaration() %s\n", toChars()); | |
969 if (!tempdecl) | |
970 { | |
971 /* Given: | |
972 * foo!( ... ) | |
973 * figure out which TemplateDeclaration foo refers to. | |
974 */ | |
975 Dsymbol s; | |
976 Dsymbol scopesym; | |
977 int i; | |
978 | |
979 Identifier id = name; | |
980 s = sc.search(loc, id, &scopesym); | |
981 if (!s) | |
982 { | |
983 error("identifier '%s' is not defined", id.toChars()); | |
984 return null; | |
985 } | |
986 version (LOG) { | |
987 printf("It's an instance of '%s' kind '%s'\n", s.toChars(), s.kind()); | |
988 if (s.parent) | |
989 printf("s.parent = '%s'\n", s.parent.toChars()); | |
990 } | |
991 withsym = scopesym.isWithScopeSymbol(); | |
992 | |
993 /* We might have found an alias within a template when | |
994 * we really want the template. | |
995 */ | |
996 TemplateInstance ti; | |
997 if (s.parent && | |
998 (ti = s.parent.isTemplateInstance()) !is null) | |
999 { | |
1000 if ( | |
1001 (ti.name == id || | |
1002 ti.toAlias().ident == id) | |
1003 && | |
1004 ti.tempdecl | |
1005 ) | |
1006 { | |
1007 /* This is so that one can refer to the enclosing | |
1008 * template, even if it has the same name as a member | |
1009 * of the template, if it has a !(arguments) | |
1010 */ | |
1011 tempdecl = ti.tempdecl; | |
1012 if (tempdecl.overroot) // if not start of overloaded list of TemplateDeclaration's | |
1013 tempdecl = tempdecl.overroot; // then get the start | |
1014 | |
1015 s = tempdecl; | |
1016 } | |
1017 } | |
1018 | |
1019 s = s.toAlias(); | |
1020 | |
1021 /* It should be a TemplateDeclaration, not some other symbol | |
1022 */ | |
1023 tempdecl = s.isTemplateDeclaration(); | |
1024 if (!tempdecl) | |
1025 { | |
1026 if (!s.parent && global.errors) | |
1027 return null; | |
1028 if (!s.parent && s.getType()) | |
1029 { | |
1030 Dsymbol s2 = s.getType().toDsymbol(sc); | |
1031 if (!s2) | |
1032 { | |
1033 error("%s is not a template declaration, it is a %s", id.toChars(), s.kind()); | |
1034 return null; | |
1035 } | |
1036 s = s2; | |
1037 } | |
1038 debug { | |
1039 //if (!s.parent) printf("s = %s %s\n", s.kind(), s.toChars()); | |
1040 } | |
1041 //assert(s.parent); | |
1042 TemplateInstance ti2 = s.parent ? s.parent.isTemplateInstance() : null; | |
1043 if (ti2 && | |
1044 (ti2.name == id || | |
1045 ti2.toAlias().ident == id) | |
1046 && | |
1047 ti2.tempdecl | |
1048 ) | |
1049 { | |
1050 /* This is so that one can refer to the enclosing | |
1051 * template, even if it has the same name as a member | |
1052 * of the template, if it has a !(arguments) | |
1053 */ | |
1054 tempdecl = ti2.tempdecl; | |
1055 if (tempdecl.overroot) // if not start of overloaded list of TemplateDeclaration's | |
1056 tempdecl = tempdecl.overroot; // then get the start | |
1057 } | |
1058 else | |
1059 { | |
1060 error("%s is not a template declaration, it is a %s", id.toChars(), s.kind()); | |
1061 return null; | |
1062 } | |
1063 } | |
1064 } | |
1065 else | |
1066 assert(tempdecl.isTemplateDeclaration()); | |
1067 | |
1068 return tempdecl; | |
1069 } | |
1070 | |
1071 TemplateDeclaration findBestMatch(Scope sc) | |
1072 { | |
1073 /* Since there can be multiple TemplateDeclaration's with the same | |
1074 * name, look for the best match. | |
1075 */ | |
1076 TemplateDeclaration td_ambig = null; | |
1077 TemplateDeclaration td_best = null; | |
1078 MATCH m_best = MATCHnomatch; | |
1079 scope Objects dedtypes = new Objects(); | |
1080 | |
1081 version (LOG) { | |
1082 printf("TemplateInstance.findBestMatch()\n"); | |
1083 } | |
1084 // First look for forward references | |
1085 for (TemplateDeclaration td = tempdecl; td; td = td.overnext) | |
1086 { | |
1087 if (!td.semanticRun) | |
1088 { | |
1089 if (td.scope_) | |
1090 { | |
1091 // Try to fix forward reference | |
1092 td.semantic(td.scope_); | |
1093 } | |
1094 if (!td.semanticRun) | |
1095 { | |
1096 error("%s forward references template declaration %s\n", toChars(), td.toChars()); | |
1097 return null; | |
1098 } | |
1099 } | |
1100 } | |
1101 | |
1102 for (TemplateDeclaration td = tempdecl; td; td = td.overnext) | |
1103 { | |
1104 MATCH m; | |
1105 | |
1106 //if (tiargs.dim) printf("2: tiargs.dim = %d, data[0] = %p\n", tiargs.dim, tiargs.data[0]); | |
1107 | |
1108 // If more arguments than parameters, | |
1109 // then this is no match. | |
1110 if (td.parameters.dim < tiargs.dim) | |
1111 { | |
1112 if (!td.isVariadic()) | |
1113 continue; | |
1114 } | |
1115 | |
1116 dedtypes.setDim(td.parameters.dim); | |
1117 dedtypes.zero(); | |
1118 assert(td.semanticRun); | |
1119 m = td.matchWithInstance(this, dedtypes, 0); | |
1120 //printf("matchWithInstance = %d\n", m); | |
1121 if (!m) // no match at all | |
1122 continue; | |
1123 | |
1124 if (m < m_best) | |
1125 goto Ltd_best; | |
1126 if (m > m_best) | |
1127 goto Ltd; | |
1128 | |
1129 { | |
1130 // Disambiguate by picking the most specialized TemplateDeclaration | |
1131 MATCH c1 = td.leastAsSpecialized(td_best); | |
1132 MATCH c2 = td_best.leastAsSpecialized(td); | |
1133 //printf("c1 = %d, c2 = %d\n", c1, c2); | |
1134 | |
1135 if (c1 > c2) | |
1136 goto Ltd; | |
1137 else if (c1 < c2) | |
1138 goto Ltd_best; | |
1139 else | |
1140 goto Lambig; | |
1141 } | |
1142 | |
1143 Lambig: // td_best and td are ambiguous | |
1144 td_ambig = td; | |
1145 continue; | |
1146 | |
1147 Ltd_best: // td_best is the best match so far | |
1148 td_ambig = null; | |
1149 continue; | |
1150 | |
1151 Ltd: // td is the new best match | |
1152 td_ambig = null; | |
1153 td_best = td; | |
1154 m_best = m; | |
1155 tdtypes.setDim(dedtypes.dim); | |
113
3482c73a991b
More cleanup for arrays
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
110
diff
changeset
|
1156 memcpy(tdtypes.ptr, dedtypes.ptr, tdtypes.dim * (void*).sizeof); |
0 | 1157 continue; |
1158 } | |
1159 | |
1160 if (!td_best) | |
1161 { | |
1162 if (tempdecl && !tempdecl.overnext) | |
1163 // Only one template, so we can give better error message | |
1164 error("%s does not match template declaration %s", toChars(), tempdecl.toChars()); | |
1165 else | |
1166 error("%s does not match any template declaration", toChars()); | |
1167 return null; | |
1168 } | |
1169 | |
1170 if (td_ambig) | |
1171 { | |
1172 error("%s matches more than one template declaration, %s and %s", | |
1173 toChars(), td_best.toChars(), td_ambig.toChars()); | |
1174 } | |
1175 | |
1176 /* The best match is td_best | |
1177 */ | |
1178 tempdecl = td_best; | |
1179 | |
1180 static if (false) { | |
1181 /* Cast any value arguments to be same type as value parameter | |
1182 */ | |
1183 for (size_t i = 0; i < tiargs.dim; i++) | |
1184 { | |
1185 Object o = cast(Object)tiargs.data[i]; | |
1186 Expression ea = isExpression(o); // value argument | |
1187 TemplateParameter tp = cast(TemplateParameter)tempdecl.parameters.data[i]; | |
1188 assert(tp); | |
1189 TemplateValueParameter tvp = tp.isTemplateValueParameter(); | |
1190 if (tvp) | |
1191 { | |
1192 assert(ea); | |
1193 ea = ea.castTo(tvp.valType); | |
1194 ea = ea.optimize(WANTvalue | WANTinterpret); | |
1195 tiargs.data[i] = cast(Object)ea; | |
1196 } | |
1197 } | |
1198 } | |
1199 | |
1200 version (LOG) { | |
1201 printf("\tIt's a match with template declaration '%s'\n", tempdecl.toChars()); | |
1202 } | |
1203 return tempdecl; | |
1204 } | |
1205 | |
1206 /**************************************************** | |
1207 * Declare parameters of template instance, initialize them with the | |
1208 * template instance arguments. | |
1209 */ | |
1210 void declareParameters(Scope sc) | |
1211 { | |
20
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
1212 //printf("TemplateInstance.declareParameters()\n"); |
0 | 1213 for (int i = 0; i < tdtypes.dim; i++) |
1214 { | |
121
347de076ad34
TemplateParameters -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
1215 auto tp = tempdecl.parameters[i]; |
0 | 1216 //Object o = cast(Object)tiargs.data[i]; |
113
3482c73a991b
More cleanup for arrays
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
110
diff
changeset
|
1217 Object o = tdtypes[i]; // initializer for tp |
0 | 1218 |
1219 //printf("\ttdtypes[%d] = %p\n", i, o); | |
1220 tempdecl.declareParameter(sc, tp, o); | |
1221 } | |
1222 } | |
1223 | |
1224 /***************************************** | |
1225 * Determines if a TemplateInstance will need a nested | |
1226 * generation of the TemplateDeclaration. | |
1227 */ | |
1228 bool hasNestedArgs(Objects args) | |
1229 { | |
1230 bool nested = false; | |
20
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
1231 //printf("TemplateInstance.hasNestedArgs('%s')\n", tempdecl.ident.toChars()); |
0 | 1232 |
1233 /* A nested instance happens when an argument references a local | |
1234 * symbol that is on the stack. | |
1235 */ | |
1236 for (size_t i = 0; i < args.dim; i++) | |
1237 { | |
113
3482c73a991b
More cleanup for arrays
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
110
diff
changeset
|
1238 Object o = args[i]; |
0 | 1239 Expression ea = isExpression(o); |
1240 Dsymbol sa = isDsymbol(o); | |
1241 Tuple va = isTuple(o); | |
1242 if (ea) | |
1243 { | |
1244 if (ea.op == TOKvar) | |
1245 { | |
1246 sa = (cast(VarExp)ea).var; | |
1247 goto Lsa; | |
1248 } | |
1249 if (ea.op == TOKfunction) | |
1250 { | |
1251 sa = (cast(FuncExp)ea).fd; | |
1252 goto Lsa; | |
1253 } | |
1254 } | |
1255 else if (sa) | |
1256 { | |
1257 Lsa: | |
1258 TemplateDeclaration td = sa.isTemplateDeclaration(); | |
96 | 1259 Declaration d = sa.isDeclaration(); |
1260 if ((td && td.literal) || | |
1261 (d && !d.isDataseg() && | |
1262 | |
1263 /// version (DMDV2) { // TODO: | |
0 | 1264 !(d.storage_class & STCmanifest) && |
1265 /// } | |
1266 (!d.isFuncDeclaration() || d.isFuncDeclaration().isNested()) && | |
96 | 1267 !isTemplateMixin() |
1268 )) | |
0 | 1269 { |
1270 // if module level template | |
1271 if (tempdecl.toParent().isModule()) | |
1272 { | |
1273 Dsymbol dparent = sa.toParent(); | |
1274 if (!isnested) | |
1275 isnested = dparent; | |
1276 else if (isnested != dparent) | |
1277 { | |
1278 /* Select the more deeply nested of the two. | |
1279 * Error if one is not nested inside the other. | |
1280 */ | |
1281 for (Dsymbol p = isnested; p; p = p.parent) | |
1282 { | |
1283 if (p == dparent) | |
1284 goto L1; // isnested is most nested | |
1285 } | |
1286 for (Dsymbol p = dparent; p; p = p.parent) | |
1287 { | |
1288 if (p == isnested) | |
1289 { | |
1290 isnested = dparent; | |
1291 goto L1; // dparent is most nested | |
1292 } | |
1293 } | |
1294 error("%s is nested in both %s and %s", | |
1295 toChars(), isnested.toChars(), dparent.toChars()); | |
1296 } | |
1297 L1: | |
1298 //printf("\tnested inside %s\n", isnested.toChars()); | |
1299 nested |= 1; | |
1300 } | |
1301 else | |
1302 error("cannot use local '%s' as parameter to non-global template %s", d.toChars(), tempdecl.toChars()); | |
1303 } | |
1304 } | |
1305 else if (va) | |
1306 { | |
1307 nested |= hasNestedArgs(va.objects); | |
1308 } | |
1309 } | |
1310 return nested; | |
1311 } | |
1312 | |
1313 /**************************************** | |
1314 * This instance needs an identifier for name mangling purposes. | |
1315 * Create one by taking the template declaration name and adding | |
1316 * the type signature for it. | |
1317 */ | |
1318 Identifier genIdent() | |
1319 { | |
1320 scope OutBuffer buf = new OutBuffer(); | |
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()); |
96 | 1323 string id = tempdecl.ident.toChars(); |
0 | 1324 buf.printf("__T%d%s", id.length, id); ///! |
96 | 1325 Objects args = tiargs; |
0 | 1326 for (int i = 0; i < args.dim; i++) |
1327 { | |
113
3482c73a991b
More cleanup for arrays
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
110
diff
changeset
|
1328 Object o = args[i]; |
0 | 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 | |
72 | 1426 override TemplateInstance isTemplateInstance() { return this; } |
0 | 1427 |
72 | 1428 override AliasDeclaration isAliasDeclaration() |
0 | 1429 { |
1430 assert(false); | |
1431 } | |
20
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
1432 } |