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
{
34 if (_is_flags
== null) {
35 _is_flags
= get_attribute ("Flags") != null;
41 private List
<EnumValue
> values
= new ArrayList
<EnumValue
> ();
42 private List
<Method
> methods
= new ArrayList
<Method
> ();
43 private List
<Constant
> constants
= new ArrayList
<Constant
> ();
45 private bool? _is_flags
;
50 * @param name type name
51 * @param source_reference reference to source code
52 * @return newly created enum
54 public Enum (string name
, SourceReference? source_reference
= null, Comment? comment
= null) {
55 base (name
, source_reference
, comment
);
59 * Appends the specified enum value to the list of values.
61 * @param value an enum value
63 public void add_value (EnumValue value
) {
64 value
.access
= SymbolAccessibility
.PUBLIC
;
67 scope
.add (value
.name
, value
);
71 * Adds the specified method as a member to this enum.
75 public override void add_method (Method m
) {
76 if (m is CreationMethod
) {
77 Report
.error (m
.source_reference
, "construction methods may only be declared within classes and structs");
82 if (m
.binding
== MemberBinding
.INSTANCE
) {
83 m
.this_parameter
= new
Parameter ("this", new
EnumValueType (this
));
84 m
.scope
.add (m
.this_parameter
.name
, m
.this_parameter
);
86 if (!(m
.return_type is VoidType
) && m
.get_postconditions ().size
> 0) {
87 m
.result_var
= new
LocalVariable (m
.return_type
.copy (), "result", null, source_reference
);
88 m
.result_var
.is_result
= true;
92 scope
.add (m
.name
, m
);
96 * Adds the specified constant as a member to this enum.
100 public override void add_constant (Constant c
) {
102 scope
.add (c
.name
, c
);
106 * Returns a copy of the list of enum values.
108 * @return list of enum values
110 public List
<EnumValue
> get_values () {
115 public void remove_all_values () {
120 * Returns a copy of the list of methods.
122 * @return list of methods
124 public List
<Method
> get_methods () {
129 * Returns a copy of the list of constants.
131 * @return list of constants
133 public List
<Constant
> get_constants () {
137 public override void accept (CodeVisitor visitor
) {
138 visitor
.visit_enum (this
);
141 public override void accept_children (CodeVisitor visitor
) {
142 foreach (EnumValue value
in values
) {
143 value
.accept (visitor
);
146 foreach (Method m
in methods
) {
150 foreach (Constant c
in constants
) {
155 public override bool is_reference_type () {
159 public override bool check (CodeContext context
) {
166 var old_source_file
= context
.analyzer
.current_source_file
;
167 var old_symbol
= context
.analyzer
.current_symbol
;
169 if (source_reference
!= null) {
170 context
.analyzer
.current_source_file
= source_reference
.file
;
172 context
.analyzer
.current_symbol
= this
;
174 if (values
.size
<= 0) {
175 Report
.error (source_reference
, "Enum `%s' requires at least one value".printf (get_full_name ()));
180 foreach (EnumValue value
in values
) {
181 value
.check (context
);
184 foreach (Method m
in methods
) {
188 foreach (Constant c
in constants
) {
192 context
.analyzer
.current_source_file
= old_source_file
;
193 context
.analyzer
.current_symbol
= old_symbol
;