gtk+-4.0: Update to 3.93.0+f4c1a404
[vala-gnome.git] / vala / valaenum.vala
blobadd510783a8449da2758ab2baa894cd4efcbfb50
1 /* valaenum.vala
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
19 * Author:
20 * Jürg Billeter <j@bitron.ch>
23 using GLib;
25 /**
26 * Represents an enum declaration in the source code.
28 public class Vala.Enum : TypeSymbol {
29 /**
30 * Specifies whether this is a flags enum.
32 public bool is_flags {
33 get {
34 if (_is_flags == null) {
35 _is_flags = get_attribute ("Flags") != null;
37 return _is_flags;
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;
47 /**
48 * Creates a new enum.
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);
58 /**
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;
66 values.add (value);
67 scope.add (value.name, value);
70 /**
71 * Adds the specified method as a member to this enum.
73 * @param m a method
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");
79 m.error = true;
80 return;
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;
91 methods.add (m);
92 scope.add (m.name, m);
95 /**
96 * Adds the specified constant as a member to this enum.
98 * @param c a constant
100 public override void add_constant (Constant c) {
101 constants.add (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 () {
111 return values;
114 // used by vapigen
115 public void remove_all_values () {
116 values.clear ();
120 * Returns a copy of the list of methods.
122 * @return list of methods
124 public List<Method> get_methods () {
125 return methods;
129 * Returns a copy of the list of constants.
131 * @return list of constants
133 public List<Constant> get_constants () {
134 return 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) {
147 m.accept (visitor);
150 foreach (Constant c in constants) {
151 c.accept (visitor);
155 public override bool is_reference_type () {
156 return false;
159 public override bool check (CodeContext context) {
160 if (checked) {
161 return !error;
164 checked = true;
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 ()));
176 error = true;
177 return false;
180 foreach (EnumValue value in values) {
181 value.check (context);
184 foreach (Method m in methods) {
185 m.check (context);
188 foreach (Constant c in constants) {
189 c.check (context);
192 context.analyzer.current_source_file = old_source_file;
193 context.analyzer.current_symbol = old_symbol;
195 return !error;