1 /* valatyperegisterfunction.vala
3 * Copyright (C) 2006-2010 Jürg Billeter
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 * Jürg Billeter <j@bitron.ch>
26 * C function to register a type at runtime.
28 public abstract class Vala
.TypeRegisterFunction
{
29 CCodeFragment source_declaration_fragment
= new
CCodeFragment ();
30 CCodeFragment declaration_fragment
= new
CCodeFragment ();
31 CCodeFragment definition_fragment
= new
CCodeFragment ();
33 public CodeContext context
{ get; set; }
36 * Constructs the C function from the specified type.
38 public void init_from_type (bool plugin
) {
39 bool use_thread_safe
= context
.require_glib_version (2, 14) && !plugin
;
41 bool fundamental
= false;
42 Class cl
= get_type_declaration () as Class
;
43 if (cl
!= null && !cl
.is_compact
&& cl
.base_class
== null) {
47 string type_id_name
= "%s_type_id".printf (get_type_declaration ().get_lower_case_cname (null));
49 var type_block
= new
CCodeBlock ();
50 CCodeDeclaration cdecl
;
51 if (use_thread_safe
) {
52 cdecl
= new
CCodeDeclaration ("gsize");
53 cdecl
.add_declarator (new
CCodeVariableDeclarator (type_id_name
+ "__volatile", new
CCodeConstant ("0")));
55 cdecl
= new
CCodeDeclaration ("GType");
56 cdecl
.add_declarator (new
CCodeVariableDeclarator (type_id_name
, new
CCodeConstant ("0")));
58 cdecl
.modifiers
= CCodeModifiers
.STATIC
;
59 if (use_thread_safe
) {
60 cdecl
.modifiers
|= CCodeModifiers
.VOLATILE
;
63 type_block
.add_statement (cdecl
);
65 source_declaration_fragment
.append (cdecl
);
70 fun
= new
CCodeFunction ("%s_get_type".printf (get_type_declaration ().get_lower_case_cname (null)), "GType");
71 fun
.attributes
= "G_GNUC_CONST";
73 /* Function will not be prototyped anyway */
74 if (get_accessibility () == SymbolAccessibility
.PRIVATE
) {
75 fun
.modifiers
= CCodeModifiers
.STATIC
;
76 // avoid C warning as this function is not always used
77 fun
.attributes
+= " G_GNUC_UNUSED";
80 fun
= new
CCodeFunction ("%s_register_type".printf (get_type_declaration ().get_lower_case_cname (null)), "GType");
81 fun
.add_parameter (new
CCodeFormalParameter ("module", "GTypeModule *"));
83 var get_fun
= new
CCodeFunction ("%s_get_type".printf (get_type_declaration ().get_lower_case_cname (null)), "GType");
84 get_fun
.attributes
= "G_GNUC_CONST";
86 declaration_fragment
.append (get_fun
.copy ());
88 get_fun
.block
= new
CCodeBlock ();
89 get_fun
.block
.add_statement (new
CCodeReturnStatement (new
CCodeIdentifier (type_id_name
)));
91 definition_fragment
.append (get_fun
);
94 string type_value_table_decl_name
= null;
95 var type_init
= new
CCodeBlock ();
98 var cgtypetabledecl
= new
CCodeDeclaration ("const GTypeValueTable");
99 cgtypetabledecl
.modifiers
= CCodeModifiers
.STATIC
;
101 cgtypetabledecl
.add_declarator (new
CCodeVariableDeclarator ( "g_define_type_value_table", new
CCodeConstant ("{ %s, %s, %s, %s, \"p\", %s, \"p\", %s }".printf (get_gtype_value_table_init_function_name (), get_gtype_value_table_free_function_name (), get_gtype_value_table_copy_function_name (), get_gtype_value_table_peek_pointer_function_name (), get_gtype_value_table_collect_value_function_name (), get_gtype_value_table_lcopy_value_function_name ()))));
102 type_value_table_decl_name
= "&g_define_type_value_table";
103 type_init
.add_statement ( cgtypetabledecl
);
106 type_value_table_decl_name
= "NULL";
110 if (get_type_declaration () is ObjectTypeSymbol
) {
111 var ctypedecl
= new
CCodeDeclaration ("const GTypeInfo");
112 ctypedecl
.modifiers
= CCodeModifiers
.STATIC
;
113 ctypedecl
.add_declarator (new
CCodeVariableDeclarator ("g_define_type_info", new
CCodeConstant ("{ sizeof (%s), (GBaseInitFunc) %s, (GBaseFinalizeFunc) %s, (GClassInitFunc) %s, (GClassFinalizeFunc) %s, NULL, %s, 0, (GInstanceInitFunc) %s, %s }".printf (get_type_struct_name (), get_base_init_func_name (), (plugin
) ?
get_base_finalize_func_name () : "NULL", get_class_init_func_name (), get_class_finalize_func_name (), get_instance_struct_size (), get_instance_init_func_name (), type_value_table_decl_name
))));
114 type_init
.add_statement (ctypedecl
);
116 var ctypefundamentaldecl
= new
CCodeDeclaration ("const GTypeFundamentalInfo");
117 ctypefundamentaldecl
.modifiers
= CCodeModifiers
.STATIC
;
118 ctypefundamentaldecl
.add_declarator (new
CCodeVariableDeclarator ("g_define_type_fundamental_info", new
CCodeConstant ("{ (G_TYPE_FLAG_CLASSED | G_TYPE_FLAG_INSTANTIATABLE | G_TYPE_FLAG_DERIVABLE | G_TYPE_FLAG_DEEP_DERIVABLE) }")));
119 type_init
.add_statement (ctypefundamentaldecl
);
123 type_init
.add_statement (get_type_interface_init_declaration ());
125 if (cl
!= null && cl
.has_class_private_fields
&& !context
.require_glib_version (2, 24)) {
126 CCodeFunctionCall quark_reg_call
;
129 quark_reg_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_quark_from_string"));
131 quark_reg_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_quark_from_static_string"));
134 quark_reg_call
.add_argument (new
CCodeConstant ("\"Vala%sClassPrivate\"".printf (get_type_declaration ().get_cname ())));
136 type_init
.add_statement (new
CCodeExpressionStatement (new
CCodeAssignment (new
CCodeIdentifier ("_vala_%s_class_private_quark".printf (get_type_declaration ().get_lower_case_cname ())), quark_reg_call
)));
139 CCodeFunctionCall reg_call
;
140 if (get_type_declaration () is Struct
) {
141 reg_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_boxed_type_register_static"));
142 } else if (get_type_declaration () is Enum
) {
143 var en
= get_type_declaration () as Enum
;
145 reg_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_flags_register_static"));
147 reg_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_enum_register_static"));
149 } else if (fundamental
) {
150 reg_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_type_register_fundamental"));
151 reg_call
.add_argument (new
CCodeFunctionCall (new
CCodeIdentifier ("g_type_fundamental_next")));
152 } else if (!plugin
) {
153 reg_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_type_register_static"));
154 reg_call
.add_argument (new
CCodeIdentifier (get_parent_type_name ()));
156 reg_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_type_module_register_type"));
157 reg_call
.add_argument (new
CCodeIdentifier ("module"));
158 reg_call
.add_argument (new
CCodeIdentifier (get_parent_type_name ()));
160 reg_call
.add_argument (new
CCodeConstant ("\"%s\"".printf (get_type_declaration ().get_cname ())));
161 if (get_type_declaration () is Struct
) {
162 var st
= (Struct
) get_type_declaration ();
163 reg_call
.add_argument (new
CCodeCastExpression (new
CCodeIdentifier (st
.get_dup_function ()), "GBoxedCopyFunc"));
164 reg_call
.add_argument (new
CCodeCastExpression (new
CCodeIdentifier (st
.get_free_function ()), "GBoxedFreeFunc"));
165 } else if (get_type_declaration () is Enum
) {
166 var en
= get_type_declaration () as Enum
;
167 var clist
= new
CCodeInitializerList (); /* or during visit time? */
169 CCodeInitializerList clist_ev
= null;
170 foreach (EnumValue ev
in en
.get_values ()) {
171 clist_ev
= new
CCodeInitializerList ();
172 clist_ev
.append (new
CCodeConstant (ev
.get_cname ()));
173 clist_ev
.append (new
CCodeIdentifier ("\"%s\"".printf (ev
.get_cname ())));
174 clist_ev
.append (ev
.get_canonical_cconstant ());
175 clist
.append (clist_ev
);
178 clist_ev
= new
CCodeInitializerList ();
179 clist_ev
.append (new
CCodeConstant ("0"));
180 clist_ev
.append (new
CCodeConstant ("NULL"));
181 clist_ev
.append (new
CCodeConstant ("NULL"));
182 clist
.append (clist_ev
);
184 var enum_decl
= new
CCodeVariableDeclarator ("values[]", clist
);
187 cdecl
= new
CCodeDeclaration ("const GFlagsValue");
189 cdecl
= new
CCodeDeclaration ("const GEnumValue");
192 cdecl
.add_declarator (enum_decl
);
193 cdecl
.modifiers
= CCodeModifiers
.STATIC
;
195 type_init
.add_statement (cdecl
);
197 reg_call
.add_argument (new
CCodeIdentifier ("values"));
199 reg_call
.add_argument (new
CCodeIdentifier ("&g_define_type_info"));
201 reg_call
.add_argument (new
CCodeIdentifier ("&g_define_type_fundamental_info"));
203 reg_call
.add_argument (new
CCodeConstant (get_type_flags ()));
206 if (use_thread_safe
&& !plugin
) {
207 var temp_decl
= new
CCodeDeclaration ("GType");
208 temp_decl
.add_declarator (new
CCodeVariableDeclarator (type_id_name
, reg_call
));
209 type_init
.add_statement (temp_decl
);
211 type_init
.add_statement (new
CCodeExpressionStatement (new
CCodeAssignment (new
CCodeIdentifier (type_id_name
), reg_call
)));
214 if (cl
!= null && cl
.has_class_private_fields
&& context
.require_glib_version (2, 24)) {
215 CCodeFunctionCall add_class_private_call
;
217 add_class_private_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_type_add_class_private"));
218 add_class_private_call
.add_argument (new
CCodeIdentifier (type_id_name
));
219 add_class_private_call
.add_argument (new
CCodeIdentifier ("sizeof (%sClassPrivate)".printf (get_type_declaration ().get_cname ())));
220 type_init
.add_statement (new
CCodeExpressionStatement (add_class_private_call
));
223 type_init
.add_statement (get_type_interface_init_statements (plugin
));
226 CCodeExpression condition
; // the condition that guards the type initialisation
227 if (use_thread_safe
) {
228 var enter
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_once_init_enter"));
229 enter
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, new
CCodeIdentifier (type_id_name
+ "__volatile")));
232 var leave
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_once_init_leave"));
233 leave
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, new
CCodeIdentifier (type_id_name
+ "__volatile")));
234 leave
.add_argument (new
CCodeIdentifier (type_id_name
));
235 type_init
.add_statement (new
CCodeExpressionStatement (leave
));
237 var id
= new
CCodeIdentifier (type_id_name
);
238 var zero
= new
CCodeConstant ("0");
239 condition
= new
CCodeBinaryExpression (CCodeBinaryOperator
.EQUALITY
, id
, zero
);
242 CCodeExpression cond
;
243 if (use_thread_safe
) {
246 cond
= new
CCodeFunctionCall (new
CCodeIdentifier ("G_UNLIKELY"));
247 (cond as CCodeFunctionCall
).add_argument (condition
);
249 var cif
= new
CCodeIfStatement (cond
, type_init
);
250 type_block
.add_statement (cif
);
252 type_block
= type_init
;
255 if (use_thread_safe
) {
256 type_block
.add_statement (new
CCodeReturnStatement (new
CCodeIdentifier (type_id_name
+ "__volatile")));
258 type_block
.add_statement (new
CCodeReturnStatement (new
CCodeIdentifier (type_id_name
)));
261 declaration_fragment
.append (fun
.copy ());
263 fun
.block
= type_block
;
265 definition_fragment
.append (fun
);
269 * Returns the data type to be registered.
271 * @return type to be registered
273 public abstract TypeSymbol
get_type_declaration ();
276 * Returns the name of the type struct in C code.
278 * @return C struct name
280 public virtual string get_type_struct_name () {
281 assert_not_reached ();
284 * Returns the name of the base_init function in C code.
286 * @return C function name
288 public virtual string get_base_init_func_name () {
289 assert_not_reached ();
293 * Returns the name of the class_finalize function in C code.
295 * @return C function name
297 public virtual string get_class_finalize_func_name () {
298 assert_not_reached ();
302 * Returns the name of the base_finalize function in C code.
304 * @return C function name
306 public virtual string get_base_finalize_func_name () {
307 assert_not_reached ();
311 * Returns the name of the class_init function in C code.
313 * @return C function name
315 public virtual string get_class_init_func_name () {
316 assert_not_reached ();
320 * Returns the size of the instance struct in C code.
322 * @return C instance struct size
324 public virtual string get_instance_struct_size () {
325 assert_not_reached ();
329 * Returns the name of the instance_init function in C code.
331 * @return C function name
333 public virtual string get_instance_init_func_name () {
334 assert_not_reached ();
338 * Returns the name of the parent type in C code.
340 * @return C function name
342 public virtual string get_parent_type_name () {
343 assert_not_reached ();
349 * Returns the C-name of the new generated GTypeValueTable init function or null when not available.
351 * @return C function name
353 public virtual string?
get_gtype_value_table_init_function_name () {
358 * Returns the C-name of the new generated GTypeValueTable peek pointer function or null when not available.
360 * @return C function name
362 public virtual string?
get_gtype_value_table_peek_pointer_function_name () {
367 * Returns the C-name of the new generated GTypeValueTable free function or null when not available.
369 * @return C function name
371 public virtual string?
get_gtype_value_table_free_function_name () {
376 * Returns the C-name of the new generated GTypeValueTable copy function or null when not available.
378 * @return C function name
380 public virtual string?
get_gtype_value_table_copy_function_name () {
385 * Returns the C-name of the new generated GTypeValueTable lcopy function or null when not available.
387 * @return C function name
389 public virtual string?
get_gtype_value_table_lcopy_value_function_name () {
394 * Returns the C-name of the new generated GTypeValueTable collect value function or null when not available.
396 * @return C function name
398 public virtual string?
get_gtype_value_table_collect_value_function_name () {
403 * Returns the set of type flags to be applied when registering.
407 public virtual string get_type_flags () {
412 * Returns additional C declarations to setup interfaces.
414 * @return C declarations
416 public virtual CCodeFragment
get_type_interface_init_declaration () {
417 return new
CCodeFragment ();
421 * Returns additional C initialization statements to setup interfaces.
423 * @return C statements
425 public abstract CCodeFragment
get_type_interface_init_statements (bool plugin
);
427 public CCodeFragment
get_source_declaration () {
428 return source_declaration_fragment
;
432 * Returns the declaration for this type register function in C code.
434 * @return C function declaration fragment
436 public CCodeFragment
get_declaration () {
437 return declaration_fragment
;
441 * Returns the definition for this type register function in C code.
443 * @return C function definition fragment
445 public CCodeFragment
get_definition () {
446 return definition_fragment
;
450 * Returns the accessibility for this type.
452 public abstract SymbolAccessibility
get_accessibility ();