[vala] Only use const for immutable structs



commit 0d26b4084827e502159eb751bbe848d858c238cf
Author: Jürg Billeter <j bitron ch>
Date:   Sun Sep 27 16:49:31 2009 +0200

    Only use const for immutable structs

 codegen/valaccodemethodmodule.vala |    2 +-
 vala/valaclass.vala                |    4 ++--
 vala/valacodewriter.vala           |   13 +++++++++----
 vala/valastruct.vala               |    7 +++----
 vapigen/valagidlparser.vala        |   16 ++++++++++------
 5 files changed, 25 insertions(+), 17 deletions(-)
---
diff --git a/codegen/valaccodemethodmodule.vala b/codegen/valaccodemethodmodule.vala
index bed3273..aa2c1fe 100644
--- a/codegen/valaccodemethodmodule.vala
+++ b/codegen/valaccodemethodmodule.vala
@@ -743,7 +743,7 @@ internal class Vala.CCodeMethodModule : CCodeStructModule {
 			if (param.parameter_type.data_type is Struct) {
 				var st = (Struct) param.parameter_type.data_type;
 				if (!st.is_simple_type () && param.direction == ParameterDirection.IN) {
-					if (st.use_const && !param.parameter_type.value_owned) {
+					if (st.is_immutable && !param.parameter_type.value_owned) {
 						ctypename = "const " + ctypename;
 					}
 
diff --git a/vala/valaclass.vala b/vala/valaclass.vala
index bf4105e..c04ce0a 100644
--- a/vala/valaclass.vala
+++ b/vala/valaclass.vala
@@ -110,7 +110,7 @@ public class Vala.Class : ObjectTypeSymbol {
 	public bool free_function_address_of { get; private set; }
 
 	private string cname;
-	private string const_cname;
+	public string const_cname { get; set; }
 	private string lower_case_cprefix;
 	private string lower_case_csuffix;
 	private string type_id;
@@ -601,7 +601,7 @@ public class Vala.Class : ObjectTypeSymbol {
 	public void set_cname (string cname) {
 		this.cname = cname;
 	}
-	
+
 	private string get_lower_case_csuffix () {
 		if (lower_case_csuffix == null) {
 			lower_case_csuffix = camel_case_to_lower_case (name);
diff --git a/vala/valacodewriter.vala b/vala/valacodewriter.vala
index cf6a682..21a40da 100644
--- a/vala/valacodewriter.vala
+++ b/vala/valacodewriter.vala
@@ -190,6 +190,9 @@ public class Vala.CodeWriter : CodeVisitor {
 		if (cl.get_cname () != cl.get_default_cname ()) {
 			write_string ("cname = \"%s\", ".printf (cl.get_cname ()));
 		}
+		if (cl.const_cname != null) {
+			write_string ("const_cname = \"%s\", ".printf (cl.const_cname));
+		}
 
 		if (cl.type_check_function != null) {
 			write_string ("type_check_function = \"%s\", ".printf (cl.type_check_function ));
@@ -304,6 +307,12 @@ public class Vala.CodeWriter : CodeVisitor {
 			return;
 		}
 		
+		if (st.is_immutable) {
+			write_indent ();
+			write_string ("[Immutable]");
+			write_newline ();
+		}
+
 		write_indent ();
 
 		write_string ("[CCode (");
@@ -316,10 +325,6 @@ public class Vala.CodeWriter : CodeVisitor {
 			write_string ("type_id = \"%s\", ".printf (st.get_type_id ()));
 		}
 
-                if (!st.use_const) {
-                        write_string ("use_const = false, ");
-                }
-
                 if (!st.has_copy_function) {
                         write_string ("has_copy_function = false, ");
                 }
diff --git a/vala/valastruct.vala b/vala/valastruct.vala
index 95e8bf0..0d0d2ed 100644
--- a/vala/valastruct.vala
+++ b/vala/valastruct.vala
@@ -85,7 +85,7 @@ public class Vala.Struct : TypeSymbol {
 	 * Specifies if 'const' should be emitted for input parameters
 	 * of this type.
 	 */
-	public bool use_const { get; set; default = true; }
+	public bool is_immutable { get; set; }
 
 	/**
 	 * Specifies whether this struct has a registered GType.
@@ -450,9 +450,6 @@ public class Vala.Struct : TypeSymbol {
 		if (a.has_argument ("has_destroy_function")) {
 			has_destroy_function = a.get_bool ("has_destroy_function");
 		}
-		if (a.has_argument ("use_const")) {
-			use_const = a.get_bool ("use_const");
-		}
 	}
 
 	private void process_boolean_type_attribute (Attribute a) {
@@ -495,6 +492,8 @@ public class Vala.Struct : TypeSymbol {
 				process_integer_type_attribute (a);
 			} else if (a.name == "FloatingType") {
 				process_floating_type_attribute (a);
+			} else if (a.name == "Immutable") {
+				is_immutable = true;
 			}
 		}
 	}
diff --git a/vapigen/valagidlparser.vala b/vapigen/valagidlparser.vala
index 6cb068f..f133bf6 100644
--- a/vapigen/valagidlparser.vala
+++ b/vapigen/valagidlparser.vala
@@ -430,9 +430,9 @@ public class Vala.GIdlParser : CodeVisitor {
 							if (eval (nv[1]) == "1") {
 								st.set_simple_type (true);
 							}
-						} else if (nv[0] == "use_const") {
-							if (eval (nv[1]) == "0") {
-								st.use_const = false;
+						} else if (nv[0] == "immutable") {
+							if (eval (nv[1]) == "1") {
+								st.is_immutable = true;
 							}
 						} else if (nv[0] == "has_type_id") {
 							if (eval (nv[1]) == "0") {
@@ -504,6 +504,8 @@ public class Vala.GIdlParser : CodeVisitor {
 							if (eval (nv[1]) == "1") {
 								cl.is_immutable = true;
 							}
+						} else if (nv[0] == "const_cname") {
+							cl.const_cname = eval (nv[1]);
 						} else if (nv[0] == "is_fundamental") {
 							if (eval (nv[1]) == "1") {
 								cl.is_compact = false;
@@ -735,9 +737,9 @@ public class Vala.GIdlParser : CodeVisitor {
 						var nv = attr.split ("=", 2);
 						if (nv[0] == "cheader_filename") {
 							st.add_cheader_filename (eval (nv[1]));
-						} else if (nv[0] == "use_const") {
-							if (eval (nv[1]) == "0") {
-								st.use_const = false;
+						} else if (nv[0] == "immutable") {
+							if (eval (nv[1]) == "1") {
+								st.is_immutable = true;
 							}
 						} else if (nv[0] == "has_copy_function") {
 							if (eval (nv[1]) == "0") {
@@ -790,6 +792,8 @@ public class Vala.GIdlParser : CodeVisitor {
 							if (eval (nv[1]) == "1") {
 								cl.is_immutable = true;
 							}
+						} else if (nv[0] == "const_cname") {
+							cl.const_cname = eval (nv[1]);
 						}
 					}
 				}



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