valaswitchlabel.vala 3.15 KB
Newer Older
1 2
/* valaswitchlabel.vala
 *
3
 * Copyright (C) 2006-2010  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 23 24 25 26 27 28 29 30 31 32 33

 * 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 switch label in the source code.
 */
public class Vala.SwitchLabel : CodeNode {
	/**
	 * Specifies the label expression.
	 */
	public Expression expression { get; set; }

34 35
	public weak SwitchSection section { get; set; }

36 37 38 39 40 41 42
	/**
	 * Creates a new switch case label.
	 *
	 * @param expr   label expression
	 * @param source reference to source code
	 * @return       newly created switch case label
	 */
43
	public SwitchLabel (Expression expr, SourceReference? source = null) {
44 45
		expression = expr;
		source_reference = source;
46 47 48 49 50 51 52 53
	}

	/**
	 * Creates a new switch default label.
	 *
	 * @param source reference to source code
	 * @return       newly created switch default label
	 */
54
	public SwitchLabel.with_default (SourceReference? source = null) {
55
		source_reference = source;
56
	}
57

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

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

66 67 68
			visitor.visit_end_full_expression (expression);
		}
	}
69

70
	public override bool check (CodeContext context) {
71
		if (expression != null) {
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
			var switch_statement = (SwitchStatement) section.parent_node;

			// enum-type inference
			var condition_target_type = switch_statement.expression.target_type;
			if (expression.symbol_reference == null && condition_target_type != null && condition_target_type.data_type is Enum) {
				var enum_type = (Enum) condition_target_type.data_type;
				foreach (var val in enum_type.get_values ()) {
					if (expression.to_string () == val.name) {
						expression.target_type = condition_target_type.copy ();
						expression.symbol_reference = val;
						break;
					}
				}
			}

87
			expression.check (context);
88 89 90 91 92 93 94 95 96 97 98

			if (!expression.is_constant ()) {
				error = true;
				Report.error (expression.source_reference, "Expression must be constant");
				return false;
			}
			if (!expression.value_type.compatible (switch_statement.expression.value_type)) {
				error = true;
				Report.error (expression.source_reference, "Cannot convert from `%s' to `%s'".printf (expression.value_type.to_string (), switch_statement.expression.value_type.to_string ()));
				return false;
			}
99 100 101 102
		}

		return true;
	}
103 104 105 106

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