3 * Copyright (C) 2006-2010 Jürg Billeter
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 * Jürg Billeter <j@bitron.ch>
26 * Represents an enum declaration in the source code.
28 public class Vala
.Enum
: TypeSymbol
{
30 * Specifies whether this is a flags enum.
32 public bool is_flags
{ get; set; }
35 * Specifies whether this enum has a registered GType.
37 public bool has_type_id
{ get; set; default = true; }
39 private List
<EnumValue
> values
= new ArrayList
<EnumValue
> ();
40 private List
<Method
> methods
= new ArrayList
<Method
> ();
41 private List
<Constant
> constants
= new ArrayList
<Constant
> ();
43 private string cprefix
;
44 private string lower_case_cprefix
;
45 private string lower_case_csuffix
;
46 private string type_id
;
51 * @param name type name
52 * @param source_reference reference to source code
53 * @return newly created enum
55 public Enum (string name
, SourceReference? source_reference
= null, Comment? comment
= null) {
56 base (name
, source_reference
, comment
);
60 * Appends the specified enum value to the list of values.
62 * @param value an enum value
64 public void add_value (EnumValue value
) {
65 value
.access
= SymbolAccessibility
.PUBLIC
;
68 scope
.add (value
.name
, value
);
72 * Adds the specified method as a member to this enum.
76 public override void add_method (Method m
) {
77 if (m is CreationMethod
) {
78 Report
.error (m
.source_reference
, "construction methods may only be declared within classes and structs");
83 if (m
.binding
== MemberBinding
.INSTANCE
) {
84 m
.this_parameter
= new
FormalParameter ("this", new
EnumValueType (this
));
85 m
.scope
.add (m
.this_parameter
.name
, m
.this_parameter
);
87 if (!(m
.return_type is VoidType
) && (CodeContext
.get ().profile
== Profile
.DOVA
|| m
.get_postconditions ().size
> 0)) {
88 m
.result_var
= new
LocalVariable (m
.return_type
.copy (), "result", null, source_reference
);
89 m
.result_var
.is_result
= true;
93 scope
.add (m
.name
, m
);
97 * Adds the specified constant as a member to this enum.
101 public override void add_constant (Constant c
) {
103 scope
.add (c
.name
, c
);
107 * Returns a copy of the list of enum values.
109 * @return list of enum values
111 public List
<EnumValue
> get_values () {
116 public void remove_all_values () {
121 * Returns a copy of the list of methods.
123 * @return list of methods
125 public List
<Method
> get_methods () {
130 * Returns a copy of the list of constants.
132 * @return list of constants
134 public List
<Constant
> get_constants () {
138 public override void accept (CodeVisitor visitor
) {
139 visitor
.visit_enum (this
);
142 public override void accept_children (CodeVisitor visitor
) {
143 foreach (EnumValue value
in values
) {
144 value
.accept (visitor
);
147 foreach (Method m
in methods
) {
151 foreach (Constant c
in constants
) {
156 public override string get_cname (bool const_type
= false) {
158 var attr
= get_attribute ("CCode");
160 cname
= attr
.get_string ("cname");
163 cname
= "%s%s".printf (parent_symbol
.get_cprefix (), name
);
169 public void set_cname (string cname
) {
173 public override string get_lower_case_cprefix () {
174 if (lower_case_cprefix
== null) {
175 lower_case_cprefix
= "%s_".printf (get_lower_case_cname (null));
177 return lower_case_cprefix
;
180 private string get_lower_case_csuffix () {
181 if (lower_case_csuffix
== null) {
182 lower_case_csuffix
= camel_case_to_lower_case (name
);
184 return lower_case_csuffix
;
187 public override string?
get_lower_case_cname (string? infix
) {
191 return "%s%s%s".printf (parent_symbol
.get_lower_case_cprefix (), infix
, get_lower_case_csuffix ());
194 public override string?
get_upper_case_cname (string? infix
= null) {
195 return get_lower_case_cname (infix
).up ();
198 public override bool is_reference_type () {
202 public override string get_cprefix () {
203 if (cprefix
== null) {
204 cprefix
= "%s_".printf (get_upper_case_cname ());
210 * Sets the string to be prepended to the name of members of this enum
211 * when used in C code.
213 * @param cprefix the prefix to be used in C code
215 public void set_cprefix (string? cprefix
) {
216 this
.cprefix
= cprefix
;
219 private void process_ccode_attribute (Attribute a
) {
220 if (a
.has_argument ("cprefix")) {
221 set_cprefix (a
.get_string ("cprefix"));
223 if (a
.has_argument ("lower_case_csuffix")) {
224 lower_case_csuffix
= a
.get_string ("lower_case_csuffix");
226 if (a
.has_argument ("cheader_filename")) {
227 var val
= a
.get_string ("cheader_filename");
228 foreach (string filename
in val
.split (",")) {
229 add_cheader_filename (filename
);
232 if (a
.has_argument ("has_type_id")) {
233 has_type_id
= a
.get_bool ("has_type_id");
235 if (a
.has_argument ("type_id")) {
236 type_id
= a
.get_string ("type_id");
241 * Process all associated attributes.
243 public void process_attributes () {
244 foreach (Attribute a
in attributes
) {
245 if (a
.name
== "CCode") {
246 process_ccode_attribute (a
);
247 } else if (a
.name
== "Flags") {
249 } else if (a
.name
== "Deprecated") {
250 process_deprecated_attribute (a
);
255 public void set_type_id (string? type_id
) {
256 this
.type_id
= type_id
;
259 public override string?
get_type_id () {
260 if (type_id
== null) {
262 type_id
= get_upper_case_cname ("TYPE_");
264 type_id
= is_flags ?
"G_TYPE_UINT" : "G_TYPE_INT";
271 public override string?
get_marshaller_type_name () {
287 public override string?
get_get_value_function () {
290 return "g_value_get_flags";
292 return "g_value_get_enum";
296 return "g_value_get_uint";
298 return "g_value_get_int";
303 public override string?
get_set_value_function () {
306 return "g_value_set_flags";
308 return "g_value_set_enum";
312 return "g_value_set_uint";
314 return "g_value_set_int";
319 public override string?
get_default_value () {
323 public override bool check (SemanticAnalyzer analyzer
) {
330 process_attributes ();
332 var old_source_file
= analyzer
.current_source_file
;
333 var old_symbol
= analyzer
.current_symbol
;
335 if (source_reference
!= null) {
336 analyzer
.current_source_file
= source_reference
.file
;
338 analyzer
.current_symbol
= this
;
340 foreach (EnumValue value
in values
) {
341 value
.check (analyzer
);
344 foreach (Method m
in methods
) {
348 foreach (Constant c
in constants
) {
352 analyzer
.current_source_file
= old_source_file
;
353 analyzer
.current_symbol
= old_symbol
;