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