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