Line data Source code
1 : /* valaswitchsection.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 : using GLib;
24 :
25 : /**
26 : * Represents a switch section in the source code.
27 : */
28 1186 : public class Vala.SwitchSection : Block {
29 1154 : private List<SwitchLabel> labels = new ArrayList<SwitchLabel> ();
30 :
31 : /**
32 : * Creates a new switch section.
33 : *
34 : * @param source_reference reference to source code
35 : * @return newly created switch section
36 : */
37 577 : public SwitchSection (SourceReference? source_reference = null) {
38 577 : base (source_reference);
39 : }
40 :
41 : /**
42 : * Appends the specified label to the list of switch labels.
43 : *
44 : * @param label a switch label
45 : */
46 669 : public void add_label (SwitchLabel label) {
47 669 : if (labels.size == 0) {
48 576 : this.source_reference = label.source_reference;
49 : }
50 :
51 669 : labels.add (label);
52 669 : label.parent_node = this;
53 : }
54 :
55 : /**
56 : * Returns the list of switch labels.
57 : *
58 : * @return switch label list
59 : */
60 1672 : public unowned List<SwitchLabel> get_labels () {
61 1672 : return labels;
62 : }
63 :
64 1280 : public bool has_default_label () {
65 3892 : foreach (SwitchLabel label in labels) {
66 1470 : if (label.expression == null) {
67 164 : return true;
68 : }
69 : }
70 :
71 1280 : return false;
72 : }
73 :
74 579 : public override void accept (CodeVisitor visitor) {
75 579 : visitor.visit_switch_section (this);
76 : }
77 :
78 579 : public override void accept_children (CodeVisitor visitor) {
79 1923 : foreach (SwitchLabel label in labels) {
80 672 : label.accept (visitor);
81 : }
82 :
83 579 : base.accept_children (visitor);
84 : }
85 :
86 571 : public override bool check (CodeContext context) {
87 571 : if (checked) {
88 0 : return !error;
89 : }
90 :
91 571 : if (!(parent_node is SwitchStatement)) {
92 1 : Report.error (source_reference, "no enclosing switch statement found");
93 1 : error = true;
94 1 : return false;
95 : }
96 :
97 1896 : foreach (SwitchLabel label in get_labels ()) {
98 663 : label.check (context);
99 : }
100 :
101 570 : if (!base.check (context)) {
102 1 : error = true;
103 : }
104 :
105 570 : checked = true;
106 :
107 570 : return !error;
108 : }
109 :
110 561 : public override void emit (CodeGenerator codegen) {
111 1869 : foreach (SwitchLabel label in labels) {
112 654 : label.emit (codegen);
113 : }
114 :
115 561 : base.emit (codegen);
116 : }
117 : }
|