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