vala: Allow read-only properties
[vala-gnome.git] / ccode / valaccodevariabledeclarator.vala
blob17fa478a3d0ae8f3b59baec5fbae8bd5b33bce0e
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
19 * Author:
20 * Jürg Billeter <j@bitron.ch>
23 using GLib;
25 /**
26 * Represents a variable declarator in the C code.
28 public class Vala.CCodeVariableDeclarator : CCodeDeclarator {
29 /**
30 * The variable name.
32 public string name { get; set; }
34 /**
35 * The optional initializer expression.
37 public CCodeExpression? initializer { get; set; }
39 /**
40 * The optional declarator suffix.
42 public CCodeDeclaratorSuffix? declarator_suffix { get; set; }
44 /**
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) {
51 this.name = name;
52 this.initializer = initializer;
53 this.declarator_suffix = declarator_suffix;
56 public CCodeVariableDeclarator.zero (string name, CCodeExpression? initializer, CCodeDeclaratorSuffix? declarator_suffix = null) {
57 this.name = name;
58 this.initializer = initializer;
59 this.declarator_suffix = declarator_suffix;
60 this.init0 = true;
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 {
104 bool array;
105 CCodeExpression? array_length;
107 public CCodeDeclaratorSuffix.with_array (CCodeExpression? array_length = null) {
108 this.array_length = array_length;
109 array = true;
112 public void write (CCodeWriter writer) {
113 if (array) {
114 writer.write_string ("[");
115 if (array_length != null) {
116 array_length.write (writer);
118 writer.write_string ("]");