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