1/* valaexpressionstatement.vala
2 *
3 * Copyright (C) 2006-2010  Jürg Billeter
4 *
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.
9
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.
14
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
18 *
19 * Author:
20 * 	Jürg Billeter <j@bitron.ch>
21 */
22
23
24/**
25 * A code statement that evaluates a given expression. The value computed by the
26 * expression, if any, is discarded.
27 */
28public class Vala.ExpressionStatement : CodeNode, Statement {
29	/**
30	 * Specifies the expression to evaluate.
31	 */
32	public Expression expression {
33		get {
34			return _expression;
35		}
36		set {
37			_expression = value;
38			_expression.parent_node = this;
39		}
40	}
41
42	private Expression _expression;
43
44	/**
45	 * Creates a new expression statement.
46	 *
47	 * @param expression        expression to evaluate
48	 * @param source_reference  reference to source code
49	 * @return                  newly created expression statement
50	 */
51	public ExpressionStatement (Expression expression, SourceReference? source_reference = null) {
52		this.source_reference = source_reference;
53		this.expression = expression;
54	}
55
56	public override void accept (CodeVisitor visitor) {
57		visitor.visit_expression_statement (this);
58	}
59
60	public override void accept_children (CodeVisitor visitor) {
61		expression.accept (visitor);
62	}
63
64	public override void replace_expression (Expression old_node, Expression new_node) {
65		if (expression == old_node) {
66			expression = new_node;
67		}
68	}
69
70	public override bool check (CodeContext context) {
71		if (checked) {
72			return !error;
73		}
74
75		checked = true;
76
77		if (!expression.check (context)) {
78			// ignore inner error
79			error = true;
80			return false;
81		} else if (expression is Literal) {
82			Report.error (source_reference, "Literal expression not allowed as statement");
83			error = true;
84			return false;
85		}
86
87		return !error;
88	}
89
90	public override void get_error_types (Collection<DataType> collection, SourceReference? source_reference = null) {
91		expression.get_error_types (collection, source_reference);
92	}
93
94	public override void emit (CodeGenerator codegen) {
95		expression.emit (codegen);
96
97		codegen.visit_expression_statement (this);
98	}
99
100	public override void get_defined_variables (Collection<Variable> collection) {
101		expression.get_defined_variables (collection);
102	}
103
104	public override void get_used_variables (Collection<Variable> collection) {
105		expression.get_used_variables (collection);
106	}
107}
108