1 /* valaccodedeclaration.vala
3 * Copyright (C) 2006-2010 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
20 * Jürg Billeter <j@bitron.ch>
26 * Represents a local variable declaration in the C code.
28 public class Vala.CCodeDeclaration : CCodeStatement {
30 * The type of the local variable.
32 public string type_name { get; set; }
34 private List<CCodeDeclarator> declarators = new ArrayList<CCodeDeclarator> ();
36 public CCodeDeclaration (string type_name) {
37 this.type_name = type_name;
41 * Adds the specified declarator to this declaration.
43 * @param decl a declarator
45 public void add_declarator (CCodeDeclarator decl) {
46 declarators.add (decl);
50 * Returns the list of declarators.
52 * @return declarators list
54 public unowned List<CCodeDeclarator> get_declarators () {
58 public override void write (CCodeWriter writer) {
59 if ((modifiers & (CCodeModifiers.STATIC | CCodeModifiers.INTERNAL | CCodeModifiers.EXTERN)) == 0) {
60 foreach (CCodeDeclarator decl in declarators) {
61 decl.write_initialization (writer);
66 private bool has_initializer () {
67 foreach (CCodeDeclarator decl in declarators) {
68 var var_decl = decl as CCodeVariableDeclarator;
69 if (var_decl != null && var_decl.initializer == null) {
76 public override void write_declaration (CCodeWriter writer) {
77 if ((modifiers & (CCodeModifiers.STATIC | CCodeModifiers.INTERNAL | CCodeModifiers.EXTERN)) != 0) {
78 // combined declaration and initialization for static and extern variables
79 writer.write_indent (line);
80 if ((modifiers & CCodeModifiers.INTERNAL) != 0) {
81 writer.write_string (GNUC_INTERNAL);
83 if ((modifiers & CCodeModifiers.STATIC) != 0) {
84 writer.write_string ("static ");
86 if ((modifiers & CCodeModifiers.VOLATILE) != 0) {
87 writer.write_string ("volatile ");
89 if ((modifiers & CCodeModifiers.EXTERN) != 0 && !has_initializer ()) {
90 writer.write_string ("VALA_EXTERN ");
92 if ((modifiers & CCodeModifiers.THREAD_LOCAL) != 0) {
93 writer.write_string ("thread_local ");
95 writer.write_string (type_name);
96 writer.write_string (" ");
99 foreach (CCodeDeclarator decl in declarators) {
101 writer.write_string (", ");
108 writer.write_string (";");
109 writer.write_newline ();
113 writer.write_indent ();
114 if ((modifiers & CCodeModifiers.REGISTER) == CCodeModifiers.REGISTER) {
115 writer.write_string ("register ");
117 if ((modifiers & CCodeModifiers.VOLATILE) != 0) {
118 writer.write_string ("volatile ");
120 writer.write_string (type_name);
121 writer.write_string (" ");
124 foreach (CCodeDeclarator decl in declarators) {
126 writer.write_string (", ");
130 decl.write_declaration (writer);
133 if (CCodeModifiers.DEPRECATED in modifiers) {
134 writer.write_string (GNUC_DEPRECATED);
137 writer.write_string (";");
138 writer.write_newline ();