OSDN Git Service

* 5ataprop.adb, 5atpopsp.adb, 5ftaprop.adb, 5gmastop.adb,
[pf3gnuchains/gcc-fork.git] / gcc / ada / sem_aggr.adb
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.2 $
10 --                                                                          --
11 --          Copyright (C) 1992-2001 Free Software Foundation, Inc.          --
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,  59 Temple Place - Suite 330,  Boston, --
22 -- MA 02111-1307, USA.                                                      --
23 --                                                                          --
24 -- GNAT was originally developed  by the GNAT team at  New York University. --
25 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
26 --                                                                          --
27 ------------------------------------------------------------------------------
28
29 with Atree;    use Atree;
30 with Checks;   use Checks;
31 with Einfo;    use Einfo;
32 with Elists;   use Elists;
33 with Errout;   use Errout;
34 with Exp_Util; use Exp_Util;
35 with Freeze;   use Freeze;
36 with Itypes;   use Itypes;
37 with Namet;    use Namet;
38 with Nmake;    use Nmake;
39 with Nlists;   use Nlists;
40 with Opt;      use Opt;
41 with Sem;      use Sem;
42 with Sem_Cat;  use Sem_Cat;
43 with Sem_Ch8;  use Sem_Ch8;
44 with Sem_Ch13; use Sem_Ch13;
45 with Sem_Eval; use Sem_Eval;
46 with Sem_Res;  use Sem_Res;
47 with Sem_Util; use Sem_Util;
48 with Sem_Type; use Sem_Type;
49 with Sinfo;    use Sinfo;
50 with Snames;   use Snames;
51 with Stringt;  use Stringt;
52 with Stand;    use Stand;
53 with Tbuild;   use Tbuild;
54 with Uintp;    use Uintp;
55
56 with GNAT.Spelling_Checker; use GNAT.Spelling_Checker;
57
58 package body Sem_Aggr is
59
60    type Case_Bounds is record
61      Choice_Lo   : Node_Id;
62      Choice_Hi   : Node_Id;
63      Choice_Node : Node_Id;
64    end record;
65
66    type Case_Table_Type is array (Nat range <>) of Case_Bounds;
67    --  Table type used by Check_Case_Choices procedure
68
69    -----------------------
70    -- Local Subprograms --
71    -----------------------
72
73    procedure Sort_Case_Table (Case_Table : in out Case_Table_Type);
74    --  Sort the Case Table using the Lower Bound of each Choice as the key.
75    --  A simple insertion sort is used since the number of choices in a case
76    --  statement of variant part will usually be small and probably in near
77    --  sorted order.
78
79    ------------------------------------------------------
80    -- Subprograms used for RECORD AGGREGATE Processing --
81    ------------------------------------------------------
82
83    procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id);
84    --  This procedure performs all the semantic checks required for record
85    --  aggregates. Note that for aggregates analysis and resolution go
86    --  hand in hand. Aggregate analysis has been delayed up to here and
87    --  it is done while resolving the aggregate.
88    --
89    --    N is the N_Aggregate node.
90    --    Typ is the record type for the aggregate resolution
91    --
92    --  While performing the semantic checks, this procedure
93    --  builds a new Component_Association_List where each record field
94    --  appears alone in a Component_Choice_List along with its corresponding
95    --  expression. The record fields in the Component_Association_List
96    --  appear in the same order in which they appear in the record type Typ.
97    --
98    --  Once this new Component_Association_List is built and all the
99    --  semantic checks performed, the original aggregate subtree is replaced
100    --  with the new named record aggregate just built. Note that the subtree
101    --  substitution is performed with Rewrite so as to be
102    --  able to retrieve the original aggregate.
103    --
104    --  The aggregate subtree manipulation performed by Resolve_Record_Aggregate
105    --  yields the aggregate format expected by Gigi. Typically, this kind of
106    --  tree manipulations are done in the expander. However, because the
107    --  semantic checks that need to be performed on record aggregates really
108    --  go hand in hand with the record aggreagate normalization, the aggregate
109    --  subtree transformation is performed during resolution rather than
110    --  expansion. Had we decided otherwise we would have had to duplicate
111    --  most of the code in the expansion procedure Expand_Record_Aggregate.
112    --  Note, however, that all the expansion concerning aggegates for tagged
113    --  records is done in Expand_Record_Aggregate.
114    --
115    --  The algorithm of Resolve_Record_Aggregate proceeds as follows:
116    --
117    --  1. Make sure that the record type against which the record aggregate
118    --     has to be resolved is not abstract. Furthermore if the type is
119    --     a null aggregate make sure the input aggregate N is also null.
120    --
121    --  2. Verify that the structure of the aggregate is that of a record
122    --     aggregate. Specifically, look for component associations and ensure
123    --     that each choice list only has identifiers or the N_Others_Choice
124    --     node. Also make sure that if present, the N_Others_Choice occurs
125    --     last and by itself.
126    --
127    --  3. If Typ contains discriminants, the values for each discriminant
128    --     is looked for. If the record type Typ has variants, we check
129    --     that the expressions corresponding to each discriminant ruling
130    --     the (possibly nested) variant parts of Typ, are static. This
131    --     allows us to determine the variant parts to which the rest of
132    --     the aggregate must conform. The names of discriminants with their
133    --     values are saved in a new association list, New_Assoc_List which
134    --     is later augmented with the names and values of the remaining
135    --     components in the record type.
136    --
137    --     During this phase we also make sure that every discriminant is
138    --     assigned exactly one value. Note that when several values
139    --     for a given discriminant are found, semantic processing continues
140    --     looking for further errors. In this case it's the first
141    --     discriminant value found which we will be recorded.
142    --
143    --     IMPORTANT NOTE: For derived tagged types this procedure expects
144    --     First_Discriminant and Next_Discriminant to give the correct list
145    --     of discriminants, in the correct order.
146    --
147    --  4. After all the discriminant values have been gathered, we can
148    --     set the Etype of the record aggregate. If Typ contains no
149    --     discriminants this is straightforward: the Etype of N is just
150    --     Typ, otherwise a new implicit constrained subtype of Typ is
151    --     built to be the Etype of N.
152    --
153    --  5. Gather the remaining record components according to the discriminant
154    --     values. This involves recursively traversing the record type
155    --     structure to see what variants are selected by the given discriminant
156    --     values. This processing is a little more convoluted if Typ is a
157    --     derived tagged types since we need to retrieve the record structure
158    --     of all the ancestors of Typ.
159    --
160    --  6. After gathering the record components we look for their values
161    --     in the record aggregate and emit appropriate error messages
162    --     should we not find such values or should they be duplicated.
163    --
164    --  7. We then make sure no illegal component names appear in the
165    --     record aggegate and make sure that the type of the record
166    --     components appearing in a same choice list is the same.
167    --     Finally we ensure that the others choice, if present, is
168    --     used to provide the value of at least a record component.
169    --
170    --  8. The original aggregate node is replaced with the new named
171    --     aggregate built in steps 3 through 6, as explained earlier.
172    --
173    --  Given the complexity of record aggregate resolution, the primary
174    --  goal of this routine is clarity and simplicity rather than execution
175    --  and storage efficiency. If there are only positional components in the
176    --  aggregate the running time is linear. If there are associations
177    --  the running time is still linear as long as the order of the
178    --  associations is not too far off the order of the components in the
179    --  record type. If this is not the case the running time is at worst
180    --  quadratic in the size of the association list.
181
182    procedure Check_Misspelled_Component
183      (Elements      : Elist_Id;
184       Component     : Node_Id);
185    --  Give possible misspelling diagnostic if Component is likely to be
186    --  a misspelling of one of the components of the Assoc_List.
187    --  This is called by Resolv_Aggr_Expr after producing
188    --  an invalid component error message.
189
190    procedure Check_Static_Discriminated_Subtype (T : Entity_Id; V : Node_Id);
191    --  An optimization: determine whether a discriminated subtype has a
192    --  static constraint, and contains array components whose length is also
193    --  static, either because they are constrained by the discriminant, or
194    --  because the original component bounds are static.
195
196    -----------------------------------------------------
197    -- Subprograms used for ARRAY AGGREGATE Processing --
198    -----------------------------------------------------
199
200    function Resolve_Array_Aggregate
201      (N              : Node_Id;
202       Index          : Node_Id;
203       Index_Constr   : Node_Id;
204       Component_Typ  : Entity_Id;
205       Others_Allowed : Boolean)
206       return           Boolean;
207    --  This procedure performs the semantic checks for an array aggregate.
208    --  True is returned if the aggregate resolution succeeds.
209    --  The procedure works by recursively checking each nested aggregate.
210    --  Specifically, after checking a sub-aggreate nested at the i-th level
211    --  we recursively check all the subaggregates at the i+1-st level (if any).
212    --  Note that for aggregates analysis and resolution go hand in hand.
213    --  Aggregate analysis has been delayed up to here and it is done while
214    --  resolving the aggregate.
215    --
216    --    N is the current N_Aggregate node to be checked.
217    --
218    --    Index is the index node corresponding to the array sub-aggregate that
219    --    we are currently checking (RM 4.3.3 (8)). Its Etype is the
220    --    corresponding index type (or subtype).
221    --
222    --    Index_Constr is the node giving the applicable index constraint if
223    --    any (RM 4.3.3 (10)). It "is a constraint provided by certain
224    --    contexts [...] that can be used to determine the bounds of the array
225    --    value specified by the aggregate". If Others_Allowed below is False
226    --    there is no applicable index constraint and this node is set to Index.
227    --
228    --    Component_Typ is the array component type.
229    --
230    --    Others_Allowed indicates whether an others choice is allowed
231    --    in the context where the top-level aggregate appeared.
232    --
233    --  The algorithm of Resolve_Array_Aggregate proceeds as follows:
234    --
235    --  1. Make sure that the others choice, if present, is by itself and
236    --     appears last in the sub-aggregate. Check that we do not have
237    --     positional and named components in the array sub-aggregate (unless
238    --     the named association is an others choice). Finally if an others
239    --     choice is present, make sure it is allowed in the aggregate contex.
240    --
241    --  2. If the array sub-aggregate contains discrete_choices:
242    --
243    --     (A) Verify their validity. Specifically verify that:
244    --
245    --        (a) If a null range is present it must be the only possible
246    --            choice in the array aggregate.
247    --
248    --        (b) Ditto for a non static range.
249    --
250    --        (c) Ditto for a non static expression.
251    --
252    --        In addition this step analyzes and resolves each discrete_choice,
253    --        making sure that its type is the type of the corresponding Index.
254    --        If we are not at the lowest array aggregate level (in the case of
255    --        multi-dimensional aggregates) then invoke Resolve_Array_Aggregate
256    --        recursively on each component expression. Otherwise, resolve the
257    --        bottom level component expressions against the expected component
258    --        type ONLY IF the component corresponds to a single discrete choice
259    --        which is not an others choice (to see why read the DELAYED
260    --        COMPONENT RESOLUTION below).
261    --
262    --     (B) Determine the bounds of the sub-aggregate and lowest and
263    --         highest choice values.
264    --
265    --  3. For positional aggregates:
266    --
267    --     (A) Loop over the component expressions either recursively invoking
268    --         Resolve_Array_Aggregate on each of these for multi-dimensional
269    --         array aggregates or resolving the bottom level component
270    --         expressions against the expected component type.
271    --
272    --     (B) Determine the bounds of the positional sub-aggregates.
273    --
274    --  4. Try to determine statically whether the evaluation of the array
275    --     sub-aggregate raises Constraint_Error. If yes emit proper
276    --     warnings. The precise checks are the following:
277    --
278    --     (A) Check that the index range defined by aggregate bounds is
279    --         compatible with corresponding index subtype.
280    --         We also check against the base type. In fact it could be that
281    --         Low/High bounds of the base type are static whereas those of
282    --         the index subtype are not. Thus if we can statically catch
283    --         a problem with respect to the base type we are guaranteed
284    --         that the same problem will arise with the index subtype
285    --
286    --     (B) If we are dealing with a named aggregate containing an others
287    --         choice and at least one discrete choice then make sure the range
288    --         specified by the discrete choices does not overflow the
289    --         aggregate bounds. We also check against the index type and base
290    --         type bounds for the same reasons given in (A).
291    --
292    --     (C) If we are dealing with a positional aggregate with an others
293    --         choice make sure the number of positional elements specified
294    --         does not overflow the aggregate bounds. We also check against
295    --         the index type and base type bounds as mentioned in (A).
296    --
297    --     Finally construct an N_Range node giving the sub-aggregate bounds.
298    --     Set the Aggregate_Bounds field of the sub-aggregate to be this
299    --     N_Range. The routine Array_Aggr_Subtype below uses such N_Ranges
300    --     to build the appropriate aggregate subtype. Aggregate_Bounds
301    --     information is needed during expansion.
302    --
303    --  DELAYED COMPONENT RESOLUTION: The resolution of bottom level component
304    --  expressions in an array aggregate may call Duplicate_Subexpr or some
305    --  other routine that inserts code just outside the outermost aggregate.
306    --  If the array aggregate contains discrete choices or an others choice,
307    --  this may be wrong. Consider for instance the following example.
308    --
309    --    type Rec is record
310    --       V : Integer := 0;
311    --    end record;
312    --
313    --    type Acc_Rec is access Rec;
314    --    Arr : array (1..3) of Acc_Rec := (1 .. 3 => new Rec);
315    --
316    --  Then the transformation of "new Rec" that occurs during resolution
317    --  entails the following code modifications
318    --
319    --    P7b : constant Acc_Rec := new Rec;
320    --    Rec_init_proc (P7b.all);
321    --    Arr : array (1..3) of Acc_Rec := (1 .. 3 => P7b);
322    --
323    --  This code transformation is clearly wrong, since we need to call
324    --  "new Rec" for each of the 3 array elements. To avoid this problem we
325    --  delay resolution of the components of non positional array aggregates
326    --  to the expansion phase. As an optimization, if the discrete choice
327    --  specifies a single value we do not delay resolution.
328
329    function Array_Aggr_Subtype (N : Node_Id; Typ : Node_Id) return Entity_Id;
330    --  This routine returns the type or subtype of an array aggregate.
331    --
332    --    N is the array aggregate node whose type we return.
333    --
334    --    Typ is the context type in which N occurs.
335    --
336    --  This routine creates an implicit array subtype whose bouds are
337    --  those defined by the aggregate. When this routine is invoked
338    --  Resolve_Array_Aggregate has already processed aggregate N. Thus the
339    --  Aggregate_Bounds of each sub-aggregate, is an N_Range node giving the
340    --  sub-aggregate bounds. When building the aggegate itype, this function
341    --  traverses the array aggregate N collecting such Aggregate_Bounds and
342    --  constructs the proper array aggregate itype.
343    --
344    --  Note that in the case of multidimensional aggregates each inner
345    --  sub-aggregate corresponding to a given array dimension, may provide a
346    --  different bounds. If it is possible to determine statically that
347    --  some sub-aggregates corresponding to the same index do not have the
348    --  same bounds, then a warning is emitted. If such check is not possible
349    --  statically (because some sub-aggregate bounds are dynamic expressions)
350    --  then this job is left to the expander. In all cases the particular
351    --  bounds that this function will chose for a given dimension is the first
352    --  N_Range node for a sub-aggregate corresponding to that dimension.
353    --
354    --  Note that the Raises_Constraint_Error flag of an array aggregate
355    --  whose evaluation is determined to raise CE by Resolve_Array_Aggregate,
356    --  is set in Resolve_Array_Aggregate but the aggregate is not
357    --  immediately replaced with a raise CE. In fact, Array_Aggr_Subtype must
358    --  first construct the proper itype for the aggregate (Gigi needs
359    --  this). After constructing the proper itype we will eventually  replace
360    --  the top-level aggregate with a raise CE (done in Resolve_Aggregate).
361    --  Of course in cases such as:
362    --
363    --     type Arr is array (integer range <>) of Integer;
364    --     A : Arr := (positive range -1 .. 2 => 0);
365    --
366    --  The bounds of the aggregate itype are cooked up to look reasonable
367    --  (in this particular case the bounds will be 1 .. 2).
368
369    procedure Aggregate_Constraint_Checks
370      (Exp       : Node_Id;
371       Check_Typ : Entity_Id);
372    --  Checks expression Exp against subtype Check_Typ. If Exp is an
373    --  aggregate and Check_Typ a constrained record type with discriminants,
374    --  we generate the appropriate discriminant checks. If Exp is an array
375    --  aggregate then emit the appropriate length checks. If Exp is a scalar
376    --  type, or a string literal, Exp is changed into Check_Typ'(Exp) to
377    --  ensure that range checks are performed at run time.
378
379    procedure Make_String_Into_Aggregate (N : Node_Id);
380    --  A string literal can appear in  a context in  which a one dimensional
381    --  array of characters is expected. This procedure simply rewrites the
382    --  string as an aggregate, prior to resolution.
383
384    ---------------------------------
385    -- Aggregate_Constraint_Checks --
386    ---------------------------------
387
388    procedure Aggregate_Constraint_Checks
389      (Exp       : Node_Id;
390       Check_Typ : Entity_Id)
391    is
392       Exp_Typ : constant Entity_Id  := Etype (Exp);
393
394    begin
395       if Raises_Constraint_Error (Exp) then
396          return;
397       end if;
398
399       --  This is really expansion activity, so make sure that expansion
400       --  is on and is allowed.
401
402       if not Expander_Active or else In_Default_Expression then
403          return;
404       end if;
405
406       --  First check if we have to insert discriminant checks
407
408       if Has_Discriminants (Exp_Typ) then
409          Apply_Discriminant_Check (Exp, Check_Typ);
410
411       --  Next emit length checks for array aggregates
412
413       elsif Is_Array_Type (Exp_Typ) then
414          Apply_Length_Check (Exp, Check_Typ);
415
416       --  Finally emit scalar and string checks. If we are dealing with a
417       --  scalar literal we need to check by hand because the Etype of
418       --  literals is not necessarily correct.
419
420       elsif Is_Scalar_Type (Exp_Typ)
421         and then Compile_Time_Known_Value (Exp)
422       then
423          if Is_Out_Of_Range (Exp, Base_Type (Check_Typ)) then
424             Apply_Compile_Time_Constraint_Error
425               (Exp, "value not in range of}?",
426                Ent => Base_Type (Check_Typ),
427                Typ => Base_Type (Check_Typ));
428
429          elsif Is_Out_Of_Range (Exp, Check_Typ) then
430             Apply_Compile_Time_Constraint_Error
431               (Exp, "value not in range of}?",
432                Ent => Check_Typ,
433                Typ => Check_Typ);
434
435          elsif not Range_Checks_Suppressed (Check_Typ) then
436             Apply_Scalar_Range_Check (Exp, Check_Typ);
437          end if;
438
439       elsif (Is_Scalar_Type (Exp_Typ)
440              or else Nkind (Exp) = N_String_Literal)
441         and then Exp_Typ /= Check_Typ
442       then
443          if Is_Entity_Name (Exp)
444            and then Ekind (Entity (Exp)) = E_Constant
445          then
446             --  If expression is a constant, it is worthwhile checking whether
447             --  it is a bound of the type.
448
449             if (Is_Entity_Name (Type_Low_Bound (Check_Typ))
450                  and then Entity (Exp) = Entity (Type_Low_Bound (Check_Typ)))
451               or else (Is_Entity_Name (Type_High_Bound (Check_Typ))
452                 and then Entity (Exp) = Entity (Type_High_Bound (Check_Typ)))
453             then
454                return;
455
456             else
457                Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
458                Analyze_And_Resolve (Exp, Check_Typ);
459             end if;
460          else
461             Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
462             Analyze_And_Resolve (Exp, Check_Typ);
463          end if;
464
465       end if;
466    end Aggregate_Constraint_Checks;
467
468    ------------------------
469    -- Array_Aggr_Subtype --
470    ------------------------
471
472    function Array_Aggr_Subtype
473      (N    : Node_Id;
474       Typ  : Entity_Id)
475       return Entity_Id
476    is
477       Aggr_Dimension : constant Pos := Number_Dimensions (Typ);
478       --  Number of aggregate index dimensions.
479
480       Aggr_Range : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
481       --  Constrained N_Range of each index dimension in our aggregate itype.
482
483       Aggr_Low   : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
484       Aggr_High  : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
485       --  Low and High bounds for each index dimension in our aggregate itype.
486
487       Is_Fully_Positional : Boolean := True;
488
489       procedure Collect_Aggr_Bounds (N : Node_Id; Dim : Pos);
490       --  N is an array (sub-)aggregate. Dim is the dimension corresponding to
491       --  (sub-)aggregate N. This procedure collects the constrained N_Range
492       --  nodes corresponding to each index dimension of our aggregate itype.
493       --  These N_Range nodes are collected in Aggr_Range above.
494       --  Likewise collect in Aggr_Low & Aggr_High above the low and high
495       --  bounds of each index dimension. If, when collecting, two bounds
496       --  corresponding to the same dimension are static and found to differ,
497       --  then emit a warning, and mark N as raising Constraint_Error.
498
499       -------------------------
500       -- Collect_Aggr_Bounds --
501       -------------------------
502
503       procedure Collect_Aggr_Bounds (N : Node_Id; Dim : Pos) is
504          This_Range : constant Node_Id := Aggregate_Bounds (N);
505          --  The aggregate range node of this specific sub-aggregate.
506
507          This_Low  : constant Node_Id := Low_Bound (Aggregate_Bounds (N));
508          This_High : constant Node_Id := High_Bound (Aggregate_Bounds (N));
509          --  The aggregate bounds of this specific sub-aggregate.
510
511          Assoc : Node_Id;
512          Expr  : Node_Id;
513
514       begin
515          --  Collect the first N_Range for a given dimension that you find.
516          --  For a given dimension they must be all equal anyway.
517
518          if No (Aggr_Range (Dim)) then
519             Aggr_Low (Dim)   := This_Low;
520             Aggr_High (Dim)  := This_High;
521             Aggr_Range (Dim) := This_Range;
522
523          else
524             if Compile_Time_Known_Value (This_Low) then
525                if not Compile_Time_Known_Value (Aggr_Low (Dim)) then
526                   Aggr_Low (Dim)  := This_Low;
527
528                elsif Expr_Value (This_Low) /= Expr_Value (Aggr_Low (Dim)) then
529                   Set_Raises_Constraint_Error (N);
530                   Error_Msg_N ("Sub-aggregate low bound mismatch?", N);
531                   Error_Msg_N ("Constraint_Error will be raised at run-time?",
532                                N);
533                end if;
534             end if;
535
536             if Compile_Time_Known_Value (This_High) then
537                if not Compile_Time_Known_Value (Aggr_High (Dim)) then
538                   Aggr_High (Dim)  := This_High;
539
540                elsif
541                  Expr_Value (This_High) /= Expr_Value (Aggr_High (Dim))
542                then
543                   Set_Raises_Constraint_Error (N);
544                   Error_Msg_N ("Sub-aggregate high bound mismatch?", N);
545                   Error_Msg_N ("Constraint_Error will be raised at run-time?",
546                                N);
547                end if;
548             end if;
549          end if;
550
551          if Dim < Aggr_Dimension then
552
553             --  Process positional components
554
555             if Present (Expressions (N)) then
556                Expr := First (Expressions (N));
557                while Present (Expr) loop
558                   Collect_Aggr_Bounds (Expr, Dim + 1);
559                   Next (Expr);
560                end loop;
561             end if;
562
563             --  Process component associations
564
565             if Present (Component_Associations (N)) then
566                Is_Fully_Positional := False;
567
568                Assoc := First (Component_Associations (N));
569                while Present (Assoc) loop
570                   Expr := Expression (Assoc);
571                   Collect_Aggr_Bounds (Expr, Dim + 1);
572                   Next (Assoc);
573                end loop;
574             end if;
575          end if;
576       end Collect_Aggr_Bounds;
577
578       --  Array_Aggr_Subtype variables
579
580       Itype : Entity_Id;
581       --  the final itype of the overall aggregate
582
583       Index_Constraints : List_Id := New_List;
584       --  The list of index constraints of the aggregate itype.
585
586    --  Start of processing for Array_Aggr_Subtype
587
588    begin
589       --  Make sure that the list of index constraints is properly attached
590       --  to the tree, and then collect the aggregate bounds.
591
592       Set_Parent (Index_Constraints, N);
593       Collect_Aggr_Bounds (N, 1);
594
595       --  Build the list of constrained indices of our aggregate itype.
596
597       for J in 1 .. Aggr_Dimension loop
598          Create_Index : declare
599             Index_Base : Entity_Id := Base_Type (Etype (Aggr_Range (J)));
600             Index_Typ  : Entity_Id;
601
602          begin
603             --  Construct the Index subtype
604
605             Index_Typ := Create_Itype (Subtype_Kind (Ekind (Index_Base)), N);
606
607             Set_Etype (Index_Typ, Index_Base);
608
609             if Is_Character_Type (Index_Base) then
610                Set_Is_Character_Type (Index_Typ);
611             end if;
612
613             Set_Size_Info      (Index_Typ,                (Index_Base));
614             Set_RM_Size        (Index_Typ, RM_Size        (Index_Base));
615             Set_First_Rep_Item (Index_Typ, First_Rep_Item (Index_Base));
616             Set_Scalar_Range   (Index_Typ, Aggr_Range (J));
617
618             if Is_Discrete_Or_Fixed_Point_Type (Index_Typ) then
619                Set_RM_Size (Index_Typ, UI_From_Int (Minimum_Size (Index_Typ)));
620             end if;
621
622             Set_Etype (Aggr_Range (J), Index_Typ);
623
624             Append (Aggr_Range (J), To => Index_Constraints);
625          end Create_Index;
626       end loop;
627
628       --  Now build the Itype
629
630       Itype := Create_Itype (E_Array_Subtype, N);
631
632       Set_First_Rep_Item         (Itype, First_Rep_Item         (Typ));
633       Set_Component_Type         (Itype, Component_Type         (Typ));
634       Set_Convention             (Itype, Convention             (Typ));
635       Set_Depends_On_Private     (Itype, Has_Private_Component  (Typ));
636       Set_Etype                  (Itype, Base_Type              (Typ));
637       Set_Has_Alignment_Clause   (Itype, Has_Alignment_Clause   (Typ));
638       Set_Is_Aliased             (Itype, Is_Aliased             (Typ));
639       Set_Suppress_Index_Checks  (Itype, Suppress_Index_Checks  (Typ));
640       Set_Suppress_Length_Checks (Itype, Suppress_Length_Checks (Typ));
641       Set_Depends_On_Private     (Itype, Depends_On_Private     (Typ));
642
643       Set_First_Index    (Itype, First (Index_Constraints));
644       Set_Is_Constrained (Itype, True);
645       Set_Is_Internal    (Itype, True);
646       Init_Size_Align    (Itype);
647
648       --  A simple optimization: purely positional aggregates of static
649       --  components should be passed to gigi unexpanded whenever possible,
650       --  and regardless of the staticness of the bounds themselves. Subse-
651       --  quent checks in exp_aggr verify that type is not packed, etc.
652
653       Set_Size_Known_At_Compile_Time (Itype,
654          Is_Fully_Positional
655            and then Comes_From_Source (N)
656            and then Size_Known_At_Compile_Time (Component_Type (Typ)));
657
658       --  We always need a freeze node for a packed array subtype, so that
659       --  we can build the Packed_Array_Type corresponding to the subtype.
660       --  If expansion is disabled, the packed array subtype is not built,
661       --  and we must not generate a freeze node for the type, or else it
662       --  will appear incomplete to gigi.
663
664       if Is_Packed (Itype) and then not In_Default_Expression
665         and then Expander_Active
666       then
667          Freeze_Itype (Itype, N);
668       end if;
669
670       return Itype;
671    end Array_Aggr_Subtype;
672
673    --------------------------------
674    -- Check_Misspelled_Component --
675    --------------------------------
676
677    procedure Check_Misspelled_Component
678      (Elements      : Elist_Id;
679       Component     : Node_Id)
680    is
681       Max_Suggestions   : constant := 2;
682
683       Nr_Of_Suggestions : Natural := 0;
684       Suggestion_1      : Entity_Id := Empty;
685       Suggestion_2      : Entity_Id := Empty;
686       Component_Elmt    : Elmt_Id;
687
688    begin
689       --  All the components of List are matched against Component and
690       --  a count is maintained of possible misspellings. When at the
691       --  end of the analysis there are one or two (not more!) possible
692       --  misspellings, these misspellings will be suggested as
693       --  possible correction.
694
695       Get_Name_String (Chars (Component));
696
697       declare
698          S  : constant String (1 .. Name_Len) :=
699                 Name_Buffer (1 .. Name_Len);
700
701       begin
702
703          Component_Elmt := First_Elmt (Elements);
704
705          while Nr_Of_Suggestions <= Max_Suggestions
706             and then Present (Component_Elmt)
707          loop
708
709             Get_Name_String (Chars (Node (Component_Elmt)));
710
711             if Is_Bad_Spelling_Of (Name_Buffer (1 .. Name_Len), S) then
712                Nr_Of_Suggestions := Nr_Of_Suggestions + 1;
713
714                case Nr_Of_Suggestions is
715                   when 1      => Suggestion_1 := Node (Component_Elmt);
716                   when 2      => Suggestion_2 := Node (Component_Elmt);
717                   when others => exit;
718                end case;
719             end if;
720
721             Next_Elmt (Component_Elmt);
722          end loop;
723
724          --  Report at most two suggestions
725
726          if Nr_Of_Suggestions = 1 then
727             Error_Msg_NE ("\possible misspelling of&",
728                Component, Suggestion_1);
729
730          elsif Nr_Of_Suggestions = 2 then
731             Error_Msg_Node_2 := Suggestion_2;
732             Error_Msg_NE ("\possible misspelling of& or&",
733               Component, Suggestion_1);
734          end if;
735       end;
736    end Check_Misspelled_Component;
737
738    ----------------------------------------
739    -- Check_Static_Discriminated_Subtype --
740    ----------------------------------------
741
742    procedure Check_Static_Discriminated_Subtype (T : Entity_Id; V : Node_Id) is
743       Disc : constant Entity_Id := First_Discriminant (T);
744       Comp : Entity_Id;
745       Ind  : Entity_Id;
746
747    begin
748       if Has_Record_Rep_Clause (Base_Type (T)) then
749          return;
750
751       elsif Present (Next_Discriminant (Disc)) then
752          return;
753
754       elsif Nkind (V) /= N_Integer_Literal then
755          return;
756       end if;
757
758       Comp := First_Component (T);
759
760       while Present (Comp) loop
761
762          if Is_Scalar_Type (Etype (Comp)) then
763             null;
764
765          elsif Is_Private_Type (Etype (Comp))
766            and then Present (Full_View (Etype (Comp)))
767            and then Is_Scalar_Type (Full_View (Etype (Comp)))
768          then
769             null;
770
771          elsif Is_Array_Type (Etype (Comp)) then
772
773             if Is_Bit_Packed_Array (Etype (Comp)) then
774                return;
775             end if;
776
777             Ind := First_Index (Etype (Comp));
778
779             while Present (Ind) loop
780
781                if Nkind (Ind) /= N_Range
782                  or else Nkind (Low_Bound (Ind)) /= N_Integer_Literal
783                  or else Nkind (High_Bound (Ind)) /= N_Integer_Literal
784                then
785                   return;
786                end if;
787
788                Next_Index (Ind);
789             end loop;
790
791          else
792             return;
793          end if;
794
795          Next_Component (Comp);
796       end loop;
797
798       --  On exit, all components have statically known sizes.
799
800       Set_Size_Known_At_Compile_Time (T);
801    end Check_Static_Discriminated_Subtype;
802
803    --------------------------------
804    -- Make_String_Into_Aggregate --
805    --------------------------------
806
807    procedure Make_String_Into_Aggregate (N : Node_Id) is
808       C      : Char_Code;
809       C_Node : Node_Id;
810       Exprs  : List_Id := New_List;
811       Loc    : constant Source_Ptr := Sloc (N);
812       New_N  : Node_Id;
813       P      : Source_Ptr := Loc + 1;
814       Str    : constant String_Id  := Strval (N);
815       Strlen : constant Nat        := String_Length (Str);
816
817    begin
818       for J in  1 .. Strlen loop
819          C := Get_String_Char (Str, J);
820          Set_Character_Literal_Name (C);
821
822          C_Node :=  Make_Character_Literal (P, Name_Find, C);
823          Set_Etype (C_Node, Any_Character);
824          Set_Analyzed (C_Node);
825          Append_To (Exprs, C_Node);
826
827          P := P + 1;
828          --  something special for wide strings ?
829       end loop;
830
831       New_N := Make_Aggregate (Loc, Expressions => Exprs);
832       Set_Analyzed (New_N);
833       Set_Etype (New_N, Any_Composite);
834
835       Rewrite (N, New_N);
836    end Make_String_Into_Aggregate;
837
838    -----------------------
839    -- Resolve_Aggregate --
840    -----------------------
841
842    procedure Resolve_Aggregate (N : Node_Id; Typ : Entity_Id) is
843       Pkind : constant Node_Kind := Nkind (Parent (N));
844
845       Aggr_Subtyp : Entity_Id;
846       --  The actual aggregate subtype. This is not necessarily the same as Typ
847       --  which is the subtype of the context in which the aggregate was found.
848
849    begin
850       if Is_Limited_Type (Typ) then
851          Error_Msg_N ("aggregate type cannot be limited", N);
852
853       elsif Is_Limited_Composite (Typ) then
854          Error_Msg_N ("aggregate type cannot have limited component", N);
855
856       elsif Is_Class_Wide_Type (Typ) then
857          Error_Msg_N ("type of aggregate cannot be class-wide", N);
858
859       elsif Typ = Any_String
860         or else Typ = Any_Composite
861       then
862          Error_Msg_N ("no unique type for aggregate", N);
863          Set_Etype (N, Any_Composite);
864
865       elsif Is_Array_Type (Typ) and then Null_Record_Present (N) then
866          Error_Msg_N ("null record forbidden in array aggregate", N);
867
868       elsif Is_Record_Type (Typ) then
869          Resolve_Record_Aggregate (N, Typ);
870
871       elsif Is_Array_Type (Typ) then
872
873          --  First a special test, for the case of a positional aggregate
874          --  of characters which can be replaced by a string literal.
875          --  Do not perform this transformation if this was a string literal
876          --  to start with, whose components needed constraint checks, or if
877          --  the component type is non-static, because it will require those
878          --  checks and be transformed back into an aggregate.
879
880          if Number_Dimensions (Typ) = 1
881            and then
882              (Root_Type (Component_Type (Typ)) = Standard_Character
883                or else
884               Root_Type (Component_Type (Typ)) = Standard_Wide_Character)
885            and then No (Component_Associations (N))
886            and then not Is_Limited_Composite (Typ)
887            and then not Is_Private_Composite (Typ)
888            and then not Is_Bit_Packed_Array (Typ)
889            and then Nkind (Original_Node (Parent (N))) /= N_String_Literal
890            and then Is_Static_Subtype (Component_Type (Typ))
891          then
892             declare
893                Expr : Node_Id;
894
895             begin
896                Expr := First (Expressions (N));
897                while Present (Expr) loop
898                   exit when Nkind (Expr) /= N_Character_Literal;
899                   Next (Expr);
900                end loop;
901
902                if No (Expr) then
903                   Start_String;
904
905                   Expr := First (Expressions (N));
906                   while Present (Expr) loop
907                      Store_String_Char (Char_Literal_Value (Expr));
908                      Next (Expr);
909                   end loop;
910
911                   Rewrite (N,
912                     Make_String_Literal (Sloc (N), End_String));
913
914                   Analyze_And_Resolve (N, Typ);
915                   return;
916                end if;
917             end;
918          end if;
919
920          --  Here if we have a real aggregate to deal with
921
922          Array_Aggregate : declare
923             Aggr_Resolved : Boolean;
924             Aggr_Typ      : Entity_Id := Etype (Typ);
925             --  This is the unconstrained array type, which is the type
926             --  against which the aggregate is to be resoved. Typ itself
927             --  is the array type of the context which may not be the same
928             --  subtype as the subtype for the final aggregate.
929
930          begin
931             --  In the following we determine whether an others choice is
932             --  allowed inside the array aggregate. The test checks the context
933             --  in which the array aggregate occurs. If the context does not
934             --  permit it, or the aggregate type is unconstrained, an others
935             --  choice is not allowed.
936             --
937             --  Note that there is no node for Explicit_Actual_Parameter.
938             --  To test for this context we therefore have to test for node
939             --  N_Parameter_Association which itself appears only if there is a
940             --  formal parameter. Consequently we also need to test for
941             --  N_Procedure_Call_Statement or N_Function_Call.
942
943             if Is_Constrained (Typ) and then
944               (Pkind = N_Assignment_Statement      or else
945                Pkind = N_Parameter_Association     or else
946                Pkind = N_Function_Call             or else
947                Pkind = N_Procedure_Call_Statement  or else
948                Pkind = N_Generic_Association       or else
949                Pkind = N_Formal_Object_Declaration or else
950                Pkind = N_Return_Statement          or else
951                Pkind = N_Object_Declaration        or else
952                Pkind = N_Component_Declaration     or else
953                Pkind = N_Parameter_Specification   or else
954                Pkind = N_Qualified_Expression      or else
955                Pkind = N_Aggregate                 or else
956                Pkind = N_Extension_Aggregate       or else
957                Pkind = N_Component_Association)
958             then
959                Aggr_Resolved :=
960                  Resolve_Array_Aggregate
961                    (N,
962                     Index          => First_Index (Aggr_Typ),
963                     Index_Constr   => First_Index (Typ),
964                     Component_Typ  => Component_Type (Typ),
965                     Others_Allowed => True);
966
967             else
968                Aggr_Resolved :=
969                  Resolve_Array_Aggregate
970                    (N,
971                     Index          => First_Index (Aggr_Typ),
972                     Index_Constr   => First_Index (Aggr_Typ),
973                     Component_Typ  => Component_Type (Typ),
974                     Others_Allowed => False);
975             end if;
976
977             if not Aggr_Resolved then
978                Aggr_Subtyp := Any_Composite;
979             else
980                Aggr_Subtyp := Array_Aggr_Subtype (N, Typ);
981             end if;
982
983             Set_Etype (N, Aggr_Subtyp);
984          end Array_Aggregate;
985
986       else
987          Error_Msg_N ("illegal context for aggregate", N);
988
989       end if;
990
991       --  If we can determine statically that the evaluation of the
992       --  aggregate raises Constraint_Error, then replace the
993       --  aggregate with an N_Raise_Constraint_Error node, but set the
994       --  Etype to the right aggregate subtype. Gigi needs this.
995
996       if Raises_Constraint_Error (N) then
997          Aggr_Subtyp := Etype (N);
998          Rewrite (N, Make_Raise_Constraint_Error (Sloc (N)));
999          Set_Raises_Constraint_Error (N);
1000          Set_Etype (N, Aggr_Subtyp);
1001          Set_Analyzed (N);
1002       end if;
1003
1004    end Resolve_Aggregate;
1005
1006    -----------------------------
1007    -- Resolve_Array_Aggregate --
1008    -----------------------------
1009
1010    function Resolve_Array_Aggregate
1011      (N              : Node_Id;
1012       Index          : Node_Id;
1013       Index_Constr   : Node_Id;
1014       Component_Typ  : Entity_Id;
1015       Others_Allowed : Boolean)
1016       return           Boolean
1017    is
1018       Loc : constant Source_Ptr := Sloc (N);
1019
1020       Failure : constant Boolean := False;
1021       Success : constant Boolean := True;
1022
1023       Index_Typ      : constant Entity_Id := Etype (Index);
1024       Index_Typ_Low  : constant Node_Id   := Type_Low_Bound  (Index_Typ);
1025       Index_Typ_High : constant Node_Id   := Type_High_Bound (Index_Typ);
1026       --  The type of the index corresponding to the array sub-aggregate
1027       --  along with its low and upper bounds
1028
1029       Index_Base      : constant Entity_Id := Base_Type (Index_Typ);
1030       Index_Base_Low  : constant Node_Id   := Type_Low_Bound (Index_Base);
1031       Index_Base_High : constant Node_Id   := Type_High_Bound (Index_Base);
1032       --  ditto for the base type
1033
1034       function Add (Val : Uint; To : Node_Id) return Node_Id;
1035       --  Creates a new expression node where Val is added to expression To.
1036       --  Tries to constant fold whenever possible. To must be an already
1037       --  analyzed expression.
1038
1039       procedure Check_Bound (BH : Node_Id; AH : in out Node_Id);
1040       --  Checks that AH (the upper bound of an array aggregate) is <= BH
1041       --  (the upper bound of the index base type). If the check fails a
1042       --  warning is emitted, the Raises_Constraint_Error Flag of N is set,
1043       --  and AH is replaced with a duplicate of BH.
1044
1045       procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id);
1046       --  Checks that range AL .. AH is compatible with range L .. H. Emits a
1047       --  warning if not and sets the Raises_Constraint_Error Flag in N.
1048
1049       procedure Check_Length (L, H : Node_Id; Len : Uint);
1050       --  Checks that range L .. H contains at least Len elements. Emits a
1051       --  warning if not and sets the Raises_Constraint_Error Flag in N.
1052
1053       function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean;
1054       --  Returns True if range L .. H is dynamic or null.
1055
1056       procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean);
1057       --  Given expression node From, this routine sets OK to False if it
1058       --  cannot statically evaluate From. Otherwise it stores this static
1059       --  value into Value.
1060
1061       function Resolve_Aggr_Expr
1062         (Expr        : Node_Id;
1063          Single_Elmt : Boolean)
1064          return        Boolean;
1065       --  Resolves aggregate expression Expr. Returs False if resolution
1066       --  fails. If Single_Elmt is set to False, the expression Expr may be
1067       --  used to initialize several array aggregate elements (this can
1068       --  happen for discrete choices such as "L .. H => Expr" or the others
1069       --  choice). In this event we do not resolve Expr unless expansion is
1070       --  disabled. To know why, see the DELAYED COMPONENT RESOLUTION
1071       --  note above.
1072
1073       ---------
1074       -- Add --
1075       ---------
1076
1077       function Add (Val : Uint; To : Node_Id) return Node_Id is
1078          Expr_Pos : Node_Id;
1079          Expr     : Node_Id;
1080          To_Pos   : Node_Id;
1081
1082       begin
1083          if Raises_Constraint_Error (To) then
1084             return To;
1085          end if;
1086
1087          --  First test if we can do constant folding
1088
1089          if Compile_Time_Known_Value (To)
1090            or else Nkind (To) = N_Integer_Literal
1091          then
1092             Expr_Pos := Make_Integer_Literal (Loc, Expr_Value (To) + Val);
1093             Set_Is_Static_Expression (Expr_Pos);
1094             Set_Etype (Expr_Pos, Etype (To));
1095             Set_Analyzed (Expr_Pos, Analyzed (To));
1096
1097             if not Is_Enumeration_Type (Index_Typ) then
1098                Expr := Expr_Pos;
1099
1100             --  If we are dealing with enumeration return
1101             --     Index_Typ'Val (Expr_Pos)
1102
1103             else
1104                Expr :=
1105                  Make_Attribute_Reference
1106                    (Loc,
1107                     Prefix         => New_Reference_To (Index_Typ, Loc),
1108                     Attribute_Name => Name_Val,
1109                     Expressions    => New_List (Expr_Pos));
1110             end if;
1111
1112             return Expr;
1113          end if;
1114
1115          --  If we are here no constant folding possible
1116
1117          if not Is_Enumeration_Type (Index_Base) then
1118             Expr :=
1119               Make_Op_Add (Loc,
1120                            Left_Opnd  => Duplicate_Subexpr (To),
1121                            Right_Opnd => Make_Integer_Literal (Loc, Val));
1122
1123          --  If we are dealing with enumeration return
1124          --    Index_Typ'Val (Index_Typ'Pos (To) + Val)
1125
1126          else
1127             To_Pos :=
1128               Make_Attribute_Reference
1129                 (Loc,
1130                  Prefix         => New_Reference_To (Index_Typ, Loc),
1131                  Attribute_Name => Name_Pos,
1132                  Expressions    => New_List (Duplicate_Subexpr (To)));
1133
1134             Expr_Pos :=
1135               Make_Op_Add (Loc,
1136                            Left_Opnd  => To_Pos,
1137                            Right_Opnd => Make_Integer_Literal (Loc, Val));
1138
1139             Expr :=
1140               Make_Attribute_Reference
1141                 (Loc,
1142                  Prefix         => New_Reference_To (Index_Typ, Loc),
1143                  Attribute_Name => Name_Val,
1144                  Expressions    => New_List (Expr_Pos));
1145          end if;
1146
1147          return Expr;
1148       end Add;
1149
1150       -----------------
1151       -- Check_Bound --
1152       -----------------
1153
1154       procedure Check_Bound (BH : Node_Id; AH : in out Node_Id) is
1155          Val_BH : Uint;
1156          Val_AH : Uint;
1157
1158          OK_BH : Boolean;
1159          OK_AH : Boolean;
1160
1161       begin
1162          Get (Value => Val_BH, From => BH, OK => OK_BH);
1163          Get (Value => Val_AH, From => AH, OK => OK_AH);
1164
1165          if OK_BH and then OK_AH and then Val_BH < Val_AH then
1166             Set_Raises_Constraint_Error (N);
1167             Error_Msg_N ("upper bound out of range?", AH);
1168             Error_Msg_N ("Constraint_Error will be raised at run-time?", AH);
1169
1170             --  You need to set AH to BH or else in the case of enumerations
1171             --  indices we will not be able to resolve the aggregate bounds.
1172
1173             AH := Duplicate_Subexpr (BH);
1174          end if;
1175       end Check_Bound;
1176
1177       ------------------
1178       -- Check_Bounds --
1179       ------------------
1180
1181       procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id) is
1182          Val_L  : Uint;
1183          Val_H  : Uint;
1184          Val_AL : Uint;
1185          Val_AH : Uint;
1186
1187          OK_L  : Boolean;
1188          OK_H  : Boolean;
1189          OK_AL : Boolean;
1190          OK_AH : Boolean;
1191
1192       begin
1193          if Raises_Constraint_Error (N)
1194            or else Dynamic_Or_Null_Range (AL, AH)
1195          then
1196             return;
1197          end if;
1198
1199          Get (Value => Val_L, From => L, OK => OK_L);
1200          Get (Value => Val_H, From => H, OK => OK_H);
1201
1202          Get (Value => Val_AL, From => AL, OK => OK_AL);
1203          Get (Value => Val_AH, From => AH, OK => OK_AH);
1204
1205          if OK_L and then Val_L > Val_AL then
1206             Set_Raises_Constraint_Error (N);
1207             Error_Msg_N ("lower bound of aggregate out of range?", N);
1208             Error_Msg_N ("Constraint_Error will be raised at run-time?", N);
1209          end if;
1210
1211          if OK_H and then Val_H < Val_AH then
1212             Set_Raises_Constraint_Error (N);
1213             Error_Msg_N ("upper bound of aggregate out of range?", N);
1214             Error_Msg_N ("Constraint_Error will be raised at run-time?", N);
1215          end if;
1216       end Check_Bounds;
1217
1218       ------------------
1219       -- Check_Length --
1220       ------------------
1221
1222       procedure Check_Length (L, H : Node_Id; Len : Uint) is
1223          Val_L  : Uint;
1224          Val_H  : Uint;
1225
1226          OK_L  : Boolean;
1227          OK_H  : Boolean;
1228
1229          Range_Len : Uint;
1230
1231       begin
1232          if Raises_Constraint_Error (N) then
1233             return;
1234          end if;
1235
1236          Get (Value => Val_L, From => L, OK => OK_L);
1237          Get (Value => Val_H, From => H, OK => OK_H);
1238
1239          if not OK_L or else not OK_H then
1240             return;
1241          end if;
1242
1243          --  If null range length is zero
1244
1245          if Val_L > Val_H then
1246             Range_Len := Uint_0;
1247          else
1248             Range_Len := Val_H - Val_L + 1;
1249          end if;
1250
1251          if Range_Len < Len then
1252             Set_Raises_Constraint_Error (N);
1253             Error_Msg_N ("Too many elements?", N);
1254             Error_Msg_N ("Constraint_Error will be raised at run-time?", N);
1255          end if;
1256       end Check_Length;
1257
1258       ---------------------------
1259       -- Dynamic_Or_Null_Range --
1260       ---------------------------
1261
1262       function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean is
1263          Val_L : Uint;
1264          Val_H : Uint;
1265
1266          OK_L  : Boolean;
1267          OK_H  : Boolean;
1268
1269       begin
1270          Get (Value => Val_L, From => L, OK => OK_L);
1271          Get (Value => Val_H, From => H, OK => OK_H);
1272
1273          return not OK_L or else not OK_H
1274            or else not Is_OK_Static_Expression (L)
1275            or else not Is_OK_Static_Expression (H)
1276            or else Val_L > Val_H;
1277       end Dynamic_Or_Null_Range;
1278
1279       ---------
1280       -- Get --
1281       ---------
1282
1283       procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean) is
1284       begin
1285          OK := True;
1286
1287          if Compile_Time_Known_Value (From) then
1288             Value := Expr_Value (From);
1289
1290          --  If expression From is something like Some_Type'Val (10) then
1291          --  Value = 10
1292
1293          elsif Nkind (From) = N_Attribute_Reference
1294            and then Attribute_Name (From) = Name_Val
1295            and then Compile_Time_Known_Value (First (Expressions (From)))
1296          then
1297             Value := Expr_Value (First (Expressions (From)));
1298
1299          else
1300             Value := Uint_0;
1301             OK := False;
1302          end if;
1303       end Get;
1304
1305       -----------------------
1306       -- Resolve_Aggr_Expr --
1307       -----------------------
1308
1309       function Resolve_Aggr_Expr
1310         (Expr        : Node_Id;
1311          Single_Elmt : Boolean)
1312          return        Boolean
1313       is
1314          Nxt_Ind        : Node_Id := Next_Index (Index);
1315          Nxt_Ind_Constr : Node_Id := Next_Index (Index_Constr);
1316          --  Index is the current index corresponding to the expression.
1317
1318          Resolution_OK : Boolean := True;
1319          --  Set to False if resolution of the expression failed.
1320
1321       begin
1322          --  If the array type against which we are resolving the aggregate
1323          --  has several dimensions, the expressions nested inside the
1324          --  aggregate must be further aggregates (or strings).
1325
1326          if Present (Nxt_Ind) then
1327             if Nkind (Expr) /= N_Aggregate then
1328
1329                --  A string literal can appear where a one-dimensional array
1330                --  of characters is expected. If the literal looks like an
1331                --  operator, it is still an operator symbol, which will be
1332                --  transformed into a string when analyzed.
1333
1334                if Is_Character_Type (Component_Typ)
1335                  and then No (Next_Index (Nxt_Ind))
1336                  and then (Nkind (Expr) = N_String_Literal
1337                             or else Nkind (Expr) = N_Operator_Symbol)
1338                then
1339                   --  A string literal used in a multidimensional array
1340                   --  aggregate in place of the final one-dimensional
1341                   --  aggregate must not be enclosed in parentheses.
1342
1343                   if Paren_Count (Expr) /= 0 then
1344                      Error_Msg_N ("No parenthesis allowed here", Expr);
1345                   end if;
1346
1347                   Make_String_Into_Aggregate (Expr);
1348
1349                else
1350                   Error_Msg_N ("nested array aggregate expected", Expr);
1351                   return Failure;
1352                end if;
1353             end if;
1354
1355             Resolution_OK := Resolve_Array_Aggregate
1356               (Expr, Nxt_Ind, Nxt_Ind_Constr, Component_Typ, Others_Allowed);
1357
1358          --  Do not resolve the expressions of discrete or others choices
1359          --  unless the expression covers a single component, or the expander
1360          --  is inactive.
1361
1362          elsif Single_Elmt
1363            or else not Expander_Active
1364            or else In_Default_Expression
1365          then
1366             Analyze_And_Resolve (Expr, Component_Typ);
1367             Check_Non_Static_Context (Expr);
1368             Aggregate_Constraint_Checks (Expr, Component_Typ);
1369          end if;
1370
1371          if Raises_Constraint_Error (Expr)
1372            and then Nkind (Parent (Expr)) /= N_Component_Association
1373          then
1374             Set_Raises_Constraint_Error (N);
1375          end if;
1376
1377          return Resolution_OK;
1378       end Resolve_Aggr_Expr;
1379
1380       --  Variables local to Resolve_Array_Aggregate
1381
1382       Assoc   : Node_Id;
1383       Choice  : Node_Id;
1384       Expr    : Node_Id;
1385
1386       Who_Cares : Node_Id;
1387
1388       Aggr_Low  : Node_Id := Empty;
1389       Aggr_High : Node_Id := Empty;
1390       --  The actual low and high bounds of this sub-aggegate
1391
1392       Choices_Low  : Node_Id := Empty;
1393       Choices_High : Node_Id := Empty;
1394       --  The lowest and highest discrete choices values for a named aggregate
1395
1396       Nb_Elements : Uint := Uint_0;
1397       --  The number of elements in a positional aggegate
1398
1399       Others_Present : Boolean := False;
1400
1401       Nb_Choices : Nat := 0;
1402       --  Contains the overall number of named choices in this sub-aggregate
1403
1404       Nb_Discrete_Choices : Nat := 0;
1405       --  The overall number of discrete choices (not counting others choice)
1406
1407       Case_Table_Size : Nat;
1408       --  Contains the size of the case table needed to sort aggregate choices
1409
1410    --  Start of processing for Resolve_Array_Aggregate
1411
1412    begin
1413       --  STEP 1: make sure the aggregate is correctly formatted
1414
1415       if Present (Component_Associations (N)) then
1416          Assoc := First (Component_Associations (N));
1417          while Present (Assoc) loop
1418             Choice := First (Choices (Assoc));
1419             while Present (Choice) loop
1420                if Nkind (Choice) = N_Others_Choice then
1421                   Others_Present := True;
1422
1423                   if Choice /= First (Choices (Assoc))
1424                     or else Present (Next (Choice))
1425                   then
1426                      Error_Msg_N
1427                        ("OTHERS must appear alone in a choice list", Choice);
1428                      return Failure;
1429                   end if;
1430
1431                   if Present (Next (Assoc)) then
1432                      Error_Msg_N
1433                        ("OTHERS must appear last in an aggregate", Choice);
1434                      return Failure;
1435                   end if;
1436
1437                   if Ada_83
1438                     and then Assoc /= First (Component_Associations (N))
1439                     and then (Nkind (Parent (N)) = N_Assignment_Statement
1440                                or else
1441                                  Nkind (Parent (N)) = N_Object_Declaration)
1442                   then
1443                      Error_Msg_N
1444                        ("(Ada 83) illegal context for OTHERS choice", N);
1445                   end if;
1446                end if;
1447
1448                Nb_Choices := Nb_Choices + 1;
1449                Next (Choice);
1450             end loop;
1451
1452             Next (Assoc);
1453          end loop;
1454       end if;
1455
1456       --  At this point we know that the others choice, if present, is by
1457       --  itself and appears last in the aggregate. Check if we have mixed
1458       --  positional and discrete associations (other than the others choice).
1459
1460       if Present (Expressions (N))
1461         and then (Nb_Choices > 1
1462                    or else (Nb_Choices = 1 and then not Others_Present))
1463       then
1464          Error_Msg_N
1465            ("named association cannot follow positional association",
1466             First (Choices (First (Component_Associations (N)))));
1467          return Failure;
1468       end if;
1469
1470       --  Test for the validity of an others choice if present
1471
1472       if Others_Present and then not Others_Allowed then
1473          Error_Msg_N
1474            ("OTHERS choice not allowed here",
1475             First (Choices (First (Component_Associations (N)))));
1476          return Failure;
1477       end if;
1478
1479       --  STEP 2: Process named components
1480
1481       if No (Expressions (N)) then
1482
1483          if Others_Present then
1484             Case_Table_Size := Nb_Choices - 1;
1485          else
1486             Case_Table_Size := Nb_Choices;
1487          end if;
1488
1489          Step_2 : declare
1490             Low  : Node_Id;
1491             High : Node_Id;
1492             --  Denote the lowest and highest values in an aggregate choice
1493
1494             Hi_Val : Uint;
1495             Lo_Val : Uint;
1496             --  High end of one range and Low end of the next. Should be
1497             --  contiguous if there is no hole in the list of values.
1498
1499             Missing_Values : Boolean;
1500             --  Set True if missing index values
1501
1502             S_Low  : Node_Id := Empty;
1503             S_High : Node_Id := Empty;
1504             --  if a choice in an aggregate is a subtype indication these
1505             --  denote the lowest and highest values of the subtype
1506
1507             Table : Case_Table_Type (1 .. Case_Table_Size);
1508             --  Used to sort all the different choice values
1509
1510             Single_Choice : Boolean;
1511             --  Set to true every time there is a single discrete choice in a
1512             --  discrete association
1513
1514             Prev_Nb_Discrete_Choices : Nat;
1515             --  Used to keep track of the number of discrete choices
1516             --  in the current association.
1517
1518          begin
1519             --  STEP 2 (A): Check discrete choices validity.
1520
1521             Assoc := First (Component_Associations (N));
1522             while Present (Assoc) loop
1523
1524                Prev_Nb_Discrete_Choices := Nb_Discrete_Choices;
1525                Choice := First (Choices (Assoc));
1526                loop
1527                   Analyze (Choice);
1528
1529                   if Nkind (Choice) = N_Others_Choice then
1530                      Single_Choice := False;
1531                      exit;
1532
1533                   --  Test for subtype mark without constraint
1534
1535                   elsif Is_Entity_Name (Choice) and then
1536                     Is_Type (Entity (Choice))
1537                   then
1538                      if Base_Type (Entity (Choice)) /= Index_Base then
1539                         Error_Msg_N
1540                           ("invalid subtype mark in aggregate choice",
1541                            Choice);
1542                         return Failure;
1543                      end if;
1544
1545                   elsif Nkind (Choice) = N_Subtype_Indication then
1546                      Resolve_Discrete_Subtype_Indication (Choice, Index_Base);
1547
1548                      --  Does the subtype indication evaluation raise CE ?
1549
1550                      Get_Index_Bounds (Subtype_Mark (Choice), S_Low, S_High);
1551                      Get_Index_Bounds (Choice, Low, High);
1552                      Check_Bounds (S_Low, S_High, Low, High);
1553
1554                   else  --  Choice is a range or an expression
1555                      Resolve (Choice, Index_Base);
1556                      Check_Non_Static_Context (Choice);
1557
1558                      --  Do not range check a choice. This check is redundant
1559                      --  since this test is already performed when we check
1560                      --  that the bounds of the array aggregate are within
1561                      --  range.
1562
1563                      Set_Do_Range_Check (Choice, False);
1564                   end if;
1565
1566                   --  If we could not resolve the discrete choice stop here
1567
1568                   if Etype (Choice) = Any_Type then
1569                      return Failure;
1570
1571                   --  If the discrete choice raises CE get its original bounds.
1572
1573                   elsif Nkind (Choice) = N_Raise_Constraint_Error then
1574                      Set_Raises_Constraint_Error (N);
1575                      Get_Index_Bounds (Original_Node (Choice), Low, High);
1576
1577                   --  Otherwise get its bounds as usual
1578
1579                   else
1580                      Get_Index_Bounds (Choice, Low, High);
1581                   end if;
1582
1583                   if (Dynamic_Or_Null_Range (Low, High)
1584                        or else (Nkind (Choice) = N_Subtype_Indication
1585                                  and then
1586                                    Dynamic_Or_Null_Range (S_Low, S_High)))
1587                     and then Nb_Choices /= 1
1588                   then
1589                      Error_Msg_N
1590                        ("dynamic or empty choice in aggregate " &
1591                         "must be the only choice", Choice);
1592                      return Failure;
1593                   end if;
1594
1595                   Nb_Discrete_Choices := Nb_Discrete_Choices + 1;
1596                   Table (Nb_Discrete_Choices).Choice_Lo := Low;
1597                   Table (Nb_Discrete_Choices).Choice_Hi := High;
1598
1599                   Next (Choice);
1600
1601                   if No (Choice) then
1602                      --  Check if we have a single discrete choice and whether
1603                      --  this discrete choice specifies a single value.
1604
1605                      Single_Choice :=
1606                        (Nb_Discrete_Choices = Prev_Nb_Discrete_Choices + 1)
1607                          and then (Low = High);
1608
1609                      exit;
1610                   end if;
1611                end loop;
1612
1613                if not
1614                  Resolve_Aggr_Expr
1615                    (Expression (Assoc), Single_Elmt => Single_Choice)
1616                then
1617                   return Failure;
1618                end if;
1619
1620                Next (Assoc);
1621             end loop;
1622
1623             --  If aggregate contains more than one choice then these must be
1624             --  static. Sort them and check that they are contiguous
1625
1626             if Nb_Discrete_Choices > 1 then
1627                Sort_Case_Table (Table);
1628                Missing_Values := False;
1629
1630                Outer : for J in 1 .. Nb_Discrete_Choices - 1 loop
1631                   if Expr_Value (Table (J).Choice_Hi) >=
1632                        Expr_Value (Table (J + 1).Choice_Lo)
1633                   then
1634                      Error_Msg_N
1635                        ("duplicate choice values in array aggregate",
1636                         Table (J).Choice_Hi);
1637                      return Failure;
1638
1639                   elsif not Others_Present then
1640
1641                      Hi_Val := Expr_Value (Table (J).Choice_Hi);
1642                      Lo_Val := Expr_Value (Table (J + 1).Choice_Lo);
1643
1644                      --  If missing values, output error messages
1645
1646                      if Lo_Val - Hi_Val > 1 then
1647
1648                         --  Header message if not first missing value
1649
1650                         if not Missing_Values then
1651                            Error_Msg_N
1652                              ("missing index value(s) in array aggregate", N);
1653                            Missing_Values := True;
1654                         end if;
1655
1656                         --  Output values of missing indexes
1657
1658                         Lo_Val := Lo_Val - 1;
1659                         Hi_Val := Hi_Val + 1;
1660
1661                         --  Enumeration type case
1662
1663                         if Is_Enumeration_Type (Index_Typ) then
1664                            Error_Msg_Name_1 :=
1665                              Chars
1666                                (Get_Enum_Lit_From_Pos
1667                                  (Index_Typ, Hi_Val, Loc));
1668
1669                            if Lo_Val = Hi_Val then
1670                               Error_Msg_N ("\  %", N);
1671                            else
1672                               Error_Msg_Name_2 :=
1673                                 Chars
1674                                   (Get_Enum_Lit_From_Pos
1675                                     (Index_Typ, Lo_Val, Loc));
1676                               Error_Msg_N ("\  % .. %", N);
1677                            end if;
1678
1679                         --  Integer types case
1680
1681                         else
1682                            Error_Msg_Uint_1 := Hi_Val;
1683
1684                            if Lo_Val = Hi_Val then
1685                               Error_Msg_N ("\  ^", N);
1686                            else
1687                               Error_Msg_Uint_2 := Lo_Val;
1688                               Error_Msg_N ("\  ^ .. ^", N);
1689                            end if;
1690                         end if;
1691                      end if;
1692                   end if;
1693                end loop Outer;
1694
1695                if Missing_Values then
1696                   Set_Etype (N, Any_Composite);
1697                   return Failure;
1698                end if;
1699             end if;
1700
1701             --  STEP 2 (B): Compute aggregate bounds and min/max choices values
1702
1703             if Nb_Discrete_Choices > 0 then
1704                Choices_Low  := Table (1).Choice_Lo;
1705                Choices_High := Table (Nb_Discrete_Choices).Choice_Hi;
1706             end if;
1707
1708             if Others_Present then
1709                Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
1710
1711             else
1712                Aggr_Low  := Choices_Low;
1713                Aggr_High := Choices_High;
1714             end if;
1715          end Step_2;
1716
1717       --  STEP 3: Process positional components
1718
1719       else
1720          --  STEP 3 (A): Process positional elements
1721
1722          Expr := First (Expressions (N));
1723          Nb_Elements := Uint_0;
1724          while Present (Expr) loop
1725             Nb_Elements := Nb_Elements + 1;
1726
1727             if not Resolve_Aggr_Expr (Expr, Single_Elmt => True) then
1728                return Failure;
1729             end if;
1730
1731             Next (Expr);
1732          end loop;
1733
1734          if Others_Present then
1735             Assoc := Last (Component_Associations (N));
1736             if not Resolve_Aggr_Expr (Expression (Assoc),
1737                                       Single_Elmt => False)
1738             then
1739                return Failure;
1740             end if;
1741          end if;
1742
1743          --  STEP 3 (B): Compute the aggregate bounds
1744
1745          if Others_Present then
1746             Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
1747
1748          else
1749             if Others_Allowed then
1750                Get_Index_Bounds (Index_Constr, Aggr_Low, Who_Cares);
1751             else
1752                Aggr_Low := Index_Typ_Low;
1753             end if;
1754
1755             Aggr_High := Add (Nb_Elements - 1, To => Aggr_Low);
1756             Check_Bound (Index_Base_High, Aggr_High);
1757          end if;
1758       end if;
1759
1760       --  STEP 4: Perform static aggregate checks and save the bounds
1761
1762       --  Check (A)
1763
1764       Check_Bounds (Index_Typ_Low, Index_Typ_High, Aggr_Low, Aggr_High);
1765       Check_Bounds (Index_Base_Low, Index_Base_High, Aggr_Low, Aggr_High);
1766
1767       --  Check (B)
1768
1769       if Others_Present and then Nb_Discrete_Choices > 0 then
1770          Check_Bounds (Aggr_Low, Aggr_High, Choices_Low, Choices_High);
1771          Check_Bounds (Index_Typ_Low, Index_Typ_High,
1772                        Choices_Low, Choices_High);
1773          Check_Bounds (Index_Base_Low, Index_Base_High,
1774                        Choices_Low, Choices_High);
1775
1776       --  Check (C)
1777
1778       elsif Others_Present and then Nb_Elements > 0 then
1779          Check_Length (Aggr_Low, Aggr_High, Nb_Elements);
1780          Check_Length (Index_Typ_Low, Index_Typ_High, Nb_Elements);
1781          Check_Length (Index_Base_Low, Index_Base_High, Nb_Elements);
1782
1783       end if;
1784
1785       if Raises_Constraint_Error (Aggr_Low)
1786         or else Raises_Constraint_Error (Aggr_High)
1787       then
1788          Set_Raises_Constraint_Error (N);
1789       end if;
1790
1791       Aggr_Low := Duplicate_Subexpr (Aggr_Low);
1792
1793       --  Do not duplicate Aggr_High if Aggr_High = Aggr_Low + Nb_Elements
1794       --  since the addition node returned by Add is not yet analyzed. Attach
1795       --  to tree and analyze first. Reset analyzed flag to insure it will get
1796       --  analyzed when it is a literal bound whose type must be properly
1797       --  set.
1798
1799       if Others_Present or else Nb_Discrete_Choices > 0 then
1800          Aggr_High := Duplicate_Subexpr (Aggr_High);
1801
1802          if Etype (Aggr_High) = Universal_Integer then
1803             Set_Analyzed (Aggr_High, False);
1804          end if;
1805       end if;
1806
1807       Set_Aggregate_Bounds
1808         (N, Make_Range (Loc, Low_Bound => Aggr_Low, High_Bound => Aggr_High));
1809
1810       --  The bounds may contain expressions that must be inserted upwards.
1811       --  Attach them fully to the tree. After analysis, remove side effects
1812       --  from upper bound, if still needed.
1813
1814       Set_Parent (Aggregate_Bounds (N), N);
1815       Analyze_And_Resolve (Aggregate_Bounds (N), Index_Typ);
1816
1817       if not Others_Present and then Nb_Discrete_Choices = 0 then
1818          Set_High_Bound (Aggregate_Bounds (N),
1819              Duplicate_Subexpr (High_Bound (Aggregate_Bounds (N))));
1820       end if;
1821
1822       return Success;
1823    end Resolve_Array_Aggregate;
1824
1825    ---------------------------------
1826    -- Resolve_Extension_Aggregate --
1827    ---------------------------------
1828
1829    --  There are two cases to consider:
1830
1831    --  a) If the ancestor part is a type mark, the components needed are
1832    --  the difference between the components of the expected type and the
1833    --  components of the given type mark.
1834
1835    --  b) If the ancestor part is an expression, it must be unambiguous,
1836    --  and once we have its type we can also compute the needed  components
1837    --  as in the previous case. In both cases, if the ancestor type is not
1838    --  the immediate ancestor, we have to build this ancestor recursively.
1839
1840    --  In both cases discriminants of the ancestor type do not play a
1841    --  role in the resolution of the needed components, because inherited
1842    --  discriminants cannot be used in a type extension. As a result we can
1843    --  compute independently the list of components of the ancestor type and
1844    --  of the expected type.
1845
1846    procedure Resolve_Extension_Aggregate (N : Node_Id; Typ : Entity_Id) is
1847       A        : constant Node_Id := Ancestor_Part (N);
1848       A_Type   : Entity_Id;
1849       I        : Interp_Index;
1850       It       : Interp;
1851       Imm_Type : Entity_Id;
1852
1853       function Valid_Ancestor_Type return Boolean;
1854       --  Verify that the type of the ancestor part is a non-private ancestor
1855       --  of the expected type.
1856
1857       function Valid_Ancestor_Type return Boolean is
1858          Imm_Type : Entity_Id;
1859
1860       begin
1861          Imm_Type := Base_Type (Typ);
1862          while Is_Derived_Type (Imm_Type)
1863            and then Etype (Imm_Type) /= Base_Type (A_Type)
1864          loop
1865             Imm_Type := Etype (Base_Type (Imm_Type));
1866          end loop;
1867
1868          if Etype (Imm_Type) /= Base_Type (A_Type) then
1869             Error_Msg_NE ("expect ancestor type of &", A, Typ);
1870             return False;
1871          else
1872             return True;
1873          end if;
1874       end Valid_Ancestor_Type;
1875
1876    --  Start of processing for Resolve_Extension_Aggregate
1877
1878    begin
1879       Analyze (A);
1880
1881       if not Is_Tagged_Type (Typ) then
1882          Error_Msg_N ("type of extension aggregate must be tagged", N);
1883          return;
1884
1885       elsif Is_Limited_Type (Typ) then
1886          Error_Msg_N ("aggregate type cannot be limited", N);
1887          return;
1888
1889       elsif Is_Class_Wide_Type (Typ) then
1890          Error_Msg_N ("aggregate cannot be of a class-wide type", N);
1891          return;
1892       end if;
1893
1894       if Is_Entity_Name (A)
1895         and then Is_Type (Entity (A))
1896       then
1897          A_Type   := Get_Full_View (Entity (A));
1898          Imm_Type := Base_Type (Typ);
1899
1900          if Valid_Ancestor_Type then
1901             Set_Entity (A, A_Type);
1902             Set_Etype  (A, A_Type);
1903
1904             Validate_Ancestor_Part (N);
1905             Resolve_Record_Aggregate (N, Typ);
1906          end if;
1907
1908       elsif Nkind (A) /= N_Aggregate then
1909          if Is_Overloaded (A) then
1910             A_Type := Any_Type;
1911             Get_First_Interp (A, I, It);
1912
1913             while Present (It.Typ) loop
1914
1915                if Is_Tagged_Type (It.Typ)
1916                   and then not Is_Limited_Type (It.Typ)
1917                then
1918                   if A_Type /= Any_Type then
1919                      Error_Msg_N ("cannot resolve expression", A);
1920                      return;
1921                   else
1922                      A_Type := It.Typ;
1923                   end if;
1924                end if;
1925
1926                Get_Next_Interp (I, It);
1927             end loop;
1928
1929             if A_Type = Any_Type then
1930                Error_Msg_N
1931                  ("ancestor part must be non-limited tagged type", A);
1932                return;
1933             end if;
1934
1935          else
1936             A_Type := Etype (A);
1937          end if;
1938
1939          if Valid_Ancestor_Type then
1940             Resolve (A, A_Type);
1941             Check_Non_Static_Context (A);
1942             Resolve_Record_Aggregate (N, Typ);
1943          end if;
1944
1945       else
1946          Error_Msg_N (" No unique type for this aggregate",  A);
1947       end if;
1948
1949    end Resolve_Extension_Aggregate;
1950
1951    ------------------------------
1952    -- Resolve_Record_Aggregate --
1953    ------------------------------
1954
1955    procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id) is
1956       Regular_Aggr    : constant Boolean := Nkind (N) /= N_Extension_Aggregate;
1957
1958       New_Assoc_List  : List_Id := New_List;
1959       New_Assoc       : Node_Id;
1960       --  New_Assoc_List is the newly built list of N_Component_Association
1961       --  nodes. New_Assoc is one such N_Component_Association node in it.
1962       --  Please note that while Assoc and New_Assoc contain the same
1963       --  kind of nodes, they are used to iterate over two different
1964       --  N_Component_Association lists.
1965
1966       Others_Etype : Entity_Id := Empty;
1967       --  This variable is used to save the Etype of the last record component
1968       --  that takes its value from the others choice. Its purpose is:
1969       --
1970       --    (a) make sure the others choice is useful
1971       --
1972       --    (b) make sure the type of all the components whose value is
1973       --        subsumed by the others choice are the same.
1974       --
1975       --  This variable is updated as a side effect of function Get_Value
1976
1977       procedure Add_Association (Component : Entity_Id; Expr : Node_Id);
1978       --  Builds a new N_Component_Association node which associates
1979       --  Component to expression Expr and adds it to the new association
1980       --  list New_Assoc_List being built.
1981
1982       function Discr_Present (Discr : Entity_Id) return Boolean;
1983       --  If aggregate N is a regular aggregate this routine will return True.
1984       --  Otherwise, if N is an extension aggreagte, Discr is a discriminant
1985       --  whose value may already have been specified by N's ancestor part,
1986       --  this routine checks whether this is indeed the case and if so
1987       --  returns False, signaling that no value for Discr should appear in the
1988       --  N's aggregate part. Also, in this case, the routine appends to
1989       --  New_Assoc_List Discr the discriminant value specified in the ancestor
1990       --  part.
1991
1992       function Get_Value
1993         (Compon                 : Node_Id;
1994          From                   : List_Id;
1995          Consider_Others_Choice : Boolean := False)
1996          return                   Node_Id;
1997       --  Given a record component stored in parameter Compon, the
1998       --  following function returns its value as it appears in the list
1999       --  From, which is a list of N_Component_Association nodes. If no
2000       --  component association has a choice for the searched component,
2001       --  the value provided by the others choice is returned, if there
2002       --  is  one and Consider_Others_Choice is set to true. Otherwise
2003       --  Empty is returned. If there is more than one component association
2004       --  giving a value for the searched record component, an error message
2005       --  is emitted and the first found value is returned.
2006       --
2007       --  If Consider_Others_Choice is set and the returned expression comes
2008       --  from the others choice, then Others_Etype is set as a side effect.
2009       --  An error message is emitted if the components taking their value
2010       --  from the others choice do not have same type.
2011
2012       procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Node_Id);
2013       --  Analyzes and resolves expression Expr against the Etype of the
2014       --  Component. This routine also applies all appropriate checks to Expr.
2015       --  It finally saves a Expr in the newly created association list that
2016       --  will be attached to the final record aggregate. Note that if the
2017       --  Parent pointer of Expr is not set then Expr was produced with a
2018       --  New_copy_Tree or some such.
2019
2020       ---------------------
2021       -- Add_Association --
2022       ---------------------
2023
2024       procedure Add_Association (Component : Entity_Id; Expr : Node_Id) is
2025          New_Assoc   : Node_Id;
2026          Choice_List : List_Id := New_List;
2027
2028       begin
2029          Append (New_Occurrence_Of (Component, Sloc (Expr)), Choice_List);
2030          New_Assoc :=
2031            Make_Component_Association (Sloc (Expr),
2032              Choices    => Choice_List,
2033              Expression => Expr);
2034          Append (New_Assoc, New_Assoc_List);
2035       end Add_Association;
2036
2037       -------------------
2038       -- Discr_Present --
2039       -------------------
2040
2041       function Discr_Present (Discr : Entity_Id) return Boolean is
2042          Loc : Source_Ptr;
2043
2044          Ancestor     : Node_Id;
2045          Discr_Expr   : Node_Id;
2046
2047          Ancestor_Typ : Entity_Id;
2048          Orig_Discr   : Entity_Id;
2049          D            : Entity_Id;
2050          D_Val        : Elmt_Id := No_Elmt; -- stop junk warning
2051
2052          Ancestor_Is_Subtyp : Boolean;
2053
2054       begin
2055          if Regular_Aggr then
2056             return True;
2057          end if;
2058
2059          Ancestor     := Ancestor_Part (N);
2060          Ancestor_Typ := Etype (Ancestor);
2061          Loc          := Sloc (Ancestor);
2062
2063          Ancestor_Is_Subtyp :=
2064            Is_Entity_Name (Ancestor) and then Is_Type (Entity (Ancestor));
2065
2066          --  If the ancestor part has no discriminants clearly N's aggregate
2067          --  part must provide a value for Discr.
2068
2069          if not Has_Discriminants (Ancestor_Typ) then
2070             return True;
2071
2072          --  If the ancestor part is an unconstrained subtype mark then the
2073          --  Discr must be present in N's aggregate part.
2074
2075          elsif Ancestor_Is_Subtyp
2076            and then not Is_Constrained (Entity (Ancestor))
2077          then
2078             return True;
2079          end if;
2080
2081          --  Now look to see if Discr was specified in the ancestor part.
2082
2083          Orig_Discr := Original_Record_Component (Discr);
2084          D          := First_Discriminant (Ancestor_Typ);
2085
2086          if Ancestor_Is_Subtyp then
2087             D_Val := First_Elmt (Discriminant_Constraint (Entity (Ancestor)));
2088          end if;
2089
2090          while Present (D) loop
2091             --  If Ancestor has already specified Disc value than
2092             --  insert its value in the final aggregate.
2093
2094             if Original_Record_Component (D) = Orig_Discr then
2095                if Ancestor_Is_Subtyp then
2096                   Discr_Expr := New_Copy_Tree (Node (D_Val));
2097                else
2098                   Discr_Expr :=
2099                     Make_Selected_Component (Loc,
2100                       Prefix        => Duplicate_Subexpr (Ancestor),
2101                       Selector_Name => New_Occurrence_Of (Discr, Loc));
2102                end if;
2103
2104                Resolve_Aggr_Expr (Discr_Expr, Discr);
2105                return False;
2106             end if;
2107
2108             Next_Discriminant (D);
2109
2110             if Ancestor_Is_Subtyp then
2111                Next_Elmt (D_Val);
2112             end if;
2113          end loop;
2114
2115          return True;
2116       end Discr_Present;
2117
2118       ---------------
2119       -- Get_Value --
2120       ---------------
2121
2122       function Get_Value
2123         (Compon                 : Node_Id;
2124          From                   : List_Id;
2125          Consider_Others_Choice : Boolean := False)
2126          return                   Node_Id
2127       is
2128          Assoc         : Node_Id;
2129          Expr          : Node_Id := Empty;
2130          Selector_Name : Node_Id;
2131
2132       begin
2133          if Present (From) then
2134             Assoc := First (From);
2135          else
2136             return Empty;
2137          end if;
2138
2139          while Present (Assoc) loop
2140             Selector_Name := First (Choices (Assoc));
2141             while Present (Selector_Name) loop
2142                if Nkind (Selector_Name) = N_Others_Choice then
2143                   if Consider_Others_Choice and then No (Expr) then
2144                      if Present (Others_Etype) and then
2145                         Base_Type (Others_Etype) /= Base_Type (Etype (Compon))
2146                      then
2147                         Error_Msg_N ("components in OTHERS choice must " &
2148                                      "have same type", Selector_Name);
2149                      end if;
2150
2151                      Others_Etype := Etype (Compon);
2152
2153                      --  We need to duplicate the expression for each
2154                      --  successive component covered by the others choice.
2155                      --  If the expression is itself an array aggregate with
2156                      --  "others", its subtype must be obtained from the
2157                      --  current component, and therefore it must be (at least
2158                      --  partly) reanalyzed.
2159
2160                      if Analyzed (Expression (Assoc)) then
2161                         Expr := New_Copy_Tree (Expression (Assoc));
2162
2163                         if Nkind (Expr) = N_Aggregate
2164                           and then Is_Array_Type (Etype (Expr))
2165                           and then No (Expressions (Expr))
2166                           and then
2167                             Nkind (First (Choices
2168                               (First (Component_Associations (Expr)))))
2169                                 = N_Others_Choice
2170                         then
2171                            Set_Analyzed (Expr, False);
2172                         end if;
2173
2174                         return Expr;
2175
2176                      else
2177                         return Expression (Assoc);
2178                      end if;
2179                   end if;
2180
2181                elsif Chars (Compon) = Chars (Selector_Name) then
2182                   if No (Expr) then
2183                      --  We need to duplicate the expression when several
2184                      --  components are grouped together with a "|" choice.
2185                      --  For instance "filed1 | filed2 => Expr"
2186
2187                      if Present (Next (Selector_Name)) then
2188                         Expr := New_Copy_Tree (Expression (Assoc));
2189                      else
2190                         Expr := Expression (Assoc);
2191                      end if;
2192
2193                   else
2194                      Error_Msg_NE
2195                        ("more than one value supplied for &",
2196                         Selector_Name, Compon);
2197
2198                   end if;
2199                end if;
2200
2201                Next (Selector_Name);
2202             end loop;
2203
2204             Next (Assoc);
2205          end loop;
2206
2207          return Expr;
2208       end Get_Value;
2209
2210       -----------------------
2211       -- Resolve_Aggr_Expr --
2212       -----------------------
2213
2214       procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Node_Id) is
2215          New_C     : Entity_Id := Component;
2216          Expr_Type : Entity_Id := Empty;
2217
2218          function Has_Expansion_Delayed (Expr : Node_Id) return Boolean;
2219          --  If the expression is an aggregate (possibly qualified) then its
2220          --  expansion is delayed until the enclosing aggregate is expanded
2221          --  into assignments. In that case, do not generate checks on the
2222          --  expression, because they will be generated later, and will other-
2223          --  wise force a copy (to remove side-effects) that would leave a
2224          --  dynamic-sized aggregate in the code, something that gigi cannot
2225          --  handle.
2226
2227          Relocate  : Boolean;
2228          --  Set to True if the resolved Expr node needs to be relocated
2229          --  when attached to the newly created association list. This node
2230          --  need not be relocated if its parent pointer is not set.
2231          --  In fact in this case Expr is the output of a New_Copy_Tree call.
2232          --  if Relocate is True then we have analyzed the expression node
2233          --  in the original aggregate and hence it needs to be relocated
2234          --  when moved over the new association list.
2235
2236          function Has_Expansion_Delayed (Expr : Node_Id) return Boolean is
2237             Kind : constant Node_Kind := Nkind (Expr);
2238
2239          begin
2240             return ((Kind = N_Aggregate
2241                        or else Kind = N_Extension_Aggregate)
2242                      and then Present (Etype (Expr))
2243                      and then Is_Record_Type (Etype (Expr))
2244                      and then Expansion_Delayed (Expr))
2245
2246               or else (Kind = N_Qualified_Expression
2247                         and then Has_Expansion_Delayed (Expression (Expr)));
2248          end Has_Expansion_Delayed;
2249
2250       --  Start of processing for  Resolve_Aggr_Expr
2251
2252       begin
2253          --  If the type of the component is elementary or the type of the
2254          --  aggregate does not contain discriminants, use the type of the
2255          --  component to resolve Expr.
2256
2257          if Is_Elementary_Type (Etype (Component))
2258            or else not Has_Discriminants (Etype (N))
2259          then
2260             Expr_Type := Etype (Component);
2261
2262          --  Otherwise we have to pick up the new type of the component from
2263          --  the new costrained subtype of the aggregate. In fact components
2264          --  which are of a composite type might be constrained by a
2265          --  discriminant, and we want to resolve Expr against the subtype were
2266          --  all discriminant occurrences are replaced with their actual value.
2267
2268          else
2269             New_C := First_Component (Etype (N));
2270             while Present (New_C) loop
2271                if Chars (New_C) = Chars (Component) then
2272                   Expr_Type := Etype (New_C);
2273                   exit;
2274                end if;
2275
2276                Next_Component (New_C);
2277             end loop;
2278
2279             pragma Assert (Present (Expr_Type));
2280
2281             --  For each range in an array type where a discriminant has been
2282             --  replaced with the constraint, check that this range is within
2283             --  the range of the base type. This checks is done in the
2284             --  _init_proc for regular objects, but has to be done here for
2285             --  aggregates since no _init_proc is called for them.
2286
2287             if Is_Array_Type (Expr_Type) then
2288                declare
2289                   Index          : Node_Id := First_Index (Expr_Type);
2290                   --  Range of the current constrained index in the array.
2291
2292                   Orig_Index     : Node_Id := First_Index (Etype (Component));
2293                   --  Range corresponding to the range Index above in the
2294                   --  original unconstrained record type. The bounds of this
2295                   --  range may be governed by discriminants.
2296
2297                   Unconstr_Index : Node_Id := First_Index (Etype (Expr_Type));
2298                   --  Range corresponding to the range Index above for the
2299                   --  unconstrained array type. This range is needed to apply
2300                   --  range checks.
2301
2302                begin
2303                   while Present (Index) loop
2304                      if Depends_On_Discriminant (Orig_Index) then
2305                         Apply_Range_Check (Index, Etype (Unconstr_Index));
2306                      end if;
2307
2308                      Next_Index (Index);
2309                      Next_Index (Orig_Index);
2310                      Next_Index (Unconstr_Index);
2311                   end loop;
2312                end;
2313             end if;
2314          end if;
2315
2316          --  If the Parent pointer of Expr is not set, Expr is an expression
2317          --  duplicated by New_Tree_Copy (this happens for record aggregates
2318          --  that look like (Field1 | Filed2 => Expr) or (others => Expr)).
2319          --  Such a duplicated expression must be attached to the tree
2320          --  before analysis and resolution to enforce the rule that a tree
2321          --  fragment should never be analyzed or resolved unless it is
2322          --  attached to the current compilation unit.
2323
2324          if No (Parent (Expr)) then
2325             Set_Parent (Expr, N);
2326             Relocate := False;
2327          else
2328             Relocate := True;
2329          end if;
2330
2331          Analyze_And_Resolve (Expr, Expr_Type);
2332          Check_Non_Static_Context (Expr);
2333
2334          if not Has_Expansion_Delayed (Expr) then
2335             Aggregate_Constraint_Checks (Expr, Expr_Type);
2336          end if;
2337
2338          if Raises_Constraint_Error (Expr) then
2339             Set_Raises_Constraint_Error (N);
2340          end if;
2341
2342          if Relocate then
2343             Add_Association (New_C, Relocate_Node (Expr));
2344          else
2345             Add_Association (New_C, Expr);
2346          end if;
2347
2348       end Resolve_Aggr_Expr;
2349
2350       --  Resolve_Record_Aggregate local variables
2351
2352       Assoc : Node_Id;
2353       --  N_Component_Association node belonging to the input aggregate N
2354
2355       Expr            : Node_Id;
2356       Positional_Expr : Node_Id;
2357
2358       Component      : Entity_Id;
2359       Component_Elmt : Elmt_Id;
2360       Components     : Elist_Id := New_Elmt_List;
2361       --  Components is the list of the record components whose value must
2362       --  be provided in the aggregate. This list does include discriminants.
2363
2364    --  Start of processing for Resolve_Record_Aggregate
2365
2366    begin
2367       --  We may end up calling Duplicate_Subexpr on expressions that are
2368       --  attached to New_Assoc_List. For this reason we need to attach it
2369       --  to the tree by setting its parent pointer to N. This parent point
2370       --  will change in STEP 8 below.
2371
2372       Set_Parent (New_Assoc_List, N);
2373
2374       --  STEP 1: abstract type and null record verification
2375
2376       if Is_Abstract (Typ) then
2377          Error_Msg_N ("type of aggregate cannot be abstract",  N);
2378       end if;
2379
2380       if No (First_Entity (Typ)) and then Null_Record_Present (N) then
2381          Set_Etype (N, Typ);
2382          return;
2383
2384       elsif Present (First_Entity (Typ))
2385         and then Null_Record_Present (N)
2386         and then not Is_Tagged_Type (Typ)
2387       then
2388          Error_Msg_N ("record aggregate cannot be null", N);
2389          return;
2390
2391       elsif No (First_Entity (Typ)) then
2392          Error_Msg_N ("record aggregate must be null", N);
2393          return;
2394       end if;
2395
2396       --  STEP 2: Verify aggregate structure
2397
2398       Step_2 : declare
2399          Selector_Name : Node_Id;
2400          Bad_Aggregate : Boolean := False;
2401
2402       begin
2403          if Present (Component_Associations (N)) then
2404             Assoc := First (Component_Associations (N));
2405          else
2406             Assoc := Empty;
2407          end if;
2408
2409          while Present (Assoc) loop
2410             Selector_Name := First (Choices (Assoc));
2411             while Present (Selector_Name) loop
2412                if Nkind (Selector_Name) = N_Identifier then
2413                   null;
2414
2415                elsif Nkind (Selector_Name) = N_Others_Choice then
2416                   if Selector_Name /= First (Choices (Assoc))
2417                     or else Present (Next (Selector_Name))
2418                   then
2419                      Error_Msg_N ("OTHERS must appear alone in a choice list",
2420                                   Selector_Name);
2421                      return;
2422
2423                   elsif Present (Next (Assoc)) then
2424                      Error_Msg_N ("OTHERS must appear last in an aggregate",
2425                                   Selector_Name);
2426                      return;
2427                   end if;
2428
2429                else
2430                   Error_Msg_N
2431                     ("selector name should be identifier or OTHERS",
2432                      Selector_Name);
2433                   Bad_Aggregate := True;
2434                end if;
2435
2436                Next (Selector_Name);
2437             end loop;
2438
2439             Next (Assoc);
2440          end loop;
2441
2442          if Bad_Aggregate then
2443             return;
2444          end if;
2445       end Step_2;
2446
2447       --  STEP 3: Find discriminant Values
2448
2449       Step_3 : declare
2450          Discrim               : Entity_Id;
2451          Missing_Discriminants : Boolean := False;
2452
2453       begin
2454          if Present (Expressions (N)) then
2455             Positional_Expr := First (Expressions (N));
2456          else
2457             Positional_Expr := Empty;
2458          end if;
2459
2460          if Has_Discriminants (Typ) then
2461             Discrim := First_Discriminant (Typ);
2462          else
2463             Discrim := Empty;
2464          end if;
2465
2466          --  First find the discriminant values in the positional components
2467
2468          while Present (Discrim) and then Present (Positional_Expr) loop
2469             if Discr_Present (Discrim) then
2470                Resolve_Aggr_Expr (Positional_Expr, Discrim);
2471                Next (Positional_Expr);
2472             end if;
2473
2474             if Present (Get_Value (Discrim, Component_Associations (N))) then
2475                Error_Msg_NE
2476                  ("more than one value supplied for discriminant&",
2477                   N, Discrim);
2478             end if;
2479
2480             Next_Discriminant (Discrim);
2481          end loop;
2482
2483          --  Find remaining discriminant values, if any, among named components
2484
2485          while Present (Discrim) loop
2486             Expr := Get_Value (Discrim, Component_Associations (N), True);
2487
2488             if not Discr_Present (Discrim) then
2489                if Present (Expr) then
2490                   Error_Msg_NE
2491                     ("more than one value supplied for discriminant&",
2492                      N, Discrim);
2493                end if;
2494
2495             elsif No (Expr) then
2496                Error_Msg_NE
2497                  ("no value supplied for discriminant &", N, Discrim);
2498                Missing_Discriminants := True;
2499
2500             else
2501                Resolve_Aggr_Expr (Expr, Discrim);
2502             end if;
2503
2504             Next_Discriminant (Discrim);
2505          end loop;
2506
2507          if Missing_Discriminants then
2508             return;
2509          end if;
2510
2511          --  At this point and until the beginning of STEP 6, New_Assoc_List
2512          --  contains only the discriminants and their values.
2513
2514       end Step_3;
2515
2516       --  STEP 4: Set the Etype of the record aggregate
2517
2518       --  ??? This code is pretty much a copy of Sem_Ch3.Build_Subtype. That
2519       --  routine should really be exported in sem_util or some such and used
2520       --  in sem_ch3 and here rather than have a copy of the code which is a
2521       --  maintenance nightmare.
2522
2523       --  ??? Performace WARNING. The current implementation creates a new
2524       --  itype for all aggregates whose base type is discriminated.
2525       --  This means that for record aggregates nested inside an array
2526       --  aggregate we will create a new itype for each record aggregate
2527       --  if the array cmponent type has discriminants. For large aggregates
2528       --  this may be a problem. What should be done in this case is
2529       --  to reuse itypes as much as possible.
2530
2531       if Has_Discriminants (Typ) then
2532          Build_Constrained_Itype : declare
2533             Loc         : constant Source_Ptr := Sloc (N);
2534             Indic       : Node_Id;
2535             Subtyp_Decl : Node_Id;
2536             Def_Id      : Entity_Id;
2537
2538             C : List_Id := New_List;
2539
2540          begin
2541             New_Assoc := First (New_Assoc_List);
2542             while Present (New_Assoc) loop
2543                Append (Duplicate_Subexpr (Expression (New_Assoc)), To => C);
2544                Next (New_Assoc);
2545             end loop;
2546
2547             Indic :=
2548               Make_Subtype_Indication (Loc,
2549                 Subtype_Mark => New_Occurrence_Of (Base_Type (Typ), Loc),
2550                 Constraint  => Make_Index_Or_Discriminant_Constraint (Loc, C));
2551
2552             Def_Id := Create_Itype (Ekind (Typ), N);
2553
2554             Subtyp_Decl :=
2555               Make_Subtype_Declaration (Loc,
2556                 Defining_Identifier => Def_Id,
2557                 Subtype_Indication  => Indic);
2558             Set_Parent (Subtyp_Decl, Parent (N));
2559
2560             --  Itypes must be analyzed with checks off (see itypes.ads).
2561
2562             Analyze (Subtyp_Decl, Suppress => All_Checks);
2563
2564             Set_Etype (N, Def_Id);
2565             Check_Static_Discriminated_Subtype
2566               (Def_Id, Expression (First (New_Assoc_List)));
2567          end Build_Constrained_Itype;
2568
2569       else
2570          Set_Etype (N, Typ);
2571       end if;
2572
2573       --  STEP 5: Get remaining components according to discriminant values
2574
2575       Step_5 : declare
2576          Record_Def      : Node_Id;
2577          Parent_Typ      : Entity_Id;
2578          Root_Typ        : Entity_Id;
2579          Parent_Typ_List : Elist_Id;
2580          Parent_Elmt     : Elmt_Id;
2581          Errors_Found    : Boolean := False;
2582          Dnode           : Node_Id;
2583
2584       begin
2585          if Is_Derived_Type (Typ) and then Is_Tagged_Type (Typ) then
2586             Parent_Typ_List := New_Elmt_List;
2587
2588             --  If this is an extension aggregate, the component list must
2589             --  include all components that are not in the given ancestor
2590             --  type. Otherwise, the component list must include components
2591             --  of all ancestors.
2592
2593             if Nkind (N) = N_Extension_Aggregate then
2594                Root_Typ := Base_Type (Etype (Ancestor_Part (N)));
2595             else
2596                Root_Typ := Root_Type (Typ);
2597
2598                if Nkind (Parent (Base_Type (Root_Typ)))
2599                     = N_Private_Type_Declaration
2600                then
2601                   Error_Msg_NE
2602                     ("type of aggregate has private ancestor&!",
2603                      N, Root_Typ);
2604                   Error_Msg_N  ("must use extension aggregate!", N);
2605                   return;
2606                end if;
2607
2608                Dnode := Declaration_Node (Base_Type (Root_Typ));
2609
2610                --  If we don't get a full declaration, then we have some
2611                --  error which will get signalled later so skip this part.
2612
2613                if Nkind (Dnode) = N_Full_Type_Declaration then
2614                   Record_Def := Type_Definition (Dnode);
2615                   Gather_Components (Typ,
2616                     Component_List (Record_Def),
2617                     Governed_By   => New_Assoc_List,
2618                     Into          => Components,
2619                     Report_Errors => Errors_Found);
2620                end if;
2621             end if;
2622
2623             Parent_Typ  := Base_Type (Typ);
2624             while Parent_Typ /= Root_Typ loop
2625
2626                Prepend_Elmt (Parent_Typ, To => Parent_Typ_List);
2627                Parent_Typ := Etype (Parent_Typ);
2628
2629                if (Nkind (Parent (Base_Type (Parent_Typ))) =
2630                                         N_Private_Type_Declaration
2631                     or else Nkind (Parent (Base_Type (Parent_Typ))) =
2632                                         N_Private_Extension_Declaration)
2633                then
2634                   if Nkind (N) /= N_Extension_Aggregate then
2635                      Error_Msg_NE
2636                        ("type of aggregate has private ancestor&!",
2637                         N, Parent_Typ);
2638                      Error_Msg_N  ("must use extension aggregate!", N);
2639                      return;
2640
2641                   elsif Parent_Typ /= Root_Typ then
2642                      Error_Msg_NE
2643                        ("ancestor part of aggregate must be private type&",
2644                          Ancestor_Part (N), Parent_Typ);
2645                      return;
2646                   end if;
2647                end if;
2648             end loop;
2649
2650             --  Now collect components from all other ancestors.
2651
2652             Parent_Elmt := First_Elmt (Parent_Typ_List);
2653             while Present (Parent_Elmt) loop
2654                Parent_Typ := Node (Parent_Elmt);
2655                Record_Def := Type_Definition (Parent (Base_Type (Parent_Typ)));
2656                Gather_Components (Empty,
2657                  Component_List (Record_Extension_Part (Record_Def)),
2658                  Governed_By   => New_Assoc_List,
2659                  Into          => Components,
2660                  Report_Errors => Errors_Found);
2661
2662                Next_Elmt (Parent_Elmt);
2663             end loop;
2664
2665          else
2666             Record_Def := Type_Definition (Parent (Base_Type (Typ)));
2667
2668             if Null_Present (Record_Def) then
2669                null;
2670             else
2671                Gather_Components (Typ,
2672                  Component_List (Record_Def),
2673                  Governed_By   => New_Assoc_List,
2674                  Into          => Components,
2675                  Report_Errors => Errors_Found);
2676             end if;
2677          end if;
2678
2679          if Errors_Found then
2680             return;
2681          end if;
2682       end Step_5;
2683
2684       --  STEP 6: Find component Values
2685
2686       Component := Empty;
2687       Component_Elmt := First_Elmt (Components);
2688
2689       --  First scan the remaining positional associations in the aggregate.
2690       --  Remember that at this point Positional_Expr contains the current
2691       --  positional association if any is left after looking for discriminant
2692       --  values in step 3.
2693
2694       while Present (Positional_Expr) and then Present (Component_Elmt) loop
2695          Component := Node (Component_Elmt);
2696          Resolve_Aggr_Expr (Positional_Expr, Component);
2697
2698          if Present (Get_Value (Component, Component_Associations (N))) then
2699             Error_Msg_NE
2700               ("more than one value supplied for Component &", N, Component);
2701          end if;
2702
2703          Next (Positional_Expr);
2704          Next_Elmt (Component_Elmt);
2705       end loop;
2706
2707       if Present (Positional_Expr) then
2708          Error_Msg_N
2709            ("too many components for record aggregate", Positional_Expr);
2710       end if;
2711
2712       --  Now scan for the named arguments of the aggregate
2713
2714       while Present (Component_Elmt) loop
2715          Component := Node (Component_Elmt);
2716          Expr := Get_Value (Component, Component_Associations (N), True);
2717
2718          if No (Expr) then
2719             Error_Msg_NE ("no value supplied for component &!", N, Component);
2720          else
2721             Resolve_Aggr_Expr (Expr, Component);
2722          end if;
2723
2724          Next_Elmt (Component_Elmt);
2725       end loop;
2726
2727       --  STEP 7: check for invalid components + check type in choice list
2728
2729       Step_7 : declare
2730          Selectr : Node_Id;
2731          --  Selector name
2732
2733          Typech  : Entity_Id;
2734          --  Type of first component in choice list
2735
2736       begin
2737          if Present (Component_Associations (N)) then
2738             Assoc := First (Component_Associations (N));
2739          else
2740             Assoc := Empty;
2741          end if;
2742
2743          Verification : while Present (Assoc) loop
2744             Selectr := First (Choices (Assoc));
2745             Typech := Empty;
2746
2747             if Nkind (Selectr) = N_Others_Choice then
2748                if No (Others_Etype) then
2749                   Error_Msg_N
2750                     ("OTHERS must represent at least one component", Selectr);
2751                end if;
2752
2753                exit Verification;
2754             end if;
2755
2756             while Present (Selectr) loop
2757                New_Assoc := First (New_Assoc_List);
2758                while Present (New_Assoc) loop
2759                   Component := First (Choices (New_Assoc));
2760                   exit when Chars (Selectr) = Chars (Component);
2761                   Next (New_Assoc);
2762                end loop;
2763
2764                --  If no association, this is not a legal component of
2765                --  of the type in question,  except if this is an internal
2766                --  component supplied by a previous expansion.
2767
2768                if No (New_Assoc) then
2769
2770                   if Chars (Selectr) /= Name_uTag
2771                     and then Chars (Selectr) /= Name_uParent
2772                     and then Chars (Selectr) /= Name_uController
2773                   then
2774                      if not Has_Discriminants (Typ) then
2775                         Error_Msg_Node_2 := Typ;
2776                         Error_Msg_N
2777                           ("& is not a component of}",
2778                             Selectr);
2779                      else
2780                         Error_Msg_N
2781                           ("& is not a component of the aggregate subtype",
2782                             Selectr);
2783                      end if;
2784
2785                      Check_Misspelled_Component (Components, Selectr);
2786                   end if;
2787
2788                elsif No (Typech) then
2789                   Typech := Base_Type (Etype (Component));
2790
2791                elsif Typech /= Base_Type (Etype (Component)) then
2792                   Error_Msg_N
2793                     ("components in choice list must have same type", Selectr);
2794                end if;
2795
2796                Next (Selectr);
2797             end loop;
2798
2799             Next (Assoc);
2800          end loop Verification;
2801       end Step_7;
2802
2803       --  STEP 8: replace the original aggregate
2804
2805       Step_8 : declare
2806          New_Aggregate : Node_Id := New_Copy (N);
2807
2808       begin
2809          Set_Expressions            (New_Aggregate, No_List);
2810          Set_Etype                  (New_Aggregate, Etype (N));
2811          Set_Component_Associations (New_Aggregate, New_Assoc_List);
2812
2813          Rewrite (N, New_Aggregate);
2814       end Step_8;
2815    end Resolve_Record_Aggregate;
2816
2817    ---------------------
2818    -- Sort_Case_Table --
2819    ---------------------
2820
2821    procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is
2822       L : Int := Case_Table'First;
2823       U : Int := Case_Table'Last;
2824       K : Int;
2825       J : Int;
2826       T : Case_Bounds;
2827
2828    begin
2829       K := L;
2830
2831       while K /= U loop
2832          T := Case_Table (K + 1);
2833          J := K + 1;
2834
2835          while J /= L
2836            and then Expr_Value (Case_Table (J - 1).Choice_Lo) >
2837                     Expr_Value (T.Choice_Lo)
2838          loop
2839             Case_Table (J) := Case_Table (J - 1);
2840             J := J - 1;
2841          end loop;
2842
2843          Case_Table (J) := T;
2844          K := K + 1;
2845       end loop;
2846    end Sort_Case_Table;
2847
2848 end Sem_Aggr;