Release 0.7.8
[vala-lang.git] / vala / valaconstant.vala
blobb382bcb5909a6cb6ac9ebf2a2e52ba04507e69fd
1 /* valaconstant.vala
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
19 * Author:
20 * Jürg Billeter <j@bitron.ch>
23 using GLib;
25 /**
26 * Represents a type member with a constant value.
28 public class Vala.Constant : Member, Lockable {
29 /**
30 * The data type of this constant.
32 public DataType type_reference {
33 get { return _data_type; }
34 set {
35 _data_type = value;
36 _data_type.parent_node = this;
40 /**
41 * The value of this constant.
43 public Expression? initializer {
44 get { return _initializer; }
45 set {
46 _initializer = value;
47 if (_initializer != null) {
48 _initializer.parent_node = this;
53 private string cname;
55 private bool lock_used = false;
57 private DataType _data_type;
59 private Expression _initializer;
61 /**
62 * Creates a new constant.
64 * @param name constant name
65 * @param type_reference constant type
66 * @param initializer constant value
67 * @param source_reference reference to source code
68 * @return newly created constant
70 public Constant (string name, DataType type_reference, Expression? initializer, SourceReference? source_reference, Comment? comment = null) {
71 base (name, source_reference, comment);
72 this.type_reference = type_reference;
73 this.initializer = initializer;
76 public override void accept (CodeVisitor visitor) {
77 visitor.visit_member (this);
79 visitor.visit_constant (this);
82 public override void accept_children (CodeVisitor visitor) {
83 type_reference.accept (visitor);
85 if (initializer != null) {
86 initializer.accept (visitor);
90 /**
91 * Returns the name of this constant as it is used in C code.
93 * @return the name to be used in C code
95 public string get_cname () {
96 if (cname == null) {
97 cname = get_default_cname ();
99 return cname;
103 * Returns the default name of this constant as it is used in C
104 * code.
106 * @return the name to be used in C code by default
108 public string get_default_cname () {
109 if (parent_symbol == null) {
110 // global constant
111 return name;
112 } else {
113 return "%s%s".printf (parent_symbol.get_lower_case_cprefix ().up (), name);
117 public bool get_lock_used () {
118 return lock_used;
121 public void set_lock_used (bool used) {
122 lock_used = used;
125 public override void replace_type (DataType old_type, DataType new_type) {
126 if (type_reference == old_type) {
127 type_reference = new_type;
131 private void process_ccode_attribute (Attribute a) {
132 if (a.has_argument ("cname")) {
133 cname = a.get_string ("cname");
135 if (a.has_argument ("cheader_filename")) {
136 var val = a.get_string ("cheader_filename");
137 foreach (string filename in val.split (",")) {
138 add_cheader_filename (filename);
144 * Process all associated attributes.
146 public void process_attributes () {
147 foreach (Attribute a in attributes) {
148 if (a.name == "CCode") {
149 process_ccode_attribute (a);
154 public override bool check (SemanticAnalyzer analyzer) {
155 if (checked) {
156 return !error;
159 checked = true;
161 process_attributes ();
163 var old_source_file = analyzer.current_source_file;
164 var old_symbol = analyzer.current_symbol;
166 if (source_reference != null) {
167 analyzer.current_source_file = source_reference.file;
169 analyzer.current_symbol = this;
171 type_reference.check (analyzer);
173 if (!check_const_type (type_reference, analyzer)) {
174 error = true;
175 Report.error (source_reference, "`%s' not supported as type for constants".printf (type_reference.to_string ()));
176 return false;
179 if (!external) {
180 if (initializer == null) {
181 error = true;
182 Report.error (source_reference, "A const field requires a initializer to be provided");
183 } else {
184 initializer.target_type = type_reference;
186 initializer.check (analyzer);
188 if (!initializer.value_type.compatible (type_reference)) {
189 error = true;
190 Report.error (source_reference, "Cannot convert from `%s' to `%s'".printf (initializer.value_type.to_string (), type_reference.to_string ()));
191 return false;
194 } else {
195 if (initializer != null) {
196 error = true;
197 Report.error (source_reference, "External constants cannot use initializers");
201 if (!external_package && !hides && get_hidden_member () != null) {
202 Report.warning (source_reference, "%s hides inherited constant `%s'. Use the `new' keyword if hiding was intentional".printf (get_full_name (), get_hidden_member ().get_full_name ()));
205 analyzer.current_source_file = old_source_file;
206 analyzer.current_symbol = old_symbol;
208 return !error;
211 bool check_const_type (DataType type, SemanticAnalyzer analyzer) {
212 if (type is ValueType) {
213 return true;
214 } else if (type is ArrayType) {
215 var array_type = type as ArrayType;
216 return check_const_type (array_type.element_type, analyzer);
217 } else if (type.data_type == analyzer.string_type.data_type) {
218 return true;
219 } else {
220 return false;