-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtemplate.c
6189 lines (5501 loc) · 186 KB
/
template.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Compiler implementation of the D programming language
// Copyright (c) 1999-2012 by Digital Mars
// All Rights Reserved
// written by Walter Bright
// http://www.digitalmars.com
// License for redistribution is by either the Artistic License
// in artistic.txt, or the GNU General Public License in gnu.txt.
// See the included readme.txt for details.
// Handle template implementation
#include <stdio.h>
#include <assert.h>
#include "root.h"
#include "aav.h"
#include "rmem.h"
#include "stringtable.h"
#include "mtype.h"
#include "template.h"
#include "init.h"
#include "expression.h"
#include "scope.h"
#include "module.h"
#include "aggregate.h"
#include "declaration.h"
#include "dsymbol.h"
#include "mars.h"
#include "dsymbol.h"
#include "identifier.h"
#include "hdrgen.h"
#include "id.h"
#if WINDOWS_SEH
#include <windows.h>
long __cdecl __ehfilter(LPEXCEPTION_POINTERS ep);
#endif
#define LOG 0
/********************************************
* These functions substitute for dynamic_cast. dynamic_cast does not work
* on earlier versions of gcc.
*/
Expression *isExpression(Object *o)
{
//return dynamic_cast<Expression *>(o);
if (!o || o->dyncast() != DYNCAST_EXPRESSION)
return NULL;
return (Expression *)o;
}
Dsymbol *isDsymbol(Object *o)
{
//return dynamic_cast<Dsymbol *>(o);
if (!o || o->dyncast() != DYNCAST_DSYMBOL)
return NULL;
return (Dsymbol *)o;
}
Type *isType(Object *o)
{
//return dynamic_cast<Type *>(o);
if (!o || o->dyncast() != DYNCAST_TYPE)
return NULL;
return (Type *)o;
}
Tuple *isTuple(Object *o)
{
//return dynamic_cast<Tuple *>(o);
if (!o || o->dyncast() != DYNCAST_TUPLE)
return NULL;
return (Tuple *)o;
}
/**************************************
* Is this Object an error?
*/
int isError(Object *o)
{
Type *t = isType(o);
if (t)
return (t->ty == Terror);
Expression *e = isExpression(o);
if (e)
return (e->op == TOKerror);
Tuple *v = isTuple(o);
if (v)
return arrayObjectIsError(&v->objects);
return 0;
}
/**************************************
* Are any of the Objects an error?
*/
int arrayObjectIsError(Objects *args)
{
for (size_t i = 0; i < args->dim; i++)
{
Object *o = args->tdata()[i];
if (isError(o))
return 1;
}
return 0;
}
/***********************
* Try to get arg as a type.
*/
Type *getType(Object *o)
{
Type *t = isType(o);
if (!t)
{ Expression *e = isExpression(o);
if (e)
t = e->type;
}
return t;
}
Dsymbol *getDsymbol(Object *oarg)
{
Dsymbol *sa;
Expression *ea = isExpression(oarg);
if (ea)
{ // Try to convert Expression to symbol
if (ea->op == TOKvar)
sa = ((VarExp *)ea)->var;
else if (ea->op == TOKfunction)
sa = ((FuncExp *)ea)->fd;
else
sa = NULL;
}
else
{ // Try to convert Type to symbol
Type *ta = isType(oarg);
if (ta)
sa = ta->toDsymbol(NULL);
else
sa = isDsymbol(oarg); // if already a symbol
}
return sa;
}
/******************************
* If o1 matches o2, return 1.
* Else, return 0.
*/
int match(Object *o1, Object *o2, TemplateDeclaration *tempdecl, Scope *sc)
{
Type *t1 = isType(o1);
Type *t2 = isType(o2);
Expression *e1 = isExpression(o1);
Expression *e2 = isExpression(o2);
Dsymbol *s1 = isDsymbol(o1);
Dsymbol *s2 = isDsymbol(o2);
Tuple *u1 = isTuple(o1);
Tuple *u2 = isTuple(o2);
//printf("\t match t1 %p t2 %p, e1 %p e2 %p, s1 %p s2 %p, u1 %p u2 %p\n", t1,t2,e1,e2,s1,s2,u1,u2);
/* A proper implementation of the various equals() overrides
* should make it possible to just do o1->equals(o2), but
* we'll do that another day.
*/
if (s1)
{
VarDeclaration *v1 = s1->isVarDeclaration();
if (v1 && v1->storage_class & STCmanifest)
{ ExpInitializer *ei1 = v1->init->isExpInitializer();
if (ei1)
e1 = ei1->exp, s1 = NULL;
}
}
if (s2)
{
VarDeclaration *v2 = s2->isVarDeclaration();
if (v2 && v2->storage_class & STCmanifest)
{ ExpInitializer *ei2 = v2->init->isExpInitializer();
if (ei2)
e2 = ei2->exp, s2 = NULL;
}
}
if (t1)
{
/* if t1 is an instance of ti, then give error
* about recursive expansions.
*/
Dsymbol *s = t1->toDsymbol(sc);
if (s && s->parent)
{ TemplateInstance *ti1 = s->parent->isTemplateInstance();
if (ti1 && ti1->tempdecl == tempdecl)
{
for (Scope *sc1 = sc; sc1; sc1 = sc1->enclosing)
{
if (sc1->scopesym == ti1)
{
error("recursive template expansion for template argument %s", t1->toChars());
return 1; // fake a match
}
}
}
}
//printf("t1 = %s\n", t1->toChars());
//printf("t2 = %s\n", t2->toChars());
if (!t2 || !t1->equals(t2))
goto Lnomatch;
}
else if (e1)
{
#if 0
if (e1 && e2)
{
printf("match %d\n", e1->equals(e2));
e1->print();
e2->print();
e1->type->print();
e2->type->print();
}
#endif
if (!e2)
goto Lnomatch;
if (!e1->equals(e2))
goto Lnomatch;
}
else if (s1)
{
if (!s2 || !s1->equals(s2) || s1->parent != s2->parent)
goto Lnomatch;
}
else if (u1)
{
if (!u2)
goto Lnomatch;
if (u1->objects.dim != u2->objects.dim)
goto Lnomatch;
for (size_t i = 0; i < u1->objects.dim; i++)
{
if (!match(u1->objects.tdata()[i],
u2->objects.tdata()[i],
tempdecl, sc))
goto Lnomatch;
}
}
//printf("match\n");
return 1; // match
Lnomatch:
//printf("nomatch\n");
return 0; // nomatch;
}
/************************************
* Match an array of them.
*/
int arrayObjectMatch(Objects *oa1, Objects *oa2, TemplateDeclaration *tempdecl, Scope *sc)
{
if (oa1 == oa2)
return 1;
if (oa1->dim != oa2->dim)
return 0;
for (size_t j = 0; j < oa1->dim; j++)
{ Object *o1 = oa1->tdata()[j];
Object *o2 = oa2->tdata()[j];
if (!match(o1, o2, tempdecl, sc))
{
return 0;
}
}
return 1;
}
/****************************************
* This makes a 'pretty' version of the template arguments.
* It's analogous to genIdent() which makes a mangled version.
*/
void ObjectToCBuffer(OutBuffer *buf, HdrGenState *hgs, Object *oarg)
{
//printf("ObjectToCBuffer()\n");
Type *t = isType(oarg);
Expression *e = isExpression(oarg);
Dsymbol *s = isDsymbol(oarg);
Tuple *v = isTuple(oarg);
/* The logic of this should match what genIdent() does. The _dynamic_cast()
* function relies on all the pretty strings to be unique for different classes
* (see Bugzilla 7375).
* Perhaps it would be better to demangle what genIdent() does.
*/
if (t)
{ //printf("\tt: %s ty = %d\n", t->toChars(), t->ty);
t->toCBuffer(buf, NULL, hgs);
}
else if (e)
{
if (e->op == TOKvar)
e = e->optimize(WANTvalue); // added to fix Bugzilla 7375
e->toCBuffer(buf, hgs);
}
else if (s)
{
char *p = s->ident ? s->ident->toChars() : s->toChars();
buf->writestring(p);
}
else if (v)
{
Objects *args = &v->objects;
for (size_t i = 0; i < args->dim; i++)
{
if (i)
buf->writeByte(',');
Object *o = (*args)[i];
ObjectToCBuffer(buf, hgs, o);
}
}
else if (!oarg)
{
buf->writestring("NULL");
}
else
{
#ifdef DEBUG
printf("bad Object = %p\n", oarg);
#endif
assert(0);
}
}
#if DMDV2
Object *objectSyntaxCopy(Object *o)
{
if (!o)
return NULL;
Type *t = isType(o);
if (t)
return t->syntaxCopy();
Expression *e = isExpression(o);
if (e)
return e->syntaxCopy();
return o;
}
#endif
/* ======================== TemplateDeclaration ============================= */
TemplateDeclaration::TemplateDeclaration(Loc loc, Identifier *id,
TemplateParameters *parameters, Expression *constraint, Dsymbols *decldefs, int ismixin)
: ScopeDsymbol(id)
{
#if LOG
printf("TemplateDeclaration(this = %p, id = '%s')\n", this, id->toChars());
#endif
#if 0
if (parameters)
for (int i = 0; i < parameters->dim; i++)
{ TemplateParameter *tp = parameters->tdata()[i];
//printf("\tparameter[%d] = %p\n", i, tp);
TemplateTypeParameter *ttp = tp->isTemplateTypeParameter();
if (ttp)
{
printf("\tparameter[%d] = %s : %s\n", i, tp->ident->toChars(), ttp->specType ? ttp->specType->toChars() : "");
}
}
#endif
this->loc = loc;
this->parameters = parameters;
this->origParameters = parameters;
this->constraint = constraint;
this->members = decldefs;
this->overnext = NULL;
this->overroot = NULL;
this->semanticRun = 0;
this->onemember = NULL;
this->literal = 0;
this->ismixin = ismixin;
this->previous = NULL;
// Compute in advance for Ddoc's use
if (members)
{
Dsymbol *s;
if (Dsymbol::oneMembers(members, &s, ident) && s)
{
onemember = s;
s->parent = this;
}
}
}
Dsymbol *TemplateDeclaration::syntaxCopy(Dsymbol *)
{
//printf("TemplateDeclaration::syntaxCopy()\n");
TemplateDeclaration *td;
TemplateParameters *p;
p = NULL;
if (parameters)
{
p = new TemplateParameters();
p->setDim(parameters->dim);
for (size_t i = 0; i < p->dim; i++)
{ TemplateParameter *tp = (*parameters)[i];
p->tdata()[i] = tp->syntaxCopy();
}
}
Expression *e = NULL;
if (constraint)
e = constraint->syntaxCopy();
Dsymbols *d = Dsymbol::arraySyntaxCopy(members);
td = new TemplateDeclaration(loc, ident, p, e, d, ismixin);
return td;
}
void TemplateDeclaration::semantic(Scope *sc)
{
#if LOG
printf("TemplateDeclaration::semantic(this = %p, id = '%s')\n", this, ident->toChars());
printf("sc->stc = %llx\n", sc->stc);
printf("sc->module = %s\n", sc->module->toChars());
#endif
if (semanticRun)
return; // semantic() already run
semanticRun = 1;
if (sc->module && sc->module->ident == Id::object && ident == Id::AssociativeArray)
{ Type::associativearray = this;
}
if (sc->func)
{
#if DMDV1
error("cannot declare template at function scope %s", sc->func->toChars());
#endif
}
if (/*global.params.useArrayBounds &&*/ sc->module)
{
// Generate this function as it may be used
// when template is instantiated in other modules
sc->module->toModuleArray();
}
if (/*global.params.useAssert &&*/ sc->module)
{
// Generate this function as it may be used
// when template is instantiated in other modules
sc->module->toModuleAssert();
}
#if DMDV2
if (/*global.params.useUnitTests &&*/ sc->module)
{
// Generate this function as it may be used
// when template is instantiated in other modules
sc->module->toModuleUnittest();
}
#endif
/* Remember Scope for later instantiations, but make
* a copy since attributes can change.
*/
this->scope = new Scope(*sc);
this->scope->setNoFree();
// Set up scope for parameters
ScopeDsymbol *paramsym = new ScopeDsymbol();
paramsym->parent = sc->parent;
Scope *paramscope = sc->push(paramsym);
paramscope->parameterSpecialization = 1;
paramscope->stc = 0;
if (!parent)
parent = sc->parent;
if (global.params.doDocComments)
{
origParameters = new TemplateParameters();
origParameters->setDim(parameters->dim);
for (size_t i = 0; i < parameters->dim; i++)
{
TemplateParameter *tp = parameters->tdata()[i];
origParameters->tdata()[i] = tp->syntaxCopy();
}
}
for (size_t i = 0; i < parameters->dim; i++)
{
TemplateParameter *tp = parameters->tdata()[i];
tp->declareParameter(paramscope);
}
for (size_t i = 0; i < parameters->dim; i++)
{
TemplateParameter *tp = parameters->tdata()[i];
tp->semantic(paramscope);
if (i + 1 != parameters->dim && tp->isTemplateTupleParameter())
error("template tuple parameter must be last one");
}
paramscope->pop();
// Compute again
onemember = NULL;
if (members)
{
Dsymbol *s;
if (Dsymbol::oneMembers(members, &s, ident) && s)
{
onemember = s;
s->parent = this;
}
}
/* BUG: should check:
* o no virtual functions or non-static data members of classes
*/
}
const char *TemplateDeclaration::kind()
{
return (onemember && onemember->isAggregateDeclaration())
? onemember->kind()
: (char *)"template";
}
/**********************************
* Overload existing TemplateDeclaration 'this' with the new one 's'.
* Return !=0 if successful; i.e. no conflict.
*/
int TemplateDeclaration::overloadInsert(Dsymbol *s)
{
TemplateDeclaration **pf;
TemplateDeclaration *f;
#if LOG
printf("TemplateDeclaration::overloadInsert('%s')\n", s->toChars());
#endif
f = s->isTemplateDeclaration();
if (!f)
return FALSE;
TemplateDeclaration *pthis = this;
for (pf = &pthis; *pf; pf = &(*pf)->overnext)
{
#if 0
// Conflict if TemplateParameter's match
// Will get caught anyway later with TemplateInstance, but
// should check it now.
TemplateDeclaration *f2 = *pf;
if (f->parameters->dim != f2->parameters->dim)
goto Lcontinue;
for (size_t i = 0; i < f->parameters->dim; i++)
{ TemplateParameter *p1 = f->parameters->tdata()[i];
TemplateParameter *p2 = f2->parameters->tdata()[i];
if (!p1->overloadMatch(p2))
goto Lcontinue;
}
#if LOG
printf("\tfalse: conflict\n");
#endif
return FALSE;
Lcontinue:
;
#endif
}
f->overroot = this;
*pf = f;
#if LOG
printf("\ttrue: no conflict\n");
#endif
return TRUE;
}
/****************************
* Declare all the function parameters as variables
* and add them to the scope
*/
void TemplateDeclaration::makeParamNamesVisibleInConstraint(Scope *paramscope, Expressions *fargs)
{
/* We do this ONLY if there is only one function in the template.
*/
FuncDeclaration *fd = onemember && onemember->toAlias() ?
onemember->toAlias()->isFuncDeclaration() : NULL;
if (fd)
{
/*
Making parameters is similar to FuncDeclaration::semantic3
*/
paramscope->parent = fd;
TypeFunction *tf = (TypeFunction *)fd->type->syntaxCopy();
// Shouldn't run semantic on default arguments and return type.
for (int i = 0; i<tf->parameters->dim; i++)
tf->parameters->tdata()[i]->defaultArg = NULL;
tf->next = NULL;
// Resolve parameter types and 'auto ref's.
tf->fargs = fargs;
tf = (TypeFunction *)tf->semantic(loc, paramscope);
Parameters *fparameters = tf->parameters;
int fvarargs = tf->varargs;
size_t nfparams = Parameter::dim(fparameters); // Num function parameters
for (size_t i = 0; i < nfparams; i++)
{
Parameter *fparam = Parameter::getNth(fparameters, i);
// Remove addMod same as func.d L1065 of FuncDeclaration::semantic3
//Type *vtype = fparam->type;
//if (fd->type && fd->isPure())
// vtype = vtype->addMod(MODconst);
fparam->storageClass &= (STCin | STCout | STCref | STClazy | STCfinal | STC_TYPECTOR | STCnodtor);
fparam->storageClass |= STCparameter;
if (fvarargs == 2 && i + 1 == nfparams)
fparam->storageClass |= STCvariadic;
}
for (size_t i = 0; i < fparameters->dim; i++)
{
Parameter *fparam = fparameters->tdata()[i];
if (!fparam->ident)
continue; // don't add it, if it has no name
VarDeclaration *v = new VarDeclaration(loc, fparam->type, fparam->ident, NULL);
v->storage_class = fparam->storageClass;
v->semantic(paramscope);
if (!paramscope->insert(v))
error("parameter %s.%s is already defined", toChars(), v->toChars());
else
v->parent = this;
}
}
}
/***************************************
* Given that ti is an instance of this TemplateDeclaration,
* deduce the types of the parameters to this, and store
* those deduced types in dedtypes[].
* Input:
* flag 1: don't do semantic() because of dummy types
* 2: don't change types in matchArg()
* Output:
* dedtypes deduced arguments
* Return match level.
*/
MATCH TemplateDeclaration::matchWithInstance(TemplateInstance *ti,
Objects *dedtypes, Expressions *fargs, int flag)
{ MATCH m;
size_t dedtypes_dim = dedtypes->dim;
#define LOGM 0
#if LOGM
printf("\n+TemplateDeclaration::matchWithInstance(this = %s, ti = %s, flag = %d)\n", toChars(), ti->toChars(), flag);
#endif
#if 0
printf("dedtypes->dim = %d, parameters->dim = %d\n", dedtypes_dim, parameters->dim);
if (ti->tiargs->dim)
printf("ti->tiargs->dim = %d, [0] = %p\n",
ti->tiargs->dim,
ti->tiargs->tdata()[0]);
#endif
dedtypes->zero();
size_t parameters_dim = parameters->dim;
int variadic = isVariadic() != NULL;
// If more arguments than parameters, no match
if (ti->tiargs->dim > parameters_dim && !variadic)
{
#if LOGM
printf(" no match: more arguments than parameters\n");
#endif
return MATCHnomatch;
}
assert(dedtypes_dim == parameters_dim);
assert(dedtypes_dim >= ti->tiargs->dim || variadic);
// Set up scope for parameters
assert((size_t)scope > 0x10000);
ScopeDsymbol *paramsym = new ScopeDsymbol();
paramsym->parent = scope->parent;
Scope *paramscope = scope->push(paramsym);
paramscope->stc = 0;
// Attempt type deduction
m = MATCHexact;
for (size_t i = 0; i < dedtypes_dim; i++)
{ MATCH m2;
TemplateParameter *tp = parameters->tdata()[i];
Declaration *sparam;
//printf("\targument [%d]\n", i);
#if LOGM
//printf("\targument [%d] is %s\n", i, oarg ? oarg->toChars() : "null");
TemplateTypeParameter *ttp = tp->isTemplateTypeParameter();
if (ttp)
printf("\tparameter[%d] is %s : %s\n", i, tp->ident->toChars(), ttp->specType ? ttp->specType->toChars() : "");
#endif
m2 = tp->matchArg(paramscope, ti->tiargs, i, parameters, dedtypes, &sparam);
//printf("\tm2 = %d\n", m2);
if (m2 == MATCHnomatch)
{
#if 0
printf("\tmatchArg() for parameter %i failed\n", i);
#endif
goto Lnomatch;
}
if (m2 < m)
m = m2;
if (!flag)
sparam->semantic(paramscope);
if (!paramscope->insert(sparam))
goto Lnomatch;
}
if (!flag)
{
/* Any parameter left without a type gets the type of
* its corresponding arg
*/
for (size_t i = 0; i < dedtypes_dim; i++)
{
if (!dedtypes->tdata()[i])
{ assert(i < ti->tiargs->dim);
dedtypes->tdata()[i] = (Type *)ti->tiargs->tdata()[i];
}
}
}
#if DMDV2
if (m && constraint && !flag)
{ /* Check to see if constraint is satisfied.
*/
makeParamNamesVisibleInConstraint(paramscope, fargs);
Expression *e = constraint->syntaxCopy();
Scope *sc = paramscope->push();
/* There's a chicken-and-egg problem here. We don't know yet if this template
* instantiation will be a local one (isnested is set), and we won't know until
* after selecting the correct template. Thus, function we're nesting inside
* is not on the sc scope chain, and this can cause errors in FuncDeclaration::getLevel().
* Workaround the problem by setting a flag to relax the checking on frame errors.
*/
sc->flags |= SCOPEstaticif;
FuncDeclaration *fd = onemember && onemember->toAlias() ?
onemember->toAlias()->isFuncDeclaration() : NULL;
Dsymbol *s = parent;
while (s->isTemplateInstance() || s->isTemplateMixin())
s = s->parent;
AggregateDeclaration *ad = s->isAggregateDeclaration();
VarDeclaration *vthissave;
if (fd && ad)
{
vthissave = fd->vthis;
fd->vthis = fd->declareThis(paramscope, ad);
}
e = e->semantic(sc);
if (fd && fd->vthis)
fd->vthis = vthissave;
sc->pop();
e = e->optimize(WANTvalue | WANTinterpret);
if (e->isBool(TRUE))
;
else if (e->isBool(FALSE))
goto Lnomatch;
else
{
e->error("constraint %s is not constant or does not evaluate to a bool", e->toChars());
}
}
#endif
#if LOGM
// Print out the results
printf("--------------------------\n");
printf("template %s\n", toChars());
printf("instance %s\n", ti->toChars());
if (m)
{
for (size_t i = 0; i < dedtypes_dim; i++)
{
TemplateParameter *tp = parameters->tdata()[i];
Object *oarg;
printf(" [%d]", i);
if (i < ti->tiargs->dim)
oarg = ti->tiargs->tdata()[i];
else
oarg = NULL;
tp->print(oarg, dedtypes->tdata()[i]);
}
}
else
goto Lnomatch;
#endif
#if LOGM
printf(" match = %d\n", m);
#endif
goto Lret;
Lnomatch:
#if LOGM
printf(" no match\n");
#endif
m = MATCHnomatch;
Lret:
paramscope->pop();
#if LOGM
printf("-TemplateDeclaration::matchWithInstance(this = %p, ti = %p) = %d\n", this, ti, m);
#endif
return m;
}
/********************************************
* Determine partial specialization order of 'this' vs td2.
* Returns:
* match this is at least as specialized as td2
* 0 td2 is more specialized than this
*/
MATCH TemplateDeclaration::leastAsSpecialized(TemplateDeclaration *td2, Expressions *fargs)
{
/* This works by taking the template parameters to this template
* declaration and feeding them to td2 as if it were a template
* instance.
* If it works, then this template is at least as specialized
* as td2.
*/
TemplateInstance ti(0, ident); // create dummy template instance
Objects dedtypes;
#define LOG_LEASTAS 0
#if LOG_LEASTAS
printf("%s.leastAsSpecialized(%s)\n", toChars(), td2->toChars());
#endif
// Set type arguments to dummy template instance to be types
// generated from the parameters to this template declaration
ti.tiargs = new Objects();
ti.tiargs->setDim(parameters->dim);
for (size_t i = 0; i < ti.tiargs->dim; i++)
{
TemplateParameter *tp = parameters->tdata()[i];
Object *p = (Object *)tp->dummyArg();
if (p)
ti.tiargs->tdata()[i] = p;
else
ti.tiargs->setDim(i);
}
// Temporary Array to hold deduced types
//dedtypes.setDim(parameters->dim);
dedtypes.setDim(td2->parameters->dim);
// Attempt a type deduction
MATCH m = td2->matchWithInstance(&ti, &dedtypes, fargs, 1);
if (m)
{
/* A non-variadic template is more specialized than a
* variadic one.
*/
if (isVariadic() && !td2->isVariadic())
goto L1;
#if LOG_LEASTAS
printf(" matches %d, so is least as specialized\n", m);
#endif
return m;
}
L1:
#if LOG_LEASTAS
printf(" doesn't match, so is not as specialized\n");
#endif
return MATCHnomatch;
}
/*************************************************
* Match function arguments against a specific template function.
* Input:
* loc instantiation location
* targsi Expression/Type initial list of template arguments
* ethis 'this' argument if !NULL
* fargs arguments to function
* Output:
* dedargs Expression/Type deduced template arguments
* Returns:
* match level
*/
MATCH TemplateDeclaration::deduceFunctionTemplateMatch(Scope *sc, Loc loc, Objects *targsi,
Expression *ethis, Expressions *fargs,
Objects *dedargs)
{
size_t nfparams;
size_t nfargs;
size_t nargsi; // array size of targsi
int fptupindex = -1;
int tuple_dim = 0;
MATCH match = MATCHexact;
FuncDeclaration *fd = onemember->toAlias()->isFuncDeclaration();
Parameters *fparameters; // function parameter list
int fvarargs; // function varargs
Objects dedtypes; // for T:T*, the dedargs is the T*, dedtypes is the T
unsigned wildmatch = 0;
TypeFunction *tf = (TypeFunction *)fd->type;
#if 0
printf("\nTemplateDeclaration::deduceFunctionTemplateMatch() %s\n", toChars());
for (size_t i = 0; i < fargs->dim; i++)
{ Expression *e = fargs->tdata()[i];
printf("\tfarg[%d] is %s, type is %s\n", i, e->toChars(), e->type->toChars());
}
printf("fd = %s\n", fd->toChars());
printf("fd->type = %s\n", fd->type->toChars());
if (ethis)
printf("ethis->type = %s\n", ethis->type->toChars());
#endif
assert((size_t)scope > 0x10000);
dedargs->setDim(parameters->dim);
dedargs->zero();
dedtypes.setDim(parameters->dim);
dedtypes.zero();
// Set up scope for parameters
ScopeDsymbol *paramsym = new ScopeDsymbol();
paramsym->parent = scope->parent;
Scope *paramscope = scope->push(paramsym);
paramscope->stc = 0;
TemplateTupleParameter *tp = isVariadic();
int tp_is_declared = 0;
#if 0
for (size_t i = 0; i < dedargs->dim; i++)
{
printf("\tdedarg[%d] = ", i);
Object *oarg = dedargs->tdata()[i];
if (oarg) printf("%s", oarg->toChars());
printf("\n");
}
#endif
nargsi = 0;
if (targsi)
{ // Set initial template arguments
nargsi = targsi->dim;
size_t n = parameters->dim;
if (tp)
n--;
if (nargsi > n)
{ if (!tp)
goto Lnomatch;
/* The extra initial template arguments
* now form the tuple argument.
*/
Tuple *t = new Tuple();