[vala/emitlocal: 1/8] codegen: Add create_temp_value



commit 91200905b50eda14570860b5a6d79e0c04080a2e
Author: Luca Bruno <lucabru src gnome org>
Date:   Sat Jun 11 10:16:32 2011 +0200

    codegen: Add create_temp_value

 codegen/valaccodebasemodule.vala |   57 ++++++++++++++++++++++++++++++++++++++
 1 files changed, 57 insertions(+), 0 deletions(-)
---
diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala
index 62a6d45..59ecf5e 100644
--- a/codegen/valaccodebasemodule.vala
+++ b/codegen/valaccodebasemodule.vala
@@ -2106,6 +2106,63 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
 		local.active = true;
 	}
 
+	private void emit_temp_part (DataType type, string name, bool init) {
+		if (is_in_coroutine ()) {
+			closure_struct.add_field (type.get_cname (), name);
+
+			// even though closure struct is zerod, we need to initialize temporary variables
+			// as they might be used multiple times when declared in a loop
+
+			if (init) {
+				var initializer = default_value_for_type (type, false);
+				if (initializer == null) {
+					cfile.add_include ("string.h");
+					var memset_call = new CCodeFunctionCall (new CCodeIdentifier ("memset"));
+					memset_call.add_argument (new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, get_variable_cexpression (name)));
+					memset_call.add_argument (new CCodeConstant ("0"));
+					memset_call.add_argument (new CCodeIdentifier ("sizeof (%s)".printf (type.get_cname ())));
+					ccode.add_expression (memset_call);
+				} else {
+					ccode.add_assignment (get_variable_cexpression (name), initializer);
+				}
+			}
+		} else {
+			var cvar = new CCodeVariableDeclarator (name, null);
+			if (init) {
+				cvar.initializer = default_value_for_type (type, true);
+				cvar.init0 = true;
+			}
+			ccode.add_declaration (type.get_cname (), cvar);
+		}
+	}
+
+	public TargetValue create_temp_value (DataType type, bool init, CodeNode node_reference, bool? value_owned = null) {
+		var local = new LocalVariable (type.copy (), "_tmp%d_".printf (next_temp_var_id++), null, node_reference.source_reference);
+		if (value_owned != null) {
+			local.variable_type.value_owned = value_owned;
+		}
+
+		var array_type = local.variable_type as ArrayType;
+		var deleg_type = local.variable_type as DelegateType;
+		emit_temp_part (local.variable_type, local.name, init);
+		if (array_type != null) {
+			for (int dim = 1; dim <= array_type.rank; dim++) {
+				emit_temp_part (int_type, get_array_length_cname (local.name, dim), init);
+			}
+		} else if (deleg_type != null) {
+			if (deleg_type.delegate_symbol.has_target) {
+				emit_temp_part (new PointerType (new VoidType ()), get_delegate_target_cname (local.name), init);
+				if (deleg_type.value_owned) {
+					emit_temp_part (gdestroynotify_type, get_delegate_target_destroy_notify_cname (local.name), init);
+				}
+			}
+		}
+
+		var result = get_local_cvalue (local);
+		set_array_size_cvalue (result, null);
+		return result;
+	}
+
 	public override void visit_initializer_list (InitializerList list) {
 		if (list.target_type.data_type is Struct) {
 			/* initializer is used as struct initializer */



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