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