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