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