Mercurial > projects > ldc
annotate dmd/class.c @ 1628:6c36e3f49b28
Merge DMD r324: bugzilla 3663 and 3664 - fwd ref regressions
---
dmd/class.c | 2 +-
dmd/enum.c | 4 +++-
dmd/enum.h | 2 ++
dmd/mars.c | 2 +-
dmd/struct.c | 5 ++++-
5 files changed, 11 insertions(+), 4 deletions(-)
author | Leandro Lucarella <llucax@gmail.com> |
---|---|
date | Wed, 06 Jan 2010 15:18:23 -0300 |
parents | 79f64d5fee9e |
children | 44b145be2ef5 |
rev | line source |
---|---|
159 | 1 |
2 // Compiler implementation of the D programming language | |
1587 | 3 // Copyright (c) 1999-2009 by Digital Mars |
159 | 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 <stdlib.h> | |
13 #include <assert.h> | |
14 | |
15 #include "root.h" | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
875
diff
changeset
|
16 #include "rmem.h" |
159 | 17 |
18 #include "enum.h" | |
19 #include "init.h" | |
20 #include "attrib.h" | |
21 #include "declaration.h" | |
22 #include "aggregate.h" | |
23 #include "id.h" | |
24 #include "mtype.h" | |
25 #include "scope.h" | |
26 #include "module.h" | |
27 #include "expression.h" | |
28 #include "statement.h" | |
29 | |
30 /********************************* ClassDeclaration ****************************/ | |
31 | |
32 ClassDeclaration *ClassDeclaration::classinfo; | |
33 ClassDeclaration *ClassDeclaration::object; | |
34 | |
35 ClassDeclaration::ClassDeclaration(Loc loc, Identifier *id, BaseClasses *baseclasses) | |
36 : AggregateDeclaration(loc, id) | |
37 { | |
38 static char msg[] = "only object.d can define this reserved class name"; | |
39 | |
40 if (baseclasses) | |
41 this->baseclasses = *baseclasses; | |
42 baseClass = NULL; | |
43 | |
44 interfaces_dim = 0; | |
45 interfaces = NULL; | |
46 | |
47 vtblInterfaces = NULL; | |
48 | |
49 //printf("ClassDeclaration(%s), dim = %d\n", id->toChars(), this->baseclasses.dim); | |
50 | |
51 // For forward references | |
52 type = new TypeClass(this); | |
53 handle = type; | |
54 | |
55 ctor = NULL; | |
56 defaultCtor = NULL; | |
57 staticCtor = NULL; | |
58 staticDtor = NULL; | |
59 | |
1146
1860414bf3b7
* Moved ir/irsymbol.cpp/h into ir/irdsymbol.cpp/h.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1103
diff
changeset
|
60 #if IN_DMD |
159 | 61 vtblsym = NULL; |
1146
1860414bf3b7
* Moved ir/irsymbol.cpp/h into ir/irdsymbol.cpp/h.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1103
diff
changeset
|
62 #endif |
159 | 63 vclassinfo = NULL; |
64 | |
65 if (id) | |
66 { // Look for special class names | |
67 | |
68 if (id == Id::__sizeof || id == Id::alignof || id == Id::mangleof) | |
69 error("illegal class name"); | |
70 | |
71 // BUG: What if this is the wrong TypeInfo, i.e. it is nested? | |
72 if (id->toChars()[0] == 'T') | |
73 { | |
74 if (id == Id::TypeInfo) | |
75 { if (Type::typeinfo) | |
76 Type::typeinfo->error("%s", msg); | |
77 Type::typeinfo = this; | |
78 } | |
79 | |
80 if (id == Id::TypeInfo_Class) | |
81 { if (Type::typeinfoclass) | |
82 Type::typeinfoclass->error("%s", msg); | |
83 Type::typeinfoclass = this; | |
84 } | |
85 | |
86 if (id == Id::TypeInfo_Interface) | |
87 { if (Type::typeinfointerface) | |
88 Type::typeinfointerface->error("%s", msg); | |
89 Type::typeinfointerface = this; | |
90 } | |
91 | |
92 if (id == Id::TypeInfo_Struct) | |
93 { if (Type::typeinfostruct) | |
94 Type::typeinfostruct->error("%s", msg); | |
95 Type::typeinfostruct = this; | |
96 } | |
97 | |
98 if (id == Id::TypeInfo_Typedef) | |
99 { if (Type::typeinfotypedef) | |
100 Type::typeinfotypedef->error("%s", msg); | |
101 Type::typeinfotypedef = this; | |
102 } | |
103 | |
104 if (id == Id::TypeInfo_Pointer) | |
105 { if (Type::typeinfopointer) | |
106 Type::typeinfopointer->error("%s", msg); | |
107 Type::typeinfopointer = this; | |
108 } | |
109 | |
110 if (id == Id::TypeInfo_Array) | |
111 { if (Type::typeinfoarray) | |
112 Type::typeinfoarray->error("%s", msg); | |
113 Type::typeinfoarray = this; | |
114 } | |
115 | |
116 if (id == Id::TypeInfo_StaticArray) | |
117 { //if (Type::typeinfostaticarray) | |
118 //Type::typeinfostaticarray->error("%s", msg); | |
119 Type::typeinfostaticarray = this; | |
120 } | |
121 | |
122 if (id == Id::TypeInfo_AssociativeArray) | |
123 { if (Type::typeinfoassociativearray) | |
124 Type::typeinfoassociativearray->error("%s", msg); | |
125 Type::typeinfoassociativearray = this; | |
126 } | |
127 | |
128 if (id == Id::TypeInfo_Enum) | |
129 { if (Type::typeinfoenum) | |
130 Type::typeinfoenum->error("%s", msg); | |
131 Type::typeinfoenum = this; | |
132 } | |
133 | |
134 if (id == Id::TypeInfo_Function) | |
135 { if (Type::typeinfofunction) | |
136 Type::typeinfofunction->error("%s", msg); | |
137 Type::typeinfofunction = this; | |
138 } | |
139 | |
140 if (id == Id::TypeInfo_Delegate) | |
141 { if (Type::typeinfodelegate) | |
142 Type::typeinfodelegate->error("%s", msg); | |
143 Type::typeinfodelegate = this; | |
144 } | |
145 | |
146 if (id == Id::TypeInfo_Tuple) | |
147 { if (Type::typeinfotypelist) | |
148 Type::typeinfotypelist->error("%s", msg); | |
149 Type::typeinfotypelist = this; | |
150 } | |
151 | |
336 | 152 #if DMDV2 |
159 | 153 if (id == Id::TypeInfo_Const) |
154 { if (Type::typeinfoconst) | |
155 Type::typeinfoconst->error("%s", msg); | |
156 Type::typeinfoconst = this; | |
157 } | |
158 | |
159 if (id == Id::TypeInfo_Invariant) | |
160 { if (Type::typeinfoinvariant) | |
161 Type::typeinfoinvariant->error("%s", msg); | |
162 Type::typeinfoinvariant = this; | |
163 } | |
1587 | 164 |
165 if (id == Id::TypeInfo_Shared) | |
166 { if (Type::typeinfoshared) | |
167 Type::typeinfoshared->error("%s", msg); | |
168 Type::typeinfoshared = this; | |
169 } | |
159 | 170 #endif |
171 } | |
172 | |
173 if (id == Id::Object) | |
174 { if (object) | |
175 object->error("%s", msg); | |
176 object = this; | |
177 } | |
178 | |
179 if (id == Id::ClassInfo) | |
180 { if (classinfo) | |
181 classinfo->error("%s", msg); | |
182 classinfo = this; | |
183 } | |
184 | |
185 if (id == Id::ModuleInfo) | |
186 { if (Module::moduleinfo) | |
187 Module::moduleinfo->error("%s", msg); | |
188 Module::moduleinfo = this; | |
189 } | |
190 } | |
191 | |
192 com = 0; | |
1530
05c235309d6f
Make the auto storage class never have the same meaning as scope.
Christian Kamm <kamm incasoftware de>
parents:
1367
diff
changeset
|
193 isscope = 0; |
159 | 194 isabstract = 0; |
195 isnested = 0; | |
196 vthis = NULL; | |
197 inuse = 0; | |
198 } | |
199 | |
200 Dsymbol *ClassDeclaration::syntaxCopy(Dsymbol *s) | |
201 { | |
202 ClassDeclaration *cd; | |
203 | |
204 //printf("ClassDeclaration::syntaxCopy('%s')\n", toChars()); | |
205 if (s) | |
206 cd = (ClassDeclaration *)s; | |
207 else | |
208 cd = new ClassDeclaration(loc, ident, NULL); | |
209 | |
210 cd->storage_class |= storage_class; | |
211 | |
212 cd->baseclasses.setDim(this->baseclasses.dim); | |
213 for (int i = 0; i < cd->baseclasses.dim; i++) | |
214 { | |
215 BaseClass *b = (BaseClass *)this->baseclasses.data[i]; | |
216 BaseClass *b2 = new BaseClass(b->type->syntaxCopy(), b->protection); | |
217 cd->baseclasses.data[i] = b2; | |
218 } | |
219 | |
220 ScopeDsymbol::syntaxCopy(cd); | |
221 return cd; | |
222 } | |
223 | |
224 void ClassDeclaration::semantic(Scope *sc) | |
225 { int i; | |
226 unsigned offset; | |
227 | |
228 //printf("ClassDeclaration::semantic(%s), type = %p, sizeok = %d, this = %p\n", toChars(), type, sizeok, this); | |
229 //printf("\tparent = %p, '%s'\n", sc->parent, sc->parent ? sc->parent->toChars() : ""); | |
230 //printf("sc->stc = %x\n", sc->stc); | |
231 | |
232 //{ static int n; if (++n == 20) *(char*)0=0; } | |
233 | |
234 if (!ident) // if anonymous class | |
1587 | 235 { const char *id = "__anonclass"; |
159 | 236 |
237 ident = Identifier::generateId(id); | |
238 } | |
239 | |
1587 | 240 if (!sc) |
241 sc = scope; | |
242 if (!parent && sc->parent && !sc->parent->isModule()) | |
243 parent = sc->parent; | |
159 | 244 |
1587 | 245 type = type->semantic(loc, sc); |
246 handle = type; | |
247 | |
159 | 248 if (!members) // if forward reference |
249 { //printf("\tclass '%s' is forward referenced\n", toChars()); | |
250 return; | |
251 } | |
252 if (symtab) | |
1628
6c36e3f49b28
Merge DMD r324: bugzilla 3663 and 3664 - fwd ref regressions
Leandro Lucarella <llucax@gmail.com>
parents:
1625
diff
changeset
|
253 { if (sizeok == 1 || !scope) |
159 | 254 { //printf("\tsemantic for '%s' is already completed\n", toChars()); |
255 return; // semantic() already completed | |
256 } | |
257 } | |
258 else | |
259 symtab = new DsymbolTable(); | |
260 | |
261 Scope *scx = NULL; | |
262 if (scope) | |
263 { sc = scope; | |
264 scx = scope; // save so we don't make redundant copies | |
265 scope = NULL; | |
266 } | |
267 #ifdef IN_GCC | |
268 methods.setDim(0); | |
269 #endif | |
270 | |
271 if (sc->stc & STCdeprecated) | |
336 | 272 { |
159 | 273 isdeprecated = 1; |
274 } | |
275 | |
276 // Expand any tuples in baseclasses[] | |
277 for (i = 0; i < baseclasses.dim; ) | |
278 { BaseClass *b = (BaseClass *)baseclasses.data[i]; | |
279 b->type = b->type->semantic(loc, sc); | |
280 Type *tb = b->type->toBasetype(); | |
281 | |
282 if (tb->ty == Ttuple) | |
283 { TypeTuple *tup = (TypeTuple *)tb; | |
284 enum PROT protection = b->protection; | |
285 baseclasses.remove(i); | |
1607
207a8a438dea
Merge DMD r253: refactor: Argument => Parameter
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
286 size_t dim = Parameter::dim(tup->arguments); |
159 | 287 for (size_t j = 0; j < dim; j++) |
1607
207a8a438dea
Merge DMD r253: refactor: Argument => Parameter
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
288 { Parameter *arg = Parameter::getNth(tup->arguments, j); |
159 | 289 b = new BaseClass(arg->type, protection); |
290 baseclasses.insert(i + j, b); | |
291 } | |
292 } | |
293 else | |
294 i++; | |
295 } | |
296 | |
297 // See if there's a base class as first in baseclasses[] | |
298 if (baseclasses.dim) | |
299 { TypeClass *tc; | |
300 BaseClass *b; | |
301 Type *tb; | |
302 | |
303 b = (BaseClass *)baseclasses.data[0]; | |
304 //b->type = b->type->semantic(loc, sc); | |
305 tb = b->type->toBasetype(); | |
306 if (tb->ty != Tclass) | |
307 { error("base type must be class or interface, not %s", b->type->toChars()); | |
308 baseclasses.remove(0); | |
309 } | |
310 else | |
311 { | |
312 tc = (TypeClass *)(tb); | |
336 | 313 |
159 | 314 if (tc->sym->isDeprecated()) |
315 { | |
316 if (!isDeprecated()) | |
317 { | |
318 // Deriving from deprecated class makes this one deprecated too | |
319 isdeprecated = 1; | |
320 | |
321 tc->checkDeprecated(loc, sc); | |
322 } | |
323 } | |
324 | |
325 if (tc->sym->isInterfaceDeclaration()) | |
326 ; | |
327 else | |
328 { | |
329 for (ClassDeclaration *cdb = tc->sym; cdb; cdb = cdb->baseClass) | |
330 { | |
331 if (cdb == this) | |
332 { | |
333 error("circular inheritance"); | |
334 baseclasses.remove(0); | |
335 goto L7; | |
336 } | |
337 } | |
1587 | 338 if (!tc->sym->symtab || tc->sym->sizeok == 0) |
339 { // Try to resolve forward reference | |
340 if (sc->mustsemantic && tc->sym->scope) | |
341 tc->sym->semantic(NULL); | |
342 } | |
159 | 343 if (!tc->sym->symtab || tc->sym->scope || tc->sym->sizeok == 0) |
344 { | |
1587 | 345 //printf("%s: forward reference of base class %s\n", toChars(), tc->sym->toChars()); |
159 | 346 //error("forward reference of base class %s", baseClass->toChars()); |
347 // Forward reference of base class, try again later | |
348 //printf("\ttry later, forward reference of base class %s\n", tc->sym->toChars()); | |
349 scope = scx ? scx : new Scope(*sc); | |
350 scope->setNoFree(); | |
1587 | 351 if (tc->sym->scope) |
352 tc->sym->scope->module->addDeferredSemantic(tc->sym); | |
159 | 353 scope->module->addDeferredSemantic(this); |
354 return; | |
355 } | |
356 else | |
357 { baseClass = tc->sym; | |
358 b->base = baseClass; | |
359 } | |
360 L7: ; | |
361 } | |
362 } | |
363 } | |
364 | |
365 // Treat the remaining entries in baseclasses as interfaces | |
366 // Check for errors, handle forward references | |
367 for (i = (baseClass ? 1 : 0); i < baseclasses.dim; ) | |
368 { TypeClass *tc; | |
369 BaseClass *b; | |
370 Type *tb; | |
371 | |
372 b = (BaseClass *)baseclasses.data[i]; | |
373 b->type = b->type->semantic(loc, sc); | |
374 tb = b->type->toBasetype(); | |
375 if (tb->ty == Tclass) | |
376 tc = (TypeClass *)tb; | |
377 else | |
378 tc = NULL; | |
379 if (!tc || !tc->sym->isInterfaceDeclaration()) | |
380 { | |
381 error("base type must be interface, not %s", b->type->toChars()); | |
382 baseclasses.remove(i); | |
383 continue; | |
384 } | |
385 else | |
386 { | |
387 if (tc->sym->isDeprecated()) | |
388 { | |
389 if (!isDeprecated()) | |
390 { | |
391 // Deriving from deprecated class makes this one deprecated too | |
392 isdeprecated = 1; | |
393 | |
394 tc->checkDeprecated(loc, sc); | |
395 } | |
396 } | |
397 | |
398 // Check for duplicate interfaces | |
399 for (size_t j = (baseClass ? 1 : 0); j < i; j++) | |
400 { | |
401 BaseClass *b2 = (BaseClass *)baseclasses.data[j]; | |
402 if (b2->base == tc->sym) | |
403 error("inherits from duplicate interface %s", b2->base->toChars()); | |
404 } | |
405 | |
1587 | 406 if (!tc->sym->symtab) |
407 { // Try to resolve forward reference | |
408 if (sc->mustsemantic && tc->sym->scope) | |
409 tc->sym->semantic(NULL); | |
410 } | |
411 | |
159 | 412 b->base = tc->sym; |
413 if (!b->base->symtab || b->base->scope) | |
414 { | |
415 //error("forward reference of base class %s", baseClass->toChars()); | |
416 // Forward reference of base, try again later | |
417 //printf("\ttry later, forward reference of base %s\n", baseClass->toChars()); | |
418 scope = scx ? scx : new Scope(*sc); | |
419 scope->setNoFree(); | |
1587 | 420 if (tc->sym->scope) |
421 tc->sym->scope->module->addDeferredSemantic(tc->sym); | |
159 | 422 scope->module->addDeferredSemantic(this); |
423 return; | |
424 } | |
425 } | |
426 i++; | |
427 } | |
428 | |
429 | |
430 // If no base class, and this is not an Object, use Object as base class | |
431 if (!baseClass && ident != Id::Object) | |
432 { | |
433 // BUG: what if Object is redefined in an inner scope? | |
434 Type *tbase = new TypeIdentifier(0, Id::Object); | |
435 BaseClass *b; | |
436 TypeClass *tc; | |
437 Type *bt; | |
438 | |
439 if (!object) | |
440 { | |
441 error("missing or corrupt object.d"); | |
442 fatal(); | |
443 } | |
444 bt = tbase->semantic(loc, sc)->toBasetype(); | |
445 b = new BaseClass(bt, PROTpublic); | |
446 baseclasses.shift(b); | |
447 assert(b->type->ty == Tclass); | |
448 tc = (TypeClass *)(b->type); | |
449 baseClass = tc->sym; | |
450 assert(!baseClass->isInterfaceDeclaration()); | |
451 b->base = baseClass; | |
452 } | |
453 | |
454 interfaces_dim = baseclasses.dim; | |
455 interfaces = (BaseClass **)baseclasses.data; | |
456 | |
457 | |
458 if (baseClass) | |
459 { | |
460 if (baseClass->storage_class & STCfinal) | |
461 error("cannot inherit from final class %s", baseClass->toChars()); | |
462 | |
463 interfaces_dim--; | |
464 interfaces++; | |
465 | |
466 // Copy vtbl[] from base class | |
467 vtbl.setDim(baseClass->vtbl.dim); | |
468 memcpy(vtbl.data, baseClass->vtbl.data, sizeof(void *) * vtbl.dim); | |
469 | |
470 // Inherit properties from base class | |
471 com = baseClass->isCOMclass(); | |
1530
05c235309d6f
Make the auto storage class never have the same meaning as scope.
Christian Kamm <kamm incasoftware de>
parents:
1367
diff
changeset
|
472 isscope = baseClass->isscope; |
159 | 473 vthis = baseClass->vthis; |
474 } | |
475 else | |
476 { | |
477 // No base class, so this is the root of the class hierarchy | |
478 vtbl.setDim(0); | |
479 vtbl.push(this); // leave room for classinfo as first member | |
480 } | |
481 | |
482 protection = sc->protection; | |
483 storage_class |= sc->stc; | |
484 | |
485 if (sizeok == 0) | |
486 { | |
487 interfaceSemantic(sc); | |
488 | |
489 for (i = 0; i < members->dim; i++) | |
490 { | |
491 Dsymbol *s = (Dsymbol *)members->data[i]; | |
492 s->addMember(sc, this, 1); | |
493 } | |
494 | |
495 /* If this is a nested class, add the hidden 'this' | |
496 * member which is a pointer to the enclosing scope. | |
497 */ | |
498 if (vthis) // if inheriting from nested class | |
499 { // Use the base class's 'this' member | |
500 isnested = 1; | |
501 if (storage_class & STCstatic) | |
502 error("static class cannot inherit from nested class %s", baseClass->toChars()); | |
503 if (toParent2() != baseClass->toParent2()) | |
658
50383e476c7e
Upgraded frontend to DMD 1.035
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
336
diff
changeset
|
504 { |
50383e476c7e
Upgraded frontend to DMD 1.035
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
336
diff
changeset
|
505 if (toParent2()) |
50383e476c7e
Upgraded frontend to DMD 1.035
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
336
diff
changeset
|
506 { |
50383e476c7e
Upgraded frontend to DMD 1.035
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
336
diff
changeset
|
507 error("is nested within %s, but super class %s is nested within %s", |
50383e476c7e
Upgraded frontend to DMD 1.035
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
336
diff
changeset
|
508 toParent2()->toChars(), |
159 | 509 baseClass->toChars(), |
658
50383e476c7e
Upgraded frontend to DMD 1.035
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
336
diff
changeset
|
510 baseClass->toParent2()->toChars()); |
50383e476c7e
Upgraded frontend to DMD 1.035
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
336
diff
changeset
|
511 } |
50383e476c7e
Upgraded frontend to DMD 1.035
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
336
diff
changeset
|
512 else |
50383e476c7e
Upgraded frontend to DMD 1.035
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
336
diff
changeset
|
513 { |
50383e476c7e
Upgraded frontend to DMD 1.035
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
336
diff
changeset
|
514 error("is not nested, but super class %s is nested within %s", |
50383e476c7e
Upgraded frontend to DMD 1.035
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
336
diff
changeset
|
515 baseClass->toChars(), |
50383e476c7e
Upgraded frontend to DMD 1.035
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
336
diff
changeset
|
516 baseClass->toParent2()->toChars()); |
50383e476c7e
Upgraded frontend to DMD 1.035
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
336
diff
changeset
|
517 } |
50383e476c7e
Upgraded frontend to DMD 1.035
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
336
diff
changeset
|
518 isnested = 0; |
50383e476c7e
Upgraded frontend to DMD 1.035
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
336
diff
changeset
|
519 } |
159 | 520 } |
521 else if (!(storage_class & STCstatic)) | |
522 { Dsymbol *s = toParent2(); | |
523 if (s) | |
524 { | |
1587 | 525 AggregateDeclaration *ad = s->isClassDeclaration(); |
159 | 526 FuncDeclaration *fd = s->isFuncDeclaration(); |
527 | |
528 | |
1587 | 529 if (ad || fd) |
159 | 530 { isnested = 1; |
531 Type *t; | |
1587 | 532 if (ad) |
533 t = ad->handle; | |
159 | 534 else if (fd) |
535 { AggregateDeclaration *ad = fd->isMember2(); | |
536 if (ad) | |
537 t = ad->handle; | |
538 else | |
539 { | |
540 t = new TypePointer(Type::tvoid); | |
541 t = t->semantic(0, sc); | |
542 } | |
543 } | |
544 else | |
545 assert(0); | |
546 assert(!vthis); | |
1367
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1358
diff
changeset
|
547 vthis = new ThisDeclaration(loc, t); |
159 | 548 members->push(vthis); |
549 } | |
550 } | |
551 } | |
552 } | |
553 | |
1530
05c235309d6f
Make the auto storage class never have the same meaning as scope.
Christian Kamm <kamm incasoftware de>
parents:
1367
diff
changeset
|
554 if (storage_class & STCauto) |
05c235309d6f
Make the auto storage class never have the same meaning as scope.
Christian Kamm <kamm incasoftware de>
parents:
1367
diff
changeset
|
555 error("storage class has no effect: auto"); |
05c235309d6f
Make the auto storage class never have the same meaning as scope.
Christian Kamm <kamm incasoftware de>
parents:
1367
diff
changeset
|
556 if (storage_class & STCscope) |
05c235309d6f
Make the auto storage class never have the same meaning as scope.
Christian Kamm <kamm incasoftware de>
parents:
1367
diff
changeset
|
557 isscope = 1; |
159 | 558 if (storage_class & STCabstract) |
559 isabstract = 1; | |
560 | |
561 sc = sc->push(this); | |
562 sc->stc &= ~(STCfinal | STCauto | STCscope | STCstatic | | |
563 STCabstract | STCdeprecated); | |
564 sc->parent = this; | |
565 sc->inunion = 0; | |
566 | |
567 if (isCOMclass()) | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
809
diff
changeset
|
568 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
809
diff
changeset
|
569 #if _WIN32 |
159 | 570 sc->linkage = LINKwindows; |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
809
diff
changeset
|
571 #else |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
809
diff
changeset
|
572 /* This enables us to use COM objects under Linux and |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
809
diff
changeset
|
573 * work with things like XPCOM |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
809
diff
changeset
|
574 */ |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
809
diff
changeset
|
575 sc->linkage = LINKc; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
809
diff
changeset
|
576 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
809
diff
changeset
|
577 } |
159 | 578 sc->protection = PROTpublic; |
579 sc->explicitProtection = 0; | |
580 sc->structalign = 8; | |
581 structalign = sc->structalign; | |
582 if (baseClass) | |
583 { sc->offset = baseClass->structsize; | |
584 alignsize = baseClass->alignsize; | |
585 // if (isnested) | |
586 // sc->offset += PTRSIZE; // room for uplevel context pointer | |
587 } | |
588 else | |
1587 | 589 { sc->offset = PTRSIZE * 2; // allow room for __vptr and __monitor |
809
69a5e4a6fc0f
Changed some hardcoded offset/alignment for classes in DMD, broke offsets for 64bits.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
658
diff
changeset
|
590 alignsize = PTRSIZE; |
159 | 591 } |
592 structsize = sc->offset; | |
593 Scope scsave = *sc; | |
594 int members_dim = members->dim; | |
595 sizeok = 0; | |
1625
79f64d5fee9e
Merge DMD r319: bugzilla 400 forward reference error...
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
596 |
79f64d5fee9e
Merge DMD r319: bugzilla 400 forward reference error...
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
597 /* Set scope so if there are forward references, we still might be able to |
79f64d5fee9e
Merge DMD r319: bugzilla 400 forward reference error...
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
598 * resolve individual members like enums. |
79f64d5fee9e
Merge DMD r319: bugzilla 400 forward reference error...
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
599 */ |
159 | 600 for (i = 0; i < members_dim; i++) |
1625
79f64d5fee9e
Merge DMD r319: bugzilla 400 forward reference error...
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
601 { Dsymbol *s = (Dsymbol *)members->data[i]; |
79f64d5fee9e
Merge DMD r319: bugzilla 400 forward reference error...
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
602 /* There are problems doing this in the general case because |
79f64d5fee9e
Merge DMD r319: bugzilla 400 forward reference error...
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
603 * Scope keeps track of things like 'offset' |
79f64d5fee9e
Merge DMD r319: bugzilla 400 forward reference error...
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
604 */ |
79f64d5fee9e
Merge DMD r319: bugzilla 400 forward reference error...
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
605 if (s->isEnumDeclaration() || (s->isAggregateDeclaration() && s->ident)) |
79f64d5fee9e
Merge DMD r319: bugzilla 400 forward reference error...
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
606 { |
79f64d5fee9e
Merge DMD r319: bugzilla 400 forward reference error...
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
607 //printf("setScope %s %s\n", s->kind(), s->toChars()); |
79f64d5fee9e
Merge DMD r319: bugzilla 400 forward reference error...
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
608 s->setScope(sc); |
79f64d5fee9e
Merge DMD r319: bugzilla 400 forward reference error...
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
609 } |
79f64d5fee9e
Merge DMD r319: bugzilla 400 forward reference error...
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
610 } |
79f64d5fee9e
Merge DMD r319: bugzilla 400 forward reference error...
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
611 |
79f64d5fee9e
Merge DMD r319: bugzilla 400 forward reference error...
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
612 for (i = 0; i < members_dim; i++) |
79f64d5fee9e
Merge DMD r319: bugzilla 400 forward reference error...
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
613 { Dsymbol *s = (Dsymbol *)members->data[i]; |
159 | 614 s->semantic(sc); |
615 } | |
616 | |
617 if (sizeok == 2) | |
618 { // semantic() failed because of forward references. | |
619 // Unwind what we did, and defer it for later | |
620 fields.setDim(0); | |
621 structsize = 0; | |
622 alignsize = 0; | |
623 structalign = 0; | |
624 | |
625 sc = sc->pop(); | |
626 | |
627 scope = scx ? scx : new Scope(*sc); | |
628 scope->setNoFree(); | |
629 scope->module->addDeferredSemantic(this); | |
630 | |
336 | 631 //printf("\tsemantic('%s') failed due to forward references\n", toChars()); |
159 | 632 return; |
633 } | |
634 | |
635 //printf("\tsemantic('%s') successful\n", toChars()); | |
636 | |
637 structsize = sc->offset; | |
638 //members->print(); | |
639 | |
640 /* Look for special member functions. | |
641 * They must be in this class, not in a base class. | |
642 */ | |
643 ctor = (CtorDeclaration *)search(0, Id::ctor, 0); | |
658
50383e476c7e
Upgraded frontend to DMD 1.035
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
336
diff
changeset
|
644 if (ctor && (ctor->toParent() != this || !ctor->isCtorDeclaration())) |
159 | 645 ctor = NULL; |
646 | |
647 // dtor = (DtorDeclaration *)search(Id::dtor, 0); | |
648 // if (dtor && dtor->toParent() != this) | |
649 // dtor = NULL; | |
650 | |
651 // inv = (InvariantDeclaration *)search(Id::classInvariant, 0); | |
652 // if (inv && inv->toParent() != this) | |
653 // inv = NULL; | |
654 | |
655 // Can be in base class | |
656 aggNew = (NewDeclaration *)search(0, Id::classNew, 0); | |
657 aggDelete = (DeleteDeclaration *)search(0, Id::classDelete, 0); | |
658 | |
659 // If this class has no constructor, but base class does, create | |
660 // a constructor: | |
661 // this() { } | |
662 if (!ctor && baseClass && baseClass->ctor) | |
663 { | |
664 //printf("Creating default this(){} for class %s\n", toChars()); | |
336 | 665 ctor = new CtorDeclaration(loc, 0, NULL, 0); |
159 | 666 ctor->fbody = new CompoundStatement(0, new Statements()); |
667 members->push(ctor); | |
668 ctor->addMember(sc, this, 1); | |
669 *sc = scsave; // why? What about sc->nofree? | |
670 sc->offset = structsize; | |
671 ctor->semantic(sc); | |
672 defaultCtor = ctor; | |
673 } | |
674 | |
675 #if 0 | |
676 if (baseClass) | |
677 { if (!aggDelete) | |
678 aggDelete = baseClass->aggDelete; | |
679 if (!aggNew) | |
680 aggNew = baseClass->aggNew; | |
681 } | |
682 #endif | |
683 | |
684 // Allocate instance of each new interface | |
685 for (i = 0; i < vtblInterfaces->dim; i++) | |
686 { | |
687 BaseClass *b = (BaseClass *)vtblInterfaces->data[i]; | |
688 unsigned thissize = PTRSIZE; | |
689 | |
690 alignmember(structalign, thissize, &sc->offset); | |
691 assert(b->offset == 0); | |
692 b->offset = sc->offset; | |
693 | |
694 // Take care of single inheritance offsets | |
695 while (b->baseInterfaces_dim) | |
696 { | |
697 b = &b->baseInterfaces[0]; | |
698 b->offset = sc->offset; | |
699 } | |
700 | |
701 sc->offset += thissize; | |
702 if (alignsize < thissize) | |
703 alignsize = thissize; | |
704 } | |
705 structsize = sc->offset; | |
706 sizeok = 1; | |
707 Module::dprogress++; | |
708 | |
709 dtor = buildDtor(sc); | |
710 | |
711 sc->pop(); | |
712 | |
713 #if 0 // Do not call until toObjfile() because of forward references | |
714 // Fill in base class vtbl[]s | |
715 for (i = 0; i < vtblInterfaces->dim; i++) | |
716 { | |
717 BaseClass *b = (BaseClass *)vtblInterfaces->data[i]; | |
718 | |
719 //b->fillVtbl(this, &b->vtbl, 1); | |
720 } | |
721 #endif | |
722 //printf("-ClassDeclaration::semantic(%s), type = %p\n", toChars(), type); | |
723 } | |
724 | |
725 void ClassDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
726 { | |
727 if (!isAnonymous()) | |
728 { | |
729 buf->printf("%s ", kind()); | |
730 buf->writestring(toChars()); | |
731 if (baseclasses.dim) | |
732 buf->writestring(" : "); | |
733 } | |
734 for (int i = 0; i < baseclasses.dim; i++) | |
735 { | |
736 BaseClass *b = (BaseClass *)baseclasses.data[i]; | |
737 | |
738 if (i) | |
739 buf->writeByte(','); | |
740 //buf->writestring(b->base->ident->toChars()); | |
741 b->type->toCBuffer(buf, NULL, hgs); | |
742 } | |
1587 | 743 if (members) |
159 | 744 { |
1587 | 745 buf->writenl(); |
746 buf->writeByte('{'); | |
747 buf->writenl(); | |
748 for (int i = 0; i < members->dim; i++) | |
749 { | |
750 Dsymbol *s = (Dsymbol *)members->data[i]; | |
159 | 751 |
1587 | 752 buf->writestring(" "); |
753 s->toCBuffer(buf, hgs); | |
754 } | |
755 buf->writestring("}"); | |
159 | 756 } |
1587 | 757 else |
758 buf->writeByte(';'); | |
159 | 759 buf->writenl(); |
760 } | |
761 | |
762 #if 0 | |
763 void ClassDeclaration::defineRef(Dsymbol *s) | |
764 { | |
765 ClassDeclaration *cd; | |
766 | |
767 AggregateDeclaration::defineRef(s); | |
768 cd = s->isClassDeclaration(); | |
769 baseType = cd->baseType; | |
770 cd->baseType = NULL; | |
771 } | |
772 #endif | |
773 | |
774 /********************************************* | |
775 * Determine if 'this' is a base class of cd. | |
776 * This is used to detect circular inheritance only. | |
777 */ | |
778 | |
779 int ClassDeclaration::isBaseOf2(ClassDeclaration *cd) | |
780 { | |
781 if (!cd) | |
782 return 0; | |
783 //printf("ClassDeclaration::isBaseOf2(this = '%s', cd = '%s')\n", toChars(), cd->toChars()); | |
784 for (int i = 0; i < cd->baseclasses.dim; i++) | |
785 { BaseClass *b = (BaseClass *)cd->baseclasses.data[i]; | |
786 | |
787 if (b->base == this || isBaseOf2(b->base)) | |
788 return 1; | |
789 } | |
790 return 0; | |
791 } | |
792 | |
793 /******************************************* | |
794 * Determine if 'this' is a base class of cd. | |
795 */ | |
796 | |
797 int ClassDeclaration::isBaseOf(ClassDeclaration *cd, int *poffset) | |
798 { | |
799 //printf("ClassDeclaration::isBaseOf(this = '%s', cd = '%s')\n", toChars(), cd->toChars()); | |
800 if (poffset) | |
801 *poffset = 0; | |
802 while (cd) | |
803 { | |
804 if (this == cd->baseClass) | |
805 return 1; | |
806 | |
807 /* cd->baseClass might not be set if cd is forward referenced. | |
808 */ | |
809 if (!cd->baseClass && cd->baseclasses.dim && !cd->isInterfaceDeclaration()) | |
810 { | |
811 cd->error("base class is forward referenced by %s", toChars()); | |
812 } | |
813 | |
814 cd = cd->baseClass; | |
815 } | |
816 return 0; | |
817 } | |
818 | |
819 Dsymbol *ClassDeclaration::search(Loc loc, Identifier *ident, int flags) | |
820 { | |
821 Dsymbol *s; | |
1587 | 822 //printf("%s.ClassDeclaration::search('%s')\n", toChars(), ident->toChars()); |
159 | 823 |
824 if (scope) | |
1587 | 825 { Scope *sc = scope; |
826 sc->mustsemantic++; | |
827 semantic(sc); | |
828 sc->mustsemantic--; | |
829 } | |
159 | 830 |
831 if (!members || !symtab || scope) | |
1587 | 832 { |
833 error("is forward referenced when looking for '%s'", ident->toChars()); | |
159 | 834 //*(char*)0=0; |
835 return NULL; | |
836 } | |
837 | |
838 s = ScopeDsymbol::search(loc, ident, flags); | |
839 if (!s) | |
840 { | |
841 // Search bases classes in depth-first, left to right order | |
842 | |
843 int i; | |
844 | |
845 for (i = 0; i < baseclasses.dim; i++) | |
846 { | |
847 BaseClass *b = (BaseClass *)baseclasses.data[i]; | |
848 | |
849 if (b->base) | |
850 { | |
851 if (!b->base->symtab) | |
852 error("base %s is forward referenced", b->base->ident->toChars()); | |
853 else | |
854 { | |
855 s = b->base->search(loc, ident, flags); | |
856 if (s == this) // happens if s is nested in this and derives from this | |
857 s = NULL; | |
858 else if (s) | |
859 break; | |
860 } | |
861 } | |
862 } | |
863 } | |
864 return s; | |
865 } | |
866 | |
867 /********************************************************** | |
868 * fd is in the vtbl[] for this class. | |
869 * Return 1 if function is hidden (not findable through search). | |
870 */ | |
871 | |
336 | 872 #if DMDV2 |
159 | 873 int isf(void *param, FuncDeclaration *fd) |
874 { | |
875 //printf("param = %p, fd = %p %s\n", param, fd, fd->toChars()); | |
876 return param == fd; | |
877 } | |
878 | |
879 int ClassDeclaration::isFuncHidden(FuncDeclaration *fd) | |
880 { | |
1587 | 881 //printf("ClassDeclaration::isFuncHidden(class = %s, fd = %s)\n", toChars(), fd->toChars()); |
159 | 882 Dsymbol *s = search(0, fd->ident, 4|2); |
883 if (!s) | |
884 { //printf("not found\n"); | |
885 /* Because, due to a hack, if there are multiple definitions | |
886 * of fd->ident, NULL is returned. | |
887 */ | |
888 return 0; | |
889 } | |
890 FuncDeclaration *fdstart = s->toAlias()->isFuncDeclaration(); | |
891 //printf("%s fdstart = %p\n", s->kind(), fdstart); | |
1358
78038e540342
Fix overload resolution issue in dmd bug 313/314 fix.
Christian Kamm <kamm incasoftware de>
parents:
1195
diff
changeset
|
892 return !overloadApply(getModule(), fdstart, &isf, fd); |
159 | 893 } |
894 #endif | |
895 | |
896 /**************** | |
897 * Find virtual function matching identifier and type. | |
898 * Used to build virtual function tables for interface implementations. | |
899 */ | |
900 | |
901 FuncDeclaration *ClassDeclaration::findFunc(Identifier *ident, TypeFunction *tf) | |
902 { | |
903 //printf("ClassDeclaration::findFunc(%s, %s) %s\n", ident->toChars(), tf->toChars(), toChars()); | |
904 | |
905 ClassDeclaration *cd = this; | |
906 Array *vtbl = &cd->vtbl; | |
907 while (1) | |
908 { | |
909 for (size_t i = 0; i < vtbl->dim; i++) | |
910 { | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
875
diff
changeset
|
911 FuncDeclaration *fd = ((Dsymbol*)vtbl->data[i])->isFuncDeclaration(); |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
875
diff
changeset
|
912 if (!fd) |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
875
diff
changeset
|
913 continue; // the first entry might be a ClassInfo |
159 | 914 |
915 //printf("\t[%d] = %s\n", i, fd->toChars()); | |
916 if (ident == fd->ident && | |
917 //tf->equals(fd->type) | |
918 fd->type->covariant(tf) == 1 | |
919 ) | |
920 { //printf("\t\tfound\n"); | |
921 return fd; | |
922 } | |
923 //else printf("\t\t%d\n", fd->type->covariant(tf)); | |
924 } | |
925 if (!cd) | |
926 break; | |
927 vtbl = &cd->vtblFinal; | |
928 cd = cd->baseClass; | |
929 } | |
930 | |
931 return NULL; | |
932 } | |
933 | |
934 void ClassDeclaration::interfaceSemantic(Scope *sc) | |
336 | 935 { |
159 | 936 vtblInterfaces = new BaseClasses(); |
937 vtblInterfaces->reserve(interfaces_dim); | |
938 | |
336 | 939 for (size_t i = 0; i < interfaces_dim; i++) |
159 | 940 { |
941 BaseClass *b = interfaces[i]; | |
942 | |
943 // If this is an interface, and it derives from a COM interface, | |
944 // then this is a COM interface too. | |
945 if (b->base->isCOMinterface()) | |
946 com = 1; | |
947 | |
948 vtblInterfaces->push(b); | |
949 b->copyBaseInterfaces(vtblInterfaces); | |
950 } | |
951 } | |
952 | |
953 /**************************************** | |
954 */ | |
955 | |
956 int ClassDeclaration::isCOMclass() | |
957 { | |
958 return com; | |
959 } | |
960 | |
961 int ClassDeclaration::isCOMinterface() | |
962 { | |
963 return 0; | |
964 } | |
965 | |
1587 | 966 #if DMDV2 |
967 int ClassDeclaration::isCPPinterface() | |
968 { | |
969 return 0; | |
970 } | |
971 #endif | |
972 | |
159 | 973 |
974 /**************************************** | |
975 */ | |
976 | |
977 int ClassDeclaration::isAbstract() | |
978 { | |
979 if (isabstract) | |
980 return TRUE; | |
981 for (int i = 1; i < vtbl.dim; i++) | |
982 { | |
983 FuncDeclaration *fd = ((Dsymbol *)vtbl.data[i])->isFuncDeclaration(); | |
984 | |
985 //printf("\tvtbl[%d] = %p\n", i, fd); | |
986 if (!fd || fd->isAbstract()) | |
987 { | |
988 isabstract |= 1; | |
989 return TRUE; | |
990 } | |
991 } | |
992 return FALSE; | |
993 } | |
994 | |
336 | 995 |
159 | 996 /**************************************** |
997 * Returns !=0 if there's an extra member which is the 'this' | |
998 * pointer to the enclosing context (enclosing class or function) | |
999 */ | |
1000 | |
1001 int ClassDeclaration::isNested() | |
1002 { | |
1003 return isnested; | |
1004 } | |
1005 | |
1006 /**************************************** | |
1007 * Determine if slot 0 of the vtbl[] is reserved for something else. | |
1008 * For class objects, yes, this is where the classinfo ptr goes. | |
1009 * For COM interfaces, no. | |
1010 * For non-COM interfaces, yes, this is where the Interface ptr goes. | |
1011 */ | |
1012 | |
1013 int ClassDeclaration::vtblOffset() | |
1014 { | |
1015 return 1; | |
1016 } | |
1017 | |
1018 /**************************************** | |
1019 */ | |
1020 | |
336 | 1021 const char *ClassDeclaration::kind() |
159 | 1022 { |
1023 return "class"; | |
1024 } | |
1025 | |
1026 /**************************************** | |
1027 */ | |
1028 | |
1029 void ClassDeclaration::addLocalClass(ClassDeclarations *aclasses) | |
1030 { | |
1031 aclasses->push(this); | |
1032 } | |
1033 | |
1034 /********************************* InterfaceDeclaration ****************************/ | |
1035 | |
1036 InterfaceDeclaration::InterfaceDeclaration(Loc loc, Identifier *id, BaseClasses *baseclasses) | |
1037 : ClassDeclaration(loc, id, baseclasses) | |
1038 { | |
1039 com = 0; | |
1040 if (id == Id::IUnknown) // IUnknown is the root of all COM objects | |
1041 com = 1; | |
1042 } | |
1043 | |
1044 Dsymbol *InterfaceDeclaration::syntaxCopy(Dsymbol *s) | |
1045 { | |
1046 InterfaceDeclaration *id; | |
1047 | |
1048 if (s) | |
1049 id = (InterfaceDeclaration *)s; | |
1050 else | |
1051 id = new InterfaceDeclaration(loc, ident, NULL); | |
1052 | |
1053 ClassDeclaration::syntaxCopy(id); | |
1054 return id; | |
1055 } | |
1056 | |
1057 void InterfaceDeclaration::semantic(Scope *sc) | |
1058 { int i; | |
1059 | |
1060 //printf("InterfaceDeclaration::semantic(%s), type = %p\n", toChars(), type); | |
1061 if (inuse) | |
1062 return; | |
1587 | 1063 |
1064 if (!sc) | |
1065 sc = scope; | |
1066 if (!parent && sc->parent && !sc->parent->isModule()) | |
1067 parent = sc->parent; | |
1068 | |
1069 type = type->semantic(loc, sc); | |
1070 handle = type; | |
1071 | |
159 | 1072 if (!members) // if forward reference |
1073 { //printf("\tinterface '%s' is forward referenced\n", toChars()); | |
1074 return; | |
1075 } | |
1076 if (symtab) // if already done | |
1077 { if (!scope) | |
1078 return; | |
1079 } | |
1080 else | |
1081 symtab = new DsymbolTable(); | |
1082 | |
1083 Scope *scx = NULL; | |
1084 if (scope) | |
1085 { sc = scope; | |
1086 scx = scope; // save so we don't make redundant copies | |
1087 scope = NULL; | |
1088 } | |
1089 | |
1090 if (sc->stc & STCdeprecated) | |
1091 { | |
1092 isdeprecated = 1; | |
1093 } | |
1094 | |
1095 // Expand any tuples in baseclasses[] | |
1096 for (i = 0; i < baseclasses.dim; ) | |
1097 { BaseClass *b = (BaseClass *)baseclasses.data[0]; | |
1098 b->type = b->type->semantic(loc, sc); | |
1099 Type *tb = b->type->toBasetype(); | |
1100 | |
1101 if (tb->ty == Ttuple) | |
1102 { TypeTuple *tup = (TypeTuple *)tb; | |
1103 enum PROT protection = b->protection; | |
1104 baseclasses.remove(i); | |
1607
207a8a438dea
Merge DMD r253: refactor: Argument => Parameter
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
1105 size_t dim = Parameter::dim(tup->arguments); |
159 | 1106 for (size_t j = 0; j < dim; j++) |
1607
207a8a438dea
Merge DMD r253: refactor: Argument => Parameter
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
1107 { Parameter *arg = Parameter::getNth(tup->arguments, j); |
159 | 1108 b = new BaseClass(arg->type, protection); |
1109 baseclasses.insert(i + j, b); | |
1110 } | |
1111 } | |
1112 else | |
1113 i++; | |
1114 } | |
1115 | |
1116 // Check for errors, handle forward references | |
1117 for (i = 0; i < baseclasses.dim; ) | |
1118 { TypeClass *tc; | |
1119 BaseClass *b; | |
1120 Type *tb; | |
1121 | |
1122 b = (BaseClass *)baseclasses.data[i]; | |
1123 b->type = b->type->semantic(loc, sc); | |
1124 tb = b->type->toBasetype(); | |
1125 if (tb->ty == Tclass) | |
1126 tc = (TypeClass *)tb; | |
1127 else | |
1128 tc = NULL; | |
1129 if (!tc || !tc->sym->isInterfaceDeclaration()) | |
1130 { | |
1131 error("base type must be interface, not %s", b->type->toChars()); | |
1132 baseclasses.remove(i); | |
1133 continue; | |
1134 } | |
1135 else | |
1136 { | |
1137 // Check for duplicate interfaces | |
1138 for (size_t j = 0; j < i; j++) | |
1139 { | |
1140 BaseClass *b2 = (BaseClass *)baseclasses.data[j]; | |
1141 if (b2->base == tc->sym) | |
1142 error("inherits from duplicate interface %s", b2->base->toChars()); | |
1143 } | |
1144 | |
1145 b->base = tc->sym; | |
1146 if (b->base == this || isBaseOf2(b->base)) | |
1147 { | |
1148 error("circular inheritance of interface"); | |
1149 baseclasses.remove(i); | |
1150 continue; | |
1151 } | |
1587 | 1152 if (!b->base->symtab) |
1153 { // Try to resolve forward reference | |
1154 if (sc->mustsemantic && b->base->scope) | |
1155 b->base->semantic(NULL); | |
1156 } | |
159 | 1157 if (!b->base->symtab || b->base->scope || b->base->inuse) |
1158 { | |
1159 //error("forward reference of base class %s", baseClass->toChars()); | |
1160 // Forward reference of base, try again later | |
1161 //printf("\ttry later, forward reference of base %s\n", b->base->toChars()); | |
1162 scope = scx ? scx : new Scope(*sc); | |
1163 scope->setNoFree(); | |
1164 scope->module->addDeferredSemantic(this); | |
1165 return; | |
1166 } | |
1167 } | |
1168 i++; | |
1169 } | |
1170 | |
1171 interfaces_dim = baseclasses.dim; | |
1172 interfaces = (BaseClass **)baseclasses.data; | |
1173 | |
1174 interfaceSemantic(sc); | |
1175 | |
1176 if (vtblOffset()) | |
1177 vtbl.push(this); // leave room at vtbl[0] for classinfo | |
1178 | |
1179 // Cat together the vtbl[]'s from base interfaces | |
1180 for (i = 0; i < interfaces_dim; i++) | |
1181 { BaseClass *b = interfaces[i]; | |
1182 | |
1183 // Skip if b has already appeared | |
1184 for (int k = 0; k < i; k++) | |
1185 { | |
1625
79f64d5fee9e
Merge DMD r319: bugzilla 400 forward reference error...
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
1186 if (b == interfaces[k]) |
159 | 1187 goto Lcontinue; |
1188 } | |
1189 | |
1190 // Copy vtbl[] from base class | |
1191 if (b->base->vtblOffset()) | |
1192 { int d = b->base->vtbl.dim; | |
1193 if (d > 1) | |
1194 { | |
1195 vtbl.reserve(d - 1); | |
1196 for (int j = 1; j < d; j++) | |
1197 vtbl.push(b->base->vtbl.data[j]); | |
1198 } | |
1199 } | |
1200 else | |
1201 { | |
1202 vtbl.append(&b->base->vtbl); | |
1203 } | |
1204 | |
1205 Lcontinue: | |
1206 ; | |
1207 } | |
1208 | |
1209 for (i = 0; i < members->dim; i++) | |
1210 { | |
1211 Dsymbol *s = (Dsymbol *)members->data[i]; | |
1212 s->addMember(sc, this, 1); | |
1213 } | |
1214 | |
1215 sc = sc->push(this); | |
1216 sc->parent = this; | |
1217 if (isCOMinterface()) | |
1218 sc->linkage = LINKwindows; | |
1219 sc->structalign = 8; | |
1220 structalign = sc->structalign; | |
1195
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1146
diff
changeset
|
1221 sc->offset = PTRSIZE * 2; |
159 | 1222 inuse++; |
1223 for (i = 0; i < members->dim; i++) | |
1224 { | |
1225 Dsymbol *s = (Dsymbol *)members->data[i]; | |
1226 s->semantic(sc); | |
1227 } | |
1228 inuse--; | |
1229 //members->print(); | |
1230 sc->pop(); | |
1231 //printf("-InterfaceDeclaration::semantic(%s), type = %p\n", toChars(), type); | |
1232 } | |
1233 | |
1234 | |
1235 /******************************************* | |
1236 * Determine if 'this' is a base class of cd. | |
1237 * (Actually, if it is an interface supported by cd) | |
1238 * Output: | |
1239 * *poffset offset to start of class | |
1240 * OFFSET_RUNTIME must determine offset at runtime | |
1241 * Returns: | |
1242 * 0 not a base | |
1243 * 1 is a base | |
1244 */ | |
1245 | |
1246 int InterfaceDeclaration::isBaseOf(ClassDeclaration *cd, int *poffset) | |
1247 { | |
1248 unsigned j; | |
1249 | |
1250 //printf("%s.InterfaceDeclaration::isBaseOf(cd = '%s')\n", toChars(), cd->toChars()); | |
1251 assert(!baseClass); | |
1252 for (j = 0; j < cd->interfaces_dim; j++) | |
1253 { | |
1254 BaseClass *b = cd->interfaces[j]; | |
1255 | |
1256 //printf("\tbase %s\n", b->base->toChars()); | |
1257 if (this == b->base) | |
1258 { | |
1259 //printf("\tfound at offset %d\n", b->offset); | |
1260 if (poffset) | |
1261 { *poffset = b->offset; | |
1262 if (j && cd->isInterfaceDeclaration()) | |
1263 *poffset = OFFSET_RUNTIME; | |
1264 } | |
1265 return 1; | |
1266 } | |
1267 if (isBaseOf(b, poffset)) | |
1268 { if (j && poffset && cd->isInterfaceDeclaration()) | |
1269 *poffset = OFFSET_RUNTIME; | |
1270 return 1; | |
1271 } | |
1272 } | |
1273 | |
1274 if (cd->baseClass && isBaseOf(cd->baseClass, poffset)) | |
1275 return 1; | |
1276 | |
1277 if (poffset) | |
1278 *poffset = 0; | |
1279 return 0; | |
1280 } | |
1281 | |
1282 | |
1283 int InterfaceDeclaration::isBaseOf(BaseClass *bc, int *poffset) | |
1284 { | |
1285 //printf("%s.InterfaceDeclaration::isBaseOf(bc = '%s')\n", toChars(), bc->base->toChars()); | |
1286 for (unsigned j = 0; j < bc->baseInterfaces_dim; j++) | |
1287 { | |
1288 BaseClass *b = &bc->baseInterfaces[j]; | |
1289 | |
1290 if (this == b->base) | |
1291 { | |
1292 if (poffset) | |
1293 { *poffset = b->offset; | |
1367
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1358
diff
changeset
|
1294 if (j && bc->base->isInterfaceDeclaration()) |
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1358
diff
changeset
|
1295 *poffset = OFFSET_RUNTIME; |
159 | 1296 } |
1297 return 1; | |
1298 } | |
1299 if (isBaseOf(b, poffset)) | |
1367
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1358
diff
changeset
|
1300 { if (j && poffset && bc->base->isInterfaceDeclaration()) |
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1358
diff
changeset
|
1301 *poffset = OFFSET_RUNTIME; |
159 | 1302 return 1; |
1303 } | |
1304 } | |
1305 if (poffset) | |
1306 *poffset = 0; | |
1307 return 0; | |
1308 } | |
1309 | |
1310 /**************************************** | |
1311 * Determine if slot 0 of the vtbl[] is reserved for something else. | |
1312 * For class objects, yes, this is where the ClassInfo ptr goes. | |
1313 * For COM interfaces, no. | |
1314 * For non-COM interfaces, yes, this is where the Interface ptr goes. | |
1315 */ | |
1316 | |
1317 int InterfaceDeclaration::vtblOffset() | |
1318 { | |
1319 if (isCOMinterface()) | |
1320 return 0; | |
1321 return 1; | |
1322 } | |
1323 | |
1324 int InterfaceDeclaration::isCOMinterface() | |
1325 { | |
1326 return com; | |
1327 } | |
1328 | |
1587 | 1329 #if DMDV2 |
1330 int InterfaceDeclaration::isCPPinterface() | |
1331 { | |
1332 return cpp; | |
1333 } | |
1334 #endif | |
1335 | |
159 | 1336 /******************************************* |
1337 */ | |
1338 | |
336 | 1339 const char *InterfaceDeclaration::kind() |
159 | 1340 { |
1341 return "interface"; | |
1342 } | |
1343 | |
1344 | |
1345 /******************************** BaseClass *****************************/ | |
1346 | |
1347 BaseClass::BaseClass() | |
1348 { | |
1349 memset(this, 0, sizeof(BaseClass)); | |
1350 } | |
1351 | |
1352 BaseClass::BaseClass(Type *type, enum PROT protection) | |
1353 { | |
1354 //printf("BaseClass(this = %p, '%s')\n", this, type->toChars()); | |
1355 this->type = type; | |
1356 this->protection = protection; | |
1357 base = NULL; | |
1358 offset = 0; | |
1359 | |
1360 baseInterfaces_dim = 0; | |
1361 baseInterfaces = NULL; | |
1362 } | |
1363 | |
1364 /**************************************** | |
1365 * Fill in vtbl[] for base class based on member functions of class cd. | |
1366 * Input: | |
1367 * vtbl if !=NULL, fill it in | |
1368 * newinstance !=0 means all entries must be filled in by members | |
1369 * of cd, not members of any base classes of cd. | |
1370 * Returns: | |
1371 * !=0 if any entries were filled in by members of cd (not exclusively | |
1372 * by base classes) | |
1373 */ | |
1374 | |
1375 int BaseClass::fillVtbl(ClassDeclaration *cd, Array *vtbl, int newinstance) | |
1376 { | |
1377 ClassDeclaration *id = base; | |
1378 int j; | |
1379 int result = 0; | |
1380 | |
1381 //printf("BaseClass::fillVtbl(this='%s', cd='%s')\n", base->toChars(), cd->toChars()); | |
1382 if (vtbl) | |
1383 vtbl->setDim(base->vtbl.dim); | |
1384 | |
1385 // first entry is ClassInfo reference | |
1386 for (j = base->vtblOffset(); j < base->vtbl.dim; j++) | |
1387 { | |
1388 FuncDeclaration *ifd = ((Dsymbol *)base->vtbl.data[j])->isFuncDeclaration(); | |
1389 FuncDeclaration *fd; | |
1390 TypeFunction *tf; | |
1391 | |
1392 //printf(" vtbl[%d] is '%s'\n", j, ifd ? ifd->toChars() : "null"); | |
1393 | |
1394 assert(ifd); | |
1395 // Find corresponding function in this class | |
1396 tf = (ifd->type->ty == Tfunction) ? (TypeFunction *)(ifd->type) : NULL; | |
1397 fd = cd->findFunc(ifd->ident, tf); | |
1398 if (fd && !fd->isAbstract()) | |
1399 { | |
1400 //printf(" found\n"); | |
1401 // Check that calling conventions match | |
1402 if (fd->linkage != ifd->linkage) | |
1403 fd->error("linkage doesn't match interface function"); | |
1404 | |
1405 // Check that it is current | |
1406 if (newinstance && | |
1407 fd->toParent() != cd && | |
1408 ifd->toParent() == base) | |
1409 cd->error("interface function %s.%s is not implemented", | |
1410 id->toChars(), ifd->ident->toChars()); | |
1411 | |
1412 if (fd->toParent() == cd) | |
1413 result = 1; | |
1414 } | |
1415 else | |
1416 { | |
1417 //printf(" not found\n"); | |
1418 // BUG: should mark this class as abstract? | |
1419 if (!cd->isAbstract()) | |
1420 cd->error("interface function %s.%s isn't implemented", | |
1421 id->toChars(), ifd->ident->toChars()); | |
1422 fd = NULL; | |
1423 } | |
1424 if (vtbl) | |
1425 vtbl->data[j] = fd; | |
1426 } | |
1427 | |
1428 return result; | |
1429 } | |
1430 | |
1431 void BaseClass::copyBaseInterfaces(BaseClasses *vtblInterfaces) | |
1432 { | |
1433 //printf("+copyBaseInterfaces(), %s\n", base->toChars()); | |
1434 // if (baseInterfaces_dim) | |
1435 // return; | |
1436 | |
1437 baseInterfaces_dim = base->interfaces_dim; | |
1438 baseInterfaces = (BaseClass *)mem.calloc(baseInterfaces_dim, sizeof(BaseClass)); | |
1439 | |
1440 //printf("%s.copyBaseInterfaces()\n", base->toChars()); | |
1441 for (int i = 0; i < baseInterfaces_dim; i++) | |
1442 { | |
1443 BaseClass *b = &baseInterfaces[i]; | |
1444 BaseClass *b2 = base->interfaces[i]; | |
1445 | |
1446 assert(b2->vtbl.dim == 0); // should not be filled yet | |
1447 memcpy(b, b2, sizeof(BaseClass)); | |
1448 | |
1449 if (i) // single inheritance is i==0 | |
1450 vtblInterfaces->push(b); // only need for M.I. | |
1451 b->copyBaseInterfaces(vtblInterfaces); | |
1452 } | |
1453 //printf("-copyBaseInterfaces\n"); | |
1454 } |