Mercurial > projects > ldc
annotate dmd/func.c @ 1617:6820110de311
Merge DMD r301: a little refactor and harmonize
---
dmd/declaration.h | 1 +
dmd/func.c | 30 ++++++++++++++++++++++++++++++
dmd/mtype.c | 51 +++++++++++++++++++++++++++++++++++++--------------
3 files changed, 68 insertions(+), 14 deletions(-)
author | Leandro Lucarella <llucax@gmail.com> |
---|---|
date | Wed, 06 Jan 2010 15:18:21 -0300 |
parents | 679d101395e8 |
children | 44b145be2ef5 |
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 | |
1617
6820110de311
Merge DMD r301: a little refactor and harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1608
diff
changeset
|
853 #if 0 |
1587 | 854 // Propagate storage class from tuple parameters to their element-parameters. |
855 if (f->parameters) | |
856 { | |
857 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
|
858 { Parameter *arg = (Parameter *)f->parameters->data[i]; |
1587 | 859 |
860 //printf("[%d] arg->type->ty = %d %s\n", i, arg->type->ty, arg->type->toChars()); | |
861 if (arg->type->ty == Ttuple) | |
862 { TypeTuple *t = (TypeTuple *)arg->type; | |
1607
207a8a438dea
Merge DMD r253: refactor: Argument => Parameter
Leandro Lucarella <llucax@gmail.com>
parents:
1602
diff
changeset
|
863 size_t dim = Parameter::dim(t->arguments); |
1587 | 864 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
|
865 { Parameter *narg = Parameter::getNth(t->arguments, j); |
1587 | 866 narg->storageClass = arg->storageClass; |
867 } | |
868 } | |
869 } | |
870 } | |
1617
6820110de311
Merge DMD r301: a little refactor and harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1608
diff
changeset
|
871 #endif |
1587 | 872 |
873 /* Declare all the function parameters as variables | |
874 * and install them in parameters[] | |
875 */ | |
1607
207a8a438dea
Merge DMD r253: refactor: Argument => Parameter
Leandro Lucarella <llucax@gmail.com>
parents:
1602
diff
changeset
|
876 size_t nparams = Parameter::dim(f->parameters); |
1587 | 877 if (nparams) |
878 { /* parameters[] has all the tuples removed, as the back end | |
879 * doesn't know about tuples | |
880 */ | |
881 parameters = new Dsymbols(); | |
882 parameters->reserve(nparams); | |
883 for (size_t i = 0; i < nparams; i++) | |
884 { | |
1607
207a8a438dea
Merge DMD r253: refactor: Argument => Parameter
Leandro Lucarella <llucax@gmail.com>
parents:
1602
diff
changeset
|
885 Parameter *arg = Parameter::getNth(f->parameters, i); |
1587 | 886 Identifier *id = arg->ident; |
887 if (!id) | |
888 { | |
889 /* Generate identifier for un-named parameter, | |
890 * because we need it later on. | |
891 */ | |
892 arg->ident = id = Identifier::generateId("_param_", i); | |
893 } | |
894 Type *vtype = arg->type; | |
895 VarDeclaration *v = new VarDeclaration(loc, vtype, id, NULL); | |
896 //printf("declaring parameter %s of type %s\n", v->toChars(), v->type->toChars()); | |
897 v->storage_class |= STCparameter; | |
898 if (f->varargs == 2 && i + 1 == nparams) | |
899 v->storage_class |= STCvariadic; | |
900 v->storage_class |= arg->storageClass & (STCin | STCout | STCref | STClazy); | |
901 if (v->storage_class & STClazy) | |
902 v->storage_class |= STCin; | |
903 v->semantic(sc2); | |
904 if (!sc2->insert(v)) | |
905 error("parameter %s.%s is already defined", toChars(), v->toChars()); | |
906 else | |
907 parameters->push(v); | |
908 localsymtab->insert(v); | |
909 v->parent = this; | |
910 } | |
911 } | |
912 | |
913 // Declare the tuple symbols and put them in the symbol table, | |
914 // but not in parameters[]. | |
915 if (f->parameters) | |
916 { | |
917 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
|
918 { Parameter *arg = (Parameter *)f->parameters->data[i]; |
1587 | 919 |
920 if (!arg->ident) | |
921 continue; // never used, so ignore | |
922 if (arg->type->ty == Ttuple) | |
923 { TypeTuple *t = (TypeTuple *)arg->type; | |
1607
207a8a438dea
Merge DMD r253: refactor: Argument => Parameter
Leandro Lucarella <llucax@gmail.com>
parents:
1602
diff
changeset
|
924 size_t dim = Parameter::dim(t->arguments); |
1587 | 925 Objects *exps = new Objects(); |
926 exps->setDim(dim); | |
927 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
|
928 { Parameter *narg = Parameter::getNth(t->arguments, j); |
1587 | 929 assert(narg->ident); |
930 VarDeclaration *v = sc2->search(0, narg->ident, NULL)->isVarDeclaration(); | |
931 assert(v); | |
932 Expression *e = new VarExp(v->loc, v); | |
933 exps->data[j] = (void *)e; | |
934 } | |
935 assert(arg->ident); | |
936 TupleDeclaration *v = new TupleDeclaration(loc, arg->ident, exps); | |
937 //printf("declaring tuple %s\n", v->toChars()); | |
938 v->isexp = 1; | |
939 if (!sc2->insert(v)) | |
940 error("parameter %s.%s is already defined", toChars(), v->toChars()); | |
941 localsymtab->insert(v); | |
942 v->parent = this; | |
943 } | |
944 } | |
945 } | |
946 | |
947 /* Do the semantic analysis on the [in] preconditions and | |
948 * [out] postconditions. | |
949 */ | |
950 sc2->incontract++; | |
951 | |
952 if (frequire) | |
953 { /* frequire is composed of the [in] contracts | |
954 */ | |
955 // BUG: need to error if accessing out parameters | |
956 // BUG: need to treat parameters as const | |
957 // BUG: need to disallow returns and throws | |
958 // BUG: verify that all in and ref parameters are read | |
959 frequire = frequire->semantic(sc2); | |
960 labtab = NULL; // so body can't refer to labels | |
961 } | |
962 | |
963 if (fensure || addPostInvariant()) | |
964 { /* fensure is composed of the [out] contracts | |
965 */ | |
966 if (!type->nextOf()) | |
967 { // Have to do semantic() on fbody first | |
968 error("post conditions are not supported if the return type is inferred"); | |
969 return; | |
970 } | |
971 | |
972 ScopeDsymbol *sym = new ScopeDsymbol(); | |
973 sym->parent = sc2->scopesym; | |
974 sc2 = sc2->push(sym); | |
975 | |
976 assert(type->nextOf()); | |
977 if (type->nextOf()->ty == Tvoid) | |
978 { | |
979 if (outId) | |
980 error("void functions have no result"); | |
981 } | |
982 else | |
983 { | |
984 if (!outId) | |
985 outId = Id::result; // provide a default | |
986 } | |
987 | |
988 if (outId) | |
989 { // Declare result variable | |
990 VarDeclaration *v; | |
991 Loc loc = this->loc; | |
992 | |
993 if (fensure) | |
994 loc = fensure->loc; | |
995 | |
996 v = new VarDeclaration(loc, type->nextOf(), outId, NULL); | |
997 v->noscope = 1; | |
998 #if DMDV2 | |
1608
679d101395e8
Merge DMD r262: bugzilla 3381 ([tdpl] Incorrect assessment of ...)
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
999 if (!isVirtual()) |
679d101395e8
Merge DMD r262: bugzilla 3381 ([tdpl] Incorrect assessment of ...)
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
1000 v->storage_class |= STCconst; |
1587 | 1001 if (f->isref) |
1002 { | |
1003 v->storage_class |= STCref | STCforeach; | |
1004 } | |
1005 #endif | |
1006 sc2->incontract--; | |
1007 v->semantic(sc2); | |
1008 sc2->incontract++; | |
1009 if (!sc2->insert(v)) | |
1010 error("out result %s is already defined", v->toChars()); | |
1011 v->parent = this; | |
1012 vresult = v; | |
1013 | |
1014 // vresult gets initialized with the function return value | |
1015 // in ReturnStatement::semantic() | |
1016 } | |
1017 | |
1018 // BUG: need to treat parameters as const | |
1019 // BUG: need to disallow returns and throws | |
1020 if (fensure) | |
1021 { fensure = fensure->semantic(sc2); | |
1022 labtab = NULL; // so body can't refer to labels | |
1023 } | |
1024 | |
1025 if (!global.params.useOut) | |
1026 { fensure = NULL; // discard | |
1027 vresult = NULL; | |
1028 } | |
1029 | |
1030 // Postcondition invariant | |
1031 if (addPostInvariant()) | |
1032 { | |
1033 Expression *e = NULL; | |
1034 if (isCtorDeclaration()) | |
1035 { | |
1036 // Call invariant directly only if it exists | |
1037 InvariantDeclaration *inv = ad->inv; | |
1038 ClassDeclaration *cd = ad->isClassDeclaration(); | |
1039 | |
1040 while (!inv && cd) | |
1041 { | |
1042 cd = cd->baseClass; | |
1043 if (!cd) | |
1044 break; | |
1045 inv = cd->inv; | |
1046 } | |
1047 if (inv) | |
1048 { | |
1049 e = new DsymbolExp(0, inv); | |
1050 e = new CallExp(0, e); | |
1051 e = e->semantic(sc2); | |
1052 } | |
1053 } | |
1054 else | |
1055 { // Call invariant virtually | |
1056 Expression *v = new ThisExp(0); | |
1057 v->type = vthis->type; | |
1058 #if STRUCTTHISREF | |
1059 if (ad->isStructDeclaration()) | |
1060 v = v->addressOf(sc); | |
1061 #endif | |
1062 e = new AssertExp(0, v); | |
1063 } | |
1064 if (e) | |
1065 { | |
1066 ExpStatement *s = new ExpStatement(0, e); | |
1067 if (fensure) | |
1068 fensure = new CompoundStatement(0, s, fensure); | |
1069 else | |
1070 fensure = s; | |
1071 } | |
1072 } | |
1073 | |
1074 if (fensure) | |
1075 { returnLabel = new LabelDsymbol(Id::returnLabel); | |
1076 LabelStatement *ls = new LabelStatement(0, Id::returnLabel, fensure); | |
1077 ls->isReturnLabel = 1; | |
1078 returnLabel->statement = ls; | |
1079 } | |
1080 sc2 = sc2->pop(); | |
1081 } | |
1082 | |
1083 sc2->incontract--; | |
1084 | |
1085 if (fbody) | |
1086 { ClassDeclaration *cd = isClassMember(); | |
1087 | |
1088 /* If this is a class constructor | |
1089 */ | |
1090 if (isCtorDeclaration() && cd) | |
1091 { | |
1092 for (int i = 0; i < cd->fields.dim; i++) | |
1093 { VarDeclaration *v = (VarDeclaration *)cd->fields.data[i]; | |
1094 | |
1095 v->ctorinit = 0; | |
1096 } | |
1097 } | |
1098 | |
1099 if (inferRetType || f->retStyle() != RETstack) | |
1100 nrvo_can = 0; | |
1101 | |
1102 fbody = fbody->semantic(sc2); | |
1103 | |
1104 if (inferRetType) | |
1105 { // If no return type inferred yet, then infer a void | |
1106 if (!type->nextOf()) | |
1107 { | |
1108 ((TypeFunction *)type)->next = Type::tvoid; | |
1109 type = type->semantic(loc, sc); | |
1110 } | |
1111 f = (TypeFunction *)type; | |
1112 } | |
1113 | |
1114 int offend = fbody ? fbody->blockExit() & BEfallthru : TRUE; | |
1115 | |
1116 if (isStaticCtorDeclaration()) | |
1117 { /* It's a static constructor. Ensure that all | |
1118 * ctor consts were initialized. | |
1119 */ | |
1120 | |
1121 Dsymbol *p = toParent(); | |
1122 ScopeDsymbol *ad = p->isScopeDsymbol(); | |
1123 if (!ad) | |
1124 { | |
1125 error("static constructor can only be member of struct/class/module, not %s %s", p->kind(), p->toChars()); | |
1126 } | |
1127 else | |
1128 { | |
1129 for (int i = 0; i < ad->members->dim; i++) | |
1130 { Dsymbol *s = (Dsymbol *)ad->members->data[i]; | |
1131 | |
1132 s->checkCtorConstInit(); | |
1133 } | |
1134 } | |
1135 } | |
1136 | |
1137 if (isCtorDeclaration() && cd) | |
1138 { | |
1139 //printf("callSuper = x%x\n", sc2->callSuper); | |
1140 | |
1141 // Verify that all the ctorinit fields got initialized | |
1142 if (!(sc2->callSuper & CSXthis_ctor)) | |
1143 { | |
1144 for (int i = 0; i < cd->fields.dim; i++) | |
1145 { VarDeclaration *v = (VarDeclaration *)cd->fields.data[i]; | |
1146 | |
1147 if (v->ctorinit == 0 && v->isCtorinit()) | |
1148 error("missing initializer for const field %s", v->toChars()); | |
1149 } | |
1150 } | |
1151 | |
1152 if (!(sc2->callSuper & CSXany_ctor) && | |
1153 cd->baseClass && cd->baseClass->ctor) | |
1154 { | |
1155 sc2->callSuper = 0; | |
1156 | |
1157 // Insert implicit super() at start of fbody | |
1158 Expression *e1 = new SuperExp(0); | |
1159 Expression *e = new CallExp(0, e1); | |
1160 | |
1161 unsigned errors = global.errors; | |
1162 global.gag++; | |
1163 e = e->semantic(sc2); | |
1164 global.gag--; | |
1165 if (errors != global.errors) | |
1166 error("no match for implicit super() call in constructor"); | |
1167 | |
1168 Statement *s = new ExpStatement(0, e); | |
1169 fbody = new CompoundStatement(0, s, fbody); | |
1170 } | |
1171 } | |
1172 else if (fes) | |
1173 { // For foreach(){} body, append a return 0; | |
1174 Expression *e = new IntegerExp(0); | |
1175 Statement *s = new ReturnStatement(0, e); | |
1176 fbody = new CompoundStatement(0, fbody, s); | |
1177 assert(!returnLabel); | |
1178 } | |
1179 else if (!hasReturnExp && type->nextOf()->ty != Tvoid) | |
1180 error("expected to return a value of type %s", type->nextOf()->toChars()); | |
1181 else if (!inlineAsm) | |
1182 { | |
1183 #if DMDV2 | |
1184 int blockexit = fbody ? fbody->blockExit() : BEfallthru; | |
1185 if (f->isnothrow && blockexit & BEthrow) | |
1186 error("'%s' is nothrow yet may throw", toChars()); | |
1187 | |
1188 int offend = blockexit & BEfallthru; | |
1189 #endif | |
1190 if (type->nextOf()->ty == Tvoid) | |
1191 { | |
1192 if (offend && isMain()) | |
1193 { // Add a return 0; statement | |
1194 Statement *s = new ReturnStatement(0, new IntegerExp(0)); | |
1195 fbody = new CompoundStatement(0, fbody, s); | |
1196 } | |
1197 } | |
1198 else | |
1199 { | |
1200 if (offend) | |
1201 { Expression *e; | |
1202 #if DMDV1 | |
1203 warning(loc, "no return exp; or assert(0); at end of function"); | |
1204 #else | |
1205 error("no return exp; or assert(0); at end of function"); | |
1206 #endif | |
1207 if (global.params.useAssert && | |
1208 !global.params.useInline) | |
1209 { /* Add an assert(0, msg); where the missing return | |
1210 * should be. | |
1211 */ | |
1212 e = new AssertExp( | |
1213 endloc, | |
1214 new IntegerExp(0), | |
1215 new StringExp(loc, (char *)"missing return expression") | |
1216 ); | |
1217 } | |
1218 else | |
1219 e = new HaltExp(endloc); | |
1220 e = new CommaExp(0, e, type->nextOf()->defaultInit()); | |
1221 e = e->semantic(sc2); | |
1222 Statement *s = new ExpStatement(0, e); | |
1223 fbody = new CompoundStatement(0, fbody, s); | |
1224 } | |
1225 } | |
1226 } | |
1227 } | |
1228 | |
1229 { | |
1230 Statements *a = new Statements(); | |
1231 | |
1232 // Merge in initialization of 'out' parameters | |
1233 if (parameters) | |
1234 { for (size_t i = 0; i < parameters->dim; i++) | |
1235 { | |
1236 VarDeclaration *v = (VarDeclaration *)parameters->data[i]; | |
1237 if (v->storage_class & STCout) | |
1238 { | |
1239 assert(v->init); | |
1240 ExpInitializer *ie = v->init->isExpInitializer(); | |
1241 assert(ie); | |
1242 a->push(new ExpStatement(0, ie->exp)); | |
1243 } | |
1244 } | |
1245 } | |
1246 | |
1247 // we'll handle variadics ourselves | |
1248 #if !IN_LLVM | |
1249 if (argptr) | |
1250 { // Initialize _argptr to point past non-variadic arg | |
1251 #if IN_GCC | |
1252 // Handled in FuncDeclaration::toObjFile | |
1253 v_argptr = argptr; | |
1254 v_argptr->init = new VoidInitializer(loc); | |
1255 #else | |
1256 Type *t = argptr->type; | |
1257 VarDeclaration *p; | |
1258 unsigned offset; | |
1259 | |
1602
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
1260 Expression *e1 = new VarExp(0, argptr); |
1587 | 1261 if (parameters && parameters->dim) |
1262 p = (VarDeclaration *)parameters->data[parameters->dim - 1]; | |
1263 else | |
1264 p = v_arguments; // last parameter is _arguments[] | |
1265 if (p->storage_class & STClazy) | |
1266 // If the last parameter is lazy, it's the size of a delegate | |
1267 offset = PTRSIZE * 2; | |
1268 else | |
1269 offset = p->type->size(); | |
1270 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
|
1271 Expression *e = new SymOffExp(0, p, offset); |
1587 | 1272 e = new AssignExp(0, e1, e); |
1273 e->type = t; | |
1274 a->push(new ExpStatement(0, e)); | |
1275 #endif // IN_GCC | |
1276 } | |
1277 | |
1278 if (_arguments) | |
1279 { | |
1280 /* Advance to elements[] member of TypeInfo_Tuple with: | |
1281 * _arguments = v_arguments.elements; | |
1282 */ | |
1283 Expression *e = new VarExp(0, v_arguments); | |
1284 e = new DotIdExp(0, e, Id::elements); | |
1285 Expression *e1 = new VarExp(0, _arguments); | |
1286 e = new AssignExp(0, e1, e); | |
1287 e->op = TOKconstruct; | |
1288 e = e->semantic(sc2); | |
1289 a->push(new ExpStatement(0, e)); | |
1290 } | |
1291 | |
1292 #endif // !IN_LLVM | |
1293 | |
1294 // Merge contracts together with body into one compound statement | |
1295 | |
1296 #ifdef _DH | |
1297 if (frequire && global.params.useIn) | |
1298 { frequire->incontract = 1; | |
1299 a->push(frequire); | |
1300 } | |
1301 #else | |
1302 if (frequire && global.params.useIn) | |
1303 a->push(frequire); | |
1304 #endif | |
1305 | |
1306 // Precondition invariant | |
1307 if (addPreInvariant()) | |
1308 { | |
1309 Expression *e = NULL; | |
1310 Expression *ee = NULL; | |
1311 if (isDtorDeclaration()) | |
1312 { | |
1313 // Call invariant directly only if it exists | |
1314 InvariantDeclaration *inv = ad->inv; | |
1315 ClassDeclaration *cd = ad->isClassDeclaration(); | |
1316 | |
1317 while (!inv && cd) | |
1318 { | |
1319 cd = cd->baseClass; | |
1320 if (!cd) | |
1321 break; | |
1322 inv = cd->inv; | |
1323 } | |
1324 if (inv) | |
1325 { | |
1326 e = new DsymbolExp(0, inv); | |
1327 e = new CallExp(0, e); | |
1328 e = e->semantic(sc2); | |
1329 } | |
1330 } | |
1331 else | |
1332 { // Call invariant virtually | |
1333 // LDC: unless this is a struct without invariant | |
1334 StructDeclaration* sd = ad->isStructDeclaration(); | |
1335 if (!sd || sd->inv) | |
1336 { | |
1337 ThisExp *v = new ThisExp(0); | |
1338 v->type = vthis->type; | |
1339 e = new AssertExp(loc, v, NULL); | |
1340 } | |
1341 | |
1342 // LDC: check for null this | |
1343 ThisExp* v = new ThisExp(0); | |
1344 v->type = vthis->type; | |
1345 #if STRUCTTHISREF | |
1346 if (ad->isStructDeclaration()) | |
1347 v = v->addressOf(sc); | |
1348 #endif | |
1349 v->var = vthis; | |
1350 | |
1351 NullExp *nv = new NullExp(0); | |
1352 nv->type = v->type; | |
1353 | |
1354 IdentityExp *ie = new IdentityExp(TOKnotidentity, 0, v, nv); | |
1355 ie->type = Type::tbool; | |
1356 | |
1357 Expression *se = new StringExp(0, (char *)"null this"); | |
1358 se = se->semantic(sc); | |
1359 se->type = Type::tchar->arrayOf(); | |
1360 | |
1361 ee = new AssertExp(loc, ie, se); | |
1362 } | |
1363 if (ee) | |
1364 { | |
1365 ExpStatement *s = new ExpStatement(0, ee); | |
1366 a->push(s); | |
1367 } | |
1368 if (e) | |
1369 { | |
1370 ExpStatement *s = new ExpStatement(0, e); | |
1371 a->push(s); | |
1372 } | |
1373 } | |
1374 | |
1375 if (fbody) | |
1376 a->push(fbody); | |
1377 | |
1378 if (fensure) | |
1379 { | |
1380 a->push(returnLabel->statement); | |
1381 | |
1382 if (type->nextOf()->ty != Tvoid) | |
1383 { | |
1384 // Create: return vresult; | |
1385 assert(vresult); | |
1386 Expression *e = new VarExp(0, vresult); | |
1387 if (tintro) | |
1388 { e = e->implicitCastTo(sc, tintro->nextOf()); | |
1389 e = e->semantic(sc); | |
1390 } | |
1391 ReturnStatement *s = new ReturnStatement(0, e); | |
1392 a->push(s); | |
1393 } | |
1394 } | |
1395 | |
1396 fbody = new CompoundStatement(0, a); | |
1397 #if DMDV2 | |
1398 /* Append destructor calls for parameters as finally blocks. | |
1399 */ | |
1400 if (parameters) | |
1401 { for (size_t i = 0; i < parameters->dim; i++) | |
1402 { | |
1403 VarDeclaration *v = (VarDeclaration *)parameters->data[i]; | |
1404 | |
1405 if (v->storage_class & (STCref | STCout)) | |
1406 continue; | |
1407 | |
1408 /* Don't do this for static arrays, since static | |
1409 * arrays are called by reference. Remove this | |
1410 * when we change them to call by value. | |
1411 */ | |
1412 if (v->type->toBasetype()->ty == Tsarray) | |
1413 continue; | |
1414 | |
1415 Expression *e = v->callAutoDtor(sc); | |
1416 if (e) | |
1417 { Statement *s = new ExpStatement(0, e); | |
1418 s = s->semantic(sc); | |
1419 if (fbody->blockExit() == BEfallthru) | |
1420 fbody = new CompoundStatement(0, fbody, s); | |
1421 else | |
1422 fbody = new TryFinallyStatement(0, fbody, s); | |
1423 } | |
1424 } | |
1425 } | |
1426 #endif | |
1427 | |
1428 // wrap body of synchronized functions in a synchronized statement | |
1429 if (isSynchronized()) | |
1430 { | |
1431 ClassDeclaration *cd = parent->isClassDeclaration(); | |
1432 if (!cd) | |
1433 error("synchronized function %s must be a member of a class", toChars()); | |
1434 | |
1435 Expression *sync; | |
1436 if (isStatic()) | |
1437 { | |
1438 // static member functions synchronize on classinfo | |
1439 sync = cd->type->dotExp(sc2, new TypeExp(loc, cd->type), Id::classinfo); | |
1440 } | |
1441 else | |
1442 { | |
1443 // non-static member functions synchronize on this | |
1444 sync = new VarExp(loc, vthis); | |
1445 } | |
1446 | |
1447 // we do not want to rerun semantics on the whole function, so we | |
1448 // manually adjust all labels in the function that currently don't | |
1449 // have an enclosingScopeExit to use the new SynchronizedStatement | |
1450 SynchronizedStatement* s = new SynchronizedStatement(loc, sync, NULL); | |
1451 s->semantic(sc2); | |
1452 s->body = fbody; | |
1453 | |
1454 // LDC | |
1455 LabelMap::iterator it, end = labmap.end(); | |
1456 for (it = labmap.begin(); it != end; ++it) | |
1457 if (it->second->enclosingScopeExit == NULL) | |
1458 it->second->enclosingScopeExit = s; | |
1459 | |
1460 a = new Statements; | |
1461 a->push(s); | |
1462 fbody = new CompoundStatement(0, a); | |
1463 } | |
1464 } | |
1465 | |
1466 sc2->callSuper = 0; | |
1467 sc2->pop(); | |
1468 } | |
1469 semanticRun = 4; | |
1470 } | |
1471 | |
1472 void FuncDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
1473 { | |
1474 //printf("FuncDeclaration::toCBuffer() '%s'\n", toChars()); | |
1475 | |
1476 type->toCBuffer(buf, ident, hgs); | |
1477 bodyToCBuffer(buf, hgs); | |
1478 } | |
1479 | |
1480 | |
1481 void FuncDeclaration::bodyToCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
1482 { | |
1483 if (fbody && | |
1484 (!hgs->hdrgen || hgs->tpltMember || canInline(1,1)) | |
1485 ) | |
1486 { buf->writenl(); | |
1487 | |
1488 // in{} | |
1489 if (frequire) | |
1490 { buf->writestring("in"); | |
1491 buf->writenl(); | |
1492 frequire->toCBuffer(buf, hgs); | |
1493 } | |
1494 | |
1495 // out{} | |
1496 if (fensure) | |
1497 { buf->writestring("out"); | |
1498 if (outId) | |
1499 { buf->writebyte('('); | |
1500 buf->writestring(outId->toChars()); | |
1501 buf->writebyte(')'); | |
1502 } | |
1503 buf->writenl(); | |
1504 fensure->toCBuffer(buf, hgs); | |
1505 } | |
1506 | |
1507 if (frequire || fensure) | |
1508 { buf->writestring("body"); | |
1509 buf->writenl(); | |
1510 } | |
1511 | |
1512 buf->writebyte('{'); | |
1513 buf->writenl(); | |
1514 fbody->toCBuffer(buf, hgs); | |
1515 buf->writebyte('}'); | |
1516 buf->writenl(); | |
1517 } | |
1518 else | |
1519 { buf->writeByte(';'); | |
1520 buf->writenl(); | |
1521 } | |
1522 } | |
1523 | |
1524 /**************************************************** | |
1525 * Merge into this function the 'in' contracts of all it overrides. | |
1526 * 'in's are OR'd together, i.e. only one of them needs to pass. | |
1527 */ | |
1528 | |
1529 Statement *FuncDeclaration::mergeFrequire(Statement *sf) | |
1530 { | |
1531 /* Implementing this is done by having the overriding function call | |
1532 * nested functions (the fdrequire functions) nested inside the overridden | |
1533 * function. This requires that the stack layout of the calling function's | |
1534 * parameters and 'this' pointer be in the same place (as the nested | |
1535 * function refers to them). | |
1536 * This is easy for the parameters, as they are all on the stack in the same | |
1537 * place by definition, since it's an overriding function. The problem is | |
1538 * getting the 'this' pointer in the same place, since it is a local variable. | |
1539 * We did some hacks in the code generator to make this happen: | |
1540 * 1. always generate exception handler frame, or at least leave space for it | |
1541 * in the frame (Windows 32 SEH only) | |
1542 * 2. always generate an EBP style frame | |
1543 * 3. since 'this' is passed in a register that is subsequently copied into | |
1544 * a stack local, allocate that local immediately following the exception | |
1545 * handler block, so it is always at the same offset from EBP. | |
1546 */ | |
1547 for (int i = 0; i < foverrides.dim; i++) | |
1548 { | |
1549 FuncDeclaration *fdv = (FuncDeclaration *)foverrides.data[i]; | |
1550 sf = fdv->mergeFrequire(sf); | |
1551 if (fdv->fdrequire) | |
1552 { | |
1553 //printf("fdv->frequire: %s\n", fdv->frequire->toChars()); | |
1554 /* Make the call: | |
1555 * try { __require(); } | |
1556 * catch { frequire; } | |
1557 */ | |
1558 Expression *eresult = NULL; | |
1559 Expression *e = new CallExp(loc, new VarExp(loc, fdv->fdrequire), eresult); | |
1560 Statement *s2 = new ExpStatement(loc, e); | |
1561 | |
1562 if (sf) | |
1563 { Catch *c = new Catch(loc, NULL, NULL, sf); | |
1564 Array *catches = new Array(); | |
1565 catches->push(c); | |
1566 sf = new TryCatchStatement(loc, s2, catches); | |
1567 } | |
1568 else | |
1569 sf = s2; | |
1570 } | |
1571 } | |
1572 return sf; | |
1573 } | |
1574 | |
1575 /**************************************************** | |
1576 * Merge into this function the 'out' contracts of all it overrides. | |
1577 * 'out's are AND'd together, i.e. all of them need to pass. | |
1578 */ | |
1579 | |
1580 Statement *FuncDeclaration::mergeFensure(Statement *sf) | |
1581 { | |
1582 /* Same comments as for mergeFrequire(), except that we take care | |
1583 * of generating a consistent reference to the 'result' local by | |
1584 * explicitly passing 'result' to the nested function as a reference | |
1585 * argument. | |
1586 * This won't work for the 'this' parameter as it would require changing | |
1587 * the semantic code for the nested function so that it looks on the parameter | |
1588 * list for the 'this' pointer, something that would need an unknown amount | |
1589 * of tweaking of various parts of the compiler that I'd rather leave alone. | |
1590 */ | |
1591 for (int i = 0; i < foverrides.dim; i++) | |
1592 { | |
1593 FuncDeclaration *fdv = (FuncDeclaration *)foverrides.data[i]; | |
1594 sf = fdv->mergeFensure(sf); | |
1595 if (fdv->fdensure) | |
1596 { | |
1597 //printf("fdv->fensure: %s\n", fdv->fensure->toChars()); | |
1598 // Make the call: __ensure(result) | |
1599 Expression *eresult = NULL; | |
1600 if (outId) | |
1601 eresult = new IdentifierExp(loc, outId); | |
1602 Expression *e = new CallExp(loc, new VarExp(loc, fdv->fdensure), eresult); | |
1603 Statement *s2 = new ExpStatement(loc, e); | |
1604 | |
1605 if (sf) | |
1606 { | |
1607 sf = new CompoundStatement(fensure->loc, s2, sf); | |
1608 } | |
1609 else | |
1610 sf = s2; | |
1611 } | |
1612 } | |
1613 return sf; | |
1614 } | |
1615 | |
1616 /**************************************************** | |
1617 * Determine if 'this' overrides fd. | |
1618 * Return !=0 if it does. | |
1619 */ | |
1620 | |
1621 int FuncDeclaration::overrides(FuncDeclaration *fd) | |
1622 { int result = 0; | |
1623 | |
1624 if (fd->ident == ident) | |
1625 { | |
1626 int cov = type->covariant(fd->type); | |
1627 if (cov) | |
1628 { ClassDeclaration *cd1 = toParent()->isClassDeclaration(); | |
1629 ClassDeclaration *cd2 = fd->toParent()->isClassDeclaration(); | |
1630 | |
1631 if (cd1 && cd2 && cd2->isBaseOf(cd1, NULL)) | |
1632 result = 1; | |
1633 } | |
1634 } | |
1635 return result; | |
1636 } | |
1637 | |
1638 /************************************************* | |
1639 * Find index of function in vtbl[0..dim] that | |
1640 * this function overrides. | |
1641 * Returns: | |
1642 * -1 didn't find one | |
1643 * -2 can't determine because of forward references | |
1644 */ | |
1645 | |
1646 int FuncDeclaration::findVtblIndex(Array *vtbl, int dim) | |
1647 { | |
1648 for (int vi = 0; vi < dim; vi++) | |
1649 { | |
1650 FuncDeclaration *fdv = ((Dsymbol *)vtbl->data[vi])->isFuncDeclaration(); | |
1651 if (fdv && fdv->ident == ident) | |
1652 { | |
1653 int cov = type->covariant(fdv->type); | |
1654 //printf("\tbaseclass cov = %d\n", cov); | |
1655 switch (cov) | |
1656 { | |
1657 case 0: // types are distinct | |
1658 break; | |
1659 | |
1660 case 1: | |
1661 return vi; | |
1662 | |
1663 case 2: | |
1664 //type->print(); | |
1665 //fdv->type->print(); | |
1666 //printf("%s %s\n", type->deco, fdv->type->deco); | |
1667 error("of type %s overrides but is not covariant with %s of type %s", | |
1668 type->toChars(), fdv->toPrettyChars(), fdv->type->toChars()); | |
1669 break; | |
1670 | |
1671 case 3: | |
1672 return -2; // forward references | |
1673 | |
1674 default: | |
1675 assert(0); | |
1676 } | |
1677 } | |
1678 } | |
1679 return -1; | |
1680 } | |
1681 | |
1682 /**************************************************** | |
1683 * Overload this FuncDeclaration with the new one f. | |
1684 * Return !=0 if successful; i.e. no conflict. | |
1685 */ | |
1686 | |
1687 int FuncDeclaration::overloadInsert(Dsymbol *s) | |
1688 { | |
1689 FuncDeclaration *f; | |
1690 AliasDeclaration *a; | |
1691 | |
1692 //printf("FuncDeclaration::overloadInsert(%s)\n", s->toChars()); | |
1693 a = s->isAliasDeclaration(); | |
1694 if (a) | |
1695 { | |
1696 if (overnext) | |
1697 return overnext->overloadInsert(a); | |
1698 if (!a->aliassym && a->type->ty != Tident && a->type->ty != Tinstance) | |
1699 { | |
1700 //printf("\ta = '%s'\n", a->type->toChars()); | |
1701 return FALSE; | |
1702 } | |
1703 overnext = a; | |
1704 //printf("\ttrue: no conflict\n"); | |
1705 return TRUE; | |
1706 } | |
1707 f = s->isFuncDeclaration(); | |
1708 if (!f) | |
1709 return FALSE; | |
1710 | |
1711 if (type && f->type && // can be NULL for overloaded constructors | |
1712 f->type->covariant(type) && | |
1713 !isFuncAliasDeclaration()) | |
1714 { | |
1715 //printf("\tfalse: conflict %s\n", kind()); | |
1716 return FALSE; | |
1717 } | |
1718 | |
1719 if (overnext) | |
1720 return overnext->overloadInsert(f); | |
1721 overnext = f; | |
1722 //printf("\ttrue: no conflict\n"); | |
1723 return TRUE; | |
1724 } | |
1725 | |
1726 /******************************************** | |
1727 * Find function in overload list that exactly matches t. | |
1728 */ | |
1729 | |
1730 /*************************************************** | |
1731 * Visit each overloaded function in turn, and call | |
1732 * (*fp)(param, f) on it. | |
1733 * Exit when no more, or (*fp)(param, f) returns 1. | |
1734 * Returns: | |
1735 * 0 continue | |
1736 * 1 done | |
1737 */ | |
1738 | |
1739 int overloadApply(Module* from, FuncDeclaration *fstart, | |
1740 int (*fp)(void *, FuncDeclaration *), | |
1741 void *param) | |
1742 { | |
1743 FuncDeclaration *f; | |
1744 Declaration *d; | |
1745 Declaration *next; | |
1746 | |
1747 for (d = fstart; d; d = next) | |
1748 { FuncAliasDeclaration *fa = d->isFuncAliasDeclaration(); | |
1749 | |
1750 if (fa) | |
1751 { | |
1752 if (fa->getModule() == from || fa->importprot != PROTprivate) | |
1753 if (overloadApply(from, fa->funcalias, fp, param)) | |
1754 return 1; | |
1755 next = fa->overnext; | |
1756 } | |
1757 else | |
1758 { | |
1759 AliasDeclaration *a = d->isAliasDeclaration(); | |
1760 | |
1761 if (a) | |
1762 { | |
1763 Dsymbol *s = a->toAlias(); | |
1764 next = s->isDeclaration(); | |
1765 if (next == a) | |
1766 break; | |
1767 if (next == fstart) | |
1768 break; | |
1769 if (a->importprot == PROTprivate && a->getModule() != from) | |
1770 if (FuncDeclaration* fd = next->isFuncDeclaration()) | |
1771 next = fd->overnext; | |
1772 } | |
1773 else | |
1774 { | |
1775 f = d->isFuncDeclaration(); | |
1776 if (!f) | |
1777 { d->error("is aliased to a function"); | |
1778 break; // BUG: should print error message? | |
1779 } | |
1780 if ((*fp)(param, f)) | |
1781 return 1; | |
1782 | |
1783 next = f->overnext; | |
1784 } | |
1785 } | |
1786 } | |
1787 return 0; | |
1788 } | |
1789 | |
1790 /******************************************** | |
1791 * If there are no overloads of function f, return that function, | |
1792 * otherwise return NULL. | |
1793 */ | |
1794 | |
1795 static int fpunique(void *param, FuncDeclaration *f) | |
1796 { FuncDeclaration **pf = (FuncDeclaration **)param; | |
1797 | |
1798 if (*pf) | |
1799 { *pf = NULL; | |
1800 return 1; // ambiguous, done | |
1801 } | |
1802 else | |
1803 { *pf = f; | |
1804 return 0; | |
1805 } | |
1806 } | |
1807 | |
1808 FuncDeclaration *FuncDeclaration::isUnique() | |
1809 { FuncDeclaration *result = NULL; | |
1810 | |
1811 overloadApply(getModule(), this, &fpunique, &result); | |
1812 return result; | |
1813 } | |
1814 | |
1815 /******************************************** | |
1816 * Find function in overload list that exactly matches t. | |
1817 */ | |
1818 | |
1819 struct Param1 | |
1820 { | |
1821 Type *t; // type to match | |
1822 FuncDeclaration *f; // return value | |
1823 }; | |
1824 | |
1825 int fp1(void *param, FuncDeclaration *f) | |
1826 { Param1 *p = (Param1 *)param; | |
1827 Type *t = p->t; | |
1828 | |
1829 if (t->equals(f->type)) | |
1830 { p->f = f; | |
1831 return 1; | |
1832 } | |
1833 | |
1834 #if DMDV2 | |
1835 /* Allow covariant matches, if it's just a const conversion | |
1836 * of the return type | |
1837 */ | |
1838 if (t->ty == Tfunction) | |
1839 { TypeFunction *tf = (TypeFunction *)f->type; | |
1840 if (tf->covariant(t) == 1 && | |
1841 tf->nextOf()->implicitConvTo(t->nextOf()) >= MATCHconst) | |
1842 { | |
1843 p->f = f; | |
1844 return 1; | |
1845 } | |
1846 } | |
1847 #endif | |
1848 return 0; | |
1849 } | |
1850 | |
1851 FuncDeclaration *FuncDeclaration::overloadExactMatch(Type *t, Module* from) | |
1852 { | |
1853 Param1 p; | |
1854 p.t = t; | |
1855 p.f = NULL; | |
1856 overloadApply(from, this, &fp1, &p); | |
1857 return p.f; | |
1858 } | |
1859 | |
1860 | |
1861 /******************************************** | |
1862 * Decide which function matches the arguments best. | |
1863 */ | |
1864 | |
1865 struct Param2 | |
1866 { | |
1867 Match *m; | |
1602
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
1868 #if DMDV2 |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
1869 Expression *ethis; |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
1870 #endif |
1587 | 1871 Expressions *arguments; |
1872 }; | |
1873 | |
1874 int fp2(void *param, FuncDeclaration *f) | |
1875 { Param2 *p = (Param2 *)param; | |
1876 Match *m = p->m; | |
1877 Expressions *arguments = p->arguments; | |
1878 MATCH match; | |
1879 | |
1880 if (f != m->lastf) // skip duplicates | |
1881 { | |
1882 m->anyf = f; | |
1883 TypeFunction *tf = (TypeFunction *)f->type; | |
1884 match = (MATCH) tf->callMatch(arguments); | |
1885 //printf("1match = %d\n", match); | |
1886 if (match != MATCHnomatch) | |
1887 { | |
1888 if (match > m->last) | |
1889 goto LfIsBetter; | |
1890 | |
1891 if (match < m->last) | |
1892 goto LlastIsBetter; | |
1893 | |
1894 /* See if one of the matches overrides the other. | |
1895 */ | |
1896 if (m->lastf->overrides(f)) | |
1897 goto LlastIsBetter; | |
1898 else if (f->overrides(m->lastf)) | |
1899 goto LfIsBetter; | |
1900 | |
1602
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
1901 #if DMDV2 |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
1902 /* 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
|
1903 * 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
|
1904 * 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
|
1905 * 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
|
1906 */ |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
1907 { |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
1908 MATCH c1 = f->leastAsSpecialized(m->lastf); |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
1909 MATCH c2 = m->lastf->leastAsSpecialized(f); |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
1910 //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
|
1911 if (c1 > c2) |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
1912 goto LfIsBetter; |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
1913 if (c1 < c2) |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
1914 goto LlastIsBetter; |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
1915 } |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
1916 #endif |
1587 | 1917 Lambiguous: |
1918 m->nextf = f; | |
1919 m->count++; | |
1920 return 0; | |
1921 | |
1922 LfIsBetter: | |
1923 m->last = match; | |
1924 m->lastf = f; | |
1925 m->count = 1; | |
1926 return 0; | |
1927 | |
1928 LlastIsBetter: | |
1929 return 0; | |
1930 } | |
1931 } | |
1932 return 0; | |
1933 } | |
1934 | |
1935 | |
1936 void overloadResolveX(Match *m, FuncDeclaration *fstart, | |
1937 Expression *ethis, Expressions *arguments, Module *from) | |
1938 { | |
1939 Param2 p; | |
1940 p.m = m; | |
1941 p.arguments = arguments; | |
1942 overloadApply(from, fstart, &fp2, &p); | |
1943 } | |
1944 | |
1945 | |
1946 FuncDeclaration *FuncDeclaration::overloadResolve(Loc loc, Expression *ethis, Expressions *arguments, Module *from, int flags) | |
1947 { | |
1948 TypeFunction *tf; | |
1949 Match m; | |
1950 | |
1951 #if 0 | |
1952 printf("FuncDeclaration::overloadResolve('%s')\n", toChars()); | |
1953 if (arguments) | |
1954 { int i; | |
1955 | |
1956 for (i = 0; i < arguments->dim; i++) | |
1957 { Expression *arg; | |
1958 | |
1959 arg = (Expression *)arguments->data[i]; | |
1960 assert(arg->type); | |
1961 printf("\t%s: ", arg->toChars()); | |
1962 arg->type->print(); | |
1963 } | |
1964 } | |
1965 #endif | |
1966 | |
1967 memset(&m, 0, sizeof(m)); | |
1968 m.last = MATCHnomatch; | |
1969 overloadResolveX(&m, this, NULL, arguments, from); | |
1970 | |
1971 if (m.count == 1) // exactly one match | |
1972 { | |
1973 return m.lastf; | |
1974 } | |
1975 else | |
1976 { | |
1977 OutBuffer buf; | |
1978 | |
1979 if (arguments) | |
1980 { | |
1981 HdrGenState hgs; | |
1982 | |
1983 argExpTypesToCBuffer(&buf, arguments, &hgs); | |
1984 } | |
1985 | |
1986 if (m.last == MATCHnomatch) | |
1987 { | |
1988 tf = (TypeFunction *)type; | |
1989 | |
1990 //printf("tf = %s, args = %s\n", tf->deco, ((Expression *)arguments->data[0])->type->deco); | |
1991 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
|
1992 Parameter::argsTypesToChars(tf->parameters, tf->varargs), |
1587 | 1993 buf.toChars()); |
1994 return m.anyf; // as long as it's not a FuncAliasDeclaration | |
1995 } | |
1996 else | |
1997 { | |
1998 #if 1 | |
1999 TypeFunction *t1 = (TypeFunction *)m.lastf->type; | |
2000 TypeFunction *t2 = (TypeFunction *)m.nextf->type; | |
2001 | |
2002 error(loc, "called with argument types:\n\t(%s)\nmatches both:\n\t%s%s\nand:\n\t%s%s", | |
2003 buf.toChars(), | |
1607
207a8a438dea
Merge DMD r253: refactor: Argument => Parameter
Leandro Lucarella <llucax@gmail.com>
parents:
1602
diff
changeset
|
2004 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
|
2005 m.nextf->toPrettyChars(), Parameter::argsTypesToChars(t2->parameters, t2->varargs)); |
1587 | 2006 #else |
2007 error(loc, "overloads %s and %s both match argument list for %s", | |
2008 m.lastf->type->toChars(), | |
2009 m.nextf->type->toChars(), | |
2010 m.lastf->toChars()); | |
2011 #endif | |
2012 return m.lastf; | |
2013 } | |
2014 } | |
2015 } | |
2016 | |
1602
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
2017 /************************************* |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
2018 * 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
|
2019 * 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
|
2020 * Returns: |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
2021 * 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
|
2022 * 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
|
2023 */ |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
2024 |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
2025 #if DMDV2 |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
2026 MATCH FuncDeclaration::leastAsSpecialized(FuncDeclaration *g) |
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 #define LOG_LEASTAS 0 |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
2029 |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
2030 #if LOG_LEASTAS |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
2031 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
|
2032 #endif |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
2033 |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
2034 /* 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
|
2035 * 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
|
2036 * as g() is. |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
2037 */ |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
2038 |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
2039 TypeFunction *tf = (TypeFunction *)type; |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
2040 TypeFunction *tg = (TypeFunction *)g->type; |
1607
207a8a438dea
Merge DMD r253: refactor: Argument => Parameter
Leandro Lucarella <llucax@gmail.com>
parents:
1602
diff
changeset
|
2041 size_t nfparams = Parameter::dim(tf->parameters); |
207a8a438dea
Merge DMD r253: refactor: Argument => Parameter
Leandro Lucarella <llucax@gmail.com>
parents:
1602
diff
changeset
|
2042 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
|
2043 MATCH match = MATCHexact; |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
2044 |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
2045 /* 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
|
2046 * 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
|
2047 */ |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
2048 if (needThis() && g->needThis()) |
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 (tf->mod != tg->mod) |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
2051 { |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
2052 if (tg->mod == MODconst) |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
2053 match = MATCHconst; |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
2054 else |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
2055 return MATCHnomatch; |
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 } |
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 /* 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
|
2060 */ |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
2061 Expressions args; |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
2062 args.setDim(nfparams); |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
2063 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
|
2064 { |
1607
207a8a438dea
Merge DMD r253: refactor: Argument => Parameter
Leandro Lucarella <llucax@gmail.com>
parents:
1602
diff
changeset
|
2065 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
|
2066 Expression *e; |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
2067 if (p->storageClass & (STCref | STCout)) |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
2068 { |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
2069 e = new IdentifierExp(0, p->ident); |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
2070 e->type = p->type; |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
2071 } |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
2072 else |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
2073 e = p->type->defaultInit(); |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
2074 args.data[u] = e; |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
2075 } |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
2076 |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
2077 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
|
2078 if (m) |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
2079 { |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
2080 /* 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
|
2081 * non-variadic one. |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
2082 */ |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
2083 if (tf->varargs && !tg->varargs) |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
2084 goto L1; // less specialized |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
2085 |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
2086 #if LOG_LEASTAS |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
2087 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
|
2088 #endif |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
2089 return m; |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
2090 } |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
2091 L1: |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
2092 #if LOG_LEASTAS |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
2093 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
|
2094 #endif |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
2095 return MATCHnomatch; |
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 |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
2098 /******************************************* |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
2099 * 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
|
2100 * 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
|
2101 * sc instantiation scope |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
2102 * loc instantiation location |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
2103 * targsi initial list of template arguments |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
2104 * 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
|
2105 * fargs arguments to function |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
2106 * 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
|
2107 */ |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
2108 |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
2109 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
|
2110 Objects *tiargs, |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
2111 Expression *ethis, |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
2112 Expressions *arguments, |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
2113 int flags) |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
2114 { |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
2115 if (!s) |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
2116 return NULL; // no match |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
2117 FuncDeclaration *f = s->isFuncDeclaration(); |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
2118 if (f) |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
2119 f = f->overloadResolve(loc, ethis, arguments); |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
2120 else |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
2121 { TemplateDeclaration *td = s->isTemplateDeclaration(); |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
2122 assert(td); |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
2123 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
|
2124 } |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
2125 return f; |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
2126 } |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
2127 #endif |
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
2128 |
1587 | 2129 /******************************** |
2130 * Labels are in a separate scope, one per function. | |
2131 */ | |
2132 | |
2133 LabelDsymbol *FuncDeclaration::searchLabel(Identifier *ident) | |
2134 { Dsymbol *s; | |
2135 | |
2136 if (!labtab) | |
2137 labtab = new DsymbolTable(); // guess we need one | |
2138 | |
2139 s = labtab->lookup(ident); | |
2140 if (!s) | |
2141 { | |
2142 s = new LabelDsymbol(ident); | |
2143 labtab->insert(s); | |
2144 } | |
2145 return (LabelDsymbol *)s; | |
2146 } | |
2147 | |
2148 /**************************************** | |
2149 * If non-static member function that has a 'this' pointer, | |
2150 * return the aggregate it is a member of. | |
2151 * Otherwise, return NULL. | |
2152 */ | |
2153 | |
2154 AggregateDeclaration *FuncDeclaration::isThis() | |
2155 { AggregateDeclaration *ad; | |
2156 | |
2157 //printf("+FuncDeclaration::isThis() '%s'\n", toChars()); | |
2158 ad = NULL; | |
2159 if ((storage_class & STCstatic) == 0) | |
2160 { | |
2161 ad = isMember2(); | |
2162 } | |
2163 //printf("-FuncDeclaration::isThis() %p\n", ad); | |
2164 return ad; | |
2165 } | |
2166 | |
2167 AggregateDeclaration *FuncDeclaration::isMember2() | |
2168 { AggregateDeclaration *ad; | |
2169 | |
2170 //printf("+FuncDeclaration::isMember2() '%s'\n", toChars()); | |
2171 ad = NULL; | |
2172 for (Dsymbol *s = this; s; s = s->parent) | |
2173 { | |
2174 //printf("\ts = '%s', parent = '%s', kind = %s\n", s->toChars(), s->parent->toChars(), s->parent->kind()); | |
2175 ad = s->isMember(); | |
2176 if (ad) | |
2177 { //printf("test4\n"); | |
2178 break; | |
2179 } | |
2180 if (!s->parent || | |
2181 (!s->parent->isTemplateInstance())) | |
2182 { //printf("test5\n"); | |
2183 break; | |
2184 } | |
2185 } | |
2186 //printf("-FuncDeclaration::isMember2() %p\n", ad); | |
2187 return ad; | |
2188 } | |
2189 | |
2190 /***************************************** | |
2191 * Determine lexical level difference from 'this' to nested function 'fd'. | |
2192 * Error if this cannot call fd. | |
2193 * Returns: | |
2194 * 0 same level | |
2195 * -1 increase nesting by 1 (fd is nested within 'this') | |
2196 * >0 decrease nesting by number | |
2197 */ | |
2198 | |
2199 int FuncDeclaration::getLevel(Loc loc, FuncDeclaration *fd) | |
2200 { int level; | |
2201 Dsymbol *s; | |
2202 Dsymbol *fdparent; | |
2203 | |
2204 //printf("FuncDeclaration::getLevel(fd = '%s')\n", fd->toChars()); | |
2205 fdparent = fd->toParent2(); | |
2206 if (fdparent == this) | |
2207 return -1; | |
2208 s = this; | |
2209 level = 0; | |
2210 while (fd != s && fdparent != s->toParent2()) | |
2211 { | |
2212 //printf("\ts = '%s'\n", s->toChars()); | |
2213 FuncDeclaration *thisfd = s->isFuncDeclaration(); | |
2214 if (thisfd) | |
2215 { if (!thisfd->isNested() && !thisfd->vthis) | |
2216 goto Lerr; | |
2217 } | |
2218 else | |
2219 { | |
2220 ClassDeclaration *thiscd = s->isClassDeclaration(); | |
2221 if (thiscd) | |
2222 { if (!thiscd->isNested()) | |
2223 goto Lerr; | |
2224 } | |
2225 else | |
2226 goto Lerr; | |
2227 } | |
2228 | |
2229 s = s->toParent2(); | |
2230 assert(s); | |
2231 level++; | |
2232 } | |
2233 return level; | |
2234 | |
2235 Lerr: | |
2236 error(loc, "cannot access frame of function %s", fd->toChars()); | |
2237 return 1; | |
2238 } | |
2239 | |
2240 void FuncDeclaration::appendExp(Expression *e) | |
2241 { Statement *s; | |
2242 | |
2243 s = new ExpStatement(0, e); | |
2244 appendState(s); | |
2245 } | |
2246 | |
2247 void FuncDeclaration::appendState(Statement *s) | |
1602
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
2248 { |
1587 | 2249 if (!fbody) |
2250 { Statements *a; | |
2251 | |
2252 a = new Statements(); | |
2253 fbody = new CompoundStatement(0, a); | |
2254 } | |
1602
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
2255 CompoundStatement *cs = fbody->isCompoundStatement(); |
1587 | 2256 cs->statements->push(s); |
2257 } | |
2258 | |
2259 const char *FuncDeclaration::toPrettyChars() | |
2260 { | |
2261 if (isMain()) | |
2262 return "D main"; | |
2263 else | |
2264 return Dsymbol::toPrettyChars(); | |
2265 } | |
2266 | |
2267 int FuncDeclaration::isMain() | |
2268 { | |
2269 return ident == Id::main && | |
2270 linkage != LINKc && !isMember() && !isNested(); | |
2271 } | |
2272 | |
2273 int FuncDeclaration::isWinMain() | |
2274 { | |
2275 //printf("FuncDeclaration::isWinMain() %s\n", toChars()); | |
2276 #if 0 | |
2277 int x = ident == Id::WinMain && | |
2278 linkage != LINKc && !isMember(); | |
2279 printf("%s\n", x ? "yes" : "no"); | |
2280 return x; | |
2281 #else | |
2282 return ident == Id::WinMain && | |
2283 linkage != LINKc && !isMember(); | |
2284 #endif | |
2285 } | |
2286 | |
2287 int FuncDeclaration::isDllMain() | |
2288 { | |
2289 return ident == Id::DllMain && | |
2290 linkage != LINKc && !isMember(); | |
2291 } | |
2292 | |
2293 int FuncDeclaration::isExport() | |
2294 { | |
2295 return protection == PROTexport; | |
2296 } | |
2297 | |
2298 int FuncDeclaration::isImportedSymbol() | |
2299 { | |
2300 //printf("isImportedSymbol()\n"); | |
2301 //printf("protection = %d\n", protection); | |
2302 return (protection == PROTexport) && !fbody; | |
2303 } | |
2304 | |
2305 // Determine if function goes into virtual function pointer table | |
2306 | |
2307 int FuncDeclaration::isVirtual() | |
2308 { | |
2309 #if 0 | |
2310 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
|
2311 printf("isMember:%p isStatic:%d private:%d ctor:%d !Dlinkage:%d\n", isMember(), isStatic(), protection == PROTprivate, isCtorDeclaration(), linkage != LINKd); |
1587 | 2312 printf("result is %d\n", |
2313 isMember() && | |
2314 !(isStatic() || protection == PROTprivate || protection == PROTpackage) && | |
2315 toParent()->isClassDeclaration()); | |
2316 #endif | |
2317 return isMember() && | |
2318 !(isStatic() || protection == PROTprivate || protection == PROTpackage) && | |
2319 toParent()->isClassDeclaration(); | |
2320 } | |
2321 | |
2322 int FuncDeclaration::isFinal() | |
2323 { | |
2324 ClassDeclaration *cd; | |
2325 #if 0 | |
2326 printf("FuncDeclaration::isFinal(%s)\n", toChars()); | |
2327 printf("%p %d %d %d %d\n", isMember(), isStatic(), protection == PROTprivate, isCtorDeclaration(), linkage != LINKd); | |
2328 printf("result is %d\n", | |
2329 isMember() && | |
2330 !(isStatic() || protection == PROTprivate || protection == PROTpackage) && | |
2331 (cd = toParent()->isClassDeclaration()) != NULL && | |
2332 cd->storage_class & STCfinal); | |
2333 #endif | |
2334 return isMember() && | |
2335 (Declaration::isFinal() || | |
2336 ((cd = toParent()->isClassDeclaration()) != NULL && cd->storage_class & STCfinal)); | |
2337 } | |
2338 | |
2339 int FuncDeclaration::isAbstract() | |
2340 { | |
2341 return storage_class & STCabstract; | |
2342 } | |
2343 | |
2344 int FuncDeclaration::isCodeseg() | |
2345 { | |
2346 return TRUE; // functions are always in the code segment | |
2347 } | |
2348 | |
2349 // Determine if function needs | |
2350 // a static frame pointer to its lexically enclosing function | |
2351 | |
2352 int FuncDeclaration::isNested() | |
2353 { | |
2354 //if (!toParent()) | |
2355 //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
|
2356 //printf("\ttoParent2() = '%s'\n", toParent2()->toChars()); |
1587 | 2357 return ((storage_class & STCstatic) == 0) && toParent2() && |
2358 (toParent2()->isFuncDeclaration() != NULL); | |
2359 } | |
2360 | |
2361 int FuncDeclaration::needThis() | |
2362 { | |
2363 //printf("FuncDeclaration::needThis() '%s'\n", toChars()); | |
2364 int i = isThis() != NULL; | |
2365 //printf("\t%d\n", i); | |
2366 if (!i && isFuncAliasDeclaration()) | |
2367 i = ((FuncAliasDeclaration *)this)->funcalias->needThis(); | |
2368 return i; | |
2369 } | |
2370 | |
2371 int FuncDeclaration::addPreInvariant() | |
2372 { | |
2373 AggregateDeclaration *ad = isThis(); | |
2374 return (ad && | |
2375 //ad->isClassDeclaration() && | |
2376 global.params.useInvariants && | |
2377 (protection == PROTpublic || protection == PROTexport) && | |
2378 !naked); | |
2379 } | |
2380 | |
2381 int FuncDeclaration::addPostInvariant() | |
2382 { | |
2383 AggregateDeclaration *ad = isThis(); | |
2384 return (ad && | |
2385 ad->inv && | |
2386 //ad->isClassDeclaration() && | |
2387 global.params.useInvariants && | |
2388 (protection == PROTpublic || protection == PROTexport) && | |
2389 !naked); | |
2390 } | |
2391 | |
2392 /********************************** | |
2393 * Generate a FuncDeclaration for a runtime library function. | |
2394 */ | |
2395 | |
2396 // | |
2397 // LDC: Adjusted to give argument info to the runtime function decl. | |
2398 // | |
2399 | |
1607
207a8a438dea
Merge DMD r253: refactor: Argument => Parameter
Leandro Lucarella <llucax@gmail.com>
parents:
1602
diff
changeset
|
2400 FuncDeclaration *FuncDeclaration::genCfunc(Parameters *args, Type *treturn, const char *name) |
1587 | 2401 { |
2402 return genCfunc(args, treturn, Lexer::idPool(name)); | |
2403 } | |
2404 | |
1607
207a8a438dea
Merge DMD r253: refactor: Argument => Parameter
Leandro Lucarella <llucax@gmail.com>
parents:
1602
diff
changeset
|
2405 FuncDeclaration *FuncDeclaration::genCfunc(Parameters *args, Type *treturn, Identifier *id) |
1587 | 2406 { |
2407 FuncDeclaration *fd; | |
2408 TypeFunction *tf; | |
2409 Dsymbol *s; | |
2410 static DsymbolTable *st = NULL; | |
2411 | |
2412 //printf("genCfunc(name = '%s')\n", id->toChars()); | |
2413 //printf("treturn\n\t"); treturn->print(); | |
2414 | |
2415 // See if already in table | |
2416 if (!st) | |
2417 st = new DsymbolTable(); | |
2418 s = st->lookup(id); | |
2419 if (s) | |
2420 { | |
2421 fd = s->isFuncDeclaration(); | |
2422 assert(fd); | |
2423 assert(fd->type->nextOf()->equals(treturn)); | |
2424 } | |
2425 else | |
2426 { | |
2427 tf = new TypeFunction(args, treturn, 0, LINKc); | |
2428 fd = new FuncDeclaration(0, 0, id, STCstatic, tf); | |
2429 fd->protection = PROTpublic; | |
2430 fd->linkage = LINKc; | |
2431 | |
2432 st->insert(fd); | |
2433 } | |
2434 return fd; | |
2435 } | |
2436 | |
2437 const char *FuncDeclaration::kind() | |
2438 { | |
2439 return "function"; | |
2440 } | |
2441 | |
2442 /******************************* | |
2443 * Look at all the variables in this function that are referenced | |
2444 * by nested functions, and determine if a closure needs to be | |
2445 * created for them. | |
2446 */ | |
2447 | |
2448 #if DMDV2 | |
2449 int FuncDeclaration::needsClosure() | |
2450 { | |
2451 /* Need a closure for all the closureVars[] if any of the | |
2452 * closureVars[] are accessed by a | |
2453 * function that escapes the scope of this function. | |
2454 * We take the conservative approach and decide that any function that: | |
2455 * 1) is a virtual function | |
2456 * 2) has its address taken | |
2457 * 3) has a parent that escapes | |
2458 * | |
2459 * Note that since a non-virtual function can be called by | |
2460 * a virtual one, if that non-virtual function accesses a closure | |
2461 * var, the closure still has to be taken. Hence, we check for isThis() | |
2462 * instead of isVirtual(). (thanks to David Friedman) | |
2463 */ | |
2464 | |
2465 //printf("FuncDeclaration::needsClosure() %s\n", toChars()); | |
2466 for (int i = 0; i < closureVars.dim; i++) | |
2467 { VarDeclaration *v = (VarDeclaration *)closureVars.data[i]; | |
2468 assert(v->isVarDeclaration()); | |
2469 //printf("\tv = %s\n", v->toChars()); | |
2470 | |
2471 for (int j = 0; j < v->nestedrefs.dim; j++) | |
2472 { FuncDeclaration *f = (FuncDeclaration *)v->nestedrefs.data[j]; | |
2473 assert(f != this); | |
2474 | |
2475 //printf("\t\tf = %s, %d, %d\n", f->toChars(), f->isVirtual(), f->tookAddressOf); | |
2476 if (f->isThis() || f->tookAddressOf) | |
2477 goto Lyes; // assume f escapes this function's scope | |
2478 | |
2479 // Look to see if any parents of f that are below this escape | |
2480 for (Dsymbol *s = f->parent; s && s != this; s = s->parent) | |
2481 { | |
2482 f = s->isFuncDeclaration(); | |
2483 if (f && (f->isThis() || f->tookAddressOf)) | |
2484 goto Lyes; | |
2485 } | |
2486 } | |
2487 } | |
2488 return 0; | |
2489 | |
2490 Lyes: | |
2491 //printf("\tneeds closure\n"); | |
2492 return 1; | |
2493 } | |
2494 #endif | |
2495 | |
1617
6820110de311
Merge DMD r301: a little refactor and harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1608
diff
changeset
|
2496 /********************************************* |
6820110de311
Merge DMD r301: a little refactor and harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1608
diff
changeset
|
2497 * Return the function's parameter list, and whether |
6820110de311
Merge DMD r301: a little refactor and harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1608
diff
changeset
|
2498 * it is variadic or not. |
6820110de311
Merge DMD r301: a little refactor and harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1608
diff
changeset
|
2499 */ |
6820110de311
Merge DMD r301: a little refactor and harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1608
diff
changeset
|
2500 |
6820110de311
Merge DMD r301: a little refactor and harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1608
diff
changeset
|
2501 Parameters *FuncDeclaration::getParameters(int *pvarargs) |
6820110de311
Merge DMD r301: a little refactor and harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1608
diff
changeset
|
2502 { Parameters *fparameters; |
6820110de311
Merge DMD r301: a little refactor and harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1608
diff
changeset
|
2503 int fvarargs; |
6820110de311
Merge DMD r301: a little refactor and harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1608
diff
changeset
|
2504 |
6820110de311
Merge DMD r301: a little refactor and harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1608
diff
changeset
|
2505 if (type) |
6820110de311
Merge DMD r301: a little refactor and harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1608
diff
changeset
|
2506 { |
6820110de311
Merge DMD r301: a little refactor and harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1608
diff
changeset
|
2507 assert(type->ty == Tfunction); |
6820110de311
Merge DMD r301: a little refactor and harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1608
diff
changeset
|
2508 TypeFunction *fdtype = (TypeFunction *)type; |
6820110de311
Merge DMD r301: a little refactor and harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1608
diff
changeset
|
2509 fparameters = fdtype->parameters; |
6820110de311
Merge DMD r301: a little refactor and harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1608
diff
changeset
|
2510 fvarargs = fdtype->varargs; |
6820110de311
Merge DMD r301: a little refactor and harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1608
diff
changeset
|
2511 } |
6820110de311
Merge DMD r301: a little refactor and harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1608
diff
changeset
|
2512 else // Constructors don't have type's |
6820110de311
Merge DMD r301: a little refactor and harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1608
diff
changeset
|
2513 { CtorDeclaration *fctor = isCtorDeclaration(); |
6820110de311
Merge DMD r301: a little refactor and harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1608
diff
changeset
|
2514 assert(fctor); |
6820110de311
Merge DMD r301: a little refactor and harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1608
diff
changeset
|
2515 fparameters = fctor->arguments; |
6820110de311
Merge DMD r301: a little refactor and harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1608
diff
changeset
|
2516 fvarargs = fctor->varargs; |
6820110de311
Merge DMD r301: a little refactor and harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1608
diff
changeset
|
2517 } |
6820110de311
Merge DMD r301: a little refactor and harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1608
diff
changeset
|
2518 if (pvarargs) |
6820110de311
Merge DMD r301: a little refactor and harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1608
diff
changeset
|
2519 *pvarargs = fvarargs; |
6820110de311
Merge DMD r301: a little refactor and harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1608
diff
changeset
|
2520 return fparameters; |
6820110de311
Merge DMD r301: a little refactor and harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1608
diff
changeset
|
2521 } |
6820110de311
Merge DMD r301: a little refactor and harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1608
diff
changeset
|
2522 |
6820110de311
Merge DMD r301: a little refactor and harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1608
diff
changeset
|
2523 |
1587 | 2524 /****************************** FuncAliasDeclaration ************************/ |
2525 | |
2526 // Used as a way to import a set of functions from another scope into this one. | |
2527 | |
2528 FuncAliasDeclaration::FuncAliasDeclaration(FuncDeclaration *funcalias) | |
2529 : FuncDeclaration(funcalias->loc, funcalias->endloc, funcalias->ident, | |
2530 (enum STC)funcalias->storage_class, funcalias->type) | |
2531 { | |
2532 assert(funcalias != this); | |
2533 this->funcalias = funcalias; | |
2534 importprot = PROTundefined; | |
2535 } | |
2536 | |
2537 const char *FuncAliasDeclaration::kind() | |
2538 { | |
2539 return "function alias"; | |
2540 } | |
2541 | |
2542 | |
2543 /****************************** FuncLiteralDeclaration ************************/ | |
2544 | |
2545 FuncLiteralDeclaration::FuncLiteralDeclaration(Loc loc, Loc endloc, Type *type, | |
2546 enum TOK tok, ForeachStatement *fes) | |
2547 : FuncDeclaration(loc, endloc, NULL, STCundefined, type) | |
2548 { | |
2549 const char *id; | |
2550 | |
2551 if (fes) | |
2552 id = "__foreachbody"; | |
2553 else if (tok == TOKdelegate) | |
2554 id = "__dgliteral"; | |
2555 else | |
2556 id = "__funcliteral"; | |
2557 this->ident = Identifier::generateId(id); | |
2558 this->tok = tok; | |
2559 this->fes = fes; | |
2560 //printf("FuncLiteralDeclaration() id = '%s', type = '%s'\n", this->ident->toChars(), type->toChars()); | |
2561 } | |
2562 | |
2563 Dsymbol *FuncLiteralDeclaration::syntaxCopy(Dsymbol *s) | |
2564 { | |
2565 FuncLiteralDeclaration *f; | |
2566 | |
2567 //printf("FuncLiteralDeclaration::syntaxCopy('%s')\n", toChars()); | |
2568 if (s) | |
2569 f = (FuncLiteralDeclaration *)s; | |
2570 else | |
2571 f = new FuncLiteralDeclaration(loc, endloc, type->syntaxCopy(), tok, fes); | |
2572 FuncDeclaration::syntaxCopy(f); | |
2573 return f; | |
2574 } | |
2575 | |
2576 int FuncLiteralDeclaration::isNested() | |
2577 { | |
2578 //printf("FuncLiteralDeclaration::isNested() '%s'\n", toChars()); | |
2579 return (tok == TOKdelegate); | |
2580 } | |
2581 | |
2582 int FuncLiteralDeclaration::isVirtual() | |
2583 { | |
2584 return FALSE; | |
2585 } | |
2586 | |
2587 const char *FuncLiteralDeclaration::kind() | |
2588 { | |
2589 // GCC requires the (char*) casts | |
2590 return (tok == TOKdelegate) ? (char*)"delegate" : (char*)"function"; | |
2591 } | |
2592 | |
2593 void FuncLiteralDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2594 { | |
2595 static Identifier *idfunc; | |
2596 static Identifier *iddel; | |
2597 | |
2598 if (!idfunc) | |
2599 idfunc = new Identifier("function", 0); | |
2600 if (!iddel) | |
2601 iddel = new Identifier("delegate", 0); | |
2602 | |
2603 type->toCBuffer(buf, ((tok == TOKdelegate) ? iddel : idfunc), hgs); | |
2604 bodyToCBuffer(buf, hgs); | |
2605 } | |
2606 | |
2607 | |
2608 /********************************* CtorDeclaration ****************************/ | |
2609 | |
1607
207a8a438dea
Merge DMD r253: refactor: Argument => Parameter
Leandro Lucarella <llucax@gmail.com>
parents:
1602
diff
changeset
|
2610 CtorDeclaration::CtorDeclaration(Loc loc, Loc endloc, Parameters *arguments, int varargs) |
1587 | 2611 : FuncDeclaration(loc, endloc, Id::ctor, STCundefined, NULL) |
2612 { | |
2613 this->arguments = arguments; | |
2614 this->varargs = varargs; | |
2615 //printf("CtorDeclaration(loc = %s) %s\n", loc.toChars(), toChars()); | |
2616 } | |
2617 | |
2618 Dsymbol *CtorDeclaration::syntaxCopy(Dsymbol *s) | |
2619 { | |
2620 CtorDeclaration *f; | |
2621 | |
2622 f = new CtorDeclaration(loc, endloc, NULL, varargs); | |
2623 | |
2624 f->outId = outId; | |
2625 f->frequire = frequire ? frequire->syntaxCopy() : NULL; | |
2626 f->fensure = fensure ? fensure->syntaxCopy() : NULL; | |
2627 f->fbody = fbody ? fbody->syntaxCopy() : NULL; | |
2628 assert(!fthrows); // deprecated | |
2629 | |
1607
207a8a438dea
Merge DMD r253: refactor: Argument => Parameter
Leandro Lucarella <llucax@gmail.com>
parents:
1602
diff
changeset
|
2630 f->arguments = Parameter::arraySyntaxCopy(arguments); |
1587 | 2631 return f; |
2632 } | |
2633 | |
2634 | |
2635 void CtorDeclaration::semantic(Scope *sc) | |
2636 { | |
2637 ClassDeclaration *cd; | |
2638 Type *tret; | |
2639 | |
2640 //printf("CtorDeclaration::semantic()\n"); | |
2641 if (type) | |
2642 return; | |
2643 | |
2644 sc = sc->push(); | |
2645 sc->stc &= ~STCstatic; // not a static constructor | |
2646 | |
2647 parent = sc->parent; | |
2648 Dsymbol *parent = toParent(); | |
2649 cd = parent->isClassDeclaration(); | |
2650 if (!cd) | |
2651 { | |
2652 error("constructors are only for class definitions"); | |
2653 fatal(); | |
2654 tret = Type::tvoid; | |
2655 } | |
2656 else | |
2657 tret = cd->type; //->referenceTo(); | |
2658 type = new TypeFunction(arguments, tret, varargs, LINKd); | |
2659 #if STRUCTTHISREF | |
2660 if (ad && ad->isStructDeclaration()) | |
2661 ((TypeFunction *)type)->isref = 1; | |
2662 #endif | |
2663 if (!originalType) | |
2664 originalType = type; | |
2665 | |
2666 sc->flags |= SCOPEctor; | |
2667 type = type->semantic(loc, sc); | |
2668 sc->flags &= ~SCOPEctor; | |
2669 | |
2670 // Append: | |
2671 // return this; | |
2672 // to the function body | |
2673 if (fbody) | |
2674 { | |
2675 Expression *e = new ThisExp(loc); | |
2676 Statement *s = new ReturnStatement(loc, e); | |
2677 fbody = new CompoundStatement(loc, fbody, s); | |
2678 } | |
2679 | |
2680 FuncDeclaration::semantic(sc); | |
2681 | |
2682 sc->pop(); | |
2683 | |
2684 // See if it's the default constructor | |
1607
207a8a438dea
Merge DMD r253: refactor: Argument => Parameter
Leandro Lucarella <llucax@gmail.com>
parents:
1602
diff
changeset
|
2685 if (cd && varargs == 0 && Parameter::dim(arguments) == 0) |
1587 | 2686 cd->defaultCtor = this; |
2687 } | |
2688 | |
2689 const char *CtorDeclaration::kind() | |
2690 { | |
2691 return "constructor"; | |
2692 } | |
2693 | |
2694 char *CtorDeclaration::toChars() | |
2695 { | |
2696 return (char *)"this"; | |
2697 } | |
2698 | |
2699 int CtorDeclaration::isVirtual() | |
2700 { | |
2701 return FALSE; | |
2702 } | |
2703 | |
2704 int CtorDeclaration::addPreInvariant() | |
2705 { | |
2706 return FALSE; | |
2707 } | |
2708 | |
2709 int CtorDeclaration::addPostInvariant() | |
2710 { | |
2711 return (isThis() && vthis && global.params.useInvariants); | |
2712 } | |
2713 | |
2714 | |
2715 void CtorDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2716 { | |
2717 buf->writestring("this"); | |
1607
207a8a438dea
Merge DMD r253: refactor: Argument => Parameter
Leandro Lucarella <llucax@gmail.com>
parents:
1602
diff
changeset
|
2718 Parameter::argsToCBuffer(buf, hgs, arguments, varargs); |
1587 | 2719 bodyToCBuffer(buf, hgs); |
2720 } | |
2721 | |
2722 /********************************* PostBlitDeclaration ****************************/ | |
2723 | |
2724 #if DMDV2 | |
2725 PostBlitDeclaration::PostBlitDeclaration(Loc loc, Loc endloc) | |
2726 : FuncDeclaration(loc, endloc, Id::_postblit, STCundefined, NULL) | |
2727 { | |
2728 } | |
2729 | |
2730 PostBlitDeclaration::PostBlitDeclaration(Loc loc, Loc endloc, Identifier *id) | |
2731 : FuncDeclaration(loc, endloc, id, STCundefined, NULL) | |
2732 { | |
2733 } | |
2734 | |
2735 Dsymbol *PostBlitDeclaration::syntaxCopy(Dsymbol *s) | |
2736 { | |
2737 assert(!s); | |
2738 PostBlitDeclaration *dd = new PostBlitDeclaration(loc, endloc, ident); | |
2739 return FuncDeclaration::syntaxCopy(dd); | |
2740 } | |
2741 | |
2742 | |
2743 void PostBlitDeclaration::semantic(Scope *sc) | |
2744 { | |
2745 //printf("PostBlitDeclaration::semantic() %s\n", toChars()); | |
2746 //printf("ident: %s, %s, %p, %p\n", ident->toChars(), Id::dtor->toChars(), ident, Id::dtor); | |
2747 parent = sc->parent; | |
2748 Dsymbol *parent = toParent(); | |
2749 StructDeclaration *ad = parent->isStructDeclaration(); | |
2750 if (!ad) | |
2751 { | |
2752 error("post blits are only for struct/union definitions, not %s %s", parent->kind(), parent->toChars()); | |
2753 } | |
2754 else if (ident == Id::_postblit) | |
2755 ad->postblits.push(this); | |
2756 type = new TypeFunction(NULL, Type::tvoid, FALSE, LINKd); | |
2757 | |
2758 sc = sc->push(); | |
2759 sc->stc &= ~STCstatic; // not static | |
2760 sc->linkage = LINKd; | |
2761 | |
2762 FuncDeclaration::semantic(sc); | |
2763 | |
2764 sc->pop(); | |
2765 } | |
2766 | |
2767 int PostBlitDeclaration::overloadInsert(Dsymbol *s) | |
2768 { | |
2769 return FALSE; // cannot overload postblits | |
2770 } | |
2771 | |
2772 int PostBlitDeclaration::addPreInvariant() | |
2773 { | |
2774 return FALSE; | |
2775 } | |
2776 | |
2777 int PostBlitDeclaration::addPostInvariant() | |
2778 { | |
2779 return (isThis() && vthis && global.params.useInvariants); | |
2780 } | |
2781 | |
2782 int PostBlitDeclaration::isVirtual() | |
2783 { | |
2784 return FALSE; | |
2785 } | |
2786 | |
2787 void PostBlitDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2788 { | |
2789 if (hgs->hdrgen) | |
2790 return; | |
2791 buf->writestring("=this()"); | |
2792 bodyToCBuffer(buf, hgs); | |
2793 } | |
2794 #endif | |
2795 | |
2796 /********************************* DtorDeclaration ****************************/ | |
2797 | |
2798 DtorDeclaration::DtorDeclaration(Loc loc, Loc endloc) | |
2799 : FuncDeclaration(loc, endloc, Id::dtor, STCundefined, NULL) | |
2800 { | |
2801 } | |
2802 | |
2803 DtorDeclaration::DtorDeclaration(Loc loc, Loc endloc, Identifier *id) | |
2804 : FuncDeclaration(loc, endloc, id, STCundefined, NULL) | |
2805 { | |
2806 } | |
2807 | |
2808 Dsymbol *DtorDeclaration::syntaxCopy(Dsymbol *s) | |
2809 { | |
2810 assert(!s); | |
2811 DtorDeclaration *dd = new DtorDeclaration(loc, endloc, ident); | |
2812 return FuncDeclaration::syntaxCopy(dd); | |
2813 } | |
2814 | |
2815 | |
2816 void DtorDeclaration::semantic(Scope *sc) | |
2817 { | |
2818 //printf("DtorDeclaration::semantic() %s\n", toChars()); | |
2819 //printf("ident: %s, %s, %p, %p\n", ident->toChars(), Id::dtor->toChars(), ident, Id::dtor); | |
2820 parent = sc->parent; | |
2821 Dsymbol *parent = toParent(); | |
2822 ClassDeclaration *cd = parent->isClassDeclaration(); | |
2823 if (!cd) | |
2824 { | |
2825 error("destructors are only for class/struct/union definitions, not %s %s", parent->kind(), parent->toChars()); | |
2826 fatal(); | |
2827 } | |
2828 else | |
2829 cd->dtors.push(this); | |
2830 type = new TypeFunction(NULL, Type::tvoid, FALSE, LINKd); | |
2831 | |
2832 sc = sc->push(); | |
2833 sc->stc &= ~STCstatic; // not a static destructor | |
2834 sc->linkage = LINKd; | |
2835 | |
2836 FuncDeclaration::semantic(sc); | |
2837 | |
2838 sc->pop(); | |
2839 } | |
2840 | |
2841 int DtorDeclaration::overloadInsert(Dsymbol *s) | |
2842 { | |
2843 return FALSE; // cannot overload destructors | |
2844 } | |
2845 | |
2846 int DtorDeclaration::addPreInvariant() | |
2847 { | |
2848 return (isThis() && vthis && global.params.useInvariants); | |
2849 } | |
2850 | |
2851 int DtorDeclaration::addPostInvariant() | |
2852 { | |
2853 return FALSE; | |
2854 } | |
2855 | |
2856 const char *DtorDeclaration::kind() | |
2857 { | |
2858 return "destructor"; | |
2859 } | |
2860 | |
2861 char *DtorDeclaration::toChars() | |
2862 { | |
2863 return (char *)"~this"; | |
2864 } | |
2865 | |
2866 int DtorDeclaration::isVirtual() | |
2867 { | |
2868 /* This should be FALSE so that dtor's don't get put into the vtbl[], | |
2869 * but doing so will require recompiling everything. | |
2870 */ | |
2871 #if BREAKABI | |
2872 return FALSE; | |
2873 #else | |
2874 return FuncDeclaration::isVirtual(); | |
2875 #endif | |
2876 } | |
2877 | |
2878 void DtorDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2879 { | |
2880 if (hgs->hdrgen) | |
2881 return; | |
2882 buf->writestring("~this()"); | |
2883 bodyToCBuffer(buf, hgs); | |
2884 } | |
2885 | |
2886 /********************************* StaticCtorDeclaration ****************************/ | |
2887 | |
2888 StaticCtorDeclaration::StaticCtorDeclaration(Loc loc, Loc endloc) | |
2889 : FuncDeclaration(loc, endloc, | |
2890 Identifier::generateId("_staticCtor"), STCstatic, NULL) | |
2891 { | |
2892 } | |
2893 | |
2894 Dsymbol *StaticCtorDeclaration::syntaxCopy(Dsymbol *s) | |
2895 { | |
2896 StaticCtorDeclaration *scd; | |
2897 | |
2898 assert(!s); | |
2899 scd = new StaticCtorDeclaration(loc, endloc); | |
2900 return FuncDeclaration::syntaxCopy(scd); | |
2901 } | |
2902 | |
2903 | |
2904 void StaticCtorDeclaration::semantic(Scope *sc) | |
2905 { | |
2906 //printf("StaticCtorDeclaration::semantic()\n"); | |
2907 | |
2908 type = new TypeFunction(NULL, Type::tvoid, FALSE, LINKd); | |
2909 | |
2910 /* If the static ctor appears within a template instantiation, | |
2911 * it could get called multiple times by the module constructors | |
2912 * for different modules. Thus, protect it with a gate. | |
2913 */ | |
2914 if (inTemplateInstance()) | |
2915 { | |
2916 /* Add this prefix to the function: | |
2917 * static int gate; | |
2918 * if (++gate != 1) return; | |
2919 * Note that this is not thread safe; should not have threads | |
2920 * during static construction. | |
2921 */ | |
2922 Identifier *id = Lexer::idPool("__gate"); | |
2923 VarDeclaration *v = new VarDeclaration(0, Type::tint32, id, NULL); | |
2924 v->storage_class = STCstatic; | |
2925 Statements *sa = new Statements(); | |
2926 Statement *s = new DeclarationStatement(0, v); | |
2927 sa->push(s); | |
2928 Expression *e = new IdentifierExp(0, id); | |
2929 e = new AddAssignExp(0, e, new IntegerExp(1)); | |
2930 e = new EqualExp(TOKnotequal, 0, e, new IntegerExp(1)); | |
2931 s = new IfStatement(0, NULL, e, new ReturnStatement(0, NULL), NULL); | |
2932 sa->push(s); | |
2933 if (fbody) | |
2934 sa->push(fbody); | |
2935 fbody = new CompoundStatement(0, sa); | |
2936 } | |
2937 | |
2938 FuncDeclaration::semantic(sc); | |
2939 | |
2940 // We're going to need ModuleInfo | |
2941 Module *m = getModule(); | |
2942 if (!m) | |
2943 m = sc->module; | |
2944 if (m) | |
2945 { m->needmoduleinfo = 1; | |
2946 #ifdef IN_GCC | |
2947 m->strictlyneedmoduleinfo = 1; | |
2948 #endif | |
2949 } | |
2950 } | |
2951 | |
2952 AggregateDeclaration *StaticCtorDeclaration::isThis() | |
2953 { | |
2954 return NULL; | |
2955 } | |
2956 | |
2957 int StaticCtorDeclaration::isStaticConstructor() | |
2958 { | |
2959 return TRUE; | |
2960 } | |
2961 | |
2962 int StaticCtorDeclaration::isVirtual() | |
2963 { | |
2964 return FALSE; | |
2965 } | |
2966 | |
2967 int StaticCtorDeclaration::addPreInvariant() | |
2968 { | |
2969 return FALSE; | |
2970 } | |
2971 | |
2972 int StaticCtorDeclaration::addPostInvariant() | |
2973 { | |
2974 return FALSE; | |
2975 } | |
2976 | |
2977 void StaticCtorDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2978 { | |
2979 if (hgs->hdrgen) | |
2980 { buf->writestring("static this();\n"); | |
2981 return; | |
2982 } | |
2983 buf->writestring("static this()"); | |
2984 bodyToCBuffer(buf, hgs); | |
2985 } | |
2986 | |
2987 /********************************* StaticDtorDeclaration ****************************/ | |
2988 | |
2989 StaticDtorDeclaration::StaticDtorDeclaration(Loc loc, Loc endloc) | |
2990 : FuncDeclaration(loc, endloc, | |
2991 Identifier::generateId("_staticDtor"), STCstatic, NULL) | |
2992 { | |
2993 vgate = NULL; | |
2994 } | |
2995 | |
2996 Dsymbol *StaticDtorDeclaration::syntaxCopy(Dsymbol *s) | |
2997 { | |
2998 StaticDtorDeclaration *sdd; | |
2999 | |
3000 assert(!s); | |
3001 sdd = new StaticDtorDeclaration(loc, endloc); | |
3002 return FuncDeclaration::syntaxCopy(sdd); | |
3003 } | |
3004 | |
3005 | |
3006 void StaticDtorDeclaration::semantic(Scope *sc) | |
3007 { | |
3008 ClassDeclaration *cd; | |
3009 Type *tret; | |
3010 | |
3011 cd = sc->scopesym->isClassDeclaration(); | |
3012 if (!cd) | |
3013 { | |
3014 } | |
3015 type = new TypeFunction(NULL, Type::tvoid, FALSE, LINKd); | |
3016 | |
3017 /* If the static ctor appears within a template instantiation, | |
3018 * it could get called multiple times by the module constructors | |
3019 * for different modules. Thus, protect it with a gate. | |
3020 */ | |
3021 if (inTemplateInstance()) | |
3022 { | |
3023 /* Add this prefix to the function: | |
3024 * static int gate; | |
3025 * if (--gate != 0) return; | |
3026 * Increment gate during constructor execution. | |
3027 * Note that this is not thread safe; should not have threads | |
3028 * during static destruction. | |
3029 */ | |
3030 Identifier *id = Lexer::idPool("__gate"); | |
3031 VarDeclaration *v = new VarDeclaration(0, Type::tint32, id, NULL); | |
3032 v->storage_class = STCstatic; | |
3033 Statements *sa = new Statements(); | |
3034 Statement *s = new DeclarationStatement(0, v); | |
3035 sa->push(s); | |
3036 Expression *e = new IdentifierExp(0, id); | |
3037 e = new AddAssignExp(0, e, new IntegerExp((uint64_t)-1)); | |
3038 e = new EqualExp(TOKnotequal, 0, e, new IntegerExp(0)); | |
3039 s = new IfStatement(0, NULL, e, new ReturnStatement(0, NULL), NULL); | |
3040 sa->push(s); | |
3041 if (fbody) | |
3042 sa->push(fbody); | |
3043 fbody = new CompoundStatement(0, sa); | |
3044 vgate = v; | |
3045 } | |
3046 | |
3047 FuncDeclaration::semantic(sc); | |
3048 | |
3049 // We're going to need ModuleInfo | |
3050 Module *m = getModule(); | |
3051 if (!m) | |
3052 m = sc->module; | |
3053 if (m) | |
3054 { m->needmoduleinfo = 1; | |
1602
a413ae7329bf
Merge DMD r243: some harmonization with D2 dmd
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
3055 //printf("module2 %s needs moduleinfo\n", m->toChars()); |
1587 | 3056 #ifdef IN_GCC |
3057 m->strictlyneedmoduleinfo = 1; | |
3058 #endif | |
3059 } | |
3060 } | |
3061 | |
3062 AggregateDeclaration *StaticDtorDeclaration::isThis() | |
3063 { | |
3064 return NULL; | |
3065 } | |
3066 | |
3067 int StaticDtorDeclaration::isStaticDestructor() | |
3068 { | |
3069 return TRUE; | |
3070 } | |
3071 | |
3072 int StaticDtorDeclaration::isVirtual() | |
3073 { | |
3074 return FALSE; | |
3075 } | |
3076 | |
3077 int StaticDtorDeclaration::addPreInvariant() | |
3078 { | |
3079 return FALSE; | |
3080 } | |
3081 | |
3082 int StaticDtorDeclaration::addPostInvariant() | |
3083 { | |
3084 return FALSE; | |
3085 } | |
3086 | |
3087 void StaticDtorDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
3088 { | |
3089 if (hgs->hdrgen) | |
3090 return; | |
3091 buf->writestring("static ~this()"); | |
3092 bodyToCBuffer(buf, hgs); | |
3093 } | |
3094 | |
3095 /********************************* InvariantDeclaration ****************************/ | |
3096 | |
3097 InvariantDeclaration::InvariantDeclaration(Loc loc, Loc endloc) | |
3098 : FuncDeclaration(loc, endloc, Id::classInvariant, STCundefined, NULL) | |
3099 { | |
3100 } | |
3101 | |
3102 Dsymbol *InvariantDeclaration::syntaxCopy(Dsymbol *s) | |
3103 { | |
3104 InvariantDeclaration *id; | |
3105 | |
3106 assert(!s); | |
3107 id = new InvariantDeclaration(loc, endloc); | |
3108 FuncDeclaration::syntaxCopy(id); | |
3109 return id; | |
3110 } | |
3111 | |
3112 | |
3113 void InvariantDeclaration::semantic(Scope *sc) | |
3114 { | |
3115 AggregateDeclaration *ad; | |
3116 Type *tret; | |
3117 | |
3118 parent = sc->parent; | |
3119 Dsymbol *parent = toParent(); | |
3120 ad = parent->isAggregateDeclaration(); | |
3121 if (!ad) | |
3122 { | |
3123 error("invariants are only for struct/union/class definitions"); | |
3124 return; | |
3125 } | |
3126 else if (ad->inv && ad->inv != this) | |
3127 { | |
3128 error("more than one invariant for %s", ad->toChars()); | |
3129 } | |
3130 ad->inv = this; | |
3131 type = new TypeFunction(NULL, Type::tvoid, FALSE, LINKd); | |
3132 | |
3133 sc = sc->push(); | |
3134 sc->stc &= ~STCstatic; // not a static invariant | |
3135 sc->incontract++; | |
3136 sc->linkage = LINKd; | |
3137 | |
3138 FuncDeclaration::semantic(sc); | |
3139 | |
3140 sc->pop(); | |
3141 } | |
3142 | |
3143 int InvariantDeclaration::isVirtual() | |
3144 { | |
3145 return FALSE; | |
3146 } | |
3147 | |
3148 int InvariantDeclaration::addPreInvariant() | |
3149 { | |
3150 return FALSE; | |
3151 } | |
3152 | |
3153 int InvariantDeclaration::addPostInvariant() | |
3154 { | |
3155 return FALSE; | |
3156 } | |
3157 | |
3158 void InvariantDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
3159 { | |
3160 if (hgs->hdrgen) | |
3161 return; | |
3162 buf->writestring("invariant"); | |
3163 bodyToCBuffer(buf, hgs); | |
3164 } | |
3165 | |
3166 | |
3167 /********************************* UnitTestDeclaration ****************************/ | |
3168 | |
3169 /******************************* | |
3170 * Generate unique unittest function Id so we can have multiple | |
3171 * instances per module. | |
3172 */ | |
3173 | |
3174 static Identifier *unitTestId() | |
3175 { | |
3176 return Lexer::uniqueId("__unittest"); | |
3177 } | |
3178 | |
3179 UnitTestDeclaration::UnitTestDeclaration(Loc loc, Loc endloc) | |
3180 : FuncDeclaration(loc, endloc, unitTestId(), STCundefined, NULL) | |
3181 { | |
3182 } | |
3183 | |
3184 Dsymbol *UnitTestDeclaration::syntaxCopy(Dsymbol *s) | |
3185 { | |
3186 UnitTestDeclaration *utd; | |
3187 | |
3188 assert(!s); | |
3189 utd = new UnitTestDeclaration(loc, endloc); | |
3190 return FuncDeclaration::syntaxCopy(utd); | |
3191 } | |
3192 | |
3193 | |
3194 void UnitTestDeclaration::semantic(Scope *sc) | |
3195 { | |
3196 if (global.params.useUnitTests) | |
3197 { | |
3198 type = new TypeFunction(NULL, Type::tvoid, FALSE, LINKd); | |
3199 Scope *sc2 = sc->push(); | |
3200 sc2->linkage = LINKd; | |
3201 FuncDeclaration::semantic(sc2); | |
3202 sc2->pop(); | |
3203 } | |
3204 | |
3205 #if 0 | |
3206 // We're going to need ModuleInfo even if the unit tests are not | |
3207 // compiled in, because other modules may import this module and refer | |
3208 // to this ModuleInfo. | |
3209 // (This doesn't make sense to me?) | |
3210 Module *m = getModule(); | |
3211 if (!m) | |
3212 m = sc->module; | |
3213 if (m) | |
3214 { | |
3215 //printf("module3 %s needs moduleinfo\n", m->toChars()); | |
3216 m->needmoduleinfo = 1; | |
3217 } | |
3218 #endif | |
3219 } | |
3220 | |
3221 AggregateDeclaration *UnitTestDeclaration::isThis() | |
3222 { | |
3223 return NULL; | |
3224 } | |
3225 | |
3226 int UnitTestDeclaration::isVirtual() | |
3227 { | |
3228 return FALSE; | |
3229 } | |
3230 | |
3231 int UnitTestDeclaration::addPreInvariant() | |
3232 { | |
3233 return FALSE; | |
3234 } | |
3235 | |
3236 int UnitTestDeclaration::addPostInvariant() | |
3237 { | |
3238 return FALSE; | |
3239 } | |
3240 | |
3241 void UnitTestDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
3242 { | |
3243 if (hgs->hdrgen) | |
3244 return; | |
3245 buf->writestring("unittest"); | |
3246 bodyToCBuffer(buf, hgs); | |
3247 } | |
3248 | |
3249 /********************************* NewDeclaration ****************************/ | |
3250 | |
1607
207a8a438dea
Merge DMD r253: refactor: Argument => Parameter
Leandro Lucarella <llucax@gmail.com>
parents:
1602
diff
changeset
|
3251 NewDeclaration::NewDeclaration(Loc loc, Loc endloc, Parameters *arguments, int varargs) |
1587 | 3252 : FuncDeclaration(loc, endloc, Id::classNew, STCstatic, NULL) |
3253 { | |
3254 this->arguments = arguments; | |
3255 this->varargs = varargs; | |
3256 } | |
3257 | |
3258 Dsymbol *NewDeclaration::syntaxCopy(Dsymbol *s) | |
3259 { | |
3260 NewDeclaration *f; | |
3261 | |
3262 f = new NewDeclaration(loc, endloc, NULL, varargs); | |
3263 | |
3264 FuncDeclaration::syntaxCopy(f); | |
3265 | |
1607
207a8a438dea
Merge DMD r253: refactor: Argument => Parameter
Leandro Lucarella <llucax@gmail.com>
parents:
1602
diff
changeset
|
3266 f->arguments = Parameter::arraySyntaxCopy(arguments); |
1587 | 3267 |
3268 return f; | |
3269 } | |
3270 | |
3271 | |
3272 void NewDeclaration::semantic(Scope *sc) | |
3273 { | |
3274 ClassDeclaration *cd; | |
3275 Type *tret; | |
3276 | |
3277 //printf("NewDeclaration::semantic()\n"); | |
3278 | |
3279 parent = sc->parent; | |
3280 Dsymbol *parent = toParent(); | |
3281 cd = parent->isClassDeclaration(); | |
3282 if (!cd && !parent->isStructDeclaration()) | |
3283 { | |
3284 error("new allocators only are for class or struct definitions"); | |
3285 } | |
3286 tret = Type::tvoid->pointerTo(); | |
3287 type = new TypeFunction(arguments, tret, varargs, LINKd); | |
3288 | |
3289 type = type->semantic(loc, sc); | |
3290 assert(type->ty == Tfunction); | |
3291 | |
3292 // Check that there is at least one argument of type size_t | |
3293 TypeFunction *tf = (TypeFunction *)type; | |
1607
207a8a438dea
Merge DMD r253: refactor: Argument => Parameter
Leandro Lucarella <llucax@gmail.com>
parents:
1602
diff
changeset
|
3294 if (Parameter::dim(tf->parameters) < 1) |
1587 | 3295 { |
3296 error("at least one argument of type size_t expected"); | |
3297 } | |
3298 else | |
3299 { | |
1607
207a8a438dea
Merge DMD r253: refactor: Argument => Parameter
Leandro Lucarella <llucax@gmail.com>
parents:
1602
diff
changeset
|
3300 Parameter *a = Parameter::getNth(tf->parameters, 0); |
1587 | 3301 if (!a->type->equals(Type::tsize_t)) |
3302 error("first argument must be type size_t, not %s", a->type->toChars()); | |
3303 } | |
3304 | |
3305 FuncDeclaration::semantic(sc); | |
3306 } | |
3307 | |
3308 const char *NewDeclaration::kind() | |
3309 { | |
3310 return "allocator"; | |
3311 } | |
3312 | |
3313 int NewDeclaration::isVirtual() | |
3314 { | |
3315 return FALSE; | |
3316 } | |
3317 | |
3318 int NewDeclaration::addPreInvariant() | |
3319 { | |
3320 return FALSE; | |
3321 } | |
3322 | |
3323 int NewDeclaration::addPostInvariant() | |
3324 { | |
3325 return FALSE; | |
3326 } | |
3327 | |
3328 void NewDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
3329 { | |
3330 buf->writestring("new"); | |
1607
207a8a438dea
Merge DMD r253: refactor: Argument => Parameter
Leandro Lucarella <llucax@gmail.com>
parents:
1602
diff
changeset
|
3331 Parameter::argsToCBuffer(buf, hgs, arguments, varargs); |
1587 | 3332 bodyToCBuffer(buf, hgs); |
3333 } | |
3334 | |
3335 | |
3336 /********************************* DeleteDeclaration ****************************/ | |
3337 | |
1607
207a8a438dea
Merge DMD r253: refactor: Argument => Parameter
Leandro Lucarella <llucax@gmail.com>
parents:
1602
diff
changeset
|
3338 DeleteDeclaration::DeleteDeclaration(Loc loc, Loc endloc, Parameters *arguments) |
1587 | 3339 : FuncDeclaration(loc, endloc, Id::classDelete, STCstatic, NULL) |
3340 { | |
3341 this->arguments = arguments; | |
3342 } | |
3343 | |
3344 Dsymbol *DeleteDeclaration::syntaxCopy(Dsymbol *s) | |
3345 { | |
3346 DeleteDeclaration *f; | |
3347 | |
3348 f = new DeleteDeclaration(loc, endloc, NULL); | |
3349 | |
3350 FuncDeclaration::syntaxCopy(f); | |
3351 | |
1607
207a8a438dea
Merge DMD r253: refactor: Argument => Parameter
Leandro Lucarella <llucax@gmail.com>
parents:
1602
diff
changeset
|
3352 f->arguments = Parameter::arraySyntaxCopy(arguments); |
1587 | 3353 |
3354 return f; | |
3355 } | |
3356 | |
3357 | |
3358 void DeleteDeclaration::semantic(Scope *sc) | |
3359 { | |
3360 ClassDeclaration *cd; | |
3361 | |
3362 //printf("DeleteDeclaration::semantic()\n"); | |
3363 | |
3364 parent = sc->parent; | |
3365 Dsymbol *parent = toParent(); | |
3366 cd = parent->isClassDeclaration(); | |
3367 if (!cd && !parent->isStructDeclaration()) | |
3368 { | |
3369 error("new allocators only are for class or struct definitions"); | |
3370 } | |
3371 type = new TypeFunction(arguments, Type::tvoid, 0, LINKd); | |
3372 | |
3373 type = type->semantic(loc, sc); | |
3374 assert(type->ty == Tfunction); | |
3375 | |
3376 // Check that there is only one argument of type void* | |
3377 TypeFunction *tf = (TypeFunction *)type; | |
1607
207a8a438dea
Merge DMD r253: refactor: Argument => Parameter
Leandro Lucarella <llucax@gmail.com>
parents:
1602
diff
changeset
|
3378 if (Parameter::dim(tf->parameters) != 1) |
1587 | 3379 { |
3380 error("one argument of type void* expected"); | |
3381 } | |
3382 else | |
3383 { | |
1607
207a8a438dea
Merge DMD r253: refactor: Argument => Parameter
Leandro Lucarella <llucax@gmail.com>
parents:
1602
diff
changeset
|
3384 Parameter *a = Parameter::getNth(tf->parameters, 0); |
1587 | 3385 if (!a->type->equals(Type::tvoid->pointerTo())) |
3386 error("one argument of type void* expected, not %s", a->type->toChars()); | |
3387 } | |
3388 | |
3389 FuncDeclaration::semantic(sc); | |
3390 } | |
3391 | |
3392 const char *DeleteDeclaration::kind() | |
3393 { | |
3394 return "deallocator"; | |
3395 } | |
3396 | |
3397 int DeleteDeclaration::isDelete() | |
3398 { | |
3399 return TRUE; | |
3400 } | |
3401 | |
3402 int DeleteDeclaration::isVirtual() | |
3403 { | |
3404 return FALSE; | |
3405 } | |
3406 | |
3407 int DeleteDeclaration::addPreInvariant() | |
3408 { | |
3409 return FALSE; | |
3410 } | |
3411 | |
3412 int DeleteDeclaration::addPostInvariant() | |
3413 { | |
3414 return FALSE; | |
3415 } | |
3416 | |
3417 void DeleteDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
3418 { | |
3419 buf->writestring("delete"); | |
1607
207a8a438dea
Merge DMD r253: refactor: Argument => Parameter
Leandro Lucarella <llucax@gmail.com>
parents:
1602
diff
changeset
|
3420 Parameter::argsToCBuffer(buf, hgs, arguments, 0); |
1587 | 3421 bodyToCBuffer(buf, hgs); |
3422 } | |
3423 | |
3424 | |
3425 | |
3426 |