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