1 /* valaunaryexpression.vala
3 * Copyright (C) 2006-2010 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
20 * Jürg Billeter <j@bitron.ch>
25 * Represents an expression with one operand in the source code.
27 * Supports +, -, !, ~, ref, out.
29 public class Vala
.UnaryExpression
: Expression
{
33 public UnaryOperator operator
{ get; set; }
38 public Expression inner
{
44 _inner
.parent_node
= this
;
48 private Expression _inner
;
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
) {
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
) {
80 private string get_operator_string () {
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 ";
90 default: assert_not_reached ();
94 public override string to_string () {
95 return get_operator_string () + _inner
.to_string ();
98 public override bool is_constant () {
99 if (operator
== UnaryOperator
.INCREMENT
|| operator
== UnaryOperator
.DECREMENT
) {
103 return inner
.is_constant ();
106 public override bool is_pure () {
107 if (operator
== UnaryOperator
.INCREMENT
|| operator
== UnaryOperator
.DECREMENT
) {
111 return inner
.is_pure ();
114 bool is_numeric_type (DataType type
) {
115 if (!(type
.data_type is Struct
)) {
119 var st
= (Struct
) type
.data_type
;
120 return st
.is_integer_type () || st
.is_floating_type ();
123 bool is_integer_type (DataType type
) {
124 if (!(type
.data_type is Struct
)) {
128 var st
= (Struct
) type
.data_type
;
129 return st
.is_integer_type ();
132 MemberAccess?
find_member_access (Expression expr
) {
133 if (expr is MemberAccess
) {
134 return (MemberAccess
) expr
;
140 public override bool check (CodeContext context
) {
147 if (operator
== UnaryOperator
.REF
|| operator
== UnaryOperator
.OUT
) {
149 inner
.target_type
= target_type
;
152 if (!inner
.check (context
)) {
153 /* if there was an error in the inner expression, skip type check */
158 if (inner
.value_type is FieldPrototype
) {
160 Report
.error (inner
.source_reference
, "Access to instance member `%s' denied".printf (inner
.symbol_reference
.get_full_name ()));
164 if (operator
== UnaryOperator
.PLUS
|| operator
== UnaryOperator
.MINUS
) {
165 // integer or floating point type
166 if (!is_numeric_type (inner
.value_type
)) {
168 Report
.error (source_reference
, "Operator not supported for `%s'".printf (inner
.value_type
.to_string ()));
172 value_type
= inner
.value_type
;
173 } else if (operator
== UnaryOperator
.LOGICAL_NEGATION
) {
175 if (!inner
.value_type
.compatible (context
.analyzer
.bool_type
)) {
177 Report
.error (source_reference
, "Operator not supported for `%s'".printf (inner
.value_type
.to_string ()));
181 value_type
= inner
.value_type
;
182 } else if (operator
== UnaryOperator
.BITWISE_COMPLEMENT
) {
184 if (!is_integer_type (inner
.value_type
) && !(inner
.value_type is EnumValueType
)) {
186 Report
.error (source_reference
, "Operator not supported for `%s'".printf (inner
.value_type
.to_string ()));
190 value_type
= inner
.value_type
;
191 } else if (operator
== UnaryOperator
.INCREMENT
||
192 operator
== UnaryOperator
.DECREMENT
) {
194 if (!is_integer_type (inner
.value_type
)) {
196 Report
.error (source_reference
, "Operator not supported for `%s'".printf (inner
.value_type
.to_string ()));
200 var ma
= find_member_access (inner
);
203 Report
.error (source_reference
, "Prefix operators not supported for this expression");
207 var old_value
= new
MemberAccess (ma
.inner
, ma
.member_name
, inner
.source_reference
);
208 var bin
= new
BinaryExpression (operator
== UnaryOperator
.INCREMENT ? BinaryOperator
.PLUS
: BinaryOperator
.MINUS
, old_value
, new
IntegerLiteral ("1"), source_reference
);
210 var assignment
= new
Assignment (ma
, bin
, AssignmentOperator
.SIMPLE
, source_reference
);
211 assignment
.target_type
= target_type
;
212 context
.analyzer
.replaced_nodes
.add (this
);
213 parent_node
.replace_expression (this
, assignment
);
214 assignment
.check (context
);
216 } else if (operator
== UnaryOperator
.REF
|| operator
== UnaryOperator
.OUT
) {
217 var ea
= inner as ElementAccess
;
218 if (inner
.symbol_reference is Field
|| inner
.symbol_reference is Parameter
|| inner
.symbol_reference is LocalVariable
||
219 (ea
!= null && ea
.container
.value_type is ArrayType
)) {
220 // ref and out can only be used with fields, parameters, local variables, and array element access
222 value_type
= inner
.value_type
;
225 Report
.error (source_reference
, "ref and out method arguments can only be used with fields, parameters, local variables, and array element access");
230 Report
.error (source_reference
, "internal error: unsupported unary operator");
237 public override void emit (CodeGenerator codegen
) {
238 inner
.emit (codegen
);
240 codegen
.visit_unary_expression (this
);
242 codegen
.visit_expression (this
);
245 public override void get_defined_variables (Collection
<LocalVariable
> collection
) {
246 inner
.get_defined_variables (collection
);
247 if (operator
== UnaryOperator
.OUT
|| operator
== UnaryOperator
.REF
) {
248 var local
= inner
.symbol_reference as LocalVariable
;
250 collection
.add (local
);
255 public override void get_used_variables (Collection
<LocalVariable
> collection
) {
256 if (operator
!= UnaryOperator
.OUT
) {
257 inner
.get_used_variables (collection
);
262 public enum Vala
.UnaryOperator
{