Mercurial > projects > ddmd
annotate dmd/expression/Util.d @ 84:be2ab491772e
Expressions -> Vector!Expression
author | Eldar Insafutdinov <e.insafutdinov@gmail.com> |
---|---|
date | Mon, 30 Aug 2010 16:12:19 +0100 |
parents | 43073c7c7769 |
children | 5c859d5fbe27 |
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 { | |
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()); | |
634 arg = arg.implicitCastTo(sc, p.type); | |
635 arg = arg.optimize(WANT.WANTvalue); | |
636 } | |
637 } | |
638 if (p.storageClass & STC.STCref) | |
639 { | |
640 arg = arg.toLvalue(sc, arg); | |
641 } | |
642 else if (p.storageClass & STC.STCout) | |
643 { | |
644 arg = arg.modifiableLvalue(sc, arg); | |
645 } | |
646 | |
647 // Convert static arrays to pointers | |
648 tb = arg.type.toBasetype(); | |
649 if (tb.ty == TY.Tsarray) | |
650 { | |
651 arg = arg.checkToPointer(); | |
652 } | |
653 version (DMDV2) { | |
654 if (tb.ty == TY.Tstruct && !(p.storageClass & (STC.STCref | STC.STCout))) | |
655 { | |
656 arg = callCpCtor(loc, sc, arg); | |
657 } | |
658 } | |
659 | |
660 // Convert lazy argument to a delegate | |
661 if (p.storageClass & STC.STClazy) | |
662 { | |
663 arg = arg.toDelegate(sc, p.type); | |
664 } | |
665 version (DMDV2) { | |
666 /* Look for arguments that cannot 'escape' from the called | |
667 * function. | |
668 */ | |
669 if (!tf.parameterEscapes(p)) | |
670 { | |
671 /* Function literals can only appear once, so if this | |
672 * appearance was scoped, there cannot be any others. | |
673 */ | |
674 if (arg.op == TOK.TOKfunction) | |
675 { | |
676 FuncExp fe = cast(FuncExp)arg; | |
677 fe.fd.tookAddressOf = 0; | |
678 } | |
679 | |
680 /* For passing a delegate to a scoped parameter, | |
681 * this doesn't count as taking the address of it. | |
682 * We only worry about 'escaping' references to the function. | |
683 */ | |
684 else if (arg.op == TOK.TOKdelegate) | |
685 { | |
686 DelegateExp de = cast(DelegateExp)arg; | |
687 if (de.e1.op == TOK.TOKvar) | |
688 { | |
689 VarExp ve = cast(VarExp)de.e1; | |
690 FuncDeclaration f = ve.var.isFuncDeclaration(); | |
691 if (f) | |
692 { | |
693 f.tookAddressOf--; | |
694 //printf("tookAddressOf = %d\n", f.tookAddressOf); | |
695 } | |
696 } | |
697 } | |
698 } | |
699 } | |
700 } | |
701 else | |
702 { | |
703 // If not D linkage, do promotions | |
704 if (tf.linkage != LINK.LINKd) | |
705 { | |
706 // Promote bytes, words, etc., to ints | |
707 arg = arg.integralPromotions(sc); | |
708 | |
709 // Promote floats to doubles | |
710 switch (arg.type.ty) | |
711 { | |
712 case TY.Tfloat32: | |
713 arg = arg.castTo(sc, Type.tfloat64); | |
714 break; | |
715 | |
716 case TY.Timaginary32: | |
717 arg = arg.castTo(sc, Type.timaginary64); | |
718 break; | |
719 default: | |
720 break; | |
721 } | |
722 } | |
723 | |
724 // Convert static arrays to dynamic arrays | |
725 tb = arg.type.toBasetype(); | |
726 if (tb.ty == TY.Tsarray) | |
727 { | |
728 TypeSArray ts = cast(TypeSArray)tb; | |
729 Type ta = ts.next.arrayOf(); | |
730 if (ts.size(arg.loc) == 0) | |
731 { | |
732 arg = new NullExp(arg.loc); | |
733 arg.type = ta; | |
734 } | |
735 else | |
736 { | |
737 arg = arg.castTo(sc, ta); | |
738 } | |
739 } | |
740 version (DMDV2) { | |
73 | 741 if (tb.ty == Tstruct) |
0 | 742 { |
743 arg = callCpCtor(loc, sc, arg); | |
744 } | |
73 | 745 } |
0 | 746 |
73 | 747 // Give error for overloaded function addresses |
748 if (arg.op == TOKsymoff) | |
749 { | |
750 SymOffExp se = cast(SymOffExp)arg; | |
751 version (DMDV2) { | |
752 bool aux = (se.hasOverloads != 0); | |
753 } else { | |
754 bool aux = true; | |
0 | 755 } |
73 | 756 if (aux && !se.var.isFuncDeclaration().isUnique()) |
757 arg.error("function %s is overloaded", arg.toChars()); | |
758 } | |
759 | |
760 arg.rvalue(); | |
761 } | |
762 | |
0 | 763 arg = arg.optimize(WANT.WANTvalue); |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
79
diff
changeset
|
764 arguments[i] = arg; |
0 | 765 if (done) |
766 break; | |
767 } | |
768 | |
769 // If D linkage and variadic, add _arguments[] as first argument | |
770 if (tf.linkage == LINK.LINKd && tf.varargs == 1) | |
771 { | |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
79
diff
changeset
|
772 auto e = createTypeInfoArray(sc, &arguments[nparams], arguments.dim - nparams); |
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
79
diff
changeset
|
773 arguments.insert(0, e); |
0 | 774 } |
775 } | |
776 | |
16
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
777 /****************************** |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
778 * 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
|
779 */ |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
780 |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
781 version (DMDV2) { |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
782 bool arrayExpressionCanThrow(Expressions exps) |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
783 { |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
784 if (exps) |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
785 { |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
79
diff
changeset
|
786 foreach (e; exps) |
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
79
diff
changeset
|
787 { |
16
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
788 if (e && e.canThrow()) |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
789 return true; |
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 } |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
792 return false; |
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 } |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
795 |
0 | 796 /**************************************** |
797 * Expand tuples. | |
798 */ | |
799 | |
800 void expandTuples(Expressions exps) | |
801 { | |
802 //printf("expandTuples()\n"); | |
803 if (exps) | |
804 { | |
805 for (size_t i = 0; i < exps.dim; i++) | |
806 { | |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
79
diff
changeset
|
807 auto arg = exps[i]; |
0 | 808 if (!arg) |
809 continue; | |
810 | |
811 // Look for tuple with 0 members | |
812 if (arg.op == TOK.TOKtype) | |
57 | 813 { |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
79
diff
changeset
|
814 auto e = cast(TypeExp)arg; |
0 | 815 if (e.type.toBasetype().ty == TY.Ttuple) |
816 { | |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
79
diff
changeset
|
817 auto tt = cast(TypeTuple)e.type.toBasetype(); |
0 | 818 |
819 if (!tt.arguments || tt.arguments.dim == 0) | |
820 { | |
821 exps.remove(i); | |
822 if (i == exps.dim) | |
823 return; | |
824 i--; | |
825 continue; | |
826 } | |
827 } | |
828 } | |
829 | |
830 // Inline expand all the tuples | |
831 while (arg.op == TOK.TOKtuple) | |
832 { | |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
79
diff
changeset
|
833 auto te = cast(TupleExp)arg; |
0 | 834 |
835 exps.remove(i); // remove arg | |
57 | 836 exps.insert(i, te.exps); // replace with tuple contents |
0 | 837 |
838 if (i == exps.dim) | |
839 return; // empty tuple, no more arguments | |
840 | |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
79
diff
changeset
|
841 arg = exps[i]; |
0 | 842 } |
843 } | |
844 } | |
845 } | |
846 | |
847 /************************************************** | |
848 * Write out argument types to buf. | |
849 */ | |
850 | |
851 void argExpTypesToCBuffer(OutBuffer buf, Expressions arguments, HdrGenState* hgs) | |
852 { | |
853 if (arguments) | |
854 { | |
855 scope OutBuffer argbuf = new OutBuffer(); | |
856 | |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
79
diff
changeset
|
857 foreach (size_t i, Expression arg; arguments) |
0 | 858 { |
859 if (i) | |
860 buf.writeByte(','); | |
861 | |
862 argbuf.reset(); | |
863 arg.type.toCBuffer2(argbuf, hgs, MOD.MODundefined); | |
864 buf.write(argbuf); | |
865 } | |
866 } | |
867 } | |
868 | |
869 /**************************************** | |
870 * Determine if scope sc has package level access to s. | |
871 */ | |
872 | |
873 bool hasPackageAccess(Scope sc, Dsymbol s) | |
874 { | |
875 version (LOG) { | |
876 printf("hasPackageAccess(s = '%s', sc = '%p')\n", s.toChars(), sc); | |
877 } | |
878 | |
879 for (; s; s = s.parent) | |
880 { | |
881 if (s.isPackage() && !s.isModule()) | |
882 break; | |
883 } | |
884 version (LOG) { | |
885 if (s) | |
886 printf("\tthis is in package '%s'\n", s.toChars()); | |
887 } | |
888 | |
889 if (s && s == sc.module_.parent) | |
890 { | |
891 version (LOG) { | |
892 printf("\ts is in same package as sc\n"); | |
893 } | |
894 return true; | |
895 } | |
896 | |
897 | |
898 version (LOG) { | |
899 printf("\tno package access\n"); | |
900 } | |
901 | |
902 return false; | |
903 } | |
904 | |
905 /********************************************* | |
906 * Call copy constructor for struct value argument. | |
907 */ | |
908 version (DMDV2) { | |
909 Expression callCpCtor(Loc loc, Scope sc, Expression e) | |
910 { | |
911 Type tb = e.type.toBasetype(); | |
912 assert(tb.ty == Tstruct); | |
913 StructDeclaration sd = (cast(TypeStruct)tb).sym; | |
914 if (sd.cpctor) | |
915 { | |
916 /* Create a variable tmp, and replace the argument e with: | |
917 * (tmp = e),tmp | |
918 * and let AssignExp() handle the construction. | |
919 * This is not the most efficent, ideally tmp would be constructed | |
920 * directly onto the stack. | |
921 */ | |
922 Identifier idtmp = Lexer.uniqueId("__tmp"); | |
923 VarDeclaration tmp = new VarDeclaration(loc, tb, idtmp, new ExpInitializer(Loc(0), e)); | |
924 Expression ae = new DeclarationExp(loc, tmp); | |
925 e = new CommaExp(loc, ae, new VarExp(loc, tmp)); | |
926 e = e.semantic(sc); | |
927 } | |
928 return e; | |
929 } | |
930 } | |
931 | |
932 /*************************************** | |
933 * Create a static array of TypeInfo references | |
934 * corresponding to an array of Expression's. | |
935 * Used to supply hidden _arguments[] value for variadic D functions. | |
936 */ | |
937 | |
938 Expression createTypeInfoArray(Scope sc, Expression* exps, int dim) | |
939 { | |
79 | 940 static if (true) |
941 { | |
57 | 942 /* Get the corresponding TypeInfo_Tuple and |
943 * point at its elements[]. | |
944 */ | |
945 | |
946 /* Create the TypeTuple corresponding to the types of args[] | |
947 */ | |
948 Arguments args = new Arguments; | |
949 args.setDim(dim); | |
950 for (size_t i = 0; i < dim; i++) | |
951 { | |
952 Argument arg = new Argument(STCin, exps[i].type, null, null); | |
953 args.data[i] = cast(void*)arg; | |
954 } | |
955 TypeTuple tup = new TypeTuple(args); | |
956 Expression e = tup.getTypeInfo(sc); | |
957 e = e.optimize(WANTvalue); | |
958 assert(e.op == TOKsymoff); // should be SymOffExp | |
959 | |
79 | 960 version (BREAKABI) |
961 { | |
57 | 962 /* |
963 * Should just pass a reference to TypeInfo_Tuple instead, | |
964 * but that would require existing code to be recompiled. | |
965 * Source compatibility can be maintained by computing _arguments[] | |
966 * at the start of the called function by offseting into the | |
967 * TypeInfo_Tuple reference. | |
968 */ | |
969 | |
79 | 970 } |
971 else | |
972 { | |
57 | 973 // Advance to elements[] member of TypeInfo_Tuple |
974 SymOffExp se = cast(SymOffExp)e; | |
975 se.offset += PTRSIZE + PTRSIZE; | |
976 | |
977 // Set type to TypeInfo[]* | |
978 se.type = Type.typeinfo.type.arrayOf().pointerTo(); | |
979 | |
980 // Indirect to get the _arguments[] value | |
981 e = new PtrExp(Loc(0), se); | |
982 e.type = se.type.next; | |
983 } | |
984 return e; | |
79 | 985 } // of static if (true) |
986 else | |
987 { | |
57 | 988 /* Improvements: |
989 * 1) create an array literal instead, | |
990 * as it would eliminate the extra dereference of loading the | |
991 * static variable. | |
992 */ | |
993 | |
994 ArrayInitializer ai = new ArrayInitializer(Loc(0)); | |
995 VarDeclaration v; | |
996 Type t; | |
997 Expression e; | |
998 scope OutBuffer buf = new OutBuffer(); | |
999 Identifier id; | |
1000 string name; | |
1001 | |
1002 // Generate identifier for _arguments[] | |
1003 buf.writestring("_arguments_"); | |
1004 for (int i = 0; i < dim; i++) | |
1005 { | |
1006 t = exps[i].type; | |
1007 t.toDecoBuffer(buf); | |
1008 } | |
1009 buf.writeByte(0); | |
1010 id = Lexer.idPool(buf.extractString()); | |
1011 | |
1012 Module m = sc.module_; | |
1013 Dsymbol s = m.symtab.lookup(id); | |
1014 | |
1015 if (s && s.parent == m) | |
1016 { | |
1017 // Use existing one | |
1018 v = s.isVarDeclaration(); | |
1019 assert(v); | |
1020 } | |
1021 else | |
1022 { | |
1023 // Generate new one | |
1024 | |
1025 for (int i = 0; i < dim; i++) | |
1026 { | |
1027 t = exps[i].type; | |
1028 e = t.getTypeInfo(sc); | |
1029 ai.addInit(new IntegerExp(i), new ExpInitializer(0, e)); | |
1030 } | |
1031 | |
1032 t = Type.typeinfo.type.arrayOf(); | |
1033 ai.type = t; | |
1034 v = new VarDeclaration(0, t, id, ai); | |
1035 m.members.push(v); | |
79 | 1036 m.symtabInsert(v); |
57 | 1037 sc = sc.push(); |
1038 sc.linkage = LINKc; | |
1039 sc.stc = STCstatic | STCcomdat; | |
1040 ai.semantic(sc, t); | |
1041 v.semantic(sc); | |
1042 v.parent = m; | |
1043 sc = sc.pop(); | |
1044 } | |
1045 e = new VarExp(0, v); | |
1046 e = e.semantic(sc); | |
1047 return e; | |
1048 } | |
0 | 1049 } |
1050 | |
1051 /************************************** | |
1052 * Evaluate builtin function. | |
1053 * Return result: null if cannot evaluate it. | |
1054 */ | |
1055 | |
53 | 1056 extern(C) extern real sinl(real); |
1057 extern(C) extern real cosl(real); | |
1058 extern(C) extern real tanl(real); | |
1059 extern(C) extern real sqrtl(real); | |
1060 extern(C) extern real fabsl(real); | |
1061 | |
0 | 1062 Expression eval_builtin(BUILTIN builtin, Expressions arguments) |
1063 { | |
53 | 1064 assert(arguments && arguments.dim); |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
79
diff
changeset
|
1065 auto arg0 = arguments[0]; |
53 | 1066 Expression e = null; |
1067 switch (builtin) | |
1068 { | |
1069 case BUILTINsin: | |
1070 if (arg0.op == TOKfloat64) | |
1071 e = new RealExp(Loc(0), sinl(arg0.toReal()), arg0.type); | |
1072 break; | |
1073 | |
1074 case BUILTINcos: | |
1075 if (arg0.op == TOKfloat64) | |
1076 e = new RealExp(Loc(0), cosl(arg0.toReal()), arg0.type); | |
1077 break; | |
1078 | |
1079 case BUILTINtan: | |
1080 if (arg0.op == TOKfloat64) | |
1081 e = new RealExp(Loc(0), tanl(arg0.toReal()), arg0.type); | |
1082 break; | |
1083 | |
1084 case BUILTINsqrt: | |
1085 if (arg0.op == TOKfloat64) | |
1086 e = new RealExp(Loc(0), sqrtl(arg0.toReal()), arg0.type); | |
1087 break; | |
1088 | |
1089 case BUILTINfabs: | |
1090 if (arg0.op == TOKfloat64) | |
1091 e = new RealExp(Loc(0), fabsl(arg0.toReal()), arg0.type); | |
1092 break; | |
1093 } | |
1094 return e; | |
0 | 1095 } |
1096 | |
1097 Expression fromConstInitializer(int result, Expression e1) | |
1098 { | |
1099 //printf("fromConstInitializer(result = %x, %s)\n", result, e1.toChars()); | |
1100 //static int xx; if (xx++ == 10) assert(0); | |
1101 Expression e = e1; | |
1102 if (e1.op == TOK.TOKvar) | |
1103 { | |
1104 VarExp ve = cast(VarExp)e1; | |
1105 VarDeclaration v = ve.var.isVarDeclaration(); | |
1106 e = expandVar(result, v); | |
1107 if (e) | |
1108 { | |
1109 if (e.type != e1.type) | |
1110 { | |
1111 // Type 'paint' operation | |
1112 e = e.copy(); | |
1113 e.type = e1.type; | |
1114 } | |
79 | 1115 e.loc = e1.loc; |
0 | 1116 } |
1117 else | |
1118 { | |
1119 e = e1; | |
1120 } | |
1121 } | |
1122 return e; | |
1123 } | |
1124 | |
1125 /************************************* | |
1126 * If variable has a const initializer, | |
1127 * return that initializer. | |
1128 */ | |
1129 | |
1130 Expression expandVar(int result, VarDeclaration v) | |
1131 { | |
1132 //printf("expandVar(result = %d, v = %p, %s)\n", result, v, v ? v.toChars() : "null"); | |
1133 | |
1134 Expression e = null; | |
1135 if (!v) | |
1136 return e; | |
1137 | |
1138 if (v.isConst() || v.isInvariant() || v.storage_class & STC.STCmanifest) | |
1139 { | |
1140 if (!v.type) | |
1141 { | |
1142 //error("ICE"); | |
1143 return e; | |
1144 } | |
1145 | |
1146 Type tb = v.type.toBasetype(); | |
1147 if (result & WANT.WANTinterpret || v.storage_class & STC.STCmanifest || (tb.ty != TY.Tsarray && tb.ty != TY.Tstruct)) | |
1148 { | |
1149 if (v.init) | |
1150 { | |
1151 if (v.inuse) | |
1152 { | |
1153 if (v.storage_class & STC.STCmanifest) | |
1154 v.error("recursive initialization of constant"); | |
1155 goto L1; | |
1156 } | |
1157 Expression ei = v.init.toExpression(); | |
1158 if (!ei) | |
1159 goto L1; | |
1160 if (ei.op == TOK.TOKconstruct || ei.op == TOK.TOKblit) | |
1161 { | |
1162 AssignExp ae = cast(AssignExp)ei; | |
1163 ei = ae.e2; | |
1164 if (ei.isConst() != 1 && ei.op != TOK.TOKstring) | |
1165 goto L1; | |
1166 if (ei.type != v.type) | |
1167 goto L1; | |
1168 } | |
1169 if (v.scope_) | |
1170 { | |
1171 v.inuse++; | |
1172 e = ei.syntaxCopy(); | |
1173 e = e.semantic(v.scope_); | |
1174 e = e.implicitCastTo(v.scope_, v.type); | |
1175 // enabling this line causes test22 in test suite to fail | |
1176 //ei.type = e.type; | |
1177 v.scope_ = null; | |
1178 v.inuse--; | |
1179 } | |
1180 else if (!ei.type) | |
1181 { | |
1182 goto L1; | |
1183 } | |
1184 else | |
1185 // Should remove the copy() operation by | |
1186 // making all mods to expressions copy-on-write | |
1187 e = ei.copy(); | |
1188 } | |
1189 else | |
1190 { | |
1191 static if (true) { | |
1192 goto L1; | |
1193 } else { | |
1194 // BUG: what if const is initialized in constructor? | |
1195 e = v.type.defaultInit(); | |
1196 e.loc = e1.loc; | |
1197 } | |
1198 } | |
1199 if (e.type != v.type) | |
1200 { | |
1201 e = e.castTo(null, v.type); | |
1202 } | |
1203 v.inuse++; | |
1204 e = e.optimize(result); | |
1205 v.inuse--; | |
1206 } | |
1207 } | |
1208 L1: | |
1209 //if (e) printf("\te = %s, e.type = %s\n", e.toChars(), e.type.toChars()); | |
1210 return e; | |
1211 } | |
1212 | |
1213 /**************************************** | |
1214 * Check access to d for expression e.d | |
1215 */ | |
1216 | |
1217 void accessCheck(Loc loc, Scope sc, Expression e, Declaration d) | |
1218 { | |
1219 version (LOG) { | |
1220 if (e) | |
1221 { | |
1222 printf("accessCheck(%s . %s)\n", e.toChars(), d.toChars()); | |
1223 printf("\te.type = %s\n", e.type.toChars()); | |
1224 } | |
1225 else | |
1226 { | |
1227 //printf("accessCheck(%s)\n", d.toChars()); | |
1228 } | |
1229 } | |
1230 if (!e) | |
1231 { | |
1232 if (d.prot() == PROT.PROTprivate && d.getModule() != sc.module_ || | |
1233 d.prot() == PROT.PROTpackage && !hasPackageAccess(sc, d)) | |
1234 | |
1235 error(loc, "%s %s.%s is not accessible from %s", | |
1236 d.kind(), d.getModule().toChars(), d.toChars(), sc.module_.toChars()); | |
1237 } | |
1238 else if (e.type.ty == TY.Tclass) | |
1239 { | |
1240 // Do access check | |
1241 ClassDeclaration cd; | |
1242 | |
1243 cd = cast(ClassDeclaration)((cast(TypeClass)e.type).sym); | |
1244 static if (true) { | |
1245 if (e.op == TOK.TOKsuper) | |
1246 { | |
1247 ClassDeclaration cd2 = sc.func.toParent().isClassDeclaration(); | |
1248 if (cd2) | |
1249 cd = cd2; | |
1250 } | |
1251 } | |
1252 cd.accessCheck(loc, sc, d); | |
1253 } | |
1254 else if (e.type.ty == TY.Tstruct) | |
1255 { | |
1256 // Do access check | |
1257 StructDeclaration cd = cast(StructDeclaration)((cast(TypeStruct)e.type).sym); | |
1258 cd.accessCheck(loc, sc, d); | |
1259 } | |
1260 } | |
1261 | |
1262 /***************************************** | |
1263 * Given array of arguments and an aggregate type, | |
1264 * if any of the argument types are missing, attempt to infer | |
1265 * them from the aggregate type. | |
1266 */ | |
1267 | |
1268 void inferApplyArgTypes(TOK op, Arguments arguments, Expression aggr) | |
1269 { | |
1270 if (!arguments || !arguments.dim) | |
1271 return; | |
1272 | |
1273 /* Return if no arguments need types. | |
1274 */ | |
1275 for (size_t u = 0; 1; u++) | |
1276 { | |
1277 if (u == arguments.dim) | |
1278 return; | |
1279 | |
1280 Argument arg = cast(Argument)arguments.data[u]; | |
1281 if (!arg.type) | |
1282 break; | |
1283 } | |
1284 | |
1285 AggregateDeclaration ad; | |
1286 | |
1287 Argument arg = cast(Argument)arguments.data[0]; | |
1288 Type taggr = aggr.type; | |
1289 if (!taggr) | |
1290 return; | |
1291 Type tab = taggr.toBasetype(); | |
1292 switch (tab.ty) | |
1293 { | |
1294 case TY.Tarray: | |
1295 case TY.Tsarray: | |
1296 case TY.Ttuple: | |
1297 if (arguments.dim == 2) | |
1298 { | |
1299 if (!arg.type) | |
1300 arg.type = Type.tsize_t; // key type | |
1301 arg = cast(Argument)arguments.data[1]; | |
1302 } | |
1303 if (!arg.type && tab.ty != TY.Ttuple) | |
1304 arg.type = tab.nextOf(); // value type | |
1305 break; | |
1306 | |
1307 case TY.Taarray: | |
1308 { | |
1309 TypeAArray taa = cast(TypeAArray)tab; | |
1310 | |
1311 if (arguments.dim == 2) | |
1312 { | |
1313 if (!arg.type) | |
1314 arg.type = taa.index; // key type | |
1315 arg = cast(Argument)arguments.data[1]; | |
1316 } | |
1317 if (!arg.type) | |
1318 arg.type = taa.next; // value type | |
1319 break; | |
1320 } | |
1321 | |
1322 case TY.Tclass: | |
1323 ad = (cast(TypeClass)tab).sym; | |
1324 goto Laggr; | |
1325 | |
1326 case TY.Tstruct: | |
1327 ad = (cast(TypeStruct)tab).sym; | |
1328 goto Laggr; | |
1329 | |
1330 Laggr: | |
1331 if (arguments.dim == 1) | |
1332 { | |
1333 if (!arg.type) | |
1334 { | |
1335 /* Look for a head() or rear() overload | |
1336 */ | |
1337 Identifier id = (op == TOK.TOKforeach) ? Id.Fhead : Id.Ftoe; | |
1338 Dsymbol s = search_function(ad, id); | |
1339 FuncDeclaration fd = s ? s.isFuncDeclaration() : null; | |
1340 if (!fd) | |
1341 { | |
1342 if (s && s.isTemplateDeclaration()) | |
1343 break; | |
1344 goto Lapply; | |
1345 } | |
1346 arg.type = fd.type.nextOf(); | |
1347 } | |
1348 break; | |
1349 } | |
1350 | |
1351 Lapply: | |
1352 { /* Look for an | |
1353 * int opApply(int delegate(ref Type [, ...]) dg); | |
1354 * overload | |
1355 */ | |
1356 Dsymbol s = search_function(ad, (op == TOK.TOKforeach_reverse) ? Id.applyReverse : Id.apply); | |
1357 if (s) | |
1358 { | |
1359 FuncDeclaration fd = s.isFuncDeclaration(); | |
1360 if (fd) | |
1361 { | |
1362 inferApplyArgTypesX(fd, arguments); | |
1363 break; | |
1364 } | |
1365 static if (false) { | |
1366 TemplateDeclaration td = s.isTemplateDeclaration(); | |
1367 if (td) | |
1368 { | |
1369 inferApplyArgTypesZ(td, arguments); | |
1370 break; | |
1371 } | |
1372 } | |
1373 } | |
1374 break; | |
1375 } | |
1376 | |
1377 case TY.Tdelegate: | |
1378 { | |
1379 if (0 && aggr.op == TOK.TOKdelegate) | |
1380 { | |
1381 DelegateExp de = cast(DelegateExp)aggr; | |
1382 | |
1383 FuncDeclaration fd = de.func.isFuncDeclaration(); | |
1384 if (fd) | |
1385 inferApplyArgTypesX(fd, arguments); | |
1386 } | |
1387 else | |
1388 { | |
1389 inferApplyArgTypesY(cast(TypeFunction)tab.nextOf(), arguments); | |
1390 } | |
1391 break; | |
1392 } | |
1393 | |
1394 default: | |
1395 break; // ignore error, caught later | |
1396 } | |
1397 } | |
1398 | |
1399 struct Param3 | |
1400 { | |
1401 /******************************** | |
1402 * Recursive helper function, | |
1403 * analogous to func.overloadResolveX(). | |
1404 */ | |
1405 | |
17
ddae60498573
Implemented mixin statements, DefaultInitExps, FileInitExps, LineInitExps and __traits.
Robert Clipsham <robert@octarineparrot.com>
parents:
16
diff
changeset
|
1406 int fp3(void*, FuncDeclaration f) |
0 | 1407 { |
1408 TypeFunction tf = cast(TypeFunction)f.type; | |
1409 if (inferApplyArgTypesY(tf, arguments) == 1) | |
1410 return 0; | |
1411 | |
1412 if (arguments.dim == 0) | |
1413 return 1; | |
1414 | |
1415 return 0; | |
1416 } | |
1417 | |
1418 Arguments arguments; | |
1419 } | |
1420 | |
1421 void inferApplyArgTypesX(FuncDeclaration fstart, Arguments arguments) | |
1422 { | |
1423 Param3 p3; | |
1424 p3.arguments = arguments; | |
17
ddae60498573
Implemented mixin statements, DefaultInitExps, FileInitExps, LineInitExps and __traits.
Robert Clipsham <robert@octarineparrot.com>
parents:
16
diff
changeset
|
1425 overloadApply(fstart, &p3.fp3, cast(void*)arguments); |
0 | 1426 } |
1427 | |
1428 /****************************** | |
1429 * Infer arguments from type of function. | |
1430 * Returns: | |
1431 * 0 match for this function | |
1432 * 1 no match for this function | |
1433 */ | |
1434 | |
1435 int inferApplyArgTypesY(TypeFunction tf, Arguments arguments) | |
1436 { | |
1437 size_t nparams; | |
1438 Argument p; | |
1439 | |
1440 if (Argument.dim(tf.parameters) != 1) | |
1441 goto Lnomatch; | |
1442 | |
1443 p = Argument.getNth(tf.parameters, 0); | |
1444 if (p.type.ty != TY.Tdelegate) | |
1445 goto Lnomatch; | |
1446 | |
1447 tf = cast(TypeFunction)p.type.nextOf(); | |
1448 assert(tf.ty == TY.Tfunction); | |
1449 | |
1450 /* We now have tf, the type of the delegate. Match it against | |
1451 * the arguments, filling in missing argument types. | |
1452 */ | |
1453 nparams = Argument.dim(tf.parameters); | |
1454 if (nparams == 0 || tf.varargs) | |
1455 goto Lnomatch; // not enough parameters | |
1456 if (arguments.dim != nparams) | |
1457 goto Lnomatch; // not enough parameters | |
1458 | |
1459 for (size_t u = 0; u < nparams; u++) | |
1460 { | |
1461 Argument arg = cast(Argument)arguments.data[u]; | |
1462 Argument param = Argument.getNth(tf.parameters, u); | |
1463 if (arg.type) | |
1464 { | |
1465 if (!arg.type.equals(param.type)) | |
1466 { | |
1467 /* Cannot resolve argument types. Indicate an | |
1468 * error by setting the number of arguments to 0. | |
1469 */ | |
1470 arguments.dim = 0; | |
1471 goto Lmatch; | |
1472 } | |
1473 continue; | |
1474 } | |
1475 arg.type = param.type; | |
1476 } | |
1477 | |
1478 Lmatch: | |
1479 return 0; | |
1480 | |
1481 Lnomatch: | |
1482 return 1; | |
1483 } | |
1484 | |
1485 /************************************************** | |
1486 * Write expression out to buf, but wrap it | |
1487 * in ( ) if its precedence is less than pr. | |
1488 */ | |
1489 | |
1490 void expToCBuffer(OutBuffer buf, HdrGenState* hgs, Expression e, PREC pr) | |
1491 { | |
1492 //if (precedence[e.op] == 0) e.dump(0); | |
1493 if (precedence[e.op] < pr || | |
1494 /* Despite precedence, we don't allow a<b<c expressions. | |
1495 * They must be parenthesized. | |
1496 */ | |
1497 (pr == PREC.PREC_rel && precedence[e.op] == pr)) | |
1498 { | |
1499 buf.writeByte('('); | |
1500 e.toCBuffer(buf, hgs); | |
1501 buf.writeByte(')'); | |
1502 } | |
1503 else | |
1504 e.toCBuffer(buf, hgs); | |
1505 } | |
1506 | |
1507 /************************************************** | |
1508 * Write out argument list to buf. | |
1509 */ | |
1510 | |
1511 void argsToCBuffer(OutBuffer buf, Expressions arguments, HdrGenState* hgs) | |
1512 { | |
1513 if (arguments) | |
1514 { | |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
79
diff
changeset
|
1515 foreach (size_t i, Expression arg; arguments) |
0 | 1516 { |
1517 if (arg) | |
1518 { | |
1519 if (i) | |
1520 buf.writeByte(','); | |
1521 expToCBuffer(buf, hgs, arg, PREC.PREC_assign); | |
1522 } | |
1523 } | |
1524 } | |
16
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
1525 } |
53 | 1526 |
1527 ulong getMask(ulong v) | |
1528 { | |
1529 ulong u = 0; | |
1530 if (v >= 0x80) | |
1531 u = 0xFF; | |
1532 while (u < v) | |
1533 u = (u << 1) | 1; | |
1534 return u; | |
64 | 1535 } |
1536 | |
1537 /****************************** | |
1538 * Perform scanForNestedRef() on an array of Expressions. | |
1539 */ | |
1540 | |
1541 void arrayExpressionScanForNestedRef(Scope sc, Expressions a) | |
1542 { | |
1543 //printf("arrayExpressionScanForNestedRef(%p)\n", a); | |
1544 if (a) | |
1545 { | |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
79
diff
changeset
|
1546 foreach (e; a) |
64 | 1547 { |
1548 if (e) | |
1549 { | |
1550 e.scanForNestedRef(sc); | |
1551 } | |
1552 } | |
1553 } | |
53 | 1554 } |