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