Mercurial > projects > ldc
annotate dmd/func.c @ 336:aaade6ded589 trunk
[svn r357] Merged DMD 1.033
author | lindquist |
---|---|
date | Sat, 12 Jul 2008 19:38:31 +0200 |
parents | f7190d9eb70c |
children | d8234836b40f |
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; | |
71 inferRetType = (type && type->nextOf() == NULL); | |
72 scope = NULL; | |
73 hasReturnExp = 0; | |
74 nrvo_can = 1; | |
75 nrvo_var = NULL; | |
76 shidden = NULL; | |
77 // llvmdc | |
78 runTimeHack = false; | |
79 } | |
80 | |
81 Dsymbol *FuncDeclaration::syntaxCopy(Dsymbol *s) | |
82 { | |
83 FuncDeclaration *f; | |
84 | |
85 //printf("FuncDeclaration::syntaxCopy('%s')\n", toChars()); | |
86 if (s) | |
87 f = (FuncDeclaration *)s; | |
88 else | |
89 f = new FuncDeclaration(loc, endloc, ident, (enum STC) storage_class, type->syntaxCopy()); | |
90 f->outId = outId; | |
91 f->frequire = frequire ? frequire->syntaxCopy() : NULL; | |
92 f->fensure = fensure ? fensure->syntaxCopy() : NULL; | |
93 f->fbody = fbody ? fbody->syntaxCopy() : NULL; | |
94 assert(!fthrows); // deprecated | |
95 return f; | |
96 } | |
97 | |
98 | |
99 // Do the semantic analysis on the external interface to the function. | |
100 | |
101 void FuncDeclaration::semantic(Scope *sc) | |
102 { TypeFunction *f; | |
103 StructDeclaration *sd; | |
104 ClassDeclaration *cd; | |
105 InterfaceDeclaration *id; | |
336 | 106 Dsymbol *pd; |
159 | 107 |
108 #if 0 | |
109 printf("FuncDeclaration::semantic(sc = %p, this = %p, '%s', linkage = %d)\n", sc, this, toPrettyChars(), sc->linkage); | |
110 if (isFuncLiteralDeclaration()) | |
111 printf("\tFuncLiteralDeclaration()\n"); | |
112 printf("sc->parent = %s\n", sc->parent->toChars()); | |
113 printf("type: %s\n", type->toChars()); | |
114 #endif | |
115 | |
116 if (type->nextOf()) | |
117 type = type->semantic(loc, sc); | |
118 //type->print(); | |
119 if (type->ty != Tfunction) | |
120 { | |
121 error("%s must be a function", toChars()); | |
122 return; | |
123 } | |
124 f = (TypeFunction *)(type); | |
125 size_t nparams = Argument::dim(f->parameters); | |
126 | |
127 linkage = sc->linkage; | |
128 // if (!parent) | |
129 { | |
130 //parent = sc->scopesym; | |
131 parent = sc->parent; | |
132 } | |
133 protection = sc->protection; | |
134 storage_class |= sc->stc; | |
135 //printf("function storage_class = x%x\n", storage_class); | |
136 Dsymbol *parent = toParent(); | |
137 | |
138 if (isConst() || isAuto() || isScope()) | |
139 error("functions cannot be const or auto"); | |
140 | |
141 if (isAbstract() && !isVirtual()) | |
142 error("non-virtual functions cannot be abstract"); | |
143 | |
144 if (isAbstract() && isFinal()) | |
145 error("cannot be both final and abstract"); | |
146 #if 0 | |
147 if (isAbstract() && fbody) | |
148 error("abstract functions cannot have bodies"); | |
149 #endif | |
150 | |
151 #if 0 | |
152 if (isStaticConstructor() || isStaticDestructor()) | |
153 { | |
154 if (!isStatic() || type->nextOf()->ty != Tvoid) | |
155 error("static constructors / destructors must be static void"); | |
156 if (f->arguments && f->arguments->dim) | |
157 error("static constructors / destructors must have empty parameter list"); | |
158 // BUG: check for invalid storage classes | |
159 } | |
160 #endif | |
161 | |
162 #ifdef IN_GCC | |
163 AggregateDeclaration *ad; | |
164 | |
165 ad = parent->isAggregateDeclaration(); | |
166 if (ad) | |
167 ad->methods.push(this); | |
168 #endif | |
169 sd = parent->isStructDeclaration(); | |
170 if (sd) | |
171 { | |
172 // Verify no constructors, destructors, etc. | |
173 if (isCtorDeclaration() || | |
174 isDtorDeclaration() | |
175 //|| isInvariantDeclaration() | |
176 //|| isUnitTestDeclaration() | |
177 ) | |
178 { | |
179 error("special member functions not allowed for %ss", sd->kind()); | |
180 } | |
181 | |
182 #if 0 | |
183 if (!sd->inv) | |
184 sd->inv = isInvariantDeclaration(); | |
185 | |
186 if (!sd->aggNew) | |
187 sd->aggNew = isNewDeclaration(); | |
188 | |
189 if (isDelete()) | |
190 { | |
191 if (sd->aggDelete) | |
192 error("multiple delete's for struct %s", sd->toChars()); | |
193 sd->aggDelete = (DeleteDeclaration *)(this); | |
194 } | |
195 #endif | |
196 } | |
197 | |
198 id = parent->isInterfaceDeclaration(); | |
199 if (id) | |
200 { | |
201 storage_class |= STCabstract; | |
202 | |
203 if (isCtorDeclaration() || | |
204 isDtorDeclaration() || | |
205 isInvariantDeclaration() || | |
206 isUnitTestDeclaration() || isNewDeclaration() || isDelete()) | |
207 error("special function not allowed in interface %s", id->toChars()); | |
208 if (fbody) | |
209 error("function body is not abstract in interface %s", id->toChars()); | |
210 } | |
211 | |
336 | 212 /* Template member functions aren't virtual: |
213 * interface TestInterface { void tpl(T)(); } | |
214 * and so won't work in interfaces | |
215 */ | |
216 if ((pd = toParent()) != NULL && | |
217 pd->isTemplateInstance() && | |
218 (pd = toParent2()) != NULL && | |
219 (id = pd->isInterfaceDeclaration()) != NULL) | |
220 { | |
221 error("template member function not allowed in interface %s", id->toChars()); | |
222 } | |
223 | |
159 | 224 cd = parent->isClassDeclaration(); |
225 if (cd) | |
226 { int vi; | |
227 CtorDeclaration *ctor; | |
228 DtorDeclaration *dtor; | |
229 InvariantDeclaration *inv; | |
230 | |
231 if (isCtorDeclaration()) | |
232 { | |
233 // ctor = (CtorDeclaration *)this; | |
234 // if (!cd->ctor) | |
235 // cd->ctor = ctor; | |
236 return; | |
237 } | |
238 | |
239 #if 0 | |
240 dtor = isDtorDeclaration(); | |
241 if (dtor) | |
242 { | |
243 if (cd->dtor) | |
244 error("multiple destructors for class %s", cd->toChars()); | |
245 cd->dtor = dtor; | |
246 } | |
247 | |
248 inv = isInvariantDeclaration(); | |
249 if (inv) | |
250 { | |
251 cd->inv = inv; | |
252 } | |
253 | |
254 if (isNewDeclaration()) | |
255 { | |
256 if (!cd->aggNew) | |
257 cd->aggNew = (NewDeclaration *)(this); | |
258 } | |
259 | |
260 if (isDelete()) | |
261 { | |
262 if (cd->aggDelete) | |
263 error("multiple delete's for class %s", cd->toChars()); | |
264 cd->aggDelete = (DeleteDeclaration *)(this); | |
265 } | |
266 #endif | |
267 | |
268 if (storage_class & STCabstract) | |
269 cd->isabstract = 1; | |
270 | |
271 // if static function, do not put in vtbl[] | |
272 if (!isVirtual()) | |
273 { | |
274 //printf("\tnot virtual\n"); | |
275 goto Ldone; | |
276 } | |
277 | |
278 // Find index of existing function in vtbl[] to override | |
279 vi = findVtblIndex(&cd->vtbl, cd->baseClass ? cd->baseClass->vtbl.dim : 0); | |
280 switch (vi) | |
281 { | |
282 case -1: // didn't find one | |
283 // This is an 'introducing' function. | |
284 | |
285 // Verify this doesn't override previous final function | |
286 if (cd->baseClass) | |
287 { Dsymbol *s = cd->baseClass->search(loc, ident, 0); | |
288 if (s) | |
289 { | |
290 FuncDeclaration *f = s->isFuncDeclaration(); | |
291 f = f->overloadExactMatch(type); | |
292 if (f && f->isFinal() && f->prot() != PROTprivate) | |
293 error("cannot override final function %s", f->toPrettyChars()); | |
294 } | |
295 } | |
296 | |
297 if (isFinal()) | |
298 { | |
299 cd->vtblFinal.push(this); | |
300 } | |
301 else | |
302 { | |
303 // Append to end of vtbl[] | |
304 //printf("\tintroducing function\n"); | |
305 introducing = 1; | |
306 vi = cd->vtbl.dim; | |
307 cd->vtbl.push(this); | |
308 vtblIndex = vi; | |
309 } | |
310 break; | |
311 | |
312 case -2: // can't determine because of fwd refs | |
313 cd->sizeok = 2; // can't finish due to forward reference | |
314 return; | |
315 | |
316 default: | |
317 { FuncDeclaration *fdv = (FuncDeclaration *)cd->vtbl.data[vi]; | |
318 // This function is covariant with fdv | |
319 if (fdv->isFinal()) | |
320 error("cannot override final function %s", fdv->toPrettyChars()); | |
321 | |
336 | 322 #if DMDV2 |
159 | 323 if (!isOverride() && global.params.warnings) |
215
a58d8f4b84df
[svn r231] Changed: warnings are no longer treated as an error.
lindquist
parents:
162
diff
changeset
|
324 warning("%s: overrides base class function %s, but is not marked with 'override'", locToChars() fdv->toPrettyChars()); |
159 | 325 #endif |
326 | |
327 if (fdv->toParent() == parent) | |
328 { | |
329 // If both are mixins, then error. | |
330 // If either is not, the one that is not overrides | |
331 // the other. | |
332 if (fdv->parent->isClassDeclaration()) | |
333 break; | |
334 if (!this->parent->isClassDeclaration() | |
335 #if !BREAKABI | |
336 && !isDtorDeclaration() | |
337 #endif | |
336 | 338 #if DMDV2 |
159 | 339 && !isPostBlitDeclaration() |
340 #endif | |
341 ) | |
342 error("multiple overrides of same function"); | |
343 } | |
344 cd->vtbl.data[vi] = (void *)this; | |
345 vtblIndex = vi; | |
346 | |
347 /* This works by whenever this function is called, | |
348 * it actually returns tintro, which gets dynamically | |
349 * cast to type. But we know that tintro is a base | |
350 * of type, so we could optimize it by not doing a | |
351 * dynamic cast, but just subtracting the isBaseOf() | |
352 * offset if the value is != null. | |
353 */ | |
354 | |
355 if (fdv->tintro) | |
356 tintro = fdv->tintro; | |
357 else if (!type->equals(fdv->type)) | |
358 { | |
359 /* Only need to have a tintro if the vptr | |
360 * offsets differ | |
361 */ | |
362 int offset; | |
363 if (fdv->type->nextOf()->isBaseOf(type->nextOf(), &offset)) | |
364 { | |
365 tintro = fdv->type; | |
366 } | |
367 } | |
368 break; | |
369 } | |
370 } | |
371 | |
372 /* Go through all the interface bases. | |
373 * If this function is covariant with any members of those interface | |
374 * functions, set the tintro. | |
375 */ | |
376 for (int i = 0; i < cd->interfaces_dim; i++) | |
377 { | |
378 #if 1 | |
379 BaseClass *b = cd->interfaces[i]; | |
380 vi = findVtblIndex(&b->base->vtbl, b->base->vtbl.dim); | |
381 switch (vi) | |
382 { | |
383 case -1: | |
384 break; | |
385 | |
386 case -2: | |
387 cd->sizeok = 2; // can't finish due to forward reference | |
388 return; | |
389 | |
390 default: | |
391 { FuncDeclaration *fdv = (FuncDeclaration *)b->base->vtbl.data[vi]; | |
392 Type *ti = NULL; | |
393 | |
394 if (fdv->tintro) | |
395 ti = fdv->tintro; | |
396 else if (!type->equals(fdv->type)) | |
397 { | |
398 /* Only need to have a tintro if the vptr | |
399 * offsets differ | |
400 */ | |
401 int offset; | |
402 if (fdv->type->nextOf()->isBaseOf(type->nextOf(), &offset)) | |
403 { | |
404 ti = fdv->type; | |
405 #if 0 | |
406 if (offset) | |
407 ti = fdv->type; | |
408 else if (type->nextOf()->ty == Tclass) | |
409 { ClassDeclaration *cdn = ((TypeClass *)type->nextOf())->sym; | |
410 if (cdn && cdn->sizeok != 1) | |
411 ti = fdv->type; | |
412 } | |
413 #endif | |
414 } | |
415 } | |
416 if (ti) | |
417 { | |
418 if (tintro && !tintro->equals(ti)) | |
419 { | |
420 error("incompatible covariant types %s and %s", tintro->toChars(), ti->toChars()); | |
421 } | |
422 tintro = ti; | |
423 } | |
424 goto L2; | |
425 } | |
426 } | |
427 #else | |
428 BaseClass *b = cd->interfaces[i]; | |
429 for (vi = 0; vi < b->base->vtbl.dim; vi++) | |
430 { | |
431 Dsymbol *s = (Dsymbol *)b->base->vtbl.data[vi]; | |
432 //printf("interface %d vtbl[%d] %p %s\n", i, vi, s, s->toChars()); | |
433 FuncDeclaration *fdv = s->isFuncDeclaration(); | |
434 if (fdv && fdv->ident == ident) | |
435 { | |
436 int cov = type->covariant(fdv->type); | |
437 //printf("\tcov = %d\n", cov); | |
438 if (cov == 2) | |
439 { | |
440 //type->print(); | |
441 //fdv->type->print(); | |
442 //printf("%s %s\n", type->deco, fdv->type->deco); | |
443 error("of type %s overrides but is not covariant with %s of type %s", | |
444 type->toChars(), fdv->toPrettyChars(), fdv->type->toChars()); | |
445 } | |
446 if (cov == 1) | |
447 { Type *ti = NULL; | |
448 | |
449 if (fdv->tintro) | |
450 ti = fdv->tintro; | |
451 else if (!type->equals(fdv->type)) | |
452 { | |
453 /* Only need to have a tintro if the vptr | |
454 * offsets differ | |
455 */ | |
456 int offset; | |
457 if (fdv->type->nextOf()->isBaseOf(type->nextOf(), &offset)) | |
458 { | |
459 ti = fdv->type; | |
460 #if 0 | |
461 if (offset) | |
462 ti = fdv->type; | |
463 else if (type->nextOf()->ty == Tclass) | |
464 { ClassDeclaration *cdn = ((TypeClass *)type->nextOf())->sym; | |
465 if (cdn && cdn->sizeok != 1) | |
466 ti = fdv->type; | |
467 } | |
468 #endif | |
469 } | |
470 } | |
471 if (ti) | |
472 { | |
473 if (tintro && !tintro->equals(ti)) | |
474 { | |
475 error("incompatible covariant types %s and %s", tintro->toChars(), ti->toChars()); | |
476 } | |
477 tintro = ti; | |
478 } | |
479 goto L2; | |
480 } | |
481 if (cov == 3) | |
482 { | |
483 cd->sizeok = 2; // can't finish due to forward reference | |
484 return; | |
485 } | |
486 } | |
487 } | |
488 #endif | |
489 } | |
490 | |
491 if (introducing && isOverride()) | |
492 { | |
493 error("does not override any function"); | |
494 } | |
495 | |
496 L2: ; | |
497 } | |
498 else if (isOverride() && !parent->isTemplateInstance()) | |
499 error("override only applies to class member functions"); | |
500 | |
501 /* Do not allow template instances to add virtual functions | |
502 * to a class. | |
503 */ | |
504 if (isVirtual()) | |
505 { | |
506 TemplateInstance *ti = parent->isTemplateInstance(); | |
507 if (ti) | |
508 { | |
509 // Take care of nested templates | |
510 while (1) | |
511 { | |
512 TemplateInstance *ti2 = ti->tempdecl->parent->isTemplateInstance(); | |
513 if (!ti2) | |
514 break; | |
515 ti = ti2; | |
516 } | |
517 | |
518 // If it's a member template | |
519 ClassDeclaration *cd = ti->tempdecl->isClassMember(); | |
520 if (cd) | |
521 { | |
522 error("cannot use template to add virtual function to class '%s'", cd->toChars()); | |
523 } | |
524 } | |
525 } | |
526 | |
527 if (isMain()) | |
528 { | |
529 // Check parameters to see if they are either () or (char[][] args) | |
530 switch (nparams) | |
531 { | |
532 case 0: | |
533 break; | |
534 | |
535 case 1: | |
536 { | |
537 Argument *arg0 = Argument::getNth(f->parameters, 0); | |
538 if (arg0->type->ty != Tarray || | |
539 arg0->type->nextOf()->ty != Tarray || | |
540 arg0->type->nextOf()->nextOf()->ty != Tchar || | |
541 arg0->storageClass & (STCout | STCref | STClazy)) | |
542 goto Lmainerr; | |
543 break; | |
544 } | |
545 | |
546 default: | |
547 goto Lmainerr; | |
548 } | |
549 | |
550 if (f->nextOf()->ty != Tint32 && f->nextOf()->ty != Tvoid) | |
551 error("must return int or void, not %s", f->nextOf()->toChars()); | |
552 if (f->varargs) | |
553 { | |
554 Lmainerr: | |
555 error("parameters must be main() or main(char[][] args)"); | |
556 } | |
557 } | |
558 | |
559 if (ident == Id::assign && (sd || cd)) | |
560 { // Disallow identity assignment operator. | |
561 | |
562 // opAssign(...) | |
563 if (nparams == 0) | |
564 { if (f->varargs == 1) | |
565 goto Lassignerr; | |
566 } | |
567 else | |
568 { | |
569 Argument *arg0 = Argument::getNth(f->parameters, 0); | |
570 Type *t0 = arg0->type->toBasetype(); | |
571 Type *tb = sd ? sd->type : cd->type; | |
572 if (arg0->type->implicitConvTo(tb) || | |
573 (sd && t0->ty == Tpointer && t0->nextOf()->implicitConvTo(tb)) | |
574 ) | |
575 { | |
576 if (nparams == 1) | |
577 goto Lassignerr; | |
578 Argument *arg1 = Argument::getNth(f->parameters, 1); | |
579 if (arg1->defaultArg) | |
580 goto Lassignerr; | |
581 } | |
582 } | |
583 } | |
584 | |
585 Ldone: | |
586 /* Save scope for possible later use (if we need the | |
587 * function internals) | |
588 */ | |
589 scope = new Scope(*sc); | |
590 scope->setNoFree(); | |
591 return; | |
592 | |
593 Lassignerr: | |
594 error("identity assignment operator overload is illegal"); | |
595 } | |
596 | |
597 void FuncDeclaration::semantic2(Scope *sc) | |
598 { | |
599 } | |
600 | |
601 // Do the semantic analysis on the internals of the function. | |
602 | |
603 void FuncDeclaration::semantic3(Scope *sc) | |
604 { TypeFunction *f; | |
605 AggregateDeclaration *ad; | |
606 VarDeclaration *argptr = NULL; | |
607 VarDeclaration *_arguments = NULL; | |
608 | |
609 if (!parent) | |
610 { | |
611 if (global.errors) | |
612 return; | |
613 //printf("FuncDeclaration::semantic3(%s '%s', sc = %p)\n", kind(), toChars(), sc); | |
614 assert(0); | |
615 } | |
616 //printf("FuncDeclaration::semantic3('%s.%s', sc = %p, loc = %s)\n", parent->toChars(), toChars(), sc, loc.toChars()); | |
617 //fflush(stdout); | |
618 //{ static int x; if (++x == 2) *(char*)0=0; } | |
619 //printf("\tlinkage = %d\n", sc->linkage); | |
620 | |
621 //printf(" sc->incontract = %d\n", sc->incontract); | |
622 if (semanticRun) | |
623 return; | |
624 semanticRun = 1; | |
625 | |
626 if (!type || type->ty != Tfunction) | |
627 return; | |
628 f = (TypeFunction *)(type); | |
629 size_t nparams = Argument::dim(f->parameters); | |
630 | |
631 // Check the 'throws' clause | |
632 if (fthrows) | |
633 { int i; | |
634 | |
635 for (i = 0; i < fthrows->dim; i++) | |
636 { | |
637 Type *t = (Type *)fthrows->data[i]; | |
638 | |
639 t = t->semantic(loc, sc); | |
640 if (!t->isClassHandle()) | |
641 error("can only throw classes, not %s", t->toChars()); | |
642 } | |
643 } | |
644 | |
645 if (fbody || frequire) | |
646 { | |
647 /* Symbol table into which we place parameters and nested functions, | |
648 * solely to diagnose name collisions. | |
649 */ | |
650 localsymtab = new DsymbolTable(); | |
651 | |
652 // Establish function scope | |
653 ScopeDsymbol *ss = new ScopeDsymbol(); | |
654 ss->parent = sc->scopesym; | |
655 Scope *sc2 = sc->push(ss); | |
656 sc2->func = this; | |
657 sc2->parent = this; | |
658 sc2->callSuper = 0; | |
659 sc2->sbreak = NULL; | |
660 sc2->scontinue = NULL; | |
661 sc2->sw = NULL; | |
662 sc2->fes = fes; | |
663 sc2->linkage = LINKd; | |
664 sc2->stc &= ~(STCauto | STCscope | STCstatic | STCabstract | STCdeprecated | STCfinal); | |
665 sc2->protection = PROTpublic; | |
666 sc2->explicitProtection = 0; | |
667 sc2->structalign = 8; | |
668 sc2->incontract = 0; | |
669 sc2->tf = NULL; | |
670 sc2->noctor = 0; | |
671 | |
672 // Declare 'this' | |
673 ad = isThis(); | |
674 if (ad) | |
675 { VarDeclaration *v; | |
676 | |
677 if (isFuncLiteralDeclaration() && isNested()) | |
678 { | |
679 error("literals cannot be class members"); | |
680 return; | |
681 } | |
682 else | |
683 { | |
684 assert(!isNested()); // can't be both member and nested | |
685 assert(ad->handle); | |
686 v = new ThisDeclaration(ad->handle); | |
687 v->storage_class |= STCparameter | STCin; | |
688 v->semantic(sc2); | |
689 if (!sc2->insert(v)) | |
690 assert(0); | |
691 v->parent = this; | |
692 vthis = v; | |
693 } | |
694 } | |
695 else if (isNested()) | |
696 { | |
697 VarDeclaration *v; | |
698 | |
699 v = new ThisDeclaration(Type::tvoid->pointerTo()); | |
700 v->storage_class |= STCparameter | STCin; | |
701 v->semantic(sc2); | |
702 if (!sc2->insert(v)) | |
703 assert(0); | |
704 v->parent = this; | |
705 vthis = v; | |
706 } | |
707 | |
708 // Declare hidden variable _arguments[] and _argptr | |
709 if (f->varargs == 1) | |
710 { Type *t; | |
711 | |
712 if (f->linkage == LINKd) | |
713 { // Declare _arguments[] | |
714 #if BREAKABI | |
715 v_arguments = new VarDeclaration(0, Type::typeinfotypelist->type, Id::_arguments_typeinfo, NULL); | |
716 v_arguments->storage_class = STCparameter | STCin; | |
717 v_arguments->semantic(sc2); | |
718 sc2->insert(v_arguments); | |
719 v_arguments->parent = this; | |
720 | |
721 t = Type::typeinfo->type->arrayOf(); | |
722 _arguments = new VarDeclaration(0, t, Id::_arguments, NULL); | |
723 _arguments->semantic(sc2); | |
724 sc2->insert(_arguments); | |
725 _arguments->parent = this; | |
726 #else | |
727 t = Type::typeinfo->type->arrayOf(); | |
728 v_arguments = new VarDeclaration(0, t, Id::_arguments, NULL); | |
729 v_arguments->storage_class = STCparameter | STCin; | |
730 v_arguments->semantic(sc2); | |
731 sc2->insert(v_arguments); | |
732 v_arguments->parent = this; | |
733 #endif | |
734 } | |
735 if (f->linkage == LINKd || (parameters && parameters->dim)) | |
736 { // Declare _argptr | |
737 #if IN_GCC | |
738 t = d_gcc_builtin_va_list_d_type; | |
739 #else | |
740 t = Type::tvoid->pointerTo(); | |
741 #endif | |
742 argptr = new VarDeclaration(0, t, Id::_argptr, NULL); | |
743 argptr->semantic(sc2); | |
744 sc2->insert(argptr); | |
745 argptr->parent = this; | |
746 } | |
747 } | |
748 | |
749 // Propagate storage class from tuple parameters to their element-parameters. | |
750 if (f->parameters) | |
751 { | |
752 for (size_t i = 0; i < f->parameters->dim; i++) | |
753 { Argument *arg = (Argument *)f->parameters->data[i]; | |
754 | |
755 if (arg->type->ty == Ttuple) | |
756 { TypeTuple *t = (TypeTuple *)arg->type; | |
757 size_t dim = Argument::dim(t->arguments); | |
758 for (size_t j = 0; j < dim; j++) | |
759 { Argument *narg = Argument::getNth(t->arguments, j); | |
760 narg->storageClass = arg->storageClass; | |
761 } | |
762 } | |
763 } | |
764 } | |
765 | |
766 // Declare all the function parameters as variables | |
767 if (nparams) | |
768 { /* parameters[] has all the tuples removed, as the back end | |
769 * doesn't know about tuples | |
770 */ | |
771 parameters = new Dsymbols(); | |
772 parameters->reserve(nparams); | |
773 for (size_t i = 0; i < nparams; i++) | |
774 { | |
775 Argument *arg = Argument::getNth(f->parameters, i); | |
776 Identifier *id = arg->ident; | |
777 if (!id) | |
778 { | |
779 /* Generate identifier for un-named parameter, | |
780 * because we need it later on. | |
781 */ | |
782 OutBuffer buf; | |
305
2b72433d5c8c
[svn r326] Fixed a bunch of issues with printf's that MinGW32 did not support.
lindquist
parents:
245
diff
changeset
|
783 buf.printf("_param_%"PRIuSIZE, i); |
159 | 784 char *name = (char *)buf.extractData(); |
785 id = new Identifier(name, TOKidentifier); | |
786 arg->ident = id; | |
787 } | |
788 VarDeclaration *v = new VarDeclaration(loc, arg->type, id, NULL); | |
789 //printf("declaring parameter %s of type %s\n", v->toChars(), v->type->toChars()); | |
790 v->storage_class |= STCparameter; | |
791 if (f->varargs == 2 && i + 1 == nparams) | |
792 v->storage_class |= STCvariadic; | |
793 v->storage_class |= arg->storageClass & (STCin | STCout | STCref | STClazy); | |
794 if (v->storage_class & STClazy) | |
795 v->storage_class |= STCin; | |
796 v->semantic(sc2); | |
245
d61ce72c39ab
[svn r262] Fixed debug info for normal function parameters.
lindquist
parents:
215
diff
changeset
|
797 #if IN_LLVM |
d61ce72c39ab
[svn r262] Fixed debug info for normal function parameters.
lindquist
parents:
215
diff
changeset
|
798 // LLVMDC: the argument needs an addres if we want to attach debug info to it. |
d61ce72c39ab
[svn r262] Fixed debug info for normal function parameters.
lindquist
parents:
215
diff
changeset
|
799 if (global.params.symdebug) |
d61ce72c39ab
[svn r262] Fixed debug info for normal function parameters.
lindquist
parents:
215
diff
changeset
|
800 v->needsStorage = true; |
d61ce72c39ab
[svn r262] Fixed debug info for normal function parameters.
lindquist
parents:
215
diff
changeset
|
801 #endif |
159 | 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); | |
830 Expression *e = new VarExp(0, v); | |
831 exps->data[j] = (void *)e; | |
832 } | |
833 assert(arg->ident); | |
834 TupleDeclaration *v = new TupleDeclaration(0, arg->ident, exps); | |
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 TypeFunction *tf; | |
1576 | |
1577 m->anyf = f; | |
1578 tf = (TypeFunction *)f->type; | |
1579 match = (MATCH) tf->callMatch(arguments); | |
1580 //printf("match = %d\n", match); | |
1581 if (match != MATCHnomatch) | |
1582 { | |
1583 if (match > m->last) | |
1584 goto LfIsBetter; | |
1585 | |
1586 if (match < m->last) | |
1587 goto LlastIsBetter; | |
1588 | |
1589 /* See if one of the matches overrides the other. | |
1590 */ | |
1591 if (m->lastf->overrides(f)) | |
1592 goto LlastIsBetter; | |
1593 else if (f->overrides(m->lastf)) | |
1594 goto LfIsBetter; | |
1595 | |
1596 Lambiguous: | |
1597 m->nextf = f; | |
1598 m->count++; | |
1599 return 0; | |
1600 | |
1601 LfIsBetter: | |
1602 m->last = match; | |
1603 m->lastf = f; | |
1604 m->count = 1; | |
1605 return 0; | |
1606 | |
1607 LlastIsBetter: | |
1608 return 0; | |
1609 } | |
1610 } | |
1611 return 0; | |
1612 } | |
1613 | |
1614 | |
1615 void overloadResolveX(Match *m, FuncDeclaration *fstart, Expressions *arguments) | |
1616 { | |
1617 Param2 p; | |
1618 p.m = m; | |
1619 p.arguments = arguments; | |
1620 overloadApply(fstart, &fp2, &p); | |
1621 } | |
1622 | |
1623 #if 0 | |
1624 // Recursive helper function | |
1625 | |
1626 void overloadResolveX(Match *m, FuncDeclaration *fstart, Expressions *arguments) | |
1627 { | |
1628 MATCH match; | |
1629 Declaration *d; | |
1630 Declaration *next; | |
1631 | |
1632 for (d = fstart; d; d = next) | |
1633 { | |
1634 FuncDeclaration *f; | |
1635 FuncAliasDeclaration *fa; | |
1636 AliasDeclaration *a; | |
1637 | |
1638 fa = d->isFuncAliasDeclaration(); | |
1639 if (fa) | |
1640 { | |
1641 overloadResolveX(m, fa->funcalias, arguments); | |
1642 next = fa->overnext; | |
1643 } | |
1644 else if ((f = d->isFuncDeclaration()) != NULL) | |
1645 { | |
1646 next = f->overnext; | |
1647 if (f == m->lastf) | |
1648 continue; // skip duplicates | |
1649 else | |
1650 { | |
1651 TypeFunction *tf; | |
1652 | |
1653 m->anyf = f; | |
1654 tf = (TypeFunction *)f->type; | |
1655 match = (MATCH) tf->callMatch(arguments); | |
1656 //printf("match = %d\n", match); | |
1657 if (match != MATCHnomatch) | |
1658 { | |
1659 if (match > m->last) | |
1660 goto LfIsBetter; | |
1661 | |
1662 if (match < m->last) | |
1663 goto LlastIsBetter; | |
1664 | |
1665 /* See if one of the matches overrides the other. | |
1666 */ | |
1667 if (m->lastf->overrides(f)) | |
1668 goto LlastIsBetter; | |
1669 else if (f->overrides(m->lastf)) | |
1670 goto LfIsBetter; | |
1671 | |
1672 Lambiguous: | |
1673 m->nextf = f; | |
1674 m->count++; | |
1675 continue; | |
1676 | |
1677 LfIsBetter: | |
1678 m->last = match; | |
1679 m->lastf = f; | |
1680 m->count = 1; | |
1681 continue; | |
1682 | |
1683 LlastIsBetter: | |
1684 continue; | |
1685 } | |
1686 } | |
1687 } | |
1688 else if ((a = d->isAliasDeclaration()) != NULL) | |
1689 { | |
1690 Dsymbol *s = a->toAlias(); | |
1691 next = s->isDeclaration(); | |
1692 if (next == a) | |
1693 break; | |
1694 if (next == fstart) | |
1695 break; | |
1696 } | |
1697 else | |
1698 { d->error("is aliased to a function"); | |
1699 break; | |
1700 } | |
1701 } | |
1702 } | |
1703 #endif | |
1704 | |
1705 FuncDeclaration *FuncDeclaration::overloadResolve(Loc loc, Expressions *arguments) | |
1706 { | |
1707 TypeFunction *tf; | |
1708 Match m; | |
1709 | |
1710 #if 0 | |
1711 printf("FuncDeclaration::overloadResolve('%s')\n", toChars()); | |
1712 if (arguments) | |
1713 { int i; | |
1714 | |
1715 for (i = 0; i < arguments->dim; i++) | |
1716 { Expression *arg; | |
1717 | |
1718 arg = (Expression *)arguments->data[i]; | |
1719 assert(arg->type); | |
1720 printf("\t%s: ", arg->toChars()); | |
1721 arg->type->print(); | |
1722 } | |
1723 } | |
1724 #endif | |
1725 | |
1726 memset(&m, 0, sizeof(m)); | |
1727 m.last = MATCHnomatch; | |
1728 overloadResolveX(&m, this, arguments); | |
1729 | |
1730 if (m.count == 1) // exactly one match | |
1731 { | |
1732 return m.lastf; | |
1733 } | |
1734 else | |
1735 { | |
1736 OutBuffer buf; | |
1737 | |
1738 if (arguments) | |
1739 { | |
1740 HdrGenState hgs; | |
1741 | |
1742 argExpTypesToCBuffer(&buf, arguments, &hgs); | |
1743 } | |
1744 | |
1745 if (m.last == MATCHnomatch) | |
1746 { | |
1747 tf = (TypeFunction *)type; | |
1748 | |
1749 //printf("tf = %s, args = %s\n", tf->deco, ((Expression *)arguments->data[0])->type->deco); | |
1750 error(loc, "%s does not match parameter types (%s)", | |
1751 Argument::argsTypesToChars(tf->parameters, tf->varargs), | |
1752 buf.toChars()); | |
1753 return m.anyf; // as long as it's not a FuncAliasDeclaration | |
1754 } | |
1755 else | |
1756 { | |
1757 #if 1 | |
1758 TypeFunction *t1 = (TypeFunction *)m.lastf->type; | |
1759 TypeFunction *t2 = (TypeFunction *)m.nextf->type; | |
1760 | |
1761 error(loc, "called with argument types:\n\t(%s)\nmatches both:\n\t%s%s\nand:\n\t%s%s", | |
1762 buf.toChars(), | |
1763 m.lastf->toPrettyChars(), Argument::argsTypesToChars(t1->parameters, t1->varargs), | |
1764 m.nextf->toPrettyChars(), Argument::argsTypesToChars(t2->parameters, t2->varargs)); | |
1765 #else | |
1766 error(loc, "overloads %s and %s both match argument list for %s", | |
1767 m.lastf->type->toChars(), | |
1768 m.nextf->type->toChars(), | |
1769 m.lastf->toChars()); | |
1770 #endif | |
1771 return m.lastf; | |
1772 } | |
1773 } | |
1774 } | |
1775 | |
1776 /******************************** | |
1777 * Labels are in a separate scope, one per function. | |
1778 */ | |
1779 | |
1780 LabelDsymbol *FuncDeclaration::searchLabel(Identifier *ident) | |
1781 { Dsymbol *s; | |
1782 | |
1783 if (!labtab) | |
1784 labtab = new DsymbolTable(); // guess we need one | |
1785 | |
1786 s = labtab->lookup(ident); | |
1787 if (!s) | |
1788 { | |
1789 s = new LabelDsymbol(ident); | |
1790 labtab->insert(s); | |
1791 } | |
1792 return (LabelDsymbol *)s; | |
1793 } | |
336 | 1794 |
159 | 1795 /**************************************** |
1796 * If non-static member function that has a 'this' pointer, | |
1797 * return the aggregate it is a member of. | |
1798 * Otherwise, return NULL. | |
1799 */ | |
1800 | |
1801 AggregateDeclaration *FuncDeclaration::isThis() | |
1802 { AggregateDeclaration *ad; | |
1803 | |
1804 //printf("+FuncDeclaration::isThis() '%s'\n", toChars()); | |
1805 ad = NULL; | |
1806 if ((storage_class & STCstatic) == 0) | |
1807 { | |
1808 ad = isMember2(); | |
1809 } | |
1810 //printf("-FuncDeclaration::isThis() %p\n", ad); | |
1811 return ad; | |
1812 } | |
1813 | |
1814 AggregateDeclaration *FuncDeclaration::isMember2() | |
1815 { AggregateDeclaration *ad; | |
1816 | |
1817 //printf("+FuncDeclaration::isMember2() '%s'\n", toChars()); | |
1818 ad = NULL; | |
1819 for (Dsymbol *s = this; s; s = s->parent) | |
1820 { | |
1821 //printf("\ts = '%s', parent = '%s', kind = %s\n", s->toChars(), s->parent->toChars(), s->parent->kind()); | |
1822 ad = s->isMember(); | |
1823 if (ad) | |
1824 { //printf("test4\n"); | |
1825 break; | |
1826 } | |
1827 if (!s->parent || | |
1828 (!s->parent->isTemplateInstance())) | |
1829 { //printf("test5\n"); | |
1830 break; | |
1831 } | |
1832 } | |
1833 //printf("-FuncDeclaration::isMember2() %p\n", ad); | |
1834 return ad; | |
1835 } | |
1836 | |
1837 /***************************************** | |
1838 * Determine lexical level difference from 'this' to nested function 'fd'. | |
1839 * Error if this cannot call fd. | |
1840 * Returns: | |
1841 * 0 same level | |
1842 * -1 increase nesting by 1 (fd is nested within 'this') | |
1843 * >0 decrease nesting by number | |
1844 */ | |
1845 | |
1846 int FuncDeclaration::getLevel(Loc loc, FuncDeclaration *fd) | |
1847 { int level; | |
1848 Dsymbol *s; | |
1849 Dsymbol *fdparent; | |
1850 | |
1851 //printf("FuncDeclaration::getLevel(fd = '%s')\n", fd->toChars()); | |
1852 fdparent = fd->toParent2(); | |
1853 if (fdparent == this) | |
1854 return -1; | |
1855 s = this; | |
1856 level = 0; | |
1857 while (fd != s && fdparent != s->toParent2()) | |
1858 { | |
1859 //printf("\ts = '%s'\n", s->toChars()); | |
1860 FuncDeclaration *thisfd = s->isFuncDeclaration(); | |
1861 if (thisfd) | |
1862 { if (!thisfd->isNested() && !thisfd->vthis) | |
1863 goto Lerr; | |
1864 } | |
1865 else | |
1866 { | |
1867 ClassDeclaration *thiscd = s->isClassDeclaration(); | |
1868 if (thiscd) | |
1869 { if (!thiscd->isNested()) | |
1870 goto Lerr; | |
1871 } | |
1872 else | |
1873 goto Lerr; | |
1874 } | |
1875 | |
1876 s = s->toParent2(); | |
1877 assert(s); | |
1878 level++; | |
1879 } | |
1880 return level; | |
1881 | |
1882 Lerr: | |
1883 error(loc, "cannot access frame of function %s", fd->toChars()); | |
1884 return 1; | |
1885 } | |
1886 | |
1887 void FuncDeclaration::appendExp(Expression *e) | |
1888 { Statement *s; | |
1889 | |
1890 s = new ExpStatement(0, e); | |
1891 appendState(s); | |
1892 } | |
1893 | |
1894 void FuncDeclaration::appendState(Statement *s) | |
1895 { CompoundStatement *cs; | |
1896 | |
1897 if (!fbody) | |
1898 { Statements *a; | |
1899 | |
1900 a = new Statements(); | |
1901 fbody = new CompoundStatement(0, a); | |
1902 } | |
1903 cs = fbody->isCompoundStatement(); | |
1904 cs->statements->push(s); | |
1905 } | |
1906 | |
1907 | |
1908 int FuncDeclaration::isMain() | |
1909 { | |
1910 return ident == Id::main && | |
1911 linkage != LINKc && !isMember() && !isNested(); | |
1912 } | |
1913 | |
1914 int FuncDeclaration::isWinMain() | |
1915 { | |
1916 return ident == Id::WinMain && | |
1917 linkage != LINKc && !isMember(); | |
1918 } | |
1919 | |
1920 int FuncDeclaration::isDllMain() | |
1921 { | |
1922 return ident == Id::DllMain && | |
1923 linkage != LINKc && !isMember(); | |
1924 } | |
1925 | |
1926 int FuncDeclaration::isExport() | |
1927 { | |
1928 return protection == PROTexport; | |
1929 } | |
1930 | |
1931 int FuncDeclaration::isImportedSymbol() | |
1932 { | |
1933 //printf("isImportedSymbol()\n"); | |
1934 //printf("protection = %d\n", protection); | |
1935 return (protection == PROTexport) && !fbody; | |
1936 } | |
1937 | |
1938 // Determine if function goes into virtual function pointer table | |
1939 | |
1940 int FuncDeclaration::isVirtual() | |
1941 { | |
1942 #if 0 | |
1943 printf("FuncDeclaration::isVirtual(%s)\n", toChars()); | |
1944 printf("%p %d %d %d %d\n", isMember(), isStatic(), protection == PROTprivate, isCtorDeclaration(), linkage != LINKd); | |
1945 printf("result is %d\n", | |
1946 isMember() && | |
1947 !(isStatic() || protection == PROTprivate || protection == PROTpackage) && | |
1948 toParent()->isClassDeclaration()); | |
1949 #endif | |
1950 return isMember() && | |
1951 !(isStatic() || protection == PROTprivate || protection == PROTpackage) && | |
1952 toParent()->isClassDeclaration(); | |
1953 } | |
1954 | |
1955 int FuncDeclaration::isAbstract() | |
1956 { | |
1957 return storage_class & STCabstract; | |
1958 } | |
1959 | |
1960 int FuncDeclaration::isCodeseg() | |
1961 { | |
1962 return TRUE; // functions are always in the code segment | |
1963 } | |
1964 | |
1965 // Determine if function needs | |
1966 // a static frame pointer to its lexically enclosing function | |
1967 | |
1968 int FuncDeclaration::isNested() | |
1969 { | |
1970 //if (!toParent()) | |
1971 //printf("FuncDeclaration::isNested('%s') parent=%p\n", toChars(), parent); | |
1972 //printf("\ttoParent() = '%s'\n", toParent()->toChars()); | |
1973 return ((storage_class & STCstatic) == 0) && | |
1974 (toParent2()->isFuncDeclaration() != NULL); | |
1975 } | |
1976 | |
1977 int FuncDeclaration::needThis() | |
1978 { | |
1979 //printf("FuncDeclaration::needThis() '%s'\n", toChars()); | |
1980 int i = isThis() != NULL; | |
1981 //printf("\t%d\n", i); | |
1982 if (!i && isFuncAliasDeclaration()) | |
1983 i = ((FuncAliasDeclaration *)this)->funcalias->needThis(); | |
1984 return i; | |
1985 } | |
1986 | |
1987 int FuncDeclaration::addPreInvariant() | |
1988 { | |
1989 AggregateDeclaration *ad = isThis(); | |
1990 return (ad && | |
1991 //ad->isClassDeclaration() && | |
1992 global.params.useInvariants && | |
1993 (protection == PROTpublic || protection == PROTexport) && | |
1994 !naked); | |
1995 } | |
1996 | |
1997 int FuncDeclaration::addPostInvariant() | |
1998 { | |
1999 AggregateDeclaration *ad = isThis(); | |
2000 return (ad && | |
2001 ad->inv && | |
2002 //ad->isClassDeclaration() && | |
2003 global.params.useInvariants && | |
2004 (protection == PROTpublic || protection == PROTexport) && | |
2005 !naked); | |
2006 } | |
2007 | |
2008 /********************************** | |
2009 * Generate a FuncDeclaration for a runtime library function. | |
2010 */ | |
2011 | |
2012 FuncDeclaration *FuncDeclaration::genCfunc(Type *treturn, char *name) | |
2013 { | |
2014 return genCfunc(treturn, Lexer::idPool(name)); | |
2015 } | |
2016 | |
2017 FuncDeclaration *FuncDeclaration::genCfunc(Type *treturn, Identifier *id) | |
2018 { | |
2019 FuncDeclaration *fd; | |
2020 TypeFunction *tf; | |
2021 Dsymbol *s; | |
2022 static DsymbolTable *st = NULL; | |
2023 | |
2024 //printf("genCfunc(name = '%s')\n", id->toChars()); | |
2025 //printf("treturn\n\t"); treturn->print(); | |
2026 | |
2027 // See if already in table | |
2028 if (!st) | |
2029 st = new DsymbolTable(); | |
2030 s = st->lookup(id); | |
2031 if (s) | |
2032 { | |
2033 fd = s->isFuncDeclaration(); | |
2034 assert(fd); | |
2035 assert(fd->type->nextOf()->equals(treturn)); | |
2036 } | |
2037 else | |
2038 { | |
2039 tf = new TypeFunction(NULL, treturn, 0, LINKc); | |
2040 fd = new FuncDeclaration(0, 0, id, STCstatic, tf); | |
2041 fd->protection = PROTpublic; | |
2042 fd->linkage = LINKc; | |
2043 | |
2044 st->insert(fd); | |
2045 } | |
2046 return fd; | |
2047 } | |
2048 | |
336 | 2049 const char *FuncDeclaration::kind() |
159 | 2050 { |
2051 return "function"; | |
2052 } | |
336 | 2053 |
159 | 2054 /******************************* |
2055 * Look at all the variables in this function that are referenced | |
2056 * by nested functions, and determine if a closure needs to be | |
2057 * created for them. | |
2058 */ | |
2059 | |
336 | 2060 #if DMDV2 |
159 | 2061 int FuncDeclaration::needsClosure() |
2062 { | |
2063 /* Need a closure for all the closureVars[] if any of the | |
2064 * closureVars[] are accessed by a | |
2065 * function that escapes the scope of this function. | |
2066 * We take the conservative approach and decide that any function that: | |
2067 * 1) is a virtual function | |
2068 * 2) has its address taken | |
2069 * 3) has a parent that escapes | |
336 | 2070 * |
2071 * Note that since a non-virtual function can be called by | |
2072 * a virtual one, if that non-virtual function accesses a closure | |
2073 * var, the closure still has to be taken. Hence, we check for isThis() | |
2074 * instead of isVirtual(). (thanks to David Friedman) | |
159 | 2075 */ |
2076 | |
2077 //printf("FuncDeclaration::needsClosure() %s\n", toChars()); | |
2078 for (int i = 0; i < closureVars.dim; i++) | |
2079 { VarDeclaration *v = (VarDeclaration *)closureVars.data[i]; | |
2080 assert(v->isVarDeclaration()); | |
2081 //printf("\tv = %s\n", v->toChars()); | |
2082 | |
2083 for (int j = 0; j < v->nestedrefs.dim; j++) | |
2084 { FuncDeclaration *f = (FuncDeclaration *)v->nestedrefs.data[j]; | |
2085 assert(f != this); | |
2086 | |
2087 //printf("\t\tf = %s, %d, %d\n", f->toChars(), f->isVirtual(), f->tookAddressOf); | |
336 | 2088 if (f->isThis() || f->tookAddressOf) |
159 | 2089 goto Lyes; // assume f escapes this function's scope |
2090 | |
2091 // Look to see if any parents of f that are below this escape | |
2092 for (Dsymbol *s = f->parent; s != this; s = s->parent) | |
2093 { | |
2094 f = s->isFuncDeclaration(); | |
336 | 2095 if (f && (f->isThis() || f->tookAddressOf)) |
159 | 2096 goto Lyes; |
2097 } | |
2098 } | |
2099 } | |
2100 return 0; | |
2101 | |
2102 Lyes: | |
2103 //printf("\tneeds closure\n"); | |
2104 return 1; | |
2105 } | |
2106 #endif | |
2107 | |
2108 /****************************** FuncAliasDeclaration ************************/ | |
2109 | |
2110 // Used as a way to import a set of functions from another scope into this one. | |
2111 | |
2112 FuncAliasDeclaration::FuncAliasDeclaration(FuncDeclaration *funcalias) | |
2113 : FuncDeclaration(funcalias->loc, funcalias->endloc, funcalias->ident, | |
2114 (enum STC)funcalias->storage_class, funcalias->type) | |
2115 { | |
2116 assert(funcalias != this); | |
2117 this->funcalias = funcalias; | |
2118 } | |
2119 | |
336 | 2120 const char *FuncAliasDeclaration::kind() |
159 | 2121 { |
2122 return "function alias"; | |
2123 } | |
2124 | |
2125 | |
2126 /****************************** FuncLiteralDeclaration ************************/ | |
2127 | |
2128 FuncLiteralDeclaration::FuncLiteralDeclaration(Loc loc, Loc endloc, Type *type, | |
2129 enum TOK tok, ForeachStatement *fes) | |
2130 : FuncDeclaration(loc, endloc, NULL, STCundefined, type) | |
2131 { | |
2132 char *id; | |
2133 | |
2134 if (fes) | |
2135 id = "__foreachbody"; | |
2136 else if (tok == TOKdelegate) | |
2137 id = "__dgliteral"; | |
2138 else | |
2139 id = "__funcliteral"; | |
2140 this->ident = Identifier::generateId(id); | |
2141 this->tok = tok; | |
2142 this->fes = fes; | |
2143 //printf("FuncLiteralDeclaration() id = '%s', type = '%s'\n", this->ident->toChars(), type->toChars()); | |
2144 } | |
2145 | |
2146 Dsymbol *FuncLiteralDeclaration::syntaxCopy(Dsymbol *s) | |
2147 { | |
2148 FuncLiteralDeclaration *f; | |
2149 | |
2150 //printf("FuncLiteralDeclaration::syntaxCopy('%s')\n", toChars()); | |
2151 if (s) | |
2152 f = (FuncLiteralDeclaration *)s; | |
2153 else | |
2154 f = new FuncLiteralDeclaration(loc, endloc, type->syntaxCopy(), tok, fes); | |
2155 FuncDeclaration::syntaxCopy(f); | |
2156 return f; | |
2157 } | |
2158 | |
2159 int FuncLiteralDeclaration::isNested() | |
2160 { | |
2161 //printf("FuncLiteralDeclaration::isNested() '%s'\n", toChars()); | |
2162 return (tok == TOKdelegate); | |
2163 } | |
2164 | |
336 | 2165 int FuncLiteralDeclaration::isVirtual() |
2166 { | |
2167 return FALSE; | |
2168 } | |
2169 | |
2170 const char *FuncLiteralDeclaration::kind() | |
159 | 2171 { |
2172 // GCC requires the (char*) casts | |
2173 return (tok == TOKdelegate) ? (char*)"delegate" : (char*)"function"; | |
2174 } | |
2175 | |
2176 void FuncLiteralDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2177 { | |
2178 static Identifier *idfunc; | |
2179 static Identifier *iddel; | |
2180 | |
2181 if (!idfunc) | |
2182 idfunc = new Identifier("function", 0); | |
2183 if (!iddel) | |
2184 iddel = new Identifier("delegate", 0); | |
2185 | |
2186 type->toCBuffer(buf, ((tok == TOKdelegate) ? iddel : idfunc), hgs); | |
2187 bodyToCBuffer(buf, hgs); | |
2188 } | |
2189 | |
2190 | |
2191 /********************************* CtorDeclaration ****************************/ | |
2192 | |
2193 CtorDeclaration::CtorDeclaration(Loc loc, Loc endloc, Arguments *arguments, int varargs) | |
2194 : FuncDeclaration(loc, endloc, Id::ctor, STCundefined, NULL) | |
2195 { | |
2196 this->arguments = arguments; | |
2197 this->varargs = varargs; | |
2198 //printf("CtorDeclaration() %s\n", toChars()); | |
2199 } | |
2200 | |
2201 Dsymbol *CtorDeclaration::syntaxCopy(Dsymbol *s) | |
2202 { | |
2203 CtorDeclaration *f; | |
2204 | |
2205 f = new CtorDeclaration(loc, endloc, NULL, varargs); | |
2206 | |
2207 f->outId = outId; | |
2208 f->frequire = frequire ? frequire->syntaxCopy() : NULL; | |
2209 f->fensure = fensure ? fensure->syntaxCopy() : NULL; | |
2210 f->fbody = fbody ? fbody->syntaxCopy() : NULL; | |
2211 assert(!fthrows); // deprecated | |
2212 | |
2213 f->arguments = Argument::arraySyntaxCopy(arguments); | |
2214 return f; | |
2215 } | |
2216 | |
2217 | |
2218 void CtorDeclaration::semantic(Scope *sc) | |
2219 { | |
2220 ClassDeclaration *cd; | |
2221 Type *tret; | |
2222 | |
2223 //printf("CtorDeclaration::semantic()\n"); | |
2224 if (type) | |
2225 return; | |
2226 | |
2227 sc = sc->push(); | |
2228 sc->stc &= ~STCstatic; // not a static constructor | |
2229 | |
2230 parent = sc->parent; | |
2231 Dsymbol *parent = toParent(); | |
2232 cd = parent->isClassDeclaration(); | |
2233 if (!cd) | |
2234 { | |
2235 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
|
2236 fatal(); |
159 | 2237 tret = Type::tvoid; |
2238 } | |
2239 else | |
2240 tret = cd->type; //->referenceTo(); | |
2241 type = new TypeFunction(arguments, tret, varargs, LINKd); | |
336 | 2242 if (!originalType) |
2243 originalType = type; | |
159 | 2244 |
2245 sc->flags |= SCOPEctor; | |
2246 type = type->semantic(loc, sc); | |
2247 sc->flags &= ~SCOPEctor; | |
2248 | |
2249 // Append: | |
2250 // return this; | |
2251 // to the function body | |
2252 if (fbody) | |
2253 { Expression *e; | |
2254 Statement *s; | |
2255 | |
2256 e = new ThisExp(0); | |
2257 s = new ReturnStatement(0, e); | |
2258 fbody = new CompoundStatement(0, fbody, s); | |
2259 } | |
2260 | |
2261 FuncDeclaration::semantic(sc); | |
2262 | |
2263 sc->pop(); | |
2264 | |
2265 // See if it's the default constructor | |
2266 if (cd && varargs == 0 && Argument::dim(arguments) == 0) | |
2267 cd->defaultCtor = this; | |
2268 } | |
2269 | |
336 | 2270 const char *CtorDeclaration::kind() |
159 | 2271 { |
2272 return "constructor"; | |
2273 } | |
2274 | |
2275 char *CtorDeclaration::toChars() | |
2276 { | |
2277 return "this"; | |
2278 } | |
2279 | |
2280 int CtorDeclaration::isVirtual() | |
2281 { | |
2282 return FALSE; | |
2283 } | |
2284 | |
2285 int CtorDeclaration::addPreInvariant() | |
2286 { | |
2287 return FALSE; | |
2288 } | |
2289 | |
2290 int CtorDeclaration::addPostInvariant() | |
2291 { | |
2292 return (vthis && global.params.useInvariants); | |
2293 } | |
2294 | |
2295 | |
2296 void CtorDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2297 { | |
2298 buf->writestring("this"); | |
2299 Argument::argsToCBuffer(buf, hgs, arguments, varargs); | |
2300 bodyToCBuffer(buf, hgs); | |
2301 } | |
2302 | |
2303 /********************************* DtorDeclaration ****************************/ | |
2304 | |
2305 DtorDeclaration::DtorDeclaration(Loc loc, Loc endloc) | |
2306 : FuncDeclaration(loc, endloc, Id::dtor, STCundefined, NULL) | |
2307 { | |
2308 } | |
2309 | |
2310 DtorDeclaration::DtorDeclaration(Loc loc, Loc endloc, Identifier *id) | |
2311 : FuncDeclaration(loc, endloc, id, STCundefined, NULL) | |
2312 { | |
2313 } | |
2314 | |
2315 Dsymbol *DtorDeclaration::syntaxCopy(Dsymbol *s) | |
2316 { | |
2317 assert(!s); | |
2318 DtorDeclaration *dd = new DtorDeclaration(loc, endloc, ident); | |
2319 return FuncDeclaration::syntaxCopy(dd); | |
2320 } | |
2321 | |
2322 | |
2323 void DtorDeclaration::semantic(Scope *sc) | |
2324 { | |
2325 ClassDeclaration *cd; | |
2326 | |
2327 parent = sc->parent; | |
2328 Dsymbol *parent = toParent(); | |
2329 cd = parent->isClassDeclaration(); | |
2330 if (!cd) | |
2331 { | |
2332 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
|
2333 fatal(); |
159 | 2334 } |
2335 else | |
2336 cd->dtors.push(this); | |
2337 type = new TypeFunction(NULL, Type::tvoid, FALSE, LINKd); | |
2338 | |
2339 sc = sc->push(); | |
2340 sc->stc &= ~STCstatic; // not a static destructor | |
2341 sc->linkage = LINKd; | |
2342 | |
2343 FuncDeclaration::semantic(sc); | |
2344 | |
2345 sc->pop(); | |
2346 } | |
2347 | |
2348 int DtorDeclaration::overloadInsert(Dsymbol *s) | |
2349 { | |
2350 return FALSE; // cannot overload destructors | |
2351 } | |
2352 | |
2353 int DtorDeclaration::addPreInvariant() | |
2354 { | |
2355 return (vthis && global.params.useInvariants); | |
2356 } | |
2357 | |
2358 int DtorDeclaration::addPostInvariant() | |
2359 { | |
2360 return FALSE; | |
2361 } | |
2362 | |
2363 int DtorDeclaration::isVirtual() | |
2364 { | |
2365 /* This should be FALSE so that dtor's don't get put into the vtbl[], | |
2366 * but doing so will require recompiling everything. | |
2367 */ | |
2368 #if BREAKABI | |
2369 return FALSE; | |
2370 #else | |
2371 return FuncDeclaration::isVirtual(); | |
2372 #endif | |
2373 } | |
2374 | |
2375 void DtorDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2376 { | |
2377 if (hgs->hdrgen) | |
2378 return; | |
2379 buf->writestring("~this()"); | |
2380 bodyToCBuffer(buf, hgs); | |
2381 } | |
2382 | |
2383 /********************************* StaticCtorDeclaration ****************************/ | |
2384 | |
2385 StaticCtorDeclaration::StaticCtorDeclaration(Loc loc, Loc endloc) | |
2386 : FuncDeclaration(loc, endloc, | |
2387 Identifier::generateId("_staticCtor"), STCstatic, NULL) | |
2388 { | |
2389 } | |
2390 | |
2391 Dsymbol *StaticCtorDeclaration::syntaxCopy(Dsymbol *s) | |
2392 { | |
2393 StaticCtorDeclaration *scd; | |
2394 | |
2395 assert(!s); | |
2396 scd = new StaticCtorDeclaration(loc, endloc); | |
2397 return FuncDeclaration::syntaxCopy(scd); | |
2398 } | |
2399 | |
2400 | |
2401 void StaticCtorDeclaration::semantic(Scope *sc) | |
2402 { | |
2403 //printf("StaticCtorDeclaration::semantic()\n"); | |
2404 | |
2405 type = new TypeFunction(NULL, Type::tvoid, FALSE, LINKd); | |
2406 | |
336 | 2407 /* If the static ctor appears within a template instantiation, |
2408 * it could get called multiple times by the module constructors | |
2409 * for different modules. Thus, protect it with a gate. | |
2410 */ | |
2411 if (inTemplateInstance()) | |
2412 { | |
2413 /* Add this prefix to the function: | |
2414 * static int gate; | |
2415 * if (++gate != 1) return; | |
2416 * Note that this is not thread safe; should not have threads | |
2417 * during static construction. | |
2418 */ | |
2419 Identifier *id = Lexer::idPool("__gate"); | |
2420 VarDeclaration *v = new VarDeclaration(0, Type::tint32, id, NULL); | |
2421 v->storage_class = STCstatic; | |
2422 Statements *sa = new Statements(); | |
2423 Statement *s = new DeclarationStatement(0, v); | |
2424 sa->push(s); | |
2425 Expression *e = new IdentifierExp(0, id); | |
2426 e = new AddAssignExp(0, e, new IntegerExp(1)); | |
2427 e = new EqualExp(TOKnotequal, 0, e, new IntegerExp(1)); | |
2428 s = new IfStatement(0, NULL, e, new ReturnStatement(0, NULL), NULL); | |
2429 sa->push(s); | |
2430 if (fbody) | |
2431 sa->push(fbody); | |
2432 fbody = new CompoundStatement(0, sa); | |
2433 } | |
2434 | |
159 | 2435 FuncDeclaration::semantic(sc); |
2436 | |
2437 // We're going to need ModuleInfo | |
2438 Module *m = getModule(); | |
2439 if (!m) | |
2440 m = sc->module; | |
2441 if (m) | |
2442 { m->needmoduleinfo = 1; | |
2443 #ifdef IN_GCC | |
2444 m->strictlyneedmoduleinfo = 1; | |
2445 #endif | |
2446 } | |
2447 } | |
2448 | |
2449 AggregateDeclaration *StaticCtorDeclaration::isThis() | |
2450 { | |
2451 return NULL; | |
2452 } | |
2453 | |
2454 int StaticCtorDeclaration::isStaticConstructor() | |
2455 { | |
2456 return TRUE; | |
2457 } | |
2458 | |
2459 int StaticCtorDeclaration::isVirtual() | |
2460 { | |
2461 return FALSE; | |
2462 } | |
2463 | |
2464 int StaticCtorDeclaration::addPreInvariant() | |
2465 { | |
2466 return FALSE; | |
2467 } | |
2468 | |
2469 int StaticCtorDeclaration::addPostInvariant() | |
2470 { | |
2471 return FALSE; | |
2472 } | |
2473 | |
2474 void StaticCtorDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2475 { | |
2476 if (hgs->hdrgen) | |
2477 { buf->writestring("static this();\n"); | |
2478 return; | |
2479 } | |
2480 buf->writestring("static this()"); | |
2481 bodyToCBuffer(buf, hgs); | |
2482 } | |
2483 | |
2484 /********************************* StaticDtorDeclaration ****************************/ | |
2485 | |
2486 StaticDtorDeclaration::StaticDtorDeclaration(Loc loc, Loc endloc) | |
2487 : FuncDeclaration(loc, endloc, | |
2488 Identifier::generateId("_staticDtor"), STCstatic, NULL) | |
2489 { | |
336 | 2490 vgate = NULL; |
159 | 2491 } |
2492 | |
2493 Dsymbol *StaticDtorDeclaration::syntaxCopy(Dsymbol *s) | |
2494 { | |
2495 StaticDtorDeclaration *sdd; | |
2496 | |
2497 assert(!s); | |
2498 sdd = new StaticDtorDeclaration(loc, endloc); | |
2499 return FuncDeclaration::syntaxCopy(sdd); | |
2500 } | |
2501 | |
2502 | |
2503 void StaticDtorDeclaration::semantic(Scope *sc) | |
2504 { | |
2505 ClassDeclaration *cd; | |
2506 Type *tret; | |
2507 | |
2508 cd = sc->scopesym->isClassDeclaration(); | |
2509 if (!cd) | |
2510 { | |
2511 } | |
2512 type = new TypeFunction(NULL, Type::tvoid, FALSE, LINKd); | |
2513 | |
336 | 2514 /* If the static ctor appears within a template instantiation, |
2515 * it could get called multiple times by the module constructors | |
2516 * for different modules. Thus, protect it with a gate. | |
2517 */ | |
2518 if (inTemplateInstance()) | |
2519 { | |
2520 /* Add this prefix to the function: | |
2521 * static int gate; | |
2522 * if (--gate != 0) return; | |
2523 * Increment gate during constructor execution. | |
2524 * Note that this is not thread safe; should not have threads | |
2525 * during static destruction. | |
2526 */ | |
2527 Identifier *id = Lexer::idPool("__gate"); | |
2528 VarDeclaration *v = new VarDeclaration(0, Type::tint32, id, NULL); | |
2529 v->storage_class = STCstatic; | |
2530 Statements *sa = new Statements(); | |
2531 Statement *s = new DeclarationStatement(0, v); | |
2532 sa->push(s); | |
2533 Expression *e = new IdentifierExp(0, id); | |
2534 e = new AddAssignExp(0, e, new IntegerExp(-1)); | |
2535 e = new EqualExp(TOKnotequal, 0, e, new IntegerExp(1)); | |
2536 s = new IfStatement(0, NULL, e, new ReturnStatement(0, NULL), NULL); | |
2537 sa->push(s); | |
2538 if (fbody) | |
2539 sa->push(fbody); | |
2540 fbody = new CompoundStatement(0, sa); | |
2541 vgate = v; | |
2542 } | |
2543 | |
159 | 2544 FuncDeclaration::semantic(sc); |
2545 | |
2546 // We're going to need ModuleInfo | |
2547 Module *m = getModule(); | |
2548 if (!m) | |
2549 m = sc->module; | |
2550 if (m) | |
2551 { m->needmoduleinfo = 1; | |
2552 #ifdef IN_GCC | |
2553 m->strictlyneedmoduleinfo = 1; | |
2554 #endif | |
2555 } | |
2556 } | |
2557 | |
2558 AggregateDeclaration *StaticDtorDeclaration::isThis() | |
2559 { | |
2560 return NULL; | |
2561 } | |
2562 | |
2563 int StaticDtorDeclaration::isStaticDestructor() | |
2564 { | |
2565 return TRUE; | |
2566 } | |
2567 | |
2568 int StaticDtorDeclaration::isVirtual() | |
2569 { | |
2570 return FALSE; | |
2571 } | |
2572 | |
2573 int StaticDtorDeclaration::addPreInvariant() | |
2574 { | |
2575 return FALSE; | |
2576 } | |
2577 | |
2578 int StaticDtorDeclaration::addPostInvariant() | |
2579 { | |
2580 return FALSE; | |
2581 } | |
2582 | |
2583 void StaticDtorDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2584 { | |
2585 if (hgs->hdrgen) | |
2586 return; | |
2587 buf->writestring("static ~this()"); | |
2588 bodyToCBuffer(buf, hgs); | |
2589 } | |
2590 | |
2591 /********************************* InvariantDeclaration ****************************/ | |
2592 | |
2593 InvariantDeclaration::InvariantDeclaration(Loc loc, Loc endloc) | |
2594 : FuncDeclaration(loc, endloc, Id::classInvariant, STCundefined, NULL) | |
2595 { | |
2596 } | |
2597 | |
2598 Dsymbol *InvariantDeclaration::syntaxCopy(Dsymbol *s) | |
2599 { | |
2600 InvariantDeclaration *id; | |
2601 | |
2602 assert(!s); | |
2603 id = new InvariantDeclaration(loc, endloc); | |
2604 FuncDeclaration::syntaxCopy(id); | |
2605 return id; | |
2606 } | |
2607 | |
2608 | |
2609 void InvariantDeclaration::semantic(Scope *sc) | |
2610 { | |
2611 AggregateDeclaration *ad; | |
2612 Type *tret; | |
2613 | |
2614 parent = sc->parent; | |
2615 Dsymbol *parent = toParent(); | |
2616 ad = parent->isAggregateDeclaration(); | |
2617 if (!ad) | |
2618 { | |
2619 error("invariants only are for struct/union/class definitions"); | |
2620 return; | |
2621 } | |
2622 else if (ad->inv && ad->inv != this) | |
2623 { | |
2624 error("more than one invariant for %s", ad->toChars()); | |
2625 } | |
2626 ad->inv = this; | |
2627 type = new TypeFunction(NULL, Type::tvoid, FALSE, LINKd); | |
2628 | |
2629 sc = sc->push(); | |
2630 sc->stc &= ~STCstatic; // not a static invariant | |
2631 sc->incontract++; | |
2632 sc->linkage = LINKd; | |
2633 | |
2634 FuncDeclaration::semantic(sc); | |
2635 | |
2636 sc->pop(); | |
2637 } | |
2638 | |
2639 int InvariantDeclaration::isVirtual() | |
2640 { | |
2641 return FALSE; | |
2642 } | |
2643 | |
2644 int InvariantDeclaration::addPreInvariant() | |
2645 { | |
2646 return FALSE; | |
2647 } | |
2648 | |
2649 int InvariantDeclaration::addPostInvariant() | |
2650 { | |
2651 return FALSE; | |
2652 } | |
2653 | |
2654 void InvariantDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2655 { | |
2656 if (hgs->hdrgen) | |
2657 return; | |
2658 buf->writestring("invariant"); | |
2659 bodyToCBuffer(buf, hgs); | |
2660 } | |
2661 | |
2662 | |
2663 /********************************* UnitTestDeclaration ****************************/ | |
2664 | |
2665 /******************************* | |
2666 * Generate unique unittest function Id so we can have multiple | |
2667 * instances per module. | |
2668 */ | |
2669 | |
2670 static Identifier *unitTestId() | |
2671 { | |
336 | 2672 return Lexer::uniqueId("__unittest"); |
159 | 2673 } |
2674 | |
2675 UnitTestDeclaration::UnitTestDeclaration(Loc loc, Loc endloc) | |
2676 : FuncDeclaration(loc, endloc, unitTestId(), STCundefined, NULL) | |
2677 { | |
2678 } | |
2679 | |
2680 Dsymbol *UnitTestDeclaration::syntaxCopy(Dsymbol *s) | |
2681 { | |
2682 UnitTestDeclaration *utd; | |
2683 | |
2684 assert(!s); | |
2685 utd = new UnitTestDeclaration(loc, endloc); | |
2686 return FuncDeclaration::syntaxCopy(utd); | |
2687 } | |
2688 | |
2689 | |
2690 void UnitTestDeclaration::semantic(Scope *sc) | |
2691 { | |
2692 if (global.params.useUnitTests) | |
2693 { | |
2694 Type *tret; | |
2695 | |
2696 type = new TypeFunction(NULL, Type::tvoid, FALSE, LINKd); | |
2697 FuncDeclaration::semantic(sc); | |
2698 } | |
2699 | |
2700 // We're going to need ModuleInfo even if the unit tests are not | |
2701 // compiled in, because other modules may import this module and refer | |
2702 // to this ModuleInfo. | |
2703 Module *m = getModule(); | |
2704 if (!m) | |
2705 m = sc->module; | |
2706 if (m) | |
2707 m->needmoduleinfo = 1; | |
2708 } | |
2709 | |
2710 AggregateDeclaration *UnitTestDeclaration::isThis() | |
2711 { | |
2712 return NULL; | |
2713 } | |
2714 | |
2715 int UnitTestDeclaration::isVirtual() | |
2716 { | |
2717 return FALSE; | |
2718 } | |
2719 | |
2720 int UnitTestDeclaration::addPreInvariant() | |
2721 { | |
2722 return FALSE; | |
2723 } | |
2724 | |
2725 int UnitTestDeclaration::addPostInvariant() | |
2726 { | |
2727 return FALSE; | |
2728 } | |
2729 | |
2730 void UnitTestDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2731 { | |
2732 if (hgs->hdrgen) | |
2733 return; | |
2734 buf->writestring("unittest"); | |
2735 bodyToCBuffer(buf, hgs); | |
2736 } | |
2737 | |
2738 /********************************* NewDeclaration ****************************/ | |
2739 | |
2740 NewDeclaration::NewDeclaration(Loc loc, Loc endloc, Arguments *arguments, int varargs) | |
2741 : FuncDeclaration(loc, endloc, Id::classNew, STCstatic, NULL) | |
2742 { | |
2743 this->arguments = arguments; | |
2744 this->varargs = varargs; | |
2745 } | |
2746 | |
2747 Dsymbol *NewDeclaration::syntaxCopy(Dsymbol *s) | |
2748 { | |
2749 NewDeclaration *f; | |
2750 | |
2751 f = new NewDeclaration(loc, endloc, NULL, varargs); | |
2752 | |
2753 FuncDeclaration::syntaxCopy(f); | |
2754 | |
2755 f->arguments = Argument::arraySyntaxCopy(arguments); | |
2756 | |
2757 return f; | |
2758 } | |
2759 | |
2760 | |
2761 void NewDeclaration::semantic(Scope *sc) | |
2762 { | |
2763 ClassDeclaration *cd; | |
2764 Type *tret; | |
2765 | |
2766 //printf("NewDeclaration::semantic()\n"); | |
2767 | |
2768 parent = sc->parent; | |
2769 Dsymbol *parent = toParent(); | |
2770 cd = parent->isClassDeclaration(); | |
2771 if (!cd && !parent->isStructDeclaration()) | |
2772 { | |
2773 error("new allocators only are for class or struct definitions"); | |
2774 } | |
2775 tret = Type::tvoid->pointerTo(); | |
2776 type = new TypeFunction(arguments, tret, varargs, LINKd); | |
2777 | |
2778 type = type->semantic(loc, sc); | |
2779 assert(type->ty == Tfunction); | |
2780 | |
2781 // Check that there is at least one argument of type uint | |
2782 TypeFunction *tf = (TypeFunction *)type; | |
2783 if (Argument::dim(tf->parameters) < 1) | |
2784 { | |
2785 error("at least one argument of type uint expected"); | |
2786 } | |
2787 else | |
2788 { | |
2789 Argument *a = Argument::getNth(tf->parameters, 0); | |
2790 if (!a->type->equals(Type::tuns32)) | |
2791 error("first argument must be type uint, not %s", a->type->toChars()); | |
2792 } | |
2793 | |
2794 FuncDeclaration::semantic(sc); | |
2795 } | |
2796 | |
336 | 2797 const char *NewDeclaration::kind() |
159 | 2798 { |
2799 return "allocator"; | |
2800 } | |
2801 | |
2802 int NewDeclaration::isVirtual() | |
2803 { | |
2804 return FALSE; | |
2805 } | |
2806 | |
2807 int NewDeclaration::addPreInvariant() | |
2808 { | |
2809 return FALSE; | |
2810 } | |
2811 | |
2812 int NewDeclaration::addPostInvariant() | |
2813 { | |
2814 return FALSE; | |
2815 } | |
2816 | |
2817 void NewDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2818 { | |
2819 buf->writestring("new"); | |
2820 Argument::argsToCBuffer(buf, hgs, arguments, varargs); | |
2821 bodyToCBuffer(buf, hgs); | |
2822 } | |
2823 | |
2824 | |
2825 /********************************* DeleteDeclaration ****************************/ | |
2826 | |
2827 DeleteDeclaration::DeleteDeclaration(Loc loc, Loc endloc, Arguments *arguments) | |
2828 : FuncDeclaration(loc, endloc, Id::classDelete, STCstatic, NULL) | |
2829 { | |
2830 this->arguments = arguments; | |
2831 } | |
2832 | |
2833 Dsymbol *DeleteDeclaration::syntaxCopy(Dsymbol *s) | |
2834 { | |
2835 DeleteDeclaration *f; | |
2836 | |
2837 f = new DeleteDeclaration(loc, endloc, NULL); | |
2838 | |
2839 FuncDeclaration::syntaxCopy(f); | |
2840 | |
2841 f->arguments = Argument::arraySyntaxCopy(arguments); | |
2842 | |
2843 return f; | |
2844 } | |
2845 | |
2846 | |
2847 void DeleteDeclaration::semantic(Scope *sc) | |
2848 { | |
2849 ClassDeclaration *cd; | |
2850 | |
2851 //printf("DeleteDeclaration::semantic()\n"); | |
2852 | |
2853 parent = sc->parent; | |
2854 Dsymbol *parent = toParent(); | |
2855 cd = parent->isClassDeclaration(); | |
2856 if (!cd && !parent->isStructDeclaration()) | |
2857 { | |
2858 error("new allocators only are for class or struct definitions"); | |
2859 } | |
2860 type = new TypeFunction(arguments, Type::tvoid, 0, LINKd); | |
2861 | |
2862 type = type->semantic(loc, sc); | |
2863 assert(type->ty == Tfunction); | |
2864 | |
2865 // Check that there is only one argument of type void* | |
2866 TypeFunction *tf = (TypeFunction *)type; | |
2867 if (Argument::dim(tf->parameters) != 1) | |
2868 { | |
2869 error("one argument of type void* expected"); | |
2870 } | |
2871 else | |
2872 { | |
2873 Argument *a = Argument::getNth(tf->parameters, 0); | |
2874 if (!a->type->equals(Type::tvoid->pointerTo())) | |
2875 error("one argument of type void* expected, not %s", a->type->toChars()); | |
2876 } | |
2877 | |
2878 FuncDeclaration::semantic(sc); | |
2879 } | |
2880 | |
336 | 2881 const char *DeleteDeclaration::kind() |
159 | 2882 { |
2883 return "deallocator"; | |
2884 } | |
2885 | |
2886 int DeleteDeclaration::isDelete() | |
2887 { | |
2888 return TRUE; | |
2889 } | |
2890 | |
2891 int DeleteDeclaration::isVirtual() | |
2892 { | |
2893 return FALSE; | |
2894 } | |
2895 | |
2896 int DeleteDeclaration::addPreInvariant() | |
2897 { | |
2898 return FALSE; | |
2899 } | |
2900 | |
2901 int DeleteDeclaration::addPostInvariant() | |
2902 { | |
2903 return FALSE; | |
2904 } | |
2905 | |
2906 void DeleteDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2907 { | |
2908 buf->writestring("delete"); | |
2909 Argument::argsToCBuffer(buf, hgs, arguments, 0); | |
2910 bodyToCBuffer(buf, hgs); | |
2911 } | |
2912 | |
2913 | |
2914 | |
2915 |