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