Mercurial > projects > ddmd
annotate dmd/EnumDeclaration.d @ 20:1628b221808d
Fleshed out more unimplemented methods.
author | Robert Clipsham <robert@octarineparrot.com> |
---|---|
date | Wed, 07 Apr 2010 00:29:13 +0100 |
parents | 10317f0c89a5 |
children | fd4acc376c45 |
rev | line source |
---|---|
0 | 1 module dmd.EnumDeclaration; |
2 | |
3 import dmd.ScopeDsymbol; | |
4 import dmd.AddExp; | |
5 import dmd.Type; | |
6 import dmd.CmpExp; | |
7 import dmd.IntegerExp; | |
8 import dmd.EqualExp; | |
9 import dmd.TOK; | |
10 import dmd.Id; | |
11 import dmd.TY; | |
12 import dmd.DsymbolTable; | |
13 import dmd.STC; | |
14 import dmd.Expression; | |
15 import dmd.Identifier; | |
16 import dmd.Dsymbol; | |
17 import dmd.Scope; | |
18 import dmd.OutBuffer; | |
19 import dmd.HdrGenState; | |
20 import dmd.Global; | |
21 import dmd.Loc; | |
22 import dmd.TypeEnum; | |
23 import dmd.EnumMember; | |
24 import dmd.DYNCAST; | |
25 import dmd.WANT; | |
26 import dmd.Id; | |
27 import dmd.Lexer; | |
28 | |
29 import dmd.backend.SC; | |
30 import dmd.backend.FL; | |
31 import dmd.backend.Util; | |
32 import dmd.backend.Symbol; | |
33 import dmd.backend.Classsym; | |
34 import dmd.backend.SFL; | |
35 import dmd.backend.LIST; | |
36 import dmd.codegen.Util; | |
37 | |
38 import std.stdio : writef; | |
39 | |
40 class EnumDeclaration : ScopeDsymbol | |
41 { /* enum ident : memtype { ... } | |
42 */ | |
43 Type type; // the TypeEnum | |
44 Type memtype; // type of the members | |
45 | |
46 version (DMDV1) { | |
47 ulong maxval; | |
48 ulong minval; | |
49 ulong defaultval; // default initializer | |
50 } else { | |
51 Expression maxval; | |
52 Expression minval; | |
53 Expression defaultval; // default initializer | |
54 } | |
55 bool isdeprecated; | |
56 | |
57 this(Loc loc, Identifier id, Type memtype) | |
58 { | |
59 super(id); | |
60 this.loc = loc; | |
61 type = new TypeEnum(this); | |
62 this.memtype = memtype; | |
63 } | |
64 | |
65 Dsymbol syntaxCopy(Dsymbol s) | |
66 { | |
20
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
67 Type t = null; |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
68 if (memtype) |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
69 t = memtype.syntaxCopy(); |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
70 |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
71 EnumDeclaration ed; |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
72 if (s) |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
73 { ed = cast(EnumDeclaration)s; |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
74 ed.memtype = t; |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
75 } |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
76 else |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
77 ed = new EnumDeclaration(loc, ident, t); |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
78 ScopeDsymbol.syntaxCopy(ed); |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
79 return ed; |
0 | 80 } |
81 | |
82 void semantic(Scope sc) | |
83 { | |
84 Type t; | |
85 Scope sce; | |
86 | |
87 //writef("EnumDeclaration.semantic(sd = %p, '%s') %s\n", sc.scopesym, sc.scopesym.toChars(), toChars()); | |
88 //writef("EnumDeclaration.semantic() %s\n", toChars()); | |
89 if (!members) // enum ident; | |
90 return; | |
91 | |
92 if (!memtype && !isAnonymous()) | |
93 { | |
94 // Set memtype if we can to reduce fwd reference errors | |
95 memtype = Type.tint32; // case 1) enum ident { ... } | |
96 } | |
97 | |
98 if (symtab) // if already done | |
99 { | |
100 if (!scope_) | |
101 return; // semantic() already completed | |
102 } | |
103 else | |
104 symtab = new DsymbolTable(); | |
105 | |
106 Scope scx = null; | |
107 if (scope_) | |
108 { sc = scope_; | |
109 scx = scope_; // save so we don't make redundant copies | |
110 scope_ = null; | |
111 } | |
112 | |
113 if (sc.stc & STC.STCdeprecated) | |
114 isdeprecated = true; | |
115 | |
116 parent = sc.parent; | |
117 | |
118 /* The separate, and distinct, cases are: | |
119 * 1. enum { ... } | |
120 * 2. enum : memtype { ... } | |
121 * 3. enum ident { ... } | |
122 * 4. enum ident : memtype { ... } | |
123 */ | |
124 | |
125 if (memtype) | |
126 { | |
127 memtype = memtype.semantic(loc, sc); | |
128 | |
129 /* Check to see if memtype is forward referenced | |
130 */ | |
131 if (memtype.ty == TY.Tenum) | |
132 { EnumDeclaration sym = cast(EnumDeclaration)memtype.toDsymbol(sc); | |
133 if (!sym.memtype || !sym.members || !sym.symtab || sym.scope_) | |
134 { | |
135 // memtype is forward referenced, so try again later | |
136 scope_ = scx ? scx : new Scope(sc); | |
137 scope_.setNoFree(); | |
138 scope_.module_.addDeferredSemantic(this); | |
139 writef("\tdeferring %s\n", toChars()); | |
140 return; | |
141 } | |
142 } | |
143 static if (false) { | |
144 // Decided to abandon this restriction for D 2.0 | |
145 if (!memtype.isintegral()) | |
146 { error("base type must be of integral type, not %s", memtype.toChars()); | |
147 memtype = Type.tint32; | |
148 } | |
149 } | |
150 } | |
151 | |
152 type = type.semantic(loc, sc); | |
153 if (isAnonymous()) | |
154 sce = sc; | |
155 else | |
156 { sce = sc.push(this); | |
157 sce.parent = this; | |
158 } | |
159 if (members.dim == 0) | |
160 error("enum %s must have at least one member", toChars()); | |
161 int first = 1; | |
162 Expression elast = null; | |
163 for (int i = 0; i < members.dim; i++) | |
164 { | |
165 EnumMember em = (cast(Dsymbol)members.data[i]).isEnumMember(); | |
166 Expression e; | |
167 | |
168 if (!em) | |
169 /* The e.semantic(sce) can insert other symbols, such as | |
170 * template instances and function literals. | |
171 */ | |
172 continue; | |
173 | |
174 //printf(" Enum member '%s'\n",em.toChars()); | |
175 if (em.type) | |
176 em.type = em.type.semantic(em.loc, sce); | |
177 e = em.value; | |
178 if (e) | |
179 { | |
180 assert(e.dyncast() == DYNCAST.DYNCAST_EXPRESSION); | |
181 e = e.semantic(sce); | |
182 e = e.optimize(WANT.WANTvalue | WANT.WANTinterpret); | |
183 if (memtype) | |
184 { | |
185 e = e.implicitCastTo(sce, memtype); | |
186 e = e.optimize(WANT.WANTvalue | WANT.WANTinterpret); | |
187 if (!isAnonymous()) | |
188 e = e.castTo(sce, type); | |
189 t = memtype; | |
190 } | |
191 else if (em.type) | |
192 { | |
193 e = e.implicitCastTo(sce, em.type); | |
194 e = e.optimize(WANT.WANTvalue | WANT.WANTinterpret); | |
195 assert(isAnonymous()); | |
196 t = e.type; | |
197 } | |
198 else | |
199 t = e.type; | |
200 } | |
201 else if (first) | |
202 { | |
203 if (memtype) | |
204 t = memtype; | |
205 else if (em.type) | |
206 t = em.type; | |
207 else | |
208 t = Type.tint32; | |
209 e = new IntegerExp(em.loc, 0, Type.tint32); | |
210 e = e.implicitCastTo(sce, t); | |
211 e = e.optimize(WANT.WANTvalue | WANT.WANTinterpret); | |
212 if (!isAnonymous()) | |
213 e = e.castTo(sce, type); | |
214 } | |
215 else | |
216 { | |
217 // Set value to (elast + 1). | |
218 // But first check that (elast != t.max) | |
219 assert(elast); | |
220 e = new EqualExp(TOK.TOKequal, em.loc, elast, t.getProperty(Loc(0), Id.max)); | |
221 e = e.semantic(sce); | |
222 e = e.optimize(WANT.WANTvalue | WANT.WANTinterpret); | |
223 if (e.toInteger()) | |
224 error("overflow of enum value %s", elast.toChars()); | |
225 | |
226 // Now set e to (elast + 1) | |
227 e = new AddExp(em.loc, elast, new IntegerExp(em.loc, 1, Type.tint32)); | |
228 e = e.semantic(sce); | |
229 e = e.castTo(sce, elast.type); | |
230 e = e.optimize(WANT.WANTvalue | WANT.WANTinterpret); | |
231 } | |
232 elast = e; | |
233 em.value = e; | |
234 | |
235 // Add to symbol table only after evaluating 'value' | |
236 if (isAnonymous()) | |
237 { | |
238 /* Anonymous enum members get added to enclosing scope. | |
239 */ | |
240 for (Scope scxx = sce; scxx; scxx = scxx.enclosing) | |
241 { | |
242 if (scxx.scopesym) | |
243 { | |
244 if (!scxx.scopesym.symtab) | |
245 scxx.scopesym.symtab = new DsymbolTable(); | |
246 em.addMember(sce, scxx.scopesym, 1); | |
247 break; | |
248 } | |
249 } | |
250 } | |
251 else | |
252 em.addMember(sc, this, 1); | |
253 | |
254 /* Compute .min, .max and .default values. | |
255 * If enum doesn't have a name, we can never identify the enum type, | |
256 * so there is no purpose for a .min, .max or .default | |
257 */ | |
258 if (!isAnonymous()) | |
259 { | |
260 if (first) | |
261 { defaultval = e; | |
262 minval = e; | |
263 maxval = e; | |
264 } | |
265 else | |
266 { Expression ec; | |
267 | |
268 /* In order to work successfully with UDTs, | |
269 * build expressions to do the comparisons, | |
270 * and let the semantic analyzer and constant | |
271 * folder give us the result. | |
272 */ | |
273 | |
274 // Compute if(e < minval) | |
275 ec = new CmpExp(TOK.TOKlt, em.loc, e, minval); | |
276 ec = ec.semantic(sce); | |
277 ec = ec.optimize(WANT.WANTvalue | WANT.WANTinterpret); | |
278 if (ec.toInteger()) | |
279 minval = e; | |
280 | |
281 ec = new CmpExp(TOK.TOKgt, em.loc, e, maxval); | |
282 ec = ec.semantic(sce); | |
283 ec = ec.optimize(WANT.WANTvalue | WANT.WANTinterpret); | |
284 if (ec.toInteger()) | |
285 maxval = e; | |
286 } | |
287 } | |
288 first = 0; | |
289 } | |
290 //printf("defaultval = %lld\n", defaultval); | |
291 | |
292 //if (defaultval) printf("defaultval: %s %s\n", defaultval.toChars(), defaultval.type.toChars()); | |
293 if (sc != sce) | |
294 sce.pop(); | |
295 //members.print(); | |
296 } | |
297 | |
298 bool oneMember(Dsymbol* ps) | |
299 { | |
20
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
300 if (isAnonymous()) |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
301 return Dsymbol.oneMembers(members, ps); |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
302 return Dsymbol.oneMember(ps); |
0 | 303 } |
304 | |
305 void toCBuffer(OutBuffer buf, HdrGenState* hgs) | |
306 { | |
20
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
307 int i; |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
308 |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
309 buf.writestring("enum "); |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
310 if (ident) |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
311 { buf.writestring(ident.toChars()); |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
312 buf.writeByte(' '); |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
313 } |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
314 if (memtype) |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
315 { |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
316 buf.writestring(": "); |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
317 memtype.toCBuffer(buf, null, hgs); |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
318 } |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
319 if (!members) |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
320 { |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
321 buf.writeByte(';'); |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
322 buf.writenl(); |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
323 return; |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
324 } |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
325 buf.writenl(); |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
326 buf.writeByte('{'); |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
327 buf.writenl(); |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
328 for (i = 0; i < members.dim; i++) |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
329 { |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
330 EnumMember em = (cast(Dsymbol)members.data[i]).isEnumMember(); |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
331 if (!em) |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
332 continue; |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
333 //buf.writestring(" "); |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
334 em.toCBuffer(buf, hgs); |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
335 buf.writeByte(','); |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
336 buf.writenl(); |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
337 } |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
338 buf.writeByte('}'); |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
339 buf.writenl(); |
0 | 340 } |
341 | |
342 Type getType() | |
343 { | |
344 return type; | |
345 } | |
346 | |
347 string kind() | |
348 { | |
349 return "enum"; | |
350 } | |
351 | |
352 version (DMDV2) { | |
353 Dsymbol search(Loc, Identifier ident, int flags) | |
354 { | |
20
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
355 //printf("%s.EnumDeclaration.search('%s')\n", toChars(), ident.toChars()); |
0 | 356 if (scope_) |
357 // Try one last time to resolve this enum | |
358 semantic(scope_); | |
359 | |
360 if (!members || !symtab || scope_) | |
361 { | |
362 error("is forward referenced when looking for '%s'", ident.toChars()); | |
363 //*(char*)0=0; | |
364 return null; | |
365 } | |
366 | |
367 return ScopeDsymbol.search(loc, ident, flags); | |
368 } | |
369 } | |
370 bool isDeprecated() // is Dsymbol deprecated? | |
371 { | |
372 return isdeprecated; | |
373 } | |
374 | |
375 void emitComment(Scope sc) | |
376 { | |
377 assert(false); | |
378 } | |
379 | |
380 void toDocBuffer(OutBuffer buf) | |
381 { | |
382 assert(false); | |
383 } | |
384 | |
385 EnumDeclaration isEnumDeclaration() { return this; } | |
386 | |
387 void toObjFile(int multiobj) // compile to .obj file | |
388 { | |
20
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
389 //printf("EnumDeclaration.toObjFile('%s')\n", toChars()); |
0 | 390 version (DMDV2) { |
391 if (isAnonymous()) | |
392 return; | |
393 } | |
394 | |
395 if (global.params.symdebug) | |
396 toDebug(); | |
397 | |
398 type.getTypeInfo(null); // generate TypeInfo | |
399 | |
400 TypeEnum tc = cast(TypeEnum)type; | |
401 if (!tc.sym.defaultval || type.isZeroInit(Loc(0))) { | |
402 ; | |
403 } else { | |
404 SC scclass = SCglobal; | |
405 if (inTemplateInstance()) | |
406 scclass = SCcomdat; | |
407 | |
408 // Generate static initializer | |
409 toInitializer(); | |
410 sinit.Sclass = scclass; | |
411 sinit.Sfl = FLdata; | |
412 version (ELFOBJ) { // Burton | |
413 sinit.Sseg = Segment.CDATA; | |
414 } | |
415 version (MACHOBJ) { | |
416 sinit.Sseg = Segment.DATA; | |
417 } | |
418 version (DMDV1) { | |
419 dtnbytes(&sinit.Sdt, tc.size(0), cast(char*)&tc.sym.defaultval); | |
20
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
420 //sinit.Sdt = tc.sym.init.toDt(); |
0 | 421 } |
422 version (DMDV2) { | |
423 tc.sym.defaultval.toDt(&sinit.Sdt); | |
424 } | |
425 outdata(sinit); | |
426 } | |
427 } | |
428 | |
429 void toDebug() | |
430 { | |
431 assert(false); | |
432 } | |
433 | |
434 int cvMember(ubyte* p) | |
435 { | |
436 assert(false); | |
437 } | |
438 | |
439 Symbol* sinit; | |
440 | |
441 Symbol* toInitializer() | |
442 { | |
443 Symbol* s; | |
444 Classsym* stag; | |
445 | |
446 if (!sinit) | |
447 { | |
448 stag = fake_classsym(Id.ClassInfo); | |
449 Identifier ident_save = ident; | |
450 if (!ident) | |
451 ident = Lexer.uniqueId("__enum"); | |
452 s = toSymbolX("__init", SCextern, stag.Stype, "Z"); | |
453 ident = ident_save; | |
454 s.Sfl = FLextern; | |
455 s.Sflags |= SFLnodebug; | |
456 slist_add(s); | |
457 sinit = s; | |
458 } | |
459 | |
460 return sinit; | |
461 } | |
20
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
462 }; |