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