Mercurial > projects > ddmd
annotate dmd/TemplateInstance.d @ 77:ad4792a1cfd6
more D-ification container accessing
author | Eldar Insafutdinov <e.insafutdinov@gmail.com> |
---|---|
date | Sun, 29 Aug 2010 14:36:55 +0100 |
parents | 7e0d548de9e6 |
children | 43073c7c7769 |
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 { | |
527 static int nest; | |
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 { | |
581 semantic3(sc2); | |
582 } | |
583 | |
584 Laftersemantic: | |
585 sc2.pop(); | |
586 | |
587 scope_.pop(); | |
588 | |
589 // Give additional context info if error occurred during instantiation | |
590 if (global.errors != errorsave) | |
591 { | |
592 error("error instantiating"); | |
593 if (tinst && !global.gag) | |
594 { | |
595 tinst.printInstantiationTrace(); | |
596 fatal(); | |
597 } | |
598 errors = 1; | |
599 if (global.gag) | |
600 tempdecl.instances.remove(tempdecl_instance_idx); | |
601 } | |
602 | |
603 version (LOG) { | |
604 printf("-TemplateInstance.semantic('%s', this=%p)\n", toChars(), this); | |
605 } | |
606 } | |
607 | |
72 | 608 override void semantic2(Scope sc) |
0 | 609 { |
610 int i; | |
611 | |
612 if (semanticRun >= 2) | |
613 return; | |
614 | |
615 semanticRun = 2; | |
616 version (LOG) { | |
20
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
617 printf("+TemplateInstance.semantic2('%s')\n", toChars()); |
0 | 618 } |
619 | |
620 if (!errors && members) | |
621 { | |
622 sc = tempdecl.scope_; | |
623 assert(sc); | |
624 sc = sc.push(argsym); | |
625 sc = sc.push(this); | |
626 sc.tinst = this; | |
627 | |
77
ad4792a1cfd6
more D-ification container accessing
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
74
diff
changeset
|
628 foreach(Dsymbol s; members) |
0 | 629 { |
630 version (LOG) { | |
631 printf("\tmember '%s', kind = '%s'\n", s.toChars(), s.kind()); | |
632 } | |
633 s.semantic2(sc); | |
634 } | |
635 | |
636 sc = sc.pop(); | |
637 sc.pop(); | |
638 } | |
639 | |
640 version (LOG) { | |
20
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
641 printf("-TemplateInstance.semantic2('%s')\n", toChars()); |
0 | 642 } |
643 } | |
644 | |
72 | 645 override void semantic3(Scope sc) |
0 | 646 { |
647 version (LOG) { | |
648 printf("TemplateInstance.semantic3('%s'), semanticRun = %d\n", toChars(), semanticRun); | |
649 } | |
650 //if (toChars()[0] == 'D') *(char*)0=0; | |
651 if (semanticRun >= 3) | |
652 return; | |
653 semanticRun = 3; | |
654 if (!errors && members) | |
655 { | |
656 sc = tempdecl.scope_; | |
657 sc = sc.push(argsym); | |
658 sc = sc.push(this); | |
659 sc.tinst = this; | |
77
ad4792a1cfd6
more D-ification container accessing
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
74
diff
changeset
|
660 foreach(Dsymbol s; members) |
0 | 661 s.semantic3(sc); |
662 sc = sc.pop(); | |
663 sc.pop(); | |
664 } | |
665 } | |
666 | |
72 | 667 override void inlineScan() |
0 | 668 { |
669 version (LOG) { | |
670 printf("TemplateInstance.inlineScan('%s')\n", toChars()); | |
671 } | |
672 if (!errors && members) | |
673 { | |
77
ad4792a1cfd6
more D-ification container accessing
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
74
diff
changeset
|
674 foreach(Dsymbol s; members) |
0 | 675 s.inlineScan(); |
676 } | |
677 } | |
678 | |
72 | 679 override void toCBuffer(OutBuffer buf, HdrGenState* hgs) |
0 | 680 { |
681 int i; | |
682 | |
683 Identifier id = name; | |
684 buf.writestring(id.toChars()); | |
685 buf.writestring("!("); | |
686 if (nest) | |
687 buf.writestring("..."); | |
688 else | |
689 { | |
690 nest++; | |
691 Objects args = tiargs; | |
692 for (i = 0; i < args.dim; i++) | |
693 { | |
694 if (i) | |
695 buf.writeByte(','); | |
696 Object oarg = cast(Object)args.data[i]; | |
697 ObjectToCBuffer(buf, hgs, oarg); | |
698 } | |
699 nest--; | |
700 } | |
701 buf.writeByte(')'); | |
702 } | |
703 | |
72 | 704 override Dsymbol toAlias() // resolve real symbol |
0 | 705 { |
706 version (LOG) { | |
20
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
707 printf("TemplateInstance.toAlias()\n"); |
0 | 708 } |
709 if (!inst) | |
710 { | |
711 error("cannot resolve forward reference"); | |
712 return this; | |
713 } | |
714 | |
715 if (inst !is this) | |
716 return inst.toAlias(); | |
717 | |
718 if (aliasdecl) | |
719 { | |
720 return aliasdecl.toAlias(); | |
721 } | |
722 | |
723 return inst; | |
724 } | |
725 | |
72 | 726 override string kind() |
0 | 727 { |
20
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
728 return "template instance"; |
0 | 729 } |
730 | |
72 | 731 override bool oneMember(Dsymbol* ps) |
0 | 732 { |
20
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
733 *ps = null; |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
734 return true; |
0 | 735 } |
736 | |
72 | 737 override string toChars() |
0 | 738 { |
739 scope OutBuffer buf = new OutBuffer(); | |
740 HdrGenState hgs; | |
741 | |
742 toCBuffer(buf, &hgs); | |
743 return buf.extractString(); | |
744 } | |
745 | |
72 | 746 override string mangle() |
0 | 747 { |
20
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
748 OutBuffer buf = new OutBuffer(); |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
749 string id; |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
750 |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
751 static if (0) { |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
752 printf("TemplateInstance.mangle() %s", toChars()); |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
753 if (parent) |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
754 printf(" parent = %s %s", parent.kind(), parent.toChars()); |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
755 printf("\n"); |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
756 } |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
757 id = ident ? ident.toChars() : toChars(); |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
758 if (!tempdecl) |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
759 error("is not defined"); |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
760 else if (tempdecl.parent) |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
761 { |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
762 string p = tempdecl.parent.mangle(); |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
763 if (p[0] == '_' && p[1] == 'D') |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
764 p = p[2..$]; |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
765 buf.writestring(p); |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
766 } |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
767 buf.printf("%d%s", id.length, id); |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
768 id = buf.toChars(); |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
769 buf.data = null; |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
770 //printf("TemplateInstance.mangle() %s = %s\n", toChars(), id); |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
771 return id; |
0 | 772 } |
773 | |
774 void printInstantiationTrace() | |
775 { | |
20
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
776 if (global.gag) |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
777 return; |
0 | 778 } |
779 | |
72 | 780 override void toObjFile(int multiobj) // compile to .obj file |
0 | 781 { |
782 version (LOG) { | |
783 printf("TemplateInstance.toObjFile('%s', this = %p)\n", toChars(), this); | |
784 } | |
785 if (!errors && members) | |
786 { | |
787 if (multiobj) | |
788 // Append to list of object files to be written later | |
789 obj_append(this); | |
790 else | |
791 { | |
77
ad4792a1cfd6
more D-ification container accessing
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
74
diff
changeset
|
792 foreach(Dsymbol s; members) |
0 | 793 s.toObjFile(multiobj); |
794 } | |
795 } | |
796 } | |
797 | |
798 // Internal | |
799 /********************************** | |
800 * Input: | |
801 * flags 1: replace const variables with their initializers | |
802 */ | |
803 static void semanticTiargs(Loc loc, Scope sc, Objects tiargs, int flags) | |
804 { | |
805 // Run semantic on each argument, place results in tiargs[] | |
806 //printf("+TemplateInstance.semanticTiargs() %s\n", toChars()); | |
807 if (!tiargs) | |
808 return; | |
809 for (size_t j = 0; j < tiargs.dim; j++) | |
810 { | |
811 Object o = cast(Object)tiargs.data[j]; | |
812 Type ta = isType(o); | |
813 Expression ea = isExpression(o); | |
814 Dsymbol sa = isDsymbol(o); | |
815 | |
816 //printf("1: tiargs.data[%d] = %p, %p, %p, ea=%p, ta=%p\n", j, o, isDsymbol(o), isTuple(o), ea, ta); | |
817 if (ta) | |
818 { | |
819 //printf("type %s\n", ta.toChars()); | |
820 // It might really be an Expression or an Alias | |
821 ta.resolve(loc, sc, &ea, &ta, &sa); | |
822 if (ea) | |
823 { | |
824 ea = ea.semantic(sc); | |
825 /* This test is to skip substituting a const var with | |
826 * its initializer. The problem is the initializer won't | |
827 * match with an 'alias' parameter. Instead, do the | |
828 * const substitution in TemplateValueParameter.matchArg(). | |
829 */ | |
830 if (ea.op != TOKvar || flags & 1) | |
831 ea = ea.optimize(WANTvalue | WANTinterpret); | |
832 | |
833 tiargs.data[j] = cast(void*)ea; | |
834 } | |
835 else if (sa) | |
836 { | |
837 tiargs.data[j] = cast(void*)sa; | |
838 TupleDeclaration d = sa.toAlias().isTupleDeclaration(); | |
839 if (d) | |
840 { | |
841 size_t dim = d.objects.dim; | |
842 tiargs.remove(j); | |
843 tiargs.insert(j, d.objects); | |
844 j--; | |
845 } | |
846 } | |
847 else if (ta) | |
848 { | |
849 Ltype: | |
850 if (ta.ty == Ttuple) | |
851 { | |
852 // Expand tuple | |
853 TypeTuple tt = cast(TypeTuple)ta; | |
854 size_t dim = tt.arguments.dim; | |
855 tiargs.remove(j); | |
856 if (dim) | |
857 { | |
858 tiargs.reserve(dim); | |
859 for (size_t i = 0; i < dim; i++) | |
860 { | |
861 Argument arg = cast(Argument)tt.arguments.data[i]; | |
862 tiargs.insert(j + i, cast(void*)arg.type); | |
863 } | |
864 } | |
865 j--; | |
866 } | |
867 else | |
868 tiargs.data[j] = cast(void*)ta; | |
869 } | |
870 else | |
871 { | |
872 assert(global.errors); | |
873 tiargs.data[j] = cast(void*)Type.terror; | |
874 } | |
875 } | |
876 else if (ea) | |
877 { | |
878 if (!ea) | |
879 { | |
880 assert(global.errors); | |
881 ea = new IntegerExp(0); | |
882 } | |
883 assert(ea); | |
884 ea = ea.semantic(sc); | |
885 if (ea.op != TOKvar || flags & 1) | |
886 ea = ea.optimize(WANTvalue | WANTinterpret); | |
887 tiargs.data[j] = cast(void*)ea; | |
888 if (ea.op == TOKtype) | |
889 { | |
890 ta = ea.type; | |
891 goto Ltype; | |
892 } | |
893 if (ea.op == TOKtuple) | |
894 { | |
895 // Expand tuple | |
896 TupleExp te = cast(TupleExp)ea; | |
897 size_t dim = te.exps.dim; | |
898 tiargs.remove(j); | |
899 if (dim) | |
900 { | |
901 tiargs.reserve(dim); | |
902 for (size_t i = 0; i < dim; i++) | |
903 tiargs.insert(j + i, te.exps.data[i]); | |
904 } | |
905 j--; | |
906 } | |
907 } | |
908 else if (sa) | |
909 { | |
910 TemplateDeclaration td = sa.isTemplateDeclaration(); | |
911 if (td && !td.semanticRun && td.literal) | |
912 td.semantic(sc); | |
913 } | |
914 else | |
915 { | |
916 assert(0); | |
917 } | |
918 //printf("1: tiargs.data[%d] = %p\n", j, tiargs.data[j]); | |
919 } | |
920 | |
921 static if (false) { | |
922 printf("-TemplateInstance.semanticTiargs('%s', this=%p)\n", toChars(), this); | |
923 for (size_t j = 0; j < tiargs.dim; j++) | |
924 { | |
925 Object o = cast(Object)tiargs.data[j]; | |
926 Type ta = isType(o); | |
927 Expression ea = isExpression(o); | |
928 Dsymbol sa = isDsymbol(o); | |
929 Tuple va = isTuple(o); | |
930 | |
931 printf("\ttiargs[%d] = ta %p, ea %p, sa %p, va %p\n", j, ta, ea, sa, va); | |
932 } | |
933 } | |
934 } | |
935 | |
936 void semanticTiargs(Scope sc) | |
937 { | |
938 //printf("+TemplateInstance.semanticTiargs() %s\n", toChars()); | |
939 if (semantictiargsdone) | |
940 return; | |
941 | |
942 semantictiargsdone = 1; | |
943 semanticTiargs(loc, sc, tiargs, 0); | |
944 } | |
945 | |
946 /********************************************** | |
947 * Find template declaration corresponding to template instance. | |
948 */ | |
949 TemplateDeclaration findTemplateDeclaration(Scope sc) | |
950 { | |
951 //printf("TemplateInstance.findTemplateDeclaration() %s\n", toChars()); | |
952 if (!tempdecl) | |
953 { | |
954 /* Given: | |
955 * foo!( ... ) | |
956 * figure out which TemplateDeclaration foo refers to. | |
957 */ | |
958 Dsymbol s; | |
959 Dsymbol scopesym; | |
960 int i; | |
961 | |
962 Identifier id = name; | |
963 s = sc.search(loc, id, &scopesym); | |
964 if (!s) | |
965 { | |
966 error("identifier '%s' is not defined", id.toChars()); | |
967 return null; | |
968 } | |
969 version (LOG) { | |
970 printf("It's an instance of '%s' kind '%s'\n", s.toChars(), s.kind()); | |
971 if (s.parent) | |
972 printf("s.parent = '%s'\n", s.parent.toChars()); | |
973 } | |
974 withsym = scopesym.isWithScopeSymbol(); | |
975 | |
976 /* We might have found an alias within a template when | |
977 * we really want the template. | |
978 */ | |
979 TemplateInstance ti; | |
980 if (s.parent && | |
981 (ti = s.parent.isTemplateInstance()) !is null) | |
982 { | |
983 if ( | |
984 (ti.name == id || | |
985 ti.toAlias().ident == id) | |
986 && | |
987 ti.tempdecl | |
988 ) | |
989 { | |
990 /* This is so that one can refer to the enclosing | |
991 * template, even if it has the same name as a member | |
992 * of the template, if it has a !(arguments) | |
993 */ | |
994 tempdecl = ti.tempdecl; | |
995 if (tempdecl.overroot) // if not start of overloaded list of TemplateDeclaration's | |
996 tempdecl = tempdecl.overroot; // then get the start | |
997 | |
998 s = tempdecl; | |
999 } | |
1000 } | |
1001 | |
1002 s = s.toAlias(); | |
1003 | |
1004 /* It should be a TemplateDeclaration, not some other symbol | |
1005 */ | |
1006 tempdecl = s.isTemplateDeclaration(); | |
1007 if (!tempdecl) | |
1008 { | |
1009 if (!s.parent && global.errors) | |
1010 return null; | |
1011 if (!s.parent && s.getType()) | |
1012 { | |
1013 Dsymbol s2 = s.getType().toDsymbol(sc); | |
1014 if (!s2) | |
1015 { | |
1016 error("%s is not a template declaration, it is a %s", id.toChars(), s.kind()); | |
1017 return null; | |
1018 } | |
1019 s = s2; | |
1020 } | |
1021 debug { | |
1022 //if (!s.parent) printf("s = %s %s\n", s.kind(), s.toChars()); | |
1023 } | |
1024 //assert(s.parent); | |
1025 TemplateInstance ti2 = s.parent ? s.parent.isTemplateInstance() : null; | |
1026 if (ti2 && | |
1027 (ti2.name == id || | |
1028 ti2.toAlias().ident == id) | |
1029 && | |
1030 ti2.tempdecl | |
1031 ) | |
1032 { | |
1033 /* This is so that one can refer to the enclosing | |
1034 * template, even if it has the same name as a member | |
1035 * of the template, if it has a !(arguments) | |
1036 */ | |
1037 tempdecl = ti2.tempdecl; | |
1038 if (tempdecl.overroot) // if not start of overloaded list of TemplateDeclaration's | |
1039 tempdecl = tempdecl.overroot; // then get the start | |
1040 } | |
1041 else | |
1042 { | |
1043 error("%s is not a template declaration, it is a %s", id.toChars(), s.kind()); | |
1044 return null; | |
1045 } | |
1046 } | |
1047 } | |
1048 else | |
1049 assert(tempdecl.isTemplateDeclaration()); | |
1050 | |
1051 return tempdecl; | |
1052 } | |
1053 | |
1054 TemplateDeclaration findBestMatch(Scope sc) | |
1055 { | |
1056 /* Since there can be multiple TemplateDeclaration's with the same | |
1057 * name, look for the best match. | |
1058 */ | |
1059 TemplateDeclaration td_ambig = null; | |
1060 TemplateDeclaration td_best = null; | |
1061 MATCH m_best = MATCHnomatch; | |
1062 scope Objects dedtypes = new Objects(); | |
1063 | |
1064 version (LOG) { | |
1065 printf("TemplateInstance.findBestMatch()\n"); | |
1066 } | |
1067 // First look for forward references | |
1068 for (TemplateDeclaration td = tempdecl; td; td = td.overnext) | |
1069 { | |
1070 if (!td.semanticRun) | |
1071 { | |
1072 if (td.scope_) | |
1073 { | |
1074 // Try to fix forward reference | |
1075 td.semantic(td.scope_); | |
1076 } | |
1077 if (!td.semanticRun) | |
1078 { | |
1079 error("%s forward references template declaration %s\n", toChars(), td.toChars()); | |
1080 return null; | |
1081 } | |
1082 } | |
1083 } | |
1084 | |
1085 for (TemplateDeclaration td = tempdecl; td; td = td.overnext) | |
1086 { | |
1087 MATCH m; | |
1088 | |
1089 //if (tiargs.dim) printf("2: tiargs.dim = %d, data[0] = %p\n", tiargs.dim, tiargs.data[0]); | |
1090 | |
1091 // If more arguments than parameters, | |
1092 // then this is no match. | |
1093 if (td.parameters.dim < tiargs.dim) | |
1094 { | |
1095 if (!td.isVariadic()) | |
1096 continue; | |
1097 } | |
1098 | |
1099 dedtypes.setDim(td.parameters.dim); | |
1100 dedtypes.zero(); | |
1101 assert(td.semanticRun); | |
1102 m = td.matchWithInstance(this, dedtypes, 0); | |
1103 //printf("matchWithInstance = %d\n", m); | |
1104 if (!m) // no match at all | |
1105 continue; | |
1106 | |
1107 if (m < m_best) | |
1108 goto Ltd_best; | |
1109 if (m > m_best) | |
1110 goto Ltd; | |
1111 | |
1112 { | |
1113 // Disambiguate by picking the most specialized TemplateDeclaration | |
1114 MATCH c1 = td.leastAsSpecialized(td_best); | |
1115 MATCH c2 = td_best.leastAsSpecialized(td); | |
1116 //printf("c1 = %d, c2 = %d\n", c1, c2); | |
1117 | |
1118 if (c1 > c2) | |
1119 goto Ltd; | |
1120 else if (c1 < c2) | |
1121 goto Ltd_best; | |
1122 else | |
1123 goto Lambig; | |
1124 } | |
1125 | |
1126 Lambig: // td_best and td are ambiguous | |
1127 td_ambig = td; | |
1128 continue; | |
1129 | |
1130 Ltd_best: // td_best is the best match so far | |
1131 td_ambig = null; | |
1132 continue; | |
1133 | |
1134 Ltd: // td is the new best match | |
1135 td_ambig = null; | |
1136 td_best = td; | |
1137 m_best = m; | |
1138 tdtypes.setDim(dedtypes.dim); | |
1139 memcpy(tdtypes.data, dedtypes.data, tdtypes.dim * (void*).sizeof); | |
1140 continue; | |
1141 } | |
1142 | |
1143 if (!td_best) | |
1144 { | |
1145 if (tempdecl && !tempdecl.overnext) | |
1146 // Only one template, so we can give better error message | |
1147 error("%s does not match template declaration %s", toChars(), tempdecl.toChars()); | |
1148 else | |
1149 error("%s does not match any template declaration", toChars()); | |
1150 return null; | |
1151 } | |
1152 | |
1153 if (td_ambig) | |
1154 { | |
1155 error("%s matches more than one template declaration, %s and %s", | |
1156 toChars(), td_best.toChars(), td_ambig.toChars()); | |
1157 } | |
1158 | |
1159 /* The best match is td_best | |
1160 */ | |
1161 tempdecl = td_best; | |
1162 | |
1163 static if (false) { | |
1164 /* Cast any value arguments to be same type as value parameter | |
1165 */ | |
1166 for (size_t i = 0; i < tiargs.dim; i++) | |
1167 { | |
1168 Object o = cast(Object)tiargs.data[i]; | |
1169 Expression ea = isExpression(o); // value argument | |
1170 TemplateParameter tp = cast(TemplateParameter)tempdecl.parameters.data[i]; | |
1171 assert(tp); | |
1172 TemplateValueParameter tvp = tp.isTemplateValueParameter(); | |
1173 if (tvp) | |
1174 { | |
1175 assert(ea); | |
1176 ea = ea.castTo(tvp.valType); | |
1177 ea = ea.optimize(WANTvalue | WANTinterpret); | |
1178 tiargs.data[i] = cast(Object)ea; | |
1179 } | |
1180 } | |
1181 } | |
1182 | |
1183 version (LOG) { | |
1184 printf("\tIt's a match with template declaration '%s'\n", tempdecl.toChars()); | |
1185 } | |
1186 return tempdecl; | |
1187 } | |
1188 | |
1189 /**************************************************** | |
1190 * Declare parameters of template instance, initialize them with the | |
1191 * template instance arguments. | |
1192 */ | |
1193 void declareParameters(Scope sc) | |
1194 { | |
20
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
1195 //printf("TemplateInstance.declareParameters()\n"); |
0 | 1196 for (int i = 0; i < tdtypes.dim; i++) |
1197 { | |
1198 TemplateParameter tp = cast(TemplateParameter)tempdecl.parameters.data[i]; | |
1199 //Object o = cast(Object)tiargs.data[i]; | |
1200 Object o = cast(Object)tdtypes.data[i]; // initializer for tp | |
1201 | |
1202 //printf("\ttdtypes[%d] = %p\n", i, o); | |
1203 tempdecl.declareParameter(sc, tp, o); | |
1204 } | |
1205 } | |
1206 | |
1207 /***************************************** | |
1208 * Determines if a TemplateInstance will need a nested | |
1209 * generation of the TemplateDeclaration. | |
1210 */ | |
1211 bool hasNestedArgs(Objects args) | |
1212 { | |
1213 bool nested = false; | |
20
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
1214 //printf("TemplateInstance.hasNestedArgs('%s')\n", tempdecl.ident.toChars()); |
0 | 1215 |
1216 /* A nested instance happens when an argument references a local | |
1217 * symbol that is on the stack. | |
1218 */ | |
1219 for (size_t i = 0; i < args.dim; i++) | |
1220 { | |
1221 Object o = cast(Object)args.data[i]; | |
1222 Expression ea = isExpression(o); | |
1223 Dsymbol sa = isDsymbol(o); | |
1224 Tuple va = isTuple(o); | |
1225 if (ea) | |
1226 { | |
1227 if (ea.op == TOKvar) | |
1228 { | |
1229 sa = (cast(VarExp)ea).var; | |
1230 goto Lsa; | |
1231 } | |
1232 if (ea.op == TOKfunction) | |
1233 { | |
1234 sa = (cast(FuncExp)ea).fd; | |
1235 goto Lsa; | |
1236 } | |
1237 } | |
1238 else if (sa) | |
1239 { | |
1240 Lsa: | |
1241 Declaration d = null; | |
1242 TemplateDeclaration td = sa.isTemplateDeclaration(); | |
1243 if (td && td.literal) | |
1244 { | |
1245 goto L2; | |
1246 } | |
1247 d = sa.isDeclaration(); | |
1248 if (d && !d.isDataseg() && | |
1249 /// version (DMDV2) { | |
1250 !(d.storage_class & STCmanifest) && | |
1251 /// } | |
1252 (!d.isFuncDeclaration() || d.isFuncDeclaration().isNested()) && | |
1253 !isTemplateMixin()) | |
1254 { | |
1255 L2: | |
1256 // if module level template | |
1257 if (tempdecl.toParent().isModule()) | |
1258 { | |
1259 Dsymbol dparent = sa.toParent(); | |
1260 if (!isnested) | |
1261 isnested = dparent; | |
1262 else if (isnested != dparent) | |
1263 { | |
1264 /* Select the more deeply nested of the two. | |
1265 * Error if one is not nested inside the other. | |
1266 */ | |
1267 for (Dsymbol p = isnested; p; p = p.parent) | |
1268 { | |
1269 if (p == dparent) | |
1270 goto L1; // isnested is most nested | |
1271 } | |
1272 for (Dsymbol p = dparent; p; p = p.parent) | |
1273 { | |
1274 if (p == isnested) | |
1275 { | |
1276 isnested = dparent; | |
1277 goto L1; // dparent is most nested | |
1278 } | |
1279 } | |
1280 error("%s is nested in both %s and %s", | |
1281 toChars(), isnested.toChars(), dparent.toChars()); | |
1282 } | |
1283 L1: | |
1284 //printf("\tnested inside %s\n", isnested.toChars()); | |
1285 nested |= 1; | |
1286 } | |
1287 else | |
1288 error("cannot use local '%s' as parameter to non-global template %s", d.toChars(), tempdecl.toChars()); | |
1289 } | |
1290 } | |
1291 else if (va) | |
1292 { | |
1293 nested |= hasNestedArgs(va.objects); | |
1294 } | |
1295 } | |
1296 return nested; | |
1297 } | |
1298 | |
1299 /**************************************** | |
1300 * This instance needs an identifier for name mangling purposes. | |
1301 * Create one by taking the template declaration name and adding | |
1302 * the type signature for it. | |
1303 */ | |
1304 Identifier genIdent() | |
1305 { | |
1306 scope OutBuffer buf = new OutBuffer(); | |
1307 string id; | |
1308 Objects args; | |
1309 | |
20
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
1310 //printf("TemplateInstance.genIdent('%s')\n", tempdecl.ident.toChars()); |
0 | 1311 id = tempdecl.ident.toChars(); |
1312 buf.printf("__T%d%s", id.length, id); ///! | |
1313 args = tiargs; | |
1314 for (int i = 0; i < args.dim; i++) | |
1315 { | |
1316 Object o = cast(Object)args.data[i]; | |
1317 Type ta = isType(o); | |
1318 Expression ea = isExpression(o); | |
1319 Dsymbol sa = isDsymbol(o); | |
1320 Tuple va = isTuple(o); | |
1321 //printf("\to [%d] %p ta %p ea %p sa %p va %p\n", i, o, ta, ea, sa, va); | |
1322 if (ta) | |
1323 { | |
1324 buf.writeByte('T'); | |
1325 if (ta.deco) | |
1326 buf.writestring(ta.deco); | |
1327 else | |
1328 { | |
1329 debug writef("ta = %d, %s\n", ta.ty, ta.toChars()); | |
1330 assert(global.errors); | |
1331 } | |
1332 } | |
1333 else if (ea) | |
1334 { | |
1335 Lea: | |
1336 long v; | |
1337 real r; | |
1338 | |
1339 ea = ea.optimize(WANTvalue | WANTinterpret); | |
1340 if (ea.op == TOKvar) | |
1341 { | |
1342 sa = (cast(VarExp)ea).var; | |
1343 ea = null; | |
1344 goto Lsa; | |
1345 } | |
1346 if (ea.op == TOKfunction) | |
1347 { | |
1348 sa = (cast(FuncExp)ea).fd; | |
1349 ea = null; | |
1350 goto Lsa; | |
1351 } | |
1352 buf.writeByte('V'); | |
1353 if (ea.op == TOKtuple) | |
1354 { | |
1355 ea.error("tuple is not a valid template value argument"); | |
1356 continue; | |
1357 } | |
1358 static if (true) { | |
1359 /* Use deco that matches what it would be for a function parameter | |
1360 */ | |
1361 buf.writestring(ea.type.deco); | |
1362 } else { | |
1363 // Use type of parameter, not type of argument | |
1364 TemplateParameter tp = cast(TemplateParameter)tempdecl.parameters.data[i]; | |
1365 assert(tp); | |
1366 TemplateValueParameter tvp = tp.isTemplateValueParameter(); | |
1367 assert(tvp); | |
1368 buf.writestring(tvp.valType.deco); | |
1369 } | |
1370 ea.toMangleBuffer(buf); | |
1371 } | |
1372 else if (sa) | |
1373 { | |
1374 Lsa: | |
1375 buf.writeByte('S'); | |
1376 Declaration d = sa.isDeclaration(); | |
1377 if (d && (!d.type || !d.type.deco)) | |
1378 { | |
1379 error("forward reference of %s", d.toChars()); | |
1380 continue; | |
1381 } | |
1382 static if (false) { | |
1383 VarDeclaration v = sa.isVarDeclaration(); | |
1384 if (v && v.storage_class & STCmanifest) | |
1385 { | |
1386 ExpInitializer ei = v.init.isExpInitializer(); | |
1387 if (ei) | |
1388 { | |
1389 ea = ei.exp; | |
1390 goto Lea; | |
1391 } | |
1392 } | |
1393 } | |
1394 string p = sa.mangle(); | |
62 | 1395 ///buf.printf("%zu%s", p.length, p); |
1396 buf.printf("%su%s", p.length, p); | |
0 | 1397 } |
1398 else if (va) | |
1399 { | |
1400 assert(i + 1 == args.dim); // must be last one | |
1401 args = va.objects; | |
1402 i = -1; | |
1403 } | |
1404 else | |
1405 assert(0); | |
1406 } | |
1407 buf.writeByte('Z'); | |
1408 id = buf.toChars(); | |
1409 buf.data = null; | |
1410 //printf("\tgenIdent = %s\n", id); | |
1411 return new Identifier(id, TOKidentifier); | |
1412 } | |
1413 | |
72 | 1414 override TemplateInstance isTemplateInstance() { return this; } |
0 | 1415 |
72 | 1416 override AliasDeclaration isAliasDeclaration() |
0 | 1417 { |
1418 assert(false); | |
1419 } | |
20
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
1420 } |