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