change run to use meson/ninja and then exec. - remove libvala code from application...
[roobuilder] / src / ccode / valaccodevariabledeclarator.vala
1 /* valaccodevariabledeclarator.vala
2  *
3  * Copyright (C) 2006-2009  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 variable declarator in the C code.
27  */
28 public class Vala.CCodeVariableDeclarator : CCodeDeclarator {
29         /**
30          * The optional initializer expression.
31          */
32         public CCodeExpression? initializer { get; set; }
33
34         /**
35          * The optional declarator suffix.
36          */
37         public CCodeDeclaratorSuffix? declarator_suffix { get; set; }
38
39         /**
40          * Initializer only used to zero memory, safe to initialize as part
41          * of declaration at beginning of block instead of separate assignment.
42          */
43         public bool init0 { get; set; }
44
45         public CCodeVariableDeclarator (string name, CCodeExpression? initializer = null, CCodeDeclaratorSuffix? declarator_suffix = null) {
46                 this.name = name;
47                 this.initializer = initializer;
48                 this.declarator_suffix = declarator_suffix;
49         }
50
51         public CCodeVariableDeclarator.zero (string name, CCodeExpression? initializer, CCodeDeclaratorSuffix? declarator_suffix = null) {
52                 this.name = name;
53                 this.initializer = initializer;
54                 this.declarator_suffix = declarator_suffix;
55                 this.init0 = true;
56         }
57
58         public override void write (CCodeWriter writer) {
59                 writer.write_string (name);
60
61                 if (declarator_suffix != null) {
62                         declarator_suffix.write (writer);
63                 }
64
65                 if (initializer != null) {
66                         writer.write_string (" = ");
67                         initializer.write (writer);
68                 }
69         }
70
71         public override void write_declaration (CCodeWriter writer) {
72                 writer.write_string (name);
73
74                 if (declarator_suffix != null) {
75                         declarator_suffix.write (writer);
76                 }
77
78                 if (initializer != null && init0) {
79                         writer.write_string (" = ");
80                         initializer.write (writer);
81                 }
82         }
83
84         public override void write_initialization (CCodeWriter writer) {
85                 if (initializer != null && !init0) {
86                         writer.write_indent (line);
87
88                         writer.write_string (name);
89                         writer.write_string (" = ");
90                         initializer.write (writer);
91
92                         writer.write_string (";");
93                         writer.write_newline ();
94                 }
95         }
96 }
97
98 public class Vala.CCodeDeclaratorSuffix {
99         bool array;
100         List<CCodeExpression>? array_length;
101
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);
106                 }
107                 array = true;
108         }
109
110         public CCodeDeclaratorSuffix.with_multi_array (List<CCodeExpression>? array_length = null) {
111                 this.array_length = array_length;
112                 array = true;
113         }
114
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);
121                                 }
122                                 writer.write_string ("]");
123                         }
124                 } else if (array) {
125                         writer.write_string ("[]");
126                 }
127         }
128 }