gtk+-4.0: Update to 3.90.0
[vala-gnome.git] / vala / valaconstant.vala
blob53b84c60e0eb91807dac1943915b4216660e7dda
1 /* valaconstant.vala
3 * Copyright (C) 2006-2011 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 : Symbol, 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? value {
44 get { return _value; }
45 set {
46 _value = value;
47 if (_value != null) {
48 _value.parent_node = this;
53 private bool lock_used = false;
55 private DataType _data_type;
57 private Expression _value;
59 /**
60 * Creates a new constant.
62 * @param name constant name
63 * @param type_reference constant type
64 * @param value constant value
65 * @param source_reference reference to source code
66 * @return newly created constant
68 public Constant (string name, DataType? type_reference, Expression? value, SourceReference? source_reference, Comment? comment = null) {
69 base (name, source_reference, comment);
70 if (type_reference != null) {
71 this.type_reference = type_reference;
73 this.value = value;
76 public override void accept (CodeVisitor visitor) {
77 visitor.visit_constant (this);
80 public override void accept_children (CodeVisitor visitor) {
81 type_reference.accept (visitor);
83 if (value != null) {
84 value.accept (visitor);
88 public bool get_lock_used () {
89 return lock_used;
92 public void set_lock_used (bool used) {
93 lock_used = used;
96 public override void replace_expression (Expression old_node, Expression new_node) {
97 if (value == old_node) {
98 value = new_node;
102 public override void replace_type (DataType old_type, DataType new_type) {
103 if (type_reference == old_type) {
104 type_reference = new_type;
108 public override bool check (CodeContext context) {
109 if (checked) {
110 return !error;
113 checked = true;
115 var old_source_file = context.analyzer.current_source_file;
116 var old_symbol = context.analyzer.current_symbol;
118 if (source_reference != null) {
119 context.analyzer.current_source_file = source_reference.file;
121 if (!(parent_symbol is Block)) {
122 // non-local constant
123 context.analyzer.current_symbol = this;
126 type_reference.check (context);
128 if (!check_const_type (type_reference, context)) {
129 error = true;
130 Report.error (source_reference, "`%s' not supported as type for constants".printf (type_reference.to_string ()));
131 return false;
134 if (!external) {
135 if (value == null) {
136 error = true;
137 Report.error (source_reference, "A const field requires a value to be provided");
138 } else {
139 value.target_type = type_reference;
141 if (!value.check (context)) {
142 error = true;
143 return false;
146 if (!value.value_type.compatible (type_reference)) {
147 error = true;
148 Report.error (source_reference, "Cannot convert from `%s' to `%s'".printf (value.value_type.to_string (), type_reference.to_string ()));
149 return false;
152 // support translated string constants for efficiency / convenience
153 // even though the expression is not a compile-time constant
154 var call = value as MethodCall;
155 if (call != null) {
156 var method_type = call.call.value_type as MethodType;
157 if (method_type != null && method_type.method_symbol.get_full_name () == "GLib._") {
158 // first argument is string
159 var literal = call.get_argument_list ().get (0) as StringLiteral;
160 if (literal != null) {
161 value = literal;
162 literal.translate = true;
167 if (!value.is_constant ()) {
168 error = true;
169 Report.error (value.source_reference, "Value must be constant");
170 return false;
173 } else {
174 if (value != null) {
175 error = true;
176 Report.error (source_reference, "External constants cannot use values");
180 if (!external_package && !hides && get_hidden_member () != null) {
181 Report.warning (source_reference, "%s hides inherited constant `%s'. Use the `new' keyword if hiding was intentional".printf (get_full_name (), get_hidden_member ().get_full_name ()));
184 context.analyzer.current_source_file = old_source_file;
185 context.analyzer.current_symbol = old_symbol;
187 active = true;
189 return !error;
192 bool check_const_type (DataType type, CodeContext context) {
193 if (type is ValueType) {
194 return true;
195 } else if (type is ArrayType) {
196 var array_type = type as ArrayType;
197 return check_const_type (array_type.element_type, context);
198 } else if (type.data_type == context.analyzer.string_type.data_type) {
199 return true;
200 } else {
201 return false;