valathrowstatement.vala 3.12 KB
Newer Older
1 2
/* valathrowstatement.vala
 *
3
 * Copyright (C) 2007-2009  Jürg Billeter
4 5 6 7
 *
 * 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
8
 * version 2.1 of the License, or (at your option) any later version.
9 10 11 12 13 14 15 16 17 18 19 20 21 22

 * 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>
 */

23
using Gee;
24 25 26 27

/**
 * Represents a throw statement in the source code.
 */
28
public class Vala.ThrowStatement : CodeNode, Statement {
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
	/**
	 * The error expression to throw.
	 */
	public Expression error_expression {
		get {
			return _error_expression;
		}
		set {
			_error_expression = value;
			if (_error_expression != null) {
				_error_expression.parent_node = this;
			}
		}
	}

44
	private Expression _error_expression;
45 46 47 48 49 50 51 52

	/**
	 * Creates a new throw statement.
	 *
	 * @param error_expression the error expression
	 * @param source_reference reference to source code
	 * @return                 newly created throw statement
	 */
53
	public ThrowStatement (Expression error_expression, SourceReference? source_reference = null) {
54 55
		this.source_reference = source_reference;
		this.error_expression = error_expression;
56
	}
57

58
	public override void accept (CodeVisitor visitor) {
59 60
		visitor.visit_throw_statement (this);
	}
61

62
	public override void accept_children (CodeVisitor visitor) {
63 64
		if (error_expression != null) {
			error_expression.accept (visitor);
65 66

			visitor.visit_end_full_expression (error_expression);
67 68 69
		}
	}

70
	public override void replace_expression (Expression old_node, Expression new_node) {
71
		if (error_expression == old_node) {
72
			error_expression = new_node;
73 74
		}
	}
75 76 77 78 79 80 81 82 83 84 85

	public override bool check (SemanticAnalyzer analyzer) {
		if (checked) {
			return !error;
		}

		checked = true;

		error_expression.target_type = new ErrorType (null, null, source_reference);
		error_expression.target_type.value_owned = true;

86
		if (error_expression != null) {
87 88 89 90
			if (!error_expression.check (analyzer)) {
				error = true;
				return false;
			}
91 92 93 94 95 96

			if (!(error_expression.value_type is ErrorType)) {
				Report.error (error_expression.source_reference, "`%s' is not an error type".printf (error_expression.value_type.to_string ()));
				error = true;
				return false;
			}
97
		}
98 99 100 101 102 103 104 105

		var error_type = error_expression.value_type.copy ();
		error_type.source_reference = source_reference;

		add_error_type (error_type);

		return !error;
	}
106 107 108 109 110 111 112 113

	public override void get_defined_variables (Collection<LocalVariable> collection) {
		error_expression.get_defined_variables (collection);
	}

	public override void get_used_variables (Collection<LocalVariable> collection) {
		error_expression.get_used_variables (collection);
	}
114
}