Use strict non-null types with --enable-experimental-non-null
[vala-lang.git] / vala / valaunaryexpression.vala
blobb2066a373d101552655f828e50d3c035a77bc366
1 /* valaunaryexpression.vala
3 * Copyright (C) 2006-2009 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>
24 /**
25 * Represents an expression with one operand in the source code.
27 * Supports +, -, !, ~, ref, out.
29 public class Vala.UnaryExpression : Expression {
30 /**
31 * The unary operator.
33 public UnaryOperator operator { get; set; }
35 /**
36 * The operand.
38 public Expression inner {
39 get {
40 return _inner;
42 set {
43 _inner = value;
44 _inner.parent_node = this;
48 private Expression _inner;
50 /**
51 * Creates a new unary expression.
53 * @param op unary operator
54 * @param inner operand
55 * @param source reference to source code
56 * @return newly created binary expression
58 public UnaryExpression (UnaryOperator op, Expression _inner, SourceReference source) {
59 operator = op;
60 inner = _inner;
61 source_reference = source;
64 public override void accept (CodeVisitor visitor) {
65 visitor.visit_unary_expression (this);
67 visitor.visit_expression (this);
70 public override void accept_children (CodeVisitor visitor) {
71 inner.accept (visitor);
74 public override void replace_expression (Expression old_node, Expression new_node) {
75 if (inner == old_node) {
76 inner = new_node;
80 private string get_operator_string () {
81 switch (_operator) {
82 case UnaryOperator.PLUS: return "+";
83 case UnaryOperator.MINUS: return "-";
84 case UnaryOperator.LOGICAL_NEGATION: return "!";
85 case UnaryOperator.BITWISE_COMPLEMENT: return "~";
86 case UnaryOperator.INCREMENT: return "++";
87 case UnaryOperator.DECREMENT: return "--";
88 case UnaryOperator.REF: return "ref ";
89 case UnaryOperator.OUT: return "out ";
92 assert_not_reached ();
95 public override string to_string () {
96 return get_operator_string () + _inner.to_string ();
99 public override bool is_pure () {
100 if (operator == UnaryOperator.INCREMENT || operator == UnaryOperator.DECREMENT) {
101 return false;
104 return inner.is_pure ();
107 bool is_numeric_type (DataType type) {
108 if (!(type.data_type is Struct)) {
109 return false;
112 var st = (Struct) type.data_type;
113 return st.is_integer_type () || st.is_floating_type ();
116 bool is_integer_type (DataType type) {
117 if (!(type.data_type is Struct)) {
118 return false;
121 var st = (Struct) type.data_type;
122 return st.is_integer_type ();
125 MemberAccess? find_member_access (Expression expr) {
126 if (expr is MemberAccess) {
127 return (MemberAccess) expr;
130 return null;
133 public override bool check (SemanticAnalyzer analyzer) {
134 if (checked) {
135 return !error;
138 checked = true;
140 if (operator == UnaryOperator.REF || operator == UnaryOperator.OUT) {
141 inner.lvalue = true;
142 inner.target_type = target_type;
145 if (!inner.check (analyzer)) {
146 /* if there was an error in the inner expression, skip type check */
147 error = true;
148 return false;
151 if (inner.value_type is FieldPrototype) {
152 error = true;
153 Report.error (inner.source_reference, "Access to instance member `%s' denied".printf (inner.symbol_reference.get_full_name ()));
154 return false;
157 if (operator == UnaryOperator.PLUS || operator == UnaryOperator.MINUS) {
158 // integer or floating point type
159 if (!is_numeric_type (inner.value_type)) {
160 error = true;
161 Report.error (source_reference, "Operator not supported for `%s'".printf (inner.value_type.to_string ()));
162 return false;
165 value_type = inner.value_type;
166 } else if (operator == UnaryOperator.LOGICAL_NEGATION) {
167 // boolean type
168 if (!inner.value_type.compatible (analyzer.bool_type)) {
169 error = true;
170 Report.error (source_reference, "Operator not supported for `%s'".printf (inner.value_type.to_string ()));
171 return false;
174 value_type = inner.value_type;
175 } else if (operator == UnaryOperator.BITWISE_COMPLEMENT) {
176 // integer type
177 if (!is_integer_type (inner.value_type)) {
178 error = true;
179 Report.error (source_reference, "Operator not supported for `%s'".printf (inner.value_type.to_string ()));
180 return false;
183 value_type = inner.value_type;
184 } else if (operator == UnaryOperator.INCREMENT ||
185 operator == UnaryOperator.DECREMENT) {
186 // integer type
187 if (!is_integer_type (inner.value_type)) {
188 error = true;
189 Report.error (source_reference, "Operator not supported for `%s'".printf (inner.value_type.to_string ()));
190 return false;
193 var ma = find_member_access (inner);
194 if (ma == null) {
195 error = true;
196 Report.error (source_reference, "Prefix operators not supported for this expression");
197 return false;
200 var old_value = new MemberAccess (ma.inner, ma.member_name, inner.source_reference);
201 var bin = new BinaryExpression (operator == UnaryOperator.INCREMENT ? BinaryOperator.PLUS : BinaryOperator.MINUS, old_value, new IntegerLiteral ("1"), source_reference);
203 var assignment = new Assignment (ma, bin, AssignmentOperator.SIMPLE, source_reference);
204 assignment.target_type = target_type;
205 analyzer.replaced_nodes.add (this);
206 parent_node.replace_expression (this, assignment);
207 assignment.check (analyzer);
208 return true;
209 } else if (operator == UnaryOperator.REF || operator == UnaryOperator.OUT) {
210 if (inner.symbol_reference is Field || inner.symbol_reference is FormalParameter || inner.symbol_reference is LocalVariable) {
211 // ref and out can only be used with fields, parameters, and local variables
212 lvalue = true;
213 value_type = inner.value_type;
214 } else {
215 error = true;
216 Report.error (source_reference, "ref and out method arguments can only be used with fields, parameters, and local variables");
217 return false;
219 } else {
220 error = true;
221 Report.error (source_reference, "internal error: unsupported unary operator");
222 return false;
225 return !error;
228 public override void get_defined_variables (Collection<LocalVariable> collection) {
229 inner.get_defined_variables (collection);
230 if (operator == UnaryOperator.OUT || operator == UnaryOperator.REF) {
231 var local = inner.symbol_reference as LocalVariable;
232 if (local != null) {
233 collection.add (local);
238 public override void get_used_variables (Collection<LocalVariable> collection) {
239 if (operator != UnaryOperator.OUT) {
240 inner.get_used_variables (collection);
245 public enum Vala.UnaryOperator {
246 NONE,
247 PLUS,
248 MINUS,
249 LOGICAL_NEGATION,
250 BITWISE_COMPLEMENT,
251 INCREMENT,
252 DECREMENT,
253 REF,