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