[vala] Support array_length_type for fields



commit 1803fb26dffce892ca6fbf258970e56bed06b0dd
Author: Evan Nemerson <evan coeus-group com>
Date:   Sat Mar 13 18:05:14 2010 +0100

    Support array_length_type for fields
    
    Fixes part of bug 529866.

 codegen/valaccodearraymodule.vala |    4 ++++
 vala/valacodewriter.vala          |   21 ++++++++++++++++-----
 vala/valafield.vala               |    8 ++++++++
 vapigen/valagidlparser.vala       |   12 ++++++++++--
 4 files changed, 38 insertions(+), 7 deletions(-)
---
diff --git a/codegen/valaccodearraymodule.vala b/codegen/valaccodearraymodule.vala
index ec7dd83..72d169f 100644
--- a/codegen/valaccodearraymodule.vala
+++ b/codegen/valaccodearraymodule.vala
@@ -311,6 +311,10 @@ internal class Vala.CCodeArrayModule : CCodeMethodCallModule {
 						} else {
 							length_expr = new CCodeMemberAccess (inst, length_cname);
 						}
+
+						if (field.array_length_type != null) {
+							length_expr = new CCodeCastExpression (length_expr, "gint");
+						}
 					} else {
 						length_expr = new CCodeIdentifier (get_array_length_cname (field.get_cname (), dim));
 					}
diff --git a/vala/valacodewriter.vala b/vala/valacodewriter.vala
index 98eee7c..bcfc8aa 100644
--- a/vala/valacodewriter.vala
+++ b/vala/valacodewriter.vala
@@ -628,7 +628,8 @@ public class Vala.CodeWriter : CodeVisitor {
 		bool custom_ctype = (f.get_ctype () != null);
 		bool custom_cheaders = (f.parent_symbol is Namespace);
 		bool custom_array_length_cname = (f.get_array_length_cname () != null);
-		if (custom_cname || custom_ctype || custom_cheaders || custom_array_length_cname || (f.no_array_length && f.field_type is ArrayType)) {
+		bool custom_array_length_type = (f.array_length_type != null);
+		if (custom_cname || custom_ctype || custom_cheaders || custom_array_length_cname || custom_array_length_type || (f.no_array_length && f.field_type is ArrayType)) {
 			write_indent ();
 			write_string ("[CCode (");
 
@@ -663,12 +664,22 @@ public class Vala.CodeWriter : CodeVisitor {
 					if (f.array_null_terminated) {
 						write_string (", array_null_terminated = true");
 					}
-				} else if (custom_array_length_cname) {
-					if (custom_cname || custom_ctype || custom_cheaders) {
-						write_string (", ");
+				} else {
+					if (custom_array_length_cname) {
+						if (custom_cname || custom_ctype || custom_cheaders) {
+							write_string (", ");
+						}
+
+						write_string ("array_length_cname = \"%s\"".printf (f.get_array_length_cname ()));
 					}
 
-					write_string ("array_length_cname = \"%s\"".printf (f.get_array_length_cname ()));
+					if (custom_array_length_type) {
+						if (custom_cname || custom_ctype || custom_cheaders || custom_array_length_cname) {
+							write_string (", ");
+						}
+
+						write_string ("array_length_type = \"%s\"".printf (f.array_length_type));
+					}
 				}
 			}
 
diff --git a/vala/valafield.vala b/vala/valafield.vala
index 209c024..87d12c1 100644
--- a/vala/valafield.vala
+++ b/vala/valafield.vala
@@ -93,6 +93,11 @@ public class Vala.Field : Member, Lockable {
 		get { return (array_length_cexpr != null); }
 	}
 
+	/**
+	 * Specifies a custom type for the array length.
+	 */
+	public string? array_length_type { get; set; default = null; }
+
 	private string? array_length_cname;
 
 	private string? array_length_cexpr;
@@ -229,6 +234,9 @@ public class Vala.Field : Member, Lockable {
 		if (a.has_argument ("array_length_cexpr")) {
 			set_array_length_cexpr (a.get_string ("array_length_cexpr"));
 		}
+		if (a.has_argument ("array_length_type")) {
+			array_length_type = a.get_string ("array_length_type");
+		}
 		if (a.has_argument ("delegate_target")) {
 			no_delegate_target = !a.get_bool ("delegate_target");
 		}
diff --git a/vapigen/valagidlparser.vala b/vapigen/valagidlparser.vala
index 47e1fea..72f1017 100644
--- a/vapigen/valagidlparser.vala
+++ b/vapigen/valagidlparser.vala
@@ -1974,6 +1974,7 @@ public class Vala.GIdlParser : CodeVisitor {
 		string cheader_filename = null;
 		string ctype = null;
 		string array_length_cname = null;
+		string array_length_type = null;
 		bool array_null_terminated = false;
 
 		var attributes = get_attributes ("%s.%s".printf (current_data_type.get_cname (), node.name));
@@ -2018,6 +2019,8 @@ public class Vala.GIdlParser : CodeVisitor {
 					}
 				} else if (nv[0] == "array_length_cname") {
 					array_length_cname = eval (nv[1]);
+				} else if (nv[0] == "array_length_type") {
+					array_length_type = eval (nv[1]);
 				}
 			}
 		}
@@ -2055,8 +2058,13 @@ public class Vala.GIdlParser : CodeVisitor {
 			field.array_null_terminated = true;
 		}
 
-		if (array_length_cname != null) {
-			field.set_array_length_cname (array_length_cname);
+		if (array_length_cname != null || array_length_type != null) {
+			if (array_length_cname != null) {
+				field.set_array_length_cname (array_length_cname);
+			}
+			if (array_length_type != null) {
+				field.array_length_type = array_length_type;
+			}
 		} else {
 			field.no_array_length = true;
 		}



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