remove obsolete ref modifier and callback keyword
[vala-lang.git] / vala / valafield.vala
blob3ccd10c8a60970e4e7abd2b1e001401599cae899
1 /* valafield.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 type or namespace field.
28 public class Vala.Field : Member, Invokable, Lockable {
29 /**
30 * The symbol name of this field.
32 public string! name { get; set construct; }
34 /**
35 * The data type of this field.
37 public TypeReference! type_reference { get; set construct; }
39 /**
40 * Specifies the expression to be used to initialize this field.
42 public Expression initializer { get; set; }
44 /**
45 * Specifies the accessibility of this field. Public accessibility
46 * doesn't limit access. Default accessibility limits access to this
47 * program or library. Private accessibility limits access to instances
48 * of the contained type.
50 public MemberAccessibility access;
52 /**
53 * Specifies whether this field may only be accessed with an instance of
54 * the contained type.
56 public bool instance {
57 get {
58 return _instance;
60 set {
61 _instance = value;
65 /**
66 * Specifies whether an array length field should implicitly be created
67 * if the field type is an array.
69 public bool no_array_length { get; set; }
71 private string cname;
72 private bool _instance = true;
74 private bool lock_used = false;
76 /**
77 * Creates a new field.
79 * @param name field name
80 * @param type field type
81 * @param init initializer expression
82 * @param source reference to source code
83 * @return newly created field
85 public Field (string! _name, TypeReference! type, Expression init, SourceReference source) {
86 name = _name;
87 type_reference = type;
88 initializer = init;
89 source_reference = source;
92 public override void accept (CodeVisitor! visitor) {
93 visitor.visit_member (this);
95 visitor.visit_field (this);
98 public override void accept_children (CodeVisitor! visitor) {
99 type_reference.accept (visitor);
101 if (initializer != null) {
102 initializer.accept (visitor);
107 * Returns the name of this field as it is used in C code.
109 * @return the name to be used in C code
111 public string! get_cname () {
112 if (cname == null) {
113 if (!instance && symbol.parent_symbol.node is DataType) {
114 var t = (DataType) symbol.parent_symbol.node;
115 cname = "%s_%s".printf (t.get_lower_case_cname (null), name);
116 } else {
117 cname = name;
120 return cname;
123 private void set_cname (string! cname) {
124 this.cname = cname;
127 private void process_ccode_attribute (Attribute! a) {
128 if (a.has_argument ("cname")) {
129 set_cname (a.get_string ("cname"));
134 * Process all associated attributes.
136 public void process_attributes () {
137 foreach (Attribute a in attributes) {
138 if (a.name == "CCode") {
139 process_ccode_attribute (a);
140 } else if (a.name == "NoArrayLength") {
141 no_array_length = true;
146 public List<weak FormalParameter> get_parameters () {
147 if (!is_invokable ()) {
148 return null;
151 var cb = (Callback) type_reference.data_type;
152 return cb.get_parameters ();
155 public TypeReference get_return_type () {
156 if (!is_invokable ()) {
157 return null;
160 var cb = (Callback) type_reference.data_type;
161 return cb.return_type;
164 public bool is_invokable () {
165 return (type_reference.data_type is Callback);
168 public bool get_lock_used () {
169 return lock_used;
172 public void set_lock_used (bool used) {
173 lock_used = used;