[vala] Simplify current symbol tracking



commit 91508874ff308307b9a61ce87d80bb5a4a0de3db
Author: Jürg Billeter <j bitron ch>
Date:   Sun Aug 16 11:59:27 2009 +0200

    Simplify current symbol tracking
    
    Replace current_type_symbol, current_class, current_struct,
    current_method, and current_property_accessor fields by properties.

 codegen/valaccodebasemodule.vala   |   47 ++++++++++++++++++++++++++++++-----
 codegen/valaccodemethodmodule.vala |    8 ------
 codegen/valaccodestructmodule.vala |    6 ++--
 codegen/valagasyncmodule.vala      |    5 ++-
 codegen/valagtypemodule.vala       |   10 +------
 vala/valaclass.vala                |    3 --
 vala/valacreationmethod.vala       |    9 -------
 vala/valamethod.vala               |    9 -------
 vala/valaproperty.vala             |    4 +-
 vala/valapropertyaccessor.vala     |   28 ++++++++++++++-------
 vala/valasemanticanalyzer.vala     |   24 ++++++++++++++++-
 vala/valastruct.vala               |    3 --
 12 files changed, 91 insertions(+), 65 deletions(-)
---
diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala
index 10237f7..1939e42 100644
--- a/codegen/valaccodebasemodule.vala
+++ b/codegen/valaccodebasemodule.vala
@@ -32,12 +32,45 @@ internal class Vala.CCodeBaseModule : CCodeModule {
 
 	public Symbol root_symbol;
 	public Symbol current_symbol;
-	public TypeSymbol current_type_symbol;
-	public Class current_class;
-	public Method current_method;
 	public DataType current_return_type;
 	public TryStatement current_try;
-	public PropertyAccessor current_property_accessor;
+
+	public Method? current_method {
+		get {
+			var sym = current_symbol;
+			while (sym is Block) {
+				sym = sym.parent_symbol;
+			}
+			return sym as Method;
+		}
+	}
+
+	public PropertyAccessor? current_property_accessor {
+		get {
+			var sym = current_symbol;
+			while (sym is Block) {
+				sym = sym.parent_symbol;
+			}
+			return sym as PropertyAccessor;
+		}
+	}
+
+	public TypeSymbol? current_type_symbol {
+		get {
+			var sym = current_symbol;
+			while (sym != null) {
+				if (sym is TypeSymbol) {
+					return (TypeSymbol) sym;
+				}
+				sym = sym.parent_symbol;
+			}
+			return null;
+		}
+	}
+
+	public Class? current_class {
+		get { return current_type_symbol as Class; }
+	}
 
 	public CCodeDeclarationSpace header_declarations;
 	public CCodeDeclarationSpace internal_header_declarations;
@@ -1234,9 +1267,9 @@ internal class Vala.CCodeBaseModule : CCodeModule {
 	}
 
 	public override void visit_property_accessor (PropertyAccessor acc) {
-		var old_property_accessor = current_property_accessor;
+		var old_symbol = current_symbol;
 		bool old_method_inner_error = current_method_inner_error;
-		current_property_accessor = acc;
+		current_symbol = acc;
 		current_method_inner_error = false;
 
 		var prop = (Property) acc.prop;
@@ -1474,7 +1507,7 @@ internal class Vala.CCodeBaseModule : CCodeModule {
 			source_type_member_definition.append (function);
 		}
 
-		current_property_accessor = old_property_accessor;
+		current_symbol = old_symbol;
 		current_return_type = old_return_type;
 		current_method_inner_error = old_method_inner_error;
 	}
diff --git a/codegen/valaccodemethodmodule.vala b/codegen/valaccodemethodmodule.vala
index 9e83e6a..d1ada7e 100644
--- a/codegen/valaccodemethodmodule.vala
+++ b/codegen/valaccodemethodmodule.vala
@@ -183,9 +183,7 @@ internal class Vala.CCodeMethodModule : CCodeStructModule {
 	}
 
 	public override void visit_method (Method m) {
-		var old_type_symbol = current_type_symbol;
 		var old_symbol = current_symbol;
-		Method old_method = current_method;
 		DataType old_return_type = current_return_type;
 		bool old_method_inner_error = current_method_inner_error;
 		bool old_in_creation_method = in_creation_method;
@@ -194,11 +192,7 @@ internal class Vala.CCodeMethodModule : CCodeStructModule {
 		var old_temp_ref_vars = temp_ref_vars;
 		var old_variable_name_map = variable_name_map;
 		var old_try = current_try;
-		if (m.parent_symbol is TypeSymbol) {
-			current_type_symbol = (TypeSymbol) m.parent_symbol;
-		}
 		current_symbol = m;
-		current_method = m;
 		current_return_type = m.return_type;
 		current_method_inner_error = false;
 		next_temp_var_id = 0;
@@ -304,9 +298,7 @@ internal class Vala.CCodeMethodModule : CCodeStructModule {
 
 		bool inner_error = current_method_inner_error;
 
-		current_type_symbol = old_type_symbol;
 		current_symbol = old_symbol;
-		current_method = old_method;
 		current_return_type = old_return_type;
 		current_method_inner_error = old_method_inner_error;
 		next_temp_var_id = old_next_temp_var_id;
diff --git a/codegen/valaccodestructmodule.vala b/codegen/valaccodestructmodule.vala
index 9ffc52a..f617598 100644
--- a/codegen/valaccodestructmodule.vala
+++ b/codegen/valaccodestructmodule.vala
@@ -139,9 +139,9 @@ internal class Vala.CCodeStructModule : CCodeBaseModule {
 	}
 
 	public override void visit_struct (Struct st) {
-		var old_type_symbol = current_type_symbol;
+		var old_symbol = current_symbol;
 		var old_instance_finalize_fragment = instance_finalize_fragment;
-		current_type_symbol = st;
+		current_symbol = st;
 		instance_finalize_fragment = new CCodeFragment ();
 
 		generate_struct_declaration (st, source_declarations);
@@ -163,7 +163,7 @@ internal class Vala.CCodeStructModule : CCodeBaseModule {
 			add_struct_free_function (st);
 		}
 
-		current_type_symbol = old_type_symbol;
+		current_symbol = old_symbol;
 		instance_finalize_fragment = old_instance_finalize_fragment;
 	}
 
diff --git a/codegen/valagasyncmodule.vala b/codegen/valagasyncmodule.vala
index 438cbab..a33635f 100644
--- a/codegen/valagasyncmodule.vala
+++ b/codegen/valagasyncmodule.vala
@@ -71,10 +71,11 @@ internal class Vala.GAsyncModule : GSignalModule {
 			var v = new LocalVariable (m.return_type, "result");
 			var ma = new MemberAccess.simple ("result");
 			ma.symbol_reference = v;
-			current_method = m;
+			var old_symbol = current_symbol;
+			current_symbol = m;
 			var unref_expr = get_unref_expression (new CCodeMemberAccess.pointer (new CCodeIdentifier ("data"), "result"), m.return_type, ma);
 			freeblock.add_statement (new CCodeExpressionStatement (unref_expr));
-			current_method = null;
+			current_symbol = old_symbol;
 		}
 
 		var freecall = new CCodeFunctionCall (new CCodeIdentifier ("g_slice_free"));
diff --git a/codegen/valagtypemodule.vala b/codegen/valagtypemodule.vala
index a26ce66..229c7bf 100644
--- a/codegen/valagtypemodule.vala
+++ b/codegen/valagtypemodule.vala
@@ -461,8 +461,6 @@ internal class Vala.GTypeModule : GErrorModule {
 
 	public override void visit_class (Class cl) {
 		var old_symbol = current_symbol;
-		var old_type_symbol = current_type_symbol;
-		var old_class = current_class;
 		var old_param_spec_struct = param_spec_struct;
 		var old_prop_enum = prop_enum;
 		var old_class_init_fragment = class_init_fragment;
@@ -472,8 +470,6 @@ internal class Vala.GTypeModule : GErrorModule {
 		var old_instance_init_fragment = instance_init_fragment;
 		var old_instance_finalize_fragment = instance_finalize_fragment;
 		current_symbol = cl;
-		current_type_symbol = cl;
-		current_class = cl;
 
 		bool is_gtypeinstance = !cl.is_compact;
 		bool is_fundamental = is_gtypeinstance && cl.base_class == null;
@@ -652,8 +648,6 @@ internal class Vala.GTypeModule : GErrorModule {
 		}
 
 		current_symbol = old_symbol;
-		current_type_symbol = old_type_symbol;
-		current_class = old_class;
 		param_spec_struct = old_param_spec_struct;
 		prop_enum = old_prop_enum;
 		class_init_fragment = old_class_init_fragment;
@@ -1797,8 +1791,8 @@ internal class Vala.GTypeModule : GErrorModule {
 	}
 
 	public override void visit_interface (Interface iface) {
+		var old_symbol = current_symbol;
 		current_symbol = iface;
-		current_type_symbol = iface;
 
 		if (iface.get_cname().len () < 3) {
 			iface.error = true;
@@ -1816,7 +1810,7 @@ internal class Vala.GTypeModule : GErrorModule {
 		type_fun.init_from_type ();
 		source_type_member_definition.append (type_fun.get_definition ());
 
-		current_type_symbol = null;
+		current_symbol = old_symbol;
 	}
 
 	public virtual TypeRegisterFunction create_interface_register_function (Interface iface) {
diff --git a/vala/valaclass.vala b/vala/valaclass.vala
index ad76ded..68862a7 100644
--- a/vala/valaclass.vala
+++ b/vala/valaclass.vala
@@ -914,13 +914,11 @@ public class Vala.Class : ObjectTypeSymbol {
 
 		var old_source_file = analyzer.current_source_file;
 		var old_symbol = analyzer.current_symbol;
-		var old_class = analyzer.current_class;
 
 		if (source_reference != null) {
 			analyzer.current_source_file = source_reference.file;
 		}
 		analyzer.current_symbol = this;
-		analyzer.current_class = this;
 
 		foreach (DataType base_type_reference in get_base_types ()) {
 			if (!base_type_reference.check (analyzer)) {
@@ -1141,7 +1139,6 @@ public class Vala.Class : ObjectTypeSymbol {
 
 		analyzer.current_source_file = old_source_file;
 		analyzer.current_symbol = old_symbol;
-		analyzer.current_class = old_class;
 
 		return !error;
 	}
diff --git a/vala/valacreationmethod.vala b/vala/valacreationmethod.vala
index 3dc1aec..e73a195 100644
--- a/vala/valacreationmethod.vala
+++ b/vala/valacreationmethod.vala
@@ -134,19 +134,12 @@ public class Vala.CreationMethod : Method {
 
 		var old_source_file = analyzer.current_source_file;
 		var old_symbol = analyzer.current_symbol;
-		var old_class = analyzer.current_class;
-		var old_struct = analyzer.current_struct;
 		var old_return_type = analyzer.current_return_type;
 
 		if (source_reference != null) {
 			analyzer.current_source_file = source_reference.file;
 		}
 		analyzer.current_symbol = this;
-		if (parent_symbol is Class) {
-			analyzer.current_class = (Class) parent_symbol;
-		} else if (parent_symbol is Struct) {
-			analyzer.current_struct = (Struct) parent_symbol;
-		}
 		analyzer.current_return_type = return_type;
 
 		foreach (FormalParameter param in get_parameters()) {
@@ -163,8 +156,6 @@ public class Vala.CreationMethod : Method {
 
 		analyzer.current_source_file = old_source_file;
 		analyzer.current_symbol = old_symbol;
-		analyzer.current_class = old_class;
-		analyzer.current_struct = old_struct;
 		analyzer.current_return_type = old_return_type;
 
 		if (analyzer.current_symbol.parent_symbol is Method) {
diff --git a/vala/valamethod.vala b/vala/valamethod.vala
index 43f3d30..e26c56f 100644
--- a/vala/valamethod.vala
+++ b/vala/valamethod.vala
@@ -720,19 +720,12 @@ public class Vala.Method : Member {
 
 		var old_source_file = analyzer.current_source_file;
 		var old_symbol = analyzer.current_symbol;
-		var old_class = analyzer.current_class;
-		var old_struct = analyzer.current_struct;
 		var old_return_type = analyzer.current_return_type;
 
 		if (source_reference != null) {
 			analyzer.current_source_file = source_reference.file;
 		}
 		analyzer.current_symbol = this;
-		if (parent_symbol is Class) {
-			analyzer.current_class = (Class) parent_symbol;
-		} else if (parent_symbol is Struct) {
-			analyzer.current_struct = (Struct) parent_symbol;
-		}
 		analyzer.current_return_type = return_type;
 
 		return_type.check (analyzer);
@@ -772,8 +765,6 @@ public class Vala.Method : Member {
 
 		analyzer.current_source_file = old_source_file;
 		analyzer.current_symbol = old_symbol;
-		analyzer.current_class = old_class;
-		analyzer.current_struct = old_struct;
 		analyzer.current_return_type = old_return_type;
 
 		if (analyzer.current_symbol.parent_symbol is Method) {
diff --git a/vala/valaproperty.vala b/vala/valaproperty.vala
index e807410..64eaa8e 100644
--- a/vala/valaproperty.vala
+++ b/vala/valaproperty.vala
@@ -48,7 +48,7 @@ public class Vala.Property : Member, Lockable {
 		set {
 			_get_accessor = value;
 			if (value != null) {
-				value.prop = this;
+				value.owner = scope;
 			}
 		}
 	}
@@ -61,7 +61,7 @@ public class Vala.Property : Member, Lockable {
 		set {
 			_set_accessor = value;
 			if (value != null) {
-				value.prop = this;
+				value.owner = scope;
 			}
 		}
 	}
diff --git a/vala/valapropertyaccessor.vala b/vala/valapropertyaccessor.vala
index de017c2..63cbfe0 100644
--- a/vala/valapropertyaccessor.vala
+++ b/vala/valapropertyaccessor.vala
@@ -25,11 +25,13 @@ using GLib;
 /**
  * Represents a get or set accessor of a property in the source code.
  */
-public class Vala.PropertyAccessor : CodeNode {
+public class Vala.PropertyAccessor : Symbol {
 	/**
 	 * The corresponding property.
 	 */
-	public weak Property prop { get; set; }
+	public Property prop {
+		get { return parent_symbol as Property; }
+	}
 
 	/**
 	 * The property type.
@@ -61,14 +63,17 @@ public class Vala.PropertyAccessor : CodeNode {
 	public bool construction { get; set; }
 
 	/**
-	 * Specifies the accessibility of this property accessor.
-	 */
-	public SymbolAccessibility access { get; set; }
-
-	/**
 	 * The accessor body.
 	 */
-	public Block? body { get; set; }
+	public Block? body {
+		get { return _body; }
+		set {
+			_body = value;
+			if (_body != null) {
+				_body.owner = scope;
+			}
+		}
+	}
 
 	public BasicBlock entry_block { get; set; }
 
@@ -104,6 +109,7 @@ public class Vala.PropertyAccessor : CodeNode {
 
 	private DataType _value_type;
 	private string? _cname;
+	private Block _body;
 	
 	/**
 	 * Creates a new property accessor.
@@ -116,12 +122,12 @@ public class Vala.PropertyAccessor : CodeNode {
 	 * @return             newly created property accessor
 	 */
 	public PropertyAccessor (bool readable, bool writable, bool construction, DataType? value_type, Block? body, SourceReference? source_reference) {
+		base (null, source_reference);
 		this.readable = readable;
 		this.writable = writable;
 		this.construction = construction;
 		this.value_type = value_type;
 		this.body = body;
-		this.source_reference = source_reference;
 	}
 
 	public override void accept (CodeVisitor visitor) {
@@ -163,7 +169,10 @@ public class Vala.PropertyAccessor : CodeNode {
 			return false;
 		}
 
+		var old_symbol = analyzer.current_symbol;
 		var old_return_type = analyzer.current_return_type;
+
+		analyzer.current_symbol = this;
 		if (readable) {
 			analyzer.current_return_type = value_type;
 		} else {
@@ -201,6 +210,7 @@ public class Vala.PropertyAccessor : CodeNode {
 			body.check (analyzer);
 		}
 
+		analyzer.current_symbol = old_symbol;
 		analyzer.current_return_type = old_return_type;
 
 		return !error;
diff --git a/vala/valasemanticanalyzer.vala b/vala/valasemanticanalyzer.vala
index 0b6feba..c62fe5e 100644
--- a/vala/valasemanticanalyzer.vala
+++ b/vala/valasemanticanalyzer.vala
@@ -35,8 +35,28 @@ public class Vala.SemanticAnalyzer : CodeVisitor {
 	public Symbol current_symbol { get; set; }
 	public SourceFile current_source_file { get; set; }
 	public DataType current_return_type;
-	public Class current_class;
-	public Struct current_struct;
+
+	public TypeSymbol? current_type_symbol {
+		get {
+			var sym = current_symbol;
+			while (sym != null) {
+				if (sym is TypeSymbol) {
+					return (TypeSymbol) sym;
+				}
+				sym = sym.parent_symbol;
+			}
+			return null;
+		}
+	}
+
+	public Class? current_class {
+		get { return current_type_symbol as Class; }
+	}
+
+
+	public Struct? current_struct {
+		get { return current_type_symbol as Struct; }
+	}
 
 	public Block insert_block;
 
diff --git a/vala/valastruct.vala b/vala/valastruct.vala
index c2d61bf..33956d7 100644
--- a/vala/valastruct.vala
+++ b/vala/valastruct.vala
@@ -717,13 +717,11 @@ public class Vala.Struct : TypeSymbol {
 
 		var old_source_file = analyzer.current_source_file;
 		var old_symbol = analyzer.current_symbol;
-		var old_struct = analyzer.current_struct;
 
 		if (source_reference != null) {
 			analyzer.current_source_file = source_reference.file;
 		}
 		analyzer.current_symbol = this;
-		analyzer.current_struct = this;
 
 		if (base_type != null) {
 			base_type.check (analyzer);
@@ -765,7 +763,6 @@ public class Vala.Struct : TypeSymbol {
 
 		analyzer.current_source_file = old_source_file;
 		analyzer.current_symbol = old_symbol;
-		analyzer.current_struct = old_struct;
 
 		return !error;
 	}



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