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