3 * Copyright (C) 2006-2009 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>
27 * Represents an enum declaration in the source code.
29 public class Vala
.Enum
: TypeSymbol
{
31 * Specifies whether this is a flags enum.
33 public bool is_flags
{ get; set; }
36 * Specifies whether this enum has a registered GType.
38 public bool has_type_id
{ get; set; default = true; }
40 private Gee
.List
<EnumValue
> values
= new ArrayList
<EnumValue
> ();
41 private Gee
.List
<Method
> methods
= new ArrayList
<Method
> ();
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
) {
66 scope
.add (value
.name
, value
);
70 * Adds the specified method as a member to this enum.
74 public void add_method (Method m
) {
75 if (m is CreationMethod
) {
76 Report
.error (m
.source_reference
, "construction methods may only be declared within classes and structs");
81 if (m
.binding
== MemberBinding
.INSTANCE
) {
82 m
.this_parameter
= new
FormalParameter ("this", new
EnumValueType (this
));
83 m
.scope
.add (m
.this_parameter
.name
, m
.this_parameter
);
87 scope
.add (m
.name
, m
);
91 * Returns a copy of the list of enum values.
93 * @return list of enum values
95 public Gee
.List
<EnumValue
> get_values () {
96 return new ReadOnlyList
<EnumValue
> (values
);
100 * Returns a copy of the list of methods.
102 * @return list of methods
104 public Gee
.List
<Method
> get_methods () {
105 return new ReadOnlyList
<Method
> (methods
);
108 public override void accept (CodeVisitor visitor
) {
109 visitor
.visit_enum (this
);
112 public override void accept_children (CodeVisitor visitor
) {
113 foreach (EnumValue value
in values
) {
114 value
.accept (visitor
);
117 foreach (Method m
in methods
) {
122 public override string get_cname (bool const_type
= false) {
124 var attr
= get_attribute ("CCode");
126 cname
= attr
.get_string ("cname");
129 cname
= "%s%s".printf (parent_symbol
.get_cprefix (), name
);
135 public void set_cname (string cname
) {
139 public override string get_lower_case_cprefix () {
140 if (lower_case_cprefix
== null) {
141 lower_case_cprefix
= "%s_".printf (get_lower_case_cname (null));
143 return lower_case_cprefix
;
146 private string get_lower_case_csuffix () {
147 if (lower_case_csuffix
== null) {
148 lower_case_csuffix
= camel_case_to_lower_case (name
);
150 return lower_case_csuffix
;
153 public override string?
get_lower_case_cname (string? infix
) {
157 return "%s%s%s".printf (parent_symbol
.get_lower_case_cprefix (), infix
, get_lower_case_csuffix ());
160 public override string?
get_upper_case_cname (string? infix
= null) {
161 return get_lower_case_cname (infix
).up ();
164 public override bool is_reference_type () {
168 public override string get_cprefix () {
169 if (cprefix
== null) {
170 cprefix
= "%s_".printf (get_upper_case_cname ());
176 * Sets the string to be prepended to the name of members of this enum
177 * when used in C code.
179 * @param cprefix the prefix to be used in C code
181 public void set_cprefix (string? cprefix
) {
182 this
.cprefix
= cprefix
;
185 private void process_ccode_attribute (Attribute a
) {
186 if (a
.has_argument ("cprefix")) {
187 set_cprefix (a
.get_string ("cprefix"));
189 if (a
.has_argument ("lower_case_csuffix")) {
190 lower_case_csuffix
= a
.get_string ("lower_case_csuffix");
192 if (a
.has_argument ("cheader_filename")) {
193 var val
= a
.get_string ("cheader_filename");
194 foreach (string filename
in val
.split (",")) {
195 add_cheader_filename (filename
);
198 if (a
.has_argument ("has_type_id")) {
199 has_type_id
= a
.get_bool ("has_type_id");
201 if (a
.has_argument ("type_id")) {
202 type_id
= a
.get_string ("type_id");
207 * Process all associated attributes.
209 public void process_attributes () {
210 foreach (Attribute a
in attributes
) {
211 if (a
.name
== "CCode") {
212 process_ccode_attribute (a
);
213 } else if (a
.name
== "Flags") {
219 public void set_type_id (string? type_id
) {
220 this
.type_id
= type_id
;
223 public override string?
get_type_id () {
224 if (type_id
== null) {
226 type_id
= get_upper_case_cname ("TYPE_");
228 type_id
= is_flags ?
"G_TYPE_UINT" : "G_TYPE_INT";
235 public override string?
get_marshaller_type_name () {
251 public override string?
get_get_value_function () {
254 return "g_value_get_flags";
256 return "g_value_get_enum";
260 return "g_value_get_uint";
262 return "g_value_get_int";
267 public override string?
get_set_value_function () {
270 return "g_value_set_flags";
272 return "g_value_set_enum";
276 return "g_value_set_uint";
278 return "g_value_set_int";
283 public override string?
get_default_value () {
287 public override string?
get_type_signature () {
295 public override bool check (SemanticAnalyzer analyzer
) {
302 process_attributes ();
304 var old_source_file
= analyzer
.current_source_file
;
306 if (source_reference
!= null) {
307 analyzer
.current_source_file
= source_reference
.file
;
310 foreach (EnumValue value
in values
) {
311 value
.check (analyzer
);
314 foreach (Method m
in methods
) {
318 analyzer
.current_source_file
= old_source_file
;