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