Mercurial > projects > ddmd
annotate dmd/expression/Util.d @ 79:43073c7c7769
updated to 2.035
also implemented a few missing functions
still crashes in Import.importAll though
author | Trass3r |
---|---|
date | Mon, 30 Aug 2010 03:57:51 +0200 |
parents | ef02e2e203c2 |
children | be2ab491772e |
rev | line source |
---|---|
0 | 1 module dmd.expression.Util; |
2 | |
3 import dmd.Expression; | |
4 import dmd.Loc; | |
53 | 5 import dmd.RealExp; |
0 | 6 import dmd.Scope; |
7 import dmd.FuncExp; | |
8 import dmd.DelegateExp; | |
9 import dmd.LINK; | |
10 import dmd.NullExp; | |
11 import dmd.SymOffExp; | |
12 import dmd.ExpInitializer; | |
13 import dmd.Lexer; | |
14 import dmd.TypeSArray; | |
15 import dmd.TypeArray; | |
16 import dmd.VarDeclaration; | |
17 import dmd.VoidInitializer; | |
18 import dmd.DeclarationExp; | |
19 import dmd.VarExp; | |
20 import dmd.NewExp; | |
21 import dmd.STC; | |
22 import dmd.WANT; | |
23 import dmd.IndexExp; | |
24 import dmd.AssignExp; | |
25 import dmd.CommaExp; | |
26 import dmd.Argument; | |
27 import dmd.DefaultInitExp; | |
28 import dmd.Identifier; | |
29 import dmd.Dsymbol; | |
30 import dmd.Global; | |
31 import dmd.ScopeDsymbol; | |
32 import dmd.DotIdExp; | |
33 import dmd.DotVarExp; | |
34 import dmd.CallExp; | |
35 import dmd.TY; | |
36 import dmd.MATCH; | |
53 | 37 import dmd.BUILTIN; |
0 | 38 import dmd.TypeFunction; |
39 import dmd.declaration.Match; | |
40 import dmd.ArrayTypes; | |
41 import dmd.Declaration; | |
42 import dmd.FuncAliasDeclaration; | |
43 import dmd.AliasDeclaration; | |
44 import dmd.FuncDeclaration; | |
45 import dmd.TemplateDeclaration; | |
46 import dmd.AggregateDeclaration; | |
47 import dmd.IntegerExp; | |
48 import dmd.Type; | |
49 import dmd.TOK; | |
50 import dmd.TypeExp; | |
51 import dmd.TypeTuple; | |
52 import dmd.TupleExp; | |
53 import dmd.OutBuffer; | |
54 import dmd.HdrGenState; | |
55 import dmd.ClassDeclaration; | |
56 import dmd.TypeClass; | |
57 import dmd.StructDeclaration; | |
58 import dmd.TypeStruct; | |
59 import dmd.MOD; | |
60 import dmd.PROT; | |
61 import dmd.PREC; | |
62 import dmd.Util; | |
63 import dmd.TypeAArray; | |
64 import dmd.Id; | |
65 | |
66 import std.stdio : writef; | |
67 | |
68 | |
69 /*********************************** | |
70 * Utility to build a function call out of this reference and argument. | |
71 */ | |
72 Expression build_overload(Loc loc, Scope sc, Expression ethis, Expression earg, Identifier id) | |
73 { | |
74 Expression e; | |
75 | |
76 //printf("build_overload(id = '%s')\n", id.toChars()); | |
77 //earg.print(); | |
78 //earg.type.print(); | |
79 e = new DotIdExp(loc, ethis, id); | |
80 | |
81 if (earg) | |
82 e = new CallExp(loc, e, earg); | |
83 else | |
84 e = new CallExp(loc, e); | |
85 | |
86 e = e.semantic(sc); | |
87 return e; | |
88 } | |
89 | |
90 /*************************************** | |
91 * Search for function funcid in aggregate ad. | |
92 */ | |
93 | |
94 Dsymbol search_function(ScopeDsymbol ad, Identifier funcid) | |
95 { | |
96 Dsymbol s; | |
97 FuncDeclaration fd; | |
98 TemplateDeclaration td; | |
99 | |
100 s = ad.search(Loc(0), funcid, 0); | |
101 if (s) | |
102 { | |
103 Dsymbol s2; | |
104 | |
105 //printf("search_function: s = '%s'\n", s.kind()); | |
106 s2 = s.toAlias(); | |
107 //printf("search_function: s2 = '%s'\n", s2.kind()); | |
108 fd = s2.isFuncDeclaration(); | |
109 if (fd && fd.type.ty == TY.Tfunction) | |
110 return fd; | |
111 | |
112 td = s2.isTemplateDeclaration(); | |
113 if (td) | |
114 return td; | |
115 } | |
116 | |
117 return null; | |
118 } | |
119 | |
120 /******************************************** | |
121 * Find function in overload list that exactly matches t. | |
122 */ | |
123 | |
124 /*************************************************** | |
125 * Visit each overloaded function in turn, and call | |
126 * dg(param, f) on it. | |
127 * Exit when no more, or dg(param, f) returns 1. | |
128 * Returns: | |
129 * 0 continue | |
130 * 1 done | |
131 */ | |
132 | |
17
ddae60498573
Implemented mixin statements, DefaultInitExps, FileInitExps, LineInitExps and __traits.
Robert Clipsham <robert@octarineparrot.com>
parents:
16
diff
changeset
|
133 int overloadApply(FuncDeclaration fstart, int delegate(void*, FuncDeclaration) dg, void* param) |
0 | 134 { |
135 FuncDeclaration f; | |
136 Declaration d; | |
137 Declaration next; | |
138 | |
139 for (d = fstart; d; d = next) | |
140 { | |
141 FuncAliasDeclaration fa = d.isFuncAliasDeclaration(); | |
142 | |
143 if (fa) | |
144 { | |
17
ddae60498573
Implemented mixin statements, DefaultInitExps, FileInitExps, LineInitExps and __traits.
Robert Clipsham <robert@octarineparrot.com>
parents:
16
diff
changeset
|
145 if (overloadApply(fa.funcalias, dg, param)) |
0 | 146 return 1; |
147 next = fa.overnext; | |
148 } | |
149 else | |
150 { | |
151 AliasDeclaration a = d.isAliasDeclaration(); | |
152 | |
153 if (a) | |
154 { | |
155 Dsymbol s = a.toAlias(); | |
156 next = s.isDeclaration(); | |
157 if (next is a) | |
158 break; | |
159 if (next is fstart) | |
160 break; | |
161 } | |
162 else | |
163 { | |
164 f = d.isFuncDeclaration(); | |
165 if (f is null) | |
166 { | |
167 d.error("is aliased to a function"); | |
168 break; // BUG: should print error message? | |
169 } | |
17
ddae60498573
Implemented mixin statements, DefaultInitExps, FileInitExps, LineInitExps and __traits.
Robert Clipsham <robert@octarineparrot.com>
parents:
16
diff
changeset
|
170 if (dg(param, f)) |
0 | 171 return 1; |
172 | |
173 next = f.overnext; | |
174 } | |
175 } | |
176 } | |
177 return 0; | |
178 } | |
179 | |
180 /******************************************** | |
181 * Decide which function matches the arguments best. | |
182 */ | |
183 | |
184 struct Param2 | |
185 { | |
186 Match* m; | |
187 Expression ethis; | |
188 Expressions arguments; | |
189 | |
17
ddae60498573
Implemented mixin statements, DefaultInitExps, FileInitExps, LineInitExps and __traits.
Robert Clipsham <robert@octarineparrot.com>
parents:
16
diff
changeset
|
190 int fp2(void*, FuncDeclaration f) |
0 | 191 { |
192 MATCH match; | |
193 | |
194 if (f != m.lastf) // skip duplicates | |
195 { | |
196 m.anyf = f; | |
197 TypeFunction tf = cast(TypeFunction)f.type; | |
198 match = tf.callMatch(f.needThis() ? ethis : null, arguments); | |
199 //printf("match = %d\n", match); | |
200 if (match != MATCH.MATCHnomatch) | |
201 { | |
202 if (match > m.last) | |
203 goto LfIsBetter; | |
204 | |
205 if (match < m.last) | |
206 goto LlastIsBetter; | |
207 | |
208 /* See if one of the matches overrides the other. | |
209 */ | |
210 if (m.lastf.overrides(f)) | |
211 goto LlastIsBetter; | |
212 else if (f.overrides(m.lastf)) | |
213 goto LfIsBetter; | |
214 | |
215 /* Try to disambiguate using template-style partial ordering rules. | |
216 * In essence, if f() and g() are ambiguous, if f() can call g(), | |
217 * but g() cannot call f(), then pick f(). | |
218 * This is because f() is "more specialized." | |
219 */ | |
220 { | |
221 MATCH c1 = f.leastAsSpecialized(m.lastf); | |
222 MATCH c2 = m.lastf.leastAsSpecialized(f); | |
223 //printf("c1 = %d, c2 = %d\n", c1, c2); | |
224 if (c1 > c2) | |
225 goto LfIsBetter; | |
226 if (c1 < c2) | |
227 goto LlastIsBetter; | |
228 } | |
229 | |
230 Lambiguous: | |
231 m.nextf = f; | |
232 m.count++; | |
233 return 0; | |
234 | |
235 LfIsBetter: | |
236 m.last = match; | |
237 m.lastf = f; | |
238 m.count = 1; | |
239 return 0; | |
240 | |
241 LlastIsBetter: | |
242 return 0; | |
243 } | |
244 } | |
245 return 0; | |
246 } | |
247 } | |
248 | |
249 struct Param1 | |
250 { | |
251 Type t; // type to match | |
252 FuncDeclaration f; // return value | |
253 | |
17
ddae60498573
Implemented mixin statements, DefaultInitExps, FileInitExps, LineInitExps and __traits.
Robert Clipsham <robert@octarineparrot.com>
parents:
16
diff
changeset
|
254 int fp1(void*, FuncDeclaration f) |
0 | 255 { |
256 if (t.equals(f.type)) | |
257 { | |
258 this.f = f; | |
259 return 1; | |
260 } | |
261 | |
262 version (DMDV2) { | |
263 /* Allow covariant matches, if it's just a const conversion | |
264 * of the return type | |
265 */ | |
266 if (t.ty == Tfunction) | |
267 { | |
268 TypeFunction tf = cast(TypeFunction)f.type; | |
269 if (tf.covariant(t) == 1 && | |
270 tf.nextOf().implicitConvTo(t.nextOf()) >= MATCHconst) | |
271 { | |
272 this.f = f; | |
273 return 1; | |
274 } | |
275 } | |
276 } | |
277 return 0; | |
278 } | |
279 } | |
280 | |
281 void overloadResolveX(Match* m, FuncDeclaration fstart, Expression ethis, Expressions arguments) | |
282 { | |
283 Param2 p; | |
284 p.m = m; | |
285 p.ethis = ethis; | |
286 p.arguments = arguments; | |
17
ddae60498573
Implemented mixin statements, DefaultInitExps, FileInitExps, LineInitExps and __traits.
Robert Clipsham <robert@octarineparrot.com>
parents:
16
diff
changeset
|
287 overloadApply(fstart, &p.fp2, &p); |
0 | 288 } |
289 | |
290 void templateResolve(Match* m, TemplateDeclaration td, Scope sc, Loc loc, Objects targsi, Expression ethis, Expressions arguments) | |
291 { | |
292 FuncDeclaration fd; | |
293 | |
294 assert(td); | |
295 fd = td.deduceFunctionTemplate(sc, loc, targsi, ethis, arguments); | |
296 if (!fd) | |
297 return; | |
298 m.anyf = fd; | |
299 if (m.last >= MATCH.MATCHexact) | |
300 { | |
301 m.nextf = fd; | |
302 m.count++; | |
303 } | |
304 else | |
305 { | |
306 m.last = MATCH.MATCHexact; | |
307 m.lastf = fd; | |
308 m.count = 1; | |
309 } | |
310 } | |
311 | |
312 /****************************** | |
313 * Perform semantic() on an array of Expressions. | |
314 */ | |
315 | |
316 void arrayExpressionSemantic(Expressions exps, Scope sc) | |
317 { | |
318 if (exps) | |
319 { | |
320 for (size_t i = 0; i < exps.dim; i++) | |
321 { | |
322 Expression e = cast(Expression)exps.data[i]; | |
323 | |
324 e = e.semantic(sc); | |
325 exps.data[i] = cast(void*)e; | |
326 } | |
327 } | |
328 } | |
329 | |
330 /**************************************** | |
331 * Preprocess arguments to function. | |
332 */ | |
333 | |
334 void preFunctionArguments(Loc loc, Scope sc, Expressions exps) | |
335 { | |
336 if (exps) | |
337 { | |
338 expandTuples(exps); | |
57 | 339 |
0 | 340 for (size_t i = 0; i < exps.dim; i++) |
341 { | |
342 Expression arg = cast(Expression)exps.data[i]; | |
343 | |
344 if (!arg.type) | |
345 { | |
346 debug { | |
57 | 347 if (!global.gag) { |
0 | 348 writef("1: \n"); |
57 | 349 } |
0 | 350 } |
351 arg.error("%s is not an expression", arg.toChars()); | |
352 arg = new IntegerExp(arg.loc, 0, Type.tint32); | |
353 } | |
354 | |
355 arg = resolveProperties(sc, arg); | |
356 exps.data[i] = cast(void*) arg; | |
357 | |
358 //arg.rvalue(); | |
359 static if (false) { | |
360 if (arg.type.ty == TY.Tfunction) | |
361 { | |
362 arg = new AddrExp(arg.loc, arg); | |
363 arg = arg.semantic(sc); | |
364 exps.data[i] = cast(void*) arg; | |
365 } | |
366 } | |
367 } | |
368 } | |
369 } | |
370 | |
371 /************************************************************* | |
372 * Given var, we need to get the | |
373 * right 'this' pointer if var is in an outer class, but our | |
374 * existing 'this' pointer is in an inner class. | |
375 * Input: | |
376 * e1 existing 'this' | |
377 * ad struct or class we need the correct 'this' for | |
378 * var the specific member of ad we're accessing | |
379 */ | |
380 | |
381 Expression getRightThis(Loc loc, Scope sc, AggregateDeclaration ad, Expression e1, Declaration var) | |
382 { | |
383 //printf("\ngetRightThis(e1 = %s, ad = %s, var = %s)\n", e1.toChars(), ad.toChars(), var.toChars()); | |
384 L1: | |
385 Type t = e1.type.toBasetype(); | |
386 //printf("e1.type = %s, var.type = %s\n", e1.type.toChars(), var.type.toChars()); | |
387 | |
388 /* If e1 is not the 'this' pointer for ad | |
389 */ | |
390 if (ad && !(t.ty == TY.Tpointer && t.nextOf().ty == TY.Tstruct && (cast(TypeStruct)t.nextOf()).sym == ad) && !(t.ty == TY.Tstruct && (cast(TypeStruct)t).sym == ad)) | |
391 { | |
392 ClassDeclaration cd = ad.isClassDeclaration(); | |
393 ClassDeclaration tcd = t.isClassHandle(); | |
394 | |
395 /* e1 is the right this if ad is a base class of e1 | |
396 */ | |
397 if (!cd || !tcd || !(tcd == cd || cd.isBaseOf(tcd, null))) | |
398 { | |
399 /* Only classes can be inner classes with an 'outer' | |
400 * member pointing to the enclosing class instance | |
401 */ | |
402 if (tcd && tcd.isNested()) | |
403 { | |
404 /* e1 is the 'this' pointer for an inner class: tcd. | |
405 * Rewrite it as the 'this' pointer for the outer class. | |
406 */ | |
407 | |
408 e1 = new DotVarExp(loc, e1, tcd.vthis); | |
409 e1.type = tcd.vthis.type; | |
410 // Do not call checkNestedRef() | |
411 //e1 = e1.semantic(sc); | |
412 | |
413 // Skip up over nested functions, and get the enclosing | |
414 // class type. | |
415 int n = 0; | |
416 Dsymbol s; | |
417 for (s = tcd.toParent(); s && s.isFuncDeclaration(); s = s.toParent()) | |
418 { | |
419 FuncDeclaration f = s.isFuncDeclaration(); | |
420 if (f.vthis) | |
421 { | |
422 //printf("rewriting e1 to %s's this\n", f.toChars()); | |
423 n++; | |
424 e1 = new VarExp(loc, f.vthis); | |
425 } | |
426 } | |
427 if (s && s.isClassDeclaration()) | |
428 { | |
429 e1.type = s.isClassDeclaration().type; | |
430 if (n > 1) | |
431 e1 = e1.semantic(sc); | |
432 } | |
433 else | |
434 e1 = e1.semantic(sc); | |
435 goto L1; | |
436 } | |
437 /* Can't find a path from e1 to ad | |
438 */ | |
439 e1.error("this for %s needs to be type %s not type %s", var.toChars(), ad.toChars(), t.toChars()); | |
440 } | |
441 } | |
442 return e1; | |
443 } | |
444 | |
445 /******************************************* | |
446 * Given a symbol that could be either a FuncDeclaration or | |
447 * a function template, resolve it to a function symbol. | |
448 * sc instantiation scope | |
449 * loc instantiation location | |
450 * targsi initial list of template arguments | |
451 * ethis if !null, the 'this' pointer argument | |
452 * fargs arguments to function | |
453 * flags 1: do not issue error message on no match, just return null | |
454 */ | |
455 | |
456 FuncDeclaration resolveFuncCall(Scope sc, Loc loc, Dsymbol s, | |
457 Objects tiargs, | |
458 Expression ethis, | |
459 Expressions arguments, | |
460 int flags) | |
461 { | |
462 if (!s) | |
463 return null; // no match | |
464 FuncDeclaration f = s.isFuncDeclaration(); | |
465 if (f) | |
466 f = f.overloadResolve(loc, ethis, arguments); | |
467 else | |
468 { | |
469 TemplateDeclaration td = s.isTemplateDeclaration(); | |
470 assert(td); | |
471 f = td.deduceFunctionTemplate(sc, loc, tiargs, null, arguments, flags); | |
472 } | |
473 return f; | |
474 } | |
475 | |
476 /**************************************** | |
477 * Now that we know the exact type of the function we're calling, | |
478 * the arguments[] need to be adjusted: | |
479 * 1. implicitly convert argument to the corresponding parameter type | |
480 * 2. add default arguments for any missing arguments | |
481 * 3. do default promotions on arguments corresponding to ... | |
482 * 4. add hidden _arguments[] argument | |
483 * 5. call copy constructor for struct value arguments | |
484 */ | |
485 | |
486 void functionArguments(Loc loc, Scope sc, TypeFunction tf, Expressions arguments) | |
487 { | |
488 uint n; | |
489 | |
490 //printf("functionArguments()\n"); | |
491 assert(arguments); | |
492 size_t nargs = arguments ? arguments.dim : 0; | |
493 size_t nparams = Argument.dim(tf.parameters); | |
494 | |
495 if (nargs > nparams && tf.varargs == 0) | |
496 error(loc, "expected %zu arguments, not %zu for non-variadic function type %s", nparams, nargs, tf.toChars()); | |
497 | |
498 n = (nargs > nparams) ? nargs : nparams; // n = max(nargs, nparams) | |
499 | |
500 int done = 0; | |
501 for (size_t i = 0; i < n; i++) | |
502 { | |
503 Expression arg; | |
504 | |
505 if (i < nargs) | |
506 arg = cast(Expression)arguments.data[i]; | |
507 else | |
508 arg = null; | |
509 | |
510 Type tb; | |
511 | |
512 if (i < nparams) | |
513 { | |
514 Argument p = Argument.getNth(tf.parameters, i); | |
515 | |
516 if (!arg) | |
517 { | |
518 if (!p.defaultArg) | |
519 { | |
520 if (tf.varargs == 2 && i + 1 == nparams) | |
521 goto L2; | |
522 | |
523 error(loc, "expected %d function arguments, not %d", nparams, nargs); | |
524 break; | |
525 } | |
526 arg = p.defaultArg; | |
527 version (DMDV2) { | |
528 if (arg.op == TOK.TOKdefault) | |
529 { | |
530 DefaultInitExp de = cast(DefaultInitExp)arg; | |
531 arg = de.resolve(loc, sc); | |
532 } | |
533 else | |
534 { | |
535 arg = arg.copy(); | |
536 } | |
537 } else { | |
538 arg = arg.copy(); | |
539 } | |
540 arguments.push(cast(void*)arg); | |
541 nargs++; | |
542 } | |
543 | |
544 if (tf.varargs == 2 && i + 1 == nparams) | |
545 { | |
546 //printf("\t\tvarargs == 2, p.type = '%s'\n", p.type.toChars()); | |
547 if (arg.implicitConvTo(p.type)) | |
548 { | |
549 if (nargs != nparams) | |
550 error(loc, "expected %zu function arguments, not %zu", nparams, nargs); | |
551 goto L1; | |
552 } | |
553 L2: | |
554 tb = p.type.toBasetype(); /// | |
555 Type tret = p.isLazyArray(); | |
556 switch (tb.ty) | |
557 { | |
558 case TY.Tsarray: | |
559 case TY.Tarray: | |
560 { // Create a static array variable v of type arg.type | |
561 version (IN_GCC) { | |
562 /* GCC 4.0 does not like zero length arrays used like | |
563 this; pass a null array value instead. Could also | |
564 just make a one-element array. */ | |
565 if (nargs - i == 0) | |
566 { | |
567 arg = new NullExp(loc); | |
568 break; | |
569 } | |
570 } | |
571 Identifier id = Lexer.uniqueId("__arrayArg"); | |
572 Type t = new TypeSArray((cast(TypeArray)tb).next, new IntegerExp(nargs - i)); | |
573 t = t.semantic(loc, sc); | |
574 VarDeclaration v = new VarDeclaration(loc, t, id, new VoidInitializer(loc)); | |
575 v.semantic(sc); | |
576 v.parent = sc.parent; | |
577 //sc.insert(v); | |
578 | |
579 Expression c = new DeclarationExp(Loc(0), v); | |
580 c.type = v.type; | |
581 | |
582 for (size_t u = i; u < nargs; u++) | |
583 { | |
584 Expression a = cast(Expression)arguments.data[u]; | |
585 if (tret && !(cast(TypeArray)tb).next.equals(a.type)) | |
586 a = a.toDelegate(sc, tret); | |
587 | |
588 Expression e = new VarExp(loc, v); | |
589 e = new IndexExp(loc, e, new IntegerExp(u + 1 - nparams)); | |
590 AssignExp ae = new AssignExp(loc, e, a); | |
591 | |
592 version (DMDV2) { | |
593 ae.op = TOK.TOKconstruct; | |
594 } | |
595 | |
596 if (c) | |
597 c = new CommaExp(loc, c, ae); | |
598 else | |
599 c = ae; | |
600 } | |
601 | |
602 arg = new VarExp(loc, v); | |
603 if (c) | |
604 arg = new CommaExp(loc, c, arg); | |
605 break; | |
606 } | |
607 | |
608 case TY.Tclass: | |
609 { /* Set arg to be: | |
610 * new Tclass(arg0, arg1, ..., argn) | |
611 */ | |
612 Expressions args = new Expressions(); | |
613 args.setDim(nargs - i); | |
614 for (size_t u = i; u < nargs; u++) | |
615 args.data[u - i] = arguments.data[u]; | |
616 arg = new NewExp(loc, null, null, p.type, args); | |
617 break; | |
618 } | |
619 | |
620 default: | |
621 if (!arg) | |
622 { | |
623 error(loc, "not enough arguments"); | |
624 return; | |
625 } | |
626 break; | |
627 } | |
628 | |
629 arg = arg.semantic(sc); | |
630 //printf("\targ = '%s'\n", arg.toChars()); | |
631 arguments.setDim(i + 1); | |
632 done = 1; | |
633 } | |
634 | |
635 L1: | |
636 if (!(p.storageClass & STC.STClazy && p.type.ty == TY.Tvoid)) | |
637 { | |
638 if (p.type != arg.type) | |
639 { | |
640 //printf("arg.type = %s, p.type = %s\n", arg.type.toChars(), p.type.toChars()); | |
641 arg = arg.implicitCastTo(sc, p.type); | |
642 arg = arg.optimize(WANT.WANTvalue); | |
643 } | |
644 } | |
645 if (p.storageClass & STC.STCref) | |
646 { | |
647 arg = arg.toLvalue(sc, arg); | |
648 } | |
649 else if (p.storageClass & STC.STCout) | |
650 { | |
651 arg = arg.modifiableLvalue(sc, arg); | |
652 } | |
653 | |
654 // Convert static arrays to pointers | |
655 tb = arg.type.toBasetype(); | |
656 if (tb.ty == TY.Tsarray) | |
657 { | |
658 arg = arg.checkToPointer(); | |
659 } | |
660 version (DMDV2) { | |
661 if (tb.ty == TY.Tstruct && !(p.storageClass & (STC.STCref | STC.STCout))) | |
662 { | |
663 arg = callCpCtor(loc, sc, arg); | |
664 } | |
665 } | |
666 | |
667 // Convert lazy argument to a delegate | |
668 if (p.storageClass & STC.STClazy) | |
669 { | |
670 arg = arg.toDelegate(sc, p.type); | |
671 } | |
672 version (DMDV2) { | |
673 /* Look for arguments that cannot 'escape' from the called | |
674 * function. | |
675 */ | |
676 if (!tf.parameterEscapes(p)) | |
677 { | |
678 /* Function literals can only appear once, so if this | |
679 * appearance was scoped, there cannot be any others. | |
680 */ | |
681 if (arg.op == TOK.TOKfunction) | |
682 { | |
683 FuncExp fe = cast(FuncExp)arg; | |
684 fe.fd.tookAddressOf = 0; | |
685 } | |
686 | |
687 /* For passing a delegate to a scoped parameter, | |
688 * this doesn't count as taking the address of it. | |
689 * We only worry about 'escaping' references to the function. | |
690 */ | |
691 else if (arg.op == TOK.TOKdelegate) | |
692 { | |
693 DelegateExp de = cast(DelegateExp)arg; | |
694 if (de.e1.op == TOK.TOKvar) | |
695 { | |
696 VarExp ve = cast(VarExp)de.e1; | |
697 FuncDeclaration f = ve.var.isFuncDeclaration(); | |
698 if (f) | |
699 { | |
700 f.tookAddressOf--; | |
701 //printf("tookAddressOf = %d\n", f.tookAddressOf); | |
702 } | |
703 } | |
704 } | |
705 } | |
706 } | |
707 } | |
708 else | |
709 { | |
710 // If not D linkage, do promotions | |
711 if (tf.linkage != LINK.LINKd) | |
712 { | |
713 // Promote bytes, words, etc., to ints | |
714 arg = arg.integralPromotions(sc); | |
715 | |
716 // Promote floats to doubles | |
717 switch (arg.type.ty) | |
718 { | |
719 case TY.Tfloat32: | |
720 arg = arg.castTo(sc, Type.tfloat64); | |
721 break; | |
722 | |
723 case TY.Timaginary32: | |
724 arg = arg.castTo(sc, Type.timaginary64); | |
725 break; | |
726 default: | |
727 break; | |
728 } | |
729 } | |
730 | |
731 // Convert static arrays to dynamic arrays | |
732 tb = arg.type.toBasetype(); | |
733 if (tb.ty == TY.Tsarray) | |
734 { | |
735 TypeSArray ts = cast(TypeSArray)tb; | |
736 Type ta = ts.next.arrayOf(); | |
737 if (ts.size(arg.loc) == 0) | |
738 { | |
739 arg = new NullExp(arg.loc); | |
740 arg.type = ta; | |
741 } | |
742 else | |
743 { | |
744 arg = arg.castTo(sc, ta); | |
745 } | |
746 } | |
747 version (DMDV2) { | |
73 | 748 if (tb.ty == Tstruct) |
0 | 749 { |
750 arg = callCpCtor(loc, sc, arg); | |
751 } | |
73 | 752 } |
0 | 753 |
73 | 754 // Give error for overloaded function addresses |
755 if (arg.op == TOKsymoff) | |
756 { | |
757 SymOffExp se = cast(SymOffExp)arg; | |
758 version (DMDV2) { | |
759 bool aux = (se.hasOverloads != 0); | |
760 } else { | |
761 bool aux = true; | |
0 | 762 } |
73 | 763 if (aux && !se.var.isFuncDeclaration().isUnique()) |
764 arg.error("function %s is overloaded", arg.toChars()); | |
765 } | |
766 | |
767 arg.rvalue(); | |
768 } | |
769 | |
0 | 770 arg = arg.optimize(WANT.WANTvalue); |
771 arguments.data[i] = cast(void*) arg; | |
772 if (done) | |
773 break; | |
774 } | |
775 | |
776 // If D linkage and variadic, add _arguments[] as first argument | |
777 if (tf.linkage == LINK.LINKd && tf.varargs == 1) | |
778 { | |
779 Expression e = createTypeInfoArray(sc, cast(Expression*)&arguments.data[nparams], arguments.dim - nparams); | |
780 arguments.insert(0, cast(void*)e); | |
781 } | |
782 } | |
783 | |
16
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
784 /****************************** |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
785 * Perform canThrow() on an array of Expressions. |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
786 */ |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
787 |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
788 version (DMDV2) { |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
789 bool arrayExpressionCanThrow(Expressions exps) |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
790 { |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
791 if (exps) |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
792 { |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
793 for (size_t i = 0; i < exps.dim; i++) |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
794 { Expression e = cast(Expression)exps.data[i]; |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
795 if (e && e.canThrow()) |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
796 return true; |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
797 } |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
798 } |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
799 return false; |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
800 } |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
801 } |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
802 |
0 | 803 /**************************************** |
804 * Expand tuples. | |
805 */ | |
806 | |
807 void expandTuples(Expressions exps) | |
808 { | |
809 //printf("expandTuples()\n"); | |
810 if (exps) | |
811 { | |
812 for (size_t i = 0; i < exps.dim; i++) | |
813 { | |
814 Expression arg = cast(Expression)exps.data[i]; | |
815 if (!arg) | |
816 continue; | |
817 | |
818 // Look for tuple with 0 members | |
819 if (arg.op == TOK.TOKtype) | |
57 | 820 { |
0 | 821 TypeExp e = cast(TypeExp)arg; |
822 if (e.type.toBasetype().ty == TY.Ttuple) | |
823 { | |
824 TypeTuple tt = cast(TypeTuple)e.type.toBasetype(); | |
825 | |
826 if (!tt.arguments || tt.arguments.dim == 0) | |
827 { | |
828 exps.remove(i); | |
829 if (i == exps.dim) | |
830 return; | |
831 i--; | |
832 continue; | |
833 } | |
834 } | |
835 } | |
836 | |
837 // Inline expand all the tuples | |
838 while (arg.op == TOK.TOKtuple) | |
839 { | |
840 TupleExp te = cast(TupleExp)arg; | |
841 | |
842 exps.remove(i); // remove arg | |
57 | 843 exps.insert(i, te.exps); // replace with tuple contents |
0 | 844 |
845 if (i == exps.dim) | |
846 return; // empty tuple, no more arguments | |
847 | |
848 arg = cast(Expression)exps.data[i]; | |
849 } | |
850 } | |
851 } | |
852 } | |
853 | |
854 /************************************************** | |
855 * Write out argument types to buf. | |
856 */ | |
857 | |
858 void argExpTypesToCBuffer(OutBuffer buf, Expressions arguments, HdrGenState* hgs) | |
859 { | |
860 if (arguments) | |
861 { | |
862 scope OutBuffer argbuf = new OutBuffer(); | |
863 | |
864 for (size_t i = 0; i < arguments.dim; i++) | |
865 { | |
866 Expression arg = cast(Expression)arguments.data[i]; | |
867 | |
868 if (i) | |
869 buf.writeByte(','); | |
870 | |
871 argbuf.reset(); | |
872 arg.type.toCBuffer2(argbuf, hgs, MOD.MODundefined); | |
873 buf.write(argbuf); | |
874 } | |
875 } | |
876 } | |
877 | |
878 /**************************************** | |
879 * Determine if scope sc has package level access to s. | |
880 */ | |
881 | |
882 bool hasPackageAccess(Scope sc, Dsymbol s) | |
883 { | |
884 version (LOG) { | |
885 printf("hasPackageAccess(s = '%s', sc = '%p')\n", s.toChars(), sc); | |
886 } | |
887 | |
888 for (; s; s = s.parent) | |
889 { | |
890 if (s.isPackage() && !s.isModule()) | |
891 break; | |
892 } | |
893 version (LOG) { | |
894 if (s) | |
895 printf("\tthis is in package '%s'\n", s.toChars()); | |
896 } | |
897 | |
898 if (s && s == sc.module_.parent) | |
899 { | |
900 version (LOG) { | |
901 printf("\ts is in same package as sc\n"); | |
902 } | |
903 return true; | |
904 } | |
905 | |
906 | |
907 version (LOG) { | |
908 printf("\tno package access\n"); | |
909 } | |
910 | |
911 return false; | |
912 } | |
913 | |
914 /********************************************* | |
915 * Call copy constructor for struct value argument. | |
916 */ | |
917 version (DMDV2) { | |
918 Expression callCpCtor(Loc loc, Scope sc, Expression e) | |
919 { | |
920 Type tb = e.type.toBasetype(); | |
921 assert(tb.ty == Tstruct); | |
922 StructDeclaration sd = (cast(TypeStruct)tb).sym; | |
923 if (sd.cpctor) | |
924 { | |
925 /* Create a variable tmp, and replace the argument e with: | |
926 * (tmp = e),tmp | |
927 * and let AssignExp() handle the construction. | |
928 * This is not the most efficent, ideally tmp would be constructed | |
929 * directly onto the stack. | |
930 */ | |
931 Identifier idtmp = Lexer.uniqueId("__tmp"); | |
932 VarDeclaration tmp = new VarDeclaration(loc, tb, idtmp, new ExpInitializer(Loc(0), e)); | |
933 Expression ae = new DeclarationExp(loc, tmp); | |
934 e = new CommaExp(loc, ae, new VarExp(loc, tmp)); | |
935 e = e.semantic(sc); | |
936 } | |
937 return e; | |
938 } | |
939 } | |
940 | |
941 /*************************************** | |
942 * Create a static array of TypeInfo references | |
943 * corresponding to an array of Expression's. | |
944 * Used to supply hidden _arguments[] value for variadic D functions. | |
945 */ | |
946 | |
947 Expression createTypeInfoArray(Scope sc, Expression* exps, int dim) | |
948 { | |
79 | 949 static if (true) |
950 { | |
57 | 951 /* Get the corresponding TypeInfo_Tuple and |
952 * point at its elements[]. | |
953 */ | |
954 | |
955 /* Create the TypeTuple corresponding to the types of args[] | |
956 */ | |
957 Arguments args = new Arguments; | |
958 args.setDim(dim); | |
959 for (size_t i = 0; i < dim; i++) | |
960 { | |
961 Argument arg = new Argument(STCin, exps[i].type, null, null); | |
962 args.data[i] = cast(void*)arg; | |
963 } | |
964 TypeTuple tup = new TypeTuple(args); | |
965 Expression e = tup.getTypeInfo(sc); | |
966 e = e.optimize(WANTvalue); | |
967 assert(e.op == TOKsymoff); // should be SymOffExp | |
968 | |
79 | 969 version (BREAKABI) |
970 { | |
57 | 971 /* |
972 * Should just pass a reference to TypeInfo_Tuple instead, | |
973 * but that would require existing code to be recompiled. | |
974 * Source compatibility can be maintained by computing _arguments[] | |
975 * at the start of the called function by offseting into the | |
976 * TypeInfo_Tuple reference. | |
977 */ | |
978 | |
79 | 979 } |
980 else | |
981 { | |
57 | 982 // Advance to elements[] member of TypeInfo_Tuple |
983 SymOffExp se = cast(SymOffExp)e; | |
984 se.offset += PTRSIZE + PTRSIZE; | |
985 | |
986 // Set type to TypeInfo[]* | |
987 se.type = Type.typeinfo.type.arrayOf().pointerTo(); | |
988 | |
989 // Indirect to get the _arguments[] value | |
990 e = new PtrExp(Loc(0), se); | |
991 e.type = se.type.next; | |
992 } | |
993 return e; | |
79 | 994 } // of static if (true) |
995 else | |
996 { | |
57 | 997 /* Improvements: |
998 * 1) create an array literal instead, | |
999 * as it would eliminate the extra dereference of loading the | |
1000 * static variable. | |
1001 */ | |
1002 | |
1003 ArrayInitializer ai = new ArrayInitializer(Loc(0)); | |
1004 VarDeclaration v; | |
1005 Type t; | |
1006 Expression e; | |
1007 scope OutBuffer buf = new OutBuffer(); | |
1008 Identifier id; | |
1009 string name; | |
1010 | |
1011 // Generate identifier for _arguments[] | |
1012 buf.writestring("_arguments_"); | |
1013 for (int i = 0; i < dim; i++) | |
1014 { | |
1015 t = exps[i].type; | |
1016 t.toDecoBuffer(buf); | |
1017 } | |
1018 buf.writeByte(0); | |
1019 id = Lexer.idPool(buf.extractString()); | |
1020 | |
1021 Module m = sc.module_; | |
1022 Dsymbol s = m.symtab.lookup(id); | |
1023 | |
1024 if (s && s.parent == m) | |
1025 { | |
1026 // Use existing one | |
1027 v = s.isVarDeclaration(); | |
1028 assert(v); | |
1029 } | |
1030 else | |
1031 { | |
1032 // Generate new one | |
1033 | |
1034 for (int i = 0; i < dim; i++) | |
1035 { | |
1036 t = exps[i].type; | |
1037 e = t.getTypeInfo(sc); | |
1038 ai.addInit(new IntegerExp(i), new ExpInitializer(0, e)); | |
1039 } | |
1040 | |
1041 t = Type.typeinfo.type.arrayOf(); | |
1042 ai.type = t; | |
1043 v = new VarDeclaration(0, t, id, ai); | |
1044 m.members.push(v); | |
79 | 1045 m.symtabInsert(v); |
57 | 1046 sc = sc.push(); |
1047 sc.linkage = LINKc; | |
1048 sc.stc = STCstatic | STCcomdat; | |
1049 ai.semantic(sc, t); | |
1050 v.semantic(sc); | |
1051 v.parent = m; | |
1052 sc = sc.pop(); | |
1053 } | |
1054 e = new VarExp(0, v); | |
1055 e = e.semantic(sc); | |
1056 return e; | |
1057 } | |
0 | 1058 } |
1059 | |
1060 /************************************** | |
1061 * Evaluate builtin function. | |
1062 * Return result: null if cannot evaluate it. | |
1063 */ | |
1064 | |
53 | 1065 extern(C) extern real sinl(real); |
1066 extern(C) extern real cosl(real); | |
1067 extern(C) extern real tanl(real); | |
1068 extern(C) extern real sqrtl(real); | |
1069 extern(C) extern real fabsl(real); | |
1070 | |
0 | 1071 Expression eval_builtin(BUILTIN builtin, Expressions arguments) |
1072 { | |
53 | 1073 assert(arguments && arguments.dim); |
1074 Expression arg0 = cast(Expression)arguments.data[0]; | |
1075 Expression e = null; | |
1076 switch (builtin) | |
1077 { | |
1078 case BUILTINsin: | |
1079 if (arg0.op == TOKfloat64) | |
1080 e = new RealExp(Loc(0), sinl(arg0.toReal()), arg0.type); | |
1081 break; | |
1082 | |
1083 case BUILTINcos: | |
1084 if (arg0.op == TOKfloat64) | |
1085 e = new RealExp(Loc(0), cosl(arg0.toReal()), arg0.type); | |
1086 break; | |
1087 | |
1088 case BUILTINtan: | |
1089 if (arg0.op == TOKfloat64) | |
1090 e = new RealExp(Loc(0), tanl(arg0.toReal()), arg0.type); | |
1091 break; | |
1092 | |
1093 case BUILTINsqrt: | |
1094 if (arg0.op == TOKfloat64) | |
1095 e = new RealExp(Loc(0), sqrtl(arg0.toReal()), arg0.type); | |
1096 break; | |
1097 | |
1098 case BUILTINfabs: | |
1099 if (arg0.op == TOKfloat64) | |
1100 e = new RealExp(Loc(0), fabsl(arg0.toReal()), arg0.type); | |
1101 break; | |
1102 } | |
1103 return e; | |
0 | 1104 } |
1105 | |
1106 Expression fromConstInitializer(int result, Expression e1) | |
1107 { | |
1108 //printf("fromConstInitializer(result = %x, %s)\n", result, e1.toChars()); | |
1109 //static int xx; if (xx++ == 10) assert(0); | |
1110 Expression e = e1; | |
1111 if (e1.op == TOK.TOKvar) | |
1112 { | |
1113 VarExp ve = cast(VarExp)e1; | |
1114 VarDeclaration v = ve.var.isVarDeclaration(); | |
1115 e = expandVar(result, v); | |
1116 if (e) | |
1117 { | |
1118 if (e.type != e1.type) | |
1119 { | |
1120 // Type 'paint' operation | |
1121 e = e.copy(); | |
1122 e.type = e1.type; | |
1123 } | |
79 | 1124 e.loc = e1.loc; |
0 | 1125 } |
1126 else | |
1127 { | |
1128 e = e1; | |
1129 } | |
1130 } | |
1131 return e; | |
1132 } | |
1133 | |
1134 /************************************* | |
1135 * If variable has a const initializer, | |
1136 * return that initializer. | |
1137 */ | |
1138 | |
1139 Expression expandVar(int result, VarDeclaration v) | |
1140 { | |
1141 //printf("expandVar(result = %d, v = %p, %s)\n", result, v, v ? v.toChars() : "null"); | |
1142 | |
1143 Expression e = null; | |
1144 if (!v) | |
1145 return e; | |
1146 | |
1147 if (v.isConst() || v.isInvariant() || v.storage_class & STC.STCmanifest) | |
1148 { | |
1149 if (!v.type) | |
1150 { | |
1151 //error("ICE"); | |
1152 return e; | |
1153 } | |
1154 | |
1155 Type tb = v.type.toBasetype(); | |
1156 if (result & WANT.WANTinterpret || v.storage_class & STC.STCmanifest || (tb.ty != TY.Tsarray && tb.ty != TY.Tstruct)) | |
1157 { | |
1158 if (v.init) | |
1159 { | |
1160 if (v.inuse) | |
1161 { | |
1162 if (v.storage_class & STC.STCmanifest) | |
1163 v.error("recursive initialization of constant"); | |
1164 goto L1; | |
1165 } | |
1166 Expression ei = v.init.toExpression(); | |
1167 if (!ei) | |
1168 goto L1; | |
1169 if (ei.op == TOK.TOKconstruct || ei.op == TOK.TOKblit) | |
1170 { | |
1171 AssignExp ae = cast(AssignExp)ei; | |
1172 ei = ae.e2; | |
1173 if (ei.isConst() != 1 && ei.op != TOK.TOKstring) | |
1174 goto L1; | |
1175 if (ei.type != v.type) | |
1176 goto L1; | |
1177 } | |
1178 if (v.scope_) | |
1179 { | |
1180 v.inuse++; | |
1181 e = ei.syntaxCopy(); | |
1182 e = e.semantic(v.scope_); | |
1183 e = e.implicitCastTo(v.scope_, v.type); | |
1184 // enabling this line causes test22 in test suite to fail | |
1185 //ei.type = e.type; | |
1186 v.scope_ = null; | |
1187 v.inuse--; | |
1188 } | |
1189 else if (!ei.type) | |
1190 { | |
1191 goto L1; | |
1192 } | |
1193 else | |
1194 // Should remove the copy() operation by | |
1195 // making all mods to expressions copy-on-write | |
1196 e = ei.copy(); | |
1197 } | |
1198 else | |
1199 { | |
1200 static if (true) { | |
1201 goto L1; | |
1202 } else { | |
1203 // BUG: what if const is initialized in constructor? | |
1204 e = v.type.defaultInit(); | |
1205 e.loc = e1.loc; | |
1206 } | |
1207 } | |
1208 if (e.type != v.type) | |
1209 { | |
1210 e = e.castTo(null, v.type); | |
1211 } | |
1212 v.inuse++; | |
1213 e = e.optimize(result); | |
1214 v.inuse--; | |
1215 } | |
1216 } | |
1217 L1: | |
1218 //if (e) printf("\te = %s, e.type = %s\n", e.toChars(), e.type.toChars()); | |
1219 return e; | |
1220 } | |
1221 | |
1222 /**************************************** | |
1223 * Check access to d for expression e.d | |
1224 */ | |
1225 | |
1226 void accessCheck(Loc loc, Scope sc, Expression e, Declaration d) | |
1227 { | |
1228 version (LOG) { | |
1229 if (e) | |
1230 { | |
1231 printf("accessCheck(%s . %s)\n", e.toChars(), d.toChars()); | |
1232 printf("\te.type = %s\n", e.type.toChars()); | |
1233 } | |
1234 else | |
1235 { | |
1236 //printf("accessCheck(%s)\n", d.toChars()); | |
1237 } | |
1238 } | |
1239 if (!e) | |
1240 { | |
1241 if (d.prot() == PROT.PROTprivate && d.getModule() != sc.module_ || | |
1242 d.prot() == PROT.PROTpackage && !hasPackageAccess(sc, d)) | |
1243 | |
1244 error(loc, "%s %s.%s is not accessible from %s", | |
1245 d.kind(), d.getModule().toChars(), d.toChars(), sc.module_.toChars()); | |
1246 } | |
1247 else if (e.type.ty == TY.Tclass) | |
1248 { | |
1249 // Do access check | |
1250 ClassDeclaration cd; | |
1251 | |
1252 cd = cast(ClassDeclaration)((cast(TypeClass)e.type).sym); | |
1253 static if (true) { | |
1254 if (e.op == TOK.TOKsuper) | |
1255 { | |
1256 ClassDeclaration cd2 = sc.func.toParent().isClassDeclaration(); | |
1257 if (cd2) | |
1258 cd = cd2; | |
1259 } | |
1260 } | |
1261 cd.accessCheck(loc, sc, d); | |
1262 } | |
1263 else if (e.type.ty == TY.Tstruct) | |
1264 { | |
1265 // Do access check | |
1266 StructDeclaration cd = cast(StructDeclaration)((cast(TypeStruct)e.type).sym); | |
1267 cd.accessCheck(loc, sc, d); | |
1268 } | |
1269 } | |
1270 | |
1271 /***************************************** | |
1272 * Given array of arguments and an aggregate type, | |
1273 * if any of the argument types are missing, attempt to infer | |
1274 * them from the aggregate type. | |
1275 */ | |
1276 | |
1277 void inferApplyArgTypes(TOK op, Arguments arguments, Expression aggr) | |
1278 { | |
1279 if (!arguments || !arguments.dim) | |
1280 return; | |
1281 | |
1282 /* Return if no arguments need types. | |
1283 */ | |
1284 for (size_t u = 0; 1; u++) | |
1285 { | |
1286 if (u == arguments.dim) | |
1287 return; | |
1288 | |
1289 Argument arg = cast(Argument)arguments.data[u]; | |
1290 if (!arg.type) | |
1291 break; | |
1292 } | |
1293 | |
1294 AggregateDeclaration ad; | |
1295 | |
1296 Argument arg = cast(Argument)arguments.data[0]; | |
1297 Type taggr = aggr.type; | |
1298 if (!taggr) | |
1299 return; | |
1300 Type tab = taggr.toBasetype(); | |
1301 switch (tab.ty) | |
1302 { | |
1303 case TY.Tarray: | |
1304 case TY.Tsarray: | |
1305 case TY.Ttuple: | |
1306 if (arguments.dim == 2) | |
1307 { | |
1308 if (!arg.type) | |
1309 arg.type = Type.tsize_t; // key type | |
1310 arg = cast(Argument)arguments.data[1]; | |
1311 } | |
1312 if (!arg.type && tab.ty != TY.Ttuple) | |
1313 arg.type = tab.nextOf(); // value type | |
1314 break; | |
1315 | |
1316 case TY.Taarray: | |
1317 { | |
1318 TypeAArray taa = cast(TypeAArray)tab; | |
1319 | |
1320 if (arguments.dim == 2) | |
1321 { | |
1322 if (!arg.type) | |
1323 arg.type = taa.index; // key type | |
1324 arg = cast(Argument)arguments.data[1]; | |
1325 } | |
1326 if (!arg.type) | |
1327 arg.type = taa.next; // value type | |
1328 break; | |
1329 } | |
1330 | |
1331 case TY.Tclass: | |
1332 ad = (cast(TypeClass)tab).sym; | |
1333 goto Laggr; | |
1334 | |
1335 case TY.Tstruct: | |
1336 ad = (cast(TypeStruct)tab).sym; | |
1337 goto Laggr; | |
1338 | |
1339 Laggr: | |
1340 if (arguments.dim == 1) | |
1341 { | |
1342 if (!arg.type) | |
1343 { | |
1344 /* Look for a head() or rear() overload | |
1345 */ | |
1346 Identifier id = (op == TOK.TOKforeach) ? Id.Fhead : Id.Ftoe; | |
1347 Dsymbol s = search_function(ad, id); | |
1348 FuncDeclaration fd = s ? s.isFuncDeclaration() : null; | |
1349 if (!fd) | |
1350 { | |
1351 if (s && s.isTemplateDeclaration()) | |
1352 break; | |
1353 goto Lapply; | |
1354 } | |
1355 arg.type = fd.type.nextOf(); | |
1356 } | |
1357 break; | |
1358 } | |
1359 | |
1360 Lapply: | |
1361 { /* Look for an | |
1362 * int opApply(int delegate(ref Type [, ...]) dg); | |
1363 * overload | |
1364 */ | |
1365 Dsymbol s = search_function(ad, (op == TOK.TOKforeach_reverse) ? Id.applyReverse : Id.apply); | |
1366 if (s) | |
1367 { | |
1368 FuncDeclaration fd = s.isFuncDeclaration(); | |
1369 if (fd) | |
1370 { | |
1371 inferApplyArgTypesX(fd, arguments); | |
1372 break; | |
1373 } | |
1374 static if (false) { | |
1375 TemplateDeclaration td = s.isTemplateDeclaration(); | |
1376 if (td) | |
1377 { | |
1378 inferApplyArgTypesZ(td, arguments); | |
1379 break; | |
1380 } | |
1381 } | |
1382 } | |
1383 break; | |
1384 } | |
1385 | |
1386 case TY.Tdelegate: | |
1387 { | |
1388 if (0 && aggr.op == TOK.TOKdelegate) | |
1389 { | |
1390 DelegateExp de = cast(DelegateExp)aggr; | |
1391 | |
1392 FuncDeclaration fd = de.func.isFuncDeclaration(); | |
1393 if (fd) | |
1394 inferApplyArgTypesX(fd, arguments); | |
1395 } | |
1396 else | |
1397 { | |
1398 inferApplyArgTypesY(cast(TypeFunction)tab.nextOf(), arguments); | |
1399 } | |
1400 break; | |
1401 } | |
1402 | |
1403 default: | |
1404 break; // ignore error, caught later | |
1405 } | |
1406 } | |
1407 | |
1408 struct Param3 | |
1409 { | |
1410 /******************************** | |
1411 * Recursive helper function, | |
1412 * analogous to func.overloadResolveX(). | |
1413 */ | |
1414 | |
17
ddae60498573
Implemented mixin statements, DefaultInitExps, FileInitExps, LineInitExps and __traits.
Robert Clipsham <robert@octarineparrot.com>
parents:
16
diff
changeset
|
1415 int fp3(void*, FuncDeclaration f) |
0 | 1416 { |
1417 TypeFunction tf = cast(TypeFunction)f.type; | |
1418 if (inferApplyArgTypesY(tf, arguments) == 1) | |
1419 return 0; | |
1420 | |
1421 if (arguments.dim == 0) | |
1422 return 1; | |
1423 | |
1424 return 0; | |
1425 } | |
1426 | |
1427 Arguments arguments; | |
1428 } | |
1429 | |
1430 void inferApplyArgTypesX(FuncDeclaration fstart, Arguments arguments) | |
1431 { | |
1432 Param3 p3; | |
1433 p3.arguments = arguments; | |
17
ddae60498573
Implemented mixin statements, DefaultInitExps, FileInitExps, LineInitExps and __traits.
Robert Clipsham <robert@octarineparrot.com>
parents:
16
diff
changeset
|
1434 overloadApply(fstart, &p3.fp3, cast(void*)arguments); |
0 | 1435 } |
1436 | |
1437 /****************************** | |
1438 * Infer arguments from type of function. | |
1439 * Returns: | |
1440 * 0 match for this function | |
1441 * 1 no match for this function | |
1442 */ | |
1443 | |
1444 int inferApplyArgTypesY(TypeFunction tf, Arguments arguments) | |
1445 { | |
1446 size_t nparams; | |
1447 Argument p; | |
1448 | |
1449 if (Argument.dim(tf.parameters) != 1) | |
1450 goto Lnomatch; | |
1451 | |
1452 p = Argument.getNth(tf.parameters, 0); | |
1453 if (p.type.ty != TY.Tdelegate) | |
1454 goto Lnomatch; | |
1455 | |
1456 tf = cast(TypeFunction)p.type.nextOf(); | |
1457 assert(tf.ty == TY.Tfunction); | |
1458 | |
1459 /* We now have tf, the type of the delegate. Match it against | |
1460 * the arguments, filling in missing argument types. | |
1461 */ | |
1462 nparams = Argument.dim(tf.parameters); | |
1463 if (nparams == 0 || tf.varargs) | |
1464 goto Lnomatch; // not enough parameters | |
1465 if (arguments.dim != nparams) | |
1466 goto Lnomatch; // not enough parameters | |
1467 | |
1468 for (size_t u = 0; u < nparams; u++) | |
1469 { | |
1470 Argument arg = cast(Argument)arguments.data[u]; | |
1471 Argument param = Argument.getNth(tf.parameters, u); | |
1472 if (arg.type) | |
1473 { | |
1474 if (!arg.type.equals(param.type)) | |
1475 { | |
1476 /* Cannot resolve argument types. Indicate an | |
1477 * error by setting the number of arguments to 0. | |
1478 */ | |
1479 arguments.dim = 0; | |
1480 goto Lmatch; | |
1481 } | |
1482 continue; | |
1483 } | |
1484 arg.type = param.type; | |
1485 } | |
1486 | |
1487 Lmatch: | |
1488 return 0; | |
1489 | |
1490 Lnomatch: | |
1491 return 1; | |
1492 } | |
1493 | |
1494 /************************************************** | |
1495 * Write expression out to buf, but wrap it | |
1496 * in ( ) if its precedence is less than pr. | |
1497 */ | |
1498 | |
1499 void expToCBuffer(OutBuffer buf, HdrGenState* hgs, Expression e, PREC pr) | |
1500 { | |
1501 //if (precedence[e.op] == 0) e.dump(0); | |
1502 if (precedence[e.op] < pr || | |
1503 /* Despite precedence, we don't allow a<b<c expressions. | |
1504 * They must be parenthesized. | |
1505 */ | |
1506 (pr == PREC.PREC_rel && precedence[e.op] == pr)) | |
1507 { | |
1508 buf.writeByte('('); | |
1509 e.toCBuffer(buf, hgs); | |
1510 buf.writeByte(')'); | |
1511 } | |
1512 else | |
1513 e.toCBuffer(buf, hgs); | |
1514 } | |
1515 | |
1516 /************************************************** | |
1517 * Write out argument list to buf. | |
1518 */ | |
1519 | |
1520 void argsToCBuffer(OutBuffer buf, Expressions arguments, HdrGenState* hgs) | |
1521 { | |
1522 if (arguments) | |
1523 { | |
1524 for (size_t i = 0; i < arguments.dim; i++) | |
1525 { | |
1526 Expression arg = cast(Expression)arguments.data[i]; | |
1527 | |
1528 if (arg) | |
1529 { | |
1530 if (i) | |
1531 buf.writeByte(','); | |
1532 expToCBuffer(buf, hgs, arg, PREC.PREC_assign); | |
1533 } | |
1534 } | |
1535 } | |
16
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
1536 } |
53 | 1537 |
1538 ulong getMask(ulong v) | |
1539 { | |
1540 ulong u = 0; | |
1541 if (v >= 0x80) | |
1542 u = 0xFF; | |
1543 while (u < v) | |
1544 u = (u << 1) | 1; | |
1545 return u; | |
64 | 1546 } |
1547 | |
1548 /****************************** | |
1549 * Perform scanForNestedRef() on an array of Expressions. | |
1550 */ | |
1551 | |
1552 void arrayExpressionScanForNestedRef(Scope sc, Expressions a) | |
1553 { | |
1554 //printf("arrayExpressionScanForNestedRef(%p)\n", a); | |
1555 if (a) | |
1556 { | |
1557 for (int i = 0; i < a.dim; i++) | |
1558 { | |
1559 Expression e = cast(Expression)a.data[i]; | |
1560 | |
1561 if (e) | |
1562 { | |
1563 e.scanForNestedRef(sc); | |
1564 } | |
1565 } | |
1566 } | |
53 | 1567 } |