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
20 * Jürg Billeter <j@bitron.ch>
26 * Represents a type or namespace field.
28 public class Vala
.Field
: Member
, Invokable
, Lockable
{
30 * The symbol name of this field.
32 public string! name
{ get; set construct; }
35 * The data type of this field.
37 public TypeReference
! type_reference
{ get; set construct; }
40 * Specifies the expression to be used to initialize this field.
42 public Expression initializer
{ get; set; }
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
;
53 * Specifies whether this field may only be accessed with an instance of
56 public bool instance
{
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; }
72 private bool _instance
= true;
74 private bool lock_used
= false;
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
) {
87 type_reference
= type
;
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 () {
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
);
123 private void set_cname (string! 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 ()) {
151 var cb
= (Callback
) type_reference
.data_type
;
152 return cb
.get_parameters ();
155 public TypeReference
get_return_type () {
156 if (!is_invokable ()) {
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 () {
172 public void set_lock_used (bool used
) {