OSDN Git Service

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