[Ada] Handling of discriminated components with a box in record aggregates

Arnaud Charlet charlet@adacore.com
Wed Sep 12 12:31:00 GMT 2007


In Ada2005, if a record component is given a box association in an aggregate
for an enclosing type, and the component is of a discriminated type, we create
a subaggregate for it with explicit values for the discriminants, and box
associations for the remaining components. We retrieve values of discriminants
from the discriminant constraint of the corresponding subtype. However, the
constraint may be given by the discriminants of the enclosing record, whose
values must be present in the enclosing aggregate itself. We must scan the
list of previous associations of the aggregate to retrieve the current value
of the discriminant, and use this current value in the subaggregate.

See gnat.dg/discr_test2.adb

2007-09-10  Ed Schonberg  <schonberg@adacore.com>

	* sem_aggr.adb (Build_Record_Aggr_Code): If an aggregate component is
	given a box association, the type of the component is discriminated,
	and the value of the discriminant is the discriminant of the enclosing
	type, retrieve its value from the aggregate itself, where it must have
	been supplied.
	Update to GPLv3.

	* sem_ch4.adb (Analyze_One_Call): Further refinement to previous fix,
	to remove other spurious ambiguities on arithmetic operations involving
	literals and addresses, on systems where Address is a visible integer
	type, when the operator is called in functional notation.
	(Try_Primitive_Operation): Within an instance, a call in prefixed form
	is legal when the types match, even if the operation is currently
	hidden.
	Update to GPLv3.

-------------- next part --------------
Index: sem_aggr.adb
===================================================================
--- sem_aggr.adb	(revision 128327)
+++ sem_aggr.adb	(working copy)
@@ -10,14 +10,13 @@
 --                                                                          --
 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
 -- terms of the  GNU General Public License as published  by the Free Soft- --
--- ware  Foundation;  either version 2,  or (at your option) any later ver- --
+-- ware  Foundation;  either version 3,  or (at your option) any later ver- --
 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
 -- for  more details.  You should have  received  a copy of the GNU General --
--- Public License  distributed with GNAT;  see file COPYING.  If not, write --
--- to  the  Free Software Foundation,  51  Franklin  Street,  Fifth  Floor, --
--- Boston, MA 02110-1301, USA.                                              --
+-- Public License  distributed with GNAT; see file COPYING3.  If not, go to --
+-- http://www.gnu.org/licenses for a complete copy of the license.          --
 --                                                                          --
 -- GNAT was originally developed  by the GNAT team at  New York University. --
 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
@@ -3039,6 +3038,8 @@ package body Sem_Aggr is
 
                      declare
                         Loc        : constant Source_Ptr := Sloc (N);
+                        Assoc      : Node_Id;
+                        Discr      : Entity_Id;
                         Discr_Elmt : Elmt_Id;
                         Discr_Val  : Node_Id;
                         Expr       : Node_Id;
@@ -3050,6 +3051,32 @@ package body Sem_Aggr is
                           First_Elmt (Discriminant_Constraint (Ctyp));
                         while Present (Discr_Elmt) loop
                            Discr_Val := Node (Discr_Elmt);
+
+                           --  The constraint may be given by a discriminant
+                           --  of the enclosing type, in which case we have
+                           --  to retrieve its value, which is part of the
+                           --  current aggregate.
+
+                           if Is_Entity_Name (Discr_Val)
+                             and then
+                               Ekind (Entity (Discr_Val)) = E_Discriminant
+                           then
+                              Discr := Entity (Discr_Val);
+
+                              Assoc := First (New_Assoc_List);
+                              while Present (Assoc) loop
+                                 if Present
+                                   (Entity (First (Choices (Assoc))))
+                                   and then
+                                     Entity (First (Choices (Assoc))) = Discr
+                                 then
+                                    Discr_Val := Expression (Assoc);
+                                    exit;
+                                 end if;
+                                 Next (Assoc);
+                              end loop;
+                           end if;
+
                            Append
                              (New_Copy_Tree (Discr_Val), Expressions (Expr));
 
Index: sem_ch4.adb
===================================================================
--- sem_ch4.adb	(revision 128327)
+++ sem_ch4.adb	(working copy)
@@ -10,14 +10,13 @@
 --                                                                          --
 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
 -- terms of the  GNU General Public License as published  by the Free Soft- --
--- ware  Foundation;  either version 2,  or (at your option) any later ver- --
+-- ware  Foundation;  either version 3,  or (at your option) any later ver- --
 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
 -- for  more details.  You should have  received  a copy of the GNU General --
--- Public License  distributed with GNAT;  see file COPYING.  If not, write --
--- to  the  Free Software Foundation,  51  Franklin  Street,  Fifth  Floor, --
--- Boston, MA 02110-1301, USA.                                              --
+-- Public License  distributed with GNAT; see file COPYING3.  If not, go to --
+-- http://www.gnu.org/licenses for a complete copy of the license.          --
 --                                                                          --
 -- GNAT was originally developed  by the GNAT team at  New York University. --
 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
@@ -2001,6 +2000,19 @@ package body Sem_Ch4 is
       Subp_Type  : constant Entity_Id := Etype (Nam);
       Norm_OK    : Boolean;
 
+      function Operator_Hidden_By (Fun : Entity_Id) return Boolean;
+      --  There may be a user-defined operator that hides the current
+      --  interpretation. We must check for this independently of the
+      --  analysis of the call with the user-defined operation, because
+      --  the parameter names may be wrong and yet the hiding takes place.
+      --  This fixes a problem with ACATS test B34014O.
+      --
+      --  When the type Address is a visible integer type, and the DEC
+      --  system extension is visible, the predefined operator may be
+      --  hidden as well, by one of the address operations in auxdec.
+      --  Finally, The abstract operations on address do not hide the
+      --  predefined operator (this is the purpose of making them abstract).
+
       procedure Indicate_Name_And_Type;
       --  If candidate interpretation matches, indicate name and type of
       --  result on call node.
@@ -2040,17 +2052,60 @@ package body Sem_Ch4 is
          end if;
       end Indicate_Name_And_Type;
 
+      ------------------------
+      -- Operator_Hidden_By --
+      ------------------------
+
+      function Operator_Hidden_By (Fun : Entity_Id) return Boolean is
+         Act1  : constant Node_Id   := First_Actual (N);
+         Act2  : constant Node_Id   := Next_Actual (Act1);
+         Form1 : constant Entity_Id := First_Formal (Fun);
+         Form2 : constant Entity_Id := Next_Formal (Form1);
+
+      begin
+         if Ekind (Fun) /= E_Function
+           or else Is_Abstract_Subprogram (Fun)
+         then
+            return False;
+
+         elsif not Has_Compatible_Type (Act1, Etype (Form1)) then
+            return False;
+
+         elsif Present (Form2) then
+            if
+              No (Act2) or else not Has_Compatible_Type (Act2, Etype (Form2))
+            then
+               return False;
+            end if;
+
+         elsif Present (Act2) then
+            return False;
+         end if;
+
+         --  Now we know that the arity of the operator matches the function,
+         --  and the function call is a valid interpretation. The function
+         --  hides the operator if it has the right signature, or if one of
+         --  its operands is a non-abstract operation on Address when this is
+         --  a visible integer type.
+
+         return Hides_Op (Fun, Nam)
+           or else Is_Descendent_Of_Address (Etype (Form1))
+           or else
+             (Present (Form2)
+               and then Is_Descendent_Of_Address (Etype (Form2)));
+      end Operator_Hidden_By;
+
    --  Start of processing for Analyze_One_Call
 
    begin
       Success := False;
 
-      --  If the subprogram has no formals, or if all the formals have
-      --  defaults, and the return type is an array type, the node may
-      --  denote an indexing of the result of a parameterless call.
-      --  In Ada 2005, the subprogram may have one non-defaulted formal,
-      --  and the call may have been written in prefix notation, so that
-      --  the rebuilt parameter list has more than one actual.
+      --  If the subprogram has no formals or if all the formals have defaults,
+      --  and the return type is an array type, the node may denote an indexing
+      --  of the result of a parameterless call. In Ada 2005, the subprogram
+      --  may have one non-defaulted formal, and the call may have been written
+      --  in prefix notation, so that the rebuilt parameter list has more than
+      --  one actual.
 
       if Present (Actuals)
         and then
@@ -2131,13 +2186,7 @@ package body Sem_Ch4 is
 
          if Etype (N) /= Prev_T then
 
-            --  There may be a user-defined operator that hides the
-            --  current interpretation. We must check for this independently
-            --  of the analysis of the call with the user-defined operation,
-            --  because the parameter names may be wrong and yet the hiding
-            --  takes place. Fixes b34014o.
-            --  The abstract operations on address do not hide the predefined
-            --  operator (this is the purpose of making them abstract).
+            --  Check that operator is not hidden by a function interpretation
 
             if Is_Overloaded (Name (N)) then
                declare
@@ -2147,21 +2196,7 @@ package body Sem_Ch4 is
                begin
                   Get_First_Interp (Name (N), I, It);
                   while Present (It.Nam) loop
-                     if Ekind (It.Nam) /= E_Operator
-                        and then not
-                          (Is_Abstract_Subprogram (It.Nam)
-                            and then
-                              Is_Descendent_Of_Address
-                                 (Etype (First_Formal (It.Nam))))
-                        and then Hides_Op (It.Nam, Nam)
-                        and then
-                          Has_Compatible_Type
-                            (First_Actual (N), Etype (First_Formal (It.Nam)))
-                        and then (No (Next_Actual (First_Actual (N)))
-                           or else Has_Compatible_Type
-                            (Next_Actual (First_Actual (N)),
-                             Etype (Next_Formal (First_Formal (It.Nam)))))
-                     then
+                     if Operator_Hidden_By (It.Nam) then
                         Set_Etype (N, Prev_T);
                         return;
                      end if;
@@ -5926,11 +5961,13 @@ package body Sem_Ch4 is
                                              (Alias (Prim_Op)), Corr_Type))
                  or else
 
-               --  Do not consider hidden primitives unless they belong to a
-               --  generic private type with a tagged parent.
+               --  Do not consider hidden primitives unless the type is
+               --  in an open scope or we are within an instance, where
+               --  visibility is known to be correct.
 
                   (Is_Hidden (Prim_Op)
-                     and then not Is_Immediately_Visible (Obj_Type))
+                     and then not Is_Immediately_Visible (Obj_Type)
+                     and then not In_Instance)
                then
                   goto Continue;
                end if;


More information about the Gcc-patches mailing list