Mercurial > projects > ddmd
annotate dmd/TypeFunction.d @ 123:9e39c7de8438
Make dmd test suite compile
author | korDen |
---|---|
date | Fri, 03 Sep 2010 20:46:58 +0400 |
parents | e28b18c23469 |
children | 1765f3ef917d |
rev | line source |
---|---|
0 | 1 module dmd.TypeFunction; |
2 | |
114 | 3 import dmd.common; |
0 | 4 import dmd.TypeNext; |
5 import dmd.TypeSArray; | |
6 import dmd.TypeArray; | |
123 | 7 import dmd.TemplateTupleParameter; |
0 | 8 import dmd.ArrayTypes; |
9 import dmd.LINK; | |
10 import dmd.StructDeclaration; | |
11 import dmd.TypeStruct; | |
12 import dmd.Global; | |
13 import dmd.STC; | |
14 import dmd.MOD; | |
73 | 15 import dmd.PROT; |
123 | 16 import dmd.TypeIdentifier; |
17 import dmd.TemplateParameter; | |
18 import dmd.Tuple; | |
0 | 19 import dmd.Type; |
20 import dmd.Loc; | |
21 import dmd.Scope; | |
22 import dmd.Identifier; | |
23 import dmd.OutBuffer; | |
24 import dmd.HdrGenState; | |
25 import dmd.CppMangleState; | |
26 import dmd.TypeInfoDeclaration; | |
27 import dmd.MATCH; | |
28 import dmd.Argument; | |
29 import dmd.Expression; | |
30 import dmd.RET; | |
31 import dmd.TY; | |
32 import dmd.Util; | |
123 | 33 import dmd.TemplateInstance : isTuple; |
0 | 34 |
35 import dmd.backend.TYPE; | |
36 import dmd.backend.PARAM; | |
37 import dmd.backend.Util; | |
38 import dmd.backend.TYM; | |
39 import dmd.backend.TF; | |
40 import dmd.backend.mTY; | |
41 | |
42 import core.stdc.stdlib; | |
43 import core.stdc.string; | |
44 | |
45 class TypeFunction : TypeNext | |
46 { | |
47 // .next is the return type | |
48 | |
49 Arguments parameters; // function parameters | |
50 int varargs; // 1: T t, ...) style for variable number of arguments | |
51 // 2: T t ...) style for variable number of arguments | |
52 bool isnothrow; // true: nothrow | |
53 bool ispure; // true: pure | |
54 bool isproperty; // can be called without parentheses | |
55 bool isref; // true: returns a reference | |
56 LINK linkage; // calling convention | |
57 | |
58 int inuse; | |
59 | |
60 this(Arguments parameters, Type treturn, int varargs, LINK linkage) | |
61 { | |
62 super(TY.Tfunction, treturn); | |
63 | |
64 //if (!treturn) *(char*)0=0; | |
65 // assert(treturn); | |
66 assert(0 <= varargs && varargs <= 2); | |
67 this.parameters = parameters; | |
68 this.varargs = varargs; | |
69 this.linkage = linkage; | |
70 } | |
71 | |
72 | 72 override Type syntaxCopy() |
0 | 73 { |
74 Type treturn = next ? next.syntaxCopy() : null; | |
75 Arguments params = Argument.arraySyntaxCopy(parameters); | |
76 TypeFunction t = new TypeFunction(params, treturn, varargs, linkage); | |
77 t.mod = mod; | |
78 t.isnothrow = isnothrow; | |
79 t.ispure = ispure; | |
80 t.isproperty = isproperty; | |
81 t.isref = isref; | |
82 | |
83 return t; | |
84 } | |
85 | |
86 version (DumbClone) { | |
87 } else { | |
88 final TypeFunction cloneTo(TypeFunction t) | |
89 { | |
90 super.cloneTo(t); | |
91 | |
92 // these 3 should be set by ctor | |
93 assert(t.parameters is null); | |
94 assert(t.varargs == varargs); | |
95 assert(t.linkage == linkage); | |
96 | |
97 t.isnothrow = isnothrow; | |
98 t.ispure = ispure; | |
99 t.isproperty = isproperty; | |
100 t.isref = isref; | |
101 t.inuse = inuse; | |
102 | |
103 if (parameters) | |
104 { | |
105 t.parameters = parameters.copy(); | |
106 for (size_t i = 0; i < parameters.dim; i++) | |
107 { | |
108 Argument arg = cast(Argument)parameters.data[i]; | |
109 Argument cpy = arg.clone(); | |
110 t.parameters.data[i] = cast(void*)cpy; | |
111 } | |
112 } | |
113 | |
114 return t; | |
115 } | |
116 | |
117 TypeFunction clone() | |
118 { | |
119 assert(this.classinfo == TypeFunction.classinfo); | |
120 return cloneTo(new TypeFunction(null, next, varargs, linkage)); | |
121 } | |
122 } | |
72 | 123 override Type semantic(Loc loc, Scope sc) |
0 | 124 { |
125 if (deco) // if semantic() already run | |
126 { | |
127 //printf("already done\n"); | |
128 return this; | |
129 } | |
130 //printf("TypeFunction.semantic() this = %p\n", this); | |
131 //printf("TypeFunction.semantic() %s, sc.stc = %x\n", toChars(), sc.stc); | |
132 | |
133 /* Copy in order to not mess up original. | |
134 * This can produce redundant copies if inferring return type, | |
135 * as semantic() will get called again on this. | |
136 */ | |
137 | |
138 TypeFunction tf = cast(TypeFunction)clone(); | |
139 | |
140 if (sc.stc & STC.STCpure) | |
141 tf.ispure = true; | |
142 if (sc.stc & STC.STCnothrow) | |
143 tf.isnothrow = true; | |
144 if (sc.stc & STC.STCref) | |
145 tf.isref = true; | |
146 | |
147 tf.linkage = sc.linkage; | |
148 if (tf.next) | |
149 { | |
150 tf.next = tf.next.semantic(loc,sc); | |
96 | 151 version(SARRAYVALUE) {} else |
152 { | |
0 | 153 if (tf.next.toBasetype().ty == TY.Tsarray) |
154 { error(loc, "functions cannot return static array %s", tf.next.toChars()); | |
155 tf.next = Type.terror; | |
156 } | |
96 | 157 } |
0 | 158 if (tf.next.toBasetype().ty == TY.Tfunction) |
159 { error(loc, "functions cannot return a function"); | |
160 tf.next = Type.terror; | |
161 } | |
162 if (tf.next.toBasetype().ty == TY.Ttuple) | |
163 { error(loc, "functions cannot return a tuple"); | |
164 tf.next = Type.terror; | |
165 } | |
166 if (tf.next.isauto() && !(sc.flags & SCOPE.SCOPEctor)) | |
167 error(loc, "functions cannot return scope %s", tf.next.toChars()); | |
168 } | |
169 | |
170 if (tf.parameters) | |
73 | 171 { |
172 /* Create a scope for evaluating the default arguments for the parameters | |
173 */ | |
174 Scope argsc = sc.push(); | |
175 argsc.stc = STCundefined; // don't inherit storage class | |
176 argsc.protection = PROT.PROTpublic; | |
177 | |
178 size_t dim = Argument.dim(tf.parameters); | |
0 | 179 |
180 for (size_t i = 0; i < dim; i++) | |
181 { Argument arg = Argument.getNth(tf.parameters, i); | |
182 | |
183 tf.inuse++; | |
73 | 184 arg.type = arg.type.semantic(loc, argsc); |
0 | 185 if (tf.inuse == 1) tf.inuse--; |
186 | |
187 arg.type = arg.type.addStorageClass(arg.storageClass); | |
188 | |
189 if (arg.storageClass & (STC.STCauto | STC.STCalias | STC.STCstatic)) | |
190 { | |
191 if (!arg.type) | |
192 continue; | |
193 } | |
194 | |
195 Type t = arg.type.toBasetype(); | |
196 | |
197 if (arg.storageClass & (STC.STCout | STC.STCref | STC.STClazy)) | |
198 { | |
96 | 199 //if (t.ty == TY.Tsarray) |
200 //error(loc, "cannot have out or ref parameter of type %s", t.toChars()); | |
201 if (arg.storageClass & STC.STCout && arg.type.mod & (STCconst | STCimmutable)) | |
202 error(loc, "cannot have const or immutabl out parameter of type %s", t.toChars()); | |
0 | 203 } |
204 if (!(arg.storageClass & STC.STClazy) && t.ty == TY.Tvoid) | |
205 error(loc, "cannot have parameter of type %s", arg.type.toChars()); | |
206 | |
207 if (arg.defaultArg) | |
208 { | |
73 | 209 arg.defaultArg = arg.defaultArg.semantic(argsc); |
210 arg.defaultArg = resolveProperties(argsc, arg.defaultArg); | |
211 arg.defaultArg = arg.defaultArg.implicitCastTo(argsc, arg.type); | |
0 | 212 } |
213 | |
214 /* If arg turns out to be a tuple, the number of parameters may | |
215 * change. | |
216 */ | |
217 if (t.ty == TY.Ttuple) | |
218 { dim = Argument.dim(tf.parameters); | |
219 i--; | |
220 } | |
221 } | |
73 | 222 argsc.pop(); |
0 | 223 } |
224 if (tf.next) | |
225 tf.deco = tf.merge().deco; | |
226 | |
227 if (tf.inuse) | |
228 { error(loc, "recursive type"); | |
229 tf.inuse = 0; | |
230 return terror; | |
231 } | |
232 | |
233 if (tf.varargs == 1 && tf.linkage != LINK.LINKd && Argument.dim(tf.parameters) == 0) | |
234 error(loc, "variadic functions with non-D linkage must have at least one parameter"); | |
235 | |
236 /* Don't return merge(), because arg identifiers and default args | |
237 * can be different | |
238 * even though the types match | |
239 */ | |
240 return tf; | |
241 } | |
242 | |
72 | 243 override void toDecoBuffer(OutBuffer buf, int flag) |
0 | 244 { |
245 ubyte mc; | |
246 | |
247 //printf("TypeFunction.toDecoBuffer() this = %p %s\n", this, toChars()); | |
248 //static int nest; if (++nest == 50) *(char*)0=0; | |
249 if (inuse) | |
250 { | |
251 inuse = 2; // flag error to caller | |
252 return; | |
253 } | |
254 inuse++; | |
255 static if (true) { | |
256 if (mod & MOD.MODshared) | |
257 buf.writeByte('O'); | |
258 if (mod & MOD.MODconst) | |
259 buf.writeByte('x'); | |
260 else if (mod & MOD.MODinvariant) | |
261 buf.writeByte('y'); | |
262 } | |
263 switch (linkage) | |
264 { | |
265 case LINK.LINKd: mc = 'F'; break; | |
266 case LINK.LINKc: mc = 'U'; break; | |
267 case LINK.LINKwindows: mc = 'W'; break; | |
268 case LINK.LINKpascal: mc = 'V'; break; | |
269 case LINK.LINKcpp: mc = 'R'; break; | |
270 } | |
271 buf.writeByte(mc); | |
272 if (ispure || isnothrow || isproperty || isref) | |
273 { | |
274 if (ispure) | |
275 buf.writestring("Na"); | |
276 if (isnothrow) | |
277 buf.writestring("Nb"); | |
278 if (isref) | |
279 buf.writestring("Nc"); | |
280 if (isproperty) | |
281 buf.writestring("Nd"); | |
282 } | |
283 // Write argument types | |
284 Argument.argsToDecoBuffer(buf, parameters); | |
285 //if (buf.data[buf.offset - 1] == '@') halt(); | |
286 buf.writeByte('Z' - varargs); // mark end of arg list | |
79 | 287 assert(next); |
0 | 288 next.toDecoBuffer(buf); |
289 inuse--; | |
290 } | |
291 | |
72 | 292 override void toCBuffer(OutBuffer buf, Identifier ident, HdrGenState* hgs) |
0 | 293 { |
294 //printf("TypeFunction.toCBuffer() this = %p\n", this); | |
295 string p = null; | |
296 | |
297 if (inuse) | |
298 { | |
299 inuse = 2; // flag error to caller | |
300 return; | |
301 } | |
302 inuse++; | |
303 | |
304 /* Use 'storage class' style for attributes | |
305 */ | |
306 if (mod & MODconst) | |
307 buf.writestring("const "); | |
308 if (mod & MODinvariant) | |
309 buf.writestring("immutable "); | |
310 if (mod & MODshared) | |
311 buf.writestring("shared "); | |
312 | |
313 if (ispure) | |
314 buf.writestring("pure "); | |
315 if (isnothrow) | |
316 buf.writestring("nothrow "); | |
317 if (isproperty) | |
318 buf.writestring("@property "); | |
319 if (isref) | |
320 buf.writestring("ref "); | |
321 | |
322 if (next && (!ident || ident.toHChars2() == ident.toChars())) | |
323 next.toCBuffer2(buf, hgs, MODundefined); | |
324 if (hgs.ddoc != 1) | |
325 { | |
326 switch (linkage) | |
327 { | |
328 case LINKd: p = null; break; | |
73 | 329 case LINKc: p = " C"; break; |
330 case LINKwindows: p = " Windows"; break; | |
331 case LINKpascal: p = " Pascal"; break; | |
332 case LINKcpp: p = " C++"; break; | |
0 | 333 default: |
334 assert(0); | |
335 } | |
336 } | |
337 | |
338 if (!hgs.hdrgen && p) | |
339 buf.writestring(p); | |
340 if (ident) | |
341 { | |
342 buf.writeByte(' '); | |
343 buf.writestring(ident.toHChars2()); | |
344 } | |
345 Argument.argsToCBuffer(buf, hgs, parameters, varargs); | |
346 inuse--; | |
347 } | |
348 | |
72 | 349 override void toCBuffer2(OutBuffer buf, HdrGenState* hgs, MOD mod) |
0 | 350 { |
351 //printf("TypeFunction::toCBuffer2() this = %p, ref = %d\n", this, isref); | |
352 string p; | |
353 | |
354 if (inuse) | |
355 { | |
356 inuse = 2; // flag error to caller | |
357 return; | |
358 } | |
359 | |
360 inuse++; | |
361 if (next) | |
362 next.toCBuffer2(buf, hgs, MODundefined); | |
363 | |
364 if (hgs.ddoc != 1) | |
365 { | |
366 switch (linkage) | |
367 { | |
368 case LINKd: p = null; break; | |
369 case LINKc: p = "C "; break; | |
370 case LINKwindows: p = "Windows "; break; | |
371 case LINKpascal: p = "Pascal "; break; | |
372 case LINKcpp: p = "C++ "; break; | |
373 default: assert(0); | |
374 } | |
375 } | |
376 | |
377 if (!hgs.hdrgen && p) | |
378 buf.writestring(p); | |
379 buf.writestring(" function"); | |
380 Argument.argsToCBuffer(buf, hgs, parameters, varargs); | |
381 | |
382 /* Use postfix style for attributes | |
383 */ | |
384 if (mod != this.mod) | |
385 { | |
386 modToBuffer(buf); | |
387 } | |
388 | |
389 if (ispure) | |
390 buf.writestring(" pure"); | |
391 if (isnothrow) | |
392 buf.writestring(" nothrow"); | |
393 if (isproperty) | |
394 buf.writestring(" @property"); | |
395 if (isref) | |
396 buf.writestring(" ref"); | |
397 | |
398 inuse--; | |
399 } | |
400 | |
72 | 401 override MATCH deduceType(Scope sc, Type tparam, TemplateParameters parameters, Objects dedtypes) |
0 | 402 { |
123 | 403 //printf("TypeFunction.deduceType()\n"); |
404 //printf("\tthis = %d, ", ty); print(); | |
405 //printf("\ttparam = %d, ", tparam.ty); tparam.print(); | |
406 | |
407 // Extra check that function characteristics must match | |
408 if (tparam && tparam.ty == Tfunction) | |
409 { | |
410 TypeFunction tp = cast(TypeFunction)tparam; | |
411 if (varargs != tp.varargs || | |
412 linkage != tp.linkage) | |
413 return MATCHnomatch; | |
414 | |
415 size_t nfargs = Argument.dim(this.parameters); | |
416 size_t nfparams = Argument.dim(tp.parameters); | |
417 | |
418 /* See if tuple match | |
419 */ | |
420 if (nfparams > 0 && nfargs >= nfparams - 1) | |
421 { | |
422 /* See if 'A' of the template parameter matches 'A' | |
423 * of the type of the last function parameter. | |
424 */ | |
425 Argument fparam = Argument.getNth(tp.parameters, nfparams - 1); | |
426 assert(fparam); | |
427 assert(fparam.type); | |
428 if (fparam.type.ty != Tident) | |
429 goto L1; | |
430 TypeIdentifier tid = cast(TypeIdentifier)fparam.type; | |
431 if (tid.idents.dim) | |
432 goto L1; | |
433 | |
434 /* Look through parameters to find tuple matching tid.ident | |
435 */ | |
436 size_t tupi = 0; | |
437 for (; 1; tupi++) | |
438 { | |
439 if (tupi == parameters.dim) | |
440 goto L1; | |
441 TemplateParameter t = parameters[tupi]; | |
442 TemplateTupleParameter tup = t.isTemplateTupleParameter(); | |
443 if (tup && tup.ident.equals(tid.ident)) | |
444 break; | |
445 } | |
446 | |
447 /* The types of the function arguments [nfparams - 1 .. nfargs] | |
448 * now form the tuple argument. | |
449 */ | |
450 int tuple_dim = nfargs - (nfparams - 1); | |
451 | |
452 /* See if existing tuple, and whether it matches or not | |
453 */ | |
454 Object o = dedtypes[tupi]; | |
455 if (o) | |
456 { | |
457 // Existing deduced argument must be a tuple, and must match | |
458 Tuple t = isTuple(o); | |
459 if (!t || t.objects.dim != tuple_dim) | |
460 return MATCHnomatch; | |
461 for (size_t i = 0; i < tuple_dim; i++) | |
462 { | |
463 Argument arg = Argument.getNth(this.parameters, nfparams - 1 + i); | |
464 if (!arg.type.equals(t.objects[i])) | |
465 return MATCHnomatch; | |
466 } | |
467 } | |
468 else | |
469 { // Create new tuple | |
470 Tuple t = new Tuple(); | |
471 t.objects.setDim(tuple_dim); | |
472 for (size_t i = 0; i < tuple_dim; i++) | |
473 { | |
474 Argument arg = Argument.getNth(this.parameters, nfparams - 1 + i); | |
475 t.objects[i] = arg.type; | |
476 } | |
477 dedtypes[tupi] = t; | |
478 } | |
479 nfparams--; // don't consider the last parameter for type deduction | |
480 goto L2; | |
481 } | |
482 | |
483 L1: | |
484 if (nfargs != nfparams) | |
485 return MATCHnomatch; | |
486 L2: | |
487 for (size_t i = 0; i < nfparams; i++) | |
488 { | |
489 Argument a = Argument.getNth(this.parameters, i); | |
490 Argument ap = Argument.getNth(tp.parameters, i); | |
491 if (a.storageClass != ap.storageClass || | |
492 !a.type.deduceType(sc, ap.type, parameters, dedtypes)) | |
493 return MATCHnomatch; | |
494 } | |
495 } | |
496 return Type.deduceType(sc, tparam, parameters, dedtypes); | |
0 | 497 } |
498 | |
72 | 499 override TypeInfoDeclaration getTypeInfoDeclaration() |
0 | 500 { |
501 assert(false); | |
502 } | |
503 | |
72 | 504 override Type reliesOnTident() |
0 | 505 { |
506 if (parameters) | |
507 { | |
508 for (size_t i = 0; i < parameters.dim; i++) | |
509 { | |
510 Argument arg = cast(Argument)parameters.data[i]; | |
511 Type t = arg.type.reliesOnTident(); | |
512 if (t) | |
513 return t; | |
514 } | |
515 } | |
516 return next.reliesOnTident(); | |
517 } | |
518 | |
519 version (CPP_MANGLE) { | |
520 void toCppMangle(OutBuffer buf, CppMangleState* cms) | |
521 { | |
522 assert(false); | |
523 } | |
524 } | |
525 | |
526 /*************************** | |
527 * Examine function signature for parameter p and see if | |
528 * p can 'escape' the scope of the function. | |
529 */ | |
530 bool parameterEscapes(Argument p) | |
531 { | |
532 /* Scope parameters do not escape. | |
533 * Allow 'lazy' to imply 'scope' - | |
534 * lazy parameters can be passed along | |
535 * as lazy parameters to the next function, but that isn't | |
536 * escaping. | |
537 */ | |
538 if (p.storageClass & (STC.STCscope | STC.STClazy)) | |
539 return false; | |
540 | |
541 if (ispure) | |
542 { /* With pure functions, we need only be concerned if p escapes | |
543 * via any return statement. | |
544 */ | |
545 Type tret = nextOf().toBasetype(); | |
546 if (!isref && !tret.hasPointers()) | |
547 { /* The result has no references, so p could not be escaping | |
548 * that way. | |
549 */ | |
550 return false; | |
551 } | |
552 } | |
553 | |
554 /* Assume it escapes in the absence of better information. | |
555 */ | |
556 return true; | |
557 } | |
558 | |
559 /******************************** | |
560 * 'args' are being matched to function 'this' | |
561 * Determine match level. | |
562 * Returns: | |
563 * MATCHxxxx | |
564 */ | |
565 MATCH callMatch(Expression ethis, Expressions args) | |
566 { | |
567 //printf("TypeFunction.callMatch() %s\n", toChars()); | |
568 MATCH match = MATCH.MATCHexact; // assume exact match | |
569 | |
570 if (ethis) | |
571 { | |
572 Type t = ethis.type; | |
573 if (t.toBasetype().ty == TY.Tpointer) | |
574 t = t.toBasetype().nextOf(); // change struct* to struct | |
575 | |
576 if (t.mod != mod) | |
577 { | |
578 if (mod == MOD.MODconst) | |
579 match = MATCH.MATCHconst; | |
580 else | |
581 return MATCH.MATCHnomatch; | |
582 } | |
583 } | |
584 | |
585 size_t nparams = Argument.dim(parameters); | |
586 size_t nargs = args ? args.dim : 0; | |
587 if (nparams == nargs) { | |
588 ; | |
589 } else if (nargs > nparams) | |
590 { | |
591 if (varargs == 0) | |
592 goto Nomatch; // too many args; no match | |
593 match = MATCH.MATCHconvert; // match ... with a "conversion" match level | |
594 } | |
595 | |
596 for (size_t u = 0; u < nparams; u++) | |
597 { | |
598 MATCH m; | |
599 Expression arg; | |
600 | |
601 // BUG: what about out and ref? | |
602 | |
603 Argument p = Argument.getNth(parameters, u); | |
604 assert(p); | |
605 if (u >= nargs) | |
606 { | |
607 if (p.defaultArg) | |
608 continue; | |
609 if (varargs == 2 && u + 1 == nparams) | |
610 goto L1; | |
611 goto Nomatch; // not enough arguments | |
612 } | |
613 | |
113
3482c73a991b
More cleanup for arrays
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
96
diff
changeset
|
614 arg = cast(Expression)args[u]; |
0 | 615 assert(arg); |
96 | 616 // writef("arg: %s, type: %s\n", arg.toChars(), arg.type.toChars()); |
617 | |
0 | 618 |
619 // Non-lvalues do not match ref or out parameters | |
96 | 620 if (p.storageClass & (STC.STCref | STC.STCout)) |
621 { | |
622 if (!arg.isLvalue()) | |
623 goto Nomatch; | |
624 } | |
625 | |
626 if (p.storageClass & STCref) | |
627 { | |
628 /* Don't allow static arrays to be passed to mutable refereces | |
629 * to static arrays if the argument cannot be modified. | |
630 */ | |
631 Type targb = arg.type.toBasetype(); | |
632 Type tparb = p.type.toBasetype(); | |
633 //writef("%s\n", targb.toChars()); | |
634 //writef("%s\n", tparb.toChars()); | |
635 if (targb.nextOf() && tparb.ty == Tsarray && | |
636 targb.nextOf().mod != tparb.nextOf().mod && | |
637 !tparb.nextOf().isConst()) | |
638 goto Nomatch; | |
639 } | |
0 | 640 |
641 if (p.storageClass & STC.STClazy && p.type.ty == TY.Tvoid && arg.type.ty != TY.Tvoid) | |
642 m = MATCH.MATCHconvert; | |
643 else | |
644 m = arg.implicitConvTo(p.type); | |
645 //printf("\tm = %d\n", m); | |
646 if (m == MATCH.MATCHnomatch) // if no match | |
647 { | |
648 L1: | |
649 if (varargs == 2 && u + 1 == nparams) // if last varargs param | |
650 { | |
651 Type tb = p.type.toBasetype(); | |
652 TypeSArray tsa; | |
653 long sz; | |
654 | |
655 switch (tb.ty) | |
656 { | |
657 case TY.Tsarray: | |
658 tsa = cast(TypeSArray)tb; | |
659 sz = tsa.dim.toInteger(); | |
660 if (sz != nargs - u) | |
661 goto Nomatch; | |
662 case TY.Tarray: | |
663 { | |
664 TypeArray ta = cast(TypeArray)tb; | |
665 for (; u < nargs; u++) | |
666 { | |
113
3482c73a991b
More cleanup for arrays
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
96
diff
changeset
|
667 arg = cast(Expression)args[u]; |
0 | 668 assert(arg); |
669 static if (true) { | |
670 /* If lazy array of delegates, | |
671 * convert arg(s) to delegate(s) | |
672 */ | |
673 Type tret = p.isLazyArray(); | |
674 if (tret) | |
675 { | |
676 if (ta.next.equals(arg.type)) | |
677 { | |
678 m = MATCH.MATCHexact; | |
679 } | |
680 else | |
681 { | |
682 m = arg.implicitConvTo(tret); | |
683 if (m == MATCH.MATCHnomatch) | |
684 { | |
685 if (tret.toBasetype().ty == TY.Tvoid) | |
686 m = MATCH.MATCHconvert; | |
687 } | |
688 } | |
689 } | |
690 else | |
691 m = arg.implicitConvTo(ta.next); | |
692 } else { | |
693 m = arg.implicitConvTo(ta.next); | |
694 } | |
695 if (m == MATCH.MATCHnomatch) | |
696 goto Nomatch; | |
697 | |
698 if (m < match) | |
699 match = m; | |
700 } | |
701 goto Ldone; | |
702 } | |
703 | |
704 case TY.Tclass: | |
705 // Should see if there's a constructor match? | |
706 // Or just leave it ambiguous? | |
707 goto Ldone; | |
708 | |
709 default: | |
710 goto Nomatch; | |
711 } | |
712 } | |
713 | |
714 goto Nomatch; | |
715 } | |
716 | |
717 if (m < match) | |
718 match = m; // pick worst match | |
719 } | |
720 | |
721 Ldone: | |
722 //printf("match = %d\n", match); | |
723 return match; | |
724 | |
725 Nomatch: | |
726 //printf("no match\n"); | |
727 return MATCH.MATCHnomatch; | |
728 } | |
729 | |
96 | 730 override type* toCtype() |
0 | 731 { |
732 if (ctype) { | |
733 return ctype; | |
734 } | |
735 | |
736 type* t; | |
737 if (true) | |
738 { | |
739 param_t* paramtypes; | |
740 tym_t tyf; | |
741 type* tp; | |
742 | |
743 paramtypes = null; | |
744 size_t nparams = Argument.dim(parameters); | |
745 for (size_t i = 0; i < nparams; i++) | |
746 { | |
747 Argument arg = Argument.getNth(parameters, i); | |
748 tp = arg.type.toCtype(); | |
749 if (arg.storageClass & (STC.STCout | STC.STCref)) | |
750 { | |
751 // C doesn't have reference types, so it's really a pointer | |
752 // to the parameter type | |
753 tp = type_allocn(TYM.TYref, tp); | |
754 } | |
755 param_append_type(¶mtypes,tp); | |
756 } | |
757 tyf = totym(); | |
758 t = type_alloc(tyf); | |
759 t.Tflags |= TF.TFprototype; | |
760 if (varargs != 1) | |
761 t.Tflags |= TF.TFfixed; | |
762 ctype = t; | |
763 t.Tnext = next.toCtype(); | |
764 t.Tnext.Tcount++; | |
765 t.Tparamtypes = paramtypes; | |
766 } | |
767 ctype = t; | |
768 return t; | |
769 } | |
770 | |
771 /*************************** | |
772 * Determine return style of function - whether in registers or | |
773 * through a hidden pointer to the caller's stack. | |
774 */ | |
96 | 775 RET retStyle() |
0 | 776 { |
777 //printf("TypeFunction.retStyle() %s\n", toChars()); | |
95 | 778 version (DMDV2) |
779 { | |
0 | 780 if (isref) |
781 return RET.RETregs; // returns a pointer | |
782 } | |
783 | |
784 Type tn = next.toBasetype(); | |
95 | 785 Type tns = tn; |
786 ulong sz = tn.size(); | |
0 | 787 |
95 | 788 version(SARRAYVALUE) |
789 { | |
790 if (tn.ty == Tsarray) | |
791 { | |
792 do | |
793 { | |
794 tns = tns.nextOf().toBasetype(); | |
795 } while (tns.ty == Tsarray); | |
796 if (tns.ty != Tstruct) | |
797 { | |
798 if (global.params.isLinux && linkage != LINKd) | |
799 {} | |
800 else | |
801 { | |
802 switch (sz) | |
803 { case 1: | |
804 case 2: | |
805 case 4: | |
806 case 8: | |
96 | 807 return RET.RETregs; // return small structs in regs |
808 // (not 3 byte structs!) | |
95 | 809 default: |
810 break; | |
811 } | |
812 } | |
96 | 813 return RET.RETstack; |
95 | 814 } |
815 } | |
816 } | |
817 if (tns.ty == TY.Tstruct) | |
0 | 818 { |
819 StructDeclaration sd = (cast(TypeStruct)tn).sym; | |
820 if (global.params.isLinux && linkage != LINK.LINKd) { | |
821 ; | |
822 } | |
95 | 823 ///version (DMDV2) { // TODO: |
824 else if (sd.dtor || sd.cpctor) | |
825 { | |
0 | 826 } |
827 ///} | |
828 else | |
829 { | |
95 | 830 switch (sz) |
0 | 831 { |
832 case 1: | |
833 case 2: | |
834 case 4: | |
835 case 8: | |
836 return RET.RETregs; // return small structs in regs | |
837 // (not 3 byte structs!) | |
838 default: | |
839 break; | |
840 } | |
841 } | |
842 return RET.RETstack; | |
843 } | |
844 else if ((global.params.isLinux || global.params.isOSX || global.params.isFreeBSD || global.params.isSolaris) && | |
845 linkage == LINK.LINKc && | |
846 tn.iscomplex()) | |
847 { | |
848 if (tn.ty == TY.Tcomplex32) | |
849 return RET.RETregs; // in EDX:EAX, not ST1:ST0 | |
850 else | |
851 return RET.RETstack; | |
852 } | |
853 else | |
854 return RET.RETregs; | |
855 } | |
856 | |
72 | 857 override TYM totym() |
0 | 858 { |
859 TYM tyf; | |
860 | |
861 //printf("TypeFunction.totym(), linkage = %d\n", linkage); | |
862 switch (linkage) | |
863 { | |
864 case LINK.LINKwindows: | |
865 tyf = (varargs == 1) ? TYM.TYnfunc : TYM.TYnsfunc; | |
866 break; | |
867 | |
868 case LINK.LINKpascal: | |
869 tyf = (varargs == 1) ? TYM.TYnfunc : TYM.TYnpfunc; | |
870 break; | |
871 | |
872 case LINK.LINKc: | |
873 tyf = TYM.TYnfunc; | |
22
fd4acc376c45
Implemented object file output and linking on linux.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
874 version (POSIX) {///TARGET_LINUX || TARGET_OSX || TARGET_FREEBSD || TARGET_SOLARIS |
0 | 875 if (retStyle() == RET.RETstack) |
876 tyf = TYM.TYhfunc; | |
877 } | |
878 break; | |
879 | |
880 case LINK.LINKd: | |
881 tyf = (varargs == 1) ? TYM.TYnfunc : TYM.TYjfunc; | |
882 break; | |
883 | |
884 case LINK.LINKcpp: | |
885 tyf = TYM.TYnfunc; | |
886 break; | |
887 | |
888 default: | |
889 writef("linkage = %d\n", linkage); | |
890 assert(0); | |
891 } | |
892 version (DMDV2) { | |
893 if (isnothrow) | |
894 tyf |= mTY.mTYnothrow; | |
895 } | |
896 return tyf; | |
897 } | |
22
fd4acc376c45
Implemented object file output and linking on linux.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
898 } |