72
|
1 module dmd.ClassDeclaration;
|
|
2
|
|
3 import dmd.AggregateDeclaration;
|
|
4 import dmd.InterfaceDeclaration;
|
|
5 import dmd.ThisDeclaration;
|
|
6 import dmd.CompoundStatement;
|
|
7 import dmd.DeleteDeclaration;
|
|
8 import dmd.NewDeclaration;
|
|
9 import dmd.CtorDeclaration;
|
|
10 import dmd.TypeIdentifier;
|
|
11 import dmd.STC;
|
|
12 import dmd.Argument;
|
|
13 import dmd.TypeTuple;
|
|
14 import dmd.TY;
|
|
15 import dmd.LINK;
|
|
16 import dmd.DsymbolTable;
|
|
17 import dmd.FuncDeclaration;
|
|
18 import dmd.Array;
|
|
19 import dmd.TypeClass;
|
|
20 import dmd.Module;
|
|
21 import dmd.Id;
|
|
22 import dmd.Type;
|
|
23 import dmd.OverloadSet;
|
|
24 import dmd.ArrayTypes;
|
|
25 import dmd.BaseClass;
|
|
26 import dmd.ClassInfoDeclaration;
|
|
27 import dmd.Loc;
|
|
28 import dmd.Identifier;
|
|
29 import dmd.Dsymbol;
|
|
30 import dmd.Scope;
|
|
31 import dmd.TypeFunction;
|
|
32 import dmd.OutBuffer;
|
|
33 import dmd.HdrGenState;
|
|
34 import dmd.VarDeclaration;
|
|
35 import dmd.Initializer;
|
|
36 import dmd.ExpInitializer;
|
|
37 import dmd.TypeSArray;
|
|
38 import dmd.ScopeDsymbol;
|
|
39 import dmd.PROT;
|
|
40 import dmd.Util;
|
|
41 import dmd.Global;
|
|
42
|
|
43 import dmd.expression.Util;
|
|
44
|
|
45 import dmd.backend.Symbol;
|
|
46 import dmd.backend.dt_t;
|
|
47 import dmd.backend.TYPE;
|
|
48 import dmd.backend.FL;
|
|
49 import dmd.backend.SFL;
|
|
50 import dmd.backend.mTY;
|
|
51 import dmd.backend.SC;
|
|
52 import dmd.backend.mTYman;
|
|
53 import dmd.backend.Util;
|
|
54 import dmd.backend.TYM;
|
|
55 import dmd.backend.Classsym;
|
|
56 import dmd.backend.glue;
|
|
57 import dmd.backend.RTLSYM;
|
|
58 import dmd.backend.LIST;
|
|
59
|
|
60 import dmd.codegen.Util;
|
|
61
|
|
62 import std.string;
|
|
63
|
|
64 version (DMDV2) {
|
|
65 enum CLASSINFO_SIZE = (0x3C+16+4); // value of ClassInfo.size
|
|
66 } else {
|
|
67 enum CLASSINFO_SIZE = (0x3C+12+4); // value of ClassInfo.size
|
|
68 }
|
|
69
|
|
70 enum OFFSET_RUNTIME = 0x76543210;
|
|
71
|
|
72 struct Param
|
|
73 {
|
|
74 int isf(void*, FuncDeclaration fd2)
|
|
75 {
|
|
76 //printf("param = %p, fd = %p %s\n", param, fd, fd.toChars());
|
|
77 return fd is fd2;
|
|
78 }
|
|
79
|
|
80 FuncDeclaration fd;
|
|
81 }
|
|
82
|
|
83 class ClassDeclaration : AggregateDeclaration
|
|
84 {
|
|
85 static __gshared ClassDeclaration object;
|
|
86 static __gshared ClassDeclaration classinfo;
|
|
87
|
|
88 ClassDeclaration baseClass; // null only if this is Object
|
|
89 FuncDeclaration staticCtor;
|
|
90 FuncDeclaration staticDtor;
|
|
91 Array vtbl; // Array of FuncDeclaration's making up the vtbl[]
|
|
92 Array vtblFinal; // More FuncDeclaration's that aren't in vtbl[]
|
|
93
|
|
94 BaseClasses baseclasses; // Array of BaseClass's; first is super,
|
|
95 // rest are Interface's
|
|
96
|
|
97 int interfaces_dim;
|
|
98 BaseClass* interfaces; // interfaces[interfaces_dim] for this class
|
|
99 // (does not include baseClass)
|
|
100
|
|
101 BaseClasses vtblInterfaces; // array of base interfaces that have
|
|
102 // their own vtbl[]
|
|
103
|
|
104 ClassInfoDeclaration vclassinfo; // the ClassInfo object for this ClassDeclaration
|
|
105 bool com; // true if this is a COM class (meaning
|
|
106 // it derives from IUnknown)
|
|
107 bool isauto; // true if this is an auto class
|
|
108 bool isabstract; // true if abstract class
|
|
109
|
|
110 int inuse; // to prevent recursive attempts
|
|
111
|
|
112 this(Loc loc, Identifier id, BaseClasses baseclasses)
|
|
113 {
|
|
114 super(loc, id);
|
|
115
|
|
116 vtbl = new Array();
|
|
117 vtblFinal = new Array();
|
|
118
|
|
119 enum msg = "only object.d can define this reserved class name";
|
|
120
|
|
121 if (baseclasses) {
|
|
122 this.baseclasses = baseclasses;
|
|
123 } else {
|
|
124 this.baseclasses = new BaseClasses();
|
|
125 }
|
|
126
|
|
127 //printf("ClassDeclaration(%s), dim = %d\n", id.toChars(), this.baseclasses.dim);
|
|
128
|
|
129 // For forward references
|
|
130 type = new TypeClass(this);
|
|
131
|
|
132 if (id)
|
|
133 {
|
|
134 // Look for special class names
|
|
135
|
|
136 if (id is Id.__sizeof || id is Id.alignof_ || id is Id.mangleof_)
|
|
137 error("illegal class name");
|
|
138
|
|
139 // BUG: What if this is the wrong TypeInfo, i.e. it is nested?
|
|
140 if (id.toChars()[0] == 'T')
|
|
141 {
|
|
142 if (id is Id.TypeInfo)
|
|
143 {
|
|
144 if (Type.typeinfo)
|
|
145 Type.typeinfo.error("%s", msg);
|
|
146 Type.typeinfo = this;
|
|
147 }
|
|
148
|
|
149 if (id is Id.TypeInfo_Class)
|
|
150 {
|
|
151 if (Type.typeinfoclass)
|
|
152 Type.typeinfoclass.error("%s", msg);
|
|
153 Type.typeinfoclass = this;
|
|
154 }
|
|
155
|
|
156 if (id is Id.TypeInfo_Interface)
|
|
157 {
|
|
158 if (Type.typeinfointerface)
|
|
159 Type.typeinfointerface.error("%s", msg);
|
|
160 Type.typeinfointerface = this;
|
|
161 }
|
|
162
|
|
163 if (id is Id.TypeInfo_Struct)
|
|
164 {
|
|
165 if (Type.typeinfostruct)
|
|
166 Type.typeinfostruct.error("%s", msg);
|
|
167 Type.typeinfostruct = this;
|
|
168 }
|
|
169
|
|
170 if (id is Id.TypeInfo_Typedef)
|
|
171 {
|
|
172 if (Type.typeinfotypedef)
|
|
173 Type.typeinfotypedef.error("%s", msg);
|
|
174 Type.typeinfotypedef = this;
|
|
175 }
|
|
176
|
|
177 if (id is Id.TypeInfo_Pointer)
|
|
178 {
|
|
179 if (Type.typeinfopointer)
|
|
180 Type.typeinfopointer.error("%s", msg);
|
|
181 Type.typeinfopointer = this;
|
|
182 }
|
|
183
|
|
184 if (id is Id.TypeInfo_Array)
|
|
185 {
|
|
186 if (Type.typeinfoarray)
|
|
187 Type.typeinfoarray.error("%s", msg);
|
|
188 Type.typeinfoarray = this;
|
|
189 }
|
|
190
|
|
191 if (id is Id.TypeInfo_StaticArray)
|
|
192 { //if (Type.typeinfostaticarray)
|
|
193 //Type.typeinfostaticarray.error("%s", msg);
|
|
194 Type.typeinfostaticarray = this;
|
|
195 }
|
|
196
|
|
197 if (id is Id.TypeInfo_AssociativeArray)
|
|
198 {
|
|
199 if (Type.typeinfoassociativearray)
|
|
200 Type.typeinfoassociativearray.error("%s", msg);
|
|
201 Type.typeinfoassociativearray = this;
|
|
202 }
|
|
203
|
|
204 if (id is Id.TypeInfo_Enum)
|
|
205 {
|
|
206 if (Type.typeinfoenum)
|
|
207 Type.typeinfoenum.error("%s", msg);
|
|
208 Type.typeinfoenum = this;
|
|
209 }
|
|
210
|
|
211 if (id is Id.TypeInfo_Function)
|
|
212 {
|
|
213 if (Type.typeinfofunction)
|
|
214 Type.typeinfofunction.error("%s", msg);
|
|
215 Type.typeinfofunction = this;
|
|
216 }
|
|
217
|
|
218 if (id is Id.TypeInfo_Delegate)
|
|
219 {
|
|
220 if (Type.typeinfodelegate)
|
|
221 Type.typeinfodelegate.error("%s", msg);
|
|
222 Type.typeinfodelegate = this;
|
|
223 }
|
|
224
|
|
225 if (id is Id.TypeInfo_Tuple)
|
|
226 {
|
|
227 if (Type.typeinfotypelist)
|
|
228 Type.typeinfotypelist.error("%s", msg);
|
|
229 Type.typeinfotypelist = this;
|
|
230 }
|
|
231
|
|
232 version (DMDV2) {
|
|
233 if (id is Id.TypeInfo_Const)
|
|
234 {
|
|
235 if (Type.typeinfoconst)
|
|
236 Type.typeinfoconst.error("%s", msg);
|
|
237 Type.typeinfoconst = this;
|
|
238 }
|
|
239
|
|
240 if (id is Id.TypeInfo_Invariant)
|
|
241 {
|
|
242 if (Type.typeinfoinvariant)
|
|
243 Type.typeinfoinvariant.error("%s", msg);
|
|
244 Type.typeinfoinvariant = this;
|
|
245 }
|
|
246
|
|
247 if (id is Id.TypeInfo_Shared)
|
|
248 {
|
|
249 if (Type.typeinfoshared)
|
|
250 Type.typeinfoshared.error("%s", msg);
|
|
251 Type.typeinfoshared = this;
|
|
252 }
|
|
253 }
|
|
254 }
|
|
255
|
|
256 if (id is Id.Object_)
|
|
257 {
|
|
258 if (object)
|
|
259 object.error("%s", msg);
|
|
260 object = this;
|
|
261 }
|
|
262
|
|
263 if (id is Id.ClassInfo)
|
|
264 {
|
|
265 if (classinfo)
|
|
266 classinfo.error("%s", msg);
|
|
267 classinfo = this;
|
|
268 }
|
|
269
|
|
270 if (id is Id.ModuleInfo)
|
|
271 {
|
|
272 if (Module.moduleinfo)
|
|
273 Module.moduleinfo.error("%s", msg);
|
|
274 Module.moduleinfo = this;
|
|
275 }
|
|
276 }
|
|
277
|
|
278 com = 0;
|
|
279 isauto = false;
|
|
280 isabstract = false;
|
|
281 inuse = 0;
|
|
282 }
|
|
283
|
|
284 override Dsymbol syntaxCopy(Dsymbol s)
|
|
285 {
|
|
286 ClassDeclaration cd;
|
|
287
|
|
288 //printf("ClassDeclaration.syntaxCopy('%s')\n", toChars());
|
|
289 if (s)
|
|
290 cd = cast(ClassDeclaration)s;
|
|
291 else
|
|
292 cd = new ClassDeclaration(loc, ident, null);
|
|
293
|
|
294 cd.storage_class |= storage_class;
|
|
295
|
|
296 cd.baseclasses.setDim(this.baseclasses.dim);
|
|
297 for (int i = 0; i < cd.baseclasses.dim; i++)
|
|
298 {
|
|
299 BaseClass b = cast(BaseClass)this.baseclasses.data[i];
|
|
300 BaseClass b2 = new BaseClass(b.type.syntaxCopy(), b.protection);
|
|
301 cd.baseclasses.data[i] = cast(void*)b2;
|
|
302 }
|
|
303
|
|
304 ScopeDsymbol.syntaxCopy(cd);
|
|
305 return cd;
|
|
306 }
|
|
307
|
|
308 override void semantic(Scope sc)
|
|
309 {
|
|
310 int i;
|
|
311 uint offset;
|
|
312
|
|
313 //printf("ClassDeclaration.semantic(%s), type = %p, sizeok = %d, this = %p\n", toChars(), type, sizeok, this);
|
|
314 //printf("\tparent = %p, '%s'\n", sc.parent, sc.parent ? sc.parent.toChars() : "");
|
|
315 //printf("sc.stc = %x\n", sc.stc);
|
|
316
|
|
317 //{ static int n; if (++n == 20) *(char*)0=0; }
|
|
318
|
|
319 if (!ident) // if anonymous class
|
|
320 {
|
|
321 string id = "__anonclass";
|
|
322 ident = Identifier.generateId(id);
|
|
323 }
|
|
324
|
|
325 if (!sc)
|
|
326 sc = scope_;
|
|
327
|
|
328 if (!parent && sc.parent && !sc.parent.isModule())
|
|
329 parent = sc.parent;
|
|
330
|
|
331 type = type.semantic(loc, sc);
|
|
332 handle = type;
|
|
333
|
|
334 if (!members) // if forward reference
|
|
335 {
|
|
336 //printf("\tclass '%s' is forward referenced\n", toChars());
|
|
337 return;
|
|
338 }
|
|
339 if (symtab)
|
|
340 { if (!scope_)
|
|
341 { //printf("\tsemantic for '%s' is already completed\n", toChars());
|
|
342 return; // semantic() already completed
|
|
343 }
|
|
344 }
|
|
345 else
|
|
346 symtab = new DsymbolTable();
|
|
347
|
|
348 Scope scx = null;
|
|
349 if (scope_)
|
|
350 { sc = scope_;
|
|
351 scx = scope_; // save so we don't make redundant copies
|
|
352 scope_ = null;
|
|
353 }
|
|
354 version (IN_GCC) {
|
|
355 methods.setDim(0);
|
|
356 }
|
|
357
|
|
358 if (sc.stc & STC.STCdeprecated)
|
|
359 {
|
|
360 isdeprecated = 1;
|
|
361 }
|
|
362
|
|
363 if (sc.linkage == LINK.LINKcpp)
|
|
364 error("cannot create C++ classes");
|
|
365
|
|
366 // Expand any tuples in baseclasses[]
|
|
367 for (i = 0; i < baseclasses.dim; )
|
|
368 {
|
|
369 BaseClass b = cast(BaseClass)baseclasses.data[i];
|
|
370 //printf("test1 %s %s\n", toChars(), b.type.toChars());
|
|
371 b.type = b.type.semantic(loc, sc);
|
|
372 //printf("test2\n");
|
|
373 Type tb = b.type.toBasetype();
|
|
374
|
|
375 if (tb.ty == TY.Ttuple)
|
|
376 {
|
|
377 TypeTuple tup = cast(TypeTuple)tb;
|
|
378 enum PROT protection = b.protection;
|
|
379 baseclasses.remove(i);
|
|
380 size_t dim = Argument.dim(tup.arguments);
|
|
381 for (size_t j = 0; j < dim; j++)
|
|
382 {
|
|
383 Argument arg = Argument.getNth(tup.arguments, j);
|
|
384 b = new BaseClass(arg.type, protection);
|
|
385 baseclasses.insert(i + j, cast(void*)b);
|
|
386 }
|
|
387 }
|
|
388 else
|
|
389 i++;
|
|
390 }
|
|
391
|
|
392 // See if there's a base class as first in baseclasses[]
|
|
393 if (baseclasses.dim)
|
|
394 {
|
|
395 TypeClass tc;
|
|
396 BaseClass b;
|
|
397 Type tb;
|
|
398
|
|
399 b = cast(BaseClass)baseclasses.data[0];
|
|
400 //b.type = b.type.semantic(loc, sc);
|
|
401 tb = b.type.toBasetype();
|
|
402 if (tb.ty != TY.Tclass)
|
|
403 { error("base type must be class or interface, not %s", b.type.toChars());
|
|
404 baseclasses.remove(0);
|
|
405 }
|
|
406 else
|
|
407 {
|
|
408 tc = cast(TypeClass)(tb);
|
|
409
|
|
410 if (tc.sym.isDeprecated())
|
|
411 {
|
|
412 if (!isDeprecated())
|
|
413 {
|
|
414 // Deriving from deprecated class makes this one deprecated too
|
|
415 isdeprecated = 1;
|
|
416
|
|
417 tc.checkDeprecated(loc, sc);
|
|
418 }
|
|
419 }
|
|
420
|
|
421 if (tc.sym.isInterfaceDeclaration()) {
|
|
422 ;
|
|
423 } else {
|
|
424 for (ClassDeclaration cdb = tc.sym; cdb; cdb = cdb.baseClass)
|
|
425 {
|
|
426 if (cdb == this)
|
|
427 {
|
|
428 error("circular inheritance");
|
|
429 baseclasses.remove(0);
|
|
430 goto L7;
|
|
431 }
|
|
432 }
|
|
433 if (!tc.sym.symtab || tc.sym.sizeok == 0)
|
|
434 { // Try to resolve forward reference
|
|
435 if (sc.mustsemantic && tc.sym.scope_)
|
|
436 tc.sym.semantic(null);
|
|
437 }
|
|
438 if (!tc.sym.symtab || tc.sym.scope_ || tc.sym.sizeok == 0)
|
|
439 {
|
|
440 //printf("%s: forward reference of base class %s\n", toChars(), tc.sym.toChars());
|
|
441 //error("forward reference of base class %s", baseClass.toChars());
|
|
442 // Forward reference of base class, try again later
|
|
443 //printf("\ttry later, forward reference of base class %s\n", tc.sym.toChars());
|
|
444 scope_ = scx ? scx : new Scope(sc);
|
|
445 scope_.setNoFree();
|
|
446 if (tc.sym.scope_)
|
|
447 tc.sym.scope_.module_.addDeferredSemantic(tc.sym);
|
|
448 scope_.module_.addDeferredSemantic(this);
|
|
449 return;
|
|
450 }
|
|
451 else
|
|
452 { baseClass = tc.sym;
|
|
453 b.base = baseClass;
|
|
454 }
|
|
455 L7: ;
|
|
456 }
|
|
457 }
|
|
458 }
|
|
459
|
|
460 // Treat the remaining entries in baseclasses as interfaces
|
|
461 // Check for errors, handle forward references
|
|
462 for (i = (baseClass ? 1 : 0); i < baseclasses.dim; )
|
|
463 { TypeClass tc;
|
|
464 BaseClass b;
|
|
465 Type tb;
|
|
466
|
|
467 b = cast(BaseClass)baseclasses.data[i];
|
|
468 b.type = b.type.semantic(loc, sc);
|
|
469 tb = b.type.toBasetype();
|
|
470 if (tb.ty == TY.Tclass)
|
|
471 tc = cast(TypeClass)tb;
|
|
472 else
|
|
473 tc = null;
|
|
474 if (!tc || !tc.sym.isInterfaceDeclaration())
|
|
475 {
|
|
476 error("base type must be interface, not %s", b.type.toChars());
|
|
477 baseclasses.remove(i);
|
|
478 continue;
|
|
479 }
|
|
480 else
|
|
481 {
|
|
482 if (tc.sym.isDeprecated())
|
|
483 {
|
|
484 if (!isDeprecated())
|
|
485 {
|
|
486 // Deriving from deprecated class makes this one deprecated too
|
|
487 isdeprecated = 1;
|
|
488
|
|
489 tc.checkDeprecated(loc, sc);
|
|
490 }
|
|
491 }
|
|
492
|
|
493 // Check for duplicate interfaces
|
|
494 for (size_t j = (baseClass ? 1 : 0); j < i; j++)
|
|
495 {
|
|
496 BaseClass b2 = cast(BaseClass)baseclasses.data[j];
|
|
497 if (b2.base == tc.sym)
|
|
498 error("inherits from duplicate interface %s", b2.base.toChars());
|
|
499 }
|
|
500
|
|
501 if (!tc.sym.symtab)
|
|
502 { // Try to resolve forward reference
|
|
503 if (sc.mustsemantic && tc.sym.scope_)
|
|
504 tc.sym.semantic(null);
|
|
505 }
|
|
506
|
|
507 b.base = tc.sym;
|
|
508 if (!b.base.symtab || b.base.scope_)
|
|
509 {
|
|
510 //error("forward reference of base class %s", baseClass.toChars());
|
|
511 // Forward reference of base, try again later
|
|
512 //printf("\ttry later, forward reference of base %s\n", baseClass.toChars());
|
|
513 scope_ = scx ? scx : new Scope(sc);
|
|
514 scope_.setNoFree();
|
|
515 if (tc.sym.scope_)
|
|
516 tc.sym.scope_.module_.addDeferredSemantic(tc.sym);
|
|
517 scope_.module_.addDeferredSemantic(this);
|
|
518 return;
|
|
519 }
|
|
520 }
|
|
521 i++;
|
|
522 }
|
|
523
|
|
524
|
|
525 // If no base class, and this is not an Object, use Object as base class
|
|
526 if (!baseClass && ident !is Id.Object_)
|
|
527 {
|
|
528 // BUG: what if Object is redefined in an inner scope?
|
|
529 Type tbase = new TypeIdentifier(Loc(0), Id.Object_);
|
|
530 BaseClass b;
|
|
531 TypeClass tc;
|
|
532 Type bt;
|
|
533
|
|
534 if (!object)
|
|
535 {
|
|
536 error("missing or corrupt object.d");
|
|
537 fatal();
|
|
538 }
|
|
539 bt = tbase.semantic(loc, sc).toBasetype();
|
|
540 b = new BaseClass(bt, PROT.PROTpublic);
|
|
541 baseclasses.shift(cast(void*)b);
|
|
542 assert(b.type.ty == TY.Tclass);
|
|
543 tc = cast(TypeClass)(b.type);
|
|
544 baseClass = tc.sym;
|
|
545 assert(!baseClass.isInterfaceDeclaration());
|
|
546 b.base = baseClass;
|
|
547 }
|
|
548
|
|
549 interfaces_dim = baseclasses.dim;
|
|
550 interfaces = cast(BaseClass*)baseclasses.data;
|
|
551
|
|
552 if (baseClass)
|
|
553 {
|
|
554 if (baseClass.storage_class & STC.STCfinal)
|
|
555 error("cannot inherit from final class %s", baseClass.toChars());
|
|
556
|
|
557 interfaces_dim--;
|
|
558 interfaces++;
|
|
559
|
|
560 // Copy vtbl[] from base class
|
|
561 vtbl.setDim(baseClass.vtbl.dim);
|
|
562 memcpy(vtbl.data, baseClass.vtbl.data, (void*).sizeof * vtbl.dim);
|
|
563
|
|
564 // Inherit properties from base class
|
|
565 com = baseClass.isCOMclass();
|
|
566 isauto = baseClass.isauto;
|
|
567 vthis = baseClass.vthis;
|
|
568 storage_class |= baseClass.storage_class & STC.STC_TYPECTOR;
|
|
569 }
|
|
570 else
|
|
571 {
|
|
572 // No base class, so this is the root of the class hierarchy
|
|
573 vtbl.setDim(0);
|
|
574 vtbl.push(cast(void*)this); // leave room for classinfo as first member
|
|
575 }
|
|
576
|
|
577 protection = sc.protection;
|
|
578 storage_class |= sc.stc;
|
|
579
|
|
580 if (sizeok == 0)
|
|
581 {
|
|
582 interfaceSemantic(sc);
|
|
583
|
|
584 for (i = 0; i < members.dim; i++)
|
|
585 {
|
|
586 Dsymbol s = cast(Dsymbol)members.data[i];
|
|
587 s.addMember(sc, this, true);
|
|
588 }
|
|
589
|
|
590 /* If this is a nested class, add the hidden 'this'
|
|
591 * member which is a pointer to the enclosing scope.
|
|
592 */
|
|
593 if (vthis) // if inheriting from nested class
|
|
594 { // Use the base class's 'this' member
|
|
595 isnested = true;
|
|
596 if (storage_class & STC.STCstatic)
|
|
597 error("static class cannot inherit from nested class %s", baseClass.toChars());
|
|
598 if (toParent2() != baseClass.toParent2())
|
|
599 {
|
|
600 if (toParent2())
|
|
601 {
|
|
602 error("is nested within %s, but super class %s is nested within %s",
|
|
603 toParent2().toChars(),
|
|
604 baseClass.toChars(),
|
|
605 baseClass.toParent2().toChars());
|
|
606 }
|
|
607 else
|
|
608 {
|
|
609 error("is not nested, but super class %s is nested within %s",
|
|
610 baseClass.toChars(),
|
|
611 baseClass.toParent2().toChars());
|
|
612 }
|
|
613 isnested = false;
|
|
614 }
|
|
615 }
|
|
616 else if (!(storage_class & STC.STCstatic))
|
|
617 {
|
|
618 Dsymbol s = toParent2();
|
|
619 if (s)
|
|
620 {
|
|
621 AggregateDeclaration ad = s.isClassDeclaration();
|
|
622 FuncDeclaration fd = s.isFuncDeclaration();
|
|
623
|
|
624 if (ad || fd)
|
|
625 { isnested = true;
|
|
626 Type t;
|
|
627 if (ad)
|
|
628 t = ad.handle;
|
|
629 else if (fd)
|
|
630 {
|
|
631 AggregateDeclaration ad2 = fd.isMember2();
|
|
632 if (ad2)
|
|
633 t = ad2.handle;
|
|
634 else
|
|
635 {
|
|
636 t = Type.tvoidptr;
|
|
637 }
|
|
638 }
|
|
639 else
|
|
640 assert(0);
|
|
641 if (t.ty == TY.Tstruct) // ref to struct
|
|
642 t = Type.tvoidptr;
|
|
643 assert(!vthis);
|
|
644 vthis = new ThisDeclaration(loc, t);
|
|
645 members.push(cast(void*)vthis);
|
|
646 }
|
|
647 }
|
|
648 }
|
|
649 }
|
|
650
|
|
651 if (storage_class & (STC.STCauto | STC.STCscope))
|
|
652 isauto = true;
|
|
653 if (storage_class & STC.STCabstract)
|
|
654 isabstract = true;
|
|
655 if (storage_class & STC.STCimmutable)
|
|
656 type = type.invariantOf();
|
|
657 else if (storage_class & STC.STCconst)
|
|
658 type = type.constOf();
|
|
659 else if (storage_class & STC.STCshared)
|
|
660 type = type.sharedOf();
|
|
661
|
|
662 sc = sc.push(this);
|
|
663 sc.stc &= ~(STC.STCfinal | STC.STCauto | STC.STCscope | STC.STCstatic |
|
|
664 STC.STCabstract | STC.STCdeprecated | STC.STC_TYPECTOR | STC.STCtls | STC.STCgshared);
|
|
665 sc.stc |= storage_class & STC.STC_TYPECTOR;
|
|
666 sc.parent = this;
|
|
667 sc.inunion = 0;
|
|
668
|
|
669 if (isCOMclass())
|
|
670 {
|
|
671 version (_WIN32) {
|
|
672 sc.linkage = LINK.LINKwindows;
|
|
673 } else {
|
|
674 /* This enables us to use COM objects under Linux and
|
|
675 * work with things like XPCOM
|
|
676 */
|
|
677 sc.linkage = LINK.LINKc;
|
|
678 }
|
|
679 }
|
|
680 sc.protection = PROT.PROTpublic;
|
|
681 sc.explicitProtection = 0;
|
|
682 sc.structalign = 8;
|
|
683 structalign = sc.structalign;
|
|
684 if (baseClass)
|
|
685 { sc.offset = baseClass.structsize;
|
|
686 alignsize = baseClass.alignsize;
|
|
687 // if (isnested)
|
|
688 // sc.offset += PTRSIZE; // room for uplevel context pointer
|
|
689 }
|
|
690 else
|
|
691 { sc.offset = PTRSIZE * 2; // allow room for __vptr and __monitor
|
|
692 alignsize = PTRSIZE;
|
|
693 }
|
|
694 structsize = sc.offset;
|
|
695 Scope scsave = sc; /// a copy must be created?
|
|
696 int members_dim = members.dim;
|
|
697 sizeok = 0;
|
|
698 for (i = 0; i < members_dim; i++)
|
|
699 {
|
|
700 Dsymbol s = cast(Dsymbol)members.data[i];
|
|
701 s.semantic(sc);
|
|
702 }
|
|
703
|
|
704 if (sizeok == 2)
|
|
705 { // semantic() failed because of forward references.
|
|
706 // Unwind what we did, and defer it for later
|
|
707 fields.setDim(0);
|
|
708 structsize = 0;
|
|
709 alignsize = 0;
|
|
710 structalign = 0;
|
|
711
|
|
712 sc = sc.pop();
|
|
713
|
|
714 scope_ = scx ? scx : new Scope(sc);
|
|
715 scope_.setNoFree();
|
|
716 scope_.module_.addDeferredSemantic(this);
|
|
717
|
|
718 //printf("\tsemantic('%s') failed due to forward references\n", toChars());
|
|
719 return;
|
|
720 }
|
|
721
|
|
722 //printf("\tsemantic('%s') successful\n", toChars());
|
|
723
|
|
724 structsize = sc.offset;
|
|
725 //members.print();
|
|
726
|
|
727 /* Look for special member functions.
|
|
728 * They must be in this class, not in a base class.
|
|
729 */
|
|
730 ctor = cast(CtorDeclaration)search(Loc(0), Id.ctor, 0);
|
|
731 if (ctor && (ctor.toParent() != this || !ctor.isCtorDeclaration()))
|
|
732 ctor = null;
|
|
733
|
|
734 // dtor = (DtorDeclaration *)search(Id.dtor, 0);
|
|
735 // if (dtor && dtor.toParent() != this)
|
|
736 // dtor = null;
|
|
737
|
|
738 // inv = (InvariantDeclaration *)search(Id.classInvariant, 0);
|
|
739 // if (inv && inv.toParent() != this)
|
|
740 // inv = null;
|
|
741
|
|
742 // Can be in base class
|
|
743 aggNew = cast(NewDeclaration)search(Loc(0), Id.classNew, 0);
|
|
744 aggDelete = cast(DeleteDeclaration)search(Loc(0), Id.classDelete, 0);
|
|
745
|
|
746 // If this class has no constructor, but base class does, create
|
|
747 // a constructor:
|
|
748 // this() { }
|
|
749 if (!ctor && baseClass && baseClass.ctor)
|
|
750 {
|
|
751 //printf("Creating default this(){} for class %s\n", toChars());
|
|
752 CtorDeclaration ctor = new CtorDeclaration(loc, Loc(0), null, 0);
|
|
753 ctor.fbody = new CompoundStatement(Loc(0), new Statements());
|
|
754 members.push(cast(void*)ctor);
|
|
755 ctor.addMember(sc, this, true);
|
|
756 sc = scsave; // why? What about sc.nofree? ///
|
|
757 sc.offset = structsize;
|
|
758 ctor.semantic(sc);
|
|
759 this.ctor = ctor;
|
|
760 defaultCtor = ctor;
|
|
761 }
|
|
762
|
|
763 static if (false) {
|
|
764 if (baseClass)
|
|
765 { if (!aggDelete)
|
|
766 aggDelete = baseClass.aggDelete;
|
|
767 if (!aggNew)
|
|
768 aggNew = baseClass.aggNew;
|
|
769 }
|
|
770 }
|
|
771
|
|
772 // Allocate instance of each new interface
|
|
773 for (i = 0; i < vtblInterfaces.dim; i++)
|
|
774 {
|
|
775 BaseClass b = cast(BaseClass)vtblInterfaces.data[i];
|
|
776 uint thissize = PTRSIZE;
|
|
777
|
|
778 alignmember(structalign, thissize, &sc.offset);
|
|
779 assert(b.offset == 0);
|
|
780 b.offset = sc.offset;
|
|
781
|
|
782 // Take care of single inheritance offsets
|
|
783 while (b.baseInterfaces.length)
|
|
784 {
|
|
785 b = b.baseInterfaces[0];
|
|
786 b.offset = sc.offset;
|
|
787 }
|
|
788
|
|
789 sc.offset += thissize;
|
|
790 if (alignsize < thissize)
|
|
791 alignsize = thissize;
|
|
792 }
|
|
793 structsize = sc.offset;
|
|
794 sizeok = 1;
|
|
795 Module.dprogress++;
|
|
796
|
|
797 dtor = buildDtor(sc);
|
|
798
|
|
799 sc.pop();
|
|
800
|
|
801 static if (false) { // Do not call until toObjfile() because of forward references
|
|
802 // Fill in base class vtbl[]s
|
|
803 for (i = 0; i < vtblInterfaces.dim; i++)
|
|
804 {
|
|
805 BaseClass b = cast(BaseClass)vtblInterfaces.data[i];
|
|
806
|
|
807 //b.fillVtbl(this, &b.vtbl, 1);
|
|
808 }
|
|
809 }
|
|
810 //printf("-ClassDeclaration.semantic(%s), type = %p\n", toChars(), type);
|
|
811 }
|
|
812
|
|
813 override void toCBuffer(OutBuffer buf, HdrGenState* hgs)
|
|
814 {
|
|
815 assert(false);
|
|
816 }
|
|
817
|
|
818 /*********************************************
|
|
819 * Determine if 'this' is a base class of cd.
|
|
820 * This is used to detect circular inheritance only.
|
|
821 */
|
|
822 int isBaseOf2(ClassDeclaration cd)
|
|
823 {
|
|
824 if (!cd)
|
|
825 return 0;
|
|
826 //printf("ClassDeclaration::isBaseOf2(this = '%s', cd = '%s')\n", toChars(), cd.toChars());
|
|
827 for (int i = 0; i < cd.baseclasses.dim; i++)
|
|
828 {
|
|
829 BaseClass b = cast(BaseClass)cd.baseclasses.data[i];
|
|
830
|
|
831 if (b.base is this || isBaseOf2(b.base))
|
|
832 return 1;
|
|
833 }
|
|
834 return 0;
|
|
835 }
|
|
836
|
|
837 /*******************************************
|
|
838 * Determine if 'this' is a base class of cd.
|
|
839 */
|
|
840 /// #define OFFSET_RUNTIME 0x76543210
|
|
841 bool isBaseOf(ClassDeclaration cd, int* poffset)
|
|
842 {
|
|
843 if (!cd)
|
|
844 return 0;
|
|
845 //printf("ClassDeclaration::isBaseOf2(this = '%s', cd = '%s')\n", toChars(), cd.toChars());
|
|
846 for (int i = 0; i < cd.baseclasses.dim; i++)
|
|
847 {
|
|
848 BaseClass b = cast(BaseClass)cd.baseclasses.data[i];
|
|
849
|
|
850 if (b.base == this || isBaseOf2(b.base))
|
|
851 return 1;
|
|
852 }
|
|
853
|
|
854 return 0;
|
|
855 }
|
|
856
|
|
857 override Dsymbol search(Loc, Identifier ident, int flags)
|
|
858 {
|
|
859 Dsymbol s;
|
|
860 //printf("%s.ClassDeclaration.search('%s')\n", toChars(), ident.toChars());
|
|
861
|
|
862 if (scope_)
|
|
863 {
|
|
864 Scope sc = scope_;
|
|
865 sc.mustsemantic++;
|
|
866 semantic(sc);
|
|
867 sc.mustsemantic--;
|
|
868 }
|
|
869
|
|
870 if (!members || !symtab || scope_)
|
|
871 {
|
|
872 error("is forward referenced when looking for '%s'", ident.toChars());
|
|
873 //*(char*)0=0;
|
|
874 return null;
|
|
875 }
|
|
876
|
|
877 s = ScopeDsymbol.search(loc, ident, flags);
|
|
878 if (!s)
|
|
879 {
|
|
880 // Search bases classes in depth-first, left to right order
|
|
881
|
|
882 int i;
|
|
883
|
|
884 for (i = 0; i < baseclasses.dim; i++)
|
|
885 {
|
|
886 BaseClass b = cast(BaseClass)baseclasses.data[i];
|
|
887
|
|
888 if (b.base)
|
|
889 {
|
|
890 if (!b.base.symtab)
|
|
891 error("base %s is forward referenced", b.base.ident.toChars());
|
|
892 else
|
|
893 {
|
|
894 s = b.base.search(loc, ident, flags);
|
|
895 if (s is this) // happens if s is nested in this and derives from this
|
|
896 s = null;
|
|
897 else if (s)
|
|
898 break;
|
|
899 }
|
|
900 }
|
|
901 }
|
|
902 }
|
|
903 return s;
|
|
904 }
|
|
905
|
|
906 version (DMDV2) {
|
|
907 bool isFuncHidden(FuncDeclaration fd)
|
|
908 {
|
|
909 //printf("ClassDeclaration::isFuncHidden(class = %s, fd = %s)\n", toChars(), fd.toChars());
|
|
910 Dsymbol s = search(Loc(0), fd.ident, 4|2);
|
|
911 if (!s)
|
|
912 {
|
|
913 //printf("not found\n");
|
|
914 /* Because, due to a hack, if there are multiple definitions
|
|
915 * of fd.ident, null is returned.
|
|
916 */
|
|
917 return false;
|
|
918 }
|
|
919
|
|
920 Param p; p.fd = fd;
|
|
921
|
|
922 s = s.toAlias();
|
|
923 OverloadSet os = s.isOverloadSet();
|
|
924 if (os)
|
|
925 {
|
|
926 for (int i = 0; i < os.a.dim; i++)
|
|
927 {
|
|
928 Dsymbol s2 = cast(Dsymbol)os.a.data[i];
|
|
929 FuncDeclaration f2 = s2.isFuncDeclaration();
|
|
930 if (f2 && overloadApply(f2, &p.isf, &p))
|
|
931 return false;
|
|
932 }
|
|
933 return true;
|
|
934 }
|
|
935 else
|
|
936 {
|
|
937 FuncDeclaration fdstart = s.isFuncDeclaration();
|
|
938 //printf("%s fdstart = %p\n", s.kind(), fdstart);
|
|
939 return !overloadApply(fdstart, &p.isf, &p);
|
|
940 }
|
|
941 }
|
|
942 }
|
|
943 FuncDeclaration findFunc(Identifier ident, TypeFunction tf)
|
|
944 {
|
|
945 //printf("ClassDeclaration.findFunc(%s, %s) %s\n", ident.toChars(), tf.toChars(), toChars());
|
|
946
|
|
947 ClassDeclaration cd = this;
|
|
948 Array vtbl = cd.vtbl;
|
|
949 while (true)
|
|
950 {
|
|
951 for (size_t i = 0; i < vtbl.dim; i++)
|
|
952 {
|
|
953 FuncDeclaration fd = (cast(Dsymbol)vtbl.data[i]).isFuncDeclaration();
|
|
954 if (!fd)
|
|
955 continue; // the first entry might be a ClassInfo
|
|
956
|
|
957 //printf("\t[%d] = %s\n", i, fd.toChars());
|
|
958 if (ident == fd.ident &&
|
|
959 //tf.equals(fd.type)
|
|
960 fd.type.covariant(tf) == 1
|
|
961 )
|
|
962 { //printf("\t\tfound\n");
|
|
963 return fd;
|
|
964 }
|
|
965 //else printf("\t\t%d\n", fd.type.covariant(tf));
|
|
966 }
|
|
967 if (!cd)
|
|
968 break;
|
|
969
|
|
970 vtbl = cd.vtblFinal;
|
|
971 cd = cd.baseClass;
|
|
972 }
|
|
973
|
|
974 return null;
|
|
975 }
|
|
976
|
|
977 void interfaceSemantic(Scope sc)
|
|
978 {
|
|
979 InterfaceDeclaration id = isInterfaceDeclaration();
|
|
980
|
|
981 vtblInterfaces = new BaseClasses();
|
|
982 vtblInterfaces.reserve(interfaces_dim);
|
|
983
|
|
984 for (size_t i = 0; i < interfaces_dim; i++)
|
|
985 {
|
|
986 BaseClass b = interfaces[i];
|
|
987
|
|
988 // If this is an interface, and it derives from a COM interface,
|
|
989 // then this is a COM interface too.
|
|
990 if (b.base.isCOMinterface())
|
|
991 com = 1;
|
|
992
|
|
993 if (b.base.isCPPinterface() && id)
|
|
994 id.cpp = 1;
|
|
995
|
|
996 vtblInterfaces.push(cast(void*)b);
|
|
997 b.copyBaseInterfaces(vtblInterfaces);
|
|
998 }
|
|
999 }
|
|
1000
|
|
1001 bool isCOMclass()
|
|
1002 {
|
|
1003 return com;
|
|
1004 }
|
|
1005
|
|
1006 bool isCOMinterface()
|
|
1007 {
|
|
1008 return false;
|
|
1009 }
|
|
1010
|
|
1011 version (DMDV2) {
|
|
1012 bool isCPPinterface()
|
|
1013 {
|
|
1014 return false;
|
|
1015 }
|
|
1016 }
|
|
1017 bool isAbstract()
|
|
1018 {
|
|
1019 if (isabstract)
|
|
1020 return true;
|
|
1021
|
|
1022 for (int i = 1; i < vtbl.dim; i++)
|
|
1023 {
|
|
1024 FuncDeclaration fd = (cast(Dsymbol)vtbl.data[i]).isFuncDeclaration();
|
|
1025
|
|
1026 //printf("\tvtbl[%d] = %p\n", i, fd);
|
|
1027 if (!fd || fd.isAbstract())
|
|
1028 {
|
|
1029 isabstract = true;
|
|
1030 return true;
|
|
1031 }
|
|
1032 }
|
|
1033
|
|
1034 return false;
|
|
1035 }
|
|
1036
|
|
1037 int vtblOffset()
|
|
1038 {
|
|
1039 assert(false);
|
|
1040 }
|
|
1041
|
|
1042 override string kind()
|
|
1043 {
|
|
1044 return "class";
|
|
1045 }
|
|
1046
|
|
1047 override string mangle()
|
|
1048 {
|
|
1049 Dsymbol parentsave = parent;
|
|
1050
|
|
1051 //printf("ClassDeclaration.mangle() %s.%s\n", parent.toChars(), toChars());
|
|
1052
|
|
1053 /* These are reserved to the compiler, so keep simple
|
|
1054 * names for them.
|
|
1055 */
|
|
1056 if (ident is Id.Exception)
|
|
1057 {
|
|
1058 if (parent.ident is Id.object)
|
|
1059 parent = null;
|
|
1060 }
|
|
1061 else if (ident is Id.TypeInfo ||
|
|
1062 // ident is Id.Exception ||
|
|
1063 ident is Id.TypeInfo_Struct ||
|
|
1064 ident is Id.TypeInfo_Class ||
|
|
1065 ident is Id.TypeInfo_Typedef ||
|
|
1066 ident is Id.TypeInfo_Tuple ||
|
|
1067 this is object ||
|
|
1068 this is classinfo ||
|
|
1069 this is Module.moduleinfo ||
|
|
1070 ident.toChars().startsWith("TypeInfo_")
|
|
1071 )
|
|
1072 {
|
|
1073 parent = null;
|
|
1074 }
|
|
1075
|
|
1076 string id = Dsymbol.mangle();
|
|
1077 parent = parentsave;
|
|
1078 return id;
|
|
1079 }
|
|
1080
|
|
1081 override void toDocBuffer(OutBuffer buf)
|
|
1082 {
|
|
1083 assert(false);
|
|
1084 }
|
|
1085
|
|
1086 override PROT getAccess(Dsymbol smember) // determine access to smember
|
|
1087 {
|
|
1088 PROT access_ret = PROT.PROTnone;
|
|
1089
|
|
1090 version (LOG) {
|
|
1091 printf("+ClassDeclaration::getAccess(this = '%s', smember = '%s')\n",
|
|
1092 toChars(), smember.toChars());
|
|
1093 }
|
|
1094 if (smember.toParent() is this)
|
|
1095 {
|
|
1096 access_ret = smember.prot();
|
|
1097 }
|
|
1098 else
|
|
1099 {
|
|
1100 PROT access;
|
|
1101 int i;
|
|
1102
|
|
1103 if (smember.isDeclaration().isStatic())
|
|
1104 {
|
|
1105 access_ret = smember.prot();
|
|
1106 }
|
|
1107
|
|
1108 for (i = 0; i < baseclasses.dim; i++)
|
|
1109 {
|
|
1110 BaseClass b = cast(BaseClass)baseclasses.data[i];
|
|
1111
|
|
1112 access = b.base.getAccess(smember);
|
|
1113 switch (access)
|
|
1114 {
|
|
1115 case PROT.PROTnone:
|
|
1116 break;
|
|
1117
|
|
1118 case PROT.PROTprivate:
|
|
1119 access = PROT.PROTnone; // private members of base class not accessible
|
|
1120 break;
|
|
1121
|
|
1122 case PROT.PROTpackage:
|
|
1123 case PROT.PROTprotected:
|
|
1124 case PROT.PROTpublic:
|
|
1125 case PROT.PROTexport:
|
|
1126 // If access is to be tightened
|
|
1127 if (b.protection < access)
|
|
1128 access = b.protection;
|
|
1129
|
|
1130 // Pick path with loosest access
|
|
1131 if (access > access_ret)
|
|
1132 access_ret = access;
|
|
1133 break;
|
|
1134
|
|
1135 default:
|
|
1136 assert(0);
|
|
1137 }
|
|
1138 }
|
|
1139 }
|
|
1140
|
|
1141 version (LOG) {
|
|
1142 printf("-ClassDeclaration::getAccess(this = '%s', smember = '%s') = %d\n",
|
|
1143 toChars(), smember.toChars(), access_ret);
|
|
1144 }
|
|
1145
|
|
1146 return access_ret;
|
|
1147 }
|
|
1148
|
|
1149 override void addLocalClass(ClassDeclarations aclasses)
|
|
1150 {
|
|
1151 aclasses.push(cast(void*)this);
|
|
1152 }
|
|
1153
|
|
1154 // Back end
|
|
1155 override void toObjFile(int multiobj) // compile to .obj file
|
|
1156 {
|
|
1157 uint i;
|
|
1158 uint offset;
|
|
1159 Symbol* sinit;
|
|
1160 enum_SC scclass;
|
|
1161
|
|
1162 //printf("ClassDeclaration.toObjFile('%s')\n", toChars());
|
|
1163
|
|
1164 if (!members)
|
|
1165 return;
|
|
1166
|
|
1167 if (multiobj)
|
|
1168 {
|
|
1169 obj_append(this);
|
|
1170 return;
|
|
1171 }
|
|
1172
|
|
1173 if (global.params.symdebug)
|
|
1174 toDebug();
|
|
1175
|
|
1176 assert(!scope_); // semantic() should have been run to completion
|
|
1177
|
|
1178 scclass = SCglobal;
|
|
1179 if (inTemplateInstance())
|
|
1180 scclass = SCcomdat;
|
|
1181
|
|
1182 // Put out the members
|
|
1183 for (i = 0; i < members.dim; i++)
|
|
1184 {
|
|
1185 Dsymbol member;
|
|
1186
|
|
1187 member = cast(Dsymbol)members.data[i];
|
|
1188 member.toObjFile(0);
|
|
1189 }
|
|
1190
|
|
1191 static if (false) {
|
|
1192 // Build destructor by aggregating dtors[]
|
|
1193 Symbol* sdtor;
|
|
1194 switch (dtors.dim)
|
|
1195 {
|
|
1196 case 0:
|
|
1197 // No destructors for this class
|
|
1198 sdtor = null;
|
|
1199 break;
|
|
1200
|
|
1201 case 1:
|
|
1202 // One destructor, just use it directly
|
|
1203 sdtor = (cast(DtorDeclaration)dtors.data[0]).toSymbol();
|
|
1204 break;
|
|
1205
|
|
1206 default:
|
|
1207 {
|
|
1208 /* Build a destructor that calls all the
|
|
1209 * other destructors in dtors[].
|
|
1210 */
|
|
1211
|
|
1212 elem* edtor = null;
|
|
1213
|
|
1214 // Declare 'this' pointer for our new destructor
|
|
1215 Symbol* sthis = symbol_calloc("this");
|
|
1216 sthis.Stype = type_fake(TYnptr);
|
|
1217 sthis.Stype.Tcount++;
|
|
1218 sthis.Sclass = SCfastpar;
|
|
1219 sthis.Spreg = AX;
|
|
1220 sthis.Sfl = FLauto;
|
|
1221
|
|
1222 // Call each of the destructors in dtors[]
|
|
1223 // in reverse order
|
|
1224 for (i = 0; i < dtors.dim; i++)
|
|
1225 {
|
|
1226 DtorDeclaration d = cast(DtorDeclaration)dtors.data[i];
|
|
1227 Symbol* s = d.toSymbol();
|
|
1228 elem* e = el_bin(OPcall, TYvoid, el_var(s), el_var(sthis));
|
|
1229 edtor = el_combine(e, edtor);
|
|
1230 }
|
|
1231
|
|
1232 // Create type for the function
|
|
1233 .type* t = type_alloc(TYjfunc);
|
|
1234 t.Tflags |= TFprototype | TFfixed;
|
|
1235 t.Tmangle = mTYman_d;
|
|
1236 t.Tnext = tsvoid;
|
|
1237 tsvoid.Tcount++;
|
|
1238
|
|
1239 // Create the function, sdtor, and write it out
|
|
1240 localgot = null;
|
|
1241 sdtor = toSymbolX("__dtor", SCglobal, t, "FZv");
|
|
1242 block* b = block_calloc();
|
|
1243 b.BC = BCret;
|
|
1244 b.Belem = edtor;
|
|
1245 sdtor.Sfunc.Fstartblock = b;
|
|
1246 cstate.CSpsymtab = &sdtor.Sfunc.Flocsym;
|
|
1247 symbol_add(sthis);
|
|
1248 writefunc(sdtor);
|
|
1249 }
|
|
1250 }
|
|
1251 }
|
|
1252
|
|
1253 // Generate C symbols
|
|
1254 toSymbol();
|
|
1255 toVtblSymbol();
|
|
1256 sinit = toInitializer();
|
|
1257
|
|
1258 //////////////////////////////////////////////
|
|
1259
|
|
1260 // Generate static initializer
|
|
1261 sinit.Sclass = scclass;
|
|
1262 sinit.Sfl = FLdata;
|
|
1263 version (ELFOBJ) { // Burton
|
|
1264 sinit.Sseg = Segment.CDATA;
|
|
1265 }
|
|
1266 version (MACHOBJ) {
|
|
1267 sinit.Sseg = Segment.DATA;
|
|
1268 }
|
|
1269 toDt(&sinit.Sdt);
|
|
1270 outdata(sinit);
|
|
1271
|
|
1272 //////////////////////////////////////////////
|
|
1273
|
|
1274 // Put out the TypeInfo
|
|
1275 type.getTypeInfo(null);
|
|
1276 type.vtinfo.toObjFile(multiobj);
|
|
1277
|
|
1278 //////////////////////////////////////////////
|
|
1279
|
|
1280 // Put out the ClassInfo
|
|
1281 csym.Sclass = scclass;
|
|
1282 csym.Sfl = FLdata;
|
|
1283
|
|
1284 /* The layout is:
|
|
1285 {
|
|
1286 void **vptr;
|
|
1287 monitor_t monitor;
|
|
1288 byte[] initializer; // static initialization data
|
|
1289 char[] name; // class name
|
|
1290 void *[] vtbl;
|
|
1291 Interface[] interfaces;
|
|
1292 ClassInfo *base; // base class
|
|
1293 void *destructor;
|
|
1294 void *invariant; // class invariant
|
|
1295 uint flags;
|
|
1296 void *deallocator;
|
|
1297 OffsetTypeInfo[] offTi;
|
|
1298 void *defaultConstructor;
|
|
1299 const(MemberInfo[]) function(string) xgetMembers; // module getMembers() function
|
|
1300 TypeInfo typeinfo;
|
|
1301 }
|
|
1302 */
|
|
1303 dt_t* dt = null;
|
|
1304 offset = CLASSINFO_SIZE; // must be ClassInfo.size
|
|
1305 if (classinfo)
|
|
1306 {
|
|
1307 if (classinfo.structsize != CLASSINFO_SIZE)
|
|
1308 error("D compiler and phobos' object.d are mismatched");
|
|
1309 }
|
|
1310
|
|
1311 if (classinfo)
|
|
1312 dtxoff(&dt, classinfo.toVtblSymbol(), 0, TYnptr); // vtbl for ClassInfo
|
|
1313 else
|
|
1314 dtdword(&dt, 0); // BUG: should be an assert()
|
|
1315
|
|
1316 dtdword(&dt, 0); // monitor
|
|
1317
|
|
1318 // initializer[]
|
|
1319 assert(structsize >= 8);
|
|
1320 dtdword(&dt, structsize); // size
|
|
1321 dtxoff(&dt, sinit, 0, TYnptr); // initializer
|
|
1322
|
|
1323 // name[]
|
|
1324 string name = ident.toChars();
|
|
1325 size_t namelen = name.length;
|
|
1326 if (!(namelen > 9 && name[0..9] == "TypeInfo_"))
|
|
1327 {
|
|
1328 name = toPrettyChars();
|
|
1329 namelen = name.length;
|
|
1330 }
|
|
1331 dtdword(&dt, namelen);
|
|
1332 dtabytes(&dt, TYnptr, 0, namelen + 1, toStringz(name));
|
|
1333
|
|
1334 // vtbl[]
|
|
1335 dtdword(&dt, vtbl.dim);
|
|
1336 dtxoff(&dt, vtblsym, 0, TYnptr);
|
|
1337
|
|
1338 // interfaces[]
|
|
1339 dtdword(&dt, vtblInterfaces.dim);
|
|
1340 if (vtblInterfaces.dim)
|
|
1341 dtxoff(&dt, csym, offset, TYnptr); // (*)
|
|
1342 else
|
|
1343 dtdword(&dt, 0);
|
|
1344
|
|
1345 // base
|
|
1346 if (baseClass)
|
|
1347 dtxoff(&dt, baseClass.toSymbol(), 0, TYnptr);
|
|
1348 else
|
|
1349 dtdword(&dt, 0);
|
|
1350
|
|
1351 // destructor
|
|
1352 if (dtor)
|
|
1353 dtxoff(&dt, dtor.toSymbol(), 0, TYnptr);
|
|
1354 else
|
|
1355 dtdword(&dt, 0);
|
|
1356
|
|
1357 // invariant
|
|
1358 if (inv)
|
|
1359 dtxoff(&dt, inv.toSymbol(), 0, TYnptr);
|
|
1360 else
|
|
1361 dtdword(&dt, 0);
|
|
1362
|
|
1363 // flags
|
|
1364 int flags = 4 | isCOMclass();
|
|
1365 version (DMDV2) {
|
|
1366 flags |= 16;
|
|
1367 }
|
|
1368 flags |= 32;
|
|
1369
|
|
1370 if (ctor)
|
|
1371 flags |= 8;
|
|
1372 for (ClassDeclaration cd = this; cd; cd = cd.baseClass)
|
|
1373 {
|
|
1374 if (cd.members)
|
|
1375 {
|
|
1376 for (size_t j = 0; j < cd.members.dim; j++)
|
|
1377 {
|
|
1378 Dsymbol sm = cast(Dsymbol)cd.members.data[j];
|
|
1379 //printf("sm = %s %s\n", sm.kind(), sm.toChars());
|
|
1380 if (sm.hasPointers())
|
|
1381 goto L2;
|
|
1382 }
|
|
1383 }
|
|
1384 }
|
|
1385 flags |= 2; // no pointers
|
|
1386 L2:
|
|
1387 dtdword(&dt, flags);
|
|
1388
|
|
1389
|
|
1390 // deallocator
|
|
1391 if (aggDelete)
|
|
1392 dtxoff(&dt, aggDelete.toSymbol(), 0, TYnptr);
|
|
1393 else
|
|
1394 dtdword(&dt, 0);
|
|
1395
|
|
1396 // offTi[]
|
|
1397 dtdword(&dt, 0);
|
|
1398 dtdword(&dt, 0); // null for now, fix later
|
|
1399
|
|
1400 // defaultConstructor
|
|
1401 if (defaultCtor)
|
|
1402 dtxoff(&dt, defaultCtor.toSymbol(), 0, TYnptr);
|
|
1403 else
|
|
1404 dtdword(&dt, 0);
|
|
1405
|
|
1406 version (DMDV2) {
|
|
1407 FuncDeclaration sgetmembers = findGetMembers();
|
|
1408 if (sgetmembers)
|
|
1409 dtxoff(&dt, sgetmembers.toSymbol(), 0, TYnptr);
|
|
1410 else
|
|
1411 dtdword(&dt, 0); // module getMembers() function
|
|
1412 }
|
|
1413
|
|
1414 dtxoff(&dt, type.vtinfo.toSymbol(), 0, TYnptr); // typeinfo
|
|
1415 //dtdword(&dt, 0);
|
|
1416
|
|
1417 //////////////////////////////////////////////
|
|
1418
|
|
1419 // Put out vtblInterfaces.data[]. Must immediately follow csym, because
|
|
1420 // of the fixup (*)
|
|
1421
|
|
1422 offset += vtblInterfaces.dim * (4 * PTRSIZE);
|
|
1423 for (i = 0; i < vtblInterfaces.dim; i++)
|
|
1424 {
|
|
1425 BaseClass b = cast(BaseClass)vtblInterfaces.data[i];
|
|
1426 ClassDeclaration id = b.base;
|
|
1427
|
|
1428 /* The layout is:
|
|
1429 * {
|
|
1430 * ClassInfo *interface;
|
|
1431 * void *[] vtbl;
|
|
1432 * unsigned offset;
|
|
1433 * }
|
|
1434 */
|
|
1435
|
|
1436 // Fill in vtbl[]
|
|
1437 b.fillVtbl(this, b.vtbl, 1);
|
|
1438
|
|
1439 dtxoff(&dt, id.toSymbol(), 0, TYnptr); // ClassInfo
|
|
1440
|
|
1441 // vtbl[]
|
|
1442 dtdword(&dt, id.vtbl.dim);
|
|
1443 dtxoff(&dt, csym, offset, TYnptr);
|
|
1444
|
|
1445 dtdword(&dt, b.offset); // this offset
|
|
1446
|
|
1447 offset += id.vtbl.dim * PTRSIZE;
|
|
1448 }
|
|
1449
|
|
1450 // Put out the vtblInterfaces.data[].vtbl[]
|
|
1451 // This must be mirrored with ClassDeclaration.baseVtblOffset()
|
|
1452 //printf("putting out %d interface vtbl[]s for '%s'\n", vtblInterfaces.dim, toChars());
|
|
1453 for (i = 0; i < vtblInterfaces.dim; i++)
|
|
1454 {
|
|
1455 BaseClass b = cast(BaseClass)vtblInterfaces.data[i];
|
|
1456 ClassDeclaration id = b.base;
|
|
1457 int j;
|
|
1458
|
|
1459 //printf(" interface[%d] is '%s'\n", i, id.toChars());
|
|
1460 j = 0;
|
|
1461 if (id.vtblOffset())
|
|
1462 {
|
|
1463 // First entry is ClassInfo reference
|
|
1464 //dtxoff(&dt, id.toSymbol(), 0, TYnptr);
|
|
1465
|
|
1466 // First entry is struct Interface reference
|
|
1467 dtxoff(&dt, csym, CLASSINFO_SIZE + i * (4 * PTRSIZE), TYnptr);
|
|
1468 j = 1;
|
|
1469 }
|
|
1470
|
|
1471 assert(id.vtbl.dim == b.vtbl.dim);
|
|
1472 for (; j < id.vtbl.dim; j++)
|
|
1473 {
|
|
1474 FuncDeclaration fd;
|
|
1475
|
|
1476 assert(j < b.vtbl.dim);
|
|
1477 static if (false) {
|
|
1478 Object o = cast(Object)b.vtbl.data[j];
|
|
1479 if (o)
|
|
1480 {
|
|
1481 printf("o = %p\n", o);
|
|
1482 assert(o.dyncast() == DYNCAST_DSYMBOL);
|
|
1483 Dsymbol s = cast(Dsymbol)o;
|
|
1484 printf("s.kind() = '%s'\n", s.kind());
|
|
1485 }
|
|
1486 }
|
|
1487 fd = cast(FuncDeclaration)b.vtbl.data[j];
|
|
1488 if (fd)
|
|
1489 dtxoff(&dt, fd.toThunkSymbol(b.offset), 0, TYnptr);
|
|
1490 else
|
|
1491 dtdword(&dt, 0);
|
|
1492 }
|
|
1493 }
|
|
1494
|
|
1495 static if (true) {
|
|
1496 // Put out the overriding interface vtbl[]s.
|
|
1497 // This must be mirrored with ClassDeclaration.baseVtblOffset()
|
|
1498 //printf("putting out overriding interface vtbl[]s for '%s' at offset x%x\n", toChars(), offset);
|
|
1499 ClassDeclaration cd;
|
|
1500 scope Array bvtbl = new Array();
|
|
1501
|
|
1502 for (cd = this.baseClass; cd; cd = cd.baseClass)
|
|
1503 {
|
|
1504 for (int k = 0; k < cd.vtblInterfaces.dim; k++)
|
|
1505 {
|
|
1506 BaseClass bs = cast(BaseClass)cd.vtblInterfaces.data[k];
|
|
1507
|
|
1508 if (bs.fillVtbl(this, bvtbl, 0))
|
|
1509 {
|
|
1510 //printf("\toverriding vtbl[] for %s\n", bs.base.toChars());
|
|
1511 ClassDeclaration id = bs.base;
|
|
1512 int j;
|
|
1513
|
|
1514 j = 0;
|
|
1515 if (id.vtblOffset())
|
|
1516 {
|
|
1517 // First entry is ClassInfo reference
|
|
1518 //dtxoff(&dt, id.toSymbol(), 0, TYnptr);
|
|
1519
|
|
1520 // First entry is struct Interface reference
|
|
1521 dtxoff(&dt, cd.toSymbol(), CLASSINFO_SIZE + k * (4 * PTRSIZE), TYnptr);
|
|
1522 j = 1;
|
|
1523 }
|
|
1524
|
|
1525 for (; j < id.vtbl.dim; j++)
|
|
1526 {
|
|
1527 assert(j < bvtbl.dim);
|
|
1528 FuncDeclaration fd = cast(FuncDeclaration)bvtbl.data[j];
|
|
1529 if (fd)
|
|
1530 dtxoff(&dt, fd.toThunkSymbol(bs.offset), 0, TYnptr);
|
|
1531 else
|
|
1532 dtdword(&dt, 0);
|
|
1533 }
|
|
1534 }
|
|
1535 }
|
|
1536 }
|
|
1537 }
|
|
1538
|
|
1539 version (INTERFACE_VIRTUAL) {
|
|
1540 // Put out the overriding interface vtbl[]s.
|
|
1541 // This must be mirrored with ClassDeclaration.baseVtblOffset()
|
|
1542 //printf("putting out overriding interface vtbl[]s for '%s' at offset x%x\n", toChars(), offset);
|
|
1543 for (i = 0; i < vtblInterfaces.dim; i++)
|
|
1544 {
|
|
1545 BaseClass b = cast(BaseClass)vtblInterfaces.data[i];
|
|
1546 ClassDeclaration cd;
|
|
1547
|
|
1548 for (cd = this.baseClass; cd; cd = cd.baseClass)
|
|
1549 {
|
|
1550 for (int k = 0; k < cd.vtblInterfaces.dim; k++)
|
|
1551 {
|
|
1552 BaseClass bs = cast(BaseClass)cd.vtblInterfaces.data[k];
|
|
1553
|
|
1554 if (b.base == bs.base)
|
|
1555 {
|
|
1556 //printf("\toverriding vtbl[] for %s\n", b.base.toChars());
|
|
1557 ClassDeclaration id = b.base;
|
|
1558 int j;
|
|
1559
|
|
1560 j = 0;
|
|
1561 if (id.vtblOffset())
|
|
1562 {
|
|
1563 // First entry is ClassInfo reference
|
|
1564 //dtxoff(&dt, id.toSymbol(), 0, TYnptr);
|
|
1565
|
|
1566 // First entry is struct Interface reference
|
|
1567 dtxoff(&dt, cd.toSymbol(), CLASSINFO_SIZE + k * (4 * PTRSIZE), TYnptr);
|
|
1568 j = 1;
|
|
1569 }
|
|
1570
|
|
1571 for (; j < id.vtbl.dim; j++)
|
|
1572 {
|
|
1573 assert(j < b.vtbl.dim);
|
|
1574 FuncDeclaration fd = cast(FuncDeclaration)b.vtbl.data[j];
|
|
1575 if (fd)
|
|
1576 dtxoff(&dt, fd.toThunkSymbol(bs.offset), 0, TYnptr);
|
|
1577 else
|
|
1578 dtdword(&dt, 0);
|
|
1579 }
|
|
1580 }
|
|
1581 }
|
|
1582 }
|
|
1583 }
|
|
1584 }
|
|
1585
|
|
1586
|
|
1587 csym.Sdt = dt;
|
|
1588 version (ELFOBJ_OR_MACHOBJ) { // Burton
|
|
1589 // ClassInfo cannot be const data, because we use the monitor on it
|
|
1590 csym.Sseg = Segment.DATA;
|
|
1591 }
|
|
1592 outdata(csym);
|
|
1593 if (isExport())
|
|
1594 obj_export(csym,0);
|
|
1595
|
|
1596 //////////////////////////////////////////////
|
|
1597
|
|
1598 // Put out the vtbl[]
|
|
1599 //printf("putting out %s.vtbl[]\n", toChars());
|
|
1600 dt = null;
|
|
1601 if (0)
|
|
1602 i = 0;
|
|
1603 else
|
|
1604 {
|
|
1605 dtxoff(&dt, csym, 0, TYnptr); // first entry is ClassInfo reference
|
|
1606 i = 1;
|
|
1607 }
|
|
1608 for (; i < vtbl.dim; i++)
|
|
1609 {
|
|
1610 FuncDeclaration fd = (cast(Dsymbol)vtbl.data[i]).isFuncDeclaration();
|
|
1611
|
|
1612 //printf("\tvtbl[%d] = %p\n", i, fd);
|
|
1613 if (fd && (fd.fbody || !isAbstract()))
|
|
1614 {
|
|
1615 Symbol* s = fd.toSymbol();
|
|
1616
|
|
1617 version (DMDV2) {
|
|
1618 if (isFuncHidden(fd))
|
|
1619 {
|
|
1620 /* fd is hidden from the view of this class.
|
|
1621 * If fd overlaps with any function in the vtbl[], then
|
|
1622 * issue 'hidden' error.
|
|
1623 */
|
|
1624 for (int j = 1; j < vtbl.dim; j++)
|
|
1625 {
|
|
1626 if (j == i)
|
|
1627 continue;
|
|
1628 FuncDeclaration fd2 = (cast(Dsymbol)vtbl.data[j]).isFuncDeclaration();
|
|
1629 if (!fd2.ident.equals(fd.ident))
|
|
1630 continue;
|
|
1631 if (fd.leastAsSpecialized(fd2) || fd2.leastAsSpecialized(fd))
|
|
1632 {
|
|
1633 if (global.params.warnings)
|
|
1634 {
|
|
1635 TypeFunction tf = cast(TypeFunction)fd.type;
|
|
1636 if (tf.ty == Tfunction)
|
|
1637 warning("%s%s is hidden by %s\n", fd.toPrettyChars(), Argument.argsTypesToChars(tf.parameters, tf.varargs), toChars());
|
|
1638 else
|
|
1639 warning("%s is hidden by %s\n", fd.toPrettyChars(), toChars());
|
|
1640 }
|
|
1641 s = rtlsym[RTLSYM_DHIDDENFUNC];
|
|
1642 break;
|
|
1643 }
|
|
1644 }
|
|
1645 }
|
|
1646 }
|
|
1647 dtxoff(&dt, s, 0, TYnptr);
|
|
1648 }
|
|
1649 else
|
|
1650 dtdword(&dt, 0);
|
|
1651 }
|
|
1652
|
|
1653 vtblsym.Sdt = dt;
|
|
1654 vtblsym.Sclass = scclass;
|
|
1655 vtblsym.Sfl = FLdata;
|
|
1656 version (ELFOBJ) {
|
|
1657 vtblsym.Sseg = Segment.CDATA;
|
|
1658 }
|
|
1659 version (MACHOBJ) {
|
|
1660 vtblsym.Sseg = Segment.DATA;
|
|
1661 }
|
|
1662 outdata(vtblsym);
|
|
1663 if (isExport())
|
|
1664 obj_export(vtblsym,0);
|
|
1665 }
|
|
1666
|
|
1667 void toDebug()
|
|
1668 {
|
|
1669 assert(false);
|
|
1670 }
|
|
1671
|
|
1672 /******************************************
|
|
1673 * Get offset of base class's vtbl[] initializer from start of csym.
|
|
1674 * Returns ~0 if not this csym.
|
|
1675 */
|
|
1676 uint baseVtblOffset(BaseClass bc)
|
|
1677 {
|
|
1678 uint csymoffset;
|
|
1679 int i;
|
|
1680
|
|
1681 //printf("ClassDeclaration.baseVtblOffset('%s', bc = %p)\n", toChars(), bc);
|
|
1682 csymoffset = CLASSINFO_SIZE;
|
|
1683 csymoffset += vtblInterfaces.dim * (4 * PTRSIZE);
|
|
1684
|
|
1685 for (i = 0; i < vtblInterfaces.dim; i++)
|
|
1686 {
|
|
1687 BaseClass b = cast(BaseClass)vtblInterfaces.data[i];
|
|
1688
|
|
1689 if (b == bc)
|
|
1690 return csymoffset;
|
|
1691 csymoffset += b.base.vtbl.dim * PTRSIZE;
|
|
1692 }
|
|
1693
|
|
1694 static if (true) {
|
|
1695 // Put out the overriding interface vtbl[]s.
|
|
1696 // This must be mirrored with ClassDeclaration.baseVtblOffset()
|
|
1697 //printf("putting out overriding interface vtbl[]s for '%s' at offset x%x\n", toChars(), offset);
|
|
1698 ClassDeclaration cd;
|
|
1699 Array bvtbl;
|
|
1700
|
|
1701 for (cd = this.baseClass; cd; cd = cd.baseClass)
|
|
1702 {
|
|
1703 for (int k = 0; k < cd.vtblInterfaces.dim; k++)
|
|
1704 {
|
|
1705 BaseClass bs = cast(BaseClass)cd.vtblInterfaces.data[k];
|
|
1706
|
|
1707 if (bs.fillVtbl(this, null, 0))
|
|
1708 {
|
|
1709 if (bc == bs)
|
|
1710 {
|
|
1711 //printf("\tcsymoffset = x%x\n", csymoffset);
|
|
1712 return csymoffset;
|
|
1713 }
|
|
1714 csymoffset += bs.base.vtbl.dim * PTRSIZE;
|
|
1715 }
|
|
1716 }
|
|
1717 }
|
|
1718 }
|
|
1719 version (INTERFACE_VIRTUAL) {
|
|
1720 for (i = 0; i < vtblInterfaces.dim; i++)
|
|
1721 {
|
|
1722 BaseClass b = cast(BaseClass)vtblInterfaces.data[i];
|
|
1723 ClassDeclaration cd;
|
|
1724
|
|
1725 for (cd = this.baseClass; cd; cd = cd.baseClass)
|
|
1726 {
|
|
1727 //printf("\tbase class %s\n", cd.toChars());
|
|
1728 for (int k = 0; k < cd.vtblInterfaces.dim; k++)
|
|
1729 {
|
|
1730 BaseClass bs = cast(BaseClass)cd.vtblInterfaces.data[k];
|
|
1731
|
|
1732 if (bc == bs)
|
|
1733 {
|
|
1734 //printf("\tcsymoffset = x%x\n", csymoffset);
|
|
1735 return csymoffset;
|
|
1736 }
|
|
1737 if (b.base == bs.base)
|
|
1738 csymoffset += bs.base.vtbl.dim * PTRSIZE;
|
|
1739 }
|
|
1740 }
|
|
1741 }
|
|
1742 }
|
|
1743
|
|
1744 return ~0;
|
|
1745 }
|
|
1746
|
|
1747 static private __gshared Classsym* scc;
|
|
1748
|
|
1749 /*************************************
|
|
1750 * Create the "ClassInfo" symbol
|
|
1751 */
|
|
1752 override Symbol* toSymbol()
|
|
1753 {
|
|
1754 if (!csym)
|
|
1755 {
|
|
1756 Symbol* s;
|
|
1757
|
|
1758 if (!scc)
|
|
1759 scc = fake_classsym(Id.ClassInfo);
|
|
1760
|
|
1761 s = toSymbolX("__Class", SC.SCextern, scc.Stype, "Z");
|
|
1762 s.Sfl = FL.FLextern;
|
|
1763 s.Sflags |= SFL.SFLnodebug;
|
|
1764 csym = s;
|
|
1765 slist_add(s);
|
|
1766 }
|
|
1767
|
|
1768 return csym;
|
|
1769 }
|
|
1770
|
|
1771 /*************************************
|
|
1772 * This is accessible via the ClassData, but since it is frequently
|
|
1773 * needed directly (like for rtti comparisons), make it directly accessible.
|
|
1774 */
|
|
1775 Symbol* toVtblSymbol()
|
|
1776 {
|
|
1777 if (!vtblsym)
|
|
1778 {
|
|
1779 if (!csym)
|
|
1780 toSymbol();
|
|
1781
|
|
1782 TYPE* t = type_alloc(TYM.TYnptr | mTY.mTYconst);
|
|
1783 t.Tnext = tsvoid;
|
|
1784 t.Tnext.Tcount++;
|
|
1785 t.Tmangle = mTYman.mTYman_d;
|
|
1786
|
|
1787 Symbol* s = toSymbolX("__vtbl", SC.SCextern, t, "Z");
|
|
1788 s.Sflags |= SFL.SFLnodebug;
|
|
1789 s.Sfl = FL.FLextern;
|
|
1790 vtblsym = s;
|
|
1791 slist_add(s);
|
|
1792 }
|
|
1793 return vtblsym;
|
|
1794 }
|
|
1795
|
|
1796 // Generate the data for the static initializer.
|
|
1797 void toDt(dt_t **pdt)
|
|
1798 {
|
|
1799 //printf("ClassDeclaration.toDt(this = '%s')\n", toChars());
|
|
1800
|
|
1801 // Put in first two members, the vtbl[] and the monitor
|
|
1802 dtxoff(pdt, toVtblSymbol(), 0, TYnptr);
|
|
1803 dtdword(pdt, 0); // monitor
|
|
1804
|
|
1805 // Put in the rest
|
|
1806 toDt2(pdt, this);
|
|
1807
|
|
1808 //printf("-ClassDeclaration.toDt(this = '%s')\n", toChars());
|
|
1809 }
|
|
1810
|
|
1811 void toDt2(dt_t** pdt, ClassDeclaration cd)
|
|
1812 {
|
|
1813 uint offset;
|
|
1814 uint i;
|
|
1815 dt_t* dt;
|
|
1816 uint csymoffset;
|
|
1817
|
|
1818 version (LOG) {
|
|
1819 printf("ClassDeclaration.toDt2(this = '%s', cd = '%s')\n", toChars(), cd.toChars());
|
|
1820 }
|
|
1821 if (baseClass)
|
|
1822 {
|
|
1823 baseClass.toDt2(pdt, cd);
|
|
1824 offset = baseClass.structsize;
|
|
1825 }
|
|
1826 else
|
|
1827 {
|
|
1828 offset = 8;
|
|
1829 }
|
|
1830
|
|
1831 // Note equivalence of this loop to struct's
|
|
1832 for (i = 0; i < fields.dim; i++)
|
|
1833 {
|
|
1834 VarDeclaration v = cast(VarDeclaration)fields.data[i];
|
|
1835 Initializer init;
|
|
1836
|
|
1837 //printf("\t\tv = '%s' v.offset = %2d, offset = %2d\n", v.toChars(), v.offset, offset);
|
|
1838 dt = null;
|
|
1839 init = v.init;
|
|
1840 if (init)
|
|
1841 {
|
|
1842 //printf("\t\t%s has initializer %s\n", v.toChars(), init.toChars());
|
|
1843 ExpInitializer ei = init.isExpInitializer();
|
|
1844 Type tb = v.type.toBasetype();
|
|
1845 if (ei && tb.ty == Tsarray)
|
|
1846 (cast(TypeSArray)tb).toDtElem(&dt, ei.exp);
|
|
1847 else
|
|
1848 dt = init.toDt();
|
|
1849 }
|
|
1850 else if (v.offset >= offset)
|
|
1851 { //printf("\t\tdefault initializer\n");
|
|
1852 v.type.toDt(&dt);
|
|
1853 }
|
|
1854 if (dt)
|
|
1855 {
|
|
1856 if (v.offset < offset)
|
|
1857 error("duplicated union initialization for %s", v.toChars());
|
|
1858 else
|
|
1859 {
|
|
1860 if (offset < v.offset)
|
|
1861 dtnzeros(pdt, v.offset - offset);
|
|
1862 dtcat(pdt, dt);
|
|
1863 offset = v.offset + cast(uint)v.type.size();
|
|
1864 }
|
|
1865 }
|
|
1866 }
|
|
1867
|
|
1868 // Interface vptr initializations
|
|
1869 toSymbol(); // define csym
|
|
1870
|
|
1871 for (i = 0; i < vtblInterfaces.dim; i++)
|
|
1872 {
|
|
1873 BaseClass b = cast(BaseClass)vtblInterfaces.data[i];
|
|
1874
|
|
1875 /// version (1 || INTERFACE_VIRTUAL) {
|
|
1876 for (ClassDeclaration cd2 = cd; 1; cd2 = cd2.baseClass)
|
|
1877 {
|
|
1878 assert(cd2);
|
|
1879 csymoffset = cd2.baseVtblOffset(b);
|
|
1880 if (csymoffset != ~0)
|
|
1881 {
|
|
1882 if (offset < b.offset)
|
|
1883 dtnzeros(pdt, b.offset - offset);
|
|
1884 dtxoff(pdt, cd2.toSymbol(), csymoffset, TYnptr);
|
|
1885 break;
|
|
1886 }
|
|
1887 }
|
|
1888 /// } else {
|
|
1889 /// csymoffset = baseVtblOffset(b);
|
|
1890 /// assert(csymoffset != ~0);
|
|
1891 /// dtxoff(pdt, csym, csymoffset, TYnptr);
|
|
1892 /// }
|
|
1893 offset = b.offset + 4;
|
|
1894 }
|
|
1895
|
|
1896 if (offset < structsize)
|
|
1897 dtnzeros(pdt, structsize - offset);
|
|
1898 }
|
|
1899
|
|
1900 Symbol* vtblsym;
|
|
1901
|
|
1902 ///ClassDeclaration isClassDeclaration() { return cast(ClassDeclaration)this; } /// huh?
|
|
1903 override ClassDeclaration isClassDeclaration() { return this; }
|
|
1904 }
|