Mercurial > projects > ldc
annotate dmd/func.c @ 1183:df386fd4030e
Commit requested by lindquist.
author | Frits van Bommel <fvbommel wxs.nl> |
---|---|
date | Tue, 31 Mar 2009 03:06:19 +0200 |
parents | a8b9fc41c34b |
children | e961851fb8be |
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 | |
1177
a8b9fc41c34b
Fix #246 by running type->semantic() on parameters so tuples are expanded.
Frits van Bommel <fvbommel wxs.nl>
parents:
1158
diff
changeset
|
772 #if IN_LLVM |
a8b9fc41c34b
Fix #246 by running type->semantic() on parameters so tuples are expanded.
Frits van Bommel <fvbommel wxs.nl>
parents:
1158
diff
changeset
|
773 // LDC make sure argument type is semanticed. |
a8b9fc41c34b
Fix #246 by running type->semantic() on parameters so tuples are expanded.
Frits van Bommel <fvbommel wxs.nl>
parents:
1158
diff
changeset
|
774 // Turns TypeTuple!(int, int) into two int parameters, for instance. |
a8b9fc41c34b
Fix #246 by running type->semantic() on parameters so tuples are expanded.
Frits van Bommel <fvbommel wxs.nl>
parents:
1158
diff
changeset
|
775 if (f->parameters) |
a8b9fc41c34b
Fix #246 by running type->semantic() on parameters so tuples are expanded.
Frits van Bommel <fvbommel wxs.nl>
parents:
1158
diff
changeset
|
776 { |
a8b9fc41c34b
Fix #246 by running type->semantic() on parameters so tuples are expanded.
Frits van Bommel <fvbommel wxs.nl>
parents:
1158
diff
changeset
|
777 for (size_t i = 0; i < Argument::dim(f->parameters); i++) |
a8b9fc41c34b
Fix #246 by running type->semantic() on parameters so tuples are expanded.
Frits van Bommel <fvbommel wxs.nl>
parents:
1158
diff
changeset
|
778 { Argument *arg = (Argument *)Argument::getNth(f->parameters, i); |
a8b9fc41c34b
Fix #246 by running type->semantic() on parameters so tuples are expanded.
Frits van Bommel <fvbommel wxs.nl>
parents:
1158
diff
changeset
|
779 Type* nw = arg->type->semantic(0, sc); |
a8b9fc41c34b
Fix #246 by running type->semantic() on parameters so tuples are expanded.
Frits van Bommel <fvbommel wxs.nl>
parents:
1158
diff
changeset
|
780 if (arg->type != nw) { |
a8b9fc41c34b
Fix #246 by running type->semantic() on parameters so tuples are expanded.
Frits van Bommel <fvbommel wxs.nl>
parents:
1158
diff
changeset
|
781 arg->type = nw; |
a8b9fc41c34b
Fix #246 by running type->semantic() on parameters so tuples are expanded.
Frits van Bommel <fvbommel wxs.nl>
parents:
1158
diff
changeset
|
782 // Examine this index again. |
a8b9fc41c34b
Fix #246 by running type->semantic() on parameters so tuples are expanded.
Frits van Bommel <fvbommel wxs.nl>
parents:
1158
diff
changeset
|
783 // This is important if it turned into a tuple. |
a8b9fc41c34b
Fix #246 by running type->semantic() on parameters so tuples are expanded.
Frits van Bommel <fvbommel wxs.nl>
parents:
1158
diff
changeset
|
784 // In particular, the empty tuple should be handled or the |
a8b9fc41c34b
Fix #246 by running type->semantic() on parameters so tuples are expanded.
Frits van Bommel <fvbommel wxs.nl>
parents:
1158
diff
changeset
|
785 // next parameter will be skipped. |
a8b9fc41c34b
Fix #246 by running type->semantic() on parameters so tuples are expanded.
Frits van Bommel <fvbommel wxs.nl>
parents:
1158
diff
changeset
|
786 // FIXME: Maybe we only need to do this for tuples, |
a8b9fc41c34b
Fix #246 by running type->semantic() on parameters so tuples are expanded.
Frits van Bommel <fvbommel wxs.nl>
parents:
1158
diff
changeset
|
787 // and can add tuple.length after decrement? |
a8b9fc41c34b
Fix #246 by running type->semantic() on parameters so tuples are expanded.
Frits van Bommel <fvbommel wxs.nl>
parents:
1158
diff
changeset
|
788 i--; |
a8b9fc41c34b
Fix #246 by running type->semantic() on parameters so tuples are expanded.
Frits van Bommel <fvbommel wxs.nl>
parents:
1158
diff
changeset
|
789 } |
a8b9fc41c34b
Fix #246 by running type->semantic() on parameters so tuples are expanded.
Frits van Bommel <fvbommel wxs.nl>
parents:
1158
diff
changeset
|
790 } |
a8b9fc41c34b
Fix #246 by running type->semantic() on parameters so tuples are expanded.
Frits van Bommel <fvbommel wxs.nl>
parents:
1158
diff
changeset
|
791 // update nparams to include expanded tuples |
a8b9fc41c34b
Fix #246 by running type->semantic() on parameters so tuples are expanded.
Frits van Bommel <fvbommel wxs.nl>
parents:
1158
diff
changeset
|
792 nparams = Argument::dim(f->parameters); |
a8b9fc41c34b
Fix #246 by running type->semantic() on parameters so tuples are expanded.
Frits van Bommel <fvbommel wxs.nl>
parents:
1158
diff
changeset
|
793 } |
a8b9fc41c34b
Fix #246 by running type->semantic() on parameters so tuples are expanded.
Frits van Bommel <fvbommel wxs.nl>
parents:
1158
diff
changeset
|
794 #endif |
a8b9fc41c34b
Fix #246 by running type->semantic() on parameters so tuples are expanded.
Frits van Bommel <fvbommel wxs.nl>
parents:
1158
diff
changeset
|
795 |
159 | 796 // Propagate storage class from tuple parameters to their element-parameters. |
797 if (f->parameters) | |
798 { | |
799 for (size_t i = 0; i < f->parameters->dim; i++) | |
800 { Argument *arg = (Argument *)f->parameters->data[i]; | |
801 | |
802 if (arg->type->ty == Ttuple) | |
803 { TypeTuple *t = (TypeTuple *)arg->type; | |
804 size_t dim = Argument::dim(t->arguments); | |
805 for (size_t j = 0; j < dim; j++) | |
806 { Argument *narg = Argument::getNth(t->arguments, j); | |
807 narg->storageClass = arg->storageClass; | |
808 } | |
809 } | |
810 } | |
811 } | |
812 | |
813 // Declare all the function parameters as variables | |
814 if (nparams) | |
815 { /* parameters[] has all the tuples removed, as the back end | |
816 * doesn't know about tuples | |
817 */ | |
818 parameters = new Dsymbols(); | |
819 parameters->reserve(nparams); | |
820 for (size_t i = 0; i < nparams; i++) | |
821 { | |
822 Argument *arg = Argument::getNth(f->parameters, i); | |
823 Identifier *id = arg->ident; | |
824 if (!id) | |
825 { | |
826 /* Generate identifier for un-named parameter, | |
827 * because we need it later on. | |
828 */ | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
468
diff
changeset
|
829 arg->ident = id = Identifier::generateId("_param_", i); |
159 | 830 } |
831 VarDeclaration *v = new VarDeclaration(loc, arg->type, id, NULL); | |
832 //printf("declaring parameter %s of type %s\n", v->toChars(), v->type->toChars()); | |
833 v->storage_class |= STCparameter; | |
834 if (f->varargs == 2 && i + 1 == nparams) | |
835 v->storage_class |= STCvariadic; | |
836 v->storage_class |= arg->storageClass & (STCin | STCout | STCref | STClazy); | |
837 if (v->storage_class & STClazy) | |
838 v->storage_class |= STCin; | |
839 v->semantic(sc2); | |
840 if (!sc2->insert(v)) | |
841 error("parameter %s.%s is already defined", toChars(), v->toChars()); | |
842 else | |
843 parameters->push(v); | |
844 localsymtab->insert(v); | |
845 v->parent = this; | |
846 } | |
847 } | |
848 | |
849 // Declare the tuple symbols and put them in the symbol table, | |
850 // but not in parameters[]. | |
851 if (f->parameters) | |
852 { | |
853 for (size_t i = 0; i < f->parameters->dim; i++) | |
854 { Argument *arg = (Argument *)f->parameters->data[i]; | |
855 | |
856 if (!arg->ident) | |
857 continue; // never used, so ignore | |
858 if (arg->type->ty == Ttuple) | |
859 { TypeTuple *t = (TypeTuple *)arg->type; | |
860 size_t dim = Argument::dim(t->arguments); | |
861 Objects *exps = new Objects(); | |
862 exps->setDim(dim); | |
863 for (size_t j = 0; j < dim; j++) | |
864 { Argument *narg = Argument::getNth(t->arguments, j); | |
865 assert(narg->ident); | |
866 VarDeclaration *v = sc2->search(0, narg->ident, NULL)->isVarDeclaration(); | |
867 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
|
868 Expression *e = new VarExp(v->loc, v); |
159 | 869 exps->data[j] = (void *)e; |
870 } | |
871 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
|
872 TupleDeclaration *v = new TupleDeclaration(loc, arg->ident, exps); |
159 | 873 //printf("declaring tuple %s\n", v->toChars()); |
874 v->isexp = 1; | |
875 if (!sc2->insert(v)) | |
876 error("parameter %s.%s is already defined", toChars(), v->toChars()); | |
877 localsymtab->insert(v); | |
878 v->parent = this; | |
879 } | |
880 } | |
881 } | |
882 | |
883 /* Do the semantic analysis on the [in] preconditions and | |
884 * [out] postconditions. | |
885 */ | |
886 sc2->incontract++; | |
887 | |
888 if (frequire) | |
889 { /* frequire is composed of the [in] contracts | |
890 */ | |
891 // BUG: need to error if accessing out parameters | |
892 // BUG: need to treat parameters as const | |
893 // BUG: need to disallow returns and throws | |
894 // BUG: verify that all in and ref parameters are read | |
895 frequire = frequire->semantic(sc2); | |
896 labtab = NULL; // so body can't refer to labels | |
897 } | |
898 | |
899 if (fensure || addPostInvariant()) | |
900 { /* fensure is composed of the [out] contracts | |
901 */ | |
902 ScopeDsymbol *sym = new ScopeDsymbol(); | |
903 sym->parent = sc2->scopesym; | |
904 sc2 = sc2->push(sym); | |
905 | |
906 assert(type->nextOf()); | |
907 if (type->nextOf()->ty == Tvoid) | |
908 { | |
909 if (outId) | |
910 error("void functions have no result"); | |
911 } | |
912 else | |
913 { | |
914 if (!outId) | |
915 outId = Id::result; // provide a default | |
916 } | |
917 | |
918 if (outId) | |
919 { // Declare result variable | |
920 VarDeclaration *v; | |
921 Loc loc = this->loc; | |
922 | |
923 if (fensure) | |
924 loc = fensure->loc; | |
925 | |
926 v = new VarDeclaration(loc, type->nextOf(), outId, NULL); | |
927 v->noauto = 1; | |
928 sc2->incontract--; | |
929 v->semantic(sc2); | |
930 sc2->incontract++; | |
931 if (!sc2->insert(v)) | |
932 error("out result %s is already defined", v->toChars()); | |
933 v->parent = this; | |
934 vresult = v; | |
935 | |
936 // vresult gets initialized with the function return value | |
937 // in ReturnStatement::semantic() | |
938 } | |
939 | |
940 // BUG: need to treat parameters as const | |
941 // BUG: need to disallow returns and throws | |
942 if (fensure) | |
943 { fensure = fensure->semantic(sc2); | |
944 labtab = NULL; // so body can't refer to labels | |
945 } | |
946 | |
947 if (!global.params.useOut) | |
948 { fensure = NULL; // discard | |
949 vresult = NULL; | |
950 } | |
951 | |
952 // Postcondition invariant | |
953 if (addPostInvariant()) | |
954 { | |
955 Expression *e = NULL; | |
956 if (isCtorDeclaration()) | |
957 { | |
958 // Call invariant directly only if it exists | |
959 InvariantDeclaration *inv = ad->inv; | |
960 ClassDeclaration *cd = ad->isClassDeclaration(); | |
961 | |
962 while (!inv && cd) | |
963 { | |
964 cd = cd->baseClass; | |
965 if (!cd) | |
966 break; | |
967 inv = cd->inv; | |
968 } | |
969 if (inv) | |
970 { | |
971 e = new DsymbolExp(0, inv); | |
972 e = new CallExp(0, e); | |
973 e = e->semantic(sc2); | |
974 } | |
975 } | |
976 else | |
977 { // Call invariant virtually | |
978 ThisExp *v = new ThisExp(0); | |
979 v->type = vthis->type; | |
980 e = new AssertExp(0, v); | |
981 } | |
982 if (e) | |
983 { | |
984 ExpStatement *s = new ExpStatement(0, e); | |
985 if (fensure) | |
986 fensure = new CompoundStatement(0, s, fensure); | |
987 else | |
988 fensure = s; | |
989 } | |
990 } | |
991 | |
992 if (fensure) | |
993 { returnLabel = new LabelDsymbol(Id::returnLabel); | |
994 LabelStatement *ls = new LabelStatement(0, Id::returnLabel, fensure); | |
995 ls->isReturnLabel = 1; | |
996 returnLabel->statement = ls; | |
997 } | |
998 sc2 = sc2->pop(); | |
999 } | |
1000 | |
1001 sc2->incontract--; | |
1002 | |
1003 if (fbody) | |
1004 { ClassDeclaration *cd = isClassMember(); | |
1005 | |
1006 /* If this is a class constructor | |
1007 */ | |
1008 if (isCtorDeclaration() && cd) | |
1009 { | |
1010 for (int i = 0; i < cd->fields.dim; i++) | |
1011 { VarDeclaration *v = (VarDeclaration *)cd->fields.data[i]; | |
1012 | |
1013 v->ctorinit = 0; | |
1014 } | |
1015 } | |
1016 | |
1017 if (inferRetType || f->retStyle() != RETstack) | |
1018 nrvo_can = 0; | |
1019 | |
1020 fbody = fbody->semantic(sc2); | |
1021 | |
1022 if (inferRetType) | |
1023 { // If no return type inferred yet, then infer a void | |
1024 if (!type->nextOf()) | |
1025 { | |
1026 ((TypeFunction *)type)->next = Type::tvoid; | |
1027 type = type->semantic(loc, sc); | |
1028 } | |
1029 f = (TypeFunction *)type; | |
1030 } | |
1031 | |
336 | 1032 int offend = fbody ? fbody->blockExit() & BEfallthru : TRUE; |
159 | 1033 |
1034 if (isStaticCtorDeclaration()) | |
1035 { /* It's a static constructor. Ensure that all | |
1036 * ctor consts were initialized. | |
1037 */ | |
1038 | |
1039 Dsymbol *p = toParent(); | |
1040 ScopeDsymbol *ad = p->isScopeDsymbol(); | |
1041 if (!ad) | |
1042 { | |
1043 error("static constructor can only be member of struct/class/module, not %s %s", p->kind(), p->toChars()); | |
1044 } | |
1045 else | |
1046 { | |
1047 for (int i = 0; i < ad->members->dim; i++) | |
1048 { Dsymbol *s = (Dsymbol *)ad->members->data[i]; | |
1049 | |
1050 s->checkCtorConstInit(); | |
1051 } | |
1052 } | |
1053 } | |
1054 | |
1055 if (isCtorDeclaration() && cd) | |
1056 { | |
1057 //printf("callSuper = x%x\n", sc2->callSuper); | |
1058 | |
1059 // Verify that all the ctorinit fields got initialized | |
1060 if (!(sc2->callSuper & CSXthis_ctor)) | |
1061 { | |
1062 for (int i = 0; i < cd->fields.dim; i++) | |
1063 { VarDeclaration *v = (VarDeclaration *)cd->fields.data[i]; | |
1064 | |
1065 if (v->ctorinit == 0 && v->isCtorinit()) | |
1066 error("missing initializer for const field %s", v->toChars()); | |
1067 } | |
1068 } | |
1069 | |
1070 if (!(sc2->callSuper & CSXany_ctor) && | |
1071 cd->baseClass && cd->baseClass->ctor) | |
1072 { | |
1073 sc2->callSuper = 0; | |
1074 | |
1075 // Insert implicit super() at start of fbody | |
1076 Expression *e1 = new SuperExp(0); | |
1077 Expression *e = new CallExp(0, e1); | |
1078 | |
1079 unsigned errors = global.errors; | |
1080 global.gag++; | |
1081 e = e->semantic(sc2); | |
1082 global.gag--; | |
1083 if (errors != global.errors) | |
1084 error("no match for implicit super() call in constructor"); | |
1085 | |
1086 Statement *s = new ExpStatement(0, e); | |
1087 fbody = new CompoundStatement(0, s, fbody); | |
1088 } | |
1089 } | |
1090 else if (fes) | |
1091 { // For foreach(){} body, append a return 0; | |
1092 Expression *e = new IntegerExp(0); | |
1093 Statement *s = new ReturnStatement(0, e); | |
1094 fbody = new CompoundStatement(0, fbody, s); | |
1095 assert(!returnLabel); | |
1096 } | |
1097 else if (!hasReturnExp && type->nextOf()->ty != Tvoid) | |
1098 error("expected to return a value of type %s", type->nextOf()->toChars()); | |
1099 else if (!inlineAsm) | |
1100 { | |
1101 if (type->nextOf()->ty == Tvoid) | |
1102 { | |
1103 if (offend && isMain()) | |
1104 { // Add a return 0; statement | |
1105 Statement *s = new ReturnStatement(0, new IntegerExp(0)); | |
1106 fbody = new CompoundStatement(0, fbody, s); | |
1107 } | |
1108 } | |
1109 else | |
1110 { | |
1111 if (offend) | |
1112 { Expression *e; | |
1113 | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
957
diff
changeset
|
1114 warning(loc, "no return at end of function"); |
159 | 1115 |
1116 if (global.params.useAssert && | |
1117 !global.params.useInline) | |
1118 { /* Add an assert(0, msg); where the missing return | |
1119 * should be. | |
1120 */ | |
1121 e = new AssertExp( | |
1122 endloc, | |
1123 new IntegerExp(0), | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
957
diff
changeset
|
1124 new StringExp(loc, (char *)"missing return expression") |
159 | 1125 ); |
1126 } | |
1127 else | |
1128 e = new HaltExp(endloc); | |
1129 e = new CommaExp(0, e, type->nextOf()->defaultInit()); | |
1130 e = e->semantic(sc2); | |
1131 Statement *s = new ExpStatement(0, e); | |
1132 fbody = new CompoundStatement(0, fbody, s); | |
1133 } | |
1134 } | |
1135 } | |
1136 } | |
1137 | |
1138 { | |
1139 Statements *a = new Statements(); | |
1140 | |
1141 // Merge in initialization of 'out' parameters | |
1142 if (parameters) | |
1143 { for (size_t i = 0; i < parameters->dim; i++) | |
1144 { | |
1145 VarDeclaration *v = (VarDeclaration *)parameters->data[i]; | |
1146 if (v->storage_class & STCout) | |
1147 { | |
1148 assert(v->init); | |
1149 ExpInitializer *ie = v->init->isExpInitializer(); | |
1150 assert(ie); | |
1151 a->push(new ExpStatement(0, ie->exp)); | |
1152 } | |
1153 } | |
1154 } | |
1155 | |
1156 // we'll handle variadics ourselves | |
1157 #if !IN_LLVM | |
1158 if (argptr) | |
1159 { // Initialize _argptr to point past non-variadic arg | |
1160 #if IN_GCC | |
1161 // Handled in FuncDeclaration::toObjFile | |
1162 v_argptr = argptr; | |
1163 v_argptr->init = new VoidInitializer(loc); | |
1164 #else | |
1165 Expression *e1; | |
1166 Expression *e; | |
1167 Type *t = argptr->type; | |
1168 VarDeclaration *p; | |
1169 unsigned offset; | |
1170 | |
1171 e1 = new VarExp(0, argptr); | |
1172 if (parameters && parameters->dim) | |
1173 p = (VarDeclaration *)parameters->data[parameters->dim - 1]; | |
1174 else | |
1175 p = v_arguments; // last parameter is _arguments[] | |
1176 offset = p->type->size(); | |
1177 offset = (offset + 3) & ~3; // assume stack aligns on 4 | |
1178 e = new SymOffExp(0, p, offset); | |
1179 e = new AssignExp(0, e1, e); | |
1180 e->type = t; | |
1181 a->push(new ExpStatement(0, e)); | |
1182 #endif // IN_GCC | |
1183 } | |
1184 | |
1185 if (_arguments) | |
1186 { | |
1187 /* Advance to elements[] member of TypeInfo_Tuple with: | |
1188 * _arguments = v_arguments.elements; | |
1189 */ | |
1190 Expression *e = new VarExp(0, v_arguments); | |
1191 e = new DotIdExp(0, e, Id::elements); | |
1192 Expression *e1 = new VarExp(0, _arguments); | |
1193 e = new AssignExp(0, e1, e); | |
1194 e = e->semantic(sc); | |
1195 a->push(new ExpStatement(0, e)); | |
1196 } | |
1197 | |
1198 #endif // !IN_LLVM | |
1199 | |
1200 // Merge contracts together with body into one compound statement | |
1201 | |
1202 #ifdef _DH | |
1203 if (frequire && global.params.useIn) | |
1204 { frequire->incontract = 1; | |
1205 a->push(frequire); | |
1206 } | |
1207 #else | |
1208 if (frequire && global.params.useIn) | |
1209 a->push(frequire); | |
1210 #endif | |
1211 | |
1212 // Precondition invariant | |
1213 if (addPreInvariant()) | |
1214 { | |
1215 Expression *e = NULL; | |
1216 if (isDtorDeclaration()) | |
1217 { | |
1218 // Call invariant directly only if it exists | |
1219 InvariantDeclaration *inv = ad->inv; | |
1220 ClassDeclaration *cd = ad->isClassDeclaration(); | |
1221 | |
1222 while (!inv && cd) | |
1223 { | |
1224 cd = cd->baseClass; | |
1225 if (!cd) | |
1226 break; | |
1227 inv = cd->inv; | |
1228 } | |
1229 if (inv) | |
1230 { | |
1231 e = new DsymbolExp(0, inv); | |
1232 e = new CallExp(0, e); | |
1233 e = e->semantic(sc2); | |
1234 } | |
1235 } | |
1236 else | |
1237 { // Call invariant virtually | |
1238 ThisExp *v = new ThisExp(0); | |
1239 v->type = vthis->type; | |
1240 Expression *se = new StringExp(0, "null this"); | |
1241 se = se->semantic(sc); | |
1242 se->type = Type::tchar->arrayOf(); | |
1243 e = new AssertExp(loc, v, se); | |
1244 } | |
1245 if (e) | |
1246 { | |
1247 ExpStatement *s = new ExpStatement(0, e); | |
1248 a->push(s); | |
1249 } | |
1250 } | |
1251 | |
1252 if (fbody) | |
1253 a->push(fbody); | |
1254 | |
1255 if (fensure) | |
1256 { | |
1257 a->push(returnLabel->statement); | |
1258 | |
1259 if (type->nextOf()->ty != Tvoid) | |
1260 { | |
1261 // Create: return vresult; | |
1262 assert(vresult); | |
1263 Expression *e = new VarExp(0, vresult); | |
1264 if (tintro) | |
1265 { e = e->implicitCastTo(sc, tintro->nextOf()); | |
1266 e = e->semantic(sc); | |
1267 } | |
1268 ReturnStatement *s = new ReturnStatement(0, e); | |
1269 a->push(s); | |
1270 } | |
1271 } | |
1272 | |
1273 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
|
1274 |
f99a3b393c03
Reorganize EnclosingHandlers to require less changes to the frontend and allow us to
Christian Kamm <kamm incasoftware de>
parents:
1133
diff
changeset
|
1275 // 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
|
1276 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
|
1277 { |
f99a3b393c03
Reorganize EnclosingHandlers to require less changes to the frontend and allow us to
Christian Kamm <kamm incasoftware de>
parents:
1133
diff
changeset
|
1278 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
|
1279 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
|
1280 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
|
1281 |
f99a3b393c03
Reorganize EnclosingHandlers to require less changes to the frontend and allow us to
Christian Kamm <kamm incasoftware de>
parents:
1133
diff
changeset
|
1282 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
|
1283 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
|
1284 { |
f99a3b393c03
Reorganize EnclosingHandlers to require less changes to the frontend and allow us to
Christian Kamm <kamm incasoftware de>
parents:
1133
diff
changeset
|
1285 // 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
|
1286 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
|
1287 } |
f99a3b393c03
Reorganize EnclosingHandlers to require less changes to the frontend and allow us to
Christian Kamm <kamm incasoftware de>
parents:
1133
diff
changeset
|
1288 else |
f99a3b393c03
Reorganize EnclosingHandlers to require less changes to the frontend and allow us to
Christian Kamm <kamm incasoftware de>
parents:
1133
diff
changeset
|
1289 { |
f99a3b393c03
Reorganize EnclosingHandlers to require less changes to the frontend and allow us to
Christian Kamm <kamm incasoftware de>
parents:
1133
diff
changeset
|
1290 // 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
|
1291 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
|
1292 } |
f99a3b393c03
Reorganize EnclosingHandlers to require less changes to the frontend and allow us to
Christian Kamm <kamm incasoftware de>
parents:
1133
diff
changeset
|
1293 |
1158
08c1c3bfea5a
Fix goto inside synchronized functions.
Christian Kamm <kamm incasoftware de>
parents:
1146
diff
changeset
|
1294 // we do not want to rerun semantics on the whole function, so we |
08c1c3bfea5a
Fix goto inside synchronized functions.
Christian Kamm <kamm incasoftware de>
parents:
1146
diff
changeset
|
1295 // manually adjust all labels in the function that currently don't |
08c1c3bfea5a
Fix goto inside synchronized functions.
Christian Kamm <kamm incasoftware de>
parents:
1146
diff
changeset
|
1296 // have an enclosingScopeExit to use the new SynchronizedStatement |
1141
f99a3b393c03
Reorganize EnclosingHandlers to require less changes to the frontend and allow us to
Christian Kamm <kamm incasoftware de>
parents:
1133
diff
changeset
|
1297 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
|
1298 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
|
1299 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
|
1300 |
1158
08c1c3bfea5a
Fix goto inside synchronized functions.
Christian Kamm <kamm incasoftware de>
parents:
1146
diff
changeset
|
1301 // LDC |
08c1c3bfea5a
Fix goto inside synchronized functions.
Christian Kamm <kamm incasoftware de>
parents:
1146
diff
changeset
|
1302 LabelMap::iterator it, end = labmap.end(); |
08c1c3bfea5a
Fix goto inside synchronized functions.
Christian Kamm <kamm incasoftware de>
parents:
1146
diff
changeset
|
1303 for (it = labmap.begin(); it != end; ++it) |
08c1c3bfea5a
Fix goto inside synchronized functions.
Christian Kamm <kamm incasoftware de>
parents:
1146
diff
changeset
|
1304 if (it->second->enclosingScopeExit == NULL) |
08c1c3bfea5a
Fix goto inside synchronized functions.
Christian Kamm <kamm incasoftware de>
parents:
1146
diff
changeset
|
1305 it->second->enclosingScopeExit = s; |
08c1c3bfea5a
Fix goto inside synchronized functions.
Christian Kamm <kamm incasoftware de>
parents:
1146
diff
changeset
|
1306 |
1141
f99a3b393c03
Reorganize EnclosingHandlers to require less changes to the frontend and allow us to
Christian Kamm <kamm incasoftware de>
parents:
1133
diff
changeset
|
1307 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
|
1308 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
|
1309 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
|
1310 } |
159 | 1311 } |
1312 | |
1313 sc2->callSuper = 0; | |
1314 sc2->pop(); | |
1315 } | |
1316 semanticRun = 2; | |
1317 } | |
1318 | |
1319 void FuncDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
1320 { | |
1321 //printf("FuncDeclaration::toCBuffer() '%s'\n", toChars()); | |
1322 | |
1323 type->toCBuffer(buf, ident, hgs); | |
1324 bodyToCBuffer(buf, hgs); | |
1325 } | |
1326 | |
1327 | |
1328 void FuncDeclaration::bodyToCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
1329 { | |
1330 if (fbody && | |
1331 (!hgs->hdrgen || hgs->tpltMember || canInline(1,1)) | |
1332 ) | |
1333 { buf->writenl(); | |
1334 | |
1335 // in{} | |
1336 if (frequire) | |
1337 { buf->writestring("in"); | |
1338 buf->writenl(); | |
1339 frequire->toCBuffer(buf, hgs); | |
1340 } | |
1341 | |
1342 // out{} | |
1343 if (fensure) | |
1344 { buf->writestring("out"); | |
1345 if (outId) | |
1346 { buf->writebyte('('); | |
1347 buf->writestring(outId->toChars()); | |
1348 buf->writebyte(')'); | |
1349 } | |
1350 buf->writenl(); | |
1351 fensure->toCBuffer(buf, hgs); | |
1352 } | |
1353 | |
1354 if (frequire || fensure) | |
1355 { buf->writestring("body"); | |
1356 buf->writenl(); | |
1357 } | |
1358 | |
1359 buf->writebyte('{'); | |
1360 buf->writenl(); | |
1361 fbody->toCBuffer(buf, hgs); | |
1362 buf->writebyte('}'); | |
1363 buf->writenl(); | |
1364 } | |
1365 else | |
1366 { buf->writeByte(';'); | |
1367 buf->writenl(); | |
1368 } | |
1369 } | |
1370 | |
1371 /**************************************************** | |
1372 * Determine if 'this' overrides fd. | |
1373 * Return !=0 if it does. | |
1374 */ | |
1375 | |
1376 int FuncDeclaration::overrides(FuncDeclaration *fd) | |
1377 { int result = 0; | |
1378 | |
1379 if (fd->ident == ident) | |
1380 { | |
1381 int cov = type->covariant(fd->type); | |
1382 if (cov) | |
1383 { ClassDeclaration *cd1 = toParent()->isClassDeclaration(); | |
1384 ClassDeclaration *cd2 = fd->toParent()->isClassDeclaration(); | |
1385 | |
1386 if (cd1 && cd2 && cd2->isBaseOf(cd1, NULL)) | |
1387 result = 1; | |
1388 } | |
1389 } | |
1390 return result; | |
1391 } | |
1392 | |
1393 /************************************************* | |
1394 * Find index of function in vtbl[0..dim] that | |
1395 * this function overrides. | |
1396 * Returns: | |
1397 * -1 didn't find one | |
1398 * -2 can't determine because of forward references | |
1399 */ | |
1400 | |
1401 int FuncDeclaration::findVtblIndex(Array *vtbl, int dim) | |
1402 { | |
1403 for (int vi = 0; vi < dim; vi++) | |
1404 { | |
1405 FuncDeclaration *fdv = ((Dsymbol *)vtbl->data[vi])->isFuncDeclaration(); | |
1406 if (fdv && fdv->ident == ident) | |
1407 { | |
1408 int cov = type->covariant(fdv->type); | |
1409 //printf("\tbaseclass cov = %d\n", cov); | |
1410 switch (cov) | |
1411 { | |
1412 case 0: // types are distinct | |
1413 break; | |
1414 | |
1415 case 1: | |
1416 return vi; | |
1417 | |
1418 case 2: | |
1419 //type->print(); | |
1420 //fdv->type->print(); | |
1421 //printf("%s %s\n", type->deco, fdv->type->deco); | |
1422 error("of type %s overrides but is not covariant with %s of type %s", | |
1423 type->toChars(), fdv->toPrettyChars(), fdv->type->toChars()); | |
1424 break; | |
1425 | |
1426 case 3: | |
1427 return -2; // forward references | |
1428 | |
1429 default: | |
1430 assert(0); | |
1431 } | |
1432 } | |
1433 } | |
1434 return -1; | |
1435 } | |
1436 | |
1437 /**************************************************** | |
1438 * Overload this FuncDeclaration with the new one f. | |
1439 * Return !=0 if successful; i.e. no conflict. | |
1440 */ | |
1441 | |
1442 int FuncDeclaration::overloadInsert(Dsymbol *s) | |
1443 { | |
1444 FuncDeclaration *f; | |
1445 AliasDeclaration *a; | |
1446 | |
1447 //printf("FuncDeclaration::overloadInsert(%s)\n", s->toChars()); | |
1448 a = s->isAliasDeclaration(); | |
1449 if (a) | |
1450 { | |
1451 if (overnext) | |
1452 return overnext->overloadInsert(a); | |
1453 if (!a->aliassym && a->type->ty != Tident && a->type->ty != Tinstance) | |
1454 { | |
1455 //printf("\ta = '%s'\n", a->type->toChars()); | |
1456 return FALSE; | |
1457 } | |
1458 overnext = a; | |
1459 //printf("\ttrue: no conflict\n"); | |
1460 return TRUE; | |
1461 } | |
1462 f = s->isFuncDeclaration(); | |
1463 if (!f) | |
1464 return FALSE; | |
1465 | |
1466 if (type && f->type && // can be NULL for overloaded constructors | |
1467 f->type->covariant(type) && | |
1468 !isFuncAliasDeclaration()) | |
1469 { | |
1470 //printf("\tfalse: conflict %s\n", kind()); | |
1471 return FALSE; | |
1472 } | |
1473 | |
1474 if (overnext) | |
1475 return overnext->overloadInsert(f); | |
1476 overnext = f; | |
1477 //printf("\ttrue: no conflict\n"); | |
1478 return TRUE; | |
1479 } | |
1480 | |
1481 /******************************************** | |
1482 * Find function in overload list that exactly matches t. | |
1483 */ | |
1484 | |
1485 /*************************************************** | |
1486 * Visit each overloaded function in turn, and call | |
1487 * (*fp)(param, f) on it. | |
1488 * Exit when no more, or (*fp)(param, f) returns 1. | |
1489 * Returns: | |
1490 * 0 continue | |
1491 * 1 done | |
1492 */ | |
1493 | |
1494 int overloadApply(FuncDeclaration *fstart, | |
1495 int (*fp)(void *, FuncDeclaration *), | |
1496 void *param) | |
1497 { | |
1498 FuncDeclaration *f; | |
1499 Declaration *d; | |
1500 Declaration *next; | |
1501 | |
1502 for (d = fstart; d; d = next) | |
1503 { FuncAliasDeclaration *fa = d->isFuncAliasDeclaration(); | |
1504 | |
1505 if (fa) | |
1506 { | |
1507 if (overloadApply(fa->funcalias, fp, param)) | |
1508 return 1; | |
1509 next = fa->overnext; | |
1510 } | |
1511 else | |
1512 { | |
1513 AliasDeclaration *a = d->isAliasDeclaration(); | |
1514 | |
1515 if (a) | |
1516 { | |
1517 Dsymbol *s = a->toAlias(); | |
1518 next = s->isDeclaration(); | |
1519 if (next == a) | |
1520 break; | |
1521 if (next == fstart) | |
1522 break; | |
1523 } | |
1524 else | |
1525 { | |
1526 f = d->isFuncDeclaration(); | |
1527 if (!f) | |
1528 { d->error("is aliased to a function"); | |
1529 break; // BUG: should print error message? | |
1530 } | |
1531 if ((*fp)(param, f)) | |
1532 return 1; | |
1533 | |
1534 next = f->overnext; | |
1535 } | |
1536 } | |
1537 } | |
1538 return 0; | |
1539 } | |
1540 | |
1541 /******************************************** | |
1542 * Find function in overload list that exactly matches t. | |
1543 */ | |
1544 | |
1545 struct Param1 | |
1546 { | |
1547 Type *t; // type to match | |
1548 FuncDeclaration *f; // return value | |
1549 }; | |
1550 | |
1551 int fp1(void *param, FuncDeclaration *f) | |
1552 { Param1 *p = (Param1 *)param; | |
1553 Type *t = p->t; | |
1554 | |
1555 if (t->equals(f->type)) | |
1556 { p->f = f; | |
1557 return 1; | |
1558 } | |
1559 | |
336 | 1560 #if DMDV2 |
159 | 1561 /* Allow covariant matches, if it's just a const conversion |
1562 * of the return type | |
1563 */ | |
1564 if (t->ty == Tfunction) | |
1565 { TypeFunction *tf = (TypeFunction *)f->type; | |
1566 if (tf->covariant(t) == 1 && | |
1567 tf->nextOf()->implicitConvTo(t->nextOf()) >= MATCHconst) | |
1568 { | |
1569 p->f = f; | |
1570 return 1; | |
1571 } | |
1572 } | |
1573 #endif | |
1574 return 0; | |
1575 } | |
1576 | |
1577 FuncDeclaration *FuncDeclaration::overloadExactMatch(Type *t) | |
1578 { | |
1579 Param1 p; | |
1580 p.t = t; | |
1581 p.f = NULL; | |
1582 overloadApply(this, &fp1, &p); | |
1583 return p.f; | |
1584 } | |
1585 | |
1586 #if 0 | |
1587 FuncDeclaration *FuncDeclaration::overloadExactMatch(Type *t) | |
1588 { | |
1589 FuncDeclaration *f; | |
1590 Declaration *d; | |
1591 Declaration *next; | |
1592 | |
1593 for (d = this; d; d = next) | |
1594 { FuncAliasDeclaration *fa = d->isFuncAliasDeclaration(); | |
1595 | |
1596 if (fa) | |
1597 { | |
1598 FuncDeclaration *f2 = fa->funcalias->overloadExactMatch(t); | |
1599 if (f2) | |
1600 return f2; | |
1601 next = fa->overnext; | |
1602 } | |
1603 else | |
1604 { | |
1605 AliasDeclaration *a = d->isAliasDeclaration(); | |
1606 | |
1607 if (a) | |
1608 { | |
1609 Dsymbol *s = a->toAlias(); | |
1610 next = s->isDeclaration(); | |
1611 if (next == a) | |
1612 break; | |
1613 } | |
1614 else | |
1615 { | |
1616 f = d->isFuncDeclaration(); | |
1617 if (!f) | |
1618 break; // BUG: should print error message? | |
1619 if (t->equals(d->type)) | |
1620 return f; | |
1621 next = f->overnext; | |
1622 } | |
1623 } | |
1624 } | |
1625 return NULL; | |
1626 } | |
1627 #endif | |
1628 | |
1629 /******************************************** | |
1630 * Decide which function matches the arguments best. | |
1631 */ | |
1632 | |
1633 struct Param2 | |
1634 { | |
1635 Match *m; | |
1636 Expressions *arguments; | |
1637 }; | |
1638 | |
1639 int fp2(void *param, FuncDeclaration *f) | |
1640 { Param2 *p = (Param2 *)param; | |
1641 Match *m = p->m; | |
1642 Expressions *arguments = p->arguments; | |
1643 MATCH match; | |
1644 | |
1645 if (f != m->lastf) // skip duplicates | |
1646 { | |
1647 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
|
1648 TypeFunction *tf = (TypeFunction *)f->type; |
159 | 1649 match = (MATCH) tf->callMatch(arguments); |
717
a26b0c5d5942
Merged DMD 1.036.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
703
diff
changeset
|
1650 //printf("1match = %d\n", match); |
159 | 1651 if (match != MATCHnomatch) |
1652 { | |
1653 if (match > m->last) | |
1654 goto LfIsBetter; | |
1655 | |
1656 if (match < m->last) | |
1657 goto LlastIsBetter; | |
1658 | |
1659 /* See if one of the matches overrides the other. | |
1660 */ | |
1661 if (m->lastf->overrides(f)) | |
1662 goto LlastIsBetter; | |
1663 else if (f->overrides(m->lastf)) | |
1664 goto LfIsBetter; | |
1665 | |
1666 Lambiguous: | |
1667 m->nextf = f; | |
1668 m->count++; | |
1669 return 0; | |
1670 | |
1671 LfIsBetter: | |
1672 m->last = match; | |
1673 m->lastf = f; | |
1674 m->count = 1; | |
1675 return 0; | |
1676 | |
1677 LlastIsBetter: | |
1678 return 0; | |
1679 } | |
1680 } | |
1681 return 0; | |
1682 } | |
1683 | |
1684 | |
1685 void overloadResolveX(Match *m, FuncDeclaration *fstart, Expressions *arguments) | |
1686 { | |
1687 Param2 p; | |
1688 p.m = m; | |
1689 p.arguments = arguments; | |
1690 overloadApply(fstart, &fp2, &p); | |
1691 } | |
1692 | |
1693 #if 0 | |
1694 // Recursive helper function | |
1695 | |
1696 void overloadResolveX(Match *m, FuncDeclaration *fstart, Expressions *arguments) | |
1697 { | |
1698 MATCH match; | |
1699 Declaration *d; | |
1700 Declaration *next; | |
1701 | |
1702 for (d = fstart; d; d = next) | |
1703 { | |
1704 FuncDeclaration *f; | |
1705 FuncAliasDeclaration *fa; | |
1706 AliasDeclaration *a; | |
1707 | |
1708 fa = d->isFuncAliasDeclaration(); | |
1709 if (fa) | |
1710 { | |
1711 overloadResolveX(m, fa->funcalias, arguments); | |
1712 next = fa->overnext; | |
1713 } | |
1714 else if ((f = d->isFuncDeclaration()) != NULL) | |
1715 { | |
1716 next = f->overnext; | |
1717 if (f == m->lastf) | |
1718 continue; // skip duplicates | |
1719 else | |
1720 { | |
1721 TypeFunction *tf; | |
1722 | |
1723 m->anyf = f; | |
1724 tf = (TypeFunction *)f->type; | |
1725 match = (MATCH) tf->callMatch(arguments); | |
717
a26b0c5d5942
Merged DMD 1.036.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
703
diff
changeset
|
1726 //printf("2match = %d\n", match); |
159 | 1727 if (match != MATCHnomatch) |
1728 { | |
1729 if (match > m->last) | |
1730 goto LfIsBetter; | |
1731 | |
1732 if (match < m->last) | |
1733 goto LlastIsBetter; | |
1734 | |
1735 /* See if one of the matches overrides the other. | |
1736 */ | |
1737 if (m->lastf->overrides(f)) | |
1738 goto LlastIsBetter; | |
1739 else if (f->overrides(m->lastf)) | |
1740 goto LfIsBetter; | |
1741 | |
1742 Lambiguous: | |
1743 m->nextf = f; | |
1744 m->count++; | |
1745 continue; | |
1746 | |
1747 LfIsBetter: | |
1748 m->last = match; | |
1749 m->lastf = f; | |
1750 m->count = 1; | |
1751 continue; | |
1752 | |
1753 LlastIsBetter: | |
1754 continue; | |
1755 } | |
1756 } | |
1757 } | |
1758 else if ((a = d->isAliasDeclaration()) != NULL) | |
1759 { | |
1760 Dsymbol *s = a->toAlias(); | |
1761 next = s->isDeclaration(); | |
1762 if (next == a) | |
1763 break; | |
1764 if (next == fstart) | |
1765 break; | |
1766 } | |
1767 else | |
1768 { d->error("is aliased to a function"); | |
1769 break; | |
1770 } | |
1771 } | |
1772 } | |
1773 #endif | |
1774 | |
1775 FuncDeclaration *FuncDeclaration::overloadResolve(Loc loc, Expressions *arguments) | |
1776 { | |
1777 TypeFunction *tf; | |
1778 Match m; | |
1779 | |
1780 #if 0 | |
1781 printf("FuncDeclaration::overloadResolve('%s')\n", toChars()); | |
1782 if (arguments) | |
1783 { int i; | |
1784 | |
1785 for (i = 0; i < arguments->dim; i++) | |
1786 { Expression *arg; | |
1787 | |
1788 arg = (Expression *)arguments->data[i]; | |
1789 assert(arg->type); | |
1790 printf("\t%s: ", arg->toChars()); | |
1791 arg->type->print(); | |
1792 } | |
1793 } | |
1794 #endif | |
1795 | |
1796 memset(&m, 0, sizeof(m)); | |
1797 m.last = MATCHnomatch; | |
1798 overloadResolveX(&m, this, arguments); | |
1799 | |
1800 if (m.count == 1) // exactly one match | |
1801 { | |
1802 return m.lastf; | |
1803 } | |
1804 else | |
1805 { | |
1806 OutBuffer buf; | |
1807 | |
1808 if (arguments) | |
1809 { | |
1810 HdrGenState hgs; | |
1811 | |
1812 argExpTypesToCBuffer(&buf, arguments, &hgs); | |
1813 } | |
1814 | |
1815 if (m.last == MATCHnomatch) | |
1816 { | |
1817 tf = (TypeFunction *)type; | |
1818 | |
1819 //printf("tf = %s, args = %s\n", tf->deco, ((Expression *)arguments->data[0])->type->deco); | |
1820 error(loc, "%s does not match parameter types (%s)", | |
1821 Argument::argsTypesToChars(tf->parameters, tf->varargs), | |
1822 buf.toChars()); | |
1823 return m.anyf; // as long as it's not a FuncAliasDeclaration | |
1824 } | |
1825 else | |
1826 { | |
1827 #if 1 | |
1828 TypeFunction *t1 = (TypeFunction *)m.lastf->type; | |
1829 TypeFunction *t2 = (TypeFunction *)m.nextf->type; | |
1830 | |
1831 error(loc, "called with argument types:\n\t(%s)\nmatches both:\n\t%s%s\nand:\n\t%s%s", | |
1832 buf.toChars(), | |
1833 m.lastf->toPrettyChars(), Argument::argsTypesToChars(t1->parameters, t1->varargs), | |
1834 m.nextf->toPrettyChars(), Argument::argsTypesToChars(t2->parameters, t2->varargs)); | |
1835 #else | |
1836 error(loc, "overloads %s and %s both match argument list for %s", | |
1837 m.lastf->type->toChars(), | |
1838 m.nextf->type->toChars(), | |
1839 m.lastf->toChars()); | |
1840 #endif | |
1841 return m.lastf; | |
1842 } | |
1843 } | |
1844 } | |
1845 | |
1846 /******************************** | |
1847 * Labels are in a separate scope, one per function. | |
1848 */ | |
1849 | |
1850 LabelDsymbol *FuncDeclaration::searchLabel(Identifier *ident) | |
1851 { Dsymbol *s; | |
1852 | |
1853 if (!labtab) | |
1854 labtab = new DsymbolTable(); // guess we need one | |
1855 | |
1856 s = labtab->lookup(ident); | |
1857 if (!s) | |
1858 { | |
1859 s = new LabelDsymbol(ident); | |
1860 labtab->insert(s); | |
1861 } | |
1862 return (LabelDsymbol *)s; | |
1863 } | |
336 | 1864 |
159 | 1865 /**************************************** |
1866 * If non-static member function that has a 'this' pointer, | |
1867 * return the aggregate it is a member of. | |
1868 * Otherwise, return NULL. | |
1869 */ | |
1870 | |
1871 AggregateDeclaration *FuncDeclaration::isThis() | |
1872 { AggregateDeclaration *ad; | |
1873 | |
1874 //printf("+FuncDeclaration::isThis() '%s'\n", toChars()); | |
1875 ad = NULL; | |
1876 if ((storage_class & STCstatic) == 0) | |
1877 { | |
1878 ad = isMember2(); | |
1879 } | |
1880 //printf("-FuncDeclaration::isThis() %p\n", ad); | |
1881 return ad; | |
1882 } | |
1883 | |
1884 AggregateDeclaration *FuncDeclaration::isMember2() | |
1885 { AggregateDeclaration *ad; | |
1886 | |
1887 //printf("+FuncDeclaration::isMember2() '%s'\n", toChars()); | |
1888 ad = NULL; | |
1889 for (Dsymbol *s = this; s; s = s->parent) | |
1890 { | |
1891 //printf("\ts = '%s', parent = '%s', kind = %s\n", s->toChars(), s->parent->toChars(), s->parent->kind()); | |
1892 ad = s->isMember(); | |
1893 if (ad) | |
1894 { //printf("test4\n"); | |
1895 break; | |
1896 } | |
1897 if (!s->parent || | |
1898 (!s->parent->isTemplateInstance())) | |
1899 { //printf("test5\n"); | |
1900 break; | |
1901 } | |
1902 } | |
1903 //printf("-FuncDeclaration::isMember2() %p\n", ad); | |
1904 return ad; | |
1905 } | |
1906 | |
1907 /***************************************** | |
1908 * Determine lexical level difference from 'this' to nested function 'fd'. | |
1909 * Error if this cannot call fd. | |
1910 * Returns: | |
1911 * 0 same level | |
1912 * -1 increase nesting by 1 (fd is nested within 'this') | |
1913 * >0 decrease nesting by number | |
1914 */ | |
1915 | |
1916 int FuncDeclaration::getLevel(Loc loc, FuncDeclaration *fd) | |
1917 { int level; | |
1918 Dsymbol *s; | |
1919 Dsymbol *fdparent; | |
1920 | |
1921 //printf("FuncDeclaration::getLevel(fd = '%s')\n", fd->toChars()); | |
1922 fdparent = fd->toParent2(); | |
1923 if (fdparent == this) | |
1924 return -1; | |
1925 s = this; | |
1926 level = 0; | |
1927 while (fd != s && fdparent != s->toParent2()) | |
1928 { | |
1929 //printf("\ts = '%s'\n", s->toChars()); | |
1930 FuncDeclaration *thisfd = s->isFuncDeclaration(); | |
1931 if (thisfd) | |
1932 { if (!thisfd->isNested() && !thisfd->vthis) | |
1933 goto Lerr; | |
1934 } | |
1935 else | |
1936 { | |
1937 ClassDeclaration *thiscd = s->isClassDeclaration(); | |
1938 if (thiscd) | |
1939 { if (!thiscd->isNested()) | |
1940 goto Lerr; | |
1941 } | |
1942 else | |
1943 goto Lerr; | |
1944 } | |
1945 | |
1946 s = s->toParent2(); | |
1947 assert(s); | |
1948 level++; | |
1949 } | |
1950 return level; | |
1951 | |
1952 Lerr: | |
1953 error(loc, "cannot access frame of function %s", fd->toChars()); | |
1954 return 1; | |
1955 } | |
1956 | |
1957 void FuncDeclaration::appendExp(Expression *e) | |
1958 { Statement *s; | |
1959 | |
1960 s = new ExpStatement(0, e); | |
1961 appendState(s); | |
1962 } | |
1963 | |
1964 void FuncDeclaration::appendState(Statement *s) | |
1965 { CompoundStatement *cs; | |
1966 | |
1967 if (!fbody) | |
1968 { Statements *a; | |
1969 | |
1970 a = new Statements(); | |
1971 fbody = new CompoundStatement(0, a); | |
1972 } | |
1973 cs = fbody->isCompoundStatement(); | |
1974 cs->statements->push(s); | |
1975 } | |
1976 | |
1977 | |
1978 int FuncDeclaration::isMain() | |
1979 { | |
1980 return ident == Id::main && | |
1981 linkage != LINKc && !isMember() && !isNested(); | |
1982 } | |
1983 | |
1984 int FuncDeclaration::isWinMain() | |
1985 { | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
468
diff
changeset
|
1986 //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
|
1987 #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
|
1988 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
|
1989 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
|
1990 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
|
1991 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
|
1992 #else |
159 | 1993 return ident == Id::WinMain && |
1994 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
|
1995 #endif |
159 | 1996 } |
1997 | |
1998 int FuncDeclaration::isDllMain() | |
1999 { | |
2000 return ident == Id::DllMain && | |
2001 linkage != LINKc && !isMember(); | |
2002 } | |
2003 | |
2004 int FuncDeclaration::isExport() | |
2005 { | |
2006 return protection == PROTexport; | |
2007 } | |
2008 | |
2009 int FuncDeclaration::isImportedSymbol() | |
2010 { | |
2011 //printf("isImportedSymbol()\n"); | |
2012 //printf("protection = %d\n", protection); | |
2013 return (protection == PROTexport) && !fbody; | |
2014 } | |
2015 | |
2016 // Determine if function goes into virtual function pointer table | |
2017 | |
2018 int FuncDeclaration::isVirtual() | |
2019 { | |
2020 #if 0 | |
2021 printf("FuncDeclaration::isVirtual(%s)\n", toChars()); | |
2022 printf("%p %d %d %d %d\n", isMember(), isStatic(), protection == PROTprivate, isCtorDeclaration(), linkage != LINKd); | |
2023 printf("result is %d\n", | |
2024 isMember() && | |
2025 !(isStatic() || protection == PROTprivate || protection == PROTpackage) && | |
2026 toParent()->isClassDeclaration()); | |
2027 #endif | |
2028 return isMember() && | |
2029 !(isStatic() || protection == PROTprivate || protection == PROTpackage) && | |
2030 toParent()->isClassDeclaration(); | |
2031 } | |
2032 | |
846
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
717
diff
changeset
|
2033 int FuncDeclaration::isFinal() |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
717
diff
changeset
|
2034 { |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
717
diff
changeset
|
2035 ClassDeclaration *cd; |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
717
diff
changeset
|
2036 #if 0 |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
717
diff
changeset
|
2037 printf("FuncDeclaration::isFinal(%s)\n", toChars()); |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
717
diff
changeset
|
2038 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
|
2039 printf("result is %d\n", |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
717
diff
changeset
|
2040 isMember() && |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
717
diff
changeset
|
2041 !(isStatic() || protection == PROTprivate || protection == PROTpackage) && |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
717
diff
changeset
|
2042 (cd = toParent()->isClassDeclaration()) != NULL && |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
717
diff
changeset
|
2043 cd->storage_class & STCfinal); |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
717
diff
changeset
|
2044 #endif |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
717
diff
changeset
|
2045 return isMember() && |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
717
diff
changeset
|
2046 (Declaration::isFinal() || |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
717
diff
changeset
|
2047 ((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
|
2048 } |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
717
diff
changeset
|
2049 |
159 | 2050 int FuncDeclaration::isAbstract() |
2051 { | |
2052 return storage_class & STCabstract; | |
2053 } | |
2054 | |
2055 int FuncDeclaration::isCodeseg() | |
2056 { | |
2057 return TRUE; // functions are always in the code segment | |
2058 } | |
2059 | |
2060 // Determine if function needs | |
2061 // a static frame pointer to its lexically enclosing function | |
2062 | |
2063 int FuncDeclaration::isNested() | |
2064 { | |
2065 //if (!toParent()) | |
2066 //printf("FuncDeclaration::isNested('%s') parent=%p\n", toChars(), parent); | |
2067 //printf("\ttoParent() = '%s'\n", toParent()->toChars()); | |
2068 return ((storage_class & STCstatic) == 0) && | |
2069 (toParent2()->isFuncDeclaration() != NULL); | |
2070 } | |
2071 | |
2072 int FuncDeclaration::needThis() | |
2073 { | |
2074 //printf("FuncDeclaration::needThis() '%s'\n", toChars()); | |
2075 int i = isThis() != NULL; | |
2076 //printf("\t%d\n", i); | |
2077 if (!i && isFuncAliasDeclaration()) | |
2078 i = ((FuncAliasDeclaration *)this)->funcalias->needThis(); | |
2079 return i; | |
2080 } | |
2081 | |
2082 int FuncDeclaration::addPreInvariant() | |
2083 { | |
2084 AggregateDeclaration *ad = isThis(); | |
2085 return (ad && | |
2086 //ad->isClassDeclaration() && | |
2087 global.params.useInvariants && | |
2088 (protection == PROTpublic || protection == PROTexport) && | |
2089 !naked); | |
2090 } | |
2091 | |
2092 int FuncDeclaration::addPostInvariant() | |
2093 { | |
2094 AggregateDeclaration *ad = isThis(); | |
2095 return (ad && | |
2096 ad->inv && | |
2097 //ad->isClassDeclaration() && | |
2098 global.params.useInvariants && | |
2099 (protection == PROTpublic || protection == PROTexport) && | |
2100 !naked); | |
2101 } | |
2102 | |
2103 /********************************** | |
2104 * Generate a FuncDeclaration for a runtime library function. | |
2105 */ | |
2106 | |
378
d8234836b40f
Get rid of runTimeHack and instead add proper argument info to the frontend
Christian Kamm <kamm incasoftware de>
parents:
336
diff
changeset
|
2107 // |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
658
diff
changeset
|
2108 // 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
|
2109 // |
d8234836b40f
Get rid of runTimeHack and instead add proper argument info to the frontend
Christian Kamm <kamm incasoftware de>
parents:
336
diff
changeset
|
2110 |
1133
eeb8b95ea92e
Cleanup DMD 1.041 merge.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1103
diff
changeset
|
2111 FuncDeclaration *FuncDeclaration::genCfunc(Arguments *args, Type *treturn, const char *name) |
159 | 2112 { |
378
d8234836b40f
Get rid of runTimeHack and instead add proper argument info to the frontend
Christian Kamm <kamm incasoftware de>
parents:
336
diff
changeset
|
2113 return genCfunc(args, treturn, Lexer::idPool(name)); |
159 | 2114 } |
2115 | |
378
d8234836b40f
Get rid of runTimeHack and instead add proper argument info to the frontend
Christian Kamm <kamm incasoftware de>
parents:
336
diff
changeset
|
2116 FuncDeclaration *FuncDeclaration::genCfunc(Arguments *args, Type *treturn, Identifier *id) |
159 | 2117 { |
2118 FuncDeclaration *fd; | |
2119 TypeFunction *tf; | |
2120 Dsymbol *s; | |
2121 static DsymbolTable *st = NULL; | |
2122 | |
2123 //printf("genCfunc(name = '%s')\n", id->toChars()); | |
2124 //printf("treturn\n\t"); treturn->print(); | |
2125 | |
2126 // See if already in table | |
2127 if (!st) | |
2128 st = new DsymbolTable(); | |
2129 s = st->lookup(id); | |
2130 if (s) | |
2131 { | |
2132 fd = s->isFuncDeclaration(); | |
2133 assert(fd); | |
2134 assert(fd->type->nextOf()->equals(treturn)); | |
2135 } | |
2136 else | |
2137 { | |
378
d8234836b40f
Get rid of runTimeHack and instead add proper argument info to the frontend
Christian Kamm <kamm incasoftware de>
parents:
336
diff
changeset
|
2138 tf = new TypeFunction(args, treturn, 0, LINKc); |
159 | 2139 fd = new FuncDeclaration(0, 0, id, STCstatic, tf); |
2140 fd->protection = PROTpublic; | |
2141 fd->linkage = LINKc; | |
2142 | |
2143 st->insert(fd); | |
2144 } | |
2145 return fd; | |
2146 } | |
2147 | |
336 | 2148 const char *FuncDeclaration::kind() |
159 | 2149 { |
2150 return "function"; | |
2151 } | |
336 | 2152 |
159 | 2153 /******************************* |
2154 * Look at all the variables in this function that are referenced | |
2155 * by nested functions, and determine if a closure needs to be | |
2156 * created for them. | |
2157 */ | |
2158 | |
336 | 2159 #if DMDV2 |
159 | 2160 int FuncDeclaration::needsClosure() |
2161 { | |
2162 /* Need a closure for all the closureVars[] if any of the | |
2163 * closureVars[] are accessed by a | |
2164 * function that escapes the scope of this function. | |
2165 * We take the conservative approach and decide that any function that: | |
2166 * 1) is a virtual function | |
2167 * 2) has its address taken | |
2168 * 3) has a parent that escapes | |
336 | 2169 * |
2170 * Note that since a non-virtual function can be called by | |
2171 * a virtual one, if that non-virtual function accesses a closure | |
2172 * var, the closure still has to be taken. Hence, we check for isThis() | |
2173 * instead of isVirtual(). (thanks to David Friedman) | |
159 | 2174 */ |
2175 | |
2176 //printf("FuncDeclaration::needsClosure() %s\n", toChars()); | |
2177 for (int i = 0; i < closureVars.dim; i++) | |
2178 { VarDeclaration *v = (VarDeclaration *)closureVars.data[i]; | |
2179 assert(v->isVarDeclaration()); | |
2180 //printf("\tv = %s\n", v->toChars()); | |
2181 | |
2182 for (int j = 0; j < v->nestedrefs.dim; j++) | |
2183 { FuncDeclaration *f = (FuncDeclaration *)v->nestedrefs.data[j]; | |
2184 assert(f != this); | |
2185 | |
2186 //printf("\t\tf = %s, %d, %d\n", f->toChars(), f->isVirtual(), f->tookAddressOf); | |
336 | 2187 if (f->isThis() || f->tookAddressOf) |
159 | 2188 goto Lyes; // assume f escapes this function's scope |
2189 | |
2190 // 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
|
2191 for (Dsymbol *s = f->parent; s && s != this; s = s->parent) |
159 | 2192 { |
2193 f = s->isFuncDeclaration(); | |
336 | 2194 if (f && (f->isThis() || f->tookAddressOf)) |
159 | 2195 goto Lyes; |
2196 } | |
2197 } | |
2198 } | |
2199 return 0; | |
2200 | |
2201 Lyes: | |
2202 //printf("\tneeds closure\n"); | |
2203 return 1; | |
2204 } | |
2205 #endif | |
2206 | |
2207 /****************************** FuncAliasDeclaration ************************/ | |
2208 | |
2209 // Used as a way to import a set of functions from another scope into this one. | |
2210 | |
2211 FuncAliasDeclaration::FuncAliasDeclaration(FuncDeclaration *funcalias) | |
2212 : FuncDeclaration(funcalias->loc, funcalias->endloc, funcalias->ident, | |
2213 (enum STC)funcalias->storage_class, funcalias->type) | |
2214 { | |
2215 assert(funcalias != this); | |
2216 this->funcalias = funcalias; | |
2217 } | |
2218 | |
336 | 2219 const char *FuncAliasDeclaration::kind() |
159 | 2220 { |
2221 return "function alias"; | |
2222 } | |
2223 | |
2224 | |
2225 /****************************** FuncLiteralDeclaration ************************/ | |
2226 | |
2227 FuncLiteralDeclaration::FuncLiteralDeclaration(Loc loc, Loc endloc, Type *type, | |
2228 enum TOK tok, ForeachStatement *fes) | |
2229 : FuncDeclaration(loc, endloc, NULL, STCundefined, type) | |
2230 { | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
957
diff
changeset
|
2231 const char *id; |
159 | 2232 |
2233 if (fes) | |
2234 id = "__foreachbody"; | |
2235 else if (tok == TOKdelegate) | |
2236 id = "__dgliteral"; | |
2237 else | |
2238 id = "__funcliteral"; | |
2239 this->ident = Identifier::generateId(id); | |
2240 this->tok = tok; | |
2241 this->fes = fes; | |
2242 //printf("FuncLiteralDeclaration() id = '%s', type = '%s'\n", this->ident->toChars(), type->toChars()); | |
2243 } | |
2244 | |
2245 Dsymbol *FuncLiteralDeclaration::syntaxCopy(Dsymbol *s) | |
2246 { | |
2247 FuncLiteralDeclaration *f; | |
2248 | |
2249 //printf("FuncLiteralDeclaration::syntaxCopy('%s')\n", toChars()); | |
2250 if (s) | |
2251 f = (FuncLiteralDeclaration *)s; | |
2252 else | |
2253 f = new FuncLiteralDeclaration(loc, endloc, type->syntaxCopy(), tok, fes); | |
2254 FuncDeclaration::syntaxCopy(f); | |
2255 return f; | |
2256 } | |
2257 | |
2258 int FuncLiteralDeclaration::isNested() | |
2259 { | |
2260 //printf("FuncLiteralDeclaration::isNested() '%s'\n", toChars()); | |
2261 return (tok == TOKdelegate); | |
2262 } | |
2263 | |
336 | 2264 int FuncLiteralDeclaration::isVirtual() |
2265 { | |
2266 return FALSE; | |
2267 } | |
2268 | |
2269 const char *FuncLiteralDeclaration::kind() | |
159 | 2270 { |
2271 // GCC requires the (char*) casts | |
2272 return (tok == TOKdelegate) ? (char*)"delegate" : (char*)"function"; | |
2273 } | |
2274 | |
2275 void FuncLiteralDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2276 { | |
2277 static Identifier *idfunc; | |
2278 static Identifier *iddel; | |
2279 | |
2280 if (!idfunc) | |
2281 idfunc = new Identifier("function", 0); | |
2282 if (!iddel) | |
2283 iddel = new Identifier("delegate", 0); | |
2284 | |
2285 type->toCBuffer(buf, ((tok == TOKdelegate) ? iddel : idfunc), hgs); | |
2286 bodyToCBuffer(buf, hgs); | |
2287 } | |
2288 | |
2289 | |
2290 /********************************* CtorDeclaration ****************************/ | |
2291 | |
2292 CtorDeclaration::CtorDeclaration(Loc loc, Loc endloc, Arguments *arguments, int varargs) | |
2293 : FuncDeclaration(loc, endloc, Id::ctor, STCundefined, NULL) | |
2294 { | |
2295 this->arguments = arguments; | |
2296 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
|
2297 //printf("CtorDeclaration(loc = %s) %s\n", loc.toChars(), toChars()); |
159 | 2298 } |
2299 | |
2300 Dsymbol *CtorDeclaration::syntaxCopy(Dsymbol *s) | |
2301 { | |
2302 CtorDeclaration *f; | |
2303 | |
2304 f = new CtorDeclaration(loc, endloc, NULL, varargs); | |
2305 | |
2306 f->outId = outId; | |
2307 f->frequire = frequire ? frequire->syntaxCopy() : NULL; | |
2308 f->fensure = fensure ? fensure->syntaxCopy() : NULL; | |
2309 f->fbody = fbody ? fbody->syntaxCopy() : NULL; | |
2310 assert(!fthrows); // deprecated | |
2311 | |
2312 f->arguments = Argument::arraySyntaxCopy(arguments); | |
2313 return f; | |
2314 } | |
2315 | |
2316 | |
2317 void CtorDeclaration::semantic(Scope *sc) | |
2318 { | |
2319 ClassDeclaration *cd; | |
2320 Type *tret; | |
2321 | |
2322 //printf("CtorDeclaration::semantic()\n"); | |
2323 if (type) | |
2324 return; | |
2325 | |
2326 sc = sc->push(); | |
2327 sc->stc &= ~STCstatic; // not a static constructor | |
2328 | |
2329 parent = sc->parent; | |
2330 Dsymbol *parent = toParent(); | |
2331 cd = parent->isClassDeclaration(); | |
2332 if (!cd) | |
2333 { | |
2334 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
|
2335 fatal(); |
159 | 2336 tret = Type::tvoid; |
2337 } | |
2338 else | |
2339 tret = cd->type; //->referenceTo(); | |
2340 type = new TypeFunction(arguments, tret, varargs, LINKd); | |
336 | 2341 if (!originalType) |
2342 originalType = type; | |
159 | 2343 |
2344 sc->flags |= SCOPEctor; | |
2345 type = type->semantic(loc, sc); | |
2346 sc->flags &= ~SCOPEctor; | |
2347 | |
2348 // Append: | |
2349 // return this; | |
2350 // to the function body | |
2351 if (fbody) | |
2352 { Expression *e; | |
2353 Statement *s; | |
2354 | |
2355 e = new ThisExp(0); | |
2356 s = new ReturnStatement(0, e); | |
2357 fbody = new CompoundStatement(0, fbody, s); | |
2358 } | |
2359 | |
2360 FuncDeclaration::semantic(sc); | |
2361 | |
2362 sc->pop(); | |
2363 | |
2364 // See if it's the default constructor | |
2365 if (cd && varargs == 0 && Argument::dim(arguments) == 0) | |
2366 cd->defaultCtor = this; | |
2367 } | |
2368 | |
336 | 2369 const char *CtorDeclaration::kind() |
159 | 2370 { |
2371 return "constructor"; | |
2372 } | |
2373 | |
2374 char *CtorDeclaration::toChars() | |
2375 { | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
957
diff
changeset
|
2376 return (char *)"this"; |
159 | 2377 } |
2378 | |
2379 int CtorDeclaration::isVirtual() | |
2380 { | |
2381 return FALSE; | |
2382 } | |
2383 | |
2384 int CtorDeclaration::addPreInvariant() | |
2385 { | |
2386 return FALSE; | |
2387 } | |
2388 | |
2389 int CtorDeclaration::addPostInvariant() | |
2390 { | |
2391 return (vthis && global.params.useInvariants); | |
2392 } | |
2393 | |
2394 | |
2395 void CtorDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2396 { | |
2397 buf->writestring("this"); | |
2398 Argument::argsToCBuffer(buf, hgs, arguments, varargs); | |
2399 bodyToCBuffer(buf, hgs); | |
2400 } | |
2401 | |
2402 /********************************* DtorDeclaration ****************************/ | |
2403 | |
2404 DtorDeclaration::DtorDeclaration(Loc loc, Loc endloc) | |
2405 : FuncDeclaration(loc, endloc, Id::dtor, STCundefined, NULL) | |
2406 { | |
2407 } | |
2408 | |
2409 DtorDeclaration::DtorDeclaration(Loc loc, Loc endloc, Identifier *id) | |
2410 : FuncDeclaration(loc, endloc, id, STCundefined, NULL) | |
2411 { | |
2412 } | |
2413 | |
2414 Dsymbol *DtorDeclaration::syntaxCopy(Dsymbol *s) | |
2415 { | |
2416 assert(!s); | |
2417 DtorDeclaration *dd = new DtorDeclaration(loc, endloc, ident); | |
2418 return FuncDeclaration::syntaxCopy(dd); | |
2419 } | |
2420 | |
2421 | |
2422 void DtorDeclaration::semantic(Scope *sc) | |
2423 { | |
2424 ClassDeclaration *cd; | |
2425 | |
2426 parent = sc->parent; | |
2427 Dsymbol *parent = toParent(); | |
2428 cd = parent->isClassDeclaration(); | |
2429 if (!cd) | |
2430 { | |
2431 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
|
2432 fatal(); |
159 | 2433 } |
2434 else | |
2435 cd->dtors.push(this); | |
2436 type = new TypeFunction(NULL, Type::tvoid, FALSE, LINKd); | |
2437 | |
2438 sc = sc->push(); | |
2439 sc->stc &= ~STCstatic; // not a static destructor | |
2440 sc->linkage = LINKd; | |
2441 | |
2442 FuncDeclaration::semantic(sc); | |
2443 | |
2444 sc->pop(); | |
2445 } | |
2446 | |
2447 int DtorDeclaration::overloadInsert(Dsymbol *s) | |
2448 { | |
2449 return FALSE; // cannot overload destructors | |
2450 } | |
2451 | |
2452 int DtorDeclaration::addPreInvariant() | |
2453 { | |
2454 return (vthis && global.params.useInvariants); | |
2455 } | |
2456 | |
2457 int DtorDeclaration::addPostInvariant() | |
2458 { | |
2459 return FALSE; | |
2460 } | |
2461 | |
2462 int DtorDeclaration::isVirtual() | |
2463 { | |
2464 /* This should be FALSE so that dtor's don't get put into the vtbl[], | |
2465 * but doing so will require recompiling everything. | |
2466 */ | |
2467 #if BREAKABI | |
2468 return FALSE; | |
2469 #else | |
2470 return FuncDeclaration::isVirtual(); | |
2471 #endif | |
2472 } | |
2473 | |
2474 void DtorDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2475 { | |
2476 if (hgs->hdrgen) | |
2477 return; | |
2478 buf->writestring("~this()"); | |
2479 bodyToCBuffer(buf, hgs); | |
2480 } | |
2481 | |
2482 /********************************* StaticCtorDeclaration ****************************/ | |
2483 | |
2484 StaticCtorDeclaration::StaticCtorDeclaration(Loc loc, Loc endloc) | |
2485 : FuncDeclaration(loc, endloc, | |
957
31bbc7f3b817
Backed out changeset [369996c08420].
Christian Kamm <kamm incasoftware de>
parents:
956
diff
changeset
|
2486 Identifier::generateId("_staticCtor"), STCstatic, NULL) |
159 | 2487 { |
2488 } | |
2489 | |
2490 Dsymbol *StaticCtorDeclaration::syntaxCopy(Dsymbol *s) | |
2491 { | |
2492 StaticCtorDeclaration *scd; | |
2493 | |
2494 assert(!s); | |
2495 scd = new StaticCtorDeclaration(loc, endloc); | |
2496 return FuncDeclaration::syntaxCopy(scd); | |
2497 } | |
2498 | |
2499 | |
2500 void StaticCtorDeclaration::semantic(Scope *sc) | |
2501 { | |
2502 //printf("StaticCtorDeclaration::semantic()\n"); | |
2503 | |
2504 type = new TypeFunction(NULL, Type::tvoid, FALSE, LINKd); | |
2505 | |
336 | 2506 /* If the static ctor appears within a template instantiation, |
2507 * it could get called multiple times by the module constructors | |
2508 * for different modules. Thus, protect it with a gate. | |
2509 */ | |
2510 if (inTemplateInstance()) | |
2511 { | |
2512 /* Add this prefix to the function: | |
2513 * static int gate; | |
2514 * if (++gate != 1) return; | |
2515 * Note that this is not thread safe; should not have threads | |
2516 * during static construction. | |
2517 */ | |
2518 Identifier *id = Lexer::idPool("__gate"); | |
2519 VarDeclaration *v = new VarDeclaration(0, Type::tint32, id, NULL); | |
2520 v->storage_class = STCstatic; | |
2521 Statements *sa = new Statements(); | |
2522 Statement *s = new DeclarationStatement(0, v); | |
2523 sa->push(s); | |
2524 Expression *e = new IdentifierExp(0, id); | |
2525 e = new AddAssignExp(0, e, new IntegerExp(1)); | |
2526 e = new EqualExp(TOKnotequal, 0, e, new IntegerExp(1)); | |
2527 s = new IfStatement(0, NULL, e, new ReturnStatement(0, NULL), NULL); | |
2528 sa->push(s); | |
2529 if (fbody) | |
2530 sa->push(fbody); | |
2531 fbody = new CompoundStatement(0, sa); | |
2532 } | |
2533 | |
159 | 2534 FuncDeclaration::semantic(sc); |
2535 | |
2536 // We're going to need ModuleInfo | |
2537 Module *m = getModule(); | |
2538 if (!m) | |
2539 m = sc->module; | |
2540 if (m) | |
2541 { m->needmoduleinfo = 1; | |
2542 #ifdef IN_GCC | |
2543 m->strictlyneedmoduleinfo = 1; | |
2544 #endif | |
2545 } | |
2546 } | |
2547 | |
2548 AggregateDeclaration *StaticCtorDeclaration::isThis() | |
2549 { | |
2550 return NULL; | |
2551 } | |
2552 | |
2553 int StaticCtorDeclaration::isStaticConstructor() | |
2554 { | |
2555 return TRUE; | |
2556 } | |
2557 | |
2558 int StaticCtorDeclaration::isVirtual() | |
2559 { | |
2560 return FALSE; | |
2561 } | |
2562 | |
2563 int StaticCtorDeclaration::addPreInvariant() | |
2564 { | |
2565 return FALSE; | |
2566 } | |
2567 | |
2568 int StaticCtorDeclaration::addPostInvariant() | |
2569 { | |
2570 return FALSE; | |
2571 } | |
2572 | |
2573 void StaticCtorDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2574 { | |
2575 if (hgs->hdrgen) | |
2576 { buf->writestring("static this();\n"); | |
2577 return; | |
2578 } | |
2579 buf->writestring("static this()"); | |
2580 bodyToCBuffer(buf, hgs); | |
2581 } | |
2582 | |
2583 /********************************* StaticDtorDeclaration ****************************/ | |
2584 | |
2585 StaticDtorDeclaration::StaticDtorDeclaration(Loc loc, Loc endloc) | |
2586 : FuncDeclaration(loc, endloc, | |
957
31bbc7f3b817
Backed out changeset [369996c08420].
Christian Kamm <kamm incasoftware de>
parents:
956
diff
changeset
|
2587 Identifier::generateId("_staticDtor"), STCstatic, NULL) |
159 | 2588 { |
336 | 2589 vgate = NULL; |
159 | 2590 } |
2591 | |
2592 Dsymbol *StaticDtorDeclaration::syntaxCopy(Dsymbol *s) | |
2593 { | |
2594 StaticDtorDeclaration *sdd; | |
2595 | |
2596 assert(!s); | |
2597 sdd = new StaticDtorDeclaration(loc, endloc); | |
2598 return FuncDeclaration::syntaxCopy(sdd); | |
2599 } | |
2600 | |
2601 | |
2602 void StaticDtorDeclaration::semantic(Scope *sc) | |
2603 { | |
2604 ClassDeclaration *cd; | |
2605 Type *tret; | |
2606 | |
2607 cd = sc->scopesym->isClassDeclaration(); | |
2608 if (!cd) | |
2609 { | |
2610 } | |
2611 type = new TypeFunction(NULL, Type::tvoid, FALSE, LINKd); | |
2612 | |
336 | 2613 /* If the static ctor appears within a template instantiation, |
2614 * it could get called multiple times by the module constructors | |
2615 * for different modules. Thus, protect it with a gate. | |
2616 */ | |
2617 if (inTemplateInstance()) | |
2618 { | |
2619 /* Add this prefix to the function: | |
2620 * static int gate; | |
2621 * if (--gate != 0) return; | |
2622 * Increment gate during constructor execution. | |
2623 * Note that this is not thread safe; should not have threads | |
2624 * during static destruction. | |
2625 */ | |
2626 Identifier *id = Lexer::idPool("__gate"); | |
2627 VarDeclaration *v = new VarDeclaration(0, Type::tint32, id, NULL); | |
2628 v->storage_class = STCstatic; | |
2629 Statements *sa = new Statements(); | |
2630 Statement *s = new DeclarationStatement(0, v); | |
2631 sa->push(s); | |
2632 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
|
2633 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
|
2634 e = new EqualExp(TOKnotequal, 0, e, new IntegerExp(0)); |
336 | 2635 s = new IfStatement(0, NULL, e, new ReturnStatement(0, NULL), NULL); |
2636 sa->push(s); | |
2637 if (fbody) | |
2638 sa->push(fbody); | |
2639 fbody = new CompoundStatement(0, sa); | |
2640 vgate = v; | |
2641 } | |
2642 | |
159 | 2643 FuncDeclaration::semantic(sc); |
2644 | |
2645 // We're going to need ModuleInfo | |
2646 Module *m = getModule(); | |
2647 if (!m) | |
2648 m = sc->module; | |
2649 if (m) | |
2650 { m->needmoduleinfo = 1; | |
2651 #ifdef IN_GCC | |
2652 m->strictlyneedmoduleinfo = 1; | |
2653 #endif | |
2654 } | |
2655 } | |
2656 | |
2657 AggregateDeclaration *StaticDtorDeclaration::isThis() | |
2658 { | |
2659 return NULL; | |
2660 } | |
2661 | |
2662 int StaticDtorDeclaration::isStaticDestructor() | |
2663 { | |
2664 return TRUE; | |
2665 } | |
2666 | |
2667 int StaticDtorDeclaration::isVirtual() | |
2668 { | |
2669 return FALSE; | |
2670 } | |
2671 | |
2672 int StaticDtorDeclaration::addPreInvariant() | |
2673 { | |
2674 return FALSE; | |
2675 } | |
2676 | |
2677 int StaticDtorDeclaration::addPostInvariant() | |
2678 { | |
2679 return FALSE; | |
2680 } | |
2681 | |
2682 void StaticDtorDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2683 { | |
2684 if (hgs->hdrgen) | |
2685 return; | |
2686 buf->writestring("static ~this()"); | |
2687 bodyToCBuffer(buf, hgs); | |
2688 } | |
2689 | |
2690 /********************************* InvariantDeclaration ****************************/ | |
2691 | |
2692 InvariantDeclaration::InvariantDeclaration(Loc loc, Loc endloc) | |
2693 : FuncDeclaration(loc, endloc, Id::classInvariant, STCundefined, NULL) | |
2694 { | |
2695 } | |
2696 | |
2697 Dsymbol *InvariantDeclaration::syntaxCopy(Dsymbol *s) | |
2698 { | |
2699 InvariantDeclaration *id; | |
2700 | |
2701 assert(!s); | |
2702 id = new InvariantDeclaration(loc, endloc); | |
2703 FuncDeclaration::syntaxCopy(id); | |
2704 return id; | |
2705 } | |
2706 | |
2707 | |
2708 void InvariantDeclaration::semantic(Scope *sc) | |
2709 { | |
2710 AggregateDeclaration *ad; | |
2711 Type *tret; | |
2712 | |
2713 parent = sc->parent; | |
2714 Dsymbol *parent = toParent(); | |
2715 ad = parent->isAggregateDeclaration(); | |
2716 if (!ad) | |
2717 { | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
957
diff
changeset
|
2718 error("invariants are only for struct/union/class definitions"); |
159 | 2719 return; |
2720 } | |
2721 else if (ad->inv && ad->inv != this) | |
2722 { | |
2723 error("more than one invariant for %s", ad->toChars()); | |
2724 } | |
2725 ad->inv = this; | |
2726 type = new TypeFunction(NULL, Type::tvoid, FALSE, LINKd); | |
2727 | |
2728 sc = sc->push(); | |
2729 sc->stc &= ~STCstatic; // not a static invariant | |
2730 sc->incontract++; | |
2731 sc->linkage = LINKd; | |
2732 | |
2733 FuncDeclaration::semantic(sc); | |
2734 | |
2735 sc->pop(); | |
2736 } | |
2737 | |
2738 int InvariantDeclaration::isVirtual() | |
2739 { | |
2740 return FALSE; | |
2741 } | |
2742 | |
2743 int InvariantDeclaration::addPreInvariant() | |
2744 { | |
2745 return FALSE; | |
2746 } | |
2747 | |
2748 int InvariantDeclaration::addPostInvariant() | |
2749 { | |
2750 return FALSE; | |
2751 } | |
2752 | |
2753 void InvariantDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2754 { | |
2755 if (hgs->hdrgen) | |
2756 return; | |
2757 buf->writestring("invariant"); | |
2758 bodyToCBuffer(buf, hgs); | |
2759 } | |
2760 | |
2761 | |
2762 /********************************* UnitTestDeclaration ****************************/ | |
2763 | |
2764 /******************************* | |
2765 * Generate unique unittest function Id so we can have multiple | |
2766 * instances per module. | |
2767 */ | |
2768 | |
2769 static Identifier *unitTestId() | |
2770 { | |
336 | 2771 return Lexer::uniqueId("__unittest"); |
159 | 2772 } |
2773 | |
2774 UnitTestDeclaration::UnitTestDeclaration(Loc loc, Loc endloc) | |
2775 : FuncDeclaration(loc, endloc, unitTestId(), STCundefined, NULL) | |
2776 { | |
2777 } | |
2778 | |
2779 Dsymbol *UnitTestDeclaration::syntaxCopy(Dsymbol *s) | |
2780 { | |
2781 UnitTestDeclaration *utd; | |
2782 | |
2783 assert(!s); | |
2784 utd = new UnitTestDeclaration(loc, endloc); | |
2785 return FuncDeclaration::syntaxCopy(utd); | |
2786 } | |
2787 | |
2788 | |
2789 void UnitTestDeclaration::semantic(Scope *sc) | |
2790 { | |
2791 if (global.params.useUnitTests) | |
2792 { | |
2793 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
|
2794 Scope *sc2 = sc->push(); |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
717
diff
changeset
|
2795 sc2->linkage = LINKd; |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
717
diff
changeset
|
2796 FuncDeclaration::semantic(sc2); |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
717
diff
changeset
|
2797 sc2->pop(); |
159 | 2798 } |
2799 | |
2800 // We're going to need ModuleInfo even if the unit tests are not | |
2801 // compiled in, because other modules may import this module and refer | |
2802 // to this ModuleInfo. | |
2803 Module *m = getModule(); | |
2804 if (!m) | |
2805 m = sc->module; | |
2806 if (m) | |
2807 m->needmoduleinfo = 1; | |
2808 } | |
2809 | |
2810 AggregateDeclaration *UnitTestDeclaration::isThis() | |
2811 { | |
2812 return NULL; | |
2813 } | |
2814 | |
2815 int UnitTestDeclaration::isVirtual() | |
2816 { | |
2817 return FALSE; | |
2818 } | |
2819 | |
2820 int UnitTestDeclaration::addPreInvariant() | |
2821 { | |
2822 return FALSE; | |
2823 } | |
2824 | |
2825 int UnitTestDeclaration::addPostInvariant() | |
2826 { | |
2827 return FALSE; | |
2828 } | |
2829 | |
2830 void UnitTestDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2831 { | |
2832 if (hgs->hdrgen) | |
2833 return; | |
2834 buf->writestring("unittest"); | |
2835 bodyToCBuffer(buf, hgs); | |
2836 } | |
2837 | |
2838 /********************************* NewDeclaration ****************************/ | |
2839 | |
2840 NewDeclaration::NewDeclaration(Loc loc, Loc endloc, Arguments *arguments, int varargs) | |
2841 : FuncDeclaration(loc, endloc, Id::classNew, STCstatic, NULL) | |
2842 { | |
2843 this->arguments = arguments; | |
2844 this->varargs = varargs; | |
2845 } | |
2846 | |
2847 Dsymbol *NewDeclaration::syntaxCopy(Dsymbol *s) | |
2848 { | |
2849 NewDeclaration *f; | |
2850 | |
2851 f = new NewDeclaration(loc, endloc, NULL, varargs); | |
2852 | |
2853 FuncDeclaration::syntaxCopy(f); | |
2854 | |
2855 f->arguments = Argument::arraySyntaxCopy(arguments); | |
2856 | |
2857 return f; | |
2858 } | |
2859 | |
2860 | |
2861 void NewDeclaration::semantic(Scope *sc) | |
2862 { | |
2863 ClassDeclaration *cd; | |
2864 Type *tret; | |
2865 | |
2866 //printf("NewDeclaration::semantic()\n"); | |
2867 | |
2868 parent = sc->parent; | |
2869 Dsymbol *parent = toParent(); | |
2870 cd = parent->isClassDeclaration(); | |
2871 if (!cd && !parent->isStructDeclaration()) | |
2872 { | |
2873 error("new allocators only are for class or struct definitions"); | |
2874 } | |
2875 tret = Type::tvoid->pointerTo(); | |
2876 type = new TypeFunction(arguments, tret, varargs, LINKd); | |
2877 | |
2878 type = type->semantic(loc, sc); | |
2879 assert(type->ty == Tfunction); | |
2880 | |
2881 // Check that there is at least one argument of type uint | |
2882 TypeFunction *tf = (TypeFunction *)type; | |
2883 if (Argument::dim(tf->parameters) < 1) | |
2884 { | |
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
|
2885 error("at least one argument of type size_t expected"); |
159 | 2886 } |
2887 else | |
2888 { | |
2889 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
|
2890 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
|
2891 error("first argument must be type size_t, not %s", a->type->toChars()); |
159 | 2892 } |
2893 | |
2894 FuncDeclaration::semantic(sc); | |
2895 } | |
2896 | |
336 | 2897 const char *NewDeclaration::kind() |
159 | 2898 { |
2899 return "allocator"; | |
2900 } | |
2901 | |
2902 int NewDeclaration::isVirtual() | |
2903 { | |
2904 return FALSE; | |
2905 } | |
2906 | |
2907 int NewDeclaration::addPreInvariant() | |
2908 { | |
2909 return FALSE; | |
2910 } | |
2911 | |
2912 int NewDeclaration::addPostInvariant() | |
2913 { | |
2914 return FALSE; | |
2915 } | |
2916 | |
2917 void NewDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2918 { | |
2919 buf->writestring("new"); | |
2920 Argument::argsToCBuffer(buf, hgs, arguments, varargs); | |
2921 bodyToCBuffer(buf, hgs); | |
2922 } | |
2923 | |
2924 | |
2925 /********************************* DeleteDeclaration ****************************/ | |
2926 | |
2927 DeleteDeclaration::DeleteDeclaration(Loc loc, Loc endloc, Arguments *arguments) | |
2928 : FuncDeclaration(loc, endloc, Id::classDelete, STCstatic, NULL) | |
2929 { | |
2930 this->arguments = arguments; | |
2931 } | |
2932 | |
2933 Dsymbol *DeleteDeclaration::syntaxCopy(Dsymbol *s) | |
2934 { | |
2935 DeleteDeclaration *f; | |
2936 | |
2937 f = new DeleteDeclaration(loc, endloc, NULL); | |
2938 | |
2939 FuncDeclaration::syntaxCopy(f); | |
2940 | |
2941 f->arguments = Argument::arraySyntaxCopy(arguments); | |
2942 | |
2943 return f; | |
2944 } | |
2945 | |
2946 | |
2947 void DeleteDeclaration::semantic(Scope *sc) | |
2948 { | |
2949 ClassDeclaration *cd; | |
2950 | |
2951 //printf("DeleteDeclaration::semantic()\n"); | |
2952 | |
2953 parent = sc->parent; | |
2954 Dsymbol *parent = toParent(); | |
2955 cd = parent->isClassDeclaration(); | |
2956 if (!cd && !parent->isStructDeclaration()) | |
2957 { | |
2958 error("new allocators only are for class or struct definitions"); | |
2959 } | |
2960 type = new TypeFunction(arguments, Type::tvoid, 0, LINKd); | |
2961 | |
2962 type = type->semantic(loc, sc); | |
2963 assert(type->ty == Tfunction); | |
2964 | |
2965 // Check that there is only one argument of type void* | |
2966 TypeFunction *tf = (TypeFunction *)type; | |
2967 if (Argument::dim(tf->parameters) != 1) | |
2968 { | |
2969 error("one argument of type void* expected"); | |
2970 } | |
2971 else | |
2972 { | |
2973 Argument *a = Argument::getNth(tf->parameters, 0); | |
2974 if (!a->type->equals(Type::tvoid->pointerTo())) | |
2975 error("one argument of type void* expected, not %s", a->type->toChars()); | |
2976 } | |
2977 | |
2978 FuncDeclaration::semantic(sc); | |
2979 } | |
2980 | |
336 | 2981 const char *DeleteDeclaration::kind() |
159 | 2982 { |
2983 return "deallocator"; | |
2984 } | |
2985 | |
2986 int DeleteDeclaration::isDelete() | |
2987 { | |
2988 return TRUE; | |
2989 } | |
2990 | |
2991 int DeleteDeclaration::isVirtual() | |
2992 { | |
2993 return FALSE; | |
2994 } | |
2995 | |
2996 int DeleteDeclaration::addPreInvariant() | |
2997 { | |
2998 return FALSE; | |
2999 } | |
3000 | |
3001 int DeleteDeclaration::addPostInvariant() | |
3002 { | |
3003 return FALSE; | |
3004 } | |
3005 | |
3006 void DeleteDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
3007 { | |
3008 buf->writestring("delete"); | |
3009 Argument::argsToCBuffer(buf, hgs, arguments, 0); | |
3010 bodyToCBuffer(buf, hgs); | |
3011 } | |
3012 | |
3013 | |
3014 | |
3015 |