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