Mercurial > projects > ddmd
annotate dmd/TypeFunction.d @ 126:1765f3ef917d
ClassDeclarations, Arguments -> Vector
author | Eldar Insafutdinov <e.insafutdinov@gmail.com> |
---|---|
date | Fri, 03 Sep 2010 23:25:55 +0100 |
parents | 9e39c7de8438 |
children | 010eb8f0e18d |
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 { | |
126
1765f3ef917d
ClassDeclarations, Arguments -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
123
diff
changeset
|
508 foreach (arg; parameters) |
0 | 509 { |
510 Type t = arg.type.reliesOnTident(); | |
511 if (t) | |
512 return t; | |
513 } | |
514 } | |
515 return next.reliesOnTident(); | |
516 } | |
517 | |
518 version (CPP_MANGLE) { | |
519 void toCppMangle(OutBuffer buf, CppMangleState* cms) | |
520 { | |
521 assert(false); | |
522 } | |
523 } | |
524 | |
525 /*************************** | |
526 * Examine function signature for parameter p and see if | |
527 * p can 'escape' the scope of the function. | |
528 */ | |
529 bool parameterEscapes(Argument p) | |
530 { | |
531 /* Scope parameters do not escape. | |
532 * Allow 'lazy' to imply 'scope' - | |
533 * lazy parameters can be passed along | |
534 * as lazy parameters to the next function, but that isn't | |
535 * escaping. | |
536 */ | |
537 if (p.storageClass & (STC.STCscope | STC.STClazy)) | |
538 return false; | |
539 | |
540 if (ispure) | |
541 { /* With pure functions, we need only be concerned if p escapes | |
542 * via any return statement. | |
543 */ | |
544 Type tret = nextOf().toBasetype(); | |
545 if (!isref && !tret.hasPointers()) | |
546 { /* The result has no references, so p could not be escaping | |
547 * that way. | |
548 */ | |
549 return false; | |
550 } | |
551 } | |
552 | |
553 /* Assume it escapes in the absence of better information. | |
554 */ | |
555 return true; | |
556 } | |
557 | |
558 /******************************** | |
559 * 'args' are being matched to function 'this' | |
560 * Determine match level. | |
561 * Returns: | |
562 * MATCHxxxx | |
563 */ | |
564 MATCH callMatch(Expression ethis, Expressions args) | |
565 { | |
566 //printf("TypeFunction.callMatch() %s\n", toChars()); | |
567 MATCH match = MATCH.MATCHexact; // assume exact match | |
568 | |
569 if (ethis) | |
570 { | |
571 Type t = ethis.type; | |
572 if (t.toBasetype().ty == TY.Tpointer) | |
573 t = t.toBasetype().nextOf(); // change struct* to struct | |
574 | |
575 if (t.mod != mod) | |
576 { | |
577 if (mod == MOD.MODconst) | |
578 match = MATCH.MATCHconst; | |
579 else | |
580 return MATCH.MATCHnomatch; | |
581 } | |
582 } | |
583 | |
584 size_t nparams = Argument.dim(parameters); | |
585 size_t nargs = args ? args.dim : 0; | |
586 if (nparams == nargs) { | |
587 ; | |
588 } else if (nargs > nparams) | |
589 { | |
590 if (varargs == 0) | |
591 goto Nomatch; // too many args; no match | |
592 match = MATCH.MATCHconvert; // match ... with a "conversion" match level | |
593 } | |
594 | |
595 for (size_t u = 0; u < nparams; u++) | |
596 { | |
597 MATCH m; | |
598 Expression arg; | |
599 | |
600 // BUG: what about out and ref? | |
601 | |
602 Argument p = Argument.getNth(parameters, u); | |
603 assert(p); | |
604 if (u >= nargs) | |
605 { | |
606 if (p.defaultArg) | |
607 continue; | |
608 if (varargs == 2 && u + 1 == nparams) | |
609 goto L1; | |
610 goto Nomatch; // not enough arguments | |
611 } | |
612 | |
113
3482c73a991b
More cleanup for arrays
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
96
diff
changeset
|
613 arg = cast(Expression)args[u]; |
0 | 614 assert(arg); |
96 | 615 // writef("arg: %s, type: %s\n", arg.toChars(), arg.type.toChars()); |
616 | |
0 | 617 |
618 // Non-lvalues do not match ref or out parameters | |
96 | 619 if (p.storageClass & (STC.STCref | STC.STCout)) |
620 { | |
621 if (!arg.isLvalue()) | |
622 goto Nomatch; | |
623 } | |
624 | |
625 if (p.storageClass & STCref) | |
626 { | |
627 /* Don't allow static arrays to be passed to mutable refereces | |
628 * to static arrays if the argument cannot be modified. | |
629 */ | |
630 Type targb = arg.type.toBasetype(); | |
631 Type tparb = p.type.toBasetype(); | |
632 //writef("%s\n", targb.toChars()); | |
633 //writef("%s\n", tparb.toChars()); | |
634 if (targb.nextOf() && tparb.ty == Tsarray && | |
635 targb.nextOf().mod != tparb.nextOf().mod && | |
636 !tparb.nextOf().isConst()) | |
637 goto Nomatch; | |
638 } | |
0 | 639 |
640 if (p.storageClass & STC.STClazy && p.type.ty == TY.Tvoid && arg.type.ty != TY.Tvoid) | |
641 m = MATCH.MATCHconvert; | |
642 else | |
643 m = arg.implicitConvTo(p.type); | |
644 //printf("\tm = %d\n", m); | |
645 if (m == MATCH.MATCHnomatch) // if no match | |
646 { | |
647 L1: | |
648 if (varargs == 2 && u + 1 == nparams) // if last varargs param | |
649 { | |
650 Type tb = p.type.toBasetype(); | |
651 TypeSArray tsa; | |
652 long sz; | |
653 | |
654 switch (tb.ty) | |
655 { | |
656 case TY.Tsarray: | |
657 tsa = cast(TypeSArray)tb; | |
658 sz = tsa.dim.toInteger(); | |
659 if (sz != nargs - u) | |
660 goto Nomatch; | |
661 case TY.Tarray: | |
662 { | |
663 TypeArray ta = cast(TypeArray)tb; | |
664 for (; u < nargs; u++) | |
665 { | |
113
3482c73a991b
More cleanup for arrays
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
96
diff
changeset
|
666 arg = cast(Expression)args[u]; |
0 | 667 assert(arg); |
668 static if (true) { | |
669 /* If lazy array of delegates, | |
670 * convert arg(s) to delegate(s) | |
671 */ | |
672 Type tret = p.isLazyArray(); | |
673 if (tret) | |
674 { | |
675 if (ta.next.equals(arg.type)) | |
676 { | |
677 m = MATCH.MATCHexact; | |
678 } | |
679 else | |
680 { | |
681 m = arg.implicitConvTo(tret); | |
682 if (m == MATCH.MATCHnomatch) | |
683 { | |
684 if (tret.toBasetype().ty == TY.Tvoid) | |
685 m = MATCH.MATCHconvert; | |
686 } | |
687 } | |
688 } | |
689 else | |
690 m = arg.implicitConvTo(ta.next); | |
691 } else { | |
692 m = arg.implicitConvTo(ta.next); | |
693 } | |
694 if (m == MATCH.MATCHnomatch) | |
695 goto Nomatch; | |
696 | |
697 if (m < match) | |
698 match = m; | |
699 } | |
700 goto Ldone; | |
701 } | |
702 | |
703 case TY.Tclass: | |
704 // Should see if there's a constructor match? | |
705 // Or just leave it ambiguous? | |
706 goto Ldone; | |
707 | |
708 default: | |
709 goto Nomatch; | |
710 } | |
711 } | |
712 | |
713 goto Nomatch; | |
714 } | |
715 | |
716 if (m < match) | |
717 match = m; // pick worst match | |
718 } | |
719 | |
720 Ldone: | |
721 //printf("match = %d\n", match); | |
722 return match; | |
723 | |
724 Nomatch: | |
725 //printf("no match\n"); | |
726 return MATCH.MATCHnomatch; | |
727 } | |
728 | |
96 | 729 override type* toCtype() |
0 | 730 { |
731 if (ctype) { | |
732 return ctype; | |
733 } | |
734 | |
735 type* t; | |
736 if (true) | |
737 { | |
738 param_t* paramtypes; | |
739 tym_t tyf; | |
740 type* tp; | |
741 | |
742 paramtypes = null; | |
743 size_t nparams = Argument.dim(parameters); | |
744 for (size_t i = 0; i < nparams; i++) | |
745 { | |
746 Argument arg = Argument.getNth(parameters, i); | |
747 tp = arg.type.toCtype(); | |
748 if (arg.storageClass & (STC.STCout | STC.STCref)) | |
749 { | |
750 // C doesn't have reference types, so it's really a pointer | |
751 // to the parameter type | |
752 tp = type_allocn(TYM.TYref, tp); | |
753 } | |
754 param_append_type(¶mtypes,tp); | |
755 } | |
756 tyf = totym(); | |
757 t = type_alloc(tyf); | |
758 t.Tflags |= TF.TFprototype; | |
759 if (varargs != 1) | |
760 t.Tflags |= TF.TFfixed; | |
761 ctype = t; | |
762 t.Tnext = next.toCtype(); | |
763 t.Tnext.Tcount++; | |
764 t.Tparamtypes = paramtypes; | |
765 } | |
766 ctype = t; | |
767 return t; | |
768 } | |
769 | |
770 /*************************** | |
771 * Determine return style of function - whether in registers or | |
772 * through a hidden pointer to the caller's stack. | |
773 */ | |
96 | 774 RET retStyle() |
0 | 775 { |
776 //printf("TypeFunction.retStyle() %s\n", toChars()); | |
95 | 777 version (DMDV2) |
778 { | |
0 | 779 if (isref) |
780 return RET.RETregs; // returns a pointer | |
781 } | |
782 | |
783 Type tn = next.toBasetype(); | |
95 | 784 Type tns = tn; |
785 ulong sz = tn.size(); | |
0 | 786 |
95 | 787 version(SARRAYVALUE) |
788 { | |
789 if (tn.ty == Tsarray) | |
790 { | |
791 do | |
792 { | |
793 tns = tns.nextOf().toBasetype(); | |
794 } while (tns.ty == Tsarray); | |
795 if (tns.ty != Tstruct) | |
796 { | |
797 if (global.params.isLinux && linkage != LINKd) | |
798 {} | |
799 else | |
800 { | |
801 switch (sz) | |
802 { case 1: | |
803 case 2: | |
804 case 4: | |
805 case 8: | |
96 | 806 return RET.RETregs; // return small structs in regs |
807 // (not 3 byte structs!) | |
95 | 808 default: |
809 break; | |
810 } | |
811 } | |
96 | 812 return RET.RETstack; |
95 | 813 } |
814 } | |
815 } | |
816 if (tns.ty == TY.Tstruct) | |
0 | 817 { |
818 StructDeclaration sd = (cast(TypeStruct)tn).sym; | |
819 if (global.params.isLinux && linkage != LINK.LINKd) { | |
820 ; | |
821 } | |
95 | 822 ///version (DMDV2) { // TODO: |
823 else if (sd.dtor || sd.cpctor) | |
824 { | |
0 | 825 } |
826 ///} | |
827 else | |
828 { | |
95 | 829 switch (sz) |
0 | 830 { |
831 case 1: | |
832 case 2: | |
833 case 4: | |
834 case 8: | |
835 return RET.RETregs; // return small structs in regs | |
836 // (not 3 byte structs!) | |
837 default: | |
838 break; | |
839 } | |
840 } | |
841 return RET.RETstack; | |
842 } | |
843 else if ((global.params.isLinux || global.params.isOSX || global.params.isFreeBSD || global.params.isSolaris) && | |
844 linkage == LINK.LINKc && | |
845 tn.iscomplex()) | |
846 { | |
847 if (tn.ty == TY.Tcomplex32) | |
848 return RET.RETregs; // in EDX:EAX, not ST1:ST0 | |
849 else | |
850 return RET.RETstack; | |
851 } | |
852 else | |
853 return RET.RETregs; | |
854 } | |
855 | |
72 | 856 override TYM totym() |
0 | 857 { |
858 TYM tyf; | |
859 | |
860 //printf("TypeFunction.totym(), linkage = %d\n", linkage); | |
861 switch (linkage) | |
862 { | |
863 case LINK.LINKwindows: | |
864 tyf = (varargs == 1) ? TYM.TYnfunc : TYM.TYnsfunc; | |
865 break; | |
866 | |
867 case LINK.LINKpascal: | |
868 tyf = (varargs == 1) ? TYM.TYnfunc : TYM.TYnpfunc; | |
869 break; | |
870 | |
871 case LINK.LINKc: | |
872 tyf = TYM.TYnfunc; | |
22
fd4acc376c45
Implemented object file output and linking on linux.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
873 version (POSIX) {///TARGET_LINUX || TARGET_OSX || TARGET_FREEBSD || TARGET_SOLARIS |
0 | 874 if (retStyle() == RET.RETstack) |
875 tyf = TYM.TYhfunc; | |
876 } | |
877 break; | |
878 | |
879 case LINK.LINKd: | |
880 tyf = (varargs == 1) ? TYM.TYnfunc : TYM.TYjfunc; | |
881 break; | |
882 | |
883 case LINK.LINKcpp: | |
884 tyf = TYM.TYnfunc; | |
885 break; | |
886 | |
887 default: | |
888 writef("linkage = %d\n", linkage); | |
889 assert(0); | |
890 } | |
891 version (DMDV2) { | |
892 if (isnothrow) | |
893 tyf |= mTY.mTYnothrow; | |
894 } | |
895 return tyf; | |
896 } | |
22
fd4acc376c45
Implemented object file output and linking on linux.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
897 } |