remove obsolete ref modifier and callback keyword
[vala-lang.git] / vala / valaclass.vala
blob66389cb9f1f683a957e34ee4b732b7d48c32ca99
1 /* valaclass.vala
3 * Copyright (C) 2006-2007 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 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 class declaration in the source code.
28 public class Vala.Class : DataType {
29 /**
30 * Specifies the base class.
32 public Class base_class { get; set; }
34 /**
35 * Specifies whether this class is abstract. Abstract classes may not be
36 * instantiated.
38 public bool is_abstract { get; set; }
40 /**
41 * Specifies whether this class is static. Static classes may not be
42 * instantiated and may only contain static members.
44 public bool is_static { get; set; }
46 /**
47 * Specifies whether this class has private fields.
49 public bool has_private_fields {
50 get {
51 return _has_private_fields;
55 private string cname;
56 private string lower_case_csuffix;
57 private string type_id;
59 private bool _has_private_fields;
61 private List<TypeParameter> type_parameters;
63 private List<TypeReference> base_types;
65 private List<Constant> constants;
66 private List<Field> fields;
67 private List<Method> methods;
68 private List<Property> properties;
69 private List<Signal> signals;
71 /**
72 * Specifies the default construction method.
74 public Method default_construction_method { get; set; }
76 /**
77 * Specifies the instance constructor.
79 public Constructor constructor { get; set; }
81 /**
82 * Specifies the instance destructor.
84 public Destructor destructor { get; set; }
86 /**
87 * Creates a new class.
89 * @param name type name
90 * @param source reference to source code
91 * @return newly created class
93 public Class (string! _name, SourceReference source = null) {
94 name = _name;
95 source_reference = source;
98 /**
99 * Adds the specified class or interface to the list of base types of
100 * this class.
102 * @param type a class or interface reference
104 public void add_base_type (TypeReference! type) {
105 base_types.append (type);
109 * Returns a copy of the base type list.
111 * @return list of base types
113 public List<weak TypeReference> get_base_types () {
114 return base_types.copy ();
118 * Appends the specified parameter to the list of type parameters.
120 * @param p a type parameter
122 public void add_type_parameter (TypeParameter! p) {
123 type_parameters.append (p);
124 p.type = this;
128 * Returns a copy of the type parameter list.
130 * @return list of type parameters
132 public List<weak TypeParameter> get_type_parameters () {
133 return type_parameters.copy ();
137 * Adds the specified constant as a member to this class.
139 * @param c a constant
141 public void add_constant (Constant! c) {
142 constants.append (c);
146 * Adds the specified field as a member to this class.
148 * @param f a field
150 public void add_field (Field! f) {
151 // non_null fields not yet supported due to initialization issues
152 f.type_reference.non_null = false;
153 fields.append (f);
154 if (f.access == MemberAccessibility.PRIVATE && f.instance) {
155 _has_private_fields = true;
160 * Returns a copy of the list of fields.
162 * @return list of fields
164 public List<weak Field> get_fields () {
165 return fields.copy ();
169 * Adds the specified method as a member to this class.
171 * @param m a method
173 public void add_method (Method! m) {
174 methods.append (m);
178 * Returns a copy of the list of methods.
180 * @return list of methods
182 public List<weak Method> get_methods () {
183 return methods.copy ();
187 * Adds the specified property as a member to this class.
189 * @param prop a property
191 public void add_property (Property! prop, bool no_field = false) {
192 properties.append (prop);
194 if (!no_field && prop.set_accessor != null && prop.set_accessor.body == null &&
195 source_reference != null && !source_reference.file.pkg) {
196 /* automatic property accessor body generation */
197 var field_type = prop.type_reference.copy ();
198 // non_null fields not yet supported due to initialization issues
199 field_type.non_null = false;
200 var f = new Field ("_%s".printf (prop.name), field_type, null, prop.source_reference);
201 f.access = MemberAccessibility.PRIVATE;
202 add_field (f);
207 * Returns a copy of the list of properties.
209 * @return list of properties
211 public List<weak Property> get_properties () {
212 return properties.copy ();
216 * Adds the specified signal as a member to this class.
218 * @param sig a signal
220 public void add_signal (Signal! sig) {
221 signals.append (sig);
225 * Returns a copy of the list of signals.
227 * @return list of signals
229 public List<weak Signal> get_signals () {
230 return signals.copy ();
233 public override void accept (CodeVisitor! visitor) {
234 visitor.visit_class (this);
237 public override void accept_children (CodeVisitor! visitor) {
238 foreach (TypeReference type in base_types) {
239 type.accept (visitor);
242 foreach (TypeParameter p in type_parameters) {
243 p.accept (visitor);
246 foreach (Field f in fields) {
247 f.accept (visitor);
250 foreach (Constant c in constants) {
251 c.accept (visitor);
254 foreach (Method m in methods) {
255 m.accept (visitor);
258 foreach (Property prop in properties) {
259 prop.accept (visitor);
262 foreach (Signal sig in signals) {
263 sig.accept (visitor);
266 if (constructor != null) {
267 constructor.accept (visitor);
270 if (destructor != null) {
271 destructor.accept (visitor);
275 public override string get_cname (bool const_type = false) {
276 if (cname == null) {
277 cname = "%s%s".printf (@namespace.get_cprefix (), name);
279 return cname;
283 * Sets the name of this class as it is used in C code.
285 * @param cname the name to be used in C code
287 public void set_cname (string! cname) {
288 this.cname = cname;
291 private string get_lower_case_csuffix () {
292 if (lower_case_csuffix == null) {
293 lower_case_csuffix = Namespace.camel_case_to_lower_case (name);
295 return lower_case_csuffix;
298 public override string get_lower_case_cname (string infix) {
299 if (infix == null) {
300 infix = "";
302 return "%s%s%s".printf (@namespace.get_lower_case_cprefix (), infix, get_lower_case_csuffix ());
305 public override string get_lower_case_cprefix () {
306 return "%s_".printf (get_lower_case_cname (null));
309 public override string get_upper_case_cname (string infix) {
310 return get_lower_case_cname (infix).up ();
313 public override bool is_reference_type () {
314 return true;
317 private void process_ccode_attribute (Attribute! a) {
318 if (a.has_argument ("cname")) {
319 set_cname (a.get_string ("cname"));
321 if (a.has_argument ("cheader_filename")) {
322 var val = a.get_string ("cheader_filename");
323 foreach (string filename in val.split (",")) {
324 add_cheader_filename (filename);
330 * Process all associated attributes.
332 public void process_attributes () {
333 foreach (Attribute a in attributes) {
334 if (a.name == "CCode") {
335 process_ccode_attribute (a);
340 public override string get_type_id () {
341 if (type_id == null) {
342 type_id = get_upper_case_cname ("TYPE_");
345 return type_id;
348 public override string get_marshaller_type_name () {
349 return "OBJECT";
352 public override string get_get_value_function () {
353 return "g_value_get_object";
356 public override string get_set_value_function () {
357 return "g_value_set_object";
360 public override bool is_reference_counting () {
361 return true;
364 public override string get_ref_function () {
365 return "g_object_ref";
368 public override string get_unref_function () {
369 return "g_object_unref";
372 public override bool is_subtype_of (DataType! t) {
373 foreach (TypeReference base_type in base_types) {
374 if (base_type.data_type == t ||
375 base_type.data_type.is_subtype_of (t)) {
376 return true;
380 return false;
383 public override int get_type_parameter_index (string! name) {
384 int i = 0;
385 foreach (TypeParameter parameter in type_parameters) {
386 if (parameter.name == name) {
387 return i;
389 i++;
391 return -1;