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