[vala/wip/attributes: 47/121] gidlparser: Drop usage of CodeNode.get_cprefix



commit 797fef1b4457326143cc6be8f1d1c3b2f60a41e6
Author: Luca Bruno <lucabru src gnome org>
Date:   Wed Jun 29 13:21:31 2011 +0200

    gidlparser: Drop usage of CodeNode.get_cprefix

 vapigen/valagidlparser.vala |   71 +++++++++++++++++++++++++-----------------
 1 files changed, 42 insertions(+), 29 deletions(-)
---
diff --git a/vapigen/valagidlparser.vala b/vapigen/valagidlparser.vala
index c2ae4fb..0fc3613 100644
--- a/vapigen/valagidlparser.vala
+++ b/vapigen/valagidlparser.vala
@@ -165,23 +165,10 @@ public class Vala.GIdlParser : CodeVisitor {
 
 		if (type_name.has_prefix (container.name)) {
 			return type_name.substring (container.name.length);
-		} else if (container.name == "GLib" && type_name.has_prefix ("G")) {
-			return type_name.substring (1);
-		} else  {
-			string best_match = null;
-			if (container is Namespace) {
-				foreach (string cprefix in ((Namespace) container).get_cprefixes ()) {
-					if (type_name.has_prefix (cprefix)) {
-						if (best_match == null || cprefix.length > best_match.length)
-							best_match = cprefix;
-					}
-				}
-               } else {
-				best_match = container.get_cprefix ();
-               }
-
-			if (best_match != null) {
-				return type_name.substring (best_match.length);;
+		} else {
+			var cprefix = get_cprefix (container);
+			if (type_name.has_prefix (cprefix)) {
+				return type_name.substring (cprefix.length);;
 			}
 		}
 
@@ -204,10 +191,26 @@ public class Vala.GIdlParser : CodeVisitor {
 		if (sym is Method) {
 			return "%s%s".printf (get_lower_case_cprefix (sym.parent_symbol), sym.name);
 		} else {
-			return "%s%s".printf (sym.parent_symbol.get_cprefix (), sym.name);
+			return "%s%s".printf (get_cprefix (sym.parent_symbol), sym.name);
 		}
 	}
 
+	private string get_lower_case_cname (Symbol sym) {
+		var lower_case_csuffix = Symbol.camel_case_to_lower_case (sym.name);
+		if (sym is ObjectTypeSymbol) {
+			// remove underscores in some cases to avoid conflicts of type macros
+			if (lower_case_csuffix.has_prefix ("type_")) {
+				lower_case_csuffix = "type" + lower_case_csuffix.substring ("type_".length);
+			} else if (lower_case_csuffix.has_prefix ("is_")) {
+				lower_case_csuffix = "is" + lower_case_csuffix.substring ("is_".length);
+			}
+			if (lower_case_csuffix.has_suffix ("_class")) {
+				lower_case_csuffix = lower_case_csuffix.substring (0, lower_case_csuffix.length - "_class".length) + "class";
+			}
+		}
+		return "%s%s".printf (get_lower_case_cprefix (sym.parent_symbol), lower_case_csuffix);
+	}
+
 	private string get_lower_case_cprefix (Symbol sym) {
 		if (sym.name == null) {
 			return "";
@@ -222,20 +225,30 @@ public class Vala.GIdlParser : CodeVisitor {
 			return cprefix;
 		}
 
-		var lower_case_csuffix = Symbol.camel_case_to_lower_case (sym.name);
+		return get_lower_case_cname (sym) + "_";
+	}
+
+	public string get_cprefix (Symbol sym) {
 		if (sym is ObjectTypeSymbol) {
-			// remove underscores in some cases to avoid conflicts of type macros
-			if (lower_case_csuffix.has_prefix ("type_")) {
-				lower_case_csuffix = "type" + lower_case_csuffix.substring ("type_".length);
-			} else if (lower_case_csuffix.has_prefix ("is_")) {
-				lower_case_csuffix = "is" + lower_case_csuffix.substring ("is_".length);
-			}
-			if (lower_case_csuffix.has_suffix ("_class")) {
-				lower_case_csuffix = lower_case_csuffix.substring (0, lower_case_csuffix.length - "_class".length) + "class";
+			return get_cname (sym);
+		} else if (sym is Enum || sym is ErrorDomain) {
+			return "%s_".printf (get_lower_case_cname (sym).up ());
+		} else if (sym is Namespace) {
+			if (sym.name != null) {
+				var cprefix = sym.get_attribute_string ("CCode", "cprefix");
+				if (cprefix != null) {
+					return cprefix;
+				}
+				return "%s%s".printf (get_cprefix (sym.parent_symbol), sym.name);
+			} else {
+				return "";
 			}
+		} else if (sym.name != null) {
+			return sym.name;
 		}
-		return "%s%s_".printf (get_lower_case_cprefix (sym.parent_symbol), lower_case_csuffix);
+		return "";
 	}
+}
 
 	private string[] get_attributes_for_node (IdlNode node) {
 		string name;
@@ -410,7 +423,7 @@ public class Vala.GIdlParser : CodeVisitor {
 			cc = cp.scope.lookup (tok) as Symbol;
 			if ( cc == null ) {
 				cc = new Namespace (tok, current_source_reference);
-				((Namespace) cc).add_cprefix (cp.get_cprefix () + tok);
+				((Namespace) cc).add_cprefix (get_cprefix (cp) + tok);
 				add_symbol_to_container (cp, cc);
 			}
 			cp = cc;



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