summaryrefslogtreecommitdiff
path: root/codegen/valaccodestructmodule.vala
blob: c2882047da4227f03f8b4bc5ffdc9f6ee205f1b4 (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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/* valaccodestructmodule.vala
 *
 * Copyright (C) 2006-2009  Jürg Billeter
 * Copyright (C) 2006-2008  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.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>
 *	Raffaele Sandrini <raffaele@sandrini.ch>
 */

using GLib;

public abstract class Vala.CCodeStructModule : CCodeBaseModule {
	public override void generate_struct_declaration (Struct st, CCodeFile decl_space) {
		if (add_symbol_declaration (decl_space, st, get_ccode_name (st))) {
			return;
		}

		if (st.is_boolean_type () || st.is_integer_type () || st.is_floating_type ()) {
			// See GTypeModule.visit_struct()
			if (st.base_struct != null) {
				generate_struct_declaration (st.base_struct, decl_space);
				decl_space.add_type_declaration (new CCodeTypeDefinition (get_ccode_name (st.base_struct), new CCodeVariableDeclarator (get_ccode_name (st))));
			} else {
				string typename = null;
				if (st.is_boolean_type ()) {
					// typedef for boolean types
					decl_space.add_include ("stdbool.h");
					typename = "bool";
				} else if (st.is_integer_type ()) {
					// typedef for integral types
					decl_space.add_include ("stdint.h");
					typename = "%sint%d_t".printf (st.signed ? "" : "u", st.width);
				} else if (st.is_floating_type ()) {
					// typedef for floating types
					typename = (st.width == 64 ? "double" : "float");
				}
				decl_space.add_type_declaration (new CCodeTypeDefinition (typename, new CCodeVariableDeclarator (get_ccode_name (st))));
			}
			return;
		}

		if (context.profile == Profile.GOBJECT) {
			if (get_ccode_has_type_id (st)) {
				decl_space.add_include ("glib-object.h");
				decl_space.add_type_declaration (new CCodeNewline ());
				var macro = "(%s_get_type ())".printf (get_ccode_lower_case_name (st, null));
				decl_space.add_type_declaration (new CCodeMacroReplacement (get_ccode_type_id (st), macro));

				var type_fun = new StructRegisterFunction (st);
				type_fun.init_from_type (context, false, true);
				decl_space.add_type_member_declaration (type_fun.get_declaration ());
			}
		}

		if (st.base_struct == null) {
			decl_space.add_type_declaration (new CCodeTypeDefinition ("struct _%s".printf (get_ccode_name (st)), new CCodeVariableDeclarator (get_ccode_name (st))));
		} else {
			decl_space.add_type_declaration (new CCodeTypeDefinition (get_ccode_name (st.base_struct), new CCodeVariableDeclarator (get_ccode_name (st))));
		}

		var instance_struct = new CCodeStruct ("_%s".printf (get_ccode_name (st)));
		instance_struct.modifiers |= (st.version.deprecated ? CCodeModifiers.DEPRECATED : 0);

		foreach (Field f in st.get_fields ()) {
			if (f.binding == MemberBinding.INSTANCE)  {
				append_field (instance_struct, f, decl_space);
			}
		}

		if (st.base_struct == null) {
			decl_space.add_type_definition (instance_struct);
		}

		var function = new CCodeFunction (get_ccode_dup_function (st), get_ccode_name (st) + "*");
		if (st.is_private_symbol ()) {
			function.modifiers = CCodeModifiers.STATIC;
		} else if (context.hide_internal && st.is_internal_symbol ()) {
			function.modifiers = CCodeModifiers.INTERNAL;
		}
		function.add_parameter (new CCodeParameter ("self", "const " + get_ccode_name (st) + "*"));
		decl_space.add_function_declaration (function);

		function = new CCodeFunction (get_ccode_free_function (st), "void");
		if (st.is_private_symbol ()) {
			function.modifiers = CCodeModifiers.STATIC;
		} else if (context.hide_internal && st.is_internal_symbol ()) {
			function.modifiers = CCodeModifiers.INTERNAL;
		}
		function.add_parameter (new CCodeParameter ("self", get_ccode_name (st) + "*"));
		decl_space.add_function_declaration (function);

		if (st.is_disposable ()) {
			function = new CCodeFunction (get_ccode_copy_function (st), "void");
			if (st.is_private_symbol ()) {
				function.modifiers = CCodeModifiers.STATIC;
			} else if (context.hide_internal && st.is_internal_symbol ()) {
				function.modifiers = CCodeModifiers.INTERNAL;
			}
			function.add_parameter (new CCodeParameter ("self", "const " + get_ccode_name (st) + "*"));
			function.add_parameter (new CCodeParameter ("dest", get_ccode_name (st) + "*"));
			decl_space.add_function_declaration (function);

			function = new CCodeFunction (get_ccode_destroy_function (st), "void");
			if (st.is_private_symbol ()) {
				function.modifiers = CCodeModifiers.STATIC;
			} else if (context.hide_internal && st.is_internal_symbol ()) {
				function.modifiers = CCodeModifiers.INTERNAL;
			}
			function.add_parameter (new CCodeParameter ("self", get_ccode_name (st) + "*"));
			decl_space.add_function_declaration (function);
		}
	}

	public override void visit_struct (Struct st) {
		push_context (new EmitContext (st));
		push_line (st.source_reference);

		if (get_ccode_has_type_id (st) && get_ccode_name (st).length < 3) {
			st.error = true;
			Report.error (st.source_reference, "Name `%s' is too short for struct using GType".printf (get_ccode_name (st)));
			return;
		}

		var old_instance_finalize_context = instance_finalize_context;
		instance_finalize_context = new EmitContext ();

		generate_struct_declaration (st, cfile);

		if (!st.is_internal_symbol ()) {
			generate_struct_declaration (st, header_file);
		}
		if (!st.is_private_symbol ()) {
			generate_struct_declaration (st, internal_header_file);
		}

		if (!st.is_boolean_type () && !st.is_integer_type () && !st.is_floating_type ()) {
			if (st.is_disposable ()) {
				begin_struct_destroy_function (st);
			}
		}

		st.accept_children (this);

		if (!st.is_boolean_type () && !st.is_integer_type () && !st.is_floating_type ()) {
			if (st.is_disposable ()) {
				add_struct_copy_function (st);
				add_struct_destroy_function (st);
			}

			add_struct_dup_function (st);
			add_struct_free_function (st);
		}

		instance_finalize_context = old_instance_finalize_context;

		pop_line ();
		pop_context ();
	}

	void add_struct_dup_function (Struct st) {
		var function = new CCodeFunction (get_ccode_dup_function (st), get_ccode_name (st) + "*");
		if (st.access == SymbolAccessibility.PRIVATE) {
			function.modifiers = CCodeModifiers.STATIC;
		}

		function.add_parameter (new CCodeParameter ("self", "const " + get_ccode_name (st) + "*"));

		push_function (function);

		ccode.add_declaration (get_ccode_name (st) + "*", new CCodeVariableDeclarator ("dup"));

		if (context.profile == Profile.GOBJECT) {
			// g_new0 needs glib.h
			cfile.add_include ("glib.h");
			var creation_call = new CCodeFunctionCall (new CCodeIdentifier ("g_new0"));
			creation_call.add_argument (new CCodeConstant (get_ccode_name (st)));
			creation_call.add_argument (new CCodeConstant ("1"));
			ccode.add_assignment (new CCodeIdentifier ("dup"), creation_call);
		} else if (context.profile == Profile.POSIX) {
			// calloc needs stdlib.h
			cfile.add_include ("stdlib.h");
			var creation_call = new CCodeFunctionCall (new CCodeIdentifier ("calloc"));
			creation_call.add_argument (new CCodeConstant ("1"));
			creation_call.add_argument (new CCodeIdentifier ("sizeof (%s*)".printf (get_ccode_name (st))));
			ccode.add_assignment (new CCodeIdentifier ("dup"), creation_call);
		}

		if (st.is_disposable ()) {
			var copy_call = new CCodeFunctionCall (new CCodeIdentifier (get_ccode_copy_function (st)));
			copy_call.add_argument (new CCodeIdentifier ("self"));
			copy_call.add_argument (new CCodeIdentifier ("dup"));
			ccode.add_expression (copy_call);
		} else {
			cfile.add_include ("string.h");

			var sizeof_call = new CCodeFunctionCall (new CCodeIdentifier ("sizeof"));
			sizeof_call.add_argument (new CCodeConstant (get_ccode_name (st)));

			var copy_call = new CCodeFunctionCall (new CCodeIdentifier ("memcpy"));
			copy_call.add_argument (new CCodeIdentifier ("dup"));
			copy_call.add_argument (new CCodeIdentifier ("self"));
			copy_call.add_argument (sizeof_call);
			ccode.add_expression (copy_call);
		}

		ccode.add_return (new CCodeIdentifier ("dup"));

		pop_function ();

		cfile.add_function (function);
	}

	void add_struct_free_function (Struct st) {
		var function = new CCodeFunction (get_ccode_free_function (st), "void");
		if (st.is_private_symbol ()) {
			function.modifiers = CCodeModifiers.STATIC;
		} else if (context.hide_internal && st.is_internal_symbol ()) {
			function.modifiers = CCodeModifiers.INTERNAL;
		}

		function.add_parameter (new CCodeParameter ("self", get_ccode_name (st) + "*"));

		push_function (function);

		if (st.is_disposable ()) {
			var destroy_call = new CCodeFunctionCall (new CCodeIdentifier (get_ccode_destroy_function (st)));
			destroy_call.add_argument (new CCodeIdentifier ("self"));
			ccode.add_expression (destroy_call);
		}

		if (context.profile == Profile.GOBJECT) {
			// g_free needs glib.h
			cfile.add_include ("glib.h");
			var free_call = new CCodeFunctionCall (new CCodeIdentifier ("g_free"));
			free_call.add_argument (new CCodeIdentifier ("self"));
			ccode.add_expression (free_call);
		} else if (context.profile == Profile.POSIX) {
			// free needs stdlib.h
			cfile.add_include ("stdlib.h");
			var free_call = new CCodeFunctionCall (new CCodeIdentifier ("free"));
			free_call.add_argument (new CCodeIdentifier ("self"));
			ccode.add_expression (free_call);
		}

		pop_function ();

		cfile.add_function (function);
	}

	void add_struct_copy_function (Struct st) {
		var function = new CCodeFunction (get_ccode_copy_function (st), "void");
		if (st.is_private_symbol ()) {
			function.modifiers = CCodeModifiers.STATIC;
		} else if (context.hide_internal && st.is_internal_symbol ()) {
			function.modifiers = CCodeModifiers.INTERNAL;
		}

		function.add_parameter (new CCodeParameter ("self", "const " + get_ccode_name (st) + "*"));
		function.add_parameter (new CCodeParameter ("dest", get_ccode_name (st) + "*"));

		push_function (function);

		var dest_struct = new GLibValue (SemanticAnalyzer.get_data_type_for_symbol (st), new CCodeIdentifier ("(*dest)"), true);
		unowned Struct sym = st;
		while (sym.base_struct != null) {
			sym = sym.base_struct;
		}
		foreach (var f in sym.get_fields ()) {
			if (f.binding == MemberBinding.INSTANCE) {
				var value = load_field (f, load_this_parameter ((TypeSymbol) st));
				if ((!(f.variable_type is DelegateType) || get_ccode_delegate_target (f)) && requires_copy (f.variable_type))  {
					value = copy_value (value, f);
					if (value == null) {
						// error case, continue to avoid critical
						continue;
					}
				}
				store_field (f, dest_struct, value);
			}
		}

		pop_function ();

		cfile.add_function (function);
	}

	void begin_struct_destroy_function (Struct st) {
		push_context (instance_finalize_context);

		var function = new CCodeFunction (get_ccode_destroy_function (st), "void");
		if (st.is_private_symbol ()) {
			function.modifiers = CCodeModifiers.STATIC;
		} else if (context.hide_internal && st.is_internal_symbol ()) {
			function.modifiers = CCodeModifiers.INTERNAL;
		}

		function.add_parameter (new CCodeParameter ("self", get_ccode_name (st) + "*"));

		push_function (function);

		pop_context ();
	}

	void add_struct_destroy_function (Struct st) {
		cfile.add_function (instance_finalize_context.ccode);
	}
}