Mercurial > projects > ldc
annotate dmd/func.c @ 1146:1860414bf3b7
* Moved ir/irsymbol.cpp/h into ir/irdsymbol.cpp/h.
* Added #if IN_DMD versioning around DMD backend specific code in the D1 frontend, D2 is currently broken.
* All part of a greater cleanup scheme.
author | Tomas Lindquist Olsen <tomas.l.olsen gmail.com> |
---|---|
date | Thu, 26 Mar 2009 20:45:53 +0100 |
parents | b28a57f4b530 |
children | 08c1c3bfea5a |
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 |
f99a3b393c03
Reorganize EnclosingHandlers to require less changes to the frontend and allow us to
Christian Kamm <kamm incasoftware de>
parents:
1133
diff
changeset
|
1270 // is is ok to not rerun semantic on the whole fbody here; only the enclosingScopeExit |
f99a3b393c03
Reorganize EnclosingHandlers to require less changes to the frontend and allow us to
Christian Kamm <kamm incasoftware de>
parents:
1133
diff
changeset
|
1271 // value will differ and as it is impossible to goto out of this 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
|
1272 // that should not lead to errors |
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 |
f99a3b393c03
Reorganize EnclosingHandlers to require less changes to the frontend and allow us to
Christian Kamm <kamm incasoftware de>
parents:
1133
diff
changeset
|
1277 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
|
1278 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
|
1279 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
|
1280 } |
159 | 1281 } |
1282 | |
1283 sc2->callSuper = 0; | |
1284 sc2->pop(); | |
1285 } | |
1286 semanticRun = 2; | |
1287 } | |
1288 | |
1289 void FuncDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
1290 { | |
1291 //printf("FuncDeclaration::toCBuffer() '%s'\n", toChars()); | |
1292 | |
1293 type->toCBuffer(buf, ident, hgs); | |
1294 bodyToCBuffer(buf, hgs); | |
1295 } | |
1296 | |
1297 | |
1298 void FuncDeclaration::bodyToCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
1299 { | |
1300 if (fbody && | |
1301 (!hgs->hdrgen || hgs->tpltMember || canInline(1,1)) | |
1302 ) | |
1303 { buf->writenl(); | |
1304 | |
1305 // in{} | |
1306 if (frequire) | |
1307 { buf->writestring("in"); | |
1308 buf->writenl(); | |
1309 frequire->toCBuffer(buf, hgs); | |
1310 } | |
1311 | |
1312 // out{} | |
1313 if (fensure) | |
1314 { buf->writestring("out"); | |
1315 if (outId) | |
1316 { buf->writebyte('('); | |
1317 buf->writestring(outId->toChars()); | |
1318 buf->writebyte(')'); | |
1319 } | |
1320 buf->writenl(); | |
1321 fensure->toCBuffer(buf, hgs); | |
1322 } | |
1323 | |
1324 if (frequire || fensure) | |
1325 { buf->writestring("body"); | |
1326 buf->writenl(); | |
1327 } | |
1328 | |
1329 buf->writebyte('{'); | |
1330 buf->writenl(); | |
1331 fbody->toCBuffer(buf, hgs); | |
1332 buf->writebyte('}'); | |
1333 buf->writenl(); | |
1334 } | |
1335 else | |
1336 { buf->writeByte(';'); | |
1337 buf->writenl(); | |
1338 } | |
1339 } | |
1340 | |
1341 /**************************************************** | |
1342 * Determine if 'this' overrides fd. | |
1343 * Return !=0 if it does. | |
1344 */ | |
1345 | |
1346 int FuncDeclaration::overrides(FuncDeclaration *fd) | |
1347 { int result = 0; | |
1348 | |
1349 if (fd->ident == ident) | |
1350 { | |
1351 int cov = type->covariant(fd->type); | |
1352 if (cov) | |
1353 { ClassDeclaration *cd1 = toParent()->isClassDeclaration(); | |
1354 ClassDeclaration *cd2 = fd->toParent()->isClassDeclaration(); | |
1355 | |
1356 if (cd1 && cd2 && cd2->isBaseOf(cd1, NULL)) | |
1357 result = 1; | |
1358 } | |
1359 } | |
1360 return result; | |
1361 } | |
1362 | |
1363 /************************************************* | |
1364 * Find index of function in vtbl[0..dim] that | |
1365 * this function overrides. | |
1366 * Returns: | |
1367 * -1 didn't find one | |
1368 * -2 can't determine because of forward references | |
1369 */ | |
1370 | |
1371 int FuncDeclaration::findVtblIndex(Array *vtbl, int dim) | |
1372 { | |
1373 for (int vi = 0; vi < dim; vi++) | |
1374 { | |
1375 FuncDeclaration *fdv = ((Dsymbol *)vtbl->data[vi])->isFuncDeclaration(); | |
1376 if (fdv && fdv->ident == ident) | |
1377 { | |
1378 int cov = type->covariant(fdv->type); | |
1379 //printf("\tbaseclass cov = %d\n", cov); | |
1380 switch (cov) | |
1381 { | |
1382 case 0: // types are distinct | |
1383 break; | |
1384 | |
1385 case 1: | |
1386 return vi; | |
1387 | |
1388 case 2: | |
1389 //type->print(); | |
1390 //fdv->type->print(); | |
1391 //printf("%s %s\n", type->deco, fdv->type->deco); | |
1392 error("of type %s overrides but is not covariant with %s of type %s", | |
1393 type->toChars(), fdv->toPrettyChars(), fdv->type->toChars()); | |
1394 break; | |
1395 | |
1396 case 3: | |
1397 return -2; // forward references | |
1398 | |
1399 default: | |
1400 assert(0); | |
1401 } | |
1402 } | |
1403 } | |
1404 return -1; | |
1405 } | |
1406 | |
1407 /**************************************************** | |
1408 * Overload this FuncDeclaration with the new one f. | |
1409 * Return !=0 if successful; i.e. no conflict. | |
1410 */ | |
1411 | |
1412 int FuncDeclaration::overloadInsert(Dsymbol *s) | |
1413 { | |
1414 FuncDeclaration *f; | |
1415 AliasDeclaration *a; | |
1416 | |
1417 //printf("FuncDeclaration::overloadInsert(%s)\n", s->toChars()); | |
1418 a = s->isAliasDeclaration(); | |
1419 if (a) | |
1420 { | |
1421 if (overnext) | |
1422 return overnext->overloadInsert(a); | |
1423 if (!a->aliassym && a->type->ty != Tident && a->type->ty != Tinstance) | |
1424 { | |
1425 //printf("\ta = '%s'\n", a->type->toChars()); | |
1426 return FALSE; | |
1427 } | |
1428 overnext = a; | |
1429 //printf("\ttrue: no conflict\n"); | |
1430 return TRUE; | |
1431 } | |
1432 f = s->isFuncDeclaration(); | |
1433 if (!f) | |
1434 return FALSE; | |
1435 | |
1436 if (type && f->type && // can be NULL for overloaded constructors | |
1437 f->type->covariant(type) && | |
1438 !isFuncAliasDeclaration()) | |
1439 { | |
1440 //printf("\tfalse: conflict %s\n", kind()); | |
1441 return FALSE; | |
1442 } | |
1443 | |
1444 if (overnext) | |
1445 return overnext->overloadInsert(f); | |
1446 overnext = f; | |
1447 //printf("\ttrue: no conflict\n"); | |
1448 return TRUE; | |
1449 } | |
1450 | |
1451 /******************************************** | |
1452 * Find function in overload list that exactly matches t. | |
1453 */ | |
1454 | |
1455 /*************************************************** | |
1456 * Visit each overloaded function in turn, and call | |
1457 * (*fp)(param, f) on it. | |
1458 * Exit when no more, or (*fp)(param, f) returns 1. | |
1459 * Returns: | |
1460 * 0 continue | |
1461 * 1 done | |
1462 */ | |
1463 | |
1464 int overloadApply(FuncDeclaration *fstart, | |
1465 int (*fp)(void *, FuncDeclaration *), | |
1466 void *param) | |
1467 { | |
1468 FuncDeclaration *f; | |
1469 Declaration *d; | |
1470 Declaration *next; | |
1471 | |
1472 for (d = fstart; d; d = next) | |
1473 { FuncAliasDeclaration *fa = d->isFuncAliasDeclaration(); | |
1474 | |
1475 if (fa) | |
1476 { | |
1477 if (overloadApply(fa->funcalias, fp, param)) | |
1478 return 1; | |
1479 next = fa->overnext; | |
1480 } | |
1481 else | |
1482 { | |
1483 AliasDeclaration *a = d->isAliasDeclaration(); | |
1484 | |
1485 if (a) | |
1486 { | |
1487 Dsymbol *s = a->toAlias(); | |
1488 next = s->isDeclaration(); | |
1489 if (next == a) | |
1490 break; | |
1491 if (next == fstart) | |
1492 break; | |
1493 } | |
1494 else | |
1495 { | |
1496 f = d->isFuncDeclaration(); | |
1497 if (!f) | |
1498 { d->error("is aliased to a function"); | |
1499 break; // BUG: should print error message? | |
1500 } | |
1501 if ((*fp)(param, f)) | |
1502 return 1; | |
1503 | |
1504 next = f->overnext; | |
1505 } | |
1506 } | |
1507 } | |
1508 return 0; | |
1509 } | |
1510 | |
1511 /******************************************** | |
1512 * Find function in overload list that exactly matches t. | |
1513 */ | |
1514 | |
1515 struct Param1 | |
1516 { | |
1517 Type *t; // type to match | |
1518 FuncDeclaration *f; // return value | |
1519 }; | |
1520 | |
1521 int fp1(void *param, FuncDeclaration *f) | |
1522 { Param1 *p = (Param1 *)param; | |
1523 Type *t = p->t; | |
1524 | |
1525 if (t->equals(f->type)) | |
1526 { p->f = f; | |
1527 return 1; | |
1528 } | |
1529 | |
336 | 1530 #if DMDV2 |
159 | 1531 /* Allow covariant matches, if it's just a const conversion |
1532 * of the return type | |
1533 */ | |
1534 if (t->ty == Tfunction) | |
1535 { TypeFunction *tf = (TypeFunction *)f->type; | |
1536 if (tf->covariant(t) == 1 && | |
1537 tf->nextOf()->implicitConvTo(t->nextOf()) >= MATCHconst) | |
1538 { | |
1539 p->f = f; | |
1540 return 1; | |
1541 } | |
1542 } | |
1543 #endif | |
1544 return 0; | |
1545 } | |
1546 | |
1547 FuncDeclaration *FuncDeclaration::overloadExactMatch(Type *t) | |
1548 { | |
1549 Param1 p; | |
1550 p.t = t; | |
1551 p.f = NULL; | |
1552 overloadApply(this, &fp1, &p); | |
1553 return p.f; | |
1554 } | |
1555 | |
1556 #if 0 | |
1557 FuncDeclaration *FuncDeclaration::overloadExactMatch(Type *t) | |
1558 { | |
1559 FuncDeclaration *f; | |
1560 Declaration *d; | |
1561 Declaration *next; | |
1562 | |
1563 for (d = this; d; d = next) | |
1564 { FuncAliasDeclaration *fa = d->isFuncAliasDeclaration(); | |
1565 | |
1566 if (fa) | |
1567 { | |
1568 FuncDeclaration *f2 = fa->funcalias->overloadExactMatch(t); | |
1569 if (f2) | |
1570 return f2; | |
1571 next = fa->overnext; | |
1572 } | |
1573 else | |
1574 { | |
1575 AliasDeclaration *a = d->isAliasDeclaration(); | |
1576 | |
1577 if (a) | |
1578 { | |
1579 Dsymbol *s = a->toAlias(); | |
1580 next = s->isDeclaration(); | |
1581 if (next == a) | |
1582 break; | |
1583 } | |
1584 else | |
1585 { | |
1586 f = d->isFuncDeclaration(); | |
1587 if (!f) | |
1588 break; // BUG: should print error message? | |
1589 if (t->equals(d->type)) | |
1590 return f; | |
1591 next = f->overnext; | |
1592 } | |
1593 } | |
1594 } | |
1595 return NULL; | |
1596 } | |
1597 #endif | |
1598 | |
1599 /******************************************** | |
1600 * Decide which function matches the arguments best. | |
1601 */ | |
1602 | |
1603 struct Param2 | |
1604 { | |
1605 Match *m; | |
1606 Expressions *arguments; | |
1607 }; | |
1608 | |
1609 int fp2(void *param, FuncDeclaration *f) | |
1610 { Param2 *p = (Param2 *)param; | |
1611 Match *m = p->m; | |
1612 Expressions *arguments = p->arguments; | |
1613 MATCH match; | |
1614 | |
1615 if (f != m->lastf) // skip duplicates | |
1616 { | |
1617 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
|
1618 TypeFunction *tf = (TypeFunction *)f->type; |
159 | 1619 match = (MATCH) tf->callMatch(arguments); |
717
a26b0c5d5942
Merged DMD 1.036.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
703
diff
changeset
|
1620 //printf("1match = %d\n", match); |
159 | 1621 if (match != MATCHnomatch) |
1622 { | |
1623 if (match > m->last) | |
1624 goto LfIsBetter; | |
1625 | |
1626 if (match < m->last) | |
1627 goto LlastIsBetter; | |
1628 | |
1629 /* See if one of the matches overrides the other. | |
1630 */ | |
1631 if (m->lastf->overrides(f)) | |
1632 goto LlastIsBetter; | |
1633 else if (f->overrides(m->lastf)) | |
1634 goto LfIsBetter; | |
1635 | |
1636 Lambiguous: | |
1637 m->nextf = f; | |
1638 m->count++; | |
1639 return 0; | |
1640 | |
1641 LfIsBetter: | |
1642 m->last = match; | |
1643 m->lastf = f; | |
1644 m->count = 1; | |
1645 return 0; | |
1646 | |
1647 LlastIsBetter: | |
1648 return 0; | |
1649 } | |
1650 } | |
1651 return 0; | |
1652 } | |
1653 | |
1654 | |
1655 void overloadResolveX(Match *m, FuncDeclaration *fstart, Expressions *arguments) | |
1656 { | |
1657 Param2 p; | |
1658 p.m = m; | |
1659 p.arguments = arguments; | |
1660 overloadApply(fstart, &fp2, &p); | |
1661 } | |
1662 | |
1663 #if 0 | |
1664 // Recursive helper function | |
1665 | |
1666 void overloadResolveX(Match *m, FuncDeclaration *fstart, Expressions *arguments) | |
1667 { | |
1668 MATCH match; | |
1669 Declaration *d; | |
1670 Declaration *next; | |
1671 | |
1672 for (d = fstart; d; d = next) | |
1673 { | |
1674 FuncDeclaration *f; | |
1675 FuncAliasDeclaration *fa; | |
1676 AliasDeclaration *a; | |
1677 | |
1678 fa = d->isFuncAliasDeclaration(); | |
1679 if (fa) | |
1680 { | |
1681 overloadResolveX(m, fa->funcalias, arguments); | |
1682 next = fa->overnext; | |
1683 } | |
1684 else if ((f = d->isFuncDeclaration()) != NULL) | |
1685 { | |
1686 next = f->overnext; | |
1687 if (f == m->lastf) | |
1688 continue; // skip duplicates | |
1689 else | |
1690 { | |
1691 TypeFunction *tf; | |
1692 | |
1693 m->anyf = f; | |
1694 tf = (TypeFunction *)f->type; | |
1695 match = (MATCH) tf->callMatch(arguments); | |
717
a26b0c5d5942
Merged DMD 1.036.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
703
diff
changeset
|
1696 //printf("2match = %d\n", match); |
159 | 1697 if (match != MATCHnomatch) |
1698 { | |
1699 if (match > m->last) | |
1700 goto LfIsBetter; | |
1701 | |
1702 if (match < m->last) | |
1703 goto LlastIsBetter; | |
1704 | |
1705 /* See if one of the matches overrides the other. | |
1706 */ | |
1707 if (m->lastf->overrides(f)) | |
1708 goto LlastIsBetter; | |
1709 else if (f->overrides(m->lastf)) | |
1710 goto LfIsBetter; | |
1711 | |
1712 Lambiguous: | |
1713 m->nextf = f; | |
1714 m->count++; | |
1715 continue; | |
1716 | |
1717 LfIsBetter: | |
1718 m->last = match; | |
1719 m->lastf = f; | |
1720 m->count = 1; | |
1721 continue; | |
1722 | |
1723 LlastIsBetter: | |
1724 continue; | |
1725 } | |
1726 } | |
1727 } | |
1728 else if ((a = d->isAliasDeclaration()) != NULL) | |
1729 { | |
1730 Dsymbol *s = a->toAlias(); | |
1731 next = s->isDeclaration(); | |
1732 if (next == a) | |
1733 break; | |
1734 if (next == fstart) | |
1735 break; | |
1736 } | |
1737 else | |
1738 { d->error("is aliased to a function"); | |
1739 break; | |
1740 } | |
1741 } | |
1742 } | |
1743 #endif | |
1744 | |
1745 FuncDeclaration *FuncDeclaration::overloadResolve(Loc loc, Expressions *arguments) | |
1746 { | |
1747 TypeFunction *tf; | |
1748 Match m; | |
1749 | |
1750 #if 0 | |
1751 printf("FuncDeclaration::overloadResolve('%s')\n", toChars()); | |
1752 if (arguments) | |
1753 { int i; | |
1754 | |
1755 for (i = 0; i < arguments->dim; i++) | |
1756 { Expression *arg; | |
1757 | |
1758 arg = (Expression *)arguments->data[i]; | |
1759 assert(arg->type); | |
1760 printf("\t%s: ", arg->toChars()); | |
1761 arg->type->print(); | |
1762 } | |
1763 } | |
1764 #endif | |
1765 | |
1766 memset(&m, 0, sizeof(m)); | |
1767 m.last = MATCHnomatch; | |
1768 overloadResolveX(&m, this, arguments); | |
1769 | |
1770 if (m.count == 1) // exactly one match | |
1771 { | |
1772 return m.lastf; | |
1773 } | |
1774 else | |
1775 { | |
1776 OutBuffer buf; | |
1777 | |
1778 if (arguments) | |
1779 { | |
1780 HdrGenState hgs; | |
1781 | |
1782 argExpTypesToCBuffer(&buf, arguments, &hgs); | |
1783 } | |
1784 | |
1785 if (m.last == MATCHnomatch) | |
1786 { | |
1787 tf = (TypeFunction *)type; | |
1788 | |
1789 //printf("tf = %s, args = %s\n", tf->deco, ((Expression *)arguments->data[0])->type->deco); | |
1790 error(loc, "%s does not match parameter types (%s)", | |
1791 Argument::argsTypesToChars(tf->parameters, tf->varargs), | |
1792 buf.toChars()); | |
1793 return m.anyf; // as long as it's not a FuncAliasDeclaration | |
1794 } | |
1795 else | |
1796 { | |
1797 #if 1 | |
1798 TypeFunction *t1 = (TypeFunction *)m.lastf->type; | |
1799 TypeFunction *t2 = (TypeFunction *)m.nextf->type; | |
1800 | |
1801 error(loc, "called with argument types:\n\t(%s)\nmatches both:\n\t%s%s\nand:\n\t%s%s", | |
1802 buf.toChars(), | |
1803 m.lastf->toPrettyChars(), Argument::argsTypesToChars(t1->parameters, t1->varargs), | |
1804 m.nextf->toPrettyChars(), Argument::argsTypesToChars(t2->parameters, t2->varargs)); | |
1805 #else | |
1806 error(loc, "overloads %s and %s both match argument list for %s", | |
1807 m.lastf->type->toChars(), | |
1808 m.nextf->type->toChars(), | |
1809 m.lastf->toChars()); | |
1810 #endif | |
1811 return m.lastf; | |
1812 } | |
1813 } | |
1814 } | |
1815 | |
1816 /******************************** | |
1817 * Labels are in a separate scope, one per function. | |
1818 */ | |
1819 | |
1820 LabelDsymbol *FuncDeclaration::searchLabel(Identifier *ident) | |
1821 { Dsymbol *s; | |
1822 | |
1823 if (!labtab) | |
1824 labtab = new DsymbolTable(); // guess we need one | |
1825 | |
1826 s = labtab->lookup(ident); | |
1827 if (!s) | |
1828 { | |
1829 s = new LabelDsymbol(ident); | |
1830 labtab->insert(s); | |
1831 } | |
1832 return (LabelDsymbol *)s; | |
1833 } | |
336 | 1834 |
159 | 1835 /**************************************** |
1836 * If non-static member function that has a 'this' pointer, | |
1837 * return the aggregate it is a member of. | |
1838 * Otherwise, return NULL. | |
1839 */ | |
1840 | |
1841 AggregateDeclaration *FuncDeclaration::isThis() | |
1842 { AggregateDeclaration *ad; | |
1843 | |
1844 //printf("+FuncDeclaration::isThis() '%s'\n", toChars()); | |
1845 ad = NULL; | |
1846 if ((storage_class & STCstatic) == 0) | |
1847 { | |
1848 ad = isMember2(); | |
1849 } | |
1850 //printf("-FuncDeclaration::isThis() %p\n", ad); | |
1851 return ad; | |
1852 } | |
1853 | |
1854 AggregateDeclaration *FuncDeclaration::isMember2() | |
1855 { AggregateDeclaration *ad; | |
1856 | |
1857 //printf("+FuncDeclaration::isMember2() '%s'\n", toChars()); | |
1858 ad = NULL; | |
1859 for (Dsymbol *s = this; s; s = s->parent) | |
1860 { | |
1861 //printf("\ts = '%s', parent = '%s', kind = %s\n", s->toChars(), s->parent->toChars(), s->parent->kind()); | |
1862 ad = s->isMember(); | |
1863 if (ad) | |
1864 { //printf("test4\n"); | |
1865 break; | |
1866 } | |
1867 if (!s->parent || | |
1868 (!s->parent->isTemplateInstance())) | |
1869 { //printf("test5\n"); | |
1870 break; | |
1871 } | |
1872 } | |
1873 //printf("-FuncDeclaration::isMember2() %p\n", ad); | |
1874 return ad; | |
1875 } | |
1876 | |
1877 /***************************************** | |
1878 * Determine lexical level difference from 'this' to nested function 'fd'. | |
1879 * Error if this cannot call fd. | |
1880 * Returns: | |
1881 * 0 same level | |
1882 * -1 increase nesting by 1 (fd is nested within 'this') | |
1883 * >0 decrease nesting by number | |
1884 */ | |
1885 | |
1886 int FuncDeclaration::getLevel(Loc loc, FuncDeclaration *fd) | |
1887 { int level; | |
1888 Dsymbol *s; | |
1889 Dsymbol *fdparent; | |
1890 | |
1891 //printf("FuncDeclaration::getLevel(fd = '%s')\n", fd->toChars()); | |
1892 fdparent = fd->toParent2(); | |
1893 if (fdparent == this) | |
1894 return -1; | |
1895 s = this; | |
1896 level = 0; | |
1897 while (fd != s && fdparent != s->toParent2()) | |
1898 { | |
1899 //printf("\ts = '%s'\n", s->toChars()); | |
1900 FuncDeclaration *thisfd = s->isFuncDeclaration(); | |
1901 if (thisfd) | |
1902 { if (!thisfd->isNested() && !thisfd->vthis) | |
1903 goto Lerr; | |
1904 } | |
1905 else | |
1906 { | |
1907 ClassDeclaration *thiscd = s->isClassDeclaration(); | |
1908 if (thiscd) | |
1909 { if (!thiscd->isNested()) | |
1910 goto Lerr; | |
1911 } | |
1912 else | |
1913 goto Lerr; | |
1914 } | |
1915 | |
1916 s = s->toParent2(); | |
1917 assert(s); | |
1918 level++; | |
1919 } | |
1920 return level; | |
1921 | |
1922 Lerr: | |
1923 error(loc, "cannot access frame of function %s", fd->toChars()); | |
1924 return 1; | |
1925 } | |
1926 | |
1927 void FuncDeclaration::appendExp(Expression *e) | |
1928 { Statement *s; | |
1929 | |
1930 s = new ExpStatement(0, e); | |
1931 appendState(s); | |
1932 } | |
1933 | |
1934 void FuncDeclaration::appendState(Statement *s) | |
1935 { CompoundStatement *cs; | |
1936 | |
1937 if (!fbody) | |
1938 { Statements *a; | |
1939 | |
1940 a = new Statements(); | |
1941 fbody = new CompoundStatement(0, a); | |
1942 } | |
1943 cs = fbody->isCompoundStatement(); | |
1944 cs->statements->push(s); | |
1945 } | |
1946 | |
1947 | |
1948 int FuncDeclaration::isMain() | |
1949 { | |
1950 return ident == Id::main && | |
1951 linkage != LINKc && !isMember() && !isNested(); | |
1952 } | |
1953 | |
1954 int FuncDeclaration::isWinMain() | |
1955 { | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
468
diff
changeset
|
1956 //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
|
1957 #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
|
1958 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
|
1959 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
|
1960 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
|
1961 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
|
1962 #else |
159 | 1963 return ident == Id::WinMain && |
1964 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
|
1965 #endif |
159 | 1966 } |
1967 | |
1968 int FuncDeclaration::isDllMain() | |
1969 { | |
1970 return ident == Id::DllMain && | |
1971 linkage != LINKc && !isMember(); | |
1972 } | |
1973 | |
1974 int FuncDeclaration::isExport() | |
1975 { | |
1976 return protection == PROTexport; | |
1977 } | |
1978 | |
1979 int FuncDeclaration::isImportedSymbol() | |
1980 { | |
1981 //printf("isImportedSymbol()\n"); | |
1982 //printf("protection = %d\n", protection); | |
1983 return (protection == PROTexport) && !fbody; | |
1984 } | |
1985 | |
1986 // Determine if function goes into virtual function pointer table | |
1987 | |
1988 int FuncDeclaration::isVirtual() | |
1989 { | |
1990 #if 0 | |
1991 printf("FuncDeclaration::isVirtual(%s)\n", toChars()); | |
1992 printf("%p %d %d %d %d\n", isMember(), isStatic(), protection == PROTprivate, isCtorDeclaration(), linkage != LINKd); | |
1993 printf("result is %d\n", | |
1994 isMember() && | |
1995 !(isStatic() || protection == PROTprivate || protection == PROTpackage) && | |
1996 toParent()->isClassDeclaration()); | |
1997 #endif | |
1998 return isMember() && | |
1999 !(isStatic() || protection == PROTprivate || protection == PROTpackage) && | |
2000 toParent()->isClassDeclaration(); | |
2001 } | |
2002 | |
846
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
717
diff
changeset
|
2003 int FuncDeclaration::isFinal() |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
717
diff
changeset
|
2004 { |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
717
diff
changeset
|
2005 ClassDeclaration *cd; |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
717
diff
changeset
|
2006 #if 0 |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
717
diff
changeset
|
2007 printf("FuncDeclaration::isFinal(%s)\n", toChars()); |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
717
diff
changeset
|
2008 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
|
2009 printf("result is %d\n", |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
717
diff
changeset
|
2010 isMember() && |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
717
diff
changeset
|
2011 !(isStatic() || protection == PROTprivate || protection == PROTpackage) && |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
717
diff
changeset
|
2012 (cd = toParent()->isClassDeclaration()) != NULL && |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
717
diff
changeset
|
2013 cd->storage_class & STCfinal); |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
717
diff
changeset
|
2014 #endif |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
717
diff
changeset
|
2015 return isMember() && |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
717
diff
changeset
|
2016 (Declaration::isFinal() || |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
717
diff
changeset
|
2017 ((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
|
2018 } |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
717
diff
changeset
|
2019 |
159 | 2020 int FuncDeclaration::isAbstract() |
2021 { | |
2022 return storage_class & STCabstract; | |
2023 } | |
2024 | |
2025 int FuncDeclaration::isCodeseg() | |
2026 { | |
2027 return TRUE; // functions are always in the code segment | |
2028 } | |
2029 | |
2030 // Determine if function needs | |
2031 // a static frame pointer to its lexically enclosing function | |
2032 | |
2033 int FuncDeclaration::isNested() | |
2034 { | |
2035 //if (!toParent()) | |
2036 //printf("FuncDeclaration::isNested('%s') parent=%p\n", toChars(), parent); | |
2037 //printf("\ttoParent() = '%s'\n", toParent()->toChars()); | |
2038 return ((storage_class & STCstatic) == 0) && | |
2039 (toParent2()->isFuncDeclaration() != NULL); | |
2040 } | |
2041 | |
2042 int FuncDeclaration::needThis() | |
2043 { | |
2044 //printf("FuncDeclaration::needThis() '%s'\n", toChars()); | |
2045 int i = isThis() != NULL; | |
2046 //printf("\t%d\n", i); | |
2047 if (!i && isFuncAliasDeclaration()) | |
2048 i = ((FuncAliasDeclaration *)this)->funcalias->needThis(); | |
2049 return i; | |
2050 } | |
2051 | |
2052 int FuncDeclaration::addPreInvariant() | |
2053 { | |
2054 AggregateDeclaration *ad = isThis(); | |
2055 return (ad && | |
2056 //ad->isClassDeclaration() && | |
2057 global.params.useInvariants && | |
2058 (protection == PROTpublic || protection == PROTexport) && | |
2059 !naked); | |
2060 } | |
2061 | |
2062 int FuncDeclaration::addPostInvariant() | |
2063 { | |
2064 AggregateDeclaration *ad = isThis(); | |
2065 return (ad && | |
2066 ad->inv && | |
2067 //ad->isClassDeclaration() && | |
2068 global.params.useInvariants && | |
2069 (protection == PROTpublic || protection == PROTexport) && | |
2070 !naked); | |
2071 } | |
2072 | |
2073 /********************************** | |
2074 * Generate a FuncDeclaration for a runtime library function. | |
2075 */ | |
2076 | |
378
d8234836b40f
Get rid of runTimeHack and instead add proper argument info to the frontend
Christian Kamm <kamm incasoftware de>
parents:
336
diff
changeset
|
2077 // |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
658
diff
changeset
|
2078 // 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
|
2079 // |
d8234836b40f
Get rid of runTimeHack and instead add proper argument info to the frontend
Christian Kamm <kamm incasoftware de>
parents:
336
diff
changeset
|
2080 |
1133
eeb8b95ea92e
Cleanup DMD 1.041 merge.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1103
diff
changeset
|
2081 FuncDeclaration *FuncDeclaration::genCfunc(Arguments *args, Type *treturn, const char *name) |
159 | 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 return genCfunc(args, treturn, Lexer::idPool(name)); |
159 | 2084 } |
2085 | |
378
d8234836b40f
Get rid of runTimeHack and instead add proper argument info to the frontend
Christian Kamm <kamm incasoftware de>
parents:
336
diff
changeset
|
2086 FuncDeclaration *FuncDeclaration::genCfunc(Arguments *args, Type *treturn, Identifier *id) |
159 | 2087 { |
2088 FuncDeclaration *fd; | |
2089 TypeFunction *tf; | |
2090 Dsymbol *s; | |
2091 static DsymbolTable *st = NULL; | |
2092 | |
2093 //printf("genCfunc(name = '%s')\n", id->toChars()); | |
2094 //printf("treturn\n\t"); treturn->print(); | |
2095 | |
2096 // See if already in table | |
2097 if (!st) | |
2098 st = new DsymbolTable(); | |
2099 s = st->lookup(id); | |
2100 if (s) | |
2101 { | |
2102 fd = s->isFuncDeclaration(); | |
2103 assert(fd); | |
2104 assert(fd->type->nextOf()->equals(treturn)); | |
2105 } | |
2106 else | |
2107 { | |
378
d8234836b40f
Get rid of runTimeHack and instead add proper argument info to the frontend
Christian Kamm <kamm incasoftware de>
parents:
336
diff
changeset
|
2108 tf = new TypeFunction(args, treturn, 0, LINKc); |
159 | 2109 fd = new FuncDeclaration(0, 0, id, STCstatic, tf); |
2110 fd->protection = PROTpublic; | |
2111 fd->linkage = LINKc; | |
2112 | |
2113 st->insert(fd); | |
2114 } | |
2115 return fd; | |
2116 } | |
2117 | |
336 | 2118 const char *FuncDeclaration::kind() |
159 | 2119 { |
2120 return "function"; | |
2121 } | |
336 | 2122 |
159 | 2123 /******************************* |
2124 * Look at all the variables in this function that are referenced | |
2125 * by nested functions, and determine if a closure needs to be | |
2126 * created for them. | |
2127 */ | |
2128 | |
336 | 2129 #if DMDV2 |
159 | 2130 int FuncDeclaration::needsClosure() |
2131 { | |
2132 /* Need a closure for all the closureVars[] if any of the | |
2133 * closureVars[] are accessed by a | |
2134 * function that escapes the scope of this function. | |
2135 * We take the conservative approach and decide that any function that: | |
2136 * 1) is a virtual function | |
2137 * 2) has its address taken | |
2138 * 3) has a parent that escapes | |
336 | 2139 * |
2140 * Note that since a non-virtual function can be called by | |
2141 * a virtual one, if that non-virtual function accesses a closure | |
2142 * var, the closure still has to be taken. Hence, we check for isThis() | |
2143 * instead of isVirtual(). (thanks to David Friedman) | |
159 | 2144 */ |
2145 | |
2146 //printf("FuncDeclaration::needsClosure() %s\n", toChars()); | |
2147 for (int i = 0; i < closureVars.dim; i++) | |
2148 { VarDeclaration *v = (VarDeclaration *)closureVars.data[i]; | |
2149 assert(v->isVarDeclaration()); | |
2150 //printf("\tv = %s\n", v->toChars()); | |
2151 | |
2152 for (int j = 0; j < v->nestedrefs.dim; j++) | |
2153 { FuncDeclaration *f = (FuncDeclaration *)v->nestedrefs.data[j]; | |
2154 assert(f != this); | |
2155 | |
2156 //printf("\t\tf = %s, %d, %d\n", f->toChars(), f->isVirtual(), f->tookAddressOf); | |
336 | 2157 if (f->isThis() || f->tookAddressOf) |
159 | 2158 goto Lyes; // assume f escapes this function's scope |
2159 | |
2160 // 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
|
2161 for (Dsymbol *s = f->parent; s && s != this; s = s->parent) |
159 | 2162 { |
2163 f = s->isFuncDeclaration(); | |
336 | 2164 if (f && (f->isThis() || f->tookAddressOf)) |
159 | 2165 goto Lyes; |
2166 } | |
2167 } | |
2168 } | |
2169 return 0; | |
2170 | |
2171 Lyes: | |
2172 //printf("\tneeds closure\n"); | |
2173 return 1; | |
2174 } | |
2175 #endif | |
2176 | |
2177 /****************************** FuncAliasDeclaration ************************/ | |
2178 | |
2179 // Used as a way to import a set of functions from another scope into this one. | |
2180 | |
2181 FuncAliasDeclaration::FuncAliasDeclaration(FuncDeclaration *funcalias) | |
2182 : FuncDeclaration(funcalias->loc, funcalias->endloc, funcalias->ident, | |
2183 (enum STC)funcalias->storage_class, funcalias->type) | |
2184 { | |
2185 assert(funcalias != this); | |
2186 this->funcalias = funcalias; | |
2187 } | |
2188 | |
336 | 2189 const char *FuncAliasDeclaration::kind() |
159 | 2190 { |
2191 return "function alias"; | |
2192 } | |
2193 | |
2194 | |
2195 /****************************** FuncLiteralDeclaration ************************/ | |
2196 | |
2197 FuncLiteralDeclaration::FuncLiteralDeclaration(Loc loc, Loc endloc, Type *type, | |
2198 enum TOK tok, ForeachStatement *fes) | |
2199 : FuncDeclaration(loc, endloc, NULL, STCundefined, type) | |
2200 { | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
957
diff
changeset
|
2201 const char *id; |
159 | 2202 |
2203 if (fes) | |
2204 id = "__foreachbody"; | |
2205 else if (tok == TOKdelegate) | |
2206 id = "__dgliteral"; | |
2207 else | |
2208 id = "__funcliteral"; | |
2209 this->ident = Identifier::generateId(id); | |
2210 this->tok = tok; | |
2211 this->fes = fes; | |
2212 //printf("FuncLiteralDeclaration() id = '%s', type = '%s'\n", this->ident->toChars(), type->toChars()); | |
2213 } | |
2214 | |
2215 Dsymbol *FuncLiteralDeclaration::syntaxCopy(Dsymbol *s) | |
2216 { | |
2217 FuncLiteralDeclaration *f; | |
2218 | |
2219 //printf("FuncLiteralDeclaration::syntaxCopy('%s')\n", toChars()); | |
2220 if (s) | |
2221 f = (FuncLiteralDeclaration *)s; | |
2222 else | |
2223 f = new FuncLiteralDeclaration(loc, endloc, type->syntaxCopy(), tok, fes); | |
2224 FuncDeclaration::syntaxCopy(f); | |
2225 return f; | |
2226 } | |
2227 | |
2228 int FuncLiteralDeclaration::isNested() | |
2229 { | |
2230 //printf("FuncLiteralDeclaration::isNested() '%s'\n", toChars()); | |
2231 return (tok == TOKdelegate); | |
2232 } | |
2233 | |
336 | 2234 int FuncLiteralDeclaration::isVirtual() |
2235 { | |
2236 return FALSE; | |
2237 } | |
2238 | |
2239 const char *FuncLiteralDeclaration::kind() | |
159 | 2240 { |
2241 // GCC requires the (char*) casts | |
2242 return (tok == TOKdelegate) ? (char*)"delegate" : (char*)"function"; | |
2243 } | |
2244 | |
2245 void FuncLiteralDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2246 { | |
2247 static Identifier *idfunc; | |
2248 static Identifier *iddel; | |
2249 | |
2250 if (!idfunc) | |
2251 idfunc = new Identifier("function", 0); | |
2252 if (!iddel) | |
2253 iddel = new Identifier("delegate", 0); | |
2254 | |
2255 type->toCBuffer(buf, ((tok == TOKdelegate) ? iddel : idfunc), hgs); | |
2256 bodyToCBuffer(buf, hgs); | |
2257 } | |
2258 | |
2259 | |
2260 /********************************* CtorDeclaration ****************************/ | |
2261 | |
2262 CtorDeclaration::CtorDeclaration(Loc loc, Loc endloc, Arguments *arguments, int varargs) | |
2263 : FuncDeclaration(loc, endloc, Id::ctor, STCundefined, NULL) | |
2264 { | |
2265 this->arguments = arguments; | |
2266 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
|
2267 //printf("CtorDeclaration(loc = %s) %s\n", loc.toChars(), toChars()); |
159 | 2268 } |
2269 | |
2270 Dsymbol *CtorDeclaration::syntaxCopy(Dsymbol *s) | |
2271 { | |
2272 CtorDeclaration *f; | |
2273 | |
2274 f = new CtorDeclaration(loc, endloc, NULL, varargs); | |
2275 | |
2276 f->outId = outId; | |
2277 f->frequire = frequire ? frequire->syntaxCopy() : NULL; | |
2278 f->fensure = fensure ? fensure->syntaxCopy() : NULL; | |
2279 f->fbody = fbody ? fbody->syntaxCopy() : NULL; | |
2280 assert(!fthrows); // deprecated | |
2281 | |
2282 f->arguments = Argument::arraySyntaxCopy(arguments); | |
2283 return f; | |
2284 } | |
2285 | |
2286 | |
2287 void CtorDeclaration::semantic(Scope *sc) | |
2288 { | |
2289 ClassDeclaration *cd; | |
2290 Type *tret; | |
2291 | |
2292 //printf("CtorDeclaration::semantic()\n"); | |
2293 if (type) | |
2294 return; | |
2295 | |
2296 sc = sc->push(); | |
2297 sc->stc &= ~STCstatic; // not a static constructor | |
2298 | |
2299 parent = sc->parent; | |
2300 Dsymbol *parent = toParent(); | |
2301 cd = parent->isClassDeclaration(); | |
2302 if (!cd) | |
2303 { | |
2304 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
|
2305 fatal(); |
159 | 2306 tret = Type::tvoid; |
2307 } | |
2308 else | |
2309 tret = cd->type; //->referenceTo(); | |
2310 type = new TypeFunction(arguments, tret, varargs, LINKd); | |
336 | 2311 if (!originalType) |
2312 originalType = type; | |
159 | 2313 |
2314 sc->flags |= SCOPEctor; | |
2315 type = type->semantic(loc, sc); | |
2316 sc->flags &= ~SCOPEctor; | |
2317 | |
2318 // Append: | |
2319 // return this; | |
2320 // to the function body | |
2321 if (fbody) | |
2322 { Expression *e; | |
2323 Statement *s; | |
2324 | |
2325 e = new ThisExp(0); | |
2326 s = new ReturnStatement(0, e); | |
2327 fbody = new CompoundStatement(0, fbody, s); | |
2328 } | |
2329 | |
2330 FuncDeclaration::semantic(sc); | |
2331 | |
2332 sc->pop(); | |
2333 | |
2334 // See if it's the default constructor | |
2335 if (cd && varargs == 0 && Argument::dim(arguments) == 0) | |
2336 cd->defaultCtor = this; | |
2337 } | |
2338 | |
336 | 2339 const char *CtorDeclaration::kind() |
159 | 2340 { |
2341 return "constructor"; | |
2342 } | |
2343 | |
2344 char *CtorDeclaration::toChars() | |
2345 { | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
957
diff
changeset
|
2346 return (char *)"this"; |
159 | 2347 } |
2348 | |
2349 int CtorDeclaration::isVirtual() | |
2350 { | |
2351 return FALSE; | |
2352 } | |
2353 | |
2354 int CtorDeclaration::addPreInvariant() | |
2355 { | |
2356 return FALSE; | |
2357 } | |
2358 | |
2359 int CtorDeclaration::addPostInvariant() | |
2360 { | |
2361 return (vthis && global.params.useInvariants); | |
2362 } | |
2363 | |
2364 | |
2365 void CtorDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2366 { | |
2367 buf->writestring("this"); | |
2368 Argument::argsToCBuffer(buf, hgs, arguments, varargs); | |
2369 bodyToCBuffer(buf, hgs); | |
2370 } | |
2371 | |
2372 /********************************* DtorDeclaration ****************************/ | |
2373 | |
2374 DtorDeclaration::DtorDeclaration(Loc loc, Loc endloc) | |
2375 : FuncDeclaration(loc, endloc, Id::dtor, STCundefined, NULL) | |
2376 { | |
2377 } | |
2378 | |
2379 DtorDeclaration::DtorDeclaration(Loc loc, Loc endloc, Identifier *id) | |
2380 : FuncDeclaration(loc, endloc, id, STCundefined, NULL) | |
2381 { | |
2382 } | |
2383 | |
2384 Dsymbol *DtorDeclaration::syntaxCopy(Dsymbol *s) | |
2385 { | |
2386 assert(!s); | |
2387 DtorDeclaration *dd = new DtorDeclaration(loc, endloc, ident); | |
2388 return FuncDeclaration::syntaxCopy(dd); | |
2389 } | |
2390 | |
2391 | |
2392 void DtorDeclaration::semantic(Scope *sc) | |
2393 { | |
2394 ClassDeclaration *cd; | |
2395 | |
2396 parent = sc->parent; | |
2397 Dsymbol *parent = toParent(); | |
2398 cd = parent->isClassDeclaration(); | |
2399 if (!cd) | |
2400 { | |
2401 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
|
2402 fatal(); |
159 | 2403 } |
2404 else | |
2405 cd->dtors.push(this); | |
2406 type = new TypeFunction(NULL, Type::tvoid, FALSE, LINKd); | |
2407 | |
2408 sc = sc->push(); | |
2409 sc->stc &= ~STCstatic; // not a static destructor | |
2410 sc->linkage = LINKd; | |
2411 | |
2412 FuncDeclaration::semantic(sc); | |
2413 | |
2414 sc->pop(); | |
2415 } | |
2416 | |
2417 int DtorDeclaration::overloadInsert(Dsymbol *s) | |
2418 { | |
2419 return FALSE; // cannot overload destructors | |
2420 } | |
2421 | |
2422 int DtorDeclaration::addPreInvariant() | |
2423 { | |
2424 return (vthis && global.params.useInvariants); | |
2425 } | |
2426 | |
2427 int DtorDeclaration::addPostInvariant() | |
2428 { | |
2429 return FALSE; | |
2430 } | |
2431 | |
2432 int DtorDeclaration::isVirtual() | |
2433 { | |
2434 /* This should be FALSE so that dtor's don't get put into the vtbl[], | |
2435 * but doing so will require recompiling everything. | |
2436 */ | |
2437 #if BREAKABI | |
2438 return FALSE; | |
2439 #else | |
2440 return FuncDeclaration::isVirtual(); | |
2441 #endif | |
2442 } | |
2443 | |
2444 void DtorDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2445 { | |
2446 if (hgs->hdrgen) | |
2447 return; | |
2448 buf->writestring("~this()"); | |
2449 bodyToCBuffer(buf, hgs); | |
2450 } | |
2451 | |
2452 /********************************* StaticCtorDeclaration ****************************/ | |
2453 | |
2454 StaticCtorDeclaration::StaticCtorDeclaration(Loc loc, Loc endloc) | |
2455 : FuncDeclaration(loc, endloc, | |
957
31bbc7f3b817
Backed out changeset [369996c08420].
Christian Kamm <kamm incasoftware de>
parents:
956
diff
changeset
|
2456 Identifier::generateId("_staticCtor"), STCstatic, NULL) |
159 | 2457 { |
2458 } | |
2459 | |
2460 Dsymbol *StaticCtorDeclaration::syntaxCopy(Dsymbol *s) | |
2461 { | |
2462 StaticCtorDeclaration *scd; | |
2463 | |
2464 assert(!s); | |
2465 scd = new StaticCtorDeclaration(loc, endloc); | |
2466 return FuncDeclaration::syntaxCopy(scd); | |
2467 } | |
2468 | |
2469 | |
2470 void StaticCtorDeclaration::semantic(Scope *sc) | |
2471 { | |
2472 //printf("StaticCtorDeclaration::semantic()\n"); | |
2473 | |
2474 type = new TypeFunction(NULL, Type::tvoid, FALSE, LINKd); | |
2475 | |
336 | 2476 /* If the static ctor appears within a template instantiation, |
2477 * it could get called multiple times by the module constructors | |
2478 * for different modules. Thus, protect it with a gate. | |
2479 */ | |
2480 if (inTemplateInstance()) | |
2481 { | |
2482 /* Add this prefix to the function: | |
2483 * static int gate; | |
2484 * if (++gate != 1) return; | |
2485 * Note that this is not thread safe; should not have threads | |
2486 * during static construction. | |
2487 */ | |
2488 Identifier *id = Lexer::idPool("__gate"); | |
2489 VarDeclaration *v = new VarDeclaration(0, Type::tint32, id, NULL); | |
2490 v->storage_class = STCstatic; | |
2491 Statements *sa = new Statements(); | |
2492 Statement *s = new DeclarationStatement(0, v); | |
2493 sa->push(s); | |
2494 Expression *e = new IdentifierExp(0, id); | |
2495 e = new AddAssignExp(0, e, new IntegerExp(1)); | |
2496 e = new EqualExp(TOKnotequal, 0, e, new IntegerExp(1)); | |
2497 s = new IfStatement(0, NULL, e, new ReturnStatement(0, NULL), NULL); | |
2498 sa->push(s); | |
2499 if (fbody) | |
2500 sa->push(fbody); | |
2501 fbody = new CompoundStatement(0, sa); | |
2502 } | |
2503 | |
159 | 2504 FuncDeclaration::semantic(sc); |
2505 | |
2506 // We're going to need ModuleInfo | |
2507 Module *m = getModule(); | |
2508 if (!m) | |
2509 m = sc->module; | |
2510 if (m) | |
2511 { m->needmoduleinfo = 1; | |
2512 #ifdef IN_GCC | |
2513 m->strictlyneedmoduleinfo = 1; | |
2514 #endif | |
2515 } | |
2516 } | |
2517 | |
2518 AggregateDeclaration *StaticCtorDeclaration::isThis() | |
2519 { | |
2520 return NULL; | |
2521 } | |
2522 | |
2523 int StaticCtorDeclaration::isStaticConstructor() | |
2524 { | |
2525 return TRUE; | |
2526 } | |
2527 | |
2528 int StaticCtorDeclaration::isVirtual() | |
2529 { | |
2530 return FALSE; | |
2531 } | |
2532 | |
2533 int StaticCtorDeclaration::addPreInvariant() | |
2534 { | |
2535 return FALSE; | |
2536 } | |
2537 | |
2538 int StaticCtorDeclaration::addPostInvariant() | |
2539 { | |
2540 return FALSE; | |
2541 } | |
2542 | |
2543 void StaticCtorDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2544 { | |
2545 if (hgs->hdrgen) | |
2546 { buf->writestring("static this();\n"); | |
2547 return; | |
2548 } | |
2549 buf->writestring("static this()"); | |
2550 bodyToCBuffer(buf, hgs); | |
2551 } | |
2552 | |
2553 /********************************* StaticDtorDeclaration ****************************/ | |
2554 | |
2555 StaticDtorDeclaration::StaticDtorDeclaration(Loc loc, Loc endloc) | |
2556 : FuncDeclaration(loc, endloc, | |
957
31bbc7f3b817
Backed out changeset [369996c08420].
Christian Kamm <kamm incasoftware de>
parents:
956
diff
changeset
|
2557 Identifier::generateId("_staticDtor"), STCstatic, NULL) |
159 | 2558 { |
336 | 2559 vgate = NULL; |
159 | 2560 } |
2561 | |
2562 Dsymbol *StaticDtorDeclaration::syntaxCopy(Dsymbol *s) | |
2563 { | |
2564 StaticDtorDeclaration *sdd; | |
2565 | |
2566 assert(!s); | |
2567 sdd = new StaticDtorDeclaration(loc, endloc); | |
2568 return FuncDeclaration::syntaxCopy(sdd); | |
2569 } | |
2570 | |
2571 | |
2572 void StaticDtorDeclaration::semantic(Scope *sc) | |
2573 { | |
2574 ClassDeclaration *cd; | |
2575 Type *tret; | |
2576 | |
2577 cd = sc->scopesym->isClassDeclaration(); | |
2578 if (!cd) | |
2579 { | |
2580 } | |
2581 type = new TypeFunction(NULL, Type::tvoid, FALSE, LINKd); | |
2582 | |
336 | 2583 /* If the static ctor appears within a template instantiation, |
2584 * it could get called multiple times by the module constructors | |
2585 * for different modules. Thus, protect it with a gate. | |
2586 */ | |
2587 if (inTemplateInstance()) | |
2588 { | |
2589 /* Add this prefix to the function: | |
2590 * static int gate; | |
2591 * if (--gate != 0) return; | |
2592 * Increment gate during constructor execution. | |
2593 * Note that this is not thread safe; should not have threads | |
2594 * during static destruction. | |
2595 */ | |
2596 Identifier *id = Lexer::idPool("__gate"); | |
2597 VarDeclaration *v = new VarDeclaration(0, Type::tint32, id, NULL); | |
2598 v->storage_class = STCstatic; | |
2599 Statements *sa = new Statements(); | |
2600 Statement *s = new DeclarationStatement(0, v); | |
2601 sa->push(s); | |
2602 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
|
2603 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
|
2604 e = new EqualExp(TOKnotequal, 0, e, new IntegerExp(0)); |
336 | 2605 s = new IfStatement(0, NULL, e, new ReturnStatement(0, NULL), NULL); |
2606 sa->push(s); | |
2607 if (fbody) | |
2608 sa->push(fbody); | |
2609 fbody = new CompoundStatement(0, sa); | |
2610 vgate = v; | |
2611 } | |
2612 | |
159 | 2613 FuncDeclaration::semantic(sc); |
2614 | |
2615 // We're going to need ModuleInfo | |
2616 Module *m = getModule(); | |
2617 if (!m) | |
2618 m = sc->module; | |
2619 if (m) | |
2620 { m->needmoduleinfo = 1; | |
2621 #ifdef IN_GCC | |
2622 m->strictlyneedmoduleinfo = 1; | |
2623 #endif | |
2624 } | |
2625 } | |
2626 | |
2627 AggregateDeclaration *StaticDtorDeclaration::isThis() | |
2628 { | |
2629 return NULL; | |
2630 } | |
2631 | |
2632 int StaticDtorDeclaration::isStaticDestructor() | |
2633 { | |
2634 return TRUE; | |
2635 } | |
2636 | |
2637 int StaticDtorDeclaration::isVirtual() | |
2638 { | |
2639 return FALSE; | |
2640 } | |
2641 | |
2642 int StaticDtorDeclaration::addPreInvariant() | |
2643 { | |
2644 return FALSE; | |
2645 } | |
2646 | |
2647 int StaticDtorDeclaration::addPostInvariant() | |
2648 { | |
2649 return FALSE; | |
2650 } | |
2651 | |
2652 void StaticDtorDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2653 { | |
2654 if (hgs->hdrgen) | |
2655 return; | |
2656 buf->writestring("static ~this()"); | |
2657 bodyToCBuffer(buf, hgs); | |
2658 } | |
2659 | |
2660 /********************************* InvariantDeclaration ****************************/ | |
2661 | |
2662 InvariantDeclaration::InvariantDeclaration(Loc loc, Loc endloc) | |
2663 : FuncDeclaration(loc, endloc, Id::classInvariant, STCundefined, NULL) | |
2664 { | |
2665 } | |
2666 | |
2667 Dsymbol *InvariantDeclaration::syntaxCopy(Dsymbol *s) | |
2668 { | |
2669 InvariantDeclaration *id; | |
2670 | |
2671 assert(!s); | |
2672 id = new InvariantDeclaration(loc, endloc); | |
2673 FuncDeclaration::syntaxCopy(id); | |
2674 return id; | |
2675 } | |
2676 | |
2677 | |
2678 void InvariantDeclaration::semantic(Scope *sc) | |
2679 { | |
2680 AggregateDeclaration *ad; | |
2681 Type *tret; | |
2682 | |
2683 parent = sc->parent; | |
2684 Dsymbol *parent = toParent(); | |
2685 ad = parent->isAggregateDeclaration(); | |
2686 if (!ad) | |
2687 { | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
957
diff
changeset
|
2688 error("invariants are only for struct/union/class definitions"); |
159 | 2689 return; |
2690 } | |
2691 else if (ad->inv && ad->inv != this) | |
2692 { | |
2693 error("more than one invariant for %s", ad->toChars()); | |
2694 } | |
2695 ad->inv = this; | |
2696 type = new TypeFunction(NULL, Type::tvoid, FALSE, LINKd); | |
2697 | |
2698 sc = sc->push(); | |
2699 sc->stc &= ~STCstatic; // not a static invariant | |
2700 sc->incontract++; | |
2701 sc->linkage = LINKd; | |
2702 | |
2703 FuncDeclaration::semantic(sc); | |
2704 | |
2705 sc->pop(); | |
2706 } | |
2707 | |
2708 int InvariantDeclaration::isVirtual() | |
2709 { | |
2710 return FALSE; | |
2711 } | |
2712 | |
2713 int InvariantDeclaration::addPreInvariant() | |
2714 { | |
2715 return FALSE; | |
2716 } | |
2717 | |
2718 int InvariantDeclaration::addPostInvariant() | |
2719 { | |
2720 return FALSE; | |
2721 } | |
2722 | |
2723 void InvariantDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2724 { | |
2725 if (hgs->hdrgen) | |
2726 return; | |
2727 buf->writestring("invariant"); | |
2728 bodyToCBuffer(buf, hgs); | |
2729 } | |
2730 | |
2731 | |
2732 /********************************* UnitTestDeclaration ****************************/ | |
2733 | |
2734 /******************************* | |
2735 * Generate unique unittest function Id so we can have multiple | |
2736 * instances per module. | |
2737 */ | |
2738 | |
2739 static Identifier *unitTestId() | |
2740 { | |
336 | 2741 return Lexer::uniqueId("__unittest"); |
159 | 2742 } |
2743 | |
2744 UnitTestDeclaration::UnitTestDeclaration(Loc loc, Loc endloc) | |
2745 : FuncDeclaration(loc, endloc, unitTestId(), STCundefined, NULL) | |
2746 { | |
2747 } | |
2748 | |
2749 Dsymbol *UnitTestDeclaration::syntaxCopy(Dsymbol *s) | |
2750 { | |
2751 UnitTestDeclaration *utd; | |
2752 | |
2753 assert(!s); | |
2754 utd = new UnitTestDeclaration(loc, endloc); | |
2755 return FuncDeclaration::syntaxCopy(utd); | |
2756 } | |
2757 | |
2758 | |
2759 void UnitTestDeclaration::semantic(Scope *sc) | |
2760 { | |
2761 if (global.params.useUnitTests) | |
2762 { | |
2763 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
|
2764 Scope *sc2 = sc->push(); |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
717
diff
changeset
|
2765 sc2->linkage = LINKd; |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
717
diff
changeset
|
2766 FuncDeclaration::semantic(sc2); |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
717
diff
changeset
|
2767 sc2->pop(); |
159 | 2768 } |
2769 | |
2770 // We're going to need ModuleInfo even if the unit tests are not | |
2771 // compiled in, because other modules may import this module and refer | |
2772 // to this ModuleInfo. | |
2773 Module *m = getModule(); | |
2774 if (!m) | |
2775 m = sc->module; | |
2776 if (m) | |
2777 m->needmoduleinfo = 1; | |
2778 } | |
2779 | |
2780 AggregateDeclaration *UnitTestDeclaration::isThis() | |
2781 { | |
2782 return NULL; | |
2783 } | |
2784 | |
2785 int UnitTestDeclaration::isVirtual() | |
2786 { | |
2787 return FALSE; | |
2788 } | |
2789 | |
2790 int UnitTestDeclaration::addPreInvariant() | |
2791 { | |
2792 return FALSE; | |
2793 } | |
2794 | |
2795 int UnitTestDeclaration::addPostInvariant() | |
2796 { | |
2797 return FALSE; | |
2798 } | |
2799 | |
2800 void UnitTestDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2801 { | |
2802 if (hgs->hdrgen) | |
2803 return; | |
2804 buf->writestring("unittest"); | |
2805 bodyToCBuffer(buf, hgs); | |
2806 } | |
2807 | |
2808 /********************************* NewDeclaration ****************************/ | |
2809 | |
2810 NewDeclaration::NewDeclaration(Loc loc, Loc endloc, Arguments *arguments, int varargs) | |
2811 : FuncDeclaration(loc, endloc, Id::classNew, STCstatic, NULL) | |
2812 { | |
2813 this->arguments = arguments; | |
2814 this->varargs = varargs; | |
2815 } | |
2816 | |
2817 Dsymbol *NewDeclaration::syntaxCopy(Dsymbol *s) | |
2818 { | |
2819 NewDeclaration *f; | |
2820 | |
2821 f = new NewDeclaration(loc, endloc, NULL, varargs); | |
2822 | |
2823 FuncDeclaration::syntaxCopy(f); | |
2824 | |
2825 f->arguments = Argument::arraySyntaxCopy(arguments); | |
2826 | |
2827 return f; | |
2828 } | |
2829 | |
2830 | |
2831 void NewDeclaration::semantic(Scope *sc) | |
2832 { | |
2833 ClassDeclaration *cd; | |
2834 Type *tret; | |
2835 | |
2836 //printf("NewDeclaration::semantic()\n"); | |
2837 | |
2838 parent = sc->parent; | |
2839 Dsymbol *parent = toParent(); | |
2840 cd = parent->isClassDeclaration(); | |
2841 if (!cd && !parent->isStructDeclaration()) | |
2842 { | |
2843 error("new allocators only are for class or struct definitions"); | |
2844 } | |
2845 tret = Type::tvoid->pointerTo(); | |
2846 type = new TypeFunction(arguments, tret, varargs, LINKd); | |
2847 | |
2848 type = type->semantic(loc, sc); | |
2849 assert(type->ty == Tfunction); | |
2850 | |
2851 // Check that there is at least one argument of type uint | |
2852 TypeFunction *tf = (TypeFunction *)type; | |
2853 if (Argument::dim(tf->parameters) < 1) | |
2854 { | |
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
|
2855 error("at least one argument of type size_t expected"); |
159 | 2856 } |
2857 else | |
2858 { | |
2859 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
|
2860 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
|
2861 error("first argument must be type size_t, not %s", a->type->toChars()); |
159 | 2862 } |
2863 | |
2864 FuncDeclaration::semantic(sc); | |
2865 } | |
2866 | |
336 | 2867 const char *NewDeclaration::kind() |
159 | 2868 { |
2869 return "allocator"; | |
2870 } | |
2871 | |
2872 int NewDeclaration::isVirtual() | |
2873 { | |
2874 return FALSE; | |
2875 } | |
2876 | |
2877 int NewDeclaration::addPreInvariant() | |
2878 { | |
2879 return FALSE; | |
2880 } | |
2881 | |
2882 int NewDeclaration::addPostInvariant() | |
2883 { | |
2884 return FALSE; | |
2885 } | |
2886 | |
2887 void NewDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2888 { | |
2889 buf->writestring("new"); | |
2890 Argument::argsToCBuffer(buf, hgs, arguments, varargs); | |
2891 bodyToCBuffer(buf, hgs); | |
2892 } | |
2893 | |
2894 | |
2895 /********************************* DeleteDeclaration ****************************/ | |
2896 | |
2897 DeleteDeclaration::DeleteDeclaration(Loc loc, Loc endloc, Arguments *arguments) | |
2898 : FuncDeclaration(loc, endloc, Id::classDelete, STCstatic, NULL) | |
2899 { | |
2900 this->arguments = arguments; | |
2901 } | |
2902 | |
2903 Dsymbol *DeleteDeclaration::syntaxCopy(Dsymbol *s) | |
2904 { | |
2905 DeleteDeclaration *f; | |
2906 | |
2907 f = new DeleteDeclaration(loc, endloc, NULL); | |
2908 | |
2909 FuncDeclaration::syntaxCopy(f); | |
2910 | |
2911 f->arguments = Argument::arraySyntaxCopy(arguments); | |
2912 | |
2913 return f; | |
2914 } | |
2915 | |
2916 | |
2917 void DeleteDeclaration::semantic(Scope *sc) | |
2918 { | |
2919 ClassDeclaration *cd; | |
2920 | |
2921 //printf("DeleteDeclaration::semantic()\n"); | |
2922 | |
2923 parent = sc->parent; | |
2924 Dsymbol *parent = toParent(); | |
2925 cd = parent->isClassDeclaration(); | |
2926 if (!cd && !parent->isStructDeclaration()) | |
2927 { | |
2928 error("new allocators only are for class or struct definitions"); | |
2929 } | |
2930 type = new TypeFunction(arguments, Type::tvoid, 0, LINKd); | |
2931 | |
2932 type = type->semantic(loc, sc); | |
2933 assert(type->ty == Tfunction); | |
2934 | |
2935 // Check that there is only one argument of type void* | |
2936 TypeFunction *tf = (TypeFunction *)type; | |
2937 if (Argument::dim(tf->parameters) != 1) | |
2938 { | |
2939 error("one argument of type void* expected"); | |
2940 } | |
2941 else | |
2942 { | |
2943 Argument *a = Argument::getNth(tf->parameters, 0); | |
2944 if (!a->type->equals(Type::tvoid->pointerTo())) | |
2945 error("one argument of type void* expected, not %s", a->type->toChars()); | |
2946 } | |
2947 | |
2948 FuncDeclaration::semantic(sc); | |
2949 } | |
2950 | |
336 | 2951 const char *DeleteDeclaration::kind() |
159 | 2952 { |
2953 return "deallocator"; | |
2954 } | |
2955 | |
2956 int DeleteDeclaration::isDelete() | |
2957 { | |
2958 return TRUE; | |
2959 } | |
2960 | |
2961 int DeleteDeclaration::isVirtual() | |
2962 { | |
2963 return FALSE; | |
2964 } | |
2965 | |
2966 int DeleteDeclaration::addPreInvariant() | |
2967 { | |
2968 return FALSE; | |
2969 } | |
2970 | |
2971 int DeleteDeclaration::addPostInvariant() | |
2972 { | |
2973 return FALSE; | |
2974 } | |
2975 | |
2976 void DeleteDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2977 { | |
2978 buf->writestring("delete"); | |
2979 Argument::argsToCBuffer(buf, hgs, arguments, 0); | |
2980 bodyToCBuffer(buf, hgs); | |
2981 } | |
2982 | |
2983 | |
2984 | |
2985 |