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