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