Mercurial > projects > ddmd
annotate dmd/expression/Util.d @ 16:5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
author | Robert Clipsham <robert@octarineparrot.com> |
---|---|
date | Sun, 04 Apr 2010 22:41:11 +0100 |
parents | 10317f0c89a5 |
children | ddae60498573 |
rev | line source |
---|---|
0 | 1 module dmd.expression.Util; |
2 | |
3 import dmd.Expression; | |
4 import dmd.Loc; | |
5 import dmd.BUILTIN; | |
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; | |
37 import dmd.TypeFunction; | |
38 import dmd.declaration.Match; | |
39 import dmd.ArrayTypes; | |
40 import dmd.Declaration; | |
41 import dmd.FuncAliasDeclaration; | |
42 import dmd.AliasDeclaration; | |
43 import dmd.FuncDeclaration; | |
44 import dmd.TemplateDeclaration; | |
45 import dmd.AggregateDeclaration; | |
46 import dmd.IntegerExp; | |
47 import dmd.Type; | |
48 import dmd.TOK; | |
49 import dmd.TypeExp; | |
50 import dmd.TypeTuple; | |
51 import dmd.TupleExp; | |
52 import dmd.OutBuffer; | |
53 import dmd.HdrGenState; | |
54 import dmd.ClassDeclaration; | |
55 import dmd.TypeClass; | |
56 import dmd.StructDeclaration; | |
57 import dmd.TypeStruct; | |
58 import dmd.MOD; | |
59 import dmd.PROT; | |
60 import dmd.PREC; | |
61 import dmd.Util; | |
62 import dmd.TypeAArray; | |
63 import dmd.Id; | |
64 | |
65 import std.stdio : writef; | |
66 | |
67 | |
68 /*********************************** | |
69 * Utility to build a function call out of this reference and argument. | |
70 */ | |
71 Expression build_overload(Loc loc, Scope sc, Expression ethis, Expression earg, Identifier id) | |
72 { | |
73 Expression e; | |
74 | |
75 //printf("build_overload(id = '%s')\n", id.toChars()); | |
76 //earg.print(); | |
77 //earg.type.print(); | |
78 e = new DotIdExp(loc, ethis, id); | |
79 | |
80 if (earg) | |
81 e = new CallExp(loc, e, earg); | |
82 else | |
83 e = new CallExp(loc, e); | |
84 | |
85 e = e.semantic(sc); | |
86 return e; | |
87 } | |
88 | |
89 /*************************************** | |
90 * Search for function funcid in aggregate ad. | |
91 */ | |
92 | |
93 Dsymbol search_function(ScopeDsymbol ad, Identifier funcid) | |
94 { | |
95 Dsymbol s; | |
96 FuncDeclaration fd; | |
97 TemplateDeclaration td; | |
98 | |
99 s = ad.search(Loc(0), funcid, 0); | |
100 if (s) | |
101 { | |
102 Dsymbol s2; | |
103 | |
104 //printf("search_function: s = '%s'\n", s.kind()); | |
105 s2 = s.toAlias(); | |
106 //printf("search_function: s2 = '%s'\n", s2.kind()); | |
107 fd = s2.isFuncDeclaration(); | |
108 if (fd && fd.type.ty == TY.Tfunction) | |
109 return fd; | |
110 | |
111 td = s2.isTemplateDeclaration(); | |
112 if (td) | |
113 return td; | |
114 } | |
115 | |
116 return null; | |
117 } | |
118 | |
119 /******************************************** | |
120 * Find function in overload list that exactly matches t. | |
121 */ | |
122 | |
123 /*************************************************** | |
124 * Visit each overloaded function in turn, and call | |
125 * dg(param, f) on it. | |
126 * Exit when no more, or dg(param, f) returns 1. | |
127 * Returns: | |
128 * 0 continue | |
129 * 1 done | |
130 */ | |
131 | |
132 int overloadApply(FuncDeclaration fstart, int delegate(FuncDeclaration) dg) | |
133 { | |
134 FuncDeclaration f; | |
135 Declaration d; | |
136 Declaration next; | |
137 | |
138 for (d = fstart; d; d = next) | |
139 { | |
140 FuncAliasDeclaration fa = d.isFuncAliasDeclaration(); | |
141 | |
142 if (fa) | |
143 { | |
144 if (overloadApply(fa.funcalias, dg)) | |
145 return 1; | |
146 next = fa.overnext; | |
147 } | |
148 else | |
149 { | |
150 AliasDeclaration a = d.isAliasDeclaration(); | |
151 | |
152 if (a) | |
153 { | |
154 Dsymbol s = a.toAlias(); | |
155 next = s.isDeclaration(); | |
156 if (next is a) | |
157 break; | |
158 if (next is fstart) | |
159 break; | |
160 } | |
161 else | |
162 { | |
163 f = d.isFuncDeclaration(); | |
164 if (f is null) | |
165 { | |
166 d.error("is aliased to a function"); | |
167 break; // BUG: should print error message? | |
168 } | |
169 if (dg(f)) | |
170 return 1; | |
171 | |
172 next = f.overnext; | |
173 } | |
174 } | |
175 } | |
176 return 0; | |
177 } | |
178 | |
179 /******************************************** | |
180 * Decide which function matches the arguments best. | |
181 */ | |
182 | |
183 struct Param2 | |
184 { | |
185 Match* m; | |
186 Expression ethis; | |
187 Expressions arguments; | |
188 | |
189 int fp2(FuncDeclaration f) | |
190 { | |
191 MATCH match; | |
192 | |
193 if (f != m.lastf) // skip duplicates | |
194 { | |
195 m.anyf = f; | |
196 TypeFunction tf = cast(TypeFunction)f.type; | |
197 match = tf.callMatch(f.needThis() ? ethis : null, arguments); | |
198 //printf("match = %d\n", match); | |
199 if (match != MATCH.MATCHnomatch) | |
200 { | |
201 if (match > m.last) | |
202 goto LfIsBetter; | |
203 | |
204 if (match < m.last) | |
205 goto LlastIsBetter; | |
206 | |
207 /* See if one of the matches overrides the other. | |
208 */ | |
209 if (m.lastf.overrides(f)) | |
210 goto LlastIsBetter; | |
211 else if (f.overrides(m.lastf)) | |
212 goto LfIsBetter; | |
213 | |
214 /* Try to disambiguate using template-style partial ordering rules. | |
215 * In essence, if f() and g() are ambiguous, if f() can call g(), | |
216 * but g() cannot call f(), then pick f(). | |
217 * This is because f() is "more specialized." | |
218 */ | |
219 { | |
220 MATCH c1 = f.leastAsSpecialized(m.lastf); | |
221 MATCH c2 = m.lastf.leastAsSpecialized(f); | |
222 //printf("c1 = %d, c2 = %d\n", c1, c2); | |
223 if (c1 > c2) | |
224 goto LfIsBetter; | |
225 if (c1 < c2) | |
226 goto LlastIsBetter; | |
227 } | |
228 | |
229 Lambiguous: | |
230 m.nextf = f; | |
231 m.count++; | |
232 return 0; | |
233 | |
234 LfIsBetter: | |
235 m.last = match; | |
236 m.lastf = f; | |
237 m.count = 1; | |
238 return 0; | |
239 | |
240 LlastIsBetter: | |
241 return 0; | |
242 } | |
243 } | |
244 return 0; | |
245 } | |
246 } | |
247 | |
248 struct Param1 | |
249 { | |
250 Type t; // type to match | |
251 FuncDeclaration f; // return value | |
252 | |
253 int fp1(FuncDeclaration f) | |
254 { | |
255 if (t.equals(f.type)) | |
256 { | |
257 this.f = f; | |
258 return 1; | |
259 } | |
260 | |
261 version (DMDV2) { | |
262 /* Allow covariant matches, if it's just a const conversion | |
263 * of the return type | |
264 */ | |
265 if (t.ty == Tfunction) | |
266 { | |
267 TypeFunction tf = cast(TypeFunction)f.type; | |
268 if (tf.covariant(t) == 1 && | |
269 tf.nextOf().implicitConvTo(t.nextOf()) >= MATCHconst) | |
270 { | |
271 this.f = f; | |
272 return 1; | |
273 } | |
274 } | |
275 } | |
276 return 0; | |
277 } | |
278 } | |
279 | |
280 void overloadResolveX(Match* m, FuncDeclaration fstart, Expression ethis, Expressions arguments) | |
281 { | |
282 Param2 p; | |
283 p.m = m; | |
284 p.ethis = ethis; | |
285 p.arguments = arguments; | |
286 overloadApply(fstart, &p.fp2); | |
287 } | |
288 | |
289 void templateResolve(Match* m, TemplateDeclaration td, Scope sc, Loc loc, Objects targsi, Expression ethis, Expressions arguments) | |
290 { | |
291 FuncDeclaration fd; | |
292 | |
293 assert(td); | |
294 fd = td.deduceFunctionTemplate(sc, loc, targsi, ethis, arguments); | |
295 if (!fd) | |
296 return; | |
297 m.anyf = fd; | |
298 if (m.last >= MATCH.MATCHexact) | |
299 { | |
300 m.nextf = fd; | |
301 m.count++; | |
302 } | |
303 else | |
304 { | |
305 m.last = MATCH.MATCHexact; | |
306 m.lastf = fd; | |
307 m.count = 1; | |
308 } | |
309 } | |
310 | |
311 /****************************** | |
312 * Perform semantic() on an array of Expressions. | |
313 */ | |
314 | |
315 void arrayExpressionSemantic(Expressions exps, Scope sc) | |
316 { | |
317 if (exps) | |
318 { | |
319 for (size_t i = 0; i < exps.dim; i++) | |
320 { | |
321 Expression e = cast(Expression)exps.data[i]; | |
322 | |
323 e = e.semantic(sc); | |
324 exps.data[i] = cast(void*)e; | |
325 } | |
326 } | |
327 } | |
328 | |
329 /**************************************** | |
330 * Preprocess arguments to function. | |
331 */ | |
332 | |
333 void preFunctionArguments(Loc loc, Scope sc, Expressions exps) | |
334 { | |
335 if (exps) | |
336 { | |
337 expandTuples(exps); | |
338 | |
339 for (size_t i = 0; i < exps.dim; i++) | |
340 { | |
341 Expression arg = cast(Expression)exps.data[i]; | |
342 | |
343 if (!arg.type) | |
344 { | |
345 debug { | |
346 if (!global.gag) | |
347 writef("1: \n"); | |
348 } | |
349 arg.error("%s is not an expression", arg.toChars()); | |
350 arg = new IntegerExp(arg.loc, 0, Type.tint32); | |
351 } | |
352 | |
353 arg = resolveProperties(sc, arg); | |
354 exps.data[i] = cast(void*) arg; | |
355 | |
356 //arg.rvalue(); | |
357 static if (false) { | |
358 if (arg.type.ty == TY.Tfunction) | |
359 { | |
360 arg = new AddrExp(arg.loc, arg); | |
361 arg = arg.semantic(sc); | |
362 exps.data[i] = cast(void*) arg; | |
363 } | |
364 } | |
365 } | |
366 } | |
367 } | |
368 | |
369 /************************************************************* | |
370 * Given var, we need to get the | |
371 * right 'this' pointer if var is in an outer class, but our | |
372 * existing 'this' pointer is in an inner class. | |
373 * Input: | |
374 * e1 existing 'this' | |
375 * ad struct or class we need the correct 'this' for | |
376 * var the specific member of ad we're accessing | |
377 */ | |
378 | |
379 Expression getRightThis(Loc loc, Scope sc, AggregateDeclaration ad, Expression e1, Declaration var) | |
380 { | |
381 //printf("\ngetRightThis(e1 = %s, ad = %s, var = %s)\n", e1.toChars(), ad.toChars(), var.toChars()); | |
382 L1: | |
383 Type t = e1.type.toBasetype(); | |
384 //printf("e1.type = %s, var.type = %s\n", e1.type.toChars(), var.type.toChars()); | |
385 | |
386 /* If e1 is not the 'this' pointer for ad | |
387 */ | |
388 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)) | |
389 { | |
390 ClassDeclaration cd = ad.isClassDeclaration(); | |
391 ClassDeclaration tcd = t.isClassHandle(); | |
392 | |
393 /* e1 is the right this if ad is a base class of e1 | |
394 */ | |
395 if (!cd || !tcd || !(tcd == cd || cd.isBaseOf(tcd, null))) | |
396 { | |
397 /* Only classes can be inner classes with an 'outer' | |
398 * member pointing to the enclosing class instance | |
399 */ | |
400 if (tcd && tcd.isNested()) | |
401 { | |
402 /* e1 is the 'this' pointer for an inner class: tcd. | |
403 * Rewrite it as the 'this' pointer for the outer class. | |
404 */ | |
405 | |
406 e1 = new DotVarExp(loc, e1, tcd.vthis); | |
407 e1.type = tcd.vthis.type; | |
408 // Do not call checkNestedRef() | |
409 //e1 = e1.semantic(sc); | |
410 | |
411 // Skip up over nested functions, and get the enclosing | |
412 // class type. | |
413 int n = 0; | |
414 Dsymbol s; | |
415 for (s = tcd.toParent(); s && s.isFuncDeclaration(); s = s.toParent()) | |
416 { | |
417 FuncDeclaration f = s.isFuncDeclaration(); | |
418 if (f.vthis) | |
419 { | |
420 //printf("rewriting e1 to %s's this\n", f.toChars()); | |
421 n++; | |
422 e1 = new VarExp(loc, f.vthis); | |
423 } | |
424 } | |
425 if (s && s.isClassDeclaration()) | |
426 { | |
427 e1.type = s.isClassDeclaration().type; | |
428 if (n > 1) | |
429 e1 = e1.semantic(sc); | |
430 } | |
431 else | |
432 e1 = e1.semantic(sc); | |
433 goto L1; | |
434 } | |
435 /* Can't find a path from e1 to ad | |
436 */ | |
437 e1.error("this for %s needs to be type %s not type %s", var.toChars(), ad.toChars(), t.toChars()); | |
438 } | |
439 } | |
440 return e1; | |
441 } | |
442 | |
443 /******************************************* | |
444 * Given a symbol that could be either a FuncDeclaration or | |
445 * a function template, resolve it to a function symbol. | |
446 * sc instantiation scope | |
447 * loc instantiation location | |
448 * targsi initial list of template arguments | |
449 * ethis if !null, the 'this' pointer argument | |
450 * fargs arguments to function | |
451 * flags 1: do not issue error message on no match, just return null | |
452 */ | |
453 | |
454 FuncDeclaration resolveFuncCall(Scope sc, Loc loc, Dsymbol s, | |
455 Objects tiargs, | |
456 Expression ethis, | |
457 Expressions arguments, | |
458 int flags) | |
459 { | |
460 if (!s) | |
461 return null; // no match | |
462 FuncDeclaration f = s.isFuncDeclaration(); | |
463 if (f) | |
464 f = f.overloadResolve(loc, ethis, arguments); | |
465 else | |
466 { | |
467 TemplateDeclaration td = s.isTemplateDeclaration(); | |
468 assert(td); | |
469 f = td.deduceFunctionTemplate(sc, loc, tiargs, null, arguments, flags); | |
470 } | |
471 return f; | |
472 } | |
473 | |
474 /**************************************** | |
475 * Now that we know the exact type of the function we're calling, | |
476 * the arguments[] need to be adjusted: | |
477 * 1. implicitly convert argument to the corresponding parameter type | |
478 * 2. add default arguments for any missing arguments | |
479 * 3. do default promotions on arguments corresponding to ... | |
480 * 4. add hidden _arguments[] argument | |
481 * 5. call copy constructor for struct value arguments | |
482 */ | |
483 | |
484 void functionArguments(Loc loc, Scope sc, TypeFunction tf, Expressions arguments) | |
485 { | |
486 uint n; | |
487 | |
488 //printf("functionArguments()\n"); | |
489 assert(arguments); | |
490 size_t nargs = arguments ? arguments.dim : 0; | |
491 size_t nparams = Argument.dim(tf.parameters); | |
492 | |
493 if (nargs > nparams && tf.varargs == 0) | |
494 error(loc, "expected %zu arguments, not %zu for non-variadic function type %s", nparams, nargs, tf.toChars()); | |
495 | |
496 n = (nargs > nparams) ? nargs : nparams; // n = max(nargs, nparams) | |
497 | |
498 int done = 0; | |
499 for (size_t i = 0; i < n; i++) | |
500 { | |
501 Expression arg; | |
502 | |
503 if (i < nargs) | |
504 arg = cast(Expression)arguments.data[i]; | |
505 else | |
506 arg = null; | |
507 | |
508 Type tb; | |
509 | |
510 if (i < nparams) | |
511 { | |
512 Argument p = Argument.getNth(tf.parameters, i); | |
513 | |
514 if (!arg) | |
515 { | |
516 if (!p.defaultArg) | |
517 { | |
518 if (tf.varargs == 2 && i + 1 == nparams) | |
519 goto L2; | |
520 | |
521 error(loc, "expected %d function arguments, not %d", nparams, nargs); | |
522 break; | |
523 } | |
524 arg = p.defaultArg; | |
525 version (DMDV2) { | |
526 if (arg.op == TOK.TOKdefault) | |
527 { | |
528 DefaultInitExp de = cast(DefaultInitExp)arg; | |
529 arg = de.resolve(loc, sc); | |
530 } | |
531 else | |
532 { | |
533 arg = arg.copy(); | |
534 } | |
535 } else { | |
536 arg = arg.copy(); | |
537 } | |
538 arguments.push(cast(void*)arg); | |
539 nargs++; | |
540 } | |
541 | |
542 if (tf.varargs == 2 && i + 1 == nparams) | |
543 { | |
544 //printf("\t\tvarargs == 2, p.type = '%s'\n", p.type.toChars()); | |
545 if (arg.implicitConvTo(p.type)) | |
546 { | |
547 if (nargs != nparams) | |
548 error(loc, "expected %zu function arguments, not %zu", nparams, nargs); | |
549 goto L1; | |
550 } | |
551 L2: | |
552 tb = p.type.toBasetype(); /// | |
553 Type tret = p.isLazyArray(); | |
554 switch (tb.ty) | |
555 { | |
556 case TY.Tsarray: | |
557 case TY.Tarray: | |
558 { // Create a static array variable v of type arg.type | |
559 version (IN_GCC) { | |
560 /* GCC 4.0 does not like zero length arrays used like | |
561 this; pass a null array value instead. Could also | |
562 just make a one-element array. */ | |
563 if (nargs - i == 0) | |
564 { | |
565 arg = new NullExp(loc); | |
566 break; | |
567 } | |
568 } | |
569 Identifier id = Lexer.uniqueId("__arrayArg"); | |
570 Type t = new TypeSArray((cast(TypeArray)tb).next, new IntegerExp(nargs - i)); | |
571 t = t.semantic(loc, sc); | |
572 VarDeclaration v = new VarDeclaration(loc, t, id, new VoidInitializer(loc)); | |
573 v.semantic(sc); | |
574 v.parent = sc.parent; | |
575 //sc.insert(v); | |
576 | |
577 Expression c = new DeclarationExp(Loc(0), v); | |
578 c.type = v.type; | |
579 | |
580 for (size_t u = i; u < nargs; u++) | |
581 { | |
582 Expression a = cast(Expression)arguments.data[u]; | |
583 if (tret && !(cast(TypeArray)tb).next.equals(a.type)) | |
584 a = a.toDelegate(sc, tret); | |
585 | |
586 Expression e = new VarExp(loc, v); | |
587 e = new IndexExp(loc, e, new IntegerExp(u + 1 - nparams)); | |
588 AssignExp ae = new AssignExp(loc, e, a); | |
589 | |
590 version (DMDV2) { | |
591 ae.op = TOK.TOKconstruct; | |
592 } | |
593 | |
594 if (c) | |
595 c = new CommaExp(loc, c, ae); | |
596 else | |
597 c = ae; | |
598 } | |
599 | |
600 arg = new VarExp(loc, v); | |
601 if (c) | |
602 arg = new CommaExp(loc, c, arg); | |
603 break; | |
604 } | |
605 | |
606 case TY.Tclass: | |
607 { /* Set arg to be: | |
608 * new Tclass(arg0, arg1, ..., argn) | |
609 */ | |
610 Expressions args = new Expressions(); | |
611 args.setDim(nargs - i); | |
612 for (size_t u = i; u < nargs; u++) | |
613 args.data[u - i] = arguments.data[u]; | |
614 arg = new NewExp(loc, null, null, p.type, args); | |
615 break; | |
616 } | |
617 | |
618 default: | |
619 if (!arg) | |
620 { | |
621 error(loc, "not enough arguments"); | |
622 return; | |
623 } | |
624 break; | |
625 } | |
626 | |
627 arg = arg.semantic(sc); | |
628 //printf("\targ = '%s'\n", arg.toChars()); | |
629 arguments.setDim(i + 1); | |
630 done = 1; | |
631 } | |
632 | |
633 L1: | |
634 if (!(p.storageClass & STC.STClazy && p.type.ty == TY.Tvoid)) | |
635 { | |
636 if (p.type != arg.type) | |
637 { | |
638 //printf("arg.type = %s, p.type = %s\n", arg.type.toChars(), p.type.toChars()); | |
639 arg = arg.implicitCastTo(sc, p.type); | |
640 arg = arg.optimize(WANT.WANTvalue); | |
641 } | |
642 } | |
643 if (p.storageClass & STC.STCref) | |
644 { | |
645 arg = arg.toLvalue(sc, arg); | |
646 } | |
647 else if (p.storageClass & STC.STCout) | |
648 { | |
649 arg = arg.modifiableLvalue(sc, arg); | |
650 } | |
651 | |
652 // Convert static arrays to pointers | |
653 tb = arg.type.toBasetype(); | |
654 if (tb.ty == TY.Tsarray) | |
655 { | |
656 arg = arg.checkToPointer(); | |
657 } | |
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) { | |
746 if (tb.ty == TY.Tstruct) | |
747 { | |
748 arg = callCpCtor(loc, sc, arg); | |
749 } | |
750 | |
751 // Give error for overloaded function addresses | |
752 if (arg.op == TOK.TOKsymoff) | |
753 { | |
754 SymOffExp se = cast(SymOffExp)arg; | |
755 if (se.hasOverloads && !se.var.isFuncDeclaration().isUnique()) | |
756 arg.error("function %s is overloaded", arg.toChars()); | |
757 } | |
758 } | |
759 arg.rvalue(); | |
760 } | |
761 arg = arg.optimize(WANT.WANTvalue); | |
762 arguments.data[i] = cast(void*) arg; | |
763 if (done) | |
764 break; | |
765 } | |
766 | |
767 // If D linkage and variadic, add _arguments[] as first argument | |
768 if (tf.linkage == LINK.LINKd && tf.varargs == 1) | |
769 { | |
770 Expression e = createTypeInfoArray(sc, cast(Expression*)&arguments.data[nparams], arguments.dim - nparams); | |
771 arguments.insert(0, cast(void*)e); | |
772 } | |
773 } | |
774 | |
16
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
775 /****************************** |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
776 * 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
|
777 */ |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
778 |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
779 version (DMDV2) { |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
780 bool arrayExpressionCanThrow(Expressions exps) |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
781 { |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
782 if (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 for (size_t i = 0; i < exps.dim; i++) |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
785 { Expression e = cast(Expression)exps.data[i]; |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
786 if (e && e.canThrow()) |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
787 return true; |
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 } |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
790 return false; |
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 } |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
793 |
0 | 794 /**************************************** |
795 * Expand tuples. | |
796 */ | |
797 | |
798 void expandTuples(Expressions exps) | |
799 { | |
800 //printf("expandTuples()\n"); | |
801 if (exps) | |
802 { | |
803 for (size_t i = 0; i < exps.dim; i++) | |
804 { | |
805 Expression arg = cast(Expression)exps.data[i]; | |
806 if (!arg) | |
807 continue; | |
808 | |
809 // Look for tuple with 0 members | |
810 if (arg.op == TOK.TOKtype) | |
811 { | |
812 TypeExp e = cast(TypeExp)arg; | |
813 if (e.type.toBasetype().ty == TY.Ttuple) | |
814 { | |
815 TypeTuple tt = cast(TypeTuple)e.type.toBasetype(); | |
816 | |
817 if (!tt.arguments || tt.arguments.dim == 0) | |
818 { | |
819 exps.remove(i); | |
820 if (i == exps.dim) | |
821 return; | |
822 i--; | |
823 continue; | |
824 } | |
825 } | |
826 } | |
827 | |
828 // Inline expand all the tuples | |
829 while (arg.op == TOK.TOKtuple) | |
830 { | |
831 TupleExp te = cast(TupleExp)arg; | |
832 | |
833 exps.remove(i); // remove arg | |
834 exps.insert(i, cast(void*)te.exps); // replace with tuple contents | |
835 | |
836 if (i == exps.dim) | |
837 return; // empty tuple, no more arguments | |
838 | |
839 arg = cast(Expression)exps.data[i]; | |
840 } | |
841 } | |
842 } | |
843 } | |
844 | |
845 /************************************************** | |
846 * Write out argument types to buf. | |
847 */ | |
848 | |
849 void argExpTypesToCBuffer(OutBuffer buf, Expressions arguments, HdrGenState* hgs) | |
850 { | |
851 if (arguments) | |
852 { | |
853 scope OutBuffer argbuf = new OutBuffer(); | |
854 | |
855 for (size_t i = 0; i < arguments.dim; i++) | |
856 { | |
857 Expression arg = cast(Expression)arguments.data[i]; | |
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 { | |
940 assert(false); | |
941 } | |
942 | |
943 /************************************** | |
944 * Evaluate builtin function. | |
945 * Return result: null if cannot evaluate it. | |
946 */ | |
947 | |
948 Expression eval_builtin(BUILTIN builtin, Expressions arguments) | |
949 { | |
950 assert(false); | |
951 } | |
952 | |
953 Expression fromConstInitializer(int result, Expression e1) | |
954 { | |
955 //printf("fromConstInitializer(result = %x, %s)\n", result, e1.toChars()); | |
956 //static int xx; if (xx++ == 10) assert(0); | |
957 Expression e = e1; | |
958 if (e1.op == TOK.TOKvar) | |
959 { | |
960 VarExp ve = cast(VarExp)e1; | |
961 VarDeclaration v = ve.var.isVarDeclaration(); | |
962 e = expandVar(result, v); | |
963 if (e) | |
964 { | |
965 if (e.type != e1.type) | |
966 { | |
967 // Type 'paint' operation | |
968 e = e.copy(); | |
969 e.type = e1.type; | |
970 } | |
971 } | |
972 else | |
973 { | |
974 e = e1; | |
975 } | |
976 } | |
977 return e; | |
978 } | |
979 | |
980 /************************************* | |
981 * If variable has a const initializer, | |
982 * return that initializer. | |
983 */ | |
984 | |
985 Expression expandVar(int result, VarDeclaration v) | |
986 { | |
987 //printf("expandVar(result = %d, v = %p, %s)\n", result, v, v ? v.toChars() : "null"); | |
988 | |
989 Expression e = null; | |
990 if (!v) | |
991 return e; | |
992 | |
993 if (v.isConst() || v.isInvariant() || v.storage_class & STC.STCmanifest) | |
994 { | |
995 if (!v.type) | |
996 { | |
997 //error("ICE"); | |
998 return e; | |
999 } | |
1000 | |
1001 Type tb = v.type.toBasetype(); | |
1002 if (result & WANT.WANTinterpret || v.storage_class & STC.STCmanifest || (tb.ty != TY.Tsarray && tb.ty != TY.Tstruct)) | |
1003 { | |
1004 if (v.init) | |
1005 { | |
1006 if (v.inuse) | |
1007 { | |
1008 if (v.storage_class & STC.STCmanifest) | |
1009 v.error("recursive initialization of constant"); | |
1010 goto L1; | |
1011 } | |
1012 Expression ei = v.init.toExpression(); | |
1013 if (!ei) | |
1014 goto L1; | |
1015 if (ei.op == TOK.TOKconstruct || ei.op == TOK.TOKblit) | |
1016 { | |
1017 AssignExp ae = cast(AssignExp)ei; | |
1018 ei = ae.e2; | |
1019 if (ei.isConst() != 1 && ei.op != TOK.TOKstring) | |
1020 goto L1; | |
1021 if (ei.type != v.type) | |
1022 goto L1; | |
1023 } | |
1024 if (v.scope_) | |
1025 { | |
1026 v.inuse++; | |
1027 e = ei.syntaxCopy(); | |
1028 e = e.semantic(v.scope_); | |
1029 e = e.implicitCastTo(v.scope_, v.type); | |
1030 // enabling this line causes test22 in test suite to fail | |
1031 //ei.type = e.type; | |
1032 v.scope_ = null; | |
1033 v.inuse--; | |
1034 } | |
1035 else if (!ei.type) | |
1036 { | |
1037 goto L1; | |
1038 } | |
1039 else | |
1040 // Should remove the copy() operation by | |
1041 // making all mods to expressions copy-on-write | |
1042 e = ei.copy(); | |
1043 } | |
1044 else | |
1045 { | |
1046 static if (true) { | |
1047 goto L1; | |
1048 } else { | |
1049 // BUG: what if const is initialized in constructor? | |
1050 e = v.type.defaultInit(); | |
1051 e.loc = e1.loc; | |
1052 } | |
1053 } | |
1054 if (e.type != v.type) | |
1055 { | |
1056 e = e.castTo(null, v.type); | |
1057 } | |
1058 v.inuse++; | |
1059 e = e.optimize(result); | |
1060 v.inuse--; | |
1061 } | |
1062 } | |
1063 L1: | |
1064 //if (e) printf("\te = %s, e.type = %s\n", e.toChars(), e.type.toChars()); | |
1065 return e; | |
1066 } | |
1067 | |
1068 /**************************************** | |
1069 * Check access to d for expression e.d | |
1070 */ | |
1071 | |
1072 void accessCheck(Loc loc, Scope sc, Expression e, Declaration d) | |
1073 { | |
1074 version (LOG) { | |
1075 if (e) | |
1076 { | |
1077 printf("accessCheck(%s . %s)\n", e.toChars(), d.toChars()); | |
1078 printf("\te.type = %s\n", e.type.toChars()); | |
1079 } | |
1080 else | |
1081 { | |
1082 //printf("accessCheck(%s)\n", d.toChars()); | |
1083 } | |
1084 } | |
1085 if (!e) | |
1086 { | |
1087 if (d.prot() == PROT.PROTprivate && d.getModule() != sc.module_ || | |
1088 d.prot() == PROT.PROTpackage && !hasPackageAccess(sc, d)) | |
1089 | |
1090 error(loc, "%s %s.%s is not accessible from %s", | |
1091 d.kind(), d.getModule().toChars(), d.toChars(), sc.module_.toChars()); | |
1092 } | |
1093 else if (e.type.ty == TY.Tclass) | |
1094 { | |
1095 // Do access check | |
1096 ClassDeclaration cd; | |
1097 | |
1098 cd = cast(ClassDeclaration)((cast(TypeClass)e.type).sym); | |
1099 static if (true) { | |
1100 if (e.op == TOK.TOKsuper) | |
1101 { | |
1102 ClassDeclaration cd2 = sc.func.toParent().isClassDeclaration(); | |
1103 if (cd2) | |
1104 cd = cd2; | |
1105 } | |
1106 } | |
1107 cd.accessCheck(loc, sc, d); | |
1108 } | |
1109 else if (e.type.ty == TY.Tstruct) | |
1110 { | |
1111 // Do access check | |
1112 StructDeclaration cd = cast(StructDeclaration)((cast(TypeStruct)e.type).sym); | |
1113 cd.accessCheck(loc, sc, d); | |
1114 } | |
1115 } | |
1116 | |
1117 /***************************************** | |
1118 * Given array of arguments and an aggregate type, | |
1119 * if any of the argument types are missing, attempt to infer | |
1120 * them from the aggregate type. | |
1121 */ | |
1122 | |
1123 void inferApplyArgTypes(TOK op, Arguments arguments, Expression aggr) | |
1124 { | |
1125 if (!arguments || !arguments.dim) | |
1126 return; | |
1127 | |
1128 /* Return if no arguments need types. | |
1129 */ | |
1130 for (size_t u = 0; 1; u++) | |
1131 { | |
1132 if (u == arguments.dim) | |
1133 return; | |
1134 | |
1135 Argument arg = cast(Argument)arguments.data[u]; | |
1136 if (!arg.type) | |
1137 break; | |
1138 } | |
1139 | |
1140 AggregateDeclaration ad; | |
1141 | |
1142 Argument arg = cast(Argument)arguments.data[0]; | |
1143 Type taggr = aggr.type; | |
1144 if (!taggr) | |
1145 return; | |
1146 Type tab = taggr.toBasetype(); | |
1147 switch (tab.ty) | |
1148 { | |
1149 case TY.Tarray: | |
1150 case TY.Tsarray: | |
1151 case TY.Ttuple: | |
1152 if (arguments.dim == 2) | |
1153 { | |
1154 if (!arg.type) | |
1155 arg.type = Type.tsize_t; // key type | |
1156 arg = cast(Argument)arguments.data[1]; | |
1157 } | |
1158 if (!arg.type && tab.ty != TY.Ttuple) | |
1159 arg.type = tab.nextOf(); // value type | |
1160 break; | |
1161 | |
1162 case TY.Taarray: | |
1163 { | |
1164 TypeAArray taa = cast(TypeAArray)tab; | |
1165 | |
1166 if (arguments.dim == 2) | |
1167 { | |
1168 if (!arg.type) | |
1169 arg.type = taa.index; // key type | |
1170 arg = cast(Argument)arguments.data[1]; | |
1171 } | |
1172 if (!arg.type) | |
1173 arg.type = taa.next; // value type | |
1174 break; | |
1175 } | |
1176 | |
1177 case TY.Tclass: | |
1178 ad = (cast(TypeClass)tab).sym; | |
1179 goto Laggr; | |
1180 | |
1181 case TY.Tstruct: | |
1182 ad = (cast(TypeStruct)tab).sym; | |
1183 goto Laggr; | |
1184 | |
1185 Laggr: | |
1186 if (arguments.dim == 1) | |
1187 { | |
1188 if (!arg.type) | |
1189 { | |
1190 /* Look for a head() or rear() overload | |
1191 */ | |
1192 Identifier id = (op == TOK.TOKforeach) ? Id.Fhead : Id.Ftoe; | |
1193 Dsymbol s = search_function(ad, id); | |
1194 FuncDeclaration fd = s ? s.isFuncDeclaration() : null; | |
1195 if (!fd) | |
1196 { | |
1197 if (s && s.isTemplateDeclaration()) | |
1198 break; | |
1199 goto Lapply; | |
1200 } | |
1201 arg.type = fd.type.nextOf(); | |
1202 } | |
1203 break; | |
1204 } | |
1205 | |
1206 Lapply: | |
1207 { /* Look for an | |
1208 * int opApply(int delegate(ref Type [, ...]) dg); | |
1209 * overload | |
1210 */ | |
1211 Dsymbol s = search_function(ad, (op == TOK.TOKforeach_reverse) ? Id.applyReverse : Id.apply); | |
1212 if (s) | |
1213 { | |
1214 FuncDeclaration fd = s.isFuncDeclaration(); | |
1215 if (fd) | |
1216 { | |
1217 inferApplyArgTypesX(fd, arguments); | |
1218 break; | |
1219 } | |
1220 static if (false) { | |
1221 TemplateDeclaration td = s.isTemplateDeclaration(); | |
1222 if (td) | |
1223 { | |
1224 inferApplyArgTypesZ(td, arguments); | |
1225 break; | |
1226 } | |
1227 } | |
1228 } | |
1229 break; | |
1230 } | |
1231 | |
1232 case TY.Tdelegate: | |
1233 { | |
1234 if (0 && aggr.op == TOK.TOKdelegate) | |
1235 { | |
1236 DelegateExp de = cast(DelegateExp)aggr; | |
1237 | |
1238 FuncDeclaration fd = de.func.isFuncDeclaration(); | |
1239 if (fd) | |
1240 inferApplyArgTypesX(fd, arguments); | |
1241 } | |
1242 else | |
1243 { | |
1244 inferApplyArgTypesY(cast(TypeFunction)tab.nextOf(), arguments); | |
1245 } | |
1246 break; | |
1247 } | |
1248 | |
1249 default: | |
1250 break; // ignore error, caught later | |
1251 } | |
1252 } | |
1253 | |
1254 struct Param3 | |
1255 { | |
1256 /******************************** | |
1257 * Recursive helper function, | |
1258 * analogous to func.overloadResolveX(). | |
1259 */ | |
1260 | |
1261 int fp3(FuncDeclaration f) | |
1262 { | |
1263 TypeFunction tf = cast(TypeFunction)f.type; | |
1264 if (inferApplyArgTypesY(tf, arguments) == 1) | |
1265 return 0; | |
1266 | |
1267 if (arguments.dim == 0) | |
1268 return 1; | |
1269 | |
1270 return 0; | |
1271 } | |
1272 | |
1273 Arguments arguments; | |
1274 } | |
1275 | |
1276 void inferApplyArgTypesX(FuncDeclaration fstart, Arguments arguments) | |
1277 { | |
1278 Param3 p3; | |
1279 p3.arguments = arguments; | |
1280 overloadApply(fstart, &p3.fp3); | |
1281 } | |
1282 | |
1283 /****************************** | |
1284 * Infer arguments from type of function. | |
1285 * Returns: | |
1286 * 0 match for this function | |
1287 * 1 no match for this function | |
1288 */ | |
1289 | |
1290 int inferApplyArgTypesY(TypeFunction tf, Arguments arguments) | |
1291 { | |
1292 size_t nparams; | |
1293 Argument p; | |
1294 | |
1295 if (Argument.dim(tf.parameters) != 1) | |
1296 goto Lnomatch; | |
1297 | |
1298 p = Argument.getNth(tf.parameters, 0); | |
1299 if (p.type.ty != TY.Tdelegate) | |
1300 goto Lnomatch; | |
1301 | |
1302 tf = cast(TypeFunction)p.type.nextOf(); | |
1303 assert(tf.ty == TY.Tfunction); | |
1304 | |
1305 /* We now have tf, the type of the delegate. Match it against | |
1306 * the arguments, filling in missing argument types. | |
1307 */ | |
1308 nparams = Argument.dim(tf.parameters); | |
1309 if (nparams == 0 || tf.varargs) | |
1310 goto Lnomatch; // not enough parameters | |
1311 if (arguments.dim != nparams) | |
1312 goto Lnomatch; // not enough parameters | |
1313 | |
1314 for (size_t u = 0; u < nparams; u++) | |
1315 { | |
1316 Argument arg = cast(Argument)arguments.data[u]; | |
1317 Argument param = Argument.getNth(tf.parameters, u); | |
1318 if (arg.type) | |
1319 { | |
1320 if (!arg.type.equals(param.type)) | |
1321 { | |
1322 /* Cannot resolve argument types. Indicate an | |
1323 * error by setting the number of arguments to 0. | |
1324 */ | |
1325 arguments.dim = 0; | |
1326 goto Lmatch; | |
1327 } | |
1328 continue; | |
1329 } | |
1330 arg.type = param.type; | |
1331 } | |
1332 | |
1333 Lmatch: | |
1334 return 0; | |
1335 | |
1336 Lnomatch: | |
1337 return 1; | |
1338 } | |
1339 | |
1340 /************************************************** | |
1341 * Write expression out to buf, but wrap it | |
1342 * in ( ) if its precedence is less than pr. | |
1343 */ | |
1344 | |
1345 void expToCBuffer(OutBuffer buf, HdrGenState* hgs, Expression e, PREC pr) | |
1346 { | |
1347 //if (precedence[e.op] == 0) e.dump(0); | |
1348 if (precedence[e.op] < pr || | |
1349 /* Despite precedence, we don't allow a<b<c expressions. | |
1350 * They must be parenthesized. | |
1351 */ | |
1352 (pr == PREC.PREC_rel && precedence[e.op] == pr)) | |
1353 { | |
1354 buf.writeByte('('); | |
1355 e.toCBuffer(buf, hgs); | |
1356 buf.writeByte(')'); | |
1357 } | |
1358 else | |
1359 e.toCBuffer(buf, hgs); | |
1360 } | |
1361 | |
1362 /************************************************** | |
1363 * Write out argument list to buf. | |
1364 */ | |
1365 | |
1366 void argsToCBuffer(OutBuffer buf, Expressions arguments, HdrGenState* hgs) | |
1367 { | |
1368 if (arguments) | |
1369 { | |
1370 for (size_t i = 0; i < arguments.dim; i++) | |
1371 { | |
1372 Expression arg = cast(Expression)arguments.data[i]; | |
1373 | |
1374 if (arg) | |
1375 { | |
1376 if (i) | |
1377 buf.writeByte(','); | |
1378 expToCBuffer(buf, hgs, arg, PREC.PREC_assign); | |
1379 } | |
1380 } | |
1381 } | |
16
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
1382 } |