1
|
1
|
336
|
2 // Copyright (c) 1999-2008 by Digital Mars
|
1
|
3 // All Rights Reserved
|
|
4 // written by Walter Bright
|
|
5 // http://www.digitalmars.com
|
|
6 // License for redistribution is by either the Artistic License
|
|
7 // in artistic.txt, or the GNU General Public License in gnu.txt.
|
|
8 // See the included readme.txt for details.
|
|
9
|
|
10 #include <stdio.h>
|
|
11 #include <assert.h>
|
|
12
|
|
13 #include "root.h"
|
|
14 #include "enum.h"
|
|
15 #include "mtype.h"
|
|
16 #include "scope.h"
|
336
|
17 #include "declaration.h"
|
1
|
18
|
|
19 /********************************* EnumDeclaration ****************************/
|
|
20
|
|
21 EnumDeclaration::EnumDeclaration(Loc loc, Identifier *id, Type *memtype)
|
|
22 : ScopeDsymbol(id)
|
|
23 {
|
|
24 this->loc = loc;
|
|
25 type = new TypeEnum(this);
|
|
26 this->memtype = memtype;
|
|
27 maxval = 0;
|
|
28 minval = 0;
|
|
29 defaultval = 0;
|
|
30 sinit = NULL;
|
336
|
31 isdeprecated = 0;
|
1
|
32 }
|
|
33
|
|
34 Dsymbol *EnumDeclaration::syntaxCopy(Dsymbol *s)
|
|
35 {
|
|
36 Type *t = NULL;
|
|
37 if (memtype)
|
|
38 t = memtype->syntaxCopy();
|
|
39
|
|
40 EnumDeclaration *ed;
|
|
41 if (s)
|
|
42 { ed = (EnumDeclaration *)s;
|
|
43 ed->memtype = t;
|
|
44 }
|
|
45 else
|
|
46 ed = new EnumDeclaration(loc, ident, t);
|
|
47 ScopeDsymbol::syntaxCopy(ed);
|
|
48 return ed;
|
|
49 }
|
|
50
|
|
51 void EnumDeclaration::semantic(Scope *sc)
|
|
52 { int i;
|
|
53 uinteger_t number;
|
|
54 Type *t;
|
|
55 Scope *sce;
|
|
56
|
|
57 //printf("EnumDeclaration::semantic(sd = %p, '%s')\n", sc->scopesym, sc->scopesym->toChars());
|
|
58 if (symtab) // if already done
|
|
59 return;
|
|
60 if (!memtype)
|
|
61 memtype = Type::tint32;
|
336
|
62 if (sc->stc & STCdeprecated)
|
|
63 isdeprecated = 1;
|
|
64
|
1
|
65 parent = sc->scopesym;
|
|
66 memtype = memtype->semantic(loc, sc);
|
|
67
|
|
68 /* Check to see if memtype is forward referenced
|
|
69 */
|
|
70 if (memtype->ty == Tenum)
|
|
71 { EnumDeclaration *sym = (EnumDeclaration *)memtype->toDsymbol(sc);
|
|
72 if (!sym->memtype)
|
|
73 {
|
|
74 error("base enum %s is forward referenced", sym->toChars());
|
|
75 memtype = Type::tint32;
|
|
76 }
|
|
77 }
|
|
78
|
|
79 if (!memtype->isintegral())
|
|
80 { error("base type must be of integral type, not %s", memtype->toChars());
|
|
81 memtype = Type::tint32;
|
|
82 }
|
|
83
|
|
84 t = isAnonymous() ? memtype : type;
|
|
85 symtab = new DsymbolTable();
|
|
86 sce = sc->push(this);
|
|
87 sce->parent = this;
|
|
88 number = 0;
|
|
89 if (!members) // enum ident;
|
|
90 return;
|
|
91 if (members->dim == 0)
|
|
92 error("enum %s must have at least one member", toChars());
|
|
93 int first = 1;
|
|
94 for (i = 0; i < members->dim; i++)
|
|
95 {
|
|
96 EnumMember *em = ((Dsymbol *)members->data[i])->isEnumMember();
|
|
97 Expression *e;
|
|
98
|
|
99 if (!em)
|
|
100 /* The e->semantic(sce) can insert other symbols, such as
|
|
101 * template instances and function literals.
|
|
102 */
|
|
103 continue;
|
|
104
|
|
105 //printf("Enum member '%s'\n",em->toChars());
|
|
106 e = em->value;
|
|
107 if (e)
|
|
108 {
|
|
109 assert(e->dyncast() == DYNCAST_EXPRESSION);
|
|
110 e = e->semantic(sce);
|
|
111 e = e->optimize(WANTvalue);
|
|
112 // Need to copy it because we're going to change the type
|
|
113 e = e->copy();
|
|
114 e = e->implicitCastTo(sc, memtype);
|
|
115 e = e->optimize(WANTvalue);
|
|
116 number = e->toInteger();
|
|
117 e->type = t;
|
|
118 }
|
|
119 else
|
|
120 { // Default is the previous number plus 1
|
|
121
|
|
122 // Check for overflow
|
|
123 if (!first)
|
|
124 {
|
|
125 switch (t->toBasetype()->ty)
|
|
126 {
|
|
127 case Tbool:
|
|
128 if (number == 2) goto Loverflow;
|
|
129 break;
|
|
130
|
|
131 case Tint8:
|
|
132 if (number == 128) goto Loverflow;
|
|
133 break;
|
|
134
|
|
135 case Tchar:
|
|
136 case Tuns8:
|
|
137 if (number == 256) goto Loverflow;
|
|
138 break;
|
|
139
|
|
140 case Tint16:
|
|
141 if (number == 0x8000) goto Loverflow;
|
|
142 break;
|
|
143
|
|
144 case Twchar:
|
|
145 case Tuns16:
|
|
146 if (number == 0x10000) goto Loverflow;
|
|
147 break;
|
|
148
|
|
149 case Tint32:
|
|
150 if (number == 0x80000000) goto Loverflow;
|
|
151 break;
|
|
152
|
|
153 case Tdchar:
|
|
154 case Tuns32:
|
|
155 if (number == 0x100000000LL) goto Loverflow;
|
|
156 break;
|
|
157
|
|
158 case Tint64:
|
|
159 if (number == 0x8000000000000000LL) goto Loverflow;
|
|
160 break;
|
|
161
|
|
162 case Tuns64:
|
|
163 if (number == 0) goto Loverflow;
|
|
164 break;
|
|
165
|
|
166 Loverflow:
|
|
167 error("overflow of enum value");
|
|
168 break;
|
|
169
|
|
170 default:
|
|
171 assert(0);
|
|
172 }
|
|
173 }
|
|
174 e = new IntegerExp(em->loc, number, t);
|
|
175 }
|
|
176 em->value = e;
|
|
177
|
|
178 // Add to symbol table only after evaluating 'value'
|
|
179 if (isAnonymous())
|
|
180 {
|
|
181 //sce->enclosing->insert(em);
|
|
182 for (Scope *scx = sce->enclosing; scx; scx = scx->enclosing)
|
|
183 {
|
|
184 if (scx->scopesym)
|
|
185 {
|
|
186 if (!scx->scopesym->symtab)
|
|
187 scx->scopesym->symtab = new DsymbolTable();
|
|
188 em->addMember(sce, scx->scopesym, 1);
|
|
189 break;
|
|
190 }
|
|
191 }
|
|
192 }
|
|
193 else
|
|
194 em->addMember(sc, this, 1);
|
|
195
|
|
196 if (first)
|
|
197 { first = 0;
|
|
198 defaultval = number;
|
|
199 minval = number;
|
|
200 maxval = number;
|
|
201 }
|
|
202 else if (memtype->isunsigned())
|
|
203 {
|
|
204 if (number < minval)
|
|
205 minval = number;
|
|
206 if (number > maxval)
|
|
207 maxval = number;
|
|
208 }
|
|
209 else
|
|
210 {
|
|
211 if ((sinteger_t)number < (sinteger_t)minval)
|
|
212 minval = number;
|
|
213 if ((sinteger_t)number > (sinteger_t)maxval)
|
|
214 maxval = number;
|
|
215 }
|
|
216
|
|
217 number++;
|
|
218 }
|
|
219 //printf("defaultval = %lld\n", defaultval);
|
|
220
|
|
221 sce->pop();
|
|
222 //members->print();
|
|
223 }
|
|
224
|
|
225 int EnumDeclaration::oneMember(Dsymbol **ps)
|
|
226 {
|
|
227 if (isAnonymous())
|
|
228 return Dsymbol::oneMembers(members, ps);
|
|
229 return Dsymbol::oneMember(ps);
|
|
230 }
|
|
231
|
|
232 void EnumDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
|
|
233 { int i;
|
|
234
|
|
235 buf->writestring("enum ");
|
|
236 if (ident)
|
|
237 { buf->writestring(ident->toChars());
|
|
238 buf->writeByte(' ');
|
|
239 }
|
|
240 if (memtype)
|
|
241 {
|
|
242 buf->writestring(": ");
|
|
243 memtype->toCBuffer(buf, NULL, hgs);
|
|
244 }
|
|
245 if (!members)
|
|
246 {
|
|
247 buf->writeByte(';');
|
|
248 buf->writenl();
|
|
249 return;
|
|
250 }
|
|
251 buf->writenl();
|
|
252 buf->writeByte('{');
|
|
253 buf->writenl();
|
|
254 for (i = 0; i < members->dim; i++)
|
|
255 {
|
|
256 EnumMember *em = ((Dsymbol *)members->data[i])->isEnumMember();
|
|
257 if (!em)
|
|
258 continue;
|
|
259 //buf->writestring(" ");
|
|
260 em->toCBuffer(buf, hgs);
|
|
261 buf->writeByte(',');
|
|
262 buf->writenl();
|
|
263 }
|
|
264 buf->writeByte('}');
|
|
265 buf->writenl();
|
|
266 }
|
|
267
|
|
268 Type *EnumDeclaration::getType()
|
|
269 {
|
|
270 return type;
|
|
271 }
|
|
272
|
336
|
273 const char *EnumDeclaration::kind()
|
1
|
274 {
|
|
275 return "enum";
|
|
276 }
|
|
277
|
336
|
278 int EnumDeclaration::isDeprecated()
|
|
279 {
|
|
280 return isdeprecated;
|
|
281 }
|
|
282
|
1
|
283 /********************************* EnumMember ****************************/
|
|
284
|
|
285 EnumMember::EnumMember(Loc loc, Identifier *id, Expression *value)
|
|
286 : Dsymbol(id)
|
|
287 {
|
|
288 this->value = value;
|
|
289 this->loc = loc;
|
|
290 }
|
|
291
|
|
292 Dsymbol *EnumMember::syntaxCopy(Dsymbol *s)
|
|
293 {
|
|
294 Expression *e = NULL;
|
|
295 if (value)
|
|
296 e = value->syntaxCopy();
|
|
297
|
|
298 EnumMember *em;
|
|
299 if (s)
|
|
300 { em = (EnumMember *)s;
|
|
301 em->loc = loc;
|
|
302 em->value = e;
|
|
303 }
|
|
304 else
|
|
305 em = new EnumMember(loc, ident, e);
|
|
306 return em;
|
|
307 }
|
|
308
|
|
309 void EnumMember::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
|
|
310 {
|
|
311 buf->writestring(ident->toChars());
|
|
312 if (value)
|
|
313 {
|
|
314 buf->writestring(" = ");
|
|
315 value->toCBuffer(buf, hgs);
|
|
316 }
|
|
317 }
|
|
318
|
336
|
319 const char *EnumMember::kind()
|
1
|
320 {
|
|
321 return "enum member";
|
|
322 }
|
|
323
|
|
324
|