Mercurial > projects > ddmd
annotate dmd/TemplateDeclaration.d @ 72:2e2a5c3f943a
reduced warnings by adding override to the methods
think this also normalizes different line endings used all over the place
author | Trass3r |
---|---|
date | Sat, 28 Aug 2010 16:19:48 +0200 |
parents | cab4c37afb89 |
children | 7e0d548de9e6 |
rev | line source |
---|---|
0 | 1 module dmd.TemplateDeclaration; |
2 | |
3 import dmd.Loc; | |
4 import dmd.ScopeDsymbol; | |
5 import dmd.ArrayTypes; | |
6 import dmd.Dsymbol; | |
7 import dmd.STC; | |
8 import dmd.TemplateThisParameter; | |
9 import dmd.Global; | |
10 import dmd.Array; | |
11 import dmd.Identifier; | |
12 import dmd.TypeArray; | |
13 import dmd.Expression; | |
14 import dmd.Scope; | |
15 import dmd.TypeIdentifier; | |
16 import dmd.TypeDelegate; | |
17 import dmd.IntegerExp; | |
18 import dmd.TypeSArray; | |
19 import dmd.StringExp; | |
20 import dmd.TOK; | |
21 import dmd.Argument; | |
22 import dmd.CtorDeclaration; | |
23 import dmd.TypeFunction; | |
24 import dmd.TY; | |
25 import dmd.OutBuffer; | |
26 import dmd.Declaration; | |
27 import dmd.HdrGenState; | |
28 import dmd.TemplateInstance; | |
29 import dmd.WANT; | |
30 import dmd.FuncDeclaration; | |
31 import dmd.TemplateTupleParameter; | |
32 import dmd.MATCH; | |
33 import dmd.Type; | |
34 import dmd.Tuple; | |
35 import dmd.TupleDeclaration; | |
36 import dmd.Initializer; | |
37 import dmd.ExpInitializer; | |
38 import dmd.TemplateValueParameter; | |
39 import dmd.AliasDeclaration; | |
40 import dmd.VarDeclaration; | |
41 import dmd.TemplateParameter; | |
42 import dmd.TemplateTypeParameter; | |
43 | |
44 import dmd.expression.Util; | |
45 | |
46 import std.stdio; | |
47 | |
48 /************************************** | |
49 * Determine if TemplateDeclaration is variadic. | |
50 */ | |
51 | |
52 TemplateTupleParameter isVariadic(TemplateParameters parameters) | |
53 { | |
54 size_t dim = parameters.dim; | |
55 TemplateTupleParameter tp = null; | |
56 | |
57 if (dim) | |
58 tp = (cast(TemplateParameter)parameters.data[dim - 1]).isTemplateTupleParameter(); | |
59 | |
60 return tp; | |
61 } | |
62 | |
63 void ObjectToCBuffer(OutBuffer buf, HdrGenState* hgs, Object oarg) | |
64 { | |
65 //printf("ObjectToCBuffer()\n"); | |
66 Type t = isType(oarg); | |
67 Expression e = isExpression(oarg); | |
68 Dsymbol s = isDsymbol(oarg); | |
69 Tuple v = isTuple(oarg); | |
70 if (t) | |
71 { | |
72 //printf("\tt: %s ty = %d\n", t.toChars(), t.ty); | |
73 t.toCBuffer(buf, null, hgs); | |
74 } | |
75 else if (e) | |
76 e.toCBuffer(buf, hgs); | |
77 else if (s) | |
78 { | |
79 string p = s.ident ? s.ident.toChars() : s.toChars(); | |
80 buf.writestring(p); | |
81 } | |
82 else if (v) | |
83 { | |
84 Objects args = v.objects; | |
85 for (size_t i = 0; i < args.dim; i++) | |
86 { | |
87 if (i) | |
88 buf.writeByte(','); | |
89 Object o = cast(Object)args.data[i]; | |
90 ObjectToCBuffer(buf, hgs, o); | |
91 } | |
92 } | |
93 else if (!oarg) | |
94 { | |
95 buf.writestring("null"); | |
96 } | |
97 else | |
98 { | |
99 debug writef("bad Object = %p\n", oarg); | |
100 assert(0); | |
101 } | |
102 } | |
103 | |
104 class TemplateDeclaration : ScopeDsymbol | |
105 { | |
106 TemplateParameters parameters; // array of TemplateParameter's | |
107 | |
108 TemplateParameters origParameters; // originals for Ddoc | |
109 Expression constraint; | |
110 Array instances; // array of TemplateInstance's | |
111 | |
112 TemplateDeclaration overnext; // next overloaded TemplateDeclaration | |
113 TemplateDeclaration overroot; // first in overnext list | |
114 | |
115 int semanticRun; // 1 semantic() run | |
116 | |
117 Dsymbol onemember; // if !=NULL then one member of this template | |
118 | |
119 int literal; // this template declaration is a literal | |
120 | |
121 this(Loc loc, Identifier id, TemplateParameters parameters, Expression constraint, Array decldefs) | |
122 { | |
123 super(id); | |
124 | |
125 version (LOG) { | |
126 printf("TemplateDeclaration(this = %p, id = '%s')\n", this, id.toChars()); | |
127 } | |
128 static if (false) { | |
129 if (parameters) | |
130 for (int i = 0; i < parameters.dim; i++) | |
131 { | |
132 TemplateParameter tp = cast(TemplateParameter)parameters.data[i]; | |
133 //printf("\tparameter[%d] = %p\n", i, tp); | |
134 TemplateTypeParameter ttp = tp.isTemplateTypeParameter(); | |
135 | |
136 if (ttp) | |
137 { | |
138 printf("\tparameter[%d] = %s : %s\n", i, tp.ident.toChars(), ttp.specType ? ttp.specType.toChars() : ""); | |
139 } | |
140 } | |
141 } | |
142 | |
143 this.loc = loc; | |
144 this.parameters = parameters; | |
145 this.origParameters = parameters; | |
146 this.constraint = constraint; | |
147 this.members = decldefs; | |
148 | |
149 instances = new Array(); | |
150 } | |
151 | |
72 | 152 override Dsymbol syntaxCopy(Dsymbol) |
0 | 153 { |
51 | 154 //printf("TemplateDeclaration.syntaxCopy()\n"); |
155 TemplateDeclaration td; | |
156 TemplateParameters p; | |
157 Array d; | |
158 | |
159 p = null; | |
160 if (parameters) | |
161 { | |
162 p = new TemplateParameters(); | |
163 p.setDim(parameters.dim); | |
164 for (int i = 0; i < p.dim; i++) | |
165 { | |
166 TemplateParameter tp = cast(TemplateParameter)parameters.data[i]; | |
167 p.data[i] = cast(void*)tp.syntaxCopy(); | |
168 } | |
169 } | |
170 | |
171 Expression e = null; | |
172 if (constraint) | |
173 e = constraint.syntaxCopy(); | |
174 d = Dsymbol.arraySyntaxCopy(members); | |
175 td = new TemplateDeclaration(loc, ident, p, e, d); | |
176 return td; | |
0 | 177 } |
178 | |
72 | 179 override void semantic(Scope sc) |
0 | 180 { |
181 version (LOG) { | |
182 printf("TemplateDeclaration.semantic(this = %p, id = '%s')\n", this, ident.toChars()); | |
183 } | |
184 if (semanticRun) | |
185 return; // semantic() already run | |
186 semanticRun = 1; | |
187 | |
188 if (sc.func) | |
189 { | |
190 version (DMDV1) { | |
191 error("cannot declare template at function scope %s", sc.func.toChars()); | |
192 } | |
193 } | |
194 | |
195 if (/*global.params.useArrayBounds &&*/ sc.module_) | |
196 { | |
197 // Generate this function as it may be used | |
198 // when template is instantiated in other modules | |
199 sc.module_.toModuleArray(); | |
200 } | |
201 | |
202 if (/*global.params.useAssert &&*/ sc.module_) | |
203 { | |
204 // Generate this function as it may be used | |
205 // when template is instantiated in other modules | |
206 sc.module_.toModuleAssert(); | |
207 } | |
208 | |
209 /* Remember Scope for later instantiations, but make | |
210 * a copy since attributes can change. | |
211 */ | |
212 this.scope_ = new Scope(sc); /// A light copy | |
213 this.scope_.setNoFree(); | |
214 | |
215 // Set up scope for parameters | |
216 ScopeDsymbol paramsym = new ScopeDsymbol(); | |
217 paramsym.parent = sc.parent; | |
218 Scope paramscope = sc.push(paramsym); | |
219 paramscope.parameterSpecialization = 1; | |
220 paramscope.stc = STCundefined; | |
221 | |
222 if (!parent) | |
223 parent = sc.parent; | |
224 | |
225 if (global.params.doDocComments) | |
226 { | |
227 origParameters = new TemplateParameters(); | |
228 origParameters.setDim(parameters.dim); | |
229 for (int i = 0; i < parameters.dim; i++) | |
230 { | |
231 TemplateParameter tp = cast(TemplateParameter)parameters.data[i]; | |
232 origParameters.data[i] = cast(void*)tp.syntaxCopy(); | |
233 } | |
234 } | |
235 | |
236 for (int i = 0; i < parameters.dim; i++) | |
237 { | |
238 TemplateParameter tp = cast(TemplateParameter)parameters.data[i]; | |
239 tp.declareParameter(paramscope); | |
240 } | |
241 | |
242 for (int i = 0; i < parameters.dim; i++) | |
243 { | |
244 TemplateParameter tp = cast(TemplateParameter)parameters.data[i]; | |
245 | |
246 tp.semantic(paramscope); | |
247 if (i + 1 != parameters.dim && tp.isTemplateTupleParameter()) | |
248 error("template tuple parameter must be last one"); | |
249 } | |
250 | |
251 paramscope.pop(); | |
252 | |
253 if (members) | |
254 { | |
255 Dsymbol s; | |
256 if (Dsymbol.oneMembers(members, &s)) | |
257 { | |
258 if (s && s.ident && s.ident.equals(ident)) | |
259 { | |
260 onemember = s; | |
261 s.parent = this; | |
262 } | |
263 } | |
264 } | |
265 | |
266 /* BUG: should check: | |
267 * o no virtual functions or non-static data members of classes | |
268 */ | |
269 } | |
270 | |
8
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
271 /********************************** |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
272 * Overload existing TemplateDeclaration 'this' with the new one 's'. |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
273 * Return !=0 if successful; i.e. no conflict. |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
274 */ |
72 | 275 override bool overloadInsert(Dsymbol s) |
0 | 276 { |
50 | 277 TemplateDeclaration *pf; |
8
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
278 TemplateDeclaration f; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
279 |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
280 version (LOG) { |
50 | 281 printf("TemplateDeclaration.overloadInsert('%.*s')\n", s.toChars()); |
8
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
282 } |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
283 f = s.isTemplateDeclaration(); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
284 if (!f) |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
285 return false; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
286 |
50 | 287 TemplateDeclaration pthis = this; |
288 for (pf = &pthis; *pf; pf = &(*pf).overnext) | |
8
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
289 { |
50 | 290 static if (false) { |
291 // Conflict if TemplateParameter's match | |
292 // Will get caught anyway later with TemplateInstance, but | |
293 // should check it now. | |
294 if (f.parameters.dim != f2.parameters.dim) | |
295 goto Lcontinue; | |
8
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
296 |
50 | 297 for (int i = 0; i < f.parameters.dim; i++) |
298 { | |
299 TemplateParameter p1 = cast(TemplateParameter)f.parameters.data[i]; | |
300 TemplateParameter p2 = cast(TemplateParameter)f2.parameters.data[i]; | |
8
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
301 |
50 | 302 if (!p1.overloadMatch(p2)) |
303 goto Lcontinue; | |
304 } | |
8
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
305 |
50 | 306 version (LOG) { |
307 printf("\tfalse: conflict\n"); | |
308 } | |
309 return false; | |
8
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
310 |
50 | 311 Lcontinue: |
8
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
312 ; |
50 | 313 } |
8
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
314 } |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
315 |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
316 f.overroot = this; |
50 | 317 *pf = f; |
8
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
318 version (LOG) { |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
319 printf("\ttrue: no conflict\n"); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
320 } |
50 | 321 |
8
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
322 return true; |
0 | 323 } |
324 | |
72 | 325 override void toCBuffer(OutBuffer buf, HdrGenState* hgs) |
0 | 326 { |
327 assert(false); | |
328 } | |
329 | |
72 | 330 override string kind() |
0 | 331 { |
50 | 332 return (onemember && onemember.isAggregateDeclaration()) |
333 ? onemember.kind() | |
334 : "template"; | |
0 | 335 } |
336 | |
72 | 337 override string toChars() |
0 | 338 { |
49 | 339 OutBuffer buf = new OutBuffer(); |
340 HdrGenState hgs; | |
341 | |
342 /// memset(&hgs, 0, hgs.sizeof); | |
343 buf.writestring(ident.toChars()); | |
344 buf.writeByte('('); | |
345 for (int i = 0; i < parameters.dim; i++) | |
346 { | |
347 TemplateParameter tp = cast(TemplateParameter)parameters.data[i]; | |
348 if (i) | |
349 buf.writeByte(','); | |
350 tp.toCBuffer(buf, &hgs); | |
351 } | |
352 buf.writeByte(')'); | |
353 version (DMDV2) { | |
354 if (constraint) | |
355 { | |
356 buf.writestring(" if ("); | |
357 constraint.toCBuffer(buf, &hgs); | |
358 buf.writeByte(')'); | |
359 } | |
360 } | |
361 buf.writeByte(0); | |
362 return buf.extractString(); | |
0 | 363 } |
364 | |
72 | 365 override void emitComment(Scope sc) |
0 | 366 { |
367 assert(false); | |
368 } | |
50 | 369 |
0 | 370 // void toDocBuffer(OutBuffer *buf); |
371 | |
372 /*************************************** | |
373 * Given that ti is an instance of this TemplateDeclaration, | |
374 * deduce the types of the parameters to this, and store | |
375 * those deduced types in dedtypes[]. | |
376 * Input: | |
377 * flag 1: don't do semantic() because of dummy types | |
378 * 2: don't change types in matchArg() | |
379 * Output: | |
380 * dedtypes deduced arguments | |
381 * Return match level. | |
382 */ | |
383 MATCH matchWithInstance(TemplateInstance ti, Objects dedtypes, int flag) | |
384 { | |
385 MATCH m; | |
386 int dedtypes_dim = dedtypes.dim; | |
387 | |
388 version (LOGM) { | |
50 | 389 printf("\n+TemplateDeclaration.matchWithInstance(this = %.*s, ti = %.*s, flag = %d)\n", toChars(), ti.toChars(), flag); |
0 | 390 } |
391 | |
392 static if (false) { | |
393 printf("dedtypes.dim = %d, parameters.dim = %d\n", dedtypes_dim, parameters.dim); | |
394 if (ti.tiargs.dim) | |
395 printf("ti.tiargs.dim = %d, [0] = %p\n", ti.tiargs.dim, ti.tiargs.data[0]); | |
396 } | |
397 dedtypes.zero(); | |
398 | |
399 int parameters_dim = parameters.dim; | |
400 int variadic = isVariadic() !is null; | |
401 | |
402 // If more arguments than parameters, no match | |
403 if (ti.tiargs.dim > parameters_dim && !variadic) | |
404 { | |
405 version (LOGM) { | |
50 | 406 printf(" no match: more arguments than parameters\n"); |
0 | 407 } |
50 | 408 return MATCHnomatch; |
0 | 409 } |
410 | |
411 assert(dedtypes_dim == parameters_dim); | |
412 assert(dedtypes_dim >= ti.tiargs.dim || variadic); | |
413 | |
414 // Set up scope for parameters | |
415 assert(cast(size_t)cast(void*)scope_ > 0x10000); | |
416 ScopeDsymbol paramsym = new ScopeDsymbol(); | |
417 paramsym.parent = scope_.parent; | |
418 Scope paramscope = scope_.push(paramsym); | |
419 paramscope.stc = STCundefined; | |
420 | |
421 // Attempt type deduction | |
422 m = MATCHexact; | |
423 for (int i = 0; i < dedtypes_dim; i++) | |
424 { | |
425 MATCH m2; | |
426 TemplateParameter tp = cast(TemplateParameter)parameters.data[i]; | |
427 Declaration sparam; | |
428 | |
429 //printf("\targument [%d]\n", i); | |
430 version (LOGM) { | |
431 //printf("\targument [%d] is %s\n", i, oarg ? oarg.toChars() : "null"); | |
432 TemplateTypeParameter *ttp = tp.isTemplateTypeParameter(); | |
433 if (ttp) | |
434 printf("\tparameter[%d] is %s : %s\n", i, tp.ident.toChars(), ttp.specType ? ttp.specType.toChars() : ""); | |
435 } | |
436 | |
437 version (DMDV1) { | |
438 m2 = tp.matchArg(paramscope, ti.tiargs, i, parameters, dedtypes, &sparam); | |
439 } else { | |
440 m2 = tp.matchArg(paramscope, ti.tiargs, i, parameters, dedtypes, &sparam, (flag & 2) ? 1 : 0); | |
441 } | |
442 //printf("\tm2 = %d\n", m2); | |
443 | |
444 if (m2 == MATCHnomatch) | |
445 { | |
446 static if (false) { | |
447 printf("\tmatchArg() for parameter %i failed\n", i); | |
448 } | |
449 goto Lnomatch; | |
450 } | |
451 | |
452 if (m2 < m) | |
453 m = m2; | |
454 | |
455 if (!flag) | |
456 sparam.semantic(paramscope); | |
457 if (!paramscope.insert(sparam)) | |
458 goto Lnomatch; | |
459 } | |
460 | |
461 if (!flag) | |
462 { | |
463 /* Any parameter left without a type gets the type of | |
464 * its corresponding arg | |
465 */ | |
466 for (int i = 0; i < dedtypes_dim; i++) | |
467 { | |
468 if (!dedtypes.data[i]) | |
469 { | |
470 assert(i < ti.tiargs.dim); | |
471 dedtypes.data[i] = ti.tiargs.data[i]; | |
472 } | |
473 } | |
474 } | |
475 | |
476 version (DMDV2) { | |
477 if (m && constraint && !(flag & 1)) | |
478 { /* Check to see if constraint is satisfied. | |
479 */ | |
480 Expression e = constraint.syntaxCopy(); | |
481 paramscope.flags |= SCOPE.SCOPEstaticif; | |
482 e = e.semantic(paramscope); | |
483 e = e.optimize(WANTvalue | WANTinterpret); | |
484 if (e.isBool(true)) { | |
485 ; | |
486 } else if (e.isBool(false)) | |
487 goto Lnomatch; | |
488 else | |
489 { | |
490 e.error("constraint %s is not constant or does not evaluate to a bool", e.toChars()); | |
491 } | |
492 } | |
493 } | |
494 | |
495 version (LOGM) { | |
496 // Print out the results | |
497 printf("--------------------------\n"); | |
498 printf("template %s\n", toChars()); | |
499 printf("instance %s\n", ti.toChars()); | |
500 if (m) | |
501 { | |
502 for (int i = 0; i < dedtypes_dim; i++) | |
503 { | |
504 TemplateParameter tp = cast(TemplateParameter)parameters.data[i]; | |
505 Object oarg; | |
506 | |
507 printf(" [%d]", i); | |
508 | |
509 if (i < ti.tiargs.dim) | |
510 oarg = cast(Object)ti.tiargs.data[i]; | |
511 else | |
512 oarg = null; | |
513 tp.print(oarg, cast(Object)dedtypes.data[i]); | |
514 } | |
515 } | |
516 else | |
517 goto Lnomatch; | |
518 } | |
519 | |
520 version (LOGM) { | |
521 printf(" match = %d\n", m); | |
522 } | |
523 goto Lret; | |
524 | |
525 Lnomatch: | |
526 version (LOGM) { | |
527 printf(" no match\n"); | |
528 } | |
529 m = MATCHnomatch; | |
530 | |
531 Lret: | |
532 paramscope.pop(); | |
533 version (LOGM) { | |
534 printf("-TemplateDeclaration.matchWithInstance(this = %p, ti = %p) = %d\n", this, ti, m); | |
535 } | |
536 return m; | |
537 } | |
538 | |
51 | 539 /******************************************** |
540 * Determine partial specialization order of 'this' vs td2. | |
541 * Returns: | |
542 * match this is at least as specialized as td2 | |
543 * 0 td2 is more specialized than this | |
544 */ | |
0 | 545 MATCH leastAsSpecialized(TemplateDeclaration td2) |
546 { | |
51 | 547 /* This works by taking the template parameters to this template |
548 * declaration and feeding them to td2 as if it were a template | |
549 * instance. | |
550 * If it works, then this template is at least as specialized | |
551 * as td2. | |
552 */ | |
553 | |
554 scope TemplateInstance ti = new TemplateInstance(Loc(0), ident); // create dummy template instance | |
555 scope Objects dedtypes = new Objects(); | |
556 | |
557 version (LOG_LEASTAS) { | |
558 printf("%s.leastAsSpecialized(%s)\n", toChars(), td2.toChars()); | |
559 } | |
560 | |
561 // Set type arguments to dummy template instance to be types | |
562 // generated from the parameters to this template declaration | |
563 ti.tiargs = new Objects(); | |
564 ti.tiargs.setDim(parameters.dim); | |
565 for (int i = 0; i < ti.tiargs.dim; i++) | |
566 { | |
567 TemplateParameter tp = cast(TemplateParameter)parameters.data[i]; | |
568 | |
569 void* p = tp.dummyArg(); | |
570 if (p) | |
571 ti.tiargs.data[i] = p; | |
572 else | |
573 ti.tiargs.setDim(i); | |
574 } | |
575 | |
576 // Temporary Array to hold deduced types | |
577 //dedtypes.setDim(parameters.dim); | |
578 dedtypes.setDim(td2.parameters.dim); | |
579 | |
580 // Attempt a type deduction | |
581 MATCH m = td2.matchWithInstance(ti, dedtypes, 1); | |
582 if (m) | |
583 { | |
584 /* A non-variadic template is more specialized than a | |
585 * variadic one. | |
586 */ | |
587 if (isVariadic() && !td2.isVariadic()) | |
588 goto L1; | |
589 | |
590 version (LOG_LEASTAS) { | |
591 printf(" matches %d, so is least as specialized\n", m); | |
592 } | |
593 return m; | |
594 } | |
595 L1: | |
596 version (LOG_LEASTAS) { | |
597 printf(" doesn't match, so is not as specialized\n"); | |
598 } | |
599 return MATCHnomatch; | |
0 | 600 } |
601 | |
602 /************************************************* | |
603 * Match function arguments against a specific template function. | |
604 * Input: | |
605 * loc instantiation location | |
606 * targsi Expression/Type initial list of template arguments | |
607 * ethis 'this' argument if !null | |
608 * fargs arguments to function | |
609 * Output: | |
610 * dedargs Expression/Type deduced template arguments | |
611 * Returns: | |
612 * match level | |
613 */ | |
614 MATCH deduceFunctionTemplateMatch(Loc loc, Objects targsi, Expression ethis, Expressions fargs, Objects dedargs) | |
615 { | |
616 size_t nfparams; | |
617 size_t nfargs; | |
618 size_t nargsi; // array size of targsi | |
619 int fptupindex = -1; | |
620 int tuple_dim = 0; | |
621 MATCH match = MATCHexact; | |
622 FuncDeclaration fd = onemember.toAlias().isFuncDeclaration(); | |
623 Arguments fparameters; // function parameter list | |
624 int fvarargs; // function varargs | |
625 scope Objects dedtypes = new Objects(); // for T:T*, the dedargs is the T*, dedtypes is the T | |
626 | |
627 static if (false) { | |
628 printf("\nTemplateDeclaration.deduceFunctionTemplateMatch() %s\n", toChars()); | |
629 for (i = 0; i < fargs.dim; i++) | |
630 { | |
631 Expression e = cast(Expression)fargs.data[i]; | |
632 printf("\tfarg[%d] is %s, type is %s\n", i, e.toChars(), e.type.toChars()); | |
633 } | |
634 printf("fd = %s\n", fd.toChars()); | |
635 printf("fd.type = %p\n", fd.type); | |
636 } | |
637 | |
638 assert(cast(size_t)cast(void*)scope_ > 0x10000); | |
639 | |
640 dedargs.setDim(parameters.dim); | |
641 dedargs.zero(); | |
642 | |
643 dedtypes.setDim(parameters.dim); | |
644 dedtypes.zero(); | |
645 | |
646 // Set up scope for parameters | |
647 ScopeDsymbol paramsym = new ScopeDsymbol(); | |
648 paramsym.parent = scope_.parent; | |
649 Scope paramscope = scope_.push(paramsym); | |
650 | |
651 TemplateTupleParameter tp = isVariadic(); | |
652 | |
653 static if (false) { | |
654 for (i = 0; i < dedargs.dim; i++) | |
655 { | |
656 printf("\tdedarg[%d] = ", i); | |
657 Object oarg = cast(Object)dedargs.data[i]; | |
658 if (oarg) printf("%s", oarg.toChars()); | |
659 printf("\n"); | |
660 } | |
661 } | |
662 | |
663 | |
664 nargsi = 0; | |
665 if (targsi) | |
666 { // Set initial template arguments | |
667 size_t n; | |
668 | |
669 nargsi = targsi.dim; | |
670 n = parameters.dim; | |
671 if (tp) | |
672 n--; | |
673 if (nargsi > n) | |
674 { | |
675 if (!tp) | |
676 goto Lnomatch; | |
677 | |
678 /* The extra initial template arguments | |
679 * now form the tuple argument. | |
680 */ | |
681 Tuple t = new Tuple(); | |
682 assert(parameters.dim); | |
683 dedargs.data[parameters.dim - 1] = cast(void*)t; | |
684 | |
685 tuple_dim = nargsi - n; | |
686 t.objects.setDim(tuple_dim); | |
687 for (size_t i = 0; i < tuple_dim; i++) | |
688 { | |
689 t.objects.data[i] = cast(void*)targsi.data[n + i]; | |
690 } | |
691 declareParameter(paramscope, tp, t); | |
692 } | |
693 else | |
694 n = nargsi; | |
695 | |
696 memcpy(dedargs.data, targsi.data, n * (*dedargs.data).sizeof); | |
697 | |
698 for (size_t i = 0; i < n; i++) | |
699 { | |
700 assert(i < parameters.dim); | |
701 TemplateParameter tp2 = cast(TemplateParameter)parameters.data[i]; | |
702 MATCH m; | |
703 Declaration sparam = null; | |
704 | |
705 m = tp2.matchArg(paramscope, dedargs, i, parameters, dedtypes, &sparam); | |
706 //printf("\tdeduceType m = %d\n", m); | |
707 if (m == MATCHnomatch) | |
708 goto Lnomatch; | |
709 if (m < match) | |
710 match = m; | |
711 | |
712 sparam.semantic(paramscope); | |
713 if (!paramscope.insert(sparam)) | |
714 goto Lnomatch; | |
715 } | |
716 } | |
717 static if (false) { | |
718 for (i = 0; i < dedargs.dim; i++) | |
719 { | |
720 printf("\tdedarg[%d] = ", i); | |
721 Object oarg = cast(Object)dedargs.data[i]; | |
722 if (oarg) printf("%s", oarg.toChars()); | |
723 printf("\n"); | |
724 } | |
725 } | |
726 | |
727 if (fd.type) | |
728 { | |
729 assert(fd.type.ty == Tfunction); | |
730 TypeFunction fdtype = cast(TypeFunction)fd.type; | |
731 fparameters = fdtype.parameters; | |
732 fvarargs = fdtype.varargs; | |
733 } | |
734 else | |
735 { | |
736 CtorDeclaration fctor = fd.isCtorDeclaration(); | |
737 assert(fctor); | |
738 fparameters = fctor.arguments; | |
739 fvarargs = fctor.varargs; | |
740 } | |
741 | |
742 nfparams = Argument.dim(fparameters); // number of function parameters | |
743 nfargs = fargs ? fargs.dim : 0; // number of function arguments | |
744 | |
745 /* Check for match of function arguments with variadic template | |
746 * parameter, such as: | |
747 * | |
748 * template Foo(T, A...) { void Foo(T t, A a); } | |
749 * void main() { Foo(1,2,3); } | |
750 */ | |
751 if (tp) // if variadic | |
752 { | |
753 if (nfparams == 0) // if no function parameters | |
754 { | |
755 Tuple t = new Tuple(); | |
756 //printf("t = %p\n", t); | |
757 dedargs.data[parameters.dim - 1] = cast(void*)t; | |
758 declareParameter(paramscope, tp, t); | |
759 goto L2; | |
760 } | |
761 else if (nfargs < nfparams - 1) | |
762 goto L1; | |
763 else | |
764 { | |
765 /* Figure out which of the function parameters matches | |
766 * the tuple template parameter. Do this by matching | |
767 * type identifiers. | |
768 * Set the index of this function parameter to fptupindex. | |
769 */ | |
770 for (fptupindex = 0; fptupindex < nfparams; fptupindex++) | |
771 { | |
772 Argument fparam = cast(Argument)fparameters.data[fptupindex]; | |
773 if (fparam.type.ty != Tident) | |
774 continue; | |
775 TypeIdentifier tid = cast(TypeIdentifier)fparam.type; | |
776 if (!tp.ident.equals(tid.ident) || tid.idents.dim) | |
777 continue; | |
778 | |
779 if (fvarargs) // variadic function doesn't | |
780 goto Lnomatch; // go with variadic template | |
781 | |
782 /* The types of the function arguments | |
783 * now form the tuple argument. | |
784 */ | |
785 Tuple t = new Tuple(); | |
786 dedargs.data[parameters.dim - 1] = cast(void*)t; | |
787 | |
788 tuple_dim = nfargs - (nfparams - 1); | |
789 t.objects.setDim(tuple_dim); | |
790 for (size_t i = 0; i < tuple_dim; i++) | |
791 { | |
792 Expression farg = cast(Expression)fargs.data[fptupindex + i]; | |
793 t.objects.data[i] = cast(void*)farg.type; | |
794 } | |
795 declareParameter(paramscope, tp, t); | |
796 goto L2; | |
797 } | |
798 fptupindex = -1; | |
799 } | |
800 } | |
801 | |
802 L1: | |
803 if (nfparams == nfargs) { | |
804 ; | |
805 } else if (nfargs > nfparams) { | |
806 if (fvarargs == 0) | |
807 goto Lnomatch; // too many args, no match | |
808 match = MATCHconvert; // match ... with a conversion | |
809 } | |
810 | |
811 L2: | |
812 version (DMDV2) { | |
813 // Match 'ethis' to any TemplateThisParameter's | |
814 if (ethis) | |
815 { | |
816 for (size_t i = 0; i < parameters.dim; i++) | |
817 { | |
818 TemplateParameter tp2 = cast(TemplateParameter)parameters.data[i]; | |
819 TemplateThisParameter ttp = tp2.isTemplateThisParameter(); | |
820 if (ttp) | |
821 { | |
822 MATCH m; | |
823 | |
824 Type t = new TypeIdentifier(Loc(0), ttp.ident); | |
825 m = ethis.type.deduceType(paramscope, t, parameters, dedtypes); | |
826 if (!m) | |
827 goto Lnomatch; | |
828 if (m < match) | |
829 match = m; // pick worst match | |
830 } | |
831 } | |
832 } | |
833 } | |
834 | |
835 // Loop through the function parameters | |
836 for (size_t i = 0; i < nfparams; i++) | |
837 { | |
838 /* Skip over function parameters which wound up | |
839 * as part of a template tuple parameter. | |
840 */ | |
841 if (i == fptupindex) | |
842 { | |
843 if (fptupindex == nfparams - 1) | |
844 break; | |
845 i += tuple_dim - 1; | |
846 continue; | |
847 } | |
848 | |
849 Argument fparam = Argument.getNth(fparameters, i); | |
850 | |
851 if (i >= nfargs) // if not enough arguments | |
852 { | |
853 if (fparam.defaultArg) | |
854 { | |
855 /* Default arguments do not participate in template argument | |
856 * deduction. | |
857 */ | |
858 goto Lmatch; | |
859 } | |
860 } | |
861 else | |
862 { | |
863 Expression farg = cast(Expression)fargs.data[i]; | |
864 static if (false) { | |
865 printf("\tfarg.type = %s\n", farg.type.toChars()); | |
866 printf("\tfparam.type = %s\n", fparam.type.toChars()); | |
867 } | |
868 Type argtype = farg.type; | |
869 | |
870 version (DMDV2) { | |
871 /* Allow string literals which are type [] to match with [dim] | |
872 */ | |
873 if (farg.op == TOKstring) | |
874 { | |
875 StringExp se = cast(StringExp)farg; | |
876 if (!se.committed && argtype.ty == Tarray && | |
877 fparam.type.toBasetype().ty == Tsarray) | |
878 { | |
879 argtype = new TypeSArray(argtype.nextOf(), new IntegerExp(se.loc, se.len, Type.tindex)); | |
880 argtype = argtype.semantic(se.loc, null); | |
881 argtype = argtype.invariantOf(); | |
882 } | |
883 } | |
884 } | |
885 | |
886 MATCH m; | |
887 m = argtype.deduceType(paramscope, fparam.type, parameters, dedtypes); | |
888 //printf("\tdeduceType m = %d\n", m); | |
889 | |
890 /* If no match, see if there's a conversion to a delegate | |
891 */ | |
892 if (!m && fparam.type.toBasetype().ty == Tdelegate) | |
893 { | |
894 TypeDelegate td = cast(TypeDelegate)fparam.type.toBasetype(); | |
895 TypeFunction tf = cast(TypeFunction)td.next; | |
896 | |
897 if (!tf.varargs && Argument.dim(tf.parameters) == 0) | |
898 { | |
899 m = farg.type.deduceType(paramscope, tf.next, parameters, dedtypes); | |
900 if (!m && tf.next.toBasetype().ty == Tvoid) | |
901 m = MATCHconvert; | |
902 } | |
903 //printf("\tm2 = %d\n", m); | |
904 } | |
905 | |
906 if (m) | |
907 { | |
908 if (m < match) | |
909 match = m; // pick worst match | |
910 continue; | |
911 } | |
912 } | |
913 | |
914 /* The following code for variadic arguments closely | |
915 * matches TypeFunction.callMatch() | |
916 */ | |
917 if (!(fvarargs == 2 && i + 1 == nfparams)) | |
918 goto Lnomatch; | |
919 | |
920 /* Check for match with function parameter T... | |
921 */ | |
922 Type tb = fparam.type.toBasetype(); | |
923 switch (tb.ty) | |
924 { | |
925 // Perhaps we can do better with this, see TypeFunction.callMatch() | |
926 case Tsarray: | |
927 { | |
928 TypeSArray tsa = cast(TypeSArray)tb; | |
929 ulong sz = tsa.dim.toInteger(); | |
930 if (sz != nfargs - i) | |
931 goto Lnomatch; | |
932 } | |
933 case Tarray: | |
934 { | |
935 TypeArray ta = cast(TypeArray)tb; | |
936 for (; i < nfargs; i++) | |
937 { | |
938 Expression arg = cast(Expression)fargs.data[i]; | |
939 assert(arg); | |
940 MATCH m; | |
941 /* If lazy array of delegates, | |
942 * convert arg(s) to delegate(s) | |
943 */ | |
944 Type tret = fparam.isLazyArray(); | |
945 if (tret) | |
946 { | |
947 if (ta.next.equals(arg.type)) | |
948 { | |
949 m = MATCHexact; | |
950 } | |
951 else | |
952 { | |
953 m = arg.implicitConvTo(tret); | |
954 if (m == MATCHnomatch) | |
955 { | |
956 if (tret.toBasetype().ty == Tvoid) | |
957 m = MATCHconvert; | |
958 } | |
959 } | |
960 } | |
961 else | |
962 { | |
963 m = arg.type.deduceType(paramscope, ta.next, parameters, dedtypes); | |
964 //m = arg.implicitConvTo(ta.next); | |
965 } | |
966 if (m == MATCHnomatch) | |
967 goto Lnomatch; | |
968 if (m < match) | |
969 match = m; | |
970 } | |
971 goto Lmatch; | |
972 } | |
973 case Tclass: | |
974 case Tident: | |
975 goto Lmatch; | |
976 | |
977 default: | |
978 goto Lnomatch; | |
979 } | |
980 } | |
981 | |
982 Lmatch: | |
983 | |
984 /* Fill in any missing arguments with their defaults. | |
985 */ | |
986 for (size_t i = nargsi; i < dedargs.dim; i++) | |
987 { | |
988 TemplateParameter tp2 = cast(TemplateParameter)parameters.data[i]; | |
989 //printf("tp2[%d] = %s\n", i, tp2.ident.toChars()); | |
990 /* For T:T*, the dedargs is the T*, dedtypes is the T | |
991 * But for function templates, we really need them to match | |
992 */ | |
993 Object oarg = cast(Object)dedargs.data[i]; | |
994 Object oded = cast(Object)dedtypes.data[i]; | |
995 //printf("1dedargs[%d] = %p, dedtypes[%d] = %p\n", i, oarg, i, oded); | |
996 //if (oarg) printf("oarg: %s\n", oarg.toChars()); | |
997 //if (oded) printf("oded: %s\n", oded.toChars()); | |
998 if (!oarg) | |
999 { | |
1000 if (oded) | |
1001 { | |
1002 if (tp2.specialization()) | |
1003 { | |
1004 /* The specialization can work as long as afterwards | |
1005 * the oded == oarg | |
1006 */ | |
1007 Declaration sparam; | |
1008 dedargs.data[i] = cast(void*)oded; | |
1009 MATCH m2 = tp2.matchArg(paramscope, dedargs, i, parameters, dedtypes, &sparam, 0); | |
1010 //printf("m2 = %d\n", m2); | |
1011 if (!m2) | |
1012 goto Lnomatch; | |
1013 if (m2 < match) | |
1014 match = m2; // pick worst match | |
1015 if (dedtypes.data[i] !is cast(void*)oded) | |
1016 error("specialization not allowed for deduced parameter %s", tp2.ident.toChars()); | |
1017 } | |
1018 } | |
1019 else | |
1020 { | |
1021 oded = tp2.defaultArg(loc, paramscope); | |
1022 if (!oded) | |
1023 goto Lnomatch; | |
1024 } | |
1025 declareParameter(paramscope, tp2, oded); | |
1026 dedargs.data[i] = cast(void*)oded; | |
1027 } | |
1028 } | |
1029 | |
1030 version (DMDV2) { | |
1031 if (constraint) | |
1032 { /* Check to see if constraint is satisfied. | |
1033 */ | |
1034 Expression e = constraint.syntaxCopy(); | |
1035 paramscope.flags |= SCOPE.SCOPEstaticif; | |
1036 e = e.semantic(paramscope); | |
1037 e = e.optimize(WANTvalue | WANTinterpret); | |
1038 if (e.isBool(true)) { | |
1039 ; | |
1040 } else if (e.isBool(false)) | |
1041 goto Lnomatch; | |
1042 else | |
1043 { | |
1044 e.error("constraint %s is not constant or does not evaluate to a bool", e.toChars()); | |
1045 } | |
1046 } | |
1047 } | |
1048 | |
1049 static if (false) { | |
1050 for (i = 0; i < dedargs.dim; i++) | |
1051 { | |
1052 Type t = cast(Type)dedargs.data[i]; | |
1053 printf("\tdedargs[%d] = %d, %s\n", i, t.dyncast(), t.toChars()); | |
1054 } | |
1055 } | |
1056 | |
1057 paramscope.pop(); | |
1058 //printf("\tmatch %d\n", match); | |
1059 return match; | |
1060 | |
1061 Lnomatch: | |
1062 paramscope.pop(); | |
1063 //printf("\tnomatch\n"); | |
1064 return MATCHnomatch; | |
1065 } | |
1066 | |
1067 /************************************************* | |
1068 * Given function arguments, figure out which template function | |
1069 * to expand, and return that function. | |
1070 * If no match, give error message and return null. | |
1071 * Input: | |
1072 * sc instantiation scope | |
1073 * loc instantiation location | |
1074 * targsi initial list of template arguments | |
1075 * ethis if !null, the 'this' pointer argument | |
1076 * fargs arguments to function | |
1077 * flags 1: do not issue error message on no match, just return null | |
1078 */ | |
1079 FuncDeclaration deduceFunctionTemplate(Scope sc, Loc loc, Objects targsi, Expression ethis, Expressions fargs, int flags = 0) | |
1080 { | |
1081 MATCH m_best = MATCHnomatch; | |
1082 TemplateDeclaration td_ambig = null; | |
1083 TemplateDeclaration td_best = null; | |
1084 Objects tdargs = new Objects(); | |
1085 TemplateInstance ti; | |
1086 FuncDeclaration fd; | |
1087 | |
1088 static if (false) { | |
1089 printf("TemplateDeclaration.deduceFunctionTemplate() %s\n", toChars()); | |
1090 printf(" targsi:\n"); | |
1091 if (targsi) | |
1092 { | |
1093 for (int i = 0; i < targsi.dim; i++) | |
1094 { | |
1095 Object arg = cast(Object)targsi.data[i]; | |
1096 printf("\t%s\n", arg.toChars()); | |
1097 } | |
1098 } | |
1099 printf(" fargs:\n"); | |
1100 for (int i = 0; i < fargs.dim; i++) | |
1101 { | |
1102 Expression arg = cast(Expression)fargs.data[i]; | |
1103 printf("\t%s %s\n", arg.type.toChars(), arg.toChars()); | |
1104 //printf("\tty = %d\n", arg.type.ty); | |
1105 } | |
1106 } | |
1107 | |
1108 for (TemplateDeclaration td = this; td; td = td.overnext) | |
1109 { | |
1110 if (!td.semanticRun) | |
1111 { | |
1112 error("forward reference to template %s", td.toChars()); | |
1113 goto Lerror; | |
1114 } | |
1115 if (!td.onemember || !td.onemember.toAlias().isFuncDeclaration()) | |
1116 { | |
1117 error("is not a function template"); | |
1118 goto Lerror; | |
1119 } | |
1120 | |
1121 MATCH m; | |
1122 scope Objects dedargs = new Objects(); | |
1123 | |
1124 m = td.deduceFunctionTemplateMatch(loc, targsi, ethis, fargs, dedargs); | |
1125 //printf("deduceFunctionTemplateMatch = %d\n", m); | |
1126 if (!m) // if no match | |
1127 continue; | |
1128 | |
1129 if (m < m_best) | |
1130 goto Ltd_best; | |
1131 if (m > m_best) | |
1132 goto Ltd; | |
1133 | |
1134 { | |
1135 // Disambiguate by picking the most specialized TemplateDeclaration | |
1136 MATCH c1 = td.leastAsSpecialized(td_best); | |
1137 MATCH c2 = td_best.leastAsSpecialized(td); | |
1138 //printf("c1 = %d, c2 = %d\n", c1, c2); | |
1139 | |
1140 if (c1 > c2) | |
1141 goto Ltd; | |
1142 else if (c1 < c2) | |
1143 goto Ltd_best; | |
1144 else | |
1145 goto Lambig; | |
1146 } | |
1147 | |
1148 Lambig: // td_best and td are ambiguous | |
1149 td_ambig = td; | |
1150 continue; | |
1151 | |
1152 Ltd_best: // td_best is the best match so far | |
1153 td_ambig = null; | |
1154 continue; | |
1155 | |
1156 Ltd: // td is the new best match | |
1157 td_ambig = null; | |
1158 assert(cast(size_t)cast(void*)td.scope_ > 0x10000); | |
1159 td_best = td; | |
1160 m_best = m; | |
1161 tdargs.setDim(dedargs.dim); | |
1162 memcpy(tdargs.data, dedargs.data, tdargs.dim * (void*).sizeof); | |
1163 continue; | |
1164 } | |
1165 if (!td_best) | |
1166 { | |
1167 if (!(flags & 1)) | |
1168 error(loc, "does not match any function template declaration"); | |
1169 goto Lerror; | |
1170 } | |
1171 if (td_ambig) | |
1172 { | |
1173 error(loc, "matches more than one function template declaration:\n %s\nand:\n %s", | |
1174 td_best.toChars(), td_ambig.toChars()); | |
1175 } | |
1176 | |
1177 /* The best match is td_best with arguments tdargs. | |
1178 * Now instantiate the template. | |
1179 */ | |
1180 assert(cast(size_t)cast(void*)td_best.scope_ > 0x10000); | |
1181 ti = new TemplateInstance(loc, td_best, tdargs); | |
1182 ti.semantic(sc); | |
1183 fd = ti.toAlias().isFuncDeclaration(); | |
1184 if (!fd) | |
1185 goto Lerror; | |
1186 return fd; | |
1187 | |
1188 Lerror: | |
1189 /// version (DMDV2) { | |
1190 if (!(flags & 1)) | |
1191 /// } | |
1192 { | |
1193 HdrGenState hgs; | |
1194 | |
1195 scope OutBuffer bufa = new OutBuffer(); | |
1196 Objects args = targsi; | |
1197 if (args) | |
1198 { | |
1199 for (int i = 0; i < args.dim; i++) | |
1200 { | |
1201 if (i) | |
1202 bufa.writeByte(','); | |
1203 Object oarg = cast(Object)args.data[i]; | |
1204 ObjectToCBuffer(bufa, &hgs, oarg); | |
1205 } | |
1206 } | |
1207 | |
1208 scope OutBuffer buf = new OutBuffer(); | |
1209 argExpTypesToCBuffer(buf, fargs, &hgs); | |
1210 error(loc, "cannot deduce template function from argument types !(%s)(%s)", bufa.toChars(), buf.toChars()); | |
1211 } | |
1212 return null; | |
1213 } | |
1214 | |
1215 /************************************************** | |
1216 * Declare template parameter tp with value o, and install it in the scope sc. | |
1217 */ | |
1218 void declareParameter(Scope sc, TemplateParameter tp, Object o) | |
1219 { | |
1220 //printf("TemplateDeclaration.declareParameter('%s', o = %p)\n", tp.ident.toChars(), o); | |
1221 | |
1222 Type targ = isType(o); | |
1223 Expression ea = isExpression(o); | |
1224 Dsymbol sa = isDsymbol(o); | |
1225 Tuple va = isTuple(o); | |
1226 | |
1227 Dsymbol s; | |
1228 | |
1229 // See if tp.ident already exists with a matching definition | |
1230 Dsymbol scopesym; | |
1231 s = sc.search(loc, tp.ident, &scopesym); | |
1232 if (s && scopesym == sc.scopesym) | |
1233 { | |
1234 TupleDeclaration td = s.isTupleDeclaration(); | |
1235 if (va && td) | |
1236 { | |
1237 Tuple tup = new Tuple(); | |
1238 assert(false); // < not implemented | |
1239 ///tup.objects = *td.objects; | |
1240 if (match(va, tup, this, sc)) | |
1241 { | |
1242 return; | |
1243 } | |
1244 } | |
1245 } | |
1246 | |
1247 if (targ) | |
1248 { | |
1249 //printf("type %s\n", targ.toChars()); | |
1250 s = new AliasDeclaration(Loc(0), tp.ident, targ); | |
1251 } | |
1252 else if (sa) | |
1253 { | |
1254 //printf("Alias %s %s;\n", sa.ident.toChars(), tp.ident.toChars()); | |
1255 s = new AliasDeclaration(Loc(0), tp.ident, sa); | |
1256 } | |
1257 else if (ea) | |
1258 { | |
1259 // tdtypes.data[i] always matches ea here | |
1260 Initializer init = new ExpInitializer(loc, ea); | |
1261 TemplateValueParameter tvp = tp.isTemplateValueParameter(); | |
1262 | |
1263 Type t = tvp ? tvp.valType : null; | |
1264 | |
1265 VarDeclaration v = new VarDeclaration(loc, t, tp.ident, init); | |
1266 v.storage_class = STCmanifest; | |
1267 s = v; | |
1268 } | |
1269 else if (va) | |
1270 { | |
1271 //printf("\ttuple\n"); | |
1272 s = new TupleDeclaration(loc, tp.ident, va.objects); | |
1273 } | |
1274 else | |
1275 { | |
1276 debug writefln(o.toString()); | |
1277 assert(0); | |
1278 } | |
1279 | |
1280 if (!sc.insert(s)) | |
1281 error("declaration %s is already defined", tp.ident.toChars()); | |
1282 | |
1283 s.semantic(sc); | |
1284 } | |
1285 | |
72 | 1286 override TemplateDeclaration isTemplateDeclaration() { return this; } |
0 | 1287 |
1288 TemplateTupleParameter isVariadic() | |
1289 { | |
1290 return .isVariadic(parameters); | |
1291 } | |
1292 | |
63 | 1293 /*********************************** |
1294 * We can overload templates. | |
1295 */ | |
72 | 1296 override bool isOverloadable() |
0 | 1297 { |
63 | 1298 return 1; |
0 | 1299 } |
72 | 1300 } |