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