d9bdd82b8851ab5157cb84dd7a26bb84b51662a7
[roobuilder] / src / ccode / valaccodedeclaration.vala
1 /* valaccodedeclaration.vala
2  *
3  * Copyright (C) 2006-2010  Jürg Billeter
4  *
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.
9
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.
14
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
18  *
19  * Author:
20  *      Jürg Billeter <j@bitron.ch>
21  */
22
23 using GLib;
24
25 /**
26  * Represents a local variable declaration in the C code.
27  */
28 public class Vala.CCodeDeclaration : CCodeStatement {
29         /**
30          * The type of the local variable.
31          */
32         public string type_name { get; set; }
33
34         private List<CCodeDeclarator> declarators = new ArrayList<CCodeDeclarator> ();
35
36         public CCodeDeclaration (string type_name) {
37                 this.type_name = type_name;
38         }
39
40         /**
41          * Adds the specified declarator to this declaration.
42          *
43          * @param decl a declarator
44          */
45         public void add_declarator (CCodeDeclarator decl) {
46                 declarators.add (decl);
47         }
48
49         /**
50          * Returns the list of declarators.
51          *
52          * @return declarators list
53          */
54         public unowned List<CCodeDeclarator> get_declarators () {
55                 return declarators;
56         }
57
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);
62                         }
63                 }
64         }
65
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) {
70                                 return false;
71                         }
72                 }
73                 return true;
74         }
75
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);
82                         }
83                         if ((modifiers & CCodeModifiers.STATIC) != 0) {
84                                 writer.write_string ("static ");
85                         }
86                         if ((modifiers & CCodeModifiers.VOLATILE) != 0) {
87                                 writer.write_string ("volatile ");
88                         }
89                         if ((modifiers & CCodeModifiers.EXTERN) != 0 && !has_initializer ()) {
90                                 writer.write_string ("VALA_EXTERN ");
91                         }
92                         if ((modifiers & CCodeModifiers.THREAD_LOCAL) != 0) {
93                                 writer.write_string ("thread_local ");
94                         }
95                         writer.write_string (type_name);
96                         writer.write_string (" ");
97
98                         bool first = true;
99                         foreach (CCodeDeclarator decl in declarators) {
100                                 if (!first) {
101                                         writer.write_string (", ");
102                                 } else {
103                                         first = false;
104                                 }
105                                 decl.write (writer);
106                         }
107
108                         writer.write_string (";");
109                         writer.write_newline ();
110                         return;
111                 }
112
113                 writer.write_indent ();
114                 if ((modifiers & CCodeModifiers.REGISTER) == CCodeModifiers.REGISTER) {
115                         writer.write_string ("register ");
116                 }
117                 if ((modifiers & CCodeModifiers.VOLATILE) != 0) {
118                         writer.write_string ("volatile ");
119                 }
120                 writer.write_string (type_name);
121                 writer.write_string (" ");
122
123                 bool first = true;
124                 foreach (CCodeDeclarator decl in declarators) {
125                         if (!first) {
126                                 writer.write_string (", ");
127                         } else {
128                                 first = false;
129                         }
130                         decl.write_declaration (writer);
131                 }
132
133                 if (CCodeModifiers.DEPRECATED in modifiers) {
134                         writer.write_string (GNUC_DEPRECATED);
135                 }
136
137                 writer.write_string (";");
138                 writer.write_newline ();
139         }
140 }