Use `internal' accessibility for namespace members by default
[vala-lang.git] / vala / valaproperty.vala
blobc8fb63f475e111681bb9ef3c00f00e043017f6de
1 /* valaproperty.vala
3 * Copyright (C) 2006-2008 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.prop = this;
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.prop = this;
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; }
150 * Nickname of this property.
152 public string nick {
153 get {
154 if (_nick == null) {
155 _nick = get_canonical_name ();
157 return _nick;
159 set { _nick = value; }
163 * The long description of this property.
165 public string blurb {
166 get {
167 if (_blurb == null) {
168 _blurb = get_canonical_name ();
170 return _blurb;
172 set { _blurb = value; }
175 private bool lock_used = false;
177 private DataType _data_type;
179 private string? _nick;
180 private string? _blurb;
182 private weak Property _base_property;
183 private Property _base_interface_property;
184 private bool base_properties_valid;
185 PropertyAccessor? _get_accessor;
186 PropertyAccessor? _set_accessor;
189 * Creates a new property.
191 * @param name property name
192 * @param type property type
193 * @param get_accessor get accessor
194 * @param set_accessor set/construct accessor
195 * @param source reference to source code
196 * @return newly created property
198 public Property (string name, DataType? property_type, PropertyAccessor? get_accessor, PropertyAccessor? set_accessor, SourceReference? source_reference = null) {
199 base (name, source_reference);
200 this.property_type = property_type;
201 this.get_accessor = get_accessor;
202 this.set_accessor = set_accessor;
205 public override void accept (CodeVisitor visitor) {
206 visitor.visit_member (this);
208 visitor.visit_property (this);
211 public override void accept_children (CodeVisitor visitor) {
212 property_type.accept (visitor);
214 if (get_accessor != null) {
215 get_accessor.accept (visitor);
217 if (set_accessor != null) {
218 set_accessor.accept (visitor);
221 if (default_expression != null) {
222 default_expression.accept (visitor);
227 * Returns the C name of this property in upper case. Words are
228 * separated by underscores. The upper case C name of the class is
229 * prefix of the result.
231 * @return the upper case name to be used in C code
233 public string get_upper_case_cname () {
234 return "%s_%s".printf (parent_symbol.get_lower_case_cname (null), camel_case_to_lower_case (name)).up ();
238 * Returns the string literal of this property to be used in C code.
240 * @return string literal to be used in C code
242 public CCodeConstant get_canonical_cconstant () {
243 return new CCodeConstant ("\"%s\"".printf (get_canonical_name ()));
246 private string get_canonical_name () {
247 var str = new StringBuilder ();
249 string i = name;
251 while (i.len () > 0) {
252 unichar c = i.get_char ();
253 if (c == '_') {
254 str.append_c ('-');
255 } else {
256 str.append_unichar (c);
259 i = i.next_char ();
262 return str.str;
265 void process_ccode_attribute (Attribute a) {
266 if (a.has_argument ("notify")) {
267 notify = a.get_bool ("notify");
272 * Process all associated attributes.
274 public void process_attributes () {
275 foreach (Attribute a in attributes) {
276 if (a.name == "CCode") {
277 process_ccode_attribute (a);
278 } else if (a.name == "NoAccessorMethod") {
279 no_accessor_method = true;
280 } else if (a.name == "Description") {
281 if (a.has_argument ("nick")) {
282 nick = a.get_string ("nick");
284 if (a.has_argument ("blurb")) {
285 blurb = a.get_string ("blurb");
291 public bool get_lock_used () {
292 return lock_used;
295 public void set_lock_used (bool used) {
296 lock_used = used;
300 * Checks whether the accessors and type of the specified property
301 * matches this property.
303 * @param prop a property
304 * @return true if the specified property is compatible to this
305 * property
307 public bool equals (Property prop2) {
308 if (!prop2.property_type.equals (property_type)) {
309 return false;
312 if ((get_accessor == null && prop2.get_accessor != null) ||
313 (get_accessor != null && prop2.get_accessor == null)) {
314 return false;
317 if ((set_accessor == null && prop2.set_accessor != null) ||
318 (set_accessor != null && prop2.set_accessor == null)) {
319 return false;
322 if (set_accessor != null) {
323 if (set_accessor.writable != prop2.set_accessor.writable) {
324 return false;
326 if (set_accessor.construction != prop2.set_accessor.construction) {
327 return false;
331 return true;
334 public override void replace_type (DataType old_type, DataType new_type) {
335 if (property_type == old_type) {
336 property_type = new_type;
340 private void find_base_properties () {
341 if (base_properties_valid) {
342 return;
345 if (parent_symbol is Class) {
346 /* VAPI classes don't specify overridden properties */
347 if (!parent_symbol.external_package) {
348 find_base_interface_property ((Class) parent_symbol);
349 if (is_virtual || overrides) {
350 find_base_class_property ((Class) parent_symbol);
352 } else if (is_virtual || is_abstract) {
353 _base_property = this;
355 } else if (parent_symbol is Interface) {
356 if (is_virtual || is_abstract) {
357 _base_interface_property = this;
361 base_properties_valid = true;
364 private void find_base_class_property (Class cl) {
365 var sym = cl.scope.lookup (name);
366 if (sym is Property) {
367 var base_property = (Property) sym;
368 if (base_property.is_abstract || base_property.is_virtual) {
369 if (!equals (base_property)) {
370 error = true;
371 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 ()));
372 return;
375 _base_property = base_property;
376 return;
380 if (cl.base_class != null) {
381 find_base_class_property (cl.base_class);
385 private void find_base_interface_property (Class cl) {
386 // FIXME report error if multiple possible base properties are found
387 foreach (DataType type in cl.get_base_types ()) {
388 if (type.data_type is Interface) {
389 var sym = type.data_type.scope.lookup (name);
390 if (sym is Property) {
391 var base_property = (Property) sym;
392 if (base_property.is_abstract) {
393 if (!equals (base_property)) {
394 error = true;
395 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 ()));
396 return;
399 _base_interface_property = base_property;
400 return;
407 public override bool check (SemanticAnalyzer analyzer) {
408 if (checked) {
409 return !error;
412 checked = true;
414 process_attributes ();
416 var old_source_file = analyzer.current_source_file;
417 var old_symbol = analyzer.current_symbol;
419 if (source_reference != null) {
420 analyzer.current_source_file = source_reference.file;
422 analyzer.current_symbol = this;
424 property_type.check (analyzer);
426 if (get_accessor != null) {
427 get_accessor.check (analyzer);
429 if (set_accessor != null) {
430 set_accessor.check (analyzer);
433 if (default_expression != null) {
434 default_expression.check (analyzer);
437 // check whether property type is at least as accessible as the property
438 if (!analyzer.is_type_accessible (this, property_type)) {
439 error = true;
440 Report.error (source_reference, "property type `%s` is less accessible than property `%s`".printf (property_type.to_string (), get_full_name ()));
443 if (!is_internal_symbol ()) {
444 if (property_type is ValueType && !property_type.is_real_struct_type ()) {
445 analyzer.current_source_file.add_type_dependency (property_type, SourceFileDependencyType.HEADER_FULL);
446 } else {
447 analyzer.current_source_file.add_type_dependency (property_type, SourceFileDependencyType.HEADER_SHALLOW);
450 analyzer.current_source_file.add_type_dependency (property_type, SourceFileDependencyType.SOURCE);
452 if (overrides && base_property == null) {
453 Report.error (source_reference, "%s: no suitable property found to override".printf (get_full_name ()));
456 /* construct properties must be public */
457 if (set_accessor != null && set_accessor.construction) {
458 if (access != SymbolAccessibility.PUBLIC) {
459 error = true;
460 Report.error (source_reference, "%s: construct properties must be public".printf (get_full_name ()));
464 if (default_expression != null && !(default_expression.value_type.compatible (property_type))) {
465 error = true;
466 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 ()));
469 analyzer.current_source_file = old_source_file;
470 analyzer.current_symbol = old_symbol;
472 return !error;