Debian Codegraph @

gcc/d/dmd/denum.d

raw
1 /**
2 * Define `enum` declarations and `enum` members.
3 *
4 * Specification: $(LINK2 https://dlang.org/spec/enum.html, Enums)
5 *
6 * Copyright: Copyright (C) 1999-2025 by The D Language Foundation, All Rights Reserved
7 * Authors: $(LINK2 https://www.digitalmars.com, Walter Bright)
8 * License: $(LINK2 https://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
9 * Source: $(LINK2 https://github.com/dlang/dmd/blob/master/compiler/src/dmd/denum.d, _denum.d)
10 * Documentation: https://dlang.org/phobos/dmd_denum.html
11 * Coverage: https://codecov.io/gh/dlang/dmd/src/master/compiler/src/dmd/denum.d
12 * References: https://dlang.org/spec/enum.html
13 */
14
15 module dmd.denum;
16
17 import core.stdc.stdio;
18
19 import dmd.astenums;
20 import dmd.attrib;
21 import dmd.gluelayer;
22 import dmd.declaration;
23 import dmd.dsymbol;
24 import dmd.expression;
25 import dmd.id;
26 import dmd.identifier;
27 import dmd.init;
28 import dmd.location;
29 import dmd.mtype;
30 import dmd.visitor;
31
32 /***********************************************************
33 * AST node for `EnumDeclaration`
34 * https://dlang.org/spec/enum.html#EnumDeclaration
35 */
36 extern (C++) final class EnumDeclaration : ScopeDsymbol
37 {
38 /* The separate, and distinct, cases are:
39 * 1. enum { ... }
40 * 2. enum : memtype { ... }
41 * 3. enum id { ... }
42 * 4. enum id : memtype { ... }
43 * 5. enum id : memtype;
44 * 6. enum id;
45 */
46 Type type; // the TypeEnum
47 Type memtype; // type of the members
48
49 Visibility visibility;
50 Expression maxval;
51 Expression minval;
52 Expression defaultval; // default initializer
53
54 // `bool` fields that are compacted into bit fields in a string mixin
55 private extern (D) static struct BitFields
56 {
57 bool isdeprecated;
58 bool added;
59 bool inuse;
60 }
61
62 import dmd.common.bitfields : generateBitFields;
63 mixin(generateBitFields!(BitFields, ubyte));
64
65 Symbol* sinit;
66
67 extern (D) this(Loc loc, Identifier ident, Type memtype)
68 {
69 super(loc, ident);
70 //printf("EnumDeclaration() %p %s : %s\n", this, toChars(), memtype.toChars());
71 type = new TypeEnum(this);
72 this.memtype = memtype;
73 this.dsym = DSYM.enumDeclaration;
74 visibility = Visibility(Visibility.Kind.undefined);
75 }
76
77 override EnumDeclaration syntaxCopy(Dsymbol s)
78 {
79 assert(!s);
80 auto ed = new EnumDeclaration(loc, ident, memtype ? memtype.syntaxCopy() : null);
81 ScopeDsymbol.syntaxCopy(ed);
82 return ed;
83 }
84
85 override Type getType()
86 {
87 return type;
88 }
89
90 override const(char)* kind() const
91 {
92 return "enum";
93 }
94
95 // is Dsymbol deprecated?
96 override bool isDeprecated() const
97 {
98 return isdeprecated;
99 }
100
101 override Visibility visible() pure nothrow @nogc @safe
102 {
103 return visibility;
104 }
105
106
107 /****************
108 * Determine if enum is a special one.
109 * Returns:
110 * `true` if special
111 */
112 bool isSpecial() const nothrow @nogc
113 {
114 return isSpecialEnumIdent(ident) && memtype;
115 }
116
117 override void accept(Visitor v)
118 {
119 v.visit(this);
120 }
121 }
122
123 /***********************************************************
124 * AST node representing a member of an enum.
125 * https://dlang.org/spec/enum.html#EnumMember
126 * https://dlang.org/spec/enum.html#AnonymousEnumMember
127 */
128 extern (C++) final class EnumMember : VarDeclaration
129 {
130 /* Can take the following forms:
131 * 1. id
132 * 2. id = value
133 * 3. type id = value
134 */
135 @property ref value() { return (cast(ExpInitializer)_init).exp; }
136
137 // A cast() is injected to 'value' after dsymbolSemantic(),
138 // but 'origValue' will preserve the original value,
139 // or previous value + 1 if none was specified.
140 Expression origValue;
141
142 Type origType;
143
144 EnumDeclaration ed;
145
146 extern (D) this(Loc loc, Identifier id, Expression value, Type origType)
147 {
148 super(loc, null, id ? id : Id.empty, new ExpInitializer(loc, value));
149 this.origValue = value;
150 this.origType = origType;
151 this.dsym = DSYM.enumMember;
152 }
153
154 extern(D) this(Loc loc, Identifier id, Expression value, Type memtype,
155 STC stc, UserAttributeDeclaration uad, DeprecatedDeclaration dd)
156 {
157 this(loc, id, value, memtype);
158 storage_class = stc;
159 userAttribDecl = uad;
160 depdecl = dd;
161 }
162
163 override EnumMember syntaxCopy(Dsymbol s)
164 {
165 assert(!s);
166 return new EnumMember(
167 loc, ident,
168 value ? value.syntaxCopy() : null,
169 origType ? origType.syntaxCopy() : null,
170 storage_class,
171 userAttribDecl ? userAttribDecl.syntaxCopy(s) : null,
172 depdecl ? depdecl.syntaxCopy(s) : null);
173 }
174
175 override const(char)* kind() const
176 {
177 return "enum member";
178 }
179
180 override void accept(Visitor v)
181 {
182 v.visit(this);
183 }
184 }
185
186 /******************************************
187 * Check for special enum names.
188 *
189 * Special enum names are used by the C++ name mangler to represent
190 * C++ types that are not basic D types.
191 * Params:
192 * ident = identifier to check for specialness
193 * Returns:
194 * `true` if it is special
195 */
196 bool isSpecialEnumIdent(const Identifier ident) @nogc nothrow
197 {
198 return ident == Id.__c_long ||
199 ident == Id.__c_ulong ||
200 ident == Id.__c_longlong ||
201 ident == Id.__c_ulonglong ||
202 ident == Id.__c_long_double ||
203 ident == Id.__c_wchar_t ||
204 ident == Id.__c_complex_float ||
205 ident == Id.__c_complex_double ||
206 ident == Id.__c_complex_real;
207 }