Bump version suffix
[vala-lang.git] / ccode / valaccodedeclaration.vala
blob95216f815854ea55d61f91ac9696863f6ebac456
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
19 * Author:
20 * Jürg Billeter <j@bitron.ch>
23 using GLib;
25 /**
26 * Represents a local variable declaration in the C code.
28 public class Vala.CCodeDeclaration : CCodeStatement {
29 /**
30 * The type of the local variable.
32 public string type_name { get; set; }
34 /**
35 * The declaration modifier.
37 public CCodeModifiers modifiers { get; set; }
39 private List<CCodeDeclarator> declarators = new ArrayList<CCodeDeclarator> ();
41 public CCodeDeclaration (string type_name) {
42 this.type_name = type_name;
45 /**
46 * Adds the specified declarator to this declaration.
48 * @param decl a declarator
50 public void add_declarator (CCodeDeclarator decl) {
51 declarators.add (decl);
54 public override void write (CCodeWriter writer) {
55 if ((modifiers & (CCodeModifiers.STATIC | CCodeModifiers.EXTERN)) == 0) {
56 foreach (CCodeDeclarator decl in declarators) {
57 decl.write_initialization (writer);
62 private bool has_initializer () {
63 foreach (CCodeDeclarator decl in declarators) {
64 var var_decl = decl as CCodeVariableDeclarator;
65 if (var_decl != null && var_decl.initializer == null) {
66 return false;
69 return true;
72 public override void write_declaration (CCodeWriter writer) {
73 if ((modifiers & (CCodeModifiers.STATIC | CCodeModifiers.EXTERN)) != 0) {
74 // combined declaration and initialization for static and extern variables
75 writer.write_indent (line);
76 if ((modifiers & CCodeModifiers.STATIC) != 0) {
77 writer.write_string ("static ");
79 if ((modifiers & CCodeModifiers.VOLATILE) != 0) {
80 writer.write_string ("volatile ");
82 if ((modifiers & CCodeModifiers.EXTERN) != 0 && !has_initializer ()) {
83 writer.write_string ("extern ");
85 if ((modifiers & CCodeModifiers.THREAD_LOCAL) != 0) {
86 writer.write_string ("thread_local ");
88 writer.write_string (type_name);
89 writer.write_string (" ");
91 bool first = true;
92 foreach (CCodeDeclarator decl in declarators) {
93 if (!first) {
94 writer.write_string (", ");
95 } else {
96 first = false;
98 decl.write (writer);
101 if (CCodeModifiers.DEPRECATED in modifiers) {
102 writer.write_string (" G_GNUC_DEPRECATED");
105 writer.write_string (";");
106 writer.write_newline ();
107 return;
110 writer.write_indent ();
111 if ((modifiers & CCodeModifiers.REGISTER) == CCodeModifiers.REGISTER) {
112 writer.write_string ("register ");
114 writer.write_string (type_name);
115 writer.write_string (" ");
117 bool first = true;
118 foreach (CCodeDeclarator decl in declarators) {
119 if (!first) {
120 writer.write_string (", ");
121 } else {
122 first = false;
124 decl.write_declaration (writer);
127 writer.write_string (";");
128 writer.write_newline ();