ada: Remove extra parentheses
[official-gcc.git] / gcc / ada / sem_aggr.adb
blobc037201885ebb035d6b760a3857514261c6108ad
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ A G G R --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2023, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Einfo; use Einfo;
30 with Einfo.Entities; use Einfo.Entities;
31 with Einfo.Utils; use Einfo.Utils;
32 with Elists; use Elists;
33 with Errout; use Errout;
34 with Expander; use Expander;
35 with Exp_Ch6; use Exp_Ch6;
36 with Exp_Tss; use Exp_Tss;
37 with Exp_Util; use Exp_Util;
38 with Freeze; use Freeze;
39 with Itypes; use Itypes;
40 with Lib; use Lib;
41 with Lib.Xref; use Lib.Xref;
42 with Namet; use Namet;
43 with Namet.Sp; use Namet.Sp;
44 with Nmake; use Nmake;
45 with Nlists; use Nlists;
46 with Opt; use Opt;
47 with Restrict; use Restrict;
48 with Rident; use Rident;
49 with Sem; use Sem;
50 with Sem_Aux; use Sem_Aux;
51 with Sem_Case; use Sem_Case;
52 with Sem_Cat; use Sem_Cat;
53 with Sem_Ch3; use Sem_Ch3;
54 with Sem_Ch8; use Sem_Ch8;
55 with Sem_Ch13; use Sem_Ch13;
56 with Sem_Dim; use Sem_Dim;
57 with Sem_Eval; use Sem_Eval;
58 with Sem_Res; use Sem_Res;
59 with Sem_Util; use Sem_Util;
60 with Sem_Type; use Sem_Type;
61 with Sem_Warn; use Sem_Warn;
62 with Sinfo; use Sinfo;
63 with Sinfo.Nodes; use Sinfo.Nodes;
64 with Sinfo.Utils; use Sinfo.Utils;
65 with Snames; use Snames;
66 with Stringt; use Stringt;
67 with Stand; use Stand;
68 with Style; use Style;
69 with Targparm; use Targparm;
70 with Tbuild; use Tbuild;
71 with Ttypes; use Ttypes;
72 with Uintp; use Uintp;
73 with Warnsw; use Warnsw;
75 package body Sem_Aggr is
77 type Case_Bounds is record
78 Lo : Node_Id;
79 -- Low bound of choice. Once we sort the Case_Table, then entries
80 -- will be in order of ascending Choice_Lo values.
82 Hi : Node_Id;
83 -- High Bound of choice. The sort does not pay any attention to the
84 -- high bound, so choices 1 .. 4 and 1 .. 5 could be in either order.
86 Highest : Uint;
87 -- If there are duplicates or missing entries, then in the sorted
88 -- table, this records the highest value among Choice_Hi values
89 -- seen so far, including this entry.
91 Choice : Node_Id;
92 -- The node of the choice
93 end record;
95 type Case_Table_Type is array (Pos range <>) of Case_Bounds;
96 -- Table type used by Check_Case_Choices procedure
98 -----------------------
99 -- Local Subprograms --
100 -----------------------
102 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type);
103 -- Sort the Case Table using the Lower Bound of each Choice as the key. A
104 -- simple insertion sort is used since the choices in a case statement will
105 -- usually be in near sorted order.
107 procedure Check_Can_Never_Be_Null (Typ : Entity_Id; Expr : Node_Id);
108 -- Ada 2005 (AI-231): Check bad usage of null for a component for which
109 -- null exclusion (NOT NULL) is specified. Typ can be an E_Array_Type for
110 -- the array case (the component type of the array will be used) or an
111 -- E_Component/E_Discriminant entity in the record case, in which case the
112 -- type of the component will be used for the test. If Typ is any other
113 -- kind of entity, the call is ignored. Expr is the component node in the
114 -- aggregate which is known to have a null value. A warning message will be
115 -- issued if the component is null excluding.
117 -- It would be better to pass the proper type for Typ ???
119 procedure Check_Expr_OK_In_Limited_Aggregate (Expr : Node_Id);
120 -- Check that Expr is either not limited or else is one of the cases of
121 -- expressions allowed for a limited component association (namely, an
122 -- aggregate, function call, or <> notation). Report error for violations.
123 -- Expression is also OK in an instance or inlining context, because we
124 -- have already preanalyzed and it is known to be type correct.
126 ------------------------------------------------------
127 -- Subprograms used for RECORD AGGREGATE Processing --
128 ------------------------------------------------------
130 procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id);
131 -- This procedure performs all the semantic checks required for record
132 -- aggregates. Note that for aggregates analysis and resolution go
133 -- hand in hand. Aggregate analysis has been delayed up to here and
134 -- it is done while resolving the aggregate.
136 -- N is the N_Aggregate node.
137 -- Typ is the record type for the aggregate resolution
139 -- While performing the semantic checks, this procedure builds a new
140 -- Component_Association_List where each record field appears alone in a
141 -- Component_Choice_List along with its corresponding expression. The
142 -- record fields in the Component_Association_List appear in the same order
143 -- in which they appear in the record type Typ.
145 -- Once this new Component_Association_List is built and all the semantic
146 -- checks performed, the original aggregate subtree is replaced with the
147 -- new named record aggregate just built. This new record aggregate has no
148 -- positional associations, so its Expressions field is set to No_List.
149 -- Note that subtree substitution is performed with Rewrite so as to be
150 -- able to retrieve the original aggregate.
152 -- The aggregate subtree manipulation performed by Resolve_Record_Aggregate
153 -- yields the aggregate format expected by Gigi. Typically, this kind of
154 -- tree manipulations are done in the expander. However, because the
155 -- semantic checks that need to be performed on record aggregates really go
156 -- hand in hand with the record aggregate normalization, the aggregate
157 -- subtree transformation is performed during resolution rather than
158 -- expansion. Had we decided otherwise we would have had to duplicate most
159 -- of the code in the expansion procedure Expand_Record_Aggregate. Note,
160 -- however, that all the expansion concerning aggregates for tagged records
161 -- is done in Expand_Record_Aggregate.
163 -- The algorithm of Resolve_Record_Aggregate proceeds as follows:
165 -- 1. Make sure that the record type against which the record aggregate
166 -- has to be resolved is not abstract. Furthermore if the type is a
167 -- null aggregate make sure the input aggregate N is also null.
169 -- 2. Verify that the structure of the aggregate is that of a record
170 -- aggregate. Specifically, look for component associations and ensure
171 -- that each choice list only has identifiers or the N_Others_Choice
172 -- node. Also make sure that if present, the N_Others_Choice occurs
173 -- last and by itself.
175 -- 3. If Typ contains discriminants, the values for each discriminant is
176 -- looked for. If the record type Typ has variants, we check that the
177 -- expressions corresponding to each discriminant ruling the (possibly
178 -- nested) variant parts of Typ, are static. This allows us to determine
179 -- the variant parts to which the rest of the aggregate must conform.
180 -- The names of discriminants with their values are saved in a new
181 -- association list, New_Assoc_List which is later augmented with the
182 -- names and values of the remaining components in the record type.
184 -- During this phase we also make sure that every discriminant is
185 -- assigned exactly one value. Note that when several values for a given
186 -- discriminant are found, semantic processing continues looking for
187 -- further errors. In this case it's the first discriminant value found
188 -- which we will be recorded.
190 -- IMPORTANT NOTE: For derived tagged types this procedure expects
191 -- First_Discriminant and Next_Discriminant to give the correct list
192 -- of discriminants, in the correct order.
194 -- 4. After all the discriminant values have been gathered, we can set the
195 -- Etype of the record aggregate. If Typ contains no discriminants this
196 -- is straightforward: the Etype of N is just Typ, otherwise a new
197 -- implicit constrained subtype of Typ is built to be the Etype of N.
199 -- 5. Gather the remaining record components according to the discriminant
200 -- values. This involves recursively traversing the record type
201 -- structure to see what variants are selected by the given discriminant
202 -- values. This processing is a little more convoluted if Typ is a
203 -- derived tagged types since we need to retrieve the record structure
204 -- of all the ancestors of Typ.
206 -- 6. After gathering the record components we look for their values in the
207 -- record aggregate and emit appropriate error messages should we not
208 -- find such values or should they be duplicated.
210 -- 7. We then make sure no illegal component names appear in the record
211 -- aggregate and make sure that the type of the record components
212 -- appearing in a same choice list is the same. Finally we ensure that
213 -- the others choice, if present, is used to provide the value of at
214 -- least a record component.
216 -- 8. The original aggregate node is replaced with the new named aggregate
217 -- built in steps 3 through 6, as explained earlier.
219 -- Given the complexity of record aggregate resolution, the primary goal of
220 -- this routine is clarity and simplicity rather than execution and storage
221 -- efficiency. If there are only positional components in the aggregate the
222 -- running time is linear. If there are associations the running time is
223 -- still linear as long as the order of the associations is not too far off
224 -- the order of the components in the record type. If this is not the case
225 -- the running time is at worst quadratic in the size of the association
226 -- list.
228 procedure Check_Misspelled_Component
229 (Elements : Elist_Id;
230 Component : Node_Id);
231 -- Give possible misspelling diagnostic if Component is likely to be a
232 -- misspelling of one of the components of the Assoc_List. This is called
233 -- by Resolve_Aggr_Expr after producing an invalid component error message.
235 -----------------------------------------------------
236 -- Subprograms used for ARRAY AGGREGATE Processing --
237 -----------------------------------------------------
239 function Resolve_Array_Aggregate
240 (N : Node_Id;
241 Index : Node_Id;
242 Index_Constr : Node_Id;
243 Component_Typ : Entity_Id;
244 Others_Allowed : Boolean) return Boolean;
245 -- This procedure performs the semantic checks for an array aggregate.
246 -- True is returned if the aggregate resolution succeeds.
248 -- The procedure works by recursively checking each nested aggregate.
249 -- Specifically, after checking a sub-aggregate nested at the i-th level
250 -- we recursively check all the subaggregates at the i+1-st level (if any).
251 -- Note that aggregates analysis and resolution go hand in hand.
252 -- Aggregate analysis has been delayed up to here and it is done while
253 -- resolving the aggregate.
255 -- N is the current N_Aggregate node to be checked.
257 -- Index is the index node corresponding to the array sub-aggregate that
258 -- we are currently checking (RM 4.3.3 (8)). Its Etype is the
259 -- corresponding index type (or subtype).
261 -- Index_Constr is the node giving the applicable index constraint if
262 -- any (RM 4.3.3 (10)). It "is a constraint provided by certain
263 -- contexts [...] that can be used to determine the bounds of the array
264 -- value specified by the aggregate". If Others_Allowed below is False
265 -- there is no applicable index constraint and this node is set to Index.
267 -- Component_Typ is the array component type.
269 -- Others_Allowed indicates whether an others choice is allowed
270 -- in the context where the top-level aggregate appeared.
272 -- The algorithm of Resolve_Array_Aggregate proceeds as follows:
274 -- 1. Make sure that the others choice, if present, is by itself and
275 -- appears last in the sub-aggregate. Check that we do not have
276 -- positional and named components in the array sub-aggregate (unless
277 -- the named association is an others choice). Finally if an others
278 -- choice is present, make sure it is allowed in the aggregate context.
280 -- 2. If the array sub-aggregate contains discrete_choices:
282 -- (A) Verify their validity. Specifically verify that:
284 -- (a) If a null range is present it must be the only possible
285 -- choice in the array aggregate.
287 -- (b) Ditto for a non static range.
289 -- (c) Ditto for a non static expression.
291 -- In addition this step analyzes and resolves each discrete_choice,
292 -- making sure that its type is the type of the corresponding Index.
293 -- If we are not at the lowest array aggregate level (in the case of
294 -- multi-dimensional aggregates) then invoke Resolve_Array_Aggregate
295 -- recursively on each component expression. Otherwise, resolve the
296 -- bottom level component expressions against the expected component
297 -- type ONLY IF the component corresponds to a single discrete choice
298 -- which is not an others choice (to see why read the DELAYED
299 -- COMPONENT RESOLUTION below).
301 -- (B) Determine the bounds of the sub-aggregate and lowest and
302 -- highest choice values.
304 -- 3. For positional aggregates:
306 -- (A) Loop over the component expressions either recursively invoking
307 -- Resolve_Array_Aggregate on each of these for multi-dimensional
308 -- array aggregates or resolving the bottom level component
309 -- expressions against the expected component type.
311 -- (B) Determine the bounds of the positional sub-aggregates.
313 -- 4. Try to determine statically whether the evaluation of the array
314 -- sub-aggregate raises Constraint_Error. If yes emit proper
315 -- warnings. The precise checks are the following:
317 -- (A) Check that the index range defined by aggregate bounds is
318 -- compatible with corresponding index subtype.
319 -- We also check against the base type. In fact it could be that
320 -- Low/High bounds of the base type are static whereas those of
321 -- the index subtype are not. Thus if we can statically catch
322 -- a problem with respect to the base type we are guaranteed
323 -- that the same problem will arise with the index subtype
325 -- (B) If we are dealing with a named aggregate containing an others
326 -- choice and at least one discrete choice then make sure the range
327 -- specified by the discrete choices does not overflow the
328 -- aggregate bounds. We also check against the index type and base
329 -- type bounds for the same reasons given in (A).
331 -- (C) If we are dealing with a positional aggregate with an others
332 -- choice make sure the number of positional elements specified
333 -- does not overflow the aggregate bounds. We also check against
334 -- the index type and base type bounds as mentioned in (A).
336 -- Finally construct an N_Range node giving the sub-aggregate bounds.
337 -- Set the Aggregate_Bounds field of the sub-aggregate to be this
338 -- N_Range. The routine Array_Aggr_Subtype below uses such N_Ranges
339 -- to build the appropriate aggregate subtype. Aggregate_Bounds
340 -- information is needed during expansion.
342 -- DELAYED COMPONENT RESOLUTION: The resolution of bottom level component
343 -- expressions in an array aggregate may call Duplicate_Subexpr or some
344 -- other routine that inserts code just outside the outermost aggregate.
345 -- If the array aggregate contains discrete choices or an others choice,
346 -- this may be wrong. Consider for instance the following example.
348 -- type Rec is record
349 -- V : Integer := 0;
350 -- end record;
352 -- type Acc_Rec is access Rec;
353 -- Arr : array (1..3) of Acc_Rec := (1 .. 3 => new Rec);
355 -- Then the transformation of "new Rec" that occurs during resolution
356 -- entails the following code modifications
358 -- P7b : constant Acc_Rec := new Rec;
359 -- RecIP (P7b.all);
360 -- Arr : array (1..3) of Acc_Rec := (1 .. 3 => P7b);
362 -- This code transformation is clearly wrong, since we need to call
363 -- "new Rec" for each of the 3 array elements. To avoid this problem we
364 -- delay resolution of the components of non positional array aggregates
365 -- to the expansion phase. As an optimization, if the discrete choice
366 -- specifies a single value we do not delay resolution.
368 function Array_Aggr_Subtype (N : Node_Id; Typ : Entity_Id) return Entity_Id;
369 -- This routine returns the type or subtype of an array aggregate.
371 -- N is the array aggregate node whose type we return.
373 -- Typ is the context type in which N occurs.
375 -- This routine creates an implicit array subtype whose bounds are
376 -- those defined by the aggregate. When this routine is invoked
377 -- Resolve_Array_Aggregate has already processed aggregate N. Thus the
378 -- Aggregate_Bounds of each sub-aggregate, is an N_Range node giving the
379 -- sub-aggregate bounds. When building the aggregate itype, this function
380 -- traverses the array aggregate N collecting such Aggregate_Bounds and
381 -- constructs the proper array aggregate itype.
383 -- Note that in the case of multidimensional aggregates each inner
384 -- sub-aggregate corresponding to a given array dimension, may provide a
385 -- different bounds. If it is possible to determine statically that
386 -- some sub-aggregates corresponding to the same index do not have the
387 -- same bounds, then a warning is emitted. If such check is not possible
388 -- statically (because some sub-aggregate bounds are dynamic expressions)
389 -- then this job is left to the expander. In all cases the particular
390 -- bounds that this function will chose for a given dimension is the first
391 -- N_Range node for a sub-aggregate corresponding to that dimension.
393 -- Note that the Raises_Constraint_Error flag of an array aggregate
394 -- whose evaluation is determined to raise CE by Resolve_Array_Aggregate,
395 -- is set in Resolve_Array_Aggregate but the aggregate is not
396 -- immediately replaced with a raise CE. In fact, Array_Aggr_Subtype must
397 -- first construct the proper itype for the aggregate (Gigi needs
398 -- this). After constructing the proper itype we will eventually replace
399 -- the top-level aggregate with a raise CE (done in Resolve_Aggregate).
400 -- Of course in cases such as:
402 -- type Arr is array (integer range <>) of Integer;
403 -- A : Arr := (positive range -1 .. 2 => 0);
405 -- The bounds of the aggregate itype are cooked up to look reasonable
406 -- (in this particular case the bounds will be 1 .. 2).
408 procedure Make_String_Into_Aggregate (N : Node_Id);
409 -- A string literal can appear in a context in which a one dimensional
410 -- array of characters is expected. This procedure simply rewrites the
411 -- string as an aggregate, prior to resolution.
413 function Resolve_Null_Array_Aggregate (N : Node_Id) return Boolean;
414 -- For the Ada 2022 construct, build a subtype with a null range for each
415 -- dimension, using the bounds from the context subtype (if the subtype
416 -- is constrained). If the subtype is unconstrained, then the bounds
417 -- are determined in much the same way as the bounds for a null string
418 -- literal with no applicable index constraint.
420 ---------------------------------
421 -- Delta aggregate processing --
422 ---------------------------------
424 procedure Resolve_Delta_Array_Aggregate (N : Node_Id; Typ : Entity_Id);
425 procedure Resolve_Delta_Record_Aggregate (N : Node_Id; Typ : Entity_Id);
427 ------------------------
428 -- Array_Aggr_Subtype --
429 ------------------------
431 function Array_Aggr_Subtype
432 (N : Node_Id;
433 Typ : Entity_Id) return Entity_Id
435 Aggr_Dimension : constant Pos := Number_Dimensions (Typ);
436 -- Number of aggregate index dimensions
438 Aggr_Range : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
439 -- Constrained N_Range of each index dimension in our aggregate itype
441 Aggr_Low : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
442 Aggr_High : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
443 -- Low and High bounds for each index dimension in our aggregate itype
445 Is_Fully_Positional : Boolean := True;
447 procedure Collect_Aggr_Bounds (N : Node_Id; Dim : Pos);
448 -- N is an array (sub-)aggregate. Dim is the dimension corresponding
449 -- to (sub-)aggregate N. This procedure collects and removes the side
450 -- effects of the constrained N_Range nodes corresponding to each index
451 -- dimension of our aggregate itype. These N_Range nodes are collected
452 -- in Aggr_Range above.
454 -- Likewise collect in Aggr_Low & Aggr_High above the low and high
455 -- bounds of each index dimension. If, when collecting, two bounds
456 -- corresponding to the same dimension are static and found to differ,
457 -- then emit a warning, and mark N as raising Constraint_Error.
459 -------------------------
460 -- Collect_Aggr_Bounds --
461 -------------------------
463 procedure Collect_Aggr_Bounds (N : Node_Id; Dim : Pos) is
464 This_Range : constant Node_Id := Aggregate_Bounds (N);
465 -- The aggregate range node of this specific sub-aggregate
467 This_Low : constant Node_Id := Low_Bound (Aggregate_Bounds (N));
468 This_High : constant Node_Id := High_Bound (Aggregate_Bounds (N));
469 -- The aggregate bounds of this specific sub-aggregate
471 Assoc : Node_Id;
472 Expr : Node_Id;
474 begin
475 Remove_Side_Effects (This_Low, Variable_Ref => True);
476 Remove_Side_Effects (This_High, Variable_Ref => True);
478 -- Collect the first N_Range for a given dimension that you find.
479 -- For a given dimension they must be all equal anyway.
481 if No (Aggr_Range (Dim)) then
482 Aggr_Low (Dim) := This_Low;
483 Aggr_High (Dim) := This_High;
484 Aggr_Range (Dim) := This_Range;
486 else
487 if Compile_Time_Known_Value (This_Low) then
488 if not Compile_Time_Known_Value (Aggr_Low (Dim)) then
489 Aggr_Low (Dim) := This_Low;
491 elsif Expr_Value (This_Low) /= Expr_Value (Aggr_Low (Dim)) then
492 Set_Raises_Constraint_Error (N);
493 Error_Msg_Warn := SPARK_Mode /= On;
494 Error_Msg_N ("sub-aggregate low bound mismatch<<", N);
495 Error_Msg_N ("\Constraint_Error [<<", N);
496 end if;
497 end if;
499 if Compile_Time_Known_Value (This_High) then
500 if not Compile_Time_Known_Value (Aggr_High (Dim)) then
501 Aggr_High (Dim) := This_High;
503 elsif
504 Expr_Value (This_High) /= Expr_Value (Aggr_High (Dim))
505 then
506 Set_Raises_Constraint_Error (N);
507 Error_Msg_Warn := SPARK_Mode /= On;
508 Error_Msg_N ("sub-aggregate high bound mismatch<<", N);
509 Error_Msg_N ("\Constraint_Error [<<", N);
510 end if;
511 end if;
512 end if;
514 if Dim < Aggr_Dimension then
516 -- Process positional components
518 if Present (Expressions (N)) then
519 Expr := First (Expressions (N));
520 while Present (Expr) loop
521 Collect_Aggr_Bounds (Expr, Dim + 1);
522 Next (Expr);
523 end loop;
524 end if;
526 -- Process component associations
528 if Present (Component_Associations (N)) then
529 Is_Fully_Positional := False;
531 Assoc := First (Component_Associations (N));
532 while Present (Assoc) loop
533 Expr := Expression (Assoc);
534 Collect_Aggr_Bounds (Expr, Dim + 1);
535 Next (Assoc);
536 end loop;
537 end if;
538 end if;
539 end Collect_Aggr_Bounds;
541 -- Array_Aggr_Subtype variables
543 Itype : Entity_Id;
544 -- The final itype of the overall aggregate
546 Index_Constraints : constant List_Id := New_List;
547 -- The list of index constraints of the aggregate itype
549 -- Start of processing for Array_Aggr_Subtype
551 begin
552 -- Make sure that the list of index constraints is properly attached to
553 -- the tree, and then collect the aggregate bounds.
555 -- If no aggregaate bounds have been set, this is an aggregate with
556 -- iterator specifications and a dynamic size to be determined by
557 -- first pass of expanded code.
559 if No (Aggregate_Bounds (N)) then
560 return Typ;
561 end if;
563 Set_Parent (Index_Constraints, N);
565 -- When resolving a null aggregate we created a list of aggregate bounds
566 -- for the consecutive dimensions. The bounds for the first dimension
567 -- are attached as the Aggregate_Bounds of the aggregate node.
569 if Is_Null_Aggregate (N) then
570 declare
571 This_Range : Node_Id := Aggregate_Bounds (N);
572 begin
573 for J in 1 .. Aggr_Dimension loop
574 Aggr_Range (J) := This_Range;
575 Next_Index (This_Range);
577 -- Remove bounds from the list, so they can be reattached as
578 -- the First_Index/Next_Index again by the code that also
579 -- handles non-null aggregates.
581 Remove (Aggr_Range (J));
582 end loop;
583 end;
584 else
585 Collect_Aggr_Bounds (N, 1);
586 end if;
588 -- Build the list of constrained indexes of our aggregate itype
590 for J in 1 .. Aggr_Dimension loop
591 Create_Index : declare
592 Index_Base : constant Entity_Id :=
593 Base_Type (Etype (Aggr_Range (J)));
594 Index_Typ : Entity_Id;
596 begin
597 -- Construct the Index subtype, and associate it with the range
598 -- construct that generates it.
600 Index_Typ :=
601 Create_Itype (Subtype_Kind (Ekind (Index_Base)), Aggr_Range (J));
603 Set_Etype (Index_Typ, Index_Base);
605 if Is_Character_Type (Index_Base) then
606 Set_Is_Character_Type (Index_Typ);
607 end if;
609 Set_Size_Info (Index_Typ, (Index_Base));
610 Set_RM_Size (Index_Typ, RM_Size (Index_Base));
611 Set_First_Rep_Item (Index_Typ, First_Rep_Item (Index_Base));
612 Set_Scalar_Range (Index_Typ, Aggr_Range (J));
614 if Is_Discrete_Or_Fixed_Point_Type (Index_Typ) then
615 Set_RM_Size (Index_Typ, UI_From_Int (Minimum_Size (Index_Typ)));
616 end if;
618 Set_Etype (Aggr_Range (J), Index_Typ);
620 Append (Aggr_Range (J), To => Index_Constraints);
621 end Create_Index;
622 end loop;
624 -- Now build the Itype
626 Itype := Create_Itype (E_Array_Subtype, N);
628 Set_First_Rep_Item (Itype, First_Rep_Item (Typ));
629 Set_Convention (Itype, Convention (Typ));
630 Set_Depends_On_Private (Itype, Has_Private_Component (Typ));
631 Set_Etype (Itype, Base_Type (Typ));
632 Set_Has_Alignment_Clause (Itype, Has_Alignment_Clause (Typ));
633 Set_Is_Aliased (Itype, Is_Aliased (Typ));
634 Set_Is_Independent (Itype, Is_Independent (Typ));
635 Set_Depends_On_Private (Itype, Depends_On_Private (Typ));
637 Copy_Suppress_Status (Index_Check, Typ, Itype);
638 Copy_Suppress_Status (Length_Check, Typ, Itype);
640 Set_First_Index (Itype, First (Index_Constraints));
641 Set_Is_Constrained (Itype, True);
642 Set_Is_Internal (Itype, True);
644 if Has_Predicates (Typ) then
645 Set_Has_Predicates (Itype);
647 -- If the base type has a predicate, capture the predicated parent
648 -- or the existing predicate function for SPARK use.
650 if Present (Predicate_Function (Typ)) then
651 Set_Predicate_Function (Itype, Predicate_Function (Typ));
653 elsif Is_Itype (Typ) then
654 Set_Predicated_Parent (Itype, Predicated_Parent (Typ));
656 else
657 Set_Predicated_Parent (Itype, Typ);
658 end if;
659 end if;
661 -- A simple optimization: purely positional aggregates of static
662 -- components should be passed to gigi unexpanded whenever possible, and
663 -- regardless of the staticness of the bounds themselves. Subsequent
664 -- checks in exp_aggr verify that type is not packed, etc.
666 Set_Size_Known_At_Compile_Time
667 (Itype,
668 Is_Fully_Positional
669 and then Comes_From_Source (N)
670 and then Size_Known_At_Compile_Time (Component_Type (Typ)));
672 -- We always need a freeze node for a packed array subtype, so that we
673 -- can build the Packed_Array_Impl_Type corresponding to the subtype. If
674 -- expansion is disabled, the packed array subtype is not built, and we
675 -- must not generate a freeze node for the type, or else it will appear
676 -- incomplete to gigi.
678 if Is_Packed (Itype)
679 and then not In_Spec_Expression
680 and then Expander_Active
681 then
682 Freeze_Itype (Itype, N);
683 end if;
685 return Itype;
686 end Array_Aggr_Subtype;
688 --------------------------------
689 -- Check_Misspelled_Component --
690 --------------------------------
692 procedure Check_Misspelled_Component
693 (Elements : Elist_Id;
694 Component : Node_Id)
696 Max_Suggestions : constant := 2;
698 Nr_Of_Suggestions : Natural := 0;
699 Suggestion_1 : Entity_Id := Empty;
700 Suggestion_2 : Entity_Id := Empty;
701 Component_Elmt : Elmt_Id;
703 begin
704 -- All the components of List are matched against Component and a count
705 -- is maintained of possible misspellings. When at the end of the
706 -- analysis there are one or two (not more) possible misspellings,
707 -- these misspellings will be suggested as possible corrections.
709 Component_Elmt := First_Elmt (Elements);
710 while Nr_Of_Suggestions <= Max_Suggestions
711 and then Present (Component_Elmt)
712 loop
713 if Is_Bad_Spelling_Of
714 (Chars (Node (Component_Elmt)),
715 Chars (Component))
716 then
717 Nr_Of_Suggestions := Nr_Of_Suggestions + 1;
719 case Nr_Of_Suggestions is
720 when 1 => Suggestion_1 := Node (Component_Elmt);
721 when 2 => Suggestion_2 := Node (Component_Elmt);
722 when others => null;
723 end case;
724 end if;
726 Next_Elmt (Component_Elmt);
727 end loop;
729 -- Report at most two suggestions
731 if Nr_Of_Suggestions = 1 then
732 Error_Msg_NE -- CODEFIX
733 ("\possible misspelling of&", Component, Suggestion_1);
735 elsif Nr_Of_Suggestions = 2 then
736 Error_Msg_Node_2 := Suggestion_2;
737 Error_Msg_NE -- CODEFIX
738 ("\possible misspelling of& or&", Component, Suggestion_1);
739 end if;
740 end Check_Misspelled_Component;
742 ----------------------------------------
743 -- Check_Expr_OK_In_Limited_Aggregate --
744 ----------------------------------------
746 procedure Check_Expr_OK_In_Limited_Aggregate (Expr : Node_Id) is
747 begin
748 if Is_Limited_Type (Etype (Expr))
749 and then Comes_From_Source (Expr)
750 then
751 if In_Instance_Body or else In_Inlined_Body then
752 null;
754 elsif not OK_For_Limited_Init (Etype (Expr), Expr) then
755 Error_Msg_N
756 ("initialization not allowed for limited types", Expr);
757 Explain_Limited_Type (Etype (Expr), Expr);
758 end if;
759 end if;
760 end Check_Expr_OK_In_Limited_Aggregate;
762 -------------------------
763 -- Is_Others_Aggregate --
764 -------------------------
766 function Is_Others_Aggregate (Aggr : Node_Id) return Boolean is
767 Assoc : constant List_Id := Component_Associations (Aggr);
769 begin
770 return No (Expressions (Aggr))
771 and then Nkind (First (Choice_List (First (Assoc)))) = N_Others_Choice;
772 end Is_Others_Aggregate;
774 -------------------------
775 -- Is_Single_Aggregate --
776 -------------------------
778 function Is_Single_Aggregate (Aggr : Node_Id) return Boolean is
779 Assoc : constant List_Id := Component_Associations (Aggr);
781 begin
782 return No (Expressions (Aggr))
783 and then No (Next (First (Assoc)))
784 and then No (Next (First (Choice_List (First (Assoc)))));
785 end Is_Single_Aggregate;
787 -----------------------
788 -- Is_Null_Aggregate --
789 -----------------------
791 function Is_Null_Aggregate (N : Node_Id) return Boolean is
792 begin
793 return Ada_Version >= Ada_2022
794 and then Is_Homogeneous_Aggregate (N)
795 and then Is_Empty_List (Expressions (N))
796 and then Is_Empty_List (Component_Associations (N));
797 end Is_Null_Aggregate;
799 ----------------------------------------
800 -- Is_Null_Array_Aggregate_High_Bound --
801 ----------------------------------------
803 function Is_Null_Array_Aggregate_High_Bound (N : Node_Id) return Boolean is
804 Original_N : constant Node_Id := Original_Node (N);
805 begin
806 return Ada_Version >= Ada_2022
807 and then not Comes_From_Source (Original_N)
808 and then Nkind (Original_N) = N_Attribute_Reference
809 and then
810 Get_Attribute_Id (Attribute_Name (Original_N)) = Attribute_Pred
811 and then Nkind (Parent (N)) in N_Range | N_Op_Le
812 and then not Comes_From_Source (Parent (N));
813 end Is_Null_Array_Aggregate_High_Bound;
815 --------------------------------
816 -- Make_String_Into_Aggregate --
817 --------------------------------
819 procedure Make_String_Into_Aggregate (N : Node_Id) is
820 Exprs : constant List_Id := New_List;
821 Loc : constant Source_Ptr := Sloc (N);
822 Str : constant String_Id := Strval (N);
823 Strlen : constant Nat := String_Length (Str);
824 C : Char_Code;
825 C_Node : Node_Id;
826 New_N : Node_Id;
827 P : Source_Ptr;
829 begin
830 P := Loc + 1;
831 for J in 1 .. Strlen loop
832 C := Get_String_Char (Str, J);
833 Set_Character_Literal_Name (C);
835 C_Node :=
836 Make_Character_Literal (P,
837 Chars => Name_Find,
838 Char_Literal_Value => UI_From_CC (C));
839 Set_Etype (C_Node, Any_Character);
840 Append_To (Exprs, C_Node);
842 P := P + 1;
843 -- Something special for wide strings???
844 end loop;
846 New_N := Make_Aggregate (Loc, Expressions => Exprs);
847 Set_Analyzed (New_N);
848 Set_Etype (New_N, Any_Composite);
850 Rewrite (N, New_N);
851 end Make_String_Into_Aggregate;
853 -----------------------
854 -- Resolve_Aggregate --
855 -----------------------
857 procedure Resolve_Aggregate (N : Node_Id; Typ : Entity_Id) is
858 Loc : constant Source_Ptr := Sloc (N);
860 Aggr_Subtyp : Entity_Id;
861 -- The actual aggregate subtype. This is not necessarily the same as Typ
862 -- which is the subtype of the context in which the aggregate was found.
864 Others_Box : Boolean := False;
865 -- Set to True if N represents a simple aggregate with only
866 -- (others => <>), not nested as part of another aggregate.
868 function Is_Full_Access_Aggregate (N : Node_Id) return Boolean;
869 -- If a full access object is initialized with an aggregate or is
870 -- assigned an aggregate, we have to prevent a piecemeal access or
871 -- assignment to the object, even if the aggregate is to be expanded.
872 -- We create a temporary for the aggregate, and assign the temporary
873 -- instead, so that the back end can generate an atomic move for it.
874 -- This is only done in the context of an object declaration or an
875 -- assignment. Function is a noop and returns false in other contexts.
877 function Within_Aggregate (N : Node_Id) return Boolean;
878 -- Return True if N is part of an N_Aggregate
880 ------------------------------
881 -- Is_Full_Access_Aggregate --
882 ------------------------------
884 function Is_Full_Access_Aggregate (N : Node_Id) return Boolean is
885 Loc : constant Source_Ptr := Sloc (N);
887 New_N : Node_Id;
888 Par : Node_Id;
889 Temp : Entity_Id;
890 Typ : Entity_Id;
892 begin
893 Par := Parent (N);
895 -- Aggregate may be qualified, so find outer context
897 if Nkind (Par) = N_Qualified_Expression then
898 Par := Parent (Par);
899 end if;
901 if not Comes_From_Source (Par) then
902 return False;
903 end if;
905 case Nkind (Par) is
906 when N_Assignment_Statement =>
907 Typ := Etype (Name (Par));
909 if not Is_Full_Access (Typ)
910 and then not Is_Full_Access_Object (Name (Par))
911 then
912 return False;
913 end if;
915 when N_Object_Declaration =>
916 Typ := Etype (Defining_Identifier (Par));
918 if not Is_Full_Access (Typ)
919 and then not Is_Full_Access (Defining_Identifier (Par))
920 then
921 return False;
922 end if;
924 when others =>
925 return False;
926 end case;
928 Temp := Make_Temporary (Loc, 'T', N);
929 New_N :=
930 Make_Object_Declaration (Loc,
931 Defining_Identifier => Temp,
932 Constant_Present => True,
933 Object_Definition => New_Occurrence_Of (Typ, Loc),
934 Expression => Relocate_Node (N));
935 Insert_Action (Par, New_N);
937 Rewrite (N, New_Occurrence_Of (Temp, Loc));
938 Analyze_And_Resolve (N, Typ);
940 return True;
941 end Is_Full_Access_Aggregate;
943 ----------------------
944 -- Within_Aggregate --
945 ----------------------
947 function Within_Aggregate (N : Node_Id) return Boolean is
948 P : Node_Id := Parent (N);
949 begin
950 while Present (P) loop
951 if Nkind (P) = N_Aggregate then
952 return True;
953 end if;
955 P := Parent (P);
956 end loop;
958 return False;
959 end Within_Aggregate;
961 -- Start of processing for Resolve_Aggregate
963 begin
964 -- Ignore junk empty aggregate resulting from parser error
966 if No (Expressions (N))
967 and then No (Component_Associations (N))
968 and then not Null_Record_Present (N)
969 then
970 return;
972 -- If the aggregate is assigned to a full access variable, we have
973 -- to prevent a piecemeal assignment even if the aggregate is to be
974 -- expanded. We create a temporary for the aggregate, and assign the
975 -- temporary instead, so that the back end can generate an atomic move
976 -- for it. This is properly an expansion activity but it must be done
977 -- before resolution because aggregate resolution cannot be done twice.
979 elsif Expander_Active and then Is_Full_Access_Aggregate (N) then
980 return;
981 end if;
983 -- If the aggregate has box-initialized components, its type must be
984 -- frozen so that initialization procedures can properly be called
985 -- in the resolution that follows. The replacement of boxes with
986 -- initialization calls is properly an expansion activity but it must
987 -- be done during resolution.
989 if Expander_Active
990 and then Present (Component_Associations (N))
991 then
992 declare
993 Comp : Node_Id;
994 First_Comp : Boolean := True;
996 begin
997 Comp := First (Component_Associations (N));
998 while Present (Comp) loop
999 if Box_Present (Comp) then
1000 if First_Comp
1001 and then No (Expressions (N))
1002 and then Nkind (First (Choices (Comp))) = N_Others_Choice
1003 and then not Within_Aggregate (N)
1004 then
1005 Others_Box := True;
1006 end if;
1008 Insert_Actions (N, Freeze_Entity (Typ, N));
1009 exit;
1010 end if;
1012 First_Comp := False;
1013 Next (Comp);
1014 end loop;
1015 end;
1016 end if;
1018 -- Check for aggregates not allowed in configurable run-time mode.
1019 -- We allow all cases of aggregates that do not come from source, since
1020 -- these are all assumed to be small (e.g. bounds of a string literal).
1021 -- We also allow aggregates of types we know to be small.
1023 if not Support_Aggregates_On_Target
1024 and then Comes_From_Source (N)
1025 and then (not Known_Static_Esize (Typ)
1026 or else Esize (Typ) > System_Max_Integer_Size)
1027 then
1028 Error_Msg_CRT ("aggregate", N);
1029 end if;
1031 -- Ada 2005 (AI-287): Limited aggregates allowed
1033 -- In an instance, ignore aggregate subcomponents that may be limited,
1034 -- because they originate in view conflicts. If the original aggregate
1035 -- is legal and the actuals are legal, the aggregate itself is legal.
1037 if Is_Limited_Type (Typ)
1038 and then Ada_Version < Ada_2005
1039 and then not In_Instance
1040 then
1041 Error_Msg_N ("aggregate type cannot be limited", N);
1042 Explain_Limited_Type (Typ, N);
1044 elsif Is_Class_Wide_Type (Typ) then
1045 Error_Msg_N ("type of aggregate cannot be class-wide", N);
1047 elsif Typ = Any_String
1048 or else Typ = Any_Composite
1049 then
1050 Error_Msg_N ("no unique type for aggregate", N);
1051 Set_Etype (N, Any_Composite);
1053 elsif Is_Array_Type (Typ) and then Null_Record_Present (N) then
1054 Error_Msg_N ("null record forbidden in array aggregate", N);
1056 elsif Has_Aspect (Typ, Aspect_Aggregate)
1057 and then Ekind (Typ) /= E_Record_Type
1058 and then Ada_Version >= Ada_2022
1059 then
1060 -- Check for Ada 2022 and () aggregate.
1062 if not Is_Homogeneous_Aggregate (N) then
1063 Error_Msg_N ("container aggregate must use '['], not ()", N);
1064 end if;
1066 Resolve_Container_Aggregate (N, Typ);
1068 elsif Is_Record_Type (Typ) then
1069 Resolve_Record_Aggregate (N, Typ);
1071 elsif Is_Array_Type (Typ) then
1073 -- First a special test, for the case of a positional aggregate of
1074 -- characters which can be replaced by a string literal.
1076 -- Do not perform this transformation if this was a string literal
1077 -- to start with, whose components needed constraint checks, or if
1078 -- the component type is non-static, because it will require those
1079 -- checks and be transformed back into an aggregate. If the index
1080 -- type is not Integer the aggregate may represent a user-defined
1081 -- string type but the context might need the original type so we
1082 -- do not perform the transformation at this point.
1084 if Number_Dimensions (Typ) = 1
1085 and then Is_Standard_Character_Type (Component_Type (Typ))
1086 and then No (Component_Associations (N))
1087 and then not Is_Limited_Composite (Typ)
1088 and then not Is_Private_Composite (Typ)
1089 and then not Is_Bit_Packed_Array (Typ)
1090 and then Nkind (Original_Node (Parent (N))) /= N_String_Literal
1091 and then Is_OK_Static_Subtype (Component_Type (Typ))
1092 and then Base_Type (Etype (First_Index (Typ))) =
1093 Base_Type (Standard_Integer)
1094 then
1095 declare
1096 Expr : Node_Id;
1098 begin
1099 Expr := First (Expressions (N));
1100 while Present (Expr) loop
1101 exit when Nkind (Expr) /= N_Character_Literal;
1102 Next (Expr);
1103 end loop;
1105 if No (Expr) then
1106 Start_String;
1108 Expr := First (Expressions (N));
1109 while Present (Expr) loop
1110 Store_String_Char (UI_To_CC (Char_Literal_Value (Expr)));
1111 Next (Expr);
1112 end loop;
1114 Rewrite (N, Make_String_Literal (Loc, End_String));
1116 Analyze_And_Resolve (N, Typ);
1117 return;
1118 end if;
1119 end;
1120 end if;
1122 -- Here if we have a real aggregate to deal with
1124 Array_Aggregate : declare
1125 Aggr_Resolved : Boolean;
1126 Aggr_Typ : constant Entity_Id := Etype (Typ);
1127 -- This is the unconstrained array type, which is the type against
1128 -- which the aggregate is to be resolved. Typ itself is the array
1129 -- type of the context which may not be the same subtype as the
1130 -- subtype for the final aggregate.
1132 Is_Null_Aggr : constant Boolean := Is_Null_Aggregate (N);
1134 begin
1135 -- In the following we determine whether an OTHERS choice is
1136 -- allowed inside the array aggregate. The test checks the context
1137 -- in which the array aggregate occurs. If the context does not
1138 -- permit it, or the aggregate type is unconstrained, an OTHERS
1139 -- choice is not allowed (except that it is always allowed on the
1140 -- right-hand side of an assignment statement; in this case the
1141 -- constrainedness of the type doesn't matter, because an array
1142 -- object is always constrained).
1144 -- If expansion is disabled (generic context, or semantics-only
1145 -- mode) actual subtypes cannot be constructed, and the type of an
1146 -- object may be its unconstrained nominal type. However, if the
1147 -- context is an assignment statement, OTHERS is allowed, because
1148 -- the target of the assignment will have a constrained subtype
1149 -- when fully compiled. Ditto if the context is an initialization
1150 -- procedure where a component may have a predicate function that
1151 -- carries the base type.
1153 -- Note that there is no node for Explicit_Actual_Parameter.
1154 -- To test for this context we therefore have to test for node
1155 -- N_Parameter_Association which itself appears only if there is a
1156 -- formal parameter. Consequently we also need to test for
1157 -- N_Procedure_Call_Statement or N_Function_Call.
1159 -- The context may be an N_Reference node, created by expansion.
1160 -- Legality of the others clause was established in the source,
1161 -- so the context is legal.
1163 Set_Etype (N, Aggr_Typ); -- May be overridden later on
1165 if Is_Null_Aggr then
1166 Set_Etype (N, Typ);
1167 Aggr_Resolved := Resolve_Null_Array_Aggregate (N);
1169 elsif Nkind (Parent (N)) = N_Assignment_Statement
1170 or else Inside_Init_Proc
1171 or else (Is_Constrained (Typ)
1172 and then Nkind (Parent (N)) in
1173 N_Parameter_Association
1174 | N_Function_Call
1175 | N_Procedure_Call_Statement
1176 | N_Generic_Association
1177 | N_Formal_Object_Declaration
1178 | N_Simple_Return_Statement
1179 | N_Object_Declaration
1180 | N_Component_Declaration
1181 | N_Parameter_Specification
1182 | N_Qualified_Expression
1183 | N_Unchecked_Type_Conversion
1184 | N_Reference
1185 | N_Aggregate
1186 | N_Extension_Aggregate
1187 | N_Component_Association
1188 | N_Case_Expression_Alternative
1189 | N_If_Expression
1190 | N_Expression_With_Actions)
1191 then
1192 Aggr_Resolved :=
1193 Resolve_Array_Aggregate
1195 Index => First_Index (Aggr_Typ),
1196 Index_Constr => First_Index (Typ),
1197 Component_Typ => Component_Type (Typ),
1198 Others_Allowed => True);
1199 else
1200 Aggr_Resolved :=
1201 Resolve_Array_Aggregate
1203 Index => First_Index (Aggr_Typ),
1204 Index_Constr => First_Index (Aggr_Typ),
1205 Component_Typ => Component_Type (Typ),
1206 Others_Allowed => False);
1207 end if;
1209 if not Aggr_Resolved then
1211 -- A parenthesized expression may have been intended as an
1212 -- aggregate, leading to a type error when analyzing the
1213 -- component. This can also happen for a nested component
1214 -- (see Analyze_Aggr_Expr).
1216 if Paren_Count (N) > 0 then
1217 Error_Msg_N
1218 ("positional aggregate cannot have one component", N);
1219 end if;
1221 Aggr_Subtyp := Any_Composite;
1223 else
1224 Aggr_Subtyp := Array_Aggr_Subtype (N, Typ);
1225 end if;
1227 Set_Etype (N, Aggr_Subtyp);
1228 end Array_Aggregate;
1230 elsif Is_Private_Type (Typ)
1231 and then Present (Full_View (Typ))
1232 and then (In_Inlined_Body or In_Instance_Body)
1233 and then Is_Composite_Type (Full_View (Typ))
1234 then
1235 Resolve (N, Full_View (Typ));
1237 else
1238 Error_Msg_N ("illegal context for aggregate", N);
1239 end if;
1241 -- If we can determine statically that the evaluation of the aggregate
1242 -- raises Constraint_Error, then replace the aggregate with an
1243 -- N_Raise_Constraint_Error node, but set the Etype to the right
1244 -- aggregate subtype. Gigi needs this.
1246 if Raises_Constraint_Error (N) then
1247 Aggr_Subtyp := Etype (N);
1248 Rewrite (N,
1249 Make_Raise_Constraint_Error (Loc, Reason => CE_Range_Check_Failed));
1250 Set_Raises_Constraint_Error (N);
1251 Set_Etype (N, Aggr_Subtyp);
1252 Set_Analyzed (N);
1253 end if;
1255 if Warn_On_No_Value_Assigned
1256 and then Others_Box
1257 and then not Is_Fully_Initialized_Type (Etype (N))
1258 then
1259 Error_Msg_N ("?v?aggregate not fully initialized", N);
1260 end if;
1262 Check_Function_Writable_Actuals (N);
1263 end Resolve_Aggregate;
1265 -----------------------------
1266 -- Resolve_Array_Aggregate --
1267 -----------------------------
1269 function Resolve_Array_Aggregate
1270 (N : Node_Id;
1271 Index : Node_Id;
1272 Index_Constr : Node_Id;
1273 Component_Typ : Entity_Id;
1274 Others_Allowed : Boolean) return Boolean
1276 Loc : constant Source_Ptr := Sloc (N);
1278 Failure : constant Boolean := False;
1279 Success : constant Boolean := True;
1281 Index_Typ : constant Entity_Id := Etype (Index);
1282 Index_Typ_Low : constant Node_Id := Type_Low_Bound (Index_Typ);
1283 Index_Typ_High : constant Node_Id := Type_High_Bound (Index_Typ);
1284 -- The type of the index corresponding to the array sub-aggregate along
1285 -- with its low and upper bounds.
1287 Index_Base : constant Entity_Id := Base_Type (Index_Typ);
1288 Index_Base_Low : constant Node_Id := Type_Low_Bound (Index_Base);
1289 Index_Base_High : constant Node_Id := Type_High_Bound (Index_Base);
1290 -- Ditto for the base type
1292 Others_Present : Boolean := False;
1294 Nb_Choices : Nat := 0;
1295 -- Contains the overall number of named choices in this sub-aggregate
1297 function Add (Val : Uint; To : Node_Id) return Node_Id;
1298 -- Creates a new expression node where Val is added to expression To.
1299 -- Tries to constant fold whenever possible. To must be an already
1300 -- analyzed expression.
1302 procedure Check_Bound (BH : Node_Id; AH : in out Node_Id);
1303 -- Checks that AH (the upper bound of an array aggregate) is less than
1304 -- or equal to BH (the upper bound of the index base type). If the check
1305 -- fails, a warning is emitted, the Raises_Constraint_Error flag of N is
1306 -- set, and AH is replaced with a duplicate of BH.
1308 procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id);
1309 -- Checks that range AL .. AH is compatible with range L .. H. Emits a
1310 -- warning if not and sets the Raises_Constraint_Error flag in N.
1312 procedure Check_Length (L, H : Node_Id; Len : Uint);
1313 -- Checks that range L .. H contains at least Len elements. Emits a
1314 -- warning if not and sets the Raises_Constraint_Error flag in N.
1316 function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean;
1317 -- Returns True if range L .. H is dynamic or null
1319 procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean);
1320 -- Given expression node From, this routine sets OK to False if it
1321 -- cannot statically evaluate From. Otherwise it stores this static
1322 -- value into Value.
1324 function Resolve_Aggr_Expr
1325 (Expr : Node_Id;
1326 Single_Elmt : Boolean) return Boolean;
1327 -- Resolves aggregate expression Expr. Returns False if resolution
1328 -- fails. If Single_Elmt is set to False, the expression Expr may be
1329 -- used to initialize several array aggregate elements (this can happen
1330 -- for discrete choices such as "L .. H => Expr" or the OTHERS choice).
1331 -- In this event we do not resolve Expr unless expansion is disabled.
1332 -- To know why, see the DELAYED COMPONENT RESOLUTION note above.
1334 -- NOTE: In the case of "... => <>", we pass the in the
1335 -- N_Component_Association node as Expr, since there is no Expression in
1336 -- that case, and we need a Sloc for the error message.
1338 procedure Resolve_Iterated_Component_Association
1339 (N : Node_Id;
1340 Index_Typ : Entity_Id);
1341 -- For AI12-061
1343 ---------
1344 -- Add --
1345 ---------
1347 function Add (Val : Uint; To : Node_Id) return Node_Id is
1348 Expr_Pos : Node_Id;
1349 Expr : Node_Id;
1350 To_Pos : Node_Id;
1352 begin
1353 if Raises_Constraint_Error (To) then
1354 return To;
1355 end if;
1357 -- First test if we can do constant folding
1359 if Compile_Time_Known_Value (To)
1360 or else Nkind (To) = N_Integer_Literal
1361 then
1362 Expr_Pos := Make_Integer_Literal (Loc, Expr_Value (To) + Val);
1363 Set_Is_Static_Expression (Expr_Pos);
1364 Set_Etype (Expr_Pos, Etype (To));
1365 Set_Analyzed (Expr_Pos, Analyzed (To));
1367 if not Is_Enumeration_Type (Index_Typ) then
1368 Expr := Expr_Pos;
1370 -- If we are dealing with enumeration return
1371 -- Index_Typ'Val (Expr_Pos)
1373 else
1374 Expr :=
1375 Make_Attribute_Reference
1376 (Loc,
1377 Prefix => New_Occurrence_Of (Index_Typ, Loc),
1378 Attribute_Name => Name_Val,
1379 Expressions => New_List (Expr_Pos));
1380 end if;
1382 return Expr;
1383 end if;
1385 -- If we are here no constant folding possible
1387 if not Is_Enumeration_Type (Index_Base) then
1388 Expr :=
1389 Make_Op_Add (Loc,
1390 Left_Opnd => Duplicate_Subexpr (To),
1391 Right_Opnd => Make_Integer_Literal (Loc, Val));
1393 -- If we are dealing with enumeration return
1394 -- Index_Typ'Val (Index_Typ'Pos (To) + Val)
1396 else
1397 To_Pos :=
1398 Make_Attribute_Reference
1399 (Loc,
1400 Prefix => New_Occurrence_Of (Index_Typ, Loc),
1401 Attribute_Name => Name_Pos,
1402 Expressions => New_List (Duplicate_Subexpr (To)));
1404 Expr_Pos :=
1405 Make_Op_Add (Loc,
1406 Left_Opnd => To_Pos,
1407 Right_Opnd => Make_Integer_Literal (Loc, Val));
1409 Expr :=
1410 Make_Attribute_Reference
1411 (Loc,
1412 Prefix => New_Occurrence_Of (Index_Typ, Loc),
1413 Attribute_Name => Name_Val,
1414 Expressions => New_List (Expr_Pos));
1416 -- If the index type has a non standard representation, the
1417 -- attributes 'Val and 'Pos expand into function calls and the
1418 -- resulting expression is considered non-safe for reevaluation
1419 -- by the backend. Relocate it into a constant temporary in order
1420 -- to make it safe for reevaluation.
1422 if Has_Non_Standard_Rep (Etype (N)) then
1423 declare
1424 Def_Id : Entity_Id;
1426 begin
1427 Def_Id := Make_Temporary (Loc, 'R', Expr);
1428 Set_Etype (Def_Id, Index_Typ);
1429 Insert_Action (N,
1430 Make_Object_Declaration (Loc,
1431 Defining_Identifier => Def_Id,
1432 Object_Definition =>
1433 New_Occurrence_Of (Index_Typ, Loc),
1434 Constant_Present => True,
1435 Expression => Relocate_Node (Expr)));
1437 Expr := New_Occurrence_Of (Def_Id, Loc);
1438 end;
1439 end if;
1440 end if;
1442 return Expr;
1443 end Add;
1445 -----------------
1446 -- Check_Bound --
1447 -----------------
1449 procedure Check_Bound (BH : Node_Id; AH : in out Node_Id) is
1450 Val_BH : Uint;
1451 Val_AH : Uint;
1453 OK_BH : Boolean;
1454 OK_AH : Boolean;
1456 begin
1457 Get (Value => Val_BH, From => BH, OK => OK_BH);
1458 Get (Value => Val_AH, From => AH, OK => OK_AH);
1460 if OK_BH and then OK_AH and then Val_BH < Val_AH then
1461 Set_Raises_Constraint_Error (N);
1462 Error_Msg_Warn := SPARK_Mode /= On;
1463 Error_Msg_N ("upper bound out of range<<", AH);
1464 Error_Msg_N ("\Constraint_Error [<<", AH);
1466 -- You need to set AH to BH or else in the case of enumerations
1467 -- indexes we will not be able to resolve the aggregate bounds.
1469 AH := Duplicate_Subexpr (BH);
1470 end if;
1471 end Check_Bound;
1473 ------------------
1474 -- Check_Bounds --
1475 ------------------
1477 procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id) is
1478 Val_L : Uint;
1479 Val_H : Uint;
1480 Val_AL : Uint;
1481 Val_AH : Uint;
1483 OK_L : Boolean;
1484 OK_H : Boolean;
1486 OK_AL : Boolean;
1487 OK_AH : Boolean;
1488 pragma Warnings (Off, OK_AL);
1489 pragma Warnings (Off, OK_AH);
1491 begin
1492 if Raises_Constraint_Error (N)
1493 or else Dynamic_Or_Null_Range (AL, AH)
1494 then
1495 return;
1496 end if;
1498 Get (Value => Val_L, From => L, OK => OK_L);
1499 Get (Value => Val_H, From => H, OK => OK_H);
1501 Get (Value => Val_AL, From => AL, OK => OK_AL);
1502 Get (Value => Val_AH, From => AH, OK => OK_AH);
1504 if OK_L and then Val_L > Val_AL then
1505 Set_Raises_Constraint_Error (N);
1506 Error_Msg_Warn := SPARK_Mode /= On;
1507 Error_Msg_N ("lower bound of aggregate out of range<<", N);
1508 Error_Msg_N ("\Constraint_Error [<<", N);
1509 end if;
1511 if OK_H and then Val_H < Val_AH then
1512 Set_Raises_Constraint_Error (N);
1513 Error_Msg_Warn := SPARK_Mode /= On;
1514 Error_Msg_N ("upper bound of aggregate out of range<<", N);
1515 Error_Msg_N ("\Constraint_Error [<<", N);
1516 end if;
1517 end Check_Bounds;
1519 ------------------
1520 -- Check_Length --
1521 ------------------
1523 procedure Check_Length (L, H : Node_Id; Len : Uint) is
1524 Val_L : Uint;
1525 Val_H : Uint;
1527 OK_L : Boolean;
1528 OK_H : Boolean;
1530 Range_Len : Uint;
1532 begin
1533 if Raises_Constraint_Error (N) then
1534 return;
1535 end if;
1537 Get (Value => Val_L, From => L, OK => OK_L);
1538 Get (Value => Val_H, From => H, OK => OK_H);
1540 if not OK_L or else not OK_H then
1541 return;
1542 end if;
1544 -- If null range length is zero
1546 if Val_L > Val_H then
1547 Range_Len := Uint_0;
1548 else
1549 Range_Len := Val_H - Val_L + 1;
1550 end if;
1552 if Range_Len < Len then
1553 Set_Raises_Constraint_Error (N);
1554 Error_Msg_Warn := SPARK_Mode /= On;
1555 Error_Msg_N ("too many elements<<", N);
1556 Error_Msg_N ("\Constraint_Error [<<", N);
1557 end if;
1558 end Check_Length;
1560 ---------------------------
1561 -- Dynamic_Or_Null_Range --
1562 ---------------------------
1564 function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean is
1565 Val_L : Uint;
1566 Val_H : Uint;
1568 OK_L : Boolean;
1569 OK_H : Boolean;
1571 begin
1572 Get (Value => Val_L, From => L, OK => OK_L);
1573 Get (Value => Val_H, From => H, OK => OK_H);
1575 return not OK_L or else not OK_H
1576 or else not Is_OK_Static_Expression (L)
1577 or else not Is_OK_Static_Expression (H)
1578 or else Val_L > Val_H;
1579 end Dynamic_Or_Null_Range;
1581 ---------
1582 -- Get --
1583 ---------
1585 procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean) is
1586 begin
1587 OK := True;
1589 if Compile_Time_Known_Value (From) then
1590 Value := Expr_Value (From);
1592 -- If expression From is something like Some_Type'Val (10) then
1593 -- Value = 10.
1595 elsif Nkind (From) = N_Attribute_Reference
1596 and then Attribute_Name (From) = Name_Val
1597 and then Compile_Time_Known_Value (First (Expressions (From)))
1598 then
1599 Value := Expr_Value (First (Expressions (From)));
1600 else
1601 Value := Uint_0;
1602 OK := False;
1603 end if;
1604 end Get;
1606 -----------------------
1607 -- Resolve_Aggr_Expr --
1608 -----------------------
1610 function Resolve_Aggr_Expr
1611 (Expr : Node_Id;
1612 Single_Elmt : Boolean) return Boolean
1614 Nxt_Ind : constant Node_Id := Next_Index (Index);
1615 Nxt_Ind_Constr : constant Node_Id := Next_Index (Index_Constr);
1616 -- Index is the current index corresponding to the expression
1618 Resolution_OK : Boolean := True;
1619 -- Set to False if resolution of the expression failed
1621 begin
1622 -- Defend against previous errors
1624 if Nkind (Expr) = N_Error
1625 or else Error_Posted (Expr)
1626 then
1627 return True;
1628 end if;
1630 -- If the array type against which we are resolving the aggregate
1631 -- has several dimensions, the expressions nested inside the
1632 -- aggregate must be further aggregates (or strings).
1634 if Present (Nxt_Ind) then
1635 if Nkind (Expr) /= N_Aggregate then
1637 -- A string literal can appear where a one-dimensional array
1638 -- of characters is expected. If the literal looks like an
1639 -- operator, it is still an operator symbol, which will be
1640 -- transformed into a string when analyzed.
1642 if Is_Character_Type (Component_Typ)
1643 and then No (Next_Index (Nxt_Ind))
1644 and then Nkind (Expr) in N_String_Literal | N_Operator_Symbol
1645 then
1646 -- A string literal used in a multidimensional array
1647 -- aggregate in place of the final one-dimensional
1648 -- aggregate must not be enclosed in parentheses.
1650 if Paren_Count (Expr) /= 0 then
1651 Error_Msg_N ("no parenthesis allowed here", Expr);
1652 end if;
1654 Make_String_Into_Aggregate (Expr);
1656 else
1657 Error_Msg_N ("nested array aggregate expected", Expr);
1659 -- If the expression is parenthesized, this may be
1660 -- a missing component association for a 1-aggregate.
1662 if Paren_Count (Expr) > 0 then
1663 Error_Msg_N
1664 ("\if single-component aggregate is intended, "
1665 & "write e.g. (1 ='> ...)", Expr);
1666 end if;
1668 return Failure;
1669 end if;
1670 end if;
1672 -- If it's "... => <>", nothing to resolve
1674 if Nkind (Expr) = N_Component_Association then
1675 pragma Assert (Box_Present (Expr));
1676 return Success;
1677 end if;
1679 -- Ada 2005 (AI-231): Propagate the type to the nested aggregate.
1680 -- Required to check the null-exclusion attribute (if present).
1681 -- This value may be overridden later on.
1683 Set_Etype (Expr, Etype (N));
1685 Resolution_OK := Resolve_Array_Aggregate
1686 (Expr, Nxt_Ind, Nxt_Ind_Constr, Component_Typ, Others_Allowed);
1688 else
1689 -- If it's "... => <>", nothing to resolve
1691 if Nkind (Expr) = N_Component_Association then
1692 pragma Assert (Box_Present (Expr));
1693 return Success;
1694 end if;
1696 -- Do not resolve the expressions of discrete or others choices
1697 -- unless the expression covers a single component, or the
1698 -- expander is inactive.
1700 -- In SPARK mode, expressions that can perform side effects will
1701 -- be recognized by the gnat2why back-end, and the whole
1702 -- subprogram will be ignored. So semantic analysis can be
1703 -- performed safely.
1705 if Single_Elmt
1706 or else not Expander_Active
1707 or else In_Spec_Expression
1708 then
1709 Analyze_And_Resolve (Expr, Component_Typ);
1710 Check_Expr_OK_In_Limited_Aggregate (Expr);
1711 Check_Non_Static_Context (Expr);
1712 Aggregate_Constraint_Checks (Expr, Component_Typ);
1713 Check_Unset_Reference (Expr);
1714 end if;
1715 end if;
1717 -- If an aggregate component has a type with predicates, an explicit
1718 -- predicate check must be applied, as for an assignment statement,
1719 -- because the aggregate might not be expanded into individual
1720 -- component assignments. If the expression covers several components
1721 -- the analysis and the predicate check take place later.
1723 if Has_Predicates (Component_Typ)
1724 and then Analyzed (Expr)
1725 then
1726 Apply_Predicate_Check (Expr, Component_Typ);
1727 end if;
1729 if Raises_Constraint_Error (Expr)
1730 and then Nkind (Parent (Expr)) /= N_Component_Association
1731 then
1732 Set_Raises_Constraint_Error (N);
1733 end if;
1735 -- If the expression has been marked as requiring a range check,
1736 -- then generate it here. It's a bit odd to be generating such
1737 -- checks in the analyzer, but harmless since Generate_Range_Check
1738 -- does nothing (other than making sure Do_Range_Check is set) if
1739 -- the expander is not active.
1741 if Do_Range_Check (Expr) then
1742 Generate_Range_Check (Expr, Component_Typ, CE_Range_Check_Failed);
1743 end if;
1745 return Resolution_OK;
1746 end Resolve_Aggr_Expr;
1748 --------------------------------------------
1749 -- Resolve_Iterated_Component_Association --
1750 --------------------------------------------
1752 procedure Resolve_Iterated_Component_Association
1753 (N : Node_Id;
1754 Index_Typ : Entity_Id)
1756 Loc : constant Source_Ptr := Sloc (N);
1757 Id : constant Entity_Id := Defining_Identifier (N);
1759 -----------------------
1760 -- Remove_References --
1761 -----------------------
1763 function Remove_Reference (N : Node_Id) return Traverse_Result;
1764 -- Remove reference to the entity Id after analysis, so it can be
1765 -- properly reanalyzed after construct is expanded into a loop.
1767 function Remove_Reference (N : Node_Id) return Traverse_Result is
1768 begin
1769 if Nkind (N) = N_Identifier
1770 and then Present (Entity (N))
1771 and then Entity (N) = Id
1772 then
1773 Set_Entity (N, Empty);
1774 Set_Etype (N, Empty);
1775 end if;
1776 Set_Analyzed (N, False);
1777 return OK;
1778 end Remove_Reference;
1780 procedure Remove_References is new Traverse_Proc (Remove_Reference);
1782 -- Local variables
1784 Choice : Node_Id;
1785 Dummy : Boolean;
1786 Scop : Entity_Id;
1787 Expr : Node_Id;
1789 -- Start of processing for Resolve_Iterated_Component_Association
1791 begin
1792 Error_Msg_Ada_2022_Feature ("iterated component", Loc);
1794 -- Create a scope in which to introduce an index, to make it visible
1795 -- for the analysis of component expression.
1797 Scop := New_Internal_Entity (E_Loop, Current_Scope, Loc, 'L');
1798 Set_Etype (Scop, Standard_Void_Type);
1799 Set_Parent (Scop, Parent (N));
1800 Push_Scope (Scop);
1802 -- If there is iterator specification, then its preanalysis will make
1803 -- the index visible.
1805 if Present (Iterator_Specification (N)) then
1806 Preanalyze (Iterator_Specification (N));
1808 -- Otherwise, analyze discrete choices and make the index visible
1810 else
1811 -- Insert index name into current scope but don't decorate it yet,
1812 -- so that a premature usage of this name in discrete choices will
1813 -- be nicely diagnosed.
1815 Enter_Name (Id);
1817 Choice := First (Discrete_Choices (N));
1819 while Present (Choice) loop
1820 if Nkind (Choice) = N_Others_Choice then
1821 Others_Present := True;
1823 else
1824 Analyze (Choice);
1826 -- Choice can be a subtype name, a range, or an expression
1828 if Is_Entity_Name (Choice)
1829 and then Is_Type (Entity (Choice))
1830 and then
1831 Base_Type (Entity (Choice)) = Base_Type (Index_Typ)
1832 then
1833 null;
1835 else
1836 Analyze_And_Resolve (Choice, Index_Typ);
1837 end if;
1838 end if;
1840 Next (Choice);
1841 end loop;
1843 -- Decorate the index variable
1845 Set_Etype (Id, Index_Typ);
1846 Mutate_Ekind (Id, E_Variable);
1847 Set_Scope (Id, Scop);
1848 end if;
1850 -- Analyze expression without expansion, to verify legality.
1851 -- When generating code, we then remove references to the index
1852 -- variable, because the expression will be analyzed anew after
1853 -- rewritting as a loop with a new index variable; when not
1854 -- generating code we leave the analyzed expression as it is.
1856 Expr := Expression (N);
1858 Expander_Mode_Save_And_Set (False);
1859 Dummy := Resolve_Aggr_Expr (Expr, Single_Elmt => False);
1860 Expander_Mode_Restore;
1862 if Operating_Mode /= Check_Semantics then
1863 Remove_References (Expr);
1864 end if;
1866 -- An iterated_component_association may appear in a nested
1867 -- aggregate for a multidimensional structure: preserve the bounds
1868 -- computed for the expression, as well as the anonymous array
1869 -- type generated for it; both are needed during array expansion.
1871 if Nkind (Expr) = N_Aggregate then
1872 Set_Aggregate_Bounds (Expression (N), Aggregate_Bounds (Expr));
1873 Set_Etype (Expression (N), Etype (Expr));
1874 end if;
1876 End_Scope;
1877 end Resolve_Iterated_Component_Association;
1879 -- Local variables
1881 Assoc : Node_Id;
1882 Choice : Node_Id;
1883 Expr : Node_Id;
1884 Discard : Node_Id;
1886 Aggr_Low : Node_Id := Empty;
1887 Aggr_High : Node_Id := Empty;
1888 -- The actual low and high bounds of this sub-aggregate
1890 Case_Table_Size : Nat;
1891 -- Contains the size of the case table needed to sort aggregate choices
1893 Choices_Low : Node_Id := Empty;
1894 Choices_High : Node_Id := Empty;
1895 -- The lowest and highest discrete choices values for a named aggregate
1897 Delete_Choice : Boolean;
1898 -- Used when replacing a subtype choice with predicate by a list
1900 Has_Iterator_Specifications : Boolean := False;
1901 -- Flag to indicate that all named associations are iterated component
1902 -- associations with iterator specifications, in which case the
1903 -- expansion will create two loops: one to evaluate the size and one
1904 -- to generate the elements (4.3.3 (20.2/5)).
1906 Nb_Elements : Uint := Uint_0;
1907 -- The number of elements in a positional aggregate
1909 Nb_Discrete_Choices : Nat := 0;
1910 -- The overall number of discrete choices (not counting others choice)
1912 -- Start of processing for Resolve_Array_Aggregate
1914 begin
1915 -- Ignore junk empty aggregate resulting from parser error
1917 if No (Expressions (N))
1918 and then No (Component_Associations (N))
1919 and then not Null_Record_Present (N)
1920 then
1921 return Failure;
1922 end if;
1924 -- Disable the warning for GNAT Mode to allow for easier transition.
1926 if Ada_Version_Explicit >= Ada_2022
1927 and then Warn_On_Obsolescent_Feature
1928 and then not GNAT_Mode
1929 and then not Is_Homogeneous_Aggregate (N)
1930 and then not Is_Enum_Array_Aggregate (N)
1931 and then Is_Parenthesis_Aggregate (N)
1932 and then Nkind (Parent (N)) /= N_Qualified_Expression
1933 and then Comes_From_Source (N)
1934 then
1935 Error_Msg_N
1936 ("?j?array aggregate using () is an" &
1937 " obsolescent syntax, use '['] instead", N);
1938 end if;
1940 -- STEP 1: make sure the aggregate is correctly formatted
1942 if Present (Component_Associations (N)) then
1944 -- Verify that all or none of the component associations
1945 -- include an iterator specification.
1947 Assoc := First (Component_Associations (N));
1948 if Nkind (Assoc) = N_Iterated_Component_Association
1949 and then Present (Iterator_Specification (Assoc))
1950 then
1951 -- All other component associations must have an iterator spec.
1953 Next (Assoc);
1954 while Present (Assoc) loop
1955 if Nkind (Assoc) /= N_Iterated_Component_Association
1956 or else No (Iterator_Specification (Assoc))
1957 then
1958 Error_Msg_N ("mixed iterated component association"
1959 & " (RM 4.3.3 (17.1/5))",
1960 Assoc);
1961 return Failure;
1962 end if;
1964 Next (Assoc);
1965 end loop;
1967 Has_Iterator_Specifications := True;
1969 else
1970 -- or none of them do.
1972 Next (Assoc);
1973 while Present (Assoc) loop
1974 if Nkind (Assoc) = N_Iterated_Component_Association
1975 and then Present (Iterator_Specification (Assoc))
1976 then
1977 Error_Msg_N ("mixed iterated component association"
1978 & " (RM 4.3.3 (17.1/5))",
1979 Assoc);
1980 return Failure;
1981 end if;
1983 Next (Assoc);
1984 end loop;
1986 end if;
1988 Assoc := First (Component_Associations (N));
1989 while Present (Assoc) loop
1990 if Nkind (Assoc) = N_Iterated_Component_Association then
1991 Resolve_Iterated_Component_Association (Assoc, Index_Typ);
1993 elsif Nkind (Assoc) /= N_Component_Association then
1994 Error_Msg_N
1995 ("invalid component association for aggregate", Assoc);
1996 return Failure;
1997 end if;
1999 Choice := First (Choice_List (Assoc));
2000 Delete_Choice := False;
2001 while Present (Choice) loop
2002 if Nkind (Choice) = N_Others_Choice then
2003 Others_Present := True;
2005 if Choice /= First (Choice_List (Assoc))
2006 or else Present (Next (Choice))
2007 then
2008 Error_Msg_N
2009 ("OTHERS must appear alone in a choice list", Choice);
2010 return Failure;
2011 end if;
2013 if Present (Next (Assoc)) then
2014 Error_Msg_N
2015 ("OTHERS must appear last in an aggregate", Choice);
2016 return Failure;
2017 end if;
2019 if Ada_Version = Ada_83
2020 and then Assoc /= First (Component_Associations (N))
2021 and then Nkind (Parent (N)) in
2022 N_Assignment_Statement | N_Object_Declaration
2023 then
2024 Error_Msg_N
2025 ("(Ada 83) illegal context for OTHERS choice", N);
2026 end if;
2028 elsif Is_Entity_Name (Choice) then
2029 Analyze (Choice);
2031 declare
2032 E : constant Entity_Id := Entity (Choice);
2033 New_Cs : List_Id;
2034 P : Node_Id;
2035 C : Node_Id;
2037 begin
2038 if Is_Type (E) and then Has_Predicates (E) then
2039 Freeze_Before (N, E);
2041 if Has_Dynamic_Predicate_Aspect (E) then
2042 Error_Msg_NE
2043 ("subtype& has dynamic predicate, not allowed "
2044 & "in aggregate choice", Choice, E);
2046 elsif not Is_OK_Static_Subtype (E) then
2047 Error_Msg_NE
2048 ("non-static subtype& has predicate, not allowed "
2049 & "in aggregate choice", Choice, E);
2050 end if;
2052 -- If the subtype has a static predicate, replace the
2053 -- original choice with the list of individual values
2054 -- covered by the predicate.
2055 -- This should be deferred to expansion time ???
2057 if Present (Static_Discrete_Predicate (E)) then
2058 Delete_Choice := True;
2060 New_Cs := New_List;
2061 P := First (Static_Discrete_Predicate (E));
2062 while Present (P) loop
2063 C := New_Copy (P);
2064 Set_Sloc (C, Sloc (Choice));
2065 Append_To (New_Cs, C);
2066 Next (P);
2067 end loop;
2069 Insert_List_After (Choice, New_Cs);
2070 end if;
2071 end if;
2072 end;
2073 end if;
2075 Nb_Choices := Nb_Choices + 1;
2077 declare
2078 C : constant Node_Id := Choice;
2080 begin
2081 Next (Choice);
2083 if Delete_Choice then
2084 Remove (C);
2085 Nb_Choices := Nb_Choices - 1;
2086 Delete_Choice := False;
2087 end if;
2088 end;
2089 end loop;
2091 Next (Assoc);
2092 end loop;
2093 end if;
2095 -- At this point we know that the others choice, if present, is by
2096 -- itself and appears last in the aggregate. Check if we have mixed
2097 -- positional and discrete associations (other than the others choice).
2099 if Present (Expressions (N))
2100 and then (Nb_Choices > 1
2101 or else (Nb_Choices = 1 and then not Others_Present))
2102 then
2103 Error_Msg_N
2104 ("cannot mix named and positional associations in array aggregate",
2105 First (Choice_List (First (Component_Associations (N)))));
2106 return Failure;
2107 end if;
2109 -- Test for the validity of an others choice if present
2111 if Others_Present and then not Others_Allowed then
2112 declare
2113 Others_N : constant Node_Id :=
2114 First (Choice_List (First (Component_Associations (N))));
2115 begin
2116 Error_Msg_N ("OTHERS choice not allowed here", Others_N);
2117 Error_Msg_N ("\qualify the aggregate with a constrained subtype "
2118 & "to provide bounds for it", Others_N);
2119 return Failure;
2120 end;
2121 end if;
2123 -- Protect against cascaded errors
2125 if Etype (Index_Typ) = Any_Type then
2126 return Failure;
2127 end if;
2129 -- STEP 2: Process named components
2131 if No (Expressions (N)) then
2132 if Others_Present then
2133 Case_Table_Size := Nb_Choices - 1;
2134 else
2135 Case_Table_Size := Nb_Choices;
2136 end if;
2138 Step_2 : declare
2139 function Empty_Range (A : Node_Id) return Boolean;
2140 -- If an association covers an empty range, some warnings on the
2141 -- expression of the association can be disabled.
2143 -----------------
2144 -- Empty_Range --
2145 -----------------
2147 function Empty_Range (A : Node_Id) return Boolean is
2148 R : constant Node_Id := First (Choices (A));
2149 begin
2150 return No (Next (R))
2151 and then Nkind (R) = N_Range
2152 and then Compile_Time_Compare
2153 (Low_Bound (R), High_Bound (R), False) = GT;
2154 end Empty_Range;
2156 -- Local variables
2158 Low : Node_Id;
2159 High : Node_Id;
2160 -- Denote the lowest and highest values in an aggregate choice
2162 S_Low : Node_Id := Empty;
2163 S_High : Node_Id := Empty;
2164 -- if a choice in an aggregate is a subtype indication these
2165 -- denote the lowest and highest values of the subtype
2167 Table : Case_Table_Type (1 .. Case_Table_Size);
2168 -- Used to sort all the different choice values
2170 Single_Choice : Boolean;
2171 -- Set to true every time there is a single discrete choice in a
2172 -- discrete association
2174 Prev_Nb_Discrete_Choices : Nat;
2175 -- Used to keep track of the number of discrete choices in the
2176 -- current association.
2178 Errors_Posted_On_Choices : Boolean := False;
2179 -- Keeps track of whether any choices have semantic errors
2181 -- Start of processing for Step_2
2183 begin
2184 -- STEP 2 (A): Check discrete choices validity
2185 -- No need if this is an element iteration.
2187 Assoc := First (Component_Associations (N));
2188 while Present (Assoc)
2189 and then Present (Choice_List (Assoc))
2190 loop
2191 Prev_Nb_Discrete_Choices := Nb_Discrete_Choices;
2192 Choice := First (Choice_List (Assoc));
2194 loop
2195 Analyze (Choice);
2197 if Nkind (Choice) = N_Others_Choice then
2198 Single_Choice := False;
2199 exit;
2201 -- Test for subtype mark without constraint
2203 elsif Is_Entity_Name (Choice) and then
2204 Is_Type (Entity (Choice))
2205 then
2206 if Base_Type (Entity (Choice)) /= Index_Base then
2207 Error_Msg_N
2208 ("invalid subtype mark in aggregate choice",
2209 Choice);
2210 return Failure;
2211 end if;
2213 -- Case of subtype indication
2215 elsif Nkind (Choice) = N_Subtype_Indication then
2216 Resolve_Discrete_Subtype_Indication (Choice, Index_Base);
2218 if Has_Dynamic_Predicate_Aspect
2219 (Entity (Subtype_Mark (Choice)))
2220 then
2221 Error_Msg_NE
2222 ("subtype& has dynamic predicate, "
2223 & "not allowed in aggregate choice",
2224 Choice, Entity (Subtype_Mark (Choice)));
2225 end if;
2227 -- Does the subtype indication evaluation raise CE?
2229 Get_Index_Bounds (Subtype_Mark (Choice), S_Low, S_High);
2230 Get_Index_Bounds (Choice, Low, High);
2231 Check_Bounds (S_Low, S_High, Low, High);
2233 -- Case of range or expression
2235 else
2236 Resolve (Choice, Index_Base);
2237 Check_Unset_Reference (Choice);
2238 Check_Non_Static_Context (Choice);
2240 -- If semantic errors were posted on the choice, then
2241 -- record that for possible early return from later
2242 -- processing (see handling of enumeration choices).
2244 if Error_Posted (Choice) then
2245 Errors_Posted_On_Choices := True;
2246 end if;
2248 -- Do not range check a choice. This check is redundant
2249 -- since this test is already done when we check that the
2250 -- bounds of the array aggregate are within range.
2252 Set_Do_Range_Check (Choice, False);
2253 end if;
2255 -- If we could not resolve the discrete choice stop here
2257 if Etype (Choice) = Any_Type then
2258 return Failure;
2260 -- If the discrete choice raises CE get its original bounds
2262 elsif Nkind (Choice) = N_Raise_Constraint_Error then
2263 Set_Raises_Constraint_Error (N);
2264 Get_Index_Bounds (Original_Node (Choice), Low, High);
2266 -- Otherwise get its bounds as usual
2268 else
2269 Get_Index_Bounds (Choice, Low, High);
2270 end if;
2272 if (Dynamic_Or_Null_Range (Low, High)
2273 or else (Nkind (Choice) = N_Subtype_Indication
2274 and then
2275 Dynamic_Or_Null_Range (S_Low, S_High)))
2276 and then Nb_Choices /= 1
2277 then
2278 Error_Msg_N
2279 ("dynamic or empty choice in aggregate "
2280 & "must be the only choice", Choice);
2281 return Failure;
2282 end if;
2284 if not (All_Composite_Constraints_Static (Low)
2285 and then All_Composite_Constraints_Static (High)
2286 and then All_Composite_Constraints_Static (S_Low)
2287 and then All_Composite_Constraints_Static (S_High))
2288 then
2289 Check_Restriction (No_Dynamic_Sized_Objects, Choice);
2290 end if;
2292 Nb_Discrete_Choices := Nb_Discrete_Choices + 1;
2293 Table (Nb_Discrete_Choices).Lo := Low;
2294 Table (Nb_Discrete_Choices).Hi := High;
2295 Table (Nb_Discrete_Choices).Choice := Choice;
2297 Next (Choice);
2299 if No (Choice) then
2301 -- Check if we have a single discrete choice and whether
2302 -- this discrete choice specifies a single value.
2304 Single_Choice :=
2305 Nb_Discrete_Choices = Prev_Nb_Discrete_Choices + 1
2306 and then Low = High;
2308 exit;
2309 end if;
2310 end loop;
2312 -- Ada 2005 (AI-231)
2314 if Ada_Version >= Ada_2005
2315 and then Known_Null (Expression (Assoc))
2316 and then not Empty_Range (Assoc)
2317 then
2318 Check_Can_Never_Be_Null (Etype (N), Expression (Assoc));
2319 end if;
2321 -- Ada 2005 (AI-287): In case of default initialized component
2322 -- we delay the resolution to the expansion phase.
2324 if Box_Present (Assoc) then
2326 -- Ada 2005 (AI-287): In case of default initialization of a
2327 -- component the expander will generate calls to the
2328 -- corresponding initialization subprogram. We need to call
2329 -- Resolve_Aggr_Expr to check the rules about
2330 -- dimensionality.
2332 if not Resolve_Aggr_Expr
2333 (Assoc, Single_Elmt => Single_Choice)
2334 then
2335 return Failure;
2336 end if;
2338 -- ??? Checks for dynamically tagged expressions below will
2339 -- be only applied to iterated_component_association after
2340 -- expansion; in particular, errors might not be reported when
2341 -- -gnatc switch is used.
2343 elsif Nkind (Assoc) = N_Iterated_Component_Association then
2344 null; -- handled above, in a loop context
2346 elsif not Resolve_Aggr_Expr
2347 (Expression (Assoc), Single_Elmt => Single_Choice)
2348 then
2349 return Failure;
2351 -- Check incorrect use of dynamically tagged expression
2353 -- We differentiate here two cases because the expression may
2354 -- not be decorated. For example, the analysis and resolution
2355 -- of the expression associated with the others choice will be
2356 -- done later with the full aggregate. In such case we
2357 -- duplicate the expression tree to analyze the copy and
2358 -- perform the required check.
2360 elsif No (Etype (Expression (Assoc))) then
2361 declare
2362 Save_Analysis : constant Boolean := Full_Analysis;
2363 Expr : constant Node_Id :=
2364 New_Copy_Tree (Expression (Assoc));
2366 begin
2367 Expander_Mode_Save_And_Set (False);
2368 Full_Analysis := False;
2370 -- Analyze the expression, making sure it is properly
2371 -- attached to the tree before we do the analysis.
2373 Set_Parent (Expr, Parent (Expression (Assoc)));
2374 Analyze (Expr);
2376 -- Compute its dimensions now, rather than at the end of
2377 -- resolution, because in the case of multidimensional
2378 -- aggregates subsequent expansion may lead to spurious
2379 -- errors.
2381 Check_Expression_Dimensions (Expr, Component_Typ);
2383 -- If the expression is a literal, propagate this info
2384 -- to the expression in the association, to enable some
2385 -- optimizations downstream.
2387 if Is_Entity_Name (Expr)
2388 and then Present (Entity (Expr))
2389 and then Ekind (Entity (Expr)) = E_Enumeration_Literal
2390 then
2391 Analyze_And_Resolve
2392 (Expression (Assoc), Component_Typ);
2393 end if;
2395 Full_Analysis := Save_Analysis;
2396 Expander_Mode_Restore;
2398 if Is_Tagged_Type (Etype (Expr)) then
2399 Check_Dynamically_Tagged_Expression
2400 (Expr => Expr,
2401 Typ => Component_Type (Etype (N)),
2402 Related_Nod => N);
2403 end if;
2404 end;
2406 elsif Is_Tagged_Type (Etype (Expression (Assoc))) then
2407 Check_Dynamically_Tagged_Expression
2408 (Expr => Expression (Assoc),
2409 Typ => Component_Type (Etype (N)),
2410 Related_Nod => N);
2411 end if;
2413 Next (Assoc);
2414 end loop;
2416 -- If aggregate contains more than one choice then these must be
2417 -- static. Check for duplicate and missing values.
2419 -- Note: there is duplicated code here wrt Check_Choice_Set in
2420 -- the body of Sem_Case, and it is possible we could just reuse
2421 -- that procedure. To be checked ???
2423 if Nb_Discrete_Choices > 1 then
2424 Check_Choices : declare
2425 Choice : Node_Id;
2426 -- Location of choice for messages
2428 Hi_Val : Uint;
2429 Lo_Val : Uint;
2430 -- High end of one range and Low end of the next. Should be
2431 -- contiguous if there is no hole in the list of values.
2433 Lo_Dup : Uint;
2434 Hi_Dup : Uint;
2435 -- End points of duplicated range
2437 Missing_Or_Duplicates : Boolean := False;
2438 -- Set True if missing or duplicate choices found
2440 procedure Output_Bad_Choices (Lo, Hi : Uint; C : Node_Id);
2441 -- Output continuation message with a representation of the
2442 -- bounds (just Lo if Lo = Hi, else Lo .. Hi). C is the
2443 -- choice node where the message is to be posted.
2445 ------------------------
2446 -- Output_Bad_Choices --
2447 ------------------------
2449 procedure Output_Bad_Choices (Lo, Hi : Uint; C : Node_Id) is
2450 begin
2451 -- Enumeration type case
2453 if Is_Enumeration_Type (Index_Typ) then
2454 Error_Msg_Name_1 :=
2455 Chars (Get_Enum_Lit_From_Pos (Index_Typ, Lo, Loc));
2456 Error_Msg_Name_2 :=
2457 Chars (Get_Enum_Lit_From_Pos (Index_Typ, Hi, Loc));
2459 if Lo = Hi then
2460 Error_Msg_N ("\\ %!", C);
2461 else
2462 Error_Msg_N ("\\ % .. %!", C);
2463 end if;
2465 -- Integer types case
2467 else
2468 Error_Msg_Uint_1 := Lo;
2469 Error_Msg_Uint_2 := Hi;
2471 if Lo = Hi then
2472 Error_Msg_N ("\\ ^!", C);
2473 else
2474 Error_Msg_N ("\\ ^ .. ^!", C);
2475 end if;
2476 end if;
2477 end Output_Bad_Choices;
2479 -- Start of processing for Check_Choices
2481 begin
2482 Sort_Case_Table (Table);
2484 -- First we do a quick linear loop to find out if we have
2485 -- any duplicates or missing entries (usually we have a
2486 -- legal aggregate, so this will get us out quickly).
2488 for J in 1 .. Nb_Discrete_Choices - 1 loop
2489 Hi_Val := Expr_Value (Table (J).Hi);
2490 Lo_Val := Expr_Value (Table (J + 1).Lo);
2492 if Lo_Val <= Hi_Val
2493 or else (Lo_Val > Hi_Val + 1
2494 and then not Others_Present)
2495 then
2496 Missing_Or_Duplicates := True;
2497 exit;
2498 end if;
2499 end loop;
2501 -- If we have missing or duplicate entries, first fill in
2502 -- the Highest entries to make life easier in the following
2503 -- loops to detect bad entries.
2505 if Missing_Or_Duplicates then
2506 Table (1).Highest := Expr_Value (Table (1).Hi);
2508 for J in 2 .. Nb_Discrete_Choices loop
2509 Table (J).Highest :=
2510 UI_Max
2511 (Table (J - 1).Highest, Expr_Value (Table (J).Hi));
2512 end loop;
2514 -- Loop through table entries to find duplicate indexes
2516 for J in 2 .. Nb_Discrete_Choices loop
2517 Lo_Val := Expr_Value (Table (J).Lo);
2518 Hi_Val := Expr_Value (Table (J).Hi);
2520 -- Case where we have duplicates (the lower bound of
2521 -- this choice is less than or equal to the highest
2522 -- high bound found so far).
2524 if Lo_Val <= Table (J - 1).Highest then
2526 -- We move backwards looking for duplicates. We can
2527 -- abandon this loop as soon as we reach a choice
2528 -- highest value that is less than Lo_Val.
2530 for K in reverse 1 .. J - 1 loop
2531 exit when Table (K).Highest < Lo_Val;
2533 -- Here we may have duplicates between entries
2534 -- for K and J. Get range of duplicates.
2536 Lo_Dup :=
2537 UI_Max (Lo_Val, Expr_Value (Table (K).Lo));
2538 Hi_Dup :=
2539 UI_Min (Hi_Val, Expr_Value (Table (K).Hi));
2541 -- Nothing to do if duplicate range is null
2543 if Lo_Dup > Hi_Dup then
2544 null;
2546 -- Otherwise place proper message
2548 else
2549 -- We place message on later choice, with a
2550 -- line reference to the earlier choice.
2552 if Sloc (Table (J).Choice) <
2553 Sloc (Table (K).Choice)
2554 then
2555 Choice := Table (K).Choice;
2556 Error_Msg_Sloc := Sloc (Table (J).Choice);
2557 else
2558 Choice := Table (J).Choice;
2559 Error_Msg_Sloc := Sloc (Table (K).Choice);
2560 end if;
2562 if Lo_Dup = Hi_Dup then
2563 Error_Msg_N
2564 ("index value in array aggregate "
2565 & "duplicates the one given#!", Choice);
2566 else
2567 Error_Msg_N
2568 ("index values in array aggregate "
2569 & "duplicate those given#!", Choice);
2570 end if;
2572 Output_Bad_Choices (Lo_Dup, Hi_Dup, Choice);
2573 end if;
2574 end loop;
2575 end if;
2576 end loop;
2578 -- Loop through entries in table to find missing indexes.
2579 -- Not needed if others, since missing impossible.
2581 if not Others_Present then
2582 for J in 2 .. Nb_Discrete_Choices loop
2583 Lo_Val := Expr_Value (Table (J).Lo);
2584 Hi_Val := Table (J - 1).Highest;
2586 if Lo_Val > Hi_Val + 1 then
2588 declare
2589 Error_Node : Node_Id;
2591 begin
2592 -- If the choice is the bound of a range in
2593 -- a subtype indication, it is not in the
2594 -- source lists for the aggregate itself, so
2595 -- post the error on the aggregate. Otherwise
2596 -- post it on choice itself.
2598 Choice := Table (J).Choice;
2600 if Is_List_Member (Choice) then
2601 Error_Node := Choice;
2602 else
2603 Error_Node := N;
2604 end if;
2606 if Hi_Val + 1 = Lo_Val - 1 then
2607 Error_Msg_N
2608 ("missing index value "
2609 & "in array aggregate!", Error_Node);
2610 else
2611 Error_Msg_N
2612 ("missing index values "
2613 & "in array aggregate!", Error_Node);
2614 end if;
2616 Output_Bad_Choices
2617 (Hi_Val + 1, Lo_Val - 1, Error_Node);
2618 end;
2619 end if;
2620 end loop;
2621 end if;
2623 -- If either missing or duplicate values, return failure
2625 Set_Etype (N, Any_Composite);
2626 return Failure;
2627 end if;
2628 end Check_Choices;
2629 end if;
2631 if Has_Iterator_Specifications then
2632 -- Bounds will be determined dynamically.
2634 return Success;
2635 end if;
2637 -- STEP 2 (B): Compute aggregate bounds and min/max choices values
2639 if Nb_Discrete_Choices > 0 then
2640 Choices_Low := Table (1).Lo;
2641 Choices_High := Table (Nb_Discrete_Choices).Hi;
2642 end if;
2644 -- If Others is present, then bounds of aggregate come from the
2645 -- index constraint (not the choices in the aggregate itself).
2647 if Others_Present then
2648 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2650 -- Abandon processing if either bound is already signalled as
2651 -- an error (prevents junk cascaded messages and blow ups).
2653 if Nkind (Aggr_Low) = N_Error
2654 or else
2655 Nkind (Aggr_High) = N_Error
2656 then
2657 return False;
2658 end if;
2660 -- No others clause present
2662 else
2663 -- Special processing if others allowed and not present. This
2664 -- means that the bounds of the aggregate come from the index
2665 -- constraint (and the length must match).
2667 if Others_Allowed then
2668 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2670 -- Abandon processing if either bound is already signalled
2671 -- as an error (stop junk cascaded messages and blow ups).
2673 if Nkind (Aggr_Low) = N_Error
2674 or else
2675 Nkind (Aggr_High) = N_Error
2676 then
2677 return False;
2678 end if;
2680 -- If others allowed, and no others present, then the array
2681 -- should cover all index values. If it does not, we will
2682 -- get a length check warning, but there is two cases where
2683 -- an additional warning is useful:
2685 -- If we have no positional components, and the length is
2686 -- wrong (which we can tell by others being allowed with
2687 -- missing components), and the index type is an enumeration
2688 -- type, then issue appropriate warnings about these missing
2689 -- components. They are only warnings, since the aggregate
2690 -- is fine, it's just the wrong length. We skip this check
2691 -- for standard character types (since there are no literals
2692 -- and it is too much trouble to concoct them), and also if
2693 -- any of the bounds have values that are not known at
2694 -- compile time.
2696 -- Another case warranting a warning is when the length
2697 -- is right, but as above we have an index type that is
2698 -- an enumeration, and the bounds do not match. This is a
2699 -- case where dubious sliding is allowed and we generate a
2700 -- warning that the bounds do not match.
2702 if No (Expressions (N))
2703 and then Nkind (Index) = N_Range
2704 and then Is_Enumeration_Type (Etype (Index))
2705 and then not Is_Standard_Character_Type (Etype (Index))
2706 and then Compile_Time_Known_Value (Aggr_Low)
2707 and then Compile_Time_Known_Value (Aggr_High)
2708 and then Compile_Time_Known_Value (Choices_Low)
2709 and then Compile_Time_Known_Value (Choices_High)
2710 then
2711 -- If any of the expressions or range bounds in choices
2712 -- have semantic errors, then do not attempt further
2713 -- resolution, to prevent cascaded errors.
2715 if Errors_Posted_On_Choices then
2716 return Failure;
2717 end if;
2719 declare
2720 ALo : constant Node_Id := Expr_Value_E (Aggr_Low);
2721 AHi : constant Node_Id := Expr_Value_E (Aggr_High);
2722 CLo : constant Node_Id := Expr_Value_E (Choices_Low);
2723 CHi : constant Node_Id := Expr_Value_E (Choices_High);
2725 Ent : Entity_Id;
2727 begin
2728 -- Warning case 1, missing values at start/end. Only
2729 -- do the check if the number of entries is too small.
2731 if (Enumeration_Pos (CHi) - Enumeration_Pos (CLo))
2733 (Enumeration_Pos (AHi) - Enumeration_Pos (ALo))
2734 then
2735 Error_Msg_N
2736 ("missing index value(s) in array aggregate??",
2739 -- Output missing value(s) at start
2741 if Chars (ALo) /= Chars (CLo) then
2742 Ent := Prev (CLo);
2744 if Chars (ALo) = Chars (Ent) then
2745 Error_Msg_Name_1 := Chars (ALo);
2746 Error_Msg_N ("\ %??", N);
2747 else
2748 Error_Msg_Name_1 := Chars (ALo);
2749 Error_Msg_Name_2 := Chars (Ent);
2750 Error_Msg_N ("\ % .. %??", N);
2751 end if;
2752 end if;
2754 -- Output missing value(s) at end
2756 if Chars (AHi) /= Chars (CHi) then
2757 Ent := Next (CHi);
2759 if Chars (AHi) = Chars (Ent) then
2760 Error_Msg_Name_1 := Chars (Ent);
2761 Error_Msg_N ("\ %??", N);
2762 else
2763 Error_Msg_Name_1 := Chars (Ent);
2764 Error_Msg_Name_2 := Chars (AHi);
2765 Error_Msg_N ("\ % .. %??", N);
2766 end if;
2767 end if;
2769 -- Warning case 2, dubious sliding. The First_Subtype
2770 -- test distinguishes between a constrained type where
2771 -- sliding is not allowed (so we will get a warning
2772 -- later that Constraint_Error will be raised), and
2773 -- the unconstrained case where sliding is permitted.
2775 elsif (Enumeration_Pos (CHi) - Enumeration_Pos (CLo))
2777 (Enumeration_Pos (AHi) - Enumeration_Pos (ALo))
2778 and then Chars (ALo) /= Chars (CLo)
2779 and then
2780 not Is_Constrained (First_Subtype (Etype (N)))
2781 then
2782 Error_Msg_N
2783 ("bounds of aggregate do not match target??", N);
2784 end if;
2785 end;
2786 end if;
2787 end if;
2789 -- If no others, aggregate bounds come from aggregate
2791 Aggr_Low := Choices_Low;
2792 Aggr_High := Choices_High;
2793 end if;
2794 end Step_2;
2796 -- STEP 3: Process positional components
2798 else
2799 -- STEP 3 (A): Process positional elements
2801 Expr := First (Expressions (N));
2802 Nb_Elements := Uint_0;
2803 while Present (Expr) loop
2804 Nb_Elements := Nb_Elements + 1;
2806 -- Ada 2005 (AI-231)
2808 if Ada_Version >= Ada_2005 and then Known_Null (Expr) then
2809 Check_Can_Never_Be_Null (Etype (N), Expr);
2810 end if;
2812 if not Resolve_Aggr_Expr (Expr, Single_Elmt => True) then
2813 return Failure;
2814 end if;
2816 -- Check incorrect use of dynamically tagged expression
2818 if Is_Tagged_Type (Etype (Expr)) then
2819 Check_Dynamically_Tagged_Expression
2820 (Expr => Expr,
2821 Typ => Component_Type (Etype (N)),
2822 Related_Nod => N);
2823 end if;
2825 Next (Expr);
2826 end loop;
2828 if Others_Present then
2829 Assoc := Last (Component_Associations (N));
2831 -- Ada 2005 (AI-231)
2833 if Ada_Version >= Ada_2005 and then Known_Null (Assoc) then
2834 Check_Can_Never_Be_Null (Etype (N), Expression (Assoc));
2835 end if;
2837 -- Ada 2005 (AI-287): In case of default initialized component,
2838 -- we delay the resolution to the expansion phase.
2840 if Box_Present (Assoc) then
2842 -- Ada 2005 (AI-287): In case of default initialization of a
2843 -- component the expander will generate calls to the
2844 -- corresponding initialization subprogram. We need to call
2845 -- Resolve_Aggr_Expr to check the rules about
2846 -- dimensionality.
2848 if not Resolve_Aggr_Expr (Assoc, Single_Elmt => False) then
2849 return Failure;
2850 end if;
2852 elsif not Resolve_Aggr_Expr (Expression (Assoc),
2853 Single_Elmt => False)
2854 then
2855 return Failure;
2857 -- Check incorrect use of dynamically tagged expression. The
2858 -- expression of the others choice has not been resolved yet.
2859 -- In order to diagnose the semantic error we create a duplicate
2860 -- tree to analyze it and perform the check.
2862 elsif Nkind (Assoc) /= N_Iterated_Component_Association then
2863 declare
2864 Save_Analysis : constant Boolean := Full_Analysis;
2865 Expr : constant Node_Id :=
2866 New_Copy_Tree (Expression (Assoc));
2868 begin
2869 Expander_Mode_Save_And_Set (False);
2870 Full_Analysis := False;
2871 Analyze (Expr);
2872 Full_Analysis := Save_Analysis;
2873 Expander_Mode_Restore;
2875 if Is_Tagged_Type (Etype (Expr)) then
2876 Check_Dynamically_Tagged_Expression
2877 (Expr => Expr,
2878 Typ => Component_Type (Etype (N)),
2879 Related_Nod => N);
2880 end if;
2881 end;
2882 end if;
2883 end if;
2885 -- STEP 3 (B): Compute the aggregate bounds
2887 if Others_Present then
2888 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2890 else
2891 if Others_Allowed then
2892 Get_Index_Bounds (Index_Constr, Aggr_Low, Discard);
2893 else
2894 Aggr_Low := Index_Typ_Low;
2895 end if;
2897 Aggr_High := Add (Nb_Elements - 1, To => Aggr_Low);
2898 Check_Bound (Index_Base_High, Aggr_High);
2899 end if;
2900 end if;
2902 -- STEP 4: Perform static aggregate checks and save the bounds
2904 -- Check (A)
2906 Check_Bounds (Index_Typ_Low, Index_Typ_High, Aggr_Low, Aggr_High);
2907 Check_Bounds (Index_Base_Low, Index_Base_High, Aggr_Low, Aggr_High);
2909 -- Check (B)
2911 if Others_Present and then Nb_Discrete_Choices > 0 then
2912 Check_Bounds (Aggr_Low, Aggr_High, Choices_Low, Choices_High);
2913 Check_Bounds (Index_Typ_Low, Index_Typ_High,
2914 Choices_Low, Choices_High);
2915 Check_Bounds (Index_Base_Low, Index_Base_High,
2916 Choices_Low, Choices_High);
2918 -- Check (C)
2920 elsif Others_Present and then Nb_Elements > 0 then
2921 Check_Length (Aggr_Low, Aggr_High, Nb_Elements);
2922 Check_Length (Index_Typ_Low, Index_Typ_High, Nb_Elements);
2923 Check_Length (Index_Base_Low, Index_Base_High, Nb_Elements);
2924 end if;
2926 if Raises_Constraint_Error (Aggr_Low)
2927 or else Raises_Constraint_Error (Aggr_High)
2928 then
2929 Set_Raises_Constraint_Error (N);
2930 end if;
2932 Aggr_Low := Duplicate_Subexpr (Aggr_Low);
2934 -- Do not duplicate Aggr_High if Aggr_High = Aggr_Low + Nb_Elements
2935 -- since the addition node returned by Add is not yet analyzed. Attach
2936 -- to tree and analyze first. Reset analyzed flag to ensure it will get
2937 -- analyzed when it is a literal bound whose type must be properly set.
2939 if Others_Present or else Nb_Discrete_Choices > 0 then
2940 Aggr_High := Duplicate_Subexpr (Aggr_High);
2942 if Etype (Aggr_High) = Universal_Integer then
2943 Set_Analyzed (Aggr_High, False);
2944 end if;
2945 end if;
2947 -- If the aggregate already has bounds attached to it, it means this is
2948 -- a positional aggregate created as an optimization by
2949 -- Exp_Aggr.Convert_To_Positional, so we don't want to change those
2950 -- bounds.
2952 if Present (Aggregate_Bounds (N))
2953 and then not Others_Allowed
2954 and then not Comes_From_Source (N)
2955 then
2956 Aggr_Low := Low_Bound (Aggregate_Bounds (N));
2957 Aggr_High := High_Bound (Aggregate_Bounds (N));
2958 end if;
2960 Set_Aggregate_Bounds
2961 (N, Make_Range (Loc, Low_Bound => Aggr_Low, High_Bound => Aggr_High));
2963 -- The bounds may contain expressions that must be inserted upwards.
2964 -- Attach them fully to the tree. After analysis, remove side effects
2965 -- from upper bound, if still needed.
2967 Set_Parent (Aggregate_Bounds (N), N);
2968 Analyze_And_Resolve (Aggregate_Bounds (N), Index_Typ);
2969 Check_Unset_Reference (Aggregate_Bounds (N));
2971 if not Others_Present and then Nb_Discrete_Choices = 0 then
2972 Set_High_Bound
2973 (Aggregate_Bounds (N),
2974 Duplicate_Subexpr (High_Bound (Aggregate_Bounds (N))));
2975 end if;
2977 -- Check the dimensions of each component in the array aggregate
2979 Analyze_Dimension_Array_Aggregate (N, Component_Typ);
2981 return Success;
2982 end Resolve_Array_Aggregate;
2984 ---------------------------------
2985 -- Resolve_Container_Aggregate --
2986 ---------------------------------
2988 procedure Resolve_Container_Aggregate (N : Node_Id; Typ : Entity_Id) is
2989 procedure Resolve_Iterated_Association
2990 (Comp : Node_Id;
2991 Key_Type : Entity_Id;
2992 Elmt_Type : Entity_Id);
2993 -- Resolve choices and expression in an iterated component association
2994 -- or an iterated element association, which has a key_expression.
2995 -- This is similar but not identical to the handling of this construct
2996 -- in an array aggregate.
2997 -- For a named container, the type of each choice must be compatible
2998 -- with the key type. For a positional container, the choice must be
2999 -- a subtype indication or an iterator specification that determines
3000 -- an element type.
3002 Asp : constant Node_Id := Find_Value_Of_Aspect (Typ, Aspect_Aggregate);
3004 Empty_Subp : Node_Id := Empty;
3005 Add_Named_Subp : Node_Id := Empty;
3006 Add_Unnamed_Subp : Node_Id := Empty;
3007 New_Indexed_Subp : Node_Id := Empty;
3008 Assign_Indexed_Subp : Node_Id := Empty;
3010 ----------------------------------
3011 -- Resolve_Iterated_Association --
3012 ----------------------------------
3014 procedure Resolve_Iterated_Association
3015 (Comp : Node_Id;
3016 Key_Type : Entity_Id;
3017 Elmt_Type : Entity_Id)
3019 Loc : constant Source_Ptr := Sloc (N);
3020 Choice : Node_Id;
3021 Copy : Node_Id;
3022 Ent : Entity_Id;
3023 Expr : Node_Id;
3024 Key_Expr : Node_Id;
3025 Id : Entity_Id;
3026 Id_Name : Name_Id;
3027 Typ : Entity_Id := Empty;
3029 begin
3030 Error_Msg_Ada_2022_Feature ("iterated component", Loc);
3032 -- If this is an Iterated_Element_Association then either a
3033 -- an Iterator_Specification or a Loop_Parameter specification
3034 -- is present. In both cases a Key_Expression is present.
3036 if Nkind (Comp) = N_Iterated_Element_Association then
3038 -- Create a temporary scope to avoid some modifications from
3039 -- escaping the Analyze call below. The original Tree will be
3040 -- reanalyzed later.
3042 Ent := New_Internal_Entity
3043 (E_Loop, Current_Scope, Sloc (Comp), 'L');
3044 Set_Etype (Ent, Standard_Void_Type);
3045 Set_Parent (Ent, Parent (Comp));
3046 Push_Scope (Ent);
3048 if Present (Loop_Parameter_Specification (Comp)) then
3049 Copy := Copy_Separate_Tree (Comp);
3051 Analyze
3052 (Loop_Parameter_Specification (Copy));
3054 Id_Name := Chars (Defining_Identifier
3055 (Loop_Parameter_Specification (Comp)));
3056 else
3057 Copy := Copy_Separate_Tree (Iterator_Specification (Comp));
3058 Analyze (Copy);
3060 Id_Name := Chars (Defining_Identifier
3061 (Iterator_Specification (Comp)));
3062 end if;
3064 -- Key expression must have the type of the key. We analyze
3065 -- a copy of the original expression, because it will be
3066 -- reanalyzed and copied as needed during expansion of the
3067 -- corresponding loop.
3069 Key_Expr := Key_Expression (Comp);
3070 Analyze_And_Resolve (New_Copy_Tree (Key_Expr), Key_Type);
3071 End_Scope;
3073 elsif Present (Iterator_Specification (Comp)) then
3074 Copy := Copy_Separate_Tree (Iterator_Specification (Comp));
3075 Id_Name :=
3076 Chars (Defining_Identifier (Iterator_Specification (Comp)));
3078 Analyze (Copy);
3079 Typ := Etype (Defining_Identifier (Copy));
3081 else
3082 Choice := First (Discrete_Choices (Comp));
3084 while Present (Choice) loop
3085 Analyze (Choice);
3087 -- Choice can be a subtype name, a range, or an expression
3089 if Is_Entity_Name (Choice)
3090 and then Is_Type (Entity (Choice))
3091 and then Base_Type (Entity (Choice)) = Base_Type (Key_Type)
3092 then
3093 null;
3095 elsif Present (Key_Type) then
3096 Analyze_And_Resolve (Choice, Key_Type);
3098 else
3099 Typ := Etype (Choice); -- assume unique for now
3100 end if;
3102 Next (Choice);
3103 end loop;
3105 Id_Name := Chars (Defining_Identifier (Comp));
3106 end if;
3108 -- Create a scope in which to introduce an index, which is usually
3109 -- visible in the expression for the component, and needed for its
3110 -- analysis.
3112 Id := Make_Defining_Identifier (Sloc (Comp), Id_Name);
3113 Ent := New_Internal_Entity (E_Loop,
3114 Current_Scope, Sloc (Comp), 'L');
3115 Set_Etype (Ent, Standard_Void_Type);
3116 Set_Parent (Ent, Parent (Comp));
3117 Push_Scope (Ent);
3119 -- Insert and decorate the loop variable in the current scope.
3120 -- The expression has to be analyzed once the loop variable is
3121 -- directly visible. Mark the variable as referenced to prevent
3122 -- spurious warnings, given that subsequent uses of its name in the
3123 -- expression will reference the internal (synonym) loop variable.
3125 Enter_Name (Id);
3127 if No (Key_Type) then
3128 pragma Assert (Present (Typ));
3129 Set_Etype (Id, Typ);
3130 else
3131 Set_Etype (Id, Key_Type);
3132 end if;
3134 Mutate_Ekind (Id, E_Variable);
3135 Set_Scope (Id, Ent);
3136 Set_Referenced (Id);
3138 -- Analyze a copy of the expression, to verify legality. We use
3139 -- a copy because the expression will be analyzed anew when the
3140 -- enclosing aggregate is expanded, and the construct is rewritten
3141 -- as a loop with a new index variable.
3143 Expr := New_Copy_Tree (Expression (Comp));
3144 Preanalyze_And_Resolve (Expr, Elmt_Type);
3145 End_Scope;
3147 end Resolve_Iterated_Association;
3149 -- Start of processing for Resolve_Container_Aggregate
3151 begin
3152 pragma Assert (Nkind (Asp) = N_Aggregate);
3154 Set_Etype (N, Typ);
3155 Parse_Aspect_Aggregate (Asp,
3156 Empty_Subp, Add_Named_Subp, Add_Unnamed_Subp,
3157 New_Indexed_Subp, Assign_Indexed_Subp);
3159 if Present (Add_Unnamed_Subp)
3160 and then No (New_Indexed_Subp)
3161 then
3162 declare
3163 Elmt_Type : constant Entity_Id :=
3164 Etype (Next_Formal
3165 (First_Formal (Entity (Add_Unnamed_Subp))));
3166 Comp : Node_Id;
3168 begin
3169 if Present (Expressions (N)) then
3170 -- positional aggregate
3172 Comp := First (Expressions (N));
3173 while Present (Comp) loop
3174 Analyze_And_Resolve (Comp, Elmt_Type);
3175 Next (Comp);
3176 end loop;
3177 end if;
3179 -- Empty aggregate, to be replaced by Empty during
3180 -- expansion, or iterated component association.
3182 if Present (Component_Associations (N)) then
3183 declare
3184 Comp : Node_Id := First (Component_Associations (N));
3185 begin
3186 while Present (Comp) loop
3187 if Nkind (Comp) /=
3188 N_Iterated_Component_Association
3189 then
3190 Error_Msg_N ("illegal component association "
3191 & "for unnamed container aggregate", Comp);
3192 return;
3193 else
3194 Resolve_Iterated_Association
3195 (Comp, Empty, Elmt_Type);
3196 end if;
3198 Next (Comp);
3199 end loop;
3200 end;
3201 end if;
3202 end;
3204 elsif Present (Add_Named_Subp) then
3205 declare
3206 -- Retrieves types of container, key, and element from the
3207 -- specified insertion procedure.
3209 Container : constant Entity_Id :=
3210 First_Formal (Entity (Add_Named_Subp));
3211 Key_Type : constant Entity_Id := Etype (Next_Formal (Container));
3212 Elmt_Type : constant Entity_Id :=
3213 Etype (Next_Formal (Next_Formal (Container)));
3214 Comp : Node_Id;
3215 Choice : Node_Id;
3217 begin
3218 Comp := First (Component_Associations (N));
3219 while Present (Comp) loop
3220 if Nkind (Comp) = N_Component_Association then
3221 Choice := First (Choices (Comp));
3223 while Present (Choice) loop
3224 Analyze_And_Resolve (Choice, Key_Type);
3225 if not Is_Static_Expression (Choice) then
3226 Error_Msg_N ("choice must be static", Choice);
3227 end if;
3229 Next (Choice);
3230 end loop;
3232 Analyze_And_Resolve (Expression (Comp), Elmt_Type);
3234 elsif Nkind (Comp) in
3235 N_Iterated_Component_Association |
3236 N_Iterated_Element_Association
3237 then
3238 Resolve_Iterated_Association
3239 (Comp, Key_Type, Elmt_Type);
3240 end if;
3242 Next (Comp);
3243 end loop;
3244 end;
3246 elsif Present (Assign_Indexed_Subp) then
3247 -- Indexed Aggregate. Positional or indexed component
3248 -- can be present, but not both. Choices must be static
3249 -- values or ranges with static bounds.
3251 declare
3252 Container : constant Entity_Id :=
3253 First_Formal (Entity (Assign_Indexed_Subp));
3254 Index_Type : constant Entity_Id := Etype (Next_Formal (Container));
3255 Comp_Type : constant Entity_Id :=
3256 Etype (Next_Formal (Next_Formal (Container)));
3257 Comp : Node_Id;
3258 Choice : Node_Id;
3259 Num_Choices : Nat := 0;
3261 Hi_Val : Uint;
3262 Lo_Val : Uint;
3263 begin
3264 if Present (Expressions (N)) then
3265 Comp := First (Expressions (N));
3266 while Present (Comp) loop
3267 Analyze_And_Resolve (Comp, Comp_Type);
3268 Next (Comp);
3269 end loop;
3270 end if;
3272 if Present (Component_Associations (N))
3273 and then not Is_Empty_List (Component_Associations (N))
3274 then
3275 if Present (Expressions (N))
3276 and then not Is_Empty_List (Expressions (N))
3277 then
3278 Error_Msg_N ("container aggregate cannot be "
3279 & "both positional and named", N);
3280 return;
3281 end if;
3283 Comp := First (Component_Associations (N));
3285 while Present (Comp) loop
3286 if Nkind (Comp) = N_Component_Association then
3287 Choice := First (Choices (Comp));
3289 while Present (Choice) loop
3290 Analyze_And_Resolve (Choice, Index_Type);
3291 Num_Choices := Num_Choices + 1;
3292 Next (Choice);
3293 end loop;
3295 Analyze_And_Resolve (Expression (Comp), Comp_Type);
3297 elsif Nkind (Comp) in
3298 N_Iterated_Component_Association |
3299 N_Iterated_Element_Association
3300 then
3301 Resolve_Iterated_Association
3302 (Comp, Index_Type, Comp_Type);
3303 Num_Choices := Num_Choices + 1;
3304 end if;
3306 Next (Comp);
3307 end loop;
3309 -- The component associations in an indexed aggregate
3310 -- must denote a contiguous set of static values. We
3311 -- build a table of values/ranges and sort it, as is done
3312 -- elsewhere for case statements and array aggregates.
3313 -- If the aggregate has a single iterated association it
3314 -- is allowed to be nonstatic and there is nothing to check.
3316 if Num_Choices > 1 then
3317 declare
3318 Table : Case_Table_Type (1 .. Num_Choices);
3319 No_Choice : Pos := 1;
3320 Lo, Hi : Node_Id;
3322 -- Traverse aggregate to determine size of needed table.
3323 -- Verify that bounds are static and that loops have no
3324 -- filters or key expressions.
3326 begin
3327 Comp := First (Component_Associations (N));
3328 while Present (Comp) loop
3329 if Nkind (Comp) = N_Iterated_Element_Association then
3330 if Present
3331 (Loop_Parameter_Specification (Comp))
3332 then
3333 if Present (Iterator_Filter
3334 (Loop_Parameter_Specification (Comp)))
3335 then
3336 Error_Msg_N
3337 ("iterator filter not allowed " &
3338 "in indexed aggregate", Comp);
3339 return;
3341 elsif Present (Key_Expression
3342 (Loop_Parameter_Specification (Comp)))
3343 then
3344 Error_Msg_N
3345 ("key expression not allowed " &
3346 "in indexed aggregate", Comp);
3347 return;
3348 end if;
3349 end if;
3350 else
3351 Choice := First (Choices (Comp));
3353 while Present (Choice) loop
3354 Get_Index_Bounds (Choice, Lo, Hi);
3355 Table (No_Choice).Choice := Choice;
3356 Table (No_Choice).Lo := Lo;
3357 Table (No_Choice).Hi := Hi;
3359 -- Verify staticness of value or range
3361 if not Is_Static_Expression (Lo)
3362 or else not Is_Static_Expression (Hi)
3363 then
3364 Error_Msg_N
3365 ("nonstatic expression for index " &
3366 "for indexed aggregate", Choice);
3367 return;
3368 end if;
3370 No_Choice := No_Choice + 1;
3371 Next (Choice);
3372 end loop;
3373 end if;
3375 Next (Comp);
3376 end loop;
3378 Sort_Case_Table (Table);
3380 for J in 1 .. Num_Choices - 1 loop
3381 Hi_Val := Expr_Value (Table (J).Hi);
3382 Lo_Val := Expr_Value (Table (J + 1).Lo);
3384 if Lo_Val = Hi_Val then
3385 Error_Msg_N
3386 ("duplicate index in indexed aggregate",
3387 Table (J + 1).Choice);
3388 exit;
3390 elsif Lo_Val < Hi_Val then
3391 Error_Msg_N
3392 ("overlapping indices in indexed aggregate",
3393 Table (J + 1).Choice);
3394 exit;
3396 elsif Lo_Val > Hi_Val + 1 then
3397 Error_Msg_N
3398 ("missing index values", Table (J + 1).Choice);
3399 exit;
3400 end if;
3401 end loop;
3402 end;
3403 end if;
3404 end if;
3405 end;
3406 end if;
3407 end Resolve_Container_Aggregate;
3409 -----------------------------
3410 -- Resolve_Delta_Aggregate --
3411 -----------------------------
3413 procedure Resolve_Delta_Aggregate (N : Node_Id; Typ : Entity_Id) is
3414 Base : constant Node_Id := Expression (N);
3416 begin
3417 Error_Msg_Ada_2022_Feature ("delta aggregate", Sloc (N));
3419 if not Is_Composite_Type (Typ) then
3420 Error_Msg_N ("not a composite type", N);
3421 end if;
3423 Analyze_And_Resolve (Base, Typ);
3425 if Is_Array_Type (Typ) then
3426 -- For an array_delta_aggregate, the base_expression and each
3427 -- expression in every array_component_association shall be of a
3428 -- nonlimited type; RM 4.3.4(13/5). However, to prevent repeated
3429 -- errors we only check the base expression and not array component
3430 -- associations.
3432 if Is_Limited_Type (Etype (Base)) then
3433 Error_Msg_N
3434 ("array delta aggregate shall be of a nonlimited type", Base);
3435 Explain_Limited_Type (Etype (Base), Base);
3436 end if;
3438 Resolve_Delta_Array_Aggregate (N, Typ);
3439 else
3441 -- Delta aggregates for record types must use parentheses,
3442 -- not square brackets.
3444 if Is_Homogeneous_Aggregate (N) then
3445 Error_Msg_N
3446 ("delta aggregates for record types must use (), not '[']", N);
3447 end if;
3449 -- The base_expression of a record_delta_aggregate can be of a
3450 -- limited type only if it is newly constructed; RM 7.5(2.1/5).
3452 Check_Expr_OK_In_Limited_Aggregate (Base);
3454 Resolve_Delta_Record_Aggregate (N, Typ);
3455 end if;
3457 Set_Etype (N, Typ);
3458 end Resolve_Delta_Aggregate;
3460 -----------------------------------
3461 -- Resolve_Delta_Array_Aggregate --
3462 -----------------------------------
3464 procedure Resolve_Delta_Array_Aggregate (N : Node_Id; Typ : Entity_Id) is
3465 Deltas : constant List_Id := Component_Associations (N);
3466 Index_Type : constant Entity_Id := Etype (First_Index (Typ));
3468 Assoc : Node_Id;
3469 Choice : Node_Id;
3470 Expr : Node_Id;
3472 begin
3473 Assoc := First (Deltas);
3474 while Present (Assoc) loop
3475 if Nkind (Assoc) = N_Iterated_Component_Association then
3476 Choice := First (Choice_List (Assoc));
3477 while Present (Choice) loop
3478 if Nkind (Choice) = N_Others_Choice then
3479 Error_Msg_N
3480 ("OTHERS not allowed in delta aggregate", Choice);
3482 elsif Nkind (Choice) = N_Subtype_Indication then
3483 Resolve_Discrete_Subtype_Indication
3484 (Choice, Base_Type (Index_Type));
3486 else
3487 Analyze_And_Resolve (Choice, Index_Type);
3488 end if;
3490 Next (Choice);
3491 end loop;
3493 declare
3494 Id : constant Entity_Id := Defining_Identifier (Assoc);
3495 Ent : constant Entity_Id :=
3496 New_Internal_Entity
3497 (E_Loop, Current_Scope, Sloc (Assoc), 'L');
3499 begin
3500 Set_Etype (Ent, Standard_Void_Type);
3501 Set_Parent (Ent, Assoc);
3502 Push_Scope (Ent);
3504 if No (Scope (Id)) then
3505 Set_Etype (Id, Index_Type);
3506 Mutate_Ekind (Id, E_Variable);
3507 Set_Scope (Id, Ent);
3508 end if;
3509 Enter_Name (Id);
3511 -- Resolve a copy of the expression, after setting
3512 -- its parent properly to preserve its context.
3514 Expr := New_Copy_Tree (Expression (Assoc));
3515 Set_Parent (Expr, Assoc);
3516 Analyze_And_Resolve (Expr, Component_Type (Typ));
3517 End_Scope;
3518 end;
3520 else
3521 Choice := First (Choice_List (Assoc));
3522 while Present (Choice) loop
3523 Analyze (Choice);
3525 if Nkind (Choice) = N_Others_Choice then
3526 Error_Msg_N
3527 ("OTHERS not allowed in delta aggregate", Choice);
3529 elsif Is_Entity_Name (Choice)
3530 and then Is_Type (Entity (Choice))
3531 then
3532 -- Choice covers a range of values
3534 if Base_Type (Entity (Choice)) /=
3535 Base_Type (Index_Type)
3536 then
3537 Error_Msg_NE
3538 ("choice does not match index type of &",
3539 Choice, Typ);
3540 end if;
3542 elsif Nkind (Choice) = N_Subtype_Indication then
3543 Resolve_Discrete_Subtype_Indication
3544 (Choice, Base_Type (Index_Type));
3546 else
3547 Resolve (Choice, Index_Type);
3548 end if;
3550 Next (Choice);
3551 end loop;
3553 -- For an array_delta_aggregate, the array_component_association
3554 -- shall not use the box symbol <>; RM 4.3.4(11/5).
3556 pragma Assert
3557 (Box_Present (Assoc) xor Present (Expression (Assoc)));
3559 if Box_Present (Assoc) then
3560 Error_Msg_N
3561 ("'<'> in array delta aggregate is not allowed", Assoc);
3562 else
3563 Analyze_And_Resolve (Expression (Assoc), Component_Type (Typ));
3564 end if;
3565 end if;
3567 Next (Assoc);
3568 end loop;
3569 end Resolve_Delta_Array_Aggregate;
3571 ------------------------------------
3572 -- Resolve_Delta_Record_Aggregate --
3573 ------------------------------------
3575 procedure Resolve_Delta_Record_Aggregate (N : Node_Id; Typ : Entity_Id) is
3577 -- Variables used to verify that discriminant-dependent components
3578 -- appear in the same variant.
3580 Comp_Ref : Entity_Id := Empty; -- init to avoid warning
3581 Variant : Node_Id;
3583 procedure Check_Variant (Id : Entity_Id);
3584 -- If a given component of the delta aggregate appears in a variant
3585 -- part, verify that it is within the same variant as that of previous
3586 -- specified variant components of the delta.
3588 function Get_Component (Nam : Node_Id) return Entity_Id;
3589 -- Locate component with a given name and return it. If none found then
3590 -- report error and return Empty.
3592 function Nested_In (V1 : Node_Id; V2 : Node_Id) return Boolean;
3593 -- Determine whether variant V1 is within variant V2
3595 function Variant_Depth (N : Node_Id) return Natural;
3596 -- Determine the distance of a variant to the enclosing type declaration
3598 --------------------
3599 -- Check_Variant --
3600 --------------------
3602 procedure Check_Variant (Id : Entity_Id) is
3603 Comp : Entity_Id;
3604 Comp_Variant : Node_Id;
3606 begin
3607 if not Has_Discriminants (Typ) then
3608 return;
3609 end if;
3611 Comp := First_Entity (Typ);
3612 while Present (Comp) loop
3613 exit when Chars (Comp) = Chars (Id);
3614 Next_Component (Comp);
3615 end loop;
3617 -- Find the variant, if any, whose component list includes the
3618 -- component declaration.
3620 Comp_Variant := Parent (Parent (List_Containing (Parent (Comp))));
3621 if Nkind (Comp_Variant) = N_Variant then
3622 if No (Variant) then
3623 Variant := Comp_Variant;
3624 Comp_Ref := Comp;
3626 elsif Variant /= Comp_Variant then
3627 declare
3628 D1 : constant Integer := Variant_Depth (Variant);
3629 D2 : constant Integer := Variant_Depth (Comp_Variant);
3631 begin
3632 if D1 = D2
3633 or else
3634 (D1 > D2 and then not Nested_In (Variant, Comp_Variant))
3635 or else
3636 (D2 > D1 and then not Nested_In (Comp_Variant, Variant))
3637 then
3638 pragma Assert (Present (Comp_Ref));
3639 Error_Msg_Node_2 := Comp_Ref;
3640 Error_Msg_NE
3641 ("& and & appear in different variants", Id, Comp);
3643 -- Otherwise retain the deeper variant for subsequent tests
3645 elsif D2 > D1 then
3646 Variant := Comp_Variant;
3647 end if;
3648 end;
3649 end if;
3650 end if;
3651 end Check_Variant;
3653 -------------------
3654 -- Get_Component --
3655 -------------------
3657 function Get_Component (Nam : Node_Id) return Entity_Id is
3658 Comp : Entity_Id;
3660 begin
3661 Comp := First_Entity (Typ);
3662 while Present (Comp) loop
3663 if Chars (Comp) = Chars (Nam) then
3664 if Ekind (Comp) = E_Discriminant then
3665 Error_Msg_N ("delta cannot apply to discriminant", Nam);
3666 end if;
3668 return Comp;
3669 end if;
3671 Next_Entity (Comp);
3672 end loop;
3674 Error_Msg_NE ("type& has no component with this name", Nam, Typ);
3675 return Empty;
3676 end Get_Component;
3678 ---------------
3679 -- Nested_In --
3680 ---------------
3682 function Nested_In (V1, V2 : Node_Id) return Boolean is
3683 Par : Node_Id;
3685 begin
3686 Par := Parent (V1);
3687 while Nkind (Par) /= N_Full_Type_Declaration loop
3688 if Par = V2 then
3689 return True;
3690 end if;
3692 Par := Parent (Par);
3693 end loop;
3695 return False;
3696 end Nested_In;
3698 -------------------
3699 -- Variant_Depth --
3700 -------------------
3702 function Variant_Depth (N : Node_Id) return Natural is
3703 Depth : Natural;
3704 Par : Node_Id;
3706 begin
3707 Depth := 0;
3708 Par := Parent (N);
3709 while Nkind (Par) /= N_Full_Type_Declaration loop
3710 Depth := Depth + 1;
3711 Par := Parent (Par);
3712 end loop;
3714 return Depth;
3715 end Variant_Depth;
3717 -- Local variables
3719 Deltas : constant List_Id := Component_Associations (N);
3721 Assoc : Node_Id;
3722 Choice : Node_Id;
3723 Comp : Entity_Id;
3724 Comp_Type : Entity_Id := Empty; -- init to avoid warning
3726 -- Start of processing for Resolve_Delta_Record_Aggregate
3728 begin
3729 Variant := Empty;
3731 Assoc := First (Deltas);
3732 while Present (Assoc) loop
3733 Choice := First (Choice_List (Assoc));
3734 while Present (Choice) loop
3735 Comp := Get_Component (Choice);
3737 if Present (Comp) then
3738 Check_Variant (Choice);
3740 Comp_Type := Etype (Comp);
3742 -- Decorate the component reference by setting its entity and
3743 -- type, as otherwise backends like GNATprove would have to
3744 -- rediscover this information by themselves.
3746 Set_Entity (Choice, Comp);
3747 Set_Etype (Choice, Comp_Type);
3748 else
3749 Comp_Type := Any_Type;
3750 end if;
3752 Next (Choice);
3753 end loop;
3755 pragma Assert (Present (Comp_Type));
3757 -- A record_component_association in record_delta_aggregate shall not
3758 -- use the box compound delimiter <> rather than an expression; see
3759 -- RM 4.3.1(17.3/5).
3761 pragma Assert (Present (Expression (Assoc)) xor Box_Present (Assoc));
3763 if Box_Present (Assoc) then
3764 Error_Msg_N
3765 ("'<'> in record delta aggregate is not allowed", Assoc);
3766 else
3767 Analyze_And_Resolve (Expression (Assoc), Comp_Type);
3769 -- The expression must not be of a limited type; RM 4.3.1(17.4/5)
3771 if Is_Limited_Type (Etype (Expression (Assoc))) then
3772 Error_Msg_N
3773 ("expression of a limited type in record delta aggregate " &
3774 "is not allowed",
3775 Expression (Assoc));
3776 end if;
3777 end if;
3779 Next (Assoc);
3780 end loop;
3781 end Resolve_Delta_Record_Aggregate;
3783 ---------------------------------
3784 -- Resolve_Extension_Aggregate --
3785 ---------------------------------
3787 -- There are two cases to consider:
3789 -- a) If the ancestor part is a type mark, the components needed are the
3790 -- difference between the components of the expected type and the
3791 -- components of the given type mark.
3793 -- b) If the ancestor part is an expression, it must be unambiguous, and
3794 -- once we have its type we can also compute the needed components as in
3795 -- the previous case. In both cases, if the ancestor type is not the
3796 -- immediate ancestor, we have to build this ancestor recursively.
3798 -- In both cases, discriminants of the ancestor type do not play a role in
3799 -- the resolution of the needed components, because inherited discriminants
3800 -- cannot be used in a type extension. As a result we can compute
3801 -- independently the list of components of the ancestor type and of the
3802 -- expected type.
3804 procedure Resolve_Extension_Aggregate (N : Node_Id; Typ : Entity_Id) is
3805 A : constant Node_Id := Ancestor_Part (N);
3806 A_Type : Entity_Id;
3807 I : Interp_Index;
3808 It : Interp;
3810 function Valid_Limited_Ancestor (Anc : Node_Id) return Boolean;
3811 -- If the type is limited, verify that the ancestor part is a legal
3812 -- expression (aggregate or function call, including 'Input)) that does
3813 -- not require a copy, as specified in 7.5(2).
3815 function Valid_Ancestor_Type return Boolean;
3816 -- Verify that the type of the ancestor part is a non-private ancestor
3817 -- of the expected type, which must be a type extension.
3819 procedure Transform_BIP_Assignment (Typ : Entity_Id);
3820 -- For an extension aggregate whose ancestor part is a build-in-place
3821 -- call returning a nonlimited type, this is used to transform the
3822 -- assignment to the ancestor part to use a temp.
3824 ----------------------------
3825 -- Valid_Limited_Ancestor --
3826 ----------------------------
3828 function Valid_Limited_Ancestor (Anc : Node_Id) return Boolean is
3829 begin
3830 if Is_Entity_Name (Anc) and then Is_Type (Entity (Anc)) then
3831 return True;
3833 -- The ancestor must be a call or an aggregate, but a call may
3834 -- have been expanded into a temporary, so check original node.
3836 elsif Nkind (Anc) in N_Aggregate
3837 | N_Extension_Aggregate
3838 | N_Function_Call
3839 then
3840 return True;
3842 elsif Nkind (Original_Node (Anc)) = N_Function_Call then
3843 return True;
3845 elsif Nkind (Anc) = N_Attribute_Reference
3846 and then Attribute_Name (Anc) = Name_Input
3847 then
3848 return True;
3850 elsif Nkind (Anc) = N_Qualified_Expression then
3851 return Valid_Limited_Ancestor (Expression (Anc));
3853 elsif Nkind (Anc) = N_Raise_Expression then
3854 return True;
3856 else
3857 return False;
3858 end if;
3859 end Valid_Limited_Ancestor;
3861 -------------------------
3862 -- Valid_Ancestor_Type --
3863 -------------------------
3865 function Valid_Ancestor_Type return Boolean is
3866 Imm_Type : Entity_Id;
3868 begin
3869 Imm_Type := Base_Type (Typ);
3870 while Is_Derived_Type (Imm_Type) loop
3871 if Etype (Imm_Type) = Base_Type (A_Type) then
3872 return True;
3874 -- The base type of the parent type may appear as a private
3875 -- extension if it is declared as such in a parent unit of the
3876 -- current one. For consistency of the subsequent analysis use
3877 -- the partial view for the ancestor part.
3879 elsif Is_Private_Type (Etype (Imm_Type))
3880 and then Present (Full_View (Etype (Imm_Type)))
3881 and then Base_Type (A_Type) = Full_View (Etype (Imm_Type))
3882 then
3883 A_Type := Etype (Imm_Type);
3884 return True;
3886 -- The parent type may be a private extension. The aggregate is
3887 -- legal if the type of the aggregate is an extension of it that
3888 -- is not a private extension.
3890 elsif Is_Private_Type (A_Type)
3891 and then not Is_Private_Type (Imm_Type)
3892 and then Present (Full_View (A_Type))
3893 and then Base_Type (Full_View (A_Type)) = Etype (Imm_Type)
3894 then
3895 return True;
3897 -- The parent type may be a raise expression (which is legal in
3898 -- any expression context).
3900 elsif A_Type = Raise_Type then
3901 A_Type := Etype (Imm_Type);
3902 return True;
3904 else
3905 Imm_Type := Etype (Base_Type (Imm_Type));
3906 end if;
3907 end loop;
3909 -- If previous loop did not find a proper ancestor, report error
3911 Error_Msg_NE ("expect ancestor type of &", A, Typ);
3912 return False;
3913 end Valid_Ancestor_Type;
3915 ------------------------------
3916 -- Transform_BIP_Assignment --
3917 ------------------------------
3919 procedure Transform_BIP_Assignment (Typ : Entity_Id) is
3920 Loc : constant Source_Ptr := Sloc (N);
3921 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'Y', A);
3922 Obj_Decl : constant Node_Id :=
3923 Make_Object_Declaration (Loc,
3924 Defining_Identifier => Def_Id,
3925 Constant_Present => True,
3926 Object_Definition => New_Occurrence_Of (Typ, Loc),
3927 Expression => A,
3928 Has_Init_Expression => True);
3929 begin
3930 Set_Etype (Def_Id, Typ);
3931 Set_Ancestor_Part (N, New_Occurrence_Of (Def_Id, Loc));
3932 Insert_Action (N, Obj_Decl);
3933 end Transform_BIP_Assignment;
3935 -- Start of processing for Resolve_Extension_Aggregate
3937 begin
3938 -- Analyze the ancestor part and account for the case where it is a
3939 -- parameterless function call.
3941 Analyze (A);
3942 Check_Parameterless_Call (A);
3944 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
3946 -- AI05-0115: If the ancestor part is a subtype mark, the ancestor
3947 -- must not have unknown discriminants. To catch cases where the
3948 -- aggregate occurs at a place where the full view of the ancestor
3949 -- type is visible and doesn't have unknown discriminants, but the
3950 -- aggregate type was derived from a partial view that has unknown
3951 -- discriminants, we check whether the aggregate type has unknown
3952 -- discriminants (unknown discriminants were inherited), along
3953 -- with checking that the partial view of the ancestor has unknown
3954 -- discriminants. (It might be sufficient to replace the entire
3955 -- condition with Has_Unknown_Discriminants (Typ), but that might
3956 -- miss some cases, not clear, and causes error changes in some tests
3957 -- such as class-wide cases, that aren't clearly improvements. ???)
3959 if Has_Unknown_Discriminants (Entity (A))
3960 or else (Has_Unknown_Discriminants (Typ)
3961 and then Partial_View_Has_Unknown_Discr (Entity (A)))
3962 then
3963 Error_Msg_NE
3964 ("aggregate not available for type& whose ancestor "
3965 & "has unknown discriminants", N, Typ);
3966 end if;
3967 end if;
3969 if not Is_Tagged_Type (Typ) then
3970 Error_Msg_N ("type of extension aggregate must be tagged", N);
3971 return;
3973 elsif Is_Limited_Type (Typ) then
3975 -- Ada 2005 (AI-287): Limited aggregates are allowed
3977 if Ada_Version < Ada_2005 then
3978 Error_Msg_N ("aggregate type cannot be limited", N);
3979 Explain_Limited_Type (Typ, N);
3980 return;
3982 elsif Valid_Limited_Ancestor (A) then
3983 null;
3985 else
3986 Error_Msg_N
3987 ("limited ancestor part must be aggregate or function call", A);
3988 end if;
3990 elsif Is_Class_Wide_Type (Typ) then
3991 Error_Msg_N ("aggregate cannot be of a class-wide type", N);
3992 return;
3993 end if;
3995 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
3996 A_Type := Get_Full_View (Entity (A));
3998 if Valid_Ancestor_Type then
3999 Set_Entity (A, A_Type);
4000 Set_Etype (A, A_Type);
4002 Validate_Ancestor_Part (N);
4003 Resolve_Record_Aggregate (N, Typ);
4004 end if;
4006 elsif Nkind (A) /= N_Aggregate then
4007 if Is_Overloaded (A) then
4008 A_Type := Any_Type;
4010 Get_First_Interp (A, I, It);
4011 while Present (It.Typ) loop
4013 -- Consider limited interpretations if Ada 2005 or higher
4015 if Is_Tagged_Type (It.Typ)
4016 and then (Ada_Version >= Ada_2005
4017 or else not Is_Limited_Type (It.Typ))
4018 then
4019 if A_Type /= Any_Type then
4020 Error_Msg_N ("cannot resolve expression", A);
4021 return;
4022 else
4023 A_Type := It.Typ;
4024 end if;
4025 end if;
4027 Get_Next_Interp (I, It);
4028 end loop;
4030 if A_Type = Any_Type then
4031 if Ada_Version >= Ada_2005 then
4032 Error_Msg_N
4033 ("ancestor part must be of a tagged type", A);
4034 else
4035 Error_Msg_N
4036 ("ancestor part must be of a nonlimited tagged type", A);
4037 end if;
4039 return;
4040 end if;
4042 else
4043 A_Type := Etype (A);
4044 end if;
4046 if Valid_Ancestor_Type then
4047 Resolve (A, A_Type);
4048 Check_Unset_Reference (A);
4049 Check_Non_Static_Context (A);
4051 -- The aggregate is illegal if the ancestor expression is a call
4052 -- to a function with a limited unconstrained result, unless the
4053 -- type of the aggregate is a null extension. This restriction
4054 -- was added in AI05-67 to simplify implementation.
4056 if Nkind (A) = N_Function_Call
4057 and then Is_Limited_Type (A_Type)
4058 and then not Is_Null_Extension (Typ)
4059 and then not Is_Constrained (A_Type)
4060 then
4061 Error_Msg_N
4062 ("type of limited ancestor part must be constrained", A);
4064 -- Reject the use of CPP constructors that leave objects partially
4065 -- initialized. For example:
4067 -- type CPP_Root is tagged limited record ...
4068 -- pragma Import (CPP, CPP_Root);
4070 -- type CPP_DT is new CPP_Root and Iface ...
4071 -- pragma Import (CPP, CPP_DT);
4073 -- type Ada_DT is new CPP_DT with ...
4075 -- Obj : Ada_DT := Ada_DT'(New_CPP_Root with others => <>);
4077 -- Using the constructor of CPP_Root the slots of the dispatch
4078 -- table of CPP_DT cannot be set, and the secondary tag of
4079 -- CPP_DT is unknown.
4081 elsif Nkind (A) = N_Function_Call
4082 and then Is_CPP_Constructor_Call (A)
4083 and then Enclosing_CPP_Parent (Typ) /= A_Type
4084 then
4085 Error_Msg_NE
4086 ("??must use 'C'P'P constructor for type &", A,
4087 Enclosing_CPP_Parent (Typ));
4089 -- The following call is not needed if the previous warning
4090 -- is promoted to an error.
4092 Resolve_Record_Aggregate (N, Typ);
4094 elsif Is_Class_Wide_Type (Etype (A))
4095 and then Nkind (Original_Node (A)) = N_Function_Call
4096 then
4097 -- If the ancestor part is a dispatching call, it appears
4098 -- statically to be a legal ancestor, but it yields any member
4099 -- of the class, and it is not possible to determine whether
4100 -- it is an ancestor of the extension aggregate (much less
4101 -- which ancestor). It is not possible to determine the
4102 -- components of the extension part.
4104 -- This check implements AI-306, which in fact was motivated by
4105 -- an AdaCore query to the ARG after this test was added.
4107 Error_Msg_N ("ancestor part must be statically tagged", A);
4108 else
4109 -- We are using the build-in-place protocol, but we can't build
4110 -- in place, because we need to call the function before
4111 -- allocating the aggregate. Could do better for null
4112 -- extensions, and maybe for nondiscriminated types.
4113 -- This is wrong for limited, but those were wrong already.
4115 if not Is_Limited_View (A_Type)
4116 and then Is_Build_In_Place_Function_Call (A)
4117 then
4118 Transform_BIP_Assignment (A_Type);
4119 end if;
4121 Resolve_Record_Aggregate (N, Typ);
4122 end if;
4123 end if;
4125 else
4126 Error_Msg_N ("no unique type for this aggregate", A);
4127 end if;
4129 Check_Function_Writable_Actuals (N);
4130 end Resolve_Extension_Aggregate;
4132 ----------------------------------
4133 -- Resolve_Null_Array_Aggregate --
4134 ----------------------------------
4136 function Resolve_Null_Array_Aggregate (N : Node_Id) return Boolean is
4137 -- Never returns False, but declared as a function to match
4138 -- other Resolve_Mumble functions.
4140 Loc : constant Source_Ptr := Sloc (N);
4141 Typ : constant Entity_Id := Etype (N);
4143 Index : Node_Id;
4144 Lo, Hi : Node_Id;
4145 Constr : constant List_Id := New_List;
4147 begin
4148 -- Attach the list of constraints at the location of the aggregate, so
4149 -- the individual constraints can be analyzed.
4151 Set_Parent (Constr, N);
4153 -- Create a constrained subtype with null dimensions
4155 Index := First_Index (Typ);
4156 while Present (Index) loop
4157 Get_Index_Bounds (Index, L => Lo, H => Hi);
4159 -- The upper bound is the predecessor of the lower bound
4161 Hi := Make_Attribute_Reference
4162 (Loc,
4163 Prefix => New_Occurrence_Of (Etype (Index), Loc),
4164 Attribute_Name => Name_Pred,
4165 Expressions => New_List (New_Copy_Tree (Lo)));
4167 Append (Make_Range (Loc, New_Copy_Tree (Lo), Hi), Constr);
4168 Analyze_And_Resolve (Last (Constr), Etype (Index));
4170 Index := Next_Index (Index);
4171 end loop;
4173 Set_Compile_Time_Known_Aggregate (N);
4174 Set_Aggregate_Bounds (N, First (Constr));
4176 return True;
4177 end Resolve_Null_Array_Aggregate;
4179 ------------------------------
4180 -- Resolve_Record_Aggregate --
4181 ------------------------------
4183 procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id) is
4184 New_Assoc_List : constant List_Id := New_List;
4185 -- New_Assoc_List is the newly built list of N_Component_Association
4186 -- nodes.
4188 Others_Etype : Entity_Id := Empty;
4189 -- This variable is used to save the Etype of the last record component
4190 -- that takes its value from the others choice. Its purpose is:
4192 -- (a) make sure the others choice is useful
4194 -- (b) make sure the type of all the components whose value is
4195 -- subsumed by the others choice are the same.
4197 -- This variable is updated as a side effect of function Get_Value.
4199 Box_Node : Node_Id := Empty;
4200 Is_Box_Present : Boolean := False;
4201 Is_Box_Init_By_Default : Boolean := False;
4202 Others_Box : Natural := 0;
4203 -- Ada 2005 (AI-287): Variables used in case of default initialization
4204 -- to provide a functionality similar to Others_Etype. Box_Present
4205 -- indicates that the component takes its default initialization;
4206 -- Others_Box counts the number of components of the current aggregate
4207 -- (which may be a sub-aggregate of a larger one) that are default-
4208 -- initialized. A value of One indicates that an others_box is present.
4209 -- Any larger value indicates that the others_box is not redundant.
4210 -- These variables, similar to Others_Etype, are also updated as a side
4211 -- effect of function Get_Value. Box_Node is used to place a warning on
4212 -- a redundant others_box.
4214 procedure Add_Association
4215 (Component : Entity_Id;
4216 Expr : Node_Id;
4217 Assoc_List : List_Id;
4218 Is_Box_Present : Boolean := False);
4219 -- Builds a new N_Component_Association node which associates Component
4220 -- to expression Expr and adds it to the association list being built,
4221 -- either New_Assoc_List, or the association being built for an inner
4222 -- aggregate.
4224 procedure Add_Discriminant_Values
4225 (New_Aggr : Node_Id;
4226 Assoc_List : List_Id);
4227 -- The constraint to a component may be given by a discriminant of the
4228 -- enclosing type, in which case we have to retrieve its value, which is
4229 -- part of the enclosing aggregate. Assoc_List provides the discriminant
4230 -- associations of the current type or of some enclosing record.
4232 function Discriminant_Present (Input_Discr : Entity_Id) return Boolean;
4233 -- If aggregate N is a regular aggregate this routine will return True.
4234 -- Otherwise, if N is an extension aggregate, then Input_Discr denotes
4235 -- a discriminant whose value may already have been specified by N's
4236 -- ancestor part. This routine checks whether this is indeed the case
4237 -- and if so returns False, signaling that no value for Input_Discr
4238 -- should appear in N's aggregate part. Also, in this case, the routine
4239 -- appends to New_Assoc_List the discriminant value specified in the
4240 -- ancestor part.
4242 -- If the aggregate is in a context with expansion delayed, it will be
4243 -- reanalyzed. The inherited discriminant values must not be reinserted
4244 -- in the component list to prevent spurious errors, but they must be
4245 -- present on first analysis to build the proper subtype indications.
4246 -- The flag Inherited_Discriminant is used to prevent the re-insertion.
4248 function Find_Private_Ancestor (Typ : Entity_Id) return Entity_Id;
4249 -- AI05-0115: Find earlier ancestor in the derivation chain that is
4250 -- derived from private view Typ. Whether the aggregate is legal depends
4251 -- on the current visibility of the type as well as that of the parent
4252 -- of the ancestor.
4254 function Get_Value
4255 (Compon : Entity_Id;
4256 From : List_Id;
4257 Consider_Others_Choice : Boolean := False) return Node_Id;
4258 -- Given a record component stored in parameter Compon, this function
4259 -- returns its value as it appears in the list From, which is a list
4260 -- of N_Component_Association nodes.
4262 -- If no component association has a choice for the searched component,
4263 -- the value provided by the others choice is returned, if there is one,
4264 -- and Consider_Others_Choice is set to true. Otherwise Empty is
4265 -- returned. If there is more than one component association giving a
4266 -- value for the searched record component, an error message is emitted
4267 -- and the first found value is returned.
4269 -- If Consider_Others_Choice is set and the returned expression comes
4270 -- from the others choice, then Others_Etype is set as a side effect.
4271 -- An error message is emitted if the components taking their value from
4272 -- the others choice do not have same type.
4274 procedure Propagate_Discriminants
4275 (Aggr : Node_Id;
4276 Assoc_List : List_Id);
4277 -- Nested components may themselves be discriminated types constrained
4278 -- by outer discriminants, whose values must be captured before the
4279 -- aggregate is expanded into assignments.
4281 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Entity_Id);
4282 -- Analyzes and resolves expression Expr against the Etype of the
4283 -- Component. This routine also applies all appropriate checks to Expr.
4284 -- It finally saves a Expr in the newly created association list that
4285 -- will be attached to the final record aggregate. Note that if the
4286 -- Parent pointer of Expr is not set then Expr was produced with a
4287 -- New_Copy_Tree or some such.
4289 procedure Rewrite_Range (Root_Type : Entity_Id; Rge : Node_Id);
4290 -- Rewrite a range node Rge when its bounds refer to non-stored
4291 -- discriminants from Root_Type, to replace them with the stored
4292 -- discriminant values. This is required in GNATprove mode, and is
4293 -- adopted in all modes to avoid special-casing GNATprove mode.
4295 ---------------------
4296 -- Add_Association --
4297 ---------------------
4299 procedure Add_Association
4300 (Component : Entity_Id;
4301 Expr : Node_Id;
4302 Assoc_List : List_Id;
4303 Is_Box_Present : Boolean := False)
4305 Choice_List : constant List_Id := New_List;
4306 Loc : Source_Ptr;
4308 begin
4309 -- If this is a box association the expression is missing, so use the
4310 -- Sloc of the aggregate itself for the new association.
4312 pragma Assert (Present (Expr) xor Is_Box_Present);
4314 if Present (Expr) then
4315 Loc := Sloc (Expr);
4316 else
4317 Loc := Sloc (N);
4318 end if;
4320 Append_To (Choice_List, New_Occurrence_Of (Component, Loc));
4322 Append_To (Assoc_List,
4323 Make_Component_Association (Loc,
4324 Choices => Choice_List,
4325 Expression => Expr,
4326 Box_Present => Is_Box_Present));
4328 -- If this association has a box for a component that is initialized
4329 -- by default, then set flag on the new association to indicate that
4330 -- the original association was for such a box-initialized component.
4332 if Is_Box_Init_By_Default then
4333 Set_Was_Default_Init_Box_Association (Last (Assoc_List));
4334 end if;
4335 end Add_Association;
4337 -----------------------------
4338 -- Add_Discriminant_Values --
4339 -----------------------------
4341 procedure Add_Discriminant_Values
4342 (New_Aggr : Node_Id;
4343 Assoc_List : List_Id)
4345 Assoc : Node_Id;
4346 Discr : Entity_Id;
4347 Discr_Elmt : Elmt_Id;
4348 Discr_Val : Node_Id;
4349 Val : Entity_Id;
4351 begin
4352 Discr := First_Discriminant (Etype (New_Aggr));
4353 Discr_Elmt := First_Elmt (Discriminant_Constraint (Etype (New_Aggr)));
4354 while Present (Discr_Elmt) loop
4355 Discr_Val := Node (Discr_Elmt);
4357 -- If the constraint is given by a discriminant then it is a
4358 -- discriminant of an enclosing record, and its value has already
4359 -- been placed in the association list.
4361 if Is_Entity_Name (Discr_Val)
4362 and then Ekind (Entity (Discr_Val)) = E_Discriminant
4363 then
4364 Val := Entity (Discr_Val);
4366 Assoc := First (Assoc_List);
4367 while Present (Assoc) loop
4368 if Present (Entity (First (Choices (Assoc))))
4369 and then Entity (First (Choices (Assoc))) = Val
4370 then
4371 Discr_Val := Expression (Assoc);
4372 exit;
4373 end if;
4375 Next (Assoc);
4376 end loop;
4377 end if;
4379 Add_Association
4380 (Discr, New_Copy_Tree (Discr_Val),
4381 Component_Associations (New_Aggr));
4383 -- If the discriminant constraint is a current instance, mark the
4384 -- current aggregate so that the self-reference can be expanded
4385 -- later. The constraint may refer to the subtype of aggregate, so
4386 -- use base type for comparison.
4388 if Nkind (Discr_Val) = N_Attribute_Reference
4389 and then Is_Entity_Name (Prefix (Discr_Val))
4390 and then Is_Type (Entity (Prefix (Discr_Val)))
4391 and then Base_Type (Etype (N)) = Entity (Prefix (Discr_Val))
4392 then
4393 Set_Has_Self_Reference (N);
4394 end if;
4396 Next_Elmt (Discr_Elmt);
4397 Next_Discriminant (Discr);
4398 end loop;
4399 end Add_Discriminant_Values;
4401 --------------------------
4402 -- Discriminant_Present --
4403 --------------------------
4405 function Discriminant_Present (Input_Discr : Entity_Id) return Boolean is
4406 Regular_Aggr : constant Boolean := Nkind (N) /= N_Extension_Aggregate;
4408 Ancestor_Is_Subtyp : Boolean;
4410 Loc : Source_Ptr;
4412 Ancestor : Node_Id;
4413 Ancestor_Typ : Entity_Id;
4414 Comp_Assoc : Node_Id;
4415 Discr : Entity_Id;
4416 Discr_Expr : Node_Id;
4417 Discr_Val : Elmt_Id := No_Elmt;
4418 Orig_Discr : Entity_Id;
4420 begin
4421 if Regular_Aggr then
4422 return True;
4423 end if;
4425 -- Check whether inherited discriminant values have already been
4426 -- inserted in the aggregate. This will be the case if we are
4427 -- re-analyzing an aggregate whose expansion was delayed.
4429 if Present (Component_Associations (N)) then
4430 Comp_Assoc := First (Component_Associations (N));
4431 while Present (Comp_Assoc) loop
4432 if Inherited_Discriminant (Comp_Assoc) then
4433 return True;
4434 end if;
4436 Next (Comp_Assoc);
4437 end loop;
4438 end if;
4440 Ancestor := Ancestor_Part (N);
4441 Ancestor_Typ := Etype (Ancestor);
4442 Loc := Sloc (Ancestor);
4444 -- For a private type with unknown discriminants, use the underlying
4445 -- record view if it is available.
4447 if Has_Unknown_Discriminants (Ancestor_Typ)
4448 and then Present (Full_View (Ancestor_Typ))
4449 and then Present (Underlying_Record_View (Full_View (Ancestor_Typ)))
4450 then
4451 Ancestor_Typ := Underlying_Record_View (Full_View (Ancestor_Typ));
4452 end if;
4454 Ancestor_Is_Subtyp :=
4455 Is_Entity_Name (Ancestor) and then Is_Type (Entity (Ancestor));
4457 -- If the ancestor part has no discriminants clearly N's aggregate
4458 -- part must provide a value for Discr.
4460 if not Has_Discriminants (Ancestor_Typ) then
4461 return True;
4463 -- If the ancestor part is an unconstrained subtype mark then the
4464 -- Discr must be present in N's aggregate part.
4466 elsif Ancestor_Is_Subtyp
4467 and then not Is_Constrained (Entity (Ancestor))
4468 then
4469 return True;
4470 end if;
4472 -- Now look to see if Discr was specified in the ancestor part
4474 if Ancestor_Is_Subtyp then
4475 Discr_Val :=
4476 First_Elmt (Discriminant_Constraint (Entity (Ancestor)));
4477 end if;
4479 Orig_Discr := Original_Record_Component (Input_Discr);
4481 Discr := First_Discriminant (Ancestor_Typ);
4482 while Present (Discr) loop
4484 -- If Ancestor has already specified Disc value then insert its
4485 -- value in the final aggregate.
4487 if Original_Record_Component (Discr) = Orig_Discr then
4488 if Ancestor_Is_Subtyp then
4489 Discr_Expr := New_Copy_Tree (Node (Discr_Val));
4490 else
4491 Discr_Expr :=
4492 Make_Selected_Component (Loc,
4493 Prefix => Duplicate_Subexpr (Ancestor),
4494 Selector_Name => New_Occurrence_Of (Input_Discr, Loc));
4495 end if;
4497 Resolve_Aggr_Expr (Discr_Expr, Input_Discr);
4498 Set_Inherited_Discriminant (Last (New_Assoc_List));
4499 return False;
4500 end if;
4502 Next_Discriminant (Discr);
4504 if Ancestor_Is_Subtyp then
4505 Next_Elmt (Discr_Val);
4506 end if;
4507 end loop;
4509 return True;
4510 end Discriminant_Present;
4512 ---------------------------
4513 -- Find_Private_Ancestor --
4514 ---------------------------
4516 function Find_Private_Ancestor (Typ : Entity_Id) return Entity_Id is
4517 Par : Entity_Id;
4519 begin
4520 Par := Typ;
4521 loop
4522 if Has_Private_Ancestor (Par)
4523 and then not Has_Private_Ancestor (Etype (Base_Type (Par)))
4524 then
4525 return Par;
4527 elsif not Is_Derived_Type (Par) then
4528 return Empty;
4530 else
4531 Par := Etype (Base_Type (Par));
4532 end if;
4533 end loop;
4534 end Find_Private_Ancestor;
4536 ---------------
4537 -- Get_Value --
4538 ---------------
4540 function Get_Value
4541 (Compon : Entity_Id;
4542 From : List_Id;
4543 Consider_Others_Choice : Boolean := False) return Node_Id
4545 Typ : constant Entity_Id := Etype (Compon);
4546 Assoc : Node_Id;
4547 Expr : Node_Id := Empty;
4548 Selector_Name : Node_Id;
4550 begin
4551 Is_Box_Present := False;
4552 Is_Box_Init_By_Default := False;
4554 if No (From) then
4555 return Empty;
4556 end if;
4558 Assoc := First (From);
4559 while Present (Assoc) loop
4560 Selector_Name := First (Choices (Assoc));
4561 while Present (Selector_Name) loop
4562 if Nkind (Selector_Name) = N_Others_Choice then
4563 if Consider_Others_Choice and then No (Expr) then
4565 -- We need to duplicate the expression for each
4566 -- successive component covered by the others choice.
4567 -- This is redundant if the others_choice covers only
4568 -- one component (small optimization possible???), but
4569 -- indispensable otherwise, because each one must be
4570 -- expanded individually to preserve side effects.
4572 -- Ada 2005 (AI-287): In case of default initialization
4573 -- of components, we duplicate the corresponding default
4574 -- expression (from the record type declaration). The
4575 -- copy must carry the sloc of the association (not the
4576 -- original expression) to prevent spurious elaboration
4577 -- checks when the default includes function calls.
4579 if Box_Present (Assoc) then
4580 Others_Box := Others_Box + 1;
4581 Is_Box_Present := True;
4583 if Expander_Active then
4584 return
4585 New_Copy_Tree_And_Copy_Dimensions
4586 (Expression (Parent (Compon)),
4587 New_Sloc => Sloc (Assoc));
4588 else
4589 return Expression (Parent (Compon));
4590 end if;
4592 else
4593 if Present (Others_Etype)
4594 and then Base_Type (Others_Etype) /= Base_Type (Typ)
4595 then
4596 -- If the components are of an anonymous access
4597 -- type they are distinct, but this is legal in
4598 -- Ada 2012 as long as designated types match.
4600 if (Ekind (Typ) = E_Anonymous_Access_Type
4601 or else Ekind (Typ) =
4602 E_Anonymous_Access_Subprogram_Type)
4603 and then Designated_Type (Typ) =
4604 Designated_Type (Others_Etype)
4605 then
4606 null;
4607 else
4608 Error_Msg_N
4609 ("components in OTHERS choice must have same "
4610 & "type", Selector_Name);
4611 end if;
4612 end if;
4614 Others_Etype := Typ;
4616 -- Copy the expression so that it is resolved
4617 -- independently for each component, This is needed
4618 -- for accessibility checks on components of anonymous
4619 -- access types, even in compile_only mode.
4621 if not Inside_A_Generic then
4622 return
4623 New_Copy_Tree_And_Copy_Dimensions
4624 (Expression (Assoc));
4625 else
4626 return Expression (Assoc);
4627 end if;
4628 end if;
4629 end if;
4631 elsif Chars (Compon) = Chars (Selector_Name) then
4632 if No (Expr) then
4634 -- Ada 2005 (AI-231)
4636 if Ada_Version >= Ada_2005
4637 and then Known_Null (Expression (Assoc))
4638 then
4639 Check_Can_Never_Be_Null (Compon, Expression (Assoc));
4640 end if;
4642 -- We need to duplicate the expression when several
4643 -- components are grouped together with a "|" choice.
4644 -- For instance "filed1 | filed2 => Expr"
4646 -- Ada 2005 (AI-287)
4648 if Box_Present (Assoc) then
4649 Is_Box_Present := True;
4651 -- Duplicate the default expression of the component
4652 -- from the record type declaration, so a new copy
4653 -- can be attached to the association.
4655 -- Note that we always copy the default expression,
4656 -- even when the association has a single choice, in
4657 -- order to create a proper association for the
4658 -- expanded aggregate.
4660 -- Component may have no default, in which case the
4661 -- expression is empty and the component is default-
4662 -- initialized, but an association for the component
4663 -- exists, and it is not covered by an others clause.
4665 -- Scalar and private types have no initialization
4666 -- procedure, so they remain uninitialized. If the
4667 -- target of the aggregate is a constant this
4668 -- deserves a warning.
4670 if No (Expression (Parent (Compon)))
4671 and then not Has_Non_Null_Base_Init_Proc (Typ)
4672 and then not Has_Aspect (Typ, Aspect_Default_Value)
4673 and then not Is_Concurrent_Type (Typ)
4674 and then Nkind (Parent (N)) = N_Object_Declaration
4675 and then Constant_Present (Parent (N))
4676 then
4677 Error_Msg_Node_2 := Typ;
4678 Error_Msg_NE
4679 ("??component& of type& is uninitialized",
4680 Assoc, Selector_Name);
4682 -- An additional reminder if the component type
4683 -- is a generic formal.
4685 if Is_Generic_Type (Base_Type (Typ)) then
4686 Error_Msg_NE
4687 ("\instance should provide actual type with "
4688 & "initialization for&", Assoc, Typ);
4689 end if;
4690 end if;
4692 return
4693 New_Copy_Tree_And_Copy_Dimensions
4694 (Expression (Parent (Compon)));
4696 else
4697 if Present (Next (Selector_Name)) then
4698 Expr := New_Copy_Tree_And_Copy_Dimensions
4699 (Expression (Assoc));
4700 else
4701 Expr := Expression (Assoc);
4702 end if;
4703 end if;
4705 Generate_Reference (Compon, Selector_Name, 'm');
4707 else
4708 Error_Msg_NE
4709 ("more than one value supplied for &",
4710 Selector_Name, Compon);
4712 end if;
4713 end if;
4715 Next (Selector_Name);
4716 end loop;
4718 Next (Assoc);
4719 end loop;
4721 return Expr;
4722 end Get_Value;
4724 -----------------------------
4725 -- Propagate_Discriminants --
4726 -----------------------------
4728 procedure Propagate_Discriminants
4729 (Aggr : Node_Id;
4730 Assoc_List : List_Id)
4732 Loc : constant Source_Ptr := Sloc (N);
4734 procedure Process_Component (Comp : Entity_Id);
4735 -- Add one component with a box association to the inner aggregate,
4736 -- and recurse if component is itself composite.
4738 -----------------------
4739 -- Process_Component --
4740 -----------------------
4742 procedure Process_Component (Comp : Entity_Id) is
4743 T : constant Entity_Id := Etype (Comp);
4744 New_Aggr : Node_Id;
4746 begin
4747 if Is_Record_Type (T) and then Has_Discriminants (T) then
4748 New_Aggr := Make_Aggregate (Loc, No_List, New_List);
4749 Set_Etype (New_Aggr, T);
4751 Add_Association
4752 (Comp, New_Aggr, Component_Associations (Aggr));
4754 -- Collect discriminant values and recurse
4756 Add_Discriminant_Values (New_Aggr, Assoc_List);
4757 Propagate_Discriminants (New_Aggr, Assoc_List);
4759 Build_Constrained_Itype
4760 (New_Aggr, T, Component_Associations (New_Aggr));
4761 else
4762 Add_Association
4763 (Comp, Empty, Component_Associations (Aggr),
4764 Is_Box_Present => True);
4765 end if;
4766 end Process_Component;
4768 -- Local variables
4770 Aggr_Type : constant Entity_Id := Base_Type (Etype (Aggr));
4771 Components : constant Elist_Id := New_Elmt_List;
4772 Def_Node : constant Node_Id :=
4773 Type_Definition (Declaration_Node (Aggr_Type));
4775 Comp : Node_Id;
4776 Comp_Elmt : Elmt_Id;
4777 Errors : Boolean;
4779 -- Start of processing for Propagate_Discriminants
4781 begin
4782 -- The component type may be a variant type. Collect the components
4783 -- that are ruled by the known values of the discriminants. Their
4784 -- values have already been inserted into the component list of the
4785 -- current aggregate.
4787 if Nkind (Def_Node) = N_Record_Definition
4788 and then Present (Component_List (Def_Node))
4789 and then Present (Variant_Part (Component_List (Def_Node)))
4790 then
4791 Gather_Components (Aggr_Type,
4792 Component_List (Def_Node),
4793 Governed_By => Component_Associations (Aggr),
4794 Into => Components,
4795 Report_Errors => Errors);
4797 Comp_Elmt := First_Elmt (Components);
4798 while Present (Comp_Elmt) loop
4799 if Ekind (Node (Comp_Elmt)) /= E_Discriminant then
4800 Process_Component (Node (Comp_Elmt));
4801 end if;
4803 Next_Elmt (Comp_Elmt);
4804 end loop;
4806 -- No variant part, iterate over all components
4808 else
4809 Comp := First_Component (Etype (Aggr));
4810 while Present (Comp) loop
4811 Process_Component (Comp);
4812 Next_Component (Comp);
4813 end loop;
4814 end if;
4815 end Propagate_Discriminants;
4817 -----------------------
4818 -- Resolve_Aggr_Expr --
4819 -----------------------
4821 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Entity_Id) is
4822 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean;
4823 -- If the expression is an aggregate (possibly qualified) then its
4824 -- expansion is delayed until the enclosing aggregate is expanded
4825 -- into assignments. In that case, do not generate checks on the
4826 -- expression, because they will be generated later, and will other-
4827 -- wise force a copy (to remove side effects) that would leave a
4828 -- dynamic-sized aggregate in the code, something that gigi cannot
4829 -- handle.
4831 ---------------------------
4832 -- Has_Expansion_Delayed --
4833 ---------------------------
4835 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean is
4836 begin
4837 return
4838 (Nkind (Expr) in N_Aggregate | N_Extension_Aggregate
4839 and then Present (Etype (Expr))
4840 and then Is_Record_Type (Etype (Expr))
4841 and then Expansion_Delayed (Expr))
4842 or else
4843 (Nkind (Expr) = N_Qualified_Expression
4844 and then Has_Expansion_Delayed (Expression (Expr)));
4845 end Has_Expansion_Delayed;
4847 -- Local variables
4849 Expr_Type : Entity_Id := Empty;
4850 New_C : Entity_Id := Component;
4851 New_Expr : Node_Id;
4853 Relocate : Boolean;
4854 -- Set to True if the resolved Expr node needs to be relocated when
4855 -- attached to the newly created association list. This node need not
4856 -- be relocated if its parent pointer is not set. In fact in this
4857 -- case Expr is the output of a New_Copy_Tree call. If Relocate is
4858 -- True then we have analyzed the expression node in the original
4859 -- aggregate and hence it needs to be relocated when moved over to
4860 -- the new association list.
4862 -- Start of processing for Resolve_Aggr_Expr
4864 begin
4865 -- If the type of the component is elementary or the type of the
4866 -- aggregate does not contain discriminants, use the type of the
4867 -- component to resolve Expr.
4869 if Is_Elementary_Type (Etype (Component))
4870 or else not Has_Discriminants (Etype (N))
4871 then
4872 Expr_Type := Etype (Component);
4874 -- Otherwise we have to pick up the new type of the component from
4875 -- the new constrained subtype of the aggregate. In fact components
4876 -- which are of a composite type might be constrained by a
4877 -- discriminant, and we want to resolve Expr against the subtype were
4878 -- all discriminant occurrences are replaced with their actual value.
4880 else
4881 New_C := First_Component (Etype (N));
4882 while Present (New_C) loop
4883 if Chars (New_C) = Chars (Component) then
4884 Expr_Type := Etype (New_C);
4885 exit;
4886 end if;
4888 Next_Component (New_C);
4889 end loop;
4891 pragma Assert (Present (Expr_Type));
4893 -- For each range in an array type where a discriminant has been
4894 -- replaced with the constraint, check that this range is within
4895 -- the range of the base type. This checks is done in the init
4896 -- proc for regular objects, but has to be done here for
4897 -- aggregates since no init proc is called for them.
4899 if Is_Array_Type (Expr_Type) then
4900 declare
4901 Index : Node_Id;
4902 -- Range of the current constrained index in the array
4904 Orig_Index : Node_Id := First_Index (Etype (Component));
4905 -- Range corresponding to the range Index above in the
4906 -- original unconstrained record type. The bounds of this
4907 -- range may be governed by discriminants.
4909 Unconstr_Index : Node_Id := First_Index (Etype (Expr_Type));
4910 -- Range corresponding to the range Index above for the
4911 -- unconstrained array type. This range is needed to apply
4912 -- range checks.
4914 begin
4915 Index := First_Index (Expr_Type);
4916 while Present (Index) loop
4917 if Depends_On_Discriminant (Orig_Index) then
4918 Apply_Range_Check (Index, Etype (Unconstr_Index));
4919 end if;
4921 Next_Index (Index);
4922 Next_Index (Orig_Index);
4923 Next_Index (Unconstr_Index);
4924 end loop;
4925 end;
4926 end if;
4927 end if;
4929 -- If the Parent pointer of Expr is not set, Expr is an expression
4930 -- duplicated by New_Tree_Copy (this happens for record aggregates
4931 -- that look like (Field1 | Filed2 => Expr) or (others => Expr)).
4932 -- Such a duplicated expression must be attached to the tree
4933 -- before analysis and resolution to enforce the rule that a tree
4934 -- fragment should never be analyzed or resolved unless it is
4935 -- attached to the current compilation unit.
4937 if No (Parent (Expr)) then
4938 Set_Parent (Expr, N);
4939 Relocate := False;
4940 else
4941 Relocate := True;
4942 end if;
4944 Analyze_And_Resolve (Expr, Expr_Type);
4945 Check_Expr_OK_In_Limited_Aggregate (Expr);
4946 Check_Non_Static_Context (Expr);
4947 Check_Unset_Reference (Expr);
4949 -- Check wrong use of class-wide types
4951 if Is_Class_Wide_Type (Etype (Expr)) then
4952 Error_Msg_N ("dynamically tagged expression not allowed", Expr);
4953 end if;
4955 if not Has_Expansion_Delayed (Expr) then
4956 Aggregate_Constraint_Checks (Expr, Expr_Type);
4957 end if;
4959 -- If an aggregate component has a type with predicates, an explicit
4960 -- predicate check must be applied, as for an assignment statement,
4961 -- because the aggregate might not be expanded into individual
4962 -- component assignments.
4964 if Has_Predicates (Expr_Type)
4965 and then Analyzed (Expr)
4966 then
4967 Apply_Predicate_Check (Expr, Expr_Type);
4968 end if;
4970 if Raises_Constraint_Error (Expr) then
4971 Set_Raises_Constraint_Error (N);
4972 end if;
4974 -- If the expression has been marked as requiring a range check, then
4975 -- generate it here. It's a bit odd to be generating such checks in
4976 -- the analyzer, but harmless since Generate_Range_Check does nothing
4977 -- (other than making sure Do_Range_Check is set) if the expander is
4978 -- not active.
4980 if Do_Range_Check (Expr) then
4981 Generate_Range_Check (Expr, Expr_Type, CE_Range_Check_Failed);
4982 end if;
4984 -- Add association Component => Expr if the caller requests it
4986 if Relocate then
4987 New_Expr := Relocate_Node (Expr);
4989 -- Since New_Expr is not gonna be analyzed later on, we need to
4990 -- propagate here the dimensions form Expr to New_Expr.
4992 Copy_Dimensions (Expr, New_Expr);
4994 else
4995 New_Expr := Expr;
4996 end if;
4998 Add_Association (New_C, New_Expr, New_Assoc_List);
4999 end Resolve_Aggr_Expr;
5001 -------------------
5002 -- Rewrite_Range --
5003 -------------------
5005 procedure Rewrite_Range (Root_Type : Entity_Id; Rge : Node_Id) is
5006 procedure Rewrite_Bound
5007 (Bound : Node_Id;
5008 Disc : Entity_Id;
5009 Expr_Disc : Node_Id);
5010 -- Rewrite a bound of the range Bound, when it is equal to the
5011 -- non-stored discriminant Disc, into the stored discriminant
5012 -- value Expr_Disc.
5014 -------------------
5015 -- Rewrite_Bound --
5016 -------------------
5018 procedure Rewrite_Bound
5019 (Bound : Node_Id;
5020 Disc : Entity_Id;
5021 Expr_Disc : Node_Id)
5023 begin
5024 if Nkind (Bound) /= N_Identifier then
5025 return;
5026 end if;
5028 -- We expect either the discriminant or the discriminal
5030 if Entity (Bound) = Disc
5031 or else (Ekind (Entity (Bound)) = E_In_Parameter
5032 and then Discriminal_Link (Entity (Bound)) = Disc)
5033 then
5034 Rewrite (Bound, New_Copy_Tree (Expr_Disc));
5035 end if;
5036 end Rewrite_Bound;
5038 -- Local variables
5040 Low, High : Node_Id;
5041 Disc : Entity_Id;
5042 Expr_Disc : Elmt_Id;
5044 -- Start of processing for Rewrite_Range
5046 begin
5047 if Has_Discriminants (Root_Type) and then Nkind (Rge) = N_Range then
5048 Low := Low_Bound (Rge);
5049 High := High_Bound (Rge);
5051 Disc := First_Discriminant (Root_Type);
5052 Expr_Disc := First_Elmt (Stored_Constraint (Etype (N)));
5053 while Present (Disc) loop
5054 Rewrite_Bound (Low, Disc, Node (Expr_Disc));
5055 Rewrite_Bound (High, Disc, Node (Expr_Disc));
5056 Next_Discriminant (Disc);
5057 Next_Elmt (Expr_Disc);
5058 end loop;
5059 end if;
5060 end Rewrite_Range;
5062 -- Local variables
5064 Components : constant Elist_Id := New_Elmt_List;
5065 -- Components is the list of the record components whose value must be
5066 -- provided in the aggregate. This list does include discriminants.
5068 Component : Entity_Id;
5069 Component_Elmt : Elmt_Id;
5070 Expr : Node_Id;
5071 Positional_Expr : Node_Id;
5073 -- Start of processing for Resolve_Record_Aggregate
5075 begin
5076 -- A record aggregate is restricted in SPARK:
5078 -- Each named association can have only a single choice.
5079 -- OTHERS cannot be used.
5080 -- Positional and named associations cannot be mixed.
5082 if Present (Component_Associations (N)) then
5083 declare
5084 Assoc : Node_Id;
5086 begin
5087 Assoc := First (Component_Associations (N));
5088 while Present (Assoc) loop
5089 if Nkind (Assoc) = N_Iterated_Component_Association then
5090 Error_Msg_N
5091 ("iterated component association can only appear in an "
5092 & "array aggregate", N);
5093 raise Unrecoverable_Error;
5094 end if;
5096 Next (Assoc);
5097 end loop;
5098 end;
5099 end if;
5101 -- We may end up calling Duplicate_Subexpr on expressions that are
5102 -- attached to New_Assoc_List. For this reason we need to attach it
5103 -- to the tree by setting its parent pointer to N. This parent point
5104 -- will change in STEP 8 below.
5106 Set_Parent (New_Assoc_List, N);
5108 -- STEP 1: abstract type and null record verification
5110 if Is_Abstract_Type (Typ) then
5111 Error_Msg_N ("type of aggregate cannot be abstract", N);
5112 end if;
5114 if No (First_Entity (Typ)) and then Null_Record_Present (N) then
5115 Set_Etype (N, Typ);
5116 return;
5118 elsif Present (First_Entity (Typ))
5119 and then Null_Record_Present (N)
5120 and then not Is_Tagged_Type (Typ)
5121 then
5122 Error_Msg_N ("record aggregate cannot be null", N);
5123 return;
5125 -- If the type has no components, then the aggregate should either
5126 -- have "null record", or in Ada 2005 it could instead have a single
5127 -- component association given by "others => <>". For Ada 95 we flag an
5128 -- error at this point, but for Ada 2005 we proceed with checking the
5129 -- associations below, which will catch the case where it's not an
5130 -- aggregate with "others => <>". Note that the legality of a <>
5131 -- aggregate for a null record type was established by AI05-016.
5133 elsif No (First_Entity (Typ))
5134 and then Ada_Version < Ada_2005
5135 then
5136 Error_Msg_N ("record aggregate must be null", N);
5137 return;
5138 end if;
5140 -- A record aggregate can only use parentheses
5142 if Nkind (N) = N_Aggregate
5143 and then Is_Homogeneous_Aggregate (N)
5144 then
5145 Error_Msg_N ("record aggregate must use (), not '[']", N);
5146 return;
5147 end if;
5149 -- STEP 2: Verify aggregate structure
5151 Step_2 : declare
5152 Assoc : Node_Id;
5153 Bad_Aggregate : Boolean := False;
5154 Selector_Name : Node_Id;
5156 begin
5157 if Present (Component_Associations (N)) then
5158 Assoc := First (Component_Associations (N));
5159 else
5160 Assoc := Empty;
5161 end if;
5163 while Present (Assoc) loop
5164 Selector_Name := First (Choices (Assoc));
5165 while Present (Selector_Name) loop
5166 if Nkind (Selector_Name) = N_Identifier then
5167 null;
5169 elsif Nkind (Selector_Name) = N_Others_Choice then
5170 if Selector_Name /= First (Choices (Assoc))
5171 or else Present (Next (Selector_Name))
5172 then
5173 Error_Msg_N
5174 ("OTHERS must appear alone in a choice list",
5175 Selector_Name);
5176 return;
5178 elsif Present (Next (Assoc)) then
5179 Error_Msg_N
5180 ("OTHERS must appear last in an aggregate",
5181 Selector_Name);
5182 return;
5184 -- (Ada 2005): If this is an association with a box,
5185 -- indicate that the association need not represent
5186 -- any component.
5188 elsif Box_Present (Assoc) then
5189 Others_Box := 1;
5190 Box_Node := Assoc;
5191 end if;
5193 else
5194 Error_Msg_N
5195 ("selector name should be identifier or OTHERS",
5196 Selector_Name);
5197 Bad_Aggregate := True;
5198 end if;
5200 Next (Selector_Name);
5201 end loop;
5203 Next (Assoc);
5204 end loop;
5206 if Bad_Aggregate then
5207 return;
5208 end if;
5209 end Step_2;
5211 -- STEP 3: Find discriminant Values
5213 Step_3 : declare
5214 Discrim : Entity_Id;
5215 Missing_Discriminants : Boolean := False;
5217 begin
5218 if Present (Expressions (N)) then
5219 Positional_Expr := First (Expressions (N));
5220 else
5221 Positional_Expr := Empty;
5222 end if;
5224 -- AI05-0115: if the ancestor part is a subtype mark, the ancestor
5225 -- must not have unknown discriminants.
5226 -- ??? We are not checking any subtype mark here and this code is not
5227 -- exercised by any test, so it's likely wrong (in particular
5228 -- we should not use Root_Type here but the subtype mark, if any),
5229 -- and possibly not needed.
5231 if Is_Derived_Type (Typ)
5232 and then Has_Unknown_Discriminants (Root_Type (Typ))
5233 and then Nkind (N) /= N_Extension_Aggregate
5234 then
5235 Error_Msg_NE
5236 ("aggregate not available for type& whose ancestor "
5237 & "has unknown discriminants", N, Typ);
5238 end if;
5240 if Has_Unknown_Discriminants (Typ)
5241 and then Present (Underlying_Record_View (Typ))
5242 then
5243 Discrim := First_Discriminant (Underlying_Record_View (Typ));
5244 elsif Has_Discriminants (Typ) then
5245 Discrim := First_Discriminant (Typ);
5246 else
5247 Discrim := Empty;
5248 end if;
5250 -- First find the discriminant values in the positional components
5252 while Present (Discrim) and then Present (Positional_Expr) loop
5253 if Discriminant_Present (Discrim) then
5254 Resolve_Aggr_Expr (Positional_Expr, Discrim);
5256 -- Ada 2005 (AI-231)
5258 if Ada_Version >= Ada_2005
5259 and then Known_Null (Positional_Expr)
5260 then
5261 Check_Can_Never_Be_Null (Discrim, Positional_Expr);
5262 end if;
5264 Next (Positional_Expr);
5265 end if;
5267 if Present (Get_Value (Discrim, Component_Associations (N))) then
5268 Error_Msg_NE
5269 ("more than one value supplied for discriminant&",
5270 N, Discrim);
5271 end if;
5273 Next_Discriminant (Discrim);
5274 end loop;
5276 -- Find remaining discriminant values if any among named components
5278 while Present (Discrim) loop
5279 Expr := Get_Value (Discrim, Component_Associations (N), True);
5281 if not Discriminant_Present (Discrim) then
5282 if Present (Expr) then
5283 Error_Msg_NE
5284 ("more than one value supplied for discriminant &",
5285 N, Discrim);
5286 end if;
5288 elsif No (Expr) then
5289 Error_Msg_NE
5290 ("no value supplied for discriminant &", N, Discrim);
5291 Missing_Discriminants := True;
5293 else
5294 Resolve_Aggr_Expr (Expr, Discrim);
5295 end if;
5297 Next_Discriminant (Discrim);
5298 end loop;
5300 if Missing_Discriminants then
5301 return;
5302 end if;
5304 -- At this point and until the beginning of STEP 6, New_Assoc_List
5305 -- contains only the discriminants and their values.
5307 end Step_3;
5309 -- STEP 4: Set the Etype of the record aggregate
5311 if Has_Discriminants (Typ)
5312 or else (Has_Unknown_Discriminants (Typ)
5313 and then Present (Underlying_Record_View (Typ)))
5314 then
5315 Build_Constrained_Itype (N, Typ, New_Assoc_List);
5316 else
5317 Set_Etype (N, Typ);
5318 end if;
5320 -- STEP 5: Get remaining components according to discriminant values
5322 Step_5 : declare
5323 Dnode : Node_Id;
5324 Errors_Found : Boolean := False;
5325 Record_Def : Node_Id;
5326 Parent_Typ : Entity_Id;
5327 Parent_Typ_List : Elist_Id;
5328 Parent_Elmt : Elmt_Id;
5329 Root_Typ : Entity_Id;
5331 begin
5332 if Is_Derived_Type (Typ) and then Is_Tagged_Type (Typ) then
5333 Parent_Typ_List := New_Elmt_List;
5335 -- If this is an extension aggregate, the component list must
5336 -- include all components that are not in the given ancestor type.
5337 -- Otherwise, the component list must include components of all
5338 -- ancestors, starting with the root.
5340 if Nkind (N) = N_Extension_Aggregate then
5341 Root_Typ := Base_Type (Etype (Ancestor_Part (N)));
5343 else
5344 -- AI05-0115: check legality of aggregate for type with a
5345 -- private ancestor.
5347 Root_Typ := Root_Type (Typ);
5348 if Has_Private_Ancestor (Typ) then
5349 declare
5350 Ancestor : constant Entity_Id :=
5351 Find_Private_Ancestor (Typ);
5352 Ancestor_Unit : constant Entity_Id :=
5353 Cunit_Entity
5354 (Get_Source_Unit (Ancestor));
5355 Parent_Unit : constant Entity_Id :=
5356 Cunit_Entity (Get_Source_Unit
5357 (Base_Type (Etype (Ancestor))));
5358 begin
5359 -- Check whether we are in a scope that has full view
5360 -- over the private ancestor and its parent. This can
5361 -- only happen if the derivation takes place in a child
5362 -- unit of the unit that declares the parent, and we are
5363 -- in the private part or body of that child unit, else
5364 -- the aggregate is illegal.
5366 if Is_Child_Unit (Ancestor_Unit)
5367 and then Scope (Ancestor_Unit) = Parent_Unit
5368 and then In_Open_Scopes (Scope (Ancestor))
5369 and then
5370 (In_Private_Part (Scope (Ancestor))
5371 or else In_Package_Body (Scope (Ancestor)))
5372 then
5373 null;
5375 else
5376 Error_Msg_NE
5377 ("type of aggregate has private ancestor&!",
5378 N, Root_Typ);
5379 Error_Msg_N ("must use extension aggregate!", N);
5380 return;
5381 end if;
5382 end;
5383 end if;
5385 Dnode := Declaration_Node (Base_Type (Root_Typ));
5387 -- If we don't get a full declaration, then we have some error
5388 -- which will get signalled later so skip this part. Otherwise
5389 -- gather components of root that apply to the aggregate type.
5390 -- We use the base type in case there is an applicable stored
5391 -- constraint that renames the discriminants of the root.
5393 if Nkind (Dnode) = N_Full_Type_Declaration then
5394 Record_Def := Type_Definition (Dnode);
5395 Gather_Components
5396 (Base_Type (Typ),
5397 Component_List (Record_Def),
5398 Governed_By => New_Assoc_List,
5399 Into => Components,
5400 Report_Errors => Errors_Found);
5402 if Errors_Found then
5403 Error_Msg_N
5404 ("discriminant controlling variant part is not static",
5406 return;
5407 end if;
5408 end if;
5409 end if;
5411 Parent_Typ := Base_Type (Typ);
5412 while Parent_Typ /= Root_Typ loop
5413 Prepend_Elmt (Parent_Typ, To => Parent_Typ_List);
5414 Parent_Typ := Etype (Parent_Typ);
5416 -- Check whether a private parent requires the use of
5417 -- an extension aggregate. This test does not apply in
5418 -- an instantiation: if the generic unit is legal so is
5419 -- the instance.
5421 if Nkind (Parent (Base_Type (Parent_Typ))) =
5422 N_Private_Type_Declaration
5423 or else Nkind (Parent (Base_Type (Parent_Typ))) =
5424 N_Private_Extension_Declaration
5425 then
5426 if Nkind (N) /= N_Extension_Aggregate
5427 and then not In_Instance
5428 then
5429 Error_Msg_NE
5430 ("type of aggregate has private ancestor&!",
5431 N, Parent_Typ);
5432 Error_Msg_N ("must use extension aggregate!", N);
5433 return;
5435 elsif Parent_Typ /= Root_Typ then
5436 Error_Msg_NE
5437 ("ancestor part of aggregate must be private type&",
5438 Ancestor_Part (N), Parent_Typ);
5439 return;
5440 end if;
5442 -- The current view of ancestor part may be a private type,
5443 -- while the context type is always non-private.
5445 elsif Is_Private_Type (Root_Typ)
5446 and then Present (Full_View (Root_Typ))
5447 and then Nkind (N) = N_Extension_Aggregate
5448 then
5449 exit when Base_Type (Full_View (Root_Typ)) = Parent_Typ;
5450 end if;
5451 end loop;
5453 -- Now collect components from all other ancestors, beginning
5454 -- with the current type. If the type has unknown discriminants
5455 -- use the component list of the Underlying_Record_View, which
5456 -- needs to be used for the subsequent expansion of the aggregate
5457 -- into assignments.
5459 Parent_Elmt := First_Elmt (Parent_Typ_List);
5460 while Present (Parent_Elmt) loop
5461 Parent_Typ := Node (Parent_Elmt);
5463 if Has_Unknown_Discriminants (Parent_Typ)
5464 and then Present (Underlying_Record_View (Typ))
5465 then
5466 Parent_Typ := Underlying_Record_View (Parent_Typ);
5467 end if;
5469 Record_Def := Type_Definition (Parent (Base_Type (Parent_Typ)));
5470 Gather_Components (Empty,
5471 Component_List (Record_Extension_Part (Record_Def)),
5472 Governed_By => New_Assoc_List,
5473 Into => Components,
5474 Report_Errors => Errors_Found);
5476 Next_Elmt (Parent_Elmt);
5477 end loop;
5479 -- Typ is not a derived tagged type
5481 else
5482 Record_Def := Type_Definition (Parent (Base_Type (Typ)));
5484 if Null_Present (Record_Def) then
5485 null;
5487 elsif not Has_Unknown_Discriminants (Typ) then
5488 Gather_Components
5489 (Base_Type (Typ),
5490 Component_List (Record_Def),
5491 Governed_By => New_Assoc_List,
5492 Into => Components,
5493 Report_Errors => Errors_Found);
5495 else
5496 Gather_Components
5497 (Base_Type (Underlying_Record_View (Typ)),
5498 Component_List (Record_Def),
5499 Governed_By => New_Assoc_List,
5500 Into => Components,
5501 Report_Errors => Errors_Found);
5502 end if;
5503 end if;
5505 if Errors_Found then
5506 return;
5507 end if;
5508 end Step_5;
5510 -- STEP 6: Find component Values
5512 Component := Empty;
5513 Component_Elmt := First_Elmt (Components);
5515 -- First scan the remaining positional associations in the aggregate.
5516 -- Remember that at this point Positional_Expr contains the current
5517 -- positional association if any is left after looking for discriminant
5518 -- values in step 3.
5520 while Present (Positional_Expr) and then Present (Component_Elmt) loop
5521 Component := Node (Component_Elmt);
5522 Resolve_Aggr_Expr (Positional_Expr, Component);
5524 -- Ada 2005 (AI-231)
5526 if Ada_Version >= Ada_2005 and then Known_Null (Positional_Expr) then
5527 Check_Can_Never_Be_Null (Component, Positional_Expr);
5528 end if;
5530 if Present (Get_Value (Component, Component_Associations (N))) then
5531 Error_Msg_NE
5532 ("more than one value supplied for component &", N, Component);
5533 end if;
5535 Next (Positional_Expr);
5536 Next_Elmt (Component_Elmt);
5537 end loop;
5539 if Present (Positional_Expr) then
5540 Error_Msg_N
5541 ("too many components for record aggregate", Positional_Expr);
5542 end if;
5544 -- Now scan for the named arguments of the aggregate
5546 while Present (Component_Elmt) loop
5547 Component := Node (Component_Elmt);
5548 Expr := Get_Value (Component, Component_Associations (N), True);
5550 -- Note: The previous call to Get_Value sets the value of the
5551 -- variable Is_Box_Present.
5553 -- Ada 2005 (AI-287): Handle components with default initialization.
5554 -- Note: This feature was originally added to Ada 2005 for limited
5555 -- but it was finally allowed with any type.
5557 if Is_Box_Present then
5558 Check_Box_Component : declare
5559 Ctyp : constant Entity_Id := Etype (Component);
5561 begin
5562 -- Initially assume that the box is for a default-initialized
5563 -- component and reset to False in cases where that's not true.
5565 Is_Box_Init_By_Default := True;
5567 -- If there is a default expression for the aggregate, copy
5568 -- it into a new association. This copy must modify the scopes
5569 -- of internal types that may be attached to the expression
5570 -- (e.g. index subtypes of arrays) because in general the type
5571 -- declaration and the aggregate appear in different scopes,
5572 -- and the backend requires the scope of the type to match the
5573 -- point at which it is elaborated.
5575 -- If the component has an initialization procedure (IP) we
5576 -- pass the component to the expander, which will generate
5577 -- the call to such IP.
5579 -- If the component has discriminants, their values must
5580 -- be taken from their subtype. This is indispensable for
5581 -- constraints that are given by the current instance of an
5582 -- enclosing type, to allow the expansion of the aggregate to
5583 -- replace the reference to the current instance by the target
5584 -- object of the aggregate.
5586 if Is_Case_Choice_Pattern (N) then
5588 -- Do not transform box component values in a case-choice
5589 -- aggregate.
5591 Add_Association
5592 (Component => Component,
5593 Expr => Empty,
5594 Assoc_List => New_Assoc_List,
5595 Is_Box_Present => True);
5597 elsif Present (Parent (Component))
5598 and then Nkind (Parent (Component)) = N_Component_Declaration
5599 and then Present (Expression (Parent (Component)))
5600 then
5601 -- If component declaration has an initialization expression
5602 -- then this is not a case of default initialization.
5604 Is_Box_Init_By_Default := False;
5606 Expr :=
5607 New_Copy_Tree_And_Copy_Dimensions
5608 (Expression (Parent (Component)),
5609 New_Scope => Current_Scope,
5610 New_Sloc => Sloc (N));
5612 -- As the type of the copied default expression may refer
5613 -- to discriminants of the record type declaration, these
5614 -- non-stored discriminants need to be rewritten into stored
5615 -- discriminant values for the aggregate. This is required
5616 -- in GNATprove mode, and is adopted in all modes to avoid
5617 -- special-casing GNATprove mode.
5619 if Is_Array_Type (Etype (Expr)) then
5620 declare
5621 Rec_Typ : constant Entity_Id := Scope (Component);
5622 -- Root record type whose discriminants may be used as
5623 -- bounds in range nodes.
5625 Assoc : Node_Id;
5626 Choice : Node_Id;
5627 Index : Node_Id;
5629 begin
5630 -- Rewrite the range nodes occurring in the indexes
5631 -- and their types.
5633 Index := First_Index (Etype (Expr));
5634 while Present (Index) loop
5635 Rewrite_Range (Rec_Typ, Index);
5636 Rewrite_Range
5637 (Rec_Typ, Scalar_Range (Etype (Index)));
5639 Next_Index (Index);
5640 end loop;
5642 -- Rewrite the range nodes occurring as aggregate
5643 -- bounds and component associations.
5645 if Nkind (Expr) = N_Aggregate then
5646 if Present (Aggregate_Bounds (Expr)) then
5647 Rewrite_Range (Rec_Typ, Aggregate_Bounds (Expr));
5648 end if;
5650 if Present (Component_Associations (Expr)) then
5651 Assoc := First (Component_Associations (Expr));
5652 while Present (Assoc) loop
5653 Choice := First (Choices (Assoc));
5654 while Present (Choice) loop
5655 Rewrite_Range (Rec_Typ, Choice);
5657 Next (Choice);
5658 end loop;
5660 Next (Assoc);
5661 end loop;
5662 end if;
5663 end if;
5664 end;
5665 end if;
5667 Add_Association
5668 (Component => Component,
5669 Expr => Expr,
5670 Assoc_List => New_Assoc_List);
5671 Set_Has_Self_Reference (N);
5673 elsif Needs_Simple_Initialization (Ctyp) then
5674 Add_Association
5675 (Component => Component,
5676 Expr => Empty,
5677 Assoc_List => New_Assoc_List,
5678 Is_Box_Present => True);
5680 elsif Has_Non_Null_Base_Init_Proc (Ctyp)
5681 or else not Expander_Active
5682 then
5683 if Is_Record_Type (Ctyp)
5684 and then Has_Discriminants (Ctyp)
5685 and then not Is_Private_Type (Ctyp)
5686 then
5687 -- We build a partially initialized aggregate with the
5688 -- values of the discriminants and box initialization
5689 -- for the rest, if other components are present.
5691 -- The type of the aggregate is the known subtype of
5692 -- the component. The capture of discriminants must be
5693 -- recursive because subcomponents may be constrained
5694 -- (transitively) by discriminants of enclosing types.
5695 -- For a private type with discriminants, a call to the
5696 -- initialization procedure will be generated, and no
5697 -- subaggregate is needed.
5699 Capture_Discriminants : declare
5700 Loc : constant Source_Ptr := Sloc (N);
5701 Expr : Node_Id;
5703 begin
5704 Expr := Make_Aggregate (Loc, No_List, New_List);
5705 Set_Etype (Expr, Ctyp);
5707 -- If the enclosing type has discriminants, they have
5708 -- been collected in the aggregate earlier, and they
5709 -- may appear as constraints of subcomponents.
5711 -- Similarly if this component has discriminants, they
5712 -- might in turn be propagated to their components.
5714 if Has_Discriminants (Typ) then
5715 Add_Discriminant_Values (Expr, New_Assoc_List);
5716 Propagate_Discriminants (Expr, New_Assoc_List);
5718 elsif Has_Discriminants (Ctyp) then
5719 Add_Discriminant_Values
5720 (Expr, Component_Associations (Expr));
5721 Propagate_Discriminants
5722 (Expr, Component_Associations (Expr));
5724 Build_Constrained_Itype
5725 (Expr, Ctyp, Component_Associations (Expr));
5727 else
5728 declare
5729 Comp : Entity_Id;
5731 begin
5732 -- If the type has additional components, create
5733 -- an OTHERS box association for them.
5735 Comp := First_Component (Ctyp);
5736 while Present (Comp) loop
5737 if Ekind (Comp) = E_Component then
5738 if not Is_Record_Type (Etype (Comp)) then
5739 Append_To
5740 (Component_Associations (Expr),
5741 Make_Component_Association (Loc,
5742 Choices =>
5743 New_List (
5744 Make_Others_Choice (Loc)),
5745 Expression => Empty,
5746 Box_Present => True));
5747 end if;
5749 exit;
5750 end if;
5752 Next_Component (Comp);
5753 end loop;
5754 end;
5755 end if;
5757 Add_Association
5758 (Component => Component,
5759 Expr => Expr,
5760 Assoc_List => New_Assoc_List);
5761 end Capture_Discriminants;
5763 -- Otherwise the component type is not a record, or it has
5764 -- not discriminants, or it is private.
5766 else
5767 Add_Association
5768 (Component => Component,
5769 Expr => Empty,
5770 Assoc_List => New_Assoc_List,
5771 Is_Box_Present => True);
5772 end if;
5774 -- Otherwise we only need to resolve the expression if the
5775 -- component has partially initialized values (required to
5776 -- expand the corresponding assignments and run-time checks).
5778 elsif Present (Expr)
5779 and then Is_Partially_Initialized_Type (Ctyp)
5780 then
5781 Resolve_Aggr_Expr (Expr, Component);
5782 end if;
5783 end Check_Box_Component;
5785 elsif No (Expr) then
5787 -- Ignore hidden components associated with the position of the
5788 -- interface tags: these are initialized dynamically.
5790 if No (Related_Type (Component)) then
5791 Error_Msg_NE
5792 ("no value supplied for component &!", N, Component);
5793 end if;
5795 else
5796 Resolve_Aggr_Expr (Expr, Component);
5797 end if;
5799 Next_Elmt (Component_Elmt);
5800 end loop;
5802 -- STEP 7: check for invalid components + check type in choice list
5804 Step_7 : declare
5805 Assoc : Node_Id;
5806 New_Assoc : Node_Id;
5808 Selectr : Node_Id;
5809 -- Selector name
5811 Typech : Entity_Id;
5812 -- Type of first component in choice list
5814 begin
5815 if Present (Component_Associations (N)) then
5816 Assoc := First (Component_Associations (N));
5817 else
5818 Assoc := Empty;
5819 end if;
5821 Verification : while Present (Assoc) loop
5822 Selectr := First (Choices (Assoc));
5823 Typech := Empty;
5825 if Nkind (Selectr) = N_Others_Choice then
5827 -- Ada 2005 (AI-287): others choice may have expression or box
5829 if No (Others_Etype) and then Others_Box = 0 then
5830 Error_Msg_N
5831 ("OTHERS must represent at least one component", Selectr);
5833 elsif Others_Box = 1 and then Warn_On_Redundant_Constructs then
5834 Error_Msg_N ("OTHERS choice is redundant?", Box_Node);
5835 Error_Msg_N
5836 ("\previous choices cover all components?", Box_Node);
5837 end if;
5839 exit Verification;
5840 end if;
5842 while Present (Selectr) loop
5843 New_Assoc := First (New_Assoc_List);
5844 while Present (New_Assoc) loop
5845 Component := First (Choices (New_Assoc));
5847 if Chars (Selectr) = Chars (Component) then
5848 if Style_Check then
5849 Check_Identifier (Selectr, Entity (Component));
5850 end if;
5852 exit;
5853 end if;
5855 Next (New_Assoc);
5856 end loop;
5858 -- If no association, this is not a legal component of the type
5859 -- in question, unless its association is provided with a box.
5861 if No (New_Assoc) then
5862 if Box_Present (Parent (Selectr)) then
5864 -- This may still be a bogus component with a box. Scan
5865 -- list of components to verify that a component with
5866 -- that name exists.
5868 declare
5869 C : Entity_Id;
5871 begin
5872 C := First_Component (Typ);
5873 while Present (C) loop
5874 if Chars (C) = Chars (Selectr) then
5876 -- If the context is an extension aggregate,
5877 -- the component must not be inherited from
5878 -- the ancestor part of the aggregate.
5880 if Nkind (N) /= N_Extension_Aggregate
5881 or else
5882 Scope (Original_Record_Component (C)) /=
5883 Etype (Ancestor_Part (N))
5884 then
5885 exit;
5886 end if;
5887 end if;
5889 Next_Component (C);
5890 end loop;
5892 if No (C) then
5893 Error_Msg_Node_2 := Typ;
5894 Error_Msg_N ("& is not a component of}", Selectr);
5895 end if;
5896 end;
5898 elsif Chars (Selectr) /= Name_uTag
5899 and then Chars (Selectr) /= Name_uParent
5900 then
5901 if not Has_Discriminants (Typ) then
5902 Error_Msg_Node_2 := Typ;
5903 Error_Msg_N ("& is not a component of}", Selectr);
5904 else
5905 Error_Msg_N
5906 ("& is not a component of the aggregate subtype",
5907 Selectr);
5908 end if;
5910 Check_Misspelled_Component (Components, Selectr);
5911 end if;
5913 elsif No (Typech) then
5914 Typech := Base_Type (Etype (Component));
5916 -- AI05-0199: In Ada 2012, several components of anonymous
5917 -- access types can appear in a choice list, as long as the
5918 -- designated types match.
5920 elsif Typech /= Base_Type (Etype (Component)) then
5921 if Ada_Version >= Ada_2012
5922 and then Ekind (Typech) = E_Anonymous_Access_Type
5923 and then
5924 Ekind (Etype (Component)) = E_Anonymous_Access_Type
5925 and then Base_Type (Designated_Type (Typech)) =
5926 Base_Type (Designated_Type (Etype (Component)))
5927 and then
5928 Subtypes_Statically_Match (Typech, (Etype (Component)))
5929 then
5930 null;
5932 elsif not Box_Present (Parent (Selectr)) then
5933 Error_Msg_N
5934 ("components in choice list must have same type",
5935 Selectr);
5936 end if;
5937 end if;
5939 Next (Selectr);
5940 end loop;
5942 Next (Assoc);
5943 end loop Verification;
5944 end Step_7;
5946 -- STEP 8: replace the original aggregate
5948 Step_8 : declare
5949 New_Aggregate : constant Node_Id := New_Copy (N);
5951 begin
5952 Set_Expressions (New_Aggregate, No_List);
5953 Set_Etype (New_Aggregate, Etype (N));
5954 Set_Component_Associations (New_Aggregate, New_Assoc_List);
5955 Set_Check_Actuals (New_Aggregate, Check_Actuals (N));
5957 Rewrite (N, New_Aggregate);
5958 end Step_8;
5960 -- Check the dimensions of the components in the record aggregate
5962 Analyze_Dimension_Extension_Or_Record_Aggregate (N);
5963 end Resolve_Record_Aggregate;
5965 -----------------------------
5966 -- Check_Can_Never_Be_Null --
5967 -----------------------------
5969 procedure Check_Can_Never_Be_Null (Typ : Entity_Id; Expr : Node_Id) is
5970 Comp_Typ : Entity_Id;
5972 begin
5973 pragma Assert
5974 (Ada_Version >= Ada_2005
5975 and then Present (Expr)
5976 and then Known_Null (Expr));
5978 case Ekind (Typ) is
5979 when E_Array_Type =>
5980 Comp_Typ := Component_Type (Typ);
5982 when E_Component
5983 | E_Discriminant
5985 Comp_Typ := Etype (Typ);
5987 when others =>
5988 return;
5989 end case;
5991 if Can_Never_Be_Null (Comp_Typ) then
5993 -- Here we know we have a constraint error. Note that we do not use
5994 -- Apply_Compile_Time_Constraint_Error here to the Expr, which might
5995 -- seem the more natural approach. That's because in some cases the
5996 -- components are rewritten, and the replacement would be missed.
5997 -- We do not mark the whole aggregate as raising a constraint error,
5998 -- because the association may be a null array range.
6000 Error_Msg_N
6001 ("(Ada 2005) NULL not allowed in null-excluding component??", Expr);
6002 Error_Msg_N
6003 ("\Constraint_Error will be raised at run time??", Expr);
6005 Rewrite (Expr,
6006 Make_Raise_Constraint_Error
6007 (Sloc (Expr), Reason => CE_Access_Check_Failed));
6008 Set_Etype (Expr, Comp_Typ);
6009 Set_Analyzed (Expr);
6010 end if;
6011 end Check_Can_Never_Be_Null;
6013 ---------------------
6014 -- Sort_Case_Table --
6015 ---------------------
6017 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is
6018 U : constant Int := Case_Table'Last;
6019 K : Int;
6020 J : Int;
6021 T : Case_Bounds;
6023 begin
6024 K := 1;
6025 while K < U loop
6026 T := Case_Table (K + 1);
6028 J := K + 1;
6029 while J > 1
6030 and then Expr_Value (Case_Table (J - 1).Lo) > Expr_Value (T.Lo)
6031 loop
6032 Case_Table (J) := Case_Table (J - 1);
6033 J := J - 1;
6034 end loop;
6036 Case_Table (J) := T;
6037 K := K + 1;
6038 end loop;
6039 end Sort_Case_Table;
6041 end Sem_Aggr;