[Ada] Attribute Loop_Entry and index check generation

Message ID 20130104090901.GA17262@adacore.com
State New
Headers show

Commit Message

Arnaud Charlet Jan. 4, 2013, 9:09 a.m.
This patch delays the generation of index checks for the following cases of
Loop_Entry attribute references:

   Prefix'Loop_Entry (Expr)
   Prefix'Loop_Entry (Expr1, Expr2, ... ExprN)

Even though these constructs appear initially as attribute references, analysis
converts them into indexed components to reflect their true semantics. Without
this patch, expansion of the indexed components would generate index checks of
the following form

   [constraint_error when not
     (blah in a'loop_entry'first .. a'loop_entry'last) "index check failed"]

and the back end would subsequently fail because it cannot process Loop_Entry.

Tested on x86_64-pc-linux-gnu, committed on trunk

2013-01-04  Hristian Kirtchev  <kirtchev@adacore.com>

	* checks.adb (Generate_Index_Checks): Delay the generation of
	the check for an indexed component where the prefix mentions
	Loop_Entry until the attribute has been properly expanded.
	* exp_ch5.adb (Expand_Loop_Entry_Attributes): Perform minor
	decoration of the constant that captures the value of Loop_Entry's
	prefix at the entry point into a loop.	Generate index checks
	for an attribute reference that has been transformed into an
	indexed component.


Index: exp_ch5.adb
--- exp_ch5.adb	(revision 194841)
+++ exp_ch5.adb	(working copy)
@@ -1828,11 +1828,29 @@ 
              Object_Definition   => New_Reference_To (Typ, Loc),
              Expression          => Relocate_Node (Prefix (LE))));
+         --  Perform minor decoration as this information will be needed for
+         --  the creation of index checks (if applicable).
+         Set_Ekind (Temp, E_Constant);
+         Set_Etype (Temp, Typ);
          --  Replace the original attribute with a reference to the constant
          Rewrite (LE, New_Reference_To (Temp, Loc));
          Set_Etype (LE, Typ);
+         --  Analysis converts attribute references of the following form
+         --     Prefix'Loop_Entry (Expr)
+         --     Prefix'Loop_Entry (Expr1, Expr2, ... ExprN)
+         --  into indexed components for error detection purposes. Generate
+         --  index checks now that 'Loop_Entry has been properly expanded.
+         if Nkind (Parent (LE)) = N_Indexed_Component then
+            Generate_Index_Checks (Parent (LE));
+         end if;
          Next_Elmt (LE_Elmt);
       end loop;
Index: checks.adb
--- checks.adb	(revision 194848)
+++ checks.adb	(working copy)
@@ -5522,6 +5522,23 @@ 
         or else Index_Checks_Suppressed (Etype (A))
+      --  The indexed component we are dealing with contains 'Loop_Entry in its
+      --  prefix. This case arises when analysis has determined that constructs
+      --  such as
+      --     Prefix'Loop_Entry (Expr)
+      --     Prefix'Loop_Entry (Expr1, Expr2, ... ExprN)
+      --  require rewriting for error detection purposes. A side effect of this
+      --  action is the generation of index checks that mention 'Loop_Entry.
+      --  Delay the generation of the check until 'Loop_Entry has been properly
+      --  expanded. This is done in Expand_Loop_Entry_Attributes.
+      elsif Nkind (Prefix (N)) = N_Attribute_Reference
+        and then Attribute_Name (Prefix (N)) = Name_Loop_Entry
+      then
+         return;
       end if;
       --  Generate a raise of constraint error with the appropriate reason and