Mercurial > projects > ddmd
annotate dmd/TemplateDeclaration.d @ 96:acd69f84627e
further work
author | Trass3r |
---|---|
date | Tue, 31 Aug 2010 02:12:15 +0200 |
parents | 39648eb578f6 |
children | 12c0c84d13fd |
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 */ | |
87
b17640f0e4e8
Fixed a bug with a Scope.this(Scope enclosing) being called instead of Scope.clone() method (as a copy ctor replacement)
korDen
parents:
79
diff
changeset
|
213 this.scope_ = sc.clone(); |
0 | 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) | |
96 | 351 { buf.writestring(",\n"); |
79 | 352 offset = buf.offset; |
353 } | |
354 s.toJsonBuffer(buf); | |
355 } | |
96 | 356 JsonRemoveComma(buf); |
79 | 357 buf.writestring("]\n"); |
358 | |
359 buf.writestring("}\n"); | |
360 } | |
361 | |
72 | 362 override string kind() |
0 | 363 { |
50 | 364 return (onemember && onemember.isAggregateDeclaration()) |
365 ? onemember.kind() | |
366 : "template"; | |
0 | 367 } |
368 | |
72 | 369 override string toChars() |
0 | 370 { |
49 | 371 OutBuffer buf = new OutBuffer(); |
372 HdrGenState hgs; | |
373 | |
374 /// memset(&hgs, 0, hgs.sizeof); | |
375 buf.writestring(ident.toChars()); | |
376 buf.writeByte('('); | |
377 for (int i = 0; i < parameters.dim; i++) | |
378 { | |
379 TemplateParameter tp = cast(TemplateParameter)parameters.data[i]; | |
380 if (i) | |
381 buf.writeByte(','); | |
382 tp.toCBuffer(buf, &hgs); | |
383 } | |
384 buf.writeByte(')'); | |
385 version (DMDV2) { | |
386 if (constraint) | |
387 { | |
388 buf.writestring(" if ("); | |
389 constraint.toCBuffer(buf, &hgs); | |
390 buf.writeByte(')'); | |
391 } | |
392 } | |
393 buf.writeByte(0); | |
394 return buf.extractString(); | |
0 | 395 } |
396 | |
72 | 397 override void emitComment(Scope sc) |
0 | 398 { |
399 assert(false); | |
400 } | |
50 | 401 |
0 | 402 // void toDocBuffer(OutBuffer *buf); |
403 | |
404 /*************************************** | |
405 * Given that ti is an instance of this TemplateDeclaration, | |
406 * deduce the types of the parameters to this, and store | |
407 * those deduced types in dedtypes[]. | |
408 * Input: | |
409 * flag 1: don't do semantic() because of dummy types | |
410 * 2: don't change types in matchArg() | |
411 * Output: | |
412 * dedtypes deduced arguments | |
413 * Return match level. | |
414 */ | |
415 MATCH matchWithInstance(TemplateInstance ti, Objects dedtypes, int flag) | |
416 { | |
417 MATCH m; | |
418 int dedtypes_dim = dedtypes.dim; | |
419 | |
420 version (LOGM) { | |
50 | 421 printf("\n+TemplateDeclaration.matchWithInstance(this = %.*s, ti = %.*s, flag = %d)\n", toChars(), ti.toChars(), flag); |
0 | 422 } |
423 | |
424 static if (false) { | |
425 printf("dedtypes.dim = %d, parameters.dim = %d\n", dedtypes_dim, parameters.dim); | |
426 if (ti.tiargs.dim) | |
427 printf("ti.tiargs.dim = %d, [0] = %p\n", ti.tiargs.dim, ti.tiargs.data[0]); | |
428 } | |
429 dedtypes.zero(); | |
430 | |
431 int parameters_dim = parameters.dim; | |
432 int variadic = isVariadic() !is null; | |
433 | |
434 // If more arguments than parameters, no match | |
435 if (ti.tiargs.dim > parameters_dim && !variadic) | |
436 { | |
437 version (LOGM) { | |
50 | 438 printf(" no match: more arguments than parameters\n"); |
0 | 439 } |
50 | 440 return MATCHnomatch; |
0 | 441 } |
442 | |
443 assert(dedtypes_dim == parameters_dim); | |
444 assert(dedtypes_dim >= ti.tiargs.dim || variadic); | |
445 | |
446 // Set up scope for parameters | |
447 assert(cast(size_t)cast(void*)scope_ > 0x10000); | |
448 ScopeDsymbol paramsym = new ScopeDsymbol(); | |
449 paramsym.parent = scope_.parent; | |
450 Scope paramscope = scope_.push(paramsym); | |
451 paramscope.stc = STCundefined; | |
452 | |
453 // Attempt type deduction | |
454 m = MATCHexact; | |
455 for (int i = 0; i < dedtypes_dim; i++) | |
456 { | |
457 MATCH m2; | |
458 TemplateParameter tp = cast(TemplateParameter)parameters.data[i]; | |
459 Declaration sparam; | |
460 | |
461 //printf("\targument [%d]\n", i); | |
462 version (LOGM) { | |
463 //printf("\targument [%d] is %s\n", i, oarg ? oarg.toChars() : "null"); | |
464 TemplateTypeParameter *ttp = tp.isTemplateTypeParameter(); | |
465 if (ttp) | |
466 printf("\tparameter[%d] is %s : %s\n", i, tp.ident.toChars(), ttp.specType ? ttp.specType.toChars() : ""); | |
467 } | |
468 | |
469 version (DMDV1) { | |
470 m2 = tp.matchArg(paramscope, ti.tiargs, i, parameters, dedtypes, &sparam); | |
471 } else { | |
472 m2 = tp.matchArg(paramscope, ti.tiargs, i, parameters, dedtypes, &sparam, (flag & 2) ? 1 : 0); | |
473 } | |
474 //printf("\tm2 = %d\n", m2); | |
475 | |
476 if (m2 == MATCHnomatch) | |
477 { | |
478 static if (false) { | |
479 printf("\tmatchArg() for parameter %i failed\n", i); | |
480 } | |
481 goto Lnomatch; | |
482 } | |
483 | |
484 if (m2 < m) | |
485 m = m2; | |
486 | |
487 if (!flag) | |
488 sparam.semantic(paramscope); | |
489 if (!paramscope.insert(sparam)) | |
490 goto Lnomatch; | |
491 } | |
492 | |
493 if (!flag) | |
494 { | |
495 /* Any parameter left without a type gets the type of | |
496 * its corresponding arg | |
497 */ | |
498 for (int i = 0; i < dedtypes_dim; i++) | |
499 { | |
500 if (!dedtypes.data[i]) | |
501 { | |
502 assert(i < ti.tiargs.dim); | |
503 dedtypes.data[i] = ti.tiargs.data[i]; | |
504 } | |
505 } | |
506 } | |
507 | |
508 version (DMDV2) { | |
509 if (m && constraint && !(flag & 1)) | |
510 { /* Check to see if constraint is satisfied. | |
511 */ | |
512 Expression e = constraint.syntaxCopy(); | |
513 paramscope.flags |= SCOPE.SCOPEstaticif; | |
514 e = e.semantic(paramscope); | |
515 e = e.optimize(WANTvalue | WANTinterpret); | |
516 if (e.isBool(true)) { | |
517 ; | |
518 } else if (e.isBool(false)) | |
519 goto Lnomatch; | |
520 else | |
521 { | |
522 e.error("constraint %s is not constant or does not evaluate to a bool", e.toChars()); | |
523 } | |
524 } | |
525 } | |
526 | |
527 version (LOGM) { | |
528 // Print out the results | |
529 printf("--------------------------\n"); | |
530 printf("template %s\n", toChars()); | |
531 printf("instance %s\n", ti.toChars()); | |
532 if (m) | |
533 { | |
534 for (int i = 0; i < dedtypes_dim; i++) | |
535 { | |
536 TemplateParameter tp = cast(TemplateParameter)parameters.data[i]; | |
537 Object oarg; | |
538 | |
539 printf(" [%d]", i); | |
540 | |
541 if (i < ti.tiargs.dim) | |
542 oarg = cast(Object)ti.tiargs.data[i]; | |
543 else | |
544 oarg = null; | |
545 tp.print(oarg, cast(Object)dedtypes.data[i]); | |
546 } | |
547 } | |
548 else | |
549 goto Lnomatch; | |
550 } | |
551 | |
552 version (LOGM) { | |
553 printf(" match = %d\n", m); | |
554 } | |
555 goto Lret; | |
556 | |
557 Lnomatch: | |
558 version (LOGM) { | |
559 printf(" no match\n"); | |
560 } | |
561 m = MATCHnomatch; | |
562 | |
563 Lret: | |
564 paramscope.pop(); | |
565 version (LOGM) { | |
566 printf("-TemplateDeclaration.matchWithInstance(this = %p, ti = %p) = %d\n", this, ti, m); | |
567 } | |
568 return m; | |
569 } | |
570 | |
51 | 571 /******************************************** |
572 * Determine partial specialization order of 'this' vs td2. | |
573 * Returns: | |
574 * match this is at least as specialized as td2 | |
575 * 0 td2 is more specialized than this | |
576 */ | |
0 | 577 MATCH leastAsSpecialized(TemplateDeclaration td2) |
578 { | |
51 | 579 /* This works by taking the template parameters to this template |
580 * declaration and feeding them to td2 as if it were a template | |
581 * instance. | |
582 * If it works, then this template is at least as specialized | |
583 * as td2. | |
584 */ | |
585 | |
586 scope TemplateInstance ti = new TemplateInstance(Loc(0), ident); // create dummy template instance | |
587 scope Objects dedtypes = new Objects(); | |
588 | |
589 version (LOG_LEASTAS) { | |
590 printf("%s.leastAsSpecialized(%s)\n", toChars(), td2.toChars()); | |
591 } | |
592 | |
593 // Set type arguments to dummy template instance to be types | |
594 // generated from the parameters to this template declaration | |
595 ti.tiargs = new Objects(); | |
596 ti.tiargs.setDim(parameters.dim); | |
597 for (int i = 0; i < ti.tiargs.dim; i++) | |
598 { | |
599 TemplateParameter tp = cast(TemplateParameter)parameters.data[i]; | |
600 | |
601 void* p = tp.dummyArg(); | |
602 if (p) | |
603 ti.tiargs.data[i] = p; | |
604 else | |
605 ti.tiargs.setDim(i); | |
606 } | |
607 | |
608 // Temporary Array to hold deduced types | |
609 //dedtypes.setDim(parameters.dim); | |
610 dedtypes.setDim(td2.parameters.dim); | |
611 | |
612 // Attempt a type deduction | |
613 MATCH m = td2.matchWithInstance(ti, dedtypes, 1); | |
614 if (m) | |
615 { | |
616 /* A non-variadic template is more specialized than a | |
617 * variadic one. | |
618 */ | |
619 if (isVariadic() && !td2.isVariadic()) | |
620 goto L1; | |
621 | |
622 version (LOG_LEASTAS) { | |
623 printf(" matches %d, so is least as specialized\n", m); | |
624 } | |
625 return m; | |
626 } | |
627 L1: | |
628 version (LOG_LEASTAS) { | |
629 printf(" doesn't match, so is not as specialized\n"); | |
630 } | |
631 return MATCHnomatch; | |
0 | 632 } |
633 | |
634 /************************************************* | |
635 * Match function arguments against a specific template function. | |
636 * Input: | |
637 * loc instantiation location | |
638 * targsi Expression/Type initial list of template arguments | |
639 * ethis 'this' argument if !null | |
640 * fargs arguments to function | |
641 * Output: | |
642 * dedargs Expression/Type deduced template arguments | |
643 * Returns: | |
644 * match level | |
645 */ | |
646 MATCH deduceFunctionTemplateMatch(Loc loc, Objects targsi, Expression ethis, Expressions fargs, Objects dedargs) | |
647 { | |
648 size_t nfparams; | |
649 size_t nfargs; | |
650 size_t nargsi; // array size of targsi | |
651 int fptupindex = -1; | |
652 int tuple_dim = 0; | |
653 MATCH match = MATCHexact; | |
654 FuncDeclaration fd = onemember.toAlias().isFuncDeclaration(); | |
655 Arguments fparameters; // function parameter list | |
656 int fvarargs; // function varargs | |
657 scope Objects dedtypes = new Objects(); // for T:T*, the dedargs is the T*, dedtypes is the T | |
658 | |
79 | 659 static if (false) |
660 { | |
0 | 661 printf("\nTemplateDeclaration.deduceFunctionTemplateMatch() %s\n", toChars()); |
662 for (i = 0; i < fargs.dim; i++) | |
663 { | |
664 Expression e = cast(Expression)fargs.data[i]; | |
665 printf("\tfarg[%d] is %s, type is %s\n", i, e.toChars(), e.type.toChars()); | |
666 } | |
667 printf("fd = %s\n", fd.toChars()); | |
668 printf("fd.type = %p\n", fd.type); | |
669 } | |
670 | |
671 assert(cast(size_t)cast(void*)scope_ > 0x10000); | |
672 | |
673 dedargs.setDim(parameters.dim); | |
674 dedargs.zero(); | |
675 | |
676 dedtypes.setDim(parameters.dim); | |
677 dedtypes.zero(); | |
678 | |
679 // Set up scope for parameters | |
680 ScopeDsymbol paramsym = new ScopeDsymbol(); | |
681 paramsym.parent = scope_.parent; | |
682 Scope paramscope = scope_.push(paramsym); | |
683 | |
684 TemplateTupleParameter tp = isVariadic(); | |
685 | |
79 | 686 static if (false) |
687 { | |
0 | 688 for (i = 0; i < dedargs.dim; i++) |
689 { | |
690 printf("\tdedarg[%d] = ", i); | |
691 Object oarg = cast(Object)dedargs.data[i]; | |
692 if (oarg) printf("%s", oarg.toChars()); | |
693 printf("\n"); | |
694 } | |
695 } | |
696 | |
697 | |
698 nargsi = 0; | |
699 if (targsi) | |
79 | 700 { |
701 // Set initial template arguments | |
0 | 702 nargsi = targsi.dim; |
79 | 703 size_t n = parameters.dim; |
0 | 704 if (tp) |
705 n--; | |
706 if (nargsi > n) | |
707 { | |
708 if (!tp) | |
709 goto Lnomatch; | |
710 | |
711 /* The extra initial template arguments | |
712 * now form the tuple argument. | |
713 */ | |
714 Tuple t = new Tuple(); | |
715 assert(parameters.dim); | |
716 dedargs.data[parameters.dim - 1] = cast(void*)t; | |
717 | |
718 tuple_dim = nargsi - n; | |
719 t.objects.setDim(tuple_dim); | |
720 for (size_t i = 0; i < tuple_dim; i++) | |
721 { | |
722 t.objects.data[i] = cast(void*)targsi.data[n + i]; | |
723 } | |
724 declareParameter(paramscope, tp, t); | |
725 } | |
726 else | |
727 n = nargsi; | |
728 | |
729 memcpy(dedargs.data, targsi.data, n * (*dedargs.data).sizeof); | |
730 | |
731 for (size_t i = 0; i < n; i++) | |
732 { | |
733 assert(i < parameters.dim); | |
734 TemplateParameter tp2 = cast(TemplateParameter)parameters.data[i]; | |
735 MATCH m; | |
736 Declaration sparam = null; | |
737 | |
738 m = tp2.matchArg(paramscope, dedargs, i, parameters, dedtypes, &sparam); | |
739 //printf("\tdeduceType m = %d\n", m); | |
740 if (m == MATCHnomatch) | |
741 goto Lnomatch; | |
742 if (m < match) | |
743 match = m; | |
744 | |
745 sparam.semantic(paramscope); | |
746 if (!paramscope.insert(sparam)) | |
747 goto Lnomatch; | |
748 } | |
749 } | |
79 | 750 static if (false) |
751 { | |
0 | 752 for (i = 0; i < dedargs.dim; i++) |
753 { | |
754 printf("\tdedarg[%d] = ", i); | |
755 Object oarg = cast(Object)dedargs.data[i]; | |
756 if (oarg) printf("%s", oarg.toChars()); | |
757 printf("\n"); | |
758 } | |
759 } | |
760 | |
761 if (fd.type) | |
762 { | |
763 assert(fd.type.ty == Tfunction); | |
764 TypeFunction fdtype = cast(TypeFunction)fd.type; | |
765 fparameters = fdtype.parameters; | |
766 fvarargs = fdtype.varargs; | |
767 } | |
768 else | |
769 { | |
770 CtorDeclaration fctor = fd.isCtorDeclaration(); | |
771 assert(fctor); | |
772 fparameters = fctor.arguments; | |
773 fvarargs = fctor.varargs; | |
774 } | |
775 | |
776 nfparams = Argument.dim(fparameters); // number of function parameters | |
777 nfargs = fargs ? fargs.dim : 0; // number of function arguments | |
778 | |
779 /* Check for match of function arguments with variadic template | |
780 * parameter, such as: | |
781 * | |
782 * template Foo(T, A...) { void Foo(T t, A a); } | |
783 * void main() { Foo(1,2,3); } | |
784 */ | |
785 if (tp) // if variadic | |
786 { | |
79 | 787 if (nfparams == 0 && nfargs != 0) // if no function parameters |
0 | 788 { |
789 Tuple t = new Tuple(); | |
790 //printf("t = %p\n", t); | |
791 dedargs.data[parameters.dim - 1] = cast(void*)t; | |
792 declareParameter(paramscope, tp, t); | |
793 goto L2; | |
794 } | |
795 else if (nfargs < nfparams - 1) | |
796 goto L1; | |
797 else | |
798 { | |
799 /* Figure out which of the function parameters matches | |
800 * the tuple template parameter. Do this by matching | |
801 * type identifiers. | |
802 * Set the index of this function parameter to fptupindex. | |
803 */ | |
804 for (fptupindex = 0; fptupindex < nfparams; fptupindex++) | |
805 { | |
806 Argument fparam = cast(Argument)fparameters.data[fptupindex]; | |
807 if (fparam.type.ty != Tident) | |
808 continue; | |
809 TypeIdentifier tid = cast(TypeIdentifier)fparam.type; | |
810 if (!tp.ident.equals(tid.ident) || tid.idents.dim) | |
811 continue; | |
812 | |
813 if (fvarargs) // variadic function doesn't | |
814 goto Lnomatch; // go with variadic template | |
815 | |
816 /* The types of the function arguments | |
817 * now form the tuple argument. | |
818 */ | |
819 Tuple t = new Tuple(); | |
820 dedargs.data[parameters.dim - 1] = cast(void*)t; | |
821 | |
822 tuple_dim = nfargs - (nfparams - 1); | |
823 t.objects.setDim(tuple_dim); | |
824 for (size_t i = 0; i < tuple_dim; i++) | |
825 { | |
90
39648eb578f6
more Expressions work
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
87
diff
changeset
|
826 auto farg = fargs[fptupindex + i]; |
0 | 827 t.objects.data[i] = cast(void*)farg.type; |
828 } | |
829 declareParameter(paramscope, tp, t); | |
830 goto L2; | |
831 } | |
832 fptupindex = -1; | |
833 } | |
834 } | |
835 | |
836 L1: | |
837 if (nfparams == nfargs) { | |
838 ; | |
839 } else if (nfargs > nfparams) { | |
840 if (fvarargs == 0) | |
841 goto Lnomatch; // too many args, no match | |
842 match = MATCHconvert; // match ... with a conversion | |
843 } | |
844 | |
845 L2: | |
846 version (DMDV2) { | |
847 // Match 'ethis' to any TemplateThisParameter's | |
848 if (ethis) | |
849 { | |
850 for (size_t i = 0; i < parameters.dim; i++) | |
851 { | |
852 TemplateParameter tp2 = cast(TemplateParameter)parameters.data[i]; | |
853 TemplateThisParameter ttp = tp2.isTemplateThisParameter(); | |
854 if (ttp) | |
855 { | |
856 MATCH m; | |
857 | |
858 Type t = new TypeIdentifier(Loc(0), ttp.ident); | |
859 m = ethis.type.deduceType(paramscope, t, parameters, dedtypes); | |
860 if (!m) | |
861 goto Lnomatch; | |
862 if (m < match) | |
863 match = m; // pick worst match | |
864 } | |
865 } | |
866 } | |
867 } | |
868 | |
869 // Loop through the function parameters | |
870 for (size_t i = 0; i < nfparams; i++) | |
871 { | |
872 /* Skip over function parameters which wound up | |
873 * as part of a template tuple parameter. | |
874 */ | |
875 if (i == fptupindex) | |
876 { | |
877 if (fptupindex == nfparams - 1) | |
878 break; | |
879 i += tuple_dim - 1; | |
880 continue; | |
881 } | |
882 | |
883 Argument fparam = Argument.getNth(fparameters, i); | |
884 | |
885 if (i >= nfargs) // if not enough arguments | |
886 { | |
887 if (fparam.defaultArg) | |
888 { | |
889 /* Default arguments do not participate in template argument | |
890 * deduction. | |
891 */ | |
892 goto Lmatch; | |
893 } | |
894 } | |
895 else | |
896 { | |
90
39648eb578f6
more Expressions work
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
87
diff
changeset
|
897 auto farg = fargs[i]; |
0 | 898 static if (false) { |
899 printf("\tfarg.type = %s\n", farg.type.toChars()); | |
900 printf("\tfparam.type = %s\n", fparam.type.toChars()); | |
901 } | |
902 Type argtype = farg.type; | |
903 | |
904 version (DMDV2) { | |
905 /* Allow string literals which are type [] to match with [dim] | |
906 */ | |
907 if (farg.op == TOKstring) | |
908 { | |
909 StringExp se = cast(StringExp)farg; | |
910 if (!se.committed && argtype.ty == Tarray && | |
911 fparam.type.toBasetype().ty == Tsarray) | |
912 { | |
913 argtype = new TypeSArray(argtype.nextOf(), new IntegerExp(se.loc, se.len, Type.tindex)); | |
914 argtype = argtype.semantic(se.loc, null); | |
915 argtype = argtype.invariantOf(); | |
916 } | |
917 } | |
918 } | |
919 | |
920 MATCH m; | |
921 m = argtype.deduceType(paramscope, fparam.type, parameters, dedtypes); | |
922 //printf("\tdeduceType m = %d\n", m); | |
923 | |
924 /* If no match, see if there's a conversion to a delegate | |
925 */ | |
926 if (!m && fparam.type.toBasetype().ty == Tdelegate) | |
927 { | |
928 TypeDelegate td = cast(TypeDelegate)fparam.type.toBasetype(); | |
929 TypeFunction tf = cast(TypeFunction)td.next; | |
930 | |
931 if (!tf.varargs && Argument.dim(tf.parameters) == 0) | |
932 { | |
933 m = farg.type.deduceType(paramscope, tf.next, parameters, dedtypes); | |
934 if (!m && tf.next.toBasetype().ty == Tvoid) | |
935 m = MATCHconvert; | |
936 } | |
937 //printf("\tm2 = %d\n", m); | |
938 } | |
939 | |
940 if (m) | |
941 { | |
942 if (m < match) | |
943 match = m; // pick worst match | |
944 continue; | |
945 } | |
946 } | |
947 | |
948 /* The following code for variadic arguments closely | |
949 * matches TypeFunction.callMatch() | |
950 */ | |
951 if (!(fvarargs == 2 && i + 1 == nfparams)) | |
952 goto Lnomatch; | |
953 | |
954 /* Check for match with function parameter T... | |
955 */ | |
956 Type tb = fparam.type.toBasetype(); | |
957 switch (tb.ty) | |
958 { | |
959 // Perhaps we can do better with this, see TypeFunction.callMatch() | |
960 case Tsarray: | |
961 { | |
962 TypeSArray tsa = cast(TypeSArray)tb; | |
963 ulong sz = tsa.dim.toInteger(); | |
964 if (sz != nfargs - i) | |
965 goto Lnomatch; | |
966 } | |
967 case Tarray: | |
968 { | |
969 TypeArray ta = cast(TypeArray)tb; | |
970 for (; i < nfargs; i++) | |
971 { | |
90
39648eb578f6
more Expressions work
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
87
diff
changeset
|
972 auto arg = fargs[i]; |
0 | 973 assert(arg); |
974 MATCH m; | |
975 /* If lazy array of delegates, | |
976 * convert arg(s) to delegate(s) | |
977 */ | |
978 Type tret = fparam.isLazyArray(); | |
979 if (tret) | |
980 { | |
981 if (ta.next.equals(arg.type)) | |
982 { | |
983 m = MATCHexact; | |
984 } | |
985 else | |
986 { | |
987 m = arg.implicitConvTo(tret); | |
988 if (m == MATCHnomatch) | |
989 { | |
990 if (tret.toBasetype().ty == Tvoid) | |
991 m = MATCHconvert; | |
992 } | |
993 } | |
994 } | |
995 else | |
996 { | |
997 m = arg.type.deduceType(paramscope, ta.next, parameters, dedtypes); | |
998 //m = arg.implicitConvTo(ta.next); | |
999 } | |
1000 if (m == MATCHnomatch) | |
1001 goto Lnomatch; | |
1002 if (m < match) | |
1003 match = m; | |
1004 } | |
1005 goto Lmatch; | |
1006 } | |
1007 case Tclass: | |
1008 case Tident: | |
1009 goto Lmatch; | |
1010 | |
1011 default: | |
1012 goto Lnomatch; | |
1013 } | |
1014 } | |
1015 | |
1016 Lmatch: | |
1017 | |
1018 /* Fill in any missing arguments with their defaults. | |
1019 */ | |
1020 for (size_t i = nargsi; i < dedargs.dim; i++) | |
1021 { | |
1022 TemplateParameter tp2 = cast(TemplateParameter)parameters.data[i]; | |
1023 //printf("tp2[%d] = %s\n", i, tp2.ident.toChars()); | |
1024 /* For T:T*, the dedargs is the T*, dedtypes is the T | |
1025 * But for function templates, we really need them to match | |
1026 */ | |
1027 Object oarg = cast(Object)dedargs.data[i]; | |
1028 Object oded = cast(Object)dedtypes.data[i]; | |
1029 //printf("1dedargs[%d] = %p, dedtypes[%d] = %p\n", i, oarg, i, oded); | |
1030 //if (oarg) printf("oarg: %s\n", oarg.toChars()); | |
1031 //if (oded) printf("oded: %s\n", oded.toChars()); | |
1032 if (!oarg) | |
1033 { | |
1034 if (oded) | |
1035 { | |
1036 if (tp2.specialization()) | |
1037 { | |
1038 /* The specialization can work as long as afterwards | |
1039 * the oded == oarg | |
1040 */ | |
1041 Declaration sparam; | |
1042 dedargs.data[i] = cast(void*)oded; | |
1043 MATCH m2 = tp2.matchArg(paramscope, dedargs, i, parameters, dedtypes, &sparam, 0); | |
1044 //printf("m2 = %d\n", m2); | |
1045 if (!m2) | |
1046 goto Lnomatch; | |
1047 if (m2 < match) | |
1048 match = m2; // pick worst match | |
1049 if (dedtypes.data[i] !is cast(void*)oded) | |
1050 error("specialization not allowed for deduced parameter %s", tp2.ident.toChars()); | |
1051 } | |
1052 } | |
1053 else | |
1054 { | |
1055 oded = tp2.defaultArg(loc, paramscope); | |
1056 if (!oded) | |
1057 goto Lnomatch; | |
1058 } | |
1059 declareParameter(paramscope, tp2, oded); | |
1060 dedargs.data[i] = cast(void*)oded; | |
1061 } | |
1062 } | |
1063 | |
1064 version (DMDV2) { | |
1065 if (constraint) | |
1066 { /* Check to see if constraint is satisfied. | |
1067 */ | |
1068 Expression e = constraint.syntaxCopy(); | |
1069 paramscope.flags |= SCOPE.SCOPEstaticif; | |
1070 e = e.semantic(paramscope); | |
1071 e = e.optimize(WANTvalue | WANTinterpret); | |
1072 if (e.isBool(true)) { | |
1073 ; | |
1074 } else if (e.isBool(false)) | |
1075 goto Lnomatch; | |
1076 else | |
1077 { | |
1078 e.error("constraint %s is not constant or does not evaluate to a bool", e.toChars()); | |
1079 } | |
1080 } | |
1081 } | |
1082 | |
1083 static if (false) { | |
1084 for (i = 0; i < dedargs.dim; i++) | |
1085 { | |
1086 Type t = cast(Type)dedargs.data[i]; | |
1087 printf("\tdedargs[%d] = %d, %s\n", i, t.dyncast(), t.toChars()); | |
1088 } | |
1089 } | |
1090 | |
1091 paramscope.pop(); | |
1092 //printf("\tmatch %d\n", match); | |
1093 return match; | |
1094 | |
1095 Lnomatch: | |
1096 paramscope.pop(); | |
1097 //printf("\tnomatch\n"); | |
1098 return MATCHnomatch; | |
1099 } | |
1100 | |
1101 /************************************************* | |
1102 * Given function arguments, figure out which template function | |
1103 * to expand, and return that function. | |
1104 * If no match, give error message and return null. | |
1105 * Input: | |
1106 * sc instantiation scope | |
1107 * loc instantiation location | |
1108 * targsi initial list of template arguments | |
1109 * ethis if !null, the 'this' pointer argument | |
1110 * fargs arguments to function | |
1111 * flags 1: do not issue error message on no match, just return null | |
1112 */ | |
1113 FuncDeclaration deduceFunctionTemplate(Scope sc, Loc loc, Objects targsi, Expression ethis, Expressions fargs, int flags = 0) | |
1114 { | |
1115 MATCH m_best = MATCHnomatch; | |
1116 TemplateDeclaration td_ambig = null; | |
1117 TemplateDeclaration td_best = null; | |
1118 Objects tdargs = new Objects(); | |
1119 TemplateInstance ti; | |
1120 FuncDeclaration fd; | |
1121 | |
1122 static if (false) { | |
1123 printf("TemplateDeclaration.deduceFunctionTemplate() %s\n", toChars()); | |
1124 printf(" targsi:\n"); | |
1125 if (targsi) | |
1126 { | |
1127 for (int i = 0; i < targsi.dim; i++) | |
1128 { | |
1129 Object arg = cast(Object)targsi.data[i]; | |
1130 printf("\t%s\n", arg.toChars()); | |
1131 } | |
1132 } | |
1133 printf(" fargs:\n"); | |
1134 for (int i = 0; i < fargs.dim; i++) | |
1135 { | |
1136 Expression arg = cast(Expression)fargs.data[i]; | |
1137 printf("\t%s %s\n", arg.type.toChars(), arg.toChars()); | |
1138 //printf("\tty = %d\n", arg.type.ty); | |
1139 } | |
1140 } | |
1141 | |
1142 for (TemplateDeclaration td = this; td; td = td.overnext) | |
1143 { | |
1144 if (!td.semanticRun) | |
1145 { | |
1146 error("forward reference to template %s", td.toChars()); | |
1147 goto Lerror; | |
1148 } | |
1149 if (!td.onemember || !td.onemember.toAlias().isFuncDeclaration()) | |
1150 { | |
1151 error("is not a function template"); | |
1152 goto Lerror; | |
1153 } | |
1154 | |
1155 MATCH m; | |
1156 scope Objects dedargs = new Objects(); | |
1157 | |
1158 m = td.deduceFunctionTemplateMatch(loc, targsi, ethis, fargs, dedargs); | |
1159 //printf("deduceFunctionTemplateMatch = %d\n", m); | |
1160 if (!m) // if no match | |
1161 continue; | |
1162 | |
1163 if (m < m_best) | |
1164 goto Ltd_best; | |
1165 if (m > m_best) | |
1166 goto Ltd; | |
1167 | |
1168 { | |
1169 // Disambiguate by picking the most specialized TemplateDeclaration | |
1170 MATCH c1 = td.leastAsSpecialized(td_best); | |
1171 MATCH c2 = td_best.leastAsSpecialized(td); | |
1172 //printf("c1 = %d, c2 = %d\n", c1, c2); | |
1173 | |
1174 if (c1 > c2) | |
1175 goto Ltd; | |
1176 else if (c1 < c2) | |
1177 goto Ltd_best; | |
1178 else | |
1179 goto Lambig; | |
1180 } | |
1181 | |
1182 Lambig: // td_best and td are ambiguous | |
1183 td_ambig = td; | |
1184 continue; | |
1185 | |
1186 Ltd_best: // td_best is the best match so far | |
1187 td_ambig = null; | |
1188 continue; | |
1189 | |
1190 Ltd: // td is the new best match | |
1191 td_ambig = null; | |
1192 assert(cast(size_t)cast(void*)td.scope_ > 0x10000); | |
1193 td_best = td; | |
1194 m_best = m; | |
1195 tdargs.setDim(dedargs.dim); | |
1196 memcpy(tdargs.data, dedargs.data, tdargs.dim * (void*).sizeof); | |
1197 continue; | |
1198 } | |
1199 if (!td_best) | |
1200 { | |
1201 if (!(flags & 1)) | |
1202 error(loc, "does not match any function template declaration"); | |
1203 goto Lerror; | |
1204 } | |
1205 if (td_ambig) | |
1206 { | |
1207 error(loc, "matches more than one function template declaration:\n %s\nand:\n %s", | |
1208 td_best.toChars(), td_ambig.toChars()); | |
1209 } | |
1210 | |
1211 /* The best match is td_best with arguments tdargs. | |
1212 * Now instantiate the template. | |
1213 */ | |
1214 assert(cast(size_t)cast(void*)td_best.scope_ > 0x10000); | |
1215 ti = new TemplateInstance(loc, td_best, tdargs); | |
1216 ti.semantic(sc); | |
1217 fd = ti.toAlias().isFuncDeclaration(); | |
1218 if (!fd) | |
1219 goto Lerror; | |
1220 return fd; | |
1221 | |
1222 Lerror: | |
1223 /// version (DMDV2) { | |
1224 if (!(flags & 1)) | |
1225 /// } | |
1226 { | |
1227 HdrGenState hgs; | |
1228 | |
1229 scope OutBuffer bufa = new OutBuffer(); | |
1230 Objects args = targsi; | |
1231 if (args) | |
1232 { | |
1233 for (int i = 0; i < args.dim; i++) | |
1234 { | |
1235 if (i) | |
1236 bufa.writeByte(','); | |
1237 Object oarg = cast(Object)args.data[i]; | |
1238 ObjectToCBuffer(bufa, &hgs, oarg); | |
1239 } | |
1240 } | |
1241 | |
1242 scope OutBuffer buf = new OutBuffer(); | |
1243 argExpTypesToCBuffer(buf, fargs, &hgs); | |
1244 error(loc, "cannot deduce template function from argument types !(%s)(%s)", bufa.toChars(), buf.toChars()); | |
1245 } | |
1246 return null; | |
1247 } | |
1248 | |
1249 /************************************************** | |
1250 * Declare template parameter tp with value o, and install it in the scope sc. | |
1251 */ | |
1252 void declareParameter(Scope sc, TemplateParameter tp, Object o) | |
1253 { | |
1254 //printf("TemplateDeclaration.declareParameter('%s', o = %p)\n", tp.ident.toChars(), o); | |
1255 | |
1256 Type targ = isType(o); | |
1257 Expression ea = isExpression(o); | |
1258 Dsymbol sa = isDsymbol(o); | |
1259 Tuple va = isTuple(o); | |
1260 | |
1261 Dsymbol s; | |
1262 | |
1263 // See if tp.ident already exists with a matching definition | |
1264 Dsymbol scopesym; | |
1265 s = sc.search(loc, tp.ident, &scopesym); | |
1266 if (s && scopesym == sc.scopesym) | |
1267 { | |
1268 TupleDeclaration td = s.isTupleDeclaration(); | |
1269 if (va && td) | |
1270 { | |
1271 Tuple tup = new Tuple(); | |
1272 assert(false); // < not implemented | |
1273 ///tup.objects = *td.objects; | |
1274 if (match(va, tup, this, sc)) | |
1275 { | |
1276 return; | |
1277 } | |
1278 } | |
1279 } | |
1280 | |
1281 if (targ) | |
1282 { | |
1283 //printf("type %s\n", targ.toChars()); | |
1284 s = new AliasDeclaration(Loc(0), tp.ident, targ); | |
1285 } | |
1286 else if (sa) | |
1287 { | |
1288 //printf("Alias %s %s;\n", sa.ident.toChars(), tp.ident.toChars()); | |
1289 s = new AliasDeclaration(Loc(0), tp.ident, sa); | |
1290 } | |
1291 else if (ea) | |
1292 { | |
1293 // tdtypes.data[i] always matches ea here | |
1294 Initializer init = new ExpInitializer(loc, ea); | |
1295 TemplateValueParameter tvp = tp.isTemplateValueParameter(); | |
1296 | |
1297 Type t = tvp ? tvp.valType : null; | |
1298 | |
1299 VarDeclaration v = new VarDeclaration(loc, t, tp.ident, init); | |
1300 v.storage_class = STCmanifest; | |
1301 s = v; | |
1302 } | |
1303 else if (va) | |
1304 { | |
1305 //printf("\ttuple\n"); | |
1306 s = new TupleDeclaration(loc, tp.ident, va.objects); | |
1307 } | |
1308 else | |
1309 { | |
1310 debug writefln(o.toString()); | |
1311 assert(0); | |
1312 } | |
1313 | |
1314 if (!sc.insert(s)) | |
1315 error("declaration %s is already defined", tp.ident.toChars()); | |
1316 | |
1317 s.semantic(sc); | |
1318 } | |
1319 | |
72 | 1320 override TemplateDeclaration isTemplateDeclaration() { return this; } |
0 | 1321 |
1322 TemplateTupleParameter isVariadic() | |
1323 { | |
1324 return .isVariadic(parameters); | |
1325 } | |
1326 | |
63 | 1327 /*********************************** |
1328 * We can overload templates. | |
1329 */ | |
72 | 1330 override bool isOverloadable() |
0 | 1331 { |
63 | 1332 return 1; |
0 | 1333 } |
72 | 1334 } |