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