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