1/* valadeclarationstatement.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 * Represents a local variable or constant declaration statement in the source code.
26 */
27public class Vala.DeclarationStatement : CodeNode, Statement {
28	/**
29	 * The local variable or constant declaration.
30	 */
31	public Symbol declaration {
32		get {
33			return _declaration;
34		}
35		set {
36			_declaration = value;
37			if (_declaration != null) {
38				_declaration.parent_node = this;
39			}
40		}
41	}
42
43	Symbol _declaration;
44
45	/**
46	 * Creates a new declaration statement.
47	 *
48	 * @param declaration       local variable declaration
49	 * @param source_reference  reference to source code
50	 * @return                  newly created declaration statement
51	 */
52	public DeclarationStatement (Symbol declaration, SourceReference? source_reference = null) {
53		this.declaration = declaration;
54		this.source_reference = source_reference;
55	}
56
57	public override void accept (CodeVisitor visitor) {
58		visitor.visit_declaration_statement (this);
59	}
60
61	public override void accept_children (CodeVisitor visitor) {
62		declaration.accept (visitor);
63	}
64
65	public override void get_error_types (Collection<DataType> collection, SourceReference? source_reference = null) {
66		if (source_reference == null) {
67			source_reference = this.source_reference;
68		}
69		unowned LocalVariable? local = declaration as LocalVariable;
70		if (local != null && local.initializer != null) {
71			local.initializer.get_error_types (collection, source_reference);
72		}
73	}
74
75	public override bool check (CodeContext context) {
76		if (checked) {
77			return !error;
78		}
79
80		checked = true;
81
82		if (!declaration.check (context)) {
83			// ignore inner error
84			error = true;
85			return false;
86		}
87
88		return !error;
89	}
90
91	public override void emit (CodeGenerator codegen) {
92		codegen.visit_declaration_statement (this);
93	}
94
95	public override void get_defined_variables (Collection<Variable> collection) {
96		unowned LocalVariable? local = declaration as LocalVariable;
97		if (local != null) {
98			unowned ArrayType? array_type = local.variable_type as ArrayType;
99			if (local.initializer != null) {
100				local.initializer.get_defined_variables (collection);
101				collection.add (local);
102			} else if (array_type != null && array_type.fixed_length) {
103				collection.add (local);
104			}
105		}
106	}
107
108	public override void get_used_variables (Collection<Variable> collection) {
109		unowned LocalVariable? local = declaration as LocalVariable;
110		if (local != null && local.initializer != null) {
111			local.initializer.get_used_variables (collection);
112		}
113	}
114}
115