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 a type or namespace field.
28 public class Vala
.Field
: Variable
, Lockable
{
30 * Specifies whether this field may only be accessed with an instance of
33 public MemberBinding binding
{ get; set; default = MemberBinding
.INSTANCE
; }
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 public bool lock_used
{ get; set; }
44 * Creates a new field.
46 * @param name field name
47 * @param variable_type field type
48 * @param initializer initializer expression
49 * @param source_reference reference to source code
50 * @return newly created field
52 public Field (string name
, DataType variable_type
, Expression? initializer
, SourceReference? source_reference
= null, Comment? comment
= null) {
53 base (variable_type
, name
, initializer
, source_reference
, comment
);
56 public override void accept (CodeVisitor visitor
) {
57 visitor
.visit_field (this
);
60 public override void accept_children (CodeVisitor visitor
) {
61 variable_type
.accept (visitor
);
63 if (initializer
!= null) {
64 initializer
.accept (visitor
);
68 public override void replace_expression (Expression old_node
, Expression new_node
) {
69 if (initializer
== old_node
) {
70 initializer
= new_node
;
74 public override void replace_type (DataType old_type
, DataType new_type
) {
75 if (variable_type
== old_type
) {
76 variable_type
= new_type
;
80 public override bool check (CodeContext context
) {
87 var old_source_file
= context
.analyzer
.current_source_file
;
88 var old_symbol
= context
.analyzer
.current_symbol
;
90 if (source_reference
!= null) {
91 context
.analyzer
.current_source_file
= source_reference
.file
;
93 context
.analyzer
.current_symbol
= this
;
95 if (variable_type is VoidType
) {
97 Report
.error (source_reference
, "'void' not supported as field type");
101 variable_type
.check (context
);
103 // check whether field type is at least as accessible as the field
104 if (!context
.analyzer
.is_type_accessible (this
, variable_type
)) {
106 Report
.error (source_reference
, "field type `%s` is less accessible than field `%s`".printf (variable_type
.to_string (), get_full_name ()));
110 if (initializer
!= null) {
111 initializer
.target_type
= variable_type
;
113 if (!initializer
.check (context
)) {
118 if (initializer
.value_type
== null) {
120 Report
.error (source_reference
, "expression type not allowed as initializer");
124 if (!initializer
.value_type
.compatible (variable_type
)) {
126 Report
.error (source_reference
, "Cannot convert from `%s' to `%s'".printf (initializer
.value_type
.to_string (), variable_type
.to_string ()));
130 if (initializer
.value_type
.is_disposable ()) {
131 /* rhs transfers ownership of the expression */
132 if (!(variable_type is PointerType
) && !variable_type
.value_owned
) {
133 /* lhs doesn't own the value */
135 Report
.error (source_reference
, "Invalid assignment from owned expression to unowned variable");
140 if (parent_symbol is Namespace
&& !initializer
.is_constant ()) {
142 Report
.error (source_reference
, "Non-constant field initializers not supported in this context");
146 if (parent_symbol is Namespace
&& initializer
.is_constant () && initializer
.is_non_null ()) {
147 if (variable_type
.is_disposable () && variable_type
.value_owned
) {
149 Report
.error (source_reference
, "Owned namespace fields can only be initialized in a function or method");
154 if (binding
== MemberBinding
.STATIC
&& parent_symbol is Class
&& ((Class
)parent_symbol
).is_compact
&& !initializer
.is_constant ()) {
156 Report
.error (source_reference
, "Static fields in compact classes cannot have non-constant initializers");
162 Report
.error (source_reference
, "External fields cannot use initializers");
166 if (binding
== MemberBinding
.INSTANCE
&& parent_symbol is Interface
) {
168 Report
.error (source_reference
, "Interfaces may not have instance fields");
172 bool field_in_header
= !is_internal_symbol ();
173 if (parent_symbol is Class
) {
174 var cl
= (Class
) parent_symbol
;
175 if (cl
.is_compact
&& !cl
.is_internal_symbol ()) {
176 // compact classes don't have priv structs
177 field_in_header
= true;
181 if (!external_package
&& !hides
&& get_hidden_member () != null) {
182 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 ()));
185 context
.analyzer
.current_source_file
= old_source_file
;
186 context
.analyzer
.current_symbol
= old_symbol
;