valacodegeneratorsourcefile.vala 9.36 KB
Newer Older
Jürg Billeter's avatar
Jürg Billeter committed
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
/* valacodegeneratorsourcefile.vala
 *
 * Copyright (C) 2006-2007  Jürg Billeter, Raffaele Sandrini
 *
 * 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 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>
 *	Raffaele Sandrini <rasa@gmx.ch>
 */

using GLib;

public class Vala.CodeGenerator {
27
	private CCodeIncludeDirective get_internal_include (string! filename) {
Jürg Billeter's avatar
Jürg Billeter committed
28
29
30
		return new CCodeIncludeDirective (filename, context.library == null);
	}

31
	public override void visit_source_file (SourceFile! source_file) {
Jürg Billeter's avatar
Jürg Billeter committed
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
		header_begin = new CCodeFragment ();
		header_type_declaration = new CCodeFragment ();
		header_type_definition = new CCodeFragment ();
		header_type_member_declaration = new CCodeFragment ();
		source_begin = new CCodeFragment ();
		source_include_directives = new CCodeFragment ();
		source_type_member_declaration = new CCodeFragment ();
		source_type_member_definition = new CCodeFragment ();
		source_signal_marshaller_definition = new CCodeFragment ();
		source_signal_marshaller_declaration = new CCodeFragment ();
		
		user_marshal_list = new HashTable (str_hash, str_equal);
		
		next_temp_var_id = 0;
		
		string_h_needed = false;
Jürg Billeter's avatar
Jürg Billeter committed
48
49
		requires_free_checked = false;
		requires_array_free = false;
Jürg Billeter's avatar
Jürg Billeter committed
50
51
52
53
54
		
		header_begin.append (new CCodeIncludeDirective ("glib.h"));
		header_begin.append (new CCodeIncludeDirective ("glib-object.h"));
		source_include_directives.append (new CCodeIncludeDirective (source_file.get_cheader_filename (), true));
		
55
		List<string> used_includes = null;
Jürg Billeter's avatar
Jürg Billeter committed
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
		used_includes.append ("glib.h");
		used_includes.append ("glib-object.h");
		used_includes.append (source_file.get_cheader_filename ());
		
		foreach (string filename1 in source_file.get_header_external_includes ()) {
			if (used_includes.find_custom (filename1, strcmp) == null) {
				header_begin.append (new CCodeIncludeDirective (filename1));
				used_includes.append (filename1);
			}
		}
		foreach (string filename2 in source_file.get_header_internal_includes ()) {
			if (used_includes.find_custom (filename2, strcmp) == null) {
				header_begin.append (get_internal_include (filename2));
				used_includes.append (filename2);
			}
		}
		foreach (string filename3 in source_file.get_source_external_includes ()) {
			if (used_includes.find_custom (filename3, strcmp) == null) {
				source_include_directives.append (new CCodeIncludeDirective (filename3));
				used_includes.append (filename3);
			}
		}
		foreach (string filename4 in source_file.get_source_internal_includes ()) {
			if (used_includes.find_custom (filename4, strcmp) == null) {
				source_include_directives.append (get_internal_include (filename4));
				used_includes.append (filename4);
			}
		}
		if (source_file.is_cycle_head) {
			foreach (SourceFile cycle_file in source_file.cycle.files) {
86
87
88
				foreach (CodeNode node in cycle_file.get_nodes ()) {
					if (node is Struct) {
						var st = (Struct) node;
Jürg Billeter's avatar
Jürg Billeter committed
89
						header_type_declaration.append (new CCodeTypeDefinition ("struct _%s".printf (st.get_cname ()), new CCodeVariableDeclarator (st.get_cname ())));
90
91
					} else if (node is Class) {
						var cl = (Class) node;
Jürg Billeter's avatar
Jürg Billeter committed
92
93
						header_type_declaration.append (new CCodeTypeDefinition ("struct _%s".printf (cl.get_cname ()), new CCodeVariableDeclarator (cl.get_cname ())));
						header_type_declaration.append (new CCodeTypeDefinition ("struct _%sClass".printf (cl.get_cname ()), new CCodeVariableDeclarator ("%sClass".printf (cl.get_cname ()))));
94
95
					} else if (node is Interface) {
						var iface = (Interface) node;
Jürg Billeter's avatar
Jürg Billeter committed
96
97
98
99
100
101
						header_type_declaration.append (new CCodeTypeDefinition ("struct _%s".printf (iface.get_cname ()), new CCodeVariableDeclarator (iface.get_cname ())));
						header_type_declaration.append (new CCodeTypeDefinition ("struct _%s".printf (iface.get_type_cname ()), new CCodeVariableDeclarator (iface.get_type_cname ())));
					}
				}
			}
		}
102
103
104

		source_file.accept_children (this);

Jürg Billeter's avatar
Jürg Billeter committed
105
106
107
108
109
110
		var header_define = get_define_for_filename (source_file.get_cheader_filename ());
		
		if (string_h_needed) {
			source_include_directives.append (new CCodeIncludeDirective ("string.h"));
		}
		
Jürg Billeter's avatar
Jürg Billeter committed
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
		/* generate hardcoded "well-known" macros */
		if (requires_free_checked) {
			source_begin.append (new CCodeMacroReplacement ("VALA_FREE_CHECKED(o,f)", "((o) == NULL ? NULL : ((o) = (f (o), NULL)))"));
		}
		if (requires_array_free) {
			var fun = new CCodeFunction ("_vala_array_free", "void");
			fun.modifiers = CCodeModifiers.STATIC;
			fun.add_parameter (new CCodeFormalParameter ("array", "gpointer"));
			fun.add_parameter (new CCodeFormalParameter ("array_length", "gint"));
			fun.add_parameter (new CCodeFormalParameter ("destroy_func", "GDestroyNotify"));
			source_type_member_declaration.append (fun.copy ());

			var cdofree = new CCodeBlock ();

			var citdecl = new CCodeDeclaration ("int");
			citdecl.add_declarator (new CCodeVariableDeclarator ("i"));
			cdofree.add_statement (citdecl);
			
			var cbody = new CCodeBlock ();
			
			var cptrarray = new CCodeCastExpression (new CCodeIdentifier ("array"), "gpointer*");
			var cea = new CCodeElementAccess (cptrarray, new CCodeIdentifier ("i"));
			var cfreecond = new CCodeBinaryExpression (CCodeBinaryOperator.INEQUALITY, cea, new CCodeConstant ("NULL"));
			var cfreecall = new CCodeFunctionCall (new CCodeIdentifier ("destroy_func"));
			cfreecall.add_argument (cea);
			cbody.add_statement (new CCodeIfStatement (cfreecond, new CCodeExpressionStatement (cfreecall)));

			var cforcond = new CCodeBinaryExpression (CCodeBinaryOperator.LESS_THAN, new CCodeIdentifier ("i"), new CCodeIdentifier ("array_length"));
			
			var cfor = new CCodeForStatement (cforcond, cbody);
			cfor.add_initializer (new CCodeAssignment (new CCodeIdentifier ("i"), new CCodeConstant ("0")));
			cfor.add_iterator (new CCodeAssignment (new CCodeIdentifier ("i"), new CCodeBinaryExpression (CCodeBinaryOperator.PLUS, new CCodeIdentifier ("i"), new CCodeConstant ("1"))));
			cdofree.add_statement (cfor);

			var ccondarr = new CCodeBinaryExpression (CCodeBinaryOperator.INEQUALITY, new CCodeIdentifier ("array"), new CCodeConstant ("NULL"));
			var ccondfunc = new CCodeBinaryExpression (CCodeBinaryOperator.INEQUALITY, new CCodeIdentifier ("destroy_func"), new CCodeConstant ("NULL"));
			var cif = new CCodeIfStatement (new CCodeBinaryExpression (CCodeBinaryOperator.AND, ccondarr, ccondfunc), cdofree);
			fun.block = new CCodeBlock ();
			fun.block.add_statement (cif);

			source_type_member_definition.append (fun);
		}
		
Jürg Billeter's avatar
Jürg Billeter committed
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
		CCodeComment comment = null;
		if (source_file.comment != null) {
			comment = new CCodeComment (source_file.comment);
		}

		var writer = new CCodeWriter (source_file.get_cheader_filename ());
		if (comment != null) {
			comment.write (writer);
		}
		writer.write_newline ();
		var once = new CCodeOnceSection (header_define);
		once.append (new CCodeNewline ());
		once.append (header_begin);
		once.append (new CCodeNewline ());
		once.append (new CCodeIdentifier ("G_BEGIN_DECLS"));
		once.append (new CCodeNewline ());
		once.append (new CCodeNewline ());
		once.append (header_type_declaration);
		once.append (new CCodeNewline ());
		once.append (header_type_definition);
		once.append (new CCodeNewline ());
		once.append (header_type_member_declaration);
		once.append (new CCodeNewline ());
		once.append (new CCodeIdentifier ("G_END_DECLS"));
		once.append (new CCodeNewline ());
		once.append (new CCodeNewline ());
		once.write (writer);
		writer.close ();
		
		writer = new CCodeWriter (source_file.get_csource_filename ());
		if (comment != null) {
			comment.write (writer);
		}
		source_begin.write (writer);
		writer.write_newline ();
		source_include_directives.write (writer);
		writer.write_newline ();
191
		source_type_member_declaration.write_declaration (writer);
Jürg Billeter's avatar
Jürg Billeter committed
192
193
		source_type_member_declaration.write (writer);
		writer.write_newline ();
194
		source_signal_marshaller_declaration.write_declaration (writer);
Jürg Billeter's avatar
Jürg Billeter committed
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
		source_signal_marshaller_declaration.write (writer);
		writer.write_newline ();
		source_type_member_definition.write (writer);
		writer.write_newline ();
		source_signal_marshaller_definition.write (writer);
		writer.write_newline ();
		writer.close ();

		header_begin = null;
		header_type_declaration = null;
		header_type_definition = null;
		header_type_member_declaration = null;
		source_begin = null;
		source_include_directives = null;
		source_type_member_declaration = null;
		source_type_member_definition = null;
		source_signal_marshaller_definition = null;
		source_signal_marshaller_declaration = null;
	}
214
	
215
	private static string get_define_for_filename (string! filename) {
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
		var define = new String ("__");
		
		var i = filename;
		while (i.len () > 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;
	}
Jürg Billeter's avatar
Jürg Billeter committed
234
}