A tree node must never be analyzed if it is not attached to the tree for the
current compilation, because the parent link is used for numerous semantic
checks and code insertions. This patch fixes a violation of this rule in the
analysis of array aggregates, where expressions in component associations are
copied to be perfom some semantic checks before being fully analyzed.
A common use of the parent field is the determination of the proper point of
insertion for generated code, in Insert_Actions. In this case, Insert_Actions
was being called to create a reference for an itype, for use by the back-end.
Such a reference is not needed if expansion is currently disabled.

The following must compile quietly:

function To_String(Item : in String) return String is
   begin
      if Item'length <= 1 then
         return Item;
      end if;
      if Item(Item'first) = '<' and then Item(Item'last) = '>' then
         if Item'length = 2 then
            return "";
         end if;
         return String'
              (1 .. 1 =>
                  Character'val
                    (Integer'value
                       (Item(Item'first + 1 .. Item'last - 1))));
      end if;
      return String'(1 .. 1 => Character'value(Item));
end To_String;

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

2011-08-02  Ed Schonberg  <schonb...@adacore.com>

        * sem_aggr.adb (Resolve_Array_Aggregate): when copying the expression
        in an association, set parent field of copy before partial analysis.
        * sem_res.adb (Resolve_Slice): create reference to itype only when
        expansion is enabled.

Index: sem_aggr.adb
===================================================================
--- sem_aggr.adb        (revision 177175)
+++ sem_aggr.adb        (working copy)
@@ -1974,6 +1974,11 @@
                   begin
                      Expander_Mode_Save_And_Set (False);
                      Full_Analysis := False;
+
+                     --  Analyze the expression, making sure it is properly
+                     --  attached to the tree before we do the analysis.
+
+                     Set_Parent (Expr, Parent (Expression (Assoc)));
                      Analyze (Expr);
 
                      --  If the expression is a literal, propagate this info
Index: sem_res.adb
===================================================================
--- sem_res.adb (revision 177177)
+++ sem_res.adb (working copy)
@@ -9817,9 +9817,10 @@
       --  so that the itype is frozen at the proper place in the tree (i.e. at
       --  the point where actions for the slice are analyzed). Note that this
       --  is different from freezing the itype immediately, which might be
-      --  premature (e.g. if the slice is within a transient scope).
+      --  premature (e.g. if the slice is within a transient scope). This needs
+      --  to be done only if expansion is enabled.
 
-      else
+      elsif Expander_Active then
          Ensure_Defined (Typ => Slice_Subtype, N => N);
       end if;
    end Set_Slice_Subtype;

Reply via email to