[vala/wip/attributes: 39/119] Add helper functions for attributes in CodeNode



commit 21683afbc5d664fee95971fb83f07554ed2d8cde
Author: Luca Bruno <lucabru src gnome org>
Date:   Tue Jun 28 23:14:47 2011 +0200

    Add helper functions for attributes in CodeNode

 codegen/valaccodebasemodule.vala     |   16 +---
 codegen/valaccodedelegatemodule.vala |    7 +--
 codegen/valagdbusmodule.vala         |   22 +----
 codegen/valagdbusservermodule.vala   |   10 +--
 codegen/valagvariantmodule.vala      |   30 ++-----
 vala/valaclass.vala                  |    5 +-
 vala/valacodenode.vala               |  165 ++++++++++++++++++++++++++++++++-
 vala/valacreationmethod.vala         |    6 +-
 vala/valaenum.vala                   |    5 +-
 vala/valafield.vala                  |   13 +---
 vala/valagirparser.vala              |    2 +-
 vala/valainterface.vala              |    5 +-
 vala/valastruct.vala                 |    9 +--
 vapigen/valagidlparser.vala          |    2 +-
 14 files changed, 194 insertions(+), 103 deletions(-)
---
diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala
index 2a87565..2072327 100644
--- a/codegen/valaccodebasemodule.vala
+++ b/codegen/valaccodebasemodule.vala
@@ -5674,9 +5674,9 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
 
 	public static string get_ccode_type_check_function (TypeSymbol sym) {
 		var cl = sym as Class;
-		var a = sym.get_attribute ("CCode");
-		if (cl != null && a != null && a.has_argument ("type_check_function")) {
-			return a.get_string ("type_check_function");
+		var a = sym.get_attribute_string ("CCode", "type_check_function");
+		if (cl != null && a != null) {
+			return a;
 		} else if ((cl != null && cl.is_compact) || sym is Struct || sym is Enum || sym is Delegate) {
 			return null;
 		} else {
@@ -5690,7 +5690,6 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
 
 	public static bool get_ccode_has_copy_function (Struct st) {
 		var a = st.get_attribute ("CCode");
-		return a != null && a.get_bool ("has_copy_function");
 		if (a != null && a.has_argument ("has_copy_function")) {
 			return a.get_bool ("has_copy_function");
 		}
@@ -5769,16 +5768,11 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
 	}
 
 	public static string? get_ccode_type (CodeNode node) {
-		var a = node.get_attribute ("CCode");
-		if (a != null) {
-			return a.get_string ("type");
-		}
-		return null;
+		return node.get_attribute_string ("CCode", "type");
 	}
 
 	public static bool get_ccode_simple_generics (Method m) {
-		var a = node.get_attribute ("CCode");
-		return a != null && a.get_bool ("simple_generics");
+		return node.get_attribute_bool ("CCode", "simple_generics");
 	}
 
 	public static string get_ccode_vfunc_name (Method m) {
diff --git a/codegen/valaccodedelegatemodule.vala b/codegen/valaccodedelegatemodule.vala
index 52eca76..cdce4e2 100644
--- a/codegen/valaccodedelegatemodule.vala
+++ b/codegen/valaccodedelegatemodule.vala
@@ -225,12 +225,7 @@ public class Vala.CCodeDelegateModule : CCodeArrayModule {
 			    && param.variable_type is ArrayType
 			    && ((ArrayType) param.variable_type).element_type.data_type == string_type.data_type) {
 				// use null-terminated string arrays for dynamic signals for compatibility reasons
-				var a = param.get_attribute ("CCode");
-				if (a == null) {
-					a = new Attribute ("CCode", param.source_reference);
-					param.attributes.append (a);
-				}
-				a.add_argument ("array_length", "false");
+				param.set_attribute_bool ("CCode", "array_length", false);
 				param.array_null_terminated = true;
 			}
 
diff --git a/codegen/valagdbusmodule.vala b/codegen/valagdbusmodule.vala
index 095340d..4189abf 100644
--- a/codegen/valagdbusmodule.vala
+++ b/codegen/valagdbusmodule.vala
@@ -22,32 +22,20 @@
 
 public class Vala.GDBusModule : GVariantModule {
 	public static string? get_dbus_name (TypeSymbol symbol) {
-		var dbus = symbol.get_attribute ("DBus");
-		if (dbus == null) {
-			return null;
-		}
-
-		return dbus.get_string ("name");
+		return symbol.get_attribute_string ("DBus", "name");
 	}
 
 	public static string get_dbus_name_for_member (Symbol symbol) {
-		var dbus = symbol.get_attribute ("DBus");
-		if (dbus != null && dbus.has_argument ("name")) {
-			return dbus.get_string ("name");
+		var dbus_name = symbol.get_attribute_string ("DBus", "name");
+		if (dbus_name = null) {
+			return dbus_name;
 		}
 
 		return Symbol.lower_case_to_camel_case (symbol.name);
 	}
 
 	public static bool is_dbus_no_reply (Method m) {
-		var dbus_attribute = m.get_attribute ("DBus");
-		if (dbus_attribute != null
-		    && dbus_attribute.has_argument ("no_reply")
-		    && dbus_attribute.get_bool ("no_reply")) {
-			return true;
-		}
-
-		return false;
+		return m.get_attribute_bool ("DBus", "no_reply");
 	}
 
 	public override void visit_error_domain (ErrorDomain edomain) {
diff --git a/codegen/valagdbusservermodule.vala b/codegen/valagdbusservermodule.vala
index f07753d..f4b971c 100644
--- a/codegen/valagdbusservermodule.vala
+++ b/codegen/valagdbusservermodule.vala
@@ -33,13 +33,9 @@ public class Vala.GDBusServerModule : GDBusClientModule {
 	}
 
 	public static string dbus_result_name (Method m) {
-		var dbus_attribute = m.get_attribute ("DBus");
-		if (dbus_attribute != null
-		    && dbus_attribute.has_argument ("result")) {
-			var result_name = dbus_attribute.get_string ("result");
-			if (result_name != null && result_name != "") {
-				return result_name;
-			}
+		var dbus_name = m.get_attribute_string ("DBus", "result");
+		if (dbus_name != null && dbus_name != "") {
+			return dbus_name;
 		}
 
 		return "result";
diff --git a/codegen/valagvariantmodule.vala b/codegen/valagvariantmodule.vala
index 859d49d..d6e02fb 100644
--- a/codegen/valagvariantmodule.vala
+++ b/codegen/valagvariantmodule.vala
@@ -44,32 +44,21 @@ public class Vala.GVariantModule : GAsyncModule {
 
 	static bool is_string_marshalled_enum (TypeSymbol? symbol) {
 		if (symbol != null && symbol is Enum) {
-			var dbus = symbol.get_attribute ("DBus");
-			return dbus != null && dbus.get_bool ("use_string_marshalling");
+			return symbol.get_attribute_bool ("DBus", "use_string_marshalling");
 		}
 		return false;
 	}
 
 	string get_dbus_value (EnumValue value, string default_value) {
-		var dbus = value.get_attribute ("DBus");
-		if (dbus == null) {
-			return default_value;
+		var dbus_value = value.get_attribute_string ("DBus", "value");
+		if (dbus_value != null) {
+			return dbus_value;;
 		}
-
-		string dbus_value = dbus.get_string ("value");
-		if (dbus_value == null) {
-			return default_value;
-		}
-		return dbus_value;
+		return default_value;
 	}
 
 	public static string? get_dbus_signature (Symbol symbol) {
-		var dbus = symbol.get_attribute ("DBus");
-		if (dbus == null) {
-			return null;
-		}
-
-		return dbus.get_string ("signature");
+		return symbol.get_attribute_string ("DBus", "signature");
 	}
 
 	bool get_basic_type_info (string signature, out BasicTypeInfo basic_type) {
@@ -104,12 +93,7 @@ public class Vala.GVariantModule : GAsyncModule {
 		} else if (is_string_marshalled_enum (datatype.data_type)) {
 			return "s";
 		} else if (datatype.data_type != null) {
-			string sig = null;
-
-			var ccode = datatype.data_type.get_attribute ("CCode");
-			if (ccode != null) {
-				sig = ccode.get_string ("type_signature");
-			}
+			string sig = datatype.data_type.get_attribute_string ("CCode", "type_signature");
 
 			var st = datatype.data_type as Struct;
 			var en = datatype.data_type as Enum;
diff --git a/vala/valaclass.vala b/vala/valaclass.vala
index 2b30864..d25b49e 100644
--- a/vala/valaclass.vala
+++ b/vala/valaclass.vala
@@ -590,10 +590,7 @@ public class Vala.Class : ObjectTypeSymbol {
 		}
 
 		if (cname == null) {
-			var attr = get_attribute ("CCode");
-			if (attr != null) {
-				cname = attr.get_string ("cname");
-			}
+			cname = attr.get_attribute_string ("CCode", "cname");
 			if (cname == null) {
 				cname = get_default_cname ();
 			}
diff --git a/vala/valacodenode.vala b/vala/valacodenode.vala
index 40728a1..5b94886 100644
--- a/vala/valacodenode.vala
+++ b/vala/valacodenode.vala
@@ -154,6 +154,108 @@ public abstract class Vala.CodeNode {
 	}
 
 	/**
+	 * Adds the specified named attribute.
+	 *
+	 * @param name attribute name
+	 */
+	public void add_attribute (string name, SourceReference? source_reference = null) {
+		var a = get_attribute (name);
+		if (a == null) {
+			a = new Attribute (name, source_refernce);
+			attributes.append (a);
+		}
+		return a;
+	}
+
+	/**
+	 * Remove the specified named attribute.
+	 *
+	 * @param name attribute name
+	 */
+	public void remove_attribute (string name) {
+		var a = get_attribute (name);
+		if (a != null) {
+			attributes.remove (a);
+		}
+	}
+
+	/**
+	 * Remove the specified named attribute argument
+	 *
+	 * @param attribute attribute name
+	 * @param argument  argument name
+	 */
+	public void remove_attribute_argument (string attribute, string argument) {
+		var a = get_attribute (name);
+		if (a != null) {
+			a.attrs.remove (argument);
+			if (a.attrs.size == 0) {
+				attributes.remove (a);
+			}
+		}
+	}
+
+	/**
+	 * Returns the string value of the specified attribute argument.
+	 *
+	 * @param attribute attribute name
+	 * @param argument  argument name
+	 * @return          string value
+	 */
+	public string? get_attribute_string (string attribute, string argument) {
+		var a = get_attribute (attribute);
+		if (a == null) {
+			return null;
+		}
+		return a.get_string (argument);
+	}
+
+	/**
+	 * Returns the integer value of the specified attribute argument.
+	 *
+	 * @param attribute attribute name
+	 * @param argument  argument name
+	 * @return          integer value
+	 */
+	public int get_attribute_integer (string attribute, string argument) {
+		var a = get_attribute (attribute);
+		if (a == null) {
+			return null;
+		}
+		return a.get_integer (argument);
+	}
+
+	/**
+	 * Returns the double value of the specified attribute argument.
+	 *
+	 * @param attribute attribute name
+	 * @param argument  argument name
+	 * @return          double value
+	 */
+	public double get_attribute_double (string attribute, string argument) {
+		var a = get_attribute (attribute);
+		if (a == null) {
+			return null;
+		}
+		return a.get_double (argument);
+	}
+
+	/**
+	 * Returns the bool value of the specified attribute argument.
+	 *
+	 * @param attribute attribute name
+	 * @param argument  argument name
+	 * @return          bool value
+	 */
+	public bool get_attribute_bool (string attribute, string argument) {
+		var a = get_attribute (attribute);
+		if (a == null) {
+			return null;
+		}
+		return a.get_bool (argument);
+	}
+
+	/**
 	 * Sets the string value of the specified attribute argument.
 	 *
 	 * @param attribute attribute name
@@ -161,14 +263,60 @@ public abstract class Vala.CodeNode {
 	 * @param value     string value
 	 */
 	public void set_attribute_string (string attribute, string argument, string value, SourceReference? source_reference = null) {
-		var attr = get_attribute (attribute);
+		var a = get_attribute (attribute);
+		if (a == null) {
+			a = new Attribute (attribute, source_reference);
+			attributes.append (a);
+		}
+		a.add_argument (argument, "\"%s\"".printf (value));
+	}
 
-		if (attr == null) {
-			attr = new Attribute (attribute, source_reference);
-			attributes.append (attr);
+	/**
+	 * Sets the integer value of the specified attribute argument.
+	 *
+	 * @param attribute attribute name
+	 * @param argument  argument name
+	 * @param value     integer value
+	 */
+	public void set_attribute_integer (string attribute, string argument, int value, SourceReference? source_reference = null) {
+		var a = get_attribute (attribute);
+		if (a == null) {
+			a = new Attribute (attribute, source_reference);
+			attributes.append (a);
+		}
+		a.add_argument (argument, value.to_string ());
+	}
+
+	/**
+	 * Sets the integer value of the specified attribute argument.
+	 *
+	 * @param attribute attribute name
+	 * @param argument  argument name
+	 * @param value     double value
+	 */
+	public void set_attribute_double (string attribute, string argument, double value, SourceReference? source_reference = null) {
+		var a = get_attribute (attribute);
+		if (a == null) {
+			a = new Attribute (attribute, source_reference);
+			attributes.append (a);
 		}
+		a.add_argument (argument, value.to_string ());
+	}
 
-		attr.add_argument (argument, "\"%s\"".printf (value));
+	/**
+	 * Sets the integer value of the specified attribute argument.
+	 *
+	 * @param attribute attribute name
+	 * @param argument  argument name
+	 * @param value     bool value
+	 */
+	public void set_attribute_bool (string attribute, string argument, int value, SourceReference? source_reference = null) {
+		var a = get_attribute (attribute);
+		if (a == null) {
+			a = new Attribute (attribute, source_reference);
+			attributes.append (a);
+		}
+		a.add_argument (argument, value.to_string ());
 	}
 
 	/**
@@ -195,6 +343,13 @@ public abstract class Vala.CodeNode {
 			attribute_caches.resize (hash * 2 + 1);
 		}
 		attribute_caches[hash] = cache;
+=======
+		var a = get_attribute (attribute);
+		if (a == null) {
+			a = new Attribute (attribute, source_reference);
+			attributes.append (a);
+		}
+		a.add_argument (argument, "\"%s\"".printf (value));
 	}
 
 	/**
diff --git a/vala/valacreationmethod.vala b/vala/valacreationmethod.vala
index 7ece293..27ce3c6 100644
--- a/vala/valacreationmethod.vala
+++ b/vala/valacreationmethod.vala
@@ -107,9 +107,9 @@ public class Vala.CreationMethod : Method {
 	}
 
 	public override string get_real_cname () {
-		var ccode_attribute = get_attribute ("CCode");
-		if (ccode_attribute != null && ccode_attribute.has_argument ("construct_function")) {
-			return ccode_attribute.get_string ("construct_function");
+		var func = get_attribute_string ("CCode", "construct_function");
+		if (func != null) {
+			return func;
 		}
 
 		return get_default_construct_function ();
diff --git a/vala/valaenum.vala b/vala/valaenum.vala
index 0acf6f9..4f7721c 100644
--- a/vala/valaenum.vala
+++ b/vala/valaenum.vala
@@ -155,10 +155,7 @@ public class Vala.Enum : TypeSymbol {
 
 	public override string get_cname (bool const_type = false) {
 		if (cname == null) {
-			var attr = get_attribute ("CCode");
-			if (attr != null) {
-				cname = attr.get_string ("cname");
-			}
+			cname = get_attribute_string ("CCode", "cname");
 			if (cname == null) {
 				cname = get_default_cname ();
 			}
diff --git a/vala/valafield.vala b/vala/valafield.vala
index 27eb95a..5c0cfa7 100644
--- a/vala/valafield.vala
+++ b/vala/valafield.vala
@@ -145,20 +145,11 @@ public class Vala.Field : Variable, Lockable {
 	}
 
 	public string? get_ctype () {
-		var attr = get_attribute ("CCode");
-		if (attr == null) {
-			return null;
-		}
-		return attr.get_string ("type");
+		return get_attribute_string ("CCode", "type");
 	}
 
 	public void set_ctype (string ctype) {
-		var attr = get_attribute ("CCode");
-		if (attr == null) {
-			attr = new Attribute ("CCode");
-			attributes.append (attr);
-		}
-		attr.add_argument ("type", "\"%s\"".printf (ctype));
+		set_attribute_string ("CCode", "type", ctype);
 	}
 
 	public override bool check (CodeContext context) {
diff --git a/vala/valagirparser.vala b/vala/valagirparser.vala
index a64f41c..1234225 100644
--- a/vala/valagirparser.vala
+++ b/vala/valagirparser.vala
@@ -2831,7 +2831,7 @@ public class Vala.GirParser : CodeVisitor {
 				st.set_cname (cname);
 			}
 			if (simple_type) {
-				st.set_simple_type (true);
+				st.set_simple_type ();
 			}
 			alias.symbol = st;
 		} else if (type_sym is Class) {
diff --git a/vala/valainterface.vala b/vala/valainterface.vala
index b56755b..f9ecc1e 100644
--- a/vala/valainterface.vala
+++ b/vala/valainterface.vala
@@ -282,10 +282,7 @@ public class Vala.Interface : ObjectTypeSymbol {
 
 	public override string get_cname (bool const_type = false) {
 		if (cname == null) {
-			var attr = get_attribute ("CCode");
-			if (attr != null) {
-				cname = attr.get_string ("cname");
-			}
+			cname = attr.get_attribute_string ("CCode", "cname");
 			if (cname == null) {
 				cname = "%s%s".printf (parent_symbol.get_cprefix (), name);
 			}
diff --git a/vala/valastruct.vala b/vala/valastruct.vala
index ecff5ce..1f458c8 100644
--- a/vala/valastruct.vala
+++ b/vala/valastruct.vala
@@ -277,10 +277,7 @@ public class Vala.Struct : TypeSymbol {
 		}
 		
 		if (cname == null) {
-			var attr = get_attribute ("CCode");
-			if (attr != null) {
-				cname = attr.get_string ("cname");
-			}
+			cname = attr.get_attribute_string ("CCode", "cname");
 			if (cname == null) {
 				cname = get_default_cname ();
 			}
@@ -706,8 +703,8 @@ public class Vala.Struct : TypeSymbol {
 	 * Marks this struct as simple type, i.e. instances will be passed by
 	 * value.
 	 */
-	public void set_simple_type (bool simple_type) {
-		attributes.append (new Attribute ("SimpleType"));
+	public void set_simple_type () {
+		add_attribute ("SimpleType");
 	}
 
 	public override void replace_type (DataType old_type, DataType new_type) {
diff --git a/vapigen/valagidlparser.vala b/vapigen/valagidlparser.vala
index a25f478..a31f0f3 100644
--- a/vapigen/valagidlparser.vala
+++ b/vapigen/valagidlparser.vala
@@ -675,7 +675,7 @@ public class Vala.GIdlParser : CodeVisitor {
 							st.set_rank (int.parse (eval (nv[1])));
 						} else if (nv[0] == "simple_type") {
 							if (eval (nv[1]) == "1") {
-								st.set_simple_type (true);
+								st.set_simple_type ();
 							}
 						} else if (nv[0] == "immutable") {
 							if (eval (nv[1]) == "1") {



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