Line data Source code
1 : /* valaenum.vala
2 : *
3 : * Copyright (C) 2006-2010 Jürg Billeter
4 : *
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.
9 :
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.
14 :
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
18 : *
19 : * Author:
20 : * Jürg Billeter <j@bitron.ch>
21 : */
22 :
23 : using GLib;
24 :
25 : /**
26 : * Represents an enum declaration in the source code.
27 : */
28 420446 : public class Vala.Enum : TypeSymbol {
29 : /**
30 : * Specifies whether this is a flags enum.
31 : */
32 : public bool is_flags {
33 711 : get {
34 711 : if (_is_flags == null) {
35 208 : _is_flags = has_attribute ("Flags");
36 : }
37 711 : return _is_flags;
38 : }
39 : }
40 :
41 418908 : private List<EnumValue> values = new ArrayList<EnumValue> ();
42 418908 : private List<Method> methods = new ArrayList<Method> ();
43 418908 : private List<Constant> constants = new ArrayList<Constant> ();
44 :
45 209454 : private bool? _is_flags;
46 :
47 : /**
48 : * Creates a new enum.
49 : *
50 : * @param name type name
51 : * @param source_reference reference to source code
52 : * @return newly created enum
53 : */
54 628362 : public Enum (string name, SourceReference? source_reference = null, Comment? comment = null) {
55 209454 : base (name, source_reference, comment);
56 : }
57 :
58 : /**
59 : * Appends the specified enum value to the list of values.
60 : *
61 : * @param value an enum value
62 : */
63 1497489 : public void add_value (EnumValue value) {
64 1497489 : value.access = SymbolAccessibility.PUBLIC;
65 :
66 1497489 : values.add (value);
67 1497489 : scope.add (value.name, value);
68 : }
69 :
70 : /**
71 : * Adds the specified method as a member to this enum.
72 : *
73 : * @param m a method
74 : */
75 12338 : public override void add_method (Method m) {
76 12338 : if (m is CreationMethod) {
77 1 : Report.error (m.source_reference, "construction methods may only be declared within classes and structs");
78 :
79 1 : m.error = true;
80 1 : return;
81 : }
82 12337 : if (m.binding == MemberBinding.INSTANCE) {
83 9874 : m.this_parameter = new Parameter ("this", new EnumValueType (this), m.source_reference);
84 9874 : m.scope.add (m.this_parameter.name, m.this_parameter);
85 : }
86 12337 : if (!(m.return_type is VoidType) && m.get_postconditions ().size > 0) {
87 0 : m.result_var = new LocalVariable (m.return_type.copy (), "result", null, m.source_reference);
88 0 : m.result_var.is_result = true;
89 : }
90 :
91 12337 : methods.add (m);
92 12337 : scope.add (m.name, m);
93 : }
94 :
95 : /**
96 : * Adds the specified constant as a member to this enum.
97 : *
98 : * @param c a constant
99 : */
100 1 : public override void add_constant (Constant c) {
101 1 : constants.add (c);
102 1 : scope.add (c.name, c);
103 : }
104 :
105 : /**
106 : * Returns the list of enum values.
107 : *
108 : * @return list of enum values
109 : */
110 1058 : public unowned List<EnumValue> get_values () {
111 1058 : return values;
112 : }
113 :
114 : /**
115 : * Returns the list of methods.
116 : *
117 : * @return list of methods
118 : */
119 826 : public unowned List<Method> get_methods () {
120 826 : return methods;
121 : }
122 :
123 : /**
124 : * Returns the list of constants.
125 : *
126 : * @return list of constants
127 : */
128 819 : public unowned List<Constant> get_constants () {
129 819 : return constants;
130 : }
131 :
132 474770 : public override void accept (CodeVisitor visitor) {
133 474770 : visitor.visit_enum (this);
134 : }
135 :
136 461158 : public override void accept_children (CodeVisitor visitor) {
137 7095750 : foreach (EnumValue value in values) {
138 3317296 : value.accept (visitor);
139 : }
140 :
141 516838 : foreach (Method m in methods) {
142 27840 : m.accept (visitor);
143 : }
144 :
145 461166 : foreach (Constant c in constants) {
146 4 : c.accept (visitor);
147 : }
148 : }
149 :
150 620 : public override bool is_reference_type () {
151 620 : return false;
152 : }
153 :
154 1673018 : public override bool check (CodeContext context) {
155 1673018 : if (checked) {
156 1478622 : return !error;
157 : }
158 :
159 194396 : checked = true;
160 :
161 194396 : var old_source_file = context.analyzer.current_source_file;
162 194396 : var old_symbol = context.analyzer.current_symbol;
163 :
164 194396 : if (source_reference != null) {
165 194396 : context.analyzer.current_source_file = source_reference.file;
166 : }
167 194396 : context.analyzer.current_symbol = this;
168 :
169 194396 : if (values.size <= 0) {
170 1 : Report.error (source_reference, "Enum `%s' requires at least one value", get_full_name ());
171 1 : error = true;
172 1 : return false;
173 : }
174 :
175 2979085 : foreach (EnumValue value in values) {
176 1392345 : value.check (context);
177 : }
178 :
179 217473 : foreach (Method m in methods) {
180 11539 : m.check (context);
181 : }
182 :
183 194397 : foreach (Constant c in constants) {
184 1 : c.check (context);
185 : }
186 :
187 194395 : context.analyzer.current_source_file = old_source_file;
188 194395 : context.analyzer.current_symbol = old_symbol;
189 :
190 194395 : return !error;
191 : }
192 : }
|