ada: Fix renaming of predefined equality operator for unchecked union types
[official-gcc.git] / gcc / ada / sem_aggr.adb
blob39189463871e02c50d06348f2f0f291733ce6ade
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 elsif Is_Record_Type (Typ) then
1069 Resolve_Record_Aggregate (N, Typ);
1071 elsif Is_Array_Type (Typ) then
1073 -- First a special test, for the case of a positional aggregate of
1074 -- characters which can be replaced by a string literal.
1076 -- Do not perform this transformation if this was a string literal
1077 -- to start with, whose components needed constraint checks, or if
1078 -- the component type is non-static, because it will require those
1079 -- checks and be transformed back into an aggregate. If the index
1080 -- type is not Integer the aggregate may represent a user-defined
1081 -- string type but the context might need the original type so we
1082 -- do not perform the transformation at this point.
1084 if Number_Dimensions (Typ) = 1
1085 and then Is_Standard_Character_Type (Component_Type (Typ))
1086 and then No (Component_Associations (N))
1087 and then not Is_Limited_Composite (Typ)
1088 and then not Is_Private_Composite (Typ)
1089 and then not Is_Bit_Packed_Array (Typ)
1090 and then Nkind (Original_Node (Parent (N))) /= N_String_Literal
1091 and then Is_OK_Static_Subtype (Component_Type (Typ))
1092 and then Base_Type (Etype (First_Index (Typ))) =
1093 Base_Type (Standard_Integer)
1094 then
1095 declare
1096 Expr : Node_Id;
1098 begin
1099 Expr := First (Expressions (N));
1100 while Present (Expr) loop
1101 exit when Nkind (Expr) /= N_Character_Literal;
1102 Next (Expr);
1103 end loop;
1105 if No (Expr) then
1106 Start_String;
1108 Expr := First (Expressions (N));
1109 while Present (Expr) loop
1110 Store_String_Char (UI_To_CC (Char_Literal_Value (Expr)));
1111 Next (Expr);
1112 end loop;
1114 Rewrite (N, Make_String_Literal (Loc, End_String));
1116 Analyze_And_Resolve (N, Typ);
1117 return;
1118 end if;
1119 end;
1120 end if;
1122 -- Here if we have a real aggregate to deal with
1124 Array_Aggregate : declare
1125 Aggr_Resolved : Boolean;
1126 Aggr_Typ : constant Entity_Id := Etype (Typ);
1127 -- This is the unconstrained array type, which is the type against
1128 -- which the aggregate is to be resolved. Typ itself is the array
1129 -- type of the context which may not be the same subtype as the
1130 -- subtype for the final aggregate.
1132 Is_Null_Aggr : constant Boolean := Is_Null_Aggregate (N);
1134 begin
1135 -- In the following we determine whether an OTHERS choice is
1136 -- allowed inside the array aggregate. The test checks the context
1137 -- in which the array aggregate occurs. If the context does not
1138 -- permit it, or the aggregate type is unconstrained, an OTHERS
1139 -- choice is not allowed (except that it is always allowed on the
1140 -- right-hand side of an assignment statement; in this case the
1141 -- constrainedness of the type doesn't matter, because an array
1142 -- object is always constrained).
1144 -- If expansion is disabled (generic context, or semantics-only
1145 -- mode) actual subtypes cannot be constructed, and the type of an
1146 -- object may be its unconstrained nominal type. However, if the
1147 -- context is an assignment statement, OTHERS is allowed, because
1148 -- the target of the assignment will have a constrained subtype
1149 -- when fully compiled. Ditto if the context is an initialization
1150 -- procedure where a component may have a predicate function that
1151 -- carries the base type.
1153 -- Note that there is no node for Explicit_Actual_Parameter.
1154 -- To test for this context we therefore have to test for node
1155 -- N_Parameter_Association which itself appears only if there is a
1156 -- formal parameter. Consequently we also need to test for
1157 -- N_Procedure_Call_Statement or N_Function_Call.
1159 -- The context may be an N_Reference node, created by expansion.
1160 -- Legality of the others clause was established in the source,
1161 -- so the context is legal.
1163 Set_Etype (N, Aggr_Typ); -- May be overridden later on
1165 if Is_Null_Aggr then
1166 Set_Etype (N, Typ);
1167 Aggr_Resolved := Resolve_Null_Array_Aggregate (N);
1169 elsif Nkind (Parent (N)) = N_Assignment_Statement
1170 or else Inside_Init_Proc
1171 or else (Is_Constrained (Typ)
1172 and then Nkind (Parent (N)) in
1173 N_Parameter_Association
1174 | N_Function_Call
1175 | N_Procedure_Call_Statement
1176 | N_Generic_Association
1177 | N_Formal_Object_Declaration
1178 | N_Simple_Return_Statement
1179 | N_Object_Declaration
1180 | N_Component_Declaration
1181 | N_Parameter_Specification
1182 | N_Qualified_Expression
1183 | N_Unchecked_Type_Conversion
1184 | N_Reference
1185 | N_Aggregate
1186 | N_Extension_Aggregate
1187 | N_Component_Association
1188 | N_Case_Expression_Alternative
1189 | N_If_Expression
1190 | N_Expression_With_Actions)
1191 then
1192 Aggr_Resolved :=
1193 Resolve_Array_Aggregate
1195 Index => First_Index (Aggr_Typ),
1196 Index_Constr => First_Index (Typ),
1197 Component_Typ => Component_Type (Typ),
1198 Others_Allowed => True);
1199 else
1200 Aggr_Resolved :=
1201 Resolve_Array_Aggregate
1203 Index => First_Index (Aggr_Typ),
1204 Index_Constr => First_Index (Aggr_Typ),
1205 Component_Typ => Component_Type (Typ),
1206 Others_Allowed => False);
1207 end if;
1209 if not Aggr_Resolved then
1211 -- A parenthesized expression may have been intended as an
1212 -- aggregate, leading to a type error when analyzing the
1213 -- component. This can also happen for a nested component
1214 -- (see Analyze_Aggr_Expr).
1216 if Paren_Count (N) > 0 then
1217 Error_Msg_N
1218 ("positional aggregate cannot have one component", N);
1219 end if;
1221 Aggr_Subtyp := Any_Composite;
1223 else
1224 Aggr_Subtyp := Array_Aggr_Subtype (N, Typ);
1225 end if;
1227 Set_Etype (N, Aggr_Subtyp);
1228 end Array_Aggregate;
1230 elsif Is_Private_Type (Typ)
1231 and then Present (Full_View (Typ))
1232 and then (In_Inlined_Body or In_Instance_Body)
1233 and then Is_Composite_Type (Full_View (Typ))
1234 then
1235 Resolve (N, Full_View (Typ));
1237 else
1238 Error_Msg_N ("illegal context for aggregate", N);
1239 end if;
1241 -- If we can determine statically that the evaluation of the aggregate
1242 -- raises Constraint_Error, then replace the aggregate with an
1243 -- N_Raise_Constraint_Error node, but set the Etype to the right
1244 -- aggregate subtype. Gigi needs this.
1246 if Raises_Constraint_Error (N) then
1247 Aggr_Subtyp := Etype (N);
1248 Rewrite (N,
1249 Make_Raise_Constraint_Error (Loc, Reason => CE_Range_Check_Failed));
1250 Set_Raises_Constraint_Error (N);
1251 Set_Etype (N, Aggr_Subtyp);
1252 Set_Analyzed (N);
1253 end if;
1255 if Warn_On_No_Value_Assigned
1256 and then Others_Box
1257 and then not Is_Fully_Initialized_Type (Etype (N))
1258 then
1259 Error_Msg_N ("?v?aggregate not fully initialized", N);
1260 end if;
1262 Check_Function_Writable_Actuals (N);
1263 end Resolve_Aggregate;
1265 -----------------------------
1266 -- Resolve_Array_Aggregate --
1267 -----------------------------
1269 function Resolve_Array_Aggregate
1270 (N : Node_Id;
1271 Index : Node_Id;
1272 Index_Constr : Node_Id;
1273 Component_Typ : Entity_Id;
1274 Others_Allowed : Boolean) return Boolean
1276 Loc : constant Source_Ptr := Sloc (N);
1278 Failure : constant Boolean := False;
1279 Success : constant Boolean := True;
1281 Index_Typ : constant Entity_Id := Etype (Index);
1282 Index_Typ_Low : constant Node_Id := Type_Low_Bound (Index_Typ);
1283 Index_Typ_High : constant Node_Id := Type_High_Bound (Index_Typ);
1284 -- The type of the index corresponding to the array sub-aggregate along
1285 -- with its low and upper bounds.
1287 Index_Base : constant Entity_Id := Base_Type (Index_Typ);
1288 Index_Base_Low : constant Node_Id := Type_Low_Bound (Index_Base);
1289 Index_Base_High : constant Node_Id := Type_High_Bound (Index_Base);
1290 -- Ditto for the base type
1292 Others_Present : Boolean := False;
1294 Nb_Choices : Nat := 0;
1295 -- Contains the overall number of named choices in this sub-aggregate
1297 function Add (Val : Uint; To : Node_Id) return Node_Id;
1298 -- Creates a new expression node where Val is added to expression To.
1299 -- Tries to constant fold whenever possible. To must be an already
1300 -- analyzed expression.
1302 procedure Check_Bound (BH : Node_Id; AH : in out Node_Id);
1303 -- Checks that AH (the upper bound of an array aggregate) is less than
1304 -- or equal to BH (the upper bound of the index base type). If the check
1305 -- fails, a warning is emitted, the Raises_Constraint_Error flag of N is
1306 -- set, and AH is replaced with a duplicate of BH.
1308 procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id);
1309 -- Checks that range AL .. AH is compatible with range L .. H. Emits a
1310 -- warning if not and sets the Raises_Constraint_Error flag in N.
1312 procedure Check_Length (L, H : Node_Id; Len : Uint);
1313 -- Checks that range L .. H contains at least Len elements. Emits a
1314 -- warning if not and sets the Raises_Constraint_Error flag in N.
1316 function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean;
1317 -- Returns True if range L .. H is dynamic or null
1319 procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean);
1320 -- Given expression node From, this routine sets OK to False if it
1321 -- cannot statically evaluate From. Otherwise it stores this static
1322 -- value into Value.
1324 function Resolve_Aggr_Expr
1325 (Expr : Node_Id;
1326 Single_Elmt : Boolean) return Boolean;
1327 -- Resolves aggregate expression Expr. Returns False if resolution
1328 -- fails. If Single_Elmt is set to False, the expression Expr may be
1329 -- used to initialize several array aggregate elements (this can happen
1330 -- for discrete choices such as "L .. H => Expr" or the OTHERS choice).
1331 -- In this event we do not resolve Expr unless expansion is disabled.
1332 -- To know why, see the DELAYED COMPONENT RESOLUTION note above.
1334 -- NOTE: In the case of "... => <>", we pass the N_Component_Association
1335 -- node as Expr, since there is no Expression and we need a Sloc for the
1336 -- error message.
1338 procedure Resolve_Iterated_Component_Association
1339 (N : Node_Id;
1340 Index_Typ : Entity_Id);
1341 -- For AI12-061
1343 procedure Warn_On_Null_Component_Association (Expr : Node_Id);
1344 -- Expr is either a conditional expression or a case expression of an
1345 -- iterated component association initializing the aggregate N with
1346 -- components that can never be null. Report warning on associations
1347 -- that may initialize some component with a null value.
1349 ---------
1350 -- Add --
1351 ---------
1353 function Add (Val : Uint; To : Node_Id) return Node_Id is
1354 Expr_Pos : Node_Id;
1355 Expr : Node_Id;
1356 To_Pos : Node_Id;
1358 begin
1359 if Raises_Constraint_Error (To) then
1360 return To;
1361 end if;
1363 -- First test if we can do constant folding
1365 if Compile_Time_Known_Value (To)
1366 or else Nkind (To) = N_Integer_Literal
1367 then
1368 Expr_Pos := Make_Integer_Literal (Loc, Expr_Value (To) + Val);
1369 Set_Is_Static_Expression (Expr_Pos);
1370 Set_Etype (Expr_Pos, Etype (To));
1371 Set_Analyzed (Expr_Pos, Analyzed (To));
1373 if not Is_Enumeration_Type (Index_Typ) then
1374 Expr := Expr_Pos;
1376 -- If we are dealing with enumeration return
1377 -- Index_Typ'Val (Expr_Pos)
1379 else
1380 Expr :=
1381 Make_Attribute_Reference
1382 (Loc,
1383 Prefix => New_Occurrence_Of (Index_Typ, Loc),
1384 Attribute_Name => Name_Val,
1385 Expressions => New_List (Expr_Pos));
1386 end if;
1388 return Expr;
1389 end if;
1391 -- If we are here no constant folding possible
1393 if not Is_Enumeration_Type (Index_Base) then
1394 Expr :=
1395 Make_Op_Add (Loc,
1396 Left_Opnd => Duplicate_Subexpr (To),
1397 Right_Opnd => Make_Integer_Literal (Loc, Val));
1399 -- If we are dealing with enumeration return
1400 -- Index_Typ'Val (Index_Typ'Pos (To) + Val)
1402 else
1403 To_Pos :=
1404 Make_Attribute_Reference
1405 (Loc,
1406 Prefix => New_Occurrence_Of (Index_Typ, Loc),
1407 Attribute_Name => Name_Pos,
1408 Expressions => New_List (Duplicate_Subexpr (To)));
1410 Expr_Pos :=
1411 Make_Op_Add (Loc,
1412 Left_Opnd => To_Pos,
1413 Right_Opnd => Make_Integer_Literal (Loc, Val));
1415 Expr :=
1416 Make_Attribute_Reference
1417 (Loc,
1418 Prefix => New_Occurrence_Of (Index_Typ, Loc),
1419 Attribute_Name => Name_Val,
1420 Expressions => New_List (Expr_Pos));
1422 -- If the index type has a non standard representation, the
1423 -- attributes 'Val and 'Pos expand into function calls and the
1424 -- resulting expression is considered non-safe for reevaluation
1425 -- by the backend. Relocate it into a constant temporary in order
1426 -- to make it safe for reevaluation.
1428 if Has_Non_Standard_Rep (Etype (N)) then
1429 declare
1430 Def_Id : Entity_Id;
1432 begin
1433 Def_Id := Make_Temporary (Loc, 'R', Expr);
1434 Set_Etype (Def_Id, Index_Typ);
1435 Insert_Action (N,
1436 Make_Object_Declaration (Loc,
1437 Defining_Identifier => Def_Id,
1438 Object_Definition =>
1439 New_Occurrence_Of (Index_Typ, Loc),
1440 Constant_Present => True,
1441 Expression => Relocate_Node (Expr)));
1443 Expr := New_Occurrence_Of (Def_Id, Loc);
1444 end;
1445 end if;
1446 end if;
1448 return Expr;
1449 end Add;
1451 -----------------
1452 -- Check_Bound --
1453 -----------------
1455 procedure Check_Bound (BH : Node_Id; AH : in out Node_Id) is
1456 Val_BH : Uint;
1457 Val_AH : Uint;
1459 OK_BH : Boolean;
1460 OK_AH : Boolean;
1462 begin
1463 Get (Value => Val_BH, From => BH, OK => OK_BH);
1464 Get (Value => Val_AH, From => AH, OK => OK_AH);
1466 if OK_BH and then OK_AH and then Val_BH < Val_AH then
1467 Set_Raises_Constraint_Error (N);
1468 Error_Msg_Warn := SPARK_Mode /= On;
1469 Error_Msg_N ("upper bound out of range<<", AH);
1470 Error_Msg_N ("\Constraint_Error [<<", AH);
1472 -- You need to set AH to BH or else in the case of enumerations
1473 -- indexes we will not be able to resolve the aggregate bounds.
1475 AH := Duplicate_Subexpr (BH);
1476 end if;
1477 end Check_Bound;
1479 ------------------
1480 -- Check_Bounds --
1481 ------------------
1483 procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id) is
1484 Val_L : Uint;
1485 Val_H : Uint;
1486 Val_AL : Uint;
1487 Val_AH : Uint;
1489 OK_L : Boolean;
1490 OK_H : Boolean;
1492 OK_AL : Boolean;
1493 OK_AH : Boolean;
1494 pragma Warnings (Off, OK_AL);
1495 pragma Warnings (Off, OK_AH);
1497 begin
1498 if Raises_Constraint_Error (N)
1499 or else Dynamic_Or_Null_Range (AL, AH)
1500 then
1501 return;
1502 end if;
1504 Get (Value => Val_L, From => L, OK => OK_L);
1505 Get (Value => Val_H, From => H, OK => OK_H);
1507 Get (Value => Val_AL, From => AL, OK => OK_AL);
1508 Get (Value => Val_AH, From => AH, OK => OK_AH);
1510 if OK_L and then Val_L > Val_AL then
1511 Set_Raises_Constraint_Error (N);
1512 Error_Msg_Warn := SPARK_Mode /= On;
1513 Error_Msg_N ("lower bound of aggregate out of range<<", N);
1514 Error_Msg_N ("\Constraint_Error [<<", N);
1515 end if;
1517 if OK_H and then Val_H < Val_AH then
1518 Set_Raises_Constraint_Error (N);
1519 Error_Msg_Warn := SPARK_Mode /= On;
1520 Error_Msg_N ("upper bound of aggregate out of range<<", N);
1521 Error_Msg_N ("\Constraint_Error [<<", N);
1522 end if;
1523 end Check_Bounds;
1525 ------------------
1526 -- Check_Length --
1527 ------------------
1529 procedure Check_Length (L, H : Node_Id; Len : Uint) is
1530 Val_L : Uint;
1531 Val_H : Uint;
1533 OK_L : Boolean;
1534 OK_H : Boolean;
1536 Range_Len : Uint;
1538 begin
1539 if Raises_Constraint_Error (N) then
1540 return;
1541 end if;
1543 Get (Value => Val_L, From => L, OK => OK_L);
1544 Get (Value => Val_H, From => H, OK => OK_H);
1546 if not OK_L or else not OK_H then
1547 return;
1548 end if;
1550 -- If null range length is zero
1552 if Val_L > Val_H then
1553 Range_Len := Uint_0;
1554 else
1555 Range_Len := Val_H - Val_L + 1;
1556 end if;
1558 if Range_Len < Len then
1559 Set_Raises_Constraint_Error (N);
1560 Error_Msg_Warn := SPARK_Mode /= On;
1561 Error_Msg_N ("too many elements<<", N);
1562 Error_Msg_N ("\Constraint_Error [<<", N);
1563 end if;
1564 end Check_Length;
1566 ---------------------------
1567 -- Dynamic_Or_Null_Range --
1568 ---------------------------
1570 function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean is
1571 Val_L : Uint;
1572 Val_H : Uint;
1574 OK_L : Boolean;
1575 OK_H : Boolean;
1577 begin
1578 Get (Value => Val_L, From => L, OK => OK_L);
1579 Get (Value => Val_H, From => H, OK => OK_H);
1581 return not OK_L or else not OK_H
1582 or else not Is_OK_Static_Expression (L)
1583 or else not Is_OK_Static_Expression (H)
1584 or else Val_L > Val_H;
1585 end Dynamic_Or_Null_Range;
1587 ---------
1588 -- Get --
1589 ---------
1591 procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean) is
1592 begin
1593 OK := True;
1595 if Compile_Time_Known_Value (From) then
1596 Value := Expr_Value (From);
1598 -- If expression From is something like Some_Type'Val (10) then
1599 -- Value = 10.
1601 elsif Nkind (From) = N_Attribute_Reference
1602 and then Attribute_Name (From) = Name_Val
1603 and then Compile_Time_Known_Value (First (Expressions (From)))
1604 then
1605 Value := Expr_Value (First (Expressions (From)));
1606 else
1607 Value := Uint_0;
1608 OK := False;
1609 end if;
1610 end Get;
1612 -----------------------
1613 -- Resolve_Aggr_Expr --
1614 -----------------------
1616 function Resolve_Aggr_Expr
1617 (Expr : Node_Id;
1618 Single_Elmt : Boolean) return Boolean
1620 Nxt_Ind : constant Node_Id := Next_Index (Index);
1621 Nxt_Ind_Constr : constant Node_Id := Next_Index (Index_Constr);
1622 -- Index is the current index corresponding to the expression
1624 Resolution_OK : Boolean := True;
1625 -- Set to False if resolution of the expression failed
1627 begin
1628 -- Defend against previous errors
1630 if Nkind (Expr) = N_Error
1631 or else Error_Posted (Expr)
1632 then
1633 return True;
1634 end if;
1636 -- If the array type against which we are resolving the aggregate
1637 -- has several dimensions, the expressions nested inside the
1638 -- aggregate must be further aggregates (or strings).
1640 if Present (Nxt_Ind) then
1641 if Nkind (Expr) /= N_Aggregate then
1643 -- A string literal can appear where a one-dimensional array
1644 -- of characters is expected. If the literal looks like an
1645 -- operator, it is still an operator symbol, which will be
1646 -- transformed into a string when analyzed.
1648 if Is_Character_Type (Component_Typ)
1649 and then No (Next_Index (Nxt_Ind))
1650 and then Nkind (Expr) in N_String_Literal | N_Operator_Symbol
1651 then
1652 -- A string literal used in a multidimensional array
1653 -- aggregate in place of the final one-dimensional
1654 -- aggregate must not be enclosed in parentheses.
1656 if Paren_Count (Expr) /= 0 then
1657 Error_Msg_N ("no parenthesis allowed here", Expr);
1658 end if;
1660 Make_String_Into_Aggregate (Expr);
1662 else
1663 Error_Msg_N ("nested array aggregate expected", Expr);
1665 -- If the expression is parenthesized, this may be
1666 -- a missing component association for a 1-aggregate.
1668 if Paren_Count (Expr) > 0 then
1669 Error_Msg_N
1670 ("\if single-component aggregate is intended, "
1671 & "write e.g. (1 ='> ...)", Expr);
1672 end if;
1674 return Failure;
1675 end if;
1676 end if;
1678 -- If it's "... => <>", nothing to resolve
1680 if Nkind (Expr) = N_Component_Association then
1681 pragma Assert (Box_Present (Expr));
1682 return Success;
1683 end if;
1685 -- Ada 2005 (AI-231): Propagate the type to the nested aggregate.
1686 -- Required to check the null-exclusion attribute (if present).
1687 -- This value may be overridden later on.
1689 Set_Etype (Expr, Etype (N));
1691 Resolution_OK := Resolve_Array_Aggregate
1692 (Expr, Nxt_Ind, Nxt_Ind_Constr, Component_Typ, Others_Allowed);
1694 else
1695 -- If it's "... => <>", nothing to resolve
1697 if Nkind (Expr) = N_Component_Association then
1698 pragma Assert (Box_Present (Expr));
1699 return Success;
1700 end if;
1702 -- Do not resolve the expressions of discrete or others choices
1703 -- unless the expression covers a single component, or the
1704 -- expander is inactive.
1706 -- In SPARK mode, expressions that can perform side effects will
1707 -- be recognized by the gnat2why back-end, and the whole
1708 -- subprogram will be ignored. So semantic analysis can be
1709 -- performed safely.
1711 if Single_Elmt
1712 or else not Expander_Active
1713 or else In_Spec_Expression
1714 then
1715 Analyze_And_Resolve (Expr, Component_Typ);
1716 Check_Expr_OK_In_Limited_Aggregate (Expr);
1717 Check_Non_Static_Context (Expr);
1718 Aggregate_Constraint_Checks (Expr, Component_Typ);
1719 Check_Unset_Reference (Expr);
1720 end if;
1721 end if;
1723 -- If an aggregate component has a type with predicates, an explicit
1724 -- predicate check must be applied, as for an assignment statement,
1725 -- because the aggregate might not be expanded into individual
1726 -- component assignments. If the expression covers several components
1727 -- the analysis and the predicate check take place later.
1729 if Has_Predicates (Component_Typ)
1730 and then Analyzed (Expr)
1731 then
1732 Apply_Predicate_Check (Expr, Component_Typ);
1733 end if;
1735 if Raises_Constraint_Error (Expr)
1736 and then Nkind (Parent (Expr)) /= N_Component_Association
1737 then
1738 Set_Raises_Constraint_Error (N);
1739 end if;
1741 -- If the expression has been marked as requiring a range check,
1742 -- then generate it here. It's a bit odd to be generating such
1743 -- checks in the analyzer, but harmless since Generate_Range_Check
1744 -- does nothing (other than making sure Do_Range_Check is set) if
1745 -- the expander is not active.
1747 if Do_Range_Check (Expr) then
1748 Generate_Range_Check (Expr, Component_Typ, CE_Range_Check_Failed);
1749 end if;
1751 return Resolution_OK;
1752 end Resolve_Aggr_Expr;
1754 --------------------------------------------
1755 -- Resolve_Iterated_Component_Association --
1756 --------------------------------------------
1758 procedure Resolve_Iterated_Component_Association
1759 (N : Node_Id;
1760 Index_Typ : Entity_Id)
1762 Loc : constant Source_Ptr := Sloc (N);
1763 Id : constant Entity_Id := Defining_Identifier (N);
1765 -----------------------
1766 -- Remove_References --
1767 -----------------------
1769 function Remove_Reference (N : Node_Id) return Traverse_Result;
1770 -- Remove reference to the entity Id after analysis, so it can be
1771 -- properly reanalyzed after construct is expanded into a loop.
1773 function Remove_Reference (N : Node_Id) return Traverse_Result is
1774 begin
1775 if Nkind (N) = N_Identifier
1776 and then Present (Entity (N))
1777 and then Entity (N) = Id
1778 then
1779 Set_Entity (N, Empty);
1780 Set_Etype (N, Empty);
1781 end if;
1782 Set_Analyzed (N, False);
1783 return OK;
1784 end Remove_Reference;
1786 procedure Remove_References is new Traverse_Proc (Remove_Reference);
1788 -- Local variables
1790 Choice : Node_Id;
1791 Dummy : Boolean;
1792 Scop : Entity_Id;
1793 Expr : constant Node_Id := Expression (N);
1795 -- Start of processing for Resolve_Iterated_Component_Association
1797 begin
1798 Error_Msg_Ada_2022_Feature ("iterated component", Loc);
1800 -- Create a scope in which to introduce an index, to make it visible
1801 -- for the analysis of component expression.
1803 Scop := New_Internal_Entity (E_Loop, Current_Scope, Loc, 'L');
1804 Set_Etype (Scop, Standard_Void_Type);
1805 Set_Parent (Scop, Parent (N));
1806 Push_Scope (Scop);
1808 -- If there is iterator specification, then its preanalysis will make
1809 -- the index visible.
1811 if Present (Iterator_Specification (N)) then
1812 Preanalyze (Iterator_Specification (N));
1814 -- Otherwise, analyze discrete choices and make the index visible
1816 else
1817 -- Insert index name into current scope but don't decorate it yet,
1818 -- so that a premature usage of this name in discrete choices will
1819 -- be nicely diagnosed.
1821 Enter_Name (Id);
1823 Choice := First (Discrete_Choices (N));
1825 while Present (Choice) loop
1826 if Nkind (Choice) = N_Others_Choice then
1827 Others_Present := True;
1829 else
1830 Analyze (Choice);
1832 -- Choice can be a subtype name, a range, or an expression
1834 if Is_Entity_Name (Choice)
1835 and then Is_Type (Entity (Choice))
1836 and then
1837 Base_Type (Entity (Choice)) = Base_Type (Index_Typ)
1838 then
1839 null;
1841 else
1842 Analyze_And_Resolve (Choice, Index_Typ);
1843 end if;
1844 end if;
1846 Next (Choice);
1847 end loop;
1849 -- Decorate the index variable
1851 Set_Etype (Id, Index_Typ);
1852 Mutate_Ekind (Id, E_Variable);
1853 Set_Is_Not_Self_Hidden (Id);
1854 Set_Scope (Id, Scop);
1855 end if;
1857 -- Analyze expression without expansion, to verify legality.
1858 -- When generating code, we then remove references to the index
1859 -- variable, because the expression will be analyzed anew after
1860 -- rewritting as a loop with a new index variable; when not
1861 -- generating code we leave the analyzed expression as it is.
1863 Dummy := Resolve_Aggr_Expr (Expr, Single_Elmt => False);
1865 if Operating_Mode /= Check_Semantics then
1866 Remove_References (Expr);
1867 end if;
1869 -- An iterated_component_association may appear in a nested
1870 -- aggregate for a multidimensional structure: preserve the bounds
1871 -- computed for the expression, as well as the anonymous array
1872 -- type generated for it; both are needed during array expansion.
1874 if Nkind (Expr) = N_Aggregate then
1875 Set_Aggregate_Bounds (Expression (N), Aggregate_Bounds (Expr));
1876 Set_Etype (Expression (N), Etype (Expr));
1877 end if;
1879 End_Scope;
1880 end Resolve_Iterated_Component_Association;
1882 ----------------------------------------
1883 -- Warn_On_Null_Component_Association --
1884 ----------------------------------------
1886 procedure Warn_On_Null_Component_Association (Expr : Node_Id) is
1887 Comp_Typ : constant Entity_Id := Component_Type (Etype (N));
1889 procedure Check_Case_Expr (N : Node_Id);
1890 -- Check if a case expression may initialize some component with a
1891 -- null value.
1893 procedure Check_Cond_Expr (N : Node_Id);
1894 -- Check if a conditional expression may initialize some component
1895 -- with a null value.
1897 procedure Check_Expr (Expr : Node_Id);
1898 -- Check if an expression may initialize some component with a
1899 -- null value.
1901 procedure Warn_On_Null_Expression_And_Rewrite (Null_Expr : Node_Id);
1902 -- Report warning on known null expression and replace the expression
1903 -- by a raise constraint error node.
1905 ---------------------
1906 -- Check_Case_Expr --
1907 ---------------------
1909 procedure Check_Case_Expr (N : Node_Id) is
1910 Alt_Node : Node_Id := First (Alternatives (N));
1912 begin
1913 while Present (Alt_Node) loop
1914 Check_Expr (Expression (Alt_Node));
1915 Next (Alt_Node);
1916 end loop;
1917 end Check_Case_Expr;
1919 ---------------------
1920 -- Check_Cond_Expr --
1921 ---------------------
1923 procedure Check_Cond_Expr (N : Node_Id) is
1924 If_Expr : Node_Id := N;
1925 Then_Expr : Node_Id;
1926 Else_Expr : Node_Id;
1928 begin
1929 Then_Expr := Next (First (Expressions (If_Expr)));
1930 Else_Expr := Next (Then_Expr);
1932 Check_Expr (Then_Expr);
1934 -- Process elsif parts (if any)
1936 while Nkind (Else_Expr) = N_If_Expression loop
1937 If_Expr := Else_Expr;
1938 Then_Expr := Next (First (Expressions (If_Expr)));
1939 Else_Expr := Next (Then_Expr);
1941 Check_Expr (Then_Expr);
1942 end loop;
1944 if Known_Null (Else_Expr) then
1945 Warn_On_Null_Expression_And_Rewrite (Else_Expr);
1946 end if;
1947 end Check_Cond_Expr;
1949 ----------------
1950 -- Check_Expr --
1951 ----------------
1953 procedure Check_Expr (Expr : Node_Id) is
1954 begin
1955 if Known_Null (Expr) then
1956 Warn_On_Null_Expression_And_Rewrite (Expr);
1958 elsif Nkind (Expr) = N_If_Expression then
1959 Check_Cond_Expr (Expr);
1961 elsif Nkind (Expr) = N_Case_Expression then
1962 Check_Case_Expr (Expr);
1963 end if;
1964 end Check_Expr;
1966 -----------------------------------------
1967 -- Warn_On_Null_Expression_And_Rewrite --
1968 -----------------------------------------
1970 procedure Warn_On_Null_Expression_And_Rewrite (Null_Expr : Node_Id) is
1971 begin
1972 Error_Msg_N
1973 ("(Ada 2005) NULL not allowed in null-excluding component??",
1974 Null_Expr);
1975 Error_Msg_N
1976 ("\Constraint_Error might be raised at run time??", Null_Expr);
1978 -- We cannot use Apply_Compile_Time_Constraint_Error because in
1979 -- some cases the components are rewritten and the runtime error
1980 -- would be missed.
1982 Rewrite (Null_Expr,
1983 Make_Raise_Constraint_Error (Sloc (Null_Expr),
1984 Reason => CE_Access_Check_Failed));
1986 Set_Etype (Null_Expr, Comp_Typ);
1987 Set_Analyzed (Null_Expr);
1988 end Warn_On_Null_Expression_And_Rewrite;
1990 -- Start of processing for Warn_On_Null_Component_Association
1992 begin
1993 pragma Assert (Can_Never_Be_Null (Comp_Typ));
1995 case Nkind (Expr) is
1996 when N_If_Expression =>
1997 Check_Cond_Expr (Expr);
1999 when N_Case_Expression =>
2000 Check_Case_Expr (Expr);
2002 when others =>
2003 pragma Assert (False);
2004 null;
2005 end case;
2006 end Warn_On_Null_Component_Association;
2008 -- Local variables
2010 Assoc : Node_Id;
2011 Choice : Node_Id;
2012 Expr : Node_Id;
2013 Discard : Node_Id;
2015 Aggr_Low : Node_Id := Empty;
2016 Aggr_High : Node_Id := Empty;
2017 -- The actual low and high bounds of this sub-aggregate
2019 Case_Table_Size : Nat;
2020 -- Contains the size of the case table needed to sort aggregate choices
2022 Choices_Low : Node_Id := Empty;
2023 Choices_High : Node_Id := Empty;
2024 -- The lowest and highest discrete choices values for a named aggregate
2026 Delete_Choice : Boolean;
2027 -- Used when replacing a subtype choice with predicate by a list
2029 Has_Iterator_Specifications : Boolean := False;
2030 -- Flag to indicate that all named associations are iterated component
2031 -- associations with iterator specifications, in which case the
2032 -- expansion will create two loops: one to evaluate the size and one
2033 -- to generate the elements (4.3.3 (20.2/5)).
2035 Nb_Elements : Uint := Uint_0;
2036 -- The number of elements in a positional aggregate
2038 Nb_Discrete_Choices : Nat := 0;
2039 -- The overall number of discrete choices (not counting others choice)
2041 -- Start of processing for Resolve_Array_Aggregate
2043 begin
2044 -- Ignore junk empty aggregate resulting from parser error
2046 if No (Expressions (N))
2047 and then No (Component_Associations (N))
2048 and then not Null_Record_Present (N)
2049 then
2050 return Failure;
2051 end if;
2053 -- Disable the warning for GNAT Mode to allow for easier transition.
2055 if Ada_Version_Explicit >= Ada_2022
2056 and then Warn_On_Obsolescent_Feature
2057 and then not GNAT_Mode
2058 and then not Is_Homogeneous_Aggregate (N)
2059 and then not Is_Enum_Array_Aggregate (N)
2060 and then Is_Parenthesis_Aggregate (N)
2061 and then Nkind (Parent (N)) /= N_Qualified_Expression
2062 and then Comes_From_Source (N)
2063 then
2064 Error_Msg_N
2065 ("?j?array aggregate using () is an" &
2066 " obsolescent syntax, use '['] instead", N);
2067 end if;
2069 -- STEP 1: make sure the aggregate is correctly formatted
2071 if Present (Component_Associations (N)) then
2073 -- Verify that all or none of the component associations
2074 -- include an iterator specification.
2076 Assoc := First (Component_Associations (N));
2077 if Nkind (Assoc) = N_Iterated_Component_Association
2078 and then Present (Iterator_Specification (Assoc))
2079 then
2080 -- All other component associations must have an iterator spec.
2082 Next (Assoc);
2083 while Present (Assoc) loop
2084 if Nkind (Assoc) /= N_Iterated_Component_Association
2085 or else No (Iterator_Specification (Assoc))
2086 then
2087 Error_Msg_N ("mixed iterated component association"
2088 & " (RM 4.3.3 (17.1/5))",
2089 Assoc);
2090 return Failure;
2091 end if;
2093 Next (Assoc);
2094 end loop;
2096 Has_Iterator_Specifications := True;
2098 else
2099 -- or none of them do.
2101 Next (Assoc);
2102 while Present (Assoc) loop
2103 if Nkind (Assoc) = N_Iterated_Component_Association
2104 and then Present (Iterator_Specification (Assoc))
2105 then
2106 Error_Msg_N ("mixed iterated component association"
2107 & " (RM 4.3.3 (17.1/5))",
2108 Assoc);
2109 return Failure;
2110 end if;
2112 Next (Assoc);
2113 end loop;
2115 end if;
2117 Assoc := First (Component_Associations (N));
2118 while Present (Assoc) loop
2119 if Nkind (Assoc) = N_Iterated_Component_Association then
2120 Resolve_Iterated_Component_Association (Assoc, Index_Typ);
2122 elsif Nkind (Assoc) /= N_Component_Association then
2123 Error_Msg_N
2124 ("invalid component association for aggregate", Assoc);
2125 return Failure;
2126 end if;
2128 Choice := First (Choice_List (Assoc));
2129 Delete_Choice := False;
2130 while Present (Choice) loop
2131 if Nkind (Choice) = N_Others_Choice then
2132 Others_Present := True;
2134 if Choice /= First (Choice_List (Assoc))
2135 or else Present (Next (Choice))
2136 then
2137 Error_Msg_N
2138 ("OTHERS must appear alone in a choice list", Choice);
2139 return Failure;
2140 end if;
2142 if Present (Next (Assoc)) then
2143 Error_Msg_N
2144 ("OTHERS must appear last in an aggregate", Choice);
2145 return Failure;
2146 end if;
2148 if Ada_Version = Ada_83
2149 and then Assoc /= First (Component_Associations (N))
2150 and then Nkind (Parent (N)) in
2151 N_Assignment_Statement | N_Object_Declaration
2152 then
2153 Error_Msg_N
2154 ("(Ada 83) illegal context for OTHERS choice", N);
2155 end if;
2157 elsif Is_Entity_Name (Choice) then
2158 Analyze (Choice);
2160 declare
2161 E : constant Entity_Id := Entity (Choice);
2162 New_Cs : List_Id;
2163 P : Node_Id;
2164 C : Node_Id;
2166 begin
2167 if Is_Type (E) and then Has_Predicates (E) then
2168 Freeze_Before (N, E);
2170 if Has_Dynamic_Predicate_Aspect (E)
2171 or else Has_Ghost_Predicate_Aspect (E)
2172 then
2173 Error_Msg_NE
2174 ("subtype& has non-static predicate, not allowed "
2175 & "in aggregate choice", Choice, E);
2177 elsif not Is_OK_Static_Subtype (E) then
2178 Error_Msg_NE
2179 ("non-static subtype& has predicate, not allowed "
2180 & "in aggregate choice", Choice, E);
2181 end if;
2183 -- If the subtype has a static predicate, replace the
2184 -- original choice with the list of individual values
2185 -- covered by the predicate.
2186 -- This should be deferred to expansion time ???
2188 if Present (Static_Discrete_Predicate (E)) then
2189 Delete_Choice := True;
2191 New_Cs := New_List;
2192 P := First (Static_Discrete_Predicate (E));
2193 while Present (P) loop
2194 C := New_Copy (P);
2195 Set_Sloc (C, Sloc (Choice));
2196 Append_To (New_Cs, C);
2197 Next (P);
2198 end loop;
2200 Insert_List_After (Choice, New_Cs);
2201 end if;
2202 end if;
2203 end;
2204 end if;
2206 Nb_Choices := Nb_Choices + 1;
2208 declare
2209 C : constant Node_Id := Choice;
2211 begin
2212 Next (Choice);
2214 if Delete_Choice then
2215 Remove (C);
2216 Nb_Choices := Nb_Choices - 1;
2217 Delete_Choice := False;
2218 end if;
2219 end;
2220 end loop;
2222 Next (Assoc);
2223 end loop;
2224 end if;
2226 -- At this point we know that the others choice, if present, is by
2227 -- itself and appears last in the aggregate. Check if we have mixed
2228 -- positional and discrete associations (other than the others choice).
2230 if Present (Expressions (N))
2231 and then (Nb_Choices > 1
2232 or else (Nb_Choices = 1 and then not Others_Present))
2233 then
2234 Error_Msg_N
2235 ("cannot mix named and positional associations in array aggregate",
2236 First (Choice_List (First (Component_Associations (N)))));
2237 return Failure;
2238 end if;
2240 -- Test for the validity of an others choice if present
2242 if Others_Present and then not Others_Allowed then
2243 declare
2244 Others_N : constant Node_Id :=
2245 First (Choice_List (First (Component_Associations (N))));
2246 begin
2247 Error_Msg_N ("OTHERS choice not allowed here", Others_N);
2248 Error_Msg_N ("\qualify the aggregate with a constrained subtype "
2249 & "to provide bounds for it", Others_N);
2250 return Failure;
2251 end;
2252 end if;
2254 -- Protect against cascaded errors
2256 if Etype (Index_Typ) = Any_Type then
2257 return Failure;
2258 end if;
2260 -- STEP 2: Process named components
2262 if No (Expressions (N)) then
2263 if Others_Present then
2264 Case_Table_Size := Nb_Choices - 1;
2265 else
2266 Case_Table_Size := Nb_Choices;
2267 end if;
2269 Step_2 : declare
2270 function Empty_Range (A : Node_Id) return Boolean;
2271 -- If an association covers an empty range, some warnings on the
2272 -- expression of the association can be disabled.
2274 -----------------
2275 -- Empty_Range --
2276 -----------------
2278 function Empty_Range (A : Node_Id) return Boolean is
2279 R : Node_Id;
2281 begin
2282 if Nkind (A) = N_Iterated_Component_Association then
2283 R := First (Discrete_Choices (A));
2284 else
2285 R := First (Choices (A));
2286 end if;
2288 return No (Next (R))
2289 and then Nkind (R) = N_Range
2290 and then Compile_Time_Compare
2291 (Low_Bound (R), High_Bound (R), False) = GT;
2292 end Empty_Range;
2294 -- Local variables
2296 Low : Node_Id;
2297 High : Node_Id;
2298 -- Denote the lowest and highest values in an aggregate choice
2300 S_Low : Node_Id := Empty;
2301 S_High : Node_Id := Empty;
2302 -- if a choice in an aggregate is a subtype indication these
2303 -- denote the lowest and highest values of the subtype
2305 Table : Case_Table_Type (1 .. Case_Table_Size);
2306 -- Used to sort all the different choice values
2308 Single_Choice : Boolean;
2309 -- Set to true every time there is a single discrete choice in a
2310 -- discrete association
2312 Prev_Nb_Discrete_Choices : Nat;
2313 -- Used to keep track of the number of discrete choices in the
2314 -- current association.
2316 Errors_Posted_On_Choices : Boolean := False;
2317 -- Keeps track of whether any choices have semantic errors
2319 -- Start of processing for Step_2
2321 begin
2322 -- STEP 2 (A): Check discrete choices validity
2323 -- No need if this is an element iteration.
2325 Assoc := First (Component_Associations (N));
2326 while Present (Assoc)
2327 and then Present (Choice_List (Assoc))
2328 loop
2329 Prev_Nb_Discrete_Choices := Nb_Discrete_Choices;
2330 Choice := First (Choice_List (Assoc));
2332 loop
2333 Analyze (Choice);
2335 if Nkind (Choice) = N_Others_Choice then
2336 Single_Choice := False;
2337 exit;
2339 -- Test for subtype mark without constraint
2341 elsif Is_Entity_Name (Choice) and then
2342 Is_Type (Entity (Choice))
2343 then
2344 if Base_Type (Entity (Choice)) /= Index_Base then
2345 Error_Msg_N
2346 ("invalid subtype mark in aggregate choice",
2347 Choice);
2348 return Failure;
2349 end if;
2351 -- Case of subtype indication
2353 elsif Nkind (Choice) = N_Subtype_Indication then
2354 Resolve_Discrete_Subtype_Indication (Choice, Index_Base);
2356 if Has_Dynamic_Predicate_Aspect
2357 (Entity (Subtype_Mark (Choice)))
2358 or else Has_Ghost_Predicate_Aspect
2359 (Entity (Subtype_Mark (Choice)))
2360 then
2361 Error_Msg_NE
2362 ("subtype& has non-static predicate, "
2363 & "not allowed in aggregate choice",
2364 Choice, Entity (Subtype_Mark (Choice)));
2365 end if;
2367 -- Does the subtype indication evaluation raise CE?
2369 Get_Index_Bounds (Subtype_Mark (Choice), S_Low, S_High);
2370 Get_Index_Bounds (Choice, Low, High);
2371 Check_Bounds (S_Low, S_High, Low, High);
2373 -- Case of range or expression
2375 else
2376 Resolve (Choice, Index_Base);
2377 Check_Unset_Reference (Choice);
2378 Check_Non_Static_Context (Choice);
2380 -- If semantic errors were posted on the choice, then
2381 -- record that for possible early return from later
2382 -- processing (see handling of enumeration choices).
2384 if Error_Posted (Choice) then
2385 Errors_Posted_On_Choices := True;
2386 end if;
2388 -- Do not range check a choice. This check is redundant
2389 -- since this test is already done when we check that the
2390 -- bounds of the array aggregate are within range.
2392 Set_Do_Range_Check (Choice, False);
2393 end if;
2395 -- If we could not resolve the discrete choice stop here
2397 if Etype (Choice) = Any_Type then
2398 return Failure;
2400 -- If the discrete choice raises CE get its original bounds
2402 elsif Nkind (Choice) = N_Raise_Constraint_Error then
2403 Set_Raises_Constraint_Error (N);
2404 Get_Index_Bounds (Original_Node (Choice), Low, High);
2406 -- Otherwise get its bounds as usual
2408 else
2409 Get_Index_Bounds (Choice, Low, High);
2410 end if;
2412 if (Dynamic_Or_Null_Range (Low, High)
2413 or else (Nkind (Choice) = N_Subtype_Indication
2414 and then
2415 Dynamic_Or_Null_Range (S_Low, S_High)))
2416 and then Nb_Choices /= 1
2417 then
2418 Error_Msg_N
2419 ("dynamic or empty choice in aggregate "
2420 & "must be the only choice", Choice);
2421 return Failure;
2422 end if;
2424 if not (All_Composite_Constraints_Static (Low)
2425 and then All_Composite_Constraints_Static (High)
2426 and then All_Composite_Constraints_Static (S_Low)
2427 and then All_Composite_Constraints_Static (S_High))
2428 then
2429 Check_Restriction (No_Dynamic_Sized_Objects, Choice);
2430 end if;
2432 Nb_Discrete_Choices := Nb_Discrete_Choices + 1;
2433 Table (Nb_Discrete_Choices).Lo := Low;
2434 Table (Nb_Discrete_Choices).Hi := High;
2435 Table (Nb_Discrete_Choices).Choice := Choice;
2437 Next (Choice);
2439 if No (Choice) then
2441 -- Check if we have a single discrete choice and whether
2442 -- this discrete choice specifies a single value.
2444 Single_Choice :=
2445 Nb_Discrete_Choices = Prev_Nb_Discrete_Choices + 1
2446 and then Low = High;
2448 exit;
2449 end if;
2450 end loop;
2452 -- Ada 2005 (AI-231)
2454 if Ada_Version >= Ada_2005
2455 and then not Empty_Range (Assoc)
2456 then
2457 if Known_Null (Expression (Assoc)) then
2458 Check_Can_Never_Be_Null (Etype (N), Expression (Assoc));
2460 -- Report warning on iterated component association that may
2461 -- initialize some component of an array of null-excluding
2462 -- access type components with a null value. For example:
2464 -- type AList is array (...) of not null access Integer;
2465 -- L : AList :=
2466 -- [for J in A'Range =>
2467 -- (if Func (J) = 0 then A(J)'Access else Null)];
2469 elsif Ada_Version >= Ada_2022
2470 and then Can_Never_Be_Null (Component_Type (Etype (N)))
2471 and then Nkind (Assoc) = N_Iterated_Component_Association
2472 and then Nkind (Expression (Assoc)) in N_If_Expression
2473 | N_Case_Expression
2474 then
2475 Warn_On_Null_Component_Association (Expression (Assoc));
2476 end if;
2477 end if;
2479 -- Ada 2005 (AI-287): In case of default initialized component
2480 -- we delay the resolution to the expansion phase.
2482 if Box_Present (Assoc) then
2484 -- Ada 2005 (AI-287): In case of default initialization of a
2485 -- component the expander will generate calls to the
2486 -- corresponding initialization subprogram. We need to call
2487 -- Resolve_Aggr_Expr to check the rules about
2488 -- dimensionality.
2490 if not Resolve_Aggr_Expr
2491 (Assoc, Single_Elmt => Single_Choice)
2492 then
2493 return Failure;
2494 end if;
2496 -- ??? Checks for dynamically tagged expressions below will
2497 -- be only applied to iterated_component_association after
2498 -- expansion; in particular, errors might not be reported when
2499 -- -gnatc switch is used.
2501 elsif Nkind (Assoc) = N_Iterated_Component_Association then
2502 null; -- handled above, in a loop context
2504 elsif not Resolve_Aggr_Expr
2505 (Expression (Assoc), Single_Elmt => Single_Choice)
2506 then
2507 return Failure;
2509 -- Check incorrect use of dynamically tagged expression
2511 -- We differentiate here two cases because the expression may
2512 -- not be decorated. For example, the analysis and resolution
2513 -- of the expression associated with the others choice will be
2514 -- done later with the full aggregate. In such case we
2515 -- duplicate the expression tree to analyze the copy and
2516 -- perform the required check.
2518 elsif No (Etype (Expression (Assoc))) then
2519 declare
2520 Save_Analysis : constant Boolean := Full_Analysis;
2521 Expr : constant Node_Id :=
2522 New_Copy_Tree (Expression (Assoc));
2524 begin
2525 Expander_Mode_Save_And_Set (False);
2526 Full_Analysis := False;
2528 -- Analyze the expression, making sure it is properly
2529 -- attached to the tree before we do the analysis.
2531 Set_Parent (Expr, Parent (Expression (Assoc)));
2532 Analyze (Expr);
2534 -- Compute its dimensions now, rather than at the end of
2535 -- resolution, because in the case of multidimensional
2536 -- aggregates subsequent expansion may lead to spurious
2537 -- errors.
2539 Check_Expression_Dimensions (Expr, Component_Typ);
2541 -- If the expression is a literal, propagate this info
2542 -- to the expression in the association, to enable some
2543 -- optimizations downstream.
2545 if Is_Entity_Name (Expr)
2546 and then Present (Entity (Expr))
2547 and then Ekind (Entity (Expr)) = E_Enumeration_Literal
2548 then
2549 Analyze_And_Resolve
2550 (Expression (Assoc), Component_Typ);
2551 end if;
2553 Full_Analysis := Save_Analysis;
2554 Expander_Mode_Restore;
2556 if Is_Tagged_Type (Etype (Expr)) then
2557 Check_Dynamically_Tagged_Expression
2558 (Expr => Expr,
2559 Typ => Component_Type (Etype (N)),
2560 Related_Nod => N);
2561 end if;
2562 end;
2564 elsif Is_Tagged_Type (Etype (Expression (Assoc))) then
2565 Check_Dynamically_Tagged_Expression
2566 (Expr => Expression (Assoc),
2567 Typ => Component_Type (Etype (N)),
2568 Related_Nod => N);
2569 end if;
2571 Next (Assoc);
2572 end loop;
2574 -- If aggregate contains more than one choice then these must be
2575 -- static. Check for duplicate and missing values.
2577 -- Note: there is duplicated code here wrt Check_Choice_Set in
2578 -- the body of Sem_Case, and it is possible we could just reuse
2579 -- that procedure. To be checked ???
2581 if Nb_Discrete_Choices > 1 then
2582 Check_Choices : declare
2583 Choice : Node_Id;
2584 -- Location of choice for messages
2586 Hi_Val : Uint;
2587 Lo_Val : Uint;
2588 -- High end of one range and Low end of the next. Should be
2589 -- contiguous if there is no hole in the list of values.
2591 Lo_Dup : Uint;
2592 Hi_Dup : Uint;
2593 -- End points of duplicated range
2595 Missing_Or_Duplicates : Boolean := False;
2596 -- Set True if missing or duplicate choices found
2598 procedure Output_Bad_Choices (Lo, Hi : Uint; C : Node_Id);
2599 -- Output continuation message with a representation of the
2600 -- bounds (just Lo if Lo = Hi, else Lo .. Hi). C is the
2601 -- choice node where the message is to be posted.
2603 ------------------------
2604 -- Output_Bad_Choices --
2605 ------------------------
2607 procedure Output_Bad_Choices (Lo, Hi : Uint; C : Node_Id) is
2608 begin
2609 -- Enumeration type case
2611 if Is_Enumeration_Type (Index_Typ) then
2612 Error_Msg_Name_1 :=
2613 Chars (Get_Enum_Lit_From_Pos (Index_Typ, Lo, Loc));
2614 Error_Msg_Name_2 :=
2615 Chars (Get_Enum_Lit_From_Pos (Index_Typ, Hi, Loc));
2617 if Lo = Hi then
2618 Error_Msg_N ("\\ %!", C);
2619 else
2620 Error_Msg_N ("\\ % .. %!", C);
2621 end if;
2623 -- Integer types case
2625 else
2626 Error_Msg_Uint_1 := Lo;
2627 Error_Msg_Uint_2 := Hi;
2629 if Lo = Hi then
2630 Error_Msg_N ("\\ ^!", C);
2631 else
2632 Error_Msg_N ("\\ ^ .. ^!", C);
2633 end if;
2634 end if;
2635 end Output_Bad_Choices;
2637 -- Start of processing for Check_Choices
2639 begin
2640 Sort_Case_Table (Table);
2642 -- First we do a quick linear loop to find out if we have
2643 -- any duplicates or missing entries (usually we have a
2644 -- legal aggregate, so this will get us out quickly).
2646 for J in 1 .. Nb_Discrete_Choices - 1 loop
2647 Hi_Val := Expr_Value (Table (J).Hi);
2648 Lo_Val := Expr_Value (Table (J + 1).Lo);
2650 if Lo_Val <= Hi_Val
2651 or else (Lo_Val > Hi_Val + 1
2652 and then not Others_Present)
2653 then
2654 Missing_Or_Duplicates := True;
2655 exit;
2656 end if;
2657 end loop;
2659 -- If we have missing or duplicate entries, first fill in
2660 -- the Highest entries to make life easier in the following
2661 -- loops to detect bad entries.
2663 if Missing_Or_Duplicates then
2664 Table (1).Highest := Expr_Value (Table (1).Hi);
2666 for J in 2 .. Nb_Discrete_Choices loop
2667 Table (J).Highest :=
2668 UI_Max
2669 (Table (J - 1).Highest, Expr_Value (Table (J).Hi));
2670 end loop;
2672 -- Loop through table entries to find duplicate indexes
2674 for J in 2 .. Nb_Discrete_Choices loop
2675 Lo_Val := Expr_Value (Table (J).Lo);
2676 Hi_Val := Expr_Value (Table (J).Hi);
2678 -- Case where we have duplicates (the lower bound of
2679 -- this choice is less than or equal to the highest
2680 -- high bound found so far).
2682 if Lo_Val <= Table (J - 1).Highest then
2684 -- We move backwards looking for duplicates. We can
2685 -- abandon this loop as soon as we reach a choice
2686 -- highest value that is less than Lo_Val.
2688 for K in reverse 1 .. J - 1 loop
2689 exit when Table (K).Highest < Lo_Val;
2691 -- Here we may have duplicates between entries
2692 -- for K and J. Get range of duplicates.
2694 Lo_Dup :=
2695 UI_Max (Lo_Val, Expr_Value (Table (K).Lo));
2696 Hi_Dup :=
2697 UI_Min (Hi_Val, Expr_Value (Table (K).Hi));
2699 -- Nothing to do if duplicate range is null
2701 if Lo_Dup > Hi_Dup then
2702 null;
2704 -- Otherwise place proper message
2706 else
2707 -- We place message on later choice, with a
2708 -- line reference to the earlier choice.
2710 if Sloc (Table (J).Choice) <
2711 Sloc (Table (K).Choice)
2712 then
2713 Choice := Table (K).Choice;
2714 Error_Msg_Sloc := Sloc (Table (J).Choice);
2715 else
2716 Choice := Table (J).Choice;
2717 Error_Msg_Sloc := Sloc (Table (K).Choice);
2718 end if;
2720 if Lo_Dup = Hi_Dup then
2721 Error_Msg_N
2722 ("index value in array aggregate "
2723 & "duplicates the one given#!", Choice);
2724 else
2725 Error_Msg_N
2726 ("index values in array aggregate "
2727 & "duplicate those given#!", Choice);
2728 end if;
2730 Output_Bad_Choices (Lo_Dup, Hi_Dup, Choice);
2731 end if;
2732 end loop;
2733 end if;
2734 end loop;
2736 -- Loop through entries in table to find missing indexes.
2737 -- Not needed if others, since missing impossible.
2739 if not Others_Present then
2740 for J in 2 .. Nb_Discrete_Choices loop
2741 Lo_Val := Expr_Value (Table (J).Lo);
2742 Hi_Val := Table (J - 1).Highest;
2744 if Lo_Val > Hi_Val + 1 then
2746 declare
2747 Error_Node : Node_Id;
2749 begin
2750 -- If the choice is the bound of a range in
2751 -- a subtype indication, it is not in the
2752 -- source lists for the aggregate itself, so
2753 -- post the error on the aggregate. Otherwise
2754 -- post it on choice itself.
2756 Choice := Table (J).Choice;
2758 if Is_List_Member (Choice) then
2759 Error_Node := Choice;
2760 else
2761 Error_Node := N;
2762 end if;
2764 if Hi_Val + 1 = Lo_Val - 1 then
2765 Error_Msg_N
2766 ("missing index value "
2767 & "in array aggregate!", Error_Node);
2768 else
2769 Error_Msg_N
2770 ("missing index values "
2771 & "in array aggregate!", Error_Node);
2772 end if;
2774 Output_Bad_Choices
2775 (Hi_Val + 1, Lo_Val - 1, Error_Node);
2776 end;
2777 end if;
2778 end loop;
2779 end if;
2781 -- If either missing or duplicate values, return failure
2783 Set_Etype (N, Any_Composite);
2784 return Failure;
2785 end if;
2786 end Check_Choices;
2787 end if;
2789 if Has_Iterator_Specifications then
2790 -- Bounds will be determined dynamically.
2792 return Success;
2793 end if;
2795 -- STEP 2 (B): Compute aggregate bounds and min/max choices values
2797 if Nb_Discrete_Choices > 0 then
2798 Choices_Low := Table (1).Lo;
2799 Choices_High := Table (Nb_Discrete_Choices).Hi;
2800 end if;
2802 -- If Others is present, then bounds of aggregate come from the
2803 -- index constraint (not the choices in the aggregate itself).
2805 if Others_Present then
2806 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2808 -- Abandon processing if either bound is already signalled as
2809 -- an error (prevents junk cascaded messages and blow ups).
2811 if Nkind (Aggr_Low) = N_Error
2812 or else
2813 Nkind (Aggr_High) = N_Error
2814 then
2815 return False;
2816 end if;
2818 -- No others clause present
2820 else
2821 -- Special processing if others allowed and not present. This
2822 -- means that the bounds of the aggregate come from the index
2823 -- constraint (and the length must match).
2825 if Others_Allowed then
2826 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2828 -- Abandon processing if either bound is already signalled
2829 -- as an error (stop junk cascaded messages and blow ups).
2831 if Nkind (Aggr_Low) = N_Error
2832 or else
2833 Nkind (Aggr_High) = N_Error
2834 then
2835 return False;
2836 end if;
2838 -- If others allowed, and no others present, then the array
2839 -- should cover all index values. If it does not, we will
2840 -- get a length check warning, but there is two cases where
2841 -- an additional warning is useful:
2843 -- If we have no positional components, and the length is
2844 -- wrong (which we can tell by others being allowed with
2845 -- missing components), and the index type is an enumeration
2846 -- type, then issue appropriate warnings about these missing
2847 -- components. They are only warnings, since the aggregate
2848 -- is fine, it's just the wrong length. We skip this check
2849 -- for standard character types (since there are no literals
2850 -- and it is too much trouble to concoct them), and also if
2851 -- any of the bounds have values that are not known at
2852 -- compile time.
2854 -- Another case warranting a warning is when the length
2855 -- is right, but as above we have an index type that is
2856 -- an enumeration, and the bounds do not match. This is a
2857 -- case where dubious sliding is allowed and we generate a
2858 -- warning that the bounds do not match.
2860 if No (Expressions (N))
2861 and then Nkind (Index) = N_Range
2862 and then Is_Enumeration_Type (Etype (Index))
2863 and then not Is_Standard_Character_Type (Etype (Index))
2864 and then Compile_Time_Known_Value (Aggr_Low)
2865 and then Compile_Time_Known_Value (Aggr_High)
2866 and then Compile_Time_Known_Value (Choices_Low)
2867 and then Compile_Time_Known_Value (Choices_High)
2868 then
2869 -- If any of the expressions or range bounds in choices
2870 -- have semantic errors, then do not attempt further
2871 -- resolution, to prevent cascaded errors.
2873 if Errors_Posted_On_Choices then
2874 return Failure;
2875 end if;
2877 declare
2878 ALo : constant Node_Id := Expr_Value_E (Aggr_Low);
2879 AHi : constant Node_Id := Expr_Value_E (Aggr_High);
2880 CLo : constant Node_Id := Expr_Value_E (Choices_Low);
2881 CHi : constant Node_Id := Expr_Value_E (Choices_High);
2883 Ent : Entity_Id;
2885 begin
2886 -- Warning case 1, missing values at start/end. Only
2887 -- do the check if the number of entries is too small.
2889 if (Enumeration_Pos (CHi) - Enumeration_Pos (CLo))
2891 (Enumeration_Pos (AHi) - Enumeration_Pos (ALo))
2892 then
2893 Error_Msg_N
2894 ("missing index value(s) in array aggregate??",
2897 -- Output missing value(s) at start
2899 if Chars (ALo) /= Chars (CLo) then
2900 Ent := Prev (CLo);
2902 if Chars (ALo) = Chars (Ent) then
2903 Error_Msg_Name_1 := Chars (ALo);
2904 Error_Msg_N ("\ %??", N);
2905 else
2906 Error_Msg_Name_1 := Chars (ALo);
2907 Error_Msg_Name_2 := Chars (Ent);
2908 Error_Msg_N ("\ % .. %??", N);
2909 end if;
2910 end if;
2912 -- Output missing value(s) at end
2914 if Chars (AHi) /= Chars (CHi) then
2915 Ent := Next (CHi);
2917 if Chars (AHi) = Chars (Ent) then
2918 Error_Msg_Name_1 := Chars (Ent);
2919 Error_Msg_N ("\ %??", N);
2920 else
2921 Error_Msg_Name_1 := Chars (Ent);
2922 Error_Msg_Name_2 := Chars (AHi);
2923 Error_Msg_N ("\ % .. %??", N);
2924 end if;
2925 end if;
2927 -- Warning case 2, dubious sliding. The First_Subtype
2928 -- test distinguishes between a constrained type where
2929 -- sliding is not allowed (so we will get a warning
2930 -- later that Constraint_Error will be raised), and
2931 -- the unconstrained case where sliding is permitted.
2933 elsif (Enumeration_Pos (CHi) - Enumeration_Pos (CLo))
2935 (Enumeration_Pos (AHi) - Enumeration_Pos (ALo))
2936 and then Chars (ALo) /= Chars (CLo)
2937 and then
2938 not Is_Constrained (First_Subtype (Etype (N)))
2939 then
2940 Error_Msg_N
2941 ("bounds of aggregate do not match target??", N);
2942 end if;
2943 end;
2944 end if;
2945 end if;
2947 -- If no others, aggregate bounds come from aggregate
2949 Aggr_Low := Choices_Low;
2950 Aggr_High := Choices_High;
2951 end if;
2952 end Step_2;
2954 -- STEP 3: Process positional components
2956 else
2957 -- STEP 3 (A): Process positional elements
2959 Expr := First (Expressions (N));
2960 Nb_Elements := Uint_0;
2961 while Present (Expr) loop
2962 Nb_Elements := Nb_Elements + 1;
2964 -- Ada 2005 (AI-231)
2966 if Ada_Version >= Ada_2005 and then Known_Null (Expr) then
2967 Check_Can_Never_Be_Null (Etype (N), Expr);
2968 end if;
2970 if not Resolve_Aggr_Expr (Expr, Single_Elmt => True) then
2971 return Failure;
2972 end if;
2974 -- Check incorrect use of dynamically tagged expression
2976 if Is_Tagged_Type (Etype (Expr)) then
2977 Check_Dynamically_Tagged_Expression
2978 (Expr => Expr,
2979 Typ => Component_Type (Etype (N)),
2980 Related_Nod => N);
2981 end if;
2983 Next (Expr);
2984 end loop;
2986 if Others_Present then
2987 Assoc := Last (Component_Associations (N));
2989 -- Ada 2005 (AI-231)
2991 if Ada_Version >= Ada_2005 and then Known_Null (Assoc) then
2992 Check_Can_Never_Be_Null (Etype (N), Expression (Assoc));
2993 end if;
2995 -- Ada 2005 (AI-287): In case of default initialized component,
2996 -- we delay the resolution to the expansion phase.
2998 if Box_Present (Assoc) then
3000 -- Ada 2005 (AI-287): In case of default initialization of a
3001 -- component the expander will generate calls to the
3002 -- corresponding initialization subprogram. We need to call
3003 -- Resolve_Aggr_Expr to check the rules about
3004 -- dimensionality.
3006 if not Resolve_Aggr_Expr (Assoc, Single_Elmt => False) then
3007 return Failure;
3008 end if;
3010 elsif not Resolve_Aggr_Expr (Expression (Assoc),
3011 Single_Elmt => False)
3012 then
3013 return Failure;
3015 -- Check incorrect use of dynamically tagged expression. The
3016 -- expression of the others choice has not been resolved yet.
3017 -- In order to diagnose the semantic error we create a duplicate
3018 -- tree to analyze it and perform the check.
3020 elsif Nkind (Assoc) /= N_Iterated_Component_Association then
3021 declare
3022 Save_Analysis : constant Boolean := Full_Analysis;
3023 Expr : constant Node_Id :=
3024 New_Copy_Tree (Expression (Assoc));
3026 begin
3027 Expander_Mode_Save_And_Set (False);
3028 Full_Analysis := False;
3029 Analyze (Expr);
3030 Full_Analysis := Save_Analysis;
3031 Expander_Mode_Restore;
3033 if Is_Tagged_Type (Etype (Expr)) then
3034 Check_Dynamically_Tagged_Expression
3035 (Expr => Expr,
3036 Typ => Component_Type (Etype (N)),
3037 Related_Nod => N);
3038 end if;
3039 end;
3040 end if;
3041 end if;
3043 -- STEP 3 (B): Compute the aggregate bounds
3045 if Others_Present then
3046 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
3048 else
3049 if Others_Allowed then
3050 Get_Index_Bounds (Index_Constr, Aggr_Low, Discard);
3051 else
3052 Aggr_Low := Index_Typ_Low;
3053 end if;
3055 Aggr_High := Add (Nb_Elements - 1, To => Aggr_Low);
3056 Check_Bound (Index_Base_High, Aggr_High);
3057 end if;
3058 end if;
3060 -- STEP 4: Perform static aggregate checks and save the bounds
3062 -- Check (A)
3064 Check_Bounds (Index_Typ_Low, Index_Typ_High, Aggr_Low, Aggr_High);
3065 Check_Bounds (Index_Base_Low, Index_Base_High, Aggr_Low, Aggr_High);
3067 -- Check (B)
3069 if Others_Present and then Nb_Discrete_Choices > 0 then
3070 Check_Bounds (Aggr_Low, Aggr_High, Choices_Low, Choices_High);
3071 Check_Bounds (Index_Typ_Low, Index_Typ_High,
3072 Choices_Low, Choices_High);
3073 Check_Bounds (Index_Base_Low, Index_Base_High,
3074 Choices_Low, Choices_High);
3076 -- Check (C)
3078 elsif Others_Present and then Nb_Elements > 0 then
3079 Check_Length (Aggr_Low, Aggr_High, Nb_Elements);
3080 Check_Length (Index_Typ_Low, Index_Typ_High, Nb_Elements);
3081 Check_Length (Index_Base_Low, Index_Base_High, Nb_Elements);
3082 end if;
3084 if Raises_Constraint_Error (Aggr_Low)
3085 or else Raises_Constraint_Error (Aggr_High)
3086 then
3087 Set_Raises_Constraint_Error (N);
3088 end if;
3090 Aggr_Low := Duplicate_Subexpr (Aggr_Low);
3092 -- Do not duplicate Aggr_High if Aggr_High = Aggr_Low + Nb_Elements
3093 -- since the addition node returned by Add is not yet analyzed. Attach
3094 -- to tree and analyze first. Reset analyzed flag to ensure it will get
3095 -- analyzed when it is a literal bound whose type must be properly set.
3097 if Others_Present or else Nb_Discrete_Choices > 0 then
3098 Aggr_High := Duplicate_Subexpr (Aggr_High);
3100 if Etype (Aggr_High) = Universal_Integer then
3101 Set_Analyzed (Aggr_High, False);
3102 end if;
3103 end if;
3105 -- If the aggregate already has bounds attached to it, it means this is
3106 -- a positional aggregate created as an optimization by
3107 -- Exp_Aggr.Convert_To_Positional, so we don't want to change those
3108 -- bounds.
3110 if Present (Aggregate_Bounds (N))
3111 and then not Others_Allowed
3112 and then not Comes_From_Source (N)
3113 then
3114 Aggr_Low := Low_Bound (Aggregate_Bounds (N));
3115 Aggr_High := High_Bound (Aggregate_Bounds (N));
3116 end if;
3118 Set_Aggregate_Bounds
3119 (N, Make_Range (Loc, Low_Bound => Aggr_Low, High_Bound => Aggr_High));
3121 -- The bounds may contain expressions that must be inserted upwards.
3122 -- Attach them fully to the tree. After analysis, remove side effects
3123 -- from upper bound, if still needed.
3125 Set_Parent (Aggregate_Bounds (N), N);
3126 Analyze_And_Resolve (Aggregate_Bounds (N), Index_Typ);
3127 Check_Unset_Reference (Aggregate_Bounds (N));
3129 if not Others_Present and then Nb_Discrete_Choices = 0 then
3130 Set_High_Bound
3131 (Aggregate_Bounds (N),
3132 Duplicate_Subexpr (High_Bound (Aggregate_Bounds (N))));
3133 end if;
3135 -- Check the dimensions of each component in the array aggregate
3137 Analyze_Dimension_Array_Aggregate (N, Component_Typ);
3139 return Success;
3140 end Resolve_Array_Aggregate;
3142 ---------------------------------
3143 -- Resolve_Container_Aggregate --
3144 ---------------------------------
3146 procedure Resolve_Container_Aggregate (N : Node_Id; Typ : Entity_Id) is
3147 procedure Resolve_Iterated_Association
3148 (Comp : Node_Id;
3149 Key_Type : Entity_Id;
3150 Elmt_Type : Entity_Id);
3151 -- Resolve choices and expression in an iterated component association
3152 -- or an iterated element association, which has a key_expression.
3153 -- This is similar but not identical to the handling of this construct
3154 -- in an array aggregate.
3155 -- For a named container, the type of each choice must be compatible
3156 -- with the key type. For a positional container, the choice must be
3157 -- a subtype indication or an iterator specification that determines
3158 -- an element type.
3160 Asp : constant Node_Id := Find_Value_Of_Aspect (Typ, Aspect_Aggregate);
3162 Empty_Subp : Node_Id := Empty;
3163 Add_Named_Subp : Node_Id := Empty;
3164 Add_Unnamed_Subp : Node_Id := Empty;
3165 New_Indexed_Subp : Node_Id := Empty;
3166 Assign_Indexed_Subp : Node_Id := Empty;
3168 ----------------------------------
3169 -- Resolve_Iterated_Association --
3170 ----------------------------------
3172 procedure Resolve_Iterated_Association
3173 (Comp : Node_Id;
3174 Key_Type : Entity_Id;
3175 Elmt_Type : Entity_Id)
3177 Loc : constant Source_Ptr := Sloc (N);
3178 Choice : Node_Id;
3179 Copy : Node_Id;
3180 Ent : Entity_Id;
3181 Expr : Node_Id;
3182 Key_Expr : Node_Id;
3183 Id : Entity_Id;
3184 Id_Name : Name_Id;
3185 Typ : Entity_Id := Empty;
3187 begin
3188 Error_Msg_Ada_2022_Feature ("iterated component", Loc);
3190 -- If this is an Iterated_Element_Association then either a
3191 -- an Iterator_Specification or a Loop_Parameter specification
3192 -- is present. In both cases a Key_Expression is present.
3194 if Nkind (Comp) = N_Iterated_Element_Association then
3196 -- Create a temporary scope to avoid some modifications from
3197 -- escaping the Analyze call below. The original Tree will be
3198 -- reanalyzed later.
3200 Ent := New_Internal_Entity
3201 (E_Loop, Current_Scope, Sloc (Comp), 'L');
3202 Set_Etype (Ent, Standard_Void_Type);
3203 Set_Parent (Ent, Parent (Comp));
3204 Push_Scope (Ent);
3206 if Present (Loop_Parameter_Specification (Comp)) then
3207 Copy := Copy_Separate_Tree (Comp);
3209 Analyze
3210 (Loop_Parameter_Specification (Copy));
3212 Id_Name := Chars (Defining_Identifier
3213 (Loop_Parameter_Specification (Comp)));
3214 else
3215 Copy := Copy_Separate_Tree (Iterator_Specification (Comp));
3216 Analyze (Copy);
3218 Id_Name := Chars (Defining_Identifier
3219 (Iterator_Specification (Comp)));
3220 end if;
3222 -- Key expression must have the type of the key. We analyze
3223 -- a copy of the original expression, because it will be
3224 -- reanalyzed and copied as needed during expansion of the
3225 -- corresponding loop.
3227 Key_Expr := Key_Expression (Comp);
3228 Analyze_And_Resolve (New_Copy_Tree (Key_Expr), Key_Type);
3229 End_Scope;
3231 elsif Present (Iterator_Specification (Comp)) then
3232 Copy := Copy_Separate_Tree (Iterator_Specification (Comp));
3233 Id_Name :=
3234 Chars (Defining_Identifier (Iterator_Specification (Comp)));
3236 Analyze (Copy);
3237 Typ := Etype (Defining_Identifier (Copy));
3239 else
3240 Choice := First (Discrete_Choices (Comp));
3242 while Present (Choice) loop
3243 Analyze (Choice);
3245 -- Choice can be a subtype name, a range, or an expression
3247 if Is_Entity_Name (Choice)
3248 and then Is_Type (Entity (Choice))
3249 and then Base_Type (Entity (Choice)) = Base_Type (Key_Type)
3250 then
3251 null;
3253 elsif Present (Key_Type) then
3254 Analyze_And_Resolve (Choice, Key_Type);
3256 else
3257 Typ := Etype (Choice); -- assume unique for now
3258 end if;
3260 Next (Choice);
3261 end loop;
3263 Id_Name := Chars (Defining_Identifier (Comp));
3264 end if;
3266 -- Create a scope in which to introduce an index, which is usually
3267 -- visible in the expression for the component, and needed for its
3268 -- analysis.
3270 Id := Make_Defining_Identifier (Sloc (Comp), Id_Name);
3271 Ent := New_Internal_Entity (E_Loop,
3272 Current_Scope, Sloc (Comp), 'L');
3273 Set_Etype (Ent, Standard_Void_Type);
3274 Set_Parent (Ent, Parent (Comp));
3275 Push_Scope (Ent);
3277 -- Insert and decorate the loop variable in the current scope.
3278 -- The expression has to be analyzed once the loop variable is
3279 -- directly visible. Mark the variable as referenced to prevent
3280 -- spurious warnings, given that subsequent uses of its name in the
3281 -- expression will reference the internal (synonym) loop variable.
3283 Enter_Name (Id);
3285 if No (Key_Type) then
3286 pragma Assert (Present (Typ));
3287 Set_Etype (Id, Typ);
3288 else
3289 Set_Etype (Id, Key_Type);
3290 end if;
3292 Mutate_Ekind (Id, E_Variable);
3293 Set_Is_Not_Self_Hidden (Id);
3294 Set_Scope (Id, Ent);
3295 Set_Referenced (Id);
3297 -- Analyze a copy of the expression, to verify legality. We use
3298 -- a copy because the expression will be analyzed anew when the
3299 -- enclosing aggregate is expanded, and the construct is rewritten
3300 -- as a loop with a new index variable.
3302 Expr := New_Copy_Tree (Expression (Comp));
3303 Preanalyze_And_Resolve (Expr, Elmt_Type);
3304 End_Scope;
3306 end Resolve_Iterated_Association;
3308 -- Start of processing for Resolve_Container_Aggregate
3310 begin
3311 pragma Assert (Nkind (Asp) = N_Aggregate);
3313 Set_Etype (N, Typ);
3314 Parse_Aspect_Aggregate (Asp,
3315 Empty_Subp, Add_Named_Subp, Add_Unnamed_Subp,
3316 New_Indexed_Subp, Assign_Indexed_Subp);
3318 if Present (Add_Unnamed_Subp)
3319 and then No (New_Indexed_Subp)
3320 and then Etype (Add_Unnamed_Subp) /= Any_Type
3321 then
3322 declare
3323 Elmt_Type : constant Entity_Id :=
3324 Etype (Next_Formal
3325 (First_Formal (Entity (Add_Unnamed_Subp))));
3326 Comp : Node_Id;
3328 begin
3329 if Present (Expressions (N)) then
3330 -- positional aggregate
3332 Comp := First (Expressions (N));
3333 while Present (Comp) loop
3334 Analyze_And_Resolve (Comp, Elmt_Type);
3335 Next (Comp);
3336 end loop;
3337 end if;
3339 -- Empty aggregate, to be replaced by Empty during
3340 -- expansion, or iterated component association.
3342 if Present (Component_Associations (N)) then
3343 declare
3344 Comp : Node_Id := First (Component_Associations (N));
3345 begin
3346 while Present (Comp) loop
3347 if Nkind (Comp) /=
3348 N_Iterated_Component_Association
3349 then
3350 Error_Msg_N ("illegal component association "
3351 & "for unnamed container aggregate", Comp);
3352 return;
3353 else
3354 Resolve_Iterated_Association
3355 (Comp, Empty, Elmt_Type);
3356 end if;
3358 Next (Comp);
3359 end loop;
3360 end;
3361 end if;
3362 end;
3364 elsif Present (Add_Named_Subp)
3365 and then Etype (Add_Named_Subp) /= Any_Type
3366 then
3367 declare
3368 -- Retrieves types of container, key, and element from the
3369 -- specified insertion procedure.
3371 Container : constant Entity_Id :=
3372 First_Formal (Entity (Add_Named_Subp));
3373 Key_Type : constant Entity_Id := Etype (Next_Formal (Container));
3374 Elmt_Type : constant Entity_Id :=
3375 Etype (Next_Formal (Next_Formal (Container)));
3376 Comp : Node_Id;
3377 Choice : Node_Id;
3379 begin
3380 Comp := First (Component_Associations (N));
3381 while Present (Comp) loop
3382 if Nkind (Comp) = N_Component_Association then
3383 Choice := First (Choices (Comp));
3385 while Present (Choice) loop
3386 Analyze_And_Resolve (Choice, Key_Type);
3387 if not Is_Static_Expression (Choice) then
3388 Error_Msg_N ("choice must be static", Choice);
3389 end if;
3391 Next (Choice);
3392 end loop;
3394 Analyze_And_Resolve (Expression (Comp), Elmt_Type);
3396 elsif Nkind (Comp) in
3397 N_Iterated_Component_Association |
3398 N_Iterated_Element_Association
3399 then
3400 Resolve_Iterated_Association
3401 (Comp, Key_Type, Elmt_Type);
3402 end if;
3404 Next (Comp);
3405 end loop;
3406 end;
3408 elsif Present (Assign_Indexed_Subp)
3409 and then Etype (Assign_Indexed_Subp) /= Any_Type
3410 then
3411 -- Indexed Aggregate. Positional or indexed component
3412 -- can be present, but not both. Choices must be static
3413 -- values or ranges with static bounds.
3415 declare
3416 Container : constant Entity_Id :=
3417 First_Formal (Entity (Assign_Indexed_Subp));
3418 Index_Type : constant Entity_Id := Etype (Next_Formal (Container));
3419 Comp_Type : constant Entity_Id :=
3420 Etype (Next_Formal (Next_Formal (Container)));
3421 Comp : Node_Id;
3422 Choice : Node_Id;
3423 Num_Choices : Nat := 0;
3425 Hi_Val : Uint;
3426 Lo_Val : Uint;
3427 begin
3428 if Present (Expressions (N)) then
3429 Comp := First (Expressions (N));
3430 while Present (Comp) loop
3431 Analyze_And_Resolve (Comp, Comp_Type);
3432 Next (Comp);
3433 end loop;
3434 end if;
3436 if Present (Component_Associations (N))
3437 and then not Is_Empty_List (Component_Associations (N))
3438 then
3439 if Present (Expressions (N))
3440 and then not Is_Empty_List (Expressions (N))
3441 then
3442 Error_Msg_N ("container aggregate cannot be "
3443 & "both positional and named", N);
3444 return;
3445 end if;
3447 Comp := First (Component_Associations (N));
3449 while Present (Comp) loop
3450 if Nkind (Comp) = N_Component_Association then
3451 Choice := First (Choices (Comp));
3453 while Present (Choice) loop
3454 Analyze_And_Resolve (Choice, Index_Type);
3455 Num_Choices := Num_Choices + 1;
3456 Next (Choice);
3457 end loop;
3459 Analyze_And_Resolve (Expression (Comp), Comp_Type);
3461 elsif Nkind (Comp) in
3462 N_Iterated_Component_Association |
3463 N_Iterated_Element_Association
3464 then
3465 Resolve_Iterated_Association
3466 (Comp, Index_Type, Comp_Type);
3467 Num_Choices := Num_Choices + 1;
3468 end if;
3470 Next (Comp);
3471 end loop;
3473 -- The component associations in an indexed aggregate
3474 -- must denote a contiguous set of static values. We
3475 -- build a table of values/ranges and sort it, as is done
3476 -- elsewhere for case statements and array aggregates.
3477 -- If the aggregate has a single iterated association it
3478 -- is allowed to be nonstatic and there is nothing to check.
3480 if Num_Choices > 1 then
3481 declare
3482 Table : Case_Table_Type (1 .. Num_Choices);
3483 No_Choice : Pos := 1;
3484 Lo, Hi : Node_Id;
3486 -- Traverse aggregate to determine size of needed table.
3487 -- Verify that bounds are static and that loops have no
3488 -- filters or key expressions.
3490 begin
3491 Comp := First (Component_Associations (N));
3492 while Present (Comp) loop
3493 if Nkind (Comp) = N_Iterated_Element_Association then
3494 if Present
3495 (Loop_Parameter_Specification (Comp))
3496 then
3497 if Present (Iterator_Filter
3498 (Loop_Parameter_Specification (Comp)))
3499 then
3500 Error_Msg_N
3501 ("iterator filter not allowed " &
3502 "in indexed aggregate", Comp);
3503 return;
3505 elsif Present (Key_Expression
3506 (Loop_Parameter_Specification (Comp)))
3507 then
3508 Error_Msg_N
3509 ("key expression not allowed " &
3510 "in indexed aggregate", Comp);
3511 return;
3512 end if;
3513 end if;
3514 else
3515 Choice := First (Choices (Comp));
3517 while Present (Choice) loop
3518 Get_Index_Bounds (Choice, Lo, Hi);
3519 Table (No_Choice).Choice := Choice;
3520 Table (No_Choice).Lo := Lo;
3521 Table (No_Choice).Hi := Hi;
3523 -- Verify staticness of value or range
3525 if not Is_Static_Expression (Lo)
3526 or else not Is_Static_Expression (Hi)
3527 then
3528 Error_Msg_N
3529 ("nonstatic expression for index " &
3530 "for indexed aggregate", Choice);
3531 return;
3532 end if;
3534 No_Choice := No_Choice + 1;
3535 Next (Choice);
3536 end loop;
3537 end if;
3539 Next (Comp);
3540 end loop;
3542 Sort_Case_Table (Table);
3544 for J in 1 .. Num_Choices - 1 loop
3545 Hi_Val := Expr_Value (Table (J).Hi);
3546 Lo_Val := Expr_Value (Table (J + 1).Lo);
3548 if Lo_Val = Hi_Val then
3549 Error_Msg_N
3550 ("duplicate index in indexed aggregate",
3551 Table (J + 1).Choice);
3552 exit;
3554 elsif Lo_Val < Hi_Val then
3555 Error_Msg_N
3556 ("overlapping indices in indexed aggregate",
3557 Table (J + 1).Choice);
3558 exit;
3560 elsif Lo_Val > Hi_Val + 1 then
3561 Error_Msg_N
3562 ("missing index values", Table (J + 1).Choice);
3563 exit;
3564 end if;
3565 end loop;
3566 end;
3567 end if;
3568 end if;
3569 end;
3570 end if;
3571 end Resolve_Container_Aggregate;
3573 -----------------------------
3574 -- Resolve_Delta_Aggregate --
3575 -----------------------------
3577 procedure Resolve_Delta_Aggregate (N : Node_Id; Typ : Entity_Id) is
3578 Base : constant Node_Id := Expression (N);
3580 begin
3581 Error_Msg_Ada_2022_Feature ("delta aggregate", Sloc (N));
3583 if not Is_Composite_Type (Typ) then
3584 Error_Msg_N ("not a composite type", N);
3585 end if;
3587 Analyze_And_Resolve (Base, Typ);
3589 if Is_Array_Type (Typ) then
3590 -- For an array_delta_aggregate, the base_expression and each
3591 -- expression in every array_component_association shall be of a
3592 -- nonlimited type; RM 4.3.4(13/5). However, to prevent repeated
3593 -- errors we only check the base expression and not array component
3594 -- associations.
3596 if Is_Limited_Type (Etype (Base)) then
3597 Error_Msg_N
3598 ("array delta aggregate shall be of a nonlimited type", Base);
3599 Explain_Limited_Type (Etype (Base), Base);
3600 end if;
3602 Resolve_Delta_Array_Aggregate (N, Typ);
3603 else
3605 -- Delta aggregates for record types must use parentheses,
3606 -- not square brackets.
3608 if Is_Homogeneous_Aggregate (N) then
3609 Error_Msg_N
3610 ("delta aggregates for record types must use (), not '[']", N);
3611 end if;
3613 -- The base_expression of a record_delta_aggregate can be of a
3614 -- limited type only if it is newly constructed; RM 7.5(2.1/5).
3616 Check_Expr_OK_In_Limited_Aggregate (Base);
3618 Resolve_Delta_Record_Aggregate (N, Typ);
3619 end if;
3621 Set_Etype (N, Typ);
3622 end Resolve_Delta_Aggregate;
3624 -----------------------------------
3625 -- Resolve_Delta_Array_Aggregate --
3626 -----------------------------------
3628 procedure Resolve_Delta_Array_Aggregate (N : Node_Id; Typ : Entity_Id) is
3629 Deltas : constant List_Id := Component_Associations (N);
3630 Index_Type : constant Entity_Id := Etype (First_Index (Typ));
3632 Assoc : Node_Id;
3633 Choice : Node_Id;
3634 Expr : Node_Id;
3636 begin
3637 Assoc := First (Deltas);
3638 while Present (Assoc) loop
3639 if Nkind (Assoc) = N_Iterated_Component_Association then
3640 Choice := First (Choice_List (Assoc));
3641 while Present (Choice) loop
3642 if Nkind (Choice) = N_Others_Choice then
3643 Error_Msg_N
3644 ("OTHERS not allowed in delta aggregate", Choice);
3646 elsif Nkind (Choice) = N_Subtype_Indication then
3647 Resolve_Discrete_Subtype_Indication
3648 (Choice, Base_Type (Index_Type));
3650 else
3651 Analyze_And_Resolve (Choice, Index_Type);
3652 end if;
3654 Next (Choice);
3655 end loop;
3657 declare
3658 Id : constant Entity_Id := Defining_Identifier (Assoc);
3659 Ent : constant Entity_Id :=
3660 New_Internal_Entity
3661 (E_Loop, Current_Scope, Sloc (Assoc), 'L');
3663 begin
3664 Set_Etype (Ent, Standard_Void_Type);
3665 Set_Parent (Ent, Assoc);
3666 Push_Scope (Ent);
3668 if No (Scope (Id)) then
3669 Set_Etype (Id, Index_Type);
3670 Mutate_Ekind (Id, E_Variable);
3671 Set_Is_Not_Self_Hidden (Id);
3672 Set_Scope (Id, Ent);
3673 end if;
3674 Enter_Name (Id);
3676 -- Resolve a copy of the expression, after setting
3677 -- its parent properly to preserve its context.
3679 Expr := New_Copy_Tree (Expression (Assoc));
3680 Set_Parent (Expr, Assoc);
3681 Analyze_And_Resolve (Expr, Component_Type (Typ));
3682 End_Scope;
3683 end;
3685 else
3686 Choice := First (Choice_List (Assoc));
3687 while Present (Choice) loop
3688 Analyze (Choice);
3690 if Nkind (Choice) = N_Others_Choice then
3691 Error_Msg_N
3692 ("OTHERS not allowed in delta aggregate", Choice);
3694 elsif Is_Entity_Name (Choice)
3695 and then Is_Type (Entity (Choice))
3696 then
3697 -- Choice covers a range of values
3699 if Base_Type (Entity (Choice)) /=
3700 Base_Type (Index_Type)
3701 then
3702 Error_Msg_NE
3703 ("choice does not match index type of &",
3704 Choice, Typ);
3705 end if;
3707 elsif Nkind (Choice) = N_Subtype_Indication then
3708 Resolve_Discrete_Subtype_Indication
3709 (Choice, Base_Type (Index_Type));
3711 else
3712 Resolve (Choice, Index_Type);
3713 end if;
3715 Next (Choice);
3716 end loop;
3718 -- For an array_delta_aggregate, the array_component_association
3719 -- shall not use the box symbol <>; RM 4.3.4(11/5).
3721 pragma Assert
3722 (Box_Present (Assoc) xor Present (Expression (Assoc)));
3724 if Box_Present (Assoc) then
3725 Error_Msg_N
3726 ("'<'> in array delta aggregate is not allowed", Assoc);
3727 else
3728 Analyze_And_Resolve (Expression (Assoc), Component_Type (Typ));
3729 end if;
3730 end if;
3732 Next (Assoc);
3733 end loop;
3734 end Resolve_Delta_Array_Aggregate;
3736 ------------------------------------
3737 -- Resolve_Delta_Record_Aggregate --
3738 ------------------------------------
3740 procedure Resolve_Delta_Record_Aggregate (N : Node_Id; Typ : Entity_Id) is
3742 -- Variables used to verify that discriminant-dependent components
3743 -- appear in the same variant.
3745 Comp_Ref : Entity_Id := Empty; -- init to avoid warning
3746 Variant : Node_Id;
3748 procedure Check_Variant (Id : Entity_Id);
3749 -- If a given component of the delta aggregate appears in a variant
3750 -- part, verify that it is within the same variant as that of previous
3751 -- specified variant components of the delta.
3753 function Get_Component (Nam : Node_Id) return Entity_Id;
3754 -- Locate component with a given name and return it. If none found then
3755 -- report error and return Empty.
3757 function Nested_In (V1 : Node_Id; V2 : Node_Id) return Boolean;
3758 -- Determine whether variant V1 is within variant V2
3760 function Variant_Depth (N : Node_Id) return Natural;
3761 -- Determine the distance of a variant to the enclosing type declaration
3763 --------------------
3764 -- Check_Variant --
3765 --------------------
3767 procedure Check_Variant (Id : Entity_Id) is
3768 Comp : Entity_Id;
3769 Comp_Variant : Node_Id;
3771 begin
3772 if not Has_Discriminants (Typ) then
3773 return;
3774 end if;
3776 Comp := First_Entity (Typ);
3777 while Present (Comp) loop
3778 exit when Chars (Comp) = Chars (Id);
3779 Next_Component (Comp);
3780 end loop;
3782 -- Find the variant, if any, whose component list includes the
3783 -- component declaration.
3785 Comp_Variant := Parent (Parent (List_Containing (Parent (Comp))));
3786 if Nkind (Comp_Variant) = N_Variant then
3787 if No (Variant) then
3788 Variant := Comp_Variant;
3789 Comp_Ref := Comp;
3791 elsif Variant /= Comp_Variant then
3792 declare
3793 D1 : constant Integer := Variant_Depth (Variant);
3794 D2 : constant Integer := Variant_Depth (Comp_Variant);
3796 begin
3797 if D1 = D2
3798 or else
3799 (D1 > D2 and then not Nested_In (Variant, Comp_Variant))
3800 or else
3801 (D2 > D1 and then not Nested_In (Comp_Variant, Variant))
3802 then
3803 pragma Assert (Present (Comp_Ref));
3804 Error_Msg_Node_2 := Comp_Ref;
3805 Error_Msg_NE
3806 ("& and & appear in different variants", Id, Comp);
3808 -- Otherwise retain the deeper variant for subsequent tests
3810 elsif D2 > D1 then
3811 Variant := Comp_Variant;
3812 end if;
3813 end;
3814 end if;
3815 end if;
3816 end Check_Variant;
3818 -------------------
3819 -- Get_Component --
3820 -------------------
3822 function Get_Component (Nam : Node_Id) return Entity_Id is
3823 Comp : Entity_Id;
3825 begin
3826 Comp := First_Entity (Typ);
3827 while Present (Comp) loop
3828 if Chars (Comp) = Chars (Nam) then
3829 if Ekind (Comp) = E_Discriminant then
3830 Error_Msg_N ("delta cannot apply to discriminant", Nam);
3831 end if;
3833 return Comp;
3834 end if;
3836 Next_Entity (Comp);
3837 end loop;
3839 Error_Msg_NE ("type& has no component with this name", Nam, Typ);
3840 return Empty;
3841 end Get_Component;
3843 ---------------
3844 -- Nested_In --
3845 ---------------
3847 function Nested_In (V1, V2 : Node_Id) return Boolean is
3848 Par : Node_Id;
3850 begin
3851 Par := Parent (V1);
3852 while Nkind (Par) /= N_Full_Type_Declaration loop
3853 if Par = V2 then
3854 return True;
3855 end if;
3857 Par := Parent (Par);
3858 end loop;
3860 return False;
3861 end Nested_In;
3863 -------------------
3864 -- Variant_Depth --
3865 -------------------
3867 function Variant_Depth (N : Node_Id) return Natural is
3868 Depth : Natural;
3869 Par : Node_Id;
3871 begin
3872 Depth := 0;
3873 Par := Parent (N);
3874 while Nkind (Par) /= N_Full_Type_Declaration loop
3875 Depth := Depth + 1;
3876 Par := Parent (Par);
3877 end loop;
3879 return Depth;
3880 end Variant_Depth;
3882 -- Local variables
3884 Deltas : constant List_Id := Component_Associations (N);
3886 Assoc : Node_Id;
3887 Choice : Node_Id;
3888 Comp : Entity_Id;
3889 Comp_Type : Entity_Id := Empty; -- init to avoid warning
3891 -- Start of processing for Resolve_Delta_Record_Aggregate
3893 begin
3894 Variant := Empty;
3896 Assoc := First (Deltas);
3897 while Present (Assoc) loop
3898 Choice := First (Choice_List (Assoc));
3899 while Present (Choice) loop
3900 Comp := Get_Component (Choice);
3902 if Present (Comp) then
3903 Check_Variant (Choice);
3905 Comp_Type := Etype (Comp);
3907 -- Decorate the component reference by setting its entity and
3908 -- type, as otherwise backends like GNATprove would have to
3909 -- rediscover this information by themselves.
3911 Set_Entity (Choice, Comp);
3912 Set_Etype (Choice, Comp_Type);
3913 else
3914 Comp_Type := Any_Type;
3915 end if;
3917 Next (Choice);
3918 end loop;
3920 pragma Assert (Present (Comp_Type));
3922 -- A record_component_association in record_delta_aggregate shall not
3923 -- use the box compound delimiter <> rather than an expression; see
3924 -- RM 4.3.1(17.3/5).
3926 pragma Assert (Present (Expression (Assoc)) xor Box_Present (Assoc));
3928 if Box_Present (Assoc) then
3929 Error_Msg_N
3930 ("'<'> in record delta aggregate is not allowed", Assoc);
3931 else
3932 Analyze_And_Resolve (Expression (Assoc), Comp_Type);
3934 -- The expression must not be of a limited type; RM 4.3.1(17.4/5)
3936 if Is_Limited_Type (Etype (Expression (Assoc))) then
3937 Error_Msg_N
3938 ("expression of a limited type in record delta aggregate " &
3939 "is not allowed",
3940 Expression (Assoc));
3941 end if;
3942 end if;
3944 Next (Assoc);
3945 end loop;
3946 end Resolve_Delta_Record_Aggregate;
3948 ---------------------------------
3949 -- Resolve_Extension_Aggregate --
3950 ---------------------------------
3952 -- There are two cases to consider:
3954 -- a) If the ancestor part is a type mark, the components needed are the
3955 -- difference between the components of the expected type and the
3956 -- components of the given type mark.
3958 -- b) If the ancestor part is an expression, it must be unambiguous, and
3959 -- once we have its type we can also compute the needed components as in
3960 -- the previous case. In both cases, if the ancestor type is not the
3961 -- immediate ancestor, we have to build this ancestor recursively.
3963 -- In both cases, discriminants of the ancestor type do not play a role in
3964 -- the resolution of the needed components, because inherited discriminants
3965 -- cannot be used in a type extension. As a result we can compute
3966 -- independently the list of components of the ancestor type and of the
3967 -- expected type.
3969 procedure Resolve_Extension_Aggregate (N : Node_Id; Typ : Entity_Id) is
3970 A : constant Node_Id := Ancestor_Part (N);
3971 A_Type : Entity_Id;
3972 I : Interp_Index;
3973 It : Interp;
3975 function Valid_Limited_Ancestor (Anc : Node_Id) return Boolean;
3976 -- If the type is limited, verify that the ancestor part is a legal
3977 -- expression (aggregate or function call, including 'Input)) that does
3978 -- not require a copy, as specified in 7.5(2).
3980 function Valid_Ancestor_Type return Boolean;
3981 -- Verify that the type of the ancestor part is a non-private ancestor
3982 -- of the expected type, which must be a type extension.
3984 procedure Transform_BIP_Assignment (Typ : Entity_Id);
3985 -- For an extension aggregate whose ancestor part is a build-in-place
3986 -- call returning a nonlimited type, this is used to transform the
3987 -- assignment to the ancestor part to use a temp.
3989 ----------------------------
3990 -- Valid_Limited_Ancestor --
3991 ----------------------------
3993 function Valid_Limited_Ancestor (Anc : Node_Id) return Boolean is
3994 begin
3995 if Is_Entity_Name (Anc) and then Is_Type (Entity (Anc)) then
3996 return True;
3998 -- The ancestor must be a call or an aggregate, but a call may
3999 -- have been expanded into a temporary, so check original node.
4001 elsif Nkind (Anc) in N_Aggregate
4002 | N_Extension_Aggregate
4003 | N_Function_Call
4004 then
4005 return True;
4007 elsif Nkind (Original_Node (Anc)) = N_Function_Call then
4008 return True;
4010 elsif Nkind (Anc) = N_Attribute_Reference
4011 and then Attribute_Name (Anc) = Name_Input
4012 then
4013 return True;
4015 elsif Nkind (Anc) = N_Qualified_Expression then
4016 return Valid_Limited_Ancestor (Expression (Anc));
4018 elsif Nkind (Anc) = N_Raise_Expression then
4019 return True;
4021 else
4022 return False;
4023 end if;
4024 end Valid_Limited_Ancestor;
4026 -------------------------
4027 -- Valid_Ancestor_Type --
4028 -------------------------
4030 function Valid_Ancestor_Type return Boolean is
4031 Imm_Type : Entity_Id;
4033 begin
4034 Imm_Type := Base_Type (Typ);
4035 while Is_Derived_Type (Imm_Type) loop
4036 if Etype (Imm_Type) = Base_Type (A_Type) then
4037 return True;
4039 -- The base type of the parent type may appear as a private
4040 -- extension if it is declared as such in a parent unit of the
4041 -- current one. For consistency of the subsequent analysis use
4042 -- the partial view for the ancestor part.
4044 elsif Is_Private_Type (Etype (Imm_Type))
4045 and then Present (Full_View (Etype (Imm_Type)))
4046 and then Base_Type (A_Type) = Full_View (Etype (Imm_Type))
4047 then
4048 A_Type := Etype (Imm_Type);
4049 return True;
4051 -- The parent type may be a private extension. The aggregate is
4052 -- legal if the type of the aggregate is an extension of it that
4053 -- is not a private extension.
4055 elsif Is_Private_Type (A_Type)
4056 and then not Is_Private_Type (Imm_Type)
4057 and then Present (Full_View (A_Type))
4058 and then Base_Type (Full_View (A_Type)) = Etype (Imm_Type)
4059 then
4060 return True;
4062 -- The parent type may be a raise expression (which is legal in
4063 -- any expression context).
4065 elsif A_Type = Raise_Type then
4066 A_Type := Etype (Imm_Type);
4067 return True;
4069 else
4070 Imm_Type := Etype (Base_Type (Imm_Type));
4071 end if;
4072 end loop;
4074 -- If previous loop did not find a proper ancestor, report error
4076 Error_Msg_NE ("expect ancestor type of &", A, Typ);
4077 return False;
4078 end Valid_Ancestor_Type;
4080 ------------------------------
4081 -- Transform_BIP_Assignment --
4082 ------------------------------
4084 procedure Transform_BIP_Assignment (Typ : Entity_Id) is
4085 Loc : constant Source_Ptr := Sloc (N);
4086 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'Y', A);
4087 Obj_Decl : constant Node_Id :=
4088 Make_Object_Declaration (Loc,
4089 Defining_Identifier => Def_Id,
4090 Constant_Present => True,
4091 Object_Definition => New_Occurrence_Of (Typ, Loc),
4092 Expression => A,
4093 Has_Init_Expression => True);
4094 begin
4095 Set_Etype (Def_Id, Typ);
4096 Set_Ancestor_Part (N, New_Occurrence_Of (Def_Id, Loc));
4097 Insert_Action (N, Obj_Decl);
4098 end Transform_BIP_Assignment;
4100 -- Start of processing for Resolve_Extension_Aggregate
4102 begin
4103 -- Analyze the ancestor part and account for the case where it is a
4104 -- parameterless function call.
4106 Analyze (A);
4107 Check_Parameterless_Call (A);
4109 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
4111 -- AI05-0115: If the ancestor part is a subtype mark, the ancestor
4112 -- must not have unknown discriminants. To catch cases where the
4113 -- aggregate occurs at a place where the full view of the ancestor
4114 -- type is visible and doesn't have unknown discriminants, but the
4115 -- aggregate type was derived from a partial view that has unknown
4116 -- discriminants, we check whether the aggregate type has unknown
4117 -- discriminants (unknown discriminants were inherited), along
4118 -- with checking that the partial view of the ancestor has unknown
4119 -- discriminants. (It might be sufficient to replace the entire
4120 -- condition with Has_Unknown_Discriminants (Typ), but that might
4121 -- miss some cases, not clear, and causes error changes in some tests
4122 -- such as class-wide cases, that aren't clearly improvements. ???)
4124 if Has_Unknown_Discriminants (Entity (A))
4125 or else (Has_Unknown_Discriminants (Typ)
4126 and then Partial_View_Has_Unknown_Discr (Entity (A)))
4127 then
4128 Error_Msg_NE
4129 ("aggregate not available for type& whose ancestor "
4130 & "has unknown discriminants", N, Typ);
4131 end if;
4132 end if;
4134 if not Is_Tagged_Type (Typ) then
4135 Error_Msg_N ("type of extension aggregate must be tagged", N);
4136 return;
4138 elsif Is_Limited_Type (Typ) then
4140 -- Ada 2005 (AI-287): Limited aggregates are allowed
4142 if Ada_Version < Ada_2005 then
4143 Error_Msg_N ("aggregate type cannot be limited", N);
4144 Explain_Limited_Type (Typ, N);
4145 return;
4147 elsif Valid_Limited_Ancestor (A) then
4148 null;
4150 else
4151 Error_Msg_N
4152 ("limited ancestor part must be aggregate or function call", A);
4153 end if;
4155 elsif Is_Class_Wide_Type (Typ) then
4156 Error_Msg_N ("aggregate cannot be of a class-wide type", N);
4157 return;
4158 end if;
4160 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
4161 A_Type := Get_Full_View (Entity (A));
4163 if Valid_Ancestor_Type then
4164 Set_Entity (A, A_Type);
4165 Set_Etype (A, A_Type);
4167 Validate_Ancestor_Part (N);
4168 Resolve_Record_Aggregate (N, Typ);
4169 end if;
4171 elsif Nkind (A) /= N_Aggregate then
4172 if Is_Overloaded (A) then
4173 A_Type := Any_Type;
4175 Get_First_Interp (A, I, It);
4176 while Present (It.Typ) loop
4178 -- Consider limited interpretations if Ada 2005 or higher
4180 if Is_Tagged_Type (It.Typ)
4181 and then (Ada_Version >= Ada_2005
4182 or else not Is_Limited_Type (It.Typ))
4183 then
4184 if A_Type /= Any_Type then
4185 Error_Msg_N ("cannot resolve expression", A);
4186 return;
4187 else
4188 A_Type := It.Typ;
4189 end if;
4190 end if;
4192 Get_Next_Interp (I, It);
4193 end loop;
4195 if A_Type = Any_Type then
4196 if Ada_Version >= Ada_2005 then
4197 Error_Msg_N
4198 ("ancestor part must be of a tagged type", A);
4199 else
4200 Error_Msg_N
4201 ("ancestor part must be of a nonlimited tagged type", A);
4202 end if;
4204 return;
4205 end if;
4207 else
4208 A_Type := Etype (A);
4209 end if;
4211 if Valid_Ancestor_Type then
4212 Resolve (A, A_Type);
4213 Check_Unset_Reference (A);
4214 Check_Non_Static_Context (A);
4216 -- The aggregate is illegal if the ancestor expression is a call
4217 -- to a function with a limited unconstrained result, unless the
4218 -- type of the aggregate is a null extension. This restriction
4219 -- was added in AI05-67 to simplify implementation.
4221 if Nkind (A) = N_Function_Call
4222 and then Is_Limited_Type (A_Type)
4223 and then not Is_Null_Extension (Typ)
4224 and then not Is_Constrained (A_Type)
4225 then
4226 Error_Msg_N
4227 ("type of limited ancestor part must be constrained", A);
4229 -- Reject the use of CPP constructors that leave objects partially
4230 -- initialized. For example:
4232 -- type CPP_Root is tagged limited record ...
4233 -- pragma Import (CPP, CPP_Root);
4235 -- type CPP_DT is new CPP_Root and Iface ...
4236 -- pragma Import (CPP, CPP_DT);
4238 -- type Ada_DT is new CPP_DT with ...
4240 -- Obj : Ada_DT := Ada_DT'(New_CPP_Root with others => <>);
4242 -- Using the constructor of CPP_Root the slots of the dispatch
4243 -- table of CPP_DT cannot be set, and the secondary tag of
4244 -- CPP_DT is unknown.
4246 elsif Nkind (A) = N_Function_Call
4247 and then Is_CPP_Constructor_Call (A)
4248 and then Enclosing_CPP_Parent (Typ) /= A_Type
4249 then
4250 Error_Msg_NE
4251 ("??must use 'C'P'P constructor for type &", A,
4252 Enclosing_CPP_Parent (Typ));
4254 -- The following call is not needed if the previous warning
4255 -- is promoted to an error.
4257 Resolve_Record_Aggregate (N, Typ);
4259 elsif Is_Class_Wide_Type (Etype (A))
4260 and then Nkind (Original_Node (A)) = N_Function_Call
4261 then
4262 -- If the ancestor part is a dispatching call, it appears
4263 -- statically to be a legal ancestor, but it yields any member
4264 -- of the class, and it is not possible to determine whether
4265 -- it is an ancestor of the extension aggregate (much less
4266 -- which ancestor). It is not possible to determine the
4267 -- components of the extension part.
4269 -- This check implements AI-306, which in fact was motivated by
4270 -- an AdaCore query to the ARG after this test was added.
4272 Error_Msg_N ("ancestor part must be statically tagged", A);
4273 else
4274 -- We are using the build-in-place protocol, but we can't build
4275 -- in place, because we need to call the function before
4276 -- allocating the aggregate. Could do better for null
4277 -- extensions, and maybe for nondiscriminated types.
4278 -- This is wrong for limited, but those were wrong already.
4280 if not Is_Limited_View (A_Type)
4281 and then Is_Build_In_Place_Function_Call (A)
4282 then
4283 Transform_BIP_Assignment (A_Type);
4284 end if;
4286 Resolve_Record_Aggregate (N, Typ);
4287 end if;
4288 end if;
4290 else
4291 Error_Msg_N ("no unique type for this aggregate", A);
4292 end if;
4294 Check_Function_Writable_Actuals (N);
4295 end Resolve_Extension_Aggregate;
4297 ----------------------------------
4298 -- Resolve_Null_Array_Aggregate --
4299 ----------------------------------
4301 function Resolve_Null_Array_Aggregate (N : Node_Id) return Boolean is
4302 -- Never returns False, but declared as a function to match
4303 -- other Resolve_Mumble functions.
4305 Loc : constant Source_Ptr := Sloc (N);
4306 Typ : constant Entity_Id := Etype (N);
4308 Index : Node_Id;
4309 Lo, Hi : Node_Id;
4310 Constr : constant List_Id := New_List;
4312 begin
4313 -- Attach the list of constraints at the location of the aggregate, so
4314 -- the individual constraints can be analyzed.
4316 Set_Parent (Constr, N);
4318 -- Create a constrained subtype with null dimensions
4320 Index := First_Index (Typ);
4321 while Present (Index) loop
4322 Get_Index_Bounds (Index, L => Lo, H => Hi);
4324 -- The upper bound is the predecessor of the lower bound
4326 Hi := Make_Attribute_Reference
4327 (Loc,
4328 Prefix => New_Occurrence_Of (Etype (Index), Loc),
4329 Attribute_Name => Name_Pred,
4330 Expressions => New_List (New_Copy_Tree (Lo)));
4332 Append (Make_Range (Loc, New_Copy_Tree (Lo), Hi), Constr);
4333 Analyze_And_Resolve (Last (Constr), Etype (Index));
4335 Next_Index (Index);
4336 end loop;
4338 Set_Compile_Time_Known_Aggregate (N);
4339 Set_Aggregate_Bounds (N, First (Constr));
4341 return True;
4342 end Resolve_Null_Array_Aggregate;
4344 ------------------------------
4345 -- Resolve_Record_Aggregate --
4346 ------------------------------
4348 procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id) is
4349 New_Assoc_List : constant List_Id := New_List;
4350 -- New_Assoc_List is the newly built list of N_Component_Association
4351 -- nodes.
4353 Others_Etype : Entity_Id := Empty;
4354 -- This variable is used to save the Etype of the last record component
4355 -- that takes its value from the others choice. Its purpose is:
4357 -- (a) make sure the others choice is useful
4359 -- (b) make sure the type of all the components whose value is
4360 -- subsumed by the others choice are the same.
4362 -- This variable is updated as a side effect of function Get_Value.
4364 Box_Node : Node_Id := Empty;
4365 Is_Box_Present : Boolean := False;
4366 Is_Box_Init_By_Default : Boolean := False;
4367 Others_Box : Natural := 0;
4368 -- Ada 2005 (AI-287): Variables used in case of default initialization
4369 -- to provide a functionality similar to Others_Etype. Box_Present
4370 -- indicates that the component takes its default initialization;
4371 -- Others_Box counts the number of components of the current aggregate
4372 -- (which may be a sub-aggregate of a larger one) that are default-
4373 -- initialized. A value of One indicates that an others_box is present.
4374 -- Any larger value indicates that the others_box is not redundant.
4375 -- These variables, similar to Others_Etype, are also updated as a side
4376 -- effect of function Get_Value. Box_Node is used to place a warning on
4377 -- a redundant others_box.
4379 procedure Add_Association
4380 (Component : Entity_Id;
4381 Expr : Node_Id;
4382 Assoc_List : List_Id;
4383 Is_Box_Present : Boolean := False);
4384 -- Builds a new N_Component_Association node which associates Component
4385 -- to expression Expr and adds it to the association list being built,
4386 -- either New_Assoc_List, or the association being built for an inner
4387 -- aggregate.
4389 procedure Add_Discriminant_Values
4390 (New_Aggr : Node_Id;
4391 Assoc_List : List_Id);
4392 -- The constraint to a component may be given by a discriminant of the
4393 -- enclosing type, in which case we have to retrieve its value, which is
4394 -- part of the enclosing aggregate. Assoc_List provides the discriminant
4395 -- associations of the current type or of some enclosing record.
4397 function Discriminant_Present (Input_Discr : Entity_Id) return Boolean;
4398 -- If aggregate N is a regular aggregate this routine will return True.
4399 -- Otherwise, if N is an extension aggregate, then Input_Discr denotes
4400 -- a discriminant whose value may already have been specified by N's
4401 -- ancestor part. This routine checks whether this is indeed the case
4402 -- and if so returns False, signaling that no value for Input_Discr
4403 -- should appear in N's aggregate part. Also, in this case, the routine
4404 -- appends to New_Assoc_List the discriminant value specified in the
4405 -- ancestor part.
4407 -- If the aggregate is in a context with expansion delayed, it will be
4408 -- reanalyzed. The inherited discriminant values must not be reinserted
4409 -- in the component list to prevent spurious errors, but they must be
4410 -- present on first analysis to build the proper subtype indications.
4411 -- The flag Inherited_Discriminant is used to prevent the re-insertion.
4413 function Find_Private_Ancestor (Typ : Entity_Id) return Entity_Id;
4414 -- AI05-0115: Find earlier ancestor in the derivation chain that is
4415 -- derived from private view Typ. Whether the aggregate is legal depends
4416 -- on the current visibility of the type as well as that of the parent
4417 -- of the ancestor.
4419 function Get_Value
4420 (Compon : Entity_Id;
4421 From : List_Id;
4422 Consider_Others_Choice : Boolean := False) return Node_Id;
4423 -- Given a record component stored in parameter Compon, this function
4424 -- returns its value as it appears in the list From, which is a list
4425 -- of N_Component_Association nodes.
4427 -- If no component association has a choice for the searched component,
4428 -- the value provided by the others choice is returned, if there is one,
4429 -- and Consider_Others_Choice is set to true. Otherwise Empty is
4430 -- returned. If there is more than one component association giving a
4431 -- value for the searched record component, an error message is emitted
4432 -- and the first found value is returned.
4434 -- If Consider_Others_Choice is set and the returned expression comes
4435 -- from the others choice, then Others_Etype is set as a side effect.
4436 -- An error message is emitted if the components taking their value from
4437 -- the others choice do not have same type.
4439 procedure Propagate_Discriminants
4440 (Aggr : Node_Id;
4441 Assoc_List : List_Id);
4442 -- Nested components may themselves be discriminated types constrained
4443 -- by outer discriminants, whose values must be captured before the
4444 -- aggregate is expanded into assignments.
4446 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Entity_Id);
4447 -- Analyzes and resolves expression Expr against the Etype of the
4448 -- Component. This routine also applies all appropriate checks to Expr.
4449 -- It finally saves a Expr in the newly created association list that
4450 -- will be attached to the final record aggregate. Note that if the
4451 -- Parent pointer of Expr is not set then Expr was produced with a
4452 -- New_Copy_Tree or some such.
4454 procedure Rewrite_Range (Root_Type : Entity_Id; Rge : Node_Id);
4455 -- Rewrite a range node Rge when its bounds refer to non-stored
4456 -- discriminants from Root_Type, to replace them with the stored
4457 -- discriminant values. This is required in GNATprove mode, and is
4458 -- adopted in all modes to avoid special-casing GNATprove mode.
4460 ---------------------
4461 -- Add_Association --
4462 ---------------------
4464 procedure Add_Association
4465 (Component : Entity_Id;
4466 Expr : Node_Id;
4467 Assoc_List : List_Id;
4468 Is_Box_Present : Boolean := False)
4470 Choice_List : constant List_Id := New_List;
4471 Loc : Source_Ptr;
4473 begin
4474 -- If this is a box association the expression is missing, so use the
4475 -- Sloc of the aggregate itself for the new association.
4477 pragma Assert (Present (Expr) xor Is_Box_Present);
4479 if Present (Expr) then
4480 Loc := Sloc (Expr);
4481 else
4482 Loc := Sloc (N);
4483 end if;
4485 Append_To (Choice_List, New_Occurrence_Of (Component, Loc));
4487 Append_To (Assoc_List,
4488 Make_Component_Association (Loc,
4489 Choices => Choice_List,
4490 Expression => Expr,
4491 Box_Present => Is_Box_Present));
4493 -- If this association has a box for a component that is initialized
4494 -- by default, then set flag on the new association to indicate that
4495 -- the original association was for such a box-initialized component.
4497 if Is_Box_Init_By_Default then
4498 Set_Was_Default_Init_Box_Association (Last (Assoc_List));
4499 end if;
4500 end Add_Association;
4502 -----------------------------
4503 -- Add_Discriminant_Values --
4504 -----------------------------
4506 procedure Add_Discriminant_Values
4507 (New_Aggr : Node_Id;
4508 Assoc_List : List_Id)
4510 Assoc : Node_Id;
4511 Discr : Entity_Id;
4512 Discr_Elmt : Elmt_Id;
4513 Discr_Val : Node_Id;
4514 Val : Entity_Id;
4516 begin
4517 Discr := First_Discriminant (Etype (New_Aggr));
4518 Discr_Elmt := First_Elmt (Discriminant_Constraint (Etype (New_Aggr)));
4519 while Present (Discr_Elmt) loop
4520 Discr_Val := Node (Discr_Elmt);
4522 -- If the constraint is given by a discriminant then it is a
4523 -- discriminant of an enclosing record, and its value has already
4524 -- been placed in the association list.
4526 if Is_Entity_Name (Discr_Val)
4527 and then Ekind (Entity (Discr_Val)) = E_Discriminant
4528 then
4529 Val := Entity (Discr_Val);
4531 Assoc := First (Assoc_List);
4532 while Present (Assoc) loop
4533 if Present (Entity (First (Choices (Assoc))))
4534 and then Entity (First (Choices (Assoc))) = Val
4535 then
4536 Discr_Val := Expression (Assoc);
4537 exit;
4538 end if;
4540 Next (Assoc);
4541 end loop;
4542 end if;
4544 Add_Association
4545 (Discr, New_Copy_Tree (Discr_Val),
4546 Component_Associations (New_Aggr));
4548 -- If the discriminant constraint is a current instance, mark the
4549 -- current aggregate so that the self-reference can be expanded
4550 -- later. The constraint may refer to the subtype of aggregate, so
4551 -- use base type for comparison.
4553 if Nkind (Discr_Val) = N_Attribute_Reference
4554 and then Is_Entity_Name (Prefix (Discr_Val))
4555 and then Is_Type (Entity (Prefix (Discr_Val)))
4556 and then Base_Type (Etype (N)) = Entity (Prefix (Discr_Val))
4557 then
4558 Set_Has_Self_Reference (N);
4559 end if;
4561 Next_Elmt (Discr_Elmt);
4562 Next_Discriminant (Discr);
4563 end loop;
4564 end Add_Discriminant_Values;
4566 --------------------------
4567 -- Discriminant_Present --
4568 --------------------------
4570 function Discriminant_Present (Input_Discr : Entity_Id) return Boolean is
4571 Regular_Aggr : constant Boolean := Nkind (N) /= N_Extension_Aggregate;
4573 Ancestor_Is_Subtyp : Boolean;
4575 Loc : Source_Ptr;
4577 Ancestor : Node_Id;
4578 Ancestor_Typ : Entity_Id;
4579 Comp_Assoc : Node_Id;
4580 Discr : Entity_Id;
4581 Discr_Expr : Node_Id;
4582 Discr_Val : Elmt_Id := No_Elmt;
4583 Orig_Discr : Entity_Id;
4585 begin
4586 if Regular_Aggr then
4587 return True;
4588 end if;
4590 -- Check whether inherited discriminant values have already been
4591 -- inserted in the aggregate. This will be the case if we are
4592 -- re-analyzing an aggregate whose expansion was delayed.
4594 if Present (Component_Associations (N)) then
4595 Comp_Assoc := First (Component_Associations (N));
4596 while Present (Comp_Assoc) loop
4597 if Inherited_Discriminant (Comp_Assoc) then
4598 return True;
4599 end if;
4601 Next (Comp_Assoc);
4602 end loop;
4603 end if;
4605 Ancestor := Ancestor_Part (N);
4606 Ancestor_Typ := Etype (Ancestor);
4607 Loc := Sloc (Ancestor);
4609 -- For a private type with unknown discriminants, use the underlying
4610 -- record view if it is available.
4612 if Has_Unknown_Discriminants (Ancestor_Typ)
4613 and then Present (Full_View (Ancestor_Typ))
4614 and then Present (Underlying_Record_View (Full_View (Ancestor_Typ)))
4615 then
4616 Ancestor_Typ := Underlying_Record_View (Full_View (Ancestor_Typ));
4617 end if;
4619 Ancestor_Is_Subtyp :=
4620 Is_Entity_Name (Ancestor) and then Is_Type (Entity (Ancestor));
4622 -- If the ancestor part has no discriminants clearly N's aggregate
4623 -- part must provide a value for Discr.
4625 if not Has_Discriminants (Ancestor_Typ) then
4626 return True;
4628 -- If the ancestor part is an unconstrained subtype mark then the
4629 -- Discr must be present in N's aggregate part.
4631 elsif Ancestor_Is_Subtyp
4632 and then not Is_Constrained (Entity (Ancestor))
4633 then
4634 return True;
4635 end if;
4637 -- Now look to see if Discr was specified in the ancestor part
4639 if Ancestor_Is_Subtyp then
4640 Discr_Val :=
4641 First_Elmt (Discriminant_Constraint (Entity (Ancestor)));
4642 end if;
4644 Orig_Discr := Original_Record_Component (Input_Discr);
4646 Discr := First_Discriminant (Ancestor_Typ);
4647 while Present (Discr) loop
4649 -- If Ancestor has already specified Disc value then insert its
4650 -- value in the final aggregate.
4652 if Original_Record_Component (Discr) = Orig_Discr then
4653 if Ancestor_Is_Subtyp then
4654 Discr_Expr := New_Copy_Tree (Node (Discr_Val));
4655 else
4656 Discr_Expr :=
4657 Make_Selected_Component (Loc,
4658 Prefix => Duplicate_Subexpr (Ancestor),
4659 Selector_Name => New_Occurrence_Of (Input_Discr, Loc));
4660 end if;
4662 Resolve_Aggr_Expr (Discr_Expr, Input_Discr);
4663 Set_Inherited_Discriminant (Last (New_Assoc_List));
4664 return False;
4665 end if;
4667 Next_Discriminant (Discr);
4669 if Ancestor_Is_Subtyp then
4670 Next_Elmt (Discr_Val);
4671 end if;
4672 end loop;
4674 return True;
4675 end Discriminant_Present;
4677 ---------------------------
4678 -- Find_Private_Ancestor --
4679 ---------------------------
4681 function Find_Private_Ancestor (Typ : Entity_Id) return Entity_Id is
4682 Par : Entity_Id;
4684 begin
4685 Par := Typ;
4686 loop
4687 if Has_Private_Ancestor (Par)
4688 and then not Has_Private_Ancestor (Etype (Base_Type (Par)))
4689 then
4690 return Par;
4692 elsif not Is_Derived_Type (Par) then
4693 return Empty;
4695 else
4696 Par := Etype (Base_Type (Par));
4697 end if;
4698 end loop;
4699 end Find_Private_Ancestor;
4701 ---------------
4702 -- Get_Value --
4703 ---------------
4705 function Get_Value
4706 (Compon : Entity_Id;
4707 From : List_Id;
4708 Consider_Others_Choice : Boolean := False) return Node_Id
4710 Typ : constant Entity_Id := Etype (Compon);
4711 Assoc : Node_Id;
4712 Expr : Node_Id := Empty;
4713 Selector_Name : Node_Id;
4715 begin
4716 Is_Box_Present := False;
4717 Is_Box_Init_By_Default := False;
4719 if No (From) then
4720 return Empty;
4721 end if;
4723 Assoc := First (From);
4724 while Present (Assoc) loop
4725 Selector_Name := First (Choices (Assoc));
4726 while Present (Selector_Name) loop
4727 if Nkind (Selector_Name) = N_Others_Choice then
4728 if Consider_Others_Choice and then No (Expr) then
4730 -- We need to duplicate the expression for each
4731 -- successive component covered by the others choice.
4732 -- This is redundant if the others_choice covers only
4733 -- one component (small optimization possible???), but
4734 -- indispensable otherwise, because each one must be
4735 -- expanded individually to preserve side effects.
4737 -- Ada 2005 (AI-287): In case of default initialization
4738 -- of components, we duplicate the corresponding default
4739 -- expression (from the record type declaration). The
4740 -- copy must carry the sloc of the association (not the
4741 -- original expression) to prevent spurious elaboration
4742 -- checks when the default includes function calls.
4744 if Box_Present (Assoc) then
4745 Others_Box := Others_Box + 1;
4746 Is_Box_Present := True;
4748 if Expander_Active then
4749 return
4750 New_Copy_Tree_And_Copy_Dimensions
4751 (Expression (Parent (Compon)),
4752 New_Sloc => Sloc (Assoc));
4753 else
4754 return Expression (Parent (Compon));
4755 end if;
4757 else
4758 if Present (Others_Etype)
4759 and then Base_Type (Others_Etype) /= Base_Type (Typ)
4760 then
4761 -- If the components are of an anonymous access
4762 -- type they are distinct, but this is legal in
4763 -- Ada 2012 as long as designated types match.
4765 if (Ekind (Typ) = E_Anonymous_Access_Type
4766 or else Ekind (Typ) =
4767 E_Anonymous_Access_Subprogram_Type)
4768 and then Designated_Type (Typ) =
4769 Designated_Type (Others_Etype)
4770 then
4771 null;
4772 else
4773 Error_Msg_N
4774 ("components in OTHERS choice must have same "
4775 & "type", Selector_Name);
4776 end if;
4777 end if;
4779 Others_Etype := Typ;
4781 -- Copy the expression so that it is resolved
4782 -- independently for each component, This is needed
4783 -- for accessibility checks on components of anonymous
4784 -- access types, even in compile_only mode.
4786 if not Inside_A_Generic then
4787 return
4788 New_Copy_Tree_And_Copy_Dimensions
4789 (Expression (Assoc));
4790 else
4791 return Expression (Assoc);
4792 end if;
4793 end if;
4794 end if;
4796 elsif Chars (Compon) = Chars (Selector_Name) then
4797 if No (Expr) then
4799 -- Ada 2005 (AI-231)
4801 if Ada_Version >= Ada_2005
4802 and then Known_Null (Expression (Assoc))
4803 then
4804 Check_Can_Never_Be_Null (Compon, Expression (Assoc));
4805 end if;
4807 -- We need to duplicate the expression when several
4808 -- components are grouped together with a "|" choice.
4809 -- For instance "filed1 | filed2 => Expr"
4811 -- Ada 2005 (AI-287)
4813 if Box_Present (Assoc) then
4814 Is_Box_Present := True;
4816 -- Duplicate the default expression of the component
4817 -- from the record type declaration, so a new copy
4818 -- can be attached to the association.
4820 -- Note that we always copy the default expression,
4821 -- even when the association has a single choice, in
4822 -- order to create a proper association for the
4823 -- expanded aggregate.
4825 -- Component may have no default, in which case the
4826 -- expression is empty and the component is default-
4827 -- initialized, but an association for the component
4828 -- exists, and it is not covered by an others clause.
4830 -- Scalar and private types have no initialization
4831 -- procedure, so they remain uninitialized. If the
4832 -- target of the aggregate is a constant this
4833 -- deserves a warning.
4835 if No (Expression (Parent (Compon)))
4836 and then not Has_Non_Null_Base_Init_Proc (Typ)
4837 and then not Has_Aspect (Typ, Aspect_Default_Value)
4838 and then not Is_Concurrent_Type (Typ)
4839 and then Nkind (Parent (N)) = N_Object_Declaration
4840 and then Constant_Present (Parent (N))
4841 then
4842 Error_Msg_Node_2 := Typ;
4843 Error_Msg_NE
4844 ("??component& of type& is uninitialized",
4845 Assoc, Selector_Name);
4847 -- An additional reminder if the component type
4848 -- is a generic formal.
4850 if Is_Generic_Type (Base_Type (Typ)) then
4851 Error_Msg_NE
4852 ("\instance should provide actual type with "
4853 & "initialization for&", Assoc, Typ);
4854 end if;
4855 end if;
4857 return
4858 New_Copy_Tree_And_Copy_Dimensions
4859 (Expression (Parent (Compon)));
4861 else
4862 if Present (Next (Selector_Name)) then
4863 Expr := New_Copy_Tree_And_Copy_Dimensions
4864 (Expression (Assoc));
4865 else
4866 Expr := Expression (Assoc);
4867 end if;
4868 end if;
4870 Generate_Reference (Compon, Selector_Name, 'm');
4872 else
4873 Error_Msg_NE
4874 ("more than one value supplied for &",
4875 Selector_Name, Compon);
4877 end if;
4878 end if;
4880 Next (Selector_Name);
4881 end loop;
4883 Next (Assoc);
4884 end loop;
4886 return Expr;
4887 end Get_Value;
4889 -----------------------------
4890 -- Propagate_Discriminants --
4891 -----------------------------
4893 procedure Propagate_Discriminants
4894 (Aggr : Node_Id;
4895 Assoc_List : List_Id)
4897 Loc : constant Source_Ptr := Sloc (N);
4899 procedure Process_Component (Comp : Entity_Id);
4900 -- Add one component with a box association to the inner aggregate,
4901 -- and recurse if component is itself composite.
4903 -----------------------
4904 -- Process_Component --
4905 -----------------------
4907 procedure Process_Component (Comp : Entity_Id) is
4908 T : constant Entity_Id := Etype (Comp);
4909 New_Aggr : Node_Id;
4911 begin
4912 if Is_Record_Type (T) and then Has_Discriminants (T) then
4913 New_Aggr := Make_Aggregate (Loc, No_List, New_List);
4914 Set_Etype (New_Aggr, T);
4916 Add_Association
4917 (Comp, New_Aggr, Component_Associations (Aggr));
4919 -- Collect discriminant values and recurse
4921 Add_Discriminant_Values (New_Aggr, Assoc_List);
4922 Propagate_Discriminants (New_Aggr, Assoc_List);
4924 Build_Constrained_Itype
4925 (New_Aggr, T, Component_Associations (New_Aggr));
4926 else
4927 Add_Association
4928 (Comp, Empty, Component_Associations (Aggr),
4929 Is_Box_Present => True);
4930 end if;
4931 end Process_Component;
4933 -- Local variables
4935 Aggr_Type : constant Entity_Id := Base_Type (Etype (Aggr));
4936 Components : constant Elist_Id := New_Elmt_List;
4937 Def_Node : constant Node_Id :=
4938 Type_Definition (Declaration_Node (Aggr_Type));
4940 Comp : Node_Id;
4941 Comp_Elmt : Elmt_Id;
4942 Errors : Boolean;
4944 -- Start of processing for Propagate_Discriminants
4946 begin
4947 -- The component type may be a variant type. Collect the components
4948 -- that are ruled by the known values of the discriminants. Their
4949 -- values have already been inserted into the component list of the
4950 -- current aggregate.
4952 if Nkind (Def_Node) = N_Record_Definition
4953 and then Present (Component_List (Def_Node))
4954 and then Present (Variant_Part (Component_List (Def_Node)))
4955 then
4956 Gather_Components (Aggr_Type,
4957 Component_List (Def_Node),
4958 Governed_By => Component_Associations (Aggr),
4959 Into => Components,
4960 Report_Errors => Errors);
4962 Comp_Elmt := First_Elmt (Components);
4963 while Present (Comp_Elmt) loop
4964 if Ekind (Node (Comp_Elmt)) /= E_Discriminant then
4965 Process_Component (Node (Comp_Elmt));
4966 end if;
4968 Next_Elmt (Comp_Elmt);
4969 end loop;
4971 -- No variant part, iterate over all components
4973 else
4974 Comp := First_Component (Etype (Aggr));
4975 while Present (Comp) loop
4976 Process_Component (Comp);
4977 Next_Component (Comp);
4978 end loop;
4979 end if;
4980 end Propagate_Discriminants;
4982 -----------------------
4983 -- Resolve_Aggr_Expr --
4984 -----------------------
4986 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Entity_Id) is
4987 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean;
4988 -- If the expression is an aggregate (possibly qualified) then its
4989 -- expansion is delayed until the enclosing aggregate is expanded
4990 -- into assignments. In that case, do not generate checks on the
4991 -- expression, because they will be generated later, and will other-
4992 -- wise force a copy (to remove side effects) that would leave a
4993 -- dynamic-sized aggregate in the code, something that gigi cannot
4994 -- handle.
4996 ---------------------------
4997 -- Has_Expansion_Delayed --
4998 ---------------------------
5000 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean is
5001 begin
5002 return
5003 (Nkind (Expr) in N_Aggregate | N_Extension_Aggregate
5004 and then Present (Etype (Expr))
5005 and then Is_Record_Type (Etype (Expr))
5006 and then Expansion_Delayed (Expr))
5007 or else
5008 (Nkind (Expr) = N_Qualified_Expression
5009 and then Has_Expansion_Delayed (Expression (Expr)));
5010 end Has_Expansion_Delayed;
5012 -- Local variables
5014 Expr_Type : Entity_Id := Empty;
5015 New_C : Entity_Id := Component;
5016 New_Expr : Node_Id;
5018 Relocate : Boolean;
5019 -- Set to True if the resolved Expr node needs to be relocated when
5020 -- attached to the newly created association list. This node need not
5021 -- be relocated if its parent pointer is not set. In fact in this
5022 -- case Expr is the output of a New_Copy_Tree call. If Relocate is
5023 -- True then we have analyzed the expression node in the original
5024 -- aggregate and hence it needs to be relocated when moved over to
5025 -- the new association list.
5027 -- Start of processing for Resolve_Aggr_Expr
5029 begin
5030 -- If the type of the component is elementary or the type of the
5031 -- aggregate does not contain discriminants, use the type of the
5032 -- component to resolve Expr.
5034 if Is_Elementary_Type (Etype (Component))
5035 or else not Has_Discriminants (Etype (N))
5036 then
5037 Expr_Type := Etype (Component);
5039 -- Otherwise we have to pick up the new type of the component from
5040 -- the new constrained subtype of the aggregate. In fact components
5041 -- which are of a composite type might be constrained by a
5042 -- discriminant, and we want to resolve Expr against the subtype were
5043 -- all discriminant occurrences are replaced with their actual value.
5045 else
5046 New_C := First_Component (Etype (N));
5047 while Present (New_C) loop
5048 if Chars (New_C) = Chars (Component) then
5049 Expr_Type := Etype (New_C);
5050 exit;
5051 end if;
5053 Next_Component (New_C);
5054 end loop;
5056 pragma Assert (Present (Expr_Type));
5058 -- For each range in an array type where a discriminant has been
5059 -- replaced with the constraint, check that this range is within
5060 -- the range of the base type. This checks is done in the init
5061 -- proc for regular objects, but has to be done here for
5062 -- aggregates since no init proc is called for them.
5064 if Is_Array_Type (Expr_Type) then
5065 declare
5066 Index : Node_Id;
5067 -- Range of the current constrained index in the array
5069 Orig_Index : Node_Id := First_Index (Etype (Component));
5070 -- Range corresponding to the range Index above in the
5071 -- original unconstrained record type. The bounds of this
5072 -- range may be governed by discriminants.
5074 Unconstr_Index : Node_Id := First_Index (Etype (Expr_Type));
5075 -- Range corresponding to the range Index above for the
5076 -- unconstrained array type. This range is needed to apply
5077 -- range checks.
5079 begin
5080 Index := First_Index (Expr_Type);
5081 while Present (Index) loop
5082 if Depends_On_Discriminant (Orig_Index) then
5083 Apply_Range_Check (Index, Etype (Unconstr_Index));
5084 end if;
5086 Next_Index (Index);
5087 Next_Index (Orig_Index);
5088 Next_Index (Unconstr_Index);
5089 end loop;
5090 end;
5091 end if;
5092 end if;
5094 -- If the Parent pointer of Expr is not set, Expr is an expression
5095 -- duplicated by New_Tree_Copy (this happens for record aggregates
5096 -- that look like (Field1 | Filed2 => Expr) or (others => Expr)).
5097 -- Such a duplicated expression must be attached to the tree
5098 -- before analysis and resolution to enforce the rule that a tree
5099 -- fragment should never be analyzed or resolved unless it is
5100 -- attached to the current compilation unit.
5102 if No (Parent (Expr)) then
5103 Set_Parent (Expr, N);
5104 Relocate := False;
5105 else
5106 Relocate := True;
5107 end if;
5109 Analyze_And_Resolve (Expr, Expr_Type);
5110 Check_Expr_OK_In_Limited_Aggregate (Expr);
5111 Check_Non_Static_Context (Expr);
5112 Check_Unset_Reference (Expr);
5114 -- Check wrong use of class-wide types
5116 if Is_Class_Wide_Type (Etype (Expr)) then
5117 Error_Msg_N ("dynamically tagged expression not allowed", Expr);
5118 end if;
5120 if not Has_Expansion_Delayed (Expr) then
5121 Aggregate_Constraint_Checks (Expr, Expr_Type);
5122 end if;
5124 -- If an aggregate component has a type with predicates, an explicit
5125 -- predicate check must be applied, as for an assignment statement,
5126 -- because the aggregate might not be expanded into individual
5127 -- component assignments.
5129 if Has_Predicates (Expr_Type)
5130 and then Analyzed (Expr)
5131 then
5132 Apply_Predicate_Check (Expr, Expr_Type);
5133 end if;
5135 if Raises_Constraint_Error (Expr) then
5136 Set_Raises_Constraint_Error (N);
5137 end if;
5139 -- If the expression has been marked as requiring a range check, then
5140 -- generate it here. It's a bit odd to be generating such checks in
5141 -- the analyzer, but harmless since Generate_Range_Check does nothing
5142 -- (other than making sure Do_Range_Check is set) if the expander is
5143 -- not active.
5145 if Do_Range_Check (Expr) then
5146 Generate_Range_Check (Expr, Expr_Type, CE_Range_Check_Failed);
5147 end if;
5149 -- Add association Component => Expr if the caller requests it
5151 if Relocate then
5152 New_Expr := Relocate_Node (Expr);
5154 -- Since New_Expr is not gonna be analyzed later on, we need to
5155 -- propagate here the dimensions form Expr to New_Expr.
5157 Copy_Dimensions (Expr, New_Expr);
5159 else
5160 New_Expr := Expr;
5161 end if;
5163 Add_Association (New_C, New_Expr, New_Assoc_List);
5164 end Resolve_Aggr_Expr;
5166 -------------------
5167 -- Rewrite_Range --
5168 -------------------
5170 procedure Rewrite_Range (Root_Type : Entity_Id; Rge : Node_Id) is
5171 procedure Rewrite_Bound
5172 (Bound : Node_Id;
5173 Disc : Entity_Id;
5174 Expr_Disc : Node_Id);
5175 -- Rewrite a bound of the range Bound, when it is equal to the
5176 -- non-stored discriminant Disc, into the stored discriminant
5177 -- value Expr_Disc.
5179 -------------------
5180 -- Rewrite_Bound --
5181 -------------------
5183 procedure Rewrite_Bound
5184 (Bound : Node_Id;
5185 Disc : Entity_Id;
5186 Expr_Disc : Node_Id)
5188 begin
5189 if Nkind (Bound) /= N_Identifier then
5190 return;
5191 end if;
5193 -- We expect either the discriminant or the discriminal
5195 if Entity (Bound) = Disc
5196 or else (Ekind (Entity (Bound)) = E_In_Parameter
5197 and then Discriminal_Link (Entity (Bound)) = Disc)
5198 then
5199 Rewrite (Bound, New_Copy_Tree (Expr_Disc));
5200 end if;
5201 end Rewrite_Bound;
5203 -- Local variables
5205 Low, High : Node_Id;
5206 Disc : Entity_Id;
5207 Expr_Disc : Elmt_Id;
5209 -- Start of processing for Rewrite_Range
5211 begin
5212 if Has_Discriminants (Root_Type) and then Nkind (Rge) = N_Range then
5213 Low := Low_Bound (Rge);
5214 High := High_Bound (Rge);
5216 Disc := First_Discriminant (Root_Type);
5217 Expr_Disc := First_Elmt (Stored_Constraint (Etype (N)));
5218 while Present (Disc) loop
5219 Rewrite_Bound (Low, Disc, Node (Expr_Disc));
5220 Rewrite_Bound (High, Disc, Node (Expr_Disc));
5221 Next_Discriminant (Disc);
5222 Next_Elmt (Expr_Disc);
5223 end loop;
5224 end if;
5225 end Rewrite_Range;
5227 -- Local variables
5229 Components : constant Elist_Id := New_Elmt_List;
5230 -- Components is the list of the record components whose value must be
5231 -- provided in the aggregate. This list does include discriminants.
5233 Component : Entity_Id;
5234 Component_Elmt : Elmt_Id;
5235 Expr : Node_Id;
5236 Positional_Expr : Node_Id;
5238 -- Start of processing for Resolve_Record_Aggregate
5240 begin
5241 -- A record aggregate is restricted in SPARK:
5243 -- Each named association can have only a single choice.
5244 -- OTHERS cannot be used.
5245 -- Positional and named associations cannot be mixed.
5247 if Present (Component_Associations (N)) then
5248 declare
5249 Assoc : Node_Id;
5251 begin
5252 Assoc := First (Component_Associations (N));
5253 while Present (Assoc) loop
5254 if Nkind (Assoc) = N_Iterated_Component_Association then
5255 Error_Msg_N
5256 ("iterated component association can only appear in an "
5257 & "array aggregate", N);
5258 raise Unrecoverable_Error;
5259 end if;
5261 Next (Assoc);
5262 end loop;
5263 end;
5264 end if;
5266 -- We may end up calling Duplicate_Subexpr on expressions that are
5267 -- attached to New_Assoc_List. For this reason we need to attach it
5268 -- to the tree by setting its parent pointer to N. This parent point
5269 -- will change in STEP 8 below.
5271 Set_Parent (New_Assoc_List, N);
5273 -- STEP 1: abstract type and null record verification
5275 if Is_Abstract_Type (Typ) then
5276 Error_Msg_N ("type of aggregate cannot be abstract", N);
5277 end if;
5279 if No (First_Entity (Typ)) and then Null_Record_Present (N) then
5280 Set_Etype (N, Typ);
5281 return;
5283 elsif Present (First_Entity (Typ))
5284 and then Null_Record_Present (N)
5285 and then not Is_Tagged_Type (Typ)
5286 then
5287 Error_Msg_N ("record aggregate cannot be null", N);
5288 return;
5290 -- If the type has no components, then the aggregate should either
5291 -- have "null record", or in Ada 2005 it could instead have a single
5292 -- component association given by "others => <>". For Ada 95 we flag an
5293 -- error at this point, but for Ada 2005 we proceed with checking the
5294 -- associations below, which will catch the case where it's not an
5295 -- aggregate with "others => <>". Note that the legality of a <>
5296 -- aggregate for a null record type was established by AI05-016.
5298 elsif No (First_Entity (Typ))
5299 and then Ada_Version < Ada_2005
5300 then
5301 Error_Msg_N ("record aggregate must be null", N);
5302 return;
5303 end if;
5305 -- A record aggregate can only use parentheses
5307 if Nkind (N) = N_Aggregate
5308 and then Is_Homogeneous_Aggregate (N)
5309 then
5310 Error_Msg_N ("record aggregate must use (), not '[']", N);
5311 return;
5312 end if;
5314 -- STEP 2: Verify aggregate structure
5316 Step_2 : declare
5317 Assoc : Node_Id;
5318 Bad_Aggregate : Boolean := False;
5319 Selector_Name : Node_Id;
5321 begin
5322 if Present (Component_Associations (N)) then
5323 Assoc := First (Component_Associations (N));
5324 else
5325 Assoc := Empty;
5326 end if;
5328 while Present (Assoc) loop
5329 Selector_Name := First (Choices (Assoc));
5330 while Present (Selector_Name) loop
5331 if Nkind (Selector_Name) = N_Identifier then
5332 null;
5334 elsif Nkind (Selector_Name) = N_Others_Choice then
5335 if Selector_Name /= First (Choices (Assoc))
5336 or else Present (Next (Selector_Name))
5337 then
5338 Error_Msg_N
5339 ("OTHERS must appear alone in a choice list",
5340 Selector_Name);
5341 return;
5343 elsif Present (Next (Assoc)) then
5344 Error_Msg_N
5345 ("OTHERS must appear last in an aggregate",
5346 Selector_Name);
5347 return;
5349 -- (Ada 2005): If this is an association with a box,
5350 -- indicate that the association need not represent
5351 -- any component.
5353 elsif Box_Present (Assoc) then
5354 Others_Box := 1;
5355 Box_Node := Assoc;
5356 end if;
5358 else
5359 Error_Msg_N
5360 ("selector name should be identifier or OTHERS",
5361 Selector_Name);
5362 Bad_Aggregate := True;
5363 end if;
5365 Next (Selector_Name);
5366 end loop;
5368 Next (Assoc);
5369 end loop;
5371 if Bad_Aggregate then
5372 return;
5373 end if;
5374 end Step_2;
5376 -- STEP 3: Find discriminant Values
5378 Step_3 : declare
5379 Discrim : Entity_Id;
5380 Missing_Discriminants : Boolean := False;
5382 begin
5383 if Present (Expressions (N)) then
5384 Positional_Expr := First (Expressions (N));
5385 else
5386 Positional_Expr := Empty;
5387 end if;
5389 -- AI05-0115: if the ancestor part is a subtype mark, the ancestor
5390 -- must not have unknown discriminants.
5391 -- ??? We are not checking any subtype mark here and this code is not
5392 -- exercised by any test, so it's likely wrong (in particular
5393 -- we should not use Root_Type here but the subtype mark, if any),
5394 -- and possibly not needed.
5396 if Is_Derived_Type (Typ)
5397 and then Has_Unknown_Discriminants (Root_Type (Typ))
5398 and then Nkind (N) /= N_Extension_Aggregate
5399 then
5400 Error_Msg_NE
5401 ("aggregate not available for type& whose ancestor "
5402 & "has unknown discriminants", N, Typ);
5403 end if;
5405 if Has_Unknown_Discriminants (Typ)
5406 and then Present (Underlying_Record_View (Typ))
5407 then
5408 Discrim := First_Discriminant (Underlying_Record_View (Typ));
5409 elsif Has_Discriminants (Typ) then
5410 Discrim := First_Discriminant (Typ);
5411 else
5412 Discrim := Empty;
5413 end if;
5415 -- First find the discriminant values in the positional components
5417 while Present (Discrim) and then Present (Positional_Expr) loop
5418 if Discriminant_Present (Discrim) then
5419 Resolve_Aggr_Expr (Positional_Expr, Discrim);
5421 -- Ada 2005 (AI-231)
5423 if Ada_Version >= Ada_2005
5424 and then Known_Null (Positional_Expr)
5425 then
5426 Check_Can_Never_Be_Null (Discrim, Positional_Expr);
5427 end if;
5429 Next (Positional_Expr);
5430 end if;
5432 if Present (Get_Value (Discrim, Component_Associations (N))) then
5433 Error_Msg_NE
5434 ("more than one value supplied for discriminant&",
5435 N, Discrim);
5436 end if;
5438 Next_Discriminant (Discrim);
5439 end loop;
5441 -- Find remaining discriminant values if any among named components
5443 while Present (Discrim) loop
5444 Expr := Get_Value (Discrim, Component_Associations (N), True);
5446 if not Discriminant_Present (Discrim) then
5447 if Present (Expr) then
5448 Error_Msg_NE
5449 ("more than one value supplied for discriminant &",
5450 N, Discrim);
5451 end if;
5453 elsif No (Expr) then
5454 Error_Msg_NE
5455 ("no value supplied for discriminant &", N, Discrim);
5456 Missing_Discriminants := True;
5458 else
5459 Resolve_Aggr_Expr (Expr, Discrim);
5460 end if;
5462 Next_Discriminant (Discrim);
5463 end loop;
5465 if Missing_Discriminants then
5466 return;
5467 end if;
5469 -- At this point and until the beginning of STEP 6, New_Assoc_List
5470 -- contains only the discriminants and their values.
5472 end Step_3;
5474 -- STEP 4: Set the Etype of the record aggregate
5476 if Has_Discriminants (Typ)
5477 or else (Has_Unknown_Discriminants (Typ)
5478 and then Present (Underlying_Record_View (Typ)))
5479 then
5480 Build_Constrained_Itype (N, Typ, New_Assoc_List);
5481 else
5482 Set_Etype (N, Typ);
5483 end if;
5485 -- STEP 5: Get remaining components according to discriminant values
5487 Step_5 : declare
5488 Dnode : Node_Id;
5489 Errors_Found : Boolean := False;
5490 Record_Def : Node_Id;
5491 Parent_Typ : Entity_Id;
5492 Parent_Typ_List : Elist_Id;
5493 Parent_Elmt : Elmt_Id;
5494 Root_Typ : Entity_Id;
5496 begin
5497 if Is_Derived_Type (Typ) and then Is_Tagged_Type (Typ) then
5498 Parent_Typ_List := New_Elmt_List;
5500 -- If this is an extension aggregate, the component list must
5501 -- include all components that are not in the given ancestor type.
5502 -- Otherwise, the component list must include components of all
5503 -- ancestors, starting with the root.
5505 if Nkind (N) = N_Extension_Aggregate then
5506 Root_Typ := Base_Type (Etype (Ancestor_Part (N)));
5508 else
5509 -- AI05-0115: check legality of aggregate for type with a
5510 -- private ancestor.
5512 Root_Typ := Root_Type (Typ);
5513 if Has_Private_Ancestor (Typ) then
5514 declare
5515 Ancestor : constant Entity_Id :=
5516 Find_Private_Ancestor (Typ);
5517 Ancestor_Unit : constant Entity_Id :=
5518 Cunit_Entity
5519 (Get_Source_Unit (Ancestor));
5520 Parent_Unit : constant Entity_Id :=
5521 Cunit_Entity (Get_Source_Unit
5522 (Base_Type (Etype (Ancestor))));
5523 begin
5524 -- Check whether we are in a scope that has full view
5525 -- over the private ancestor and its parent. This can
5526 -- only happen if the derivation takes place in a child
5527 -- unit of the unit that declares the parent, and we are
5528 -- in the private part or body of that child unit, else
5529 -- the aggregate is illegal.
5531 if Is_Child_Unit (Ancestor_Unit)
5532 and then Scope (Ancestor_Unit) = Parent_Unit
5533 and then In_Open_Scopes (Scope (Ancestor))
5534 and then
5535 (In_Private_Part (Scope (Ancestor))
5536 or else In_Package_Body (Scope (Ancestor)))
5537 then
5538 null;
5540 else
5541 Error_Msg_NE
5542 ("type of aggregate has private ancestor&!",
5543 N, Root_Typ);
5544 Error_Msg_N ("must use extension aggregate!", N);
5545 return;
5546 end if;
5547 end;
5548 end if;
5550 Dnode := Declaration_Node (Base_Type (Root_Typ));
5552 -- If we don't get a full declaration, then we have some error
5553 -- which will get signalled later so skip this part. Otherwise
5554 -- gather components of root that apply to the aggregate type.
5555 -- We use the base type in case there is an applicable stored
5556 -- constraint that renames the discriminants of the root.
5558 if Nkind (Dnode) = N_Full_Type_Declaration then
5559 Record_Def := Type_Definition (Dnode);
5560 Gather_Components
5561 (Base_Type (Typ),
5562 Component_List (Record_Def),
5563 Governed_By => New_Assoc_List,
5564 Into => Components,
5565 Report_Errors => Errors_Found);
5567 if Errors_Found then
5568 Error_Msg_N
5569 ("discriminant controlling variant part is not static",
5571 return;
5572 end if;
5573 end if;
5574 end if;
5576 Parent_Typ := Base_Type (Typ);
5577 while Parent_Typ /= Root_Typ loop
5578 Prepend_Elmt (Parent_Typ, To => Parent_Typ_List);
5579 Parent_Typ := Etype (Parent_Typ);
5581 -- Check whether a private parent requires the use of
5582 -- an extension aggregate. This test does not apply in
5583 -- an instantiation: if the generic unit is legal so is
5584 -- the instance.
5586 if Nkind (Parent (Base_Type (Parent_Typ))) =
5587 N_Private_Type_Declaration
5588 or else Nkind (Parent (Base_Type (Parent_Typ))) =
5589 N_Private_Extension_Declaration
5590 then
5591 if Nkind (N) /= N_Extension_Aggregate
5592 and then not In_Instance
5593 then
5594 Error_Msg_NE
5595 ("type of aggregate has private ancestor&!",
5596 N, Parent_Typ);
5597 Error_Msg_N ("must use extension aggregate!", N);
5598 return;
5600 elsif Parent_Typ /= Root_Typ then
5601 Error_Msg_NE
5602 ("ancestor part of aggregate must be private type&",
5603 Ancestor_Part (N), Parent_Typ);
5604 return;
5605 end if;
5607 -- The current view of ancestor part may be a private type,
5608 -- while the context type is always non-private.
5610 elsif Is_Private_Type (Root_Typ)
5611 and then Present (Full_View (Root_Typ))
5612 and then Nkind (N) = N_Extension_Aggregate
5613 then
5614 exit when Base_Type (Full_View (Root_Typ)) = Parent_Typ;
5615 end if;
5616 end loop;
5618 -- Now collect components from all other ancestors, beginning
5619 -- with the current type. If the type has unknown discriminants
5620 -- use the component list of the Underlying_Record_View, which
5621 -- needs to be used for the subsequent expansion of the aggregate
5622 -- into assignments.
5624 Parent_Elmt := First_Elmt (Parent_Typ_List);
5625 while Present (Parent_Elmt) loop
5626 Parent_Typ := Node (Parent_Elmt);
5628 if Has_Unknown_Discriminants (Parent_Typ)
5629 and then Present (Underlying_Record_View (Typ))
5630 then
5631 Parent_Typ := Underlying_Record_View (Parent_Typ);
5632 end if;
5634 Record_Def := Type_Definition (Parent (Base_Type (Parent_Typ)));
5635 Gather_Components (Parent_Typ,
5636 Component_List (Record_Extension_Part (Record_Def)),
5637 Governed_By => New_Assoc_List,
5638 Into => Components,
5639 Report_Errors => Errors_Found);
5641 Next_Elmt (Parent_Elmt);
5642 end loop;
5644 -- Typ is not a derived tagged type
5646 else
5647 Record_Def := Type_Definition (Parent (Base_Type (Typ)));
5649 if Null_Present (Record_Def) then
5650 null;
5652 elsif not Has_Unknown_Discriminants (Typ) then
5653 Gather_Components
5654 (Base_Type (Typ),
5655 Component_List (Record_Def),
5656 Governed_By => New_Assoc_List,
5657 Into => Components,
5658 Report_Errors => Errors_Found);
5660 else
5661 Gather_Components
5662 (Base_Type (Underlying_Record_View (Typ)),
5663 Component_List (Record_Def),
5664 Governed_By => New_Assoc_List,
5665 Into => Components,
5666 Report_Errors => Errors_Found);
5667 end if;
5668 end if;
5670 if Errors_Found then
5671 return;
5672 end if;
5673 end Step_5;
5675 -- STEP 6: Find component Values
5677 Component_Elmt := First_Elmt (Components);
5679 -- First scan the remaining positional associations in the aggregate.
5680 -- Remember that at this point Positional_Expr contains the current
5681 -- positional association if any is left after looking for discriminant
5682 -- values in step 3.
5684 while Present (Positional_Expr) and then Present (Component_Elmt) loop
5685 Component := Node (Component_Elmt);
5686 Resolve_Aggr_Expr (Positional_Expr, Component);
5688 -- Ada 2005 (AI-231)
5690 if Ada_Version >= Ada_2005 and then Known_Null (Positional_Expr) then
5691 Check_Can_Never_Be_Null (Component, Positional_Expr);
5692 end if;
5694 if Present (Get_Value (Component, Component_Associations (N))) then
5695 Error_Msg_NE
5696 ("more than one value supplied for component &", N, Component);
5697 end if;
5699 Next (Positional_Expr);
5700 Next_Elmt (Component_Elmt);
5701 end loop;
5703 if Present (Positional_Expr) then
5704 Error_Msg_N
5705 ("too many components for record aggregate", Positional_Expr);
5706 end if;
5708 -- Now scan for the named arguments of the aggregate
5710 while Present (Component_Elmt) loop
5711 Component := Node (Component_Elmt);
5712 Expr := Get_Value (Component, Component_Associations (N), True);
5714 -- Note: The previous call to Get_Value sets the value of the
5715 -- variable Is_Box_Present.
5717 -- Ada 2005 (AI-287): Handle components with default initialization.
5718 -- Note: This feature was originally added to Ada 2005 for limited
5719 -- but it was finally allowed with any type.
5721 if Is_Box_Present then
5722 Check_Box_Component : declare
5723 Ctyp : constant Entity_Id := Etype (Component);
5725 begin
5726 -- Initially assume that the box is for a default-initialized
5727 -- component and reset to False in cases where that's not true.
5729 Is_Box_Init_By_Default := True;
5731 -- If there is a default expression for the aggregate, copy
5732 -- it into a new association. This copy must modify the scopes
5733 -- of internal types that may be attached to the expression
5734 -- (e.g. index subtypes of arrays) because in general the type
5735 -- declaration and the aggregate appear in different scopes,
5736 -- and the backend requires the scope of the type to match the
5737 -- point at which it is elaborated.
5739 -- If the component has an initialization procedure (IP) we
5740 -- pass the component to the expander, which will generate
5741 -- the call to such IP.
5743 -- If the component has discriminants, their values must
5744 -- be taken from their subtype. This is indispensable for
5745 -- constraints that are given by the current instance of an
5746 -- enclosing type, to allow the expansion of the aggregate to
5747 -- replace the reference to the current instance by the target
5748 -- object of the aggregate.
5750 if Is_Case_Choice_Pattern (N) then
5752 -- Do not transform box component values in a case-choice
5753 -- aggregate.
5755 Add_Association
5756 (Component => Component,
5757 Expr => Empty,
5758 Assoc_List => New_Assoc_List,
5759 Is_Box_Present => True);
5761 elsif Present (Parent (Component))
5762 and then Nkind (Parent (Component)) = N_Component_Declaration
5763 and then Present (Expression (Parent (Component)))
5764 then
5765 -- If component declaration has an initialization expression
5766 -- then this is not a case of default initialization.
5768 Is_Box_Init_By_Default := False;
5770 Expr :=
5771 New_Copy_Tree_And_Copy_Dimensions
5772 (Expression (Parent (Component)),
5773 New_Scope => Current_Scope,
5774 New_Sloc => Sloc (N));
5776 -- As the type of the copied default expression may refer
5777 -- to discriminants of the record type declaration, these
5778 -- non-stored discriminants need to be rewritten into stored
5779 -- discriminant values for the aggregate. This is required
5780 -- in GNATprove mode, and is adopted in all modes to avoid
5781 -- special-casing GNATprove mode.
5783 if Is_Array_Type (Etype (Expr)) then
5784 declare
5785 Rec_Typ : constant Entity_Id := Scope (Component);
5786 -- Root record type whose discriminants may be used as
5787 -- bounds in range nodes.
5789 Assoc : Node_Id;
5790 Choice : Node_Id;
5791 Index : Node_Id;
5793 begin
5794 -- Rewrite the range nodes occurring in the indexes
5795 -- and their types.
5797 Index := First_Index (Etype (Expr));
5798 while Present (Index) loop
5799 Rewrite_Range (Rec_Typ, Index);
5800 Rewrite_Range
5801 (Rec_Typ, Scalar_Range (Etype (Index)));
5803 Next_Index (Index);
5804 end loop;
5806 -- Rewrite the range nodes occurring as aggregate
5807 -- bounds and component associations.
5809 if Nkind (Expr) = N_Aggregate then
5810 if Present (Aggregate_Bounds (Expr)) then
5811 Rewrite_Range (Rec_Typ, Aggregate_Bounds (Expr));
5812 end if;
5814 if Present (Component_Associations (Expr)) then
5815 Assoc := First (Component_Associations (Expr));
5816 while Present (Assoc) loop
5817 Choice := First (Choices (Assoc));
5818 while Present (Choice) loop
5819 Rewrite_Range (Rec_Typ, Choice);
5821 Next (Choice);
5822 end loop;
5824 Next (Assoc);
5825 end loop;
5826 end if;
5827 end if;
5828 end;
5829 end if;
5831 Add_Association
5832 (Component => Component,
5833 Expr => Expr,
5834 Assoc_List => New_Assoc_List);
5835 Set_Has_Self_Reference (N);
5837 elsif Needs_Simple_Initialization (Ctyp) then
5838 Add_Association
5839 (Component => Component,
5840 Expr => Empty,
5841 Assoc_List => New_Assoc_List,
5842 Is_Box_Present => True);
5844 elsif Has_Non_Null_Base_Init_Proc (Ctyp)
5845 or else not Expander_Active
5846 then
5847 if Is_Record_Type (Ctyp)
5848 and then Has_Discriminants (Ctyp)
5849 and then not Is_Private_Type (Ctyp)
5850 then
5851 -- We build a partially initialized aggregate with the
5852 -- values of the discriminants and box initialization
5853 -- for the rest, if other components are present.
5855 -- The type of the aggregate is the known subtype of
5856 -- the component. The capture of discriminants must be
5857 -- recursive because subcomponents may be constrained
5858 -- (transitively) by discriminants of enclosing types.
5859 -- For a private type with discriminants, a call to the
5860 -- initialization procedure will be generated, and no
5861 -- subaggregate is needed.
5863 Capture_Discriminants : declare
5864 Loc : constant Source_Ptr := Sloc (N);
5865 Expr : Node_Id;
5867 begin
5868 Expr := Make_Aggregate (Loc, No_List, New_List);
5869 Set_Etype (Expr, Ctyp);
5871 -- If the enclosing type has discriminants, they have
5872 -- been collected in the aggregate earlier, and they
5873 -- may appear as constraints of subcomponents.
5875 -- Similarly if this component has discriminants, they
5876 -- might in turn be propagated to their components.
5878 if Has_Discriminants (Typ) then
5879 Add_Discriminant_Values (Expr, New_Assoc_List);
5880 Propagate_Discriminants (Expr, New_Assoc_List);
5882 elsif Has_Discriminants (Ctyp) then
5883 Add_Discriminant_Values
5884 (Expr, Component_Associations (Expr));
5885 Propagate_Discriminants
5886 (Expr, Component_Associations (Expr));
5888 Build_Constrained_Itype
5889 (Expr, Ctyp, Component_Associations (Expr));
5891 else
5892 declare
5893 Comp : Entity_Id;
5895 begin
5896 -- If the type has additional components, create
5897 -- an OTHERS box association for them.
5899 Comp := First_Component (Ctyp);
5900 while Present (Comp) loop
5901 if Ekind (Comp) = E_Component then
5902 if not Is_Record_Type (Etype (Comp)) then
5903 Append_To
5904 (Component_Associations (Expr),
5905 Make_Component_Association (Loc,
5906 Choices =>
5907 New_List (
5908 Make_Others_Choice (Loc)),
5909 Expression => Empty,
5910 Box_Present => True));
5911 end if;
5913 exit;
5914 end if;
5916 Next_Component (Comp);
5917 end loop;
5918 end;
5919 end if;
5921 Add_Association
5922 (Component => Component,
5923 Expr => Expr,
5924 Assoc_List => New_Assoc_List);
5925 end Capture_Discriminants;
5927 -- Otherwise the component type is not a record, or it has
5928 -- not discriminants, or it is private.
5930 else
5931 Add_Association
5932 (Component => Component,
5933 Expr => Empty,
5934 Assoc_List => New_Assoc_List,
5935 Is_Box_Present => True);
5936 end if;
5938 -- Otherwise we only need to resolve the expression if the
5939 -- component has partially initialized values (required to
5940 -- expand the corresponding assignments and run-time checks).
5942 elsif Present (Expr)
5943 and then Is_Partially_Initialized_Type (Ctyp)
5944 then
5945 Resolve_Aggr_Expr (Expr, Component);
5946 end if;
5947 end Check_Box_Component;
5949 elsif No (Expr) then
5951 -- Ignore hidden components associated with the position of the
5952 -- interface tags: these are initialized dynamically.
5954 if No (Related_Type (Component)) then
5955 Error_Msg_NE
5956 ("no value supplied for component &!", N, Component);
5957 end if;
5959 else
5960 Resolve_Aggr_Expr (Expr, Component);
5961 end if;
5963 Next_Elmt (Component_Elmt);
5964 end loop;
5966 -- STEP 7: check for invalid components + check type in choice list
5968 Step_7 : declare
5969 Assoc : Node_Id;
5970 New_Assoc : Node_Id;
5972 Selectr : Node_Id;
5973 -- Selector name
5975 Typech : Entity_Id;
5976 -- Type of first component in choice list
5978 begin
5979 if Present (Component_Associations (N)) then
5980 Assoc := First (Component_Associations (N));
5981 else
5982 Assoc := Empty;
5983 end if;
5985 Verification : while Present (Assoc) loop
5986 Selectr := First (Choices (Assoc));
5987 Typech := Empty;
5989 if Nkind (Selectr) = N_Others_Choice then
5991 -- Ada 2005 (AI-287): others choice may have expression or box
5993 if No (Others_Etype) and then Others_Box = 0 then
5994 Error_Msg_N
5995 ("OTHERS must represent at least one component", Selectr);
5997 elsif Others_Box = 1 and then Warn_On_Redundant_Constructs then
5998 Error_Msg_N ("OTHERS choice is redundant?r?", Box_Node);
5999 Error_Msg_N
6000 ("\previous choices cover all components?r?", Box_Node);
6001 end if;
6003 exit Verification;
6004 end if;
6006 while Present (Selectr) loop
6007 Component := Empty;
6008 New_Assoc := First (New_Assoc_List);
6009 while Present (New_Assoc) loop
6010 Component := First (Choices (New_Assoc));
6012 if Chars (Selectr) = Chars (Component) then
6013 if Style_Check then
6014 Check_Identifier (Selectr, Entity (Component));
6015 end if;
6017 exit;
6018 end if;
6020 Next (New_Assoc);
6021 end loop;
6023 -- If we found an association, then this is a legal component
6024 -- of the type in question.
6026 pragma Assert (if Present (New_Assoc) then Present (Component));
6028 -- If no association, this is not a legal component of the type
6029 -- in question, unless its association is provided with a box.
6031 if No (New_Assoc) then
6032 if Box_Present (Parent (Selectr)) then
6034 -- This may still be a bogus component with a box. Scan
6035 -- list of components to verify that a component with
6036 -- that name exists.
6038 declare
6039 C : Entity_Id;
6041 begin
6042 C := First_Component (Typ);
6043 while Present (C) loop
6044 if Chars (C) = Chars (Selectr) then
6046 -- If the context is an extension aggregate,
6047 -- the component must not be inherited from
6048 -- the ancestor part of the aggregate.
6050 if Nkind (N) /= N_Extension_Aggregate
6051 or else
6052 Scope (Original_Record_Component (C)) /=
6053 Etype (Ancestor_Part (N))
6054 then
6055 exit;
6056 end if;
6057 end if;
6059 Next_Component (C);
6060 end loop;
6062 if No (C) then
6063 Error_Msg_Node_2 := Typ;
6064 Error_Msg_N ("& is not a component of}", Selectr);
6065 end if;
6066 end;
6068 elsif Chars (Selectr) /= Name_uTag
6069 and then Chars (Selectr) /= Name_uParent
6070 then
6071 if not Has_Discriminants (Typ) then
6072 Error_Msg_Node_2 := Typ;
6073 Error_Msg_N ("& is not a component of}", Selectr);
6074 else
6075 Error_Msg_N
6076 ("& is not a component of the aggregate subtype",
6077 Selectr);
6078 end if;
6080 Check_Misspelled_Component (Components, Selectr);
6081 end if;
6083 elsif No (Typech) then
6084 Typech := Base_Type (Etype (Component));
6086 -- AI05-0199: In Ada 2012, several components of anonymous
6087 -- access types can appear in a choice list, as long as the
6088 -- designated types match.
6090 elsif Typech /= Base_Type (Etype (Component)) then
6091 if Ada_Version >= Ada_2012
6092 and then Ekind (Typech) = E_Anonymous_Access_Type
6093 and then
6094 Ekind (Etype (Component)) = E_Anonymous_Access_Type
6095 and then Base_Type (Designated_Type (Typech)) =
6096 Base_Type (Designated_Type (Etype (Component)))
6097 and then
6098 Subtypes_Statically_Match (Typech, (Etype (Component)))
6099 then
6100 null;
6102 elsif not Box_Present (Parent (Selectr)) then
6103 Error_Msg_N
6104 ("components in choice list must have same type",
6105 Selectr);
6106 end if;
6107 end if;
6109 Next (Selectr);
6110 end loop;
6112 Next (Assoc);
6113 end loop Verification;
6114 end Step_7;
6116 -- STEP 8: replace the original aggregate
6118 Step_8 : declare
6119 New_Aggregate : constant Node_Id := New_Copy (N);
6121 begin
6122 Set_Expressions (New_Aggregate, No_List);
6123 Set_Etype (New_Aggregate, Etype (N));
6124 Set_Component_Associations (New_Aggregate, New_Assoc_List);
6125 Set_Check_Actuals (New_Aggregate, Check_Actuals (N));
6127 Rewrite (N, New_Aggregate);
6128 end Step_8;
6130 -- Check the dimensions of the components in the record aggregate
6132 Analyze_Dimension_Extension_Or_Record_Aggregate (N);
6133 end Resolve_Record_Aggregate;
6135 -----------------------------
6136 -- Check_Can_Never_Be_Null --
6137 -----------------------------
6139 procedure Check_Can_Never_Be_Null (Typ : Entity_Id; Expr : Node_Id) is
6140 Comp_Typ : Entity_Id;
6142 begin
6143 pragma Assert
6144 (Ada_Version >= Ada_2005
6145 and then Present (Expr)
6146 and then Known_Null (Expr));
6148 case Ekind (Typ) is
6149 when E_Array_Type =>
6150 Comp_Typ := Component_Type (Typ);
6152 when E_Component
6153 | E_Discriminant
6155 Comp_Typ := Etype (Typ);
6157 when others =>
6158 return;
6159 end case;
6161 if Can_Never_Be_Null (Comp_Typ) then
6163 -- Here we know we have a constraint error. Note that we do not use
6164 -- Apply_Compile_Time_Constraint_Error here to the Expr, which might
6165 -- seem the more natural approach. That's because in some cases the
6166 -- components are rewritten, and the replacement would be missed.
6167 -- We do not mark the whole aggregate as raising a constraint error,
6168 -- because the association may be a null array range.
6170 Error_Msg_N
6171 ("(Ada 2005) NULL not allowed in null-excluding component??", Expr);
6172 Error_Msg_N
6173 ("\Constraint_Error will be raised at run time??", Expr);
6175 Rewrite (Expr,
6176 Make_Raise_Constraint_Error
6177 (Sloc (Expr), Reason => CE_Access_Check_Failed));
6178 Set_Etype (Expr, Comp_Typ);
6179 Set_Analyzed (Expr);
6180 end if;
6181 end Check_Can_Never_Be_Null;
6183 ---------------------
6184 -- Sort_Case_Table --
6185 ---------------------
6187 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is
6188 U : constant Int := Case_Table'Last;
6189 K : Int;
6190 J : Int;
6191 T : Case_Bounds;
6193 begin
6194 K := 1;
6195 while K < U loop
6196 T := Case_Table (K + 1);
6198 J := K + 1;
6199 while J > 1
6200 and then Expr_Value (Case_Table (J - 1).Lo) > Expr_Value (T.Lo)
6201 loop
6202 Case_Table (J) := Case_Table (J - 1);
6203 J := J - 1;
6204 end loop;
6206 Case_Table (J) := T;
6207 K := K + 1;
6208 end loop;
6209 end Sort_Case_Table;
6211 end Sem_Aggr;