[vala] codegen: Use get_this_type() to simplify several if statements
- From: Luca Bruno <lucabru src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [vala] codegen: Use get_this_type() to simplify several if statements
- Date: Sat, 10 Sep 2011 14:35:14 +0000 (UTC)
commit 097b31af35cd587674a8cddd5dccc49c01b89858
Author: Luca Bruno <lucabru src gnome org>
Date: Sat Sep 10 11:00:30 2011 +0200
codegen: Use get_this_type() to simplify several if statements
codegen/valaccodebasemodule.vala | 30 +++++++++++++++++-------------
codegen/valaccodedelegatemodule.vala | 2 --
codegen/valaccodememberaccessmodule.vala | 2 +-
codegen/valaccodemethodcallmodule.vala | 2 +-
codegen/valaccodemethodmodule.vala | 8 --------
codegen/valagobjectmodule.vala | 10 ----------
6 files changed, 19 insertions(+), 35 deletions(-)
---
diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala
index 5b66896..17e1abe 100644
--- a/codegen/valaccodebasemodule.vala
+++ b/codegen/valaccodebasemodule.vala
@@ -116,6 +116,16 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
}
}
+ public Constructor? current_constructor {
+ get {
+ var sym = current_symbol;
+ while (sym is Block) {
+ sym = sym.parent_symbol;
+ }
+ return sym as Constructor;
+ }
+ }
+
public DataType? current_return_type {
get {
var m = current_method;
@@ -236,8 +246,6 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
public int next_regex_id = 0;
public bool in_creation_method { get { return current_method is CreationMethod; } }
- public bool in_constructor = false;
- public bool in_static_or_class_context = false;
public bool current_method_inner_error {
get { return emit_context.current_method_inner_error; }
@@ -1743,8 +1751,7 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
data.add_field ("Block%dData *".printf (parent_block_id), "_data%d_".printf (parent_block_id));
} else {
- if (in_constructor || (current_method != null && current_method.binding == MemberBinding.INSTANCE) ||
- (current_property_accessor != null && current_property_accessor.prop.binding == MemberBinding.INSTANCE)) {
+ if (get_this_type () != null) {
data.add_field ("%s *".printf (get_ccode_name (current_type_symbol)), "self");
}
@@ -1806,13 +1813,11 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
ccode.add_assignment (new CCodeMemberAccess.pointer (get_variable_cexpression ("_data%d_".printf (block_id)), "_data%d_".printf (parent_block_id)), ref_call);
} else {
- bool in_instance_method = (current_method != null && current_method.binding == MemberBinding.INSTANCE);
- bool in_instance_property = (current_property_accessor != null && current_property_accessor.prop.binding == MemberBinding.INSTANCE);
// skip self assignment in toplevel block of creation methods with chainup as self is not set at the beginning of the method
// the chainup statement takes care of assigning self in the closure struct
bool in_creation_method_with_chainup = (current_method is CreationMethod && current_class != null && current_class.base_class != null);
- if (in_constructor || (in_instance_method && (!in_creation_method_with_chainup || current_method.body != b)) || in_instance_property) {
+ if (get_this_type () != null && (!in_creation_method_with_chainup || current_method.body != b)) {
var ref_call = new CCodeFunctionCall (get_dup_func_expression (get_data_type_for_symbol (current_type_symbol), b.source_reference));
ref_call.add_argument (get_result_cexpression ("self"));
@@ -1896,8 +1901,7 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
ccode.add_expression (unref_call);
ccode.add_assignment (new CCodeMemberAccess.pointer (new CCodeIdentifier ("_data%d_".printf (block_id)), "_data%d_".printf (parent_block_id)), new CCodeConstant ("NULL"));
} else {
- if (in_constructor || (current_method != null && current_method.binding == MemberBinding.INSTANCE) ||
- (current_property_accessor != null && current_property_accessor.prop.binding == MemberBinding.INSTANCE)) {
+ if (get_this_type () != null) {
var this_value = new GLibValue (get_data_type_for_symbol (current_type_symbol), new CCodeMemberAccess.pointer (new CCodeIdentifier ("_data%d_".printf (block_id)), "self"), true);
ccode.add_expression (destroy_value (this_value));
}
@@ -3402,9 +3406,7 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
} else if (member.is_class_member ()) {
CCodeExpression klass;
- if (current_method != null && current_method.binding == MemberBinding.INSTANCE ||
- current_property_accessor != null && current_property_accessor.prop.binding == MemberBinding.INSTANCE ||
- (in_constructor && !in_static_or_class_context)) {
+ if (get_this_type () != null) {
var k = new CCodeFunctionCall (new CCodeIdentifier ("G_OBJECT_GET_CLASS"));
k.add_argument (new CCodeIdentifier ("self"));
klass = k;
@@ -4995,7 +4997,7 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
set_delegate_target_destroy_notify (lambda, new CCodeConstant ("NULL"));
}
set_delegate_target (lambda, delegate_target);
- } else if (get_this_type () != null || in_constructor) {
+ } else if (get_this_type () != null) {
CCodeExpression delegate_target = get_result_cexpression ("self");
if (expr_owned || delegate_type.is_called_once) {
if (get_this_type () != null) {
@@ -5899,6 +5901,8 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
return current_method.this_parameter.variable_type;
} else if (current_property_accessor != null && current_property_accessor.prop.binding == MemberBinding.INSTANCE) {
return current_property_accessor.prop.this_parameter.variable_type;
+ } else if (current_constructor != null && current_constructor.binding == MemberBinding.INSTANCE) {
+ return current_constructor.this_parameter.variable_type;
}
return null;
}
diff --git a/codegen/valaccodedelegatemodule.vala b/codegen/valaccodedelegatemodule.vala
index 6975e8e..0c2242d 100644
--- a/codegen/valaccodedelegatemodule.vala
+++ b/codegen/valaccodedelegatemodule.vala
@@ -416,8 +416,6 @@ public class Vala.CCodeDelegateModule : CCodeArrayModule {
destroy_notify = new CCodeIdentifier ("block%d_data_unref".printf (block_id));
} else if (get_this_type () != null && m.binding != MemberBinding.STATIC && !m.is_async_callback && is_reference_counting (m.this_parameter.variable_type.data_type)) {
destroy_notify = get_destroy_func_expression (m.this_parameter.variable_type);
- } else if (in_constructor) {
- destroy_notify = new CCodeIdentifier ("g_object_unref");
}
if (destroy_notify != null) {
diff --git a/codegen/valaccodememberaccessmodule.vala b/codegen/valaccodememberaccessmodule.vala
index 349a96e..c4f75aa 100644
--- a/codegen/valaccodememberaccessmodule.vala
+++ b/codegen/valaccodememberaccessmodule.vala
@@ -587,7 +587,7 @@ public abstract class Vala.CCodeMemberAccessModule : CCodeControlFlowModule {
CCodeExpression klass;
if (instance == null) {
- if (in_static_or_class_context) {
+ if (get_this_type () == null) {
// Accessing the field from a static or class constructor
klass = new CCodeIdentifier ("klass");
} else {
diff --git a/codegen/valaccodemethodcallmodule.vala b/codegen/valaccodemethodcallmodule.vala
index d9ae9a6..04cecfd 100644
--- a/codegen/valaccodemethodcallmodule.vala
+++ b/codegen/valaccodemethodcallmodule.vala
@@ -207,7 +207,7 @@ public class Vala.CCodeMethodCallModule : CCodeAssignmentModule {
CCodeExpression klass;
if (ma.inner == null) {
- if (in_static_or_class_context) {
+ if (get_this_type () == null) {
// Accessing the method from a static or class constructor
klass = new CCodeIdentifier ("klass");
} else {
diff --git a/codegen/valaccodemethodmodule.vala b/codegen/valaccodemethodmodule.vala
index 8550f29..a5be334 100644
--- a/codegen/valaccodemethodmodule.vala
+++ b/codegen/valaccodemethodmodule.vala
@@ -283,11 +283,6 @@ public abstract class Vala.CCodeMethodModule : CCodeStructModule {
creturn_type = new VoidType ();
}
- if (m.binding == MemberBinding.CLASS || m.binding == MemberBinding.STATIC) {
- in_static_or_class_context = true;
- }
-
-
foreach (Parameter param in m.get_parameters ()) {
param.accept (this);
}
@@ -660,9 +655,6 @@ public abstract class Vala.CCodeMethodModule : CCodeStructModule {
cfile.add_function (ccode);
}
-
- in_static_or_class_context = false;
-
pop_context ();
if ((m.is_abstract || m.is_virtual) && !m.coroutine &&
diff --git a/codegen/valagobjectmodule.vala b/codegen/valagobjectmodule.vala
index 08f0af9..a22a8b2 100644
--- a/codegen/valagobjectmodule.vala
+++ b/codegen/valagobjectmodule.vala
@@ -402,12 +402,6 @@ public class Vala.GObjectModule : GTypeModule {
public override void visit_constructor (Constructor c) {
push_line (c.source_reference);
- if (c.binding == MemberBinding.CLASS || c.binding == MemberBinding.STATIC) {
- in_static_or_class_context = true;
- } else {
- in_constructor = true;
- }
-
var cl = (Class) c.parent_symbol;
if (c.binding == MemberBinding.INSTANCE) {
@@ -511,10 +505,6 @@ public class Vala.GObjectModule : GTypeModule {
Report.error (c.source_reference, "internal error: constructors must have instance, class, or static binding");
}
- in_static_or_class_context = false;
-
- in_constructor = false;
-
pop_line ();
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]