Line data Source code
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 99026 : public class Vala.CCodeVariableDeclarator : CCodeDeclarator {
29 : /**
30 : * The optional initializer expression.
31 : */
32 206787 : public CCodeExpression? initializer { get; set; }
33 :
34 : /**
35 : * The optional declarator suffix.
36 : */
37 196350 : 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 10154 : public bool init0 { get; set; }
44 :
45 289878 : public CCodeVariableDeclarator (string name, CCodeExpression? initializer = null, CCodeDeclaratorSuffix? declarator_suffix = null) {
46 96626 : this.name = name;
47 96626 : this.initializer = initializer;
48 96626 : this.declarator_suffix = declarator_suffix;
49 : }
50 :
51 4647 : public CCodeVariableDeclarator.zero (string name, CCodeExpression? initializer, CCodeDeclaratorSuffix? declarator_suffix = null) {
52 1549 : this.name = name;
53 1549 : this.initializer = initializer;
54 1549 : this.declarator_suffix = declarator_suffix;
55 1549 : this.init0 = true;
56 : }
57 :
58 6465 : public override void write (CCodeWriter writer) {
59 6465 : writer.write_string (name);
60 :
61 6465 : if (declarator_suffix != null) {
62 531 : declarator_suffix.write (writer);
63 : }
64 :
65 6465 : if (initializer != null) {
66 4827 : writer.write_string (" = ");
67 4827 : initializer.write (writer);
68 : }
69 : }
70 :
71 74425 : public override void write_declaration (CCodeWriter writer) {
72 74425 : writer.write_string (name);
73 :
74 74425 : if (declarator_suffix != null) {
75 84 : declarator_suffix.write (writer);
76 : }
77 :
78 74425 : if (initializer != null && init0) {
79 10105 : writer.write_string (" = ");
80 10105 : initializer.write (writer);
81 : }
82 : }
83 :
84 63871 : public override void write_initialization (CCodeWriter writer) {
85 63871 : if (initializer != null && !init0) {
86 4374 : writer.write_indent (line);
87 :
88 4374 : writer.write_string (name);
89 4374 : writer.write_string (" = ");
90 4374 : initializer.write (writer);
91 :
92 4374 : writer.write_string (";");
93 4374 : writer.write_newline ();
94 : }
95 : }
96 : }
97 :
98 2349 : public class Vala.CCodeDeclaratorSuffix {
99 : bool array;
100 691 : List<CCodeExpression>? array_length;
101 :
102 1272 : public CCodeDeclaratorSuffix.with_array (CCodeExpression? array_length = null) {
103 636 : if (array_length != null) {
104 635 : this.array_length = new ArrayList<CCodeExpression> ();
105 635 : this.array_length.add (array_length);
106 : }
107 636 : array = true;
108 : }
109 :
110 110 : public CCodeDeclaratorSuffix.with_multi_array (List<CCodeExpression>? array_length = null) {
111 55 : this.array_length = array_length;
112 55 : array = true;
113 : }
114 :
115 615 : public void write (CCodeWriter writer) {
116 615 : if (array_length != null && array_length.size > 0) {
117 1850 : foreach (var length in array_length) {
118 618 : writer.write_string ("[");
119 618 : if (length != null) {
120 618 : length.write (writer);
121 : }
122 618 : writer.write_string ("]");
123 : }
124 1 : } else if (array) {
125 1 : writer.write_string ("[]");
126 : }
127 : }
128 : }
|