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