1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
|
/* valaccodestructmodule.vala
*
* Copyright (C) 2006-2009 Jürg Billeter
* Copyright (C) 2006-2008 Raffaele Sandrini
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Author:
* Jürg Billeter <j@bitron.ch>
* Raffaele Sandrini <raffaele@sandrini.ch>
*/
using GLib;
public abstract class Vala.CCodeStructModule : CCodeBaseModule {
public override void generate_struct_declaration (Struct st, CCodeFile decl_space) {
if (add_symbol_declaration (decl_space, st, get_ccode_name (st))) {
return;
}
if (st.base_struct != null) {
generate_struct_declaration (st.base_struct, decl_space);
} else if (!st.external_package) {
// custom simple type structs cannot have a type id which depends on head-allocation
if (st.has_attribute ("SimpleType") && !st.has_attribute_argument ("CCode", "type_id")) {
st.set_attribute_bool ("CCode", "has_type_id", false);
}
}
if (st.is_boolean_type () || st.is_integer_type () || st.is_floating_type ()) {
string typename;
// See GTypeModule.visit_struct()
if (st.base_struct != null) {
typename = get_ccode_name (st.base_struct);
} else if (st.is_boolean_type ()) {
// typedef for boolean types
decl_space.add_include ("stdbool.h");
typename = "bool";
} else if (st.is_integer_type ()) {
// typedef for integral types
decl_space.add_include ("stdint.h");
typename = "%sint%d_t".printf (st.signed ? "" : "u", st.width);
} else if (st.is_floating_type ()) {
// typedef for floating types
typename = (st.width == 64 ? "double" : "float");
} else {
assert_not_reached ();
}
decl_space.add_type_declaration (new CCodeTypeDefinition (typename, new CCodeVariableDeclarator (get_ccode_name (st))));
return;
}
if (context.profile == Profile.GOBJECT) {
if (get_ccode_has_type_id (st)) {
decl_space.add_include ("glib-object.h");
decl_space.add_type_declaration (new CCodeNewline ());
var macro = "(%s_get_type ())".printf (get_ccode_lower_case_name (st, null));
decl_space.add_type_declaration (new CCodeMacroReplacement (get_ccode_type_id (st), macro));
var type_fun = new StructRegisterFunction (st);
type_fun.init_from_type (context, false, true);
decl_space.add_type_member_declaration (type_fun.get_declaration ());
requires_vala_extern = true;
}
}
if (st.base_struct == null) {
decl_space.add_type_declaration (new CCodeTypeDefinition ("struct _%s".printf (get_ccode_name (st)), new CCodeVariableDeclarator (get_ccode_name (st))));
} else {
decl_space.add_type_declaration (new CCodeTypeDefinition (get_ccode_name (st.base_struct), new CCodeVariableDeclarator (get_ccode_name (st))));
}
var instance_struct = new CCodeStruct ("_%s".printf (get_ccode_name (st)));
if (st.version.deprecated) {
if (context.profile == Profile.GOBJECT) {
decl_space.add_include ("glib.h");
}
instance_struct.modifiers |= CCodeModifiers.DEPRECATED;
}
foreach (Field f in st.get_fields ()) {
if (f.binding == MemberBinding.INSTANCE) {
append_field (instance_struct, f, decl_space);
}
}
if (st.base_struct == null) {
decl_space.add_type_definition (instance_struct);
}
if (st.is_simple_type ()) {
return;
}
var function = new CCodeFunction (get_ccode_dup_function (st), get_ccode_name (st) + "*");
if (st.is_private_symbol ()) {
function.modifiers = CCodeModifiers.STATIC;
} else if (context.hide_internal && st.is_internal_symbol ()) {
function.modifiers = CCodeModifiers.INTERNAL;
} else {
function.modifiers |= CCodeModifiers.EXTERN;
requires_vala_extern = true;
}
function.add_parameter (new CCodeParameter ("self", "const " + get_ccode_name (st) + "*"));
decl_space.add_function_declaration (function);
function = new CCodeFunction (get_ccode_free_function (st), "void");
if (st.is_private_symbol ()) {
function.modifiers = CCodeModifiers.STATIC;
} else if (context.hide_internal && st.is_internal_symbol ()) {
function.modifiers = CCodeModifiers.INTERNAL;
} else {
function.modifiers = CCodeModifiers.EXTERN;
requires_vala_extern = true;
}
function.add_parameter (new CCodeParameter ("self", get_ccode_name (st) + "*"));
decl_space.add_function_declaration (function);
if (st.is_disposable ()) {
function = new CCodeFunction (get_ccode_copy_function (st), "void");
if (st.is_private_symbol ()) {
function.modifiers = CCodeModifiers.STATIC;
} else if (context.hide_internal && st.is_internal_symbol ()) {
function.modifiers = CCodeModifiers.INTERNAL;
} else {
function.modifiers = CCodeModifiers.EXTERN;
requires_vala_extern = true;
}
function.add_parameter (new CCodeParameter ("self", "const " + get_ccode_name (st) + "*"));
function.add_parameter (new CCodeParameter ("dest", get_ccode_name (st) + "*"));
decl_space.add_function_declaration (function);
function = new CCodeFunction (get_ccode_destroy_function (st), "void");
if (st.is_private_symbol ()) {
function.modifiers = CCodeModifiers.STATIC;
} else if (context.hide_internal && st.is_internal_symbol ()) {
function.modifiers = CCodeModifiers.INTERNAL;
} else {
function.modifiers = CCodeModifiers.EXTERN;
requires_vala_extern = true;
}
function.add_parameter (new CCodeParameter ("self", get_ccode_name (st) + "*"));
decl_space.add_function_declaration (function);
}
if (context.profile == Profile.GOBJECT) {
generate_auto_cleanup_clear (st, decl_space);
}
}
void generate_auto_cleanup_clear (Struct st, CCodeFile decl_space) {
if (st.is_disposable ()
&& (context.header_filename == null|| decl_space.file_type == CCodeFileType.PUBLIC_HEADER
|| (decl_space.file_type == CCodeFileType.INTERNAL_HEADER && st.is_internal_symbol ()))) {
string auto_cleanup_clear_func = get_ccode_destroy_function (st);
if (auto_cleanup_clear_func == null || auto_cleanup_clear_func == "") {
Report.error (st.source_reference, "internal error: auto_cleanup_clear_func not available");
}
decl_space.add_type_member_declaration (new CCodeIdentifier ("G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC (%s, %s)".printf (get_ccode_name (st), auto_cleanup_clear_func)));
decl_space.add_type_member_declaration (new CCodeNewline ());
}
}
public override void visit_struct (Struct st) {
push_context (new EmitContext (st));
push_line (st.source_reference);
var old_instance_finalize_context = instance_finalize_context;
instance_finalize_context = new EmitContext ();
generate_struct_declaration (st, cfile);
if (!st.is_internal_symbol ()) {
generate_struct_declaration (st, header_file);
}
if (!st.is_private_symbol ()) {
generate_struct_declaration (st, internal_header_file);
}
if (!st.is_boolean_type () && !st.is_integer_type () && !st.is_floating_type ()) {
if (st.is_disposable ()) {
begin_struct_destroy_function (st);
}
}
st.accept_children (this);
if (!st.is_boolean_type () && !st.is_integer_type () && !st.is_floating_type ()) {
if (st.is_disposable ()) {
add_struct_copy_function (st);
add_struct_destroy_function (st);
}
if (!st.is_simple_type ()) {
add_struct_dup_function (st);
add_struct_free_function (st);
}
}
instance_finalize_context = old_instance_finalize_context;
pop_line ();
pop_context ();
}
void add_struct_dup_function (Struct st) {
var function = new CCodeFunction (get_ccode_dup_function (st), get_ccode_name (st) + "*");
if (st.access == SymbolAccessibility.PRIVATE) {
function.modifiers = CCodeModifiers.STATIC;
}
function.add_parameter (new CCodeParameter ("self", "const " + get_ccode_name (st) + "*"));
push_function (function);
ccode.add_declaration (get_ccode_name (st) + "*", new CCodeVariableDeclarator ("dup"));
if (context.profile == Profile.GOBJECT) {
// g_new0 needs glib.h
cfile.add_include ("glib.h");
var creation_call = new CCodeFunctionCall (new CCodeIdentifier ("g_new0"));
creation_call.add_argument (new CCodeConstant (get_ccode_name (st)));
creation_call.add_argument (new CCodeConstant ("1"));
ccode.add_assignment (new CCodeIdentifier ("dup"), creation_call);
} else if (context.profile == Profile.POSIX) {
// calloc needs stdlib.h
cfile.add_include ("stdlib.h");
var sizeof_call = new CCodeFunctionCall (new CCodeIdentifier ("sizeof"));
sizeof_call.add_argument (new CCodeConstant (get_ccode_name (st)));
var creation_call = new CCodeFunctionCall (new CCodeIdentifier ("calloc"));
creation_call.add_argument (new CCodeConstant ("1"));
creation_call.add_argument (sizeof_call);
ccode.add_assignment (new CCodeIdentifier ("dup"), creation_call);
}
if (st.is_disposable ()) {
var copy_call = new CCodeFunctionCall (new CCodeIdentifier (get_ccode_copy_function (st)));
copy_call.add_argument (new CCodeIdentifier ("self"));
copy_call.add_argument (new CCodeIdentifier ("dup"));
ccode.add_expression (copy_call);
} else {
cfile.add_include ("string.h");
var sizeof_call = new CCodeFunctionCall (new CCodeIdentifier ("sizeof"));
sizeof_call.add_argument (new CCodeConstant (get_ccode_name (st)));
var copy_call = new CCodeFunctionCall (new CCodeIdentifier ("memcpy"));
copy_call.add_argument (new CCodeIdentifier ("dup"));
copy_call.add_argument (new CCodeIdentifier ("self"));
copy_call.add_argument (sizeof_call);
ccode.add_expression (copy_call);
}
ccode.add_return (new CCodeIdentifier ("dup"));
pop_function ();
cfile.add_function (function);
}
void add_struct_free_function (Struct st) {
var function = new CCodeFunction (get_ccode_free_function (st), "void");
if (st.is_private_symbol ()) {
function.modifiers = CCodeModifiers.STATIC;
} else if (context.hide_internal && st.is_internal_symbol ()) {
function.modifiers = CCodeModifiers.INTERNAL;
}
function.add_parameter (new CCodeParameter ("self", get_ccode_name (st) + "*"));
push_function (function);
if (st.is_disposable ()) {
var destroy_call = new CCodeFunctionCall (new CCodeIdentifier (get_ccode_destroy_function (st)));
destroy_call.add_argument (new CCodeIdentifier ("self"));
ccode.add_expression (destroy_call);
}
if (context.profile == Profile.GOBJECT) {
// g_free needs glib.h
cfile.add_include ("glib.h");
var free_call = new CCodeFunctionCall (new CCodeIdentifier ("g_free"));
free_call.add_argument (new CCodeIdentifier ("self"));
ccode.add_expression (free_call);
} else if (context.profile == Profile.POSIX) {
// free needs stdlib.h
cfile.add_include ("stdlib.h");
var free_call = new CCodeFunctionCall (new CCodeIdentifier ("free"));
free_call.add_argument (new CCodeIdentifier ("self"));
ccode.add_expression (free_call);
}
pop_function ();
cfile.add_function (function);
}
void add_struct_copy_function (Struct st) {
var function = new CCodeFunction (get_ccode_copy_function (st), "void");
if (st.is_private_symbol ()) {
function.modifiers = CCodeModifiers.STATIC;
} else if (context.hide_internal && st.is_internal_symbol ()) {
function.modifiers = CCodeModifiers.INTERNAL;
}
function.add_parameter (new CCodeParameter ("self", "const " + get_ccode_name (st) + "*"));
function.add_parameter (new CCodeParameter ("dest", get_ccode_name (st) + "*"));
push_function (function);
var dest_struct = new GLibValue (SemanticAnalyzer.get_data_type_for_symbol (st), new CCodeIdentifier ("(*dest)"), true);
unowned Struct sym = st;
while (sym.base_struct != null) {
sym = sym.base_struct;
}
foreach (var f in sym.get_fields ()) {
if (f.binding == MemberBinding.INSTANCE) {
var value = load_field (f, load_this_parameter ((TypeSymbol) st));
if ((!(f.variable_type is DelegateType) || get_ccode_delegate_target (f)) && requires_copy (f.variable_type)) {
value = copy_value (value, f);
if (value == null) {
// error case, continue to avoid critical
continue;
}
}
store_field (f, dest_struct, value);
}
}
pop_function ();
cfile.add_function (function);
}
void begin_struct_destroy_function (Struct st) {
push_context (instance_finalize_context);
var function = new CCodeFunction (get_ccode_destroy_function (st), "void");
if (st.is_private_symbol ()) {
function.modifiers = CCodeModifiers.STATIC;
} else if (context.hide_internal && st.is_internal_symbol ()) {
function.modifiers = CCodeModifiers.INTERNAL;
}
function.add_parameter (new CCodeParameter ("self", get_ccode_name (st) + "*"));
push_function (function);
pop_context ();
}
void add_struct_destroy_function (Struct st) {
unowned Struct sym = st;
while (sym.base_struct != null) {
sym = sym.base_struct;
}
if (st != sym) {
push_context (instance_finalize_context);
var destroy_func = new CCodeFunctionCall (new CCodeIdentifier (get_ccode_destroy_function (sym)));
destroy_func.add_argument (new CCodeIdentifier ("self"));
ccode.add_expression (destroy_func);
pop_context ();
}
cfile.add_function (instance_finalize_context.ccode);
}
}
|