summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJürg Billeter <j@bitron.ch>2011-12-18 11:37:32 +0100
committerJürg Billeter <j@bitron.ch>2011-12-21 16:20:44 +0100
commitcc83eeab15a9a1324eb8f7de6fd98a765c2a952d (patch)
tree9a5956234081a031e2580e57fd7a219a59d9c724
parent5844b1636982a98b985d7c35c5e51928774ca873 (diff)
downloadvala-cc83eeab15a9a1324eb8f7de6fd98a765c2a952d.tar.gz
codegen: Fix external fields
-rw-r--r--codegen/valaccodebasemodule.vala190
1 files changed, 96 insertions, 94 deletions
diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala
index dd934f65e..f5ca0c0bb 100644
--- a/codegen/valaccodebasemodule.vala
+++ b/codegen/valaccodebasemodule.vala
@@ -1101,130 +1101,132 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
generate_field_declaration (f, internal_header_file);
}
- lhs = new CCodeIdentifier (get_ccode_name (f));
+ if (!f.external) {
+ lhs = new CCodeIdentifier (get_ccode_name (f));
- var var_decl = new CCodeVariableDeclarator (get_ccode_name (f), null, get_ccode_declarator_suffix (f.variable_type));
- var_decl.initializer = default_value_for_type (f.variable_type, true);
+ var var_decl = new CCodeVariableDeclarator (get_ccode_name (f), null, get_ccode_declarator_suffix (f.variable_type));
+ var_decl.initializer = default_value_for_type (f.variable_type, true);
- if (class_init_context != null) {
- push_context (class_init_context);
- } else {
- push_context (new EmitContext ());
- }
+ if (class_init_context != null) {
+ push_context (class_init_context);
+ } else {
+ push_context (new EmitContext ());
+ }
- if (f.initializer != null) {
- f.initializer.emit (this);
+ if (f.initializer != null) {
+ f.initializer.emit (this);
- var init = get_cvalue (f.initializer);
- if (is_constant_ccode_expression (init)) {
- var_decl.initializer = init;
+ var init = get_cvalue (f.initializer);
+ if (is_constant_ccode_expression (init)) {
+ var_decl.initializer = init;
+ }
}
- }
-
- var var_def = new CCodeDeclaration (field_ctype);
- var_def.add_declarator (var_decl);
- if (!f.is_private_symbol ()) {
- var_def.modifiers = CCodeModifiers.EXTERN;
- } else {
- var_def.modifiers = CCodeModifiers.STATIC;
- }
- cfile.add_type_member_declaration (var_def);
- /* add array length fields where necessary */
- if (f.variable_type is ArrayType && get_ccode_array_length (f)) {
- var array_type = (ArrayType) f.variable_type;
+ var var_def = new CCodeDeclaration (field_ctype);
+ var_def.add_declarator (var_decl);
+ if (!f.is_private_symbol ()) {
+ var_def.modifiers = CCodeModifiers.EXTERN;
+ } else {
+ var_def.modifiers = CCodeModifiers.STATIC;
+ }
+ cfile.add_type_member_declaration (var_def);
- if (!array_type.fixed_length) {
- for (int dim = 1; dim <= array_type.rank; dim++) {
- var len_type = int_type.copy ();
+ /* add array length fields where necessary */
+ if (f.variable_type is ArrayType && get_ccode_array_length (f)) {
+ var array_type = (ArrayType) f.variable_type;
- var len_def = new CCodeDeclaration (get_ccode_name (len_type));
- len_def.add_declarator (new CCodeVariableDeclarator (get_array_length_cname (get_ccode_name (f), dim), new CCodeConstant ("0")));
- if (!f.is_private_symbol ()) {
- len_def.modifiers = CCodeModifiers.EXTERN;
- } else {
- len_def.modifiers = CCodeModifiers.STATIC;
+ if (!array_type.fixed_length) {
+ for (int dim = 1; dim <= array_type.rank; dim++) {
+ var len_type = int_type.copy ();
+
+ var len_def = new CCodeDeclaration (get_ccode_name (len_type));
+ len_def.add_declarator (new CCodeVariableDeclarator (get_array_length_cname (get_ccode_name (f), dim), new CCodeConstant ("0")));
+ if (!f.is_private_symbol ()) {
+ len_def.modifiers = CCodeModifiers.EXTERN;
+ } else {
+ len_def.modifiers = CCodeModifiers.STATIC;
+ }
+ cfile.add_type_member_declaration (len_def);
}
- cfile.add_type_member_declaration (len_def);
- }
- if (array_type.rank == 1 && f.is_internal_symbol ()) {
- var len_type = int_type.copy ();
+ if (array_type.rank == 1 && f.is_internal_symbol ()) {
+ var len_type = int_type.copy ();
- var cdecl = new CCodeDeclaration (get_ccode_name (len_type));
- cdecl.add_declarator (new CCodeVariableDeclarator (get_array_size_cname (get_ccode_name (f)), new CCodeConstant ("0")));
- cdecl.modifiers = CCodeModifiers.STATIC;
- cfile.add_type_member_declaration (cdecl);
- }
- }
- } else if (f.variable_type is DelegateType) {
- var delegate_type = (DelegateType) f.variable_type;
- if (delegate_type.delegate_symbol.has_target) {
- // create field to store delegate target
-
- var target_def = new CCodeDeclaration ("gpointer");
- target_def.add_declarator (new CCodeVariableDeclarator (get_delegate_target_cname (get_ccode_name (f)), new CCodeConstant ("NULL")));
- if (!f.is_private_symbol ()) {
- target_def.modifiers = CCodeModifiers.EXTERN;
- } else {
- target_def.modifiers = CCodeModifiers.STATIC;
+ var cdecl = new CCodeDeclaration (get_ccode_name (len_type));
+ cdecl.add_declarator (new CCodeVariableDeclarator (get_array_size_cname (get_ccode_name (f)), new CCodeConstant ("0")));
+ cdecl.modifiers = CCodeModifiers.STATIC;
+ cfile.add_type_member_declaration (cdecl);
+ }
}
- cfile.add_type_member_declaration (target_def);
+ } else if (f.variable_type is DelegateType) {
+ var delegate_type = (DelegateType) f.variable_type;
+ if (delegate_type.delegate_symbol.has_target) {
+ // create field to store delegate target
- if (delegate_type.value_owned) {
- var target_destroy_notify_def = new CCodeDeclaration ("GDestroyNotify");
- target_destroy_notify_def.add_declarator (new CCodeVariableDeclarator (get_delegate_target_destroy_notify_cname (get_ccode_name (f)), new CCodeConstant ("NULL")));
+ var target_def = new CCodeDeclaration ("gpointer");
+ target_def.add_declarator (new CCodeVariableDeclarator (get_delegate_target_cname (get_ccode_name (f)), new CCodeConstant ("NULL")));
if (!f.is_private_symbol ()) {
- target_destroy_notify_def.modifiers = CCodeModifiers.EXTERN;
+ target_def.modifiers = CCodeModifiers.EXTERN;
} else {
- target_destroy_notify_def.modifiers = CCodeModifiers.STATIC;
+ target_def.modifiers = CCodeModifiers.STATIC;
}
- cfile.add_type_member_declaration (target_destroy_notify_def);
+ cfile.add_type_member_declaration (target_def);
+
+ if (delegate_type.value_owned) {
+ var target_destroy_notify_def = new CCodeDeclaration ("GDestroyNotify");
+ target_destroy_notify_def.add_declarator (new CCodeVariableDeclarator (get_delegate_target_destroy_notify_cname (get_ccode_name (f)), new CCodeConstant ("NULL")));
+ if (!f.is_private_symbol ()) {
+ target_destroy_notify_def.modifiers = CCodeModifiers.EXTERN;
+ } else {
+ target_destroy_notify_def.modifiers = CCodeModifiers.STATIC;
+ }
+ cfile.add_type_member_declaration (target_destroy_notify_def);
+ }
}
}
- }
- if (f.initializer != null) {
- var rhs = get_cvalue (f.initializer);
- if (!is_constant_ccode_expression (rhs)) {
- if (f.parent_symbol is Class) {
- if (f.initializer is InitializerList) {
- ccode.open_block ();
+ if (f.initializer != null) {
+ var rhs = get_cvalue (f.initializer);
+ if (!is_constant_ccode_expression (rhs)) {
+ if (f.parent_symbol is Class) {
+ if (f.initializer is InitializerList) {
+ ccode.open_block ();
- var temp_decl = get_temp_variable (f.variable_type);
- var vardecl = new CCodeVariableDeclarator.zero (temp_decl.name, rhs);
- ccode.add_declaration (get_ccode_name (temp_decl.variable_type), vardecl);
+ var temp_decl = get_temp_variable (f.variable_type);
+ var vardecl = new CCodeVariableDeclarator.zero (temp_decl.name, rhs);
+ ccode.add_declaration (get_ccode_name (temp_decl.variable_type), vardecl);
- var tmp = get_variable_cexpression (get_variable_cname (temp_decl.name));
- ccode.add_assignment (lhs, tmp);
+ var tmp = get_variable_cexpression (get_variable_cname (temp_decl.name));
+ ccode.add_assignment (lhs, tmp);
- ccode.close ();
- } else {
- ccode.add_assignment (lhs, rhs);
- }
+ ccode.close ();
+ } else {
+ ccode.add_assignment (lhs, rhs);
+ }
- if (f.variable_type is ArrayType && get_ccode_array_length (f) &&
- f.initializer is ArrayCreationExpression) {
- var array_type = (ArrayType) f.variable_type;
- var field_value = get_field_cvalue (f, null);
+ if (f.variable_type is ArrayType && get_ccode_array_length (f) &&
+ f.initializer is ArrayCreationExpression) {
+ var array_type = (ArrayType) f.variable_type;
+ var field_value = get_field_cvalue (f, null);
- List<Expression> sizes = ((ArrayCreationExpression) f.initializer).get_sizes ();
- for (int dim = 1; dim <= array_type.rank; dim++) {
- var array_len_lhs = get_array_length_cvalue (field_value, dim);
- var size = sizes[dim - 1];
- ccode.add_assignment (array_len_lhs, get_cvalue (size));
+ List<Expression> sizes = ((ArrayCreationExpression) f.initializer).get_sizes ();
+ for (int dim = 1; dim <= array_type.rank; dim++) {
+ var array_len_lhs = get_array_length_cvalue (field_value, dim);
+ var size = sizes[dim - 1];
+ ccode.add_assignment (array_len_lhs, get_cvalue (size));
+ }
}
+ } else {
+ f.error = true;
+ Report.error (f.source_reference, "Non-constant field initializers not supported in this context");
+ return;
}
- } else {
- f.error = true;
- Report.error (f.source_reference, "Non-constant field initializers not supported in this context");
- return;
}
}
- }
- pop_context ();
+ pop_context ();
+ }
}
pop_line ();