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