[vala/wip/issue/327: 50/64] More work



commit 9f1999e2715a61ba7989323248fc3ce6fe11735f
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Sat Mar 28 15:03:41 2020 +0100

    More work

 vala/valacodewriter.vala    | 10 ++++++++++
 vala/valamemberaccess.vala  | 20 ++++++++++----------
 vala/valawithstatement.vala | 36 ++++++++++++++++++++++++++++++++++--
 3 files changed, 54 insertions(+), 12 deletions(-)
---
diff --git a/vala/valacodewriter.vala b/vala/valacodewriter.vala
index 28ad9fd34..9658cdb1f 100644
--- a/vala/valacodewriter.vala
+++ b/vala/valacodewriter.vala
@@ -1161,6 +1161,16 @@ public class Vala.CodeWriter : CodeVisitor {
                write_newline ();
        }
 
+       public override void visit_with_statement (WithStatement stmt) {
+               write_indent ();
+               write_string ("with (");
+               stmt.expression.accept (this);
+               write_string (")");
+               stmt.body.accept (this);
+               write_string (";");
+               write_newline ();
+       }
+
        public override void visit_yield_statement (YieldStatement y) {
                write_indent ();
                write_string ("yield");
diff --git a/vala/valamemberaccess.vala b/vala/valamemberaccess.vala
index 3b114176d..5f922deba 100644
--- a/vala/valamemberaccess.vala
+++ b/vala/valamemberaccess.vala
@@ -278,6 +278,16 @@ public class Vala.MemberAccess : Expression {
 
                                symbol_reference = SemanticAnalyzer.symbol_lookup_inherited (sym, 
member_name);
 
+                               if (symbol_reference == null && sym is WithStatement) {
+                                       unowned WithStatement w = (WithStatement) sym;
+                                       symbol_reference = w.with_variable.variable_type.get_member 
(member_name);
+                                       if (symbol_reference != null) {
+                                               inner = new MemberAccess (null, w.with_variable.name, 
source_reference);
+                                               inner.check (context);
+                                               may_access_instance_members = true;
+                                       }
+                               }
+
                                if (symbol_reference == null && sym is TypeSymbol && 
may_access_instance_members) {
                                        // used for generated to_string methods in enums
                                        symbol_reference = this_parameter.variable_type.get_member 
(member_name);
@@ -302,16 +312,6 @@ public class Vala.MemberAccess : Expression {
                                        }
                                }
 
-                               if (symbol_reference == null && sym is WithStatement) {
-                                       unowned WithStatement w = (WithStatement) sym;
-                                       symbol_reference = w.expression.value_type.get_member (member_name);
-                                       if (symbol_reference != null) {
-                                               inner = w.expression;
-                                       }
-                                       may_access_instance_members = true;
-                                       may_access_klass_members = true;
-                               }
-
                                sym = sym.parent_symbol;
                        }
 
diff --git a/vala/valawithstatement.vala b/vala/valawithstatement.vala
index 50fc65e0d..edacf93ca 100644
--- a/vala/valawithstatement.vala
+++ b/vala/valawithstatement.vala
@@ -23,6 +23,8 @@
 using GLib;
 
 public class Vala.WithStatement : Block {
+       private static int next_with_id = 0;
+
        /**
         * Expression representing the type of body's dominant scope.
         */
@@ -34,6 +36,11 @@ public class Vala.WithStatement : Block {
                }
        }
 
+       /**
+        * Specifies the with-variable.
+        */
+       public LocalVariable with_variable { get; private set; }
+
        /**
         * The block which dominant scope is type of expression.
         */
@@ -61,7 +68,9 @@ public class Vala.WithStatement : Block {
        }
 
        public override void accept_children (CodeVisitor visitor) {
-               expression.accept (visitor);
+               if (expression.symbol_reference == with_variable) {
+                       expression.accept (visitor);
+               }
                if (body != null) {
                        body.accept (visitor);
                }
@@ -76,6 +85,13 @@ public class Vala.WithStatement : Block {
        public override bool check (CodeContext context) {
                expression.check (context);
 
+               LocalVariable local_var = expression.symbol_reference as LocalVariable;
+               if (local_var == null) {
+                       local_var = new LocalVariable (expression.value_type, "_with_local%d_".printf 
(next_with_id++), expression, source_reference);
+                       body.insert_statement (0, new DeclarationStatement (local_var, source_reference));
+               }
+               with_variable = local_var;
+
                var old_symbol = context.analyzer.current_symbol;
                owner = context.analyzer.current_symbol.scope;
                context.analyzer.current_symbol = this;
@@ -88,7 +104,23 @@ public class Vala.WithStatement : Block {
        }
 
        public override void emit (CodeGenerator codegen) {
-               expression.emit (codegen);
+               if (expression.symbol_reference == with_variable) {
+                       expression.emit (codegen);
+               }
                body.emit (codegen);
        }
+
+       public override void get_error_types (Collection<DataType> collection, SourceReference? 
source_reference = null) {
+               if (source_reference == null) {
+                       source_reference = this.source_reference;
+               }
+               expression.get_error_types (collection, source_reference);
+               body.get_error_types (collection, source_reference);
+       }
+
+       public override void get_defined_variables (Collection<Variable> collection) {
+               if (expression.symbol_reference != with_variable) {
+                       collection.add (with_variable);
+               }
+       }
 }


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