[vala/switch-to-gir: 10/10] girparser: Simplify postprocessing callbacks. Use symbols info for records.



commit 4930f2e958e26bb17c5c58db8e073993ce2db26e
Author: Luca Bruno <lucabru src gnome org>
Date:   Tue Nov 16 10:24:37 2010 +0100

    girparser: Simplify postprocessing callbacks. Use symbols info for records.

 vala/valagirparser.vala |   70 +++++++++++++++++++++-------------------------
 1 files changed, 32 insertions(+), 38 deletions(-)
---
diff --git a/vala/valagirparser.vala b/vala/valagirparser.vala
index 8e4e44c..0494760 100644
--- a/vala/valagirparser.vala
+++ b/vala/valagirparser.vala
@@ -479,11 +479,6 @@ public class Vala.GirParser : CodeVisitor {
 		public HashMap<string,string> girdata;
 	}
 
-	class CallbackScope {
-		public Symbol parent_symbol;
-		public UnresolvedSymbol gtype_struct_for;
-	}
-
 	class Alias {
 		public string name;
 		public DataType base_type;
@@ -521,7 +516,7 @@ public class Vala.GirParser : CodeVisitor {
 	ArrayList<UnresolvedSymbol> unresolved_gir_symbols = new ArrayList<UnresolvedSymbol> ();
 	HashMap<UnresolvedSymbol,ArrayList<Symbol>> symbol_reparent_map = new HashMap<UnresolvedSymbol,ArrayList<Symbol>> (unresolved_symbol_hash, unresolved_symbol_equal);
 	HashMap<Namespace,ArrayList<Method>> namespace_methods = new HashMap<Namespace,ArrayList<Method>> ();
-	HashMap<CallbackScope,ArrayList<Delegate>> gtype_callbacks = new HashMap<CallbackScope,ArrayList<Delegate>> (callback_scope_hash, callback_scope_equal);
+	HashMap<UnresolvedSymbol,ArrayList<Delegate>> gtype_callbacks = new HashMap<UnresolvedSymbol,ArrayList<Delegate>> (unresolved_symbol_hash, unresolved_symbol_equal);
 	ArrayList<Alias> aliases = new ArrayList<Alias> ();
 
 	/**
@@ -538,7 +533,6 @@ public class Vala.GirParser : CodeVisitor {
 		resolve_gir_symbols ();
 
 		postprocess_reparenting ();
-		postprocess_gtype_callbacks ();
 		postprocess_aliases ();
 		postprocess_namespace_methods ();
 	}
@@ -1415,6 +1409,7 @@ public class Vala.GirParser : CodeVisitor {
 		merge_add_process (ns);
 		current_symbols_info = old_symbols_info;
 		current_symbol = old_symbol;
+		postprocess_gtype_callbacks (ns);
 
 		if (!new_namespace) {
 			ns = null;
@@ -1862,6 +1857,10 @@ public class Vala.GirParser : CodeVisitor {
 		}
 
 		next ();
+		var old_symbols_info = current_symbols_info;
+		var old_symbol = current_symbol;
+		current_symbols_info = new HashMap<string,ArrayList<SymbolInfo>> (str_hash, str_equal);
+		current_symbol = st;
 		while (current_token == MarkupTokenType.START_ELEMENT) {
 			if (!push_metadata ()) {
 				skip_element ();
@@ -1869,11 +1868,11 @@ public class Vala.GirParser : CodeVisitor {
 			}
 
 			if (reader.name == "field") {
-				st.add_field (parse_field ());
+				add_symbol_info (parse_field ());
 			} else if (reader.name == "constructor") {
 				parse_constructor ();
 			} else if (reader.name == "method") {
-				st.add_method (parse_method ("method"));
+				add_symbol_info (parse_method ("method"));
 			} else if (reader.name == "union") {
 				Struct s = parse_union ();
 				var s_fields = s.get_fields ();
@@ -1892,7 +1891,11 @@ public class Vala.GirParser : CodeVisitor {
 		}
 		end_element ("record");
 
+		merge_add_process (st);
+		current_symbols_info = old_symbols_info;
+		current_symbol = old_symbol;
 		current_gtype_struct_for = null;
+
 		return st;
 	}
 
@@ -2095,13 +2098,11 @@ public class Vala.GirParser : CodeVisitor {
 		type = element_get_type (type, true);
 		if (type is DelegateType && current_gtype_struct_for != null) {
 			// virtual
-			var callback_scope = new CallbackScope ();
-			callback_scope.parent_symbol = current_symbol;
-			callback_scope.gtype_struct_for = parse_symbol_from_string (current_gtype_struct_for);
-			ArrayList<Delegate> callbacks = gtype_callbacks.get (callback_scope);
+			var gtype_struct_for = parse_symbol_from_string (current_gtype_struct_for);
+			ArrayList<Delegate> callbacks = gtype_callbacks.get (gtype_struct_for);
 			if (callbacks == null) {
 				callbacks = new ArrayList<Delegate> ();
-				gtype_callbacks.set (callback_scope, callbacks);
+				gtype_callbacks.set (gtype_struct_for, callbacks);
 			}
 			callbacks.add (((DelegateType) type).delegate_symbol);
 		}
@@ -2456,7 +2457,10 @@ public class Vala.GirParser : CodeVisitor {
 		cl.set_dup_function ("g_boxed_copy");
 
 		next ();
-
+		var old_symbols_info = current_symbols_info;
+		var old_symbol = current_symbol;
+		current_symbols_info = new HashMap<string,ArrayList<SymbolInfo>> (str_hash, str_equal);
+		current_symbol = cl;
 		while (current_token == MarkupTokenType.START_ELEMENT) {
 			if (!push_metadata ()) {
 				skip_element ();
@@ -2464,11 +2468,11 @@ public class Vala.GirParser : CodeVisitor {
 			}
 
 			if (reader.name == "field") {
-				cl.add_field (parse_field ());
+				add_symbol_info (parse_field ());
 			} else if (reader.name == "constructor") {
 				parse_constructor ();
 			} else if (reader.name == "method") {
-				cl.add_method (parse_method ("method"));
+				add_symbol_info (parse_method ("method"));
 			} else {
 				// error
 				Report.error (get_current_src (), "unknown child element `%s' in `class'".printf (reader.name));
@@ -2477,12 +2481,12 @@ public class Vala.GirParser : CodeVisitor {
 
 			pop_metadata ();
 		}
+		end_element ("record");
+
+		merge_add_process (cl);
+		current_symbols_info = old_symbols_info;
+		current_symbol = old_symbol;
 
-		if (current_token != MarkupTokenType.END_ELEMENT) {
-			// error
-			Report.error (get_current_src (), "expected end element");
-		}
-		next ();
 		return cl;
 	}
 
@@ -2665,14 +2669,15 @@ public class Vala.GirParser : CodeVisitor {
 		}
 	}
 
-	void postprocess_gtype_callbacks () {
-		foreach (CallbackScope callback_scope in gtype_callbacks.get_keys ()) {
-			var gtype = resolve_symbol (callback_scope.parent_symbol.scope, callback_scope.gtype_struct_for) as ObjectTypeSymbol;
+	void postprocess_gtype_callbacks (Namespace ns) {
+		foreach (UnresolvedSymbol gtype_struct_for in gtype_callbacks.get_keys ()) {
+			// parent symbol is the record, therefore use parent of parent symbol
+			var gtype = resolve_symbol (ns.scope, gtype_struct_for) as ObjectTypeSymbol;
 			if (gtype == null) {
-				Report.error (null, "unknown symbol `%s'".printf (callback_scope.gtype_struct_for.to_string ()));
+				Report.error (null, "unknown symbol `%s' while postprocessing callbacks".printf (gtype_struct_for.name));
 				continue;
 			}
-			ArrayList<Delegate> callbacks = gtype_callbacks.get (callback_scope);
+			ArrayList<Delegate> callbacks = gtype_callbacks.get (gtype_struct_for);
 			foreach (Delegate d in callbacks) {
 				var symbol = gtype.scope.lookup (d.name);
 				if (symbol == null) {
@@ -2835,15 +2840,4 @@ public class Vala.GirParser : CodeVisitor {
 		}
 		return true;
 	}
-
-	static uint callback_scope_hash (void *ptr) {
-		var cs = (CallbackScope) ptr;
-		return unresolved_symbol_hash (cs.gtype_struct_for);
-	}
-
-	static bool callback_scope_equal (void *ptr1, void *ptr2) {
-		var cs1 = (CallbackScope) ptr1;
-		var cs2 = (CallbackScope) ptr2;
-		return cs1.parent_symbol == cs2.parent_symbol && unresolved_symbol_equal (cs1.gtype_struct_for, cs2.gtype_struct_for);
-	}
 }



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