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