OSDN Git Service

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