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