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