comparison dmd/template.c @ 1:c53b6e3fe49a trunk

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