[vala] Do not create array lists where not necessary



commit 664ebfd2edfae79da695252a3ba90658a5741f40
Author: Jürg Billeter <j bitron ch>
Date:   Thu Aug 12 00:32:50 2010 +0200

    Do not create array lists where not necessary

 codegen/valaccodearraymodule.vala       |    4 +-
 codegen/valaccodebasemodule.vala        |   31 ++++++++++++++++++----------
 codegen/valaccodecontrolflowmodule.vala |    4 +-
 codegen/valadovabasemodule.vala         |   25 ++++++++++++++++-------
 vala/valaexpression.vala                |   16 ++++++++++++--
 vala/valasymbol.vala                    |   33 +++++++++++++++++++++---------
 6 files changed, 77 insertions(+), 36 deletions(-)
---
diff --git a/codegen/valaccodearraymodule.vala b/codegen/valaccodearraymodule.vala
index 96558f5..9d67ff3 100644
--- a/codegen/valaccodearraymodule.vala
+++ b/codegen/valaccodearraymodule.vala
@@ -173,7 +173,7 @@ public class Vala.CCodeArrayModule : CCodeMethodCallModule {
 			return (CCodeExpression) get_ccodenode (length_expr);
 		} else if (array_expr is MethodCall || array_expr is CastExpression || array_expr is SliceExpression) {
 			List<CCodeExpression> size = array_expr.get_array_sizes ();
-			if (size.size >= dim) {
+			if (size != null && size.size >= dim) {
 				return size[dim - 1];
 			}
 		} else if (array_expr.symbol_reference != null) {
@@ -325,7 +325,7 @@ public class Vala.CCodeArrayModule : CCodeMethodCallModule {
 				var prop = (Property) array_expr.symbol_reference;
 				if (!prop.no_array_length) {
 					List<CCodeExpression> size = array_expr.get_array_sizes ();
-					if (size.size >= dim) {
+					if (size != null && size.size >= dim) {
 						return size[dim - 1];
 					}
 				}
diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala
index b2871c5..0905507 100644
--- a/codegen/valaccodebasemodule.vala
+++ b/codegen/valaccodebasemodule.vala
@@ -3070,11 +3070,17 @@ public class Vala.CCodeBaseModule : CCodeModule {
 		 * expr.temp_vars = temp_vars;
 		 * when deep list copying works
 		 */
-		expr.temp_vars.clear ();
-		foreach (LocalVariable local in temp_vars) {
-			expr.temp_vars.add (local);
+		if (temp_vars.size > 0) {
+			if (expr.temp_vars == null) {
+				expr.temp_vars = new ArrayList<LocalVariable> ();
+			} else {
+				expr.temp_vars.clear ();
+			}
+			foreach (LocalVariable local in temp_vars) {
+				expr.add_temp_var (local);
+			}
+			temp_vars.clear ();
 		}
-		temp_vars.clear ();
 
 		if (((List<LocalVariable>) temp_ref_vars).size == 0) {
 			/* nothing to do without temporary variables */
@@ -3095,7 +3101,7 @@ public class Vala.CCodeBaseModule : CCodeModule {
 			}
 
 			full_expr_var = get_temp_variable (expr_type, true, expr, false);
-			expr.temp_vars.add (full_expr_var);
+			expr.add_temp_var (full_expr_var);
 		
 			expr_list.append_expression (new CCodeAssignment (get_variable_cexpression (full_expr_var.name), (CCodeExpression) expr.ccodenode));
 		}
@@ -3116,7 +3122,10 @@ public class Vala.CCodeBaseModule : CCodeModule {
 		temp_ref_vars.clear ();
 	}
 	
-	public void append_temp_decl (CCodeFragment cfrag, List<LocalVariable> temp_vars) {
+	public void append_temp_decl (CCodeFragment cfrag, List<LocalVariable>? temp_vars) {
+		if (temp_vars == null) {
+			return;
+		}
 		foreach (LocalVariable local in temp_vars) {
 			var cdecl = new CCodeDeclaration (local.variable_type.get_cname ());
 
@@ -3220,10 +3229,10 @@ public class Vala.CCodeBaseModule : CCodeModule {
 		temp_ref_vars.clear ();
 	}
 	
-	public void create_temp_decl (Statement stmt, List<LocalVariable> temp_vars) {
+	public void create_temp_decl (Statement stmt, List<LocalVariable>? temp_vars) {
 		/* declare temporary variables */
 		
-		if (temp_vars.size == 0) {
+		if (temp_vars == null || temp_vars.size == 0) {
 			/* nothing to do without temporary variables */
 			return;
 		}
@@ -3385,7 +3394,7 @@ public class Vala.CCodeBaseModule : CCodeModule {
 			ccomma.append_expression (get_variable_cexpression (return_expr_decl.name));
 
 			stmt.return_expression.ccodenode = ccomma;
-			stmt.return_expression.temp_vars.add (return_expr_decl);
+			stmt.return_expression.add_temp_var (return_expr_decl);
 		} else if ((current_method != null || current_property_accessor != null) && current_return_type is DelegateType) {
 			var delegate_type = (DelegateType) current_return_type;
 			if (delegate_type.delegate_symbol.has_target) {
@@ -3412,7 +3421,7 @@ public class Vala.CCodeBaseModule : CCodeModule {
 				ccomma.append_expression (get_variable_cexpression (return_expr_decl.name));
 
 				stmt.return_expression.ccodenode = ccomma;
-				stmt.return_expression.temp_vars.add (return_expr_decl);
+				stmt.return_expression.add_temp_var (return_expr_decl);
 			}
 		}
 
@@ -3626,7 +3635,7 @@ public class Vala.CCodeBaseModule : CCodeModule {
 		}
 
 		var regex_var = get_temp_variable (regex_type, true, expr, false);
-		expr.temp_vars.add (regex_var);
+		expr.add_temp_var (regex_var);
 
 		var cdecl = new CCodeDeclaration ("GRegex*");
 
diff --git a/codegen/valaccodecontrolflowmodule.vala b/codegen/valaccodecontrolflowmodule.vala
index 9daa528..7b938f8 100644
--- a/codegen/valaccodecontrolflowmodule.vala
+++ b/codegen/valaccodecontrolflowmodule.vala
@@ -47,7 +47,7 @@ public class Vala.CCodeControlFlowModule : CCodeMethodModule {
 	void visit_string_switch_statement (SwitchStatement stmt) {
 		// we need a temporary variable to save the property value
 		var temp_var = get_temp_variable (stmt.expression.value_type, stmt.expression.value_type.value_owned, stmt, false);
-		stmt.expression.temp_vars.add (temp_var);
+		stmt.expression.add_temp_var (temp_var);
 
 		var ctemp = get_variable_cexpression (temp_var.name);
 		var cinit = new CCodeAssignment (ctemp, (CCodeExpression) stmt.expression.ccodenode);
@@ -66,7 +66,7 @@ public class Vala.CCodeControlFlowModule : CCodeMethodModule {
 		var ccond = new CCodeConditionalExpression (cisnull, new CCodeConstant ("0"), cquark);
 
 		temp_var = get_temp_variable (gquark_type);
-		stmt.expression.temp_vars.add (temp_var);
+		stmt.expression.add_temp_var (temp_var);
 
 		int label_count = 0;
 
diff --git a/codegen/valadovabasemodule.vala b/codegen/valadovabasemodule.vala
index d1b9a6b..248c51e 100644
--- a/codegen/valadovabasemodule.vala
+++ b/codegen/valadovabasemodule.vala
@@ -1228,11 +1228,17 @@ internal class Vala.DovaBaseModule : CCodeModule {
 		 * expr.temp_vars = temp_vars;
 		 * when deep list copying works
 		 */
-		expr.temp_vars.clear ();
-		foreach (LocalVariable local in temp_vars) {
-			expr.temp_vars.add (local);
+		if (temp_vars.size > 0) {
+			if (expr.temp_vars == null) {
+				expr.temp_vars = new ArrayList<LocalVariable> ();
+			} else {
+				expr.temp_vars.clear ();
+			}
+			foreach (LocalVariable local in temp_vars) {
+				expr.temp_vars.add (local);
+			}
+			temp_vars.clear ();
 		}
-		temp_vars.clear ();
 
 		if (((List<LocalVariable>) temp_ref_vars).size == 0) {
 			/* nothing to do without temporary variables */
@@ -1245,7 +1251,7 @@ internal class Vala.DovaBaseModule : CCodeModule {
 		}
 
 		var full_expr_var = get_temp_variable (expr_type, true, expr);
-		expr.temp_vars.add (full_expr_var);
+		expr.add_temp_var (full_expr_var);
 
 		var expr_list = new CCodeCommaExpression ();
 		expr_list.append_expression (new CCodeAssignment (get_variable_cexpression (full_expr_var.name), (CCodeExpression) expr.ccodenode));
@@ -1263,7 +1269,10 @@ internal class Vala.DovaBaseModule : CCodeModule {
 		temp_ref_vars.clear ();
 	}
 
-	public void append_temp_decl (CCodeFragment cfrag, List<LocalVariable> temp_vars) {
+	public void append_temp_decl (CCodeFragment cfrag, List<LocalVariable>? temp_vars) {
+		if (temp_vars == null) {
+			return;
+		}
 		foreach (LocalVariable local in temp_vars) {
 			var cdecl = new CCodeDeclaration (local.variable_type.get_cname ());
 
@@ -1364,10 +1373,10 @@ internal class Vala.DovaBaseModule : CCodeModule {
 		temp_ref_vars.clear ();
 	}
 
-	public void create_temp_decl (Statement stmt, List<LocalVariable> temp_vars) {
+	public void create_temp_decl (Statement stmt, List<LocalVariable>? temp_vars) {
 		/* declare temporary variables */
 
-		if (temp_vars.size == 0) {
+		if (temp_vars == null || temp_vars.size == 0) {
 			/* nothing to do without temporary variables */
 			return;
 		}
diff --git a/vala/valaexpression.vala b/vala/valaexpression.vala
index afd54aa..23c01e2 100644
--- a/vala/valaexpression.vala
+++ b/vala/valaexpression.vala
@@ -61,9 +61,9 @@ public abstract class Vala.Expression : CodeNode {
 	 *
 	 * The code generator sets and uses them for memory management.
 	 */
-	public ArrayList<LocalVariable> temp_vars = new ArrayList<LocalVariable> ();
+	public ArrayList<LocalVariable> temp_vars;
 
-	private List<CCodeExpression> array_sizes = new ArrayList<CCodeExpression> ();
+	private List<CCodeExpression> array_sizes;
 
 	public CCodeExpression? delegate_target { get; set; }
 	public CCodeExpression? delegate_target_destroy_notify { get; set; }
@@ -89,10 +89,20 @@ public abstract class Vala.Expression : CodeNode {
 		return false;
 	}
 
+	public void add_temp_var (LocalVariable local) {
+		if (temp_vars == null) {
+			temp_vars = new ArrayList<LocalVariable> ();
+		}
+		temp_vars.add (local);
+	}
+
 	/**
 	 * Add an array size C code expression.
 	 */
 	public void append_array_size (CCodeExpression size) {
+		if (array_sizes == null) {
+			array_sizes = new ArrayList<CCodeExpression> ();
+		}
 		array_sizes.add (size);
 	}
 
@@ -100,7 +110,7 @@ public abstract class Vala.Expression : CodeNode {
 	 * Get the C code expression for array sizes for all dimensions
 	 * ascending from left to right.
 	 */
-	public List<CCodeExpression> get_array_sizes () {
+	public List<CCodeExpression>? get_array_sizes () {
 		return array_sizes;
 	}
 
diff --git a/vala/valasymbol.vala b/vala/valasymbol.vala
index 2fb6c3a..1aa5ed7 100644
--- a/vala/valasymbol.vala
+++ b/vala/valasymbol.vala
@@ -97,7 +97,7 @@ public abstract class Vala.Symbol : CodeNode {
 
 	public Comment? comment { get; set; }
 
-	private List<string> cheader_filenames = new ArrayList<string> ();
+	private List<string> cheader_filenames;
 
 	/**
 	 * Specifies whether this method explicitly hides a member of a base
@@ -233,6 +233,8 @@ public abstract class Vala.Symbol : CodeNode {
 		return "";
 	}
 
+	static List<string> _empty_string_list;
+
 	/**
 	 * Returns a list of C header filenames users of this symbol must
 	 * include.
@@ -240,16 +242,24 @@ public abstract class Vala.Symbol : CodeNode {
 	 * @return list of C header filenames for this symbol
 	 */
 	public virtual List<string> get_cheader_filenames () {
-		// parent_symbol can be null on incremental parsing
-		if (cheader_filenames.size == 0 && parent_symbol != null) {
-			/* default to header filenames of the namespace */
-			foreach (string filename in parent_symbol.get_cheader_filenames ()) {
-				add_cheader_filename (filename);
-			}
+		if (cheader_filenames == null || cheader_filenames.size == 0) {
+			// parent_symbol can be null on incremental parsing
+			if (parent_symbol != null) {
+				/* default to header filenames of the namespace */
+				var parent_header_filenames = parent_symbol.get_cheader_filenames ();
+				if (parent_header_filenames.size > 0) {
+					return parent_header_filenames;
+				}
 
-			if (cheader_filenames.size == 0 && source_reference != null && !external_package) {
-				// don't add default include directives for VAPI files
-				cheader_filenames.add (source_reference.file.get_cinclude_filename ());
+				if (source_reference != null && !external_package) {
+					// don't add default include directives for VAPI files
+					add_cheader_filename (source_reference.file.get_cinclude_filename ());
+				}
+			} else {
+				if (_empty_string_list == null) {
+					_empty_string_list = new ArrayList<string> ();
+				}
+				return _empty_string_list;
 			}
 		}
 		return cheader_filenames;
@@ -455,6 +465,9 @@ public abstract class Vala.Symbol : CodeNode {
 	 * @param filename a C header filename
 	 */
 	public void add_cheader_filename (string filename) {
+		if (cheader_filenames == null) {
+			cheader_filenames = new ArrayList<string> ();
+		}
 		cheader_filenames.add (filename);
 	}
 



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