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