Line data Source code
1 : /* valaccodedeclaration.vala
2 : *
3 : * Copyright (C) 2006-2010 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 local variable declaration in the C code.
27 : */
28 185052 : public class Vala.CCodeDeclaration : CCodeStatement {
29 : /**
30 : * The type of the local variable.
31 : */
32 276327 : public string type_name { get; set; }
33 :
34 184218 : private List<CCodeDeclarator> declarators = new ArrayList<CCodeDeclarator> ();
35 :
36 276327 : public CCodeDeclaration (string type_name) {
37 92109 : this.type_name = type_name;
38 : }
39 :
40 : /**
41 : * Adds the specified declarator to this declaration.
42 : *
43 : * @param decl a declarator
44 : */
45 92109 : public void add_declarator (CCodeDeclarator decl) {
46 92109 : declarators.add (decl);
47 : }
48 :
49 : /**
50 : * Returns the list of declarators.
51 : *
52 : * @return declarators list
53 : */
54 0 : public unowned List<CCodeDeclarator> get_declarators () {
55 0 : return declarators;
56 : }
57 :
58 69798 : public override void write (CCodeWriter writer) {
59 69798 : if ((modifiers & (CCodeModifiers.STATIC | CCodeModifiers.INTERNAL | CCodeModifiers.EXTERN)) == 0) {
60 191613 : foreach (CCodeDeclarator decl in declarators) {
61 63871 : decl.write_initialization (writer);
62 : }
63 : }
64 : }
65 :
66 548 : private bool has_initializer () {
67 1130 : foreach (CCodeDeclarator decl in declarators) {
68 548 : var var_decl = decl as CCodeVariableDeclarator;
69 548 : if (var_decl != null && var_decl.initializer == null) {
70 257 : return false;
71 : }
72 : }
73 548 : return true;
74 : }
75 :
76 77619 : public override void write_declaration (CCodeWriter writer) {
77 77619 : if ((modifiers & (CCodeModifiers.STATIC | CCodeModifiers.INTERNAL | CCodeModifiers.EXTERN)) != 0) {
78 : // combined declaration and initialization for static and extern variables
79 5927 : writer.write_indent (line);
80 5927 : if ((modifiers & CCodeModifiers.INTERNAL) != 0) {
81 0 : writer.write_string (GNUC_INTERNAL);
82 : }
83 5927 : if ((modifiers & CCodeModifiers.STATIC) != 0) {
84 5379 : writer.write_string ("static ");
85 : }
86 5927 : if ((modifiers & CCodeModifiers.VOLATILE) != 0) {
87 1059 : writer.write_string ("volatile ");
88 : }
89 5927 : if ((modifiers & CCodeModifiers.EXTERN) != 0 && !has_initializer ()) {
90 257 : writer.write_string ("VALA_EXTERN ");
91 : }
92 5927 : if ((modifiers & CCodeModifiers.THREAD_LOCAL) != 0) {
93 0 : writer.write_string ("thread_local ");
94 : }
95 5927 : writer.write_string (type_name);
96 5927 : writer.write_string (" ");
97 :
98 5927 : bool first = true;
99 17781 : foreach (CCodeDeclarator decl in declarators) {
100 5927 : if (!first) {
101 0 : writer.write_string (", ");
102 : } else {
103 : first = false;
104 : }
105 5927 : decl.write (writer);
106 : }
107 :
108 5927 : writer.write_string (";");
109 5927 : writer.write_newline ();
110 5927 : return;
111 : }
112 :
113 71692 : writer.write_indent ();
114 71692 : if ((modifiers & CCodeModifiers.REGISTER) == CCodeModifiers.REGISTER) {
115 136 : writer.write_string ("register ");
116 : }
117 71692 : if ((modifiers & CCodeModifiers.VOLATILE) != 0) {
118 0 : writer.write_string ("volatile ");
119 : }
120 71692 : writer.write_string (type_name);
121 71692 : writer.write_string (" ");
122 :
123 71692 : bool first = true;
124 215076 : foreach (CCodeDeclarator decl in declarators) {
125 71692 : if (!first) {
126 0 : writer.write_string (", ");
127 : } else {
128 : first = false;
129 : }
130 71692 : decl.write_declaration (writer);
131 : }
132 :
133 71692 : if (CCodeModifiers.DEPRECATED in modifiers) {
134 2 : writer.write_string (GNUC_DEPRECATED);
135 : }
136 :
137 71692 : writer.write_string (";");
138 71692 : writer.write_newline ();
139 : }
140 : }
|