[vala/wip/bug567269: 4/7] Allow multiple chain-up calls in the constructor



commit 2531f3fba926cde27f5f83de2446c4072923a80a
Author: Simon Werbeck <simon werbeck gmail com>
Date:   Thu Nov 27 15:24:44 2014 +0100

    Allow multiple chain-up calls in the constructor
    
    This requires that the instance parameter is initialized at the end of
    the constructor.
    Another issue is the possibility of initializing 'this' more than once,
    so I added a warning when this is the case.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=567269

 vala/valaflowanalyzer.vala |   19 +++++++++++++++++--
 vala/valamethodcall.vala   |    4 ----
 2 files changed, 17 insertions(+), 6 deletions(-)
---
diff --git a/vala/valaflowanalyzer.vala b/vala/valaflowanalyzer.vala
index 56a399d..dd7470d 100644
--- a/vala/valaflowanalyzer.vala
+++ b/vala/valaflowanalyzer.vala
@@ -202,6 +202,16 @@ public class Vala.FlowAnalyzer : CodeVisitor {
                                        m.return_block.add_node (param_ma);
                                }
                        }
+
+                       // ensure instance parameter is defined at end of creation method
+                       if (m is CreationMethod) {
+                               var cm = (CreationMethod) m;
+                               if (cm.chain_up) {
+                                       var this_ma = new MemberAccess.simple ("this");
+                                       this_ma.symbol_reference = cm.this_parameter;
+                                       m.return_block.add_node (this_ma);
+                               }
+                       }
                }
 
                current_block = new BasicBlock ();
@@ -471,7 +481,7 @@ public class Vala.FlowAnalyzer : CodeVisitor {
                        node.get_defined_variables (defined_variables);
 
                        foreach (Variable variable in defined_variables) {
-                               process_assignment (var_map, variable);
+                               process_assignment (var_map, variable, node);
                        }
                }
 
@@ -511,7 +521,7 @@ public class Vala.FlowAnalyzer : CodeVisitor {
                }
        }
 
-       Variable process_assignment (Map<Symbol, List<Variable>> var_map, Variable var_symbol) {
+       Variable process_assignment (Map<Symbol, List<Variable>> var_map, Variable var_symbol, CodeNode? 
node_reference = null) {
                var variable_stack = var_map.get (var_symbol);
                if (variable_stack == null) {
                        variable_stack = new ArrayList<Variable> ();
@@ -524,6 +534,11 @@ public class Vala.FlowAnalyzer : CodeVisitor {
                if (var_symbol is LocalVariable) {
                        versioned_var = new LocalVariable (var_symbol.variable_type.copy (), var_symbol.name, 
null, var_symbol.source_reference);
                } else {
+                       if (var_symbol.name == "this" && var_symbol.parent_symbol is CreationMethod && 
((CreationMethod)var_symbol.parent_symbol).chain_up) {
+                               if (variable_stack.size > 0) {
+                                       Report.warning (node_reference.source_reference, "possible 
reassignment of 'this'");
+                               }
+                       }
                        // parameter
                        versioned_var = new Parameter (var_symbol.name, var_symbol.variable_type.copy (), 
var_symbol.source_reference);
                }
diff --git a/vala/valamethodcall.vala b/vala/valamethodcall.vala
index f8208b7..08c7c4e 100644
--- a/vala/valamethodcall.vala
+++ b/vala/valamethodcall.vala
@@ -238,10 +238,6 @@ public class Vala.MethodCall : Expression {
                                error = true;
                                Report.error (source_reference, "invocation not supported in this context");
                                return false;
-                       } else if (cm.chain_up) {
-                               error = true;
-                               Report.error (source_reference, "Multiple constructor calls in the same 
constructor are not permitted");
-                               return false;
                        }
                        cm.chain_up = true;
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]