vapi: Update GIR-based bindings
[vala-gnome.git] / vala / valaswitchstatement.vala
blobc583f8db9d6b9f3cc8364f721c011f6bb189112d
1 /* valaswitchstatement.vala
3 * Copyright (C) 2006-2010 Jürg Billeter
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.
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.
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
19 * Author:
20 * Jürg Billeter <j@bitron.ch>
23 using GLib;
25 /**
26 * Represents a switch selection statement in the source code.
28 public class Vala.SwitchStatement : CodeNode, Statement {
29 /**
30 * Specifies the switch expression.
32 public Expression expression {
33 get {
34 return _expression;
36 set {
37 _expression = value;
38 _expression.parent_node = this;
42 private Expression _expression;
43 private List<SwitchSection> sections = new ArrayList<SwitchSection> ();
45 /**
46 * Creates a new switch statement.
48 * @param expression switch expression
49 * @param source_reference reference to source code
50 * @return newly created switch statement
52 public SwitchStatement (Expression expression, SourceReference? source_reference) {
53 this.source_reference = source_reference;
54 this.expression = expression;
57 /**
58 * Appends the specified section to the list of switch sections.
60 * @param section a switch section
62 public void add_section (SwitchSection section) {
63 section.parent_node = this;
64 sections.add (section);
67 /**
68 * Returns a copy of the list of switch sections.
70 * @return section list
72 public List<SwitchSection> get_sections () {
73 return sections;
76 public override void accept (CodeVisitor visitor) {
77 visitor.visit_switch_statement (this);
80 public override void accept_children (CodeVisitor visitor) {
81 expression.accept (visitor);
83 visitor.visit_end_full_expression (expression);
85 foreach (SwitchSection section in sections) {
86 section.accept (visitor);
90 public override void replace_expression (Expression old_node, Expression new_node) {
91 if (expression == old_node) {
92 expression = new_node;
96 public override bool check (CodeContext context) {
97 if (checked) {
98 return !error;
101 checked = true;
103 if (!expression.check (context)) {
104 error = true;
105 return false;
108 if (expression.value_type == null ||
109 (!(expression.value_type is IntegerType) &&
110 !(expression.value_type is EnumValueType) &&
111 !expression.value_type.compatible (context.analyzer.string_type))) {
112 Report.error (expression.source_reference, "Integer or string expression expected");
113 error = true;
114 return false;
117 // ensure that possibly owned (string) expression stays alive
118 expression.target_type = expression.value_type.copy ();
119 expression.target_type.nullable = false;
121 var labelset = new HashSet<string> (str_hash, str_equal);
122 foreach (SwitchSection section in sections) {
123 section.check (context);
125 // check for duplicate literal case labels
126 foreach (SwitchLabel label in section.get_labels ()) {
127 if (label.expression != null) {
128 string? value = null;
129 if (label.expression is StringLiteral) {
130 value = ((StringLiteral)label.expression).eval ();
131 } else if (label.expression is Literal) {
132 value = ((Literal)label.expression).to_string ();
133 } else if (label.expression.is_constant ()) {
134 value = label.expression.to_string ();
137 if (value != null && !labelset.add (value)) {
138 error = true;
139 Report.error (label.expression.source_reference, "Switch statement already contains this label");
143 add_error_types (section.get_error_types ());
146 return !error;
149 public override void emit (CodeGenerator codegen) {
150 expression.emit (codegen);
152 codegen.visit_end_full_expression (expression);
154 codegen.visit_switch_statement (this);