Do not allow abstract properties in non-abstract classes
[vala-lang.git] / vala / valaproperty.vala
blobd55b687f5f8735036c4596b63652da89485ad831
1 /* valaproperty.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>
21 * Raffaele Sandrini <raffaele@sandrini.ch>
24 using GLib;
26 /**
27 * Represents a property declaration in the source code.
29 public class Vala.Property : Member, 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 FormalParameter this_parameter { get; set; }
74 /**
75 * Specifies whether a `notify` signal should be emitted on property
76 * changes.
78 public bool notify { get; set; default = true; }
80 /**
81 * Specifies whether the implementation of this property does not
82 * provide getter/setter methods.
84 public bool no_accessor_method { get; set; }
86 /**
87 * Specifies whether automatic accessor code generation should be
88 * disabled.
90 public bool interface_only { get; set; }
92 /**
93 * Specifies whether this property is abstract. Abstract properties have
94 * no accessor bodies, may only be specified within abstract classes and
95 * interfaces, and must be overriden by derived non-abstract classes.
97 public bool is_abstract { get; set; }
99 /**
100 * Specifies whether this property is virtual. Virtual properties may be
101 * overridden by derived classes.
103 public bool is_virtual { get; set; }
106 * Specifies whether this property overrides a virtual or abstract
107 * property of a base type.
109 public bool overrides { get; set; }
112 * Reference the the Field that holds this property
114 public Field field { get; set; }
117 * Specifies whether this field may only be accessed with an instance of
118 * the contained type.
120 public MemberBinding binding { get; set; default = MemberBinding.INSTANCE; }
123 * Specifies the virtual or abstract property this property overrides.
124 * Reference must be weak as virtual properties set base_property to
125 * themselves.
127 public Property base_property {
128 get {
129 find_base_properties ();
130 return _base_property;
135 * Specifies the abstract interface property this property implements.
137 public Property base_interface_property {
138 get {
139 find_base_properties ();
140 return _base_interface_property;
145 * Specifies the default value of this property.
147 public Expression default_expression { get; set; }
149 public bool no_array_length { get; set; }
151 public bool array_null_terminated { get; set; }
154 * Nickname of this property.
156 public string nick {
157 get {
158 if (_nick == null) {
159 _nick = get_canonical_name ();
161 return _nick;
163 set { _nick = value; }
167 * The long description of this property.
169 public string blurb {
170 get {
171 if (_blurb == null) {
172 _blurb = get_canonical_name ();
174 return _blurb;
176 set { _blurb = value; }
179 private bool lock_used = false;
181 private DataType _data_type;
183 private string? _nick;
184 private string? _blurb;
186 private weak Property _base_property;
187 private Property _base_interface_property;
188 private bool base_properties_valid;
189 PropertyAccessor? _get_accessor;
190 PropertyAccessor? _set_accessor;
193 * Creates a new property.
195 * @param name property name
196 * @param type property type
197 * @param get_accessor get accessor
198 * @param set_accessor set/construct accessor
199 * @param source reference to source code
200 * @return newly created property
202 public Property (string name, DataType? property_type, PropertyAccessor? get_accessor, PropertyAccessor? set_accessor, SourceReference? source_reference = null, Comment? comment = null) {
203 base (name, source_reference, comment);
204 this.property_type = property_type;
205 this.get_accessor = get_accessor;
206 this.set_accessor = set_accessor;
209 public override void accept (CodeVisitor visitor) {
210 visitor.visit_member (this);
212 visitor.visit_property (this);
215 public override void accept_children (CodeVisitor visitor) {
216 property_type.accept (visitor);
218 if (get_accessor != null) {
219 get_accessor.accept (visitor);
221 if (set_accessor != null) {
222 set_accessor.accept (visitor);
225 if (default_expression != null) {
226 default_expression.accept (visitor);
231 * Returns the C name of this property in upper case. Words are
232 * separated by underscores. The upper case C name of the class is
233 * prefix of the result.
235 * @return the upper case name to be used in C code
237 public string get_upper_case_cname () {
238 return "%s_%s".printf (parent_symbol.get_lower_case_cname (null), camel_case_to_lower_case (name)).up ();
242 * Returns the string literal of this property to be used in C code.
244 * @return string literal to be used in C code
246 public CCodeConstant get_canonical_cconstant () {
247 return new CCodeConstant ("\"%s\"".printf (get_canonical_name ()));
250 public string get_canonical_name () {
251 var str = new StringBuilder ();
253 string i = name;
255 while (i.len () > 0) {
256 unichar c = i.get_char ();
257 if (c == '_') {
258 str.append_c ('-');
259 } else {
260 str.append_unichar (c);
263 i = i.next_char ();
266 return str.str;
269 void process_ccode_attribute (Attribute a) {
270 if (a.has_argument ("notify")) {
271 notify = a.get_bool ("notify");
273 if (a.has_argument ("array_length")) {
274 no_array_length = !a.get_bool ("array_length");
276 if (a.has_argument ("array_null_terminated")) {
277 array_null_terminated = a.get_bool ("array_null_terminated");
282 * Process all associated attributes.
284 public void process_attributes () {
285 foreach (Attribute a in attributes) {
286 if (a.name == "CCode") {
287 process_ccode_attribute (a);
288 } else if (a.name == "NoAccessorMethod") {
289 no_accessor_method = true;
290 } else if (a.name == "Description") {
291 if (a.has_argument ("nick")) {
292 nick = a.get_string ("nick");
294 if (a.has_argument ("blurb")) {
295 blurb = a.get_string ("blurb");
297 } else if (a.name == "Deprecated") {
298 process_deprecated_attribute (a);
303 public bool get_lock_used () {
304 return lock_used;
307 public void set_lock_used (bool used) {
308 lock_used = used;
312 * Checks whether the accessors and type of the specified property
313 * matches this property.
315 * @param prop a property
316 * @return true if the specified property is compatible to this
317 * property
319 public bool equals (Property prop2) {
320 if ((get_accessor == null && prop2.get_accessor != null) ||
321 (get_accessor != null && prop2.get_accessor == null)) {
322 return false;
325 if ((set_accessor == null && prop2.set_accessor != null) ||
326 (set_accessor != null && prop2.set_accessor == null)) {
327 return false;
330 if (get_accessor != null) {
331 // check accessor value_type instead of property_type
332 // due to possible ownership differences
333 if (!prop2.get_accessor.value_type.equals (get_accessor.value_type)) {
334 return false;
338 if (set_accessor != null) {
339 // check accessor value_type instead of property_type
340 // due to possible ownership differences
341 if (!prop2.set_accessor.value_type.equals (set_accessor.value_type)) {
342 return false;
345 if (set_accessor.writable != prop2.set_accessor.writable) {
346 return false;
348 if (set_accessor.construction != prop2.set_accessor.construction) {
349 return false;
353 return true;
356 public override void replace_type (DataType old_type, DataType new_type) {
357 if (property_type == old_type) {
358 property_type = new_type;
362 private void find_base_properties () {
363 if (base_properties_valid) {
364 return;
367 if (parent_symbol is Class) {
368 find_base_interface_property ((Class) parent_symbol);
369 if (is_virtual || overrides) {
370 find_base_class_property ((Class) parent_symbol);
372 } else if (parent_symbol is Interface) {
373 if (is_virtual || is_abstract) {
374 _base_interface_property = this;
378 base_properties_valid = true;
381 private void find_base_class_property (Class cl) {
382 var sym = cl.scope.lookup (name);
383 if (sym is Property) {
384 var base_property = (Property) sym;
385 if (base_property.is_abstract || base_property.is_virtual) {
386 if (!equals (base_property)) {
387 error = true;
388 Report.error (source_reference, "Type and/or accessors of overriding property `%s' do not match overridden property `%s'.".printf (get_full_name (), base_property.get_full_name ()));
389 return;
392 _base_property = base_property;
393 return;
397 if (cl.base_class != null) {
398 find_base_class_property (cl.base_class);
402 private void find_base_interface_property (Class cl) {
403 // FIXME report error if multiple possible base properties are found
404 foreach (DataType type in cl.get_base_types ()) {
405 if (type.data_type is Interface) {
406 var sym = type.data_type.scope.lookup (name);
407 if (sym is Property) {
408 var base_property = (Property) sym;
409 if (base_property.is_abstract) {
410 if (!equals (base_property)) {
411 error = true;
412 Report.error (source_reference, "Type and/or accessors of overriding property `%s' do not match overridden property `%s'.".printf (get_full_name (), base_property.get_full_name ()));
413 return;
416 _base_interface_property = base_property;
417 return;
424 public override bool check (SemanticAnalyzer analyzer) {
425 if (checked) {
426 return !error;
429 checked = true;
431 process_attributes ();
433 if (is_abstract) {
434 if (parent_symbol is Class) {
435 var cl = (Class) parent_symbol;
436 if (!cl.is_abstract) {
437 error = true;
438 Report.error (source_reference, "Abstract properties may not be declared in non-abstract classes");
439 return false;
441 } else if (!(parent_symbol is Interface)) {
442 error = true;
443 Report.error (source_reference, "Abstract properties may not be declared outside of classes and interfaces");
444 return false;
446 } else if (is_virtual) {
447 if (!(parent_symbol is Class) && !(parent_symbol is Interface)) {
448 error = true;
449 Report.error (source_reference, "Virtual properties may not be declared outside of classes and interfaces");
450 return false;
453 if (parent_symbol is Class) {
454 var cl = (Class) parent_symbol;
455 if (cl.is_compact) {
456 error = true;
457 Report.error (source_reference, "Virtual properties may not be declared in compact classes");
458 return false;
461 } else if (overrides) {
462 if (!(parent_symbol is Class)) {
463 error = true;
464 Report.error (source_reference, "Properties may not be overridden outside of classes");
465 return false;
467 } else if (access == SymbolAccessibility.PROTECTED) {
468 if (!(parent_symbol is Class) && !(parent_symbol is Interface)) {
469 error = true;
470 Report.error (source_reference, "Protected properties may not be declared outside of classes and interfaces");
471 return false;
475 var old_source_file = analyzer.current_source_file;
476 var old_symbol = analyzer.current_symbol;
478 if (source_reference != null) {
479 analyzer.current_source_file = source_reference.file;
481 analyzer.current_symbol = this;
483 property_type.check (analyzer);
485 if (get_accessor != null) {
486 get_accessor.check (analyzer);
488 if (set_accessor != null) {
489 set_accessor.check (analyzer);
492 if (default_expression != null) {
493 default_expression.check (analyzer);
496 // check whether property type is at least as accessible as the property
497 if (!analyzer.is_type_accessible (this, property_type)) {
498 error = true;
499 Report.error (source_reference, "property type `%s` is less accessible than property `%s`".printf (property_type.to_string (), get_full_name ()));
502 if (overrides && base_property == null) {
503 Report.error (source_reference, "%s: no suitable property found to override".printf (get_full_name ()));
506 if (!external_package && !overrides && !hides && get_hidden_member () != null) {
507 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 ()));
510 /* construct properties must be public */
511 if (set_accessor != null && set_accessor.construction) {
512 if (access != SymbolAccessibility.PUBLIC) {
513 error = true;
514 Report.error (source_reference, "%s: construct properties must be public".printf (get_full_name ()));
518 if (default_expression != null && !default_expression.error && default_expression.value_type != null && !(default_expression.value_type.compatible (property_type))) {
519 error = true;
520 Report.error (default_expression.source_reference, "Expected initializer of type `%s' but got `%s'".printf (property_type.to_string (), default_expression.value_type.to_string ()));
523 analyzer.current_source_file = old_source_file;
524 analyzer.current_symbol = old_symbol;
526 return !error;