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 {
30 * The optional initializer expression.
32 public CCodeExpression? initializer { get; set; }
35 * The optional declarator suffix.
37 public CCodeDeclaratorSuffix? declarator_suffix { get; set; }
40 * Initializer only used to zero memory, safe to initialize as part
41 * of declaration at beginning of block instead of separate assignment.
43 public bool init0 { get; set; }
45 public CCodeVariableDeclarator (string name, CCodeExpression? initializer = null, CCodeDeclaratorSuffix? declarator_suffix = null) {
47 this.initializer = initializer;
48 this.declarator_suffix = declarator_suffix;
51 public CCodeVariableDeclarator.zero (string name, CCodeExpression? initializer, CCodeDeclaratorSuffix? declarator_suffix = null) {
53 this.initializer = initializer;
54 this.declarator_suffix = declarator_suffix;
58 public override void write (CCodeWriter writer) {
59 writer.write_string (name);
61 if (declarator_suffix != null) {
62 declarator_suffix.write (writer);
65 if (initializer != null) {
66 writer.write_string (" = ");
67 initializer.write (writer);
71 public override void write_declaration (CCodeWriter writer) {
72 writer.write_string (name);
74 if (declarator_suffix != null) {
75 declarator_suffix.write (writer);
78 if (initializer != null && init0) {
79 writer.write_string (" = ");
80 initializer.write (writer);
84 public override void write_initialization (CCodeWriter writer) {
85 if (initializer != null && !init0) {
86 writer.write_indent (line);
88 writer.write_string (name);
89 writer.write_string (" = ");
90 initializer.write (writer);
92 writer.write_string (";");
93 writer.write_newline ();
98 public class Vala.CCodeDeclaratorSuffix {
100 List<CCodeExpression>? array_length;
102 public CCodeDeclaratorSuffix.with_array (CCodeExpression? array_length = null) {
103 if (array_length != null) {
104 this.array_length = new ArrayList<CCodeExpression> ();
105 this.array_length.add (array_length);
110 public CCodeDeclaratorSuffix.with_multi_array (List<CCodeExpression>? array_length = null) {
111 this.array_length = array_length;
115 public void write (CCodeWriter writer) {
116 if (array_length != null && array_length.size > 0) {
117 foreach (var length in array_length) {
118 writer.write_string ("[");
119 if (length != null) {
120 length.write (writer);
122 writer.write_string ("]");
125 writer.write_string ("[]");