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