[phc-internals] [phc commit] r1426 - in trunk/src: hir_to_mir process_mir

codesite-noreply at google.com codesite-noreply at google.com
Thu Jul 3 22:38:56 IST 2008


Author: paul.biggar
Date: Thu Jul  3 14:38:03 2008
New Revision: 1426

Modified:
   trunk/src/hir_to_mir/Lower_control_flow.cpp
   trunk/src/process_mir/Foreach_uppering.cpp

Log:
If an array is used in a foreach, but is not an array, we must throw an 
error and convert it to an array. We did this in Foreach_uppering, but 
the appropriate place is in Lower_control_flow, as its necessary for 
generated code too. This moved it there.


Modified: trunk/src/hir_to_mir/Lower_control_flow.cpp
==============================================================================
--- trunk/src/hir_to_mir/Lower_control_flow.cpp	(original)
+++ trunk/src/hir_to_mir/Lower_control_flow.cpp	Thu Jul  3 14:38:03 2008
@@ -155,6 +155,11 @@
  *		 ...;
  *   }
  * into
+ *			if (!is_array ($arr))
+ *			{
+ *				trigger_error (\"Invalid argument supplied for foreach()\", E_USER_WARNING);"
+ *				$arr = (array)($arr);
+ *			}
  *			foreach_reset ($arr, iter);
  *		L0:
  *			$T = foreach_has_key ($arr, iter);
@@ -171,9 +176,20 @@

 void Lower_control_flow::lower_foreach (Foreach* in, List<Statement*>* out)
 {
+	VARIABLE_NAME* cast_array_name = fresh_var_name ("FE");
+
  	/* We wrap a number of MIR nodes in foreign, but we need to convert 
some of
 	 * them first. */
-	MIR::VARIABLE_NAME* array_name = fold_var (in->arr);
+	MIR::VARIABLE_NAME* array_name = fold_var (in->arr); // keep the attributes
+
+	(*out
+		<< "if (!is_array ($" << in->arr << ")) "
+		<< "{"
+		<< "	trigger_error (\"Invalid argument supplied for foreach()\", E_USER_WARNING);"
+		<< "	$" << in->arr << " = (array)($" << in->arr << ");"
+		<< "}"
+	).to_pass (s("HIR-to-MIR"), in);
+

 	// foreach_reset ($arr, iter);
 	MIR::HT_ITERATOR* iter = MIR::fresh_iter ();

Modified: trunk/src/process_mir/Foreach_uppering.cpp
==============================================================================
--- trunk/src/process_mir/Foreach_uppering.cpp	(original)
+++ trunk/src/process_mir/Foreach_uppering.cpp	Thu Jul  3 14:38:03 2008
@@ -13,8 +13,7 @@
  void Foreach_uppering::pre_foreach_reset (MIR::Foreach_reset* in, 
List<Statement*>* out)
 {
 	(*out
-		<< "if (!is_array ($" << in->array << ")) trigger_error (\"Invalid 
argument supplied for foreach()\", E_USER_WARNING);"
-		<< "$" << in->iter << " = new ArrayObject ((array)($" << in->array << "));\n"
+		<< "$" << in->iter << " = new ArrayObject ($" << in->array << ");\n"
 		<< "$" << in->iter << " = $" << in->iter << "->getIterator();"
 	).finish (in);
 }


More information about the phc-internals mailing list