Mercurial > projects > ddmd
annotate dmd/FuncDeclaration.d @ 68:ee3a9f34dc48
final bits of codegen implementation to compile Phobos
author | korDen |
---|---|
date | Tue, 24 Aug 2010 16:44:34 +0400 |
parents | cab4c37afb89 |
children | 2e2a5c3f943a |
rev | line source |
---|---|
0 | 1 module dmd.FuncDeclaration; |
2 | |
3 import dmd.Declaration; | |
4 import dmd.DotIdExp; | |
63 | 5 import dmd.AddrExp; |
0 | 6 import dmd.TryFinallyStatement; |
7 import dmd.StaticDtorDeclaration; | |
63 | 8 import dmd.GlobalExpressions; |
0 | 9 import dmd.PeelStatement; |
10 import dmd.SynchronizedStatement; | |
11 import dmd.TOK; | |
12 import dmd.SymOffExp; | |
13 import dmd.AssignExp; | |
14 import dmd.ExpInitializer; | |
15 import dmd.BE; | |
16 import dmd.Id; | |
17 import dmd.StringExp; | |
18 import dmd.DsymbolExp; | |
19 import dmd.HaltExp; | |
20 import dmd.CommaExp; | |
21 import dmd.ReturnStatement; | |
22 import dmd.IntegerExp; | |
23 import dmd.ExpStatement; | |
24 import dmd.CSX; | |
25 import dmd.CompoundStatement; | |
26 import dmd.LabelStatement; | |
27 import dmd.ThisExp; | |
28 import dmd.SuperExp; | |
29 import dmd.IdentifierExp; | |
30 import dmd.AssertExp; | |
31 import dmd.CallExp; | |
32 import dmd.RET; | |
33 import dmd.VarExp; | |
34 import dmd.TupleDeclaration; | |
35 import dmd.ThisDeclaration; | |
36 import dmd.TypeTuple; | |
37 import dmd.TemplateInstance; | |
38 import dmd.ScopeDsymbol; | |
39 import dmd.AliasDeclaration; | |
40 import dmd.MOD; | |
41 import dmd.PROT; | |
42 import dmd.Lexer; | |
43 import dmd.LINK; | |
44 import dmd.CtorDeclaration; | |
45 import dmd.Global; | |
46 import dmd.DtorDeclaration; | |
47 import dmd.InvariantDeclaration; | |
48 import dmd.TY; | |
49 import dmd.PtrExp; | |
50 import dmd.DeclarationExp; | |
51 import dmd.InlineDoState; | |
52 import dmd.Argument; | |
53 import dmd.StructDeclaration; | |
54 import dmd.ClassDeclaration; | |
55 import dmd.InterfaceDeclaration; | |
56 import dmd.Array; | |
57 import dmd.Statement; | |
58 import dmd.Identifier; | |
59 import dmd.VarDeclaration; | |
60 import dmd.LabelDsymbol; | |
61 import dmd.DsymbolTable; | |
62 import dmd.ArrayTypes; | |
63 import dmd.Loc; | |
64 import dmd.ILS; | |
65 import dmd.ForeachStatement; | |
66 import dmd.Type; | |
67 import dmd.BUILTIN; | |
68 import dmd.TypeFunction; | |
69 import dmd.Expression; | |
70 import dmd.STC; | |
71 import dmd.Dsymbol; | |
72 import dmd.Scope; | |
73 import dmd.OutBuffer; | |
74 import dmd.HdrGenState; | |
75 import dmd.MATCH; | |
76 import dmd.AggregateDeclaration; | |
77 import dmd.InterState; | |
78 import dmd.InlineScanState; | |
79 import dmd.IRState; | |
80 import dmd.Util; | |
81 import dmd.BaseClass; | |
82 import dmd.Module; | |
83 import dmd.ILS; | |
84 import dmd.InlineCostState; | |
85 | |
86 import dmd.expression.Util; | |
87 | |
88 import dmd.declaration.Match; | |
89 | |
90 import dmd.backend.Symbol; | |
91 import dmd.backend.func_t; | |
92 import dmd.backend.Util; | |
93 import dmd.backend.glue; | |
94 import dmd.backend.SC; | |
95 import dmd.backend.F; | |
96 import dmd.backend.Cstate; | |
97 import dmd.backend.TYM; | |
98 import dmd.backend.OPER; | |
99 import dmd.backend.TYFL; | |
100 import dmd.backend.TYPE; | |
101 import dmd.backend.SFL; | |
102 import dmd.backend.mTY; | |
103 import dmd.backend.FL; | |
104 import dmd.backend.REG; | |
105 import dmd.backend.block; | |
106 import dmd.backend.Blockx; | |
107 import dmd.backend.Config; | |
108 import dmd.backend.BC; | |
109 import dmd.backend.elem; | |
110 import dmd.backend.targ_types; | |
111 import dmd.backend.mTYman; | |
112 import dmd.backend.RTLSYM; | |
113 import dmd.backend.LIST; | |
114 | |
115 import core.stdc.stdio; | |
116 import core.stdc.string; | |
63 | 117 version (Bug4054) import core.memory; |
118 | |
119 import dmd.interpret.Util; | |
0 | 120 |
121 import std.string; | |
122 | |
123 class FuncDeclaration : Declaration | |
124 { | |
125 Array fthrows; // Array of Type's of exceptions (not used) | |
126 Statement frequire; | |
127 Statement fensure; | |
128 Statement fbody; | |
129 | |
130 Identifier outId; // identifier for out statement | |
131 VarDeclaration vresult; // variable corresponding to outId | |
132 LabelDsymbol returnLabel; // where the return goes | |
133 | |
134 DsymbolTable localsymtab; // used to prevent symbols in different | |
135 // scopes from having the same name | |
136 VarDeclaration vthis; // 'this' parameter (member and nested) | |
137 VarDeclaration v_arguments; // '_arguments' parameter | |
138 version (IN_GCC) { | |
139 VarDeclaration v_argptr; // '_argptr' variable | |
140 } | |
141 Dsymbols parameters; // Array of VarDeclaration's for parameters | |
142 DsymbolTable labtab; // statement label symbol table | |
143 Declaration overnext; // next in overload list | |
144 Loc endloc; // location of closing curly bracket | |
145 int vtblIndex = -1; // for member functions, index into vtbl[] | |
146 int naked; // !=0 if naked | |
147 int inlineAsm; // !=0 if has inline assembler | |
148 ILS inlineStatus = ILS.ILSuninitialized; | |
149 int inlineNest; // !=0 if nested inline | |
150 int cantInterpret; // !=0 if cannot interpret function | |
151 int semanticRun; // 1 semantic() run | |
152 // 2 semantic2() run | |
153 // 3 semantic3() started | |
154 // 4 semantic3() done | |
155 // 5 toObjFile() run | |
156 // this function's frame ptr | |
157 ForeachStatement fes; // if foreach body, this is the foreach | |
158 int introducing; // !=0 if 'introducing' function | |
159 Type tintro; // if !=null, then this is the type | |
160 // of the 'introducing' function | |
161 // this one is overriding | |
162 int inferRetType; // !=0 if return type is to be inferred | |
163 | |
164 // Things that should really go into Scope | |
165 int hasReturnExp; // 1 if there's a return exp; statement | |
166 // 2 if there's a throw statement | |
167 // 4 if there's an assert(0) | |
168 // 8 if there's inline asm | |
169 | |
170 // Support for NRVO (named return value optimization) | |
171 bool nrvo_can = true; // !=0 means we can do it | |
172 VarDeclaration nrvo_var; // variable to replace with shidden | |
173 Symbol* shidden; // hidden pointer passed to function | |
174 | |
175 version (DMDV2) { | |
176 BUILTIN builtin; // set if this is a known, builtin | |
177 // function we can evaluate at compile | |
178 // time | |
179 | |
180 int tookAddressOf; // set if someone took the address of | |
181 // this function | |
182 Dsymbols closureVars; // local variables in this function | |
183 // which are referenced by nested | |
184 // functions | |
185 } else { | |
186 int nestedFrameRef; // !=0 if nested variables referenced | |
187 } | |
188 | |
189 this(Loc loc, Loc endloc, Identifier id, STC storage_class, Type type) | |
190 { | |
191 super(id); | |
192 | |
193 //printf("FuncDeclaration(id = '%s', type = %p)\n", id.toChars(), type); | |
194 //printf("storage_class = x%x\n", storage_class); | |
195 this.storage_class = storage_class; | |
196 this.type = type; | |
197 this.loc = loc; | |
198 this.endloc = endloc; | |
199 | |
200 /* The type given for "infer the return type" is a TypeFunction with | |
201 * null for the return type. | |
202 */ | |
203 inferRetType = (type && type.nextOf() is null); | |
204 | |
205 closureVars = new Dsymbols(); | |
206 | |
207 version (DMDV2) { | |
208 builtin = BUILTIN.BUILTINunknown; | |
209 } | |
210 } | |
211 | |
212 Dsymbol syntaxCopy(Dsymbol s) | |
213 { | |
214 FuncDeclaration f; | |
215 | |
216 //printf("FuncDeclaration::syntaxCopy('%s')\n", toChars()); | |
217 if (s) | |
218 f = cast(FuncDeclaration)s; | |
219 else | |
220 f = new FuncDeclaration(loc, endloc, ident, storage_class, type.syntaxCopy()); | |
221 | |
222 f.outId = outId; | |
223 f.frequire = frequire ? frequire.syntaxCopy() : null; | |
224 f.fensure = fensure ? fensure.syntaxCopy() : null; | |
225 f.fbody = fbody ? fbody.syntaxCopy() : null; | |
226 assert(!fthrows); // deprecated | |
227 | |
228 return f; | |
229 } | |
230 | |
231 // Do the semantic analysis on the external interface to the function. | |
232 void semantic(Scope sc) | |
233 { | |
234 TypeFunction f; | |
235 StructDeclaration sd; | |
236 ClassDeclaration cd; | |
237 InterfaceDeclaration id; | |
238 Dsymbol pd; | |
239 | |
240 static if (false) { | |
241 printf("FuncDeclaration.semantic(sc = %p, this = %p, '%s', linkage = %d)\n", sc, this, toPrettyChars(), sc.linkage); | |
242 if (isFuncLiteralDeclaration()) | |
243 printf("\tFuncLiteralDeclaration()\n"); | |
244 printf("sc.parent = %s, parent = %s\n", sc.parent.toChars(), parent ? parent.toChars() : ""); | |
245 printf("type: %p, %s\n", type, type.toChars()); | |
246 } | |
247 | |
248 if (semanticRun && isFuncLiteralDeclaration()) | |
249 { | |
250 /* Member functions that have return types that are | |
251 * forward references can have semantic() run more than | |
252 * once on them. | |
253 * See test\interface2.d, test20 | |
254 */ | |
255 return; | |
256 } | |
257 assert(semanticRun <= 1); | |
258 semanticRun = 1; | |
259 | |
260 storage_class |= sc.stc & ~STC.STCref; | |
261 //printf("function storage_class = x%x\n", storage_class); | |
262 | |
263 if (!originalType) | |
264 originalType = type; | |
265 if (!type.deco) | |
266 { | |
267 /* Apply const and invariant storage class | |
268 * to the function type | |
269 */ | |
270 type = type.semantic(loc, sc); | |
271 STC stc = storage_class; | |
272 if (type.isInvariant()) | |
273 stc |= STC.STCimmutable; | |
274 if (type.isConst()) | |
275 stc |= STC.STCconst; | |
276 if (type.isShared() || storage_class & STC.STCsynchronized) | |
277 stc |= STC.STCshared; | |
278 switch (stc & STC.STC_TYPECTOR) | |
279 { | |
280 case STC.STCimmutable: | |
281 case STC.STCimmutable | STC.STCconst: | |
282 case STC.STCimmutable | STC.STCconst | STC.STCshared: | |
283 case STC.STCimmutable | STC.STCshared: | |
284 // Don't use toInvariant(), as that will do a merge() | |
285 type = type.makeInvariant(); | |
286 type.deco = type.merge().deco; | |
287 break; | |
288 | |
289 case STC.STCconst: | |
290 type = type.makeConst(); | |
291 type.deco = type.merge().deco; | |
292 break; | |
293 | |
294 case STC.STCshared | STC.STCconst: | |
295 type = type.makeSharedConst(); | |
296 type.deco = type.merge().deco; | |
297 break; | |
298 | |
299 case STC.STCshared: | |
300 type = type.makeShared(); | |
301 type.deco = type.merge().deco; | |
302 break; | |
303 | |
304 case STC.STCundefined: | |
305 break; | |
306 | |
307 default: | |
308 assert(0); | |
309 } | |
310 } | |
311 //type.print(); | |
312 if (type.ty != TY.Tfunction) | |
313 { | |
314 error("%s must be a function", toChars()); | |
315 return; | |
316 } | |
317 f = cast(TypeFunction)type; | |
318 size_t nparams = Argument.dim(f.parameters); | |
319 | |
320 linkage = sc.linkage; | |
321 // if (!parent) | |
322 { | |
323 //parent = sc.scopesym; | |
324 parent = sc.parent; | |
325 } | |
326 protection = sc.protection; | |
327 Dsymbol parent = toParent(); | |
328 | |
329 if (storage_class & STC.STCscope) | |
330 error("functions cannot be scope"); | |
331 | |
332 if (isAbstract() && !isVirtual()) | |
333 error("non-virtual functions cannot be abstract"); | |
334 | |
335 if ((f.isConst() || f.isInvariant()) && !isThis()) | |
336 error("without 'this' cannot be const/immutable"); | |
337 | |
338 if (isAbstract() && isFinal()) | |
339 error("cannot be both final and abstract"); | |
340 static if (false) { | |
341 if (isAbstract() && fbody) | |
342 error("abstract functions cannot have bodies"); | |
343 } | |
344 | |
345 static if (false) { | |
346 if (isStaticConstructor() || isStaticDestructor()) | |
347 { | |
348 if (!isStatic() || type.nextOf().ty != Tvoid) | |
349 error("static constructors / destructors must be static void"); | |
350 if (f.arguments && f.arguments.dim) | |
351 error("static constructors / destructors must have empty parameter list"); | |
352 // BUG: check for invalid storage classes | |
353 } | |
354 } | |
355 | |
356 version (IN_GCC) { | |
357 AggregateDeclaration ad; | |
358 | |
359 ad = parent.isAggregateDeclaration(); | |
360 if (ad) | |
361 ad.methods.push(cast(void*)this); | |
362 } | |
363 sd = parent.isStructDeclaration(); | |
364 if (sd) | |
365 { | |
366 if (isCtorDeclaration()) | |
367 { | |
368 return; | |
369 } | |
370 static if (false) { | |
371 // Verify no constructors, destructors, etc. | |
372 if (isCtorDeclaration() | |
373 //||isDtorDeclaration() | |
374 //|| isInvariantDeclaration() | |
375 //|| isUnitTestDeclaration() | |
376 ) | |
377 { | |
378 error("special member functions not allowed for %ss", sd.kind()); | |
379 } | |
380 | |
381 if (!sd.inv) | |
382 sd.inv = isInvariantDeclaration(); | |
383 | |
384 if (!sd.aggNew) | |
385 sd.aggNew = isNewDeclaration(); | |
386 | |
387 if (isDelete()) | |
388 { | |
389 if (sd.aggDelete) | |
390 error("multiple delete's for struct %s", sd.toChars()); | |
391 sd.aggDelete = cast(DeleteDeclaration)this; | |
392 } | |
393 } | |
394 } | |
395 | |
396 id = parent.isInterfaceDeclaration(); | |
397 if (id) | |
398 { | |
399 storage_class |= STC.STCabstract; | |
400 | |
401 if (isCtorDeclaration() || | |
402 ///static if (DMDV2) { | |
403 isPostBlitDeclaration() || | |
404 ///} | |
405 isDtorDeclaration() || | |
406 isInvariantDeclaration() || | |
407 isUnitTestDeclaration() || isNewDeclaration() || isDelete()) | |
408 error("special function not allowed in interface %s", id.toChars()); | |
409 if (fbody) | |
410 error("function body is not abstract in interface %s", id.toChars()); | |
411 } | |
412 | |
413 /* Template member functions aren't virtual: | |
414 * interface TestInterface { void tpl(T)(); } | |
415 * and so won't work in interfaces | |
416 */ | |
417 if ((pd = toParent()) !is null && | |
418 pd.isTemplateInstance() && | |
419 (pd = toParent2()) !is null && | |
420 (id = pd.isInterfaceDeclaration()) !is null) | |
421 { | |
422 error("template member function not allowed in interface %s", id.toChars()); | |
423 } | |
424 | |
425 cd = parent.isClassDeclaration(); | |
426 if (cd) | |
427 { int vi; | |
428 CtorDeclaration ctor; | |
429 DtorDeclaration dtor; | |
430 InvariantDeclaration inv; | |
431 | |
432 if (isCtorDeclaration()) | |
433 { | |
434 // ctor = cast(CtorDeclaration)this; | |
435 // if (!cd.ctor) | |
436 // cd.ctor = ctor; | |
437 return; | |
438 } | |
439 | |
440 static if (false) { | |
441 dtor = isDtorDeclaration(); | |
442 if (dtor) | |
443 { | |
444 if (cd.dtor) | |
445 error("multiple destructors for class %s", cd.toChars()); | |
446 cd.dtor = dtor; | |
447 } | |
448 | |
449 inv = isInvariantDeclaration(); | |
450 if (inv) | |
451 { | |
452 cd.inv = inv; | |
453 } | |
454 | |
455 if (isNewDeclaration()) | |
456 { | |
457 if (!cd.aggNew) | |
458 cd.aggNew = cast(NewDeclaration)this; | |
459 } | |
460 | |
461 if (isDelete()) | |
462 { | |
463 if (cd.aggDelete) | |
464 error("multiple delete's for class %s", cd.toChars()); | |
465 cd.aggDelete = cast(DeleteDeclaration)this; | |
466 } | |
467 } | |
468 | |
469 if (storage_class & STC.STCabstract) | |
470 cd.isabstract = true; | |
471 | |
472 // if static function, do not put in vtbl[] | |
473 if (!isVirtual()) | |
474 { | |
475 //printf("\tnot virtual\n"); | |
476 goto Ldone; | |
477 } | |
478 | |
479 // Find index of existing function in vtbl[] to override | |
480 vi = findVtblIndex(cd.vtbl, cd.baseClass ? cd.baseClass.vtbl.dim : 0); | |
481 switch (vi) | |
482 { | |
483 case -1: | |
484 /* Didn't find one, so | |
485 * This is an 'introducing' function which gets a new | |
486 * slot in the vtbl[]. | |
487 */ | |
488 | |
489 // Verify this doesn't override previous final function | |
490 if (cd.baseClass) | |
491 { | |
492 Dsymbol s = cd.baseClass.search(loc, ident, 0); | |
493 if (s) | |
494 { | |
495 FuncDeclaration ff = s.isFuncDeclaration(); | |
496 ff = ff.overloadExactMatch(type); | |
497 if (ff && ff.isFinal() && ff.prot() != PROT.PROTprivate) | |
498 error("cannot override final function %s", ff.toPrettyChars()); | |
499 } | |
500 } | |
501 | |
502 if (isFinal()) | |
503 { | |
504 if (isOverride()) | |
505 error("does not override any function"); | |
506 cd.vtblFinal.push(cast(void*)this); | |
507 } | |
508 else | |
509 { | |
510 // Append to end of vtbl[] | |
511 //printf("\tintroducing function\n"); | |
512 introducing = 1; | |
513 vi = cd.vtbl.dim; | |
514 cd.vtbl.push(cast(void*)this); | |
515 vtblIndex = vi; | |
516 } | |
517 break; | |
518 | |
519 case -2: // can't determine because of fwd refs | |
520 cd.sizeok = 2; // can't finish due to forward reference | |
521 return; | |
522 | |
523 default: | |
524 { | |
525 FuncDeclaration fdv = cast(FuncDeclaration)cd.vtbl.data[vi]; | |
526 // This function is covariant with fdv | |
527 if (fdv.isFinal()) | |
528 error("cannot override final function %s", fdv.toPrettyChars()); | |
529 | |
530 version (DMDV2) { | |
531 if (!isOverride()) | |
532 warning(loc, "overrides base class function %s, but is not marked with 'override'", fdv.toPrettyChars()); | |
533 } | |
534 | |
535 if (fdv.toParent() == parent) | |
536 { | |
537 // If both are mixins, then error. | |
538 // If either is not, the one that is not overrides | |
539 // the other. | |
540 if (fdv.parent.isClassDeclaration()) | |
541 break; | |
542 if (!this.parent.isClassDeclaration() | |
543 ///static if (!BREAKABI) { | |
544 && !isDtorDeclaration() | |
545 ///} | |
546 ///version (DMDV2) { | |
547 && !isPostBlitDeclaration() | |
548 ///} | |
549 ) | |
550 error("multiple overrides of same function"); | |
551 } | |
552 cd.vtbl.data[vi] = cast(void*)this; | |
553 vtblIndex = vi; | |
554 | |
555 /* This works by whenever this function is called, | |
556 * it actually returns tintro, which gets dynamically | |
557 * cast to type. But we know that tintro is a base | |
558 * of type, so we could optimize it by not doing a | |
559 * dynamic cast, but just subtracting the isBaseOf() | |
560 * offset if the value is != null. | |
561 */ | |
562 | |
563 if (fdv.tintro) | |
564 tintro = fdv.tintro; | |
565 else if (!type.equals(fdv.type)) | |
566 { | |
567 /* Only need to have a tintro if the vptr | |
568 * offsets differ | |
569 */ | |
570 int offset; | |
571 if (fdv.type.nextOf().isBaseOf(type.nextOf(), &offset)) | |
572 { | |
573 tintro = fdv.type; | |
574 } | |
575 } | |
576 break; | |
577 } | |
578 } | |
579 | |
580 /* Go through all the interface bases. | |
581 * If this function is covariant with any members of those interface | |
582 * functions, set the tintro. | |
583 */ | |
584 for (int i = 0; i < cd.interfaces_dim; i++) | |
585 { | |
586 BaseClass b = cd.interfaces[i]; | |
587 vi = findVtblIndex(b.base.vtbl, b.base.vtbl.dim); | |
588 switch (vi) | |
589 { | |
590 case -1: | |
591 break; | |
592 | |
593 case -2: | |
594 cd.sizeok = 2; // can't finish due to forward reference | |
595 return; | |
596 | |
597 default: | |
598 { FuncDeclaration fdv = cast(FuncDeclaration)b.base.vtbl.data[vi]; | |
599 Type ti = null; | |
600 | |
601 if (fdv.tintro) | |
602 ti = fdv.tintro; | |
603 else if (!type.equals(fdv.type)) | |
604 { | |
605 /* Only need to have a tintro if the vptr | |
606 * offsets differ | |
607 */ | |
608 int offset; | |
609 if (fdv.type.nextOf().isBaseOf(type.nextOf(), &offset)) | |
610 { | |
611 ti = fdv.type; | |
612 static if (false) { | |
613 if (offset) | |
614 ti = fdv.type; | |
615 else if (type.nextOf().ty == Tclass) | |
616 { | |
617 ClassDeclaration cdn = (cast(TypeClass)type.nextOf()).sym; | |
618 if (cdn && cdn.sizeok != 1) | |
619 ti = fdv.type; | |
620 } | |
621 } | |
622 } | |
623 } | |
624 if (ti) | |
625 { | |
626 if (tintro && !tintro.equals(ti)) | |
627 { | |
628 error("incompatible covariant types %s and %s", tintro.toChars(), ti.toChars()); | |
629 } | |
630 tintro = ti; | |
631 } | |
632 goto L2; | |
633 } | |
634 } | |
635 } | |
636 | |
637 if (introducing && isOverride()) | |
638 { | |
639 error("does not override any function"); | |
640 } | |
641 | |
642 L2: ; | |
643 } | |
644 else if (isOverride() && !parent.isTemplateInstance()) | |
645 error("override only applies to class member functions"); | |
646 | |
647 /* Do not allow template instances to add virtual functions | |
648 * to a class. | |
649 */ | |
650 if (isVirtual()) | |
651 { | |
652 TemplateInstance ti = parent.isTemplateInstance(); | |
653 if (ti) | |
654 { | |
655 // Take care of nested templates | |
656 while (1) | |
657 { | |
658 TemplateInstance ti2 = ti.tempdecl.parent.isTemplateInstance(); | |
659 if (!ti2) | |
660 break; | |
661 ti = ti2; | |
662 } | |
663 | |
664 // If it's a member template | |
665 ClassDeclaration cdd = ti.tempdecl.isClassMember(); | |
666 if (cdd) | |
667 { | |
668 error("cannot use template to add virtual function to class '%s'", cdd.toChars()); | |
669 } | |
670 } | |
671 } | |
672 | |
673 if (isMain()) | |
674 { | |
675 // Check parameters to see if they are either () or (char[][] args) | |
676 switch (nparams) | |
677 { | |
678 case 0: | |
679 break; | |
680 | |
681 case 1: | |
682 { | |
683 Argument arg0 = Argument.getNth(f.parameters, 0); | |
684 if (arg0.type.ty != TY.Tarray || | |
685 arg0.type.nextOf().ty != TY.Tarray || | |
686 arg0.type.nextOf().nextOf().ty != TY.Tchar || | |
687 arg0.storageClass & (STC.STCout | STC.STCref | STC.STClazy)) | |
688 goto Lmainerr; | |
689 break; | |
690 } | |
691 | |
692 default: | |
693 goto Lmainerr; | |
694 } | |
695 | |
696 if (f.nextOf().ty != TY.Tint32 && f.nextOf().ty != TY.Tvoid) | |
697 error("must return int or void, not %s", f.nextOf().toChars()); | |
698 if (f.varargs) | |
699 { | |
700 Lmainerr: | |
701 error("parameters must be main() or main(char[][] args)"); | |
702 } | |
703 } | |
704 | |
705 if (ident == Id.assign && (sd || cd)) | |
706 { // Disallow identity assignment operator. | |
707 | |
708 // opAssign(...) | |
709 if (nparams == 0) | |
710 { if (f.varargs == 1) | |
711 goto Lassignerr; | |
712 } | |
713 else | |
714 { | |
715 Argument arg0 = Argument.getNth(f.parameters, 0); | |
716 Type t0 = arg0.type.toBasetype(); | |
717 Type tb = sd ? sd.type : cd.type; | |
718 if (arg0.type.implicitConvTo(tb) || | |
719 (sd && t0.ty == TY.Tpointer && t0.nextOf().implicitConvTo(tb)) | |
720 ) | |
721 { | |
722 if (nparams == 1) | |
723 goto Lassignerr; | |
724 Argument arg1 = Argument.getNth(f.parameters, 1); | |
725 if (arg1.defaultArg) | |
726 goto Lassignerr; | |
727 } | |
728 } | |
729 } | |
730 | |
731 Ldone: | |
732 /* Save scope for possible later use (if we need the | |
733 * function internals) | |
734 */ | |
735 scope_ = new Scope(sc); | |
736 scope_.setNoFree(); | |
737 return; | |
738 | |
739 Lassignerr: | |
740 if (sd) | |
741 { | |
742 sd.hasIdentityAssign = 1; // don't need to generate it | |
743 goto Ldone; | |
744 } | |
745 error("identity assignment operator overload is illegal"); | |
746 } | |
747 | |
748 void semantic2(Scope sc) | |
749 { | |
750 } | |
751 | |
752 // Do the semantic analysis on the internals of the function. | |
753 void semantic3(Scope sc) | |
754 { | |
755 TypeFunction f; | |
756 VarDeclaration argptr = null; | |
757 VarDeclaration _arguments = null; | |
758 | |
759 if (!parent) | |
760 { | |
761 if (global.errors) | |
762 return; | |
763 //printf("FuncDeclaration.semantic3(%s '%s', sc = %p)\n", kind(), toChars(), sc); | |
764 assert(0); | |
765 } | |
766 //printf("FuncDeclaration.semantic3('%s.%s', sc = %p, loc = %s)\n", parent.toChars(), toChars(), sc, loc.toChars()); | |
767 //fflush(stdout); | |
768 //printf("storage class = x%x %x\n", sc.stc, storage_class); | |
769 //{ static int x; if (++x == 2) *(char*)0=0; } | |
770 //printf("\tlinkage = %d\n", sc.linkage); | |
771 | |
772 //printf(" sc.incontract = %d\n", sc.incontract); | |
773 if (semanticRun >= 3) | |
774 return; | |
775 semanticRun = 3; | |
776 | |
777 if (!type || type.ty != TY.Tfunction) | |
778 return; | |
779 f = cast(TypeFunction)(type); | |
780 | |
781 // Check the 'throws' clause | |
782 if (fthrows) | |
783 { | |
784 for (int i = 0; i < fthrows.dim; i++) | |
785 { | |
786 Type t = cast(Type)fthrows.data[i]; | |
787 | |
788 t = t.semantic(loc, sc); | |
789 if (!t.isClassHandle()) | |
790 error("can only throw classes, not %s", t.toChars()); | |
791 } | |
792 } | |
793 | |
794 if (fbody || frequire) | |
795 { | |
796 /* Symbol table into which we place parameters and nested functions, | |
797 * solely to diagnose name collisions. | |
798 */ | |
799 localsymtab = new DsymbolTable(); | |
800 | |
801 // Establish function scope | |
802 ScopeDsymbol ss = new ScopeDsymbol(); | |
803 ss.parent = sc.scopesym; | |
804 Scope sc2 = sc.push(ss); | |
805 sc2.func = this; | |
806 sc2.parent = this; | |
807 sc2.callSuper = 0; | |
808 sc2.sbreak = null; | |
809 sc2.scontinue = null; | |
810 sc2.sw = null; | |
811 sc2.fes = fes; | |
812 sc2.linkage = LINK.LINKd; | |
813 sc2.stc &= ~(STC.STCauto | STC.STCscope | STC.STCstatic | STC.STCabstract | STC.STCdeprecated | STC.STC_TYPECTOR | STC.STCfinal | STC.STCtls | STC.STCgshared | STC.STCref); | |
814 sc2.protection = PROT.PROTpublic; | |
815 sc2.explicitProtection = 0; | |
816 sc2.structalign = 8; | |
817 sc2.incontract = 0; | |
818 sc2.tf = null; | |
819 sc2.noctor = 0; | |
820 | |
821 // Declare 'this' | |
822 AggregateDeclaration ad = isThis(); | |
823 if (ad) | |
824 { VarDeclaration v; | |
825 | |
826 if (isFuncLiteralDeclaration() && isNested()) | |
827 { | |
828 error("literals cannot be class members"); | |
829 return; | |
830 } | |
831 else | |
832 { | |
833 assert(!isNested()); // can't be both member and nested | |
834 assert(ad.handle); | |
835 Type thandle = ad.handle; | |
836 version (STRUCTTHISREF) { | |
837 thandle = thandle.addMod(type.mod); | |
838 thandle = thandle.addStorageClass(storage_class); | |
839 if (isPure()) | |
840 thandle = thandle.addMod(MOD.MODconst); | |
841 } else { | |
842 if (storage_class & STC.STCconst || type.isConst()) | |
843 { | |
844 assert(0); // BUG: shared not handled | |
845 if (thandle.ty == TY.Tclass) | |
846 thandle = thandle.constOf(); | |
847 else | |
848 { assert(thandle.ty == TY.Tpointer); | |
849 thandle = thandle.nextOf().constOf().pointerTo(); | |
850 } | |
851 } | |
852 else if (storage_class & STC.STCimmutable || type.isInvariant()) | |
853 { | |
854 if (thandle.ty == TY.Tclass) | |
855 thandle = thandle.invariantOf(); | |
856 else | |
857 { assert(thandle.ty == TY.Tpointer); | |
858 thandle = thandle.nextOf().invariantOf().pointerTo(); | |
859 } | |
860 } | |
861 else if (storage_class & STC.STCshared || type.isShared()) | |
862 { | |
863 assert(0); // not implemented | |
864 } | |
865 } | |
866 v = new ThisDeclaration(loc, thandle); | |
867 v.storage_class |= STC.STCparameter; | |
868 version (STRUCTTHISREF) { | |
869 if (thandle.ty == TY.Tstruct) | |
870 v.storage_class |= STC.STCref; | |
871 } | |
872 v.semantic(sc2); | |
873 if (!sc2.insert(v)) | |
874 assert(0); | |
875 v.parent = this; | |
876 vthis = v; | |
877 } | |
878 } | |
879 else if (isNested()) | |
880 { | |
881 /* The 'this' for a nested function is the link to the | |
882 * enclosing function's stack frame. | |
883 * Note that nested functions and member functions are disjoint. | |
884 */ | |
885 VarDeclaration v = new ThisDeclaration(loc, Type.tvoid.pointerTo()); | |
886 v.storage_class |= STC.STCparameter; | |
887 v.semantic(sc2); | |
888 if (!sc2.insert(v)) | |
889 assert(0); | |
890 v.parent = this; | |
891 vthis = v; | |
892 } | |
893 | |
894 // Declare hidden variable _arguments[] and _argptr | |
895 if (f.varargs == 1) | |
896 { | |
897 version (TARGET_NET) { | |
898 varArgs(sc2, f, argptr, _arguments); | |
899 } else { | |
900 Type t; | |
901 | |
902 if (f.linkage == LINK.LINKd) | |
903 { | |
904 // Declare _arguments[] | |
905 version (BREAKABI) { | |
906 v_arguments = new VarDeclaration(Loc(0), Type.typeinfotypelist.type, Id._arguments_typeinfo, null); | |
907 v_arguments.storage_class = STCparameter; | |
908 v_arguments.semantic(sc2); | |
909 sc2.insert(v_arguments); | |
910 v_arguments.parent = this; | |
911 | |
912 //t = Type.typeinfo.type.constOf().arrayOf(); | |
913 t = Type.typeinfo.type.arrayOf(); | |
914 _arguments = new VarDeclaration(Loc(0), t, Id._arguments, null); | |
915 _arguments.semantic(sc2); | |
916 sc2.insert(_arguments); | |
917 _arguments.parent = this; | |
918 } else { | |
919 t = Type.typeinfo.type.arrayOf(); | |
920 v_arguments = new VarDeclaration(Loc(0), t, Id._arguments, null); | |
921 v_arguments.storage_class = STC.STCparameter | STC.STCin; | |
922 v_arguments.semantic(sc2); | |
923 sc2.insert(v_arguments); | |
924 v_arguments.parent = this; | |
925 } | |
926 } | |
927 if (f.linkage == LINK.LINKd || (parameters && parameters.dim)) | |
928 { // Declare _argptr | |
929 version (IN_GCC) { | |
930 t = d_gcc_builtin_va_list_d_type; | |
931 } else { | |
932 t = Type.tvoid.pointerTo(); | |
933 } | |
934 argptr = new VarDeclaration(Loc(0), t, Id._argptr, null); | |
935 argptr.semantic(sc2); | |
936 sc2.insert(argptr); | |
937 argptr.parent = this; | |
938 } | |
939 } | |
940 } | |
941 | |
942 // Propagate storage class from tuple parameters to their element-parameters. | |
943 if (f.parameters) | |
944 { | |
945 for (size_t i = 0; i < f.parameters.dim; i++) | |
946 { Argument arg = cast(Argument)f.parameters.data[i]; | |
947 | |
948 //printf("[%d] arg.type.ty = %d %s\n", i, arg.type.ty, arg.type.toChars()); | |
949 if (arg.type.ty == TY.Ttuple) | |
950 { TypeTuple t = cast(TypeTuple)arg.type; | |
951 size_t dim = Argument.dim(t.arguments); | |
952 for (size_t j = 0; j < dim; j++) | |
953 { Argument narg = Argument.getNth(t.arguments, j); | |
954 narg.storageClass = arg.storageClass; | |
955 } | |
956 } | |
957 } | |
958 } | |
959 | |
960 /* Declare all the function parameters as variables | |
961 * and install them in parameters[] | |
962 */ | |
963 size_t nparams = Argument.dim(f.parameters); | |
964 if (nparams) | |
965 { /* parameters[] has all the tuples removed, as the back end | |
966 * doesn't know about tuples | |
967 */ | |
968 parameters = new Dsymbols(); | |
969 parameters.reserve(nparams); | |
970 for (size_t i = 0; i < nparams; i++) | |
971 { | |
972 Argument arg = Argument.getNth(f.parameters, i); | |
973 Identifier id = arg.ident; | |
974 if (!id) | |
975 { | |
976 /* Generate identifier for un-named parameter, | |
977 * because we need it later on. | |
978 */ | |
979 arg.ident = id = Identifier.generateId("_param_", i); | |
980 } | |
981 Type vtype = arg.type; | |
982 if (isPure()) | |
983 vtype = vtype.addMod(MOD.MODconst); | |
984 VarDeclaration v = new VarDeclaration(loc, vtype, id, null); | |
985 //printf("declaring parameter %s of type %s\n", v.toChars(), v.type.toChars()); | |
986 v.storage_class |= STC.STCparameter; | |
987 if (f.varargs == 2 && i + 1 == nparams) | |
988 v.storage_class |= STC.STCvariadic; | |
989 v.storage_class |= arg.storageClass & (STC.STCin | STC.STCout | STC.STCref | STC.STClazy | STC.STCfinal | STC.STC_TYPECTOR | STC.STCnodtor); | |
990 v.semantic(sc2); | |
991 if (!sc2.insert(v)) | |
992 error("parameter %s.%s is already defined", toChars(), v.toChars()); | |
993 else | |
994 parameters.push(cast(void*)v); | |
995 localsymtab.insert(v); | |
996 v.parent = this; | |
997 } | |
998 } | |
999 | |
1000 // Declare the tuple symbols and put them in the symbol table, | |
1001 // but not in parameters[]. | |
1002 if (f.parameters) | |
1003 { | |
1004 for (size_t i = 0; i < f.parameters.dim; i++) | |
1005 { Argument arg = cast(Argument)f.parameters.data[i]; | |
1006 | |
1007 if (!arg.ident) | |
1008 continue; // never used, so ignore | |
1009 if (arg.type.ty == TY.Ttuple) | |
1010 { TypeTuple t = cast(TypeTuple)arg.type; | |
1011 size_t dim = Argument.dim(t.arguments); | |
1012 Objects exps = new Objects(); | |
1013 exps.setDim(dim); | |
1014 for (size_t j = 0; j < dim; j++) | |
1015 { Argument narg = Argument.getNth(t.arguments, j); | |
1016 assert(narg.ident); | |
1017 VarDeclaration v = sc2.search(Loc(0), narg.ident, null).isVarDeclaration(); | |
1018 assert(v); | |
1019 Expression e = new VarExp(v.loc, v); | |
1020 exps.data[j] = cast(void*)e; | |
1021 } | |
1022 assert(arg.ident); | |
1023 TupleDeclaration v = new TupleDeclaration(loc, arg.ident, exps); | |
1024 //printf("declaring tuple %s\n", v.toChars()); | |
1025 v.isexp = 1; | |
1026 if (!sc2.insert(v)) | |
1027 error("parameter %s.%s is already defined", toChars(), v.toChars()); | |
1028 localsymtab.insert(v); | |
1029 v.parent = this; | |
1030 } | |
1031 } | |
1032 } | |
1033 | |
1034 /* Do the semantic analysis on the [in] preconditions and | |
1035 * [out] postconditions. | |
1036 */ | |
1037 sc2.incontract++; | |
1038 | |
1039 if (frequire) | |
1040 { /* frequire is composed of the [in] contracts | |
1041 */ | |
1042 // BUG: need to error if accessing out parameters | |
1043 // BUG: need to treat parameters as const | |
1044 // BUG: need to disallow returns and throws | |
1045 // BUG: verify that all in and ref parameters are read | |
1046 frequire = frequire.semantic(sc2); | |
1047 labtab = null; // so body can't refer to labels | |
1048 } | |
1049 | |
1050 if (fensure || addPostInvariant()) | |
1051 { /* fensure is composed of the [out] contracts | |
1052 */ | |
1053 ScopeDsymbol sym = new ScopeDsymbol(); | |
1054 sym.parent = sc2.scopesym; | |
1055 sc2 = sc2.push(sym); | |
1056 | |
1057 assert(type.nextOf()); | |
1058 if (type.nextOf().ty == TY.Tvoid) | |
1059 { | |
1060 if (outId) | |
1061 error("void functions have no result"); | |
1062 } | |
1063 else | |
1064 { | |
1065 if (!outId) | |
1066 outId = Id.result; // provide a default | |
1067 } | |
1068 | |
1069 if (outId) | |
1070 { // Declare result variable | |
1071 VarDeclaration v; | |
1072 Loc loc = this.loc; | |
1073 | |
1074 if (fensure) | |
1075 loc = fensure.loc; | |
1076 | |
1077 v = new VarDeclaration(loc, type.nextOf(), outId, null); | |
1078 v.noauto = true; | |
1079 version (DMDV2) { | |
1080 if (f.isref) | |
1081 { | |
1082 v.storage_class |= STC.STCref | STC.STCforeach; | |
1083 } | |
1084 } | |
1085 sc2.incontract--; | |
1086 v.semantic(sc2); | |
1087 sc2.incontract++; | |
1088 if (!sc2.insert(v)) | |
1089 error("out result %s is already defined", v.toChars()); | |
1090 v.parent = this; | |
1091 vresult = v; | |
1092 | |
1093 // vresult gets initialized with the function return value | |
1094 // in ReturnStatement.semantic() | |
1095 } | |
1096 | |
1097 // BUG: need to treat parameters as const | |
1098 // BUG: need to disallow returns and throws | |
1099 if (fensure) | |
1100 { fensure = fensure.semantic(sc2); | |
1101 labtab = null; // so body can't refer to labels | |
1102 } | |
1103 | |
1104 if (!global.params.useOut) | |
1105 { fensure = null; // discard | |
1106 vresult = null; | |
1107 } | |
1108 | |
1109 // Postcondition invariant | |
1110 if (addPostInvariant()) | |
1111 { | |
1112 Expression e = null; | |
1113 if (isCtorDeclaration()) | |
1114 { | |
1115 // Call invariant directly only if it exists | |
1116 InvariantDeclaration inv = ad.inv; | |
1117 ClassDeclaration cd = ad.isClassDeclaration(); | |
1118 | |
1119 while (!inv && cd) | |
1120 { | |
1121 cd = cd.baseClass; | |
1122 if (!cd) | |
1123 break; | |
1124 inv = cd.inv; | |
1125 } | |
1126 if (inv) | |
1127 { | |
1128 e = new DsymbolExp(Loc(0), inv); | |
1129 e = new CallExp(Loc(0), e); | |
1130 e = e.semantic(sc2); | |
1131 } | |
1132 } | |
1133 else | |
1134 { // Call invariant virtually | |
1135 Expression v = new ThisExp(Loc(0)); | |
1136 v.type = vthis.type; | |
1137 version (STRUCTTHISREF) { | |
1138 if (ad.isStructDeclaration()) | |
1139 v = v.addressOf(sc); | |
1140 } | |
1141 e = new AssertExp(Loc(0), v); | |
1142 } | |
1143 if (e) | |
1144 { | |
1145 ExpStatement s = new ExpStatement(Loc(0), e); | |
1146 if (fensure) | |
1147 fensure = new CompoundStatement(Loc(0), s, fensure); | |
1148 else | |
1149 fensure = s; | |
1150 } | |
1151 } | |
1152 | |
1153 if (fensure) | |
1154 { returnLabel = new LabelDsymbol(Id.returnLabel); | |
1155 LabelStatement ls = new LabelStatement(Loc(0), Id.returnLabel, fensure); | |
1156 ls.isReturnLabel = 1; | |
1157 returnLabel.statement = ls; | |
1158 } | |
1159 sc2 = sc2.pop(); | |
1160 } | |
1161 | |
1162 sc2.incontract--; | |
1163 | |
1164 if (fbody) | |
1165 { ClassDeclaration cd = isClassMember(); | |
1166 | |
1167 /* If this is a class constructor | |
1168 */ | |
1169 if (isCtorDeclaration() && cd) | |
1170 { | |
1171 for (int i = 0; i < cd.fields.dim; i++) | |
1172 { VarDeclaration v = cast(VarDeclaration)cd.fields.data[i]; | |
1173 | |
1174 v.ctorinit = 0; | |
1175 } | |
1176 } | |
1177 | |
1178 if (inferRetType || f.retStyle() != RET.RETstack) | |
1179 nrvo_can = 0; | |
1180 | |
1181 fbody = fbody.semantic(sc2); | |
1182 if (!fbody) | |
1183 fbody = new CompoundStatement(Loc(0), new Statements()); | |
1184 | |
1185 if (inferRetType) | |
1186 { // If no return type inferred yet, then infer a void | |
1187 if (!type.nextOf()) | |
1188 { | |
1189 (cast(TypeFunction)type).next = Type.tvoid; | |
1190 type = type.semantic(loc, sc); | |
1191 } | |
1192 f = cast(TypeFunction)type; | |
1193 } | |
1194 | |
1195 if (isStaticCtorDeclaration()) | |
1196 { | |
1197 /* It's a static constructor. Ensure that all | |
1198 * ctor consts were initialized. | |
1199 */ | |
1200 | |
1201 Dsymbol p = toParent(); | |
1202 ScopeDsymbol add = p.isScopeDsymbol(); | |
1203 if (!add) | |
1204 { | |
1205 error("static constructor can only be member of struct/class/module, not %s %s", p.kind(), p.toChars()); | |
1206 } | |
1207 else | |
1208 { | |
1209 for (int i = 0; i < add.members.dim; i++) | |
1210 { Dsymbol s = cast(Dsymbol)add.members.data[i]; | |
1211 | |
1212 s.checkCtorConstInit(); | |
1213 } | |
1214 } | |
1215 } | |
1216 | |
1217 if (isCtorDeclaration() && cd) | |
1218 { | |
1219 //printf("callSuper = x%x\n", sc2.callSuper); | |
1220 | |
1221 // Verify that all the ctorinit fields got initialized | |
1222 if (!(sc2.callSuper & CSX.CSXthis_ctor)) | |
1223 { | |
1224 for (int i = 0; i < cd.fields.dim; i++) | |
1225 { VarDeclaration v = cast(VarDeclaration)cd.fields.data[i]; | |
1226 | |
1227 if (v.ctorinit == 0 && v.isCtorinit()) | |
1228 error("missing initializer for final field %s", v.toChars()); | |
1229 } | |
1230 } | |
1231 | |
1232 if (!(sc2.callSuper & CSX.CSXany_ctor) && | |
1233 cd.baseClass && cd.baseClass.ctor) | |
1234 { | |
1235 sc2.callSuper = 0; | |
1236 | |
1237 // Insert implicit super() at start of fbody | |
1238 Expression e1 = new SuperExp(Loc(0)); | |
1239 Expression e = new CallExp(Loc(0), e1); | |
1240 | |
1241 e = e.trySemantic(sc2); | |
1242 if (!e) | |
1243 error("no match for implicit super() call in constructor"); | |
1244 else | |
1245 { | |
1246 Statement s = new ExpStatement(Loc(0), e); | |
1247 fbody = new CompoundStatement(Loc(0), s, fbody); | |
1248 } | |
1249 } | |
1250 } | |
1251 else if (fes) | |
1252 { // For foreach(){} body, append a return 0; | |
1253 Expression e = new IntegerExp(0); | |
1254 Statement s = new ReturnStatement(Loc(0), e); | |
1255 fbody = new CompoundStatement(Loc(0), fbody, s); | |
1256 assert(!returnLabel); | |
1257 } | |
1258 else if (!hasReturnExp && type.nextOf().ty != TY.Tvoid) | |
1259 error("expected to return a value of type %s", type.nextOf().toChars()); | |
1260 else if (!inlineAsm) | |
1261 { | |
1262 version (DMDV2) { | |
1263 BE blockexit = fbody ? fbody.blockExit() : BE.BEfallthru; | |
1264 if (f.isnothrow && blockexit & BE.BEthrow) | |
1265 error("'%s' is nothrow yet may throw", toChars()); | |
1266 | |
1267 int offend = blockexit & BE.BEfallthru; | |
1268 } | |
1269 if (type.nextOf().ty == TY.Tvoid) | |
1270 { | |
1271 if (offend && isMain()) | |
1272 { // Add a return 0; statement | |
1273 Statement s = new ReturnStatement(Loc(0), new IntegerExp(0)); | |
1274 fbody = new CompoundStatement(Loc(0), fbody, s); | |
1275 } | |
1276 } | |
1277 else | |
1278 { | |
1279 if (offend) | |
1280 { | |
1281 Expression e; | |
1282 version (DMDV1) { | |
1283 warning(loc, "no return exp; or assert(0); at end of function"); | |
1284 } else { | |
1285 error("no return exp; or assert(0); at end of function"); | |
1286 } | |
1287 if (global.params.useAssert && | |
1288 !global.params.useInline) | |
1289 { /* Add an assert(0, msg); where the missing return | |
1290 * should be. | |
1291 */ | |
1292 e = new AssertExp( | |
1293 endloc, | |
1294 new IntegerExp(0), | |
1295 new StringExp(loc, "missing return expression") | |
1296 ); | |
1297 } | |
1298 else | |
1299 e = new HaltExp(endloc); | |
1300 | |
1301 e = new CommaExp(Loc(0), e, type.nextOf().defaultInit(Loc(0))); | |
1302 e = e.semantic(sc2); | |
1303 Statement s = new ExpStatement(Loc(0), e); | |
1304 fbody = new CompoundStatement(Loc(0), fbody, s); | |
1305 } | |
1306 } | |
1307 } | |
1308 } | |
1309 | |
1310 { | |
1311 Statements a = new Statements(); | |
1312 | |
1313 // Merge in initialization of 'out' parameters | |
1314 if (parameters) | |
1315 { for (size_t i = 0; i < parameters.dim; i++) | |
1316 { | |
1317 VarDeclaration v = cast(VarDeclaration)parameters.data[i]; | |
1318 if (v.storage_class & STC.STCout) | |
1319 { | |
1320 assert(v.init); | |
1321 ExpInitializer ie = v.init.isExpInitializer(); | |
1322 assert(ie); | |
1323 a.push(cast(void*)new ExpStatement(Loc(0), ie.exp)); | |
1324 } | |
1325 } | |
1326 } | |
1327 | |
1328 if (argptr) | |
1329 { // Initialize _argptr to point past non-variadic arg | |
1330 version (IN_GCC) { | |
1331 // Handled in FuncDeclaration.toObjFile | |
1332 v_argptr = argptr; | |
1333 v_argptr.init = new VoidInitializer(loc); | |
1334 } else { | |
1335 Expression e1; | |
1336 Expression e; | |
1337 Type t = argptr.type; | |
1338 VarDeclaration p; | |
1339 uint offset; | |
1340 | |
1341 e1 = new VarExp(Loc(0), argptr); | |
1342 if (parameters && parameters.dim) | |
1343 p = cast(VarDeclaration)parameters.data[parameters.dim - 1]; | |
1344 else | |
1345 p = v_arguments; // last parameter is _arguments[] | |
1346 offset = cast(uint)p.type.size(); /// | |
1347 offset = (offset + 3) & ~3; // assume stack aligns on 4 | |
1348 e = new SymOffExp(Loc(0), p, offset); | |
1349 e = new AssignExp(Loc(0), e1, e); | |
1350 e.type = t; | |
1351 a.push(cast(void*)new ExpStatement(Loc(0), e)); | |
1352 } | |
1353 } | |
1354 | |
1355 if (_arguments) | |
1356 { | |
1357 /* Advance to elements[] member of TypeInfo_Tuple with: | |
1358 * _arguments = v_arguments.elements; | |
1359 */ | |
1360 Expression e = new VarExp(Loc(0), v_arguments); | |
1361 e = new DotIdExp(Loc(0), e, Id.elements); | |
1362 Expression e1 = new VarExp(Loc(0), _arguments); | |
1363 e = new AssignExp(Loc(0), e1, e); | |
1364 e.op = TOK.TOKconstruct; | |
1365 e = e.semantic(sc2); | |
1366 a.push(cast(void*)new ExpStatement(Loc(0), e)); | |
1367 } | |
1368 | |
1369 // Merge contracts together with body into one compound statement | |
1370 | |
1371 version (_DH) { | |
1372 if (frequire && global.params.useIn) | |
1373 { frequire.incontract = 1; | |
1374 a.push(frequire); | |
1375 } | |
1376 } else { | |
1377 if (frequire && global.params.useIn) | |
1378 a.push(cast(void*)frequire); | |
1379 } | |
1380 | |
1381 // Precondition invariant | |
1382 if (addPreInvariant()) | |
1383 { | |
1384 Expression e = null; | |
1385 if (isDtorDeclaration()) | |
1386 { | |
1387 // Call invariant directly only if it exists | |
1388 InvariantDeclaration inv = ad.inv; | |
1389 ClassDeclaration cd = ad.isClassDeclaration(); | |
1390 | |
1391 while (!inv && cd) | |
1392 { | |
1393 cd = cd.baseClass; | |
1394 if (!cd) | |
1395 break; | |
1396 inv = cd.inv; | |
1397 } | |
1398 if (inv) | |
1399 { | |
1400 e = new DsymbolExp(Loc(0), inv); | |
1401 e = new CallExp(Loc(0), e); | |
1402 e = e.semantic(sc2); | |
1403 } | |
1404 } | |
1405 else | |
1406 { // Call invariant virtually | |
1407 Expression v = new ThisExp(Loc(0)); | |
1408 v.type = vthis.type; | |
1409 version (STRUCTTHISREF) { | |
1410 if (ad.isStructDeclaration()) | |
1411 v = v.addressOf(sc); | |
1412 } | |
1413 Expression se = new StringExp(Loc(0), "null this"); | |
1414 se = se.semantic(sc); | |
1415 se.type = Type.tchar.arrayOf(); | |
1416 e = new AssertExp(loc, v, se); | |
1417 } | |
1418 if (e) | |
1419 { | |
1420 ExpStatement s = new ExpStatement(Loc(0), e); | |
1421 a.push(cast(void*)s); | |
1422 } | |
1423 } | |
1424 | |
1425 if (fbody) | |
1426 a.push(cast(void*)fbody); | |
1427 | |
1428 if (fensure) | |
1429 { | |
1430 a.push(cast(void*)returnLabel.statement); | |
1431 | |
1432 if (type.nextOf().ty != TY.Tvoid) | |
1433 { | |
1434 // Create: return vresult; | |
1435 assert(vresult); | |
1436 Expression e = new VarExp(Loc(0), vresult); | |
1437 if (tintro) | |
1438 { e = e.implicitCastTo(sc, tintro.nextOf()); | |
1439 e = e.semantic(sc); | |
1440 } | |
1441 ReturnStatement s = new ReturnStatement(Loc(0), e); | |
1442 a.push(cast(void*)s); | |
1443 } | |
1444 } | |
1445 | |
1446 fbody = new CompoundStatement(Loc(0), a); | |
1447 version (DMDV2) { | |
1448 /* Append destructor calls for parameters as finally blocks. | |
1449 */ | |
1450 if (parameters) | |
1451 { for (size_t i = 0; i < parameters.dim; i++) | |
1452 { | |
1453 VarDeclaration v = cast(VarDeclaration)parameters.data[i]; | |
1454 | |
1455 if (v.storage_class & (STC.STCref | STC.STCout)) | |
1456 continue; | |
1457 | |
1458 /* Don't do this for static arrays, since static | |
1459 * arrays are called by reference. Remove this | |
1460 * when we change them to call by value. | |
1461 */ | |
1462 if (v.type.toBasetype().ty == TY.Tsarray) | |
1463 continue; | |
1464 | |
1465 Expression e = v.callAutoDtor(sc); | |
1466 if (e) | |
1467 { Statement s = new ExpStatement(Loc(0), e); | |
1468 s = s.semantic(sc); | |
1469 if (fbody.blockExit() == BE.BEfallthru) | |
1470 fbody = new CompoundStatement(Loc(0), fbody, s); | |
1471 else | |
1472 fbody = new TryFinallyStatement(Loc(0), fbody, s); | |
1473 } | |
1474 } | |
1475 } | |
1476 } | |
1477 | |
1478 static if (true) { | |
1479 if (isSynchronized()) | |
1480 { /* Wrap the entire function body in a synchronized statement | |
1481 */ | |
1482 ClassDeclaration cd = parent.isClassDeclaration(); | |
1483 if (cd) | |
1484 { | |
1485 ///version (TARGET_WINDOS) { | |
1486 if (/*config.flags2 & CFG2.CFG2seh &&*/ // always on for WINDOS | |
1487 !isStatic() && !fbody.usesEH()) | |
1488 { | |
1489 /* The back end uses the "jmonitor" hack for syncing; | |
1490 * no need to do the sync at this level. | |
1491 */ | |
1492 } | |
1493 else | |
1494 ///} | |
1495 { | |
1496 Expression vsync; | |
1497 if (isStatic()) | |
1498 { | |
1499 // The monitor is in the ClassInfo | |
1500 vsync = new DotIdExp(loc, new DsymbolExp(loc, cd), Id.classinfo_); | |
1501 } | |
1502 else | |
1503 { // 'this' is the monitor | |
1504 vsync = new VarExp(loc, vthis); | |
1505 } | |
1506 fbody = new PeelStatement(fbody); // don't redo semantic() | |
1507 fbody = new SynchronizedStatement(loc, vsync, fbody); | |
1508 fbody = fbody.semantic(sc2); | |
1509 } | |
1510 } | |
1511 else | |
1512 { | |
1513 error("synchronized function %s must be a member of a class", toChars()); | |
1514 } | |
1515 } | |
1516 } | |
1517 } | |
1518 | |
1519 sc2.callSuper = 0; | |
1520 sc2.pop(); | |
1521 } | |
1522 semanticRun = 4; | |
1523 } | |
1524 | |
1525 // called from semantic3 | |
1526 void varArgs(Scope sc, TypeFunction, ref VarDeclaration, ref VarDeclaration) | |
1527 { | |
1528 assert(false); | |
1529 } | |
1530 | |
1531 void toCBuffer(OutBuffer buf, HdrGenState* hgs) | |
1532 { | |
1533 assert(false); | |
1534 } | |
1535 | |
1536 void bodyToCBuffer(OutBuffer buf, HdrGenState* hgs) | |
1537 { | |
1538 assert(false); | |
1539 } | |
1540 | |
1541 /**************************************************** | |
1542 * Determine if 'this' overrides fd. | |
1543 * Return true if it does. | |
1544 */ | |
1545 bool overrides(FuncDeclaration fd) | |
1546 { | |
1547 bool result = false; | |
1548 | |
1549 if (fd.ident == ident) | |
1550 { | |
1551 int cov = type.covariant(fd.type); | |
1552 if (cov) | |
1553 { | |
1554 ClassDeclaration cd1 = toParent().isClassDeclaration(); | |
1555 ClassDeclaration cd2 = fd.toParent().isClassDeclaration(); | |
1556 | |
1557 if (cd1 && cd2 && cd2.isBaseOf(cd1, null)) | |
1558 result = true; | |
1559 } | |
1560 } | |
1561 return result; | |
1562 } | |
1563 | |
1564 /************************************************* | |
1565 * Find index of function in vtbl[0..dim] that | |
1566 * this function overrides. | |
1567 * Returns: | |
1568 * -1 didn't find one | |
1569 * -2 can't determine because of forward references | |
1570 */ | |
1571 int findVtblIndex(Array vtbl, int dim) | |
1572 { | |
1573 for (int vi = 0; vi < dim; vi++) | |
1574 { | |
1575 FuncDeclaration fdv = (cast(Dsymbol)vtbl.data[vi]).isFuncDeclaration(); | |
1576 if (fdv && fdv.ident is ident) | |
1577 { | |
1578 int cov = type.covariant(fdv.type); | |
1579 //printf("\tbaseclass cov = %d\n", cov); | |
1580 switch (cov) | |
1581 { | |
1582 case 0: // types are distinct | |
1583 break; | |
1584 | |
1585 case 1: | |
1586 return vi; | |
1587 | |
1588 case 2: | |
1589 //type.print(); | |
1590 //fdv.type.print(); | |
1591 //printf("%s %s\n", type.deco, fdv.type.deco); | |
1592 error("of type %s overrides but is not covariant with %s of type %s", | |
1593 type.toChars(), fdv.toPrettyChars(), fdv.type.toChars()); | |
1594 break; | |
1595 | |
1596 case 3: | |
1597 return -2; // forward references | |
1598 } | |
1599 } | |
1600 } | |
1601 return -1; | |
1602 } | |
1603 | |
1604 /**************************************************** | |
1605 * Overload this FuncDeclaration with the new one f. | |
1606 * Return !=0 if successful; i.e. no conflict. | |
1607 */ | |
1608 bool overloadInsert(Dsymbol s) | |
1609 { | |
1610 FuncDeclaration f; | |
1611 AliasDeclaration a; | |
1612 | |
1613 //writef("FuncDeclaration.overloadInsert(%s)\n", s.toChars()); | |
1614 a = s.isAliasDeclaration(); | |
1615 if (a) | |
1616 { | |
1617 if (overnext) | |
1618 return overnext.overloadInsert(a); | |
1619 | |
1620 if (!a.aliassym && a.type.ty != TY.Tident && a.type.ty != TY.Tinstance) | |
1621 { | |
1622 //writef("\ta = '%s'\n", a.type.toChars()); | |
1623 return false; | |
1624 } | |
1625 overnext = a; | |
1626 //printf("\ttrue: no conflict\n"); | |
1627 return true; | |
1628 } | |
1629 f = s.isFuncDeclaration(); | |
1630 if (!f) | |
1631 return false; | |
1632 | |
1633 static if (false) { | |
1634 /* Disable this check because: | |
1635 * const void foo(); | |
1636 * semantic() isn't run yet on foo(), so the const hasn't been | |
1637 * applied yet. | |
1638 */ | |
1639 if (type) | |
1640 { | |
1641 printf("type = %s\n", type.toChars()); | |
1642 printf("f.type = %s\n", f.type.toChars()); | |
1643 } | |
1644 if (type && f.type && // can be null for overloaded constructors | |
1645 f.type.covariant(type) && | |
1646 f.type.mod == type.mod && | |
1647 !isFuncAliasDeclaration()) | |
1648 { | |
1649 //printf("\tfalse: conflict %s\n", kind()); | |
1650 return false; | |
1651 } | |
1652 } | |
1653 | |
1654 if (overnext) | |
1655 return overnext.overloadInsert(f); | |
1656 overnext = f; | |
1657 //printf("\ttrue: no conflict\n"); | |
1658 return true; | |
1659 } | |
1660 | |
1661 FuncDeclaration overloadExactMatch(Type t) | |
1662 { | |
1663 Param1 p; | |
1664 p.t = t; | |
1665 p.f = null; | |
17
ddae60498573
Implemented mixin statements, DefaultInitExps, FileInitExps, LineInitExps and __traits.
Robert Clipsham <robert@octarineparrot.com>
parents:
14
diff
changeset
|
1666 overloadApply(this, &p.fp1, &p); |
0 | 1667 return p.f; |
1668 } | |
1669 | |
1670 FuncDeclaration overloadResolve(Loc loc, Expression ethis, Expressions arguments, int flags = 0) | |
1671 { | |
1672 TypeFunction tf; | |
1673 Match m; | |
1674 | |
1675 static if (false) { | |
1676 printf("FuncDeclaration.overloadResolve('%s')\n", toChars()); | |
1677 if (arguments) | |
1678 { | |
1679 int i; | |
1680 | |
1681 for (i = 0; i < arguments.dim; i++) | |
1682 { | |
1683 Expression arg; | |
1684 | |
1685 arg = cast(Expression)arguments.data[i]; | |
1686 assert(arg.type); | |
1687 printf("\t%s: ", arg.toChars()); | |
1688 arg.type.print(); | |
1689 } | |
1690 } | |
1691 } | |
1692 | |
1693 m.last = MATCH.MATCHnomatch; | |
1694 overloadResolveX(&m, this, ethis, arguments); | |
1695 | |
1696 if (m.count == 1) // exactly one match | |
1697 { | |
1698 return m.lastf; | |
1699 } | |
1700 else | |
1701 { | |
1702 scope OutBuffer buf = new OutBuffer(); | |
1703 | |
1704 buf.writeByte('('); | |
1705 if (arguments) | |
1706 { | |
1707 HdrGenState hgs; | |
1708 | |
1709 argExpTypesToCBuffer(buf, arguments, &hgs); | |
1710 buf.writeByte(')'); | |
1711 if (ethis) | |
1712 ethis.type.modToBuffer(buf); | |
1713 } | |
1714 else | |
1715 buf.writeByte(')'); | |
1716 | |
1717 if (m.last == MATCH.MATCHnomatch) | |
1718 { | |
1719 if (flags & 1) // if do not print error messages | |
1720 return null; // no match | |
1721 | |
1722 tf = cast(TypeFunction)type; | |
1723 | |
1724 scope OutBuffer buf2 = new OutBuffer(); | |
1725 tf.modToBuffer(buf2); | |
1726 | |
1727 //printf("tf = %s, args = %s\n", tf.deco, ((Expression *)arguments.data[0]).type.deco); | |
1728 error(loc, "%s%s is not callable using argument types %s", | |
1729 Argument.argsTypesToChars(tf.parameters, tf.varargs), | |
1730 buf2.toChars(), | |
1731 buf.toChars()); | |
1732 return m.anyf; // as long as it's not a FuncAliasDeclaration | |
1733 } | |
1734 else | |
1735 { | |
1736 static if (true) { | |
1737 TypeFunction t1 = cast(TypeFunction)m.lastf.type; | |
1738 TypeFunction t2 = cast(TypeFunction)m.nextf.type; | |
1739 | |
1740 error(loc, "called with argument types:\n\t(%s)\nmatches both:\n\t%s%s\nand:\n\t%s%s", | |
1741 buf.toChars(), | |
1742 m.lastf.toPrettyChars(), Argument.argsTypesToChars(t1.parameters, t1.varargs), | |
1743 m.nextf.toPrettyChars(), Argument.argsTypesToChars(t2.parameters, t2.varargs)); | |
1744 } else { | |
1745 error(loc, "overloads %s and %s both match argument list for %s", | |
1746 m.lastf.type.toChars(), | |
1747 m.nextf.type.toChars(), | |
1748 m.lastf.toChars()); | |
1749 } | |
1750 return m.lastf; | |
1751 } | |
1752 } | |
1753 } | |
1754 | |
1755 /************************************* | |
1756 * Determine partial specialization order of 'this' vs g. | |
1757 * This is very similar to TemplateDeclaration.leastAsSpecialized(). | |
1758 * Returns: | |
1759 * match 'this' is at least as specialized as g | |
1760 * 0 g is more specialized than 'this' | |
1761 */ | |
1762 MATCH leastAsSpecialized(FuncDeclaration g) | |
1763 { | |
1764 version (LOG_LEASTAS) { | |
1765 printf("%s.leastAsSpecialized(%s)\n", toChars(), g.toChars()); | |
1766 } | |
1767 | |
1768 /* This works by calling g() with f()'s parameters, and | |
1769 * if that is possible, then f() is at least as specialized | |
1770 * as g() is. | |
1771 */ | |
1772 | |
1773 TypeFunction tf = cast(TypeFunction)type; | |
1774 TypeFunction tg = cast(TypeFunction)g.type; | |
1775 size_t nfparams = Argument.dim(tf.parameters); | |
1776 size_t ngparams = Argument.dim(tg.parameters); | |
1777 MATCH match = MATCHexact; | |
1778 | |
1779 /* If both functions have a 'this' pointer, and the mods are not | |
1780 * the same and g's is not const, then this is less specialized. | |
1781 */ | |
1782 if (needThis() && g.needThis()) | |
1783 { | |
1784 if (tf.mod != tg.mod) | |
1785 { | |
1786 if (tg.mod == MODconst) | |
1787 match = MATCHconst; | |
1788 else | |
1789 return MATCHnomatch; | |
1790 } | |
1791 } | |
1792 | |
1793 /* Create a dummy array of arguments out of the parameters to f() | |
1794 */ | |
1795 scope Expressions args = new Expressions(); | |
1796 args.setDim(nfparams); | |
1797 for (int u = 0; u < nfparams; u++) | |
1798 { | |
1799 Argument p = Argument.getNth(tf.parameters, u); | |
1800 Expression e; | |
1801 if (p.storageClass & (STCref | STCout)) | |
1802 { | |
1803 e = new IdentifierExp(Loc(0), p.ident); | |
1804 e.type = p.type; | |
1805 } | |
1806 else | |
1807 e = p.type.defaultInit(Loc(0)); | |
1808 | |
1809 args.data[u] = cast(void*)e; | |
1810 } | |
1811 | |
1812 MATCH m = cast(MATCH) tg.callMatch(null, args); | |
1813 if (m) | |
1814 { | |
1815 /* A variadic parameter list is less specialized than a | |
1816 * non-variadic one. | |
1817 */ | |
1818 if (tf.varargs && !tg.varargs) | |
1819 goto L1; // less specialized | |
1820 | |
1821 version (LOG_LEASTAS) { | |
1822 printf(" matches %d, so is least as specialized\n", m); | |
1823 } | |
1824 return m; | |
1825 } | |
1826 L1: | |
1827 version (LOG_LEASTAS) { | |
1828 printf(" doesn't match, so is not as specialized\n"); | |
1829 } | |
1830 return MATCHnomatch; | |
1831 } | |
1832 | |
1833 /******************************** | |
1834 * Labels are in a separate scope, one per function. | |
1835 */ | |
1836 LabelDsymbol searchLabel(Identifier ident) | |
1837 { | |
1838 Dsymbol s; | |
1839 | |
1840 if (!labtab) | |
1841 labtab = new DsymbolTable(); // guess we need one | |
1842 | |
1843 s = labtab.lookup(ident); | |
1844 if (!s) | |
1845 { | |
1846 s = new LabelDsymbol(ident); | |
1847 labtab.insert(s); | |
1848 } | |
1849 | |
1850 return cast(LabelDsymbol)s; | |
1851 } | |
1852 | |
1853 /**************************************** | |
1854 * If non-static member function that has a 'this' pointer, | |
1855 * return the aggregate it is a member of. | |
1856 * Otherwise, return null. | |
1857 */ | |
1858 AggregateDeclaration isThis() | |
1859 { | |
1860 AggregateDeclaration ad = null; | |
1861 | |
1862 //printf("+FuncDeclaration.isThis() '%s'\n", toChars()); | |
1863 if ((storage_class & STC.STCstatic) == 0) | |
1864 { | |
1865 ad = isMember2(); | |
1866 } | |
1867 //printf("-FuncDeclaration.isThis() %p\n", ad); | |
1868 return ad; | |
1869 } | |
1870 | |
1871 AggregateDeclaration isMember2() | |
1872 { | |
1873 AggregateDeclaration ad = null; | |
1874 | |
1875 //printf("+FuncDeclaration.isMember2() '%s'\n", toChars()); | |
1876 for (Dsymbol s = this; s; s = s.parent) | |
1877 { | |
1878 //printf("\ts = '%s', parent = '%s', kind = %s\n", s.toChars(), s.parent.toChars(), s.parent.kind()); | |
1879 ad = s.isMember(); | |
1880 if (ad) | |
1881 { //printf("test4\n"); | |
1882 break; | |
1883 } | |
1884 if (!s.parent || (!s.parent.isTemplateInstance())) | |
1885 { //printf("test5\n"); | |
1886 break; | |
1887 } | |
1888 } | |
1889 //printf("-FuncDeclaration.isMember2() %p\n", ad); | |
1890 return ad; | |
1891 } | |
1892 | |
1893 /***************************************** | |
1894 * Determine lexical level difference from 'this' to nested function 'fd'. | |
1895 * Error if this cannot call fd. | |
1896 * Returns: | |
1897 * 0 same level | |
1898 * -1 increase nesting by 1 (fd is nested within 'this') | |
1899 * >0 decrease nesting by number | |
1900 */ | |
1901 int getLevel(Loc loc, FuncDeclaration fd) // lexical nesting level difference | |
1902 { | |
1903 int level; | |
1904 Dsymbol s; | |
1905 Dsymbol fdparent; | |
1906 | |
1907 //printf("FuncDeclaration.getLevel(fd = '%s')\n", fd.toChars()); | |
1908 fdparent = fd.toParent2(); | |
1909 if (fdparent == this) | |
1910 return -1; | |
1911 s = this; | |
1912 level = 0; | |
1913 while (fd != s && fdparent != s.toParent2()) | |
1914 { | |
1915 //printf("\ts = '%s'\n", s.toChars()); | |
1916 FuncDeclaration thisfd = s.isFuncDeclaration(); | |
1917 if (thisfd) | |
1918 { | |
1919 if (!thisfd.isNested() && !thisfd.vthis) | |
1920 goto Lerr; | |
1921 } | |
1922 else | |
1923 { | |
1924 AggregateDeclaration thiscd = s.isAggregateDeclaration(); | |
1925 if (thiscd) | |
1926 { | |
1927 if (!thiscd.isNested()) | |
1928 goto Lerr; | |
1929 } | |
1930 else | |
1931 goto Lerr; | |
1932 } | |
1933 | |
1934 s = s.toParent2(); | |
1935 assert(s); | |
1936 level++; | |
1937 } | |
1938 return level; | |
1939 | |
1940 Lerr: | |
1941 error(loc, "cannot access frame of function %s", fd.toChars()); | |
1942 return 1; | |
1943 } | |
1944 | |
1945 void appendExp(Expression e) | |
1946 { | |
1947 assert(false); | |
1948 } | |
1949 | |
1950 void appendState(Statement s) | |
1951 { | |
1952 assert(false); | |
1953 } | |
1954 | |
1955 string mangle() | |
1956 out (result) | |
1957 { | |
1958 assert(result.length > 0); | |
1959 } | |
1960 body | |
1961 { | |
1962 if (isMain()) { | |
1963 return "_Dmain"; | |
1964 } | |
1965 | |
1966 if (isWinMain() || isDllMain() || ident == Id.tls_get_addr) | |
1967 return ident.toChars(); | |
1968 | |
1969 assert(this); | |
1970 | |
1971 return Declaration.mangle(); | |
1972 } | |
1973 | |
1974 string toPrettyChars() | |
1975 { | |
1976 if (isMain()) | |
1977 return "D main"; | |
1978 else | |
1979 return Dsymbol.toPrettyChars(); | |
1980 } | |
1981 | |
1982 int isMain() | |
1983 { | |
1984 return ident is Id.main && linkage != LINK.LINKc && !isMember() && !isNested(); | |
1985 } | |
1986 | |
1987 int isWinMain() | |
1988 { | |
1989 //printf("FuncDeclaration::isWinMain() %s\n", toChars()); | |
1990 static if (false) { | |
1991 int x = ident == Id.WinMain && | |
1992 linkage != LINK.LINKc && !isMember(); | |
1993 printf("%s\n", x ? "yes" : "no"); | |
1994 return x; | |
1995 } else { | |
1996 return ident == Id.WinMain && linkage != LINK.LINKc && !isMember(); | |
1997 } | |
1998 } | |
1999 | |
2000 int isDllMain() | |
2001 { | |
2002 return ident == Id.DllMain && linkage != LINK.LINKc && !isMember(); | |
2003 } | |
2004 | |
2005 /********************************** | |
2006 * Determine if function is a builtin one that we can | |
2007 * evaluate at compile time. | |
2008 */ | |
2009 BUILTIN isBuiltin() | |
2010 { | |
2011 static string FeZe = "FNaNbeZe"; // pure nothrow real function(real) | |
2012 | |
2013 //printf("FuncDeclaration::isBuiltin() %s\n", toChars()); | |
2014 if (builtin == BUILTIN.BUILTINunknown) | |
2015 { | |
2016 builtin = BUILTIN.BUILTINnot; | |
2017 if (parent && parent.isModule()) | |
2018 { | |
2019 // If it's in the std.math package | |
2020 if (parent.ident == Id.math && parent.parent && parent.parent.ident == Id.std && !parent.parent.parent) | |
2021 { | |
2022 //printf("deco = %s\n", type.deco); | |
2023 if (type.deco == FeZe) | |
2024 { | |
2025 if (ident == Id.sin) | |
2026 builtin = BUILTIN.BUILTINsin; | |
2027 else if (ident == Id.cos) | |
2028 builtin = BUILTIN.BUILTINcos; | |
2029 else if (ident == Id.tan) | |
2030 builtin = BUILTIN.BUILTINtan; | |
2031 else if (ident == Id._sqrt) | |
2032 builtin = BUILTIN.BUILTINsqrt; | |
2033 else if (ident == Id.fabs) | |
2034 builtin = BUILTIN.BUILTINfabs; | |
2035 //printf("builtin = %d\n", builtin); | |
2036 } | |
2037 // if float or double versions | |
2038 else if (type.deco == "FNaNbdZd" || type.deco == "FNaNbfZf") | |
2039 { | |
2040 if (ident == Id._sqrt) | |
2041 builtin = BUILTIN.BUILTINsqrt; | |
2042 } | |
2043 } | |
2044 } | |
2045 } | |
2046 | |
2047 return builtin; | |
2048 } | |
2049 | |
2050 bool isExport() | |
2051 { | |
2052 return protection == PROT.PROTexport; | |
2053 } | |
2054 | |
2055 bool isImportedSymbol() | |
2056 { | |
2057 //printf("isImportedSymbol()\n"); | |
2058 //printf("protection = %d\n", protection); | |
2059 return (protection == PROT.PROTexport) && !fbody; | |
2060 } | |
2061 | |
2062 bool isAbstract() | |
2063 { | |
2064 return (storage_class & STC.STCabstract) != 0; | |
2065 } | |
2066 | |
2067 bool isCodeseg() | |
2068 { | |
68
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
63
diff
changeset
|
2069 return true; // functions are always in the code segment |
0 | 2070 } |
2071 | |
2072 bool isOverloadable() | |
2073 { | |
63 | 2074 return 1; // functions can be overloaded |
0 | 2075 } |
2076 | |
2077 bool isPure() | |
2078 { | |
2079 //printf("FuncDeclaration::isPure() '%s'\n", toChars()); | |
2080 assert(type.ty == TY.Tfunction); | |
2081 return (cast(TypeFunction)this.type).ispure; | |
2082 } | |
2083 | |
2084 bool isNested() | |
2085 { | |
2086 //if (!toParent()) | |
2087 //printf("FuncDeclaration.isNested('%s') parent=%p\n", toChars(), parent); | |
2088 //printf("\ttoParent2() = '%s'\n", toParent2().toChars()); | |
2089 return ((storage_class & STC.STCstatic) == 0) && | |
2090 (toParent2().isFuncDeclaration() !is null); | |
2091 } | |
2092 | |
2093 bool needThis() | |
2094 { | |
2095 //printf("FuncDeclaration.needThis() '%s'\n", toChars()); | |
2096 bool needThis = isThis() !is null; | |
2097 | |
2098 //printf("\t%d\n", i); | |
2099 if (!needThis) { | |
2100 if (auto fa = isFuncAliasDeclaration()) { | |
2101 needThis = fa.funcalias.needThis(); | |
2102 } | |
2103 } | |
2104 | |
2105 return needThis; | |
2106 } | |
2107 | |
2108 bool isVirtual() | |
2109 { | |
2110 static if (false) { | |
2111 printf("FuncDeclaration.isVirtual(%s)\n", toChars()); | |
2112 printf("isMember:%p isStatic:%d private:%d ctor:%d !Dlinkage:%d\n", isMember(), isStatic(), protection == PROT.PROTprivate, isCtorDeclaration(), linkage != LINK.LINKd); | |
2113 printf("result is %d\n", | |
2114 isMember() && !(isStatic() || protection == PROT.PROTprivate || protection == PROT.PROTpackage) && toParent().isClassDeclaration()); | |
2115 } | |
2116 return isMember() && !(isStatic() || protection == PROT.PROTprivate || protection == PROT.PROTpackage) && toParent().isClassDeclaration(); | |
2117 } | |
2118 | |
2119 int isFinal() | |
2120 { | |
2121 ClassDeclaration cd; | |
2122 static if (false) { | |
2123 printf("FuncDeclaration.isFinal(%s)\n", toChars()); | |
2124 printf("%p %d %d %d %d\n", isMember(), isStatic(), protection == PROT.PROTprivate, isCtorDeclaration(), linkage != LINK.LINKd); | |
2125 printf("result is %d\n", | |
2126 isMember() && !(isStatic() || protection == PROT.PROTprivate || protection == PROT.PROTpackage) && (cd = toParent().isClassDeclaration()) !is null && cd.storage_class & STC.STCfinal); | |
2127 } | |
2128 return isMember() && (Declaration.isFinal() || ((cd = toParent().isClassDeclaration()) !is null && cd.storage_class & STC.STCfinal)); | |
2129 } | |
2130 | |
2131 bool addPreInvariant() | |
2132 { | |
2133 AggregateDeclaration ad = isThis(); | |
2134 return (ad && | |
2135 //ad.isClassDeclaration() && | |
2136 global.params.useInvariants && | |
2137 (protection == PROT.PROTpublic || protection == PROT.PROTexport) && | |
2138 !naked && | |
2139 ident !is Id.cpctor); | |
2140 } | |
2141 | |
2142 bool addPostInvariant() | |
2143 { | |
2144 AggregateDeclaration ad = isThis(); | |
2145 return (ad && ad.inv && | |
2146 //ad.isClassDeclaration() && | |
2147 global.params.useInvariants && | |
2148 (protection == PROT.PROTpublic || protection == PROT.PROTexport) && | |
2149 !naked && | |
2150 ident !is Id.cpctor); | |
2151 } | |
2152 | |
63 | 2153 /************************************* |
2154 * Attempt to interpret a function given the arguments. | |
2155 * Input: | |
2156 * istate state for calling function (null if none) | |
2157 * arguments function arguments | |
2158 * thisarg 'this', if a needThis() function, null if not. | |
2159 * | |
2160 * Return result expression if successful, null if not. | |
2161 */ | |
2162 Expression interpret(InterState istate, Expressions arguments, Expression thisarg = null) | |
0 | 2163 { |
63 | 2164 version (LOG) { |
2165 printf("\n********\nFuncDeclaration.interpret(istate = %p) %s\n", istate, toChars()); | |
2166 printf("cantInterpret = %d, semanticRun = %d\n", cantInterpret, semanticRun); | |
2167 } | |
2168 if (global.errors) | |
2169 return null; | |
2170 if (ident == Id.aaLen) | |
2171 return interpret_aaLen(istate, arguments); | |
2172 else if (ident == Id.aaKeys) | |
2173 return interpret_aaKeys(istate, arguments); | |
2174 else if (ident == Id.aaValues) | |
2175 return interpret_aaValues(istate, arguments); | |
2176 | |
2177 if (cantInterpret || semanticRun == 3) | |
2178 return null; | |
2179 | |
2180 if (!fbody) | |
2181 { | |
2182 cantInterpret = 1; | |
2183 return null; | |
2184 } | |
2185 | |
2186 if (semanticRun < 3 && scope_) | |
2187 { | |
2188 semantic3(scope_); | |
2189 if (global.errors) // if errors compiling this function | |
2190 return null; | |
2191 } | |
2192 if (semanticRun < 4) | |
2193 return null; | |
2194 | |
2195 Type tb = type.toBasetype(); | |
2196 assert(tb.ty == Tfunction); | |
2197 TypeFunction tf = cast(TypeFunction)tb; | |
2198 Type tret = tf.next.toBasetype(); | |
2199 if (tf.varargs) | |
2200 { | |
2201 cantInterpret = 1; | |
2202 error("Variadic functions are not yet implemented in CTFE"); | |
2203 return null; | |
2204 } | |
2205 | |
2206 // Ensure there are no lazy parameters | |
2207 if (tf.parameters) | |
2208 { | |
2209 size_t dim = Argument.dim(tf.parameters); | |
2210 for (size_t i = 0; i < dim; i++) | |
2211 { | |
2212 Argument arg = Argument.getNth(tf.parameters, i); | |
2213 if (arg.storageClass & STClazy) | |
2214 { | |
2215 cantInterpret = 1; | |
2216 return null; | |
2217 } | |
2218 } | |
2219 } | |
2220 | |
2221 scope InterState istatex = new InterState(); | |
2222 istatex.caller = istate; | |
2223 istatex.fd = this; | |
2224 istatex.localThis = thisarg; | |
2225 | |
2226 scope Expressions vsave = new Expressions(); // place to save previous parameter values | |
2227 size_t dim = 0; | |
2228 if (needThis() && !thisarg) | |
2229 { | |
2230 cantInterpret = 1; | |
2231 // error, no this. Prevent segfault. | |
2232 error("need 'this' to access member %s", toChars()); | |
2233 return null; | |
2234 } | |
2235 if (arguments) | |
2236 { | |
2237 dim = arguments.dim; | |
2238 assert(!dim || (parameters && (parameters.dim == dim))); | |
2239 vsave.setDim(dim); | |
2240 | |
2241 /* Evaluate all the arguments to the function, | |
2242 * store the results in eargs[] | |
2243 */ | |
2244 scope Expressions eargs = new Expressions(); | |
2245 eargs.setDim(dim); | |
2246 | |
2247 for (size_t i = 0; i < dim; i++) | |
2248 { | |
2249 Expression earg = cast(Expression)arguments.data[i]; | |
2250 Argument arg = Argument.getNth(tf.parameters, i); | |
2251 | |
2252 if (arg.storageClass & (STCout | STCref)) | |
2253 { | |
2254 } | |
2255 else | |
2256 { /* Value parameters | |
2257 */ | |
2258 Type ta = arg.type.toBasetype(); | |
2259 if (ta.ty == Tsarray && earg.op == TOKaddress) | |
2260 { | |
2261 /* Static arrays are passed by a simple pointer. | |
2262 * Skip past this to get at the actual arg. | |
2263 */ | |
2264 earg = (cast(AddrExp)earg).e1; | |
2265 } | |
2266 earg = earg.interpret(istate ? istate : istatex); | |
2267 if (earg is EXP_CANT_INTERPRET) | |
2268 { | |
2269 cantInterpret = 1; | |
2270 return null; | |
2271 } | |
2272 } | |
2273 eargs.data[i] = cast(void*)earg; | |
2274 } | |
2275 | |
2276 for (size_t i = 0; i < dim; i++) | |
2277 { | |
2278 Expression earg = cast(Expression)eargs.data[i]; | |
2279 Argument arg = Argument.getNth(tf.parameters, i); | |
2280 VarDeclaration v = cast(VarDeclaration)parameters.data[i]; | |
2281 vsave.data[i] = cast(void*)v.value; | |
2282 version (LOG) { | |
2283 printf("arg[%d] = %s\n", i, earg.toChars()); | |
2284 } | |
2285 if (arg.storageClass & (STCout | STCref) && earg.op==TOKvar) | |
2286 { | |
2287 /* Bind out or ref parameter to the corresponding | |
2288 * variable v2 | |
2289 */ | |
2290 if (!istate) | |
2291 { | |
2292 cantInterpret = 1; | |
2293 error("%s cannot be by passed by reference at compile time", earg.toChars()); | |
2294 return null; // can't bind to non-interpreted vars | |
2295 } | |
2296 // We need to chase down all of the the passed parameters until | |
2297 // we find something that isn't a TOKvar, then create a variable | |
2298 // containg that expression. | |
2299 VarDeclaration v2; | |
2300 while (1) | |
2301 { | |
2302 VarExp ve = cast(VarExp)earg; | |
2303 v2 = ve.var.isVarDeclaration(); | |
2304 if (!v2) | |
2305 { | |
2306 cantInterpret = 1; | |
2307 return null; | |
2308 } | |
2309 if (!v2.value || v2.value.op != TOKvar) | |
2310 break; | |
2311 if ((cast(VarExp)v2.value).var.isSymbolDeclaration()) | |
2312 { | |
2313 // This can happen if v is a struct initialized to | |
2314 // 0 using an __initZ SymbolDeclaration from | |
2315 // TypeStruct.defaultInit() | |
2316 break; // eg default-initialized variable | |
2317 } | |
2318 earg = v2.value; | |
2319 } | |
2320 | |
2321 v.value = new VarExp(earg.loc, v2); | |
2322 | |
2323 /* Don't restore the value of v2 upon function return | |
2324 */ | |
2325 assert(istate); | |
2326 for (size_t j = 0; j < istate.vars.dim; j++) | |
2327 { | |
2328 VarDeclaration vd = cast(VarDeclaration)istate.vars.data[j]; | |
2329 if (vd == v2) | |
2330 { | |
2331 istate.vars.data[j] = null; | |
2332 break; | |
2333 } | |
2334 } | |
2335 } | |
2336 else | |
2337 { | |
2338 // Value parameters and non-trivial references | |
2339 v.value = earg; | |
2340 } | |
2341 version (LOG) { | |
2342 printf("interpreted arg[%d] = %s\n", i, earg.toChars()); | |
2343 } | |
2344 } | |
2345 } | |
2346 // Don't restore the value of 'this' upon function return | |
2347 if (needThis() && thisarg.op==TOKvar) { | |
2348 VarDeclaration thisvar = (cast(VarExp)thisarg).var.isVarDeclaration(); | |
2349 for (size_t i = 0; i < istate.vars.dim; i++) | |
2350 { | |
2351 VarDeclaration v = cast(VarDeclaration)istate.vars.data[i]; | |
2352 if (v == thisvar) | |
2353 { | |
2354 istate.vars.data[i] = null; | |
2355 break; | |
2356 } | |
2357 } | |
2358 } | |
2359 | |
2360 /* Save the values of the local variables used | |
2361 */ | |
2362 scope Expressions valueSaves = new Expressions(); | |
2363 if (istate && !isNested()) | |
2364 { | |
2365 //printf("saving local variables...\n"); | |
2366 valueSaves.setDim(istate.vars.dim); | |
2367 for (size_t i = 0; i < istate.vars.dim; i++) | |
2368 { | |
2369 VarDeclaration v = cast(VarDeclaration)istate.vars.data[i]; | |
2370 if (v) | |
2371 { | |
2372 //printf("\tsaving [%d] %s = %s\n", i, v.toChars(), v.value ? v.value.toChars() : ""); | |
2373 valueSaves.data[i] = cast(void*)v.value; | |
2374 v.value = null; | |
2375 } | |
2376 } | |
2377 } | |
2378 | |
2379 Expression e = null; | |
2380 while (1) | |
2381 { | |
2382 e = fbody.interpret(istatex); | |
2383 if (e is EXP_CANT_INTERPRET) | |
2384 { | |
2385 version (LOG) { | |
2386 printf("function body failed to interpret\n"); | |
2387 } | |
2388 e = null; | |
2389 } | |
2390 | |
2391 /* This is how we deal with a recursive statement AST | |
2392 * that has arbitrary goto statements in it. | |
2393 * Bubble up a 'result' which is the target of the goto | |
2394 * statement, then go recursively down the AST looking | |
2395 * for that statement, then execute starting there. | |
2396 */ | |
2397 if (e is EXP_GOTO_INTERPRET) | |
2398 { | |
2399 istatex.start = istatex.gotoTarget; // set starting statement | |
2400 istatex.gotoTarget = null; | |
2401 } | |
2402 else | |
2403 break; | |
2404 } | |
2405 /* Restore the parameter values | |
2406 */ | |
2407 for (size_t i = 0; i < dim; i++) | |
2408 { | |
2409 VarDeclaration v = cast(VarDeclaration)parameters.data[i]; | |
2410 v.value = cast(Expression)vsave.data[i]; | |
2411 } | |
2412 | |
2413 if (istate && !isNested()) | |
2414 { | |
2415 /* Restore the variable values | |
2416 */ | |
2417 //printf("restoring local variables...\n"); | |
2418 for (size_t i = 0; i < istate.vars.dim; i++) | |
2419 { | |
2420 VarDeclaration v = cast(VarDeclaration)istate.vars.data[i]; | |
2421 if (v) | |
2422 { | |
2423 v.value = cast(Expression)valueSaves.data[i]; | |
2424 //printf("\trestoring [%d] %s = %s\n", i, v.toChars(), v.value ? v.value.toChars() : ""); | |
2425 } | |
2426 } | |
2427 } | |
2428 return e; | |
0 | 2429 } |
2430 | |
2431 void inlineScan() | |
2432 { | |
2433 InlineScanState iss; | |
2434 | |
2435 version (LOG) { | |
2436 printf("FuncDeclaration.inlineScan('%s')\n", toChars()); | |
2437 } | |
2438 ///memset(&iss, 0, sizeof(iss)); | |
2439 iss.fd = this; | |
2440 if (fbody) | |
2441 { | |
2442 inlineNest++; | |
2443 fbody = fbody.inlineScan(&iss); | |
2444 inlineNest--; | |
2445 } | |
2446 } | |
2447 | |
2448 int canInline(int hasthis, int hdrscan = 0) | |
2449 { | |
2450 int cost; | |
2451 | |
2452 // #define CANINLINE_LOG 0 | |
2453 | |
2454 version (CANINLINE_LOG) { | |
2455 printf("FuncDeclaration.canInline(hasthis = %d, '%s')\n", hasthis, toChars()); | |
2456 } | |
2457 | |
2458 if (needThis() && !hasthis) | |
2459 return 0; | |
2460 | |
2461 if (inlineNest || (semanticRun < 3 && !hdrscan)) | |
2462 { | |
2463 version (CANINLINE_LOG) { | |
2464 printf("\t1: no, inlineNest = %d, semanticRun = %d\n", inlineNest, semanticRun); | |
2465 } | |
2466 return 0; | |
2467 } | |
2468 | |
2469 switch (inlineStatus) | |
2470 { | |
2471 case ILS.ILSyes: | |
2472 version (CANINLINE_LOG) { | |
2473 printf("\t1: yes %s\n", toChars()); | |
2474 } | |
2475 return 1; | |
2476 | |
2477 case ILS.ILSno: | |
2478 version (CANINLINE_LOG) { | |
2479 printf("\t1: no %s\n", toChars()); | |
2480 } | |
2481 return 0; | |
2482 | |
2483 case ILS.ILSuninitialized: | |
2484 break; | |
2485 | |
2486 default: | |
2487 assert(0); | |
2488 } | |
2489 | |
2490 if (type) | |
2491 { | |
2492 assert(type.ty == Tfunction); | |
2493 TypeFunction tf = cast(TypeFunction)type; | |
2494 if (tf.varargs == 1) // no variadic parameter lists | |
2495 goto Lno; | |
2496 | |
2497 /* Don't inline a function that returns non-void, but has | |
2498 * no return expression. | |
2499 */ | |
2500 if (tf.next && tf.next.ty != Tvoid && | |
2501 !(hasReturnExp & 1) && | |
2502 !hdrscan) | |
2503 goto Lno; | |
2504 } | |
2505 else | |
2506 { | |
2507 CtorDeclaration ctor = isCtorDeclaration(); | |
2508 if (ctor && ctor.varargs == 1) | |
2509 goto Lno; | |
2510 } | |
2511 | |
2512 if ( | |
2513 !fbody || | |
2514 !hdrscan && | |
2515 ( | |
2516 /// static if (false) { | |
2517 /// isCtorDeclaration() || // cannot because need to convert: | |
2518 /// // return; | |
2519 /// // to: | |
2520 /// // return this; | |
2521 /// } | |
2522 isSynchronized() || | |
2523 isImportedSymbol() || | |
2524 /// version (DMDV2) { | |
2525 closureVars.dim || // no nested references to this frame | |
2526 /// } else { | |
2527 /// nestedFrameRef || // no nested references to this frame | |
2528 /// } | |
2529 (isVirtual() && !isFinal()) | |
2530 )) | |
2531 { | |
2532 goto Lno; | |
2533 } | |
2534 | |
2535 /* If any parameters are Tsarray's (which are passed by reference) | |
2536 * or out parameters (also passed by reference), don't do inlining. | |
2537 */ | |
2538 if (parameters) | |
2539 { | |
2540 for (int i = 0; i < parameters.dim; i++) | |
2541 { | |
2542 VarDeclaration v = cast(VarDeclaration)parameters.data[i]; | |
2543 if (v.isOut() || v.isRef() || v.type.toBasetype().ty == Tsarray) | |
2544 goto Lno; | |
2545 } | |
2546 } | |
2547 | |
2548 InlineCostState ics; | |
2549 ///memset(&ics, 0, sizeof(ics)); | |
2550 ics.hasthis = hasthis; | |
2551 ics.fd = this; | |
2552 ics.hdrscan = hdrscan; | |
2553 cost = fbody.inlineCost(&ics); | |
2554 version (CANINLINE_LOG) { | |
2555 printf("cost = %d\n", cost); | |
2556 } | |
2557 if (cost >= COST_MAX) | |
2558 goto Lno; | |
2559 | |
2560 if (!hdrscan) // Don't scan recursively for header content scan | |
2561 inlineScan(); | |
2562 | |
2563 Lyes: | |
2564 if (!hdrscan) // Don't modify inlineStatus for header content scan | |
2565 inlineStatus = ILS.ILSyes; | |
2566 version (CANINLINE_LOG) { | |
2567 printf("\t2: yes %s\n", toChars()); | |
2568 } | |
2569 return 1; | |
2570 | |
2571 Lno: | |
2572 if (!hdrscan) // Don't modify inlineStatus for header content scan | |
2573 inlineStatus = ILS.ILSno; | |
2574 version (CANINLINE_LOG) { | |
2575 printf("\t2: no %s\n", toChars()); | |
2576 } | |
2577 return 0; | |
2578 } | |
2579 | |
2580 Expression doInline(InlineScanState* iss, Expression ethis, Array arguments) | |
2581 { | |
2582 InlineDoState ids = new InlineDoState(); | |
2583 DeclarationExp de; | |
2584 Expression e = null; | |
2585 | |
2586 version (LOG) { | |
2587 printf("FuncDeclaration.doInline('%s')\n", toChars()); | |
2588 } | |
2589 | |
2590 ///memset(&ids, 0, sizeof(ids)); | |
2591 ids.parent = iss.fd; | |
2592 | |
2593 // Set up vthis | |
2594 if (ethis) | |
2595 { | |
2596 VarDeclaration vthis; | |
2597 ExpInitializer ei; | |
2598 VarExp ve; | |
2599 | |
2600 version (STRUCTTHISREF) { | |
2601 if (ethis.type.ty == Tpointer) | |
2602 { | |
2603 Type t = ethis.type.nextOf(); | |
2604 ethis = new PtrExp(ethis.loc, ethis); | |
2605 ethis.type = t; | |
2606 } | |
2607 ei = new ExpInitializer(ethis.loc, ethis); | |
2608 | |
2609 vthis = new VarDeclaration(ethis.loc, ethis.type, Id.This, ei); | |
2610 if (ethis.type.ty != Tclass) | |
2611 vthis.storage_class = STCref; | |
2612 else | |
2613 vthis.storage_class = STCin; | |
2614 } else { | |
2615 if (ethis.type.ty != Tclass && ethis.type.ty != Tpointer) | |
2616 { | |
2617 ethis = ethis.addressOf(null); | |
2618 } | |
2619 | |
2620 ei = new ExpInitializer(ethis.loc, ethis); | |
2621 | |
2622 vthis = new VarDeclaration(ethis.loc, ethis.type, Id.This, ei); | |
2623 vthis.storage_class = STCin; | |
2624 } | |
2625 vthis.linkage = LINKd; | |
2626 vthis.parent = iss.fd; | |
2627 | |
2628 ve = new VarExp(vthis.loc, vthis); | |
2629 ve.type = vthis.type; | |
2630 | |
2631 ei.exp = new AssignExp(vthis.loc, ve, ethis); | |
2632 ei.exp.type = ve.type; | |
2633 version (STRUCTTHISREF) { | |
2634 if (ethis.type.ty != Tclass) | |
2635 { | |
2636 /* This is a reference initialization, not a simple assignment. | |
2637 */ | |
2638 ei.exp.op = TOKconstruct; | |
2639 } | |
2640 } | |
2641 | |
2642 ids.vthis = vthis; | |
2643 } | |
2644 | |
2645 // Set up parameters | |
2646 if (ethis) | |
2647 { | |
2648 e = new DeclarationExp(Loc(0), ids.vthis); | |
2649 e.type = Type.tvoid; | |
2650 } | |
2651 | |
2652 if (arguments && arguments.dim) | |
2653 { | |
2654 assert(parameters.dim == arguments.dim); | |
2655 | |
2656 for (int i = 0; i < arguments.dim; i++) | |
2657 { | |
2658 VarDeclaration vfrom = cast(VarDeclaration)parameters.data[i]; | |
2659 VarDeclaration vto; | |
2660 Expression arg = cast(Expression)arguments.data[i]; | |
2661 ExpInitializer ei; | |
2662 VarExp ve; | |
2663 | |
2664 ei = new ExpInitializer(arg.loc, arg); | |
2665 | |
2666 vto = new VarDeclaration(vfrom.loc, vfrom.type, vfrom.ident, ei); | |
2667 vto.storage_class |= vfrom.storage_class & (STCin | STCout | STClazy | STCref); | |
2668 vto.linkage = vfrom.linkage; | |
2669 vto.parent = iss.fd; | |
2670 //printf("vto = '%s', vto.storage_class = x%x\n", vto.toChars(), vto.storage_class); | |
2671 //printf("vto.parent = '%s'\n", iss.fd.toChars()); | |
2672 | |
2673 ve = new VarExp(vto.loc, vto); | |
2674 //ve.type = vto.type; | |
2675 ve.type = arg.type; | |
2676 | |
2677 ei.exp = new AssignExp(vto.loc, ve, arg); | |
2678 ei.exp.type = ve.type; | |
2679 //ve.type.print(); | |
2680 //arg.type.print(); | |
2681 //ei.exp.print(); | |
2682 | |
2683 ids.from.push(cast(void*)vfrom); | |
2684 ids.to.push(cast(void*)vto); | |
2685 | |
2686 de = new DeclarationExp(Loc(0), vto); | |
2687 de.type = Type.tvoid; | |
2688 | |
2689 e = Expression.combine(e, de); | |
2690 } | |
2691 } | |
2692 | |
2693 inlineNest++; | |
2694 Expression eb = fbody.doInline(ids); | |
2695 inlineNest--; | |
2696 //eb.type.print(); | |
2697 //eb.print(); | |
2698 //eb.dump(0); | |
2699 return Expression.combine(e, eb); | |
2700 } | |
2701 | |
2702 string kind() | |
2703 { | |
2704 return "function"; | |
2705 } | |
2706 | |
2707 void toDocBuffer(OutBuffer buf) | |
2708 { | |
2709 assert(false); | |
2710 } | |
2711 | |
2712 FuncDeclaration isUnique() | |
2713 { | |
2714 assert(false); | |
2715 } | |
2716 | |
2717 /******************************* | |
2718 * Look at all the variables in this function that are referenced | |
2719 * by nested functions, and determine if a closure needs to be | |
2720 * created for them. | |
2721 */ | |
2722 bool needsClosure() | |
2723 { | |
2724 /* Need a closure for all the closureVars[] if any of the | |
2725 * closureVars[] are accessed by a | |
2726 * function that escapes the scope of this function. | |
2727 * We take the conservative approach and decide that any function that: | |
2728 * 1) is a virtual function | |
2729 * 2) has its address taken | |
2730 * 3) has a parent that escapes | |
2731 * | |
2732 * Note that since a non-virtual function can be called by | |
2733 * a virtual one, if that non-virtual function accesses a closure | |
2734 * var, the closure still has to be taken. Hence, we check for isThis() | |
2735 * instead of isVirtual(). (thanks to David Friedman) | |
2736 */ | |
2737 | |
2738 //printf("FuncDeclaration.needsClosure() %s\n", toChars()); | |
2739 for (int i = 0; i < closureVars.dim; i++) | |
2740 { | |
2741 VarDeclaration v = cast(VarDeclaration)closureVars.data[i]; | |
2742 assert(v.isVarDeclaration()); | |
2743 //printf("\tv = %s\n", v.toChars()); | |
2744 | |
2745 for (int j = 0; j < v.nestedrefs.dim; j++) | |
2746 { FuncDeclaration f = cast(FuncDeclaration)v.nestedrefs.data[j]; | |
2747 assert(f != this); | |
2748 | |
2749 //printf("\t\tf = %s, %d, %p, %d\n", f.toChars(), f.isVirtual(), f.isThis(), f.tookAddressOf); | |
2750 if (f.isThis() || f.tookAddressOf) | |
2751 goto Lyes; // assume f escapes this function's scope | |
2752 | |
2753 // Look to see if any parents of f that are below this escape | |
2754 for (Dsymbol s = f.parent; s && s !is this; s = s.parent) | |
2755 { | |
2756 f = s.isFuncDeclaration(); | |
2757 if (f && (f.isThis() || f.tookAddressOf)) { | |
2758 goto Lyes; | |
2759 } | |
2760 } | |
2761 } | |
2762 } | |
2763 return false; | |
2764 | |
2765 Lyes: | |
2766 //printf("\tneeds closure\n"); | |
2767 return true; | |
2768 } | |
2769 | |
2770 static FuncDeclaration genCfunc(Type treturn, string name) | |
2771 { | |
2772 return genCfunc(treturn, Lexer.idPool(name)); | |
2773 } | |
2774 | |
2775 /********************************** | |
2776 * Generate a FuncDeclaration for a runtime library function. | |
2777 */ | |
2778 static FuncDeclaration genCfunc(Type treturn, Identifier id) | |
2779 { | |
2780 FuncDeclaration fd; | |
2781 TypeFunction tf; | |
2782 Dsymbol s; | |
2783 static DsymbolTable st = null; | |
2784 | |
2785 //printf("genCfunc(name = '%s')\n", id.toChars()); | |
2786 //printf("treturn\n\t"); treturn.print(); | |
2787 | |
2788 // See if already in table | |
2789 if (!st) | |
2790 st = new DsymbolTable(); | |
2791 | |
2792 s = st.lookup(id); | |
2793 if (s) | |
2794 { | |
2795 fd = s.isFuncDeclaration(); | |
2796 assert(fd); | |
2797 assert(fd.type.nextOf().equals(treturn)); | |
2798 } | |
2799 else | |
2800 { | |
2801 tf = new TypeFunction(null, treturn, 0, LINK.LINKc); | |
2802 fd = new FuncDeclaration(Loc(0), Loc(0), id, STCstatic, tf); | |
2803 fd.protection = PROT.PROTpublic; | |
2804 fd.linkage = LINK.LINKc; | |
2805 | |
2806 st.insert(fd); | |
2807 } | |
2808 return fd; | |
2809 } | |
2810 | |
2811 Symbol* toSymbol() | |
2812 { | |
2813 if (!csym) | |
2814 { | |
2815 Symbol* s; | |
2816 TYPE* t; | |
2817 string id; | |
2818 | |
2819 static if (false) { | |
2820 id = ident.toChars(); | |
2821 } else { | |
2822 id = mangle(); | |
2823 } | |
2824 //writef("FuncDeclaration.toSymbol(%s %s)\n", kind(), toChars()); | |
2825 //writef("\tid = '%s'\n", id); | |
2826 //writef("\ttype = %s\n", type.toChars()); | |
2827 s = symbol_calloc(toStringz(id)); | |
2828 slist_add(s); | |
2829 | |
2830 { | |
2831 s.prettyIdent = toStringz(toPrettyChars()); | |
2832 s.Sclass = SC.SCglobal; | |
2833 symbol_func(s); | |
2834 func_t* f = s.Sfunc; | |
2835 if (isVirtual()) | |
2836 f.Fflags |= F.Fvirtual; | |
2837 else if (isMember2()) | |
2838 f.Fflags |= F.Fstatic; | |
2839 f.Fstartline.Slinnum = loc.linnum; | |
2840 f.Fstartline.Sfilename = cast(char*)toStringz(loc.filename); | |
2841 if (endloc.linnum) | |
2842 { | |
2843 f.Fendline.Slinnum = endloc.linnum; | |
2844 f.Fendline.Sfilename = cast(char*)toStringz(endloc.filename); | |
2845 } | |
2846 else | |
2847 { | |
2848 f.Fendline.Slinnum = loc.linnum; | |
2849 f.Fendline.Sfilename = cast(char*)toStringz(loc.filename); | |
2850 } | |
2851 t = type.toCtype(); | |
2852 } | |
2853 | |
2854 mangle_t msave = t.Tmangle; | |
2855 if (isMain()) | |
2856 { | |
2857 t.Tty = TYM.TYnfunc; | |
2858 t.Tmangle = mTYman.mTYman_c; | |
2859 } | |
2860 else | |
2861 { | |
2862 switch (linkage) | |
2863 { | |
2864 case LINK.LINKwindows: | |
2865 t.Tmangle = mTYman.mTYman_std; | |
2866 break; | |
2867 | |
2868 case LINK.LINKpascal: | |
2869 t.Tty = TYM.TYnpfunc; | |
2870 t.Tmangle = mTYman.mTYman_pas; | |
2871 break; | |
2872 | |
2873 case LINK.LINKc: | |
2874 t.Tmangle = mTYman.mTYman_c; | |
2875 break; | |
2876 | |
2877 case LINK.LINKd: | |
2878 t.Tmangle = mTYman.mTYman_d; | |
2879 break; | |
2880 | |
2881 case LINK.LINKcpp: | |
2882 { t.Tmangle = mTYman.mTYman_cpp; | |
2883 version (TARGET_WINDOS) { | |
2884 if (isThis()) | |
2885 t.Tty = TYM.TYmfunc; | |
2886 } | |
2887 s.Sflags |= SFL.SFLpublic; | |
2888 Dsymbol parent = toParent(); | |
2889 ClassDeclaration cd = parent.isClassDeclaration(); | |
2890 if (cd) | |
2891 { | |
2892 .type* tt = cd.type.toCtype(); | |
2893 s.Sscope = tt.Tnext.Ttag; | |
2894 } | |
2895 break; | |
2896 } | |
2897 default: | |
2898 writef("linkage = %d\n", linkage); | |
2899 assert(0); | |
2900 } | |
2901 } | |
2902 if (msave) | |
2903 assert(msave == t.Tmangle); | |
2904 //printf("Tty = %x, mangle = x%x\n", t.Tty, t.Tmangle); | |
2905 t.Tcount++; | |
2906 s.Stype = t; | |
2907 //s.Sfielddef = this; | |
2908 | |
2909 csym = s; | |
2910 } | |
2911 return csym; | |
2912 } | |
2913 | |
2914 Symbol* toThunkSymbol(int offset) // thunk version | |
2915 { | |
2916 Symbol *sthunk; | |
2917 | |
2918 toSymbol(); | |
2919 | |
2920 static if (false) { | |
2921 char *id; | |
2922 char *n; | |
2923 type *t; | |
2924 | |
2925 n = sym.Sident; | |
63 | 2926 version (Bug4054) { |
2927 id = cast(char*) GC.malloc(8 + 5 + strlen(n) + 1); | |
2928 } else { | |
2929 id = cast(char*) alloca(8 + 5 + strlen(n) + 1); | |
2930 } | |
0 | 2931 sprintf(id, "_thunk%d__%s", offset, n); |
2932 s = symbol_calloc(id); | |
2933 slist_add(s); | |
2934 s.Stype = csym.Stype; | |
2935 s.Stype.Tcount++; | |
2936 } | |
2937 sthunk = symbol_generate(SCstatic, csym.Stype); | |
2938 sthunk.Sflags |= SFLimplem; | |
2939 cod3_thunk(sthunk, csym, 0, TYnptr, -offset, -1, 0); | |
2940 return sthunk; | |
2941 } | |
2942 | |
2943 void toObjFile(int multiobj) // compile to .obj file | |
2944 { | |
2945 Symbol* s; | |
2946 func_t* f; | |
2947 Symbol* senter; | |
2948 Symbol* sexit; | |
2949 | |
2950 FuncDeclaration func = this; | |
2951 ClassDeclaration cd = func.parent.isClassDeclaration(); | |
2952 int reverse; | |
2953 int i; | |
2954 int has_arguments; | |
2955 | |
2956 //printf("FuncDeclaration.toObjFile(%p, %s.%s)\n", func, parent.toChars(), func.toChars()); | |
2957 static if (false) { | |
2958 //printf("line = %d\n",func.getWhere() / LINEINC); | |
2959 EEcontext ee = env.getEEcontext(); | |
2960 if (ee.EEcompile == 2) | |
2961 { | |
2962 if (ee.EElinnum < (func.getWhere() / LINEINC) || | |
2963 ee.EElinnum > (func.endwhere / LINEINC) | |
2964 ) | |
2965 return; // don't compile this function | |
2966 ee.EEfunc = func.toSymbol(); | |
2967 } | |
2968 } | |
2969 | |
2970 if (multiobj && !isStaticDtorDeclaration() && !isStaticCtorDeclaration()) | |
2971 { | |
2972 obj_append(this); | |
2973 return; | |
2974 } | |
2975 | |
2976 if (semanticRun >= 5) // if toObjFile() already run | |
2977 return; | |
2978 | |
2979 semanticRun = 5; | |
2980 | |
2981 if (!func.fbody) | |
2982 { | |
2983 return; | |
2984 } | |
2985 | |
2986 if (func.isUnitTestDeclaration() && !global.params.useUnitTests) | |
2987 return; | |
2988 | |
2989 if (global.params.verbose) | |
2990 writef("function %s\n",func.toChars()); | |
2991 | |
2992 s = func.toSymbol(); | |
2993 f = s.Sfunc; | |
2994 | |
2995 version (TARGET_OSX) { | |
2996 s.Sclass = SC.SCcomdat; | |
2997 } else { | |
2998 s.Sclass = SC.SCglobal; | |
2999 } | |
3000 | |
3001 for (Dsymbol p = parent; p; p = p.parent) | |
3002 { | |
3003 if (p.isTemplateInstance()) | |
3004 { | |
3005 s.Sclass = SC.SCcomdat; | |
3006 break; | |
3007 } | |
3008 } | |
3009 | |
3010 if (isNested()) | |
3011 { | |
3012 // if (!(config.flags3 & CFG3pic)) | |
3013 // s.Sclass = SCstatic; | |
3014 f.Fflags3 |= F3.Fnested; | |
3015 } | |
3016 else | |
3017 { | |
3018 const(char)* libname = (global.params.symdebug) ? global.params.debuglibname : global.params.defaultlibname; | |
3019 | |
3020 // Pull in RTL startup code | |
3021 if (func.isMain()) | |
3022 { objextdef("_main"); | |
22
fd4acc376c45
Implemented object file output and linking on linux.
Robert Clipsham <robert@octarineparrot.com>
parents:
17
diff
changeset
|
3023 version (POSIX) { ///TARGET_LINUX || TARGET_OSX || TARGET_FREEBSD || TARGET_SOLARIS |
0 | 3024 obj_ehsections(); // initialize exception handling sections |
3025 } else { | |
3026 objextdef("__acrtused_con"); | |
3027 } | |
3028 obj_includelib(libname); | |
3029 s.Sclass = SC.SCglobal; | |
3030 } | |
3031 else if (strcmp(s.Sident.ptr, "main".ptr) == 0 && linkage == LINK.LINKc) | |
3032 s.Sclass = SC.SCglobal; | |
3033 | |
3034 else if (func.isWinMain()) | |
3035 { | |
3036 objextdef("__acrtused"); | |
3037 obj_includelib(libname); | |
3038 s.Sclass = SC.SCglobal; | |
3039 } | |
3040 | |
3041 // Pull in RTL startup code | |
3042 else if (func.isDllMain()) | |
3043 { | |
3044 objextdef("__acrtused_dll"); | |
3045 obj_includelib(libname); | |
3046 s.Sclass = SC.SCglobal; | |
3047 } | |
3048 } | |
3049 | |
3050 cstate.CSpsymtab = &f.Flocsym; | |
3051 | |
3052 // Find module m for this function | |
3053 Module m = null; | |
3054 for (Dsymbol p = parent; p; p = p.parent) | |
3055 { | |
3056 m = p.isModule(); | |
3057 if (m) | |
3058 break; | |
3059 } | |
3060 | |
3061 IRState irs = IRState(m, func); | |
3062 Array deferToObj = new Array(); // write these to OBJ file later | |
3063 irs.deferToObj = deferToObj; | |
3064 | |
3065 TypeFunction tf; | |
3066 RET retmethod; | |
3067 Symbol* shidden = null; | |
3068 Symbol* sthis = null; | |
3069 tym_t tyf; | |
3070 | |
3071 tyf = tybasic(s.Stype.Tty); | |
3072 //printf("linkage = %d, tyf = x%x\n", linkage, tyf); | |
3073 reverse = tyrevfunc(s.Stype.Tty); | |
3074 | |
3075 assert(func.type.ty == TY.Tfunction); | |
3076 tf = cast(TypeFunction)(func.type); | |
3077 has_arguments = (tf.linkage == LINK.LINKd) && (tf.varargs == 1); | |
3078 retmethod = tf.retStyle(); | |
3079 if (retmethod == RET.RETstack) | |
3080 { | |
3081 // If function returns a struct, put a pointer to that | |
3082 // as the first argument | |
3083 .type* thidden = tf.next.pointerTo().toCtype(); | |
3084 char hiddenparam[5+4+1]; | |
3085 static int hiddenparami; // how many we've generated so far | |
3086 | |
3087 sprintf(hiddenparam.ptr, "__HID%d".ptr, ++hiddenparami); | |
3088 shidden = symbol_name(hiddenparam.ptr, SC.SCparameter, thidden); | |
3089 shidden.Sflags |= SFL.SFLtrue | SFL.SFLfree; | |
3090 | |
3091 version (DMDV1) { | |
3092 bool nestedref = func.nrvo_can && func.nrvo_var && func.nrvo_var.nestedref; | |
3093 } else { | |
3094 bool nestedref = func.nrvo_can && func.nrvo_var && (func.nrvo_var.nestedrefs.dim != 0); | |
3095 } | |
3096 if (nestedref) { | |
3097 type_setcv(&shidden.Stype, shidden.Stype.Tty | mTY.mTYvolatile); | |
3098 } | |
3099 | |
3100 irs.shidden = shidden; | |
3101 this.shidden = shidden; | |
3102 } | |
3103 | |
3104 if (vthis) | |
3105 { | |
3106 assert(!vthis.csym); | |
3107 sthis = vthis.toSymbol(); | |
3108 irs.sthis = sthis; | |
3109 if (!(f.Fflags3 & F3.Fnested)) | |
3110 f.Fflags3 |= F3.Fmember; | |
3111 } | |
3112 | |
3113 Symbol** params; | |
3114 uint pi; | |
3115 | |
3116 // Estimate number of parameters, pi | |
3117 pi = (v_arguments !is null); | |
3118 if (parameters) | |
3119 pi += parameters.dim; | |
3120 | |
3121 // Allow extra 2 for sthis and shidden | |
14
2cc604139636
Implemented Linux support for ddmd. Some parts are a bit hacky to just "get it working", that said, druntime and phobos compile, and unittests pass.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
3122 version (Bug4054) |
2cc604139636
Implemented Linux support for ddmd. Some parts are a bit hacky to just "get it working", that said, druntime and phobos compile, and unittests pass.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
3123 params = cast(Symbol**)GC.malloc((pi + 2) * (Symbol*).sizeof); |
2cc604139636
Implemented Linux support for ddmd. Some parts are a bit hacky to just "get it working", that said, druntime and phobos compile, and unittests pass.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
3124 else |
0 | 3125 params = cast(Symbol**)alloca((pi + 2) * (Symbol*).sizeof); |
3126 | |
3127 // Get the actual number of parameters, pi, and fill in the params[] | |
3128 pi = 0; | |
3129 if (v_arguments) | |
3130 { | |
3131 params[pi] = v_arguments.toSymbol(); | |
3132 pi += 1; | |
3133 } | |
3134 if (parameters) | |
3135 { | |
3136 for (i = 0; i < parameters.dim; i++) | |
3137 { | |
3138 VarDeclaration v = cast(VarDeclaration)parameters.data[i]; | |
3139 debug { | |
3140 if (v.csym) | |
3141 writef("parameter '%s'\n", v.toChars()); | |
3142 } | |
3143 assert(!v.csym); | |
3144 params[pi + i] = v.toSymbol(); | |
3145 } | |
3146 pi += i; | |
3147 } | |
3148 | |
3149 if (reverse) | |
3150 { | |
3151 // Reverse params[] entries | |
3152 for (i = 0; i < pi/2; i++) | |
3153 { | |
3154 Symbol* sptmp = params[i]; | |
3155 params[i] = params[pi - 1 - i]; | |
3156 params[pi - 1 - i] = sptmp; | |
3157 } | |
3158 } | |
3159 | |
3160 if (shidden) | |
3161 { | |
3162 static if (false) { | |
3163 // shidden becomes last parameter | |
3164 params[pi] = shidden; | |
3165 } else { | |
3166 // shidden becomes first parameter | |
3167 memmove(params + 1, params, pi * (*params).sizeof); | |
3168 params[0] = shidden; | |
3169 } | |
3170 pi++; | |
3171 } | |
3172 | |
3173 | |
3174 if (sthis) | |
3175 { | |
3176 static if (false) { | |
3177 // sthis becomes last parameter | |
3178 params[pi] = sthis; | |
3179 } else { | |
3180 // sthis becomes first parameter | |
3181 memmove(params + 1, params, pi * (*params).sizeof); | |
3182 params[0] = sthis; | |
3183 } | |
3184 pi++; | |
3185 } | |
3186 | |
3187 if ((global.params.isLinux || global.params.isOSX || global.params.isFreeBSD || global.params.isSolaris) && | |
3188 linkage != LINK.LINKd && shidden && sthis) | |
3189 { | |
3190 /* swap shidden and sthis | |
3191 */ | |
3192 Symbol* sp = params[0]; | |
3193 params[0] = params[1]; | |
3194 params[1] = sp; | |
3195 } | |
3196 | |
3197 for (i = 0; i < pi; i++) | |
3198 { | |
3199 Symbol *sp = params[i]; | |
3200 sp.Sclass = SC.SCparameter; | |
3201 sp.Sflags &= ~SFL.SFLspill; | |
3202 sp.Sfl = FL.FLpara; | |
3203 symbol_add(sp); | |
3204 } | |
3205 | |
3206 // First parameter goes in register | |
3207 if (pi) | |
3208 { | |
3209 Symbol* sp = params[0]; | |
3210 if ((tyf == TYM.TYjfunc || tyf == TYM.TYmfunc) && type_jparam(sp.Stype)) | |
3211 { | |
3212 sp.Sclass = SC.SCfastpar; | |
3213 sp.Spreg = (tyf == TYM.TYjfunc) ? REG.AX : REG.CX; | |
3214 sp.Sfl = FL.FLauto; | |
3215 //printf("'%s' is SCfastpar\n",sp.Sident); | |
3216 } | |
3217 } | |
3218 | |
3219 if (func.fbody) | |
3220 { | |
3221 block* b; | |
3222 Blockx bx; | |
3223 Statement sbody; | |
3224 | |
3225 localgot = null; | |
3226 | |
3227 sbody = func.fbody; | |
3228 ///memset(&bx, 0, (bx).sizeof); | |
3229 bx.startblock = block_calloc(); | |
3230 bx.curblock = bx.startblock; | |
3231 bx.funcsym = s; | |
3232 bx.scope_index = -1; | |
3233 bx.classdec = cd; | |
3234 bx.member = func; | |
3235 bx.module_ = getModule(); | |
3236 irs.blx = &bx; | |
3237 | |
3238 buildClosure(&irs); | |
3239 | |
3240 static if (false) { | |
3241 if (func.isSynchronized()) | |
3242 { | |
3243 if (cd) | |
3244 { | |
3245 elem *esync; | |
3246 if (func.isStatic()) | |
3247 { // monitor is in ClassInfo | |
3248 esync = el_ptr(cd.toSymbol()); | |
3249 } | |
3250 else | |
3251 { // 'this' is the monitor | |
3252 esync = el_var(sthis); | |
3253 } | |
3254 | |
3255 if (func.isStatic() || sbody.usesEH() || | |
3256 !(config.flags2 & CFG2.CFG2seh)) | |
3257 { // BUG: what if frequire or fensure uses EH? | |
3258 | |
3259 sbody = new SynchronizedStatement(func.loc, esync, sbody); | |
3260 } | |
3261 else | |
3262 { | |
3263 version (TARGET_WINDOS) { | |
3264 if (config.flags2 & CFG2.CFG2seh) | |
3265 { | |
3266 /* The "jmonitor" uses an optimized exception handling frame | |
3267 * which is a little shorter than the more general EH frame. | |
3268 * It isn't strictly necessary. | |
3269 */ | |
3270 s.Sfunc.Fflags3 |= Fjmonitor; | |
3271 } | |
3272 } | |
3273 el_free(esync); | |
3274 } | |
3275 } | |
3276 else | |
3277 { | |
3278 error("synchronized function %s must be a member of a class", func.toChars()); | |
3279 } | |
3280 } | |
3281 } else version (TARGET_WINDOS) { | |
3282 if (func.isSynchronized() && cd && config.flags2 & CFG2.CFG2seh && | |
3283 !func.isStatic() && !sbody.usesEH()) | |
3284 { | |
3285 /* The "jmonitor" hack uses an optimized exception handling frame | |
3286 * which is a little shorter than the more general EH frame. | |
3287 */ | |
3288 s.Sfunc.Fflags3 |= F3.Fjmonitor; | |
3289 } | |
3290 } | |
3291 | |
3292 sbody.toIR(&irs); | |
3293 bx.curblock.BC = BC.BCret; | |
3294 | |
3295 f.Fstartblock = bx.startblock; | |
3296 // einit = el_combine(einit,bx.init); | |
3297 | |
3298 if (isCtorDeclaration()) | |
3299 { | |
3300 assert(sthis); | |
3301 for (b = f.Fstartblock; b; b = b.Bnext) | |
3302 { | |
3303 if (b.BC == BC.BCret) | |
3304 { | |
3305 b.BC = BC.BCretexp; | |
3306 b.Belem = el_combine(b.Belem, el_var(sthis)); | |
3307 } | |
3308 } | |
3309 } | |
3310 } | |
3311 | |
3312 // If static constructor | |
3313 if (isStaticConstructor()) | |
3314 { | |
3315 elem* e = el_una(OPER.OPucall, TYM.TYvoid, el_var(s)); | |
3316 ector = el_combine(ector, e); | |
3317 } | |
3318 | |
3319 // If static destructor | |
3320 if (isStaticDestructor()) | |
3321 { | |
3322 elem* e; | |
3323 | |
3324 version (STATICCTOR) { | |
3325 e = el_bin(OPER.OPcall, TYM.TYvoid, el_var(rtlsym[RTLSYM.RTLSYM_FATEXIT]), el_ptr(s)); | |
3326 ector = el_combine(ector, e); | |
3327 dtorcount++; | |
3328 } else { | |
3329 StaticDtorDeclaration f2 = isStaticDtorDeclaration(); | |
3330 assert(f2); | |
3331 if (f2.vgate) | |
3332 { | |
3333 /* Increment destructor's vgate at construction time | |
3334 */ | |
3335 ectorgates.push(cast(void*)f2); | |
3336 } | |
3337 | |
3338 e = el_una(OPER.OPucall, TYM.TYvoid, el_var(s)); | |
3339 edtor = el_combine(e, edtor); | |
3340 } | |
3341 } | |
3342 | |
3343 // If unit test | |
3344 if (isUnitTestDeclaration()) | |
3345 { | |
3346 elem* e = el_una(OPER.OPucall, TYM.TYvoid, el_var(s)); | |
3347 etest = el_combine(etest, e); | |
3348 } | |
3349 | |
3350 if (global.errors) | |
3351 return; | |
3352 | |
3353 writefunc(s); | |
3354 | |
3355 if (isExport()) { | |
3356 obj_export(s, Poffset); | |
3357 } | |
3358 | |
3359 for (i = 0; i < irs.deferToObj.dim; i++) | |
3360 { | |
3361 Dsymbol ss = cast(Dsymbol)irs.deferToObj.data[i]; | |
3362 ss.toObjFile(0); | |
3363 } | |
3364 | |
22
fd4acc376c45
Implemented object file output and linking on linux.
Robert Clipsham <robert@octarineparrot.com>
parents:
17
diff
changeset
|
3365 version (POSIX) { ///TARGET_LINUX || TARGET_OSX || TARGET_FREEBSD || TARGET_SOLARIS |
0 | 3366 // A hack to get a pointer to this function put in the .dtors segment |
3367 if (ident && ident.toChars() == "_STD") { | |
3368 obj_staticdtor(s); | |
3369 } | |
3370 } | |
3371 version (DMDV2) { | |
3372 if (irs.startaddress) | |
3373 { | |
3374 writef("Setting start address\n"); | |
3375 obj_startaddress(irs.startaddress); | |
3376 } | |
3377 } | |
3378 } | |
3379 | |
3380 int cvMember(ubyte* p) | |
3381 { | |
3382 assert(false); | |
3383 } | |
3384 | |
3385 /************************************* | |
3386 * Closures are implemented by taking the local variables that | |
3387 * need to survive the scope of the function, and copying them | |
3388 * into a gc allocated chuck of memory. That chunk, called the | |
3389 * closure here, is inserted into the linked list of stack | |
3390 * frames instead of the usual stack frame. | |
3391 * | |
3392 * buildClosure() inserts code just after the function prolog | |
3393 * is complete. It allocates memory for the closure, allocates | |
3394 * a local variable (sclosure) to point to it, inserts into it | |
3395 * the link to the enclosing frame, and copies into it the parameters | |
3396 * that are referred to in nested functions. | |
3397 * In VarExp.toElem and SymOffExp.toElem, when referring to a | |
3398 * variable that is in a closure, takes the offset from sclosure rather | |
3399 * than from the frame pointer. | |
3400 * | |
3401 * getEthis() and NewExp.toElem need to use sclosure, if set, rather | |
3402 * than the current frame pointer. | |
3403 */ | |
3404 void buildClosure(IRState* irs) | |
3405 { | |
3406 if (needsClosure()) | |
3407 { | |
3408 // Generate closure on the heap | |
3409 // BUG: doesn't capture variadic arguments passed to this function | |
3410 | |
3411 version (DMDV2) { | |
3412 /* BUG: doesn't handle destructors for the local variables. | |
3413 * The way to do it is to make the closure variables the fields | |
3414 * of a class object: | |
3415 * class Closure | |
3416 * { vtbl[] | |
3417 * monitor | |
3418 * ptr to destructor | |
3419 * sthis | |
3420 * ... closure variables ... | |
3421 * ~this() { call destructor } | |
3422 * } | |
3423 */ | |
3424 } | |
3425 //printf("FuncDeclaration.buildClosure()\n"); | |
3426 Symbol* sclosure; | |
3427 sclosure = symbol_name("__closptr".ptr, SC.SCauto, Type.tvoidptr.toCtype()); | |
3428 sclosure.Sflags |= SFL.SFLtrue | SFL.SFLfree; | |
3429 symbol_add(sclosure); | |
3430 irs.sclosure = sclosure; | |
3431 | |
3432 uint offset = PTRSIZE; // leave room for previous sthis | |
3433 for (int i = 0; i < closureVars.dim; i++) | |
3434 { | |
3435 VarDeclaration v = cast(VarDeclaration)closureVars.data[i]; | |
3436 assert(v.isVarDeclaration()); | |
3437 | |
3438 version (DMDV2) { | |
3439 if (v.needsAutoDtor()) | |
3440 v.error("has scoped destruction, cannot build closure"); | |
3441 } | |
3442 /* Align and allocate space for v in the closure | |
3443 * just like AggregateDeclaration.addField() does. | |
3444 */ | |
3445 uint memsize; | |
3446 uint memalignsize; | |
3447 uint xalign; | |
3448 /// version (DMDV2) { | |
3449 if (v.storage_class & STC.STClazy) | |
3450 { | |
3451 /* Lazy variables are really delegates, | |
3452 * so give same answers that TypeDelegate would | |
3453 */ | |
3454 memsize = PTRSIZE * 2; | |
3455 memalignsize = memsize; | |
3456 xalign = global.structalign; | |
3457 } | |
3458 else | |
3459 /// } | |
3460 { | |
3461 memsize = cast(uint)v.type.size(); | |
3462 memalignsize = v.type.alignsize(); | |
3463 xalign = v.type.memalign(global.structalign); | |
3464 } | |
3465 AggregateDeclaration.alignmember(xalign, memalignsize, &offset); | |
3466 v.offset = offset; | |
3467 offset += memsize; | |
3468 | |
3469 /* Can't do nrvo if the variable is put in a closure, since | |
3470 * what the shidden points to may no longer exist. | |
3471 */ | |
3472 if (nrvo_can && nrvo_var == v) | |
3473 { | |
3474 nrvo_can = 0; | |
3475 } | |
3476 } | |
3477 // offset is now the size of the closure | |
3478 | |
3479 // Allocate memory for the closure | |
3480 elem* e; | |
3481 e = el_long(TYM.TYint, offset); | |
3482 e = el_bin(OPER.OPcall, TYM.TYnptr, el_var(rtlsym[RTLSYM.RTLSYM_ALLOCMEMORY]), e); | |
3483 | |
3484 // Assign block of memory to sclosure | |
3485 // sclosure = allocmemory(sz); | |
3486 e = el_bin(OPER.OPeq, TYM.TYvoid, el_var(sclosure), e); | |
3487 | |
3488 // Set the first element to sthis | |
3489 // *(sclosure + 0) = sthis; | |
3490 elem* ethis; | |
3491 if (irs.sthis) | |
3492 ethis = el_var(irs.sthis); | |
3493 else | |
3494 ethis = el_long(TYM.TYnptr, 0); | |
3495 elem *ex = el_una(OPER.OPind, TYM.TYnptr, el_var(sclosure)); | |
3496 ex = el_bin(OPER.OPeq, TYM.TYnptr, ex, ethis); | |
3497 e = el_combine(e, ex); | |
3498 | |
3499 // Copy function parameters into closure | |
3500 for (int i = 0; i < closureVars.dim; i++) | |
3501 { VarDeclaration v = cast(VarDeclaration)closureVars.data[i]; | |
3502 | |
3503 if (!v.isParameter()) | |
3504 continue; | |
3505 TYM tym = v.type.totym(); | |
3506 if (v.type.toBasetype().ty == TY.Tsarray || v.isOut() || v.isRef()) | |
3507 tym = TYM.TYnptr; // reference parameters are just pointers | |
3508 /// version (DMDV2) { | |
3509 else if (v.storage_class & STC.STClazy) | |
3510 tym = TYM.TYdelegate; | |
3511 /// } | |
3512 ex = el_bin(OPER.OPadd, TYM.TYnptr, el_var(sclosure), el_long(TYM.TYint, v.offset)); | |
3513 ex = el_una(OPER.OPind, tym, ex); | |
3514 if (ex.Ety == TYM.TYstruct) | |
3515 { | |
3516 ex.Enumbytes = cast(uint)v.type.size(); | |
3517 ex = el_bin(OPER.OPstreq, tym, ex, el_var(v.toSymbol())); | |
3518 ex.Enumbytes = cast(uint)v.type.size(); | |
3519 } | |
3520 else | |
3521 { | |
3522 ex = el_bin(OPER.OPeq, tym, ex, el_var(v.toSymbol())); | |
3523 } | |
3524 | |
3525 e = el_combine(e, ex); | |
3526 } | |
3527 | |
3528 block_appendexp(irs.blx.curblock, e); | |
3529 } | |
3530 } | |
3531 | |
3532 FuncDeclaration isFuncDeclaration() { return this; } | |
14
2cc604139636
Implemented Linux support for ddmd. Some parts are a bit hacky to just "get it working", that said, druntime and phobos compile, and unittests pass.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
3533 } |