codegen: Custom abstract methods of GLib.Source are handled differently
[vala-gnome.git] / vala / valaproperty.vala
blobe0a8e4390cdb1887b92e8f82426251c7607f2b35
1 /* valaproperty.vala
3 * Copyright (C) 2006-2012 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>
21 * Raffaele Sandrini <raffaele@sandrini.ch>
24 using GLib;
26 /**
27 * Represents a property declaration in the source code.
29 public class Vala.Property : Symbol, Lockable {
30 /**
31 * The property type.
33 public DataType? property_type {
34 get { return _data_type; }
35 set {
36 _data_type = value;
37 if (value != null) {
38 _data_type.parent_node = this;
43 /**
44 * The get accessor of this property if available.
46 public PropertyAccessor? get_accessor {
47 get { return _get_accessor; }
48 set {
49 _get_accessor = value;
50 if (value != null) {
51 value.owner = scope;
56 /**
57 * The set/construct accessor of this property if available.
59 public PropertyAccessor? set_accessor {
60 get { return _set_accessor; }
61 set {
62 _set_accessor = value;
63 if (value != null) {
64 value.owner = scope;
69 /**
70 * Represents the generated `this` parameter in this property.
72 public Parameter this_parameter { get; set; }
74 /**
75 * Specifies whether automatic accessor code generation should be
76 * disabled.
78 public bool interface_only { get; set; }
80 /**
81 * Specifies whether this property is abstract. Abstract properties have
82 * no accessor bodies, may only be specified within abstract classes and
83 * interfaces, and must be overriden by derived non-abstract classes.
85 public bool is_abstract { get; set; }
87 /**
88 * Specifies whether this property is virtual. Virtual properties may be
89 * overridden by derived classes.
91 public bool is_virtual { get; set; }
93 /**
94 * Specifies whether this property overrides a virtual or abstract
95 * property of a base type.
97 public bool overrides { get; set; }
99 /**
100 * Reference the the Field that holds this property
102 public Field field { get; set; }
105 * Specifies whether this field may only be accessed with an instance of
106 * the contained type.
108 public MemberBinding binding { get; set; default = MemberBinding.INSTANCE; }
111 * The nick of this property
113 public string nick {
114 get {
115 if (_nick == null) {
116 _nick = get_attribute_string ("Description", "nick");
117 if (_nick == null) {
118 _nick = name.replace ("_", "-");
121 return _nick;
126 * The blurb of this property
128 public string blurb {
129 get {
130 if (_blurb == null) {
131 _blurb = get_attribute_string ("Description", "blurb");
132 if (_blurb == null) {
133 _blurb = name.replace ("_", "-");
136 return _blurb;
141 * Specifies whether this a property triggers a notify.
143 public bool notify {
144 get {
145 if (_notify == null) {
146 _notify = get_attribute_bool ("CCode", "notify", true);
148 return _notify;
153 * Specifies the virtual or abstract property this property overrides.
154 * Reference must be weak as virtual properties set base_property to
155 * themselves.
157 public Property base_property {
158 get {
159 find_base_properties ();
160 return _base_property;
165 * Specifies the abstract interface property this property implements.
167 public Property base_interface_property {
168 get {
169 find_base_properties ();
170 return _base_interface_property;
175 * Specifies the default value of this property.
177 public Expression initializer {
178 get {
179 return _initializer;
181 set {
182 _initializer = value;
183 _initializer.parent_node = this;
187 private Expression _initializer;
189 private bool lock_used = false;
191 private DataType _data_type;
193 private weak Property _base_property;
194 private Property _base_interface_property;
195 private bool base_properties_valid;
196 PropertyAccessor? _get_accessor;
197 PropertyAccessor? _set_accessor;
198 private string? _nick;
199 private string? _blurb;
200 private bool? _notify;
203 * Creates a new property.
205 * @param name property name
206 * @param property_type property type
207 * @param get_accessor get accessor
208 * @param set_accessor set/construct accessor
209 * @param source_reference reference to source code
210 * @return newly created property
212 public Property (string name, DataType? property_type, PropertyAccessor? get_accessor, PropertyAccessor? set_accessor, SourceReference? source_reference = null, Comment? comment = null) {
213 base (name, source_reference, comment);
214 this.property_type = property_type;
215 this.get_accessor = get_accessor;
216 this.set_accessor = set_accessor;
219 public override void accept (CodeVisitor visitor) {
220 visitor.visit_property (this);
223 public override void accept_children (CodeVisitor visitor) {
224 property_type.accept (visitor);
226 if (get_accessor != null) {
227 get_accessor.accept (visitor);
229 if (set_accessor != null) {
230 set_accessor.accept (visitor);
233 if (initializer != null) {
234 initializer.accept (visitor);
238 public bool get_lock_used () {
239 return lock_used;
242 public void set_lock_used (bool used) {
243 lock_used = used;
247 * Checks whether the accessors of this property are compatible
248 * with the specified base property.
250 * @param base_property a property
251 * @param invalid_match error string about which check failed
252 * @return true if the specified property is compatible to this property
254 public bool compatible (Property base_property, out string? invalid_match) {
255 if ((get_accessor == null && base_property.get_accessor != null) ||
256 (get_accessor != null && base_property.get_accessor == null)) {
257 invalid_match = "incompatible get accessor";
258 return false;
261 if ((set_accessor == null && base_property.set_accessor != null) ||
262 (set_accessor != null && base_property.set_accessor == null)) {
263 invalid_match = "incompatible set accessor";
264 return false;
267 var object_type = SemanticAnalyzer.get_data_type_for_symbol (parent_symbol);
269 if (get_accessor != null) {
270 // check accessor value_type instead of property_type
271 // due to possible ownership differences
272 var actual_base_type = base_property.get_accessor.value_type.get_actual_type (object_type, null, this);
273 if (!actual_base_type.equals (get_accessor.value_type)) {
274 invalid_match = "incompatible get accessor type";
275 return false;
279 if (set_accessor != null) {
280 // check accessor value_type instead of property_type
281 // due to possible ownership differences
282 var actual_base_type = base_property.set_accessor.value_type.get_actual_type (object_type, null, this);
283 if (!actual_base_type.equals (set_accessor.value_type)) {
284 invalid_match = "incompatible set accessor type";
285 return false;
288 if (set_accessor.writable != base_property.set_accessor.writable) {
289 invalid_match = "incompatible set accessor";
290 return false;
292 if (set_accessor.construction != base_property.set_accessor.construction) {
293 invalid_match = "incompatible set accessor";
294 return false;
298 invalid_match = null;
299 return true;
302 public override void replace_type (DataType old_type, DataType new_type) {
303 if (property_type == old_type) {
304 property_type = new_type;
308 public override void replace_expression (Expression old_node, Expression new_node) {
309 if (initializer == old_node) {
310 initializer = new_node;
314 private void find_base_properties () {
315 if (base_properties_valid) {
316 return;
319 if (parent_symbol is Class) {
320 find_base_interface_property ((Class) parent_symbol);
321 if (is_virtual || overrides) {
322 find_base_class_property ((Class) parent_symbol);
324 } else if (parent_symbol is Interface) {
325 if (is_virtual || is_abstract) {
326 _base_interface_property = this;
330 base_properties_valid = true;
333 private void find_base_class_property (Class cl) {
334 var sym = cl.scope.lookup (name);
335 if (sym is Property) {
336 var base_property = (Property) sym;
337 if (base_property.is_abstract || base_property.is_virtual) {
338 string invalid_match;
339 if (!compatible (base_property, out invalid_match)) {
340 error = true;
341 Report.error (source_reference, "Type and/or accessors of overriding property `%s' do not match overridden property `%s': %s.".printf (get_full_name (), base_property.get_full_name (), invalid_match));
342 return;
345 _base_property = base_property;
346 return;
350 if (cl.base_class != null) {
351 find_base_class_property (cl.base_class);
355 private void find_base_interface_property (Class cl) {
356 // FIXME report error if multiple possible base properties are found
357 foreach (DataType type in cl.get_base_types ()) {
358 if (type.data_type is Interface) {
359 var sym = type.data_type.scope.lookup (name);
360 if (sym is Property) {
361 var base_property = (Property) sym;
362 if (base_property.is_abstract || base_property.is_virtual) {
363 string invalid_match;
364 if (!compatible (base_property, out invalid_match)) {
365 error = true;
366 Report.error (source_reference, "Type and/or accessors of overriding property `%s' do not match overridden property `%s': %s.".printf (get_full_name (), base_property.get_full_name (), invalid_match));
367 return;
370 _base_interface_property = base_property;
371 return;
378 public override bool check (CodeContext context) {
379 if (checked) {
380 return !error;
383 checked = true;
385 if (parent_symbol is Class && (is_abstract || is_virtual)) {
386 var cl = (Class) parent_symbol;
387 if (cl.is_compact && cl.base_class != null) {
388 error = true;
389 Report.error (source_reference, "Abstract and virtual properties may not be declared in derived compact classes");
390 return false;
394 if (is_abstract) {
395 if (parent_symbol is Class) {
396 var cl = (Class) parent_symbol;
397 if (!cl.is_abstract) {
398 error = true;
399 Report.error (source_reference, "Abstract properties may not be declared in non-abstract classes");
400 return false;
402 } else if (!(parent_symbol is Interface)) {
403 error = true;
404 Report.error (source_reference, "Abstract properties may not be declared outside of classes and interfaces");
405 return false;
407 } else if (is_virtual) {
408 if (!(parent_symbol is Class) && !(parent_symbol is Interface)) {
409 error = true;
410 Report.error (source_reference, "Virtual properties may not be declared outside of classes and interfaces");
411 return false;
413 } else if (overrides) {
414 if (!(parent_symbol is Class)) {
415 error = true;
416 Report.error (source_reference, "Properties may not be overridden outside of classes");
417 return false;
419 } else if (access == SymbolAccessibility.PROTECTED) {
420 if (!(parent_symbol is Class) && !(parent_symbol is Interface)) {
421 error = true;
422 Report.error (source_reference, "Protected properties may not be declared outside of classes and interfaces");
423 return false;
427 var old_source_file = context.analyzer.current_source_file;
428 var old_symbol = context.analyzer.current_symbol;
430 if (source_reference != null) {
431 context.analyzer.current_source_file = source_reference.file;
433 context.analyzer.current_symbol = this;
435 if (property_type is VoidType) {
436 error = true;
437 Report.error (source_reference, "'void' not supported as property type");
438 return false;
441 property_type.check (context);
443 if (get_accessor == null && set_accessor == null) {
444 error = true;
445 Report.error (source_reference, "Property `%s' must have a `get' accessor and/or a `set' mutator".printf (get_full_name ()));
446 return false;
449 if (get_accessor != null) {
450 get_accessor.check (context);
452 if (set_accessor != null) {
453 set_accessor.check (context);
456 if (initializer != null) {
457 initializer.check (context);
460 // check whether property type is at least as accessible as the property
461 if (!context.analyzer.is_type_accessible (this, property_type)) {
462 error = true;
463 Report.error (source_reference, "property type `%s` is less accessible than property `%s`".printf (property_type.to_string (), get_full_name ()));
466 if (overrides && base_property == null) {
467 Report.error (source_reference, "%s: no suitable property found to override".printf (get_full_name ()));
470 if (!external_package && !overrides && !hides && get_hidden_member () != null) {
471 Report.warning (source_reference, "%s hides inherited property `%s'. Use the `new' keyword if hiding was intentional".printf (get_full_name (), get_hidden_member ().get_full_name ()));
474 /* construct properties must be public */
475 if (set_accessor != null && set_accessor.construction) {
476 if (access != SymbolAccessibility.PUBLIC) {
477 error = true;
478 Report.error (source_reference, "%s: construct properties must be public".printf (get_full_name ()));
482 if (initializer != null && !initializer.error && initializer.value_type != null && !(initializer.value_type.compatible (property_type))) {
483 error = true;
484 Report.error (initializer.source_reference, "Expected initializer of type `%s' but got `%s'".printf (property_type.to_string (), initializer.value_type.to_string ()));
487 context.analyzer.current_source_file = old_source_file;
488 context.analyzer.current_symbol = old_symbol;
490 return !error;