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