vala r1145 - in trunk: . gobject vala vapigen



Author: juergbi
Date: Fri Mar 21 16:37:04 2008
New Revision: 1145
URL: http://svn.gnome.org/viewvc/vala?rev=1145&view=rev

Log:
2008-03-21  Juerg Billeter  <j bitron ch>

	* vala/valaenum.vala, vala/valaenumvalue.vala,
	  vala/valainterfacewriter.vala, gobject/valaccodegenerator.vala,
	  gobject/valaccodegeneratorinterface.vala,
	  vapigen/valagidlparser.vala: register enums with GType,
	  patch by Marc-Andre Lureau, fixes bug 517294


Modified:
   trunk/ChangeLog
   trunk/gobject/valaccodegenerator.vala
   trunk/gobject/valaccodegeneratorinterface.vala
   trunk/vala/valaenum.vala
   trunk/vala/valaenumvalue.vala
   trunk/vala/valainterfacewriter.vala
   trunk/vapigen/valagidlparser.vala

Modified: trunk/gobject/valaccodegenerator.vala
==============================================================================
--- trunk/gobject/valaccodegenerator.vala	(original)
+++ trunk/gobject/valaccodegenerator.vala	Fri Mar 21 16:37:04 2008
@@ -275,9 +275,101 @@
 		if (en.source_reference.comment != null) {
 			header_type_definition.append (new CCodeComment (en.source_reference.comment));
 		}
-		header_type_definition.append (cenum);
+
+		CCodeFragment decl_frag;
+		CCodeFragment def_frag;
+		if (en.access != SymbolAccessibility.PRIVATE) {
+			decl_frag = header_type_declaration;
+			def_frag = header_type_definition;
+		} else {
+			decl_frag = source_type_member_declaration;
+			def_frag = source_type_member_declaration;
+		}
+
+		def_frag.append (cenum);
+		def_frag.append (new CCodeNewline ());
 
 		en.accept_children (this);
+
+		if (!en.has_type_id) {
+			return;
+		}
+
+		decl_frag.append (new CCodeNewline ());
+
+		var macro = "(%s_get_type ())".printf (en.get_lower_case_cname (null));
+		decl_frag.append (new CCodeMacroReplacement (en.get_upper_case_cname ("TYPE_"), macro));
+
+		var clist = new CCodeInitializerList (); /* or during visit time? */
+		CCodeInitializerList clist_ev = null;
+		foreach (EnumValue ev in en.get_values ()) {
+			clist_ev = new CCodeInitializerList ();
+			clist_ev.append (new CCodeConstant (ev.get_cname ()));
+			clist_ev.append (new CCodeIdentifier ("\"%s\"".printf (ev.get_cname ())));
+			clist_ev.append (ev.get_canonical_cconstant ());
+			clist.append (clist_ev);
+		}
+
+		clist_ev = new CCodeInitializerList ();
+		clist_ev.append (new CCodeConstant ("0"));
+		clist_ev.append (new CCodeConstant ("NULL"));
+		clist_ev.append (new CCodeConstant ("NULL"));
+		clist.append (clist_ev);
+
+		var enum_decl = new CCodeVariableDeclarator.with_initializer ("values[]", clist);
+
+		CCodeDeclaration cdecl = null;
+		if (en.is_flags) {
+			cdecl = new CCodeDeclaration ("const GFlagsValue");
+		} else {
+			cdecl = new CCodeDeclaration ("const GEnumValue");
+		}
+
+		cdecl.add_declarator (enum_decl);
+		cdecl.modifiers = CCodeModifiers.STATIC;
+
+		var type_init = new CCodeBlock ();
+
+		type_init.add_statement (cdecl);
+
+		var fun_name = "%s_get_type".printf (en.get_lower_case_cname (null));
+		var regfun = new CCodeFunction (fun_name, "GType");
+		var regblock = new CCodeBlock ();
+
+		cdecl = new CCodeDeclaration ("GType");
+		string type_id_name = "%s_type_id".printf (en.get_lower_case_cname (null));
+		cdecl.add_declarator (new CCodeVariableDeclarator.with_initializer (type_id_name, new CCodeConstant ("0")));
+		cdecl.modifiers = CCodeModifiers.STATIC;
+		regblock.add_statement (cdecl);
+
+		CCodeFunctionCall reg_call;
+		if (en.is_flags) {
+			reg_call = new CCodeFunctionCall (new CCodeIdentifier ("g_flags_register_static"));
+		} else {
+			reg_call = new CCodeFunctionCall (new CCodeIdentifier ("g_enum_register_static"));
+		}
+
+		reg_call.add_argument (new CCodeConstant ("\"%s\"".printf (en.get_cname())));
+		reg_call.add_argument (new CCodeIdentifier ("values"));
+
+		type_init.add_statement (new CCodeExpressionStatement (new CCodeAssignment (new CCodeIdentifier (type_id_name), reg_call)));
+
+		var cond = new CCodeFunctionCall (new CCodeIdentifier ("G_UNLIKELY"));
+		cond.add_argument (new CCodeBinaryExpression (CCodeBinaryOperator.EQUALITY, new CCodeIdentifier (type_id_name), new CCodeConstant ("0")));
+		var cif = new CCodeIfStatement (cond, type_init);
+		regblock.add_statement (cif);
+
+		regblock.add_statement (new CCodeReturnStatement (new CCodeConstant (type_id_name)));
+
+		if (en.access != SymbolAccessibility.PRIVATE) {
+			header_type_member_declaration.append (regfun.copy ());
+		} else {
+			source_type_member_declaration.append (regfun.copy ());
+		}
+		regfun.block = regblock;
+
+		source_type_member_definition.append (new CCodeNewline ());
+		source_type_member_definition.append (regfun);
 	}
 
 	public override void visit_enum_value (EnumValue! ev) {

Modified: trunk/gobject/valaccodegeneratorinterface.vala
==============================================================================
--- trunk/gobject/valaccodegeneratorinterface.vala	(original)
+++ trunk/gobject/valaccodegeneratorinterface.vala	Fri Mar 21 16:37:04 2008
@@ -104,8 +104,33 @@
 		} else if (prop.type_reference.data_type == string_type.data_type) {
 			cspec.call = new CCodeIdentifier ("g_param_spec_string");
 			cspec.add_argument (new CCodeConstant ("NULL"));
-		} else if (prop.type_reference.data_type == int_type.data_type
-			   || prop.type_reference.data_type is Enum) {
+		} else if (prop.type_reference.data_type is Enum) {
+			var e = prop.type_reference.data_type as Enum;
+			if (e.has_type_id) {
+				if (e.is_flags) {
+					cspec.call = new CCodeIdentifier ("g_param_spec_flags");
+				} else {
+					cspec.call = new CCodeIdentifier ("g_param_spec_enum");
+				}
+				cspec.add_argument (new CCodeIdentifier (e.get_type_id ()));
+			} else {
+				if (e.is_flags) {
+					cspec.call = new CCodeIdentifier ("g_param_spec_uint");
+					cspec.add_argument (new CCodeConstant ("0"));
+					cspec.add_argument (new CCodeConstant ("G_MAXUINT"));
+				} else {
+					cspec.call = new CCodeIdentifier ("g_param_spec_int");
+					cspec.add_argument (new CCodeConstant ("G_MININT"));
+					cspec.add_argument (new CCodeConstant ("G_MAXINT"));
+				}
+			}
+
+			if (prop.default_expression != null) {
+				cspec.add_argument ((CCodeExpression) prop.default_expression.ccodenode);
+			} else {
+				cspec.add_argument (new CCodeConstant (prop.type_reference.data_type.get_default_value ()));
+			}
+		} else if (prop.type_reference.data_type == int_type.data_type) {
 			cspec.call = new CCodeIdentifier ("g_param_spec_int");
 			cspec.add_argument (new CCodeConstant ("G_MININT"));
 			cspec.add_argument (new CCodeConstant ("G_MAXINT"));

Modified: trunk/vala/valaenum.vala
==============================================================================
--- trunk/vala/valaenum.vala	(original)
+++ trunk/vala/valaenum.vala	Fri Mar 21 16:37:04 2008
@@ -32,12 +32,18 @@
 	 */
 	public bool is_flags { get; set; }
 
+	/**
+	 * Specifies whether this enum has a registered GType.
+	 */
+	public bool has_type_id { get; set; default (true); }
+
 	private Gee.List<EnumValue> values = new ArrayList<EnumValue> ();
 	private Gee.List<Method> methods = new ArrayList<Method> ();
 	private string cname;
 	private string cprefix;
 	private string lower_case_cprefix;
 	private string lower_case_csuffix;
+	private string type_id;
 
 	/**
 	 * Creates a new enum.
@@ -140,8 +146,8 @@
 		return "%s%s%s".printf (parent_symbol.get_lower_case_cprefix (), infix, get_lower_case_csuffix ());
 	}
 
-	public override string get_upper_case_cname (string infix) {
-		return get_lower_case_cname (null).up ();
+	public override string get_upper_case_cname (string infix = null) {
+		return get_lower_case_cname (infix).up ();
 	}
 
 	public override bool is_reference_type () {
@@ -160,7 +166,7 @@
 	 */
 	public string! get_cprefix () {
 		if (cprefix == null) {
-			cprefix = "%s_".printf (get_upper_case_cname (null));
+			cprefix = "%s_".printf (get_upper_case_cname ());
 		}
 		return cprefix;
 	}
@@ -191,6 +197,12 @@
 				add_cheader_filename (filename);
 			}
 		}
+		if (a.has_argument ("has_type_id")) {
+			has_type_id = a.get_bool ("has_type_id");
+		}
+		if (a.has_argument ("type_id")) {
+			type_id = a.get_string ("type_id");
+		}
 	}
 	
 	/**
@@ -206,24 +218,68 @@
 		}
 	}
 
+	public void set_type_id (string! type_id) {
+		this.type_id = type_id;
+	}
+
 	public override string get_type_id () {
-		// FIXME: use GType-registered enums
-		return "G_TYPE_INT";
+		if (type_id == null) {
+			if (has_type_id) {
+				type_id = get_upper_case_cname ("TYPE_");
+			} else {
+				type_id = is_flags ? "G_TYPE_UINT" : "G_TYPE_INT";
+			}
+		}
+
+		return type_id;
 	}
 	
 	public override string get_marshaller_type_name () {
-		// FIXME: use GType-registered enums
+		if (has_type_id) {
+			if (is_flags) {
+				return "FLAGS";
+			} else {
+				return "ENUM";
+			}
+		} else {
+			if (is_flags) {
+				return "UINT";
+			} else {
 		return "INT";
 	}
+		}
+	}
 
 	public override string get_get_value_function () {
-		// FIXME: use GType-registered enums
+		if (has_type_id) {
+			if (is_flags) {
+				return "g_value_get_flags";
+			} else {
+				return "g_value_get_enum";
+			}
+		} else {
+			if (is_flags) {
+				return "g_value_get_uint";
+			} else {
 		return "g_value_get_int";
 	}
+		}
+	}
 	
 	public override string get_set_value_function () {
-		// FIXME: use GType-registered enums
+		if (has_type_id) {
+			if (is_flags) {
+				return "g_value_set_flags";
+			} else {
+				return "g_value_set_enum";
+			}
+		} else {
+			if (is_flags) {
 		return "g_value_set_int";
+			} else {
+				return "g_value_set_uint";
+			}
+		}
 	}
 
 	public override string get_default_value () {

Modified: trunk/vala/valaenumvalue.vala
==============================================================================
--- trunk/vala/valaenumvalue.vala	(original)
+++ trunk/vala/valaenumvalue.vala	Fri Mar 21 16:37:04 2008
@@ -54,6 +54,33 @@
 		this.source_reference = source_reference;
 	}
 	
+	/**
+	 * Returns the string literal of this signal to be used in C code.
+	 * (FIXME: from vlaasignal.vala)
+	 *
+	 * @return string literal to be used in C code
+	 */
+	public CCodeConstant! get_canonical_cconstant () {
+		var str = new String ("\"");
+
+		string i = name;
+
+		while (i.len () > 0) {
+			unichar c = i.get_char ();
+			if (c == '_') {
+				str.append_c ('-');
+			} else {
+				str.append_unichar (c.tolower ());
+			}
+
+			i = i.next_char ();
+		}
+
+		str.append_c ('"');
+
+		return new CCodeConstant (str.str);
+	}
+
 	public override void accept (CodeVisitor! visitor) {
 		visitor.visit_enum_value (this);
 	}

Modified: trunk/vala/valainterfacewriter.vala
==============================================================================
--- trunk/vala/valainterfacewriter.vala	(original)
+++ trunk/vala/valainterfacewriter.vala	Fri Mar 21 16:37:04 2008
@@ -310,7 +310,14 @@
 				cheaders = "%s,%s".printf (cheaders, cheader);
 			}
 		}
-		write_string ("[CCode (cprefix = \"%s\", cheader_filename = \"%s\")]".printf (en.get_cprefix (), cheaders));
+
+		write_string ("[CCode (cprefix = \"%s\", ".printf (en.get_cprefix ()));
+
+		if (!en.has_type_id) {
+			write_string ("has_type_id = \"%d\", ".printf (en.has_type_id));
+		}
+
+		write_string ("cheader_filename = \"%s\")]".printf (cheaders));
 
 		if (en.is_flags) {
 			write_indent ();

Modified: trunk/vapigen/valagidlparser.vala
==============================================================================
--- trunk/vapigen/valagidlparser.vala	(original)
+++ trunk/vapigen/valagidlparser.vala	Fri Mar 21 16:37:04 2008
@@ -712,9 +712,10 @@
 	
 	private Typesymbol parse_enum (IdlNodeEnum! en_node) {
 		weak IdlNode node = (IdlNode) en_node;
-	
+
 		var en = new Enum (node.name, current_source_reference);
 		en.access = SymbolAccessibility.PUBLIC;
+		en.has_type_id = (en_node.gtype_name != null && en_node.gtype_name != "");
 		
 		string common_prefix = null;
 		



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