MATCH: Improve `A CMP 0 ? A : -A` set of patterns to use bitwise_equal_p.
[official-gcc.git] / gcc / ada / sem_aggr.adb
blob597c3ce2dd11e538a852909ed58d25a250581619
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ A G G R --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2023, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Einfo; use Einfo;
30 with Einfo.Entities; use Einfo.Entities;
31 with Einfo.Utils; use Einfo.Utils;
32 with Elists; use Elists;
33 with Errout; use Errout;
34 with Expander; use Expander;
35 with Exp_Ch6; use Exp_Ch6;
36 with Exp_Tss; use Exp_Tss;
37 with Exp_Util; use Exp_Util;
38 with Freeze; use Freeze;
39 with Itypes; use Itypes;
40 with Lib; use Lib;
41 with Lib.Xref; use Lib.Xref;
42 with Namet; use Namet;
43 with Namet.Sp; use Namet.Sp;
44 with Nmake; use Nmake;
45 with Nlists; use Nlists;
46 with Opt; use Opt;
47 with Restrict; use Restrict;
48 with Rident; use Rident;
49 with Sem; use Sem;
50 with Sem_Aux; use Sem_Aux;
51 with Sem_Case; use Sem_Case;
52 with Sem_Cat; use Sem_Cat;
53 with Sem_Ch3; use Sem_Ch3;
54 with Sem_Ch8; use Sem_Ch8;
55 with Sem_Ch13; use Sem_Ch13;
56 with Sem_Dim; use Sem_Dim;
57 with Sem_Eval; use Sem_Eval;
58 with Sem_Res; use Sem_Res;
59 with Sem_Util; use Sem_Util;
60 with Sem_Type; use Sem_Type;
61 with Sem_Warn; use Sem_Warn;
62 with Sinfo; use Sinfo;
63 with Sinfo.Nodes; use Sinfo.Nodes;
64 with Sinfo.Utils; use Sinfo.Utils;
65 with Snames; use Snames;
66 with Stringt; use Stringt;
67 with Stand; use Stand;
68 with Style; use Style;
69 with Targparm; use Targparm;
70 with Tbuild; use Tbuild;
71 with Ttypes; use Ttypes;
72 with Uintp; use Uintp;
73 with Warnsw; use Warnsw;
75 package body Sem_Aggr is
77 type Case_Bounds is record
78 Lo : Node_Id;
79 -- Low bound of choice. Once we sort the Case_Table, then entries
80 -- will be in order of ascending Choice_Lo values.
82 Hi : Node_Id;
83 -- High Bound of choice. The sort does not pay any attention to the
84 -- high bound, so choices 1 .. 4 and 1 .. 5 could be in either order.
86 Highest : Uint;
87 -- If there are duplicates or missing entries, then in the sorted
88 -- table, this records the highest value among Choice_Hi values
89 -- seen so far, including this entry.
91 Choice : Node_Id;
92 -- The node of the choice
93 end record;
95 type Case_Table_Type is array (Pos range <>) of Case_Bounds;
96 -- Table type used by Check_Case_Choices procedure
98 -----------------------
99 -- Local Subprograms --
100 -----------------------
102 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type);
103 -- Sort the Case Table using the Lower Bound of each Choice as the key. A
104 -- simple insertion sort is used since the choices in a case statement will
105 -- usually be in near sorted order.
107 procedure Check_Can_Never_Be_Null (Typ : Entity_Id; Expr : Node_Id);
108 -- Ada 2005 (AI-231): Check bad usage of null for a component for which
109 -- null exclusion (NOT NULL) is specified. Typ can be an E_Array_Type for
110 -- the array case (the component type of the array will be used) or an
111 -- E_Component/E_Discriminant entity in the record case, in which case the
112 -- type of the component will be used for the test. If Typ is any other
113 -- kind of entity, the call is ignored. Expr is the component node in the
114 -- aggregate which is known to have a null value. A warning message will be
115 -- issued if the component is null excluding.
117 -- It would be better to pass the proper type for Typ ???
119 procedure Check_Expr_OK_In_Limited_Aggregate (Expr : Node_Id);
120 -- Check that Expr is either not limited or else is one of the cases of
121 -- expressions allowed for a limited component association (namely, an
122 -- aggregate, function call, or <> notation). Report error for violations.
123 -- Expression is also OK in an instance or inlining context, because we
124 -- have already preanalyzed and it is known to be type correct.
126 ------------------------------------------------------
127 -- Subprograms used for RECORD AGGREGATE Processing --
128 ------------------------------------------------------
130 procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id);
131 -- This procedure performs all the semantic checks required for record
132 -- aggregates. Note that for aggregates analysis and resolution go
133 -- hand in hand. Aggregate analysis has been delayed up to here and
134 -- it is done while resolving the aggregate.
136 -- N is the N_Aggregate node.
137 -- Typ is the record type for the aggregate resolution
139 -- While performing the semantic checks, this procedure builds a new
140 -- Component_Association_List where each record field appears alone in a
141 -- Component_Choice_List along with its corresponding expression. The
142 -- record fields in the Component_Association_List appear in the same order
143 -- in which they appear in the record type Typ.
145 -- Once this new Component_Association_List is built and all the semantic
146 -- checks performed, the original aggregate subtree is replaced with the
147 -- new named record aggregate just built. This new record aggregate has no
148 -- positional associations, so its Expressions field is set to No_List.
149 -- Note that subtree substitution is performed with Rewrite so as to be
150 -- able to retrieve the original aggregate.
152 -- The aggregate subtree manipulation performed by Resolve_Record_Aggregate
153 -- yields the aggregate format expected by Gigi. Typically, this kind of
154 -- tree manipulations are done in the expander. However, because the
155 -- semantic checks that need to be performed on record aggregates really go
156 -- hand in hand with the record aggregate normalization, the aggregate
157 -- subtree transformation is performed during resolution rather than
158 -- expansion. Had we decided otherwise we would have had to duplicate most
159 -- of the code in the expansion procedure Expand_Record_Aggregate. Note,
160 -- however, that all the expansion concerning aggregates for tagged records
161 -- is done in Expand_Record_Aggregate.
163 -- The algorithm of Resolve_Record_Aggregate proceeds as follows:
165 -- 1. Make sure that the record type against which the record aggregate
166 -- has to be resolved is not abstract. Furthermore if the type is a
167 -- null aggregate make sure the input aggregate N is also null.
169 -- 2. Verify that the structure of the aggregate is that of a record
170 -- aggregate. Specifically, look for component associations and ensure
171 -- that each choice list only has identifiers or the N_Others_Choice
172 -- node. Also make sure that if present, the N_Others_Choice occurs
173 -- last and by itself.
175 -- 3. If Typ contains discriminants, the values for each discriminant is
176 -- looked for. If the record type Typ has variants, we check that the
177 -- expressions corresponding to each discriminant ruling the (possibly
178 -- nested) variant parts of Typ, are static. This allows us to determine
179 -- the variant parts to which the rest of the aggregate must conform.
180 -- The names of discriminants with their values are saved in a new
181 -- association list, New_Assoc_List which is later augmented with the
182 -- names and values of the remaining components in the record type.
184 -- During this phase we also make sure that every discriminant is
185 -- assigned exactly one value. Note that when several values for a given
186 -- discriminant are found, semantic processing continues looking for
187 -- further errors. In this case it's the first discriminant value found
188 -- which we will be recorded.
190 -- IMPORTANT NOTE: For derived tagged types this procedure expects
191 -- First_Discriminant and Next_Discriminant to give the correct list
192 -- of discriminants, in the correct order.
194 -- 4. After all the discriminant values have been gathered, we can set the
195 -- Etype of the record aggregate. If Typ contains no discriminants this
196 -- is straightforward: the Etype of N is just Typ, otherwise a new
197 -- implicit constrained subtype of Typ is built to be the Etype of N.
199 -- 5. Gather the remaining record components according to the discriminant
200 -- values. This involves recursively traversing the record type
201 -- structure to see what variants are selected by the given discriminant
202 -- values. This processing is a little more convoluted if Typ is a
203 -- derived tagged types since we need to retrieve the record structure
204 -- of all the ancestors of Typ.
206 -- 6. After gathering the record components we look for their values in the
207 -- record aggregate and emit appropriate error messages should we not
208 -- find such values or should they be duplicated.
210 -- 7. We then make sure no illegal component names appear in the record
211 -- aggregate and make sure that the type of the record components
212 -- appearing in a same choice list is the same. Finally we ensure that
213 -- the others choice, if present, is used to provide the value of at
214 -- least a record component.
216 -- 8. The original aggregate node is replaced with the new named aggregate
217 -- built in steps 3 through 6, as explained earlier.
219 -- Given the complexity of record aggregate resolution, the primary goal of
220 -- this routine is clarity and simplicity rather than execution and storage
221 -- efficiency. If there are only positional components in the aggregate the
222 -- running time is linear. If there are associations the running time is
223 -- still linear as long as the order of the associations is not too far off
224 -- the order of the components in the record type. If this is not the case
225 -- the running time is at worst quadratic in the size of the association
226 -- list.
228 procedure Check_Misspelled_Component
229 (Elements : Elist_Id;
230 Component : Node_Id);
231 -- Give possible misspelling diagnostic if Component is likely to be a
232 -- misspelling of one of the components of the Assoc_List. This is called
233 -- by Resolve_Aggr_Expr after producing an invalid component error message.
235 -----------------------------------------------------
236 -- Subprograms used for ARRAY AGGREGATE Processing --
237 -----------------------------------------------------
239 function Resolve_Array_Aggregate
240 (N : Node_Id;
241 Index : Node_Id;
242 Index_Constr : Node_Id;
243 Component_Typ : Entity_Id;
244 Others_Allowed : Boolean) return Boolean;
245 -- This procedure performs the semantic checks for an array aggregate.
246 -- True is returned if the aggregate resolution succeeds.
248 -- The procedure works by recursively checking each nested aggregate.
249 -- Specifically, after checking a sub-aggregate nested at the i-th level
250 -- we recursively check all the subaggregates at the i+1-st level (if any).
251 -- Note that aggregates analysis and resolution go hand in hand.
252 -- Aggregate analysis has been delayed up to here and it is done while
253 -- resolving the aggregate.
255 -- N is the current N_Aggregate node to be checked.
257 -- Index is the index node corresponding to the array sub-aggregate that
258 -- we are currently checking (RM 4.3.3 (8)). Its Etype is the
259 -- corresponding index type (or subtype).
261 -- Index_Constr is the node giving the applicable index constraint if
262 -- any (RM 4.3.3 (10)). It "is a constraint provided by certain
263 -- contexts [...] that can be used to determine the bounds of the array
264 -- value specified by the aggregate". If Others_Allowed below is False
265 -- there is no applicable index constraint and this node is set to Index.
267 -- Component_Typ is the array component type.
269 -- Others_Allowed indicates whether an others choice is allowed
270 -- in the context where the top-level aggregate appeared.
272 -- The algorithm of Resolve_Array_Aggregate proceeds as follows:
274 -- 1. Make sure that the others choice, if present, is by itself and
275 -- appears last in the sub-aggregate. Check that we do not have
276 -- positional and named components in the array sub-aggregate (unless
277 -- the named association is an others choice). Finally if an others
278 -- choice is present, make sure it is allowed in the aggregate context.
280 -- 2. If the array sub-aggregate contains discrete_choices:
282 -- (A) Verify their validity. Specifically verify that:
284 -- (a) If a null range is present it must be the only possible
285 -- choice in the array aggregate.
287 -- (b) Ditto for a non static range.
289 -- (c) Ditto for a non static expression.
291 -- In addition this step analyzes and resolves each discrete_choice,
292 -- making sure that its type is the type of the corresponding Index.
293 -- If we are not at the lowest array aggregate level (in the case of
294 -- multi-dimensional aggregates) then invoke Resolve_Array_Aggregate
295 -- recursively on each component expression. Otherwise, resolve the
296 -- bottom level component expressions against the expected component
297 -- type ONLY IF the component corresponds to a single discrete choice
298 -- which is not an others choice (to see why read the DELAYED
299 -- COMPONENT RESOLUTION below).
301 -- (B) Determine the bounds of the sub-aggregate and lowest and
302 -- highest choice values.
304 -- 3. For positional aggregates:
306 -- (A) Loop over the component expressions either recursively invoking
307 -- Resolve_Array_Aggregate on each of these for multi-dimensional
308 -- array aggregates or resolving the bottom level component
309 -- expressions against the expected component type.
311 -- (B) Determine the bounds of the positional sub-aggregates.
313 -- 4. Try to determine statically whether the evaluation of the array
314 -- sub-aggregate raises Constraint_Error. If yes emit proper
315 -- warnings. The precise checks are the following:
317 -- (A) Check that the index range defined by aggregate bounds is
318 -- compatible with corresponding index subtype.
319 -- We also check against the base type. In fact it could be that
320 -- Low/High bounds of the base type are static whereas those of
321 -- the index subtype are not. Thus if we can statically catch
322 -- a problem with respect to the base type we are guaranteed
323 -- that the same problem will arise with the index subtype
325 -- (B) If we are dealing with a named aggregate containing an others
326 -- choice and at least one discrete choice then make sure the range
327 -- specified by the discrete choices does not overflow the
328 -- aggregate bounds. We also check against the index type and base
329 -- type bounds for the same reasons given in (A).
331 -- (C) If we are dealing with a positional aggregate with an others
332 -- choice make sure the number of positional elements specified
333 -- does not overflow the aggregate bounds. We also check against
334 -- the index type and base type bounds as mentioned in (A).
336 -- Finally construct an N_Range node giving the sub-aggregate bounds.
337 -- Set the Aggregate_Bounds field of the sub-aggregate to be this
338 -- N_Range. The routine Array_Aggr_Subtype below uses such N_Ranges
339 -- to build the appropriate aggregate subtype. Aggregate_Bounds
340 -- information is needed during expansion.
342 -- DELAYED COMPONENT RESOLUTION: The resolution of bottom level component
343 -- expressions in an array aggregate may call Duplicate_Subexpr or some
344 -- other routine that inserts code just outside the outermost aggregate.
345 -- If the array aggregate contains discrete choices or an others choice,
346 -- this may be wrong. Consider for instance the following example.
348 -- type Rec is record
349 -- V : Integer := 0;
350 -- end record;
352 -- type Acc_Rec is access Rec;
353 -- Arr : array (1..3) of Acc_Rec := (1 .. 3 => new Rec);
355 -- Then the transformation of "new Rec" that occurs during resolution
356 -- entails the following code modifications
358 -- P7b : constant Acc_Rec := new Rec;
359 -- RecIP (P7b.all);
360 -- Arr : array (1..3) of Acc_Rec := (1 .. 3 => P7b);
362 -- This code transformation is clearly wrong, since we need to call
363 -- "new Rec" for each of the 3 array elements. To avoid this problem we
364 -- delay resolution of the components of non positional array aggregates
365 -- to the expansion phase. As an optimization, if the discrete choice
366 -- specifies a single value we do not delay resolution.
368 function Array_Aggr_Subtype (N : Node_Id; Typ : Entity_Id) return Entity_Id;
369 -- This routine returns the type or subtype of an array aggregate.
371 -- N is the array aggregate node whose type we return.
373 -- Typ is the context type in which N occurs.
375 -- This routine creates an implicit array subtype whose bounds are
376 -- those defined by the aggregate. When this routine is invoked
377 -- Resolve_Array_Aggregate has already processed aggregate N. Thus the
378 -- Aggregate_Bounds of each sub-aggregate, is an N_Range node giving the
379 -- sub-aggregate bounds. When building the aggregate itype, this function
380 -- traverses the array aggregate N collecting such Aggregate_Bounds and
381 -- constructs the proper array aggregate itype.
383 -- Note that in the case of multidimensional aggregates each inner
384 -- sub-aggregate corresponding to a given array dimension, may provide a
385 -- different bounds. If it is possible to determine statically that
386 -- some sub-aggregates corresponding to the same index do not have the
387 -- same bounds, then a warning is emitted. If such check is not possible
388 -- statically (because some sub-aggregate bounds are dynamic expressions)
389 -- then this job is left to the expander. In all cases the particular
390 -- bounds that this function will chose for a given dimension is the first
391 -- N_Range node for a sub-aggregate corresponding to that dimension.
393 -- Note that the Raises_Constraint_Error flag of an array aggregate
394 -- whose evaluation is determined to raise CE by Resolve_Array_Aggregate,
395 -- is set in Resolve_Array_Aggregate but the aggregate is not
396 -- immediately replaced with a raise CE. In fact, Array_Aggr_Subtype must
397 -- first construct the proper itype for the aggregate (Gigi needs
398 -- this). After constructing the proper itype we will eventually replace
399 -- the top-level aggregate with a raise CE (done in Resolve_Aggregate).
400 -- Of course in cases such as:
402 -- type Arr is array (integer range <>) of Integer;
403 -- A : Arr := (positive range -1 .. 2 => 0);
405 -- The bounds of the aggregate itype are cooked up to look reasonable
406 -- (in this particular case the bounds will be 1 .. 2).
408 procedure Make_String_Into_Aggregate (N : Node_Id);
409 -- A string literal can appear in a context in which a one dimensional
410 -- array of characters is expected. This procedure simply rewrites the
411 -- string as an aggregate, prior to resolution.
413 function Resolve_Null_Array_Aggregate (N : Node_Id) return Boolean;
414 -- For the Ada 2022 construct, build a subtype with a null range for each
415 -- dimension, using the bounds from the context subtype (if the subtype
416 -- is constrained). If the subtype is unconstrained, then the bounds
417 -- are determined in much the same way as the bounds for a null string
418 -- literal with no applicable index constraint.
420 ---------------------------------
421 -- Delta aggregate processing --
422 ---------------------------------
424 procedure Resolve_Delta_Array_Aggregate (N : Node_Id; Typ : Entity_Id);
425 procedure Resolve_Delta_Record_Aggregate (N : Node_Id; Typ : Entity_Id);
427 ------------------------
428 -- Array_Aggr_Subtype --
429 ------------------------
431 function Array_Aggr_Subtype
432 (N : Node_Id;
433 Typ : Entity_Id) return Entity_Id
435 Aggr_Dimension : constant Pos := Number_Dimensions (Typ);
436 -- Number of aggregate index dimensions
438 Aggr_Range : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
439 -- Constrained N_Range of each index dimension in our aggregate itype
441 Aggr_Low : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
442 Aggr_High : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
443 -- Low and High bounds for each index dimension in our aggregate itype
445 Is_Fully_Positional : Boolean := True;
447 procedure Collect_Aggr_Bounds (N : Node_Id; Dim : Pos);
448 -- N is an array (sub-)aggregate. Dim is the dimension corresponding
449 -- to (sub-)aggregate N. This procedure collects and removes the side
450 -- effects of the constrained N_Range nodes corresponding to each index
451 -- dimension of our aggregate itype. These N_Range nodes are collected
452 -- in Aggr_Range above.
454 -- Likewise collect in Aggr_Low & Aggr_High above the low and high
455 -- bounds of each index dimension. If, when collecting, two bounds
456 -- corresponding to the same dimension are static and found to differ,
457 -- then emit a warning, and mark N as raising Constraint_Error.
459 -------------------------
460 -- Collect_Aggr_Bounds --
461 -------------------------
463 procedure Collect_Aggr_Bounds (N : Node_Id; Dim : Pos) is
464 This_Range : constant Node_Id := Aggregate_Bounds (N);
465 -- The aggregate range node of this specific sub-aggregate
467 This_Low : constant Node_Id := Low_Bound (This_Range);
468 This_High : constant Node_Id := High_Bound (This_Range);
469 -- The aggregate bounds of this specific sub-aggregate
471 Assoc : Node_Id;
472 Expr : Node_Id;
474 begin
475 Remove_Side_Effects (This_Low, Variable_Ref => True);
476 Remove_Side_Effects (This_High, Variable_Ref => True);
478 -- Collect the first N_Range for a given dimension that you find.
479 -- For a given dimension they must be all equal anyway.
481 if No (Aggr_Range (Dim)) then
482 Aggr_Low (Dim) := This_Low;
483 Aggr_High (Dim) := This_High;
484 Aggr_Range (Dim) := This_Range;
486 else
487 if Compile_Time_Known_Value (This_Low) then
488 if not Compile_Time_Known_Value (Aggr_Low (Dim)) then
489 Aggr_Low (Dim) := This_Low;
491 elsif Expr_Value (This_Low) /= Expr_Value (Aggr_Low (Dim)) then
492 Set_Raises_Constraint_Error (N);
493 Error_Msg_Warn := SPARK_Mode /= On;
494 Error_Msg_N ("sub-aggregate low bound mismatch<<", N);
495 Error_Msg_N ("\Constraint_Error [<<", N);
496 end if;
497 end if;
499 if Compile_Time_Known_Value (This_High) then
500 if not Compile_Time_Known_Value (Aggr_High (Dim)) then
501 Aggr_High (Dim) := This_High;
503 elsif
504 Expr_Value (This_High) /= Expr_Value (Aggr_High (Dim))
505 then
506 Set_Raises_Constraint_Error (N);
507 Error_Msg_Warn := SPARK_Mode /= On;
508 Error_Msg_N ("sub-aggregate high bound mismatch<<", N);
509 Error_Msg_N ("\Constraint_Error [<<", N);
510 end if;
511 end if;
512 end if;
514 if Dim < Aggr_Dimension then
516 -- Process positional components
518 if Present (Expressions (N)) then
519 Expr := First (Expressions (N));
520 while Present (Expr) loop
521 Collect_Aggr_Bounds (Expr, Dim + 1);
522 Next (Expr);
523 end loop;
524 end if;
526 -- Process component associations
528 if Present (Component_Associations (N)) then
529 Is_Fully_Positional := False;
531 Assoc := First (Component_Associations (N));
532 while Present (Assoc) loop
533 Expr := Expression (Assoc);
534 Collect_Aggr_Bounds (Expr, Dim + 1);
535 Next (Assoc);
536 end loop;
537 end if;
538 end if;
539 end Collect_Aggr_Bounds;
541 -- Array_Aggr_Subtype variables
543 Itype : Entity_Id;
544 -- The final itype of the overall aggregate
546 Index_Constraints : constant List_Id := New_List;
547 -- The list of index constraints of the aggregate itype
549 -- Start of processing for Array_Aggr_Subtype
551 begin
552 -- Make sure that the list of index constraints is properly attached to
553 -- the tree, and then collect the aggregate bounds.
555 -- If no aggregaate bounds have been set, this is an aggregate with
556 -- iterator specifications and a dynamic size to be determined by
557 -- first pass of expanded code.
559 if No (Aggregate_Bounds (N)) then
560 return Typ;
561 end if;
563 Set_Parent (Index_Constraints, N);
565 -- When resolving a null aggregate we created a list of aggregate bounds
566 -- for the consecutive dimensions. The bounds for the first dimension
567 -- are attached as the Aggregate_Bounds of the aggregate node.
569 if Is_Null_Aggregate (N) then
570 declare
571 This_Range : Node_Id := Aggregate_Bounds (N);
572 begin
573 for J in 1 .. Aggr_Dimension loop
574 Aggr_Range (J) := This_Range;
575 Next_Index (This_Range);
577 -- Remove bounds from the list, so they can be reattached as
578 -- the First_Index/Next_Index again by the code that also
579 -- handles non-null aggregates.
581 Remove (Aggr_Range (J));
582 end loop;
583 end;
584 else
585 Collect_Aggr_Bounds (N, 1);
586 end if;
588 -- Build the list of constrained indexes of our aggregate itype
590 for J in 1 .. Aggr_Dimension loop
591 Create_Index : declare
592 Index_Base : constant Entity_Id :=
593 Base_Type (Etype (Aggr_Range (J)));
594 Index_Typ : Entity_Id;
596 begin
597 -- Construct the Index subtype, and associate it with the range
598 -- construct that generates it.
600 Index_Typ :=
601 Create_Itype (Subtype_Kind (Ekind (Index_Base)), Aggr_Range (J));
603 Set_Etype (Index_Typ, Index_Base);
605 if Is_Character_Type (Index_Base) then
606 Set_Is_Character_Type (Index_Typ);
607 end if;
609 Set_Size_Info (Index_Typ, (Index_Base));
610 Set_RM_Size (Index_Typ, RM_Size (Index_Base));
611 Set_First_Rep_Item (Index_Typ, First_Rep_Item (Index_Base));
612 Set_Scalar_Range (Index_Typ, Aggr_Range (J));
614 if Is_Discrete_Or_Fixed_Point_Type (Index_Typ) then
615 Set_RM_Size (Index_Typ, UI_From_Int (Minimum_Size (Index_Typ)));
616 end if;
618 Set_Etype (Aggr_Range (J), Index_Typ);
620 Append (Aggr_Range (J), To => Index_Constraints);
621 end Create_Index;
622 end loop;
624 -- Now build the Itype
626 Itype := Create_Itype (E_Array_Subtype, N);
628 Set_First_Rep_Item (Itype, First_Rep_Item (Typ));
629 Set_Convention (Itype, Convention (Typ));
630 Set_Depends_On_Private (Itype, Has_Private_Component (Typ));
631 Set_Etype (Itype, Base_Type (Typ));
632 Set_Has_Alignment_Clause (Itype, Has_Alignment_Clause (Typ));
633 Set_Is_Aliased (Itype, Is_Aliased (Typ));
634 Set_Is_Independent (Itype, Is_Independent (Typ));
635 Set_Depends_On_Private (Itype, Depends_On_Private (Typ));
637 Copy_Suppress_Status (Index_Check, Typ, Itype);
638 Copy_Suppress_Status (Length_Check, Typ, Itype);
640 Set_First_Index (Itype, First (Index_Constraints));
641 Set_Is_Constrained (Itype, True);
642 Set_Is_Internal (Itype, True);
644 if Has_Predicates (Typ) then
645 Set_Has_Predicates (Itype);
647 -- If the base type has a predicate, capture the predicated parent
648 -- or the existing predicate function for SPARK use.
650 if Present (Predicate_Function (Typ)) then
651 Set_Predicate_Function (Itype, Predicate_Function (Typ));
653 elsif Is_Itype (Typ) then
654 Set_Predicated_Parent (Itype, Predicated_Parent (Typ));
656 else
657 Set_Predicated_Parent (Itype, Typ);
658 end if;
659 end if;
661 -- A simple optimization: purely positional aggregates of static
662 -- components should be passed to gigi unexpanded whenever possible, and
663 -- regardless of the staticness of the bounds themselves. Subsequent
664 -- checks in exp_aggr verify that type is not packed, etc.
666 Set_Size_Known_At_Compile_Time
667 (Itype,
668 Is_Fully_Positional
669 and then Comes_From_Source (N)
670 and then Size_Known_At_Compile_Time (Component_Type (Typ)));
672 -- We always need a freeze node for a packed array subtype, so that we
673 -- can build the Packed_Array_Impl_Type corresponding to the subtype. If
674 -- expansion is disabled, the packed array subtype is not built, and we
675 -- must not generate a freeze node for the type, or else it will appear
676 -- incomplete to gigi.
678 if Is_Packed (Itype)
679 and then not In_Spec_Expression
680 and then Expander_Active
681 then
682 Freeze_Itype (Itype, N);
683 end if;
685 return Itype;
686 end Array_Aggr_Subtype;
688 --------------------------------
689 -- Check_Misspelled_Component --
690 --------------------------------
692 procedure Check_Misspelled_Component
693 (Elements : Elist_Id;
694 Component : Node_Id)
696 Max_Suggestions : constant := 2;
698 Nr_Of_Suggestions : Natural := 0;
699 Suggestion_1 : Entity_Id := Empty;
700 Suggestion_2 : Entity_Id := Empty;
701 Component_Elmt : Elmt_Id;
703 begin
704 -- All the components of List are matched against Component and a count
705 -- is maintained of possible misspellings. When at the end of the
706 -- analysis there are one or two (not more) possible misspellings,
707 -- these misspellings will be suggested as possible corrections.
709 Component_Elmt := First_Elmt (Elements);
710 while Nr_Of_Suggestions <= Max_Suggestions
711 and then Present (Component_Elmt)
712 loop
713 if Is_Bad_Spelling_Of
714 (Chars (Node (Component_Elmt)),
715 Chars (Component))
716 then
717 Nr_Of_Suggestions := Nr_Of_Suggestions + 1;
719 case Nr_Of_Suggestions is
720 when 1 => Suggestion_1 := Node (Component_Elmt);
721 when 2 => Suggestion_2 := Node (Component_Elmt);
722 when others => null;
723 end case;
724 end if;
726 Next_Elmt (Component_Elmt);
727 end loop;
729 -- Report at most two suggestions
731 if Nr_Of_Suggestions = 1 then
732 Error_Msg_NE -- CODEFIX
733 ("\possible misspelling of&", Component, Suggestion_1);
735 elsif Nr_Of_Suggestions = 2 then
736 Error_Msg_Node_2 := Suggestion_2;
737 Error_Msg_NE -- CODEFIX
738 ("\possible misspelling of& or&", Component, Suggestion_1);
739 end if;
740 end Check_Misspelled_Component;
742 ----------------------------------------
743 -- Check_Expr_OK_In_Limited_Aggregate --
744 ----------------------------------------
746 procedure Check_Expr_OK_In_Limited_Aggregate (Expr : Node_Id) is
747 begin
748 if Is_Limited_Type (Etype (Expr))
749 and then Comes_From_Source (Expr)
750 then
751 if In_Instance_Body or else In_Inlined_Body then
752 null;
754 elsif not OK_For_Limited_Init (Etype (Expr), Expr) then
755 Error_Msg_N
756 ("initialization not allowed for limited types", Expr);
757 Explain_Limited_Type (Etype (Expr), Expr);
758 end if;
759 end if;
760 end Check_Expr_OK_In_Limited_Aggregate;
762 -------------------------
763 -- Is_Others_Aggregate --
764 -------------------------
766 function Is_Others_Aggregate (Aggr : Node_Id) return Boolean is
767 Assoc : constant List_Id := Component_Associations (Aggr);
769 begin
770 return No (Expressions (Aggr))
771 and then Nkind (First (Choice_List (First (Assoc)))) = N_Others_Choice;
772 end Is_Others_Aggregate;
774 -------------------------
775 -- Is_Single_Aggregate --
776 -------------------------
778 function Is_Single_Aggregate (Aggr : Node_Id) return Boolean is
779 Assoc : constant List_Id := Component_Associations (Aggr);
781 begin
782 return No (Expressions (Aggr))
783 and then No (Next (First (Assoc)))
784 and then No (Next (First (Choice_List (First (Assoc)))));
785 end Is_Single_Aggregate;
787 -----------------------
788 -- Is_Null_Aggregate --
789 -----------------------
791 function Is_Null_Aggregate (N : Node_Id) return Boolean is
792 begin
793 return Ada_Version >= Ada_2022
794 and then Is_Homogeneous_Aggregate (N)
795 and then Is_Empty_List (Expressions (N))
796 and then Is_Empty_List (Component_Associations (N));
797 end Is_Null_Aggregate;
799 ----------------------------------------
800 -- Is_Null_Array_Aggregate_High_Bound --
801 ----------------------------------------
803 function Is_Null_Array_Aggregate_High_Bound (N : Node_Id) return Boolean is
804 Original_N : constant Node_Id := Original_Node (N);
805 begin
806 return Ada_Version >= Ada_2022
807 and then not Comes_From_Source (Original_N)
808 and then Nkind (Original_N) = N_Attribute_Reference
809 and then
810 Get_Attribute_Id (Attribute_Name (Original_N)) = Attribute_Pred
811 and then Nkind (Parent (N)) in N_Range | N_Op_Le
812 and then not Comes_From_Source (Parent (N));
813 end Is_Null_Array_Aggregate_High_Bound;
815 --------------------------------
816 -- Make_String_Into_Aggregate --
817 --------------------------------
819 procedure Make_String_Into_Aggregate (N : Node_Id) is
820 Exprs : constant List_Id := New_List;
821 Loc : constant Source_Ptr := Sloc (N);
822 Str : constant String_Id := Strval (N);
823 Strlen : constant Nat := String_Length (Str);
824 C : Char_Code;
825 C_Node : Node_Id;
826 New_N : Node_Id;
827 P : Source_Ptr;
829 begin
830 P := Loc + 1;
831 for J in 1 .. Strlen loop
832 C := Get_String_Char (Str, J);
833 Set_Character_Literal_Name (C);
835 C_Node :=
836 Make_Character_Literal (P,
837 Chars => Name_Find,
838 Char_Literal_Value => UI_From_CC (C));
839 Set_Etype (C_Node, Any_Character);
840 Append_To (Exprs, C_Node);
842 P := P + 1;
843 -- Something special for wide strings???
844 end loop;
846 New_N := Make_Aggregate (Loc, Expressions => Exprs);
847 Set_Analyzed (New_N);
848 Set_Etype (New_N, Any_Composite);
850 Rewrite (N, New_N);
851 end Make_String_Into_Aggregate;
853 -----------------------
854 -- Resolve_Aggregate --
855 -----------------------
857 procedure Resolve_Aggregate (N : Node_Id; Typ : Entity_Id) is
858 Loc : constant Source_Ptr := Sloc (N);
860 Aggr_Subtyp : Entity_Id;
861 -- The actual aggregate subtype. This is not necessarily the same as Typ
862 -- which is the subtype of the context in which the aggregate was found.
864 Others_Box : Boolean := False;
865 -- Set to True if N represents a simple aggregate with only
866 -- (others => <>), not nested as part of another aggregate.
868 function Is_Full_Access_Aggregate (N : Node_Id) return Boolean;
869 -- If a full access object is initialized with an aggregate or is
870 -- assigned an aggregate, we have to prevent a piecemeal access or
871 -- assignment to the object, even if the aggregate is to be expanded.
872 -- We create a temporary for the aggregate, and assign the temporary
873 -- instead, so that the back end can generate an atomic move for it.
874 -- This is only done in the context of an object declaration or an
875 -- assignment. Function is a noop and returns false in other contexts.
877 function Within_Aggregate (N : Node_Id) return Boolean;
878 -- Return True if N is part of an N_Aggregate
880 ------------------------------
881 -- Is_Full_Access_Aggregate --
882 ------------------------------
884 function Is_Full_Access_Aggregate (N : Node_Id) return Boolean is
885 Loc : constant Source_Ptr := Sloc (N);
887 New_N : Node_Id;
888 Par : Node_Id;
889 Temp : Entity_Id;
890 Typ : Entity_Id;
892 begin
893 Par := Parent (N);
895 -- Aggregate may be qualified, so find outer context
897 if Nkind (Par) = N_Qualified_Expression then
898 Par := Parent (Par);
899 end if;
901 if not Comes_From_Source (Par) then
902 return False;
903 end if;
905 case Nkind (Par) is
906 when N_Assignment_Statement =>
907 Typ := Etype (Name (Par));
909 if not Is_Full_Access (Typ)
910 and then not Is_Full_Access_Object (Name (Par))
911 then
912 return False;
913 end if;
915 when N_Object_Declaration =>
916 Typ := Etype (Defining_Identifier (Par));
918 if not Is_Full_Access (Typ)
919 and then not Is_Full_Access (Defining_Identifier (Par))
920 then
921 return False;
922 end if;
924 when others =>
925 return False;
926 end case;
928 Temp := Make_Temporary (Loc, 'T', N);
929 New_N :=
930 Make_Object_Declaration (Loc,
931 Defining_Identifier => Temp,
932 Constant_Present => True,
933 Object_Definition => New_Occurrence_Of (Typ, Loc),
934 Expression => Relocate_Node (N));
935 Insert_Action (Par, New_N);
937 Rewrite (N, New_Occurrence_Of (Temp, Loc));
938 Analyze_And_Resolve (N, Typ);
940 return True;
941 end Is_Full_Access_Aggregate;
943 ----------------------
944 -- Within_Aggregate --
945 ----------------------
947 function Within_Aggregate (N : Node_Id) return Boolean is
948 P : Node_Id := Parent (N);
949 begin
950 while Present (P) loop
951 if Nkind (P) = N_Aggregate then
952 return True;
953 end if;
955 P := Parent (P);
956 end loop;
958 return False;
959 end Within_Aggregate;
961 -- Start of processing for Resolve_Aggregate
963 begin
964 -- Ignore junk empty aggregate resulting from parser error
966 if No (Expressions (N))
967 and then No (Component_Associations (N))
968 and then not Null_Record_Present (N)
969 then
970 return;
972 -- If the aggregate is assigned to a full access variable, we have
973 -- to prevent a piecemeal assignment even if the aggregate is to be
974 -- expanded. We create a temporary for the aggregate, and assign the
975 -- temporary instead, so that the back end can generate an atomic move
976 -- for it. This is properly an expansion activity but it must be done
977 -- before resolution because aggregate resolution cannot be done twice.
979 elsif Expander_Active and then Is_Full_Access_Aggregate (N) then
980 return;
981 end if;
983 -- If the aggregate has box-initialized components, its type must be
984 -- frozen so that initialization procedures can properly be called
985 -- in the resolution that follows. The replacement of boxes with
986 -- initialization calls is properly an expansion activity but it must
987 -- be done during resolution.
989 if Expander_Active
990 and then Present (Component_Associations (N))
991 then
992 declare
993 Comp : Node_Id;
994 First_Comp : Boolean := True;
996 begin
997 Comp := First (Component_Associations (N));
998 while Present (Comp) loop
999 if Box_Present (Comp) then
1000 if First_Comp
1001 and then No (Expressions (N))
1002 and then Nkind (First (Choices (Comp))) = N_Others_Choice
1003 and then not Within_Aggregate (N)
1004 then
1005 Others_Box := True;
1006 end if;
1008 Insert_Actions (N, Freeze_Entity (Typ, N));
1009 exit;
1010 end if;
1012 First_Comp := False;
1013 Next (Comp);
1014 end loop;
1015 end;
1016 end if;
1018 -- Check for aggregates not allowed in configurable run-time mode.
1019 -- We allow all cases of aggregates that do not come from source, since
1020 -- these are all assumed to be small (e.g. bounds of a string literal).
1021 -- We also allow aggregates of types we know to be small.
1023 if not Support_Aggregates_On_Target
1024 and then Comes_From_Source (N)
1025 and then (not Known_Static_Esize (Typ)
1026 or else Esize (Typ) > System_Max_Integer_Size)
1027 then
1028 Error_Msg_CRT ("aggregate", N);
1029 end if;
1031 -- Ada 2005 (AI-287): Limited aggregates allowed
1033 -- In an instance, ignore aggregate subcomponents that may be limited,
1034 -- because they originate in view conflicts. If the original aggregate
1035 -- is legal and the actuals are legal, the aggregate itself is legal.
1037 if Is_Limited_Type (Typ)
1038 and then Ada_Version < Ada_2005
1039 and then not In_Instance
1040 then
1041 Error_Msg_N ("aggregate type cannot be limited", N);
1042 Explain_Limited_Type (Typ, N);
1044 elsif Is_Class_Wide_Type (Typ) then
1045 Error_Msg_N ("type of aggregate cannot be class-wide", N);
1047 elsif Typ = Any_String
1048 or else Typ = Any_Composite
1049 then
1050 Error_Msg_N ("no unique type for aggregate", N);
1051 Set_Etype (N, Any_Composite);
1053 elsif Is_Array_Type (Typ) and then Null_Record_Present (N) then
1054 Error_Msg_N ("null record forbidden in array aggregate", N);
1056 elsif Has_Aspect (Typ, Aspect_Aggregate)
1057 and then Ekind (Typ) /= E_Record_Type
1058 and then Ada_Version >= Ada_2022
1059 then
1060 -- Check for Ada 2022 and () aggregate.
1062 if not Is_Homogeneous_Aggregate (N) then
1063 Error_Msg_N ("container aggregate must use '['], not ()", N);
1064 end if;
1066 Resolve_Container_Aggregate (N, Typ);
1068 -- Check Ada 2022 empty aggregate [] initializing a record type that has
1069 -- aspect aggregate; the empty aggregate will be expanded into a call to
1070 -- the empty function specified in the aspect aggregate.
1072 elsif Has_Aspect (Typ, Aspect_Aggregate)
1073 and then Ekind (Typ) = E_Record_Type
1074 and then Is_Homogeneous_Aggregate (N)
1075 and then Is_Empty_List (Expressions (N))
1076 and then Is_Empty_List (Component_Associations (N))
1077 and then Ada_Version >= Ada_2022
1078 then
1079 Resolve_Container_Aggregate (N, Typ);
1081 elsif Is_Record_Type (Typ) then
1082 Resolve_Record_Aggregate (N, Typ);
1084 elsif Is_Array_Type (Typ) then
1086 -- First a special test, for the case of a positional aggregate of
1087 -- characters which can be replaced by a string literal.
1089 -- Do not perform this transformation if this was a string literal
1090 -- to start with, whose components needed constraint checks, or if
1091 -- the component type is non-static, because it will require those
1092 -- checks and be transformed back into an aggregate. If the index
1093 -- type is not Integer the aggregate may represent a user-defined
1094 -- string type but the context might need the original type so we
1095 -- do not perform the transformation at this point.
1097 if Number_Dimensions (Typ) = 1
1098 and then Is_Standard_Character_Type (Component_Type (Typ))
1099 and then No (Component_Associations (N))
1100 and then not Is_Limited_Composite (Typ)
1101 and then not Is_Private_Composite (Typ)
1102 and then not Is_Bit_Packed_Array (Typ)
1103 and then Nkind (Original_Node (Parent (N))) /= N_String_Literal
1104 and then Is_OK_Static_Subtype (Component_Type (Typ))
1105 and then Base_Type (Etype (First_Index (Typ))) =
1106 Base_Type (Standard_Integer)
1107 then
1108 declare
1109 Expr : Node_Id;
1111 begin
1112 Expr := First (Expressions (N));
1113 while Present (Expr) loop
1114 exit when Nkind (Expr) /= N_Character_Literal;
1115 Next (Expr);
1116 end loop;
1118 if No (Expr) then
1119 Start_String;
1121 Expr := First (Expressions (N));
1122 while Present (Expr) loop
1123 Store_String_Char (UI_To_CC (Char_Literal_Value (Expr)));
1124 Next (Expr);
1125 end loop;
1127 Rewrite (N, Make_String_Literal (Loc, End_String));
1129 Analyze_And_Resolve (N, Typ);
1130 return;
1131 end if;
1132 end;
1133 end if;
1135 -- Here if we have a real aggregate to deal with
1137 Array_Aggregate : declare
1138 Aggr_Resolved : Boolean;
1139 Aggr_Typ : constant Entity_Id := Etype (Typ);
1140 -- This is the unconstrained array type, which is the type against
1141 -- which the aggregate is to be resolved. Typ itself is the array
1142 -- type of the context which may not be the same subtype as the
1143 -- subtype for the final aggregate.
1145 Is_Null_Aggr : constant Boolean := Is_Null_Aggregate (N);
1147 begin
1148 -- In the following we determine whether an OTHERS choice is
1149 -- allowed inside the array aggregate. The test checks the context
1150 -- in which the array aggregate occurs. If the context does not
1151 -- permit it, or the aggregate type is unconstrained, an OTHERS
1152 -- choice is not allowed (except that it is always allowed on the
1153 -- right-hand side of an assignment statement; in this case the
1154 -- constrainedness of the type doesn't matter, because an array
1155 -- object is always constrained).
1157 -- If expansion is disabled (generic context, or semantics-only
1158 -- mode) actual subtypes cannot be constructed, and the type of an
1159 -- object may be its unconstrained nominal type. However, if the
1160 -- context is an assignment statement, OTHERS is allowed, because
1161 -- the target of the assignment will have a constrained subtype
1162 -- when fully compiled. Ditto if the context is an initialization
1163 -- procedure where a component may have a predicate function that
1164 -- carries the base type.
1166 -- Note that there is no node for Explicit_Actual_Parameter.
1167 -- To test for this context we therefore have to test for node
1168 -- N_Parameter_Association which itself appears only if there is a
1169 -- formal parameter. Consequently we also need to test for
1170 -- N_Procedure_Call_Statement or N_Function_Call.
1172 -- The context may be an N_Reference node, created by expansion.
1173 -- Legality of the others clause was established in the source,
1174 -- so the context is legal.
1176 Set_Etype (N, Aggr_Typ); -- May be overridden later on
1178 if Is_Null_Aggr then
1179 Set_Etype (N, Typ);
1180 Aggr_Resolved := Resolve_Null_Array_Aggregate (N);
1182 elsif Nkind (Parent (N)) = N_Assignment_Statement
1183 or else Inside_Init_Proc
1184 or else (Is_Constrained (Typ)
1185 and then Nkind (Parent (N)) in
1186 N_Parameter_Association
1187 | N_Function_Call
1188 | N_Procedure_Call_Statement
1189 | N_Generic_Association
1190 | N_Formal_Object_Declaration
1191 | N_Simple_Return_Statement
1192 | N_Object_Declaration
1193 | N_Component_Declaration
1194 | N_Parameter_Specification
1195 | N_Qualified_Expression
1196 | N_Unchecked_Type_Conversion
1197 | N_Reference
1198 | N_Aggregate
1199 | N_Extension_Aggregate
1200 | N_Component_Association
1201 | N_Case_Expression_Alternative
1202 | N_If_Expression
1203 | N_Expression_With_Actions)
1204 then
1205 Aggr_Resolved :=
1206 Resolve_Array_Aggregate
1208 Index => First_Index (Aggr_Typ),
1209 Index_Constr => First_Index (Typ),
1210 Component_Typ => Component_Type (Typ),
1211 Others_Allowed => True);
1212 else
1213 Aggr_Resolved :=
1214 Resolve_Array_Aggregate
1216 Index => First_Index (Aggr_Typ),
1217 Index_Constr => First_Index (Aggr_Typ),
1218 Component_Typ => Component_Type (Typ),
1219 Others_Allowed => False);
1220 end if;
1222 if not Aggr_Resolved then
1224 -- A parenthesized expression may have been intended as an
1225 -- aggregate, leading to a type error when analyzing the
1226 -- component. This can also happen for a nested component
1227 -- (see Analyze_Aggr_Expr).
1229 if Paren_Count (N) > 0 then
1230 Error_Msg_N
1231 ("positional aggregate cannot have one component", N);
1232 end if;
1234 Aggr_Subtyp := Any_Composite;
1236 else
1237 Aggr_Subtyp := Array_Aggr_Subtype (N, Typ);
1238 end if;
1240 Set_Etype (N, Aggr_Subtyp);
1241 end Array_Aggregate;
1243 elsif Is_Private_Type (Typ)
1244 and then Present (Full_View (Typ))
1245 and then (In_Inlined_Body or In_Instance_Body)
1246 and then Is_Composite_Type (Full_View (Typ))
1247 then
1248 Resolve (N, Full_View (Typ));
1250 else
1251 Error_Msg_N ("illegal context for aggregate", N);
1252 end if;
1254 -- If we can determine statically that the evaluation of the aggregate
1255 -- raises Constraint_Error, then replace the aggregate with an
1256 -- N_Raise_Constraint_Error node, but set the Etype to the right
1257 -- aggregate subtype. Gigi needs this.
1259 if Raises_Constraint_Error (N) then
1260 Aggr_Subtyp := Etype (N);
1261 Rewrite (N,
1262 Make_Raise_Constraint_Error (Loc, Reason => CE_Range_Check_Failed));
1263 Set_Raises_Constraint_Error (N);
1264 Set_Etype (N, Aggr_Subtyp);
1265 Set_Analyzed (N);
1266 end if;
1268 if Warn_On_No_Value_Assigned
1269 and then Others_Box
1270 and then not Is_Fully_Initialized_Type (Etype (N))
1271 then
1272 Error_Msg_N ("?v?aggregate not fully initialized", N);
1273 end if;
1275 Check_Function_Writable_Actuals (N);
1276 end Resolve_Aggregate;
1278 -----------------------------
1279 -- Resolve_Array_Aggregate --
1280 -----------------------------
1282 function Resolve_Array_Aggregate
1283 (N : Node_Id;
1284 Index : Node_Id;
1285 Index_Constr : Node_Id;
1286 Component_Typ : Entity_Id;
1287 Others_Allowed : Boolean) return Boolean
1289 Loc : constant Source_Ptr := Sloc (N);
1291 Failure : constant Boolean := False;
1292 Success : constant Boolean := True;
1294 Index_Typ : constant Entity_Id := Etype (Index);
1295 Index_Typ_Low : constant Node_Id := Type_Low_Bound (Index_Typ);
1296 Index_Typ_High : constant Node_Id := Type_High_Bound (Index_Typ);
1297 -- The type of the index corresponding to the array sub-aggregate along
1298 -- with its low and upper bounds.
1300 Index_Base : constant Entity_Id := Base_Type (Index_Typ);
1301 Index_Base_Low : constant Node_Id := Type_Low_Bound (Index_Base);
1302 Index_Base_High : constant Node_Id := Type_High_Bound (Index_Base);
1303 -- Ditto for the base type
1305 Others_Present : Boolean := False;
1307 Nb_Choices : Nat := 0;
1308 -- Contains the overall number of named choices in this sub-aggregate
1310 function Add (Val : Uint; To : Node_Id) return Node_Id;
1311 -- Creates a new expression node where Val is added to expression To.
1312 -- Tries to constant fold whenever possible. To must be an already
1313 -- analyzed expression.
1315 procedure Check_Bound (BH : Node_Id; AH : in out Node_Id);
1316 -- Checks that AH (the upper bound of an array aggregate) is less than
1317 -- or equal to BH (the upper bound of the index base type). If the check
1318 -- fails, a warning is emitted, the Raises_Constraint_Error flag of N is
1319 -- set, and AH is replaced with a duplicate of BH.
1321 procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id);
1322 -- Checks that range AL .. AH is compatible with range L .. H. Emits a
1323 -- warning if not and sets the Raises_Constraint_Error flag in N.
1325 procedure Check_Length (L, H : Node_Id; Len : Uint);
1326 -- Checks that range L .. H contains at least Len elements. Emits a
1327 -- warning if not and sets the Raises_Constraint_Error flag in N.
1329 function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean;
1330 -- Returns True if range L .. H is dynamic or null
1332 procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean);
1333 -- Given expression node From, this routine sets OK to False if it
1334 -- cannot statically evaluate From. Otherwise it stores this static
1335 -- value into Value.
1337 function Resolve_Aggr_Expr
1338 (Expr : Node_Id;
1339 Single_Elmt : Boolean) return Boolean;
1340 -- Resolves aggregate expression Expr. Returns False if resolution
1341 -- fails. If Single_Elmt is set to False, the expression Expr may be
1342 -- used to initialize several array aggregate elements (this can happen
1343 -- for discrete choices such as "L .. H => Expr" or the OTHERS choice).
1344 -- In this event we do not resolve Expr unless expansion is disabled.
1345 -- To know why, see the DELAYED COMPONENT RESOLUTION note above.
1347 -- NOTE: In the case of "... => <>", we pass the N_Component_Association
1348 -- node as Expr, since there is no Expression and we need a Sloc for the
1349 -- error message.
1351 procedure Resolve_Iterated_Component_Association
1352 (N : Node_Id;
1353 Index_Typ : Entity_Id);
1354 -- For AI12-061
1356 procedure Warn_On_Null_Component_Association (Expr : Node_Id);
1357 -- Expr is either a conditional expression or a case expression of an
1358 -- iterated component association initializing the aggregate N with
1359 -- components that can never be null. Report warning on associations
1360 -- that may initialize some component with a null value.
1362 ---------
1363 -- Add --
1364 ---------
1366 function Add (Val : Uint; To : Node_Id) return Node_Id is
1367 Expr_Pos : Node_Id;
1368 Expr : Node_Id;
1369 To_Pos : Node_Id;
1371 begin
1372 if Raises_Constraint_Error (To) then
1373 return To;
1374 end if;
1376 -- First test if we can do constant folding
1378 if Compile_Time_Known_Value (To)
1379 or else Nkind (To) = N_Integer_Literal
1380 then
1381 Expr_Pos := Make_Integer_Literal (Loc, Expr_Value (To) + Val);
1382 Set_Is_Static_Expression (Expr_Pos);
1383 Set_Etype (Expr_Pos, Etype (To));
1384 Set_Analyzed (Expr_Pos, Analyzed (To));
1386 if not Is_Enumeration_Type (Index_Typ) then
1387 Expr := Expr_Pos;
1389 -- If we are dealing with enumeration return
1390 -- Index_Typ'Val (Expr_Pos)
1392 else
1393 Expr :=
1394 Make_Attribute_Reference
1395 (Loc,
1396 Prefix => New_Occurrence_Of (Index_Typ, Loc),
1397 Attribute_Name => Name_Val,
1398 Expressions => New_List (Expr_Pos));
1399 end if;
1401 return Expr;
1402 end if;
1404 -- If we are here no constant folding possible
1406 if not Is_Enumeration_Type (Index_Base) then
1407 Expr :=
1408 Make_Op_Add (Loc,
1409 Left_Opnd => Duplicate_Subexpr (To),
1410 Right_Opnd => Make_Integer_Literal (Loc, Val));
1412 -- If we are dealing with enumeration return
1413 -- Index_Typ'Val (Index_Typ'Pos (To) + Val)
1415 else
1416 To_Pos :=
1417 Make_Attribute_Reference
1418 (Loc,
1419 Prefix => New_Occurrence_Of (Index_Typ, Loc),
1420 Attribute_Name => Name_Pos,
1421 Expressions => New_List (Duplicate_Subexpr (To)));
1423 Expr_Pos :=
1424 Make_Op_Add (Loc,
1425 Left_Opnd => To_Pos,
1426 Right_Opnd => Make_Integer_Literal (Loc, Val));
1428 Expr :=
1429 Make_Attribute_Reference
1430 (Loc,
1431 Prefix => New_Occurrence_Of (Index_Typ, Loc),
1432 Attribute_Name => Name_Val,
1433 Expressions => New_List (Expr_Pos));
1435 -- If the index type has a non standard representation, the
1436 -- attributes 'Val and 'Pos expand into function calls and the
1437 -- resulting expression is considered non-safe for reevaluation
1438 -- by the backend. Relocate it into a constant temporary in order
1439 -- to make it safe for reevaluation.
1441 if Has_Non_Standard_Rep (Etype (N)) then
1442 declare
1443 Def_Id : Entity_Id;
1445 begin
1446 Def_Id := Make_Temporary (Loc, 'R', Expr);
1447 Set_Etype (Def_Id, Index_Typ);
1448 Insert_Action (N,
1449 Make_Object_Declaration (Loc,
1450 Defining_Identifier => Def_Id,
1451 Object_Definition =>
1452 New_Occurrence_Of (Index_Typ, Loc),
1453 Constant_Present => True,
1454 Expression => Relocate_Node (Expr)));
1456 Expr := New_Occurrence_Of (Def_Id, Loc);
1457 end;
1458 end if;
1459 end if;
1461 return Expr;
1462 end Add;
1464 -----------------
1465 -- Check_Bound --
1466 -----------------
1468 procedure Check_Bound (BH : Node_Id; AH : in out Node_Id) is
1469 Val_BH : Uint;
1470 Val_AH : Uint;
1472 OK_BH : Boolean;
1473 OK_AH : Boolean;
1475 begin
1476 Get (Value => Val_BH, From => BH, OK => OK_BH);
1477 Get (Value => Val_AH, From => AH, OK => OK_AH);
1479 if OK_BH and then OK_AH and then Val_BH < Val_AH then
1480 Set_Raises_Constraint_Error (N);
1481 Error_Msg_Warn := SPARK_Mode /= On;
1482 Error_Msg_N ("upper bound out of range<<", AH);
1483 Error_Msg_N ("\Constraint_Error [<<", AH);
1485 -- You need to set AH to BH or else in the case of enumerations
1486 -- indexes we will not be able to resolve the aggregate bounds.
1488 AH := Duplicate_Subexpr (BH);
1489 end if;
1490 end Check_Bound;
1492 ------------------
1493 -- Check_Bounds --
1494 ------------------
1496 procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id) is
1497 Val_L : Uint;
1498 Val_H : Uint;
1499 Val_AL : Uint;
1500 Val_AH : Uint;
1502 OK_L : Boolean;
1503 OK_H : Boolean;
1505 OK_AL : Boolean;
1506 OK_AH : Boolean;
1507 pragma Warnings (Off, OK_AL);
1508 pragma Warnings (Off, OK_AH);
1510 begin
1511 if Raises_Constraint_Error (N)
1512 or else Dynamic_Or_Null_Range (AL, AH)
1513 then
1514 return;
1515 end if;
1517 Get (Value => Val_L, From => L, OK => OK_L);
1518 Get (Value => Val_H, From => H, OK => OK_H);
1520 Get (Value => Val_AL, From => AL, OK => OK_AL);
1521 Get (Value => Val_AH, From => AH, OK => OK_AH);
1523 if OK_L and then Val_L > Val_AL then
1524 Set_Raises_Constraint_Error (N);
1525 Error_Msg_Warn := SPARK_Mode /= On;
1526 Error_Msg_N ("lower bound of aggregate out of range<<", N);
1527 Error_Msg_N ("\Constraint_Error [<<", N);
1528 end if;
1530 if OK_H and then Val_H < Val_AH then
1531 Set_Raises_Constraint_Error (N);
1532 Error_Msg_Warn := SPARK_Mode /= On;
1533 Error_Msg_N ("upper bound of aggregate out of range<<", N);
1534 Error_Msg_N ("\Constraint_Error [<<", N);
1535 end if;
1536 end Check_Bounds;
1538 ------------------
1539 -- Check_Length --
1540 ------------------
1542 procedure Check_Length (L, H : Node_Id; Len : Uint) is
1543 Val_L : Uint;
1544 Val_H : Uint;
1546 OK_L : Boolean;
1547 OK_H : Boolean;
1549 Range_Len : Uint;
1551 begin
1552 if Raises_Constraint_Error (N) then
1553 return;
1554 end if;
1556 Get (Value => Val_L, From => L, OK => OK_L);
1557 Get (Value => Val_H, From => H, OK => OK_H);
1559 if not OK_L or else not OK_H then
1560 return;
1561 end if;
1563 -- If null range length is zero
1565 if Val_L > Val_H then
1566 Range_Len := Uint_0;
1567 else
1568 Range_Len := Val_H - Val_L + 1;
1569 end if;
1571 if Range_Len < Len then
1572 Set_Raises_Constraint_Error (N);
1573 Error_Msg_Warn := SPARK_Mode /= On;
1574 Error_Msg_N ("too many elements<<", N);
1575 Error_Msg_N ("\Constraint_Error [<<", N);
1576 end if;
1577 end Check_Length;
1579 ---------------------------
1580 -- Dynamic_Or_Null_Range --
1581 ---------------------------
1583 function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean is
1584 Val_L : Uint;
1585 Val_H : Uint;
1587 OK_L : Boolean;
1588 OK_H : Boolean;
1590 begin
1591 Get (Value => Val_L, From => L, OK => OK_L);
1592 Get (Value => Val_H, From => H, OK => OK_H);
1594 return not OK_L or else not OK_H
1595 or else not Is_OK_Static_Expression (L)
1596 or else not Is_OK_Static_Expression (H)
1597 or else Val_L > Val_H;
1598 end Dynamic_Or_Null_Range;
1600 ---------
1601 -- Get --
1602 ---------
1604 procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean) is
1605 begin
1606 OK := True;
1608 if Compile_Time_Known_Value (From) then
1609 Value := Expr_Value (From);
1611 -- If expression From is something like Some_Type'Val (10) then
1612 -- Value = 10.
1614 elsif Nkind (From) = N_Attribute_Reference
1615 and then Attribute_Name (From) = Name_Val
1616 and then Compile_Time_Known_Value (First (Expressions (From)))
1617 then
1618 Value := Expr_Value (First (Expressions (From)));
1619 else
1620 Value := Uint_0;
1621 OK := False;
1622 end if;
1623 end Get;
1625 -----------------------
1626 -- Resolve_Aggr_Expr --
1627 -----------------------
1629 function Resolve_Aggr_Expr
1630 (Expr : Node_Id;
1631 Single_Elmt : Boolean) return Boolean
1633 Nxt_Ind : constant Node_Id := Next_Index (Index);
1634 Nxt_Ind_Constr : constant Node_Id := Next_Index (Index_Constr);
1635 -- Index is the current index corresponding to the expression
1637 Resolution_OK : Boolean := True;
1638 -- Set to False if resolution of the expression failed
1640 begin
1641 -- Defend against previous errors
1643 if Nkind (Expr) = N_Error
1644 or else Error_Posted (Expr)
1645 then
1646 return True;
1647 end if;
1649 -- If the array type against which we are resolving the aggregate
1650 -- has several dimensions, the expressions nested inside the
1651 -- aggregate must be further aggregates (or strings).
1653 if Present (Nxt_Ind) then
1654 if Nkind (Expr) /= N_Aggregate then
1656 -- A string literal can appear where a one-dimensional array
1657 -- of characters is expected. If the literal looks like an
1658 -- operator, it is still an operator symbol, which will be
1659 -- transformed into a string when analyzed.
1661 if Is_Character_Type (Component_Typ)
1662 and then No (Next_Index (Nxt_Ind))
1663 and then Nkind (Expr) in N_String_Literal | N_Operator_Symbol
1664 then
1665 -- A string literal used in a multidimensional array
1666 -- aggregate in place of the final one-dimensional
1667 -- aggregate must not be enclosed in parentheses.
1669 if Paren_Count (Expr) /= 0 then
1670 Error_Msg_N ("no parenthesis allowed here", Expr);
1671 end if;
1673 Make_String_Into_Aggregate (Expr);
1675 else
1676 Error_Msg_N ("nested array aggregate expected", Expr);
1678 -- If the expression is parenthesized, this may be
1679 -- a missing component association for a 1-aggregate.
1681 if Paren_Count (Expr) > 0 then
1682 Error_Msg_N
1683 ("\if single-component aggregate is intended, "
1684 & "write e.g. (1 ='> ...)", Expr);
1685 end if;
1687 return Failure;
1688 end if;
1689 end if;
1691 -- If it's "... => <>", nothing to resolve
1693 if Nkind (Expr) = N_Component_Association then
1694 pragma Assert (Box_Present (Expr));
1695 return Success;
1696 end if;
1698 -- Ada 2005 (AI-231): Propagate the type to the nested aggregate.
1699 -- Required to check the null-exclusion attribute (if present).
1700 -- This value may be overridden later on.
1702 Set_Etype (Expr, Etype (N));
1704 Resolution_OK := Resolve_Array_Aggregate
1705 (Expr, Nxt_Ind, Nxt_Ind_Constr, Component_Typ, Others_Allowed);
1707 else
1708 -- If it's "... => <>", nothing to resolve
1710 if Nkind (Expr) = N_Component_Association then
1711 pragma Assert (Box_Present (Expr));
1712 return Success;
1713 end if;
1715 -- Do not resolve the expressions of discrete or others choices
1716 -- unless the expression covers a single component, or the
1717 -- expander is inactive.
1719 -- In SPARK mode, expressions that can perform side effects will
1720 -- be recognized by the gnat2why back-end, and the whole
1721 -- subprogram will be ignored. So semantic analysis can be
1722 -- performed safely.
1724 if Single_Elmt
1725 or else not Expander_Active
1726 or else In_Spec_Expression
1727 then
1728 Analyze_And_Resolve (Expr, Component_Typ);
1729 Check_Expr_OK_In_Limited_Aggregate (Expr);
1730 Check_Non_Static_Context (Expr);
1731 Aggregate_Constraint_Checks (Expr, Component_Typ);
1732 Check_Unset_Reference (Expr);
1733 end if;
1734 end if;
1736 -- If an aggregate component has a type with predicates, an explicit
1737 -- predicate check must be applied, as for an assignment statement,
1738 -- because the aggregate might not be expanded into individual
1739 -- component assignments. If the expression covers several components
1740 -- the analysis and the predicate check take place later.
1742 if Has_Predicates (Component_Typ)
1743 and then Analyzed (Expr)
1744 then
1745 Apply_Predicate_Check (Expr, Component_Typ);
1746 end if;
1748 if Raises_Constraint_Error (Expr)
1749 and then Nkind (Parent (Expr)) /= N_Component_Association
1750 then
1751 Set_Raises_Constraint_Error (N);
1752 end if;
1754 -- If the expression has been marked as requiring a range check,
1755 -- then generate it here. It's a bit odd to be generating such
1756 -- checks in the analyzer, but harmless since Generate_Range_Check
1757 -- does nothing (other than making sure Do_Range_Check is set) if
1758 -- the expander is not active.
1760 if Do_Range_Check (Expr) then
1761 Generate_Range_Check (Expr, Component_Typ, CE_Range_Check_Failed);
1762 end if;
1764 return Resolution_OK;
1765 end Resolve_Aggr_Expr;
1767 --------------------------------------------
1768 -- Resolve_Iterated_Component_Association --
1769 --------------------------------------------
1771 procedure Resolve_Iterated_Component_Association
1772 (N : Node_Id;
1773 Index_Typ : Entity_Id)
1775 Loc : constant Source_Ptr := Sloc (N);
1776 Id : constant Entity_Id := Defining_Identifier (N);
1778 -----------------------
1779 -- Remove_References --
1780 -----------------------
1782 function Remove_Reference (N : Node_Id) return Traverse_Result;
1783 -- Remove reference to the entity Id after analysis, so it can be
1784 -- properly reanalyzed after construct is expanded into a loop.
1786 function Remove_Reference (N : Node_Id) return Traverse_Result is
1787 begin
1788 if Nkind (N) = N_Identifier
1789 and then Present (Entity (N))
1790 and then Entity (N) = Id
1791 then
1792 Set_Entity (N, Empty);
1793 Set_Etype (N, Empty);
1794 end if;
1795 Set_Analyzed (N, False);
1796 return OK;
1797 end Remove_Reference;
1799 procedure Remove_References is new Traverse_Proc (Remove_Reference);
1801 -- Local variables
1803 Choice : Node_Id;
1804 Dummy : Boolean;
1805 Scop : Entity_Id;
1806 Expr : constant Node_Id := Expression (N);
1808 -- Start of processing for Resolve_Iterated_Component_Association
1810 begin
1811 Error_Msg_Ada_2022_Feature ("iterated component", Loc);
1813 -- Create a scope in which to introduce an index, to make it visible
1814 -- for the analysis of component expression.
1816 Scop := New_Internal_Entity (E_Loop, Current_Scope, Loc, 'L');
1817 Set_Etype (Scop, Standard_Void_Type);
1818 Set_Parent (Scop, Parent (N));
1819 Push_Scope (Scop);
1821 -- If there is iterator specification, then its preanalysis will make
1822 -- the index visible.
1824 if Present (Iterator_Specification (N)) then
1825 Preanalyze (Iterator_Specification (N));
1827 -- Otherwise, analyze discrete choices and make the index visible
1829 else
1830 -- Insert index name into current scope but don't decorate it yet,
1831 -- so that a premature usage of this name in discrete choices will
1832 -- be nicely diagnosed.
1834 Enter_Name (Id);
1836 Choice := First (Discrete_Choices (N));
1838 while Present (Choice) loop
1839 if Nkind (Choice) = N_Others_Choice then
1840 Others_Present := True;
1842 else
1843 Analyze (Choice);
1845 -- Choice can be a subtype name, a range, or an expression
1847 if Is_Entity_Name (Choice)
1848 and then Is_Type (Entity (Choice))
1849 and then
1850 Base_Type (Entity (Choice)) = Base_Type (Index_Typ)
1851 then
1852 null;
1854 else
1855 Analyze_And_Resolve (Choice, Index_Typ);
1856 end if;
1857 end if;
1859 Next (Choice);
1860 end loop;
1862 -- Decorate the index variable
1864 Set_Etype (Id, Index_Typ);
1865 Mutate_Ekind (Id, E_Variable);
1866 Set_Is_Not_Self_Hidden (Id);
1867 Set_Scope (Id, Scop);
1868 end if;
1870 -- Analyze expression without expansion, to verify legality.
1871 -- When generating code, we then remove references to the index
1872 -- variable, because the expression will be analyzed anew after
1873 -- rewritting as a loop with a new index variable; when not
1874 -- generating code we leave the analyzed expression as it is.
1876 Dummy := Resolve_Aggr_Expr (Expr, Single_Elmt => False);
1878 if Operating_Mode /= Check_Semantics then
1879 Remove_References (Expr);
1880 end if;
1882 -- An iterated_component_association may appear in a nested
1883 -- aggregate for a multidimensional structure: preserve the bounds
1884 -- computed for the expression, as well as the anonymous array
1885 -- type generated for it; both are needed during array expansion.
1887 if Nkind (Expr) = N_Aggregate then
1888 Set_Aggregate_Bounds (Expression (N), Aggregate_Bounds (Expr));
1889 Set_Etype (Expression (N), Etype (Expr));
1890 end if;
1892 End_Scope;
1893 end Resolve_Iterated_Component_Association;
1895 ----------------------------------------
1896 -- Warn_On_Null_Component_Association --
1897 ----------------------------------------
1899 procedure Warn_On_Null_Component_Association (Expr : Node_Id) is
1900 Comp_Typ : constant Entity_Id := Component_Type (Etype (N));
1902 procedure Check_Case_Expr (N : Node_Id);
1903 -- Check if a case expression may initialize some component with a
1904 -- null value.
1906 procedure Check_Cond_Expr (N : Node_Id);
1907 -- Check if a conditional expression may initialize some component
1908 -- with a null value.
1910 procedure Check_Expr (Expr : Node_Id);
1911 -- Check if an expression may initialize some component with a
1912 -- null value.
1914 procedure Warn_On_Null_Expression_And_Rewrite (Null_Expr : Node_Id);
1915 -- Report warning on known null expression and replace the expression
1916 -- by a raise constraint error node.
1918 ---------------------
1919 -- Check_Case_Expr --
1920 ---------------------
1922 procedure Check_Case_Expr (N : Node_Id) is
1923 Alt_Node : Node_Id := First (Alternatives (N));
1925 begin
1926 while Present (Alt_Node) loop
1927 Check_Expr (Expression (Alt_Node));
1928 Next (Alt_Node);
1929 end loop;
1930 end Check_Case_Expr;
1932 ---------------------
1933 -- Check_Cond_Expr --
1934 ---------------------
1936 procedure Check_Cond_Expr (N : Node_Id) is
1937 If_Expr : Node_Id := N;
1938 Then_Expr : Node_Id;
1939 Else_Expr : Node_Id;
1941 begin
1942 Then_Expr := Next (First (Expressions (If_Expr)));
1943 Else_Expr := Next (Then_Expr);
1945 Check_Expr (Then_Expr);
1947 -- Process elsif parts (if any)
1949 while Nkind (Else_Expr) = N_If_Expression loop
1950 If_Expr := Else_Expr;
1951 Then_Expr := Next (First (Expressions (If_Expr)));
1952 Else_Expr := Next (Then_Expr);
1954 Check_Expr (Then_Expr);
1955 end loop;
1957 if Known_Null (Else_Expr) then
1958 Warn_On_Null_Expression_And_Rewrite (Else_Expr);
1959 end if;
1960 end Check_Cond_Expr;
1962 ----------------
1963 -- Check_Expr --
1964 ----------------
1966 procedure Check_Expr (Expr : Node_Id) is
1967 begin
1968 if Known_Null (Expr) then
1969 Warn_On_Null_Expression_And_Rewrite (Expr);
1971 elsif Nkind (Expr) = N_If_Expression then
1972 Check_Cond_Expr (Expr);
1974 elsif Nkind (Expr) = N_Case_Expression then
1975 Check_Case_Expr (Expr);
1976 end if;
1977 end Check_Expr;
1979 -----------------------------------------
1980 -- Warn_On_Null_Expression_And_Rewrite --
1981 -----------------------------------------
1983 procedure Warn_On_Null_Expression_And_Rewrite (Null_Expr : Node_Id) is
1984 begin
1985 Error_Msg_N
1986 ("(Ada 2005) NULL not allowed in null-excluding component??",
1987 Null_Expr);
1988 Error_Msg_N
1989 ("\Constraint_Error might be raised at run time??", Null_Expr);
1991 -- We cannot use Apply_Compile_Time_Constraint_Error because in
1992 -- some cases the components are rewritten and the runtime error
1993 -- would be missed.
1995 Rewrite (Null_Expr,
1996 Make_Raise_Constraint_Error (Sloc (Null_Expr),
1997 Reason => CE_Access_Check_Failed));
1999 Set_Etype (Null_Expr, Comp_Typ);
2000 Set_Analyzed (Null_Expr);
2001 end Warn_On_Null_Expression_And_Rewrite;
2003 -- Start of processing for Warn_On_Null_Component_Association
2005 begin
2006 pragma Assert (Can_Never_Be_Null (Comp_Typ));
2008 case Nkind (Expr) is
2009 when N_If_Expression =>
2010 Check_Cond_Expr (Expr);
2012 when N_Case_Expression =>
2013 Check_Case_Expr (Expr);
2015 when others =>
2016 pragma Assert (False);
2017 null;
2018 end case;
2019 end Warn_On_Null_Component_Association;
2021 -- Local variables
2023 Assoc : Node_Id;
2024 Choice : Node_Id;
2025 Expr : Node_Id;
2026 Discard : Node_Id;
2028 Aggr_Low : Node_Id := Empty;
2029 Aggr_High : Node_Id := Empty;
2030 -- The actual low and high bounds of this sub-aggregate
2032 Case_Table_Size : Nat;
2033 -- Contains the size of the case table needed to sort aggregate choices
2035 Choices_Low : Node_Id := Empty;
2036 Choices_High : Node_Id := Empty;
2037 -- The lowest and highest discrete choices values for a named aggregate
2039 Delete_Choice : Boolean;
2040 -- Used when replacing a subtype choice with predicate by a list
2042 Has_Iterator_Specifications : Boolean := False;
2043 -- Flag to indicate that all named associations are iterated component
2044 -- associations with iterator specifications, in which case the
2045 -- expansion will create two loops: one to evaluate the size and one
2046 -- to generate the elements (4.3.3 (20.2/5)).
2048 Nb_Elements : Uint := Uint_0;
2049 -- The number of elements in a positional aggregate
2051 Nb_Discrete_Choices : Nat := 0;
2052 -- The overall number of discrete choices (not counting others choice)
2054 -- Start of processing for Resolve_Array_Aggregate
2056 begin
2057 -- Ignore junk empty aggregate resulting from parser error
2059 if No (Expressions (N))
2060 and then No (Component_Associations (N))
2061 and then not Null_Record_Present (N)
2062 then
2063 return Failure;
2064 end if;
2066 -- Disable the warning for GNAT Mode to allow for easier transition.
2068 if Ada_Version_Explicit >= Ada_2022
2069 and then Warn_On_Obsolescent_Feature
2070 and then not GNAT_Mode
2071 and then not Is_Homogeneous_Aggregate (N)
2072 and then not Is_Enum_Array_Aggregate (N)
2073 and then Is_Parenthesis_Aggregate (N)
2074 and then Nkind (Parent (N)) /= N_Qualified_Expression
2075 and then Comes_From_Source (N)
2076 then
2077 Error_Msg_N
2078 ("?j?array aggregate using () is an" &
2079 " obsolescent syntax, use '['] instead", N);
2080 end if;
2082 -- STEP 1: make sure the aggregate is correctly formatted
2084 if Is_Null_Aggregate (N) then
2085 null;
2087 elsif Present (Component_Associations (N)) then
2089 -- Verify that all or none of the component associations
2090 -- include an iterator specification.
2092 Assoc := First (Component_Associations (N));
2093 if Nkind (Assoc) = N_Iterated_Component_Association
2094 and then Present (Iterator_Specification (Assoc))
2095 then
2096 -- All other component associations must have an iterator spec.
2098 Next (Assoc);
2099 while Present (Assoc) loop
2100 if Nkind (Assoc) /= N_Iterated_Component_Association
2101 or else No (Iterator_Specification (Assoc))
2102 then
2103 Error_Msg_N ("mixed iterated component association"
2104 & " (RM 4.3.3 (17.1/5))",
2105 Assoc);
2106 return Failure;
2107 end if;
2109 Next (Assoc);
2110 end loop;
2112 Has_Iterator_Specifications := True;
2114 else
2115 -- or none of them do.
2117 Next (Assoc);
2118 while Present (Assoc) loop
2119 if Nkind (Assoc) = N_Iterated_Component_Association
2120 and then Present (Iterator_Specification (Assoc))
2121 then
2122 Error_Msg_N ("mixed iterated component association"
2123 & " (RM 4.3.3 (17.1/5))",
2124 Assoc);
2125 return Failure;
2126 end if;
2128 Next (Assoc);
2129 end loop;
2131 end if;
2133 Assoc := First (Component_Associations (N));
2134 while Present (Assoc) loop
2135 if Nkind (Assoc) = N_Iterated_Component_Association then
2136 Resolve_Iterated_Component_Association (Assoc, Index_Typ);
2138 elsif Nkind (Assoc) /= N_Component_Association then
2139 Error_Msg_N
2140 ("invalid component association for aggregate", Assoc);
2141 return Failure;
2142 end if;
2144 Choice := First (Choice_List (Assoc));
2145 Delete_Choice := False;
2146 while Present (Choice) loop
2147 if Nkind (Choice) = N_Others_Choice then
2148 Others_Present := True;
2150 if Choice /= First (Choice_List (Assoc))
2151 or else Present (Next (Choice))
2152 then
2153 Error_Msg_N
2154 ("OTHERS must appear alone in a choice list", Choice);
2155 return Failure;
2156 end if;
2158 if Present (Next (Assoc)) then
2159 Error_Msg_N
2160 ("OTHERS must appear last in an aggregate", Choice);
2161 return Failure;
2162 end if;
2164 if Ada_Version = Ada_83
2165 and then Assoc /= First (Component_Associations (N))
2166 and then Nkind (Parent (N)) in
2167 N_Assignment_Statement | N_Object_Declaration
2168 then
2169 Error_Msg_N
2170 ("(Ada 83) illegal context for OTHERS choice", N);
2171 end if;
2173 elsif Is_Entity_Name (Choice) then
2174 Analyze (Choice);
2176 declare
2177 E : constant Entity_Id := Entity (Choice);
2178 New_Cs : List_Id;
2179 P : Node_Id;
2180 C : Node_Id;
2182 begin
2183 if Is_Type (E) and then Has_Predicates (E) then
2184 Freeze_Before (N, E);
2186 if Has_Dynamic_Predicate_Aspect (E)
2187 or else Has_Ghost_Predicate_Aspect (E)
2188 then
2189 Error_Msg_NE
2190 ("subtype& has non-static predicate, not allowed "
2191 & "in aggregate choice", Choice, E);
2193 elsif not Is_OK_Static_Subtype (E) then
2194 Error_Msg_NE
2195 ("non-static subtype& has predicate, not allowed "
2196 & "in aggregate choice", Choice, E);
2197 end if;
2199 -- If the subtype has a static predicate, replace the
2200 -- original choice with the list of individual values
2201 -- covered by the predicate.
2202 -- This should be deferred to expansion time ???
2204 if Present (Static_Discrete_Predicate (E)) then
2205 Delete_Choice := True;
2207 New_Cs := New_List;
2208 P := First (Static_Discrete_Predicate (E));
2209 while Present (P) loop
2210 C := New_Copy (P);
2211 Set_Sloc (C, Sloc (Choice));
2212 Append_To (New_Cs, C);
2213 Next (P);
2214 end loop;
2216 Insert_List_After (Choice, New_Cs);
2217 end if;
2218 end if;
2219 end;
2220 end if;
2222 Nb_Choices := Nb_Choices + 1;
2224 declare
2225 C : constant Node_Id := Choice;
2227 begin
2228 Next (Choice);
2230 if Delete_Choice then
2231 Remove (C);
2232 Nb_Choices := Nb_Choices - 1;
2233 Delete_Choice := False;
2234 end if;
2235 end;
2236 end loop;
2238 Next (Assoc);
2239 end loop;
2240 end if;
2242 -- At this point we know that the others choice, if present, is by
2243 -- itself and appears last in the aggregate. Check if we have mixed
2244 -- positional and discrete associations (other than the others choice).
2246 if Present (Expressions (N))
2247 and then (Nb_Choices > 1
2248 or else (Nb_Choices = 1 and then not Others_Present))
2249 then
2250 Error_Msg_N
2251 ("cannot mix named and positional associations in array aggregate",
2252 First (Choice_List (First (Component_Associations (N)))));
2253 return Failure;
2254 end if;
2256 -- Test for the validity of an others choice if present
2258 if Others_Present and then not Others_Allowed then
2259 declare
2260 Others_N : constant Node_Id :=
2261 First (Choice_List (First (Component_Associations (N))));
2262 begin
2263 Error_Msg_N ("OTHERS choice not allowed here", Others_N);
2264 Error_Msg_N ("\qualify the aggregate with a constrained subtype "
2265 & "to provide bounds for it", Others_N);
2266 return Failure;
2267 end;
2268 end if;
2270 -- Protect against cascaded errors
2272 if Etype (Index_Typ) = Any_Type then
2273 return Failure;
2274 end if;
2276 -- STEP 2: Process named components
2278 if No (Expressions (N)) then
2279 if Others_Present then
2280 Case_Table_Size := Nb_Choices - 1;
2281 else
2282 Case_Table_Size := Nb_Choices;
2283 end if;
2285 Step_2 : declare
2286 function Empty_Range (A : Node_Id) return Boolean;
2287 -- If an association covers an empty range, some warnings on the
2288 -- expression of the association can be disabled.
2290 -----------------
2291 -- Empty_Range --
2292 -----------------
2294 function Empty_Range (A : Node_Id) return Boolean is
2295 R : Node_Id;
2297 begin
2298 if Nkind (A) = N_Iterated_Component_Association then
2299 R := First (Discrete_Choices (A));
2300 else
2301 R := First (Choices (A));
2302 end if;
2304 return No (Next (R))
2305 and then Nkind (R) = N_Range
2306 and then Compile_Time_Compare
2307 (Low_Bound (R), High_Bound (R), False) = GT;
2308 end Empty_Range;
2310 -- Local variables
2312 Low : Node_Id;
2313 High : Node_Id;
2314 -- Denote the lowest and highest values in an aggregate choice
2316 S_Low : Node_Id := Empty;
2317 S_High : Node_Id := Empty;
2318 -- if a choice in an aggregate is a subtype indication these
2319 -- denote the lowest and highest values of the subtype
2321 Table : Case_Table_Type (1 .. Case_Table_Size);
2322 -- Used to sort all the different choice values
2324 Single_Choice : Boolean;
2325 -- Set to true every time there is a single discrete choice in a
2326 -- discrete association
2328 Prev_Nb_Discrete_Choices : Nat;
2329 -- Used to keep track of the number of discrete choices in the
2330 -- current association.
2332 Errors_Posted_On_Choices : Boolean := False;
2333 -- Keeps track of whether any choices have semantic errors
2335 -- Start of processing for Step_2
2337 begin
2338 -- STEP 2 (A): Check discrete choices validity
2339 -- No need if this is an element iteration.
2341 Assoc := First (Component_Associations (N));
2342 while Present (Assoc)
2343 and then Present (Choice_List (Assoc))
2344 loop
2345 Prev_Nb_Discrete_Choices := Nb_Discrete_Choices;
2346 Choice := First (Choice_List (Assoc));
2348 loop
2349 Analyze (Choice);
2351 if Nkind (Choice) = N_Others_Choice then
2352 Single_Choice := False;
2353 exit;
2355 -- Test for subtype mark without constraint
2357 elsif Is_Entity_Name (Choice) and then
2358 Is_Type (Entity (Choice))
2359 then
2360 if Base_Type (Entity (Choice)) /= Index_Base then
2361 Error_Msg_N
2362 ("invalid subtype mark in aggregate choice",
2363 Choice);
2364 return Failure;
2365 end if;
2367 -- Case of subtype indication
2369 elsif Nkind (Choice) = N_Subtype_Indication then
2370 Resolve_Discrete_Subtype_Indication (Choice, Index_Base);
2372 if Has_Dynamic_Predicate_Aspect
2373 (Entity (Subtype_Mark (Choice)))
2374 or else Has_Ghost_Predicate_Aspect
2375 (Entity (Subtype_Mark (Choice)))
2376 then
2377 Error_Msg_NE
2378 ("subtype& has non-static predicate, "
2379 & "not allowed in aggregate choice",
2380 Choice, Entity (Subtype_Mark (Choice)));
2381 end if;
2383 -- Does the subtype indication evaluation raise CE?
2385 Get_Index_Bounds (Subtype_Mark (Choice), S_Low, S_High);
2386 Get_Index_Bounds (Choice, Low, High);
2387 Check_Bounds (S_Low, S_High, Low, High);
2389 -- Case of range or expression
2391 else
2392 Resolve (Choice, Index_Base);
2393 Check_Unset_Reference (Choice);
2394 Check_Non_Static_Context (Choice);
2396 -- If semantic errors were posted on the choice, then
2397 -- record that for possible early return from later
2398 -- processing (see handling of enumeration choices).
2400 if Error_Posted (Choice) then
2401 Errors_Posted_On_Choices := True;
2402 end if;
2404 -- Do not range check a choice. This check is redundant
2405 -- since this test is already done when we check that the
2406 -- bounds of the array aggregate are within range.
2408 Set_Do_Range_Check (Choice, False);
2409 end if;
2411 -- If we could not resolve the discrete choice stop here
2413 if Etype (Choice) = Any_Type then
2414 return Failure;
2416 -- If the discrete choice raises CE get its original bounds
2418 elsif Nkind (Choice) = N_Raise_Constraint_Error then
2419 Set_Raises_Constraint_Error (N);
2420 Get_Index_Bounds (Original_Node (Choice), Low, High);
2422 -- Otherwise get its bounds as usual
2424 else
2425 Get_Index_Bounds (Choice, Low, High);
2426 end if;
2428 if (Dynamic_Or_Null_Range (Low, High)
2429 or else (Nkind (Choice) = N_Subtype_Indication
2430 and then
2431 Dynamic_Or_Null_Range (S_Low, S_High)))
2432 and then Nb_Choices /= 1
2433 then
2434 Error_Msg_N
2435 ("dynamic or empty choice in aggregate "
2436 & "must be the only choice", Choice);
2437 return Failure;
2438 end if;
2440 if not (All_Composite_Constraints_Static (Low)
2441 and then All_Composite_Constraints_Static (High)
2442 and then All_Composite_Constraints_Static (S_Low)
2443 and then All_Composite_Constraints_Static (S_High))
2444 then
2445 Check_Restriction (No_Dynamic_Sized_Objects, Choice);
2446 end if;
2448 Nb_Discrete_Choices := Nb_Discrete_Choices + 1;
2449 Table (Nb_Discrete_Choices).Lo := Low;
2450 Table (Nb_Discrete_Choices).Hi := High;
2451 Table (Nb_Discrete_Choices).Choice := Choice;
2453 Next (Choice);
2455 if No (Choice) then
2457 -- Check if we have a single discrete choice and whether
2458 -- this discrete choice specifies a single value.
2460 Single_Choice :=
2461 Nb_Discrete_Choices = Prev_Nb_Discrete_Choices + 1
2462 and then Low = High;
2464 exit;
2465 end if;
2466 end loop;
2468 -- Ada 2005 (AI-231)
2470 if Ada_Version >= Ada_2005
2471 and then not Empty_Range (Assoc)
2472 then
2473 if Known_Null (Expression (Assoc)) then
2474 Check_Can_Never_Be_Null (Etype (N), Expression (Assoc));
2476 -- Report warning on iterated component association that may
2477 -- initialize some component of an array of null-excluding
2478 -- access type components with a null value. For example:
2480 -- type AList is array (...) of not null access Integer;
2481 -- L : AList :=
2482 -- [for J in A'Range =>
2483 -- (if Func (J) = 0 then A(J)'Access else Null)];
2485 elsif Ada_Version >= Ada_2022
2486 and then Can_Never_Be_Null (Component_Type (Etype (N)))
2487 and then Nkind (Assoc) = N_Iterated_Component_Association
2488 and then Nkind (Expression (Assoc)) in N_If_Expression
2489 | N_Case_Expression
2490 then
2491 Warn_On_Null_Component_Association (Expression (Assoc));
2492 end if;
2493 end if;
2495 -- Ada 2005 (AI-287): In case of default initialized component
2496 -- we delay the resolution to the expansion phase.
2498 if Box_Present (Assoc) then
2500 -- Ada 2005 (AI-287): In case of default initialization of a
2501 -- component the expander will generate calls to the
2502 -- corresponding initialization subprogram. We need to call
2503 -- Resolve_Aggr_Expr to check the rules about
2504 -- dimensionality.
2506 if not Resolve_Aggr_Expr
2507 (Assoc, Single_Elmt => Single_Choice)
2508 then
2509 return Failure;
2510 end if;
2512 -- ??? Checks for dynamically tagged expressions below will
2513 -- be only applied to iterated_component_association after
2514 -- expansion; in particular, errors might not be reported when
2515 -- -gnatc switch is used.
2517 elsif Nkind (Assoc) = N_Iterated_Component_Association then
2518 null; -- handled above, in a loop context
2520 elsif not Resolve_Aggr_Expr
2521 (Expression (Assoc), Single_Elmt => Single_Choice)
2522 then
2523 return Failure;
2525 -- Check incorrect use of dynamically tagged expression
2527 -- We differentiate here two cases because the expression may
2528 -- not be decorated. For example, the analysis and resolution
2529 -- of the expression associated with the others choice will be
2530 -- done later with the full aggregate. In such case we
2531 -- duplicate the expression tree to analyze the copy and
2532 -- perform the required check.
2534 elsif No (Etype (Expression (Assoc))) then
2535 declare
2536 Save_Analysis : constant Boolean := Full_Analysis;
2537 Expr : constant Node_Id :=
2538 New_Copy_Tree (Expression (Assoc));
2540 begin
2541 Expander_Mode_Save_And_Set (False);
2542 Full_Analysis := False;
2544 -- Analyze the expression, making sure it is properly
2545 -- attached to the tree before we do the analysis.
2547 Set_Parent (Expr, Parent (Expression (Assoc)));
2548 Analyze (Expr);
2550 -- Compute its dimensions now, rather than at the end of
2551 -- resolution, because in the case of multidimensional
2552 -- aggregates subsequent expansion may lead to spurious
2553 -- errors.
2555 Check_Expression_Dimensions (Expr, Component_Typ);
2557 -- If the expression is a literal, propagate this info
2558 -- to the expression in the association, to enable some
2559 -- optimizations downstream.
2561 if Is_Entity_Name (Expr)
2562 and then Present (Entity (Expr))
2563 and then Ekind (Entity (Expr)) = E_Enumeration_Literal
2564 then
2565 Analyze_And_Resolve
2566 (Expression (Assoc), Component_Typ);
2567 end if;
2569 Full_Analysis := Save_Analysis;
2570 Expander_Mode_Restore;
2572 if Is_Tagged_Type (Etype (Expr)) then
2573 Check_Dynamically_Tagged_Expression
2574 (Expr => Expr,
2575 Typ => Component_Type (Etype (N)),
2576 Related_Nod => N);
2577 end if;
2578 end;
2580 elsif Is_Tagged_Type (Etype (Expression (Assoc))) then
2581 Check_Dynamically_Tagged_Expression
2582 (Expr => Expression (Assoc),
2583 Typ => Component_Type (Etype (N)),
2584 Related_Nod => N);
2585 end if;
2587 Next (Assoc);
2588 end loop;
2590 -- If aggregate contains more than one choice then these must be
2591 -- static. Check for duplicate and missing values.
2593 -- Note: there is duplicated code here wrt Check_Choice_Set in
2594 -- the body of Sem_Case, and it is possible we could just reuse
2595 -- that procedure. To be checked ???
2597 if Nb_Discrete_Choices > 1 then
2598 Check_Choices : declare
2599 Choice : Node_Id;
2600 -- Location of choice for messages
2602 Hi_Val : Uint;
2603 Lo_Val : Uint;
2604 -- High end of one range and Low end of the next. Should be
2605 -- contiguous if there is no hole in the list of values.
2607 Lo_Dup : Uint;
2608 Hi_Dup : Uint;
2609 -- End points of duplicated range
2611 Missing_Or_Duplicates : Boolean := False;
2612 -- Set True if missing or duplicate choices found
2614 procedure Output_Bad_Choices (Lo, Hi : Uint; C : Node_Id);
2615 -- Output continuation message with a representation of the
2616 -- bounds (just Lo if Lo = Hi, else Lo .. Hi). C is the
2617 -- choice node where the message is to be posted.
2619 ------------------------
2620 -- Output_Bad_Choices --
2621 ------------------------
2623 procedure Output_Bad_Choices (Lo, Hi : Uint; C : Node_Id) is
2624 begin
2625 -- Enumeration type case
2627 if Is_Enumeration_Type (Index_Typ) then
2628 Error_Msg_Name_1 :=
2629 Chars (Get_Enum_Lit_From_Pos (Index_Typ, Lo, Loc));
2630 Error_Msg_Name_2 :=
2631 Chars (Get_Enum_Lit_From_Pos (Index_Typ, Hi, Loc));
2633 if Lo = Hi then
2634 Error_Msg_N ("\\ %!", C);
2635 else
2636 Error_Msg_N ("\\ % .. %!", C);
2637 end if;
2639 -- Integer types case
2641 else
2642 Error_Msg_Uint_1 := Lo;
2643 Error_Msg_Uint_2 := Hi;
2645 if Lo = Hi then
2646 Error_Msg_N ("\\ ^!", C);
2647 else
2648 Error_Msg_N ("\\ ^ .. ^!", C);
2649 end if;
2650 end if;
2651 end Output_Bad_Choices;
2653 -- Start of processing for Check_Choices
2655 begin
2656 Sort_Case_Table (Table);
2658 -- First we do a quick linear loop to find out if we have
2659 -- any duplicates or missing entries (usually we have a
2660 -- legal aggregate, so this will get us out quickly).
2662 for J in 1 .. Nb_Discrete_Choices - 1 loop
2663 Hi_Val := Expr_Value (Table (J).Hi);
2664 Lo_Val := Expr_Value (Table (J + 1).Lo);
2666 if Lo_Val <= Hi_Val
2667 or else (Lo_Val > Hi_Val + 1
2668 and then not Others_Present)
2669 then
2670 Missing_Or_Duplicates := True;
2671 exit;
2672 end if;
2673 end loop;
2675 -- If we have missing or duplicate entries, first fill in
2676 -- the Highest entries to make life easier in the following
2677 -- loops to detect bad entries.
2679 if Missing_Or_Duplicates then
2680 Table (1).Highest := Expr_Value (Table (1).Hi);
2682 for J in 2 .. Nb_Discrete_Choices loop
2683 Table (J).Highest :=
2684 UI_Max
2685 (Table (J - 1).Highest, Expr_Value (Table (J).Hi));
2686 end loop;
2688 -- Loop through table entries to find duplicate indexes
2690 for J in 2 .. Nb_Discrete_Choices loop
2691 Lo_Val := Expr_Value (Table (J).Lo);
2692 Hi_Val := Expr_Value (Table (J).Hi);
2694 -- Case where we have duplicates (the lower bound of
2695 -- this choice is less than or equal to the highest
2696 -- high bound found so far).
2698 if Lo_Val <= Table (J - 1).Highest then
2700 -- We move backwards looking for duplicates. We can
2701 -- abandon this loop as soon as we reach a choice
2702 -- highest value that is less than Lo_Val.
2704 for K in reverse 1 .. J - 1 loop
2705 exit when Table (K).Highest < Lo_Val;
2707 -- Here we may have duplicates between entries
2708 -- for K and J. Get range of duplicates.
2710 Lo_Dup :=
2711 UI_Max (Lo_Val, Expr_Value (Table (K).Lo));
2712 Hi_Dup :=
2713 UI_Min (Hi_Val, Expr_Value (Table (K).Hi));
2715 -- Nothing to do if duplicate range is null
2717 if Lo_Dup > Hi_Dup then
2718 null;
2720 -- Otherwise place proper message
2722 else
2723 -- We place message on later choice, with a
2724 -- line reference to the earlier choice.
2726 if Sloc (Table (J).Choice) <
2727 Sloc (Table (K).Choice)
2728 then
2729 Choice := Table (K).Choice;
2730 Error_Msg_Sloc := Sloc (Table (J).Choice);
2731 else
2732 Choice := Table (J).Choice;
2733 Error_Msg_Sloc := Sloc (Table (K).Choice);
2734 end if;
2736 if Lo_Dup = Hi_Dup then
2737 Error_Msg_N
2738 ("index value in array aggregate "
2739 & "duplicates the one given#!", Choice);
2740 else
2741 Error_Msg_N
2742 ("index values in array aggregate "
2743 & "duplicate those given#!", Choice);
2744 end if;
2746 Output_Bad_Choices (Lo_Dup, Hi_Dup, Choice);
2747 end if;
2748 end loop;
2749 end if;
2750 end loop;
2752 -- Loop through entries in table to find missing indexes.
2753 -- Not needed if others, since missing impossible.
2755 if not Others_Present then
2756 for J in 2 .. Nb_Discrete_Choices loop
2757 Lo_Val := Expr_Value (Table (J).Lo);
2758 Hi_Val := Table (J - 1).Highest;
2760 if Lo_Val > Hi_Val + 1 then
2762 declare
2763 Error_Node : Node_Id;
2765 begin
2766 -- If the choice is the bound of a range in
2767 -- a subtype indication, it is not in the
2768 -- source lists for the aggregate itself, so
2769 -- post the error on the aggregate. Otherwise
2770 -- post it on choice itself.
2772 Choice := Table (J).Choice;
2774 if Is_List_Member (Choice) then
2775 Error_Node := Choice;
2776 else
2777 Error_Node := N;
2778 end if;
2780 if Hi_Val + 1 = Lo_Val - 1 then
2781 Error_Msg_N
2782 ("missing index value "
2783 & "in array aggregate!", Error_Node);
2784 else
2785 Error_Msg_N
2786 ("missing index values "
2787 & "in array aggregate!", Error_Node);
2788 end if;
2790 Output_Bad_Choices
2791 (Hi_Val + 1, Lo_Val - 1, Error_Node);
2792 end;
2793 end if;
2794 end loop;
2795 end if;
2797 -- If either missing or duplicate values, return failure
2799 Set_Etype (N, Any_Composite);
2800 return Failure;
2801 end if;
2802 end Check_Choices;
2803 end if;
2805 if Has_Iterator_Specifications then
2806 -- Bounds will be determined dynamically.
2808 return Success;
2809 end if;
2811 -- STEP 2 (B): Compute aggregate bounds and min/max choices values
2813 if Nb_Discrete_Choices > 0 then
2814 Choices_Low := Table (1).Lo;
2815 Choices_High := Table (Nb_Discrete_Choices).Hi;
2816 end if;
2818 -- If Others is present, then bounds of aggregate come from the
2819 -- index constraint (not the choices in the aggregate itself).
2821 if Others_Present then
2822 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2824 -- Abandon processing if either bound is already signalled as
2825 -- an error (prevents junk cascaded messages and blow ups).
2827 if Nkind (Aggr_Low) = N_Error
2828 or else
2829 Nkind (Aggr_High) = N_Error
2830 then
2831 return False;
2832 end if;
2834 -- No others clause present
2836 else
2837 -- Special processing if others allowed and not present. This
2838 -- means that the bounds of the aggregate come from the index
2839 -- constraint (and the length must match).
2841 if Others_Allowed then
2842 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2844 -- Abandon processing if either bound is already signalled
2845 -- as an error (stop junk cascaded messages and blow ups).
2847 if Nkind (Aggr_Low) = N_Error
2848 or else
2849 Nkind (Aggr_High) = N_Error
2850 then
2851 return False;
2852 end if;
2854 -- If others allowed, and no others present, then the array
2855 -- should cover all index values. If it does not, we will
2856 -- get a length check warning, but there is two cases where
2857 -- an additional warning is useful:
2859 -- If we have no positional components, and the length is
2860 -- wrong (which we can tell by others being allowed with
2861 -- missing components), and the index type is an enumeration
2862 -- type, then issue appropriate warnings about these missing
2863 -- components. They are only warnings, since the aggregate
2864 -- is fine, it's just the wrong length. We skip this check
2865 -- for standard character types (since there are no literals
2866 -- and it is too much trouble to concoct them), and also if
2867 -- any of the bounds have values that are not known at
2868 -- compile time.
2870 -- Another case warranting a warning is when the length
2871 -- is right, but as above we have an index type that is
2872 -- an enumeration, and the bounds do not match. This is a
2873 -- case where dubious sliding is allowed and we generate a
2874 -- warning that the bounds do not match.
2876 if No (Expressions (N))
2877 and then Nkind (Index) = N_Range
2878 and then Is_Enumeration_Type (Etype (Index))
2879 and then not Is_Standard_Character_Type (Etype (Index))
2880 and then Compile_Time_Known_Value (Aggr_Low)
2881 and then Compile_Time_Known_Value (Aggr_High)
2882 and then Compile_Time_Known_Value (Choices_Low)
2883 and then Compile_Time_Known_Value (Choices_High)
2884 then
2885 -- If any of the expressions or range bounds in choices
2886 -- have semantic errors, then do not attempt further
2887 -- resolution, to prevent cascaded errors.
2889 if Errors_Posted_On_Choices then
2890 return Failure;
2891 end if;
2893 declare
2894 ALo : constant Node_Id := Expr_Value_E (Aggr_Low);
2895 AHi : constant Node_Id := Expr_Value_E (Aggr_High);
2896 CLo : constant Node_Id := Expr_Value_E (Choices_Low);
2897 CHi : constant Node_Id := Expr_Value_E (Choices_High);
2899 Ent : Entity_Id;
2901 begin
2902 -- Warning case 1, missing values at start/end. Only
2903 -- do the check if the number of entries is too small.
2905 if (Enumeration_Pos (CHi) - Enumeration_Pos (CLo))
2907 (Enumeration_Pos (AHi) - Enumeration_Pos (ALo))
2908 then
2909 Error_Msg_N
2910 ("missing index value(s) in array aggregate??",
2913 -- Output missing value(s) at start
2915 if Chars (ALo) /= Chars (CLo) then
2916 Ent := Prev (CLo);
2918 if Chars (ALo) = Chars (Ent) then
2919 Error_Msg_Name_1 := Chars (ALo);
2920 Error_Msg_N ("\ %??", N);
2921 else
2922 Error_Msg_Name_1 := Chars (ALo);
2923 Error_Msg_Name_2 := Chars (Ent);
2924 Error_Msg_N ("\ % .. %??", N);
2925 end if;
2926 end if;
2928 -- Output missing value(s) at end
2930 if Chars (AHi) /= Chars (CHi) then
2931 Ent := Next (CHi);
2933 if Chars (AHi) = Chars (Ent) then
2934 Error_Msg_Name_1 := Chars (Ent);
2935 Error_Msg_N ("\ %??", N);
2936 else
2937 Error_Msg_Name_1 := Chars (Ent);
2938 Error_Msg_Name_2 := Chars (AHi);
2939 Error_Msg_N ("\ % .. %??", N);
2940 end if;
2941 end if;
2943 -- Warning case 2, dubious sliding. The First_Subtype
2944 -- test distinguishes between a constrained type where
2945 -- sliding is not allowed (so we will get a warning
2946 -- later that Constraint_Error will be raised), and
2947 -- the unconstrained case where sliding is permitted.
2949 elsif (Enumeration_Pos (CHi) - Enumeration_Pos (CLo))
2951 (Enumeration_Pos (AHi) - Enumeration_Pos (ALo))
2952 and then Chars (ALo) /= Chars (CLo)
2953 and then
2954 not Is_Constrained (First_Subtype (Etype (N)))
2955 then
2956 Error_Msg_N
2957 ("bounds of aggregate do not match target??", N);
2958 end if;
2959 end;
2960 end if;
2961 end if;
2963 -- If no others, aggregate bounds come from aggregate
2965 Aggr_Low := Choices_Low;
2966 Aggr_High := Choices_High;
2967 end if;
2968 end Step_2;
2970 -- STEP 3: Process positional components
2972 else
2973 -- STEP 3 (A): Process positional elements
2975 Expr := First (Expressions (N));
2976 Nb_Elements := Uint_0;
2977 while Present (Expr) loop
2978 Nb_Elements := Nb_Elements + 1;
2980 -- Ada 2005 (AI-231)
2982 if Ada_Version >= Ada_2005 and then Known_Null (Expr) then
2983 Check_Can_Never_Be_Null (Etype (N), Expr);
2984 end if;
2986 if not Resolve_Aggr_Expr (Expr, Single_Elmt => True) then
2987 return Failure;
2988 end if;
2990 -- Check incorrect use of dynamically tagged expression
2992 if Is_Tagged_Type (Etype (Expr)) then
2993 Check_Dynamically_Tagged_Expression
2994 (Expr => Expr,
2995 Typ => Component_Type (Etype (N)),
2996 Related_Nod => N);
2997 end if;
2999 Next (Expr);
3000 end loop;
3002 if Others_Present then
3003 Assoc := Last (Component_Associations (N));
3005 -- Ada 2005 (AI-231)
3007 if Ada_Version >= Ada_2005 and then Known_Null (Assoc) then
3008 Check_Can_Never_Be_Null (Etype (N), Expression (Assoc));
3009 end if;
3011 -- Ada 2005 (AI-287): In case of default initialized component,
3012 -- we delay the resolution to the expansion phase.
3014 if Box_Present (Assoc) then
3016 -- Ada 2005 (AI-287): In case of default initialization of a
3017 -- component the expander will generate calls to the
3018 -- corresponding initialization subprogram. We need to call
3019 -- Resolve_Aggr_Expr to check the rules about
3020 -- dimensionality.
3022 if not Resolve_Aggr_Expr (Assoc, Single_Elmt => False) then
3023 return Failure;
3024 end if;
3026 elsif not Resolve_Aggr_Expr (Expression (Assoc),
3027 Single_Elmt => False)
3028 then
3029 return Failure;
3031 -- Check incorrect use of dynamically tagged expression. The
3032 -- expression of the others choice has not been resolved yet.
3033 -- In order to diagnose the semantic error we create a duplicate
3034 -- tree to analyze it and perform the check.
3036 elsif Nkind (Assoc) /= N_Iterated_Component_Association then
3037 declare
3038 Save_Analysis : constant Boolean := Full_Analysis;
3039 Expr : constant Node_Id :=
3040 New_Copy_Tree (Expression (Assoc));
3042 begin
3043 Expander_Mode_Save_And_Set (False);
3044 Full_Analysis := False;
3045 Analyze (Expr);
3046 Full_Analysis := Save_Analysis;
3047 Expander_Mode_Restore;
3049 if Is_Tagged_Type (Etype (Expr)) then
3050 Check_Dynamically_Tagged_Expression
3051 (Expr => Expr,
3052 Typ => Component_Type (Etype (N)),
3053 Related_Nod => N);
3054 end if;
3055 end;
3056 end if;
3057 end if;
3059 -- STEP 3 (B): Compute the aggregate bounds
3061 if Others_Present then
3062 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
3064 else
3065 if Others_Allowed then
3066 Get_Index_Bounds (Index_Constr, Aggr_Low, Discard);
3067 else
3068 Aggr_Low := Index_Typ_Low;
3069 end if;
3071 Aggr_High := Add (Nb_Elements - 1, To => Aggr_Low);
3072 Check_Bound (Index_Base_High, Aggr_High);
3073 end if;
3074 end if;
3076 -- STEP 4: Perform static aggregate checks and save the bounds
3078 -- Check (A)
3080 Check_Bounds (Index_Typ_Low, Index_Typ_High, Aggr_Low, Aggr_High);
3081 Check_Bounds (Index_Base_Low, Index_Base_High, Aggr_Low, Aggr_High);
3083 -- Check (B)
3085 if Others_Present and then Nb_Discrete_Choices > 0 then
3086 Check_Bounds (Aggr_Low, Aggr_High, Choices_Low, Choices_High);
3087 Check_Bounds (Index_Typ_Low, Index_Typ_High,
3088 Choices_Low, Choices_High);
3089 Check_Bounds (Index_Base_Low, Index_Base_High,
3090 Choices_Low, Choices_High);
3092 -- Check (C)
3094 elsif Others_Present and then Nb_Elements > 0 then
3095 Check_Length (Aggr_Low, Aggr_High, Nb_Elements);
3096 Check_Length (Index_Typ_Low, Index_Typ_High, Nb_Elements);
3097 Check_Length (Index_Base_Low, Index_Base_High, Nb_Elements);
3098 end if;
3100 if Raises_Constraint_Error (Aggr_Low)
3101 or else Raises_Constraint_Error (Aggr_High)
3102 then
3103 Set_Raises_Constraint_Error (N);
3104 end if;
3106 Aggr_Low := Duplicate_Subexpr (Aggr_Low);
3108 -- Do not duplicate Aggr_High if Aggr_High = Aggr_Low + Nb_Elements
3109 -- since the addition node returned by Add is not yet analyzed. Attach
3110 -- to tree and analyze first. Reset analyzed flag to ensure it will get
3111 -- analyzed when it is a literal bound whose type must be properly set.
3113 if Others_Present or else Nb_Discrete_Choices > 0 then
3114 Aggr_High := Duplicate_Subexpr (Aggr_High);
3116 if Etype (Aggr_High) = Universal_Integer then
3117 Set_Analyzed (Aggr_High, False);
3118 end if;
3119 end if;
3121 -- If the aggregate already has bounds attached to it, it means this is
3122 -- a positional aggregate created as an optimization by
3123 -- Exp_Aggr.Convert_To_Positional, so we don't want to change those
3124 -- bounds.
3126 if Present (Aggregate_Bounds (N))
3127 and then not Others_Allowed
3128 and then not Comes_From_Source (N)
3129 then
3130 Aggr_Low := Low_Bound (Aggregate_Bounds (N));
3131 Aggr_High := High_Bound (Aggregate_Bounds (N));
3132 end if;
3134 Set_Aggregate_Bounds
3135 (N, Make_Range (Loc, Low_Bound => Aggr_Low, High_Bound => Aggr_High));
3137 -- The bounds may contain expressions that must be inserted upwards.
3138 -- Attach them fully to the tree. After analysis, remove side effects
3139 -- from upper bound, if still needed.
3141 Set_Parent (Aggregate_Bounds (N), N);
3142 Analyze_And_Resolve (Aggregate_Bounds (N), Index_Typ);
3143 Check_Unset_Reference (Aggregate_Bounds (N));
3145 if not Others_Present and then Nb_Discrete_Choices = 0 then
3146 Set_High_Bound
3147 (Aggregate_Bounds (N),
3148 Duplicate_Subexpr (High_Bound (Aggregate_Bounds (N))));
3149 end if;
3151 -- Check the dimensions of each component in the array aggregate
3153 Analyze_Dimension_Array_Aggregate (N, Component_Typ);
3155 return Success;
3156 end Resolve_Array_Aggregate;
3158 ---------------------------------
3159 -- Resolve_Container_Aggregate --
3160 ---------------------------------
3162 procedure Resolve_Container_Aggregate (N : Node_Id; Typ : Entity_Id) is
3163 procedure Resolve_Iterated_Association
3164 (Comp : Node_Id;
3165 Key_Type : Entity_Id;
3166 Elmt_Type : Entity_Id);
3167 -- Resolve choices and expression in an iterated component association
3168 -- or an iterated element association, which has a key_expression.
3169 -- This is similar but not identical to the handling of this construct
3170 -- in an array aggregate.
3171 -- For a named container, the type of each choice must be compatible
3172 -- with the key type. For a positional container, the choice must be
3173 -- a subtype indication or an iterator specification that determines
3174 -- an element type.
3176 Asp : constant Node_Id := Find_Value_Of_Aspect (Typ, Aspect_Aggregate);
3178 Empty_Subp : Node_Id := Empty;
3179 Add_Named_Subp : Node_Id := Empty;
3180 Add_Unnamed_Subp : Node_Id := Empty;
3181 New_Indexed_Subp : Node_Id := Empty;
3182 Assign_Indexed_Subp : Node_Id := Empty;
3184 ----------------------------------
3185 -- Resolve_Iterated_Association --
3186 ----------------------------------
3188 procedure Resolve_Iterated_Association
3189 (Comp : Node_Id;
3190 Key_Type : Entity_Id;
3191 Elmt_Type : Entity_Id)
3193 Loc : constant Source_Ptr := Sloc (N);
3194 Choice : Node_Id;
3195 Copy : Node_Id;
3196 Ent : Entity_Id;
3197 Expr : Node_Id;
3198 Key_Expr : Node_Id;
3199 Id : Entity_Id;
3200 Id_Name : Name_Id;
3201 Typ : Entity_Id := Empty;
3203 begin
3204 Error_Msg_Ada_2022_Feature ("iterated component", Loc);
3206 -- If this is an Iterated_Element_Association then either a
3207 -- an Iterator_Specification or a Loop_Parameter specification
3208 -- is present. In both cases a Key_Expression is present.
3210 if Nkind (Comp) = N_Iterated_Element_Association then
3212 -- Create a temporary scope to avoid some modifications from
3213 -- escaping the Analyze call below. The original Tree will be
3214 -- reanalyzed later.
3216 Ent := New_Internal_Entity
3217 (E_Loop, Current_Scope, Sloc (Comp), 'L');
3218 Set_Etype (Ent, Standard_Void_Type);
3219 Set_Parent (Ent, Parent (Comp));
3220 Push_Scope (Ent);
3222 if Present (Loop_Parameter_Specification (Comp)) then
3223 Copy := Copy_Separate_Tree (Comp);
3225 Analyze
3226 (Loop_Parameter_Specification (Copy));
3228 Id_Name := Chars (Defining_Identifier
3229 (Loop_Parameter_Specification (Comp)));
3230 else
3231 Copy := Copy_Separate_Tree (Iterator_Specification (Comp));
3232 Analyze (Copy);
3234 Id_Name := Chars (Defining_Identifier
3235 (Iterator_Specification (Comp)));
3236 end if;
3238 -- Key expression must have the type of the key. We analyze
3239 -- a copy of the original expression, because it will be
3240 -- reanalyzed and copied as needed during expansion of the
3241 -- corresponding loop.
3243 Key_Expr := Key_Expression (Comp);
3244 Analyze_And_Resolve (New_Copy_Tree (Key_Expr), Key_Type);
3245 End_Scope;
3247 Typ := Key_Type;
3249 elsif Present (Iterator_Specification (Comp)) then
3250 -- Create a temporary scope to avoid some modifications from
3251 -- escaping the Analyze call below. The original Tree will be
3252 -- reanalyzed later.
3254 Ent := New_Internal_Entity
3255 (E_Loop, Current_Scope, Sloc (Comp), 'L');
3256 Set_Etype (Ent, Standard_Void_Type);
3257 Set_Parent (Ent, Parent (Comp));
3258 Push_Scope (Ent);
3260 Copy := Copy_Separate_Tree (Iterator_Specification (Comp));
3261 Id_Name :=
3262 Chars (Defining_Identifier (Iterator_Specification (Comp)));
3264 Preanalyze (Copy);
3266 End_Scope;
3268 Typ := Etype (Defining_Identifier (Copy));
3270 else
3271 Choice := First (Discrete_Choices (Comp));
3273 while Present (Choice) loop
3274 Analyze (Choice);
3276 -- Choice can be a subtype name, a range, or an expression
3278 if Is_Entity_Name (Choice)
3279 and then Is_Type (Entity (Choice))
3280 and then Base_Type (Entity (Choice)) = Base_Type (Key_Type)
3281 then
3282 null;
3284 elsif Present (Key_Type) then
3285 Analyze_And_Resolve (Choice, Key_Type);
3286 Typ := Key_Type;
3287 else
3288 Typ := Etype (Choice); -- assume unique for now
3289 end if;
3291 Next (Choice);
3292 end loop;
3294 Id_Name := Chars (Defining_Identifier (Comp));
3295 end if;
3297 -- Create a scope in which to introduce an index, which is usually
3298 -- visible in the expression for the component, and needed for its
3299 -- analysis.
3301 Id := Make_Defining_Identifier (Sloc (Comp), Id_Name);
3302 Ent := New_Internal_Entity (E_Loop,
3303 Current_Scope, Sloc (Comp), 'L');
3304 Set_Etype (Ent, Standard_Void_Type);
3305 Set_Parent (Ent, Parent (Comp));
3306 Push_Scope (Ent);
3308 -- Insert and decorate the loop variable in the current scope.
3309 -- The expression has to be analyzed once the loop variable is
3310 -- directly visible. Mark the variable as referenced to prevent
3311 -- spurious warnings, given that subsequent uses of its name in the
3312 -- expression will reference the internal (synonym) loop variable.
3314 Enter_Name (Id);
3316 pragma Assert (Present (Typ));
3317 Set_Etype (Id, Typ);
3319 Mutate_Ekind (Id, E_Variable);
3320 Set_Is_Not_Self_Hidden (Id);
3321 Set_Scope (Id, Ent);
3322 Set_Referenced (Id);
3324 -- Analyze a copy of the expression, to verify legality. We use
3325 -- a copy because the expression will be analyzed anew when the
3326 -- enclosing aggregate is expanded, and the construct is rewritten
3327 -- as a loop with a new index variable.
3329 Expr := New_Copy_Tree (Expression (Comp));
3330 Preanalyze_And_Resolve (Expr, Elmt_Type);
3331 End_Scope;
3333 end Resolve_Iterated_Association;
3335 -- Start of processing for Resolve_Container_Aggregate
3337 begin
3338 pragma Assert (Nkind (Asp) = N_Aggregate);
3340 Set_Etype (N, Typ);
3341 Parse_Aspect_Aggregate (Asp,
3342 Empty_Subp, Add_Named_Subp, Add_Unnamed_Subp,
3343 New_Indexed_Subp, Assign_Indexed_Subp);
3345 if Present (Add_Unnamed_Subp)
3346 and then No (New_Indexed_Subp)
3347 and then Present (Etype (Add_Unnamed_Subp))
3348 and then Etype (Add_Unnamed_Subp) /= Any_Type
3349 then
3350 declare
3351 Elmt_Type : constant Entity_Id :=
3352 Etype (Next_Formal
3353 (First_Formal (Entity (Add_Unnamed_Subp))));
3354 Comp : Node_Id;
3356 begin
3357 if Present (Expressions (N)) then
3358 -- positional aggregate
3360 Comp := First (Expressions (N));
3361 while Present (Comp) loop
3362 Analyze_And_Resolve (Comp, Elmt_Type);
3363 Next (Comp);
3364 end loop;
3365 end if;
3367 -- Empty aggregate, to be replaced by Empty during
3368 -- expansion, or iterated component association.
3370 if Present (Component_Associations (N)) then
3371 declare
3372 Comp : Node_Id := First (Component_Associations (N));
3373 begin
3374 while Present (Comp) loop
3375 if Nkind (Comp) /=
3376 N_Iterated_Component_Association
3377 then
3378 Error_Msg_N ("illegal component association "
3379 & "for unnamed container aggregate", Comp);
3380 return;
3381 else
3382 Resolve_Iterated_Association
3383 (Comp, Empty, Elmt_Type);
3384 end if;
3386 Next (Comp);
3387 end loop;
3388 end;
3389 end if;
3390 end;
3392 elsif Present (Add_Named_Subp)
3393 and then Etype (Add_Named_Subp) /= Any_Type
3394 then
3395 declare
3396 -- Retrieves types of container, key, and element from the
3397 -- specified insertion procedure.
3399 Container : constant Entity_Id :=
3400 First_Formal (Entity (Add_Named_Subp));
3401 Key_Type : constant Entity_Id := Etype (Next_Formal (Container));
3402 Elmt_Type : constant Entity_Id :=
3403 Etype (Next_Formal (Next_Formal (Container)));
3404 Comp : Node_Id;
3405 Choice : Node_Id;
3407 begin
3408 Comp := First (Component_Associations (N));
3409 while Present (Comp) loop
3410 if Nkind (Comp) = N_Component_Association then
3411 Choice := First (Choices (Comp));
3413 while Present (Choice) loop
3414 Analyze_And_Resolve (Choice, Key_Type);
3415 if not Is_Static_Expression (Choice) then
3416 Error_Msg_N ("choice must be static", Choice);
3417 end if;
3419 Next (Choice);
3420 end loop;
3422 Analyze_And_Resolve (Expression (Comp), Elmt_Type);
3424 elsif Nkind (Comp) in
3425 N_Iterated_Component_Association |
3426 N_Iterated_Element_Association
3427 then
3428 Resolve_Iterated_Association
3429 (Comp, Key_Type, Elmt_Type);
3430 end if;
3432 Next (Comp);
3433 end loop;
3434 end;
3436 elsif Present (Assign_Indexed_Subp)
3437 and then Etype (Assign_Indexed_Subp) /= Any_Type
3438 then
3439 -- Indexed Aggregate. Positional or indexed component
3440 -- can be present, but not both. Choices must be static
3441 -- values or ranges with static bounds.
3443 declare
3444 Container : constant Entity_Id :=
3445 First_Formal (Entity (Assign_Indexed_Subp));
3446 Index_Type : constant Entity_Id := Etype (Next_Formal (Container));
3447 Comp_Type : constant Entity_Id :=
3448 Etype (Next_Formal (Next_Formal (Container)));
3449 Comp : Node_Id;
3450 Choice : Node_Id;
3451 Num_Choices : Nat := 0;
3453 Hi_Val : Uint;
3454 Lo_Val : Uint;
3455 begin
3456 if Present (Expressions (N)) then
3457 Comp := First (Expressions (N));
3458 while Present (Comp) loop
3459 Analyze_And_Resolve (Comp, Comp_Type);
3460 Next (Comp);
3461 end loop;
3462 end if;
3464 if Present (Component_Associations (N))
3465 and then not Is_Empty_List (Component_Associations (N))
3466 then
3467 if Present (Expressions (N))
3468 and then not Is_Empty_List (Expressions (N))
3469 then
3470 Error_Msg_N ("container aggregate cannot be "
3471 & "both positional and named", N);
3472 return;
3473 end if;
3475 Comp := First (Component_Associations (N));
3477 while Present (Comp) loop
3478 if Nkind (Comp) = N_Component_Association then
3479 Choice := First (Choices (Comp));
3481 while Present (Choice) loop
3482 Analyze_And_Resolve (Choice, Index_Type);
3483 Num_Choices := Num_Choices + 1;
3484 Next (Choice);
3485 end loop;
3487 Analyze_And_Resolve (Expression (Comp), Comp_Type);
3489 elsif Nkind (Comp) in
3490 N_Iterated_Component_Association |
3491 N_Iterated_Element_Association
3492 then
3493 Resolve_Iterated_Association
3494 (Comp, Index_Type, Comp_Type);
3495 Num_Choices := Num_Choices + 1;
3496 end if;
3498 Next (Comp);
3499 end loop;
3501 -- The component associations in an indexed aggregate
3502 -- must denote a contiguous set of static values. We
3503 -- build a table of values/ranges and sort it, as is done
3504 -- elsewhere for case statements and array aggregates.
3505 -- If the aggregate has a single iterated association it
3506 -- is allowed to be nonstatic and there is nothing to check.
3508 if Num_Choices > 1 then
3509 declare
3510 Table : Case_Table_Type (1 .. Num_Choices);
3511 No_Choice : Pos := 1;
3512 Lo, Hi : Node_Id;
3514 -- Traverse aggregate to determine size of needed table.
3515 -- Verify that bounds are static and that loops have no
3516 -- filters or key expressions.
3518 begin
3519 Comp := First (Component_Associations (N));
3520 while Present (Comp) loop
3521 if Nkind (Comp) = N_Iterated_Element_Association then
3522 if Present
3523 (Loop_Parameter_Specification (Comp))
3524 then
3525 if Present (Iterator_Filter
3526 (Loop_Parameter_Specification (Comp)))
3527 then
3528 Error_Msg_N
3529 ("iterator filter not allowed " &
3530 "in indexed aggregate", Comp);
3531 return;
3533 elsif Present (Key_Expression
3534 (Loop_Parameter_Specification (Comp)))
3535 then
3536 Error_Msg_N
3537 ("key expression not allowed " &
3538 "in indexed aggregate", Comp);
3539 return;
3540 end if;
3541 end if;
3542 else
3543 Choice := First (Choices (Comp));
3545 while Present (Choice) loop
3546 Get_Index_Bounds (Choice, Lo, Hi);
3547 Table (No_Choice).Choice := Choice;
3548 Table (No_Choice).Lo := Lo;
3549 Table (No_Choice).Hi := Hi;
3551 -- Verify staticness of value or range
3553 if not Is_Static_Expression (Lo)
3554 or else not Is_Static_Expression (Hi)
3555 then
3556 Error_Msg_N
3557 ("nonstatic expression for index " &
3558 "for indexed aggregate", Choice);
3559 return;
3560 end if;
3562 No_Choice := No_Choice + 1;
3563 Next (Choice);
3564 end loop;
3565 end if;
3567 Next (Comp);
3568 end loop;
3570 Sort_Case_Table (Table);
3572 for J in 1 .. Num_Choices - 1 loop
3573 Hi_Val := Expr_Value (Table (J).Hi);
3574 Lo_Val := Expr_Value (Table (J + 1).Lo);
3576 if Lo_Val = Hi_Val then
3577 Error_Msg_N
3578 ("duplicate index in indexed aggregate",
3579 Table (J + 1).Choice);
3580 exit;
3582 elsif Lo_Val < Hi_Val then
3583 Error_Msg_N
3584 ("overlapping indices in indexed aggregate",
3585 Table (J + 1).Choice);
3586 exit;
3588 elsif Lo_Val > Hi_Val + 1 then
3589 Error_Msg_N
3590 ("missing index values", Table (J + 1).Choice);
3591 exit;
3592 end if;
3593 end loop;
3594 end;
3595 end if;
3596 end if;
3597 end;
3598 end if;
3599 end Resolve_Container_Aggregate;
3601 -----------------------------
3602 -- Resolve_Delta_Aggregate --
3603 -----------------------------
3605 procedure Resolve_Delta_Aggregate (N : Node_Id; Typ : Entity_Id) is
3606 Base : constant Node_Id := Expression (N);
3608 begin
3609 Error_Msg_Ada_2022_Feature ("delta aggregate", Sloc (N));
3611 if not Is_Composite_Type (Typ) then
3612 Error_Msg_N ("not a composite type", N);
3613 end if;
3615 Analyze_And_Resolve (Base, Typ);
3617 if Is_Array_Type (Typ) then
3618 -- For an array_delta_aggregate, the base_expression and each
3619 -- expression in every array_component_association shall be of a
3620 -- nonlimited type; RM 4.3.4(13/5). However, to prevent repeated
3621 -- errors we only check the base expression and not array component
3622 -- associations.
3624 if Is_Limited_Type (Etype (Base)) then
3625 Error_Msg_N
3626 ("array delta aggregate shall be of a nonlimited type", Base);
3627 Explain_Limited_Type (Etype (Base), Base);
3628 end if;
3630 Resolve_Delta_Array_Aggregate (N, Typ);
3631 else
3633 -- Delta aggregates for record types must use parentheses,
3634 -- not square brackets.
3636 if Is_Homogeneous_Aggregate (N) then
3637 Error_Msg_N
3638 ("delta aggregates for record types must use (), not '[']", N);
3639 end if;
3641 -- The base_expression of a record_delta_aggregate can be of a
3642 -- limited type only if it is newly constructed; RM 7.5(2.1/5).
3644 Check_Expr_OK_In_Limited_Aggregate (Base);
3646 Resolve_Delta_Record_Aggregate (N, Typ);
3647 end if;
3649 Set_Etype (N, Typ);
3650 end Resolve_Delta_Aggregate;
3652 -----------------------------------
3653 -- Resolve_Delta_Array_Aggregate --
3654 -----------------------------------
3656 procedure Resolve_Delta_Array_Aggregate (N : Node_Id; Typ : Entity_Id) is
3657 Deltas : constant List_Id := Component_Associations (N);
3658 Index_Type : constant Entity_Id := Etype (First_Index (Typ));
3660 Assoc : Node_Id;
3661 Choice : Node_Id;
3662 Expr : Node_Id;
3664 begin
3665 Assoc := First (Deltas);
3666 while Present (Assoc) loop
3667 if Nkind (Assoc) = N_Iterated_Component_Association then
3668 Choice := First (Choice_List (Assoc));
3669 while Present (Choice) loop
3670 if Nkind (Choice) = N_Others_Choice then
3671 Error_Msg_N
3672 ("OTHERS not allowed in delta aggregate", Choice);
3674 elsif Nkind (Choice) = N_Subtype_Indication then
3675 Resolve_Discrete_Subtype_Indication
3676 (Choice, Base_Type (Index_Type));
3678 else
3679 Analyze_And_Resolve (Choice, Index_Type);
3680 end if;
3682 Next (Choice);
3683 end loop;
3685 declare
3686 Id : constant Entity_Id := Defining_Identifier (Assoc);
3687 Ent : constant Entity_Id :=
3688 New_Internal_Entity
3689 (E_Loop, Current_Scope, Sloc (Assoc), 'L');
3691 begin
3692 Set_Etype (Ent, Standard_Void_Type);
3693 Set_Parent (Ent, Assoc);
3694 Push_Scope (Ent);
3696 if No (Scope (Id)) then
3697 Set_Etype (Id, Index_Type);
3698 Mutate_Ekind (Id, E_Variable);
3699 Set_Is_Not_Self_Hidden (Id);
3700 Set_Scope (Id, Ent);
3701 end if;
3702 Enter_Name (Id);
3704 -- Resolve a copy of the expression, after setting
3705 -- its parent properly to preserve its context.
3707 Expr := New_Copy_Tree (Expression (Assoc));
3708 Set_Parent (Expr, Assoc);
3709 Analyze_And_Resolve (Expr, Component_Type (Typ));
3710 End_Scope;
3711 end;
3713 else
3714 Choice := First (Choice_List (Assoc));
3715 while Present (Choice) loop
3716 Analyze (Choice);
3718 if Nkind (Choice) = N_Others_Choice then
3719 Error_Msg_N
3720 ("OTHERS not allowed in delta aggregate", Choice);
3722 elsif Is_Entity_Name (Choice)
3723 and then Is_Type (Entity (Choice))
3724 then
3725 -- Choice covers a range of values
3727 if Base_Type (Entity (Choice)) /=
3728 Base_Type (Index_Type)
3729 then
3730 Error_Msg_NE
3731 ("choice does not match index type of &",
3732 Choice, Typ);
3733 end if;
3735 elsif Nkind (Choice) = N_Subtype_Indication then
3736 Resolve_Discrete_Subtype_Indication
3737 (Choice, Base_Type (Index_Type));
3739 else
3740 Resolve (Choice, Index_Type);
3741 end if;
3743 Next (Choice);
3744 end loop;
3746 -- For an array_delta_aggregate, the array_component_association
3747 -- shall not use the box symbol <>; RM 4.3.4(11/5).
3749 pragma Assert
3750 (Box_Present (Assoc) xor Present (Expression (Assoc)));
3752 if Box_Present (Assoc) then
3753 Error_Msg_N
3754 ("'<'> in array delta aggregate is not allowed", Assoc);
3755 else
3756 Analyze_And_Resolve (Expression (Assoc), Component_Type (Typ));
3757 end if;
3758 end if;
3760 Next (Assoc);
3761 end loop;
3762 end Resolve_Delta_Array_Aggregate;
3764 ------------------------------------
3765 -- Resolve_Delta_Record_Aggregate --
3766 ------------------------------------
3768 procedure Resolve_Delta_Record_Aggregate (N : Node_Id; Typ : Entity_Id) is
3770 -- Variables used to verify that discriminant-dependent components
3771 -- appear in the same variant.
3773 Comp_Ref : Entity_Id := Empty; -- init to avoid warning
3774 Variant : Node_Id;
3776 procedure Check_Variant (Id : Entity_Id);
3777 -- If a given component of the delta aggregate appears in a variant
3778 -- part, verify that it is within the same variant as that of previous
3779 -- specified variant components of the delta.
3781 function Get_Component (Nam : Node_Id) return Entity_Id;
3782 -- Locate component with a given name and return it. If none found then
3783 -- report error and return Empty.
3785 function Nested_In (V1 : Node_Id; V2 : Node_Id) return Boolean;
3786 -- Determine whether variant V1 is within variant V2
3788 function Variant_Depth (N : Node_Id) return Natural;
3789 -- Determine the distance of a variant to the enclosing type declaration
3791 --------------------
3792 -- Check_Variant --
3793 --------------------
3795 procedure Check_Variant (Id : Entity_Id) is
3796 Comp : Entity_Id;
3797 Comp_Variant : Node_Id;
3799 begin
3800 if not Has_Discriminants (Typ) then
3801 return;
3802 end if;
3804 Comp := First_Entity (Typ);
3805 while Present (Comp) loop
3806 exit when Chars (Comp) = Chars (Id);
3807 Next_Component (Comp);
3808 end loop;
3810 -- Find the variant, if any, whose component list includes the
3811 -- component declaration.
3813 Comp_Variant := Parent (Parent (List_Containing (Parent (Comp))));
3814 if Nkind (Comp_Variant) = N_Variant then
3815 if No (Variant) then
3816 Variant := Comp_Variant;
3817 Comp_Ref := Comp;
3819 elsif Variant /= Comp_Variant then
3820 declare
3821 D1 : constant Integer := Variant_Depth (Variant);
3822 D2 : constant Integer := Variant_Depth (Comp_Variant);
3824 begin
3825 if D1 = D2
3826 or else
3827 (D1 > D2 and then not Nested_In (Variant, Comp_Variant))
3828 or else
3829 (D2 > D1 and then not Nested_In (Comp_Variant, Variant))
3830 then
3831 pragma Assert (Present (Comp_Ref));
3832 Error_Msg_Node_2 := Comp_Ref;
3833 Error_Msg_NE
3834 ("& and & appear in different variants", Id, Comp);
3836 -- Otherwise retain the deeper variant for subsequent tests
3838 elsif D2 > D1 then
3839 Variant := Comp_Variant;
3840 end if;
3841 end;
3842 end if;
3843 end if;
3844 end Check_Variant;
3846 -------------------
3847 -- Get_Component --
3848 -------------------
3850 function Get_Component (Nam : Node_Id) return Entity_Id is
3851 Comp : Entity_Id;
3853 begin
3854 Comp := First_Entity (Typ);
3855 while Present (Comp) loop
3856 if Chars (Comp) = Chars (Nam) then
3857 if Ekind (Comp) = E_Discriminant then
3858 Error_Msg_N ("delta cannot apply to discriminant", Nam);
3859 end if;
3861 return Comp;
3862 end if;
3864 Next_Entity (Comp);
3865 end loop;
3867 Error_Msg_NE ("type& has no component with this name", Nam, Typ);
3868 return Empty;
3869 end Get_Component;
3871 ---------------
3872 -- Nested_In --
3873 ---------------
3875 function Nested_In (V1, V2 : Node_Id) return Boolean is
3876 Par : Node_Id;
3878 begin
3879 Par := Parent (V1);
3880 while Nkind (Par) /= N_Full_Type_Declaration loop
3881 if Par = V2 then
3882 return True;
3883 end if;
3885 Par := Parent (Par);
3886 end loop;
3888 return False;
3889 end Nested_In;
3891 -------------------
3892 -- Variant_Depth --
3893 -------------------
3895 function Variant_Depth (N : Node_Id) return Natural is
3896 Depth : Natural;
3897 Par : Node_Id;
3899 begin
3900 Depth := 0;
3901 Par := Parent (N);
3902 while Nkind (Par) /= N_Full_Type_Declaration loop
3903 Depth := Depth + 1;
3904 Par := Parent (Par);
3905 end loop;
3907 return Depth;
3908 end Variant_Depth;
3910 -- Local variables
3912 Deltas : constant List_Id := Component_Associations (N);
3914 Assoc : Node_Id;
3915 Choice : Node_Id;
3916 Comp : Entity_Id;
3917 Comp_Type : Entity_Id := Empty; -- init to avoid warning
3919 -- Start of processing for Resolve_Delta_Record_Aggregate
3921 begin
3922 Variant := Empty;
3924 Assoc := First (Deltas);
3925 while Present (Assoc) loop
3926 Choice := First (Choice_List (Assoc));
3927 while Present (Choice) loop
3928 Comp := Get_Component (Choice);
3930 if Present (Comp) then
3931 Check_Variant (Choice);
3933 Comp_Type := Etype (Comp);
3935 -- Decorate the component reference by setting its entity and
3936 -- type, as otherwise backends like GNATprove would have to
3937 -- rediscover this information by themselves.
3939 Set_Entity (Choice, Comp);
3940 Set_Etype (Choice, Comp_Type);
3941 else
3942 Comp_Type := Any_Type;
3943 end if;
3945 Next (Choice);
3946 end loop;
3948 pragma Assert (Present (Comp_Type));
3950 -- A record_component_association in record_delta_aggregate shall not
3951 -- use the box compound delimiter <> rather than an expression; see
3952 -- RM 4.3.1(17.3/5).
3954 pragma Assert (Present (Expression (Assoc)) xor Box_Present (Assoc));
3956 if Box_Present (Assoc) then
3957 Error_Msg_N
3958 ("'<'> in record delta aggregate is not allowed", Assoc);
3959 else
3960 Analyze_And_Resolve (Expression (Assoc), Comp_Type);
3962 -- The expression must not be of a limited type; RM 4.3.1(17.4/5)
3964 if Is_Limited_Type (Etype (Expression (Assoc))) then
3965 Error_Msg_N
3966 ("expression of a limited type in record delta aggregate " &
3967 "is not allowed",
3968 Expression (Assoc));
3969 end if;
3970 end if;
3972 Next (Assoc);
3973 end loop;
3974 end Resolve_Delta_Record_Aggregate;
3976 ---------------------------------
3977 -- Resolve_Extension_Aggregate --
3978 ---------------------------------
3980 -- There are two cases to consider:
3982 -- a) If the ancestor part is a type mark, the components needed are the
3983 -- difference between the components of the expected type and the
3984 -- components of the given type mark.
3986 -- b) If the ancestor part is an expression, it must be unambiguous, and
3987 -- once we have its type we can also compute the needed components as in
3988 -- the previous case. In both cases, if the ancestor type is not the
3989 -- immediate ancestor, we have to build this ancestor recursively.
3991 -- In both cases, discriminants of the ancestor type do not play a role in
3992 -- the resolution of the needed components, because inherited discriminants
3993 -- cannot be used in a type extension. As a result we can compute
3994 -- independently the list of components of the ancestor type and of the
3995 -- expected type.
3997 procedure Resolve_Extension_Aggregate (N : Node_Id; Typ : Entity_Id) is
3998 A : constant Node_Id := Ancestor_Part (N);
3999 A_Type : Entity_Id;
4000 I : Interp_Index;
4001 It : Interp;
4003 function Valid_Limited_Ancestor (Anc : Node_Id) return Boolean;
4004 -- If the type is limited, verify that the ancestor part is a legal
4005 -- expression (aggregate or function call, including 'Input)) that does
4006 -- not require a copy, as specified in 7.5(2).
4008 function Valid_Ancestor_Type return Boolean;
4009 -- Verify that the type of the ancestor part is a non-private ancestor
4010 -- of the expected type, which must be a type extension.
4012 procedure Transform_BIP_Assignment (Typ : Entity_Id);
4013 -- For an extension aggregate whose ancestor part is a build-in-place
4014 -- call returning a nonlimited type, this is used to transform the
4015 -- assignment to the ancestor part to use a temp.
4017 ----------------------------
4018 -- Valid_Limited_Ancestor --
4019 ----------------------------
4021 function Valid_Limited_Ancestor (Anc : Node_Id) return Boolean is
4022 begin
4023 if Is_Entity_Name (Anc) and then Is_Type (Entity (Anc)) then
4024 return True;
4026 -- The ancestor must be a call or an aggregate, but a call may
4027 -- have been expanded into a temporary, so check original node.
4029 elsif Nkind (Anc) in N_Aggregate
4030 | N_Extension_Aggregate
4031 | N_Function_Call
4032 then
4033 return True;
4035 elsif Nkind (Original_Node (Anc)) = N_Function_Call then
4036 return True;
4038 elsif Nkind (Anc) = N_Attribute_Reference
4039 and then Attribute_Name (Anc) = Name_Input
4040 then
4041 return True;
4043 elsif Nkind (Anc) = N_Qualified_Expression then
4044 return Valid_Limited_Ancestor (Expression (Anc));
4046 elsif Nkind (Anc) = N_Raise_Expression then
4047 return True;
4049 else
4050 return False;
4051 end if;
4052 end Valid_Limited_Ancestor;
4054 -------------------------
4055 -- Valid_Ancestor_Type --
4056 -------------------------
4058 function Valid_Ancestor_Type return Boolean is
4059 Imm_Type : Entity_Id;
4061 begin
4062 Imm_Type := Base_Type (Typ);
4063 while Is_Derived_Type (Imm_Type) loop
4064 if Etype (Imm_Type) = Base_Type (A_Type) then
4065 return True;
4067 -- The base type of the parent type may appear as a private
4068 -- extension if it is declared as such in a parent unit of the
4069 -- current one. For consistency of the subsequent analysis use
4070 -- the partial view for the ancestor part.
4072 elsif Is_Private_Type (Etype (Imm_Type))
4073 and then Present (Full_View (Etype (Imm_Type)))
4074 and then Base_Type (A_Type) = Full_View (Etype (Imm_Type))
4075 then
4076 A_Type := Etype (Imm_Type);
4077 return True;
4079 -- The parent type may be a private extension. The aggregate is
4080 -- legal if the type of the aggregate is an extension of it that
4081 -- is not a private extension.
4083 elsif Is_Private_Type (A_Type)
4084 and then not Is_Private_Type (Imm_Type)
4085 and then Present (Full_View (A_Type))
4086 and then Base_Type (Full_View (A_Type)) = Etype (Imm_Type)
4087 then
4088 return True;
4090 -- The parent type may be a raise expression (which is legal in
4091 -- any expression context).
4093 elsif A_Type = Raise_Type then
4094 A_Type := Etype (Imm_Type);
4095 return True;
4097 else
4098 Imm_Type := Etype (Base_Type (Imm_Type));
4099 end if;
4100 end loop;
4102 -- If previous loop did not find a proper ancestor, report error
4104 Error_Msg_NE ("expect ancestor type of &", A, Typ);
4105 return False;
4106 end Valid_Ancestor_Type;
4108 ------------------------------
4109 -- Transform_BIP_Assignment --
4110 ------------------------------
4112 procedure Transform_BIP_Assignment (Typ : Entity_Id) is
4113 Loc : constant Source_Ptr := Sloc (N);
4114 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'Y', A);
4115 Obj_Decl : constant Node_Id :=
4116 Make_Object_Declaration (Loc,
4117 Defining_Identifier => Def_Id,
4118 Constant_Present => True,
4119 Object_Definition => New_Occurrence_Of (Typ, Loc),
4120 Expression => A,
4121 Has_Init_Expression => True);
4122 begin
4123 Set_Etype (Def_Id, Typ);
4124 Set_Ancestor_Part (N, New_Occurrence_Of (Def_Id, Loc));
4125 Insert_Action (N, Obj_Decl);
4126 end Transform_BIP_Assignment;
4128 -- Start of processing for Resolve_Extension_Aggregate
4130 begin
4131 -- Analyze the ancestor part and account for the case where it is a
4132 -- parameterless function call.
4134 Analyze (A);
4135 Check_Parameterless_Call (A);
4137 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
4139 -- AI05-0115: If the ancestor part is a subtype mark, the ancestor
4140 -- must not have unknown discriminants. To catch cases where the
4141 -- aggregate occurs at a place where the full view of the ancestor
4142 -- type is visible and doesn't have unknown discriminants, but the
4143 -- aggregate type was derived from a partial view that has unknown
4144 -- discriminants, we check whether the aggregate type has unknown
4145 -- discriminants (unknown discriminants were inherited), along
4146 -- with checking that the partial view of the ancestor has unknown
4147 -- discriminants. (It might be sufficient to replace the entire
4148 -- condition with Has_Unknown_Discriminants (Typ), but that might
4149 -- miss some cases, not clear, and causes error changes in some tests
4150 -- such as class-wide cases, that aren't clearly improvements. ???)
4152 if Has_Unknown_Discriminants (Entity (A))
4153 or else (Has_Unknown_Discriminants (Typ)
4154 and then Partial_View_Has_Unknown_Discr (Entity (A)))
4155 then
4156 Error_Msg_NE
4157 ("aggregate not available for type& whose ancestor "
4158 & "has unknown discriminants", N, Typ);
4159 end if;
4160 end if;
4162 if not Is_Tagged_Type (Typ) then
4163 Error_Msg_N ("type of extension aggregate must be tagged", N);
4164 return;
4166 elsif Is_Limited_Type (Typ) then
4168 -- Ada 2005 (AI-287): Limited aggregates are allowed
4170 if Ada_Version < Ada_2005 then
4171 Error_Msg_N ("aggregate type cannot be limited", N);
4172 Explain_Limited_Type (Typ, N);
4173 return;
4175 elsif Valid_Limited_Ancestor (A) then
4176 null;
4178 else
4179 Error_Msg_N
4180 ("limited ancestor part must be aggregate or function call", A);
4181 end if;
4183 elsif Is_Class_Wide_Type (Typ) then
4184 Error_Msg_N ("aggregate cannot be of a class-wide type", N);
4185 return;
4186 end if;
4188 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
4189 A_Type := Get_Full_View (Entity (A));
4191 if Valid_Ancestor_Type then
4192 Set_Entity (A, A_Type);
4193 Set_Etype (A, A_Type);
4195 Validate_Ancestor_Part (N);
4196 Resolve_Record_Aggregate (N, Typ);
4197 end if;
4199 elsif Nkind (A) /= N_Aggregate then
4200 if Is_Overloaded (A) then
4201 A_Type := Any_Type;
4203 Get_First_Interp (A, I, It);
4204 while Present (It.Typ) loop
4206 -- Consider limited interpretations if Ada 2005 or higher
4208 if Is_Tagged_Type (It.Typ)
4209 and then (Ada_Version >= Ada_2005
4210 or else not Is_Limited_Type (It.Typ))
4211 then
4212 if A_Type /= Any_Type then
4213 Error_Msg_N ("cannot resolve expression", A);
4214 return;
4215 else
4216 A_Type := It.Typ;
4217 end if;
4218 end if;
4220 Get_Next_Interp (I, It);
4221 end loop;
4223 if A_Type = Any_Type then
4224 if Ada_Version >= Ada_2005 then
4225 Error_Msg_N
4226 ("ancestor part must be of a tagged type", A);
4227 else
4228 Error_Msg_N
4229 ("ancestor part must be of a nonlimited tagged type", A);
4230 end if;
4232 return;
4233 end if;
4235 else
4236 A_Type := Etype (A);
4237 end if;
4239 if Valid_Ancestor_Type then
4240 Resolve (A, A_Type);
4241 Check_Unset_Reference (A);
4242 Check_Non_Static_Context (A);
4244 -- The aggregate is illegal if the ancestor expression is a call
4245 -- to a function with a limited unconstrained result, unless the
4246 -- type of the aggregate is a null extension. This restriction
4247 -- was added in AI05-67 to simplify implementation.
4249 if Nkind (A) = N_Function_Call
4250 and then Is_Limited_Type (A_Type)
4251 and then not Is_Null_Extension (Typ)
4252 and then not Is_Constrained (A_Type)
4253 then
4254 Error_Msg_N
4255 ("type of limited ancestor part must be constrained", A);
4257 -- Reject the use of CPP constructors that leave objects partially
4258 -- initialized. For example:
4260 -- type CPP_Root is tagged limited record ...
4261 -- pragma Import (CPP, CPP_Root);
4263 -- type CPP_DT is new CPP_Root and Iface ...
4264 -- pragma Import (CPP, CPP_DT);
4266 -- type Ada_DT is new CPP_DT with ...
4268 -- Obj : Ada_DT := Ada_DT'(New_CPP_Root with others => <>);
4270 -- Using the constructor of CPP_Root the slots of the dispatch
4271 -- table of CPP_DT cannot be set, and the secondary tag of
4272 -- CPP_DT is unknown.
4274 elsif Nkind (A) = N_Function_Call
4275 and then Is_CPP_Constructor_Call (A)
4276 and then Enclosing_CPP_Parent (Typ) /= A_Type
4277 then
4278 Error_Msg_NE
4279 ("??must use 'C'P'P constructor for type &", A,
4280 Enclosing_CPP_Parent (Typ));
4282 -- The following call is not needed if the previous warning
4283 -- is promoted to an error.
4285 Resolve_Record_Aggregate (N, Typ);
4287 elsif Is_Class_Wide_Type (Etype (A))
4288 and then Nkind (Original_Node (A)) = N_Function_Call
4289 then
4290 -- If the ancestor part is a dispatching call, it appears
4291 -- statically to be a legal ancestor, but it yields any member
4292 -- of the class, and it is not possible to determine whether
4293 -- it is an ancestor of the extension aggregate (much less
4294 -- which ancestor). It is not possible to determine the
4295 -- components of the extension part.
4297 -- This check implements AI-306, which in fact was motivated by
4298 -- an AdaCore query to the ARG after this test was added.
4300 Error_Msg_N ("ancestor part must be statically tagged", A);
4301 else
4302 -- We are using the build-in-place protocol, but we can't build
4303 -- in place, because we need to call the function before
4304 -- allocating the aggregate. Could do better for null
4305 -- extensions, and maybe for nondiscriminated types.
4306 -- This is wrong for limited, but those were wrong already.
4308 if not Is_Limited_View (A_Type)
4309 and then Is_Build_In_Place_Function_Call (A)
4310 then
4311 Transform_BIP_Assignment (A_Type);
4312 end if;
4314 Resolve_Record_Aggregate (N, Typ);
4315 end if;
4316 end if;
4318 else
4319 Error_Msg_N ("no unique type for this aggregate", A);
4320 end if;
4322 Check_Function_Writable_Actuals (N);
4323 end Resolve_Extension_Aggregate;
4325 ----------------------------------
4326 -- Resolve_Null_Array_Aggregate --
4327 ----------------------------------
4329 function Resolve_Null_Array_Aggregate (N : Node_Id) return Boolean is
4330 -- Never returns False, but declared as a function to match
4331 -- other Resolve_Mumble functions.
4333 Loc : constant Source_Ptr := Sloc (N);
4334 Typ : constant Entity_Id := Etype (N);
4336 Index : Node_Id;
4337 Lo, Hi : Node_Id;
4338 Constr : constant List_Id := New_List;
4340 begin
4341 -- Attach the list of constraints at the location of the aggregate, so
4342 -- the individual constraints can be analyzed.
4344 Set_Parent (Constr, N);
4346 -- Create a constrained subtype with null dimensions
4348 Index := First_Index (Typ);
4349 while Present (Index) loop
4350 Get_Index_Bounds (Index, L => Lo, H => Hi);
4352 -- The upper bound is the predecessor of the lower bound
4354 Hi := Make_Attribute_Reference
4355 (Loc,
4356 Prefix => New_Occurrence_Of (Etype (Index), Loc),
4357 Attribute_Name => Name_Pred,
4358 Expressions => New_List (New_Copy_Tree (Lo)));
4360 Append (Make_Range (Loc, New_Copy_Tree (Lo), Hi), Constr);
4361 Analyze_And_Resolve (Last (Constr), Etype (Index));
4363 Next_Index (Index);
4364 end loop;
4366 Set_Compile_Time_Known_Aggregate (N);
4367 Set_Aggregate_Bounds (N, First (Constr));
4369 return True;
4370 end Resolve_Null_Array_Aggregate;
4372 ------------------------------
4373 -- Resolve_Record_Aggregate --
4374 ------------------------------
4376 procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id) is
4377 New_Assoc_List : constant List_Id := New_List;
4378 -- New_Assoc_List is the newly built list of N_Component_Association
4379 -- nodes.
4381 Others_Etype : Entity_Id := Empty;
4382 -- This variable is used to save the Etype of the last record component
4383 -- that takes its value from the others choice. Its purpose is:
4385 -- (a) make sure the others choice is useful
4387 -- (b) make sure the type of all the components whose value is
4388 -- subsumed by the others choice are the same.
4390 -- This variable is updated as a side effect of function Get_Value.
4392 Box_Node : Node_Id := Empty;
4393 Is_Box_Present : Boolean := False;
4394 Is_Box_Init_By_Default : Boolean := False;
4395 Others_Box : Natural := 0;
4396 -- Ada 2005 (AI-287): Variables used in case of default initialization
4397 -- to provide a functionality similar to Others_Etype. Box_Present
4398 -- indicates that the component takes its default initialization;
4399 -- Others_Box counts the number of components of the current aggregate
4400 -- (which may be a sub-aggregate of a larger one) that are default-
4401 -- initialized. A value of One indicates that an others_box is present.
4402 -- Any larger value indicates that the others_box is not redundant.
4403 -- These variables, similar to Others_Etype, are also updated as a side
4404 -- effect of function Get_Value. Box_Node is used to place a warning on
4405 -- a redundant others_box.
4407 procedure Add_Association
4408 (Component : Entity_Id;
4409 Expr : Node_Id;
4410 Assoc_List : List_Id;
4411 Is_Box_Present : Boolean := False);
4412 -- Builds a new N_Component_Association node which associates Component
4413 -- to expression Expr and adds it to the association list being built,
4414 -- either New_Assoc_List, or the association being built for an inner
4415 -- aggregate.
4417 procedure Add_Discriminant_Values
4418 (New_Aggr : Node_Id;
4419 Assoc_List : List_Id);
4420 -- The constraint to a component may be given by a discriminant of the
4421 -- enclosing type, in which case we have to retrieve its value, which is
4422 -- part of the enclosing aggregate. Assoc_List provides the discriminant
4423 -- associations of the current type or of some enclosing record.
4425 function Discriminant_Present (Input_Discr : Entity_Id) return Boolean;
4426 -- If aggregate N is a regular aggregate this routine will return True.
4427 -- Otherwise, if N is an extension aggregate, then Input_Discr denotes
4428 -- a discriminant whose value may already have been specified by N's
4429 -- ancestor part. This routine checks whether this is indeed the case
4430 -- and if so returns False, signaling that no value for Input_Discr
4431 -- should appear in N's aggregate part. Also, in this case, the routine
4432 -- appends to New_Assoc_List the discriminant value specified in the
4433 -- ancestor part.
4435 -- If the aggregate is in a context with expansion delayed, it will be
4436 -- reanalyzed. The inherited discriminant values must not be reinserted
4437 -- in the component list to prevent spurious errors, but they must be
4438 -- present on first analysis to build the proper subtype indications.
4439 -- The flag Inherited_Discriminant is used to prevent the re-insertion.
4441 function Find_Private_Ancestor (Typ : Entity_Id) return Entity_Id;
4442 -- AI05-0115: Find earlier ancestor in the derivation chain that is
4443 -- derived from private view Typ. Whether the aggregate is legal depends
4444 -- on the current visibility of the type as well as that of the parent
4445 -- of the ancestor.
4447 function Get_Value
4448 (Compon : Entity_Id;
4449 From : List_Id;
4450 Consider_Others_Choice : Boolean := False) return Node_Id;
4451 -- Given a record component stored in parameter Compon, this function
4452 -- returns its value as it appears in the list From, which is a list
4453 -- of N_Component_Association nodes.
4455 -- If no component association has a choice for the searched component,
4456 -- the value provided by the others choice is returned, if there is one,
4457 -- and Consider_Others_Choice is set to true. Otherwise Empty is
4458 -- returned. If there is more than one component association giving a
4459 -- value for the searched record component, an error message is emitted
4460 -- and the first found value is returned.
4462 -- If Consider_Others_Choice is set and the returned expression comes
4463 -- from the others choice, then Others_Etype is set as a side effect.
4464 -- An error message is emitted if the components taking their value from
4465 -- the others choice do not have same type.
4467 procedure Propagate_Discriminants
4468 (Aggr : Node_Id;
4469 Assoc_List : List_Id);
4470 -- Nested components may themselves be discriminated types constrained
4471 -- by outer discriminants, whose values must be captured before the
4472 -- aggregate is expanded into assignments.
4474 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Entity_Id);
4475 -- Analyzes and resolves expression Expr against the Etype of the
4476 -- Component. This routine also applies all appropriate checks to Expr.
4477 -- It finally saves a Expr in the newly created association list that
4478 -- will be attached to the final record aggregate. Note that if the
4479 -- Parent pointer of Expr is not set then Expr was produced with a
4480 -- New_Copy_Tree or some such.
4482 procedure Rewrite_Range (Root_Type : Entity_Id; Rge : Node_Id);
4483 -- Rewrite a range node Rge when its bounds refer to non-stored
4484 -- discriminants from Root_Type, to replace them with the stored
4485 -- discriminant values. This is required in GNATprove mode, and is
4486 -- adopted in all modes to avoid special-casing GNATprove mode.
4488 ---------------------
4489 -- Add_Association --
4490 ---------------------
4492 procedure Add_Association
4493 (Component : Entity_Id;
4494 Expr : Node_Id;
4495 Assoc_List : List_Id;
4496 Is_Box_Present : Boolean := False)
4498 Choice_List : constant List_Id := New_List;
4499 Loc : Source_Ptr;
4501 begin
4502 -- If this is a box association the expression is missing, so use the
4503 -- Sloc of the aggregate itself for the new association.
4505 pragma Assert (Present (Expr) xor Is_Box_Present);
4507 if Present (Expr) then
4508 Loc := Sloc (Expr);
4509 else
4510 Loc := Sloc (N);
4511 end if;
4513 Append_To (Choice_List, New_Occurrence_Of (Component, Loc));
4515 Append_To (Assoc_List,
4516 Make_Component_Association (Loc,
4517 Choices => Choice_List,
4518 Expression => Expr,
4519 Box_Present => Is_Box_Present));
4521 -- If this association has a box for a component that is initialized
4522 -- by default, then set flag on the new association to indicate that
4523 -- the original association was for such a box-initialized component.
4525 if Is_Box_Init_By_Default then
4526 Set_Was_Default_Init_Box_Association (Last (Assoc_List));
4527 end if;
4528 end Add_Association;
4530 -----------------------------
4531 -- Add_Discriminant_Values --
4532 -----------------------------
4534 procedure Add_Discriminant_Values
4535 (New_Aggr : Node_Id;
4536 Assoc_List : List_Id)
4538 Assoc : Node_Id;
4539 Discr : Entity_Id;
4540 Discr_Elmt : Elmt_Id;
4541 Discr_Val : Node_Id;
4542 Val : Entity_Id;
4544 begin
4545 Discr := First_Discriminant (Etype (New_Aggr));
4546 Discr_Elmt := First_Elmt (Discriminant_Constraint (Etype (New_Aggr)));
4547 while Present (Discr_Elmt) loop
4548 Discr_Val := Node (Discr_Elmt);
4550 -- If the constraint is given by a discriminant then it is a
4551 -- discriminant of an enclosing record, and its value has already
4552 -- been placed in the association list.
4554 if Is_Entity_Name (Discr_Val)
4555 and then Ekind (Entity (Discr_Val)) = E_Discriminant
4556 then
4557 Val := Entity (Discr_Val);
4559 Assoc := First (Assoc_List);
4560 while Present (Assoc) loop
4561 if Present (Entity (First (Choices (Assoc))))
4562 and then Entity (First (Choices (Assoc))) = Val
4563 then
4564 Discr_Val := Expression (Assoc);
4565 exit;
4566 end if;
4568 Next (Assoc);
4569 end loop;
4570 end if;
4572 Add_Association
4573 (Discr, New_Copy_Tree (Discr_Val),
4574 Component_Associations (New_Aggr));
4576 -- If the discriminant constraint is a current instance, mark the
4577 -- current aggregate so that the self-reference can be expanded by
4578 -- Build_Record_Aggr_Code.Replace_Type later.
4580 if Nkind (Discr_Val) = N_Attribute_Reference
4581 and then Is_Entity_Name (Prefix (Discr_Val))
4582 and then Is_Type (Entity (Prefix (Discr_Val)))
4583 and then
4584 Is_Ancestor
4585 (Entity (Prefix (Discr_Val)),
4586 Etype (N),
4587 Use_Full_View => True)
4588 then
4589 Set_Has_Self_Reference (N);
4590 end if;
4592 Next_Elmt (Discr_Elmt);
4593 Next_Discriminant (Discr);
4594 end loop;
4595 end Add_Discriminant_Values;
4597 --------------------------
4598 -- Discriminant_Present --
4599 --------------------------
4601 function Discriminant_Present (Input_Discr : Entity_Id) return Boolean is
4602 Regular_Aggr : constant Boolean := Nkind (N) /= N_Extension_Aggregate;
4604 Ancestor_Is_Subtyp : Boolean;
4606 Loc : Source_Ptr;
4608 Ancestor : Node_Id;
4609 Ancestor_Typ : Entity_Id;
4610 Comp_Assoc : Node_Id;
4611 Discr : Entity_Id;
4612 Discr_Expr : Node_Id;
4613 Discr_Val : Elmt_Id := No_Elmt;
4614 Orig_Discr : Entity_Id;
4616 begin
4617 if Regular_Aggr then
4618 return True;
4619 end if;
4621 -- Check whether inherited discriminant values have already been
4622 -- inserted in the aggregate. This will be the case if we are
4623 -- re-analyzing an aggregate whose expansion was delayed.
4625 if Present (Component_Associations (N)) then
4626 Comp_Assoc := First (Component_Associations (N));
4627 while Present (Comp_Assoc) loop
4628 if Inherited_Discriminant (Comp_Assoc) then
4629 return True;
4630 end if;
4632 Next (Comp_Assoc);
4633 end loop;
4634 end if;
4636 Ancestor := Ancestor_Part (N);
4637 Ancestor_Typ := Etype (Ancestor);
4638 Loc := Sloc (Ancestor);
4640 -- For a private type with unknown discriminants, use the underlying
4641 -- record view if it is available.
4643 if Has_Unknown_Discriminants (Ancestor_Typ)
4644 and then Present (Full_View (Ancestor_Typ))
4645 and then Present (Underlying_Record_View (Full_View (Ancestor_Typ)))
4646 then
4647 Ancestor_Typ := Underlying_Record_View (Full_View (Ancestor_Typ));
4648 end if;
4650 Ancestor_Is_Subtyp :=
4651 Is_Entity_Name (Ancestor) and then Is_Type (Entity (Ancestor));
4653 -- If the ancestor part has no discriminants clearly N's aggregate
4654 -- part must provide a value for Discr.
4656 if not Has_Discriminants (Ancestor_Typ) then
4657 return True;
4659 -- If the ancestor part is an unconstrained subtype mark then the
4660 -- Discr must be present in N's aggregate part.
4662 elsif Ancestor_Is_Subtyp
4663 and then not Is_Constrained (Entity (Ancestor))
4664 then
4665 return True;
4666 end if;
4668 -- Now look to see if Discr was specified in the ancestor part
4670 if Ancestor_Is_Subtyp then
4671 Discr_Val :=
4672 First_Elmt (Discriminant_Constraint (Entity (Ancestor)));
4673 end if;
4675 Orig_Discr := Original_Record_Component (Input_Discr);
4677 Discr := First_Discriminant (Ancestor_Typ);
4678 while Present (Discr) loop
4680 -- If Ancestor has already specified Disc value then insert its
4681 -- value in the final aggregate.
4683 if Original_Record_Component (Discr) = Orig_Discr then
4684 if Ancestor_Is_Subtyp then
4685 Discr_Expr := New_Copy_Tree (Node (Discr_Val));
4686 else
4687 Discr_Expr :=
4688 Make_Selected_Component (Loc,
4689 Prefix => Duplicate_Subexpr (Ancestor),
4690 Selector_Name => New_Occurrence_Of (Input_Discr, Loc));
4691 end if;
4693 Resolve_Aggr_Expr (Discr_Expr, Input_Discr);
4694 Set_Inherited_Discriminant (Last (New_Assoc_List));
4695 return False;
4696 end if;
4698 Next_Discriminant (Discr);
4700 if Ancestor_Is_Subtyp then
4701 Next_Elmt (Discr_Val);
4702 end if;
4703 end loop;
4705 return True;
4706 end Discriminant_Present;
4708 ---------------------------
4709 -- Find_Private_Ancestor --
4710 ---------------------------
4712 function Find_Private_Ancestor (Typ : Entity_Id) return Entity_Id is
4713 Par : Entity_Id;
4715 begin
4716 Par := Typ;
4717 loop
4718 if Has_Private_Ancestor (Par)
4719 and then not Has_Private_Ancestor (Etype (Base_Type (Par)))
4720 then
4721 return Par;
4723 elsif not Is_Derived_Type (Par) then
4724 return Empty;
4726 else
4727 Par := Etype (Base_Type (Par));
4728 end if;
4729 end loop;
4730 end Find_Private_Ancestor;
4732 ---------------
4733 -- Get_Value --
4734 ---------------
4736 function Get_Value
4737 (Compon : Entity_Id;
4738 From : List_Id;
4739 Consider_Others_Choice : Boolean := False) return Node_Id
4741 Typ : constant Entity_Id := Etype (Compon);
4742 Assoc : Node_Id;
4743 Expr : Node_Id := Empty;
4744 Selector_Name : Node_Id;
4746 begin
4747 Is_Box_Present := False;
4748 Is_Box_Init_By_Default := False;
4750 if No (From) then
4751 return Empty;
4752 end if;
4754 Assoc := First (From);
4755 while Present (Assoc) loop
4756 Selector_Name := First (Choices (Assoc));
4757 while Present (Selector_Name) loop
4758 if Nkind (Selector_Name) = N_Others_Choice then
4759 if Consider_Others_Choice and then No (Expr) then
4761 -- We need to duplicate the expression for each
4762 -- successive component covered by the others choice.
4763 -- This is redundant if the others_choice covers only
4764 -- one component (small optimization possible???), but
4765 -- indispensable otherwise, because each one must be
4766 -- expanded individually to preserve side effects.
4768 -- Ada 2005 (AI-287): In case of default initialization
4769 -- of components, we duplicate the corresponding default
4770 -- expression (from the record type declaration). The
4771 -- copy must carry the sloc of the association (not the
4772 -- original expression) to prevent spurious elaboration
4773 -- checks when the default includes function calls.
4775 if Box_Present (Assoc) then
4776 Others_Box := Others_Box + 1;
4777 Is_Box_Present := True;
4779 if Expander_Active then
4780 return
4781 New_Copy_Tree_And_Copy_Dimensions
4782 (Expression (Parent (Compon)),
4783 New_Sloc => Sloc (Assoc));
4784 else
4785 return Expression (Parent (Compon));
4786 end if;
4788 else
4789 if Present (Others_Etype)
4790 and then Base_Type (Others_Etype) /= Base_Type (Typ)
4791 then
4792 -- If the components are of an anonymous access
4793 -- type they are distinct, but this is legal in
4794 -- Ada 2012 as long as designated types match.
4796 if (Ekind (Typ) = E_Anonymous_Access_Type
4797 or else Ekind (Typ) =
4798 E_Anonymous_Access_Subprogram_Type)
4799 and then Designated_Type (Typ) =
4800 Designated_Type (Others_Etype)
4801 then
4802 null;
4803 else
4804 Error_Msg_N
4805 ("components in OTHERS choice must have same "
4806 & "type", Selector_Name);
4807 end if;
4808 end if;
4810 Others_Etype := Typ;
4812 -- Copy the expression so that it is resolved
4813 -- independently for each component, This is needed
4814 -- for accessibility checks on components of anonymous
4815 -- access types, even in compile_only mode.
4817 if not Inside_A_Generic then
4818 return
4819 New_Copy_Tree_And_Copy_Dimensions
4820 (Expression (Assoc));
4821 else
4822 return Expression (Assoc);
4823 end if;
4824 end if;
4825 end if;
4827 elsif Chars (Compon) = Chars (Selector_Name) then
4828 if No (Expr) then
4830 -- Ada 2005 (AI-231)
4832 if Ada_Version >= Ada_2005
4833 and then Known_Null (Expression (Assoc))
4834 then
4835 Check_Can_Never_Be_Null (Compon, Expression (Assoc));
4836 end if;
4838 -- We need to duplicate the expression when several
4839 -- components are grouped together with a "|" choice.
4840 -- For instance "filed1 | filed2 => Expr"
4842 -- Ada 2005 (AI-287)
4844 if Box_Present (Assoc) then
4845 Is_Box_Present := True;
4847 -- Duplicate the default expression of the component
4848 -- from the record type declaration, so a new copy
4849 -- can be attached to the association.
4851 -- Note that we always copy the default expression,
4852 -- even when the association has a single choice, in
4853 -- order to create a proper association for the
4854 -- expanded aggregate.
4856 -- Component may have no default, in which case the
4857 -- expression is empty and the component is default-
4858 -- initialized, but an association for the component
4859 -- exists, and it is not covered by an others clause.
4861 -- Scalar and private types have no initialization
4862 -- procedure, so they remain uninitialized. If the
4863 -- target of the aggregate is a constant this
4864 -- deserves a warning.
4866 if No (Expression (Parent (Compon)))
4867 and then not Has_Non_Null_Base_Init_Proc (Typ)
4868 and then not Has_Aspect (Typ, Aspect_Default_Value)
4869 and then not Is_Concurrent_Type (Typ)
4870 and then Nkind (Parent (N)) = N_Object_Declaration
4871 and then Constant_Present (Parent (N))
4872 then
4873 Error_Msg_Node_2 := Typ;
4874 Error_Msg_NE
4875 ("??component& of type& is uninitialized",
4876 Assoc, Selector_Name);
4878 -- An additional reminder if the component type
4879 -- is a generic formal.
4881 if Is_Generic_Type (Base_Type (Typ)) then
4882 Error_Msg_NE
4883 ("\instance should provide actual type with "
4884 & "initialization for&", Assoc, Typ);
4885 end if;
4886 end if;
4888 return
4889 New_Copy_Tree_And_Copy_Dimensions
4890 (Expression (Parent (Compon)));
4892 else
4893 if Present (Next (Selector_Name)) then
4894 Expr := New_Copy_Tree_And_Copy_Dimensions
4895 (Expression (Assoc));
4896 else
4897 Expr := Expression (Assoc);
4898 end if;
4899 end if;
4901 Generate_Reference (Compon, Selector_Name, 'm');
4903 else
4904 Error_Msg_NE
4905 ("more than one value supplied for &",
4906 Selector_Name, Compon);
4908 end if;
4909 end if;
4911 Next (Selector_Name);
4912 end loop;
4914 Next (Assoc);
4915 end loop;
4917 return Expr;
4918 end Get_Value;
4920 -----------------------------
4921 -- Propagate_Discriminants --
4922 -----------------------------
4924 procedure Propagate_Discriminants
4925 (Aggr : Node_Id;
4926 Assoc_List : List_Id)
4928 Loc : constant Source_Ptr := Sloc (N);
4930 procedure Process_Component (Comp : Entity_Id);
4931 -- Add one component with a box association to the inner aggregate,
4932 -- and recurse if component is itself composite.
4934 -----------------------
4935 -- Process_Component --
4936 -----------------------
4938 procedure Process_Component (Comp : Entity_Id) is
4939 T : constant Entity_Id := Etype (Comp);
4940 New_Aggr : Node_Id;
4942 begin
4943 if Is_Record_Type (T) and then Has_Discriminants (T) then
4944 New_Aggr := Make_Aggregate (Loc, No_List, New_List);
4945 Set_Etype (New_Aggr, T);
4947 Add_Association
4948 (Comp, New_Aggr, Component_Associations (Aggr));
4950 -- Collect discriminant values and recurse
4952 Add_Discriminant_Values (New_Aggr, Assoc_List);
4953 Propagate_Discriminants (New_Aggr, Assoc_List);
4955 Build_Constrained_Itype
4956 (New_Aggr, T, Component_Associations (New_Aggr));
4957 else
4958 Add_Association
4959 (Comp, Empty, Component_Associations (Aggr),
4960 Is_Box_Present => True);
4961 end if;
4962 end Process_Component;
4964 -- Local variables
4966 Aggr_Type : constant Entity_Id := Base_Type (Etype (Aggr));
4967 Components : constant Elist_Id := New_Elmt_List;
4968 Def_Node : constant Node_Id :=
4969 Type_Definition (Declaration_Node (Aggr_Type));
4971 Comp : Node_Id;
4972 Comp_Elmt : Elmt_Id;
4973 Errors : Boolean;
4975 -- Start of processing for Propagate_Discriminants
4977 begin
4978 -- The component type may be a variant type. Collect the components
4979 -- that are ruled by the known values of the discriminants. Their
4980 -- values have already been inserted into the component list of the
4981 -- current aggregate.
4983 if Nkind (Def_Node) = N_Record_Definition
4984 and then Present (Component_List (Def_Node))
4985 and then Present (Variant_Part (Component_List (Def_Node)))
4986 then
4987 Gather_Components (Aggr_Type,
4988 Component_List (Def_Node),
4989 Governed_By => Component_Associations (Aggr),
4990 Into => Components,
4991 Report_Errors => Errors);
4993 Comp_Elmt := First_Elmt (Components);
4994 while Present (Comp_Elmt) loop
4995 if Ekind (Node (Comp_Elmt)) /= E_Discriminant then
4996 Process_Component (Node (Comp_Elmt));
4997 end if;
4999 Next_Elmt (Comp_Elmt);
5000 end loop;
5002 -- No variant part, iterate over all components
5004 else
5005 Comp := First_Component (Etype (Aggr));
5006 while Present (Comp) loop
5007 Process_Component (Comp);
5008 Next_Component (Comp);
5009 end loop;
5010 end if;
5011 end Propagate_Discriminants;
5013 -----------------------
5014 -- Resolve_Aggr_Expr --
5015 -----------------------
5017 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Entity_Id) is
5018 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean;
5019 -- If the expression is an aggregate (possibly qualified) then its
5020 -- expansion is delayed until the enclosing aggregate is expanded
5021 -- into assignments. In that case, do not generate checks on the
5022 -- expression, because they will be generated later, and will other-
5023 -- wise force a copy (to remove side effects) that would leave a
5024 -- dynamic-sized aggregate in the code, something that gigi cannot
5025 -- handle.
5027 ---------------------------
5028 -- Has_Expansion_Delayed --
5029 ---------------------------
5031 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean is
5032 begin
5033 return
5034 (Nkind (Expr) in N_Aggregate | N_Extension_Aggregate
5035 and then Present (Etype (Expr))
5036 and then Is_Record_Type (Etype (Expr))
5037 and then Expansion_Delayed (Expr))
5038 or else
5039 (Nkind (Expr) = N_Qualified_Expression
5040 and then Has_Expansion_Delayed (Expression (Expr)));
5041 end Has_Expansion_Delayed;
5043 -- Local variables
5045 Expr_Type : Entity_Id := Empty;
5046 New_C : Entity_Id := Component;
5047 New_Expr : Node_Id;
5049 Relocate : Boolean;
5050 -- Set to True if the resolved Expr node needs to be relocated when
5051 -- attached to the newly created association list. This node need not
5052 -- be relocated if its parent pointer is not set. In fact in this
5053 -- case Expr is the output of a New_Copy_Tree call. If Relocate is
5054 -- True then we have analyzed the expression node in the original
5055 -- aggregate and hence it needs to be relocated when moved over to
5056 -- the new association list.
5058 -- Start of processing for Resolve_Aggr_Expr
5060 begin
5061 -- If the type of the component is elementary or the type of the
5062 -- aggregate does not contain discriminants, use the type of the
5063 -- component to resolve Expr.
5065 if Is_Elementary_Type (Etype (Component))
5066 or else not Has_Discriminants (Etype (N))
5067 then
5068 Expr_Type := Etype (Component);
5070 -- Otherwise we have to pick up the new type of the component from
5071 -- the new constrained subtype of the aggregate. In fact components
5072 -- which are of a composite type might be constrained by a
5073 -- discriminant, and we want to resolve Expr against the subtype were
5074 -- all discriminant occurrences are replaced with their actual value.
5076 else
5077 New_C := First_Component (Etype (N));
5078 while Present (New_C) loop
5079 if Chars (New_C) = Chars (Component) then
5080 Expr_Type := Etype (New_C);
5081 exit;
5082 end if;
5084 Next_Component (New_C);
5085 end loop;
5087 pragma Assert (Present (Expr_Type));
5089 -- For each range in an array type where a discriminant has been
5090 -- replaced with the constraint, check that this range is within
5091 -- the range of the base type. This checks is done in the init
5092 -- proc for regular objects, but has to be done here for
5093 -- aggregates since no init proc is called for them.
5095 if Is_Array_Type (Expr_Type) then
5096 declare
5097 Index : Node_Id;
5098 -- Range of the current constrained index in the array
5100 Orig_Index : Node_Id := First_Index (Etype (Component));
5101 -- Range corresponding to the range Index above in the
5102 -- original unconstrained record type. The bounds of this
5103 -- range may be governed by discriminants.
5105 Unconstr_Index : Node_Id := First_Index (Etype (Expr_Type));
5106 -- Range corresponding to the range Index above for the
5107 -- unconstrained array type. This range is needed to apply
5108 -- range checks.
5110 begin
5111 Index := First_Index (Expr_Type);
5112 while Present (Index) loop
5113 if Depends_On_Discriminant (Orig_Index) then
5114 Apply_Range_Check (Index, Etype (Unconstr_Index));
5115 end if;
5117 Next_Index (Index);
5118 Next_Index (Orig_Index);
5119 Next_Index (Unconstr_Index);
5120 end loop;
5121 end;
5122 end if;
5123 end if;
5125 -- If the Parent pointer of Expr is not set, Expr is an expression
5126 -- duplicated by New_Tree_Copy (this happens for record aggregates
5127 -- that look like (Field1 | Filed2 => Expr) or (others => Expr)).
5128 -- Such a duplicated expression must be attached to the tree
5129 -- before analysis and resolution to enforce the rule that a tree
5130 -- fragment should never be analyzed or resolved unless it is
5131 -- attached to the current compilation unit.
5133 if No (Parent (Expr)) then
5134 Set_Parent (Expr, N);
5135 Relocate := False;
5136 else
5137 Relocate := True;
5138 end if;
5140 Analyze_And_Resolve (Expr, Expr_Type);
5141 Check_Expr_OK_In_Limited_Aggregate (Expr);
5142 Check_Non_Static_Context (Expr);
5143 Check_Unset_Reference (Expr);
5145 -- Check wrong use of class-wide types
5147 if Is_Class_Wide_Type (Etype (Expr)) then
5148 Error_Msg_N ("dynamically tagged expression not allowed", Expr);
5149 end if;
5151 if not Has_Expansion_Delayed (Expr) then
5152 Aggregate_Constraint_Checks (Expr, Expr_Type);
5153 end if;
5155 -- If an aggregate component has a type with predicates, an explicit
5156 -- predicate check must be applied, as for an assignment statement,
5157 -- because the aggregate might not be expanded into individual
5158 -- component assignments.
5160 if Has_Predicates (Expr_Type)
5161 and then Analyzed (Expr)
5162 then
5163 Apply_Predicate_Check (Expr, Expr_Type);
5164 end if;
5166 if Raises_Constraint_Error (Expr) then
5167 Set_Raises_Constraint_Error (N);
5168 end if;
5170 -- If the expression has been marked as requiring a range check, then
5171 -- generate it here. It's a bit odd to be generating such checks in
5172 -- the analyzer, but harmless since Generate_Range_Check does nothing
5173 -- (other than making sure Do_Range_Check is set) if the expander is
5174 -- not active.
5176 if Do_Range_Check (Expr) then
5177 Generate_Range_Check (Expr, Expr_Type, CE_Range_Check_Failed);
5178 end if;
5180 -- Add association Component => Expr if the caller requests it
5182 if Relocate then
5183 New_Expr := Relocate_Node (Expr);
5185 -- Since New_Expr is not gonna be analyzed later on, we need to
5186 -- propagate here the dimensions form Expr to New_Expr.
5188 Copy_Dimensions (Expr, New_Expr);
5190 else
5191 New_Expr := Expr;
5192 end if;
5194 Add_Association (New_C, New_Expr, New_Assoc_List);
5195 end Resolve_Aggr_Expr;
5197 -------------------
5198 -- Rewrite_Range --
5199 -------------------
5201 procedure Rewrite_Range (Root_Type : Entity_Id; Rge : Node_Id) is
5202 procedure Rewrite_Bound
5203 (Bound : Node_Id;
5204 Disc : Entity_Id;
5205 Expr_Disc : Node_Id);
5206 -- Rewrite a bound of the range Bound, when it is equal to the
5207 -- non-stored discriminant Disc, into the stored discriminant
5208 -- value Expr_Disc.
5210 -------------------
5211 -- Rewrite_Bound --
5212 -------------------
5214 procedure Rewrite_Bound
5215 (Bound : Node_Id;
5216 Disc : Entity_Id;
5217 Expr_Disc : Node_Id)
5219 begin
5220 if Nkind (Bound) /= N_Identifier then
5221 return;
5222 end if;
5224 -- We expect either the discriminant or the discriminal
5226 if Entity (Bound) = Disc
5227 or else (Ekind (Entity (Bound)) = E_In_Parameter
5228 and then Discriminal_Link (Entity (Bound)) = Disc)
5229 then
5230 Rewrite (Bound, New_Copy_Tree (Expr_Disc));
5231 end if;
5232 end Rewrite_Bound;
5234 -- Local variables
5236 Low, High : Node_Id;
5237 Disc : Entity_Id;
5238 Expr_Disc : Elmt_Id;
5240 -- Start of processing for Rewrite_Range
5242 begin
5243 if Has_Discriminants (Root_Type) and then Nkind (Rge) = N_Range then
5244 Low := Low_Bound (Rge);
5245 High := High_Bound (Rge);
5247 Disc := First_Discriminant (Root_Type);
5248 Expr_Disc := First_Elmt (Stored_Constraint (Etype (N)));
5249 while Present (Disc) loop
5250 Rewrite_Bound (Low, Disc, Node (Expr_Disc));
5251 Rewrite_Bound (High, Disc, Node (Expr_Disc));
5252 Next_Discriminant (Disc);
5253 Next_Elmt (Expr_Disc);
5254 end loop;
5255 end if;
5256 end Rewrite_Range;
5258 -- Local variables
5260 Components : constant Elist_Id := New_Elmt_List;
5261 -- Components is the list of the record components whose value must be
5262 -- provided in the aggregate. This list does include discriminants.
5264 Component : Entity_Id;
5265 Component_Elmt : Elmt_Id;
5266 Expr : Node_Id;
5267 Positional_Expr : Node_Id;
5269 -- Start of processing for Resolve_Record_Aggregate
5271 begin
5272 -- A record aggregate is restricted in SPARK:
5274 -- Each named association can have only a single choice.
5275 -- OTHERS cannot be used.
5276 -- Positional and named associations cannot be mixed.
5278 if Present (Component_Associations (N)) then
5279 declare
5280 Assoc : Node_Id;
5282 begin
5283 Assoc := First (Component_Associations (N));
5284 while Present (Assoc) loop
5285 if Nkind (Assoc) = N_Iterated_Component_Association then
5286 Error_Msg_N
5287 ("iterated component association can only appear in an "
5288 & "array aggregate", N);
5289 raise Unrecoverable_Error;
5290 end if;
5292 Next (Assoc);
5293 end loop;
5294 end;
5295 end if;
5297 -- We may end up calling Duplicate_Subexpr on expressions that are
5298 -- attached to New_Assoc_List. For this reason we need to attach it
5299 -- to the tree by setting its parent pointer to N. This parent point
5300 -- will change in STEP 8 below.
5302 Set_Parent (New_Assoc_List, N);
5304 -- STEP 1: abstract type and null record verification
5306 if Is_Abstract_Type (Typ) then
5307 Error_Msg_N ("type of aggregate cannot be abstract", N);
5308 end if;
5310 if No (First_Entity (Typ)) and then Null_Record_Present (N) then
5311 Set_Etype (N, Typ);
5312 return;
5314 elsif Present (First_Entity (Typ))
5315 and then Null_Record_Present (N)
5316 and then not Is_Tagged_Type (Typ)
5317 then
5318 Error_Msg_N ("record aggregate cannot be null", N);
5319 return;
5321 -- If the type has no components, then the aggregate should either
5322 -- have "null record", or in Ada 2005 it could instead have a single
5323 -- component association given by "others => <>". For Ada 95 we flag an
5324 -- error at this point, but for Ada 2005 we proceed with checking the
5325 -- associations below, which will catch the case where it's not an
5326 -- aggregate with "others => <>". Note that the legality of a <>
5327 -- aggregate for a null record type was established by AI05-016.
5329 elsif No (First_Entity (Typ))
5330 and then Ada_Version < Ada_2005
5331 then
5332 Error_Msg_N ("record aggregate must be null", N);
5333 return;
5334 end if;
5336 -- A record aggregate can only use parentheses
5338 if Nkind (N) = N_Aggregate
5339 and then Is_Homogeneous_Aggregate (N)
5340 then
5341 Error_Msg_N ("record aggregate must use (), not '[']", N);
5342 return;
5343 end if;
5345 -- STEP 2: Verify aggregate structure
5347 Step_2 : declare
5348 Assoc : Node_Id;
5349 Bad_Aggregate : Boolean := False;
5350 Selector_Name : Node_Id;
5352 begin
5353 if Present (Component_Associations (N)) then
5354 Assoc := First (Component_Associations (N));
5355 else
5356 Assoc := Empty;
5357 end if;
5359 while Present (Assoc) loop
5360 Selector_Name := First (Choices (Assoc));
5361 while Present (Selector_Name) loop
5362 if Nkind (Selector_Name) = N_Identifier then
5363 null;
5365 elsif Nkind (Selector_Name) = N_Others_Choice then
5366 if Selector_Name /= First (Choices (Assoc))
5367 or else Present (Next (Selector_Name))
5368 then
5369 Error_Msg_N
5370 ("OTHERS must appear alone in a choice list",
5371 Selector_Name);
5372 return;
5374 elsif Present (Next (Assoc)) then
5375 Error_Msg_N
5376 ("OTHERS must appear last in an aggregate",
5377 Selector_Name);
5378 return;
5380 -- (Ada 2005): If this is an association with a box,
5381 -- indicate that the association need not represent
5382 -- any component.
5384 elsif Box_Present (Assoc) then
5385 Others_Box := 1;
5386 Box_Node := Assoc;
5387 end if;
5389 else
5390 Error_Msg_N
5391 ("selector name should be identifier or OTHERS",
5392 Selector_Name);
5393 Bad_Aggregate := True;
5394 end if;
5396 Next (Selector_Name);
5397 end loop;
5399 Next (Assoc);
5400 end loop;
5402 if Bad_Aggregate then
5403 return;
5404 end if;
5405 end Step_2;
5407 -- STEP 3: Find discriminant Values
5409 Step_3 : declare
5410 Discrim : Entity_Id;
5411 Missing_Discriminants : Boolean := False;
5413 begin
5414 if Present (Expressions (N)) then
5415 Positional_Expr := First (Expressions (N));
5416 else
5417 Positional_Expr := Empty;
5418 end if;
5420 -- AI05-0115: if the ancestor part is a subtype mark, the ancestor
5421 -- must not have unknown discriminants.
5422 -- ??? We are not checking any subtype mark here and this code is not
5423 -- exercised by any test, so it's likely wrong (in particular
5424 -- we should not use Root_Type here but the subtype mark, if any),
5425 -- and possibly not needed.
5427 if Is_Derived_Type (Typ)
5428 and then Has_Unknown_Discriminants (Root_Type (Typ))
5429 and then Nkind (N) /= N_Extension_Aggregate
5430 then
5431 Error_Msg_NE
5432 ("aggregate not available for type& whose ancestor "
5433 & "has unknown discriminants", N, Typ);
5434 end if;
5436 if Has_Unknown_Discriminants (Typ)
5437 and then Present (Underlying_Record_View (Typ))
5438 then
5439 Discrim := First_Discriminant (Underlying_Record_View (Typ));
5440 elsif Has_Discriminants (Typ) then
5441 Discrim := First_Discriminant (Typ);
5442 else
5443 Discrim := Empty;
5444 end if;
5446 -- First find the discriminant values in the positional components
5448 while Present (Discrim) and then Present (Positional_Expr) loop
5449 if Discriminant_Present (Discrim) then
5450 Resolve_Aggr_Expr (Positional_Expr, Discrim);
5452 -- Ada 2005 (AI-231)
5454 if Ada_Version >= Ada_2005
5455 and then Known_Null (Positional_Expr)
5456 then
5457 Check_Can_Never_Be_Null (Discrim, Positional_Expr);
5458 end if;
5460 Next (Positional_Expr);
5461 end if;
5463 if Present (Get_Value (Discrim, Component_Associations (N))) then
5464 Error_Msg_NE
5465 ("more than one value supplied for discriminant&",
5466 N, Discrim);
5467 end if;
5469 Next_Discriminant (Discrim);
5470 end loop;
5472 -- Find remaining discriminant values if any among named components
5474 while Present (Discrim) loop
5475 Expr := Get_Value (Discrim, Component_Associations (N), True);
5477 if not Discriminant_Present (Discrim) then
5478 if Present (Expr) then
5479 Error_Msg_NE
5480 ("more than one value supplied for discriminant &",
5481 N, Discrim);
5482 end if;
5484 elsif No (Expr) then
5485 Error_Msg_NE
5486 ("no value supplied for discriminant &", N, Discrim);
5487 Missing_Discriminants := True;
5489 else
5490 Resolve_Aggr_Expr (Expr, Discrim);
5491 end if;
5493 Next_Discriminant (Discrim);
5494 end loop;
5496 if Missing_Discriminants then
5497 return;
5498 end if;
5500 -- At this point and until the beginning of STEP 6, New_Assoc_List
5501 -- contains only the discriminants and their values.
5503 end Step_3;
5505 -- STEP 4: Set the Etype of the record aggregate
5507 if Has_Discriminants (Typ)
5508 or else (Has_Unknown_Discriminants (Typ)
5509 and then Present (Underlying_Record_View (Typ)))
5510 then
5511 Build_Constrained_Itype (N, Typ, New_Assoc_List);
5512 else
5513 Set_Etype (N, Typ);
5514 end if;
5516 -- STEP 5: Get remaining components according to discriminant values
5518 Step_5 : declare
5519 Dnode : Node_Id;
5520 Errors_Found : Boolean := False;
5521 Record_Def : Node_Id;
5522 Parent_Typ : Entity_Id;
5523 Parent_Typ_List : Elist_Id;
5524 Parent_Elmt : Elmt_Id;
5525 Root_Typ : Entity_Id;
5527 begin
5528 if Is_Derived_Type (Typ) and then Is_Tagged_Type (Typ) then
5529 Parent_Typ_List := New_Elmt_List;
5531 -- If this is an extension aggregate, the component list must
5532 -- include all components that are not in the given ancestor type.
5533 -- Otherwise, the component list must include components of all
5534 -- ancestors, starting with the root.
5536 if Nkind (N) = N_Extension_Aggregate then
5537 Root_Typ := Base_Type (Etype (Ancestor_Part (N)));
5539 else
5540 -- AI05-0115: check legality of aggregate for type with a
5541 -- private ancestor.
5543 Root_Typ := Root_Type (Typ);
5544 if Has_Private_Ancestor (Typ) then
5545 declare
5546 Ancestor : constant Entity_Id :=
5547 Find_Private_Ancestor (Typ);
5548 Ancestor_Unit : constant Entity_Id :=
5549 Cunit_Entity
5550 (Get_Source_Unit (Ancestor));
5551 Parent_Unit : constant Entity_Id :=
5552 Cunit_Entity (Get_Source_Unit
5553 (Base_Type (Etype (Ancestor))));
5554 begin
5555 -- Check whether we are in a scope that has full view
5556 -- over the private ancestor and its parent. This can
5557 -- only happen if the derivation takes place in a child
5558 -- unit of the unit that declares the parent, and we are
5559 -- in the private part or body of that child unit, else
5560 -- the aggregate is illegal.
5562 if Is_Child_Unit (Ancestor_Unit)
5563 and then Scope (Ancestor_Unit) = Parent_Unit
5564 and then In_Open_Scopes (Scope (Ancestor))
5565 and then
5566 (In_Private_Part (Scope (Ancestor))
5567 or else In_Package_Body (Scope (Ancestor)))
5568 then
5569 null;
5571 else
5572 Error_Msg_NE
5573 ("type of aggregate has private ancestor&!",
5574 N, Root_Typ);
5575 Error_Msg_N ("must use extension aggregate!", N);
5576 return;
5577 end if;
5578 end;
5579 end if;
5581 Dnode := Declaration_Node (Base_Type (Root_Typ));
5583 -- If we don't get a full declaration, then we have some error
5584 -- which will get signalled later so skip this part. Otherwise
5585 -- gather components of root that apply to the aggregate type.
5586 -- We use the base type in case there is an applicable stored
5587 -- constraint that renames the discriminants of the root.
5589 if Nkind (Dnode) = N_Full_Type_Declaration then
5590 Record_Def := Type_Definition (Dnode);
5591 Gather_Components
5592 (Base_Type (Typ),
5593 Component_List (Record_Def),
5594 Governed_By => New_Assoc_List,
5595 Into => Components,
5596 Report_Errors => Errors_Found);
5598 if Errors_Found then
5599 Error_Msg_N
5600 ("discriminant controlling variant part is not static",
5602 return;
5603 end if;
5604 end if;
5605 end if;
5607 Parent_Typ := Base_Type (Typ);
5608 while Parent_Typ /= Root_Typ loop
5609 Prepend_Elmt (Parent_Typ, To => Parent_Typ_List);
5610 Parent_Typ := Etype (Parent_Typ);
5612 -- Check whether a private parent requires the use of
5613 -- an extension aggregate. This test does not apply in
5614 -- an instantiation: if the generic unit is legal so is
5615 -- the instance.
5617 if Nkind (Parent (Base_Type (Parent_Typ))) =
5618 N_Private_Type_Declaration
5619 or else Nkind (Parent (Base_Type (Parent_Typ))) =
5620 N_Private_Extension_Declaration
5621 then
5622 if Nkind (N) /= N_Extension_Aggregate
5623 and then not In_Instance
5624 then
5625 Error_Msg_NE
5626 ("type of aggregate has private ancestor&!",
5627 N, Parent_Typ);
5628 Error_Msg_N ("must use extension aggregate!", N);
5629 return;
5631 elsif Parent_Typ /= Root_Typ then
5632 Error_Msg_NE
5633 ("ancestor part of aggregate must be private type&",
5634 Ancestor_Part (N), Parent_Typ);
5635 return;
5636 end if;
5638 -- The current view of ancestor part may be a private type,
5639 -- while the context type is always non-private.
5641 elsif Is_Private_Type (Root_Typ)
5642 and then Present (Full_View (Root_Typ))
5643 and then Nkind (N) = N_Extension_Aggregate
5644 then
5645 exit when Base_Type (Full_View (Root_Typ)) = Parent_Typ;
5646 end if;
5647 end loop;
5649 -- Now collect components from all other ancestors, beginning
5650 -- with the current type. If the type has unknown discriminants
5651 -- use the component list of the Underlying_Record_View, which
5652 -- needs to be used for the subsequent expansion of the aggregate
5653 -- into assignments.
5655 Parent_Elmt := First_Elmt (Parent_Typ_List);
5656 while Present (Parent_Elmt) loop
5657 Parent_Typ := Node (Parent_Elmt);
5659 if Has_Unknown_Discriminants (Parent_Typ)
5660 and then Present (Underlying_Record_View (Typ))
5661 then
5662 Parent_Typ := Underlying_Record_View (Parent_Typ);
5663 end if;
5665 Record_Def := Type_Definition (Parent (Base_Type (Parent_Typ)));
5666 Gather_Components (Parent_Typ,
5667 Component_List (Record_Extension_Part (Record_Def)),
5668 Governed_By => New_Assoc_List,
5669 Into => Components,
5670 Report_Errors => Errors_Found);
5672 Next_Elmt (Parent_Elmt);
5673 end loop;
5675 -- Typ is not a derived tagged type
5677 else
5678 Record_Def := Type_Definition (Parent (Base_Type (Typ)));
5680 if Null_Present (Record_Def) then
5681 null;
5683 elsif not Has_Unknown_Discriminants (Typ) then
5684 Gather_Components
5685 (Base_Type (Typ),
5686 Component_List (Record_Def),
5687 Governed_By => New_Assoc_List,
5688 Into => Components,
5689 Report_Errors => Errors_Found);
5691 else
5692 Gather_Components
5693 (Base_Type (Underlying_Record_View (Typ)),
5694 Component_List (Record_Def),
5695 Governed_By => New_Assoc_List,
5696 Into => Components,
5697 Report_Errors => Errors_Found);
5698 end if;
5699 end if;
5701 if Errors_Found then
5702 return;
5703 end if;
5704 end Step_5;
5706 -- STEP 6: Find component Values
5708 Component_Elmt := First_Elmt (Components);
5710 -- First scan the remaining positional associations in the aggregate.
5711 -- Remember that at this point Positional_Expr contains the current
5712 -- positional association if any is left after looking for discriminant
5713 -- values in step 3.
5715 while Present (Positional_Expr) and then Present (Component_Elmt) loop
5716 Component := Node (Component_Elmt);
5717 Resolve_Aggr_Expr (Positional_Expr, Component);
5719 -- Ada 2005 (AI-231)
5721 if Ada_Version >= Ada_2005 and then Known_Null (Positional_Expr) then
5722 Check_Can_Never_Be_Null (Component, Positional_Expr);
5723 end if;
5725 if Present (Get_Value (Component, Component_Associations (N))) then
5726 Error_Msg_NE
5727 ("more than one value supplied for component &", N, Component);
5728 end if;
5730 Next (Positional_Expr);
5731 Next_Elmt (Component_Elmt);
5732 end loop;
5734 if Present (Positional_Expr) then
5735 Error_Msg_N
5736 ("too many components for record aggregate", Positional_Expr);
5737 end if;
5739 -- Now scan for the named arguments of the aggregate
5741 while Present (Component_Elmt) loop
5742 Component := Node (Component_Elmt);
5743 Expr := Get_Value (Component, Component_Associations (N), True);
5745 -- Note: The previous call to Get_Value sets the value of the
5746 -- variable Is_Box_Present.
5748 -- Ada 2005 (AI-287): Handle components with default initialization.
5749 -- Note: This feature was originally added to Ada 2005 for limited
5750 -- but it was finally allowed with any type.
5752 if Is_Box_Present then
5753 Check_Box_Component : declare
5754 Ctyp : constant Entity_Id := Etype (Component);
5756 begin
5757 -- Initially assume that the box is for a default-initialized
5758 -- component and reset to False in cases where that's not true.
5760 Is_Box_Init_By_Default := True;
5762 -- If there is a default expression for the aggregate, copy
5763 -- it into a new association. This copy must modify the scopes
5764 -- of internal types that may be attached to the expression
5765 -- (e.g. index subtypes of arrays) because in general the type
5766 -- declaration and the aggregate appear in different scopes,
5767 -- and the backend requires the scope of the type to match the
5768 -- point at which it is elaborated.
5770 -- If the component has an initialization procedure (IP) we
5771 -- pass the component to the expander, which will generate
5772 -- the call to such IP.
5774 -- If the component has discriminants, their values must
5775 -- be taken from their subtype. This is indispensable for
5776 -- constraints that are given by the current instance of an
5777 -- enclosing type, to allow the expansion of the aggregate to
5778 -- replace the reference to the current instance by the target
5779 -- object of the aggregate.
5781 if Is_Case_Choice_Pattern (N) then
5783 -- Do not transform box component values in a case-choice
5784 -- aggregate.
5786 Add_Association
5787 (Component => Component,
5788 Expr => Empty,
5789 Assoc_List => New_Assoc_List,
5790 Is_Box_Present => True);
5792 elsif Present (Parent (Component))
5793 and then Nkind (Parent (Component)) = N_Component_Declaration
5794 and then Present (Expression (Parent (Component)))
5795 then
5796 -- If component declaration has an initialization expression
5797 -- then this is not a case of default initialization.
5799 Is_Box_Init_By_Default := False;
5801 Expr :=
5802 New_Copy_Tree_And_Copy_Dimensions
5803 (Expression (Parent (Component)),
5804 New_Scope => Current_Scope,
5805 New_Sloc => Sloc (N));
5807 -- As the type of the copied default expression may refer
5808 -- to discriminants of the record type declaration, these
5809 -- non-stored discriminants need to be rewritten into stored
5810 -- discriminant values for the aggregate. This is required
5811 -- in GNATprove mode, and is adopted in all modes to avoid
5812 -- special-casing GNATprove mode.
5814 if Is_Array_Type (Etype (Expr)) then
5815 declare
5816 Rec_Typ : constant Entity_Id := Scope (Component);
5817 -- Root record type whose discriminants may be used as
5818 -- bounds in range nodes.
5820 Assoc : Node_Id;
5821 Choice : Node_Id;
5822 Index : Node_Id;
5824 begin
5825 -- Rewrite the range nodes occurring in the indexes
5826 -- and their types.
5828 Index := First_Index (Etype (Expr));
5829 while Present (Index) loop
5830 Rewrite_Range (Rec_Typ, Index);
5831 Rewrite_Range
5832 (Rec_Typ, Scalar_Range (Etype (Index)));
5834 Next_Index (Index);
5835 end loop;
5837 -- Rewrite the range nodes occurring as aggregate
5838 -- bounds and component associations.
5840 if Nkind (Expr) = N_Aggregate then
5841 if Present (Aggregate_Bounds (Expr)) then
5842 Rewrite_Range (Rec_Typ, Aggregate_Bounds (Expr));
5843 end if;
5845 if Present (Component_Associations (Expr)) then
5846 Assoc := First (Component_Associations (Expr));
5847 while Present (Assoc) loop
5848 Choice := First (Choices (Assoc));
5849 while Present (Choice) loop
5850 Rewrite_Range (Rec_Typ, Choice);
5852 Next (Choice);
5853 end loop;
5855 Next (Assoc);
5856 end loop;
5857 end if;
5858 end if;
5859 end;
5860 end if;
5862 Add_Association
5863 (Component => Component,
5864 Expr => Expr,
5865 Assoc_List => New_Assoc_List);
5866 Set_Has_Self_Reference (N);
5868 elsif Needs_Simple_Initialization (Ctyp) then
5869 Add_Association
5870 (Component => Component,
5871 Expr => Empty,
5872 Assoc_List => New_Assoc_List,
5873 Is_Box_Present => True);
5875 elsif Has_Non_Null_Base_Init_Proc (Ctyp)
5876 or else not Expander_Active
5877 then
5878 if Is_Record_Type (Ctyp)
5879 and then Has_Discriminants (Ctyp)
5880 and then not Is_Private_Type (Ctyp)
5881 then
5882 -- We build a partially initialized aggregate with the
5883 -- values of the discriminants and box initialization
5884 -- for the rest, if other components are present.
5886 -- The type of the aggregate is the known subtype of
5887 -- the component. The capture of discriminants must be
5888 -- recursive because subcomponents may be constrained
5889 -- (transitively) by discriminants of enclosing types.
5890 -- For a private type with discriminants, a call to the
5891 -- initialization procedure will be generated, and no
5892 -- subaggregate is needed.
5894 Capture_Discriminants : declare
5895 Loc : constant Source_Ptr := Sloc (N);
5896 Expr : Node_Id;
5898 begin
5899 Expr := Make_Aggregate (Loc, No_List, New_List);
5900 Set_Etype (Expr, Ctyp);
5902 -- If the enclosing type has discriminants, they have
5903 -- been collected in the aggregate earlier, and they
5904 -- may appear as constraints of subcomponents.
5906 -- Similarly if this component has discriminants, they
5907 -- might in turn be propagated to their components.
5909 if Has_Discriminants (Typ) then
5910 Add_Discriminant_Values (Expr, New_Assoc_List);
5911 Propagate_Discriminants (Expr, New_Assoc_List);
5913 elsif Has_Discriminants (Ctyp) then
5914 Add_Discriminant_Values
5915 (Expr, Component_Associations (Expr));
5916 Propagate_Discriminants
5917 (Expr, Component_Associations (Expr));
5919 Build_Constrained_Itype
5920 (Expr, Ctyp, Component_Associations (Expr));
5922 else
5923 declare
5924 Comp : Entity_Id;
5926 begin
5927 -- If the type has additional components, create
5928 -- an OTHERS box association for them.
5930 Comp := First_Component (Ctyp);
5931 while Present (Comp) loop
5932 if Ekind (Comp) = E_Component then
5933 if not Is_Record_Type (Etype (Comp)) then
5934 Append_To
5935 (Component_Associations (Expr),
5936 Make_Component_Association (Loc,
5937 Choices =>
5938 New_List (
5939 Make_Others_Choice (Loc)),
5940 Expression => Empty,
5941 Box_Present => True));
5942 end if;
5944 exit;
5945 end if;
5947 Next_Component (Comp);
5948 end loop;
5949 end;
5950 end if;
5952 Add_Association
5953 (Component => Component,
5954 Expr => Expr,
5955 Assoc_List => New_Assoc_List);
5956 end Capture_Discriminants;
5958 -- Otherwise the component type is not a record, or it has
5959 -- not discriminants, or it is private.
5961 else
5962 Add_Association
5963 (Component => Component,
5964 Expr => Empty,
5965 Assoc_List => New_Assoc_List,
5966 Is_Box_Present => True);
5967 end if;
5969 -- Otherwise we only need to resolve the expression if the
5970 -- component has partially initialized values (required to
5971 -- expand the corresponding assignments and run-time checks).
5973 elsif Present (Expr)
5974 and then Is_Partially_Initialized_Type (Ctyp)
5975 then
5976 Resolve_Aggr_Expr (Expr, Component);
5977 end if;
5978 end Check_Box_Component;
5980 elsif No (Expr) then
5982 -- Ignore hidden components associated with the position of the
5983 -- interface tags: these are initialized dynamically.
5985 if No (Related_Type (Component)) then
5986 Error_Msg_NE
5987 ("no value supplied for component &!", N, Component);
5988 end if;
5990 else
5991 Resolve_Aggr_Expr (Expr, Component);
5992 end if;
5994 Next_Elmt (Component_Elmt);
5995 end loop;
5997 -- STEP 7: check for invalid components + check type in choice list
5999 Step_7 : declare
6000 Assoc : Node_Id;
6001 New_Assoc : Node_Id;
6003 Selectr : Node_Id;
6004 -- Selector name
6006 Typech : Entity_Id;
6007 -- Type of first component in choice list
6009 begin
6010 if Present (Component_Associations (N)) then
6011 Assoc := First (Component_Associations (N));
6012 else
6013 Assoc := Empty;
6014 end if;
6016 Verification : while Present (Assoc) loop
6017 Selectr := First (Choices (Assoc));
6018 Typech := Empty;
6020 if Nkind (Selectr) = N_Others_Choice then
6022 -- Ada 2005 (AI-287): others choice may have expression or box
6024 if No (Others_Etype) and then Others_Box = 0 then
6025 Error_Msg_N
6026 ("OTHERS must represent at least one component", Selectr);
6028 elsif Others_Box = 1 and then Warn_On_Redundant_Constructs then
6029 Error_Msg_N ("OTHERS choice is redundant?r?", Box_Node);
6030 Error_Msg_N
6031 ("\previous choices cover all components?r?", Box_Node);
6032 end if;
6034 exit Verification;
6035 end if;
6037 while Present (Selectr) loop
6038 Component := Empty;
6039 New_Assoc := First (New_Assoc_List);
6040 while Present (New_Assoc) loop
6041 Component := First (Choices (New_Assoc));
6043 if Chars (Selectr) = Chars (Component) then
6044 if Style_Check then
6045 Check_Identifier (Selectr, Entity (Component));
6046 end if;
6048 exit;
6049 end if;
6051 Next (New_Assoc);
6052 end loop;
6054 -- If we found an association, then this is a legal component
6055 -- of the type in question.
6057 pragma Assert (if Present (New_Assoc) then Present (Component));
6059 -- If no association, this is not a legal component of the type
6060 -- in question, unless its association is provided with a box.
6062 if No (New_Assoc) then
6063 if Box_Present (Parent (Selectr)) then
6065 -- This may still be a bogus component with a box. Scan
6066 -- list of components to verify that a component with
6067 -- that name exists.
6069 declare
6070 C : Entity_Id;
6072 begin
6073 C := First_Component (Typ);
6074 while Present (C) loop
6075 if Chars (C) = Chars (Selectr) then
6077 -- If the context is an extension aggregate,
6078 -- the component must not be inherited from
6079 -- the ancestor part of the aggregate.
6081 if Nkind (N) /= N_Extension_Aggregate
6082 or else
6083 Scope (Original_Record_Component (C)) /=
6084 Etype (Ancestor_Part (N))
6085 then
6086 exit;
6087 end if;
6088 end if;
6090 Next_Component (C);
6091 end loop;
6093 if No (C) then
6094 Error_Msg_Node_2 := Typ;
6095 Error_Msg_N ("& is not a component of}", Selectr);
6096 end if;
6097 end;
6099 elsif Chars (Selectr) /= Name_uTag
6100 and then Chars (Selectr) /= Name_uParent
6101 then
6102 if not Has_Discriminants (Typ) then
6103 Error_Msg_Node_2 := Typ;
6104 Error_Msg_N ("& is not a component of}", Selectr);
6105 else
6106 Error_Msg_N
6107 ("& is not a component of the aggregate subtype",
6108 Selectr);
6109 end if;
6111 Check_Misspelled_Component (Components, Selectr);
6112 end if;
6114 elsif No (Typech) then
6115 Typech := Base_Type (Etype (Component));
6117 -- AI05-0199: In Ada 2012, several components of anonymous
6118 -- access types can appear in a choice list, as long as the
6119 -- designated types match.
6121 elsif Typech /= Base_Type (Etype (Component)) then
6122 if Ada_Version >= Ada_2012
6123 and then Ekind (Typech) = E_Anonymous_Access_Type
6124 and then
6125 Ekind (Etype (Component)) = E_Anonymous_Access_Type
6126 and then Base_Type (Designated_Type (Typech)) =
6127 Base_Type (Designated_Type (Etype (Component)))
6128 and then
6129 Subtypes_Statically_Match (Typech, (Etype (Component)))
6130 then
6131 null;
6133 elsif not Box_Present (Parent (Selectr)) then
6134 Error_Msg_N
6135 ("components in choice list must have same type",
6136 Selectr);
6137 end if;
6138 end if;
6140 Next (Selectr);
6141 end loop;
6143 Next (Assoc);
6144 end loop Verification;
6145 end Step_7;
6147 -- STEP 8: replace the original aggregate
6149 Step_8 : declare
6150 New_Aggregate : constant Node_Id := New_Copy (N);
6152 begin
6153 Set_Expressions (New_Aggregate, No_List);
6154 Set_Etype (New_Aggregate, Etype (N));
6155 Set_Component_Associations (New_Aggregate, New_Assoc_List);
6156 Set_Check_Actuals (New_Aggregate, Check_Actuals (N));
6158 Rewrite (N, New_Aggregate);
6159 end Step_8;
6161 -- Check the dimensions of the components in the record aggregate
6163 Analyze_Dimension_Extension_Or_Record_Aggregate (N);
6164 end Resolve_Record_Aggregate;
6166 -----------------------------
6167 -- Check_Can_Never_Be_Null --
6168 -----------------------------
6170 procedure Check_Can_Never_Be_Null (Typ : Entity_Id; Expr : Node_Id) is
6171 Comp_Typ : Entity_Id;
6173 begin
6174 pragma Assert
6175 (Ada_Version >= Ada_2005
6176 and then Present (Expr)
6177 and then Known_Null (Expr));
6179 case Ekind (Typ) is
6180 when E_Array_Type =>
6181 Comp_Typ := Component_Type (Typ);
6183 when E_Component
6184 | E_Discriminant
6186 Comp_Typ := Etype (Typ);
6188 when others =>
6189 return;
6190 end case;
6192 if Can_Never_Be_Null (Comp_Typ) then
6194 -- Here we know we have a constraint error. Note that we do not use
6195 -- Apply_Compile_Time_Constraint_Error here to the Expr, which might
6196 -- seem the more natural approach. That's because in some cases the
6197 -- components are rewritten, and the replacement would be missed.
6198 -- We do not mark the whole aggregate as raising a constraint error,
6199 -- because the association may be a null array range.
6201 Error_Msg_N
6202 ("(Ada 2005) NULL not allowed in null-excluding component??", Expr);
6203 Error_Msg_N
6204 ("\Constraint_Error will be raised at run time??", Expr);
6206 Rewrite (Expr,
6207 Make_Raise_Constraint_Error
6208 (Sloc (Expr), Reason => CE_Access_Check_Failed));
6209 Set_Etype (Expr, Comp_Typ);
6210 Set_Analyzed (Expr);
6211 end if;
6212 end Check_Can_Never_Be_Null;
6214 ---------------------
6215 -- Sort_Case_Table --
6216 ---------------------
6218 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is
6219 U : constant Int := Case_Table'Last;
6220 K : Int;
6221 J : Int;
6222 T : Case_Bounds;
6224 begin
6225 K := 1;
6226 while K < U loop
6227 T := Case_Table (K + 1);
6229 J := K + 1;
6230 while J > 1
6231 and then Expr_Value (Case_Table (J - 1).Lo) > Expr_Value (T.Lo)
6232 loop
6233 Case_Table (J) := Case_Table (J - 1);
6234 J := J - 1;
6235 end loop;
6237 Case_Table (J) := T;
6238 K := K + 1;
6239 end loop;
6240 end Sort_Case_Table;
6242 end Sem_Aggr;