Warn when using result variable with incompatible type to prepare possible
[vala-lang.git] / vala / valaexpressionstatement.vala
blob69cf0bd85dde90ed5044112ed5e1ef1c34455bbe
1 /* valaexpressionstatement.vala
3 * Copyright (C) 2006-2008 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 Gee;
25 /**
26 * A code statement that evaluates a given expression. The value computed by the
27 * expression, if any, is discarded.
29 public class Vala.ExpressionStatement : CodeNode, Statement {
30 /**
31 * Specifies the expression to evaluate.
33 public Expression expression {
34 get {
35 return _expression;
37 set {
38 _expression = value;
39 _expression.parent_node = this;
43 private Expression _expression;
45 /**
46 * Creates a new expression statement.
48 * @param expr expression to evaluate
49 * @param source reference to source code
50 * @return newly created expression statement
52 public ExpressionStatement (Expression expression, SourceReference? source_reference = null) {
53 this.source_reference = source_reference;
54 this.expression = expression;
57 public override void accept (CodeVisitor visitor) {
58 visitor.visit_expression_statement (this);
61 public override void accept_children (CodeVisitor visitor) {
62 expression.accept (visitor);
65 public override void replace_expression (Expression old_node, Expression new_node) {
66 if (expression == old_node) {
67 expression = new_node;
71 /**
72 * Returns the property this statement sets, if any.
74 * @return the property this statement sets, or null if it doesn't set
75 * a property
77 public Property? assigned_property () {
78 if (expression is Assignment) {
79 var assign = (Assignment) expression;
80 if (assign.left is MemberAccess) {
81 var ma = (MemberAccess) assign.left;
82 if (ma.symbol_reference is Property) {
83 return (Property) ma.symbol_reference;
88 return null;
91 public override bool check (SemanticAnalyzer analyzer) {
92 if (checked) {
93 return !error;
96 checked = true;
98 if (!expression.check (analyzer)) {
99 // ignore inner error
100 error = true;
101 return false;
104 add_error_types (expression.get_error_types ());
106 return !error;
109 public override void get_defined_variables (Collection<LocalVariable> collection) {
110 expression.get_defined_variables (collection);
113 public override void get_used_variables (Collection<LocalVariable> collection) {
114 expression.get_used_variables (collection);