[vala/switch-to-gir] girparser: Resolve gir namespaces to vala namespaces.



commit ac43a0f23076eaab24faaf96a09c6d70b9e5c29b
Author: Luca Bruno <lethalman88 gmail com>
Date:   Tue Aug 24 10:42:10 2010 +0200

    girparser: Resolve gir namespaces to vala namespaces.
    
    Based on patch by Abderrahim Kitouni.

 vala/valagirparser.vala |   77 +++++++++++++++++++++++++++++++++++++----------
 1 files changed, 61 insertions(+), 16 deletions(-)
---
diff --git a/vala/valagirparser.vala b/vala/valagirparser.vala
index e475bda..470f377 100644
--- a/vala/valagirparser.vala
+++ b/vala/valagirparser.vala
@@ -42,6 +42,9 @@ public class Vala.GirParser : CodeVisitor {
 
 	HashMap<string,ArrayList<Method>> gtype_callbacks = new HashMap<string,ArrayList<Method>> (str_hash, str_equal);
 
+	HashMap<string,string> gir_namespaces = new HashMap<string,string> (str_hash, str_equal);
+	ArrayList<UnresolvedType> unresolved_namespaced_types = new ArrayList<UnresolvedType> ();
+
 	/**
 	 * Parses all .gir source files in the specified code
 	 * context and builds a code tree.
@@ -52,6 +55,7 @@ public class Vala.GirParser : CodeVisitor {
 		this.context = context;
 		glib_ns = context.root.scope.lookup ("GLib") as Namespace;
 		context.accept (this);
+		resolve_gir_namespaces ();
 	}
 
 	public override void visit_source_file (SourceFile source_file) {
@@ -209,10 +213,19 @@ public class Vala.GirParser : CodeVisitor {
 		start_element ("namespace");
 
 		bool new_namespace = false;
-		string namespace_name = transform_namespace_name (reader.get_attribute ("name"));
+		string? cprefix = reader.get_attribute ("c:prefix");
+		string namespace_name = cprefix;
+		string gir_namespace = reader.get_attribute ("name");
+		string gir_version = reader.get_attribute ("version");
+		if (namespace_name == null) {
+			namespace_name = gir_namespace;
+		}
+
 		var ns = context.root.scope.lookup (namespace_name) as Namespace;
 		if (ns == null) {
 			ns = new Namespace (namespace_name, get_current_src ());
+			ns.source_reference.file.gir_namespace = gir_namespace;
+			ns.source_reference.file.gir_version = gir_version;
 			new_namespace = true;
 		} else {
 			if (ns.external_package) {
@@ -221,7 +234,6 @@ public class Vala.GirParser : CodeVisitor {
 			}
 		}
 
-		string? cprefix = reader.get_attribute ("c:prefix");
 		if (cprefix != null) {
 			ns.add_cprefix (cprefix);
 			ns.set_lower_case_cprefix (Symbol.camel_case_to_lower_case (cprefix) + "_");
@@ -615,9 +627,10 @@ public class Vala.GirParser : CodeVisitor {
 			string[] type_components = type_name.split (".");
 			if (type_components[1] != null) {
 				// namespaced name
-				string namespace_name = transform_namespace_name (type_components[0]);
-				string transformed_type_name = type_components[1];
-				type = new UnresolvedType.from_symbol (new UnresolvedSymbol (new UnresolvedSymbol (null, namespace_name), transformed_type_name));
+				string namespace_component = type_components[0];
+				string type_component = type_components[1];
+				type = new UnresolvedType.from_symbol (new UnresolvedSymbol (new UnresolvedSymbol (null, namespace_component), type_component));
+				unresolved_namespaced_types.add ((UnresolvedType) type);
 			} else {
 				type = new UnresolvedType.from_symbol (new UnresolvedSymbol (null, type_name));
 			}
@@ -626,17 +639,6 @@ public class Vala.GirParser : CodeVisitor {
 		return type;
 	}
 
-	string transform_namespace_name (string gir_module_name) {
-		if (gir_module_name == "GObject") {
-			return "GLib";
-		} else if (gir_module_name == "Gio") {
-			return "GLib";
-		} else if (gir_module_name == "GModule") {
-			return "GLib";
-		}
-		return gir_module_name;
-	}
-
 	Struct parse_record () {
 		start_element ("record");
 		var st = new Struct (reader.get_attribute ("name"), get_current_src ());
@@ -1398,5 +1400,48 @@ public class Vala.GirParser : CodeVisitor {
 			Report.error (null, "Metadata file `%s' not found".printf (metadata_filename));
 		}
 	}
+
+	string? get_gir_namespace (Namespace ns) {
+		var gir_namespace = ns.source_reference.file.gir_namespace;
+		if (gir_namespace != null) {
+			return gir_namespace;
+		}
+
+		var a = ns.get_attribute ("CCode");
+		if (a.has_argument ("gir_namespace")) {
+			return a.get_string ("gir_namespace");
+		}
+		return null;
+	}
+
+	void collect_gir_namespaces (Namespace ns) {
+		foreach (Namespace sub_ns in ns.get_namespaces ()) {
+			collect_gir_namespaces (sub_ns);
+		}
+		if (ns.name == null) {
+			return;
+		}
+
+		var gir_namespace = get_gir_namespace (ns);
+		if (gir_namespace != null && !(gir_namespace in gir_namespaces)) {
+			gir_namespaces[gir_namespace] = ns.name;
+		}
+	}
+
+	void resolve_gir_namespaces () {
+		collect_gir_namespaces (context.root);
+
+		foreach (var type in unresolved_namespaced_types) {
+			if (type.unresolved_symbol != null) {
+				var symbol = type.unresolved_symbol;
+				while (symbol.inner != null) {
+					if (symbol.name in gir_namespaces) {
+						// map the gir namespace to the vala namespace
+						symbol.name = gir_namespaces[symbol.name];
+					}
+				}
+			}
+		}
+	}
 }
 



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