Mercurial > projects > ldc
annotate dmd/template.c @ 1615:3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
bugzilla 2816 Sudden-death static assert is not very useful.
---
dmd/expression.h | 12 ++++++
dmd/staticassert.c | 10 +---
dmd/template.c | 108 ++++++++++++++++++++++++++++++++++++++++++++++++----
dmd/template.h | 2 +
4 files changed, 117 insertions(+), 15 deletions(-)
author | Leandro Lucarella <llucax@gmail.com> |
---|---|
date | Wed, 06 Jan 2010 15:18:21 -0300 |
parents | 4f63d530861f |
children | a87f1d6ff48e |
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 { | |
1615
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
3500 #if WINDOWS_SEH |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
3501 __try |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
3502 { |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
3503 #endif |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
3504 static int nest; |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
3505 if (++nest > 300) |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
3506 { |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
3507 global.gag = 0; // ensure error message gets printed |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
3508 error("recursive expansion"); |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
3509 fatal(); |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
3510 } |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
3511 semantic3(sc2); |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
3512 --nest; |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
3513 #if WINDOWS_SEH |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
3514 } |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
3515 __except (__ehfilter(GetExceptionInformation())) |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
3516 { |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
3517 global.gag = 0; // ensure error message gets printed |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
3518 error("recursive expansion"); |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
3519 fatal(); |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
3520 } |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
3521 #endif |
336 | 3522 } |
3523 | |
3524 Laftersemantic: | |
3525 sc2->pop(); | |
3526 | |
3527 scope->pop(); | |
3528 | |
3529 // Give additional context info if error occurred during instantiation | |
3530 if (global.errors != errorsave) | |
3531 { | |
3532 error("error instantiating"); | |
1615
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
3533 if (tinst) |
1367
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1195
diff
changeset
|
3534 { tinst->printInstantiationTrace(); |
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1195
diff
changeset
|
3535 fatal(); |
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1195
diff
changeset
|
3536 } |
336 | 3537 errors = 1; |
3538 if (global.gag) | |
3539 tempdecl->instances.remove(tempdecl_instance_idx); | |
3540 } | |
3541 | |
3542 #if LOG | |
3543 printf("-TemplateInstance::semantic('%s', this=%p)\n", toChars(), this); | |
3544 #endif | |
3545 } | |
3546 | |
3547 | |
3548 void TemplateInstance::semanticTiargs(Scope *sc) | |
3549 { | |
3550 //printf("+TemplateInstance::semanticTiargs() %s\n", toChars()); | |
3551 if (semantictiargsdone) | |
3552 return; | |
3553 semantictiargsdone = 1; | |
1195
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1174
diff
changeset
|
3554 semanticTiargs(loc, sc, tiargs, 0); |
336 | 3555 } |
3556 | |
1195
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1174
diff
changeset
|
3557 /********************************** |
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1174
diff
changeset
|
3558 * Input: |
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1174
diff
changeset
|
3559 * 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
|
3560 */ |
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1174
diff
changeset
|
3561 |
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1174
diff
changeset
|
3562 void TemplateInstance::semanticTiargs(Loc loc, Scope *sc, Objects *tiargs, int flags) |
336 | 3563 { |
3564 // Run semantic on each argument, place results in tiargs[] | |
3565 //printf("+TemplateInstance::semanticTiargs() %s\n", toChars()); | |
3566 if (!tiargs) | |
3567 return; | |
3568 for (size_t j = 0; j < tiargs->dim; j++) | |
3569 { | |
3570 Object *o = (Object *)tiargs->data[j]; | |
3571 Type *ta = isType(o); | |
3572 Expression *ea = isExpression(o); | |
3573 Dsymbol *sa = isDsymbol(o); | |
3574 | |
3575 //printf("1: tiargs->data[%d] = %p, %p, %p, ea=%p, ta=%p\n", j, o, isDsymbol(o), isTuple(o), ea, ta); | |
3576 if (ta) | |
3577 { | |
3578 //printf("type %s\n", ta->toChars()); | |
3579 // It might really be an Expression or an Alias | |
3580 ta->resolve(loc, sc, &ea, &ta, &sa); | |
3581 if (ea) | |
3582 { | |
3583 ea = ea->semantic(sc); | |
3584 ea = ea->optimize(WANTvalue | WANTinterpret); | |
3585 tiargs->data[j] = ea; | |
3586 } | |
3587 else if (sa) | |
3588 { tiargs->data[j] = sa; | |
3589 TupleDeclaration *d = sa->toAlias()->isTupleDeclaration(); | |
3590 if (d) | |
3591 { | |
3592 size_t dim = d->objects->dim; | |
3593 tiargs->remove(j); | |
3594 tiargs->insert(j, d->objects); | |
3595 j--; | |
3596 } | |
3597 } | |
3598 else if (ta) | |
3599 { | |
1587 | 3600 Ltype: |
336 | 3601 if (ta->ty == Ttuple) |
3602 { // Expand tuple | |
3603 TypeTuple *tt = (TypeTuple *)ta; | |
3604 size_t dim = tt->arguments->dim; | |
3605 tiargs->remove(j); | |
3606 if (dim) | |
3607 { tiargs->reserve(dim); | |
3608 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
|
3609 { Parameter *arg = (Parameter *)tt->arguments->data[i]; |
336 | 3610 tiargs->insert(j + i, arg->type); |
3611 } | |
3612 } | |
3613 j--; | |
3614 } | |
3615 else | |
3616 tiargs->data[j] = ta; | |
3617 } | |
3618 else | |
3619 { | |
3620 assert(global.errors); | |
3621 tiargs->data[j] = Type::terror; | |
3622 } | |
3623 } | |
3624 else if (ea) | |
3625 { | |
3626 if (!ea) | |
3627 { assert(global.errors); | |
3628 ea = new IntegerExp(0); | |
3629 } | |
3630 assert(ea); | |
3631 ea = ea->semantic(sc); | |
3632 ea = ea->optimize(WANTvalue | WANTinterpret); | |
3633 tiargs->data[j] = ea; | |
3634 if (ea->op == TOKtype) | |
1587 | 3635 { ta = ea->type; |
3636 goto Ltype; | |
3637 } | |
3638 if (ea->op == TOKtuple) | |
3639 { // Expand tuple | |
3640 TupleExp *te = (TupleExp *)ea; | |
3641 size_t dim = te->exps->dim; | |
3642 tiargs->remove(j); | |
3643 if (dim) | |
3644 { tiargs->reserve(dim); | |
3645 for (size_t i = 0; i < dim; i++) | |
3646 tiargs->insert(j + i, te->exps->data[i]); | |
3647 } | |
3648 j--; | |
3649 } | |
336 | 3650 } |
3651 else if (sa) | |
3652 { | |
3653 } | |
3654 else | |
3655 { | |
3656 assert(0); | |
3657 } | |
3658 //printf("1: tiargs->data[%d] = %p\n", j, tiargs->data[j]); | |
3659 } | |
3660 #if 0 | |
3661 printf("-TemplateInstance::semanticTiargs('%s', this=%p)\n", toChars(), this); | |
3662 for (size_t j = 0; j < tiargs->dim; j++) | |
3663 { | |
3664 Object *o = (Object *)tiargs->data[j]; | |
3665 Type *ta = isType(o); | |
3666 Expression *ea = isExpression(o); | |
3667 Dsymbol *sa = isDsymbol(o); | |
3668 Tuple *va = isTuple(o); | |
3669 | |
3670 printf("\ttiargs[%d] = ta %p, ea %p, sa %p, va %p\n", j, ta, ea, sa, va); | |
3671 } | |
3672 #endif | |
3673 } | |
3674 | |
3675 /********************************************** | |
3676 * Find template declaration corresponding to template instance. | |
3677 */ | |
3678 | |
3679 TemplateDeclaration *TemplateInstance::findTemplateDeclaration(Scope *sc) | |
3680 { | |
3681 //printf("TemplateInstance::findTemplateDeclaration() %s\n", toChars()); | |
3682 if (!tempdecl) | |
3683 { | |
3684 /* Given: | |
3685 * foo!( ... ) | |
3686 * figure out which TemplateDeclaration foo refers to. | |
3687 */ | |
3688 Dsymbol *s; | |
3689 Dsymbol *scopesym; | |
3690 Identifier *id; | |
3691 int i; | |
3692 | |
3693 id = name; | |
3694 s = sc->search(loc, id, &scopesym); | |
3695 if (!s) | |
1615
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
3696 { error("template '%s' is not defined", id->toChars()); |
336 | 3697 return NULL; |
3698 } | |
3699 #if LOG | |
3700 printf("It's an instance of '%s' kind '%s'\n", s->toChars(), s->kind()); | |
3701 if (s->parent) | |
3702 printf("s->parent = '%s'\n", s->parent->toChars()); | |
3703 #endif | |
3704 withsym = scopesym->isWithScopeSymbol(); | |
3705 | |
3706 /* We might have found an alias within a template when | |
3707 * we really want the template. | |
3708 */ | |
3709 TemplateInstance *ti; | |
3710 if (s->parent && | |
3711 (ti = s->parent->isTemplateInstance()) != NULL) | |
3712 { | |
3713 if ( | |
3714 (ti->name == id || | |
3715 ti->toAlias()->ident == id) | |
3716 && | |
3717 ti->tempdecl) | |
3718 { | |
3719 /* This is so that one can refer to the enclosing | |
3720 * template, even if it has the same name as a member | |
3721 * of the template, if it has a !(arguments) | |
3722 */ | |
3723 tempdecl = ti->tempdecl; | |
3724 if (tempdecl->overroot) // if not start of overloaded list of TemplateDeclaration's | |
3725 tempdecl = tempdecl->overroot; // then get the start | |
3726 s = tempdecl; | |
3727 } | |
3728 } | |
3729 | |
3730 s = s->toAlias(); | |
3731 | |
3732 /* It should be a TemplateDeclaration, not some other symbol | |
3733 */ | |
3734 tempdecl = s->isTemplateDeclaration(); | |
3735 if (!tempdecl) | |
3736 { | |
3737 if (!s->parent && global.errors) | |
3738 return NULL; | |
3739 if (!s->parent && s->getType()) | |
3740 { Dsymbol *s2 = s->getType()->toDsymbol(sc); | |
3741 if (!s2) | |
3742 { | |
3743 error("%s is not a template declaration, it is a %s", id->toChars(), s->kind()); | |
3744 return NULL; | |
3745 } | |
3746 s = s2; | |
3747 } | |
3748 #ifdef DEBUG | |
3749 //if (!s->parent) printf("s = %s %s\n", s->kind(), s->toChars()); | |
3750 #endif | |
3751 //assert(s->parent); | |
3752 TemplateInstance *ti = s->parent ? s->parent->isTemplateInstance() : NULL; | |
3753 if (ti && | |
3754 (ti->name == id || | |
3755 ti->toAlias()->ident == id) | |
3756 && | |
3757 ti->tempdecl) | |
3758 { | |
3759 /* This is so that one can refer to the enclosing | |
3760 * template, even if it has the same name as a member | |
3761 * of the template, if it has a !(arguments) | |
3762 */ | |
3763 tempdecl = ti->tempdecl; | |
3764 if (tempdecl->overroot) // if not start of overloaded list of TemplateDeclaration's | |
3765 tempdecl = tempdecl->overroot; // then get the start | |
3766 } | |
3767 else | |
3768 { | |
3769 error("%s is not a template declaration, it is a %s", id->toChars(), s->kind()); | |
3770 return NULL; | |
3771 } | |
3772 } | |
3773 } | |
3774 else | |
3775 assert(tempdecl->isTemplateDeclaration()); | |
3776 return tempdecl; | |
3777 } | |
3778 | |
3779 TemplateDeclaration *TemplateInstance::findBestMatch(Scope *sc) | |
3780 { | |
3781 /* Since there can be multiple TemplateDeclaration's with the same | |
3782 * name, look for the best match. | |
3783 */ | |
3784 TemplateDeclaration *td_ambig = NULL; | |
3785 TemplateDeclaration *td_best = NULL; | |
3786 MATCH m_best = MATCHnomatch; | |
3787 Objects dedtypes; | |
3788 | |
3789 #if LOG | |
3790 printf("TemplateInstance::findBestMatch()\n"); | |
3791 #endif | |
1587 | 3792 // First look for forward references |
3793 for (TemplateDeclaration *td = tempdecl; td; td = td->overnext) | |
3794 { | |
3795 if (!td->semanticRun) | |
3796 { | |
3797 if (td->scope) | |
3798 { // Try to fix forward reference | |
3799 td->semantic(td->scope); | |
3800 } | |
3801 if (!td->semanticRun) | |
3802 { | |
3803 error("%s forward references template declaration %s\n", toChars(), td->toChars()); | |
3804 return NULL; | |
3805 } | |
3806 } | |
3807 } | |
3808 | |
336 | 3809 for (TemplateDeclaration *td = tempdecl; td; td = td->overnext) |
3810 { | |
3811 MATCH m; | |
3812 | |
3813 //if (tiargs->dim) printf("2: tiargs->dim = %d, data[0] = %p\n", tiargs->dim, tiargs->data[0]); | |
3814 | |
3815 // If more arguments than parameters, | |
3816 // then this is no match. | |
3817 if (td->parameters->dim < tiargs->dim) | |
3818 { | |
3819 if (!td->isVariadic()) | |
3820 continue; | |
3821 } | |
3822 | |
3823 dedtypes.setDim(td->parameters->dim); | |
3824 dedtypes.zero(); | |
1587 | 3825 assert(td->semanticRun); |
336 | 3826 m = td->matchWithInstance(this, &dedtypes, 0); |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
3827 //printf("matchWithInstance = %d\n", m); |
336 | 3828 if (!m) // no match at all |
3829 continue; | |
3830 | |
3831 if (m < m_best) | |
3832 goto Ltd_best; | |
3833 if (m > m_best) | |
3834 goto Ltd; | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
3835 |
336 | 3836 { |
3837 // 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
|
3838 MATCH c1 = td->leastAsSpecialized(td_best); |
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1174
diff
changeset
|
3839 MATCH c2 = td_best->leastAsSpecialized(td); |
336 | 3840 //printf("c1 = %d, c2 = %d\n", c1, c2); |
3841 | |
3842 if (c1 > c2) | |
3843 goto Ltd; | |
3844 else if (c1 < c2) | |
3845 goto Ltd_best; | |
3846 else | |
3847 goto Lambig; | |
3848 } | |
3849 | |
3850 Lambig: // td_best and td are ambiguous | |
3851 td_ambig = td; | |
3852 continue; | |
3853 | |
3854 Ltd_best: // td_best is the best match so far | |
3855 td_ambig = NULL; | |
3856 continue; | |
3857 | |
3858 Ltd: // td is the new best match | |
3859 td_ambig = NULL; | |
3860 td_best = td; | |
3861 m_best = m; | |
3862 tdtypes.setDim(dedtypes.dim); | |
3863 memcpy(tdtypes.data, dedtypes.data, tdtypes.dim * sizeof(void *)); | |
3864 continue; | |
3865 } | |
3866 | |
3867 if (!td_best) | |
3868 { | |
1195
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1174
diff
changeset
|
3869 if (tempdecl && !tempdecl->overnext) |
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1174
diff
changeset
|
3870 // 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
|
3871 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
|
3872 else |
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1174
diff
changeset
|
3873 error("%s does not match any template declaration", toChars()); |
336 | 3874 return NULL; |
3875 } | |
3876 if (td_ambig) | |
3877 { | |
3878 error("%s matches more than one template declaration, %s and %s", | |
3879 toChars(), td_best->toChars(), td_ambig->toChars()); | |
3880 } | |
3881 | |
3882 /* The best match is td_best | |
3883 */ | |
3884 tempdecl = td_best; | |
3885 | |
3886 #if 0 | |
3887 /* Cast any value arguments to be same type as value parameter | |
3888 */ | |
3889 for (size_t i = 0; i < tiargs->dim; i++) | |
3890 { Object *o = (Object *)tiargs->data[i]; | |
3891 Expression *ea = isExpression(o); // value argument | |
3892 TemplateParameter *tp = (TemplateParameter *)tempdecl->parameters->data[i]; | |
3893 assert(tp); | |
3894 TemplateValueParameter *tvp = tp->isTemplateValueParameter(); | |
3895 if (tvp) | |
3896 { | |
3897 assert(ea); | |
3898 ea = ea->castTo(tvp->valType); | |
3899 ea = ea->optimize(WANTvalue | WANTinterpret); | |
3900 tiargs->data[i] = (Object *)ea; | |
3901 } | |
3902 } | |
3903 #endif | |
3904 | |
3905 #if LOG | |
3906 printf("\tIt's a match with template declaration '%s'\n", tempdecl->toChars()); | |
3907 #endif | |
3908 return tempdecl; | |
3909 } | |
3910 | |
3911 | |
3912 /***************************************** | |
3913 * Determines if a TemplateInstance will need a nested | |
3914 * generation of the TemplateDeclaration. | |
3915 */ | |
3916 | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1089
diff
changeset
|
3917 int TemplateInstance::hasNestedArgs(Objects *args) |
336 | 3918 { int nested = 0; |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1089
diff
changeset
|
3919 //printf("TemplateInstance::hasNestedArgs('%s')\n", tempdecl->ident->toChars()); |
336 | 3920 |
3921 /* A nested instance happens when an argument references a local | |
3922 * symbol that is on the stack. | |
3923 */ | |
3924 for (size_t i = 0; i < args->dim; i++) | |
3925 { Object *o = (Object *)args->data[i]; | |
3926 Expression *ea = isExpression(o); | |
3927 Dsymbol *sa = isDsymbol(o); | |
3928 Tuple *va = isTuple(o); | |
3929 if (ea) | |
3930 { | |
3931 if (ea->op == TOKvar) | |
3932 { | |
3933 sa = ((VarExp *)ea)->var; | |
3934 goto Lsa; | |
3935 } | |
3936 if (ea->op == TOKfunction) | |
3937 { | |
3938 sa = ((FuncExp *)ea)->fd; | |
3939 goto Lsa; | |
3940 } | |
3941 } | |
3942 else if (sa) | |
3943 { | |
3944 Lsa: | |
3945 Declaration *d = sa->isDeclaration(); | |
3946 if (d && !d->isDataseg() && | |
3947 #if DMDV2 | |
3948 !(d->storage_class & STCmanifest) && | |
3949 #endif | |
3950 (!d->isFuncDeclaration() || d->isFuncDeclaration()->isNested()) && | |
3951 !isTemplateMixin()) | |
3952 { | |
3953 // if module level template | |
3954 if (tempdecl->toParent()->isModule()) | |
3955 { Dsymbol *dparent = d->toParent(); | |
3956 if (!isnested) | |
3957 isnested = dparent; | |
3958 else if (isnested != dparent) | |
3959 { | |
3960 /* Select the more deeply nested of the two. | |
3961 * Error if one is not nested inside the other. | |
3962 */ | |
3963 for (Dsymbol *p = isnested; p; p = p->parent) | |
3964 { | |
3965 if (p == dparent) | |
3966 goto L1; // isnested is most nested | |
3967 } | |
876
27a379f288bf
Merged DMD 1.039
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
875
diff
changeset
|
3968 for (Dsymbol *p = dparent; p; p = p->parent) |
336 | 3969 { |
3970 if (p == isnested) | |
3971 { isnested = dparent; | |
3972 goto L1; // dparent is most nested | |
3973 } | |
3974 } | |
876
27a379f288bf
Merged DMD 1.039
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
875
diff
changeset
|
3975 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
|
3976 toChars(), isnested->toChars(), dparent->toChars()); |
336 | 3977 } |
3978 L1: | |
3979 //printf("\tnested inside %s\n", isnested->toChars()); | |
3980 nested |= 1; | |
3981 } | |
3982 else | |
1195
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1174
diff
changeset
|
3983 error("cannot use local '%s' as parameter to non-global template %s", d->toChars(), tempdecl->toChars()); |
336 | 3984 } |
3985 } | |
3986 else if (va) | |
3987 { | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1089
diff
changeset
|
3988 nested |= hasNestedArgs(&va->objects); |
336 | 3989 } |
3990 } | |
3991 return nested; | |
3992 } | |
3993 | |
3994 /**************************************** | |
3995 * This instance needs an identifier for name mangling purposes. | |
3996 * Create one by taking the template declaration name and adding | |
3997 * the type signature for it. | |
3998 */ | |
3999 | |
4000 Identifier *TemplateInstance::genIdent() | |
4001 { OutBuffer buf; | |
4002 | |
4003 //printf("TemplateInstance::genIdent('%s')\n", tempdecl->ident->toChars()); | |
1615
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
4004 char *id = tempdecl->ident->toChars(); |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1089
diff
changeset
|
4005 buf.printf("__T%zu%s", strlen(id), id); |
1615
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
4006 Objects *args = tiargs; |
336 | 4007 for (int i = 0; i < args->dim; i++) |
4008 { Object *o = (Object *)args->data[i]; | |
4009 Type *ta = isType(o); | |
4010 Expression *ea = isExpression(o); | |
4011 Dsymbol *sa = isDsymbol(o); | |
4012 Tuple *va = isTuple(o); | |
1615
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
4013 //printf("\to [%d] %p ta %p ea %p sa %p va %p\n", i, o, ta, ea, sa, va); |
336 | 4014 if (ta) |
4015 { | |
4016 buf.writeByte('T'); | |
4017 if (ta->deco) | |
4018 buf.writestring(ta->deco); | |
4019 else | |
4020 { | |
4021 #ifdef DEBUG | |
4022 printf("ta = %d, %s\n", ta->ty, ta->toChars()); | |
4023 #endif | |
4024 assert(global.errors); | |
4025 } | |
4026 } | |
4027 else if (ea) | |
4028 { sinteger_t v; | |
4029 real_t r; | |
4030 | |
1587 | 4031 ea = ea->optimize(WANTvalue | WANTinterpret); |
336 | 4032 if (ea->op == TOKvar) |
4033 { | |
4034 sa = ((VarExp *)ea)->var; | |
4035 ea = NULL; | |
4036 goto Lsa; | |
4037 } | |
4038 if (ea->op == TOKfunction) | |
4039 { | |
4040 sa = ((FuncExp *)ea)->fd; | |
4041 ea = NULL; | |
4042 goto Lsa; | |
4043 } | |
4044 buf.writeByte('V'); | |
4045 if (ea->op == TOKtuple) | |
4046 { ea->error("tuple is not a valid template value argument"); | |
4047 continue; | |
4048 } | |
4049 #if 1 | |
4050 /* Use deco that matches what it would be for a function parameter | |
4051 */ | |
4052 buf.writestring(ea->type->deco); | |
4053 #else | |
4054 // Use type of parameter, not type of argument | |
4055 TemplateParameter *tp = (TemplateParameter *)tempdecl->parameters->data[i]; | |
4056 assert(tp); | |
4057 TemplateValueParameter *tvp = tp->isTemplateValueParameter(); | |
4058 assert(tvp); | |
4059 buf.writestring(tvp->valType->deco); | |
4060 #endif | |
4061 ea->toMangleBuffer(&buf); | |
4062 } | |
4063 else if (sa) | |
4064 { | |
4065 Lsa: | |
4066 buf.writeByte('S'); | |
4067 Declaration *d = sa->isDeclaration(); | |
1367
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1195
diff
changeset
|
4068 if (d && (!d->type || !d->type->deco)) |
336 | 4069 error("forward reference of %s", d->toChars()); |
4070 else | |
4071 { | |
4072 char *p = sa->mangle(); | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1089
diff
changeset
|
4073 buf.printf("%zu%s", strlen(p), p); |
336 | 4074 } |
4075 } | |
4076 else if (va) | |
4077 { | |
4078 assert(i + 1 == args->dim); // must be last one | |
4079 args = &va->objects; | |
4080 i = -1; | |
4081 } | |
4082 else | |
4083 assert(0); | |
4084 } | |
4085 buf.writeByte('Z'); | |
4086 id = buf.toChars(); | |
4087 buf.data = NULL; | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
4088 //printf("\tgenIdent = %s\n", id); |
336 | 4089 return new Identifier(id, TOKidentifier); |
4090 } | |
4091 | |
4092 | |
4093 /**************************************************** | |
4094 * Declare parameters of template instance, initialize them with the | |
4095 * template instance arguments. | |
4096 */ | |
4097 | |
1587 | 4098 void TemplateInstance::declareParameters(Scope *sc) |
336 | 4099 { |
4100 //printf("TemplateInstance::declareParameters()\n"); | |
4101 for (int i = 0; i < tdtypes.dim; i++) | |
4102 { | |
4103 TemplateParameter *tp = (TemplateParameter *)tempdecl->parameters->data[i]; | |
4104 //Object *o = (Object *)tiargs->data[i]; | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
4105 Object *o = (Object *)tdtypes.data[i]; // initializer for tp |
336 | 4106 |
4107 //printf("\ttdtypes[%d] = %p\n", i, o); | |
1587 | 4108 tempdecl->declareParameter(sc, tp, o); |
336 | 4109 } |
4110 } | |
4111 | |
4112 | |
1609
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
4113 /***************************************************** |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
4114 * Determine if template instance is really a template function, |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
4115 * 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
|
4116 * arguments. |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
4117 */ |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
4118 |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
4119 int TemplateInstance::needsTypeInference(Scope *sc) |
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 //printf("TemplateInstance::needsTypeInference() %s\n", toChars()); |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
4122 if (!tempdecl) |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
4123 tempdecl = findTemplateDeclaration(sc); |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
4124 for (TemplateDeclaration *td = tempdecl; td; td = td->overnext) |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
4125 { |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
4126 /* If any of the overloaded template declarations need inference, |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
4127 * then return TRUE |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
4128 */ |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
4129 FuncDeclaration *fd; |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
4130 if (!td->onemember || |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
4131 (fd = td->onemember->toAlias()->isFuncDeclaration()) == NULL || |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
4132 fd->type->ty != Tfunction) |
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 /* Not a template function, therefore type inference is not possible. |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
4135 */ |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
4136 //printf("false\n"); |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
4137 return FALSE; |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
4138 } |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
4139 |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
4140 /* 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
|
4141 * to instantiate the template. |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
4142 */ |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
4143 TemplateTupleParameter *tp = td->isVariadic(); |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
4144 //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
|
4145 TypeFunction *fdtype = (TypeFunction *)fd->type; |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
4146 if (Parameter::dim(fdtype->parameters) && |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
4147 (tp || tiargs->dim < td->parameters->dim)) |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
4148 return TRUE; |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
4149 } |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
4150 //printf("false\n"); |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
4151 return FALSE; |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
4152 } |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
4153 |
336 | 4154 void TemplateInstance::semantic2(Scope *sc) |
4155 { int i; | |
4156 | |
1587 | 4157 if (semanticRun >= 2) |
336 | 4158 return; |
1587 | 4159 semanticRun = 2; |
336 | 4160 #if LOG |
4161 printf("+TemplateInstance::semantic2('%s')\n", toChars()); | |
4162 #endif | |
4163 if (!errors && members) | |
4164 { | |
4165 sc = tempdecl->scope; | |
4166 assert(sc); | |
4167 sc = sc->push(argsym); | |
4168 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
|
4169 sc->tinst = this; |
336 | 4170 for (i = 0; i < members->dim; i++) |
4171 { | |
4172 Dsymbol *s = (Dsymbol *)members->data[i]; | |
4173 #if LOG | |
4174 printf("\tmember '%s', kind = '%s'\n", s->toChars(), s->kind()); | |
4175 #endif | |
4176 s->semantic2(sc); | |
4177 } | |
4178 sc = sc->pop(); | |
4179 sc->pop(); | |
4180 } | |
4181 #if LOG | |
4182 printf("-TemplateInstance::semantic2('%s')\n", toChars()); | |
4183 #endif | |
4184 } | |
4185 | |
4186 void TemplateInstance::semantic3(Scope *sc) | |
4187 { | |
4188 #if LOG | |
1587 | 4189 printf("TemplateInstance::semantic3('%s'), semanticRun = %d\n", toChars(), semanticRun); |
336 | 4190 #endif |
4191 //if (toChars()[0] == 'D') *(char*)0=0; | |
1587 | 4192 if (semanticRun >= 3) |
336 | 4193 return; |
1587 | 4194 semanticRun = 3; |
336 | 4195 if (!errors && members) |
4196 { | |
4197 sc = tempdecl->scope; | |
4198 sc = sc->push(argsym); | |
4199 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
|
4200 sc->tinst = this; |
336 | 4201 for (int i = 0; i < members->dim; i++) |
4202 { | |
4203 Dsymbol *s = (Dsymbol *)members->data[i]; | |
4204 s->semantic3(sc); | |
4205 } | |
4206 sc = sc->pop(); | |
4207 sc->pop(); | |
4208 } | |
4209 } | |
4210 | |
1147
dbe4af57b240
Changed use of toObjFile to a new codegen method.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1146
diff
changeset
|
4211 #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
|
4212 |
1615
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
4213 /************************************** |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
4214 * Given an error instantiating the TemplateInstance, |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
4215 * give the nested TemplateInstance instantiations that got |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
4216 * us here. Those are a list threaded into the nested scopes. |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
4217 */ |
1367
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1195
diff
changeset
|
4218 void TemplateInstance::printInstantiationTrace() |
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1195
diff
changeset
|
4219 { |
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1195
diff
changeset
|
4220 if (global.gag) |
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1195
diff
changeset
|
4221 return; |
1615
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
4222 |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
4223 const int max_shown = 6; |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
4224 const char format[] = "%s: instantiated from here: %s\n"; |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
4225 |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
4226 // determine instantiation depth and number of recursive instantiations |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
4227 int n_instantiations = 1; |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
4228 int n_totalrecursions = 0; |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
4229 for (TemplateInstance *cur = this; cur; cur = cur->tinst) |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
4230 { |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
4231 ++n_instantiations; |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
4232 // If two instantiations use the same declaration, they are recursive. |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
4233 // (this works even if they are instantiated from different places in the |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
4234 // same template). |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
4235 // In principle, we could also check for multiple-template recursion, but it's |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
4236 // probably not worthwhile. |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
4237 if (cur->tinst && cur->tempdecl && cur->tinst->tempdecl |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
4238 && cur->tempdecl->loc.equals(cur->tinst->tempdecl->loc)) |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
4239 ++n_totalrecursions; |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
4240 } |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
4241 |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
4242 // show full trace only if it's short or verbose is on |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
4243 if (n_instantiations <= max_shown || global.params.verbose) |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
4244 { |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
4245 for (TemplateInstance *cur = this; cur; cur = cur->tinst) |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
4246 { |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
4247 fprintf(stdmsg, format, cur->loc.toChars(), cur->toChars()); |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
4248 } |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
4249 } |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
4250 else if (n_instantiations - n_totalrecursions <= max_shown) |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
4251 { |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
4252 // By collapsing recursive instantiations into a single line, |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
4253 // we can stay under the limit. |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
4254 int recursionDepth=0; |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
4255 for (TemplateInstance *cur = this; cur; cur = cur->tinst) |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
4256 { |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
4257 if (cur->tinst && cur->tempdecl && cur->tinst->tempdecl |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
4258 && cur->tempdecl->loc.equals(cur->tinst->tempdecl->loc)) |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
4259 { |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
4260 ++recursionDepth; |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
4261 } |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
4262 else |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
4263 { |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
4264 if (recursionDepth) |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
4265 fprintf(stdmsg, "%s: %d recursive instantiations from here: %s\n", cur->loc.toChars(), recursionDepth+2, cur->toChars()); |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
4266 else |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
4267 fprintf(stdmsg,format, cur->loc.toChars(), cur->toChars()); |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
4268 recursionDepth = 0; |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
4269 } |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
4270 } |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
4271 } |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
4272 else |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
4273 { |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
4274 // Even after collapsing the recursions, the depth is too deep. |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
4275 // Just display the first few and last few instantiations. |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
4276 size_t i = 0; |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
4277 for (TemplateInstance *cur = this; cur; cur = cur->tinst) |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
4278 { |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
4279 if (i == max_shown / 2) |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
4280 fprintf(stdmsg," ... (%d instantiations, -v to show) ...\n", n_instantiations - max_shown); |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
4281 |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
4282 if (i < max_shown / 2 || |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
4283 i >= n_instantiations - max_shown + max_shown / 2) |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
4284 fprintf(stdmsg, format, cur->loc.toChars(), cur->toChars()); |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
4285 ++i; |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
4286 } |
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
4287 } |
1367
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1195
diff
changeset
|
4288 } |
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1195
diff
changeset
|
4289 |
336 | 4290 void TemplateInstance::toObjFile(int multiobj) |
4291 { | |
4292 #if LOG | |
4293 printf("TemplateInstance::toObjFile('%s', this = %p)\n", toChars(), this); | |
4294 #endif | |
4295 if (!errors && members) | |
4296 { | |
4297 if (multiobj) | |
4298 // Append to list of object files to be written later | |
4299 //obj_append(this); | |
4300 assert(0 && "multiobj"); | |
4301 else | |
4302 { | |
4303 for (int i = 0; i < members->dim; i++) | |
4304 { | |
4305 Dsymbol *s = (Dsymbol *)members->data[i]; | |
4306 s->toObjFile(multiobj); | |
4307 } | |
4308 } | |
4309 } | |
4310 } | |
4311 | |
1147
dbe4af57b240
Changed use of toObjFile to a new codegen method.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1146
diff
changeset
|
4312 #endif |
dbe4af57b240
Changed use of toObjFile to a new codegen method.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1146
diff
changeset
|
4313 |
336 | 4314 void TemplateInstance::inlineScan() |
4315 { | |
4316 #if LOG | |
4317 printf("TemplateInstance::inlineScan('%s')\n", toChars()); | |
4318 #endif | |
4319 if (!errors && members) | |
4320 { | |
4321 for (int i = 0; i < members->dim; i++) | |
4322 { | |
4323 Dsymbol *s = (Dsymbol *)members->data[i]; | |
4324 s->inlineScan(); | |
4325 } | |
4326 } | |
4327 } | |
4328 | |
4329 void TemplateInstance::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
4330 { | |
4331 int i; | |
4332 | |
4333 Identifier *id = name; | |
4334 buf->writestring(id->toChars()); | |
4335 buf->writestring("!("); | |
4336 if (nest) | |
4337 buf->writestring("..."); | |
4338 else | |
4339 { | |
4340 nest++; | |
4341 Objects *args = tiargs; | |
4342 for (i = 0; i < args->dim; i++) | |
4343 { | |
4344 if (i) | |
4345 buf->writeByte(','); | |
4346 Object *oarg = (Object *)args->data[i]; | |
4347 ObjectToCBuffer(buf, hgs, oarg); | |
4348 } | |
4349 nest--; | |
4350 } | |
4351 buf->writeByte(')'); | |
4352 } | |
4353 | |
4354 | |
4355 Dsymbol *TemplateInstance::toAlias() | |
4356 { | |
4357 #if LOG | |
4358 printf("TemplateInstance::toAlias()\n"); | |
4359 #endif | |
4360 if (!inst) | |
4361 { error("cannot resolve forward reference"); | |
1587 | 4362 errors = 1; |
336 | 4363 return this; |
4364 } | |
4365 | |
4366 if (inst != this) | |
4367 return inst->toAlias(); | |
4368 | |
4369 if (aliasdecl) | |
1615
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
4370 { |
336 | 4371 return aliasdecl->toAlias(); |
1615
3da302cc4966
Merge DMD r294: bugzilla 2816 Sudden-death static assert is not...
Leandro Lucarella <llucax@gmail.com>
parents:
1610
diff
changeset
|
4372 } |
336 | 4373 |
4374 return inst; | |
4375 } | |
4376 | |
4377 AliasDeclaration *TemplateInstance::isAliasDeclaration() | |
4378 { | |
4379 return aliasdecl; | |
4380 } | |
4381 | |
4382 const char *TemplateInstance::kind() | |
4383 { | |
4384 return "template instance"; | |
4385 } | |
4386 | |
4387 int TemplateInstance::oneMember(Dsymbol **ps) | |
4388 { | |
4389 *ps = NULL; | |
4390 return TRUE; | |
4391 } | |
4392 | |
4393 char *TemplateInstance::toChars() | |
4394 { | |
4395 OutBuffer buf; | |
4396 HdrGenState hgs; | |
4397 char *s; | |
4398 | |
4399 toCBuffer(&buf, &hgs); | |
4400 s = buf.toChars(); | |
4401 buf.data = NULL; | |
4402 return s; | |
4403 } | |
4404 | |
1367
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1195
diff
changeset
|
4405 #if IN_LLVM |
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1195
diff
changeset
|
4406 |
561
d4e95db0e62b
Introducing template instantiation traces for static asserts and errors within templates.
Christian Kamm <kamm incasoftware de>
parents:
527
diff
changeset
|
4407 void TemplateInstance::printInstantiationTrace() |
d4e95db0e62b
Introducing template instantiation traces for static asserts and errors within templates.
Christian Kamm <kamm incasoftware de>
parents:
527
diff
changeset
|
4408 { |
d4e95db0e62b
Introducing template instantiation traces for static asserts and errors within templates.
Christian Kamm <kamm incasoftware de>
parents:
527
diff
changeset
|
4409 if(global.gag) |
d4e95db0e62b
Introducing template instantiation traces for static asserts and errors within templates.
Christian Kamm <kamm incasoftware de>
parents:
527
diff
changeset
|
4410 return; |
d4e95db0e62b
Introducing template instantiation traces for static asserts and errors within templates.
Christian Kamm <kamm incasoftware de>
parents:
527
diff
changeset
|
4411 |
d4e95db0e62b
Introducing template instantiation traces for static asserts and errors within templates.
Christian Kamm <kamm incasoftware de>
parents:
527
diff
changeset
|
4412 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
|
4413 |
d4e95db0e62b
Introducing template instantiation traces for static asserts and errors within templates.
Christian Kamm <kamm incasoftware de>
parents:
527
diff
changeset
|
4414 // determine instantiation depth |
d4e95db0e62b
Introducing template instantiation traces for static asserts and errors within templates.
Christian Kamm <kamm incasoftware de>
parents:
527
diff
changeset
|
4415 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
|
4416 TemplateInstance* cur = this; |
d4e95db0e62b
Introducing template instantiation traces for static asserts and errors within templates.
Christian Kamm <kamm incasoftware de>
parents:
527
diff
changeset
|
4417 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
|
4418 ++n_instantiations; |
d4e95db0e62b
Introducing template instantiation traces for static asserts and errors within templates.
Christian Kamm <kamm incasoftware de>
parents:
527
diff
changeset
|
4419 |
d4e95db0e62b
Introducing template instantiation traces for static asserts and errors within templates.
Christian Kamm <kamm incasoftware de>
parents:
527
diff
changeset
|
4420 // 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
|
4421 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
|
4422 { |
d4e95db0e62b
Introducing template instantiation traces for static asserts and errors within templates.
Christian Kamm <kamm incasoftware de>
parents:
527
diff
changeset
|
4423 cur = this; |
d4e95db0e62b
Introducing template instantiation traces for static asserts and errors within templates.
Christian Kamm <kamm incasoftware de>
parents:
527
diff
changeset
|
4424 while(cur) |
d4e95db0e62b
Introducing template instantiation traces for static asserts and errors within templates.
Christian Kamm <kamm incasoftware de>
parents:
527
diff
changeset
|
4425 { |
d4e95db0e62b
Introducing template instantiation traces for static asserts and errors within templates.
Christian Kamm <kamm incasoftware de>
parents:
527
diff
changeset
|
4426 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
|
4427 cur = cur->tinst; |
d4e95db0e62b
Introducing template instantiation traces for static asserts and errors within templates.
Christian Kamm <kamm incasoftware de>
parents:
527
diff
changeset
|
4428 } |
d4e95db0e62b
Introducing template instantiation traces for static asserts and errors within templates.
Christian Kamm <kamm incasoftware de>
parents:
527
diff
changeset
|
4429 } |
d4e95db0e62b
Introducing template instantiation traces for static asserts and errors within templates.
Christian Kamm <kamm incasoftware de>
parents:
527
diff
changeset
|
4430 else |
d4e95db0e62b
Introducing template instantiation traces for static asserts and errors within templates.
Christian Kamm <kamm incasoftware de>
parents:
527
diff
changeset
|
4431 { |
d4e95db0e62b
Introducing template instantiation traces for static asserts and errors within templates.
Christian Kamm <kamm incasoftware de>
parents:
527
diff
changeset
|
4432 cur = this; |
d4e95db0e62b
Introducing template instantiation traces for static asserts and errors within templates.
Christian Kamm <kamm incasoftware de>
parents:
527
diff
changeset
|
4433 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
|
4434 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
|
4435 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
|
4436 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
|
4437 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
|
4438 {} |
d4e95db0e62b
Introducing template instantiation traces for static asserts and errors within templates.
Christian Kamm <kamm incasoftware de>
parents:
527
diff
changeset
|
4439 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
|
4440 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
|
4441 } |
d4e95db0e62b
Introducing template instantiation traces for static asserts and errors within templates.
Christian Kamm <kamm incasoftware de>
parents:
527
diff
changeset
|
4442 } |
d4e95db0e62b
Introducing template instantiation traces for static asserts and errors within templates.
Christian Kamm <kamm incasoftware de>
parents:
527
diff
changeset
|
4443 |
1367
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1195
diff
changeset
|
4444 #endif |
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1195
diff
changeset
|
4445 |
336 | 4446 /* ======================== TemplateMixin ================================ */ |
4447 | |
4448 TemplateMixin::TemplateMixin(Loc loc, Identifier *ident, Type *tqual, | |
4449 Array *idents, Objects *tiargs) | |
4450 : TemplateInstance(loc, (Identifier *)idents->data[idents->dim - 1]) | |
4451 { | |
4452 //printf("TemplateMixin(ident = '%s')\n", ident ? ident->toChars() : ""); | |
4453 this->ident = ident; | |
4454 this->tqual = tqual; | |
4455 this->idents = idents; | |
4456 this->tiargs = tiargs ? tiargs : new Objects(); | |
4457 } | |
4458 | |
4459 Dsymbol *TemplateMixin::syntaxCopy(Dsymbol *s) | |
4460 { TemplateMixin *tm; | |
4461 | |
4462 Array *ids = new Array(); | |
4463 ids->setDim(idents->dim); | |
4464 for (int i = 0; i < idents->dim; i++) | |
4465 { // Matches TypeQualified::syntaxCopyHelper() | |
4466 Identifier *id = (Identifier *)idents->data[i]; | |
4467 if (id->dyncast() == DYNCAST_DSYMBOL) | |
4468 { | |
4469 TemplateInstance *ti = (TemplateInstance *)id; | |
4470 | |
4471 ti = (TemplateInstance *)ti->syntaxCopy(NULL); | |
4472 id = (Identifier *)ti; | |
4473 } | |
4474 ids->data[i] = id; | |
4475 } | |
4476 | |
4477 tm = new TemplateMixin(loc, ident, | |
4478 (Type *)(tqual ? tqual->syntaxCopy() : NULL), | |
4479 ids, tiargs); | |
4480 TemplateInstance::syntaxCopy(tm); | |
4481 return tm; | |
4482 } | |
4483 | |
4484 void TemplateMixin::semantic(Scope *sc) | |
4485 { | |
4486 #if LOG | |
4487 printf("+TemplateMixin::semantic('%s', this=%p)\n", toChars(), this); | |
4488 fflush(stdout); | |
4489 #endif | |
1587 | 4490 if (semanticRun) |
4491 { | |
336 | 4492 // This for when a class/struct contains mixin members, and |
4493 // is done over because of forward references | |
1587 | 4494 if (parent && toParent()->isAggregateDeclaration()) |
4495 semanticRun = 1; // do over | |
4496 else | |
4497 { | |
336 | 4498 #if LOG |
1587 | 4499 printf("\tsemantic done\n"); |
336 | 4500 #endif |
1587 | 4501 return; |
4502 } | |
336 | 4503 } |
1587 | 4504 if (!semanticRun) |
4505 semanticRun = 1; | |
336 | 4506 #if LOG |
4507 printf("\tdo semantic\n"); | |
4508 #endif | |
4509 | |
4510 #if !IN_LLVM | |
4511 // dont know what this is | |
4512 util_progress(); | |
4513 #endif | |
4514 | |
4515 Scope *scx = NULL; | |
4516 if (scope) | |
4517 { sc = scope; | |
4518 scx = scope; // save so we don't make redundant copies | |
4519 scope = NULL; | |
4520 } | |
4521 | |
4522 // Follow qualifications to find the TemplateDeclaration | |
4523 if (!tempdecl) | |
4524 { Dsymbol *s; | |
4525 int i; | |
4526 Identifier *id; | |
4527 | |
4528 if (tqual) | |
4529 { s = tqual->toDsymbol(sc); | |
4530 i = 0; | |
4531 } | |
4532 else | |
4533 { | |
4534 i = 1; | |
4535 id = (Identifier *)idents->data[0]; | |
4536 switch (id->dyncast()) | |
4537 { | |
4538 case DYNCAST_IDENTIFIER: | |
4539 s = sc->search(loc, id, NULL); | |
4540 break; | |
4541 | |
4542 case DYNCAST_DSYMBOL: | |
4543 { | |
4544 TemplateInstance *ti = (TemplateInstance *)id; | |
4545 ti->semantic(sc); | |
4546 s = ti; | |
4547 break; | |
4548 } | |
4549 default: | |
4550 assert(0); | |
4551 } | |
4552 } | |
4553 | |
4554 for (; i < idents->dim; i++) | |
4555 { | |
4556 if (!s) | |
4557 break; | |
4558 id = (Identifier *)idents->data[i]; | |
4559 s = s->searchX(loc, sc, id); | |
4560 } | |
4561 if (!s) | |
4562 { | |
4563 error("is not defined"); | |
4564 inst = this; | |
4565 return; | |
4566 } | |
4567 tempdecl = s->toAlias()->isTemplateDeclaration(); | |
4568 if (!tempdecl) | |
4569 { | |
4570 error("%s isn't a template", s->toChars()); | |
4571 inst = this; | |
4572 return; | |
4573 } | |
4574 } | |
4575 | |
4576 // Look for forward reference | |
4577 assert(tempdecl); | |
4578 for (TemplateDeclaration *td = tempdecl; td; td = td->overnext) | |
4579 { | |
1587 | 4580 if (!td->semanticRun) |
336 | 4581 { |
4582 /* Cannot handle forward references if mixin is a struct member, | |
4583 * because addField must happen during struct's semantic, not | |
4584 * during the mixin semantic. | |
4585 * runDeferred will re-run mixin's semantic outside of the struct's | |
4586 * semantic. | |
4587 */ | |
1587 | 4588 semanticRun = 0; |
336 | 4589 AggregateDeclaration *ad = toParent()->isAggregateDeclaration(); |
4590 if (ad) | |
4591 ad->sizeok = 2; | |
4592 else | |
4593 { | |
4594 // Forward reference | |
4595 //printf("forward reference - deferring\n"); | |
4596 scope = scx ? scx : new Scope(*sc); | |
4597 scope->setNoFree(); | |
4598 scope->module->addDeferredSemantic(this); | |
4599 } | |
4600 return; | |
4601 } | |
4602 } | |
4603 | |
4604 // Run semantic on each argument, place results in tiargs[] | |
4605 semanticTiargs(sc); | |
1587 | 4606 if (errors) |
4607 return; | |
336 | 4608 |
4609 tempdecl = findBestMatch(sc); | |
4610 if (!tempdecl) | |
4611 { inst = this; | |
4612 return; // error recovery | |
4613 } | |
4614 | |
4615 if (!ident) | |
4616 ident = genIdent(); | |
4617 | |
4618 inst = this; | |
4619 parent = sc->parent; | |
4620 | |
4621 /* Detect recursive mixin instantiations. | |
4622 */ | |
4623 for (Dsymbol *s = parent; s; s = s->parent) | |
4624 { | |
4625 //printf("\ts = '%s'\n", s->toChars()); | |
4626 TemplateMixin *tm = s->isTemplateMixin(); | |
4627 if (!tm || tempdecl != tm->tempdecl) | |
4628 continue; | |
4629 | |
4630 /* Different argument list lengths happen with variadic args | |
4631 */ | |
4632 if (tiargs->dim != tm->tiargs->dim) | |
4633 continue; | |
4634 | |
4635 for (int i = 0; i < tiargs->dim; i++) | |
4636 { Object *o = (Object *)tiargs->data[i]; | |
4637 Type *ta = isType(o); | |
4638 Expression *ea = isExpression(o); | |
4639 Dsymbol *sa = isDsymbol(o); | |
4640 Object *tmo = (Object *)tm->tiargs->data[i]; | |
4641 if (ta) | |
4642 { | |
4643 Type *tmta = isType(tmo); | |
4644 if (!tmta) | |
4645 goto Lcontinue; | |
4646 if (!ta->equals(tmta)) | |
4647 goto Lcontinue; | |
4648 } | |
4649 else if (ea) | |
4650 { Expression *tme = isExpression(tmo); | |
4651 if (!tme || !ea->equals(tme)) | |
4652 goto Lcontinue; | |
4653 } | |
4654 else if (sa) | |
4655 { | |
4656 Dsymbol *tmsa = isDsymbol(tmo); | |
4657 if (sa != tmsa) | |
4658 goto Lcontinue; | |
4659 } | |
4660 else | |
4661 assert(0); | |
4662 } | |
4663 error("recursive mixin instantiation"); | |
4664 return; | |
4665 | |
4666 Lcontinue: | |
4667 continue; | |
4668 } | |
4669 | |
4670 // Copy the syntax trees from the TemplateDeclaration | |
4671 members = Dsymbol::arraySyntaxCopy(tempdecl->members); | |
4672 if (!members) | |
4673 return; | |
4674 | |
4675 symtab = new DsymbolTable(); | |
4676 | |
4677 for (Scope *sce = sc; 1; sce = sce->enclosing) | |
4678 { | |
4679 ScopeDsymbol *sds = (ScopeDsymbol *)sce->scopesym; | |
4680 if (sds) | |
4681 { | |
4682 sds->importScope(this, PROTpublic); | |
4683 break; | |
4684 } | |
4685 } | |
4686 | |
4687 #if LOG | |
4688 printf("\tcreate scope for template parameters '%s'\n", toChars()); | |
4689 #endif | |
4690 Scope *scy = sc; | |
4691 scy = sc->push(this); | |
4692 scy->parent = this; | |
4693 | |
4694 argsym = new ScopeDsymbol(); | |
4695 argsym->parent = scy->parent; | |
1587 | 4696 Scope *argscope = scy->push(argsym); |
336 | 4697 |
4698 unsigned errorsave = global.errors; | |
4699 | |
4700 // Declare each template parameter as an alias for the argument type | |
1587 | 4701 declareParameters(argscope); |
336 | 4702 |
4703 // Add members to enclosing scope, as well as this scope | |
4704 for (unsigned i = 0; i < members->dim; i++) | |
4705 { Dsymbol *s; | |
4706 | |
4707 s = (Dsymbol *)members->data[i]; | |
1587 | 4708 s->addMember(argscope, this, i); |
336 | 4709 //sc->insert(s); |
4710 //printf("sc->parent = %p, sc->scopesym = %p\n", sc->parent, sc->scopesym); | |
4711 //printf("s->parent = %s\n", s->parent->toChars()); | |
4712 } | |
4713 | |
4714 // Do semantic() analysis on template instance members | |
4715 #if LOG | |
4716 printf("\tdo semantic() on template instance members '%s'\n", toChars()); | |
4717 #endif | |
4718 Scope *sc2; | |
1587 | 4719 sc2 = argscope->push(this); |
336 | 4720 sc2->offset = sc->offset; |
1367
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1195
diff
changeset
|
4721 |
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1195
diff
changeset
|
4722 static int nest; |
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1195
diff
changeset
|
4723 //printf("%d\n", nest); |
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1195
diff
changeset
|
4724 if (++nest > 500) |
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1195
diff
changeset
|
4725 { |
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1195
diff
changeset
|
4726 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
|
4727 error("recursive expansion"); |
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1195
diff
changeset
|
4728 fatal(); |
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1195
diff
changeset
|
4729 } |
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1195
diff
changeset
|
4730 |
336 | 4731 for (int i = 0; i < members->dim; i++) |
4732 { | |
4733 Dsymbol *s = (Dsymbol *)members->data[i]; | |
4734 s->semantic(sc2); | |
4735 } | |
1367
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1195
diff
changeset
|
4736 |
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1195
diff
changeset
|
4737 nest--; |
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1195
diff
changeset
|
4738 |
336 | 4739 sc->offset = sc2->offset; |
4740 | |
4741 /* The problem is when to parse the initializer for a variable. | |
4742 * Perhaps VarDeclaration::semantic() should do it like it does | |
4743 * for initializers inside a function. | |
4744 */ | |
4745 // if (sc->parent->isFuncDeclaration()) | |
4746 | |
4747 semantic2(sc2); | |
4748 | |
4749 if (sc->func) | |
4750 { | |
4751 semantic3(sc2); | |
4752 } | |
4753 | |
4754 // Give additional context info if error occurred during instantiation | |
4755 if (global.errors != errorsave) | |
4756 { | |
4757 error("error instantiating"); | |
4758 } | |
4759 | |
4760 sc2->pop(); | |
4761 | |
1587 | 4762 argscope->pop(); |
336 | 4763 |
4764 // if (!isAnonymous()) | |
4765 { | |
4766 scy->pop(); | |
4767 } | |
4768 #if LOG | |
4769 printf("-TemplateMixin::semantic('%s', this=%p)\n", toChars(), this); | |
4770 #endif | |
4771 } | |
4772 | |
4773 void TemplateMixin::semantic2(Scope *sc) | |
4774 { int i; | |
4775 | |
1587 | 4776 if (semanticRun >= 2) |
336 | 4777 return; |
1587 | 4778 semanticRun = 2; |
336 | 4779 #if LOG |
4780 printf("+TemplateMixin::semantic2('%s')\n", toChars()); | |
4781 #endif | |
4782 if (members) | |
4783 { | |
4784 assert(sc); | |
4785 sc = sc->push(argsym); | |
4786 sc = sc->push(this); | |
4787 for (i = 0; i < members->dim; i++) | |
4788 { | |
4789 Dsymbol *s = (Dsymbol *)members->data[i]; | |
4790 #if LOG | |
4791 printf("\tmember '%s', kind = '%s'\n", s->toChars(), s->kind()); | |
4792 #endif | |
4793 s->semantic2(sc); | |
4794 } | |
4795 sc = sc->pop(); | |
4796 sc->pop(); | |
4797 } | |
4798 #if LOG | |
4799 printf("-TemplateMixin::semantic2('%s')\n", toChars()); | |
4800 #endif | |
4801 } | |
4802 | |
4803 void TemplateMixin::semantic3(Scope *sc) | |
4804 { int i; | |
4805 | |
1587 | 4806 if (semanticRun >= 3) |
336 | 4807 return; |
1587 | 4808 semanticRun = 3; |
336 | 4809 #if LOG |
4810 printf("TemplateMixin::semantic3('%s')\n", toChars()); | |
4811 #endif | |
4812 if (members) | |
4813 { | |
4814 sc = sc->push(argsym); | |
4815 sc = sc->push(this); | |
4816 for (i = 0; i < members->dim; i++) | |
4817 { | |
4818 Dsymbol *s = (Dsymbol *)members->data[i]; | |
4819 s->semantic3(sc); | |
4820 } | |
4821 sc = sc->pop(); | |
4822 sc->pop(); | |
4823 } | |
4824 } | |
4825 | |
4826 void TemplateMixin::inlineScan() | |
4827 { | |
4828 TemplateInstance::inlineScan(); | |
4829 } | |
4830 | |
4831 const char *TemplateMixin::kind() | |
4832 { | |
4833 return "mixin"; | |
4834 } | |
4835 | |
4836 int TemplateMixin::oneMember(Dsymbol **ps) | |
4837 { | |
4838 return Dsymbol::oneMember(ps); | |
4839 } | |
4840 | |
4841 int TemplateMixin::hasPointers() | |
4842 { | |
4843 //printf("TemplateMixin::hasPointers() %s\n", toChars()); | |
4844 for (size_t i = 0; i < members->dim; i++) | |
4845 { | |
4846 Dsymbol *s = (Dsymbol *)members->data[i]; | |
4847 //printf(" s = %s %s\n", s->kind(), s->toChars()); | |
4848 if (s->hasPointers()) | |
4849 { | |
4850 return 1; | |
4851 } | |
4852 } | |
4853 return 0; | |
4854 } | |
4855 | |
4856 char *TemplateMixin::toChars() | |
4857 { | |
4858 OutBuffer buf; | |
4859 HdrGenState hgs; | |
4860 char *s; | |
4861 | |
4862 TemplateInstance::toCBuffer(&buf, &hgs); | |
4863 s = buf.toChars(); | |
4864 buf.data = NULL; | |
4865 return s; | |
4866 } | |
4867 | |
4868 void TemplateMixin::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
4869 { | |
4870 buf->writestring("mixin "); | |
4871 | |
4872 for (int i = 0; i < idents->dim; i++) | |
4873 { Identifier *id = (Identifier *)idents->data[i]; | |
4874 | |
4875 if (i) | |
4876 buf->writeByte('.'); | |
4877 buf->writestring(id->toChars()); | |
4878 } | |
4879 buf->writestring("!("); | |
4880 if (tiargs) | |
4881 { | |
4882 for (int i = 0; i < tiargs->dim; i++) | |
4883 { if (i) | |
4884 buf->writebyte(','); | |
4885 Object *oarg = (Object *)tiargs->data[i]; | |
4886 Type *t = isType(oarg); | |
4887 Expression *e = isExpression(oarg); | |
4888 Dsymbol *s = isDsymbol(oarg); | |
4889 if (t) | |
4890 t->toCBuffer(buf, NULL, hgs); | |
4891 else if (e) | |
4892 e->toCBuffer(buf, hgs); | |
4893 else if (s) | |
4894 { | |
4895 char *p = s->ident ? s->ident->toChars() : s->toChars(); | |
4896 buf->writestring(p); | |
4897 } | |
4898 else if (!oarg) | |
4899 { | |
4900 buf->writestring("NULL"); | |
4901 } | |
4902 else | |
4903 { | |
4904 assert(0); | |
4905 } | |
4906 } | |
4907 } | |
4908 buf->writebyte(')'); | |
4909 if (ident) | |
4910 { | |
4911 buf->writebyte(' '); | |
4912 buf->writestring(ident->toChars()); | |
4913 } | |
4914 buf->writebyte(';'); | |
4915 buf->writenl(); | |
4916 } | |
4917 | |
4918 | |
1147
dbe4af57b240
Changed use of toObjFile to a new codegen method.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1146
diff
changeset
|
4919 #if IN_DMD |
336 | 4920 void TemplateMixin::toObjFile(int multiobj) |
4921 { | |
4922 //printf("TemplateMixin::toObjFile('%s')\n", toChars()); | |
4923 TemplateInstance::toObjFile(multiobj); | |
4924 } | |
1174
0b26cfb2d445
Add newlines to end of file.
Christian Kamm <kamm incasoftware de>
parents:
1147
diff
changeset
|
4925 #endif |
0b26cfb2d445
Add newlines to end of file.
Christian Kamm <kamm incasoftware de>
parents:
1147
diff
changeset
|
4926 |