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