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