Revert "codegen: Don't emit g_type_add_instance_private() in *_register_type()"
[vala-gnome.git] / vala / valafield.vala
blob0227e8a8aab5ffa6d3222b64c9259696004c599c
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 public bool lock_used { get; set; }
43 /**
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) {
81 if (checked) {
82 return !error;
85 checked = true;
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) {
96 error = true;
97 Report.error (source_reference, "'void' not supported as field type");
98 return false;
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)) {
105 error = true;
106 Report.error (source_reference, "field type `%s` is less accessible than field `%s`".printf (variable_type.to_string (), get_full_name ()));
107 return false;
110 if (initializer != null) {
111 initializer.target_type = variable_type;
113 if (!initializer.check (context)) {
114 error = true;
115 return false;
118 if (initializer.value_type == null) {
119 error = true;
120 Report.error (source_reference, "expression type not allowed as initializer");
121 return false;
124 if (!initializer.value_type.compatible (variable_type)) {
125 error = true;
126 Report.error (source_reference, "Cannot convert from `%s' to `%s'".printf (initializer.value_type.to_string (), variable_type.to_string ()));
127 return false;
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 */
134 error = true;
135 Report.error (source_reference, "Invalid assignment from owned expression to unowned variable");
136 return false;
140 if (parent_symbol is Namespace && !initializer.is_constant ()) {
141 error = true;
142 Report.error (source_reference, "Non-constant field initializers not supported in this context");
143 return false;
146 if (parent_symbol is Namespace && initializer.is_constant () && initializer.is_non_null ()) {
147 if (variable_type.is_disposable () && variable_type.value_owned) {
148 error = true;
149 Report.error (source_reference, "Owned namespace fields can only be initialized in a function or method");
150 return false;
154 if (binding == MemberBinding.STATIC && parent_symbol is Class && ((Class)parent_symbol).is_compact && !initializer.is_constant ()) {
155 error = true;
156 Report.error (source_reference, "Static fields in compact classes cannot have non-constant initializers");
157 return false;
160 if (external) {
161 error = true;
162 Report.error (source_reference, "External fields cannot use initializers");
166 if (binding == MemberBinding.INSTANCE && parent_symbol is Interface) {
167 error = true;
168 Report.error (source_reference, "Interfaces may not have instance fields");
169 return false;
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;
188 return !error;