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