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 }
|
|
791 }
|
|
792
|
|
793 // Declare the tuple symbols and put them in the symbol table,
|
|
794 // but not in parameters[].
|
|
795 if (f->parameters)
|
|
796 {
|
|
797 for (size_t i = 0; i < f->parameters->dim; i++)
|
|
798 { Argument *arg = (Argument *)f->parameters->data[i];
|
|
799
|
|
800 if (!arg->ident)
|
|
801 continue; // never used, so ignore
|
|
802 if (arg->type->ty == Ttuple)
|
|
803 { TypeTuple *t = (TypeTuple *)arg->type;
|
|
804 size_t dim = Argument::dim(t->arguments);
|
|
805 Objects *exps = new Objects();
|
|
806 exps->setDim(dim);
|
|
807 for (size_t j = 0; j < dim; j++)
|
|
808 { Argument *narg = Argument::getNth(t->arguments, j);
|
|
809 assert(narg->ident);
|
|
810 VarDeclaration *v = sc2->search(0, narg->ident, NULL)->isVarDeclaration();
|
|
811 assert(v);
|
|
812 Expression *e = new VarExp(0, v);
|
|
813 exps->data[j] = (void *)e;
|
|
814 }
|
|
815 assert(arg->ident);
|
|
816 TupleDeclaration *v = new TupleDeclaration(0, arg->ident, exps);
|
|
817 //printf("declaring tuple %s\n", v->toChars());
|
|
818 v->isexp = 1;
|
|
819 if (!sc2->insert(v))
|
|
820 error("parameter %s.%s is already defined", toChars(), v->toChars());
|
|
821 localsymtab->insert(v);
|
|
822 v->parent = this;
|
|
823 }
|
|
824 }
|
|
825 }
|
|
826
|
|
827 /* Do the semantic analysis on the [in] preconditions and
|
|
828 * [out] postconditions.
|
|
829 */
|
|
830 sc2->incontract++;
|
|
831
|
|
832 if (frequire)
|
|
833 { /* frequire is composed of the [in] contracts
|
|
834 */
|
|
835 // BUG: need to error if accessing out parameters
|
|
836 // BUG: need to treat parameters as const
|
|
837 // BUG: need to disallow returns and throws
|
|
838 // BUG: verify that all in and ref parameters are read
|
|
839 frequire = frequire->semantic(sc2);
|
|
840 labtab = NULL; // so body can't refer to labels
|
|
841 }
|
|
842
|
|
843 if (fensure || addPostInvariant())
|
|
844 { /* fensure is composed of the [out] contracts
|
|
845 */
|
|
846 ScopeDsymbol *sym = new ScopeDsymbol();
|
|
847 sym->parent = sc2->scopesym;
|
|
848 sc2 = sc2->push(sym);
|
|
849
|
|
850 assert(type->nextOf());
|
|
851 if (type->nextOf()->ty == Tvoid)
|
|
852 {
|
|
853 if (outId)
|
|
854 error("void functions have no result");
|
|
855 }
|
|
856 else
|
|
857 {
|
|
858 if (!outId)
|
|
859 outId = Id::result; // provide a default
|
|
860 }
|
|
861
|
|
862 if (outId)
|
|
863 { // Declare result variable
|
|
864 VarDeclaration *v;
|
|
865 Loc loc = this->loc;
|
|
866
|
|
867 if (fensure)
|
|
868 loc = fensure->loc;
|
|
869
|
|
870 v = new VarDeclaration(loc, type->nextOf(), outId, NULL);
|
|
871 v->noauto = 1;
|
|
872 sc2->incontract--;
|
|
873 v->semantic(sc2);
|
|
874 sc2->incontract++;
|
|
875 if (!sc2->insert(v))
|
|
876 error("out result %s is already defined", v->toChars());
|
|
877 v->parent = this;
|
|
878 vresult = v;
|
|
879
|
|
880 // vresult gets initialized with the function return value
|
|
881 // in ReturnStatement::semantic()
|
|
882 }
|
|
883
|
|
884 // BUG: need to treat parameters as const
|
|
885 // BUG: need to disallow returns and throws
|
|
886 if (fensure)
|
|
887 { fensure = fensure->semantic(sc2);
|
|
888 labtab = NULL; // so body can't refer to labels
|
|
889 }
|
|
890
|
|
891 if (!global.params.useOut)
|
|
892 { fensure = NULL; // discard
|
|
893 vresult = NULL;
|
|
894 }
|
|
895
|
|
896 // Postcondition invariant
|
|
897 if (addPostInvariant())
|
|
898 {
|
|
899 Expression *e = NULL;
|
|
900 if (isCtorDeclaration())
|
|
901 {
|
|
902 // Call invariant directly only if it exists
|
|
903 InvariantDeclaration *inv = ad->inv;
|
|
904 ClassDeclaration *cd = ad->isClassDeclaration();
|
|
905
|
|
906 while (!inv && cd)
|
|
907 {
|
|
908 cd = cd->baseClass;
|
|
909 if (!cd)
|
|
910 break;
|
|
911 inv = cd->inv;
|
|
912 }
|
|
913 if (inv)
|
|
914 {
|
|
915 e = new DsymbolExp(0, inv);
|
|
916 e = new CallExp(0, e);
|
|
917 e = e->semantic(sc2);
|
|
918 }
|
|
919 }
|
|
920 else
|
|
921 { // Call invariant virtually
|
|
922 ThisExp *v = new ThisExp(0);
|
|
923 v->type = vthis->type;
|
|
924 e = new AssertExp(0, v);
|
|
925 }
|
|
926 if (e)
|
|
927 {
|
|
928 ExpStatement *s = new ExpStatement(0, e);
|
|
929 if (fensure)
|
|
930 fensure = new CompoundStatement(0, s, fensure);
|
|
931 else
|
|
932 fensure = s;
|
|
933 }
|
|
934 }
|
|
935
|
|
936 if (fensure)
|
|
937 { returnLabel = new LabelDsymbol(Id::returnLabel);
|
|
938 LabelStatement *ls = new LabelStatement(0, Id::returnLabel, fensure);
|
|
939 ls->isReturnLabel = 1;
|
|
940 returnLabel->statement = ls;
|
|
941 }
|
|
942 sc2 = sc2->pop();
|
|
943 }
|
|
944
|
|
945 sc2->incontract--;
|
|
946
|
|
947 if (fbody)
|
|
948 { ClassDeclaration *cd = isClassMember();
|
|
949
|
|
950 /* If this is a class constructor
|
|
951 */
|
|
952 if (isCtorDeclaration() && cd)
|
|
953 {
|
|
954 for (int i = 0; i < cd->fields.dim; i++)
|
|
955 { VarDeclaration *v = (VarDeclaration *)cd->fields.data[i];
|
|
956
|
|
957 v->ctorinit = 0;
|
|
958 }
|
|
959 }
|
|
960
|
|
961 if (inferRetType || f->retStyle() != RETstack)
|
|
962 nrvo_can = 0;
|
|
963
|
|
964 fbody = fbody->semantic(sc2);
|
|
965
|
|
966 if (inferRetType)
|
|
967 { // If no return type inferred yet, then infer a void
|
|
968 if (!type->nextOf())
|
|
969 {
|
|
970 ((TypeFunction *)type)->next = Type::tvoid;
|
|
971 type = type->semantic(loc, sc);
|
|
972 }
|
|
973 f = (TypeFunction *)type;
|
|
974 }
|
|
975
|
|
976 int offend = fbody ? fbody->fallOffEnd() : TRUE;
|
|
977
|
|
978 if (isStaticCtorDeclaration())
|
|
979 { /* It's a static constructor. Ensure that all
|
|
980 * ctor consts were initialized.
|
|
981 */
|
|
982
|
|
983 Dsymbol *p = toParent();
|
|
984 ScopeDsymbol *ad = p->isScopeDsymbol();
|
|
985 if (!ad)
|
|
986 {
|
|
987 error("static constructor can only be member of struct/class/module, not %s %s", p->kind(), p->toChars());
|
|
988 }
|
|
989 else
|
|
990 {
|
|
991 for (int i = 0; i < ad->members->dim; i++)
|
|
992 { Dsymbol *s = (Dsymbol *)ad->members->data[i];
|
|
993
|
|
994 s->checkCtorConstInit();
|
|
995 }
|
|
996 }
|
|
997 }
|
|
998
|
|
999 if (isCtorDeclaration() && cd)
|
|
1000 {
|
|
1001 //printf("callSuper = x%x\n", sc2->callSuper);
|
|
1002
|
|
1003 // Verify that all the ctorinit fields got initialized
|
|
1004 if (!(sc2->callSuper & CSXthis_ctor))
|
|
1005 {
|
|
1006 for (int i = 0; i < cd->fields.dim; i++)
|
|
1007 { VarDeclaration *v = (VarDeclaration *)cd->fields.data[i];
|
|
1008
|
|
1009 if (v->ctorinit == 0 && v->isCtorinit())
|
|
1010 error("missing initializer for const field %s", v->toChars());
|
|
1011 }
|
|
1012 }
|
|
1013
|
|
1014 if (!(sc2->callSuper & CSXany_ctor) &&
|
|
1015 cd->baseClass && cd->baseClass->ctor)
|
|
1016 {
|
|
1017 sc2->callSuper = 0;
|
|
1018
|
|
1019 // Insert implicit super() at start of fbody
|
|
1020 Expression *e1 = new SuperExp(0);
|
|
1021 Expression *e = new CallExp(0, e1);
|
|
1022
|
|
1023 unsigned errors = global.errors;
|
|
1024 global.gag++;
|
|
1025 e = e->semantic(sc2);
|
|
1026 global.gag--;
|
|
1027 if (errors != global.errors)
|
|
1028 error("no match for implicit super() call in constructor");
|
|
1029
|
|
1030 Statement *s = new ExpStatement(0, e);
|
|
1031 fbody = new CompoundStatement(0, s, fbody);
|
|
1032 }
|
|
1033 }
|
|
1034 else if (fes)
|
|
1035 { // For foreach(){} body, append a return 0;
|
|
1036 Expression *e = new IntegerExp(0);
|
|
1037 Statement *s = new ReturnStatement(0, e);
|
|
1038 fbody = new CompoundStatement(0, fbody, s);
|
|
1039 assert(!returnLabel);
|
|
1040 }
|
|
1041 else if (!hasReturnExp && type->nextOf()->ty != Tvoid)
|
|
1042 error("expected to return a value of type %s", type->nextOf()->toChars());
|
|
1043 else if (!inlineAsm)
|
|
1044 {
|
|
1045 if (type->nextOf()->ty == Tvoid)
|
|
1046 {
|
|
1047 if (offend && isMain())
|
|
1048 { // Add a return 0; statement
|
|
1049 Statement *s = new ReturnStatement(0, new IntegerExp(0));
|
|
1050 fbody = new CompoundStatement(0, fbody, s);
|
|
1051 }
|
|
1052 }
|
|
1053 else
|
|
1054 {
|
|
1055 if (offend)
|
|
1056 { Expression *e;
|
|
1057
|
|
1058 if (global.params.warnings)
|
|
1059 { fprintf(stdmsg, "warning - ");
|
|
1060 error("no return at end of function");
|
|
1061 }
|
|
1062
|
|
1063 if (global.params.useAssert &&
|
|
1064 !global.params.useInline)
|
|
1065 { /* Add an assert(0, msg); where the missing return
|
|
1066 * should be.
|
|
1067 */
|
|
1068 e = new AssertExp(
|
|
1069 endloc,
|
|
1070 new IntegerExp(0),
|
|
1071 new StringExp(loc, "missing return expression")
|
|
1072 );
|
|
1073 }
|
|
1074 else
|
|
1075 e = new HaltExp(endloc);
|
|
1076 e = new CommaExp(0, e, type->nextOf()->defaultInit());
|
|
1077 e = e->semantic(sc2);
|
|
1078 Statement *s = new ExpStatement(0, e);
|
|
1079 fbody = new CompoundStatement(0, fbody, s);
|
|
1080 }
|
|
1081 }
|
|
1082 }
|
|
1083 }
|
|
1084
|
|
1085 {
|
|
1086 Statements *a = new Statements();
|
|
1087
|
|
1088 // Merge in initialization of 'out' parameters
|
|
1089 if (parameters)
|
|
1090 { for (size_t i = 0; i < parameters->dim; i++)
|
|
1091 {
|
|
1092 VarDeclaration *v = (VarDeclaration *)parameters->data[i];
|
|
1093 if (v->storage_class & STCout)
|
|
1094 {
|
|
1095 assert(v->init);
|
|
1096 ExpInitializer *ie = v->init->isExpInitializer();
|
|
1097 assert(ie);
|
|
1098 a->push(new ExpStatement(0, ie->exp));
|
|
1099 }
|
|
1100 }
|
|
1101 }
|
|
1102
|
|
1103 // we'll handle variadics ourselves
|
|
1104 #if !IN_LLVM
|
|
1105 if (argptr)
|
|
1106 { // Initialize _argptr to point past non-variadic arg
|
|
1107 #if IN_GCC
|
|
1108 // Handled in FuncDeclaration::toObjFile
|
|
1109 v_argptr = argptr;
|
|
1110 v_argptr->init = new VoidInitializer(loc);
|
|
1111 #else
|
|
1112 Expression *e1;
|
|
1113 Expression *e;
|
|
1114 Type *t = argptr->type;
|
|
1115 VarDeclaration *p;
|
|
1116 unsigned offset;
|
|
1117
|
|
1118 e1 = new VarExp(0, argptr);
|
|
1119 if (parameters && parameters->dim)
|
|
1120 p = (VarDeclaration *)parameters->data[parameters->dim - 1];
|
|
1121 else
|
|
1122 p = v_arguments; // last parameter is _arguments[]
|
|
1123 offset = p->type->size();
|
|
1124 offset = (offset + 3) & ~3; // assume stack aligns on 4
|
|
1125 e = new SymOffExp(0, p, offset);
|
|
1126 e = new AssignExp(0, e1, e);
|
|
1127 e->type = t;
|
|
1128 a->push(new ExpStatement(0, e));
|
|
1129 #endif // IN_GCC
|
|
1130 }
|
|
1131
|
|
1132 if (_arguments)
|
|
1133 {
|
|
1134 /* Advance to elements[] member of TypeInfo_Tuple with:
|
|
1135 * _arguments = v_arguments.elements;
|
|
1136 */
|
|
1137 Expression *e = new VarExp(0, v_arguments);
|
|
1138 e = new DotIdExp(0, e, Id::elements);
|
|
1139 Expression *e1 = new VarExp(0, _arguments);
|
|
1140 e = new AssignExp(0, e1, e);
|
|
1141 e = e->semantic(sc);
|
|
1142 a->push(new ExpStatement(0, e));
|
|
1143 }
|
|
1144
|
|
1145 #endif // !IN_LLVM
|
|
1146
|
|
1147 // Merge contracts together with body into one compound statement
|
|
1148
|
|
1149 #ifdef _DH
|
|
1150 if (frequire && global.params.useIn)
|
|
1151 { frequire->incontract = 1;
|
|
1152 a->push(frequire);
|
|
1153 }
|
|
1154 #else
|
|
1155 if (frequire && global.params.useIn)
|
|
1156 a->push(frequire);
|
|
1157 #endif
|
|
1158
|
|
1159 // Precondition invariant
|
|
1160 if (addPreInvariant())
|
|
1161 {
|
|
1162 Expression *e = NULL;
|
|
1163 if (isDtorDeclaration())
|
|
1164 {
|
|
1165 // Call invariant directly only if it exists
|
|
1166 InvariantDeclaration *inv = ad->inv;
|
|
1167 ClassDeclaration *cd = ad->isClassDeclaration();
|
|
1168
|
|
1169 while (!inv && cd)
|
|
1170 {
|
|
1171 cd = cd->baseClass;
|
|
1172 if (!cd)
|
|
1173 break;
|
|
1174 inv = cd->inv;
|
|
1175 }
|
|
1176 if (inv)
|
|
1177 {
|
|
1178 e = new DsymbolExp(0, inv);
|
|
1179 e = new CallExp(0, e);
|
|
1180 e = e->semantic(sc2);
|
|
1181 }
|
|
1182 }
|
|
1183 else
|
|
1184 { // Call invariant virtually
|
|
1185 ThisExp *v = new ThisExp(0);
|
|
1186 v->type = vthis->type;
|
|
1187 Expression *se = new StringExp(0, "null this");
|
|
1188 se = se->semantic(sc);
|
|
1189 se->type = Type::tchar->arrayOf();
|
|
1190 e = new AssertExp(loc, v, se);
|
|
1191 }
|
|
1192 if (e)
|
|
1193 {
|
|
1194 ExpStatement *s = new ExpStatement(0, e);
|
|
1195 a->push(s);
|
|
1196 }
|
|
1197 }
|
|
1198
|
|
1199 if (fbody)
|
|
1200 a->push(fbody);
|
|
1201
|
|
1202 if (fensure)
|
|
1203 {
|
|
1204 a->push(returnLabel->statement);
|
|
1205
|
|
1206 if (type->nextOf()->ty != Tvoid)
|
|
1207 {
|
|
1208 // Create: return vresult;
|
|
1209 assert(vresult);
|
|
1210 Expression *e = new VarExp(0, vresult);
|
|
1211 if (tintro)
|
|
1212 { e = e->implicitCastTo(sc, tintro->nextOf());
|
|
1213 e = e->semantic(sc);
|
|
1214 }
|
|
1215 ReturnStatement *s = new ReturnStatement(0, e);
|
|
1216 a->push(s);
|
|
1217 }
|
|
1218 }
|
|
1219
|
|
1220 fbody = new CompoundStatement(0, a);
|
|
1221 }
|
|
1222
|
|
1223 sc2->callSuper = 0;
|
|
1224 sc2->pop();
|
|
1225 }
|
|
1226 semanticRun = 2;
|
|
1227 }
|
|
1228
|
|
1229 void FuncDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
|
|
1230 {
|
|
1231 //printf("FuncDeclaration::toCBuffer() '%s'\n", toChars());
|
|
1232
|
|
1233 type->toCBuffer(buf, ident, hgs);
|
|
1234 bodyToCBuffer(buf, hgs);
|
|
1235 }
|
|
1236
|
|
1237
|
|
1238 void FuncDeclaration::bodyToCBuffer(OutBuffer *buf, HdrGenState *hgs)
|
|
1239 {
|
|
1240 if (fbody &&
|
|
1241 (!hgs->hdrgen || hgs->tpltMember || canInline(1,1))
|
|
1242 )
|
|
1243 { buf->writenl();
|
|
1244
|
|
1245 // in{}
|
|
1246 if (frequire)
|
|
1247 { buf->writestring("in");
|
|
1248 buf->writenl();
|
|
1249 frequire->toCBuffer(buf, hgs);
|
|
1250 }
|
|
1251
|
|
1252 // out{}
|
|
1253 if (fensure)
|
|
1254 { buf->writestring("out");
|
|
1255 if (outId)
|
|
1256 { buf->writebyte('(');
|
|
1257 buf->writestring(outId->toChars());
|
|
1258 buf->writebyte(')');
|
|
1259 }
|
|
1260 buf->writenl();
|
|
1261 fensure->toCBuffer(buf, hgs);
|
|
1262 }
|
|
1263
|
|
1264 if (frequire || fensure)
|
|
1265 { buf->writestring("body");
|
|
1266 buf->writenl();
|
|
1267 }
|
|
1268
|
|
1269 buf->writebyte('{');
|
|
1270 buf->writenl();
|
|
1271 fbody->toCBuffer(buf, hgs);
|
|
1272 buf->writebyte('}');
|
|
1273 buf->writenl();
|
|
1274 }
|
|
1275 else
|
|
1276 { buf->writeByte(';');
|
|
1277 buf->writenl();
|
|
1278 }
|
|
1279 }
|
|
1280
|
|
1281 /****************************************************
|
|
1282 * Determine if 'this' overrides fd.
|
|
1283 * Return !=0 if it does.
|
|
1284 */
|
|
1285
|
|
1286 int FuncDeclaration::overrides(FuncDeclaration *fd)
|
|
1287 { int result = 0;
|
|
1288
|
|
1289 if (fd->ident == ident)
|
|
1290 {
|
|
1291 int cov = type->covariant(fd->type);
|
|
1292 if (cov)
|
|
1293 { ClassDeclaration *cd1 = toParent()->isClassDeclaration();
|
|
1294 ClassDeclaration *cd2 = fd->toParent()->isClassDeclaration();
|
|
1295
|
|
1296 if (cd1 && cd2 && cd2->isBaseOf(cd1, NULL))
|
|
1297 result = 1;
|
|
1298 }
|
|
1299 }
|
|
1300 return result;
|
|
1301 }
|
|
1302
|
|
1303 /*************************************************
|
|
1304 * Find index of function in vtbl[0..dim] that
|
|
1305 * this function overrides.
|
|
1306 * Returns:
|
|
1307 * -1 didn't find one
|
|
1308 * -2 can't determine because of forward references
|
|
1309 */
|
|
1310
|
|
1311 int FuncDeclaration::findVtblIndex(Array *vtbl, int dim)
|
|
1312 {
|
|
1313 for (int vi = 0; vi < dim; vi++)
|
|
1314 {
|
|
1315 FuncDeclaration *fdv = ((Dsymbol *)vtbl->data[vi])->isFuncDeclaration();
|
|
1316 if (fdv && fdv->ident == ident)
|
|
1317 {
|
|
1318 int cov = type->covariant(fdv->type);
|
|
1319 //printf("\tbaseclass cov = %d\n", cov);
|
|
1320 switch (cov)
|
|
1321 {
|
|
1322 case 0: // types are distinct
|
|
1323 break;
|
|
1324
|
|
1325 case 1:
|
|
1326 return vi;
|
|
1327
|
|
1328 case 2:
|
|
1329 //type->print();
|
|
1330 //fdv->type->print();
|
|
1331 //printf("%s %s\n", type->deco, fdv->type->deco);
|
|
1332 error("of type %s overrides but is not covariant with %s of type %s",
|
|
1333 type->toChars(), fdv->toPrettyChars(), fdv->type->toChars());
|
|
1334 break;
|
|
1335
|
|
1336 case 3:
|
|
1337 return -2; // forward references
|
|
1338
|
|
1339 default:
|
|
1340 assert(0);
|
|
1341 }
|
|
1342 }
|
|
1343 }
|
|
1344 return -1;
|
|
1345 }
|
|
1346
|
|
1347 /****************************************************
|
|
1348 * Overload this FuncDeclaration with the new one f.
|
|
1349 * Return !=0 if successful; i.e. no conflict.
|
|
1350 */
|
|
1351
|
|
1352 int FuncDeclaration::overloadInsert(Dsymbol *s)
|
|
1353 {
|
|
1354 FuncDeclaration *f;
|
|
1355 AliasDeclaration *a;
|
|
1356
|
|
1357 //printf("FuncDeclaration::overloadInsert(%s)\n", s->toChars());
|
|
1358 a = s->isAliasDeclaration();
|
|
1359 if (a)
|
|
1360 {
|
|
1361 if (overnext)
|
|
1362 return overnext->overloadInsert(a);
|
|
1363 if (!a->aliassym && a->type->ty != Tident && a->type->ty != Tinstance)
|
|
1364 {
|
|
1365 //printf("\ta = '%s'\n", a->type->toChars());
|
|
1366 return FALSE;
|
|
1367 }
|
|
1368 overnext = a;
|
|
1369 //printf("\ttrue: no conflict\n");
|
|
1370 return TRUE;
|
|
1371 }
|
|
1372 f = s->isFuncDeclaration();
|
|
1373 if (!f)
|
|
1374 return FALSE;
|
|
1375
|
|
1376 if (type && f->type && // can be NULL for overloaded constructors
|
|
1377 f->type->covariant(type) &&
|
|
1378 !isFuncAliasDeclaration())
|
|
1379 {
|
|
1380 //printf("\tfalse: conflict %s\n", kind());
|
|
1381 return FALSE;
|
|
1382 }
|
|
1383
|
|
1384 if (overnext)
|
|
1385 return overnext->overloadInsert(f);
|
|
1386 overnext = f;
|
|
1387 //printf("\ttrue: no conflict\n");
|
|
1388 return TRUE;
|
|
1389 }
|
|
1390
|
|
1391 /********************************************
|
|
1392 * Find function in overload list that exactly matches t.
|
|
1393 */
|
|
1394
|
|
1395 /***************************************************
|
|
1396 * Visit each overloaded function in turn, and call
|
|
1397 * (*fp)(param, f) on it.
|
|
1398 * Exit when no more, or (*fp)(param, f) returns 1.
|
|
1399 * Returns:
|
|
1400 * 0 continue
|
|
1401 * 1 done
|
|
1402 */
|
|
1403
|
|
1404 int overloadApply(FuncDeclaration *fstart,
|
|
1405 int (*fp)(void *, FuncDeclaration *),
|
|
1406 void *param)
|
|
1407 {
|
|
1408 FuncDeclaration *f;
|
|
1409 Declaration *d;
|
|
1410 Declaration *next;
|
|
1411
|
|
1412 for (d = fstart; d; d = next)
|
|
1413 { FuncAliasDeclaration *fa = d->isFuncAliasDeclaration();
|
|
1414
|
|
1415 if (fa)
|
|
1416 {
|
|
1417 if (overloadApply(fa->funcalias, fp, param))
|
|
1418 return 1;
|
|
1419 next = fa->overnext;
|
|
1420 }
|
|
1421 else
|
|
1422 {
|
|
1423 AliasDeclaration *a = d->isAliasDeclaration();
|
|
1424
|
|
1425 if (a)
|
|
1426 {
|
|
1427 Dsymbol *s = a->toAlias();
|
|
1428 next = s->isDeclaration();
|
|
1429 if (next == a)
|
|
1430 break;
|
|
1431 if (next == fstart)
|
|
1432 break;
|
|
1433 }
|
|
1434 else
|
|
1435 {
|
|
1436 f = d->isFuncDeclaration();
|
|
1437 if (!f)
|
|
1438 { d->error("is aliased to a function");
|
|
1439 break; // BUG: should print error message?
|
|
1440 }
|
|
1441 if ((*fp)(param, f))
|
|
1442 return 1;
|
|
1443
|
|
1444 next = f->overnext;
|
|
1445 }
|
|
1446 }
|
|
1447 }
|
|
1448 return 0;
|
|
1449 }
|
|
1450
|
|
1451 /********************************************
|
|
1452 * Find function in overload list that exactly matches t.
|
|
1453 */
|
|
1454
|
|
1455 struct Param1
|
|
1456 {
|
|
1457 Type *t; // type to match
|
|
1458 FuncDeclaration *f; // return value
|
|
1459 };
|
|
1460
|
|
1461 int fp1(void *param, FuncDeclaration *f)
|
|
1462 { Param1 *p = (Param1 *)param;
|
|
1463 Type *t = p->t;
|
|
1464
|
|
1465 if (t->equals(f->type))
|
|
1466 { p->f = f;
|
|
1467 return 1;
|
|
1468 }
|
|
1469
|
|
1470 #if V2
|
|
1471 /* Allow covariant matches, if it's just a const conversion
|
|
1472 * of the return type
|
|
1473 */
|
|
1474 if (t->ty == Tfunction)
|
|
1475 { TypeFunction *tf = (TypeFunction *)f->type;
|
|
1476 if (tf->covariant(t) == 1 &&
|
|
1477 tf->nextOf()->implicitConvTo(t->nextOf()) >= MATCHconst)
|
|
1478 {
|
|
1479 p->f = f;
|
|
1480 return 1;
|
|
1481 }
|
|
1482 }
|
|
1483 #endif
|
|
1484 return 0;
|
|
1485 }
|
|
1486
|
|
1487 FuncDeclaration *FuncDeclaration::overloadExactMatch(Type *t)
|
|
1488 {
|
|
1489 Param1 p;
|
|
1490 p.t = t;
|
|
1491 p.f = NULL;
|
|
1492 overloadApply(this, &fp1, &p);
|
|
1493 return p.f;
|
|
1494 }
|
|
1495
|
|
1496 #if 0
|
|
1497 FuncDeclaration *FuncDeclaration::overloadExactMatch(Type *t)
|
|
1498 {
|
|
1499 FuncDeclaration *f;
|
|
1500 Declaration *d;
|
|
1501 Declaration *next;
|
|
1502
|
|
1503 for (d = this; d; d = next)
|
|
1504 { FuncAliasDeclaration *fa = d->isFuncAliasDeclaration();
|
|
1505
|
|
1506 if (fa)
|
|
1507 {
|
|
1508 FuncDeclaration *f2 = fa->funcalias->overloadExactMatch(t);
|
|
1509 if (f2)
|
|
1510 return f2;
|
|
1511 next = fa->overnext;
|
|
1512 }
|
|
1513 else
|
|
1514 {
|
|
1515 AliasDeclaration *a = d->isAliasDeclaration();
|
|
1516
|
|
1517 if (a)
|
|
1518 {
|
|
1519 Dsymbol *s = a->toAlias();
|
|
1520 next = s->isDeclaration();
|
|
1521 if (next == a)
|
|
1522 break;
|
|
1523 }
|
|
1524 else
|
|
1525 {
|
|
1526 f = d->isFuncDeclaration();
|
|
1527 if (!f)
|
|
1528 break; // BUG: should print error message?
|
|
1529 if (t->equals(d->type))
|
|
1530 return f;
|
|
1531 next = f->overnext;
|
|
1532 }
|
|
1533 }
|
|
1534 }
|
|
1535 return NULL;
|
|
1536 }
|
|
1537 #endif
|
|
1538
|
|
1539 /********************************************
|
|
1540 * Decide which function matches the arguments best.
|
|
1541 */
|
|
1542
|
|
1543 struct Param2
|
|
1544 {
|
|
1545 Match *m;
|
|
1546 Expressions *arguments;
|
|
1547 };
|
|
1548
|
|
1549 int fp2(void *param, FuncDeclaration *f)
|
|
1550 { Param2 *p = (Param2 *)param;
|
|
1551 Match *m = p->m;
|
|
1552 Expressions *arguments = p->arguments;
|
|
1553 MATCH match;
|
|
1554
|
|
1555 if (f != m->lastf) // skip duplicates
|
|
1556 {
|
|
1557 TypeFunction *tf;
|
|
1558
|
|
1559 m->anyf = f;
|
|
1560 tf = (TypeFunction *)f->type;
|
|
1561 match = (MATCH) tf->callMatch(arguments);
|
|
1562 //printf("match = %d\n", match);
|
|
1563 if (match != MATCHnomatch)
|
|
1564 {
|
|
1565 if (match > m->last)
|
|
1566 goto LfIsBetter;
|
|
1567
|
|
1568 if (match < m->last)
|
|
1569 goto LlastIsBetter;
|
|
1570
|
|
1571 /* See if one of the matches overrides the other.
|
|
1572 */
|
|
1573 if (m->lastf->overrides(f))
|
|
1574 goto LlastIsBetter;
|
|
1575 else if (f->overrides(m->lastf))
|
|
1576 goto LfIsBetter;
|
|
1577
|
|
1578 Lambiguous:
|
|
1579 m->nextf = f;
|
|
1580 m->count++;
|
|
1581 return 0;
|
|
1582
|
|
1583 LfIsBetter:
|
|
1584 m->last = match;
|
|
1585 m->lastf = f;
|
|
1586 m->count = 1;
|
|
1587 return 0;
|
|
1588
|
|
1589 LlastIsBetter:
|
|
1590 return 0;
|
|
1591 }
|
|
1592 }
|
|
1593 return 0;
|
|
1594 }
|
|
1595
|
|
1596
|
|
1597 void overloadResolveX(Match *m, FuncDeclaration *fstart, Expressions *arguments)
|
|
1598 {
|
|
1599 Param2 p;
|
|
1600 p.m = m;
|
|
1601 p.arguments = arguments;
|
|
1602 overloadApply(fstart, &fp2, &p);
|
|
1603 }
|
|
1604
|
|
1605 #if 0
|
|
1606 // Recursive helper function
|
|
1607
|
|
1608 void overloadResolveX(Match *m, FuncDeclaration *fstart, Expressions *arguments)
|
|
1609 {
|
|
1610 MATCH match;
|
|
1611 Declaration *d;
|
|
1612 Declaration *next;
|
|
1613
|
|
1614 for (d = fstart; d; d = next)
|
|
1615 {
|
|
1616 FuncDeclaration *f;
|
|
1617 FuncAliasDeclaration *fa;
|
|
1618 AliasDeclaration *a;
|
|
1619
|
|
1620 fa = d->isFuncAliasDeclaration();
|
|
1621 if (fa)
|
|
1622 {
|
|
1623 overloadResolveX(m, fa->funcalias, arguments);
|
|
1624 next = fa->overnext;
|
|
1625 }
|
|
1626 else if ((f = d->isFuncDeclaration()) != NULL)
|
|
1627 {
|
|
1628 next = f->overnext;
|
|
1629 if (f == m->lastf)
|
|
1630 continue; // skip duplicates
|
|
1631 else
|
|
1632 {
|
|
1633 TypeFunction *tf;
|
|
1634
|
|
1635 m->anyf = f;
|
|
1636 tf = (TypeFunction *)f->type;
|
|
1637 match = (MATCH) tf->callMatch(arguments);
|
|
1638 //printf("match = %d\n", match);
|
|
1639 if (match != MATCHnomatch)
|
|
1640 {
|
|
1641 if (match > m->last)
|
|
1642 goto LfIsBetter;
|
|
1643
|
|
1644 if (match < m->last)
|
|
1645 goto LlastIsBetter;
|
|
1646
|
|
1647 /* See if one of the matches overrides the other.
|
|
1648 */
|
|
1649 if (m->lastf->overrides(f))
|
|
1650 goto LlastIsBetter;
|
|
1651 else if (f->overrides(m->lastf))
|
|
1652 goto LfIsBetter;
|
|
1653
|
|
1654 Lambiguous:
|
|
1655 m->nextf = f;
|
|
1656 m->count++;
|
|
1657 continue;
|
|
1658
|
|
1659 LfIsBetter:
|
|
1660 m->last = match;
|
|
1661 m->lastf = f;
|
|
1662 m->count = 1;
|
|
1663 continue;
|
|
1664
|
|
1665 LlastIsBetter:
|
|
1666 continue;
|
|
1667 }
|
|
1668 }
|
|
1669 }
|
|
1670 else if ((a = d->isAliasDeclaration()) != NULL)
|
|
1671 {
|
|
1672 Dsymbol *s = a->toAlias();
|
|
1673 next = s->isDeclaration();
|
|
1674 if (next == a)
|
|
1675 break;
|
|
1676 if (next == fstart)
|
|
1677 break;
|
|
1678 }
|
|
1679 else
|
|
1680 { d->error("is aliased to a function");
|
|
1681 break;
|
|
1682 }
|
|
1683 }
|
|
1684 }
|
|
1685 #endif
|
|
1686
|
|
1687 FuncDeclaration *FuncDeclaration::overloadResolve(Loc loc, Expressions *arguments)
|
|
1688 {
|
|
1689 TypeFunction *tf;
|
|
1690 Match m;
|
|
1691
|
|
1692 #if 0
|
|
1693 printf("FuncDeclaration::overloadResolve('%s')\n", toChars());
|
|
1694 if (arguments)
|
|
1695 { int i;
|
|
1696
|
|
1697 for (i = 0; i < arguments->dim; i++)
|
|
1698 { Expression *arg;
|
|
1699
|
|
1700 arg = (Expression *)arguments->data[i];
|
|
1701 assert(arg->type);
|
|
1702 printf("\t%s: ", arg->toChars());
|
|
1703 arg->type->print();
|
|
1704 }
|
|
1705 }
|
|
1706 #endif
|
|
1707
|
|
1708 memset(&m, 0, sizeof(m));
|
|
1709 m.last = MATCHnomatch;
|
|
1710 overloadResolveX(&m, this, arguments);
|
|
1711
|
|
1712 if (m.count == 1) // exactly one match
|
|
1713 {
|
|
1714 return m.lastf;
|
|
1715 }
|
|
1716 else
|
|
1717 {
|
|
1718 OutBuffer buf;
|
|
1719
|
|
1720 if (arguments)
|
|
1721 {
|
|
1722 HdrGenState hgs;
|
|
1723
|
|
1724 argExpTypesToCBuffer(&buf, arguments, &hgs);
|
|
1725 }
|
|
1726
|
|
1727 if (m.last == MATCHnomatch)
|
|
1728 {
|
|
1729 tf = (TypeFunction *)type;
|
|
1730
|
|
1731 //printf("tf = %s, args = %s\n", tf->deco, ((Expression *)arguments->data[0])->type->deco);
|
|
1732 error(loc, "%s does not match parameter types (%s)",
|
|
1733 Argument::argsTypesToChars(tf->parameters, tf->varargs),
|
|
1734 buf.toChars());
|
|
1735 return m.anyf; // as long as it's not a FuncAliasDeclaration
|
|
1736 }
|
|
1737 else
|
|
1738 {
|
|
1739 #if 1
|
|
1740 TypeFunction *t1 = (TypeFunction *)m.lastf->type;
|
|
1741 TypeFunction *t2 = (TypeFunction *)m.nextf->type;
|
|
1742
|
|
1743 error(loc, "called with argument types:\n\t(%s)\nmatches both:\n\t%s%s\nand:\n\t%s%s",
|
|
1744 buf.toChars(),
|
|
1745 m.lastf->toPrettyChars(), Argument::argsTypesToChars(t1->parameters, t1->varargs),
|
|
1746 m.nextf->toPrettyChars(), Argument::argsTypesToChars(t2->parameters, t2->varargs));
|
|
1747 #else
|
|
1748 error(loc, "overloads %s and %s both match argument list for %s",
|
|
1749 m.lastf->type->toChars(),
|
|
1750 m.nextf->type->toChars(),
|
|
1751 m.lastf->toChars());
|
|
1752 #endif
|
|
1753 return m.lastf;
|
|
1754 }
|
|
1755 }
|
|
1756 }
|
|
1757
|
|
1758 /********************************
|
|
1759 * Labels are in a separate scope, one per function.
|
|
1760 */
|
|
1761
|
|
1762 LabelDsymbol *FuncDeclaration::searchLabel(Identifier *ident)
|
|
1763 { Dsymbol *s;
|
|
1764
|
|
1765 if (!labtab)
|
|
1766 labtab = new DsymbolTable(); // guess we need one
|
|
1767
|
|
1768 s = labtab->lookup(ident);
|
|
1769 if (!s)
|
|
1770 {
|
|
1771 s = new LabelDsymbol(ident);
|
|
1772 labtab->insert(s);
|
|
1773 }
|
|
1774 return (LabelDsymbol *)s;
|
|
1775 }
|
|
1776 /****************************************
|
|
1777 * If non-static member function that has a 'this' pointer,
|
|
1778 * return the aggregate it is a member of.
|
|
1779 * Otherwise, return NULL.
|
|
1780 */
|
|
1781
|
|
1782 AggregateDeclaration *FuncDeclaration::isThis()
|
|
1783 { AggregateDeclaration *ad;
|
|
1784
|
|
1785 //printf("+FuncDeclaration::isThis() '%s'\n", toChars());
|
|
1786 ad = NULL;
|
|
1787 if ((storage_class & STCstatic) == 0)
|
|
1788 {
|
|
1789 ad = isMember2();
|
|
1790 }
|
|
1791 //printf("-FuncDeclaration::isThis() %p\n", ad);
|
|
1792 return ad;
|
|
1793 }
|
|
1794
|
|
1795 AggregateDeclaration *FuncDeclaration::isMember2()
|
|
1796 { AggregateDeclaration *ad;
|
|
1797
|
|
1798 //printf("+FuncDeclaration::isMember2() '%s'\n", toChars());
|
|
1799 ad = NULL;
|
|
1800 for (Dsymbol *s = this; s; s = s->parent)
|
|
1801 {
|
|
1802 //printf("\ts = '%s', parent = '%s', kind = %s\n", s->toChars(), s->parent->toChars(), s->parent->kind());
|
|
1803 ad = s->isMember();
|
|
1804 if (ad)
|
|
1805 { //printf("test4\n");
|
|
1806 break;
|
|
1807 }
|
|
1808 if (!s->parent ||
|
|
1809 (!s->parent->isTemplateInstance()))
|
|
1810 { //printf("test5\n");
|
|
1811 break;
|
|
1812 }
|
|
1813 }
|
|
1814 //printf("-FuncDeclaration::isMember2() %p\n", ad);
|
|
1815 return ad;
|
|
1816 }
|
|
1817
|
|
1818 /*****************************************
|
|
1819 * Determine lexical level difference from 'this' to nested function 'fd'.
|
|
1820 * Error if this cannot call fd.
|
|
1821 * Returns:
|
|
1822 * 0 same level
|
|
1823 * -1 increase nesting by 1 (fd is nested within 'this')
|
|
1824 * >0 decrease nesting by number
|
|
1825 */
|
|
1826
|
|
1827 int FuncDeclaration::getLevel(Loc loc, FuncDeclaration *fd)
|
|
1828 { int level;
|
|
1829 Dsymbol *s;
|
|
1830 Dsymbol *fdparent;
|
|
1831
|
|
1832 //printf("FuncDeclaration::getLevel(fd = '%s')\n", fd->toChars());
|
|
1833 fdparent = fd->toParent2();
|
|
1834 if (fdparent == this)
|
|
1835 return -1;
|
|
1836 s = this;
|
|
1837 level = 0;
|
|
1838 while (fd != s && fdparent != s->toParent2())
|
|
1839 {
|
|
1840 //printf("\ts = '%s'\n", s->toChars());
|
|
1841 FuncDeclaration *thisfd = s->isFuncDeclaration();
|
|
1842 if (thisfd)
|
|
1843 { if (!thisfd->isNested() && !thisfd->vthis)
|
|
1844 goto Lerr;
|
|
1845 }
|
|
1846 else
|
|
1847 {
|
|
1848 ClassDeclaration *thiscd = s->isClassDeclaration();
|
|
1849 if (thiscd)
|
|
1850 { if (!thiscd->isNested())
|
|
1851 goto Lerr;
|
|
1852 }
|
|
1853 else
|
|
1854 goto Lerr;
|
|
1855 }
|
|
1856
|
|
1857 s = s->toParent2();
|
|
1858 assert(s);
|
|
1859 level++;
|
|
1860 }
|
|
1861 return level;
|
|
1862
|
|
1863 Lerr:
|
|
1864 error(loc, "cannot access frame of function %s", fd->toChars());
|
|
1865 return 1;
|
|
1866 }
|
|
1867
|
|
1868 void FuncDeclaration::appendExp(Expression *e)
|
|
1869 { Statement *s;
|
|
1870
|
|
1871 s = new ExpStatement(0, e);
|
|
1872 appendState(s);
|
|
1873 }
|
|
1874
|
|
1875 void FuncDeclaration::appendState(Statement *s)
|
|
1876 { CompoundStatement *cs;
|
|
1877
|
|
1878 if (!fbody)
|
|
1879 { Statements *a;
|
|
1880
|
|
1881 a = new Statements();
|
|
1882 fbody = new CompoundStatement(0, a);
|
|
1883 }
|
|
1884 cs = fbody->isCompoundStatement();
|
|
1885 cs->statements->push(s);
|
|
1886 }
|
|
1887
|
|
1888
|
|
1889 int FuncDeclaration::isMain()
|
|
1890 {
|
|
1891 return ident == Id::main &&
|
|
1892 linkage != LINKc && !isMember() && !isNested();
|
|
1893 }
|
|
1894
|
|
1895 int FuncDeclaration::isWinMain()
|
|
1896 {
|
|
1897 return ident == Id::WinMain &&
|
|
1898 linkage != LINKc && !isMember();
|
|
1899 }
|
|
1900
|
|
1901 int FuncDeclaration::isDllMain()
|
|
1902 {
|
|
1903 return ident == Id::DllMain &&
|
|
1904 linkage != LINKc && !isMember();
|
|
1905 }
|
|
1906
|
|
1907 int FuncDeclaration::isExport()
|
|
1908 {
|
|
1909 return protection == PROTexport;
|
|
1910 }
|
|
1911
|
|
1912 int FuncDeclaration::isImportedSymbol()
|
|
1913 {
|
|
1914 //printf("isImportedSymbol()\n");
|
|
1915 //printf("protection = %d\n", protection);
|
|
1916 return (protection == PROTexport) && !fbody;
|
|
1917 }
|
|
1918
|
|
1919 // Determine if function goes into virtual function pointer table
|
|
1920
|
|
1921 int FuncDeclaration::isVirtual()
|
|
1922 {
|
|
1923 #if 0
|
|
1924 printf("FuncDeclaration::isVirtual(%s)\n", toChars());
|
|
1925 printf("%p %d %d %d %d\n", isMember(), isStatic(), protection == PROTprivate, isCtorDeclaration(), linkage != LINKd);
|
|
1926 printf("result is %d\n",
|
|
1927 isMember() &&
|
|
1928 !(isStatic() || protection == PROTprivate || protection == PROTpackage) &&
|
|
1929 toParent()->isClassDeclaration());
|
|
1930 #endif
|
|
1931 return isMember() &&
|
|
1932 !(isStatic() || protection == PROTprivate || protection == PROTpackage) &&
|
|
1933 toParent()->isClassDeclaration();
|
|
1934 }
|
|
1935
|
|
1936 int FuncDeclaration::isAbstract()
|
|
1937 {
|
|
1938 return storage_class & STCabstract;
|
|
1939 }
|
|
1940
|
|
1941 int FuncDeclaration::isCodeseg()
|
|
1942 {
|
|
1943 return TRUE; // functions are always in the code segment
|
|
1944 }
|
|
1945
|
|
1946 // Determine if function needs
|
|
1947 // a static frame pointer to its lexically enclosing function
|
|
1948
|
|
1949 int FuncDeclaration::isNested()
|
|
1950 {
|
|
1951 //if (!toParent())
|
|
1952 //printf("FuncDeclaration::isNested('%s') parent=%p\n", toChars(), parent);
|
|
1953 //printf("\ttoParent() = '%s'\n", toParent()->toChars());
|
|
1954 return ((storage_class & STCstatic) == 0) &&
|
|
1955 (toParent2()->isFuncDeclaration() != NULL);
|
|
1956 }
|
|
1957
|
|
1958 int FuncDeclaration::needThis()
|
|
1959 {
|
|
1960 //printf("FuncDeclaration::needThis() '%s'\n", toChars());
|
|
1961 int i = isThis() != NULL;
|
|
1962 //printf("\t%d\n", i);
|
|
1963 if (!i && isFuncAliasDeclaration())
|
|
1964 i = ((FuncAliasDeclaration *)this)->funcalias->needThis();
|
|
1965 return i;
|
|
1966 }
|
|
1967
|
|
1968 int FuncDeclaration::addPreInvariant()
|
|
1969 {
|
|
1970 AggregateDeclaration *ad = isThis();
|
|
1971 return (ad &&
|
|
1972 //ad->isClassDeclaration() &&
|
|
1973 global.params.useInvariants &&
|
|
1974 (protection == PROTpublic || protection == PROTexport) &&
|
|
1975 !naked);
|
|
1976 }
|
|
1977
|
|
1978 int FuncDeclaration::addPostInvariant()
|
|
1979 {
|
|
1980 AggregateDeclaration *ad = isThis();
|
|
1981 return (ad &&
|
|
1982 ad->inv &&
|
|
1983 //ad->isClassDeclaration() &&
|
|
1984 global.params.useInvariants &&
|
|
1985 (protection == PROTpublic || protection == PROTexport) &&
|
|
1986 !naked);
|
|
1987 }
|
|
1988
|
|
1989 /**********************************
|
|
1990 * Generate a FuncDeclaration for a runtime library function.
|
|
1991 */
|
|
1992
|
|
1993 FuncDeclaration *FuncDeclaration::genCfunc(Type *treturn, char *name)
|
|
1994 {
|
|
1995 return genCfunc(treturn, Lexer::idPool(name));
|
|
1996 }
|
|
1997
|
|
1998 FuncDeclaration *FuncDeclaration::genCfunc(Type *treturn, Identifier *id)
|
|
1999 {
|
|
2000 FuncDeclaration *fd;
|
|
2001 TypeFunction *tf;
|
|
2002 Dsymbol *s;
|
|
2003 static DsymbolTable *st = NULL;
|
|
2004
|
|
2005 //printf("genCfunc(name = '%s')\n", id->toChars());
|
|
2006 //printf("treturn\n\t"); treturn->print();
|
|
2007
|
|
2008 // See if already in table
|
|
2009 if (!st)
|
|
2010 st = new DsymbolTable();
|
|
2011 s = st->lookup(id);
|
|
2012 if (s)
|
|
2013 {
|
|
2014 fd = s->isFuncDeclaration();
|
|
2015 assert(fd);
|
|
2016 assert(fd->type->nextOf()->equals(treturn));
|
|
2017 }
|
|
2018 else
|
|
2019 {
|
|
2020 tf = new TypeFunction(NULL, treturn, 0, LINKc);
|
|
2021 fd = new FuncDeclaration(0, 0, id, STCstatic, tf);
|
|
2022 fd->protection = PROTpublic;
|
|
2023 fd->linkage = LINKc;
|
|
2024
|
|
2025 st->insert(fd);
|
|
2026 }
|
|
2027 return fd;
|
|
2028 }
|
|
2029
|
|
2030 char *FuncDeclaration::kind()
|
|
2031 {
|
|
2032 return "function";
|
|
2033 }
|
|
2034 /*******************************
|
|
2035 * Look at all the variables in this function that are referenced
|
|
2036 * by nested functions, and determine if a closure needs to be
|
|
2037 * created for them.
|
|
2038 */
|
|
2039
|
|
2040 #if V2
|
|
2041 int FuncDeclaration::needsClosure()
|
|
2042 {
|
|
2043 /* Need a closure for all the closureVars[] if any of the
|
|
2044 * closureVars[] are accessed by a
|
|
2045 * function that escapes the scope of this function.
|
|
2046 * We take the conservative approach and decide that any function that:
|
|
2047 * 1) is a virtual function
|
|
2048 * 2) has its address taken
|
|
2049 * 3) has a parent that escapes
|
|
2050 * escapes.
|
|
2051 */
|
|
2052
|
|
2053 //printf("FuncDeclaration::needsClosure() %s\n", toChars());
|
|
2054 for (int i = 0; i < closureVars.dim; i++)
|
|
2055 { VarDeclaration *v = (VarDeclaration *)closureVars.data[i];
|
|
2056 assert(v->isVarDeclaration());
|
|
2057 //printf("\tv = %s\n", v->toChars());
|
|
2058
|
|
2059 for (int j = 0; j < v->nestedrefs.dim; j++)
|
|
2060 { FuncDeclaration *f = (FuncDeclaration *)v->nestedrefs.data[j];
|
|
2061 assert(f != this);
|
|
2062
|
|
2063 //printf("\t\tf = %s, %d, %d\n", f->toChars(), f->isVirtual(), f->tookAddressOf);
|
|
2064 if (f->isVirtual() || f->tookAddressOf)
|
|
2065 goto Lyes; // assume f escapes this function's scope
|
|
2066
|
|
2067 // Look to see if any parents of f that are below this escape
|
|
2068 for (Dsymbol *s = f->parent; s != this; s = s->parent)
|
|
2069 {
|
|
2070 f = s->isFuncDeclaration();
|
|
2071 if (f && (f->isVirtual() || f->tookAddressOf))
|
|
2072 goto Lyes;
|
|
2073 }
|
|
2074 }
|
|
2075 }
|
|
2076 return 0;
|
|
2077
|
|
2078 Lyes:
|
|
2079 //printf("\tneeds closure\n");
|
|
2080 return 1;
|
|
2081 }
|
|
2082 #endif
|
|
2083
|
|
2084 /****************************** FuncAliasDeclaration ************************/
|
|
2085
|
|
2086 // Used as a way to import a set of functions from another scope into this one.
|
|
2087
|
|
2088 FuncAliasDeclaration::FuncAliasDeclaration(FuncDeclaration *funcalias)
|
|
2089 : FuncDeclaration(funcalias->loc, funcalias->endloc, funcalias->ident,
|
|
2090 (enum STC)funcalias->storage_class, funcalias->type)
|
|
2091 {
|
|
2092 assert(funcalias != this);
|
|
2093 this->funcalias = funcalias;
|
|
2094 }
|
|
2095
|
|
2096 char *FuncAliasDeclaration::kind()
|
|
2097 {
|
|
2098 return "function alias";
|
|
2099 }
|
|
2100
|
|
2101
|
|
2102 /****************************** FuncLiteralDeclaration ************************/
|
|
2103
|
|
2104 FuncLiteralDeclaration::FuncLiteralDeclaration(Loc loc, Loc endloc, Type *type,
|
|
2105 enum TOK tok, ForeachStatement *fes)
|
|
2106 : FuncDeclaration(loc, endloc, NULL, STCundefined, type)
|
|
2107 {
|
|
2108 char *id;
|
|
2109
|
|
2110 if (fes)
|
|
2111 id = "__foreachbody";
|
|
2112 else if (tok == TOKdelegate)
|
|
2113 id = "__dgliteral";
|
|
2114 else
|
|
2115 id = "__funcliteral";
|
|
2116 this->ident = Identifier::generateId(id);
|
|
2117 this->tok = tok;
|
|
2118 this->fes = fes;
|
|
2119 //printf("FuncLiteralDeclaration() id = '%s', type = '%s'\n", this->ident->toChars(), type->toChars());
|
|
2120 }
|
|
2121
|
|
2122 Dsymbol *FuncLiteralDeclaration::syntaxCopy(Dsymbol *s)
|
|
2123 {
|
|
2124 FuncLiteralDeclaration *f;
|
|
2125
|
|
2126 //printf("FuncLiteralDeclaration::syntaxCopy('%s')\n", toChars());
|
|
2127 if (s)
|
|
2128 f = (FuncLiteralDeclaration *)s;
|
|
2129 else
|
|
2130 f = new FuncLiteralDeclaration(loc, endloc, type->syntaxCopy(), tok, fes);
|
|
2131 FuncDeclaration::syntaxCopy(f);
|
|
2132 return f;
|
|
2133 }
|
|
2134
|
|
2135 int FuncLiteralDeclaration::isNested()
|
|
2136 {
|
|
2137 //printf("FuncLiteralDeclaration::isNested() '%s'\n", toChars());
|
|
2138 return (tok == TOKdelegate);
|
|
2139 }
|
|
2140
|
|
2141 char *FuncLiteralDeclaration::kind()
|
|
2142 {
|
|
2143 // GCC requires the (char*) casts
|
|
2144 return (tok == TOKdelegate) ? (char*)"delegate" : (char*)"function";
|
|
2145 }
|
|
2146
|
|
2147 void FuncLiteralDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
|
|
2148 {
|
|
2149 static Identifier *idfunc;
|
|
2150 static Identifier *iddel;
|
|
2151
|
|
2152 if (!idfunc)
|
|
2153 idfunc = new Identifier("function", 0);
|
|
2154 if (!iddel)
|
|
2155 iddel = new Identifier("delegate", 0);
|
|
2156
|
|
2157 type->toCBuffer(buf, ((tok == TOKdelegate) ? iddel : idfunc), hgs);
|
|
2158 bodyToCBuffer(buf, hgs);
|
|
2159 }
|
|
2160
|
|
2161
|
|
2162 /********************************* CtorDeclaration ****************************/
|
|
2163
|
|
2164 CtorDeclaration::CtorDeclaration(Loc loc, Loc endloc, Arguments *arguments, int varargs)
|
|
2165 : FuncDeclaration(loc, endloc, Id::ctor, STCundefined, NULL)
|
|
2166 {
|
|
2167 this->arguments = arguments;
|
|
2168 this->varargs = varargs;
|
|
2169 //printf("CtorDeclaration() %s\n", toChars());
|
|
2170 }
|
|
2171
|
|
2172 Dsymbol *CtorDeclaration::syntaxCopy(Dsymbol *s)
|
|
2173 {
|
|
2174 CtorDeclaration *f;
|
|
2175
|
|
2176 f = new CtorDeclaration(loc, endloc, NULL, varargs);
|
|
2177
|
|
2178 f->outId = outId;
|
|
2179 f->frequire = frequire ? frequire->syntaxCopy() : NULL;
|
|
2180 f->fensure = fensure ? fensure->syntaxCopy() : NULL;
|
|
2181 f->fbody = fbody ? fbody->syntaxCopy() : NULL;
|
|
2182 assert(!fthrows); // deprecated
|
|
2183
|
|
2184 f->arguments = Argument::arraySyntaxCopy(arguments);
|
|
2185 return f;
|
|
2186 }
|
|
2187
|
|
2188
|
|
2189 void CtorDeclaration::semantic(Scope *sc)
|
|
2190 {
|
|
2191 ClassDeclaration *cd;
|
|
2192 Type *tret;
|
|
2193
|
|
2194 //printf("CtorDeclaration::semantic()\n");
|
|
2195 if (type)
|
|
2196 return;
|
|
2197
|
|
2198 sc = sc->push();
|
|
2199 sc->stc &= ~STCstatic; // not a static constructor
|
|
2200
|
|
2201 parent = sc->parent;
|
|
2202 Dsymbol *parent = toParent();
|
|
2203 cd = parent->isClassDeclaration();
|
|
2204 if (!cd)
|
|
2205 {
|
|
2206 error("constructors are only for class definitions");
|
|
2207 tret = Type::tvoid;
|
|
2208 }
|
|
2209 else
|
|
2210 tret = cd->type; //->referenceTo();
|
|
2211 type = new TypeFunction(arguments, tret, varargs, LINKd);
|
|
2212
|
|
2213 sc->flags |= SCOPEctor;
|
|
2214 type = type->semantic(loc, sc);
|
|
2215 sc->flags &= ~SCOPEctor;
|
|
2216
|
|
2217 // Append:
|
|
2218 // return this;
|
|
2219 // to the function body
|
|
2220 if (fbody)
|
|
2221 { Expression *e;
|
|
2222 Statement *s;
|
|
2223
|
|
2224 e = new ThisExp(0);
|
|
2225 s = new ReturnStatement(0, e);
|
|
2226 fbody = new CompoundStatement(0, fbody, s);
|
|
2227 }
|
|
2228
|
|
2229 FuncDeclaration::semantic(sc);
|
|
2230
|
|
2231 sc->pop();
|
|
2232
|
|
2233 // See if it's the default constructor
|
|
2234 if (cd && varargs == 0 && Argument::dim(arguments) == 0)
|
|
2235 cd->defaultCtor = this;
|
|
2236 }
|
|
2237
|
|
2238 char *CtorDeclaration::kind()
|
|
2239 {
|
|
2240 return "constructor";
|
|
2241 }
|
|
2242
|
|
2243 char *CtorDeclaration::toChars()
|
|
2244 {
|
|
2245 return "this";
|
|
2246 }
|
|
2247
|
|
2248 int CtorDeclaration::isVirtual()
|
|
2249 {
|
|
2250 return FALSE;
|
|
2251 }
|
|
2252
|
|
2253 int CtorDeclaration::addPreInvariant()
|
|
2254 {
|
|
2255 return FALSE;
|
|
2256 }
|
|
2257
|
|
2258 int CtorDeclaration::addPostInvariant()
|
|
2259 {
|
|
2260 return (vthis && global.params.useInvariants);
|
|
2261 }
|
|
2262
|
|
2263
|
|
2264 void CtorDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
|
|
2265 {
|
|
2266 buf->writestring("this");
|
|
2267 Argument::argsToCBuffer(buf, hgs, arguments, varargs);
|
|
2268 bodyToCBuffer(buf, hgs);
|
|
2269 }
|
|
2270
|
|
2271 /********************************* DtorDeclaration ****************************/
|
|
2272
|
|
2273 DtorDeclaration::DtorDeclaration(Loc loc, Loc endloc)
|
|
2274 : FuncDeclaration(loc, endloc, Id::dtor, STCundefined, NULL)
|
|
2275 {
|
|
2276 }
|
|
2277
|
|
2278 DtorDeclaration::DtorDeclaration(Loc loc, Loc endloc, Identifier *id)
|
|
2279 : FuncDeclaration(loc, endloc, id, STCundefined, NULL)
|
|
2280 {
|
|
2281 }
|
|
2282
|
|
2283 Dsymbol *DtorDeclaration::syntaxCopy(Dsymbol *s)
|
|
2284 {
|
|
2285 assert(!s);
|
|
2286 DtorDeclaration *dd = new DtorDeclaration(loc, endloc, ident);
|
|
2287 return FuncDeclaration::syntaxCopy(dd);
|
|
2288 }
|
|
2289
|
|
2290
|
|
2291 void DtorDeclaration::semantic(Scope *sc)
|
|
2292 {
|
|
2293 ClassDeclaration *cd;
|
|
2294
|
|
2295 parent = sc->parent;
|
|
2296 Dsymbol *parent = toParent();
|
|
2297 cd = parent->isClassDeclaration();
|
|
2298 if (!cd)
|
|
2299 {
|
|
2300 error("destructors only are for class definitions");
|
|
2301 }
|
|
2302 else
|
|
2303 cd->dtors.push(this);
|
|
2304 type = new TypeFunction(NULL, Type::tvoid, FALSE, LINKd);
|
|
2305
|
|
2306 sc = sc->push();
|
|
2307 sc->stc &= ~STCstatic; // not a static destructor
|
|
2308 sc->linkage = LINKd;
|
|
2309
|
|
2310 FuncDeclaration::semantic(sc);
|
|
2311
|
|
2312 sc->pop();
|
|
2313 }
|
|
2314
|
|
2315 int DtorDeclaration::overloadInsert(Dsymbol *s)
|
|
2316 {
|
|
2317 return FALSE; // cannot overload destructors
|
|
2318 }
|
|
2319
|
|
2320 int DtorDeclaration::addPreInvariant()
|
|
2321 {
|
|
2322 return (vthis && global.params.useInvariants);
|
|
2323 }
|
|
2324
|
|
2325 int DtorDeclaration::addPostInvariant()
|
|
2326 {
|
|
2327 return FALSE;
|
|
2328 }
|
|
2329
|
|
2330 int DtorDeclaration::isVirtual()
|
|
2331 {
|
|
2332 /* This should be FALSE so that dtor's don't get put into the vtbl[],
|
|
2333 * but doing so will require recompiling everything.
|
|
2334 */
|
|
2335 #if BREAKABI
|
|
2336 return FALSE;
|
|
2337 #else
|
|
2338 return FuncDeclaration::isVirtual();
|
|
2339 #endif
|
|
2340 }
|
|
2341
|
|
2342 void DtorDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
|
|
2343 {
|
|
2344 if (hgs->hdrgen)
|
|
2345 return;
|
|
2346 buf->writestring("~this()");
|
|
2347 bodyToCBuffer(buf, hgs);
|
|
2348 }
|
|
2349
|
|
2350 /********************************* StaticCtorDeclaration ****************************/
|
|
2351
|
|
2352 StaticCtorDeclaration::StaticCtorDeclaration(Loc loc, Loc endloc)
|
|
2353 : FuncDeclaration(loc, endloc,
|
|
2354 Identifier::generateId("_staticCtor"), STCstatic, NULL)
|
|
2355 {
|
|
2356 }
|
|
2357
|
|
2358 Dsymbol *StaticCtorDeclaration::syntaxCopy(Dsymbol *s)
|
|
2359 {
|
|
2360 StaticCtorDeclaration *scd;
|
|
2361
|
|
2362 assert(!s);
|
|
2363 scd = new StaticCtorDeclaration(loc, endloc);
|
|
2364 return FuncDeclaration::syntaxCopy(scd);
|
|
2365 }
|
|
2366
|
|
2367
|
|
2368 void StaticCtorDeclaration::semantic(Scope *sc)
|
|
2369 {
|
|
2370 //printf("StaticCtorDeclaration::semantic()\n");
|
|
2371
|
|
2372 type = new TypeFunction(NULL, Type::tvoid, FALSE, LINKd);
|
|
2373
|
|
2374 FuncDeclaration::semantic(sc);
|
|
2375
|
|
2376 // We're going to need ModuleInfo
|
|
2377 Module *m = getModule();
|
|
2378 if (!m)
|
|
2379 m = sc->module;
|
|
2380 if (m)
|
|
2381 { m->needmoduleinfo = 1;
|
|
2382 #ifdef IN_GCC
|
|
2383 m->strictlyneedmoduleinfo = 1;
|
|
2384 #endif
|
|
2385 }
|
|
2386 }
|
|
2387
|
|
2388 AggregateDeclaration *StaticCtorDeclaration::isThis()
|
|
2389 {
|
|
2390 return NULL;
|
|
2391 }
|
|
2392
|
|
2393 int StaticCtorDeclaration::isStaticConstructor()
|
|
2394 {
|
|
2395 return TRUE;
|
|
2396 }
|
|
2397
|
|
2398 int StaticCtorDeclaration::isVirtual()
|
|
2399 {
|
|
2400 return FALSE;
|
|
2401 }
|
|
2402
|
|
2403 int StaticCtorDeclaration::addPreInvariant()
|
|
2404 {
|
|
2405 return FALSE;
|
|
2406 }
|
|
2407
|
|
2408 int StaticCtorDeclaration::addPostInvariant()
|
|
2409 {
|
|
2410 return FALSE;
|
|
2411 }
|
|
2412
|
|
2413 void StaticCtorDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
|
|
2414 {
|
|
2415 if (hgs->hdrgen)
|
|
2416 { buf->writestring("static this();\n");
|
|
2417 return;
|
|
2418 }
|
|
2419 buf->writestring("static this()");
|
|
2420 bodyToCBuffer(buf, hgs);
|
|
2421 }
|
|
2422
|
|
2423 /********************************* StaticDtorDeclaration ****************************/
|
|
2424
|
|
2425 StaticDtorDeclaration::StaticDtorDeclaration(Loc loc, Loc endloc)
|
|
2426 : FuncDeclaration(loc, endloc,
|
|
2427 Identifier::generateId("_staticDtor"), STCstatic, NULL)
|
|
2428 {
|
|
2429 }
|
|
2430
|
|
2431 Dsymbol *StaticDtorDeclaration::syntaxCopy(Dsymbol *s)
|
|
2432 {
|
|
2433 StaticDtorDeclaration *sdd;
|
|
2434
|
|
2435 assert(!s);
|
|
2436 sdd = new StaticDtorDeclaration(loc, endloc);
|
|
2437 return FuncDeclaration::syntaxCopy(sdd);
|
|
2438 }
|
|
2439
|
|
2440
|
|
2441 void StaticDtorDeclaration::semantic(Scope *sc)
|
|
2442 {
|
|
2443 ClassDeclaration *cd;
|
|
2444 Type *tret;
|
|
2445
|
|
2446 cd = sc->scopesym->isClassDeclaration();
|
|
2447 if (!cd)
|
|
2448 {
|
|
2449 }
|
|
2450 type = new TypeFunction(NULL, Type::tvoid, FALSE, LINKd);
|
|
2451
|
|
2452 FuncDeclaration::semantic(sc);
|
|
2453
|
|
2454 // We're going to need ModuleInfo
|
|
2455 Module *m = getModule();
|
|
2456 if (!m)
|
|
2457 m = sc->module;
|
|
2458 if (m)
|
|
2459 { m->needmoduleinfo = 1;
|
|
2460 #ifdef IN_GCC
|
|
2461 m->strictlyneedmoduleinfo = 1;
|
|
2462 #endif
|
|
2463 }
|
|
2464 }
|
|
2465
|
|
2466 AggregateDeclaration *StaticDtorDeclaration::isThis()
|
|
2467 {
|
|
2468 return NULL;
|
|
2469 }
|
|
2470
|
|
2471 int StaticDtorDeclaration::isStaticDestructor()
|
|
2472 {
|
|
2473 return TRUE;
|
|
2474 }
|
|
2475
|
|
2476 int StaticDtorDeclaration::isVirtual()
|
|
2477 {
|
|
2478 return FALSE;
|
|
2479 }
|
|
2480
|
|
2481 int StaticDtorDeclaration::addPreInvariant()
|
|
2482 {
|
|
2483 return FALSE;
|
|
2484 }
|
|
2485
|
|
2486 int StaticDtorDeclaration::addPostInvariant()
|
|
2487 {
|
|
2488 return FALSE;
|
|
2489 }
|
|
2490
|
|
2491 void StaticDtorDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
|
|
2492 {
|
|
2493 if (hgs->hdrgen)
|
|
2494 return;
|
|
2495 buf->writestring("static ~this()");
|
|
2496 bodyToCBuffer(buf, hgs);
|
|
2497 }
|
|
2498
|
|
2499 /********************************* InvariantDeclaration ****************************/
|
|
2500
|
|
2501 InvariantDeclaration::InvariantDeclaration(Loc loc, Loc endloc)
|
|
2502 : FuncDeclaration(loc, endloc, Id::classInvariant, STCundefined, NULL)
|
|
2503 {
|
|
2504 }
|
|
2505
|
|
2506 Dsymbol *InvariantDeclaration::syntaxCopy(Dsymbol *s)
|
|
2507 {
|
|
2508 InvariantDeclaration *id;
|
|
2509
|
|
2510 assert(!s);
|
|
2511 id = new InvariantDeclaration(loc, endloc);
|
|
2512 FuncDeclaration::syntaxCopy(id);
|
|
2513 return id;
|
|
2514 }
|
|
2515
|
|
2516
|
|
2517 void InvariantDeclaration::semantic(Scope *sc)
|
|
2518 {
|
|
2519 AggregateDeclaration *ad;
|
|
2520 Type *tret;
|
|
2521
|
|
2522 parent = sc->parent;
|
|
2523 Dsymbol *parent = toParent();
|
|
2524 ad = parent->isAggregateDeclaration();
|
|
2525 if (!ad)
|
|
2526 {
|
|
2527 error("invariants only are for struct/union/class definitions");
|
|
2528 return;
|
|
2529 }
|
|
2530 else if (ad->inv && ad->inv != this)
|
|
2531 {
|
|
2532 error("more than one invariant for %s", ad->toChars());
|
|
2533 }
|
|
2534 ad->inv = this;
|
|
2535 type = new TypeFunction(NULL, Type::tvoid, FALSE, LINKd);
|
|
2536
|
|
2537 sc = sc->push();
|
|
2538 sc->stc &= ~STCstatic; // not a static invariant
|
|
2539 sc->incontract++;
|
|
2540 sc->linkage = LINKd;
|
|
2541
|
|
2542 FuncDeclaration::semantic(sc);
|
|
2543
|
|
2544 sc->pop();
|
|
2545 }
|
|
2546
|
|
2547 int InvariantDeclaration::isVirtual()
|
|
2548 {
|
|
2549 return FALSE;
|
|
2550 }
|
|
2551
|
|
2552 int InvariantDeclaration::addPreInvariant()
|
|
2553 {
|
|
2554 return FALSE;
|
|
2555 }
|
|
2556
|
|
2557 int InvariantDeclaration::addPostInvariant()
|
|
2558 {
|
|
2559 return FALSE;
|
|
2560 }
|
|
2561
|
|
2562 void InvariantDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
|
|
2563 {
|
|
2564 if (hgs->hdrgen)
|
|
2565 return;
|
|
2566 buf->writestring("invariant");
|
|
2567 bodyToCBuffer(buf, hgs);
|
|
2568 }
|
|
2569
|
|
2570
|
|
2571 /********************************* UnitTestDeclaration ****************************/
|
|
2572
|
|
2573 /*******************************
|
|
2574 * Generate unique unittest function Id so we can have multiple
|
|
2575 * instances per module.
|
|
2576 */
|
|
2577
|
|
2578 static Identifier *unitTestId()
|
|
2579 {
|
|
2580 static int n;
|
|
2581 char buffer[10 + sizeof(n)*3 + 1];
|
|
2582
|
|
2583 sprintf(buffer,"__unittest%d", n);
|
|
2584 n++;
|
|
2585 return Lexer::idPool(buffer);
|
|
2586 }
|
|
2587
|
|
2588 UnitTestDeclaration::UnitTestDeclaration(Loc loc, Loc endloc)
|
|
2589 : FuncDeclaration(loc, endloc, unitTestId(), STCundefined, NULL)
|
|
2590 {
|
|
2591 }
|
|
2592
|
|
2593 Dsymbol *UnitTestDeclaration::syntaxCopy(Dsymbol *s)
|
|
2594 {
|
|
2595 UnitTestDeclaration *utd;
|
|
2596
|
|
2597 assert(!s);
|
|
2598 utd = new UnitTestDeclaration(loc, endloc);
|
|
2599 return FuncDeclaration::syntaxCopy(utd);
|
|
2600 }
|
|
2601
|
|
2602
|
|
2603 void UnitTestDeclaration::semantic(Scope *sc)
|
|
2604 {
|
|
2605 if (global.params.useUnitTests)
|
|
2606 {
|
|
2607 Type *tret;
|
|
2608
|
|
2609 type = new TypeFunction(NULL, Type::tvoid, FALSE, LINKd);
|
|
2610 FuncDeclaration::semantic(sc);
|
|
2611 }
|
|
2612
|
|
2613 // We're going to need ModuleInfo even if the unit tests are not
|
|
2614 // compiled in, because other modules may import this module and refer
|
|
2615 // to this ModuleInfo.
|
|
2616 Module *m = getModule();
|
|
2617 if (!m)
|
|
2618 m = sc->module;
|
|
2619 if (m)
|
|
2620 m->needmoduleinfo = 1;
|
|
2621 }
|
|
2622
|
|
2623 AggregateDeclaration *UnitTestDeclaration::isThis()
|
|
2624 {
|
|
2625 return NULL;
|
|
2626 }
|
|
2627
|
|
2628 int UnitTestDeclaration::isVirtual()
|
|
2629 {
|
|
2630 return FALSE;
|
|
2631 }
|
|
2632
|
|
2633 int UnitTestDeclaration::addPreInvariant()
|
|
2634 {
|
|
2635 return FALSE;
|
|
2636 }
|
|
2637
|
|
2638 int UnitTestDeclaration::addPostInvariant()
|
|
2639 {
|
|
2640 return FALSE;
|
|
2641 }
|
|
2642
|
|
2643 void UnitTestDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
|
|
2644 {
|
|
2645 if (hgs->hdrgen)
|
|
2646 return;
|
|
2647 buf->writestring("unittest");
|
|
2648 bodyToCBuffer(buf, hgs);
|
|
2649 }
|
|
2650
|
|
2651 /********************************* NewDeclaration ****************************/
|
|
2652
|
|
2653 NewDeclaration::NewDeclaration(Loc loc, Loc endloc, Arguments *arguments, int varargs)
|
|
2654 : FuncDeclaration(loc, endloc, Id::classNew, STCstatic, NULL)
|
|
2655 {
|
|
2656 this->arguments = arguments;
|
|
2657 this->varargs = varargs;
|
|
2658 }
|
|
2659
|
|
2660 Dsymbol *NewDeclaration::syntaxCopy(Dsymbol *s)
|
|
2661 {
|
|
2662 NewDeclaration *f;
|
|
2663
|
|
2664 f = new NewDeclaration(loc, endloc, NULL, varargs);
|
|
2665
|
|
2666 FuncDeclaration::syntaxCopy(f);
|
|
2667
|
|
2668 f->arguments = Argument::arraySyntaxCopy(arguments);
|
|
2669
|
|
2670 return f;
|
|
2671 }
|
|
2672
|
|
2673
|
|
2674 void NewDeclaration::semantic(Scope *sc)
|
|
2675 {
|
|
2676 ClassDeclaration *cd;
|
|
2677 Type *tret;
|
|
2678
|
|
2679 //printf("NewDeclaration::semantic()\n");
|
|
2680
|
|
2681 parent = sc->parent;
|
|
2682 Dsymbol *parent = toParent();
|
|
2683 cd = parent->isClassDeclaration();
|
|
2684 if (!cd && !parent->isStructDeclaration())
|
|
2685 {
|
|
2686 error("new allocators only are for class or struct definitions");
|
|
2687 }
|
|
2688 tret = Type::tvoid->pointerTo();
|
|
2689 type = new TypeFunction(arguments, tret, varargs, LINKd);
|
|
2690
|
|
2691 type = type->semantic(loc, sc);
|
|
2692 assert(type->ty == Tfunction);
|
|
2693
|
|
2694 // Check that there is at least one argument of type uint
|
|
2695 TypeFunction *tf = (TypeFunction *)type;
|
|
2696 if (Argument::dim(tf->parameters) < 1)
|
|
2697 {
|
|
2698 error("at least one argument of type uint expected");
|
|
2699 }
|
|
2700 else
|
|
2701 {
|
|
2702 Argument *a = Argument::getNth(tf->parameters, 0);
|
|
2703 if (!a->type->equals(Type::tuns32))
|
|
2704 error("first argument must be type uint, not %s", a->type->toChars());
|
|
2705 }
|
|
2706
|
|
2707 FuncDeclaration::semantic(sc);
|
|
2708 }
|
|
2709
|
|
2710 char *NewDeclaration::kind()
|
|
2711 {
|
|
2712 return "allocator";
|
|
2713 }
|
|
2714
|
|
2715 int NewDeclaration::isVirtual()
|
|
2716 {
|
|
2717 return FALSE;
|
|
2718 }
|
|
2719
|
|
2720 int NewDeclaration::addPreInvariant()
|
|
2721 {
|
|
2722 return FALSE;
|
|
2723 }
|
|
2724
|
|
2725 int NewDeclaration::addPostInvariant()
|
|
2726 {
|
|
2727 return FALSE;
|
|
2728 }
|
|
2729
|
|
2730 void NewDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
|
|
2731 {
|
|
2732 buf->writestring("new");
|
|
2733 Argument::argsToCBuffer(buf, hgs, arguments, varargs);
|
|
2734 bodyToCBuffer(buf, hgs);
|
|
2735 }
|
|
2736
|
|
2737
|
|
2738 /********************************* DeleteDeclaration ****************************/
|
|
2739
|
|
2740 DeleteDeclaration::DeleteDeclaration(Loc loc, Loc endloc, Arguments *arguments)
|
|
2741 : FuncDeclaration(loc, endloc, Id::classDelete, STCstatic, NULL)
|
|
2742 {
|
|
2743 this->arguments = arguments;
|
|
2744 }
|
|
2745
|
|
2746 Dsymbol *DeleteDeclaration::syntaxCopy(Dsymbol *s)
|
|
2747 {
|
|
2748 DeleteDeclaration *f;
|
|
2749
|
|
2750 f = new DeleteDeclaration(loc, endloc, NULL);
|
|
2751
|
|
2752 FuncDeclaration::syntaxCopy(f);
|
|
2753
|
|
2754 f->arguments = Argument::arraySyntaxCopy(arguments);
|
|
2755
|
|
2756 return f;
|
|
2757 }
|
|
2758
|
|
2759
|
|
2760 void DeleteDeclaration::semantic(Scope *sc)
|
|
2761 {
|
|
2762 ClassDeclaration *cd;
|
|
2763
|
|
2764 //printf("DeleteDeclaration::semantic()\n");
|
|
2765
|
|
2766 parent = sc->parent;
|
|
2767 Dsymbol *parent = toParent();
|
|
2768 cd = parent->isClassDeclaration();
|
|
2769 if (!cd && !parent->isStructDeclaration())
|
|
2770 {
|
|
2771 error("new allocators only are for class or struct definitions");
|
|
2772 }
|
|
2773 type = new TypeFunction(arguments, Type::tvoid, 0, LINKd);
|
|
2774
|
|
2775 type = type->semantic(loc, sc);
|
|
2776 assert(type->ty == Tfunction);
|
|
2777
|
|
2778 // Check that there is only one argument of type void*
|
|
2779 TypeFunction *tf = (TypeFunction *)type;
|
|
2780 if (Argument::dim(tf->parameters) != 1)
|
|
2781 {
|
|
2782 error("one argument of type void* expected");
|
|
2783 }
|
|
2784 else
|
|
2785 {
|
|
2786 Argument *a = Argument::getNth(tf->parameters, 0);
|
|
2787 if (!a->type->equals(Type::tvoid->pointerTo()))
|
|
2788 error("one argument of type void* expected, not %s", a->type->toChars());
|
|
2789 }
|
|
2790
|
|
2791 FuncDeclaration::semantic(sc);
|
|
2792 }
|
|
2793
|
|
2794 char *DeleteDeclaration::kind()
|
|
2795 {
|
|
2796 return "deallocator";
|
|
2797 }
|
|
2798
|
|
2799 int DeleteDeclaration::isDelete()
|
|
2800 {
|
|
2801 return TRUE;
|
|
2802 }
|
|
2803
|
|
2804 int DeleteDeclaration::isVirtual()
|
|
2805 {
|
|
2806 return FALSE;
|
|
2807 }
|
|
2808
|
|
2809 int DeleteDeclaration::addPreInvariant()
|
|
2810 {
|
|
2811 return FALSE;
|
|
2812 }
|
|
2813
|
|
2814 int DeleteDeclaration::addPostInvariant()
|
|
2815 {
|
|
2816 return FALSE;
|
|
2817 }
|
|
2818
|
|
2819 void DeleteDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
|
|
2820 {
|
|
2821 buf->writestring("delete");
|
|
2822 Argument::argsToCBuffer(buf, hgs, arguments, 0);
|
|
2823 bodyToCBuffer(buf, hgs);
|
|
2824 }
|
|
2825
|
|
2826
|
|
2827
|
|
2828
|