336
|
1
|
|
2 // Compiler implementation of the D programming language
|
|
3 // Copyright (c) 1999-2008 by Digital Mars
|
|
4 // All Rights Reserved
|
|
5 // written by Walter Bright
|
|
6 // http://www.digitalmars.com
|
|
7 // License for redistribution is by either the Artistic License
|
|
8 // in artistic.txt, or the GNU General Public License in gnu.txt.
|
|
9 // See the included readme.txt for details.
|
|
10
|
|
11 // Handle template implementation
|
|
12
|
|
13 #include <stdio.h>
|
|
14 #include <assert.h>
|
|
15
|
|
16 #if !IN_LLVM
|
|
17 #if _WIN32
|
|
18 #include <windows.h>
|
|
19 long __cdecl __ehfilter(LPEXCEPTION_POINTERS ep);
|
|
20 #endif
|
|
21 #endif
|
|
22
|
|
23 #include "root.h"
|
|
24 #include "mem.h"
|
|
25 #include "stringtable.h"
|
|
26 #include "mars.h"
|
|
27 #include "identifier.h"
|
|
28 #include "mtype.h"
|
|
29 #include "template.h"
|
|
30 #include "init.h"
|
|
31 #include "expression.h"
|
|
32 #include "scope.h"
|
|
33 #include "module.h"
|
|
34 #include "aggregate.h"
|
|
35 #include "declaration.h"
|
|
36 #include "dsymbol.h"
|
|
37 #include "hdrgen.h"
|
|
38
|
|
39 #define LOG 0
|
|
40
|
|
41 /********************************************
|
|
42 * These functions substitute for dynamic_cast. dynamic_cast does not work
|
|
43 * on earlier versions of gcc.
|
|
44 */
|
|
45
|
|
46 Expression *isExpression(Object *o)
|
|
47 {
|
|
48 //return dynamic_cast<Expression *>(o);
|
|
49 if (!o || o->dyncast() != DYNCAST_EXPRESSION)
|
|
50 return NULL;
|
|
51 return (Expression *)o;
|
|
52 }
|
|
53
|
|
54 Dsymbol *isDsymbol(Object *o)
|
|
55 {
|
|
56 //return dynamic_cast<Dsymbol *>(o);
|
|
57 if (!o || o->dyncast() != DYNCAST_DSYMBOL)
|
|
58 return NULL;
|
|
59 return (Dsymbol *)o;
|
|
60 }
|
|
61
|
|
62 Type *isType(Object *o)
|
|
63 {
|
|
64 //return dynamic_cast<Type *>(o);
|
|
65 if (!o || o->dyncast() != DYNCAST_TYPE)
|
|
66 return NULL;
|
|
67 return (Type *)o;
|
|
68 }
|
|
69
|
|
70 Tuple *isTuple(Object *o)
|
|
71 {
|
|
72 //return dynamic_cast<Tuple *>(o);
|
|
73 if (!o || o->dyncast() != DYNCAST_TUPLE)
|
|
74 return NULL;
|
|
75 return (Tuple *)o;
|
|
76 }
|
|
77
|
|
78
|
|
79 /***********************
|
|
80 * Try to get arg as a type.
|
|
81 */
|
|
82
|
|
83 Type *getType(Object *o)
|
|
84 {
|
|
85 Type *t = isType(o);
|
|
86 if (!t)
|
|
87 { Expression *e = isExpression(o);
|
|
88 if (e)
|
|
89 t = e->type;
|
|
90 }
|
|
91 return t;
|
|
92 }
|
|
93
|
|
94 Dsymbol *getDsymbol(Object *oarg)
|
|
95 {
|
|
96 Dsymbol *sa;
|
|
97 Expression *ea = isExpression(oarg);
|
|
98 if (ea)
|
|
99 { // Try to convert Expression to symbol
|
|
100 if (ea->op == TOKvar)
|
|
101 sa = ((VarExp *)ea)->var;
|
|
102 else if (ea->op == TOKfunction)
|
|
103 sa = ((FuncExp *)ea)->fd;
|
|
104 else
|
|
105 sa = NULL;
|
|
106 }
|
|
107 else
|
|
108 { // Try to convert Type to symbol
|
|
109 Type *ta = isType(oarg);
|
|
110 if (ta)
|
|
111 sa = ta->toDsymbol(NULL);
|
|
112 else
|
|
113 sa = isDsymbol(oarg); // if already a symbol
|
|
114 }
|
|
115 return sa;
|
|
116 }
|
|
117
|
|
118 /******************************
|
|
119 * If o1 matches o2, return 1.
|
|
120 * Else, return 0.
|
|
121 */
|
|
122
|
|
123 int match(Object *o1, Object *o2, TemplateDeclaration *tempdecl, Scope *sc)
|
|
124 {
|
|
125 Type *t1 = isType(o1);
|
|
126 Type *t2 = isType(o2);
|
|
127 Expression *e1 = isExpression(o1);
|
|
128 Expression *e2 = isExpression(o2);
|
|
129 Dsymbol *s1 = isDsymbol(o1);
|
|
130 Dsymbol *s2 = isDsymbol(o2);
|
|
131 Tuple *v1 = isTuple(o1);
|
|
132 Tuple *v2 = isTuple(o2);
|
|
133
|
|
134 //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);
|
|
135
|
|
136 /* A proper implementation of the various equals() overrides
|
|
137 * should make it possible to just do o1->equals(o2), but
|
|
138 * we'll do that another day.
|
|
139 */
|
|
140
|
|
141 if (t1)
|
|
142 {
|
|
143 /* if t1 is an instance of ti, then give error
|
|
144 * about recursive expansions.
|
|
145 */
|
|
146 Dsymbol *s = t1->toDsymbol(sc);
|
|
147 if (s && s->parent)
|
|
148 { TemplateInstance *ti1 = s->parent->isTemplateInstance();
|
|
149 if (ti1 && ti1->tempdecl == tempdecl)
|
|
150 {
|
|
151 for (Scope *sc1 = sc; sc1; sc1 = sc1->enclosing)
|
|
152 {
|
|
153 if (sc1->scopesym == ti1)
|
|
154 {
|
|
155 error("recursive template expansion for template argument %s", t1->toChars());
|
|
156 return 1; // fake a match
|
|
157 }
|
|
158 }
|
|
159 }
|
|
160 }
|
|
161
|
|
162 if (!t2 || !t1->equals(t2))
|
|
163 goto Lnomatch;
|
|
164 }
|
|
165 else if (e1)
|
|
166 {
|
|
167 #if 0
|
|
168 if (e1 && e2)
|
|
169 {
|
|
170 printf("match %d\n", e1->equals(e2));
|
|
171 e1->print();
|
|
172 e2->print();
|
|
173 e1->type->print();
|
|
174 e2->type->print();
|
|
175 }
|
|
176 #endif
|
|
177 if (!e2)
|
|
178 goto Lnomatch;
|
|
179 if (!e1->equals(e2))
|
|
180 goto Lnomatch;
|
|
181 }
|
|
182 else if (s1)
|
|
183 {
|
|
184 //printf("%p %s, %p %s\n", s1, s1->toChars(), s2, s2->toChars());
|
|
185 if (!s2 || !s1->equals(s2) || s1->parent != s2->parent)
|
|
186 {
|
|
187 goto Lnomatch;
|
|
188 }
|
|
189 }
|
|
190 else if (v1)
|
|
191 {
|
|
192 if (!v2)
|
|
193 goto Lnomatch;
|
|
194 if (v1->objects.dim != v2->objects.dim)
|
|
195 goto Lnomatch;
|
|
196 for (size_t i = 0; i < v1->objects.dim; i++)
|
|
197 {
|
|
198 if (!match((Object *)v1->objects.data[i],
|
|
199 (Object *)v2->objects.data[i],
|
|
200 tempdecl, sc))
|
|
201 goto Lnomatch;
|
|
202 }
|
|
203 }
|
|
204 return 1; // match
|
|
205 Lnomatch:
|
|
206 return 0; // nomatch;
|
|
207 }
|
|
208
|
|
209 /****************************************
|
|
210 */
|
|
211
|
|
212 void ObjectToCBuffer(OutBuffer *buf, HdrGenState *hgs, Object *oarg)
|
|
213 {
|
|
214 //printf("ObjectToCBuffer()\n");
|
|
215 Type *t = isType(oarg);
|
|
216 Expression *e = isExpression(oarg);
|
|
217 Dsymbol *s = isDsymbol(oarg);
|
|
218 Tuple *v = isTuple(oarg);
|
|
219 if (t)
|
|
220 { //printf("\tt: %s ty = %d\n", t->toChars(), t->ty);
|
|
221 t->toCBuffer(buf, NULL, hgs);
|
|
222 }
|
|
223 else if (e)
|
|
224 e->toCBuffer(buf, hgs);
|
|
225 else if (s)
|
|
226 {
|
|
227 char *p = s->ident ? s->ident->toChars() : s->toChars();
|
|
228 buf->writestring(p);
|
|
229 }
|
|
230 else if (v)
|
|
231 {
|
|
232 Objects *args = &v->objects;
|
|
233 for (size_t i = 0; i < args->dim; i++)
|
|
234 {
|
|
235 if (i)
|
|
236 buf->writeByte(',');
|
|
237 Object *o = (Object *)args->data[i];
|
|
238 ObjectToCBuffer(buf, hgs, o);
|
|
239 }
|
|
240 }
|
|
241 else if (!oarg)
|
|
242 {
|
|
243 buf->writestring("NULL");
|
|
244 }
|
|
245 else
|
|
246 {
|
|
247 #ifdef DEBUG
|
|
248 printf("bad Object = %p\n", oarg);
|
|
249 #endif
|
|
250 assert(0);
|
|
251 }
|
|
252 }
|
|
253
|
|
254
|
|
255
|
|
256 /* ======================== TemplateDeclaration ============================= */
|
|
257
|
|
258 TemplateDeclaration::TemplateDeclaration(Loc loc, Identifier *id, TemplateParameters *parameters, Array *decldefs)
|
|
259 : ScopeDsymbol(id)
|
|
260 {
|
|
261 #if LOG
|
|
262 printf("TemplateDeclaration(this = %p, id = '%s')\n", this, id->toChars());
|
|
263 #endif
|
|
264 #if 0
|
|
265 if (parameters)
|
|
266 for (int i = 0; i < parameters->dim; i++)
|
|
267 { TemplateParameter *tp = (TemplateParameter *)parameters->data[i];
|
|
268 //printf("\tparameter[%d] = %p\n", i, tp);
|
|
269 TemplateTypeParameter *ttp = tp->isTemplateTypeParameter();
|
|
270
|
|
271 if (ttp)
|
|
272 {
|
|
273 printf("\tparameter[%d] = %s : %s\n", i, tp->ident->toChars(), ttp->specType ? ttp->specType->toChars() : "");
|
|
274 }
|
|
275 }
|
|
276 #endif
|
|
277 this->loc = loc;
|
|
278 this->parameters = parameters;
|
|
279 this->origParameters = parameters;
|
|
280 this->members = decldefs;
|
|
281 this->overnext = NULL;
|
|
282 this->overroot = NULL;
|
|
283 this->scope = NULL;
|
|
284 this->onemember = NULL;
|
|
285 }
|
|
286
|
|
287 Dsymbol *TemplateDeclaration::syntaxCopy(Dsymbol *)
|
|
288 {
|
|
289 //printf("TemplateDeclaration::syntaxCopy()\n");
|
|
290 TemplateDeclaration *td;
|
|
291 TemplateParameters *p;
|
|
292 Array *d;
|
|
293
|
|
294 p = NULL;
|
|
295 if (parameters)
|
|
296 {
|
|
297 p = new TemplateParameters();
|
|
298 p->setDim(parameters->dim);
|
|
299 for (int i = 0; i < p->dim; i++)
|
|
300 { TemplateParameter *tp = (TemplateParameter *)parameters->data[i];
|
|
301 p->data[i] = (void *)tp->syntaxCopy();
|
|
302 }
|
|
303 }
|
|
304 d = Dsymbol::arraySyntaxCopy(members);
|
|
305 td = new TemplateDeclaration(loc, ident, p, d);
|
|
306 return td;
|
|
307 }
|
|
308
|
|
309 void TemplateDeclaration::semantic(Scope *sc)
|
|
310 {
|
|
311 #if LOG
|
|
312 printf("TemplateDeclaration::semantic(this = %p, id = '%s')\n", this, ident->toChars());
|
|
313 #endif
|
|
314 if (scope)
|
|
315 return; // semantic() already run
|
|
316
|
|
317 if (sc->func)
|
|
318 {
|
|
319 error("cannot declare template at function scope %s", sc->func->toChars());
|
|
320 }
|
|
321
|
|
322 if (/*global.params.useArrayBounds &&*/ sc->module)
|
|
323 {
|
|
324 // Generate this function as it may be used
|
|
325 // when template is instantiated in other modules
|
|
326 sc->module->toModuleArray();
|
|
327 }
|
|
328
|
|
329 if (/*global.params.useAssert &&*/ sc->module)
|
|
330 {
|
|
331 // Generate this function as it may be used
|
|
332 // when template is instantiated in other modules
|
|
333 sc->module->toModuleAssert();
|
|
334 }
|
|
335
|
|
336 /* Remember Scope for later instantiations, but make
|
|
337 * a copy since attributes can change.
|
|
338 */
|
|
339 this->scope = new Scope(*sc);
|
|
340 this->scope->setNoFree();
|
|
341
|
|
342 // Set up scope for parameters
|
|
343 ScopeDsymbol *paramsym = new ScopeDsymbol();
|
|
344 paramsym->parent = sc->parent;
|
|
345 Scope *paramscope = sc->push(paramsym);
|
|
346 paramscope->parameterSpecialization = 1;
|
|
347
|
|
348 if (global.params.doDocComments)
|
|
349 {
|
|
350 origParameters = new TemplateParameters();
|
|
351 origParameters->setDim(parameters->dim);
|
|
352 for (int i = 0; i < parameters->dim; i++)
|
|
353 {
|
|
354 TemplateParameter *tp = (TemplateParameter *)parameters->data[i];
|
|
355 origParameters->data[i] = (void *)tp->syntaxCopy();
|
|
356 }
|
|
357 }
|
|
358
|
|
359 for (int i = 0; i < parameters->dim; i++)
|
|
360 {
|
|
361 TemplateParameter *tp = (TemplateParameter *)parameters->data[i];
|
|
362
|
|
363 tp->declareParameter(paramscope);
|
|
364 }
|
|
365
|
|
366 for (int i = 0; i < parameters->dim; i++)
|
|
367 {
|
|
368 TemplateParameter *tp = (TemplateParameter *)parameters->data[i];
|
|
369
|
|
370 tp->semantic(paramscope);
|
|
371 if (i + 1 != parameters->dim && tp->isTemplateTupleParameter())
|
|
372 error("template tuple parameter must be last one");
|
|
373 }
|
|
374
|
|
375 paramscope->pop();
|
|
376
|
|
377 if (members)
|
|
378 {
|
|
379 Dsymbol *s;
|
|
380 if (Dsymbol::oneMembers(members, &s))
|
|
381 {
|
|
382 if (s && s->ident && s->ident->equals(ident))
|
|
383 {
|
|
384 onemember = s;
|
|
385 s->parent = this;
|
|
386 }
|
|
387 }
|
|
388 }
|
|
389
|
|
390 /* BUG: should check:
|
|
391 * o no virtual functions or non-static data members of classes
|
|
392 */
|
|
393 }
|
|
394
|
|
395 const char *TemplateDeclaration::kind()
|
|
396 {
|
|
397 return (onemember && onemember->isAggregateDeclaration())
|
|
398 ? onemember->kind()
|
|
399 : (char *)"template";
|
|
400 }
|
|
401
|
|
402 /**********************************
|
|
403 * Overload existing TemplateDeclaration 'this' with the new one 's'.
|
|
404 * Return !=0 if successful; i.e. no conflict.
|
|
405 */
|
|
406
|
|
407 int TemplateDeclaration::overloadInsert(Dsymbol *s)
|
|
408 {
|
|
409 TemplateDeclaration **pf;
|
|
410 TemplateDeclaration *f;
|
|
411
|
|
412 #if LOG
|
|
413 printf("TemplateDeclaration::overloadInsert('%s')\n", s->toChars());
|
|
414 #endif
|
|
415 f = s->isTemplateDeclaration();
|
|
416 if (!f)
|
|
417 return FALSE;
|
|
418 TemplateDeclaration *pthis = this;
|
|
419 for (pf = &pthis; *pf; pf = &(*pf)->overnext)
|
|
420 {
|
|
421 #if 0
|
|
422 // Conflict if TemplateParameter's match
|
|
423 // Will get caught anyway later with TemplateInstance, but
|
|
424 // should check it now.
|
|
425 TemplateDeclaration *f2 = *pf;
|
|
426
|
|
427 if (f->parameters->dim != f2->parameters->dim)
|
|
428 goto Lcontinue;
|
|
429
|
|
430 for (int i = 0; i < f->parameters->dim; i++)
|
|
431 { TemplateParameter *p1 = (TemplateParameter *)f->parameters->data[i];
|
|
432 TemplateParameter *p2 = (TemplateParameter *)f2->parameters->data[i];
|
|
433
|
|
434 if (!p1->overloadMatch(p2))
|
|
435 goto Lcontinue;
|
|
436 }
|
|
437
|
|
438 #if LOG
|
|
439 printf("\tfalse: conflict\n");
|
|
440 #endif
|
|
441 return FALSE;
|
|
442
|
|
443 Lcontinue:
|
|
444 ;
|
|
445 #endif
|
|
446 }
|
|
447
|
|
448 f->overroot = this;
|
|
449 *pf = f;
|
|
450 #if LOG
|
|
451 printf("\ttrue: no conflict\n");
|
|
452 #endif
|
|
453 return TRUE;
|
|
454 }
|
|
455
|
|
456 /***************************************
|
|
457 * Given that ti is an instance of this TemplateDeclaration,
|
|
458 * deduce the types of the parameters to this, and store
|
|
459 * those deduced types in dedtypes[].
|
|
460 * Input:
|
|
461 * flag 1: don't do semantic() because of dummy types
|
|
462 * 2: don't change types in matchArg()
|
|
463 * Output:
|
|
464 * dedtypes deduced arguments
|
|
465 * Return match level.
|
|
466 */
|
|
467
|
|
468 MATCH TemplateDeclaration::matchWithInstance(TemplateInstance *ti,
|
|
469 Objects *dedtypes, int flag)
|
|
470 { MATCH m;
|
|
471 int dedtypes_dim = dedtypes->dim;
|
|
472
|
|
473 #define LOGM 0
|
|
474 #if LOGM
|
|
475 printf("\n+TemplateDeclaration::matchWithInstance(this = %s, ti = %s, flag = %d)\n", toChars(), ti->toChars(), flag);
|
|
476 #endif
|
|
477
|
|
478 #if 0
|
|
479 printf("dedtypes->dim = %d, parameters->dim = %d\n", dedtypes_dim, parameters->dim);
|
|
480 if (ti->tiargs->dim)
|
|
481 printf("ti->tiargs->dim = %d, [0] = %p\n",
|
|
482 ti->tiargs->dim,
|
|
483 ti->tiargs->data[0]);
|
|
484 #endif
|
|
485 dedtypes->zero();
|
|
486
|
|
487 int parameters_dim = parameters->dim;
|
|
488 int variadic = isVariadic() != NULL;
|
|
489
|
|
490 // If more arguments than parameters, no match
|
|
491 if (ti->tiargs->dim > parameters_dim && !variadic)
|
|
492 {
|
|
493 #if LOGM
|
|
494 printf(" no match: more arguments than parameters\n");
|
|
495 #endif
|
|
496 return MATCHnomatch;
|
|
497 }
|
|
498
|
|
499 assert(dedtypes_dim == parameters_dim);
|
|
500 assert(dedtypes_dim >= ti->tiargs->dim || variadic);
|
|
501
|
|
502 // Set up scope for parameters
|
|
503 assert((size_t)scope > 0x10000);
|
|
504 ScopeDsymbol *paramsym = new ScopeDsymbol();
|
|
505 paramsym->parent = scope->parent;
|
|
506 Scope *paramscope = scope->push(paramsym);
|
|
507
|
|
508 // Attempt type deduction
|
|
509 m = MATCHexact;
|
|
510 for (int i = 0; i < dedtypes_dim; i++)
|
|
511 { MATCH m2;
|
|
512 TemplateParameter *tp = (TemplateParameter *)parameters->data[i];
|
|
513 Declaration *sparam;
|
|
514
|
|
515 //printf("\targument [%d]\n", i);
|
|
516 #if LOGM
|
|
517 //printf("\targument [%d] is %s\n", i, oarg ? oarg->toChars() : "null");
|
|
518 TemplateTypeParameter *ttp = tp->isTemplateTypeParameter();
|
|
519 if (ttp)
|
|
520 printf("\tparameter[%d] is %s : %s\n", i, tp->ident->toChars(), ttp->specType ? ttp->specType->toChars() : "");
|
|
521 #endif
|
|
522
|
|
523 #if DMDV1
|
|
524 m2 = tp->matchArg(paramscope, ti->tiargs, i, parameters, dedtypes, &sparam);
|
|
525 #else
|
|
526 m2 = tp->matchArg(paramscope, ti->tiargs, i, parameters, dedtypes, &sparam, (flag & 2) ? 1 : 0);
|
|
527
|
|
528 #endif
|
|
529 //printf("\tm2 = %d\n", m2);
|
|
530
|
|
531 if (m2 == MATCHnomatch)
|
|
532 {
|
|
533 #if 0
|
|
534 printf("\tmatchArg() for parameter %i failed\n", i);
|
|
535 #endif
|
|
536 goto Lnomatch;
|
|
537 }
|
|
538
|
|
539 if (m2 < m)
|
|
540 m = m2;
|
|
541
|
|
542 if (!flag)
|
|
543 sparam->semantic(paramscope);
|
|
544 if (!paramscope->insert(sparam))
|
|
545 goto Lnomatch;
|
|
546 }
|
|
547
|
|
548 if (!flag)
|
|
549 {
|
|
550 // Any parameter left without a type gets the type of its corresponding arg
|
|
551 for (int i = 0; i < dedtypes_dim; i++)
|
|
552 {
|
|
553 if (!dedtypes->data[i])
|
|
554 { assert(i < ti->tiargs->dim);
|
|
555 dedtypes->data[i] = ti->tiargs->data[i];
|
|
556 }
|
|
557 }
|
|
558 }
|
|
559
|
|
560 #if LOGM
|
|
561 // Print out the results
|
|
562 printf("--------------------------\n");
|
|
563 printf("template %s\n", toChars());
|
|
564 printf("instance %s\n", ti->toChars());
|
|
565 if (m)
|
|
566 {
|
|
567 for (int i = 0; i < dedtypes_dim; i++)
|
|
568 {
|
|
569 TemplateParameter *tp = (TemplateParameter *)parameters->data[i];
|
|
570 Object *oarg;
|
|
571
|
|
572 printf(" [%d]", i);
|
|
573
|
|
574 if (i < ti->tiargs->dim)
|
|
575 oarg = (Object *)ti->tiargs->data[i];
|
|
576 else
|
|
577 oarg = NULL;
|
|
578 tp->print(oarg, (Object *)dedtypes->data[i]);
|
|
579 }
|
|
580 }
|
|
581 else
|
|
582 goto Lnomatch;
|
|
583 #endif
|
|
584
|
|
585 #if LOGM
|
|
586 printf(" match = %d\n", m);
|
|
587 #endif
|
|
588 goto Lret;
|
|
589
|
|
590 Lnomatch:
|
|
591 #if LOGM
|
|
592 printf(" no match\n");
|
|
593 #endif
|
|
594 m = MATCHnomatch;
|
|
595
|
|
596 Lret:
|
|
597 paramscope->pop();
|
|
598 #if LOGM
|
|
599 printf("-TemplateDeclaration::matchWithInstance(this = %p, ti = %p) = %d\n", this, ti, m);
|
|
600 #endif
|
|
601 return m;
|
|
602 }
|
|
603
|
|
604 /********************************************
|
|
605 * Determine partial specialization order of 'this' vs td2.
|
|
606 * Returns:
|
|
607 * 1 this is at least as specialized as td2
|
|
608 * 0 td2 is more specialized than this
|
|
609 */
|
|
610
|
|
611 int TemplateDeclaration::leastAsSpecialized(TemplateDeclaration *td2)
|
|
612 {
|
|
613 /* This works by taking the template parameters to this template
|
|
614 * declaration and feeding them to td2 as if it were a template
|
|
615 * instance.
|
|
616 * If it works, then this template is at least as specialized
|
|
617 * as td2.
|
|
618 */
|
|
619
|
|
620 TemplateInstance ti(0, ident); // create dummy template instance
|
|
621 Objects dedtypes;
|
|
622
|
|
623 #define LOG_LEASTAS 0
|
|
624
|
|
625 #if LOG_LEASTAS
|
|
626 printf("%s.leastAsSpecialized(%s)\n", toChars(), td2->toChars());
|
|
627 #endif
|
|
628
|
|
629 // Set type arguments to dummy template instance to be types
|
|
630 // generated from the parameters to this template declaration
|
|
631 ti.tiargs = new Objects();
|
|
632 ti.tiargs->setDim(parameters->dim);
|
|
633 for (int i = 0; i < ti.tiargs->dim; i++)
|
|
634 {
|
|
635 TemplateParameter *tp = (TemplateParameter *)parameters->data[i];
|
|
636
|
|
637 void *p = tp->dummyArg();
|
|
638 if (p)
|
|
639 ti.tiargs->data[i] = p;
|
|
640 else
|
|
641 ti.tiargs->setDim(i);
|
|
642 }
|
|
643
|
|
644 // Temporary Array to hold deduced types
|
|
645 //dedtypes.setDim(parameters->dim);
|
|
646 dedtypes.setDim(td2->parameters->dim);
|
|
647
|
|
648 // Attempt a type deduction
|
|
649 if (td2->matchWithInstance(&ti, &dedtypes, 1))
|
|
650 {
|
|
651 /* A non-variadic template is more specialized than a
|
|
652 * variadic one.
|
|
653 */
|
|
654 if (isVariadic() && !td2->isVariadic())
|
|
655 goto L1;
|
|
656
|
|
657 #if LOG_LEASTAS
|
|
658 printf(" matches, so is least as specialized\n");
|
|
659 #endif
|
|
660 return 1;
|
|
661 }
|
|
662 L1:
|
|
663 #if LOG_LEASTAS
|
|
664 printf(" doesn't match, so is not as specialized\n");
|
|
665 #endif
|
|
666 return 0;
|
|
667 }
|
|
668
|
|
669
|
|
670 /*************************************************
|
|
671 * Match function arguments against a specific template function.
|
|
672 * Input:
|
|
673 * targsi Expression/Type initial list of template arguments
|
|
674 * fargs arguments to function
|
|
675 * Output:
|
|
676 * dedargs Expression/Type deduced template arguments
|
|
677 * Returns:
|
|
678 * match level
|
|
679 */
|
|
680
|
|
681 MATCH TemplateDeclaration::deduceFunctionTemplateMatch(Objects *targsi, Expressions *fargs,
|
|
682 Objects *dedargs)
|
|
683 {
|
|
684 size_t i;
|
|
685 size_t nfparams;
|
|
686 size_t nfargs;
|
|
687 size_t nargsi; // array size of targsi
|
|
688 int fptupindex = -1;
|
|
689 int tuple_dim = 0;
|
|
690 MATCH match = MATCHexact;
|
|
691 FuncDeclaration *fd = onemember->toAlias()->isFuncDeclaration();
|
|
692 TypeFunction *fdtype; // type of fd
|
|
693 TemplateTupleParameter *tp;
|
|
694 Objects dedtypes; // for T:T*, the dedargs is the T*, dedtypes is the T
|
|
695
|
|
696 #if 0
|
|
697 printf("\nTemplateDeclaration::deduceFunctionTemplateMatch() %s\n", toChars());
|
|
698 for (i = 0; i < fargs->dim; i++)
|
|
699 { Expression *e = (Expression *)fargs->data[i];
|
|
700 printf("\tfarg[%d] is %s, type is %s\n", i, e->toChars(), e->type->toChars());
|
|
701 }
|
|
702 #endif
|
|
703
|
|
704 assert((size_t)scope > 0x10000);
|
|
705
|
|
706 dedargs->setDim(parameters->dim);
|
|
707 dedargs->zero();
|
|
708
|
|
709 dedtypes.setDim(parameters->dim);
|
|
710 dedtypes.zero();
|
|
711
|
|
712 // Set up scope for parameters
|
|
713 ScopeDsymbol *paramsym = new ScopeDsymbol();
|
|
714 paramsym->parent = scope->parent;
|
|
715 Scope *paramscope = scope->push(paramsym);
|
|
716
|
|
717 tp = isVariadic();
|
|
718
|
|
719 nargsi = 0;
|
|
720 if (targsi)
|
|
721 { // Set initial template arguments
|
|
722
|
|
723 nargsi = targsi->dim;
|
|
724 if (nargsi > parameters->dim)
|
|
725 { if (!tp)
|
|
726 goto Lnomatch;
|
|
727 dedargs->setDim(nargsi);
|
|
728 dedargs->zero();
|
|
729 }
|
|
730
|
|
731 memcpy(dedargs->data, targsi->data, nargsi * sizeof(*dedargs->data));
|
|
732
|
|
733 for (i = 0; i < nargsi; i++)
|
|
734 { TemplateParameter *tp = (TemplateParameter *)parameters->data[i];
|
|
735 MATCH m;
|
|
736 Declaration *sparam;
|
|
737
|
|
738 m = tp->matchArg(paramscope, dedargs, i, parameters, &dedtypes, &sparam);
|
|
739 //printf("\tdeduceType m = %d\n", m);
|
|
740 if (m == MATCHnomatch)
|
|
741 goto Lnomatch;
|
|
742 if (m < match)
|
|
743 match = m;
|
|
744
|
|
745 sparam->semantic(paramscope);
|
|
746 if (!paramscope->insert(sparam))
|
|
747 goto Lnomatch;
|
|
748 }
|
|
749 }
|
|
750
|
|
751 assert(fd->type->ty == Tfunction);
|
|
752 fdtype = (TypeFunction *)fd->type;
|
|
753
|
|
754 nfparams = Argument::dim(fdtype->parameters); // number of function parameters
|
|
755 nfargs = fargs->dim; // number of function arguments
|
|
756
|
|
757 /* Check for match of function arguments with variadic template
|
|
758 * parameter, such as:
|
|
759 *
|
|
760 * template Foo(T, A...) { void Foo(T t, A a); }
|
|
761 * void main() { Foo(1,2,3); }
|
|
762 */
|
|
763 tp = isVariadic();
|
|
764 if (tp) // if variadic
|
|
765 {
|
|
766 if (nfparams == 0) // if no function parameters
|
|
767 {
|
|
768 Tuple *t = new Tuple();
|
|
769 //printf("t = %p\n", t);
|
|
770 dedargs->data[parameters->dim - 1] = (void *)t;
|
|
771 goto L2;
|
|
772 }
|
|
773 else if (nfargs < nfparams - 1)
|
|
774 goto L1;
|
|
775 else
|
|
776 {
|
|
777 /* Figure out which of the function parameters matches
|
|
778 * the tuple template parameter. Do this by matching
|
|
779 * type identifiers.
|
|
780 * Set the index of this function parameter to fptupindex.
|
|
781 */
|
|
782 for (fptupindex = 0; fptupindex < nfparams; fptupindex++)
|
|
783 {
|
|
784 Argument *fparam = (Argument *)fdtype->parameters->data[fptupindex];
|
|
785 if (fparam->type->ty != Tident)
|
|
786 continue;
|
|
787 TypeIdentifier *tid = (TypeIdentifier *)fparam->type;
|
|
788 if (!tp->ident->equals(tid->ident) || tid->idents.dim)
|
|
789 continue;
|
|
790
|
|
791 if (fdtype->varargs) // variadic function doesn't
|
|
792 goto Lnomatch; // go with variadic template
|
|
793
|
|
794 /* The types of the function arguments
|
|
795 * now form the tuple argument.
|
|
796 */
|
|
797 Tuple *t = new Tuple();
|
|
798 dedargs->data[parameters->dim - 1] = (void *)t;
|
|
799
|
|
800 tuple_dim = nfargs - (nfparams - 1);
|
|
801 t->objects.setDim(tuple_dim);
|
|
802 for (i = 0; i < tuple_dim; i++)
|
|
803 { Expression *farg = (Expression *)fargs->data[fptupindex + i];
|
|
804 t->objects.data[i] = (void *)farg->type;
|
|
805 }
|
|
806 goto L2;
|
|
807 }
|
|
808 fptupindex = -1;
|
|
809 }
|
|
810 }
|
|
811
|
|
812 L1:
|
|
813 if (nfparams == nfargs)
|
|
814 ;
|
|
815 else if (nfargs > nfparams)
|
|
816 {
|
|
817 if (fdtype->varargs == 0)
|
|
818 goto Lnomatch; // too many args, no match
|
|
819 match = MATCHconvert; // match ... with a conversion
|
|
820 }
|
|
821
|
|
822 L2:
|
|
823 // Loop through the function parameters
|
|
824 for (i = 0; i < nfparams; i++)
|
|
825 {
|
|
826 /* Skip over function parameters which wound up
|
|
827 * as part of a template tuple parameter.
|
|
828 */
|
|
829 if (i == fptupindex)
|
|
830 { if (fptupindex == nfparams - 1)
|
|
831 break;
|
|
832 i += tuple_dim - 1;
|
|
833 continue;
|
|
834 }
|
|
835
|
|
836 Argument *fparam = Argument::getNth(fdtype->parameters, i);
|
|
837
|
|
838 if (i >= nfargs) // if not enough arguments
|
|
839 {
|
|
840 if (fparam->defaultArg)
|
|
841 { /* Default arguments do not participate in template argument
|
|
842 * deduction.
|
|
843 */
|
|
844 goto Lmatch;
|
|
845 }
|
|
846 }
|
|
847 else
|
|
848 { Expression *farg = (Expression *)fargs->data[i];
|
|
849 #if 0
|
|
850 printf("\tfarg->type = %s\n", farg->type->toChars());
|
|
851 printf("\tfparam->type = %s\n", fparam->type->toChars());
|
|
852 #endif
|
|
853
|
|
854 MATCH m;
|
|
855 m = farg->type->deduceType(scope, fparam->type, parameters, &dedtypes);
|
|
856 //printf("\tdeduceType m = %d\n", m);
|
|
857
|
|
858 /* If no match, see if there's a conversion to a delegate
|
|
859 */
|
|
860 if (!m && fparam->type->toBasetype()->ty == Tdelegate)
|
|
861 {
|
|
862 TypeDelegate *td = (TypeDelegate *)fparam->type->toBasetype();
|
|
863 TypeFunction *tf = (TypeFunction *)td->next;
|
|
864
|
|
865 if (!tf->varargs && Argument::dim(tf->parameters) == 0)
|
|
866 {
|
|
867 m = farg->type->deduceType(scope, tf->next, parameters, &dedtypes);
|
|
868 if (!m && tf->next->toBasetype()->ty == Tvoid)
|
|
869 m = MATCHconvert;
|
|
870 }
|
|
871 //printf("\tm2 = %d\n", m);
|
|
872 }
|
|
873
|
|
874 if (m)
|
|
875 { if (m < match)
|
|
876 match = m; // pick worst match
|
|
877 continue;
|
|
878 }
|
|
879 }
|
|
880
|
|
881 /* The following code for variadic arguments closely
|
|
882 * matches TypeFunction::callMatch()
|
|
883 */
|
|
884 if (!(fdtype->varargs == 2 && i + 1 == nfparams))
|
|
885 goto Lnomatch;
|
|
886
|
|
887 /* Check for match with function parameter T...
|
|
888 */
|
|
889 Type *tb = fparam->type->toBasetype();
|
|
890 switch (tb->ty)
|
|
891 {
|
|
892 // Perhaps we can do better with this, see TypeFunction::callMatch()
|
|
893 case Tsarray:
|
|
894 { TypeSArray *tsa = (TypeSArray *)tb;
|
|
895 integer_t sz = tsa->dim->toInteger();
|
|
896 if (sz != nfargs - i)
|
|
897 goto Lnomatch;
|
|
898 }
|
|
899 case Tarray:
|
|
900 { TypeArray *ta = (TypeArray *)tb;
|
|
901 for (; i < nfargs; i++)
|
|
902 {
|
|
903 Expression *arg = (Expression *)fargs->data[i];
|
|
904 assert(arg);
|
|
905 MATCH m;
|
|
906 /* If lazy array of delegates,
|
|
907 * convert arg(s) to delegate(s)
|
|
908 */
|
|
909 Type *tret = fparam->isLazyArray();
|
|
910 if (tret)
|
|
911 {
|
|
912 if (ta->next->equals(arg->type))
|
|
913 { m = MATCHexact;
|
|
914 }
|
|
915 else
|
|
916 {
|
|
917 m = arg->implicitConvTo(tret);
|
|
918 if (m == MATCHnomatch)
|
|
919 {
|
|
920 if (tret->toBasetype()->ty == Tvoid)
|
|
921 m = MATCHconvert;
|
|
922 }
|
|
923 }
|
|
924 }
|
|
925 else
|
|
926 {
|
|
927 m = arg->type->deduceType(scope, ta->next, parameters, &dedtypes);
|
|
928 //m = arg->implicitConvTo(ta->next);
|
|
929 }
|
|
930 if (m == MATCHnomatch)
|
|
931 goto Lnomatch;
|
|
932 if (m < match)
|
|
933 match = m;
|
|
934 }
|
|
935 goto Lmatch;
|
|
936 }
|
|
937 case Tclass:
|
|
938 case Tident:
|
|
939 goto Lmatch;
|
|
940
|
|
941 default:
|
|
942 goto Lnomatch;
|
|
943 }
|
|
944 }
|
|
945
|
|
946 Lmatch:
|
|
947
|
|
948 /* Fill in any missing arguments with their defaults.
|
|
949 */
|
|
950 for (i = nargsi; i < dedargs->dim; i++)
|
|
951 {
|
|
952 TemplateParameter *tp = (TemplateParameter *)parameters->data[i];
|
|
953 //printf("tp[%d] = %s\n", i, tp->ident->toChars());
|
|
954 /* For T:T*, the dedargs is the T*, dedtypes is the T
|
|
955 * But for function templates, we really need them to match
|
|
956 */
|
|
957 Object *oarg = (Object *)dedargs->data[i];
|
|
958 Object *oded = (Object *)dedtypes.data[i];
|
|
959 //printf("1dedargs[%d] = %p, dedtypes[%d] = %p\n", i, oarg, i, oded);
|
|
960 if (!oarg)
|
|
961 {
|
|
962 if (oded)
|
|
963 {
|
|
964 if (tp->specialization())
|
|
965 { /* The specialization can work as long as afterwards
|
|
966 * the oded == oarg
|
|
967 */
|
|
968 Declaration *sparam;
|
|
969 dedargs->data[i] = (void *)oded;
|
|
970 MATCH m2 = tp->matchArg(paramscope, dedargs, i, parameters, &dedtypes, &sparam);
|
|
971 //printf("m2 = %d\n", m2);
|
|
972 if (!m2)
|
|
973 goto Lnomatch;
|
|
974 if (m2 < match)
|
|
975 match = m2; // pick worst match
|
|
976 if (dedtypes.data[i] != oded)
|
|
977 error("specialization not allowed for deduced parameter %s", tp->ident->toChars());
|
|
978 }
|
|
979 }
|
|
980 else
|
|
981 { oded = tp->defaultArg(paramscope);
|
|
982 if (!oded)
|
|
983 goto Lnomatch;
|
|
984 }
|
|
985 declareParameter(paramscope, tp, oded);
|
|
986 dedargs->data[i] = (void *)oded;
|
|
987 }
|
|
988 }
|
|
989
|
|
990 #if 0
|
|
991 for (i = 0; i < dedargs->dim; i++)
|
|
992 { Type *t = (Type *)dedargs->data[i];
|
|
993 printf("\tdedargs[%d] = %d, %s\n", i, t->dyncast(), t->toChars());
|
|
994 }
|
|
995 #endif
|
|
996
|
|
997 paramscope->pop();
|
|
998 //printf("\tmatch %d\n", match);
|
|
999 return match;
|
|
1000
|
|
1001 Lnomatch:
|
|
1002 paramscope->pop();
|
|
1003 //printf("\tnomatch\n");
|
|
1004 return MATCHnomatch;
|
|
1005 }
|
|
1006
|
|
1007 /**************************************************
|
|
1008 * Declare template parameter tp with value o.
|
|
1009 */
|
|
1010
|
|
1011 void TemplateDeclaration::declareParameter(Scope *sc, TemplateParameter *tp, Object *o)
|
|
1012 {
|
|
1013 //printf("TemplateDeclaration::declareParameter('%s', o = %p)\n", tp->ident->toChars(), o);
|
|
1014
|
|
1015 Type *targ = isType(o);
|
|
1016 Expression *ea = isExpression(o);
|
|
1017 Dsymbol *sa = isDsymbol(o);
|
|
1018 Tuple *va = isTuple(o);
|
|
1019
|
|
1020 Dsymbol *s;
|
|
1021
|
|
1022 if (targ)
|
|
1023 {
|
|
1024 //printf("type %s\n", targ->toChars());
|
|
1025 s = new AliasDeclaration(0, tp->ident, targ);
|
|
1026 }
|
|
1027 else if (sa)
|
|
1028 {
|
|
1029 //printf("Alias %s %s;\n", sa->ident->toChars(), tp->ident->toChars());
|
|
1030 s = new AliasDeclaration(0, tp->ident, sa);
|
|
1031 }
|
|
1032 else if (ea)
|
|
1033 {
|
|
1034 // tdtypes.data[i] always matches ea here
|
|
1035 Initializer *init = new ExpInitializer(loc, ea);
|
|
1036 TemplateValueParameter *tvp = tp->isTemplateValueParameter();
|
|
1037 assert(tvp);
|
|
1038
|
|
1039 VarDeclaration *v = new VarDeclaration(loc, tvp->valType, tp->ident, init);
|
|
1040 v->storage_class = STCconst;
|
|
1041 s = v;
|
|
1042 }
|
|
1043 else if (va)
|
|
1044 {
|
|
1045 //printf("\ttuple\n");
|
|
1046 s = new TupleDeclaration(loc, tp->ident, &va->objects);
|
|
1047 }
|
|
1048 else
|
|
1049 {
|
|
1050 #ifdef DEBUG
|
|
1051 o->print();
|
|
1052 #endif
|
|
1053 assert(0);
|
|
1054 }
|
|
1055 if (!sc->insert(s))
|
|
1056 error("declaration %s is already defined", tp->ident->toChars());
|
|
1057 s->semantic(sc);
|
|
1058 }
|
|
1059
|
|
1060 /**************************************
|
|
1061 * Determine if TemplateDeclaration is variadic.
|
|
1062 */
|
|
1063
|
|
1064 TemplateTupleParameter *isVariadic(TemplateParameters *parameters)
|
|
1065 { size_t dim = parameters->dim;
|
|
1066 TemplateTupleParameter *tp = NULL;
|
|
1067
|
|
1068 if (dim)
|
|
1069 tp = ((TemplateParameter *)parameters->data[dim - 1])->isTemplateTupleParameter();
|
|
1070 return tp;
|
|
1071 }
|
|
1072
|
|
1073 TemplateTupleParameter *TemplateDeclaration::isVariadic()
|
|
1074 {
|
|
1075 return ::isVariadic(parameters);
|
|
1076 }
|
|
1077
|
|
1078 /***********************************
|
|
1079 * We can overload templates.
|
|
1080 */
|
|
1081
|
|
1082 int TemplateDeclaration::isOverloadable()
|
|
1083 {
|
|
1084 return 1;
|
|
1085 }
|
|
1086
|
|
1087 /*************************************************
|
|
1088 * Given function arguments, figure out which template function
|
|
1089 * to expand, and return that function.
|
|
1090 * If no match, give error message and return NULL.
|
|
1091 * Input:
|
|
1092 * sc instantiation scope
|
|
1093 * loc instantiation location
|
|
1094 * targsi initial list of template arguments
|
|
1095 * fargs arguments to function
|
|
1096 */
|
|
1097
|
|
1098 FuncDeclaration *TemplateDeclaration::deduceFunctionTemplate(Scope *sc, Loc loc,
|
|
1099 Objects *targsi, Expressions *fargs)
|
|
1100 {
|
|
1101 MATCH m_best = MATCHnomatch;
|
|
1102 TemplateDeclaration *td_ambig = NULL;
|
|
1103 TemplateDeclaration *td_best = NULL;
|
|
1104 Objects *tdargs = new Objects();
|
|
1105 TemplateInstance *ti;
|
|
1106 FuncDeclaration *fd;
|
|
1107
|
|
1108 #if 0
|
|
1109 printf("TemplateDeclaration::deduceFunctionTemplate() %s\n", toChars());
|
|
1110 printf(" targsi:\n");
|
|
1111 if (targsi)
|
|
1112 { for (int i = 0; i < targsi->dim; i++)
|
|
1113 { Object *arg = (Object *)targsi->data[i];
|
|
1114 printf("\t%s\n", arg->toChars());
|
|
1115 }
|
|
1116 }
|
|
1117 printf(" fargs:\n");
|
|
1118 for (int i = 0; i < fargs->dim; i++)
|
|
1119 { Expression *arg = (Expression *)fargs->data[i];
|
|
1120 printf("\t%s %s\n", arg->type->toChars(), arg->toChars());
|
|
1121 //printf("\tty = %d\n", arg->type->ty);
|
|
1122 }
|
|
1123 #endif
|
|
1124
|
|
1125 for (TemplateDeclaration *td = this; td; td = td->overnext)
|
|
1126 {
|
|
1127 if (!td->scope)
|
|
1128 {
|
|
1129 error("forward reference to template %s", td->toChars());
|
|
1130 goto Lerror;
|
|
1131 }
|
|
1132 if (!td->onemember || !td->onemember->toAlias()->isFuncDeclaration())
|
|
1133 {
|
|
1134 error("is not a function template");
|
|
1135 goto Lerror;
|
|
1136 }
|
|
1137
|
|
1138 MATCH m;
|
|
1139 Objects dedargs;
|
|
1140
|
|
1141 m = td->deduceFunctionTemplateMatch(targsi, fargs, &dedargs);
|
|
1142 //printf("deduceFunctionTemplateMatch = %d\n", m);
|
|
1143 if (!m) // if no match
|
|
1144 continue;
|
|
1145
|
|
1146 if (m < m_best)
|
|
1147 goto Ltd_best;
|
|
1148 if (m > m_best)
|
|
1149 goto Ltd;
|
|
1150
|
|
1151 {
|
|
1152 // Disambiguate by picking the most specialized TemplateDeclaration
|
|
1153 int c1 = td->leastAsSpecialized(td_best);
|
|
1154 int c2 = td_best->leastAsSpecialized(td);
|
|
1155 //printf("c1 = %d, c2 = %d\n", c1, c2);
|
|
1156
|
|
1157 if (c1 > c2)
|
|
1158 goto Ltd;
|
|
1159 else if (c1 < c2)
|
|
1160 goto Ltd_best;
|
|
1161 else
|
|
1162 goto Lambig;
|
|
1163 }
|
|
1164
|
|
1165 Lambig: // td_best and td are ambiguous
|
|
1166 td_ambig = td;
|
|
1167 continue;
|
|
1168
|
|
1169 Ltd_best: // td_best is the best match so far
|
|
1170 td_ambig = NULL;
|
|
1171 continue;
|
|
1172
|
|
1173 Ltd: // td is the new best match
|
|
1174 td_ambig = NULL;
|
|
1175 assert((size_t)td->scope > 0x10000);
|
|
1176 td_best = td;
|
|
1177 m_best = m;
|
|
1178 tdargs->setDim(dedargs.dim);
|
|
1179 memcpy(tdargs->data, dedargs.data, tdargs->dim * sizeof(void *));
|
|
1180 continue;
|
|
1181 }
|
|
1182 if (!td_best)
|
|
1183 {
|
|
1184 error(loc, "does not match any template declaration");
|
|
1185 goto Lerror;
|
|
1186 }
|
|
1187 if (td_ambig)
|
|
1188 {
|
|
1189 error(loc, "%s matches more than one function template declaration, %s and %s",
|
|
1190 toChars(), td_best->toChars(), td_ambig->toChars());
|
|
1191 }
|
|
1192
|
|
1193 /* The best match is td_best with arguments tdargs.
|
|
1194 * Now instantiate the template.
|
|
1195 */
|
|
1196 assert((size_t)td_best->scope > 0x10000);
|
|
1197 ti = new TemplateInstance(loc, td_best, tdargs);
|
|
1198 ti->semantic(sc);
|
|
1199 fd = ti->toAlias()->isFuncDeclaration();
|
|
1200 if (!fd)
|
|
1201 goto Lerror;
|
|
1202 return fd;
|
|
1203
|
|
1204 Lerror:
|
|
1205 {
|
|
1206 OutBuffer buf;
|
|
1207 HdrGenState hgs;
|
|
1208
|
|
1209 argExpTypesToCBuffer(&buf, fargs, &hgs);
|
|
1210 error(loc, "cannot deduce template function from argument types (%s)",
|
|
1211 buf.toChars());
|
|
1212 return NULL;
|
|
1213 }
|
|
1214 }
|
|
1215
|
|
1216 void TemplateDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
|
|
1217 {
|
|
1218 #if 0 // Should handle template functions
|
|
1219 if (onemember && onemember->isFuncDeclaration())
|
|
1220 buf->writestring("foo ");
|
|
1221 #endif
|
|
1222 buf->writestring(kind());
|
|
1223 buf->writeByte(' ');
|
|
1224 buf->writestring(ident->toChars());
|
|
1225 buf->writeByte('(');
|
|
1226 for (int i = 0; i < parameters->dim; i++)
|
|
1227 {
|
|
1228 TemplateParameter *tp = (TemplateParameter *)parameters->data[i];
|
|
1229 if (hgs->ddoc)
|
|
1230 tp = (TemplateParameter *)origParameters->data[i];
|
|
1231 if (i)
|
|
1232 buf->writeByte(',');
|
|
1233 tp->toCBuffer(buf, hgs);
|
|
1234 }
|
|
1235 buf->writeByte(')');
|
|
1236
|
|
1237 if (hgs->hdrgen)
|
|
1238 {
|
|
1239 hgs->tpltMember++;
|
|
1240 buf->writenl();
|
|
1241 buf->writebyte('{');
|
|
1242 buf->writenl();
|
|
1243 for (int i = 0; i < members->dim; i++)
|
|
1244 {
|
|
1245 Dsymbol *s = (Dsymbol *)members->data[i];
|
|
1246 s->toCBuffer(buf, hgs);
|
|
1247 }
|
|
1248 buf->writebyte('}');
|
|
1249 buf->writenl();
|
|
1250 hgs->tpltMember--;
|
|
1251 }
|
|
1252 }
|
|
1253
|
|
1254
|
|
1255 char *TemplateDeclaration::toChars()
|
|
1256 { OutBuffer buf;
|
|
1257 HdrGenState hgs;
|
|
1258
|
|
1259 memset(&hgs, 0, sizeof(hgs));
|
|
1260 buf.writestring(ident->toChars());
|
|
1261 buf.writeByte('(');
|
|
1262 for (int i = 0; i < parameters->dim; i++)
|
|
1263 {
|
|
1264 TemplateParameter *tp = (TemplateParameter *)parameters->data[i];
|
|
1265 if (i)
|
|
1266 buf.writeByte(',');
|
|
1267 tp->toCBuffer(&buf, &hgs);
|
|
1268 }
|
|
1269 buf.writeByte(')');
|
|
1270 buf.writeByte(0);
|
|
1271 return (char *)buf.extractData();
|
|
1272 }
|
|
1273
|
|
1274 /* ======================== Type ============================================ */
|
|
1275
|
|
1276 /****
|
|
1277 * Given an identifier, figure out which TemplateParameter it is.
|
|
1278 * Return -1 if not found.
|
|
1279 */
|
|
1280
|
|
1281 int templateIdentifierLookup(Identifier *id, TemplateParameters *parameters)
|
|
1282 {
|
|
1283 for (size_t i = 0; i < parameters->dim; i++)
|
|
1284 { TemplateParameter *tp = (TemplateParameter *)parameters->data[i];
|
|
1285
|
|
1286 if (tp->ident->equals(id))
|
|
1287 return i;
|
|
1288 }
|
|
1289 return -1;
|
|
1290 }
|
|
1291
|
|
1292 int templateParameterLookup(Type *tparam, TemplateParameters *parameters)
|
|
1293 {
|
|
1294 assert(tparam->ty == Tident);
|
|
1295 TypeIdentifier *tident = (TypeIdentifier *)tparam;
|
|
1296 //printf("\ttident = '%s'\n", tident->toChars());
|
|
1297 if (tident->idents.dim == 0)
|
|
1298 {
|
|
1299 return templateIdentifierLookup(tident->ident, parameters);
|
|
1300 }
|
|
1301 return -1;
|
|
1302 }
|
|
1303
|
|
1304 /* These form the heart of template argument deduction.
|
|
1305 * Given 'this' being the type argument to the template instance,
|
|
1306 * it is matched against the template declaration parameter specialization
|
|
1307 * 'tparam' to determine the type to be used for the parameter.
|
|
1308 * Example:
|
|
1309 * template Foo(T:T*) // template declaration
|
|
1310 * Foo!(int*) // template instantiation
|
|
1311 * Input:
|
|
1312 * this = int*
|
|
1313 * tparam = T
|
|
1314 * parameters = [ T:T* ] // Array of TemplateParameter's
|
|
1315 * Output:
|
|
1316 * dedtypes = [ int ] // Array of Expression/Type's
|
|
1317 */
|
|
1318
|
|
1319 MATCH Type::deduceType(Scope *sc, Type *tparam, TemplateParameters *parameters,
|
|
1320 Objects *dedtypes)
|
|
1321 {
|
|
1322 //printf("Type::deduceType()\n");
|
|
1323 //printf("\tthis = %d, ", ty); print();
|
|
1324 //printf("\ttparam = %d, ", tparam->ty); tparam->print();
|
|
1325 if (!tparam)
|
|
1326 goto Lnomatch;
|
|
1327
|
|
1328 if (this == tparam)
|
|
1329 goto Lexact;
|
|
1330
|
|
1331 if (tparam->ty == Tident)
|
|
1332 {
|
|
1333 // Determine which parameter tparam is
|
|
1334 int i = templateParameterLookup(tparam, parameters);
|
|
1335 if (i == -1)
|
|
1336 {
|
|
1337 if (!sc)
|
|
1338 goto Lnomatch;
|
|
1339
|
|
1340 /* Need a loc to go with the semantic routine.
|
|
1341 */
|
|
1342 Loc loc;
|
|
1343 if (parameters->dim)
|
|
1344 {
|
|
1345 TemplateParameter *tp = (TemplateParameter *)parameters->data[0];
|
|
1346 loc = tp->loc;
|
|
1347 }
|
|
1348
|
|
1349 /* BUG: what if tparam is a template instance, that
|
|
1350 * has as an argument another Tident?
|
|
1351 */
|
|
1352 tparam = tparam->semantic(loc, sc);
|
|
1353 assert(tparam->ty != Tident);
|
|
1354 return deduceType(sc, tparam, parameters, dedtypes);
|
|
1355 }
|
|
1356
|
|
1357 TemplateParameter *tp = (TemplateParameter *)parameters->data[i];
|
|
1358
|
|
1359 // Found the corresponding parameter tp
|
|
1360 if (!tp->isTemplateTypeParameter())
|
|
1361 goto Lnomatch;
|
|
1362 Type *at = (Type *)dedtypes->data[i];
|
|
1363 if (!at)
|
|
1364 {
|
|
1365 dedtypes->data[i] = (void *)this;
|
|
1366 goto Lexact;
|
|
1367 }
|
|
1368 if (equals(at))
|
|
1369 goto Lexact;
|
|
1370 else if (ty == Tclass && at->ty == Tclass)
|
|
1371 {
|
|
1372 return (MATCH) implicitConvTo(at);
|
|
1373 }
|
|
1374 else if (ty == Tsarray && at->ty == Tarray &&
|
|
1375 nextOf()->equals(at->nextOf()))
|
|
1376 {
|
|
1377 goto Lexact;
|
|
1378 }
|
|
1379 else
|
|
1380 goto Lnomatch;
|
|
1381 }
|
|
1382
|
|
1383 if (ty != tparam->ty)
|
|
1384 return implicitConvTo(tparam);
|
|
1385 // goto Lnomatch;
|
|
1386
|
|
1387 if (nextOf())
|
|
1388 return nextOf()->deduceType(sc, tparam->nextOf(), parameters, dedtypes);
|
|
1389
|
|
1390 Lexact:
|
|
1391 return MATCHexact;
|
|
1392
|
|
1393 Lnomatch:
|
|
1394 return MATCHnomatch;
|
|
1395 }
|
|
1396
|
|
1397 MATCH TypeSArray::deduceType(Scope *sc, Type *tparam, TemplateParameters *parameters,
|
|
1398 Objects *dedtypes)
|
|
1399 {
|
|
1400 #if 0
|
|
1401 printf("TypeSArray::deduceType()\n");
|
|
1402 printf("\tthis = %d, ", ty); print();
|
|
1403 printf("\ttparam = %d, ", tparam->ty); tparam->print();
|
|
1404 #endif
|
|
1405
|
|
1406 // Extra check that array dimensions must match
|
|
1407 if (tparam)
|
|
1408 {
|
|
1409 if (tparam->ty == Tsarray)
|
|
1410 {
|
|
1411 TypeSArray *tp = (TypeSArray *)tparam;
|
|
1412
|
|
1413 if (tp->dim->op == TOKvar &&
|
|
1414 ((VarExp *)tp->dim)->var->storage_class & STCtemplateparameter)
|
|
1415 { int i = templateIdentifierLookup(((VarExp *)tp->dim)->var->ident, parameters);
|
|
1416 // This code matches code in TypeInstance::deduceType()
|
|
1417 if (i == -1)
|
|
1418 goto Lnomatch;
|
|
1419 TemplateParameter *tp = (TemplateParameter *)parameters->data[i];
|
|
1420 TemplateValueParameter *tvp = tp->isTemplateValueParameter();
|
|
1421 if (!tvp)
|
|
1422 goto Lnomatch;
|
|
1423 Expression *e = (Expression *)dedtypes->data[i];
|
|
1424 if (e)
|
|
1425 {
|
|
1426 if (!dim->equals(e))
|
|
1427 goto Lnomatch;
|
|
1428 }
|
|
1429 else
|
|
1430 { Type *vt = tvp->valType->semantic(0, sc);
|
|
1431 MATCH m = (MATCH)dim->implicitConvTo(vt);
|
|
1432 if (!m)
|
|
1433 goto Lnomatch;
|
|
1434 dedtypes->data[i] = dim;
|
|
1435 }
|
|
1436 }
|
|
1437 else if (dim->toInteger() != tp->dim->toInteger())
|
|
1438 return MATCHnomatch;
|
|
1439 }
|
|
1440 else if (tparam->ty == Taarray)
|
|
1441 {
|
|
1442 TypeAArray *tp = (TypeAArray *)tparam;
|
|
1443 if (tp->index->ty == Tident)
|
|
1444 { TypeIdentifier *tident = (TypeIdentifier *)tp->index;
|
|
1445
|
|
1446 if (tident->idents.dim == 0)
|
|
1447 { Identifier *id = tident->ident;
|
|
1448
|
|
1449 for (size_t i = 0; i < parameters->dim; i++)
|
|
1450 {
|
|
1451 TemplateParameter *tp = (TemplateParameter *)parameters->data[i];
|
|
1452
|
|
1453 if (tp->ident->equals(id))
|
|
1454 { // Found the corresponding template parameter
|
|
1455 TemplateValueParameter *tvp = tp->isTemplateValueParameter();
|
|
1456 if (!tvp || !tvp->valType->isintegral())
|
|
1457 goto Lnomatch;
|
|
1458
|
|
1459 if (dedtypes->data[i])
|
|
1460 {
|
|
1461 if (!dim->equals((Object *)dedtypes->data[i]))
|
|
1462 goto Lnomatch;
|
|
1463 }
|
|
1464 else
|
|
1465 { dedtypes->data[i] = (void *)dim;
|
|
1466 }
|
|
1467 return next->deduceType(sc, tparam->nextOf(), parameters, dedtypes);
|
|
1468 }
|
|
1469 }
|
|
1470 }
|
|
1471 }
|
|
1472 }
|
|
1473 else if (tparam->ty == Tarray)
|
|
1474 { MATCH m;
|
|
1475
|
|
1476 m = next->deduceType(sc, tparam->nextOf(), parameters, dedtypes);
|
|
1477 if (m == MATCHexact)
|
|
1478 m = MATCHconvert;
|
|
1479 return m;
|
|
1480 }
|
|
1481 }
|
|
1482 return Type::deduceType(sc, tparam, parameters, dedtypes);
|
|
1483
|
|
1484 Lnomatch:
|
|
1485 return MATCHnomatch;
|
|
1486 }
|
|
1487
|
|
1488 MATCH TypeAArray::deduceType(Scope *sc, Type *tparam, TemplateParameters *parameters, Objects *dedtypes)
|
|
1489 {
|
|
1490 #if 0
|
|
1491 printf("TypeAArray::deduceType()\n");
|
|
1492 printf("\tthis = %d, ", ty); print();
|
|
1493 printf("\ttparam = %d, ", tparam->ty); tparam->print();
|
|
1494 #endif
|
|
1495
|
|
1496 // Extra check that index type must match
|
|
1497 if (tparam && tparam->ty == Taarray)
|
|
1498 {
|
|
1499 TypeAArray *tp = (TypeAArray *)tparam;
|
|
1500 if (!index->deduceType(sc, tp->index, parameters, dedtypes))
|
|
1501 {
|
|
1502 return MATCHnomatch;
|
|
1503 }
|
|
1504 }
|
|
1505 return Type::deduceType(sc, tparam, parameters, dedtypes);
|
|
1506 }
|
|
1507
|
|
1508 MATCH TypeFunction::deduceType(Scope *sc, Type *tparam, TemplateParameters *parameters, Objects *dedtypes)
|
|
1509 {
|
|
1510 //printf("TypeFunction::deduceType()\n");
|
|
1511 //printf("\tthis = %d, ", ty); print();
|
|
1512 //printf("\ttparam = %d, ", tparam->ty); tparam->print();
|
|
1513
|
|
1514 // Extra check that function characteristics must match
|
|
1515 if (tparam && tparam->ty == Tfunction)
|
|
1516 {
|
|
1517 TypeFunction *tp = (TypeFunction *)tparam;
|
|
1518 if (varargs != tp->varargs ||
|
|
1519 linkage != tp->linkage)
|
|
1520 return MATCHnomatch;
|
|
1521
|
|
1522 size_t nfargs = Argument::dim(this->parameters);
|
|
1523 size_t nfparams = Argument::dim(tp->parameters);
|
|
1524
|
|
1525 /* See if tuple match
|
|
1526 */
|
|
1527 if (nfparams > 0 && nfargs >= nfparams - 1)
|
|
1528 {
|
|
1529 /* See if 'A' of the template parameter matches 'A'
|
|
1530 * of the type of the last function parameter.
|
|
1531 */
|
|
1532 Argument *fparam = (Argument *)tp->parameters->data[nfparams - 1];
|
|
1533 if (fparam->type->ty != Tident)
|
|
1534 goto L1;
|
|
1535 TypeIdentifier *tid = (TypeIdentifier *)fparam->type;
|
|
1536 if (tid->idents.dim)
|
|
1537 goto L1;
|
|
1538
|
|
1539 /* Look through parameters to find tuple matching tid->ident
|
|
1540 */
|
|
1541 size_t tupi = 0;
|
|
1542 for (; 1; tupi++)
|
|
1543 { if (tupi == parameters->dim)
|
|
1544 goto L1;
|
|
1545 TemplateParameter *t = (TemplateParameter *)parameters->data[tupi];
|
|
1546 TemplateTupleParameter *tup = t->isTemplateTupleParameter();
|
|
1547 if (tup && tup->ident->equals(tid->ident))
|
|
1548 break;
|
|
1549 }
|
|
1550
|
|
1551 /* The types of the function arguments [nfparams - 1 .. nfargs]
|
|
1552 * now form the tuple argument.
|
|
1553 */
|
|
1554 int tuple_dim = nfargs - (nfparams - 1);
|
|
1555
|
|
1556 /* See if existing tuple, and whether it matches or not
|
|
1557 */
|
|
1558 Object *o = (Object *)dedtypes->data[tupi];
|
|
1559 if (o)
|
|
1560 { // Existing deduced argument must be a tuple, and must match
|
|
1561 Tuple *t = isTuple(o);
|
|
1562 if (!t || t->objects.dim != tuple_dim)
|
|
1563 return MATCHnomatch;
|
|
1564 for (size_t i = 0; i < tuple_dim; i++)
|
|
1565 { Argument *arg = Argument::getNth(this->parameters, nfparams - 1 + i);
|
|
1566 if (!arg->type->equals((Object *)t->objects.data[i]))
|
|
1567 return MATCHnomatch;
|
|
1568 }
|
|
1569 }
|
|
1570 else
|
|
1571 { // Create new tuple
|
|
1572 Tuple *t = new Tuple();
|
|
1573 t->objects.setDim(tuple_dim);
|
|
1574 for (size_t i = 0; i < tuple_dim; i++)
|
|
1575 { Argument *arg = Argument::getNth(this->parameters, nfparams - 1 + i);
|
|
1576 t->objects.data[i] = (void *)arg->type;
|
|
1577 }
|
|
1578 dedtypes->data[tupi] = (void *)t;
|
|
1579 }
|
|
1580 nfparams--; // don't consider the last parameter for type deduction
|
|
1581 goto L2;
|
|
1582 }
|
|
1583
|
|
1584 L1:
|
|
1585 if (nfargs != nfparams)
|
|
1586 return MATCHnomatch;
|
|
1587 L2:
|
|
1588 for (size_t i = 0; i < nfparams; i++)
|
|
1589 {
|
|
1590 Argument *a = Argument::getNth(this->parameters, i);
|
|
1591 Argument *ap = Argument::getNth(tp->parameters, i);
|
|
1592 if (a->storageClass != ap->storageClass ||
|
|
1593 !a->type->deduceType(sc, ap->type, parameters, dedtypes))
|
|
1594 return MATCHnomatch;
|
|
1595 }
|
|
1596 }
|
|
1597 return Type::deduceType(sc, tparam, parameters, dedtypes);
|
|
1598 }
|
|
1599
|
|
1600 MATCH TypeIdentifier::deduceType(Scope *sc, Type *tparam, TemplateParameters *parameters, Objects *dedtypes)
|
|
1601 {
|
|
1602 // Extra check
|
|
1603 if (tparam && tparam->ty == Tident)
|
|
1604 {
|
|
1605 TypeIdentifier *tp = (TypeIdentifier *)tparam;
|
|
1606
|
|
1607 for (int i = 0; i < idents.dim; i++)
|
|
1608 {
|
|
1609 Identifier *id1 = (Identifier *)idents.data[i];
|
|
1610 Identifier *id2 = (Identifier *)tp->idents.data[i];
|
|
1611
|
|
1612 if (!id1->equals(id2))
|
|
1613 return MATCHnomatch;
|
|
1614 }
|
|
1615 }
|
|
1616 return Type::deduceType(sc, tparam, parameters, dedtypes);
|
|
1617 }
|
|
1618
|
|
1619 MATCH TypeInstance::deduceType(Scope *sc,
|
|
1620 Type *tparam, TemplateParameters *parameters,
|
|
1621 Objects *dedtypes)
|
|
1622 {
|
|
1623 //printf("TypeInstance::deduceType(tparam = %s) %s\n", tparam->toChars(), toChars());
|
|
1624 //printf("\ttparam = %d, ", tparam->ty); tparam->print();
|
|
1625
|
|
1626 // Extra check
|
|
1627 if (tparam && tparam->ty == Tinstance)
|
|
1628 {
|
|
1629 TypeInstance *tp = (TypeInstance *)tparam;
|
|
1630
|
|
1631 //printf("tempinst->tempdecl = %p\n", tempinst->tempdecl);
|
|
1632 //printf("tp->tempinst->tempdecl = %p\n", tp->tempinst->tempdecl);
|
|
1633 if (!tp->tempinst->tempdecl)
|
|
1634 { //printf("tp->tempinst->name = '%s'\n", tp->tempinst->name->toChars());
|
|
1635 if (!tp->tempinst->name->equals(tempinst->name))
|
|
1636 {
|
|
1637 /* Handle case of:
|
|
1638 * template Foo(T : sa!(T), alias sa)
|
|
1639 */
|
|
1640 int i = templateIdentifierLookup(tp->tempinst->name, parameters);
|
|
1641 if (i == -1)
|
|
1642 { /* Didn't find it as a parameter identifier. Try looking
|
|
1643 * it up and seeing if is an alias. See Bugzilla 1454
|
|
1644 */
|
|
1645 Dsymbol *s = tempinst->tempdecl->scope->search(0, tp->tempinst->name, NULL);
|
|
1646 if (s)
|
|
1647 {
|
|
1648 s = s->toAlias();
|
|
1649 TemplateDeclaration *td = s->isTemplateDeclaration();
|
|
1650 if (td && td == tempinst->tempdecl)
|
|
1651 goto L2;
|
|
1652 }
|
|
1653 goto Lnomatch;
|
|
1654 }
|
|
1655 TemplateParameter *tpx = (TemplateParameter *)parameters->data[i];
|
|
1656 // This logic duplicates tpx->matchArg()
|
|
1657 TemplateAliasParameter *ta = tpx->isTemplateAliasParameter();
|
|
1658 if (!ta)
|
|
1659 goto Lnomatch;
|
|
1660 Dsymbol *sa = tempinst->tempdecl;
|
|
1661 if (!sa)
|
|
1662 goto Lnomatch;
|
|
1663 if (ta->specAlias && sa != ta->specAlias)
|
|
1664 goto Lnomatch;
|
|
1665 if (dedtypes->data[i])
|
|
1666 { // Must match already deduced symbol
|
|
1667 Dsymbol *s = (Dsymbol *)dedtypes->data[i];
|
|
1668
|
|
1669 if (s != sa)
|
|
1670 goto Lnomatch;
|
|
1671 }
|
|
1672 dedtypes->data[i] = sa;
|
|
1673 }
|
|
1674 }
|
|
1675 else if (tempinst->tempdecl != tp->tempinst->tempdecl)
|
|
1676 goto Lnomatch;
|
|
1677
|
|
1678 L2:
|
|
1679 if (tempinst->tiargs->dim != tp->tempinst->tiargs->dim)
|
|
1680 goto Lnomatch;
|
|
1681
|
|
1682 for (int i = 0; i < tempinst->tiargs->dim; i++)
|
|
1683 {
|
|
1684 //printf("\ttest: tempinst->tiargs[%d]\n", i);
|
|
1685 int j;
|
|
1686 Object *o1 = (Object *)tempinst->tiargs->data[i];
|
|
1687 Object *o2 = (Object *)tp->tempinst->tiargs->data[i];
|
|
1688
|
|
1689 Type *t1 = isType(o1);
|
|
1690 Type *t2 = isType(o2);
|
|
1691
|
|
1692 Expression *e1 = isExpression(o1);
|
|
1693 Expression *e2 = isExpression(o2);
|
|
1694
|
|
1695 #if 0
|
|
1696 if (t1) printf("t1 = %s\n", t1->toChars());
|
|
1697 if (t2) printf("t2 = %s\n", t2->toChars());
|
|
1698 if (e1) printf("e1 = %s\n", e1->toChars());
|
|
1699 if (e2) printf("e2 = %s\n", e2->toChars());
|
|
1700 #endif
|
|
1701
|
|
1702 if (t1 && t2)
|
|
1703 {
|
|
1704 if (!t1->deduceType(sc, t2, parameters, dedtypes))
|
|
1705 goto Lnomatch;
|
|
1706 }
|
|
1707 else if (e1 && e2)
|
|
1708 {
|
|
1709 if (!e1->equals(e2))
|
|
1710 { if (e2->op == TOKvar)
|
|
1711 {
|
|
1712 /*
|
|
1713 * (T:Number!(e2), int e2)
|
|
1714 */
|
|
1715 j = templateIdentifierLookup(((VarExp *)e2)->var->ident, parameters);
|
|
1716 goto L1;
|
|
1717 }
|
|
1718 goto Lnomatch;
|
|
1719 }
|
|
1720 }
|
|
1721 else if (e1 && t2 && t2->ty == Tident)
|
|
1722 {
|
|
1723 j = templateParameterLookup(t2, parameters);
|
|
1724 L1:
|
|
1725 if (j == -1)
|
|
1726 goto Lnomatch;
|
|
1727 TemplateParameter *tp = (TemplateParameter *)parameters->data[j];
|
|
1728 // BUG: use tp->matchArg() instead of the following
|
|
1729 TemplateValueParameter *tv = tp->isTemplateValueParameter();
|
|
1730 if (!tv)
|
|
1731 goto Lnomatch;
|
|
1732 Expression *e = (Expression *)dedtypes->data[j];
|
|
1733 if (e)
|
|
1734 {
|
|
1735 if (!e1->equals(e))
|
|
1736 goto Lnomatch;
|
|
1737 }
|
|
1738 else
|
|
1739 { Type *vt = tv->valType->semantic(0, sc);
|
|
1740 MATCH m = (MATCH)e1->implicitConvTo(vt);
|
|
1741 if (!m)
|
|
1742 goto Lnomatch;
|
|
1743 dedtypes->data[j] = e1;
|
|
1744 }
|
|
1745 }
|
|
1746 // BUG: Need to handle alias and tuple parameters
|
|
1747 else
|
|
1748 goto Lnomatch;
|
|
1749 }
|
|
1750 }
|
|
1751 return Type::deduceType(sc, tparam, parameters, dedtypes);
|
|
1752
|
|
1753 Lnomatch:
|
|
1754 return MATCHnomatch;
|
|
1755 }
|
|
1756
|
|
1757 MATCH TypeStruct::deduceType(Scope *sc, Type *tparam, TemplateParameters *parameters, Objects *dedtypes)
|
|
1758 {
|
|
1759 //printf("TypeStruct::deduceType()\n");
|
|
1760 //printf("\tthis->parent = %s, ", sym->parent->toChars()); print();
|
|
1761 //printf("\ttparam = %d, ", tparam->ty); tparam->print();
|
|
1762
|
|
1763 /* If this struct is a template struct, and we're matching
|
|
1764 * it against a template instance, convert the struct type
|
|
1765 * to a template instance, too, and try again.
|
|
1766 */
|
|
1767 TemplateInstance *ti = sym->parent->isTemplateInstance();
|
|
1768
|
|
1769 if (tparam && tparam->ty == Tinstance)
|
|
1770 {
|
|
1771 if (ti && ti->toAlias() == sym)
|
|
1772 {
|
|
1773 TypeInstance *t = new TypeInstance(0, ti);
|
|
1774 return t->deduceType(sc, tparam, parameters, dedtypes);
|
|
1775 }
|
|
1776
|
|
1777 /* Match things like:
|
|
1778 * S!(T).foo
|
|
1779 */
|
|
1780 TypeInstance *tpi = (TypeInstance *)tparam;
|
|
1781 if (tpi->idents.dim)
|
|
1782 { Identifier *id = (Identifier *)tpi->idents.data[tpi->idents.dim - 1];
|
|
1783 if (id->dyncast() == DYNCAST_IDENTIFIER && sym->ident->equals(id))
|
|
1784 {
|
|
1785 Type *tparent = sym->parent->getType();
|
|
1786 if (tparent)
|
|
1787 {
|
|
1788 /* Slice off the .foo in S!(T).foo
|
|
1789 */
|
|
1790 tpi->idents.dim--;
|
|
1791 MATCH m = tparent->deduceType(sc, tpi, parameters, dedtypes);
|
|
1792 tpi->idents.dim++;
|
|
1793 return m;
|
|
1794 }
|
|
1795 }
|
|
1796 }
|
|
1797 }
|
|
1798
|
|
1799 // Extra check
|
|
1800 if (tparam && tparam->ty == Tstruct)
|
|
1801 {
|
|
1802 TypeStruct *tp = (TypeStruct *)tparam;
|
|
1803
|
|
1804 if (sym != tp->sym)
|
|
1805 return MATCHnomatch;
|
|
1806 }
|
|
1807 return Type::deduceType(sc, tparam, parameters, dedtypes);
|
|
1808 }
|
|
1809
|
|
1810 MATCH TypeEnum::deduceType(Scope *sc, Type *tparam, TemplateParameters *parameters, Objects *dedtypes)
|
|
1811 {
|
|
1812 // Extra check
|
|
1813 if (tparam && tparam->ty == Tenum)
|
|
1814 {
|
|
1815 TypeEnum *tp = (TypeEnum *)tparam;
|
|
1816
|
|
1817 if (sym != tp->sym)
|
|
1818 return MATCHnomatch;
|
|
1819 }
|
|
1820 return Type::deduceType(sc, tparam, parameters, dedtypes);
|
|
1821 }
|
|
1822
|
|
1823 MATCH TypeTypedef::deduceType(Scope *sc, Type *tparam, TemplateParameters *parameters, Objects *dedtypes)
|
|
1824 {
|
|
1825 // Extra check
|
|
1826 if (tparam && tparam->ty == Ttypedef)
|
|
1827 {
|
|
1828 TypeTypedef *tp = (TypeTypedef *)tparam;
|
|
1829
|
|
1830 if (sym != tp->sym)
|
|
1831 return MATCHnomatch;
|
|
1832 }
|
|
1833 return Type::deduceType(sc, tparam, parameters, dedtypes);
|
|
1834 }
|
|
1835
|
|
1836 MATCH TypeClass::deduceType(Scope *sc, Type *tparam, TemplateParameters *parameters, Objects *dedtypes)
|
|
1837 {
|
|
1838 //printf("TypeClass::deduceType(this = %s)\n", toChars());
|
|
1839
|
|
1840 /* If this class is a template class, and we're matching
|
|
1841 * it against a template instance, convert the class type
|
|
1842 * to a template instance, too, and try again.
|
|
1843 */
|
|
1844 TemplateInstance *ti = sym->parent->isTemplateInstance();
|
|
1845
|
|
1846 if (tparam && tparam->ty == Tinstance)
|
|
1847 {
|
|
1848 if (ti && ti->toAlias() == sym)
|
|
1849 {
|
|
1850 TypeInstance *t = new TypeInstance(0, ti);
|
|
1851 return t->deduceType(sc, tparam, parameters, dedtypes);
|
|
1852 }
|
|
1853
|
|
1854 /* Match things like:
|
|
1855 * S!(T).foo
|
|
1856 */
|
|
1857 TypeInstance *tpi = (TypeInstance *)tparam;
|
|
1858 if (tpi->idents.dim)
|
|
1859 { Identifier *id = (Identifier *)tpi->idents.data[tpi->idents.dim - 1];
|
|
1860 if (id->dyncast() == DYNCAST_IDENTIFIER && sym->ident->equals(id))
|
|
1861 {
|
|
1862 Type *tparent = sym->parent->getType();
|
|
1863 if (tparent)
|
|
1864 {
|
|
1865 /* Slice off the .foo in S!(T).foo
|
|
1866 */
|
|
1867 tpi->idents.dim--;
|
|
1868 MATCH m = tparent->deduceType(sc, tpi, parameters, dedtypes);
|
|
1869 tpi->idents.dim++;
|
|
1870 return m;
|
|
1871 }
|
|
1872 }
|
|
1873 }
|
|
1874 }
|
|
1875
|
|
1876 // Extra check
|
|
1877 if (tparam && tparam->ty == Tclass)
|
|
1878 {
|
|
1879 TypeClass *tp = (TypeClass *)tparam;
|
|
1880
|
|
1881 //printf("\t%d\n", (MATCH) implicitConvTo(tp));
|
|
1882 return (MATCH) implicitConvTo(tp);
|
|
1883 }
|
|
1884 return Type::deduceType(sc, tparam, parameters, dedtypes);
|
|
1885 }
|
|
1886
|
|
1887 /* ======================== TemplateParameter =============================== */
|
|
1888
|
|
1889 TemplateParameter::TemplateParameter(Loc loc, Identifier *ident)
|
|
1890 {
|
|
1891 this->loc = loc;
|
|
1892 this->ident = ident;
|
|
1893 this->sparam = NULL;
|
|
1894 }
|
|
1895
|
|
1896 TemplateTypeParameter *TemplateParameter::isTemplateTypeParameter()
|
|
1897 {
|
|
1898 return NULL;
|
|
1899 }
|
|
1900
|
|
1901 TemplateValueParameter *TemplateParameter::isTemplateValueParameter()
|
|
1902 {
|
|
1903 return NULL;
|
|
1904 }
|
|
1905
|
|
1906 TemplateAliasParameter *TemplateParameter::isTemplateAliasParameter()
|
|
1907 {
|
|
1908 return NULL;
|
|
1909 }
|
|
1910
|
|
1911 TemplateTupleParameter *TemplateParameter::isTemplateTupleParameter()
|
|
1912 {
|
|
1913 return NULL;
|
|
1914 }
|
|
1915
|
|
1916 #if DMDV2
|
|
1917 TemplateThisParameter *TemplateParameter::isTemplateThisParameter()
|
|
1918 {
|
|
1919 return NULL;
|
|
1920 }
|
|
1921 #endif
|
|
1922
|
|
1923 /* ======================== TemplateTypeParameter =========================== */
|
|
1924
|
|
1925 // type-parameter
|
|
1926
|
|
1927 TemplateTypeParameter::TemplateTypeParameter(Loc loc, Identifier *ident, Type *specType,
|
|
1928 Type *defaultType)
|
|
1929 : TemplateParameter(loc, ident)
|
|
1930 {
|
|
1931 this->ident = ident;
|
|
1932 this->specType = specType;
|
|
1933 this->defaultType = defaultType;
|
|
1934 }
|
|
1935
|
|
1936 TemplateTypeParameter *TemplateTypeParameter::isTemplateTypeParameter()
|
|
1937 {
|
|
1938 return this;
|
|
1939 }
|
|
1940
|
|
1941 TemplateParameter *TemplateTypeParameter::syntaxCopy()
|
|
1942 {
|
|
1943 TemplateTypeParameter *tp = new TemplateTypeParameter(loc, ident, specType, defaultType);
|
|
1944 if (tp->specType)
|
|
1945 tp->specType = specType->syntaxCopy();
|
|
1946 if (defaultType)
|
|
1947 tp->defaultType = defaultType->syntaxCopy();
|
|
1948 return tp;
|
|
1949 }
|
|
1950
|
|
1951 void TemplateTypeParameter::declareParameter(Scope *sc)
|
|
1952 {
|
|
1953 //printf("TemplateTypeParameter::declareParameter('%s')\n", ident->toChars());
|
|
1954 TypeIdentifier *ti = new TypeIdentifier(loc, ident);
|
|
1955 sparam = new AliasDeclaration(loc, ident, ti);
|
|
1956 if (!sc->insert(sparam))
|
|
1957 error(loc, "parameter '%s' multiply defined", ident->toChars());
|
|
1958 }
|
|
1959
|
|
1960 void TemplateTypeParameter::semantic(Scope *sc)
|
|
1961 {
|
|
1962 //printf("TemplateTypeParameter::semantic('%s')\n", ident->toChars());
|
|
1963 if (specType)
|
|
1964 {
|
|
1965 specType = specType->semantic(loc, sc);
|
|
1966 }
|
|
1967 #if 0 // Don't do semantic() until instantiation
|
|
1968 if (defaultType)
|
|
1969 {
|
|
1970 defaultType = defaultType->semantic(loc, sc);
|
|
1971 }
|
|
1972 #endif
|
|
1973 }
|
|
1974
|
|
1975 /****************************************
|
|
1976 * Determine if two TemplateParameters are the same
|
|
1977 * as far as TemplateDeclaration overloading goes.
|
|
1978 * Returns:
|
|
1979 * 1 match
|
|
1980 * 0 no match
|
|
1981 */
|
|
1982
|
|
1983 int TemplateTypeParameter::overloadMatch(TemplateParameter *tp)
|
|
1984 {
|
|
1985 TemplateTypeParameter *ttp = tp->isTemplateTypeParameter();
|
|
1986
|
|
1987 if (ttp)
|
|
1988 {
|
|
1989 if (specType != ttp->specType)
|
|
1990 goto Lnomatch;
|
|
1991
|
|
1992 if (specType && !specType->equals(ttp->specType))
|
|
1993 goto Lnomatch;
|
|
1994
|
|
1995 return 1; // match
|
|
1996 }
|
|
1997
|
|
1998 Lnomatch:
|
|
1999 return 0;
|
|
2000 }
|
|
2001
|
|
2002 /*******************************************
|
|
2003 * Match to a particular TemplateParameter.
|
|
2004 * Input:
|
|
2005 * i i'th argument
|
|
2006 * tiargs[] actual arguments to template instance
|
|
2007 * parameters[] template parameters
|
|
2008 * dedtypes[] deduced arguments to template instance
|
|
2009 * *psparam set to symbol declared and initialized to dedtypes[i]
|
|
2010 */
|
|
2011
|
|
2012 MATCH TemplateTypeParameter::matchArg(Scope *sc, Objects *tiargs,
|
|
2013 int i, TemplateParameters *parameters, Objects *dedtypes,
|
|
2014 Declaration **psparam)
|
|
2015 {
|
|
2016 //printf("TemplateTypeParameter::matchArg()\n");
|
|
2017 Type *t;
|
|
2018 Object *oarg;
|
|
2019 MATCH m = MATCHexact;
|
|
2020 Type *ta;
|
|
2021
|
|
2022 if (i < tiargs->dim)
|
|
2023 oarg = (Object *)tiargs->data[i];
|
|
2024 else
|
|
2025 { // Get default argument instead
|
|
2026 oarg = defaultArg(sc);
|
|
2027 if (!oarg)
|
|
2028 { assert(i < dedtypes->dim);
|
|
2029 // It might have already been deduced
|
|
2030 oarg = (Object *)dedtypes->data[i];
|
|
2031 if (!oarg)
|
|
2032 goto Lnomatch;
|
|
2033 }
|
|
2034 }
|
|
2035
|
|
2036 ta = isType(oarg);
|
|
2037 if (!ta)
|
|
2038 goto Lnomatch;
|
|
2039 //printf("ta is %s\n", ta->toChars());
|
|
2040
|
|
2041 t = (Type *)dedtypes->data[i];
|
|
2042
|
|
2043 if (specType)
|
|
2044 {
|
|
2045 //printf("\tcalling deduceType(): ta is %s, specType is %s\n", ta->toChars(), specType->toChars());
|
|
2046 MATCH m2 = ta->deduceType(sc, specType, parameters, dedtypes);
|
|
2047 if (m2 == MATCHnomatch)
|
|
2048 { //printf("\tfailed deduceType\n");
|
|
2049 goto Lnomatch;
|
|
2050 }
|
|
2051
|
|
2052 if (m2 < m)
|
|
2053 m = m2;
|
|
2054 t = (Type *)dedtypes->data[i];
|
|
2055 }
|
|
2056 else
|
|
2057 {
|
|
2058 // So that matches with specializations are better
|
|
2059 m = MATCHconvert;
|
|
2060 if (t)
|
|
2061 { // Must match already deduced type
|
|
2062
|
|
2063 m = MATCHexact;
|
|
2064 if (!t->equals(ta))
|
|
2065 { //printf("t = %s ta = %s\n", t->toChars(), ta->toChars());
|
|
2066 goto Lnomatch;
|
|
2067 }
|
|
2068 }
|
|
2069 }
|
|
2070
|
|
2071 if (!t)
|
|
2072 {
|
|
2073 dedtypes->data[i] = ta;
|
|
2074 t = ta;
|
|
2075 }
|
|
2076 *psparam = new AliasDeclaration(loc, ident, t);
|
|
2077 //printf("\tm = %d\n", m);
|
|
2078 return m;
|
|
2079
|
|
2080 Lnomatch:
|
|
2081 *psparam = NULL;
|
|
2082 //printf("\tm = %d\n", MATCHnomatch);
|
|
2083 return MATCHnomatch;
|
|
2084 }
|
|
2085
|
|
2086
|
|
2087 void TemplateTypeParameter::print(Object *oarg, Object *oded)
|
|
2088 {
|
|
2089 printf(" %s\n", ident->toChars());
|
|
2090
|
|
2091 Type *t = isType(oarg);
|
|
2092 Type *ta = isType(oded);
|
|
2093
|
|
2094 assert(ta);
|
|
2095
|
|
2096 if (specType)
|
|
2097 printf("\tSpecialization: %s\n", specType->toChars());
|
|
2098 if (defaultType)
|
|
2099 printf("\tDefault: %s\n", defaultType->toChars());
|
|
2100 printf("\tArgument: %s\n", t ? t->toChars() : "NULL");
|
|
2101 printf("\tDeduced Type: %s\n", ta->toChars());
|
|
2102 }
|
|
2103
|
|
2104
|
|
2105 void TemplateTypeParameter::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
|
|
2106 {
|
|
2107 buf->writestring(ident->toChars());
|
|
2108 if (specType)
|
|
2109 {
|
|
2110 buf->writestring(" : ");
|
|
2111 specType->toCBuffer(buf, NULL, hgs);
|
|
2112 }
|
|
2113 if (defaultType)
|
|
2114 {
|
|
2115 buf->writestring(" = ");
|
|
2116 defaultType->toCBuffer(buf, NULL, hgs);
|
|
2117 }
|
|
2118 }
|
|
2119
|
|
2120
|
|
2121 void *TemplateTypeParameter::dummyArg()
|
|
2122 { Type *t;
|
|
2123
|
|
2124 if (specType)
|
|
2125 t = specType;
|
|
2126 else
|
|
2127 { // Use this for alias-parameter's too (?)
|
|
2128 t = new TypeIdentifier(loc, ident);
|
|
2129 }
|
|
2130 return (void *)t;
|
|
2131 }
|
|
2132
|
|
2133
|
|
2134 Object *TemplateTypeParameter::specialization()
|
|
2135 {
|
|
2136 return specType;
|
|
2137 }
|
|
2138
|
|
2139
|
|
2140 Object *TemplateTypeParameter::defaultArg(Scope *sc)
|
|
2141 {
|
|
2142 Type *t;
|
|
2143
|
|
2144 t = defaultType;
|
|
2145 if (t)
|
|
2146 {
|
|
2147 t = t->syntaxCopy();
|
|
2148 t = t->semantic(loc, sc);
|
|
2149 }
|
|
2150 return t;
|
|
2151 }
|
|
2152
|
|
2153 /* ======================== TemplateThisParameter =========================== */
|
|
2154
|
|
2155 #if DMDV2
|
|
2156 // this-parameter
|
|
2157
|
|
2158 TemplateThisParameter::TemplateThisParameter(Loc loc, Identifier *ident,
|
|
2159 Type *specType,
|
|
2160 Type *defaultType)
|
|
2161 : TemplateTypeParameter(loc, ident, specType, defaultType)
|
|
2162 {
|
|
2163 }
|
|
2164
|
|
2165 TemplateThisParameter *TemplateThisParameter::isTemplateThisParameter()
|
|
2166 {
|
|
2167 return this;
|
|
2168 }
|
|
2169
|
|
2170 TemplateParameter *TemplateThisParameter::syntaxCopy()
|
|
2171 {
|
|
2172 TemplateThisParameter *tp = new TemplateThisParameter(loc, ident, specType, defaultType);
|
|
2173 if (tp->specType)
|
|
2174 tp->specType = specType->syntaxCopy();
|
|
2175 if (defaultType)
|
|
2176 tp->defaultType = defaultType->syntaxCopy();
|
|
2177 return tp;
|
|
2178 }
|
|
2179
|
|
2180 void TemplateThisParameter::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
|
|
2181 {
|
|
2182 buf->writestring("this ");
|
|
2183 TemplateTypeParameter::toCBuffer(buf, hgs);
|
|
2184 }
|
|
2185 #endif
|
|
2186
|
|
2187 /* ======================== TemplateAliasParameter ========================== */
|
|
2188
|
|
2189 // alias-parameter
|
|
2190
|
|
2191 Dsymbol *TemplateAliasParameter::sdummy = NULL;
|
|
2192
|
|
2193 TemplateAliasParameter::TemplateAliasParameter(Loc loc, Identifier *ident, Type *specAliasT, Type *defaultAlias)
|
|
2194 : TemplateParameter(loc, ident)
|
|
2195 {
|
|
2196 this->ident = ident;
|
|
2197 this->specAliasT = specAliasT;
|
|
2198 this->defaultAlias = defaultAlias;
|
|
2199
|
|
2200 this->specAlias = NULL;
|
|
2201 }
|
|
2202
|
|
2203 TemplateAliasParameter *TemplateAliasParameter::isTemplateAliasParameter()
|
|
2204 {
|
|
2205 return this;
|
|
2206 }
|
|
2207
|
|
2208 TemplateParameter *TemplateAliasParameter::syntaxCopy()
|
|
2209 {
|
|
2210 TemplateAliasParameter *tp = new TemplateAliasParameter(loc, ident, specAliasT, defaultAlias);
|
|
2211 if (tp->specAliasT)
|
|
2212 tp->specAliasT = specAliasT->syntaxCopy();
|
|
2213 if (defaultAlias)
|
|
2214 tp->defaultAlias = defaultAlias->syntaxCopy();
|
|
2215 return tp;
|
|
2216 }
|
|
2217
|
|
2218 void TemplateAliasParameter::declareParameter(Scope *sc)
|
|
2219 {
|
|
2220 TypeIdentifier *ti = new TypeIdentifier(loc, ident);
|
|
2221 sparam = new AliasDeclaration(loc, ident, ti);
|
|
2222 if (!sc->insert(sparam))
|
|
2223 error(loc, "parameter '%s' multiply defined", ident->toChars());
|
|
2224 }
|
|
2225
|
|
2226 void TemplateAliasParameter::semantic(Scope *sc)
|
|
2227 {
|
|
2228 if (specAliasT)
|
|
2229 {
|
|
2230 specAlias = specAliasT->toDsymbol(sc);
|
|
2231 if (!specAlias)
|
|
2232 error(loc, "%s is not a symbol", specAliasT->toChars());
|
|
2233 }
|
|
2234 #if 0 // Don't do semantic() until instantiation
|
|
2235 if (defaultAlias)
|
|
2236 defaultAlias = defaultAlias->semantic(loc, sc);
|
|
2237 #endif
|
|
2238 }
|
|
2239
|
|
2240 int TemplateAliasParameter::overloadMatch(TemplateParameter *tp)
|
|
2241 {
|
|
2242 TemplateAliasParameter *tap = tp->isTemplateAliasParameter();
|
|
2243
|
|
2244 if (tap)
|
|
2245 {
|
|
2246 if (specAlias != tap->specAlias)
|
|
2247 goto Lnomatch;
|
|
2248
|
|
2249 return 1; // match
|
|
2250 }
|
|
2251
|
|
2252 Lnomatch:
|
|
2253 return 0;
|
|
2254 }
|
|
2255
|
|
2256 MATCH TemplateAliasParameter::matchArg(Scope *sc,
|
|
2257 Objects *tiargs, int i, TemplateParameters *parameters, Objects *dedtypes,
|
|
2258 Declaration **psparam)
|
|
2259 {
|
|
2260 Dsymbol *sa;
|
|
2261 Object *oarg;
|
|
2262 Expression *ea;
|
|
2263
|
|
2264 //printf("TemplateAliasParameter::matchArg()\n");
|
|
2265
|
|
2266 if (i < tiargs->dim)
|
|
2267 oarg = (Object *)tiargs->data[i];
|
|
2268 else
|
|
2269 { // Get default argument instead
|
|
2270 oarg = defaultArg(sc);
|
|
2271 if (!oarg)
|
|
2272 { assert(i < dedtypes->dim);
|
|
2273 // It might have already been deduced
|
|
2274 oarg = (Object *)dedtypes->data[i];
|
|
2275 if (!oarg)
|
|
2276 goto Lnomatch;
|
|
2277 }
|
|
2278 }
|
|
2279
|
|
2280 sa = getDsymbol(oarg);
|
|
2281 if (!sa)
|
|
2282 goto Lnomatch;
|
|
2283
|
|
2284 if (specAlias)
|
|
2285 {
|
|
2286 if (!sa || sa == sdummy)
|
|
2287 goto Lnomatch;
|
|
2288 if (sa != specAlias)
|
|
2289 goto Lnomatch;
|
|
2290 }
|
|
2291 else if (dedtypes->data[i])
|
|
2292 { // Must match already deduced symbol
|
|
2293 Dsymbol *s = (Dsymbol *)dedtypes->data[i];
|
|
2294
|
|
2295 if (!sa || s != sa)
|
|
2296 goto Lnomatch;
|
|
2297 }
|
|
2298 dedtypes->data[i] = sa;
|
|
2299
|
|
2300 *psparam = new AliasDeclaration(loc, ident, sa);
|
|
2301 return MATCHexact;
|
|
2302
|
|
2303 Lnomatch:
|
|
2304 *psparam = NULL;
|
|
2305 return MATCHnomatch;
|
|
2306 }
|
|
2307
|
|
2308
|
|
2309 void TemplateAliasParameter::print(Object *oarg, Object *oded)
|
|
2310 {
|
|
2311 printf(" %s\n", ident->toChars());
|
|
2312
|
|
2313 Dsymbol *sa = isDsymbol(oded);
|
|
2314 assert(sa);
|
|
2315
|
|
2316 printf("\tArgument alias: %s\n", sa->toChars());
|
|
2317 }
|
|
2318
|
|
2319 void TemplateAliasParameter::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
|
|
2320 {
|
|
2321 buf->writestring("alias ");
|
|
2322 buf->writestring(ident->toChars());
|
|
2323 if (specAliasT)
|
|
2324 {
|
|
2325 buf->writestring(" : ");
|
|
2326 specAliasT->toCBuffer(buf, NULL, hgs);
|
|
2327 }
|
|
2328 if (defaultAlias)
|
|
2329 {
|
|
2330 buf->writestring(" = ");
|
|
2331 defaultAlias->toCBuffer(buf, NULL, hgs);
|
|
2332 }
|
|
2333 }
|
|
2334
|
|
2335
|
|
2336 void *TemplateAliasParameter::dummyArg()
|
|
2337 { Dsymbol *s;
|
|
2338
|
|
2339 s = specAlias;
|
|
2340 if (!s)
|
|
2341 {
|
|
2342 if (!sdummy)
|
|
2343 sdummy = new Dsymbol();
|
|
2344 s = sdummy;
|
|
2345 }
|
|
2346 return (void*)s;
|
|
2347 }
|
|
2348
|
|
2349
|
|
2350 Object *TemplateAliasParameter::specialization()
|
|
2351 {
|
|
2352 return specAliasT;
|
|
2353 }
|
|
2354
|
|
2355
|
|
2356 Object *TemplateAliasParameter::defaultArg(Scope *sc)
|
|
2357 {
|
|
2358 Dsymbol *s = NULL;
|
|
2359
|
|
2360 if (defaultAlias)
|
|
2361 {
|
|
2362 s = defaultAlias->toDsymbol(sc);
|
|
2363 if (!s)
|
|
2364 error("%s is not a symbol", defaultAlias->toChars());
|
|
2365 }
|
|
2366 return s;
|
|
2367 }
|
|
2368
|
|
2369 /* ======================== TemplateValueParameter ========================== */
|
|
2370
|
|
2371 // value-parameter
|
|
2372
|
|
2373 Expression *TemplateValueParameter::edummy = NULL;
|
|
2374
|
|
2375 TemplateValueParameter::TemplateValueParameter(Loc loc, Identifier *ident, Type *valType,
|
|
2376 Expression *specValue, Expression *defaultValue)
|
|
2377 : TemplateParameter(loc, ident)
|
|
2378 {
|
|
2379 this->ident = ident;
|
|
2380 this->valType = valType;
|
|
2381 this->specValue = specValue;
|
|
2382 this->defaultValue = defaultValue;
|
|
2383 }
|
|
2384
|
|
2385 TemplateValueParameter *TemplateValueParameter::isTemplateValueParameter()
|
|
2386 {
|
|
2387 return this;
|
|
2388 }
|
|
2389
|
|
2390 TemplateParameter *TemplateValueParameter::syntaxCopy()
|
|
2391 {
|
|
2392 TemplateValueParameter *tp =
|
|
2393 new TemplateValueParameter(loc, ident, valType, specValue, defaultValue);
|
|
2394 tp->valType = valType->syntaxCopy();
|
|
2395 if (specValue)
|
|
2396 tp->specValue = specValue->syntaxCopy();
|
|
2397 if (defaultValue)
|
|
2398 tp->defaultValue = defaultValue->syntaxCopy();
|
|
2399 return tp;
|
|
2400 }
|
|
2401
|
|
2402 void TemplateValueParameter::declareParameter(Scope *sc)
|
|
2403 {
|
|
2404 VarDeclaration *v = new VarDeclaration(loc, valType, ident, NULL);
|
|
2405 v->storage_class = STCtemplateparameter;
|
|
2406 if (!sc->insert(v))
|
|
2407 error(loc, "parameter '%s' multiply defined", ident->toChars());
|
|
2408 sparam = v;
|
|
2409 }
|
|
2410
|
|
2411 void TemplateValueParameter::semantic(Scope *sc)
|
|
2412 {
|
|
2413 sparam->semantic(sc);
|
|
2414 valType = valType->semantic(loc, sc);
|
|
2415 if (!(valType->isintegral() || valType->isfloating() || valType->isString()) &&
|
|
2416 valType->ty != Tident)
|
|
2417 error(loc, "arithmetic/string type expected for value-parameter, not %s", valType->toChars());
|
|
2418
|
|
2419 if (specValue)
|
|
2420 { Expression *e = specValue;
|
|
2421
|
|
2422 e = e->semantic(sc);
|
|
2423 e = e->implicitCastTo(sc, valType);
|
|
2424 e = e->optimize(WANTvalue | WANTinterpret);
|
|
2425 if (e->op == TOKint64 || e->op == TOKfloat64 ||
|
|
2426 e->op == TOKcomplex80 || e->op == TOKnull || e->op == TOKstring)
|
|
2427 specValue = e;
|
|
2428 //e->toInteger();
|
|
2429 }
|
|
2430
|
|
2431 #if 0 // defer semantic analysis to arg match
|
|
2432 if (defaultValue)
|
|
2433 { Expression *e = defaultValue;
|
|
2434
|
|
2435 e = e->semantic(sc);
|
|
2436 e = e->implicitCastTo(sc, valType);
|
|
2437 e = e->optimize(WANTvalue | WANTinterpret);
|
|
2438 if (e->op == TOKint64)
|
|
2439 defaultValue = e;
|
|
2440 //e->toInteger();
|
|
2441 }
|
|
2442 #endif
|
|
2443 }
|
|
2444
|
|
2445 int TemplateValueParameter::overloadMatch(TemplateParameter *tp)
|
|
2446 {
|
|
2447 TemplateValueParameter *tvp = tp->isTemplateValueParameter();
|
|
2448
|
|
2449 if (tvp)
|
|
2450 {
|
|
2451 if (valType != tvp->valType)
|
|
2452 goto Lnomatch;
|
|
2453
|
|
2454 if (valType && !valType->equals(tvp->valType))
|
|
2455 goto Lnomatch;
|
|
2456
|
|
2457 if (specValue != tvp->specValue)
|
|
2458 goto Lnomatch;
|
|
2459
|
|
2460 return 1; // match
|
|
2461 }
|
|
2462
|
|
2463 Lnomatch:
|
|
2464 return 0;
|
|
2465 }
|
|
2466
|
|
2467
|
|
2468 MATCH TemplateValueParameter::matchArg(Scope *sc,
|
|
2469 Objects *tiargs, int i, TemplateParameters *parameters, Objects *dedtypes,
|
|
2470 Declaration **psparam)
|
|
2471 {
|
|
2472 //printf("TemplateValueParameter::matchArg()\n");
|
|
2473
|
|
2474 Initializer *init;
|
|
2475 Declaration *sparam;
|
|
2476 MATCH m = MATCHexact;
|
|
2477 Expression *ei;
|
|
2478 Object *oarg;
|
|
2479
|
|
2480 if (i < tiargs->dim)
|
|
2481 oarg = (Object *)tiargs->data[i];
|
|
2482 else
|
|
2483 { // Get default argument instead
|
|
2484 oarg = defaultArg(sc);
|
|
2485 if (!oarg)
|
|
2486 { assert(i < dedtypes->dim);
|
|
2487 // It might have already been deduced
|
|
2488 oarg = (Object *)dedtypes->data[i];
|
|
2489 if (!oarg)
|
|
2490 goto Lnomatch;
|
|
2491 }
|
|
2492 }
|
|
2493
|
|
2494 ei = isExpression(oarg);
|
|
2495 Type *vt;
|
|
2496
|
|
2497 if (!ei && oarg)
|
|
2498 goto Lnomatch;
|
|
2499
|
|
2500 if (specValue)
|
|
2501 {
|
|
2502 if (!ei || ei == edummy)
|
|
2503 goto Lnomatch;
|
|
2504
|
|
2505 Expression *e = specValue;
|
|
2506
|
|
2507 e = e->semantic(sc);
|
|
2508 e = e->implicitCastTo(sc, valType);
|
|
2509 e = e->optimize(WANTvalue | WANTinterpret);
|
|
2510
|
|
2511 ei = ei->syntaxCopy();
|
|
2512 ei = ei->semantic(sc);
|
|
2513 ei = ei->optimize(WANTvalue | WANTinterpret);
|
|
2514 //printf("ei: %s, %s\n", ei->toChars(), ei->type->toChars());
|
|
2515 //printf("e : %s, %s\n", e->toChars(), e->type->toChars());
|
|
2516 if (!ei->equals(e))
|
|
2517 goto Lnomatch;
|
|
2518 }
|
|
2519 else if (dedtypes->data[i])
|
|
2520 { // Must match already deduced value
|
|
2521 Expression *e = (Expression *)dedtypes->data[i];
|
|
2522
|
|
2523 if (!ei || !ei->equals(e))
|
|
2524 goto Lnomatch;
|
|
2525 }
|
|
2526 Lmatch:
|
|
2527 //printf("valType: %s, ty = %d\n", valType->toChars(), valType->ty);
|
|
2528 vt = valType->semantic(0, sc);
|
|
2529 //printf("ei: %s, %s\n", ei->toChars(), ei->type->toChars());
|
|
2530 if (ei->type)
|
|
2531 {
|
|
2532 m = (MATCH)ei->implicitConvTo(vt);
|
|
2533 //printf("m: %d\n", m);
|
|
2534 if (!m)
|
|
2535 goto Lnomatch;
|
|
2536 }
|
|
2537 dedtypes->data[i] = ei;
|
|
2538
|
|
2539 init = new ExpInitializer(loc, ei);
|
|
2540 sparam = new VarDeclaration(loc, vt, ident, init);
|
|
2541 sparam->storage_class = STCconst;
|
|
2542 *psparam = sparam;
|
|
2543 return m;
|
|
2544
|
|
2545 Lnomatch:
|
|
2546 //printf("\tno match\n");
|
|
2547 *psparam = NULL;
|
|
2548 return MATCHnomatch;
|
|
2549 }
|
|
2550
|
|
2551
|
|
2552 void TemplateValueParameter::print(Object *oarg, Object *oded)
|
|
2553 {
|
|
2554 printf(" %s\n", ident->toChars());
|
|
2555
|
|
2556 Expression *ea = isExpression(oded);
|
|
2557
|
|
2558 if (specValue)
|
|
2559 printf("\tSpecialization: %s\n", specValue->toChars());
|
|
2560 printf("\tArgument Value: %s\n", ea ? ea->toChars() : "NULL");
|
|
2561 }
|
|
2562
|
|
2563
|
|
2564 void TemplateValueParameter::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
|
|
2565 {
|
|
2566 valType->toCBuffer(buf, ident, hgs);
|
|
2567 if (specValue)
|
|
2568 {
|
|
2569 buf->writestring(" : ");
|
|
2570 specValue->toCBuffer(buf, hgs);
|
|
2571 }
|
|
2572 if (defaultValue)
|
|
2573 {
|
|
2574 buf->writestring(" = ");
|
|
2575 defaultValue->toCBuffer(buf, hgs);
|
|
2576 }
|
|
2577 }
|
|
2578
|
|
2579
|
|
2580 void *TemplateValueParameter::dummyArg()
|
|
2581 { Expression *e;
|
|
2582
|
|
2583 e = specValue;
|
|
2584 if (!e)
|
|
2585 {
|
|
2586 // Create a dummy value
|
|
2587 if (!edummy)
|
|
2588 edummy = valType->defaultInit();
|
|
2589 e = edummy;
|
|
2590 }
|
|
2591 return (void *)e;
|
|
2592 }
|
|
2593
|
|
2594
|
|
2595 Object *TemplateValueParameter::specialization()
|
|
2596 {
|
|
2597 return specValue;
|
|
2598 }
|
|
2599
|
|
2600
|
|
2601 Object *TemplateValueParameter::defaultArg(Scope *sc)
|
|
2602 {
|
|
2603 Expression *e = defaultValue;
|
|
2604 if (e)
|
|
2605 {
|
|
2606 e = e->syntaxCopy();
|
|
2607 e = e->semantic(sc);
|
|
2608 #if DMDV2
|
|
2609 if (e->op == TOKdefault)
|
|
2610 { DefaultInitExp *de = (DefaultInitExp *)e;
|
|
2611 e = de->resolve(loc, sc);
|
|
2612 }
|
|
2613 #endif
|
|
2614 }
|
|
2615 return e;
|
|
2616 }
|
|
2617
|
|
2618 /* ======================== TemplateTupleParameter ========================== */
|
|
2619
|
|
2620 // variadic-parameter
|
|
2621
|
|
2622 TemplateTupleParameter::TemplateTupleParameter(Loc loc, Identifier *ident)
|
|
2623 : TemplateParameter(loc, ident)
|
|
2624 {
|
|
2625 this->ident = ident;
|
|
2626 }
|
|
2627
|
|
2628 TemplateTupleParameter *TemplateTupleParameter::isTemplateTupleParameter()
|
|
2629 {
|
|
2630 return this;
|
|
2631 }
|
|
2632
|
|
2633 TemplateParameter *TemplateTupleParameter::syntaxCopy()
|
|
2634 {
|
|
2635 TemplateTupleParameter *tp = new TemplateTupleParameter(loc, ident);
|
|
2636 return tp;
|
|
2637 }
|
|
2638
|
|
2639 void TemplateTupleParameter::declareParameter(Scope *sc)
|
|
2640 {
|
|
2641 TypeIdentifier *ti = new TypeIdentifier(loc, ident);
|
|
2642 sparam = new AliasDeclaration(loc, ident, ti);
|
|
2643 if (!sc->insert(sparam))
|
|
2644 error(loc, "parameter '%s' multiply defined", ident->toChars());
|
|
2645 }
|
|
2646
|
|
2647 void TemplateTupleParameter::semantic(Scope *sc)
|
|
2648 {
|
|
2649 }
|
|
2650
|
|
2651 int TemplateTupleParameter::overloadMatch(TemplateParameter *tp)
|
|
2652 {
|
|
2653 TemplateTupleParameter *tvp = tp->isTemplateTupleParameter();
|
|
2654
|
|
2655 if (tvp)
|
|
2656 {
|
|
2657 return 1; // match
|
|
2658 }
|
|
2659
|
|
2660 Lnomatch:
|
|
2661 return 0;
|
|
2662 }
|
|
2663
|
|
2664 MATCH TemplateTupleParameter::matchArg(Scope *sc,
|
|
2665 Objects *tiargs, int i, TemplateParameters *parameters,
|
|
2666 Objects *dedtypes,
|
|
2667 Declaration **psparam)
|
|
2668 {
|
|
2669 //printf("TemplateTupleParameter::matchArg()\n");
|
|
2670
|
|
2671 /* The rest of the actual arguments (tiargs[]) form the match
|
|
2672 * for the variadic parameter.
|
|
2673 */
|
|
2674 assert(i + 1 == dedtypes->dim); // must be the last one
|
|
2675 Tuple *ovar;
|
|
2676 if (i + 1 == tiargs->dim && isTuple((Object *)tiargs->data[i]))
|
|
2677 ovar = isTuple((Object *)tiargs->data[i]);
|
|
2678 else
|
|
2679 {
|
|
2680 ovar = new Tuple();
|
|
2681 //printf("ovar = %p\n", ovar);
|
|
2682 if (i < tiargs->dim)
|
|
2683 {
|
|
2684 //printf("i = %d, tiargs->dim = %d\n", i, tiargs->dim);
|
|
2685 ovar->objects.setDim(tiargs->dim - i);
|
|
2686 for (size_t j = 0; j < ovar->objects.dim; j++)
|
|
2687 ovar->objects.data[j] = tiargs->data[i + j];
|
|
2688 }
|
|
2689 }
|
|
2690 *psparam = new TupleDeclaration(loc, ident, &ovar->objects);
|
|
2691 dedtypes->data[i] = (void *)ovar;
|
|
2692 return MATCHexact;
|
|
2693 }
|
|
2694
|
|
2695
|
|
2696 void TemplateTupleParameter::print(Object *oarg, Object *oded)
|
|
2697 {
|
|
2698 printf(" %s... [", ident->toChars());
|
|
2699 Tuple *v = isTuple(oded);
|
|
2700 assert(v);
|
|
2701
|
|
2702 //printf("|%d| ", v->objects.dim);
|
|
2703 for (int i = 0; i < v->objects.dim; i++)
|
|
2704 {
|
|
2705 if (i)
|
|
2706 printf(", ");
|
|
2707
|
|
2708 Object *o = (Object *)v->objects.data[i];
|
|
2709
|
|
2710 Dsymbol *sa = isDsymbol(o);
|
|
2711 if (sa)
|
|
2712 printf("alias: %s", sa->toChars());
|
|
2713
|
|
2714 Type *ta = isType(o);
|
|
2715 if (ta)
|
|
2716 printf("type: %s", ta->toChars());
|
|
2717
|
|
2718 Expression *ea = isExpression(o);
|
|
2719 if (ea)
|
|
2720 printf("exp: %s", ea->toChars());
|
|
2721
|
|
2722 assert(!isTuple(o)); // no nested Tuple arguments
|
|
2723 }
|
|
2724
|
|
2725 printf("]\n");
|
|
2726 }
|
|
2727
|
|
2728 void TemplateTupleParameter::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
|
|
2729 {
|
|
2730 buf->writestring(ident->toChars());
|
|
2731 buf->writestring("...");
|
|
2732 }
|
|
2733
|
|
2734
|
|
2735 void *TemplateTupleParameter::dummyArg()
|
|
2736 {
|
|
2737 return NULL;
|
|
2738 }
|
|
2739
|
|
2740
|
|
2741 Object *TemplateTupleParameter::specialization()
|
|
2742 {
|
|
2743 return NULL;
|
|
2744 }
|
|
2745
|
|
2746
|
|
2747 Object *TemplateTupleParameter::defaultArg(Scope *sc)
|
|
2748 {
|
|
2749 return NULL;
|
|
2750 }
|
|
2751
|
|
2752 /* ======================== TemplateInstance ================================ */
|
|
2753
|
|
2754 TemplateInstance::TemplateInstance(Loc loc, Identifier *ident)
|
|
2755 : ScopeDsymbol(NULL)
|
|
2756 {
|
|
2757 #if LOG
|
|
2758 printf("TemplateInstance(this = %p, ident = '%s')\n", this, ident ? ident->toChars() : "null");
|
|
2759 #endif
|
|
2760 this->loc = loc;
|
|
2761 this->name = ident;
|
|
2762 this->tiargs = NULL;
|
|
2763 this->tempdecl = NULL;
|
|
2764 this->inst = NULL;
|
|
2765 this->argsym = NULL;
|
|
2766 this->aliasdecl = NULL;
|
|
2767 this->semanticdone = 0;
|
|
2768 this->semantictiargsdone = 0;
|
|
2769 this->withsym = NULL;
|
|
2770 this->nest = 0;
|
|
2771 this->havetempdecl = 0;
|
|
2772 this->isnested = NULL;
|
|
2773 this->errors = 0;
|
|
2774 }
|
|
2775
|
|
2776
|
|
2777 TemplateInstance::TemplateInstance(Loc loc, TemplateDeclaration *td, Objects *tiargs)
|
|
2778 : ScopeDsymbol(NULL)
|
|
2779 {
|
|
2780 #if LOG
|
|
2781 printf("TemplateInstance(this = %p, tempdecl = '%s')\n", this, td->toChars());
|
|
2782 #endif
|
|
2783 this->loc = loc;
|
|
2784 this->name = td->ident;
|
|
2785 this->tiargs = tiargs;
|
|
2786 this->tempdecl = td;
|
|
2787 this->inst = NULL;
|
|
2788 this->argsym = NULL;
|
|
2789 this->aliasdecl = NULL;
|
|
2790 this->semanticdone = 0;
|
|
2791 this->semantictiargsdone = 1;
|
|
2792 this->withsym = NULL;
|
|
2793 this->nest = 0;
|
|
2794 this->havetempdecl = 1;
|
|
2795 this->isnested = NULL;
|
|
2796 this->errors = 0;
|
|
2797
|
|
2798 assert((size_t)tempdecl->scope > 0x10000);
|
|
2799 }
|
|
2800
|
|
2801
|
|
2802 Objects *TemplateInstance::arraySyntaxCopy(Objects *objs)
|
|
2803 {
|
|
2804 Objects *a = NULL;
|
|
2805 if (objs)
|
|
2806 { a = new Objects();
|
|
2807 a->setDim(objs->dim);
|
|
2808 for (size_t i = 0; i < objs->dim; i++)
|
|
2809 {
|
|
2810 Type *ta = isType((Object *)objs->data[i]);
|
|
2811 if (ta)
|
|
2812 a->data[i] = ta->syntaxCopy();
|
|
2813 else
|
|
2814 {
|
|
2815 Expression *ea = isExpression((Object *)objs->data[i]);
|
|
2816 assert(ea);
|
|
2817 a->data[i] = ea->syntaxCopy();
|
|
2818 }
|
|
2819 }
|
|
2820 }
|
|
2821 return a;
|
|
2822 }
|
|
2823
|
|
2824 Dsymbol *TemplateInstance::syntaxCopy(Dsymbol *s)
|
|
2825 {
|
|
2826 TemplateInstance *ti;
|
|
2827 int i;
|
|
2828
|
|
2829 if (s)
|
|
2830 ti = (TemplateInstance *)s;
|
|
2831 else
|
|
2832 ti = new TemplateInstance(loc, name);
|
|
2833
|
|
2834 ti->tiargs = arraySyntaxCopy(tiargs);
|
|
2835
|
|
2836 ScopeDsymbol::syntaxCopy(ti);
|
|
2837 return ti;
|
|
2838 }
|
|
2839
|
|
2840
|
|
2841 void TemplateInstance::semantic(Scope *sc)
|
|
2842 {
|
|
2843 if (global.errors)
|
|
2844 {
|
|
2845 if (!global.gag)
|
|
2846 {
|
|
2847 /* Trying to soldier on rarely generates useful messages
|
|
2848 * at this point.
|
|
2849 */
|
|
2850 fatal();
|
|
2851 }
|
|
2852 return;
|
|
2853 }
|
|
2854 #if LOG
|
|
2855 printf("\n+TemplateInstance::semantic('%s', this=%p)\n", toChars(), this);
|
|
2856 #endif
|
|
2857 if (inst) // if semantic() was already run
|
|
2858 {
|
|
2859 #if LOG
|
|
2860 printf("-TemplateInstance::semantic('%s', this=%p) already run\n", inst->toChars(), inst);
|
|
2861 #endif
|
|
2862 return;
|
|
2863 }
|
|
2864
|
|
2865 if (semanticdone != 0)
|
|
2866 {
|
|
2867 error(loc, "recursive template expansion");
|
|
2868 // inst = this;
|
|
2869 return;
|
|
2870 }
|
|
2871 semanticdone = 1;
|
|
2872
|
|
2873 #if LOG
|
|
2874 printf("\tdo semantic\n");
|
|
2875 #endif
|
|
2876 if (havetempdecl)
|
|
2877 {
|
|
2878 assert((size_t)tempdecl->scope > 0x10000);
|
|
2879 // Deduce tdtypes
|
|
2880 tdtypes.setDim(tempdecl->parameters->dim);
|
|
2881 if (!tempdecl->matchWithInstance(this, &tdtypes, 0))
|
|
2882 {
|
|
2883 error("incompatible arguments for template instantiation");
|
|
2884 inst = this;
|
|
2885 return;
|
|
2886 }
|
|
2887 }
|
|
2888 else
|
|
2889 {
|
|
2890 // Run semantic on each argument, place results in tiargs[]
|
|
2891 semanticTiargs(sc);
|
|
2892
|
|
2893 tempdecl = findTemplateDeclaration(sc);
|
|
2894 if (tempdecl)
|
|
2895 tempdecl = findBestMatch(sc);
|
|
2896 if (!tempdecl || global.errors)
|
|
2897 { inst = this;
|
|
2898 //printf("error return %p, %d\n", tempdecl, global.errors);
|
|
2899 return; // error recovery
|
|
2900 }
|
|
2901 }
|
|
2902
|
|
2903 isNested(tiargs);
|
|
2904
|
|
2905 /* See if there is an existing TemplateInstantiation that already
|
|
2906 * implements the typeargs. If so, just refer to that one instead.
|
|
2907 */
|
|
2908
|
|
2909 for (size_t i = 0; i < tempdecl->instances.dim; i++)
|
|
2910 {
|
|
2911 TemplateInstance *ti = (TemplateInstance *)tempdecl->instances.data[i];
|
|
2912 #if LOG
|
|
2913 printf("\t%s: checking for match with instance %d (%p): '%s'\n", toChars(), i, ti, ti->toChars());
|
|
2914 #endif
|
|
2915 assert(tdtypes.dim == ti->tdtypes.dim);
|
|
2916
|
|
2917 // Nesting must match
|
|
2918 if (isnested != ti->isnested)
|
|
2919 continue;
|
|
2920 #if 0
|
|
2921 if (isnested && sc->parent != ti->parent)
|
|
2922 continue;
|
|
2923 #endif
|
|
2924 for (size_t j = 0; j < tdtypes.dim; j++)
|
|
2925 { Object *o1 = (Object *)tdtypes.data[j];
|
|
2926 Object *o2 = (Object *)ti->tdtypes.data[j];
|
|
2927 if (!match(o1, o2, tempdecl, sc))
|
|
2928 goto L1;
|
|
2929 }
|
|
2930
|
|
2931 // It's a match
|
|
2932 inst = ti;
|
|
2933 parent = ti->parent;
|
|
2934 #if LOG
|
|
2935 printf("\tit's a match with instance %p\n", inst);
|
|
2936 #endif
|
|
2937 return;
|
|
2938
|
|
2939 L1:
|
|
2940 ;
|
|
2941 }
|
|
2942
|
|
2943 /* So, we need to implement 'this' instance.
|
|
2944 */
|
|
2945 #if LOG
|
|
2946 printf("\timplement template instance '%s'\n", toChars());
|
|
2947 #endif
|
|
2948 unsigned errorsave = global.errors;
|
|
2949 inst = this;
|
|
2950 int tempdecl_instance_idx = tempdecl->instances.dim;
|
|
2951 tempdecl->instances.push(this);
|
|
2952 parent = tempdecl->parent;
|
|
2953 //printf("parent = '%s'\n", parent->kind());
|
|
2954
|
|
2955 ident = genIdent(); // need an identifier for name mangling purposes.
|
|
2956
|
|
2957 #if 1
|
|
2958 if (isnested)
|
|
2959 parent = isnested;
|
|
2960 #endif
|
|
2961 //printf("parent = '%s'\n", parent->kind());
|
|
2962
|
|
2963 // Add 'this' to the enclosing scope's members[] so the semantic routines
|
|
2964 // will get called on the instance members
|
|
2965 #if 1
|
|
2966 int dosemantic3 = 0;
|
|
2967 { Array *a;
|
|
2968 int i;
|
|
2969
|
|
2970 if (sc->scopesym && sc->scopesym->members && !sc->scopesym->isTemplateMixin())
|
|
2971 {
|
|
2972 //printf("\t1: adding to %s %s\n", sc->scopesym->kind(), sc->scopesym->toChars());
|
|
2973 a = sc->scopesym->members;
|
|
2974 }
|
|
2975 else
|
|
2976 { Module *m = sc->module->importedFrom;
|
|
2977 //printf("\t2: adding to module %s instead of module %s\n", m->toChars(), sc->module->toChars());
|
|
2978 a = m->members;
|
|
2979 if (m->semanticdone >= 3)
|
|
2980 dosemantic3 = 1;
|
|
2981 }
|
|
2982 for (int i = 0; 1; i++)
|
|
2983 {
|
|
2984 if (i == a->dim)
|
|
2985 {
|
|
2986 a->push(this);
|
|
2987 break;
|
|
2988 }
|
|
2989 if (this == (Dsymbol *)a->data[i]) // if already in Array
|
|
2990 break;
|
|
2991 }
|
|
2992 }
|
|
2993 #endif
|
|
2994
|
|
2995 // Copy the syntax trees from the TemplateDeclaration
|
|
2996 members = Dsymbol::arraySyntaxCopy(tempdecl->members);
|
|
2997
|
|
2998 // Create our own scope for the template parameters
|
|
2999 Scope *scope = tempdecl->scope;
|
|
3000 if (!scope)
|
|
3001 {
|
|
3002 error("forward reference to template declaration %s\n", tempdecl->toChars());
|
|
3003 return;
|
|
3004 }
|
|
3005
|
|
3006 #if LOG
|
|
3007 printf("\tcreate scope for template parameters '%s'\n", toChars());
|
|
3008 #endif
|
|
3009 argsym = new ScopeDsymbol();
|
|
3010 argsym->parent = scope->parent;
|
|
3011 scope = scope->push(argsym);
|
|
3012
|
|
3013 // Declare each template parameter as an alias for the argument type
|
|
3014 declareParameters(scope);
|
|
3015
|
|
3016 // Add members of template instance to template instance symbol table
|
|
3017 // parent = scope->scopesym;
|
|
3018 symtab = new DsymbolTable();
|
|
3019 int memnum = 0;
|
|
3020 for (int i = 0; i < members->dim; i++)
|
|
3021 {
|
|
3022 Dsymbol *s = (Dsymbol *)members->data[i];
|
|
3023 #if LOG
|
|
3024 printf("\t[%d] adding member '%s' %p kind %s to '%s', memnum = %d\n", i, s->toChars(), s, s->kind(), this->toChars(), memnum);
|
|
3025 #endif
|
|
3026 memnum |= s->addMember(scope, this, memnum);
|
|
3027 }
|
|
3028 #if LOG
|
|
3029 printf("adding members done\n");
|
|
3030 #endif
|
|
3031
|
|
3032 /* See if there is only one member of template instance, and that
|
|
3033 * member has the same name as the template instance.
|
|
3034 * If so, this template instance becomes an alias for that member.
|
|
3035 */
|
|
3036 //printf("members->dim = %d\n", members->dim);
|
|
3037 if (members->dim)
|
|
3038 {
|
|
3039 Dsymbol *s;
|
|
3040 if (Dsymbol::oneMembers(members, &s) && s)
|
|
3041 {
|
|
3042 //printf("s->kind = '%s'\n", s->kind());
|
|
3043 //s->print();
|
|
3044 //printf("'%s', '%s'\n", s->ident->toChars(), tempdecl->ident->toChars());
|
|
3045 if (s->ident && s->ident->equals(tempdecl->ident))
|
|
3046 {
|
|
3047 //printf("setting aliasdecl\n");
|
|
3048 aliasdecl = new AliasDeclaration(loc, s->ident, s);
|
|
3049 }
|
|
3050 }
|
|
3051 }
|
|
3052
|
|
3053 // Do semantic() analysis on template instance members
|
|
3054 #if LOG
|
|
3055 printf("\tdo semantic() on template instance members '%s'\n", toChars());
|
|
3056 #endif
|
|
3057 Scope *sc2;
|
|
3058 sc2 = scope->push(this);
|
|
3059 //printf("isnested = %d, sc->parent = %s\n", isnested, sc->parent->toChars());
|
|
3060 sc2->parent = /*isnested ? sc->parent :*/ this;
|
|
3061
|
|
3062 #if !IN_LLVM
|
|
3063 #if _WIN32
|
|
3064 __try
|
|
3065 {
|
|
3066 #endif
|
|
3067 #endif
|
|
3068 for (int i = 0; i < members->dim; i++)
|
|
3069 {
|
|
3070 Dsymbol *s = (Dsymbol *)members->data[i];
|
|
3071 //printf("\t[%d] semantic on '%s' %p kind %s in '%s'\n", i, s->toChars(), s, s->kind(), this->toChars());
|
|
3072 //printf("test: isnested = %d, sc2->parent = %s\n", isnested, sc2->parent->toChars());
|
|
3073 // if (isnested)
|
|
3074 // s->parent = sc->parent;
|
|
3075 //printf("test3: isnested = %d, s->parent = %s\n", isnested, s->parent->toChars());
|
|
3076 s->semantic(sc2);
|
|
3077 //printf("test4: isnested = %d, s->parent = %s\n", isnested, s->parent->toChars());
|
|
3078 sc2->module->runDeferredSemantic();
|
|
3079 }
|
|
3080 #if !IN_LLVM
|
|
3081 #if _WIN32
|
|
3082 }
|
|
3083 __except (__ehfilter(GetExceptionInformation()))
|
|
3084 {
|
|
3085 global.gag = 0; // ensure error message gets printed
|
|
3086 error("recursive expansion");
|
|
3087 fatal();
|
|
3088 }
|
|
3089 #endif
|
|
3090 #endif
|
|
3091
|
|
3092 /* If any of the instantiation members didn't get semantic() run
|
|
3093 * on them due to forward references, we cannot run semantic2()
|
|
3094 * or semantic3() yet.
|
|
3095 */
|
|
3096 for (size_t i = 0; i < Module::deferred.dim; i++)
|
|
3097 { Dsymbol *sd = (Dsymbol *)Module::deferred.data[i];
|
|
3098
|
|
3099 if (sd->parent == this)
|
|
3100 goto Laftersemantic;
|
|
3101 }
|
|
3102
|
|
3103 /* The problem is when to parse the initializer for a variable.
|
|
3104 * Perhaps VarDeclaration::semantic() should do it like it does
|
|
3105 * for initializers inside a function.
|
|
3106 */
|
|
3107 // if (sc->parent->isFuncDeclaration())
|
|
3108
|
|
3109 /* BUG 782: this has problems if the classes this depends on
|
|
3110 * are forward referenced. Find a way to defer semantic()
|
|
3111 * on this template.
|
|
3112 */
|
|
3113 semantic2(sc2);
|
|
3114
|
|
3115 if (sc->func || dosemantic3)
|
|
3116 {
|
|
3117 semantic3(sc2);
|
|
3118 }
|
|
3119
|
|
3120 Laftersemantic:
|
|
3121 sc2->pop();
|
|
3122
|
|
3123 scope->pop();
|
|
3124
|
|
3125 // Give additional context info if error occurred during instantiation
|
|
3126 if (global.errors != errorsave)
|
|
3127 {
|
|
3128 error("error instantiating");
|
|
3129 errors = 1;
|
|
3130 if (global.gag)
|
|
3131 tempdecl->instances.remove(tempdecl_instance_idx);
|
|
3132 }
|
|
3133
|
|
3134 #if LOG
|
|
3135 printf("-TemplateInstance::semantic('%s', this=%p)\n", toChars(), this);
|
|
3136 #endif
|
|
3137 }
|
|
3138
|
|
3139
|
|
3140 void TemplateInstance::semanticTiargs(Scope *sc)
|
|
3141 {
|
|
3142 //printf("+TemplateInstance::semanticTiargs() %s\n", toChars());
|
|
3143 if (semantictiargsdone)
|
|
3144 return;
|
|
3145 semantictiargsdone = 1;
|
|
3146 semanticTiargs(loc, sc, tiargs);
|
|
3147 }
|
|
3148
|
|
3149 void TemplateInstance::semanticTiargs(Loc loc, Scope *sc, Objects *tiargs)
|
|
3150 {
|
|
3151 // Run semantic on each argument, place results in tiargs[]
|
|
3152 //printf("+TemplateInstance::semanticTiargs() %s\n", toChars());
|
|
3153 if (!tiargs)
|
|
3154 return;
|
|
3155 for (size_t j = 0; j < tiargs->dim; j++)
|
|
3156 {
|
|
3157 Object *o = (Object *)tiargs->data[j];
|
|
3158 Type *ta = isType(o);
|
|
3159 Expression *ea = isExpression(o);
|
|
3160 Dsymbol *sa = isDsymbol(o);
|
|
3161
|
|
3162 //printf("1: tiargs->data[%d] = %p, %p, %p, ea=%p, ta=%p\n", j, o, isDsymbol(o), isTuple(o), ea, ta);
|
|
3163 if (ta)
|
|
3164 {
|
|
3165 //printf("type %s\n", ta->toChars());
|
|
3166 // It might really be an Expression or an Alias
|
|
3167 ta->resolve(loc, sc, &ea, &ta, &sa);
|
|
3168 if (ea)
|
|
3169 {
|
|
3170 ea = ea->semantic(sc);
|
|
3171 ea = ea->optimize(WANTvalue | WANTinterpret);
|
|
3172 tiargs->data[j] = ea;
|
|
3173 }
|
|
3174 else if (sa)
|
|
3175 { tiargs->data[j] = sa;
|
|
3176 TupleDeclaration *d = sa->toAlias()->isTupleDeclaration();
|
|
3177 if (d)
|
|
3178 {
|
|
3179 size_t dim = d->objects->dim;
|
|
3180 tiargs->remove(j);
|
|
3181 tiargs->insert(j, d->objects);
|
|
3182 j--;
|
|
3183 }
|
|
3184 }
|
|
3185 else if (ta)
|
|
3186 {
|
|
3187 if (ta->ty == Ttuple)
|
|
3188 { // Expand tuple
|
|
3189 TypeTuple *tt = (TypeTuple *)ta;
|
|
3190 size_t dim = tt->arguments->dim;
|
|
3191 tiargs->remove(j);
|
|
3192 if (dim)
|
|
3193 { tiargs->reserve(dim);
|
|
3194 for (size_t i = 0; i < dim; i++)
|
|
3195 { Argument *arg = (Argument *)tt->arguments->data[i];
|
|
3196 tiargs->insert(j + i, arg->type);
|
|
3197 }
|
|
3198 }
|
|
3199 j--;
|
|
3200 }
|
|
3201 else
|
|
3202 tiargs->data[j] = ta;
|
|
3203 }
|
|
3204 else
|
|
3205 {
|
|
3206 assert(global.errors);
|
|
3207 tiargs->data[j] = Type::terror;
|
|
3208 }
|
|
3209 }
|
|
3210 else if (ea)
|
|
3211 {
|
|
3212 if (!ea)
|
|
3213 { assert(global.errors);
|
|
3214 ea = new IntegerExp(0);
|
|
3215 }
|
|
3216 assert(ea);
|
|
3217 ea = ea->semantic(sc);
|
|
3218 ea = ea->optimize(WANTvalue | WANTinterpret);
|
|
3219 tiargs->data[j] = ea;
|
|
3220 if (ea->op == TOKtype)
|
|
3221 tiargs->data[j] = ea->type;
|
|
3222 }
|
|
3223 else if (sa)
|
|
3224 {
|
|
3225 }
|
|
3226 else
|
|
3227 {
|
|
3228 assert(0);
|
|
3229 }
|
|
3230 //printf("1: tiargs->data[%d] = %p\n", j, tiargs->data[j]);
|
|
3231 }
|
|
3232 #if 0
|
|
3233 printf("-TemplateInstance::semanticTiargs('%s', this=%p)\n", toChars(), this);
|
|
3234 for (size_t j = 0; j < tiargs->dim; j++)
|
|
3235 {
|
|
3236 Object *o = (Object *)tiargs->data[j];
|
|
3237 Type *ta = isType(o);
|
|
3238 Expression *ea = isExpression(o);
|
|
3239 Dsymbol *sa = isDsymbol(o);
|
|
3240 Tuple *va = isTuple(o);
|
|
3241
|
|
3242 printf("\ttiargs[%d] = ta %p, ea %p, sa %p, va %p\n", j, ta, ea, sa, va);
|
|
3243 }
|
|
3244 #endif
|
|
3245 }
|
|
3246
|
|
3247 /**********************************************
|
|
3248 * Find template declaration corresponding to template instance.
|
|
3249 */
|
|
3250
|
|
3251 TemplateDeclaration *TemplateInstance::findTemplateDeclaration(Scope *sc)
|
|
3252 {
|
|
3253 //printf("TemplateInstance::findTemplateDeclaration() %s\n", toChars());
|
|
3254 if (!tempdecl)
|
|
3255 {
|
|
3256 /* Given:
|
|
3257 * foo!( ... )
|
|
3258 * figure out which TemplateDeclaration foo refers to.
|
|
3259 */
|
|
3260 Dsymbol *s;
|
|
3261 Dsymbol *scopesym;
|
|
3262 Identifier *id;
|
|
3263 int i;
|
|
3264
|
|
3265 id = name;
|
|
3266 s = sc->search(loc, id, &scopesym);
|
|
3267 if (!s)
|
|
3268 { error("identifier '%s' is not defined", id->toChars());
|
|
3269 return NULL;
|
|
3270 }
|
|
3271 #if LOG
|
|
3272 printf("It's an instance of '%s' kind '%s'\n", s->toChars(), s->kind());
|
|
3273 if (s->parent)
|
|
3274 printf("s->parent = '%s'\n", s->parent->toChars());
|
|
3275 #endif
|
|
3276 withsym = scopesym->isWithScopeSymbol();
|
|
3277
|
|
3278 /* We might have found an alias within a template when
|
|
3279 * we really want the template.
|
|
3280 */
|
|
3281 TemplateInstance *ti;
|
|
3282 if (s->parent &&
|
|
3283 (ti = s->parent->isTemplateInstance()) != NULL)
|
|
3284 {
|
|
3285 if (
|
|
3286 (ti->name == id ||
|
|
3287 ti->toAlias()->ident == id)
|
|
3288 &&
|
|
3289 ti->tempdecl)
|
|
3290 {
|
|
3291 /* This is so that one can refer to the enclosing
|
|
3292 * template, even if it has the same name as a member
|
|
3293 * of the template, if it has a !(arguments)
|
|
3294 */
|
|
3295 tempdecl = ti->tempdecl;
|
|
3296 if (tempdecl->overroot) // if not start of overloaded list of TemplateDeclaration's
|
|
3297 tempdecl = tempdecl->overroot; // then get the start
|
|
3298 s = tempdecl;
|
|
3299 }
|
|
3300 }
|
|
3301
|
|
3302 s = s->toAlias();
|
|
3303
|
|
3304 /* It should be a TemplateDeclaration, not some other symbol
|
|
3305 */
|
|
3306 tempdecl = s->isTemplateDeclaration();
|
|
3307 if (!tempdecl)
|
|
3308 {
|
|
3309 if (!s->parent && global.errors)
|
|
3310 return NULL;
|
|
3311 if (!s->parent && s->getType())
|
|
3312 { Dsymbol *s2 = s->getType()->toDsymbol(sc);
|
|
3313 if (!s2)
|
|
3314 {
|
|
3315 error("%s is not a template declaration, it is a %s", id->toChars(), s->kind());
|
|
3316 return NULL;
|
|
3317 }
|
|
3318 s = s2;
|
|
3319 }
|
|
3320 #ifdef DEBUG
|
|
3321 //if (!s->parent) printf("s = %s %s\n", s->kind(), s->toChars());
|
|
3322 #endif
|
|
3323 //assert(s->parent);
|
|
3324 TemplateInstance *ti = s->parent ? s->parent->isTemplateInstance() : NULL;
|
|
3325 if (ti &&
|
|
3326 (ti->name == id ||
|
|
3327 ti->toAlias()->ident == id)
|
|
3328 &&
|
|
3329 ti->tempdecl)
|
|
3330 {
|
|
3331 /* This is so that one can refer to the enclosing
|
|
3332 * template, even if it has the same name as a member
|
|
3333 * of the template, if it has a !(arguments)
|
|
3334 */
|
|
3335 tempdecl = ti->tempdecl;
|
|
3336 if (tempdecl->overroot) // if not start of overloaded list of TemplateDeclaration's
|
|
3337 tempdecl = tempdecl->overroot; // then get the start
|
|
3338 }
|
|
3339 else
|
|
3340 {
|
|
3341 error("%s is not a template declaration, it is a %s", id->toChars(), s->kind());
|
|
3342 return NULL;
|
|
3343 }
|
|
3344 }
|
|
3345 }
|
|
3346 else
|
|
3347 assert(tempdecl->isTemplateDeclaration());
|
|
3348 return tempdecl;
|
|
3349 }
|
|
3350
|
|
3351 TemplateDeclaration *TemplateInstance::findBestMatch(Scope *sc)
|
|
3352 {
|
|
3353 /* Since there can be multiple TemplateDeclaration's with the same
|
|
3354 * name, look for the best match.
|
|
3355 */
|
|
3356 TemplateDeclaration *td_ambig = NULL;
|
|
3357 TemplateDeclaration *td_best = NULL;
|
|
3358 MATCH m_best = MATCHnomatch;
|
|
3359 Objects dedtypes;
|
|
3360
|
|
3361 #if LOG
|
|
3362 printf("TemplateInstance::findBestMatch()\n");
|
|
3363 #endif
|
|
3364 for (TemplateDeclaration *td = tempdecl; td; td = td->overnext)
|
|
3365 {
|
|
3366 MATCH m;
|
|
3367
|
|
3368 //if (tiargs->dim) printf("2: tiargs->dim = %d, data[0] = %p\n", tiargs->dim, tiargs->data[0]);
|
|
3369
|
|
3370 // If more arguments than parameters,
|
|
3371 // then this is no match.
|
|
3372 if (td->parameters->dim < tiargs->dim)
|
|
3373 {
|
|
3374 if (!td->isVariadic())
|
|
3375 continue;
|
|
3376 }
|
|
3377
|
|
3378 dedtypes.setDim(td->parameters->dim);
|
|
3379 dedtypes.zero();
|
|
3380 if (!td->scope)
|
|
3381 {
|
|
3382 error("forward reference to template declaration %s", td->toChars());
|
|
3383 return NULL;
|
|
3384 }
|
|
3385 m = td->matchWithInstance(this, &dedtypes, 0);
|
|
3386 //printf("m = %d\n", m);
|
|
3387 if (!m) // no match at all
|
|
3388 continue;
|
|
3389
|
|
3390 #if 1
|
|
3391 if (m < m_best)
|
|
3392 goto Ltd_best;
|
|
3393 if (m > m_best)
|
|
3394 goto Ltd;
|
|
3395 #else
|
|
3396 if (!m_best)
|
|
3397 goto Ltd;
|
|
3398 #endif
|
|
3399 {
|
|
3400 // Disambiguate by picking the most specialized TemplateDeclaration
|
|
3401 int c1 = td->leastAsSpecialized(td_best);
|
|
3402 int c2 = td_best->leastAsSpecialized(td);
|
|
3403 //printf("c1 = %d, c2 = %d\n", c1, c2);
|
|
3404
|
|
3405 if (c1 > c2)
|
|
3406 goto Ltd;
|
|
3407 else if (c1 < c2)
|
|
3408 goto Ltd_best;
|
|
3409 else
|
|
3410 goto Lambig;
|
|
3411 }
|
|
3412
|
|
3413 Lambig: // td_best and td are ambiguous
|
|
3414 td_ambig = td;
|
|
3415 continue;
|
|
3416
|
|
3417 Ltd_best: // td_best is the best match so far
|
|
3418 td_ambig = NULL;
|
|
3419 continue;
|
|
3420
|
|
3421 Ltd: // td is the new best match
|
|
3422 td_ambig = NULL;
|
|
3423 td_best = td;
|
|
3424 m_best = m;
|
|
3425 tdtypes.setDim(dedtypes.dim);
|
|
3426 memcpy(tdtypes.data, dedtypes.data, tdtypes.dim * sizeof(void *));
|
|
3427 continue;
|
|
3428 }
|
|
3429
|
|
3430 if (!td_best)
|
|
3431 {
|
|
3432 error("%s does not match any template declaration", toChars());
|
|
3433 return NULL;
|
|
3434 }
|
|
3435 if (td_ambig)
|
|
3436 {
|
|
3437 error("%s matches more than one template declaration, %s and %s",
|
|
3438 toChars(), td_best->toChars(), td_ambig->toChars());
|
|
3439 }
|
|
3440
|
|
3441 /* The best match is td_best
|
|
3442 */
|
|
3443 tempdecl = td_best;
|
|
3444
|
|
3445 #if 0
|
|
3446 /* Cast any value arguments to be same type as value parameter
|
|
3447 */
|
|
3448 for (size_t i = 0; i < tiargs->dim; i++)
|
|
3449 { Object *o = (Object *)tiargs->data[i];
|
|
3450 Expression *ea = isExpression(o); // value argument
|
|
3451 TemplateParameter *tp = (TemplateParameter *)tempdecl->parameters->data[i];
|
|
3452 assert(tp);
|
|
3453 TemplateValueParameter *tvp = tp->isTemplateValueParameter();
|
|
3454 if (tvp)
|
|
3455 {
|
|
3456 assert(ea);
|
|
3457 ea = ea->castTo(tvp->valType);
|
|
3458 ea = ea->optimize(WANTvalue | WANTinterpret);
|
|
3459 tiargs->data[i] = (Object *)ea;
|
|
3460 }
|
|
3461 }
|
|
3462 #endif
|
|
3463
|
|
3464 #if LOG
|
|
3465 printf("\tIt's a match with template declaration '%s'\n", tempdecl->toChars());
|
|
3466 #endif
|
|
3467 return tempdecl;
|
|
3468 }
|
|
3469
|
|
3470
|
|
3471 /*****************************************
|
|
3472 * Determines if a TemplateInstance will need a nested
|
|
3473 * generation of the TemplateDeclaration.
|
|
3474 */
|
|
3475
|
|
3476 int TemplateInstance::isNested(Objects *args)
|
|
3477 { int nested = 0;
|
|
3478 //printf("TemplateInstance::isNested('%s')\n", tempdecl->ident->toChars());
|
|
3479
|
|
3480 /* A nested instance happens when an argument references a local
|
|
3481 * symbol that is on the stack.
|
|
3482 */
|
|
3483 for (size_t i = 0; i < args->dim; i++)
|
|
3484 { Object *o = (Object *)args->data[i];
|
|
3485 Expression *ea = isExpression(o);
|
|
3486 Dsymbol *sa = isDsymbol(o);
|
|
3487 Tuple *va = isTuple(o);
|
|
3488 if (ea)
|
|
3489 {
|
|
3490 if (ea->op == TOKvar)
|
|
3491 {
|
|
3492 sa = ((VarExp *)ea)->var;
|
|
3493 goto Lsa;
|
|
3494 }
|
|
3495 if (ea->op == TOKfunction)
|
|
3496 {
|
|
3497 sa = ((FuncExp *)ea)->fd;
|
|
3498 goto Lsa;
|
|
3499 }
|
|
3500 }
|
|
3501 else if (sa)
|
|
3502 {
|
|
3503 Lsa:
|
|
3504 Declaration *d = sa->isDeclaration();
|
|
3505 if (d && !d->isDataseg() &&
|
|
3506 #if DMDV2
|
|
3507 !(d->storage_class & STCmanifest) &&
|
|
3508 #endif
|
|
3509 (!d->isFuncDeclaration() || d->isFuncDeclaration()->isNested()) &&
|
|
3510 !isTemplateMixin())
|
|
3511 {
|
|
3512 // if module level template
|
|
3513 if (tempdecl->toParent()->isModule())
|
|
3514 { Dsymbol *dparent = d->toParent();
|
|
3515 if (!isnested)
|
|
3516 isnested = dparent;
|
|
3517 else if (isnested != dparent)
|
|
3518 {
|
|
3519 /* Select the more deeply nested of the two.
|
|
3520 * Error if one is not nested inside the other.
|
|
3521 */
|
|
3522 for (Dsymbol *p = isnested; p; p = p->parent)
|
|
3523 {
|
|
3524 if (p == dparent)
|
|
3525 goto L1; // isnested is most nested
|
|
3526 }
|
|
3527 for (Dsymbol *p = dparent; 1; p = p->parent)
|
|
3528 {
|
|
3529 if (p == isnested)
|
|
3530 { isnested = dparent;
|
|
3531 goto L1; // dparent is most nested
|
|
3532 }
|
|
3533 }
|
|
3534 error("is nested in both %s and %s", isnested->toChars(), dparent->toChars());
|
|
3535 }
|
|
3536 L1:
|
|
3537 //printf("\tnested inside %s\n", isnested->toChars());
|
|
3538 nested |= 1;
|
|
3539 }
|
|
3540 else
|
|
3541 error("cannot use local '%s' as template parameter", d->toChars());
|
|
3542 }
|
|
3543 }
|
|
3544 else if (va)
|
|
3545 {
|
|
3546 nested |= isNested(&va->objects);
|
|
3547 }
|
|
3548 }
|
|
3549 return nested;
|
|
3550 }
|
|
3551
|
|
3552 /****************************************
|
|
3553 * This instance needs an identifier for name mangling purposes.
|
|
3554 * Create one by taking the template declaration name and adding
|
|
3555 * the type signature for it.
|
|
3556 */
|
|
3557
|
|
3558 Identifier *TemplateInstance::genIdent()
|
|
3559 { OutBuffer buf;
|
|
3560 char *id;
|
|
3561 Objects *args;
|
|
3562
|
|
3563 //printf("TemplateInstance::genIdent('%s')\n", tempdecl->ident->toChars());
|
|
3564 id = tempdecl->ident->toChars();
|
|
3565 buf.printf("__T%"PRIuSIZE"%s", strlen(id), id);
|
|
3566 args = tiargs;
|
|
3567 for (int i = 0; i < args->dim; i++)
|
|
3568 { Object *o = (Object *)args->data[i];
|
|
3569 Type *ta = isType(o);
|
|
3570 Expression *ea = isExpression(o);
|
|
3571 Dsymbol *sa = isDsymbol(o);
|
|
3572 Tuple *va = isTuple(o);
|
|
3573 //printf("\to %p ta %p ea %p sa %p va %p\n", o, ta, ea, sa, va);
|
|
3574 if (ta)
|
|
3575 {
|
|
3576 buf.writeByte('T');
|
|
3577 if (ta->deco)
|
|
3578 buf.writestring(ta->deco);
|
|
3579 else
|
|
3580 {
|
|
3581 #ifdef DEBUG
|
|
3582 printf("ta = %d, %s\n", ta->ty, ta->toChars());
|
|
3583 #endif
|
|
3584 assert(global.errors);
|
|
3585 }
|
|
3586 }
|
|
3587 else if (ea)
|
|
3588 { sinteger_t v;
|
|
3589 real_t r;
|
|
3590
|
|
3591 if (ea->op == TOKvar)
|
|
3592 {
|
|
3593 sa = ((VarExp *)ea)->var;
|
|
3594 ea = NULL;
|
|
3595 goto Lsa;
|
|
3596 }
|
|
3597 if (ea->op == TOKfunction)
|
|
3598 {
|
|
3599 sa = ((FuncExp *)ea)->fd;
|
|
3600 ea = NULL;
|
|
3601 goto Lsa;
|
|
3602 }
|
|
3603 buf.writeByte('V');
|
|
3604 if (ea->op == TOKtuple)
|
|
3605 { ea->error("tuple is not a valid template value argument");
|
|
3606 continue;
|
|
3607 }
|
|
3608 #if 1
|
|
3609 /* Use deco that matches what it would be for a function parameter
|
|
3610 */
|
|
3611 buf.writestring(ea->type->deco);
|
|
3612 #else
|
|
3613 // Use type of parameter, not type of argument
|
|
3614 TemplateParameter *tp = (TemplateParameter *)tempdecl->parameters->data[i];
|
|
3615 assert(tp);
|
|
3616 TemplateValueParameter *tvp = tp->isTemplateValueParameter();
|
|
3617 assert(tvp);
|
|
3618 buf.writestring(tvp->valType->deco);
|
|
3619 #endif
|
|
3620 ea->toMangleBuffer(&buf);
|
|
3621 }
|
|
3622 else if (sa)
|
|
3623 {
|
|
3624 Lsa:
|
|
3625 buf.writeByte('S');
|
|
3626 Declaration *d = sa->isDeclaration();
|
|
3627 if (d && !d->type->deco)
|
|
3628 error("forward reference of %s", d->toChars());
|
|
3629 else
|
|
3630 {
|
|
3631 char *p = sa->mangle();
|
|
3632 buf.printf("%"PRIuSIZE"%s", strlen(p), p);
|
|
3633 }
|
|
3634 }
|
|
3635 else if (va)
|
|
3636 {
|
|
3637 assert(i + 1 == args->dim); // must be last one
|
|
3638 args = &va->objects;
|
|
3639 i = -1;
|
|
3640 }
|
|
3641 else
|
|
3642 assert(0);
|
|
3643 }
|
|
3644 buf.writeByte('Z');
|
|
3645 id = buf.toChars();
|
|
3646 buf.data = NULL;
|
|
3647 return new Identifier(id, TOKidentifier);
|
|
3648 }
|
|
3649
|
|
3650
|
|
3651 /****************************************************
|
|
3652 * Declare parameters of template instance, initialize them with the
|
|
3653 * template instance arguments.
|
|
3654 */
|
|
3655
|
|
3656 void TemplateInstance::declareParameters(Scope *scope)
|
|
3657 {
|
|
3658 //printf("TemplateInstance::declareParameters()\n");
|
|
3659 for (int i = 0; i < tdtypes.dim; i++)
|
|
3660 {
|
|
3661 TemplateParameter *tp = (TemplateParameter *)tempdecl->parameters->data[i];
|
|
3662 //Object *o = (Object *)tiargs->data[i];
|
|
3663 Object *o = (Object *)tdtypes.data[i];
|
|
3664
|
|
3665 //printf("\ttdtypes[%d] = %p\n", i, o);
|
|
3666 tempdecl->declareParameter(scope, tp, o);
|
|
3667 }
|
|
3668 }
|
|
3669
|
|
3670
|
|
3671 void TemplateInstance::semantic2(Scope *sc)
|
|
3672 { int i;
|
|
3673
|
|
3674 if (semanticdone >= 2)
|
|
3675 return;
|
|
3676 semanticdone = 2;
|
|
3677 #if LOG
|
|
3678 printf("+TemplateInstance::semantic2('%s')\n", toChars());
|
|
3679 #endif
|
|
3680 if (!errors && members)
|
|
3681 {
|
|
3682 sc = tempdecl->scope;
|
|
3683 assert(sc);
|
|
3684 sc = sc->push(argsym);
|
|
3685 sc = sc->push(this);
|
|
3686 for (i = 0; i < members->dim; i++)
|
|
3687 {
|
|
3688 Dsymbol *s = (Dsymbol *)members->data[i];
|
|
3689 #if LOG
|
|
3690 printf("\tmember '%s', kind = '%s'\n", s->toChars(), s->kind());
|
|
3691 #endif
|
|
3692 s->semantic2(sc);
|
|
3693 }
|
|
3694 sc = sc->pop();
|
|
3695 sc->pop();
|
|
3696 }
|
|
3697 #if LOG
|
|
3698 printf("-TemplateInstance::semantic2('%s')\n", toChars());
|
|
3699 #endif
|
|
3700 }
|
|
3701
|
|
3702 void TemplateInstance::semantic3(Scope *sc)
|
|
3703 {
|
|
3704 #if LOG
|
|
3705 printf("TemplateInstance::semantic3('%s'), semanticdone = %d\n", toChars(), semanticdone);
|
|
3706 #endif
|
|
3707 //if (toChars()[0] == 'D') *(char*)0=0;
|
|
3708 if (semanticdone >= 3)
|
|
3709 return;
|
|
3710 semanticdone = 3;
|
|
3711 if (!errors && members)
|
|
3712 {
|
|
3713 sc = tempdecl->scope;
|
|
3714 sc = sc->push(argsym);
|
|
3715 sc = sc->push(this);
|
|
3716 for (int i = 0; i < members->dim; i++)
|
|
3717 {
|
|
3718 Dsymbol *s = (Dsymbol *)members->data[i];
|
|
3719 s->semantic3(sc);
|
|
3720 }
|
|
3721 sc = sc->pop();
|
|
3722 sc->pop();
|
|
3723 }
|
|
3724 }
|
|
3725
|
|
3726 void TemplateInstance::toObjFile(int multiobj)
|
|
3727 {
|
|
3728 #if LOG
|
|
3729 printf("TemplateInstance::toObjFile('%s', this = %p)\n", toChars(), this);
|
|
3730 #endif
|
|
3731 if (!errors && members)
|
|
3732 {
|
|
3733 if (multiobj)
|
|
3734 // Append to list of object files to be written later
|
|
3735 //obj_append(this);
|
|
3736 assert(0 && "multiobj");
|
|
3737 else
|
|
3738 {
|
|
3739 for (int i = 0; i < members->dim; i++)
|
|
3740 {
|
|
3741 Dsymbol *s = (Dsymbol *)members->data[i];
|
|
3742 s->toObjFile(multiobj);
|
|
3743 }
|
|
3744 }
|
|
3745 }
|
|
3746 }
|
|
3747
|
|
3748 void TemplateInstance::inlineScan()
|
|
3749 {
|
|
3750 #if LOG
|
|
3751 printf("TemplateInstance::inlineScan('%s')\n", toChars());
|
|
3752 #endif
|
|
3753 if (!errors && members)
|
|
3754 {
|
|
3755 for (int i = 0; i < members->dim; i++)
|
|
3756 {
|
|
3757 Dsymbol *s = (Dsymbol *)members->data[i];
|
|
3758 s->inlineScan();
|
|
3759 }
|
|
3760 }
|
|
3761 }
|
|
3762
|
|
3763 void TemplateInstance::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
|
|
3764 {
|
|
3765 int i;
|
|
3766
|
|
3767 Identifier *id = name;
|
|
3768 buf->writestring(id->toChars());
|
|
3769 buf->writestring("!(");
|
|
3770 if (nest)
|
|
3771 buf->writestring("...");
|
|
3772 else
|
|
3773 {
|
|
3774 nest++;
|
|
3775 Objects *args = tiargs;
|
|
3776 for (i = 0; i < args->dim; i++)
|
|
3777 {
|
|
3778 if (i)
|
|
3779 buf->writeByte(',');
|
|
3780 Object *oarg = (Object *)args->data[i];
|
|
3781 ObjectToCBuffer(buf, hgs, oarg);
|
|
3782 }
|
|
3783 nest--;
|
|
3784 }
|
|
3785 buf->writeByte(')');
|
|
3786 }
|
|
3787
|
|
3788
|
|
3789 Dsymbol *TemplateInstance::toAlias()
|
|
3790 {
|
|
3791 #if LOG
|
|
3792 printf("TemplateInstance::toAlias()\n");
|
|
3793 #endif
|
|
3794 if (!inst)
|
|
3795 { error("cannot resolve forward reference");
|
|
3796 return this;
|
|
3797 }
|
|
3798
|
|
3799 if (inst != this)
|
|
3800 return inst->toAlias();
|
|
3801
|
|
3802 if (aliasdecl)
|
|
3803 return aliasdecl->toAlias();
|
|
3804
|
|
3805 return inst;
|
|
3806 }
|
|
3807
|
|
3808 AliasDeclaration *TemplateInstance::isAliasDeclaration()
|
|
3809 {
|
|
3810 return aliasdecl;
|
|
3811 }
|
|
3812
|
|
3813 const char *TemplateInstance::kind()
|
|
3814 {
|
|
3815 return "template instance";
|
|
3816 }
|
|
3817
|
|
3818 int TemplateInstance::oneMember(Dsymbol **ps)
|
|
3819 {
|
|
3820 *ps = NULL;
|
|
3821 return TRUE;
|
|
3822 }
|
|
3823
|
|
3824 char *TemplateInstance::toChars()
|
|
3825 {
|
|
3826 OutBuffer buf;
|
|
3827 HdrGenState hgs;
|
|
3828 char *s;
|
|
3829
|
|
3830 toCBuffer(&buf, &hgs);
|
|
3831 s = buf.toChars();
|
|
3832 buf.data = NULL;
|
|
3833 return s;
|
|
3834 }
|
|
3835
|
|
3836 /* ======================== TemplateMixin ================================ */
|
|
3837
|
|
3838 TemplateMixin::TemplateMixin(Loc loc, Identifier *ident, Type *tqual,
|
|
3839 Array *idents, Objects *tiargs)
|
|
3840 : TemplateInstance(loc, (Identifier *)idents->data[idents->dim - 1])
|
|
3841 {
|
|
3842 //printf("TemplateMixin(ident = '%s')\n", ident ? ident->toChars() : "");
|
|
3843 this->ident = ident;
|
|
3844 this->tqual = tqual;
|
|
3845 this->idents = idents;
|
|
3846 this->tiargs = tiargs ? tiargs : new Objects();
|
|
3847 this->scope = NULL;
|
|
3848 }
|
|
3849
|
|
3850 Dsymbol *TemplateMixin::syntaxCopy(Dsymbol *s)
|
|
3851 { TemplateMixin *tm;
|
|
3852
|
|
3853 Array *ids = new Array();
|
|
3854 ids->setDim(idents->dim);
|
|
3855 for (int i = 0; i < idents->dim; i++)
|
|
3856 { // Matches TypeQualified::syntaxCopyHelper()
|
|
3857 Identifier *id = (Identifier *)idents->data[i];
|
|
3858 if (id->dyncast() == DYNCAST_DSYMBOL)
|
|
3859 {
|
|
3860 TemplateInstance *ti = (TemplateInstance *)id;
|
|
3861
|
|
3862 ti = (TemplateInstance *)ti->syntaxCopy(NULL);
|
|
3863 id = (Identifier *)ti;
|
|
3864 }
|
|
3865 ids->data[i] = id;
|
|
3866 }
|
|
3867
|
|
3868 tm = new TemplateMixin(loc, ident,
|
|
3869 (Type *)(tqual ? tqual->syntaxCopy() : NULL),
|
|
3870 ids, tiargs);
|
|
3871 TemplateInstance::syntaxCopy(tm);
|
|
3872 return tm;
|
|
3873 }
|
|
3874
|
|
3875 void TemplateMixin::semantic(Scope *sc)
|
|
3876 {
|
|
3877 #if LOG
|
|
3878 printf("+TemplateMixin::semantic('%s', this=%p)\n", toChars(), this);
|
|
3879 fflush(stdout);
|
|
3880 #endif
|
|
3881 if (semanticdone &&
|
|
3882 // This for when a class/struct contains mixin members, and
|
|
3883 // is done over because of forward references
|
|
3884 (!parent || !toParent()->isAggregateDeclaration()))
|
|
3885 {
|
|
3886 #if LOG
|
|
3887 printf("\tsemantic done\n");
|
|
3888 #endif
|
|
3889 return;
|
|
3890 }
|
|
3891 if (!semanticdone)
|
|
3892 semanticdone = 1;
|
|
3893 #if LOG
|
|
3894 printf("\tdo semantic\n");
|
|
3895 #endif
|
|
3896
|
|
3897 #if !IN_LLVM
|
|
3898 // dont know what this is
|
|
3899 util_progress();
|
|
3900 #endif
|
|
3901
|
|
3902 Scope *scx = NULL;
|
|
3903 if (scope)
|
|
3904 { sc = scope;
|
|
3905 scx = scope; // save so we don't make redundant copies
|
|
3906 scope = NULL;
|
|
3907 }
|
|
3908
|
|
3909 // Follow qualifications to find the TemplateDeclaration
|
|
3910 if (!tempdecl)
|
|
3911 { Dsymbol *s;
|
|
3912 int i;
|
|
3913 Identifier *id;
|
|
3914
|
|
3915 if (tqual)
|
|
3916 { s = tqual->toDsymbol(sc);
|
|
3917 i = 0;
|
|
3918 }
|
|
3919 else
|
|
3920 {
|
|
3921 i = 1;
|
|
3922 id = (Identifier *)idents->data[0];
|
|
3923 switch (id->dyncast())
|
|
3924 {
|
|
3925 case DYNCAST_IDENTIFIER:
|
|
3926 s = sc->search(loc, id, NULL);
|
|
3927 break;
|
|
3928
|
|
3929 case DYNCAST_DSYMBOL:
|
|
3930 {
|
|
3931 TemplateInstance *ti = (TemplateInstance *)id;
|
|
3932 ti->semantic(sc);
|
|
3933 s = ti;
|
|
3934 break;
|
|
3935 }
|
|
3936 default:
|
|
3937 assert(0);
|
|
3938 }
|
|
3939 }
|
|
3940
|
|
3941 for (; i < idents->dim; i++)
|
|
3942 {
|
|
3943 if (!s)
|
|
3944 break;
|
|
3945 id = (Identifier *)idents->data[i];
|
|
3946 s = s->searchX(loc, sc, id);
|
|
3947 }
|
|
3948 if (!s)
|
|
3949 {
|
|
3950 error("is not defined");
|
|
3951 inst = this;
|
|
3952 return;
|
|
3953 }
|
|
3954 tempdecl = s->toAlias()->isTemplateDeclaration();
|
|
3955 if (!tempdecl)
|
|
3956 {
|
|
3957 error("%s isn't a template", s->toChars());
|
|
3958 inst = this;
|
|
3959 return;
|
|
3960 }
|
|
3961 }
|
|
3962
|
|
3963 // Look for forward reference
|
|
3964 assert(tempdecl);
|
|
3965 for (TemplateDeclaration *td = tempdecl; td; td = td->overnext)
|
|
3966 {
|
|
3967 if (!td->scope)
|
|
3968 {
|
|
3969 /* Cannot handle forward references if mixin is a struct member,
|
|
3970 * because addField must happen during struct's semantic, not
|
|
3971 * during the mixin semantic.
|
|
3972 * runDeferred will re-run mixin's semantic outside of the struct's
|
|
3973 * semantic.
|
|
3974 */
|
|
3975 semanticdone = 0;
|
|
3976 AggregateDeclaration *ad = toParent()->isAggregateDeclaration();
|
|
3977 if (ad)
|
|
3978 ad->sizeok = 2;
|
|
3979 else
|
|
3980 {
|
|
3981 // Forward reference
|
|
3982 //printf("forward reference - deferring\n");
|
|
3983 scope = scx ? scx : new Scope(*sc);
|
|
3984 scope->setNoFree();
|
|
3985 scope->module->addDeferredSemantic(this);
|
|
3986 }
|
|
3987 return;
|
|
3988 }
|
|
3989 }
|
|
3990
|
|
3991 // Run semantic on each argument, place results in tiargs[]
|
|
3992 semanticTiargs(sc);
|
|
3993
|
|
3994 tempdecl = findBestMatch(sc);
|
|
3995 if (!tempdecl)
|
|
3996 { inst = this;
|
|
3997 return; // error recovery
|
|
3998 }
|
|
3999
|
|
4000 if (!ident)
|
|
4001 ident = genIdent();
|
|
4002
|
|
4003 inst = this;
|
|
4004 parent = sc->parent;
|
|
4005
|
|
4006 /* Detect recursive mixin instantiations.
|
|
4007 */
|
|
4008 for (Dsymbol *s = parent; s; s = s->parent)
|
|
4009 {
|
|
4010 //printf("\ts = '%s'\n", s->toChars());
|
|
4011 TemplateMixin *tm = s->isTemplateMixin();
|
|
4012 if (!tm || tempdecl != tm->tempdecl)
|
|
4013 continue;
|
|
4014
|
|
4015 /* Different argument list lengths happen with variadic args
|
|
4016 */
|
|
4017 if (tiargs->dim != tm->tiargs->dim)
|
|
4018 continue;
|
|
4019
|
|
4020 for (int i = 0; i < tiargs->dim; i++)
|
|
4021 { Object *o = (Object *)tiargs->data[i];
|
|
4022 Type *ta = isType(o);
|
|
4023 Expression *ea = isExpression(o);
|
|
4024 Dsymbol *sa = isDsymbol(o);
|
|
4025 Object *tmo = (Object *)tm->tiargs->data[i];
|
|
4026 if (ta)
|
|
4027 {
|
|
4028 Type *tmta = isType(tmo);
|
|
4029 if (!tmta)
|
|
4030 goto Lcontinue;
|
|
4031 if (!ta->equals(tmta))
|
|
4032 goto Lcontinue;
|
|
4033 }
|
|
4034 else if (ea)
|
|
4035 { Expression *tme = isExpression(tmo);
|
|
4036 if (!tme || !ea->equals(tme))
|
|
4037 goto Lcontinue;
|
|
4038 }
|
|
4039 else if (sa)
|
|
4040 {
|
|
4041 Dsymbol *tmsa = isDsymbol(tmo);
|
|
4042 if (sa != tmsa)
|
|
4043 goto Lcontinue;
|
|
4044 }
|
|
4045 else
|
|
4046 assert(0);
|
|
4047 }
|
|
4048 error("recursive mixin instantiation");
|
|
4049 return;
|
|
4050
|
|
4051 Lcontinue:
|
|
4052 continue;
|
|
4053 }
|
|
4054
|
|
4055 // Copy the syntax trees from the TemplateDeclaration
|
|
4056 members = Dsymbol::arraySyntaxCopy(tempdecl->members);
|
|
4057 if (!members)
|
|
4058 return;
|
|
4059
|
|
4060 symtab = new DsymbolTable();
|
|
4061
|
|
4062 for (Scope *sce = sc; 1; sce = sce->enclosing)
|
|
4063 {
|
|
4064 ScopeDsymbol *sds = (ScopeDsymbol *)sce->scopesym;
|
|
4065 if (sds)
|
|
4066 {
|
|
4067 sds->importScope(this, PROTpublic);
|
|
4068 break;
|
|
4069 }
|
|
4070 }
|
|
4071
|
|
4072 #if LOG
|
|
4073 printf("\tcreate scope for template parameters '%s'\n", toChars());
|
|
4074 #endif
|
|
4075 Scope *scy = sc;
|
|
4076 scy = sc->push(this);
|
|
4077 scy->parent = this;
|
|
4078
|
|
4079 argsym = new ScopeDsymbol();
|
|
4080 argsym->parent = scy->parent;
|
|
4081 Scope *scope = scy->push(argsym);
|
|
4082
|
|
4083 unsigned errorsave = global.errors;
|
|
4084
|
|
4085 // Declare each template parameter as an alias for the argument type
|
|
4086 declareParameters(scope);
|
|
4087
|
|
4088 // Add members to enclosing scope, as well as this scope
|
|
4089 for (unsigned i = 0; i < members->dim; i++)
|
|
4090 { Dsymbol *s;
|
|
4091
|
|
4092 s = (Dsymbol *)members->data[i];
|
|
4093 s->addMember(scope, this, i);
|
|
4094 //sc->insert(s);
|
|
4095 //printf("sc->parent = %p, sc->scopesym = %p\n", sc->parent, sc->scopesym);
|
|
4096 //printf("s->parent = %s\n", s->parent->toChars());
|
|
4097 }
|
|
4098
|
|
4099 // Do semantic() analysis on template instance members
|
|
4100 #if LOG
|
|
4101 printf("\tdo semantic() on template instance members '%s'\n", toChars());
|
|
4102 #endif
|
|
4103 Scope *sc2;
|
|
4104 sc2 = scope->push(this);
|
|
4105 sc2->offset = sc->offset;
|
|
4106 for (int i = 0; i < members->dim; i++)
|
|
4107 {
|
|
4108 Dsymbol *s = (Dsymbol *)members->data[i];
|
|
4109 s->semantic(sc2);
|
|
4110 }
|
|
4111 sc->offset = sc2->offset;
|
|
4112
|
|
4113 /* The problem is when to parse the initializer for a variable.
|
|
4114 * Perhaps VarDeclaration::semantic() should do it like it does
|
|
4115 * for initializers inside a function.
|
|
4116 */
|
|
4117 // if (sc->parent->isFuncDeclaration())
|
|
4118
|
|
4119 semantic2(sc2);
|
|
4120
|
|
4121 if (sc->func)
|
|
4122 {
|
|
4123 semantic3(sc2);
|
|
4124 }
|
|
4125
|
|
4126 // Give additional context info if error occurred during instantiation
|
|
4127 if (global.errors != errorsave)
|
|
4128 {
|
|
4129 error("error instantiating");
|
|
4130 }
|
|
4131
|
|
4132 sc2->pop();
|
|
4133
|
|
4134 scope->pop();
|
|
4135
|
|
4136 // if (!isAnonymous())
|
|
4137 {
|
|
4138 scy->pop();
|
|
4139 }
|
|
4140 #if LOG
|
|
4141 printf("-TemplateMixin::semantic('%s', this=%p)\n", toChars(), this);
|
|
4142 #endif
|
|
4143 }
|
|
4144
|
|
4145 void TemplateMixin::semantic2(Scope *sc)
|
|
4146 { int i;
|
|
4147
|
|
4148 if (semanticdone >= 2)
|
|
4149 return;
|
|
4150 semanticdone = 2;
|
|
4151 #if LOG
|
|
4152 printf("+TemplateMixin::semantic2('%s')\n", toChars());
|
|
4153 #endif
|
|
4154 if (members)
|
|
4155 {
|
|
4156 assert(sc);
|
|
4157 sc = sc->push(argsym);
|
|
4158 sc = sc->push(this);
|
|
4159 for (i = 0; i < members->dim; i++)
|
|
4160 {
|
|
4161 Dsymbol *s = (Dsymbol *)members->data[i];
|
|
4162 #if LOG
|
|
4163 printf("\tmember '%s', kind = '%s'\n", s->toChars(), s->kind());
|
|
4164 #endif
|
|
4165 s->semantic2(sc);
|
|
4166 }
|
|
4167 sc = sc->pop();
|
|
4168 sc->pop();
|
|
4169 }
|
|
4170 #if LOG
|
|
4171 printf("-TemplateMixin::semantic2('%s')\n", toChars());
|
|
4172 #endif
|
|
4173 }
|
|
4174
|
|
4175 void TemplateMixin::semantic3(Scope *sc)
|
|
4176 { int i;
|
|
4177
|
|
4178 if (semanticdone >= 3)
|
|
4179 return;
|
|
4180 semanticdone = 3;
|
|
4181 #if LOG
|
|
4182 printf("TemplateMixin::semantic3('%s')\n", toChars());
|
|
4183 #endif
|
|
4184 if (members)
|
|
4185 {
|
|
4186 sc = sc->push(argsym);
|
|
4187 sc = sc->push(this);
|
|
4188 for (i = 0; i < members->dim; i++)
|
|
4189 {
|
|
4190 Dsymbol *s = (Dsymbol *)members->data[i];
|
|
4191 s->semantic3(sc);
|
|
4192 }
|
|
4193 sc = sc->pop();
|
|
4194 sc->pop();
|
|
4195 }
|
|
4196 }
|
|
4197
|
|
4198 void TemplateMixin::inlineScan()
|
|
4199 {
|
|
4200 TemplateInstance::inlineScan();
|
|
4201 }
|
|
4202
|
|
4203 const char *TemplateMixin::kind()
|
|
4204 {
|
|
4205 return "mixin";
|
|
4206 }
|
|
4207
|
|
4208 int TemplateMixin::oneMember(Dsymbol **ps)
|
|
4209 {
|
|
4210 return Dsymbol::oneMember(ps);
|
|
4211 }
|
|
4212
|
|
4213 int TemplateMixin::hasPointers()
|
|
4214 {
|
|
4215 //printf("TemplateMixin::hasPointers() %s\n", toChars());
|
|
4216 for (size_t i = 0; i < members->dim; i++)
|
|
4217 {
|
|
4218 Dsymbol *s = (Dsymbol *)members->data[i];
|
|
4219 //printf(" s = %s %s\n", s->kind(), s->toChars());
|
|
4220 if (s->hasPointers())
|
|
4221 {
|
|
4222 return 1;
|
|
4223 }
|
|
4224 }
|
|
4225 return 0;
|
|
4226 }
|
|
4227
|
|
4228 char *TemplateMixin::toChars()
|
|
4229 {
|
|
4230 OutBuffer buf;
|
|
4231 HdrGenState hgs;
|
|
4232 char *s;
|
|
4233
|
|
4234 TemplateInstance::toCBuffer(&buf, &hgs);
|
|
4235 s = buf.toChars();
|
|
4236 buf.data = NULL;
|
|
4237 return s;
|
|
4238 }
|
|
4239
|
|
4240 void TemplateMixin::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
|
|
4241 {
|
|
4242 buf->writestring("mixin ");
|
|
4243
|
|
4244 for (int i = 0; i < idents->dim; i++)
|
|
4245 { Identifier *id = (Identifier *)idents->data[i];
|
|
4246
|
|
4247 if (i)
|
|
4248 buf->writeByte('.');
|
|
4249 buf->writestring(id->toChars());
|
|
4250 }
|
|
4251 buf->writestring("!(");
|
|
4252 if (tiargs)
|
|
4253 {
|
|
4254 for (int i = 0; i < tiargs->dim; i++)
|
|
4255 { if (i)
|
|
4256 buf->writebyte(',');
|
|
4257 Object *oarg = (Object *)tiargs->data[i];
|
|
4258 Type *t = isType(oarg);
|
|
4259 Expression *e = isExpression(oarg);
|
|
4260 Dsymbol *s = isDsymbol(oarg);
|
|
4261 if (t)
|
|
4262 t->toCBuffer(buf, NULL, hgs);
|
|
4263 else if (e)
|
|
4264 e->toCBuffer(buf, hgs);
|
|
4265 else if (s)
|
|
4266 {
|
|
4267 char *p = s->ident ? s->ident->toChars() : s->toChars();
|
|
4268 buf->writestring(p);
|
|
4269 }
|
|
4270 else if (!oarg)
|
|
4271 {
|
|
4272 buf->writestring("NULL");
|
|
4273 }
|
|
4274 else
|
|
4275 {
|
|
4276 assert(0);
|
|
4277 }
|
|
4278 }
|
|
4279 }
|
|
4280 buf->writebyte(')');
|
|
4281 if (ident)
|
|
4282 {
|
|
4283 buf->writebyte(' ');
|
|
4284 buf->writestring(ident->toChars());
|
|
4285 }
|
|
4286 buf->writebyte(';');
|
|
4287 buf->writenl();
|
|
4288 }
|
|
4289
|
|
4290
|
|
4291 void TemplateMixin::toObjFile(int multiobj)
|
|
4292 {
|
|
4293 //printf("TemplateMixin::toObjFile('%s')\n", toChars());
|
|
4294 TemplateInstance::toObjFile(multiobj);
|
|
4295 }
|
|
4296
|