Mercurial > projects > ddmd
annotate dmd/expression/Util.d @ 53:a8b50ff7f201
ForeachStatement.syntaxCopy
SliceExp.syntaxCopy
AnonDeclaration.syntaxCopy
SwitchStatement.syntaxCopy
CaseStatement.syntaxCopy
BreakStatement.syntaxCopy
ThrowStatement.syntaxCopy
NewExp.syntaxCopy
DefaultStatement.syntaxCopy
AssertExp.syntaxCopy
ClassDeclaration.syntaxCopy
TypeTypedef.constConv
eval_builtin
ComplexExp.isConst
DVCondition.syntaxCopy
OrExp.getIntRange
AndExp.getIntRange
getMask
IntegerExp.getIntRange
Type.sizemask
CastExp.getIntRange
Expression.getIntRange
author | korDen |
---|---|
date | Sat, 21 Aug 2010 12:15:47 +0400 |
parents | ddae60498573 |
children | 3adbb28755e8 |
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); | |
339 | |
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 { | |
347 if (!global.gag) | |
348 writef("1: \n"); | |
349 } | |
350 arg.error("%s is not an expression", arg.toChars()); | |
351 arg = new IntegerExp(arg.loc, 0, Type.tint32); | |
352 } | |
353 | |
354 arg = resolveProperties(sc, arg); | |
355 exps.data[i] = cast(void*) arg; | |
356 | |
357 //arg.rvalue(); | |
358 static if (false) { | |
359 if (arg.type.ty == TY.Tfunction) | |
360 { | |
361 arg = new AddrExp(arg.loc, arg); | |
362 arg = arg.semantic(sc); | |
363 exps.data[i] = cast(void*) arg; | |
364 } | |
365 } | |
366 } | |
367 } | |
368 } | |
369 | |
370 /************************************************************* | |
371 * Given var, we need to get the | |
372 * right 'this' pointer if var is in an outer class, but our | |
373 * existing 'this' pointer is in an inner class. | |
374 * Input: | |
375 * e1 existing 'this' | |
376 * ad struct or class we need the correct 'this' for | |
377 * var the specific member of ad we're accessing | |
378 */ | |
379 | |
380 Expression getRightThis(Loc loc, Scope sc, AggregateDeclaration ad, Expression e1, Declaration var) | |
381 { | |
382 //printf("\ngetRightThis(e1 = %s, ad = %s, var = %s)\n", e1.toChars(), ad.toChars(), var.toChars()); | |
383 L1: | |
384 Type t = e1.type.toBasetype(); | |
385 //printf("e1.type = %s, var.type = %s\n", e1.type.toChars(), var.type.toChars()); | |
386 | |
387 /* If e1 is not the 'this' pointer for ad | |
388 */ | |
389 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)) | |
390 { | |
391 ClassDeclaration cd = ad.isClassDeclaration(); | |
392 ClassDeclaration tcd = t.isClassHandle(); | |
393 | |
394 /* e1 is the right this if ad is a base class of e1 | |
395 */ | |
396 if (!cd || !tcd || !(tcd == cd || cd.isBaseOf(tcd, null))) | |
397 { | |
398 /* Only classes can be inner classes with an 'outer' | |
399 * member pointing to the enclosing class instance | |
400 */ | |
401 if (tcd && tcd.isNested()) | |
402 { | |
403 /* e1 is the 'this' pointer for an inner class: tcd. | |
404 * Rewrite it as the 'this' pointer for the outer class. | |
405 */ | |
406 | |
407 e1 = new DotVarExp(loc, e1, tcd.vthis); | |
408 e1.type = tcd.vthis.type; | |
409 // Do not call checkNestedRef() | |
410 //e1 = e1.semantic(sc); | |
411 | |
412 // Skip up over nested functions, and get the enclosing | |
413 // class type. | |
414 int n = 0; | |
415 Dsymbol s; | |
416 for (s = tcd.toParent(); s && s.isFuncDeclaration(); s = s.toParent()) | |
417 { | |
418 FuncDeclaration f = s.isFuncDeclaration(); | |
419 if (f.vthis) | |
420 { | |
421 //printf("rewriting e1 to %s's this\n", f.toChars()); | |
422 n++; | |
423 e1 = new VarExp(loc, f.vthis); | |
424 } | |
425 } | |
426 if (s && s.isClassDeclaration()) | |
427 { | |
428 e1.type = s.isClassDeclaration().type; | |
429 if (n > 1) | |
430 e1 = e1.semantic(sc); | |
431 } | |
432 else | |
433 e1 = e1.semantic(sc); | |
434 goto L1; | |
435 } | |
436 /* Can't find a path from e1 to ad | |
437 */ | |
438 e1.error("this for %s needs to be type %s not type %s", var.toChars(), ad.toChars(), t.toChars()); | |
439 } | |
440 } | |
441 return e1; | |
442 } | |
443 | |
444 /******************************************* | |
445 * Given a symbol that could be either a FuncDeclaration or | |
446 * a function template, resolve it to a function symbol. | |
447 * sc instantiation scope | |
448 * loc instantiation location | |
449 * targsi initial list of template arguments | |
450 * ethis if !null, the 'this' pointer argument | |
451 * fargs arguments to function | |
452 * flags 1: do not issue error message on no match, just return null | |
453 */ | |
454 | |
455 FuncDeclaration resolveFuncCall(Scope sc, Loc loc, Dsymbol s, | |
456 Objects tiargs, | |
457 Expression ethis, | |
458 Expressions arguments, | |
459 int flags) | |
460 { | |
461 if (!s) | |
462 return null; // no match | |
463 FuncDeclaration f = s.isFuncDeclaration(); | |
464 if (f) | |
465 f = f.overloadResolve(loc, ethis, arguments); | |
466 else | |
467 { | |
468 TemplateDeclaration td = s.isTemplateDeclaration(); | |
469 assert(td); | |
470 f = td.deduceFunctionTemplate(sc, loc, tiargs, null, arguments, flags); | |
471 } | |
472 return f; | |
473 } | |
474 | |
475 /**************************************** | |
476 * Now that we know the exact type of the function we're calling, | |
477 * the arguments[] need to be adjusted: | |
478 * 1. implicitly convert argument to the corresponding parameter type | |
479 * 2. add default arguments for any missing arguments | |
480 * 3. do default promotions on arguments corresponding to ... | |
481 * 4. add hidden _arguments[] argument | |
482 * 5. call copy constructor for struct value arguments | |
483 */ | |
484 | |
485 void functionArguments(Loc loc, Scope sc, TypeFunction tf, Expressions arguments) | |
486 { | |
487 uint n; | |
488 | |
489 //printf("functionArguments()\n"); | |
490 assert(arguments); | |
491 size_t nargs = arguments ? arguments.dim : 0; | |
492 size_t nparams = Argument.dim(tf.parameters); | |
493 | |
494 if (nargs > nparams && tf.varargs == 0) | |
495 error(loc, "expected %zu arguments, not %zu for non-variadic function type %s", nparams, nargs, tf.toChars()); | |
496 | |
497 n = (nargs > nparams) ? nargs : nparams; // n = max(nargs, nparams) | |
498 | |
499 int done = 0; | |
500 for (size_t i = 0; i < n; i++) | |
501 { | |
502 Expression arg; | |
503 | |
504 if (i < nargs) | |
505 arg = cast(Expression)arguments.data[i]; | |
506 else | |
507 arg = null; | |
508 | |
509 Type tb; | |
510 | |
511 if (i < nparams) | |
512 { | |
513 Argument p = Argument.getNth(tf.parameters, i); | |
514 | |
515 if (!arg) | |
516 { | |
517 if (!p.defaultArg) | |
518 { | |
519 if (tf.varargs == 2 && i + 1 == nparams) | |
520 goto L2; | |
521 | |
522 error(loc, "expected %d function arguments, not %d", nparams, nargs); | |
523 break; | |
524 } | |
525 arg = p.defaultArg; | |
526 version (DMDV2) { | |
527 if (arg.op == TOK.TOKdefault) | |
528 { | |
529 DefaultInitExp de = cast(DefaultInitExp)arg; | |
530 arg = de.resolve(loc, sc); | |
531 } | |
532 else | |
533 { | |
534 arg = arg.copy(); | |
535 } | |
536 } else { | |
537 arg = arg.copy(); | |
538 } | |
539 arguments.push(cast(void*)arg); | |
540 nargs++; | |
541 } | |
542 | |
543 if (tf.varargs == 2 && i + 1 == nparams) | |
544 { | |
545 //printf("\t\tvarargs == 2, p.type = '%s'\n", p.type.toChars()); | |
546 if (arg.implicitConvTo(p.type)) | |
547 { | |
548 if (nargs != nparams) | |
549 error(loc, "expected %zu function arguments, not %zu", nparams, nargs); | |
550 goto L1; | |
551 } | |
552 L2: | |
553 tb = p.type.toBasetype(); /// | |
554 Type tret = p.isLazyArray(); | |
555 switch (tb.ty) | |
556 { | |
557 case TY.Tsarray: | |
558 case TY.Tarray: | |
559 { // Create a static array variable v of type arg.type | |
560 version (IN_GCC) { | |
561 /* GCC 4.0 does not like zero length arrays used like | |
562 this; pass a null array value instead. Could also | |
563 just make a one-element array. */ | |
564 if (nargs - i == 0) | |
565 { | |
566 arg = new NullExp(loc); | |
567 break; | |
568 } | |
569 } | |
570 Identifier id = Lexer.uniqueId("__arrayArg"); | |
571 Type t = new TypeSArray((cast(TypeArray)tb).next, new IntegerExp(nargs - i)); | |
572 t = t.semantic(loc, sc); | |
573 VarDeclaration v = new VarDeclaration(loc, t, id, new VoidInitializer(loc)); | |
574 v.semantic(sc); | |
575 v.parent = sc.parent; | |
576 //sc.insert(v); | |
577 | |
578 Expression c = new DeclarationExp(Loc(0), v); | |
579 c.type = v.type; | |
580 | |
581 for (size_t u = i; u < nargs; u++) | |
582 { | |
583 Expression a = cast(Expression)arguments.data[u]; | |
584 if (tret && !(cast(TypeArray)tb).next.equals(a.type)) | |
585 a = a.toDelegate(sc, tret); | |
586 | |
587 Expression e = new VarExp(loc, v); | |
588 e = new IndexExp(loc, e, new IntegerExp(u + 1 - nparams)); | |
589 AssignExp ae = new AssignExp(loc, e, a); | |
590 | |
591 version (DMDV2) { | |
592 ae.op = TOK.TOKconstruct; | |
593 } | |
594 | |
595 if (c) | |
596 c = new CommaExp(loc, c, ae); | |
597 else | |
598 c = ae; | |
599 } | |
600 | |
601 arg = new VarExp(loc, v); | |
602 if (c) | |
603 arg = new CommaExp(loc, c, arg); | |
604 break; | |
605 } | |
606 | |
607 case TY.Tclass: | |
608 { /* Set arg to be: | |
609 * new Tclass(arg0, arg1, ..., argn) | |
610 */ | |
611 Expressions args = new Expressions(); | |
612 args.setDim(nargs - i); | |
613 for (size_t u = i; u < nargs; u++) | |
614 args.data[u - i] = arguments.data[u]; | |
615 arg = new NewExp(loc, null, null, p.type, args); | |
616 break; | |
617 } | |
618 | |
619 default: | |
620 if (!arg) | |
621 { | |
622 error(loc, "not enough arguments"); | |
623 return; | |
624 } | |
625 break; | |
626 } | |
627 | |
628 arg = arg.semantic(sc); | |
629 //printf("\targ = '%s'\n", arg.toChars()); | |
630 arguments.setDim(i + 1); | |
631 done = 1; | |
632 } | |
633 | |
634 L1: | |
635 if (!(p.storageClass & STC.STClazy && p.type.ty == TY.Tvoid)) | |
636 { | |
637 if (p.type != arg.type) | |
638 { | |
639 //printf("arg.type = %s, p.type = %s\n", arg.type.toChars(), p.type.toChars()); | |
640 arg = arg.implicitCastTo(sc, p.type); | |
641 arg = arg.optimize(WANT.WANTvalue); | |
642 } | |
643 } | |
644 if (p.storageClass & STC.STCref) | |
645 { | |
646 arg = arg.toLvalue(sc, arg); | |
647 } | |
648 else if (p.storageClass & STC.STCout) | |
649 { | |
650 arg = arg.modifiableLvalue(sc, arg); | |
651 } | |
652 | |
653 // Convert static arrays to pointers | |
654 tb = arg.type.toBasetype(); | |
655 if (tb.ty == TY.Tsarray) | |
656 { | |
657 arg = arg.checkToPointer(); | |
658 } | |
659 version (DMDV2) { | |
660 if (tb.ty == TY.Tstruct && !(p.storageClass & (STC.STCref | STC.STCout))) | |
661 { | |
662 arg = callCpCtor(loc, sc, arg); | |
663 } | |
664 } | |
665 | |
666 // Convert lazy argument to a delegate | |
667 if (p.storageClass & STC.STClazy) | |
668 { | |
669 arg = arg.toDelegate(sc, p.type); | |
670 } | |
671 version (DMDV2) { | |
672 /* Look for arguments that cannot 'escape' from the called | |
673 * function. | |
674 */ | |
675 if (!tf.parameterEscapes(p)) | |
676 { | |
677 /* Function literals can only appear once, so if this | |
678 * appearance was scoped, there cannot be any others. | |
679 */ | |
680 if (arg.op == TOK.TOKfunction) | |
681 { | |
682 FuncExp fe = cast(FuncExp)arg; | |
683 fe.fd.tookAddressOf = 0; | |
684 } | |
685 | |
686 /* For passing a delegate to a scoped parameter, | |
687 * this doesn't count as taking the address of it. | |
688 * We only worry about 'escaping' references to the function. | |
689 */ | |
690 else if (arg.op == TOK.TOKdelegate) | |
691 { | |
692 DelegateExp de = cast(DelegateExp)arg; | |
693 if (de.e1.op == TOK.TOKvar) | |
694 { | |
695 VarExp ve = cast(VarExp)de.e1; | |
696 FuncDeclaration f = ve.var.isFuncDeclaration(); | |
697 if (f) | |
698 { | |
699 f.tookAddressOf--; | |
700 //printf("tookAddressOf = %d\n", f.tookAddressOf); | |
701 } | |
702 } | |
703 } | |
704 } | |
705 } | |
706 } | |
707 else | |
708 { | |
709 // If not D linkage, do promotions | |
710 if (tf.linkage != LINK.LINKd) | |
711 { | |
712 // Promote bytes, words, etc., to ints | |
713 arg = arg.integralPromotions(sc); | |
714 | |
715 // Promote floats to doubles | |
716 switch (arg.type.ty) | |
717 { | |
718 case TY.Tfloat32: | |
719 arg = arg.castTo(sc, Type.tfloat64); | |
720 break; | |
721 | |
722 case TY.Timaginary32: | |
723 arg = arg.castTo(sc, Type.timaginary64); | |
724 break; | |
725 default: | |
726 break; | |
727 } | |
728 } | |
729 | |
730 // Convert static arrays to dynamic arrays | |
731 tb = arg.type.toBasetype(); | |
732 if (tb.ty == TY.Tsarray) | |
733 { | |
734 TypeSArray ts = cast(TypeSArray)tb; | |
735 Type ta = ts.next.arrayOf(); | |
736 if (ts.size(arg.loc) == 0) | |
737 { | |
738 arg = new NullExp(arg.loc); | |
739 arg.type = ta; | |
740 } | |
741 else | |
742 { | |
743 arg = arg.castTo(sc, ta); | |
744 } | |
745 } | |
746 version (DMDV2) { | |
747 if (tb.ty == TY.Tstruct) | |
748 { | |
749 arg = callCpCtor(loc, sc, arg); | |
750 } | |
751 | |
752 // Give error for overloaded function addresses | |
753 if (arg.op == TOK.TOKsymoff) | |
754 { | |
755 SymOffExp se = cast(SymOffExp)arg; | |
756 if (se.hasOverloads && !se.var.isFuncDeclaration().isUnique()) | |
757 arg.error("function %s is overloaded", arg.toChars()); | |
758 } | |
759 } | |
760 arg.rvalue(); | |
761 } | |
762 arg = arg.optimize(WANT.WANTvalue); | |
763 arguments.data[i] = cast(void*) arg; | |
764 if (done) | |
765 break; | |
766 } | |
767 | |
768 // If D linkage and variadic, add _arguments[] as first argument | |
769 if (tf.linkage == LINK.LINKd && tf.varargs == 1) | |
770 { | |
771 Expression e = createTypeInfoArray(sc, cast(Expression*)&arguments.data[nparams], arguments.dim - nparams); | |
772 arguments.insert(0, cast(void*)e); | |
773 } | |
774 } | |
775 | |
16
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
776 /****************************** |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
777 * 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
|
778 */ |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
779 |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
780 version (DMDV2) { |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
781 bool arrayExpressionCanThrow(Expressions exps) |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
782 { |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
783 if (exps) |
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 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
|
786 { 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
|
787 if (e && e.canThrow()) |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
788 return true; |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
789 } |
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 return false; |
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 } |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
794 |
0 | 795 /**************************************** |
796 * Expand tuples. | |
797 */ | |
798 | |
799 void expandTuples(Expressions exps) | |
800 { | |
801 //printf("expandTuples()\n"); | |
802 if (exps) | |
803 { | |
804 for (size_t i = 0; i < exps.dim; i++) | |
805 { | |
806 Expression arg = cast(Expression)exps.data[i]; | |
807 if (!arg) | |
808 continue; | |
809 | |
810 // Look for tuple with 0 members | |
811 if (arg.op == TOK.TOKtype) | |
812 { | |
813 TypeExp e = cast(TypeExp)arg; | |
814 if (e.type.toBasetype().ty == TY.Ttuple) | |
815 { | |
816 TypeTuple tt = cast(TypeTuple)e.type.toBasetype(); | |
817 | |
818 if (!tt.arguments || tt.arguments.dim == 0) | |
819 { | |
820 exps.remove(i); | |
821 if (i == exps.dim) | |
822 return; | |
823 i--; | |
824 continue; | |
825 } | |
826 } | |
827 } | |
828 | |
829 // Inline expand all the tuples | |
830 while (arg.op == TOK.TOKtuple) | |
831 { | |
832 TupleExp te = cast(TupleExp)arg; | |
833 | |
834 exps.remove(i); // remove arg | |
835 exps.insert(i, cast(void*)te.exps); // replace with tuple contents | |
836 | |
837 if (i == exps.dim) | |
838 return; // empty tuple, no more arguments | |
839 | |
840 arg = cast(Expression)exps.data[i]; | |
841 } | |
842 } | |
843 } | |
844 } | |
845 | |
846 /************************************************** | |
847 * Write out argument types to buf. | |
848 */ | |
849 | |
850 void argExpTypesToCBuffer(OutBuffer buf, Expressions arguments, HdrGenState* hgs) | |
851 { | |
852 if (arguments) | |
853 { | |
854 scope OutBuffer argbuf = new OutBuffer(); | |
855 | |
856 for (size_t i = 0; i < arguments.dim; i++) | |
857 { | |
858 Expression arg = cast(Expression)arguments.data[i]; | |
859 | |
860 if (i) | |
861 buf.writeByte(','); | |
862 | |
863 argbuf.reset(); | |
864 arg.type.toCBuffer2(argbuf, hgs, MOD.MODundefined); | |
865 buf.write(argbuf); | |
866 } | |
867 } | |
868 } | |
869 | |
870 /**************************************** | |
871 * Determine if scope sc has package level access to s. | |
872 */ | |
873 | |
874 bool hasPackageAccess(Scope sc, Dsymbol s) | |
875 { | |
876 version (LOG) { | |
877 printf("hasPackageAccess(s = '%s', sc = '%p')\n", s.toChars(), sc); | |
878 } | |
879 | |
880 for (; s; s = s.parent) | |
881 { | |
882 if (s.isPackage() && !s.isModule()) | |
883 break; | |
884 } | |
885 version (LOG) { | |
886 if (s) | |
887 printf("\tthis is in package '%s'\n", s.toChars()); | |
888 } | |
889 | |
890 if (s && s == sc.module_.parent) | |
891 { | |
892 version (LOG) { | |
893 printf("\ts is in same package as sc\n"); | |
894 } | |
895 return true; | |
896 } | |
897 | |
898 | |
899 version (LOG) { | |
900 printf("\tno package access\n"); | |
901 } | |
902 | |
903 return false; | |
904 } | |
905 | |
906 /********************************************* | |
907 * Call copy constructor for struct value argument. | |
908 */ | |
909 version (DMDV2) { | |
910 Expression callCpCtor(Loc loc, Scope sc, Expression e) | |
911 { | |
912 Type tb = e.type.toBasetype(); | |
913 assert(tb.ty == Tstruct); | |
914 StructDeclaration sd = (cast(TypeStruct)tb).sym; | |
915 if (sd.cpctor) | |
916 { | |
917 /* Create a variable tmp, and replace the argument e with: | |
918 * (tmp = e),tmp | |
919 * and let AssignExp() handle the construction. | |
920 * This is not the most efficent, ideally tmp would be constructed | |
921 * directly onto the stack. | |
922 */ | |
923 Identifier idtmp = Lexer.uniqueId("__tmp"); | |
924 VarDeclaration tmp = new VarDeclaration(loc, tb, idtmp, new ExpInitializer(Loc(0), e)); | |
925 Expression ae = new DeclarationExp(loc, tmp); | |
926 e = new CommaExp(loc, ae, new VarExp(loc, tmp)); | |
927 e = e.semantic(sc); | |
928 } | |
929 return e; | |
930 } | |
931 } | |
932 | |
933 /*************************************** | |
934 * Create a static array of TypeInfo references | |
935 * corresponding to an array of Expression's. | |
936 * Used to supply hidden _arguments[] value for variadic D functions. | |
937 */ | |
938 | |
939 Expression createTypeInfoArray(Scope sc, Expression* exps, int dim) | |
940 { | |
941 assert(false); | |
942 } | |
943 | |
944 /************************************** | |
945 * Evaluate builtin function. | |
946 * Return result: null if cannot evaluate it. | |
947 */ | |
948 | |
53 | 949 extern(C) extern real sinl(real); |
950 extern(C) extern real cosl(real); | |
951 extern(C) extern real tanl(real); | |
952 extern(C) extern real sqrtl(real); | |
953 extern(C) extern real fabsl(real); | |
954 | |
0 | 955 Expression eval_builtin(BUILTIN builtin, Expressions arguments) |
956 { | |
53 | 957 assert(arguments && arguments.dim); |
958 Expression arg0 = cast(Expression)arguments.data[0]; | |
959 Expression e = null; | |
960 switch (builtin) | |
961 { | |
962 case BUILTINsin: | |
963 if (arg0.op == TOKfloat64) | |
964 e = new RealExp(Loc(0), sinl(arg0.toReal()), arg0.type); | |
965 break; | |
966 | |
967 case BUILTINcos: | |
968 if (arg0.op == TOKfloat64) | |
969 e = new RealExp(Loc(0), cosl(arg0.toReal()), arg0.type); | |
970 break; | |
971 | |
972 case BUILTINtan: | |
973 if (arg0.op == TOKfloat64) | |
974 e = new RealExp(Loc(0), tanl(arg0.toReal()), arg0.type); | |
975 break; | |
976 | |
977 case BUILTINsqrt: | |
978 if (arg0.op == TOKfloat64) | |
979 e = new RealExp(Loc(0), sqrtl(arg0.toReal()), arg0.type); | |
980 break; | |
981 | |
982 case BUILTINfabs: | |
983 if (arg0.op == TOKfloat64) | |
984 e = new RealExp(Loc(0), fabsl(arg0.toReal()), arg0.type); | |
985 break; | |
986 } | |
987 return e; | |
0 | 988 } |
989 | |
990 Expression fromConstInitializer(int result, Expression e1) | |
991 { | |
992 //printf("fromConstInitializer(result = %x, %s)\n", result, e1.toChars()); | |
993 //static int xx; if (xx++ == 10) assert(0); | |
994 Expression e = e1; | |
995 if (e1.op == TOK.TOKvar) | |
996 { | |
997 VarExp ve = cast(VarExp)e1; | |
998 VarDeclaration v = ve.var.isVarDeclaration(); | |
999 e = expandVar(result, v); | |
1000 if (e) | |
1001 { | |
1002 if (e.type != e1.type) | |
1003 { | |
1004 // Type 'paint' operation | |
1005 e = e.copy(); | |
1006 e.type = e1.type; | |
1007 } | |
1008 } | |
1009 else | |
1010 { | |
1011 e = e1; | |
1012 } | |
1013 } | |
1014 return e; | |
1015 } | |
1016 | |
1017 /************************************* | |
1018 * If variable has a const initializer, | |
1019 * return that initializer. | |
1020 */ | |
1021 | |
1022 Expression expandVar(int result, VarDeclaration v) | |
1023 { | |
1024 //printf("expandVar(result = %d, v = %p, %s)\n", result, v, v ? v.toChars() : "null"); | |
1025 | |
1026 Expression e = null; | |
1027 if (!v) | |
1028 return e; | |
1029 | |
1030 if (v.isConst() || v.isInvariant() || v.storage_class & STC.STCmanifest) | |
1031 { | |
1032 if (!v.type) | |
1033 { | |
1034 //error("ICE"); | |
1035 return e; | |
1036 } | |
1037 | |
1038 Type tb = v.type.toBasetype(); | |
1039 if (result & WANT.WANTinterpret || v.storage_class & STC.STCmanifest || (tb.ty != TY.Tsarray && tb.ty != TY.Tstruct)) | |
1040 { | |
1041 if (v.init) | |
1042 { | |
1043 if (v.inuse) | |
1044 { | |
1045 if (v.storage_class & STC.STCmanifest) | |
1046 v.error("recursive initialization of constant"); | |
1047 goto L1; | |
1048 } | |
1049 Expression ei = v.init.toExpression(); | |
1050 if (!ei) | |
1051 goto L1; | |
1052 if (ei.op == TOK.TOKconstruct || ei.op == TOK.TOKblit) | |
1053 { | |
1054 AssignExp ae = cast(AssignExp)ei; | |
1055 ei = ae.e2; | |
1056 if (ei.isConst() != 1 && ei.op != TOK.TOKstring) | |
1057 goto L1; | |
1058 if (ei.type != v.type) | |
1059 goto L1; | |
1060 } | |
1061 if (v.scope_) | |
1062 { | |
1063 v.inuse++; | |
1064 e = ei.syntaxCopy(); | |
1065 e = e.semantic(v.scope_); | |
1066 e = e.implicitCastTo(v.scope_, v.type); | |
1067 // enabling this line causes test22 in test suite to fail | |
1068 //ei.type = e.type; | |
1069 v.scope_ = null; | |
1070 v.inuse--; | |
1071 } | |
1072 else if (!ei.type) | |
1073 { | |
1074 goto L1; | |
1075 } | |
1076 else | |
1077 // Should remove the copy() operation by | |
1078 // making all mods to expressions copy-on-write | |
1079 e = ei.copy(); | |
1080 } | |
1081 else | |
1082 { | |
1083 static if (true) { | |
1084 goto L1; | |
1085 } else { | |
1086 // BUG: what if const is initialized in constructor? | |
1087 e = v.type.defaultInit(); | |
1088 e.loc = e1.loc; | |
1089 } | |
1090 } | |
1091 if (e.type != v.type) | |
1092 { | |
1093 e = e.castTo(null, v.type); | |
1094 } | |
1095 v.inuse++; | |
1096 e = e.optimize(result); | |
1097 v.inuse--; | |
1098 } | |
1099 } | |
1100 L1: | |
1101 //if (e) printf("\te = %s, e.type = %s\n", e.toChars(), e.type.toChars()); | |
1102 return e; | |
1103 } | |
1104 | |
1105 /**************************************** | |
1106 * Check access to d for expression e.d | |
1107 */ | |
1108 | |
1109 void accessCheck(Loc loc, Scope sc, Expression e, Declaration d) | |
1110 { | |
1111 version (LOG) { | |
1112 if (e) | |
1113 { | |
1114 printf("accessCheck(%s . %s)\n", e.toChars(), d.toChars()); | |
1115 printf("\te.type = %s\n", e.type.toChars()); | |
1116 } | |
1117 else | |
1118 { | |
1119 //printf("accessCheck(%s)\n", d.toChars()); | |
1120 } | |
1121 } | |
1122 if (!e) | |
1123 { | |
1124 if (d.prot() == PROT.PROTprivate && d.getModule() != sc.module_ || | |
1125 d.prot() == PROT.PROTpackage && !hasPackageAccess(sc, d)) | |
1126 | |
1127 error(loc, "%s %s.%s is not accessible from %s", | |
1128 d.kind(), d.getModule().toChars(), d.toChars(), sc.module_.toChars()); | |
1129 } | |
1130 else if (e.type.ty == TY.Tclass) | |
1131 { | |
1132 // Do access check | |
1133 ClassDeclaration cd; | |
1134 | |
1135 cd = cast(ClassDeclaration)((cast(TypeClass)e.type).sym); | |
1136 static if (true) { | |
1137 if (e.op == TOK.TOKsuper) | |
1138 { | |
1139 ClassDeclaration cd2 = sc.func.toParent().isClassDeclaration(); | |
1140 if (cd2) | |
1141 cd = cd2; | |
1142 } | |
1143 } | |
1144 cd.accessCheck(loc, sc, d); | |
1145 } | |
1146 else if (e.type.ty == TY.Tstruct) | |
1147 { | |
1148 // Do access check | |
1149 StructDeclaration cd = cast(StructDeclaration)((cast(TypeStruct)e.type).sym); | |
1150 cd.accessCheck(loc, sc, d); | |
1151 } | |
1152 } | |
1153 | |
1154 /***************************************** | |
1155 * Given array of arguments and an aggregate type, | |
1156 * if any of the argument types are missing, attempt to infer | |
1157 * them from the aggregate type. | |
1158 */ | |
1159 | |
1160 void inferApplyArgTypes(TOK op, Arguments arguments, Expression aggr) | |
1161 { | |
1162 if (!arguments || !arguments.dim) | |
1163 return; | |
1164 | |
1165 /* Return if no arguments need types. | |
1166 */ | |
1167 for (size_t u = 0; 1; u++) | |
1168 { | |
1169 if (u == arguments.dim) | |
1170 return; | |
1171 | |
1172 Argument arg = cast(Argument)arguments.data[u]; | |
1173 if (!arg.type) | |
1174 break; | |
1175 } | |
1176 | |
1177 AggregateDeclaration ad; | |
1178 | |
1179 Argument arg = cast(Argument)arguments.data[0]; | |
1180 Type taggr = aggr.type; | |
1181 if (!taggr) | |
1182 return; | |
1183 Type tab = taggr.toBasetype(); | |
1184 switch (tab.ty) | |
1185 { | |
1186 case TY.Tarray: | |
1187 case TY.Tsarray: | |
1188 case TY.Ttuple: | |
1189 if (arguments.dim == 2) | |
1190 { | |
1191 if (!arg.type) | |
1192 arg.type = Type.tsize_t; // key type | |
1193 arg = cast(Argument)arguments.data[1]; | |
1194 } | |
1195 if (!arg.type && tab.ty != TY.Ttuple) | |
1196 arg.type = tab.nextOf(); // value type | |
1197 break; | |
1198 | |
1199 case TY.Taarray: | |
1200 { | |
1201 TypeAArray taa = cast(TypeAArray)tab; | |
1202 | |
1203 if (arguments.dim == 2) | |
1204 { | |
1205 if (!arg.type) | |
1206 arg.type = taa.index; // key type | |
1207 arg = cast(Argument)arguments.data[1]; | |
1208 } | |
1209 if (!arg.type) | |
1210 arg.type = taa.next; // value type | |
1211 break; | |
1212 } | |
1213 | |
1214 case TY.Tclass: | |
1215 ad = (cast(TypeClass)tab).sym; | |
1216 goto Laggr; | |
1217 | |
1218 case TY.Tstruct: | |
1219 ad = (cast(TypeStruct)tab).sym; | |
1220 goto Laggr; | |
1221 | |
1222 Laggr: | |
1223 if (arguments.dim == 1) | |
1224 { | |
1225 if (!arg.type) | |
1226 { | |
1227 /* Look for a head() or rear() overload | |
1228 */ | |
1229 Identifier id = (op == TOK.TOKforeach) ? Id.Fhead : Id.Ftoe; | |
1230 Dsymbol s = search_function(ad, id); | |
1231 FuncDeclaration fd = s ? s.isFuncDeclaration() : null; | |
1232 if (!fd) | |
1233 { | |
1234 if (s && s.isTemplateDeclaration()) | |
1235 break; | |
1236 goto Lapply; | |
1237 } | |
1238 arg.type = fd.type.nextOf(); | |
1239 } | |
1240 break; | |
1241 } | |
1242 | |
1243 Lapply: | |
1244 { /* Look for an | |
1245 * int opApply(int delegate(ref Type [, ...]) dg); | |
1246 * overload | |
1247 */ | |
1248 Dsymbol s = search_function(ad, (op == TOK.TOKforeach_reverse) ? Id.applyReverse : Id.apply); | |
1249 if (s) | |
1250 { | |
1251 FuncDeclaration fd = s.isFuncDeclaration(); | |
1252 if (fd) | |
1253 { | |
1254 inferApplyArgTypesX(fd, arguments); | |
1255 break; | |
1256 } | |
1257 static if (false) { | |
1258 TemplateDeclaration td = s.isTemplateDeclaration(); | |
1259 if (td) | |
1260 { | |
1261 inferApplyArgTypesZ(td, arguments); | |
1262 break; | |
1263 } | |
1264 } | |
1265 } | |
1266 break; | |
1267 } | |
1268 | |
1269 case TY.Tdelegate: | |
1270 { | |
1271 if (0 && aggr.op == TOK.TOKdelegate) | |
1272 { | |
1273 DelegateExp de = cast(DelegateExp)aggr; | |
1274 | |
1275 FuncDeclaration fd = de.func.isFuncDeclaration(); | |
1276 if (fd) | |
1277 inferApplyArgTypesX(fd, arguments); | |
1278 } | |
1279 else | |
1280 { | |
1281 inferApplyArgTypesY(cast(TypeFunction)tab.nextOf(), arguments); | |
1282 } | |
1283 break; | |
1284 } | |
1285 | |
1286 default: | |
1287 break; // ignore error, caught later | |
1288 } | |
1289 } | |
1290 | |
1291 struct Param3 | |
1292 { | |
1293 /******************************** | |
1294 * Recursive helper function, | |
1295 * analogous to func.overloadResolveX(). | |
1296 */ | |
1297 | |
17
ddae60498573
Implemented mixin statements, DefaultInitExps, FileInitExps, LineInitExps and __traits.
Robert Clipsham <robert@octarineparrot.com>
parents:
16
diff
changeset
|
1298 int fp3(void*, FuncDeclaration f) |
0 | 1299 { |
1300 TypeFunction tf = cast(TypeFunction)f.type; | |
1301 if (inferApplyArgTypesY(tf, arguments) == 1) | |
1302 return 0; | |
1303 | |
1304 if (arguments.dim == 0) | |
1305 return 1; | |
1306 | |
1307 return 0; | |
1308 } | |
1309 | |
1310 Arguments arguments; | |
1311 } | |
1312 | |
1313 void inferApplyArgTypesX(FuncDeclaration fstart, Arguments arguments) | |
1314 { | |
1315 Param3 p3; | |
1316 p3.arguments = arguments; | |
17
ddae60498573
Implemented mixin statements, DefaultInitExps, FileInitExps, LineInitExps and __traits.
Robert Clipsham <robert@octarineparrot.com>
parents:
16
diff
changeset
|
1317 overloadApply(fstart, &p3.fp3, cast(void*)arguments); |
0 | 1318 } |
1319 | |
1320 /****************************** | |
1321 * Infer arguments from type of function. | |
1322 * Returns: | |
1323 * 0 match for this function | |
1324 * 1 no match for this function | |
1325 */ | |
1326 | |
1327 int inferApplyArgTypesY(TypeFunction tf, Arguments arguments) | |
1328 { | |
1329 size_t nparams; | |
1330 Argument p; | |
1331 | |
1332 if (Argument.dim(tf.parameters) != 1) | |
1333 goto Lnomatch; | |
1334 | |
1335 p = Argument.getNth(tf.parameters, 0); | |
1336 if (p.type.ty != TY.Tdelegate) | |
1337 goto Lnomatch; | |
1338 | |
1339 tf = cast(TypeFunction)p.type.nextOf(); | |
1340 assert(tf.ty == TY.Tfunction); | |
1341 | |
1342 /* We now have tf, the type of the delegate. Match it against | |
1343 * the arguments, filling in missing argument types. | |
1344 */ | |
1345 nparams = Argument.dim(tf.parameters); | |
1346 if (nparams == 0 || tf.varargs) | |
1347 goto Lnomatch; // not enough parameters | |
1348 if (arguments.dim != nparams) | |
1349 goto Lnomatch; // not enough parameters | |
1350 | |
1351 for (size_t u = 0; u < nparams; u++) | |
1352 { | |
1353 Argument arg = cast(Argument)arguments.data[u]; | |
1354 Argument param = Argument.getNth(tf.parameters, u); | |
1355 if (arg.type) | |
1356 { | |
1357 if (!arg.type.equals(param.type)) | |
1358 { | |
1359 /* Cannot resolve argument types. Indicate an | |
1360 * error by setting the number of arguments to 0. | |
1361 */ | |
1362 arguments.dim = 0; | |
1363 goto Lmatch; | |
1364 } | |
1365 continue; | |
1366 } | |
1367 arg.type = param.type; | |
1368 } | |
1369 | |
1370 Lmatch: | |
1371 return 0; | |
1372 | |
1373 Lnomatch: | |
1374 return 1; | |
1375 } | |
1376 | |
1377 /************************************************** | |
1378 * Write expression out to buf, but wrap it | |
1379 * in ( ) if its precedence is less than pr. | |
1380 */ | |
1381 | |
1382 void expToCBuffer(OutBuffer buf, HdrGenState* hgs, Expression e, PREC pr) | |
1383 { | |
1384 //if (precedence[e.op] == 0) e.dump(0); | |
1385 if (precedence[e.op] < pr || | |
1386 /* Despite precedence, we don't allow a<b<c expressions. | |
1387 * They must be parenthesized. | |
1388 */ | |
1389 (pr == PREC.PREC_rel && precedence[e.op] == pr)) | |
1390 { | |
1391 buf.writeByte('('); | |
1392 e.toCBuffer(buf, hgs); | |
1393 buf.writeByte(')'); | |
1394 } | |
1395 else | |
1396 e.toCBuffer(buf, hgs); | |
1397 } | |
1398 | |
1399 /************************************************** | |
1400 * Write out argument list to buf. | |
1401 */ | |
1402 | |
1403 void argsToCBuffer(OutBuffer buf, Expressions arguments, HdrGenState* hgs) | |
1404 { | |
1405 if (arguments) | |
1406 { | |
1407 for (size_t i = 0; i < arguments.dim; i++) | |
1408 { | |
1409 Expression arg = cast(Expression)arguments.data[i]; | |
1410 | |
1411 if (arg) | |
1412 { | |
1413 if (i) | |
1414 buf.writeByte(','); | |
1415 expToCBuffer(buf, hgs, arg, PREC.PREC_assign); | |
1416 } | |
1417 } | |
1418 } | |
16
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
1419 } |
53 | 1420 |
1421 ulong getMask(ulong v) | |
1422 { | |
1423 ulong u = 0; | |
1424 if (v >= 0x80) | |
1425 u = 0xFF; | |
1426 while (u < v) | |
1427 u = (u << 1) | 1; | |
1428 return u; | |
1429 } |