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