summaryrefslogtreecommitdiff
path: root/vala/valaunaryexpression.vala
blob: b45d7da834abc3de560a56a53a31b2dc30165772 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/* valaunaryexpression.vala
 *
 * Copyright (C) 2006-2011  Jürg Billeter
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.

 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 *
 * Author:
 * 	Jürg Billeter <j@bitron.ch>
 */


/**
 * Represents an expression with one operand in the source code.
 *
 * Supports +, -, !, ~, ref, out.
 */
public class Vala.UnaryExpression : Expression {
	/**
	 * The unary operator.
	 */
	public UnaryOperator operator { get; private set; }

	/**
	 * The operand.
	 */
	public Expression inner {
		get {
			return _inner;
		}
		private set {
			_inner = value;
			_inner.parent_node = this;
		}
	}

	private Expression _inner;

	/**
	 * Creates a new unary expression.
	 *
	 * @param op     unary operator
	 * @param _inner operand
	 * @param source reference to source code
	 * @return       newly created binary expression
	 */
	public UnaryExpression (UnaryOperator op, Expression _inner, SourceReference? source = null) {
		operator = op;
		inner = _inner;
		source_reference = source;
	}

	public override void accept (CodeVisitor visitor) {
		visitor.visit_unary_expression (this);

		visitor.visit_expression (this);
	}

	public override void accept_children (CodeVisitor visitor) {
		inner.accept (visitor);
	}

	public override void replace_expression (Expression old_node, Expression new_node) {
		if (inner == old_node) {
			inner = new_node;
		}
	}

	public override string to_string () {
		return operator.to_string () + _inner.to_string ();
	}

	public override bool is_constant () {
		if (operator == UnaryOperator.INCREMENT || operator == UnaryOperator.DECREMENT) {
			return false;
		}

		if (operator == UnaryOperator.REF || operator == UnaryOperator.OUT) {
			unowned Field? field = inner.symbol_reference as Field;
			if (field != null && field.binding == MemberBinding.STATIC) {
				return true;
			} else {
				return false;
			}
		}

		return inner.is_constant ();
	}

	public override bool is_pure () {
		if (operator == UnaryOperator.INCREMENT || operator == UnaryOperator.DECREMENT) {
			return false;
		}

		return inner.is_pure ();
	}

	public override bool is_accessible (Symbol sym) {
		return inner.is_accessible (sym);
	}

	bool is_numeric_type (DataType type) {
		unowned Struct? st = type.type_symbol as Struct;
		if (type.nullable || st == null) {
			return false;
		}

		return st.is_integer_type () || st.is_floating_type ();
	}

	bool is_integer_type (DataType type) {
		unowned Struct? st = type.type_symbol as Struct;
		if (type.nullable || st == null) {
			return false;
		}

		return st.is_integer_type ();
	}

	public override void get_error_types (Collection<DataType> collection, SourceReference? source_reference = null) {
		inner.get_error_types (collection, source_reference);
	}

	public override bool check (CodeContext context) {
		if (checked) {
			return !error;
		}

		checked = true;

		if (operator == UnaryOperator.REF || operator == UnaryOperator.OUT) {
			inner.lvalue = true;
			inner.target_type = target_type;
		} else if (operator == UnaryOperator.INCREMENT || operator == UnaryOperator.DECREMENT) {
			inner.lvalue = true;
		}

		if (!inner.check (context)) {
			/* if there was an error in the inner expression, skip type check */
			error = true;
			return false;
		} else if (inner.value_type == null) {
			error = true;
			Report.error (inner.source_reference, "Invalid inner operand");
			return false;
		}

		if (inner.value_type is FieldPrototype || inner.value_type is PropertyPrototype) {
			error = true;
			Report.error (inner.source_reference, "Access to instance member `%s' denied", inner.symbol_reference.get_full_name ());
			return false;
		}

		switch (operator) {
		case UnaryOperator.PLUS:
		case UnaryOperator.MINUS:
			// integer or floating point type
			if (!is_numeric_type (inner.value_type)) {
				error = true;
				Report.error (source_reference, "Operator not supported for `%s'", inner.value_type.to_string ());
				return false;
			}

			value_type = inner.value_type;
			break;
		case UnaryOperator.LOGICAL_NEGATION:
			// boolean type
			if (inner.value_type.nullable || !inner.value_type.compatible (context.analyzer.bool_type)) {
				error = true;
				Report.error (source_reference, "Operator not supported for `%s'", inner.value_type.to_string ());
				return false;
			}

			value_type = inner.value_type;
			break;
		case UnaryOperator.BITWISE_COMPLEMENT:
			// integer type
			if (!is_integer_type (inner.value_type) && !(inner.value_type is EnumValueType)) {
				error = true;
				Report.error (source_reference, "Operator not supported for `%s'", inner.value_type.to_string ());
				return false;
			}

			value_type = inner.value_type;
			break;
		case UnaryOperator.INCREMENT:
		case UnaryOperator.DECREMENT:
			// integer type
			if (!is_integer_type (inner.value_type)) {
				error = true;
				Report.error (source_reference, "Operator not supported for `%s'", inner.value_type.to_string ());
				return false;
			}

			if (!(inner is MemberAccess)) {
				error = true;
				Report.error (source_reference, "Prefix operators not supported for this expression");
				return false;
			}

			value_type = inner.value_type;
			break;
		case UnaryOperator.REF:
		case UnaryOperator.OUT:
			unowned ElementAccess? ea = inner as ElementAccess;
			if (inner.symbol_reference is Field || inner.symbol_reference is Parameter || inner.symbol_reference is LocalVariable ||
			    (ea != null && ea.container.value_type is ArrayType)) {
				// ref and out can only be used with fields, parameters, local variables, and array element access
				lvalue = true;
				// `ref foo` or `out foo` is used as synonym for `&foo`
				if (parent_node is InitializerList || parent_node is MemberInitializer) {
					value_type = new PointerType (inner.value_type, inner.source_reference);
				} else {
					value_type = inner.value_type;
				}
			} else {
				error = true;
				Report.error (source_reference, "ref and out method arguments can only be used with fields, parameters, local variables, and array element access");
				return false;
			}
			if (inner.symbol_reference != null && inner.symbol_reference.get_attribute ("GtkChild") != null) {
				error = true;
				Report.error (source_reference, "Assignment of [GtkChild] `%s' is not allowed", inner.symbol_reference.get_full_name ());
				return false;
			}
			break;
		default:
			error = true;
			Report.error (source_reference, "internal error: unsupported unary operator");
			return false;
		}

		value_type.check (context);

		return !error;
	}

	public override void emit (CodeGenerator codegen) {
		inner.emit (codegen);

		codegen.visit_unary_expression (this);

		codegen.visit_expression (this);
	}

	public override void get_defined_variables (Collection<Variable> collection) {
		inner.get_defined_variables (collection);
		if (operator == UnaryOperator.OUT || operator == UnaryOperator.REF) {
			unowned LocalVariable? local = inner.symbol_reference as LocalVariable;
			unowned Parameter? param = inner.symbol_reference as Parameter;
			if (local != null) {
				collection.add (local);
			}
			if (param != null && param.direction == ParameterDirection.OUT) {
				collection.add (param);
			}
		}
	}

	public override void get_used_variables (Collection<Variable> collection) {
		if (operator != UnaryOperator.OUT) {
			inner.get_used_variables (collection);
		}
	}
}

public enum Vala.UnaryOperator {
	NONE,
	PLUS,
	MINUS,
	LOGICAL_NEGATION,
	BITWISE_COMPLEMENT,
	INCREMENT,
	DECREMENT,
	REF,
	OUT;

	public unowned string to_string () {
		switch (this) {
		case PLUS: return "+";
		case MINUS: return "-";
		case LOGICAL_NEGATION: return "!";
		case BITWISE_COMPLEMENT: return "~";
		case INCREMENT: return "++";
		case DECREMENT: return "--";
		case REF: return "ref ";
		case OUT: return "out ";
		default: assert_not_reached ();
		}
	}

}