]>
Commit | Line | Data |
---|---|---|
b4c522fa IB |
1 | |
2 | /* Compiler implementation of the D programming language | |
3 | * Copyright (C) 1999-2018 by The D Language Foundation, All Rights Reserved | |
4 | * written by Walter Bright | |
5 | * http://www.digitalmars.com | |
6 | * Distributed under the Boost Software License, Version 1.0. | |
7 | * http://www.boost.org/LICENSE_1_0.txt | |
8 | * https://github.com/D-Programming-Language/dmd/blob/master/src/template.c | |
9 | */ | |
10 | ||
11 | // Handle template implementation | |
12 | ||
13 | #include <stdio.h> | |
14 | #include <assert.h> | |
15 | ||
16 | #include "root/root.h" | |
17 | #include "root/aav.h" | |
18 | #include "root/rmem.h" | |
19 | #include "root/stringtable.h" | |
20 | #include "root/hash.h" | |
21 | ||
22 | #include "mangle.h" | |
23 | #include "mtype.h" | |
24 | #include "template.h" | |
25 | #include "init.h" | |
26 | #include "expression.h" | |
27 | #include "scope.h" | |
28 | #include "module.h" | |
29 | #include "aggregate.h" | |
30 | #include "declaration.h" | |
31 | #include "dsymbol.h" | |
32 | #include "mars.h" | |
33 | #include "dsymbol.h" | |
34 | #include "identifier.h" | |
35 | #include "hdrgen.h" | |
36 | #include "id.h" | |
37 | #include "attrib.h" | |
38 | #include "tokens.h" | |
39 | ||
40 | #define IDX_NOTFOUND (0x12345678) // index is not found | |
41 | ||
42 | Type *rawTypeMerge(Type *t1, Type *t2); | |
43 | bool MODimplicitConv(MOD modfrom, MOD modto); | |
44 | MATCH MODmethodConv(MOD modfrom, MOD modto); | |
45 | MOD MODmerge(MOD mod1, MOD mod2); | |
46 | ||
47 | static size_t templateParameterLookup(Type *tparam, TemplateParameters *parameters); | |
48 | static int arrayObjectMatch(Objects *oa1, Objects *oa2); | |
49 | static unsigned char deduceWildHelper(Type *t, Type **at, Type *tparam); | |
50 | static MATCH deduceTypeHelper(Type *t, Type **at, Type *tparam); | |
51 | static bool reliesOnTident(Type *t, TemplateParameters *tparams = NULL, size_t iStart = 0); | |
52 | Expression *semantic(Expression *e, Scope *sc); | |
53 | bool evalStaticCondition(Scope *sc, Expression *exp, Expression *e, bool &errors); | |
54 | ||
55 | /******************************************** | |
56 | * These functions substitute for dynamic_cast. dynamic_cast does not work | |
57 | * on earlier versions of gcc. | |
58 | */ | |
59 | ||
60 | Expression *isExpression(RootObject *o) | |
61 | { | |
62 | //return dynamic_cast<Expression *>(o); | |
63 | if (!o || o->dyncast() != DYNCAST_EXPRESSION) | |
64 | return NULL; | |
65 | return (Expression *)o; | |
66 | } | |
67 | ||
68 | Dsymbol *isDsymbol(RootObject *o) | |
69 | { | |
70 | //return dynamic_cast<Dsymbol *>(o); | |
71 | if (!o || o->dyncast() != DYNCAST_DSYMBOL) | |
72 | return NULL; | |
73 | return (Dsymbol *)o; | |
74 | } | |
75 | ||
76 | Type *isType(RootObject *o) | |
77 | { | |
78 | //return dynamic_cast<Type *>(o); | |
79 | if (!o || o->dyncast() != DYNCAST_TYPE) | |
80 | return NULL; | |
81 | return (Type *)o; | |
82 | } | |
83 | ||
84 | Tuple *isTuple(RootObject *o) | |
85 | { | |
86 | //return dynamic_cast<Tuple *>(o); | |
87 | if (!o || o->dyncast() != DYNCAST_TUPLE) | |
88 | return NULL; | |
89 | return (Tuple *)o; | |
90 | } | |
91 | ||
92 | Parameter *isParameter(RootObject *o) | |
93 | { | |
94 | //return dynamic_cast<Parameter *>(o); | |
95 | if (!o || o->dyncast() != DYNCAST_PARAMETER) | |
96 | return NULL; | |
97 | return (Parameter *)o; | |
98 | } | |
99 | ||
100 | /************************************** | |
101 | * Is this Object an error? | |
102 | */ | |
103 | bool isError(RootObject *o) | |
104 | { | |
105 | Type *t = isType(o); | |
106 | if (t) | |
107 | return (t->ty == Terror); | |
108 | Expression *e = isExpression(o); | |
109 | if (e) | |
110 | return (e->op == TOKerror || !e->type || e->type->ty == Terror); | |
111 | Tuple *v = isTuple(o); | |
112 | if (v) | |
113 | return arrayObjectIsError(&v->objects); | |
114 | Dsymbol *s = isDsymbol(o); | |
115 | assert(s); | |
116 | if (s->errors) | |
117 | return true; | |
118 | return s->parent ? isError(s->parent) : false; | |
119 | } | |
120 | ||
121 | /************************************** | |
122 | * Are any of the Objects an error? | |
123 | */ | |
124 | bool arrayObjectIsError(Objects *args) | |
125 | { | |
126 | for (size_t i = 0; i < args->dim; i++) | |
127 | { | |
128 | RootObject *o = (*args)[i]; | |
129 | if (isError(o)) | |
130 | return true; | |
131 | } | |
132 | return false; | |
133 | } | |
134 | ||
135 | /*********************** | |
136 | * Try to get arg as a type. | |
137 | */ | |
138 | ||
139 | Type *getType(RootObject *o) | |
140 | { | |
141 | Type *t = isType(o); | |
142 | if (!t) | |
143 | { | |
144 | Expression *e = isExpression(o); | |
145 | if (e) | |
146 | t = e->type; | |
147 | } | |
148 | return t; | |
149 | } | |
150 | ||
151 | Dsymbol *getDsymbol(RootObject *oarg) | |
152 | { | |
153 | //printf("getDsymbol()\n"); | |
154 | //printf("e %p s %p t %p v %p\n", isExpression(oarg), isDsymbol(oarg), isType(oarg), isTuple(oarg)); | |
155 | ||
156 | Dsymbol *sa; | |
157 | Expression *ea = isExpression(oarg); | |
158 | if (ea) | |
159 | { | |
160 | // Try to convert Expression to symbol | |
161 | if (ea->op == TOKvar) | |
162 | sa = ((VarExp *)ea)->var; | |
163 | else if (ea->op == TOKfunction) | |
164 | { | |
165 | if (((FuncExp *)ea)->td) | |
166 | sa = ((FuncExp *)ea)->td; | |
167 | else | |
168 | sa = ((FuncExp *)ea)->fd; | |
169 | } | |
170 | else if (ea->op == TOKtemplate) | |
171 | sa = ((TemplateExp *)ea)->td; | |
172 | else | |
173 | sa = NULL; | |
174 | } | |
175 | else | |
176 | { | |
177 | // Try to convert Type to symbol | |
178 | Type *ta = isType(oarg); | |
179 | if (ta) | |
180 | sa = ta->toDsymbol(NULL); | |
181 | else | |
182 | sa = isDsymbol(oarg); // if already a symbol | |
183 | } | |
184 | return sa; | |
185 | } | |
186 | ||
187 | /*********************** | |
188 | * Try to get value from manifest constant | |
189 | */ | |
190 | ||
191 | static Expression *getValue(Expression *e) | |
192 | { | |
193 | if (e && e->op == TOKvar) | |
194 | { | |
195 | VarDeclaration *v = ((VarExp *)e)->var->isVarDeclaration(); | |
196 | if (v && v->storage_class & STCmanifest) | |
197 | { | |
198 | e = v->getConstInitializer(); | |
199 | } | |
200 | } | |
201 | return e; | |
202 | } | |
203 | ||
204 | static Expression *getValue(Dsymbol *&s) | |
205 | { | |
206 | Expression *e = NULL; | |
207 | if (s) | |
208 | { | |
209 | VarDeclaration *v = s->isVarDeclaration(); | |
210 | if (v && v->storage_class & STCmanifest) | |
211 | { | |
212 | e = v->getConstInitializer(); | |
213 | } | |
214 | } | |
215 | return e; | |
216 | } | |
217 | ||
218 | /********************************** | |
219 | * Return true if e could be valid only as a template value parameter. | |
220 | * Return false if it might be an alias or tuple. | |
221 | * (Note that even in this case, it could still turn out to be a value). | |
222 | */ | |
223 | bool definitelyValueParameter(Expression *e) | |
224 | { | |
225 | // None of these can be value parameters | |
226 | if (e->op == TOKtuple || e->op == TOKscope || | |
227 | e->op == TOKtype || e->op == TOKdottype || | |
228 | e->op == TOKtemplate || e->op == TOKdottd || | |
229 | e->op == TOKfunction || e->op == TOKerror || | |
230 | e->op == TOKthis || e->op == TOKsuper) | |
231 | return false; | |
232 | ||
233 | if (e->op != TOKdotvar) | |
234 | return true; | |
235 | ||
236 | /* Template instantiations involving a DotVar expression are difficult. | |
237 | * In most cases, they should be treated as a value parameter, and interpreted. | |
238 | * But they might also just be a fully qualified name, which should be treated | |
239 | * as an alias. | |
240 | */ | |
241 | ||
242 | // x.y.f cannot be a value | |
243 | FuncDeclaration *f = ((DotVarExp *)e)->var->isFuncDeclaration(); | |
244 | if (f) | |
245 | return false; | |
246 | ||
247 | while (e->op == TOKdotvar) | |
248 | { | |
249 | e = ((DotVarExp *)e)->e1; | |
250 | } | |
251 | // this.x.y and super.x.y couldn't possibly be valid values. | |
252 | if (e->op == TOKthis || e->op == TOKsuper) | |
253 | return false; | |
254 | ||
255 | // e.type.x could be an alias | |
256 | if (e->op == TOKdottype) | |
257 | return false; | |
258 | ||
259 | // var.x.y is the only other possible form of alias | |
260 | if (e->op != TOKvar) | |
261 | return true; | |
262 | ||
263 | VarDeclaration *v = ((VarExp *)e)->var->isVarDeclaration(); | |
264 | ||
265 | // func.x.y is not an alias | |
266 | if (!v) | |
267 | return true; | |
268 | ||
269 | // TODO: Should we force CTFE if it is a global constant? | |
270 | ||
271 | return false; | |
272 | } | |
273 | ||
274 | static Expression *getExpression(RootObject *o) | |
275 | { | |
276 | Dsymbol *s = isDsymbol(o); | |
277 | return s ? getValue(s) : getValue(isExpression(o)); | |
278 | } | |
279 | ||
280 | /****************************** | |
281 | * If o1 matches o2, return true. | |
282 | * Else, return false. | |
283 | */ | |
284 | ||
285 | static bool match(RootObject *o1, RootObject *o2) | |
286 | { | |
287 | //printf("match() o1 = %p %s (%d), o2 = %p %s (%d)\n", | |
288 | // o1, o1->toChars(), o1->dyncast(), o2, o2->toChars(), o2->dyncast()); | |
289 | ||
290 | /* A proper implementation of the various equals() overrides | |
291 | * should make it possible to just do o1->equals(o2), but | |
292 | * we'll do that another day. | |
293 | */ | |
294 | ||
295 | /* Manifest constants should be compared by their values, | |
296 | * at least in template arguments. | |
297 | */ | |
298 | ||
299 | if (Type *t1 = isType(o1)) | |
300 | { | |
301 | Type *t2 = isType(o2); | |
302 | if (!t2) | |
303 | goto Lnomatch; | |
304 | ||
305 | //printf("\tt1 = %s\n", t1->toChars()); | |
306 | //printf("\tt2 = %s\n", t2->toChars()); | |
307 | if (!t1->equals(t2)) | |
308 | goto Lnomatch; | |
309 | ||
310 | goto Lmatch; | |
311 | } | |
312 | if (Expression *e1 = getExpression(o1)) | |
313 | { | |
314 | Expression *e2 = getExpression(o2); | |
315 | if (!e2) | |
316 | goto Lnomatch; | |
317 | ||
318 | //printf("\te1 = %s %s %s\n", e1->type->toChars(), Token::toChars(e1->op), e1->toChars()); | |
319 | //printf("\te2 = %s %s %s\n", e2->type->toChars(), Token::toChars(e2->op), e2->toChars()); | |
320 | ||
321 | // two expressions can be equal although they do not have the same | |
322 | // type; that happens when they have the same value. So check type | |
323 | // as well as expression equality to ensure templates are properly | |
324 | // matched. | |
325 | if (!e1->type->equals(e2->type) || !e1->equals(e2)) | |
326 | goto Lnomatch; | |
327 | ||
328 | goto Lmatch; | |
329 | } | |
330 | if (Dsymbol *s1 = isDsymbol(o1)) | |
331 | { | |
332 | Dsymbol *s2 = isDsymbol(o2); | |
333 | if (!s2) | |
334 | goto Lnomatch; | |
335 | ||
336 | //printf("\ts1 = %s\n", s1->toChars()); | |
337 | //printf("\ts2 = %s\n", s2->toChars()); | |
338 | if (!s1->equals(s2)) | |
339 | goto Lnomatch; | |
340 | if (s1->parent != s2->parent && !s1->isFuncDeclaration() && !s2->isFuncDeclaration()) | |
341 | goto Lnomatch; | |
342 | ||
343 | goto Lmatch; | |
344 | } | |
345 | if (Tuple *u1 = isTuple(o1)) | |
346 | { | |
347 | Tuple *u2 = isTuple(o2); | |
348 | if (!u2) | |
349 | goto Lnomatch; | |
350 | ||
351 | //printf("\tu1 = %s\n", u1->toChars()); | |
352 | //printf("\tu2 = %s\n", u2->toChars()); | |
353 | if (!arrayObjectMatch(&u1->objects, &u2->objects)) | |
354 | goto Lnomatch; | |
355 | ||
356 | goto Lmatch; | |
357 | } | |
358 | Lmatch: | |
359 | //printf("\t-> match\n"); | |
360 | return true; | |
361 | ||
362 | Lnomatch: | |
363 | //printf("\t-> nomatch\n"); | |
364 | return false; | |
365 | } | |
366 | ||
367 | ||
368 | /************************************ | |
369 | * Match an array of them. | |
370 | */ | |
371 | int arrayObjectMatch(Objects *oa1, Objects *oa2) | |
372 | { | |
373 | if (oa1 == oa2) | |
374 | return 1; | |
375 | if (oa1->dim != oa2->dim) | |
376 | return 0; | |
377 | for (size_t j = 0; j < oa1->dim; j++) | |
378 | { | |
379 | RootObject *o1 = (*oa1)[j]; | |
380 | RootObject *o2 = (*oa2)[j]; | |
381 | if (!match(o1, o2)) | |
382 | { | |
383 | return 0; | |
384 | } | |
385 | } | |
386 | return 1; | |
387 | } | |
388 | ||
389 | ||
390 | /************************************ | |
391 | * Computes hash of expression. | |
392 | * Handles all Expression classes and MUST match their equals method, | |
393 | * i.e. e1->equals(e2) implies expressionHash(e1) == expressionHash(e2). | |
394 | */ | |
395 | static hash_t expressionHash(Expression *e) | |
396 | { | |
397 | switch (e->op) | |
398 | { | |
399 | case TOKint64: | |
400 | return (size_t) ((IntegerExp *)e)->getInteger(); | |
401 | ||
402 | case TOKfloat64: | |
403 | return CTFloat::hash(((RealExp *)e)->value); | |
404 | ||
405 | case TOKcomplex80: | |
406 | { | |
407 | ComplexExp *ce = (ComplexExp *)e; | |
408 | return mixHash(CTFloat::hash(ce->toReal()), CTFloat::hash(ce->toImaginary())); | |
409 | } | |
410 | ||
411 | case TOKidentifier: | |
412 | return (size_t)(void *) ((IdentifierExp *)e)->ident; | |
413 | ||
414 | case TOKnull: | |
415 | return (size_t)(void *) ((NullExp *)e)->type; | |
416 | ||
417 | case TOKstring: | |
418 | { | |
419 | StringExp *se = (StringExp *)e; | |
420 | return calcHash((const char *)se->string, se->len * se->sz); | |
421 | } | |
422 | ||
423 | case TOKtuple: | |
424 | { | |
425 | TupleExp *te = (TupleExp *)e; | |
426 | size_t hash = 0; | |
427 | hash += te->e0 ? expressionHash(te->e0) : 0; | |
428 | for (size_t i = 0; i < te->exps->dim; i++) | |
429 | { | |
430 | Expression *elem = (*te->exps)[i]; | |
431 | hash = mixHash(hash, expressionHash(elem)); | |
432 | } | |
433 | return hash; | |
434 | } | |
435 | ||
436 | case TOKarrayliteral: | |
437 | { | |
438 | ArrayLiteralExp *ae = (ArrayLiteralExp *)e; | |
439 | size_t hash = 0; | |
440 | for (size_t i = 0; i < ae->elements->dim; i++) | |
441 | hash = mixHash(hash, expressionHash(ae->getElement(i))); | |
442 | return hash; | |
443 | } | |
444 | ||
445 | case TOKassocarrayliteral: | |
446 | { | |
447 | AssocArrayLiteralExp *ae = (AssocArrayLiteralExp *)e; | |
448 | size_t hash = 0; | |
449 | for (size_t i = 0; i < ae->keys->dim; i++) | |
450 | // reduction needs associative op as keys are unsorted (use XOR) | |
451 | hash ^= mixHash(expressionHash((*ae->keys)[i]), expressionHash((*ae->values)[i])); | |
452 | return hash; | |
453 | } | |
454 | ||
455 | case TOKstructliteral: | |
456 | { | |
457 | StructLiteralExp *se = (StructLiteralExp *)e; | |
458 | size_t hash = 0; | |
459 | for (size_t i = 0; i < se->elements->dim; i++) | |
460 | { | |
461 | Expression *elem = (*se->elements)[i]; | |
462 | hash = mixHash(hash, elem ? expressionHash(elem) : 0); | |
463 | } | |
464 | return hash; | |
465 | } | |
466 | ||
467 | case TOKvar: | |
468 | return (size_t)(void *) ((VarExp *)e)->var; | |
469 | ||
470 | case TOKfunction: | |
471 | return (size_t)(void *) ((FuncExp *)e)->fd; | |
472 | ||
473 | default: | |
474 | // no custom equals for this expression | |
475 | // equals based on identity | |
476 | return (size_t)(void *) e; | |
477 | } | |
478 | } | |
479 | ||
480 | ||
481 | /************************************ | |
482 | * Return hash of Objects. | |
483 | */ | |
484 | static hash_t arrayObjectHash(Objects *oa1) | |
485 | { | |
486 | hash_t hash = 0; | |
487 | for (size_t j = 0; j < oa1->dim; j++) | |
488 | { | |
489 | /* Must follow the logic of match() | |
490 | */ | |
491 | RootObject *o1 = (*oa1)[j]; | |
492 | if (Type *t1 = isType(o1)) | |
493 | hash = mixHash(hash, (size_t)t1->deco); | |
494 | else if (Expression *e1 = getExpression(o1)) | |
495 | hash = mixHash(hash, expressionHash(e1)); | |
496 | else if (Dsymbol *s1 = isDsymbol(o1)) | |
497 | { | |
498 | FuncAliasDeclaration *fa1 = s1->isFuncAliasDeclaration(); | |
499 | if (fa1) | |
500 | s1 = fa1->toAliasFunc(); | |
501 | hash = mixHash(hash, mixHash((size_t)(void *)s1->getIdent(), (size_t)(void *)s1->parent)); | |
502 | } | |
503 | else if (Tuple *u1 = isTuple(o1)) | |
504 | hash = mixHash(hash, arrayObjectHash(&u1->objects)); | |
505 | } | |
506 | return hash; | |
507 | } | |
508 | ||
509 | RootObject *objectSyntaxCopy(RootObject *o) | |
510 | { | |
511 | if (!o) | |
512 | return NULL; | |
513 | if (Type *t = isType(o)) | |
514 | return t->syntaxCopy(); | |
515 | if (Expression *e = isExpression(o)) | |
516 | return e->syntaxCopy(); | |
517 | return o; | |
518 | } | |
519 | ||
520 | ||
521 | /* ======================== TemplateDeclaration ============================= */ | |
522 | ||
523 | TemplateDeclaration::TemplateDeclaration(Loc loc, Identifier *id, | |
524 | TemplateParameters *parameters, Expression *constraint, Dsymbols *decldefs, bool ismixin, bool literal) | |
525 | : ScopeDsymbol(id) | |
526 | { | |
527 | this->loc = loc; | |
528 | this->parameters = parameters; | |
529 | this->origParameters = parameters; | |
530 | this->constraint = constraint; | |
531 | this->members = decldefs; | |
532 | this->overnext = NULL; | |
533 | this->overroot = NULL; | |
534 | this->funcroot = NULL; | |
535 | this->onemember = NULL; | |
536 | this->literal = literal; | |
537 | this->ismixin = ismixin; | |
538 | this->isstatic = true; | |
539 | this->previous = NULL; | |
540 | this->protection = Prot(PROTundefined); | |
541 | this->instances = NULL; | |
542 | ||
543 | // Compute in advance for Ddoc's use | |
544 | // Bugzilla 11153: ident could be NULL if parsing fails. | |
545 | if (members && ident) | |
546 | { | |
547 | Dsymbol *s; | |
548 | if (Dsymbol::oneMembers(members, &s, ident) && s) | |
549 | { | |
550 | onemember = s; | |
551 | s->parent = this; | |
552 | } | |
553 | } | |
554 | } | |
555 | ||
556 | Dsymbol *TemplateDeclaration::syntaxCopy(Dsymbol *) | |
557 | { | |
558 | //printf("TemplateDeclaration::syntaxCopy()\n"); | |
559 | TemplateParameters *p = NULL; | |
560 | if (parameters) | |
561 | { | |
562 | p = new TemplateParameters(); | |
563 | p->setDim(parameters->dim); | |
564 | for (size_t i = 0; i < p->dim; i++) | |
565 | (*p)[i] = (*parameters)[i]->syntaxCopy(); | |
566 | } | |
567 | return new TemplateDeclaration(loc, ident, p, | |
568 | constraint ? constraint->syntaxCopy() : NULL, | |
569 | Dsymbol::arraySyntaxCopy(members), ismixin, literal); | |
570 | } | |
571 | ||
572 | void TemplateDeclaration::semantic(Scope *sc) | |
573 | { | |
574 | if (semanticRun != PASSinit) | |
575 | return; // semantic() already run | |
576 | ||
577 | // Remember templates defined in module object that we need to know about | |
578 | if (sc->_module && sc->_module->ident == Id::object) | |
579 | { | |
580 | if (ident == Id::RTInfo) | |
581 | Type::rtinfo = this; | |
582 | } | |
583 | ||
584 | /* Remember Scope for later instantiations, but make | |
585 | * a copy since attributes can change. | |
586 | */ | |
587 | if (!this->_scope) | |
588 | { | |
589 | this->_scope = sc->copy(); | |
590 | this->_scope->setNoFree(); | |
591 | } | |
592 | ||
593 | semanticRun = PASSsemantic; | |
594 | ||
595 | parent = sc->parent; | |
596 | protection = sc->protection; | |
597 | isstatic = toParent()->isModule() || (_scope->stc & STCstatic); | |
598 | ||
599 | if (!isstatic) | |
600 | { | |
601 | if (AggregateDeclaration *ad = parent->pastMixin()->isAggregateDeclaration()) | |
602 | ad->makeNested(); | |
603 | } | |
604 | ||
605 | // Set up scope for parameters | |
606 | ScopeDsymbol *paramsym = new ScopeDsymbol(); | |
607 | paramsym->parent = parent; | |
608 | Scope *paramscope = sc->push(paramsym); | |
609 | paramscope->stc = 0; | |
610 | ||
611 | if (global.params.doDocComments) | |
612 | { | |
613 | origParameters = new TemplateParameters(); | |
614 | origParameters->setDim(parameters->dim); | |
615 | for (size_t i = 0; i < parameters->dim; i++) | |
616 | { | |
617 | TemplateParameter *tp = (*parameters)[i]; | |
618 | (*origParameters)[i] = tp->syntaxCopy(); | |
619 | } | |
620 | } | |
621 | ||
622 | for (size_t i = 0; i < parameters->dim; i++) | |
623 | { | |
624 | TemplateParameter *tp = (*parameters)[i]; | |
625 | ||
626 | if (!tp->declareParameter(paramscope)) | |
627 | { | |
628 | error(tp->loc, "parameter '%s' multiply defined", tp->ident->toChars()); | |
629 | errors = true; | |
630 | } | |
631 | if (!tp->semantic(paramscope, parameters)) | |
632 | { | |
633 | errors = true; | |
634 | } | |
635 | if (i + 1 != parameters->dim && tp->isTemplateTupleParameter()) | |
636 | { | |
637 | error("template tuple parameter must be last one"); | |
638 | errors = true; | |
639 | } | |
640 | } | |
641 | ||
642 | /* Calculate TemplateParameter::dependent | |
643 | */ | |
644 | TemplateParameters tparams; | |
645 | tparams.setDim(1); | |
646 | for (size_t i = 0; i < parameters->dim; i++) | |
647 | { | |
648 | TemplateParameter *tp = (*parameters)[i]; | |
649 | tparams[0] = tp; | |
650 | ||
651 | for (size_t j = 0; j < parameters->dim; j++) | |
652 | { | |
653 | // Skip cases like: X(T : T) | |
654 | if (i == j) | |
655 | continue; | |
656 | ||
657 | if (TemplateTypeParameter *ttp = (*parameters)[j]->isTemplateTypeParameter()) | |
658 | { | |
659 | if (reliesOnTident(ttp->specType, &tparams)) | |
660 | tp->dependent = true; | |
661 | } | |
662 | else if (TemplateAliasParameter *tap = (*parameters)[j]->isTemplateAliasParameter()) | |
663 | { | |
664 | if (reliesOnTident(tap->specType, &tparams) || | |
665 | reliesOnTident(isType(tap->specAlias), &tparams)) | |
666 | { | |
667 | tp->dependent = true; | |
668 | } | |
669 | } | |
670 | } | |
671 | } | |
672 | ||
673 | paramscope->pop(); | |
674 | ||
675 | // Compute again | |
676 | onemember = NULL; | |
677 | if (members) | |
678 | { | |
679 | Dsymbol *s; | |
680 | if (Dsymbol::oneMembers(members, &s, ident) && s) | |
681 | { | |
682 | onemember = s; | |
683 | s->parent = this; | |
684 | } | |
685 | } | |
686 | ||
687 | /* BUG: should check: | |
688 | * o no virtual functions or non-static data members of classes | |
689 | */ | |
690 | } | |
691 | ||
692 | const char *TemplateDeclaration::kind() | |
693 | { | |
694 | return (onemember && onemember->isAggregateDeclaration()) | |
695 | ? onemember->kind() | |
696 | : "template"; | |
697 | } | |
698 | ||
699 | /********************************** | |
700 | * Overload existing TemplateDeclaration 'this' with the new one 's'. | |
701 | * Return true if successful; i.e. no conflict. | |
702 | */ | |
703 | ||
704 | bool TemplateDeclaration::overloadInsert(Dsymbol *s) | |
705 | { | |
706 | FuncDeclaration *fd = s->isFuncDeclaration(); | |
707 | if (fd) | |
708 | { | |
709 | if (funcroot) | |
710 | return funcroot->overloadInsert(fd); | |
711 | funcroot = fd; | |
712 | return funcroot->overloadInsert(this); | |
713 | } | |
714 | ||
715 | TemplateDeclaration *td = s->isTemplateDeclaration(); | |
716 | if (!td) | |
717 | return false; | |
718 | ||
719 | TemplateDeclaration *pthis = this; | |
720 | TemplateDeclaration **ptd; | |
721 | for (ptd = &pthis; *ptd; ptd = &(*ptd)->overnext) | |
722 | { | |
723 | } | |
724 | ||
725 | td->overroot = this; | |
726 | *ptd = td; | |
727 | return true; | |
728 | } | |
729 | ||
730 | /**************************** | |
731 | * Check to see if constraint is satisfied. | |
732 | */ | |
733 | bool TemplateDeclaration::evaluateConstraint( | |
734 | TemplateInstance *ti, Scope *sc, Scope *paramscope, | |
735 | Objects *dedargs, FuncDeclaration *fd) | |
736 | { | |
737 | /* Detect recursive attempts to instantiate this template declaration, | |
738 | * Bugzilla 4072 | |
739 | * void foo(T)(T x) if (is(typeof(foo(x)))) { } | |
740 | * static assert(!is(typeof(foo(7)))); | |
741 | * Recursive attempts are regarded as a constraint failure. | |
742 | */ | |
743 | /* There's a chicken-and-egg problem here. We don't know yet if this template | |
744 | * instantiation will be a local one (enclosing is set), and we won't know until | |
745 | * after selecting the correct template. Thus, function we're nesting inside | |
746 | * is not on the sc scope chain, and this can cause errors in FuncDeclaration::getLevel(). | |
747 | * Workaround the problem by setting a flag to relax the checking on frame errors. | |
748 | */ | |
749 | ||
750 | for (TemplatePrevious *p = previous; p; p = p->prev) | |
751 | { | |
752 | if (arrayObjectMatch(p->dedargs, dedargs)) | |
753 | { | |
754 | //printf("recursive, no match p->sc=%p %p %s\n", p->sc, this, this->toChars()); | |
755 | /* It must be a subscope of p->sc, other scope chains are not recursive | |
756 | * instantiations. | |
757 | */ | |
758 | for (Scope *scx = sc; scx; scx = scx->enclosing) | |
759 | { | |
760 | if (scx == p->sc) | |
761 | return false; | |
762 | } | |
763 | } | |
764 | /* BUG: should also check for ref param differences | |
765 | */ | |
766 | } | |
767 | ||
768 | TemplatePrevious pr; | |
769 | pr.prev = previous; | |
770 | pr.sc = paramscope; | |
771 | pr.dedargs = dedargs; | |
772 | previous = ≺ // add this to threaded list | |
773 | ||
774 | Scope *scx = paramscope->push(ti); | |
775 | scx->parent = ti; | |
776 | scx->tinst = NULL; | |
777 | scx->minst = NULL; | |
778 | ||
779 | assert(!ti->symtab); | |
780 | if (fd) | |
781 | { | |
782 | /* Declare all the function parameters as variables and add them to the scope | |
783 | * Making parameters is similar to FuncDeclaration::semantic3 | |
784 | */ | |
785 | TypeFunction *tf = (TypeFunction *)fd->type; | |
786 | assert(tf->ty == Tfunction); | |
787 | ||
788 | scx->parent = fd; | |
789 | ||
790 | Parameters *fparameters = tf->parameters; | |
791 | int fvarargs = tf->varargs; | |
792 | ||
793 | size_t nfparams = Parameter::dim(fparameters); | |
794 | for (size_t i = 0; i < nfparams; i++) | |
795 | { | |
796 | Parameter *fparam = Parameter::getNth(fparameters, i); | |
797 | fparam->storageClass &= (STCin | STCout | STCref | STClazy | STCfinal | STC_TYPECTOR | STCnodtor); | |
798 | fparam->storageClass |= STCparameter; | |
799 | if (fvarargs == 2 && i + 1 == nfparams) | |
800 | fparam->storageClass |= STCvariadic; | |
801 | } | |
802 | for (size_t i = 0; i < fparameters->dim; i++) | |
803 | { | |
804 | Parameter *fparam = (*fparameters)[i]; | |
805 | if (!fparam->ident) | |
806 | continue; // don't add it, if it has no name | |
807 | VarDeclaration *v = new VarDeclaration(loc, fparam->type, fparam->ident, NULL); | |
808 | v->storage_class = fparam->storageClass; | |
809 | v->semantic(scx); | |
810 | if (!ti->symtab) | |
811 | ti->symtab = new DsymbolTable(); | |
812 | if (!scx->insert(v)) | |
813 | error("parameter %s.%s is already defined", toChars(), v->toChars()); | |
814 | else | |
815 | v->parent = fd; | |
816 | } | |
817 | if (isstatic) | |
818 | fd->storage_class |= STCstatic; | |
819 | ||
820 | fd->vthis = fd->declareThis(scx, fd->isThis()); | |
821 | } | |
822 | ||
823 | Expression *e = constraint->syntaxCopy(); | |
824 | ||
825 | assert(ti->inst == NULL); | |
826 | ti->inst = ti; // temporary instantiation to enable genIdent() | |
827 | ||
828 | scx->flags |= SCOPEconstraint; | |
829 | bool errors = false; | |
830 | bool result = evalStaticCondition(scx, constraint, e, errors); | |
831 | ti->inst = NULL; | |
832 | ti->symtab = NULL; | |
833 | scx = scx->pop(); | |
834 | previous = pr.prev; // unlink from threaded list | |
835 | if (errors) | |
836 | return false; | |
837 | return result; | |
838 | } | |
839 | ||
840 | /*************************************** | |
841 | * Given that ti is an instance of this TemplateDeclaration, | |
842 | * deduce the types of the parameters to this, and store | |
843 | * those deduced types in dedtypes[]. | |
844 | * Input: | |
845 | * flag 1: don't do semantic() because of dummy types | |
846 | * 2: don't change types in matchArg() | |
847 | * Output: | |
848 | * dedtypes deduced arguments | |
849 | * Return match level. | |
850 | */ | |
851 | ||
852 | MATCH TemplateDeclaration::matchWithInstance(Scope *sc, TemplateInstance *ti, | |
853 | Objects *dedtypes, Expressions *fargs, int flag) | |
854 | { | |
855 | MATCH m; | |
856 | size_t dedtypes_dim = dedtypes->dim; | |
857 | ||
858 | dedtypes->zero(); | |
859 | ||
860 | if (errors) | |
861 | return MATCHnomatch; | |
862 | ||
863 | size_t parameters_dim = parameters->dim; | |
864 | int variadic = isVariadic() != NULL; | |
865 | ||
866 | // If more arguments than parameters, no match | |
867 | if (ti->tiargs->dim > parameters_dim && !variadic) | |
868 | { | |
869 | return MATCHnomatch; | |
870 | } | |
871 | ||
872 | assert(dedtypes_dim == parameters_dim); | |
873 | assert(dedtypes_dim >= ti->tiargs->dim || variadic); | |
874 | ||
875 | assert(_scope); | |
876 | ||
877 | // Set up scope for template parameters | |
878 | ScopeDsymbol *paramsym = new ScopeDsymbol(); | |
879 | paramsym->parent = _scope->parent; | |
880 | Scope *paramscope = _scope->push(paramsym); | |
881 | paramscope->tinst = ti; | |
882 | paramscope->minst = sc->minst; | |
883 | paramscope->callsc = sc; | |
884 | paramscope->stc = 0; | |
885 | ||
886 | // Attempt type deduction | |
887 | m = MATCHexact; | |
888 | for (size_t i = 0; i < dedtypes_dim; i++) | |
889 | { | |
890 | MATCH m2; | |
891 | TemplateParameter *tp = (*parameters)[i]; | |
892 | Declaration *sparam; | |
893 | ||
894 | //printf("\targument [%d]\n", i); | |
895 | m2 = tp->matchArg(ti->loc, paramscope, ti->tiargs, i, parameters, dedtypes, &sparam); | |
896 | //printf("\tm2 = %d\n", m2); | |
897 | ||
898 | if (m2 == MATCHnomatch) | |
899 | { | |
900 | goto Lnomatch; | |
901 | } | |
902 | ||
903 | if (m2 < m) | |
904 | m = m2; | |
905 | ||
906 | if (!flag) | |
907 | sparam->semantic(paramscope); | |
908 | if (!paramscope->insert(sparam)) // TODO: This check can make more early | |
909 | goto Lnomatch; // in TemplateDeclaration::semantic, and | |
910 | // then we don't need to make sparam if flags == 0 | |
911 | } | |
912 | ||
913 | if (!flag) | |
914 | { | |
915 | /* Any parameter left without a type gets the type of | |
916 | * its corresponding arg | |
917 | */ | |
918 | for (size_t i = 0; i < dedtypes_dim; i++) | |
919 | { | |
920 | if (!(*dedtypes)[i]) | |
921 | { | |
922 | assert(i < ti->tiargs->dim); | |
923 | (*dedtypes)[i] = (Type *)(*ti->tiargs)[i]; | |
924 | } | |
925 | } | |
926 | } | |
927 | ||
928 | if (m > MATCHnomatch && constraint && !flag) | |
929 | { | |
930 | if (ti->hasNestedArgs(ti->tiargs, this->isstatic)) // TODO: should gag error | |
931 | ti->parent = ti->enclosing; | |
932 | else | |
933 | ti->parent = this->parent; | |
934 | ||
935 | // Similar to doHeaderInstantiation | |
936 | FuncDeclaration *fd = onemember ? onemember->isFuncDeclaration() : NULL; | |
937 | if (fd) | |
938 | { | |
939 | assert(fd->type->ty == Tfunction); | |
940 | TypeFunction *tf = (TypeFunction *)fd->type->syntaxCopy(); | |
941 | ||
942 | fd = new FuncDeclaration(fd->loc, fd->endloc, fd->ident, fd->storage_class, tf); | |
943 | fd->parent = ti; | |
944 | fd->inferRetType = true; | |
945 | ||
946 | // Shouldn't run semantic on default arguments and return type. | |
947 | for (size_t i = 0; i < tf->parameters->dim; i++) | |
948 | (*tf->parameters)[i]->defaultArg = NULL; | |
949 | tf->next = NULL; | |
950 | ||
951 | // Resolve parameter types and 'auto ref's. | |
952 | tf->fargs = fargs; | |
953 | unsigned olderrors = global.startGagging(); | |
954 | fd->type = tf->semantic(loc, paramscope); | |
955 | if (global.endGagging(olderrors)) | |
956 | { | |
957 | assert(fd->type->ty != Tfunction); | |
958 | goto Lnomatch; | |
959 | } | |
960 | assert(fd->type->ty == Tfunction); | |
961 | fd->originalType = fd->type; // for mangling | |
962 | } | |
963 | ||
964 | // TODO: dedtypes => ti->tiargs ? | |
965 | if (!evaluateConstraint(ti, sc, paramscope, dedtypes, fd)) | |
966 | goto Lnomatch; | |
967 | } | |
968 | ||
969 | goto Lret; | |
970 | ||
971 | Lnomatch: | |
972 | m = MATCHnomatch; | |
973 | ||
974 | Lret: | |
975 | paramscope->pop(); | |
976 | return m; | |
977 | } | |
978 | ||
979 | /******************************************** | |
980 | * Determine partial specialization order of 'this' vs td2. | |
981 | * Returns: | |
982 | * match this is at least as specialized as td2 | |
983 | * 0 td2 is more specialized than this | |
984 | */ | |
985 | ||
986 | MATCH TemplateDeclaration::leastAsSpecialized(Scope *sc, TemplateDeclaration *td2, Expressions *fargs) | |
987 | { | |
988 | /* This works by taking the template parameters to this template | |
989 | * declaration and feeding them to td2 as if it were a template | |
990 | * instance. | |
991 | * If it works, then this template is at least as specialized | |
992 | * as td2. | |
993 | */ | |
994 | ||
995 | TemplateInstance ti(Loc(), ident); // create dummy template instance | |
996 | // Set type arguments to dummy template instance to be types | |
997 | // generated from the parameters to this template declaration | |
998 | ti.tiargs = new Objects(); | |
999 | ti.tiargs->reserve(parameters->dim); | |
1000 | for (size_t i = 0; i < parameters->dim; i++) | |
1001 | { | |
1002 | TemplateParameter *tp = (*parameters)[i]; | |
1003 | if (tp->dependent) | |
1004 | break; | |
1005 | RootObject *p = (RootObject *)tp->dummyArg(); | |
1006 | if (!p) | |
1007 | break; | |
1008 | ||
1009 | ti.tiargs->push(p); | |
1010 | } | |
1011 | ||
1012 | // Temporary Array to hold deduced types | |
1013 | Objects dedtypes; | |
1014 | dedtypes.setDim(td2->parameters->dim); | |
1015 | ||
1016 | // Attempt a type deduction | |
1017 | MATCH m = td2->matchWithInstance(sc, &ti, &dedtypes, fargs, 1); | |
1018 | if (m > MATCHnomatch) | |
1019 | { | |
1020 | /* A non-variadic template is more specialized than a | |
1021 | * variadic one. | |
1022 | */ | |
1023 | TemplateTupleParameter *tp = isVariadic(); | |
1024 | if (tp && !tp->dependent && !td2->isVariadic()) | |
1025 | goto L1; | |
1026 | ||
1027 | return m; | |
1028 | } | |
1029 | L1: | |
1030 | return MATCHnomatch; | |
1031 | } | |
1032 | ||
1033 | static Expression *emptyArrayElement = NULL; | |
1034 | ||
1035 | class TypeDeduced : public Type | |
1036 | { | |
1037 | public: | |
1038 | Type *tded; | |
1039 | Expressions argexps; // corresponding expressions | |
1040 | Types tparams; // tparams[i]->mod | |
1041 | ||
1042 | TypeDeduced(Type *tt, Expression *e, Type *tparam) | |
1043 | : Type(Tnone) | |
1044 | { | |
1045 | tded = tt; | |
1046 | argexps.push(e); | |
1047 | tparams.push(tparam); | |
1048 | } | |
1049 | ||
1050 | virtual ~TypeDeduced() | |
1051 | { | |
1052 | } | |
1053 | ||
1054 | void update(Expression *e, Type *tparam) | |
1055 | { | |
1056 | argexps.push(e); | |
1057 | tparams.push(tparam); | |
1058 | } | |
1059 | void update(Type *tt, Expression *e, Type *tparam) | |
1060 | { | |
1061 | tded = tt; | |
1062 | argexps.push(e); | |
1063 | tparams.push(tparam); | |
1064 | } | |
1065 | MATCH matchAll(Type *tt) | |
1066 | { | |
1067 | MATCH match = MATCHexact; | |
1068 | for (size_t j = 0; j < argexps.dim; j++) | |
1069 | { | |
1070 | Expression *e = argexps[j]; | |
1071 | assert(e); | |
1072 | if (e == emptyArrayElement) | |
1073 | continue; | |
1074 | ||
1075 | Type *t = tt->addMod(tparams[j]->mod)->substWildTo(MODconst); | |
1076 | ||
1077 | MATCH m = e->implicitConvTo(t); | |
1078 | if (match > m) | |
1079 | match = m; | |
1080 | if (match <= MATCHnomatch) | |
1081 | break; | |
1082 | } | |
1083 | return match; | |
1084 | } | |
1085 | }; | |
1086 | ||
1087 | /************************************************* | |
1088 | * Match function arguments against a specific template function. | |
1089 | * Input: | |
1090 | * ti | |
1091 | * sc instantiation scope | |
1092 | * fd | |
1093 | * tthis 'this' argument if !NULL | |
1094 | * fargs arguments to function | |
1095 | * Output: | |
1096 | * fd Partially instantiated function declaration | |
1097 | * ti->tdtypes Expression/Type deduced template arguments | |
1098 | * Returns: | |
1099 | * match level | |
1100 | * bit 0-3 Match template parameters by inferred template arguments | |
1101 | * bit 4-7 Match template parameters by initial template arguments | |
1102 | */ | |
1103 | ||
1104 | MATCH TemplateDeclaration::deduceFunctionTemplateMatch( | |
1105 | TemplateInstance *ti, Scope *sc, | |
1106 | FuncDeclaration *&fd, Type *tthis, Expressions *fargs) | |
1107 | { | |
1108 | size_t nfparams; | |
1109 | size_t nfargs; | |
1110 | size_t ntargs; // array size of tiargs | |
1111 | size_t fptupindex = IDX_NOTFOUND; | |
1112 | MATCH match = MATCHexact; | |
1113 | MATCH matchTiargs = MATCHexact; | |
1114 | Parameters *fparameters; // function parameter list | |
1115 | int fvarargs; // function varargs | |
1116 | unsigned wildmatch = 0; | |
1117 | size_t inferStart = 0; | |
1118 | ||
1119 | Loc instLoc = ti->loc; | |
1120 | Objects *tiargs = ti->tiargs; | |
1121 | Objects *dedargs = new Objects(); | |
1122 | Objects* dedtypes = &ti->tdtypes; // for T:T*, the dedargs is the T*, dedtypes is the T | |
1123 | ||
1124 | assert(_scope); | |
1125 | ||
1126 | dedargs->setDim(parameters->dim); | |
1127 | dedargs->zero(); | |
1128 | ||
1129 | dedtypes->setDim(parameters->dim); | |
1130 | dedtypes->zero(); | |
1131 | ||
1132 | if (errors || fd->errors) | |
1133 | return MATCHnomatch; | |
1134 | ||
1135 | // Set up scope for parameters | |
1136 | ScopeDsymbol *paramsym = new ScopeDsymbol(); | |
1137 | paramsym->parent = _scope->parent; // should use hasnestedArgs and enclosing? | |
1138 | Scope *paramscope = _scope->push(paramsym); | |
1139 | paramscope->tinst = ti; | |
1140 | paramscope->minst = sc->minst; | |
1141 | paramscope->callsc = sc; | |
1142 | paramscope->stc = 0; | |
1143 | ||
1144 | TemplateTupleParameter *tp = isVariadic(); | |
1145 | Tuple *declaredTuple = NULL; | |
1146 | ||
1147 | ntargs = 0; | |
1148 | if (tiargs) | |
1149 | { | |
1150 | // Set initial template arguments | |
1151 | ntargs = tiargs->dim; | |
1152 | size_t n = parameters->dim; | |
1153 | if (tp) | |
1154 | n--; | |
1155 | if (ntargs > n) | |
1156 | { | |
1157 | if (!tp) | |
1158 | goto Lnomatch; | |
1159 | ||
1160 | /* The extra initial template arguments | |
1161 | * now form the tuple argument. | |
1162 | */ | |
1163 | Tuple *t = new Tuple(); | |
1164 | assert(parameters->dim); | |
1165 | (*dedargs)[parameters->dim - 1] = t; | |
1166 | ||
1167 | t->objects.setDim(ntargs - n); | |
1168 | for (size_t i = 0; i < t->objects.dim; i++) | |
1169 | { | |
1170 | t->objects[i] = (*tiargs)[n + i]; | |
1171 | } | |
1172 | declareParameter(paramscope, tp, t); | |
1173 | declaredTuple = t; | |
1174 | } | |
1175 | else | |
1176 | n = ntargs; | |
1177 | ||
1178 | memcpy(dedargs->tdata(), tiargs->tdata(), n * sizeof(*dedargs->tdata())); | |
1179 | ||
1180 | for (size_t i = 0; i < n; i++) | |
1181 | { | |
1182 | assert(i < parameters->dim); | |
1183 | Declaration *sparam = NULL; | |
1184 | MATCH m = (*parameters)[i]->matchArg(instLoc, paramscope, dedargs, i, parameters, dedtypes, &sparam); | |
1185 | //printf("\tdeduceType m = %d\n", m); | |
1186 | if (m <= MATCHnomatch) | |
1187 | goto Lnomatch; | |
1188 | if (m < matchTiargs) | |
1189 | matchTiargs = m; | |
1190 | ||
1191 | sparam->semantic(paramscope); | |
1192 | if (!paramscope->insert(sparam)) | |
1193 | goto Lnomatch; | |
1194 | } | |
1195 | if (n < parameters->dim && !declaredTuple) | |
1196 | { | |
1197 | inferStart = n; | |
1198 | } | |
1199 | else | |
1200 | inferStart = parameters->dim; | |
1201 | //printf("tiargs matchTiargs = %d\n", matchTiargs); | |
1202 | } | |
1203 | ||
1204 | fparameters = fd->getParameters(&fvarargs); | |
1205 | nfparams = Parameter::dim(fparameters); // number of function parameters | |
1206 | nfargs = fargs ? fargs->dim : 0; // number of function arguments | |
1207 | ||
1208 | /* Check for match of function arguments with variadic template | |
1209 | * parameter, such as: | |
1210 | * | |
1211 | * void foo(T, A...)(T t, A a); | |
1212 | * void main() { foo(1,2,3); } | |
1213 | */ | |
1214 | if (tp) // if variadic | |
1215 | { | |
1216 | // TemplateTupleParameter always makes most lesser matching. | |
1217 | matchTiargs = MATCHconvert; | |
1218 | ||
1219 | if (nfparams == 0 && nfargs != 0) // if no function parameters | |
1220 | { | |
1221 | if (!declaredTuple) | |
1222 | { | |
1223 | Tuple *t = new Tuple(); | |
1224 | //printf("t = %p\n", t); | |
1225 | (*dedargs)[parameters->dim - 1] = t; | |
1226 | declareParameter(paramscope, tp, t); | |
1227 | declaredTuple = t; | |
1228 | } | |
1229 | } | |
1230 | else | |
1231 | { | |
1232 | /* Figure out which of the function parameters matches | |
1233 | * the tuple template parameter. Do this by matching | |
1234 | * type identifiers. | |
1235 | * Set the index of this function parameter to fptupindex. | |
1236 | */ | |
1237 | for (fptupindex = 0; fptupindex < nfparams; fptupindex++) | |
1238 | { | |
1239 | Parameter *fparam = (*fparameters)[fptupindex]; | |
1240 | if (fparam->type->ty != Tident) | |
1241 | continue; | |
1242 | TypeIdentifier *tid = (TypeIdentifier *)fparam->type; | |
1243 | if (!tp->ident->equals(tid->ident) || tid->idents.dim) | |
1244 | continue; | |
1245 | ||
1246 | if (fvarargs) // variadic function doesn't | |
1247 | goto Lnomatch; // go with variadic template | |
1248 | ||
1249 | goto L1; | |
1250 | } | |
1251 | fptupindex = IDX_NOTFOUND; | |
1252 | L1: | |
1253 | ; | |
1254 | } | |
1255 | } | |
1256 | ||
1257 | if (toParent()->isModule() || (_scope->stc & STCstatic)) | |
1258 | tthis = NULL; | |
1259 | if (tthis) | |
1260 | { | |
1261 | bool hasttp = false; | |
1262 | ||
1263 | // Match 'tthis' to any TemplateThisParameter's | |
1264 | for (size_t i = 0; i < parameters->dim; i++) | |
1265 | { | |
1266 | TemplateThisParameter *ttp = (*parameters)[i]->isTemplateThisParameter(); | |
1267 | if (ttp) | |
1268 | { | |
1269 | hasttp = true; | |
1270 | ||
1271 | Type *t = new TypeIdentifier(Loc(), ttp->ident); | |
1272 | MATCH m = deduceType(tthis, paramscope, t, parameters, dedtypes); | |
1273 | if (m <= MATCHnomatch) | |
1274 | goto Lnomatch; | |
1275 | if (m < match) | |
1276 | match = m; // pick worst match | |
1277 | } | |
1278 | } | |
1279 | ||
1280 | // Match attributes of tthis against attributes of fd | |
1281 | if (fd->type && !fd->isCtorDeclaration()) | |
1282 | { | |
1283 | StorageClass stc = _scope->stc | fd->storage_class2; | |
1284 | // Propagate parent storage class (see bug 5504) | |
1285 | Dsymbol *p = parent; | |
1286 | while (p->isTemplateDeclaration() || p->isTemplateInstance()) | |
1287 | p = p->parent; | |
1288 | AggregateDeclaration *ad = p->isAggregateDeclaration(); | |
1289 | if (ad) | |
1290 | stc |= ad->storage_class; | |
1291 | ||
1292 | unsigned char mod = fd->type->mod; | |
1293 | if (stc & STCimmutable) | |
1294 | mod = MODimmutable; | |
1295 | else | |
1296 | { | |
1297 | if (stc & (STCshared | STCsynchronized)) | |
1298 | mod |= MODshared; | |
1299 | if (stc & STCconst) | |
1300 | mod |= MODconst; | |
1301 | if (stc & STCwild) | |
1302 | mod |= MODwild; | |
1303 | } | |
1304 | ||
1305 | unsigned char thismod = tthis->mod; | |
1306 | if (hasttp) | |
1307 | mod = MODmerge(thismod, mod); | |
1308 | MATCH m = MODmethodConv(thismod, mod); | |
1309 | if (m <= MATCHnomatch) | |
1310 | goto Lnomatch; | |
1311 | if (m < match) | |
1312 | match = m; | |
1313 | } | |
1314 | } | |
1315 | ||
1316 | // Loop through the function parameters | |
1317 | { | |
1318 | //printf("%s\n\tnfargs = %d, nfparams = %d, tuple_dim = %d\n", toChars(), nfargs, nfparams, declaredTuple ? declaredTuple->objects.dim : 0); | |
1319 | //printf("\ttp = %p, fptupindex = %d, found = %d, declaredTuple = %s\n", tp, fptupindex, fptupindex != IDX_NOTFOUND, declaredTuple ? declaredTuple->toChars() : NULL); | |
1320 | size_t argi = 0; | |
1321 | size_t nfargs2 = nfargs; // nfargs + supplied defaultArgs | |
1322 | for (size_t parami = 0; parami < nfparams; parami++) | |
1323 | { | |
1324 | Parameter *fparam = Parameter::getNth(fparameters, parami); | |
1325 | ||
1326 | // Apply function parameter storage classes to parameter types | |
1327 | Type *prmtype = fparam->type->addStorageClass(fparam->storageClass); | |
1328 | ||
1329 | Expression *farg; | |
1330 | ||
1331 | /* See function parameters which wound up | |
1332 | * as part of a template tuple parameter. | |
1333 | */ | |
1334 | if (fptupindex != IDX_NOTFOUND && parami == fptupindex) | |
1335 | { | |
1336 | assert(prmtype->ty == Tident); | |
1337 | TypeIdentifier *tid = (TypeIdentifier *)prmtype; | |
1338 | if (!declaredTuple) | |
1339 | { | |
1340 | /* The types of the function arguments | |
1341 | * now form the tuple argument. | |
1342 | */ | |
1343 | declaredTuple = new Tuple(); | |
1344 | (*dedargs)[parameters->dim - 1] = declaredTuple; | |
1345 | ||
1346 | /* Count function parameters following a tuple parameter. | |
1347 | * void foo(U, T...)(int y, T, U, int) {} // rem == 2 (U, int) | |
1348 | */ | |
1349 | size_t rem = 0; | |
1350 | for (size_t j = parami + 1; j < nfparams; j++) | |
1351 | { | |
1352 | Parameter *p = Parameter::getNth(fparameters, j); | |
1353 | if (!reliesOnTident(p->type, parameters, inferStart)) | |
1354 | { | |
1355 | Type *pt = p->type->syntaxCopy()->semantic(fd->loc, paramscope); | |
1356 | rem += pt->ty == Ttuple ? ((TypeTuple *)pt)->arguments->dim : 1; | |
1357 | } | |
1358 | else | |
1359 | { | |
1360 | ++rem; | |
1361 | } | |
1362 | } | |
1363 | ||
1364 | if (nfargs2 - argi < rem) | |
1365 | goto Lnomatch; | |
1366 | declaredTuple->objects.setDim(nfargs2 - argi - rem); | |
1367 | for (size_t i = 0; i < declaredTuple->objects.dim; i++) | |
1368 | { | |
1369 | farg = (*fargs)[argi + i]; | |
1370 | ||
1371 | // Check invalid arguments to detect errors early. | |
1372 | if (farg->op == TOKerror || farg->type->ty == Terror) | |
1373 | goto Lnomatch; | |
1374 | ||
1375 | if (!(fparam->storageClass & STClazy) && farg->type->ty == Tvoid) | |
1376 | goto Lnomatch; | |
1377 | ||
1378 | Type *tt; | |
1379 | MATCH m; | |
1380 | if (unsigned char wm = deduceWildHelper(farg->type, &tt, tid)) | |
1381 | { | |
1382 | wildmatch |= wm; | |
1383 | m = MATCHconst; | |
1384 | } | |
1385 | else | |
1386 | { | |
1387 | m = deduceTypeHelper(farg->type, &tt, tid); | |
1388 | } | |
1389 | if (m <= MATCHnomatch) | |
1390 | goto Lnomatch; | |
1391 | if (m < match) | |
1392 | match = m; | |
1393 | ||
1394 | /* Remove top const for dynamic array types and pointer types | |
1395 | */ | |
1396 | if ((tt->ty == Tarray || tt->ty == Tpointer) && | |
1397 | !tt->isMutable() && | |
1398 | (!(fparam->storageClass & STCref) || | |
1399 | ((fparam->storageClass & STCauto) && !farg->isLvalue()))) | |
1400 | { | |
1401 | tt = tt->mutableOf(); | |
1402 | } | |
1403 | declaredTuple->objects[i] = tt; | |
1404 | } | |
1405 | declareParameter(paramscope, tp, declaredTuple); | |
1406 | } | |
1407 | else | |
1408 | { | |
1409 | // Bugzilla 6810: If declared tuple is not a type tuple, | |
1410 | // it cannot be function parameter types. | |
1411 | for (size_t i = 0; i < declaredTuple->objects.dim; i++) | |
1412 | { | |
1413 | if (!isType(declaredTuple->objects[i])) | |
1414 | goto Lnomatch; | |
1415 | } | |
1416 | } | |
1417 | assert(declaredTuple); | |
1418 | argi += declaredTuple->objects.dim; | |
1419 | continue; | |
1420 | } | |
1421 | ||
1422 | // If parameter type doesn't depend on inferred template parameters, | |
1423 | // semantic it to get actual type. | |
1424 | if (!reliesOnTident(prmtype, parameters, inferStart)) | |
1425 | { | |
1426 | // should copy prmtype to avoid affecting semantic result | |
1427 | prmtype = prmtype->syntaxCopy()->semantic(fd->loc, paramscope); | |
1428 | ||
1429 | if (prmtype->ty == Ttuple) | |
1430 | { | |
1431 | TypeTuple *tt = (TypeTuple *)prmtype; | |
1432 | size_t tt_dim = tt->arguments->dim; | |
1433 | for (size_t j = 0; j < tt_dim; j++, ++argi) | |
1434 | { | |
1435 | Parameter *p = (*tt->arguments)[j]; | |
1436 | if (j == tt_dim - 1 && fvarargs == 2 && parami + 1 == nfparams && argi < nfargs) | |
1437 | { | |
1438 | prmtype = p->type; | |
1439 | goto Lvarargs; | |
1440 | } | |
1441 | if (argi >= nfargs) | |
1442 | { | |
1443 | if (p->defaultArg) | |
1444 | continue; | |
1445 | goto Lnomatch; | |
1446 | } | |
1447 | farg = (*fargs)[argi]; | |
1448 | if (!farg->implicitConvTo(p->type)) | |
1449 | goto Lnomatch; | |
1450 | } | |
1451 | continue; | |
1452 | } | |
1453 | } | |
1454 | ||
1455 | if (argi >= nfargs) // if not enough arguments | |
1456 | { | |
1457 | if (!fparam->defaultArg) | |
1458 | goto Lvarargs; | |
1459 | ||
1460 | /* Bugzilla 2803: Before the starting of type deduction from the function | |
1461 | * default arguments, set the already deduced parameters into paramscope. | |
1462 | * It's necessary to avoid breaking existing acceptable code. Cases: | |
1463 | * | |
1464 | * 1. Already deduced template parameters can appear in fparam->defaultArg: | |
1465 | * auto foo(A, B)(A a, B b = A.stringof); | |
1466 | * foo(1); | |
1467 | * // at fparam == 'B b = A.string', A is equivalent with the deduced type 'int' | |
1468 | * | |
1469 | * 2. If prmtype depends on default-specified template parameter, the | |
1470 | * default type should be preferred. | |
1471 | * auto foo(N = size_t, R)(R r, N start = 0) | |
1472 | * foo([1,2,3]); | |
1473 | * // at fparam `N start = 0`, N should be 'size_t' before | |
1474 | * // the deduction result from fparam->defaultArg. | |
1475 | */ | |
1476 | if (argi == nfargs) | |
1477 | { | |
1478 | for (size_t i = 0; i < dedtypes->dim; i++) | |
1479 | { | |
1480 | Type *at = isType((*dedtypes)[i]); | |
1481 | if (at && at->ty == Tnone) | |
1482 | { | |
1483 | TypeDeduced *xt = (TypeDeduced *)at; | |
1484 | (*dedtypes)[i] = xt->tded; // 'unbox' | |
1485 | delete xt; | |
1486 | } | |
1487 | } | |
1488 | for (size_t i = ntargs; i < dedargs->dim; i++) | |
1489 | { | |
1490 | TemplateParameter *tparam = (*parameters)[i]; | |
1491 | ||
1492 | RootObject *oarg = (*dedargs)[i]; | |
1493 | RootObject *oded = (*dedtypes)[i]; | |
1494 | if (!oarg) | |
1495 | { | |
1496 | if (oded) | |
1497 | { | |
1498 | if (tparam->specialization() || !tparam->isTemplateTypeParameter()) | |
1499 | { | |
1500 | /* The specialization can work as long as afterwards | |
1501 | * the oded == oarg | |
1502 | */ | |
1503 | (*dedargs)[i] = oded; | |
1504 | MATCH m2 = tparam->matchArg(instLoc, paramscope, dedargs, i, parameters, dedtypes, NULL); | |
1505 | //printf("m2 = %d\n", m2); | |
1506 | if (m2 <= MATCHnomatch) | |
1507 | goto Lnomatch; | |
1508 | if (m2 < matchTiargs) | |
1509 | matchTiargs = m2; // pick worst match | |
1510 | if (!(*dedtypes)[i]->equals(oded)) | |
1511 | error("specialization not allowed for deduced parameter %s", tparam->ident->toChars()); | |
1512 | } | |
1513 | else | |
1514 | { | |
1515 | if (MATCHconvert < matchTiargs) | |
1516 | matchTiargs = MATCHconvert; | |
1517 | } | |
1518 | (*dedargs)[i] = declareParameter(paramscope, tparam, oded); | |
1519 | } | |
1520 | else | |
1521 | { | |
1522 | oded = tparam->defaultArg(instLoc, paramscope); | |
1523 | if (oded) | |
1524 | (*dedargs)[i] = declareParameter(paramscope, tparam, oded); | |
1525 | } | |
1526 | } | |
1527 | } | |
1528 | } | |
1529 | nfargs2 = argi + 1; | |
1530 | ||
1531 | /* If prmtype does not depend on any template parameters: | |
1532 | * | |
1533 | * auto foo(T)(T v, double x = 0); | |
1534 | * foo("str"); | |
1535 | * // at fparam == 'double x = 0' | |
1536 | * | |
1537 | * or, if all template parameters in the prmtype are already deduced: | |
1538 | * | |
1539 | * auto foo(R)(R range, ElementType!R sum = 0); | |
1540 | * foo([1,2,3]); | |
1541 | * // at fparam == 'ElementType!R sum = 0' | |
1542 | * | |
1543 | * Deducing prmtype from fparam->defaultArg is not necessary. | |
1544 | */ | |
1545 | if (prmtype->deco || | |
1546 | prmtype->syntaxCopy()->trySemantic(loc, paramscope)) | |
1547 | { | |
1548 | ++argi; | |
1549 | continue; | |
1550 | } | |
1551 | ||
1552 | // Deduce prmtype from the defaultArg. | |
1553 | farg = fparam->defaultArg->syntaxCopy(); | |
1554 | farg = ::semantic(farg, paramscope); | |
1555 | farg = resolveProperties(paramscope, farg); | |
1556 | } | |
1557 | else | |
1558 | { | |
1559 | farg = (*fargs)[argi]; | |
1560 | } | |
1561 | { | |
1562 | // Check invalid arguments to detect errors early. | |
1563 | if (farg->op == TOKerror || farg->type->ty == Terror) | |
1564 | goto Lnomatch; | |
1565 | ||
1566 | Type *att = NULL; | |
1567 | Lretry: | |
1568 | Type *argtype = farg->type; | |
1569 | ||
1570 | if (!(fparam->storageClass & STClazy) && argtype->ty == Tvoid && farg->op != TOKfunction) | |
1571 | goto Lnomatch; | |
1572 | ||
1573 | // Bugzilla 12876: optimize arugument to allow CT-known length matching | |
1574 | farg = farg->optimize(WANTvalue, (fparam->storageClass & (STCref | STCout)) != 0); | |
1575 | //printf("farg = %s %s\n", farg->type->toChars(), farg->toChars()); | |
1576 | ||
1577 | RootObject *oarg = farg; | |
1578 | if ((fparam->storageClass & STCref) && | |
1579 | (!(fparam->storageClass & STCauto) || farg->isLvalue())) | |
1580 | { | |
1581 | /* Allow expressions that have CT-known boundaries and type [] to match with [dim] | |
1582 | */ | |
1583 | Type *taai; | |
1584 | if (argtype->ty == Tarray && | |
1585 | (prmtype->ty == Tsarray || | |
1586 | (prmtype->ty == Taarray && (taai = ((TypeAArray *)prmtype)->index)->ty == Tident && | |
1587 | ((TypeIdentifier *)taai)->idents.dim == 0))) | |
1588 | { | |
1589 | if (farg->op == TOKstring) | |
1590 | { | |
1591 | StringExp *se = (StringExp *)farg; | |
1592 | argtype = se->type->nextOf()->sarrayOf(se->len); | |
1593 | } | |
1594 | else if (farg->op == TOKarrayliteral) | |
1595 | { | |
1596 | ArrayLiteralExp *ae = (ArrayLiteralExp *)farg; | |
1597 | argtype = ae->type->nextOf()->sarrayOf(ae->elements->dim); | |
1598 | } | |
1599 | else if (farg->op == TOKslice) | |
1600 | { | |
1601 | SliceExp *se = (SliceExp *)farg; | |
1602 | if (Type *tsa = toStaticArrayType(se)) | |
1603 | argtype = tsa; | |
1604 | } | |
1605 | } | |
1606 | ||
1607 | oarg = argtype; | |
1608 | } | |
1609 | else if ((fparam->storageClass & STCout) == 0 && | |
1610 | (argtype->ty == Tarray || argtype->ty == Tpointer) && | |
1611 | templateParameterLookup(prmtype, parameters) != IDX_NOTFOUND && | |
1612 | ((TypeIdentifier *)prmtype)->idents.dim == 0) | |
1613 | { | |
1614 | /* The farg passing to the prmtype always make a copy. Therefore, | |
1615 | * we can shrink the set of the deduced type arguments for prmtype | |
1616 | * by adjusting top-qualifier of the argtype. | |
1617 | * | |
1618 | * prmtype argtype ta | |
1619 | * T <- const(E)[] const(E)[] | |
1620 | * T <- const(E[]) const(E)[] | |
1621 | * qualifier(T) <- const(E)[] const(E[]) | |
1622 | * qualifier(T) <- const(E[]) const(E[]) | |
1623 | */ | |
1624 | Type *ta = argtype->castMod(prmtype->mod ? argtype->nextOf()->mod : 0); | |
1625 | if (ta != argtype) | |
1626 | { | |
1627 | Expression *ea = farg->copy(); | |
1628 | ea->type = ta; | |
1629 | oarg = ea; | |
1630 | } | |
1631 | } | |
1632 | ||
1633 | if (fvarargs == 2 && parami + 1 == nfparams && argi + 1 < nfargs) | |
1634 | goto Lvarargs; | |
1635 | ||
1636 | unsigned wm = 0; | |
1637 | MATCH m = deduceType(oarg, paramscope, prmtype, parameters, dedtypes, &wm, inferStart); | |
1638 | //printf("\tL%d deduceType m = %d, wm = x%x, wildmatch = x%x\n", __LINE__, m, wm, wildmatch); | |
1639 | wildmatch |= wm; | |
1640 | ||
1641 | /* If no match, see if the argument can be matched by using | |
1642 | * implicit conversions. | |
1643 | */ | |
1644 | if (m == MATCHnomatch && prmtype->deco) | |
1645 | m = farg->implicitConvTo(prmtype); | |
1646 | ||
1647 | if (m == MATCHnomatch) | |
1648 | { | |
1649 | AggregateDeclaration *ad = isAggregate(farg->type); | |
1650 | if (ad && ad->aliasthis && argtype != att) | |
1651 | { | |
1652 | if (!att && argtype->checkAliasThisRec()) // Bugzilla 12537 | |
1653 | att = argtype; | |
1654 | ||
1655 | /* If a semantic error occurs while doing alias this, | |
1656 | * eg purity(bug 7295), just regard it as not a match. | |
1657 | */ | |
1658 | if (Expression *e = resolveAliasThis(sc, farg, true)) | |
1659 | { | |
1660 | farg = e; | |
1661 | goto Lretry; | |
1662 | } | |
1663 | } | |
1664 | } | |
1665 | ||
1666 | if (m > MATCHnomatch && (fparam->storageClass & (STCref | STCauto)) == STCref) | |
1667 | { | |
1668 | if (!farg->isLvalue()) | |
1669 | { | |
1670 | if ((farg->op == TOKstring || farg->op == TOKslice) && | |
1671 | (prmtype->ty == Tsarray || prmtype->ty == Taarray)) | |
1672 | { | |
1673 | // Allow conversion from T[lwr .. upr] to ref T[upr-lwr] | |
1674 | } | |
1675 | else | |
1676 | goto Lnomatch; | |
1677 | } | |
1678 | } | |
1679 | if (m > MATCHnomatch && (fparam->storageClass & STCout)) | |
1680 | { | |
1681 | if (!farg->isLvalue()) | |
1682 | goto Lnomatch; | |
1683 | if (!farg->type->isMutable()) // Bugzilla 11916 | |
1684 | goto Lnomatch; | |
1685 | } | |
1686 | if (m == MATCHnomatch && (fparam->storageClass & STClazy) && prmtype->ty == Tvoid && | |
1687 | farg->type->ty != Tvoid) | |
1688 | m = MATCHconvert; | |
1689 | ||
1690 | if (m != MATCHnomatch) | |
1691 | { | |
1692 | if (m < match) | |
1693 | match = m; // pick worst match | |
1694 | argi++; | |
1695 | continue; | |
1696 | } | |
1697 | } | |
1698 | ||
1699 | Lvarargs: | |
1700 | /* The following code for variadic arguments closely | |
1701 | * matches TypeFunction::callMatch() | |
1702 | */ | |
1703 | if (!(fvarargs == 2 && parami + 1 == nfparams)) | |
1704 | goto Lnomatch; | |
1705 | ||
1706 | /* Check for match with function parameter T... | |
1707 | */ | |
1708 | Type *tb = prmtype->toBasetype(); | |
1709 | switch (tb->ty) | |
1710 | { | |
1711 | // 6764 fix - TypeAArray may be TypeSArray have not yet run semantic(). | |
1712 | case Tsarray: | |
1713 | case Taarray: | |
1714 | // Perhaps we can do better with this, see TypeFunction::callMatch() | |
1715 | if (tb->ty == Tsarray) | |
1716 | { | |
1717 | TypeSArray *tsa = (TypeSArray *)tb; | |
1718 | dinteger_t sz = tsa->dim->toInteger(); | |
1719 | if (sz != nfargs - argi) | |
1720 | goto Lnomatch; | |
1721 | } | |
1722 | else if (tb->ty == Taarray) | |
1723 | { | |
1724 | TypeAArray *taa = (TypeAArray *)tb; | |
1725 | Expression *dim = new IntegerExp(instLoc, nfargs - argi, Type::tsize_t); | |
1726 | ||
1727 | size_t i = templateParameterLookup(taa->index, parameters); | |
1728 | if (i == IDX_NOTFOUND) | |
1729 | { | |
1730 | Expression *e; | |
1731 | Type *t; | |
1732 | Dsymbol *s; | |
1733 | Scope *sco; | |
1734 | ||
1735 | unsigned errors = global.startGagging(); | |
1736 | /* ref: https://issues.dlang.org/show_bug.cgi?id=11118 | |
1737 | * The parameter isn't part of the template | |
1738 | * ones, let's try to find it in the | |
1739 | * instantiation scope 'sc' and the one | |
1740 | * belonging to the template itself. */ | |
1741 | sco = sc; | |
1742 | taa->index->resolve(instLoc, sco, &e, &t, &s); | |
1743 | if (!e) | |
1744 | { | |
1745 | sco = paramscope; | |
1746 | taa->index->resolve(instLoc, sco, &e, &t, &s); | |
1747 | } | |
1748 | global.endGagging(errors); | |
1749 | ||
1750 | if (!e) | |
1751 | { | |
1752 | goto Lnomatch; | |
1753 | } | |
1754 | ||
1755 | e = e->ctfeInterpret(); | |
1756 | e = e->implicitCastTo(sco, Type::tsize_t); | |
1757 | e = e->optimize(WANTvalue); | |
1758 | if (!dim->equals(e)) | |
1759 | goto Lnomatch; | |
1760 | } | |
1761 | else | |
1762 | { | |
1763 | // This code matches code in TypeInstance::deduceType() | |
1764 | TemplateParameter *tprm = (*parameters)[i]; | |
1765 | TemplateValueParameter *tvp = tprm->isTemplateValueParameter(); | |
1766 | if (!tvp) | |
1767 | goto Lnomatch; | |
1768 | Expression *e = (Expression *)(*dedtypes)[i]; | |
1769 | if (e) | |
1770 | { | |
1771 | if (!dim->equals(e)) | |
1772 | goto Lnomatch; | |
1773 | } | |
1774 | else | |
1775 | { | |
1776 | Type *vt = tvp->valType->semantic(Loc(), sc); | |
1777 | MATCH m = (MATCH)dim->implicitConvTo(vt); | |
1778 | if (m <= MATCHnomatch) | |
1779 | goto Lnomatch; | |
1780 | (*dedtypes)[i] = dim; | |
1781 | } | |
1782 | } | |
1783 | } | |
1784 | /* fall through */ | |
1785 | case Tarray: | |
1786 | { | |
1787 | TypeArray *ta = (TypeArray *)tb; | |
1788 | Type *tret = fparam->isLazyArray(); | |
1789 | for (; argi < nfargs; argi++) | |
1790 | { | |
1791 | Expression *arg = (*fargs)[argi]; | |
1792 | assert(arg); | |
1793 | ||
1794 | MATCH m; | |
1795 | /* If lazy array of delegates, | |
1796 | * convert arg(s) to delegate(s) | |
1797 | */ | |
1798 | if (tret) | |
1799 | { | |
1800 | if (ta->next->equals(arg->type)) | |
1801 | { | |
1802 | m = MATCHexact; | |
1803 | } | |
1804 | else | |
1805 | { | |
1806 | m = arg->implicitConvTo(tret); | |
1807 | if (m == MATCHnomatch) | |
1808 | { | |
1809 | if (tret->toBasetype()->ty == Tvoid) | |
1810 | m = MATCHconvert; | |
1811 | } | |
1812 | } | |
1813 | } | |
1814 | else | |
1815 | { | |
1816 | unsigned wm = 0; | |
1817 | m = deduceType(arg, paramscope, ta->next, parameters, dedtypes, &wm, inferStart); | |
1818 | wildmatch |= wm; | |
1819 | } | |
1820 | if (m == MATCHnomatch) | |
1821 | goto Lnomatch; | |
1822 | if (m < match) | |
1823 | match = m; | |
1824 | } | |
1825 | goto Lmatch; | |
1826 | } | |
1827 | case Tclass: | |
1828 | case Tident: | |
1829 | goto Lmatch; | |
1830 | ||
1831 | default: | |
1832 | goto Lnomatch; | |
1833 | } | |
1834 | ++argi; | |
1835 | } | |
1836 | //printf("-> argi = %d, nfargs = %d, nfargs2 = %d\n", argi, nfargs, nfargs2); | |
1837 | if (argi != nfargs2 && !fvarargs) | |
1838 | goto Lnomatch; | |
1839 | } | |
1840 | ||
1841 | Lmatch: | |
1842 | ||
1843 | for (size_t i = 0; i < dedtypes->dim; i++) | |
1844 | { | |
1845 | Type *at = isType((*dedtypes)[i]); | |
1846 | if (at) | |
1847 | { | |
1848 | if (at->ty == Tnone) | |
1849 | { | |
1850 | TypeDeduced *xt = (TypeDeduced *)at; | |
1851 | at = xt->tded; // 'unbox' | |
1852 | delete xt; | |
1853 | } | |
1854 | (*dedtypes)[i] = at->merge2(); | |
1855 | } | |
1856 | } | |
1857 | for (size_t i = ntargs; i < dedargs->dim; i++) | |
1858 | { | |
1859 | TemplateParameter *tparam = (*parameters)[i]; | |
1860 | //printf("tparam[%d] = %s\n", i, tparam->ident->toChars()); | |
1861 | /* For T:T*, the dedargs is the T*, dedtypes is the T | |
1862 | * But for function templates, we really need them to match | |
1863 | */ | |
1864 | RootObject *oarg = (*dedargs)[i]; | |
1865 | RootObject *oded = (*dedtypes)[i]; | |
1866 | //printf("1dedargs[%d] = %p, dedtypes[%d] = %p\n", i, oarg, i, oded); | |
1867 | //if (oarg) printf("oarg: %s\n", oarg->toChars()); | |
1868 | //if (oded) printf("oded: %s\n", oded->toChars()); | |
1869 | if (!oarg) | |
1870 | { | |
1871 | if (oded) | |
1872 | { | |
1873 | if (tparam->specialization() || !tparam->isTemplateTypeParameter()) | |
1874 | { | |
1875 | /* The specialization can work as long as afterwards | |
1876 | * the oded == oarg | |
1877 | */ | |
1878 | (*dedargs)[i] = oded; | |
1879 | MATCH m2 = tparam->matchArg(instLoc, paramscope, dedargs, i, parameters, dedtypes, NULL); | |
1880 | //printf("m2 = %d\n", m2); | |
1881 | if (m2 <= MATCHnomatch) | |
1882 | goto Lnomatch; | |
1883 | if (m2 < matchTiargs) | |
1884 | matchTiargs = m2; // pick worst match | |
1885 | if (!(*dedtypes)[i]->equals(oded)) | |
1886 | error("specialization not allowed for deduced parameter %s", tparam->ident->toChars()); | |
1887 | } | |
1888 | else | |
1889 | { | |
1890 | if (MATCHconvert < matchTiargs) | |
1891 | matchTiargs = MATCHconvert; | |
1892 | } | |
1893 | } | |
1894 | else | |
1895 | { | |
1896 | oded = tparam->defaultArg(instLoc, paramscope); | |
1897 | if (!oded) | |
1898 | { | |
1899 | // if tuple parameter and | |
1900 | // tuple parameter was not in function parameter list and | |
1901 | // we're one or more arguments short (i.e. no tuple argument) | |
1902 | if (tparam == tp && | |
1903 | fptupindex == IDX_NOTFOUND && | |
1904 | ntargs <= dedargs->dim - 1) | |
1905 | { | |
1906 | // make tuple argument an empty tuple | |
1907 | oded = (RootObject *)new Tuple(); | |
1908 | } | |
1909 | else | |
1910 | goto Lnomatch; | |
1911 | } | |
1912 | if (isError(oded)) | |
1913 | goto Lerror; | |
1914 | ntargs++; | |
1915 | ||
1916 | /* At the template parameter T, the picked default template argument | |
1917 | * X!int should be matched to T in order to deduce dependent | |
1918 | * template parameter A. | |
1919 | * auto foo(T : X!A = X!int, A...)() { ... } | |
1920 | * foo(); // T <-- X!int, A <-- (int) | |
1921 | */ | |
1922 | if (tparam->specialization()) | |
1923 | { | |
1924 | (*dedargs)[i] = oded; | |
1925 | MATCH m2 = tparam->matchArg(instLoc, paramscope, dedargs, i, parameters, dedtypes, NULL); | |
1926 | //printf("m2 = %d\n", m2); | |
1927 | if (m2 <= MATCHnomatch) | |
1928 | goto Lnomatch; | |
1929 | if (m2 < matchTiargs) | |
1930 | matchTiargs = m2; // pick worst match | |
1931 | if (!(*dedtypes)[i]->equals(oded)) | |
1932 | error("specialization not allowed for deduced parameter %s", tparam->ident->toChars()); | |
1933 | } | |
1934 | } | |
1935 | oded = declareParameter(paramscope, tparam, oded); | |
1936 | (*dedargs)[i] = oded; | |
1937 | } | |
1938 | } | |
1939 | ||
1940 | /* Bugzilla 7469: As same as the code for 7469 in findBestMatch, | |
1941 | * expand a Tuple in dedargs to normalize template arguments. | |
1942 | */ | |
1943 | if (size_t d = dedargs->dim) | |
1944 | { | |
1945 | if (Tuple *va = isTuple((*dedargs)[d - 1])) | |
1946 | { | |
1947 | if (va->objects.dim) | |
1948 | { | |
1949 | dedargs->setDim(d - 1); | |
1950 | dedargs->insert(d - 1, &va->objects); | |
1951 | } | |
1952 | } | |
1953 | } | |
1954 | ti->tiargs = dedargs; // update to the normalized template arguments. | |
1955 | ||
1956 | // Partially instantiate function for constraint and fd->leastAsSpecialized() | |
1957 | { | |
1958 | assert(paramsym); | |
1959 | Scope *sc2 = _scope; | |
1960 | sc2 = sc2->push(paramsym); | |
1961 | sc2 = sc2->push(ti); | |
1962 | sc2->parent = ti; | |
1963 | sc2->tinst = ti; | |
1964 | sc2->minst = sc->minst; | |
1965 | ||
1966 | fd = doHeaderInstantiation(ti, sc2, fd, tthis, fargs); | |
1967 | ||
1968 | sc2 = sc2->pop(); | |
1969 | sc2 = sc2->pop(); | |
1970 | ||
1971 | if (!fd) | |
1972 | goto Lnomatch; | |
1973 | } | |
1974 | ||
1975 | if (constraint) | |
1976 | { | |
1977 | if (!evaluateConstraint(ti, sc, paramscope, dedargs, fd)) | |
1978 | goto Lnomatch; | |
1979 | } | |
1980 | ||
1981 | paramscope->pop(); | |
1982 | //printf("\tmatch %d\n", match); | |
1983 | return (MATCH)(match | (matchTiargs<<4)); | |
1984 | ||
1985 | Lnomatch: | |
1986 | paramscope->pop(); | |
1987 | //printf("\tnomatch\n"); | |
1988 | return MATCHnomatch; | |
1989 | ||
1990 | Lerror: // todo: for the future improvement | |
1991 | paramscope->pop(); | |
1992 | //printf("\terror\n"); | |
1993 | return MATCHnomatch; | |
1994 | } | |
1995 | ||
1996 | /************************************************** | |
1997 | * Declare template parameter tp with value o, and install it in the scope sc. | |
1998 | */ | |
1999 | ||
2000 | RootObject *TemplateDeclaration::declareParameter(Scope *sc, TemplateParameter *tp, RootObject *o) | |
2001 | { | |
2002 | //printf("TemplateDeclaration::declareParameter('%s', o = %p)\n", tp->ident->toChars(), o); | |
2003 | ||
2004 | Type *ta = isType(o); | |
2005 | Expression *ea = isExpression(o); | |
2006 | Dsymbol *sa = isDsymbol(o); | |
2007 | Tuple *va = isTuple(o); | |
2008 | ||
2009 | Declaration *d; | |
2010 | VarDeclaration *v = NULL; | |
2011 | ||
2012 | if (ea && ea->op == TOKtype) | |
2013 | ta = ea->type; | |
2014 | else if (ea && ea->op == TOKscope) | |
2015 | sa = ((ScopeExp *)ea)->sds; | |
2016 | else if (ea && (ea->op == TOKthis || ea->op == TOKsuper)) | |
2017 | sa = ((ThisExp *)ea)->var; | |
2018 | else if (ea && ea->op == TOKfunction) | |
2019 | { | |
2020 | if (((FuncExp *)ea)->td) | |
2021 | sa = ((FuncExp *)ea)->td; | |
2022 | else | |
2023 | sa = ((FuncExp *)ea)->fd; | |
2024 | } | |
2025 | ||
2026 | if (ta) | |
2027 | { | |
2028 | //printf("type %s\n", ta->toChars()); | |
2029 | d = new AliasDeclaration(Loc(), tp->ident, ta); | |
2030 | } | |
2031 | else if (sa) | |
2032 | { | |
2033 | //printf("Alias %s %s;\n", sa->ident->toChars(), tp->ident->toChars()); | |
2034 | d = new AliasDeclaration(Loc(), tp->ident, sa); | |
2035 | } | |
2036 | else if (ea) | |
2037 | { | |
2038 | // tdtypes.data[i] always matches ea here | |
2039 | Initializer *init = new ExpInitializer(loc, ea); | |
2040 | TemplateValueParameter *tvp = tp->isTemplateValueParameter(); | |
2041 | ||
2042 | Type *t = tvp ? tvp->valType : NULL; | |
2043 | ||
2044 | v = new VarDeclaration(loc, t, tp->ident, init); | |
2045 | v->storage_class = STCmanifest | STCtemplateparameter; | |
2046 | d = v; | |
2047 | } | |
2048 | else if (va) | |
2049 | { | |
2050 | //printf("\ttuple\n"); | |
2051 | d = new TupleDeclaration(loc, tp->ident, &va->objects); | |
2052 | } | |
2053 | else | |
2054 | { | |
2055 | assert(0); | |
2056 | } | |
2057 | ||
2058 | d->storage_class |= STCtemplateparameter; | |
2059 | if (ta) | |
2060 | { | |
2061 | Type *t = ta; | |
2062 | // consistent with Type::checkDeprecated() | |
2063 | while (t->ty != Tenum) | |
2064 | { | |
2065 | if (!t->nextOf()) break; | |
2066 | t = ((TypeNext *)t)->next; | |
2067 | } | |
2068 | if (Dsymbol *s = t->toDsymbol(sc)) | |
2069 | { | |
2070 | if (s->isDeprecated()) | |
2071 | d->storage_class |= STCdeprecated; | |
2072 | } | |
2073 | } | |
2074 | else if (sa) | |
2075 | { | |
2076 | if (sa->isDeprecated()) | |
2077 | d->storage_class |= STCdeprecated; | |
2078 | } | |
2079 | ||
2080 | if (!sc->insert(d)) | |
2081 | error("declaration %s is already defined", tp->ident->toChars()); | |
2082 | d->semantic(sc); | |
2083 | ||
2084 | /* So the caller's o gets updated with the result of semantic() being run on o | |
2085 | */ | |
2086 | if (v) | |
2087 | o = initializerToExpression(v->_init); | |
2088 | return o; | |
2089 | } | |
2090 | ||
2091 | /************************************** | |
2092 | * Determine if TemplateDeclaration is variadic. | |
2093 | */ | |
2094 | ||
2095 | TemplateTupleParameter *isVariadic(TemplateParameters *parameters) | |
2096 | { | |
2097 | size_t dim = parameters->dim; | |
2098 | TemplateTupleParameter *tp = NULL; | |
2099 | ||
2100 | if (dim) | |
2101 | tp = ((*parameters)[dim - 1])->isTemplateTupleParameter(); | |
2102 | return tp; | |
2103 | } | |
2104 | ||
2105 | TemplateTupleParameter *TemplateDeclaration::isVariadic() | |
2106 | { | |
2107 | return ::isVariadic(parameters); | |
2108 | } | |
2109 | ||
2110 | /*********************************** | |
2111 | * We can overload templates. | |
2112 | */ | |
2113 | ||
2114 | bool TemplateDeclaration::isOverloadable() | |
2115 | { | |
2116 | return true; | |
2117 | } | |
2118 | ||
2119 | /************************************************* | |
2120 | * Given function arguments, figure out which template function | |
2121 | * to expand, and return matching result. | |
2122 | * Input: | |
2123 | * m matching result | |
2124 | * dstart the root of overloaded function templates | |
2125 | * loc instantiation location | |
2126 | * sc instantiation scope | |
2127 | * tiargs initial list of template arguments | |
2128 | * tthis if !NULL, the 'this' pointer argument | |
2129 | * fargs arguments to function | |
2130 | */ | |
2131 | ||
2132 | void functionResolve(Match *m, Dsymbol *dstart, Loc loc, Scope *sc, | |
2133 | Objects *tiargs, Type *tthis, Expressions *fargs) | |
2134 | { | |
2135 | struct ParamDeduce | |
2136 | { | |
2137 | // context | |
2138 | Loc loc; | |
2139 | Scope *sc; | |
2140 | Type *tthis; | |
2141 | Objects *tiargs; | |
2142 | Expressions *fargs; | |
2143 | // result | |
2144 | Match *m; | |
2145 | int property; // 0: unintialized | |
2146 | // 1: seen @property | |
2147 | // 2: not @property | |
2148 | size_t ov_index; | |
2149 | TemplateDeclaration *td_best; | |
2150 | TemplateInstance *ti_best; | |
2151 | MATCH ta_last; | |
2152 | Type *tthis_best; | |
2153 | ||
2154 | static int fp(void *param, Dsymbol *s) | |
2155 | { | |
2156 | if (s->errors) | |
2157 | return 0; | |
2158 | if (FuncDeclaration *fd = s->isFuncDeclaration()) | |
2159 | return ((ParamDeduce *)param)->applyFunction(fd); | |
2160 | if (TemplateDeclaration *td = s->isTemplateDeclaration()) | |
2161 | return ((ParamDeduce *)param)->applyTemplate(td); | |
2162 | return 0; | |
2163 | } | |
2164 | ||
2165 | int applyFunction(FuncDeclaration *fd) | |
2166 | { | |
2167 | // skip duplicates | |
2168 | if (fd == m->lastf) | |
2169 | return 0; | |
2170 | // explicitly specified tiargs never match to non template function | |
2171 | if (tiargs && tiargs->dim > 0) | |
2172 | return 0; | |
2173 | ||
2174 | if (fd->semanticRun == PASSinit && fd->_scope) | |
2175 | { | |
2176 | Ungag ungag = fd->ungagSpeculative(); | |
2177 | fd->semantic(fd->_scope); | |
2178 | } | |
2179 | if (fd->semanticRun == PASSinit) | |
2180 | { | |
2181 | ::error(loc, "forward reference to template %s", fd->toChars()); | |
2182 | return 1; | |
2183 | } | |
2184 | //printf("fd = %s %s, fargs = %s\n", fd->toChars(), fd->type->toChars(), fargs->toChars()); | |
2185 | m->anyf = fd; | |
2186 | TypeFunction *tf = (TypeFunction *)fd->type; | |
2187 | ||
2188 | int prop = (tf->isproperty) ? 1 : 2; | |
2189 | if (property == 0) | |
2190 | property = prop; | |
2191 | else if (property != prop) | |
2192 | error(fd->loc, "cannot overload both property and non-property functions"); | |
2193 | ||
2194 | /* For constructors, qualifier check will be opposite direction. | |
2195 | * Qualified constructor always makes qualified object, then will be checked | |
2196 | * that it is implicitly convertible to tthis. | |
2197 | */ | |
2198 | Type *tthis_fd = fd->needThis() ? tthis : NULL; | |
2199 | bool isCtorCall = tthis_fd && fd->isCtorDeclaration(); | |
2200 | if (isCtorCall) | |
2201 | { | |
2202 | //printf("%s tf->mod = x%x tthis_fd->mod = x%x %d\n", tf->toChars(), | |
2203 | // tf->mod, tthis_fd->mod, fd->isolateReturn()); | |
2204 | if (MODimplicitConv(tf->mod, tthis_fd->mod) || | |
2205 | (tf->isWild() && tf->isShared() == tthis_fd->isShared()) || | |
2206 | fd->isolateReturn()) | |
2207 | { | |
2208 | /* && tf->isShared() == tthis_fd->isShared()*/ | |
2209 | // Uniquely constructed object can ignore shared qualifier. | |
2210 | // TODO: Is this appropriate? | |
2211 | tthis_fd = NULL; | |
2212 | } | |
2213 | else | |
2214 | return 0; // MATCHnomatch | |
2215 | } | |
2216 | MATCH mfa = tf->callMatch(tthis_fd, fargs); | |
2217 | //printf("test1: mfa = %d\n", mfa); | |
2218 | if (mfa > MATCHnomatch) | |
2219 | { | |
2220 | if (mfa > m->last) goto LfIsBetter; | |
2221 | if (mfa < m->last) goto LlastIsBetter; | |
2222 | ||
2223 | /* See if one of the matches overrides the other. | |
2224 | */ | |
2225 | assert(m->lastf); | |
2226 | if (m->lastf->overrides(fd)) goto LlastIsBetter; | |
2227 | if (fd->overrides(m->lastf)) goto LfIsBetter; | |
2228 | ||
2229 | /* Try to disambiguate using template-style partial ordering rules. | |
2230 | * In essence, if f() and g() are ambiguous, if f() can call g(), | |
2231 | * but g() cannot call f(), then pick f(). | |
2232 | * This is because f() is "more specialized." | |
2233 | */ | |
2234 | { | |
2235 | MATCH c1 = fd->leastAsSpecialized(m->lastf); | |
2236 | MATCH c2 = m->lastf->leastAsSpecialized(fd); | |
2237 | //printf("c1 = %d, c2 = %d\n", c1, c2); | |
2238 | if (c1 > c2) goto LfIsBetter; | |
2239 | if (c1 < c2) goto LlastIsBetter; | |
2240 | } | |
2241 | ||
2242 | /* The 'overrides' check above does covariant checking only | |
2243 | * for virtual member functions. It should do it for all functions, | |
2244 | * but in order to not risk breaking code we put it after | |
2245 | * the 'leastAsSpecialized' check. | |
2246 | * In the future try moving it before. | |
2247 | * I.e. a not-the-same-but-covariant match is preferred, | |
2248 | * as it is more restrictive. | |
2249 | */ | |
2250 | if (!m->lastf->type->equals(fd->type)) | |
2251 | { | |
2252 | //printf("cov: %d %d\n", m->lastf->type->covariant(fd->type), fd->type->covariant(m->lastf->type)); | |
2253 | if (m->lastf->type->covariant(fd->type) == 1) goto LlastIsBetter; | |
2254 | if (fd->type->covariant(m->lastf->type) == 1) goto LfIsBetter; | |
2255 | } | |
2256 | ||
2257 | /* If the two functions are the same function, like: | |
2258 | * int foo(int); | |
2259 | * int foo(int x) { ... } | |
2260 | * then pick the one with the body. | |
2261 | */ | |
2262 | if (tf->equals(m->lastf->type) && | |
2263 | fd->storage_class == m->lastf->storage_class && | |
2264 | fd->parent == m->lastf->parent && | |
2265 | fd->protection == m->lastf->protection && | |
2266 | fd->linkage == m->lastf->linkage) | |
2267 | { | |
2268 | if ( fd->fbody && !m->lastf->fbody) goto LfIsBetter; | |
2269 | if (!fd->fbody && m->lastf->fbody) goto LlastIsBetter; | |
2270 | } | |
2271 | ||
2272 | // Bugzilla 14450: Prefer exact qualified constructor for the creating object type | |
2273 | if (isCtorCall && tf->mod != m->lastf->type->mod) | |
2274 | { | |
2275 | if (tthis->mod == tf->mod) goto LfIsBetter; | |
2276 | if (tthis->mod == m->lastf->type->mod) goto LlastIsBetter; | |
2277 | } | |
2278 | ||
2279 | m->nextf = fd; | |
2280 | m->count++; | |
2281 | return 0; | |
2282 | ||
2283 | LlastIsBetter: | |
2284 | return 0; | |
2285 | ||
2286 | LfIsBetter: | |
2287 | td_best = NULL; | |
2288 | ti_best = NULL; | |
2289 | ta_last = MATCHexact; | |
2290 | m->last = mfa; | |
2291 | m->lastf = fd; | |
2292 | tthis_best = tthis_fd; | |
2293 | ov_index = 0; | |
2294 | m->count = 1; | |
2295 | return 0; | |
2296 | } | |
2297 | return 0; | |
2298 | } | |
2299 | ||
2300 | int applyTemplate(TemplateDeclaration *td) | |
2301 | { | |
2302 | //printf("applyTemplate()\n"); | |
2303 | // skip duplicates | |
2304 | if (td == td_best) | |
2305 | return 0; | |
2306 | ||
2307 | if (!sc) | |
2308 | sc = td->_scope; // workaround for Type::aliasthisOf | |
2309 | ||
2310 | if (td->semanticRun == PASSinit && td->_scope) | |
2311 | { | |
2312 | // Try to fix forward reference. Ungag errors while doing so. | |
2313 | Ungag ungag = td->ungagSpeculative(); | |
2314 | td->semantic(td->_scope); | |
2315 | } | |
2316 | if (td->semanticRun == PASSinit) | |
2317 | { | |
2318 | ::error(loc, "forward reference to template %s", td->toChars()); | |
2319 | Lerror: | |
2320 | m->lastf = NULL; | |
2321 | m->count = 0; | |
2322 | m->last = MATCHnomatch; | |
2323 | return 1; | |
2324 | } | |
2325 | //printf("td = %s\n", td->toChars()); | |
2326 | ||
2327 | FuncDeclaration *f; | |
2328 | f = td->onemember ? td->onemember->isFuncDeclaration() : NULL; | |
2329 | if (!f) | |
2330 | { | |
2331 | if (!tiargs) | |
2332 | tiargs = new Objects(); | |
2333 | TemplateInstance *ti = new TemplateInstance(loc, td, tiargs); | |
2334 | Objects dedtypes; | |
2335 | dedtypes.setDim(td->parameters->dim); | |
2336 | assert(td->semanticRun != PASSinit); | |
2337 | MATCH mta = td->matchWithInstance(sc, ti, &dedtypes, fargs, 0); | |
2338 | //printf("matchWithInstance = %d\n", mta); | |
2339 | if (mta <= MATCHnomatch || mta < ta_last) // no match or less match | |
2340 | return 0; | |
2341 | ||
2342 | ti->semantic(sc, fargs); | |
2343 | if (!ti->inst) // if template failed to expand | |
2344 | return 0; | |
2345 | ||
2346 | Dsymbol *s = ti->inst->toAlias(); | |
2347 | FuncDeclaration *fd; | |
2348 | if (TemplateDeclaration *tdx = s->isTemplateDeclaration()) | |
2349 | { | |
2350 | Objects dedtypesX; // empty tiargs | |
2351 | ||
2352 | // Bugzilla 11553: Check for recursive instantiation of tdx. | |
2353 | for (TemplatePrevious *p = tdx->previous; p; p = p->prev) | |
2354 | { | |
2355 | if (arrayObjectMatch(p->dedargs, &dedtypesX)) | |
2356 | { | |
2357 | //printf("recursive, no match p->sc=%p %p %s\n", p->sc, this, this->toChars()); | |
2358 | /* It must be a subscope of p->sc, other scope chains are not recursive | |
2359 | * instantiations. | |
2360 | */ | |
2361 | for (Scope *scx = sc; scx; scx = scx->enclosing) | |
2362 | { | |
2363 | if (scx == p->sc) | |
2364 | { | |
2365 | error(loc, "recursive template expansion while looking for %s.%s", ti->toChars(), tdx->toChars()); | |
2366 | goto Lerror; | |
2367 | } | |
2368 | } | |
2369 | } | |
2370 | /* BUG: should also check for ref param differences | |
2371 | */ | |
2372 | } | |
2373 | ||
2374 | TemplatePrevious pr; | |
2375 | pr.prev = tdx->previous; | |
2376 | pr.sc = sc; | |
2377 | pr.dedargs = &dedtypesX; | |
2378 | tdx->previous = ≺ // add this to threaded list | |
2379 | ||
2380 | fd = resolveFuncCall(loc, sc, s, NULL, tthis, fargs, 1); | |
2381 | ||
2382 | tdx->previous = pr.prev; // unlink from threaded list | |
2383 | } | |
2384 | else if (s->isFuncDeclaration()) | |
2385 | { | |
2386 | fd = resolveFuncCall(loc, sc, s, NULL, tthis, fargs, 1); | |
2387 | } | |
2388 | else | |
2389 | goto Lerror; | |
2390 | ||
2391 | if (!fd) | |
2392 | return 0; | |
2393 | ||
2394 | if (fd->type->ty != Tfunction) | |
2395 | { | |
2396 | m->lastf = fd; // to propagate "error match" | |
2397 | m->count = 1; | |
2398 | m->last = MATCHnomatch; | |
2399 | return 1; | |
2400 | } | |
2401 | ||
2402 | Type *tthis_fd = fd->needThis() && !fd->isCtorDeclaration() ? tthis : NULL; | |
2403 | ||
2404 | TypeFunction *tf = (TypeFunction *)fd->type; | |
2405 | MATCH mfa = tf->callMatch(tthis_fd, fargs); | |
2406 | if (mfa < m->last) | |
2407 | return 0; | |
2408 | ||
2409 | if (mta < ta_last) goto Ltd_best2; | |
2410 | if (mta > ta_last) goto Ltd2; | |
2411 | ||
2412 | if (mfa < m->last) goto Ltd_best2; | |
2413 | if (mfa > m->last) goto Ltd2; | |
2414 | ||
2415 | //printf("Lambig2\n"); | |
2416 | m->nextf = fd; | |
2417 | m->count++; | |
2418 | return 0; | |
2419 | ||
2420 | Ltd_best2: | |
2421 | return 0; | |
2422 | ||
2423 | Ltd2: | |
2424 | // td is the new best match | |
2425 | assert(td->_scope); | |
2426 | td_best = td; | |
2427 | ti_best = NULL; | |
2428 | property = 0; // (backward compatibility) | |
2429 | ta_last = mta; | |
2430 | m->last = mfa; | |
2431 | m->lastf = fd; | |
2432 | tthis_best = tthis_fd; | |
2433 | ov_index = 0; | |
2434 | m->nextf = NULL; | |
2435 | m->count = 1; | |
2436 | return 0; | |
2437 | } | |
2438 | ||
2439 | //printf("td = %s\n", td->toChars()); | |
2440 | for (size_t ovi = 0; f; f = f->overnext0, ovi++) | |
2441 | { | |
2442 | if (f->type->ty != Tfunction || f->errors) | |
2443 | goto Lerror; | |
2444 | ||
2445 | /* This is a 'dummy' instance to evaluate constraint properly. | |
2446 | */ | |
2447 | TemplateInstance *ti = new TemplateInstance(loc, td, tiargs); | |
2448 | ti->parent = td->parent; // Maybe calculating valid 'enclosing' is unnecessary. | |
2449 | ||
2450 | FuncDeclaration *fd = f; | |
2451 | int x = td->deduceFunctionTemplateMatch(ti, sc, fd, tthis, fargs); | |
2452 | MATCH mta = (MATCH)(x >> 4); | |
2453 | MATCH mfa = (MATCH)(x & 0xF); | |
2454 | //printf("match:t/f = %d/%d\n", mta, mfa); | |
2455 | if (!fd || mfa == MATCHnomatch) | |
2456 | continue; | |
2457 | ||
2458 | Type *tthis_fd = fd->needThis() ? tthis : NULL; | |
2459 | ||
2460 | bool isCtorCall = tthis_fd && fd->isCtorDeclaration(); | |
2461 | if (isCtorCall) | |
2462 | { | |
2463 | // Constructor call requires additional check. | |
2464 | ||
2465 | TypeFunction *tf = (TypeFunction *)fd->type; | |
2466 | assert(tf->next); | |
2467 | if (MODimplicitConv(tf->mod, tthis_fd->mod) || | |
2468 | (tf->isWild() && tf->isShared() == tthis_fd->isShared()) || | |
2469 | fd->isolateReturn()) | |
2470 | { | |
2471 | tthis_fd = NULL; | |
2472 | } | |
2473 | else | |
2474 | continue; // MATCHnomatch | |
2475 | } | |
2476 | ||
2477 | if (mta < ta_last) goto Ltd_best; | |
2478 | if (mta > ta_last) goto Ltd; | |
2479 | ||
2480 | if (mfa < m->last) goto Ltd_best; | |
2481 | if (mfa > m->last) goto Ltd; | |
2482 | ||
2483 | if (td_best) | |
2484 | { | |
2485 | // Disambiguate by picking the most specialized TemplateDeclaration | |
2486 | MATCH c1 = td->leastAsSpecialized(sc, td_best, fargs); | |
2487 | MATCH c2 = td_best->leastAsSpecialized(sc, td, fargs); | |
2488 | //printf("1: c1 = %d, c2 = %d\n", c1, c2); | |
2489 | if (c1 > c2) goto Ltd; | |
2490 | if (c1 < c2) goto Ltd_best; | |
2491 | } | |
2492 | assert(fd && m->lastf); | |
2493 | { | |
2494 | // Disambiguate by tf->callMatch | |
2495 | TypeFunction *tf1 = (TypeFunction *)fd->type; | |
2496 | assert(tf1->ty == Tfunction); | |
2497 | TypeFunction *tf2 = (TypeFunction *)m->lastf->type; | |
2498 | assert(tf2->ty == Tfunction); | |
2499 | MATCH c1 = tf1->callMatch(tthis_fd, fargs); | |
2500 | MATCH c2 = tf2->callMatch(tthis_best, fargs); | |
2501 | //printf("2: c1 = %d, c2 = %d\n", c1, c2); | |
2502 | if (c1 > c2) goto Ltd; | |
2503 | if (c1 < c2) goto Ltd_best; | |
2504 | } | |
2505 | { | |
2506 | // Disambiguate by picking the most specialized FunctionDeclaration | |
2507 | MATCH c1 = fd->leastAsSpecialized(m->lastf); | |
2508 | MATCH c2 = m->lastf->leastAsSpecialized(fd); | |
2509 | //printf("3: c1 = %d, c2 = %d\n", c1, c2); | |
2510 | if (c1 > c2) goto Ltd; | |
2511 | if (c1 < c2) goto Ltd_best; | |
2512 | } | |
2513 | ||
2514 | // Bugzilla 14450: Prefer exact qualified constructor for the creating object type | |
2515 | if (isCtorCall && fd->type->mod != m->lastf->type->mod) | |
2516 | { | |
2517 | if (tthis->mod == fd->type->mod) goto Ltd; | |
2518 | if (tthis->mod == m->lastf->type->mod) goto Ltd_best; | |
2519 | } | |
2520 | ||
2521 | m->nextf = fd; | |
2522 | m->count++; | |
2523 | continue; | |
2524 | ||
2525 | Ltd_best: // td_best is the best match so far | |
2526 | //printf("Ltd_best\n"); | |
2527 | continue; | |
2528 | ||
2529 | Ltd: // td is the new best match | |
2530 | //printf("Ltd\n"); | |
2531 | assert(td->_scope); | |
2532 | td_best = td; | |
2533 | ti_best = ti; | |
2534 | property = 0; // (backward compatibility) | |
2535 | ta_last = mta; | |
2536 | m->last = mfa; | |
2537 | m->lastf = fd; | |
2538 | tthis_best = tthis_fd; | |
2539 | ov_index = ovi; | |
2540 | m->nextf = NULL; | |
2541 | m->count = 1; | |
2542 | continue; | |
2543 | } | |
2544 | return 0; | |
2545 | } | |
2546 | }; | |
2547 | ParamDeduce p; | |
2548 | // context | |
2549 | p.loc = loc; | |
2550 | p.sc = sc; | |
2551 | p.tthis = tthis; | |
2552 | p.tiargs = tiargs; | |
2553 | p.fargs = fargs; | |
2554 | ||
2555 | // result | |
2556 | p.m = m; | |
2557 | p.property = 0; | |
2558 | p.ov_index = 0; | |
2559 | p.td_best = NULL; | |
2560 | p.ti_best = NULL; | |
2561 | p.ta_last = m->last != MATCHnomatch ? MATCHexact : MATCHnomatch; | |
2562 | p.tthis_best = NULL; | |
2563 | ||
2564 | TemplateDeclaration *td = dstart->isTemplateDeclaration(); | |
2565 | if (td && td->funcroot) | |
2566 | dstart = td->funcroot; | |
2567 | ||
2568 | overloadApply(dstart, &p, &ParamDeduce::fp); | |
2569 | ||
2570 | //printf("td_best = %p, m->lastf = %p\n", p.td_best, m->lastf); | |
2571 | if (p.td_best && p.ti_best && m->count == 1) | |
2572 | { | |
2573 | // Matches to template function | |
2574 | assert(p.td_best->onemember && p.td_best->onemember->isFuncDeclaration()); | |
2575 | ||
2576 | /* The best match is td_best with arguments tdargs. | |
2577 | * Now instantiate the template. | |
2578 | */ | |
2579 | assert(p.td_best->_scope); | |
2580 | if (!sc) | |
2581 | sc = p.td_best->_scope; // workaround for Type::aliasthisOf | |
2582 | ||
2583 | TemplateInstance *ti = new TemplateInstance(loc, p.td_best, p.ti_best->tiargs); | |
2584 | ti->semantic(sc, fargs); | |
2585 | ||
2586 | m->lastf = ti->toAlias()->isFuncDeclaration(); | |
2587 | if (!m->lastf) | |
2588 | goto Lnomatch; | |
2589 | if (ti->errors) | |
2590 | { | |
2591 | Lerror: | |
2592 | m->count = 1; | |
2593 | assert(m->lastf); | |
2594 | m->last = MATCHnomatch; | |
2595 | return; | |
2596 | } | |
2597 | ||
2598 | // look forward instantiated overload function | |
2599 | // Dsymbol::oneMembers is alredy called in TemplateInstance::semantic. | |
2600 | // it has filled overnext0d | |
2601 | while (p.ov_index--) | |
2602 | { | |
2603 | m->lastf = m->lastf->overnext0; | |
2604 | assert(m->lastf); | |
2605 | } | |
2606 | ||
2607 | p.tthis_best = m->lastf->needThis() && !m->lastf->isCtorDeclaration() ? tthis : NULL; | |
2608 | ||
2609 | TypeFunction *tf = (TypeFunction *)m->lastf->type; | |
2610 | if (tf->ty == Terror) | |
2611 | goto Lerror; | |
2612 | assert(tf->ty == Tfunction); | |
2613 | if (!tf->callMatch(p.tthis_best, fargs)) | |
2614 | goto Lnomatch; | |
2615 | ||
2616 | /* As Bugzilla 3682 shows, a template instance can be matched while instantiating | |
2617 | * that same template. Thus, the function type can be incomplete. Complete it. | |
2618 | * | |
2619 | * Bugzilla 9208: For auto function, completion should be deferred to the end of | |
2620 | * its semantic3. Should not complete it in here. | |
2621 | */ | |
2622 | if (tf->next && !m->lastf->inferRetType) | |
2623 | { | |
2624 | m->lastf->type = tf->semantic(loc, sc); | |
2625 | } | |
2626 | } | |
2627 | else if (m->lastf) | |
2628 | { | |
2629 | // Matches to non template function, | |
2630 | // or found matches were ambiguous. | |
2631 | assert(m->count >= 1); | |
2632 | } | |
2633 | else | |
2634 | { | |
2635 | Lnomatch: | |
2636 | m->count = 0; | |
2637 | m->lastf = NULL; | |
2638 | m->last = MATCHnomatch; | |
2639 | } | |
2640 | } | |
2641 | ||
2642 | /************************************************* | |
2643 | * Limited function template instantiation for using fd->leastAsSpecialized() | |
2644 | */ | |
2645 | FuncDeclaration *TemplateDeclaration::doHeaderInstantiation( | |
2646 | TemplateInstance *ti, Scope *sc2, | |
2647 | FuncDeclaration *fd, Type *tthis, Expressions *fargs) | |
2648 | { | |
2649 | assert(fd); | |
2650 | ||
2651 | // function body and contracts are not need | |
2652 | if (fd->isCtorDeclaration()) | |
2653 | fd = new CtorDeclaration(fd->loc, fd->endloc, fd->storage_class, fd->type->syntaxCopy()); | |
2654 | else | |
2655 | fd = new FuncDeclaration(fd->loc, fd->endloc, fd->ident, fd->storage_class, fd->type->syntaxCopy()); | |
2656 | fd->parent = ti; | |
2657 | ||
2658 | assert(fd->type->ty == Tfunction); | |
2659 | TypeFunction *tf = (TypeFunction *)fd->type; | |
2660 | tf->fargs = fargs; | |
2661 | ||
2662 | if (tthis) | |
2663 | { | |
2664 | // Match 'tthis' to any TemplateThisParameter's | |
2665 | bool hasttp = false; | |
2666 | for (size_t i = 0; i < parameters->dim; i++) | |
2667 | { | |
2668 | TemplateParameter *tp = (*parameters)[i]; | |
2669 | TemplateThisParameter *ttp = tp->isTemplateThisParameter(); | |
2670 | if (ttp) | |
2671 | hasttp = true; | |
2672 | } | |
2673 | if (hasttp) | |
2674 | { | |
2675 | tf = (TypeFunction *)tf->addSTC(ModToStc(tthis->mod)); | |
2676 | assert(!tf->deco); | |
2677 | } | |
2678 | } | |
2679 | ||
2680 | Scope *scx = sc2->push(); | |
2681 | ||
2682 | // Shouldn't run semantic on default arguments and return type. | |
2683 | for (size_t i = 0; i < tf->parameters->dim; i++) | |
2684 | (*tf->parameters)[i]->defaultArg = NULL; | |
2685 | if (fd->isCtorDeclaration()) | |
2686 | { | |
2687 | // For constructors, emitting return type is necessary for | |
2688 | // isolateReturn() in functionResolve. | |
2689 | scx->flags |= SCOPEctor; | |
2690 | ||
2691 | Dsymbol *parent = toParent2(); | |
2692 | Type *tret; | |
2693 | AggregateDeclaration *ad = parent->isAggregateDeclaration(); | |
2694 | if (!ad || parent->isUnionDeclaration()) | |
2695 | { | |
2696 | tret = Type::tvoid; | |
2697 | } | |
2698 | else | |
2699 | { | |
2700 | tret = ad->handleType(); | |
2701 | assert(tret); | |
2702 | tret = tret->addStorageClass(fd->storage_class | scx->stc); | |
2703 | tret = tret->addMod(tf->mod); | |
2704 | } | |
2705 | tf->next = tret; | |
2706 | if (ad && ad->isStructDeclaration()) | |
2707 | tf->isref = 1; | |
2708 | //printf("tf = %s\n", tf->toChars()); | |
2709 | } | |
2710 | else | |
2711 | tf->next = NULL; | |
2712 | fd->type = tf; | |
2713 | fd->type = fd->type->addSTC(scx->stc); | |
2714 | fd->type = fd->type->semantic(fd->loc, scx); | |
2715 | scx = scx->pop(); | |
2716 | ||
2717 | if (fd->type->ty != Tfunction) | |
2718 | return NULL; | |
2719 | ||
2720 | fd->originalType = fd->type; // for mangling | |
2721 | //printf("\t[%s] fd->type = %s, mod = %x, ", loc.toChars(), fd->type->toChars(), fd->type->mod); | |
2722 | //printf("fd->needThis() = %d\n", fd->needThis()); | |
2723 | ||
2724 | return fd; | |
2725 | } | |
2726 | ||
2727 | bool TemplateDeclaration::hasStaticCtorOrDtor() | |
2728 | { | |
2729 | return false; // don't scan uninstantiated templates | |
2730 | } | |
2731 | ||
2732 | const char *TemplateDeclaration::toChars() | |
2733 | { | |
2734 | if (literal) | |
2735 | return Dsymbol::toChars(); | |
2736 | ||
2737 | OutBuffer buf; | |
2738 | HdrGenState hgs; | |
2739 | ||
2740 | buf.writestring(ident->toChars()); | |
2741 | buf.writeByte('('); | |
2742 | for (size_t i = 0; i < parameters->dim; i++) | |
2743 | { | |
2744 | TemplateParameter *tp = (*parameters)[i]; | |
2745 | if (i) | |
2746 | buf.writestring(", "); | |
2747 | ::toCBuffer(tp, &buf, &hgs); | |
2748 | } | |
2749 | buf.writeByte(')'); | |
2750 | ||
2751 | if (onemember) | |
2752 | { | |
2753 | FuncDeclaration *fd = onemember->isFuncDeclaration(); | |
2754 | if (fd && fd->type) | |
2755 | { | |
2756 | TypeFunction *tf = (TypeFunction *)fd->type; | |
2757 | buf.writestring(parametersTypeToChars(tf->parameters, tf->varargs)); | |
2758 | } | |
2759 | } | |
2760 | ||
2761 | if (constraint) | |
2762 | { | |
2763 | buf.writestring(" if ("); | |
2764 | ::toCBuffer(constraint, &buf, &hgs); | |
2765 | buf.writeByte(')'); | |
2766 | } | |
2767 | return buf.extractString(); | |
2768 | } | |
2769 | ||
2770 | Prot TemplateDeclaration::prot() | |
2771 | { | |
2772 | return protection; | |
2773 | } | |
2774 | ||
2775 | /**************************************************** | |
2776 | * Given a new instance tithis of this TemplateDeclaration, | |
2777 | * see if there already exists an instance. | |
2778 | * If so, return that existing instance. | |
2779 | */ | |
2780 | ||
2781 | TemplateInstance *TemplateDeclaration::findExistingInstance(TemplateInstance *tithis, Expressions *fargs) | |
2782 | { | |
2783 | //printf("findExistingInstance(%p)\n", tithis); | |
2784 | tithis->fargs = fargs; | |
2785 | TemplateInstances *tinstances = (TemplateInstances *)dmd_aaGetRvalue((AA *)instances, (void *)tithis->toHash()); | |
2786 | if (tinstances) | |
2787 | { | |
2788 | for (size_t i = 0; i < tinstances->dim; i++) | |
2789 | { | |
2790 | TemplateInstance *ti = (*tinstances)[i]; | |
2791 | if (tithis->compare(ti) == 0) | |
2792 | return ti; | |
2793 | } | |
2794 | } | |
2795 | return NULL; | |
2796 | } | |
2797 | ||
2798 | /******************************************** | |
2799 | * Add instance ti to TemplateDeclaration's table of instances. | |
2800 | * Return a handle we can use to later remove it if it fails instantiation. | |
2801 | */ | |
2802 | ||
2803 | TemplateInstance *TemplateDeclaration::addInstance(TemplateInstance *ti) | |
2804 | { | |
2805 | //printf("addInstance() %p %p\n", instances, ti); | |
2806 | TemplateInstances **ptinstances = (TemplateInstances **)dmd_aaGet((AA **)&instances, (void *)ti->toHash()); | |
2807 | if (!*ptinstances) | |
2808 | *ptinstances = new TemplateInstances(); | |
2809 | (*ptinstances)->push(ti); | |
2810 | return ti; | |
2811 | } | |
2812 | ||
2813 | /******************************************* | |
2814 | * Remove TemplateInstance from table of instances. | |
2815 | * Input: | |
2816 | * handle returned by addInstance() | |
2817 | */ | |
2818 | ||
2819 | void TemplateDeclaration::removeInstance(TemplateInstance *handle) | |
2820 | { | |
2821 | //printf("removeInstance()\n"); | |
2822 | TemplateInstances *tinstances = (TemplateInstances *)dmd_aaGetRvalue((AA *)instances, (void *)handle->toHash()); | |
2823 | if (tinstances) | |
2824 | { | |
2825 | for (size_t i = 0; i < tinstances->dim; i++) | |
2826 | { | |
2827 | TemplateInstance *ti = (*tinstances)[i]; | |
2828 | if (handle == ti) | |
2829 | { | |
2830 | tinstances->remove(i); | |
2831 | break; | |
2832 | } | |
2833 | } | |
2834 | } | |
2835 | } | |
2836 | ||
2837 | /* ======================== Type ============================================ */ | |
2838 | ||
2839 | /**** | |
2840 | * Given an identifier, figure out which TemplateParameter it is. | |
2841 | * Return IDX_NOTFOUND if not found. | |
2842 | */ | |
2843 | ||
2844 | static size_t templateIdentifierLookup(Identifier *id, TemplateParameters *parameters) | |
2845 | { | |
2846 | for (size_t i = 0; i < parameters->dim; i++) | |
2847 | { | |
2848 | TemplateParameter *tp = (*parameters)[i]; | |
2849 | if (tp->ident->equals(id)) | |
2850 | return i; | |
2851 | } | |
2852 | return IDX_NOTFOUND; | |
2853 | } | |
2854 | ||
2855 | size_t templateParameterLookup(Type *tparam, TemplateParameters *parameters) | |
2856 | { | |
2857 | if (tparam->ty == Tident) | |
2858 | { | |
2859 | TypeIdentifier *tident = (TypeIdentifier *)tparam; | |
2860 | //printf("\ttident = '%s'\n", tident->toChars()); | |
2861 | return templateIdentifierLookup(tident->ident, parameters); | |
2862 | } | |
2863 | return IDX_NOTFOUND; | |
2864 | } | |
2865 | ||
2866 | unsigned char deduceWildHelper(Type *t, Type **at, Type *tparam) | |
2867 | { | |
2868 | if ((tparam->mod & MODwild) == 0) | |
2869 | return 0; | |
2870 | ||
2871 | *at = NULL; | |
2872 | ||
2873 | #define X(U,T) ((U) << 4) | (T) | |
2874 | switch (X(tparam->mod, t->mod)) | |
2875 | { | |
2876 | case X(MODwild, 0): | |
2877 | case X(MODwild, MODconst): | |
2878 | case X(MODwild, MODshared): | |
2879 | case X(MODwild, MODshared | MODconst): | |
2880 | case X(MODwild, MODimmutable): | |
2881 | case X(MODwildconst, 0): | |
2882 | case X(MODwildconst, MODconst): | |
2883 | case X(MODwildconst, MODshared): | |
2884 | case X(MODwildconst, MODshared | MODconst): | |
2885 | case X(MODwildconst, MODimmutable): | |
2886 | case X(MODshared | MODwild, MODshared): | |
2887 | case X(MODshared | MODwild, MODshared | MODconst): | |
2888 | case X(MODshared | MODwild, MODimmutable): | |
2889 | case X(MODshared | MODwildconst, MODshared): | |
2890 | case X(MODshared | MODwildconst, MODshared | MODconst): | |
2891 | case X(MODshared | MODwildconst, MODimmutable): | |
2892 | { | |
2893 | unsigned char wm = (t->mod & ~MODshared); | |
2894 | if (wm == 0) | |
2895 | wm = MODmutable; | |
2896 | unsigned char m = (t->mod & (MODconst | MODimmutable)) | (tparam->mod & t->mod & MODshared); | |
2897 | *at = t->unqualify(m); | |
2898 | return wm; | |
2899 | } | |
2900 | ||
2901 | case X(MODwild, MODwild): | |
2902 | case X(MODwild, MODwildconst): | |
2903 | case X(MODwild, MODshared | MODwild): | |
2904 | case X(MODwild, MODshared | MODwildconst): | |
2905 | case X(MODwildconst, MODwild): | |
2906 | case X(MODwildconst, MODwildconst): | |
2907 | case X(MODwildconst, MODshared | MODwild): | |
2908 | case X(MODwildconst, MODshared | MODwildconst): | |
2909 | case X(MODshared | MODwild, MODshared | MODwild): | |
2910 | case X(MODshared | MODwild, MODshared | MODwildconst): | |
2911 | case X(MODshared | MODwildconst, MODshared | MODwild): | |
2912 | case X(MODshared | MODwildconst, MODshared | MODwildconst): | |
2913 | { | |
2914 | *at = t->unqualify(tparam->mod & t->mod); | |
2915 | return MODwild; | |
2916 | } | |
2917 | ||
2918 | default: | |
2919 | return 0; | |
2920 | } | |
2921 | #undef X | |
2922 | } | |
2923 | ||
2924 | MATCH deduceTypeHelper(Type *t, Type **at, Type *tparam) | |
2925 | { | |
2926 | // 9*9 == 81 cases | |
2927 | ||
2928 | #define X(U,T) ((U) << 4) | (T) | |
2929 | switch (X(tparam->mod, t->mod)) | |
2930 | { | |
2931 | case X(0, 0): | |
2932 | case X(0, MODconst): | |
2933 | case X(0, MODwild): | |
2934 | case X(0, MODwildconst): | |
2935 | case X(0, MODshared): | |
2936 | case X(0, MODshared | MODconst): | |
2937 | case X(0, MODshared | MODwild): | |
2938 | case X(0, MODshared | MODwildconst): | |
2939 | case X(0, MODimmutable): | |
2940 | // foo(U) T => T | |
2941 | // foo(U) const(T) => const(T) | |
2942 | // foo(U) inout(T) => inout(T) | |
2943 | // foo(U) inout(const(T)) => inout(const(T)) | |
2944 | // foo(U) shared(T) => shared(T) | |
2945 | // foo(U) shared(const(T)) => shared(const(T)) | |
2946 | // foo(U) shared(inout(T)) => shared(inout(T)) | |
2947 | // foo(U) shared(inout(const(T))) => shared(inout(const(T))) | |
2948 | // foo(U) immutable(T) => immutable(T) | |
2949 | { | |
2950 | *at = t; | |
2951 | return MATCHexact; | |
2952 | } | |
2953 | ||
2954 | case X(MODconst, MODconst): | |
2955 | case X(MODwild, MODwild): | |
2956 | case X(MODwildconst, MODwildconst): | |
2957 | case X(MODshared, MODshared): | |
2958 | case X(MODshared | MODconst, MODshared | MODconst): | |
2959 | case X(MODshared | MODwild, MODshared | MODwild): | |
2960 | case X(MODshared | MODwildconst, MODshared | MODwildconst): | |
2961 | case X(MODimmutable, MODimmutable): | |
2962 | // foo(const(U)) const(T) => T | |
2963 | // foo(inout(U)) inout(T) => T | |
2964 | // foo(inout(const(U))) inout(const(T)) => T | |
2965 | // foo(shared(U)) shared(T) => T | |
2966 | // foo(shared(const(U))) shared(const(T)) => T | |
2967 | // foo(shared(inout(U))) shared(inout(T)) => T | |
2968 | // foo(shared(inout(const(U)))) shared(inout(const(T))) => T | |
2969 | // foo(immutable(U)) immutable(T) => T | |
2970 | { | |
2971 | *at = t->mutableOf()->unSharedOf(); | |
2972 | return MATCHexact; | |
2973 | } | |
2974 | ||
2975 | case X(MODconst, 0): | |
2976 | case X(MODconst, MODwild): | |
2977 | case X(MODconst, MODwildconst): | |
2978 | case X(MODconst, MODshared | MODconst): | |
2979 | case X(MODconst, MODshared | MODwild): | |
2980 | case X(MODconst, MODshared | MODwildconst): | |
2981 | case X(MODconst, MODimmutable): | |
2982 | case X(MODwild, MODshared | MODwild): | |
2983 | case X(MODwildconst, MODshared | MODwildconst): | |
2984 | case X(MODshared | MODconst, MODimmutable): | |
2985 | // foo(const(U)) T => T | |
2986 | // foo(const(U)) inout(T) => T | |
2987 | // foo(const(U)) inout(const(T)) => T | |
2988 | // foo(const(U)) shared(const(T)) => shared(T) | |
2989 | // foo(const(U)) shared(inout(T)) => shared(T) | |
2990 | // foo(const(U)) shared(inout(const(T))) => shared(T) | |
2991 | // foo(const(U)) immutable(T) => T | |
2992 | // foo(inout(U)) shared(inout(T)) => shared(T) | |
2993 | // foo(inout(const(U))) shared(inout(const(T))) => shared(T) | |
2994 | // foo(shared(const(U))) immutable(T) => T | |
2995 | { | |
2996 | *at = t->mutableOf(); | |
2997 | return MATCHconst; | |
2998 | } | |
2999 | ||
3000 | case X(MODconst, MODshared): | |
3001 | // foo(const(U)) shared(T) => shared(T) | |
3002 | { | |
3003 | *at = t; | |
3004 | return MATCHconst; | |
3005 | } | |
3006 | ||
3007 | case X(MODshared, MODshared | MODconst): | |
3008 | case X(MODshared, MODshared | MODwild): | |
3009 | case X(MODshared, MODshared | MODwildconst): | |
3010 | case X(MODshared | MODconst, MODshared): | |
3011 | // foo(shared(U)) shared(const(T)) => const(T) | |
3012 | // foo(shared(U)) shared(inout(T)) => inout(T) | |
3013 | // foo(shared(U)) shared(inout(const(T))) => inout(const(T)) | |
3014 | // foo(shared(const(U))) shared(T) => T | |
3015 | { | |
3016 | *at = t->unSharedOf(); | |
3017 | return MATCHconst; | |
3018 | } | |
3019 | ||
3020 | case X(MODwildconst, MODimmutable): | |
3021 | case X(MODshared | MODconst, MODshared | MODwildconst): | |
3022 | case X(MODshared | MODwildconst, MODimmutable): | |
3023 | case X(MODshared | MODwildconst, MODshared | MODwild): | |
3024 | // foo(inout(const(U))) immutable(T) => T | |
3025 | // foo(shared(const(U))) shared(inout(const(T))) => T | |
3026 | // foo(shared(inout(const(U)))) immutable(T) => T | |
3027 | // foo(shared(inout(const(U)))) shared(inout(T)) => T | |
3028 | { | |
3029 | *at = t->unSharedOf()->mutableOf(); | |
3030 | return MATCHconst; | |
3031 | } | |
3032 | ||
3033 | case X(MODshared | MODconst, MODshared | MODwild): | |
3034 | // foo(shared(const(U))) shared(inout(T)) => T | |
3035 | { | |
3036 | *at = t->unSharedOf()->mutableOf(); | |
3037 | return MATCHconst; | |
3038 | } | |
3039 | ||
3040 | case X(MODwild, 0): | |
3041 | case X(MODwild, MODconst): | |
3042 | case X(MODwild, MODwildconst): | |
3043 | case X(MODwild, MODimmutable): | |
3044 | case X(MODwild, MODshared): | |
3045 | case X(MODwild, MODshared | MODconst): | |
3046 | case X(MODwild, MODshared | MODwildconst): | |
3047 | case X(MODwildconst, 0): | |
3048 | case X(MODwildconst, MODconst): | |
3049 | case X(MODwildconst, MODwild): | |
3050 | case X(MODwildconst, MODshared): | |
3051 | case X(MODwildconst, MODshared | MODconst): | |
3052 | case X(MODwildconst, MODshared | MODwild): | |
3053 | case X(MODshared, 0): | |
3054 | case X(MODshared, MODconst): | |
3055 | case X(MODshared, MODwild): | |
3056 | case X(MODshared, MODwildconst): | |
3057 | case X(MODshared, MODimmutable): | |
3058 | case X(MODshared | MODconst, 0): | |
3059 | case X(MODshared | MODconst, MODconst): | |
3060 | case X(MODshared | MODconst, MODwild): | |
3061 | case X(MODshared | MODconst, MODwildconst): | |
3062 | case X(MODshared | MODwild, 0): | |
3063 | case X(MODshared | MODwild, MODconst): | |
3064 | case X(MODshared | MODwild, MODwild): | |
3065 | case X(MODshared | MODwild, MODwildconst): | |
3066 | case X(MODshared | MODwild, MODimmutable): | |
3067 | case X(MODshared | MODwild, MODshared): | |
3068 | case X(MODshared | MODwild, MODshared | MODconst): | |
3069 | case X(MODshared | MODwild, MODshared | MODwildconst): | |
3070 | case X(MODshared | MODwildconst, 0): | |
3071 | case X(MODshared | MODwildconst, MODconst): | |
3072 | case X(MODshared | MODwildconst, MODwild): | |
3073 | case X(MODshared | MODwildconst, MODwildconst): | |
3074 | case X(MODshared | MODwildconst, MODshared): | |
3075 | case X(MODshared | MODwildconst, MODshared | MODconst): | |
3076 | case X(MODimmutable, 0): | |
3077 | case X(MODimmutable, MODconst): | |
3078 | case X(MODimmutable, MODwild): | |
3079 | case X(MODimmutable, MODwildconst): | |
3080 | case X(MODimmutable, MODshared): | |
3081 | case X(MODimmutable, MODshared | MODconst): | |
3082 | case X(MODimmutable, MODshared | MODwild): | |
3083 | case X(MODimmutable, MODshared | MODwildconst): | |
3084 | // foo(inout(U)) T => nomatch | |
3085 | // foo(inout(U)) const(T) => nomatch | |
3086 | // foo(inout(U)) inout(const(T)) => nomatch | |
3087 | // foo(inout(U)) immutable(T) => nomatch | |
3088 | // foo(inout(U)) shared(T) => nomatch | |
3089 | // foo(inout(U)) shared(const(T)) => nomatch | |
3090 | // foo(inout(U)) shared(inout(const(T))) => nomatch | |
3091 | // foo(inout(const(U))) T => nomatch | |
3092 | // foo(inout(const(U))) const(T) => nomatch | |
3093 | // foo(inout(const(U))) inout(T) => nomatch | |
3094 | // foo(inout(const(U))) shared(T) => nomatch | |
3095 | // foo(inout(const(U))) shared(const(T)) => nomatch | |
3096 | // foo(inout(const(U))) shared(inout(T)) => nomatch | |
3097 | // foo(shared(U)) T => nomatch | |
3098 | // foo(shared(U)) const(T) => nomatch | |
3099 | // foo(shared(U)) inout(T) => nomatch | |
3100 | // foo(shared(U)) inout(const(T)) => nomatch | |
3101 | // foo(shared(U)) immutable(T) => nomatch | |
3102 | // foo(shared(const(U))) T => nomatch | |
3103 | // foo(shared(const(U))) const(T) => nomatch | |
3104 | // foo(shared(const(U))) inout(T) => nomatch | |
3105 | // foo(shared(const(U))) inout(const(T)) => nomatch | |
3106 | // foo(shared(inout(U))) T => nomatch | |
3107 | // foo(shared(inout(U))) const(T) => nomatch | |
3108 | // foo(shared(inout(U))) inout(T) => nomatch | |
3109 | // foo(shared(inout(U))) inout(const(T)) => nomatch | |
3110 | // foo(shared(inout(U))) immutable(T) => nomatch | |
3111 | // foo(shared(inout(U))) shared(T) => nomatch | |
3112 | // foo(shared(inout(U))) shared(const(T)) => nomatch | |
3113 | // foo(shared(inout(U))) shared(inout(const(T))) => nomatch | |
3114 | // foo(shared(inout(const(U)))) T => nomatch | |
3115 | // foo(shared(inout(const(U)))) const(T) => nomatch | |
3116 | // foo(shared(inout(const(U)))) inout(T) => nomatch | |
3117 | // foo(shared(inout(const(U)))) inout(const(T)) => nomatch | |
3118 | // foo(shared(inout(const(U)))) shared(T) => nomatch | |
3119 | // foo(shared(inout(const(U)))) shared(const(T)) => nomatch | |
3120 | // foo(immutable(U)) T => nomatch | |
3121 | // foo(immutable(U)) const(T) => nomatch | |
3122 | // foo(immutable(U)) inout(T) => nomatch | |
3123 | // foo(immutable(U)) inout(const(T)) => nomatch | |
3124 | // foo(immutable(U)) shared(T) => nomatch | |
3125 | // foo(immutable(U)) shared(const(T)) => nomatch | |
3126 | // foo(immutable(U)) shared(inout(T)) => nomatch | |
3127 | // foo(immutable(U)) shared(inout(const(T))) => nomatch | |
3128 | return MATCHnomatch; | |
3129 | ||
3130 | default: | |
3131 | assert(0); | |
3132 | return MATCHnomatch; // silence compiler warning about missing return | |
3133 | } | |
3134 | #undef X | |
3135 | } | |
3136 | ||
3137 | /* These form the heart of template argument deduction. | |
3138 | * Given 'this' being the type argument to the template instance, | |
3139 | * it is matched against the template declaration parameter specialization | |
3140 | * 'tparam' to determine the type to be used for the parameter. | |
3141 | * Example: | |
3142 | * template Foo(T:T*) // template declaration | |
3143 | * Foo!(int*) // template instantiation | |
3144 | * Input: | |
3145 | * this = int* | |
3146 | * tparam = T* | |
3147 | * parameters = [ T:T* ] // Array of TemplateParameter's | |
3148 | * Output: | |
3149 | * dedtypes = [ int ] // Array of Expression/Type's | |
3150 | */ | |
3151 | MATCH deduceType(RootObject *o, Scope *sc, Type *tparam, TemplateParameters *parameters, | |
3152 | Objects *dedtypes, unsigned *wm, size_t inferStart) | |
3153 | { | |
3154 | class DeduceType : public Visitor | |
3155 | { | |
3156 | public: | |
3157 | Scope *sc; | |
3158 | Type *tparam; | |
3159 | TemplateParameters *parameters; | |
3160 | Objects *dedtypes; | |
3161 | unsigned *wm; | |
3162 | size_t inferStart; | |
3163 | MATCH result; | |
3164 | ||
3165 | DeduceType(Scope *sc, Type *tparam, TemplateParameters *parameters, Objects *dedtypes, unsigned *wm, size_t inferStart) | |
3166 | : sc(sc), tparam(tparam), parameters(parameters), dedtypes(dedtypes), wm(wm), inferStart(inferStart) | |
3167 | { | |
3168 | result = MATCHnomatch; | |
3169 | } | |
3170 | ||
3171 | void visit(Type *t) | |
3172 | { | |
3173 | if (!tparam) | |
3174 | goto Lnomatch; | |
3175 | ||
3176 | if (t == tparam) | |
3177 | goto Lexact; | |
3178 | ||
3179 | if (tparam->ty == Tident) | |
3180 | { | |
3181 | // Determine which parameter tparam is | |
3182 | size_t i = templateParameterLookup(tparam, parameters); | |
3183 | if (i == IDX_NOTFOUND) | |
3184 | { | |
3185 | if (!sc) | |
3186 | goto Lnomatch; | |
3187 | ||
3188 | /* Need a loc to go with the semantic routine. | |
3189 | */ | |
3190 | Loc loc; | |
3191 | if (parameters->dim) | |
3192 | { | |
3193 | TemplateParameter *tp = (*parameters)[0]; | |
3194 | loc = tp->loc; | |
3195 | } | |
3196 | ||
3197 | /* BUG: what if tparam is a template instance, that | |
3198 | * has as an argument another Tident? | |
3199 | */ | |
3200 | tparam = tparam->semantic(loc, sc); | |
3201 | assert(tparam->ty != Tident); | |
3202 | result = deduceType(t, sc, tparam, parameters, dedtypes, wm); | |
3203 | return; | |
3204 | } | |
3205 | ||
3206 | TemplateParameter *tp = (*parameters)[i]; | |
3207 | ||
3208 | TypeIdentifier *tident = (TypeIdentifier *)tparam; | |
3209 | if (tident->idents.dim > 0) | |
3210 | { | |
3211 | //printf("matching %s to %s\n", tparam->toChars(), t->toChars()); | |
3212 | Dsymbol *s = t->toDsymbol(sc); | |
3213 | for (size_t j = tident->idents.dim; j-- > 0; ) | |
3214 | { | |
3215 | RootObject *id = tident->idents[j]; | |
3216 | if (id->dyncast() == DYNCAST_IDENTIFIER) | |
3217 | { | |
3218 | if (!s || !s->parent) | |
3219 | goto Lnomatch; | |
3220 | Dsymbol *s2 = s->parent->search(Loc(), (Identifier *)id); | |
3221 | if (!s2) | |
3222 | goto Lnomatch; | |
3223 | s2 = s2->toAlias(); | |
3224 | //printf("[%d] s = %s %s, s2 = %s %s\n", j, s->kind(), s->toChars(), s2->kind(), s2->toChars()); | |
3225 | if (s != s2) | |
3226 | { | |
3227 | if (Type *tx = s2->getType()) | |
3228 | { | |
3229 | if (s != tx->toDsymbol(sc)) | |
3230 | goto Lnomatch; | |
3231 | } | |
3232 | else | |
3233 | goto Lnomatch; | |
3234 | } | |
3235 | s = s->parent; | |
3236 | } | |
3237 | else | |
3238 | goto Lnomatch; | |
3239 | } | |
3240 | //printf("[e] s = %s\n", s?s->toChars():"(null)"); | |
3241 | if (tp->isTemplateTypeParameter()) | |
3242 | { | |
3243 | Type *tt = s->getType(); | |
3244 | if (!tt) | |
3245 | goto Lnomatch; | |
3246 | Type *at = (Type *)(*dedtypes)[i]; | |
3247 | if (at && at->ty == Tnone) | |
3248 | at = ((TypeDeduced *)at)->tded; | |
3249 | if (!at || tt->equals(at)) | |
3250 | { | |
3251 | (*dedtypes)[i] = tt; | |
3252 | goto Lexact; | |
3253 | } | |
3254 | } | |
3255 | if (tp->isTemplateAliasParameter()) | |
3256 | { | |
3257 | Dsymbol *s2 = (Dsymbol *)(*dedtypes)[i]; | |
3258 | if (!s2 || s == s2) | |
3259 | { | |
3260 | (*dedtypes)[i] = s; | |
3261 | goto Lexact; | |
3262 | } | |
3263 | } | |
3264 | goto Lnomatch; | |
3265 | } | |
3266 | ||
3267 | // Found the corresponding parameter tp | |
3268 | if (!tp->isTemplateTypeParameter()) | |
3269 | goto Lnomatch; | |
3270 | ||
3271 | Type *at = (Type *)(*dedtypes)[i]; | |
3272 | Type *tt; | |
3273 | if (unsigned char wx = wm ? deduceWildHelper(t, &tt, tparam) : 0) | |
3274 | { | |
3275 | // type vs (none) | |
3276 | if (!at) | |
3277 | { | |
3278 | (*dedtypes)[i] = tt; | |
3279 | *wm |= wx; | |
3280 | result = MATCHconst; | |
3281 | return; | |
3282 | } | |
3283 | ||
3284 | // type vs expressions | |
3285 | if (at->ty == Tnone) | |
3286 | { | |
3287 | TypeDeduced *xt = (TypeDeduced *)at; | |
3288 | result = xt->matchAll(tt); | |
3289 | if (result > MATCHnomatch) | |
3290 | { | |
3291 | (*dedtypes)[i] = tt; | |
3292 | if (result > MATCHconst) | |
3293 | result = MATCHconst; // limit level for inout matches | |
3294 | delete xt; | |
3295 | } | |
3296 | return; | |
3297 | } | |
3298 | ||
3299 | // type vs type | |
3300 | if (tt->equals(at)) | |
3301 | { | |
3302 | (*dedtypes)[i] = tt; // Prefer current type match | |
3303 | goto Lconst; | |
3304 | } | |
3305 | if (tt->implicitConvTo(at->constOf())) | |
3306 | { | |
3307 | (*dedtypes)[i] = at->constOf()->mutableOf(); | |
3308 | *wm |= MODconst; | |
3309 | goto Lconst; | |
3310 | } | |
3311 | if (at->implicitConvTo(tt->constOf())) | |
3312 | { | |
3313 | (*dedtypes)[i] = tt->constOf()->mutableOf(); | |
3314 | *wm |= MODconst; | |
3315 | goto Lconst; | |
3316 | } | |
3317 | goto Lnomatch; | |
3318 | } | |
3319 | else if (MATCH m = deduceTypeHelper(t, &tt, tparam)) | |
3320 | { | |
3321 | // type vs (none) | |
3322 | if (!at) | |
3323 | { | |
3324 | (*dedtypes)[i] = tt; | |
3325 | result = m; | |
3326 | return; | |
3327 | } | |
3328 | ||
3329 | // type vs expressions | |
3330 | if (at->ty == Tnone) | |
3331 | { | |
3332 | TypeDeduced *xt = (TypeDeduced *)at; | |
3333 | result = xt->matchAll(tt); | |
3334 | if (result > MATCHnomatch) | |
3335 | { | |
3336 | (*dedtypes)[i] = tt; | |
3337 | delete xt; | |
3338 | } | |
3339 | return; | |
3340 | } | |
3341 | ||
3342 | // type vs type | |
3343 | if (tt->equals(at)) | |
3344 | { | |
3345 | goto Lexact; | |
3346 | } | |
3347 | if (tt->ty == Tclass && at->ty == Tclass) | |
3348 | { | |
3349 | result = tt->implicitConvTo(at); | |
3350 | return; | |
3351 | } | |
3352 | if (tt->ty == Tsarray && at->ty == Tarray && | |
3353 | tt->nextOf()->implicitConvTo(at->nextOf()) >= MATCHconst) | |
3354 | { | |
3355 | goto Lexact; | |
3356 | } | |
3357 | } | |
3358 | goto Lnomatch; | |
3359 | } | |
3360 | ||
3361 | if (tparam->ty == Ttypeof) | |
3362 | { | |
3363 | /* Need a loc to go with the semantic routine. | |
3364 | */ | |
3365 | Loc loc; | |
3366 | if (parameters->dim) | |
3367 | { | |
3368 | TemplateParameter *tp = (*parameters)[0]; | |
3369 | loc = tp->loc; | |
3370 | } | |
3371 | ||
3372 | tparam = tparam->semantic(loc, sc); | |
3373 | } | |
3374 | if (t->ty != tparam->ty) | |
3375 | { | |
3376 | if (Dsymbol *sym = t->toDsymbol(sc)) | |
3377 | { | |
3378 | if (sym->isforwardRef() && !tparam->deco) | |
3379 | goto Lnomatch; | |
3380 | } | |
3381 | ||
3382 | MATCH m = t->implicitConvTo(tparam); | |
3383 | if (m == MATCHnomatch) | |
3384 | { | |
3385 | if (t->ty == Tclass) | |
3386 | { | |
3387 | TypeClass *tc = (TypeClass *)t; | |
3388 | if (tc->sym->aliasthis && !(tc->att & RECtracingDT)) | |
3389 | { | |
3390 | tc->att = (AliasThisRec)(tc->att | RECtracingDT); | |
3391 | m = deduceType(t->aliasthisOf(), sc, tparam, parameters, dedtypes, wm); | |
3392 | tc->att = (AliasThisRec)(tc->att & ~RECtracingDT); | |
3393 | } | |
3394 | } | |
3395 | else if (t->ty == Tstruct) | |
3396 | { | |
3397 | TypeStruct *ts = (TypeStruct *)t; | |
3398 | if (ts->sym->aliasthis && !(ts->att & RECtracingDT)) | |
3399 | { | |
3400 | ts->att = (AliasThisRec)(ts->att | RECtracingDT); | |
3401 | m = deduceType(t->aliasthisOf(), sc, tparam, parameters, dedtypes, wm); | |
3402 | ts->att = (AliasThisRec)(ts->att & ~RECtracingDT); | |
3403 | } | |
3404 | } | |
3405 | } | |
3406 | result = m; | |
3407 | return; | |
3408 | } | |
3409 | ||
3410 | if (t->nextOf()) | |
3411 | { | |
3412 | if (tparam->deco && !tparam->hasWild()) | |
3413 | { | |
3414 | result = t->implicitConvTo(tparam); | |
3415 | return; | |
3416 | } | |
3417 | ||
3418 | Type *tpn = tparam->nextOf(); | |
3419 | if (wm && t->ty == Taarray && tparam->isWild()) | |
3420 | { | |
3421 | // Bugzilla 12403: In IFTI, stop inout matching on transitive part of AA types. | |
3422 | tpn = tpn->substWildTo(MODmutable); | |
3423 | } | |
3424 | ||
3425 | result = deduceType(t->nextOf(), sc, tpn, parameters, dedtypes, wm); | |
3426 | return; | |
3427 | } | |
3428 | ||
3429 | Lexact: | |
3430 | result = MATCHexact; | |
3431 | return; | |
3432 | ||
3433 | Lnomatch: | |
3434 | result = MATCHnomatch; | |
3435 | return; | |
3436 | ||
3437 | Lconst: | |
3438 | result = MATCHconst; | |
3439 | } | |
3440 | ||
3441 | void visit(TypeVector *t) | |
3442 | { | |
3443 | if (tparam->ty == Tvector) | |
3444 | { | |
3445 | TypeVector *tp = (TypeVector *)tparam; | |
3446 | result = deduceType(t->basetype, sc, tp->basetype, parameters, dedtypes, wm); | |
3447 | return; | |
3448 | } | |
3449 | visit((Type *)t); | |
3450 | } | |
3451 | ||
3452 | void visit(TypeDArray *t) | |
3453 | { | |
3454 | visit((Type *)t); | |
3455 | } | |
3456 | ||
3457 | void visit(TypeSArray *t) | |
3458 | { | |
3459 | // Extra check that array dimensions must match | |
3460 | if (tparam) | |
3461 | { | |
3462 | if (tparam->ty == Tarray) | |
3463 | { | |
3464 | MATCH m = deduceType(t->next, sc, tparam->nextOf(), parameters, dedtypes, wm); | |
3465 | result = (m >= MATCHconst) ? MATCHconvert : MATCHnomatch; | |
3466 | return; | |
3467 | } | |
3468 | ||
3469 | TemplateParameter *tp = NULL; | |
3470 | Expression *edim = NULL; | |
3471 | size_t i; | |
3472 | if (tparam->ty == Tsarray) | |
3473 | { | |
3474 | TypeSArray *tsa = (TypeSArray *)tparam; | |
3475 | if (tsa->dim->op == TOKvar && | |
3476 | ((VarExp *)tsa->dim)->var->storage_class & STCtemplateparameter) | |
3477 | { | |
3478 | Identifier *id = ((VarExp *)tsa->dim)->var->ident; | |
3479 | i = templateIdentifierLookup(id, parameters); | |
3480 | assert(i != IDX_NOTFOUND); | |
3481 | tp = (*parameters)[i]; | |
3482 | } | |
3483 | else | |
3484 | edim = tsa->dim; | |
3485 | } | |
3486 | else if (tparam->ty == Taarray) | |
3487 | { | |
3488 | TypeAArray *taa = (TypeAArray *)tparam; | |
3489 | i = templateParameterLookup(taa->index, parameters); | |
3490 | if (i != IDX_NOTFOUND) | |
3491 | tp = (*parameters)[i]; | |
3492 | else | |
3493 | { | |
3494 | Expression *e; | |
3495 | Type *tx; | |
3496 | Dsymbol *s; | |
3497 | taa->index->resolve(Loc(), sc, &e, &tx, &s); | |
3498 | edim = s ? getValue(s) : getValue(e); | |
3499 | } | |
3500 | } | |
3501 | if ((tp && tp->matchArg(sc, t->dim, i, parameters, dedtypes, NULL)) || | |
3502 | (edim && edim->toInteger() == t->dim->toInteger())) | |
3503 | { | |
3504 | result = deduceType(t->next, sc, tparam->nextOf(), parameters, dedtypes, wm); | |
3505 | return; | |
3506 | } | |
3507 | } | |
3508 | visit((Type *)t); | |
3509 | return; | |
3510 | ||
3511 | result = MATCHnomatch; | |
3512 | } | |
3513 | ||
3514 | void visit(TypeAArray *t) | |
3515 | { | |
3516 | // Extra check that index type must match | |
3517 | if (tparam && tparam->ty == Taarray) | |
3518 | { | |
3519 | TypeAArray *tp = (TypeAArray *)tparam; | |
3520 | if (!deduceType(t->index, sc, tp->index, parameters, dedtypes)) | |
3521 | { | |
3522 | result = MATCHnomatch; | |
3523 | return; | |
3524 | } | |
3525 | } | |
3526 | visit((Type *)t); | |
3527 | } | |
3528 | ||
3529 | void visit(TypeFunction *t) | |
3530 | { | |
3531 | //printf("TypeFunction::deduceType()\n"); | |
3532 | //printf("\tthis = %d, ", t->ty); t->print(); | |
3533 | //printf("\ttparam = %d, ", tparam->ty); tparam->print(); | |
3534 | ||
3535 | // Extra check that function characteristics must match | |
3536 | if (tparam && tparam->ty == Tfunction) | |
3537 | { | |
3538 | TypeFunction *tp = (TypeFunction *)tparam; | |
3539 | if (t->varargs != tp->varargs || | |
3540 | t->linkage != tp->linkage) | |
3541 | { | |
3542 | result = MATCHnomatch; | |
3543 | return; | |
3544 | } | |
3545 | ||
3546 | size_t nfargs = Parameter::dim(t->parameters); | |
3547 | size_t nfparams = Parameter::dim(tp->parameters); | |
3548 | ||
3549 | // bug 2579 fix: Apply function parameter storage classes to parameter types | |
3550 | for (size_t i = 0; i < nfparams; i++) | |
3551 | { | |
3552 | Parameter *fparam = Parameter::getNth(tp->parameters, i); | |
3553 | fparam->type = fparam->type->addStorageClass(fparam->storageClass); | |
3554 | fparam->storageClass &= ~(STC_TYPECTOR | STCin); | |
3555 | } | |
3556 | //printf("\t-> this = %d, ", t->ty); t->print(); | |
3557 | //printf("\t-> tparam = %d, ", tparam->ty); tparam->print(); | |
3558 | ||
3559 | /* See if tuple match | |
3560 | */ | |
3561 | if (nfparams > 0 && nfargs >= nfparams - 1) | |
3562 | { | |
3563 | /* See if 'A' of the template parameter matches 'A' | |
3564 | * of the type of the last function parameter. | |
3565 | */ | |
3566 | Parameter *fparam = Parameter::getNth(tp->parameters, nfparams - 1); | |
3567 | assert(fparam); | |
3568 | assert(fparam->type); | |
3569 | if (fparam->type->ty != Tident) | |
3570 | goto L1; | |
3571 | TypeIdentifier *tid = (TypeIdentifier *)fparam->type; | |
3572 | if (tid->idents.dim) | |
3573 | goto L1; | |
3574 | ||
3575 | /* Look through parameters to find tuple matching tid->ident | |
3576 | */ | |
3577 | size_t tupi = 0; | |
3578 | for (; 1; tupi++) | |
3579 | { | |
3580 | if (tupi == parameters->dim) | |
3581 | goto L1; | |
3582 | TemplateParameter *tx = (*parameters)[tupi]; | |
3583 | TemplateTupleParameter *tup = tx->isTemplateTupleParameter(); | |
3584 | if (tup && tup->ident->equals(tid->ident)) | |
3585 | break; | |
3586 | } | |
3587 | ||
3588 | /* The types of the function arguments [nfparams - 1 .. nfargs] | |
3589 | * now form the tuple argument. | |
3590 | */ | |
3591 | size_t tuple_dim = nfargs - (nfparams - 1); | |
3592 | ||
3593 | /* See if existing tuple, and whether it matches or not | |
3594 | */ | |
3595 | RootObject *o = (*dedtypes)[tupi]; | |
3596 | if (o) | |
3597 | { | |
3598 | // Existing deduced argument must be a tuple, and must match | |
3599 | Tuple *tup = isTuple(o); | |
3600 | if (!tup || tup->objects.dim != tuple_dim) | |
3601 | { | |
3602 | result = MATCHnomatch; | |
3603 | return; | |
3604 | } | |
3605 | for (size_t i = 0; i < tuple_dim; i++) | |
3606 | { | |
3607 | Parameter *arg = Parameter::getNth(t->parameters, nfparams - 1 + i); | |
3608 | if (!arg->type->equals(tup->objects[i])) | |
3609 | { | |
3610 | result = MATCHnomatch; | |
3611 | return; | |
3612 | } | |
3613 | } | |
3614 | } | |
3615 | else | |
3616 | { | |
3617 | // Create new tuple | |
3618 | Tuple *tup = new Tuple(); | |
3619 | tup->objects.setDim(tuple_dim); | |
3620 | for (size_t i = 0; i < tuple_dim; i++) | |
3621 | { | |
3622 | Parameter *arg = Parameter::getNth(t->parameters, nfparams - 1 + i); | |
3623 | tup->objects[i] = arg->type; | |
3624 | } | |
3625 | (*dedtypes)[tupi] = tup; | |
3626 | } | |
3627 | nfparams--; // don't consider the last parameter for type deduction | |
3628 | goto L2; | |
3629 | } | |
3630 | ||
3631 | L1: | |
3632 | if (nfargs != nfparams) | |
3633 | { | |
3634 | result = MATCHnomatch; | |
3635 | return; | |
3636 | } | |
3637 | L2: | |
3638 | for (size_t i = 0; i < nfparams; i++) | |
3639 | { | |
3640 | Parameter *a = Parameter::getNth(t->parameters, i); | |
3641 | Parameter *ap = Parameter::getNth(tp->parameters, i); | |
3642 | ||
3643 | if (!a->isCovariant(t->isref, ap) || | |
3644 | !deduceType(a->type, sc, ap->type, parameters, dedtypes)) | |
3645 | { | |
3646 | result = MATCHnomatch; | |
3647 | return; | |
3648 | } | |
3649 | } | |
3650 | } | |
3651 | visit((Type *)t); | |
3652 | } | |
3653 | ||
3654 | void visit(TypeIdentifier *t) | |
3655 | { | |
3656 | // Extra check | |
3657 | if (tparam && tparam->ty == Tident) | |
3658 | { | |
3659 | TypeIdentifier *tp = (TypeIdentifier *)tparam; | |
3660 | ||
3661 | for (size_t i = 0; i < t->idents.dim; i++) | |
3662 | { | |
3663 | RootObject *id1 = t->idents[i]; | |
3664 | RootObject *id2 = tp->idents[i]; | |
3665 | ||
3666 | if (!id1->equals(id2)) | |
3667 | { | |
3668 | result = MATCHnomatch; | |
3669 | return; | |
3670 | } | |
3671 | } | |
3672 | } | |
3673 | visit((Type *)t); | |
3674 | } | |
3675 | ||
3676 | void visit(TypeInstance *t) | |
3677 | { | |
3678 | // Extra check | |
3679 | if (tparam && tparam->ty == Tinstance && t->tempinst->tempdecl) | |
3680 | { | |
3681 | TemplateDeclaration *tempdecl = t->tempinst->tempdecl->isTemplateDeclaration(); | |
3682 | assert(tempdecl); | |
3683 | ||
3684 | TypeInstance *tp = (TypeInstance *)tparam; | |
3685 | ||
3686 | //printf("tempinst->tempdecl = %p\n", tempdecl); | |
3687 | //printf("tp->tempinst->tempdecl = %p\n", tp->tempinst->tempdecl); | |
3688 | if (!tp->tempinst->tempdecl) | |
3689 | { | |
3690 | //printf("tp->tempinst->name = '%s'\n", tp->tempinst->name->toChars()); | |
3691 | ||
3692 | /* Handle case of: | |
3693 | * template Foo(T : sa!(T), alias sa) | |
3694 | */ | |
3695 | size_t i = templateIdentifierLookup(tp->tempinst->name, parameters); | |
3696 | if (i == IDX_NOTFOUND) | |
3697 | { | |
3698 | /* Didn't find it as a parameter identifier. Try looking | |
3699 | * it up and seeing if is an alias. See Bugzilla 1454 | |
3700 | */ | |
3701 | TypeIdentifier *tid = new TypeIdentifier(tp->loc, tp->tempinst->name); | |
3702 | Type *tx; | |
3703 | Expression *e; | |
3704 | Dsymbol *s; | |
3705 | tid->resolve(tp->loc, sc, &e, &tx, &s); | |
3706 | if (tx) | |
3707 | { | |
3708 | s = tx->toDsymbol(sc); | |
3709 | if (TemplateInstance *ti = s ? s->parent->isTemplateInstance() : NULL) | |
3710 | { | |
3711 | // Bugzilla 14290: Try to match with ti->tempecl, | |
3712 | // only when ti is an enclosing instance. | |
3713 | Dsymbol *p = sc->parent; | |
3714 | while (p && p != ti) | |
3715 | p = p->parent; | |
3716 | if (p) | |
3717 | s = ti->tempdecl; | |
3718 | } | |
3719 | } | |
3720 | if (s) | |
3721 | { | |
3722 | s = s->toAlias(); | |
3723 | TemplateDeclaration *td = s->isTemplateDeclaration(); | |
3724 | if (td) | |
3725 | { | |
3726 | if (td->overroot) | |
3727 | td = td->overroot; | |
3728 | for (; td; td = td->overnext) | |
3729 | { | |
3730 | if (td == tempdecl) | |
3731 | goto L2; | |
3732 | } | |
3733 | } | |
3734 | } | |
3735 | goto Lnomatch; | |
3736 | } | |
3737 | TemplateParameter *tpx = (*parameters)[i]; | |
3738 | if (!tpx->matchArg(sc, tempdecl, i, parameters, dedtypes, NULL)) | |
3739 | goto Lnomatch; | |
3740 | } | |
3741 | else if (tempdecl != tp->tempinst->tempdecl) | |
3742 | goto Lnomatch; | |
3743 | ||
3744 | L2: | |
3745 | ||
3746 | for (size_t i = 0; 1; i++) | |
3747 | { | |
3748 | //printf("\ttest: tempinst->tiargs[%d]\n", i); | |
3749 | RootObject *o1 = NULL; | |
3750 | if (i < t->tempinst->tiargs->dim) | |
3751 | o1 = (*t->tempinst->tiargs)[i]; | |
3752 | else if (i < t->tempinst->tdtypes.dim && i < tp->tempinst->tiargs->dim) | |
3753 | { | |
3754 | // Pick up default arg | |
3755 | o1 = t->tempinst->tdtypes[i]; | |
3756 | } | |
3757 | else if (i >= tp->tempinst->tiargs->dim) | |
3758 | break; | |
3759 | ||
3760 | if (i >= tp->tempinst->tiargs->dim) | |
3761 | { | |
3762 | size_t dim = tempdecl->parameters->dim - (tempdecl->isVariadic() ? 1 : 0); | |
3763 | while (i < dim && ((*tempdecl->parameters)[i]->dependent || | |
3764 | (*tempdecl->parameters)[i]->hasDefaultArg())) | |
3765 | { | |
3766 | i++; | |
3767 | } | |
3768 | if (i >= dim) | |
3769 | break; // match if all remained parameters are dependent | |
3770 | goto Lnomatch; | |
3771 | } | |
3772 | ||
3773 | RootObject *o2 = (*tp->tempinst->tiargs)[i]; | |
3774 | Type *t2 = isType(o2); | |
3775 | ||
3776 | size_t j = (t2 && t2->ty == Tident && i == tp->tempinst->tiargs->dim - 1) | |
3777 | ? templateParameterLookup(t2, parameters) : IDX_NOTFOUND; | |
3778 | if (j != IDX_NOTFOUND && j == parameters->dim - 1 && | |
3779 | (*parameters)[j]->isTemplateTupleParameter()) | |
3780 | { | |
3781 | /* Given: | |
3782 | * struct A(B...) {} | |
3783 | * alias A!(int, float) X; | |
3784 | * static if (is(X Y == A!(Z), Z...)) {} | |
3785 | * deduce that Z is a tuple(int, float) | |
3786 | */ | |
3787 | ||
3788 | /* Create tuple from remaining args | |
3789 | */ | |
3790 | Tuple *vt = new Tuple(); | |
3791 | size_t vtdim = (tempdecl->isVariadic() | |
3792 | ? t->tempinst->tiargs->dim : t->tempinst->tdtypes.dim) - i; | |
3793 | vt->objects.setDim(vtdim); | |
3794 | for (size_t k = 0; k < vtdim; k++) | |
3795 | { | |
3796 | RootObject *o; | |
3797 | if (k < t->tempinst->tiargs->dim) | |
3798 | o = (*t->tempinst->tiargs)[i + k]; | |
3799 | else // Pick up default arg | |
3800 | o = t->tempinst->tdtypes[i + k]; | |
3801 | vt->objects[k] = o; | |
3802 | } | |
3803 | ||
3804 | Tuple *v = (Tuple *)(*dedtypes)[j]; | |
3805 | if (v) | |
3806 | { | |
3807 | if (!match(v, vt)) | |
3808 | goto Lnomatch; | |
3809 | } | |
3810 | else | |
3811 | (*dedtypes)[j] = vt; | |
3812 | break; | |
3813 | } | |
3814 | else if (!o1) | |
3815 | break; | |
3816 | ||
3817 | Type *t1 = isType(o1); | |
3818 | Dsymbol *s1 = isDsymbol(o1); | |
3819 | Dsymbol *s2 = isDsymbol(o2); | |
3820 | Expression *e1 = s1 ? getValue(s1) : getValue(isExpression(o1)); | |
3821 | Expression *e2 = isExpression(o2); | |
3822 | ||
3823 | if (t1 && t2) | |
3824 | { | |
3825 | if (!deduceType(t1, sc, t2, parameters, dedtypes)) | |
3826 | goto Lnomatch; | |
3827 | } | |
3828 | else if (e1 && e2) | |
3829 | { | |
3830 | Le: | |
3831 | e1 = e1->ctfeInterpret(); | |
3832 | ||
3833 | /* If it is one of the template parameters for this template, | |
3834 | * we should not attempt to interpret it. It already has a value. | |
3835 | */ | |
3836 | if (e2->op == TOKvar && | |
3837 | (((VarExp *)e2)->var->storage_class & STCtemplateparameter)) | |
3838 | { | |
3839 | /* | |
3840 | * (T:Number!(e2), int e2) | |
3841 | */ | |
3842 | j = templateIdentifierLookup(((VarExp *)e2)->var->ident, parameters); | |
3843 | if (j != IDX_NOTFOUND) | |
3844 | goto L1; | |
3845 | // The template parameter was not from this template | |
3846 | // (it may be from a parent template, for example) | |
3847 | } | |
3848 | ||
3849 | e2 = ::semantic(e2, sc); // Bugzilla 13417 | |
3850 | e2 = e2->ctfeInterpret(); | |
3851 | ||
3852 | //printf("e1 = %s, type = %s %d\n", e1->toChars(), e1->type->toChars(), e1->type->ty); | |
3853 | //printf("e2 = %s, type = %s %d\n", e2->toChars(), e2->type->toChars(), e2->type->ty); | |
3854 | if (!e1->equals(e2)) | |
3855 | { | |
3856 | if (!e2->implicitConvTo(e1->type)) | |
3857 | goto Lnomatch; | |
3858 | ||
3859 | e2 = e2->implicitCastTo(sc, e1->type); | |
3860 | e2 = e2->ctfeInterpret(); | |
3861 | if (!e1->equals(e2)) | |
3862 | goto Lnomatch; | |
3863 | } | |
3864 | } | |
3865 | else if (e1 && t2 && t2->ty == Tident) | |
3866 | { | |
3867 | j = templateParameterLookup(t2, parameters); | |
3868 | L1: | |
3869 | if (j == IDX_NOTFOUND) | |
3870 | { | |
3871 | t2->resolve(((TypeIdentifier *)t2)->loc, sc, &e2, &t2, &s2); | |
3872 | if (e2) | |
3873 | goto Le; | |
3874 | goto Lnomatch; | |
3875 | } | |
3876 | if (!(*parameters)[j]->matchArg(sc, e1, j, parameters, dedtypes, NULL)) | |
3877 | goto Lnomatch; | |
3878 | } | |
3879 | else if (s1 && s2) | |
3880 | { | |
3881 | Ls: | |
3882 | if (!s1->equals(s2)) | |
3883 | goto Lnomatch; | |
3884 | } | |
3885 | else if (s1 && t2 && t2->ty == Tident) | |
3886 | { | |
3887 | j = templateParameterLookup(t2, parameters); | |
3888 | if (j == IDX_NOTFOUND) | |
3889 | { | |
3890 | t2->resolve(((TypeIdentifier *)t2)->loc, sc, &e2, &t2, &s2); | |
3891 | if (s2) | |
3892 | goto Ls; | |
3893 | goto Lnomatch; | |
3894 | } | |
3895 | if (!(*parameters)[j]->matchArg(sc, s1, j, parameters, dedtypes, NULL)) | |
3896 | goto Lnomatch; | |
3897 | } | |
3898 | else | |
3899 | goto Lnomatch; | |
3900 | } | |
3901 | } | |
3902 | visit((Type *)t); | |
3903 | return; | |
3904 | ||
3905 | Lnomatch: | |
3906 | //printf("no match\n"); | |
3907 | result = MATCHnomatch; | |
3908 | } | |
3909 | ||
3910 | void visit(TypeStruct *t) | |
3911 | { | |
3912 | /* If this struct is a template struct, and we're matching | |
3913 | * it against a template instance, convert the struct type | |
3914 | * to a template instance, too, and try again. | |
3915 | */ | |
3916 | TemplateInstance *ti = t->sym->parent->isTemplateInstance(); | |
3917 | ||
3918 | if (tparam && tparam->ty == Tinstance) | |
3919 | { | |
3920 | if (ti && ti->toAlias() == t->sym) | |
3921 | { | |
3922 | TypeInstance *tx = new TypeInstance(Loc(), ti); | |
3923 | result = deduceType(tx, sc, tparam, parameters, dedtypes, wm); | |
3924 | return; | |
3925 | } | |
3926 | ||
3927 | /* Match things like: | |
3928 | * S!(T).foo | |
3929 | */ | |
3930 | TypeInstance *tpi = (TypeInstance *)tparam; | |
3931 | if (tpi->idents.dim) | |
3932 | { | |
3933 | RootObject *id = tpi->idents[tpi->idents.dim - 1]; | |
3934 | if (id->dyncast() == DYNCAST_IDENTIFIER && t->sym->ident->equals((Identifier *)id)) | |
3935 | { | |
3936 | Type *tparent = t->sym->parent->getType(); | |
3937 | if (tparent) | |
3938 | { | |
3939 | /* Slice off the .foo in S!(T).foo | |
3940 | */ | |
3941 | tpi->idents.dim--; | |
3942 | result = deduceType(tparent, sc, tpi, parameters, dedtypes, wm); | |
3943 | tpi->idents.dim++; | |
3944 | return; | |
3945 | } | |
3946 | } | |
3947 | } | |
3948 | } | |
3949 | ||
3950 | // Extra check | |
3951 | if (tparam && tparam->ty == Tstruct) | |
3952 | { | |
3953 | TypeStruct *tp = (TypeStruct *)tparam; | |
3954 | ||
3955 | //printf("\t%d\n", (MATCH) t->implicitConvTo(tp)); | |
3956 | if (wm && t->deduceWild(tparam, false)) | |
3957 | { | |
3958 | result = MATCHconst; | |
3959 | return; | |
3960 | } | |
3961 | result = t->implicitConvTo(tp); | |
3962 | return; | |
3963 | } | |
3964 | visit((Type *)t); | |
3965 | } | |
3966 | ||
3967 | void visit(TypeEnum *t) | |
3968 | { | |
3969 | // Extra check | |
3970 | if (tparam && tparam->ty == Tenum) | |
3971 | { | |
3972 | TypeEnum *tp = (TypeEnum *)tparam; | |
3973 | if (t->sym == tp->sym) | |
3974 | visit((Type *)t); | |
3975 | else | |
3976 | result = MATCHnomatch; | |
3977 | return; | |
3978 | } | |
3979 | Type *tb = t->toBasetype(); | |
3980 | if (tb->ty == tparam->ty || | |
3981 | (tb->ty == Tsarray && tparam->ty == Taarray)) | |
3982 | { | |
3983 | result = deduceType(tb, sc, tparam, parameters, dedtypes, wm); | |
3984 | return; | |
3985 | } | |
3986 | visit((Type *)t); | |
3987 | } | |
3988 | ||
3989 | /* Helper for TypeClass::deduceType(). | |
3990 | * Classes can match with implicit conversion to a base class or interface. | |
3991 | * This is complicated, because there may be more than one base class which | |
3992 | * matches. In such cases, one or more parameters remain ambiguous. | |
3993 | * For example, | |
3994 | * | |
3995 | * interface I(X, Y) {} | |
3996 | * class C : I(uint, double), I(char, double) {} | |
3997 | * C x; | |
3998 | * foo(T, U)( I!(T, U) x) | |
3999 | * | |
4000 | * deduces that U is double, but T remains ambiguous (could be char or uint). | |
4001 | * | |
4002 | * Given a baseclass b, and initial deduced types 'dedtypes', this function | |
4003 | * tries to match tparam with b, and also tries all base interfaces of b. | |
4004 | * If a match occurs, numBaseClassMatches is incremented, and the new deduced | |
4005 | * types are ANDed with the current 'best' estimate for dedtypes. | |
4006 | */ | |
4007 | static void deduceBaseClassParameters(BaseClass *b, | |
4008 | Scope *sc, Type *tparam, TemplateParameters *parameters, Objects *dedtypes, | |
4009 | Objects *best, int &numBaseClassMatches) | |
4010 | { | |
4011 | TemplateInstance *parti = b->sym ? b->sym->parent->isTemplateInstance() : NULL; | |
4012 | if (parti) | |
4013 | { | |
4014 | // Make a temporary copy of dedtypes so we don't destroy it | |
4015 | Objects *tmpdedtypes = new Objects(); | |
4016 | tmpdedtypes->setDim(dedtypes->dim); | |
4017 | memcpy(tmpdedtypes->tdata(), dedtypes->tdata(), dedtypes->dim * sizeof(void *)); | |
4018 | ||
4019 | TypeInstance *t = new TypeInstance(Loc(), parti); | |
4020 | MATCH m = deduceType(t, sc, tparam, parameters, tmpdedtypes); | |
4021 | if (m > MATCHnomatch) | |
4022 | { | |
4023 | // If this is the first ever match, it becomes our best estimate | |
4024 | if (numBaseClassMatches==0) | |
4025 | memcpy(best->tdata(), tmpdedtypes->tdata(), tmpdedtypes->dim * sizeof(void *)); | |
4026 | else for (size_t k = 0; k < tmpdedtypes->dim; ++k) | |
4027 | { | |
4028 | // If we've found more than one possible type for a parameter, | |
4029 | // mark it as unknown. | |
4030 | if ((*tmpdedtypes)[k] != (*best)[k]) | |
4031 | (*best)[k] = (*dedtypes)[k]; | |
4032 | } | |
4033 | ++numBaseClassMatches; | |
4034 | } | |
4035 | } | |
4036 | // Now recursively test the inherited interfaces | |
4037 | for (size_t j = 0; j < b->baseInterfaces.length; ++j) | |
4038 | { | |
4039 | BaseClass *bi = &b->baseInterfaces.ptr[j]; | |
4040 | deduceBaseClassParameters(bi, | |
4041 | sc, tparam, parameters, dedtypes, | |
4042 | best, numBaseClassMatches); | |
4043 | } | |
4044 | ||
4045 | } | |
4046 | ||
4047 | void visit(TypeClass *t) | |
4048 | { | |
4049 | //printf("TypeClass::deduceType(this = %s)\n", t->toChars()); | |
4050 | ||
4051 | /* If this class is a template class, and we're matching | |
4052 | * it against a template instance, convert the class type | |
4053 | * to a template instance, too, and try again. | |
4054 | */ | |
4055 | TemplateInstance *ti = t->sym->parent->isTemplateInstance(); | |
4056 | ||
4057 | if (tparam && tparam->ty == Tinstance) | |
4058 | { | |
4059 | if (ti && ti->toAlias() == t->sym) | |
4060 | { | |
4061 | TypeInstance *tx = new TypeInstance(Loc(), ti); | |
4062 | MATCH m = deduceType(tx, sc, tparam, parameters, dedtypes, wm); | |
4063 | // Even if the match fails, there is still a chance it could match | |
4064 | // a base class. | |
4065 | if (m != MATCHnomatch) | |
4066 | { | |
4067 | result = m; | |
4068 | return; | |
4069 | } | |
4070 | } | |
4071 | ||
4072 | /* Match things like: | |
4073 | * S!(T).foo | |
4074 | */ | |
4075 | TypeInstance *tpi = (TypeInstance *)tparam; | |
4076 | if (tpi->idents.dim) | |
4077 | { | |
4078 | RootObject *id = tpi->idents[tpi->idents.dim - 1]; | |
4079 | if (id->dyncast() == DYNCAST_IDENTIFIER && t->sym->ident->equals((Identifier *)id)) | |
4080 | { | |
4081 | Type *tparent = t->sym->parent->getType(); | |
4082 | if (tparent) | |
4083 | { | |
4084 | /* Slice off the .foo in S!(T).foo | |
4085 | */ | |
4086 | tpi->idents.dim--; | |
4087 | result = deduceType(tparent, sc, tpi, parameters, dedtypes, wm); | |
4088 | tpi->idents.dim++; | |
4089 | return; | |
4090 | } | |
4091 | } | |
4092 | } | |
4093 | ||
4094 | // If it matches exactly or via implicit conversion, we're done | |
4095 | visit((Type *)t); | |
4096 | if (result != MATCHnomatch) | |
4097 | return; | |
4098 | ||
4099 | /* There is still a chance to match via implicit conversion to | |
4100 | * a base class or interface. Because there could be more than one such | |
4101 | * match, we need to check them all. | |
4102 | */ | |
4103 | ||
4104 | int numBaseClassMatches = 0; // Have we found an interface match? | |
4105 | ||
4106 | // Our best guess at dedtypes | |
4107 | Objects *best = new Objects(); | |
4108 | best->setDim(dedtypes->dim); | |
4109 | ||
4110 | ClassDeclaration *s = t->sym; | |
4111 | while (s && s->baseclasses->dim > 0) | |
4112 | { | |
4113 | // Test the base class | |
4114 | deduceBaseClassParameters((*s->baseclasses)[0], | |
4115 | sc, tparam, parameters, dedtypes, | |
4116 | best, numBaseClassMatches); | |
4117 | ||
4118 | // Test the interfaces inherited by the base class | |
4119 | for (size_t i = 0; i < s->interfaces.length; ++i) | |
4120 | { | |
4121 | BaseClass *b = s->interfaces.ptr[i]; | |
4122 | deduceBaseClassParameters(b, sc, tparam, parameters, dedtypes, | |
4123 | best, numBaseClassMatches); | |
4124 | } | |
4125 | s = (*s->baseclasses)[0]->sym; | |
4126 | } | |
4127 | ||
4128 | if (numBaseClassMatches == 0) | |
4129 | { | |
4130 | result = MATCHnomatch; | |
4131 | return; | |
4132 | } | |
4133 | ||
4134 | // If we got at least one match, copy the known types into dedtypes | |
4135 | memcpy(dedtypes->tdata(), best->tdata(), best->dim * sizeof(void *)); | |
4136 | result = MATCHconvert; | |
4137 | return; | |
4138 | } | |
4139 | ||
4140 | // Extra check | |
4141 | if (tparam && tparam->ty == Tclass) | |
4142 | { | |
4143 | TypeClass *tp = (TypeClass *)tparam; | |
4144 | ||
4145 | //printf("\t%d\n", (MATCH) t->implicitConvTo(tp)); | |
4146 | if (wm && t->deduceWild(tparam, false)) | |
4147 | { | |
4148 | result = MATCHconst; | |
4149 | return; | |
4150 | } | |
4151 | result = t->implicitConvTo(tp); | |
4152 | return; | |
4153 | } | |
4154 | visit((Type *)t); | |
4155 | } | |
4156 | ||
4157 | void visit(Expression *e) | |
4158 | { | |
4159 | //printf("Expression::deduceType(e = %s)\n", e->toChars()); | |
4160 | size_t i = templateParameterLookup(tparam, parameters); | |
4161 | if (i == IDX_NOTFOUND || ((TypeIdentifier *)tparam)->idents.dim > 0) | |
4162 | { | |
4163 | if (e == emptyArrayElement && tparam->ty == Tarray) | |
4164 | { | |
4165 | Type *tn = ((TypeNext *)tparam)->next; | |
4166 | result = deduceType(emptyArrayElement, sc, tn, parameters, dedtypes, wm); | |
4167 | return; | |
4168 | } | |
4169 | e->type->accept(this); | |
4170 | return; | |
4171 | } | |
4172 | ||
4173 | TemplateTypeParameter *tp = (*parameters)[i]->isTemplateTypeParameter(); | |
4174 | if (!tp) | |
4175 | return; // nomatch | |
4176 | ||
4177 | if (e == emptyArrayElement) | |
4178 | { | |
4179 | if ((*dedtypes)[i]) | |
4180 | { | |
4181 | result = MATCHexact; | |
4182 | return; | |
4183 | } | |
4184 | if (tp->defaultType) | |
4185 | { | |
4186 | tp->defaultType->accept(this); | |
4187 | return; | |
4188 | } | |
4189 | } | |
4190 | ||
4191 | Type *at = (Type *)(*dedtypes)[i]; | |
4192 | Type *tt; | |
4193 | if (unsigned char wx = deduceWildHelper(e->type, &tt, tparam)) | |
4194 | { | |
4195 | *wm |= wx; | |
4196 | result = MATCHconst; | |
4197 | } | |
4198 | else if (MATCH m = deduceTypeHelper(e->type, &tt, tparam)) | |
4199 | { | |
4200 | result = m; | |
4201 | } | |
4202 | else | |
4203 | return; // nomatch | |
4204 | ||
4205 | // expression vs (none) | |
4206 | if (!at) | |
4207 | { | |
4208 | (*dedtypes)[i] = new TypeDeduced(tt, e, tparam); | |
4209 | return; | |
4210 | } | |
4211 | ||
4212 | TypeDeduced *xt = NULL; | |
4213 | if (at->ty == Tnone) | |
4214 | { | |
4215 | xt = (TypeDeduced *)at; | |
4216 | at = xt->tded; | |
4217 | } | |
4218 | ||
4219 | // From previous matched expressions to current deduced type | |
4220 | MATCH match1 = xt ? xt->matchAll(tt) : MATCHnomatch; | |
4221 | ||
4222 | // From current expresssion to previous deduced type | |
4223 | Type *pt = at->addMod(tparam->mod); | |
4224 | if (*wm) | |
4225 | pt = pt->substWildTo(*wm); | |
4226 | MATCH match2 = e->implicitConvTo(pt); | |
4227 | ||
4228 | if (match1 > MATCHnomatch && match2 > MATCHnomatch) | |
4229 | { | |
4230 | if (at->implicitConvTo(tt) <= MATCHnomatch) | |
4231 | match1 = MATCHnomatch; // Prefer at | |
4232 | else if (tt->implicitConvTo(at) <= MATCHnomatch) | |
4233 | match2 = MATCHnomatch; // Prefer tt | |
4234 | else if (tt->isTypeBasic() && tt->ty == at->ty && tt->mod != at->mod) | |
4235 | { | |
4236 | if (!tt->isMutable() && !at->isMutable()) | |
4237 | tt = tt->mutableOf()->addMod(MODmerge(tt->mod, at->mod)); | |
4238 | else if (tt->isMutable()) | |
4239 | { | |
4240 | if (at->mod == 0) // Prefer unshared | |
4241 | match1 = MATCHnomatch; | |
4242 | else | |
4243 | match2 = MATCHnomatch; | |
4244 | } | |
4245 | else if (at->isMutable()) | |
4246 | { | |
4247 | if (tt->mod == 0) // Prefer unshared | |
4248 | match2 = MATCHnomatch; | |
4249 | else | |
4250 | match1 = MATCHnomatch; | |
4251 | } | |
4252 | //printf("tt = %s, at = %s\n", tt->toChars(), at->toChars()); | |
4253 | } | |
4254 | else | |
4255 | { | |
4256 | match1 = MATCHnomatch; | |
4257 | match2 = MATCHnomatch; | |
4258 | } | |
4259 | } | |
4260 | if (match1 > MATCHnomatch) | |
4261 | { | |
4262 | // Prefer current match: tt | |
4263 | if (xt) | |
4264 | xt->update(tt, e, tparam); | |
4265 | else | |
4266 | (*dedtypes)[i] = tt; | |
4267 | result = match1; | |
4268 | return; | |
4269 | } | |
4270 | if (match2 > MATCHnomatch) | |
4271 | { | |
4272 | // Prefer previous match: (*dedtypes)[i] | |
4273 | if (xt) | |
4274 | xt->update(e, tparam); | |
4275 | result = match2; | |
4276 | return; | |
4277 | } | |
4278 | ||
4279 | /* Deduce common type | |
4280 | */ | |
4281 | if (Type *t = rawTypeMerge(at, tt)) | |
4282 | { | |
4283 | if (xt) | |
4284 | xt->update(t, e, tparam); | |
4285 | else | |
4286 | (*dedtypes)[i] = t; | |
4287 | ||
4288 | pt = tt->addMod(tparam->mod); | |
4289 | if (*wm) | |
4290 | pt = pt->substWildTo(*wm); | |
4291 | result = e->implicitConvTo(pt); | |
4292 | return; | |
4293 | } | |
4294 | ||
4295 | result = MATCHnomatch; | |
4296 | } | |
4297 | ||
4298 | MATCH deduceEmptyArrayElement() | |
4299 | { | |
4300 | if (!emptyArrayElement) | |
4301 | { | |
4302 | emptyArrayElement = new IdentifierExp(Loc(), Id::p); // dummy | |
4303 | emptyArrayElement->type = Type::tvoid; | |
4304 | } | |
4305 | assert(tparam->ty == Tarray); | |
4306 | ||
4307 | Type *tn = ((TypeNext *)tparam)->next; | |
4308 | return deduceType(emptyArrayElement, sc, tn, parameters, dedtypes, wm); | |
4309 | } | |
4310 | ||
4311 | void visit(NullExp *e) | |
4312 | { | |
4313 | if (tparam->ty == Tarray && e->type->ty == Tnull) | |
4314 | { | |
4315 | // tparam:T[] <- e:null (void[]) | |
4316 | result = deduceEmptyArrayElement(); | |
4317 | return; | |
4318 | } | |
4319 | visit((Expression *)e); | |
4320 | } | |
4321 | ||
4322 | void visit(StringExp *e) | |
4323 | { | |
4324 | Type *taai; | |
4325 | if (e->type->ty == Tarray && | |
4326 | (tparam->ty == Tsarray || | |
4327 | (tparam->ty == Taarray && (taai = ((TypeAArray *)tparam)->index)->ty == Tident && | |
4328 | ((TypeIdentifier *)taai)->idents.dim == 0))) | |
4329 | { | |
4330 | // Consider compile-time known boundaries | |
4331 | e->type->nextOf()->sarrayOf(e->len)->accept(this); | |
4332 | return; | |
4333 | } | |
4334 | visit((Expression *)e); | |
4335 | } | |
4336 | ||
4337 | void visit(ArrayLiteralExp *e) | |
4338 | { | |
4339 | if ((!e->elements || !e->elements->dim) && | |
4340 | e->type->toBasetype()->nextOf()->ty == Tvoid && | |
4341 | tparam->ty == Tarray) | |
4342 | { | |
4343 | // tparam:T[] <- e:[] (void[]) | |
4344 | result = deduceEmptyArrayElement(); | |
4345 | return; | |
4346 | } | |
4347 | ||
4348 | if (tparam->ty == Tarray && e->elements && e->elements->dim) | |
4349 | { | |
4350 | Type *tn = ((TypeDArray *)tparam)->next; | |
4351 | result = MATCHexact; | |
4352 | if (e->basis) | |
4353 | { | |
4354 | MATCH m = deduceType(e->basis, sc, tn, parameters, dedtypes, wm); | |
4355 | if (m < result) | |
4356 | result = m; | |
4357 | } | |
4358 | for (size_t i = 0; i < e->elements->dim; i++) | |
4359 | { | |
4360 | if (result <= MATCHnomatch) | |
4361 | break; | |
4362 | Expression *el = (*e->elements)[i]; | |
4363 | if (!el) | |
4364 | continue; | |
4365 | MATCH m = deduceType(el, sc, tn, parameters, dedtypes, wm); | |
4366 | if (m < result) | |
4367 | result = m; | |
4368 | } | |
4369 | return; | |
4370 | } | |
4371 | ||
4372 | Type *taai; | |
4373 | if (e->type->ty == Tarray && | |
4374 | (tparam->ty == Tsarray || | |
4375 | (tparam->ty == Taarray && (taai = ((TypeAArray *)tparam)->index)->ty == Tident && | |
4376 | ((TypeIdentifier *)taai)->idents.dim == 0))) | |
4377 | { | |
4378 | // Consider compile-time known boundaries | |
4379 | e->type->nextOf()->sarrayOf(e->elements->dim)->accept(this); | |
4380 | return; | |
4381 | } | |
4382 | visit((Expression *)e); | |
4383 | } | |
4384 | ||
4385 | void visit(AssocArrayLiteralExp *e) | |
4386 | { | |
4387 | if (tparam->ty == Taarray && e->keys && e->keys->dim) | |
4388 | { | |
4389 | TypeAArray *taa = (TypeAArray *)tparam; | |
4390 | result = MATCHexact; | |
4391 | for (size_t i = 0; i < e->keys->dim; i++) | |
4392 | { | |
4393 | MATCH m1 = deduceType((*e->keys)[i], sc, taa->index, parameters, dedtypes, wm); | |
4394 | if (m1 < result) | |
4395 | result = m1; | |
4396 | if (result <= MATCHnomatch) | |
4397 | break; | |
4398 | MATCH m2 = deduceType((*e->values)[i], sc, taa->next, parameters, dedtypes, wm); | |
4399 | if (m2 < result) | |
4400 | result = m2; | |
4401 | if (result <= MATCHnomatch) | |
4402 | break; | |
4403 | } | |
4404 | return; | |
4405 | } | |
4406 | visit((Expression *)e); | |
4407 | } | |
4408 | ||
4409 | void visit(FuncExp *e) | |
4410 | { | |
4411 | //printf("e->type = %s, tparam = %s\n", e->type->toChars(), tparam->toChars()); | |
4412 | if (e->td) | |
4413 | { | |
4414 | Type *to = tparam; | |
4415 | if (!to->nextOf() || to->nextOf()->ty != Tfunction) | |
4416 | return; | |
4417 | TypeFunction *tof = (TypeFunction *)to->nextOf(); | |
4418 | ||
4419 | // Parameter types inference from 'tof' | |
4420 | assert(e->td->_scope); | |
4421 | TypeFunction *tf = (TypeFunction *)e->fd->type; | |
4422 | //printf("\ttof = %s\n", tof->toChars()); | |
4423 | //printf("\ttf = %s\n", tf->toChars()); | |
4424 | size_t dim = Parameter::dim(tf->parameters); | |
4425 | ||
4426 | if (Parameter::dim(tof->parameters) != dim || | |
4427 | tof->varargs != tf->varargs) | |
4428 | return; | |
4429 | ||
4430 | Objects *tiargs = new Objects(); | |
4431 | tiargs->reserve(e->td->parameters->dim); | |
4432 | ||
4433 | for (size_t i = 0; i < e->td->parameters->dim; i++) | |
4434 | { | |
4435 | TemplateParameter *tp = (*e->td->parameters)[i]; | |
4436 | size_t u = 0; | |
4437 | for (; u < dim; u++) | |
4438 | { | |
4439 | Parameter *p = Parameter::getNth(tf->parameters, u); | |
4440 | if (p->type->ty == Tident && | |
4441 | ((TypeIdentifier *)p->type)->ident == tp->ident) | |
4442 | { | |
4443 | break; | |
4444 | } | |
4445 | } | |
4446 | assert(u < dim); | |
4447 | Parameter *pto = Parameter::getNth(tof->parameters, u); | |
4448 | if (!pto) | |
4449 | break; | |
4450 | Type *t = pto->type->syntaxCopy(); // Bugzilla 11774 | |
4451 | if (reliesOnTident(t, parameters, inferStart)) | |
4452 | return; | |
4453 | t = t->semantic(e->loc, sc); | |
4454 | if (t->ty == Terror) | |
4455 | return; | |
4456 | tiargs->push(t); | |
4457 | } | |
4458 | ||
4459 | // Set target of return type inference | |
4460 | if (!tf->next && tof->next) | |
4461 | e->fd->treq = tparam; | |
4462 | ||
4463 | TemplateInstance *ti = new TemplateInstance(e->loc, e->td, tiargs); | |
4464 | Expression *ex = new ScopeExp(e->loc, ti); | |
4465 | ex = ::semantic(ex, e->td->_scope); | |
4466 | ||
4467 | // Reset inference target for the later re-semantic | |
4468 | e->fd->treq = NULL; | |
4469 | ||
4470 | if (ex->op == TOKerror) | |
4471 | return; | |
4472 | if (ex->op != TOKfunction) | |
4473 | return; | |
4474 | visit(ex->type); | |
4475 | return; | |
4476 | } | |
4477 | ||
4478 | Type *t = e->type; | |
4479 | ||
4480 | if (t->ty == Tdelegate && tparam->ty == Tpointer) | |
4481 | return; | |
4482 | ||
4483 | // Allow conversion from implicit function pointer to delegate | |
4484 | if (e->tok == TOKreserved && | |
4485 | t->ty == Tpointer && tparam->ty == Tdelegate) | |
4486 | { | |
4487 | TypeFunction *tf = (TypeFunction *)t->nextOf(); | |
4488 | t = (new TypeDelegate(tf))->merge(); | |
4489 | } | |
4490 | //printf("tparam = %s <= e->type = %s, t = %s\n", tparam->toChars(), e->type->toChars(), t->toChars()); | |
4491 | visit(t); | |
4492 | } | |
4493 | ||
4494 | void visit(SliceExp *e) | |
4495 | { | |
4496 | Type *taai; | |
4497 | if (e->type->ty == Tarray && | |
4498 | (tparam->ty == Tsarray || | |
4499 | (tparam->ty == Taarray && (taai = ((TypeAArray *)tparam)->index)->ty == Tident && | |
4500 | ((TypeIdentifier *)taai)->idents.dim == 0))) | |
4501 | { | |
4502 | // Consider compile-time known boundaries | |
4503 | if (Type *tsa = toStaticArrayType(e)) | |
4504 | { | |
4505 | tsa->accept(this); | |
4506 | return; | |
4507 | } | |
4508 | } | |
4509 | visit((Expression *)e); | |
4510 | } | |
4511 | ||
4512 | void visit(CommaExp *e) | |
4513 | { | |
4514 | ((CommaExp *)e)->e2->accept(this); | |
4515 | } | |
4516 | }; | |
4517 | ||
4518 | DeduceType v(sc, tparam, parameters, dedtypes, wm, inferStart); | |
4519 | if (Type *t = isType(o)) | |
4520 | t->accept(&v); | |
4521 | else | |
4522 | { | |
4523 | assert(isExpression(o) && wm); | |
4524 | ((Expression *)o)->accept(&v); | |
4525 | } | |
4526 | return v.result; | |
4527 | } | |
4528 | ||
4529 | /******************************* | |
4530 | * Input: | |
4531 | * t Tested type, if NULL, returns NULL. | |
4532 | * tparams Optional template parameters. | |
4533 | * == NULL: | |
4534 | * If one of the subtypes of this type is a TypeIdentifier, | |
4535 | * i.e. it's an unresolved type, return that type. | |
4536 | * != NULL: | |
4537 | * Only when the TypeIdentifier is one of template parameters, | |
4538 | * return that type. | |
4539 | */ | |
4540 | ||
4541 | bool reliesOnTident(Type *t, TemplateParameters *tparams, size_t iStart) | |
4542 | { | |
4543 | class ReliesOnTident : public Visitor | |
4544 | { | |
4545 | public: | |
4546 | TemplateParameters *tparams; | |
4547 | size_t iStart; | |
4548 | bool result; | |
4549 | ||
4550 | ReliesOnTident(TemplateParameters *tparams, size_t iStart) | |
4551 | : tparams(tparams), iStart(iStart) | |
4552 | { | |
4553 | result = false; | |
4554 | } | |
4555 | ||
4556 | void visit(Type *) | |
4557 | { | |
4558 | } | |
4559 | ||
4560 | void visit(TypeNext *t) | |
4561 | { | |
4562 | t->next->accept(this); | |
4563 | } | |
4564 | ||
4565 | void visit(TypeVector *t) | |
4566 | { | |
4567 | t->basetype->accept(this); | |
4568 | } | |
4569 | ||
4570 | void visit(TypeAArray *t) | |
4571 | { | |
4572 | visit((TypeNext *)t); | |
4573 | if (!result) | |
4574 | t->index->accept(this); | |
4575 | } | |
4576 | ||
4577 | void visit(TypeFunction *t) | |
4578 | { | |
4579 | size_t dim = Parameter::dim(t->parameters); | |
4580 | for (size_t i = 0; i < dim; i++) | |
4581 | { | |
4582 | Parameter *fparam = Parameter::getNth(t->parameters, i); | |
4583 | fparam->type->accept(this); | |
4584 | if (result) | |
4585 | return; | |
4586 | } | |
4587 | if (t->next) | |
4588 | t->next->accept(this); | |
4589 | } | |
4590 | ||
4591 | void visit(TypeIdentifier *t) | |
4592 | { | |
4593 | if (!tparams) | |
4594 | { | |
4595 | result = true; | |
4596 | return; | |
4597 | } | |
4598 | ||
4599 | for (size_t i = iStart; i < tparams->dim; i++) | |
4600 | { | |
4601 | TemplateParameter *tp = (*tparams)[i]; | |
4602 | if (tp->ident->equals(t->ident)) | |
4603 | { | |
4604 | result = true; | |
4605 | return; | |
4606 | } | |
4607 | } | |
4608 | } | |
4609 | ||
4610 | void visit(TypeInstance *t) | |
4611 | { | |
4612 | if (!tparams) | |
4613 | return; | |
4614 | ||
4615 | for (size_t i = iStart; i < tparams->dim; i++) | |
4616 | { | |
4617 | TemplateParameter *tp = (*tparams)[i]; | |
4618 | if (t->tempinst->name == tp->ident) | |
4619 | { | |
4620 | result = true; | |
4621 | return; | |
4622 | } | |
4623 | } | |
4624 | if (!t->tempinst->tiargs) | |
4625 | return; | |
4626 | for (size_t i = 0; i < t->tempinst->tiargs->dim; i++) | |
4627 | { | |
4628 | Type *ta = isType((*t->tempinst->tiargs)[i]); | |
4629 | if (ta) | |
4630 | { | |
4631 | ta->accept(this); | |
4632 | if (result) | |
4633 | return; | |
4634 | } | |
4635 | } | |
4636 | } | |
4637 | ||
4638 | void visit(TypeTypeof *t) | |
4639 | { | |
4640 | //printf("TypeTypeof::reliesOnTident('%s')\n", t->toChars()); | |
4641 | t->exp->accept(this); | |
4642 | } | |
4643 | ||
4644 | void visit(TypeTuple *t) | |
4645 | { | |
4646 | if (t->arguments) | |
4647 | { | |
4648 | for (size_t i = 0; i < t->arguments->dim; i++) | |
4649 | { | |
4650 | Parameter *arg = (*t->arguments)[i]; | |
4651 | arg->type->accept(this); | |
4652 | if (result) | |
4653 | return; | |
4654 | } | |
4655 | } | |
4656 | } | |
4657 | ||
4658 | void visit(Expression *) | |
4659 | { | |
4660 | //printf("Expression::reliesOnTident('%s')\n", e->toChars()); | |
4661 | } | |
4662 | ||
4663 | void visit(IdentifierExp *e) | |
4664 | { | |
4665 | //printf("IdentifierExp::reliesOnTident('%s')\n", e->toChars()); | |
4666 | for (size_t i = iStart; i < tparams->dim; i++) | |
4667 | { | |
4668 | TemplateParameter *tp = (*tparams)[i]; | |
4669 | if (e->ident == tp->ident) | |
4670 | { | |
4671 | result = true; | |
4672 | return; | |
4673 | } | |
4674 | } | |
4675 | } | |
4676 | ||
4677 | void visit(TupleExp *e) | |
4678 | { | |
4679 | //printf("TupleExp::reliesOnTident('%s')\n", e->toChars()); | |
4680 | if (e->exps) | |
4681 | { | |
4682 | for (size_t i = 0; i < e->exps->dim; i++) | |
4683 | { | |
4684 | Expression *ea = (*e->exps)[i]; | |
4685 | ea->accept(this); | |
4686 | if (result) | |
4687 | return; | |
4688 | } | |
4689 | } | |
4690 | } | |
4691 | ||
4692 | void visit(ArrayLiteralExp *e) | |
4693 | { | |
4694 | //printf("ArrayLiteralExp::reliesOnTident('%s')\n", e->toChars()); | |
4695 | if (e->elements) | |
4696 | { | |
4697 | for (size_t i = 0; i < e->elements->dim; i++) | |
4698 | { | |
4699 | Expression *el = (*e->elements)[i]; | |
4700 | el->accept(this); | |
4701 | if (result) | |
4702 | return; | |
4703 | } | |
4704 | } | |
4705 | } | |
4706 | ||
4707 | void visit(AssocArrayLiteralExp *e) | |
4708 | { | |
4709 | //printf("AssocArrayLiteralExp::reliesOnTident('%s')\n", e->toChars()); | |
4710 | for (size_t i = 0; i < e->keys->dim; i++) | |
4711 | { | |
4712 | Expression *ek = (*e->keys)[i]; | |
4713 | ek->accept(this); | |
4714 | if (result) | |
4715 | return; | |
4716 | } | |
4717 | for (size_t i = 0; i < e->values->dim; i++) | |
4718 | { | |
4719 | Expression *ev = (*e->values)[i]; | |
4720 | ev->accept(this); | |
4721 | if (result) | |
4722 | return; | |
4723 | } | |
4724 | } | |
4725 | ||
4726 | void visit(StructLiteralExp *e) | |
4727 | { | |
4728 | //printf("StructLiteralExp::reliesOnTident('%s')\n", e->toChars()); | |
4729 | if (e->elements) | |
4730 | { | |
4731 | for (size_t i = 0; i < e->elements->dim; i++) | |
4732 | { | |
4733 | Expression *ea = (*e->elements)[i]; | |
4734 | ea->accept(this); | |
4735 | if (result) | |
4736 | return; | |
4737 | } | |
4738 | } | |
4739 | } | |
4740 | ||
4741 | void visit(TypeExp *e) | |
4742 | { | |
4743 | //printf("TypeExp::reliesOnTident('%s')\n", e->toChars()); | |
4744 | e->type->accept(this); | |
4745 | } | |
4746 | ||
4747 | void visit(NewExp *e) | |
4748 | { | |
4749 | //printf("NewExp::reliesOnTident('%s')\n", e->toChars()); | |
4750 | if (e->thisexp) | |
4751 | e->thisexp->accept(this); | |
4752 | if (!result && e->newargs) | |
4753 | { | |
4754 | for (size_t i = 0; i < e->newargs->dim; i++) | |
4755 | { | |
4756 | Expression *ea = (*e->newargs)[i]; | |
4757 | ea->accept(this); | |
4758 | if (result) | |
4759 | return; | |
4760 | } | |
4761 | } | |
4762 | e->newtype->accept(this); | |
4763 | if (!result && e->arguments) | |
4764 | { | |
4765 | for (size_t i = 0; i < e->arguments->dim; i++) | |
4766 | { | |
4767 | Expression *ea = (*e->arguments)[i]; | |
4768 | ea->accept(this); | |
4769 | if (result) | |
4770 | return; | |
4771 | } | |
4772 | } | |
4773 | } | |
4774 | ||
4775 | void visit(NewAnonClassExp *) | |
4776 | { | |
4777 | //printf("NewAnonClassExp::reliesOnTident('%s')\n", e->toChars()); | |
4778 | result = true; | |
4779 | } | |
4780 | ||
4781 | void visit(FuncExp *) | |
4782 | { | |
4783 | //printf("FuncExp::reliesOnTident('%s')\n", e->toChars()); | |
4784 | result = true; | |
4785 | } | |
4786 | ||
4787 | void visit(TypeidExp *e) | |
4788 | { | |
4789 | //printf("TypeidExp::reliesOnTident('%s')\n", e->toChars()); | |
4790 | if (Expression *ea = isExpression(e->obj)) | |
4791 | ea->accept(this); | |
4792 | else if (Type *ta = isType(e->obj)) | |
4793 | ta->accept(this); | |
4794 | } | |
4795 | ||
4796 | void visit(TraitsExp *e) | |
4797 | { | |
4798 | //printf("TraitsExp::reliesOnTident('%s')\n", e->toChars()); | |
4799 | if (e->args) | |
4800 | { | |
4801 | for (size_t i = 0; i < e->args->dim; i++) | |
4802 | { | |
4803 | RootObject *oa = (*e->args)[i]; | |
4804 | if (Expression *ea = isExpression(oa)) | |
4805 | ea->accept(this); | |
4806 | else if (Type *ta = isType(oa)) | |
4807 | ta->accept(this); | |
4808 | if (result) | |
4809 | return; | |
4810 | } | |
4811 | } | |
4812 | } | |
4813 | ||
4814 | void visit(IsExp *e) | |
4815 | { | |
4816 | //printf("IsExp::reliesOnTident('%s')\n", e->toChars()); | |
4817 | e->targ->accept(this); | |
4818 | } | |
4819 | ||
4820 | void visit(UnaExp *e) | |
4821 | { | |
4822 | //printf("UnaExp::reliesOnTident('%s')\n", e->toChars()); | |
4823 | e->e1->accept(this); | |
4824 | } | |
4825 | ||
4826 | void visit(DotTemplateInstanceExp *e) | |
4827 | { | |
4828 | //printf("DotTemplateInstanceExp::reliesOnTident('%s')\n", e->toChars()); | |
4829 | visit((UnaExp *)e); | |
4830 | if (!result && e->ti->tiargs) | |
4831 | { | |
4832 | for (size_t i = 0; i < e->ti->tiargs->dim; i++) | |
4833 | { | |
4834 | RootObject *oa = (*e->ti->tiargs)[i]; | |
4835 | if (Expression *ea = isExpression(oa)) | |
4836 | ea->accept(this); | |
4837 | else if (Type *ta = isType(oa)) | |
4838 | ta->accept(this); | |
4839 | if (result) | |
4840 | return; | |
4841 | } | |
4842 | } | |
4843 | } | |
4844 | ||
4845 | void visit(CallExp *e) | |
4846 | { | |
4847 | //printf("CallExp::reliesOnTident('%s')\n", e->toChars()); | |
4848 | visit((UnaExp *)e); | |
4849 | if (!result && e->arguments) | |
4850 | { | |
4851 | for (size_t i = 0; i < e->arguments->dim; i++) | |
4852 | { | |
4853 | Expression *ea = (*e->arguments)[i]; | |
4854 | ea->accept(this); | |
4855 | if (result) | |
4856 | return; | |
4857 | } | |
4858 | } | |
4859 | } | |
4860 | ||
4861 | void visit(CastExp *e) | |
4862 | { | |
4863 | //printf("CastExp::reliesOnTident('%s')\n", e->toChars()); | |
4864 | visit((UnaExp *)e); | |
4865 | // e.to can be null for cast() with no type | |
4866 | if (!result && e->to) | |
4867 | e->to->accept(this); | |
4868 | } | |
4869 | ||
4870 | void visit(SliceExp *e) | |
4871 | { | |
4872 | //printf("SliceExp::reliesOnTident('%s')\n", e->toChars()); | |
4873 | visit((UnaExp *)e); | |
4874 | if (!result && e->lwr) | |
4875 | e->lwr->accept(this); | |
4876 | if (!result && e->upr) | |
4877 | e->upr->accept(this); | |
4878 | } | |
4879 | ||
4880 | void visit(IntervalExp *e) | |
4881 | { | |
4882 | //printf("IntervalExp::reliesOnTident('%s')\n", e->toChars()); | |
4883 | e->lwr->accept(this); | |
4884 | if (!result) | |
4885 | e->upr->accept(this); | |
4886 | } | |
4887 | ||
4888 | void visit(ArrayExp *e) | |
4889 | { | |
4890 | //printf("ArrayExp::reliesOnTident('%s')\n", e->toChars()); | |
4891 | visit((UnaExp *)e); | |
4892 | if (!result && e->arguments) | |
4893 | { | |
4894 | for (size_t i = 0; i < e->arguments->dim; i++) | |
4895 | { | |
4896 | Expression *ea = (*e->arguments)[i]; | |
4897 | ea->accept(this); | |
4898 | } | |
4899 | } | |
4900 | } | |
4901 | ||
4902 | void visit(BinExp *e) | |
4903 | { | |
4904 | //printf("BinExp::reliesOnTident('%s')\n", e->toChars()); | |
4905 | e->e1->accept(this); | |
4906 | if (!result) | |
4907 | e->e2->accept(this); | |
4908 | } | |
4909 | ||
4910 | void visit(CondExp *e) | |
4911 | { | |
4912 | //printf("BinExp::reliesOnTident('%s')\n", e->toChars()); | |
4913 | e->econd->accept(this); | |
4914 | if (!result) | |
4915 | visit((BinExp *)e); | |
4916 | } | |
4917 | }; | |
4918 | ||
4919 | if (!t) | |
4920 | return false; | |
4921 | ||
4922 | ReliesOnTident v(tparams, iStart); | |
4923 | t->accept(&v); | |
4924 | return v.result; | |
4925 | } | |
4926 | ||
4927 | /* ======================== TemplateParameter =============================== */ | |
4928 | ||
4929 | TemplateParameter::TemplateParameter(Loc loc, Identifier *ident) | |
4930 | { | |
4931 | this->loc = loc; | |
4932 | this->ident = ident; | |
4933 | this->dependent = false; | |
4934 | } | |
4935 | ||
4936 | TemplateTypeParameter *TemplateParameter::isTemplateTypeParameter() | |
4937 | { | |
4938 | return NULL; | |
4939 | } | |
4940 | ||
4941 | TemplateValueParameter *TemplateParameter::isTemplateValueParameter() | |
4942 | { | |
4943 | return NULL; | |
4944 | } | |
4945 | ||
4946 | TemplateAliasParameter *TemplateParameter::isTemplateAliasParameter() | |
4947 | { | |
4948 | return NULL; | |
4949 | } | |
4950 | ||
4951 | TemplateTupleParameter *TemplateParameter::isTemplateTupleParameter() | |
4952 | { | |
4953 | return NULL; | |
4954 | } | |
4955 | ||
4956 | TemplateThisParameter *TemplateParameter::isTemplateThisParameter() | |
4957 | { | |
4958 | return NULL; | |
4959 | } | |
4960 | ||
4961 | /******************************************* | |
4962 | * Match to a particular TemplateParameter. | |
4963 | * Input: | |
4964 | * instLoc location that the template is instantiated. | |
4965 | * tiargs[] actual arguments to template instance | |
4966 | * i i'th argument | |
4967 | * parameters[] template parameters | |
4968 | * dedtypes[] deduced arguments to template instance | |
4969 | * *psparam set to symbol declared and initialized to dedtypes[i] | |
4970 | */ | |
4971 | MATCH TemplateParameter::matchArg(Loc instLoc, Scope *sc, Objects *tiargs, | |
4972 | size_t i, TemplateParameters *parameters, Objects *dedtypes, | |
4973 | Declaration **psparam) | |
4974 | { | |
4975 | RootObject *oarg; | |
4976 | ||
4977 | if (i < tiargs->dim) | |
4978 | oarg = (*tiargs)[i]; | |
4979 | else | |
4980 | { | |
4981 | // Get default argument instead | |
4982 | oarg = defaultArg(instLoc, sc); | |
4983 | if (!oarg) | |
4984 | { | |
4985 | assert(i < dedtypes->dim); | |
4986 | // It might have already been deduced | |
4987 | oarg = (*dedtypes)[i]; | |
4988 | if (!oarg) | |
4989 | goto Lnomatch; | |
4990 | } | |
4991 | } | |
4992 | return matchArg(sc, oarg, i, parameters, dedtypes, psparam); | |
4993 | ||
4994 | Lnomatch: | |
4995 | if (psparam) | |
4996 | *psparam = NULL; | |
4997 | return MATCHnomatch; | |
4998 | } | |
4999 | ||
5000 | /* ======================== TemplateTypeParameter =========================== */ | |
5001 | ||
5002 | // type-parameter | |
5003 | ||
5004 | Type *TemplateTypeParameter::tdummy = NULL; | |
5005 | ||
5006 | TemplateTypeParameter::TemplateTypeParameter(Loc loc, Identifier *ident, Type *specType, | |
5007 | Type *defaultType) | |
5008 | : TemplateParameter(loc, ident) | |
5009 | { | |
5010 | this->ident = ident; | |
5011 | this->specType = specType; | |
5012 | this->defaultType = defaultType; | |
5013 | } | |
5014 | ||
5015 | TemplateTypeParameter *TemplateTypeParameter::isTemplateTypeParameter() | |
5016 | { | |
5017 | return this; | |
5018 | } | |
5019 | ||
5020 | TemplateParameter *TemplateTypeParameter::syntaxCopy() | |
5021 | { | |
5022 | return new TemplateTypeParameter(loc, ident, | |
5023 | specType ? specType->syntaxCopy() : NULL, | |
5024 | defaultType ? defaultType->syntaxCopy() : NULL); | |
5025 | } | |
5026 | ||
5027 | bool TemplateTypeParameter::declareParameter(Scope *sc) | |
5028 | { | |
5029 | //printf("TemplateTypeParameter::declareParameter('%s')\n", ident->toChars()); | |
5030 | TypeIdentifier *ti = new TypeIdentifier(loc, ident); | |
5031 | Declaration *ad = new AliasDeclaration(loc, ident, ti); | |
5032 | return sc->insert(ad) != NULL; | |
5033 | } | |
5034 | ||
5035 | bool TemplateTypeParameter::semantic(Scope *sc, TemplateParameters *parameters) | |
5036 | { | |
5037 | //printf("TemplateTypeParameter::semantic('%s')\n", ident->toChars()); | |
5038 | if (specType && !reliesOnTident(specType, parameters)) | |
5039 | { | |
5040 | specType = specType->semantic(loc, sc); | |
5041 | } | |
5042 | return !(specType && isError(specType)); | |
5043 | } | |
5044 | ||
5045 | MATCH TemplateTypeParameter::matchArg(Scope *sc, RootObject *oarg, | |
5046 | size_t i, TemplateParameters *parameters, Objects *dedtypes, | |
5047 | Declaration **psparam) | |
5048 | { | |
5049 | //printf("TemplateTypeParameter::matchArg('%s')\n", ident->toChars()); | |
5050 | MATCH m = MATCHexact; | |
5051 | Type *ta = isType(oarg); | |
5052 | if (!ta) | |
5053 | { | |
5054 | //printf("%s %p %p %p\n", oarg->toChars(), isExpression(oarg), isDsymbol(oarg), isTuple(oarg)); | |
5055 | goto Lnomatch; | |
5056 | } | |
5057 | //printf("ta is %s\n", ta->toChars()); | |
5058 | ||
5059 | if (specType) | |
5060 | { | |
5061 | if (!ta || ta == tdummy) | |
5062 | goto Lnomatch; | |
5063 | ||
5064 | //printf("\tcalling deduceType(): ta is %s, specType is %s\n", ta->toChars(), specType->toChars()); | |
5065 | MATCH m2 = deduceType(ta, sc, specType, parameters, dedtypes); | |
5066 | if (m2 <= MATCHnomatch) | |
5067 | { | |
5068 | //printf("\tfailed deduceType\n"); | |
5069 | goto Lnomatch; | |
5070 | } | |
5071 | ||
5072 | if (m2 < m) | |
5073 | m = m2; | |
5074 | if ((*dedtypes)[i]) | |
5075 | { | |
5076 | Type *t = (Type *)(*dedtypes)[i]; | |
5077 | ||
5078 | if (dependent && !t->equals(ta)) // Bugzilla 14357 | |
5079 | goto Lnomatch; | |
5080 | ||
5081 | /* This is a self-dependent parameter. For example: | |
5082 | * template X(T : T*) {} | |
5083 | * template X(T : S!T, alias S) {} | |
5084 | */ | |
5085 | //printf("t = %s ta = %s\n", t->toChars(), ta->toChars()); | |
5086 | ta = t; | |
5087 | } | |
5088 | } | |
5089 | else | |
5090 | { | |
5091 | if ((*dedtypes)[i]) | |
5092 | { | |
5093 | // Must match already deduced type | |
5094 | Type *t = (Type *)(*dedtypes)[i]; | |
5095 | ||
5096 | if (!t->equals(ta)) | |
5097 | { | |
5098 | //printf("t = %s ta = %s\n", t->toChars(), ta->toChars()); | |
5099 | goto Lnomatch; | |
5100 | } | |
5101 | } | |
5102 | else | |
5103 | { | |
5104 | // So that matches with specializations are better | |
5105 | m = MATCHconvert; | |
5106 | } | |
5107 | } | |
5108 | (*dedtypes)[i] = ta; | |
5109 | ||
5110 | if (psparam) | |
5111 | *psparam = new AliasDeclaration(loc, ident, ta); | |
5112 | //printf("\tm = %d\n", m); | |
5113 | return dependent ? MATCHexact : m; | |
5114 | ||
5115 | Lnomatch: | |
5116 | if (psparam) | |
5117 | *psparam = NULL; | |
5118 | //printf("\tm = %d\n", MATCHnomatch); | |
5119 | return MATCHnomatch; | |
5120 | } | |
5121 | ||
5122 | ||
5123 | void TemplateTypeParameter::print(RootObject *oarg, RootObject *oded) | |
5124 | { | |
5125 | printf(" %s\n", ident->toChars()); | |
5126 | ||
5127 | Type *t = isType(oarg); | |
5128 | Type *ta = isType(oded); | |
5129 | ||
5130 | assert(ta); | |
5131 | ||
5132 | if (specType) | |
5133 | printf("\tSpecialization: %s\n", specType->toChars()); | |
5134 | if (defaultType) | |
5135 | printf("\tDefault: %s\n", defaultType->toChars()); | |
5136 | printf("\tParameter: %s\n", t ? t->toChars() : "NULL"); | |
5137 | printf("\tDeduced Type: %s\n", ta->toChars()); | |
5138 | } | |
5139 | ||
5140 | void *TemplateTypeParameter::dummyArg() | |
5141 | { | |
5142 | Type *t = specType; | |
5143 | if (!t) | |
5144 | { | |
5145 | // Use this for alias-parameter's too (?) | |
5146 | if (!tdummy) | |
5147 | tdummy = new TypeIdentifier(loc, ident); | |
5148 | t = tdummy; | |
5149 | } | |
5150 | return (void *)t; | |
5151 | } | |
5152 | ||
5153 | ||
5154 | RootObject *TemplateTypeParameter::specialization() | |
5155 | { | |
5156 | return specType; | |
5157 | } | |
5158 | ||
5159 | RootObject *TemplateTypeParameter::defaultArg(Loc, Scope *sc) | |
5160 | { | |
5161 | Type *t = defaultType; | |
5162 | if (t) | |
5163 | { | |
5164 | t = t->syntaxCopy(); | |
5165 | t = t->semantic(loc, sc); // use the parameter loc | |
5166 | } | |
5167 | return t; | |
5168 | } | |
5169 | ||
5170 | bool TemplateTypeParameter::hasDefaultArg() | |
5171 | { | |
5172 | return defaultType != NULL; | |
5173 | } | |
5174 | ||
5175 | /* ======================== TemplateThisParameter =========================== */ | |
5176 | ||
5177 | // this-parameter | |
5178 | ||
5179 | TemplateThisParameter::TemplateThisParameter(Loc loc, Identifier *ident, | |
5180 | Type *specType, | |
5181 | Type *defaultType) | |
5182 | : TemplateTypeParameter(loc, ident, specType, defaultType) | |
5183 | { | |
5184 | } | |
5185 | ||
5186 | TemplateThisParameter *TemplateThisParameter::isTemplateThisParameter() | |
5187 | { | |
5188 | return this; | |
5189 | } | |
5190 | ||
5191 | TemplateParameter *TemplateThisParameter::syntaxCopy() | |
5192 | { | |
5193 | return new TemplateThisParameter(loc, ident, | |
5194 | specType ? specType->syntaxCopy() : NULL, | |
5195 | defaultType ? defaultType->syntaxCopy() : NULL); | |
5196 | } | |
5197 | ||
5198 | /* ======================== TemplateAliasParameter ========================== */ | |
5199 | ||
5200 | // alias-parameter | |
5201 | ||
5202 | Dsymbol *TemplateAliasParameter::sdummy = NULL; | |
5203 | ||
5204 | TemplateAliasParameter::TemplateAliasParameter(Loc loc, Identifier *ident, | |
5205 | Type *specType, RootObject *specAlias, RootObject *defaultAlias) | |
5206 | : TemplateParameter(loc, ident) | |
5207 | { | |
5208 | this->ident = ident; | |
5209 | this->specType = specType; | |
5210 | this->specAlias = specAlias; | |
5211 | this->defaultAlias = defaultAlias; | |
5212 | } | |
5213 | ||
5214 | TemplateAliasParameter *TemplateAliasParameter::isTemplateAliasParameter() | |
5215 | { | |
5216 | return this; | |
5217 | } | |
5218 | ||
5219 | TemplateParameter *TemplateAliasParameter::syntaxCopy() | |
5220 | { | |
5221 | return new TemplateAliasParameter(loc, ident, | |
5222 | specType ? specType->syntaxCopy() : NULL, | |
5223 | objectSyntaxCopy(specAlias), | |
5224 | objectSyntaxCopy(defaultAlias)); | |
5225 | } | |
5226 | ||
5227 | bool TemplateAliasParameter::declareParameter(Scope *sc) | |
5228 | { | |
5229 | TypeIdentifier *ti = new TypeIdentifier(loc, ident); | |
5230 | Declaration *ad = new AliasDeclaration(loc, ident, ti); | |
5231 | return sc->insert(ad) != NULL; | |
5232 | } | |
5233 | ||
5234 | static RootObject *aliasParameterSemantic(Loc loc, Scope *sc, RootObject *o, TemplateParameters *parameters) | |
5235 | { | |
5236 | if (o) | |
5237 | { | |
5238 | Expression *ea = isExpression(o); | |
5239 | Type *ta = isType(o); | |
5240 | if (ta && (!parameters || !reliesOnTident(ta, parameters))) | |
5241 | { | |
5242 | Dsymbol *s = ta->toDsymbol(sc); | |
5243 | if (s) | |
5244 | o = s; | |
5245 | else | |
5246 | o = ta->semantic(loc, sc); | |
5247 | } | |
5248 | else if (ea) | |
5249 | { | |
5250 | sc = sc->startCTFE(); | |
5251 | ea = ::semantic(ea, sc); | |
5252 | sc = sc->endCTFE(); | |
5253 | o = ea->ctfeInterpret(); | |
5254 | } | |
5255 | } | |
5256 | return o; | |
5257 | } | |
5258 | ||
5259 | bool TemplateAliasParameter::semantic(Scope *sc, TemplateParameters *parameters) | |
5260 | { | |
5261 | if (specType && !reliesOnTident(specType, parameters)) | |
5262 | { | |
5263 | specType = specType->semantic(loc, sc); | |
5264 | } | |
5265 | specAlias = aliasParameterSemantic(loc, sc, specAlias, parameters); | |
5266 | return !(specType && isError(specType)) && | |
5267 | !(specAlias && isError(specAlias)); | |
5268 | } | |
5269 | ||
5270 | MATCH TemplateAliasParameter::matchArg(Scope *sc, RootObject *oarg, | |
5271 | size_t i, TemplateParameters *parameters, Objects *dedtypes, | |
5272 | Declaration **psparam) | |
5273 | { | |
5274 | //printf("TemplateAliasParameter::matchArg('%s')\n", ident->toChars()); | |
5275 | MATCH m = MATCHexact; | |
5276 | Type *ta = isType(oarg); | |
5277 | RootObject *sa = ta && !ta->deco ? NULL : getDsymbol(oarg); | |
5278 | Expression *ea = isExpression(oarg); | |
5279 | if (ea && (ea->op == TOKthis || ea->op == TOKsuper)) | |
5280 | sa = ((ThisExp *)ea)->var; | |
5281 | else if (ea && ea->op == TOKscope) | |
5282 | sa = ((ScopeExp *)ea)->sds; | |
5283 | if (sa) | |
5284 | { | |
5285 | if (((Dsymbol *)sa)->isAggregateDeclaration()) | |
5286 | m = MATCHconvert; | |
5287 | ||
5288 | /* specType means the alias must be a declaration with a type | |
5289 | * that matches specType. | |
5290 | */ | |
5291 | if (specType) | |
5292 | { | |
5293 | Declaration *d = ((Dsymbol *)sa)->isDeclaration(); | |
5294 | if (!d) | |
5295 | goto Lnomatch; | |
5296 | if (!d->type->equals(specType)) | |
5297 | goto Lnomatch; | |
5298 | } | |
5299 | } | |
5300 | else | |
5301 | { | |
5302 | sa = oarg; | |
5303 | if (ea) | |
5304 | { | |
5305 | if (specType) | |
5306 | { | |
5307 | if (!ea->type->equals(specType)) | |
5308 | goto Lnomatch; | |
5309 | } | |
5310 | } | |
5311 | else if (ta && ta->ty == Tinstance && !specAlias) | |
5312 | { | |
5313 | /* Bugzilla xxxxx: Specialized parameter should be prefeerd | |
5314 | * match to the template type parameter. | |
5315 | * template X(alias a) {} // a == this | |
5316 | * template X(alias a : B!A, alias B, A...) {} // B!A => ta | |
5317 | */ | |
5318 | } | |
5319 | else if (sa && sa == TemplateTypeParameter::tdummy) | |
5320 | { | |
5321 | /* Bugzilla 2025: Aggregate Types should preferentially | |
5322 | * match to the template type parameter. | |
5323 | * template X(alias a) {} // a == this | |
5324 | * template X(T) {} // T => sa | |
5325 | */ | |
5326 | } | |
5327 | else | |
5328 | goto Lnomatch; | |
5329 | } | |
5330 | ||
5331 | if (specAlias) | |
5332 | { | |
5333 | if (sa == sdummy) | |
5334 | goto Lnomatch; | |
5335 | Dsymbol *sx = isDsymbol(sa); | |
5336 | if (sa != specAlias && sx) | |
5337 | { | |
5338 | Type *talias = isType(specAlias); | |
5339 | if (!talias) | |
5340 | goto Lnomatch; | |
5341 | ||
5342 | TemplateInstance *ti = sx->isTemplateInstance(); | |
5343 | if (!ti && sx->parent) | |
5344 | { | |
5345 | ti = sx->parent->isTemplateInstance(); | |
5346 | if (ti && ti->name != sx->ident) | |
5347 | goto Lnomatch; | |
5348 | } | |
5349 | if (!ti) | |
5350 | goto Lnomatch; | |
5351 | ||
5352 | Type *t = new TypeInstance(Loc(), ti); | |
5353 | MATCH m2 = deduceType(t, sc, talias, parameters, dedtypes); | |
5354 | if (m2 <= MATCHnomatch) | |
5355 | goto Lnomatch; | |
5356 | } | |
5357 | } | |
5358 | else if ((*dedtypes)[i]) | |
5359 | { | |
5360 | // Must match already deduced symbol | |
5361 | RootObject *si = (*dedtypes)[i]; | |
5362 | if (!sa || si != sa) | |
5363 | goto Lnomatch; | |
5364 | } | |
5365 | (*dedtypes)[i] = sa; | |
5366 | ||
5367 | if (psparam) | |
5368 | { | |
5369 | if (Dsymbol *s = isDsymbol(sa)) | |
5370 | { | |
5371 | *psparam = new AliasDeclaration(loc, ident, s); | |
5372 | } | |
5373 | else if (Type *t = isType(sa)) | |
5374 | { | |
5375 | *psparam = new AliasDeclaration(loc, ident, t); | |
5376 | } | |
5377 | else | |
5378 | { | |
5379 | assert(ea); | |
5380 | ||
5381 | // Declare manifest constant | |
5382 | Initializer *init = new ExpInitializer(loc, ea); | |
5383 | VarDeclaration *v = new VarDeclaration(loc, NULL, ident, init); | |
5384 | v->storage_class = STCmanifest; | |
5385 | v->semantic(sc); | |
5386 | *psparam = v; | |
5387 | } | |
5388 | } | |
5389 | return dependent ? MATCHexact : m; | |
5390 | ||
5391 | Lnomatch: | |
5392 | if (psparam) | |
5393 | *psparam = NULL; | |
5394 | //printf("\tm = %d\n", MATCHnomatch); | |
5395 | return MATCHnomatch; | |
5396 | } | |
5397 | ||
5398 | ||
5399 | void TemplateAliasParameter::print(RootObject *, RootObject *oded) | |
5400 | { | |
5401 | printf(" %s\n", ident->toChars()); | |
5402 | ||
5403 | Dsymbol *sa = isDsymbol(oded); | |
5404 | assert(sa); | |
5405 | ||
5406 | printf("\tParameter alias: %s\n", sa->toChars()); | |
5407 | } | |
5408 | ||
5409 | void *TemplateAliasParameter::dummyArg() | |
5410 | { | |
5411 | RootObject *s = specAlias; | |
5412 | if (!s) | |
5413 | { | |
5414 | if (!sdummy) | |
5415 | sdummy = new Dsymbol(); | |
5416 | s = sdummy; | |
5417 | } | |
5418 | return (void*)s; | |
5419 | } | |
5420 | ||
5421 | ||
5422 | RootObject *TemplateAliasParameter::specialization() | |
5423 | { | |
5424 | return specAlias; | |
5425 | } | |
5426 | ||
5427 | RootObject *TemplateAliasParameter::defaultArg(Loc, Scope *sc) | |
5428 | { | |
5429 | RootObject *da = defaultAlias; | |
5430 | Type *ta = isType(defaultAlias); | |
5431 | if (ta) | |
5432 | { | |
5433 | if (ta->ty == Tinstance) | |
5434 | { | |
5435 | // If the default arg is a template, instantiate for each type | |
5436 | da = ta->syntaxCopy(); | |
5437 | } | |
5438 | } | |
5439 | ||
5440 | RootObject *o = aliasParameterSemantic(loc, sc, da, NULL); // use the parameter loc | |
5441 | return o; | |
5442 | } | |
5443 | ||
5444 | bool TemplateAliasParameter::hasDefaultArg() | |
5445 | { | |
5446 | return defaultAlias != NULL; | |
5447 | } | |
5448 | ||
5449 | /* ======================== TemplateValueParameter ========================== */ | |
5450 | ||
5451 | // value-parameter | |
5452 | ||
5453 | AA *TemplateValueParameter::edummies = NULL; | |
5454 | ||
5455 | TemplateValueParameter::TemplateValueParameter(Loc loc, Identifier *ident, Type *valType, | |
5456 | Expression *specValue, Expression *defaultValue) | |
5457 | : TemplateParameter(loc, ident) | |
5458 | { | |
5459 | this->ident = ident; | |
5460 | this->valType = valType; | |
5461 | this->specValue = specValue; | |
5462 | this->defaultValue = defaultValue; | |
5463 | } | |
5464 | ||
5465 | TemplateValueParameter *TemplateValueParameter::isTemplateValueParameter() | |
5466 | { | |
5467 | return this; | |
5468 | } | |
5469 | ||
5470 | TemplateParameter *TemplateValueParameter::syntaxCopy() | |
5471 | { | |
5472 | return new TemplateValueParameter(loc, ident, | |
5473 | valType->syntaxCopy(), | |
5474 | specValue ? specValue->syntaxCopy() : NULL, | |
5475 | defaultValue ? defaultValue->syntaxCopy() : NULL); | |
5476 | } | |
5477 | ||
5478 | bool TemplateValueParameter::declareParameter(Scope *sc) | |
5479 | { | |
5480 | VarDeclaration *v = new VarDeclaration(loc, valType, ident, NULL); | |
5481 | v->storage_class = STCtemplateparameter; | |
5482 | return sc->insert(v) != NULL; | |
5483 | } | |
5484 | ||
5485 | bool TemplateValueParameter::semantic(Scope *sc, TemplateParameters *) | |
5486 | { | |
5487 | valType = valType->semantic(loc, sc); | |
5488 | ||
5489 | return !isError(valType); | |
5490 | } | |
5491 | ||
5492 | MATCH TemplateValueParameter::matchArg(Scope *sc, RootObject *oarg, | |
5493 | size_t i, TemplateParameters *, Objects *dedtypes, Declaration **psparam) | |
5494 | { | |
5495 | //printf("TemplateValueParameter::matchArg('%s')\n", ident->toChars()); | |
5496 | ||
5497 | MATCH m = MATCHexact; | |
5498 | ||
5499 | Expression *ei = isExpression(oarg); | |
5500 | Type *vt; | |
5501 | ||
5502 | if (!ei && oarg) | |
5503 | { | |
5504 | Dsymbol *si = isDsymbol(oarg); | |
5505 | FuncDeclaration *f = si ? si->isFuncDeclaration() : NULL; | |
5506 | if (!f || !f->fbody || f->needThis()) | |
5507 | goto Lnomatch; | |
5508 | ||
5509 | ei = new VarExp(loc, f); | |
5510 | ei = ::semantic(ei, sc); | |
5511 | ||
5512 | /* If a function is really property-like, and then | |
5513 | * it's CTFEable, ei will be a literal expression. | |
5514 | */ | |
5515 | unsigned int olderrors = global.startGagging(); | |
5516 | ei = resolveProperties(sc, ei); | |
5517 | ei = ei->ctfeInterpret(); | |
5518 | if (global.endGagging(olderrors) || ei->op == TOKerror) | |
5519 | goto Lnomatch; | |
5520 | ||
5521 | /* Bugzilla 14520: A property-like function can match to both | |
5522 | * TemplateAlias and ValueParameter. But for template overloads, | |
5523 | * it should always prefer alias parameter to be consistent | |
5524 | * template match result. | |
5525 | * | |
5526 | * template X(alias f) { enum X = 1; } | |
5527 | * template X(int val) { enum X = 2; } | |
5528 | * int f1() { return 0; } // CTFEable | |
5529 | * int f2(); // body-less function is not CTFEable | |
5530 | * enum x1 = X!f1; // should be 1 | |
5531 | * enum x2 = X!f2; // should be 1 | |
5532 | * | |
5533 | * e.g. The x1 value must be same even if the f1 definition will be moved | |
5534 | * into di while stripping body code. | |
5535 | */ | |
5536 | m = MATCHconvert; | |
5537 | } | |
5538 | ||
5539 | if (ei && ei->op == TOKvar) | |
5540 | { | |
5541 | // Resolve const variables that we had skipped earlier | |
5542 | ei = ei->ctfeInterpret(); | |
5543 | } | |
5544 | ||
5545 | //printf("\tvalType: %s, ty = %d\n", valType->toChars(), valType->ty); | |
5546 | vt = valType->semantic(loc, sc); | |
5547 | //printf("ei: %s, ei->type: %s\n", ei->toChars(), ei->type->toChars()); | |
5548 | //printf("vt = %s\n", vt->toChars()); | |
5549 | ||
5550 | if (ei->type) | |
5551 | { | |
5552 | MATCH m2 = ei->implicitConvTo(vt); | |
5553 | //printf("m: %d\n", m); | |
5554 | if (m2 < m) | |
5555 | m = m2; | |
5556 | if (m <= MATCHnomatch) | |
5557 | goto Lnomatch; | |
5558 | ei = ei->implicitCastTo(sc, vt); | |
5559 | ei = ei->ctfeInterpret(); | |
5560 | } | |
5561 | ||
5562 | if (specValue) | |
5563 | { | |
5564 | if (!ei || (Expression *)dmd_aaGetRvalue(edummies, (void *)ei->type) == ei) | |
5565 | goto Lnomatch; | |
5566 | ||
5567 | Expression *e = specValue; | |
5568 | ||
5569 | sc = sc->startCTFE(); | |
5570 | e = ::semantic(e, sc); | |
5571 | e = resolveProperties(sc, e); | |
5572 | sc = sc->endCTFE(); | |
5573 | e = e->implicitCastTo(sc, vt); | |
5574 | e = e->ctfeInterpret(); | |
5575 | ||
5576 | ei = ei->syntaxCopy(); | |
5577 | sc = sc->startCTFE(); | |
5578 | ei = ::semantic(ei, sc); | |
5579 | sc = sc->endCTFE(); | |
5580 | ei = ei->implicitCastTo(sc, vt); | |
5581 | ei = ei->ctfeInterpret(); | |
5582 | //printf("\tei: %s, %s\n", ei->toChars(), ei->type->toChars()); | |
5583 | //printf("\te : %s, %s\n", e->toChars(), e->type->toChars()); | |
5584 | if (!ei->equals(e)) | |
5585 | goto Lnomatch; | |
5586 | } | |
5587 | else | |
5588 | { | |
5589 | if ((*dedtypes)[i]) | |
5590 | { | |
5591 | // Must match already deduced value | |
5592 | Expression *e = (Expression *)(*dedtypes)[i]; | |
5593 | ||
5594 | if (!ei || !ei->equals(e)) | |
5595 | goto Lnomatch; | |
5596 | } | |
5597 | } | |
5598 | (*dedtypes)[i] = ei; | |
5599 | ||
5600 | if (psparam) | |
5601 | { | |
5602 | Initializer *init = new ExpInitializer(loc, ei); | |
5603 | Declaration *sparam = new VarDeclaration(loc, vt, ident, init); | |
5604 | sparam->storage_class = STCmanifest; | |
5605 | *psparam = sparam; | |
5606 | } | |
5607 | return dependent ? MATCHexact : m; | |
5608 | ||
5609 | Lnomatch: | |
5610 | //printf("\tno match\n"); | |
5611 | if (psparam) | |
5612 | *psparam = NULL; | |
5613 | return MATCHnomatch; | |
5614 | } | |
5615 | ||
5616 | ||
5617 | void TemplateValueParameter::print(RootObject *, RootObject *oded) | |
5618 | { | |
5619 | printf(" %s\n", ident->toChars()); | |
5620 | ||
5621 | Expression *ea = isExpression(oded); | |
5622 | ||
5623 | if (specValue) | |
5624 | printf("\tSpecialization: %s\n", specValue->toChars()); | |
5625 | printf("\tParameter Value: %s\n", ea ? ea->toChars() : "NULL"); | |
5626 | } | |
5627 | ||
5628 | void *TemplateValueParameter::dummyArg() | |
5629 | { | |
5630 | Expression *e = specValue; | |
5631 | if (!e) | |
5632 | { | |
5633 | // Create a dummy value | |
5634 | Expression **pe = (Expression **)dmd_aaGet(&edummies, (void *)valType); | |
5635 | if (!*pe) | |
5636 | *pe = valType->defaultInit(); | |
5637 | e = *pe; | |
5638 | } | |
5639 | return (void *)e; | |
5640 | } | |
5641 | ||
5642 | ||
5643 | RootObject *TemplateValueParameter::specialization() | |
5644 | { | |
5645 | return specValue; | |
5646 | } | |
5647 | ||
5648 | RootObject *TemplateValueParameter::defaultArg(Loc instLoc, Scope *sc) | |
5649 | { | |
5650 | Expression *e = defaultValue; | |
5651 | if (e) | |
5652 | { | |
5653 | e = e->syntaxCopy(); | |
5654 | if ((e = ::semantic(e, sc)) == NULL) | |
5655 | return NULL; | |
5656 | if ((e = resolveProperties(sc, e)) == NULL) | |
5657 | return NULL; | |
5658 | e = e->resolveLoc(instLoc, sc); // use the instantiated loc | |
5659 | e = e->optimize(WANTvalue); | |
5660 | } | |
5661 | return e; | |
5662 | } | |
5663 | ||
5664 | bool TemplateValueParameter::hasDefaultArg() | |
5665 | { | |
5666 | return defaultValue != NULL; | |
5667 | } | |
5668 | ||
5669 | /* ======================== TemplateTupleParameter ========================== */ | |
5670 | ||
5671 | // variadic-parameter | |
5672 | ||
5673 | TemplateTupleParameter::TemplateTupleParameter(Loc loc, Identifier *ident) | |
5674 | : TemplateParameter(loc, ident) | |
5675 | { | |
5676 | this->ident = ident; | |
5677 | } | |
5678 | ||
5679 | TemplateTupleParameter *TemplateTupleParameter::isTemplateTupleParameter() | |
5680 | { | |
5681 | return this; | |
5682 | } | |
5683 | ||
5684 | TemplateParameter *TemplateTupleParameter::syntaxCopy() | |
5685 | { | |
5686 | return new TemplateTupleParameter(loc, ident); | |
5687 | } | |
5688 | ||
5689 | bool TemplateTupleParameter::declareParameter(Scope *sc) | |
5690 | { | |
5691 | TypeIdentifier *ti = new TypeIdentifier(loc, ident); | |
5692 | Declaration *ad = new AliasDeclaration(loc, ident, ti); | |
5693 | return sc->insert(ad) != NULL; | |
5694 | } | |
5695 | ||
5696 | bool TemplateTupleParameter::semantic(Scope *, TemplateParameters *) | |
5697 | { | |
5698 | return true; | |
5699 | } | |
5700 | ||
5701 | MATCH TemplateTupleParameter::matchArg(Loc, Scope *sc, Objects *tiargs, | |
5702 | size_t i, TemplateParameters *parameters, Objects *dedtypes, | |
5703 | Declaration **psparam) | |
5704 | { | |
5705 | /* The rest of the actual arguments (tiargs[]) form the match | |
5706 | * for the variadic parameter. | |
5707 | */ | |
5708 | assert(i + 1 == dedtypes->dim); // must be the last one | |
5709 | Tuple *ovar; | |
5710 | ||
5711 | if (Tuple *u = isTuple((*dedtypes)[i])) | |
5712 | { | |
5713 | // It has already been deduced | |
5714 | ovar = u; | |
5715 | } | |
5716 | else if (i + 1 == tiargs->dim && isTuple((*tiargs)[i])) | |
5717 | ovar = isTuple((*tiargs)[i]); | |
5718 | else | |
5719 | { | |
5720 | ovar = new Tuple(); | |
5721 | //printf("ovar = %p\n", ovar); | |
5722 | if (i < tiargs->dim) | |
5723 | { | |
5724 | //printf("i = %d, tiargs->dim = %d\n", i, tiargs->dim); | |
5725 | ovar->objects.setDim(tiargs->dim - i); | |
5726 | for (size_t j = 0; j < ovar->objects.dim; j++) | |
5727 | ovar->objects[j] = (*tiargs)[i + j]; | |
5728 | } | |
5729 | } | |
5730 | return matchArg(sc, ovar, i, parameters, dedtypes, psparam); | |
5731 | } | |
5732 | ||
5733 | MATCH TemplateTupleParameter::matchArg(Scope *, RootObject *oarg, | |
5734 | size_t i, TemplateParameters *, Objects *dedtypes, Declaration **psparam) | |
5735 | { | |
5736 | //printf("TemplateTupleParameter::matchArg('%s')\n", ident->toChars()); | |
5737 | Tuple *ovar = isTuple(oarg); | |
5738 | if (!ovar) | |
5739 | return MATCHnomatch; | |
5740 | if ((*dedtypes)[i]) | |
5741 | { | |
5742 | Tuple *tup = isTuple((*dedtypes)[i]); | |
5743 | if (!tup) | |
5744 | return MATCHnomatch; | |
5745 | if (!match(tup, ovar)) | |
5746 | return MATCHnomatch; | |
5747 | } | |
5748 | (*dedtypes)[i] = ovar; | |
5749 | ||
5750 | if (psparam) | |
5751 | *psparam = new TupleDeclaration(loc, ident, &ovar->objects); | |
5752 | return dependent ? MATCHexact : MATCHconvert; | |
5753 | } | |
5754 | ||
5755 | ||
5756 | void TemplateTupleParameter::print(RootObject *, RootObject *oded) | |
5757 | { | |
5758 | printf(" %s... [", ident->toChars()); | |
5759 | Tuple *v = isTuple(oded); | |
5760 | assert(v); | |
5761 | ||
5762 | //printf("|%d| ", v->objects.dim); | |
5763 | for (size_t i = 0; i < v->objects.dim; i++) | |
5764 | { | |
5765 | if (i) | |
5766 | printf(", "); | |
5767 | ||
5768 | RootObject *o = v->objects[i]; | |
5769 | ||
5770 | Dsymbol *sa = isDsymbol(o); | |
5771 | if (sa) | |
5772 | printf("alias: %s", sa->toChars()); | |
5773 | ||
5774 | Type *ta = isType(o); | |
5775 | if (ta) | |
5776 | printf("type: %s", ta->toChars()); | |
5777 | ||
5778 | Expression *ea = isExpression(o); | |
5779 | if (ea) | |
5780 | printf("exp: %s", ea->toChars()); | |
5781 | ||
5782 | assert(!isTuple(o)); // no nested Tuple arguments | |
5783 | } | |
5784 | ||
5785 | printf("]\n"); | |
5786 | } | |
5787 | ||
5788 | void *TemplateTupleParameter::dummyArg() | |
5789 | { | |
5790 | return NULL; | |
5791 | } | |
5792 | ||
5793 | ||
5794 | RootObject *TemplateTupleParameter::specialization() | |
5795 | { | |
5796 | return NULL; | |
5797 | } | |
5798 | ||
5799 | RootObject *TemplateTupleParameter::defaultArg(Loc, Scope *) | |
5800 | { | |
5801 | return NULL; | |
5802 | } | |
5803 | ||
5804 | bool TemplateTupleParameter::hasDefaultArg() | |
5805 | { | |
5806 | return false; | |
5807 | } | |
5808 | ||
5809 | /* ======================== TemplateInstance ================================ */ | |
5810 | ||
5811 | TemplateInstance::TemplateInstance(Loc loc, Identifier *ident) | |
5812 | : ScopeDsymbol(NULL) | |
5813 | { | |
5814 | this->loc = loc; | |
5815 | this->name = ident; | |
5816 | this->tiargs = NULL; | |
5817 | this->tempdecl = NULL; | |
5818 | this->inst = NULL; | |
5819 | this->tinst = NULL; | |
5820 | this->tnext = NULL; | |
5821 | this->minst = NULL; | |
5822 | this->deferred = NULL; | |
5823 | this->memberOf = NULL; | |
5824 | this->argsym = NULL; | |
5825 | this->aliasdecl = NULL; | |
5826 | this->semantictiargsdone = false; | |
5827 | this->inuse = 0; | |
5828 | this->nest = 0; | |
5829 | this->havetempdecl = false; | |
5830 | this->enclosing = NULL; | |
5831 | this->gagged = false; | |
5832 | this->hash = 0; | |
5833 | this->fargs = NULL; | |
5834 | } | |
5835 | ||
5836 | /***************** | |
5837 | * This constructor is only called when we figured out which function | |
5838 | * template to instantiate. | |
5839 | */ | |
5840 | ||
5841 | TemplateInstance::TemplateInstance(Loc loc, TemplateDeclaration *td, Objects *tiargs) | |
5842 | : ScopeDsymbol(NULL) | |
5843 | { | |
5844 | this->loc = loc; | |
5845 | this->name = td->ident; | |
5846 | this->tiargs = tiargs; | |
5847 | this->tempdecl = td; | |
5848 | this->inst = NULL; | |
5849 | this->tinst = NULL; | |
5850 | this->tnext = NULL; | |
5851 | this->minst = NULL; | |
5852 | this->deferred = NULL; | |
5853 | this->memberOf = NULL; | |
5854 | this->argsym = NULL; | |
5855 | this->aliasdecl = NULL; | |
5856 | this->semantictiargsdone = true; | |
5857 | this->inuse = 0; | |
5858 | this->nest = 0; | |
5859 | this->havetempdecl = true; | |
5860 | this->enclosing = NULL; | |
5861 | this->gagged = false; | |
5862 | this->hash = 0; | |
5863 | this->fargs = NULL; | |
5864 | ||
5865 | assert(tempdecl->_scope); | |
5866 | } | |
5867 | ||
5868 | ||
5869 | Objects *TemplateInstance::arraySyntaxCopy(Objects *objs) | |
5870 | { | |
5871 | Objects *a = NULL; | |
5872 | if (objs) | |
5873 | { | |
5874 | a = new Objects(); | |
5875 | a->setDim(objs->dim); | |
5876 | for (size_t i = 0; i < objs->dim; i++) | |
5877 | (*a)[i] = objectSyntaxCopy((*objs)[i]); | |
5878 | } | |
5879 | return a; | |
5880 | } | |
5881 | ||
5882 | Dsymbol *TemplateInstance::syntaxCopy(Dsymbol *s) | |
5883 | { | |
5884 | TemplateInstance *ti = | |
5885 | s ? (TemplateInstance *)s | |
5886 | : new TemplateInstance(loc, name); | |
5887 | ti->tiargs = arraySyntaxCopy(tiargs); | |
5888 | TemplateDeclaration *td; | |
5889 | if (inst && tempdecl && (td = tempdecl->isTemplateDeclaration()) != NULL) | |
5890 | td->ScopeDsymbol::syntaxCopy(ti); | |
5891 | else | |
5892 | ScopeDsymbol::syntaxCopy(ti); | |
5893 | return ti; | |
5894 | } | |
5895 | ||
5896 | void TemplateInstance::semantic(Scope *sc) | |
5897 | { | |
5898 | semantic(sc, NULL); | |
5899 | } | |
5900 | ||
5901 | void TemplateInstance::expandMembers(Scope *sc2) | |
5902 | { | |
5903 | for (size_t i = 0; i < members->dim; i++) | |
5904 | { | |
5905 | Dsymbol *s = (*members)[i]; | |
5906 | s->setScope(sc2); | |
5907 | } | |
5908 | ||
5909 | for (size_t i = 0; i < members->dim; i++) | |
5910 | { | |
5911 | Dsymbol *s = (*members)[i]; | |
5912 | s->importAll(sc2); | |
5913 | } | |
5914 | ||
5915 | for (size_t i = 0; i < members->dim; i++) | |
5916 | { | |
5917 | Dsymbol *s = (*members)[i]; | |
5918 | //printf("\t[%d] semantic on '%s' %p kind %s in '%s'\n", i, s->toChars(), s, s->kind(), this->toChars()); | |
5919 | //printf("test: enclosing = %d, sc2->parent = %s\n", enclosing, sc2->parent->toChars()); | |
5920 | // if (enclosing) | |
5921 | // s->parent = sc->parent; | |
5922 | //printf("test3: enclosing = %d, s->parent = %s\n", enclosing, s->parent->toChars()); | |
5923 | s->semantic(sc2); | |
5924 | //printf("test4: enclosing = %d, s->parent = %s\n", enclosing, s->parent->toChars()); | |
5925 | Module::runDeferredSemantic(); | |
5926 | } | |
5927 | } | |
5928 | ||
5929 | void TemplateInstance::tryExpandMembers(Scope *sc2) | |
5930 | { | |
5931 | static int nest; | |
5932 | // extracted to a function to allow windows SEH to work without destructors in the same function | |
5933 | //printf("%d\n", nest); | |
5934 | if (++nest > 500) | |
5935 | { | |
5936 | global.gag = 0; // ensure error message gets printed | |
5937 | error("recursive expansion"); | |
5938 | fatal(); | |
5939 | } | |
5940 | ||
5941 | expandMembers(sc2); | |
5942 | ||
5943 | nest--; | |
5944 | } | |
5945 | ||
5946 | void TemplateInstance::trySemantic3(Scope *sc2) | |
5947 | { | |
5948 | // extracted to a function to allow windows SEH to work without destructors in the same function | |
5949 | static int nest; | |
5950 | //printf("%d\n", nest); | |
5951 | if (++nest > 300) | |
5952 | { | |
5953 | global.gag = 0; // ensure error message gets printed | |
5954 | error("recursive expansion"); | |
5955 | fatal(); | |
5956 | } | |
5957 | semantic3(sc2); | |
5958 | ||
5959 | --nest; | |
5960 | } | |
5961 | ||
5962 | void TemplateInstance::semantic(Scope *sc, Expressions *fargs) | |
5963 | { | |
5964 | //printf("[%s] TemplateInstance::semantic('%s', this=%p, gag = %d, sc = %p)\n", loc.toChars(), toChars(), this, global.gag, sc); | |
5965 | if (inst) // if semantic() was already run | |
5966 | { | |
5967 | return; | |
5968 | } | |
5969 | if (semanticRun != PASSinit) | |
5970 | { | |
5971 | Ungag ungag(global.gag); | |
5972 | if (!gagged) | |
5973 | global.gag = 0; | |
5974 | error(loc, "recursive template expansion"); | |
5975 | if (gagged) | |
5976 | semanticRun = PASSinit; | |
5977 | else | |
5978 | inst = this; | |
5979 | errors = true; | |
5980 | return; | |
5981 | } | |
5982 | ||
5983 | // Get the enclosing template instance from the scope tinst | |
5984 | tinst = sc->tinst; | |
5985 | ||
5986 | // Get the instantiating module from the scope minst | |
5987 | minst = sc->minst; | |
5988 | // Bugzilla 10920: If the enclosing function is non-root symbol, | |
5989 | // this instance should be speculative. | |
5990 | if (!tinst && sc->func && sc->func->inNonRoot()) | |
5991 | { | |
5992 | minst = NULL; | |
5993 | } | |
5994 | ||
5995 | gagged = (global.gag > 0); | |
5996 | ||
5997 | semanticRun = PASSsemantic; | |
5998 | ||
5999 | /* Find template declaration first, | |
6000 | * then run semantic on each argument (place results in tiargs[]), | |
6001 | * last find most specialized template from overload list/set. | |
6002 | */ | |
6003 | if (!findTempDecl(sc, NULL) || | |
6004 | !semanticTiargs(sc) || | |
6005 | !findBestMatch(sc, fargs)) | |
6006 | { | |
6007 | Lerror: | |
6008 | if (gagged) | |
6009 | { | |
6010 | // Bugzilla 13220: Rollback status for later semantic re-running. | |
6011 | semanticRun = PASSinit; | |
6012 | } | |
6013 | else | |
6014 | inst = this; | |
6015 | errors = true; | |
6016 | return; | |
6017 | } | |
6018 | TemplateDeclaration *tempdecl = this->tempdecl->isTemplateDeclaration(); | |
6019 | assert(tempdecl); | |
6020 | ||
6021 | // If tempdecl is a mixin, disallow it | |
6022 | if (tempdecl->ismixin) | |
6023 | { | |
6024 | error("mixin templates are not regular templates"); | |
6025 | goto Lerror; | |
6026 | } | |
6027 | ||
6028 | hasNestedArgs(tiargs, tempdecl->isstatic); | |
6029 | if (errors) | |
6030 | goto Lerror; | |
6031 | ||
6032 | /* See if there is an existing TemplateInstantiation that already | |
6033 | * implements the typeargs. If so, just refer to that one instead. | |
6034 | */ | |
6035 | inst = tempdecl->findExistingInstance(this, fargs); | |
6036 | TemplateInstance *errinst = NULL; | |
6037 | if (!inst) | |
6038 | { | |
6039 | // So, we need to implement 'this' instance. | |
6040 | } | |
6041 | else if (inst->gagged && !gagged && inst->errors) | |
6042 | { | |
6043 | // If the first instantiation had failed, re-run semantic, | |
6044 | // so that error messages are shown. | |
6045 | errinst = inst; | |
6046 | } | |
6047 | else | |
6048 | { | |
6049 | // It's a match | |
6050 | parent = inst->parent; | |
6051 | errors = inst->errors; | |
6052 | ||
6053 | // If both this and the previous instantiation were gagged, | |
6054 | // use the number of errors that happened last time. | |
6055 | global.errors += errors; | |
6056 | global.gaggedErrors += errors; | |
6057 | ||
6058 | // If the first instantiation was gagged, but this is not: | |
6059 | if (inst->gagged) | |
6060 | { | |
6061 | // It had succeeded, mark it is a non-gagged instantiation, | |
6062 | // and reuse it. | |
6063 | inst->gagged = gagged; | |
6064 | } | |
6065 | ||
6066 | this->tnext = inst->tnext; | |
6067 | inst->tnext = this; | |
6068 | ||
6069 | /* A module can have explicit template instance and its alias | |
6070 | * in module scope (e,g, `alias Base64 = Base64Impl!('+', '/');`). | |
6071 | * If the first instantiation 'inst' had happened in non-root module, | |
6072 | * compiler can assume that its instantiated code would be included | |
6073 | * in the separately compiled obj/lib file (e.g. phobos.lib). | |
6074 | * | |
6075 | * However, if 'this' second instantiation happened in root module, | |
6076 | * compiler might need to invoke its codegen (Bugzilla 2500 & 2644). | |
6077 | * But whole import graph is not determined until all semantic pass finished, | |
6078 | * so 'inst' should conservatively finish the semantic3 pass for the codegen. | |
6079 | */ | |
6080 | if (minst && minst->isRoot() && !(inst->minst && inst->minst->isRoot())) | |
6081 | { | |
6082 | /* Swap the position of 'inst' and 'this' in the instantiation graph. | |
6083 | * Then, the primary instance `inst` will be changed to a root instance. | |
6084 | * | |
6085 | * Before: | |
6086 | * non-root -> A!() -> B!()[inst] -> C!() | |
6087 | * | | |
6088 | * root -> D!() -> B!()[this] | |
6089 | * | |
6090 | * After: | |
6091 | * non-root -> A!() -> B!()[this] | |
6092 | * | | |
6093 | * root -> D!() -> B!()[inst] -> C!() | |
6094 | */ | |
6095 | Module *mi = minst; | |
6096 | TemplateInstance *ti = tinst; | |
6097 | minst = inst->minst; | |
6098 | tinst = inst->tinst; | |
6099 | inst->minst = mi; | |
6100 | inst->tinst = ti; | |
6101 | ||
6102 | if (minst) // if inst was not speculative | |
6103 | { | |
6104 | /* Add 'inst' once again to the root module members[], then the | |
6105 | * instance members will get codegen chances. | |
6106 | */ | |
6107 | inst->appendToModuleMember(); | |
6108 | } | |
6109 | } | |
6110 | ||
6111 | return; | |
6112 | } | |
6113 | unsigned errorsave = global.errors; | |
6114 | ||
6115 | inst = this; | |
6116 | parent = enclosing ? enclosing : tempdecl->parent; | |
6117 | //printf("parent = '%s'\n", parent->kind()); | |
6118 | ||
6119 | TemplateInstance *tempdecl_instance_idx = tempdecl->addInstance(this); | |
6120 | ||
6121 | //getIdent(); | |
6122 | ||
6123 | // Store the place we added it to in target_symbol_list(_idx) so we can | |
6124 | // remove it later if we encounter an error. | |
6125 | Dsymbols *target_symbol_list = appendToModuleMember(); | |
6126 | size_t target_symbol_list_idx = target_symbol_list ? target_symbol_list->dim - 1 : 0; | |
6127 | ||
6128 | // Copy the syntax trees from the TemplateDeclaration | |
6129 | members = Dsymbol::arraySyntaxCopy(tempdecl->members); | |
6130 | ||
6131 | // resolve TemplateThisParameter | |
6132 | for (size_t i = 0; i < tempdecl->parameters->dim; i++) | |
6133 | { | |
6134 | if ((*tempdecl->parameters)[i]->isTemplateThisParameter() == NULL) | |
6135 | continue; | |
6136 | Type *t = isType((*tiargs)[i]); | |
6137 | assert(t); | |
6138 | if (StorageClass stc = ModToStc(t->mod)) | |
6139 | { | |
6140 | //printf("t = %s, stc = x%llx\n", t->toChars(), stc); | |
6141 | Dsymbols *s = new Dsymbols(); | |
6142 | s->push(new StorageClassDeclaration(stc, members)); | |
6143 | members = s; | |
6144 | } | |
6145 | break; | |
6146 | } | |
6147 | ||
6148 | // Create our own scope for the template parameters | |
6149 | Scope *scope = tempdecl->_scope; | |
6150 | if (tempdecl->semanticRun == PASSinit) | |
6151 | { | |
6152 | error("template instantiation %s forward references template declaration %s", toChars(), tempdecl->toChars()); | |
6153 | return; | |
6154 | } | |
6155 | ||
6156 | argsym = new ScopeDsymbol(); | |
6157 | argsym->parent = scope->parent; | |
6158 | scope = scope->push(argsym); | |
6159 | scope->tinst = this; | |
6160 | scope->minst = minst; | |
6161 | //scope->stc = 0; | |
6162 | ||
6163 | // Declare each template parameter as an alias for the argument type | |
6164 | Scope *paramscope = scope->push(); | |
6165 | paramscope->stc = 0; | |
6166 | paramscope->protection = Prot(PROTpublic); // Bugzilla 14169: template parameters should be public | |
6167 | declareParameters(paramscope); | |
6168 | paramscope->pop(); | |
6169 | ||
6170 | // Add members of template instance to template instance symbol table | |
6171 | // parent = scope->scopesym; | |
6172 | symtab = new DsymbolTable(); | |
6173 | for (size_t i = 0; i < members->dim; i++) | |
6174 | { | |
6175 | Dsymbol *s = (*members)[i]; | |
6176 | s->addMember(scope, this); | |
6177 | } | |
6178 | ||
6179 | /* See if there is only one member of template instance, and that | |
6180 | * member has the same name as the template instance. | |
6181 | * If so, this template instance becomes an alias for that member. | |
6182 | */ | |
6183 | //printf("members->dim = %d\n", members->dim); | |
6184 | if (members->dim) | |
6185 | { | |
6186 | Dsymbol *s; | |
6187 | if (Dsymbol::oneMembers(members, &s, tempdecl->ident) && s) | |
6188 | { | |
6189 | //printf("tempdecl->ident = %s, s = '%s'\n", tempdecl->ident->toChars(), s->kind(), s->toPrettyChars()); | |
6190 | //printf("setting aliasdecl\n"); | |
6191 | aliasdecl = s; | |
6192 | } | |
6193 | } | |
6194 | ||
6195 | /* If function template declaration | |
6196 | */ | |
6197 | if (fargs && aliasdecl) | |
6198 | { | |
6199 | FuncDeclaration *fd = aliasdecl->isFuncDeclaration(); | |
6200 | if (fd) | |
6201 | { | |
6202 | /* Transmit fargs to type so that TypeFunction::semantic() can | |
6203 | * resolve any "auto ref" storage classes. | |
6204 | */ | |
6205 | TypeFunction *tf = (TypeFunction *)fd->type; | |
6206 | if (tf && tf->ty == Tfunction) | |
6207 | tf->fargs = fargs; | |
6208 | } | |
6209 | } | |
6210 | ||
6211 | // Do semantic() analysis on template instance members | |
6212 | Scope *sc2; | |
6213 | sc2 = scope->push(this); | |
6214 | //printf("enclosing = %d, sc->parent = %s\n", enclosing, sc->parent->toChars()); | |
6215 | sc2->parent = this; | |
6216 | sc2->tinst = this; | |
6217 | sc2->minst = minst; | |
6218 | ||
6219 | tryExpandMembers(sc2); | |
6220 | ||
6221 | semanticRun = PASSsemanticdone; | |
6222 | ||
6223 | /* ConditionalDeclaration may introduce eponymous declaration, | |
6224 | * so we should find it once again after semantic. | |
6225 | */ | |
6226 | if (members->dim) | |
6227 | { | |
6228 | Dsymbol *s; | |
6229 | if (Dsymbol::oneMembers(members, &s, tempdecl->ident) && s) | |
6230 | { | |
6231 | if (!aliasdecl || aliasdecl != s) | |
6232 | { | |
6233 | //printf("tempdecl->ident = %s, s = '%s'\n", tempdecl->ident->toChars(), s->kind(), s->toPrettyChars()); | |
6234 | //printf("setting aliasdecl 2\n"); | |
6235 | aliasdecl = s; | |
6236 | } | |
6237 | } | |
6238 | } | |
6239 | ||
6240 | if (global.errors != errorsave) | |
6241 | goto Laftersemantic; | |
6242 | ||
6243 | /* If any of the instantiation members didn't get semantic() run | |
6244 | * on them due to forward references, we cannot run semantic2() | |
6245 | * or semantic3() yet. | |
6246 | */ | |
6247 | { | |
6248 | bool found_deferred_ad = false; | |
6249 | for (size_t i = 0; i < Module::deferred.dim; i++) | |
6250 | { | |
6251 | Dsymbol *sd = Module::deferred[i]; | |
6252 | AggregateDeclaration *ad = sd->isAggregateDeclaration(); | |
6253 | if (ad && ad->parent && ad->parent->isTemplateInstance()) | |
6254 | { | |
6255 | //printf("deferred template aggregate: %s %s\n", | |
6256 | // sd->parent->toChars(), sd->toChars()); | |
6257 | found_deferred_ad = true; | |
6258 | if (ad->parent == this) | |
6259 | { | |
6260 | ad->deferred = this; | |
6261 | break; | |
6262 | } | |
6263 | } | |
6264 | } | |
6265 | if (found_deferred_ad || Module::deferred.dim) | |
6266 | goto Laftersemantic; | |
6267 | } | |
6268 | ||
6269 | /* The problem is when to parse the initializer for a variable. | |
6270 | * Perhaps VarDeclaration::semantic() should do it like it does | |
6271 | * for initializers inside a function. | |
6272 | */ | |
6273 | //if (sc->parent->isFuncDeclaration()) | |
6274 | { | |
6275 | /* BUG 782: this has problems if the classes this depends on | |
6276 | * are forward referenced. Find a way to defer semantic() | |
6277 | * on this template. | |
6278 | */ | |
6279 | semantic2(sc2); | |
6280 | } | |
6281 | if (global.errors != errorsave) | |
6282 | goto Laftersemantic; | |
6283 | ||
6284 | if ((sc->func || (sc->flags & SCOPEfullinst)) && !tinst) | |
6285 | { | |
6286 | /* If a template is instantiated inside function, the whole instantiation | |
6287 | * should be done at that position. But, immediate running semantic3 of | |
6288 | * dependent templates may cause unresolved forward reference (Bugzilla 9050). | |
6289 | * To avoid the issue, don't run semantic3 until semantic and semantic2 done. | |
6290 | */ | |
6291 | TemplateInstances deferred; | |
6292 | this->deferred = &deferred; | |
6293 | ||
6294 | //printf("Run semantic3 on %s\n", toChars()); | |
6295 | trySemantic3(sc2); | |
6296 | ||
6297 | for (size_t i = 0; i < deferred.dim; i++) | |
6298 | { | |
6299 | //printf("+ run deferred semantic3 on %s\n", deferred[i]->toChars()); | |
6300 | deferred[i]->semantic3(NULL); | |
6301 | } | |
6302 | ||
6303 | this->deferred = NULL; | |
6304 | } | |
6305 | else if (tinst) | |
6306 | { | |
6307 | bool doSemantic3 = false; | |
6308 | if (sc->func && aliasdecl && aliasdecl->toAlias()->isFuncDeclaration()) | |
6309 | { | |
6310 | /* Template function instantiation should run semantic3 immediately | |
6311 | * for attribute inference. | |
6312 | */ | |
6313 | trySemantic3(sc2); | |
6314 | } | |
6315 | else if (sc->func) | |
6316 | { | |
6317 | /* A lambda function in template arguments might capture the | |
6318 | * instantiated scope context. For the correct context inference, | |
6319 | * all instantiated functions should run the semantic3 immediately. | |
6320 | * See also compilable/test14973.d | |
6321 | */ | |
6322 | for (size_t i = 0; i < tdtypes.dim; i++) | |
6323 | { | |
6324 | RootObject *oarg = tdtypes[i]; | |
6325 | Dsymbol *s = getDsymbol(oarg); | |
6326 | if (!s) | |
6327 | continue; | |
6328 | ||
6329 | if (TemplateDeclaration *td = s->isTemplateDeclaration()) | |
6330 | { | |
6331 | if (!td->literal) | |
6332 | continue; | |
6333 | assert(td->members && td->members->dim == 1); | |
6334 | s = (*td->members)[0]; | |
6335 | } | |
6336 | if (FuncLiteralDeclaration *fld = s->isFuncLiteralDeclaration()) | |
6337 | { | |
6338 | if (fld->tok == TOKreserved) | |
6339 | { | |
6340 | doSemantic3 = true; | |
6341 | break; | |
6342 | } | |
6343 | } | |
6344 | } | |
6345 | //printf("[%s] %s doSemantic3 = %d\n", loc.toChars(), toChars(), doSemantic3); | |
6346 | } | |
6347 | if (doSemantic3) | |
6348 | trySemantic3(sc2); | |
6349 | ||
6350 | TemplateInstance *ti = tinst; | |
6351 | int nest = 0; | |
6352 | while (ti && !ti->deferred && ti->tinst) | |
6353 | { | |
6354 | ti = ti->tinst; | |
6355 | if (++nest > 500) | |
6356 | { | |
6357 | global.gag = 0; // ensure error message gets printed | |
6358 | error("recursive expansion"); | |
6359 | fatal(); | |
6360 | } | |
6361 | } | |
6362 | if (ti && ti->deferred) | |
6363 | { | |
6364 | //printf("deferred semantic3 of %p %s, ti = %s, ti->deferred = %p\n", this, toChars(), ti->toChars()); | |
6365 | for (size_t i = 0; ; i++) | |
6366 | { | |
6367 | if (i == ti->deferred->dim) | |
6368 | { | |
6369 | ti->deferred->push(this); | |
6370 | break; | |
6371 | } | |
6372 | if ((*ti->deferred)[i] == this) | |
6373 | break; | |
6374 | } | |
6375 | } | |
6376 | } | |
6377 | ||
6378 | if (aliasdecl) | |
6379 | { | |
6380 | /* Bugzilla 13816: AliasDeclaration tries to resolve forward reference | |
6381 | * twice (See inuse check in AliasDeclaration::toAlias()). It's | |
6382 | * necessary to resolve mutual references of instantiated symbols, but | |
6383 | * it will left a true recursive alias in tuple declaration - an | |
6384 | * AliasDeclaration A refers TupleDeclaration B, and B contains A | |
6385 | * in its elements. To correctly make it an error, we strictly need to | |
6386 | * resolve the alias of eponymous member. | |
6387 | */ | |
6388 | aliasdecl = aliasdecl->toAlias2(); | |
6389 | } | |
6390 | ||
6391 | Laftersemantic: | |
6392 | sc2->pop(); | |
6393 | ||
6394 | scope->pop(); | |
6395 | ||
6396 | // Give additional context info if error occurred during instantiation | |
6397 | if (global.errors != errorsave) | |
6398 | { | |
6399 | if (!errors) | |
6400 | { | |
6401 | if (!tempdecl->literal) | |
6402 | error(loc, "error instantiating"); | |
6403 | if (tinst) | |
6404 | tinst->printInstantiationTrace(); | |
6405 | } | |
6406 | errors = true; | |
6407 | if (gagged) | |
6408 | { | |
6409 | // Errors are gagged, so remove the template instance from the | |
6410 | // instance/symbol lists we added it to and reset our state to | |
6411 | // finish clean and so we can try to instantiate it again later | |
6412 | // (see bugzilla 4302 and 6602). | |
6413 | tempdecl->removeInstance(tempdecl_instance_idx); | |
6414 | if (target_symbol_list) | |
6415 | { | |
6416 | // Because we added 'this' in the last position above, we | |
6417 | // should be able to remove it without messing other indices up. | |
6418 | assert((*target_symbol_list)[target_symbol_list_idx] == this); | |
6419 | target_symbol_list->remove(target_symbol_list_idx); | |
6420 | memberOf = NULL; // no longer a member | |
6421 | } | |
6422 | semanticRun = PASSinit; | |
6423 | inst = NULL; | |
6424 | symtab = NULL; | |
6425 | } | |
6426 | } | |
6427 | else if (errinst) | |
6428 | { | |
6429 | /* Bugzilla 14541: If the previous gagged instance had failed by | |
6430 | * circular references, currrent "error reproduction instantiation" | |
6431 | * might succeed, because of the difference of instantiated context. | |
6432 | * On such case, the cached error instance needs to be overridden by the | |
6433 | * succeeded instance. | |
6434 | */ | |
6435 | //printf("replaceInstance()\n"); | |
6436 | TemplateInstances *tinstances = (TemplateInstances *)dmd_aaGetRvalue((AA *)tempdecl->instances, (void *)hash); | |
6437 | assert(tinstances); | |
6438 | for (size_t i = 0; i < tinstances->dim; i++) | |
6439 | { | |
6440 | TemplateInstance *ti = (*tinstances)[i]; | |
6441 | if (ti == errinst) | |
6442 | { | |
6443 | (*tinstances)[i] = this; // override | |
6444 | break; | |
6445 | } | |
6446 | } | |
6447 | } | |
6448 | } | |
6449 | ||
6450 | ||
6451 | /********************************************** | |
6452 | * Find template declaration corresponding to template instance. | |
6453 | * | |
6454 | * Returns: | |
6455 | * false if finding fails. | |
6456 | * Note: | |
6457 | * This function is reentrant against error occurrence. If returns false, | |
6458 | * any members of this object won't be modified, and repetition call will | |
6459 | * reproduce same error. | |
6460 | */ | |
6461 | ||
6462 | bool TemplateInstance::findTempDecl(Scope *sc, WithScopeSymbol **pwithsym) | |
6463 | { | |
6464 | if (pwithsym) | |
6465 | *pwithsym = NULL; | |
6466 | ||
6467 | if (havetempdecl) | |
6468 | return true; | |
6469 | ||
6470 | //printf("TemplateInstance::findTempDecl() %s\n", toChars()); | |
6471 | if (!tempdecl) | |
6472 | { | |
6473 | /* Given: | |
6474 | * foo!( ... ) | |
6475 | * figure out which TemplateDeclaration foo refers to. | |
6476 | */ | |
6477 | Identifier *id = name; | |
6478 | Dsymbol *scopesym; | |
6479 | Dsymbol *s = sc->search(loc, id, &scopesym); | |
6480 | if (!s) | |
6481 | { | |
6482 | s = sc->search_correct(id); | |
6483 | if (s) | |
6484 | error("template '%s' is not defined, did you mean %s?", id->toChars(), s->toChars()); | |
6485 | else | |
6486 | error("template '%s' is not defined", id->toChars()); | |
6487 | return false; | |
6488 | } | |
6489 | ||
6490 | if (pwithsym) | |
6491 | *pwithsym = scopesym->isWithScopeSymbol(); | |
6492 | ||
6493 | /* We might have found an alias within a template when | |
6494 | * we really want the template. | |
6495 | */ | |
6496 | TemplateInstance *ti; | |
6497 | if (s->parent && | |
6498 | (ti = s->parent->isTemplateInstance()) != NULL) | |
6499 | { | |
6500 | if (ti->tempdecl && ti->tempdecl->ident == id) | |
6501 | { | |
6502 | /* This is so that one can refer to the enclosing | |
6503 | * template, even if it has the same name as a member | |
6504 | * of the template, if it has a !(arguments) | |
6505 | */ | |
6506 | TemplateDeclaration *td = ti->tempdecl->isTemplateDeclaration(); | |
6507 | assert(td); | |
6508 | if (td->overroot) // if not start of overloaded list of TemplateDeclaration's | |
6509 | td = td->overroot; // then get the start | |
6510 | s = td; | |
6511 | } | |
6512 | } | |
6513 | ||
6514 | if (!updateTempDecl(sc, s)) | |
6515 | { | |
6516 | return false; | |
6517 | } | |
6518 | } | |
6519 | assert(tempdecl); | |
6520 | ||
6521 | struct ParamFwdTi | |
6522 | { | |
6523 | static int fp(void *param, Dsymbol *s) | |
6524 | { | |
6525 | TemplateDeclaration *td = s->isTemplateDeclaration(); | |
6526 | if (!td) | |
6527 | return 0; | |
6528 | ||
6529 | TemplateInstance *ti = (TemplateInstance *)param; | |
6530 | if (td->semanticRun == PASSinit) | |
6531 | { | |
6532 | if (td->_scope) | |
6533 | { | |
6534 | // Try to fix forward reference. Ungag errors while doing so. | |
6535 | Ungag ungag = td->ungagSpeculative(); | |
6536 | td->semantic(td->_scope); | |
6537 | } | |
6538 | if (td->semanticRun == PASSinit) | |
6539 | { | |
6540 | ti->error("%s forward references template declaration %s", ti->toChars(), td->toChars()); | |
6541 | return 1; | |
6542 | } | |
6543 | } | |
6544 | return 0; | |
6545 | } | |
6546 | }; | |
6547 | // Look for forward references | |
6548 | OverloadSet *tovers = tempdecl->isOverloadSet(); | |
6549 | size_t overs_dim = tovers ? tovers->a.dim : 1; | |
6550 | for (size_t oi = 0; oi < overs_dim; oi++) | |
6551 | { | |
6552 | if (overloadApply(tovers ? tovers->a[oi] : tempdecl, (void *)this, &ParamFwdTi::fp)) | |
6553 | return false; | |
6554 | } | |
6555 | return true; | |
6556 | } | |
6557 | ||
6558 | /********************************************** | |
6559 | * Confirm s is a valid template, then store it. | |
6560 | * Input: | |
6561 | * sc | |
6562 | * s candidate symbol of template. It may be: | |
6563 | * TemplateDeclaration | |
6564 | * FuncDeclaration with findTemplateDeclRoot() != NULL | |
6565 | * OverloadSet which contains candidates | |
6566 | * Returns: | |
6567 | * true if updating succeeds. | |
6568 | */ | |
6569 | ||
6570 | bool TemplateInstance::updateTempDecl(Scope *sc, Dsymbol *s) | |
6571 | { | |
6572 | if (s) | |
6573 | { | |
6574 | Identifier *id = name; | |
6575 | s = s->toAlias(); | |
6576 | ||
6577 | /* If an OverloadSet, look for a unique member that is a template declaration | |
6578 | */ | |
6579 | OverloadSet *os = s->isOverloadSet(); | |
6580 | if (os) | |
6581 | { | |
6582 | s = NULL; | |
6583 | for (size_t i = 0; i < os->a.dim; i++) | |
6584 | { | |
6585 | Dsymbol *s2 = os->a[i]; | |
6586 | if (FuncDeclaration *f = s2->isFuncDeclaration()) | |
6587 | s2 = f->findTemplateDeclRoot(); | |
6588 | else | |
6589 | s2 = s2->isTemplateDeclaration(); | |
6590 | if (s2) | |
6591 | { | |
6592 | if (s) | |
6593 | { | |
6594 | tempdecl = os; | |
6595 | return true; | |
6596 | } | |
6597 | s = s2; | |
6598 | } | |
6599 | } | |
6600 | if (!s) | |
6601 | { | |
6602 | error("template '%s' is not defined", id->toChars()); | |
6603 | return false; | |
6604 | } | |
6605 | } | |
6606 | ||
6607 | OverDeclaration *od = s->isOverDeclaration(); | |
6608 | if (od) | |
6609 | { | |
6610 | tempdecl = od; // TODO: more strict check | |
6611 | return true; | |
6612 | } | |
6613 | ||
6614 | /* It should be a TemplateDeclaration, not some other symbol | |
6615 | */ | |
6616 | if (FuncDeclaration *f = s->isFuncDeclaration()) | |
6617 | tempdecl = f->findTemplateDeclRoot(); | |
6618 | else | |
6619 | tempdecl = s->isTemplateDeclaration(); | |
6620 | if (!tempdecl) | |
6621 | { | |
6622 | if (!s->parent && global.errors) | |
6623 | return false; | |
6624 | if (!s->parent && s->getType()) | |
6625 | { | |
6626 | Dsymbol *s2 = s->getType()->toDsymbol(sc); | |
6627 | if (!s2) | |
6628 | { | |
6629 | error("%s is not a template declaration, it is a %s", id->toChars(), s->kind()); | |
6630 | return false; | |
6631 | } | |
6632 | s = s2; | |
6633 | } | |
6634 | //assert(s->parent); | |
6635 | TemplateInstance *ti = s->parent ? s->parent->isTemplateInstance() : NULL; | |
6636 | if (ti && | |
6637 | (ti->name == s->ident || | |
6638 | ti->toAlias()->ident == s->ident) | |
6639 | && | |
6640 | ti->tempdecl) | |
6641 | { | |
6642 | /* This is so that one can refer to the enclosing | |
6643 | * template, even if it has the same name as a member | |
6644 | * of the template, if it has a !(arguments) | |
6645 | */ | |
6646 | TemplateDeclaration *td = ti->tempdecl->isTemplateDeclaration(); | |
6647 | assert(td); | |
6648 | if (td->overroot) // if not start of overloaded list of TemplateDeclaration's | |
6649 | td = td->overroot; // then get the start | |
6650 | tempdecl = td; | |
6651 | } | |
6652 | else | |
6653 | { | |
6654 | error("%s is not a template declaration, it is a %s", id->toChars(), s->kind()); | |
6655 | return false; | |
6656 | } | |
6657 | } | |
6658 | } | |
6659 | return (tempdecl != NULL); | |
6660 | } | |
6661 | ||
6662 | /********************************** | |
6663 | * Run semantic on the elements of tiargs. | |
6664 | * Input: | |
6665 | * sc | |
6666 | * Returns: | |
6667 | * false if one or more arguments have errors. | |
6668 | * Note: | |
6669 | * This function is reentrant against error occurrence. If returns false, | |
6670 | * all elements of tiargs won't be modified. | |
6671 | */ | |
6672 | ||
6673 | bool TemplateInstance::semanticTiargs(Scope *sc) | |
6674 | { | |
6675 | //printf("+TemplateInstance::semanticTiargs() %s\n", toChars()); | |
6676 | if (semantictiargsdone) | |
6677 | return true; | |
6678 | if (semanticTiargs(loc, sc, tiargs, 0)) | |
6679 | { | |
6680 | // cache the result iff semantic analysis succeeded entirely | |
6681 | semantictiargsdone = 1; | |
6682 | return true; | |
6683 | } | |
6684 | return false; | |
6685 | } | |
6686 | ||
6687 | /********************************** | |
6688 | * Run semantic of tiargs as arguments of template. | |
6689 | * Input: | |
6690 | * loc | |
6691 | * sc | |
6692 | * tiargs array of template arguments | |
6693 | * flags 1: replace const variables with their initializers | |
6694 | * 2: don't devolve Parameter to Type | |
6695 | * Returns: | |
6696 | * false if one or more arguments have errors. | |
6697 | */ | |
6698 | ||
6699 | bool TemplateInstance::semanticTiargs(Loc loc, Scope *sc, Objects *tiargs, int flags) | |
6700 | { | |
6701 | // Run semantic on each argument, place results in tiargs[] | |
6702 | //printf("+TemplateInstance::semanticTiargs()\n"); | |
6703 | if (!tiargs) | |
6704 | return true; | |
6705 | bool err = false; | |
6706 | for (size_t j = 0; j < tiargs->dim; j++) | |
6707 | { | |
6708 | RootObject *o = (*tiargs)[j]; | |
6709 | Type *ta = isType(o); | |
6710 | Expression *ea = isExpression(o); | |
6711 | Dsymbol *sa = isDsymbol(o); | |
6712 | ||
6713 | //printf("1: (*tiargs)[%d] = %p, s=%p, v=%p, ea=%p, ta=%p\n", j, o, isDsymbol(o), isTuple(o), ea, ta); | |
6714 | if (ta) | |
6715 | { | |
6716 | //printf("type %s\n", ta->toChars()); | |
6717 | // It might really be an Expression or an Alias | |
6718 | ta->resolve(loc, sc, &ea, &ta, &sa); | |
6719 | if (ea) goto Lexpr; | |
6720 | if (sa) goto Ldsym; | |
6721 | if (ta == NULL) | |
6722 | { | |
6723 | assert(global.errors); | |
6724 | ta = Type::terror; | |
6725 | } | |
6726 | ||
6727 | Ltype: | |
6728 | if (ta->ty == Ttuple) | |
6729 | { | |
6730 | // Expand tuple | |
6731 | TypeTuple *tt = (TypeTuple *)ta; | |
6732 | size_t dim = tt->arguments->dim; | |
6733 | tiargs->remove(j); | |
6734 | if (dim) | |
6735 | { | |
6736 | tiargs->reserve(dim); | |
6737 | for (size_t i = 0; i < dim; i++) | |
6738 | { | |
6739 | Parameter *arg = (*tt->arguments)[i]; | |
6740 | if (flags & 2 && arg->ident) | |
6741 | tiargs->insert(j + i, arg); | |
6742 | else | |
6743 | tiargs->insert(j + i, arg->type); | |
6744 | } | |
6745 | } | |
6746 | j--; | |
6747 | continue; | |
6748 | } | |
6749 | if (ta->ty == Terror) | |
6750 | { | |
6751 | err = true; | |
6752 | continue; | |
6753 | } | |
6754 | (*tiargs)[j] = ta->merge2(); | |
6755 | } | |
6756 | else if (ea) | |
6757 | { | |
6758 | Lexpr: | |
6759 | //printf("+[%d] ea = %s %s\n", j, Token::toChars(ea->op), ea->toChars()); | |
6760 | if (flags & 1) // only used by __traits | |
6761 | { | |
6762 | ea = ::semantic(ea, sc); | |
6763 | ||
6764 | // must not interpret the args, excepting template parameters | |
6765 | if (ea->op != TOKvar || | |
6766 | (((VarExp *)ea)->var->storage_class & STCtemplateparameter)) | |
6767 | { | |
6768 | ea = ea->optimize(WANTvalue); | |
6769 | } | |
6770 | } | |
6771 | else | |
6772 | { | |
6773 | sc = sc->startCTFE(); | |
6774 | ea = ::semantic(ea, sc); | |
6775 | sc = sc->endCTFE(); | |
6776 | ||
6777 | if (ea->op == TOKvar) | |
6778 | { | |
6779 | /* This test is to skip substituting a const var with | |
6780 | * its initializer. The problem is the initializer won't | |
6781 | * match with an 'alias' parameter. Instead, do the | |
6782 | * const substitution in TemplateValueParameter::matchArg(). | |
6783 | */ | |
6784 | } | |
6785 | else if (definitelyValueParameter(ea)) | |
6786 | { | |
6787 | if (ea->checkValue()) // check void expression | |
6788 | ea = new ErrorExp(); | |
6789 | unsigned int olderrs = global.errors; | |
6790 | ea = ea->ctfeInterpret(); | |
6791 | if (global.errors != olderrs) | |
6792 | ea = new ErrorExp(); | |
6793 | } | |
6794 | } | |
6795 | //printf("-[%d] ea = %s %s\n", j, Token::toChars(ea->op), ea->toChars()); | |
6796 | if (ea->op == TOKtuple) | |
6797 | { | |
6798 | // Expand tuple | |
6799 | TupleExp *te = (TupleExp *)ea; | |
6800 | size_t dim = te->exps->dim; | |
6801 | tiargs->remove(j); | |
6802 | if (dim) | |
6803 | { | |
6804 | tiargs->reserve(dim); | |
6805 | for (size_t i = 0; i < dim; i++) | |
6806 | tiargs->insert(j + i, (*te->exps)[i]); | |
6807 | } | |
6808 | j--; | |
6809 | continue; | |
6810 | } | |
6811 | if (ea->op == TOKerror) | |
6812 | { | |
6813 | err = true; | |
6814 | continue; | |
6815 | } | |
6816 | (*tiargs)[j] = ea; | |
6817 | ||
6818 | if (ea->op == TOKtype) | |
6819 | { | |
6820 | ta = ea->type; | |
6821 | goto Ltype; | |
6822 | } | |
6823 | if (ea->op == TOKscope) | |
6824 | { | |
6825 | sa = ((ScopeExp *)ea)->sds; | |
6826 | goto Ldsym; | |
6827 | } | |
6828 | if (ea->op == TOKfunction) | |
6829 | { | |
6830 | FuncExp *fe = (FuncExp *)ea; | |
6831 | /* A function literal, that is passed to template and | |
6832 | * already semanticed as function pointer, never requires | |
6833 | * outer frame. So convert it to global function is valid. | |
6834 | */ | |
6835 | if (fe->fd->tok == TOKreserved && fe->type->ty == Tpointer) | |
6836 | { | |
6837 | // change to non-nested | |
6838 | fe->fd->tok = TOKfunction; | |
6839 | fe->fd->vthis = NULL; | |
6840 | } | |
6841 | else if (fe->td) | |
6842 | { | |
6843 | /* If template argument is a template lambda, | |
6844 | * get template declaration itself. */ | |
6845 | //sa = fe->td; | |
6846 | //goto Ldsym; | |
6847 | } | |
6848 | } | |
6849 | if (ea->op == TOKdotvar) | |
6850 | { | |
6851 | // translate expression to dsymbol. | |
6852 | sa = ((DotVarExp *)ea)->var; | |
6853 | goto Ldsym; | |
6854 | } | |
6855 | if (ea->op == TOKtemplate) | |
6856 | { | |
6857 | sa = ((TemplateExp *)ea)->td; | |
6858 | goto Ldsym; | |
6859 | } | |
6860 | if (ea->op == TOKdottd) | |
6861 | { | |
6862 | // translate expression to dsymbol. | |
6863 | sa = ((DotTemplateExp *)ea)->td; | |
6864 | goto Ldsym; | |
6865 | } | |
6866 | } | |
6867 | else if (sa) | |
6868 | { | |
6869 | Ldsym: | |
6870 | //printf("dsym %s %s\n", sa->kind(), sa->toChars()); | |
6871 | if (sa->errors) | |
6872 | { | |
6873 | err = true; | |
6874 | continue; | |
6875 | } | |
6876 | ||
6877 | TupleDeclaration *d = sa->toAlias()->isTupleDeclaration(); | |
6878 | if (d) | |
6879 | { | |
6880 | // Expand tuple | |
6881 | tiargs->remove(j); | |
6882 | tiargs->insert(j, d->objects); | |
6883 | j--; | |
6884 | continue; | |
6885 | } | |
6886 | if (FuncAliasDeclaration *fa = sa->isFuncAliasDeclaration()) | |
6887 | { | |
6888 | FuncDeclaration *f = fa->toAliasFunc(); | |
6889 | if (!fa->hasOverloads && f->isUnique()) | |
6890 | { | |
6891 | // Strip FuncAlias only when the aliased function | |
6892 | // does not have any overloads. | |
6893 | sa = f; | |
6894 | } | |
6895 | } | |
6896 | (*tiargs)[j] = sa; | |
6897 | ||
6898 | TemplateDeclaration *td = sa->isTemplateDeclaration(); | |
6899 | if (td && td->semanticRun == PASSinit && td->literal) | |
6900 | { | |
6901 | td->semantic(sc); | |
6902 | } | |
6903 | FuncDeclaration *fd = sa->isFuncDeclaration(); | |
6904 | if (fd) | |
6905 | fd->functionSemantic(); | |
6906 | } | |
6907 | else if (isParameter(o)) | |
6908 | { | |
6909 | } | |
6910 | else | |
6911 | { | |
6912 | assert(0); | |
6913 | } | |
6914 | //printf("1: (*tiargs)[%d] = %p\n", j, (*tiargs)[j]); | |
6915 | } | |
6916 | return !err; | |
6917 | } | |
6918 | ||
6919 | bool TemplateInstance::findBestMatch(Scope *sc, Expressions *fargs) | |
6920 | { | |
6921 | if (havetempdecl) | |
6922 | { | |
6923 | TemplateDeclaration *tempdecl = this->tempdecl->isTemplateDeclaration(); | |
6924 | assert(tempdecl); | |
6925 | assert(tempdecl->_scope); | |
6926 | // Deduce tdtypes | |
6927 | tdtypes.setDim(tempdecl->parameters->dim); | |
6928 | if (!tempdecl->matchWithInstance(sc, this, &tdtypes, fargs, 2)) | |
6929 | { | |
6930 | error("incompatible arguments for template instantiation"); | |
6931 | return false; | |
6932 | } | |
6933 | // TODO: Normalizing tiargs for bugzilla 7469 is necessary? | |
6934 | return true; | |
6935 | } | |
6936 | ||
6937 | unsigned errs = global.errors; | |
6938 | ||
6939 | struct ParamBest | |
6940 | { | |
6941 | // context | |
6942 | Scope *sc; | |
6943 | TemplateInstance *ti; | |
6944 | Objects dedtypes; | |
6945 | // result | |
6946 | TemplateDeclaration *td_best; | |
6947 | TemplateDeclaration *td_ambig; | |
6948 | MATCH m_best; | |
6949 | ||
6950 | static int fp(void *param, Dsymbol *s) | |
6951 | { | |
6952 | return ((ParamBest *)param)->fp(s); | |
6953 | } | |
6954 | int fp(Dsymbol *s) | |
6955 | { | |
6956 | TemplateDeclaration *td = s->isTemplateDeclaration(); | |
6957 | if (!td) | |
6958 | return 0; | |
6959 | ||
6960 | if (td == td_best) // skip duplicates | |
6961 | return 0; | |
6962 | ||
6963 | //printf("td = %s\n", td->toPrettyChars()); | |
6964 | ||
6965 | // If more arguments than parameters, | |
6966 | // then this is no match. | |
6967 | if (td->parameters->dim < ti->tiargs->dim) | |
6968 | { | |
6969 | if (!td->isVariadic()) | |
6970 | return 0; | |
6971 | } | |
6972 | ||
6973 | dedtypes.setDim(td->parameters->dim); | |
6974 | dedtypes.zero(); | |
6975 | assert(td->semanticRun != PASSinit); | |
6976 | MATCH m = td->matchWithInstance(sc, ti, &dedtypes, ti->fargs, 0); | |
6977 | //printf("matchWithInstance = %d\n", m); | |
6978 | if (m <= MATCHnomatch) // no match at all | |
6979 | return 0; | |
6980 | ||
6981 | if (m < m_best) goto Ltd_best; | |
6982 | if (m > m_best) goto Ltd; | |
6983 | ||
6984 | { | |
6985 | // Disambiguate by picking the most specialized TemplateDeclaration | |
6986 | MATCH c1 = td->leastAsSpecialized(sc, td_best, ti->fargs); | |
6987 | MATCH c2 = td_best->leastAsSpecialized(sc, td, ti->fargs); | |
6988 | //printf("c1 = %d, c2 = %d\n", c1, c2); | |
6989 | if (c1 > c2) goto Ltd; | |
6990 | if (c1 < c2) goto Ltd_best; | |
6991 | } | |
6992 | ||
6993 | td_ambig = td; | |
6994 | return 0; | |
6995 | ||
6996 | Ltd_best: // td_best is the best match so far | |
6997 | td_ambig = NULL; | |
6998 | return 0; | |
6999 | ||
7000 | Ltd: // td is the new best match | |
7001 | td_ambig = NULL; | |
7002 | td_best = td; | |
7003 | m_best = m; | |
7004 | ti->tdtypes.setDim(dedtypes.dim); | |
7005 | memcpy(ti->tdtypes.tdata(), dedtypes.tdata(), ti->tdtypes.dim * sizeof(void *)); | |
7006 | return 0; | |
7007 | } | |
7008 | }; | |
7009 | ParamBest p; | |
7010 | // context | |
7011 | p.ti = this; | |
7012 | p.sc = sc; | |
7013 | ||
7014 | /* Since there can be multiple TemplateDeclaration's with the same | |
7015 | * name, look for the best match. | |
7016 | */ | |
7017 | TemplateDeclaration *td_last = NULL; | |
7018 | ||
7019 | OverloadSet *tovers = tempdecl->isOverloadSet(); | |
7020 | size_t overs_dim = tovers ? tovers->a.dim : 1; | |
7021 | for (size_t oi = 0; oi < overs_dim; oi++) | |
7022 | { | |
7023 | // result | |
7024 | p.td_best = NULL; | |
7025 | p.td_ambig = NULL; | |
7026 | p.m_best = MATCHnomatch; | |
7027 | overloadApply(tovers ? tovers->a[oi] : tempdecl, &p, &ParamBest::fp); | |
7028 | ||
7029 | if (p.td_ambig) | |
7030 | { | |
7031 | ::error(loc, "%s %s.%s matches more than one template declaration:\n%s: %s\nand\n%s: %s", | |
7032 | p.td_best->kind(), p.td_best->parent->toPrettyChars(), p.td_best->ident->toChars(), | |
7033 | p.td_best->loc.toChars() , p.td_best->toChars(), | |
7034 | p.td_ambig->loc.toChars(), p.td_ambig->toChars()); | |
7035 | return false; | |
7036 | } | |
7037 | if (p.td_best) | |
7038 | { | |
7039 | if (!td_last) | |
7040 | td_last = p.td_best; | |
7041 | else if (td_last != p.td_best) | |
7042 | { | |
7043 | ScopeDsymbol::multiplyDefined(loc, td_last, p.td_best); | |
7044 | return false; | |
7045 | } | |
7046 | } | |
7047 | } | |
7048 | ||
7049 | if (td_last) | |
7050 | { | |
7051 | /* Bugzilla 7469: Normalize tiargs by using corresponding deduced | |
7052 | * template value parameters and tuples for the correct mangling. | |
7053 | * | |
7054 | * By doing this before hasNestedArgs, CTFEable local variable will be | |
7055 | * accepted as a value parameter. For example: | |
7056 | * | |
7057 | * void foo() { | |
7058 | * struct S(int n) {} // non-global template | |
7059 | * const int num = 1; // CTFEable local variable | |
7060 | * S!num s; // S!1 is instantiated, not S!num | |
7061 | * } | |
7062 | */ | |
7063 | size_t dim = td_last->parameters->dim - (td_last->isVariadic() ? 1 : 0); | |
7064 | for (size_t i = 0; i < dim; i++) | |
7065 | { | |
7066 | if (tiargs->dim <= i) | |
7067 | tiargs->push(tdtypes[i]); | |
7068 | assert(i < tiargs->dim); | |
7069 | ||
7070 | TemplateValueParameter *tvp = (*td_last->parameters)[i]->isTemplateValueParameter(); | |
7071 | if (!tvp) | |
7072 | continue; | |
7073 | assert(tdtypes[i]); | |
7074 | // tdtypes[i] is already normalized to the required type in matchArg | |
7075 | ||
7076 | (*tiargs)[i] = tdtypes[i]; | |
7077 | } | |
7078 | if (td_last->isVariadic() && tiargs->dim == dim && tdtypes[dim]) | |
7079 | { | |
7080 | Tuple *va = isTuple(tdtypes[dim]); | |
7081 | assert(va); | |
7082 | for (size_t i = 0; i < va->objects.dim; i++) | |
7083 | tiargs->push(va->objects[i]); | |
7084 | } | |
7085 | } | |
7086 | else if (errors && inst) | |
7087 | { | |
7088 | // instantiation was failed with error reporting | |
7089 | assert(global.errors); | |
7090 | return false; | |
7091 | } | |
7092 | else | |
7093 | { | |
7094 | TemplateDeclaration *tdecl = tempdecl->isTemplateDeclaration(); | |
7095 | ||
7096 | if (errs != global.errors) | |
7097 | errorSupplemental(loc, "while looking for match for %s", toChars()); | |
7098 | else if (tdecl && !tdecl->overnext) | |
7099 | { | |
7100 | // Only one template, so we can give better error message | |
7101 | error("does not match template declaration %s", tdecl->toChars()); | |
7102 | } | |
7103 | else | |
7104 | ::error(loc, "%s %s.%s does not match any template declaration", | |
7105 | tempdecl->kind(), tempdecl->parent->toPrettyChars(), tempdecl->ident->toChars()); | |
7106 | return false; | |
7107 | } | |
7108 | ||
7109 | /* The best match is td_last | |
7110 | */ | |
7111 | tempdecl = td_last; | |
7112 | ||
7113 | return (errs == global.errors); | |
7114 | } | |
7115 | ||
7116 | /***************************************************** | |
7117 | * Determine if template instance is really a template function, | |
7118 | * and that template function needs to infer types from the function | |
7119 | * arguments. | |
7120 | * | |
7121 | * Like findBestMatch, iterate possible template candidates, | |
7122 | * but just looks only the necessity of type inference. | |
7123 | */ | |
7124 | ||
7125 | bool TemplateInstance::needsTypeInference(Scope *sc, int flag) | |
7126 | { | |
7127 | //printf("TemplateInstance::needsTypeInference() %s\n", toChars()); | |
7128 | if (semanticRun != PASSinit) | |
7129 | return false; | |
7130 | ||
7131 | struct ParamNeedsInf | |
7132 | { | |
7133 | // context | |
7134 | Scope *sc; | |
7135 | TemplateInstance *ti; | |
7136 | int flag; | |
7137 | // result | |
7138 | Objects dedtypes; | |
7139 | size_t count; | |
7140 | ||
7141 | static int fp(void *param, Dsymbol *s) | |
7142 | { | |
7143 | return ((ParamNeedsInf *)param)->fp(s); | |
7144 | } | |
7145 | int fp(Dsymbol *s) | |
7146 | { | |
7147 | TemplateDeclaration *td = s->isTemplateDeclaration(); | |
7148 | if (!td) | |
7149 | { | |
7150 | return 0; | |
7151 | } | |
7152 | ||
7153 | /* If any of the overloaded template declarations need inference, | |
7154 | * then return true | |
7155 | */ | |
7156 | FuncDeclaration *fd; | |
7157 | if (!td->onemember) | |
7158 | return 0; | |
7159 | if (TemplateDeclaration *td2 = td->onemember->isTemplateDeclaration()) | |
7160 | { | |
7161 | if (!td2->onemember || !td2->onemember->isFuncDeclaration()) | |
7162 | return 0; | |
7163 | if (ti->tiargs->dim >= td->parameters->dim - (td->isVariadic() ? 1 : 0)) | |
7164 | return 0; | |
7165 | return 1; | |
7166 | } | |
7167 | if ((fd = td->onemember->isFuncDeclaration()) == NULL || | |
7168 | fd->type->ty != Tfunction) | |
7169 | { | |
7170 | return 0; | |
7171 | } | |
7172 | ||
7173 | for (size_t i = 0; i < td->parameters->dim; i++) | |
7174 | { | |
7175 | if ((*td->parameters)[i]->isTemplateThisParameter()) | |
7176 | return 1; | |
7177 | } | |
7178 | ||
7179 | /* Determine if the instance arguments, tiargs, are all that is necessary | |
7180 | * to instantiate the template. | |
7181 | */ | |
7182 | //printf("tp = %p, td->parameters->dim = %d, tiargs->dim = %d\n", tp, td->parameters->dim, ti->tiargs->dim); | |
7183 | TypeFunction *tf = (TypeFunction *)fd->type; | |
7184 | if (size_t dim = Parameter::dim(tf->parameters)) | |
7185 | { | |
7186 | TemplateParameter *tp = td->isVariadic(); | |
7187 | if (tp && td->parameters->dim > 1) | |
7188 | return 1; | |
7189 | ||
7190 | if (!tp && ti->tiargs->dim < td->parameters->dim) | |
7191 | { | |
7192 | // Can remain tiargs be filled by default arguments? | |
7193 | for (size_t i = ti->tiargs->dim; i < td->parameters->dim; i++) | |
7194 | { | |
7195 | if (!(*td->parameters)[i]->hasDefaultArg()) | |
7196 | return 1; | |
7197 | } | |
7198 | } | |
7199 | ||
7200 | for (size_t i = 0; i < dim; i++) | |
7201 | { | |
7202 | // 'auto ref' needs inference. | |
7203 | if (Parameter::getNth(tf->parameters, i)->storageClass & STCauto) | |
7204 | return 1; | |
7205 | } | |
7206 | } | |
7207 | ||
7208 | if (!flag) | |
7209 | { | |
7210 | /* Calculate the need for overload resolution. | |
7211 | * When only one template can match with tiargs, inference is not necessary. | |
7212 | */ | |
7213 | dedtypes.setDim(td->parameters->dim); | |
7214 | dedtypes.zero(); | |
7215 | if (td->semanticRun == PASSinit) | |
7216 | { | |
7217 | if (td->_scope) | |
7218 | { | |
7219 | // Try to fix forward reference. Ungag errors while doing so. | |
7220 | Ungag ungag = td->ungagSpeculative(); | |
7221 | td->semantic(td->_scope); | |
7222 | } | |
7223 | if (td->semanticRun == PASSinit) | |
7224 | { | |
7225 | ti->error("%s forward references template declaration %s", ti->toChars(), td->toChars()); | |
7226 | return 1; | |
7227 | } | |
7228 | } | |
7229 | assert(td->semanticRun != PASSinit); | |
7230 | MATCH m = td->matchWithInstance(sc, ti, &dedtypes, NULL, 0); | |
7231 | if (m <= MATCHnomatch) | |
7232 | return 0; | |
7233 | } | |
7234 | ||
7235 | /* If there is more than one function template which matches, we may | |
7236 | * need type inference (see Bugzilla 4430) | |
7237 | */ | |
7238 | if (++count > 1) | |
7239 | return 1; | |
7240 | ||
7241 | return 0; | |
7242 | } | |
7243 | }; | |
7244 | ParamNeedsInf p; | |
7245 | // context | |
7246 | p.ti = this; | |
7247 | p.sc = sc; | |
7248 | p.flag = flag; | |
7249 | // result | |
7250 | p.count = 0; | |
7251 | ||
7252 | OverloadSet *tovers = tempdecl->isOverloadSet(); | |
7253 | size_t overs_dim = tovers ? tovers->a.dim : 1; | |
7254 | unsigned olderrs = global.errors; | |
7255 | for (size_t oi = 0; oi < overs_dim; oi++) | |
7256 | { | |
7257 | if (overloadApply(tovers ? tovers->a[oi] : tempdecl, &p, &ParamNeedsInf::fp)) | |
7258 | return true; | |
7259 | } | |
7260 | if (olderrs != global.errors) | |
7261 | { | |
7262 | if (!global.gag) | |
7263 | { | |
7264 | errorSupplemental(loc, "while looking for match for %s", toChars()); | |
7265 | semanticRun = PASSsemanticdone; | |
7266 | inst = this; | |
7267 | } | |
7268 | errors = true; | |
7269 | } | |
7270 | //printf("false\n"); | |
7271 | return false; | |
7272 | } | |
7273 | ||
7274 | ||
7275 | /***************************************** | |
7276 | * Determines if a TemplateInstance will need a nested | |
7277 | * generation of the TemplateDeclaration. | |
7278 | * Sets enclosing property if so, and returns != 0; | |
7279 | */ | |
7280 | ||
7281 | bool TemplateInstance::hasNestedArgs(Objects *args, bool isstatic) | |
7282 | { | |
7283 | int nested = 0; | |
7284 | //printf("TemplateInstance::hasNestedArgs('%s')\n", tempdecl->ident->toChars()); | |
7285 | ||
7286 | /* A nested instance happens when an argument references a local | |
7287 | * symbol that is on the stack. | |
7288 | */ | |
7289 | for (size_t i = 0; i < args->dim; i++) | |
7290 | { | |
7291 | RootObject *o = (*args)[i]; | |
7292 | Expression *ea = isExpression(o); | |
7293 | Dsymbol *sa = isDsymbol(o); | |
7294 | Tuple *va = isTuple(o); | |
7295 | if (ea) | |
7296 | { | |
7297 | if (ea->op == TOKvar) | |
7298 | { | |
7299 | sa = ((VarExp *)ea)->var; | |
7300 | goto Lsa; | |
7301 | } | |
7302 | if (ea->op == TOKthis) | |
7303 | { | |
7304 | sa = ((ThisExp *)ea)->var; | |
7305 | goto Lsa; | |
7306 | } | |
7307 | if (ea->op == TOKfunction) | |
7308 | { | |
7309 | if (((FuncExp *)ea)->td) | |
7310 | sa = ((FuncExp *)ea)->td; | |
7311 | else | |
7312 | sa = ((FuncExp *)ea)->fd; | |
7313 | goto Lsa; | |
7314 | } | |
7315 | // Emulate Expression::toMangleBuffer call that had exist in TemplateInstance::genIdent. | |
7316 | if (ea->op != TOKint64 && | |
7317 | ea->op != TOKfloat64 && | |
7318 | ea->op != TOKcomplex80 && | |
7319 | ea->op != TOKnull && | |
7320 | ea->op != TOKstring && | |
7321 | ea->op != TOKarrayliteral && | |
7322 | ea->op != TOKassocarrayliteral && | |
7323 | ea->op != TOKstructliteral) | |
7324 | { | |
7325 | ea->error("expression %s is not a valid template value argument", ea->toChars()); | |
7326 | errors = true; | |
7327 | } | |
7328 | } | |
7329 | else if (sa) | |
7330 | { | |
7331 | Lsa: | |
7332 | sa = sa->toAlias(); | |
7333 | TemplateDeclaration *td = sa->isTemplateDeclaration(); | |
7334 | if (td) | |
7335 | { | |
7336 | TemplateInstance *ti = sa->toParent()->isTemplateInstance(); | |
7337 | if (ti && ti->enclosing) | |
7338 | sa = ti; | |
7339 | } | |
7340 | TemplateInstance *ti = sa->isTemplateInstance(); | |
7341 | Declaration *d = sa->isDeclaration(); | |
7342 | if ((td && td->literal) || | |
7343 | (ti && ti->enclosing) || | |
7344 | (d && !d->isDataseg() && | |
7345 | !(d->storage_class & STCmanifest) && | |
7346 | (!d->isFuncDeclaration() || d->isFuncDeclaration()->isNested()) && | |
7347 | !isTemplateMixin() | |
7348 | )) | |
7349 | { | |
7350 | // if module level template | |
7351 | if (isstatic) | |
7352 | { | |
7353 | Dsymbol *dparent = sa->toParent2(); | |
7354 | if (!enclosing) | |
7355 | enclosing = dparent; | |
7356 | else if (enclosing != dparent) | |
7357 | { | |
7358 | /* Select the more deeply nested of the two. | |
7359 | * Error if one is not nested inside the other. | |
7360 | */ | |
7361 | for (Dsymbol *p = enclosing; p; p = p->parent) | |
7362 | { | |
7363 | if (p == dparent) | |
7364 | goto L1; // enclosing is most nested | |
7365 | } | |
7366 | for (Dsymbol *p = dparent; p; p = p->parent) | |
7367 | { | |
7368 | if (p == enclosing) | |
7369 | { | |
7370 | enclosing = dparent; | |
7371 | goto L1; // dparent is most nested | |
7372 | } | |
7373 | } | |
7374 | error("%s is nested in both %s and %s", | |
7375 | toChars(), enclosing->toChars(), dparent->toChars()); | |
7376 | errors = true; | |
7377 | } | |
7378 | L1: | |
7379 | //printf("\tnested inside %s\n", enclosing->toChars()); | |
7380 | nested |= 1; | |
7381 | } | |
7382 | else | |
7383 | { | |
7384 | error("cannot use local '%s' as parameter to non-global template %s", sa->toChars(), tempdecl->toChars()); | |
7385 | errors = true; | |
7386 | } | |
7387 | } | |
7388 | } | |
7389 | else if (va) | |
7390 | { | |
7391 | nested |= (int)hasNestedArgs(&va->objects, isstatic); | |
7392 | } | |
7393 | } | |
7394 | //printf("-TemplateInstance::hasNestedArgs('%s') = %d\n", tempdecl->ident->toChars(), nested); | |
7395 | return nested != 0; | |
7396 | } | |
7397 | ||
7398 | /***************************************** | |
7399 | * Append 'this' to the specific module members[] | |
7400 | */ | |
7401 | Dsymbols *TemplateInstance::appendToModuleMember() | |
7402 | { | |
7403 | Module *mi = minst; // instantiated -> inserted module | |
7404 | ||
7405 | if (global.params.useUnitTests || | |
7406 | global.params.debuglevel) | |
7407 | { | |
7408 | // Turn all non-root instances to speculative | |
7409 | if (mi && !mi->isRoot()) | |
7410 | mi = NULL; | |
7411 | } | |
7412 | ||
7413 | //printf("%s->appendToModuleMember() enclosing = %s mi = %s\n", | |
7414 | // toPrettyChars(), | |
7415 | // enclosing ? enclosing->toPrettyChars() : NULL, | |
7416 | // mi ? mi->toPrettyChars() : NULL); | |
7417 | if (!mi || mi->isRoot()) | |
7418 | { | |
7419 | /* If the instantiated module is speculative or root, insert to the | |
7420 | * member of a root module. Then: | |
7421 | * - semantic3 pass will get called on the instance members. | |
7422 | * - codegen pass will get a selection chance to do/skip it. | |
7423 | */ | |
7424 | ||
7425 | struct N | |
7426 | { | |
7427 | static Dsymbol *getStrictEnclosing(TemplateInstance *ti) | |
7428 | { | |
7429 | do | |
7430 | { | |
7431 | if (ti->enclosing) | |
7432 | return ti->enclosing; | |
7433 | ti = ti->tempdecl->isInstantiated(); | |
7434 | } | |
7435 | while (ti); | |
7436 | return NULL; | |
7437 | } | |
7438 | }; | |
7439 | Dsymbol *enc = N::getStrictEnclosing(this); | |
7440 | ||
7441 | // insert target is made stable by using the module | |
7442 | // where tempdecl is declared. | |
7443 | mi = (enc ? enc : tempdecl)->getModule(); | |
7444 | if (!mi->isRoot()) | |
7445 | mi = mi->importedFrom; | |
7446 | assert(mi->isRoot()); | |
7447 | } | |
7448 | else | |
7449 | { | |
7450 | /* If the instantiated module is non-root, insert to the member of the | |
7451 | * non-root module. Then: | |
7452 | * - semantic3 pass won't be called on the instance. | |
7453 | * - codegen pass won't reach to the instance. | |
7454 | */ | |
7455 | } | |
7456 | //printf("\t--> mi = %s\n", mi->toPrettyChars()); | |
7457 | ||
7458 | if (memberOf == mi) // already a member | |
7459 | { | |
7460 | return NULL; | |
7461 | } | |
7462 | ||
7463 | Dsymbols *a = mi->members; | |
7464 | a->push(this); | |
7465 | memberOf = mi; | |
7466 | if (mi->semanticRun >= PASSsemantic2done && mi->isRoot()) | |
7467 | Module::addDeferredSemantic2(this); | |
7468 | if (mi->semanticRun >= PASSsemantic3done && mi->isRoot()) | |
7469 | Module::addDeferredSemantic3(this); | |
7470 | return a; | |
7471 | } | |
7472 | ||
7473 | /**************************************** | |
7474 | * This instance needs an identifier for name mangling purposes. | |
7475 | * Create one by taking the template declaration name and adding | |
7476 | * the type signature for it. | |
7477 | */ | |
7478 | ||
7479 | Identifier *TemplateInstance::genIdent(Objects *args) | |
7480 | { | |
7481 | TemplateDeclaration *tempdecl = this->tempdecl->isTemplateDeclaration(); | |
7482 | assert(tempdecl); | |
7483 | ||
7484 | //printf("TemplateInstance::genIdent('%s')\n", tempdecl->ident->toChars()); | |
7485 | OutBuffer buf; | |
7486 | const char *id = tempdecl->ident->toChars(); | |
7487 | if (!members) | |
7488 | { | |
7489 | // Use "__U" for the symbols declared inside template constraint. | |
7490 | buf.printf("__U%llu%s", (ulonglong)strlen(id), id); | |
7491 | } | |
7492 | else | |
7493 | buf.printf("__T%llu%s", (ulonglong)strlen(id), id); | |
7494 | size_t nparams = tempdecl->parameters->dim - (tempdecl->isVariadic() ? 1 : 0); | |
7495 | for (size_t i = 0; i < args->dim; i++) | |
7496 | { | |
7497 | RootObject *o = (*args)[i]; | |
7498 | Type *ta = isType(o); | |
7499 | Expression *ea = isExpression(o); | |
7500 | Dsymbol *sa = isDsymbol(o); | |
7501 | Tuple *va = isTuple(o); | |
7502 | //printf("\to [%d] %p ta %p ea %p sa %p va %p\n", i, o, ta, ea, sa, va); | |
7503 | if (i < nparams && (*tempdecl->parameters)[i]->specialization()) | |
7504 | buf.writeByte('H'); // Bugzilla 6574 | |
7505 | if (ta) | |
7506 | { | |
7507 | buf.writeByte('T'); | |
7508 | if (ta->deco) | |
7509 | buf.writestring(ta->deco); | |
7510 | else | |
7511 | { | |
7512 | assert(global.errors); | |
7513 | } | |
7514 | } | |
7515 | else if (ea) | |
7516 | { | |
7517 | // Don't interpret it yet, it might actually be an alias template parameter. | |
7518 | // Only constfold manifest constants, not const/immutable lvalues, see https://issues.dlang.org/show_bug.cgi?id=17339. | |
7519 | const bool keepLvalue = true; | |
7520 | ea = ea->optimize(WANTvalue, keepLvalue); | |
7521 | if (ea->op == TOKvar) | |
7522 | { | |
7523 | sa = ((VarExp *)ea)->var; | |
7524 | ea = NULL; | |
7525 | goto Lsa; | |
7526 | } | |
7527 | if (ea->op == TOKthis) | |
7528 | { | |
7529 | sa = ((ThisExp *)ea)->var; | |
7530 | ea = NULL; | |
7531 | goto Lsa; | |
7532 | } | |
7533 | if (ea->op == TOKfunction) | |
7534 | { | |
7535 | if (((FuncExp *)ea)->td) | |
7536 | sa = ((FuncExp *)ea)->td; | |
7537 | else | |
7538 | sa = ((FuncExp *)ea)->fd; | |
7539 | ea = NULL; | |
7540 | goto Lsa; | |
7541 | } | |
7542 | buf.writeByte('V'); | |
7543 | if (ea->op == TOKtuple) | |
7544 | { | |
7545 | ea->error("tuple is not a valid template value argument"); | |
7546 | continue; | |
7547 | } | |
7548 | // Now that we know it is not an alias, we MUST obtain a value | |
7549 | unsigned olderr = global.errors; | |
7550 | ea = ea->ctfeInterpret(); | |
7551 | if (ea->op == TOKerror || olderr != global.errors) | |
7552 | continue; | |
7553 | ||
7554 | /* Use deco that matches what it would be for a function parameter | |
7555 | */ | |
7556 | buf.writestring(ea->type->deco); | |
7557 | mangleToBuffer(ea, &buf); | |
7558 | } | |
7559 | else if (sa) | |
7560 | { | |
7561 | Lsa: | |
7562 | buf.writeByte('S'); | |
7563 | sa = sa->toAlias(); | |
7564 | Declaration *d = sa->isDeclaration(); | |
7565 | if (d && (!d->type || !d->type->deco)) | |
7566 | { | |
7567 | error("forward reference of %s %s", d->kind(), d->toChars()); | |
7568 | continue; | |
7569 | } | |
7570 | ||
7571 | OutBuffer bufsa; | |
7572 | mangleToBuffer(sa, &bufsa); | |
7573 | const char *s = bufsa.extractString(); | |
7574 | ||
7575 | /* Bugzilla 3043: if the first character of s is a digit this | |
7576 | * causes ambiguity issues because the digits of the two numbers are adjacent. | |
7577 | * Current demanglers resolve this by trying various places to separate the | |
7578 | * numbers until one gets a successful demangle. | |
7579 | * Unfortunately, fixing this ambiguity will break existing binary | |
7580 | * compatibility and the demanglers, so we'll leave it as is. | |
7581 | */ | |
7582 | buf.printf("%u%s", (unsigned)strlen(s), s); | |
7583 | } | |
7584 | else if (va) | |
7585 | { | |
7586 | assert(i + 1 == args->dim); // must be last one | |
7587 | args = &va->objects; | |
7588 | i = -(size_t)1; | |
7589 | } | |
7590 | else | |
7591 | assert(0); | |
7592 | } | |
7593 | buf.writeByte('Z'); | |
7594 | id = buf.peekString(); | |
7595 | //printf("\tgenIdent = %s\n", id); | |
7596 | return Identifier::idPool(id); | |
7597 | } | |
7598 | ||
7599 | /************************************* | |
7600 | * Lazily generate identifier for template instance. | |
7601 | * This is because 75% of the ident's are never needed. | |
7602 | */ | |
7603 | ||
7604 | Identifier *TemplateInstance::getIdent() | |
7605 | { | |
7606 | if (!ident && inst && !errors) | |
7607 | ident = genIdent(tiargs); // need an identifier for name mangling purposes. | |
7608 | return ident; | |
7609 | } | |
7610 | ||
7611 | /**************************************************** | |
7612 | * Declare parameters of template instance, initialize them with the | |
7613 | * template instance arguments. | |
7614 | */ | |
7615 | ||
7616 | void TemplateInstance::declareParameters(Scope *sc) | |
7617 | { | |
7618 | TemplateDeclaration *tempdecl = this->tempdecl->isTemplateDeclaration(); | |
7619 | assert(tempdecl); | |
7620 | ||
7621 | //printf("TemplateInstance::declareParameters()\n"); | |
7622 | for (size_t i = 0; i < tdtypes.dim; i++) | |
7623 | { | |
7624 | TemplateParameter *tp = (*tempdecl->parameters)[i]; | |
7625 | //RootObject *o = (*tiargs)[i]; | |
7626 | RootObject *o = tdtypes[i]; // initializer for tp | |
7627 | ||
7628 | //printf("\ttdtypes[%d] = %p\n", i, o); | |
7629 | tempdecl->declareParameter(sc, tp, o); | |
7630 | } | |
7631 | } | |
7632 | ||
7633 | void TemplateInstance::semantic2(Scope *sc) | |
7634 | { | |
7635 | if (semanticRun >= PASSsemantic2) | |
7636 | return; | |
7637 | semanticRun = PASSsemantic2; | |
7638 | if (!errors && members) | |
7639 | { | |
7640 | TemplateDeclaration *tempdecl = this->tempdecl->isTemplateDeclaration(); | |
7641 | assert(tempdecl); | |
7642 | ||
7643 | sc = tempdecl->_scope; | |
7644 | assert(sc); | |
7645 | sc = sc->push(argsym); | |
7646 | sc = sc->push(this); | |
7647 | sc->tinst = this; | |
7648 | sc->minst = minst; | |
7649 | ||
7650 | int needGagging = (gagged && !global.gag); | |
7651 | unsigned int olderrors = global.errors; | |
7652 | int oldGaggedErrors = -1; // dead-store to prevent spurious warning | |
7653 | if (needGagging) | |
7654 | oldGaggedErrors = global.startGagging(); | |
7655 | ||
7656 | for (size_t i = 0; i < members->dim; i++) | |
7657 | { | |
7658 | Dsymbol *s = (*members)[i]; | |
7659 | s->semantic2(sc); | |
7660 | if (gagged && global.errors != olderrors) | |
7661 | break; | |
7662 | } | |
7663 | ||
7664 | if (global.errors != olderrors) | |
7665 | { | |
7666 | if (!errors) | |
7667 | { | |
7668 | if (!tempdecl->literal) | |
7669 | error(loc, "error instantiating"); | |
7670 | if (tinst) | |
7671 | tinst->printInstantiationTrace(); | |
7672 | } | |
7673 | errors = true; | |
7674 | } | |
7675 | if (needGagging) | |
7676 | global.endGagging(oldGaggedErrors); | |
7677 | ||
7678 | sc = sc->pop(); | |
7679 | sc->pop(); | |
7680 | } | |
7681 | } | |
7682 | ||
7683 | void TemplateInstance::semantic3(Scope *sc) | |
7684 | { | |
7685 | //if (toChars()[0] == 'D') *(char*)0=0; | |
7686 | if (semanticRun >= PASSsemantic3) | |
7687 | return; | |
7688 | semanticRun = PASSsemantic3; | |
7689 | if (!errors && members) | |
7690 | { | |
7691 | TemplateDeclaration *tempdecl = this->tempdecl->isTemplateDeclaration(); | |
7692 | assert(tempdecl); | |
7693 | ||
7694 | sc = tempdecl->_scope; | |
7695 | sc = sc->push(argsym); | |
7696 | sc = sc->push(this); | |
7697 | sc->tinst = this; | |
7698 | sc->minst = minst; | |
7699 | ||
7700 | int needGagging = (gagged && !global.gag); | |
7701 | unsigned int olderrors = global.errors; | |
7702 | int oldGaggedErrors = -1; // dead-store to prevent spurious warning | |
7703 | /* If this is a gagged instantiation, gag errors. | |
7704 | * Future optimisation: If the results are actually needed, errors | |
7705 | * would already be gagged, so we don't really need to run semantic | |
7706 | * on the members. | |
7707 | */ | |
7708 | if (needGagging) | |
7709 | oldGaggedErrors = global.startGagging(); | |
7710 | ||
7711 | for (size_t i = 0; i < members->dim; i++) | |
7712 | { | |
7713 | Dsymbol *s = (*members)[i]; | |
7714 | s->semantic3(sc); | |
7715 | if (gagged && global.errors != olderrors) | |
7716 | break; | |
7717 | } | |
7718 | ||
7719 | if (global.errors != olderrors) | |
7720 | { | |
7721 | if (!errors) | |
7722 | { | |
7723 | if (!tempdecl->literal) | |
7724 | error(loc, "error instantiating"); | |
7725 | if (tinst) | |
7726 | tinst->printInstantiationTrace(); | |
7727 | } | |
7728 | errors = true; | |
7729 | } | |
7730 | if (needGagging) | |
7731 | global.endGagging(oldGaggedErrors); | |
7732 | ||
7733 | sc = sc->pop(); | |
7734 | sc->pop(); | |
7735 | } | |
7736 | } | |
7737 | ||
7738 | /************************************** | |
7739 | * Given an error instantiating the TemplateInstance, | |
7740 | * give the nested TemplateInstance instantiations that got | |
7741 | * us here. Those are a list threaded into the nested scopes. | |
7742 | */ | |
7743 | void TemplateInstance::printInstantiationTrace() | |
7744 | { | |
7745 | if (global.gag) | |
7746 | return; | |
7747 | ||
7748 | const unsigned max_shown = 6; | |
7749 | const char format[] = "instantiated from here: %s"; | |
7750 | ||
7751 | // determine instantiation depth and number of recursive instantiations | |
7752 | unsigned n_instantiations = 1; | |
7753 | unsigned n_totalrecursions = 0; | |
7754 | for (TemplateInstance *cur = this; cur; cur = cur->tinst) | |
7755 | { | |
7756 | ++n_instantiations; | |
7757 | // If two instantiations use the same declaration, they are recursive. | |
7758 | // (this works even if they are instantiated from different places in the | |
7759 | // same template). | |
7760 | // In principle, we could also check for multiple-template recursion, but it's | |
7761 | // probably not worthwhile. | |
7762 | if (cur->tinst && cur->tempdecl && cur->tinst->tempdecl | |
7763 | && cur->tempdecl->loc.equals(cur->tinst->tempdecl->loc)) | |
7764 | ++n_totalrecursions; | |
7765 | } | |
7766 | ||
7767 | // show full trace only if it's short or verbose is on | |
7768 | if (n_instantiations <= max_shown || global.params.verbose) | |
7769 | { | |
7770 | for (TemplateInstance *cur = this; cur; cur = cur->tinst) | |
7771 | { | |
7772 | cur->errors = true; | |
7773 | errorSupplemental(cur->loc, format, cur->toChars()); | |
7774 | } | |
7775 | } | |
7776 | else if (n_instantiations - n_totalrecursions <= max_shown) | |
7777 | { | |
7778 | // By collapsing recursive instantiations into a single line, | |
7779 | // we can stay under the limit. | |
7780 | int recursionDepth=0; | |
7781 | for (TemplateInstance *cur = this; cur; cur = cur->tinst) | |
7782 | { | |
7783 | cur->errors = true; | |
7784 | if (cur->tinst && cur->tempdecl && cur->tinst->tempdecl | |
7785 | && cur->tempdecl->loc.equals(cur->tinst->tempdecl->loc)) | |
7786 | { | |
7787 | ++recursionDepth; | |
7788 | } | |
7789 | else | |
7790 | { | |
7791 | if (recursionDepth) | |
7792 | errorSupplemental(cur->loc, "%d recursive instantiations from here: %s", recursionDepth+2, cur->toChars()); | |
7793 | else | |
7794 | errorSupplemental(cur->loc, format, cur->toChars()); | |
7795 | recursionDepth = 0; | |
7796 | } | |
7797 | } | |
7798 | } | |
7799 | else | |
7800 | { | |
7801 | // Even after collapsing the recursions, the depth is too deep. | |
7802 | // Just display the first few and last few instantiations. | |
7803 | unsigned i = 0; | |
7804 | for (TemplateInstance *cur = this; cur; cur = cur->tinst) | |
7805 | { | |
7806 | cur->errors = true; | |
7807 | ||
7808 | if (i == max_shown / 2) | |
7809 | errorSupplemental(cur->loc, "... (%d instantiations, -v to show) ...", n_instantiations - max_shown); | |
7810 | ||
7811 | if (i < max_shown / 2 || | |
7812 | i >= n_instantiations - max_shown + max_shown / 2) | |
7813 | errorSupplemental(cur->loc, format, cur->toChars()); | |
7814 | ++i; | |
7815 | } | |
7816 | } | |
7817 | } | |
7818 | ||
7819 | Dsymbol *TemplateInstance::toAlias() | |
7820 | { | |
7821 | if (!inst) | |
7822 | { | |
7823 | // Maybe we can resolve it | |
7824 | if (_scope) | |
7825 | { | |
7826 | semantic(_scope); | |
7827 | } | |
7828 | if (!inst) | |
7829 | { | |
7830 | error("cannot resolve forward reference"); | |
7831 | errors = true; | |
7832 | return this; | |
7833 | } | |
7834 | } | |
7835 | ||
7836 | if (inst != this) | |
7837 | return inst->toAlias(); | |
7838 | ||
7839 | if (aliasdecl) | |
7840 | { | |
7841 | return aliasdecl->toAlias(); | |
7842 | } | |
7843 | ||
7844 | return inst; | |
7845 | } | |
7846 | ||
7847 | const char *TemplateInstance::kind() | |
7848 | { | |
7849 | return "template instance"; | |
7850 | } | |
7851 | ||
7852 | bool TemplateInstance::oneMember(Dsymbol **ps, Identifier *) | |
7853 | { | |
7854 | *ps = NULL; | |
7855 | return true; | |
7856 | } | |
7857 | ||
7858 | const char *TemplateInstance::toChars() | |
7859 | { | |
7860 | OutBuffer buf; | |
7861 | toCBufferInstance(this, &buf); | |
7862 | return buf.extractString(); | |
7863 | } | |
7864 | ||
7865 | const char *TemplateInstance::toPrettyCharsHelper() | |
7866 | { | |
7867 | OutBuffer buf; | |
7868 | toCBufferInstance(this, &buf, true); | |
7869 | return buf.extractString(); | |
7870 | } | |
7871 | ||
7872 | /************************************* | |
7873 | * Compare proposed template instantiation with existing template instantiation. | |
7874 | * Note that this is not commutative because of the auto ref check. | |
7875 | * Params: | |
7876 | * this = proposed template instantiation | |
7877 | * o = existing template instantiation | |
7878 | * Returns: | |
7879 | * 0 for match, 1 for no match | |
7880 | */ | |
7881 | int TemplateInstance::compare(RootObject *o) | |
7882 | { | |
7883 | TemplateInstance *ti = (TemplateInstance *)o; | |
7884 | ||
7885 | //printf("this = %p, ti = %p\n", this, ti); | |
7886 | assert(tdtypes.dim == ti->tdtypes.dim); | |
7887 | ||
7888 | // Nesting must match | |
7889 | if (enclosing != ti->enclosing) | |
7890 | { | |
7891 | //printf("test2 enclosing %s ti->enclosing %s\n", enclosing ? enclosing->toChars() : "", ti->enclosing ? ti->enclosing->toChars() : ""); | |
7892 | goto Lnotequals; | |
7893 | } | |
7894 | //printf("parent = %s, ti->parent = %s\n", parent->toPrettyChars(), ti->parent->toPrettyChars()); | |
7895 | ||
7896 | if (!arrayObjectMatch(&tdtypes, &ti->tdtypes)) | |
7897 | goto Lnotequals; | |
7898 | ||
7899 | /* Template functions may have different instantiations based on | |
7900 | * "auto ref" parameters. | |
7901 | */ | |
7902 | if (FuncDeclaration *fd = ti->toAlias()->isFuncDeclaration()) | |
7903 | { | |
7904 | if (!fd->errors) | |
7905 | { | |
7906 | Parameters *fparameters = fd->getParameters(NULL); | |
7907 | size_t nfparams = Parameter::dim(fparameters); // Num function parameters | |
7908 | for (size_t j = 0; j < nfparams; j++) | |
7909 | { | |
7910 | Parameter *fparam = Parameter::getNth(fparameters, j); | |
7911 | if (fparam->storageClass & STCautoref) // if "auto ref" | |
7912 | { | |
7913 | if (!fargs) | |
7914 | goto Lnotequals; | |
7915 | if (fargs->dim <= j) | |
7916 | break; | |
7917 | Expression *farg = (*fargs)[j]; | |
7918 | if (farg->isLvalue()) | |
7919 | { | |
7920 | if (!(fparam->storageClass & STCref)) | |
7921 | goto Lnotequals; // auto ref's don't match | |
7922 | } | |
7923 | else | |
7924 | { | |
7925 | if (fparam->storageClass & STCref) | |
7926 | goto Lnotequals; // auto ref's don't match | |
7927 | } | |
7928 | } | |
7929 | } | |
7930 | } | |
7931 | } | |
7932 | return 0; | |
7933 | ||
7934 | Lnotequals: | |
7935 | return 1; | |
7936 | } | |
7937 | ||
7938 | hash_t TemplateInstance::toHash() | |
7939 | { | |
7940 | if (!hash) | |
7941 | { | |
7942 | hash = (size_t)(void *)enclosing; | |
7943 | hash += arrayObjectHash(&tdtypes); | |
7944 | hash += hash == 0; | |
7945 | } | |
7946 | return hash; | |
7947 | } | |
7948 | ||
7949 | /************************************** | |
7950 | * IsExpression can evaluate the specified type speculatively, and even if | |
7951 | * it instantiates any symbols, they are normally unnecessary for the | |
7952 | * final executable. | |
7953 | * However, if those symbols leak to the actual code, compiler should remark | |
7954 | * them as non-speculative to generate their code and link to the final executable. | |
7955 | */ | |
7956 | void unSpeculative(Scope *sc, RootObject *o) | |
7957 | { | |
7958 | if (!o) | |
7959 | return; | |
7960 | ||
7961 | if (Tuple *tup = isTuple(o)) | |
7962 | { | |
7963 | for (size_t i = 0; i < tup->objects.dim; i++) | |
7964 | { | |
7965 | unSpeculative(sc, tup->objects[i]); | |
7966 | } | |
7967 | return; | |
7968 | } | |
7969 | ||
7970 | Dsymbol *s = getDsymbol(o); | |
7971 | if (!s) | |
7972 | return; | |
7973 | ||
7974 | if (Declaration *d = s->isDeclaration()) | |
7975 | { | |
7976 | if (VarDeclaration *vd = d->isVarDeclaration()) | |
7977 | o = vd->type; | |
7978 | else if (AliasDeclaration *ad = d->isAliasDeclaration()) | |
7979 | { | |
7980 | o = ad->getType(); | |
7981 | if (!o) | |
7982 | o = ad->toAlias(); | |
7983 | } | |
7984 | else | |
7985 | o = d->toAlias(); | |
7986 | ||
7987 | s = getDsymbol(o); | |
7988 | if (!s) | |
7989 | return; | |
7990 | } | |
7991 | ||
7992 | if (TemplateInstance *ti = s->isTemplateInstance()) | |
7993 | { | |
7994 | // If the instance is already non-speculative, | |
7995 | // or it is leaked to the speculative scope. | |
7996 | if (ti->minst != NULL || sc->minst == NULL) | |
7997 | return; | |
7998 | ||
7999 | // Remark as non-speculative instance. | |
8000 | ti->minst = sc->minst; | |
8001 | if (!ti->tinst) | |
8002 | ti->tinst = sc->tinst; | |
8003 | ||
8004 | unSpeculative(sc, ti->tempdecl); | |
8005 | } | |
8006 | ||
8007 | if (TemplateInstance *ti = s->isInstantiated()) | |
8008 | unSpeculative(sc, ti); | |
8009 | } | |
8010 | ||
8011 | /*********************************************** | |
8012 | * Returns true if this is not instantiated in non-root module, and | |
8013 | * is a part of non-speculative instantiatiation. | |
8014 | * | |
8015 | * Note: minst does not stabilize until semantic analysis is completed, | |
8016 | * so don't call this function during semantic analysis to return precise result. | |
8017 | */ | |
8018 | bool TemplateInstance::needsCodegen() | |
8019 | { | |
8020 | // Now -allInst is just for the backward compatibility. | |
8021 | if (global.params.allInst) | |
8022 | { | |
8023 | //printf("%s minst = %s, enclosing (%s)->isNonRoot = %d\n", | |
8024 | // toPrettyChars(), minst ? minst->toChars() : NULL, | |
8025 | // enclosing ? enclosing->toPrettyChars() : NULL, enclosing && enclosing->inNonRoot()); | |
8026 | if (enclosing) | |
8027 | { | |
8028 | // Bugzilla 14588: If the captured context is not a function | |
8029 | // (e.g. class), the instance layout determination is guaranteed, | |
8030 | // because the semantic/semantic2 pass will be executed | |
8031 | // even for non-root instances. | |
8032 | if (!enclosing->isFuncDeclaration()) | |
8033 | return true; | |
8034 | ||
8035 | // Bugzilla 14834: If the captured context is a function, | |
8036 | // this excessive instantiation may cause ODR violation, because | |
8037 | // -allInst and others doesn't guarantee the semantic3 execution | |
8038 | // for that function. | |
8039 | ||
8040 | // If the enclosing is also an instantiated function, | |
8041 | // we have to rely on the ancestor's needsCodegen() result. | |
8042 | if (TemplateInstance *ti = enclosing->isInstantiated()) | |
8043 | return ti->needsCodegen(); | |
8044 | ||
8045 | // Bugzilla 13415: If and only if the enclosing scope needs codegen, | |
8046 | // this nested templates would also need code generation. | |
8047 | return !enclosing->inNonRoot(); | |
8048 | } | |
8049 | return true; | |
8050 | } | |
8051 | ||
8052 | if (!minst) | |
8053 | { | |
8054 | // If this is a speculative instantiation, | |
8055 | // 1. do codegen if ancestors really needs codegen. | |
8056 | // 2. become non-speculative if siblings are not speculative | |
8057 | ||
8058 | TemplateInstance *tnext = this->tnext; | |
8059 | TemplateInstance *tinst = this->tinst; | |
8060 | // At first, disconnect chain first to prevent infinite recursion. | |
8061 | this->tnext = NULL; | |
8062 | this->tinst = NULL; | |
8063 | ||
8064 | // Determine necessity of tinst before tnext. | |
8065 | if (tinst && tinst->needsCodegen()) | |
8066 | { | |
8067 | minst = tinst->minst; // cache result | |
8068 | assert(minst); | |
8069 | assert(minst->isRoot() || minst->rootImports()); | |
8070 | return true; | |
8071 | } | |
8072 | if (tnext && (tnext->needsCodegen() || tnext->minst)) | |
8073 | { | |
8074 | minst = tnext->minst; // cache result | |
8075 | assert(minst); | |
8076 | return minst->isRoot() || minst->rootImports(); | |
8077 | } | |
8078 | ||
8079 | // Elide codegen because this is really speculative. | |
8080 | return false; | |
8081 | } | |
8082 | ||
8083 | /* Even when this is reached to the codegen pass, | |
8084 | * a non-root nested template should not generate code, | |
8085 | * due to avoid ODR violation. | |
8086 | */ | |
8087 | if (enclosing && enclosing->inNonRoot()) | |
8088 | { | |
8089 | if (tinst) | |
8090 | { | |
8091 | bool r = tinst->needsCodegen(); | |
8092 | minst = tinst->minst; // cache result | |
8093 | return r; | |
8094 | } | |
8095 | if (tnext) | |
8096 | { | |
8097 | bool r = tnext->needsCodegen(); | |
8098 | minst = tnext->minst; // cache result | |
8099 | return r; | |
8100 | } | |
8101 | return false; | |
8102 | } | |
8103 | ||
8104 | /* The issue is that if the importee is compiled with a different -debug | |
8105 | * setting than the importer, the importer may believe it exists | |
8106 | * in the compiled importee when it does not, when the instantiation | |
8107 | * is behind a conditional debug declaration. | |
8108 | */ | |
8109 | // workaround for Bugzilla 11239 | |
8110 | if (global.params.useUnitTests || | |
8111 | global.params.debuglevel) | |
8112 | { | |
8113 | // Prefer instantiations from root modules, to maximize link-ability. | |
8114 | if (minst->isRoot()) | |
8115 | return true; | |
8116 | ||
8117 | TemplateInstance *tnext = this->tnext; | |
8118 | TemplateInstance *tinst = this->tinst; | |
8119 | this->tnext = NULL; | |
8120 | this->tinst = NULL; | |
8121 | ||
8122 | if (tinst && tinst->needsCodegen()) | |
8123 | { | |
8124 | minst = tinst->minst; // cache result | |
8125 | assert(minst); | |
8126 | assert(minst->isRoot() || minst->rootImports()); | |
8127 | return true; | |
8128 | } | |
8129 | if (tnext && tnext->needsCodegen()) | |
8130 | { | |
8131 | minst = tnext->minst; // cache result | |
8132 | assert(minst); | |
8133 | assert(minst->isRoot() || minst->rootImports()); | |
8134 | return true; | |
8135 | } | |
8136 | ||
8137 | // Bugzilla 2500 case | |
8138 | if (minst->rootImports()) | |
8139 | return true; | |
8140 | ||
8141 | // Elide codegen because this is not included in root instances. | |
8142 | return false; | |
8143 | } | |
8144 | else | |
8145 | { | |
8146 | // Prefer instantiations from non-root module, to minimize object code size. | |
8147 | ||
8148 | /* If a TemplateInstance is ever instantiated by non-root modules, | |
8149 | * we do not have to generate code for it, | |
8150 | * because it will be generated when the non-root module is compiled. | |
8151 | * | |
8152 | * But, if the non-root 'minst' imports any root modules, it might still need codegen. | |
8153 | * | |
8154 | * The problem is if A imports B, and B imports A, and both A | |
8155 | * and B instantiate the same template, does the compilation of A | |
8156 | * or the compilation of B do the actual instantiation? | |
8157 | * | |
8158 | * See Bugzilla 2500. | |
8159 | */ | |
8160 | if (!minst->isRoot() && !minst->rootImports()) | |
8161 | return false; | |
8162 | ||
8163 | TemplateInstance *tnext = this->tnext; | |
8164 | this->tnext = NULL; | |
8165 | ||
8166 | if (tnext && !tnext->needsCodegen() && tnext->minst) | |
8167 | { | |
8168 | minst = tnext->minst; // cache result | |
8169 | assert(!minst->isRoot()); | |
8170 | return false; | |
8171 | } | |
8172 | ||
8173 | // Do codegen because this is not included in non-root instances. | |
8174 | return true; | |
8175 | } | |
8176 | } | |
8177 | ||
8178 | /* ======================== TemplateMixin ================================ */ | |
8179 | ||
8180 | TemplateMixin::TemplateMixin(Loc loc, Identifier *ident, TypeQualified *tqual, Objects *tiargs) | |
8181 | : TemplateInstance(loc, tqual->idents.dim ? (Identifier *)tqual->idents[tqual->idents.dim - 1] | |
8182 | : ((TypeIdentifier *)tqual)->ident) | |
8183 | { | |
8184 | //printf("TemplateMixin(ident = '%s')\n", ident ? ident->toChars() : ""); | |
8185 | this->ident = ident; | |
8186 | this->tqual = tqual; | |
8187 | this->tiargs = tiargs ? tiargs : new Objects(); | |
8188 | } | |
8189 | ||
8190 | Dsymbol *TemplateMixin::syntaxCopy(Dsymbol *) | |
8191 | { | |
8192 | TemplateMixin *tm = new TemplateMixin(loc, ident, | |
8193 | (TypeQualified *)tqual->syntaxCopy(), tiargs); | |
8194 | return TemplateInstance::syntaxCopy(tm); | |
8195 | } | |
8196 | ||
8197 | bool TemplateMixin::findTempDecl(Scope *sc) | |
8198 | { | |
8199 | // Follow qualifications to find the TemplateDeclaration | |
8200 | if (!tempdecl) | |
8201 | { | |
8202 | Expression *e; | |
8203 | Type *t; | |
8204 | Dsymbol *s; | |
8205 | tqual->resolve(loc, sc, &e, &t, &s); | |
8206 | if (!s) | |
8207 | { | |
8208 | error("is not defined"); | |
8209 | return false; | |
8210 | } | |
8211 | s = s->toAlias(); | |
8212 | tempdecl = s->isTemplateDeclaration(); | |
8213 | OverloadSet *os = s->isOverloadSet(); | |
8214 | ||
8215 | /* If an OverloadSet, look for a unique member that is a template declaration | |
8216 | */ | |
8217 | if (os) | |
8218 | { | |
8219 | Dsymbol *ds = NULL; | |
8220 | for (size_t i = 0; i < os->a.dim; i++) | |
8221 | { | |
8222 | Dsymbol *s2 = os->a[i]->isTemplateDeclaration(); | |
8223 | if (s2) | |
8224 | { | |
8225 | if (ds) | |
8226 | { | |
8227 | tempdecl = os; | |
8228 | break; | |
8229 | } | |
8230 | ds = s2; | |
8231 | } | |
8232 | } | |
8233 | } | |
8234 | if (!tempdecl) | |
8235 | { | |
8236 | error("%s isn't a template", s->toChars()); | |
8237 | return false; | |
8238 | } | |
8239 | } | |
8240 | assert(tempdecl); | |
8241 | ||
8242 | struct ParamFwdResTm | |
8243 | { | |
8244 | static int fp(void *param, Dsymbol *s) | |
8245 | { | |
8246 | TemplateDeclaration *td = s->isTemplateDeclaration(); | |
8247 | if (!td) | |
8248 | return 0; | |
8249 | ||
8250 | TemplateMixin *tm = (TemplateMixin *)param; | |
8251 | if (td->semanticRun == PASSinit) | |
8252 | { | |
8253 | if (td->_scope) | |
8254 | td->semantic(td->_scope); | |
8255 | else | |
8256 | { | |
8257 | tm->semanticRun = PASSinit; | |
8258 | return 1; | |
8259 | } | |
8260 | } | |
8261 | return 0; | |
8262 | } | |
8263 | }; | |
8264 | // Look for forward references | |
8265 | OverloadSet *tovers = tempdecl->isOverloadSet(); | |
8266 | size_t overs_dim = tovers ? tovers->a.dim : 1; | |
8267 | for (size_t oi = 0; oi < overs_dim; oi++) | |
8268 | { | |
8269 | if (overloadApply(tovers ? tovers->a[oi] : tempdecl, (void *)this, &ParamFwdResTm::fp)) | |
8270 | return false; | |
8271 | } | |
8272 | return true; | |
8273 | } | |
8274 | ||
8275 | void TemplateMixin::semantic(Scope *sc) | |
8276 | { | |
8277 | if (semanticRun != PASSinit) | |
8278 | { | |
8279 | // When a class/struct contains mixin members, and is done over | |
8280 | // because of forward references, never reach here so semanticRun | |
8281 | // has been reset to PASSinit. | |
8282 | return; | |
8283 | } | |
8284 | semanticRun = PASSsemantic; | |
8285 | ||
8286 | Scope *scx = NULL; | |
8287 | if (_scope) | |
8288 | { | |
8289 | sc = _scope; | |
8290 | scx = _scope; // save so we don't make redundant copies | |
8291 | _scope = NULL; | |
8292 | } | |
8293 | ||
8294 | /* Run semantic on each argument, place results in tiargs[], | |
8295 | * then find best match template with tiargs | |
8296 | */ | |
8297 | if (!findTempDecl(sc) || | |
8298 | !semanticTiargs(sc) || | |
8299 | !findBestMatch(sc, NULL)) | |
8300 | { | |
8301 | if (semanticRun == PASSinit) // forward reference had occured | |
8302 | { | |
8303 | //printf("forward reference - deferring\n"); | |
8304 | _scope = scx ? scx : sc->copy(); | |
8305 | _scope->setNoFree(); | |
8306 | _scope->_module->addDeferredSemantic(this); | |
8307 | return; | |
8308 | } | |
8309 | ||
8310 | inst = this; | |
8311 | errors = true; | |
8312 | return; // error recovery | |
8313 | } | |
8314 | TemplateDeclaration *tempdecl = this->tempdecl->isTemplateDeclaration(); | |
8315 | assert(tempdecl); | |
8316 | ||
8317 | if (!ident) | |
8318 | { | |
8319 | /* Assign scope local unique identifier, as same as lambdas. | |
8320 | */ | |
8321 | const char *s = "__mixin"; | |
8322 | ||
8323 | DsymbolTable *symtab; | |
8324 | if (FuncDeclaration *func = sc->parent->isFuncDeclaration()) | |
8325 | { | |
8326 | symtab = func->localsymtab; | |
8327 | if (symtab) | |
8328 | { | |
8329 | // Inside template constraint, symtab is not set yet. | |
8330 | goto L1; | |
8331 | } | |
8332 | } | |
8333 | else | |
8334 | { | |
8335 | symtab = sc->parent->isScopeDsymbol()->symtab; | |
8336 | L1: | |
8337 | assert(symtab); | |
8338 | int num = (int)dmd_aaLen(symtab->tab) + 1; | |
8339 | ident = Identifier::generateId(s, num); | |
8340 | symtab->insert(this); | |
8341 | } | |
8342 | } | |
8343 | ||
8344 | inst = this; | |
8345 | parent = sc->parent; | |
8346 | ||
8347 | /* Detect recursive mixin instantiations. | |
8348 | */ | |
8349 | for (Dsymbol *s = parent; s; s = s->parent) | |
8350 | { | |
8351 | //printf("\ts = '%s'\n", s->toChars()); | |
8352 | TemplateMixin *tm = s->isTemplateMixin(); | |
8353 | if (!tm || tempdecl != tm->tempdecl) | |
8354 | continue; | |
8355 | ||
8356 | /* Different argument list lengths happen with variadic args | |
8357 | */ | |
8358 | if (tiargs->dim != tm->tiargs->dim) | |
8359 | continue; | |
8360 | ||
8361 | for (size_t i = 0; i < tiargs->dim; i++) | |
8362 | { | |
8363 | RootObject *o = (*tiargs)[i]; | |
8364 | Type *ta = isType(o); | |
8365 | Expression *ea = isExpression(o); | |
8366 | Dsymbol *sa = isDsymbol(o); | |
8367 | RootObject *tmo = (*tm->tiargs)[i]; | |
8368 | if (ta) | |
8369 | { | |
8370 | Type *tmta = isType(tmo); | |
8371 | if (!tmta) | |
8372 | goto Lcontinue; | |
8373 | if (!ta->equals(tmta)) | |
8374 | goto Lcontinue; | |
8375 | } | |
8376 | else if (ea) | |
8377 | { | |
8378 | Expression *tme = isExpression(tmo); | |
8379 | if (!tme || !ea->equals(tme)) | |
8380 | goto Lcontinue; | |
8381 | } | |
8382 | else if (sa) | |
8383 | { | |
8384 | Dsymbol *tmsa = isDsymbol(tmo); | |
8385 | if (sa != tmsa) | |
8386 | goto Lcontinue; | |
8387 | } | |
8388 | else | |
8389 | assert(0); | |
8390 | } | |
8391 | error("recursive mixin instantiation"); | |
8392 | return; | |
8393 | ||
8394 | Lcontinue: | |
8395 | continue; | |
8396 | } | |
8397 | ||
8398 | // Copy the syntax trees from the TemplateDeclaration | |
8399 | members = Dsymbol::arraySyntaxCopy(tempdecl->members); | |
8400 | if (!members) | |
8401 | return; | |
8402 | ||
8403 | symtab = new DsymbolTable(); | |
8404 | ||
8405 | for (Scope *sce = sc; 1; sce = sce->enclosing) | |
8406 | { | |
8407 | ScopeDsymbol *sds = (ScopeDsymbol *)sce->scopesym; | |
8408 | if (sds) | |
8409 | { | |
8410 | sds->importScope(this, Prot(PROTpublic)); | |
8411 | break; | |
8412 | } | |
8413 | } | |
8414 | ||
8415 | Scope *scy = sc->push(this); | |
8416 | scy->parent = this; | |
8417 | ||
8418 | argsym = new ScopeDsymbol(); | |
8419 | argsym->parent = scy->parent; | |
8420 | Scope *argscope = scy->push(argsym); | |
8421 | ||
8422 | unsigned errorsave = global.errors; | |
8423 | ||
8424 | // Declare each template parameter as an alias for the argument type | |
8425 | declareParameters(argscope); | |
8426 | ||
8427 | // Add members to enclosing scope, as well as this scope | |
8428 | for (size_t i = 0; i < members->dim; i++) | |
8429 | { | |
8430 | Dsymbol *s = (*members)[i]; | |
8431 | s->addMember(argscope, this); | |
8432 | //printf("sc->parent = %p, sc->scopesym = %p\n", sc->parent, sc->scopesym); | |
8433 | //printf("s->parent = %s\n", s->parent->toChars()); | |
8434 | } | |
8435 | ||
8436 | // Do semantic() analysis on template instance members | |
8437 | Scope *sc2 = argscope->push(this); | |
8438 | //size_t deferred_dim = Module::deferred.dim; | |
8439 | ||
8440 | static int nest; | |
8441 | //printf("%d\n", nest); | |
8442 | if (++nest > 500) | |
8443 | { | |
8444 | global.gag = 0; // ensure error message gets printed | |
8445 | error("recursive expansion"); | |
8446 | fatal(); | |
8447 | } | |
8448 | ||
8449 | for (size_t i = 0; i < members->dim; i++) | |
8450 | { | |
8451 | Dsymbol *s = (*members)[i]; | |
8452 | s->setScope(sc2); | |
8453 | } | |
8454 | ||
8455 | for (size_t i = 0; i < members->dim; i++) | |
8456 | { | |
8457 | Dsymbol *s = (*members)[i]; | |
8458 | s->importAll(sc2); | |
8459 | } | |
8460 | ||
8461 | for (size_t i = 0; i < members->dim; i++) | |
8462 | { | |
8463 | Dsymbol *s = (*members)[i]; | |
8464 | s->semantic(sc2); | |
8465 | } | |
8466 | ||
8467 | nest--; | |
8468 | ||
8469 | /* In DeclDefs scope, TemplateMixin does not have to handle deferred symbols. | |
8470 | * Because the members would already call Module::addDeferredSemantic() for themselves. | |
8471 | * See Struct, Class, Interface, and EnumDeclaration::semantic(). | |
8472 | */ | |
8473 | //if (!sc->func && Module::deferred.dim > deferred_dim) {} | |
8474 | ||
8475 | AggregateDeclaration *ad = toParent()->isAggregateDeclaration(); | |
8476 | if (sc->func && !ad) | |
8477 | { | |
8478 | semantic2(sc2); | |
8479 | semantic3(sc2); | |
8480 | } | |
8481 | ||
8482 | // Give additional context info if error occurred during instantiation | |
8483 | if (global.errors != errorsave) | |
8484 | { | |
8485 | error("error instantiating"); | |
8486 | errors = true; | |
8487 | } | |
8488 | ||
8489 | sc2->pop(); | |
8490 | argscope->pop(); | |
8491 | scy->pop(); | |
8492 | } | |
8493 | ||
8494 | void TemplateMixin::semantic2(Scope *sc) | |
8495 | { | |
8496 | if (semanticRun >= PASSsemantic2) | |
8497 | return; | |
8498 | semanticRun = PASSsemantic2; | |
8499 | if (members) | |
8500 | { | |
8501 | assert(sc); | |
8502 | sc = sc->push(argsym); | |
8503 | sc = sc->push(this); | |
8504 | for (size_t i = 0; i < members->dim; i++) | |
8505 | { | |
8506 | Dsymbol *s = (*members)[i]; | |
8507 | s->semantic2(sc); | |
8508 | } | |
8509 | sc = sc->pop(); | |
8510 | sc->pop(); | |
8511 | } | |
8512 | } | |
8513 | ||
8514 | void TemplateMixin::semantic3(Scope *sc) | |
8515 | { | |
8516 | if (semanticRun >= PASSsemantic3) | |
8517 | return; | |
8518 | semanticRun = PASSsemantic3; | |
8519 | if (members) | |
8520 | { | |
8521 | sc = sc->push(argsym); | |
8522 | sc = sc->push(this); | |
8523 | for (size_t i = 0; i < members->dim; i++) | |
8524 | { | |
8525 | Dsymbol *s = (*members)[i]; | |
8526 | s->semantic3(sc); | |
8527 | } | |
8528 | sc = sc->pop(); | |
8529 | sc->pop(); | |
8530 | } | |
8531 | } | |
8532 | ||
8533 | const char *TemplateMixin::kind() | |
8534 | { | |
8535 | return "mixin"; | |
8536 | } | |
8537 | ||
8538 | bool TemplateMixin::oneMember(Dsymbol **ps, Identifier *ident) | |
8539 | { | |
8540 | return Dsymbol::oneMember(ps, ident); | |
8541 | } | |
8542 | ||
8543 | int TemplateMixin::apply(Dsymbol_apply_ft_t fp, void *param) | |
8544 | { | |
8545 | if (_scope) // if fwd reference | |
8546 | semantic(NULL); // try to resolve it | |
8547 | if (members) | |
8548 | { | |
8549 | for (size_t i = 0; i < members->dim; i++) | |
8550 | { | |
8551 | Dsymbol *s = (*members)[i]; | |
8552 | if (s) | |
8553 | { | |
8554 | if (s->apply(fp, param)) | |
8555 | return 1; | |
8556 | } | |
8557 | } | |
8558 | } | |
8559 | return 0; | |
8560 | } | |
8561 | ||
8562 | bool TemplateMixin::hasPointers() | |
8563 | { | |
8564 | //printf("TemplateMixin::hasPointers() %s\n", toChars()); | |
8565 | ||
8566 | if (members) | |
8567 | { | |
8568 | for (size_t i = 0; i < members->dim; i++) | |
8569 | { | |
8570 | Dsymbol *s = (*members)[i]; | |
8571 | //printf(" s = %s %s\n", s->kind(), s->toChars()); | |
8572 | if (s->hasPointers()) | |
8573 | { | |
8574 | return true; | |
8575 | } | |
8576 | } | |
8577 | } | |
8578 | return false; | |
8579 | } | |
8580 | ||
8581 | void TemplateMixin::setFieldOffset(AggregateDeclaration *ad, unsigned *poffset, bool isunion) | |
8582 | { | |
8583 | //printf("TemplateMixin::setFieldOffset() %s\n", toChars()); | |
8584 | if (_scope) // if fwd reference | |
8585 | semantic(NULL); // try to resolve it | |
8586 | if (members) | |
8587 | { | |
8588 | for (size_t i = 0; i < members->dim; i++) | |
8589 | { | |
8590 | Dsymbol *s = (*members)[i]; | |
8591 | //printf("\t%s\n", s->toChars()); | |
8592 | s->setFieldOffset(ad, poffset, isunion); | |
8593 | } | |
8594 | } | |
8595 | } | |
8596 | ||
8597 | const char *TemplateMixin::toChars() | |
8598 | { | |
8599 | OutBuffer buf; | |
8600 | toCBufferInstance(this, &buf); | |
8601 | return buf.extractString(); | |
8602 | } |