summaryrefslogtreecommitdiff
path: root/vala/valablock.vala
blob: e002909bf3c0e46bcc73d26914878e22ad6d4489 (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
/* valablock.vala
 *
 * Copyright (C) 2006-2010  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>
 */

using GLib;

/**
 * Represents a source code block.
 */
public class Vala.Block : Symbol, Statement {
	/**
	 * Specifies whether this block contains a jump statement. This
	 * information can be used to remove unreachable block cleanup code.
	 */
	public bool contains_jump_statement { get; set; }

	/**
	 * Specifies whether the end of this block is unreachable.
	 */
	public bool unreachable_exit { get; set; }

	public bool captured { get; set; }

	private List<Statement> statement_list = new ArrayList<Statement> ();
	private List<LocalVariable> local_variables = new ArrayList<LocalVariable> ();
	private List<Constant> local_constants = new ArrayList<Constant> ();

	/**
	 * Creates a new block.
	 *
	 * @param source_reference  reference to source code
	 */
	public Block (SourceReference? source_reference = null) {
		base (null, source_reference);
	}

	/**
	 * Append a statement to this block.
	 *
	 * @param stmt a statement
	 */
	public void add_statement (Statement stmt) {
		stmt.parent_node = this;
		statement_list.add (stmt);
	}

	public void insert_statement (int index, Statement stmt) {
		stmt.parent_node = this;
		statement_list.insert (index, stmt);
	}

	/**
	 * Returns a copy of the list of statements.
	 *
	 * @return statement list
	 */
	public List<Statement> get_statements () {
		var list = new ArrayList<Statement> ();
		foreach (Statement stmt in statement_list) {
			unowned StatementList? stmt_list = stmt as StatementList;
			if (stmt_list != null) {
				for (int i = 0; i < stmt_list.length; i++) {
					list.add (stmt_list.get (i));
				}
			} else {
				list.add (stmt);
			}
		}
		return list;
	}

	/**
	 * Add a local variable to this block.
	 *
	 * @param local a variable declarator
	 */
	public void add_local_variable (LocalVariable local) {
		unowned Symbol? parent_block = parent_symbol;
		while (parent_block is Block || parent_block is Method || parent_block is PropertyAccessor) {
			if (parent_block.scope.lookup (local.name) != null) {
				Report.error (local.source_reference, "Local variable `%s' conflicts with a local variable or constant declared in a parent scope", local.name);
				break;
			}
			parent_block = parent_block.parent_symbol;
		}
		local_variables.add (local);
	}

	public void remove_local_variable (LocalVariable local) {
		local_variables.remove (local);
	}

	/**
	 * Returns the list of local variables.
	 *
	 * @return variable declarator list
	 */
	public unowned List<LocalVariable> get_local_variables () {
		return local_variables;
	}

	public void add_local_constant (Constant constant) {
		unowned Symbol? parent_block = parent_symbol;
		while (parent_block is Block || parent_block is Method || parent_block is PropertyAccessor) {
			if (parent_block.scope.lookup (constant.name) != null) {
				Report.error (constant.source_reference, "Local constant `%s' conflicts with a local variable or constant declared in a parent scope", constant.name);
				break;
			}
			parent_block = parent_block.parent_symbol;
		}
		local_constants.add (constant);
		scope.add (constant.name, constant);
	}

	/**
	 * Returns the list of local constants.
	 *
	 * @return constants list
	 */
	public unowned List<Constant> get_local_constants () {
		return local_constants;
	}

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

	public override void accept_children (CodeVisitor visitor) {
		foreach (Statement stmt in statement_list) {
			stmt.accept (visitor);
		}
	}

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

		checked = true;

		owner = context.analyzer.current_symbol.scope;

		var old_symbol = context.analyzer.current_symbol;
		var old_insert_block = context.analyzer.insert_block;
		context.analyzer.current_symbol = this;
		context.analyzer.insert_block = this;

		for (int i = 0; i < statement_list.size; i++) {
			if (!statement_list[i].check (context)) {
				error = true;
			}
		}

		foreach (LocalVariable local in get_local_variables ()) {
			local.active = false;
		}

		foreach (Constant constant in local_constants) {
			constant.active = false;
		}

		context.analyzer.current_symbol = old_symbol;
		context.analyzer.insert_block = old_insert_block;

		return !error;
	}

	public override void get_error_types (Collection<DataType> collection, SourceReference? source_reference = null) {
		// use get_statements () instead of statement_list to not miss errors within StatementList objects
		foreach (Statement stmt in get_statements ()) {
			stmt.get_error_types (collection, source_reference);
		}
	}

	public override void emit (CodeGenerator codegen) {
		codegen.visit_block (this);
	}

	public void insert_before (Statement stmt, Statement new_stmt) {
		for (int i = 0; i < statement_list.size; i++) {
			var stmt_list = statement_list[i] as StatementList;
			if (stmt_list != null) {
				for (int j = 0; j < stmt_list.length; j++) {
					if (stmt_list.get (j) == stmt) {
						stmt_list.insert (j, new_stmt);
						new_stmt.parent_node = this;
						break;
					}
				}
			} else if (statement_list[i] == stmt) {
				stmt_list = new StatementList (source_reference);
				stmt_list.add (new_stmt);
				stmt_list.add (stmt);
				statement_list[i] = stmt_list;
				new_stmt.parent_node = this;
			}
		}
	}

	public void replace_statement (Statement old_stmt, Statement new_stmt) {
		for (int i = 0; i < statement_list.size; i++) {
			var stmt_list = statement_list[i] as StatementList;
			if (stmt_list != null) {
				for (int j = 0; j < stmt_list.length; j++) {
					if (stmt_list.get (j) == old_stmt) {
						stmt_list.set (j, new_stmt);
						new_stmt.parent_node = this;
						break;
					}
				}
			} else if (statement_list[i] == old_stmt) {
				statement_list[i] = new_stmt;
				new_stmt.parent_node = this;
				break;
			}
		}
	}
}