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