home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / gnat-2.06-src.tgz / tar.out / fsf / gnat / ada / sem_aggr.adb < prev    next >
Text File  |  1996-09-28  |  85KB  |  2,296 lines

  1. ------------------------------------------------------------------------------
  2. --                                                                          --
  3. --                         GNAT COMPILER COMPONENTS                         --
  4. --                                                                          --
  5. --                             S E M _ A G G R                              --
  6. --                                                                          --
  7. --                                 B o d y                                  --
  8. --                                                                          --
  9. --                            $Revision: 1.79 $                             --
  10. --                                                                          --
  11. --           Copyright (c) 1992,1993,1994 NYU, All Rights Reserved          --
  12. --                                                                          --
  13. -- GNAT is free software;  you can  redistribute it  and/or modify it under --
  14. -- terms of the  GNU General Public License as published  by the Free Soft- --
  15. -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
  16. -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
  17. -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
  18. -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
  19. -- for  more details.  You should have  received  a copy of the GNU General --
  20. -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
  21. -- to the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. --
  22. --                                                                          --
  23. ------------------------------------------------------------------------------
  24.  
  25. with Atree;    use Atree;
  26. with Einfo;    use Einfo;
  27. with Elists;   use Elists;
  28. with Errout;   use Errout;
  29. with Expander; use Expander;
  30. with Exp_Util; use Exp_Util;
  31. with Features; use Features;
  32. with Freeze;   use Freeze;
  33. with Itypes;   use Itypes;
  34. with Namet;    use Namet;
  35. with Nmake;    use Nmake;
  36. with Nlists;   use Nlists;
  37. with Opt;      use Opt;
  38. with Rtsfind;  use Rtsfind;
  39. with Sem;      use Sem;
  40. with Sem_Ch3;  use Sem_Ch3;
  41. with Sem_Ch5;  use Sem_Ch5;
  42. with Sem_Eval; use Sem_Eval;
  43. with Sem_Res;  use Sem_Res;
  44. with Sem_Util; use Sem_Util;
  45. with Sem_Type; use Sem_Type;
  46. with Sinfo;    use Sinfo;
  47. with Snames;   use Snames;
  48. with Stringt;  use Stringt;
  49. with Stand;    use Stand;
  50. with Tbuild;   use Tbuild;
  51. with Uintp;    use Uintp;
  52.  
  53. with System.Parameters;
  54.  
  55. package body Sem_Aggr is
  56.  
  57.    ------------------------------------------------------
  58.    -- Subprogram Specs for RECORD AGGREGATE Processing --
  59.    ------------------------------------------------------
  60.  
  61.    procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id);
  62.    --  This procedure performs all the semantic checks required for record
  63.    --  aggregates. This procedure assumes that the others choice is by
  64.    --  itself and appears last in the aggregate, if it is present. This
  65.    --  test was previously done is Resolve_Aggregate.
  66.    --
  67.    --    N is the N_Aggregate node.
  68.    --    Typ is the record type for the aggregate resolution
  69.    --
  70.    --  While performing the semantic checks, this procedure
  71.    --  builds a new Component_Association_List where each record field
  72.    --  appears alone in a Component_Choice_List along with its corresponding
  73.    --  expression. The record fields in the Component_Association_List
  74.    --  appear in the same order in which they appear in the record type Typ.
  75.    --
  76.    --  Once this new Component_Association_List is built and all the
  77.    --  semantic checks performed, the original aggregate subtree is replaced
  78.    --  with the new named record aggregate just built. Note that the subtree
  79.    --  substitution is performed with Rewrite_Substitute_Tree so as to be
  80.    --  able to retrieve the original aggregate.
  81.    --
  82.    --  The aggregate subtree manipulation performed by Resolve_Record_Aggregate
  83.    --  yields the aggregate format expected by Gigi. Typically, this kind of
  84.    --  tree manipulations are done in the expander. However, because the
  85.    --  semantic checks that need to be performed on record aggregates really
  86.    --  go hand in hand with the record aggreagate normalization, the aggregate
  87.    --  subtree transformation is performed during resolution rather than
  88.    --  expansion. Had we decided otherwise we would have had to duplicate
  89.    --  most of the code in the expansion procedure Expand_Record_Aggregate.
  90.    --  Note, however, that all the expansion concerning aggegates for tagged
  91.    --  records is done in Expand_Record_Aggregate.
  92.    --
  93.    --  The algorithm of Resolve_Record_Aggregate proceeds as follows:
  94.    --
  95.    --  1. Make sure that the record type against which the record aggregate
  96.    --     has to be resolved is not abstract. Furthermore if the type is
  97.    --     a null aggregate make sure the input aggregate N is also null.
  98.    --
  99.    --  2. Verify that the structure of the aggregate is that of a record
  100.    --     aggregate. Specifically, look for component associations and ensure
  101.    --     that each choice list only has identifiers or the N_Others_Choice
  102.    --     node. Note that at this point Analyze_Aggregate has already made
  103.    --     sure that the N_Others_Choice occurs last and by itself.
  104.    --
  105.    --  3. If Typ contains discriminants, the values for each discriminant
  106.    --     is looked for. If the record type Typ has variants, we check
  107.    --     that the expressions corresponding to each discriminant ruling
  108.    --     the (possibly nested) variant parts of Typ, are static. This
  109.    --     allows us to determine the variant parts to which the rest of
  110.    --     the aggregate must conform. The names of discriminants with their
  111.    --     values are saved in a new association list, New_Assoc_List which
  112.    --     is later augmented with the names and values of the remaining
  113.    --     components in the record type.
  114.    --
  115.    --     During this phase we also make sure that every discriminant is
  116.    --     assigned exactly one value. Note that when several values
  117.    --     for a given discriminant are found, semantic processing continues
  118.    --     looking for further errors. In this case it's the first
  119.    --     discriminant value found which we will be recorded.
  120.    --
  121.    --     IMPORTANT NOTE: For derived tagged types this procedure expects
  122.    --     First_Discriminant and Next_Discriminant to give the correct list
  123.    --     of discriminants, in the correct order.
  124.    --
  125.    --  4. After all the discriminant values have been gathered, we can
  126.    --     set the Etype of the record aggregate. If Typ contains no
  127.    --     discriminants this is straightforward: the Etype of N is just
  128.    --     Typ, otherwise a new implicit constrained subtype of Typ is
  129.    --     built to be the Etype of N.
  130.    --
  131.    --  5. Gather the remaining record components according to the discriminant
  132.    --     values. This involves recursively traversing the record type
  133.    --     structure to see what variants are selected by the given discriminant
  134.    --     values. This processing is a little more convoluted if Typ is a
  135.    --     derived tagged types since we need to retrieve the record structure
  136.    --     of all the ancestors of Typ.
  137.    --
  138.    --  6. After gathering the record components we look for their values
  139.    --     in the record aggregate and emit appropriate error messages
  140.    --     should we not find such values or should they be duplicated.
  141.    --
  142.    --  7. We then make sure no illegal component names appear in the
  143.    --     record aggegate and make sure that the type of the record
  144.    --     components appearing in a same choice list is the same.
  145.    --     Finally we ensure that the others choice, if present, is
  146.    --     used to provide the value of at least a record component.
  147.    --
  148.    --  8. The original aggregate node is replaced with the new named
  149.    --     aggregate built in steps 3 through 6, as explained earlier.
  150.    --
  151.    --  Given the complexity of record aggregate resolution, the primary
  152.    --  goal of this routine is clarity and simplicity rather than execution
  153.    --  and storage efficiency. If there are only positional components in the
  154.    --  aggregate the running time is linear. If there are associations
  155.    --  the running time is still linear as long as the order of the
  156.    --  associations is not too far off the order of the components in the
  157.    --  record type. If this is not the case the running time is at worst
  158.    --  quadratic in the size of the association list.
  159.  
  160.    procedure Gather_Components
  161.      (Comp_List     : Node_Id;
  162.       Governed_By   : List_Id;
  163.       Into          : Elist_Id;
  164.       Report_Errors : out Boolean);
  165.    --  The purpose of this procedure is to gather the valid components
  166.    --  in a record type according to the values of its discriminants.
  167.    --  Specifically:
  168.    --
  169.    --    Comp_List is an N_Component_List node.
  170.    --
  171.    --    Governed_By is a list of N_Component_Association nodes,
  172.    --     where each choice list contains the name of a discriminant and
  173.    --     the expression field gives its value. The values of the
  174.    --     discriminants governing the (possibly nested) variant parts in
  175.    --     Comp_List are found in this Component_Association List.
  176.    --
  177.    --    Into is the list where the valid components are appended.
  178.    --     Note that Into need not be an Empty list. If it's not, components
  179.    --     are attached to its tail.
  180.    --
  181.    --    Report_Errors is set to True if the values of the discriminants
  182.    --     are non-static.
  183.  
  184.    -----------------------------------------------------
  185.    -- Subprogram Specs for ARRAY AGGREGATE Processing --
  186.    -----------------------------------------------------
  187.  
  188.    function Resolve_Array_Aggregate
  189.      (N              : Node_Id;
  190.       Index          : Node_Id;
  191.       Component_Typ  : Entity_Id;
  192.       Others_Allowed : Boolean)
  193.       return Boolean;
  194.    --  This procedure performs the semantic checks for an array aggregate.
  195.    --  True is returned if the aggregate resolution succeeds.
  196.    --  The procedure works by recursively checking each nested aggregate.
  197.    --  Specifically, after checking a sub-aggreate nested at the i-th level
  198.    --  we recursively check all the subaggregates at the i+1-st level (if any).
  199.    --
  200.    --    N is the current N_Aggregate node to be checked.
  201.    --
  202.    --    Index is the index node corresponding to the array sub-aggregate that
  203.    --    we are currently checking (RM 4.3.3 (8)). it is the node giving the
  204.    --    applicable index constraint if any (RM 4.3.3 (10)).
  205.    --    It "is a constraint provided by certain contexts [...] that can be
  206.    --    used to determine the bounds of the array value specified by the
  207.    --    aggregate". If Others_Allowed below is False there is no applicable
  208.    --    index constraint.
  209.    --
  210.    --    Component_Typ is the array component type.
  211.    --
  212.    --    Others_Allowed indicates whether an others choice is allowed
  213.    --    in the context where the top-level aggregate appeared.
  214.    --
  215.    --  The algorithm of Resolve_Array_Aggregate proceeds as follows:
  216.    --
  217.    --  1. Make sure that the others choice, if present, is by itself
  218.    --     and appears last in the sub-aggregate. This test has already been
  219.    --     performed during the analysis phase. If the sub-aggregate format was
  220.    --     found to be incorrect during analysis, the Etype of the sub-aggregate
  221.    --     is set to Any_Type. Also check that we do not have positional and
  222.    --     named components in the array sub-aggregate (unless the named
  223.    --     association is an others choice). Finally if an others choice is
  224.    --     present, make sure it is allowed in the aggregate contex.
  225.    --
  226.    --  2. If the array sub-aggregate contains discrete_choices:
  227.    --
  228.    --     (A) Verify their validity. Specifically verify that:
  229.    --
  230.    --        (a) If a null range is present it must be the only possible
  231.    --            choice in the array aggregate.
  232.    --
  233.    --        (b) Ditto for a non static range.
  234.    --
  235.    --        (c) Ditto for a non static expression.
  236.    --
  237.    --        In addition this step analyzes and resolves each discrete_choice,
  238.    --        making sure that its type is the type of the corresponding Index.
  239.    --        If we are not at the lowest array aggregate level (in the case of
  240.    --        multi-dimensional aggregates) then invoke Resolve_Array_Aggregate
  241.    --        recursively on each component expression. Otherwise resolve
  242.    --        the bottom level component expressions against the expected
  243.    --        component type.
  244.    --
  245.    --     (B) Determine the bounds of the sub-aggregate and lowest and
  246.    --         highest choice values.
  247.    --
  248.    --  3. For positional aggregates:
  249.    --
  250.    --     (A) Loop over the component expressions either recursively invoking
  251.    --         Resolve_Array_Aggregate on each of these for multi-dimensional
  252.    --         array aggregates or resolving the bottom level component
  253.    --         expressions against the expected component type.
  254.    --
  255.    --     (B) Determine the bounds of the positional sub-aggregates.
  256.    --
  257.    --  4. Try to determine statically whether the evaluation of the array
  258.    --     sub-aggregate raises Constraint_Error. If yes emit proper
  259.    --     warnings. The precise checks are the following:
  260.    --
  261.    --     (A) Check that the index range defined by aggregate bounds is
  262.    --         compatible with corresponding index subtype.
  263.    --         We also check against the base type. In fact it could be that
  264.    --         Low/High bounds of the base type are static whereas those of
  265.    --         the index subtype are not. Thus if we can statically catch
  266.    --         a problem with respect to the base type we are guaranteed
  267.    --         that the same problem will arise with the index subtype
  268.    --
  269.    --     (B) If we are dealing with a named aggregate containing an others
  270.    --         choice and at least one discrete choice then make sure the range
  271.    --         specified by the discrete choices does not overflow the
  272.    --         aggregate bounds.
  273.    --
  274.    --     (C) If we are dealing with a positional aggregate with an others
  275.    --         choice make sure the number of positional elements specified
  276.    --         does not overflow the aggregate bounds.
  277.    --
  278.    --     Finally construct an N_Range node giving the sub-aggregate bounds.
  279.    --     Set the Aggregate_Bounds field of the sub-aggregate to be this
  280.    --     N_Range. The routine Array_Aggr_Subtype below uses such N_Ranges
  281.    --     to build the appropriate aggregate subtype. Aggregate_Bounds
  282.    --     information is needed during expansion.
  283.  
  284.    function Array_Aggr_Subtype (N : Node_Id; Typ : Node_Id) return Entity_Id;
  285.    --  This routine returns the type or subtype of an array aggregate.
  286.    --
  287.    --    N is the array aggregate node whose type we return.
  288.    --
  289.    --    Typ is the context type in which N occurs.
  290.    --
  291.    --  This routine creates an implicit array subtype whose bouds are
  292.    --  those defined by the aggregate. When this routine is invoked
  293.    --  Resolve_Array_Aggregate has already processed aggregate N. Thus the
  294.    --  Aggregate_Bounds of each sub-aggregate, is an N_Range node giving the
  295.    --  sub-aggregate bounds. When building the aggegate itype, this function
  296.    --  traverses the array aggregate N collecting such Aggregate_Bounds and
  297.    --  constructs the proper array aggregate itype.
  298.    --
  299.    --  Note that in the case of multidimensional aggregates each inner
  300.    --  sub-aggregate corresponding to a given array dimension, may provide a
  301.    --  different bounds. If it is possible to determine statically that
  302.    --  some sub-aggregates corresponding to the same index do not have the
  303.    --  same bounds, then a warning is emitted. If such check is not possible
  304.    --  statically (because some sub-aggregate bounds are dynamic expressions)
  305.    --  then this job is left to the expander. In all cases the particular
  306.    --  bounds that this function will chose for a given dimension is the first
  307.    --  N_Range node for a sub-aggregate corresponding to that dimension.
  308.  
  309.    function Constraint_Err (N : Node_Id) return Boolean;
  310.    --  Returns True if N is an N_Raise_Constraint_Error node or its
  311.    --  Raises_Constraint_Error flag is set.
  312.  
  313.    function Raises_CE (N : Node_Id; Dim : Pos) return Boolean;
  314.    --  Checks whether the array aggregate N contains a sub-aggreage whose
  315.    --  Raises_Constraint_Error flag is set. Returns True if yes. Dim is the
  316.    --  number of indices of N. It is needed to figure out the structure of N
  317.    --  (ie how many sub-aggregates it has).
  318.  
  319.    procedure Make_String_Into_Aggregate (N : Node_Id);
  320.    --  A string literal can appear in  a context in  which a one dimensional
  321.    --  array of characters is expected. This procedure simply rewrites the
  322.    --  string as an aggregate, prior to resolution.
  323.  
  324.    -----------------------
  325.    -- Resolve_Aggregate --
  326.    -----------------------
  327.  
  328.    procedure Resolve_Aggregate (N : Node_Id; Typ : Entity_Id) is
  329.       Pkind : constant Node_Kind := Nkind (Parent (N));
  330.  
  331.    begin
  332.       --  Make sure that the others choice is by itself and appears
  333.       --  last in the aggregate, if it's present. This test is actually
  334.       --  not performed here. The code for this is in Analyze_Aggregate.
  335.       --  What Analyze_Aggregate does is to set the Etype of the record
  336.       --  aggregate to be Any_Type to signal that there was a problem
  337.       --  with an others choice.
  338.  
  339.       if Etype (N) = Any_Type then
  340.          Set_Etype (N, Any_Composite);
  341.  
  342.       elsif Is_Limited_Type (Typ) then
  343.          Error_Msg_N ("aggregate type cannot be limited", N);
  344.  
  345.       elsif Is_Class_Wide_Type (Typ) then
  346.          Error_Msg_N ("aggregate cannot be of a class-wide type", N);
  347.  
  348.       elsif Is_Record_Type (Typ) then
  349.          Resolve_Record_Aggregate (N, Typ);
  350.  
  351.       elsif Is_Array_Type (Typ) then
  352.          Array_Aggregate : declare
  353.             Aggr_Subtyp   : Entity_Id;
  354.             Aggr_Resolved : Boolean;
  355.  
  356.          begin
  357.             --  In the following we determine whether an others choice is
  358.             --  allowed inside the array aggregate. The test checks the context
  359.             --  in which the array aggregate occurs. If the context does not
  360.             --  permit it, or the aggregate type is unconstrained, an others
  361.             --  choice is not allowed.
  362.             --
  363.             --  Note that there is no node for Explicit_Actual_Parameter.
  364.             --  To test for this context we therefore have to test for node
  365.             --  N_Parameter_Association which itself appears only if there is a
  366.             --  formal parameter. Consequently we also need to test for
  367.             --  N_Procedure_Call_Statement or N_Function_Call.
  368.  
  369.             if Is_Constrained (Typ) and then
  370.               (Pkind = N_Assignment_Statement      or else
  371.                Pkind = N_Parameter_Association     or else
  372.                Pkind = N_Function_Call             or else
  373.                Pkind = N_Procedure_Call_Statement  or else
  374.                Pkind = N_Generic_Association       or else
  375.                Pkind = N_Formal_Object_Declaration or else
  376.                Pkind = N_Return_Statement          or else
  377.                Pkind = N_Object_Declaration        or else
  378.                Pkind = N_Component_Declaration     or else
  379.                Pkind = N_Parameter_Specification   or else
  380.                Pkind = N_Qualified_Expression      or else
  381.                Pkind = N_Aggregate                 or else
  382.                Pkind = N_Component_Association)
  383.             then
  384.                Aggr_Resolved :=
  385.                  Resolve_Array_Aggregate
  386.                    (N,
  387.                     Index          => First_Index (Typ),
  388.                     Component_Typ  => Component_Type (Typ),
  389.                     Others_Allowed => True);
  390.  
  391.             else
  392.                Aggr_Resolved :=
  393.                  Resolve_Array_Aggregate
  394.                    (N,
  395.                     Index          => First_Index (Typ),
  396.                     Component_Typ  => Component_Type (Typ),
  397.                     Others_Allowed => False);
  398.             end if;
  399.  
  400.             if not Aggr_Resolved then
  401.                Aggr_Subtyp := Any_Composite;
  402.  
  403.             else
  404.                Aggr_Subtyp := Array_Aggr_Subtype (N, Typ);
  405.  
  406.                --  If we can determine statically that the evaluation of the
  407.                --  array aggregate raises Constraint_Error, then replace the
  408.                --  aggregate with an N_Raise_Constraint_Error node, but set the
  409.                --  Etype to the Aggr_Subtyp computed above for Gigi.
  410.  
  411.                if Raises_CE (N, Number_Dimensions (Typ)) then
  412.                   Rewrite_Substitute_Tree
  413.                     (N, Make_Raise_Constraint_Error (Sloc (N)));
  414.                   Set_Raises_Constraint_Error (N);
  415.                   Set_Analyzed (N, True);
  416.                end if;
  417.             end if;
  418.  
  419.             Set_Etype (N, Aggr_Subtyp);
  420.          end Array_Aggregate;
  421.  
  422.       else
  423.          Error_Msg_N ("illegal context for aggregate", N);
  424.  
  425.       end if;
  426.    end Resolve_Aggregate;
  427.  
  428.    ---------------------------------
  429.    -- Resolve_Extension_Aggregate --
  430.    ---------------------------------
  431.  
  432.    --  There are two cases to consider:
  433.  
  434.    --  a) If the ancestor part is a type mark, the components needed are
  435.    --  the difference between the components of the expected type and the
  436.    --  components of the given type mark.
  437.  
  438.    --  b) If the ancestor part is an expression, it must be unambiguous,
  439.    --  and once we have its type we can also compute the needed  components
  440.    --  as in the previous case. In both cases, if the ancestor type is not
  441.    --  the immediate ancestor, we have to build this ancestor recursively.
  442.  
  443.    --  In both cases discriminants of the ancestor type do not play a
  444.    --  role in the resolution of the needed components, because inherited
  445.    --  discriminants cannot be used in a type extension. As a result we can
  446.    --  compute independently the list of components of the ancestor type and
  447.    --  of the expected type.
  448.  
  449.    procedure Resolve_Extension_Aggregate (N : Node_Id; Typ : Entity_Id) is
  450.       A        :  constant Node_Id := Ancestor_Part (N);
  451.       A_Type   : Entity_Id;
  452.       I        : Interp_Index;
  453.       It       : Interp;
  454.       Imm_Type : Entity_Id;
  455.  
  456.       function Valid_Ancestor_Type return Boolean;
  457.       --  Verify that the type of the ancestor part is a non-private ancestor
  458.       --  of the expected type.
  459.  
  460.       function Valid_Ancestor_Type return Boolean is
  461.          Imm_Type : Entity_Id := Base_Type (Typ);
  462.       begin
  463.          while Is_Derived_Type (Imm_Type)
  464.            and then Etype (Imm_Type) /= Base_Type (A_Type)
  465.          loop
  466.             Imm_Type := Etype (Base_Type (Imm_Type));
  467.          end loop;
  468.  
  469.          if Etype (Imm_Type) /= Base_Type (A_Type) then
  470.             Error_Msg_NE ("expect ancestor type of &", A, Typ);
  471.             return false;
  472.          else
  473.             return true;
  474.          end if;
  475.       end Valid_Ancestor_Type;
  476.  
  477.    begin
  478.       if not Is_Tagged_Type (Typ) then
  479.          Error_Msg_N ("type of extension aggregate must be tagged", N);
  480.          return;
  481.  
  482.       elsif Is_Limited_Type (Typ) then
  483.          Error_Msg_N ("aggregate type cannot be limited", N);
  484.          return;
  485.  
  486.       elsif Is_Class_Wide_Type (Typ) then
  487.          Error_Msg_N ("aggregate cannot be of a class-wide type", N);
  488.          return;
  489.       end if;
  490.  
  491.       if Is_Entity_Name (A)
  492.         and then Is_Type (Entity (A))
  493.       then
  494.          A_Type   := Entity (A);
  495.          Imm_Type := Base_Type (Typ);
  496.  
  497.          if Valid_Ancestor_Type then
  498.             Resolve_Record_Aggregate (N, Typ);
  499.          end if;
  500.  
  501.       elsif Nkind (A) /= N_Aggregate then
  502.          if Is_Overloaded (A) then
  503.             A_Type := Any_Type;
  504.             Get_First_Interp (A, I, It);
  505.  
  506.             while Present (It.Typ) loop
  507.  
  508.                if Is_Tagged_Type (It.Typ)
  509.                   and then not Is_Limited_Type (It.Typ)
  510.                then
  511.                   if A_Type /= Any_Type then
  512.                      Error_Msg_N ("cannot resolve expression", A);
  513.                      return;
  514.                   else
  515.                      A_Type := It.Typ;
  516.                   end if;
  517.                end if;
  518.  
  519.                Get_Next_Interp (I, It);
  520.             end loop;
  521.  
  522.             if A_Type = Any_Type then
  523.                Error_Msg_N
  524.                  ("ancestor part must be non-limited tagged type", A);
  525.                return;
  526.             end if;
  527.  
  528.          else
  529.             A_Type := Etype (A);
  530.          end if;
  531.  
  532.  
  533.          if Valid_Ancestor_Type then
  534.             Resolve (A, A_Type);
  535.             Resolve_Record_Aggregate (N, Typ);
  536.          end if;
  537.  
  538.       else
  539.          Error_Msg_N (" No unique type for this aggregate",  A);
  540.       end if;
  541.  
  542.    end Resolve_Extension_Aggregate;
  543.  
  544.    ------------------------------
  545.    -- Resolve_Record_Aggregate --
  546.    ------------------------------
  547.  
  548.    procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id) is
  549.       Expr            : Node_Id;
  550.       Record_Def      : Node_Id;
  551.       Positional_Expr : Node_Id;
  552.  
  553.       Assoc : Node_Id;
  554.       --  N_Component_Association node belonging to the input aggregate N
  555.  
  556.       New_Assoc_List  : List_Id := New_List;
  557.       New_Assoc       : Node_Id;
  558.       --  New_Assoc_List is the newly built list of N_Component_Association
  559.       --  nodes. New_Assoc is one such N_Component_Association node in it.
  560.       --  Please note that while Assoc and New_Assoc contain the same
  561.       --  kind of nodes, they are used to iterate over two different
  562.       --  N_Component_Association lists.
  563.  
  564.       New_Aggregate  : Node_Id := New_Copy (N);
  565.       Component      : Entity_Id;
  566.       Component_Elmt : Elmt_Id;
  567.       Components     : Elist_Id := New_Elmt_List;
  568.       --  Components is the list of the record components whose value must
  569.       --  be provided in the aggregate. This list does include discriminants.
  570.  
  571.       Next_Expr : Node_Id;
  572.       Others_Etype : Entity_Id := Empty;
  573.       --  This variable is used to save the Etype of the last record component
  574.       --  that takes its value from the others choice. Its purpose is:
  575.       --
  576.       --    (a) make sure the others choice is useful
  577.       --
  578.       --    (b) make sure the type of all the components whose value is
  579.       --        subsumed by the others choice are the same.
  580.       --
  581.       --  This variable is updated as a side effect of function Get_Value
  582.  
  583.       procedure Add_Association (Component : Entity_Id; Expr : Node_Id);
  584.       --  Builds a new N_Component_Association node which associates
  585.       --  Component to expression Expr and adds it to the new association
  586.       --  list New_Assoc_List being built.
  587.  
  588.       function Get_Value
  589.         (Compon                 : Node_Id;
  590.          From                   : List_Id;
  591.          Consider_Others_Choice : Boolean := False)
  592.          return Node_Id;
  593.       --  Given a record component stored in parameter Compon, the
  594.       --  following function returns its value as it appears in the list
  595.       --  From, which is a list of N_Component_Association nodes. If no
  596.       --  component association has a choice for the searched component,
  597.       --  the value provided by the others choice is returned, if there
  598.       --  is  one and Consider_Others_Choice is set to true. Otherwise
  599.       --  Empty is returned. If there is more than one component association
  600.       --  giving a value for the searched record component, an error message
  601.       --  is emitted and the first found value is returned.
  602.       --
  603.       --  If Consider_Others_Choice is set and the returned expression comes
  604.       --  from the others choice, then Others_Etype is set as a side effect.
  605.       --  An error message is emitted if the components taking their value
  606.       --  from the others choice do not have same type.
  607.  
  608.       function Replace_Discriminants (In_Type : Entity_Id) return Entity_Id;
  609.       --  In_Type is a type or subtype. If In_Type is a record subtype or an
  610.       --  array subtype, a new Itype is created and returned. This Itype is
  611.       --  attached to the aggregate node N. It is a copy of In_Type except
  612.       --  that every occurrence of a record discriminant of the original
  613.       --  record aggregate type Typ is replaced with its corresponding value
  614.       --  as given by the record aggregate.
  615.       --
  616.       --  Note that the Itype is created only if there is at least one such
  617.       --  discriminant in subtype In_Type. Otherwise In_Type is returned.
  618.       --  For example consider the following code:
  619.       --
  620.       --    type rec (D : integer) is record
  621.       --       F : String (1..D);
  622.       --    end record;
  623.       --
  624.       --    X : rec := (D => 3, "abc");
  625.       --
  626.       --  When analyzing the record aggregate, the Etype initially found for
  627.       --  field F is String (1 .. D). However the Etype of "abc" must be
  628.       --  String  (1..3). Replace_Discriminants carries out precisely this
  629.       --  transformation, i.e. in this case
  630.       --
  631.       --    Replace_Discriminants (String (1..D))
  632.       --
  633.       --  returns
  634.       --
  635.       --    String (1..3).
  636.       --
  637.       --  Note that this function begins creating an Itype, before it knows
  638.       --  whether it will be useful or not. If the Itype is not needed, that
  639.       --  storage is wasted.
  640.  
  641.       ---------------------
  642.       -- Add_Association --
  643.       ---------------------
  644.  
  645.       procedure Add_Association (Component : Entity_Id; Expr : Node_Id) is
  646.          New_Assoc   : Node_Id;
  647.          Choice_List : List_Id := New_List;
  648.  
  649.       begin
  650.          Append (New_Occurrence_Of (Component, Sloc (Expr)), Choice_List);
  651.          New_Assoc :=
  652.            Make_Component_Association (Sloc (Expr),
  653.              Choices    => Choice_List,
  654.              Expression => Expr);
  655.          Append (New_Assoc, New_Assoc_List);
  656.       end Add_Association;
  657.  
  658.       ---------------
  659.       -- Get_Value --
  660.       ---------------
  661.  
  662.       function Get_Value
  663.         (Compon                 : Node_Id;
  664.          From                   : List_Id;
  665.          Consider_Others_Choice : Boolean := False)
  666.          return                   Node_Id
  667.       is
  668.          Assoc         : Node_Id;
  669.          Expr          : Node_Id := Empty;
  670.          Selector_Name : Node_Id;
  671.          New_Expr      : Node_Id;
  672.  
  673.       begin
  674.          if Present (From) then
  675.             Assoc := First (From);
  676.          else
  677.             return Empty;
  678.          end if;
  679.  
  680.          while Present (Assoc) loop
  681.             Selector_Name := First (Choices (Assoc));
  682.             while Present (Selector_Name) loop
  683.                if Nkind (Selector_Name) = N_Others_Choice then
  684.                   if Consider_Others_Choice and then No (Expr) then
  685.                      if Present (Others_Etype) and then
  686.                         Base_Type (Others_Etype) /= Base_Type (Etype (Compon))
  687.                      then
  688.                         Error_Msg_N ("components in OTHERS choice must " &
  689.                                      "have same type", Selector_Name);
  690.                      end if;
  691.  
  692.                      Others_Etype := Etype (Compon);
  693.                      New_Expr := New_Copy_Tree (Expression (Assoc));
  694.                      Save_Interps (Expression (Assoc), New_Expr);
  695.                      return New_Expr;
  696.                   end if;
  697.  
  698.                elsif Chars (Compon) = Chars (Selector_Name) then
  699.                   if No (Expr) then
  700.                      Expr := Expression (Assoc);
  701.                   else
  702.                      Error_Msg_NE
  703.                        ("more than one value supplied for &",
  704.                         Selector_Name, Compon);
  705.                   end if;
  706.                end if;
  707.  
  708.                Selector_Name := Next (Selector_Name);
  709.             end loop;
  710.             Assoc := Next (Assoc);
  711.          end loop;
  712.  
  713.          return Expr;
  714.       end Get_Value;
  715.  
  716.       ---------------------------
  717.       -- Replace_Discriminants --
  718.       ---------------------------
  719.  
  720.       function Replace_Discriminants (In_Type : Entity_Id) return Entity_Id is
  721.          New_Expr_List      : Elist_Id;
  722.          Discrim_Constraint : Elmt_Id;
  723.  
  724.          Index_Type     : Entity_Id;
  725.          Old_Index      : Node_Id;
  726.          New_Index      : Node_Id;
  727.          New_Index_List : List_Id;
  728.  
  729.          Old_Expr  : Node_Id;
  730.          New_Expr  : Node_Id;
  731.          Low_Expr  : Node_Id;
  732.          High_Expr : Node_Id;
  733.  
  734.          Itype  : Entity_Id;
  735.          Need_To_Create_Itype : Boolean := False;
  736.  
  737.       begin
  738.          --  In the following code N refers to the input aggregate node and Typ
  739.          --  its type. These are the parameters of Resolve_Record_Aggregate.
  740.  
  741.          if not Has_Discriminants (Typ)
  742.            or else (Ekind (In_Type) /= E_Record_Subtype
  743.                       and then
  744.                     Ekind (In_Type) /= E_Array_Subtype)
  745.          then
  746.             return In_Type;
  747.          end if;
  748.  
  749.          if Ekind (In_Type) = E_Record_Subtype and then
  750.             Has_Discriminants (In_Type)
  751.          then
  752.             New_Expr_List := New_Elmt_List;
  753.  
  754.             Discrim_Constraint :=
  755.               First_Elmt (Discriminant_Constraint (In_Type));
  756.  
  757.             while Present (Discrim_Constraint) loop
  758.                Old_Expr := Node (Discrim_Constraint);
  759.  
  760.                if Nkind (Old_Expr)          = N_Identifier   and then
  761.                   Ekind (Entity (Old_Expr)) = E_Discriminant
  762.                then
  763.                   Need_To_Create_Itype := True;
  764.                   New_Expr := Get_Value (Old_Expr, From => New_Assoc_List);
  765.                   pragma Assert (Present (New_Expr));
  766.                   Append_Elmt (New_Expr, New_Expr_List);
  767.  
  768.                else
  769.                   Append_Elmt (Old_Expr, New_Expr_List);
  770.                end if;
  771.  
  772.                Discrim_Constraint := Next_Elmt (Discrim_Constraint);
  773.             end loop;
  774.  
  775.             if not Need_To_Create_Itype then
  776.                return In_Type;
  777.             end if;
  778.  
  779.             Itype := New_Itype (E_Record_Subtype, N);
  780.  
  781.             Set_Etype                   (Itype, Base_Type (In_Type));
  782.             Set_Esize                   (Itype, Esize (In_Type));
  783.             Set_Discriminant_Constraint (Itype, New_Expr_List);
  784.             Set_Is_Tagged_Type          (Itype, Is_Tagged_Type (In_Type));
  785.             Set_Has_Discriminants       (Itype);
  786.             Set_Is_Constrained          (Itype);
  787.             Set_First_Entity            (Itype, First_Entity (In_Type));
  788.             Set_Last_Entity             (Itype, Last_Entity (In_Type));
  789.             Set_Has_Tasks               (Itype, Has_Tasks (In_Type));
  790.             Set_Depends_On_Private      (Itype, Depends_On_Private (In_Type));
  791.             Set_Has_Controlled          (Itype, Has_Controlled (In_Type));
  792.  
  793.             if Is_Tagged_Type (In_Type) then
  794.                Set_Access_Disp_Table (Itype, Access_Disp_Table (In_Type));
  795.                Set_Is_Controlled (Itype, Is_Controlled (In_Type));
  796.             end if;
  797.  
  798.             return Itype;
  799.  
  800.          elsif Ekind (In_Type) = E_Array_Subtype then
  801.             New_Index_List := New_List;
  802.  
  803.             Old_Index := First_Index (In_Type);
  804.             while Present (Old_Index) loop
  805.                New_Index := New_Copy_Tree (Old_Index);
  806.  
  807.                if Nkind (New_Index) = N_Range then
  808.                   Set_Etype (New_Index, Base_Type (Etype (Old_Index)));
  809.  
  810.                   Get_Index_Bounds (New_Index, Low_Expr, High_Expr);
  811.  
  812.                   Old_Expr := Low_Expr;
  813.                   for J in 1 .. 2 loop
  814.                      if Nkind (Old_Expr)         = N_Identifier   and then
  815.                        Ekind (Entity (Old_Expr)) = E_Discriminant
  816.                      then
  817.                         Need_To_Create_Itype := True;
  818.                         New_Expr :=
  819.                           Get_Value (Old_Expr, From => New_Assoc_List);
  820.                         pragma Assert (Present (New_Expr));
  821.  
  822.                         if J = 1 then
  823.                            Set_Low_Bound
  824.                              (New_Index, New_Copy_Tree (New_Expr));
  825.                         else
  826.                            Set_High_Bound
  827.                              (New_Index, New_Copy_Tree (New_Expr));
  828.                         end if;
  829.                      end if;
  830.  
  831.                      Old_Expr := High_Expr;
  832.                   end loop;
  833.  
  834.                   --  Create anonymous index type for range.
  835.  
  836.                   Index_Type :=
  837.                     New_Itype (Subtype_Kind (Ekind (Etype (New_Index))), N);
  838.  
  839.                   Set_Etype (Index_Type, Etype (New_Index));
  840.  
  841.                   if Is_Character_Type (Etype (New_Index)) then
  842.                      Set_Is_Character_Type (Index_Type, True);
  843.                   end if;
  844.  
  845.                   Set_Esize            (Index_Type, Esize (Etype (New_Index)));
  846.                   Set_Alignment_Clause (Index_Type,
  847.                                         Alignment_Clause (Etype (New_Index)));
  848.                   Set_Scalar_Range     (Index_Type, New_Index);
  849.  
  850.                   Set_Etype (New_Index, Index_Type);
  851.                end if;
  852.  
  853.                Append (New_Index, To => New_Index_List);
  854.                Old_Index := Next_Index (Old_Index);
  855.             end loop;
  856.  
  857.             if not Need_To_Create_Itype then
  858.                return In_Type;
  859.             end if;
  860.  
  861.             Itype := New_Itype (E_Array_Subtype, N);
  862.  
  863.             Set_Is_Constrained (Itype);
  864.             Set_Etype          (Itype, Base_Type (In_Type));
  865.             Set_Esize          (Itype, Esize (In_Type));
  866.             Set_First_Index    (Itype, First (New_Index_List));
  867.             Set_Component_Type (Itype,
  868.                                   Replace_Discriminants
  869.                                     (In_Type => Component_Type (In_Type)));
  870.             Set_Has_Tasks      (Itype, Has_Tasks (In_Type));
  871.             Set_Has_Controlled (Itype, Has_Controlled (In_Type));
  872.             Set_Depends_On_Private (Itype, Depends_On_Private (In_Type));
  873.  
  874.             return Itype;
  875.          end if;
  876.  
  877.          return In_Type;
  878.       end Replace_Discriminants;
  879.  
  880.    --  Start processing for Resolve_Record_Aggregate
  881.  
  882.    begin
  883.  
  884.       --  New Aggregate eventually replaces the aggregate being resolved. It
  885.       --  is initialized here, and attached to the tree explicitly to enforce
  886.       --  the rule that a tree fragment should never be analyzed or resolved
  887.       --  unless it is attached to the current compilation unit.
  888.  
  889.       Set_Component_Associations (New_Aggregate, New_Assoc_List);
  890.       Set_Parent (New_Aggregate, Parent (N));
  891.  
  892.       --  STEP 1: abstract type and null record verification
  893.  
  894.       if Is_Abstract (Typ) then
  895.          Error_Msg_N ("type of aggregate cannot be abstract",  N);
  896.       end if;
  897.  
  898.       if No (First_Entity (Typ)) and then Null_Record_Present (N) then
  899.          Set_Etype (N, Typ);
  900.          return;
  901.  
  902.       elsif Present (First_Entity (Typ))
  903.         and then Null_Record_Present (N)
  904.         and then not Is_Tagged_Type (Typ)
  905.       then
  906.          Error_Msg_N ("record aggregate cannot be null", N);
  907.          return;
  908.  
  909.       elsif No (First_Entity (Typ)) then
  910.          Error_Msg_N ("record aggregate must be null", N);
  911.          return;
  912.       end if;
  913.  
  914.       --  STEP 2: Verify aggregate structure
  915.  
  916.       Step_2 : declare
  917.          Selector_Name : Node_Id;
  918.          Bad_Aggregate : Boolean := False;
  919.  
  920.       begin
  921.          if Present (Component_Associations (N)) then
  922.             Assoc := First (Component_Associations (N));
  923.          else
  924.             Assoc := Empty;
  925.          end if;
  926.  
  927.          while Present (Assoc) loop
  928.             Selector_Name := First (Choices (Assoc));
  929.             while Present (Selector_Name) loop
  930.                if Nkind (Selector_Name) /= N_Identifier and then
  931.                   Nkind (Selector_Name) /= N_Others_Choice
  932.                then
  933.                   Error_Msg_N
  934.                     ("selector name should be identifier or OTHERS",
  935.                      Selector_Name);
  936.                   Bad_Aggregate := True;
  937.                end if;
  938.  
  939.                Selector_Name := Next (Selector_Name);
  940.             end loop;
  941.  
  942.             Assoc := Next (Assoc);
  943.          end loop;
  944.  
  945.          if Bad_Aggregate then
  946.             return;
  947.          end if;
  948.       end Step_2;
  949.  
  950.       --  STEP 3: Find discriminant Values
  951.  
  952.       Step_3 : declare
  953.          Discrim               : Entity_Id;
  954.          Missing_Discriminants : Boolean := False;
  955.  
  956.       begin
  957.          if Present (Expressions (N)) then
  958.             Positional_Expr := First (Expressions (N));
  959.          else
  960.             Positional_Expr := Empty;
  961.          end if;
  962.  
  963.          if Has_Discriminants (Typ) then
  964.             Discrim := First_Discriminant (Typ);
  965.          else
  966.             Discrim := Empty;
  967.          end if;
  968.  
  969.          --  First find the discriminant values in the positional components
  970.  
  971.          while Present (Discrim) and then Present (Positional_Expr) loop
  972.             Next_Expr := Next (Positional_Expr);
  973.             Remove (Positional_Expr);
  974.             Add_Association (Discrim, Positional_Expr);
  975.             Resolve (Positional_Expr, Etype (Discrim));
  976.             Check_Non_Static_Context (Positional_Expr);
  977.  
  978.             if Present (Get_Value (Discrim,
  979.                                    From => Component_Associations (N)))
  980.             then
  981.                Error_Msg_NE
  982.                  ("more than one value supplied for discriminant&",
  983.                   N, Discrim);
  984.             end if;
  985.  
  986.             Positional_Expr := Next_Expr;
  987.             Discrim         := Next_Discriminant (Discrim);
  988.          end loop;
  989.  
  990.          --  Find remaining discriminant values, if any, among named components
  991.  
  992.          while Present (Discrim) loop
  993.             Expr :=
  994.               Get_Value
  995.                 (Discrim,
  996.                  From => Component_Associations (N),
  997.                  Consider_Others_Choice => True);
  998.  
  999.             if No (Expr) then
  1000.                Error_Msg_NE
  1001.                  ("no value supplied for discriminant &", N, Discrim);
  1002.                Missing_Discriminants := True;
  1003.  
  1004.             else
  1005.                Add_Association (Discrim, Expr);
  1006.                Resolve (Expr, Etype (Discrim));
  1007.                Check_Non_Static_Context (Expr);
  1008.             end if;
  1009.  
  1010.             Discrim := Next_Discriminant (Discrim);
  1011.          end loop;
  1012.  
  1013.          if Missing_Discriminants then
  1014.             return;
  1015.          end if;
  1016.  
  1017.          --  At this point and until the beginning of STEP 6, New_Assoc_List
  1018.          --  contains only the discriminants and their values.
  1019.  
  1020.       end Step_3;
  1021.  
  1022.       --  STEP 4: Set the Etype of the record aggregate
  1023.  
  1024.       if Has_Discriminants (Typ) then
  1025.          Build_Constrained_Itype : declare
  1026.             Discrim_Exprs : Elist_Id  := New_Elmt_List;
  1027.             Constr_Itype  : Entity_Id := New_Itype (E_Record_Subtype, N);
  1028.  
  1029.          begin
  1030.             New_Assoc  := First (New_Assoc_List);
  1031.             while Present (New_Assoc) loop
  1032.                Append_Elmt (Expression (New_Assoc), Discrim_Exprs);
  1033.                New_Assoc  := Next (New_Assoc);
  1034.             end loop;
  1035.  
  1036.             Set_Etype                   (Constr_Itype, Base_Type (Typ));
  1037.             Set_Esize                   (Constr_Itype, Esize (Typ));
  1038.             Set_Is_Tagged_Type          (Constr_Itype, Is_Tagged_Type (Typ));
  1039.             Set_Has_Discriminants       (Constr_Itype);
  1040.             Set_Is_Constrained          (Constr_Itype);
  1041.             Set_First_Entity            (Constr_Itype, First_Entity (Typ));
  1042.             Set_Last_Entity             (Constr_Itype, Last_Entity (Typ));
  1043.             Set_Discriminant_Constraint (Constr_Itype, Discrim_Exprs);
  1044.             Set_Has_Tasks               (Constr_Itype, Has_Tasks (Typ));
  1045.             Set_Has_Controlled          (Constr_Itype, Has_Controlled (Typ));
  1046.  
  1047.             if Is_Tagged_Type (Typ) then
  1048.                Set_Access_Disp_Table (Constr_Itype, Access_Disp_Table (Typ));
  1049.                Set_Is_Controlled (Constr_Itype, Is_Controlled (Typ));
  1050.             end if;
  1051.  
  1052.             Set_Etype (N, Constr_Itype);
  1053.          end Build_Constrained_Itype;
  1054.  
  1055.       else
  1056.          Set_Etype (N, Typ);
  1057.       end if;
  1058.  
  1059.       --  STEP 5: Get remaining components according to discriminant values
  1060.  
  1061.       Step_5 : declare
  1062.          Parent_Typ      : Entity_Id;
  1063.          Root_Typ        : Entity_Id;
  1064.          Parent_Typ_List : Elist_Id;
  1065.          Parent_Elmt     : Elmt_Id;
  1066.          Errors_Found    : Boolean := False;
  1067.  
  1068.  
  1069.       begin
  1070.          if Is_Derived_Type (Typ) and then Is_Tagged_Type (Typ) then
  1071.             Parent_Typ_List := New_Elmt_List;
  1072.  
  1073.             --  If this is an extension aggregate, the component list must
  1074.             --  include all components that are not in the given ancestor
  1075.             --  type. Otherwise,  the component list must include components
  1076.             --  of all ancestors.
  1077.  
  1078.             if Nkind (N) = N_Extension_Aggregate then
  1079.                Root_Typ := Base_Type (Etype (Ancestor_Part (N)));
  1080.             else
  1081.                Root_Typ := Root_Type (Typ);
  1082.  
  1083.                if Nkind (Parent (Base_Type (Root_Typ)))
  1084.                     = N_Private_Type_Declaration
  1085.                then
  1086.                   Error_Msg_NE
  1087.                     ("type of aggregate has private ancestor&!",
  1088.                      N, Root_Typ);
  1089.                   Error_Msg_N  ("must use extension aggregate!", N);
  1090.                   return;
  1091.                end if;
  1092.  
  1093.                Record_Def := Type_Definition (Parent (Base_Type (Root_Typ)));
  1094.  
  1095.                Gather_Components
  1096.                  (Component_List (Record_Def),
  1097.                   Governed_By   => New_Assoc_List,
  1098.                   Into          => Components,
  1099.                   Report_Errors => Errors_Found);
  1100.             end if;
  1101.  
  1102.             Parent_Typ  := Base_Type (Typ);
  1103.             while Parent_Typ /= Root_Typ loop
  1104.  
  1105.                Prepend_Elmt (Parent_Typ, To => Parent_Typ_List);
  1106.                Parent_Typ := Etype (Parent_Typ);
  1107.  
  1108.                if Nkind (Parent (Base_Type (Parent_Typ))) =
  1109.                                         N_Private_Type_Declaration
  1110.                  and then Nkind (N) /= N_Extension_Aggregate
  1111.                then
  1112.                   Error_Msg_NE
  1113.                     ("type of aggregate has private ancestor&!",
  1114.                      N, Parent_Typ);
  1115.                   Error_Msg_N  ("must use extension aggregate!", N);
  1116.                   return;
  1117.                end if;
  1118.             end loop;
  1119.  
  1120.             --  Now collect components from all other ancestors.
  1121.  
  1122.             Parent_Elmt := First_Elmt (Parent_Typ_List);
  1123.  
  1124.             while Present (Parent_Elmt) loop
  1125.                Parent_Typ := Node (Parent_Elmt);
  1126.                Record_Def := Type_Definition (Parent (Base_Type (Parent_Typ)));
  1127.                Gather_Components
  1128.                  (Component_List (Record_Extension_Part (Record_Def)),
  1129.                   Governed_By   => New_Assoc_List,
  1130.                   Into          => Components,
  1131.                   Report_Errors => Errors_Found);
  1132.                Parent_Elmt := Next_Elmt (Parent_Elmt);
  1133.             end loop;
  1134.  
  1135.          else
  1136.             Record_Def := Type_Definition (Parent (Base_Type (Typ)));
  1137.  
  1138.             if Null_Present (Record_Def) then
  1139.                null;
  1140.             else
  1141.                Gather_Components
  1142.                  (Component_List (Record_Def),
  1143.                   Governed_By   => New_Assoc_List,
  1144.                   Into          => Components,
  1145.                   Report_Errors => Errors_Found);
  1146.             end if;
  1147.          end if;
  1148.  
  1149.          if Errors_Found then
  1150.             return;
  1151.          end if;
  1152.       end Step_5;
  1153.  
  1154.       --  STEP 6: Find component Values
  1155.  
  1156.       Component_Elmt := First_Elmt (Components);
  1157.  
  1158.       --  First scan the remaining positional associations in the aggregate.
  1159.       --  Remember that at this point Positional_Expr contains the current
  1160.       --  positional association if any is left after looking for discriminant
  1161.       --  values in step 3.
  1162.  
  1163.       while Present (Positional_Expr) and then Present (Component_Elmt) loop
  1164.          Next_Expr := Next (Positional_Expr);
  1165.          Component := Node (Component_Elmt);
  1166.          Remove (Positional_Expr);
  1167.          Add_Association (Component, Positional_Expr);
  1168.          Resolve
  1169.            (Positional_Expr,
  1170.             Replace_Discriminants (In_Type => Etype (Component)));
  1171.          Check_Non_Static_Context (Positional_Expr);
  1172.  
  1173.          if Present
  1174.            (Get_Value (Component, From => Component_Associations (N)))
  1175.          then
  1176.             Error_Msg_NE
  1177.               ("more than one value supplied for Component &", N, Component);
  1178.          end if;
  1179.  
  1180.          Positional_Expr := Next_Expr;
  1181.          Component_Elmt  := Next_Elmt (Component_Elmt);
  1182.       end loop;
  1183.  
  1184.       if Present (Positional_Expr) then
  1185.          Error_Msg_N
  1186.            ("too many components for record aggregate", Positional_Expr);
  1187.       end if;
  1188.  
  1189.       --  Now scan for the named arguments of the aggregate
  1190.  
  1191.       while Present (Component_Elmt) loop
  1192.          Component := Node (Component_Elmt);
  1193.          Expr :=
  1194.            Get_Value (Component,
  1195.                       From => Component_Associations (N),
  1196.                       Consider_Others_Choice => True);
  1197.  
  1198.          if No (Expr) then
  1199.             Error_Msg_NE ("no value supplied for component &", N, Component);
  1200.          else
  1201.             Add_Association (Component, Expr);
  1202.             Resolve
  1203.               (Expr, Replace_Discriminants (In_Type => Etype (Component)));
  1204.             Check_Non_Static_Context (Expr);
  1205.          end if;
  1206.  
  1207.          Component_Elmt := Next_Elmt (Component_Elmt);
  1208.       end loop;
  1209.  
  1210.       --  STEP 7: check for invalid components + check type in choice list
  1211.  
  1212.       Step_7 : declare
  1213.          Selectr : Node_Id;
  1214.          --  Selector name
  1215.  
  1216.          Typech  : Entity_Id;
  1217.          --  Type of first component in choice list
  1218.  
  1219.       begin
  1220.          if Present (Component_Associations (N)) then
  1221.             Assoc := First (Component_Associations (N));
  1222.          else
  1223.             Assoc := Empty;
  1224.          end if;
  1225.  
  1226.          Verification : while Present (Assoc) loop
  1227.             Selectr := First (Choices (Assoc));
  1228.             Typech := Empty;
  1229.  
  1230.             if Nkind (Selectr) = N_Others_Choice then
  1231.                if No (Others_Etype) then
  1232.                   Error_Msg_N
  1233.                     ("OTHERS must represent at least one component", Selectr);
  1234.                end if;
  1235.  
  1236.                exit Verification;
  1237.             end if;
  1238.  
  1239.             while Present (Selectr) loop
  1240.                New_Assoc := First (New_Assoc_List);
  1241.                while Present (New_Assoc) loop
  1242.                   Component := First (Choices (New_Assoc));
  1243.                   exit when Chars (Selectr) = Chars (Component);
  1244.                   New_Assoc := Next (New_Assoc);
  1245.                end loop;
  1246.  
  1247.                --  If no association, this is not a a legal component of
  1248.                --  of the type in question,  except if this is an internal
  1249.                --  component supplied by a previous expansion.
  1250.  
  1251.                if No (New_Assoc) then
  1252.  
  1253.                   if Chars (Selectr) /= Name_uTag
  1254.                     and then Chars (Selectr) /= Name_uParent
  1255.                     and then Chars (Selectr) /= Name_uController
  1256.                   then
  1257.                      Error_Msg_N ("component & is undefined", Selectr);
  1258.                   end if;
  1259.  
  1260.                elsif No (Typech) then
  1261.                   Typech := Base_Type (Etype (Component));
  1262.  
  1263.                elsif Typech /= Base_Type (Etype (Component)) then
  1264.                   Error_Msg_N
  1265.                     ("components in choice list must have same type", Selectr);
  1266.                end if;
  1267.  
  1268.                Selectr := Next (Selectr);
  1269.             end loop;
  1270.  
  1271.             Assoc := Next (Assoc);
  1272.          end loop Verification;
  1273.       end Step_7;
  1274.  
  1275.       --  STEP 8: replace the original aggregate
  1276.  
  1277.       Step_8 : declare
  1278.          New_Aggregate : Node_Id := New_Copy (N);
  1279.  
  1280.       begin
  1281.          Set_Expressions            (New_Aggregate, No_List);
  1282.          Set_Etype                  (New_Aggregate, Etype (N));
  1283.          Set_Component_Associations (New_Aggregate, New_Assoc_List);
  1284.          Rewrite_Substitute_Tree (N, New_Aggregate);
  1285.       end Step_8;
  1286.    end Resolve_Record_Aggregate;
  1287.  
  1288.    -----------------------
  1289.    -- Gather_Components --
  1290.    -----------------------
  1291.  
  1292.    procedure Gather_Components
  1293.      (Comp_List     : Node_Id;
  1294.       Governed_By   : List_Id;
  1295.       Into          : Elist_Id;
  1296.       Report_Errors : out Boolean)
  1297.    is
  1298.       Assoc           : Node_Id;
  1299.       Variant         : Node_Id;
  1300.       Discrete_Choice : Node_Id;
  1301.       Comp_Item       : Node_Id;
  1302.  
  1303.       Discrim         : Entity_Id;
  1304.       Discrim_Name    : Node_Id;
  1305.       Discrim_Value   : Node_Id;
  1306.  
  1307.    begin
  1308.       Report_Errors := False;
  1309.  
  1310.       if No (Comp_List) or else Null_Present (Comp_List) then
  1311.          return;
  1312.  
  1313.       elsif Present (Component_Items (Comp_List)) then
  1314.          Comp_Item := First (Component_Items (Comp_List));
  1315.  
  1316.       else
  1317.          Comp_Item := Empty;
  1318.       end if;
  1319.  
  1320.       while Present (Comp_Item) loop
  1321.  
  1322.          --  Skip the tag of a tagged record, as well as all items
  1323.          --  that are not user components (anonymous types, rep clauses,
  1324.          --  Parent field, controller field).
  1325.  
  1326.          if Nkind (Comp_Item) = N_Component_Declaration
  1327.            and then Chars (Defining_Identifier (Comp_Item)) /= Name_uTag
  1328.            and then Chars (Defining_Identifier (Comp_Item)) /= Name_uParent
  1329.            and then Chars (Defining_Identifier (Comp_Item)) /= Name_uController
  1330.          then
  1331.             Append_Elmt (Defining_Identifier (Comp_Item), Into);
  1332.          end if;
  1333.  
  1334.          Comp_Item := Next (Comp_Item);
  1335.       end loop;
  1336.  
  1337.       if No (Variant_Part (Comp_List)) then
  1338.          return;
  1339.       else
  1340.          Discrim_Name := Name (Variant_Part (Comp_List));
  1341.          Variant      := First (Variants (Variant_Part (Comp_List)));
  1342.       end if;
  1343.  
  1344.       --  Look for the discriminant that governs this variant part.
  1345.       --  The discriminant *must* be in the Governed_By List
  1346.  
  1347.       Assoc := First (Governed_By);
  1348.       loop
  1349.          Discrim := First (Choices (Assoc));
  1350.          exit when Chars (Discrim_Name) = Chars (Discrim);
  1351.          Assoc := Next (Assoc);
  1352.       end loop;
  1353.  
  1354.       Discrim_Value := Expression (Assoc);
  1355.  
  1356.       if not (Is_Static_Expression (Discrim_Value)
  1357.         and then Is_Static_Subtype (Etype (Discrim_Value)))
  1358.       then
  1359.          Error_Msg_NE
  1360.            ("value for discriminant & must be static", Discrim_Value, Discrim);
  1361.          Report_Errors := True;
  1362.          return;
  1363.       end if;
  1364.  
  1365.       Search_For_Discriminant_Value : declare
  1366.          Low  : Node_Id;
  1367.          High : Node_Id;
  1368.  
  1369.          UI_High          : Uint;
  1370.          UI_Low           : Uint;
  1371.          UI_Discrim_Value : constant Uint := Expr_Value (Discrim_Value);
  1372.  
  1373.       begin
  1374.          Find_Discrete_Value : while Present (Variant) loop
  1375.             Discrete_Choice := First (Discrete_Choices (Variant));
  1376.             while Present (Discrete_Choice) loop
  1377.  
  1378.                exit Find_Discrete_Value when
  1379.                  Nkind (Discrete_Choice) = N_Others_Choice;
  1380.  
  1381.                Get_Index_Bounds (Discrete_Choice, Low, High);
  1382.  
  1383.                UI_Low  := Expr_Value (Low);
  1384.                UI_High := Expr_Value (High);
  1385.  
  1386.                exit Find_Discrete_Value when
  1387.                  UI_Low <= UI_Discrim_Value
  1388.                    and then
  1389.                  UI_High >= UI_Discrim_Value;
  1390.  
  1391.                Discrete_Choice := Next (Discrete_Choice);
  1392.             end loop;
  1393.  
  1394.             Variant := Next (Variant);
  1395.          end loop Find_Discrete_Value;
  1396.       end Search_For_Discriminant_Value;
  1397.  
  1398.       if No (Variant) then
  1399.          Error_Msg_NE
  1400.            ("value of discriminant & is out of range", Discrim_Value, Discrim);
  1401.          Report_Errors := True;
  1402.          return;
  1403.       end  if;
  1404.  
  1405.       --  If we have found the corresponding choice, recursively add its
  1406.       --  components to the Into list.
  1407.  
  1408.       Gather_Components
  1409.         (Component_List (Variant), Governed_By, Into, Report_Errors);
  1410.    end Gather_Components;
  1411.  
  1412.    ------------------------
  1413.    -- Array_Aggr_Subtype --
  1414.    ------------------------
  1415.  
  1416.    function Array_Aggr_Subtype
  1417.      (N : Node_Id;
  1418.       Typ : Entity_Id)
  1419.       return Entity_Id
  1420.    is
  1421.       Aggr_Dimension : constant Pos := Number_Dimensions (Typ);
  1422.       --  Number of aggregate index dimensions.
  1423.  
  1424.       Aggr_Range : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
  1425.       --  Constrained N_Range of each index dimension in our aggregate itype.
  1426.  
  1427.       Aggr_Low   : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
  1428.       Aggr_High  : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
  1429.       --  Low and High bounds for each index dimension in our aggregate itype.
  1430.  
  1431.       procedure Collect_Aggr_Bounds (N : Node_Id; Dim : Pos);
  1432.       --  N is an array (sub-)aggregate. Dim is the dimension corresponding to
  1433.       --  (sub-)aggregate N. This procedure collects the constrained N_Range
  1434.       --  nodes corresponding to each index dimension of our aggregate itype.
  1435.       --  These N_Range nodes are collected in Aggr_Range above.
  1436.       --  Likewise collect in Aggr_Low & Aggr_High above the low and high
  1437.       --  bounds of each index dimension. If, when collecting, two bounds
  1438.       --  corresponding to the same dimension are static and found to differ,
  1439.       --  then emit a warning, and mark N as raising Constraint_Error.
  1440.  
  1441.       -------------------------
  1442.       -- Collect_Aggr_Bounds --
  1443.       -------------------------
  1444.  
  1445.       procedure Collect_Aggr_Bounds (N : Node_Id; Dim : Pos) is
  1446.          This_Range : constant Node_Id := Aggregate_Bounds (N);
  1447.          --  The aggregate range node of this specific sub-aggregate.
  1448.  
  1449.          This_Low  : constant Node_Id := Low_Bound (Aggregate_Bounds (N));
  1450.          This_High : constant Node_Id := High_Bound (Aggregate_Bounds (N));
  1451.          --  The aggregate bounds of this specific sub-aggregate.
  1452.  
  1453.          Assoc  : Node_Id;
  1454.          Expr   : Node_Id;
  1455.  
  1456.       begin
  1457.          --  Collect the first N_Range for a given dimension that you find.
  1458.          --  For a given dimension they must be all equal anyway.
  1459.  
  1460.          if No (Aggr_Range (Dim)) then
  1461.             Aggr_Low (Dim)   := This_Low;
  1462.             Aggr_High (Dim)  := This_High;
  1463.             Aggr_Range (Dim) := This_Range;
  1464.  
  1465.          else
  1466.             if Is_OK_Static_Expression (This_Low) then
  1467.                if not Is_OK_Static_Expression (Aggr_Low (Dim)) then
  1468.                   Aggr_Low (Dim)  := This_Low;
  1469.  
  1470.                elsif Expr_Value (This_Low) /= Expr_Value (Aggr_Low (Dim)) then
  1471.                   Set_Raises_Constraint_Error (N);
  1472.                   Error_Msg_N ("Sub-aggregate low bound mismatch?", N);
  1473.                   Error_Msg_N ("Constraint_Error will be raised at run-time?",
  1474.                                N);
  1475.                end if;
  1476.             end if;
  1477.  
  1478.             if Is_OK_Static_Expression (This_High) then
  1479.                if not Is_OK_Static_Expression (Aggr_High (Dim)) then
  1480.                   Aggr_High (Dim)  := This_High;
  1481.  
  1482.                elsif
  1483.                  Expr_Value (This_High) /= Expr_Value (Aggr_High (Dim))
  1484.                then
  1485.                   Set_Raises_Constraint_Error (N);
  1486.                   Error_Msg_N ("Sub-aggregate high bound mismatch?", N);
  1487.                   Error_Msg_N ("Constraint_Error will be raised at run-time?",
  1488.                                N);
  1489.                end if;
  1490.             end if;
  1491.          end if;
  1492.  
  1493.          if Dim < Aggr_Dimension then
  1494.  
  1495.             --  Process positional components
  1496.  
  1497.             if Present (Expressions (N)) then
  1498.                Expr := First (Expressions (N));
  1499.                while Present (Expr) loop
  1500.                   Collect_Aggr_Bounds (Expr, Dim + 1);
  1501.                   Expr := Next (Expr);
  1502.                end loop;
  1503.             end if;
  1504.  
  1505.             --  Process component associations
  1506.  
  1507.             if Present (Component_Associations (N)) then
  1508.                Assoc := First (Component_Associations (N));
  1509.                while Present (Assoc) loop
  1510.                   Expr := Expression (Assoc);
  1511.                   Collect_Aggr_Bounds (Expr, Dim + 1);
  1512.                   Assoc := Next (Assoc);
  1513.                end loop;
  1514.             end if;
  1515.          end if;
  1516.       end Collect_Aggr_Bounds;
  1517.  
  1518.       --  Array_Aggr_Subtype variables
  1519.  
  1520.       Itype : Entity_Id;
  1521.       --  the final itype of the overall aggregate
  1522.  
  1523.       Index_Constraints : List_Id := New_List;
  1524.       --  The list of index constraints of the aggregate itype.
  1525.  
  1526.    --  Begin of Array_Aggr_Subtype
  1527.  
  1528.    begin
  1529.       Collect_Aggr_Bounds (N, 1);
  1530.  
  1531.       --  Build the list of constrained indices of our aggregate itype.
  1532.  
  1533.       for I in 1 .. Aggr_Dimension loop
  1534.          Create_Index : declare
  1535.             Index_Base : Entity_Id := Base_Type (Etype (Aggr_Range (I)));
  1536.             Index_Typ  : Entity_Id;
  1537.  
  1538.          begin
  1539.             --  Construct the Index subtype
  1540.  
  1541.             Index_Typ := New_Itype (Subtype_Kind (Ekind (Index_Base)), N);
  1542.  
  1543.             Set_Etype (Index_Typ, Index_Base);
  1544.  
  1545.             if Is_Character_Type (Index_Base) then
  1546.                Set_Is_Character_Type (Index_Typ, True);
  1547.             end if;
  1548.  
  1549.             Set_Esize            (Index_Typ, Esize (Index_Base));
  1550.             Set_Alignment_Clause (Index_Typ, Alignment_Clause (Index_Base));
  1551.             Set_Scalar_Range     (Index_Typ, Aggr_Range (I));
  1552.  
  1553.             Set_Etype (Aggr_Range (I), Index_Typ);
  1554.  
  1555.             Append (Aggr_Range (I), To => Index_Constraints);
  1556.          end Create_Index;
  1557.       end loop;
  1558.  
  1559.       --  Now build the Itype
  1560.  
  1561.       Itype := New_Itype (E_Array_Subtype, N);
  1562.  
  1563.       Set_Alignment_Clause       (Itype, Alignment_Clause (Typ));
  1564.       Set_Component_Type         (Itype, Component_Type (Typ));
  1565.       Set_Depends_On_Private     (Itype, Has_Private_Component (Typ));
  1566.       Set_Etype                  (Itype, Base_Type (Typ));
  1567.       Set_Esize                  (Itype, Esize (Typ));
  1568.       Set_First_Index            (Itype, First (Index_Constraints));
  1569.       Set_Has_Alignment_Clause   (Itype, Has_Alignment_Clause (Typ));
  1570.       Set_Has_Atomic_Components  (Itype, Has_Atomic_Components (Typ));
  1571.       Set_Has_Controlled         (Itype, Has_Controlled (Typ));
  1572.       Set_Has_Non_Standard_Rep   (Itype, Has_Non_Standard_Rep (Typ));
  1573.       Set_Has_Size_Clause        (Itype, Has_Size_Clause (Typ));
  1574.       Set_Is_Aliased             (Itype, Is_Aliased (Typ));
  1575.       Set_Is_Constrained         (Itype, True);
  1576.       Set_Is_Internal            (Itype, True);
  1577.       Set_Is_Packed              (Itype, Is_Packed (Typ));
  1578.       Set_Suppress_Index_Checks  (Itype, Suppress_Index_Checks (Typ));
  1579.       Set_Suppress_Length_Checks (Itype, Suppress_Length_Checks (Typ));
  1580.  
  1581.       --  We always need a freeze node for a packed array subtype, so that
  1582.       --  we can build the Packed_Array_Type corresponding to the subtype.
  1583.  
  1584.       if Is_Packed (Itype) then
  1585.          Set_Has_Delayed_Freeze (Itype);
  1586.       end if;
  1587.  
  1588.       --  If the subtype is not that of a record component, build a freeze
  1589.       --  node if parent still needs one.
  1590.  
  1591.       if not Is_Type (Scope (Itype)) then
  1592.          Set_Depends_On_Private (Itype, Depends_On_Private (Typ));
  1593.  
  1594.          if Has_Delayed_Freeze (Typ) and then not Is_Frozen (Typ) then
  1595.             Set_Has_Delayed_Freeze (Itype);
  1596.          end if;
  1597.       end if;
  1598.  
  1599.       Freeze_Before (Parent (N), Itype);
  1600.  
  1601.       return Itype;
  1602.    end Array_Aggr_Subtype;
  1603.  
  1604.    --------------------
  1605.    -- Constraint_Err --
  1606.    --------------------
  1607.  
  1608.    function Constraint_Err (N : Node_Id) return Boolean is
  1609.       Kind : Node_Kind := Nkind (N);
  1610.    begin
  1611.       return Kind = N_Raise_Constraint_Error
  1612.         or else (Kind in N_Subexpr and then Raises_Constraint_Error (N));
  1613.    end Constraint_Err;
  1614.  
  1615.    --------------------------------
  1616.    -- Make_String_Into_Aggregate --
  1617.    --------------------------------
  1618.  
  1619.    procedure Make_String_Into_Aggregate (N : Node_Id) is
  1620.       C          : Char_Code;
  1621.       C_Node     : Node_Id;
  1622.       Exprs      : List_Id := New_List;
  1623.       Loc        : constant Source_Ptr := Sloc (N);
  1624.       New_N      : Node_Id;
  1625.       P          : Source_Ptr := Loc + 1;
  1626.       Str        : constant String_Id  := Strval (N);
  1627.       Strlen     : constant Nat        := String_Length (Str);
  1628.  
  1629.    begin
  1630.       for J in  1 .. Strlen loop
  1631.          C := Get_String_Char (Str, J);
  1632.          Set_Character_Literal_Name (C);
  1633.  
  1634.          C_Node :=  Make_Character_Literal (P, Name_Find, C);
  1635.          Set_Etype (C_Node, Any_Character);
  1636.          Set_Analyzed (C_Node);
  1637.          Append_To (Exprs, C_Node);
  1638.  
  1639.          P := P + 1;
  1640.          --  something special for wide strings ?
  1641.       end loop;
  1642.  
  1643.       New_N := Make_Aggregate (Loc, Expressions => Exprs);
  1644.       Set_Analyzed (New_N);
  1645.       Set_Etype (New_N, Any_Composite);
  1646.  
  1647.       Rewrite_Substitute_Tree (N, New_N);
  1648.    end Make_String_Into_Aggregate;
  1649.  
  1650.    ---------------
  1651.    -- Raises_CE --
  1652.    ---------------
  1653.  
  1654.    function Raises_CE (N : Node_Id; Dim : Pos) return Boolean is
  1655.       Assoc  : Node_Id;
  1656.       Expr   : Node_Id;
  1657.  
  1658.    begin
  1659.       if Constraint_Err (N) then
  1660.          return True;
  1661.       end if;
  1662.  
  1663.       if Dim > 1 then
  1664.          --  Process component associations
  1665.  
  1666.          if Present (Component_Associations (N)) then
  1667.             Assoc := First (Component_Associations (N));
  1668.             while Present (Assoc) loop
  1669.                Expr := Expression (Assoc);
  1670.  
  1671.                if Raises_CE (Expr, Dim - 1) then
  1672.                   return True;
  1673.                end if;
  1674.  
  1675.                Assoc := Next (Assoc);
  1676.             end loop;
  1677.          end if;
  1678.  
  1679.          --  Process positional components
  1680.  
  1681.          if Present (Expressions (N)) then
  1682.             Expr := First (Expressions (N));
  1683.  
  1684.             while Present (Expr) loop
  1685.                if Raises_CE (Expr, Dim - 1) then
  1686.                   return True;
  1687.                end if;
  1688.  
  1689.                Expr := Next (Expr);
  1690.             end loop;
  1691.          end if;
  1692.       end if;
  1693.  
  1694.       return False;
  1695.    end Raises_CE;
  1696.  
  1697.    -----------------------------
  1698.    -- Resolve_Array_Aggregate --
  1699.    -----------------------------
  1700.  
  1701.    function Resolve_Array_Aggregate
  1702.      (N              : Node_Id;
  1703.       Index          : Node_Id;
  1704.       Component_Typ  : Entity_Id;
  1705.       Others_Allowed : Boolean)
  1706.       return Boolean
  1707.    is
  1708.       Loc : constant Source_Ptr := Sloc (N);
  1709.  
  1710.       Failure : constant Boolean := False;
  1711.       Success : constant Boolean := True;
  1712.  
  1713.       Index_Typ      : constant Entity_Id := Etype (Index);
  1714.       Index_Typ_Low  : constant Node_Id   := Type_Low_Bound  (Index_Typ);
  1715.       Index_Typ_High : constant Node_Id   := Type_High_Bound (Index_Typ);
  1716.       --  The type of the index corresponding to the array sub-aggregate
  1717.       --  along with its low and upper bounds
  1718.  
  1719.       Index_Base      : constant Entity_Id := Base_Type (Index_Typ);
  1720.       Index_Base_Low  : constant Node_Id   := Type_Low_Bound (Index_Base);
  1721.       Index_Base_High : constant Node_Id   := Type_High_Bound (Index_Base);
  1722.       --  ditto for the base type
  1723.  
  1724.       function Add (Val : Uint; To : Node_Id) return Node_Id;
  1725.       --  Creates a new expression node where Val is added to expression To.
  1726.       --  Tries to constant fold whenever possible. To must be an already
  1727.       --  analyzed expression.
  1728.  
  1729.       procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id);
  1730.       --  Checks that range AL .. AH is compatible with range L .. H. Emits a
  1731.       --  warning if not and sets the Raises_Constraint_Error Flag in N.
  1732.  
  1733.       procedure Check_Length (L, H : Node_Id; Len : Uint);
  1734.       --  Checks that range L .. H contains at least Len elements. Emits a
  1735.       --  warning if not and sets the Raises_Constraint_Error Flag in N.
  1736.  
  1737.       function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean;
  1738.       --  Returns True if range L .. H is dynamic or null.
  1739.  
  1740.       procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean);
  1741.       --  Given expression node From, this routine sets OK to False if it
  1742.       --  cannot statically evaluate From. Otherwise it stores this static
  1743.       --  value into Value.
  1744.  
  1745.       function Index_Typ_First return Node_Id;
  1746.       --  Returns Index_Typ'First.
  1747.  
  1748.       function Resolve_Aggr_Expr (Expr : Node_Id) return Boolean;
  1749.       --  Resolves aggregate expression Expr. Returs False if resolution fails.
  1750.  
  1751.       ---------
  1752.       -- Add --
  1753.       ---------
  1754.  
  1755.       function Add (Val : Uint; To : Node_Id) return Node_Id is
  1756.          Expr_Pos : Node_Id;
  1757.          Expr     : Node_Id;
  1758.          To_Pos   : Node_Id;
  1759.  
  1760.       begin
  1761.          if Constraint_Err (To) then
  1762.             return To;
  1763.          end if;
  1764.  
  1765.          --  First test if we can do constant folding
  1766.  
  1767.          if Is_OK_Static_Expression (To) then
  1768.             Expr_Pos := Make_Integer_Literal (Loc, Expr_Value (To) + Val);
  1769.             Set_Is_Static_Expression (Expr_Pos);
  1770.  
  1771.             if not Is_Enumeration_Type (Index_Typ) then
  1772.                Expr := Expr_Pos;
  1773.  
  1774.             --  If we are dealing with enumeration return
  1775.             --     Index_Typ'Val (Expr_Pos)
  1776.  
  1777.             else
  1778.                Expr :=
  1779.                  Make_Attribute_Reference
  1780.                    (Loc,
  1781.                     Prefix         => New_Reference_To (Index_Typ, Loc),
  1782.                     Attribute_Name => Name_Val,
  1783.                     Expressions    => New_List (Expr_Pos));
  1784.             end if;
  1785.  
  1786.             return Expr;
  1787.          end if;
  1788.  
  1789.          --  If we are here no constant folding possible
  1790.  
  1791.          if not Is_Enumeration_Type (Index_Base) then
  1792.             Expr :=
  1793.               Make_Op_Add (Loc,
  1794.                            Left_Opnd  => Duplicate_Subexpr (To),
  1795.                            Right_Opnd => Make_Integer_Literal (Loc, Val));
  1796.  
  1797.          --  If we are dealing with enumeration return
  1798.          --    Index_Typ'Val (Index_Typ'Pos (To) + Val)
  1799.  
  1800.          else
  1801.             To_Pos :=
  1802.               Make_Attribute_Reference
  1803.                 (Loc,
  1804.                  Prefix         => New_Reference_To (Index_Typ, Loc),
  1805.                  Attribute_Name => Name_Pos,
  1806.                  Expressions    => New_List (Duplicate_Subexpr (To)));
  1807.  
  1808.             Expr_Pos :=
  1809.               Make_Op_Add (Loc,
  1810.                            Left_Opnd  => To_Pos,
  1811.                            Right_Opnd => Make_Integer_Literal (Loc, Val));
  1812.  
  1813.             Expr :=
  1814.               Make_Attribute_Reference
  1815.                 (Loc,
  1816.                  Prefix         => New_Reference_To (Index_Typ, Loc),
  1817.                  Attribute_Name => Name_Val,
  1818.                  Expressions    => New_List (Expr_Pos));
  1819.          end if;
  1820.  
  1821.          return Expr;
  1822.       end Add;
  1823.  
  1824.       ------------------
  1825.       -- Check_Bounds --
  1826.       ------------------
  1827.  
  1828.       procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id) is
  1829.          Val_L  : Uint;
  1830.          Val_H  : Uint;
  1831.          Val_AL : Uint;
  1832.          Val_AH : Uint;
  1833.  
  1834.          OK_L  : Boolean;
  1835.          OK_H  : Boolean;
  1836.          OK_AL : Boolean;
  1837.          OK_AH : Boolean;
  1838.  
  1839.       begin
  1840.          if Constraint_Err (N) or else Dynamic_Or_Null_Range (AL, AH) then
  1841.             return;
  1842.          end if;
  1843.  
  1844.          Get (Value => Val_L, From => L, OK => OK_L);
  1845.          Get (Value => Val_H, From => H, OK => OK_H);
  1846.  
  1847.          Get (Value => Val_AL, From => AL, OK => OK_AL);
  1848.          Get (Value => Val_AH, From => AH, OK => OK_AH);
  1849.  
  1850.          if OK_L and then Val_L > Val_AL then
  1851.             Set_Raises_Constraint_Error (N);
  1852.             Error_Msg_N ("lower bound out of range?", AL);
  1853.             Error_Msg_N ("Constraint_Error will be raised at run-time?", AL);
  1854.          end if;
  1855.  
  1856.          if OK_H and then Val_H < Val_AH then
  1857.             Set_Raises_Constraint_Error (N);
  1858.             Error_Msg_N ("upper bound out of range?", AH);
  1859.             Error_Msg_N ("Constraint_Error will be raised at run-time?", AH);
  1860.          end if;
  1861.       end Check_Bounds;
  1862.  
  1863.       ------------------
  1864.       -- Check_Length --
  1865.       ------------------
  1866.  
  1867.       procedure Check_Length (L, H : Node_Id; Len : Uint) is
  1868.          Val_L  : Uint;
  1869.          Val_H  : Uint;
  1870.  
  1871.          OK_L  : Boolean;
  1872.          OK_H  : Boolean;
  1873.  
  1874.          Range_Len : Uint;
  1875.  
  1876.       begin
  1877.          if Constraint_Err (N) then
  1878.             return;
  1879.          end if;
  1880.  
  1881.          Get (Value => Val_L, From => L, OK => OK_L);
  1882.          Get (Value => Val_H, From => H, OK => OK_H);
  1883.  
  1884.          if not OK_L or else not OK_H then
  1885.             return;
  1886.          end if;
  1887.  
  1888.          --  If null range length is zero
  1889.  
  1890.          if Val_L > Val_H then
  1891.             Range_Len := Uint_0;
  1892.          else
  1893.             Range_Len := Val_H - Val_L + 1;
  1894.          end if;
  1895.  
  1896.          if Range_Len < Len then
  1897.             Set_Raises_Constraint_Error (N);
  1898.             Error_Msg_N ("Too many elements?", N);
  1899.             Error_Msg_N ("Constraint_Error will be raised at run-time?", N);
  1900.          end if;
  1901.       end Check_Length;
  1902.  
  1903.       ---------------------------
  1904.       -- Dynamic_Or_Null_Range --
  1905.       ---------------------------
  1906.  
  1907.       function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean is
  1908.       begin
  1909.          return not Is_OK_Static_Expression (L)
  1910.            or else not Is_OK_Static_Expression (H)
  1911.            or else Expr_Value (L) > Expr_Value (H);
  1912.       end Dynamic_Or_Null_Range;
  1913.  
  1914.       ---------
  1915.       -- Get --
  1916.       ---------
  1917.  
  1918.       procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean) is
  1919.       begin
  1920.          OK := True;
  1921.  
  1922.          if Is_OK_Static_Expression (From) then
  1923.             Value := Expr_Value (From);
  1924.  
  1925.          --  If expression From is something like Some_Type'Val (10) then
  1926.          --  Value = 10
  1927.  
  1928.          elsif Nkind (From) = N_Attribute_Reference
  1929.            and then Attribute_Name (From) = Name_Val
  1930.            and then Is_OK_Static_Expression (First (Expressions (From)))
  1931.          then
  1932.             Value := Expr_Value (First (Expressions (From)));
  1933.  
  1934.          else
  1935.             OK := False;
  1936.  
  1937.          end if;
  1938.       end Get;
  1939.  
  1940.       ---------------------
  1941.       -- Index_Typ_First --
  1942.       ---------------------
  1943.  
  1944.       function Index_Typ_First return Node_Id is
  1945.       begin
  1946.          return
  1947.            Make_Attribute_Reference
  1948.              (Loc,
  1949.               Prefix         => New_Reference_To (Index_Typ, Loc),
  1950.               Attribute_Name => Name_First);
  1951.       end Index_Typ_First;
  1952.  
  1953.       -----------------------
  1954.       -- Resolve_Aggr_Expr --
  1955.       -----------------------
  1956.  
  1957.       function Resolve_Aggr_Expr (Expr : Node_Id) return Boolean is
  1958.          Nxt_Index : Node_Id := Next_Index (Index);
  1959.          --  Index is the current index corresponding to the expresion.
  1960.  
  1961.       begin
  1962.          if Present (Nxt_Index) then
  1963.             if Nkind (Expr) /= N_Aggregate then
  1964.  
  1965.                --  A string literal can appear where a one-dimensional array
  1966.                --  of characters is expected.
  1967.  
  1968.                if Is_Character_Type (Component_Typ)
  1969.                  and then No (Next_Index (Nxt_Index))
  1970.                  and then Nkind (Expr) = N_String_Literal
  1971.                then
  1972.                   Make_String_Into_Aggregate (Expr);
  1973.                else
  1974.                   Error_Msg_N ("nested array aggregate expected", Expr);
  1975.                   return Failure;
  1976.                end if;
  1977.             end if;
  1978.  
  1979.             return Resolve_Array_Aggregate
  1980.               (Expr, Nxt_Index, Component_Typ, Others_Allowed);
  1981.  
  1982.          else
  1983.             Resolve (Expr, Component_Typ);
  1984.             Check_Non_Static_Context (Expr);
  1985.  
  1986.          end if;
  1987.  
  1988.          return Success;
  1989.       end Resolve_Aggr_Expr;
  1990.  
  1991.       --  Variables local to Resolve_Array_Aggregate
  1992.  
  1993.       Assoc     : Node_Id;
  1994.       Choice    : Node_Id;
  1995.       Expr      : Node_Id;
  1996.  
  1997.       Who_Cares : Node_Id;
  1998.  
  1999.       Aggr_Low  : Node_Id := Empty;
  2000.       Aggr_High : Node_Id := Empty;
  2001.       --  The actual low and high bounds of this sub-aggegate
  2002.  
  2003.       Choices_Low  : Node_Id := Empty;
  2004.       Choices_High : Node_Id := Empty;
  2005.       --  The lowest and highest discrete choices values for a named aggregate
  2006.  
  2007.       Nb_Elements : Uint := Uint_0;
  2008.       --  The number of elements in a positional aggegate
  2009.  
  2010.       Others_Present : Boolean := False;
  2011.  
  2012.       Nb_Choices : Nat := 0;
  2013.       --  Contains the overall number of named choices in this sub-aggregate
  2014.  
  2015.       Nb_Discrete_Choices : Nat := 0;
  2016.       --  The overall number of discrete choices (not counting others choice)
  2017.  
  2018.       Case_Table_Size : Nat;
  2019.       --  Contains the size of the case table needed to sort aggregate choices
  2020.  
  2021.    --  Begin of Resolve_Array_Aggregate
  2022.  
  2023.    begin
  2024.       --  STEP 1: make sure the aggregate is correctly formatted
  2025.  
  2026.       if Etype (N) = Any_Type then
  2027.          Set_Etype (N, Any_Composite);
  2028.          return Failure;
  2029.       end if;
  2030.  
  2031.       --  At this point we know that the others choice, if present, is by
  2032.       --  itself and appears last in the aggregate.
  2033.  
  2034.       if Present (Expressions (N))
  2035.         and then Present (Component_Associations (N))
  2036.         and then
  2037.           Nkind (First (Choices (First
  2038.             (Component_Associations (N))))) /= N_Others_Choice
  2039.       then
  2040.          Error_Msg_N ("mixed positional/named associations", N);
  2041.          return Failure;
  2042.       end if;
  2043.  
  2044.       --  Test for the validity of an others choice if present
  2045.  
  2046.       if Present (Component_Associations (N)) then
  2047.          Assoc := Last (Component_Associations (N));
  2048.          Others_Present := (Nkind (First (Choices (Assoc))) = N_Others_Choice);
  2049.       else
  2050.          Others_Present := False;
  2051.       end if;
  2052.  
  2053.       if Others_Present and then (not Others_Allowed) then
  2054.          Error_Msg_N ("OTHERS choice not allowed here",
  2055.                       First (Choices (Assoc)));
  2056.          return Failure;
  2057.       end if;
  2058.  
  2059.       --  STEP 2: Process named components
  2060.  
  2061.       if No (Expressions (N)) then
  2062.  
  2063.          --  Count the overall number of choices so that we can allocate array
  2064.          --  Table below to contain the discrete choices in the aggregate.
  2065.  
  2066.          Assoc := First (Component_Associations (N));
  2067.          while Present (Assoc) loop
  2068.             Choice := First (Choices (Assoc));
  2069.             while Present (Choice) loop
  2070.                Nb_Choices := Nb_Choices + 1;
  2071.                Choice := Next (Choice);
  2072.             end loop;
  2073.  
  2074.             Assoc := Next (Assoc);
  2075.          end loop;
  2076.  
  2077.          if Others_Present then
  2078.             Case_Table_Size := Nb_Choices - 1;
  2079.          else
  2080.             Case_Table_Size := Nb_Choices;
  2081.          end if;
  2082.  
  2083.          Step_2 : declare
  2084.             Low  : Node_Id;
  2085.             High : Node_Id;
  2086.             --  Denote the lowest and highest values in an aggregate choice
  2087.  
  2088.             S_Low  : Node_Id := Empty;
  2089.             S_High : Node_Id := Empty;
  2090.             --  if a choice in an aggregate is a subtype indication these
  2091.             --  denote the lowest and highest values of the subtype
  2092.  
  2093.             Table : Case_Table_Type (1 .. Case_Table_Size);
  2094.             --  Used to sort all the different choice values
  2095.  
  2096.          begin
  2097.             --  STEP 2 (A): Check discrete choices validity.
  2098.  
  2099.             Assoc := First (Component_Associations (N));
  2100.             while Present (Assoc) loop
  2101.  
  2102.                Choice := First (Choices (Assoc));
  2103.                while Present (Choice) loop
  2104.                   Analyze (Choice);
  2105.  
  2106.                   if Nkind (Choice) = N_Others_Choice then
  2107.                      exit;
  2108.  
  2109.                   --  Test for subtype mark without constraint
  2110.  
  2111.                   elsif Is_Entity_Name (Choice) and then
  2112.                     Is_Type (Entity (Choice))
  2113.                   then
  2114.                      if Base_Type (Entity (Choice)) /= Index_Base then
  2115.                         Error_Msg_N
  2116.                           ("invalid subtype mark in aggregate choice", Choice);
  2117.                         return Failure;
  2118.                      end if;
  2119.  
  2120.                   elsif Nkind (Choice) = N_Subtype_Indication then
  2121.                      Resolve_Discrete_Subtype_Indication (Choice, Index_Typ);
  2122.  
  2123.                      --  Does the subtype indication evaluation raise CE ?
  2124.  
  2125.                      Get_Index_Bounds (Subtype_Mark (Choice), S_Low, S_High);
  2126.                      Get_Index_Bounds (Choice, Low, High);
  2127.                      Check_Bounds (S_Low, S_High, Low, High);
  2128.  
  2129.                   else  --  Choice is a range or an expression
  2130.                      Resolve (Choice, Index_Typ);
  2131.                      Check_Non_Static_Context (Choice);
  2132.  
  2133.                   end if;
  2134.  
  2135.                   --  If we could not resolve the discrete choice stop here
  2136.  
  2137.                   if Etype (Choice) = Any_Type then
  2138.                      return Failure;
  2139.                   end if;
  2140.  
  2141.                   Get_Index_Bounds (Choice, Low, High);
  2142.  
  2143.                   if (Dynamic_Or_Null_Range (Low, High)
  2144.                       or else (Nkind (Choice) = N_Subtype_Indication
  2145.                                and then Dynamic_Or_Null_Range (S_Low, S_High)))
  2146.                     and then Nb_Choices /= 1
  2147.                   then
  2148.                      Error_Msg_N ("dynamic or empty choice in aggregate " &
  2149.                                   "must be the only choice", Choice);
  2150.                      return Failure;
  2151.                   end if;
  2152.  
  2153.                   Nb_Discrete_Choices := Nb_Discrete_Choices + 1;
  2154.                   Table (Nb_Discrete_Choices).Choice_Lo := Low;
  2155.                   Table (Nb_Discrete_Choices).Choice_Hi := High;
  2156.  
  2157.                   Choice := Next (Choice);
  2158.                end loop;
  2159.  
  2160.                if not Resolve_Aggr_Expr (Expression (Assoc)) then
  2161.                   return Failure;
  2162.                end if;
  2163.  
  2164.                Assoc := Next (Assoc);
  2165.             end loop;
  2166.  
  2167.             --  If aggregate contains more than one choice then these must be
  2168.             --  static. Sort them and check that they are contiguous
  2169.  
  2170.             if Nb_Discrete_Choices > 1 then
  2171.                Sort_Case_Table (Table);
  2172.  
  2173.                for J in 1 .. Nb_Discrete_Choices - 1 loop
  2174.                   if Expr_Value (Table (J).Choice_Hi) >=
  2175.                     Expr_Value (Table (J + 1).Choice_Lo)
  2176.                   then
  2177.                      Error_Msg_N ("duplicate choice values in array aggregate",
  2178.                                   Table (J).Choice_Hi);
  2179.                      return Failure;
  2180.  
  2181.                   elsif (not Others_Present)
  2182.                     and then
  2183.                     (Expr_Value (Table (J + 1).Choice_Lo) -
  2184.                      Expr_Value (Table (J).Choice_Hi)) > 1
  2185.                   then
  2186.                      Error_Msg_N ("missing association in array aggregate", N);
  2187.                      Set_Etype (N, Any_Composite);
  2188.                      return Failure;
  2189.  
  2190.                   end if;
  2191.                end loop;
  2192.             end if;
  2193.  
  2194.             --  STEP 2 (B): Compute aggregate bounds and min/max choices values
  2195.  
  2196.             if Nb_Discrete_Choices > 0 then
  2197.                Choices_Low  := Table (1).Choice_Lo;
  2198.                Choices_High := Table (Nb_Discrete_Choices).Choice_Hi;
  2199.             end if;
  2200.  
  2201.             if Others_Present then
  2202.                Get_Index_Bounds (Index, Aggr_Low, Aggr_High);
  2203.  
  2204.             else
  2205.                Aggr_Low  := Choices_Low;
  2206.                Aggr_High := Choices_High;
  2207.             end if;
  2208.          end Step_2;
  2209.  
  2210.       --  STEP 3: Process positional components
  2211.  
  2212.       else
  2213.          --  STEP 3 (A): Process positional elements
  2214.  
  2215.          Expr := First (Expressions (N));
  2216.          Nb_Elements := Uint_0;
  2217.          while Present (Expr) loop
  2218.             Nb_Elements := Nb_Elements + 1;
  2219.  
  2220.             if not Resolve_Aggr_Expr (Expr) then
  2221.                return Failure;
  2222.             end if;
  2223.  
  2224.             Expr := Next (Expr);
  2225.          end loop;
  2226.  
  2227.          if Others_Present then
  2228.             Assoc := Last (Component_Associations (N));
  2229.             if not Resolve_Aggr_Expr (Expression (Assoc)) then
  2230.                return Failure;
  2231.             end if;
  2232.          end if;
  2233.  
  2234.          --  STEP 3 (B): Compute the aggregate bounds
  2235.  
  2236.          if Others_Present then
  2237.             Get_Index_Bounds (Index, Aggr_Low, Aggr_High);
  2238.  
  2239.          else
  2240.             if Others_Allowed then
  2241.                Get_Index_Bounds (Index, Aggr_Low, Who_Cares);
  2242.             else
  2243.                Aggr_Low := Index_Typ_Low;
  2244.             end if;
  2245.  
  2246.             Aggr_High := Add (Nb_Elements - 1, To => Aggr_Low);
  2247.          end if;
  2248.       end if;
  2249.  
  2250.       --  STEP 4: Perform static aggregate checks and save the bounds
  2251.  
  2252.       --  Check (A)
  2253.  
  2254.       Check_Bounds (Index_Typ_Low, Index_Typ_High, Aggr_Low, Aggr_High);
  2255.       Check_Bounds (Index_Base_Low, Index_Base_High, Aggr_Low, Aggr_High);
  2256.  
  2257.       --  Check (B)
  2258.  
  2259.       if Others_Present and then Nb_Discrete_Choices > 0 then
  2260.          Check_Bounds (Index_Typ_Low, Index_Typ_High,
  2261.                        Choices_Low, Choices_High);
  2262.          Check_Bounds (Index_Base_Low, Index_Base_High,
  2263.                        Choices_Low, Choices_High);
  2264.  
  2265.       --  Check (C)
  2266.  
  2267.       elsif Others_Present and then Nb_Elements > 0 then
  2268.          Check_Length (Index_Typ_Low, Index_Typ_High, Nb_Elements);
  2269.          Check_Length (Index_Base_Low, Index_Base_High, Nb_Elements);
  2270.  
  2271.       end if;
  2272.  
  2273.       if Constraint_Err (Aggr_Low) or else Constraint_Err (Aggr_High) then
  2274.          Set_Raises_Constraint_Error (N);
  2275.       end if;
  2276.  
  2277.       Aggr_Low := Duplicate_Subexpr (Aggr_Low);
  2278.  
  2279.       --  Do not duplicate Aggr_High if Aggr_High = Aggr_Low + Nb_Elements
  2280.       --  since the addition node returned by Add is not yet analyzed
  2281.  
  2282.       if Others_Present or else Nb_Discrete_Choices > 0 then
  2283.          Aggr_High := Duplicate_Subexpr (Aggr_High);
  2284.       end if;
  2285.  
  2286.       Set_Aggregate_Bounds
  2287.         (N, Make_Range (Loc, Low_Bound => Aggr_Low, High_Bound => Aggr_High));
  2288.  
  2289.       Analyze (Aggregate_Bounds (N));
  2290.       Resolve (Aggregate_Bounds (N), Index_Typ);
  2291.  
  2292.       return Success;
  2293.    end Resolve_Array_Aggregate;
  2294.  
  2295. end Sem_Aggr;
  2296.