Add -H command-line option to generate C header file for public API, stub
[vala-lang.git] / ccode / valaccodedeclaration.vala
blob7a0223bab26d9c9b195b608e1ff1267ffc574b48
1 /* valaccodedeclaration.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 local variable declaration in the C code.
29 public class Vala.CCodeDeclaration : CCodeStatement {
30 /**
31 * The type of the local variable.
33 public string type_name { get; set; }
35 /**
36 * The declaration modifier.
38 public CCodeModifiers modifiers { get; set; }
40 private Gee.List<CCodeDeclarator> declarators = new ArrayList<CCodeDeclarator> ();
42 public CCodeDeclaration (string type_name) {
43 this.type_name = type_name;
46 /**
47 * Adds the specified declarator to this declaration.
49 * @param decl a declarator
51 public void add_declarator (CCodeDeclarator decl) {
52 declarators.add (decl);
55 public override void write (CCodeWriter writer) {
56 if ((modifiers & (CCodeModifiers.STATIC | CCodeModifiers.EXTERN)) != 0) {
57 // combined declaration and initialization for static and extern variables
58 writer.write_indent (line);
59 if ((modifiers & CCodeModifiers.STATIC) != 0) {
60 writer.write_string ("static ");
62 if ((modifiers & CCodeModifiers.VOLATILE) != 0) {
63 writer.write_string ("volatile ");
65 if ((modifiers & CCodeModifiers.EXTERN) != 0 && !has_initializer ()) {
66 writer.write_string ("extern ");
68 writer.write_string (type_name);
69 writer.write_string (" ");
71 bool first = true;
72 foreach (CCodeDeclarator decl in declarators) {
73 if (!first) {
74 writer.write_string (", ");
75 } else {
76 first = false;
78 decl.write (writer);
81 writer.write_string (";");
82 writer.write_newline ();
83 } else {
84 foreach (CCodeDeclarator decl in declarators) {
85 decl.write_initialization (writer);
90 private bool has_initializer () {
91 foreach (CCodeDeclarator decl in declarators) {
92 var var_decl = decl as CCodeVariableDeclarator;
93 if (var_decl != null && var_decl.initializer == null) {
94 return false;
97 return true;
100 public override void write_declaration (CCodeWriter writer) {
101 if ((modifiers & (CCodeModifiers.STATIC | CCodeModifiers.EXTERN)) != 0) {
102 // no separate declaration for static variables
103 return;
106 writer.write_indent ();
107 if ((modifiers & CCodeModifiers.REGISTER) == CCodeModifiers.REGISTER) {
108 writer.write_string ("register ");
110 writer.write_string (type_name);
111 writer.write_string (" ");
113 bool first = true;
114 foreach (CCodeDeclarator decl in declarators) {
115 if (!first) {
116 writer.write_string (", ");
117 } else {
118 first = false;
120 decl.write_declaration (writer);
123 writer.write_string (";");
124 writer.write_newline ();