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