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