1 /* valaccodevariabledeclarator.vala
3 * Copyright (C) 2006-2009 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 variable declarator in the C code.
28 public class Vala
.CCodeVariableDeclarator
: CCodeDeclarator
{
32 public string name
{ get; set; }
35 * The optional initializer expression.
37 public CCodeExpression? initializer
{ get; set; }
40 * The optional declarator suffix.
42 public CCodeDeclaratorSuffix? declarator_suffix
{ get; set; }
45 * Initializer only used to zero memory, safe to initialize as part
46 * of declaration at beginning of block instead of separate assignment.
48 public bool init0
{ get; set; }
50 public CCodeVariableDeclarator (string name
, CCodeExpression? initializer
= null, CCodeDeclaratorSuffix? declarator_suffix
= null) {
52 this
.initializer
= initializer
;
53 this
.declarator_suffix
= declarator_suffix
;
56 public CCodeVariableDeclarator
.zero (string name
, CCodeExpression? initializer
, CCodeDeclaratorSuffix? declarator_suffix
= null) {
58 this
.initializer
= initializer
;
59 this
.declarator_suffix
= declarator_suffix
;
63 public override void write (CCodeWriter writer
) {
64 writer
.write_string (name
);
66 if (declarator_suffix
!= null) {
67 declarator_suffix
.write (writer
);
70 if (initializer
!= null) {
71 writer
.write_string (" = ");
72 initializer
.write (writer
);
76 public override void write_declaration (CCodeWriter writer
) {
77 writer
.write_string (name
);
79 if (declarator_suffix
!= null) {
80 declarator_suffix
.write (writer
);
83 if (initializer
!= null && init0
) {
84 writer
.write_string (" = ");
85 initializer
.write (writer
);
89 public override void write_initialization (CCodeWriter writer
) {
90 if (initializer
!= null && !init0
) {
91 writer
.write_indent (line
);
93 writer
.write_string (name
);
94 writer
.write_string (" = ");
95 initializer
.write (writer
);
97 writer
.write_string (";");
98 writer
.write_newline ();
103 public class Vala
.CCodeDeclaratorSuffix
{
105 CCodeExpression? array_length
;
107 public CCodeDeclaratorSuffix
.with_array (CCodeExpression? array_length
= null) {
108 this
.array_length
= array_length
;
112 public void write (CCodeWriter writer
) {
114 writer
.write_string ("[");
115 if (array_length
!= null) {
116 array_length
.write (writer
);
118 writer
.write_string ("]");