Add string.replace method, patch by Ali Sabil
[vala-lang.git] / vala / valaconstant.vala
blob555531e29b02ef68d93d3702d8a19dfe85673d58
1 /* valaconstant.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>
23 using GLib;
25 /**
26 * Represents a type member with a constant value.
28 public class Vala.Constant : Member, Lockable {
29 /**
30 * The data type of this constant.
32 public DataType type_reference {
33 get { return _data_type; }
34 set {
35 _data_type = value;
36 _data_type.parent_node = this;
40 /**
41 * The value of this constant.
43 public Expression? initializer {
44 get { return _initializer; }
45 set {
46 _initializer = value;
47 if (_initializer != null) {
48 _initializer.parent_node = this;
53 private string cname;
55 private bool lock_used = false;
57 private DataType _data_type;
59 private Expression _initializer;
61 /**
62 * Creates a new constant.
64 * @param name constant name
65 * @param type_reference constant type
66 * @param initializer constant value
67 * @param source_reference reference to source code
68 * @return newly created constant
70 public Constant (string name, DataType type_reference, Expression? initializer, SourceReference? source_reference) {
71 base (name, source_reference);
72 this.type_reference = type_reference;
73 this.initializer = initializer;
76 public override void accept (CodeVisitor visitor) {
77 visitor.visit_member (this);
79 visitor.visit_constant (this);
82 public override void accept_children (CodeVisitor visitor) {
83 type_reference.accept (visitor);
85 if (initializer != null) {
86 initializer.accept (visitor);
90 /**
91 * Returns the name of this constant as it is used in C code.
93 * @return the name to be used in C code
95 public string get_cname () {
96 if (cname == null) {
97 cname = get_default_cname ();
99 return cname;
103 * Returns the default name of this constant as it is used in C
104 * code.
106 * @return the name to be used in C code by default
108 public string get_default_cname () {
109 if (parent_symbol == null) {
110 // global constant
111 return name;
112 } else {
113 return "%s%s".printf (parent_symbol.get_lower_case_cprefix ().up (), name);
117 public bool get_lock_used () {
118 return lock_used;
121 public void set_lock_used (bool used) {
122 lock_used = used;
125 public override void replace_type (DataType old_type, DataType new_type) {
126 if (type_reference == old_type) {
127 type_reference = new_type;
131 private void process_ccode_attribute (Attribute a) {
132 if (a.has_argument ("cname")) {
133 cname = a.get_string ("cname");
135 if (a.has_argument ("cheader_filename")) {
136 var val = a.get_string ("cheader_filename");
137 foreach (string filename in val.split (",")) {
138 add_cheader_filename (filename);
144 * Process all associated attributes.
146 public void process_attributes () {
147 foreach (Attribute a in attributes) {
148 if (a.name == "CCode") {
149 process_ccode_attribute (a);
154 public override bool check (SemanticAnalyzer analyzer) {
155 if (checked) {
156 return !error;
159 checked = true;
161 process_attributes ();
163 type_reference.check (analyzer);
165 if (!external_package) {
166 if (initializer == null) {
167 error = true;
168 Report.error (source_reference, "A const field requires a initializer to be provided");
169 } else {
170 initializer.target_type = type_reference;
172 initializer.check (analyzer);
176 return !error;