Release 0.7.8
[vala-lang.git] / codegen / valaclassregisterfunction.vala
blob1e299e780adfe080ca439b6d028c1cea2c290250
1 /* valaclassregisterfunction.vala
3 * Copyright (C) 2006-2008 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
19 * Author:
20 * Jürg Billeter <j@bitron.ch>
23 using GLib;
25 /**
26 * C function to register a class at runtime.
28 public class Vala.ClassRegisterFunction : TypeRegisterFunction {
29 /**
30 * Specifies the class to be registered.
32 public weak Class class_reference { get; set; }
34 /**
35 * Creates a new C function to register the specified class at runtime.
37 * @param cl a class
38 * @return newly created class register function
40 public ClassRegisterFunction (Class cl, CodeContext context) {
41 class_reference = cl;
42 this.context = context;
45 public override TypeSymbol get_type_declaration () {
46 return class_reference;
49 public override string get_type_struct_name () {
50 return "%sClass".printf (class_reference.get_cname ());
53 public override string get_base_init_func_name () {
54 if (class_reference.class_constructor != null || class_reference.has_class_private_fields) {
55 return "%s_base_init".printf (class_reference.get_lower_case_cname (null));
56 } else {
57 return "NULL";
61 public override string get_class_finalize_func_name () {
62 if (class_reference.static_destructor != null) {
63 return "%s_class_finalize".printf (class_reference.get_lower_case_cname (null));
64 } else {
65 return "NULL";
69 public override string get_base_finalize_func_name () {
70 if (class_reference.class_destructor != null || class_reference.has_class_private_fields) {
71 return "%s_base_finalize".printf (class_reference.get_lower_case_cname (null));
72 } else {
73 return "NULL";
77 public override string get_class_init_func_name () {
78 return "%s_class_init".printf (class_reference.get_lower_case_cname (null));
81 public override string get_instance_struct_size () {
82 return "sizeof (%s)".printf (class_reference.get_cname ());
85 public override string get_instance_init_func_name () {
86 return "%s_instance_init".printf (class_reference.get_lower_case_cname (null));
89 public override string get_parent_type_name () {
90 return class_reference.base_class.get_type_id ();
93 public override string get_type_flags () {
94 if (class_reference.is_abstract) {
95 return "G_TYPE_FLAG_ABSTRACT";
96 } else {
97 return "0";
101 public override SymbolAccessibility get_accessibility () {
102 return class_reference.access;
105 public override string? get_gtype_value_table_init_function_name () {
106 bool is_fundamental = !class_reference.is_compact && class_reference.base_class == null;
107 if ( is_fundamental )
108 return "%s_init".printf (class_reference.get_lower_case_cname ("value_"));
110 return null;
113 public override string? get_gtype_value_table_free_function_name () {
114 bool is_fundamental = !class_reference.is_compact && class_reference.base_class == null;
115 if ( is_fundamental )
116 return "%s_free_value".printf (class_reference.get_lower_case_cname ("value_"));
118 return null;
121 public override string? get_gtype_value_table_copy_function_name () {
122 bool is_fundamental = !class_reference.is_compact && class_reference.base_class == null;
123 if ( is_fundamental )
124 return "%s_copy_value".printf (class_reference.get_lower_case_cname ("value_"));
126 return null;
129 public override string? get_gtype_value_table_peek_pointer_function_name () {
130 bool is_fundamental = !class_reference.is_compact && class_reference.base_class == null;
131 if ( is_fundamental )
132 return "%s_peek_pointer".printf (class_reference.get_lower_case_cname ("value_"));
134 return null;
137 public override string? get_gtype_value_table_collect_value_function_name () {
138 bool is_fundamental = !class_reference.is_compact && class_reference.base_class == null;
139 if ( is_fundamental )
140 return "%s_collect_value".printf (class_reference.get_lower_case_cname ("value_"));
142 return null;
145 public override string? get_gtype_value_table_lcopy_value_function_name () {
146 bool is_fundamental = !class_reference.is_compact && class_reference.base_class == null;
147 if ( is_fundamental )
148 return "%s_lcopy_value".printf (class_reference.get_lower_case_cname ("value_"));
150 return null;
153 public override CCodeFragment get_type_interface_init_declaration () {
154 var frag = new CCodeFragment ();
156 foreach (DataType base_type in class_reference.get_base_types ()) {
157 if (!(base_type.data_type is Interface)) {
158 continue;
161 var iface = (Interface) base_type.data_type;
163 var iface_info_name = "%s_info".printf (iface.get_lower_case_cname (null));
165 var ctypedecl = new CCodeDeclaration ("const GInterfaceInfo");
166 ctypedecl.modifiers = CCodeModifiers.STATIC;
167 ctypedecl.add_declarator (new CCodeVariableDeclarator (iface_info_name, new CCodeConstant ("{ (GInterfaceInitFunc) %s_%s_interface_init, (GInterfaceFinalizeFunc) NULL, NULL}".printf (class_reference.get_lower_case_cname (null), iface.get_lower_case_cname (null)))));
168 frag.append (ctypedecl);
171 return frag;
174 public override CCodeFragment get_type_interface_init_statements () {
175 var frag = new CCodeFragment ();
177 foreach (DataType base_type in class_reference.get_base_types ()) {
178 if (!(base_type.data_type is Interface)) {
179 continue;
182 var iface = (Interface) base_type.data_type;
184 var iface_info_name = "%s_info".printf (iface.get_lower_case_cname (null));
186 var reg_call = new CCodeFunctionCall (new CCodeIdentifier ("g_type_add_interface_static"));
187 reg_call.add_argument (new CCodeIdentifier ("%s_type_id".printf (class_reference.get_lower_case_cname (null))));
188 reg_call.add_argument (new CCodeIdentifier (iface.get_type_id ()));
189 reg_call.add_argument (new CCodeIdentifier ("&%s".printf (iface_info_name)));
190 frag.append (new CCodeExpressionStatement (reg_call));
193 return frag;