OSDN Git Service

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