comparison dmd/InterfaceDeclaration.d @ 0:10317f0c89a5

Initial commit
author korDen
date Sat, 24 Oct 2009 08:42:06 +0400
parents
children 427f8aa74d28 fd4acc376c45
comparison
equal deleted inserted replaced
-1:000000000000 0:10317f0c89a5
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;
20 import dmd.BaseClass;
21 import dmd.Id;
22
23 import dmd.backend.Symbol;
24 import dmd.backend.TYM;
25 import dmd.backend.dt_t;
26 import dmd.backend.Util;
27 import dmd.codegen.Util;
28 import dmd.backend.SC;
29 import dmd.backend.FL;
30 import dmd.backend.LIST;
31 import dmd.backend.SFL;
32
33 class InterfaceDeclaration : ClassDeclaration
34 {
35 version (DMDV2) {
36 bool cpp; // true if this is a C++ interface
37 }
38 this(Loc loc, Identifier id, BaseClasses baseclasses)
39 {
40 super(loc, id, baseclasses);
41
42 if (id is Id.IUnknown) // IUnknown is the root of all COM interfaces
43 {
44 com = true;
45 cpp = true; // IUnknown is also a C++ interface
46 }
47 }
48
49 Dsymbol syntaxCopy(Dsymbol s)
50 {
51 assert(false);
52 }
53
54 void semantic(Scope sc)
55 {
56 int i;
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[]
97 for (i = 0; i < baseclasses.dim; )
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
122 for (i = 0; i < baseclasses.dim; )
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());
169 scope_ = scx ? scx : new Scope(sc);
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
191 for (i = 0; i < interfaces_dim; i++)
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
224 for (i = 0; i < members.dim; i++)
225 {
226 Dsymbol s = cast(Dsymbol)members.data[i];
227 s.addMember(sc, this, 1);
228 }
229
230 sc = sc.push(this);
231 sc.stc &= ~(STC.STCfinal | STC.STCauto | STC.STCscope | STC.STCstatic |
232 STC.STCabstract | STC.STCdeprecated | STC.STC_TYPECTOR | STC.STCtls | STC.STCgshared);
233 sc.stc |= storage_class & STC.STC_TYPECTOR;
234 sc.parent = this;
235 if (isCOMinterface())
236 sc.linkage = LINK.LINKwindows;
237 else if (isCPPinterface())
238 sc.linkage = LINK.LINKcpp;
239 sc.structalign = 8;
240 structalign = sc.structalign;
241 sc.offset = PTRSIZE * 2;
242 inuse++;
243 for (i = 0; i < members.dim; i++)
244 {
245 Dsymbol s = cast(Dsymbol)members.data[i];
246 s.semantic(sc);
247 }
248 inuse--;
249 //members.print();
250 sc.pop();
251 //printf("-InterfaceDeclaration.semantic(%s), type = %p\n", toChars(), type);
252 }
253
254 bool isBaseOf(ClassDeclaration cd, int* poffset)
255 {
256 uint j;
257
258 //printf("%s.InterfaceDeclaration.isBaseOf(cd = '%s')\n", toChars(), cd.toChars());
259 assert(!baseClass);
260 for (j = 0; j < cd.interfaces_dim; j++)
261 {
262 BaseClass b = cd.interfaces[j];
263
264 //printf("\tbase %s\n", b.base.toChars());
265 if (this == b.base)
266 {
267 //printf("\tfound at offset %d\n", b.offset);
268 if (poffset)
269 {
270 *poffset = b.offset;
271 if (j && cd.isInterfaceDeclaration())
272 *poffset = OFFSET_RUNTIME;
273 }
274 return true;
275 }
276 if (isBaseOf(b, poffset))
277 {
278 if (j && poffset && cd.isInterfaceDeclaration())
279 *poffset = OFFSET_RUNTIME;
280 return true;
281 }
282 }
283
284 if (cd.baseClass && isBaseOf(cd.baseClass, poffset))
285 return true;
286
287 if (poffset)
288 *poffset = 0;
289 return false;
290 }
291
292 bool isBaseOf(BaseClass bc, int* poffset)
293 {
294 assert(false);
295 }
296
297 string kind()
298 {
299 assert(false);
300 }
301
302 /****************************************
303 * Determine if slot 0 of the vtbl[] is reserved for something else.
304 * For class objects, yes, this is where the ClassInfo ptr goes.
305 * For COM interfaces, no.
306 * For non-COM interfaces, yes, this is where the Interface ptr goes.
307 */
308 int vtblOffset()
309 {
310 if (isCOMinterface() || isCPPinterface())
311 return 0;
312 return 1;
313 }
314
315 version (DMDV2) {
316 bool isCPPinterface()
317 {
318 return cpp;
319 }
320 }
321 bool isCOMinterface()
322 {
323 return com;
324 }
325
326 void toObjFile(int multiobj) // compile to .obj file
327 {
328 uint i;
329 uint offset;
330 Symbol* sinit;
331 SC scclass;
332
333 //printf("InterfaceDeclaration.toObjFile('%s')\n", toChars());
334
335 if (!members)
336 return;
337
338 if (global.params.symdebug)
339 toDebug();
340
341 scclass = SCglobal;
342 if (inTemplateInstance())
343 scclass = SCcomdat;
344
345 // Put out the members
346 for (i = 0; i < members.dim; i++)
347 {
348 Dsymbol member;
349
350 member = cast(Dsymbol)members.data[i];
351 if (!member.isFuncDeclaration())
352 member.toObjFile(0);
353 }
354
355 // Generate C symbols
356 toSymbol();
357
358 //////////////////////////////////////////////
359
360 // Put out the TypeInfo
361 type.getTypeInfo(null);
362 type.vtinfo.toObjFile(multiobj);
363
364 //////////////////////////////////////////////
365
366 // Put out the ClassInfo
367 csym.Sclass = scclass;
368 csym.Sfl = FLdata;
369
370 /* The layout is:
371 {
372 void **vptr;
373 monitor_t monitor;
374 byte[] initializer; // static initialization data
375 char[] name; // class name
376 void *[] vtbl;
377 Interface[] interfaces;
378 Object *base; // base class
379 void *destructor;
380 void *invariant; // class invariant
381 uint flags;
382 void *deallocator;
383 OffsetTypeInfo[] offTi;
384 void *defaultConstructor;
385 #if DMDV2
386 const(MemberInfo[]) function(string) xgetMembers; // module getMembers() function
387 #endif
388 TypeInfo typeinfo;
389 }
390 */
391 dt_t *dt = null;
392
393 if (classinfo)
394 dtxoff(&dt, classinfo.toVtblSymbol(), 0, TYnptr); // vtbl for ClassInfo
395 else
396 dtdword(&dt, 0); // BUG: should be an assert()
397 dtdword(&dt, 0); // monitor
398
399 // initializer[]
400 dtdword(&dt, 0); // size
401 dtdword(&dt, 0); // initializer
402
403 // name[]
404 string name = toPrettyChars();
405 size_t namelen = name.length;
406 dtdword(&dt, namelen);
407 dtabytes(&dt, TYnptr, 0, namelen + 1, toStringz(name));
408
409 // vtbl[]
410 dtdword(&dt, 0);
411 dtdword(&dt, 0);
412
413 // vtblInterfaces.data[]
414 dtdword(&dt, vtblInterfaces.dim);
415 if (vtblInterfaces.dim)
416 {
417 if (classinfo)
418 assert(classinfo.structsize == CLASSINFO_SIZE);
419 offset = CLASSINFO_SIZE;
420 dtxoff(&dt, csym, offset, TYnptr); // (*)
421 }
422 else
423 dtdword(&dt, 0);
424
425 // base
426 assert(!baseClass);
427 dtdword(&dt, 0);
428
429 // dtor
430 dtdword(&dt, 0);
431
432 // invariant
433 dtdword(&dt, 0);
434
435 // flags
436 dtdword(&dt, 4 | isCOMinterface() | 32);
437
438 // deallocator
439 dtdword(&dt, 0);
440
441 // offTi[]
442 dtdword(&dt, 0);
443 dtdword(&dt, 0); // null for now, fix later
444
445 // defaultConstructor
446 dtdword(&dt, 0);
447
448 version (DMDV2) {
449 // xgetMembers
450 dtdword(&dt, 0);
451 }
452
453 dtxoff(&dt, type.vtinfo.toSymbol(), 0, TYnptr); // typeinfo
454
455 //////////////////////////////////////////////
456
457 // Put out vtblInterfaces.data[]. Must immediately follow csym, because
458 // of the fixup (*)
459
460 offset += vtblInterfaces.dim * (4 * PTRSIZE);
461 for (i = 0; i < vtblInterfaces.dim; i++)
462 {
463 BaseClass b = cast(BaseClass)vtblInterfaces.data[i];
464 ClassDeclaration id = b.base;
465
466 // ClassInfo
467 dtxoff(&dt, id.toSymbol(), 0, TYnptr);
468
469 // vtbl[]
470 dtdword(&dt, 0);
471 dtdword(&dt, 0);
472
473 // this offset
474 dtdword(&dt, b.offset);
475 }
476
477 csym.Sdt = dt;
478 version (ELFOBJ) {
479 csym.Sseg = CDATA;
480 }
481 version (MACHOBJ) {
482 csym.Sseg = DATA;
483 }
484 outdata(csym);
485 if (isExport())
486 obj_export(csym,0);
487 }
488
489 /*************************************
490 * Create the "InterfaceInfo" symbol
491 */
492 Symbol* toSymbol()
493 {
494 if (!csym)
495 {
496 Symbol *s;
497
498 if (!scc)
499 scc = fake_classsym(Id.ClassInfo);
500
501 s = toSymbolX("__Interface", SCextern, scc.Stype, "Z");
502 s.Sfl = FLextern;
503 s.Sflags |= SFLnodebug;
504 csym = s;
505 slist_add(s);
506 }
507 return csym;
508 }
509
510 InterfaceDeclaration isInterfaceDeclaration() { return this; }
511 }