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