codegen: Fix floating reference regression with Variants
[vala-gnome.git] / ccode / valaccodedeclaration.vala
blobeb69c6a61660330eef2c54d1209faab6118b579f
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 private List<CCodeDeclarator> declarators = new ArrayList<CCodeDeclarator> ();
36 public CCodeDeclaration (string type_name) {
37 this.type_name = type_name;
40 /**
41 * Adds the specified declarator to this declaration.
43 * @param decl a declarator
45 public void add_declarator (CCodeDeclarator decl) {
46 declarators.add (decl);
49 public override void write (CCodeWriter writer) {
50 if ((modifiers & (CCodeModifiers.STATIC | CCodeModifiers.INTERNAL | CCodeModifiers.EXTERN)) == 0) {
51 foreach (CCodeDeclarator decl in declarators) {
52 decl.write_initialization (writer);
57 private bool has_initializer () {
58 foreach (CCodeDeclarator decl in declarators) {
59 var var_decl = decl as CCodeVariableDeclarator;
60 if (var_decl != null && var_decl.initializer == null) {
61 return false;
64 return true;
67 public override void write_declaration (CCodeWriter writer) {
68 if ((modifiers & (CCodeModifiers.STATIC | CCodeModifiers.INTERNAL | CCodeModifiers.EXTERN)) != 0) {
69 // combined declaration and initialization for static and extern variables
70 writer.write_indent (line);
71 if ((modifiers & CCodeModifiers.INTERNAL) != 0) {
72 writer.write_string ("G_GNUC_INTERNAL ");
74 if ((modifiers & CCodeModifiers.STATIC) != 0) {
75 writer.write_string ("static ");
77 if ((modifiers & CCodeModifiers.VOLATILE) != 0) {
78 writer.write_string ("volatile ");
80 if ((modifiers & CCodeModifiers.EXTERN) != 0 && !has_initializer ()) {
81 writer.write_string ("extern ");
83 if ((modifiers & CCodeModifiers.THREAD_LOCAL) != 0) {
84 writer.write_string ("thread_local ");
86 writer.write_string (type_name);
87 writer.write_string (" ");
89 bool first = true;
90 foreach (CCodeDeclarator decl in declarators) {
91 if (!first) {
92 writer.write_string (", ");
93 } else {
94 first = false;
96 decl.write (writer);
99 writer.write_string (";");
100 writer.write_newline ();
101 return;
104 writer.write_indent ();
105 if ((modifiers & CCodeModifiers.REGISTER) == CCodeModifiers.REGISTER) {
106 writer.write_string ("register ");
108 if ((modifiers & CCodeModifiers.VOLATILE) != 0) {
109 writer.write_string ("volatile ");
111 writer.write_string (type_name);
112 writer.write_string (" ");
114 bool first = true;
115 foreach (CCodeDeclarator decl in declarators) {
116 if (!first) {
117 writer.write_string (", ");
118 } else {
119 first = false;
121 decl.write_declaration (writer);
124 if (CCodeModifiers.DEPRECATED in modifiers) {
125 writer.write_string (" G_GNUC_DEPRECATED");
128 writer.write_string (";");
129 writer.write_newline ();