OSDN Git Service

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