Mercurial > projects > ldc
annotate dmd/func.c @ 913:29c0d1194033
Fix #198 and #199 by making CTFE on static struct initializers work.
Renamed SymbolDeclaration to StaticStructInitDeclaration to make its usage clearer.
author | Christian Kamm <kamm incasoftware de> |
---|---|
date | Sun, 01 Feb 2009 20:20:56 +0100 |
parents | ea82e2077f9d |
children | 6c09179ebba0 |
rev | line source |
---|---|
159 | 1 |
2 // Compiler implementation of the D programming language | |
3 // Copyright (c) 1999-2008 by Digital Mars | |
4 // All Rights Reserved | |
5 // written by Walter Bright | |
6 // http://www.digitalmars.com | |
7 // License for redistribution is by either the Artistic License | |
8 // in artistic.txt, or the GNU General Public License in gnu.txt. | |
9 // See the included readme.txt for details. | |
10 | |
11 #include <stdio.h> | |
12 #include <assert.h> | |
13 | |
14 #include "mars.h" | |
15 #include "init.h" | |
16 #include "declaration.h" | |
17 #include "attrib.h" | |
18 #include "expression.h" | |
19 #include "scope.h" | |
20 #include "mtype.h" | |
21 #include "aggregate.h" | |
22 #include "identifier.h" | |
23 #include "id.h" | |
24 #include "module.h" | |
25 #include "statement.h" | |
26 #include "template.h" | |
27 #include "hdrgen.h" | |
28 | |
29 #ifdef IN_GCC | |
30 #include "d-dmd-gcc.h" | |
31 #endif | |
32 | |
33 /********************************* FuncDeclaration ****************************/ | |
34 | |
35 FuncDeclaration::FuncDeclaration(Loc loc, Loc endloc, Identifier *id, enum STC storage_class, Type *type) | |
36 : Declaration(id) | |
37 { | |
38 //printf("FuncDeclaration(id = '%s', type = %p)\n", id->toChars(), type); | |
39 this->storage_class = storage_class; | |
40 this->type = type; | |
41 this->loc = loc; | |
42 this->endloc = endloc; | |
43 fthrows = NULL; | |
44 frequire = NULL; | |
45 outId = NULL; | |
46 vresult = NULL; | |
47 returnLabel = NULL; | |
48 fensure = NULL; | |
49 fbody = NULL; | |
50 localsymtab = NULL; | |
51 vthis = NULL; | |
52 v_arguments = NULL; | |
53 #if IN_GCC | |
54 v_argptr = NULL; | |
55 #endif | |
56 parameters = NULL; | |
57 labtab = NULL; | |
58 overnext = NULL; | |
59 vtblIndex = -1; | |
60 hasReturnExp = 0; | |
61 naked = 0; | |
62 inlineStatus = ILSuninitialized; | |
63 inlineNest = 0; | |
64 inlineAsm = 0; | |
65 cantInterpret = 0; | |
66 semanticRun = 0; | |
67 nestedFrameRef = 0; | |
68 fes = NULL; | |
69 introducing = 0; | |
70 tintro = NULL; | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
468
diff
changeset
|
71 /* The type given for "infer the return type" is a TypeFunction with |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
468
diff
changeset
|
72 * NULL for the return type. |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
468
diff
changeset
|
73 */ |
159 | 74 inferRetType = (type && type->nextOf() == NULL); |
75 scope = NULL; | |
76 hasReturnExp = 0; | |
77 nrvo_can = 1; | |
78 nrvo_var = NULL; | |
79 shidden = NULL; | |
703
06576ece1a1b
Changed premake.lua to work with mingw.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
80 |
06576ece1a1b
Changed premake.lua to work with mingw.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
81 // LDC |
06576ece1a1b
Changed premake.lua to work with mingw.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
82 isArrayOp = false; |
159 | 83 } |
84 | |
85 Dsymbol *FuncDeclaration::syntaxCopy(Dsymbol *s) | |
86 { | |
87 FuncDeclaration *f; | |
88 | |
89 //printf("FuncDeclaration::syntaxCopy('%s')\n", toChars()); | |
90 if (s) | |
91 f = (FuncDeclaration *)s; | |
92 else | |
93 f = new FuncDeclaration(loc, endloc, ident, (enum STC) storage_class, type->syntaxCopy()); | |
94 f->outId = outId; | |
95 f->frequire = frequire ? frequire->syntaxCopy() : NULL; | |
96 f->fensure = fensure ? fensure->syntaxCopy() : NULL; | |
97 f->fbody = fbody ? fbody->syntaxCopy() : NULL; | |
98 assert(!fthrows); // deprecated | |
443
44f08170f4ef
Removed tango from the repository and instead added a runtime dir with the files needed to patch and build tango from svn.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
378
diff
changeset
|
99 |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
658
diff
changeset
|
100 // LDC |
443
44f08170f4ef
Removed tango from the repository and instead added a runtime dir with the files needed to patch and build tango from svn.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
378
diff
changeset
|
101 f->intrinsicName = intrinsicName; |
44f08170f4ef
Removed tango from the repository and instead added a runtime dir with the files needed to patch and build tango from svn.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
378
diff
changeset
|
102 |
159 | 103 return f; |
104 } | |
105 | |
106 | |
107 // Do the semantic analysis on the external interface to the function. | |
108 | |
109 void FuncDeclaration::semantic(Scope *sc) | |
110 { TypeFunction *f; | |
111 StructDeclaration *sd; | |
112 ClassDeclaration *cd; | |
113 InterfaceDeclaration *id; | |
336 | 114 Dsymbol *pd; |
159 | 115 |
116 #if 0 | |
117 printf("FuncDeclaration::semantic(sc = %p, this = %p, '%s', linkage = %d)\n", sc, this, toPrettyChars(), sc->linkage); | |
118 if (isFuncLiteralDeclaration()) | |
119 printf("\tFuncLiteralDeclaration()\n"); | |
120 printf("sc->parent = %s\n", sc->parent->toChars()); | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
468
diff
changeset
|
121 printf("type: %p, %s\n", type, type->toChars()); |
159 | 122 #endif |
123 | |
124 if (type->nextOf()) | |
125 type = type->semantic(loc, sc); | |
126 //type->print(); | |
127 if (type->ty != Tfunction) | |
128 { | |
129 error("%s must be a function", toChars()); | |
130 return; | |
131 } | |
132 f = (TypeFunction *)(type); | |
133 size_t nparams = Argument::dim(f->parameters); | |
134 | |
135 linkage = sc->linkage; | |
136 // if (!parent) | |
137 { | |
138 //parent = sc->scopesym; | |
139 parent = sc->parent; | |
140 } | |
141 protection = sc->protection; | |
142 storage_class |= sc->stc; | |
143 //printf("function storage_class = x%x\n", storage_class); | |
144 Dsymbol *parent = toParent(); | |
145 | |
658
50383e476c7e
Upgraded frontend to DMD 1.035
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
644
diff
changeset
|
146 if (ident == Id::ctor && !isCtorDeclaration()) |
50383e476c7e
Upgraded frontend to DMD 1.035
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
644
diff
changeset
|
147 error("_ctor is reserved for constructors"); |
50383e476c7e
Upgraded frontend to DMD 1.035
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
644
diff
changeset
|
148 |
159 | 149 if (isConst() || isAuto() || isScope()) |
150 error("functions cannot be const or auto"); | |
151 | |
152 if (isAbstract() && !isVirtual()) | |
153 error("non-virtual functions cannot be abstract"); | |
154 | |
155 if (isAbstract() && isFinal()) | |
156 error("cannot be both final and abstract"); | |
157 #if 0 | |
158 if (isAbstract() && fbody) | |
159 error("abstract functions cannot have bodies"); | |
160 #endif | |
161 | |
162 #if 0 | |
163 if (isStaticConstructor() || isStaticDestructor()) | |
164 { | |
165 if (!isStatic() || type->nextOf()->ty != Tvoid) | |
166 error("static constructors / destructors must be static void"); | |
167 if (f->arguments && f->arguments->dim) | |
168 error("static constructors / destructors must have empty parameter list"); | |
169 // BUG: check for invalid storage classes | |
170 } | |
171 #endif | |
172 | |
173 #ifdef IN_GCC | |
174 AggregateDeclaration *ad; | |
175 | |
176 ad = parent->isAggregateDeclaration(); | |
177 if (ad) | |
178 ad->methods.push(this); | |
179 #endif | |
180 sd = parent->isStructDeclaration(); | |
181 if (sd) | |
182 { | |
183 // Verify no constructors, destructors, etc. | |
184 if (isCtorDeclaration() || | |
185 isDtorDeclaration() | |
186 //|| isInvariantDeclaration() | |
187 //|| isUnitTestDeclaration() | |
188 ) | |
189 { | |
190 error("special member functions not allowed for %ss", sd->kind()); | |
191 } | |
192 | |
193 #if 0 | |
194 if (!sd->inv) | |
195 sd->inv = isInvariantDeclaration(); | |
196 | |
197 if (!sd->aggNew) | |
198 sd->aggNew = isNewDeclaration(); | |
199 | |
200 if (isDelete()) | |
201 { | |
202 if (sd->aggDelete) | |
203 error("multiple delete's for struct %s", sd->toChars()); | |
204 sd->aggDelete = (DeleteDeclaration *)(this); | |
205 } | |
206 #endif | |
207 } | |
208 | |
209 id = parent->isInterfaceDeclaration(); | |
210 if (id) | |
211 { | |
212 storage_class |= STCabstract; | |
213 | |
214 if (isCtorDeclaration() || | |
215 isDtorDeclaration() || | |
216 isInvariantDeclaration() || | |
217 isUnitTestDeclaration() || isNewDeclaration() || isDelete()) | |
218 error("special function not allowed in interface %s", id->toChars()); | |
219 if (fbody) | |
220 error("function body is not abstract in interface %s", id->toChars()); | |
221 } | |
222 | |
336 | 223 /* Template member functions aren't virtual: |
224 * interface TestInterface { void tpl(T)(); } | |
225 * and so won't work in interfaces | |
226 */ | |
227 if ((pd = toParent()) != NULL && | |
228 pd->isTemplateInstance() && | |
229 (pd = toParent2()) != NULL && | |
230 (id = pd->isInterfaceDeclaration()) != NULL) | |
231 { | |
232 error("template member function not allowed in interface %s", id->toChars()); | |
233 } | |
234 | |
159 | 235 cd = parent->isClassDeclaration(); |
236 if (cd) | |
237 { int vi; | |
238 CtorDeclaration *ctor; | |
239 DtorDeclaration *dtor; | |
240 InvariantDeclaration *inv; | |
241 | |
242 if (isCtorDeclaration()) | |
243 { | |
244 // ctor = (CtorDeclaration *)this; | |
245 // if (!cd->ctor) | |
246 // cd->ctor = ctor; | |
247 return; | |
248 } | |
249 | |
250 #if 0 | |
251 dtor = isDtorDeclaration(); | |
252 if (dtor) | |
253 { | |
254 if (cd->dtor) | |
255 error("multiple destructors for class %s", cd->toChars()); | |
256 cd->dtor = dtor; | |
257 } | |
258 | |
259 inv = isInvariantDeclaration(); | |
260 if (inv) | |
261 { | |
262 cd->inv = inv; | |
263 } | |
264 | |
265 if (isNewDeclaration()) | |
266 { | |
267 if (!cd->aggNew) | |
268 cd->aggNew = (NewDeclaration *)(this); | |
269 } | |
270 | |
271 if (isDelete()) | |
272 { | |
273 if (cd->aggDelete) | |
274 error("multiple delete's for class %s", cd->toChars()); | |
275 cd->aggDelete = (DeleteDeclaration *)(this); | |
276 } | |
277 #endif | |
278 | |
279 if (storage_class & STCabstract) | |
280 cd->isabstract = 1; | |
281 | |
282 // if static function, do not put in vtbl[] | |
283 if (!isVirtual()) | |
284 { | |
285 //printf("\tnot virtual\n"); | |
286 goto Ldone; | |
287 } | |
288 | |
289 // Find index of existing function in vtbl[] to override | |
290 vi = findVtblIndex(&cd->vtbl, cd->baseClass ? cd->baseClass->vtbl.dim : 0); | |
291 switch (vi) | |
292 { | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
468
diff
changeset
|
293 case -1: |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
468
diff
changeset
|
294 /* Didn't find one, so |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
468
diff
changeset
|
295 * This is an 'introducing' function which gets a new |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
468
diff
changeset
|
296 * slot in the vtbl[]. |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
468
diff
changeset
|
297 */ |
159 | 298 |
299 // Verify this doesn't override previous final function | |
300 if (cd->baseClass) | |
301 { Dsymbol *s = cd->baseClass->search(loc, ident, 0); | |
302 if (s) | |
303 { | |
304 FuncDeclaration *f = s->isFuncDeclaration(); | |
305 f = f->overloadExactMatch(type); | |
306 if (f && f->isFinal() && f->prot() != PROTprivate) | |
307 error("cannot override final function %s", f->toPrettyChars()); | |
308 } | |
309 } | |
310 | |
311 if (isFinal()) | |
312 { | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
313 if (isOverride()) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
314 error("does not override any function"); |
159 | 315 cd->vtblFinal.push(this); |
316 } | |
317 else | |
318 { | |
319 // Append to end of vtbl[] | |
320 //printf("\tintroducing function\n"); | |
321 introducing = 1; | |
322 vi = cd->vtbl.dim; | |
323 cd->vtbl.push(this); | |
324 vtblIndex = vi; | |
325 } | |
326 break; | |
327 | |
328 case -2: // can't determine because of fwd refs | |
329 cd->sizeok = 2; // can't finish due to forward reference | |
330 return; | |
331 | |
332 default: | |
333 { FuncDeclaration *fdv = (FuncDeclaration *)cd->vtbl.data[vi]; | |
334 // This function is covariant with fdv | |
335 if (fdv->isFinal()) | |
336 error("cannot override final function %s", fdv->toPrettyChars()); | |
337 | |
336 | 338 #if DMDV2 |
159 | 339 if (!isOverride() && global.params.warnings) |
215
a58d8f4b84df
[svn r231] Changed: warnings are no longer treated as an error.
lindquist
parents:
162
diff
changeset
|
340 warning("%s: overrides base class function %s, but is not marked with 'override'", locToChars() fdv->toPrettyChars()); |
159 | 341 #endif |
342 | |
343 if (fdv->toParent() == parent) | |
344 { | |
345 // If both are mixins, then error. | |
346 // If either is not, the one that is not overrides | |
347 // the other. | |
348 if (fdv->parent->isClassDeclaration()) | |
349 break; | |
350 if (!this->parent->isClassDeclaration() | |
351 #if !BREAKABI | |
352 && !isDtorDeclaration() | |
353 #endif | |
336 | 354 #if DMDV2 |
159 | 355 && !isPostBlitDeclaration() |
356 #endif | |
357 ) | |
358 error("multiple overrides of same function"); | |
359 } | |
360 cd->vtbl.data[vi] = (void *)this; | |
361 vtblIndex = vi; | |
362 | |
363 /* This works by whenever this function is called, | |
364 * it actually returns tintro, which gets dynamically | |
365 * cast to type. But we know that tintro is a base | |
366 * of type, so we could optimize it by not doing a | |
367 * dynamic cast, but just subtracting the isBaseOf() | |
368 * offset if the value is != null. | |
369 */ | |
370 | |
371 if (fdv->tintro) | |
372 tintro = fdv->tintro; | |
373 else if (!type->equals(fdv->type)) | |
374 { | |
375 /* Only need to have a tintro if the vptr | |
376 * offsets differ | |
377 */ | |
378 int offset; | |
379 if (fdv->type->nextOf()->isBaseOf(type->nextOf(), &offset)) | |
380 { | |
381 tintro = fdv->type; | |
382 } | |
383 } | |
384 break; | |
385 } | |
386 } | |
387 | |
388 /* Go through all the interface bases. | |
389 * If this function is covariant with any members of those interface | |
390 * functions, set the tintro. | |
391 */ | |
392 for (int i = 0; i < cd->interfaces_dim; i++) | |
393 { | |
394 #if 1 | |
395 BaseClass *b = cd->interfaces[i]; | |
396 vi = findVtblIndex(&b->base->vtbl, b->base->vtbl.dim); | |
397 switch (vi) | |
398 { | |
399 case -1: | |
400 break; | |
401 | |
402 case -2: | |
403 cd->sizeok = 2; // can't finish due to forward reference | |
404 return; | |
405 | |
406 default: | |
407 { FuncDeclaration *fdv = (FuncDeclaration *)b->base->vtbl.data[vi]; | |
408 Type *ti = NULL; | |
409 | |
410 if (fdv->tintro) | |
411 ti = fdv->tintro; | |
412 else if (!type->equals(fdv->type)) | |
413 { | |
414 /* Only need to have a tintro if the vptr | |
415 * offsets differ | |
416 */ | |
417 int offset; | |
418 if (fdv->type->nextOf()->isBaseOf(type->nextOf(), &offset)) | |
419 { | |
420 ti = fdv->type; | |
421 #if 0 | |
422 if (offset) | |
423 ti = fdv->type; | |
424 else if (type->nextOf()->ty == Tclass) | |
425 { ClassDeclaration *cdn = ((TypeClass *)type->nextOf())->sym; | |
426 if (cdn && cdn->sizeok != 1) | |
427 ti = fdv->type; | |
428 } | |
429 #endif | |
430 } | |
431 } | |
432 if (ti) | |
433 { | |
434 if (tintro && !tintro->equals(ti)) | |
435 { | |
436 error("incompatible covariant types %s and %s", tintro->toChars(), ti->toChars()); | |
437 } | |
438 tintro = ti; | |
439 } | |
440 goto L2; | |
441 } | |
442 } | |
443 #else | |
444 BaseClass *b = cd->interfaces[i]; | |
445 for (vi = 0; vi < b->base->vtbl.dim; vi++) | |
446 { | |
447 Dsymbol *s = (Dsymbol *)b->base->vtbl.data[vi]; | |
448 //printf("interface %d vtbl[%d] %p %s\n", i, vi, s, s->toChars()); | |
449 FuncDeclaration *fdv = s->isFuncDeclaration(); | |
450 if (fdv && fdv->ident == ident) | |
451 { | |
452 int cov = type->covariant(fdv->type); | |
453 //printf("\tcov = %d\n", cov); | |
454 if (cov == 2) | |
455 { | |
456 //type->print(); | |
457 //fdv->type->print(); | |
458 //printf("%s %s\n", type->deco, fdv->type->deco); | |
459 error("of type %s overrides but is not covariant with %s of type %s", | |
460 type->toChars(), fdv->toPrettyChars(), fdv->type->toChars()); | |
461 } | |
462 if (cov == 1) | |
463 { Type *ti = NULL; | |
464 | |
465 if (fdv->tintro) | |
466 ti = fdv->tintro; | |
467 else if (!type->equals(fdv->type)) | |
468 { | |
469 /* Only need to have a tintro if the vptr | |
470 * offsets differ | |
471 */ | |
472 int offset; | |
473 if (fdv->type->nextOf()->isBaseOf(type->nextOf(), &offset)) | |
474 { | |
475 ti = fdv->type; | |
476 #if 0 | |
477 if (offset) | |
478 ti = fdv->type; | |
479 else if (type->nextOf()->ty == Tclass) | |
480 { ClassDeclaration *cdn = ((TypeClass *)type->nextOf())->sym; | |
481 if (cdn && cdn->sizeok != 1) | |
482 ti = fdv->type; | |
483 } | |
484 #endif | |
485 } | |
486 } | |
487 if (ti) | |
488 { | |
489 if (tintro && !tintro->equals(ti)) | |
490 { | |
491 error("incompatible covariant types %s and %s", tintro->toChars(), ti->toChars()); | |
492 } | |
493 tintro = ti; | |
494 } | |
495 goto L2; | |
496 } | |
497 if (cov == 3) | |
498 { | |
499 cd->sizeok = 2; // can't finish due to forward reference | |
500 return; | |
501 } | |
502 } | |
503 } | |
504 #endif | |
505 } | |
506 | |
507 if (introducing && isOverride()) | |
508 { | |
509 error("does not override any function"); | |
510 } | |
511 | |
512 L2: ; | |
513 } | |
514 else if (isOverride() && !parent->isTemplateInstance()) | |
515 error("override only applies to class member functions"); | |
516 | |
517 /* Do not allow template instances to add virtual functions | |
518 * to a class. | |
519 */ | |
520 if (isVirtual()) | |
521 { | |
522 TemplateInstance *ti = parent->isTemplateInstance(); | |
523 if (ti) | |
524 { | |
525 // Take care of nested templates | |
526 while (1) | |
527 { | |
528 TemplateInstance *ti2 = ti->tempdecl->parent->isTemplateInstance(); | |
529 if (!ti2) | |
530 break; | |
531 ti = ti2; | |
532 } | |
533 | |
534 // If it's a member template | |
535 ClassDeclaration *cd = ti->tempdecl->isClassMember(); | |
536 if (cd) | |
537 { | |
538 error("cannot use template to add virtual function to class '%s'", cd->toChars()); | |
539 } | |
540 } | |
541 } | |
542 | |
543 if (isMain()) | |
544 { | |
545 // Check parameters to see if they are either () or (char[][] args) | |
546 switch (nparams) | |
547 { | |
548 case 0: | |
549 break; | |
550 | |
551 case 1: | |
552 { | |
553 Argument *arg0 = Argument::getNth(f->parameters, 0); | |
554 if (arg0->type->ty != Tarray || | |
555 arg0->type->nextOf()->ty != Tarray || | |
556 arg0->type->nextOf()->nextOf()->ty != Tchar || | |
557 arg0->storageClass & (STCout | STCref | STClazy)) | |
558 goto Lmainerr; | |
559 break; | |
560 } | |
561 | |
562 default: | |
563 goto Lmainerr; | |
564 } | |
565 | |
566 if (f->nextOf()->ty != Tint32 && f->nextOf()->ty != Tvoid) | |
567 error("must return int or void, not %s", f->nextOf()->toChars()); | |
568 if (f->varargs) | |
569 { | |
570 Lmainerr: | |
571 error("parameters must be main() or main(char[][] args)"); | |
572 } | |
573 } | |
574 | |
575 if (ident == Id::assign && (sd || cd)) | |
576 { // Disallow identity assignment operator. | |
577 | |
578 // opAssign(...) | |
579 if (nparams == 0) | |
580 { if (f->varargs == 1) | |
581 goto Lassignerr; | |
582 } | |
583 else | |
584 { | |
585 Argument *arg0 = Argument::getNth(f->parameters, 0); | |
586 Type *t0 = arg0->type->toBasetype(); | |
587 Type *tb = sd ? sd->type : cd->type; | |
588 if (arg0->type->implicitConvTo(tb) || | |
589 (sd && t0->ty == Tpointer && t0->nextOf()->implicitConvTo(tb)) | |
590 ) | |
591 { | |
592 if (nparams == 1) | |
593 goto Lassignerr; | |
594 Argument *arg1 = Argument::getNth(f->parameters, 1); | |
595 if (arg1->defaultArg) | |
596 goto Lassignerr; | |
597 } | |
598 } | |
599 } | |
600 | |
601 Ldone: | |
602 /* Save scope for possible later use (if we need the | |
603 * function internals) | |
604 */ | |
605 scope = new Scope(*sc); | |
606 scope->setNoFree(); | |
607 return; | |
608 | |
609 Lassignerr: | |
610 error("identity assignment operator overload is illegal"); | |
611 } | |
612 | |
613 void FuncDeclaration::semantic2(Scope *sc) | |
614 { | |
615 } | |
616 | |
617 // Do the semantic analysis on the internals of the function. | |
618 | |
619 void FuncDeclaration::semantic3(Scope *sc) | |
620 { TypeFunction *f; | |
621 AggregateDeclaration *ad; | |
622 VarDeclaration *argptr = NULL; | |
623 VarDeclaration *_arguments = NULL; | |
624 | |
625 if (!parent) | |
626 { | |
627 if (global.errors) | |
628 return; | |
629 //printf("FuncDeclaration::semantic3(%s '%s', sc = %p)\n", kind(), toChars(), sc); | |
630 assert(0); | |
631 } | |
632 //printf("FuncDeclaration::semantic3('%s.%s', sc = %p, loc = %s)\n", parent->toChars(), toChars(), sc, loc.toChars()); | |
633 //fflush(stdout); | |
634 //{ static int x; if (++x == 2) *(char*)0=0; } | |
635 //printf("\tlinkage = %d\n", sc->linkage); | |
636 | |
637 //printf(" sc->incontract = %d\n", sc->incontract); | |
638 if (semanticRun) | |
639 return; | |
640 semanticRun = 1; | |
641 | |
642 if (!type || type->ty != Tfunction) | |
643 return; | |
644 f = (TypeFunction *)(type); | |
645 size_t nparams = Argument::dim(f->parameters); | |
646 | |
647 // Check the 'throws' clause | |
648 if (fthrows) | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
468
diff
changeset
|
649 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
468
diff
changeset
|
650 for (int i = 0; i < fthrows->dim; i++) |
159 | 651 { |
652 Type *t = (Type *)fthrows->data[i]; | |
653 | |
654 t = t->semantic(loc, sc); | |
655 if (!t->isClassHandle()) | |
656 error("can only throw classes, not %s", t->toChars()); | |
657 } | |
658 } | |
659 | |
660 if (fbody || frequire) | |
661 { | |
662 /* Symbol table into which we place parameters and nested functions, | |
663 * solely to diagnose name collisions. | |
664 */ | |
665 localsymtab = new DsymbolTable(); | |
666 | |
667 // Establish function scope | |
668 ScopeDsymbol *ss = new ScopeDsymbol(); | |
669 ss->parent = sc->scopesym; | |
670 Scope *sc2 = sc->push(ss); | |
671 sc2->func = this; | |
672 sc2->parent = this; | |
673 sc2->callSuper = 0; | |
674 sc2->sbreak = NULL; | |
675 sc2->scontinue = NULL; | |
676 sc2->sw = NULL; | |
677 sc2->fes = fes; | |
678 sc2->linkage = LINKd; | |
679 sc2->stc &= ~(STCauto | STCscope | STCstatic | STCabstract | STCdeprecated | STCfinal); | |
680 sc2->protection = PROTpublic; | |
681 sc2->explicitProtection = 0; | |
682 sc2->structalign = 8; | |
683 sc2->incontract = 0; | |
684 sc2->tf = NULL; | |
644
49466efd4fba
Don't let outer enclosing handlers propagate to nested function declarations.
Christian Kamm <kamm incasoftware de>
parents:
510
diff
changeset
|
685 sc2->tfOfTry = NULL; |
159 | 686 sc2->noctor = 0; |
687 | |
688 // Declare 'this' | |
689 ad = isThis(); | |
690 if (ad) | |
691 { VarDeclaration *v; | |
692 | |
693 if (isFuncLiteralDeclaration() && isNested()) | |
694 { | |
695 error("literals cannot be class members"); | |
696 return; | |
697 } | |
698 else | |
699 { | |
700 assert(!isNested()); // can't be both member and nested | |
701 assert(ad->handle); | |
702 v = new ThisDeclaration(ad->handle); | |
703 v->storage_class |= STCparameter | STCin; | |
704 v->semantic(sc2); | |
705 if (!sc2->insert(v)) | |
706 assert(0); | |
707 v->parent = this; | |
708 vthis = v; | |
709 } | |
710 } | |
711 else if (isNested()) | |
712 { | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
468
diff
changeset
|
713 /* The 'this' for a nested function is the link to the |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
468
diff
changeset
|
714 * enclosing function's stack frame. |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
468
diff
changeset
|
715 * Note that nested functions and member functions are disjoint. |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
468
diff
changeset
|
716 */ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
468
diff
changeset
|
717 VarDeclaration *v = new ThisDeclaration(Type::tvoid->pointerTo()); |
159 | 718 v->storage_class |= STCparameter | STCin; |
719 v->semantic(sc2); | |
720 if (!sc2->insert(v)) | |
721 assert(0); | |
722 v->parent = this; | |
723 vthis = v; | |
724 } | |
725 | |
726 // Declare hidden variable _arguments[] and _argptr | |
727 if (f->varargs == 1) | |
728 { Type *t; | |
729 | |
730 if (f->linkage == LINKd) | |
731 { // Declare _arguments[] | |
732 #if BREAKABI | |
733 v_arguments = new VarDeclaration(0, Type::typeinfotypelist->type, Id::_arguments_typeinfo, NULL); | |
734 v_arguments->storage_class = STCparameter | STCin; | |
735 v_arguments->semantic(sc2); | |
736 sc2->insert(v_arguments); | |
737 v_arguments->parent = this; | |
738 | |
739 t = Type::typeinfo->type->arrayOf(); | |
740 _arguments = new VarDeclaration(0, t, Id::_arguments, NULL); | |
741 _arguments->semantic(sc2); | |
742 sc2->insert(_arguments); | |
743 _arguments->parent = this; | |
744 #else | |
745 t = Type::typeinfo->type->arrayOf(); | |
746 v_arguments = new VarDeclaration(0, t, Id::_arguments, NULL); | |
747 v_arguments->storage_class = STCparameter | STCin; | |
748 v_arguments->semantic(sc2); | |
749 sc2->insert(v_arguments); | |
750 v_arguments->parent = this; | |
751 #endif | |
752 } | |
753 if (f->linkage == LINKd || (parameters && parameters->dim)) | |
754 { // Declare _argptr | |
755 #if IN_GCC | |
756 t = d_gcc_builtin_va_list_d_type; | |
757 #else | |
758 t = Type::tvoid->pointerTo(); | |
759 #endif | |
760 argptr = new VarDeclaration(0, t, Id::_argptr, NULL); | |
761 argptr->semantic(sc2); | |
762 sc2->insert(argptr); | |
763 argptr->parent = this; | |
764 } | |
765 } | |
766 | |
767 // Propagate storage class from tuple parameters to their element-parameters. | |
768 if (f->parameters) | |
769 { | |
770 for (size_t i = 0; i < f->parameters->dim; i++) | |
771 { Argument *arg = (Argument *)f->parameters->data[i]; | |
772 | |
773 if (arg->type->ty == Ttuple) | |
774 { TypeTuple *t = (TypeTuple *)arg->type; | |
775 size_t dim = Argument::dim(t->arguments); | |
776 for (size_t j = 0; j < dim; j++) | |
777 { Argument *narg = Argument::getNth(t->arguments, j); | |
778 narg->storageClass = arg->storageClass; | |
779 } | |
780 } | |
781 } | |
782 } | |
783 | |
784 // Declare all the function parameters as variables | |
785 if (nparams) | |
786 { /* parameters[] has all the tuples removed, as the back end | |
787 * doesn't know about tuples | |
788 */ | |
789 parameters = new Dsymbols(); | |
790 parameters->reserve(nparams); | |
791 for (size_t i = 0; i < nparams; i++) | |
792 { | |
793 Argument *arg = Argument::getNth(f->parameters, i); | |
794 Identifier *id = arg->ident; | |
795 if (!id) | |
796 { | |
797 /* Generate identifier for un-named parameter, | |
798 * because we need it later on. | |
799 */ | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
468
diff
changeset
|
800 arg->ident = id = Identifier::generateId("_param_", i); |
159 | 801 } |
802 VarDeclaration *v = new VarDeclaration(loc, arg->type, id, NULL); | |
803 //printf("declaring parameter %s of type %s\n", v->toChars(), v->type->toChars()); | |
804 v->storage_class |= STCparameter; | |
805 if (f->varargs == 2 && i + 1 == nparams) | |
806 v->storage_class |= STCvariadic; | |
807 v->storage_class |= arg->storageClass & (STCin | STCout | STCref | STClazy); | |
808 if (v->storage_class & STClazy) | |
809 v->storage_class |= STCin; | |
810 v->semantic(sc2); | |
811 if (!sc2->insert(v)) | |
812 error("parameter %s.%s is already defined", toChars(), v->toChars()); | |
813 else | |
814 parameters->push(v); | |
815 localsymtab->insert(v); | |
816 v->parent = this; | |
817 } | |
818 } | |
819 | |
820 // Declare the tuple symbols and put them in the symbol table, | |
821 // but not in parameters[]. | |
822 if (f->parameters) | |
823 { | |
824 for (size_t i = 0; i < f->parameters->dim; i++) | |
825 { Argument *arg = (Argument *)f->parameters->data[i]; | |
826 | |
827 if (!arg->ident) | |
828 continue; // never used, so ignore | |
829 if (arg->type->ty == Ttuple) | |
830 { TypeTuple *t = (TypeTuple *)arg->type; | |
831 size_t dim = Argument::dim(t->arguments); | |
832 Objects *exps = new Objects(); | |
833 exps->setDim(dim); | |
834 for (size_t j = 0; j < dim; j++) | |
835 { Argument *narg = Argument::getNth(t->arguments, j); | |
836 assert(narg->ident); | |
837 VarDeclaration *v = sc2->search(0, narg->ident, NULL)->isVarDeclaration(); | |
838 assert(v); | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
468
diff
changeset
|
839 Expression *e = new VarExp(v->loc, v); |
159 | 840 exps->data[j] = (void *)e; |
841 } | |
842 assert(arg->ident); | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
468
diff
changeset
|
843 TupleDeclaration *v = new TupleDeclaration(loc, arg->ident, exps); |
159 | 844 //printf("declaring tuple %s\n", v->toChars()); |
845 v->isexp = 1; | |
846 if (!sc2->insert(v)) | |
847 error("parameter %s.%s is already defined", toChars(), v->toChars()); | |
848 localsymtab->insert(v); | |
849 v->parent = this; | |
850 } | |
851 } | |
852 } | |
853 | |
854 /* Do the semantic analysis on the [in] preconditions and | |
855 * [out] postconditions. | |
856 */ | |
857 sc2->incontract++; | |
858 | |
859 if (frequire) | |
860 { /* frequire is composed of the [in] contracts | |
861 */ | |
862 // BUG: need to error if accessing out parameters | |
863 // BUG: need to treat parameters as const | |
864 // BUG: need to disallow returns and throws | |
865 // BUG: verify that all in and ref parameters are read | |
866 frequire = frequire->semantic(sc2); | |
867 labtab = NULL; // so body can't refer to labels | |
868 } | |
869 | |
870 if (fensure || addPostInvariant()) | |
871 { /* fensure is composed of the [out] contracts | |
872 */ | |
873 ScopeDsymbol *sym = new ScopeDsymbol(); | |
874 sym->parent = sc2->scopesym; | |
875 sc2 = sc2->push(sym); | |
876 | |
877 assert(type->nextOf()); | |
878 if (type->nextOf()->ty == Tvoid) | |
879 { | |
880 if (outId) | |
881 error("void functions have no result"); | |
882 } | |
883 else | |
884 { | |
885 if (!outId) | |
886 outId = Id::result; // provide a default | |
887 } | |
888 | |
889 if (outId) | |
890 { // Declare result variable | |
891 VarDeclaration *v; | |
892 Loc loc = this->loc; | |
893 | |
894 if (fensure) | |
895 loc = fensure->loc; | |
896 | |
897 v = new VarDeclaration(loc, type->nextOf(), outId, NULL); | |
898 v->noauto = 1; | |
899 sc2->incontract--; | |
900 v->semantic(sc2); | |
901 sc2->incontract++; | |
902 if (!sc2->insert(v)) | |
903 error("out result %s is already defined", v->toChars()); | |
904 v->parent = this; | |
905 vresult = v; | |
906 | |
907 // vresult gets initialized with the function return value | |
908 // in ReturnStatement::semantic() | |
909 } | |
910 | |
911 // BUG: need to treat parameters as const | |
912 // BUG: need to disallow returns and throws | |
913 if (fensure) | |
914 { fensure = fensure->semantic(sc2); | |
915 labtab = NULL; // so body can't refer to labels | |
916 } | |
917 | |
918 if (!global.params.useOut) | |
919 { fensure = NULL; // discard | |
920 vresult = NULL; | |
921 } | |
922 | |
923 // Postcondition invariant | |
924 if (addPostInvariant()) | |
925 { | |
926 Expression *e = NULL; | |
927 if (isCtorDeclaration()) | |
928 { | |
929 // Call invariant directly only if it exists | |
930 InvariantDeclaration *inv = ad->inv; | |
931 ClassDeclaration *cd = ad->isClassDeclaration(); | |
932 | |
933 while (!inv && cd) | |
934 { | |
935 cd = cd->baseClass; | |
936 if (!cd) | |
937 break; | |
938 inv = cd->inv; | |
939 } | |
940 if (inv) | |
941 { | |
942 e = new DsymbolExp(0, inv); | |
943 e = new CallExp(0, e); | |
944 e = e->semantic(sc2); | |
945 } | |
946 } | |
947 else | |
948 { // Call invariant virtually | |
949 ThisExp *v = new ThisExp(0); | |
950 v->type = vthis->type; | |
951 e = new AssertExp(0, v); | |
952 } | |
953 if (e) | |
954 { | |
955 ExpStatement *s = new ExpStatement(0, e); | |
956 if (fensure) | |
957 fensure = new CompoundStatement(0, s, fensure); | |
958 else | |
959 fensure = s; | |
960 } | |
961 } | |
962 | |
963 if (fensure) | |
964 { returnLabel = new LabelDsymbol(Id::returnLabel); | |
965 LabelStatement *ls = new LabelStatement(0, Id::returnLabel, fensure); | |
966 ls->isReturnLabel = 1; | |
967 returnLabel->statement = ls; | |
968 } | |
969 sc2 = sc2->pop(); | |
970 } | |
971 | |
972 sc2->incontract--; | |
973 | |
974 if (fbody) | |
975 { ClassDeclaration *cd = isClassMember(); | |
976 | |
977 /* If this is a class constructor | |
978 */ | |
979 if (isCtorDeclaration() && cd) | |
980 { | |
981 for (int i = 0; i < cd->fields.dim; i++) | |
982 { VarDeclaration *v = (VarDeclaration *)cd->fields.data[i]; | |
983 | |
984 v->ctorinit = 0; | |
985 } | |
986 } | |
987 | |
988 if (inferRetType || f->retStyle() != RETstack) | |
989 nrvo_can = 0; | |
990 | |
991 fbody = fbody->semantic(sc2); | |
992 | |
993 if (inferRetType) | |
994 { // If no return type inferred yet, then infer a void | |
995 if (!type->nextOf()) | |
996 { | |
997 ((TypeFunction *)type)->next = Type::tvoid; | |
998 type = type->semantic(loc, sc); | |
999 } | |
1000 f = (TypeFunction *)type; | |
1001 } | |
1002 | |
336 | 1003 int offend = fbody ? fbody->blockExit() & BEfallthru : TRUE; |
159 | 1004 |
1005 if (isStaticCtorDeclaration()) | |
1006 { /* It's a static constructor. Ensure that all | |
1007 * ctor consts were initialized. | |
1008 */ | |
1009 | |
1010 Dsymbol *p = toParent(); | |
1011 ScopeDsymbol *ad = p->isScopeDsymbol(); | |
1012 if (!ad) | |
1013 { | |
1014 error("static constructor can only be member of struct/class/module, not %s %s", p->kind(), p->toChars()); | |
1015 } | |
1016 else | |
1017 { | |
1018 for (int i = 0; i < ad->members->dim; i++) | |
1019 { Dsymbol *s = (Dsymbol *)ad->members->data[i]; | |
1020 | |
1021 s->checkCtorConstInit(); | |
1022 } | |
1023 } | |
1024 } | |
1025 | |
1026 if (isCtorDeclaration() && cd) | |
1027 { | |
1028 //printf("callSuper = x%x\n", sc2->callSuper); | |
1029 | |
1030 // Verify that all the ctorinit fields got initialized | |
1031 if (!(sc2->callSuper & CSXthis_ctor)) | |
1032 { | |
1033 for (int i = 0; i < cd->fields.dim; i++) | |
1034 { VarDeclaration *v = (VarDeclaration *)cd->fields.data[i]; | |
1035 | |
1036 if (v->ctorinit == 0 && v->isCtorinit()) | |
1037 error("missing initializer for const field %s", v->toChars()); | |
1038 } | |
1039 } | |
1040 | |
1041 if (!(sc2->callSuper & CSXany_ctor) && | |
1042 cd->baseClass && cd->baseClass->ctor) | |
1043 { | |
1044 sc2->callSuper = 0; | |
1045 | |
1046 // Insert implicit super() at start of fbody | |
1047 Expression *e1 = new SuperExp(0); | |
1048 Expression *e = new CallExp(0, e1); | |
1049 | |
1050 unsigned errors = global.errors; | |
1051 global.gag++; | |
1052 e = e->semantic(sc2); | |
1053 global.gag--; | |
1054 if (errors != global.errors) | |
1055 error("no match for implicit super() call in constructor"); | |
1056 | |
1057 Statement *s = new ExpStatement(0, e); | |
1058 fbody = new CompoundStatement(0, s, fbody); | |
1059 } | |
1060 } | |
1061 else if (fes) | |
1062 { // For foreach(){} body, append a return 0; | |
1063 Expression *e = new IntegerExp(0); | |
1064 Statement *s = new ReturnStatement(0, e); | |
1065 fbody = new CompoundStatement(0, fbody, s); | |
1066 assert(!returnLabel); | |
1067 } | |
1068 else if (!hasReturnExp && type->nextOf()->ty != Tvoid) | |
1069 error("expected to return a value of type %s", type->nextOf()->toChars()); | |
1070 else if (!inlineAsm) | |
1071 { | |
1072 if (type->nextOf()->ty == Tvoid) | |
1073 { | |
1074 if (offend && isMain()) | |
1075 { // Add a return 0; statement | |
1076 Statement *s = new ReturnStatement(0, new IntegerExp(0)); | |
1077 fbody = new CompoundStatement(0, fbody, s); | |
1078 } | |
1079 } | |
1080 else | |
1081 { | |
1082 if (offend) | |
1083 { Expression *e; | |
1084 | |
1085 if (global.params.warnings) | |
215
a58d8f4b84df
[svn r231] Changed: warnings are no longer treated as an error.
lindquist
parents:
162
diff
changeset
|
1086 { warning("%s: no return at end of function", locToChars()); |
159 | 1087 } |
1088 | |
1089 if (global.params.useAssert && | |
1090 !global.params.useInline) | |
1091 { /* Add an assert(0, msg); where the missing return | |
1092 * should be. | |
1093 */ | |
1094 e = new AssertExp( | |
1095 endloc, | |
1096 new IntegerExp(0), | |
1097 new StringExp(loc, "missing return expression") | |
1098 ); | |
1099 } | |
1100 else | |
1101 e = new HaltExp(endloc); | |
1102 e = new CommaExp(0, e, type->nextOf()->defaultInit()); | |
1103 e = e->semantic(sc2); | |
1104 Statement *s = new ExpStatement(0, e); | |
1105 fbody = new CompoundStatement(0, fbody, s); | |
1106 } | |
1107 } | |
1108 } | |
1109 } | |
1110 | |
1111 { | |
1112 Statements *a = new Statements(); | |
1113 | |
1114 // Merge in initialization of 'out' parameters | |
1115 if (parameters) | |
1116 { for (size_t i = 0; i < parameters->dim; i++) | |
1117 { | |
1118 VarDeclaration *v = (VarDeclaration *)parameters->data[i]; | |
1119 if (v->storage_class & STCout) | |
1120 { | |
1121 assert(v->init); | |
1122 ExpInitializer *ie = v->init->isExpInitializer(); | |
1123 assert(ie); | |
1124 a->push(new ExpStatement(0, ie->exp)); | |
1125 } | |
1126 } | |
1127 } | |
1128 | |
1129 // we'll handle variadics ourselves | |
1130 #if !IN_LLVM | |
1131 if (argptr) | |
1132 { // Initialize _argptr to point past non-variadic arg | |
1133 #if IN_GCC | |
1134 // Handled in FuncDeclaration::toObjFile | |
1135 v_argptr = argptr; | |
1136 v_argptr->init = new VoidInitializer(loc); | |
1137 #else | |
1138 Expression *e1; | |
1139 Expression *e; | |
1140 Type *t = argptr->type; | |
1141 VarDeclaration *p; | |
1142 unsigned offset; | |
1143 | |
1144 e1 = new VarExp(0, argptr); | |
1145 if (parameters && parameters->dim) | |
1146 p = (VarDeclaration *)parameters->data[parameters->dim - 1]; | |
1147 else | |
1148 p = v_arguments; // last parameter is _arguments[] | |
1149 offset = p->type->size(); | |
1150 offset = (offset + 3) & ~3; // assume stack aligns on 4 | |
1151 e = new SymOffExp(0, p, offset); | |
1152 e = new AssignExp(0, e1, e); | |
1153 e->type = t; | |
1154 a->push(new ExpStatement(0, e)); | |
1155 #endif // IN_GCC | |
1156 } | |
1157 | |
1158 if (_arguments) | |
1159 { | |
1160 /* Advance to elements[] member of TypeInfo_Tuple with: | |
1161 * _arguments = v_arguments.elements; | |
1162 */ | |
1163 Expression *e = new VarExp(0, v_arguments); | |
1164 e = new DotIdExp(0, e, Id::elements); | |
1165 Expression *e1 = new VarExp(0, _arguments); | |
1166 e = new AssignExp(0, e1, e); | |
1167 e = e->semantic(sc); | |
1168 a->push(new ExpStatement(0, e)); | |
1169 } | |
1170 | |
1171 #endif // !IN_LLVM | |
1172 | |
1173 // Merge contracts together with body into one compound statement | |
1174 | |
1175 #ifdef _DH | |
1176 if (frequire && global.params.useIn) | |
1177 { frequire->incontract = 1; | |
1178 a->push(frequire); | |
1179 } | |
1180 #else | |
1181 if (frequire && global.params.useIn) | |
1182 a->push(frequire); | |
1183 #endif | |
1184 | |
1185 // Precondition invariant | |
1186 if (addPreInvariant()) | |
1187 { | |
1188 Expression *e = NULL; | |
1189 if (isDtorDeclaration()) | |
1190 { | |
1191 // Call invariant directly only if it exists | |
1192 InvariantDeclaration *inv = ad->inv; | |
1193 ClassDeclaration *cd = ad->isClassDeclaration(); | |
1194 | |
1195 while (!inv && cd) | |
1196 { | |
1197 cd = cd->baseClass; | |
1198 if (!cd) | |
1199 break; | |
1200 inv = cd->inv; | |
1201 } | |
1202 if (inv) | |
1203 { | |
1204 e = new DsymbolExp(0, inv); | |
1205 e = new CallExp(0, e); | |
1206 e = e->semantic(sc2); | |
1207 } | |
1208 } | |
1209 else | |
1210 { // Call invariant virtually | |
1211 ThisExp *v = new ThisExp(0); | |
1212 v->type = vthis->type; | |
1213 Expression *se = new StringExp(0, "null this"); | |
1214 se = se->semantic(sc); | |
1215 se->type = Type::tchar->arrayOf(); | |
1216 e = new AssertExp(loc, v, se); | |
1217 } | |
1218 if (e) | |
1219 { | |
1220 ExpStatement *s = new ExpStatement(0, e); | |
1221 a->push(s); | |
1222 } | |
1223 } | |
1224 | |
1225 if (fbody) | |
1226 a->push(fbody); | |
1227 | |
1228 if (fensure) | |
1229 { | |
1230 a->push(returnLabel->statement); | |
1231 | |
1232 if (type->nextOf()->ty != Tvoid) | |
1233 { | |
1234 // Create: return vresult; | |
1235 assert(vresult); | |
1236 Expression *e = new VarExp(0, vresult); | |
1237 if (tintro) | |
1238 { e = e->implicitCastTo(sc, tintro->nextOf()); | |
1239 e = e->semantic(sc); | |
1240 } | |
1241 ReturnStatement *s = new ReturnStatement(0, e); | |
1242 a->push(s); | |
1243 } | |
1244 } | |
1245 | |
1246 fbody = new CompoundStatement(0, a); | |
1247 } | |
1248 | |
1249 sc2->callSuper = 0; | |
1250 sc2->pop(); | |
1251 } | |
1252 semanticRun = 2; | |
1253 } | |
1254 | |
1255 void FuncDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
1256 { | |
1257 //printf("FuncDeclaration::toCBuffer() '%s'\n", toChars()); | |
1258 | |
1259 type->toCBuffer(buf, ident, hgs); | |
1260 bodyToCBuffer(buf, hgs); | |
1261 } | |
1262 | |
1263 | |
1264 void FuncDeclaration::bodyToCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
1265 { | |
1266 if (fbody && | |
1267 (!hgs->hdrgen || hgs->tpltMember || canInline(1,1)) | |
1268 ) | |
1269 { buf->writenl(); | |
1270 | |
1271 // in{} | |
1272 if (frequire) | |
1273 { buf->writestring("in"); | |
1274 buf->writenl(); | |
1275 frequire->toCBuffer(buf, hgs); | |
1276 } | |
1277 | |
1278 // out{} | |
1279 if (fensure) | |
1280 { buf->writestring("out"); | |
1281 if (outId) | |
1282 { buf->writebyte('('); | |
1283 buf->writestring(outId->toChars()); | |
1284 buf->writebyte(')'); | |
1285 } | |
1286 buf->writenl(); | |
1287 fensure->toCBuffer(buf, hgs); | |
1288 } | |
1289 | |
1290 if (frequire || fensure) | |
1291 { buf->writestring("body"); | |
1292 buf->writenl(); | |
1293 } | |
1294 | |
1295 buf->writebyte('{'); | |
1296 buf->writenl(); | |
1297 fbody->toCBuffer(buf, hgs); | |
1298 buf->writebyte('}'); | |
1299 buf->writenl(); | |
1300 } | |
1301 else | |
1302 { buf->writeByte(';'); | |
1303 buf->writenl(); | |
1304 } | |
1305 } | |
1306 | |
1307 /**************************************************** | |
1308 * Determine if 'this' overrides fd. | |
1309 * Return !=0 if it does. | |
1310 */ | |
1311 | |
1312 int FuncDeclaration::overrides(FuncDeclaration *fd) | |
1313 { int result = 0; | |
1314 | |
1315 if (fd->ident == ident) | |
1316 { | |
1317 int cov = type->covariant(fd->type); | |
1318 if (cov) | |
1319 { ClassDeclaration *cd1 = toParent()->isClassDeclaration(); | |
1320 ClassDeclaration *cd2 = fd->toParent()->isClassDeclaration(); | |
1321 | |
1322 if (cd1 && cd2 && cd2->isBaseOf(cd1, NULL)) | |
1323 result = 1; | |
1324 } | |
1325 } | |
1326 return result; | |
1327 } | |
1328 | |
1329 /************************************************* | |
1330 * Find index of function in vtbl[0..dim] that | |
1331 * this function overrides. | |
1332 * Returns: | |
1333 * -1 didn't find one | |
1334 * -2 can't determine because of forward references | |
1335 */ | |
1336 | |
1337 int FuncDeclaration::findVtblIndex(Array *vtbl, int dim) | |
1338 { | |
1339 for (int vi = 0; vi < dim; vi++) | |
1340 { | |
1341 FuncDeclaration *fdv = ((Dsymbol *)vtbl->data[vi])->isFuncDeclaration(); | |
1342 if (fdv && fdv->ident == ident) | |
1343 { | |
1344 int cov = type->covariant(fdv->type); | |
1345 //printf("\tbaseclass cov = %d\n", cov); | |
1346 switch (cov) | |
1347 { | |
1348 case 0: // types are distinct | |
1349 break; | |
1350 | |
1351 case 1: | |
1352 return vi; | |
1353 | |
1354 case 2: | |
1355 //type->print(); | |
1356 //fdv->type->print(); | |
1357 //printf("%s %s\n", type->deco, fdv->type->deco); | |
1358 error("of type %s overrides but is not covariant with %s of type %s", | |
1359 type->toChars(), fdv->toPrettyChars(), fdv->type->toChars()); | |
1360 break; | |
1361 | |
1362 case 3: | |
1363 return -2; // forward references | |
1364 | |
1365 default: | |
1366 assert(0); | |
1367 } | |
1368 } | |
1369 } | |
1370 return -1; | |
1371 } | |
1372 | |
1373 /**************************************************** | |
1374 * Overload this FuncDeclaration with the new one f. | |
1375 * Return !=0 if successful; i.e. no conflict. | |
1376 */ | |
1377 | |
1378 int FuncDeclaration::overloadInsert(Dsymbol *s) | |
1379 { | |
1380 FuncDeclaration *f; | |
1381 AliasDeclaration *a; | |
1382 | |
1383 //printf("FuncDeclaration::overloadInsert(%s)\n", s->toChars()); | |
1384 a = s->isAliasDeclaration(); | |
1385 if (a) | |
1386 { | |
1387 if (overnext) | |
1388 return overnext->overloadInsert(a); | |
1389 if (!a->aliassym && a->type->ty != Tident && a->type->ty != Tinstance) | |
1390 { | |
1391 //printf("\ta = '%s'\n", a->type->toChars()); | |
1392 return FALSE; | |
1393 } | |
1394 overnext = a; | |
1395 //printf("\ttrue: no conflict\n"); | |
1396 return TRUE; | |
1397 } | |
1398 f = s->isFuncDeclaration(); | |
1399 if (!f) | |
1400 return FALSE; | |
1401 | |
1402 if (type && f->type && // can be NULL for overloaded constructors | |
1403 f->type->covariant(type) && | |
1404 !isFuncAliasDeclaration()) | |
1405 { | |
1406 //printf("\tfalse: conflict %s\n", kind()); | |
1407 return FALSE; | |
1408 } | |
1409 | |
1410 if (overnext) | |
1411 return overnext->overloadInsert(f); | |
1412 overnext = f; | |
1413 //printf("\ttrue: no conflict\n"); | |
1414 return TRUE; | |
1415 } | |
1416 | |
1417 /******************************************** | |
1418 * Find function in overload list that exactly matches t. | |
1419 */ | |
1420 | |
1421 /*************************************************** | |
1422 * Visit each overloaded function in turn, and call | |
1423 * (*fp)(param, f) on it. | |
1424 * Exit when no more, or (*fp)(param, f) returns 1. | |
1425 * Returns: | |
1426 * 0 continue | |
1427 * 1 done | |
1428 */ | |
1429 | |
1430 int overloadApply(FuncDeclaration *fstart, | |
1431 int (*fp)(void *, FuncDeclaration *), | |
1432 void *param) | |
1433 { | |
1434 FuncDeclaration *f; | |
1435 Declaration *d; | |
1436 Declaration *next; | |
1437 | |
1438 for (d = fstart; d; d = next) | |
1439 { FuncAliasDeclaration *fa = d->isFuncAliasDeclaration(); | |
1440 | |
1441 if (fa) | |
1442 { | |
1443 if (overloadApply(fa->funcalias, fp, param)) | |
1444 return 1; | |
1445 next = fa->overnext; | |
1446 } | |
1447 else | |
1448 { | |
1449 AliasDeclaration *a = d->isAliasDeclaration(); | |
1450 | |
1451 if (a) | |
1452 { | |
1453 Dsymbol *s = a->toAlias(); | |
1454 next = s->isDeclaration(); | |
1455 if (next == a) | |
1456 break; | |
1457 if (next == fstart) | |
1458 break; | |
1459 } | |
1460 else | |
1461 { | |
1462 f = d->isFuncDeclaration(); | |
1463 if (!f) | |
1464 { d->error("is aliased to a function"); | |
1465 break; // BUG: should print error message? | |
1466 } | |
1467 if ((*fp)(param, f)) | |
1468 return 1; | |
1469 | |
1470 next = f->overnext; | |
1471 } | |
1472 } | |
1473 } | |
1474 return 0; | |
1475 } | |
1476 | |
1477 /******************************************** | |
1478 * Find function in overload list that exactly matches t. | |
1479 */ | |
1480 | |
1481 struct Param1 | |
1482 { | |
1483 Type *t; // type to match | |
1484 FuncDeclaration *f; // return value | |
1485 }; | |
1486 | |
1487 int fp1(void *param, FuncDeclaration *f) | |
1488 { Param1 *p = (Param1 *)param; | |
1489 Type *t = p->t; | |
1490 | |
1491 if (t->equals(f->type)) | |
1492 { p->f = f; | |
1493 return 1; | |
1494 } | |
1495 | |
336 | 1496 #if DMDV2 |
159 | 1497 /* Allow covariant matches, if it's just a const conversion |
1498 * of the return type | |
1499 */ | |
1500 if (t->ty == Tfunction) | |
1501 { TypeFunction *tf = (TypeFunction *)f->type; | |
1502 if (tf->covariant(t) == 1 && | |
1503 tf->nextOf()->implicitConvTo(t->nextOf()) >= MATCHconst) | |
1504 { | |
1505 p->f = f; | |
1506 return 1; | |
1507 } | |
1508 } | |
1509 #endif | |
1510 return 0; | |
1511 } | |
1512 | |
1513 FuncDeclaration *FuncDeclaration::overloadExactMatch(Type *t) | |
1514 { | |
1515 Param1 p; | |
1516 p.t = t; | |
1517 p.f = NULL; | |
1518 overloadApply(this, &fp1, &p); | |
1519 return p.f; | |
1520 } | |
1521 | |
1522 #if 0 | |
1523 FuncDeclaration *FuncDeclaration::overloadExactMatch(Type *t) | |
1524 { | |
1525 FuncDeclaration *f; | |
1526 Declaration *d; | |
1527 Declaration *next; | |
1528 | |
1529 for (d = this; d; d = next) | |
1530 { FuncAliasDeclaration *fa = d->isFuncAliasDeclaration(); | |
1531 | |
1532 if (fa) | |
1533 { | |
1534 FuncDeclaration *f2 = fa->funcalias->overloadExactMatch(t); | |
1535 if (f2) | |
1536 return f2; | |
1537 next = fa->overnext; | |
1538 } | |
1539 else | |
1540 { | |
1541 AliasDeclaration *a = d->isAliasDeclaration(); | |
1542 | |
1543 if (a) | |
1544 { | |
1545 Dsymbol *s = a->toAlias(); | |
1546 next = s->isDeclaration(); | |
1547 if (next == a) | |
1548 break; | |
1549 } | |
1550 else | |
1551 { | |
1552 f = d->isFuncDeclaration(); | |
1553 if (!f) | |
1554 break; // BUG: should print error message? | |
1555 if (t->equals(d->type)) | |
1556 return f; | |
1557 next = f->overnext; | |
1558 } | |
1559 } | |
1560 } | |
1561 return NULL; | |
1562 } | |
1563 #endif | |
1564 | |
1565 /******************************************** | |
1566 * Decide which function matches the arguments best. | |
1567 */ | |
1568 | |
1569 struct Param2 | |
1570 { | |
1571 Match *m; | |
1572 Expressions *arguments; | |
1573 }; | |
1574 | |
1575 int fp2(void *param, FuncDeclaration *f) | |
1576 { Param2 *p = (Param2 *)param; | |
1577 Match *m = p->m; | |
1578 Expressions *arguments = p->arguments; | |
1579 MATCH match; | |
1580 | |
1581 if (f != m->lastf) // skip duplicates | |
1582 { | |
1583 m->anyf = f; | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
468
diff
changeset
|
1584 TypeFunction *tf = (TypeFunction *)f->type; |
159 | 1585 match = (MATCH) tf->callMatch(arguments); |
717
a26b0c5d5942
Merged DMD 1.036.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
703
diff
changeset
|
1586 //printf("1match = %d\n", match); |
159 | 1587 if (match != MATCHnomatch) |
1588 { | |
1589 if (match > m->last) | |
1590 goto LfIsBetter; | |
1591 | |
1592 if (match < m->last) | |
1593 goto LlastIsBetter; | |
1594 | |
1595 /* See if one of the matches overrides the other. | |
1596 */ | |
1597 if (m->lastf->overrides(f)) | |
1598 goto LlastIsBetter; | |
1599 else if (f->overrides(m->lastf)) | |
1600 goto LfIsBetter; | |
1601 | |
1602 Lambiguous: | |
1603 m->nextf = f; | |
1604 m->count++; | |
1605 return 0; | |
1606 | |
1607 LfIsBetter: | |
1608 m->last = match; | |
1609 m->lastf = f; | |
1610 m->count = 1; | |
1611 return 0; | |
1612 | |
1613 LlastIsBetter: | |
1614 return 0; | |
1615 } | |
1616 } | |
1617 return 0; | |
1618 } | |
1619 | |
1620 | |
1621 void overloadResolveX(Match *m, FuncDeclaration *fstart, Expressions *arguments) | |
1622 { | |
1623 Param2 p; | |
1624 p.m = m; | |
1625 p.arguments = arguments; | |
1626 overloadApply(fstart, &fp2, &p); | |
1627 } | |
1628 | |
1629 #if 0 | |
1630 // Recursive helper function | |
1631 | |
1632 void overloadResolveX(Match *m, FuncDeclaration *fstart, Expressions *arguments) | |
1633 { | |
1634 MATCH match; | |
1635 Declaration *d; | |
1636 Declaration *next; | |
1637 | |
1638 for (d = fstart; d; d = next) | |
1639 { | |
1640 FuncDeclaration *f; | |
1641 FuncAliasDeclaration *fa; | |
1642 AliasDeclaration *a; | |
1643 | |
1644 fa = d->isFuncAliasDeclaration(); | |
1645 if (fa) | |
1646 { | |
1647 overloadResolveX(m, fa->funcalias, arguments); | |
1648 next = fa->overnext; | |
1649 } | |
1650 else if ((f = d->isFuncDeclaration()) != NULL) | |
1651 { | |
1652 next = f->overnext; | |
1653 if (f == m->lastf) | |
1654 continue; // skip duplicates | |
1655 else | |
1656 { | |
1657 TypeFunction *tf; | |
1658 | |
1659 m->anyf = f; | |
1660 tf = (TypeFunction *)f->type; | |
1661 match = (MATCH) tf->callMatch(arguments); | |
717
a26b0c5d5942
Merged DMD 1.036.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
703
diff
changeset
|
1662 //printf("2match = %d\n", match); |
159 | 1663 if (match != MATCHnomatch) |
1664 { | |
1665 if (match > m->last) | |
1666 goto LfIsBetter; | |
1667 | |
1668 if (match < m->last) | |
1669 goto LlastIsBetter; | |
1670 | |
1671 /* See if one of the matches overrides the other. | |
1672 */ | |
1673 if (m->lastf->overrides(f)) | |
1674 goto LlastIsBetter; | |
1675 else if (f->overrides(m->lastf)) | |
1676 goto LfIsBetter; | |
1677 | |
1678 Lambiguous: | |
1679 m->nextf = f; | |
1680 m->count++; | |
1681 continue; | |
1682 | |
1683 LfIsBetter: | |
1684 m->last = match; | |
1685 m->lastf = f; | |
1686 m->count = 1; | |
1687 continue; | |
1688 | |
1689 LlastIsBetter: | |
1690 continue; | |
1691 } | |
1692 } | |
1693 } | |
1694 else if ((a = d->isAliasDeclaration()) != NULL) | |
1695 { | |
1696 Dsymbol *s = a->toAlias(); | |
1697 next = s->isDeclaration(); | |
1698 if (next == a) | |
1699 break; | |
1700 if (next == fstart) | |
1701 break; | |
1702 } | |
1703 else | |
1704 { d->error("is aliased to a function"); | |
1705 break; | |
1706 } | |
1707 } | |
1708 } | |
1709 #endif | |
1710 | |
1711 FuncDeclaration *FuncDeclaration::overloadResolve(Loc loc, Expressions *arguments) | |
1712 { | |
1713 TypeFunction *tf; | |
1714 Match m; | |
1715 | |
1716 #if 0 | |
1717 printf("FuncDeclaration::overloadResolve('%s')\n", toChars()); | |
1718 if (arguments) | |
1719 { int i; | |
1720 | |
1721 for (i = 0; i < arguments->dim; i++) | |
1722 { Expression *arg; | |
1723 | |
1724 arg = (Expression *)arguments->data[i]; | |
1725 assert(arg->type); | |
1726 printf("\t%s: ", arg->toChars()); | |
1727 arg->type->print(); | |
1728 } | |
1729 } | |
1730 #endif | |
1731 | |
1732 memset(&m, 0, sizeof(m)); | |
1733 m.last = MATCHnomatch; | |
1734 overloadResolveX(&m, this, arguments); | |
1735 | |
1736 if (m.count == 1) // exactly one match | |
1737 { | |
1738 return m.lastf; | |
1739 } | |
1740 else | |
1741 { | |
1742 OutBuffer buf; | |
1743 | |
1744 if (arguments) | |
1745 { | |
1746 HdrGenState hgs; | |
1747 | |
1748 argExpTypesToCBuffer(&buf, arguments, &hgs); | |
1749 } | |
1750 | |
1751 if (m.last == MATCHnomatch) | |
1752 { | |
1753 tf = (TypeFunction *)type; | |
1754 | |
1755 //printf("tf = %s, args = %s\n", tf->deco, ((Expression *)arguments->data[0])->type->deco); | |
1756 error(loc, "%s does not match parameter types (%s)", | |
1757 Argument::argsTypesToChars(tf->parameters, tf->varargs), | |
1758 buf.toChars()); | |
1759 return m.anyf; // as long as it's not a FuncAliasDeclaration | |
1760 } | |
1761 else | |
1762 { | |
1763 #if 1 | |
1764 TypeFunction *t1 = (TypeFunction *)m.lastf->type; | |
1765 TypeFunction *t2 = (TypeFunction *)m.nextf->type; | |
1766 | |
1767 error(loc, "called with argument types:\n\t(%s)\nmatches both:\n\t%s%s\nand:\n\t%s%s", | |
1768 buf.toChars(), | |
1769 m.lastf->toPrettyChars(), Argument::argsTypesToChars(t1->parameters, t1->varargs), | |
1770 m.nextf->toPrettyChars(), Argument::argsTypesToChars(t2->parameters, t2->varargs)); | |
1771 #else | |
1772 error(loc, "overloads %s and %s both match argument list for %s", | |
1773 m.lastf->type->toChars(), | |
1774 m.nextf->type->toChars(), | |
1775 m.lastf->toChars()); | |
1776 #endif | |
1777 return m.lastf; | |
1778 } | |
1779 } | |
1780 } | |
1781 | |
1782 /******************************** | |
1783 * Labels are in a separate scope, one per function. | |
1784 */ | |
1785 | |
1786 LabelDsymbol *FuncDeclaration::searchLabel(Identifier *ident) | |
1787 { Dsymbol *s; | |
1788 | |
1789 if (!labtab) | |
1790 labtab = new DsymbolTable(); // guess we need one | |
1791 | |
1792 s = labtab->lookup(ident); | |
1793 if (!s) | |
1794 { | |
1795 s = new LabelDsymbol(ident); | |
1796 labtab->insert(s); | |
1797 } | |
1798 return (LabelDsymbol *)s; | |
1799 } | |
336 | 1800 |
159 | 1801 /**************************************** |
1802 * If non-static member function that has a 'this' pointer, | |
1803 * return the aggregate it is a member of. | |
1804 * Otherwise, return NULL. | |
1805 */ | |
1806 | |
1807 AggregateDeclaration *FuncDeclaration::isThis() | |
1808 { AggregateDeclaration *ad; | |
1809 | |
1810 //printf("+FuncDeclaration::isThis() '%s'\n", toChars()); | |
1811 ad = NULL; | |
1812 if ((storage_class & STCstatic) == 0) | |
1813 { | |
1814 ad = isMember2(); | |
1815 } | |
1816 //printf("-FuncDeclaration::isThis() %p\n", ad); | |
1817 return ad; | |
1818 } | |
1819 | |
1820 AggregateDeclaration *FuncDeclaration::isMember2() | |
1821 { AggregateDeclaration *ad; | |
1822 | |
1823 //printf("+FuncDeclaration::isMember2() '%s'\n", toChars()); | |
1824 ad = NULL; | |
1825 for (Dsymbol *s = this; s; s = s->parent) | |
1826 { | |
1827 //printf("\ts = '%s', parent = '%s', kind = %s\n", s->toChars(), s->parent->toChars(), s->parent->kind()); | |
1828 ad = s->isMember(); | |
1829 if (ad) | |
1830 { //printf("test4\n"); | |
1831 break; | |
1832 } | |
1833 if (!s->parent || | |
1834 (!s->parent->isTemplateInstance())) | |
1835 { //printf("test5\n"); | |
1836 break; | |
1837 } | |
1838 } | |
1839 //printf("-FuncDeclaration::isMember2() %p\n", ad); | |
1840 return ad; | |
1841 } | |
1842 | |
1843 /***************************************** | |
1844 * Determine lexical level difference from 'this' to nested function 'fd'. | |
1845 * Error if this cannot call fd. | |
1846 * Returns: | |
1847 * 0 same level | |
1848 * -1 increase nesting by 1 (fd is nested within 'this') | |
1849 * >0 decrease nesting by number | |
1850 */ | |
1851 | |
1852 int FuncDeclaration::getLevel(Loc loc, FuncDeclaration *fd) | |
1853 { int level; | |
1854 Dsymbol *s; | |
1855 Dsymbol *fdparent; | |
1856 | |
1857 //printf("FuncDeclaration::getLevel(fd = '%s')\n", fd->toChars()); | |
1858 fdparent = fd->toParent2(); | |
1859 if (fdparent == this) | |
1860 return -1; | |
1861 s = this; | |
1862 level = 0; | |
1863 while (fd != s && fdparent != s->toParent2()) | |
1864 { | |
1865 //printf("\ts = '%s'\n", s->toChars()); | |
1866 FuncDeclaration *thisfd = s->isFuncDeclaration(); | |
1867 if (thisfd) | |
1868 { if (!thisfd->isNested() && !thisfd->vthis) | |
1869 goto Lerr; | |
1870 } | |
1871 else | |
1872 { | |
1873 ClassDeclaration *thiscd = s->isClassDeclaration(); | |
1874 if (thiscd) | |
1875 { if (!thiscd->isNested()) | |
1876 goto Lerr; | |
1877 } | |
1878 else | |
1879 goto Lerr; | |
1880 } | |
1881 | |
1882 s = s->toParent2(); | |
1883 assert(s); | |
1884 level++; | |
1885 } | |
1886 return level; | |
1887 | |
1888 Lerr: | |
1889 error(loc, "cannot access frame of function %s", fd->toChars()); | |
1890 return 1; | |
1891 } | |
1892 | |
1893 void FuncDeclaration::appendExp(Expression *e) | |
1894 { Statement *s; | |
1895 | |
1896 s = new ExpStatement(0, e); | |
1897 appendState(s); | |
1898 } | |
1899 | |
1900 void FuncDeclaration::appendState(Statement *s) | |
1901 { CompoundStatement *cs; | |
1902 | |
1903 if (!fbody) | |
1904 { Statements *a; | |
1905 | |
1906 a = new Statements(); | |
1907 fbody = new CompoundStatement(0, a); | |
1908 } | |
1909 cs = fbody->isCompoundStatement(); | |
1910 cs->statements->push(s); | |
1911 } | |
1912 | |
1913 | |
1914 int FuncDeclaration::isMain() | |
1915 { | |
1916 return ident == Id::main && | |
1917 linkage != LINKc && !isMember() && !isNested(); | |
1918 } | |
1919 | |
1920 int FuncDeclaration::isWinMain() | |
1921 { | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
468
diff
changeset
|
1922 //printf("FuncDeclaration::isWinMain() %s\n", toChars()); |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
468
diff
changeset
|
1923 #if 0 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
468
diff
changeset
|
1924 int x = ident == Id::WinMain && |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
468
diff
changeset
|
1925 linkage != LINKc && !isMember(); |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
468
diff
changeset
|
1926 printf("%s\n", x ? "yes" : "no"); |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
468
diff
changeset
|
1927 return x; |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
468
diff
changeset
|
1928 #else |
159 | 1929 return ident == Id::WinMain && |
1930 linkage != LINKc && !isMember(); | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
468
diff
changeset
|
1931 #endif |
159 | 1932 } |
1933 | |
1934 int FuncDeclaration::isDllMain() | |
1935 { | |
1936 return ident == Id::DllMain && | |
1937 linkage != LINKc && !isMember(); | |
1938 } | |
1939 | |
1940 int FuncDeclaration::isExport() | |
1941 { | |
1942 return protection == PROTexport; | |
1943 } | |
1944 | |
1945 int FuncDeclaration::isImportedSymbol() | |
1946 { | |
1947 //printf("isImportedSymbol()\n"); | |
1948 //printf("protection = %d\n", protection); | |
1949 return (protection == PROTexport) && !fbody; | |
1950 } | |
1951 | |
1952 // Determine if function goes into virtual function pointer table | |
1953 | |
1954 int FuncDeclaration::isVirtual() | |
1955 { | |
1956 #if 0 | |
1957 printf("FuncDeclaration::isVirtual(%s)\n", toChars()); | |
1958 printf("%p %d %d %d %d\n", isMember(), isStatic(), protection == PROTprivate, isCtorDeclaration(), linkage != LINKd); | |
1959 printf("result is %d\n", | |
1960 isMember() && | |
1961 !(isStatic() || protection == PROTprivate || protection == PROTpackage) && | |
1962 toParent()->isClassDeclaration()); | |
1963 #endif | |
1964 return isMember() && | |
1965 !(isStatic() || protection == PROTprivate || protection == PROTpackage) && | |
1966 toParent()->isClassDeclaration(); | |
1967 } | |
1968 | |
846
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
717
diff
changeset
|
1969 int FuncDeclaration::isFinal() |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
717
diff
changeset
|
1970 { |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
717
diff
changeset
|
1971 ClassDeclaration *cd; |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
717
diff
changeset
|
1972 #if 0 |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
717
diff
changeset
|
1973 printf("FuncDeclaration::isFinal(%s)\n", toChars()); |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
717
diff
changeset
|
1974 printf("%p %d %d %d %d\n", isMember(), isStatic(), protection == PROTprivate, isCtorDeclaration(), linkage != LINKd); |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
717
diff
changeset
|
1975 printf("result is %d\n", |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
717
diff
changeset
|
1976 isMember() && |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
717
diff
changeset
|
1977 !(isStatic() || protection == PROTprivate || protection == PROTpackage) && |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
717
diff
changeset
|
1978 (cd = toParent()->isClassDeclaration()) != NULL && |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
717
diff
changeset
|
1979 cd->storage_class & STCfinal); |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
717
diff
changeset
|
1980 #endif |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
717
diff
changeset
|
1981 return isMember() && |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
717
diff
changeset
|
1982 (Declaration::isFinal() || |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
717
diff
changeset
|
1983 ((cd = toParent()->isClassDeclaration()) != NULL && cd->storage_class & STCfinal)); |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
717
diff
changeset
|
1984 } |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
717
diff
changeset
|
1985 |
159 | 1986 int FuncDeclaration::isAbstract() |
1987 { | |
1988 return storage_class & STCabstract; | |
1989 } | |
1990 | |
1991 int FuncDeclaration::isCodeseg() | |
1992 { | |
1993 return TRUE; // functions are always in the code segment | |
1994 } | |
1995 | |
1996 // Determine if function needs | |
1997 // a static frame pointer to its lexically enclosing function | |
1998 | |
1999 int FuncDeclaration::isNested() | |
2000 { | |
2001 //if (!toParent()) | |
2002 //printf("FuncDeclaration::isNested('%s') parent=%p\n", toChars(), parent); | |
2003 //printf("\ttoParent() = '%s'\n", toParent()->toChars()); | |
2004 return ((storage_class & STCstatic) == 0) && | |
2005 (toParent2()->isFuncDeclaration() != NULL); | |
2006 } | |
2007 | |
2008 int FuncDeclaration::needThis() | |
2009 { | |
2010 //printf("FuncDeclaration::needThis() '%s'\n", toChars()); | |
2011 int i = isThis() != NULL; | |
2012 //printf("\t%d\n", i); | |
2013 if (!i && isFuncAliasDeclaration()) | |
2014 i = ((FuncAliasDeclaration *)this)->funcalias->needThis(); | |
2015 return i; | |
2016 } | |
2017 | |
2018 int FuncDeclaration::addPreInvariant() | |
2019 { | |
2020 AggregateDeclaration *ad = isThis(); | |
2021 return (ad && | |
2022 //ad->isClassDeclaration() && | |
2023 global.params.useInvariants && | |
2024 (protection == PROTpublic || protection == PROTexport) && | |
2025 !naked); | |
2026 } | |
2027 | |
2028 int FuncDeclaration::addPostInvariant() | |
2029 { | |
2030 AggregateDeclaration *ad = isThis(); | |
2031 return (ad && | |
2032 ad->inv && | |
2033 //ad->isClassDeclaration() && | |
2034 global.params.useInvariants && | |
2035 (protection == PROTpublic || protection == PROTexport) && | |
2036 !naked); | |
2037 } | |
2038 | |
2039 /********************************** | |
2040 * Generate a FuncDeclaration for a runtime library function. | |
2041 */ | |
2042 | |
378
d8234836b40f
Get rid of runTimeHack and instead add proper argument info to the frontend
Christian Kamm <kamm incasoftware de>
parents:
336
diff
changeset
|
2043 // |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
658
diff
changeset
|
2044 // LDC: Adjusted to give argument info to the runtime function decl. |
378
d8234836b40f
Get rid of runTimeHack and instead add proper argument info to the frontend
Christian Kamm <kamm incasoftware de>
parents:
336
diff
changeset
|
2045 // |
d8234836b40f
Get rid of runTimeHack and instead add proper argument info to the frontend
Christian Kamm <kamm incasoftware de>
parents:
336
diff
changeset
|
2046 |
d8234836b40f
Get rid of runTimeHack and instead add proper argument info to the frontend
Christian Kamm <kamm incasoftware de>
parents:
336
diff
changeset
|
2047 FuncDeclaration *FuncDeclaration::genCfunc(Arguments *args, Type *treturn, char *name) |
159 | 2048 { |
378
d8234836b40f
Get rid of runTimeHack and instead add proper argument info to the frontend
Christian Kamm <kamm incasoftware de>
parents:
336
diff
changeset
|
2049 return genCfunc(args, treturn, Lexer::idPool(name)); |
159 | 2050 } |
2051 | |
378
d8234836b40f
Get rid of runTimeHack and instead add proper argument info to the frontend
Christian Kamm <kamm incasoftware de>
parents:
336
diff
changeset
|
2052 FuncDeclaration *FuncDeclaration::genCfunc(Arguments *args, Type *treturn, Identifier *id) |
159 | 2053 { |
2054 FuncDeclaration *fd; | |
2055 TypeFunction *tf; | |
2056 Dsymbol *s; | |
2057 static DsymbolTable *st = NULL; | |
2058 | |
2059 //printf("genCfunc(name = '%s')\n", id->toChars()); | |
2060 //printf("treturn\n\t"); treturn->print(); | |
2061 | |
2062 // See if already in table | |
2063 if (!st) | |
2064 st = new DsymbolTable(); | |
2065 s = st->lookup(id); | |
2066 if (s) | |
2067 { | |
2068 fd = s->isFuncDeclaration(); | |
2069 assert(fd); | |
2070 assert(fd->type->nextOf()->equals(treturn)); | |
2071 } | |
2072 else | |
2073 { | |
378
d8234836b40f
Get rid of runTimeHack and instead add proper argument info to the frontend
Christian Kamm <kamm incasoftware de>
parents:
336
diff
changeset
|
2074 tf = new TypeFunction(args, treturn, 0, LINKc); |
159 | 2075 fd = new FuncDeclaration(0, 0, id, STCstatic, tf); |
2076 fd->protection = PROTpublic; | |
2077 fd->linkage = LINKc; | |
2078 | |
2079 st->insert(fd); | |
2080 } | |
2081 return fd; | |
2082 } | |
2083 | |
336 | 2084 const char *FuncDeclaration::kind() |
159 | 2085 { |
2086 return "function"; | |
2087 } | |
336 | 2088 |
159 | 2089 /******************************* |
2090 * Look at all the variables in this function that are referenced | |
2091 * by nested functions, and determine if a closure needs to be | |
2092 * created for them. | |
2093 */ | |
2094 | |
336 | 2095 #if DMDV2 |
159 | 2096 int FuncDeclaration::needsClosure() |
2097 { | |
2098 /* Need a closure for all the closureVars[] if any of the | |
2099 * closureVars[] are accessed by a | |
2100 * function that escapes the scope of this function. | |
2101 * We take the conservative approach and decide that any function that: | |
2102 * 1) is a virtual function | |
2103 * 2) has its address taken | |
2104 * 3) has a parent that escapes | |
336 | 2105 * |
2106 * Note that since a non-virtual function can be called by | |
2107 * a virtual one, if that non-virtual function accesses a closure | |
2108 * var, the closure still has to be taken. Hence, we check for isThis() | |
2109 * instead of isVirtual(). (thanks to David Friedman) | |
159 | 2110 */ |
2111 | |
2112 //printf("FuncDeclaration::needsClosure() %s\n", toChars()); | |
2113 for (int i = 0; i < closureVars.dim; i++) | |
2114 { VarDeclaration *v = (VarDeclaration *)closureVars.data[i]; | |
2115 assert(v->isVarDeclaration()); | |
2116 //printf("\tv = %s\n", v->toChars()); | |
2117 | |
2118 for (int j = 0; j < v->nestedrefs.dim; j++) | |
2119 { FuncDeclaration *f = (FuncDeclaration *)v->nestedrefs.data[j]; | |
2120 assert(f != this); | |
2121 | |
2122 //printf("\t\tf = %s, %d, %d\n", f->toChars(), f->isVirtual(), f->tookAddressOf); | |
336 | 2123 if (f->isThis() || f->tookAddressOf) |
159 | 2124 goto Lyes; // assume f escapes this function's scope |
2125 | |
2126 // Look to see if any parents of f that are below this escape | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
468
diff
changeset
|
2127 for (Dsymbol *s = f->parent; s && s != this; s = s->parent) |
159 | 2128 { |
2129 f = s->isFuncDeclaration(); | |
336 | 2130 if (f && (f->isThis() || f->tookAddressOf)) |
159 | 2131 goto Lyes; |
2132 } | |
2133 } | |
2134 } | |
2135 return 0; | |
2136 | |
2137 Lyes: | |
2138 //printf("\tneeds closure\n"); | |
2139 return 1; | |
2140 } | |
2141 #endif | |
2142 | |
2143 /****************************** FuncAliasDeclaration ************************/ | |
2144 | |
2145 // Used as a way to import a set of functions from another scope into this one. | |
2146 | |
2147 FuncAliasDeclaration::FuncAliasDeclaration(FuncDeclaration *funcalias) | |
2148 : FuncDeclaration(funcalias->loc, funcalias->endloc, funcalias->ident, | |
2149 (enum STC)funcalias->storage_class, funcalias->type) | |
2150 { | |
2151 assert(funcalias != this); | |
2152 this->funcalias = funcalias; | |
2153 } | |
2154 | |
336 | 2155 const char *FuncAliasDeclaration::kind() |
159 | 2156 { |
2157 return "function alias"; | |
2158 } | |
2159 | |
2160 | |
2161 /****************************** FuncLiteralDeclaration ************************/ | |
2162 | |
2163 FuncLiteralDeclaration::FuncLiteralDeclaration(Loc loc, Loc endloc, Type *type, | |
2164 enum TOK tok, ForeachStatement *fes) | |
2165 : FuncDeclaration(loc, endloc, NULL, STCundefined, type) | |
2166 { | |
2167 char *id; | |
2168 | |
2169 if (fes) | |
2170 id = "__foreachbody"; | |
2171 else if (tok == TOKdelegate) | |
2172 id = "__dgliteral"; | |
2173 else | |
2174 id = "__funcliteral"; | |
2175 this->ident = Identifier::generateId(id); | |
2176 this->tok = tok; | |
2177 this->fes = fes; | |
2178 //printf("FuncLiteralDeclaration() id = '%s', type = '%s'\n", this->ident->toChars(), type->toChars()); | |
2179 } | |
2180 | |
2181 Dsymbol *FuncLiteralDeclaration::syntaxCopy(Dsymbol *s) | |
2182 { | |
2183 FuncLiteralDeclaration *f; | |
2184 | |
2185 //printf("FuncLiteralDeclaration::syntaxCopy('%s')\n", toChars()); | |
2186 if (s) | |
2187 f = (FuncLiteralDeclaration *)s; | |
2188 else | |
2189 f = new FuncLiteralDeclaration(loc, endloc, type->syntaxCopy(), tok, fes); | |
2190 FuncDeclaration::syntaxCopy(f); | |
2191 return f; | |
2192 } | |
2193 | |
2194 int FuncLiteralDeclaration::isNested() | |
2195 { | |
2196 //printf("FuncLiteralDeclaration::isNested() '%s'\n", toChars()); | |
2197 return (tok == TOKdelegate); | |
2198 } | |
2199 | |
336 | 2200 int FuncLiteralDeclaration::isVirtual() |
2201 { | |
2202 return FALSE; | |
2203 } | |
2204 | |
2205 const char *FuncLiteralDeclaration::kind() | |
159 | 2206 { |
2207 // GCC requires the (char*) casts | |
2208 return (tok == TOKdelegate) ? (char*)"delegate" : (char*)"function"; | |
2209 } | |
2210 | |
2211 void FuncLiteralDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2212 { | |
2213 static Identifier *idfunc; | |
2214 static Identifier *iddel; | |
2215 | |
2216 if (!idfunc) | |
2217 idfunc = new Identifier("function", 0); | |
2218 if (!iddel) | |
2219 iddel = new Identifier("delegate", 0); | |
2220 | |
2221 type->toCBuffer(buf, ((tok == TOKdelegate) ? iddel : idfunc), hgs); | |
2222 bodyToCBuffer(buf, hgs); | |
2223 } | |
2224 | |
2225 | |
2226 /********************************* CtorDeclaration ****************************/ | |
2227 | |
2228 CtorDeclaration::CtorDeclaration(Loc loc, Loc endloc, Arguments *arguments, int varargs) | |
2229 : FuncDeclaration(loc, endloc, Id::ctor, STCundefined, NULL) | |
2230 { | |
2231 this->arguments = arguments; | |
2232 this->varargs = varargs; | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
468
diff
changeset
|
2233 //printf("CtorDeclaration(loc = %s) %s\n", loc.toChars(), toChars()); |
159 | 2234 } |
2235 | |
2236 Dsymbol *CtorDeclaration::syntaxCopy(Dsymbol *s) | |
2237 { | |
2238 CtorDeclaration *f; | |
2239 | |
2240 f = new CtorDeclaration(loc, endloc, NULL, varargs); | |
2241 | |
2242 f->outId = outId; | |
2243 f->frequire = frequire ? frequire->syntaxCopy() : NULL; | |
2244 f->fensure = fensure ? fensure->syntaxCopy() : NULL; | |
2245 f->fbody = fbody ? fbody->syntaxCopy() : NULL; | |
2246 assert(!fthrows); // deprecated | |
2247 | |
2248 f->arguments = Argument::arraySyntaxCopy(arguments); | |
2249 return f; | |
2250 } | |
2251 | |
2252 | |
2253 void CtorDeclaration::semantic(Scope *sc) | |
2254 { | |
2255 ClassDeclaration *cd; | |
2256 Type *tret; | |
2257 | |
2258 //printf("CtorDeclaration::semantic()\n"); | |
2259 if (type) | |
2260 return; | |
2261 | |
2262 sc = sc->push(); | |
2263 sc->stc &= ~STCstatic; // not a static constructor | |
2264 | |
2265 parent = sc->parent; | |
2266 Dsymbol *parent = toParent(); | |
2267 cd = parent->isClassDeclaration(); | |
2268 if (!cd) | |
2269 { | |
2270 error("constructors are only for class definitions"); | |
333
f7190d9eb70c
[svn r354] Fix the regressions between [346] and [353] by making constructors and destructors outside classes fatal errors.
ChristianK
parents:
305
diff
changeset
|
2271 fatal(); |
159 | 2272 tret = Type::tvoid; |
2273 } | |
2274 else | |
2275 tret = cd->type; //->referenceTo(); | |
2276 type = new TypeFunction(arguments, tret, varargs, LINKd); | |
336 | 2277 if (!originalType) |
2278 originalType = type; | |
159 | 2279 |
2280 sc->flags |= SCOPEctor; | |
2281 type = type->semantic(loc, sc); | |
2282 sc->flags &= ~SCOPEctor; | |
2283 | |
2284 // Append: | |
2285 // return this; | |
2286 // to the function body | |
2287 if (fbody) | |
2288 { Expression *e; | |
2289 Statement *s; | |
2290 | |
2291 e = new ThisExp(0); | |
2292 s = new ReturnStatement(0, e); | |
2293 fbody = new CompoundStatement(0, fbody, s); | |
2294 } | |
2295 | |
2296 FuncDeclaration::semantic(sc); | |
2297 | |
2298 sc->pop(); | |
2299 | |
2300 // See if it's the default constructor | |
2301 if (cd && varargs == 0 && Argument::dim(arguments) == 0) | |
2302 cd->defaultCtor = this; | |
2303 } | |
2304 | |
336 | 2305 const char *CtorDeclaration::kind() |
159 | 2306 { |
2307 return "constructor"; | |
2308 } | |
2309 | |
2310 char *CtorDeclaration::toChars() | |
2311 { | |
2312 return "this"; | |
2313 } | |
2314 | |
2315 int CtorDeclaration::isVirtual() | |
2316 { | |
2317 return FALSE; | |
2318 } | |
2319 | |
2320 int CtorDeclaration::addPreInvariant() | |
2321 { | |
2322 return FALSE; | |
2323 } | |
2324 | |
2325 int CtorDeclaration::addPostInvariant() | |
2326 { | |
2327 return (vthis && global.params.useInvariants); | |
2328 } | |
2329 | |
2330 | |
2331 void CtorDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2332 { | |
2333 buf->writestring("this"); | |
2334 Argument::argsToCBuffer(buf, hgs, arguments, varargs); | |
2335 bodyToCBuffer(buf, hgs); | |
2336 } | |
2337 | |
2338 /********************************* DtorDeclaration ****************************/ | |
2339 | |
2340 DtorDeclaration::DtorDeclaration(Loc loc, Loc endloc) | |
2341 : FuncDeclaration(loc, endloc, Id::dtor, STCundefined, NULL) | |
2342 { | |
2343 } | |
2344 | |
2345 DtorDeclaration::DtorDeclaration(Loc loc, Loc endloc, Identifier *id) | |
2346 : FuncDeclaration(loc, endloc, id, STCundefined, NULL) | |
2347 { | |
2348 } | |
2349 | |
2350 Dsymbol *DtorDeclaration::syntaxCopy(Dsymbol *s) | |
2351 { | |
2352 assert(!s); | |
2353 DtorDeclaration *dd = new DtorDeclaration(loc, endloc, ident); | |
2354 return FuncDeclaration::syntaxCopy(dd); | |
2355 } | |
2356 | |
2357 | |
2358 void DtorDeclaration::semantic(Scope *sc) | |
2359 { | |
2360 ClassDeclaration *cd; | |
2361 | |
2362 parent = sc->parent; | |
2363 Dsymbol *parent = toParent(); | |
2364 cd = parent->isClassDeclaration(); | |
2365 if (!cd) | |
2366 { | |
2367 error("destructors only are for class definitions"); | |
333
f7190d9eb70c
[svn r354] Fix the regressions between [346] and [353] by making constructors and destructors outside classes fatal errors.
ChristianK
parents:
305
diff
changeset
|
2368 fatal(); |
159 | 2369 } |
2370 else | |
2371 cd->dtors.push(this); | |
2372 type = new TypeFunction(NULL, Type::tvoid, FALSE, LINKd); | |
2373 | |
2374 sc = sc->push(); | |
2375 sc->stc &= ~STCstatic; // not a static destructor | |
2376 sc->linkage = LINKd; | |
2377 | |
2378 FuncDeclaration::semantic(sc); | |
2379 | |
2380 sc->pop(); | |
2381 } | |
2382 | |
2383 int DtorDeclaration::overloadInsert(Dsymbol *s) | |
2384 { | |
2385 return FALSE; // cannot overload destructors | |
2386 } | |
2387 | |
2388 int DtorDeclaration::addPreInvariant() | |
2389 { | |
2390 return (vthis && global.params.useInvariants); | |
2391 } | |
2392 | |
2393 int DtorDeclaration::addPostInvariant() | |
2394 { | |
2395 return FALSE; | |
2396 } | |
2397 | |
2398 int DtorDeclaration::isVirtual() | |
2399 { | |
2400 /* This should be FALSE so that dtor's don't get put into the vtbl[], | |
2401 * but doing so will require recompiling everything. | |
2402 */ | |
2403 #if BREAKABI | |
2404 return FALSE; | |
2405 #else | |
2406 return FuncDeclaration::isVirtual(); | |
2407 #endif | |
2408 } | |
2409 | |
2410 void DtorDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2411 { | |
2412 if (hgs->hdrgen) | |
2413 return; | |
2414 buf->writestring("~this()"); | |
2415 bodyToCBuffer(buf, hgs); | |
2416 } | |
2417 | |
2418 /********************************* StaticCtorDeclaration ****************************/ | |
2419 | |
2420 StaticCtorDeclaration::StaticCtorDeclaration(Loc loc, Loc endloc) | |
2421 : FuncDeclaration(loc, endloc, | |
2422 Identifier::generateId("_staticCtor"), STCstatic, NULL) | |
2423 { | |
2424 } | |
2425 | |
2426 Dsymbol *StaticCtorDeclaration::syntaxCopy(Dsymbol *s) | |
2427 { | |
2428 StaticCtorDeclaration *scd; | |
2429 | |
2430 assert(!s); | |
2431 scd = new StaticCtorDeclaration(loc, endloc); | |
2432 return FuncDeclaration::syntaxCopy(scd); | |
2433 } | |
2434 | |
2435 | |
2436 void StaticCtorDeclaration::semantic(Scope *sc) | |
2437 { | |
2438 //printf("StaticCtorDeclaration::semantic()\n"); | |
2439 | |
2440 type = new TypeFunction(NULL, Type::tvoid, FALSE, LINKd); | |
2441 | |
336 | 2442 /* If the static ctor appears within a template instantiation, |
2443 * it could get called multiple times by the module constructors | |
2444 * for different modules. Thus, protect it with a gate. | |
2445 */ | |
2446 if (inTemplateInstance()) | |
2447 { | |
2448 /* Add this prefix to the function: | |
2449 * static int gate; | |
2450 * if (++gate != 1) return; | |
2451 * Note that this is not thread safe; should not have threads | |
2452 * during static construction. | |
2453 */ | |
2454 Identifier *id = Lexer::idPool("__gate"); | |
2455 VarDeclaration *v = new VarDeclaration(0, Type::tint32, id, NULL); | |
2456 v->storage_class = STCstatic; | |
2457 Statements *sa = new Statements(); | |
2458 Statement *s = new DeclarationStatement(0, v); | |
2459 sa->push(s); | |
2460 Expression *e = new IdentifierExp(0, id); | |
2461 e = new AddAssignExp(0, e, new IntegerExp(1)); | |
2462 e = new EqualExp(TOKnotequal, 0, e, new IntegerExp(1)); | |
2463 s = new IfStatement(0, NULL, e, new ReturnStatement(0, NULL), NULL); | |
2464 sa->push(s); | |
2465 if (fbody) | |
2466 sa->push(fbody); | |
2467 fbody = new CompoundStatement(0, sa); | |
2468 } | |
2469 | |
159 | 2470 FuncDeclaration::semantic(sc); |
2471 | |
2472 // We're going to need ModuleInfo | |
2473 Module *m = getModule(); | |
2474 if (!m) | |
2475 m = sc->module; | |
2476 if (m) | |
2477 { m->needmoduleinfo = 1; | |
2478 #ifdef IN_GCC | |
2479 m->strictlyneedmoduleinfo = 1; | |
2480 #endif | |
2481 } | |
2482 } | |
2483 | |
2484 AggregateDeclaration *StaticCtorDeclaration::isThis() | |
2485 { | |
2486 return NULL; | |
2487 } | |
2488 | |
2489 int StaticCtorDeclaration::isStaticConstructor() | |
2490 { | |
2491 return TRUE; | |
2492 } | |
2493 | |
2494 int StaticCtorDeclaration::isVirtual() | |
2495 { | |
2496 return FALSE; | |
2497 } | |
2498 | |
2499 int StaticCtorDeclaration::addPreInvariant() | |
2500 { | |
2501 return FALSE; | |
2502 } | |
2503 | |
2504 int StaticCtorDeclaration::addPostInvariant() | |
2505 { | |
2506 return FALSE; | |
2507 } | |
2508 | |
2509 void StaticCtorDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2510 { | |
2511 if (hgs->hdrgen) | |
2512 { buf->writestring("static this();\n"); | |
2513 return; | |
2514 } | |
2515 buf->writestring("static this()"); | |
2516 bodyToCBuffer(buf, hgs); | |
2517 } | |
2518 | |
2519 /********************************* StaticDtorDeclaration ****************************/ | |
2520 | |
2521 StaticDtorDeclaration::StaticDtorDeclaration(Loc loc, Loc endloc) | |
2522 : FuncDeclaration(loc, endloc, | |
2523 Identifier::generateId("_staticDtor"), STCstatic, NULL) | |
2524 { | |
336 | 2525 vgate = NULL; |
159 | 2526 } |
2527 | |
2528 Dsymbol *StaticDtorDeclaration::syntaxCopy(Dsymbol *s) | |
2529 { | |
2530 StaticDtorDeclaration *sdd; | |
2531 | |
2532 assert(!s); | |
2533 sdd = new StaticDtorDeclaration(loc, endloc); | |
2534 return FuncDeclaration::syntaxCopy(sdd); | |
2535 } | |
2536 | |
2537 | |
2538 void StaticDtorDeclaration::semantic(Scope *sc) | |
2539 { | |
2540 ClassDeclaration *cd; | |
2541 Type *tret; | |
2542 | |
2543 cd = sc->scopesym->isClassDeclaration(); | |
2544 if (!cd) | |
2545 { | |
2546 } | |
2547 type = new TypeFunction(NULL, Type::tvoid, FALSE, LINKd); | |
2548 | |
336 | 2549 /* If the static ctor appears within a template instantiation, |
2550 * it could get called multiple times by the module constructors | |
2551 * for different modules. Thus, protect it with a gate. | |
2552 */ | |
2553 if (inTemplateInstance()) | |
2554 { | |
2555 /* Add this prefix to the function: | |
2556 * static int gate; | |
2557 * if (--gate != 0) return; | |
2558 * Increment gate during constructor execution. | |
2559 * Note that this is not thread safe; should not have threads | |
2560 * during static destruction. | |
2561 */ | |
2562 Identifier *id = Lexer::idPool("__gate"); | |
2563 VarDeclaration *v = new VarDeclaration(0, Type::tint32, id, NULL); | |
2564 v->storage_class = STCstatic; | |
2565 Statements *sa = new Statements(); | |
2566 Statement *s = new DeclarationStatement(0, v); | |
2567 sa->push(s); | |
2568 Expression *e = new IdentifierExp(0, id); | |
2569 e = new AddAssignExp(0, e, new IntegerExp(-1)); | |
658
50383e476c7e
Upgraded frontend to DMD 1.035
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
644
diff
changeset
|
2570 e = new EqualExp(TOKnotequal, 0, e, new IntegerExp(0)); |
336 | 2571 s = new IfStatement(0, NULL, e, new ReturnStatement(0, NULL), NULL); |
2572 sa->push(s); | |
2573 if (fbody) | |
2574 sa->push(fbody); | |
2575 fbody = new CompoundStatement(0, sa); | |
2576 vgate = v; | |
2577 } | |
2578 | |
159 | 2579 FuncDeclaration::semantic(sc); |
2580 | |
2581 // We're going to need ModuleInfo | |
2582 Module *m = getModule(); | |
2583 if (!m) | |
2584 m = sc->module; | |
2585 if (m) | |
2586 { m->needmoduleinfo = 1; | |
2587 #ifdef IN_GCC | |
2588 m->strictlyneedmoduleinfo = 1; | |
2589 #endif | |
2590 } | |
2591 } | |
2592 | |
2593 AggregateDeclaration *StaticDtorDeclaration::isThis() | |
2594 { | |
2595 return NULL; | |
2596 } | |
2597 | |
2598 int StaticDtorDeclaration::isStaticDestructor() | |
2599 { | |
2600 return TRUE; | |
2601 } | |
2602 | |
2603 int StaticDtorDeclaration::isVirtual() | |
2604 { | |
2605 return FALSE; | |
2606 } | |
2607 | |
2608 int StaticDtorDeclaration::addPreInvariant() | |
2609 { | |
2610 return FALSE; | |
2611 } | |
2612 | |
2613 int StaticDtorDeclaration::addPostInvariant() | |
2614 { | |
2615 return FALSE; | |
2616 } | |
2617 | |
2618 void StaticDtorDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2619 { | |
2620 if (hgs->hdrgen) | |
2621 return; | |
2622 buf->writestring("static ~this()"); | |
2623 bodyToCBuffer(buf, hgs); | |
2624 } | |
2625 | |
2626 /********************************* InvariantDeclaration ****************************/ | |
2627 | |
2628 InvariantDeclaration::InvariantDeclaration(Loc loc, Loc endloc) | |
2629 : FuncDeclaration(loc, endloc, Id::classInvariant, STCundefined, NULL) | |
2630 { | |
2631 } | |
2632 | |
2633 Dsymbol *InvariantDeclaration::syntaxCopy(Dsymbol *s) | |
2634 { | |
2635 InvariantDeclaration *id; | |
2636 | |
2637 assert(!s); | |
2638 id = new InvariantDeclaration(loc, endloc); | |
2639 FuncDeclaration::syntaxCopy(id); | |
2640 return id; | |
2641 } | |
2642 | |
2643 | |
2644 void InvariantDeclaration::semantic(Scope *sc) | |
2645 { | |
2646 AggregateDeclaration *ad; | |
2647 Type *tret; | |
2648 | |
2649 parent = sc->parent; | |
2650 Dsymbol *parent = toParent(); | |
2651 ad = parent->isAggregateDeclaration(); | |
2652 if (!ad) | |
2653 { | |
2654 error("invariants only are for struct/union/class definitions"); | |
2655 return; | |
2656 } | |
2657 else if (ad->inv && ad->inv != this) | |
2658 { | |
2659 error("more than one invariant for %s", ad->toChars()); | |
2660 } | |
2661 ad->inv = this; | |
2662 type = new TypeFunction(NULL, Type::tvoid, FALSE, LINKd); | |
2663 | |
2664 sc = sc->push(); | |
2665 sc->stc &= ~STCstatic; // not a static invariant | |
2666 sc->incontract++; | |
2667 sc->linkage = LINKd; | |
2668 | |
2669 FuncDeclaration::semantic(sc); | |
2670 | |
2671 sc->pop(); | |
2672 } | |
2673 | |
2674 int InvariantDeclaration::isVirtual() | |
2675 { | |
2676 return FALSE; | |
2677 } | |
2678 | |
2679 int InvariantDeclaration::addPreInvariant() | |
2680 { | |
2681 return FALSE; | |
2682 } | |
2683 | |
2684 int InvariantDeclaration::addPostInvariant() | |
2685 { | |
2686 return FALSE; | |
2687 } | |
2688 | |
2689 void InvariantDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2690 { | |
2691 if (hgs->hdrgen) | |
2692 return; | |
2693 buf->writestring("invariant"); | |
2694 bodyToCBuffer(buf, hgs); | |
2695 } | |
2696 | |
2697 | |
2698 /********************************* UnitTestDeclaration ****************************/ | |
2699 | |
2700 /******************************* | |
2701 * Generate unique unittest function Id so we can have multiple | |
2702 * instances per module. | |
2703 */ | |
2704 | |
2705 static Identifier *unitTestId() | |
2706 { | |
336 | 2707 return Lexer::uniqueId("__unittest"); |
159 | 2708 } |
2709 | |
2710 UnitTestDeclaration::UnitTestDeclaration(Loc loc, Loc endloc) | |
2711 : FuncDeclaration(loc, endloc, unitTestId(), STCundefined, NULL) | |
2712 { | |
2713 } | |
2714 | |
2715 Dsymbol *UnitTestDeclaration::syntaxCopy(Dsymbol *s) | |
2716 { | |
2717 UnitTestDeclaration *utd; | |
2718 | |
2719 assert(!s); | |
2720 utd = new UnitTestDeclaration(loc, endloc); | |
2721 return FuncDeclaration::syntaxCopy(utd); | |
2722 } | |
2723 | |
2724 | |
2725 void UnitTestDeclaration::semantic(Scope *sc) | |
2726 { | |
2727 if (global.params.useUnitTests) | |
2728 { | |
2729 type = new TypeFunction(NULL, Type::tvoid, FALSE, LINKd); | |
846
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
717
diff
changeset
|
2730 Scope *sc2 = sc->push(); |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
717
diff
changeset
|
2731 sc2->linkage = LINKd; |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
717
diff
changeset
|
2732 FuncDeclaration::semantic(sc2); |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
717
diff
changeset
|
2733 sc2->pop(); |
159 | 2734 } |
2735 | |
2736 // We're going to need ModuleInfo even if the unit tests are not | |
2737 // compiled in, because other modules may import this module and refer | |
2738 // to this ModuleInfo. | |
2739 Module *m = getModule(); | |
2740 if (!m) | |
2741 m = sc->module; | |
2742 if (m) | |
2743 m->needmoduleinfo = 1; | |
2744 } | |
2745 | |
2746 AggregateDeclaration *UnitTestDeclaration::isThis() | |
2747 { | |
2748 return NULL; | |
2749 } | |
2750 | |
2751 int UnitTestDeclaration::isVirtual() | |
2752 { | |
2753 return FALSE; | |
2754 } | |
2755 | |
2756 int UnitTestDeclaration::addPreInvariant() | |
2757 { | |
2758 return FALSE; | |
2759 } | |
2760 | |
2761 int UnitTestDeclaration::addPostInvariant() | |
2762 { | |
2763 return FALSE; | |
2764 } | |
2765 | |
2766 void UnitTestDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2767 { | |
2768 if (hgs->hdrgen) | |
2769 return; | |
2770 buf->writestring("unittest"); | |
2771 bodyToCBuffer(buf, hgs); | |
2772 } | |
2773 | |
2774 /********************************* NewDeclaration ****************************/ | |
2775 | |
2776 NewDeclaration::NewDeclaration(Loc loc, Loc endloc, Arguments *arguments, int varargs) | |
2777 : FuncDeclaration(loc, endloc, Id::classNew, STCstatic, NULL) | |
2778 { | |
2779 this->arguments = arguments; | |
2780 this->varargs = varargs; | |
2781 } | |
2782 | |
2783 Dsymbol *NewDeclaration::syntaxCopy(Dsymbol *s) | |
2784 { | |
2785 NewDeclaration *f; | |
2786 | |
2787 f = new NewDeclaration(loc, endloc, NULL, varargs); | |
2788 | |
2789 FuncDeclaration::syntaxCopy(f); | |
2790 | |
2791 f->arguments = Argument::arraySyntaxCopy(arguments); | |
2792 | |
2793 return f; | |
2794 } | |
2795 | |
2796 | |
2797 void NewDeclaration::semantic(Scope *sc) | |
2798 { | |
2799 ClassDeclaration *cd; | |
2800 Type *tret; | |
2801 | |
2802 //printf("NewDeclaration::semantic()\n"); | |
2803 | |
2804 parent = sc->parent; | |
2805 Dsymbol *parent = toParent(); | |
2806 cd = parent->isClassDeclaration(); | |
2807 if (!cd && !parent->isStructDeclaration()) | |
2808 { | |
2809 error("new allocators only are for class or struct definitions"); | |
2810 } | |
2811 tret = Type::tvoid->pointerTo(); | |
2812 type = new TypeFunction(arguments, tret, varargs, LINKd); | |
2813 | |
2814 type = type->semantic(loc, sc); | |
2815 assert(type->ty == Tfunction); | |
2816 | |
2817 // Check that there is at least one argument of type uint | |
2818 TypeFunction *tf = (TypeFunction *)type; | |
2819 if (Argument::dim(tf->parameters) < 1) | |
2820 { | |
891
ea82e2077f9d
Change custom allocator new size argument to size_t instead of uint. See #175.
Christian Kamm <kamm incasoftware de>
parents:
875
diff
changeset
|
2821 error("at least one argument of type size_t expected"); |
159 | 2822 } |
2823 else | |
2824 { | |
2825 Argument *a = Argument::getNth(tf->parameters, 0); | |
891
ea82e2077f9d
Change custom allocator new size argument to size_t instead of uint. See #175.
Christian Kamm <kamm incasoftware de>
parents:
875
diff
changeset
|
2826 if (!a->type->equals(Type::tsize_t)) |
ea82e2077f9d
Change custom allocator new size argument to size_t instead of uint. See #175.
Christian Kamm <kamm incasoftware de>
parents:
875
diff
changeset
|
2827 error("first argument must be type size_t, not %s", a->type->toChars()); |
159 | 2828 } |
2829 | |
2830 FuncDeclaration::semantic(sc); | |
2831 } | |
2832 | |
336 | 2833 const char *NewDeclaration::kind() |
159 | 2834 { |
2835 return "allocator"; | |
2836 } | |
2837 | |
2838 int NewDeclaration::isVirtual() | |
2839 { | |
2840 return FALSE; | |
2841 } | |
2842 | |
2843 int NewDeclaration::addPreInvariant() | |
2844 { | |
2845 return FALSE; | |
2846 } | |
2847 | |
2848 int NewDeclaration::addPostInvariant() | |
2849 { | |
2850 return FALSE; | |
2851 } | |
2852 | |
2853 void NewDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2854 { | |
2855 buf->writestring("new"); | |
2856 Argument::argsToCBuffer(buf, hgs, arguments, varargs); | |
2857 bodyToCBuffer(buf, hgs); | |
2858 } | |
2859 | |
2860 | |
2861 /********************************* DeleteDeclaration ****************************/ | |
2862 | |
2863 DeleteDeclaration::DeleteDeclaration(Loc loc, Loc endloc, Arguments *arguments) | |
2864 : FuncDeclaration(loc, endloc, Id::classDelete, STCstatic, NULL) | |
2865 { | |
2866 this->arguments = arguments; | |
2867 } | |
2868 | |
2869 Dsymbol *DeleteDeclaration::syntaxCopy(Dsymbol *s) | |
2870 { | |
2871 DeleteDeclaration *f; | |
2872 | |
2873 f = new DeleteDeclaration(loc, endloc, NULL); | |
2874 | |
2875 FuncDeclaration::syntaxCopy(f); | |
2876 | |
2877 f->arguments = Argument::arraySyntaxCopy(arguments); | |
2878 | |
2879 return f; | |
2880 } | |
2881 | |
2882 | |
2883 void DeleteDeclaration::semantic(Scope *sc) | |
2884 { | |
2885 ClassDeclaration *cd; | |
2886 | |
2887 //printf("DeleteDeclaration::semantic()\n"); | |
2888 | |
2889 parent = sc->parent; | |
2890 Dsymbol *parent = toParent(); | |
2891 cd = parent->isClassDeclaration(); | |
2892 if (!cd && !parent->isStructDeclaration()) | |
2893 { | |
2894 error("new allocators only are for class or struct definitions"); | |
2895 } | |
2896 type = new TypeFunction(arguments, Type::tvoid, 0, LINKd); | |
2897 | |
2898 type = type->semantic(loc, sc); | |
2899 assert(type->ty == Tfunction); | |
2900 | |
2901 // Check that there is only one argument of type void* | |
2902 TypeFunction *tf = (TypeFunction *)type; | |
2903 if (Argument::dim(tf->parameters) != 1) | |
2904 { | |
2905 error("one argument of type void* expected"); | |
2906 } | |
2907 else | |
2908 { | |
2909 Argument *a = Argument::getNth(tf->parameters, 0); | |
2910 if (!a->type->equals(Type::tvoid->pointerTo())) | |
2911 error("one argument of type void* expected, not %s", a->type->toChars()); | |
2912 } | |
2913 | |
2914 FuncDeclaration::semantic(sc); | |
2915 } | |
2916 | |
336 | 2917 const char *DeleteDeclaration::kind() |
159 | 2918 { |
2919 return "deallocator"; | |
2920 } | |
2921 | |
2922 int DeleteDeclaration::isDelete() | |
2923 { | |
2924 return TRUE; | |
2925 } | |
2926 | |
2927 int DeleteDeclaration::isVirtual() | |
2928 { | |
2929 return FALSE; | |
2930 } | |
2931 | |
2932 int DeleteDeclaration::addPreInvariant() | |
2933 { | |
2934 return FALSE; | |
2935 } | |
2936 | |
2937 int DeleteDeclaration::addPostInvariant() | |
2938 { | |
2939 return FALSE; | |
2940 } | |
2941 | |
2942 void DeleteDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2943 { | |
2944 buf->writestring("delete"); | |
2945 Argument::argsToCBuffer(buf, hgs, arguments, 0); | |
2946 bodyToCBuffer(buf, hgs); | |
2947 } | |
2948 | |
2949 | |
2950 | |
2951 |