ada: Fix (again) incorrect handling of Aggregate aspect
[official-gcc.git] / gcc / ada / sem_aggr.adb
blob33c44c42a24c1828c5cf9ff3de6d4aeb2be05cec
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-2023, 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 ------------------------------------------------------------------------------
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Einfo; use Einfo;
30 with Einfo.Entities; use Einfo.Entities;
31 with Einfo.Utils; use Einfo.Utils;
32 with Elists; use Elists;
33 with Errout; use Errout;
34 with Expander; use Expander;
35 with Exp_Ch6; use Exp_Ch6;
36 with Exp_Tss; use Exp_Tss;
37 with Exp_Util; use Exp_Util;
38 with Freeze; use Freeze;
39 with Itypes; use Itypes;
40 with Lib; use Lib;
41 with Lib.Xref; use Lib.Xref;
42 with Namet; use Namet;
43 with Namet.Sp; use Namet.Sp;
44 with Nmake; use Nmake;
45 with Nlists; use Nlists;
46 with Opt; use Opt;
47 with Restrict; use Restrict;
48 with Rident; use Rident;
49 with Sem; use Sem;
50 with Sem_Aux; use Sem_Aux;
51 with Sem_Case; use Sem_Case;
52 with Sem_Cat; use Sem_Cat;
53 with Sem_Ch3; use Sem_Ch3;
54 with Sem_Ch8; use Sem_Ch8;
55 with Sem_Ch13; use Sem_Ch13;
56 with Sem_Dim; use Sem_Dim;
57 with Sem_Eval; use Sem_Eval;
58 with Sem_Res; use Sem_Res;
59 with Sem_Util; use Sem_Util;
60 with Sem_Type; use Sem_Type;
61 with Sem_Warn; use Sem_Warn;
62 with Sinfo; use Sinfo;
63 with Sinfo.Nodes; use Sinfo.Nodes;
64 with Sinfo.Utils; use Sinfo.Utils;
65 with Snames; use Snames;
66 with Stringt; use Stringt;
67 with Stand; use Stand;
68 with Style; use Style;
69 with Targparm; use Targparm;
70 with Tbuild; use Tbuild;
71 with Ttypes; use Ttypes;
72 with Uintp; use Uintp;
73 with Warnsw; use Warnsw;
75 package body Sem_Aggr is
77 type Case_Bounds is record
78 Lo : Node_Id;
79 -- Low bound of choice. Once we sort the Case_Table, then entries
80 -- will be in order of ascending Choice_Lo values.
82 Hi : Node_Id;
83 -- High Bound of choice. The sort does not pay any attention to the
84 -- high bound, so choices 1 .. 4 and 1 .. 5 could be in either order.
86 Highest : Uint;
87 -- If there are duplicates or missing entries, then in the sorted
88 -- table, this records the highest value among Choice_Hi values
89 -- seen so far, including this entry.
91 Choice : Node_Id;
92 -- The node of the choice
93 end record;
95 type Case_Table_Type is array (Pos range <>) of Case_Bounds;
96 -- Table type used by Check_Case_Choices procedure
98 -----------------------
99 -- Local Subprograms --
100 -----------------------
102 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type);
103 -- Sort the Case Table using the Lower Bound of each Choice as the key. A
104 -- simple insertion sort is used since the choices in a case statement will
105 -- usually be in near sorted order.
107 procedure Check_Can_Never_Be_Null (Typ : Entity_Id; Expr : Node_Id);
108 -- Ada 2005 (AI-231): Check bad usage of null for a component for which
109 -- null exclusion (NOT NULL) is specified. Typ can be an E_Array_Type for
110 -- the array case (the component type of the array will be used) or an
111 -- E_Component/E_Discriminant entity in the record case, in which case the
112 -- type of the component will be used for the test. If Typ is any other
113 -- kind of entity, the call is ignored. Expr is the component node in the
114 -- aggregate which is known to have a null value. A warning message will be
115 -- issued if the component is null excluding.
117 -- It would be better to pass the proper type for Typ ???
119 procedure Check_Expr_OK_In_Limited_Aggregate (Expr : Node_Id);
120 -- Check that Expr is either not limited or else is one of the cases of
121 -- expressions allowed for a limited component association (namely, an
122 -- aggregate, function call, or <> notation). Report error for violations.
123 -- Expression is also OK in an instance or inlining context, because we
124 -- have already preanalyzed and it is known to be type correct.
126 ------------------------------------------------------
127 -- Subprograms used for RECORD AGGREGATE Processing --
128 ------------------------------------------------------
130 procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id);
131 -- This procedure performs all the semantic checks required for record
132 -- aggregates. Note that for aggregates analysis and resolution go
133 -- hand in hand. Aggregate analysis has been delayed up to here and
134 -- it is done while resolving the aggregate.
136 -- N is the N_Aggregate node.
137 -- Typ is the record type for the aggregate resolution
139 -- While performing the semantic checks, this procedure builds a new
140 -- Component_Association_List where each record field appears alone in a
141 -- Component_Choice_List along with its corresponding expression. The
142 -- record fields in the Component_Association_List appear in the same order
143 -- in which they appear in the record type Typ.
145 -- Once this new Component_Association_List is built and all the semantic
146 -- checks performed, the original aggregate subtree is replaced with the
147 -- new named record aggregate just built. This new record aggregate has no
148 -- positional associations, so its Expressions field is set to No_List.
149 -- Note that subtree substitution is performed with Rewrite so as to be
150 -- able to retrieve the original aggregate.
152 -- The aggregate subtree manipulation performed by Resolve_Record_Aggregate
153 -- yields the aggregate format expected by Gigi. Typically, this kind of
154 -- tree manipulations are done in the expander. However, because the
155 -- semantic checks that need to be performed on record aggregates really go
156 -- hand in hand with the record aggregate normalization, the aggregate
157 -- subtree transformation is performed during resolution rather than
158 -- expansion. Had we decided otherwise we would have had to duplicate most
159 -- of the code in the expansion procedure Expand_Record_Aggregate. Note,
160 -- however, that all the expansion concerning aggregates for tagged records
161 -- is done in Expand_Record_Aggregate.
163 -- The algorithm of Resolve_Record_Aggregate proceeds as follows:
165 -- 1. Make sure that the record type against which the record aggregate
166 -- has to be resolved is not abstract. Furthermore if the type is a
167 -- null aggregate make sure the input aggregate N is also null.
169 -- 2. Verify that the structure of the aggregate is that of a record
170 -- aggregate. Specifically, look for component associations and ensure
171 -- that each choice list only has identifiers or the N_Others_Choice
172 -- node. Also make sure that if present, the N_Others_Choice occurs
173 -- last and by itself.
175 -- 3. If Typ contains discriminants, the values for each discriminant is
176 -- looked for. If the record type Typ has variants, we check that the
177 -- expressions corresponding to each discriminant ruling the (possibly
178 -- nested) variant parts of Typ, are static. This allows us to determine
179 -- the variant parts to which the rest of the aggregate must conform.
180 -- The names of discriminants with their values are saved in a new
181 -- association list, New_Assoc_List which is later augmented with the
182 -- names and values of the remaining components in the record type.
184 -- During this phase we also make sure that every discriminant is
185 -- assigned exactly one value. Note that when several values for a given
186 -- discriminant are found, semantic processing continues looking for
187 -- further errors. In this case it's the first discriminant value found
188 -- which we will be recorded.
190 -- IMPORTANT NOTE: For derived tagged types this procedure expects
191 -- First_Discriminant and Next_Discriminant to give the correct list
192 -- of discriminants, in the correct order.
194 -- 4. After all the discriminant values have been gathered, we can set the
195 -- Etype of the record aggregate. If Typ contains no discriminants this
196 -- is straightforward: the Etype of N is just Typ, otherwise a new
197 -- implicit constrained subtype of Typ is built to be the Etype of N.
199 -- 5. Gather the remaining record components according to the discriminant
200 -- values. This involves recursively traversing the record type
201 -- structure to see what variants are selected by the given discriminant
202 -- values. This processing is a little more convoluted if Typ is a
203 -- derived tagged types since we need to retrieve the record structure
204 -- of all the ancestors of Typ.
206 -- 6. After gathering the record components we look for their values in the
207 -- record aggregate and emit appropriate error messages should we not
208 -- find such values or should they be duplicated.
210 -- 7. We then make sure no illegal component names appear in the record
211 -- aggregate and make sure that the type of the record components
212 -- appearing in a same choice list is the same. Finally we ensure that
213 -- the others choice, if present, is used to provide the value of at
214 -- least a record component.
216 -- 8. The original aggregate node is replaced with the new named aggregate
217 -- built in steps 3 through 6, as explained earlier.
219 -- Given the complexity of record aggregate resolution, the primary goal of
220 -- this routine is clarity and simplicity rather than execution and storage
221 -- efficiency. If there are only positional components in the aggregate the
222 -- running time is linear. If there are associations the running time is
223 -- still linear as long as the order of the associations is not too far off
224 -- the order of the components in the record type. If this is not the case
225 -- the running time is at worst quadratic in the size of the association
226 -- list.
228 procedure Check_Misspelled_Component
229 (Elements : Elist_Id;
230 Component : Node_Id);
231 -- Give possible misspelling diagnostic if Component is likely to be a
232 -- misspelling of one of the components of the Assoc_List. This is called
233 -- by Resolve_Aggr_Expr after producing an invalid component error message.
235 -----------------------------------------------------
236 -- Subprograms used for ARRAY AGGREGATE Processing --
237 -----------------------------------------------------
239 function Resolve_Array_Aggregate
240 (N : Node_Id;
241 Index : Node_Id;
242 Index_Constr : Node_Id;
243 Component_Typ : Entity_Id;
244 Others_Allowed : Boolean) return Boolean;
245 -- This procedure performs the semantic checks for an array aggregate.
246 -- True is returned if the aggregate resolution succeeds.
248 -- The procedure works by recursively checking each nested aggregate.
249 -- Specifically, after checking a sub-aggregate nested at the i-th level
250 -- we recursively check all the subaggregates at the i+1-st level (if any).
251 -- Note that aggregates analysis and resolution go hand in hand.
252 -- Aggregate analysis has been delayed up to here and it is done while
253 -- resolving the aggregate.
255 -- N is the current N_Aggregate node to be checked.
257 -- Index is the index node corresponding to the array sub-aggregate that
258 -- we are currently checking (RM 4.3.3 (8)). Its Etype is the
259 -- corresponding index type (or subtype).
261 -- Index_Constr is the node giving the applicable index constraint if
262 -- any (RM 4.3.3 (10)). It "is a constraint provided by certain
263 -- contexts [...] that can be used to determine the bounds of the array
264 -- value specified by the aggregate". If Others_Allowed below is False
265 -- there is no applicable index constraint and this node is set to Index.
267 -- Component_Typ is the array component type.
269 -- Others_Allowed indicates whether an others choice is allowed
270 -- in the context where the top-level aggregate appeared.
272 -- The algorithm of Resolve_Array_Aggregate proceeds as follows:
274 -- 1. Make sure that the others choice, if present, is by itself and
275 -- appears last in the sub-aggregate. Check that we do not have
276 -- positional and named components in the array sub-aggregate (unless
277 -- the named association is an others choice). Finally if an others
278 -- choice is present, make sure it is allowed in the aggregate context.
280 -- 2. If the array sub-aggregate contains discrete_choices:
282 -- (A) Verify their validity. Specifically verify that:
284 -- (a) If a null range is present it must be the only possible
285 -- choice in the array aggregate.
287 -- (b) Ditto for a non static range.
289 -- (c) Ditto for a non static expression.
291 -- In addition this step analyzes and resolves each discrete_choice,
292 -- making sure that its type is the type of the corresponding Index.
293 -- If we are not at the lowest array aggregate level (in the case of
294 -- multi-dimensional aggregates) then invoke Resolve_Array_Aggregate
295 -- recursively on each component expression. Otherwise, resolve the
296 -- bottom level component expressions against the expected component
297 -- type ONLY IF the component corresponds to a single discrete choice
298 -- which is not an others choice (to see why read the DELAYED
299 -- COMPONENT RESOLUTION below).
301 -- (B) Determine the bounds of the sub-aggregate and lowest and
302 -- highest choice values.
304 -- 3. For positional aggregates:
306 -- (A) Loop over the component expressions either recursively invoking
307 -- Resolve_Array_Aggregate on each of these for multi-dimensional
308 -- array aggregates or resolving the bottom level component
309 -- expressions against the expected component type.
311 -- (B) Determine the bounds of the positional sub-aggregates.
313 -- 4. Try to determine statically whether the evaluation of the array
314 -- sub-aggregate raises Constraint_Error. If yes emit proper
315 -- warnings. The precise checks are the following:
317 -- (A) Check that the index range defined by aggregate bounds is
318 -- compatible with corresponding index subtype.
319 -- We also check against the base type. In fact it could be that
320 -- Low/High bounds of the base type are static whereas those of
321 -- the index subtype are not. Thus if we can statically catch
322 -- a problem with respect to the base type we are guaranteed
323 -- that the same problem will arise with the index subtype
325 -- (B) If we are dealing with a named aggregate containing an others
326 -- choice and at least one discrete choice then make sure the range
327 -- specified by the discrete choices does not overflow the
328 -- aggregate bounds. We also check against the index type and base
329 -- type bounds for the same reasons given in (A).
331 -- (C) If we are dealing with a positional aggregate with an others
332 -- choice make sure the number of positional elements specified
333 -- does not overflow the aggregate bounds. We also check against
334 -- the index type and base type bounds as mentioned in (A).
336 -- Finally construct an N_Range node giving the sub-aggregate bounds.
337 -- Set the Aggregate_Bounds field of the sub-aggregate to be this
338 -- N_Range. The routine Array_Aggr_Subtype below uses such N_Ranges
339 -- to build the appropriate aggregate subtype. Aggregate_Bounds
340 -- information is needed during expansion.
342 -- DELAYED COMPONENT RESOLUTION: The resolution of bottom level component
343 -- expressions in an array aggregate may call Duplicate_Subexpr or some
344 -- other routine that inserts code just outside the outermost aggregate.
345 -- If the array aggregate contains discrete choices or an others choice,
346 -- this may be wrong. Consider for instance the following example.
348 -- type Rec is record
349 -- V : Integer := 0;
350 -- end record;
352 -- type Acc_Rec is access Rec;
353 -- Arr : array (1..3) of Acc_Rec := (1 .. 3 => new Rec);
355 -- Then the transformation of "new Rec" that occurs during resolution
356 -- entails the following code modifications
358 -- P7b : constant Acc_Rec := new Rec;
359 -- RecIP (P7b.all);
360 -- Arr : array (1..3) of Acc_Rec := (1 .. 3 => P7b);
362 -- This code transformation is clearly wrong, since we need to call
363 -- "new Rec" for each of the 3 array elements. To avoid this problem we
364 -- delay resolution of the components of non positional array aggregates
365 -- to the expansion phase. As an optimization, if the discrete choice
366 -- specifies a single value we do not delay resolution.
368 function Array_Aggr_Subtype (N : Node_Id; Typ : Entity_Id) return Entity_Id;
369 -- This routine returns the type or subtype of an array aggregate.
371 -- N is the array aggregate node whose type we return.
373 -- Typ is the context type in which N occurs.
375 -- This routine creates an implicit array subtype whose bounds are
376 -- those defined by the aggregate. When this routine is invoked
377 -- Resolve_Array_Aggregate has already processed aggregate N. Thus the
378 -- Aggregate_Bounds of each sub-aggregate, is an N_Range node giving the
379 -- sub-aggregate bounds. When building the aggregate itype, this function
380 -- traverses the array aggregate N collecting such Aggregate_Bounds and
381 -- constructs the proper array aggregate itype.
383 -- Note that in the case of multidimensional aggregates each inner
384 -- sub-aggregate corresponding to a given array dimension, may provide a
385 -- different bounds. If it is possible to determine statically that
386 -- some sub-aggregates corresponding to the same index do not have the
387 -- same bounds, then a warning is emitted. If such check is not possible
388 -- statically (because some sub-aggregate bounds are dynamic expressions)
389 -- then this job is left to the expander. In all cases the particular
390 -- bounds that this function will chose for a given dimension is the first
391 -- N_Range node for a sub-aggregate corresponding to that dimension.
393 -- Note that the Raises_Constraint_Error flag of an array aggregate
394 -- whose evaluation is determined to raise CE by Resolve_Array_Aggregate,
395 -- is set in Resolve_Array_Aggregate but the aggregate is not
396 -- immediately replaced with a raise CE. In fact, Array_Aggr_Subtype must
397 -- first construct the proper itype for the aggregate (Gigi needs
398 -- this). After constructing the proper itype we will eventually replace
399 -- the top-level aggregate with a raise CE (done in Resolve_Aggregate).
400 -- Of course in cases such as:
402 -- type Arr is array (integer range <>) of Integer;
403 -- A : Arr := (positive range -1 .. 2 => 0);
405 -- The bounds of the aggregate itype are cooked up to look reasonable
406 -- (in this particular case the bounds will be 1 .. 2).
408 procedure Make_String_Into_Aggregate (N : Node_Id);
409 -- A string literal can appear in a context in which a one dimensional
410 -- array of characters is expected. This procedure simply rewrites the
411 -- string as an aggregate, prior to resolution.
413 function Resolve_Null_Array_Aggregate (N : Node_Id) return Boolean;
414 -- For the Ada 2022 construct, build a subtype with a null range for each
415 -- dimension, using the bounds from the context subtype (if the subtype
416 -- is constrained). If the subtype is unconstrained, then the bounds
417 -- are determined in much the same way as the bounds for a null string
418 -- literal with no applicable index constraint.
420 ---------------------------------
421 -- Delta aggregate processing --
422 ---------------------------------
424 procedure Resolve_Delta_Array_Aggregate (N : Node_Id; Typ : Entity_Id);
425 procedure Resolve_Delta_Record_Aggregate (N : Node_Id; Typ : Entity_Id);
427 ------------------------
428 -- Array_Aggr_Subtype --
429 ------------------------
431 function Array_Aggr_Subtype
432 (N : Node_Id;
433 Typ : Entity_Id) return Entity_Id
435 Aggr_Dimension : constant Pos := Number_Dimensions (Typ);
436 -- Number of aggregate index dimensions
438 Aggr_Range : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
439 -- Constrained N_Range of each index dimension in our aggregate itype
441 Aggr_Low : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
442 Aggr_High : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
443 -- Low and High bounds for each index dimension in our aggregate itype
445 Is_Fully_Positional : Boolean := True;
447 procedure Collect_Aggr_Bounds (N : Node_Id; Dim : Pos);
448 -- N is an array (sub-)aggregate. Dim is the dimension corresponding
449 -- to (sub-)aggregate N. This procedure collects and removes the side
450 -- effects of the constrained N_Range nodes corresponding to each index
451 -- dimension of our aggregate itype. These N_Range nodes are collected
452 -- in Aggr_Range above.
454 -- Likewise collect in Aggr_Low & Aggr_High above the low and high
455 -- bounds of each index dimension. If, when collecting, two bounds
456 -- corresponding to the same dimension are static and found to differ,
457 -- then emit a warning, and mark N as raising Constraint_Error.
459 -------------------------
460 -- Collect_Aggr_Bounds --
461 -------------------------
463 procedure Collect_Aggr_Bounds (N : Node_Id; Dim : Pos) is
464 This_Range : constant Node_Id := Aggregate_Bounds (N);
465 -- The aggregate range node of this specific sub-aggregate
467 This_Low : constant Node_Id := Low_Bound (Aggregate_Bounds (N));
468 This_High : constant Node_Id := High_Bound (Aggregate_Bounds (N));
469 -- The aggregate bounds of this specific sub-aggregate
471 Assoc : Node_Id;
472 Expr : Node_Id;
474 begin
475 Remove_Side_Effects (This_Low, Variable_Ref => True);
476 Remove_Side_Effects (This_High, Variable_Ref => True);
478 -- Collect the first N_Range for a given dimension that you find.
479 -- For a given dimension they must be all equal anyway.
481 if No (Aggr_Range (Dim)) then
482 Aggr_Low (Dim) := This_Low;
483 Aggr_High (Dim) := This_High;
484 Aggr_Range (Dim) := This_Range;
486 else
487 if Compile_Time_Known_Value (This_Low) then
488 if not Compile_Time_Known_Value (Aggr_Low (Dim)) then
489 Aggr_Low (Dim) := This_Low;
491 elsif Expr_Value (This_Low) /= Expr_Value (Aggr_Low (Dim)) then
492 Set_Raises_Constraint_Error (N);
493 Error_Msg_Warn := SPARK_Mode /= On;
494 Error_Msg_N ("sub-aggregate low bound mismatch<<", N);
495 Error_Msg_N ("\Constraint_Error [<<", N);
496 end if;
497 end if;
499 if Compile_Time_Known_Value (This_High) then
500 if not Compile_Time_Known_Value (Aggr_High (Dim)) then
501 Aggr_High (Dim) := This_High;
503 elsif
504 Expr_Value (This_High) /= Expr_Value (Aggr_High (Dim))
505 then
506 Set_Raises_Constraint_Error (N);
507 Error_Msg_Warn := SPARK_Mode /= On;
508 Error_Msg_N ("sub-aggregate high bound mismatch<<", N);
509 Error_Msg_N ("\Constraint_Error [<<", N);
510 end if;
511 end if;
512 end if;
514 if Dim < Aggr_Dimension then
516 -- Process positional components
518 if Present (Expressions (N)) then
519 Expr := First (Expressions (N));
520 while Present (Expr) loop
521 Collect_Aggr_Bounds (Expr, Dim + 1);
522 Next (Expr);
523 end loop;
524 end if;
526 -- Process component associations
528 if Present (Component_Associations (N)) then
529 Is_Fully_Positional := False;
531 Assoc := First (Component_Associations (N));
532 while Present (Assoc) loop
533 Expr := Expression (Assoc);
534 Collect_Aggr_Bounds (Expr, Dim + 1);
535 Next (Assoc);
536 end loop;
537 end if;
538 end if;
539 end Collect_Aggr_Bounds;
541 -- Array_Aggr_Subtype variables
543 Itype : Entity_Id;
544 -- The final itype of the overall aggregate
546 Index_Constraints : constant List_Id := New_List;
547 -- The list of index constraints of the aggregate itype
549 -- Start of processing for Array_Aggr_Subtype
551 begin
552 -- Make sure that the list of index constraints is properly attached to
553 -- the tree, and then collect the aggregate bounds.
555 -- If no aggregaate bounds have been set, this is an aggregate with
556 -- iterator specifications and a dynamic size to be determined by
557 -- first pass of expanded code.
559 if No (Aggregate_Bounds (N)) then
560 return Typ;
561 end if;
563 Set_Parent (Index_Constraints, N);
565 -- When resolving a null aggregate we created a list of aggregate bounds
566 -- for the consecutive dimensions. The bounds for the first dimension
567 -- are attached as the Aggregate_Bounds of the aggregate node.
569 if Is_Null_Aggregate (N) then
570 declare
571 This_Range : Node_Id := Aggregate_Bounds (N);
572 begin
573 for J in 1 .. Aggr_Dimension loop
574 Aggr_Range (J) := This_Range;
575 Next_Index (This_Range);
577 -- Remove bounds from the list, so they can be reattached as
578 -- the First_Index/Next_Index again by the code that also
579 -- handles non-null aggregates.
581 Remove (Aggr_Range (J));
582 end loop;
583 end;
584 else
585 Collect_Aggr_Bounds (N, 1);
586 end if;
588 -- Build the list of constrained indexes of our aggregate itype
590 for J in 1 .. Aggr_Dimension loop
591 Create_Index : declare
592 Index_Base : constant Entity_Id :=
593 Base_Type (Etype (Aggr_Range (J)));
594 Index_Typ : Entity_Id;
596 begin
597 -- Construct the Index subtype, and associate it with the range
598 -- construct that generates it.
600 Index_Typ :=
601 Create_Itype (Subtype_Kind (Ekind (Index_Base)), Aggr_Range (J));
603 Set_Etype (Index_Typ, Index_Base);
605 if Is_Character_Type (Index_Base) then
606 Set_Is_Character_Type (Index_Typ);
607 end if;
609 Set_Size_Info (Index_Typ, (Index_Base));
610 Set_RM_Size (Index_Typ, RM_Size (Index_Base));
611 Set_First_Rep_Item (Index_Typ, First_Rep_Item (Index_Base));
612 Set_Scalar_Range (Index_Typ, Aggr_Range (J));
614 if Is_Discrete_Or_Fixed_Point_Type (Index_Typ) then
615 Set_RM_Size (Index_Typ, UI_From_Int (Minimum_Size (Index_Typ)));
616 end if;
618 Set_Etype (Aggr_Range (J), Index_Typ);
620 Append (Aggr_Range (J), To => Index_Constraints);
621 end Create_Index;
622 end loop;
624 -- Now build the Itype
626 Itype := Create_Itype (E_Array_Subtype, N);
628 Set_First_Rep_Item (Itype, First_Rep_Item (Typ));
629 Set_Convention (Itype, Convention (Typ));
630 Set_Depends_On_Private (Itype, Has_Private_Component (Typ));
631 Set_Etype (Itype, Base_Type (Typ));
632 Set_Has_Alignment_Clause (Itype, Has_Alignment_Clause (Typ));
633 Set_Is_Aliased (Itype, Is_Aliased (Typ));
634 Set_Is_Independent (Itype, Is_Independent (Typ));
635 Set_Depends_On_Private (Itype, Depends_On_Private (Typ));
637 Copy_Suppress_Status (Index_Check, Typ, Itype);
638 Copy_Suppress_Status (Length_Check, Typ, Itype);
640 Set_First_Index (Itype, First (Index_Constraints));
641 Set_Is_Constrained (Itype, True);
642 Set_Is_Internal (Itype, True);
644 if Has_Predicates (Typ) then
645 Set_Has_Predicates (Itype);
647 -- If the base type has a predicate, capture the predicated parent
648 -- or the existing predicate function for SPARK use.
650 if Present (Predicate_Function (Typ)) then
651 Set_Predicate_Function (Itype, Predicate_Function (Typ));
653 elsif Is_Itype (Typ) then
654 Set_Predicated_Parent (Itype, Predicated_Parent (Typ));
656 else
657 Set_Predicated_Parent (Itype, Typ);
658 end if;
659 end if;
661 -- A simple optimization: purely positional aggregates of static
662 -- components should be passed to gigi unexpanded whenever possible, and
663 -- regardless of the staticness of the bounds themselves. Subsequent
664 -- checks in exp_aggr verify that type is not packed, etc.
666 Set_Size_Known_At_Compile_Time
667 (Itype,
668 Is_Fully_Positional
669 and then Comes_From_Source (N)
670 and then Size_Known_At_Compile_Time (Component_Type (Typ)));
672 -- We always need a freeze node for a packed array subtype, so that we
673 -- can build the Packed_Array_Impl_Type corresponding to the subtype. If
674 -- expansion is disabled, the packed array subtype is not built, and we
675 -- must not generate a freeze node for the type, or else it will appear
676 -- incomplete to gigi.
678 if Is_Packed (Itype)
679 and then not In_Spec_Expression
680 and then Expander_Active
681 then
682 Freeze_Itype (Itype, N);
683 end if;
685 return Itype;
686 end Array_Aggr_Subtype;
688 --------------------------------
689 -- Check_Misspelled_Component --
690 --------------------------------
692 procedure Check_Misspelled_Component
693 (Elements : Elist_Id;
694 Component : Node_Id)
696 Max_Suggestions : constant := 2;
698 Nr_Of_Suggestions : Natural := 0;
699 Suggestion_1 : Entity_Id := Empty;
700 Suggestion_2 : Entity_Id := Empty;
701 Component_Elmt : Elmt_Id;
703 begin
704 -- All the components of List are matched against Component and a count
705 -- is maintained of possible misspellings. When at the end of the
706 -- analysis there are one or two (not more) possible misspellings,
707 -- these misspellings will be suggested as possible corrections.
709 Component_Elmt := First_Elmt (Elements);
710 while Nr_Of_Suggestions <= Max_Suggestions
711 and then Present (Component_Elmt)
712 loop
713 if Is_Bad_Spelling_Of
714 (Chars (Node (Component_Elmt)),
715 Chars (Component))
716 then
717 Nr_Of_Suggestions := Nr_Of_Suggestions + 1;
719 case Nr_Of_Suggestions is
720 when 1 => Suggestion_1 := Node (Component_Elmt);
721 when 2 => Suggestion_2 := Node (Component_Elmt);
722 when others => null;
723 end case;
724 end if;
726 Next_Elmt (Component_Elmt);
727 end loop;
729 -- Report at most two suggestions
731 if Nr_Of_Suggestions = 1 then
732 Error_Msg_NE -- CODEFIX
733 ("\possible misspelling of&", Component, Suggestion_1);
735 elsif Nr_Of_Suggestions = 2 then
736 Error_Msg_Node_2 := Suggestion_2;
737 Error_Msg_NE -- CODEFIX
738 ("\possible misspelling of& or&", Component, Suggestion_1);
739 end if;
740 end Check_Misspelled_Component;
742 ----------------------------------------
743 -- Check_Expr_OK_In_Limited_Aggregate --
744 ----------------------------------------
746 procedure Check_Expr_OK_In_Limited_Aggregate (Expr : Node_Id) is
747 begin
748 if Is_Limited_Type (Etype (Expr))
749 and then Comes_From_Source (Expr)
750 then
751 if In_Instance_Body or else In_Inlined_Body then
752 null;
754 elsif not OK_For_Limited_Init (Etype (Expr), Expr) then
755 Error_Msg_N
756 ("initialization not allowed for limited types", Expr);
757 Explain_Limited_Type (Etype (Expr), Expr);
758 end if;
759 end if;
760 end Check_Expr_OK_In_Limited_Aggregate;
762 -------------------------
763 -- Is_Others_Aggregate --
764 -------------------------
766 function Is_Others_Aggregate (Aggr : Node_Id) return Boolean is
767 Assoc : constant List_Id := Component_Associations (Aggr);
769 begin
770 return No (Expressions (Aggr))
771 and then Nkind (First (Choice_List (First (Assoc)))) = N_Others_Choice;
772 end Is_Others_Aggregate;
774 -------------------------
775 -- Is_Single_Aggregate --
776 -------------------------
778 function Is_Single_Aggregate (Aggr : Node_Id) return Boolean is
779 Assoc : constant List_Id := Component_Associations (Aggr);
781 begin
782 return No (Expressions (Aggr))
783 and then No (Next (First (Assoc)))
784 and then No (Next (First (Choice_List (First (Assoc)))));
785 end Is_Single_Aggregate;
787 -----------------------
788 -- Is_Null_Aggregate --
789 -----------------------
791 function Is_Null_Aggregate (N : Node_Id) return Boolean is
792 begin
793 return Ada_Version >= Ada_2022
794 and then Is_Homogeneous_Aggregate (N)
795 and then Is_Empty_List (Expressions (N))
796 and then Is_Empty_List (Component_Associations (N));
797 end Is_Null_Aggregate;
799 ----------------------------------------
800 -- Is_Null_Array_Aggregate_High_Bound --
801 ----------------------------------------
803 function Is_Null_Array_Aggregate_High_Bound (N : Node_Id) return Boolean is
804 Original_N : constant Node_Id := Original_Node (N);
805 begin
806 return Ada_Version >= Ada_2022
807 and then not Comes_From_Source (Original_N)
808 and then Nkind (Original_N) = N_Attribute_Reference
809 and then
810 Get_Attribute_Id (Attribute_Name (Original_N)) = Attribute_Pred
811 and then Nkind (Parent (N)) in N_Range | N_Op_Le
812 and then not Comes_From_Source (Parent (N));
813 end Is_Null_Array_Aggregate_High_Bound;
815 --------------------------------
816 -- Make_String_Into_Aggregate --
817 --------------------------------
819 procedure Make_String_Into_Aggregate (N : Node_Id) is
820 Exprs : constant List_Id := New_List;
821 Loc : constant Source_Ptr := Sloc (N);
822 Str : constant String_Id := Strval (N);
823 Strlen : constant Nat := String_Length (Str);
824 C : Char_Code;
825 C_Node : Node_Id;
826 New_N : Node_Id;
827 P : Source_Ptr;
829 begin
830 P := Loc + 1;
831 for J in 1 .. Strlen loop
832 C := Get_String_Char (Str, J);
833 Set_Character_Literal_Name (C);
835 C_Node :=
836 Make_Character_Literal (P,
837 Chars => Name_Find,
838 Char_Literal_Value => UI_From_CC (C));
839 Set_Etype (C_Node, Any_Character);
840 Append_To (Exprs, C_Node);
842 P := P + 1;
843 -- Something special for wide strings???
844 end loop;
846 New_N := Make_Aggregate (Loc, Expressions => Exprs);
847 Set_Analyzed (New_N);
848 Set_Etype (New_N, Any_Composite);
850 Rewrite (N, New_N);
851 end Make_String_Into_Aggregate;
853 -----------------------
854 -- Resolve_Aggregate --
855 -----------------------
857 procedure Resolve_Aggregate (N : Node_Id; Typ : Entity_Id) is
858 Loc : constant Source_Ptr := Sloc (N);
860 Aggr_Subtyp : Entity_Id;
861 -- The actual aggregate subtype. This is not necessarily the same as Typ
862 -- which is the subtype of the context in which the aggregate was found.
864 Others_Box : Boolean := False;
865 -- Set to True if N represents a simple aggregate with only
866 -- (others => <>), not nested as part of another aggregate.
868 function Is_Full_Access_Aggregate (N : Node_Id) return Boolean;
869 -- If a full access object is initialized with an aggregate or is
870 -- assigned an aggregate, we have to prevent a piecemeal access or
871 -- assignment to the object, even if the aggregate is to be expanded.
872 -- We create a temporary for the aggregate, and assign the temporary
873 -- instead, so that the back end can generate an atomic move for it.
874 -- This is only done in the context of an object declaration or an
875 -- assignment. Function is a noop and returns false in other contexts.
877 function Within_Aggregate (N : Node_Id) return Boolean;
878 -- Return True if N is part of an N_Aggregate
880 ------------------------------
881 -- Is_Full_Access_Aggregate --
882 ------------------------------
884 function Is_Full_Access_Aggregate (N : Node_Id) return Boolean is
885 Loc : constant Source_Ptr := Sloc (N);
887 New_N : Node_Id;
888 Par : Node_Id;
889 Temp : Entity_Id;
890 Typ : Entity_Id;
892 begin
893 Par := Parent (N);
895 -- Aggregate may be qualified, so find outer context
897 if Nkind (Par) = N_Qualified_Expression then
898 Par := Parent (Par);
899 end if;
901 if not Comes_From_Source (Par) then
902 return False;
903 end if;
905 case Nkind (Par) is
906 when N_Assignment_Statement =>
907 Typ := Etype (Name (Par));
909 if not Is_Full_Access (Typ)
910 and then not Is_Full_Access_Object (Name (Par))
911 then
912 return False;
913 end if;
915 when N_Object_Declaration =>
916 Typ := Etype (Defining_Identifier (Par));
918 if not Is_Full_Access (Typ)
919 and then not Is_Full_Access (Defining_Identifier (Par))
920 then
921 return False;
922 end if;
924 when others =>
925 return False;
926 end case;
928 Temp := Make_Temporary (Loc, 'T', N);
929 New_N :=
930 Make_Object_Declaration (Loc,
931 Defining_Identifier => Temp,
932 Constant_Present => True,
933 Object_Definition => New_Occurrence_Of (Typ, Loc),
934 Expression => Relocate_Node (N));
935 Insert_Action (Par, New_N);
937 Rewrite (N, New_Occurrence_Of (Temp, Loc));
938 Analyze_And_Resolve (N, Typ);
940 return True;
941 end Is_Full_Access_Aggregate;
943 ----------------------
944 -- Within_Aggregate --
945 ----------------------
947 function Within_Aggregate (N : Node_Id) return Boolean is
948 P : Node_Id := Parent (N);
949 begin
950 while Present (P) loop
951 if Nkind (P) = N_Aggregate then
952 return True;
953 end if;
955 P := Parent (P);
956 end loop;
958 return False;
959 end Within_Aggregate;
961 -- Start of processing for Resolve_Aggregate
963 begin
964 -- Ignore junk empty aggregate resulting from parser error
966 if No (Expressions (N))
967 and then No (Component_Associations (N))
968 and then not Null_Record_Present (N)
969 then
970 return;
972 -- If the aggregate is assigned to a full access variable, we have
973 -- to prevent a piecemeal assignment even if the aggregate is to be
974 -- expanded. We create a temporary for the aggregate, and assign the
975 -- temporary instead, so that the back end can generate an atomic move
976 -- for it. This is properly an expansion activity but it must be done
977 -- before resolution because aggregate resolution cannot be done twice.
979 elsif Expander_Active and then Is_Full_Access_Aggregate (N) then
980 return;
981 end if;
983 -- If the aggregate has box-initialized components, its type must be
984 -- frozen so that initialization procedures can properly be called
985 -- in the resolution that follows. The replacement of boxes with
986 -- initialization calls is properly an expansion activity but it must
987 -- be done during resolution.
989 if Expander_Active
990 and then Present (Component_Associations (N))
991 then
992 declare
993 Comp : Node_Id;
994 First_Comp : Boolean := True;
996 begin
997 Comp := First (Component_Associations (N));
998 while Present (Comp) loop
999 if Box_Present (Comp) then
1000 if First_Comp
1001 and then No (Expressions (N))
1002 and then Nkind (First (Choices (Comp))) = N_Others_Choice
1003 and then not Within_Aggregate (N)
1004 then
1005 Others_Box := True;
1006 end if;
1008 Insert_Actions (N, Freeze_Entity (Typ, N));
1009 exit;
1010 end if;
1012 First_Comp := False;
1013 Next (Comp);
1014 end loop;
1015 end;
1016 end if;
1018 -- Check for aggregates not allowed in configurable run-time mode.
1019 -- We allow all cases of aggregates that do not come from source, since
1020 -- these are all assumed to be small (e.g. bounds of a string literal).
1021 -- We also allow aggregates of types we know to be small.
1023 if not Support_Aggregates_On_Target
1024 and then Comes_From_Source (N)
1025 and then (not Known_Static_Esize (Typ)
1026 or else Esize (Typ) > System_Max_Integer_Size)
1027 then
1028 Error_Msg_CRT ("aggregate", N);
1029 end if;
1031 -- Ada 2005 (AI-287): Limited aggregates allowed
1033 -- In an instance, ignore aggregate subcomponents that may be limited,
1034 -- because they originate in view conflicts. If the original aggregate
1035 -- is legal and the actuals are legal, the aggregate itself is legal.
1037 if Is_Limited_Type (Typ)
1038 and then Ada_Version < Ada_2005
1039 and then not In_Instance
1040 then
1041 Error_Msg_N ("aggregate type cannot be limited", N);
1042 Explain_Limited_Type (Typ, N);
1044 elsif Is_Class_Wide_Type (Typ) then
1045 Error_Msg_N ("type of aggregate cannot be class-wide", N);
1047 elsif Typ = Any_String
1048 or else Typ = Any_Composite
1049 then
1050 Error_Msg_N ("no unique type for aggregate", N);
1051 Set_Etype (N, Any_Composite);
1053 elsif Is_Array_Type (Typ) and then Null_Record_Present (N) then
1054 Error_Msg_N ("null record forbidden in array aggregate", N);
1056 elsif Has_Aspect (Typ, Aspect_Aggregate)
1057 and then Ekind (Typ) /= E_Record_Type
1058 and then Ada_Version >= Ada_2022
1059 then
1060 -- Check for Ada 2022 and () aggregate.
1062 if not Is_Homogeneous_Aggregate (N) then
1063 Error_Msg_N ("container aggregate must use '['], not ()", N);
1064 end if;
1066 Resolve_Container_Aggregate (N, Typ);
1068 elsif Is_Record_Type (Typ) then
1069 Resolve_Record_Aggregate (N, Typ);
1071 elsif Is_Array_Type (Typ) then
1073 -- First a special test, for the case of a positional aggregate of
1074 -- characters which can be replaced by a string literal.
1076 -- Do not perform this transformation if this was a string literal
1077 -- to start with, whose components needed constraint checks, or if
1078 -- the component type is non-static, because it will require those
1079 -- checks and be transformed back into an aggregate. If the index
1080 -- type is not Integer the aggregate may represent a user-defined
1081 -- string type but the context might need the original type so we
1082 -- do not perform the transformation at this point.
1084 if Number_Dimensions (Typ) = 1
1085 and then Is_Standard_Character_Type (Component_Type (Typ))
1086 and then No (Component_Associations (N))
1087 and then not Is_Limited_Composite (Typ)
1088 and then not Is_Private_Composite (Typ)
1089 and then not Is_Bit_Packed_Array (Typ)
1090 and then Nkind (Original_Node (Parent (N))) /= N_String_Literal
1091 and then Is_OK_Static_Subtype (Component_Type (Typ))
1092 and then Base_Type (Etype (First_Index (Typ))) =
1093 Base_Type (Standard_Integer)
1094 then
1095 declare
1096 Expr : Node_Id;
1098 begin
1099 Expr := First (Expressions (N));
1100 while Present (Expr) loop
1101 exit when Nkind (Expr) /= N_Character_Literal;
1102 Next (Expr);
1103 end loop;
1105 if No (Expr) then
1106 Start_String;
1108 Expr := First (Expressions (N));
1109 while Present (Expr) loop
1110 Store_String_Char (UI_To_CC (Char_Literal_Value (Expr)));
1111 Next (Expr);
1112 end loop;
1114 Rewrite (N, Make_String_Literal (Loc, End_String));
1116 Analyze_And_Resolve (N, Typ);
1117 return;
1118 end if;
1119 end;
1120 end if;
1122 -- Here if we have a real aggregate to deal with
1124 Array_Aggregate : declare
1125 Aggr_Resolved : Boolean;
1126 Aggr_Typ : constant Entity_Id := Etype (Typ);
1127 -- This is the unconstrained array type, which is the type against
1128 -- which the aggregate is to be resolved. Typ itself is the array
1129 -- type of the context which may not be the same subtype as the
1130 -- subtype for the final aggregate.
1132 Is_Null_Aggr : constant Boolean := Is_Null_Aggregate (N);
1134 begin
1135 -- In the following we determine whether an OTHERS choice is
1136 -- allowed inside the array aggregate. The test checks the context
1137 -- in which the array aggregate occurs. If the context does not
1138 -- permit it, or the aggregate type is unconstrained, an OTHERS
1139 -- choice is not allowed (except that it is always allowed on the
1140 -- right-hand side of an assignment statement; in this case the
1141 -- constrainedness of the type doesn't matter, because an array
1142 -- object is always constrained).
1144 -- If expansion is disabled (generic context, or semantics-only
1145 -- mode) actual subtypes cannot be constructed, and the type of an
1146 -- object may be its unconstrained nominal type. However, if the
1147 -- context is an assignment statement, OTHERS is allowed, because
1148 -- the target of the assignment will have a constrained subtype
1149 -- when fully compiled. Ditto if the context is an initialization
1150 -- procedure where a component may have a predicate function that
1151 -- carries the base type.
1153 -- Note that there is no node for Explicit_Actual_Parameter.
1154 -- To test for this context we therefore have to test for node
1155 -- N_Parameter_Association which itself appears only if there is a
1156 -- formal parameter. Consequently we also need to test for
1157 -- N_Procedure_Call_Statement or N_Function_Call.
1159 -- The context may be an N_Reference node, created by expansion.
1160 -- Legality of the others clause was established in the source,
1161 -- so the context is legal.
1163 Set_Etype (N, Aggr_Typ); -- May be overridden later on
1165 if Is_Null_Aggr then
1166 Set_Etype (N, Typ);
1167 Aggr_Resolved := Resolve_Null_Array_Aggregate (N);
1169 elsif Nkind (Parent (N)) = N_Assignment_Statement
1170 or else Inside_Init_Proc
1171 or else (Is_Constrained (Typ)
1172 and then Nkind (Parent (N)) in
1173 N_Parameter_Association
1174 | N_Function_Call
1175 | N_Procedure_Call_Statement
1176 | N_Generic_Association
1177 | N_Formal_Object_Declaration
1178 | N_Simple_Return_Statement
1179 | N_Object_Declaration
1180 | N_Component_Declaration
1181 | N_Parameter_Specification
1182 | N_Qualified_Expression
1183 | N_Unchecked_Type_Conversion
1184 | N_Reference
1185 | N_Aggregate
1186 | N_Extension_Aggregate
1187 | N_Component_Association
1188 | N_Case_Expression_Alternative
1189 | N_If_Expression
1190 | N_Expression_With_Actions)
1191 then
1192 Aggr_Resolved :=
1193 Resolve_Array_Aggregate
1195 Index => First_Index (Aggr_Typ),
1196 Index_Constr => First_Index (Typ),
1197 Component_Typ => Component_Type (Typ),
1198 Others_Allowed => True);
1199 else
1200 Aggr_Resolved :=
1201 Resolve_Array_Aggregate
1203 Index => First_Index (Aggr_Typ),
1204 Index_Constr => First_Index (Aggr_Typ),
1205 Component_Typ => Component_Type (Typ),
1206 Others_Allowed => False);
1207 end if;
1209 if not Aggr_Resolved then
1211 -- A parenthesized expression may have been intended as an
1212 -- aggregate, leading to a type error when analyzing the
1213 -- component. This can also happen for a nested component
1214 -- (see Analyze_Aggr_Expr).
1216 if Paren_Count (N) > 0 then
1217 Error_Msg_N
1218 ("positional aggregate cannot have one component", N);
1219 end if;
1221 Aggr_Subtyp := Any_Composite;
1223 else
1224 Aggr_Subtyp := Array_Aggr_Subtype (N, Typ);
1225 end if;
1227 Set_Etype (N, Aggr_Subtyp);
1228 end Array_Aggregate;
1230 elsif Is_Private_Type (Typ)
1231 and then Present (Full_View (Typ))
1232 and then (In_Inlined_Body or In_Instance_Body)
1233 and then Is_Composite_Type (Full_View (Typ))
1234 then
1235 Resolve (N, Full_View (Typ));
1237 else
1238 Error_Msg_N ("illegal context for aggregate", N);
1239 end if;
1241 -- If we can determine statically that the evaluation of the aggregate
1242 -- raises Constraint_Error, then replace the aggregate with an
1243 -- N_Raise_Constraint_Error node, but set the Etype to the right
1244 -- aggregate subtype. Gigi needs this.
1246 if Raises_Constraint_Error (N) then
1247 Aggr_Subtyp := Etype (N);
1248 Rewrite (N,
1249 Make_Raise_Constraint_Error (Loc, Reason => CE_Range_Check_Failed));
1250 Set_Raises_Constraint_Error (N);
1251 Set_Etype (N, Aggr_Subtyp);
1252 Set_Analyzed (N);
1253 end if;
1255 if Warn_On_No_Value_Assigned
1256 and then Others_Box
1257 and then not Is_Fully_Initialized_Type (Etype (N))
1258 then
1259 Error_Msg_N ("?v?aggregate not fully initialized", N);
1260 end if;
1262 Check_Function_Writable_Actuals (N);
1263 end Resolve_Aggregate;
1265 -----------------------------
1266 -- Resolve_Array_Aggregate --
1267 -----------------------------
1269 function Resolve_Array_Aggregate
1270 (N : Node_Id;
1271 Index : Node_Id;
1272 Index_Constr : Node_Id;
1273 Component_Typ : Entity_Id;
1274 Others_Allowed : Boolean) return Boolean
1276 Loc : constant Source_Ptr := Sloc (N);
1278 Failure : constant Boolean := False;
1279 Success : constant Boolean := True;
1281 Index_Typ : constant Entity_Id := Etype (Index);
1282 Index_Typ_Low : constant Node_Id := Type_Low_Bound (Index_Typ);
1283 Index_Typ_High : constant Node_Id := Type_High_Bound (Index_Typ);
1284 -- The type of the index corresponding to the array sub-aggregate along
1285 -- with its low and upper bounds.
1287 Index_Base : constant Entity_Id := Base_Type (Index_Typ);
1288 Index_Base_Low : constant Node_Id := Type_Low_Bound (Index_Base);
1289 Index_Base_High : constant Node_Id := Type_High_Bound (Index_Base);
1290 -- Ditto for the base type
1292 Others_Present : Boolean := False;
1294 Nb_Choices : Nat := 0;
1295 -- Contains the overall number of named choices in this sub-aggregate
1297 function Add (Val : Uint; To : Node_Id) return Node_Id;
1298 -- Creates a new expression node where Val is added to expression To.
1299 -- Tries to constant fold whenever possible. To must be an already
1300 -- analyzed expression.
1302 procedure Check_Bound (BH : Node_Id; AH : in out Node_Id);
1303 -- Checks that AH (the upper bound of an array aggregate) is less than
1304 -- or equal to BH (the upper bound of the index base type). If the check
1305 -- fails, a warning is emitted, the Raises_Constraint_Error flag of N is
1306 -- set, and AH is replaced with a duplicate of BH.
1308 procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id);
1309 -- Checks that range AL .. AH is compatible with range L .. H. Emits a
1310 -- warning if not and sets the Raises_Constraint_Error flag in N.
1312 procedure Check_Length (L, H : Node_Id; Len : Uint);
1313 -- Checks that range L .. H contains at least Len elements. Emits a
1314 -- warning if not and sets the Raises_Constraint_Error flag in N.
1316 function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean;
1317 -- Returns True if range L .. H is dynamic or null
1319 procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean);
1320 -- Given expression node From, this routine sets OK to False if it
1321 -- cannot statically evaluate From. Otherwise it stores this static
1322 -- value into Value.
1324 function Resolve_Aggr_Expr
1325 (Expr : Node_Id;
1326 Single_Elmt : Boolean) return Boolean;
1327 -- Resolves aggregate expression Expr. Returns False if resolution
1328 -- fails. If Single_Elmt is set to False, the expression Expr may be
1329 -- used to initialize several array aggregate elements (this can happen
1330 -- for discrete choices such as "L .. H => Expr" or the OTHERS choice).
1331 -- In this event we do not resolve Expr unless expansion is disabled.
1332 -- To know why, see the DELAYED COMPONENT RESOLUTION note above.
1334 -- NOTE: In the case of "... => <>", we pass the in the
1335 -- N_Component_Association node as Expr, since there is no Expression in
1336 -- that case, and we need a Sloc for the error message.
1338 procedure Resolve_Iterated_Component_Association
1339 (N : Node_Id;
1340 Index_Typ : Entity_Id);
1341 -- For AI12-061
1343 ---------
1344 -- Add --
1345 ---------
1347 function Add (Val : Uint; To : Node_Id) return Node_Id is
1348 Expr_Pos : Node_Id;
1349 Expr : Node_Id;
1350 To_Pos : Node_Id;
1352 begin
1353 if Raises_Constraint_Error (To) then
1354 return To;
1355 end if;
1357 -- First test if we can do constant folding
1359 if Compile_Time_Known_Value (To)
1360 or else Nkind (To) = N_Integer_Literal
1361 then
1362 Expr_Pos := Make_Integer_Literal (Loc, Expr_Value (To) + Val);
1363 Set_Is_Static_Expression (Expr_Pos);
1364 Set_Etype (Expr_Pos, Etype (To));
1365 Set_Analyzed (Expr_Pos, Analyzed (To));
1367 if not Is_Enumeration_Type (Index_Typ) then
1368 Expr := Expr_Pos;
1370 -- If we are dealing with enumeration return
1371 -- Index_Typ'Val (Expr_Pos)
1373 else
1374 Expr :=
1375 Make_Attribute_Reference
1376 (Loc,
1377 Prefix => New_Occurrence_Of (Index_Typ, Loc),
1378 Attribute_Name => Name_Val,
1379 Expressions => New_List (Expr_Pos));
1380 end if;
1382 return Expr;
1383 end if;
1385 -- If we are here no constant folding possible
1387 if not Is_Enumeration_Type (Index_Base) then
1388 Expr :=
1389 Make_Op_Add (Loc,
1390 Left_Opnd => Duplicate_Subexpr (To),
1391 Right_Opnd => Make_Integer_Literal (Loc, Val));
1393 -- If we are dealing with enumeration return
1394 -- Index_Typ'Val (Index_Typ'Pos (To) + Val)
1396 else
1397 To_Pos :=
1398 Make_Attribute_Reference
1399 (Loc,
1400 Prefix => New_Occurrence_Of (Index_Typ, Loc),
1401 Attribute_Name => Name_Pos,
1402 Expressions => New_List (Duplicate_Subexpr (To)));
1404 Expr_Pos :=
1405 Make_Op_Add (Loc,
1406 Left_Opnd => To_Pos,
1407 Right_Opnd => Make_Integer_Literal (Loc, Val));
1409 Expr :=
1410 Make_Attribute_Reference
1411 (Loc,
1412 Prefix => New_Occurrence_Of (Index_Typ, Loc),
1413 Attribute_Name => Name_Val,
1414 Expressions => New_List (Expr_Pos));
1416 -- If the index type has a non standard representation, the
1417 -- attributes 'Val and 'Pos expand into function calls and the
1418 -- resulting expression is considered non-safe for reevaluation
1419 -- by the backend. Relocate it into a constant temporary in order
1420 -- to make it safe for reevaluation.
1422 if Has_Non_Standard_Rep (Etype (N)) then
1423 declare
1424 Def_Id : Entity_Id;
1426 begin
1427 Def_Id := Make_Temporary (Loc, 'R', Expr);
1428 Set_Etype (Def_Id, Index_Typ);
1429 Insert_Action (N,
1430 Make_Object_Declaration (Loc,
1431 Defining_Identifier => Def_Id,
1432 Object_Definition =>
1433 New_Occurrence_Of (Index_Typ, Loc),
1434 Constant_Present => True,
1435 Expression => Relocate_Node (Expr)));
1437 Expr := New_Occurrence_Of (Def_Id, Loc);
1438 end;
1439 end if;
1440 end if;
1442 return Expr;
1443 end Add;
1445 -----------------
1446 -- Check_Bound --
1447 -----------------
1449 procedure Check_Bound (BH : Node_Id; AH : in out Node_Id) is
1450 Val_BH : Uint;
1451 Val_AH : Uint;
1453 OK_BH : Boolean;
1454 OK_AH : Boolean;
1456 begin
1457 Get (Value => Val_BH, From => BH, OK => OK_BH);
1458 Get (Value => Val_AH, From => AH, OK => OK_AH);
1460 if OK_BH and then OK_AH and then Val_BH < Val_AH then
1461 Set_Raises_Constraint_Error (N);
1462 Error_Msg_Warn := SPARK_Mode /= On;
1463 Error_Msg_N ("upper bound out of range<<", AH);
1464 Error_Msg_N ("\Constraint_Error [<<", AH);
1466 -- You need to set AH to BH or else in the case of enumerations
1467 -- indexes we will not be able to resolve the aggregate bounds.
1469 AH := Duplicate_Subexpr (BH);
1470 end if;
1471 end Check_Bound;
1473 ------------------
1474 -- Check_Bounds --
1475 ------------------
1477 procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id) is
1478 Val_L : Uint;
1479 Val_H : Uint;
1480 Val_AL : Uint;
1481 Val_AH : Uint;
1483 OK_L : Boolean;
1484 OK_H : Boolean;
1486 OK_AL : Boolean;
1487 OK_AH : Boolean;
1488 pragma Warnings (Off, OK_AL);
1489 pragma Warnings (Off, OK_AH);
1491 begin
1492 if Raises_Constraint_Error (N)
1493 or else Dynamic_Or_Null_Range (AL, AH)
1494 then
1495 return;
1496 end if;
1498 Get (Value => Val_L, From => L, OK => OK_L);
1499 Get (Value => Val_H, From => H, OK => OK_H);
1501 Get (Value => Val_AL, From => AL, OK => OK_AL);
1502 Get (Value => Val_AH, From => AH, OK => OK_AH);
1504 if OK_L and then Val_L > Val_AL then
1505 Set_Raises_Constraint_Error (N);
1506 Error_Msg_Warn := SPARK_Mode /= On;
1507 Error_Msg_N ("lower bound of aggregate out of range<<", N);
1508 Error_Msg_N ("\Constraint_Error [<<", N);
1509 end if;
1511 if OK_H and then Val_H < Val_AH then
1512 Set_Raises_Constraint_Error (N);
1513 Error_Msg_Warn := SPARK_Mode /= On;
1514 Error_Msg_N ("upper bound of aggregate out of range<<", N);
1515 Error_Msg_N ("\Constraint_Error [<<", N);
1516 end if;
1517 end Check_Bounds;
1519 ------------------
1520 -- Check_Length --
1521 ------------------
1523 procedure Check_Length (L, H : Node_Id; Len : Uint) is
1524 Val_L : Uint;
1525 Val_H : Uint;
1527 OK_L : Boolean;
1528 OK_H : Boolean;
1530 Range_Len : Uint;
1532 begin
1533 if Raises_Constraint_Error (N) then
1534 return;
1535 end if;
1537 Get (Value => Val_L, From => L, OK => OK_L);
1538 Get (Value => Val_H, From => H, OK => OK_H);
1540 if not OK_L or else not OK_H then
1541 return;
1542 end if;
1544 -- If null range length is zero
1546 if Val_L > Val_H then
1547 Range_Len := Uint_0;
1548 else
1549 Range_Len := Val_H - Val_L + 1;
1550 end if;
1552 if Range_Len < Len then
1553 Set_Raises_Constraint_Error (N);
1554 Error_Msg_Warn := SPARK_Mode /= On;
1555 Error_Msg_N ("too many elements<<", N);
1556 Error_Msg_N ("\Constraint_Error [<<", N);
1557 end if;
1558 end Check_Length;
1560 ---------------------------
1561 -- Dynamic_Or_Null_Range --
1562 ---------------------------
1564 function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean is
1565 Val_L : Uint;
1566 Val_H : Uint;
1568 OK_L : Boolean;
1569 OK_H : Boolean;
1571 begin
1572 Get (Value => Val_L, From => L, OK => OK_L);
1573 Get (Value => Val_H, From => H, OK => OK_H);
1575 return not OK_L or else not OK_H
1576 or else not Is_OK_Static_Expression (L)
1577 or else not Is_OK_Static_Expression (H)
1578 or else Val_L > Val_H;
1579 end Dynamic_Or_Null_Range;
1581 ---------
1582 -- Get --
1583 ---------
1585 procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean) is
1586 begin
1587 OK := True;
1589 if Compile_Time_Known_Value (From) then
1590 Value := Expr_Value (From);
1592 -- If expression From is something like Some_Type'Val (10) then
1593 -- Value = 10.
1595 elsif Nkind (From) = N_Attribute_Reference
1596 and then Attribute_Name (From) = Name_Val
1597 and then Compile_Time_Known_Value (First (Expressions (From)))
1598 then
1599 Value := Expr_Value (First (Expressions (From)));
1600 else
1601 Value := Uint_0;
1602 OK := False;
1603 end if;
1604 end Get;
1606 -----------------------
1607 -- Resolve_Aggr_Expr --
1608 -----------------------
1610 function Resolve_Aggr_Expr
1611 (Expr : Node_Id;
1612 Single_Elmt : Boolean) return Boolean
1614 Nxt_Ind : constant Node_Id := Next_Index (Index);
1615 Nxt_Ind_Constr : constant Node_Id := Next_Index (Index_Constr);
1616 -- Index is the current index corresponding to the expression
1618 Resolution_OK : Boolean := True;
1619 -- Set to False if resolution of the expression failed
1621 begin
1622 -- Defend against previous errors
1624 if Nkind (Expr) = N_Error
1625 or else Error_Posted (Expr)
1626 then
1627 return True;
1628 end if;
1630 -- If the array type against which we are resolving the aggregate
1631 -- has several dimensions, the expressions nested inside the
1632 -- aggregate must be further aggregates (or strings).
1634 if Present (Nxt_Ind) then
1635 if Nkind (Expr) /= N_Aggregate then
1637 -- A string literal can appear where a one-dimensional array
1638 -- of characters is expected. If the literal looks like an
1639 -- operator, it is still an operator symbol, which will be
1640 -- transformed into a string when analyzed.
1642 if Is_Character_Type (Component_Typ)
1643 and then No (Next_Index (Nxt_Ind))
1644 and then Nkind (Expr) in N_String_Literal | N_Operator_Symbol
1645 then
1646 -- A string literal used in a multidimensional array
1647 -- aggregate in place of the final one-dimensional
1648 -- aggregate must not be enclosed in parentheses.
1650 if Paren_Count (Expr) /= 0 then
1651 Error_Msg_N ("no parenthesis allowed here", Expr);
1652 end if;
1654 Make_String_Into_Aggregate (Expr);
1656 else
1657 Error_Msg_N ("nested array aggregate expected", Expr);
1659 -- If the expression is parenthesized, this may be
1660 -- a missing component association for a 1-aggregate.
1662 if Paren_Count (Expr) > 0 then
1663 Error_Msg_N
1664 ("\if single-component aggregate is intended, "
1665 & "write e.g. (1 ='> ...)", Expr);
1666 end if;
1668 return Failure;
1669 end if;
1670 end if;
1672 -- If it's "... => <>", nothing to resolve
1674 if Nkind (Expr) = N_Component_Association then
1675 pragma Assert (Box_Present (Expr));
1676 return Success;
1677 end if;
1679 -- Ada 2005 (AI-231): Propagate the type to the nested aggregate.
1680 -- Required to check the null-exclusion attribute (if present).
1681 -- This value may be overridden later on.
1683 Set_Etype (Expr, Etype (N));
1685 Resolution_OK := Resolve_Array_Aggregate
1686 (Expr, Nxt_Ind, Nxt_Ind_Constr, Component_Typ, Others_Allowed);
1688 else
1689 -- If it's "... => <>", nothing to resolve
1691 if Nkind (Expr) = N_Component_Association then
1692 pragma Assert (Box_Present (Expr));
1693 return Success;
1694 end if;
1696 -- Do not resolve the expressions of discrete or others choices
1697 -- unless the expression covers a single component, or the
1698 -- expander is inactive.
1700 -- In SPARK mode, expressions that can perform side effects will
1701 -- be recognized by the gnat2why back-end, and the whole
1702 -- subprogram will be ignored. So semantic analysis can be
1703 -- performed safely.
1705 if Single_Elmt
1706 or else not Expander_Active
1707 or else In_Spec_Expression
1708 then
1709 Analyze_And_Resolve (Expr, Component_Typ);
1710 Check_Expr_OK_In_Limited_Aggregate (Expr);
1711 Check_Non_Static_Context (Expr);
1712 Aggregate_Constraint_Checks (Expr, Component_Typ);
1713 Check_Unset_Reference (Expr);
1714 end if;
1715 end if;
1717 -- If an aggregate component has a type with predicates, an explicit
1718 -- predicate check must be applied, as for an assignment statement,
1719 -- because the aggregate might not be expanded into individual
1720 -- component assignments. If the expression covers several components
1721 -- the analysis and the predicate check take place later.
1723 if Has_Predicates (Component_Typ)
1724 and then Analyzed (Expr)
1725 then
1726 Apply_Predicate_Check (Expr, Component_Typ);
1727 end if;
1729 if Raises_Constraint_Error (Expr)
1730 and then Nkind (Parent (Expr)) /= N_Component_Association
1731 then
1732 Set_Raises_Constraint_Error (N);
1733 end if;
1735 -- If the expression has been marked as requiring a range check,
1736 -- then generate it here. It's a bit odd to be generating such
1737 -- checks in the analyzer, but harmless since Generate_Range_Check
1738 -- does nothing (other than making sure Do_Range_Check is set) if
1739 -- the expander is not active.
1741 if Do_Range_Check (Expr) then
1742 Generate_Range_Check (Expr, Component_Typ, CE_Range_Check_Failed);
1743 end if;
1745 return Resolution_OK;
1746 end Resolve_Aggr_Expr;
1748 --------------------------------------------
1749 -- Resolve_Iterated_Component_Association --
1750 --------------------------------------------
1752 procedure Resolve_Iterated_Component_Association
1753 (N : Node_Id;
1754 Index_Typ : Entity_Id)
1756 Loc : constant Source_Ptr := Sloc (N);
1757 Id : constant Entity_Id := Defining_Identifier (N);
1759 -----------------------
1760 -- Remove_References --
1761 -----------------------
1763 function Remove_Reference (N : Node_Id) return Traverse_Result;
1764 -- Remove reference to the entity Id after analysis, so it can be
1765 -- properly reanalyzed after construct is expanded into a loop.
1767 function Remove_Reference (N : Node_Id) return Traverse_Result is
1768 begin
1769 if Nkind (N) = N_Identifier
1770 and then Present (Entity (N))
1771 and then Entity (N) = Id
1772 then
1773 Set_Entity (N, Empty);
1774 Set_Etype (N, Empty);
1775 end if;
1776 Set_Analyzed (N, False);
1777 return OK;
1778 end Remove_Reference;
1780 procedure Remove_References is new Traverse_Proc (Remove_Reference);
1782 -- Local variables
1784 Choice : Node_Id;
1785 Dummy : Boolean;
1786 Scop : Entity_Id;
1787 Expr : Node_Id;
1789 -- Start of processing for Resolve_Iterated_Component_Association
1791 begin
1792 Error_Msg_Ada_2022_Feature ("iterated component", Loc);
1794 -- Create a scope in which to introduce an index, to make it visible
1795 -- for the analysis of component expression.
1797 Scop := New_Internal_Entity (E_Loop, Current_Scope, Loc, 'L');
1798 Set_Etype (Scop, Standard_Void_Type);
1799 Set_Parent (Scop, Parent (N));
1800 Push_Scope (Scop);
1802 -- If there is iterator specification, then its preanalysis will make
1803 -- the index visible.
1805 if Present (Iterator_Specification (N)) then
1806 Preanalyze (Iterator_Specification (N));
1808 -- Otherwise, analyze discrete choices and make the index visible
1810 else
1811 -- Insert index name into current scope but don't decorate it yet,
1812 -- so that a premature usage of this name in discrete choices will
1813 -- be nicely diagnosed.
1815 Enter_Name (Id);
1817 Choice := First (Discrete_Choices (N));
1819 while Present (Choice) loop
1820 if Nkind (Choice) = N_Others_Choice then
1821 Others_Present := True;
1823 else
1824 Analyze (Choice);
1826 -- Choice can be a subtype name, a range, or an expression
1828 if Is_Entity_Name (Choice)
1829 and then Is_Type (Entity (Choice))
1830 and then
1831 Base_Type (Entity (Choice)) = Base_Type (Index_Typ)
1832 then
1833 null;
1835 else
1836 Analyze_And_Resolve (Choice, Index_Typ);
1837 end if;
1838 end if;
1840 Next (Choice);
1841 end loop;
1843 -- Decorate the index variable
1845 Set_Etype (Id, Index_Typ);
1846 Mutate_Ekind (Id, E_Variable);
1847 Set_Is_Not_Self_Hidden (Id);
1848 Set_Scope (Id, Scop);
1849 end if;
1851 -- Analyze expression without expansion, to verify legality.
1852 -- When generating code, we then remove references to the index
1853 -- variable, because the expression will be analyzed anew after
1854 -- rewritting as a loop with a new index variable; when not
1855 -- generating code we leave the analyzed expression as it is.
1857 Expr := Expression (N);
1859 Expander_Mode_Save_And_Set (False);
1860 Dummy := Resolve_Aggr_Expr (Expr, Single_Elmt => False);
1861 Expander_Mode_Restore;
1863 if Operating_Mode /= Check_Semantics then
1864 Remove_References (Expr);
1865 end if;
1867 -- An iterated_component_association may appear in a nested
1868 -- aggregate for a multidimensional structure: preserve the bounds
1869 -- computed for the expression, as well as the anonymous array
1870 -- type generated for it; both are needed during array expansion.
1872 if Nkind (Expr) = N_Aggregate then
1873 Set_Aggregate_Bounds (Expression (N), Aggregate_Bounds (Expr));
1874 Set_Etype (Expression (N), Etype (Expr));
1875 end if;
1877 End_Scope;
1878 end Resolve_Iterated_Component_Association;
1880 -- Local variables
1882 Assoc : Node_Id;
1883 Choice : Node_Id;
1884 Expr : Node_Id;
1885 Discard : Node_Id;
1887 Aggr_Low : Node_Id := Empty;
1888 Aggr_High : Node_Id := Empty;
1889 -- The actual low and high bounds of this sub-aggregate
1891 Case_Table_Size : Nat;
1892 -- Contains the size of the case table needed to sort aggregate choices
1894 Choices_Low : Node_Id := Empty;
1895 Choices_High : Node_Id := Empty;
1896 -- The lowest and highest discrete choices values for a named aggregate
1898 Delete_Choice : Boolean;
1899 -- Used when replacing a subtype choice with predicate by a list
1901 Has_Iterator_Specifications : Boolean := False;
1902 -- Flag to indicate that all named associations are iterated component
1903 -- associations with iterator specifications, in which case the
1904 -- expansion will create two loops: one to evaluate the size and one
1905 -- to generate the elements (4.3.3 (20.2/5)).
1907 Nb_Elements : Uint := Uint_0;
1908 -- The number of elements in a positional aggregate
1910 Nb_Discrete_Choices : Nat := 0;
1911 -- The overall number of discrete choices (not counting others choice)
1913 -- Start of processing for Resolve_Array_Aggregate
1915 begin
1916 -- Ignore junk empty aggregate resulting from parser error
1918 if No (Expressions (N))
1919 and then No (Component_Associations (N))
1920 and then not Null_Record_Present (N)
1921 then
1922 return Failure;
1923 end if;
1925 -- Disable the warning for GNAT Mode to allow for easier transition.
1927 if Ada_Version_Explicit >= Ada_2022
1928 and then Warn_On_Obsolescent_Feature
1929 and then not GNAT_Mode
1930 and then not Is_Homogeneous_Aggregate (N)
1931 and then not Is_Enum_Array_Aggregate (N)
1932 and then Is_Parenthesis_Aggregate (N)
1933 and then Nkind (Parent (N)) /= N_Qualified_Expression
1934 and then Comes_From_Source (N)
1935 then
1936 Error_Msg_N
1937 ("?j?array aggregate using () is an" &
1938 " obsolescent syntax, use '['] instead", N);
1939 end if;
1941 -- STEP 1: make sure the aggregate is correctly formatted
1943 if Present (Component_Associations (N)) then
1945 -- Verify that all or none of the component associations
1946 -- include an iterator specification.
1948 Assoc := First (Component_Associations (N));
1949 if Nkind (Assoc) = N_Iterated_Component_Association
1950 and then Present (Iterator_Specification (Assoc))
1951 then
1952 -- All other component associations must have an iterator spec.
1954 Next (Assoc);
1955 while Present (Assoc) loop
1956 if Nkind (Assoc) /= N_Iterated_Component_Association
1957 or else No (Iterator_Specification (Assoc))
1958 then
1959 Error_Msg_N ("mixed iterated component association"
1960 & " (RM 4.3.3 (17.1/5))",
1961 Assoc);
1962 return Failure;
1963 end if;
1965 Next (Assoc);
1966 end loop;
1968 Has_Iterator_Specifications := True;
1970 else
1971 -- or none of them do.
1973 Next (Assoc);
1974 while Present (Assoc) loop
1975 if Nkind (Assoc) = N_Iterated_Component_Association
1976 and then Present (Iterator_Specification (Assoc))
1977 then
1978 Error_Msg_N ("mixed iterated component association"
1979 & " (RM 4.3.3 (17.1/5))",
1980 Assoc);
1981 return Failure;
1982 end if;
1984 Next (Assoc);
1985 end loop;
1987 end if;
1989 Assoc := First (Component_Associations (N));
1990 while Present (Assoc) loop
1991 if Nkind (Assoc) = N_Iterated_Component_Association then
1992 Resolve_Iterated_Component_Association (Assoc, Index_Typ);
1994 elsif Nkind (Assoc) /= N_Component_Association then
1995 Error_Msg_N
1996 ("invalid component association for aggregate", Assoc);
1997 return Failure;
1998 end if;
2000 Choice := First (Choice_List (Assoc));
2001 Delete_Choice := False;
2002 while Present (Choice) loop
2003 if Nkind (Choice) = N_Others_Choice then
2004 Others_Present := True;
2006 if Choice /= First (Choice_List (Assoc))
2007 or else Present (Next (Choice))
2008 then
2009 Error_Msg_N
2010 ("OTHERS must appear alone in a choice list", Choice);
2011 return Failure;
2012 end if;
2014 if Present (Next (Assoc)) then
2015 Error_Msg_N
2016 ("OTHERS must appear last in an aggregate", Choice);
2017 return Failure;
2018 end if;
2020 if Ada_Version = Ada_83
2021 and then Assoc /= First (Component_Associations (N))
2022 and then Nkind (Parent (N)) in
2023 N_Assignment_Statement | N_Object_Declaration
2024 then
2025 Error_Msg_N
2026 ("(Ada 83) illegal context for OTHERS choice", N);
2027 end if;
2029 elsif Is_Entity_Name (Choice) then
2030 Analyze (Choice);
2032 declare
2033 E : constant Entity_Id := Entity (Choice);
2034 New_Cs : List_Id;
2035 P : Node_Id;
2036 C : Node_Id;
2038 begin
2039 if Is_Type (E) and then Has_Predicates (E) then
2040 Freeze_Before (N, E);
2042 if Has_Dynamic_Predicate_Aspect (E) then
2043 Error_Msg_NE
2044 ("subtype& has dynamic predicate, not allowed "
2045 & "in aggregate choice", Choice, E);
2047 elsif not Is_OK_Static_Subtype (E) then
2048 Error_Msg_NE
2049 ("non-static subtype& has predicate, not allowed "
2050 & "in aggregate choice", Choice, E);
2051 end if;
2053 -- If the subtype has a static predicate, replace the
2054 -- original choice with the list of individual values
2055 -- covered by the predicate.
2056 -- This should be deferred to expansion time ???
2058 if Present (Static_Discrete_Predicate (E)) then
2059 Delete_Choice := True;
2061 New_Cs := New_List;
2062 P := First (Static_Discrete_Predicate (E));
2063 while Present (P) loop
2064 C := New_Copy (P);
2065 Set_Sloc (C, Sloc (Choice));
2066 Append_To (New_Cs, C);
2067 Next (P);
2068 end loop;
2070 Insert_List_After (Choice, New_Cs);
2071 end if;
2072 end if;
2073 end;
2074 end if;
2076 Nb_Choices := Nb_Choices + 1;
2078 declare
2079 C : constant Node_Id := Choice;
2081 begin
2082 Next (Choice);
2084 if Delete_Choice then
2085 Remove (C);
2086 Nb_Choices := Nb_Choices - 1;
2087 Delete_Choice := False;
2088 end if;
2089 end;
2090 end loop;
2092 Next (Assoc);
2093 end loop;
2094 end if;
2096 -- At this point we know that the others choice, if present, is by
2097 -- itself and appears last in the aggregate. Check if we have mixed
2098 -- positional and discrete associations (other than the others choice).
2100 if Present (Expressions (N))
2101 and then (Nb_Choices > 1
2102 or else (Nb_Choices = 1 and then not Others_Present))
2103 then
2104 Error_Msg_N
2105 ("cannot mix named and positional associations in array aggregate",
2106 First (Choice_List (First (Component_Associations (N)))));
2107 return Failure;
2108 end if;
2110 -- Test for the validity of an others choice if present
2112 if Others_Present and then not Others_Allowed then
2113 declare
2114 Others_N : constant Node_Id :=
2115 First (Choice_List (First (Component_Associations (N))));
2116 begin
2117 Error_Msg_N ("OTHERS choice not allowed here", Others_N);
2118 Error_Msg_N ("\qualify the aggregate with a constrained subtype "
2119 & "to provide bounds for it", Others_N);
2120 return Failure;
2121 end;
2122 end if;
2124 -- Protect against cascaded errors
2126 if Etype (Index_Typ) = Any_Type then
2127 return Failure;
2128 end if;
2130 -- STEP 2: Process named components
2132 if No (Expressions (N)) then
2133 if Others_Present then
2134 Case_Table_Size := Nb_Choices - 1;
2135 else
2136 Case_Table_Size := Nb_Choices;
2137 end if;
2139 Step_2 : declare
2140 function Empty_Range (A : Node_Id) return Boolean;
2141 -- If an association covers an empty range, some warnings on the
2142 -- expression of the association can be disabled.
2144 -----------------
2145 -- Empty_Range --
2146 -----------------
2148 function Empty_Range (A : Node_Id) return Boolean is
2149 R : constant Node_Id := First (Choices (A));
2150 begin
2151 return No (Next (R))
2152 and then Nkind (R) = N_Range
2153 and then Compile_Time_Compare
2154 (Low_Bound (R), High_Bound (R), False) = GT;
2155 end Empty_Range;
2157 -- Local variables
2159 Low : Node_Id;
2160 High : Node_Id;
2161 -- Denote the lowest and highest values in an aggregate choice
2163 S_Low : Node_Id := Empty;
2164 S_High : Node_Id := Empty;
2165 -- if a choice in an aggregate is a subtype indication these
2166 -- denote the lowest and highest values of the subtype
2168 Table : Case_Table_Type (1 .. Case_Table_Size);
2169 -- Used to sort all the different choice values
2171 Single_Choice : Boolean;
2172 -- Set to true every time there is a single discrete choice in a
2173 -- discrete association
2175 Prev_Nb_Discrete_Choices : Nat;
2176 -- Used to keep track of the number of discrete choices in the
2177 -- current association.
2179 Errors_Posted_On_Choices : Boolean := False;
2180 -- Keeps track of whether any choices have semantic errors
2182 -- Start of processing for Step_2
2184 begin
2185 -- STEP 2 (A): Check discrete choices validity
2186 -- No need if this is an element iteration.
2188 Assoc := First (Component_Associations (N));
2189 while Present (Assoc)
2190 and then Present (Choice_List (Assoc))
2191 loop
2192 Prev_Nb_Discrete_Choices := Nb_Discrete_Choices;
2193 Choice := First (Choice_List (Assoc));
2195 loop
2196 Analyze (Choice);
2198 if Nkind (Choice) = N_Others_Choice then
2199 Single_Choice := False;
2200 exit;
2202 -- Test for subtype mark without constraint
2204 elsif Is_Entity_Name (Choice) and then
2205 Is_Type (Entity (Choice))
2206 then
2207 if Base_Type (Entity (Choice)) /= Index_Base then
2208 Error_Msg_N
2209 ("invalid subtype mark in aggregate choice",
2210 Choice);
2211 return Failure;
2212 end if;
2214 -- Case of subtype indication
2216 elsif Nkind (Choice) = N_Subtype_Indication then
2217 Resolve_Discrete_Subtype_Indication (Choice, Index_Base);
2219 if Has_Dynamic_Predicate_Aspect
2220 (Entity (Subtype_Mark (Choice)))
2221 then
2222 Error_Msg_NE
2223 ("subtype& has dynamic predicate, "
2224 & "not allowed in aggregate choice",
2225 Choice, Entity (Subtype_Mark (Choice)));
2226 end if;
2228 -- Does the subtype indication evaluation raise CE?
2230 Get_Index_Bounds (Subtype_Mark (Choice), S_Low, S_High);
2231 Get_Index_Bounds (Choice, Low, High);
2232 Check_Bounds (S_Low, S_High, Low, High);
2234 -- Case of range or expression
2236 else
2237 Resolve (Choice, Index_Base);
2238 Check_Unset_Reference (Choice);
2239 Check_Non_Static_Context (Choice);
2241 -- If semantic errors were posted on the choice, then
2242 -- record that for possible early return from later
2243 -- processing (see handling of enumeration choices).
2245 if Error_Posted (Choice) then
2246 Errors_Posted_On_Choices := True;
2247 end if;
2249 -- Do not range check a choice. This check is redundant
2250 -- since this test is already done when we check that the
2251 -- bounds of the array aggregate are within range.
2253 Set_Do_Range_Check (Choice, False);
2254 end if;
2256 -- If we could not resolve the discrete choice stop here
2258 if Etype (Choice) = Any_Type then
2259 return Failure;
2261 -- If the discrete choice raises CE get its original bounds
2263 elsif Nkind (Choice) = N_Raise_Constraint_Error then
2264 Set_Raises_Constraint_Error (N);
2265 Get_Index_Bounds (Original_Node (Choice), Low, High);
2267 -- Otherwise get its bounds as usual
2269 else
2270 Get_Index_Bounds (Choice, Low, High);
2271 end if;
2273 if (Dynamic_Or_Null_Range (Low, High)
2274 or else (Nkind (Choice) = N_Subtype_Indication
2275 and then
2276 Dynamic_Or_Null_Range (S_Low, S_High)))
2277 and then Nb_Choices /= 1
2278 then
2279 Error_Msg_N
2280 ("dynamic or empty choice in aggregate "
2281 & "must be the only choice", Choice);
2282 return Failure;
2283 end if;
2285 if not (All_Composite_Constraints_Static (Low)
2286 and then All_Composite_Constraints_Static (High)
2287 and then All_Composite_Constraints_Static (S_Low)
2288 and then All_Composite_Constraints_Static (S_High))
2289 then
2290 Check_Restriction (No_Dynamic_Sized_Objects, Choice);
2291 end if;
2293 Nb_Discrete_Choices := Nb_Discrete_Choices + 1;
2294 Table (Nb_Discrete_Choices).Lo := Low;
2295 Table (Nb_Discrete_Choices).Hi := High;
2296 Table (Nb_Discrete_Choices).Choice := Choice;
2298 Next (Choice);
2300 if No (Choice) then
2302 -- Check if we have a single discrete choice and whether
2303 -- this discrete choice specifies a single value.
2305 Single_Choice :=
2306 Nb_Discrete_Choices = Prev_Nb_Discrete_Choices + 1
2307 and then Low = High;
2309 exit;
2310 end if;
2311 end loop;
2313 -- Ada 2005 (AI-231)
2315 if Ada_Version >= Ada_2005
2316 and then Known_Null (Expression (Assoc))
2317 and then not Empty_Range (Assoc)
2318 then
2319 Check_Can_Never_Be_Null (Etype (N), Expression (Assoc));
2320 end if;
2322 -- Ada 2005 (AI-287): In case of default initialized component
2323 -- we delay the resolution to the expansion phase.
2325 if Box_Present (Assoc) then
2327 -- Ada 2005 (AI-287): In case of default initialization of a
2328 -- component the expander will generate calls to the
2329 -- corresponding initialization subprogram. We need to call
2330 -- Resolve_Aggr_Expr to check the rules about
2331 -- dimensionality.
2333 if not Resolve_Aggr_Expr
2334 (Assoc, Single_Elmt => Single_Choice)
2335 then
2336 return Failure;
2337 end if;
2339 -- ??? Checks for dynamically tagged expressions below will
2340 -- be only applied to iterated_component_association after
2341 -- expansion; in particular, errors might not be reported when
2342 -- -gnatc switch is used.
2344 elsif Nkind (Assoc) = N_Iterated_Component_Association then
2345 null; -- handled above, in a loop context
2347 elsif not Resolve_Aggr_Expr
2348 (Expression (Assoc), Single_Elmt => Single_Choice)
2349 then
2350 return Failure;
2352 -- Check incorrect use of dynamically tagged expression
2354 -- We differentiate here two cases because the expression may
2355 -- not be decorated. For example, the analysis and resolution
2356 -- of the expression associated with the others choice will be
2357 -- done later with the full aggregate. In such case we
2358 -- duplicate the expression tree to analyze the copy and
2359 -- perform the required check.
2361 elsif No (Etype (Expression (Assoc))) then
2362 declare
2363 Save_Analysis : constant Boolean := Full_Analysis;
2364 Expr : constant Node_Id :=
2365 New_Copy_Tree (Expression (Assoc));
2367 begin
2368 Expander_Mode_Save_And_Set (False);
2369 Full_Analysis := False;
2371 -- Analyze the expression, making sure it is properly
2372 -- attached to the tree before we do the analysis.
2374 Set_Parent (Expr, Parent (Expression (Assoc)));
2375 Analyze (Expr);
2377 -- Compute its dimensions now, rather than at the end of
2378 -- resolution, because in the case of multidimensional
2379 -- aggregates subsequent expansion may lead to spurious
2380 -- errors.
2382 Check_Expression_Dimensions (Expr, Component_Typ);
2384 -- If the expression is a literal, propagate this info
2385 -- to the expression in the association, to enable some
2386 -- optimizations downstream.
2388 if Is_Entity_Name (Expr)
2389 and then Present (Entity (Expr))
2390 and then Ekind (Entity (Expr)) = E_Enumeration_Literal
2391 then
2392 Analyze_And_Resolve
2393 (Expression (Assoc), Component_Typ);
2394 end if;
2396 Full_Analysis := Save_Analysis;
2397 Expander_Mode_Restore;
2399 if Is_Tagged_Type (Etype (Expr)) then
2400 Check_Dynamically_Tagged_Expression
2401 (Expr => Expr,
2402 Typ => Component_Type (Etype (N)),
2403 Related_Nod => N);
2404 end if;
2405 end;
2407 elsif Is_Tagged_Type (Etype (Expression (Assoc))) then
2408 Check_Dynamically_Tagged_Expression
2409 (Expr => Expression (Assoc),
2410 Typ => Component_Type (Etype (N)),
2411 Related_Nod => N);
2412 end if;
2414 Next (Assoc);
2415 end loop;
2417 -- If aggregate contains more than one choice then these must be
2418 -- static. Check for duplicate and missing values.
2420 -- Note: there is duplicated code here wrt Check_Choice_Set in
2421 -- the body of Sem_Case, and it is possible we could just reuse
2422 -- that procedure. To be checked ???
2424 if Nb_Discrete_Choices > 1 then
2425 Check_Choices : declare
2426 Choice : Node_Id;
2427 -- Location of choice for messages
2429 Hi_Val : Uint;
2430 Lo_Val : Uint;
2431 -- High end of one range and Low end of the next. Should be
2432 -- contiguous if there is no hole in the list of values.
2434 Lo_Dup : Uint;
2435 Hi_Dup : Uint;
2436 -- End points of duplicated range
2438 Missing_Or_Duplicates : Boolean := False;
2439 -- Set True if missing or duplicate choices found
2441 procedure Output_Bad_Choices (Lo, Hi : Uint; C : Node_Id);
2442 -- Output continuation message with a representation of the
2443 -- bounds (just Lo if Lo = Hi, else Lo .. Hi). C is the
2444 -- choice node where the message is to be posted.
2446 ------------------------
2447 -- Output_Bad_Choices --
2448 ------------------------
2450 procedure Output_Bad_Choices (Lo, Hi : Uint; C : Node_Id) is
2451 begin
2452 -- Enumeration type case
2454 if Is_Enumeration_Type (Index_Typ) then
2455 Error_Msg_Name_1 :=
2456 Chars (Get_Enum_Lit_From_Pos (Index_Typ, Lo, Loc));
2457 Error_Msg_Name_2 :=
2458 Chars (Get_Enum_Lit_From_Pos (Index_Typ, Hi, Loc));
2460 if Lo = Hi then
2461 Error_Msg_N ("\\ %!", C);
2462 else
2463 Error_Msg_N ("\\ % .. %!", C);
2464 end if;
2466 -- Integer types case
2468 else
2469 Error_Msg_Uint_1 := Lo;
2470 Error_Msg_Uint_2 := Hi;
2472 if Lo = Hi then
2473 Error_Msg_N ("\\ ^!", C);
2474 else
2475 Error_Msg_N ("\\ ^ .. ^!", C);
2476 end if;
2477 end if;
2478 end Output_Bad_Choices;
2480 -- Start of processing for Check_Choices
2482 begin
2483 Sort_Case_Table (Table);
2485 -- First we do a quick linear loop to find out if we have
2486 -- any duplicates or missing entries (usually we have a
2487 -- legal aggregate, so this will get us out quickly).
2489 for J in 1 .. Nb_Discrete_Choices - 1 loop
2490 Hi_Val := Expr_Value (Table (J).Hi);
2491 Lo_Val := Expr_Value (Table (J + 1).Lo);
2493 if Lo_Val <= Hi_Val
2494 or else (Lo_Val > Hi_Val + 1
2495 and then not Others_Present)
2496 then
2497 Missing_Or_Duplicates := True;
2498 exit;
2499 end if;
2500 end loop;
2502 -- If we have missing or duplicate entries, first fill in
2503 -- the Highest entries to make life easier in the following
2504 -- loops to detect bad entries.
2506 if Missing_Or_Duplicates then
2507 Table (1).Highest := Expr_Value (Table (1).Hi);
2509 for J in 2 .. Nb_Discrete_Choices loop
2510 Table (J).Highest :=
2511 UI_Max
2512 (Table (J - 1).Highest, Expr_Value (Table (J).Hi));
2513 end loop;
2515 -- Loop through table entries to find duplicate indexes
2517 for J in 2 .. Nb_Discrete_Choices loop
2518 Lo_Val := Expr_Value (Table (J).Lo);
2519 Hi_Val := Expr_Value (Table (J).Hi);
2521 -- Case where we have duplicates (the lower bound of
2522 -- this choice is less than or equal to the highest
2523 -- high bound found so far).
2525 if Lo_Val <= Table (J - 1).Highest then
2527 -- We move backwards looking for duplicates. We can
2528 -- abandon this loop as soon as we reach a choice
2529 -- highest value that is less than Lo_Val.
2531 for K in reverse 1 .. J - 1 loop
2532 exit when Table (K).Highest < Lo_Val;
2534 -- Here we may have duplicates between entries
2535 -- for K and J. Get range of duplicates.
2537 Lo_Dup :=
2538 UI_Max (Lo_Val, Expr_Value (Table (K).Lo));
2539 Hi_Dup :=
2540 UI_Min (Hi_Val, Expr_Value (Table (K).Hi));
2542 -- Nothing to do if duplicate range is null
2544 if Lo_Dup > Hi_Dup then
2545 null;
2547 -- Otherwise place proper message
2549 else
2550 -- We place message on later choice, with a
2551 -- line reference to the earlier choice.
2553 if Sloc (Table (J).Choice) <
2554 Sloc (Table (K).Choice)
2555 then
2556 Choice := Table (K).Choice;
2557 Error_Msg_Sloc := Sloc (Table (J).Choice);
2558 else
2559 Choice := Table (J).Choice;
2560 Error_Msg_Sloc := Sloc (Table (K).Choice);
2561 end if;
2563 if Lo_Dup = Hi_Dup then
2564 Error_Msg_N
2565 ("index value in array aggregate "
2566 & "duplicates the one given#!", Choice);
2567 else
2568 Error_Msg_N
2569 ("index values in array aggregate "
2570 & "duplicate those given#!", Choice);
2571 end if;
2573 Output_Bad_Choices (Lo_Dup, Hi_Dup, Choice);
2574 end if;
2575 end loop;
2576 end if;
2577 end loop;
2579 -- Loop through entries in table to find missing indexes.
2580 -- Not needed if others, since missing impossible.
2582 if not Others_Present then
2583 for J in 2 .. Nb_Discrete_Choices loop
2584 Lo_Val := Expr_Value (Table (J).Lo);
2585 Hi_Val := Table (J - 1).Highest;
2587 if Lo_Val > Hi_Val + 1 then
2589 declare
2590 Error_Node : Node_Id;
2592 begin
2593 -- If the choice is the bound of a range in
2594 -- a subtype indication, it is not in the
2595 -- source lists for the aggregate itself, so
2596 -- post the error on the aggregate. Otherwise
2597 -- post it on choice itself.
2599 Choice := Table (J).Choice;
2601 if Is_List_Member (Choice) then
2602 Error_Node := Choice;
2603 else
2604 Error_Node := N;
2605 end if;
2607 if Hi_Val + 1 = Lo_Val - 1 then
2608 Error_Msg_N
2609 ("missing index value "
2610 & "in array aggregate!", Error_Node);
2611 else
2612 Error_Msg_N
2613 ("missing index values "
2614 & "in array aggregate!", Error_Node);
2615 end if;
2617 Output_Bad_Choices
2618 (Hi_Val + 1, Lo_Val - 1, Error_Node);
2619 end;
2620 end if;
2621 end loop;
2622 end if;
2624 -- If either missing or duplicate values, return failure
2626 Set_Etype (N, Any_Composite);
2627 return Failure;
2628 end if;
2629 end Check_Choices;
2630 end if;
2632 if Has_Iterator_Specifications then
2633 -- Bounds will be determined dynamically.
2635 return Success;
2636 end if;
2638 -- STEP 2 (B): Compute aggregate bounds and min/max choices values
2640 if Nb_Discrete_Choices > 0 then
2641 Choices_Low := Table (1).Lo;
2642 Choices_High := Table (Nb_Discrete_Choices).Hi;
2643 end if;
2645 -- If Others is present, then bounds of aggregate come from the
2646 -- index constraint (not the choices in the aggregate itself).
2648 if Others_Present then
2649 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2651 -- Abandon processing if either bound is already signalled as
2652 -- an error (prevents junk cascaded messages and blow ups).
2654 if Nkind (Aggr_Low) = N_Error
2655 or else
2656 Nkind (Aggr_High) = N_Error
2657 then
2658 return False;
2659 end if;
2661 -- No others clause present
2663 else
2664 -- Special processing if others allowed and not present. This
2665 -- means that the bounds of the aggregate come from the index
2666 -- constraint (and the length must match).
2668 if Others_Allowed then
2669 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2671 -- Abandon processing if either bound is already signalled
2672 -- as an error (stop junk cascaded messages and blow ups).
2674 if Nkind (Aggr_Low) = N_Error
2675 or else
2676 Nkind (Aggr_High) = N_Error
2677 then
2678 return False;
2679 end if;
2681 -- If others allowed, and no others present, then the array
2682 -- should cover all index values. If it does not, we will
2683 -- get a length check warning, but there is two cases where
2684 -- an additional warning is useful:
2686 -- If we have no positional components, and the length is
2687 -- wrong (which we can tell by others being allowed with
2688 -- missing components), and the index type is an enumeration
2689 -- type, then issue appropriate warnings about these missing
2690 -- components. They are only warnings, since the aggregate
2691 -- is fine, it's just the wrong length. We skip this check
2692 -- for standard character types (since there are no literals
2693 -- and it is too much trouble to concoct them), and also if
2694 -- any of the bounds have values that are not known at
2695 -- compile time.
2697 -- Another case warranting a warning is when the length
2698 -- is right, but as above we have an index type that is
2699 -- an enumeration, and the bounds do not match. This is a
2700 -- case where dubious sliding is allowed and we generate a
2701 -- warning that the bounds do not match.
2703 if No (Expressions (N))
2704 and then Nkind (Index) = N_Range
2705 and then Is_Enumeration_Type (Etype (Index))
2706 and then not Is_Standard_Character_Type (Etype (Index))
2707 and then Compile_Time_Known_Value (Aggr_Low)
2708 and then Compile_Time_Known_Value (Aggr_High)
2709 and then Compile_Time_Known_Value (Choices_Low)
2710 and then Compile_Time_Known_Value (Choices_High)
2711 then
2712 -- If any of the expressions or range bounds in choices
2713 -- have semantic errors, then do not attempt further
2714 -- resolution, to prevent cascaded errors.
2716 if Errors_Posted_On_Choices then
2717 return Failure;
2718 end if;
2720 declare
2721 ALo : constant Node_Id := Expr_Value_E (Aggr_Low);
2722 AHi : constant Node_Id := Expr_Value_E (Aggr_High);
2723 CLo : constant Node_Id := Expr_Value_E (Choices_Low);
2724 CHi : constant Node_Id := Expr_Value_E (Choices_High);
2726 Ent : Entity_Id;
2728 begin
2729 -- Warning case 1, missing values at start/end. Only
2730 -- do the check if the number of entries is too small.
2732 if (Enumeration_Pos (CHi) - Enumeration_Pos (CLo))
2734 (Enumeration_Pos (AHi) - Enumeration_Pos (ALo))
2735 then
2736 Error_Msg_N
2737 ("missing index value(s) in array aggregate??",
2740 -- Output missing value(s) at start
2742 if Chars (ALo) /= Chars (CLo) then
2743 Ent := Prev (CLo);
2745 if Chars (ALo) = Chars (Ent) then
2746 Error_Msg_Name_1 := Chars (ALo);
2747 Error_Msg_N ("\ %??", N);
2748 else
2749 Error_Msg_Name_1 := Chars (ALo);
2750 Error_Msg_Name_2 := Chars (Ent);
2751 Error_Msg_N ("\ % .. %??", N);
2752 end if;
2753 end if;
2755 -- Output missing value(s) at end
2757 if Chars (AHi) /= Chars (CHi) then
2758 Ent := Next (CHi);
2760 if Chars (AHi) = Chars (Ent) then
2761 Error_Msg_Name_1 := Chars (Ent);
2762 Error_Msg_N ("\ %??", N);
2763 else
2764 Error_Msg_Name_1 := Chars (Ent);
2765 Error_Msg_Name_2 := Chars (AHi);
2766 Error_Msg_N ("\ % .. %??", N);
2767 end if;
2768 end if;
2770 -- Warning case 2, dubious sliding. The First_Subtype
2771 -- test distinguishes between a constrained type where
2772 -- sliding is not allowed (so we will get a warning
2773 -- later that Constraint_Error will be raised), and
2774 -- the unconstrained case where sliding is permitted.
2776 elsif (Enumeration_Pos (CHi) - Enumeration_Pos (CLo))
2778 (Enumeration_Pos (AHi) - Enumeration_Pos (ALo))
2779 and then Chars (ALo) /= Chars (CLo)
2780 and then
2781 not Is_Constrained (First_Subtype (Etype (N)))
2782 then
2783 Error_Msg_N
2784 ("bounds of aggregate do not match target??", N);
2785 end if;
2786 end;
2787 end if;
2788 end if;
2790 -- If no others, aggregate bounds come from aggregate
2792 Aggr_Low := Choices_Low;
2793 Aggr_High := Choices_High;
2794 end if;
2795 end Step_2;
2797 -- STEP 3: Process positional components
2799 else
2800 -- STEP 3 (A): Process positional elements
2802 Expr := First (Expressions (N));
2803 Nb_Elements := Uint_0;
2804 while Present (Expr) loop
2805 Nb_Elements := Nb_Elements + 1;
2807 -- Ada 2005 (AI-231)
2809 if Ada_Version >= Ada_2005 and then Known_Null (Expr) then
2810 Check_Can_Never_Be_Null (Etype (N), Expr);
2811 end if;
2813 if not Resolve_Aggr_Expr (Expr, Single_Elmt => True) then
2814 return Failure;
2815 end if;
2817 -- Check incorrect use of dynamically tagged expression
2819 if Is_Tagged_Type (Etype (Expr)) then
2820 Check_Dynamically_Tagged_Expression
2821 (Expr => Expr,
2822 Typ => Component_Type (Etype (N)),
2823 Related_Nod => N);
2824 end if;
2826 Next (Expr);
2827 end loop;
2829 if Others_Present then
2830 Assoc := Last (Component_Associations (N));
2832 -- Ada 2005 (AI-231)
2834 if Ada_Version >= Ada_2005 and then Known_Null (Assoc) then
2835 Check_Can_Never_Be_Null (Etype (N), Expression (Assoc));
2836 end if;
2838 -- Ada 2005 (AI-287): In case of default initialized component,
2839 -- we delay the resolution to the expansion phase.
2841 if Box_Present (Assoc) then
2843 -- Ada 2005 (AI-287): In case of default initialization of a
2844 -- component the expander will generate calls to the
2845 -- corresponding initialization subprogram. We need to call
2846 -- Resolve_Aggr_Expr to check the rules about
2847 -- dimensionality.
2849 if not Resolve_Aggr_Expr (Assoc, Single_Elmt => False) then
2850 return Failure;
2851 end if;
2853 elsif not Resolve_Aggr_Expr (Expression (Assoc),
2854 Single_Elmt => False)
2855 then
2856 return Failure;
2858 -- Check incorrect use of dynamically tagged expression. The
2859 -- expression of the others choice has not been resolved yet.
2860 -- In order to diagnose the semantic error we create a duplicate
2861 -- tree to analyze it and perform the check.
2863 elsif Nkind (Assoc) /= N_Iterated_Component_Association then
2864 declare
2865 Save_Analysis : constant Boolean := Full_Analysis;
2866 Expr : constant Node_Id :=
2867 New_Copy_Tree (Expression (Assoc));
2869 begin
2870 Expander_Mode_Save_And_Set (False);
2871 Full_Analysis := False;
2872 Analyze (Expr);
2873 Full_Analysis := Save_Analysis;
2874 Expander_Mode_Restore;
2876 if Is_Tagged_Type (Etype (Expr)) then
2877 Check_Dynamically_Tagged_Expression
2878 (Expr => Expr,
2879 Typ => Component_Type (Etype (N)),
2880 Related_Nod => N);
2881 end if;
2882 end;
2883 end if;
2884 end if;
2886 -- STEP 3 (B): Compute the aggregate bounds
2888 if Others_Present then
2889 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2891 else
2892 if Others_Allowed then
2893 Get_Index_Bounds (Index_Constr, Aggr_Low, Discard);
2894 else
2895 Aggr_Low := Index_Typ_Low;
2896 end if;
2898 Aggr_High := Add (Nb_Elements - 1, To => Aggr_Low);
2899 Check_Bound (Index_Base_High, Aggr_High);
2900 end if;
2901 end if;
2903 -- STEP 4: Perform static aggregate checks and save the bounds
2905 -- Check (A)
2907 Check_Bounds (Index_Typ_Low, Index_Typ_High, Aggr_Low, Aggr_High);
2908 Check_Bounds (Index_Base_Low, Index_Base_High, Aggr_Low, Aggr_High);
2910 -- Check (B)
2912 if Others_Present and then Nb_Discrete_Choices > 0 then
2913 Check_Bounds (Aggr_Low, Aggr_High, Choices_Low, Choices_High);
2914 Check_Bounds (Index_Typ_Low, Index_Typ_High,
2915 Choices_Low, Choices_High);
2916 Check_Bounds (Index_Base_Low, Index_Base_High,
2917 Choices_Low, Choices_High);
2919 -- Check (C)
2921 elsif Others_Present and then Nb_Elements > 0 then
2922 Check_Length (Aggr_Low, Aggr_High, Nb_Elements);
2923 Check_Length (Index_Typ_Low, Index_Typ_High, Nb_Elements);
2924 Check_Length (Index_Base_Low, Index_Base_High, Nb_Elements);
2925 end if;
2927 if Raises_Constraint_Error (Aggr_Low)
2928 or else Raises_Constraint_Error (Aggr_High)
2929 then
2930 Set_Raises_Constraint_Error (N);
2931 end if;
2933 Aggr_Low := Duplicate_Subexpr (Aggr_Low);
2935 -- Do not duplicate Aggr_High if Aggr_High = Aggr_Low + Nb_Elements
2936 -- since the addition node returned by Add is not yet analyzed. Attach
2937 -- to tree and analyze first. Reset analyzed flag to ensure it will get
2938 -- analyzed when it is a literal bound whose type must be properly set.
2940 if Others_Present or else Nb_Discrete_Choices > 0 then
2941 Aggr_High := Duplicate_Subexpr (Aggr_High);
2943 if Etype (Aggr_High) = Universal_Integer then
2944 Set_Analyzed (Aggr_High, False);
2945 end if;
2946 end if;
2948 -- If the aggregate already has bounds attached to it, it means this is
2949 -- a positional aggregate created as an optimization by
2950 -- Exp_Aggr.Convert_To_Positional, so we don't want to change those
2951 -- bounds.
2953 if Present (Aggregate_Bounds (N))
2954 and then not Others_Allowed
2955 and then not Comes_From_Source (N)
2956 then
2957 Aggr_Low := Low_Bound (Aggregate_Bounds (N));
2958 Aggr_High := High_Bound (Aggregate_Bounds (N));
2959 end if;
2961 Set_Aggregate_Bounds
2962 (N, Make_Range (Loc, Low_Bound => Aggr_Low, High_Bound => Aggr_High));
2964 -- The bounds may contain expressions that must be inserted upwards.
2965 -- Attach them fully to the tree. After analysis, remove side effects
2966 -- from upper bound, if still needed.
2968 Set_Parent (Aggregate_Bounds (N), N);
2969 Analyze_And_Resolve (Aggregate_Bounds (N), Index_Typ);
2970 Check_Unset_Reference (Aggregate_Bounds (N));
2972 if not Others_Present and then Nb_Discrete_Choices = 0 then
2973 Set_High_Bound
2974 (Aggregate_Bounds (N),
2975 Duplicate_Subexpr (High_Bound (Aggregate_Bounds (N))));
2976 end if;
2978 -- Check the dimensions of each component in the array aggregate
2980 Analyze_Dimension_Array_Aggregate (N, Component_Typ);
2982 return Success;
2983 end Resolve_Array_Aggregate;
2985 ---------------------------------
2986 -- Resolve_Container_Aggregate --
2987 ---------------------------------
2989 procedure Resolve_Container_Aggregate (N : Node_Id; Typ : Entity_Id) is
2990 procedure Resolve_Iterated_Association
2991 (Comp : Node_Id;
2992 Key_Type : Entity_Id;
2993 Elmt_Type : Entity_Id);
2994 -- Resolve choices and expression in an iterated component association
2995 -- or an iterated element association, which has a key_expression.
2996 -- This is similar but not identical to the handling of this construct
2997 -- in an array aggregate.
2998 -- For a named container, the type of each choice must be compatible
2999 -- with the key type. For a positional container, the choice must be
3000 -- a subtype indication or an iterator specification that determines
3001 -- an element type.
3003 Asp : constant Node_Id := Find_Value_Of_Aspect (Typ, Aspect_Aggregate);
3005 Empty_Subp : Node_Id := Empty;
3006 Add_Named_Subp : Node_Id := Empty;
3007 Add_Unnamed_Subp : Node_Id := Empty;
3008 New_Indexed_Subp : Node_Id := Empty;
3009 Assign_Indexed_Subp : Node_Id := Empty;
3011 ----------------------------------
3012 -- Resolve_Iterated_Association --
3013 ----------------------------------
3015 procedure Resolve_Iterated_Association
3016 (Comp : Node_Id;
3017 Key_Type : Entity_Id;
3018 Elmt_Type : Entity_Id)
3020 Loc : constant Source_Ptr := Sloc (N);
3021 Choice : Node_Id;
3022 Copy : Node_Id;
3023 Ent : Entity_Id;
3024 Expr : Node_Id;
3025 Key_Expr : Node_Id;
3026 Id : Entity_Id;
3027 Id_Name : Name_Id;
3028 Typ : Entity_Id := Empty;
3030 begin
3031 Error_Msg_Ada_2022_Feature ("iterated component", Loc);
3033 -- If this is an Iterated_Element_Association then either a
3034 -- an Iterator_Specification or a Loop_Parameter specification
3035 -- is present. In both cases a Key_Expression is present.
3037 if Nkind (Comp) = N_Iterated_Element_Association then
3039 -- Create a temporary scope to avoid some modifications from
3040 -- escaping the Analyze call below. The original Tree will be
3041 -- reanalyzed later.
3043 Ent := New_Internal_Entity
3044 (E_Loop, Current_Scope, Sloc (Comp), 'L');
3045 Set_Etype (Ent, Standard_Void_Type);
3046 Set_Parent (Ent, Parent (Comp));
3047 Push_Scope (Ent);
3049 if Present (Loop_Parameter_Specification (Comp)) then
3050 Copy := Copy_Separate_Tree (Comp);
3052 Analyze
3053 (Loop_Parameter_Specification (Copy));
3055 Id_Name := Chars (Defining_Identifier
3056 (Loop_Parameter_Specification (Comp)));
3057 else
3058 Copy := Copy_Separate_Tree (Iterator_Specification (Comp));
3059 Analyze (Copy);
3061 Id_Name := Chars (Defining_Identifier
3062 (Iterator_Specification (Comp)));
3063 end if;
3065 -- Key expression must have the type of the key. We analyze
3066 -- a copy of the original expression, because it will be
3067 -- reanalyzed and copied as needed during expansion of the
3068 -- corresponding loop.
3070 Key_Expr := Key_Expression (Comp);
3071 Analyze_And_Resolve (New_Copy_Tree (Key_Expr), Key_Type);
3072 End_Scope;
3074 elsif Present (Iterator_Specification (Comp)) then
3075 Copy := Copy_Separate_Tree (Iterator_Specification (Comp));
3076 Id_Name :=
3077 Chars (Defining_Identifier (Iterator_Specification (Comp)));
3079 Analyze (Copy);
3080 Typ := Etype (Defining_Identifier (Copy));
3082 else
3083 Choice := First (Discrete_Choices (Comp));
3085 while Present (Choice) loop
3086 Analyze (Choice);
3088 -- Choice can be a subtype name, a range, or an expression
3090 if Is_Entity_Name (Choice)
3091 and then Is_Type (Entity (Choice))
3092 and then Base_Type (Entity (Choice)) = Base_Type (Key_Type)
3093 then
3094 null;
3096 elsif Present (Key_Type) then
3097 Analyze_And_Resolve (Choice, Key_Type);
3099 else
3100 Typ := Etype (Choice); -- assume unique for now
3101 end if;
3103 Next (Choice);
3104 end loop;
3106 Id_Name := Chars (Defining_Identifier (Comp));
3107 end if;
3109 -- Create a scope in which to introduce an index, which is usually
3110 -- visible in the expression for the component, and needed for its
3111 -- analysis.
3113 Id := Make_Defining_Identifier (Sloc (Comp), Id_Name);
3114 Ent := New_Internal_Entity (E_Loop,
3115 Current_Scope, Sloc (Comp), 'L');
3116 Set_Etype (Ent, Standard_Void_Type);
3117 Set_Parent (Ent, Parent (Comp));
3118 Push_Scope (Ent);
3120 -- Insert and decorate the loop variable in the current scope.
3121 -- The expression has to be analyzed once the loop variable is
3122 -- directly visible. Mark the variable as referenced to prevent
3123 -- spurious warnings, given that subsequent uses of its name in the
3124 -- expression will reference the internal (synonym) loop variable.
3126 Enter_Name (Id);
3128 if No (Key_Type) then
3129 pragma Assert (Present (Typ));
3130 Set_Etype (Id, Typ);
3131 else
3132 Set_Etype (Id, Key_Type);
3133 end if;
3135 Mutate_Ekind (Id, E_Variable);
3136 Set_Is_Not_Self_Hidden (Id);
3137 Set_Scope (Id, Ent);
3138 Set_Referenced (Id);
3140 -- Analyze a copy of the expression, to verify legality. We use
3141 -- a copy because the expression will be analyzed anew when the
3142 -- enclosing aggregate is expanded, and the construct is rewritten
3143 -- as a loop with a new index variable.
3145 Expr := New_Copy_Tree (Expression (Comp));
3146 Preanalyze_And_Resolve (Expr, Elmt_Type);
3147 End_Scope;
3149 end Resolve_Iterated_Association;
3151 -- Start of processing for Resolve_Container_Aggregate
3153 begin
3154 pragma Assert (Nkind (Asp) = N_Aggregate);
3156 Set_Etype (N, Typ);
3157 Parse_Aspect_Aggregate (Asp,
3158 Empty_Subp, Add_Named_Subp, Add_Unnamed_Subp,
3159 New_Indexed_Subp, Assign_Indexed_Subp);
3161 if Present (Add_Unnamed_Subp)
3162 and then No (New_Indexed_Subp)
3163 and then Etype (Add_Unnamed_Subp) /= Any_Type
3164 then
3165 declare
3166 Elmt_Type : constant Entity_Id :=
3167 Etype (Next_Formal
3168 (First_Formal (Entity (Add_Unnamed_Subp))));
3169 Comp : Node_Id;
3171 begin
3172 if Present (Expressions (N)) then
3173 -- positional aggregate
3175 Comp := First (Expressions (N));
3176 while Present (Comp) loop
3177 Analyze_And_Resolve (Comp, Elmt_Type);
3178 Next (Comp);
3179 end loop;
3180 end if;
3182 -- Empty aggregate, to be replaced by Empty during
3183 -- expansion, or iterated component association.
3185 if Present (Component_Associations (N)) then
3186 declare
3187 Comp : Node_Id := First (Component_Associations (N));
3188 begin
3189 while Present (Comp) loop
3190 if Nkind (Comp) /=
3191 N_Iterated_Component_Association
3192 then
3193 Error_Msg_N ("illegal component association "
3194 & "for unnamed container aggregate", Comp);
3195 return;
3196 else
3197 Resolve_Iterated_Association
3198 (Comp, Empty, Elmt_Type);
3199 end if;
3201 Next (Comp);
3202 end loop;
3203 end;
3204 end if;
3205 end;
3207 elsif Present (Add_Named_Subp)
3208 and then Etype (Add_Named_Subp) /= Any_Type
3209 then
3210 declare
3211 -- Retrieves types of container, key, and element from the
3212 -- specified insertion procedure.
3214 Container : constant Entity_Id :=
3215 First_Formal (Entity (Add_Named_Subp));
3216 Key_Type : constant Entity_Id := Etype (Next_Formal (Container));
3217 Elmt_Type : constant Entity_Id :=
3218 Etype (Next_Formal (Next_Formal (Container)));
3219 Comp : Node_Id;
3220 Choice : Node_Id;
3222 begin
3223 Comp := First (Component_Associations (N));
3224 while Present (Comp) loop
3225 if Nkind (Comp) = N_Component_Association then
3226 Choice := First (Choices (Comp));
3228 while Present (Choice) loop
3229 Analyze_And_Resolve (Choice, Key_Type);
3230 if not Is_Static_Expression (Choice) then
3231 Error_Msg_N ("choice must be static", Choice);
3232 end if;
3234 Next (Choice);
3235 end loop;
3237 Analyze_And_Resolve (Expression (Comp), Elmt_Type);
3239 elsif Nkind (Comp) in
3240 N_Iterated_Component_Association |
3241 N_Iterated_Element_Association
3242 then
3243 Resolve_Iterated_Association
3244 (Comp, Key_Type, Elmt_Type);
3245 end if;
3247 Next (Comp);
3248 end loop;
3249 end;
3251 elsif Present (Assign_Indexed_Subp)
3252 and then Etype (Assign_Indexed_Subp) /= Any_Type
3253 then
3254 -- Indexed Aggregate. Positional or indexed component
3255 -- can be present, but not both. Choices must be static
3256 -- values or ranges with static bounds.
3258 declare
3259 Container : constant Entity_Id :=
3260 First_Formal (Entity (Assign_Indexed_Subp));
3261 Index_Type : constant Entity_Id := Etype (Next_Formal (Container));
3262 Comp_Type : constant Entity_Id :=
3263 Etype (Next_Formal (Next_Formal (Container)));
3264 Comp : Node_Id;
3265 Choice : Node_Id;
3266 Num_Choices : Nat := 0;
3268 Hi_Val : Uint;
3269 Lo_Val : Uint;
3270 begin
3271 if Present (Expressions (N)) then
3272 Comp := First (Expressions (N));
3273 while Present (Comp) loop
3274 Analyze_And_Resolve (Comp, Comp_Type);
3275 Next (Comp);
3276 end loop;
3277 end if;
3279 if Present (Component_Associations (N))
3280 and then not Is_Empty_List (Component_Associations (N))
3281 then
3282 if Present (Expressions (N))
3283 and then not Is_Empty_List (Expressions (N))
3284 then
3285 Error_Msg_N ("container aggregate cannot be "
3286 & "both positional and named", N);
3287 return;
3288 end if;
3290 Comp := First (Component_Associations (N));
3292 while Present (Comp) loop
3293 if Nkind (Comp) = N_Component_Association then
3294 Choice := First (Choices (Comp));
3296 while Present (Choice) loop
3297 Analyze_And_Resolve (Choice, Index_Type);
3298 Num_Choices := Num_Choices + 1;
3299 Next (Choice);
3300 end loop;
3302 Analyze_And_Resolve (Expression (Comp), Comp_Type);
3304 elsif Nkind (Comp) in
3305 N_Iterated_Component_Association |
3306 N_Iterated_Element_Association
3307 then
3308 Resolve_Iterated_Association
3309 (Comp, Index_Type, Comp_Type);
3310 Num_Choices := Num_Choices + 1;
3311 end if;
3313 Next (Comp);
3314 end loop;
3316 -- The component associations in an indexed aggregate
3317 -- must denote a contiguous set of static values. We
3318 -- build a table of values/ranges and sort it, as is done
3319 -- elsewhere for case statements and array aggregates.
3320 -- If the aggregate has a single iterated association it
3321 -- is allowed to be nonstatic and there is nothing to check.
3323 if Num_Choices > 1 then
3324 declare
3325 Table : Case_Table_Type (1 .. Num_Choices);
3326 No_Choice : Pos := 1;
3327 Lo, Hi : Node_Id;
3329 -- Traverse aggregate to determine size of needed table.
3330 -- Verify that bounds are static and that loops have no
3331 -- filters or key expressions.
3333 begin
3334 Comp := First (Component_Associations (N));
3335 while Present (Comp) loop
3336 if Nkind (Comp) = N_Iterated_Element_Association then
3337 if Present
3338 (Loop_Parameter_Specification (Comp))
3339 then
3340 if Present (Iterator_Filter
3341 (Loop_Parameter_Specification (Comp)))
3342 then
3343 Error_Msg_N
3344 ("iterator filter not allowed " &
3345 "in indexed aggregate", Comp);
3346 return;
3348 elsif Present (Key_Expression
3349 (Loop_Parameter_Specification (Comp)))
3350 then
3351 Error_Msg_N
3352 ("key expression not allowed " &
3353 "in indexed aggregate", Comp);
3354 return;
3355 end if;
3356 end if;
3357 else
3358 Choice := First (Choices (Comp));
3360 while Present (Choice) loop
3361 Get_Index_Bounds (Choice, Lo, Hi);
3362 Table (No_Choice).Choice := Choice;
3363 Table (No_Choice).Lo := Lo;
3364 Table (No_Choice).Hi := Hi;
3366 -- Verify staticness of value or range
3368 if not Is_Static_Expression (Lo)
3369 or else not Is_Static_Expression (Hi)
3370 then
3371 Error_Msg_N
3372 ("nonstatic expression for index " &
3373 "for indexed aggregate", Choice);
3374 return;
3375 end if;
3377 No_Choice := No_Choice + 1;
3378 Next (Choice);
3379 end loop;
3380 end if;
3382 Next (Comp);
3383 end loop;
3385 Sort_Case_Table (Table);
3387 for J in 1 .. Num_Choices - 1 loop
3388 Hi_Val := Expr_Value (Table (J).Hi);
3389 Lo_Val := Expr_Value (Table (J + 1).Lo);
3391 if Lo_Val = Hi_Val then
3392 Error_Msg_N
3393 ("duplicate index in indexed aggregate",
3394 Table (J + 1).Choice);
3395 exit;
3397 elsif Lo_Val < Hi_Val then
3398 Error_Msg_N
3399 ("overlapping indices in indexed aggregate",
3400 Table (J + 1).Choice);
3401 exit;
3403 elsif Lo_Val > Hi_Val + 1 then
3404 Error_Msg_N
3405 ("missing index values", Table (J + 1).Choice);
3406 exit;
3407 end if;
3408 end loop;
3409 end;
3410 end if;
3411 end if;
3412 end;
3413 end if;
3414 end Resolve_Container_Aggregate;
3416 -----------------------------
3417 -- Resolve_Delta_Aggregate --
3418 -----------------------------
3420 procedure Resolve_Delta_Aggregate (N : Node_Id; Typ : Entity_Id) is
3421 Base : constant Node_Id := Expression (N);
3423 begin
3424 Error_Msg_Ada_2022_Feature ("delta aggregate", Sloc (N));
3426 if not Is_Composite_Type (Typ) then
3427 Error_Msg_N ("not a composite type", N);
3428 end if;
3430 Analyze_And_Resolve (Base, Typ);
3432 if Is_Array_Type (Typ) then
3433 -- For an array_delta_aggregate, the base_expression and each
3434 -- expression in every array_component_association shall be of a
3435 -- nonlimited type; RM 4.3.4(13/5). However, to prevent repeated
3436 -- errors we only check the base expression and not array component
3437 -- associations.
3439 if Is_Limited_Type (Etype (Base)) then
3440 Error_Msg_N
3441 ("array delta aggregate shall be of a nonlimited type", Base);
3442 Explain_Limited_Type (Etype (Base), Base);
3443 end if;
3445 Resolve_Delta_Array_Aggregate (N, Typ);
3446 else
3448 -- Delta aggregates for record types must use parentheses,
3449 -- not square brackets.
3451 if Is_Homogeneous_Aggregate (N) then
3452 Error_Msg_N
3453 ("delta aggregates for record types must use (), not '[']", N);
3454 end if;
3456 -- The base_expression of a record_delta_aggregate can be of a
3457 -- limited type only if it is newly constructed; RM 7.5(2.1/5).
3459 Check_Expr_OK_In_Limited_Aggregate (Base);
3461 Resolve_Delta_Record_Aggregate (N, Typ);
3462 end if;
3464 Set_Etype (N, Typ);
3465 end Resolve_Delta_Aggregate;
3467 -----------------------------------
3468 -- Resolve_Delta_Array_Aggregate --
3469 -----------------------------------
3471 procedure Resolve_Delta_Array_Aggregate (N : Node_Id; Typ : Entity_Id) is
3472 Deltas : constant List_Id := Component_Associations (N);
3473 Index_Type : constant Entity_Id := Etype (First_Index (Typ));
3475 Assoc : Node_Id;
3476 Choice : Node_Id;
3477 Expr : Node_Id;
3479 begin
3480 Assoc := First (Deltas);
3481 while Present (Assoc) loop
3482 if Nkind (Assoc) = N_Iterated_Component_Association then
3483 Choice := First (Choice_List (Assoc));
3484 while Present (Choice) loop
3485 if Nkind (Choice) = N_Others_Choice then
3486 Error_Msg_N
3487 ("OTHERS not allowed in delta aggregate", Choice);
3489 elsif Nkind (Choice) = N_Subtype_Indication then
3490 Resolve_Discrete_Subtype_Indication
3491 (Choice, Base_Type (Index_Type));
3493 else
3494 Analyze_And_Resolve (Choice, Index_Type);
3495 end if;
3497 Next (Choice);
3498 end loop;
3500 declare
3501 Id : constant Entity_Id := Defining_Identifier (Assoc);
3502 Ent : constant Entity_Id :=
3503 New_Internal_Entity
3504 (E_Loop, Current_Scope, Sloc (Assoc), 'L');
3506 begin
3507 Set_Etype (Ent, Standard_Void_Type);
3508 Set_Parent (Ent, Assoc);
3509 Push_Scope (Ent);
3511 if No (Scope (Id)) then
3512 Set_Etype (Id, Index_Type);
3513 Mutate_Ekind (Id, E_Variable);
3514 Set_Is_Not_Self_Hidden (Id);
3515 Set_Scope (Id, Ent);
3516 end if;
3517 Enter_Name (Id);
3519 -- Resolve a copy of the expression, after setting
3520 -- its parent properly to preserve its context.
3522 Expr := New_Copy_Tree (Expression (Assoc));
3523 Set_Parent (Expr, Assoc);
3524 Analyze_And_Resolve (Expr, Component_Type (Typ));
3525 End_Scope;
3526 end;
3528 else
3529 Choice := First (Choice_List (Assoc));
3530 while Present (Choice) loop
3531 Analyze (Choice);
3533 if Nkind (Choice) = N_Others_Choice then
3534 Error_Msg_N
3535 ("OTHERS not allowed in delta aggregate", Choice);
3537 elsif Is_Entity_Name (Choice)
3538 and then Is_Type (Entity (Choice))
3539 then
3540 -- Choice covers a range of values
3542 if Base_Type (Entity (Choice)) /=
3543 Base_Type (Index_Type)
3544 then
3545 Error_Msg_NE
3546 ("choice does not match index type of &",
3547 Choice, Typ);
3548 end if;
3550 elsif Nkind (Choice) = N_Subtype_Indication then
3551 Resolve_Discrete_Subtype_Indication
3552 (Choice, Base_Type (Index_Type));
3554 else
3555 Resolve (Choice, Index_Type);
3556 end if;
3558 Next (Choice);
3559 end loop;
3561 -- For an array_delta_aggregate, the array_component_association
3562 -- shall not use the box symbol <>; RM 4.3.4(11/5).
3564 pragma Assert
3565 (Box_Present (Assoc) xor Present (Expression (Assoc)));
3567 if Box_Present (Assoc) then
3568 Error_Msg_N
3569 ("'<'> in array delta aggregate is not allowed", Assoc);
3570 else
3571 Analyze_And_Resolve (Expression (Assoc), Component_Type (Typ));
3572 end if;
3573 end if;
3575 Next (Assoc);
3576 end loop;
3577 end Resolve_Delta_Array_Aggregate;
3579 ------------------------------------
3580 -- Resolve_Delta_Record_Aggregate --
3581 ------------------------------------
3583 procedure Resolve_Delta_Record_Aggregate (N : Node_Id; Typ : Entity_Id) is
3585 -- Variables used to verify that discriminant-dependent components
3586 -- appear in the same variant.
3588 Comp_Ref : Entity_Id := Empty; -- init to avoid warning
3589 Variant : Node_Id;
3591 procedure Check_Variant (Id : Entity_Id);
3592 -- If a given component of the delta aggregate appears in a variant
3593 -- part, verify that it is within the same variant as that of previous
3594 -- specified variant components of the delta.
3596 function Get_Component (Nam : Node_Id) return Entity_Id;
3597 -- Locate component with a given name and return it. If none found then
3598 -- report error and return Empty.
3600 function Nested_In (V1 : Node_Id; V2 : Node_Id) return Boolean;
3601 -- Determine whether variant V1 is within variant V2
3603 function Variant_Depth (N : Node_Id) return Natural;
3604 -- Determine the distance of a variant to the enclosing type declaration
3606 --------------------
3607 -- Check_Variant --
3608 --------------------
3610 procedure Check_Variant (Id : Entity_Id) is
3611 Comp : Entity_Id;
3612 Comp_Variant : Node_Id;
3614 begin
3615 if not Has_Discriminants (Typ) then
3616 return;
3617 end if;
3619 Comp := First_Entity (Typ);
3620 while Present (Comp) loop
3621 exit when Chars (Comp) = Chars (Id);
3622 Next_Component (Comp);
3623 end loop;
3625 -- Find the variant, if any, whose component list includes the
3626 -- component declaration.
3628 Comp_Variant := Parent (Parent (List_Containing (Parent (Comp))));
3629 if Nkind (Comp_Variant) = N_Variant then
3630 if No (Variant) then
3631 Variant := Comp_Variant;
3632 Comp_Ref := Comp;
3634 elsif Variant /= Comp_Variant then
3635 declare
3636 D1 : constant Integer := Variant_Depth (Variant);
3637 D2 : constant Integer := Variant_Depth (Comp_Variant);
3639 begin
3640 if D1 = D2
3641 or else
3642 (D1 > D2 and then not Nested_In (Variant, Comp_Variant))
3643 or else
3644 (D2 > D1 and then not Nested_In (Comp_Variant, Variant))
3645 then
3646 pragma Assert (Present (Comp_Ref));
3647 Error_Msg_Node_2 := Comp_Ref;
3648 Error_Msg_NE
3649 ("& and & appear in different variants", Id, Comp);
3651 -- Otherwise retain the deeper variant for subsequent tests
3653 elsif D2 > D1 then
3654 Variant := Comp_Variant;
3655 end if;
3656 end;
3657 end if;
3658 end if;
3659 end Check_Variant;
3661 -------------------
3662 -- Get_Component --
3663 -------------------
3665 function Get_Component (Nam : Node_Id) return Entity_Id is
3666 Comp : Entity_Id;
3668 begin
3669 Comp := First_Entity (Typ);
3670 while Present (Comp) loop
3671 if Chars (Comp) = Chars (Nam) then
3672 if Ekind (Comp) = E_Discriminant then
3673 Error_Msg_N ("delta cannot apply to discriminant", Nam);
3674 end if;
3676 return Comp;
3677 end if;
3679 Next_Entity (Comp);
3680 end loop;
3682 Error_Msg_NE ("type& has no component with this name", Nam, Typ);
3683 return Empty;
3684 end Get_Component;
3686 ---------------
3687 -- Nested_In --
3688 ---------------
3690 function Nested_In (V1, V2 : Node_Id) return Boolean is
3691 Par : Node_Id;
3693 begin
3694 Par := Parent (V1);
3695 while Nkind (Par) /= N_Full_Type_Declaration loop
3696 if Par = V2 then
3697 return True;
3698 end if;
3700 Par := Parent (Par);
3701 end loop;
3703 return False;
3704 end Nested_In;
3706 -------------------
3707 -- Variant_Depth --
3708 -------------------
3710 function Variant_Depth (N : Node_Id) return Natural is
3711 Depth : Natural;
3712 Par : Node_Id;
3714 begin
3715 Depth := 0;
3716 Par := Parent (N);
3717 while Nkind (Par) /= N_Full_Type_Declaration loop
3718 Depth := Depth + 1;
3719 Par := Parent (Par);
3720 end loop;
3722 return Depth;
3723 end Variant_Depth;
3725 -- Local variables
3727 Deltas : constant List_Id := Component_Associations (N);
3729 Assoc : Node_Id;
3730 Choice : Node_Id;
3731 Comp : Entity_Id;
3732 Comp_Type : Entity_Id := Empty; -- init to avoid warning
3734 -- Start of processing for Resolve_Delta_Record_Aggregate
3736 begin
3737 Variant := Empty;
3739 Assoc := First (Deltas);
3740 while Present (Assoc) loop
3741 Choice := First (Choice_List (Assoc));
3742 while Present (Choice) loop
3743 Comp := Get_Component (Choice);
3745 if Present (Comp) then
3746 Check_Variant (Choice);
3748 Comp_Type := Etype (Comp);
3750 -- Decorate the component reference by setting its entity and
3751 -- type, as otherwise backends like GNATprove would have to
3752 -- rediscover this information by themselves.
3754 Set_Entity (Choice, Comp);
3755 Set_Etype (Choice, Comp_Type);
3756 else
3757 Comp_Type := Any_Type;
3758 end if;
3760 Next (Choice);
3761 end loop;
3763 pragma Assert (Present (Comp_Type));
3765 -- A record_component_association in record_delta_aggregate shall not
3766 -- use the box compound delimiter <> rather than an expression; see
3767 -- RM 4.3.1(17.3/5).
3769 pragma Assert (Present (Expression (Assoc)) xor Box_Present (Assoc));
3771 if Box_Present (Assoc) then
3772 Error_Msg_N
3773 ("'<'> in record delta aggregate is not allowed", Assoc);
3774 else
3775 Analyze_And_Resolve (Expression (Assoc), Comp_Type);
3777 -- The expression must not be of a limited type; RM 4.3.1(17.4/5)
3779 if Is_Limited_Type (Etype (Expression (Assoc))) then
3780 Error_Msg_N
3781 ("expression of a limited type in record delta aggregate " &
3782 "is not allowed",
3783 Expression (Assoc));
3784 end if;
3785 end if;
3787 Next (Assoc);
3788 end loop;
3789 end Resolve_Delta_Record_Aggregate;
3791 ---------------------------------
3792 -- Resolve_Extension_Aggregate --
3793 ---------------------------------
3795 -- There are two cases to consider:
3797 -- a) If the ancestor part is a type mark, the components needed are the
3798 -- difference between the components of the expected type and the
3799 -- components of the given type mark.
3801 -- b) If the ancestor part is an expression, it must be unambiguous, and
3802 -- once we have its type we can also compute the needed components as in
3803 -- the previous case. In both cases, if the ancestor type is not the
3804 -- immediate ancestor, we have to build this ancestor recursively.
3806 -- In both cases, discriminants of the ancestor type do not play a role in
3807 -- the resolution of the needed components, because inherited discriminants
3808 -- cannot be used in a type extension. As a result we can compute
3809 -- independently the list of components of the ancestor type and of the
3810 -- expected type.
3812 procedure Resolve_Extension_Aggregate (N : Node_Id; Typ : Entity_Id) is
3813 A : constant Node_Id := Ancestor_Part (N);
3814 A_Type : Entity_Id;
3815 I : Interp_Index;
3816 It : Interp;
3818 function Valid_Limited_Ancestor (Anc : Node_Id) return Boolean;
3819 -- If the type is limited, verify that the ancestor part is a legal
3820 -- expression (aggregate or function call, including 'Input)) that does
3821 -- not require a copy, as specified in 7.5(2).
3823 function Valid_Ancestor_Type return Boolean;
3824 -- Verify that the type of the ancestor part is a non-private ancestor
3825 -- of the expected type, which must be a type extension.
3827 procedure Transform_BIP_Assignment (Typ : Entity_Id);
3828 -- For an extension aggregate whose ancestor part is a build-in-place
3829 -- call returning a nonlimited type, this is used to transform the
3830 -- assignment to the ancestor part to use a temp.
3832 ----------------------------
3833 -- Valid_Limited_Ancestor --
3834 ----------------------------
3836 function Valid_Limited_Ancestor (Anc : Node_Id) return Boolean is
3837 begin
3838 if Is_Entity_Name (Anc) and then Is_Type (Entity (Anc)) then
3839 return True;
3841 -- The ancestor must be a call or an aggregate, but a call may
3842 -- have been expanded into a temporary, so check original node.
3844 elsif Nkind (Anc) in N_Aggregate
3845 | N_Extension_Aggregate
3846 | N_Function_Call
3847 then
3848 return True;
3850 elsif Nkind (Original_Node (Anc)) = N_Function_Call then
3851 return True;
3853 elsif Nkind (Anc) = N_Attribute_Reference
3854 and then Attribute_Name (Anc) = Name_Input
3855 then
3856 return True;
3858 elsif Nkind (Anc) = N_Qualified_Expression then
3859 return Valid_Limited_Ancestor (Expression (Anc));
3861 elsif Nkind (Anc) = N_Raise_Expression then
3862 return True;
3864 else
3865 return False;
3866 end if;
3867 end Valid_Limited_Ancestor;
3869 -------------------------
3870 -- Valid_Ancestor_Type --
3871 -------------------------
3873 function Valid_Ancestor_Type return Boolean is
3874 Imm_Type : Entity_Id;
3876 begin
3877 Imm_Type := Base_Type (Typ);
3878 while Is_Derived_Type (Imm_Type) loop
3879 if Etype (Imm_Type) = Base_Type (A_Type) then
3880 return True;
3882 -- The base type of the parent type may appear as a private
3883 -- extension if it is declared as such in a parent unit of the
3884 -- current one. For consistency of the subsequent analysis use
3885 -- the partial view for the ancestor part.
3887 elsif Is_Private_Type (Etype (Imm_Type))
3888 and then Present (Full_View (Etype (Imm_Type)))
3889 and then Base_Type (A_Type) = Full_View (Etype (Imm_Type))
3890 then
3891 A_Type := Etype (Imm_Type);
3892 return True;
3894 -- The parent type may be a private extension. The aggregate is
3895 -- legal if the type of the aggregate is an extension of it that
3896 -- is not a private extension.
3898 elsif Is_Private_Type (A_Type)
3899 and then not Is_Private_Type (Imm_Type)
3900 and then Present (Full_View (A_Type))
3901 and then Base_Type (Full_View (A_Type)) = Etype (Imm_Type)
3902 then
3903 return True;
3905 -- The parent type may be a raise expression (which is legal in
3906 -- any expression context).
3908 elsif A_Type = Raise_Type then
3909 A_Type := Etype (Imm_Type);
3910 return True;
3912 else
3913 Imm_Type := Etype (Base_Type (Imm_Type));
3914 end if;
3915 end loop;
3917 -- If previous loop did not find a proper ancestor, report error
3919 Error_Msg_NE ("expect ancestor type of &", A, Typ);
3920 return False;
3921 end Valid_Ancestor_Type;
3923 ------------------------------
3924 -- Transform_BIP_Assignment --
3925 ------------------------------
3927 procedure Transform_BIP_Assignment (Typ : Entity_Id) is
3928 Loc : constant Source_Ptr := Sloc (N);
3929 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'Y', A);
3930 Obj_Decl : constant Node_Id :=
3931 Make_Object_Declaration (Loc,
3932 Defining_Identifier => Def_Id,
3933 Constant_Present => True,
3934 Object_Definition => New_Occurrence_Of (Typ, Loc),
3935 Expression => A,
3936 Has_Init_Expression => True);
3937 begin
3938 Set_Etype (Def_Id, Typ);
3939 Set_Ancestor_Part (N, New_Occurrence_Of (Def_Id, Loc));
3940 Insert_Action (N, Obj_Decl);
3941 end Transform_BIP_Assignment;
3943 -- Start of processing for Resolve_Extension_Aggregate
3945 begin
3946 -- Analyze the ancestor part and account for the case where it is a
3947 -- parameterless function call.
3949 Analyze (A);
3950 Check_Parameterless_Call (A);
3952 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
3954 -- AI05-0115: If the ancestor part is a subtype mark, the ancestor
3955 -- must not have unknown discriminants. To catch cases where the
3956 -- aggregate occurs at a place where the full view of the ancestor
3957 -- type is visible and doesn't have unknown discriminants, but the
3958 -- aggregate type was derived from a partial view that has unknown
3959 -- discriminants, we check whether the aggregate type has unknown
3960 -- discriminants (unknown discriminants were inherited), along
3961 -- with checking that the partial view of the ancestor has unknown
3962 -- discriminants. (It might be sufficient to replace the entire
3963 -- condition with Has_Unknown_Discriminants (Typ), but that might
3964 -- miss some cases, not clear, and causes error changes in some tests
3965 -- such as class-wide cases, that aren't clearly improvements. ???)
3967 if Has_Unknown_Discriminants (Entity (A))
3968 or else (Has_Unknown_Discriminants (Typ)
3969 and then Partial_View_Has_Unknown_Discr (Entity (A)))
3970 then
3971 Error_Msg_NE
3972 ("aggregate not available for type& whose ancestor "
3973 & "has unknown discriminants", N, Typ);
3974 end if;
3975 end if;
3977 if not Is_Tagged_Type (Typ) then
3978 Error_Msg_N ("type of extension aggregate must be tagged", N);
3979 return;
3981 elsif Is_Limited_Type (Typ) then
3983 -- Ada 2005 (AI-287): Limited aggregates are allowed
3985 if Ada_Version < Ada_2005 then
3986 Error_Msg_N ("aggregate type cannot be limited", N);
3987 Explain_Limited_Type (Typ, N);
3988 return;
3990 elsif Valid_Limited_Ancestor (A) then
3991 null;
3993 else
3994 Error_Msg_N
3995 ("limited ancestor part must be aggregate or function call", A);
3996 end if;
3998 elsif Is_Class_Wide_Type (Typ) then
3999 Error_Msg_N ("aggregate cannot be of a class-wide type", N);
4000 return;
4001 end if;
4003 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
4004 A_Type := Get_Full_View (Entity (A));
4006 if Valid_Ancestor_Type then
4007 Set_Entity (A, A_Type);
4008 Set_Etype (A, A_Type);
4010 Validate_Ancestor_Part (N);
4011 Resolve_Record_Aggregate (N, Typ);
4012 end if;
4014 elsif Nkind (A) /= N_Aggregate then
4015 if Is_Overloaded (A) then
4016 A_Type := Any_Type;
4018 Get_First_Interp (A, I, It);
4019 while Present (It.Typ) loop
4021 -- Consider limited interpretations if Ada 2005 or higher
4023 if Is_Tagged_Type (It.Typ)
4024 and then (Ada_Version >= Ada_2005
4025 or else not Is_Limited_Type (It.Typ))
4026 then
4027 if A_Type /= Any_Type then
4028 Error_Msg_N ("cannot resolve expression", A);
4029 return;
4030 else
4031 A_Type := It.Typ;
4032 end if;
4033 end if;
4035 Get_Next_Interp (I, It);
4036 end loop;
4038 if A_Type = Any_Type then
4039 if Ada_Version >= Ada_2005 then
4040 Error_Msg_N
4041 ("ancestor part must be of a tagged type", A);
4042 else
4043 Error_Msg_N
4044 ("ancestor part must be of a nonlimited tagged type", A);
4045 end if;
4047 return;
4048 end if;
4050 else
4051 A_Type := Etype (A);
4052 end if;
4054 if Valid_Ancestor_Type then
4055 Resolve (A, A_Type);
4056 Check_Unset_Reference (A);
4057 Check_Non_Static_Context (A);
4059 -- The aggregate is illegal if the ancestor expression is a call
4060 -- to a function with a limited unconstrained result, unless the
4061 -- type of the aggregate is a null extension. This restriction
4062 -- was added in AI05-67 to simplify implementation.
4064 if Nkind (A) = N_Function_Call
4065 and then Is_Limited_Type (A_Type)
4066 and then not Is_Null_Extension (Typ)
4067 and then not Is_Constrained (A_Type)
4068 then
4069 Error_Msg_N
4070 ("type of limited ancestor part must be constrained", A);
4072 -- Reject the use of CPP constructors that leave objects partially
4073 -- initialized. For example:
4075 -- type CPP_Root is tagged limited record ...
4076 -- pragma Import (CPP, CPP_Root);
4078 -- type CPP_DT is new CPP_Root and Iface ...
4079 -- pragma Import (CPP, CPP_DT);
4081 -- type Ada_DT is new CPP_DT with ...
4083 -- Obj : Ada_DT := Ada_DT'(New_CPP_Root with others => <>);
4085 -- Using the constructor of CPP_Root the slots of the dispatch
4086 -- table of CPP_DT cannot be set, and the secondary tag of
4087 -- CPP_DT is unknown.
4089 elsif Nkind (A) = N_Function_Call
4090 and then Is_CPP_Constructor_Call (A)
4091 and then Enclosing_CPP_Parent (Typ) /= A_Type
4092 then
4093 Error_Msg_NE
4094 ("??must use 'C'P'P constructor for type &", A,
4095 Enclosing_CPP_Parent (Typ));
4097 -- The following call is not needed if the previous warning
4098 -- is promoted to an error.
4100 Resolve_Record_Aggregate (N, Typ);
4102 elsif Is_Class_Wide_Type (Etype (A))
4103 and then Nkind (Original_Node (A)) = N_Function_Call
4104 then
4105 -- If the ancestor part is a dispatching call, it appears
4106 -- statically to be a legal ancestor, but it yields any member
4107 -- of the class, and it is not possible to determine whether
4108 -- it is an ancestor of the extension aggregate (much less
4109 -- which ancestor). It is not possible to determine the
4110 -- components of the extension part.
4112 -- This check implements AI-306, which in fact was motivated by
4113 -- an AdaCore query to the ARG after this test was added.
4115 Error_Msg_N ("ancestor part must be statically tagged", A);
4116 else
4117 -- We are using the build-in-place protocol, but we can't build
4118 -- in place, because we need to call the function before
4119 -- allocating the aggregate. Could do better for null
4120 -- extensions, and maybe for nondiscriminated types.
4121 -- This is wrong for limited, but those were wrong already.
4123 if not Is_Limited_View (A_Type)
4124 and then Is_Build_In_Place_Function_Call (A)
4125 then
4126 Transform_BIP_Assignment (A_Type);
4127 end if;
4129 Resolve_Record_Aggregate (N, Typ);
4130 end if;
4131 end if;
4133 else
4134 Error_Msg_N ("no unique type for this aggregate", A);
4135 end if;
4137 Check_Function_Writable_Actuals (N);
4138 end Resolve_Extension_Aggregate;
4140 ----------------------------------
4141 -- Resolve_Null_Array_Aggregate --
4142 ----------------------------------
4144 function Resolve_Null_Array_Aggregate (N : Node_Id) return Boolean is
4145 -- Never returns False, but declared as a function to match
4146 -- other Resolve_Mumble functions.
4148 Loc : constant Source_Ptr := Sloc (N);
4149 Typ : constant Entity_Id := Etype (N);
4151 Index : Node_Id;
4152 Lo, Hi : Node_Id;
4153 Constr : constant List_Id := New_List;
4155 begin
4156 -- Attach the list of constraints at the location of the aggregate, so
4157 -- the individual constraints can be analyzed.
4159 Set_Parent (Constr, N);
4161 -- Create a constrained subtype with null dimensions
4163 Index := First_Index (Typ);
4164 while Present (Index) loop
4165 Get_Index_Bounds (Index, L => Lo, H => Hi);
4167 -- The upper bound is the predecessor of the lower bound
4169 Hi := Make_Attribute_Reference
4170 (Loc,
4171 Prefix => New_Occurrence_Of (Etype (Index), Loc),
4172 Attribute_Name => Name_Pred,
4173 Expressions => New_List (New_Copy_Tree (Lo)));
4175 Append (Make_Range (Loc, New_Copy_Tree (Lo), Hi), Constr);
4176 Analyze_And_Resolve (Last (Constr), Etype (Index));
4178 Index := Next_Index (Index);
4179 end loop;
4181 Set_Compile_Time_Known_Aggregate (N);
4182 Set_Aggregate_Bounds (N, First (Constr));
4184 return True;
4185 end Resolve_Null_Array_Aggregate;
4187 ------------------------------
4188 -- Resolve_Record_Aggregate --
4189 ------------------------------
4191 procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id) is
4192 New_Assoc_List : constant List_Id := New_List;
4193 -- New_Assoc_List is the newly built list of N_Component_Association
4194 -- nodes.
4196 Others_Etype : Entity_Id := Empty;
4197 -- This variable is used to save the Etype of the last record component
4198 -- that takes its value from the others choice. Its purpose is:
4200 -- (a) make sure the others choice is useful
4202 -- (b) make sure the type of all the components whose value is
4203 -- subsumed by the others choice are the same.
4205 -- This variable is updated as a side effect of function Get_Value.
4207 Box_Node : Node_Id := Empty;
4208 Is_Box_Present : Boolean := False;
4209 Is_Box_Init_By_Default : Boolean := False;
4210 Others_Box : Natural := 0;
4211 -- Ada 2005 (AI-287): Variables used in case of default initialization
4212 -- to provide a functionality similar to Others_Etype. Box_Present
4213 -- indicates that the component takes its default initialization;
4214 -- Others_Box counts the number of components of the current aggregate
4215 -- (which may be a sub-aggregate of a larger one) that are default-
4216 -- initialized. A value of One indicates that an others_box is present.
4217 -- Any larger value indicates that the others_box is not redundant.
4218 -- These variables, similar to Others_Etype, are also updated as a side
4219 -- effect of function Get_Value. Box_Node is used to place a warning on
4220 -- a redundant others_box.
4222 procedure Add_Association
4223 (Component : Entity_Id;
4224 Expr : Node_Id;
4225 Assoc_List : List_Id;
4226 Is_Box_Present : Boolean := False);
4227 -- Builds a new N_Component_Association node which associates Component
4228 -- to expression Expr and adds it to the association list being built,
4229 -- either New_Assoc_List, or the association being built for an inner
4230 -- aggregate.
4232 procedure Add_Discriminant_Values
4233 (New_Aggr : Node_Id;
4234 Assoc_List : List_Id);
4235 -- The constraint to a component may be given by a discriminant of the
4236 -- enclosing type, in which case we have to retrieve its value, which is
4237 -- part of the enclosing aggregate. Assoc_List provides the discriminant
4238 -- associations of the current type or of some enclosing record.
4240 function Discriminant_Present (Input_Discr : Entity_Id) return Boolean;
4241 -- If aggregate N is a regular aggregate this routine will return True.
4242 -- Otherwise, if N is an extension aggregate, then Input_Discr denotes
4243 -- a discriminant whose value may already have been specified by N's
4244 -- ancestor part. This routine checks whether this is indeed the case
4245 -- and if so returns False, signaling that no value for Input_Discr
4246 -- should appear in N's aggregate part. Also, in this case, the routine
4247 -- appends to New_Assoc_List the discriminant value specified in the
4248 -- ancestor part.
4250 -- If the aggregate is in a context with expansion delayed, it will be
4251 -- reanalyzed. The inherited discriminant values must not be reinserted
4252 -- in the component list to prevent spurious errors, but they must be
4253 -- present on first analysis to build the proper subtype indications.
4254 -- The flag Inherited_Discriminant is used to prevent the re-insertion.
4256 function Find_Private_Ancestor (Typ : Entity_Id) return Entity_Id;
4257 -- AI05-0115: Find earlier ancestor in the derivation chain that is
4258 -- derived from private view Typ. Whether the aggregate is legal depends
4259 -- on the current visibility of the type as well as that of the parent
4260 -- of the ancestor.
4262 function Get_Value
4263 (Compon : Entity_Id;
4264 From : List_Id;
4265 Consider_Others_Choice : Boolean := False) return Node_Id;
4266 -- Given a record component stored in parameter Compon, this function
4267 -- returns its value as it appears in the list From, which is a list
4268 -- of N_Component_Association nodes.
4270 -- If no component association has a choice for the searched component,
4271 -- the value provided by the others choice is returned, if there is one,
4272 -- and Consider_Others_Choice is set to true. Otherwise Empty is
4273 -- returned. If there is more than one component association giving a
4274 -- value for the searched record component, an error message is emitted
4275 -- and the first found value is returned.
4277 -- If Consider_Others_Choice is set and the returned expression comes
4278 -- from the others choice, then Others_Etype is set as a side effect.
4279 -- An error message is emitted if the components taking their value from
4280 -- the others choice do not have same type.
4282 procedure Propagate_Discriminants
4283 (Aggr : Node_Id;
4284 Assoc_List : List_Id);
4285 -- Nested components may themselves be discriminated types constrained
4286 -- by outer discriminants, whose values must be captured before the
4287 -- aggregate is expanded into assignments.
4289 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Entity_Id);
4290 -- Analyzes and resolves expression Expr against the Etype of the
4291 -- Component. This routine also applies all appropriate checks to Expr.
4292 -- It finally saves a Expr in the newly created association list that
4293 -- will be attached to the final record aggregate. Note that if the
4294 -- Parent pointer of Expr is not set then Expr was produced with a
4295 -- New_Copy_Tree or some such.
4297 procedure Rewrite_Range (Root_Type : Entity_Id; Rge : Node_Id);
4298 -- Rewrite a range node Rge when its bounds refer to non-stored
4299 -- discriminants from Root_Type, to replace them with the stored
4300 -- discriminant values. This is required in GNATprove mode, and is
4301 -- adopted in all modes to avoid special-casing GNATprove mode.
4303 ---------------------
4304 -- Add_Association --
4305 ---------------------
4307 procedure Add_Association
4308 (Component : Entity_Id;
4309 Expr : Node_Id;
4310 Assoc_List : List_Id;
4311 Is_Box_Present : Boolean := False)
4313 Choice_List : constant List_Id := New_List;
4314 Loc : Source_Ptr;
4316 begin
4317 -- If this is a box association the expression is missing, so use the
4318 -- Sloc of the aggregate itself for the new association.
4320 pragma Assert (Present (Expr) xor Is_Box_Present);
4322 if Present (Expr) then
4323 Loc := Sloc (Expr);
4324 else
4325 Loc := Sloc (N);
4326 end if;
4328 Append_To (Choice_List, New_Occurrence_Of (Component, Loc));
4330 Append_To (Assoc_List,
4331 Make_Component_Association (Loc,
4332 Choices => Choice_List,
4333 Expression => Expr,
4334 Box_Present => Is_Box_Present));
4336 -- If this association has a box for a component that is initialized
4337 -- by default, then set flag on the new association to indicate that
4338 -- the original association was for such a box-initialized component.
4340 if Is_Box_Init_By_Default then
4341 Set_Was_Default_Init_Box_Association (Last (Assoc_List));
4342 end if;
4343 end Add_Association;
4345 -----------------------------
4346 -- Add_Discriminant_Values --
4347 -----------------------------
4349 procedure Add_Discriminant_Values
4350 (New_Aggr : Node_Id;
4351 Assoc_List : List_Id)
4353 Assoc : Node_Id;
4354 Discr : Entity_Id;
4355 Discr_Elmt : Elmt_Id;
4356 Discr_Val : Node_Id;
4357 Val : Entity_Id;
4359 begin
4360 Discr := First_Discriminant (Etype (New_Aggr));
4361 Discr_Elmt := First_Elmt (Discriminant_Constraint (Etype (New_Aggr)));
4362 while Present (Discr_Elmt) loop
4363 Discr_Val := Node (Discr_Elmt);
4365 -- If the constraint is given by a discriminant then it is a
4366 -- discriminant of an enclosing record, and its value has already
4367 -- been placed in the association list.
4369 if Is_Entity_Name (Discr_Val)
4370 and then Ekind (Entity (Discr_Val)) = E_Discriminant
4371 then
4372 Val := Entity (Discr_Val);
4374 Assoc := First (Assoc_List);
4375 while Present (Assoc) loop
4376 if Present (Entity (First (Choices (Assoc))))
4377 and then Entity (First (Choices (Assoc))) = Val
4378 then
4379 Discr_Val := Expression (Assoc);
4380 exit;
4381 end if;
4383 Next (Assoc);
4384 end loop;
4385 end if;
4387 Add_Association
4388 (Discr, New_Copy_Tree (Discr_Val),
4389 Component_Associations (New_Aggr));
4391 -- If the discriminant constraint is a current instance, mark the
4392 -- current aggregate so that the self-reference can be expanded
4393 -- later. The constraint may refer to the subtype of aggregate, so
4394 -- use base type for comparison.
4396 if Nkind (Discr_Val) = N_Attribute_Reference
4397 and then Is_Entity_Name (Prefix (Discr_Val))
4398 and then Is_Type (Entity (Prefix (Discr_Val)))
4399 and then Base_Type (Etype (N)) = Entity (Prefix (Discr_Val))
4400 then
4401 Set_Has_Self_Reference (N);
4402 end if;
4404 Next_Elmt (Discr_Elmt);
4405 Next_Discriminant (Discr);
4406 end loop;
4407 end Add_Discriminant_Values;
4409 --------------------------
4410 -- Discriminant_Present --
4411 --------------------------
4413 function Discriminant_Present (Input_Discr : Entity_Id) return Boolean is
4414 Regular_Aggr : constant Boolean := Nkind (N) /= N_Extension_Aggregate;
4416 Ancestor_Is_Subtyp : Boolean;
4418 Loc : Source_Ptr;
4420 Ancestor : Node_Id;
4421 Ancestor_Typ : Entity_Id;
4422 Comp_Assoc : Node_Id;
4423 Discr : Entity_Id;
4424 Discr_Expr : Node_Id;
4425 Discr_Val : Elmt_Id := No_Elmt;
4426 Orig_Discr : Entity_Id;
4428 begin
4429 if Regular_Aggr then
4430 return True;
4431 end if;
4433 -- Check whether inherited discriminant values have already been
4434 -- inserted in the aggregate. This will be the case if we are
4435 -- re-analyzing an aggregate whose expansion was delayed.
4437 if Present (Component_Associations (N)) then
4438 Comp_Assoc := First (Component_Associations (N));
4439 while Present (Comp_Assoc) loop
4440 if Inherited_Discriminant (Comp_Assoc) then
4441 return True;
4442 end if;
4444 Next (Comp_Assoc);
4445 end loop;
4446 end if;
4448 Ancestor := Ancestor_Part (N);
4449 Ancestor_Typ := Etype (Ancestor);
4450 Loc := Sloc (Ancestor);
4452 -- For a private type with unknown discriminants, use the underlying
4453 -- record view if it is available.
4455 if Has_Unknown_Discriminants (Ancestor_Typ)
4456 and then Present (Full_View (Ancestor_Typ))
4457 and then Present (Underlying_Record_View (Full_View (Ancestor_Typ)))
4458 then
4459 Ancestor_Typ := Underlying_Record_View (Full_View (Ancestor_Typ));
4460 end if;
4462 Ancestor_Is_Subtyp :=
4463 Is_Entity_Name (Ancestor) and then Is_Type (Entity (Ancestor));
4465 -- If the ancestor part has no discriminants clearly N's aggregate
4466 -- part must provide a value for Discr.
4468 if not Has_Discriminants (Ancestor_Typ) then
4469 return True;
4471 -- If the ancestor part is an unconstrained subtype mark then the
4472 -- Discr must be present in N's aggregate part.
4474 elsif Ancestor_Is_Subtyp
4475 and then not Is_Constrained (Entity (Ancestor))
4476 then
4477 return True;
4478 end if;
4480 -- Now look to see if Discr was specified in the ancestor part
4482 if Ancestor_Is_Subtyp then
4483 Discr_Val :=
4484 First_Elmt (Discriminant_Constraint (Entity (Ancestor)));
4485 end if;
4487 Orig_Discr := Original_Record_Component (Input_Discr);
4489 Discr := First_Discriminant (Ancestor_Typ);
4490 while Present (Discr) loop
4492 -- If Ancestor has already specified Disc value then insert its
4493 -- value in the final aggregate.
4495 if Original_Record_Component (Discr) = Orig_Discr then
4496 if Ancestor_Is_Subtyp then
4497 Discr_Expr := New_Copy_Tree (Node (Discr_Val));
4498 else
4499 Discr_Expr :=
4500 Make_Selected_Component (Loc,
4501 Prefix => Duplicate_Subexpr (Ancestor),
4502 Selector_Name => New_Occurrence_Of (Input_Discr, Loc));
4503 end if;
4505 Resolve_Aggr_Expr (Discr_Expr, Input_Discr);
4506 Set_Inherited_Discriminant (Last (New_Assoc_List));
4507 return False;
4508 end if;
4510 Next_Discriminant (Discr);
4512 if Ancestor_Is_Subtyp then
4513 Next_Elmt (Discr_Val);
4514 end if;
4515 end loop;
4517 return True;
4518 end Discriminant_Present;
4520 ---------------------------
4521 -- Find_Private_Ancestor --
4522 ---------------------------
4524 function Find_Private_Ancestor (Typ : Entity_Id) return Entity_Id is
4525 Par : Entity_Id;
4527 begin
4528 Par := Typ;
4529 loop
4530 if Has_Private_Ancestor (Par)
4531 and then not Has_Private_Ancestor (Etype (Base_Type (Par)))
4532 then
4533 return Par;
4535 elsif not Is_Derived_Type (Par) then
4536 return Empty;
4538 else
4539 Par := Etype (Base_Type (Par));
4540 end if;
4541 end loop;
4542 end Find_Private_Ancestor;
4544 ---------------
4545 -- Get_Value --
4546 ---------------
4548 function Get_Value
4549 (Compon : Entity_Id;
4550 From : List_Id;
4551 Consider_Others_Choice : Boolean := False) return Node_Id
4553 Typ : constant Entity_Id := Etype (Compon);
4554 Assoc : Node_Id;
4555 Expr : Node_Id := Empty;
4556 Selector_Name : Node_Id;
4558 begin
4559 Is_Box_Present := False;
4560 Is_Box_Init_By_Default := False;
4562 if No (From) then
4563 return Empty;
4564 end if;
4566 Assoc := First (From);
4567 while Present (Assoc) loop
4568 Selector_Name := First (Choices (Assoc));
4569 while Present (Selector_Name) loop
4570 if Nkind (Selector_Name) = N_Others_Choice then
4571 if Consider_Others_Choice and then No (Expr) then
4573 -- We need to duplicate the expression for each
4574 -- successive component covered by the others choice.
4575 -- This is redundant if the others_choice covers only
4576 -- one component (small optimization possible???), but
4577 -- indispensable otherwise, because each one must be
4578 -- expanded individually to preserve side effects.
4580 -- Ada 2005 (AI-287): In case of default initialization
4581 -- of components, we duplicate the corresponding default
4582 -- expression (from the record type declaration). The
4583 -- copy must carry the sloc of the association (not the
4584 -- original expression) to prevent spurious elaboration
4585 -- checks when the default includes function calls.
4587 if Box_Present (Assoc) then
4588 Others_Box := Others_Box + 1;
4589 Is_Box_Present := True;
4591 if Expander_Active then
4592 return
4593 New_Copy_Tree_And_Copy_Dimensions
4594 (Expression (Parent (Compon)),
4595 New_Sloc => Sloc (Assoc));
4596 else
4597 return Expression (Parent (Compon));
4598 end if;
4600 else
4601 if Present (Others_Etype)
4602 and then Base_Type (Others_Etype) /= Base_Type (Typ)
4603 then
4604 -- If the components are of an anonymous access
4605 -- type they are distinct, but this is legal in
4606 -- Ada 2012 as long as designated types match.
4608 if (Ekind (Typ) = E_Anonymous_Access_Type
4609 or else Ekind (Typ) =
4610 E_Anonymous_Access_Subprogram_Type)
4611 and then Designated_Type (Typ) =
4612 Designated_Type (Others_Etype)
4613 then
4614 null;
4615 else
4616 Error_Msg_N
4617 ("components in OTHERS choice must have same "
4618 & "type", Selector_Name);
4619 end if;
4620 end if;
4622 Others_Etype := Typ;
4624 -- Copy the expression so that it is resolved
4625 -- independently for each component, This is needed
4626 -- for accessibility checks on components of anonymous
4627 -- access types, even in compile_only mode.
4629 if not Inside_A_Generic then
4630 return
4631 New_Copy_Tree_And_Copy_Dimensions
4632 (Expression (Assoc));
4633 else
4634 return Expression (Assoc);
4635 end if;
4636 end if;
4637 end if;
4639 elsif Chars (Compon) = Chars (Selector_Name) then
4640 if No (Expr) then
4642 -- Ada 2005 (AI-231)
4644 if Ada_Version >= Ada_2005
4645 and then Known_Null (Expression (Assoc))
4646 then
4647 Check_Can_Never_Be_Null (Compon, Expression (Assoc));
4648 end if;
4650 -- We need to duplicate the expression when several
4651 -- components are grouped together with a "|" choice.
4652 -- For instance "filed1 | filed2 => Expr"
4654 -- Ada 2005 (AI-287)
4656 if Box_Present (Assoc) then
4657 Is_Box_Present := True;
4659 -- Duplicate the default expression of the component
4660 -- from the record type declaration, so a new copy
4661 -- can be attached to the association.
4663 -- Note that we always copy the default expression,
4664 -- even when the association has a single choice, in
4665 -- order to create a proper association for the
4666 -- expanded aggregate.
4668 -- Component may have no default, in which case the
4669 -- expression is empty and the component is default-
4670 -- initialized, but an association for the component
4671 -- exists, and it is not covered by an others clause.
4673 -- Scalar and private types have no initialization
4674 -- procedure, so they remain uninitialized. If the
4675 -- target of the aggregate is a constant this
4676 -- deserves a warning.
4678 if No (Expression (Parent (Compon)))
4679 and then not Has_Non_Null_Base_Init_Proc (Typ)
4680 and then not Has_Aspect (Typ, Aspect_Default_Value)
4681 and then not Is_Concurrent_Type (Typ)
4682 and then Nkind (Parent (N)) = N_Object_Declaration
4683 and then Constant_Present (Parent (N))
4684 then
4685 Error_Msg_Node_2 := Typ;
4686 Error_Msg_NE
4687 ("??component& of type& is uninitialized",
4688 Assoc, Selector_Name);
4690 -- An additional reminder if the component type
4691 -- is a generic formal.
4693 if Is_Generic_Type (Base_Type (Typ)) then
4694 Error_Msg_NE
4695 ("\instance should provide actual type with "
4696 & "initialization for&", Assoc, Typ);
4697 end if;
4698 end if;
4700 return
4701 New_Copy_Tree_And_Copy_Dimensions
4702 (Expression (Parent (Compon)));
4704 else
4705 if Present (Next (Selector_Name)) then
4706 Expr := New_Copy_Tree_And_Copy_Dimensions
4707 (Expression (Assoc));
4708 else
4709 Expr := Expression (Assoc);
4710 end if;
4711 end if;
4713 Generate_Reference (Compon, Selector_Name, 'm');
4715 else
4716 Error_Msg_NE
4717 ("more than one value supplied for &",
4718 Selector_Name, Compon);
4720 end if;
4721 end if;
4723 Next (Selector_Name);
4724 end loop;
4726 Next (Assoc);
4727 end loop;
4729 return Expr;
4730 end Get_Value;
4732 -----------------------------
4733 -- Propagate_Discriminants --
4734 -----------------------------
4736 procedure Propagate_Discriminants
4737 (Aggr : Node_Id;
4738 Assoc_List : List_Id)
4740 Loc : constant Source_Ptr := Sloc (N);
4742 procedure Process_Component (Comp : Entity_Id);
4743 -- Add one component with a box association to the inner aggregate,
4744 -- and recurse if component is itself composite.
4746 -----------------------
4747 -- Process_Component --
4748 -----------------------
4750 procedure Process_Component (Comp : Entity_Id) is
4751 T : constant Entity_Id := Etype (Comp);
4752 New_Aggr : Node_Id;
4754 begin
4755 if Is_Record_Type (T) and then Has_Discriminants (T) then
4756 New_Aggr := Make_Aggregate (Loc, No_List, New_List);
4757 Set_Etype (New_Aggr, T);
4759 Add_Association
4760 (Comp, New_Aggr, Component_Associations (Aggr));
4762 -- Collect discriminant values and recurse
4764 Add_Discriminant_Values (New_Aggr, Assoc_List);
4765 Propagate_Discriminants (New_Aggr, Assoc_List);
4767 Build_Constrained_Itype
4768 (New_Aggr, T, Component_Associations (New_Aggr));
4769 else
4770 Add_Association
4771 (Comp, Empty, Component_Associations (Aggr),
4772 Is_Box_Present => True);
4773 end if;
4774 end Process_Component;
4776 -- Local variables
4778 Aggr_Type : constant Entity_Id := Base_Type (Etype (Aggr));
4779 Components : constant Elist_Id := New_Elmt_List;
4780 Def_Node : constant Node_Id :=
4781 Type_Definition (Declaration_Node (Aggr_Type));
4783 Comp : Node_Id;
4784 Comp_Elmt : Elmt_Id;
4785 Errors : Boolean;
4787 -- Start of processing for Propagate_Discriminants
4789 begin
4790 -- The component type may be a variant type. Collect the components
4791 -- that are ruled by the known values of the discriminants. Their
4792 -- values have already been inserted into the component list of the
4793 -- current aggregate.
4795 if Nkind (Def_Node) = N_Record_Definition
4796 and then Present (Component_List (Def_Node))
4797 and then Present (Variant_Part (Component_List (Def_Node)))
4798 then
4799 Gather_Components (Aggr_Type,
4800 Component_List (Def_Node),
4801 Governed_By => Component_Associations (Aggr),
4802 Into => Components,
4803 Report_Errors => Errors);
4805 Comp_Elmt := First_Elmt (Components);
4806 while Present (Comp_Elmt) loop
4807 if Ekind (Node (Comp_Elmt)) /= E_Discriminant then
4808 Process_Component (Node (Comp_Elmt));
4809 end if;
4811 Next_Elmt (Comp_Elmt);
4812 end loop;
4814 -- No variant part, iterate over all components
4816 else
4817 Comp := First_Component (Etype (Aggr));
4818 while Present (Comp) loop
4819 Process_Component (Comp);
4820 Next_Component (Comp);
4821 end loop;
4822 end if;
4823 end Propagate_Discriminants;
4825 -----------------------
4826 -- Resolve_Aggr_Expr --
4827 -----------------------
4829 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Entity_Id) is
4830 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean;
4831 -- If the expression is an aggregate (possibly qualified) then its
4832 -- expansion is delayed until the enclosing aggregate is expanded
4833 -- into assignments. In that case, do not generate checks on the
4834 -- expression, because they will be generated later, and will other-
4835 -- wise force a copy (to remove side effects) that would leave a
4836 -- dynamic-sized aggregate in the code, something that gigi cannot
4837 -- handle.
4839 ---------------------------
4840 -- Has_Expansion_Delayed --
4841 ---------------------------
4843 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean is
4844 begin
4845 return
4846 (Nkind (Expr) in N_Aggregate | N_Extension_Aggregate
4847 and then Present (Etype (Expr))
4848 and then Is_Record_Type (Etype (Expr))
4849 and then Expansion_Delayed (Expr))
4850 or else
4851 (Nkind (Expr) = N_Qualified_Expression
4852 and then Has_Expansion_Delayed (Expression (Expr)));
4853 end Has_Expansion_Delayed;
4855 -- Local variables
4857 Expr_Type : Entity_Id := Empty;
4858 New_C : Entity_Id := Component;
4859 New_Expr : Node_Id;
4861 Relocate : Boolean;
4862 -- Set to True if the resolved Expr node needs to be relocated when
4863 -- attached to the newly created association list. This node need not
4864 -- be relocated if its parent pointer is not set. In fact in this
4865 -- case Expr is the output of a New_Copy_Tree call. If Relocate is
4866 -- True then we have analyzed the expression node in the original
4867 -- aggregate and hence it needs to be relocated when moved over to
4868 -- the new association list.
4870 -- Start of processing for Resolve_Aggr_Expr
4872 begin
4873 -- If the type of the component is elementary or the type of the
4874 -- aggregate does not contain discriminants, use the type of the
4875 -- component to resolve Expr.
4877 if Is_Elementary_Type (Etype (Component))
4878 or else not Has_Discriminants (Etype (N))
4879 then
4880 Expr_Type := Etype (Component);
4882 -- Otherwise we have to pick up the new type of the component from
4883 -- the new constrained subtype of the aggregate. In fact components
4884 -- which are of a composite type might be constrained by a
4885 -- discriminant, and we want to resolve Expr against the subtype were
4886 -- all discriminant occurrences are replaced with their actual value.
4888 else
4889 New_C := First_Component (Etype (N));
4890 while Present (New_C) loop
4891 if Chars (New_C) = Chars (Component) then
4892 Expr_Type := Etype (New_C);
4893 exit;
4894 end if;
4896 Next_Component (New_C);
4897 end loop;
4899 pragma Assert (Present (Expr_Type));
4901 -- For each range in an array type where a discriminant has been
4902 -- replaced with the constraint, check that this range is within
4903 -- the range of the base type. This checks is done in the init
4904 -- proc for regular objects, but has to be done here for
4905 -- aggregates since no init proc is called for them.
4907 if Is_Array_Type (Expr_Type) then
4908 declare
4909 Index : Node_Id;
4910 -- Range of the current constrained index in the array
4912 Orig_Index : Node_Id := First_Index (Etype (Component));
4913 -- Range corresponding to the range Index above in the
4914 -- original unconstrained record type. The bounds of this
4915 -- range may be governed by discriminants.
4917 Unconstr_Index : Node_Id := First_Index (Etype (Expr_Type));
4918 -- Range corresponding to the range Index above for the
4919 -- unconstrained array type. This range is needed to apply
4920 -- range checks.
4922 begin
4923 Index := First_Index (Expr_Type);
4924 while Present (Index) loop
4925 if Depends_On_Discriminant (Orig_Index) then
4926 Apply_Range_Check (Index, Etype (Unconstr_Index));
4927 end if;
4929 Next_Index (Index);
4930 Next_Index (Orig_Index);
4931 Next_Index (Unconstr_Index);
4932 end loop;
4933 end;
4934 end if;
4935 end if;
4937 -- If the Parent pointer of Expr is not set, Expr is an expression
4938 -- duplicated by New_Tree_Copy (this happens for record aggregates
4939 -- that look like (Field1 | Filed2 => Expr) or (others => Expr)).
4940 -- Such a duplicated expression must be attached to the tree
4941 -- before analysis and resolution to enforce the rule that a tree
4942 -- fragment should never be analyzed or resolved unless it is
4943 -- attached to the current compilation unit.
4945 if No (Parent (Expr)) then
4946 Set_Parent (Expr, N);
4947 Relocate := False;
4948 else
4949 Relocate := True;
4950 end if;
4952 Analyze_And_Resolve (Expr, Expr_Type);
4953 Check_Expr_OK_In_Limited_Aggregate (Expr);
4954 Check_Non_Static_Context (Expr);
4955 Check_Unset_Reference (Expr);
4957 -- Check wrong use of class-wide types
4959 if Is_Class_Wide_Type (Etype (Expr)) then
4960 Error_Msg_N ("dynamically tagged expression not allowed", Expr);
4961 end if;
4963 if not Has_Expansion_Delayed (Expr) then
4964 Aggregate_Constraint_Checks (Expr, Expr_Type);
4965 end if;
4967 -- If an aggregate component has a type with predicates, an explicit
4968 -- predicate check must be applied, as for an assignment statement,
4969 -- because the aggregate might not be expanded into individual
4970 -- component assignments.
4972 if Has_Predicates (Expr_Type)
4973 and then Analyzed (Expr)
4974 then
4975 Apply_Predicate_Check (Expr, Expr_Type);
4976 end if;
4978 if Raises_Constraint_Error (Expr) then
4979 Set_Raises_Constraint_Error (N);
4980 end if;
4982 -- If the expression has been marked as requiring a range check, then
4983 -- generate it here. It's a bit odd to be generating such checks in
4984 -- the analyzer, but harmless since Generate_Range_Check does nothing
4985 -- (other than making sure Do_Range_Check is set) if the expander is
4986 -- not active.
4988 if Do_Range_Check (Expr) then
4989 Generate_Range_Check (Expr, Expr_Type, CE_Range_Check_Failed);
4990 end if;
4992 -- Add association Component => Expr if the caller requests it
4994 if Relocate then
4995 New_Expr := Relocate_Node (Expr);
4997 -- Since New_Expr is not gonna be analyzed later on, we need to
4998 -- propagate here the dimensions form Expr to New_Expr.
5000 Copy_Dimensions (Expr, New_Expr);
5002 else
5003 New_Expr := Expr;
5004 end if;
5006 Add_Association (New_C, New_Expr, New_Assoc_List);
5007 end Resolve_Aggr_Expr;
5009 -------------------
5010 -- Rewrite_Range --
5011 -------------------
5013 procedure Rewrite_Range (Root_Type : Entity_Id; Rge : Node_Id) is
5014 procedure Rewrite_Bound
5015 (Bound : Node_Id;
5016 Disc : Entity_Id;
5017 Expr_Disc : Node_Id);
5018 -- Rewrite a bound of the range Bound, when it is equal to the
5019 -- non-stored discriminant Disc, into the stored discriminant
5020 -- value Expr_Disc.
5022 -------------------
5023 -- Rewrite_Bound --
5024 -------------------
5026 procedure Rewrite_Bound
5027 (Bound : Node_Id;
5028 Disc : Entity_Id;
5029 Expr_Disc : Node_Id)
5031 begin
5032 if Nkind (Bound) /= N_Identifier then
5033 return;
5034 end if;
5036 -- We expect either the discriminant or the discriminal
5038 if Entity (Bound) = Disc
5039 or else (Ekind (Entity (Bound)) = E_In_Parameter
5040 and then Discriminal_Link (Entity (Bound)) = Disc)
5041 then
5042 Rewrite (Bound, New_Copy_Tree (Expr_Disc));
5043 end if;
5044 end Rewrite_Bound;
5046 -- Local variables
5048 Low, High : Node_Id;
5049 Disc : Entity_Id;
5050 Expr_Disc : Elmt_Id;
5052 -- Start of processing for Rewrite_Range
5054 begin
5055 if Has_Discriminants (Root_Type) and then Nkind (Rge) = N_Range then
5056 Low := Low_Bound (Rge);
5057 High := High_Bound (Rge);
5059 Disc := First_Discriminant (Root_Type);
5060 Expr_Disc := First_Elmt (Stored_Constraint (Etype (N)));
5061 while Present (Disc) loop
5062 Rewrite_Bound (Low, Disc, Node (Expr_Disc));
5063 Rewrite_Bound (High, Disc, Node (Expr_Disc));
5064 Next_Discriminant (Disc);
5065 Next_Elmt (Expr_Disc);
5066 end loop;
5067 end if;
5068 end Rewrite_Range;
5070 -- Local variables
5072 Components : constant Elist_Id := New_Elmt_List;
5073 -- Components is the list of the record components whose value must be
5074 -- provided in the aggregate. This list does include discriminants.
5076 Component : Entity_Id;
5077 Component_Elmt : Elmt_Id;
5078 Expr : Node_Id;
5079 Positional_Expr : Node_Id;
5081 -- Start of processing for Resolve_Record_Aggregate
5083 begin
5084 -- A record aggregate is restricted in SPARK:
5086 -- Each named association can have only a single choice.
5087 -- OTHERS cannot be used.
5088 -- Positional and named associations cannot be mixed.
5090 if Present (Component_Associations (N)) then
5091 declare
5092 Assoc : Node_Id;
5094 begin
5095 Assoc := First (Component_Associations (N));
5096 while Present (Assoc) loop
5097 if Nkind (Assoc) = N_Iterated_Component_Association then
5098 Error_Msg_N
5099 ("iterated component association can only appear in an "
5100 & "array aggregate", N);
5101 raise Unrecoverable_Error;
5102 end if;
5104 Next (Assoc);
5105 end loop;
5106 end;
5107 end if;
5109 -- We may end up calling Duplicate_Subexpr on expressions that are
5110 -- attached to New_Assoc_List. For this reason we need to attach it
5111 -- to the tree by setting its parent pointer to N. This parent point
5112 -- will change in STEP 8 below.
5114 Set_Parent (New_Assoc_List, N);
5116 -- STEP 1: abstract type and null record verification
5118 if Is_Abstract_Type (Typ) then
5119 Error_Msg_N ("type of aggregate cannot be abstract", N);
5120 end if;
5122 if No (First_Entity (Typ)) and then Null_Record_Present (N) then
5123 Set_Etype (N, Typ);
5124 return;
5126 elsif Present (First_Entity (Typ))
5127 and then Null_Record_Present (N)
5128 and then not Is_Tagged_Type (Typ)
5129 then
5130 Error_Msg_N ("record aggregate cannot be null", N);
5131 return;
5133 -- If the type has no components, then the aggregate should either
5134 -- have "null record", or in Ada 2005 it could instead have a single
5135 -- component association given by "others => <>". For Ada 95 we flag an
5136 -- error at this point, but for Ada 2005 we proceed with checking the
5137 -- associations below, which will catch the case where it's not an
5138 -- aggregate with "others => <>". Note that the legality of a <>
5139 -- aggregate for a null record type was established by AI05-016.
5141 elsif No (First_Entity (Typ))
5142 and then Ada_Version < Ada_2005
5143 then
5144 Error_Msg_N ("record aggregate must be null", N);
5145 return;
5146 end if;
5148 -- A record aggregate can only use parentheses
5150 if Nkind (N) = N_Aggregate
5151 and then Is_Homogeneous_Aggregate (N)
5152 then
5153 Error_Msg_N ("record aggregate must use (), not '[']", N);
5154 return;
5155 end if;
5157 -- STEP 2: Verify aggregate structure
5159 Step_2 : declare
5160 Assoc : Node_Id;
5161 Bad_Aggregate : Boolean := False;
5162 Selector_Name : Node_Id;
5164 begin
5165 if Present (Component_Associations (N)) then
5166 Assoc := First (Component_Associations (N));
5167 else
5168 Assoc := Empty;
5169 end if;
5171 while Present (Assoc) loop
5172 Selector_Name := First (Choices (Assoc));
5173 while Present (Selector_Name) loop
5174 if Nkind (Selector_Name) = N_Identifier then
5175 null;
5177 elsif Nkind (Selector_Name) = N_Others_Choice then
5178 if Selector_Name /= First (Choices (Assoc))
5179 or else Present (Next (Selector_Name))
5180 then
5181 Error_Msg_N
5182 ("OTHERS must appear alone in a choice list",
5183 Selector_Name);
5184 return;
5186 elsif Present (Next (Assoc)) then
5187 Error_Msg_N
5188 ("OTHERS must appear last in an aggregate",
5189 Selector_Name);
5190 return;
5192 -- (Ada 2005): If this is an association with a box,
5193 -- indicate that the association need not represent
5194 -- any component.
5196 elsif Box_Present (Assoc) then
5197 Others_Box := 1;
5198 Box_Node := Assoc;
5199 end if;
5201 else
5202 Error_Msg_N
5203 ("selector name should be identifier or OTHERS",
5204 Selector_Name);
5205 Bad_Aggregate := True;
5206 end if;
5208 Next (Selector_Name);
5209 end loop;
5211 Next (Assoc);
5212 end loop;
5214 if Bad_Aggregate then
5215 return;
5216 end if;
5217 end Step_2;
5219 -- STEP 3: Find discriminant Values
5221 Step_3 : declare
5222 Discrim : Entity_Id;
5223 Missing_Discriminants : Boolean := False;
5225 begin
5226 if Present (Expressions (N)) then
5227 Positional_Expr := First (Expressions (N));
5228 else
5229 Positional_Expr := Empty;
5230 end if;
5232 -- AI05-0115: if the ancestor part is a subtype mark, the ancestor
5233 -- must not have unknown discriminants.
5234 -- ??? We are not checking any subtype mark here and this code is not
5235 -- exercised by any test, so it's likely wrong (in particular
5236 -- we should not use Root_Type here but the subtype mark, if any),
5237 -- and possibly not needed.
5239 if Is_Derived_Type (Typ)
5240 and then Has_Unknown_Discriminants (Root_Type (Typ))
5241 and then Nkind (N) /= N_Extension_Aggregate
5242 then
5243 Error_Msg_NE
5244 ("aggregate not available for type& whose ancestor "
5245 & "has unknown discriminants", N, Typ);
5246 end if;
5248 if Has_Unknown_Discriminants (Typ)
5249 and then Present (Underlying_Record_View (Typ))
5250 then
5251 Discrim := First_Discriminant (Underlying_Record_View (Typ));
5252 elsif Has_Discriminants (Typ) then
5253 Discrim := First_Discriminant (Typ);
5254 else
5255 Discrim := Empty;
5256 end if;
5258 -- First find the discriminant values in the positional components
5260 while Present (Discrim) and then Present (Positional_Expr) loop
5261 if Discriminant_Present (Discrim) then
5262 Resolve_Aggr_Expr (Positional_Expr, Discrim);
5264 -- Ada 2005 (AI-231)
5266 if Ada_Version >= Ada_2005
5267 and then Known_Null (Positional_Expr)
5268 then
5269 Check_Can_Never_Be_Null (Discrim, Positional_Expr);
5270 end if;
5272 Next (Positional_Expr);
5273 end if;
5275 if Present (Get_Value (Discrim, Component_Associations (N))) then
5276 Error_Msg_NE
5277 ("more than one value supplied for discriminant&",
5278 N, Discrim);
5279 end if;
5281 Next_Discriminant (Discrim);
5282 end loop;
5284 -- Find remaining discriminant values if any among named components
5286 while Present (Discrim) loop
5287 Expr := Get_Value (Discrim, Component_Associations (N), True);
5289 if not Discriminant_Present (Discrim) then
5290 if Present (Expr) then
5291 Error_Msg_NE
5292 ("more than one value supplied for discriminant &",
5293 N, Discrim);
5294 end if;
5296 elsif No (Expr) then
5297 Error_Msg_NE
5298 ("no value supplied for discriminant &", N, Discrim);
5299 Missing_Discriminants := True;
5301 else
5302 Resolve_Aggr_Expr (Expr, Discrim);
5303 end if;
5305 Next_Discriminant (Discrim);
5306 end loop;
5308 if Missing_Discriminants then
5309 return;
5310 end if;
5312 -- At this point and until the beginning of STEP 6, New_Assoc_List
5313 -- contains only the discriminants and their values.
5315 end Step_3;
5317 -- STEP 4: Set the Etype of the record aggregate
5319 if Has_Discriminants (Typ)
5320 or else (Has_Unknown_Discriminants (Typ)
5321 and then Present (Underlying_Record_View (Typ)))
5322 then
5323 Build_Constrained_Itype (N, Typ, New_Assoc_List);
5324 else
5325 Set_Etype (N, Typ);
5326 end if;
5328 -- STEP 5: Get remaining components according to discriminant values
5330 Step_5 : declare
5331 Dnode : Node_Id;
5332 Errors_Found : Boolean := False;
5333 Record_Def : Node_Id;
5334 Parent_Typ : Entity_Id;
5335 Parent_Typ_List : Elist_Id;
5336 Parent_Elmt : Elmt_Id;
5337 Root_Typ : Entity_Id;
5339 begin
5340 if Is_Derived_Type (Typ) and then Is_Tagged_Type (Typ) then
5341 Parent_Typ_List := New_Elmt_List;
5343 -- If this is an extension aggregate, the component list must
5344 -- include all components that are not in the given ancestor type.
5345 -- Otherwise, the component list must include components of all
5346 -- ancestors, starting with the root.
5348 if Nkind (N) = N_Extension_Aggregate then
5349 Root_Typ := Base_Type (Etype (Ancestor_Part (N)));
5351 else
5352 -- AI05-0115: check legality of aggregate for type with a
5353 -- private ancestor.
5355 Root_Typ := Root_Type (Typ);
5356 if Has_Private_Ancestor (Typ) then
5357 declare
5358 Ancestor : constant Entity_Id :=
5359 Find_Private_Ancestor (Typ);
5360 Ancestor_Unit : constant Entity_Id :=
5361 Cunit_Entity
5362 (Get_Source_Unit (Ancestor));
5363 Parent_Unit : constant Entity_Id :=
5364 Cunit_Entity (Get_Source_Unit
5365 (Base_Type (Etype (Ancestor))));
5366 begin
5367 -- Check whether we are in a scope that has full view
5368 -- over the private ancestor and its parent. This can
5369 -- only happen if the derivation takes place in a child
5370 -- unit of the unit that declares the parent, and we are
5371 -- in the private part or body of that child unit, else
5372 -- the aggregate is illegal.
5374 if Is_Child_Unit (Ancestor_Unit)
5375 and then Scope (Ancestor_Unit) = Parent_Unit
5376 and then In_Open_Scopes (Scope (Ancestor))
5377 and then
5378 (In_Private_Part (Scope (Ancestor))
5379 or else In_Package_Body (Scope (Ancestor)))
5380 then
5381 null;
5383 else
5384 Error_Msg_NE
5385 ("type of aggregate has private ancestor&!",
5386 N, Root_Typ);
5387 Error_Msg_N ("must use extension aggregate!", N);
5388 return;
5389 end if;
5390 end;
5391 end if;
5393 Dnode := Declaration_Node (Base_Type (Root_Typ));
5395 -- If we don't get a full declaration, then we have some error
5396 -- which will get signalled later so skip this part. Otherwise
5397 -- gather components of root that apply to the aggregate type.
5398 -- We use the base type in case there is an applicable stored
5399 -- constraint that renames the discriminants of the root.
5401 if Nkind (Dnode) = N_Full_Type_Declaration then
5402 Record_Def := Type_Definition (Dnode);
5403 Gather_Components
5404 (Base_Type (Typ),
5405 Component_List (Record_Def),
5406 Governed_By => New_Assoc_List,
5407 Into => Components,
5408 Report_Errors => Errors_Found);
5410 if Errors_Found then
5411 Error_Msg_N
5412 ("discriminant controlling variant part is not static",
5414 return;
5415 end if;
5416 end if;
5417 end if;
5419 Parent_Typ := Base_Type (Typ);
5420 while Parent_Typ /= Root_Typ loop
5421 Prepend_Elmt (Parent_Typ, To => Parent_Typ_List);
5422 Parent_Typ := Etype (Parent_Typ);
5424 -- Check whether a private parent requires the use of
5425 -- an extension aggregate. This test does not apply in
5426 -- an instantiation: if the generic unit is legal so is
5427 -- the instance.
5429 if Nkind (Parent (Base_Type (Parent_Typ))) =
5430 N_Private_Type_Declaration
5431 or else Nkind (Parent (Base_Type (Parent_Typ))) =
5432 N_Private_Extension_Declaration
5433 then
5434 if Nkind (N) /= N_Extension_Aggregate
5435 and then not In_Instance
5436 then
5437 Error_Msg_NE
5438 ("type of aggregate has private ancestor&!",
5439 N, Parent_Typ);
5440 Error_Msg_N ("must use extension aggregate!", N);
5441 return;
5443 elsif Parent_Typ /= Root_Typ then
5444 Error_Msg_NE
5445 ("ancestor part of aggregate must be private type&",
5446 Ancestor_Part (N), Parent_Typ);
5447 return;
5448 end if;
5450 -- The current view of ancestor part may be a private type,
5451 -- while the context type is always non-private.
5453 elsif Is_Private_Type (Root_Typ)
5454 and then Present (Full_View (Root_Typ))
5455 and then Nkind (N) = N_Extension_Aggregate
5456 then
5457 exit when Base_Type (Full_View (Root_Typ)) = Parent_Typ;
5458 end if;
5459 end loop;
5461 -- Now collect components from all other ancestors, beginning
5462 -- with the current type. If the type has unknown discriminants
5463 -- use the component list of the Underlying_Record_View, which
5464 -- needs to be used for the subsequent expansion of the aggregate
5465 -- into assignments.
5467 Parent_Elmt := First_Elmt (Parent_Typ_List);
5468 while Present (Parent_Elmt) loop
5469 Parent_Typ := Node (Parent_Elmt);
5471 if Has_Unknown_Discriminants (Parent_Typ)
5472 and then Present (Underlying_Record_View (Typ))
5473 then
5474 Parent_Typ := Underlying_Record_View (Parent_Typ);
5475 end if;
5477 Record_Def := Type_Definition (Parent (Base_Type (Parent_Typ)));
5478 Gather_Components (Empty,
5479 Component_List (Record_Extension_Part (Record_Def)),
5480 Governed_By => New_Assoc_List,
5481 Into => Components,
5482 Report_Errors => Errors_Found);
5484 Next_Elmt (Parent_Elmt);
5485 end loop;
5487 -- Typ is not a derived tagged type
5489 else
5490 Record_Def := Type_Definition (Parent (Base_Type (Typ)));
5492 if Null_Present (Record_Def) then
5493 null;
5495 elsif not Has_Unknown_Discriminants (Typ) then
5496 Gather_Components
5497 (Base_Type (Typ),
5498 Component_List (Record_Def),
5499 Governed_By => New_Assoc_List,
5500 Into => Components,
5501 Report_Errors => Errors_Found);
5503 else
5504 Gather_Components
5505 (Base_Type (Underlying_Record_View (Typ)),
5506 Component_List (Record_Def),
5507 Governed_By => New_Assoc_List,
5508 Into => Components,
5509 Report_Errors => Errors_Found);
5510 end if;
5511 end if;
5513 if Errors_Found then
5514 return;
5515 end if;
5516 end Step_5;
5518 -- STEP 6: Find component Values
5520 Component := Empty;
5521 Component_Elmt := First_Elmt (Components);
5523 -- First scan the remaining positional associations in the aggregate.
5524 -- Remember that at this point Positional_Expr contains the current
5525 -- positional association if any is left after looking for discriminant
5526 -- values in step 3.
5528 while Present (Positional_Expr) and then Present (Component_Elmt) loop
5529 Component := Node (Component_Elmt);
5530 Resolve_Aggr_Expr (Positional_Expr, Component);
5532 -- Ada 2005 (AI-231)
5534 if Ada_Version >= Ada_2005 and then Known_Null (Positional_Expr) then
5535 Check_Can_Never_Be_Null (Component, Positional_Expr);
5536 end if;
5538 if Present (Get_Value (Component, Component_Associations (N))) then
5539 Error_Msg_NE
5540 ("more than one value supplied for component &", N, Component);
5541 end if;
5543 Next (Positional_Expr);
5544 Next_Elmt (Component_Elmt);
5545 end loop;
5547 if Present (Positional_Expr) then
5548 Error_Msg_N
5549 ("too many components for record aggregate", Positional_Expr);
5550 end if;
5552 -- Now scan for the named arguments of the aggregate
5554 while Present (Component_Elmt) loop
5555 Component := Node (Component_Elmt);
5556 Expr := Get_Value (Component, Component_Associations (N), True);
5558 -- Note: The previous call to Get_Value sets the value of the
5559 -- variable Is_Box_Present.
5561 -- Ada 2005 (AI-287): Handle components with default initialization.
5562 -- Note: This feature was originally added to Ada 2005 for limited
5563 -- but it was finally allowed with any type.
5565 if Is_Box_Present then
5566 Check_Box_Component : declare
5567 Ctyp : constant Entity_Id := Etype (Component);
5569 begin
5570 -- Initially assume that the box is for a default-initialized
5571 -- component and reset to False in cases where that's not true.
5573 Is_Box_Init_By_Default := True;
5575 -- If there is a default expression for the aggregate, copy
5576 -- it into a new association. This copy must modify the scopes
5577 -- of internal types that may be attached to the expression
5578 -- (e.g. index subtypes of arrays) because in general the type
5579 -- declaration and the aggregate appear in different scopes,
5580 -- and the backend requires the scope of the type to match the
5581 -- point at which it is elaborated.
5583 -- If the component has an initialization procedure (IP) we
5584 -- pass the component to the expander, which will generate
5585 -- the call to such IP.
5587 -- If the component has discriminants, their values must
5588 -- be taken from their subtype. This is indispensable for
5589 -- constraints that are given by the current instance of an
5590 -- enclosing type, to allow the expansion of the aggregate to
5591 -- replace the reference to the current instance by the target
5592 -- object of the aggregate.
5594 if Is_Case_Choice_Pattern (N) then
5596 -- Do not transform box component values in a case-choice
5597 -- aggregate.
5599 Add_Association
5600 (Component => Component,
5601 Expr => Empty,
5602 Assoc_List => New_Assoc_List,
5603 Is_Box_Present => True);
5605 elsif Present (Parent (Component))
5606 and then Nkind (Parent (Component)) = N_Component_Declaration
5607 and then Present (Expression (Parent (Component)))
5608 then
5609 -- If component declaration has an initialization expression
5610 -- then this is not a case of default initialization.
5612 Is_Box_Init_By_Default := False;
5614 Expr :=
5615 New_Copy_Tree_And_Copy_Dimensions
5616 (Expression (Parent (Component)),
5617 New_Scope => Current_Scope,
5618 New_Sloc => Sloc (N));
5620 -- As the type of the copied default expression may refer
5621 -- to discriminants of the record type declaration, these
5622 -- non-stored discriminants need to be rewritten into stored
5623 -- discriminant values for the aggregate. This is required
5624 -- in GNATprove mode, and is adopted in all modes to avoid
5625 -- special-casing GNATprove mode.
5627 if Is_Array_Type (Etype (Expr)) then
5628 declare
5629 Rec_Typ : constant Entity_Id := Scope (Component);
5630 -- Root record type whose discriminants may be used as
5631 -- bounds in range nodes.
5633 Assoc : Node_Id;
5634 Choice : Node_Id;
5635 Index : Node_Id;
5637 begin
5638 -- Rewrite the range nodes occurring in the indexes
5639 -- and their types.
5641 Index := First_Index (Etype (Expr));
5642 while Present (Index) loop
5643 Rewrite_Range (Rec_Typ, Index);
5644 Rewrite_Range
5645 (Rec_Typ, Scalar_Range (Etype (Index)));
5647 Next_Index (Index);
5648 end loop;
5650 -- Rewrite the range nodes occurring as aggregate
5651 -- bounds and component associations.
5653 if Nkind (Expr) = N_Aggregate then
5654 if Present (Aggregate_Bounds (Expr)) then
5655 Rewrite_Range (Rec_Typ, Aggregate_Bounds (Expr));
5656 end if;
5658 if Present (Component_Associations (Expr)) then
5659 Assoc := First (Component_Associations (Expr));
5660 while Present (Assoc) loop
5661 Choice := First (Choices (Assoc));
5662 while Present (Choice) loop
5663 Rewrite_Range (Rec_Typ, Choice);
5665 Next (Choice);
5666 end loop;
5668 Next (Assoc);
5669 end loop;
5670 end if;
5671 end if;
5672 end;
5673 end if;
5675 Add_Association
5676 (Component => Component,
5677 Expr => Expr,
5678 Assoc_List => New_Assoc_List);
5679 Set_Has_Self_Reference (N);
5681 elsif Needs_Simple_Initialization (Ctyp) then
5682 Add_Association
5683 (Component => Component,
5684 Expr => Empty,
5685 Assoc_List => New_Assoc_List,
5686 Is_Box_Present => True);
5688 elsif Has_Non_Null_Base_Init_Proc (Ctyp)
5689 or else not Expander_Active
5690 then
5691 if Is_Record_Type (Ctyp)
5692 and then Has_Discriminants (Ctyp)
5693 and then not Is_Private_Type (Ctyp)
5694 then
5695 -- We build a partially initialized aggregate with the
5696 -- values of the discriminants and box initialization
5697 -- for the rest, if other components are present.
5699 -- The type of the aggregate is the known subtype of
5700 -- the component. The capture of discriminants must be
5701 -- recursive because subcomponents may be constrained
5702 -- (transitively) by discriminants of enclosing types.
5703 -- For a private type with discriminants, a call to the
5704 -- initialization procedure will be generated, and no
5705 -- subaggregate is needed.
5707 Capture_Discriminants : declare
5708 Loc : constant Source_Ptr := Sloc (N);
5709 Expr : Node_Id;
5711 begin
5712 Expr := Make_Aggregate (Loc, No_List, New_List);
5713 Set_Etype (Expr, Ctyp);
5715 -- If the enclosing type has discriminants, they have
5716 -- been collected in the aggregate earlier, and they
5717 -- may appear as constraints of subcomponents.
5719 -- Similarly if this component has discriminants, they
5720 -- might in turn be propagated to their components.
5722 if Has_Discriminants (Typ) then
5723 Add_Discriminant_Values (Expr, New_Assoc_List);
5724 Propagate_Discriminants (Expr, New_Assoc_List);
5726 elsif Has_Discriminants (Ctyp) then
5727 Add_Discriminant_Values
5728 (Expr, Component_Associations (Expr));
5729 Propagate_Discriminants
5730 (Expr, Component_Associations (Expr));
5732 Build_Constrained_Itype
5733 (Expr, Ctyp, Component_Associations (Expr));
5735 else
5736 declare
5737 Comp : Entity_Id;
5739 begin
5740 -- If the type has additional components, create
5741 -- an OTHERS box association for them.
5743 Comp := First_Component (Ctyp);
5744 while Present (Comp) loop
5745 if Ekind (Comp) = E_Component then
5746 if not Is_Record_Type (Etype (Comp)) then
5747 Append_To
5748 (Component_Associations (Expr),
5749 Make_Component_Association (Loc,
5750 Choices =>
5751 New_List (
5752 Make_Others_Choice (Loc)),
5753 Expression => Empty,
5754 Box_Present => True));
5755 end if;
5757 exit;
5758 end if;
5760 Next_Component (Comp);
5761 end loop;
5762 end;
5763 end if;
5765 Add_Association
5766 (Component => Component,
5767 Expr => Expr,
5768 Assoc_List => New_Assoc_List);
5769 end Capture_Discriminants;
5771 -- Otherwise the component type is not a record, or it has
5772 -- not discriminants, or it is private.
5774 else
5775 Add_Association
5776 (Component => Component,
5777 Expr => Empty,
5778 Assoc_List => New_Assoc_List,
5779 Is_Box_Present => True);
5780 end if;
5782 -- Otherwise we only need to resolve the expression if the
5783 -- component has partially initialized values (required to
5784 -- expand the corresponding assignments and run-time checks).
5786 elsif Present (Expr)
5787 and then Is_Partially_Initialized_Type (Ctyp)
5788 then
5789 Resolve_Aggr_Expr (Expr, Component);
5790 end if;
5791 end Check_Box_Component;
5793 elsif No (Expr) then
5795 -- Ignore hidden components associated with the position of the
5796 -- interface tags: these are initialized dynamically.
5798 if No (Related_Type (Component)) then
5799 Error_Msg_NE
5800 ("no value supplied for component &!", N, Component);
5801 end if;
5803 else
5804 Resolve_Aggr_Expr (Expr, Component);
5805 end if;
5807 Next_Elmt (Component_Elmt);
5808 end loop;
5810 -- STEP 7: check for invalid components + check type in choice list
5812 Step_7 : declare
5813 Assoc : Node_Id;
5814 New_Assoc : Node_Id;
5816 Selectr : Node_Id;
5817 -- Selector name
5819 Typech : Entity_Id;
5820 -- Type of first component in choice list
5822 begin
5823 if Present (Component_Associations (N)) then
5824 Assoc := First (Component_Associations (N));
5825 else
5826 Assoc := Empty;
5827 end if;
5829 Verification : while Present (Assoc) loop
5830 Selectr := First (Choices (Assoc));
5831 Typech := Empty;
5833 if Nkind (Selectr) = N_Others_Choice then
5835 -- Ada 2005 (AI-287): others choice may have expression or box
5837 if No (Others_Etype) and then Others_Box = 0 then
5838 Error_Msg_N
5839 ("OTHERS must represent at least one component", Selectr);
5841 elsif Others_Box = 1 and then Warn_On_Redundant_Constructs then
5842 Error_Msg_N ("OTHERS choice is redundant?r?", Box_Node);
5843 Error_Msg_N
5844 ("\previous choices cover all components?r?", Box_Node);
5845 end if;
5847 exit Verification;
5848 end if;
5850 while Present (Selectr) loop
5851 New_Assoc := First (New_Assoc_List);
5852 while Present (New_Assoc) loop
5853 Component := First (Choices (New_Assoc));
5855 if Chars (Selectr) = Chars (Component) then
5856 if Style_Check then
5857 Check_Identifier (Selectr, Entity (Component));
5858 end if;
5860 exit;
5861 end if;
5863 Next (New_Assoc);
5864 end loop;
5866 -- If no association, this is not a legal component of the type
5867 -- in question, unless its association is provided with a box.
5869 if No (New_Assoc) then
5870 if Box_Present (Parent (Selectr)) then
5872 -- This may still be a bogus component with a box. Scan
5873 -- list of components to verify that a component with
5874 -- that name exists.
5876 declare
5877 C : Entity_Id;
5879 begin
5880 C := First_Component (Typ);
5881 while Present (C) loop
5882 if Chars (C) = Chars (Selectr) then
5884 -- If the context is an extension aggregate,
5885 -- the component must not be inherited from
5886 -- the ancestor part of the aggregate.
5888 if Nkind (N) /= N_Extension_Aggregate
5889 or else
5890 Scope (Original_Record_Component (C)) /=
5891 Etype (Ancestor_Part (N))
5892 then
5893 exit;
5894 end if;
5895 end if;
5897 Next_Component (C);
5898 end loop;
5900 if No (C) then
5901 Error_Msg_Node_2 := Typ;
5902 Error_Msg_N ("& is not a component of}", Selectr);
5903 end if;
5904 end;
5906 elsif Chars (Selectr) /= Name_uTag
5907 and then Chars (Selectr) /= Name_uParent
5908 then
5909 if not Has_Discriminants (Typ) then
5910 Error_Msg_Node_2 := Typ;
5911 Error_Msg_N ("& is not a component of}", Selectr);
5912 else
5913 Error_Msg_N
5914 ("& is not a component of the aggregate subtype",
5915 Selectr);
5916 end if;
5918 Check_Misspelled_Component (Components, Selectr);
5919 end if;
5921 elsif No (Typech) then
5922 Typech := Base_Type (Etype (Component));
5924 -- AI05-0199: In Ada 2012, several components of anonymous
5925 -- access types can appear in a choice list, as long as the
5926 -- designated types match.
5928 elsif Typech /= Base_Type (Etype (Component)) then
5929 if Ada_Version >= Ada_2012
5930 and then Ekind (Typech) = E_Anonymous_Access_Type
5931 and then
5932 Ekind (Etype (Component)) = E_Anonymous_Access_Type
5933 and then Base_Type (Designated_Type (Typech)) =
5934 Base_Type (Designated_Type (Etype (Component)))
5935 and then
5936 Subtypes_Statically_Match (Typech, (Etype (Component)))
5937 then
5938 null;
5940 elsif not Box_Present (Parent (Selectr)) then
5941 Error_Msg_N
5942 ("components in choice list must have same type",
5943 Selectr);
5944 end if;
5945 end if;
5947 Next (Selectr);
5948 end loop;
5950 Next (Assoc);
5951 end loop Verification;
5952 end Step_7;
5954 -- STEP 8: replace the original aggregate
5956 Step_8 : declare
5957 New_Aggregate : constant Node_Id := New_Copy (N);
5959 begin
5960 Set_Expressions (New_Aggregate, No_List);
5961 Set_Etype (New_Aggregate, Etype (N));
5962 Set_Component_Associations (New_Aggregate, New_Assoc_List);
5963 Set_Check_Actuals (New_Aggregate, Check_Actuals (N));
5965 Rewrite (N, New_Aggregate);
5966 end Step_8;
5968 -- Check the dimensions of the components in the record aggregate
5970 Analyze_Dimension_Extension_Or_Record_Aggregate (N);
5971 end Resolve_Record_Aggregate;
5973 -----------------------------
5974 -- Check_Can_Never_Be_Null --
5975 -----------------------------
5977 procedure Check_Can_Never_Be_Null (Typ : Entity_Id; Expr : Node_Id) is
5978 Comp_Typ : Entity_Id;
5980 begin
5981 pragma Assert
5982 (Ada_Version >= Ada_2005
5983 and then Present (Expr)
5984 and then Known_Null (Expr));
5986 case Ekind (Typ) is
5987 when E_Array_Type =>
5988 Comp_Typ := Component_Type (Typ);
5990 when E_Component
5991 | E_Discriminant
5993 Comp_Typ := Etype (Typ);
5995 when others =>
5996 return;
5997 end case;
5999 if Can_Never_Be_Null (Comp_Typ) then
6001 -- Here we know we have a constraint error. Note that we do not use
6002 -- Apply_Compile_Time_Constraint_Error here to the Expr, which might
6003 -- seem the more natural approach. That's because in some cases the
6004 -- components are rewritten, and the replacement would be missed.
6005 -- We do not mark the whole aggregate as raising a constraint error,
6006 -- because the association may be a null array range.
6008 Error_Msg_N
6009 ("(Ada 2005) NULL not allowed in null-excluding component??", Expr);
6010 Error_Msg_N
6011 ("\Constraint_Error will be raised at run time??", Expr);
6013 Rewrite (Expr,
6014 Make_Raise_Constraint_Error
6015 (Sloc (Expr), Reason => CE_Access_Check_Failed));
6016 Set_Etype (Expr, Comp_Typ);
6017 Set_Analyzed (Expr);
6018 end if;
6019 end Check_Can_Never_Be_Null;
6021 ---------------------
6022 -- Sort_Case_Table --
6023 ---------------------
6025 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is
6026 U : constant Int := Case_Table'Last;
6027 K : Int;
6028 J : Int;
6029 T : Case_Bounds;
6031 begin
6032 K := 1;
6033 while K < U loop
6034 T := Case_Table (K + 1);
6036 J := K + 1;
6037 while J > 1
6038 and then Expr_Value (Case_Table (J - 1).Lo) > Expr_Value (T.Lo)
6039 loop
6040 Case_Table (J) := Case_Table (J - 1);
6041 J := J - 1;
6042 end loop;
6044 Case_Table (J) := T;
6045 K := K + 1;
6046 end loop;
6047 end Sort_Case_Table;
6049 end Sem_Aggr;