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