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