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