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