[vala/wip/attributes: 3/4] codegen: Add get_ccode_cname



commit 653bc24cdef2e64154d6763ecfc0529b73fc7d7f
Author: Luca Bruno <lucabru src gnome org>
Date:   Wed Jun 22 21:04:27 2011 +0200

    codegen: Add get_ccode_cname

 codegen/valaccodebasemodule.vala |  129 ++++++++++++++++++++++++++++++++++++++
 1 files changed, 129 insertions(+), 0 deletions(-)
---
diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala
index 9f4a13a..8c1f064 100644
--- a/codegen/valaccodebasemodule.vala
+++ b/codegen/valaccodebasemodule.vala
@@ -5831,6 +5831,135 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
 		var glib_value = (GLibValue) value;
 		return glib_value.array_length_cexpr;
 	}
+
+	/* CCode attribute */
+
+	public static string get_ccode_default_cname (CodeNode node) {
+		var sym = node as Symbol;
+		if (sym != null) {
+			if (sym is Constant) {
+				return "%s%s".printf (sym.parent_symbol.get_lower_case_cprefix ().up (), sym.name);
+			} else if (sym is Field) {
+				if (((Field) sym).binding == MemberBinding.STATIC) {
+					return "%s%s".printf (sym.parent_symbol.get_lower_case_cprefix (), sym.name);
+				} else {
+					return sym.name;
+				}
+			} else if (sym is Method) {
+				if (sym.name == "main" && sym.parent_symbol.name == null) {
+					// avoid conflict with generated main function
+					return "_vala_main";
+				} else if (sym.name.has_prefix ("_")) {
+					return "_%s%s".printf (sym.parent_symbol.get_lower_case_cprefix (), sym.name.substring (1));
+				} else {
+					return "%s%s".printf (sym.parent_symbol.get_lower_case_cprefix (), sym.name);
+				}
+			} else if (sym is PropertyAccessor) {
+				var acc = (PropertyAccessor) sym;
+				var t = (TypeSymbol) acc.prop.parent_symbol;
+
+				if (acc.readable) {
+					return "%sget_%s".printf (t.get_lower_case_cprefix (), acc.prop.name);
+				} else {
+					return "%sset_%s".printf (t.get_lower_case_cprefix (), acc.prop.name);
+				}
+			} else if (sym is Signal) {
+				return Symbol.camel_case_to_lower_case (sym.name);
+			} else {
+				return "%s%s".printf (sym.parent_symbol.get_cprefix (), sym.name);
+			}
+		} else if (node is ObjectType) {
+			var type = (ObjectType) node;
+			string cname;
+			if (!type.value_owned) {
+				cname = get_ccode_const_cname (type.type_symbol);
+			} else {
+				cname = get_ccode_cname (type.type_symbol);
+			}
+			return "%s*".printf (cname);
+		} else if (node is ArrayType) {
+			var type = (ArrayType) node;
+			var cname = get_ccode_cname (type.element_type);
+			if (type.inline_allocated) {
+				return cname;
+			} else {
+				return "%s*".printf (cname);
+			}
+		} else if (node is DelegateType) {
+			var type = (DelegateType) node;
+			return get_ccode_cname (type.delegate_symbol);
+		} else if (node is ErrorType) {
+			return "GError*";
+		} else if (node is GenericType) {
+			var type = (GenericType) node;
+			if (type.value_owned) {
+				return "gpointer";
+			} else {
+				return "gconstpointer";
+			}
+		} else if (node is MethodType || node is NullType) {
+			return "gpointer";
+		} else if (node is PointerType) {
+			var type = (PointerType) node;
+			if (type.base_type.data_type != null && type.base_type.data_type.is_reference_type ()) {
+				return get_ccode_cname (type.base_type);
+			} else {
+				return "%s*".printf (get_ccode_cname (type.base_type));
+			}
+		} else if (node is VoidType) {
+			return "void";
+		} else if (node is ClassType) {
+			var type = (ClassType) node;
+			return "%sClass*".printf (get_ccode_cname (type.class_symbol));
+		} else if (node is InterfaceType) {
+			var type = (InterfaceType) node;
+			return "%s*".printf (get_ccode_type_cname (type.interface_symbol));
+		} else if (node is ValueType) {
+			var type = (ValueType) node;
+			var cname = get_ccode_cname (type.type_symbol);
+			if (type.nullable) {
+				return "%s*".printf (cname);
+			} else {
+				return cname;
+			}
+		} else {
+			Report.error (node.source_reference, "Unresolved type reference");
+			return "";
+		}
+	}
+
+	public static string get_ccode_cname (CodeNode node) {
+		var value = node.get_attribute_argument ("CCode", "cname");
+		if (value == null) {
+			var def = get_ccode_default_cname (node);
+			node.set_attribute_default_argument ("CCode", "cname", def);
+			return def;
+		}
+		return (string) value;
+	}
+
+	public static string get_ccode_const_cname (CodeNode node) {
+		var value = node.get_attribute_argument ("CCode", "const_cname");
+		if (value == null) {
+			var def = get_ccode_cname (node);
+			if (node is Class) {
+				def = "const %s".printf (def);
+			}
+			node.set_attribute_default_argument ("CCode", "cname", def);
+			return def;
+		}
+		return (string) value;
+	}
+
+	public static string get_ccode_type_cname (CodeNode node) {
+		var value = node.get_attribute_argument ("CCode", "type_cname");
+		if (value == null) {
+			var def = "%sIface".printf (((Interface) node).name);
+			node.set_attribute_default_argument ("CCode", "type_cname", def);
+			return def;
+		}
+		return (string) value;
+	}
 }
 
 public class Vala.GLibValue : TargetValue {



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