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