Mercurial > projects > ddmd
annotate dmd/InterfaceDeclaration.d @ 87:b17640f0e4e8
Fixed a bug with a Scope.this(Scope enclosing) being called instead of Scope.clone() method (as a copy ctor replacement)
author | korDen |
---|---|
date | Mon, 30 Aug 2010 19:56:27 +0400 |
parents | ad4792a1cfd6 |
children | 37b95c347975 |
rev | line source |
---|---|
0 | 1 module dmd.InterfaceDeclaration; |
2 | |
3 import dmd.ClassDeclaration; | |
4 import dmd.Loc; | |
5 import dmd.DsymbolTable; | |
6 import dmd.STC; | |
7 import dmd.Type; | |
8 import dmd.TY; | |
9 import dmd.LINK; | |
10 import dmd.Argument; | |
11 import dmd.Util; | |
12 import dmd.TypeTuple; | |
13 import dmd.PROT; | |
14 import dmd.TypeClass; | |
15 import dmd.Identifier; | |
16 import dmd.ArrayTypes; | |
17 import dmd.Dsymbol; | |
18 import dmd.Scope; | |
19 import dmd.Global; | |
22
fd4acc376c45
Implemented object file output and linking on linux.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
20 import dmd.Module; |
0 | 21 import dmd.BaseClass; |
22 import dmd.Id; | |
23 | |
24 import dmd.backend.Symbol; | |
25 import dmd.backend.TYM; | |
26 import dmd.backend.dt_t; | |
27 import dmd.backend.Util; | |
28 import dmd.codegen.Util; | |
29 import dmd.backend.SC; | |
30 import dmd.backend.FL; | |
31 import dmd.backend.LIST; | |
32 import dmd.backend.SFL; | |
33 | |
34 class InterfaceDeclaration : ClassDeclaration | |
35 { | |
36 version (DMDV2) { | |
37 bool cpp; // true if this is a C++ interface | |
38 } | |
39 this(Loc loc, Identifier id, BaseClasses baseclasses) | |
40 { | |
41 super(loc, id, baseclasses); | |
42 | |
43 if (id is Id.IUnknown) // IUnknown is the root of all COM interfaces | |
44 { | |
45 com = true; | |
46 cpp = true; // IUnknown is also a C++ interface | |
47 } | |
48 } | |
49 | |
72 | 50 override Dsymbol syntaxCopy(Dsymbol s) |
0 | 51 { |
52 assert(false); | |
53 } | |
54 | |
72 | 55 override void semantic(Scope sc) |
0 | 56 { |
57 int i; | |
58 | |
59 //printf("InterfaceDeclaration.semantic(%s), type = %p\n", toChars(), type); | |
60 if (inuse) | |
61 return; | |
62 | |
63 if (!sc) | |
64 sc = scope_; | |
65 if (!parent && sc.parent && !sc.parent.isModule()) | |
66 parent = sc.parent; | |
67 | |
68 type = type.semantic(loc, sc); | |
69 handle = type; | |
70 | |
71 if (!members) // if forward reference | |
72 { | |
73 //printf("\tinterface '%s' is forward referenced\n", toChars()); | |
74 return; | |
75 } | |
76 if (symtab) // if already done | |
77 { | |
78 if (!scope_) | |
79 return; | |
80 } | |
81 else | |
82 symtab = new DsymbolTable(); | |
83 | |
84 Scope scx = null; | |
85 if (scope_) | |
86 { | |
87 sc = scope_; | |
88 scx = scope_; // save so we don't make redundant copies | |
89 scope_ = null; | |
90 } | |
91 | |
92 if (sc.stc & STC.STCdeprecated) | |
93 { | |
94 isdeprecated = true; | |
95 } | |
96 | |
97 // Expand any tuples in baseclasses[] | |
98 for (i = 0; i < baseclasses.dim; ) | |
99 { | |
100 BaseClass b = cast(BaseClass)baseclasses.data[0]; | |
101 b.type = b.type.semantic(loc, sc); | |
102 Type tb = b.type.toBasetype(); | |
103 | |
104 if (tb.ty == TY.Ttuple) | |
105 { TypeTuple tup = cast(TypeTuple)tb; | |
106 PROT protection = b.protection; | |
107 baseclasses.remove(i); | |
108 size_t dim = Argument.dim(tup.arguments); | |
109 for (size_t j = 0; j < dim; j++) | |
110 { Argument arg = Argument.getNth(tup.arguments, j); | |
111 b = new BaseClass(arg.type, protection); | |
112 baseclasses.insert(i + j, cast(void*)b); | |
113 } | |
114 } | |
115 else | |
116 i++; | |
117 } | |
118 | |
119 if (!baseclasses.dim && sc.linkage == LINK.LINKcpp) | |
120 cpp = 1; | |
121 | |
122 // Check for errors, handle forward references | |
123 for (i = 0; i < baseclasses.dim; ) | |
124 { | |
125 TypeClass tc; | |
126 BaseClass b; | |
127 Type tb; | |
128 | |
129 b = cast(BaseClass)baseclasses.data[i]; | |
130 b.type = b.type.semantic(loc, sc); | |
131 tb = b.type.toBasetype(); | |
132 if (tb.ty == TY.Tclass) | |
133 tc = cast(TypeClass)tb; | |
134 else | |
135 tc = null; | |
136 if (!tc || !tc.sym.isInterfaceDeclaration()) | |
137 { | |
138 error("base type must be interface, not %s", b.type.toChars()); | |
139 baseclasses.remove(i); | |
140 continue; | |
141 } | |
142 else | |
143 { | |
144 // Check for duplicate interfaces | |
145 for (size_t j = 0; j < i; j++) | |
146 { | |
147 BaseClass b2 = cast(BaseClass)baseclasses.data[j]; | |
148 if (b2.base is tc.sym) | |
149 error("inherits from duplicate interface %s", b2.base.toChars()); | |
150 } | |
151 | |
152 b.base = tc.sym; | |
153 if (b.base == this || isBaseOf2(b.base)) | |
154 { | |
155 error("circular inheritance of interface"); | |
156 baseclasses.remove(i); | |
157 continue; | |
158 } | |
159 if (!b.base.symtab) | |
160 { | |
161 // Try to resolve forward reference | |
162 if (sc.mustsemantic && b.base.scope_) | |
163 b.base.semantic(null); | |
164 } | |
165 if (!b.base.symtab || b.base.scope_ || b.base.inuse) | |
166 { | |
167 //error("forward reference of base class %s", baseClass.toChars()); | |
168 // Forward reference of base, try again later | |
169 //printf("\ttry later, forward reference of base %s\n", b.base.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:
77
diff
changeset
|
170 scope_ = scx ? scx : sc.clone(); |
0 | 171 scope_.setNoFree(); |
172 scope_.module_.addDeferredSemantic(this); | |
173 return; | |
174 } | |
175 } | |
176 static if (false) { | |
177 // Inherit const/invariant from base class | |
178 storage_class |= b.base.storage_class & STC.STC_TYPECTOR; | |
179 } | |
180 i++; | |
181 } | |
182 | |
183 interfaces_dim = baseclasses.dim; | |
184 interfaces = cast(BaseClass*)baseclasses.data; | |
185 | |
186 interfaceSemantic(sc); | |
187 | |
188 if (vtblOffset()) | |
189 vtbl.push(cast(void*)this); // leave room at vtbl[0] for classinfo | |
190 | |
191 // Cat together the vtbl[]'s from base interfaces | |
192 for (i = 0; i < interfaces_dim; i++) | |
193 { | |
194 BaseClass b = interfaces[i]; | |
195 | |
196 // Skip if b has already appeared | |
197 for (int k = 0; k < i; k++) | |
198 { | |
199 if (b == interfaces[i]) | |
200 goto Lcontinue; | |
201 } | |
202 | |
203 // Copy vtbl[] from base class | |
204 if (b.base.vtblOffset()) | |
205 { int d = b.base.vtbl.dim; | |
206 if (d > 1) | |
207 { | |
208 vtbl.reserve(d - 1); | |
209 for (int j = 1; j < d; j++) | |
210 vtbl.push(b.base.vtbl.data[j]); | |
211 } | |
212 } | |
213 else | |
214 { | |
215 vtbl.append(b.base.vtbl); | |
216 } | |
217 | |
218 Lcontinue: | |
219 ; | |
220 } | |
221 | |
222 protection = sc.protection; | |
223 storage_class |= sc.stc & STC.STC_TYPECTOR; | |
224 | |
77
ad4792a1cfd6
more D-ification container accessing
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
72
diff
changeset
|
225 foreach(Dsymbol s; members) |
13 | 226 s.addMember(sc, this, true); |
0 | 227 |
228 sc = sc.push(this); | |
229 sc.stc &= ~(STC.STCfinal | STC.STCauto | STC.STCscope | STC.STCstatic | | |
230 STC.STCabstract | STC.STCdeprecated | STC.STC_TYPECTOR | STC.STCtls | STC.STCgshared); | |
231 sc.stc |= storage_class & STC.STC_TYPECTOR; | |
232 sc.parent = this; | |
233 if (isCOMinterface()) | |
234 sc.linkage = LINK.LINKwindows; | |
235 else if (isCPPinterface()) | |
236 sc.linkage = LINK.LINKcpp; | |
237 sc.structalign = 8; | |
238 structalign = sc.structalign; | |
239 sc.offset = PTRSIZE * 2; | |
240 inuse++; | |
77
ad4792a1cfd6
more D-ification container accessing
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
72
diff
changeset
|
241 foreach(Dsymbol s; members) |
0 | 242 s.semantic(sc); |
243 inuse--; | |
244 //members.print(); | |
245 sc.pop(); | |
246 //printf("-InterfaceDeclaration.semantic(%s), type = %p\n", toChars(), type); | |
247 } | |
248 | |
72 | 249 override bool isBaseOf(ClassDeclaration cd, int* poffset) |
0 | 250 { |
251 uint j; | |
252 | |
253 //printf("%s.InterfaceDeclaration.isBaseOf(cd = '%s')\n", toChars(), cd.toChars()); | |
254 assert(!baseClass); | |
255 for (j = 0; j < cd.interfaces_dim; j++) | |
256 { | |
257 BaseClass b = cd.interfaces[j]; | |
258 | |
259 //printf("\tbase %s\n", b.base.toChars()); | |
260 if (this == b.base) | |
261 { | |
262 //printf("\tfound at offset %d\n", b.offset); | |
263 if (poffset) | |
264 { | |
265 *poffset = b.offset; | |
266 if (j && cd.isInterfaceDeclaration()) | |
267 *poffset = OFFSET_RUNTIME; | |
268 } | |
269 return true; | |
270 } | |
271 if (isBaseOf(b, poffset)) | |
272 { | |
273 if (j && poffset && cd.isInterfaceDeclaration()) | |
274 *poffset = OFFSET_RUNTIME; | |
275 return true; | |
276 } | |
277 } | |
278 | |
279 if (cd.baseClass && isBaseOf(cd.baseClass, poffset)) | |
280 return true; | |
281 | |
282 if (poffset) | |
283 *poffset = 0; | |
284 return false; | |
285 } | |
286 | |
287 bool isBaseOf(BaseClass bc, int* poffset) | |
288 { | |
64 | 289 //printf("%s.InterfaceDeclaration.isBaseOf(bc = '%s')\n", toChars(), bc.base.toChars()); |
290 for (uint j = 0; j < bc.baseInterfaces.length; j++) | |
291 { | |
292 BaseClass b = bc.baseInterfaces[j]; | |
293 | |
294 if (this == b.base) | |
295 { | |
296 if (poffset) | |
297 { | |
298 *poffset = b.offset; | |
299 if (j && bc.base.isInterfaceDeclaration()) | |
300 *poffset = OFFSET_RUNTIME; | |
301 } | |
302 return 1; | |
303 } | |
304 if (isBaseOf(b, poffset)) | |
305 { | |
306 if (j && poffset && bc.base.isInterfaceDeclaration()) | |
307 *poffset = OFFSET_RUNTIME; | |
308 return 1; | |
309 } | |
310 } | |
311 if (poffset) | |
312 *poffset = 0; | |
313 return 0; | |
0 | 314 } |
315 | |
72 | 316 override string kind() |
0 | 317 { |
318 assert(false); | |
319 } | |
320 | |
321 /**************************************** | |
322 * Determine if slot 0 of the vtbl[] is reserved for something else. | |
323 * For class objects, yes, this is where the ClassInfo ptr goes. | |
324 * For COM interfaces, no. | |
325 * For non-COM interfaces, yes, this is where the Interface ptr goes. | |
326 */ | |
72 | 327 override int vtblOffset() |
0 | 328 { |
329 if (isCOMinterface() || isCPPinterface()) | |
330 return 0; | |
331 return 1; | |
332 } | |
333 | |
334 version (DMDV2) { | |
72 | 335 override bool isCPPinterface() |
0 | 336 { |
337 return cpp; | |
338 } | |
339 } | |
72 | 340 override bool isCOMinterface() |
0 | 341 { |
342 return com; | |
343 } | |
344 | |
72 | 345 override void toObjFile(int multiobj) // compile to .obj file |
0 | 346 { |
347 uint i; | |
348 uint offset; | |
349 Symbol* sinit; | |
350 SC scclass; | |
351 | |
352 //printf("InterfaceDeclaration.toObjFile('%s')\n", toChars()); | |
353 | |
354 if (!members) | |
355 return; | |
356 | |
357 if (global.params.symdebug) | |
358 toDebug(); | |
359 | |
360 scclass = SCglobal; | |
361 if (inTemplateInstance()) | |
362 scclass = SCcomdat; | |
363 | |
364 // Put out the members | |
77
ad4792a1cfd6
more D-ification container accessing
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
72
diff
changeset
|
365 foreach(Dsymbol member; members) |
0 | 366 { |
367 if (!member.isFuncDeclaration()) | |
368 member.toObjFile(0); | |
369 } | |
370 | |
371 // Generate C symbols | |
372 toSymbol(); | |
373 | |
374 ////////////////////////////////////////////// | |
375 | |
376 // Put out the TypeInfo | |
377 type.getTypeInfo(null); | |
378 type.vtinfo.toObjFile(multiobj); | |
379 | |
380 ////////////////////////////////////////////// | |
381 | |
382 // Put out the ClassInfo | |
383 csym.Sclass = scclass; | |
384 csym.Sfl = FLdata; | |
385 | |
386 /* The layout is: | |
387 { | |
388 void **vptr; | |
389 monitor_t monitor; | |
390 byte[] initializer; // static initialization data | |
391 char[] name; // class name | |
392 void *[] vtbl; | |
393 Interface[] interfaces; | |
394 Object *base; // base class | |
395 void *destructor; | |
396 void *invariant; // class invariant | |
397 uint flags; | |
398 void *deallocator; | |
399 OffsetTypeInfo[] offTi; | |
400 void *defaultConstructor; | |
401 #if DMDV2 | |
402 const(MemberInfo[]) function(string) xgetMembers; // module getMembers() function | |
403 #endif | |
404 TypeInfo typeinfo; | |
405 } | |
406 */ | |
407 dt_t *dt = null; | |
408 | |
409 if (classinfo) | |
410 dtxoff(&dt, classinfo.toVtblSymbol(), 0, TYnptr); // vtbl for ClassInfo | |
411 else | |
412 dtdword(&dt, 0); // BUG: should be an assert() | |
413 dtdword(&dt, 0); // monitor | |
414 | |
415 // initializer[] | |
416 dtdword(&dt, 0); // size | |
417 dtdword(&dt, 0); // initializer | |
418 | |
419 // name[] | |
420 string name = toPrettyChars(); | |
421 size_t namelen = name.length; | |
422 dtdword(&dt, namelen); | |
423 dtabytes(&dt, TYnptr, 0, namelen + 1, toStringz(name)); | |
424 | |
425 // vtbl[] | |
426 dtdword(&dt, 0); | |
427 dtdword(&dt, 0); | |
428 | |
429 // vtblInterfaces.data[] | |
430 dtdword(&dt, vtblInterfaces.dim); | |
431 if (vtblInterfaces.dim) | |
432 { | |
433 if (classinfo) | |
434 assert(classinfo.structsize == CLASSINFO_SIZE); | |
435 offset = CLASSINFO_SIZE; | |
436 dtxoff(&dt, csym, offset, TYnptr); // (*) | |
437 } | |
438 else | |
439 dtdword(&dt, 0); | |
440 | |
441 // base | |
442 assert(!baseClass); | |
443 dtdword(&dt, 0); | |
444 | |
445 // dtor | |
446 dtdword(&dt, 0); | |
447 | |
448 // invariant | |
449 dtdword(&dt, 0); | |
450 | |
451 // flags | |
452 dtdword(&dt, 4 | isCOMinterface() | 32); | |
453 | |
454 // deallocator | |
455 dtdword(&dt, 0); | |
456 | |
457 // offTi[] | |
458 dtdword(&dt, 0); | |
459 dtdword(&dt, 0); // null for now, fix later | |
460 | |
461 // defaultConstructor | |
462 dtdword(&dt, 0); | |
463 | |
464 version (DMDV2) { | |
465 // xgetMembers | |
466 dtdword(&dt, 0); | |
467 } | |
468 | |
469 dtxoff(&dt, type.vtinfo.toSymbol(), 0, TYnptr); // typeinfo | |
470 | |
471 ////////////////////////////////////////////// | |
472 | |
473 // Put out vtblInterfaces.data[]. Must immediately follow csym, because | |
474 // of the fixup (*) | |
475 | |
476 offset += vtblInterfaces.dim * (4 * PTRSIZE); | |
477 for (i = 0; i < vtblInterfaces.dim; i++) | |
478 { | |
479 BaseClass b = cast(BaseClass)vtblInterfaces.data[i]; | |
480 ClassDeclaration id = b.base; | |
481 | |
482 // ClassInfo | |
483 dtxoff(&dt, id.toSymbol(), 0, TYnptr); | |
484 | |
485 // vtbl[] | |
486 dtdword(&dt, 0); | |
487 dtdword(&dt, 0); | |
488 | |
489 // this offset | |
490 dtdword(&dt, b.offset); | |
491 } | |
492 | |
493 csym.Sdt = dt; | |
494 version (ELFOBJ) { | |
22
fd4acc376c45
Implemented object file output and linking on linux.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
495 csym.Sseg = Segment.CDATA; |
0 | 496 } |
497 version (MACHOBJ) { | |
22
fd4acc376c45
Implemented object file output and linking on linux.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
498 csym.Sseg = Segment.DATA; |
0 | 499 } |
500 outdata(csym); | |
501 if (isExport()) | |
502 obj_export(csym,0); | |
503 } | |
504 | |
505 /************************************* | |
506 * Create the "InterfaceInfo" symbol | |
507 */ | |
72 | 508 override Symbol* toSymbol() |
0 | 509 { |
510 if (!csym) | |
511 { | |
512 Symbol *s; | |
513 | |
514 if (!scc) | |
515 scc = fake_classsym(Id.ClassInfo); | |
516 | |
517 s = toSymbolX("__Interface", SCextern, scc.Stype, "Z"); | |
518 s.Sfl = FLextern; | |
519 s.Sflags |= SFLnodebug; | |
520 csym = s; | |
521 slist_add(s); | |
522 } | |
523 return csym; | |
524 } | |
525 | |
72 | 526 override InterfaceDeclaration isInterfaceDeclaration() { return this; } |
22
fd4acc376c45
Implemented object file output and linking on linux.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
527 } |