[vala/wip/transform: 68/72] Fix get_current_* methods in the semantic analyzer



commit 604e1dde53312a5c71a961d20ee82f07fcd6e77d
Author: Luca Bruno <lucabru src gnome org>
Date:   Thu May 2 20:32:51 2013 +0200

    Fix get_current_* methods in the semantic analyzer

 codegen/valaccodetransformer.vala      |    6 +++---
 vala/valablock.vala                    |    2 +-
 vala/valaconditionalexpression.vala    |    2 +-
 vala/valacreationmethod.vala           |    2 +-
 vala/valalambdaexpression.vala         |    2 +-
 vala/valalocalvariable.vala            |   17 ++++++++++-------
 vala/valamemberaccess.vala             |    6 +++---
 vala/valanamespace.vala                |    8 ++++++++
 vala/valaobjectcreationexpression.vala |    2 +-
 vala/valasemanticanalyzer.vala         |   27 +++++++++++++++++----------
 vala/valasymbol.vala                   |    4 ++++
 11 files changed, 50 insertions(+), 28 deletions(-)
---
diff --git a/codegen/valaccodetransformer.vala b/codegen/valaccodetransformer.vala
index 8616653..abeddf7 100644
--- a/codegen/valaccodetransformer.vala
+++ b/codegen/valaccodetransformer.vala
@@ -363,7 +363,7 @@ public class Vala.CCodeTransformer : CodeTransformer {
                if (expr.tree_can_fail) {
                        if (expr.parent_node is LocalVariable || expr.parent_node is ExpressionStatement) {
                                // simple statements, no side effects after method call
-                       } else if (!(context.analyzer.get_current_symbol (expr) is Block)) {
+                       } else if (!(context.analyzer.get_current_non_local_symbol (expr) is Block)) {
                                // can't handle errors in field initializers
                                Report.error (expr.source_reference, "Field initializers must not throw 
errors");
                        } else {
@@ -423,7 +423,7 @@ public class Vala.CCodeTransformer : CodeTransformer {
                var target_type = expr.target_type != null ? expr.target_type.copy () : null;
                push_builder (new CodeBuilder (context, parent_statement, expr.source_reference));
 
-               if (context.analyzer.get_current_symbol (expr) is Block
+               if (context.analyzer.get_current_non_local_symbol (expr) is Block
                    && (expr.operator == BinaryOperator.AND || expr.operator == BinaryOperator.OR)) {
                        var is_and = expr.operator == BinaryOperator.AND;
                        var result = b.add_temp_declaration (data_type ("bool"));
@@ -498,7 +498,7 @@ public class Vala.CCodeTransformer : CodeTransformer {
                if (expr.tree_can_fail) {
                        if (expr.parent_node is LocalVariable || expr.parent_node is ExpressionStatement) {
                                // simple statements, no side effects after method call
-                       } else if (!(context.analyzer.get_current_symbol (expr) is Block)) {
+                       } else if (!(context.analyzer.get_current_non_local_symbol (expr) is Block)) {
                                // can't handle errors in field initializers
                                Report.error (expr.source_reference, "Field initializers must not throw 
errors");
                        } else {
diff --git a/vala/valablock.vala b/vala/valablock.vala
index 0109971..e501716 100644
--- a/vala/valablock.vala
+++ b/vala/valablock.vala
@@ -154,7 +154,7 @@ public class Vala.Block : Symbol, Statement {
 
                checked = true;
 
-               owner = context.analyzer.get_current_symbol (parent_node).scope;
+               owner = context.analyzer.get_current_non_local_symbol (parent_node).scope;
 
                foreach (var stmt in this) {
                        stmt.check (context);
diff --git a/vala/valaconditionalexpression.vala b/vala/valaconditionalexpression.vala
index e9c809a..f3da484 100644
--- a/vala/valaconditionalexpression.vala
+++ b/vala/valaconditionalexpression.vala
@@ -123,7 +123,7 @@ public class Vala.ConditionalExpression : Expression {
 
                checked = true;
 
-               if (!(context.analyzer.get_current_symbol (this) is Block)) {
+               if (!(context.analyzer.get_current_non_local_symbol (this) is Block)) {
                        Report.error (source_reference, "Conditional expressions may only be used in blocks");
                        error = true;
                        return false;
diff --git a/vala/valacreationmethod.vala b/vala/valacreationmethod.vala
index 6a0ec8c..2a6a9d1 100644
--- a/vala/valacreationmethod.vala
+++ b/vala/valacreationmethod.vala
@@ -88,7 +88,7 @@ public class Vala.CreationMethod : Method {
 
                if (class_name != null && class_name != parent_symbol.name) {
                        // class_name is null for constructors generated by GIdlParser
-                       Report.error (source_reference, "missing return type in method `%s.%s´".printf 
(context.analyzer.get_current_symbol (this).get_full_name (), class_name));
+                       Report.error (source_reference, "missing return type in method `%s.%s´".printf 
(context.analyzer.get_current_symbol (parent_node).get_full_name (), class_name));
                        error = true;
                        return false;
                }
diff --git a/vala/valalambdaexpression.vala b/vala/valalambdaexpression.vala
index a18c5ec..716ee3b 100644
--- a/vala/valalambdaexpression.vala
+++ b/vala/valalambdaexpression.vala
@@ -162,7 +162,7 @@ public class Vala.LambdaExpression : Expression {
                                sym = sym.parent_symbol;
                        }
                }
-               method.owner = context.analyzer.get_current_symbol (this).scope;
+               method.owner = context.analyzer.get_current_non_local_symbol (this).scope;
 
                var lambda_params = get_parameters ();
                Iterator<Parameter> lambda_param_it = lambda_params.iterator ();
diff --git a/vala/valalocalvariable.vala b/vala/valalocalvariable.vala
index 3019be0..ab95075 100644
--- a/vala/valalocalvariable.vala
+++ b/vala/valalocalvariable.vala
@@ -84,6 +84,16 @@ public class Vala.LocalVariable : Variable {
 
                checked = true;
 
+               // current_symbol is a Method if this is the `result'
+               // variable used for postconditions
+               var block = context.analyzer.get_current_block (this);
+               if (block != null) {
+                       /* so that we can use parent_symbol */
+                       block.add_local_variable (this);
+               }
+
+               active = false;
+
                if (variable_type != null) {
                        if (variable_type is VoidType) {
                                error = true;
@@ -171,13 +181,6 @@ public class Vala.LocalVariable : Variable {
                        }
                }
 
-               // current_symbol is a Method if this is the `result'
-               // variable used for postconditions
-               var block = context.analyzer.get_current_block (this);
-               if (block != null) {
-                       block.add_local_variable (this);
-               }
-
                active = true;
 
                return !error;
diff --git a/vala/valamemberaccess.vala b/vala/valamemberaccess.vala
index 1161b4a..63e2c85 100644
--- a/vala/valamemberaccess.vala
+++ b/vala/valamemberaccess.vala
@@ -223,7 +223,7 @@ public class Vala.MemberAccess : Expression {
                                }
                        }
 
-                       base_symbol = context.analyzer.get_current_symbol (this);
+                       base_symbol = context.analyzer.get_current_non_local_symbol (this);
 
                        // track whether method has been found to make sure that access
                        // to instance member is denied from within static lambda expressions
@@ -700,7 +700,7 @@ public class Vala.MemberAccess : Expression {
                        var target_type = (TypeSymbol) member.parent_symbol;
 
                        bool in_subtype = false;
-                       for (Symbol this_symbol = context.analyzer.get_current_symbol (this); this_symbol != 
null; this_symbol = this_symbol.parent_symbol) {
+                       for (Symbol this_symbol = context.analyzer.get_current_non_local_symbol (this); 
this_symbol != null; this_symbol = this_symbol.parent_symbol) {
                                if (this_symbol == target_type) {
                                        // required for interfaces with non-abstract methods
                                        // accessing protected interface members
@@ -724,7 +724,7 @@ public class Vala.MemberAccess : Expression {
                        var target_type = member.parent_symbol;
 
                        bool in_target_type = false;
-                       for (Symbol this_symbol = context.analyzer.get_current_symbol (this); this_symbol != 
null; this_symbol = this_symbol.parent_symbol) {
+                       for (Symbol this_symbol = context.analyzer.get_current_non_local_symbol (this); 
this_symbol != null; this_symbol = this_symbol.parent_symbol) {
                                if (target_type == this_symbol) {
                                        in_target_type = true;
                                        break;
diff --git a/vala/valanamespace.vala b/vala/valanamespace.vala
index 47dd4ee..c253205 100644
--- a/vala/valanamespace.vala
+++ b/vala/valanamespace.vala
@@ -523,4 +523,12 @@ public class Vala.Namespace : Symbol {
 
                return !error;
        }
+
+       public override string to_string () {
+               if (name == null) {
+                       return "(root namespace)";
+               } else {
+                       return "namespace %s".printf (name);
+               }
+       }
 }
diff --git a/vala/valaobjectcreationexpression.vala b/vala/valaobjectcreationexpression.vala
index 0efee81..42e276e 100644
--- a/vala/valaobjectcreationexpression.vala
+++ b/vala/valaobjectcreationexpression.vala
@@ -269,7 +269,7 @@ public class Vala.ObjectCreationExpression : Expression {
 
                        if (symbol_reference != null && symbol_reference.access == 
SymbolAccessibility.PRIVATE) {
                                bool in_target_type = false;
-                               for (Symbol this_symbol = context.analyzer.get_current_symbol (this); 
this_symbol != null; this_symbol = this_symbol.parent_symbol) {
+                               for (Symbol this_symbol = context.analyzer.get_current_non_local_symbol 
(this); this_symbol != null; this_symbol = this_symbol.parent_symbol) {
                                        if (this_symbol == cl) {
                                                in_target_type = true;
                                                break;
diff --git a/vala/valasemanticanalyzer.vala b/vala/valasemanticanalyzer.vala
index 6c66c82..840b8ed 100644
--- a/vala/valasemanticanalyzer.vala
+++ b/vala/valasemanticanalyzer.vala
@@ -126,18 +126,22 @@ public class Vala.SemanticAnalyzer : CodeVisitor {
                file.check (context);
        }
 
-       public unowned Symbol get_current_symbol (CodeNode node) {
-               while (!(node is Symbol) || is_local_symbol ((Symbol) node)) {
+       public unowned Symbol? get_current_symbol (CodeNode node) {
+               while (node != null && !(node is Symbol)) {
                        node = node.parent_node;
                }
                return (Symbol) node;
        }
 
-       public bool is_local_symbol (Symbol sym) {
-               if (sym is LocalVariable) {
-                       return true;
+       public unowned Symbol? get_current_non_local_symbol (CodeNode node) {
+               while (node != null && (!(node is Symbol) || is_local_symbol ((Symbol) node))) {
+                       node = node.parent_node;
                }
-               if (sym is Constant && sym.parent_symbol is Block) {
+               return (Symbol) node;
+       }
+
+       public bool is_local_symbol (Symbol sym) {
+               if ((sym is LocalVariable || sym is Constant) && sym.parent_symbol is Block) {
                        return true;
                }
                return false;
@@ -162,7 +166,7 @@ public class Vala.SemanticAnalyzer : CodeVisitor {
 
        public unowned Method? get_current_method (CodeNode node) {
                unowned Symbol sym = get_current_symbol (node);
-               while (sym is Block) {
+               while (sym != null && !(sym is Method)) {
                        sym = sym.parent_symbol;
                }
                return sym as Method;
@@ -178,7 +182,7 @@ public class Vala.SemanticAnalyzer : CodeVisitor {
 
        public unowned PropertyAccessor? get_current_property_accessor (CodeNode node) {
                unowned Symbol sym = get_current_symbol (node);
-               while (sym is Block) {
+               while (sym != null && !(sym is PropertyAccessor)) {
                        sym = sym.parent_symbol;
                }
                return sym as PropertyAccessor;
@@ -186,7 +190,7 @@ public class Vala.SemanticAnalyzer : CodeVisitor {
 
        public unowned Symbol? get_current_method_or_property_accessor (CodeNode node) {
                unowned Symbol sym = get_current_symbol (node);
-               while (sym is Block) {
+               while (sym != null && !(sym is Method) && !(sym is PropertyAccessor)) {
                        sym = sym.parent_symbol;
                }
                if (sym is Method) {
@@ -221,7 +225,10 @@ public class Vala.SemanticAnalyzer : CodeVisitor {
        }
 
        public unowned Block? get_current_block (CodeNode node) {
-               return get_current_symbol (node) as Block;
+               while (node != null && !(node is Block)) {
+                       node = node.parent_node;
+               }
+               return (Block) node;
        }
 
        // check whether type is at least as accessible as the specified symbol
diff --git a/vala/valasymbol.vala b/vala/valasymbol.vala
index cc92116..5464e5e 100644
--- a/vala/valasymbol.vala
+++ b/vala/valasymbol.vala
@@ -536,6 +536,10 @@ public abstract class Vala.Symbol : CodeNode {
        public virtual void add_destructor (Destructor d) {
                Report.error (d.source_reference, "unexpected declaration");
        }
+
+       public override string to_string () {
+               return get_full_name();
+       }
 }
 
 public enum Vala.SymbolAccessibility {


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