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