Mercurial > projects > ddmd
annotate dmd/ClassDeclaration.d @ 74:7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
author | Eldar Insafutdinov <e.insafutdinov@gmail.com> |
---|---|
date | Sun, 29 Aug 2010 09:43:40 +0100 |
parents | ef02e2e203c2 |
children | ad4792a1cfd6 |
rev | line source |
---|---|
73 | 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); | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
73
diff
changeset
|
645 members.push(vthis); |
73 | 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()); | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
73
diff
changeset
|
754 members.push(ctor); |
73 | 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 if (!isAnonymous()) | |
816 { | |
817 buf.printf("%s ", kind()); | |
818 buf.writestring(toChars()); | |
819 if (baseclasses.dim) | |
820 buf.writestring(" : "); | |
821 } | |
822 for (int i = 0; i < baseclasses.dim; i++) | |
823 { | |
824 BaseClass b = cast(BaseClass)baseclasses.data[i]; | |
825 | |
826 if (i) | |
827 buf.writeByte(','); | |
828 //buf.writestring(b.base.ident.toChars()); | |
829 b.type.toCBuffer(buf, null, hgs); | |
830 } | |
831 if (members) | |
832 { | |
833 buf.writenl(); | |
834 buf.writeByte('{'); | |
835 buf.writenl(); | |
836 for (int i = 0; i < members.dim; i++) | |
837 { | |
838 Dsymbol s = cast(Dsymbol)members.data[i]; | |
839 | |
840 buf.writestring(" "); | |
841 s.toCBuffer(buf, hgs); | |
842 } | |
843 buf.writestring("}"); | |
844 } | |
845 else | |
846 buf.writeByte(';'); | |
847 buf.writenl(); | |
848 } | |
849 | |
850 /********************************************* | |
851 * Determine if 'this' is a base class of cd. | |
852 * This is used to detect circular inheritance only. | |
853 */ | |
854 int isBaseOf2(ClassDeclaration cd) | |
855 { | |
856 if (!cd) | |
857 return 0; | |
858 //printf("ClassDeclaration::isBaseOf2(this = '%s', cd = '%s')\n", toChars(), cd.toChars()); | |
859 for (int i = 0; i < cd.baseclasses.dim; i++) | |
860 { | |
861 BaseClass b = cast(BaseClass)cd.baseclasses.data[i]; | |
862 | |
863 if (b.base is this || isBaseOf2(b.base)) | |
864 return 1; | |
865 } | |
866 return 0; | |
867 } | |
868 | |
869 /******************************************* | |
870 * Determine if 'this' is a base class of cd. | |
871 */ | |
872 /// #define OFFSET_RUNTIME 0x76543210 | |
873 bool isBaseOf(ClassDeclaration cd, int* poffset) | |
874 { | |
875 if (!cd) | |
876 return 0; | |
877 //printf("ClassDeclaration::isBaseOf2(this = '%s', cd = '%s')\n", toChars(), cd.toChars()); | |
878 for (int i = 0; i < cd.baseclasses.dim; i++) | |
879 { | |
880 BaseClass b = cast(BaseClass)cd.baseclasses.data[i]; | |
881 | |
882 if (b.base == this || isBaseOf2(b.base)) | |
883 return 1; | |
884 } | |
885 | |
886 return 0; | |
887 } | |
888 | |
889 override Dsymbol search(Loc, Identifier ident, int flags) | |
890 { | |
891 Dsymbol s; | |
892 //printf("%s.ClassDeclaration.search('%s')\n", toChars(), ident.toChars()); | |
893 | |
894 if (scope_) | |
895 { | |
896 Scope sc = scope_; | |
897 sc.mustsemantic++; | |
898 semantic(sc); | |
899 sc.mustsemantic--; | |
900 } | |
901 | |
902 if (!members || !symtab || scope_) | |
903 { | |
904 error("is forward referenced when looking for '%s'", ident.toChars()); | |
905 //*(char*)0=0; | |
906 return null; | |
907 } | |
908 | |
909 s = ScopeDsymbol.search(loc, ident, flags); | |
910 if (!s) | |
911 { | |
912 // Search bases classes in depth-first, left to right order | |
913 | |
914 int i; | |
915 | |
916 for (i = 0; i < baseclasses.dim; i++) | |
917 { | |
918 BaseClass b = cast(BaseClass)baseclasses.data[i]; | |
919 | |
920 if (b.base) | |
921 { | |
922 if (!b.base.symtab) | |
923 error("base %s is forward referenced", b.base.ident.toChars()); | |
924 else | |
925 { | |
926 s = b.base.search(loc, ident, flags); | |
927 if (s is this) // happens if s is nested in this and derives from this | |
928 s = null; | |
929 else if (s) | |
930 break; | |
931 } | |
932 } | |
933 } | |
934 } | |
935 return s; | |
936 } | |
937 | |
938 version (DMDV2) { | |
939 bool isFuncHidden(FuncDeclaration fd) | |
940 { | |
941 //printf("ClassDeclaration::isFuncHidden(class = %s, fd = %s)\n", toChars(), fd.toChars()); | |
942 Dsymbol s = search(Loc(0), fd.ident, 4|2); | |
943 if (!s) | |
944 { | |
945 //printf("not found\n"); | |
946 /* Because, due to a hack, if there are multiple definitions | |
947 * of fd.ident, null is returned. | |
948 */ | |
949 return false; | |
950 } | |
951 | |
952 Param p; p.fd = fd; | |
953 | |
954 s = s.toAlias(); | |
955 OverloadSet os = s.isOverloadSet(); | |
956 if (os) | |
957 { | |
958 for (int i = 0; i < os.a.dim; i++) | |
959 { | |
960 Dsymbol s2 = cast(Dsymbol)os.a.data[i]; | |
961 FuncDeclaration f2 = s2.isFuncDeclaration(); | |
962 if (f2 && overloadApply(f2, &p.isf, &p)) | |
963 return false; | |
964 } | |
965 return true; | |
966 } | |
967 else | |
968 { | |
969 FuncDeclaration fdstart = s.isFuncDeclaration(); | |
970 //printf("%s fdstart = %p\n", s.kind(), fdstart); | |
971 return !overloadApply(fdstart, &p.isf, &p); | |
972 } | |
973 } | |
974 } | |
975 FuncDeclaration findFunc(Identifier ident, TypeFunction tf) | |
976 { | |
977 //printf("ClassDeclaration.findFunc(%s, %s) %s\n", ident.toChars(), tf.toChars(), toChars()); | |
978 | |
979 ClassDeclaration cd = this; | |
980 Array vtbl = cd.vtbl; | |
981 while (true) | |
982 { | |
983 for (size_t i = 0; i < vtbl.dim; i++) | |
984 { | |
985 FuncDeclaration fd = (cast(Dsymbol)vtbl.data[i]).isFuncDeclaration(); | |
986 if (!fd) | |
987 continue; // the first entry might be a ClassInfo | |
988 | |
989 //printf("\t[%d] = %s\n", i, fd.toChars()); | |
990 if (ident == fd.ident && | |
991 //tf.equals(fd.type) | |
992 fd.type.covariant(tf) == 1 | |
993 ) | |
994 { //printf("\t\tfound\n"); | |
995 return fd; | |
996 } | |
997 //else printf("\t\t%d\n", fd.type.covariant(tf)); | |
998 } | |
999 if (!cd) | |
1000 break; | |
1001 | |
1002 vtbl = cd.vtblFinal; | |
1003 cd = cd.baseClass; | |
1004 } | |
1005 | |
1006 return null; | |
1007 } | |
1008 | |
1009 void interfaceSemantic(Scope sc) | |
1010 { | |
1011 InterfaceDeclaration id = isInterfaceDeclaration(); | |
1012 | |
1013 vtblInterfaces = new BaseClasses(); | |
1014 vtblInterfaces.reserve(interfaces_dim); | |
1015 | |
1016 for (size_t i = 0; i < interfaces_dim; i++) | |
1017 { | |
1018 BaseClass b = interfaces[i]; | |
1019 | |
1020 // If this is an interface, and it derives from a COM interface, | |
1021 // then this is a COM interface too. | |
1022 if (b.base.isCOMinterface()) | |
1023 com = 1; | |
1024 | |
1025 if (b.base.isCPPinterface() && id) | |
1026 id.cpp = 1; | |
1027 | |
1028 vtblInterfaces.push(cast(void*)b); | |
1029 b.copyBaseInterfaces(vtblInterfaces); | |
1030 } | |
1031 } | |
1032 | |
1033 bool isCOMclass() | |
1034 { | |
1035 return com; | |
1036 } | |
1037 | |
1038 bool isCOMinterface() | |
1039 { | |
1040 return false; | |
1041 } | |
1042 | |
1043 version (DMDV2) { | |
1044 bool isCPPinterface() | |
1045 { | |
1046 return false; | |
1047 } | |
1048 } | |
1049 bool isAbstract() | |
1050 { | |
1051 if (isabstract) | |
1052 return true; | |
1053 | |
1054 for (int i = 1; i < vtbl.dim; i++) | |
1055 { | |
1056 FuncDeclaration fd = (cast(Dsymbol)vtbl.data[i]).isFuncDeclaration(); | |
1057 | |
1058 //printf("\tvtbl[%d] = %p\n", i, fd); | |
1059 if (!fd || fd.isAbstract()) | |
1060 { | |
1061 isabstract = true; | |
1062 return true; | |
1063 } | |
1064 } | |
1065 | |
1066 return false; | |
1067 } | |
1068 | |
1069 int vtblOffset() | |
1070 { | |
1071 assert(false); | |
1072 } | |
1073 | |
1074 override string kind() | |
1075 { | |
1076 return "class"; | |
1077 } | |
1078 | |
1079 override string mangle() | |
1080 { | |
1081 Dsymbol parentsave = parent; | |
1082 | |
1083 //printf("ClassDeclaration.mangle() %s.%s\n", parent.toChars(), toChars()); | |
1084 | |
1085 /* These are reserved to the compiler, so keep simple | |
1086 * names for them. | |
1087 */ | |
1088 if (ident is Id.Exception) | |
1089 { | |
1090 if (parent.ident is Id.object) | |
1091 parent = null; | |
1092 } | |
1093 else if (ident is Id.TypeInfo || | |
1094 // ident is Id.Exception || | |
1095 ident is Id.TypeInfo_Struct || | |
1096 ident is Id.TypeInfo_Class || | |
1097 ident is Id.TypeInfo_Typedef || | |
1098 ident is Id.TypeInfo_Tuple || | |
1099 this is object || | |
1100 this is classinfo || | |
1101 this is Module.moduleinfo || | |
1102 ident.toChars().startsWith("TypeInfo_") | |
1103 ) | |
1104 { | |
1105 parent = null; | |
1106 } | |
1107 | |
1108 string id = Dsymbol.mangle(); | |
1109 parent = parentsave; | |
1110 return id; | |
1111 } | |
1112 | |
1113 override void toDocBuffer(OutBuffer buf) | |
1114 { | |
1115 assert(false); | |
1116 } | |
1117 | |
1118 override PROT getAccess(Dsymbol smember) // determine access to smember | |
1119 { | |
1120 PROT access_ret = PROT.PROTnone; | |
1121 | |
1122 version (LOG) { | |
1123 printf("+ClassDeclaration::getAccess(this = '%s', smember = '%s')\n", | |
1124 toChars(), smember.toChars()); | |
1125 } | |
1126 if (smember.toParent() is this) | |
1127 { | |
1128 access_ret = smember.prot(); | |
1129 } | |
1130 else | |
1131 { | |
1132 PROT access; | |
1133 int i; | |
1134 | |
1135 if (smember.isDeclaration().isStatic()) | |
1136 { | |
1137 access_ret = smember.prot(); | |
1138 } | |
1139 | |
1140 for (i = 0; i < baseclasses.dim; i++) | |
1141 { | |
1142 BaseClass b = cast(BaseClass)baseclasses.data[i]; | |
1143 | |
1144 access = b.base.getAccess(smember); | |
1145 switch (access) | |
1146 { | |
1147 case PROT.PROTnone: | |
1148 break; | |
1149 | |
1150 case PROT.PROTprivate: | |
1151 access = PROT.PROTnone; // private members of base class not accessible | |
1152 break; | |
1153 | |
1154 case PROT.PROTpackage: | |
1155 case PROT.PROTprotected: | |
1156 case PROT.PROTpublic: | |
1157 case PROT.PROTexport: | |
1158 // If access is to be tightened | |
1159 if (b.protection < access) | |
1160 access = b.protection; | |
1161 | |
1162 // Pick path with loosest access | |
1163 if (access > access_ret) | |
1164 access_ret = access; | |
1165 break; | |
1166 | |
1167 default: | |
1168 assert(0); | |
1169 } | |
1170 } | |
1171 } | |
1172 | |
1173 version (LOG) { | |
1174 printf("-ClassDeclaration::getAccess(this = '%s', smember = '%s') = %d\n", | |
1175 toChars(), smember.toChars(), access_ret); | |
1176 } | |
1177 | |
1178 return access_ret; | |
1179 } | |
1180 | |
1181 override void addLocalClass(ClassDeclarations aclasses) | |
1182 { | |
1183 aclasses.push(cast(void*)this); | |
1184 } | |
1185 | |
1186 // Back end | |
1187 override void toObjFile(int multiobj) // compile to .obj file | |
1188 { | |
1189 uint i; | |
1190 uint offset; | |
1191 Symbol* sinit; | |
1192 enum_SC scclass; | |
1193 | |
1194 //printf("ClassDeclaration.toObjFile('%s')\n", toChars()); | |
1195 | |
1196 if (!members) | |
1197 return; | |
1198 | |
1199 if (multiobj) | |
1200 { | |
1201 obj_append(this); | |
1202 return; | |
1203 } | |
1204 | |
1205 if (global.params.symdebug) | |
1206 toDebug(); | |
1207 | |
1208 assert(!scope_); // semantic() should have been run to completion | |
1209 | |
1210 scclass = SCglobal; | |
1211 if (inTemplateInstance()) | |
1212 scclass = SCcomdat; | |
1213 | |
1214 // Put out the members | |
1215 for (i = 0; i < members.dim; i++) | |
1216 { | |
1217 Dsymbol member; | |
1218 | |
1219 member = cast(Dsymbol)members.data[i]; | |
1220 member.toObjFile(0); | |
1221 } | |
1222 | |
1223 static if (false) { | |
1224 // Build destructor by aggregating dtors[] | |
1225 Symbol* sdtor; | |
1226 switch (dtors.dim) | |
1227 { | |
1228 case 0: | |
1229 // No destructors for this class | |
1230 sdtor = null; | |
1231 break; | |
1232 | |
1233 case 1: | |
1234 // One destructor, just use it directly | |
1235 sdtor = (cast(DtorDeclaration)dtors.data[0]).toSymbol(); | |
1236 break; | |
1237 | |
1238 default: | |
1239 { | |
1240 /* Build a destructor that calls all the | |
1241 * other destructors in dtors[]. | |
1242 */ | |
1243 | |
1244 elem* edtor = null; | |
1245 | |
1246 // Declare 'this' pointer for our new destructor | |
1247 Symbol* sthis = symbol_calloc("this"); | |
1248 sthis.Stype = type_fake(TYnptr); | |
1249 sthis.Stype.Tcount++; | |
1250 sthis.Sclass = SCfastpar; | |
1251 sthis.Spreg = AX; | |
1252 sthis.Sfl = FLauto; | |
1253 | |
1254 // Call each of the destructors in dtors[] | |
1255 // in reverse order | |
1256 for (i = 0; i < dtors.dim; i++) | |
1257 { | |
1258 DtorDeclaration d = cast(DtorDeclaration)dtors.data[i]; | |
1259 Symbol* s = d.toSymbol(); | |
1260 elem* e = el_bin(OPcall, TYvoid, el_var(s), el_var(sthis)); | |
1261 edtor = el_combine(e, edtor); | |
1262 } | |
1263 | |
1264 // Create type for the function | |
1265 .type* t = type_alloc(TYjfunc); | |
1266 t.Tflags |= TFprototype | TFfixed; | |
1267 t.Tmangle = mTYman_d; | |
1268 t.Tnext = tsvoid; | |
1269 tsvoid.Tcount++; | |
1270 | |
1271 // Create the function, sdtor, and write it out | |
1272 localgot = null; | |
1273 sdtor = toSymbolX("__dtor", SCglobal, t, "FZv"); | |
1274 block* b = block_calloc(); | |
1275 b.BC = BCret; | |
1276 b.Belem = edtor; | |
1277 sdtor.Sfunc.Fstartblock = b; | |
1278 cstate.CSpsymtab = &sdtor.Sfunc.Flocsym; | |
1279 symbol_add(sthis); | |
1280 writefunc(sdtor); | |
1281 } | |
1282 } | |
1283 } | |
1284 | |
1285 // Generate C symbols | |
1286 toSymbol(); | |
1287 toVtblSymbol(); | |
1288 sinit = toInitializer(); | |
1289 | |
1290 ////////////////////////////////////////////// | |
1291 | |
1292 // Generate static initializer | |
1293 sinit.Sclass = scclass; | |
1294 sinit.Sfl = FLdata; | |
1295 version (ELFOBJ) { // Burton | |
1296 sinit.Sseg = Segment.CDATA; | |
1297 } | |
1298 version (MACHOBJ) { | |
1299 sinit.Sseg = Segment.DATA; | |
1300 } | |
1301 toDt(&sinit.Sdt); | |
1302 outdata(sinit); | |
1303 | |
1304 ////////////////////////////////////////////// | |
1305 | |
1306 // Put out the TypeInfo | |
1307 type.getTypeInfo(null); | |
1308 type.vtinfo.toObjFile(multiobj); | |
1309 | |
1310 ////////////////////////////////////////////// | |
1311 | |
1312 // Put out the ClassInfo | |
1313 csym.Sclass = scclass; | |
1314 csym.Sfl = FLdata; | |
1315 | |
1316 /* The layout is: | |
1317 { | |
1318 void **vptr; | |
1319 monitor_t monitor; | |
1320 byte[] initializer; // static initialization data | |
1321 char[] name; // class name | |
1322 void *[] vtbl; | |
1323 Interface[] interfaces; | |
1324 ClassInfo *base; // base class | |
1325 void *destructor; | |
1326 void *invariant; // class invariant | |
1327 uint flags; | |
1328 void *deallocator; | |
1329 OffsetTypeInfo[] offTi; | |
1330 void *defaultConstructor; | |
1331 const(MemberInfo[]) function(string) xgetMembers; // module getMembers() function | |
1332 TypeInfo typeinfo; | |
1333 } | |
1334 */ | |
1335 dt_t* dt = null; | |
1336 offset = CLASSINFO_SIZE; // must be ClassInfo.size | |
1337 if (classinfo) | |
1338 { | |
1339 if (classinfo.structsize != CLASSINFO_SIZE) | |
1340 error("D compiler and phobos' object.d are mismatched"); | |
1341 } | |
1342 | |
1343 if (classinfo) | |
1344 dtxoff(&dt, classinfo.toVtblSymbol(), 0, TYnptr); // vtbl for ClassInfo | |
1345 else | |
1346 dtdword(&dt, 0); // BUG: should be an assert() | |
1347 | |
1348 dtdword(&dt, 0); // monitor | |
1349 | |
1350 // initializer[] | |
1351 assert(structsize >= 8); | |
1352 dtdword(&dt, structsize); // size | |
1353 dtxoff(&dt, sinit, 0, TYnptr); // initializer | |
1354 | |
1355 // name[] | |
1356 string name = ident.toChars(); | |
1357 size_t namelen = name.length; | |
1358 if (!(namelen > 9 && name[0..9] == "TypeInfo_")) | |
1359 { | |
1360 name = toPrettyChars(); | |
1361 namelen = name.length; | |
1362 } | |
1363 dtdword(&dt, namelen); | |
1364 dtabytes(&dt, TYnptr, 0, namelen + 1, toStringz(name)); | |
1365 | |
1366 // vtbl[] | |
1367 dtdword(&dt, vtbl.dim); | |
1368 dtxoff(&dt, vtblsym, 0, TYnptr); | |
1369 | |
1370 // interfaces[] | |
1371 dtdword(&dt, vtblInterfaces.dim); | |
1372 if (vtblInterfaces.dim) | |
1373 dtxoff(&dt, csym, offset, TYnptr); // (*) | |
1374 else | |
1375 dtdword(&dt, 0); | |
1376 | |
1377 // base | |
1378 if (baseClass) | |
1379 dtxoff(&dt, baseClass.toSymbol(), 0, TYnptr); | |
1380 else | |
1381 dtdword(&dt, 0); | |
1382 | |
1383 // destructor | |
1384 if (dtor) | |
1385 dtxoff(&dt, dtor.toSymbol(), 0, TYnptr); | |
1386 else | |
1387 dtdword(&dt, 0); | |
1388 | |
1389 // invariant | |
1390 if (inv) | |
1391 dtxoff(&dt, inv.toSymbol(), 0, TYnptr); | |
1392 else | |
1393 dtdword(&dt, 0); | |
1394 | |
1395 // flags | |
1396 int flags = 4 | isCOMclass(); | |
1397 version (DMDV2) { | |
1398 flags |= 16; | |
1399 } | |
1400 flags |= 32; | |
1401 | |
1402 if (ctor) | |
1403 flags |= 8; | |
1404 for (ClassDeclaration cd = this; cd; cd = cd.baseClass) | |
1405 { | |
1406 if (cd.members) | |
1407 { | |
1408 for (size_t j = 0; j < cd.members.dim; j++) | |
1409 { | |
1410 Dsymbol sm = cast(Dsymbol)cd.members.data[j]; | |
1411 //printf("sm = %s %s\n", sm.kind(), sm.toChars()); | |
1412 if (sm.hasPointers()) | |
1413 goto L2; | |
1414 } | |
1415 } | |
1416 } | |
1417 flags |= 2; // no pointers | |
1418 L2: | |
1419 dtdword(&dt, flags); | |
1420 | |
1421 | |
1422 // deallocator | |
1423 if (aggDelete) | |
1424 dtxoff(&dt, aggDelete.toSymbol(), 0, TYnptr); | |
1425 else | |
1426 dtdword(&dt, 0); | |
1427 | |
1428 // offTi[] | |
1429 dtdword(&dt, 0); | |
1430 dtdword(&dt, 0); // null for now, fix later | |
1431 | |
1432 // defaultConstructor | |
1433 if (defaultCtor) | |
1434 dtxoff(&dt, defaultCtor.toSymbol(), 0, TYnptr); | |
1435 else | |
1436 dtdword(&dt, 0); | |
1437 | |
1438 version (DMDV2) { | |
1439 FuncDeclaration sgetmembers = findGetMembers(); | |
1440 if (sgetmembers) | |
1441 dtxoff(&dt, sgetmembers.toSymbol(), 0, TYnptr); | |
1442 else | |
1443 dtdword(&dt, 0); // module getMembers() function | |
1444 } | |
1445 | |
1446 dtxoff(&dt, type.vtinfo.toSymbol(), 0, TYnptr); // typeinfo | |
1447 //dtdword(&dt, 0); | |
1448 | |
1449 ////////////////////////////////////////////// | |
1450 | |
1451 // Put out vtblInterfaces.data[]. Must immediately follow csym, because | |
1452 // of the fixup (*) | |
1453 | |
1454 offset += vtblInterfaces.dim * (4 * PTRSIZE); | |
1455 for (i = 0; i < vtblInterfaces.dim; i++) | |
1456 { | |
1457 BaseClass b = cast(BaseClass)vtblInterfaces.data[i]; | |
1458 ClassDeclaration id = b.base; | |
1459 | |
1460 /* The layout is: | |
1461 * { | |
1462 * ClassInfo *interface; | |
1463 * void *[] vtbl; | |
1464 * unsigned offset; | |
1465 * } | |
1466 */ | |
1467 | |
1468 // Fill in vtbl[] | |
1469 b.fillVtbl(this, b.vtbl, 1); | |
1470 | |
1471 dtxoff(&dt, id.toSymbol(), 0, TYnptr); // ClassInfo | |
1472 | |
1473 // vtbl[] | |
1474 dtdword(&dt, id.vtbl.dim); | |
1475 dtxoff(&dt, csym, offset, TYnptr); | |
1476 | |
1477 dtdword(&dt, b.offset); // this offset | |
1478 | |
1479 offset += id.vtbl.dim * PTRSIZE; | |
1480 } | |
1481 | |
1482 // Put out the vtblInterfaces.data[].vtbl[] | |
1483 // This must be mirrored with ClassDeclaration.baseVtblOffset() | |
1484 //printf("putting out %d interface vtbl[]s for '%s'\n", vtblInterfaces.dim, toChars()); | |
1485 for (i = 0; i < vtblInterfaces.dim; i++) | |
1486 { | |
1487 BaseClass b = cast(BaseClass)vtblInterfaces.data[i]; | |
1488 ClassDeclaration id = b.base; | |
1489 int j; | |
1490 | |
1491 //printf(" interface[%d] is '%s'\n", i, id.toChars()); | |
1492 j = 0; | |
1493 if (id.vtblOffset()) | |
1494 { | |
1495 // First entry is ClassInfo reference | |
1496 //dtxoff(&dt, id.toSymbol(), 0, TYnptr); | |
1497 | |
1498 // First entry is struct Interface reference | |
1499 dtxoff(&dt, csym, CLASSINFO_SIZE + i * (4 * PTRSIZE), TYnptr); | |
1500 j = 1; | |
1501 } | |
1502 | |
1503 assert(id.vtbl.dim == b.vtbl.dim); | |
1504 for (; j < id.vtbl.dim; j++) | |
1505 { | |
1506 FuncDeclaration fd; | |
1507 | |
1508 assert(j < b.vtbl.dim); | |
1509 static if (false) { | |
1510 Object o = cast(Object)b.vtbl.data[j]; | |
1511 if (o) | |
1512 { | |
1513 printf("o = %p\n", o); | |
1514 assert(o.dyncast() == DYNCAST_DSYMBOL); | |
1515 Dsymbol s = cast(Dsymbol)o; | |
1516 printf("s.kind() = '%s'\n", s.kind()); | |
1517 } | |
1518 } | |
1519 fd = cast(FuncDeclaration)b.vtbl.data[j]; | |
1520 if (fd) | |
1521 dtxoff(&dt, fd.toThunkSymbol(b.offset), 0, TYnptr); | |
1522 else | |
1523 dtdword(&dt, 0); | |
1524 } | |
1525 } | |
1526 | |
1527 static if (true) { | |
1528 // Put out the overriding interface vtbl[]s. | |
1529 // This must be mirrored with ClassDeclaration.baseVtblOffset() | |
1530 //printf("putting out overriding interface vtbl[]s for '%s' at offset x%x\n", toChars(), offset); | |
1531 ClassDeclaration cd; | |
1532 scope Array bvtbl = new Array(); | |
1533 | |
1534 for (cd = this.baseClass; cd; cd = cd.baseClass) | |
1535 { | |
1536 for (int k = 0; k < cd.vtblInterfaces.dim; k++) | |
1537 { | |
1538 BaseClass bs = cast(BaseClass)cd.vtblInterfaces.data[k]; | |
1539 | |
1540 if (bs.fillVtbl(this, bvtbl, 0)) | |
1541 { | |
1542 //printf("\toverriding vtbl[] for %s\n", bs.base.toChars()); | |
1543 ClassDeclaration id = bs.base; | |
1544 int j; | |
1545 | |
1546 j = 0; | |
1547 if (id.vtblOffset()) | |
1548 { | |
1549 // First entry is ClassInfo reference | |
1550 //dtxoff(&dt, id.toSymbol(), 0, TYnptr); | |
1551 | |
1552 // First entry is struct Interface reference | |
1553 dtxoff(&dt, cd.toSymbol(), CLASSINFO_SIZE + k * (4 * PTRSIZE), TYnptr); | |
1554 j = 1; | |
1555 } | |
1556 | |
1557 for (; j < id.vtbl.dim; j++) | |
1558 { | |
1559 assert(j < bvtbl.dim); | |
1560 FuncDeclaration fd = cast(FuncDeclaration)bvtbl.data[j]; | |
1561 if (fd) | |
1562 dtxoff(&dt, fd.toThunkSymbol(bs.offset), 0, TYnptr); | |
1563 else | |
1564 dtdword(&dt, 0); | |
1565 } | |
1566 } | |
1567 } | |
1568 } | |
1569 } | |
1570 | |
1571 version (INTERFACE_VIRTUAL) { | |
1572 // Put out the overriding interface vtbl[]s. | |
1573 // This must be mirrored with ClassDeclaration.baseVtblOffset() | |
1574 //printf("putting out overriding interface vtbl[]s for '%s' at offset x%x\n", toChars(), offset); | |
1575 for (i = 0; i < vtblInterfaces.dim; i++) | |
1576 { | |
1577 BaseClass b = cast(BaseClass)vtblInterfaces.data[i]; | |
1578 ClassDeclaration cd; | |
1579 | |
1580 for (cd = this.baseClass; cd; cd = cd.baseClass) | |
1581 { | |
1582 for (int k = 0; k < cd.vtblInterfaces.dim; k++) | |
1583 { | |
1584 BaseClass bs = cast(BaseClass)cd.vtblInterfaces.data[k]; | |
1585 | |
1586 if (b.base == bs.base) | |
1587 { | |
1588 //printf("\toverriding vtbl[] for %s\n", b.base.toChars()); | |
1589 ClassDeclaration id = b.base; | |
1590 int j; | |
1591 | |
1592 j = 0; | |
1593 if (id.vtblOffset()) | |
1594 { | |
1595 // First entry is ClassInfo reference | |
1596 //dtxoff(&dt, id.toSymbol(), 0, TYnptr); | |
1597 | |
1598 // First entry is struct Interface reference | |
1599 dtxoff(&dt, cd.toSymbol(), CLASSINFO_SIZE + k * (4 * PTRSIZE), TYnptr); | |
1600 j = 1; | |
1601 } | |
1602 | |
1603 for (; j < id.vtbl.dim; j++) | |
1604 { | |
1605 assert(j < b.vtbl.dim); | |
1606 FuncDeclaration fd = cast(FuncDeclaration)b.vtbl.data[j]; | |
1607 if (fd) | |
1608 dtxoff(&dt, fd.toThunkSymbol(bs.offset), 0, TYnptr); | |
1609 else | |
1610 dtdword(&dt, 0); | |
1611 } | |
1612 } | |
1613 } | |
1614 } | |
1615 } | |
1616 } | |
1617 | |
1618 | |
1619 csym.Sdt = dt; | |
1620 version (ELFOBJ_OR_MACHOBJ) { // Burton | |
1621 // ClassInfo cannot be const data, because we use the monitor on it | |
1622 csym.Sseg = Segment.DATA; | |
1623 } | |
1624 outdata(csym); | |
1625 if (isExport()) | |
1626 obj_export(csym,0); | |
1627 | |
1628 ////////////////////////////////////////////// | |
1629 | |
1630 // Put out the vtbl[] | |
1631 //printf("putting out %s.vtbl[]\n", toChars()); | |
1632 dt = null; | |
1633 if (0) | |
1634 i = 0; | |
1635 else | |
1636 { | |
1637 dtxoff(&dt, csym, 0, TYnptr); // first entry is ClassInfo reference | |
1638 i = 1; | |
1639 } | |
1640 for (; i < vtbl.dim; i++) | |
1641 { | |
1642 FuncDeclaration fd = (cast(Dsymbol)vtbl.data[i]).isFuncDeclaration(); | |
1643 | |
1644 //printf("\tvtbl[%d] = %p\n", i, fd); | |
1645 if (fd && (fd.fbody || !isAbstract())) | |
1646 { | |
1647 Symbol* s = fd.toSymbol(); | |
1648 | |
1649 version (DMDV2) { | |
1650 if (isFuncHidden(fd)) | |
1651 { | |
1652 /* fd is hidden from the view of this class. | |
1653 * If fd overlaps with any function in the vtbl[], then | |
1654 * issue 'hidden' error. | |
1655 */ | |
1656 for (int j = 1; j < vtbl.dim; j++) | |
1657 { | |
1658 if (j == i) | |
1659 continue; | |
1660 FuncDeclaration fd2 = (cast(Dsymbol)vtbl.data[j]).isFuncDeclaration(); | |
1661 if (!fd2.ident.equals(fd.ident)) | |
1662 continue; | |
1663 if (fd.leastAsSpecialized(fd2) || fd2.leastAsSpecialized(fd)) | |
1664 { | |
1665 if (global.params.warnings) | |
1666 { | |
1667 TypeFunction tf = cast(TypeFunction)fd.type; | |
1668 if (tf.ty == Tfunction) | |
1669 warning("%s%s is hidden by %s\n", fd.toPrettyChars(), Argument.argsTypesToChars(tf.parameters, tf.varargs), toChars()); | |
1670 else | |
1671 warning("%s is hidden by %s\n", fd.toPrettyChars(), toChars()); | |
1672 } | |
1673 s = rtlsym[RTLSYM_DHIDDENFUNC]; | |
1674 break; | |
1675 } | |
1676 } | |
1677 } | |
1678 } | |
1679 dtxoff(&dt, s, 0, TYnptr); | |
1680 } | |
1681 else | |
1682 dtdword(&dt, 0); | |
1683 } | |
1684 | |
1685 vtblsym.Sdt = dt; | |
1686 vtblsym.Sclass = scclass; | |
1687 vtblsym.Sfl = FLdata; | |
1688 version (ELFOBJ) { | |
1689 vtblsym.Sseg = Segment.CDATA; | |
1690 } | |
1691 version (MACHOBJ) { | |
1692 vtblsym.Sseg = Segment.DATA; | |
1693 } | |
1694 outdata(vtblsym); | |
1695 if (isExport()) | |
1696 obj_export(vtblsym,0); | |
1697 } | |
1698 | |
1699 void toDebug() | |
1700 { | |
1701 assert(false); | |
1702 } | |
1703 | |
1704 /****************************************** | |
1705 * Get offset of base class's vtbl[] initializer from start of csym. | |
1706 * Returns ~0 if not this csym. | |
1707 */ | |
1708 uint baseVtblOffset(BaseClass bc) | |
1709 { | |
1710 uint csymoffset; | |
1711 int i; | |
1712 | |
1713 //printf("ClassDeclaration.baseVtblOffset('%s', bc = %p)\n", toChars(), bc); | |
1714 csymoffset = CLASSINFO_SIZE; | |
1715 csymoffset += vtblInterfaces.dim * (4 * PTRSIZE); | |
1716 | |
1717 for (i = 0; i < vtblInterfaces.dim; i++) | |
1718 { | |
1719 BaseClass b = cast(BaseClass)vtblInterfaces.data[i]; | |
1720 | |
1721 if (b == bc) | |
1722 return csymoffset; | |
1723 csymoffset += b.base.vtbl.dim * PTRSIZE; | |
1724 } | |
1725 | |
1726 static if (true) { | |
1727 // Put out the overriding interface vtbl[]s. | |
1728 // This must be mirrored with ClassDeclaration.baseVtblOffset() | |
1729 //printf("putting out overriding interface vtbl[]s for '%s' at offset x%x\n", toChars(), offset); | |
1730 ClassDeclaration cd; | |
1731 Array bvtbl; | |
1732 | |
1733 for (cd = this.baseClass; cd; cd = cd.baseClass) | |
1734 { | |
1735 for (int k = 0; k < cd.vtblInterfaces.dim; k++) | |
1736 { | |
1737 BaseClass bs = cast(BaseClass)cd.vtblInterfaces.data[k]; | |
1738 | |
1739 if (bs.fillVtbl(this, null, 0)) | |
1740 { | |
1741 if (bc == bs) | |
1742 { | |
1743 //printf("\tcsymoffset = x%x\n", csymoffset); | |
1744 return csymoffset; | |
1745 } | |
1746 csymoffset += bs.base.vtbl.dim * PTRSIZE; | |
1747 } | |
1748 } | |
1749 } | |
1750 } | |
1751 version (INTERFACE_VIRTUAL) { | |
1752 for (i = 0; i < vtblInterfaces.dim; i++) | |
1753 { | |
1754 BaseClass b = cast(BaseClass)vtblInterfaces.data[i]; | |
1755 ClassDeclaration cd; | |
1756 | |
1757 for (cd = this.baseClass; cd; cd = cd.baseClass) | |
1758 { | |
1759 //printf("\tbase class %s\n", cd.toChars()); | |
1760 for (int k = 0; k < cd.vtblInterfaces.dim; k++) | |
1761 { | |
1762 BaseClass bs = cast(BaseClass)cd.vtblInterfaces.data[k]; | |
1763 | |
1764 if (bc == bs) | |
1765 { | |
1766 //printf("\tcsymoffset = x%x\n", csymoffset); | |
1767 return csymoffset; | |
1768 } | |
1769 if (b.base == bs.base) | |
1770 csymoffset += bs.base.vtbl.dim * PTRSIZE; | |
1771 } | |
1772 } | |
1773 } | |
1774 } | |
1775 | |
1776 return ~0; | |
1777 } | |
1778 | |
1779 static private __gshared Classsym* scc; | |
1780 | |
1781 /************************************* | |
1782 * Create the "ClassInfo" symbol | |
1783 */ | |
1784 override Symbol* toSymbol() | |
1785 { | |
1786 if (!csym) | |
1787 { | |
1788 Symbol* s; | |
1789 | |
1790 if (!scc) | |
1791 scc = fake_classsym(Id.ClassInfo); | |
1792 | |
1793 s = toSymbolX("__Class", SC.SCextern, scc.Stype, "Z"); | |
1794 s.Sfl = FL.FLextern; | |
1795 s.Sflags |= SFL.SFLnodebug; | |
1796 csym = s; | |
1797 slist_add(s); | |
1798 } | |
1799 | |
1800 return csym; | |
1801 } | |
1802 | |
1803 /************************************* | |
1804 * This is accessible via the ClassData, but since it is frequently | |
1805 * needed directly (like for rtti comparisons), make it directly accessible. | |
1806 */ | |
1807 Symbol* toVtblSymbol() | |
1808 { | |
1809 if (!vtblsym) | |
1810 { | |
1811 if (!csym) | |
1812 toSymbol(); | |
1813 | |
1814 TYPE* t = type_alloc(TYM.TYnptr | mTY.mTYconst); | |
1815 t.Tnext = tsvoid; | |
1816 t.Tnext.Tcount++; | |
1817 t.Tmangle = mTYman.mTYman_d; | |
1818 | |
1819 Symbol* s = toSymbolX("__vtbl", SC.SCextern, t, "Z"); | |
1820 s.Sflags |= SFL.SFLnodebug; | |
1821 s.Sfl = FL.FLextern; | |
1822 vtblsym = s; | |
1823 slist_add(s); | |
1824 } | |
1825 return vtblsym; | |
1826 } | |
1827 | |
1828 // Generate the data for the static initializer. | |
1829 void toDt(dt_t **pdt) | |
1830 { | |
1831 //printf("ClassDeclaration.toDt(this = '%s')\n", toChars()); | |
1832 | |
1833 // Put in first two members, the vtbl[] and the monitor | |
1834 dtxoff(pdt, toVtblSymbol(), 0, TYnptr); | |
1835 dtdword(pdt, 0); // monitor | |
1836 | |
1837 // Put in the rest | |
1838 toDt2(pdt, this); | |
1839 | |
1840 //printf("-ClassDeclaration.toDt(this = '%s')\n", toChars()); | |
1841 } | |
1842 | |
1843 void toDt2(dt_t** pdt, ClassDeclaration cd) | |
1844 { | |
1845 uint offset; | |
1846 uint i; | |
1847 dt_t* dt; | |
1848 uint csymoffset; | |
1849 | |
1850 version (LOG) { | |
1851 printf("ClassDeclaration.toDt2(this = '%s', cd = '%s')\n", toChars(), cd.toChars()); | |
1852 } | |
1853 if (baseClass) | |
1854 { | |
1855 baseClass.toDt2(pdt, cd); | |
1856 offset = baseClass.structsize; | |
1857 } | |
1858 else | |
1859 { | |
1860 offset = 8; | |
1861 } | |
1862 | |
1863 // Note equivalence of this loop to struct's | |
1864 for (i = 0; i < fields.dim; i++) | |
1865 { | |
1866 VarDeclaration v = cast(VarDeclaration)fields.data[i]; | |
1867 Initializer init; | |
1868 | |
1869 //printf("\t\tv = '%s' v.offset = %2d, offset = %2d\n", v.toChars(), v.offset, offset); | |
1870 dt = null; | |
1871 init = v.init; | |
1872 if (init) | |
1873 { | |
1874 //printf("\t\t%s has initializer %s\n", v.toChars(), init.toChars()); | |
1875 ExpInitializer ei = init.isExpInitializer(); | |
1876 Type tb = v.type.toBasetype(); | |
1877 if (ei && tb.ty == Tsarray) | |
1878 (cast(TypeSArray)tb).toDtElem(&dt, ei.exp); | |
1879 else | |
1880 dt = init.toDt(); | |
1881 } | |
1882 else if (v.offset >= offset) | |
1883 { //printf("\t\tdefault initializer\n"); | |
1884 v.type.toDt(&dt); | |
1885 } | |
1886 if (dt) | |
1887 { | |
1888 if (v.offset < offset) | |
1889 error("duplicated union initialization for %s", v.toChars()); | |
1890 else | |
1891 { | |
1892 if (offset < v.offset) | |
1893 dtnzeros(pdt, v.offset - offset); | |
1894 dtcat(pdt, dt); | |
1895 offset = v.offset + cast(uint)v.type.size(); | |
1896 } | |
1897 } | |
1898 } | |
1899 | |
1900 // Interface vptr initializations | |
1901 toSymbol(); // define csym | |
1902 | |
1903 for (i = 0; i < vtblInterfaces.dim; i++) | |
1904 { | |
1905 BaseClass b = cast(BaseClass)vtblInterfaces.data[i]; | |
1906 | |
1907 /// version (1 || INTERFACE_VIRTUAL) { | |
1908 for (ClassDeclaration cd2 = cd; 1; cd2 = cd2.baseClass) | |
1909 { | |
1910 assert(cd2); | |
1911 csymoffset = cd2.baseVtblOffset(b); | |
1912 if (csymoffset != ~0) | |
1913 { | |
1914 if (offset < b.offset) | |
1915 dtnzeros(pdt, b.offset - offset); | |
1916 dtxoff(pdt, cd2.toSymbol(), csymoffset, TYnptr); | |
1917 break; | |
1918 } | |
1919 } | |
1920 /// } else { | |
1921 /// csymoffset = baseVtblOffset(b); | |
1922 /// assert(csymoffset != ~0); | |
1923 /// dtxoff(pdt, csym, csymoffset, TYnptr); | |
1924 /// } | |
1925 offset = b.offset + 4; | |
1926 } | |
1927 | |
1928 if (offset < structsize) | |
1929 dtnzeros(pdt, structsize - offset); | |
1930 } | |
1931 | |
1932 Symbol* vtblsym; | |
1933 | |
1934 ///ClassDeclaration isClassDeclaration() { return cast(ClassDeclaration)this; } /// huh? | |
1935 override ClassDeclaration isClassDeclaration() { return this; } | |
1936 } |