1 /* valagtypemodule.vala
3 * Copyright (C) 2006-2010 Jürg Billeter
4 * Copyright (C) 2006-2008 Raffaele Sandrini
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 * Jürg Billeter <j@bitron.ch>
22 * Raffaele Sandrini <raffaele@sandrini.ch>
26 public class Vala
.GTypeModule
: GErrorModule
{
27 public override CCodeParameter
generate_parameter (Parameter param
, CCodeFile decl_space
, Map
<int,CCodeParameter
> cparam_map
, Map
<int,CCodeExpression
>? carg_map
) {
28 if (!(param
.variable_type is ObjectType
)) {
29 return base.generate_parameter (param
, decl_space
, cparam_map
, carg_map
);
32 generate_type_declaration (param
.variable_type
, decl_space
);
34 string ctypename
= get_ccode_name (param
.variable_type
);
36 if (param
.direction
!= ParameterDirection
.IN
) {
37 ctypename
= "%s *".printf (ctypename
);
40 var cparam
= new
CCodeParameter (get_variable_cname (param
.name
), ctypename
);
41 if (param
.format_arg
) {
42 cparam
.modifiers
= CCodeModifiers
.FORMAT_ARG
;
45 cparam_map
.set (get_param_pos (get_ccode_pos (param
)), cparam
);
46 if (carg_map
!= null) {
47 carg_map
.set (get_param_pos (get_ccode_pos (param
)), get_variable_cexpression (param
.name
));
53 public override void generate_class_declaration (Class cl
, CCodeFile decl_space
) {
54 if (add_symbol_declaration (decl_space
, cl
, get_ccode_name (cl
))) {
58 if (cl
.base_class
!= null) {
59 // base class declaration
60 // necessary for ref and unref function declarations
61 generate_class_declaration (cl
.base_class
, decl_space
);
64 bool is_gtypeinstance
= !cl
.is_compact
;
65 bool is_fundamental
= is_gtypeinstance
&& cl
.base_class
== null;
66 bool is_gsource
= cl
.base_class
== gsource_type
;
68 if (is_gtypeinstance
) {
69 decl_space
.add_type_declaration (new
CCodeNewline ());
70 var macro
= "(%s_get_type ())".printf (get_ccode_lower_case_name (cl
, null));
71 decl_space
.add_type_declaration (new
CCodeMacroReplacement (get_ccode_type_id (cl
), macro
));
73 macro
= "(G_TYPE_CHECK_INSTANCE_CAST ((obj), %s, %s))".printf (get_ccode_type_id (cl
), get_ccode_name (cl
));
74 decl_space
.add_type_declaration (new
CCodeMacroReplacement ("%s(obj)".printf (get_ccode_upper_case_name (cl
, null)), macro
));
76 macro
= "(G_TYPE_CHECK_CLASS_CAST ((klass), %s, %sClass))".printf (get_ccode_type_id (cl
), get_ccode_name (cl
));
77 decl_space
.add_type_declaration (new
CCodeMacroReplacement ("%s_CLASS(klass)".printf (get_ccode_upper_case_name (cl
, null)), macro
));
79 macro
= "(G_TYPE_CHECK_INSTANCE_TYPE ((obj), %s))".printf (get_ccode_type_id (cl
));
80 decl_space
.add_type_declaration (new
CCodeMacroReplacement ("%s(obj)".printf (get_ccode_type_check_function (cl
)), macro
));
82 macro
= "(G_TYPE_CHECK_CLASS_TYPE ((klass), %s))".printf (get_ccode_type_id (cl
));
83 decl_space
.add_type_declaration (new
CCodeMacroReplacement ("%s_CLASS(klass)".printf (get_ccode_type_check_function (cl
)), macro
));
85 macro
= "(G_TYPE_INSTANCE_GET_CLASS ((obj), %s, %sClass))".printf (get_ccode_type_id (cl
), get_ccode_name (cl
));
86 decl_space
.add_type_declaration (new
CCodeMacroReplacement ("%s_GET_CLASS(obj)".printf (get_ccode_upper_case_name (cl
, null)), macro
));
87 decl_space
.add_type_declaration (new
CCodeNewline ());
90 if (cl
.is_compact
&& cl
.base_class
!= null && !is_gsource
) {
91 decl_space
.add_type_declaration (new
CCodeTypeDefinition (get_ccode_name (cl
.base_class
), new
CCodeVariableDeclarator (get_ccode_name (cl
))));
93 decl_space
.add_type_declaration (new
CCodeTypeDefinition ("struct _%s".printf (get_ccode_name (cl
)), new
CCodeVariableDeclarator (get_ccode_name (cl
))));
97 var ref_fun
= new
CCodeFunction ("%sref".printf (get_ccode_lower_case_prefix (cl
)), "gpointer");
98 var unref_fun
= new
CCodeFunction ("%sunref".printf (get_ccode_lower_case_prefix (cl
)), "void");
99 if (cl
.is_private_symbol ()) {
100 ref_fun
.modifiers
= CCodeModifiers
.STATIC
;
101 unref_fun
.modifiers
= CCodeModifiers
.STATIC
;
102 } else if (context
.hide_internal
&& cl
.is_internal_symbol ()) {
103 ref_fun
.modifiers
= CCodeModifiers
.INTERNAL
;
104 unref_fun
.modifiers
= CCodeModifiers
.INTERNAL
;
107 ref_fun
.add_parameter (new
CCodeParameter ("instance", "gpointer"));
108 unref_fun
.add_parameter (new
CCodeParameter ("instance", "gpointer"));
110 decl_space
.add_function_declaration (ref_fun
);
111 decl_space
.add_function_declaration (unref_fun
);
113 // GParamSpec and GValue functions
114 string function_name
= get_ccode_lower_case_name (cl
, "param_spec_");
116 var function
= new
CCodeFunction (function_name
, "GParamSpec*");
117 function
.add_parameter (new
CCodeParameter ("name", "const gchar*"));
118 function
.add_parameter (new
CCodeParameter ("nick", "const gchar*"));
119 function
.add_parameter (new
CCodeParameter ("blurb", "const gchar*"));
120 function
.add_parameter (new
CCodeParameter ("object_type", "GType"));
121 function
.add_parameter (new
CCodeParameter ("flags", "GParamFlags"));
123 if (cl
.is_private_symbol ()) {
124 // avoid C warning as this function is not always used
125 function
.modifiers
= CCodeModifiers
.STATIC
| CCodeModifiers
.UNUSED
;
126 } else if (context
.hide_internal
&& cl
.is_internal_symbol ()) {
127 function
.modifiers
= CCodeModifiers
.INTERNAL
;
130 decl_space
.add_function_declaration (function
);
132 function
= new
CCodeFunction (get_ccode_set_value_function (cl
), "void");
133 function
.add_parameter (new
CCodeParameter ("value", "GValue*"));
134 function
.add_parameter (new
CCodeParameter ("v_object", "gpointer"));
136 if (cl
.is_private_symbol ()) {
137 // avoid C warning as this function is not always used
138 function
.modifiers
= CCodeModifiers
.STATIC
| CCodeModifiers
.UNUSED
;
139 } else if (context
.hide_internal
&& cl
.is_internal_symbol ()) {
140 // avoid C warning as this function is not always used
141 function
.modifiers
= CCodeModifiers
.INTERNAL
| CCodeModifiers
.UNUSED
;
144 decl_space
.add_function_declaration (function
);
146 function
= new
CCodeFunction (get_ccode_take_value_function (cl
), "void");
147 function
.add_parameter (new
CCodeParameter ("value", "GValue*"));
148 function
.add_parameter (new
CCodeParameter ("v_object", "gpointer"));
150 if (cl
.is_private_symbol ()) {
151 // avoid C warning as this function is not always used
152 function
.modifiers
= CCodeModifiers
.STATIC
| CCodeModifiers
.UNUSED
;
153 } else if (context
.hide_internal
&& cl
.is_internal_symbol ()) {
154 function
.modifiers
= CCodeModifiers
.INTERNAL
;
157 decl_space
.add_function_declaration (function
);
159 function
= new
CCodeFunction (get_ccode_get_value_function (cl
), "gpointer");
160 function
.add_parameter (new
CCodeParameter ("value", "const GValue*"));
162 if (cl
.is_private_symbol ()) {
163 // avoid C warning as this function is not always used
164 function
.modifiers
= CCodeModifiers
.STATIC
| CCodeModifiers
.UNUSED
;
165 } else if (context
.hide_internal
&& cl
.is_internal_symbol ()) {
166 // avoid C warning as this function is not always used
167 function
.modifiers
= CCodeModifiers
.INTERNAL
| CCodeModifiers
.UNUSED
;
170 decl_space
.add_function_declaration (function
);
171 } else if (!is_gtypeinstance
&& !is_gsource
) {
172 if (cl
.base_class
== null) {
173 var function
= new
CCodeFunction ("%sfree".printf (get_ccode_lower_case_prefix (cl
)), "void");
174 if (cl
.is_private_symbol ()) {
175 function
.modifiers
= CCodeModifiers
.STATIC
;
176 } else if (context
.hide_internal
&& cl
.is_internal_symbol ()) {
177 function
.modifiers
= CCodeModifiers
.INTERNAL
;
180 function
.add_parameter (new
CCodeParameter ("self", "%s *".printf (get_ccode_name (cl
))));
182 decl_space
.add_function_declaration (function
);
186 if (is_gtypeinstance
) {
187 decl_space
.add_type_declaration (new
CCodeTypeDefinition ("struct _%sClass".printf (get_ccode_name (cl
)), new
CCodeVariableDeclarator ("%sClass".printf (get_ccode_name (cl
)))));
189 var type_fun
= new
ClassRegisterFunction (cl
, context
);
190 type_fun
.init_from_type (in_plugin
, true);
191 decl_space
.add_type_member_declaration (type_fun
.get_declaration ());
195 public override void generate_class_struct_declaration (Class cl
, CCodeFile decl_space
) {
196 if (add_symbol_declaration (decl_space
, cl
, "struct _%s".printf (get_ccode_name (cl
)))) {
200 if (cl
.base_class
!= null) {
201 // base class declaration
202 generate_class_struct_declaration (cl
.base_class
, decl_space
);
204 foreach (DataType base_type
in cl
.get_base_types ()) {
205 var iface
= base_type
.data_type as Interface
;
207 generate_interface_declaration (iface
, decl_space
);
211 generate_class_declaration (cl
, decl_space
);
213 bool is_gtypeinstance
= !cl
.is_compact
;
214 bool is_fundamental
= is_gtypeinstance
&& cl
.base_class
== null;
215 bool is_gsource
= cl
.base_class
== gsource_type
;
217 var instance_struct
= new
CCodeStruct ("_%s".printf (get_ccode_name (cl
)));
218 var type_struct
= new
CCodeStruct ("_%sClass".printf (get_ccode_name (cl
)));
220 if (cl
.base_class
!= null) {
221 instance_struct
.add_field (get_ccode_name (cl
.base_class
), "parent_instance");
222 } else if (is_fundamental
) {
223 decl_space
.add_include ("glib-object.h");
224 instance_struct
.add_field ("GTypeInstance", "parent_instance");
225 instance_struct
.add_field ("volatile int", "ref_count");
228 if (cl
.is_compact
&& cl
.base_class
== null && cl
.get_fields ().size
== 0) {
229 // add dummy member, C doesn't allow empty structs
230 instance_struct
.add_field ("int", "dummy");
233 if (is_gtypeinstance
) {
234 decl_space
.add_type_declaration (new
CCodeTypeDefinition ("struct %sPrivate".printf (instance_struct
.name
), new
CCodeVariableDeclarator ("%sPrivate".printf (get_ccode_name (cl
)))));
236 instance_struct
.add_field ("%sPrivate *".printf (get_ccode_name (cl
)), "priv");
237 if (is_fundamental
) {
238 type_struct
.add_field ("GTypeClass", "parent_class");
240 type_struct
.add_field ("%sClass".printf (get_ccode_name (cl
.base_class
)), "parent_class");
243 if (is_fundamental
) {
244 type_struct
.add_field ("void", "(*finalize) (%s *self)".printf (get_ccode_name (cl
)));
248 foreach (Method m
in cl
.get_methods ()) {
249 generate_virtual_method_declaration (m
, decl_space
, type_struct
);
252 foreach (Signal sig
in cl
.get_signals ()) {
253 if (sig
.default_handler
!= null) {
254 generate_virtual_method_declaration (sig
.default_handler
, decl_space
, type_struct
);
258 foreach (Property prop
in cl
.get_properties ()) {
259 if (!prop
.is_abstract
&& !prop
.is_virtual
) {
262 generate_type_declaration (prop
.property_type
, decl_space
);
264 var t
= (ObjectTypeSymbol
) prop
.parent_symbol
;
266 var this_type
= new
ObjectType (t
);
267 var cselfparam
= new
CCodeParameter ("self", get_ccode_name (this_type
));
269 if (prop
.get_accessor
!= null) {
270 var vdeclarator
= new
CCodeFunctionDeclarator ("get_%s".printf (prop
.name
));
271 vdeclarator
.add_parameter (cselfparam
);
273 if (prop
.property_type
.is_real_non_null_struct_type ()) {
274 var cvalueparam
= new
CCodeParameter ("result", "%s *".printf (get_ccode_name (prop
.get_accessor
.value_type
)));
275 vdeclarator
.add_parameter (cvalueparam
);
276 creturn_type
= "void";
278 creturn_type
= get_ccode_name (prop
.get_accessor
.value_type
);
281 var array_type
= prop
.property_type as ArrayType
;
282 if (array_type
!= null) {
283 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
284 vdeclarator
.add_parameter (new
CCodeParameter (get_array_length_cname ("result", dim
), "int*"));
286 } else if ((prop
.property_type is DelegateType
) && ((DelegateType
) prop
.property_type
).delegate_symbol
.has_target
) {
287 vdeclarator
.add_parameter (new
CCodeParameter (get_delegate_target_cname ("result"), "gpointer*"));
290 var vdecl
= new
CCodeDeclaration (creturn_type
);
291 vdecl
.add_declarator (vdeclarator
);
292 type_struct
.add_declaration (vdecl
);
294 if (prop
.set_accessor
!= null) {
295 CCodeParameter cvalueparam
;
296 if (prop
.property_type
.is_real_non_null_struct_type ()) {
297 cvalueparam
= new
CCodeParameter ("value", "%s *".printf (get_ccode_name (prop
.set_accessor
.value_type
)));
299 cvalueparam
= new
CCodeParameter ("value", get_ccode_name (prop
.set_accessor
.value_type
));
302 var vdeclarator
= new
CCodeFunctionDeclarator ("set_%s".printf (prop
.name
));
303 vdeclarator
.add_parameter (cselfparam
);
304 vdeclarator
.add_parameter (cvalueparam
);
306 var array_type
= prop
.property_type as ArrayType
;
307 if (array_type
!= null) {
308 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
309 vdeclarator
.add_parameter (new
CCodeParameter (get_array_length_cname ("value", dim
), "int"));
311 } else if ((prop
.property_type is DelegateType
) && ((DelegateType
) prop
.property_type
).delegate_symbol
.has_target
) {
312 vdeclarator
.add_parameter (new
CCodeParameter (get_delegate_target_cname ("value"), "gpointer"));
315 var vdecl
= new
CCodeDeclaration ("void");
316 vdecl
.add_declarator (vdeclarator
);
317 type_struct
.add_declaration (vdecl
);
321 foreach (Field f
in cl
.get_fields ()) {
322 if (f
.access
!= SymbolAccessibility
.PRIVATE
) {
323 CCodeModifiers modifiers
= (f
.is_volatile ? CCodeModifiers
.VOLATILE
: 0) | (f
.version
.deprecated ? CCodeModifiers
.DEPRECATED
: 0);
324 if (f
.binding
== MemberBinding
.INSTANCE
) {
325 generate_type_declaration (f
.variable_type
, decl_space
);
327 instance_struct
.add_field (get_ccode_name (f
.variable_type
), get_ccode_name (f
), modifiers
, get_ccode_declarator_suffix (f
.variable_type
));
328 if (f
.variable_type is ArrayType
&& get_ccode_array_length (f
)) {
329 // create fields to store array dimensions
330 var array_type
= (ArrayType
) f
.variable_type
;
332 if (!array_type
.fixed_length
) {
333 var len_type
= int_type
.copy ();
335 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
337 if (get_ccode_array_length_name (f
) != null) {
338 length_cname
= get_ccode_array_length_name (f
);
340 length_cname
= get_array_length_cname (get_ccode_name (f
), dim
);
342 instance_struct
.add_field (get_ccode_name (len_type
), length_cname
);
345 if (array_type
.rank
== 1 && f
.is_internal_symbol ()) {
346 instance_struct
.add_field (get_ccode_name (len_type
), get_array_size_cname (get_ccode_name (f
)));
349 } else if (f
.variable_type is DelegateType
) {
350 var delegate_type
= (DelegateType
) f
.variable_type
;
351 if (delegate_type
.delegate_symbol
.has_target
) {
352 // create field to store delegate target
353 instance_struct
.add_field ("gpointer", get_ccode_delegate_target_name (f
));
354 if (delegate_type
.is_disposable ()) {
355 instance_struct
.add_field ("GDestroyNotify", get_delegate_target_destroy_notify_cname (get_ccode_name (f
)));
359 } else if (f
.binding
== MemberBinding
.CLASS
) {
360 type_struct
.add_field (get_ccode_name (f
.variable_type
), get_ccode_name (f
), modifiers
);
365 if (!cl
.is_compact
|| cl
.base_class
== null || is_gsource
) {
366 // derived compact classes do not have a struct
367 decl_space
.add_type_definition (instance_struct
);
370 if (is_gtypeinstance
) {
371 decl_space
.add_type_definition (type_struct
);
375 public virtual void generate_virtual_method_declaration (Method m
, CCodeFile decl_space
, CCodeStruct type_struct
) {
376 if (!m
.is_abstract
&& !m
.is_virtual
) {
380 var creturn_type
= m
.return_type
;
381 if (m
.return_type
.is_real_non_null_struct_type ()) {
382 // structs are returned via out parameter
383 creturn_type
= new
VoidType ();
386 // add vfunc field to the type struct
387 var vdeclarator
= new
CCodeFunctionDeclarator (get_ccode_vfunc_name (m
));
388 var cparam_map
= new HashMap
<int,CCodeParameter
> (direct_hash
, direct_equal
);
390 if (m
.printf_format
) {
391 vdeclarator
.modifiers
|= CCodeModifiers
.PRINTF
;
392 } else if (m
.scanf_format
) {
393 vdeclarator
.modifiers
|= CCodeModifiers
.SCANF
;
396 if (m
.version
.deprecated
) {
397 vdeclarator
.modifiers
|= CCodeModifiers
.DEPRECATED
;
400 generate_cparameters (m
, decl_space
, cparam_map
, new
CCodeFunction ("fake"), vdeclarator
);
402 var vdecl
= new
CCodeDeclaration (get_ccode_name (creturn_type
));
403 vdecl
.add_declarator (vdeclarator
);
404 type_struct
.add_declaration (vdecl
);
407 void generate_class_private_declaration (Class cl
, CCodeFile decl_space
) {
408 if (decl_space
.add_declaration ("%sPrivate".printf (get_ccode_name (cl
)))) {
412 bool is_gtypeinstance
= !cl
.is_compact
;
413 bool has_class_locks
= false;
415 var instance_priv_struct
= new
CCodeStruct ("_%sPrivate".printf (get_ccode_name (cl
)));
416 var type_priv_struct
= new
CCodeStruct ("_%sClassPrivate".printf (get_ccode_name (cl
)));
418 if (is_gtypeinstance
) {
419 /* create type, dup_func, and destroy_func fields for generic types */
420 foreach (TypeParameter type_param
in cl
.get_type_parameters ()) {
423 func_name
= "%s_type".printf (type_param
.name
.down ());
424 instance_priv_struct
.add_field ("GType", func_name
);
426 func_name
= "%s_dup_func".printf (type_param
.name
.down ());
427 instance_priv_struct
.add_field ("GBoxedCopyFunc", func_name
);
429 func_name
= "%s_destroy_func".printf (type_param
.name
.down ());
430 instance_priv_struct
.add_field ("GDestroyNotify", func_name
);
434 foreach (Field f
in cl
.get_fields ()) {
435 CCodeModifiers modifiers
= (f
.is_volatile ? CCodeModifiers
.VOLATILE
: 0) | (f
.version
.deprecated ? CCodeModifiers
.DEPRECATED
: 0);
436 if (f
.binding
== MemberBinding
.INSTANCE
) {
437 if (f
.access
== SymbolAccessibility
.PRIVATE
) {
438 generate_type_declaration (f
.variable_type
, decl_space
);
440 instance_priv_struct
.add_field (get_ccode_name (f
.variable_type
), get_ccode_name (f
), modifiers
, get_ccode_declarator_suffix (f
.variable_type
));
441 if (f
.variable_type is ArrayType
&& get_ccode_array_length (f
)) {
442 // create fields to store array dimensions
443 var array_type
= (ArrayType
) f
.variable_type
;
444 var len_type
= int_type
.copy ();
446 if (!array_type
.fixed_length
) {
447 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
449 if (get_ccode_array_length_name (f
) != null) {
450 length_cname
= get_ccode_array_length_name (f
);
452 length_cname
= get_array_length_cname (get_ccode_name (f
), dim
);
454 instance_priv_struct
.add_field (get_ccode_name (len_type
), length_cname
);
457 if (array_type
.rank
== 1 && f
.is_internal_symbol ()) {
458 instance_priv_struct
.add_field (get_ccode_name (len_type
), get_array_size_cname (get_ccode_name (f
)));
461 } else if (f
.variable_type is DelegateType
) {
462 var delegate_type
= (DelegateType
) f
.variable_type
;
463 if (delegate_type
.delegate_symbol
.has_target
) {
464 // create field to store delegate target
465 instance_priv_struct
.add_field ("gpointer", get_ccode_delegate_target_name (f
));
466 if (delegate_type
.is_disposable ()) {
467 instance_priv_struct
.add_field ("GDestroyNotify", get_delegate_target_destroy_notify_cname (get_ccode_name (f
)));
473 if (f
.get_lock_used ()) {
474 cl
.has_private_fields
= true;
475 // add field for mutex
476 instance_priv_struct
.add_field (get_ccode_name (mutex_type
), get_symbol_lock_name (get_ccode_name (f
)));
478 } else if (f
.binding
== MemberBinding
.CLASS
) {
479 if (f
.access
== SymbolAccessibility
.PRIVATE
) {
480 type_priv_struct
.add_field (get_ccode_name (f
.variable_type
), get_ccode_name (f
), modifiers
);
483 if (f
.get_lock_used ()) {
484 has_class_locks
= true;
485 // add field for mutex
486 type_priv_struct
.add_field (get_ccode_name (mutex_type
), get_symbol_lock_name (get_ccode_name (f
)));
491 foreach (Property prop
in cl
.get_properties ()) {
492 if (prop
.binding
== MemberBinding
.INSTANCE
) {
493 if (prop
.get_lock_used ()) {
494 cl
.has_private_fields
= true;
495 // add field for mutex
496 instance_priv_struct
.add_field (get_ccode_name (mutex_type
), get_symbol_lock_name (prop
.name
));
498 } else if (prop
.binding
== MemberBinding
.CLASS
) {
499 if (prop
.get_lock_used ()) {
500 has_class_locks
= true;
501 // add field for mutex
502 type_priv_struct
.add_field (get_ccode_name (mutex_type
), get_symbol_lock_name (prop
.name
));
507 if (is_gtypeinstance
) {
508 if (cl
.has_class_private_fields
|| has_class_locks
) {
509 decl_space
.add_type_declaration (new
CCodeTypeDefinition ("struct %s".printf (type_priv_struct
.name
), new
CCodeVariableDeclarator ("%sClassPrivate".printf (get_ccode_name (cl
)))));
512 /* only add the *Private struct if it is not empty, i.e. we actually have private data */
513 if (cl
.has_private_fields
|| cl
.get_type_parameters ().size
> 0) {
514 decl_space
.add_type_definition (instance_priv_struct
);
515 var macro
= "(G_TYPE_INSTANCE_GET_PRIVATE ((o), %s, %sPrivate))".printf (get_ccode_type_id (cl
), get_ccode_name (cl
));
516 decl_space
.add_type_member_declaration (new
CCodeMacroReplacement ("%s_GET_PRIVATE(o)".printf (get_ccode_upper_case_name (cl
, null)), macro
));
519 if (cl
.has_class_private_fields
|| has_class_locks
) {
520 decl_space
.add_type_member_declaration (type_priv_struct
);
522 string macro
= "(G_TYPE_CLASS_GET_PRIVATE (klass, %s, %sClassPrivate))".printf (get_ccode_type_id (cl
), get_ccode_name (cl
));
523 decl_space
.add_type_member_declaration (new
CCodeMacroReplacement ("%s_GET_CLASS_PRIVATE(klass)".printf (get_ccode_upper_case_name (cl
, null)), macro
));
525 decl_space
.add_type_member_declaration (prop_enum
);
527 if (cl
.has_private_fields
) {
528 Report
.error (cl
.source_reference
, "Private fields not supported in compact classes");
533 public override void visit_class (Class cl
) {
534 push_context (new
EmitContext (cl
));
535 push_line (cl
.source_reference
);
537 var old_param_spec_struct
= param_spec_struct
;
538 var old_prop_enum
= prop_enum
;
539 var old_signal_enum
= signal_enum
;
540 var old_class_init_context
= class_init_context
;
541 var old_base_init_context
= base_init_context
;
542 var old_class_finalize_context
= class_finalize_context
;
543 var old_base_finalize_context
= base_finalize_context
;
544 var old_instance_init_context
= instance_init_context
;
545 var old_instance_finalize_context
= instance_finalize_context
;
547 bool is_gtypeinstance
= !cl
.is_compact
;
548 bool is_fundamental
= is_gtypeinstance
&& cl
.base_class
== null;
550 if (get_ccode_name (cl
).length
< 3) {
552 Report
.error (cl
.source_reference
, "Class name `%s' is too short".printf (get_ccode_name (cl
)));
556 prop_enum
= new
CCodeEnum ();
557 prop_enum
.add_value (new
CCodeEnumValue ("%s_0_PROPERTY".printf (get_ccode_upper_case_name (cl
, null))));
558 signal_enum
= new
CCodeEnum ();
559 class_init_context
= new
EmitContext (cl
);
560 base_init_context
= new
EmitContext (cl
);
561 class_finalize_context
= new
EmitContext (cl
);
562 base_finalize_context
= new
EmitContext (cl
);
563 instance_init_context
= new
EmitContext (cl
);
564 instance_finalize_context
= new
EmitContext (cl
);
567 generate_class_struct_declaration (cl
, cfile
);
568 generate_class_private_declaration (cl
, cfile
);
570 if (!cl
.is_internal_symbol ()) {
571 generate_class_struct_declaration (cl
, header_file
);
573 if (!cl
.is_private_symbol ()) {
574 generate_class_struct_declaration (cl
, internal_header_file
);
577 if (is_gtypeinstance
) {
578 begin_base_init_function (cl
);
579 begin_class_init_function (cl
);
580 begin_instance_init_function (cl
);
582 begin_base_finalize_function (cl
);
583 begin_class_finalize_function (cl
);
584 begin_finalize_function (cl
);
586 if (cl
.base_class
== null || cl
.base_class
== gsource_type
) {
587 begin_instance_init_function (cl
);
588 begin_finalize_function (cl
);
592 cl
.accept_children (this
);
594 if (is_gtypeinstance
) {
595 if (is_fundamental
) {
596 param_spec_struct
= new
CCodeStruct ( "_%sParamSpec%s".printf(get_ccode_prefix (cl
.parent_symbol
), cl
.name
));
597 param_spec_struct
.add_field ("GParamSpec", "parent_instance");
598 cfile
.add_type_definition (param_spec_struct
);
600 cfile
.add_type_declaration (new
CCodeTypeDefinition ("struct %s".printf (param_spec_struct
.name
), new
CCodeVariableDeclarator ( "%sParamSpec%s".printf(get_ccode_prefix (cl
.parent_symbol
), cl
.name
))));
603 gvaluecollector_h_needed
= true;
605 add_type_value_table_init_function (cl
);
606 add_type_value_table_free_function (cl
);
607 add_type_value_table_copy_function (cl
);
608 add_type_value_table_peek_pointer_function (cl
);
609 add_type_value_table_collect_value_function (cl
);
610 add_type_value_table_lcopy_value_function (cl
);
611 add_g_param_spec_type_function (cl
);
612 add_g_value_get_function (cl
);
613 add_g_value_set_function (cl
);
614 add_g_value_take_function (cl
);
616 var ref_count
= new
CCodeAssignment (new CCodeMemberAccess
.pointer (new
CCodeIdentifier ("self"), "ref_count"), new
CCodeConstant ("1"));
617 push_context (instance_init_context
);
618 ccode
.add_expression (ref_count
);
622 if (cl
.get_signals ().size
> 0) {
623 var last_signal
= "%s_NUM_SIGNALS".printf (get_ccode_upper_case_name (cl
));
624 signal_enum
.add_value (new
CCodeEnumValue (last_signal
));
625 cfile
.add_type_declaration (signal_enum
);
627 var signal_array_decl
= new
CCodeDeclaration ("guint");
628 signal_array_decl
.modifiers
|= CCodeModifiers
.STATIC
;
629 signal_array_decl
.add_declarator (new
CCodeVariableDeclarator ("%s_signals".printf (get_ccode_lower_case_name (cl
)), new
CCodeConstant ("{0}"), new CCodeDeclaratorSuffix
.with_array (new
CCodeIdentifier (last_signal
))));
630 cfile
.add_type_declaration (signal_array_decl
);
634 if (cl
.class_constructor
!= null) {
635 add_base_init_function (cl
);
637 add_class_init_function (cl
);
639 if (cl
.class_destructor
!= null) {
640 add_base_finalize_function (cl
);
643 if (cl
.static_destructor
!= null) {
644 add_class_finalize_function (cl
);
647 foreach (DataType base_type
in cl
.get_base_types ()) {
648 if (base_type
.data_type is Interface
) {
649 add_interface_init_function (cl
, (Interface
) base_type
.data_type
);
653 add_instance_init_function (cl
);
655 if (!cl
.is_compact
&& (cl
.get_fields ().size
> 0 || cl
.destructor
!= null || cl
.is_fundamental ())) {
656 add_finalize_function (cl
);
659 if (cl
.comment
!= null) {
660 cfile
.add_type_member_definition (new
CCodeComment (cl
.comment
.content
));
663 var type_fun
= new
ClassRegisterFunction (cl
, context
);
664 type_fun
.init_from_type (in_plugin
, false);
665 cfile
.add_type_member_declaration (type_fun
.get_source_declaration ());
666 cfile
.add_type_member_definition (type_fun
.get_definition ());
668 if (is_fundamental
) {
669 var ref_count
= new CCodeMemberAccess
.pointer (new
CCodeIdentifier ("self"), "ref_count");
672 var ref_fun
= new
CCodeFunction ("%sref".printf (get_ccode_lower_case_prefix (cl
)), "gpointer");
673 ref_fun
.add_parameter (new
CCodeParameter ("instance", "gpointer"));
674 if (cl
.is_private_symbol ()) {
675 ref_fun
.modifiers
= CCodeModifiers
.STATIC
;
676 } else if (context
.hide_internal
&& cl
.is_internal_symbol ()) {
677 ref_fun
.modifiers
= CCodeModifiers
.INTERNAL
;
679 push_function (ref_fun
);
681 ccode
.add_declaration ("%s *".printf (get_ccode_name (cl
)), new
CCodeVariableDeclarator ("self", new
CCodeIdentifier ("instance")));
682 var ccall
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_atomic_int_inc"));
683 ccall
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, ref_count
));
684 ccode
.add_expression (ccall
);
685 ccode
.add_return (new
CCodeIdentifier ("instance"));
688 cfile
.add_function (ref_fun
);
691 var unref_fun
= new
CCodeFunction ("%sunref".printf (get_ccode_lower_case_prefix (cl
)), "void");
692 unref_fun
.add_parameter (new
CCodeParameter ("instance", "gpointer"));
693 if (cl
.is_private_symbol ()) {
694 unref_fun
.modifiers
= CCodeModifiers
.STATIC
;
695 } else if (context
.hide_internal
&& cl
.is_internal_symbol ()) {
696 unref_fun
.modifiers
= CCodeModifiers
.INTERNAL
;
698 push_function (unref_fun
);
700 ccode
.add_declaration ("%s *".printf (get_ccode_name (cl
)), new
CCodeVariableDeclarator ("self", new
CCodeIdentifier ("instance")));
701 ccall
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_atomic_int_dec_and_test"));
702 ccall
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, ref_count
));
703 ccode
.open_if (ccall
);
705 var get_class
= new
CCodeFunctionCall (new
CCodeIdentifier ("%s_GET_CLASS".printf (get_ccode_upper_case_name (cl
, null))));
706 get_class
.add_argument (new
CCodeIdentifier ("self"));
709 var ccast
= new
CCodeFunctionCall (new
CCodeIdentifier ("%s_GET_CLASS".printf (get_ccode_upper_case_name (cl
, null))));
710 ccast
.add_argument (new
CCodeIdentifier ("self"));
711 ccall
= new
CCodeFunctionCall (new CCodeMemberAccess
.pointer (ccast
, "finalize"));
712 ccall
.add_argument (new
CCodeIdentifier ("self"));
713 ccode
.add_expression (ccall
);
715 // free type instance
716 var free
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_type_free_instance"));
717 free
.add_argument (new
CCodeCastExpression (new
CCodeIdentifier ("self"), "GTypeInstance *"));
718 ccode
.add_expression (free
);
722 cfile
.add_function (unref_fun
);
725 if (cl
.base_class
== null || cl
.base_class
== gsource_type
) {
726 // derived compact classes do not have fields
727 add_instance_init_function (cl
);
728 add_finalize_function (cl
);
732 param_spec_struct
= old_param_spec_struct
;
733 prop_enum
= old_prop_enum
;
734 signal_enum
= old_signal_enum
;
735 class_init_context
= old_class_init_context
;
736 base_init_context
= old_base_init_context
;
737 class_finalize_context
= old_class_finalize_context
;
738 base_finalize_context
= old_base_finalize_context
;
739 instance_init_context
= old_instance_init_context
;
740 instance_finalize_context
= old_instance_finalize_context
;
746 private void add_type_value_table_init_function (Class cl
) {
747 var function
= new
CCodeFunction ("%s_init".printf (get_ccode_lower_case_name (cl
, "value_")), "void");
748 function
.add_parameter (new
CCodeParameter ("value", "GValue*"));
749 function
.modifiers
= CCodeModifiers
.STATIC
;
751 push_function (function
);
752 ccode
.add_assignment (new
CCodeMemberAccess (new CCodeMemberAccess
.pointer (new
CCodeIdentifier ("value"), "data[0]"), "v_pointer"), new
CCodeConstant ("NULL"));
754 cfile
.add_function (function
);
757 private void add_type_value_table_free_function (Class cl
) {
758 var function
= new
CCodeFunction ("%s_free_value".printf (get_ccode_lower_case_name (cl
, "value_")), "void");
759 function
.add_parameter (new
CCodeParameter ("value", "GValue*"));
760 function
.modifiers
= CCodeModifiers
.STATIC
;
762 push_function (function
);
764 var vpointer
= new
CCodeMemberAccess(new CCodeMemberAccess
.pointer (new
CCodeIdentifier ("value"), "data[0]"),"v_pointer");
765 var ccall
= new
CCodeFunctionCall (new
CCodeIdentifier ("%sunref".printf (get_ccode_lower_case_prefix (cl
))));
766 ccall
.add_argument (vpointer
);
768 ccode
.open_if (vpointer
);
769 ccode
.add_expression (ccall
);
773 cfile
.add_function (function
);
776 private void add_type_value_table_copy_function (Class cl
) {
777 var function
= new
CCodeFunction ("%s_copy_value".printf (get_ccode_lower_case_name (cl
, "value_")), "void");
778 function
.add_parameter (new
CCodeParameter ("src_value", "const GValue*"));
779 function
.add_parameter (new
CCodeParameter ("dest_value", "GValue*"));
780 function
.modifiers
= CCodeModifiers
.STATIC
;
782 push_function (function
);
784 var dest_vpointer
= new
CCodeMemberAccess (new CCodeMemberAccess
.pointer (new
CCodeIdentifier ("dest_value"), "data[0]"), "v_pointer");
785 var src_vpointer
= new
CCodeMemberAccess (new CCodeMemberAccess
.pointer (new
CCodeIdentifier ("src_value"), "data[0]"), "v_pointer");
787 var ref_ccall
= new
CCodeFunctionCall (new
CCodeIdentifier ("%sref".printf (get_ccode_lower_case_prefix (cl
))));
788 ref_ccall
.add_argument ( src_vpointer
);
790 ccode
.open_if (src_vpointer
);
791 ccode
.add_assignment (dest_vpointer
, ref_ccall
);
793 ccode
.add_assignment (dest_vpointer
, new
CCodeConstant ("NULL"));
797 cfile
.add_function (function
);
800 private void add_type_value_table_peek_pointer_function (Class cl
) {
801 var function
= new
CCodeFunction ("%s_peek_pointer".printf (get_ccode_lower_case_name (cl
, "value_")), "gpointer");
802 function
.add_parameter (new
CCodeParameter ("value", "const GValue*"));
803 function
.modifiers
= CCodeModifiers
.STATIC
;
805 push_function (function
);
807 var vpointer
= new
CCodeMemberAccess (new CCodeMemberAccess
.pointer (new
CCodeIdentifier ("value"), "data[0]"), "v_pointer");
808 ccode
.add_return (vpointer
);
811 cfile
.add_function (function
);
814 private void add_type_value_table_lcopy_value_function ( Class cl
) {
815 var function
= new
CCodeFunction ("%s_lcopy_value".printf (get_ccode_lower_case_name (cl
, "value_")), "gchar*");
816 function
.add_parameter (new
CCodeParameter ("value", "const GValue*"));
817 function
.add_parameter (new
CCodeParameter ("n_collect_values", "guint"));
818 function
.add_parameter (new
CCodeParameter ("collect_values", "GTypeCValue*"));
819 function
.add_parameter (new
CCodeParameter ("collect_flags", "guint"));
820 function
.modifiers
= CCodeModifiers
.STATIC
;
822 var vpointer
= new
CCodeMemberAccess (new CCodeMemberAccess
.pointer (new
CCodeIdentifier ("value"), "data[0]"), "v_pointer");
823 var object_p_ptr
= new
CCodeIdentifier ("*object_p");
824 var null_
= new
CCodeConstant ("NULL");
826 push_function (function
);
828 ccode
.add_declaration ("%s **".printf (get_ccode_name (cl
)), new
CCodeVariableDeclarator ("object_p", new
CCodeMemberAccess (new
CCodeIdentifier ("collect_values[0]"), "v_pointer")));
830 var value_type_name_fct
= new
CCodeFunctionCall (new
CCodeIdentifier ("G_VALUE_TYPE_NAME"));
831 value_type_name_fct
.add_argument (new
CCodeConstant ("value"));
833 var assert_condition
= new
CCodeUnaryExpression (CCodeUnaryOperator
.LOGICAL_NEGATION
, new
CCodeIdentifier ("object_p"));
834 ccode
.open_if (assert_condition
);
835 var assert_printf
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_strdup_printf"));
836 assert_printf
.add_argument (new
CCodeConstant ("\"value location for `%s' passed as NULL\""));
837 assert_printf
.add_argument (value_type_name_fct
);
838 ccode
.add_return (assert_printf
);
841 var main_condition
= new
CCodeUnaryExpression (CCodeUnaryOperator
.LOGICAL_NEGATION
, vpointer
);
842 var main_else_if_condition
= new
CCodeBinaryExpression (CCodeBinaryOperator
.BITWISE_AND
, new
CCodeIdentifier ("collect_flags"), new
CCodeIdentifier ("G_VALUE_NOCOPY_CONTENTS"));
843 var ref_fct
= new
CCodeFunctionCall (new
CCodeIdentifier (get_ccode_ref_function (cl
)));
844 ref_fct
.add_argument (vpointer
);
845 ccode
.open_if (main_condition
);
846 ccode
.add_assignment (object_p_ptr
, null_
);
847 ccode
.else_if (main_else_if_condition
);
848 ccode
.add_assignment (object_p_ptr
, vpointer
);
850 ccode
.add_assignment (object_p_ptr
, ref_fct
);
853 ccode
.add_return (null_
);
855 cfile
.add_function (function
);
858 private void add_type_value_table_collect_value_function (Class cl
) {
859 var function
= new
CCodeFunction ("%s_collect_value".printf (get_ccode_lower_case_name (cl
, "value_")), "gchar*");
860 function
.add_parameter (new
CCodeParameter ("value", "GValue*"));
861 function
.add_parameter (new
CCodeParameter ("n_collect_values", "guint"));
862 function
.add_parameter (new
CCodeParameter ("collect_values", "GTypeCValue*"));
863 function
.add_parameter (new
CCodeParameter ("collect_flags", "guint"));
864 function
.modifiers
= CCodeModifiers
.STATIC
;
866 var vpointer
= new
CCodeMemberAccess (new CCodeMemberAccess
.pointer (new
CCodeIdentifier ("value"), "data[0]"), "v_pointer");
868 push_function (function
);
870 var collect_vpointer
= new
CCodeMemberAccess (new
CCodeIdentifier ("collect_values[0]"), "v_pointer");
872 ccode
.open_if (collect_vpointer
);
873 ccode
.add_declaration ("%s *".printf (get_ccode_name (cl
)), new
CCodeVariableDeclarator ("object", collect_vpointer
));
874 var obj_identifier
= new
CCodeIdentifier ("object");
875 var l_expression
= new
CCodeMemberAccess (new CCodeMemberAccess
.pointer (obj_identifier
, "parent_instance"), "g_class");
876 var sub_condition
= new
CCodeBinaryExpression (CCodeBinaryOperator
.EQUALITY
, l_expression
, new
CCodeConstant ("NULL"));
877 var value_type_name_fct
= new
CCodeFunctionCall (new
CCodeIdentifier ("G_VALUE_TYPE_NAME"));
878 value_type_name_fct
.add_argument (new
CCodeConstant ("value"));
880 ccode
.open_if (sub_condition
);
881 var true_return
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_strconcat"));
882 true_return
.add_argument (new
CCodeConstant ("\"invalid unclassed object pointer for value type `\""));
883 true_return
.add_argument (value_type_name_fct
);
884 true_return
.add_argument (new
CCodeConstant ("\"'\""));
885 true_return
.add_argument (new
CCodeConstant ("NULL"));
886 ccode
.add_return (true_return
);
888 var reg_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_value_type_compatible"));
889 var type_check
= new
CCodeFunctionCall (new
CCodeIdentifier ("G_TYPE_FROM_INSTANCE"));
890 type_check
.add_argument (new
CCodeIdentifier ("object"));
891 reg_call
.add_argument (type_check
);
892 var stored_type
= new
CCodeFunctionCall (new
CCodeIdentifier ("G_VALUE_TYPE"));
893 stored_type
.add_argument (new
CCodeIdentifier ("value"));
894 reg_call
.add_argument (stored_type
);
896 ccode
.else_if (new
CCodeUnaryExpression (CCodeUnaryOperator
.LOGICAL_NEGATION
, reg_call
));
897 var false_return
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_strconcat"));
898 var type_name_fct
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_type_name"));
899 type_name_fct
.add_argument (type_check
);
900 false_return
.add_argument (new
CCodeConstant ("\"invalid object type `\""));
901 false_return
.add_argument (type_name_fct
);
902 false_return
.add_argument (new
CCodeConstant ("\"' for value type `\""));
903 false_return
.add_argument (value_type_name_fct
);
904 false_return
.add_argument (new
CCodeConstant ("\"'\""));
905 false_return
.add_argument (new
CCodeConstant ("NULL"));
906 ccode
.add_return (false_return
);
910 var ref_call
= new
CCodeFunctionCall (new
CCodeIdentifier (get_ccode_ref_function (cl
)));
911 ref_call
.add_argument (new
CCodeIdentifier ("object"));
912 ccode
.add_assignment (vpointer
, ref_call
);
915 ccode
.add_assignment (vpointer
, new
CCodeConstant ("NULL"));
919 ccode
.add_return (new
CCodeConstant ("NULL"));
922 cfile
.add_function (function
);
925 private void add_g_param_spec_type_function (Class cl
) {
926 string function_name
= get_ccode_lower_case_name (cl
, "param_spec_");
928 var function
= new
CCodeFunction (function_name
, "GParamSpec*");
929 function
.add_parameter (new
CCodeParameter ("name", "const gchar*"));
930 function
.add_parameter (new
CCodeParameter ("nick", "const gchar*"));
931 function
.add_parameter (new
CCodeParameter ("blurb", "const gchar*"));
932 function
.add_parameter (new
CCodeParameter ("object_type", "GType"));
933 function
.add_parameter (new
CCodeParameter ("flags", "GParamFlags"));
935 if (cl
.is_private_symbol ()) {
936 function
.modifiers
= CCodeModifiers
.STATIC
;
937 } else if (context
.hide_internal
&& cl
.is_internal_symbol ()) {
938 function
.modifiers
= CCodeModifiers
.INTERNAL
;
941 push_function (function
);
943 ccode
.add_declaration ("%sParamSpec%s*".printf (get_ccode_prefix (cl
.parent_symbol
), cl
.name
), new
CCodeVariableDeclarator ("spec"));
945 var subccall
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_type_is_a"));
946 subccall
.add_argument (new
CCodeIdentifier ("object_type"));
947 subccall
.add_argument (new
CCodeIdentifier ( get_ccode_type_id (cl
) ));
949 var ccall
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_return_val_if_fail"));
950 ccall
.add_argument (subccall
);
951 ccall
.add_argument (new
CCodeIdentifier ("NULL"));
952 ccode
.add_expression (ccall
);
954 ccall
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_param_spec_internal"));
955 ccall
.add_argument (new
CCodeIdentifier ( "G_TYPE_PARAM_OBJECT" ));
956 ccall
.add_argument (new
CCodeIdentifier ("name"));
957 ccall
.add_argument (new
CCodeIdentifier ("nick"));
958 ccall
.add_argument (new
CCodeIdentifier ("blurb"));
959 ccall
.add_argument (new
CCodeIdentifier ("flags"));
961 ccode
.add_assignment (new
CCodeIdentifier ("spec"), ccall
);
963 ccall
= new
CCodeFunctionCall (new
CCodeIdentifier ("G_PARAM_SPEC"));
964 ccall
.add_argument (new
CCodeIdentifier ("spec"));
966 ccode
.add_assignment (new CCodeMemberAccess
.pointer (ccall
, "value_type"), new
CCodeIdentifier ("object_type"));
967 ccode
.add_return (ccall
);
970 cfile
.add_function (function
);
973 private void add_g_value_set_function (Class cl
) {
974 var function
= new
CCodeFunction (get_ccode_set_value_function (cl
), "void");
975 function
.add_parameter (new
CCodeParameter ("value", "GValue*"));
976 function
.add_parameter (new
CCodeParameter ("v_object", "gpointer"));
978 if (cl
.is_private_symbol ()) {
979 function
.modifiers
= CCodeModifiers
.STATIC
;
980 } else if (context
.hide_internal
&& cl
.is_internal_symbol ()) {
981 function
.modifiers
= CCodeModifiers
.INTERNAL
;
984 var vpointer
= new
CCodeMemberAccess (new CCodeMemberAccess
.pointer (new
CCodeIdentifier ("value"), "data[0]"), "v_pointer");
986 push_function (function
);
988 ccode
.add_declaration ("%s *".printf (get_ccode_name (cl
)), new
CCodeVariableDeclarator ("old"));
990 var ccall_typecheck
= new
CCodeFunctionCall (new
CCodeIdentifier ("G_TYPE_CHECK_VALUE_TYPE"));
991 ccall_typecheck
.add_argument (new
CCodeIdentifier ( "value" ));
992 ccall_typecheck
.add_argument (new
CCodeIdentifier ( get_ccode_type_id (cl
) ));
994 var ccall
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_return_if_fail"));
995 ccall
.add_argument (ccall_typecheck
);
996 ccode
.add_expression (ccall
);
998 ccode
.add_assignment (new
CCodeConstant ("old"), vpointer
);
1000 ccode
.open_if (new
CCodeIdentifier ("v_object"));
1001 ccall_typecheck
= new
CCodeFunctionCall (new
CCodeIdentifier ("G_TYPE_CHECK_INSTANCE_TYPE"));
1002 ccall_typecheck
.add_argument (new
CCodeIdentifier ( "v_object" ));
1003 ccall_typecheck
.add_argument (new
CCodeIdentifier ( get_ccode_type_id (cl
) ));
1005 ccall
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_return_if_fail"));
1006 ccall
.add_argument (ccall_typecheck
);
1007 ccode
.add_expression (ccall
);
1009 var ccall_typefrominstance
= new
CCodeFunctionCall (new
CCodeIdentifier ("G_TYPE_FROM_INSTANCE"));
1010 ccall_typefrominstance
.add_argument (new
CCodeIdentifier ( "v_object" ));
1012 var ccall_gvaluetype
= new
CCodeFunctionCall (new
CCodeIdentifier ("G_VALUE_TYPE"));
1013 ccall_gvaluetype
.add_argument (new
CCodeIdentifier ( "value" ));
1015 var ccall_typecompatible
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_value_type_compatible"));
1016 ccall_typecompatible
.add_argument (ccall_typefrominstance
);
1017 ccall_typecompatible
.add_argument (ccall_gvaluetype
);
1019 ccall
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_return_if_fail"));
1020 ccall
.add_argument (ccall_typecompatible
);
1021 ccode
.add_expression (ccall
);
1023 ccode
.add_assignment (vpointer
, new
CCodeConstant ("v_object"));
1025 ccall
= new
CCodeFunctionCall (new
CCodeIdentifier (get_ccode_ref_function (cl
)));
1026 ccall
.add_argument (vpointer
);
1027 ccode
.add_expression (ccall
);
1030 ccode
.add_assignment (vpointer
, new
CCodeConstant ("NULL"));
1033 ccode
.open_if (new
CCodeIdentifier ("old"));
1034 ccall
= new
CCodeFunctionCall (new
CCodeIdentifier (get_ccode_unref_function (cl
)));
1035 ccall
.add_argument (new
CCodeIdentifier ("old"));
1036 ccode
.add_expression (ccall
);
1040 cfile
.add_function (function
);
1043 private void add_g_value_take_function (Class cl
) {
1044 var function
= new
CCodeFunction (get_ccode_take_value_function (cl
), "void");
1045 function
.add_parameter (new
CCodeParameter ("value", "GValue*"));
1046 function
.add_parameter (new
CCodeParameter ("v_object", "gpointer"));
1048 if (cl
.is_private_symbol ()) {
1049 function
.modifiers
= CCodeModifiers
.STATIC
;
1050 } else if (context
.hide_internal
&& cl
.is_internal_symbol ()) {
1051 function
.modifiers
= CCodeModifiers
.INTERNAL
;
1054 var vpointer
= new
CCodeMemberAccess(new CCodeMemberAccess
.pointer (new
CCodeIdentifier ("value"), "data[0]"),"v_pointer");
1056 push_function (function
);
1058 ccode
.add_declaration ("%s *".printf (get_ccode_name (cl
)), new
CCodeVariableDeclarator ("old"));
1060 var ccall_typecheck
= new
CCodeFunctionCall (new
CCodeIdentifier ("G_TYPE_CHECK_VALUE_TYPE"));
1061 ccall_typecheck
.add_argument (new
CCodeIdentifier ( "value" ));
1062 ccall_typecheck
.add_argument (new
CCodeIdentifier ( get_ccode_type_id (cl
) ));
1064 var ccall
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_return_if_fail"));
1065 ccall
.add_argument (ccall_typecheck
);
1066 ccode
.add_expression (ccall
);
1068 ccode
.add_assignment (new
CCodeConstant ("old"), vpointer
);
1070 ccode
.open_if (new
CCodeIdentifier ("v_object"));
1072 ccall_typecheck
= new
CCodeFunctionCall (new
CCodeIdentifier ("G_TYPE_CHECK_INSTANCE_TYPE"));
1073 ccall_typecheck
.add_argument (new
CCodeIdentifier ( "v_object" ));
1074 ccall_typecheck
.add_argument (new
CCodeIdentifier ( get_ccode_type_id (cl
) ));
1076 ccall
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_return_if_fail"));
1077 ccall
.add_argument (ccall_typecheck
);
1078 ccode
.add_expression (ccall
);
1080 var ccall_typefrominstance
= new
CCodeFunctionCall (new
CCodeIdentifier ("G_TYPE_FROM_INSTANCE"));
1081 ccall_typefrominstance
.add_argument (new
CCodeIdentifier ( "v_object" ));
1083 var ccall_gvaluetype
= new
CCodeFunctionCall (new
CCodeIdentifier ("G_VALUE_TYPE"));
1084 ccall_gvaluetype
.add_argument (new
CCodeIdentifier ( "value" ));
1086 var ccall_typecompatible
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_value_type_compatible"));
1087 ccall_typecompatible
.add_argument (ccall_typefrominstance
);
1088 ccall_typecompatible
.add_argument (ccall_gvaluetype
);
1090 ccall
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_return_if_fail"));
1091 ccall
.add_argument (ccall_typecompatible
);
1092 ccode
.add_expression (ccall
);
1094 ccode
.add_assignment (vpointer
, new
CCodeConstant ("v_object"));
1097 ccode
.add_assignment (vpointer
, new
CCodeConstant ("NULL"));
1100 ccode
.open_if (new
CCodeIdentifier ("old"));
1101 ccall
= new
CCodeFunctionCall (new
CCodeIdentifier (get_ccode_unref_function (cl
)));
1102 ccall
.add_argument (new
CCodeIdentifier ("old"));
1103 ccode
.add_expression (ccall
);
1107 cfile
.add_function (function
);
1110 private void add_g_value_get_function (Class cl
) {
1111 var function
= new
CCodeFunction (get_ccode_get_value_function (cl
), "gpointer");
1112 function
.add_parameter (new
CCodeParameter ("value", "const GValue*"));
1114 if (cl
.is_private_symbol ()) {
1115 function
.modifiers
= CCodeModifiers
.STATIC
;
1116 } else if (context
.hide_internal
&& cl
.is_internal_symbol ()) {
1117 function
.modifiers
= CCodeModifiers
.INTERNAL
;
1120 var vpointer
= new
CCodeMemberAccess(new CCodeMemberAccess
.pointer (new
CCodeIdentifier ("value"), "data[0]"),"v_pointer");
1122 push_function (function
);
1124 var ccall_typecheck
= new
CCodeFunctionCall (new
CCodeIdentifier ("G_TYPE_CHECK_VALUE_TYPE"));
1125 ccall_typecheck
.add_argument (new
CCodeIdentifier ("value"));
1126 ccall_typecheck
.add_argument (new
CCodeIdentifier (get_ccode_type_id (cl
)));
1128 var ccall
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_return_val_if_fail"));
1129 ccall
.add_argument (ccall_typecheck
);
1130 ccall
.add_argument (new
CCodeIdentifier ("NULL"));
1131 ccode
.add_expression (ccall
);
1133 ccode
.add_return (vpointer
);
1136 cfile
.add_function (function
);
1139 private void begin_base_init_function (Class cl
) {
1140 push_context (base_init_context
);
1142 var base_init
= new
CCodeFunction ("%s_base_init".printf (get_ccode_lower_case_name (cl
, null)), "void");
1143 base_init
.add_parameter (new
CCodeParameter ("klass", "%sClass *".printf (get_ccode_name (cl
))));
1144 base_init
.modifiers
= CCodeModifiers
.STATIC
;
1146 push_function (base_init
);
1151 private void add_base_init_function (Class cl
) {
1152 cfile
.add_function (base_init_context
.ccode
);
1155 public virtual void generate_class_init (Class cl
) {
1158 public virtual void end_instance_init (Class cl
) {
1161 private void begin_class_init_function (Class cl
) {
1162 push_context (class_init_context
);
1164 var func
= new
CCodeFunction ("%s_class_init".printf (get_ccode_lower_case_name (cl
, null)));
1165 func
.add_parameter (new
CCodeParameter ("klass", "%sClass *".printf (get_ccode_name (cl
))));
1166 func
.modifiers
= CCodeModifiers
.STATIC
;
1168 CCodeFunctionCall ccall
;
1170 /* save pointer to parent class */
1171 var parent_decl
= new
CCodeDeclaration ("gpointer");
1172 var parent_var_decl
= new
CCodeVariableDeclarator ("%s_parent_class".printf (get_ccode_lower_case_name (cl
, null)));
1173 parent_var_decl
.initializer
= new
CCodeConstant ("NULL");
1174 parent_decl
.add_declarator (parent_var_decl
);
1175 parent_decl
.modifiers
= CCodeModifiers
.STATIC
;
1176 cfile
.add_type_member_declaration (parent_decl
);
1178 push_function (func
);
1180 ccall
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_type_class_peek_parent"));
1181 ccall
.add_argument (new
CCodeIdentifier ("klass"));
1182 var parent_assignment
= new
CCodeAssignment (new
CCodeIdentifier ("%s_parent_class".printf (get_ccode_lower_case_name (cl
, null))), ccall
);
1183 ccode
.add_expression (parent_assignment
);
1186 if (!cl
.is_compact
&& !cl
.is_subtype_of (gobject_type
) && (cl
.get_fields ().size
> 0 || cl
.destructor
!= null || cl
.is_fundamental ())) {
1187 // set finalize function
1188 var fundamental_class
= cl
;
1189 while (fundamental_class
.base_class
!= null) {
1190 fundamental_class
= fundamental_class
.base_class
;
1193 var ccast
= new
CCodeCastExpression (new
CCodeIdentifier ("klass"), "%sClass *".printf (get_ccode_name (fundamental_class
)));
1194 var finalize_assignment
= new
CCodeAssignment (new CCodeMemberAccess
.pointer (ccast
, "finalize"), new
CCodeIdentifier ("%sfinalize".printf (get_ccode_lower_case_prefix (cl
))));
1195 ccode
.add_expression (finalize_assignment
);
1198 /* add struct for private fields */
1199 if (cl
.has_private_fields
|| cl
.get_type_parameters ().size
> 0) {
1200 ccall
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_type_class_add_private"));
1201 ccall
.add_argument (new
CCodeIdentifier ("klass"));
1202 ccall
.add_argument (new
CCodeConstant ("sizeof (%sPrivate)".printf (get_ccode_name (cl
))));
1203 ccode
.add_expression (ccall
);
1206 /* connect overridden methods */
1207 foreach (Method m
in cl
.get_methods ()) {
1208 if (m
.base_method
== null) {
1211 var base_type
= (ObjectTypeSymbol
) m
.base_method
.parent_symbol
;
1213 // there is currently no default handler for abstract async methods
1214 if (!m
.is_abstract
|| !m
.coroutine
) {
1215 CCodeExpression cfunc
= new
CCodeIdentifier (get_ccode_real_name (m
));
1216 cfunc
= cast_method_pointer (m
.base_method
, cfunc
, base_type
, (m
.coroutine ?
1 : 3));
1217 var ccast
= new
CCodeCastExpression (new
CCodeIdentifier ("klass"), "%sClass *".printf (get_ccode_name (base_type
)));
1218 ccode
.add_assignment (new CCodeMemberAccess
.pointer (ccast
, get_ccode_vfunc_name (m
.base_method
)), cfunc
);
1221 cfunc
= new
CCodeIdentifier (get_ccode_finish_real_name (m
));
1222 cfunc
= cast_method_pointer (m
.base_method
, cfunc
, base_type
, 2);
1223 ccode
.add_assignment (new CCodeMemberAccess
.pointer (ccast
, get_ccode_finish_vfunc_name (m
.base_method
)), cfunc
);
1228 /* connect default signal handlers */
1229 foreach (Signal sig
in cl
.get_signals ()) {
1230 if (sig
.default_handler
== null) {
1233 var ccast
= new
CCodeCastExpression (new
CCodeIdentifier ("klass"), "%sClass *".printf (get_ccode_name (cl
)));
1234 ccode
.add_assignment (new CCodeMemberAccess
.pointer (ccast
, get_ccode_vfunc_name (sig
.default_handler
)), new
CCodeIdentifier (get_ccode_real_name (sig
.default_handler
)));
1237 /* connect overridden properties */
1238 foreach (Property prop
in cl
.get_properties ()) {
1239 if (prop
.base_property
== null) {
1242 var base_type
= prop
.base_property
.parent_symbol
;
1244 var ccast
= new
CCodeFunctionCall (new
CCodeIdentifier ("%s_CLASS".printf (get_ccode_upper_case_name (base_type
))));
1245 ccast
.add_argument (new
CCodeIdentifier ("klass"));
1247 if (!get_ccode_no_accessor_method (prop
.base_property
) && !get_ccode_concrete_accessor (prop
.base_property
)) {
1248 if (prop
.get_accessor
!= null) {
1249 string cname
= CCodeBaseModule
.get_ccode_real_name (prop
.get_accessor
);
1250 ccode
.add_assignment (new CCodeMemberAccess
.pointer (ccast
, "get_%s".printf (prop
.name
)), new
CCodeIdentifier (cname
));
1252 if (prop
.set_accessor
!= null) {
1253 string cname
= CCodeBaseModule
.get_ccode_real_name (prop
.set_accessor
);
1254 ccode
.add_assignment (new CCodeMemberAccess
.pointer (ccast
, "set_%s".printf (prop
.name
)), new
CCodeIdentifier (cname
));
1259 generate_class_init (cl
);
1261 if (!cl
.is_compact
) {
1262 /* create signals */
1263 foreach (Signal sig
in cl
.get_signals ()) {
1264 if (sig
.comment
!= null) {
1265 ccode
.add_statement (new
CCodeComment (sig
.comment
.content
));
1267 ccode
.add_expression (get_signal_creation (sig
, cl
));
1274 private void add_class_init_function (Class cl
) {
1275 cfile
.add_function (class_init_context
.ccode
);
1278 private void add_generic_accessor_function (string base_name
, string return_type
, CCodeExpression? expression
, TypeParameter p
, Class cl
, Interface iface
) {
1279 string name
= "%s_%s_%s".printf (get_ccode_lower_case_name (cl
), get_ccode_lower_case_name (iface
), base_name
);
1281 var function
= new
CCodeFunction (name
, return_type
);
1282 function
.modifiers
= CCodeModifiers
.STATIC
;
1283 var this_type
= get_data_type_for_symbol (cl
);
1284 function
.add_parameter (new
CCodeParameter ("self", get_ccode_name (this_type
)));
1285 push_function (function
);
1286 ccode
.add_return (expression
);
1288 cfile
.add_function (function
);
1290 CCodeExpression cfunc
= new
CCodeIdentifier (function
.name
);
1291 string cast
= "%s (*)".printf (return_type
);
1292 string cast_args
= "%s *".printf (get_ccode_name (iface
));
1293 cast
= "%s (%s)".printf (cast
, cast_args
);
1294 cfunc
= new
CCodeCastExpression (cfunc
, cast
);
1295 var ciface
= new
CCodeIdentifier ("iface");
1296 ccode
.add_assignment (new CCodeMemberAccess
.pointer (ciface
, base_name
), cfunc
);
1299 private void add_interface_init_function (Class cl
, Interface iface
) {
1300 var iface_init
= new
CCodeFunction ("%s_%s_interface_init".printf (get_ccode_lower_case_name (cl
), get_ccode_lower_case_name (iface
)), "void");
1301 iface_init
.add_parameter (new
CCodeParameter ("iface", "%s *".printf (get_ccode_type_name (iface
))));
1302 iface_init
.modifiers
= CCodeModifiers
.STATIC
;
1304 push_function (iface_init
);
1306 CCodeFunctionCall ccall
;
1308 /* save pointer to parent vtable */
1309 string parent_iface_var
= "%s_%s_parent_iface".printf (get_ccode_lower_case_name (cl
), get_ccode_lower_case_name (iface
));
1310 var parent_decl
= new
CCodeDeclaration ("%s *".printf (get_ccode_type_name (iface
)));
1311 var parent_var_decl
= new
CCodeVariableDeclarator (parent_iface_var
);
1312 parent_var_decl
.initializer
= new
CCodeConstant ("NULL");
1313 parent_decl
.add_declarator (parent_var_decl
);
1314 parent_decl
.modifiers
= CCodeModifiers
.STATIC
;
1315 cfile
.add_type_member_declaration (parent_decl
);
1316 ccall
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_type_interface_peek_parent"));
1317 ccall
.add_argument (new
CCodeIdentifier ("iface"));
1318 ccode
.add_assignment (new
CCodeIdentifier (parent_iface_var
), ccall
);
1320 foreach (Method m
in cl
.get_methods ()) {
1321 if (m
.base_interface_method
== null) {
1325 var base_type
= m
.base_interface_method
.parent_symbol
;
1326 if (base_type
!= iface
) {
1330 var ciface
= new
CCodeIdentifier ("iface");
1331 CCodeExpression cfunc
;
1332 if (m
.is_abstract
|| m
.is_virtual
) {
1333 cfunc
= new
CCodeIdentifier (get_ccode_name (m
));
1335 cfunc
= new
CCodeIdentifier (get_ccode_real_name (m
));
1337 cfunc
= cast_method_pointer (m
.base_interface_method
, cfunc
, iface
, (m
.coroutine ?
1 : 3));
1338 ccode
.add_assignment (new CCodeMemberAccess
.pointer (ciface
, get_ccode_vfunc_name (m
.base_interface_method
)), cfunc
);
1341 if (m
.is_abstract
|| m
.is_virtual
) {
1342 cfunc
= new
CCodeIdentifier (get_ccode_finish_name (m
));
1344 cfunc
= new
CCodeIdentifier (get_ccode_finish_real_name (m
));
1346 cfunc
= cast_method_pointer (m
.base_interface_method
, cfunc
, iface
, 2);
1347 ccode
.add_assignment (new CCodeMemberAccess
.pointer (ciface
, get_ccode_finish_vfunc_name (m
.base_interface_method
)), cfunc
);
1351 if (iface
.get_attribute ("GenericAccessors") != null) {
1352 foreach (TypeParameter p
in iface
.get_type_parameters ()) {
1353 GenericType p_type
= new
GenericType (p
);
1354 DataType p_data_type
= p_type
.get_actual_type (get_data_type_for_symbol (cl
), null, cl
);
1356 add_generic_accessor_function ("get_%s_type".printf (p
.name
.down ()),
1358 get_type_id_expression (p_data_type
),
1361 add_generic_accessor_function ("get_%s_dup_func".printf (p
.name
.down ()),
1363 get_dup_func_expression (p_data_type
, null),
1366 add_generic_accessor_function ("get_%s_destroy_func".printf (p
.name
.down ()),
1368 get_destroy_func_expression (p_data_type
),
1373 // connect inherited implementations
1374 foreach (Method m
in iface
.get_methods ()) {
1375 if (m
.is_abstract
) {
1376 Method cl_method
= null;
1377 var base_class
= cl
;
1378 while (base_class
!= null && cl_method
== null) {
1379 cl_method
= base_class
.scope
.lookup (m
.name
) as Method
;
1380 base_class
= base_class
.base_class
;
1382 if (base_class
!= null && cl_method
.parent_symbol
!= cl
) {
1383 // method inherited from base class
1385 var base_method
= cl_method
;
1386 if (cl_method
.base_method
!= null) {
1387 base_method
= cl_method
.base_method
;
1388 } else if (cl_method
.base_interface_method
!= null) {
1389 base_method
= cl_method
.base_interface_method
;
1392 generate_method_declaration (base_method
, cfile
);
1394 CCodeExpression cfunc
= new
CCodeIdentifier (get_ccode_name (base_method
));
1395 cfunc
= cast_method_pointer (base_method
, cfunc
, iface
);
1396 var ciface
= new
CCodeIdentifier ("iface");
1397 ccode
.add_assignment (new CCodeMemberAccess
.pointer (ciface
, get_ccode_vfunc_name (m
)), cfunc
);
1402 foreach (Property prop
in cl
.get_properties ()) {
1403 if (prop
.base_interface_property
== null) {
1407 var base_type
= (ObjectTypeSymbol
) prop
.base_interface_property
.parent_symbol
;
1408 if (base_type
!= iface
) {
1412 var ciface
= new
CCodeIdentifier ("iface");
1414 if (!get_ccode_no_accessor_method (prop
.base_interface_property
) && !get_ccode_concrete_accessor (prop
.base_interface_property
)) {
1415 if (prop
.get_accessor
!= null) {
1416 string cname
= CCodeBaseModule
.get_ccode_real_name (prop
.get_accessor
);
1417 if (prop
.is_abstract
|| prop
.is_virtual
) {
1418 cname
= CCodeBaseModule
.get_ccode_name (prop
.get_accessor
);
1421 CCodeExpression cfunc
= new
CCodeIdentifier (cname
);
1422 if (prop
.is_abstract
|| prop
.is_virtual
) {
1423 cfunc
= cast_property_accessor_pointer (prop
.get_accessor
, cfunc
, base_type
);
1425 ccode
.add_assignment (new CCodeMemberAccess
.pointer (ciface
, "get_%s".printf (prop
.name
)), cfunc
);
1427 if (prop
.set_accessor
!= null) {
1428 string cname
= CCodeBaseModule
.get_ccode_real_name (prop
.set_accessor
);
1429 if (prop
.is_abstract
|| prop
.is_virtual
) {
1430 cname
= CCodeBaseModule
.get_ccode_name (prop
.set_accessor
);
1433 CCodeExpression cfunc
= new
CCodeIdentifier (cname
);
1434 if (prop
.is_abstract
|| prop
.is_virtual
) {
1435 cfunc
= cast_property_accessor_pointer (prop
.set_accessor
, cfunc
, base_type
);
1437 ccode
.add_assignment (new CCodeMemberAccess
.pointer (ciface
, "set_%s".printf (prop
.name
)), cfunc
);
1442 foreach (Property prop
in iface
.get_properties ()) {
1443 if (!prop
.is_abstract
) {
1447 Property cl_prop
= null;
1448 var base_class
= cl
;
1449 while (base_class
!= null && cl_prop
== null) {
1450 cl_prop
= base_class
.scope
.lookup (prop
.name
) as Property
;
1451 base_class
= base_class
.base_class
;
1453 if (base_class
!= null && cl_prop
.parent_symbol
!= cl
) {
1454 // property inherited from base class
1456 var base_property
= cl_prop
;
1457 if (cl_prop
.base_property
!= null) {
1458 base_property
= cl_prop
.base_property
;
1459 } else if (cl_prop
.base_interface_property
!= null) {
1460 base_property
= cl_prop
.base_interface_property
;
1463 var ciface
= new
CCodeIdentifier ("iface");
1465 if (base_property
.get_accessor
!= null && prop
.get_accessor
!= null) {
1466 generate_property_accessor_declaration (base_property
.get_accessor
, cfile
);
1468 string cname
= get_ccode_name (base_property
.get_accessor
);
1469 CCodeExpression cfunc
= new
CCodeIdentifier (cname
);
1470 cfunc
= cast_property_accessor_pointer (prop
.get_accessor
, cfunc
, iface
);
1471 ccode
.add_assignment (new CCodeMemberAccess
.pointer (ciface
, "get_%s".printf (prop
.name
)), cfunc
);
1473 if (base_property
.set_accessor
!= null && prop
.set_accessor
!= null) {
1474 generate_property_accessor_declaration (base_property
.set_accessor
, cfile
);
1476 string cname
= get_ccode_name (base_property
.set_accessor
);
1477 CCodeExpression cfunc
= new
CCodeIdentifier (cname
);
1478 cfunc
= cast_property_accessor_pointer (prop
.set_accessor
, cfunc
, iface
);
1479 ccode
.add_assignment (new CCodeMemberAccess
.pointer (ciface
, "set_%s".printf (prop
.name
)), cfunc
);
1485 cfile
.add_function (iface_init
);
1488 CCodeExpression
cast_property_accessor_pointer (PropertyAccessor acc
, CCodeExpression cfunc
, ObjectTypeSymbol base_type
) {
1490 if (acc
.readable
&& acc
.value_type
.is_real_non_null_struct_type ()) {
1491 cast
= "void (*) (%s *, %s *)".printf (get_ccode_name (base_type
), get_ccode_name (acc
.value_type
));
1492 } else if (acc
.readable
) {
1493 cast
= "%s (*) (%s *)".printf (get_ccode_name (acc
.value_type
), get_ccode_name (base_type
));
1494 } else if (acc
.value_type
.is_real_non_null_struct_type ()) {
1495 cast
= "void (*) (%s *, %s *)".printf (get_ccode_name (base_type
), get_ccode_name (acc
.value_type
));
1497 cast
= "void (*) (%s *, %s)".printf (get_ccode_name (base_type
), get_ccode_name (acc
.value_type
));
1499 return new
CCodeCastExpression (cfunc
, cast
);
1502 CCodeExpression
cast_method_pointer (Method m
, CCodeExpression cfunc
, ObjectTypeSymbol base_type
, int direction
= 3) {
1503 // Cast the function pointer to match the interface
1505 if (direction
== 1 || m
.return_type
.is_real_non_null_struct_type ()) {
1508 cast
= "%s (*)".printf (get_ccode_name (m
.return_type
));
1510 string cast_args
= "%s *".printf (get_ccode_name (base_type
));
1512 var vdeclarator
= new
CCodeFunctionDeclarator (get_ccode_vfunc_name (m
));
1513 var cparam_map
= new HashMap
<int,CCodeParameter
> (direct_hash
, direct_equal
);
1515 generate_cparameters (m
, cfile
, cparam_map
, new
CCodeFunction ("fake"), vdeclarator
, null, null, direction
);
1517 // append C arguments in the right order
1522 foreach (int pos
in cparam_map
.get_keys ()) {
1523 if (pos
> last_pos
&& (min_pos
== -1 || pos
< min_pos
)) {
1527 if (last_pos
!= -1) { // Skip the 1st parameter
1528 if (min_pos
== -1) {
1532 var tmp
= cparam_map
.get (min_pos
);
1534 cast_args
= "%s, ...".printf (cast_args
);
1536 cast_args
= "%s, %s".printf (cast_args
, tmp
.type_name
);
1541 cast
= "%s (%s)".printf (cast
, cast_args
);
1542 return new
CCodeCastExpression (cfunc
, cast
);
1545 private void begin_instance_init_function (Class cl
) {
1546 push_context (instance_init_context
);
1548 var func
= new
CCodeFunction ("%s_instance_init".printf (get_ccode_lower_case_name (cl
, null)));
1549 func
.add_parameter (new
CCodeParameter ("self", "%s *".printf (get_ccode_name (cl
))));
1550 func
.modifiers
= CCodeModifiers
.STATIC
;
1552 push_function (func
);
1554 if (cl
.is_compact
) {
1555 // Add declaration, since the instance_init function is explicitly called
1556 // by the creation methods
1557 cfile
.add_function_declaration (func
);
1560 if (!cl
.is_compact
&& (cl
.has_private_fields
|| cl
.get_type_parameters ().size
> 0)) {
1561 var ccall
= new
CCodeFunctionCall (new
CCodeIdentifier ("%s_GET_PRIVATE".printf (get_ccode_upper_case_name (cl
, null))));
1562 ccall
.add_argument (new
CCodeIdentifier ("self"));
1563 func
.add_assignment (new CCodeMemberAccess
.pointer (new
CCodeIdentifier ("self"), "priv"), ccall
);
1569 private void add_instance_init_function (Class cl
) {
1570 push_context (instance_init_context
);
1571 end_instance_init (cl
);
1574 cfile
.add_function (instance_init_context
.ccode
);
1577 private void begin_class_finalize_function (Class cl
) {
1578 push_context (class_finalize_context
);
1580 var function
= new
CCodeFunction ("%s_class_finalize".printf (get_ccode_lower_case_name (cl
, null)), "void");
1581 function
.modifiers
= CCodeModifiers
.STATIC
;
1583 function
.add_parameter (new
CCodeParameter ("klass", "%sClass *".printf (get_ccode_name (cl
))));
1585 push_function (function
);
1587 if (cl
.static_destructor
!= null) {
1588 cl
.static_destructor
.body
.emit (this
);
1594 private void add_class_finalize_function (Class cl
) {
1595 cfile
.add_function_declaration (class_finalize_context
.ccode
);
1596 cfile
.add_function (class_finalize_context
.ccode
);
1599 private void begin_base_finalize_function (Class cl
) {
1600 push_context (base_finalize_context
);
1602 var function
= new
CCodeFunction ("%s_base_finalize".printf (get_ccode_lower_case_name (cl
, null)), "void");
1603 function
.modifiers
= CCodeModifiers
.STATIC
;
1605 function
.add_parameter (new
CCodeParameter ("klass", "%sClass *".printf (get_ccode_name (cl
))));
1607 push_function (function
);
1609 if (cl
.class_destructor
!= null) {
1610 cl
.class_destructor
.body
.emit (this
);
1616 private void add_base_finalize_function (Class cl
) {
1617 push_context (base_finalize_context
);
1619 cfile
.add_function_declaration (ccode
);
1620 cfile
.add_function (ccode
);
1625 private void begin_finalize_function (Class cl
) {
1626 push_context (instance_finalize_context
);
1628 bool is_gsource
= cl
.base_class
== gsource_type
;
1630 if (!cl
.is_compact
|| is_gsource
) {
1631 var fundamental_class
= cl
;
1632 while (fundamental_class
.base_class
!= null) {
1633 fundamental_class
= fundamental_class
.base_class
;
1636 var func
= new
CCodeFunction ("%s_finalize".printf (get_ccode_lower_case_name (cl
, null)));
1637 func
.add_parameter (new
CCodeParameter ("obj", "%s *".printf (get_ccode_name (fundamental_class
))));
1638 func
.modifiers
= CCodeModifiers
.STATIC
;
1640 push_function (func
);
1643 cfile
.add_function_declaration (func
);
1646 CCodeExpression ccast
;
1647 if (!cl
.is_compact
) {
1648 ccast
= generate_instance_cast (new
CCodeIdentifier ("obj"), cl
);
1650 ccast
= new
CCodeCastExpression (new
CCodeIdentifier ("obj"), "%s *".printf (get_ccode_name (cl
)));
1653 ccode
.add_declaration ("%s *".printf (get_ccode_name (cl
)), new
CCodeVariableDeclarator ("self"));
1654 ccode
.add_assignment (new
CCodeIdentifier ("self"), ccast
);
1656 if (!cl
.is_compact
&& cl
.base_class
== null) {
1657 // non-gobject class
1658 var call
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_signal_handlers_destroy"));
1659 call
.add_argument (new
CCodeIdentifier ("self"));
1660 ccode
.add_expression (call
);
1663 var function
= new
CCodeFunction ("%sfree".printf (get_ccode_lower_case_prefix (cl
)), "void");
1664 if (cl
.is_private_symbol ()) {
1665 function
.modifiers
= CCodeModifiers
.STATIC
;
1666 } else if (context
.hide_internal
&& cl
.is_internal_symbol ()) {
1667 function
.modifiers
= CCodeModifiers
.INTERNAL
;
1670 function
.add_parameter (new
CCodeParameter ("self", "%s *".printf (get_ccode_name (cl
))));
1672 push_function (function
);
1675 if (cl
.destructor
!= null) {
1676 cl
.destructor
.body
.emit (this
);
1678 if (current_method_inner_error
) {
1679 ccode
.add_declaration ("GError *", new CCodeVariableDeclarator
.zero ("_inner_error_", new
CCodeConstant ("NULL")));
1682 if (current_method_return
) {
1683 // support return statements in destructors
1684 ccode
.add_label ("_return");
1691 private void add_finalize_function (Class cl
) {
1692 if (!cl
.is_compact
) {
1693 var fundamental_class
= cl
;
1694 while (fundamental_class
.base_class
!= null) {
1695 fundamental_class
= fundamental_class
.base_class
;
1698 // chain up to finalize function of the base class
1699 if (cl
.base_class
!= null) {
1700 var ccast
= new
CCodeFunctionCall (new
CCodeIdentifier ("%s_CLASS".printf (get_ccode_upper_case_name (fundamental_class
))));
1701 ccast
.add_argument (new
CCodeIdentifier ("%s_parent_class".printf (get_ccode_lower_case_name (cl
, null))));
1702 var ccall
= new
CCodeFunctionCall (new CCodeMemberAccess
.pointer (ccast
, "finalize"));
1703 ccall
.add_argument (new
CCodeIdentifier ("obj"));
1704 push_context (instance_finalize_context
);
1705 ccode
.add_expression (ccall
);
1709 cfile
.add_function_declaration (instance_finalize_context
.ccode
);
1710 } else if (cl
.base_class
== null) {
1711 var ccall
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_slice_free"));
1712 ccall
.add_argument (new
CCodeIdentifier (get_ccode_name (cl
)));
1713 ccall
.add_argument (new
CCodeIdentifier ("self"));
1714 push_context (instance_finalize_context
);
1715 ccode
.add_expression (ccall
);
1719 cfile
.add_function (instance_finalize_context
.ccode
);
1722 public override CCodeFunctionCall
get_param_spec (Property prop
) {
1723 var cspec
= new
CCodeFunctionCall ();
1724 cspec
.add_argument (get_property_canonical_cconstant (prop
));
1725 cspec
.add_argument (new
CCodeConstant ("\"%s\"".printf (prop
.nick
)));
1726 cspec
.add_argument (new
CCodeConstant ("\"%s\"".printf (prop
.blurb
)));
1729 if (prop
.property_type
.data_type is Class
|| prop
.property_type
.data_type is Interface
) {
1730 string param_spec_name
= get_ccode_param_spec_function (prop
.property_type
.data_type
);
1731 cspec
.call
= new
CCodeIdentifier (param_spec_name
);
1732 if (param_spec_name
== "g_param_spec_string") {
1733 cspec
.add_argument (new
CCodeConstant ("NULL"));
1734 } else if (param_spec_name
== "g_param_spec_variant") {
1735 cspec
.add_argument (new
CCodeConstant ("G_VARIANT_TYPE_ANY"));
1736 cspec
.add_argument (new
CCodeConstant ("NULL"));
1737 } else if (get_ccode_type_id (prop
.property_type
.data_type
) != "G_TYPE_POINTER") {
1738 cspec
.add_argument (new
CCodeIdentifier (get_ccode_type_id (prop
.property_type
.data_type
)));
1740 } else if (prop
.property_type
.data_type is Enum
) {
1741 var e
= prop
.property_type
.data_type as Enum
;
1742 if (get_ccode_has_type_id (e
)) {
1744 cspec
.call
= new
CCodeIdentifier ("g_param_spec_flags");
1746 cspec
.call
= new
CCodeIdentifier ("g_param_spec_enum");
1748 cspec
.add_argument (new
CCodeIdentifier (get_ccode_type_id (e
)));
1751 cspec
.call
= new
CCodeIdentifier ("g_param_spec_uint");
1752 cspec
.add_argument (new
CCodeConstant ("0"));
1753 cspec
.add_argument (new
CCodeConstant ("G_MAXUINT"));
1755 cspec
.call
= new
CCodeIdentifier ("g_param_spec_int");
1756 cspec
.add_argument (new
CCodeConstant ("G_MININT"));
1757 cspec
.add_argument (new
CCodeConstant ("G_MAXINT"));
1761 if (prop
.initializer
!= null) {
1762 cspec
.add_argument ((CCodeExpression
) get_ccodenode (prop
.initializer
));
1764 cspec
.add_argument (new
CCodeConstant (get_ccode_default_value (prop
.property_type
.data_type
)));
1766 } else if (prop
.property_type
.data_type is Struct
) {
1767 var st
= (Struct
) prop
.property_type
.data_type
;
1768 var type_id
= get_ccode_type_id (st
);
1769 if (type_id
== "G_TYPE_INT") {
1770 cspec
.call
= new
CCodeIdentifier ("g_param_spec_int");
1771 cspec
.add_argument (new
CCodeConstant ("G_MININT"));
1772 cspec
.add_argument (new
CCodeConstant ("G_MAXINT"));
1773 if (prop
.initializer
!= null) {
1774 cspec
.add_argument ((CCodeExpression
) get_ccodenode (prop
.initializer
));
1776 cspec
.add_argument (new
CCodeConstant ("0"));
1778 } else if (type_id
== "G_TYPE_UINT") {
1779 cspec
.call
= new
CCodeIdentifier ("g_param_spec_uint");
1780 cspec
.add_argument (new
CCodeConstant ("0"));
1781 cspec
.add_argument (new
CCodeConstant ("G_MAXUINT"));
1782 if (prop
.initializer
!= null) {
1783 cspec
.add_argument ((CCodeExpression
) get_ccodenode (prop
.initializer
));
1785 cspec
.add_argument (new
CCodeConstant ("0U"));
1787 } else if (type_id
== "G_TYPE_INT64") {
1788 cspec
.call
= new
CCodeIdentifier ("g_param_spec_int64");
1789 cspec
.add_argument (new
CCodeConstant ("G_MININT64"));
1790 cspec
.add_argument (new
CCodeConstant ("G_MAXINT64"));
1791 if (prop
.initializer
!= null) {
1792 cspec
.add_argument ((CCodeExpression
) get_ccodenode (prop
.initializer
));
1794 cspec
.add_argument (new
CCodeConstant ("0"));
1796 } else if (type_id
== "G_TYPE_UINT64") {
1797 cspec
.call
= new
CCodeIdentifier ("g_param_spec_uint64");
1798 cspec
.add_argument (new
CCodeConstant ("0"));
1799 cspec
.add_argument (new
CCodeConstant ("G_MAXUINT64"));
1800 if (prop
.initializer
!= null) {
1801 cspec
.add_argument ((CCodeExpression
) get_ccodenode (prop
.initializer
));
1803 cspec
.add_argument (new
CCodeConstant ("0U"));
1805 } else if (type_id
== "G_TYPE_LONG") {
1806 cspec
.call
= new
CCodeIdentifier ("g_param_spec_long");
1807 cspec
.add_argument (new
CCodeConstant ("G_MINLONG"));
1808 cspec
.add_argument (new
CCodeConstant ("G_MAXLONG"));
1809 if (prop
.initializer
!= null) {
1810 cspec
.add_argument ((CCodeExpression
) get_ccodenode (prop
.initializer
));
1812 cspec
.add_argument (new
CCodeConstant ("0L"));
1814 } else if (type_id
== "G_TYPE_ULONG") {
1815 cspec
.call
= new
CCodeIdentifier ("g_param_spec_ulong");
1816 cspec
.add_argument (new
CCodeConstant ("0"));
1817 cspec
.add_argument (new
CCodeConstant ("G_MAXULONG"));
1818 if (prop
.initializer
!= null) {
1819 cspec
.add_argument ((CCodeExpression
) get_ccodenode (prop
.initializer
));
1821 cspec
.add_argument (new
CCodeConstant ("0UL"));
1823 } else if (type_id
== "G_TYPE_BOOLEAN") {
1824 cspec
.call
= new
CCodeIdentifier ("g_param_spec_boolean");
1825 if (prop
.initializer
!= null) {
1826 cspec
.add_argument ((CCodeExpression
) get_ccodenode (prop
.initializer
));
1828 cspec
.add_argument (new
CCodeConstant ("FALSE"));
1830 } else if (type_id
== "G_TYPE_CHAR") {
1831 cspec
.call
= new
CCodeIdentifier ("g_param_spec_char");
1832 cspec
.add_argument (new
CCodeConstant ("G_MININT8"));
1833 cspec
.add_argument (new
CCodeConstant ("G_MAXINT8"));
1834 if (prop
.initializer
!= null) {
1835 cspec
.add_argument ((CCodeExpression
) get_ccodenode (prop
.initializer
));
1837 cspec
.add_argument (new
CCodeConstant ("0"));
1839 } else if (type_id
== "G_TYPE_UCHAR") {
1840 cspec
.call
= new
CCodeIdentifier ("g_param_spec_uchar");
1841 cspec
.add_argument (new
CCodeConstant ("0"));
1842 cspec
.add_argument (new
CCodeConstant ("G_MAXUINT8"));
1843 if (prop
.initializer
!= null) {
1844 cspec
.add_argument ((CCodeExpression
) get_ccodenode (prop
.initializer
));
1846 cspec
.add_argument (new
CCodeConstant ("0"));
1848 } else if (type_id
== "G_TYPE_FLOAT") {
1849 cspec
.call
= new
CCodeIdentifier ("g_param_spec_float");
1850 cspec
.add_argument (new
CCodeConstant ("-G_MAXFLOAT"));
1851 cspec
.add_argument (new
CCodeConstant ("G_MAXFLOAT"));
1852 if (prop
.initializer
!= null) {
1853 cspec
.add_argument ((CCodeExpression
) get_ccodenode (prop
.initializer
));
1855 cspec
.add_argument (new
CCodeConstant ("0.0F"));
1857 } else if (type_id
== "G_TYPE_DOUBLE") {
1858 cspec
.call
= new
CCodeIdentifier ("g_param_spec_double");
1859 cspec
.add_argument (new
CCodeConstant ("-G_MAXDOUBLE"));
1860 cspec
.add_argument (new
CCodeConstant ("G_MAXDOUBLE"));
1861 if (prop
.initializer
!= null) {
1862 cspec
.add_argument ((CCodeExpression
) get_ccodenode (prop
.initializer
));
1864 cspec
.add_argument (new
CCodeConstant ("0.0"));
1866 } else if (type_id
== "G_TYPE_GTYPE") {
1867 cspec
.call
= new
CCodeIdentifier ("g_param_spec_gtype");
1868 if (prop
.initializer
!= null) {
1869 cspec
.add_argument ((CCodeExpression
) get_ccodenode (prop
.initializer
));
1871 cspec
.add_argument (new
CCodeConstant ("G_TYPE_NONE"));
1874 cspec
.call
= new
CCodeIdentifier ("g_param_spec_boxed");
1875 cspec
.add_argument (new
CCodeIdentifier (type_id
));
1877 } else if (prop
.property_type is ArrayType
&& ((ArrayType
)prop
.property_type
).element_type
.data_type
== string_type
.data_type
) {
1878 cspec
.call
= new
CCodeIdentifier ("g_param_spec_boxed");
1879 cspec
.add_argument (new
CCodeIdentifier ("G_TYPE_STRV"));
1881 cspec
.call
= new
CCodeIdentifier ("g_param_spec_pointer");
1884 var pflags
= "G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB";
1885 if (prop
.get_accessor
!= null && prop
.get_accessor
.access
!= SymbolAccessibility
.PRIVATE
) {
1886 pflags
= "%s%s".printf (pflags
, " | G_PARAM_READABLE");
1888 if (prop
.set_accessor
!= null && prop
.set_accessor
.access
!= SymbolAccessibility
.PRIVATE
) {
1889 pflags
= "%s%s".printf (pflags
, " | G_PARAM_WRITABLE");
1890 if (prop
.set_accessor
.construction
) {
1891 if (prop
.set_accessor
.writable
) {
1892 pflags
= "%s%s".printf (pflags
, " | G_PARAM_CONSTRUCT");
1894 pflags
= "%s%s".printf (pflags
, " | G_PARAM_CONSTRUCT_ONLY");
1898 if (prop
.version
.deprecated
) {
1899 pflags
= "%s%s".printf (pflags
, " | G_PARAM_DEPRECATED");
1901 cspec
.add_argument (new
CCodeConstant (pflags
));
1906 public override void generate_interface_declaration (Interface iface
, CCodeFile decl_space
) {
1907 if (add_symbol_declaration (decl_space
, iface
, get_ccode_name (iface
))) {
1911 foreach (DataType prerequisite
in iface
.get_prerequisites ()) {
1912 var prereq_cl
= prerequisite
.data_type as Class
;
1913 var prereq_iface
= prerequisite
.data_type as Interface
;
1914 if (prereq_cl
!= null) {
1915 generate_class_declaration (prereq_cl
, decl_space
);
1916 } else if (prereq_iface
!= null) {
1917 generate_interface_declaration (prereq_iface
, decl_space
);
1921 var type_struct
= new
CCodeStruct ("_%s".printf (get_ccode_type_name (iface
)));
1923 decl_space
.add_type_declaration (new
CCodeNewline ());
1924 var macro
= "(%s_get_type ())".printf (get_ccode_lower_case_name (iface
, null));
1925 decl_space
.add_type_declaration (new
CCodeMacroReplacement (get_ccode_type_id (iface
), macro
));
1927 macro
= "(G_TYPE_CHECK_INSTANCE_CAST ((obj), %s, %s))".printf (get_ccode_type_id (iface
), get_ccode_name (iface
));
1928 decl_space
.add_type_declaration (new
CCodeMacroReplacement ("%s(obj)".printf (get_ccode_upper_case_name (iface
, null)), macro
));
1930 macro
= "(G_TYPE_CHECK_INSTANCE_TYPE ((obj), %s))".printf (get_ccode_type_id (iface
));
1931 decl_space
.add_type_declaration (new
CCodeMacroReplacement ("%s(obj)".printf (get_ccode_type_check_function (iface
)), macro
));
1933 macro
= "(G_TYPE_INSTANCE_GET_INTERFACE ((obj), %s, %s))".printf (get_ccode_type_id (iface
), get_ccode_type_name (iface
));
1934 decl_space
.add_type_declaration (new
CCodeMacroReplacement ("%s_GET_INTERFACE(obj)".printf (get_ccode_upper_case_name (iface
, null)), macro
));
1935 decl_space
.add_type_declaration (new
CCodeNewline ());
1937 decl_space
.add_type_declaration (new
CCodeTypeDefinition ("struct _%s".printf (get_ccode_name (iface
)), new
CCodeVariableDeclarator (get_ccode_name (iface
))));
1938 decl_space
.add_type_declaration (new
CCodeTypeDefinition ("struct %s".printf (type_struct
.name
), new
CCodeVariableDeclarator (get_ccode_type_name (iface
))));
1940 type_struct
.add_field ("GTypeInterface", "parent_iface");
1942 if (iface
.get_attribute ("GenericAccessors") != null) {
1943 foreach (TypeParameter p
in iface
.get_type_parameters ()) {
1944 string method_name
= "get_%s_type".printf (p
.name
.down ());
1945 var vdeclarator
= new
CCodeFunctionDeclarator (method_name
);
1946 var this_type
= get_data_type_for_symbol (iface
);
1947 vdeclarator
.add_parameter (new
CCodeParameter ("self", get_ccode_name (this_type
)));
1949 var vdecl
= new
CCodeDeclaration ("GType");
1950 vdecl
.add_declarator (vdeclarator
);
1951 type_struct
.add_declaration (vdecl
);
1953 method_name
= "get_%s_dup_func".printf (p
.name
.down ());
1954 vdeclarator
= new
CCodeFunctionDeclarator (method_name
);
1955 this_type
= get_data_type_for_symbol (iface
);
1956 vdeclarator
.add_parameter (new
CCodeParameter ("self", get_ccode_name (this_type
)));
1958 vdecl
= new
CCodeDeclaration ("GBoxedCopyFunc");
1959 vdecl
.add_declarator (vdeclarator
);
1960 type_struct
.add_declaration (vdecl
);
1962 method_name
= "get_%s_destroy_func".printf (p
.name
.down ());
1963 vdeclarator
= new
CCodeFunctionDeclarator (method_name
);
1964 this_type
= get_data_type_for_symbol (iface
);
1965 vdeclarator
.add_parameter (new
CCodeParameter ("self", get_ccode_name (this_type
)));
1967 vdecl
= new
CCodeDeclaration ("GDestroyNotify");
1968 vdecl
.add_declarator (vdeclarator
);
1969 type_struct
.add_declaration (vdecl
);
1973 foreach (Symbol sym
in iface
.get_virtuals ()) {
1977 if ((m
= sym as Method
) != null) {
1978 generate_virtual_method_declaration (m
, decl_space
, type_struct
);
1979 } else if ((sig
= sym as Signal
) != null) {
1980 if (sig
.default_handler
!= null) {
1981 generate_virtual_method_declaration (sig
.default_handler
, decl_space
, type_struct
);
1983 } else if ((prop
= sym as Property
) != null) {
1984 generate_type_declaration (prop
.property_type
, decl_space
);
1986 var t
= (ObjectTypeSymbol
) prop
.parent_symbol
;
1988 bool returns_real_struct
= prop
.property_type
.is_real_non_null_struct_type ();
1990 var this_type
= new
ObjectType (t
);
1991 var cselfparam
= new
CCodeParameter ("self", get_ccode_name (this_type
));
1993 if (prop
.get_accessor
!= null) {
1994 var vdeclarator
= new
CCodeFunctionDeclarator ("get_%s".printf (prop
.name
));
1995 vdeclarator
.add_parameter (cselfparam
);
1996 string creturn_type
;
1997 if (returns_real_struct
) {
1998 var cvalueparam
= new
CCodeParameter ("value", "%s *".printf (get_ccode_name (prop
.get_accessor
.value_type
)));
1999 vdeclarator
.add_parameter (cvalueparam
);
2000 creturn_type
= "void";
2002 creturn_type
= get_ccode_name (prop
.get_accessor
.value_type
);
2005 var array_type
= prop
.property_type as ArrayType
;
2006 if (array_type
!= null) {
2007 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
2008 vdeclarator
.add_parameter (new
CCodeParameter (get_array_length_cname ("result", dim
), "int*"));
2012 var vdecl
= new
CCodeDeclaration (creturn_type
);
2013 vdecl
.add_declarator (vdeclarator
);
2014 type_struct
.add_declaration (vdecl
);
2016 if (prop
.set_accessor
!= null) {
2017 var vdeclarator
= new
CCodeFunctionDeclarator ("set_%s".printf (prop
.name
));
2018 vdeclarator
.add_parameter (cselfparam
);
2019 if (returns_real_struct
) {
2020 var cvalueparam
= new
CCodeParameter ("value", "%s *".printf (get_ccode_name (prop
.set_accessor
.value_type
)));
2021 vdeclarator
.add_parameter (cvalueparam
);
2023 var cvalueparam
= new
CCodeParameter ("value", get_ccode_name (prop
.set_accessor
.value_type
));
2024 vdeclarator
.add_parameter (cvalueparam
);
2027 var array_type
= prop
.property_type as ArrayType
;
2028 if (array_type
!= null) {
2029 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
2030 vdeclarator
.add_parameter (new
CCodeParameter (get_array_length_cname ("value", dim
), "int"));
2034 var vdecl
= new
CCodeDeclaration ("void");
2035 vdecl
.add_declarator (vdeclarator
);
2036 type_struct
.add_declaration (vdecl
);
2039 assert_not_reached ();
2043 decl_space
.add_type_definition (type_struct
);
2045 var type_fun
= new
InterfaceRegisterFunction (iface
, context
);
2046 type_fun
.init_from_type (in_plugin
, true);
2047 decl_space
.add_type_member_declaration (type_fun
.get_declaration ());
2050 public override void visit_interface (Interface iface
) {
2051 push_context (new
EmitContext (iface
));
2052 push_line (iface
.source_reference
);
2054 var old_signal_enum
= signal_enum
;
2056 if (get_ccode_name (iface
).length
< 3) {
2058 Report
.error (iface
.source_reference
, "Interface name `%s' is too short".printf (get_ccode_name (iface
)));
2062 signal_enum
= new
CCodeEnum ();
2064 generate_interface_declaration (iface
, cfile
);
2065 if (!iface
.is_internal_symbol ()) {
2066 generate_interface_declaration (iface
, header_file
);
2068 if (!iface
.is_private_symbol ()) {
2069 generate_interface_declaration (iface
, internal_header_file
);
2072 iface
.accept_children (this
);
2074 if (iface
.get_signals ().size
> 0) {
2075 var last_signal
= "%s_NUM_SIGNALS".printf (get_ccode_upper_case_name (iface
));
2076 signal_enum
.add_value (new
CCodeEnumValue (last_signal
));
2077 cfile
.add_type_declaration (signal_enum
);
2079 var signal_array_decl
= new
CCodeDeclaration ("guint");
2080 signal_array_decl
.modifiers
|= CCodeModifiers
.STATIC
;
2081 signal_array_decl
.add_declarator (new
CCodeVariableDeclarator ("%s_signals".printf (get_ccode_lower_case_name (iface
)), new
CCodeConstant ("{0}"), new CCodeDeclaratorSuffix
.with_array (new
CCodeIdentifier (last_signal
))));
2082 cfile
.add_type_declaration (signal_array_decl
);
2085 add_interface_base_init_function (iface
);
2087 if (iface
.comment
!= null) {
2088 cfile
.add_type_member_definition (new
CCodeComment (iface
.comment
.content
));
2091 var type_fun
= new
InterfaceRegisterFunction (iface
, context
);
2092 type_fun
.init_from_type (in_plugin
, false);
2093 cfile
.add_type_member_declaration (type_fun
.get_source_declaration ());
2094 cfile
.add_type_member_definition (type_fun
.get_definition ());
2096 signal_enum
= old_signal_enum
;
2102 private void add_interface_base_init_function (Interface iface
) {
2103 push_context (new
EmitContext (iface
));
2105 var base_init
= new
CCodeFunction ("%s_base_init".printf (get_ccode_lower_case_name (iface
, null)), "void");
2106 base_init
.add_parameter (new
CCodeParameter ("iface", "%sIface *".printf (get_ccode_name (iface
))));
2107 base_init
.modifiers
= CCodeModifiers
.STATIC
;
2109 push_function (base_init
);
2111 /* make sure not to run the initialization code twice */
2112 ccode
.add_declaration (get_ccode_name (bool_type
), new
CCodeVariableDeclarator ("initialized", new
CCodeConstant ("FALSE")), CCodeModifiers
.STATIC
);
2113 ccode
.open_if (new
CCodeUnaryExpression (CCodeUnaryOperator
.LOGICAL_NEGATION
, new
CCodeIdentifier ("initialized")));
2115 ccode
.add_assignment (new
CCodeIdentifier ("initialized"), new
CCodeConstant ("TRUE"));
2117 if (iface
.is_subtype_of (gobject_type
)) {
2118 /* create properties */
2119 var props
= iface
.get_properties ();
2120 foreach (Property prop
in props
) {
2121 if (prop
.is_abstract
) {
2122 if (!is_gobject_property (prop
)) {
2126 if (prop
.comment
!= null) {
2127 ccode
.add_statement (new
CCodeComment (prop
.comment
.content
));
2130 var cinst
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_object_interface_install_property"));
2131 cinst
.add_argument (new
CCodeIdentifier ("iface"));
2132 cinst
.add_argument (get_param_spec (prop
));
2134 ccode
.add_expression (cinst
);
2139 var ciface
= new
CCodeIdentifier ("iface");
2141 /* connect default signal handlers */
2142 foreach (Signal sig
in iface
.get_signals ()) {
2143 if (sig
.default_handler
== null) {
2146 var cname
= get_ccode_real_name (sig
.default_handler
);
2147 ccode
.add_assignment (new CCodeMemberAccess
.pointer (ciface
, get_ccode_vfunc_name (sig
.default_handler
)), new
CCodeIdentifier (cname
));
2150 /* create signals */
2151 foreach (Signal sig
in iface
.get_signals ()) {
2152 if (sig
.comment
!= null) {
2153 ccode
.add_statement (new
CCodeComment (sig
.comment
.content
));
2155 ccode
.add_expression (get_signal_creation (sig
, iface
));
2158 // connect default implementations
2159 foreach (Method m
in iface
.get_methods ()) {
2161 var cname
= get_ccode_real_name (m
);
2162 ccode
.add_assignment (new CCodeMemberAccess
.pointer (ciface
, get_ccode_vfunc_name (m
)), new
CCodeIdentifier (cname
));
2164 ccode
.add_assignment (new CCodeMemberAccess
.pointer (ciface
, get_ccode_finish_vfunc_name (m
)), new
CCodeIdentifier (get_ccode_finish_real_name (m
)));
2169 foreach (Property prop
in iface
.get_properties ()) {
2170 if (prop
.is_virtual
) {
2171 if (prop
.get_accessor
!= null) {
2172 string cname
= CCodeBaseModule
.get_ccode_real_name (prop
.get_accessor
);
2173 ccode
.add_assignment (new CCodeMemberAccess
.pointer (ciface
, "get_%s".printf (prop
.name
)), new
CCodeIdentifier (cname
));
2175 if (prop
.set_accessor
!= null) {
2176 string cname
= CCodeBaseModule
.get_ccode_real_name (prop
.set_accessor
);
2177 ccode
.add_assignment (new CCodeMemberAccess
.pointer (ciface
, "set_%s".printf (prop
.name
)), new
CCodeIdentifier (cname
));
2186 cfile
.add_function (base_init
);
2189 public override void visit_struct (Struct st
) {
2190 base.visit_struct (st
);
2192 if (get_ccode_has_type_id (st
)) {
2193 push_line (st
.source_reference
);
2194 var type_fun
= new
StructRegisterFunction (st
, context
);
2195 type_fun
.init_from_type (false, false);
2196 cfile
.add_type_member_definition (type_fun
.get_definition ());
2201 public override void visit_enum (Enum en
) {
2202 base.visit_enum (en
);
2204 if (get_ccode_has_type_id (en
)) {
2205 push_line (en
.source_reference
);
2206 var type_fun
= new
EnumRegisterFunction (en
, context
);
2207 type_fun
.init_from_type (false, false);
2208 cfile
.add_type_member_definition (type_fun
.get_definition ());
2213 public override void visit_method_call (MethodCall expr
) {
2214 var ma
= expr
.call as MemberAccess
;
2215 var mtype
= expr
.call
.value_type as MethodType
;
2216 if (mtype
== null || ma
== null || ma
.inner
== null ||
2217 !(ma
.inner
.value_type is EnumValueType
) || !get_ccode_has_type_id (ma
.inner
.value_type
.data_type
) ||
2218 mtype
.method_symbol
!= ((EnumValueType
) ma
.inner
.value_type
).get_to_string_method ()) {
2219 base.visit_method_call (expr
);
2222 // to_string() on a gtype enum
2224 bool is_flags
= ((Enum
) ((EnumValueType
) ma
.inner
.value_type
).type_symbol
).is_flags
;
2226 push_line (expr
.source_reference
);
2227 var temp_var
= get_temp_variable (new
CType (is_flags ?
"GFlagsValue*" : "GEnumValue*"), false, expr
, false);
2228 emit_temp_var (temp_var
);
2230 var class_ref
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_type_class_ref"));
2231 class_ref
.add_argument (new
CCodeIdentifier (get_ccode_type_id (ma
.inner
.value_type
)));
2232 var get_value
= new
CCodeFunctionCall (new
CCodeIdentifier (is_flags ?
"g_flags_get_first_value" : "g_enum_get_value"));
2233 get_value
.add_argument (class_ref
);
2234 get_value
.add_argument ((CCodeExpression
) get_ccodenode (((MemberAccess
) expr
.call
).inner
));
2236 ccode
.add_assignment (get_variable_cexpression (temp_var
.name
), get_value
);
2237 var is_null_value
= new
CCodeBinaryExpression (CCodeBinaryOperator
.INEQUALITY
, get_variable_cexpression (temp_var
.name
), new
CCodeIdentifier ("NULL"));
2238 set_cvalue (expr
, new
CCodeConditionalExpression (is_null_value
, new CCodeMemberAccess
.pointer (get_variable_cexpression (temp_var
.name
), "value_name"), new
CCodeIdentifier ("NULL")));
2242 public override void visit_property (Property prop
) {
2243 var cl
= current_type_symbol as Class
;
2244 var st
= current_type_symbol as Struct
;
2246 var base_prop
= prop
;
2247 if (prop
.base_property
!= null) {
2248 base_prop
= prop
.base_property
;
2249 } else if (prop
.base_interface_property
!= null) {
2250 base_prop
= prop
.base_interface_property
;
2253 if (base_prop
.get_attribute ("NoAccessorMethod") == null &&
2254 prop
.name
== "type" && ((cl
!= null && !cl
.is_compact
) || (st
!= null && get_ccode_has_type_id (st
)))) {
2255 Report
.error (prop
.source_reference
, "Property 'type' not allowed");
2258 base.visit_property (prop
);
2261 public override void create_type_check_statement (CodeNode method_node
, DataType ret_type
, TypeSymbol t
, bool non_null
, string var_name
) {
2262 var ccheck
= new
CCodeFunctionCall ();
2264 if (!context
.assert
) {
2266 } else if (context
.checking
&& ((t is Class
&& !((Class
) t
).is_compact
) || t is Interface
)) {
2267 var ctype_check
= new
CCodeFunctionCall (new
CCodeIdentifier (get_ccode_type_check_function (t
)));
2268 ctype_check
.add_argument (new
CCodeIdentifier (var_name
));
2270 CCodeExpression cexpr
= ctype_check
;
2272 var cnull
= new
CCodeBinaryExpression (CCodeBinaryOperator
.EQUALITY
, new
CCodeIdentifier (var_name
), new
CCodeConstant ("NULL"));
2274 cexpr
= new
CCodeBinaryExpression (CCodeBinaryOperator
.OR
, cnull
, ctype_check
);
2276 ccheck
.add_argument (cexpr
);
2277 } else if (!non_null
|| (t is Struct
&& ((Struct
) t
).is_simple_type ())) {
2279 } else if (t
== glist_type
|| t
== gslist_type
) {
2280 // NULL is empty list
2283 var cnonnull
= new
CCodeBinaryExpression (CCodeBinaryOperator
.INEQUALITY
, new
CCodeIdentifier (var_name
), new
CCodeConstant ("NULL"));
2284 ccheck
.add_argument (cnonnull
);
2287 var cm
= method_node as CreationMethod
;
2288 if (cm
!= null && cm
.parent_symbol is ObjectTypeSymbol
) {
2289 ccheck
.call
= new
CCodeIdentifier ("g_return_val_if_fail");
2290 ccheck
.add_argument (new
CCodeConstant ("NULL"));
2291 } else if (ret_type is VoidType
) {
2293 ccheck
.call
= new
CCodeIdentifier ("g_return_if_fail");
2295 ccheck
.call
= new
CCodeIdentifier ("g_return_val_if_fail");
2297 var cdefault
= default_value_for_type (ret_type
, false);
2298 if (cdefault
!= null) {
2299 ccheck
.add_argument (cdefault
);
2300 } else if (ret_type
.data_type is Struct
&& !((Struct
) ret_type
.data_type
).is_simple_type ()) {
2301 ccheck
.add_argument (new
CCodeIdentifier ("result"));
2307 ccode
.add_expression (ccheck
);