6c9bbe4af27ac666c09acf58ebd335f50d15f8d0
[roobuilder] / src / ccode / valaccodeblock.vala
1 /* valaccodeblock.vala
2  *
3  * Copyright (C) 2006-2008  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 C code block.
27  */
28 public class Vala.CCodeBlock : CCodeStatement {
29         /**
30          * Specifies whether a newline at the end of the block should be
31          * suppressed.
32          */
33         public bool suppress_newline { get; set; }
34
35         private List<CCodeNode> statements = new ArrayList<CCodeNode> ();
36
37         /**
38          * Prepend the specified statement to the list of statements.
39          */
40         public void prepend_statement (CCodeNode statement) {
41                 statements.insert (0, statement);
42         }
43
44         /**
45          * Append the specified statement to the list of statements.
46          */
47         public void add_statement (CCodeNode statement) {
48                 /* allow generic nodes to include comments */
49                 statements.add (statement);
50         }
51
52         public override void write (CCodeWriter writer) {
53                 // the last reachable statement
54                 CCodeNode last_statement = null;
55
56                 writer.write_begin_block ();
57                 foreach (CCodeNode statement in statements) {
58                         statement.write_declaration (writer);
59
60                         // determine last reachable statement
61                         if (statement is CCodeLabel || statement is CCodeCaseStatement) {
62                                 last_statement = null;
63                         } else if (statement is CCodeReturnStatement || statement is CCodeGotoStatement
64                         || statement is CCodeContinueStatement || statement is CCodeBreakStatement) {
65                                 last_statement = statement;
66                         }
67                 }
68
69                 foreach (CCodeNode statement in statements) {
70                         statement.write (writer);
71
72                         // only output reachable code
73                         if (statement == last_statement) {
74                                 break;
75                         }
76                 }
77
78                 writer.write_end_block ();
79
80                 if (!suppress_newline) {
81                         writer.write_newline ();
82                 }
83         }
84 }