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
20 * Jürg Billeter <j@bitron.ch>
27 * Represents a local variable declaration in the C code.
29 public class Vala
.CCodeDeclaration
: CCodeStatement
{
31 * The type of the local variable.
33 public string type_name
{ get; set; }
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
;
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 (" ");
72 foreach (CCodeDeclarator decl
in declarators
) {
74 writer
.write_string (", ");
81 writer
.write_string (";");
82 writer
.write_newline ();
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) {
100 public override void write_declaration (CCodeWriter writer
) {
101 if ((modifiers
& (CCodeModifiers
.STATIC
| CCodeModifiers
.EXTERN
)) != 0) {
102 // no separate declaration for static variables
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 (" ");
114 foreach (CCodeDeclarator decl
in declarators
) {
116 writer
.write_string (", ");
120 decl
.write_declaration (writer
);
123 writer
.write_string (";");
124 writer
.write_newline ();