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