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