Bump version suffix
[vala-lang.git] / vala / valafield.vala
blob4a927d2bda353ad71061c974b0f5aab080a6b1db
1 /* valafield.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 a type or namespace field.
28 public class Vala.Field : Variable, Lockable {
29 /**
30 * Specifies whether this field may only be accessed with an instance of
31 * the contained type.
33 public MemberBinding binding { get; set; default = MemberBinding.INSTANCE; }
35 /**
36 * Specifies whether the field is volatile. Volatile fields are
37 * necessary to allow multi-threaded access.
39 public bool is_volatile { get; set; }
41 private string cname;
43 private bool lock_used = false;
45 /**
46 * Creates a new field.
48 * @param name field name
49 * @param type field type
50 * @param init initializer expression
51 * @param source reference to source code
52 * @return newly created field
54 public Field (string name, DataType variable_type, Expression? initializer, SourceReference? source_reference = null, Comment? comment = null) {
55 base (variable_type, name, initializer, source_reference, comment);
58 public override void accept (CodeVisitor visitor) {
59 visitor.visit_field (this);
62 public override void accept_children (CodeVisitor visitor) {
63 variable_type.accept (visitor);
65 if (initializer != null) {
66 initializer.accept (visitor);
70 /**
71 * Returns the name of this field as it is used in C code.
73 * @return the name to be used in C code
75 public string get_cname () {
76 if (cname == null) {
77 cname = get_default_cname ();
79 return cname;
82 /**
83 * Sets the name of this field as it is used in C code.
85 * @param cname the name to be used in C code
87 public void set_cname (string cname) {
88 this.cname = cname;
91 /**
92 * Returns the default name of this field as it is used in C code.
94 * @return the name to be used in C code by default
96 public string get_default_cname () {
97 if (binding == MemberBinding.STATIC) {
98 return parent_symbol.get_lower_case_cprefix () + name;
99 } else {
100 return name;
104 private void process_ccode_attribute (Attribute a) {
105 if (a.has_argument ("cname")) {
106 set_cname (a.get_string ("cname"));
108 if (a.has_argument ("cheader_filename")) {
109 var val = a.get_string ("cheader_filename");
110 foreach (string filename in val.split (",")) {
111 add_cheader_filename (filename);
114 if (a.has_argument ("array_length")) {
115 no_array_length = !a.get_bool ("array_length");
117 if (a.has_argument ("array_null_terminated")) {
118 array_null_terminated = a.get_bool ("array_null_terminated");
120 if (a.has_argument ("array_length_cname")) {
121 set_array_length_cname (a.get_string ("array_length_cname"));
123 if (a.has_argument ("array_length_cexpr")) {
124 set_array_length_cexpr (a.get_string ("array_length_cexpr"));
126 if (a.has_argument ("array_length_type")) {
127 array_length_type = a.get_string ("array_length_type");
129 if (a.has_argument ("delegate_target")) {
130 no_delegate_target = !a.get_bool ("delegate_target");
135 * Process all associated attributes.
137 public override void process_attributes () {
138 base.process_attributes ();
140 foreach (Attribute a in attributes) {
141 if (a.name == "CCode") {
142 process_ccode_attribute (a);
143 } else if (a.name == "Deprecated") {
144 process_deprecated_attribute (a);
145 } else if (a.name == "Experimental") {
146 process_experimental_attribute (a);
151 public bool get_lock_used () {
152 return lock_used;
155 public void set_lock_used (bool used) {
156 lock_used = used;
159 public override void replace_expression (Expression old_node, Expression new_node) {
160 if (initializer == old_node) {
161 initializer = new_node;
165 public override void replace_type (DataType old_type, DataType new_type) {
166 if (variable_type == old_type) {
167 variable_type = new_type;
171 public string? get_ctype () {
172 var attr = get_attribute ("CCode");
173 if (attr == null) {
174 return null;
176 return attr.get_string ("type");
179 public void set_ctype (string ctype) {
180 var attr = get_attribute ("CCode");
181 if (attr == null) {
182 attr = new Attribute ("CCode");
183 attributes.append (attr);
185 attr.add_argument ("type", "\"%s\"".printf (ctype));
188 public override bool check (CodeContext context) {
189 if (checked) {
190 return !error;
193 checked = true;
195 var old_source_file = context.analyzer.current_source_file;
196 var old_symbol = context.analyzer.current_symbol;
198 if (source_reference != null) {
199 context.analyzer.current_source_file = source_reference.file;
201 context.analyzer.current_symbol = this;
203 if (variable_type is VoidType) {
204 error = true;
205 Report.error (source_reference, "'void' not supported as field type");
206 return false;
209 variable_type.check (context);
211 // check whether field type is at least as accessible as the field
212 if (!context.analyzer.is_type_accessible (this, variable_type)) {
213 error = true;
214 Report.error (source_reference, "field type `%s` is less accessible than field `%s`".printf (variable_type.to_string (), get_full_name ()));
215 return false;
218 process_attributes ();
220 if (initializer != null) {
221 initializer.target_type = variable_type;
223 if (!initializer.check (context)) {
224 error = true;
225 return false;
228 if (initializer.value_type == null) {
229 error = true;
230 Report.error (source_reference, "expression type not allowed as initializer");
231 return false;
234 if (!initializer.value_type.compatible (variable_type)) {
235 error = true;
236 Report.error (source_reference, "Cannot convert from `%s' to `%s'".printf (initializer.value_type.to_string (), variable_type.to_string ()));
237 return false;
240 if (external) {
241 error = true;
242 Report.error (source_reference, "External fields cannot use initializers");
246 if (binding == MemberBinding.INSTANCE && parent_symbol is Interface) {
247 error = true;
248 Report.error (source_reference, "Interfaces may not have instance fields");
249 return false;
252 bool field_in_header = !is_internal_symbol ();
253 if (parent_symbol is Class) {
254 var cl = (Class) parent_symbol;
255 if (cl.is_compact && !cl.is_internal_symbol ()) {
256 // compact classes don't have priv structs
257 field_in_header = true;
261 if (!external_package && !hides && get_hidden_member () != null) {
262 Report.warning (source_reference, "%s hides inherited field `%s'. Use the `new' keyword if hiding was intentional".printf (get_full_name (), get_hidden_member ().get_full_name ()));
265 context.analyzer.current_source_file = old_source_file;
266 context.analyzer.current_symbol = old_symbol;
268 return !error;