Line data Source code
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 : */
28 337125 : public class Vala.ExpressionStatement : CodeNode, Statement {
29 : /**
30 : * Specifies the expression to evaluate.
31 : */
32 : public Expression expression {
33 2776175 : get {
34 2776175 : return _expression;
35 : }
36 334068 : private set {
37 334068 : _expression = value;
38 334068 : _expression.parent_node = this;
39 : }
40 : }
41 :
42 334051 : 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 1002153 : public ExpressionStatement (Expression expression, SourceReference? source_reference = null) {
52 334051 : this.source_reference = source_reference;
53 334051 : this.expression = expression;
54 : }
55 :
56 415967 : public override void accept (CodeVisitor visitor) {
57 415967 : visitor.visit_expression_statement (this);
58 : }
59 :
60 415967 : public override void accept_children (CodeVisitor visitor) {
61 415967 : expression.accept (visitor);
62 : }
63 :
64 17 : public override void replace_expression (Expression old_node, Expression new_node) {
65 17 : if (expression == old_node) {
66 17 : expression = new_node;
67 : }
68 : }
69 :
70 302872 : public override bool check (CodeContext context) {
71 302872 : if (checked) {
72 0 : return !error;
73 : }
74 :
75 302872 : checked = true;
76 :
77 302872 : if (!expression.check (context)) {
78 : // ignore inner error
79 96 : error = true;
80 96 : return false;
81 302776 : } else if (expression is Literal) {
82 2 : Report.error (source_reference, "Literal expression not allowed as statement");
83 2 : error = true;
84 2 : return false;
85 : }
86 :
87 302774 : return !error;
88 : }
89 :
90 549912 : public override void get_error_types (Collection<DataType> collection, SourceReference? source_reference = null) {
91 549912 : expression.get_error_types (collection, source_reference);
92 : }
93 :
94 30642 : public override void emit (CodeGenerator codegen) {
95 30642 : expression.emit (codegen);
96 :
97 30642 : codegen.visit_expression_statement (this);
98 : }
99 :
100 645879 : public override void get_defined_variables (Collection<Variable> collection) {
101 645879 : expression.get_defined_variables (collection);
102 : }
103 :
104 215293 : public override void get_used_variables (Collection<Variable> collection) {
105 215293 : expression.get_used_variables (collection);
106 : }
107 : }
|