gstreamer: Cherry-pick some fixes from 0.42
[vala-gnome.git] / ccode / valaccodefile.vala
blob128cd393830183c1c94635eb9196df56a829d8d2
1 /* valaccodefile.vala
3 * Copyright (C) 2009-2011 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>
24 public class Vala.CCodeFile {
25 public bool is_header { get; set; }
27 Set<string> declarations = new HashSet<string> (str_hash, str_equal);
28 Set<string> includes = new HashSet<string> (str_hash, str_equal);
29 CCodeFragment comments = new CCodeFragment ();
30 CCodeFragment include_directives = new CCodeFragment ();
31 CCodeFragment type_declaration = new CCodeFragment ();
32 CCodeFragment type_definition = new CCodeFragment ();
33 CCodeFragment type_member_declaration = new CCodeFragment ();
34 CCodeFragment constant_declaration = new CCodeFragment ();
35 CCodeFragment type_member_definition = new CCodeFragment ();
37 public bool add_declaration (string name) {
38 if (name in declarations) {
39 return true;
41 declarations.add (name);
42 return false;
45 public void add_comment (CCodeComment comment) {
46 comments.append (comment);
49 public void add_include (string filename, bool local = false) {
50 if (!(filename in includes)) {
51 include_directives.append (new CCodeIncludeDirective (filename, local));
52 includes.add (filename);
56 public void add_type_declaration (CCodeNode node) {
57 type_declaration.append (node);
60 public void add_type_definition (CCodeNode node) {
61 type_definition.append (node);
64 public void add_type_member_declaration (CCodeNode node) {
65 type_member_declaration.append (node);
68 public void add_constant_declaration (CCodeNode node) {
69 constant_declaration.append (node);
72 public void add_type_member_definition (CCodeNode node) {
73 type_member_definition.append (node);
76 public void add_function_declaration (CCodeFunction func) {
77 var decl = func.copy ();
78 decl.is_declaration = true;
79 type_member_declaration.append (decl);
82 public void add_function (CCodeFunction func) {
83 type_member_definition.append (func);
86 public List<string> get_symbols () {
87 var symbols = new ArrayList<string> ();
88 get_symbols_from_fragment (symbols, type_member_declaration);
89 return symbols;
92 void get_symbols_from_fragment (List<string> symbols, CCodeFragment fragment) {
93 foreach (CCodeNode node in fragment.get_children ()) {
94 if (node is CCodeFragment) {
95 get_symbols_from_fragment (symbols, (CCodeFragment) node);
96 } else {
97 var func = node as CCodeFunction;
98 if (func != null) {
99 symbols.add (func.name);
105 static string get_define_for_filename (string filename) {
106 var define = new StringBuilder ("__");
108 var i = filename;
109 while (i.length > 0) {
110 var c = i.get_char ();
111 if (c.isalnum () && c < 0x80) {
112 define.append_unichar (c.toupper ());
113 } else {
114 define.append_c ('_');
117 i = i.next_char ();
120 define.append ("__");
122 return define.str;
125 public bool store (string filename, string? source_filename, bool write_version, bool line_directives, string? begin_decls = null, string? end_decls = null) {
126 var writer = new CCodeWriter (filename, source_filename);
127 if (!writer.open (write_version)) {
128 return false;
131 if (!is_header) {
132 writer.line_directives = line_directives;
134 comments.write (writer);
135 writer.write_newline ();
136 include_directives.write (writer);
137 writer.write_newline ();
138 type_declaration.write_combined (writer);
139 writer.write_newline ();
140 type_definition.write_combined (writer);
141 writer.write_newline ();
142 type_member_declaration.write_declaration (writer);
143 writer.write_newline ();
144 type_member_declaration.write (writer);
145 writer.write_newline ();
146 constant_declaration.write_combined (writer);
147 writer.write_newline ();
148 type_member_definition.write (writer);
149 writer.write_newline ();
150 } else {
151 writer.write_newline ();
153 var once = new CCodeOnceSection (get_define_for_filename (writer.filename));
154 once.append (new CCodeNewline ());
155 once.append (include_directives);
156 once.append (new CCodeNewline ());
158 if (begin_decls != null) {
159 once.append (new CCodeIdentifier (begin_decls));
160 once.append (new CCodeNewline ());
163 once.append (new CCodeNewline ());
164 once.append (type_declaration);
165 once.append (new CCodeNewline ());
166 once.append (type_definition);
167 once.append (new CCodeNewline ());
168 once.append (type_member_declaration);
169 once.append (new CCodeNewline ());
170 once.append (constant_declaration);
171 once.append (new CCodeNewline ());
173 if (begin_decls != null) {
174 once.append (new CCodeIdentifier (end_decls));
175 once.append (new CCodeNewline ());
178 once.append (new CCodeNewline ());
179 once.write (writer);
182 writer.close ();
184 return true;