0
|
1 module dmd.BaseClass;
|
|
2
|
|
3 import dmd.Type;
|
|
4 import dmd.PROT;
|
|
5 import dmd.ClassDeclaration;
|
|
6 import dmd.Array;
|
|
7 import dmd.TY;
|
|
8 import dmd.TypeFunction;
|
|
9 import dmd.Dsymbol;
|
|
10 import dmd.FuncDeclaration;
|
|
11 import dmd.ArrayTypes;
|
|
12
|
|
13 import core.stdc.stdlib;
|
|
14 import core.stdc.string;
|
|
15
|
4
|
16 import core.memory;
|
2
|
17
|
0
|
18 class BaseClass
|
|
19 {
|
|
20 Type type; // (before semantic processing)
|
|
21 PROT protection; // protection for the base interface
|
|
22
|
|
23 ClassDeclaration base;
|
|
24 int offset; // 'this' pointer offset
|
|
25 Array vtbl; // for interfaces: Array of FuncDeclaration's
|
|
26 // making up the vtbl[]
|
|
27
|
|
28 //int baseInterfaces_dim;
|
|
29 BaseClass[] baseInterfaces; // if BaseClass is an interface, these
|
|
30 // are a copy of the InterfaceDeclaration::interfaces
|
|
31
|
|
32 this()
|
|
33 {
|
|
34 vtbl = new Array();
|
|
35 }
|
|
36
|
|
37 this(Type type, PROT protection)
|
|
38 {
|
|
39 //printf("BaseClass(this = %p, '%s')\n", this, type->toChars());
|
|
40 this.type = type;
|
|
41 this.protection = protection;
|
|
42
|
|
43 vtbl = new Array();
|
|
44 }
|
|
45
|
|
46 /****************************************
|
|
47 * Fill in vtbl[] for base class based on member functions of class cd.
|
|
48 * Input:
|
|
49 * vtbl if !=null, fill it in
|
|
50 * newinstance !=0 means all entries must be filled in by members
|
|
51 * of cd, not members of any base classes of cd.
|
|
52 * Returns:
|
|
53 * true if any entries were filled in by members of cd (not exclusively
|
|
54 * by base classes)
|
|
55 */
|
|
56 bool fillVtbl(ClassDeclaration cd, Array vtbl, int newinstance)
|
|
57 {
|
|
58 ClassDeclaration id = base;
|
|
59 int j;
|
|
60 bool result = false;
|
|
61
|
|
62 //printf("BaseClass.fillVtbl(this='%s', cd='%s')\n", base.toChars(), cd.toChars());
|
|
63 if (vtbl)
|
|
64 vtbl.setDim(base.vtbl.dim);
|
|
65
|
|
66 // first entry is ClassInfo reference
|
|
67 for (j = base.vtblOffset(); j < base.vtbl.dim; j++)
|
|
68 {
|
|
69 FuncDeclaration ifd = (cast(Dsymbol)base.vtbl.data[j]).isFuncDeclaration();
|
|
70 FuncDeclaration fd;
|
|
71 TypeFunction tf;
|
|
72
|
|
73 //printf(" vtbl[%d] is '%s'\n", j, ifd ? ifd.toChars() : "null");
|
|
74
|
|
75 assert(ifd);
|
|
76 // Find corresponding function in this class
|
|
77 tf = (ifd.type.ty == Tfunction) ? cast(TypeFunction)(ifd.type) : null;
|
|
78 fd = cd.findFunc(ifd.ident, tf);
|
|
79 if (fd && !fd.isAbstract())
|
|
80 {
|
|
81 //printf(" found\n");
|
|
82 // Check that calling conventions match
|
|
83 if (fd.linkage != ifd.linkage)
|
|
84 fd.error("linkage doesn't match interface function");
|
|
85
|
|
86 // Check that it is current
|
|
87 if (newinstance &&
|
|
88 fd.toParent() != cd &&
|
|
89 ifd.toParent() == base
|
|
90 )
|
|
91 cd.error("interface function %s.%s is not implemented",
|
|
92 id.toChars(), ifd.ident.toChars());
|
|
93
|
|
94 if (fd.toParent() == cd)
|
|
95 result = true;
|
|
96 }
|
|
97 else
|
|
98 {
|
|
99 //printf(" not found\n");
|
|
100 // BUG: should mark this class as abstract?
|
|
101 if (!cd.isAbstract())
|
|
102 cd.error("interface function %s.%s isn't implemented", id.toChars(), ifd.ident.toChars());
|
|
103 fd = null;
|
|
104 }
|
|
105 if (vtbl)
|
|
106 vtbl.data[j] = cast(void*)fd;
|
|
107 }
|
|
108
|
|
109 return result;
|
|
110 }
|
|
111
|
|
112 void copyBaseInterfaces(BaseClasses vtblInterfaces)
|
|
113 {
|
|
114 //printf("+copyBaseInterfaces(), %s\n", base.toChars());
|
|
115 // if (baseInterfaces_dim)
|
|
116 // return;
|
|
117
|
|
118 baseInterfaces.length = base.interfaces_dim;
|
|
119
|
|
120 int size = BaseClass.classinfo.init.length;
|
|
121
|
|
122 //printf("%s.copyBaseInterfaces()\n", base.toChars());
|
|
123 for (int i = 0; i < baseInterfaces.length; i++)
|
|
124 {
|
|
125 BaseClass b2 = base.interfaces[i];
|
|
126 assert(b2.vtbl.dim == 0); // should not be filled yet
|
|
127
|
2
|
128 void* mem = GC.malloc(size);
|
0
|
129 memcpy(mem, cast(void*)b2, size);
|
|
130
|
|
131 BaseClass b = cast(BaseClass)mem;
|
|
132 baseInterfaces[i] = b;
|
|
133
|
|
134 if (i) // single inheritance is i==0
|
|
135 vtblInterfaces.push(cast(void*)b); // only need for M.I.
|
|
136 b.copyBaseInterfaces(vtblInterfaces);
|
|
137 }
|
|
138 //printf("-copyBaseInterfaces\n");
|
|
139 }
|
|
140 } |