xfail gnat.dg/trampoline3.adb scan-assembler-not check on hppa*-*-*
[official-gcc.git] / gcc / ada / sem_aggr.adb
blobe381af101c85b3e1bca3c9a987eae89a1e470f01
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-2024, 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.Utils; use Einfo.Utils;
31 with Elists; use Elists;
32 with Errout; use Errout;
33 with Expander; use Expander;
34 with Exp_Tss; use Exp_Tss;
35 with Exp_Util; use Exp_Util;
36 with Freeze; use Freeze;
37 with Itypes; use Itypes;
38 with Lib; use Lib;
39 with Lib.Xref; use Lib.Xref;
40 with Namet; use Namet;
41 with Namet.Sp; use Namet.Sp;
42 with Nmake; use Nmake;
43 with Nlists; use Nlists;
44 with Opt; use Opt;
45 with Restrict; use Restrict;
46 with Rident; use Rident;
47 with Sem; use Sem;
48 with Sem_Aux; use Sem_Aux;
49 with Sem_Case; use Sem_Case;
50 with Sem_Cat; use Sem_Cat;
51 with Sem_Ch3; use Sem_Ch3;
52 with Sem_Ch8; use Sem_Ch8;
53 with Sem_Ch13; use Sem_Ch13;
54 with Sem_Dim; use Sem_Dim;
55 with Sem_Eval; use Sem_Eval;
56 with Sem_Res; use Sem_Res;
57 with Sem_Util; use Sem_Util;
58 with Sem_Type; use Sem_Type;
59 with Sem_Warn; use Sem_Warn;
60 with Sinfo; use Sinfo;
61 with Sinfo.Nodes; use Sinfo.Nodes;
62 with Sinfo.Utils; use Sinfo.Utils;
63 with Snames; use Snames;
64 with Stringt; use Stringt;
65 with Stand; use Stand;
66 with Style; use Style;
67 with Targparm; use Targparm;
68 with Tbuild; use Tbuild;
69 with Ttypes; use Ttypes;
70 with Uintp; use Uintp;
71 with Warnsw; use Warnsw;
73 package body Sem_Aggr is
75 type Case_Bounds is record
76 Lo : Node_Id;
77 -- Low bound of choice. Once we sort the Case_Table, then entries
78 -- will be in order of ascending Choice_Lo values.
80 Hi : Node_Id;
81 -- High Bound of choice. The sort does not pay any attention to the
82 -- high bound, so choices 1 .. 4 and 1 .. 5 could be in either order.
84 Highest : Uint;
85 -- If there are duplicates or missing entries, then in the sorted
86 -- table, this records the highest value among Choice_Hi values
87 -- seen so far, including this entry.
89 Choice : Node_Id;
90 -- The node of the choice
91 end record;
93 type Case_Table_Type is array (Pos range <>) of Case_Bounds;
94 -- Table type used by Check_Case_Choices procedure
96 -----------------------
97 -- Local Subprograms --
98 -----------------------
100 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type);
101 -- Sort the Case Table using the Lower Bound of each Choice as the key. A
102 -- simple insertion sort is used since the choices in a case statement will
103 -- usually be in near sorted order.
105 procedure Check_Can_Never_Be_Null (Typ : Entity_Id; Expr : Node_Id);
106 -- Ada 2005 (AI-231): Check bad usage of null for a component for which
107 -- null exclusion (NOT NULL) is specified. Typ can be an E_Array_Type for
108 -- the array case (the component type of the array will be used) or an
109 -- E_Component/E_Discriminant entity in the record case, in which case the
110 -- type of the component will be used for the test. If Typ is any other
111 -- kind of entity, the call is ignored. Expr is the component node in the
112 -- aggregate which is known to have a null value. A warning message will be
113 -- issued if the component is null excluding.
115 -- It would be better to pass the proper type for Typ ???
117 procedure Check_Expr_OK_In_Limited_Aggregate (Expr : Node_Id);
118 -- Check that Expr is either not limited or else is one of the cases of
119 -- expressions allowed for a limited component association (namely, an
120 -- aggregate, function call, or <> notation). Report error for violations.
121 -- Expression is also OK in an instance or inlining context, because we
122 -- have already preanalyzed and it is known to be type correct.
124 ------------------------------------------------------
125 -- Subprograms used for RECORD AGGREGATE Processing --
126 ------------------------------------------------------
128 procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id);
129 -- This procedure performs all the semantic checks required for record
130 -- aggregates. Note that for aggregates analysis and resolution go
131 -- hand in hand. Aggregate analysis has been delayed up to here and
132 -- it is done while resolving the aggregate.
134 -- N is the N_Aggregate node.
135 -- Typ is the record type for the aggregate resolution
137 -- While performing the semantic checks, this procedure builds a new
138 -- Component_Association_List where each record field appears alone in a
139 -- Component_Choice_List along with its corresponding expression. The
140 -- record fields in the Component_Association_List appear in the same order
141 -- in which they appear in the record type Typ.
143 -- Once this new Component_Association_List is built and all the semantic
144 -- checks performed, the original aggregate subtree is replaced with the
145 -- new named record aggregate just built. This new record aggregate has no
146 -- positional associations, so its Expressions field is set to No_List.
147 -- Note that subtree substitution is performed with Rewrite so as to be
148 -- able to retrieve the original aggregate.
150 -- The aggregate subtree manipulation performed by Resolve_Record_Aggregate
151 -- yields the aggregate format expected by Gigi. Typically, this kind of
152 -- tree manipulations are done in the expander. However, because the
153 -- semantic checks that need to be performed on record aggregates really go
154 -- hand in hand with the record aggregate normalization, the aggregate
155 -- subtree transformation is performed during resolution rather than
156 -- expansion. Had we decided otherwise we would have had to duplicate most
157 -- of the code in the expansion procedure Expand_Record_Aggregate. Note,
158 -- however, that all the expansion concerning aggregates for tagged records
159 -- is done in Expand_Record_Aggregate.
161 -- The algorithm of Resolve_Record_Aggregate proceeds as follows:
163 -- 1. Make sure that the record type against which the record aggregate
164 -- has to be resolved is not abstract. Furthermore if the type is a
165 -- null aggregate make sure the input aggregate N is also null.
167 -- 2. Verify that the structure of the aggregate is that of a record
168 -- aggregate. Specifically, look for component associations and ensure
169 -- that each choice list only has identifiers or the N_Others_Choice
170 -- node. Also make sure that if present, the N_Others_Choice occurs
171 -- last and by itself.
173 -- 3. If Typ contains discriminants, the values for each discriminant is
174 -- looked for. If the record type Typ has variants, we check that the
175 -- expressions corresponding to each discriminant ruling the (possibly
176 -- nested) variant parts of Typ, are static. This allows us to determine
177 -- the variant parts to which the rest of the aggregate must conform.
178 -- The names of discriminants with their values are saved in a new
179 -- association list, New_Assoc_List which is later augmented with the
180 -- names and values of the remaining components in the record type.
182 -- During this phase we also make sure that every discriminant is
183 -- assigned exactly one value. Note that when several values for a given
184 -- discriminant are found, semantic processing continues looking for
185 -- further errors. In this case it's the first discriminant value found
186 -- which we will be recorded.
188 -- IMPORTANT NOTE: For derived tagged types this procedure expects
189 -- First_Discriminant and Next_Discriminant to give the correct list
190 -- of discriminants, in the correct order.
192 -- 4. After all the discriminant values have been gathered, we can set the
193 -- Etype of the record aggregate. If Typ contains no discriminants this
194 -- is straightforward: the Etype of N is just Typ, otherwise a new
195 -- implicit constrained subtype of Typ is built to be the Etype of N.
197 -- 5. Gather the remaining record components according to the discriminant
198 -- values. This involves recursively traversing the record type
199 -- structure to see what variants are selected by the given discriminant
200 -- values. This processing is a little more convoluted if Typ is a
201 -- derived tagged types since we need to retrieve the record structure
202 -- of all the ancestors of Typ.
204 -- 6. After gathering the record components we look for their values in the
205 -- record aggregate and emit appropriate error messages should we not
206 -- find such values or should they be duplicated.
208 -- 7. We then make sure no illegal component names appear in the record
209 -- aggregate and make sure that the type of the record components
210 -- appearing in a same choice list is the same. Finally we ensure that
211 -- the others choice, if present, is used to provide the value of at
212 -- least a record component.
214 -- 8. The original aggregate node is replaced with the new named aggregate
215 -- built in steps 3 through 6, as explained earlier.
217 -- Given the complexity of record aggregate resolution, the primary goal of
218 -- this routine is clarity and simplicity rather than execution and storage
219 -- efficiency. If there are only positional components in the aggregate the
220 -- running time is linear. If there are associations the running time is
221 -- still linear as long as the order of the associations is not too far off
222 -- the order of the components in the record type. If this is not the case
223 -- the running time is at worst quadratic in the size of the association
224 -- list.
226 procedure Check_Misspelled_Component
227 (Elements : Elist_Id;
228 Component : Node_Id);
229 -- Give possible misspelling diagnostic if Component is likely to be a
230 -- misspelling of one of the components of the Assoc_List. This is called
231 -- by Resolve_Aggr_Expr after producing an invalid component error message.
233 -----------------------------------------------------
234 -- Subprograms used for ARRAY AGGREGATE Processing --
235 -----------------------------------------------------
237 function Resolve_Array_Aggregate
238 (N : Node_Id;
239 Index : Node_Id;
240 Index_Constr : Node_Id;
241 Component_Typ : Entity_Id;
242 Others_Allowed : Boolean) return Boolean;
243 -- This procedure performs the semantic checks for an array aggregate.
244 -- True is returned if the aggregate resolution succeeds.
246 -- The procedure works by recursively checking each nested aggregate.
247 -- Specifically, after checking a sub-aggregate nested at the i-th level
248 -- we recursively check all the subaggregates at the i+1-st level (if any).
249 -- Note that aggregates analysis and resolution go hand in hand.
250 -- Aggregate analysis has been delayed up to here and it is done while
251 -- resolving the aggregate.
253 -- N is the current N_Aggregate node to be checked.
255 -- Index is the index node corresponding to the array sub-aggregate that
256 -- we are currently checking (RM 4.3.3 (8)). Its Etype is the
257 -- corresponding index type (or subtype).
259 -- Index_Constr is the node giving the applicable index constraint if
260 -- any (RM 4.3.3 (10)). It "is a constraint provided by certain
261 -- contexts [...] that can be used to determine the bounds of the array
262 -- value specified by the aggregate". If Others_Allowed below is False
263 -- there is no applicable index constraint and this node is set to Index.
265 -- Component_Typ is the array component type.
267 -- Others_Allowed indicates whether an others choice is allowed
268 -- in the context where the top-level aggregate appeared.
270 -- The algorithm of Resolve_Array_Aggregate proceeds as follows:
272 -- 1. Make sure that the others choice, if present, is by itself and
273 -- appears last in the sub-aggregate. Check that we do not have
274 -- positional and named components in the array sub-aggregate (unless
275 -- the named association is an others choice). Finally if an others
276 -- choice is present, make sure it is allowed in the aggregate context.
278 -- 2. If the array sub-aggregate contains discrete_choices:
280 -- (A) Verify their validity. Specifically verify that:
282 -- (a) If a null range is present it must be the only possible
283 -- choice in the array aggregate.
285 -- (b) Ditto for a non static range.
287 -- (c) Ditto for a non static expression.
289 -- In addition this step analyzes and resolves each discrete_choice,
290 -- making sure that its type is the type of the corresponding Index.
291 -- If we are not at the lowest array aggregate level (in the case of
292 -- multi-dimensional aggregates) then invoke Resolve_Array_Aggregate
293 -- recursively on each component expression. Otherwise, resolve the
294 -- bottom level component expressions against the expected component
295 -- type ONLY IF the component corresponds to a single discrete choice
296 -- which is not an others choice (to see why read the DELAYED
297 -- COMPONENT RESOLUTION below).
299 -- (B) Determine the bounds of the sub-aggregate and lowest and
300 -- highest choice values.
302 -- 3. For positional aggregates:
304 -- (A) Loop over the component expressions either recursively invoking
305 -- Resolve_Array_Aggregate on each of these for multi-dimensional
306 -- array aggregates or resolving the bottom level component
307 -- expressions against the expected component type.
309 -- (B) Determine the bounds of the positional sub-aggregates.
311 -- 4. Try to determine statically whether the evaluation of the array
312 -- sub-aggregate raises Constraint_Error. If yes emit proper
313 -- warnings. The precise checks are the following:
315 -- (A) Check that the index range defined by aggregate bounds is
316 -- compatible with corresponding index subtype.
317 -- We also check against the base type. In fact it could be that
318 -- Low/High bounds of the base type are static whereas those of
319 -- the index subtype are not. Thus if we can statically catch
320 -- a problem with respect to the base type we are guaranteed
321 -- that the same problem will arise with the index subtype
323 -- (B) If we are dealing with a named aggregate containing an others
324 -- choice and at least one discrete choice then make sure the range
325 -- specified by the discrete choices does not overflow the
326 -- aggregate bounds. We also check against the index type and base
327 -- type bounds for the same reasons given in (A).
329 -- (C) If we are dealing with a positional aggregate with an others
330 -- choice make sure the number of positional elements specified
331 -- does not overflow the aggregate bounds. We also check against
332 -- the index type and base type bounds as mentioned in (A).
334 -- Finally construct an N_Range node giving the sub-aggregate bounds.
335 -- Set the Aggregate_Bounds field of the sub-aggregate to be this
336 -- N_Range. The routine Array_Aggr_Subtype below uses such N_Ranges
337 -- to build the appropriate aggregate subtype. Aggregate_Bounds
338 -- information is needed during expansion.
340 -- DELAYED COMPONENT RESOLUTION: The resolution of bottom level component
341 -- expressions in an array aggregate may call Duplicate_Subexpr or some
342 -- other routine that inserts code just outside the outermost aggregate.
343 -- If the array aggregate contains discrete choices or an others choice,
344 -- this may be wrong. Consider for instance the following example.
346 -- type Rec is record
347 -- V : Integer := 0;
348 -- end record;
350 -- type Acc_Rec is access Rec;
351 -- Arr : array (1..3) of Acc_Rec := (1 .. 3 => new Rec);
353 -- Then the transformation of "new Rec" that occurs during resolution
354 -- entails the following code modifications
356 -- P7b : constant Acc_Rec := new Rec;
357 -- RecIP (P7b.all);
358 -- Arr : array (1..3) of Acc_Rec := (1 .. 3 => P7b);
360 -- This code transformation is clearly wrong, since we need to call
361 -- "new Rec" for each of the 3 array elements. To avoid this problem we
362 -- delay resolution of the components of non positional array aggregates
363 -- to the expansion phase. As an optimization, if the discrete choice
364 -- specifies a single value we do not delay resolution.
366 function Array_Aggr_Subtype (N : Node_Id; Typ : Entity_Id) return Entity_Id;
367 -- This routine returns the type or subtype of an array aggregate.
369 -- N is the array aggregate node whose type we return.
371 -- Typ is the context type in which N occurs.
373 -- This routine creates an implicit array subtype whose bounds are
374 -- those defined by the aggregate. When this routine is invoked
375 -- Resolve_Array_Aggregate has already processed aggregate N. Thus the
376 -- Aggregate_Bounds of each sub-aggregate, is an N_Range node giving the
377 -- sub-aggregate bounds. When building the aggregate itype, this function
378 -- traverses the array aggregate N collecting such Aggregate_Bounds and
379 -- constructs the proper array aggregate itype.
381 -- Note that in the case of multidimensional aggregates each inner
382 -- sub-aggregate corresponding to a given array dimension, may provide a
383 -- different bounds. If it is possible to determine statically that
384 -- some sub-aggregates corresponding to the same index do not have the
385 -- same bounds, then a warning is emitted. If such check is not possible
386 -- statically (because some sub-aggregate bounds are dynamic expressions)
387 -- then this job is left to the expander. In all cases the particular
388 -- bounds that this function will chose for a given dimension is the first
389 -- N_Range node for a sub-aggregate corresponding to that dimension.
391 -- Note that the Raises_Constraint_Error flag of an array aggregate
392 -- whose evaluation is determined to raise CE by Resolve_Array_Aggregate,
393 -- is set in Resolve_Array_Aggregate but the aggregate is not
394 -- immediately replaced with a raise CE. In fact, Array_Aggr_Subtype must
395 -- first construct the proper itype for the aggregate (Gigi needs
396 -- this). After constructing the proper itype we will eventually replace
397 -- the top-level aggregate with a raise CE (done in Resolve_Aggregate).
398 -- Of course in cases such as:
400 -- type Arr is array (integer range <>) of Integer;
401 -- A : Arr := (positive range -1 .. 2 => 0);
403 -- The bounds of the aggregate itype are cooked up to look reasonable
404 -- (in this particular case the bounds will be 1 .. 2).
406 procedure Make_String_Into_Aggregate (N : Node_Id);
407 -- A string literal can appear in a context in which a one dimensional
408 -- array of characters is expected. This procedure simply rewrites the
409 -- string as an aggregate, prior to resolution.
411 function Resolve_Null_Array_Aggregate (N : Node_Id) return Boolean;
412 -- For the Ada 2022 construct, build a subtype with a null range for each
413 -- dimension, using the bounds from the context subtype (if the subtype
414 -- is constrained). If the subtype is unconstrained, then the bounds
415 -- are determined in much the same way as the bounds for a null string
416 -- literal with no applicable index constraint.
418 ---------------------------------
419 -- Delta aggregate processing --
420 ---------------------------------
422 procedure Resolve_Delta_Array_Aggregate (N : Node_Id; Typ : Entity_Id);
423 procedure Resolve_Delta_Record_Aggregate (N : Node_Id; Typ : Entity_Id);
424 procedure Resolve_Deep_Delta_Assoc (N : Node_Id; Typ : Entity_Id);
425 -- Resolve the names/expressions in a component association for
426 -- a deep delta aggregate. Typ is the type of the enclosing object.
428 ------------------------
429 -- Array_Aggr_Subtype --
430 ------------------------
432 function Array_Aggr_Subtype
433 (N : Node_Id;
434 Typ : Entity_Id) return Entity_Id
436 Aggr_Dimension : constant Pos := Number_Dimensions (Typ);
437 -- Number of aggregate index dimensions
439 Aggr_Range : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
440 -- Constrained N_Range of each index dimension in our aggregate itype
442 Aggr_Low : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
443 Aggr_High : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
444 -- Low and High bounds for each index dimension in our aggregate itype
446 Is_Fully_Positional : Boolean := True;
448 procedure Collect_Aggr_Bounds (N : Node_Id; Dim : Pos);
449 -- N is an array (sub-)aggregate. Dim is the dimension corresponding
450 -- to (sub-)aggregate N. This procedure collects and removes the side
451 -- effects of the constrained N_Range nodes corresponding to each index
452 -- dimension of our aggregate itype. These N_Range nodes are collected
453 -- in Aggr_Range above.
455 -- Likewise collect in Aggr_Low & Aggr_High above the low and high
456 -- bounds of each index dimension. If, when collecting, two bounds
457 -- corresponding to the same dimension are static and found to differ,
458 -- then emit a warning, and mark N as raising Constraint_Error.
460 -------------------------
461 -- Collect_Aggr_Bounds --
462 -------------------------
464 procedure Collect_Aggr_Bounds (N : Node_Id; Dim : Pos) is
465 This_Range : constant Node_Id := Aggregate_Bounds (N);
466 -- The aggregate range node of this specific sub-aggregate
468 This_Low : constant Node_Id := Low_Bound (This_Range);
469 This_High : constant Node_Id := High_Bound (This_Range);
470 -- The aggregate bounds of this specific sub-aggregate
472 Assoc : Node_Id;
473 Expr : Node_Id;
475 begin
476 Remove_Side_Effects (This_Low, Variable_Ref => True);
477 Remove_Side_Effects (This_High, Variable_Ref => True);
479 -- Collect the first N_Range for a given dimension that you find.
480 -- For a given dimension they must be all equal anyway.
482 if No (Aggr_Range (Dim)) then
483 Aggr_Low (Dim) := This_Low;
484 Aggr_High (Dim) := This_High;
485 Aggr_Range (Dim) := This_Range;
487 else
488 if Compile_Time_Known_Value (This_Low) then
489 if not Compile_Time_Known_Value (Aggr_Low (Dim)) then
490 Aggr_Low (Dim) := This_Low;
492 elsif Expr_Value (This_Low) /= Expr_Value (Aggr_Low (Dim)) then
493 Set_Raises_Constraint_Error (N);
494 Error_Msg_Warn := SPARK_Mode /= On;
495 Error_Msg_N ("sub-aggregate low bound mismatch<<", N);
496 Error_Msg_N ("\Constraint_Error [<<", N);
497 end if;
498 end if;
500 if Compile_Time_Known_Value (This_High) then
501 if not Compile_Time_Known_Value (Aggr_High (Dim)) then
502 Aggr_High (Dim) := This_High;
504 elsif
505 Expr_Value (This_High) /= Expr_Value (Aggr_High (Dim))
506 then
507 Set_Raises_Constraint_Error (N);
508 Error_Msg_Warn := SPARK_Mode /= On;
509 Error_Msg_N ("sub-aggregate high bound mismatch<<", N);
510 Error_Msg_N ("\Constraint_Error [<<", N);
511 end if;
512 end if;
513 end if;
515 if Dim < Aggr_Dimension then
517 -- Process positional components
519 if Present (Expressions (N)) then
520 Expr := First (Expressions (N));
521 while Present (Expr) loop
522 Collect_Aggr_Bounds (Expr, Dim + 1);
523 Next (Expr);
524 end loop;
525 end if;
527 -- Process component associations
529 if Present (Component_Associations (N)) then
530 Is_Fully_Positional := False;
532 Assoc := First (Component_Associations (N));
533 while Present (Assoc) loop
534 Expr := Expression (Assoc);
535 Collect_Aggr_Bounds (Expr, Dim + 1);
536 Next (Assoc);
537 end loop;
538 end if;
539 end if;
540 end Collect_Aggr_Bounds;
542 -- Array_Aggr_Subtype variables
544 Itype : Entity_Id;
545 -- The final itype of the overall aggregate
547 Index_Constraints : constant List_Id := New_List;
548 -- The list of index constraints of the aggregate itype
550 -- Start of processing for Array_Aggr_Subtype
552 begin
553 -- Make sure that the list of index constraints is properly attached to
554 -- the tree, and then collect the aggregate bounds.
556 -- If no aggregaate bounds have been set, this is an aggregate with
557 -- iterator specifications and a dynamic size to be determined by
558 -- first pass of expanded code.
560 if No (Aggregate_Bounds (N)) then
561 return Typ;
562 end if;
564 Set_Parent (Index_Constraints, N);
566 -- When resolving a null aggregate we created a list of aggregate bounds
567 -- for the consecutive dimensions. The bounds for the first dimension
568 -- are attached as the Aggregate_Bounds of the aggregate node.
570 if Is_Null_Aggregate (N) then
571 declare
572 This_Range : Node_Id := Aggregate_Bounds (N);
573 begin
574 for J in 1 .. Aggr_Dimension loop
575 Aggr_Range (J) := This_Range;
576 Next_Index (This_Range);
578 -- Remove bounds from the list, so they can be reattached as
579 -- the First_Index/Next_Index again by the code that also
580 -- handles non-null aggregates.
582 Remove (Aggr_Range (J));
583 end loop;
584 end;
585 else
586 Collect_Aggr_Bounds (N, 1);
587 end if;
589 -- Build the list of constrained indexes of our aggregate itype
591 for J in 1 .. Aggr_Dimension loop
592 Create_Index : declare
593 Index_Base : constant Entity_Id :=
594 Base_Type (Etype (Aggr_Range (J)));
595 Index_Typ : Entity_Id;
597 begin
598 -- Construct the Index subtype, and associate it with the range
599 -- construct that generates it.
601 Index_Typ :=
602 Create_Itype (Subtype_Kind (Ekind (Index_Base)), Aggr_Range (J));
604 Set_Etype (Index_Typ, Index_Base);
606 if Is_Character_Type (Index_Base) then
607 Set_Is_Character_Type (Index_Typ);
608 end if;
610 Set_Size_Info (Index_Typ, (Index_Base));
611 Set_RM_Size (Index_Typ, RM_Size (Index_Base));
612 Set_First_Rep_Item (Index_Typ, First_Rep_Item (Index_Base));
613 Set_Scalar_Range (Index_Typ, Aggr_Range (J));
615 if Is_Discrete_Or_Fixed_Point_Type (Index_Typ) then
616 Set_RM_Size (Index_Typ, UI_From_Int (Minimum_Size (Index_Typ)));
617 end if;
619 Set_Etype (Aggr_Range (J), Index_Typ);
621 Append (Aggr_Range (J), To => Index_Constraints);
622 end Create_Index;
623 end loop;
625 -- Now build the Itype
627 Itype := Create_Itype (E_Array_Subtype, N);
629 Set_First_Rep_Item (Itype, First_Rep_Item (Typ));
630 Set_Convention (Itype, Convention (Typ));
631 Set_Depends_On_Private (Itype, Has_Private_Component (Typ));
632 Set_Etype (Itype, Base_Type (Typ));
633 Set_Has_Alignment_Clause (Itype, Has_Alignment_Clause (Typ));
634 Set_Is_Aliased (Itype, Is_Aliased (Typ));
635 Set_Is_Independent (Itype, Is_Independent (Typ));
636 Set_Depends_On_Private (Itype, Depends_On_Private (Typ));
638 Copy_Suppress_Status (Index_Check, Typ, Itype);
639 Copy_Suppress_Status (Length_Check, Typ, Itype);
641 Set_First_Index (Itype, First (Index_Constraints));
642 Set_Is_Constrained (Itype, True);
643 Set_Is_Internal (Itype, True);
645 if Has_Predicates (Typ) then
646 Set_Has_Predicates (Itype);
648 -- If the base type has a predicate, capture the predicated parent
649 -- or the existing predicate function for SPARK use.
651 if Present (Predicate_Function (Typ)) then
652 Set_Predicate_Function (Itype, Predicate_Function (Typ));
654 elsif Is_Itype (Typ) then
655 Set_Predicated_Parent (Itype, Predicated_Parent (Typ));
657 else
658 Set_Predicated_Parent (Itype, Typ);
659 end if;
660 end if;
662 -- A simple optimization: purely positional aggregates of static
663 -- components should be passed to gigi unexpanded whenever possible, and
664 -- regardless of the staticness of the bounds themselves. Subsequent
665 -- checks in exp_aggr verify that type is not packed, etc.
667 Set_Size_Known_At_Compile_Time
668 (Itype,
669 Is_Fully_Positional
670 and then Comes_From_Source (N)
671 and then Size_Known_At_Compile_Time (Component_Type (Typ)));
673 -- We always need a freeze node for a packed array subtype, so that we
674 -- can build the Packed_Array_Impl_Type corresponding to the subtype. If
675 -- expansion is disabled, the packed array subtype is not built, and we
676 -- must not generate a freeze node for the type, or else it will appear
677 -- incomplete to gigi.
679 if Is_Packed (Itype)
680 and then not In_Spec_Expression
681 and then Expander_Active
682 then
683 Freeze_Itype (Itype, N);
684 end if;
686 return Itype;
687 end Array_Aggr_Subtype;
689 --------------------------------
690 -- Check_Misspelled_Component --
691 --------------------------------
693 procedure Check_Misspelled_Component
694 (Elements : Elist_Id;
695 Component : Node_Id)
697 Max_Suggestions : constant := 2;
699 Nr_Of_Suggestions : Natural := 0;
700 Suggestion_1 : Entity_Id := Empty;
701 Suggestion_2 : Entity_Id := Empty;
702 Component_Elmt : Elmt_Id;
704 begin
705 -- All the components of List are matched against Component and a count
706 -- is maintained of possible misspellings. When at the end of the
707 -- analysis there are one or two (not more) possible misspellings,
708 -- these misspellings will be suggested as possible corrections.
710 Component_Elmt := First_Elmt (Elements);
711 while Nr_Of_Suggestions <= Max_Suggestions
712 and then Present (Component_Elmt)
713 loop
714 if Is_Bad_Spelling_Of
715 (Chars (Node (Component_Elmt)),
716 Chars (Component))
717 then
718 Nr_Of_Suggestions := Nr_Of_Suggestions + 1;
720 case Nr_Of_Suggestions is
721 when 1 => Suggestion_1 := Node (Component_Elmt);
722 when 2 => Suggestion_2 := Node (Component_Elmt);
723 when others => null;
724 end case;
725 end if;
727 Next_Elmt (Component_Elmt);
728 end loop;
730 -- Report at most two suggestions
732 if Nr_Of_Suggestions = 1 then
733 Error_Msg_NE -- CODEFIX
734 ("\possible misspelling of&", Component, Suggestion_1);
736 elsif Nr_Of_Suggestions = 2 then
737 Error_Msg_Node_2 := Suggestion_2;
738 Error_Msg_NE -- CODEFIX
739 ("\possible misspelling of& or&", Component, Suggestion_1);
740 end if;
741 end Check_Misspelled_Component;
743 ----------------------------------------
744 -- Check_Expr_OK_In_Limited_Aggregate --
745 ----------------------------------------
747 procedure Check_Expr_OK_In_Limited_Aggregate (Expr : Node_Id) is
748 begin
749 if Is_Limited_Type (Etype (Expr))
750 and then Comes_From_Source (Expr)
751 then
752 if In_Instance_Body or else In_Inlined_Body then
753 null;
755 elsif not OK_For_Limited_Init (Etype (Expr), Expr) then
756 Error_Msg_N
757 ("initialization not allowed for limited types", Expr);
758 Explain_Limited_Type (Etype (Expr), Expr);
759 end if;
760 end if;
761 end Check_Expr_OK_In_Limited_Aggregate;
763 --------------------
764 -- Is_Deep_Choice --
765 --------------------
767 function Is_Deep_Choice
768 (Choice : Node_Id;
769 Aggr_Type : Type_Kind_Id) return Boolean
771 Pref : Node_Id := Choice;
772 begin
773 while not Is_Root_Prefix_Of_Deep_Choice (Pref) loop
774 Pref := Prefix (Pref);
775 end loop;
777 if Is_Array_Type (Aggr_Type) then
778 return Paren_Count (Pref) > 0
779 and then Pref /= Choice;
780 else
781 return Pref /= Choice;
782 end if;
783 end Is_Deep_Choice;
785 -------------------------
786 -- Is_Others_Aggregate --
787 -------------------------
789 function Is_Others_Aggregate (Aggr : Node_Id) return Boolean is
790 Assoc : constant List_Id := Component_Associations (Aggr);
792 begin
793 return No (Expressions (Aggr))
794 and then Nkind (First (Choice_List (First (Assoc)))) = N_Others_Choice;
795 end Is_Others_Aggregate;
797 -----------------------------------
798 -- Is_Root_Prefix_Of_Deep_Choice --
799 -----------------------------------
801 function Is_Root_Prefix_Of_Deep_Choice (Pref : Node_Id) return Boolean is
802 begin
803 return Paren_Count (Pref) > 0
804 or else Nkind (Pref) not in N_Indexed_Component
805 | N_Selected_Component;
806 end Is_Root_Prefix_Of_Deep_Choice;
808 -------------------------
809 -- Is_Single_Aggregate --
810 -------------------------
812 function Is_Single_Aggregate (Aggr : Node_Id) return Boolean is
813 Assoc : constant List_Id := Component_Associations (Aggr);
815 begin
816 return No (Expressions (Aggr))
817 and then No (Next (First (Assoc)))
818 and then No (Next (First (Choice_List (First (Assoc)))));
819 end Is_Single_Aggregate;
821 -----------------------
822 -- Is_Null_Aggregate --
823 -----------------------
825 function Is_Null_Aggregate (N : Node_Id) return Boolean is
826 begin
827 return Ada_Version >= Ada_2022
828 and then Is_Homogeneous_Aggregate (N)
829 and then Is_Empty_List (Expressions (N))
830 and then Is_Empty_List (Component_Associations (N));
831 end Is_Null_Aggregate;
833 ----------------------------------------
834 -- Is_Null_Array_Aggregate_High_Bound --
835 ----------------------------------------
837 function Is_Null_Array_Aggregate_High_Bound (N : Node_Id) return Boolean is
838 Original_N : constant Node_Id := Original_Node (N);
839 begin
840 return Ada_Version >= Ada_2022
841 and then not Comes_From_Source (Original_N)
842 and then Nkind (Original_N) = N_Attribute_Reference
843 and then
844 Get_Attribute_Id (Attribute_Name (Original_N)) = Attribute_Pred
845 and then Nkind (Parent (N)) in N_Range | N_Op_Le
846 and then not Comes_From_Source (Parent (N));
847 end Is_Null_Array_Aggregate_High_Bound;
849 --------------------------------
850 -- Make_String_Into_Aggregate --
851 --------------------------------
853 procedure Make_String_Into_Aggregate (N : Node_Id) is
854 Exprs : constant List_Id := New_List;
855 Loc : constant Source_Ptr := Sloc (N);
856 Str : constant String_Id := Strval (N);
857 Strlen : constant Nat := String_Length (Str);
858 C : Char_Code;
859 C_Node : Node_Id;
860 New_N : Node_Id;
861 P : Source_Ptr;
863 begin
864 P := Loc + 1;
865 for J in 1 .. Strlen loop
866 C := Get_String_Char (Str, J);
867 Set_Character_Literal_Name (C);
869 C_Node :=
870 Make_Character_Literal (P,
871 Chars => Name_Find,
872 Char_Literal_Value => UI_From_CC (C));
873 Set_Etype (C_Node, Any_Character);
874 Append_To (Exprs, C_Node);
876 P := P + 1;
877 -- Something special for wide strings???
878 end loop;
880 New_N := Make_Aggregate (Loc, Expressions => Exprs);
881 Set_Analyzed (New_N);
882 Set_Etype (New_N, Any_Composite);
884 Rewrite (N, New_N);
885 end Make_String_Into_Aggregate;
887 -----------------------
888 -- Resolve_Aggregate --
889 -----------------------
891 procedure Resolve_Aggregate (N : Node_Id; Typ : Entity_Id) is
892 Loc : constant Source_Ptr := Sloc (N);
894 Aggr_Subtyp : Entity_Id;
895 -- The actual aggregate subtype. This is not necessarily the same as Typ
896 -- which is the subtype of the context in which the aggregate was found.
898 Others_Box : Boolean := False;
899 -- Set to True if N represents a simple aggregate with only
900 -- (others => <>), not nested as part of another aggregate.
902 function Is_Full_Access_Aggregate (N : Node_Id) return Boolean;
903 -- If a full access object is initialized with an aggregate or is
904 -- assigned an aggregate, we have to prevent a piecemeal access or
905 -- assignment to the object, even if the aggregate is to be expanded.
906 -- We create a temporary for the aggregate, and assign the temporary
907 -- instead, so that the back end can generate an atomic move for it.
908 -- This is only done in the context of an object declaration or an
909 -- assignment. Function is a noop and returns false in other contexts.
911 function Within_Aggregate (N : Node_Id) return Boolean;
912 -- Return True if N is part of an N_Aggregate
914 ------------------------------
915 -- Is_Full_Access_Aggregate --
916 ------------------------------
918 function Is_Full_Access_Aggregate (N : Node_Id) return Boolean is
919 Loc : constant Source_Ptr := Sloc (N);
921 New_N : Node_Id;
922 Par : Node_Id;
923 Temp : Entity_Id;
924 Typ : Entity_Id;
926 begin
927 Par := Parent (N);
929 -- Aggregate may be qualified, so find outer context
931 if Nkind (Par) = N_Qualified_Expression then
932 Par := Parent (Par);
933 end if;
935 if not Comes_From_Source (Par) then
936 return False;
937 end if;
939 case Nkind (Par) is
940 when N_Assignment_Statement =>
941 Typ := Etype (Name (Par));
943 if not Is_Full_Access (Typ)
944 and then not Is_Full_Access_Object (Name (Par))
945 then
946 return False;
947 end if;
949 when N_Object_Declaration =>
950 Typ := Etype (Defining_Identifier (Par));
952 if not Is_Full_Access (Typ)
953 and then not Is_Full_Access (Defining_Identifier (Par))
954 then
955 return False;
956 end if;
958 when others =>
959 return False;
960 end case;
962 Temp := Make_Temporary (Loc, 'T', N);
963 New_N :=
964 Make_Object_Declaration (Loc,
965 Defining_Identifier => Temp,
966 Constant_Present => True,
967 Object_Definition => New_Occurrence_Of (Typ, Loc),
968 Expression => Relocate_Node (N));
969 Insert_Action (Par, New_N);
971 Rewrite (N, New_Occurrence_Of (Temp, Loc));
972 Analyze_And_Resolve (N, Typ);
974 return True;
975 end Is_Full_Access_Aggregate;
977 ----------------------
978 -- Within_Aggregate --
979 ----------------------
981 function Within_Aggregate (N : Node_Id) return Boolean is
982 P : Node_Id := Parent (N);
983 begin
984 while Present (P) loop
985 if Nkind (P) = N_Aggregate then
986 return True;
987 end if;
989 P := Parent (P);
990 end loop;
992 return False;
993 end Within_Aggregate;
995 -- Start of processing for Resolve_Aggregate
997 begin
998 -- Ignore junk empty aggregate resulting from parser error
1000 if No (Expressions (N))
1001 and then No (Component_Associations (N))
1002 and then not Null_Record_Present (N)
1003 then
1004 return;
1006 -- If the aggregate is assigned to a full access variable, we have
1007 -- to prevent a piecemeal assignment even if the aggregate is to be
1008 -- expanded. We create a temporary for the aggregate, and assign the
1009 -- temporary instead, so that the back end can generate an atomic move
1010 -- for it. This is properly an expansion activity but it must be done
1011 -- before resolution because aggregate resolution cannot be done twice.
1013 elsif Expander_Active and then Is_Full_Access_Aggregate (N) then
1014 return;
1015 end if;
1017 -- If the aggregate has box-initialized components, its type must be
1018 -- frozen so that initialization procedures can properly be called
1019 -- in the resolution that follows. The replacement of boxes with
1020 -- initialization calls is properly an expansion activity but it must
1021 -- be done during resolution.
1023 if Expander_Active
1024 and then Present (Component_Associations (N))
1025 then
1026 declare
1027 Comp : Node_Id;
1028 First_Comp : Boolean := True;
1030 begin
1031 Comp := First (Component_Associations (N));
1032 while Present (Comp) loop
1033 if Box_Present (Comp) then
1034 if First_Comp
1035 and then No (Expressions (N))
1036 and then Nkind (First (Choices (Comp))) = N_Others_Choice
1037 and then not Within_Aggregate (N)
1038 then
1039 Others_Box := True;
1040 end if;
1042 Insert_Actions (N, Freeze_Entity (Typ, N));
1043 exit;
1044 end if;
1046 First_Comp := False;
1047 Next (Comp);
1048 end loop;
1049 end;
1050 end if;
1052 -- Check for aggregates not allowed in configurable run-time mode.
1053 -- We allow all cases of aggregates that do not come from source, since
1054 -- these are all assumed to be small (e.g. bounds of a string literal).
1055 -- We also allow aggregates of types we know to be small.
1057 if not Support_Aggregates_On_Target
1058 and then Comes_From_Source (N)
1059 and then (not Known_Static_Esize (Typ)
1060 or else Esize (Typ) > System_Max_Integer_Size)
1061 then
1062 Error_Msg_CRT ("aggregate", N);
1063 end if;
1065 -- Ada 2005 (AI-287): Limited aggregates allowed
1067 -- In an instance, ignore aggregate subcomponents that may be limited,
1068 -- because they originate in view conflicts. If the original aggregate
1069 -- is legal and the actuals are legal, the aggregate itself is legal.
1071 if Is_Limited_Type (Typ)
1072 and then Ada_Version < Ada_2005
1073 and then not In_Instance
1074 then
1075 Error_Msg_N ("aggregate type cannot be limited", N);
1076 Explain_Limited_Type (Typ, N);
1078 elsif Is_Class_Wide_Type (Typ) then
1079 Error_Msg_N ("type of aggregate cannot be class-wide", N);
1081 elsif Typ = Any_String
1082 or else Typ = Any_Composite
1083 then
1084 Error_Msg_N ("no unique type for aggregate", N);
1085 Set_Etype (N, Any_Composite);
1087 elsif Is_Array_Type (Typ) and then Null_Record_Present (N) then
1088 Error_Msg_N ("null record forbidden in array aggregate", N);
1090 elsif Has_Aspect (Typ, Aspect_Aggregate)
1091 and then Ekind (Typ) /= E_Record_Type
1092 and then Ada_Version >= Ada_2022
1093 then
1094 -- Check for Ada 2022 and () aggregate.
1096 if not Is_Homogeneous_Aggregate (N) then
1097 Error_Msg_N ("container aggregate must use '['], not ()", N);
1098 end if;
1100 Resolve_Container_Aggregate (N, Typ);
1102 -- Check Ada 2022 empty aggregate [] initializing a record type that has
1103 -- aspect aggregate; the empty aggregate will be expanded into a call to
1104 -- the empty function specified in the aspect aggregate.
1106 elsif Has_Aspect (Typ, Aspect_Aggregate)
1107 and then Ekind (Typ) = E_Record_Type
1108 and then Is_Homogeneous_Aggregate (N)
1109 and then Is_Empty_List (Expressions (N))
1110 and then Is_Empty_List (Component_Associations (N))
1111 and then Ada_Version >= Ada_2022
1112 then
1113 Resolve_Container_Aggregate (N, Typ);
1115 elsif Is_Record_Type (Typ) then
1116 Resolve_Record_Aggregate (N, Typ);
1118 elsif Is_Array_Type (Typ) then
1120 -- First a special test, for the case of a positional aggregate of
1121 -- characters which can be replaced by a string literal.
1123 -- Do not perform this transformation if this was a string literal
1124 -- to start with, whose components needed constraint checks, or if
1125 -- the component type is non-static, because it will require those
1126 -- checks and be transformed back into an aggregate. If the index
1127 -- type is not Integer the aggregate may represent a user-defined
1128 -- string type but the context might need the original type so we
1129 -- do not perform the transformation at this point.
1131 if Number_Dimensions (Typ) = 1
1132 and then Is_Standard_Character_Type (Component_Type (Typ))
1133 and then No (Component_Associations (N))
1134 and then not Is_Limited_Composite (Typ)
1135 and then not Is_Private_Composite (Typ)
1136 and then not Is_Bit_Packed_Array (Typ)
1137 and then Nkind (Original_Node (Parent (N))) /= N_String_Literal
1138 and then Is_OK_Static_Subtype (Component_Type (Typ))
1139 and then Base_Type (Etype (First_Index (Typ))) =
1140 Base_Type (Standard_Integer)
1141 then
1142 declare
1143 Expr : Node_Id;
1145 begin
1146 Expr := First (Expressions (N));
1147 while Present (Expr) loop
1148 exit when Nkind (Expr) /= N_Character_Literal;
1149 Next (Expr);
1150 end loop;
1152 if No (Expr) then
1153 Start_String;
1155 Expr := First (Expressions (N));
1156 while Present (Expr) loop
1157 Store_String_Char (UI_To_CC (Char_Literal_Value (Expr)));
1158 Next (Expr);
1159 end loop;
1161 Rewrite (N, Make_String_Literal (Loc, End_String));
1163 Analyze_And_Resolve (N, Typ);
1164 return;
1165 end if;
1166 end;
1167 end if;
1169 -- Here if we have a real aggregate to deal with
1171 Array_Aggregate : declare
1172 Aggr_Resolved : Boolean;
1173 Aggr_Typ : constant Entity_Id := Etype (Typ);
1174 -- This is the unconstrained array type, which is the type against
1175 -- which the aggregate is to be resolved. Typ itself is the array
1176 -- type of the context which may not be the same subtype as the
1177 -- subtype for the final aggregate.
1179 Is_Null_Aggr : constant Boolean := Is_Null_Aggregate (N);
1181 begin
1182 -- In the following we determine whether an OTHERS choice is
1183 -- allowed inside the array aggregate. The test checks the context
1184 -- in which the array aggregate occurs. If the context does not
1185 -- permit it, or the aggregate type is unconstrained, an OTHERS
1186 -- choice is not allowed (except that it is always allowed on the
1187 -- right-hand side of an assignment statement; in this case the
1188 -- constrainedness of the type doesn't matter, because an array
1189 -- object is always constrained).
1191 -- If expansion is disabled (generic context, or semantics-only
1192 -- mode) actual subtypes cannot be constructed, and the type of an
1193 -- object may be its unconstrained nominal type. However, if the
1194 -- context is an assignment statement, OTHERS is allowed, because
1195 -- the target of the assignment will have a constrained subtype
1196 -- when fully compiled. Ditto if the context is an initialization
1197 -- procedure where a component may have a predicate function that
1198 -- carries the base type.
1200 -- Note that there is no node for Explicit_Actual_Parameter.
1201 -- To test for this context we therefore have to test for node
1202 -- N_Parameter_Association which itself appears only if there is a
1203 -- formal parameter. Consequently we also need to test for
1204 -- N_Procedure_Call_Statement or N_Function_Call.
1206 -- The context may be an N_Reference node, created by expansion.
1207 -- Legality of the others clause was established in the source,
1208 -- so the context is legal.
1210 Set_Etype (N, Aggr_Typ); -- May be overridden later on
1212 if Is_Null_Aggr then
1213 Set_Etype (N, Typ);
1214 Aggr_Resolved := Resolve_Null_Array_Aggregate (N);
1216 elsif Nkind (Parent (N)) = N_Assignment_Statement
1217 or else Inside_Init_Proc
1218 or else (Is_Constrained (Typ)
1219 and then Nkind (Parent (N)) in
1220 N_Parameter_Association
1221 | N_Function_Call
1222 | N_Procedure_Call_Statement
1223 | N_Generic_Association
1224 | N_Formal_Object_Declaration
1225 | N_Simple_Return_Statement
1226 | N_Object_Declaration
1227 | N_Component_Declaration
1228 | N_Parameter_Specification
1229 | N_Qualified_Expression
1230 | N_Unchecked_Type_Conversion
1231 | N_Reference
1232 | N_Aggregate
1233 | N_Extension_Aggregate
1234 | N_Component_Association
1235 | N_Case_Expression_Alternative
1236 | N_If_Expression
1237 | N_Expression_With_Actions)
1238 then
1239 Aggr_Resolved :=
1240 Resolve_Array_Aggregate
1242 Index => First_Index (Aggr_Typ),
1243 Index_Constr => First_Index (Typ),
1244 Component_Typ => Component_Type (Typ),
1245 Others_Allowed => True);
1246 else
1247 Aggr_Resolved :=
1248 Resolve_Array_Aggregate
1250 Index => First_Index (Aggr_Typ),
1251 Index_Constr => First_Index (Aggr_Typ),
1252 Component_Typ => Component_Type (Typ),
1253 Others_Allowed => False);
1254 end if;
1256 if not Aggr_Resolved then
1258 -- A parenthesized expression may have been intended as an
1259 -- aggregate, leading to a type error when analyzing the
1260 -- component. This can also happen for a nested component
1261 -- (see Analyze_Aggr_Expr).
1263 if Paren_Count (N) > 0 then
1264 Error_Msg_N
1265 ("positional aggregate cannot have one component", N);
1266 end if;
1268 Aggr_Subtyp := Any_Composite;
1270 else
1271 Aggr_Subtyp := Array_Aggr_Subtype (N, Typ);
1272 end if;
1274 Set_Etype (N, Aggr_Subtyp);
1275 end Array_Aggregate;
1277 elsif Is_Private_Type (Typ)
1278 and then Present (Full_View (Typ))
1279 and then (In_Inlined_Body or In_Instance_Body)
1280 and then Is_Composite_Type (Full_View (Typ))
1281 then
1282 Resolve (N, Full_View (Typ));
1284 else
1285 Error_Msg_N ("illegal context for aggregate", N);
1286 end if;
1288 -- If we can determine statically that the evaluation of the aggregate
1289 -- raises Constraint_Error, then replace the aggregate with an
1290 -- N_Raise_Constraint_Error node, but set the Etype to the right
1291 -- aggregate subtype. Gigi needs this.
1293 if Raises_Constraint_Error (N) then
1294 Aggr_Subtyp := Etype (N);
1295 Rewrite (N,
1296 Make_Raise_Constraint_Error (Loc, Reason => CE_Range_Check_Failed));
1297 Set_Raises_Constraint_Error (N);
1298 Set_Etype (N, Aggr_Subtyp);
1299 Set_Analyzed (N);
1300 end if;
1302 if Warn_On_No_Value_Assigned
1303 and then Others_Box
1304 and then not Is_Fully_Initialized_Type (Etype (N))
1305 then
1306 Error_Msg_N ("?v?aggregate not fully initialized", N);
1307 end if;
1309 Check_Function_Writable_Actuals (N);
1310 end Resolve_Aggregate;
1312 -----------------------------
1313 -- Resolve_Array_Aggregate --
1314 -----------------------------
1316 function Resolve_Array_Aggregate
1317 (N : Node_Id;
1318 Index : Node_Id;
1319 Index_Constr : Node_Id;
1320 Component_Typ : Entity_Id;
1321 Others_Allowed : Boolean) return Boolean
1323 Loc : constant Source_Ptr := Sloc (N);
1325 Failure : constant Boolean := False;
1326 Success : constant Boolean := True;
1328 Index_Typ : constant Entity_Id := Etype (Index);
1329 Index_Typ_Low : constant Node_Id := Type_Low_Bound (Index_Typ);
1330 Index_Typ_High : constant Node_Id := Type_High_Bound (Index_Typ);
1331 -- The type of the index corresponding to the array sub-aggregate along
1332 -- with its low and upper bounds.
1334 Index_Base : constant Entity_Id := Base_Type (Index_Typ);
1335 Index_Base_Low : constant Node_Id := Type_Low_Bound (Index_Base);
1336 Index_Base_High : constant Node_Id := Type_High_Bound (Index_Base);
1337 -- Ditto for the base type
1339 Others_Present : Boolean := False;
1341 Nb_Choices : Nat := 0;
1342 -- Contains the overall number of named choices in this sub-aggregate
1344 function Add (Val : Uint; To : Node_Id) return Node_Id;
1345 -- Creates a new expression node where Val is added to expression To.
1346 -- Tries to constant fold whenever possible. To must be an already
1347 -- analyzed expression.
1349 procedure Check_Bound (BH : Node_Id; AH : in out Node_Id);
1350 -- Checks that AH (the upper bound of an array aggregate) is less than
1351 -- or equal to BH (the upper bound of the index base type). If the check
1352 -- fails, a warning is emitted, the Raises_Constraint_Error flag of N is
1353 -- set, and AH is replaced with a duplicate of BH.
1355 procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id);
1356 -- Checks that range AL .. AH is compatible with range L .. H. Emits a
1357 -- warning if not and sets the Raises_Constraint_Error flag in N.
1359 procedure Check_Length (L, H : Node_Id; Len : Uint);
1360 -- Checks that range L .. H contains at least Len elements. Emits a
1361 -- warning if not and sets the Raises_Constraint_Error flag in N.
1363 function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean;
1364 -- Returns True if range L .. H is dynamic or null
1366 procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean);
1367 -- Given expression node From, this routine sets OK to False if it
1368 -- cannot statically evaluate From. Otherwise it stores this static
1369 -- value into Value.
1371 function Resolve_Aggr_Expr
1372 (Expr : Node_Id;
1373 Single_Elmt : Boolean) return Boolean;
1374 -- Resolves aggregate expression Expr. Returns False if resolution
1375 -- fails. If Single_Elmt is set to False, the expression Expr may be
1376 -- used to initialize several array aggregate elements (this can happen
1377 -- for discrete choices such as "L .. H => Expr" or the OTHERS choice).
1378 -- In this event we do not resolve Expr unless expansion is disabled.
1379 -- To know why, see the DELAYED COMPONENT RESOLUTION note above.
1381 -- NOTE: In the case of "... => <>", we pass the N_Component_Association
1382 -- node as Expr, since there is no Expression and we need a Sloc for the
1383 -- error message.
1385 procedure Resolve_Iterated_Component_Association
1386 (N : Node_Id;
1387 Index_Typ : Entity_Id);
1388 -- For AI12-061
1390 procedure Warn_On_Null_Component_Association (Expr : Node_Id);
1391 -- Expr is either a conditional expression or a case expression of an
1392 -- iterated component association initializing the aggregate N with
1393 -- components that can never be null. Report warning on associations
1394 -- that may initialize some component with a null value.
1396 ---------
1397 -- Add --
1398 ---------
1400 function Add (Val : Uint; To : Node_Id) return Node_Id is
1401 Expr_Pos : Node_Id;
1402 Expr : Node_Id;
1403 To_Pos : Node_Id;
1405 begin
1406 if Raises_Constraint_Error (To) then
1407 return To;
1408 end if;
1410 -- First test if we can do constant folding
1412 if Compile_Time_Known_Value (To)
1413 or else Nkind (To) = N_Integer_Literal
1414 then
1415 Expr_Pos := Make_Integer_Literal (Loc, Expr_Value (To) + Val);
1416 Set_Is_Static_Expression (Expr_Pos);
1417 Set_Etype (Expr_Pos, Etype (To));
1418 Set_Analyzed (Expr_Pos, Analyzed (To));
1420 if not Is_Enumeration_Type (Index_Typ) then
1421 Expr := Expr_Pos;
1423 -- If we are dealing with enumeration return
1424 -- Index_Typ'Val (Expr_Pos)
1426 else
1427 Expr :=
1428 Make_Attribute_Reference
1429 (Loc,
1430 Prefix => New_Occurrence_Of (Index_Typ, Loc),
1431 Attribute_Name => Name_Val,
1432 Expressions => New_List (Expr_Pos));
1433 end if;
1435 return Expr;
1436 end if;
1438 -- If we are here no constant folding possible
1440 if not Is_Enumeration_Type (Index_Base) then
1441 Expr :=
1442 Make_Op_Add (Loc,
1443 Left_Opnd => Duplicate_Subexpr (To),
1444 Right_Opnd => Make_Integer_Literal (Loc, Val));
1446 -- If we are dealing with enumeration return
1447 -- Index_Typ'Val (Index_Typ'Pos (To) + Val)
1449 else
1450 To_Pos :=
1451 Make_Attribute_Reference
1452 (Loc,
1453 Prefix => New_Occurrence_Of (Index_Typ, Loc),
1454 Attribute_Name => Name_Pos,
1455 Expressions => New_List (Duplicate_Subexpr (To)));
1457 Expr_Pos :=
1458 Make_Op_Add (Loc,
1459 Left_Opnd => To_Pos,
1460 Right_Opnd => Make_Integer_Literal (Loc, Val));
1462 Expr :=
1463 Make_Attribute_Reference
1464 (Loc,
1465 Prefix => New_Occurrence_Of (Index_Typ, Loc),
1466 Attribute_Name => Name_Val,
1467 Expressions => New_List (Expr_Pos));
1469 -- If the index type has a non standard representation, the
1470 -- attributes 'Val and 'Pos expand into function calls and the
1471 -- resulting expression is considered non-safe for reevaluation
1472 -- by the backend. Relocate it into a constant temporary in order
1473 -- to make it safe for reevaluation.
1475 if Has_Non_Standard_Rep (Etype (N)) then
1476 declare
1477 Def_Id : Entity_Id;
1479 begin
1480 Def_Id := Make_Temporary (Loc, 'R', Expr);
1481 Set_Etype (Def_Id, Index_Typ);
1482 Insert_Action (N,
1483 Make_Object_Declaration (Loc,
1484 Defining_Identifier => Def_Id,
1485 Object_Definition =>
1486 New_Occurrence_Of (Index_Typ, Loc),
1487 Constant_Present => True,
1488 Expression => Relocate_Node (Expr)));
1490 Expr := New_Occurrence_Of (Def_Id, Loc);
1491 end;
1492 end if;
1493 end if;
1495 return Expr;
1496 end Add;
1498 -----------------
1499 -- Check_Bound --
1500 -----------------
1502 procedure Check_Bound (BH : Node_Id; AH : in out Node_Id) is
1503 Val_BH : Uint;
1504 Val_AH : Uint;
1506 OK_BH : Boolean;
1507 OK_AH : Boolean;
1509 begin
1510 Get (Value => Val_BH, From => BH, OK => OK_BH);
1511 Get (Value => Val_AH, From => AH, OK => OK_AH);
1513 if OK_BH and then OK_AH and then Val_BH < Val_AH then
1514 Set_Raises_Constraint_Error (N);
1515 Error_Msg_Warn := SPARK_Mode /= On;
1516 Error_Msg_N ("upper bound out of range<<", AH);
1517 Error_Msg_N ("\Constraint_Error [<<", AH);
1519 -- You need to set AH to BH or else in the case of enumerations
1520 -- indexes we will not be able to resolve the aggregate bounds.
1522 AH := Duplicate_Subexpr (BH);
1523 end if;
1524 end Check_Bound;
1526 ------------------
1527 -- Check_Bounds --
1528 ------------------
1530 procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id) is
1531 Val_L : Uint;
1532 Val_H : Uint;
1533 Val_AL : Uint;
1534 Val_AH : Uint;
1536 OK_L : Boolean;
1537 OK_H : Boolean;
1539 OK_AL : Boolean;
1540 OK_AH : Boolean;
1541 pragma Warnings (Off, OK_AL);
1542 pragma Warnings (Off, OK_AH);
1544 begin
1545 if Raises_Constraint_Error (N)
1546 or else Dynamic_Or_Null_Range (AL, AH)
1547 then
1548 return;
1549 end if;
1551 Get (Value => Val_L, From => L, OK => OK_L);
1552 Get (Value => Val_H, From => H, OK => OK_H);
1554 Get (Value => Val_AL, From => AL, OK => OK_AL);
1555 Get (Value => Val_AH, From => AH, OK => OK_AH);
1557 if OK_L and then Val_L > Val_AL then
1558 Set_Raises_Constraint_Error (N);
1559 Error_Msg_Warn := SPARK_Mode /= On;
1560 Error_Msg_N ("lower bound of aggregate out of range<<", N);
1561 Error_Msg_N ("\Constraint_Error [<<", N);
1562 end if;
1564 if OK_H and then Val_H < Val_AH then
1565 Set_Raises_Constraint_Error (N);
1566 Error_Msg_Warn := SPARK_Mode /= On;
1567 Error_Msg_N ("upper bound of aggregate out of range<<", N);
1568 Error_Msg_N ("\Constraint_Error [<<", N);
1569 end if;
1570 end Check_Bounds;
1572 ------------------
1573 -- Check_Length --
1574 ------------------
1576 procedure Check_Length (L, H : Node_Id; Len : Uint) is
1577 Val_L : Uint;
1578 Val_H : Uint;
1580 OK_L : Boolean;
1581 OK_H : Boolean;
1583 Range_Len : Uint;
1585 begin
1586 if Raises_Constraint_Error (N) then
1587 return;
1588 end if;
1590 Get (Value => Val_L, From => L, OK => OK_L);
1591 Get (Value => Val_H, From => H, OK => OK_H);
1593 if not OK_L or else not OK_H then
1594 return;
1595 end if;
1597 -- If null range length is zero
1599 if Val_L > Val_H then
1600 Range_Len := Uint_0;
1601 else
1602 Range_Len := Val_H - Val_L + 1;
1603 end if;
1605 if Range_Len < Len then
1606 Set_Raises_Constraint_Error (N);
1607 Error_Msg_Warn := SPARK_Mode /= On;
1608 Error_Msg_N ("too many elements<<", N);
1609 Error_Msg_N ("\Constraint_Error [<<", N);
1610 end if;
1611 end Check_Length;
1613 ---------------------------
1614 -- Dynamic_Or_Null_Range --
1615 ---------------------------
1617 function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean is
1618 Val_L : Uint;
1619 Val_H : Uint;
1621 OK_L : Boolean;
1622 OK_H : Boolean;
1624 begin
1625 Get (Value => Val_L, From => L, OK => OK_L);
1626 Get (Value => Val_H, From => H, OK => OK_H);
1628 return not OK_L or else not OK_H
1629 or else not Is_OK_Static_Expression (L)
1630 or else not Is_OK_Static_Expression (H)
1631 or else Val_L > Val_H;
1632 end Dynamic_Or_Null_Range;
1634 ---------
1635 -- Get --
1636 ---------
1638 procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean) is
1639 begin
1640 OK := True;
1642 if Compile_Time_Known_Value (From) then
1643 Value := Expr_Value (From);
1645 -- If expression From is something like Some_Type'Val (10) then
1646 -- Value = 10.
1648 elsif Nkind (From) = N_Attribute_Reference
1649 and then Attribute_Name (From) = Name_Val
1650 and then Compile_Time_Known_Value (First (Expressions (From)))
1651 then
1652 Value := Expr_Value (First (Expressions (From)));
1653 else
1654 Value := Uint_0;
1655 OK := False;
1656 end if;
1657 end Get;
1659 -----------------------
1660 -- Resolve_Aggr_Expr --
1661 -----------------------
1663 function Resolve_Aggr_Expr
1664 (Expr : Node_Id;
1665 Single_Elmt : Boolean) return Boolean
1667 Nxt_Ind : constant Node_Id := Next_Index (Index);
1668 Nxt_Ind_Constr : constant Node_Id := Next_Index (Index_Constr);
1669 -- Index is the current index corresponding to the expression
1671 Resolution_OK : Boolean := True;
1672 -- Set to False if resolution of the expression failed
1674 begin
1675 -- Defend against previous errors
1677 if Nkind (Expr) = N_Error
1678 or else Error_Posted (Expr)
1679 then
1680 return True;
1681 end if;
1683 -- If the array type against which we are resolving the aggregate
1684 -- has several dimensions, the expressions nested inside the
1685 -- aggregate must be further aggregates (or strings).
1687 if Present (Nxt_Ind) then
1688 if Nkind (Expr) /= N_Aggregate then
1690 -- A string literal can appear where a one-dimensional array
1691 -- of characters is expected. If the literal looks like an
1692 -- operator, it is still an operator symbol, which will be
1693 -- transformed into a string when analyzed.
1695 if Is_Character_Type (Component_Typ)
1696 and then No (Next_Index (Nxt_Ind))
1697 and then Nkind (Expr) in N_String_Literal | N_Operator_Symbol
1698 then
1699 -- A string literal used in a multidimensional array
1700 -- aggregate in place of the final one-dimensional
1701 -- aggregate must not be enclosed in parentheses.
1703 if Paren_Count (Expr) /= 0 then
1704 Error_Msg_N ("no parenthesis allowed here", Expr);
1705 end if;
1707 Make_String_Into_Aggregate (Expr);
1709 else
1710 Error_Msg_N ("nested array aggregate expected", Expr);
1712 -- If the expression is parenthesized, this may be
1713 -- a missing component association for a 1-aggregate.
1715 if Paren_Count (Expr) > 0 then
1716 Error_Msg_N
1717 ("\if single-component aggregate is intended, "
1718 & "write e.g. (1 ='> ...)", Expr);
1719 end if;
1721 return Failure;
1722 end if;
1723 end if;
1725 -- If it's "... => <>", nothing to resolve
1727 if Nkind (Expr) = N_Component_Association then
1728 pragma Assert (Box_Present (Expr));
1729 return Success;
1730 end if;
1732 -- Ada 2005 (AI-231): Propagate the type to the nested aggregate.
1733 -- Required to check the null-exclusion attribute (if present).
1734 -- This value may be overridden later on.
1736 Set_Etype (Expr, Etype (N));
1738 Resolution_OK := Resolve_Array_Aggregate
1739 (Expr, Nxt_Ind, Nxt_Ind_Constr, Component_Typ, Others_Allowed);
1741 else
1742 -- If it's "... => <>", nothing to resolve
1744 if Nkind (Expr) = N_Component_Association then
1745 pragma Assert (Box_Present (Expr));
1746 return Success;
1747 end if;
1749 -- Do not resolve the expressions of discrete or others choices
1750 -- unless the expression covers a single component, or the
1751 -- expander is inactive.
1753 -- In SPARK mode, expressions that can perform side effects will
1754 -- be recognized by the gnat2why back-end, and the whole
1755 -- subprogram will be ignored. So semantic analysis can be
1756 -- performed safely.
1758 if Single_Elmt
1759 or else not Expander_Active
1760 or else In_Spec_Expression
1761 then
1762 Analyze_And_Resolve (Expr, Component_Typ);
1763 Check_Expr_OK_In_Limited_Aggregate (Expr);
1764 Check_Non_Static_Context (Expr);
1765 Aggregate_Constraint_Checks (Expr, Component_Typ);
1766 Check_Unset_Reference (Expr);
1767 end if;
1768 end if;
1770 -- If an aggregate component has a type with predicates, an explicit
1771 -- predicate check must be applied, as for an assignment statement,
1772 -- because the aggregate might not be expanded into individual
1773 -- component assignments. If the expression covers several components
1774 -- the analysis and the predicate check take place later.
1776 if Has_Predicates (Component_Typ)
1777 and then Analyzed (Expr)
1778 then
1779 Apply_Predicate_Check (Expr, Component_Typ);
1780 end if;
1782 if Raises_Constraint_Error (Expr)
1783 and then Nkind (Parent (Expr)) /= N_Component_Association
1784 then
1785 Set_Raises_Constraint_Error (N);
1786 end if;
1788 -- If the expression has been marked as requiring a range check,
1789 -- then generate it here. It's a bit odd to be generating such
1790 -- checks in the analyzer, but harmless since Generate_Range_Check
1791 -- does nothing (other than making sure Do_Range_Check is set) if
1792 -- the expander is not active.
1794 if Do_Range_Check (Expr) then
1795 Generate_Range_Check (Expr, Component_Typ, CE_Range_Check_Failed);
1796 end if;
1798 return Resolution_OK;
1799 end Resolve_Aggr_Expr;
1801 --------------------------------------------
1802 -- Resolve_Iterated_Component_Association --
1803 --------------------------------------------
1805 procedure Resolve_Iterated_Component_Association
1806 (N : Node_Id;
1807 Index_Typ : Entity_Id)
1809 Loc : constant Source_Ptr := Sloc (N);
1810 Id : constant Entity_Id := Defining_Identifier (N);
1812 -----------------------
1813 -- Remove_References --
1814 -----------------------
1816 function Remove_Reference (N : Node_Id) return Traverse_Result;
1817 -- Remove reference to the entity Id after analysis, so it can be
1818 -- properly reanalyzed after construct is expanded into a loop.
1820 function Remove_Reference (N : Node_Id) return Traverse_Result is
1821 begin
1822 if Nkind (N) = N_Identifier
1823 and then Present (Entity (N))
1824 and then Entity (N) = Id
1825 then
1826 Set_Entity (N, Empty);
1827 Set_Etype (N, Empty);
1828 end if;
1829 Set_Analyzed (N, False);
1830 return OK;
1831 end Remove_Reference;
1833 procedure Remove_References is new Traverse_Proc (Remove_Reference);
1835 -- Local variables
1837 Choice : Node_Id;
1838 Dummy : Boolean;
1839 Scop : Entity_Id;
1840 Expr : constant Node_Id := Expression (N);
1842 -- Start of processing for Resolve_Iterated_Component_Association
1844 begin
1845 Error_Msg_Ada_2022_Feature ("iterated component", Loc);
1847 -- Create a scope in which to introduce an index, to make it visible
1848 -- for the analysis of component expression.
1850 Scop := New_Internal_Entity (E_Loop, Current_Scope, Loc, 'L');
1851 Set_Etype (Scop, Standard_Void_Type);
1852 Set_Parent (Scop, Parent (N));
1853 Push_Scope (Scop);
1855 -- If there is iterator specification, then its preanalysis will make
1856 -- the index visible.
1858 if Present (Iterator_Specification (N)) then
1859 Preanalyze (Iterator_Specification (N));
1861 -- Otherwise, analyze discrete choices and make the index visible
1863 else
1864 -- Insert index name into current scope but don't decorate it yet,
1865 -- so that a premature usage of this name in discrete choices will
1866 -- be nicely diagnosed.
1868 Enter_Name (Id);
1870 Choice := First (Discrete_Choices (N));
1872 while Present (Choice) loop
1873 if Nkind (Choice) = N_Others_Choice then
1874 Others_Present := True;
1876 else
1877 Analyze (Choice);
1879 -- Choice can be a subtype name, a range, or an expression
1881 if Is_Entity_Name (Choice)
1882 and then Is_Type (Entity (Choice))
1883 and then
1884 Base_Type (Entity (Choice)) = Base_Type (Index_Typ)
1885 then
1886 null;
1888 else
1889 Analyze_And_Resolve (Choice, Index_Typ);
1890 end if;
1891 end if;
1893 Next (Choice);
1894 end loop;
1896 -- Decorate the index variable
1898 Set_Etype (Id, Index_Typ);
1899 Mutate_Ekind (Id, E_Variable);
1900 Set_Is_Not_Self_Hidden (Id);
1901 Set_Scope (Id, Scop);
1902 end if;
1904 -- Analyze expression without expansion, to verify legality.
1905 -- When generating code, we then remove references to the index
1906 -- variable, because the expression will be analyzed anew after
1907 -- rewritting as a loop with a new index variable; when not
1908 -- generating code we leave the analyzed expression as it is.
1910 Dummy := Resolve_Aggr_Expr (Expr, Single_Elmt => False);
1912 if Operating_Mode /= Check_Semantics then
1913 Remove_References (Expr);
1914 end if;
1916 -- An iterated_component_association may appear in a nested
1917 -- aggregate for a multidimensional structure: preserve the bounds
1918 -- computed for the expression, as well as the anonymous array
1919 -- type generated for it; both are needed during array expansion.
1921 if Nkind (Expr) = N_Aggregate then
1922 Set_Aggregate_Bounds (Expression (N), Aggregate_Bounds (Expr));
1923 Set_Etype (Expression (N), Etype (Expr));
1924 end if;
1926 End_Scope;
1927 end Resolve_Iterated_Component_Association;
1929 ----------------------------------------
1930 -- Warn_On_Null_Component_Association --
1931 ----------------------------------------
1933 procedure Warn_On_Null_Component_Association (Expr : Node_Id) is
1934 Comp_Typ : constant Entity_Id := Component_Type (Etype (N));
1936 procedure Check_Case_Expr (N : Node_Id);
1937 -- Check if a case expression may initialize some component with a
1938 -- null value.
1940 procedure Check_Cond_Expr (N : Node_Id);
1941 -- Check if a conditional expression may initialize some component
1942 -- with a null value.
1944 procedure Check_Expr (Expr : Node_Id);
1945 -- Check if an expression may initialize some component with a
1946 -- null value.
1948 procedure Warn_On_Null_Expression_And_Rewrite (Null_Expr : Node_Id);
1949 -- Report warning on known null expression and replace the expression
1950 -- by a raise constraint error node.
1952 ---------------------
1953 -- Check_Case_Expr --
1954 ---------------------
1956 procedure Check_Case_Expr (N : Node_Id) is
1957 Alt_Node : Node_Id := First (Alternatives (N));
1959 begin
1960 while Present (Alt_Node) loop
1961 Check_Expr (Expression (Alt_Node));
1962 Next (Alt_Node);
1963 end loop;
1964 end Check_Case_Expr;
1966 ---------------------
1967 -- Check_Cond_Expr --
1968 ---------------------
1970 procedure Check_Cond_Expr (N : Node_Id) is
1971 If_Expr : Node_Id := N;
1972 Then_Expr : Node_Id;
1973 Else_Expr : Node_Id;
1975 begin
1976 Then_Expr := Next (First (Expressions (If_Expr)));
1977 Else_Expr := Next (Then_Expr);
1979 Check_Expr (Then_Expr);
1981 -- Process elsif parts (if any)
1983 while Nkind (Else_Expr) = N_If_Expression loop
1984 If_Expr := Else_Expr;
1985 Then_Expr := Next (First (Expressions (If_Expr)));
1986 Else_Expr := Next (Then_Expr);
1988 Check_Expr (Then_Expr);
1989 end loop;
1991 if Known_Null (Else_Expr) then
1992 Warn_On_Null_Expression_And_Rewrite (Else_Expr);
1993 end if;
1994 end Check_Cond_Expr;
1996 ----------------
1997 -- Check_Expr --
1998 ----------------
2000 procedure Check_Expr (Expr : Node_Id) is
2001 begin
2002 if Known_Null (Expr) then
2003 Warn_On_Null_Expression_And_Rewrite (Expr);
2005 elsif Nkind (Expr) = N_If_Expression then
2006 Check_Cond_Expr (Expr);
2008 elsif Nkind (Expr) = N_Case_Expression then
2009 Check_Case_Expr (Expr);
2010 end if;
2011 end Check_Expr;
2013 -----------------------------------------
2014 -- Warn_On_Null_Expression_And_Rewrite --
2015 -----------------------------------------
2017 procedure Warn_On_Null_Expression_And_Rewrite (Null_Expr : Node_Id) is
2018 begin
2019 Error_Msg_N
2020 ("(Ada 2005) NULL not allowed in null-excluding component??",
2021 Null_Expr);
2022 Error_Msg_N
2023 ("\Constraint_Error might be raised at run time??", Null_Expr);
2025 -- We cannot use Apply_Compile_Time_Constraint_Error because in
2026 -- some cases the components are rewritten and the runtime error
2027 -- would be missed.
2029 Rewrite (Null_Expr,
2030 Make_Raise_Constraint_Error (Sloc (Null_Expr),
2031 Reason => CE_Access_Check_Failed));
2033 Set_Etype (Null_Expr, Comp_Typ);
2034 Set_Analyzed (Null_Expr);
2035 end Warn_On_Null_Expression_And_Rewrite;
2037 -- Start of processing for Warn_On_Null_Component_Association
2039 begin
2040 pragma Assert (Can_Never_Be_Null (Comp_Typ));
2042 case Nkind (Expr) is
2043 when N_If_Expression =>
2044 Check_Cond_Expr (Expr);
2046 when N_Case_Expression =>
2047 Check_Case_Expr (Expr);
2049 when others =>
2050 pragma Assert (False);
2051 null;
2052 end case;
2053 end Warn_On_Null_Component_Association;
2055 -- Local variables
2057 Assoc : Node_Id;
2058 Choice : Node_Id;
2059 Expr : Node_Id;
2060 Discard : Node_Id;
2062 Aggr_Low : Node_Id := Empty;
2063 Aggr_High : Node_Id := Empty;
2064 -- The actual low and high bounds of this sub-aggregate
2066 Case_Table_Size : Nat;
2067 -- Contains the size of the case table needed to sort aggregate choices
2069 Choices_Low : Node_Id := Empty;
2070 Choices_High : Node_Id := Empty;
2071 -- The lowest and highest discrete choices values for a named aggregate
2073 Delete_Choice : Boolean;
2074 -- Used when replacing a subtype choice with predicate by a list
2076 Has_Iterator_Specifications : Boolean := False;
2077 -- Flag to indicate that all named associations are iterated component
2078 -- associations with iterator specifications, in which case the
2079 -- expansion will create two loops: one to evaluate the size and one
2080 -- to generate the elements (4.3.3 (20.2/5)).
2082 Nb_Elements : Uint := Uint_0;
2083 -- The number of elements in a positional aggregate
2085 Nb_Discrete_Choices : Nat := 0;
2086 -- The overall number of discrete choices (not counting others choice)
2088 -- Start of processing for Resolve_Array_Aggregate
2090 begin
2091 -- Ignore junk empty aggregate resulting from parser error
2093 if No (Expressions (N))
2094 and then No (Component_Associations (N))
2095 and then not Null_Record_Present (N)
2096 then
2097 return Failure;
2098 end if;
2100 -- Disable the warning for GNAT Mode to allow for easier transition.
2102 if Ada_Version_Explicit >= Ada_2022
2103 and then Warn_On_Obsolescent_Feature
2104 and then not GNAT_Mode
2105 and then not Is_Homogeneous_Aggregate (N)
2106 and then not Is_Enum_Array_Aggregate (N)
2107 and then Is_Parenthesis_Aggregate (N)
2108 and then Nkind (Parent (N)) /= N_Qualified_Expression
2109 and then Comes_From_Source (N)
2110 then
2111 Error_Msg_N
2112 ("?j?array aggregate using () is an" &
2113 " obsolescent syntax, use '['] instead", N);
2114 end if;
2116 -- STEP 1: make sure the aggregate is correctly formatted
2118 if Is_Null_Aggregate (N) then
2119 null;
2121 elsif Present (Component_Associations (N)) then
2123 -- Verify that all or none of the component associations
2124 -- include an iterator specification.
2126 Assoc := First (Component_Associations (N));
2127 if Nkind (Assoc) = N_Iterated_Component_Association
2128 and then Present (Iterator_Specification (Assoc))
2129 then
2130 -- All other component associations must have an iterator spec.
2132 Next (Assoc);
2133 while Present (Assoc) loop
2134 if Nkind (Assoc) /= N_Iterated_Component_Association
2135 or else No (Iterator_Specification (Assoc))
2136 then
2137 Error_Msg_N ("mixed iterated component association"
2138 & " (RM 4.3.3 (17.1/5))",
2139 Assoc);
2140 return Failure;
2141 end if;
2143 Next (Assoc);
2144 end loop;
2146 Has_Iterator_Specifications := True;
2148 else
2149 -- or none of them do.
2151 Next (Assoc);
2152 while Present (Assoc) loop
2153 if Nkind (Assoc) = N_Iterated_Component_Association
2154 and then Present (Iterator_Specification (Assoc))
2155 then
2156 Error_Msg_N ("mixed iterated component association"
2157 & " (RM 4.3.3 (17.1/5))",
2158 Assoc);
2159 return Failure;
2160 end if;
2162 Next (Assoc);
2163 end loop;
2165 end if;
2167 Assoc := First (Component_Associations (N));
2168 while Present (Assoc) loop
2169 if Nkind (Assoc) = N_Iterated_Component_Association then
2170 Resolve_Iterated_Component_Association (Assoc, Index_Typ);
2172 elsif Nkind (Assoc) /= N_Component_Association then
2173 Error_Msg_N
2174 ("invalid component association for aggregate", Assoc);
2175 return Failure;
2176 end if;
2178 Choice := First (Choice_List (Assoc));
2179 Delete_Choice := False;
2180 while Present (Choice) loop
2181 if Nkind (Choice) = N_Others_Choice then
2182 Others_Present := True;
2184 if Choice /= First (Choice_List (Assoc))
2185 or else Present (Next (Choice))
2186 then
2187 Error_Msg_N
2188 ("OTHERS must appear alone in a choice list", Choice);
2189 return Failure;
2190 end if;
2192 if Present (Next (Assoc)) then
2193 Error_Msg_N
2194 ("OTHERS must appear last in an aggregate", Choice);
2195 return Failure;
2196 end if;
2198 if Ada_Version = Ada_83
2199 and then Assoc /= First (Component_Associations (N))
2200 and then Nkind (Parent (N)) in
2201 N_Assignment_Statement | N_Object_Declaration
2202 then
2203 Error_Msg_N
2204 ("(Ada 83) illegal context for OTHERS choice", N);
2205 end if;
2207 elsif Is_Entity_Name (Choice) then
2208 Analyze (Choice);
2210 declare
2211 E : constant Entity_Id := Entity (Choice);
2212 New_Cs : List_Id;
2213 P : Node_Id;
2214 C : Node_Id;
2216 begin
2217 if Is_Type (E) and then Has_Predicates (E) then
2218 Freeze_Before (N, E);
2220 if Has_Dynamic_Predicate_Aspect (E)
2221 or else Has_Ghost_Predicate_Aspect (E)
2222 then
2223 Error_Msg_NE
2224 ("subtype& has non-static predicate, not allowed "
2225 & "in aggregate choice", Choice, E);
2227 elsif not Is_OK_Static_Subtype (E) then
2228 Error_Msg_NE
2229 ("non-static subtype& has predicate, not allowed "
2230 & "in aggregate choice", Choice, E);
2231 end if;
2233 -- If the subtype has a static predicate, replace the
2234 -- original choice with the list of individual values
2235 -- covered by the predicate.
2236 -- This should be deferred to expansion time ???
2238 if Present (Static_Discrete_Predicate (E)) then
2239 Delete_Choice := True;
2241 New_Cs := New_List;
2242 P := First (Static_Discrete_Predicate (E));
2243 while Present (P) loop
2244 C := New_Copy (P);
2245 Set_Sloc (C, Sloc (Choice));
2246 Append_To (New_Cs, C);
2247 Next (P);
2248 end loop;
2250 Insert_List_After (Choice, New_Cs);
2251 end if;
2252 end if;
2253 end;
2254 end if;
2256 Nb_Choices := Nb_Choices + 1;
2258 declare
2259 C : constant Node_Id := Choice;
2261 begin
2262 Next (Choice);
2264 if Delete_Choice then
2265 Remove (C);
2266 Nb_Choices := Nb_Choices - 1;
2267 Delete_Choice := False;
2268 end if;
2269 end;
2270 end loop;
2272 Next (Assoc);
2273 end loop;
2274 end if;
2276 -- At this point we know that the others choice, if present, is by
2277 -- itself and appears last in the aggregate. Check if we have mixed
2278 -- positional and discrete associations (other than the others choice).
2280 if Present (Expressions (N))
2281 and then (Nb_Choices > 1
2282 or else (Nb_Choices = 1 and then not Others_Present))
2283 then
2284 Error_Msg_N
2285 ("cannot mix named and positional associations in array aggregate",
2286 First (Choice_List (First (Component_Associations (N)))));
2287 return Failure;
2288 end if;
2290 -- Test for the validity of an others choice if present
2292 if Others_Present and then not Others_Allowed then
2293 declare
2294 Others_N : constant Node_Id :=
2295 First (Choice_List (First (Component_Associations (N))));
2296 begin
2297 Error_Msg_N ("OTHERS choice not allowed here", Others_N);
2298 Error_Msg_N ("\qualify the aggregate with a constrained subtype "
2299 & "to provide bounds for it", Others_N);
2300 return Failure;
2301 end;
2302 end if;
2304 -- Protect against cascaded errors
2306 if Etype (Index_Typ) = Any_Type then
2307 return Failure;
2308 end if;
2310 -- STEP 2: Process named components
2312 if No (Expressions (N)) then
2313 if Others_Present then
2314 Case_Table_Size := Nb_Choices - 1;
2315 else
2316 Case_Table_Size := Nb_Choices;
2317 end if;
2319 Step_2 : declare
2320 function Empty_Range (A : Node_Id) return Boolean;
2321 -- If an association covers an empty range, some warnings on the
2322 -- expression of the association can be disabled.
2324 -----------------
2325 -- Empty_Range --
2326 -----------------
2328 function Empty_Range (A : Node_Id) return Boolean is
2329 R : Node_Id;
2331 begin
2332 if Nkind (A) = N_Iterated_Component_Association then
2333 R := First (Discrete_Choices (A));
2334 else
2335 R := First (Choices (A));
2336 end if;
2338 return No (Next (R))
2339 and then Nkind (R) = N_Range
2340 and then Compile_Time_Compare
2341 (Low_Bound (R), High_Bound (R), False) = GT;
2342 end Empty_Range;
2344 -- Local variables
2346 Low : Node_Id;
2347 High : Node_Id;
2348 -- Denote the lowest and highest values in an aggregate choice
2350 S_Low : Node_Id := Empty;
2351 S_High : Node_Id := Empty;
2352 -- if a choice in an aggregate is a subtype indication these
2353 -- denote the lowest and highest values of the subtype
2355 Table : Case_Table_Type (1 .. Case_Table_Size);
2356 -- Used to sort all the different choice values
2358 Single_Choice : Boolean;
2359 -- Set to true every time there is a single discrete choice in a
2360 -- discrete association
2362 Prev_Nb_Discrete_Choices : Nat;
2363 -- Used to keep track of the number of discrete choices in the
2364 -- current association.
2366 Errors_Posted_On_Choices : Boolean := False;
2367 -- Keeps track of whether any choices have semantic errors
2369 -- Start of processing for Step_2
2371 begin
2372 -- STEP 2 (A): Check discrete choices validity
2373 -- No need if this is an element iteration.
2375 Assoc := First (Component_Associations (N));
2376 while Present (Assoc)
2377 and then Present (Choice_List (Assoc))
2378 loop
2379 Prev_Nb_Discrete_Choices := Nb_Discrete_Choices;
2380 Choice := First (Choice_List (Assoc));
2382 loop
2383 Analyze (Choice);
2385 if Nkind (Choice) = N_Others_Choice then
2386 Single_Choice := False;
2387 exit;
2389 -- Test for subtype mark without constraint
2391 elsif Is_Entity_Name (Choice) and then
2392 Is_Type (Entity (Choice))
2393 then
2394 if Base_Type (Entity (Choice)) /= Index_Base then
2395 Error_Msg_N
2396 ("invalid subtype mark in aggregate choice",
2397 Choice);
2398 return Failure;
2399 end if;
2401 -- Case of subtype indication
2403 elsif Nkind (Choice) = N_Subtype_Indication then
2404 Resolve_Discrete_Subtype_Indication (Choice, Index_Base);
2406 if Has_Dynamic_Predicate_Aspect
2407 (Entity (Subtype_Mark (Choice)))
2408 or else Has_Ghost_Predicate_Aspect
2409 (Entity (Subtype_Mark (Choice)))
2410 then
2411 Error_Msg_NE
2412 ("subtype& has non-static predicate, "
2413 & "not allowed in aggregate choice",
2414 Choice, Entity (Subtype_Mark (Choice)));
2415 end if;
2417 -- Does the subtype indication evaluation raise CE?
2419 Get_Index_Bounds (Subtype_Mark (Choice), S_Low, S_High);
2420 Get_Index_Bounds (Choice, Low, High);
2421 Check_Bounds (S_Low, S_High, Low, High);
2423 -- Case of range or expression
2425 else
2426 Resolve (Choice, Index_Base);
2427 Check_Unset_Reference (Choice);
2428 Check_Non_Static_Context (Choice);
2430 -- If semantic errors were posted on the choice, then
2431 -- record that for possible early return from later
2432 -- processing (see handling of enumeration choices).
2434 if Error_Posted (Choice) then
2435 Errors_Posted_On_Choices := True;
2436 end if;
2438 -- Do not range check a choice. This check is redundant
2439 -- since this test is already done when we check that the
2440 -- bounds of the array aggregate are within range.
2442 Set_Do_Range_Check (Choice, False);
2443 end if;
2445 -- If we could not resolve the discrete choice stop here
2447 if Etype (Choice) = Any_Type then
2448 return Failure;
2450 -- If the discrete choice raises CE get its original bounds
2452 elsif Nkind (Choice) = N_Raise_Constraint_Error then
2453 Set_Raises_Constraint_Error (N);
2454 Get_Index_Bounds (Original_Node (Choice), Low, High);
2456 -- Otherwise get its bounds as usual
2458 else
2459 Get_Index_Bounds (Choice, Low, High);
2460 end if;
2462 if (Dynamic_Or_Null_Range (Low, High)
2463 or else (Nkind (Choice) = N_Subtype_Indication
2464 and then
2465 Dynamic_Or_Null_Range (S_Low, S_High)))
2466 and then Nb_Choices /= 1
2467 then
2468 Error_Msg_N
2469 ("dynamic or empty choice in aggregate "
2470 & "must be the only choice", Choice);
2471 return Failure;
2472 end if;
2474 if not (All_Composite_Constraints_Static (Low)
2475 and then All_Composite_Constraints_Static (High)
2476 and then All_Composite_Constraints_Static (S_Low)
2477 and then All_Composite_Constraints_Static (S_High))
2478 then
2479 Check_Restriction (No_Dynamic_Sized_Objects, Choice);
2480 end if;
2482 Nb_Discrete_Choices := Nb_Discrete_Choices + 1;
2483 Table (Nb_Discrete_Choices).Lo := Low;
2484 Table (Nb_Discrete_Choices).Hi := High;
2485 Table (Nb_Discrete_Choices).Choice := Choice;
2487 Next (Choice);
2489 if No (Choice) then
2491 -- Check if we have a single discrete choice and whether
2492 -- this discrete choice specifies a single value.
2494 Single_Choice :=
2495 Nb_Discrete_Choices = Prev_Nb_Discrete_Choices + 1
2496 and then Low = High;
2498 exit;
2499 end if;
2500 end loop;
2502 -- Ada 2005 (AI-231)
2504 if Ada_Version >= Ada_2005
2505 and then not Empty_Range (Assoc)
2506 then
2507 if Known_Null (Expression (Assoc)) then
2508 Check_Can_Never_Be_Null (Etype (N), Expression (Assoc));
2510 -- Report warning on iterated component association that may
2511 -- initialize some component of an array of null-excluding
2512 -- access type components with a null value. For example:
2514 -- type AList is array (...) of not null access Integer;
2515 -- L : AList :=
2516 -- [for J in A'Range =>
2517 -- (if Func (J) = 0 then A(J)'Access else Null)];
2519 elsif Ada_Version >= Ada_2022
2520 and then Can_Never_Be_Null (Component_Type (Etype (N)))
2521 and then Nkind (Assoc) = N_Iterated_Component_Association
2522 and then Nkind (Expression (Assoc)) in N_If_Expression
2523 | N_Case_Expression
2524 then
2525 Warn_On_Null_Component_Association (Expression (Assoc));
2526 end if;
2527 end if;
2529 -- Ada 2005 (AI-287): In case of default initialized component
2530 -- we delay the resolution to the expansion phase.
2532 if Box_Present (Assoc) then
2534 -- Ada 2005 (AI-287): In case of default initialization of a
2535 -- component the expander will generate calls to the
2536 -- corresponding initialization subprogram. We need to call
2537 -- Resolve_Aggr_Expr to check the rules about
2538 -- dimensionality.
2540 if not Resolve_Aggr_Expr
2541 (Assoc, Single_Elmt => Single_Choice)
2542 then
2543 return Failure;
2544 end if;
2546 -- ??? Checks for dynamically tagged expressions below will
2547 -- be only applied to iterated_component_association after
2548 -- expansion; in particular, errors might not be reported when
2549 -- -gnatc switch is used.
2551 elsif Nkind (Assoc) = N_Iterated_Component_Association then
2552 null; -- handled above, in a loop context
2554 elsif not Resolve_Aggr_Expr
2555 (Expression (Assoc), Single_Elmt => Single_Choice)
2556 then
2557 return Failure;
2559 -- Check incorrect use of dynamically tagged expression
2561 -- We differentiate here two cases because the expression may
2562 -- not be decorated. For example, the analysis and resolution
2563 -- of the expression associated with the others choice will be
2564 -- done later with the full aggregate. In such case we
2565 -- duplicate the expression tree to analyze the copy and
2566 -- perform the required check.
2568 elsif No (Etype (Expression (Assoc))) then
2569 declare
2570 Save_Analysis : constant Boolean := Full_Analysis;
2571 Expr : constant Node_Id :=
2572 New_Copy_Tree (Expression (Assoc));
2574 begin
2575 Expander_Mode_Save_And_Set (False);
2576 Full_Analysis := False;
2578 -- Analyze the expression, making sure it is properly
2579 -- attached to the tree before we do the analysis.
2581 Set_Parent (Expr, Parent (Expression (Assoc)));
2582 Analyze (Expr);
2584 -- Compute its dimensions now, rather than at the end of
2585 -- resolution, because in the case of multidimensional
2586 -- aggregates subsequent expansion may lead to spurious
2587 -- errors.
2589 Check_Expression_Dimensions (Expr, Component_Typ);
2591 -- If the expression is a literal, propagate this info
2592 -- to the expression in the association, to enable some
2593 -- optimizations downstream.
2595 if Is_Entity_Name (Expr)
2596 and then Present (Entity (Expr))
2597 and then Ekind (Entity (Expr)) = E_Enumeration_Literal
2598 then
2599 Analyze_And_Resolve
2600 (Expression (Assoc), Component_Typ);
2601 end if;
2603 Full_Analysis := Save_Analysis;
2604 Expander_Mode_Restore;
2606 if Is_Tagged_Type (Etype (Expr)) then
2607 Check_Dynamically_Tagged_Expression
2608 (Expr => Expr,
2609 Typ => Component_Type (Etype (N)),
2610 Related_Nod => N);
2611 end if;
2612 end;
2614 elsif Is_Tagged_Type (Etype (Expression (Assoc))) then
2615 Check_Dynamically_Tagged_Expression
2616 (Expr => Expression (Assoc),
2617 Typ => Component_Type (Etype (N)),
2618 Related_Nod => N);
2619 end if;
2621 Next (Assoc);
2622 end loop;
2624 -- If aggregate contains more than one choice then these must be
2625 -- static. Check for duplicate and missing values.
2627 -- Note: there is duplicated code here wrt Check_Choice_Set in
2628 -- the body of Sem_Case, and it is possible we could just reuse
2629 -- that procedure. To be checked ???
2631 if Nb_Discrete_Choices > 1 then
2632 Check_Choices : declare
2633 Choice : Node_Id;
2634 -- Location of choice for messages
2636 Hi_Val : Uint;
2637 Lo_Val : Uint;
2638 -- High end of one range and Low end of the next. Should be
2639 -- contiguous if there is no hole in the list of values.
2641 Lo_Dup : Uint;
2642 Hi_Dup : Uint;
2643 -- End points of duplicated range
2645 Missing_Or_Duplicates : Boolean := False;
2646 -- Set True if missing or duplicate choices found
2648 procedure Output_Bad_Choices (Lo, Hi : Uint; C : Node_Id);
2649 -- Output continuation message with a representation of the
2650 -- bounds (just Lo if Lo = Hi, else Lo .. Hi). C is the
2651 -- choice node where the message is to be posted.
2653 ------------------------
2654 -- Output_Bad_Choices --
2655 ------------------------
2657 procedure Output_Bad_Choices (Lo, Hi : Uint; C : Node_Id) is
2658 begin
2659 -- Enumeration type case
2661 if Is_Enumeration_Type (Index_Typ) then
2662 Error_Msg_Name_1 :=
2663 Chars (Get_Enum_Lit_From_Pos (Index_Typ, Lo, Loc));
2664 Error_Msg_Name_2 :=
2665 Chars (Get_Enum_Lit_From_Pos (Index_Typ, Hi, Loc));
2667 if Lo = Hi then
2668 Error_Msg_N ("\\ %!", C);
2669 else
2670 Error_Msg_N ("\\ % .. %!", C);
2671 end if;
2673 -- Integer types case
2675 else
2676 Error_Msg_Uint_1 := Lo;
2677 Error_Msg_Uint_2 := Hi;
2679 if Lo = Hi then
2680 Error_Msg_N ("\\ ^!", C);
2681 else
2682 Error_Msg_N ("\\ ^ .. ^!", C);
2683 end if;
2684 end if;
2685 end Output_Bad_Choices;
2687 -- Start of processing for Check_Choices
2689 begin
2690 Sort_Case_Table (Table);
2692 -- First we do a quick linear loop to find out if we have
2693 -- any duplicates or missing entries (usually we have a
2694 -- legal aggregate, so this will get us out quickly).
2696 for J in 1 .. Nb_Discrete_Choices - 1 loop
2697 Hi_Val := Expr_Value (Table (J).Hi);
2698 Lo_Val := Expr_Value (Table (J + 1).Lo);
2700 if Lo_Val <= Hi_Val
2701 or else (Lo_Val > Hi_Val + 1
2702 and then not Others_Present)
2703 then
2704 Missing_Or_Duplicates := True;
2705 exit;
2706 end if;
2707 end loop;
2709 -- If we have missing or duplicate entries, first fill in
2710 -- the Highest entries to make life easier in the following
2711 -- loops to detect bad entries.
2713 if Missing_Or_Duplicates then
2714 Table (1).Highest := Expr_Value (Table (1).Hi);
2716 for J in 2 .. Nb_Discrete_Choices loop
2717 Table (J).Highest :=
2718 UI_Max
2719 (Table (J - 1).Highest, Expr_Value (Table (J).Hi));
2720 end loop;
2722 -- Loop through table entries to find duplicate indexes
2724 for J in 2 .. Nb_Discrete_Choices loop
2725 Lo_Val := Expr_Value (Table (J).Lo);
2726 Hi_Val := Expr_Value (Table (J).Hi);
2728 -- Case where we have duplicates (the lower bound of
2729 -- this choice is less than or equal to the highest
2730 -- high bound found so far).
2732 if Lo_Val <= Table (J - 1).Highest then
2734 -- We move backwards looking for duplicates. We can
2735 -- abandon this loop as soon as we reach a choice
2736 -- highest value that is less than Lo_Val.
2738 for K in reverse 1 .. J - 1 loop
2739 exit when Table (K).Highest < Lo_Val;
2741 -- Here we may have duplicates between entries
2742 -- for K and J. Get range of duplicates.
2744 Lo_Dup :=
2745 UI_Max (Lo_Val, Expr_Value (Table (K).Lo));
2746 Hi_Dup :=
2747 UI_Min (Hi_Val, Expr_Value (Table (K).Hi));
2749 -- Nothing to do if duplicate range is null
2751 if Lo_Dup > Hi_Dup then
2752 null;
2754 -- Otherwise place proper message
2756 else
2757 -- We place message on later choice, with a
2758 -- line reference to the earlier choice.
2760 if Sloc (Table (J).Choice) <
2761 Sloc (Table (K).Choice)
2762 then
2763 Choice := Table (K).Choice;
2764 Error_Msg_Sloc := Sloc (Table (J).Choice);
2765 else
2766 Choice := Table (J).Choice;
2767 Error_Msg_Sloc := Sloc (Table (K).Choice);
2768 end if;
2770 if Lo_Dup = Hi_Dup then
2771 Error_Msg_N
2772 ("index value in array aggregate "
2773 & "duplicates the one given#!", Choice);
2774 else
2775 Error_Msg_N
2776 ("index values in array aggregate "
2777 & "duplicate those given#!", Choice);
2778 end if;
2780 Output_Bad_Choices (Lo_Dup, Hi_Dup, Choice);
2781 end if;
2782 end loop;
2783 end if;
2784 end loop;
2786 -- Loop through entries in table to find missing indexes.
2787 -- Not needed if others, since missing impossible.
2789 if not Others_Present then
2790 for J in 2 .. Nb_Discrete_Choices loop
2791 Lo_Val := Expr_Value (Table (J).Lo);
2792 Hi_Val := Table (J - 1).Highest;
2794 if Lo_Val > Hi_Val + 1 then
2796 declare
2797 Error_Node : Node_Id;
2799 begin
2800 -- If the choice is the bound of a range in
2801 -- a subtype indication, it is not in the
2802 -- source lists for the aggregate itself, so
2803 -- post the error on the aggregate. Otherwise
2804 -- post it on choice itself.
2806 Choice := Table (J).Choice;
2808 if Is_List_Member (Choice) then
2809 Error_Node := Choice;
2810 else
2811 Error_Node := N;
2812 end if;
2814 if Hi_Val + 1 = Lo_Val - 1 then
2815 Error_Msg_N
2816 ("missing index value "
2817 & "in array aggregate!", Error_Node);
2818 else
2819 Error_Msg_N
2820 ("missing index values "
2821 & "in array aggregate!", Error_Node);
2822 end if;
2824 Output_Bad_Choices
2825 (Hi_Val + 1, Lo_Val - 1, Error_Node);
2826 end;
2827 end if;
2828 end loop;
2829 end if;
2831 -- If either missing or duplicate values, return failure
2833 Set_Etype (N, Any_Composite);
2834 return Failure;
2835 end if;
2836 end Check_Choices;
2837 end if;
2839 if Has_Iterator_Specifications then
2840 -- Bounds will be determined dynamically.
2842 return Success;
2843 end if;
2845 -- STEP 2 (B): Compute aggregate bounds and min/max choices values
2847 if Nb_Discrete_Choices > 0 then
2848 Choices_Low := Table (1).Lo;
2849 Choices_High := Table (Nb_Discrete_Choices).Hi;
2850 end if;
2852 -- If Others is present, then bounds of aggregate come from the
2853 -- index constraint (not the choices in the aggregate itself).
2855 if Others_Present then
2856 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2858 -- Abandon processing if either bound is already signalled as
2859 -- an error (prevents junk cascaded messages and blow ups).
2861 if Nkind (Aggr_Low) = N_Error
2862 or else
2863 Nkind (Aggr_High) = N_Error
2864 then
2865 return False;
2866 end if;
2868 -- No others clause present
2870 else
2871 -- Special processing if others allowed and not present. This
2872 -- means that the bounds of the aggregate come from the index
2873 -- constraint (and the length must match).
2875 if Others_Allowed then
2876 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2878 -- Abandon processing if either bound is already signalled
2879 -- as an error (stop junk cascaded messages and blow ups).
2881 if Nkind (Aggr_Low) = N_Error
2882 or else
2883 Nkind (Aggr_High) = N_Error
2884 then
2885 return False;
2886 end if;
2888 -- If others allowed, and no others present, then the array
2889 -- should cover all index values. If it does not, we will
2890 -- get a length check warning, but there is two cases where
2891 -- an additional warning is useful:
2893 -- If we have no positional components, and the length is
2894 -- wrong (which we can tell by others being allowed with
2895 -- missing components), and the index type is an enumeration
2896 -- type, then issue appropriate warnings about these missing
2897 -- components. They are only warnings, since the aggregate
2898 -- is fine, it's just the wrong length. We skip this check
2899 -- for standard character types (since there are no literals
2900 -- and it is too much trouble to concoct them), and also if
2901 -- any of the bounds have values that are not known at
2902 -- compile time.
2904 -- Another case warranting a warning is when the length
2905 -- is right, but as above we have an index type that is
2906 -- an enumeration, and the bounds do not match. This is a
2907 -- case where dubious sliding is allowed and we generate a
2908 -- warning that the bounds do not match.
2910 if No (Expressions (N))
2911 and then Nkind (Index) = N_Range
2912 and then Is_Enumeration_Type (Etype (Index))
2913 and then not Is_Standard_Character_Type (Etype (Index))
2914 and then Compile_Time_Known_Value (Aggr_Low)
2915 and then Compile_Time_Known_Value (Aggr_High)
2916 and then Compile_Time_Known_Value (Choices_Low)
2917 and then Compile_Time_Known_Value (Choices_High)
2918 then
2919 -- If any of the expressions or range bounds in choices
2920 -- have semantic errors, then do not attempt further
2921 -- resolution, to prevent cascaded errors.
2923 if Errors_Posted_On_Choices then
2924 return Failure;
2925 end if;
2927 declare
2928 ALo : constant Node_Id := Expr_Value_E (Aggr_Low);
2929 AHi : constant Node_Id := Expr_Value_E (Aggr_High);
2930 CLo : constant Node_Id := Expr_Value_E (Choices_Low);
2931 CHi : constant Node_Id := Expr_Value_E (Choices_High);
2933 Ent : Entity_Id;
2935 begin
2936 -- Warning case 1, missing values at start/end. Only
2937 -- do the check if the number of entries is too small.
2939 if (Enumeration_Pos (CHi) - Enumeration_Pos (CLo))
2941 (Enumeration_Pos (AHi) - Enumeration_Pos (ALo))
2942 then
2943 Error_Msg_N
2944 ("missing index value(s) in array aggregate??",
2947 -- Output missing value(s) at start
2949 if Chars (ALo) /= Chars (CLo) then
2950 Ent := Prev (CLo);
2952 if Chars (ALo) = Chars (Ent) then
2953 Error_Msg_Name_1 := Chars (ALo);
2954 Error_Msg_N ("\ %??", N);
2955 else
2956 Error_Msg_Name_1 := Chars (ALo);
2957 Error_Msg_Name_2 := Chars (Ent);
2958 Error_Msg_N ("\ % .. %??", N);
2959 end if;
2960 end if;
2962 -- Output missing value(s) at end
2964 if Chars (AHi) /= Chars (CHi) then
2965 Ent := Next (CHi);
2967 if Chars (AHi) = Chars (Ent) then
2968 Error_Msg_Name_1 := Chars (Ent);
2969 Error_Msg_N ("\ %??", N);
2970 else
2971 Error_Msg_Name_1 := Chars (Ent);
2972 Error_Msg_Name_2 := Chars (AHi);
2973 Error_Msg_N ("\ % .. %??", N);
2974 end if;
2975 end if;
2977 -- Warning case 2, dubious sliding. The First_Subtype
2978 -- test distinguishes between a constrained type where
2979 -- sliding is not allowed (so we will get a warning
2980 -- later that Constraint_Error will be raised), and
2981 -- the unconstrained case where sliding is permitted.
2983 elsif (Enumeration_Pos (CHi) - Enumeration_Pos (CLo))
2985 (Enumeration_Pos (AHi) - Enumeration_Pos (ALo))
2986 and then Chars (ALo) /= Chars (CLo)
2987 and then
2988 not Is_Constrained (First_Subtype (Etype (N)))
2989 then
2990 Error_Msg_N
2991 ("bounds of aggregate do not match target??", N);
2992 end if;
2993 end;
2994 end if;
2995 end if;
2997 -- If no others, aggregate bounds come from aggregate
2999 Aggr_Low := Choices_Low;
3000 Aggr_High := Choices_High;
3001 end if;
3002 end Step_2;
3004 -- STEP 3: Process positional components
3006 else
3007 -- STEP 3 (A): Process positional elements
3009 Expr := First (Expressions (N));
3010 Nb_Elements := Uint_0;
3011 while Present (Expr) loop
3012 Nb_Elements := Nb_Elements + 1;
3014 -- Ada 2005 (AI-231)
3016 if Ada_Version >= Ada_2005 and then Known_Null (Expr) then
3017 Check_Can_Never_Be_Null (Etype (N), Expr);
3018 end if;
3020 if not Resolve_Aggr_Expr (Expr, Single_Elmt => True) then
3021 return Failure;
3022 end if;
3024 -- Check incorrect use of dynamically tagged expression
3026 if Is_Tagged_Type (Etype (Expr)) then
3027 Check_Dynamically_Tagged_Expression
3028 (Expr => Expr,
3029 Typ => Component_Type (Etype (N)),
3030 Related_Nod => N);
3031 end if;
3033 Next (Expr);
3034 end loop;
3036 if Others_Present then
3037 Assoc := Last (Component_Associations (N));
3039 -- Ada 2005 (AI-231)
3041 if Ada_Version >= Ada_2005 and then Known_Null (Assoc) then
3042 Check_Can_Never_Be_Null (Etype (N), Expression (Assoc));
3043 end if;
3045 -- Ada 2005 (AI-287): In case of default initialized component,
3046 -- we delay the resolution to the expansion phase.
3048 if Box_Present (Assoc) then
3050 -- Ada 2005 (AI-287): In case of default initialization of a
3051 -- component the expander will generate calls to the
3052 -- corresponding initialization subprogram. We need to call
3053 -- Resolve_Aggr_Expr to check the rules about
3054 -- dimensionality.
3056 if not Resolve_Aggr_Expr (Assoc, Single_Elmt => False) then
3057 return Failure;
3058 end if;
3060 elsif not Resolve_Aggr_Expr (Expression (Assoc),
3061 Single_Elmt => False)
3062 then
3063 return Failure;
3065 -- Check incorrect use of dynamically tagged expression. The
3066 -- expression of the others choice has not been resolved yet.
3067 -- In order to diagnose the semantic error we create a duplicate
3068 -- tree to analyze it and perform the check.
3070 elsif Nkind (Assoc) /= N_Iterated_Component_Association then
3071 declare
3072 Save_Analysis : constant Boolean := Full_Analysis;
3073 Expr : constant Node_Id :=
3074 New_Copy_Tree (Expression (Assoc));
3076 begin
3077 Expander_Mode_Save_And_Set (False);
3078 Full_Analysis := False;
3079 Analyze (Expr);
3080 Full_Analysis := Save_Analysis;
3081 Expander_Mode_Restore;
3083 if Is_Tagged_Type (Etype (Expr)) then
3084 Check_Dynamically_Tagged_Expression
3085 (Expr => Expr,
3086 Typ => Component_Type (Etype (N)),
3087 Related_Nod => N);
3088 end if;
3089 end;
3090 end if;
3091 end if;
3093 -- STEP 3 (B): Compute the aggregate bounds
3095 if Others_Present then
3096 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
3098 else
3099 if Others_Allowed then
3100 Get_Index_Bounds (Index_Constr, Aggr_Low, Discard);
3101 else
3102 Aggr_Low := Index_Typ_Low;
3103 end if;
3105 Aggr_High := Add (Nb_Elements - 1, To => Aggr_Low);
3106 Check_Bound (Index_Base_High, Aggr_High);
3107 end if;
3108 end if;
3110 -- STEP 4: Perform static aggregate checks and save the bounds
3112 -- Check (A)
3114 Check_Bounds (Index_Typ_Low, Index_Typ_High, Aggr_Low, Aggr_High);
3115 Check_Bounds (Index_Base_Low, Index_Base_High, Aggr_Low, Aggr_High);
3117 -- Check (B)
3119 if Others_Present and then Nb_Discrete_Choices > 0 then
3120 Check_Bounds (Aggr_Low, Aggr_High, Choices_Low, Choices_High);
3121 Check_Bounds (Index_Typ_Low, Index_Typ_High,
3122 Choices_Low, Choices_High);
3123 Check_Bounds (Index_Base_Low, Index_Base_High,
3124 Choices_Low, Choices_High);
3126 -- Check (C)
3128 elsif Others_Present and then Nb_Elements > 0 then
3129 Check_Length (Aggr_Low, Aggr_High, Nb_Elements);
3130 Check_Length (Index_Typ_Low, Index_Typ_High, Nb_Elements);
3131 Check_Length (Index_Base_Low, Index_Base_High, Nb_Elements);
3132 end if;
3134 if Raises_Constraint_Error (Aggr_Low)
3135 or else Raises_Constraint_Error (Aggr_High)
3136 then
3137 Set_Raises_Constraint_Error (N);
3138 end if;
3140 Aggr_Low := Duplicate_Subexpr (Aggr_Low);
3142 -- Do not duplicate Aggr_High if Aggr_High = Aggr_Low + Nb_Elements
3143 -- since the addition node returned by Add is not yet analyzed. Attach
3144 -- to tree and analyze first. Reset analyzed flag to ensure it will get
3145 -- analyzed when it is a literal bound whose type must be properly set.
3147 if Others_Present or else Nb_Discrete_Choices > 0 then
3148 Aggr_High := Duplicate_Subexpr (Aggr_High);
3150 if Etype (Aggr_High) = Universal_Integer then
3151 Set_Analyzed (Aggr_High, False);
3152 end if;
3153 end if;
3155 -- If the aggregate already has bounds attached to it, it means this is
3156 -- a positional aggregate created as an optimization by
3157 -- Exp_Aggr.Convert_To_Positional, so we don't want to change those
3158 -- bounds.
3160 if Present (Aggregate_Bounds (N))
3161 and then not Others_Allowed
3162 and then not Comes_From_Source (N)
3163 then
3164 Aggr_Low := Low_Bound (Aggregate_Bounds (N));
3165 Aggr_High := High_Bound (Aggregate_Bounds (N));
3166 end if;
3168 Set_Aggregate_Bounds
3169 (N, Make_Range (Loc, Low_Bound => Aggr_Low, High_Bound => Aggr_High));
3171 -- The bounds may contain expressions that must be inserted upwards.
3172 -- Attach them fully to the tree. After analysis, remove side effects
3173 -- from upper bound, if still needed.
3175 Set_Parent (Aggregate_Bounds (N), N);
3176 Analyze_And_Resolve (Aggregate_Bounds (N), Index_Typ);
3177 Check_Unset_Reference (Aggregate_Bounds (N));
3179 if not Others_Present and then Nb_Discrete_Choices = 0 then
3180 Set_High_Bound
3181 (Aggregate_Bounds (N),
3182 Duplicate_Subexpr (High_Bound (Aggregate_Bounds (N))));
3183 end if;
3185 -- Check the dimensions of each component in the array aggregate
3187 Analyze_Dimension_Array_Aggregate (N, Component_Typ);
3189 return Success;
3190 end Resolve_Array_Aggregate;
3192 ---------------------------------
3193 -- Resolve_Container_Aggregate --
3194 ---------------------------------
3196 procedure Resolve_Container_Aggregate (N : Node_Id; Typ : Entity_Id) is
3197 procedure Resolve_Iterated_Association
3198 (Comp : Node_Id;
3199 Key_Type : Entity_Id;
3200 Elmt_Type : Entity_Id);
3201 -- Resolve choices and expression in an iterated component association
3202 -- or an iterated element association, which has a key_expression.
3203 -- This is similar but not identical to the handling of this construct
3204 -- in an array aggregate.
3205 -- For a named container, the type of each choice must be compatible
3206 -- with the key type. For a positional container, the choice must be
3207 -- a subtype indication or an iterator specification that determines
3208 -- an element type.
3210 Asp : constant Node_Id := Find_Value_Of_Aspect (Typ, Aspect_Aggregate);
3212 Empty_Subp : Node_Id := Empty;
3213 Add_Named_Subp : Node_Id := Empty;
3214 Add_Unnamed_Subp : Node_Id := Empty;
3215 New_Indexed_Subp : Node_Id := Empty;
3216 Assign_Indexed_Subp : Node_Id := Empty;
3218 ----------------------------------
3219 -- Resolve_Iterated_Association --
3220 ----------------------------------
3222 procedure Resolve_Iterated_Association
3223 (Comp : Node_Id;
3224 Key_Type : Entity_Id;
3225 Elmt_Type : Entity_Id)
3227 Loc : constant Source_Ptr := Sloc (N);
3228 Choice : Node_Id;
3229 Copy : Node_Id;
3230 Ent : Entity_Id;
3231 Expr : Node_Id;
3232 Key_Expr : Node_Id;
3233 Id : Entity_Id;
3234 Id_Name : Name_Id;
3235 Typ : Entity_Id := Empty;
3237 begin
3238 Error_Msg_Ada_2022_Feature ("iterated component", Loc);
3240 -- If this is an Iterated_Element_Association then either a
3241 -- an Iterator_Specification or a Loop_Parameter specification
3242 -- is present. In both cases a Key_Expression is present.
3244 if Nkind (Comp) = N_Iterated_Element_Association then
3246 -- Create a temporary scope to avoid some modifications from
3247 -- escaping the Analyze call below. The original Tree will be
3248 -- reanalyzed later.
3250 Ent := New_Internal_Entity
3251 (E_Loop, Current_Scope, Sloc (Comp), 'L');
3252 Set_Etype (Ent, Standard_Void_Type);
3253 Set_Parent (Ent, Parent (Comp));
3254 Push_Scope (Ent);
3256 if Present (Loop_Parameter_Specification (Comp)) then
3257 Copy := Copy_Separate_Tree (Comp);
3259 Analyze
3260 (Loop_Parameter_Specification (Copy));
3262 Id_Name := Chars (Defining_Identifier
3263 (Loop_Parameter_Specification (Comp)));
3264 else
3265 Copy := Copy_Separate_Tree (Iterator_Specification (Comp));
3266 Analyze (Copy);
3268 Id_Name := Chars (Defining_Identifier
3269 (Iterator_Specification (Comp)));
3270 end if;
3272 -- Key expression must have the type of the key. We preanalyze
3273 -- a copy of the original expression, because it will be
3274 -- reanalyzed and copied as needed during expansion of the
3275 -- corresponding loop.
3277 Key_Expr := Key_Expression (Comp);
3278 Preanalyze_And_Resolve (New_Copy_Tree (Key_Expr), Key_Type);
3279 End_Scope;
3281 Typ := Key_Type;
3283 elsif Present (Iterator_Specification (Comp)) then
3284 -- Create a temporary scope to avoid some modifications from
3285 -- escaping the Analyze call below. The original Tree will be
3286 -- reanalyzed later.
3288 Ent := New_Internal_Entity
3289 (E_Loop, Current_Scope, Sloc (Comp), 'L');
3290 Set_Etype (Ent, Standard_Void_Type);
3291 Set_Parent (Ent, Parent (Comp));
3292 Push_Scope (Ent);
3294 Copy := Copy_Separate_Tree (Iterator_Specification (Comp));
3295 Id_Name :=
3296 Chars (Defining_Identifier (Iterator_Specification (Comp)));
3298 Preanalyze (Copy);
3300 End_Scope;
3302 Typ := Etype (Defining_Identifier (Copy));
3304 else
3305 Choice := First (Discrete_Choices (Comp));
3307 while Present (Choice) loop
3308 Analyze (Choice);
3310 -- Choice can be a subtype name, a range, or an expression
3312 if Is_Entity_Name (Choice)
3313 and then Is_Type (Entity (Choice))
3314 and then Base_Type (Entity (Choice)) = Base_Type (Key_Type)
3315 then
3316 null;
3318 elsif Present (Key_Type) then
3319 Analyze_And_Resolve (Choice, Key_Type);
3320 Typ := Key_Type;
3321 else
3322 Typ := Etype (Choice); -- assume unique for now
3323 end if;
3325 Next (Choice);
3326 end loop;
3328 Id_Name := Chars (Defining_Identifier (Comp));
3329 end if;
3331 -- Create a scope in which to introduce an index, which is usually
3332 -- visible in the expression for the component, and needed for its
3333 -- analysis.
3335 Id := Make_Defining_Identifier (Sloc (Comp), Id_Name);
3336 Ent := New_Internal_Entity (E_Loop,
3337 Current_Scope, Sloc (Comp), 'L');
3338 Set_Etype (Ent, Standard_Void_Type);
3339 Set_Parent (Ent, Parent (Comp));
3340 Push_Scope (Ent);
3342 -- Insert and decorate the loop variable in the current scope.
3343 -- The expression has to be analyzed once the loop variable is
3344 -- directly visible. Mark the variable as referenced to prevent
3345 -- spurious warnings, given that subsequent uses of its name in the
3346 -- expression will reference the internal (synonym) loop variable.
3348 Enter_Name (Id);
3350 pragma Assert (Present (Typ));
3351 Set_Etype (Id, Typ);
3353 Mutate_Ekind (Id, E_Variable);
3354 Set_Is_Not_Self_Hidden (Id);
3355 Set_Scope (Id, Ent);
3356 Set_Referenced (Id);
3358 -- Analyze a copy of the expression, to verify legality. We use
3359 -- a copy because the expression will be analyzed anew when the
3360 -- enclosing aggregate is expanded, and the construct is rewritten
3361 -- as a loop with a new index variable.
3363 Expr := New_Copy_Tree (Expression (Comp));
3364 Preanalyze_And_Resolve (Expr, Elmt_Type);
3365 End_Scope;
3367 end Resolve_Iterated_Association;
3369 -- Start of processing for Resolve_Container_Aggregate
3371 begin
3372 pragma Assert (Nkind (Asp) = N_Aggregate);
3374 Set_Etype (N, Typ);
3375 Parse_Aspect_Aggregate (Asp,
3376 Empty_Subp, Add_Named_Subp, Add_Unnamed_Subp,
3377 New_Indexed_Subp, Assign_Indexed_Subp);
3379 if Present (Add_Unnamed_Subp)
3380 and then No (New_Indexed_Subp)
3381 and then Present (Etype (Add_Unnamed_Subp))
3382 and then Etype (Add_Unnamed_Subp) /= Any_Type
3383 then
3384 declare
3385 Elmt_Type : constant Entity_Id :=
3386 Etype (Next_Formal
3387 (First_Formal (Entity (Add_Unnamed_Subp))));
3388 Comp : Node_Id;
3390 begin
3391 if Present (Expressions (N)) then
3392 -- positional aggregate
3394 Comp := First (Expressions (N));
3395 while Present (Comp) loop
3396 Analyze_And_Resolve (Comp, Elmt_Type);
3397 Next (Comp);
3398 end loop;
3399 end if;
3401 -- Empty aggregate, to be replaced by Empty during
3402 -- expansion, or iterated component association.
3404 if Present (Component_Associations (N)) then
3405 declare
3406 Comp : Node_Id := First (Component_Associations (N));
3407 begin
3408 while Present (Comp) loop
3409 if Nkind (Comp) /=
3410 N_Iterated_Component_Association
3411 then
3412 Error_Msg_N ("illegal component association "
3413 & "for unnamed container aggregate", Comp);
3414 return;
3415 else
3416 Resolve_Iterated_Association
3417 (Comp, Empty, Elmt_Type);
3418 end if;
3420 Next (Comp);
3421 end loop;
3422 end;
3423 end if;
3424 end;
3426 elsif Present (Add_Named_Subp)
3427 and then Etype (Add_Named_Subp) /= Any_Type
3428 then
3429 declare
3430 -- Retrieves types of container, key, and element from the
3431 -- specified insertion procedure.
3433 Container : constant Entity_Id :=
3434 First_Formal (Entity (Add_Named_Subp));
3435 Key_Type : constant Entity_Id := Etype (Next_Formal (Container));
3436 Elmt_Type : constant Entity_Id :=
3437 Etype (Next_Formal (Next_Formal (Container)));
3439 Comp_Assocs : constant List_Id := Component_Associations (N);
3440 Comp : Node_Id;
3441 Choice : Node_Id;
3443 begin
3444 -- In the Add_Named case, the aggregate must consist of named
3445 -- associations (Add_Unnnamed is not allowed), so we issue an
3446 -- error if there are positional associations.
3448 if No (Comp_Assocs)
3449 and then Present (Expressions (N))
3450 then
3451 Error_Msg_N ("container aggregate must be "
3452 & "named, not positional", N);
3453 return;
3454 end if;
3456 Comp := First (Comp_Assocs);
3457 while Present (Comp) loop
3458 if Nkind (Comp) = N_Component_Association then
3459 Choice := First (Choices (Comp));
3461 while Present (Choice) loop
3462 Analyze_And_Resolve (Choice, Key_Type);
3463 if not Is_Static_Expression (Choice) then
3464 Error_Msg_N ("choice must be static", Choice);
3465 end if;
3467 Next (Choice);
3468 end loop;
3470 Analyze_And_Resolve (Expression (Comp), Elmt_Type);
3472 elsif Nkind (Comp) in
3473 N_Iterated_Component_Association |
3474 N_Iterated_Element_Association
3475 then
3476 Resolve_Iterated_Association
3477 (Comp, Key_Type, Elmt_Type);
3478 end if;
3480 Next (Comp);
3481 end loop;
3482 end;
3484 elsif Present (Assign_Indexed_Subp)
3485 and then Etype (Assign_Indexed_Subp) /= Any_Type
3486 then
3487 -- Indexed Aggregate. Positional or indexed component
3488 -- can be present, but not both. Choices must be static
3489 -- values or ranges with static bounds.
3491 declare
3492 Container : constant Entity_Id :=
3493 First_Formal (Entity (Assign_Indexed_Subp));
3494 Index_Type : constant Entity_Id := Etype (Next_Formal (Container));
3495 Comp_Type : constant Entity_Id :=
3496 Etype (Next_Formal (Next_Formal (Container)));
3497 Comp : Node_Id;
3498 Choice : Node_Id;
3499 Num_Choices : Nat := 0;
3501 Hi_Val : Uint;
3502 Lo_Val : Uint;
3503 begin
3504 if Present (Expressions (N)) then
3505 Comp := First (Expressions (N));
3506 while Present (Comp) loop
3507 Analyze_And_Resolve (Comp, Comp_Type);
3508 Next (Comp);
3509 end loop;
3510 end if;
3512 if Present (Component_Associations (N))
3513 and then not Is_Empty_List (Component_Associations (N))
3514 then
3515 if Present (Expressions (N))
3516 and then not Is_Empty_List (Expressions (N))
3517 then
3518 Error_Msg_N ("container aggregate cannot be "
3519 & "both positional and named", N);
3520 return;
3521 end if;
3523 Comp := First (Component_Associations (N));
3525 while Present (Comp) loop
3526 if Nkind (Comp) = N_Component_Association then
3527 Choice := First (Choices (Comp));
3529 while Present (Choice) loop
3530 Analyze_And_Resolve (Choice, Index_Type);
3531 Num_Choices := Num_Choices + 1;
3532 Next (Choice);
3533 end loop;
3535 Analyze_And_Resolve (Expression (Comp), Comp_Type);
3537 elsif Nkind (Comp) in
3538 N_Iterated_Component_Association |
3539 N_Iterated_Element_Association
3540 then
3541 Resolve_Iterated_Association
3542 (Comp, Index_Type, Comp_Type);
3543 Num_Choices := Num_Choices + 1;
3544 end if;
3546 Next (Comp);
3547 end loop;
3549 -- The component associations in an indexed aggregate
3550 -- must denote a contiguous set of static values. We
3551 -- build a table of values/ranges and sort it, as is done
3552 -- elsewhere for case statements and array aggregates.
3553 -- If the aggregate has a single iterated association it
3554 -- is allowed to be nonstatic and there is nothing to check.
3556 if Num_Choices > 1 then
3557 declare
3558 Table : Case_Table_Type (1 .. Num_Choices);
3559 No_Choice : Pos := 1;
3560 Lo, Hi : Node_Id;
3562 -- Traverse aggregate to determine size of needed table.
3563 -- Verify that bounds are static and that loops have no
3564 -- filters or key expressions.
3566 begin
3567 Comp := First (Component_Associations (N));
3568 while Present (Comp) loop
3569 if Nkind (Comp) = N_Iterated_Element_Association then
3570 if Present
3571 (Loop_Parameter_Specification (Comp))
3572 then
3573 if Present (Iterator_Filter
3574 (Loop_Parameter_Specification (Comp)))
3575 then
3576 Error_Msg_N
3577 ("iterator filter not allowed " &
3578 "in indexed aggregate", Comp);
3579 return;
3581 elsif Present (Key_Expression
3582 (Loop_Parameter_Specification (Comp)))
3583 then
3584 Error_Msg_N
3585 ("key expression not allowed " &
3586 "in indexed aggregate", Comp);
3587 return;
3588 end if;
3589 end if;
3590 else
3592 -- If Nkind is N_Iterated_Component_Association,
3593 -- this corresponds to an iterator_specification
3594 -- with a loop_parameter_specification, and we
3595 -- have to pick up Discrete_Choices. In this case
3596 -- there will be just one "choice", which will
3597 -- typically be a range.
3599 if Nkind (Comp) = N_Iterated_Component_Association
3600 then
3601 Choice := First (Discrete_Choices (Comp));
3603 -- Case where there's a list of choices
3605 else
3606 Choice := First (Choices (Comp));
3607 end if;
3609 while Present (Choice) loop
3610 Get_Index_Bounds (Choice, Lo, Hi);
3611 Table (No_Choice).Choice := Choice;
3612 Table (No_Choice).Lo := Lo;
3613 Table (No_Choice).Hi := Hi;
3615 -- Verify staticness of value or range
3617 if not Is_Static_Expression (Lo)
3618 or else not Is_Static_Expression (Hi)
3619 then
3620 Error_Msg_N
3621 ("nonstatic expression for index " &
3622 "for indexed aggregate", Choice);
3623 return;
3624 end if;
3626 No_Choice := No_Choice + 1;
3627 Next (Choice);
3628 end loop;
3629 end if;
3631 Next (Comp);
3632 end loop;
3634 Sort_Case_Table (Table);
3636 for J in 1 .. Num_Choices - 1 loop
3637 Hi_Val := Expr_Value (Table (J).Hi);
3638 Lo_Val := Expr_Value (Table (J + 1).Lo);
3640 if Lo_Val = Hi_Val then
3641 Error_Msg_N
3642 ("duplicate index in indexed aggregate",
3643 Table (J + 1).Choice);
3644 exit;
3646 elsif Lo_Val < Hi_Val then
3647 Error_Msg_N
3648 ("overlapping indices in indexed aggregate",
3649 Table (J + 1).Choice);
3650 exit;
3652 elsif Lo_Val > Hi_Val + 1 then
3653 Error_Msg_N
3654 ("missing index values", Table (J + 1).Choice);
3655 exit;
3656 end if;
3657 end loop;
3658 end;
3659 end if;
3660 end if;
3661 end;
3662 end if;
3663 end Resolve_Container_Aggregate;
3665 -----------------------------
3666 -- Resolve_Delta_Aggregate --
3667 -----------------------------
3669 procedure Resolve_Delta_Aggregate (N : Node_Id; Typ : Entity_Id) is
3670 Base : constant Node_Id := Expression (N);
3672 begin
3673 Error_Msg_Ada_2022_Feature ("delta aggregate", Sloc (N));
3675 if not Is_Composite_Type (Typ) then
3676 Error_Msg_N ("not a composite type", N);
3677 end if;
3679 Analyze_And_Resolve (Base, Typ);
3681 if Is_Array_Type (Typ) then
3682 -- For an array_delta_aggregate, the base_expression and each
3683 -- expression in every array_component_association shall be of a
3684 -- nonlimited type; RM 4.3.4(13/5). However, to prevent repeated
3685 -- errors we only check the base expression and not array component
3686 -- associations.
3688 if Is_Limited_Type (Etype (Base)) then
3689 Error_Msg_N
3690 ("array delta aggregate shall be of a nonlimited type", Base);
3691 Explain_Limited_Type (Etype (Base), Base);
3692 end if;
3694 Resolve_Delta_Array_Aggregate (N, Typ);
3695 else
3697 -- Delta aggregates for record types must use parentheses,
3698 -- not square brackets.
3700 if Is_Homogeneous_Aggregate (N) then
3701 Error_Msg_N
3702 ("delta aggregates for record types must use (), not '[']", N);
3703 end if;
3705 -- The base_expression of a record_delta_aggregate can be of a
3706 -- limited type only if it is newly constructed; RM 7.5(2.1/5).
3708 Check_Expr_OK_In_Limited_Aggregate (Base);
3710 Resolve_Delta_Record_Aggregate (N, Typ);
3711 end if;
3713 Set_Etype (N, Typ);
3714 end Resolve_Delta_Aggregate;
3716 -----------------------------------
3717 -- Resolve_Delta_Array_Aggregate --
3718 -----------------------------------
3720 procedure Resolve_Delta_Array_Aggregate (N : Node_Id; Typ : Entity_Id) is
3721 Deltas : constant List_Id := Component_Associations (N);
3722 Index_Type : constant Entity_Id := Etype (First_Index (Typ));
3724 Assoc : Node_Id;
3725 Choice : Node_Id;
3726 Expr : Node_Id;
3728 Deep_Choice_Seen : Boolean := False;
3730 begin
3731 Assoc := First (Deltas);
3732 while Present (Assoc) loop
3733 if Nkind (Assoc) = N_Iterated_Component_Association then
3734 Choice := First (Choice_List (Assoc));
3735 while Present (Choice) loop
3736 if Nkind (Choice) = N_Others_Choice then
3737 Error_Msg_N
3738 ("OTHERS not allowed in delta aggregate", Choice);
3740 elsif Nkind (Choice) = N_Subtype_Indication then
3741 Resolve_Discrete_Subtype_Indication
3742 (Choice, Base_Type (Index_Type));
3744 else
3745 Analyze_And_Resolve (Choice, Index_Type);
3746 end if;
3748 Next (Choice);
3749 end loop;
3751 declare
3752 Id : constant Entity_Id := Defining_Identifier (Assoc);
3753 Ent : constant Entity_Id :=
3754 New_Internal_Entity
3755 (E_Loop, Current_Scope, Sloc (Assoc), 'L');
3757 begin
3758 Set_Etype (Ent, Standard_Void_Type);
3759 Set_Parent (Ent, Assoc);
3760 Push_Scope (Ent);
3762 if No (Scope (Id)) then
3763 Set_Etype (Id, Index_Type);
3764 Mutate_Ekind (Id, E_Variable);
3765 Set_Is_Not_Self_Hidden (Id);
3766 Set_Scope (Id, Ent);
3767 end if;
3768 Enter_Name (Id);
3770 -- Resolve a copy of the expression, after setting
3771 -- its parent properly to preserve its context.
3773 Expr := New_Copy_Tree (Expression (Assoc));
3774 Set_Parent (Expr, Assoc);
3775 Analyze_And_Resolve (Expr, Component_Type (Typ));
3776 End_Scope;
3777 end;
3779 else
3780 Choice := First (Choice_List (Assoc));
3781 while Present (Choice) loop
3782 if Is_Deep_Choice (Choice, Typ) then
3783 pragma Assert (All_Extensions_Allowed);
3784 Deep_Choice_Seen := True;
3786 -- a deep delta aggregate
3787 Resolve_Deep_Delta_Assoc (Assoc, Typ);
3788 else
3789 Analyze (Choice);
3791 if Nkind (Choice) = N_Others_Choice then
3792 Error_Msg_N
3793 ("OTHERS not allowed in delta aggregate", Choice);
3795 elsif Is_Entity_Name (Choice)
3796 and then Is_Type (Entity (Choice))
3797 then
3798 -- Choice covers a range of values
3800 if Base_Type (Entity (Choice)) /=
3801 Base_Type (Index_Type)
3802 then
3803 Error_Msg_NE
3804 ("choice does not match index type of &",
3805 Choice, Typ);
3806 end if;
3808 elsif Nkind (Choice) = N_Subtype_Indication then
3809 Resolve_Discrete_Subtype_Indication
3810 (Choice, Base_Type (Index_Type));
3812 else
3813 Resolve (Choice, Index_Type);
3814 end if;
3815 end if;
3817 Next (Choice);
3818 end loop;
3820 -- For an array_delta_aggregate, the array_component_association
3821 -- shall not use the box symbol <>; RM 4.3.4(11/5).
3823 pragma Assert
3824 (Box_Present (Assoc) xor Present (Expression (Assoc)));
3826 if Box_Present (Assoc) then
3827 Error_Msg_N
3828 ("'<'> in array delta aggregate is not allowed", Assoc);
3829 elsif not Deep_Choice_Seen then
3830 Analyze_And_Resolve (Expression (Assoc), Component_Type (Typ));
3831 end if;
3832 end if;
3834 Next (Assoc);
3835 end loop;
3836 end Resolve_Delta_Array_Aggregate;
3838 ------------------------------------
3839 -- Resolve_Delta_Record_Aggregate --
3840 ------------------------------------
3842 procedure Resolve_Delta_Record_Aggregate (N : Node_Id; Typ : Entity_Id) is
3844 -- Variables used to verify that discriminant-dependent components
3845 -- appear in the same variant.
3847 Comp_Ref : Entity_Id := Empty; -- init to avoid warning
3848 Variant : Node_Id;
3850 procedure Check_Variant (Id : Node_Id);
3851 -- If a given component of the delta aggregate appears in a variant
3852 -- part, verify that it is within the same variant as that of previous
3853 -- specified variant components of the delta.
3855 function Get_Component_Type
3856 (Selector : Node_Id; Enclosing_Type : Entity_Id) return Entity_Id;
3857 -- Locate component with a given name and return its type.
3858 -- If none found then report error and return Empty.
3860 function Nested_In (V1 : Node_Id; V2 : Node_Id) return Boolean;
3861 -- Determine whether variant V1 is within variant V2
3863 function Variant_Depth (N : Node_Id) return Natural;
3864 -- Determine the distance of a variant to the enclosing type declaration
3866 --------------------
3867 -- Check_Variant --
3868 --------------------
3870 procedure Check_Variant (Id : Node_Id) is
3871 Comp : Entity_Id;
3872 Comp_Variant : Node_Id;
3874 begin
3875 if not Has_Discriminants (Typ) then
3876 return;
3877 end if;
3879 Comp := First_Entity (Typ);
3880 while Present (Comp) loop
3881 exit when Chars (Comp) = Chars (Id);
3882 Next_Component (Comp);
3883 end loop;
3885 -- Find the variant, if any, whose component list includes the
3886 -- component declaration.
3888 Comp_Variant := Parent (Parent (List_Containing (Parent (Comp))));
3889 if Nkind (Comp_Variant) = N_Variant then
3890 if No (Variant) then
3891 Variant := Comp_Variant;
3892 Comp_Ref := Comp;
3894 elsif Variant /= Comp_Variant then
3895 declare
3896 D1 : constant Integer := Variant_Depth (Variant);
3897 D2 : constant Integer := Variant_Depth (Comp_Variant);
3899 begin
3900 if D1 = D2
3901 or else
3902 (D1 > D2 and then not Nested_In (Variant, Comp_Variant))
3903 or else
3904 (D2 > D1 and then not Nested_In (Comp_Variant, Variant))
3905 then
3906 pragma Assert (Present (Comp_Ref));
3907 Error_Msg_Node_2 := Comp_Ref;
3908 Error_Msg_NE
3909 ("& and & appear in different variants", Id, Comp);
3911 -- Otherwise retain the deeper variant for subsequent tests
3913 elsif D2 > D1 then
3914 Variant := Comp_Variant;
3915 end if;
3916 end;
3917 end if;
3918 end if;
3919 end Check_Variant;
3921 ------------------------
3922 -- Get_Component_Type --
3923 ------------------------
3925 function Get_Component_Type
3926 (Selector : Node_Id; Enclosing_Type : Entity_Id) return Entity_Id
3928 Comp : Entity_Id;
3929 begin
3930 case Nkind (Selector) is
3931 when N_Selected_Component | N_Indexed_Component =>
3932 -- a deep delta aggregate choice
3934 declare
3935 Prefix_Type : constant Entity_Id :=
3936 Get_Component_Type (Prefix (Selector), Enclosing_Type);
3937 begin
3938 if No (Prefix_Type) then
3939 pragma Assert (Serious_Errors_Detected > 0);
3940 return Empty;
3941 end if;
3943 -- Set the type of the prefix for GNATprove
3945 Set_Etype (Prefix (Selector), Prefix_Type);
3947 if Nkind (Selector) = N_Selected_Component then
3948 return Get_Component_Type
3949 (Selector_Name (Selector),
3950 Enclosing_Type => Prefix_Type);
3951 elsif not Is_Array_Type (Prefix_Type) then
3952 Error_Msg_NE
3953 ("type& is not an array type",
3954 Selector, Prefix_Type);
3955 elsif Number_Dimensions (Prefix_Type) /= 1 then
3956 Error_Msg_NE
3957 ("array type& not one-dimensional",
3958 Selector, Prefix_Type);
3959 elsif List_Length (Expressions (Selector)) /= 1 then
3960 Error_Msg_NE
3961 ("wrong number of indices for array type&",
3962 Selector, Prefix_Type);
3963 else
3964 Analyze_And_Resolve
3965 (First (Expressions (Selector)),
3966 Etype (First_Index (Prefix_Type)));
3967 return Component_Type (Prefix_Type);
3968 end if;
3969 end;
3971 when others =>
3972 null;
3973 end case;
3975 Comp := First_Entity (Enclosing_Type);
3976 while Present (Comp) loop
3977 if Chars (Comp) = Chars (Selector) then
3978 if Ekind (Comp) = E_Discriminant then
3979 Error_Msg_N ("delta cannot apply to discriminant", Selector);
3980 end if;
3982 Set_Entity (Selector, Comp);
3983 Set_Etype (Selector, Etype (Comp));
3985 return Etype (Comp);
3986 end if;
3988 Next_Entity (Comp);
3989 end loop;
3991 Error_Msg_NE
3992 ("type& has no component with this name", Selector, Enclosing_Type);
3993 return Empty;
3994 end Get_Component_Type;
3996 ---------------
3997 -- Nested_In --
3998 ---------------
4000 function Nested_In (V1, V2 : Node_Id) return Boolean is
4001 Par : Node_Id;
4003 begin
4004 Par := Parent (V1);
4005 while Nkind (Par) /= N_Full_Type_Declaration loop
4006 if Par = V2 then
4007 return True;
4008 end if;
4010 Par := Parent (Par);
4011 end loop;
4013 return False;
4014 end Nested_In;
4016 -------------------
4017 -- Variant_Depth --
4018 -------------------
4020 function Variant_Depth (N : Node_Id) return Natural is
4021 Depth : Natural;
4022 Par : Node_Id;
4024 begin
4025 Depth := 0;
4026 Par := Parent (N);
4027 while Nkind (Par) /= N_Full_Type_Declaration loop
4028 Depth := Depth + 1;
4029 Par := Parent (Par);
4030 end loop;
4032 return Depth;
4033 end Variant_Depth;
4035 -- Local variables
4037 Deltas : constant List_Id := Component_Associations (N);
4039 Assoc : Node_Id;
4040 Choice : Node_Id;
4041 Comp_Type : Entity_Id := Empty; -- init to avoid warning
4042 Deep_Choice : Boolean;
4044 -- Start of processing for Resolve_Delta_Record_Aggregate
4046 begin
4047 Variant := Empty;
4049 Assoc := First (Deltas);
4050 while Present (Assoc) loop
4051 Choice := First (Choice_List (Assoc));
4052 while Present (Choice) loop
4053 Deep_Choice := Nkind (Choice) /= N_Identifier;
4054 if Deep_Choice then
4055 Error_Msg_GNAT_Extension
4056 ("deep delta aggregate", Sloc (Choice));
4057 end if;
4059 Comp_Type := Get_Component_Type
4060 (Selector => Choice, Enclosing_Type => Typ);
4062 -- Set the type of the choice for GNATprove
4064 if Deep_Choice then
4065 Set_Etype (Choice, Comp_Type);
4066 end if;
4068 if Present (Comp_Type) then
4069 if not Deep_Choice then
4070 -- ??? Not clear yet how RM 4.3.1(17.7) applies to a
4071 -- deep delta aggregate.
4072 Check_Variant (Choice);
4073 end if;
4074 else
4075 Comp_Type := Any_Type;
4076 end if;
4078 Next (Choice);
4079 end loop;
4081 pragma Assert (Present (Comp_Type));
4083 -- A record_component_association in record_delta_aggregate shall not
4084 -- use the box compound delimiter <> rather than an expression; see
4085 -- RM 4.3.1(17.3/5).
4087 pragma Assert (Present (Expression (Assoc)) xor Box_Present (Assoc));
4089 if Box_Present (Assoc) then
4090 Error_Msg_N
4091 ("'<'> in record delta aggregate is not allowed", Assoc);
4092 else
4093 Analyze_And_Resolve (Expression (Assoc), Comp_Type);
4095 -- The expression must not be of a limited type; RM 4.3.1(17.4/5)
4097 if Is_Limited_Type (Etype (Expression (Assoc))) then
4098 Error_Msg_N
4099 ("expression of a limited type in record delta aggregate " &
4100 "is not allowed",
4101 Expression (Assoc));
4102 end if;
4103 end if;
4105 Next (Assoc);
4106 end loop;
4107 end Resolve_Delta_Record_Aggregate;
4109 ------------------------------
4110 -- Resolve_Deep_Delta_Assoc --
4111 ------------------------------
4113 procedure Resolve_Deep_Delta_Assoc (N : Node_Id; Typ : Entity_Id) is
4114 Choice : constant Node_Id := First (Choice_List (N));
4115 Enclosing_Type : Entity_Id := Typ;
4117 procedure Resolve_Choice_Prefix
4118 (Choice_Prefix : Node_Id; Enclosing_Type : in out Entity_Id);
4119 -- Recursively analyze selectors. Enclosing_Type is set to
4120 -- type of the last component.
4122 ---------------------------
4123 -- Resolve_Choice_Prefix --
4124 ---------------------------
4126 procedure Resolve_Choice_Prefix
4127 (Choice_Prefix : Node_Id; Enclosing_Type : in out Entity_Id)
4129 Selector : Node_Id := Choice_Prefix;
4130 begin
4131 if not Is_Root_Prefix_Of_Deep_Choice (Choice_Prefix) then
4132 Resolve_Choice_Prefix (Prefix (Choice_Prefix), Enclosing_Type);
4134 if Nkind (Choice_Prefix) = N_Selected_Component then
4135 Selector := Selector_Name (Choice_Prefix);
4136 else
4137 pragma Assert (Nkind (Choice_Prefix) = N_Indexed_Component);
4138 Selector := First (Expressions (Choice_Prefix));
4139 end if;
4140 end if;
4142 if Is_Array_Type (Enclosing_Type) then
4143 Analyze_And_Resolve (Selector,
4144 Etype (First_Index (Enclosing_Type)));
4145 Enclosing_Type := Component_Type (Enclosing_Type);
4146 else
4147 declare
4148 Comp : Entity_Id := First_Entity (Enclosing_Type);
4149 Found : Boolean := False;
4150 begin
4151 while Present (Comp) and not Found loop
4152 if Chars (Comp) = Chars (Selector) then
4153 if Ekind (Comp) = E_Discriminant then
4154 Error_Msg_N ("delta cannot apply to discriminant",
4155 Selector);
4156 end if;
4157 Found := True;
4158 Set_Entity (Selector, Comp);
4159 Set_Etype (Selector, Etype (Comp));
4160 Set_Analyzed (Selector);
4161 Enclosing_Type := Etype (Comp);
4162 else
4163 Next_Entity (Comp);
4164 end if;
4165 end loop;
4166 if not Found then
4167 Error_Msg_NE
4168 ("type& has no component with this name",
4169 Selector, Enclosing_Type);
4170 end if;
4171 end;
4172 end if;
4174 -- Set the type of the prefix for GNATprove, except for the root
4175 -- prefix, whose type is already the expected one for a record
4176 -- delta aggregate, or the type of the array index for an
4177 -- array delta aggregate (the only case here really since
4178 -- Resolve_Deep_Delta_Assoc is only called for array delta
4179 -- aggregates).
4181 if Selector /= Choice_Prefix then
4182 Set_Etype (Choice_Prefix, Enclosing_Type);
4183 end if;
4184 end Resolve_Choice_Prefix;
4185 begin
4186 declare
4187 Unimplemented : exception; -- TEMPORARY
4188 begin
4189 if Present (Next (Choice)) then
4190 raise Unimplemented;
4191 end if;
4192 end;
4194 Resolve_Choice_Prefix (Choice, Enclosing_Type);
4195 Analyze_And_Resolve (Expression (N), Enclosing_Type);
4196 end Resolve_Deep_Delta_Assoc;
4198 ---------------------------------
4199 -- Resolve_Extension_Aggregate --
4200 ---------------------------------
4202 -- There are two cases to consider:
4204 -- a) If the ancestor part is a type mark, the components needed are the
4205 -- difference between the components of the expected type and the
4206 -- components of the given type mark.
4208 -- b) If the ancestor part is an expression, it must be unambiguous, and
4209 -- once we have its type we can also compute the needed components as in
4210 -- the previous case. In both cases, if the ancestor type is not the
4211 -- immediate ancestor, we have to build this ancestor recursively.
4213 -- In both cases, discriminants of the ancestor type do not play a role in
4214 -- the resolution of the needed components, because inherited discriminants
4215 -- cannot be used in a type extension. As a result we can compute
4216 -- independently the list of components of the ancestor type and of the
4217 -- expected type.
4219 procedure Resolve_Extension_Aggregate (N : Node_Id; Typ : Entity_Id) is
4220 A : constant Node_Id := Ancestor_Part (N);
4221 A_Type : Entity_Id;
4222 I : Interp_Index;
4223 It : Interp;
4225 function Valid_Limited_Ancestor (Anc : Node_Id) return Boolean;
4226 -- If the type is limited, verify that the ancestor part is a legal
4227 -- expression (aggregate or function call, including 'Input)) that does
4228 -- not require a copy, as specified in 7.5(2).
4230 function Valid_Ancestor_Type return Boolean;
4231 -- Verify that the type of the ancestor part is a non-private ancestor
4232 -- of the expected type, which must be a type extension.
4234 ----------------------------
4235 -- Valid_Limited_Ancestor --
4236 ----------------------------
4238 function Valid_Limited_Ancestor (Anc : Node_Id) return Boolean is
4239 begin
4240 if Is_Entity_Name (Anc) and then Is_Type (Entity (Anc)) then
4241 return True;
4243 -- The ancestor must be a call or an aggregate, but a call may
4244 -- have been expanded into a temporary, so check original node.
4246 elsif Nkind (Anc) in N_Aggregate
4247 | N_Extension_Aggregate
4248 | N_Function_Call
4249 then
4250 return True;
4252 elsif Nkind (Original_Node (Anc)) = N_Function_Call then
4253 return True;
4255 elsif Nkind (Anc) = N_Attribute_Reference
4256 and then Attribute_Name (Anc) = Name_Input
4257 then
4258 return True;
4260 elsif Nkind (Anc) = N_Qualified_Expression then
4261 return Valid_Limited_Ancestor (Expression (Anc));
4263 elsif Nkind (Anc) = N_Raise_Expression then
4264 return True;
4266 else
4267 return False;
4268 end if;
4269 end Valid_Limited_Ancestor;
4271 -------------------------
4272 -- Valid_Ancestor_Type --
4273 -------------------------
4275 function Valid_Ancestor_Type return Boolean is
4276 Imm_Type : Entity_Id;
4278 begin
4279 Imm_Type := Base_Type (Typ);
4280 while Is_Derived_Type (Imm_Type) loop
4281 if Etype (Imm_Type) = Base_Type (A_Type) then
4282 return True;
4284 -- The base type of the parent type may appear as a private
4285 -- extension if it is declared as such in a parent unit of the
4286 -- current one. For consistency of the subsequent analysis use
4287 -- the partial view for the ancestor part.
4289 elsif Is_Private_Type (Etype (Imm_Type))
4290 and then Present (Full_View (Etype (Imm_Type)))
4291 and then Base_Type (A_Type) = Full_View (Etype (Imm_Type))
4292 then
4293 A_Type := Etype (Imm_Type);
4294 return True;
4296 -- The parent type may be a private extension. The aggregate is
4297 -- legal if the type of the aggregate is an extension of it that
4298 -- is not a private extension.
4300 elsif Is_Private_Type (A_Type)
4301 and then not Is_Private_Type (Imm_Type)
4302 and then Present (Full_View (A_Type))
4303 and then Base_Type (Full_View (A_Type)) = Etype (Imm_Type)
4304 then
4305 return True;
4307 -- The parent type may be a raise expression (which is legal in
4308 -- any expression context).
4310 elsif A_Type = Raise_Type then
4311 A_Type := Etype (Imm_Type);
4312 return True;
4314 else
4315 Imm_Type := Etype (Base_Type (Imm_Type));
4316 end if;
4317 end loop;
4319 -- If previous loop did not find a proper ancestor, report error
4321 Error_Msg_NE ("expect ancestor type of &", A, Typ);
4322 return False;
4323 end Valid_Ancestor_Type;
4325 -- Start of processing for Resolve_Extension_Aggregate
4327 begin
4328 -- Analyze the ancestor part and account for the case where it is a
4329 -- parameterless function call.
4331 Analyze (A);
4332 Check_Parameterless_Call (A);
4334 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
4336 -- AI05-0115: If the ancestor part is a subtype mark, the ancestor
4337 -- must not have unknown discriminants. To catch cases where the
4338 -- aggregate occurs at a place where the full view of the ancestor
4339 -- type is visible and doesn't have unknown discriminants, but the
4340 -- aggregate type was derived from a partial view that has unknown
4341 -- discriminants, we check whether the aggregate type has unknown
4342 -- discriminants (unknown discriminants were inherited), along
4343 -- with checking that the partial view of the ancestor has unknown
4344 -- discriminants. (It might be sufficient to replace the entire
4345 -- condition with Has_Unknown_Discriminants (Typ), but that might
4346 -- miss some cases, not clear, and causes error changes in some tests
4347 -- such as class-wide cases, that aren't clearly improvements. ???)
4349 if Has_Unknown_Discriminants (Entity (A))
4350 or else (Has_Unknown_Discriminants (Typ)
4351 and then Partial_View_Has_Unknown_Discr (Entity (A)))
4352 then
4353 Error_Msg_NE
4354 ("aggregate not available for type& whose ancestor "
4355 & "has unknown discriminants", N, Typ);
4356 end if;
4357 end if;
4359 if not Is_Tagged_Type (Typ) then
4360 Error_Msg_N ("type of extension aggregate must be tagged", N);
4361 return;
4363 elsif Is_Limited_Type (Typ) then
4365 -- Ada 2005 (AI-287): Limited aggregates are allowed
4367 if Ada_Version < Ada_2005 then
4368 Error_Msg_N ("aggregate type cannot be limited", N);
4369 Explain_Limited_Type (Typ, N);
4370 return;
4372 elsif Valid_Limited_Ancestor (A) then
4373 null;
4375 else
4376 Error_Msg_N
4377 ("limited ancestor part must be aggregate or function call", A);
4378 end if;
4380 elsif Is_Class_Wide_Type (Typ) then
4381 Error_Msg_N ("aggregate cannot be of a class-wide type", N);
4382 return;
4383 end if;
4385 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
4386 A_Type := Get_Full_View (Entity (A));
4388 if Valid_Ancestor_Type then
4389 Set_Entity (A, A_Type);
4390 Set_Etype (A, A_Type);
4392 Validate_Ancestor_Part (N);
4393 Resolve_Record_Aggregate (N, Typ);
4394 end if;
4396 elsif Nkind (A) /= N_Aggregate then
4397 if Is_Overloaded (A) then
4398 A_Type := Any_Type;
4400 Get_First_Interp (A, I, It);
4401 while Present (It.Typ) loop
4403 -- Consider limited interpretations if Ada 2005 or higher
4405 if Is_Tagged_Type (It.Typ)
4406 and then (Ada_Version >= Ada_2005
4407 or else not Is_Limited_Type (It.Typ))
4408 then
4409 if A_Type /= Any_Type then
4410 Error_Msg_N ("cannot resolve expression", A);
4411 return;
4412 else
4413 A_Type := It.Typ;
4414 end if;
4415 end if;
4417 Get_Next_Interp (I, It);
4418 end loop;
4420 if A_Type = Any_Type then
4421 if Ada_Version >= Ada_2005 then
4422 Error_Msg_N
4423 ("ancestor part must be of a tagged type", A);
4424 else
4425 Error_Msg_N
4426 ("ancestor part must be of a nonlimited tagged type", A);
4427 end if;
4429 return;
4430 end if;
4432 else
4433 A_Type := Etype (A);
4434 end if;
4436 if Valid_Ancestor_Type then
4437 Resolve (A, A_Type);
4438 Check_Unset_Reference (A);
4439 Check_Non_Static_Context (A);
4441 -- The aggregate is illegal if the ancestor expression is a call
4442 -- to a function with a limited unconstrained result, unless the
4443 -- type of the aggregate is a null extension. This restriction
4444 -- was added in AI05-67 to simplify implementation.
4446 if Nkind (A) = N_Function_Call
4447 and then Is_Limited_Type (A_Type)
4448 and then not Is_Null_Extension (Typ)
4449 and then not Is_Constrained (A_Type)
4450 then
4451 Error_Msg_N
4452 ("type of limited ancestor part must be constrained", A);
4454 -- Reject the use of CPP constructors that leave objects partially
4455 -- initialized. For example:
4457 -- type CPP_Root is tagged limited record ...
4458 -- pragma Import (CPP, CPP_Root);
4460 -- type CPP_DT is new CPP_Root and Iface ...
4461 -- pragma Import (CPP, CPP_DT);
4463 -- type Ada_DT is new CPP_DT with ...
4465 -- Obj : Ada_DT := Ada_DT'(New_CPP_Root with others => <>);
4467 -- Using the constructor of CPP_Root the slots of the dispatch
4468 -- table of CPP_DT cannot be set, and the secondary tag of
4469 -- CPP_DT is unknown.
4471 elsif Nkind (A) = N_Function_Call
4472 and then Is_CPP_Constructor_Call (A)
4473 and then Enclosing_CPP_Parent (Typ) /= A_Type
4474 then
4475 Error_Msg_NE
4476 ("??must use 'C'P'P constructor for type &", A,
4477 Enclosing_CPP_Parent (Typ));
4479 -- The following call is not needed if the previous warning
4480 -- is promoted to an error.
4482 Resolve_Record_Aggregate (N, Typ);
4484 elsif Is_Class_Wide_Type (Etype (A))
4485 and then Nkind (Original_Node (A)) = N_Function_Call
4486 then
4487 -- If the ancestor part is a dispatching call, it appears
4488 -- statically to be a legal ancestor, but it yields any member
4489 -- of the class, and it is not possible to determine whether
4490 -- it is an ancestor of the extension aggregate (much less
4491 -- which ancestor). It is not possible to determine the
4492 -- components of the extension part.
4494 -- This check implements AI-306, which in fact was motivated by
4495 -- an AdaCore query to the ARG after this test was added.
4497 Error_Msg_N ("ancestor part must be statically tagged", A);
4499 else
4500 Resolve_Record_Aggregate (N, Typ);
4501 end if;
4502 end if;
4504 else
4505 Error_Msg_N ("no unique type for this aggregate", A);
4506 end if;
4508 Check_Function_Writable_Actuals (N);
4509 end Resolve_Extension_Aggregate;
4511 ----------------------------------
4512 -- Resolve_Null_Array_Aggregate --
4513 ----------------------------------
4515 function Resolve_Null_Array_Aggregate (N : Node_Id) return Boolean is
4516 -- Never returns False, but declared as a function to match
4517 -- other Resolve_Mumble functions.
4519 Loc : constant Source_Ptr := Sloc (N);
4520 Typ : constant Entity_Id := Etype (N);
4522 Index : Node_Id;
4523 Lo, Hi : Node_Id;
4524 Constr : constant List_Id := New_List;
4526 begin
4527 -- Attach the list of constraints at the location of the aggregate, so
4528 -- the individual constraints can be analyzed.
4530 Set_Parent (Constr, N);
4532 -- Create a constrained subtype with null dimensions
4534 Index := First_Index (Typ);
4535 while Present (Index) loop
4536 Get_Index_Bounds (Index, L => Lo, H => Hi);
4538 -- The upper bound is the predecessor of the lower bound
4540 Hi := Make_Attribute_Reference
4541 (Loc,
4542 Prefix => New_Occurrence_Of (Etype (Index), Loc),
4543 Attribute_Name => Name_Pred,
4544 Expressions => New_List (New_Copy_Tree (Lo)));
4546 Append (Make_Range (Loc, New_Copy_Tree (Lo), Hi), Constr);
4547 Analyze_And_Resolve (Last (Constr), Etype (Index));
4549 Next_Index (Index);
4550 end loop;
4552 Set_Compile_Time_Known_Aggregate (N);
4553 Set_Aggregate_Bounds (N, First (Constr));
4555 return True;
4556 end Resolve_Null_Array_Aggregate;
4558 ------------------------------
4559 -- Resolve_Record_Aggregate --
4560 ------------------------------
4562 procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id) is
4563 New_Assoc_List : constant List_Id := New_List;
4564 -- New_Assoc_List is the newly built list of N_Component_Association
4565 -- nodes.
4567 Others_Etype : Entity_Id := Empty;
4568 -- This variable is used to save the Etype of the last record component
4569 -- that takes its value from the others choice. Its purpose is:
4571 -- (a) make sure the others choice is useful
4573 -- (b) make sure the type of all the components whose value is
4574 -- subsumed by the others choice are the same.
4576 -- This variable is updated as a side effect of function Get_Value.
4578 Box_Node : Node_Id := Empty;
4579 Is_Box_Present : Boolean := False;
4580 Is_Box_Init_By_Default : Boolean := False;
4581 Others_Box : Natural := 0;
4582 -- Ada 2005 (AI-287): Variables used in case of default initialization
4583 -- to provide a functionality similar to Others_Etype. Box_Present
4584 -- indicates that the component takes its default initialization;
4585 -- Others_Box counts the number of components of the current aggregate
4586 -- (which may be a sub-aggregate of a larger one) that are default-
4587 -- initialized. A value of One indicates that an others_box is present.
4588 -- Any larger value indicates that the others_box is not redundant.
4589 -- These variables, similar to Others_Etype, are also updated as a side
4590 -- effect of function Get_Value. Box_Node is used to place a warning on
4591 -- a redundant others_box.
4593 procedure Add_Association
4594 (Component : Entity_Id;
4595 Expr : Node_Id;
4596 Assoc_List : List_Id;
4597 Is_Box_Present : Boolean := False);
4598 -- Builds a new N_Component_Association node which associates Component
4599 -- to expression Expr and adds it to the association list being built,
4600 -- either New_Assoc_List, or the association being built for an inner
4601 -- aggregate.
4603 function Discriminant_Present (Input_Discr : Entity_Id) return Boolean;
4604 -- If aggregate N is a regular aggregate this routine will return True.
4605 -- Otherwise, if N is an extension aggregate, then Input_Discr denotes
4606 -- a discriminant whose value may already have been specified by N's
4607 -- ancestor part. This routine checks whether this is indeed the case
4608 -- and if so returns False, signaling that no value for Input_Discr
4609 -- should appear in N's aggregate part. Also, in this case, the routine
4610 -- appends to New_Assoc_List the discriminant value specified in the
4611 -- ancestor part.
4613 -- If the aggregate is in a context with expansion delayed, it will be
4614 -- reanalyzed. The inherited discriminant values must not be reinserted
4615 -- in the component list to prevent spurious errors, but they must be
4616 -- present on first analysis to build the proper subtype indications.
4617 -- The flag Inherited_Discriminant is used to prevent the re-insertion.
4619 function Find_Private_Ancestor (Typ : Entity_Id) return Entity_Id;
4620 -- AI05-0115: Find earlier ancestor in the derivation chain that is
4621 -- derived from private view Typ. Whether the aggregate is legal depends
4622 -- on the current visibility of the type as well as that of the parent
4623 -- of the ancestor.
4625 function Get_Value
4626 (Compon : Entity_Id;
4627 From : List_Id;
4628 Consider_Others_Choice : Boolean := False) return Node_Id;
4629 -- Given a record component stored in parameter Compon, this function
4630 -- returns its value as it appears in the list From, which is a list
4631 -- of N_Component_Association nodes.
4633 -- If no component association has a choice for the searched component,
4634 -- the value provided by the others choice is returned, if there is one,
4635 -- and Consider_Others_Choice is set to true. Otherwise Empty is
4636 -- returned. If there is more than one component association giving a
4637 -- value for the searched record component, an error message is emitted
4638 -- and the first found value is returned.
4640 -- If Consider_Others_Choice is set and the returned expression comes
4641 -- from the others choice, then Others_Etype is set as a side effect.
4642 -- An error message is emitted if the components taking their value from
4643 -- the others choice do not have same type.
4645 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Entity_Id);
4646 -- Analyzes and resolves expression Expr against the Etype of the
4647 -- Component. This routine also applies all appropriate checks to Expr.
4648 -- It finally saves a Expr in the newly created association list that
4649 -- will be attached to the final record aggregate. Note that if the
4650 -- Parent pointer of Expr is not set then Expr was produced with a
4651 -- New_Copy_Tree or some such.
4653 procedure Rewrite_Range (Root_Type : Entity_Id; Rge : Node_Id);
4654 -- Rewrite a range node Rge when its bounds refer to non-stored
4655 -- discriminants from Root_Type, to replace them with the stored
4656 -- discriminant values. This is required in GNATprove mode, and is
4657 -- adopted in all modes to avoid special-casing GNATprove mode.
4659 ---------------------
4660 -- Add_Association --
4661 ---------------------
4663 procedure Add_Association
4664 (Component : Entity_Id;
4665 Expr : Node_Id;
4666 Assoc_List : List_Id;
4667 Is_Box_Present : Boolean := False)
4669 Choice_List : constant List_Id := New_List;
4670 Loc : Source_Ptr;
4672 begin
4673 -- If this is a box association the expression is missing, so use the
4674 -- Sloc of the aggregate itself for the new association.
4676 pragma Assert (Present (Expr) xor Is_Box_Present);
4678 if Present (Expr) then
4679 Loc := Sloc (Expr);
4680 else
4681 Loc := Sloc (N);
4682 end if;
4684 Append_To (Choice_List, New_Occurrence_Of (Component, Loc));
4686 Append_To (Assoc_List,
4687 Make_Component_Association (Loc,
4688 Choices => Choice_List,
4689 Expression => Expr,
4690 Box_Present => Is_Box_Present));
4692 -- If this association has a box for a component that is initialized
4693 -- by default, then set flag on the new association to indicate that
4694 -- the original association was for such a box-initialized component.
4696 if Is_Box_Init_By_Default then
4697 Set_Was_Default_Init_Box_Association (Last (Assoc_List));
4698 end if;
4699 end Add_Association;
4701 --------------------------
4702 -- Discriminant_Present --
4703 --------------------------
4705 function Discriminant_Present (Input_Discr : Entity_Id) return Boolean is
4706 Regular_Aggr : constant Boolean := Nkind (N) /= N_Extension_Aggregate;
4708 Ancestor_Is_Subtyp : Boolean;
4710 Loc : Source_Ptr;
4712 Ancestor : Node_Id;
4713 Ancestor_Typ : Entity_Id;
4714 Comp_Assoc : Node_Id;
4715 Discr : Entity_Id;
4716 Discr_Expr : Node_Id;
4717 Discr_Val : Elmt_Id := No_Elmt;
4718 Orig_Discr : Entity_Id;
4720 begin
4721 if Regular_Aggr then
4722 return True;
4723 end if;
4725 -- Check whether inherited discriminant values have already been
4726 -- inserted in the aggregate. This will be the case if we are
4727 -- re-analyzing an aggregate whose expansion was delayed.
4729 if Present (Component_Associations (N)) then
4730 Comp_Assoc := First (Component_Associations (N));
4731 while Present (Comp_Assoc) loop
4732 if Inherited_Discriminant (Comp_Assoc) then
4733 return True;
4734 end if;
4736 Next (Comp_Assoc);
4737 end loop;
4738 end if;
4740 Ancestor := Ancestor_Part (N);
4741 Ancestor_Typ := Etype (Ancestor);
4742 Loc := Sloc (Ancestor);
4744 -- For a private type with unknown discriminants, use the underlying
4745 -- record view if it is available.
4747 if Has_Unknown_Discriminants (Ancestor_Typ)
4748 and then Present (Full_View (Ancestor_Typ))
4749 and then Present (Underlying_Record_View (Full_View (Ancestor_Typ)))
4750 then
4751 Ancestor_Typ := Underlying_Record_View (Full_View (Ancestor_Typ));
4752 end if;
4754 Ancestor_Is_Subtyp :=
4755 Is_Entity_Name (Ancestor) and then Is_Type (Entity (Ancestor));
4757 -- If the ancestor part has no discriminants clearly N's aggregate
4758 -- part must provide a value for Discr.
4760 if not Has_Discriminants (Ancestor_Typ) then
4761 return True;
4763 -- If the ancestor part is an unconstrained subtype mark then the
4764 -- Discr must be present in N's aggregate part.
4766 elsif Ancestor_Is_Subtyp
4767 and then not Is_Constrained (Entity (Ancestor))
4768 then
4769 return True;
4770 end if;
4772 -- Now look to see if Discr was specified in the ancestor part
4774 if Ancestor_Is_Subtyp then
4775 Discr_Val :=
4776 First_Elmt (Discriminant_Constraint (Entity (Ancestor)));
4777 end if;
4779 Orig_Discr := Original_Record_Component (Input_Discr);
4781 Discr := First_Discriminant (Ancestor_Typ);
4782 while Present (Discr) loop
4784 -- If Ancestor has already specified Disc value then insert its
4785 -- value in the final aggregate.
4787 if Original_Record_Component (Discr) = Orig_Discr then
4788 if Ancestor_Is_Subtyp then
4789 Discr_Expr := New_Copy_Tree (Node (Discr_Val));
4790 else
4791 Discr_Expr :=
4792 Make_Selected_Component (Loc,
4793 Prefix => Duplicate_Subexpr (Ancestor),
4794 Selector_Name => New_Occurrence_Of (Input_Discr, Loc));
4795 end if;
4797 Resolve_Aggr_Expr (Discr_Expr, Input_Discr);
4798 Set_Inherited_Discriminant (Last (New_Assoc_List));
4799 return False;
4800 end if;
4802 Next_Discriminant (Discr);
4804 if Ancestor_Is_Subtyp then
4805 Next_Elmt (Discr_Val);
4806 end if;
4807 end loop;
4809 return True;
4810 end Discriminant_Present;
4812 ---------------------------
4813 -- Find_Private_Ancestor --
4814 ---------------------------
4816 function Find_Private_Ancestor (Typ : Entity_Id) return Entity_Id is
4817 Par : Entity_Id;
4819 begin
4820 Par := Typ;
4821 loop
4822 if Has_Private_Ancestor (Par)
4823 and then not Has_Private_Ancestor (Etype (Base_Type (Par)))
4824 then
4825 return Par;
4827 elsif not Is_Derived_Type (Par) then
4828 return Empty;
4830 else
4831 Par := Etype (Base_Type (Par));
4832 end if;
4833 end loop;
4834 end Find_Private_Ancestor;
4836 ---------------
4837 -- Get_Value --
4838 ---------------
4840 function Get_Value
4841 (Compon : Entity_Id;
4842 From : List_Id;
4843 Consider_Others_Choice : Boolean := False) return Node_Id
4845 Typ : constant Entity_Id := Etype (Compon);
4846 Assoc : Node_Id;
4847 Expr : Node_Id := Empty;
4848 Selector_Name : Node_Id;
4850 begin
4851 Is_Box_Present := False;
4852 Is_Box_Init_By_Default := False;
4854 if No (From) then
4855 return Empty;
4856 end if;
4858 Assoc := First (From);
4859 while Present (Assoc) loop
4860 Selector_Name := First (Choices (Assoc));
4861 while Present (Selector_Name) loop
4862 if Nkind (Selector_Name) = N_Others_Choice then
4863 if Consider_Others_Choice and then No (Expr) then
4865 -- We need to duplicate the expression for each
4866 -- successive component covered by the others choice.
4867 -- This is redundant if the others_choice covers only
4868 -- one component (small optimization possible???), but
4869 -- indispensable otherwise, because each one must be
4870 -- expanded individually to preserve side effects.
4872 -- Ada 2005 (AI-287): In case of default initialization
4873 -- of components, we duplicate the corresponding default
4874 -- expression (from the record type declaration). The
4875 -- copy must carry the sloc of the association (not the
4876 -- original expression) to prevent spurious elaboration
4877 -- checks when the default includes function calls.
4879 if Box_Present (Assoc) then
4880 Others_Box := Others_Box + 1;
4881 Is_Box_Present := True;
4883 if Expander_Active then
4884 return
4885 New_Copy_Tree_And_Copy_Dimensions
4886 (Expression (Parent (Compon)),
4887 New_Sloc => Sloc (Assoc));
4888 else
4889 return Expression (Parent (Compon));
4890 end if;
4892 else
4893 if Present (Others_Etype)
4894 and then Base_Type (Others_Etype) /= Base_Type (Typ)
4895 then
4896 -- If the components are of an anonymous access
4897 -- type they are distinct, but this is legal in
4898 -- Ada 2012 as long as designated types match.
4900 if (Ekind (Typ) = E_Anonymous_Access_Type
4901 or else Ekind (Typ) =
4902 E_Anonymous_Access_Subprogram_Type)
4903 and then Designated_Type (Typ) =
4904 Designated_Type (Others_Etype)
4905 then
4906 null;
4907 else
4908 Error_Msg_N
4909 ("components in OTHERS choice must have same "
4910 & "type", Selector_Name);
4911 end if;
4912 end if;
4914 Others_Etype := Typ;
4916 -- Copy the expression so that it is resolved
4917 -- independently for each component, This is needed
4918 -- for accessibility checks on components of anonymous
4919 -- access types, even in compile_only mode.
4921 if not Inside_A_Generic then
4922 return
4923 New_Copy_Tree_And_Copy_Dimensions
4924 (Expression (Assoc));
4925 else
4926 return Expression (Assoc);
4927 end if;
4928 end if;
4929 end if;
4931 elsif Chars (Compon) = Chars (Selector_Name) then
4932 if No (Expr) then
4934 -- Ada 2005 (AI-231)
4936 if Ada_Version >= Ada_2005
4937 and then Known_Null (Expression (Assoc))
4938 then
4939 Check_Can_Never_Be_Null (Compon, Expression (Assoc));
4940 end if;
4942 -- We need to duplicate the expression when several
4943 -- components are grouped together with a "|" choice.
4944 -- For instance "filed1 | filed2 => Expr"
4946 -- Ada 2005 (AI-287)
4948 if Box_Present (Assoc) then
4949 Is_Box_Present := True;
4951 -- Duplicate the default expression of the component
4952 -- from the record type declaration, so a new copy
4953 -- can be attached to the association.
4955 -- Note that we always copy the default expression,
4956 -- even when the association has a single choice, in
4957 -- order to create a proper association for the
4958 -- expanded aggregate.
4960 -- Component may have no default, in which case the
4961 -- expression is empty and the component is default-
4962 -- initialized, but an association for the component
4963 -- exists, and it is not covered by an others clause.
4965 -- Scalar and private types have no initialization
4966 -- procedure, so they remain uninitialized. If the
4967 -- target of the aggregate is a constant this
4968 -- deserves a warning.
4970 if No (Expression (Parent (Compon)))
4971 and then not Has_Non_Null_Base_Init_Proc (Typ)
4972 and then not Has_Aspect (Typ, Aspect_Default_Value)
4973 and then not Is_Concurrent_Type (Typ)
4974 and then Nkind (Parent (N)) = N_Object_Declaration
4975 and then Constant_Present (Parent (N))
4976 then
4977 Error_Msg_Node_2 := Typ;
4978 Error_Msg_NE
4979 ("??component& of type& is uninitialized",
4980 Assoc, Selector_Name);
4982 -- An additional reminder if the component type
4983 -- is a generic formal.
4985 if Is_Generic_Type (Base_Type (Typ)) then
4986 Error_Msg_NE
4987 ("\instance should provide actual type with "
4988 & "initialization for&", Assoc, Typ);
4989 end if;
4990 end if;
4992 return
4993 New_Copy_Tree_And_Copy_Dimensions
4994 (Expression (Parent (Compon)));
4996 else
4997 if Present (Next (Selector_Name)) then
4998 Expr := New_Copy_Tree_And_Copy_Dimensions
4999 (Expression (Assoc));
5000 else
5001 Expr := Expression (Assoc);
5002 end if;
5003 end if;
5005 Generate_Reference (Compon, Selector_Name, 'm');
5007 else
5008 Error_Msg_NE
5009 ("more than one value supplied for &",
5010 Selector_Name, Compon);
5012 end if;
5013 end if;
5015 Next (Selector_Name);
5016 end loop;
5018 Next (Assoc);
5019 end loop;
5021 return Expr;
5022 end Get_Value;
5024 -----------------------
5025 -- Resolve_Aggr_Expr --
5026 -----------------------
5028 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Entity_Id) is
5029 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean;
5030 -- If the expression is an aggregate (possibly qualified) then its
5031 -- expansion is delayed until the enclosing aggregate is expanded
5032 -- into assignments. In that case, do not generate checks on the
5033 -- expression, because they will be generated later, and will other-
5034 -- wise force a copy (to remove side effects) that would leave a
5035 -- dynamic-sized aggregate in the code, something that gigi cannot
5036 -- handle.
5038 ---------------------------
5039 -- Has_Expansion_Delayed --
5040 ---------------------------
5042 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean is
5043 begin
5044 return
5045 (Nkind (Expr) in N_Aggregate | N_Extension_Aggregate
5046 and then Present (Etype (Expr))
5047 and then Is_Record_Type (Etype (Expr))
5048 and then Expansion_Delayed (Expr))
5049 or else
5050 (Nkind (Expr) = N_Qualified_Expression
5051 and then Has_Expansion_Delayed (Expression (Expr)));
5052 end Has_Expansion_Delayed;
5054 -- Local variables
5056 Expr_Type : Entity_Id := Empty;
5057 New_C : Entity_Id := Component;
5058 New_Expr : Node_Id;
5060 Relocate : Boolean;
5061 -- Set to True if the resolved Expr node needs to be relocated when
5062 -- attached to the newly created association list. This node need not
5063 -- be relocated if its parent pointer is not set. In fact in this
5064 -- case Expr is the output of a New_Copy_Tree call. If Relocate is
5065 -- True then we have analyzed the expression node in the original
5066 -- aggregate and hence it needs to be relocated when moved over to
5067 -- the new association list.
5069 -- Start of processing for Resolve_Aggr_Expr
5071 begin
5072 -- If the type of the component is elementary or the type of the
5073 -- aggregate does not contain discriminants, use the type of the
5074 -- component to resolve Expr.
5076 if Is_Elementary_Type (Etype (Component))
5077 or else not Has_Discriminants (Etype (N))
5078 then
5079 Expr_Type := Etype (Component);
5081 -- Otherwise we have to pick up the new type of the component from
5082 -- the new constrained subtype of the aggregate. In fact components
5083 -- which are of a composite type might be constrained by a
5084 -- discriminant, and we want to resolve Expr against the subtype were
5085 -- all discriminant occurrences are replaced with their actual value.
5087 else
5088 New_C := First_Component (Etype (N));
5089 while Present (New_C) loop
5090 if Chars (New_C) = Chars (Component) then
5091 Expr_Type := Etype (New_C);
5092 exit;
5093 end if;
5095 Next_Component (New_C);
5096 end loop;
5098 pragma Assert (Present (Expr_Type));
5100 -- For each range in an array type where a discriminant has been
5101 -- replaced with the constraint, check that this range is within
5102 -- the range of the base type. This checks is done in the init
5103 -- proc for regular objects, but has to be done here for
5104 -- aggregates since no init proc is called for them.
5106 if Is_Array_Type (Expr_Type) then
5107 declare
5108 Index : Node_Id;
5109 -- Range of the current constrained index in the array
5111 Orig_Index : Node_Id := First_Index (Etype (Component));
5112 -- Range corresponding to the range Index above in the
5113 -- original unconstrained record type. The bounds of this
5114 -- range may be governed by discriminants.
5116 Unconstr_Index : Node_Id := First_Index (Etype (Expr_Type));
5117 -- Range corresponding to the range Index above for the
5118 -- unconstrained array type. This range is needed to apply
5119 -- range checks.
5121 begin
5122 Index := First_Index (Expr_Type);
5123 while Present (Index) loop
5124 if Depends_On_Discriminant (Orig_Index) then
5125 Apply_Range_Check (Index, Etype (Unconstr_Index));
5126 end if;
5128 Next_Index (Index);
5129 Next_Index (Orig_Index);
5130 Next_Index (Unconstr_Index);
5131 end loop;
5132 end;
5133 end if;
5134 end if;
5136 -- If the Parent pointer of Expr is not set, Expr is an expression
5137 -- duplicated by New_Tree_Copy (this happens for record aggregates
5138 -- that look like (Field1 | Filed2 => Expr) or (others => Expr)).
5139 -- Such a duplicated expression must be attached to the tree
5140 -- before analysis and resolution to enforce the rule that a tree
5141 -- fragment should never be analyzed or resolved unless it is
5142 -- attached to the current compilation unit.
5144 if No (Parent (Expr)) then
5145 Set_Parent (Expr, N);
5146 Relocate := False;
5147 else
5148 Relocate := True;
5149 end if;
5151 Analyze_And_Resolve (Expr, Expr_Type);
5152 Check_Expr_OK_In_Limited_Aggregate (Expr);
5153 Check_Non_Static_Context (Expr);
5154 Check_Unset_Reference (Expr);
5156 -- Check wrong use of class-wide types
5158 if Is_Class_Wide_Type (Etype (Expr)) then
5159 Error_Msg_N ("dynamically tagged expression not allowed", Expr);
5160 end if;
5162 if not Has_Expansion_Delayed (Expr) then
5163 Aggregate_Constraint_Checks (Expr, Expr_Type);
5164 end if;
5166 -- If an aggregate component has a type with predicates, an explicit
5167 -- predicate check must be applied, as for an assignment statement,
5168 -- because the aggregate might not be expanded into individual
5169 -- component assignments.
5171 if Has_Predicates (Expr_Type)
5172 and then Analyzed (Expr)
5173 then
5174 Apply_Predicate_Check (Expr, Expr_Type);
5175 end if;
5177 if Raises_Constraint_Error (Expr) then
5178 Set_Raises_Constraint_Error (N);
5179 end if;
5181 -- If the expression has been marked as requiring a range check, then
5182 -- generate it here. It's a bit odd to be generating such checks in
5183 -- the analyzer, but harmless since Generate_Range_Check does nothing
5184 -- (other than making sure Do_Range_Check is set) if the expander is
5185 -- not active.
5187 if Do_Range_Check (Expr) then
5188 Generate_Range_Check (Expr, Expr_Type, CE_Range_Check_Failed);
5189 end if;
5191 -- Add association Component => Expr if the caller requests it
5193 if Relocate then
5194 New_Expr := Relocate_Node (Expr);
5196 -- Since New_Expr is not gonna be analyzed later on, we need to
5197 -- propagate here the dimensions form Expr to New_Expr.
5199 Copy_Dimensions (Expr, New_Expr);
5201 else
5202 New_Expr := Expr;
5203 end if;
5205 Add_Association (New_C, New_Expr, New_Assoc_List);
5206 end Resolve_Aggr_Expr;
5208 -------------------
5209 -- Rewrite_Range --
5210 -------------------
5212 procedure Rewrite_Range (Root_Type : Entity_Id; Rge : Node_Id) is
5213 procedure Rewrite_Bound
5214 (Bound : Node_Id;
5215 Disc : Entity_Id;
5216 Expr_Disc : Node_Id);
5217 -- Rewrite a bound of the range Bound, when it is equal to the
5218 -- non-stored discriminant Disc, into the stored discriminant
5219 -- value Expr_Disc.
5221 -------------------
5222 -- Rewrite_Bound --
5223 -------------------
5225 procedure Rewrite_Bound
5226 (Bound : Node_Id;
5227 Disc : Entity_Id;
5228 Expr_Disc : Node_Id)
5230 begin
5231 if Nkind (Bound) /= N_Identifier then
5232 return;
5233 end if;
5235 -- We expect either the discriminant or the discriminal
5237 if Entity (Bound) = Disc
5238 or else (Ekind (Entity (Bound)) = E_In_Parameter
5239 and then Discriminal_Link (Entity (Bound)) = Disc)
5240 then
5241 Rewrite (Bound, New_Copy_Tree (Expr_Disc));
5242 end if;
5243 end Rewrite_Bound;
5245 -- Local variables
5247 Low, High : Node_Id;
5248 Disc : Entity_Id;
5249 Expr_Disc : Elmt_Id;
5251 -- Start of processing for Rewrite_Range
5253 begin
5254 if Has_Discriminants (Root_Type) and then Nkind (Rge) = N_Range then
5255 Low := Low_Bound (Rge);
5256 High := High_Bound (Rge);
5258 Disc := First_Discriminant (Root_Type);
5259 Expr_Disc := First_Elmt (Stored_Constraint (Etype (N)));
5260 while Present (Disc) loop
5261 Rewrite_Bound (Low, Disc, Node (Expr_Disc));
5262 Rewrite_Bound (High, Disc, Node (Expr_Disc));
5263 Next_Discriminant (Disc);
5264 Next_Elmt (Expr_Disc);
5265 end loop;
5266 end if;
5267 end Rewrite_Range;
5269 -- Local variables
5271 Components : constant Elist_Id := New_Elmt_List;
5272 -- Components is the list of the record components whose value must be
5273 -- provided in the aggregate. This list does include discriminants.
5275 Component : Entity_Id;
5276 Component_Elmt : Elmt_Id;
5277 Expr : Node_Id;
5278 Positional_Expr : Node_Id;
5280 -- Start of processing for Resolve_Record_Aggregate
5282 begin
5283 -- A record aggregate is restricted in SPARK:
5285 -- Each named association can have only a single choice.
5286 -- OTHERS cannot be used.
5287 -- Positional and named associations cannot be mixed.
5289 if Present (Component_Associations (N)) then
5290 declare
5291 Assoc : Node_Id;
5293 begin
5294 Assoc := First (Component_Associations (N));
5295 while Present (Assoc) loop
5296 if Nkind (Assoc) = N_Iterated_Component_Association then
5297 Error_Msg_N
5298 ("iterated component association can only appear in an "
5299 & "array aggregate", N);
5300 raise Unrecoverable_Error;
5301 end if;
5303 Next (Assoc);
5304 end loop;
5305 end;
5306 end if;
5308 -- We may end up calling Duplicate_Subexpr on expressions that are
5309 -- attached to New_Assoc_List. For this reason we need to attach it
5310 -- to the tree by setting its parent pointer to N. This parent point
5311 -- will change in STEP 8 below.
5313 Set_Parent (New_Assoc_List, N);
5315 -- STEP 1: abstract type and null record verification
5317 if Is_Abstract_Type (Typ) then
5318 Error_Msg_N ("type of aggregate cannot be abstract", N);
5319 end if;
5321 if No (First_Entity (Typ)) and then Null_Record_Present (N) then
5322 Set_Etype (N, Typ);
5323 return;
5325 elsif Present (First_Entity (Typ))
5326 and then Null_Record_Present (N)
5327 and then not Is_Tagged_Type (Typ)
5328 then
5329 Error_Msg_N ("record aggregate cannot be null", N);
5330 return;
5332 -- If the type has no components, then the aggregate should either
5333 -- have "null record", or in Ada 2005 it could instead have a single
5334 -- component association given by "others => <>". For Ada 95 we flag an
5335 -- error at this point, but for Ada 2005 we proceed with checking the
5336 -- associations below, which will catch the case where it's not an
5337 -- aggregate with "others => <>". Note that the legality of a <>
5338 -- aggregate for a null record type was established by AI05-016.
5340 elsif No (First_Entity (Typ))
5341 and then Ada_Version < Ada_2005
5342 then
5343 Error_Msg_N ("record aggregate must be null", N);
5344 return;
5345 end if;
5347 -- A record aggregate can only use parentheses
5349 if Nkind (N) = N_Aggregate
5350 and then Is_Homogeneous_Aggregate (N)
5351 then
5352 Error_Msg_N ("record aggregate must use (), not '[']", N);
5353 return;
5354 end if;
5356 -- STEP 2: Verify aggregate structure
5358 Step_2 : declare
5359 Assoc : Node_Id;
5360 Bad_Aggregate : Boolean := False;
5361 Selector_Name : Node_Id;
5363 begin
5364 if Present (Component_Associations (N)) then
5365 Assoc := First (Component_Associations (N));
5366 else
5367 Assoc := Empty;
5368 end if;
5370 while Present (Assoc) loop
5371 Selector_Name := First (Choices (Assoc));
5372 while Present (Selector_Name) loop
5373 if Nkind (Selector_Name) = N_Identifier then
5374 null;
5376 elsif Nkind (Selector_Name) = N_Others_Choice then
5377 if Selector_Name /= First (Choices (Assoc))
5378 or else Present (Next (Selector_Name))
5379 then
5380 Error_Msg_N
5381 ("OTHERS must appear alone in a choice list",
5382 Selector_Name);
5383 return;
5385 elsif Present (Next (Assoc)) then
5386 Error_Msg_N
5387 ("OTHERS must appear last in an aggregate",
5388 Selector_Name);
5389 return;
5391 -- (Ada 2005): If this is an association with a box,
5392 -- indicate that the association need not represent
5393 -- any component.
5395 elsif Box_Present (Assoc) then
5396 Others_Box := 1;
5397 Box_Node := Assoc;
5398 end if;
5400 else
5401 Error_Msg_N
5402 ("selector name should be identifier or OTHERS",
5403 Selector_Name);
5404 Bad_Aggregate := True;
5405 end if;
5407 Next (Selector_Name);
5408 end loop;
5410 Next (Assoc);
5411 end loop;
5413 if Bad_Aggregate then
5414 return;
5415 end if;
5416 end Step_2;
5418 -- STEP 3: Find discriminant Values
5420 Step_3 : declare
5421 Discrim : Entity_Id;
5422 Missing_Discriminants : Boolean := False;
5424 begin
5425 if Present (Expressions (N)) then
5426 Positional_Expr := First (Expressions (N));
5427 else
5428 Positional_Expr := Empty;
5429 end if;
5431 -- AI05-0115: if the ancestor part is a subtype mark, the ancestor
5432 -- must not have unknown discriminants.
5433 -- ??? We are not checking any subtype mark here and this code is not
5434 -- exercised by any test, so it's likely wrong (in particular
5435 -- we should not use Root_Type here but the subtype mark, if any),
5436 -- and possibly not needed.
5438 if Is_Derived_Type (Typ)
5439 and then Has_Unknown_Discriminants (Root_Type (Typ))
5440 and then Nkind (N) /= N_Extension_Aggregate
5441 then
5442 Error_Msg_NE
5443 ("aggregate not available for type& whose ancestor "
5444 & "has unknown discriminants", N, Typ);
5445 end if;
5447 if Has_Unknown_Discriminants (Typ)
5448 and then Present (Underlying_Record_View (Typ))
5449 then
5450 Discrim := First_Discriminant (Underlying_Record_View (Typ));
5451 elsif Has_Discriminants (Typ) then
5452 Discrim := First_Discriminant (Typ);
5453 else
5454 Discrim := Empty;
5455 end if;
5457 -- First find the discriminant values in the positional components
5459 while Present (Discrim) and then Present (Positional_Expr) loop
5460 if Discriminant_Present (Discrim) then
5461 Resolve_Aggr_Expr (Positional_Expr, Discrim);
5463 -- Ada 2005 (AI-231)
5465 if Ada_Version >= Ada_2005
5466 and then Known_Null (Positional_Expr)
5467 then
5468 Check_Can_Never_Be_Null (Discrim, Positional_Expr);
5469 end if;
5471 Next (Positional_Expr);
5472 end if;
5474 if Present (Get_Value (Discrim, Component_Associations (N))) then
5475 Error_Msg_NE
5476 ("more than one value supplied for discriminant&",
5477 N, Discrim);
5478 end if;
5480 Next_Discriminant (Discrim);
5481 end loop;
5483 -- Find remaining discriminant values if any among named components
5485 while Present (Discrim) loop
5486 Expr := Get_Value (Discrim, Component_Associations (N), True);
5488 if not Discriminant_Present (Discrim) then
5489 if Present (Expr) then
5490 Error_Msg_NE
5491 ("more than one value supplied for discriminant &",
5492 N, Discrim);
5493 end if;
5495 elsif No (Expr) then
5496 Error_Msg_NE
5497 ("no value supplied for discriminant &", N, Discrim);
5498 Missing_Discriminants := True;
5500 else
5501 Resolve_Aggr_Expr (Expr, Discrim);
5502 end if;
5504 Next_Discriminant (Discrim);
5505 end loop;
5507 if Missing_Discriminants then
5508 return;
5509 end if;
5511 -- At this point and until the beginning of STEP 6, New_Assoc_List
5512 -- contains only the discriminants and their values.
5514 end Step_3;
5516 -- STEP 4: Set the Etype of the record aggregate
5518 if Has_Discriminants (Typ)
5519 or else (Has_Unknown_Discriminants (Typ)
5520 and then Present (Underlying_Record_View (Typ)))
5521 then
5522 Build_Constrained_Itype (N, Typ, New_Assoc_List);
5523 else
5524 Set_Etype (N, Typ);
5525 end if;
5527 -- STEP 5: Get remaining components according to discriminant values
5529 Step_5 : declare
5530 Dnode : Node_Id;
5531 Errors_Found : Boolean := False;
5532 Record_Def : Node_Id;
5533 Parent_Typ : Entity_Id;
5534 Parent_Typ_List : Elist_Id;
5535 Parent_Elmt : Elmt_Id;
5536 Root_Typ : Entity_Id;
5538 begin
5539 if Is_Derived_Type (Typ) and then Is_Tagged_Type (Typ) then
5540 Parent_Typ_List := New_Elmt_List;
5542 -- If this is an extension aggregate, the component list must
5543 -- include all components that are not in the given ancestor type.
5544 -- Otherwise, the component list must include components of all
5545 -- ancestors, starting with the root.
5547 if Nkind (N) = N_Extension_Aggregate then
5548 Root_Typ := Base_Type (Etype (Ancestor_Part (N)));
5550 else
5551 -- AI05-0115: check legality of aggregate for type with a
5552 -- private ancestor.
5554 Root_Typ := Root_Type (Typ);
5555 if Has_Private_Ancestor (Typ) then
5556 declare
5557 Ancestor : constant Entity_Id :=
5558 Find_Private_Ancestor (Typ);
5559 Ancestor_Unit : constant Entity_Id :=
5560 Cunit_Entity
5561 (Get_Source_Unit (Ancestor));
5562 Parent_Unit : constant Entity_Id :=
5563 Cunit_Entity (Get_Source_Unit
5564 (Base_Type (Etype (Ancestor))));
5565 begin
5566 -- Check whether we are in a scope that has full view
5567 -- over the private ancestor and its parent. This can
5568 -- only happen if the derivation takes place in a child
5569 -- unit of the unit that declares the parent, and we are
5570 -- in the private part or body of that child unit, else
5571 -- the aggregate is illegal.
5573 if Is_Child_Unit (Ancestor_Unit)
5574 and then Scope (Ancestor_Unit) = Parent_Unit
5575 and then In_Open_Scopes (Scope (Ancestor))
5576 and then
5577 (In_Private_Part (Scope (Ancestor))
5578 or else In_Package_Body (Scope (Ancestor)))
5579 then
5580 null;
5582 else
5583 Error_Msg_NE
5584 ("type of aggregate has private ancestor&!",
5585 N, Root_Typ);
5586 Error_Msg_N ("must use extension aggregate!", N);
5587 return;
5588 end if;
5589 end;
5590 end if;
5592 Dnode := Declaration_Node (Base_Type (Root_Typ));
5594 -- If we don't get a full declaration, then we have some error
5595 -- which will get signalled later so skip this part. Otherwise
5596 -- gather components of root that apply to the aggregate type.
5597 -- We use the base type in case there is an applicable stored
5598 -- constraint that renames the discriminants of the root.
5600 if Nkind (Dnode) = N_Full_Type_Declaration then
5601 Record_Def := Type_Definition (Dnode);
5602 Gather_Components
5603 (Base_Type (Typ),
5604 Component_List (Record_Def),
5605 Governed_By => New_Assoc_List,
5606 Into => Components,
5607 Report_Errors => Errors_Found);
5609 if Errors_Found then
5610 Error_Msg_N
5611 ("discriminant controlling variant part is not static",
5613 return;
5614 end if;
5615 end if;
5616 end if;
5618 Parent_Typ := Base_Type (Typ);
5619 while Parent_Typ /= Root_Typ loop
5620 Prepend_Elmt (Parent_Typ, To => Parent_Typ_List);
5621 Parent_Typ := Etype (Parent_Typ);
5623 -- Check whether a private parent requires the use of
5624 -- an extension aggregate.
5626 if Nkind (Parent (Base_Type (Parent_Typ))) =
5627 N_Private_Type_Declaration
5628 or else Nkind (Parent (Base_Type (Parent_Typ))) =
5629 N_Private_Extension_Declaration
5630 then
5631 if Nkind (N) /= N_Extension_Aggregate then
5632 Error_Msg_NE
5633 ("type of aggregate has private ancestor&!",
5634 N, Parent_Typ);
5635 Error_Msg_N ("must use extension aggregate!", N);
5636 return;
5638 elsif Parent_Typ /= Root_Typ then
5639 Error_Msg_NE
5640 ("ancestor part of aggregate must be private type&",
5641 Ancestor_Part (N), Parent_Typ);
5642 return;
5643 end if;
5645 -- The current view of ancestor part may be a private type,
5646 -- while the context type is always non-private.
5648 elsif Is_Private_Type (Root_Typ)
5649 and then Present (Full_View (Root_Typ))
5650 and then Nkind (N) = N_Extension_Aggregate
5651 then
5652 exit when Base_Type (Full_View (Root_Typ)) = Parent_Typ;
5653 end if;
5654 end loop;
5656 -- Now collect components from all other ancestors, beginning
5657 -- with the current type. If the type has unknown discriminants
5658 -- use the component list of the Underlying_Record_View, which
5659 -- needs to be used for the subsequent expansion of the aggregate
5660 -- into assignments.
5662 Parent_Elmt := First_Elmt (Parent_Typ_List);
5663 while Present (Parent_Elmt) loop
5664 Parent_Typ := Node (Parent_Elmt);
5666 if Has_Unknown_Discriminants (Parent_Typ)
5667 and then Present (Underlying_Record_View (Typ))
5668 then
5669 Parent_Typ := Underlying_Record_View (Parent_Typ);
5670 end if;
5672 Record_Def := Type_Definition (Parent (Base_Type (Parent_Typ)));
5673 Gather_Components (Parent_Typ,
5674 Component_List (Record_Extension_Part (Record_Def)),
5675 Governed_By => New_Assoc_List,
5676 Into => Components,
5677 Report_Errors => Errors_Found);
5679 Next_Elmt (Parent_Elmt);
5680 end loop;
5682 -- Typ is not a derived tagged type
5684 else
5685 Record_Def := Type_Definition (Parent (Base_Type (Typ)));
5687 if Null_Present (Record_Def) then
5688 null;
5690 elsif not Has_Unknown_Discriminants (Typ) then
5691 Gather_Components
5692 (Base_Type (Typ),
5693 Component_List (Record_Def),
5694 Governed_By => New_Assoc_List,
5695 Into => Components,
5696 Report_Errors => Errors_Found);
5698 else
5699 Gather_Components
5700 (Base_Type (Underlying_Record_View (Typ)),
5701 Component_List (Record_Def),
5702 Governed_By => New_Assoc_List,
5703 Into => Components,
5704 Report_Errors => Errors_Found);
5705 end if;
5706 end if;
5708 if Errors_Found then
5709 return;
5710 end if;
5711 end Step_5;
5713 -- STEP 6: Find component Values
5715 Component_Elmt := First_Elmt (Components);
5717 -- First scan the remaining positional associations in the aggregate.
5718 -- Remember that at this point Positional_Expr contains the current
5719 -- positional association if any is left after looking for discriminant
5720 -- values in step 3.
5722 while Present (Positional_Expr) and then Present (Component_Elmt) loop
5723 Component := Node (Component_Elmt);
5724 Resolve_Aggr_Expr (Positional_Expr, Component);
5726 -- Ada 2005 (AI-231)
5728 if Ada_Version >= Ada_2005 and then Known_Null (Positional_Expr) then
5729 Check_Can_Never_Be_Null (Component, Positional_Expr);
5730 end if;
5732 if Present (Get_Value (Component, Component_Associations (N))) then
5733 Error_Msg_NE
5734 ("more than one value supplied for component &", N, Component);
5735 end if;
5737 Next (Positional_Expr);
5738 Next_Elmt (Component_Elmt);
5739 end loop;
5741 if Present (Positional_Expr) then
5742 Error_Msg_N
5743 ("too many components for record aggregate", Positional_Expr);
5744 end if;
5746 -- Now scan for the named arguments of the aggregate
5748 while Present (Component_Elmt) loop
5749 Component := Node (Component_Elmt);
5750 Expr := Get_Value (Component, Component_Associations (N), True);
5752 -- Note: The previous call to Get_Value sets the value of the
5753 -- variable Is_Box_Present.
5755 -- Ada 2005 (AI-287): Handle components with default initialization.
5756 -- Note: This feature was originally added to Ada 2005 for limited
5757 -- but it was finally allowed with any type.
5759 if Is_Box_Present then
5760 Check_Box_Component : declare
5761 Ctyp : constant Entity_Id := Etype (Component);
5763 begin
5764 -- Initially assume that the box is for a default-initialized
5765 -- component and reset to False in cases where that's not true.
5767 Is_Box_Init_By_Default := True;
5769 -- If there is a default expression for the aggregate, copy
5770 -- it into a new association. This copy must modify the scopes
5771 -- of internal types that may be attached to the expression
5772 -- (e.g. index subtypes of arrays) because in general the type
5773 -- declaration and the aggregate appear in different scopes,
5774 -- and the backend requires the scope of the type to match the
5775 -- point at which it is elaborated.
5777 -- If the component has an initialization procedure (IP) we
5778 -- pass the component to the expander, which will generate
5779 -- the call to such IP.
5781 -- If the component has discriminants, their values must
5782 -- be taken from their subtype. This is indispensable for
5783 -- constraints that are given by the current instance of an
5784 -- enclosing type, to allow the expansion of the aggregate to
5785 -- replace the reference to the current instance by the target
5786 -- object of the aggregate.
5788 if Is_Case_Choice_Pattern (N) then
5790 -- Do not transform box component values in a case-choice
5791 -- aggregate.
5793 Add_Association
5794 (Component => Component,
5795 Expr => Empty,
5796 Assoc_List => New_Assoc_List,
5797 Is_Box_Present => True);
5799 elsif Present (Parent (Component))
5800 and then Nkind (Parent (Component)) = N_Component_Declaration
5801 and then Present (Expression (Parent (Component)))
5802 then
5803 -- If component declaration has an initialization expression
5804 -- then this is not a case of default initialization.
5806 Is_Box_Init_By_Default := False;
5808 Expr :=
5809 New_Copy_Tree_And_Copy_Dimensions
5810 (Expression (Parent (Component)),
5811 New_Scope => Current_Scope,
5812 New_Sloc => Sloc (N));
5814 -- As the type of the copied default expression may refer
5815 -- to discriminants of the record type declaration, these
5816 -- non-stored discriminants need to be rewritten into stored
5817 -- discriminant values for the aggregate. This is required
5818 -- in GNATprove mode, and is adopted in all modes to avoid
5819 -- special-casing GNATprove mode.
5821 if Is_Array_Type (Etype (Expr)) then
5822 declare
5823 Rec_Typ : constant Entity_Id := Scope (Component);
5824 -- Root record type whose discriminants may be used as
5825 -- bounds in range nodes.
5827 Assoc : Node_Id;
5828 Choice : Node_Id;
5829 Index : Node_Id;
5831 begin
5832 -- Rewrite the range nodes occurring in the indexes
5833 -- and their types.
5835 Index := First_Index (Etype (Expr));
5836 while Present (Index) loop
5837 Rewrite_Range (Rec_Typ, Index);
5838 Rewrite_Range
5839 (Rec_Typ, Scalar_Range (Etype (Index)));
5841 Next_Index (Index);
5842 end loop;
5844 -- Rewrite the range nodes occurring as aggregate
5845 -- bounds and component associations.
5847 if Nkind (Expr) = N_Aggregate then
5848 if Present (Aggregate_Bounds (Expr)) then
5849 Rewrite_Range (Rec_Typ, Aggregate_Bounds (Expr));
5850 end if;
5852 if Present (Component_Associations (Expr)) then
5853 Assoc := First (Component_Associations (Expr));
5854 while Present (Assoc) loop
5855 Choice := First (Choices (Assoc));
5856 while Present (Choice) loop
5857 Rewrite_Range (Rec_Typ, Choice);
5859 Next (Choice);
5860 end loop;
5862 Next (Assoc);
5863 end loop;
5864 end if;
5865 end if;
5866 end;
5867 end if;
5869 Add_Association
5870 (Component => Component,
5871 Expr => Expr,
5872 Assoc_List => New_Assoc_List);
5873 Set_Has_Self_Reference (N);
5875 elsif Needs_Simple_Initialization (Ctyp)
5876 or else Has_Non_Null_Base_Init_Proc (Ctyp)
5877 or else not Expander_Active
5878 then
5879 Add_Association
5880 (Component => Component,
5881 Expr => Empty,
5882 Assoc_List => New_Assoc_List,
5883 Is_Box_Present => True);
5885 -- Otherwise we only need to resolve the expression if the
5886 -- component has partially initialized values (required to
5887 -- expand the corresponding assignments and run-time checks).
5889 elsif Present (Expr)
5890 and then Is_Partially_Initialized_Type (Ctyp)
5891 then
5892 Resolve_Aggr_Expr (Expr, Component);
5893 end if;
5894 end Check_Box_Component;
5896 elsif No (Expr) then
5898 -- Ignore hidden components associated with the position of the
5899 -- interface tags: these are initialized dynamically.
5901 if No (Related_Type (Component)) then
5902 Error_Msg_NE
5903 ("no value supplied for component &!", N, Component);
5904 end if;
5906 else
5907 Resolve_Aggr_Expr (Expr, Component);
5908 end if;
5910 Next_Elmt (Component_Elmt);
5911 end loop;
5913 -- STEP 7: check for invalid components + check type in choice list
5915 Step_7 : declare
5916 Assoc : Node_Id;
5917 New_Assoc : Node_Id;
5919 Selectr : Node_Id;
5920 -- Selector name
5922 Typech : Entity_Id;
5923 -- Type of first component in choice list
5925 begin
5926 if Present (Component_Associations (N)) then
5927 Assoc := First (Component_Associations (N));
5928 else
5929 Assoc := Empty;
5930 end if;
5932 Verification : while Present (Assoc) loop
5933 Selectr := First (Choices (Assoc));
5934 Typech := Empty;
5936 if Nkind (Selectr) = N_Others_Choice then
5938 -- Ada 2005 (AI-287): others choice may have expression or box
5940 if No (Others_Etype) and then Others_Box = 0 then
5941 Error_Msg_N
5942 ("OTHERS must represent at least one component", Selectr);
5944 elsif Others_Box = 1 and then Warn_On_Redundant_Constructs then
5945 Error_Msg_N ("OTHERS choice is redundant?r?", Box_Node);
5946 Error_Msg_N
5947 ("\previous choices cover all components?r?", Box_Node);
5948 end if;
5950 exit Verification;
5951 end if;
5953 while Present (Selectr) loop
5954 Component := Empty;
5955 New_Assoc := First (New_Assoc_List);
5956 while Present (New_Assoc) loop
5957 Component := First (Choices (New_Assoc));
5959 if Chars (Selectr) = Chars (Component) then
5960 if Style_Check then
5961 Check_Identifier (Selectr, Entity (Component));
5962 end if;
5964 exit;
5965 end if;
5967 Next (New_Assoc);
5968 end loop;
5970 -- If we found an association, then this is a legal component
5971 -- of the type in question.
5973 pragma Assert (if Present (New_Assoc) then Present (Component));
5975 -- If no association, this is not a legal component of the type
5976 -- in question, unless its association is provided with a box.
5978 if No (New_Assoc) then
5979 if Box_Present (Parent (Selectr)) then
5981 -- This may still be a bogus component with a box. Scan
5982 -- list of components to verify that a component with
5983 -- that name exists.
5985 declare
5986 C : Entity_Id;
5988 begin
5989 C := First_Component (Typ);
5990 while Present (C) loop
5991 if Chars (C) = Chars (Selectr) then
5993 -- If the context is an extension aggregate,
5994 -- the component must not be inherited from
5995 -- the ancestor part of the aggregate.
5997 if Nkind (N) /= N_Extension_Aggregate
5998 or else
5999 Scope (Original_Record_Component (C)) /=
6000 Etype (Ancestor_Part (N))
6001 then
6002 exit;
6003 end if;
6004 end if;
6006 Next_Component (C);
6007 end loop;
6009 if No (C) then
6010 Error_Msg_Node_2 := Typ;
6011 Error_Msg_N ("& is not a component of}", Selectr);
6012 end if;
6013 end;
6015 elsif Chars (Selectr) /= Name_uTag
6016 and then Chars (Selectr) /= Name_uParent
6017 then
6018 if not Has_Discriminants (Typ) then
6019 Error_Msg_Node_2 := Typ;
6020 Error_Msg_N ("& is not a component of}", Selectr);
6021 else
6022 Error_Msg_N
6023 ("& is not a component of the aggregate subtype",
6024 Selectr);
6025 end if;
6027 Check_Misspelled_Component (Components, Selectr);
6028 end if;
6030 elsif No (Typech) then
6031 Typech := Base_Type (Etype (Component));
6033 -- AI05-0199: In Ada 2012, several components of anonymous
6034 -- access types can appear in a choice list, as long as the
6035 -- designated types match.
6037 elsif Typech /= Base_Type (Etype (Component)) then
6038 if Ada_Version >= Ada_2012
6039 and then Ekind (Typech) = E_Anonymous_Access_Type
6040 and then
6041 Ekind (Etype (Component)) = E_Anonymous_Access_Type
6042 and then Base_Type (Designated_Type (Typech)) =
6043 Base_Type (Designated_Type (Etype (Component)))
6044 and then
6045 Subtypes_Statically_Match (Typech, (Etype (Component)))
6046 then
6047 null;
6049 elsif not Box_Present (Parent (Selectr)) then
6050 Error_Msg_N
6051 ("components in choice list must have same type",
6052 Selectr);
6053 end if;
6054 end if;
6056 Next (Selectr);
6057 end loop;
6059 Next (Assoc);
6060 end loop Verification;
6061 end Step_7;
6063 -- STEP 8: replace the original aggregate
6065 Step_8 : declare
6066 New_Aggregate : constant Node_Id := New_Copy (N);
6068 begin
6069 Set_Expressions (New_Aggregate, No_List);
6070 Set_Etype (New_Aggregate, Etype (N));
6071 Set_Component_Associations (New_Aggregate, New_Assoc_List);
6072 Set_Check_Actuals (New_Aggregate, Check_Actuals (N));
6074 Rewrite (N, New_Aggregate);
6075 end Step_8;
6077 -- Check the dimensions of the components in the record aggregate
6079 Analyze_Dimension_Extension_Or_Record_Aggregate (N);
6080 end Resolve_Record_Aggregate;
6082 -----------------------------
6083 -- Check_Can_Never_Be_Null --
6084 -----------------------------
6086 procedure Check_Can_Never_Be_Null (Typ : Entity_Id; Expr : Node_Id) is
6087 Comp_Typ : Entity_Id;
6089 begin
6090 pragma Assert
6091 (Ada_Version >= Ada_2005
6092 and then Present (Expr)
6093 and then Known_Null (Expr));
6095 case Ekind (Typ) is
6096 when E_Array_Type =>
6097 Comp_Typ := Component_Type (Typ);
6099 when E_Component
6100 | E_Discriminant
6102 Comp_Typ := Etype (Typ);
6104 when others =>
6105 return;
6106 end case;
6108 if Can_Never_Be_Null (Comp_Typ) then
6110 -- Here we know we have a constraint error. Note that we do not use
6111 -- Apply_Compile_Time_Constraint_Error here to the Expr, which might
6112 -- seem the more natural approach. That's because in some cases the
6113 -- components are rewritten, and the replacement would be missed.
6114 -- We do not mark the whole aggregate as raising a constraint error,
6115 -- because the association may be a null array range.
6117 Error_Msg_N
6118 ("(Ada 2005) NULL not allowed in null-excluding component??", Expr);
6119 Error_Msg_N
6120 ("\Constraint_Error will be raised at run time??", Expr);
6122 Rewrite (Expr,
6123 Make_Raise_Constraint_Error
6124 (Sloc (Expr), Reason => CE_Access_Check_Failed));
6125 Set_Etype (Expr, Comp_Typ);
6126 Set_Analyzed (Expr);
6127 end if;
6128 end Check_Can_Never_Be_Null;
6130 ---------------------
6131 -- Sort_Case_Table --
6132 ---------------------
6134 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is
6135 U : constant Int := Case_Table'Last;
6136 K : Int;
6137 J : Int;
6138 T : Case_Bounds;
6140 begin
6141 K := 1;
6142 while K < U loop
6143 T := Case_Table (K + 1);
6145 J := K + 1;
6146 while J > 1
6147 and then Expr_Value (Case_Table (J - 1).Lo) > Expr_Value (T.Lo)
6148 loop
6149 Case_Table (J) := Case_Table (J - 1);
6150 J := J - 1;
6151 end loop;
6153 Case_Table (J) := T;
6154 K := K + 1;
6155 end loop;
6156 end Sort_Case_Table;
6158 end Sem_Aggr;