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