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