Add -H command-line option to generate C header file for public API, stub
[vala-lang.git] / ccode / valaccodeblock.vala
blobcff8613fe1b764654b396d30ee2dff2388092871
1 /* valaccodeblock.vala
3 * Copyright (C) 2006-2008 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;
24 using Gee;
26 /**
27 * Represents a C code block.
29 public class Vala.CCodeBlock : CCodeStatement {
30 /**
31 * Specifies whether a newline at the end of the block should be
32 * suppressed.
34 public bool suppress_newline { get; set; }
36 private Gee.List<CCodeNode> statements = new ArrayList<CCodeNode> ();
38 /**
39 * Prepend the specified statement to the list of statements.
41 public void prepend_statement (CCodeNode statement) {
42 statements.insert (0, statement);
45 /**
46 * Append the specified statement to the list of statements.
48 public void add_statement (CCodeNode statement) {
49 /* allow generic nodes to include comments */
50 statements.add (statement);
53 public override void write (CCodeWriter writer) {
54 // the last reachable statement
55 CCodeNode last_statement = null;
57 writer.write_begin_block ();
58 foreach (CCodeNode statement in statements) {
59 statement.write_declaration (writer);
61 // determine last reachable statement
62 if (statement is CCodeLabel || statement is CCodeCaseStatement) {
63 last_statement = null;
64 } else if (statement is CCodeReturnStatement || statement is CCodeGotoStatement
65 || statement is CCodeContinueStatement || statement is CCodeBreakStatement) {
66 last_statement = statement;
70 foreach (CCodeNode statement in statements) {
71 statement.write (writer);
73 // only output reachable code
74 if (statement == last_statement) {
75 break;
79 writer.write_end_block ();
81 if (!suppress_newline) {
82 writer.write_newline ();