[vala/wip/transform: 182/214] Fix get_current_* methods in the semantic analyzer



commit 4140b579f68bd68839f6348e2835dde6cc1370c6
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 8941b77..bd64fb0 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 {
@@ -424,7 +424,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"));
@@ -499,7 +499,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 1de2711..6d9cf74 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 f5ea62e..47e95e8 100644
--- a/vala/valaconditionalexpression.vala
+++ b/vala/valaconditionalexpression.vala
@@ -127,7 +127,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 d104e44..1723388 100644
--- a/vala/valalambdaexpression.vala
+++ b/vala/valalambdaexpression.vala
@@ -161,7 +161,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 61013a9..b69c9b8 100644
--- a/vala/valalocalvariable.vala
+++ b/vala/valalocalvariable.vala
@@ -79,6 +79,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;
@@ -189,13 +199,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 0b348dd..f2ff12a 100644
--- a/vala/valamemberaccess.vala
+++ b/vala/valamemberaccess.vala
@@ -227,7 +227,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
@@ -703,7 +703,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
@@ -727,7 +727,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 405c287..400e1a8 100644
--- a/vala/valaobjectcreationexpression.vala
+++ b/vala/valaobjectcreationexpression.vala
@@ -293,7 +293,7 @@ public class Vala.ObjectCreationExpression : Expression {
                        if (symbol_reference != null
                            && (symbol_reference.access == SymbolAccessibility.PRIVATE || 
symbol_reference.access == SymbolAccessibility.PROTECTED)) {
                                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 abfc844..1ad223c 100644
--- a/vala/valasemanticanalyzer.vala
+++ b/vala/valasemanticanalyzer.vala
@@ -128,18 +128,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;
@@ -164,7 +168,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;
@@ -180,7 +184,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;
@@ -188,7 +192,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) {
@@ -223,7 +227,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 3dcd820..a6ef1aa 100644
--- a/vala/valasymbol.vala
+++ b/vala/valasymbol.vala
@@ -467,6 +467,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]