summaryrefslogtreecommitdiff
path: root/ccode/valaccodefile.vala
blob: f61da07dfa1039868156bfdf87012613cadeace6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/* valaccodefile.vala
 *
 * Copyright (C) 2009-2011  Jürg Billeter
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.

 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 *
 * Author:
 * 	Jürg Billeter <j@bitron.ch>
 */


public class Vala.CCodeFile {
	public CCodeFileType file_type { get; private set; }

	public weak SourceFile? file { get; private set; }

	Set<string> features = new HashSet<string> (str_hash, str_equal);
	Set<string> declarations = new HashSet<string> (str_hash, str_equal);
	Set<string> definitions = new HashSet<string> (str_hash, str_equal);
	Set<string> includes = new HashSet<string> (str_hash, str_equal);
	CCodeFragment comments = new CCodeFragment ();
	CCodeFragment feature_test_macros = new CCodeFragment ();
	CCodeFragment define_directives = new CCodeFragment ();
	CCodeFragment include_directives = new CCodeFragment ();
	CCodeFragment type_declaration = new CCodeFragment ();
	CCodeFragment type_definition = new CCodeFragment ();
	CCodeFragment type_member_declaration = new CCodeFragment ();
	CCodeFragment constant_declaration = new CCodeFragment ();
	CCodeFragment type_member_definition = new CCodeFragment ();

	public CCodeFile (CCodeFileType type = CCodeFileType.SOURCE, SourceFile? source_file = null) {
		file = source_file;
		file_type = type;
	}

	public bool add_declaration (string name) {
		if (name in declarations) {
			return true;
		}
		declarations.add (name);
		return false;
	}

	public void add_comment (CCodeComment comment) {
		comments.append (comment);
	}

	public void add_feature_test_macro (string feature_test_macro) {
		if (!(feature_test_macro in features)) {
			feature_test_macros.append (new CCodeDefine (feature_test_macro));
			features.add (feature_test_macro);
		}
	}

	public void add_include (string filename, bool local = false) {
		if (!(filename in includes)) {
			include_directives.append (new CCodeIncludeDirective (filename, local));
			includes.add (filename);
		}
	}

	public void add_define (CCodeNode node) {
		define_directives.append (node);
	}

	public void add_type_declaration (CCodeNode node) {
		type_declaration.append (node);
	}

	public void add_type_definition (CCodeNode node) {
		type_definition.append (node);
	}

	public void add_type_member_declaration (CCodeNode node) {
		type_member_declaration.append (node);
	}

	public void add_constant_declaration (CCodeNode node) {
		constant_declaration.append (node);
	}

	public void add_type_member_definition (CCodeNode node) {
		type_member_definition.append (node);
	}

	public void add_function_declaration (CCodeFunction func) {
		declarations.add (func.name);

		var decl = func.copy ();
		decl.is_declaration = true;
		type_member_declaration.append (decl);
	}

	public void add_function (CCodeFunction func) {
		if (!definitions.add (func.name)) {
			Report.error (null, "internal: Redefinition of `%s'", func.name);
			return;
		}

		type_member_definition.append (func);
	}

	public List<string> get_symbols () {
		var symbols = new ArrayList<string> (str_equal);
		get_symbols_from_fragment (symbols, type_member_declaration);
		return symbols;
	}

	void get_symbols_from_fragment (List<string> symbols, CCodeFragment fragment) {
		foreach (CCodeNode node in fragment.get_children ()) {
			if (node is CCodeFragment) {
				get_symbols_from_fragment (symbols, (CCodeFragment) node);
			} else {
				var func = node as CCodeFunction;
				if (func != null) {
					symbols.add (func.name);
				}
			}
		}
	}

	static string get_define_for_filename (string filename) {
		var define = new StringBuilder ("__");

		var i = filename;
		while (i.length > 0) {
			var c = i.get_char ();
			if (c.isalnum  () && c < 0x80) {
				define.append_unichar (c.toupper ());
			} else {
				define.append_c ('_');
			}

			i = i.next_char ();
		}

		define.append ("__");

		return define.str;
	}

	public bool store (string filename, string? source_filename, bool write_version, bool line_directives, string? begin_decls = null, string? end_decls = null) {
		var writer = new CCodeWriter (filename, source_filename);
		if (!writer.open (write_version)) {
			return false;
		}

		if (file_type == CCodeFileType.SOURCE) {
			writer.line_directives = line_directives;

			comments.write (writer);
			writer.write_newline ();
			feature_test_macros.write (writer);
			writer.write_newline ();
			include_directives.write (writer);
			writer.write_newline ();
			define_directives.write (writer);
			writer.write_newline ();
			type_declaration.write_combined (writer);
			writer.write_newline ();
			type_definition.write_combined (writer);
			writer.write_newline ();
			type_member_declaration.write_declaration (writer);
			writer.write_newline ();
			type_member_declaration.write (writer);
			writer.write_newline ();
			constant_declaration.write_combined (writer);
			writer.write_newline ();
			type_member_definition.write (writer);
			writer.write_newline ();
		} else {
			writer.write_newline ();

			var once = new CCodeOnceSection (get_define_for_filename (writer.filename));
			once.append (new CCodeNewline ());
			once.append (include_directives);
			once.append (new CCodeNewline ());

			if (begin_decls != null) {
				once.append (new CCodeIdentifier (begin_decls));
				once.append (new CCodeNewline ());
			}

			once.append (new CCodeNewline ());
			once.append (define_directives);
			once.append (new CCodeNewline ());
			once.append (type_declaration);
			once.append (new CCodeNewline ());
			once.append (type_definition);
			once.append (new CCodeNewline ());
			once.append (type_member_declaration);
			once.append (new CCodeNewline ());
			once.append (constant_declaration);
			once.append (new CCodeNewline ());

			if (end_decls != null) {
				once.append (new CCodeIdentifier (end_decls));
				once.append (new CCodeNewline ());
			}

			once.append (new CCodeNewline ());
			once.write (writer);
		}

		writer.close ();

		return true;
	}
}

[Flags]
public enum CCodeFileType {
	SOURCE,
	PUBLIC_HEADER,
	INTERNAL_HEADER,
	HEADER = PUBLIC_HEADER | INTERNAL_HEADER
}