Mercurial > projects > ddmd
annotate dmd/TypeEnum.d @ 179:cd48cb899aee
Updated to dmd2.040
author | korDen |
---|---|
date | Sun, 17 Oct 2010 20:56:07 +0400 |
parents | e3afd1303184 |
children | b0d41ff5e0df |
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 { | |
179 | 245 // Enum is forward referenced. We don't need to resolve the whole thing, |
246 // just the base type | |
247 if (sym.memtype) | |
248 { | |
249 sym.memtype = sym.memtype.semantic(sym.loc, sym.scope_); | |
250 } | |
251 else | |
252 { | |
253 if (!sym.isAnonymous()) | |
254 sym.memtype = Type.tint32; | |
255 } | |
135 | 256 } |
0 | 257 if (!sym.memtype) |
258 { | |
259 debug writef("2: "); | |
260 error(sym.loc, "enum %s is forward referenced", sym.toChars()); | |
261 return tint32; | |
262 } | |
263 | |
264 return sym.memtype.toBasetype(); | |
265 } | |
266 | |
72 | 267 override Expression defaultInit(Loc loc) |
0 | 268 { |
269 version (LOGDEFAULTINIT) { | |
270 printf("TypeEnum::defaultInit() '%s'\n", toChars()); | |
271 } | |
272 // Initialize to first member of enum | |
273 //printf("%s\n", sym.defaultval.type.toChars()); | |
274 if (!sym.defaultval) | |
275 { | |
276 error(loc, "forward reference of %s.init", toChars()); | |
277 return new ErrorExp(); | |
278 } | |
279 return sym.defaultval; | |
280 } | |
281 | |
72 | 282 override bool isZeroInit(Loc loc) |
0 | 283 { |
284 if (!sym.defaultval) | |
285 { | |
286 debug writef("3: "); | |
287 error(loc, "enum %s is forward referenced", sym.toChars()); | |
288 return 0; | |
289 } | |
290 return sym.defaultval.isBool(false); | |
291 } | |
292 | |
93
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
79
diff
changeset
|
293 override MATCH deduceType(Scope sc, Type tparam, TemplateParameters parameters, Objects dedtypes) |
0 | 294 { |
93
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
79
diff
changeset
|
295 // Extra check |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
79
diff
changeset
|
296 if (tparam && tparam.ty == Tenum) |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
79
diff
changeset
|
297 { |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
79
diff
changeset
|
298 TypeEnum tp = cast(TypeEnum)tparam; |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
79
diff
changeset
|
299 |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
79
diff
changeset
|
300 if (sym != tp.sym) |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
79
diff
changeset
|
301 return MATCHnomatch; |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
79
diff
changeset
|
302 } |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
79
diff
changeset
|
303 return Type.deduceType(sc, tparam, parameters, dedtypes); |
0 | 304 } |
305 | |
72 | 306 override TypeInfoDeclaration getTypeInfoDeclaration() |
0 | 307 { |
308 return new TypeInfoEnumDeclaration(this); | |
309 } | |
310 | |
72 | 311 override bool hasPointers() |
0 | 312 { |
313 return toBasetype().hasPointers(); | |
314 } | |
315 | |
316 version (CPP_MANGLE) { | |
317 void toCppMangle(OutBuffer buf, CppMangleState* cms) | |
318 { | |
319 assert(false); | |
320 } | |
321 } | |
322 | |
72 | 323 override type* toCtype() |
0 | 324 { |
325 return sym.memtype.toCtype(); | |
326 } | |
72 | 327 } |