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