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