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