annotate dmd/EnumDeclaration.d @ 0:10317f0c89a5

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