Mercurial > projects > ddmd
annotate dmd/TypeEnum.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 | 0c8cc2a10f99 |
children | cd48cb899aee |
rev | line source |
---|---|
0 | 1 module dmd.TypeEnum; |
2 | |
114 | 3 import dmd.common; |
0 | 4 import dmd.Type; |
5 import dmd.EnumDeclaration; | |
6 import dmd.Scope; | |
7 import dmd.Loc; | |
8 import dmd.Id; | |
9 import dmd.ErrorExp; | |
10 import dmd.Dsymbol; | |
11 import dmd.EnumMember; | |
12 import dmd.OutBuffer; | |
13 import dmd.HdrGenState; | |
14 import dmd.Expression; | |
15 import dmd.Identifier; | |
16 import dmd.MATCH; | |
17 import dmd.OutBuffer; | |
18 import dmd.CppMangleState; | |
156 | 19 import dmd.StringExp; |
0 | 20 import dmd.TypeInfoDeclaration; |
21 import dmd.TypeInfoEnumDeclaration; | |
22 import dmd.ArrayTypes; | |
23 import dmd.TY; | |
68
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
0
diff
changeset
|
24 import dmd.MOD; |
0 | 25 import dmd.Util; |
26 | |
27 import dmd.backend.TYPE; | |
28 | |
29 class TypeEnum : Type | |
30 { | |
31 EnumDeclaration sym; | |
32 | |
33 this(EnumDeclaration sym) | |
34 { | |
178 | 35 register(); |
0 | 36 super(TY.Tenum); |
37 this.sym = sym; | |
38 } | |
39 | |
72 | 40 override Type syntaxCopy() |
0 | 41 { |
42 assert(false); | |
43 } | |
44 | |
72 | 45 override ulong size(Loc loc) |
0 | 46 { |
47 if (!sym.memtype) | |
48 { | |
49 error(loc, "enum %s is forward referenced", sym.toChars()); | |
50 return 4; | |
51 } | |
52 return sym.memtype.size(loc); | |
53 } | |
54 | |
156 | 55 override uint alignsize() |
0 | 56 { |
156 | 57 if (!sym.memtype) |
58 { | |
59 debug writef("1: "); | |
60 | |
61 error(Loc(0), "enum %s is forward referenced", sym.toChars()); | |
62 return 4; | |
63 } | |
64 return sym.memtype.alignsize(); | |
0 | 65 } |
156 | 66 |
67 override string toChars() | |
0 | 68 { |
156 | 69 if (mod) |
70 return super.toChars(); | |
71 return sym.toChars(); | |
0 | 72 } |
73 | |
72 | 74 override Type semantic(Loc loc, Scope sc) |
0 | 75 { |
76 //printf("TypeEnum::semantic() %s\n", toChars()); | |
77 //sym.semantic(sc); | |
78 return merge(); | |
79 } | |
80 | |
72 | 81 override Dsymbol toDsymbol(Scope sc) |
0 | 82 { |
83 return sym; | |
84 } | |
85 | |
72 | 86 override void toDecoBuffer(OutBuffer buf, int flag) |
0 | 87 { |
88 string name = sym.mangle(); | |
89 Type.toDecoBuffer(buf, flag); | |
90 buf.printf("%s", name); | |
91 } | |
92 | |
72 | 93 override void toCBuffer2(OutBuffer buf, HdrGenState* hgs, MOD mod) |
0 | 94 { |
68
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
0
diff
changeset
|
95 if (mod != this.mod) |
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
0
diff
changeset
|
96 { |
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
0
diff
changeset
|
97 toCBuffer3(buf, hgs, mod); |
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
0
diff
changeset
|
98 return; |
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
0
diff
changeset
|
99 } |
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
0
diff
changeset
|
100 buf.writestring(sym.toChars()); |
0 | 101 } |
102 | |
72 | 103 override Expression dotExp(Scope sc, Expression e, Identifier ident) |
0 | 104 { |
105 version (LOGDOTEXP) { | |
106 printf("TypeEnum::dotExp(e = '%s', ident = '%s') '%s'\n", e.toChars(), ident.toChars(), toChars()); | |
107 } | |
108 Dsymbol s = sym.search(e.loc, ident, 0); | |
109 if (!s) | |
110 { | |
111 if (ident is Id.max || | |
112 ident is Id.min || | |
113 ident is Id.init_ || | |
114 ident is Id.stringof_ || | |
115 !sym.memtype | |
116 ) | |
117 { | |
118 return getProperty(e.loc, ident); | |
119 } | |
120 | |
121 return sym.memtype.dotExp(sc, e, ident); | |
122 } | |
123 | |
124 EnumMember m = s.isEnumMember(); | |
125 Expression em = m.value.copy(); | |
126 em.loc = e.loc; | |
127 return em; | |
128 } | |
129 | |
156 | 130 override Expression getProperty(Loc loc, Identifier ident) |
0 | 131 { |
156 | 132 Expression e; |
133 | |
134 if (ident is Id.max) | |
135 { | |
136 if (!sym.maxval) | |
137 goto Lfwd; | |
138 e = sym.maxval; | |
139 } | |
140 else if (ident is Id.min) | |
141 { | |
142 if (!sym.minval) | |
143 goto Lfwd; | |
144 e = sym.minval; | |
145 } | |
146 else if (ident is Id.init_) | |
147 { | |
148 e = defaultInitLiteral(loc); | |
149 } | |
150 else if (ident is Id.stringof_) | |
151 { | |
152 string s = toChars(); | |
153 e = new StringExp(loc, s, 'c'); | |
154 Scope sc; | |
155 e = e.semantic(sc); | |
156 } | |
157 else if (ident is Id.mangleof_) | |
158 { | |
159 e = Type.getProperty(loc, ident); | |
160 } | |
161 else | |
162 { | |
163 e = toBasetype().getProperty(loc, ident); | |
164 } | |
165 return e; | |
166 | |
167 Lfwd: | |
168 error(loc, "forward reference of %s.%s", toChars(), ident.toChars()); | |
169 return new ErrorExp(); | |
0 | 170 } |
171 | |
72 | 172 override bool isintegral() |
0 | 173 { |
73 | 174 return sym.memtype.isintegral(); |
0 | 175 } |
176 | |
72 | 177 override bool isfloating() |
0 | 178 { |
73 | 179 return sym.memtype.isfloating(); |
180 } | |
181 | |
79 | 182 override bool isreal() |
73 | 183 { |
184 return sym.memtype.isreal(); | |
185 } | |
186 | |
79 | 187 override bool isimaginary() |
73 | 188 { |
189 return sym.memtype.isimaginary(); | |
190 } | |
191 | |
79 | 192 override bool iscomplex() |
73 | 193 { |
194 return sym.memtype.iscomplex(); | |
195 } | |
196 | |
79 | 197 override bool checkBoolean() |
73 | 198 { |
199 return sym.memtype.checkBoolean(); | |
200 } | |
201 | |
79 | 202 override bool isAssignable() |
73 | 203 { |
204 return sym.memtype.isAssignable(); | |
0 | 205 } |
206 | |
72 | 207 override bool isscalar() |
0 | 208 { |
73 | 209 return sym.memtype.isscalar(); |
0 | 210 } |
211 | |
72 | 212 override bool isunsigned() |
0 | 213 { |
214 return sym.memtype.isunsigned(); | |
215 } | |
216 | |
72 | 217 override MATCH implicitConvTo(Type to) |
0 | 218 { |
219 MATCH m; | |
220 | |
221 //printf("TypeEnum::implicitConvTo()\n"); | |
222 if (ty == to.ty && sym == (cast(TypeEnum)to).sym) | |
223 m = (mod == to.mod) ? MATCHexact : MATCHconst; | |
224 else if (sym.memtype.implicitConvTo(to)) | |
225 m = MATCHconvert; // match with conversions | |
226 else | |
227 m = MATCHnomatch; // no match | |
228 return m; | |
229 } | |
230 | |
72 | 231 override MATCH constConv(Type to) |
0 | 232 { |
135 | 233 if (equals(to)) |
234 return MATCHexact; | |
235 if (ty == to.ty && sym == (cast(TypeEnum)to).sym && | |
236 MODimplicitConv(mod, to.mod)) | |
237 return MATCHconst; | |
238 return MATCHnomatch; | |
0 | 239 } |
240 | |
72 | 241 override Type toBasetype() |
0 | 242 { |
135 | 243 if (sym.scope_) |
244 { | |
245 sym.semantic(null); // attempt to resolve forward reference | |
246 } | |
0 | 247 if (!sym.memtype) |
248 { | |
249 debug writef("2: "); | |
250 error(sym.loc, "enum %s is forward referenced", sym.toChars()); | |
251 return tint32; | |
252 } | |
253 | |
254 return sym.memtype.toBasetype(); | |
255 } | |
256 | |
72 | 257 override Expression defaultInit(Loc loc) |
0 | 258 { |
259 version (LOGDEFAULTINIT) { | |
260 printf("TypeEnum::defaultInit() '%s'\n", toChars()); | |
261 } | |
262 // Initialize to first member of enum | |
263 //printf("%s\n", sym.defaultval.type.toChars()); | |
264 if (!sym.defaultval) | |
265 { | |
266 error(loc, "forward reference of %s.init", toChars()); | |
267 return new ErrorExp(); | |
268 } | |
269 return sym.defaultval; | |
270 } | |
271 | |
72 | 272 override bool isZeroInit(Loc loc) |
0 | 273 { |
274 if (!sym.defaultval) | |
275 { | |
276 debug writef("3: "); | |
277 error(loc, "enum %s is forward referenced", sym.toChars()); | |
278 return 0; | |
279 } | |
280 return sym.defaultval.isBool(false); | |
281 } | |
282 | |
93
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
79
diff
changeset
|
283 override MATCH deduceType(Scope sc, Type tparam, TemplateParameters parameters, Objects dedtypes) |
0 | 284 { |
93
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
79
diff
changeset
|
285 // Extra check |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
79
diff
changeset
|
286 if (tparam && tparam.ty == Tenum) |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
79
diff
changeset
|
287 { |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
79
diff
changeset
|
288 TypeEnum tp = cast(TypeEnum)tparam; |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
79
diff
changeset
|
289 |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
79
diff
changeset
|
290 if (sym != tp.sym) |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
79
diff
changeset
|
291 return MATCHnomatch; |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
79
diff
changeset
|
292 } |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
79
diff
changeset
|
293 return Type.deduceType(sc, tparam, parameters, dedtypes); |
0 | 294 } |
295 | |
72 | 296 override TypeInfoDeclaration getTypeInfoDeclaration() |
0 | 297 { |
298 return new TypeInfoEnumDeclaration(this); | |
299 } | |
300 | |
72 | 301 override bool hasPointers() |
0 | 302 { |
303 return toBasetype().hasPointers(); | |
304 } | |
305 | |
306 version (CPP_MANGLE) { | |
307 void toCppMangle(OutBuffer buf, CppMangleState* cms) | |
308 { | |
309 assert(false); | |
310 } | |
311 } | |
312 | |
72 | 313 override type* toCtype() |
0 | 314 { |
315 return sym.memtype.toCtype(); | |
316 } | |
72 | 317 } |