PR27116, Spelling errors found by Debian style checker
[official-gcc.git] / gcc / ada / sem_aggr.adb
blob364217d03db4e166f7f406dc78a0caac19998019
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ A G G R --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2023, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Einfo; use Einfo;
30 with Einfo.Entities; use Einfo.Entities;
31 with Einfo.Utils; use Einfo.Utils;
32 with Elists; use Elists;
33 with Errout; use Errout;
34 with Expander; use Expander;
35 with Exp_Ch6; use Exp_Ch6;
36 with Exp_Tss; use Exp_Tss;
37 with Exp_Util; use Exp_Util;
38 with Freeze; use Freeze;
39 with Itypes; use Itypes;
40 with Lib; use Lib;
41 with Lib.Xref; use Lib.Xref;
42 with Namet; use Namet;
43 with Namet.Sp; use Namet.Sp;
44 with Nmake; use Nmake;
45 with Nlists; use Nlists;
46 with Opt; use Opt;
47 with Restrict; use Restrict;
48 with Rident; use Rident;
49 with Sem; use Sem;
50 with Sem_Aux; use Sem_Aux;
51 with Sem_Case; use Sem_Case;
52 with Sem_Cat; use Sem_Cat;
53 with Sem_Ch3; use Sem_Ch3;
54 with Sem_Ch8; use Sem_Ch8;
55 with Sem_Ch13; use Sem_Ch13;
56 with Sem_Dim; use Sem_Dim;
57 with Sem_Eval; use Sem_Eval;
58 with Sem_Res; use Sem_Res;
59 with Sem_Util; use Sem_Util;
60 with Sem_Type; use Sem_Type;
61 with Sem_Warn; use Sem_Warn;
62 with Sinfo; use Sinfo;
63 with Sinfo.Nodes; use Sinfo.Nodes;
64 with Sinfo.Utils; use Sinfo.Utils;
65 with Snames; use Snames;
66 with Stringt; use Stringt;
67 with Stand; use Stand;
68 with Style; use Style;
69 with Targparm; use Targparm;
70 with Tbuild; use Tbuild;
71 with Ttypes; use Ttypes;
72 with Uintp; use Uintp;
73 with Warnsw; use Warnsw;
75 package body Sem_Aggr is
77 type Case_Bounds is record
78 Lo : Node_Id;
79 -- Low bound of choice. Once we sort the Case_Table, then entries
80 -- will be in order of ascending Choice_Lo values.
82 Hi : Node_Id;
83 -- High Bound of choice. The sort does not pay any attention to the
84 -- high bound, so choices 1 .. 4 and 1 .. 5 could be in either order.
86 Highest : Uint;
87 -- If there are duplicates or missing entries, then in the sorted
88 -- table, this records the highest value among Choice_Hi values
89 -- seen so far, including this entry.
91 Choice : Node_Id;
92 -- The node of the choice
93 end record;
95 type Case_Table_Type is array (Pos range <>) of Case_Bounds;
96 -- Table type used by Check_Case_Choices procedure
98 -----------------------
99 -- Local Subprograms --
100 -----------------------
102 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type);
103 -- Sort the Case Table using the Lower Bound of each Choice as the key. A
104 -- simple insertion sort is used since the choices in a case statement will
105 -- usually be in near sorted order.
107 procedure Check_Can_Never_Be_Null (Typ : Entity_Id; Expr : Node_Id);
108 -- Ada 2005 (AI-231): Check bad usage of null for a component for which
109 -- null exclusion (NOT NULL) is specified. Typ can be an E_Array_Type for
110 -- the array case (the component type of the array will be used) or an
111 -- E_Component/E_Discriminant entity in the record case, in which case the
112 -- type of the component will be used for the test. If Typ is any other
113 -- kind of entity, the call is ignored. Expr is the component node in the
114 -- aggregate which is known to have a null value. A warning message will be
115 -- issued if the component is null excluding.
117 -- It would be better to pass the proper type for Typ ???
119 procedure Check_Expr_OK_In_Limited_Aggregate (Expr : Node_Id);
120 -- Check that Expr is either not limited or else is one of the cases of
121 -- expressions allowed for a limited component association (namely, an
122 -- aggregate, function call, or <> notation). Report error for violations.
123 -- Expression is also OK in an instance or inlining context, because we
124 -- have already preanalyzed and it is known to be type correct.
126 ------------------------------------------------------
127 -- Subprograms used for RECORD AGGREGATE Processing --
128 ------------------------------------------------------
130 procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id);
131 -- This procedure performs all the semantic checks required for record
132 -- aggregates. Note that for aggregates analysis and resolution go
133 -- hand in hand. Aggregate analysis has been delayed up to here and
134 -- it is done while resolving the aggregate.
136 -- N is the N_Aggregate node.
137 -- Typ is the record type for the aggregate resolution
139 -- While performing the semantic checks, this procedure builds a new
140 -- Component_Association_List where each record field appears alone in a
141 -- Component_Choice_List along with its corresponding expression. The
142 -- record fields in the Component_Association_List appear in the same order
143 -- in which they appear in the record type Typ.
145 -- Once this new Component_Association_List is built and all the semantic
146 -- checks performed, the original aggregate subtree is replaced with the
147 -- new named record aggregate just built. This new record aggregate has no
148 -- positional associations, so its Expressions field is set to No_List.
149 -- Note that subtree substitution is performed with Rewrite so as to be
150 -- able to retrieve the original aggregate.
152 -- The aggregate subtree manipulation performed by Resolve_Record_Aggregate
153 -- yields the aggregate format expected by Gigi. Typically, this kind of
154 -- tree manipulations are done in the expander. However, because the
155 -- semantic checks that need to be performed on record aggregates really go
156 -- hand in hand with the record aggregate normalization, the aggregate
157 -- subtree transformation is performed during resolution rather than
158 -- expansion. Had we decided otherwise we would have had to duplicate most
159 -- of the code in the expansion procedure Expand_Record_Aggregate. Note,
160 -- however, that all the expansion concerning aggregates for tagged records
161 -- is done in Expand_Record_Aggregate.
163 -- The algorithm of Resolve_Record_Aggregate proceeds as follows:
165 -- 1. Make sure that the record type against which the record aggregate
166 -- has to be resolved is not abstract. Furthermore if the type is a
167 -- null aggregate make sure the input aggregate N is also null.
169 -- 2. Verify that the structure of the aggregate is that of a record
170 -- aggregate. Specifically, look for component associations and ensure
171 -- that each choice list only has identifiers or the N_Others_Choice
172 -- node. Also make sure that if present, the N_Others_Choice occurs
173 -- last and by itself.
175 -- 3. If Typ contains discriminants, the values for each discriminant is
176 -- looked for. If the record type Typ has variants, we check that the
177 -- expressions corresponding to each discriminant ruling the (possibly
178 -- nested) variant parts of Typ, are static. This allows us to determine
179 -- the variant parts to which the rest of the aggregate must conform.
180 -- The names of discriminants with their values are saved in a new
181 -- association list, New_Assoc_List which is later augmented with the
182 -- names and values of the remaining components in the record type.
184 -- During this phase we also make sure that every discriminant is
185 -- assigned exactly one value. Note that when several values for a given
186 -- discriminant are found, semantic processing continues looking for
187 -- further errors. In this case it's the first discriminant value found
188 -- which we will be recorded.
190 -- IMPORTANT NOTE: For derived tagged types this procedure expects
191 -- First_Discriminant and Next_Discriminant to give the correct list
192 -- of discriminants, in the correct order.
194 -- 4. After all the discriminant values have been gathered, we can set the
195 -- Etype of the record aggregate. If Typ contains no discriminants this
196 -- is straightforward: the Etype of N is just Typ, otherwise a new
197 -- implicit constrained subtype of Typ is built to be the Etype of N.
199 -- 5. Gather the remaining record components according to the discriminant
200 -- values. This involves recursively traversing the record type
201 -- structure to see what variants are selected by the given discriminant
202 -- values. This processing is a little more convoluted if Typ is a
203 -- derived tagged types since we need to retrieve the record structure
204 -- of all the ancestors of Typ.
206 -- 6. After gathering the record components we look for their values in the
207 -- record aggregate and emit appropriate error messages should we not
208 -- find such values or should they be duplicated.
210 -- 7. We then make sure no illegal component names appear in the record
211 -- aggregate and make sure that the type of the record components
212 -- appearing in a same choice list is the same. Finally we ensure that
213 -- the others choice, if present, is used to provide the value of at
214 -- least a record component.
216 -- 8. The original aggregate node is replaced with the new named aggregate
217 -- built in steps 3 through 6, as explained earlier.
219 -- Given the complexity of record aggregate resolution, the primary goal of
220 -- this routine is clarity and simplicity rather than execution and storage
221 -- efficiency. If there are only positional components in the aggregate the
222 -- running time is linear. If there are associations the running time is
223 -- still linear as long as the order of the associations is not too far off
224 -- the order of the components in the record type. If this is not the case
225 -- the running time is at worst quadratic in the size of the association
226 -- list.
228 procedure Check_Misspelled_Component
229 (Elements : Elist_Id;
230 Component : Node_Id);
231 -- Give possible misspelling diagnostic if Component is likely to be a
232 -- misspelling of one of the components of the Assoc_List. This is called
233 -- by Resolve_Aggr_Expr after producing an invalid component error message.
235 -----------------------------------------------------
236 -- Subprograms used for ARRAY AGGREGATE Processing --
237 -----------------------------------------------------
239 function Resolve_Array_Aggregate
240 (N : Node_Id;
241 Index : Node_Id;
242 Index_Constr : Node_Id;
243 Component_Typ : Entity_Id;
244 Others_Allowed : Boolean) return Boolean;
245 -- This procedure performs the semantic checks for an array aggregate.
246 -- True is returned if the aggregate resolution succeeds.
248 -- The procedure works by recursively checking each nested aggregate.
249 -- Specifically, after checking a sub-aggregate nested at the i-th level
250 -- we recursively check all the subaggregates at the i+1-st level (if any).
251 -- Note that aggregates analysis and resolution go hand in hand.
252 -- Aggregate analysis has been delayed up to here and it is done while
253 -- resolving the aggregate.
255 -- N is the current N_Aggregate node to be checked.
257 -- Index is the index node corresponding to the array sub-aggregate that
258 -- we are currently checking (RM 4.3.3 (8)). Its Etype is the
259 -- corresponding index type (or subtype).
261 -- Index_Constr is the node giving the applicable index constraint if
262 -- any (RM 4.3.3 (10)). It "is a constraint provided by certain
263 -- contexts [...] that can be used to determine the bounds of the array
264 -- value specified by the aggregate". If Others_Allowed below is False
265 -- there is no applicable index constraint and this node is set to Index.
267 -- Component_Typ is the array component type.
269 -- Others_Allowed indicates whether an others choice is allowed
270 -- in the context where the top-level aggregate appeared.
272 -- The algorithm of Resolve_Array_Aggregate proceeds as follows:
274 -- 1. Make sure that the others choice, if present, is by itself and
275 -- appears last in the sub-aggregate. Check that we do not have
276 -- positional and named components in the array sub-aggregate (unless
277 -- the named association is an others choice). Finally if an others
278 -- choice is present, make sure it is allowed in the aggregate context.
280 -- 2. If the array sub-aggregate contains discrete_choices:
282 -- (A) Verify their validity. Specifically verify that:
284 -- (a) If a null range is present it must be the only possible
285 -- choice in the array aggregate.
287 -- (b) Ditto for a non static range.
289 -- (c) Ditto for a non static expression.
291 -- In addition this step analyzes and resolves each discrete_choice,
292 -- making sure that its type is the type of the corresponding Index.
293 -- If we are not at the lowest array aggregate level (in the case of
294 -- multi-dimensional aggregates) then invoke Resolve_Array_Aggregate
295 -- recursively on each component expression. Otherwise, resolve the
296 -- bottom level component expressions against the expected component
297 -- type ONLY IF the component corresponds to a single discrete choice
298 -- which is not an others choice (to see why read the DELAYED
299 -- COMPONENT RESOLUTION below).
301 -- (B) Determine the bounds of the sub-aggregate and lowest and
302 -- highest choice values.
304 -- 3. For positional aggregates:
306 -- (A) Loop over the component expressions either recursively invoking
307 -- Resolve_Array_Aggregate on each of these for multi-dimensional
308 -- array aggregates or resolving the bottom level component
309 -- expressions against the expected component type.
311 -- (B) Determine the bounds of the positional sub-aggregates.
313 -- 4. Try to determine statically whether the evaluation of the array
314 -- sub-aggregate raises Constraint_Error. If yes emit proper
315 -- warnings. The precise checks are the following:
317 -- (A) Check that the index range defined by aggregate bounds is
318 -- compatible with corresponding index subtype.
319 -- We also check against the base type. In fact it could be that
320 -- Low/High bounds of the base type are static whereas those of
321 -- the index subtype are not. Thus if we can statically catch
322 -- a problem with respect to the base type we are guaranteed
323 -- that the same problem will arise with the index subtype
325 -- (B) If we are dealing with a named aggregate containing an others
326 -- choice and at least one discrete choice then make sure the range
327 -- specified by the discrete choices does not overflow the
328 -- aggregate bounds. We also check against the index type and base
329 -- type bounds for the same reasons given in (A).
331 -- (C) If we are dealing with a positional aggregate with an others
332 -- choice make sure the number of positional elements specified
333 -- does not overflow the aggregate bounds. We also check against
334 -- the index type and base type bounds as mentioned in (A).
336 -- Finally construct an N_Range node giving the sub-aggregate bounds.
337 -- Set the Aggregate_Bounds field of the sub-aggregate to be this
338 -- N_Range. The routine Array_Aggr_Subtype below uses such N_Ranges
339 -- to build the appropriate aggregate subtype. Aggregate_Bounds
340 -- information is needed during expansion.
342 -- DELAYED COMPONENT RESOLUTION: The resolution of bottom level component
343 -- expressions in an array aggregate may call Duplicate_Subexpr or some
344 -- other routine that inserts code just outside the outermost aggregate.
345 -- If the array aggregate contains discrete choices or an others choice,
346 -- this may be wrong. Consider for instance the following example.
348 -- type Rec is record
349 -- V : Integer := 0;
350 -- end record;
352 -- type Acc_Rec is access Rec;
353 -- Arr : array (1..3) of Acc_Rec := (1 .. 3 => new Rec);
355 -- Then the transformation of "new Rec" that occurs during resolution
356 -- entails the following code modifications
358 -- P7b : constant Acc_Rec := new Rec;
359 -- RecIP (P7b.all);
360 -- Arr : array (1..3) of Acc_Rec := (1 .. 3 => P7b);
362 -- This code transformation is clearly wrong, since we need to call
363 -- "new Rec" for each of the 3 array elements. To avoid this problem we
364 -- delay resolution of the components of non positional array aggregates
365 -- to the expansion phase. As an optimization, if the discrete choice
366 -- specifies a single value we do not delay resolution.
368 function Array_Aggr_Subtype (N : Node_Id; Typ : Entity_Id) return Entity_Id;
369 -- This routine returns the type or subtype of an array aggregate.
371 -- N is the array aggregate node whose type we return.
373 -- Typ is the context type in which N occurs.
375 -- This routine creates an implicit array subtype whose bounds are
376 -- those defined by the aggregate. When this routine is invoked
377 -- Resolve_Array_Aggregate has already processed aggregate N. Thus the
378 -- Aggregate_Bounds of each sub-aggregate, is an N_Range node giving the
379 -- sub-aggregate bounds. When building the aggregate itype, this function
380 -- traverses the array aggregate N collecting such Aggregate_Bounds and
381 -- constructs the proper array aggregate itype.
383 -- Note that in the case of multidimensional aggregates each inner
384 -- sub-aggregate corresponding to a given array dimension, may provide a
385 -- different bounds. If it is possible to determine statically that
386 -- some sub-aggregates corresponding to the same index do not have the
387 -- same bounds, then a warning is emitted. If such check is not possible
388 -- statically (because some sub-aggregate bounds are dynamic expressions)
389 -- then this job is left to the expander. In all cases the particular
390 -- bounds that this function will chose for a given dimension is the first
391 -- N_Range node for a sub-aggregate corresponding to that dimension.
393 -- Note that the Raises_Constraint_Error flag of an array aggregate
394 -- whose evaluation is determined to raise CE by Resolve_Array_Aggregate,
395 -- is set in Resolve_Array_Aggregate but the aggregate is not
396 -- immediately replaced with a raise CE. In fact, Array_Aggr_Subtype must
397 -- first construct the proper itype for the aggregate (Gigi needs
398 -- this). After constructing the proper itype we will eventually replace
399 -- the top-level aggregate with a raise CE (done in Resolve_Aggregate).
400 -- Of course in cases such as:
402 -- type Arr is array (integer range <>) of Integer;
403 -- A : Arr := (positive range -1 .. 2 => 0);
405 -- The bounds of the aggregate itype are cooked up to look reasonable
406 -- (in this particular case the bounds will be 1 .. 2).
408 procedure Make_String_Into_Aggregate (N : Node_Id);
409 -- A string literal can appear in a context in which a one dimensional
410 -- array of characters is expected. This procedure simply rewrites the
411 -- string as an aggregate, prior to resolution.
413 function Resolve_Null_Array_Aggregate (N : Node_Id) return Boolean;
414 -- For the Ada 2022 construct, build a subtype with a null range for each
415 -- dimension, using the bounds from the context subtype (if the subtype
416 -- is constrained). If the subtype is unconstrained, then the bounds
417 -- are determined in much the same way as the bounds for a null string
418 -- literal with no applicable index constraint.
420 ---------------------------------
421 -- Delta aggregate processing --
422 ---------------------------------
424 procedure Resolve_Delta_Array_Aggregate (N : Node_Id; Typ : Entity_Id);
425 procedure Resolve_Delta_Record_Aggregate (N : Node_Id; Typ : Entity_Id);
427 ------------------------
428 -- Array_Aggr_Subtype --
429 ------------------------
431 function Array_Aggr_Subtype
432 (N : Node_Id;
433 Typ : Entity_Id) return Entity_Id
435 Aggr_Dimension : constant Pos := Number_Dimensions (Typ);
436 -- Number of aggregate index dimensions
438 Aggr_Range : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
439 -- Constrained N_Range of each index dimension in our aggregate itype
441 Aggr_Low : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
442 Aggr_High : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
443 -- Low and High bounds for each index dimension in our aggregate itype
445 Is_Fully_Positional : Boolean := True;
447 procedure Collect_Aggr_Bounds (N : Node_Id; Dim : Pos);
448 -- N is an array (sub-)aggregate. Dim is the dimension corresponding
449 -- to (sub-)aggregate N. This procedure collects and removes the side
450 -- effects of the constrained N_Range nodes corresponding to each index
451 -- dimension of our aggregate itype. These N_Range nodes are collected
452 -- in Aggr_Range above.
454 -- Likewise collect in Aggr_Low & Aggr_High above the low and high
455 -- bounds of each index dimension. If, when collecting, two bounds
456 -- corresponding to the same dimension are static and found to differ,
457 -- then emit a warning, and mark N as raising Constraint_Error.
459 -------------------------
460 -- Collect_Aggr_Bounds --
461 -------------------------
463 procedure Collect_Aggr_Bounds (N : Node_Id; Dim : Pos) is
464 This_Range : constant Node_Id := Aggregate_Bounds (N);
465 -- The aggregate range node of this specific sub-aggregate
467 This_Low : constant Node_Id := Low_Bound (This_Range);
468 This_High : constant Node_Id := High_Bound (This_Range);
469 -- The aggregate bounds of this specific sub-aggregate
471 Assoc : Node_Id;
472 Expr : Node_Id;
474 begin
475 Remove_Side_Effects (This_Low, Variable_Ref => True);
476 Remove_Side_Effects (This_High, Variable_Ref => True);
478 -- Collect the first N_Range for a given dimension that you find.
479 -- For a given dimension they must be all equal anyway.
481 if No (Aggr_Range (Dim)) then
482 Aggr_Low (Dim) := This_Low;
483 Aggr_High (Dim) := This_High;
484 Aggr_Range (Dim) := This_Range;
486 else
487 if Compile_Time_Known_Value (This_Low) then
488 if not Compile_Time_Known_Value (Aggr_Low (Dim)) then
489 Aggr_Low (Dim) := This_Low;
491 elsif Expr_Value (This_Low) /= Expr_Value (Aggr_Low (Dim)) then
492 Set_Raises_Constraint_Error (N);
493 Error_Msg_Warn := SPARK_Mode /= On;
494 Error_Msg_N ("sub-aggregate low bound mismatch<<", N);
495 Error_Msg_N ("\Constraint_Error [<<", N);
496 end if;
497 end if;
499 if Compile_Time_Known_Value (This_High) then
500 if not Compile_Time_Known_Value (Aggr_High (Dim)) then
501 Aggr_High (Dim) := This_High;
503 elsif
504 Expr_Value (This_High) /= Expr_Value (Aggr_High (Dim))
505 then
506 Set_Raises_Constraint_Error (N);
507 Error_Msg_Warn := SPARK_Mode /= On;
508 Error_Msg_N ("sub-aggregate high bound mismatch<<", N);
509 Error_Msg_N ("\Constraint_Error [<<", N);
510 end if;
511 end if;
512 end if;
514 if Dim < Aggr_Dimension then
516 -- Process positional components
518 if Present (Expressions (N)) then
519 Expr := First (Expressions (N));
520 while Present (Expr) loop
521 Collect_Aggr_Bounds (Expr, Dim + 1);
522 Next (Expr);
523 end loop;
524 end if;
526 -- Process component associations
528 if Present (Component_Associations (N)) then
529 Is_Fully_Positional := False;
531 Assoc := First (Component_Associations (N));
532 while Present (Assoc) loop
533 Expr := Expression (Assoc);
534 Collect_Aggr_Bounds (Expr, Dim + 1);
535 Next (Assoc);
536 end loop;
537 end if;
538 end if;
539 end Collect_Aggr_Bounds;
541 -- Array_Aggr_Subtype variables
543 Itype : Entity_Id;
544 -- The final itype of the overall aggregate
546 Index_Constraints : constant List_Id := New_List;
547 -- The list of index constraints of the aggregate itype
549 -- Start of processing for Array_Aggr_Subtype
551 begin
552 -- Make sure that the list of index constraints is properly attached to
553 -- the tree, and then collect the aggregate bounds.
555 -- If no aggregaate bounds have been set, this is an aggregate with
556 -- iterator specifications and a dynamic size to be determined by
557 -- first pass of expanded code.
559 if No (Aggregate_Bounds (N)) then
560 return Typ;
561 end if;
563 Set_Parent (Index_Constraints, N);
565 -- When resolving a null aggregate we created a list of aggregate bounds
566 -- for the consecutive dimensions. The bounds for the first dimension
567 -- are attached as the Aggregate_Bounds of the aggregate node.
569 if Is_Null_Aggregate (N) then
570 declare
571 This_Range : Node_Id := Aggregate_Bounds (N);
572 begin
573 for J in 1 .. Aggr_Dimension loop
574 Aggr_Range (J) := This_Range;
575 Next_Index (This_Range);
577 -- Remove bounds from the list, so they can be reattached as
578 -- the First_Index/Next_Index again by the code that also
579 -- handles non-null aggregates.
581 Remove (Aggr_Range (J));
582 end loop;
583 end;
584 else
585 Collect_Aggr_Bounds (N, 1);
586 end if;
588 -- Build the list of constrained indexes of our aggregate itype
590 for J in 1 .. Aggr_Dimension loop
591 Create_Index : declare
592 Index_Base : constant Entity_Id :=
593 Base_Type (Etype (Aggr_Range (J)));
594 Index_Typ : Entity_Id;
596 begin
597 -- Construct the Index subtype, and associate it with the range
598 -- construct that generates it.
600 Index_Typ :=
601 Create_Itype (Subtype_Kind (Ekind (Index_Base)), Aggr_Range (J));
603 Set_Etype (Index_Typ, Index_Base);
605 if Is_Character_Type (Index_Base) then
606 Set_Is_Character_Type (Index_Typ);
607 end if;
609 Set_Size_Info (Index_Typ, (Index_Base));
610 Set_RM_Size (Index_Typ, RM_Size (Index_Base));
611 Set_First_Rep_Item (Index_Typ, First_Rep_Item (Index_Base));
612 Set_Scalar_Range (Index_Typ, Aggr_Range (J));
614 if Is_Discrete_Or_Fixed_Point_Type (Index_Typ) then
615 Set_RM_Size (Index_Typ, UI_From_Int (Minimum_Size (Index_Typ)));
616 end if;
618 Set_Etype (Aggr_Range (J), Index_Typ);
620 Append (Aggr_Range (J), To => Index_Constraints);
621 end Create_Index;
622 end loop;
624 -- Now build the Itype
626 Itype := Create_Itype (E_Array_Subtype, N);
628 Set_First_Rep_Item (Itype, First_Rep_Item (Typ));
629 Set_Convention (Itype, Convention (Typ));
630 Set_Depends_On_Private (Itype, Has_Private_Component (Typ));
631 Set_Etype (Itype, Base_Type (Typ));
632 Set_Has_Alignment_Clause (Itype, Has_Alignment_Clause (Typ));
633 Set_Is_Aliased (Itype, Is_Aliased (Typ));
634 Set_Is_Independent (Itype, Is_Independent (Typ));
635 Set_Depends_On_Private (Itype, Depends_On_Private (Typ));
637 Copy_Suppress_Status (Index_Check, Typ, Itype);
638 Copy_Suppress_Status (Length_Check, Typ, Itype);
640 Set_First_Index (Itype, First (Index_Constraints));
641 Set_Is_Constrained (Itype, True);
642 Set_Is_Internal (Itype, True);
644 if Has_Predicates (Typ) then
645 Set_Has_Predicates (Itype);
647 -- If the base type has a predicate, capture the predicated parent
648 -- or the existing predicate function for SPARK use.
650 if Present (Predicate_Function (Typ)) then
651 Set_Predicate_Function (Itype, Predicate_Function (Typ));
653 elsif Is_Itype (Typ) then
654 Set_Predicated_Parent (Itype, Predicated_Parent (Typ));
656 else
657 Set_Predicated_Parent (Itype, Typ);
658 end if;
659 end if;
661 -- A simple optimization: purely positional aggregates of static
662 -- components should be passed to gigi unexpanded whenever possible, and
663 -- regardless of the staticness of the bounds themselves. Subsequent
664 -- checks in exp_aggr verify that type is not packed, etc.
666 Set_Size_Known_At_Compile_Time
667 (Itype,
668 Is_Fully_Positional
669 and then Comes_From_Source (N)
670 and then Size_Known_At_Compile_Time (Component_Type (Typ)));
672 -- We always need a freeze node for a packed array subtype, so that we
673 -- can build the Packed_Array_Impl_Type corresponding to the subtype. If
674 -- expansion is disabled, the packed array subtype is not built, and we
675 -- must not generate a freeze node for the type, or else it will appear
676 -- incomplete to gigi.
678 if Is_Packed (Itype)
679 and then not In_Spec_Expression
680 and then Expander_Active
681 then
682 Freeze_Itype (Itype, N);
683 end if;
685 return Itype;
686 end Array_Aggr_Subtype;
688 --------------------------------
689 -- Check_Misspelled_Component --
690 --------------------------------
692 procedure Check_Misspelled_Component
693 (Elements : Elist_Id;
694 Component : Node_Id)
696 Max_Suggestions : constant := 2;
698 Nr_Of_Suggestions : Natural := 0;
699 Suggestion_1 : Entity_Id := Empty;
700 Suggestion_2 : Entity_Id := Empty;
701 Component_Elmt : Elmt_Id;
703 begin
704 -- All the components of List are matched against Component and a count
705 -- is maintained of possible misspellings. When at the end of the
706 -- analysis there are one or two (not more) possible misspellings,
707 -- these misspellings will be suggested as possible corrections.
709 Component_Elmt := First_Elmt (Elements);
710 while Nr_Of_Suggestions <= Max_Suggestions
711 and then Present (Component_Elmt)
712 loop
713 if Is_Bad_Spelling_Of
714 (Chars (Node (Component_Elmt)),
715 Chars (Component))
716 then
717 Nr_Of_Suggestions := Nr_Of_Suggestions + 1;
719 case Nr_Of_Suggestions is
720 when 1 => Suggestion_1 := Node (Component_Elmt);
721 when 2 => Suggestion_2 := Node (Component_Elmt);
722 when others => null;
723 end case;
724 end if;
726 Next_Elmt (Component_Elmt);
727 end loop;
729 -- Report at most two suggestions
731 if Nr_Of_Suggestions = 1 then
732 Error_Msg_NE -- CODEFIX
733 ("\possible misspelling of&", Component, Suggestion_1);
735 elsif Nr_Of_Suggestions = 2 then
736 Error_Msg_Node_2 := Suggestion_2;
737 Error_Msg_NE -- CODEFIX
738 ("\possible misspelling of& or&", Component, Suggestion_1);
739 end if;
740 end Check_Misspelled_Component;
742 ----------------------------------------
743 -- Check_Expr_OK_In_Limited_Aggregate --
744 ----------------------------------------
746 procedure Check_Expr_OK_In_Limited_Aggregate (Expr : Node_Id) is
747 begin
748 if Is_Limited_Type (Etype (Expr))
749 and then Comes_From_Source (Expr)
750 then
751 if In_Instance_Body or else In_Inlined_Body then
752 null;
754 elsif not OK_For_Limited_Init (Etype (Expr), Expr) then
755 Error_Msg_N
756 ("initialization not allowed for limited types", Expr);
757 Explain_Limited_Type (Etype (Expr), Expr);
758 end if;
759 end if;
760 end Check_Expr_OK_In_Limited_Aggregate;
762 -------------------------
763 -- Is_Others_Aggregate --
764 -------------------------
766 function Is_Others_Aggregate (Aggr : Node_Id) return Boolean is
767 Assoc : constant List_Id := Component_Associations (Aggr);
769 begin
770 return No (Expressions (Aggr))
771 and then Nkind (First (Choice_List (First (Assoc)))) = N_Others_Choice;
772 end Is_Others_Aggregate;
774 -------------------------
775 -- Is_Single_Aggregate --
776 -------------------------
778 function Is_Single_Aggregate (Aggr : Node_Id) return Boolean is
779 Assoc : constant List_Id := Component_Associations (Aggr);
781 begin
782 return No (Expressions (Aggr))
783 and then No (Next (First (Assoc)))
784 and then No (Next (First (Choice_List (First (Assoc)))));
785 end Is_Single_Aggregate;
787 -----------------------
788 -- Is_Null_Aggregate --
789 -----------------------
791 function Is_Null_Aggregate (N : Node_Id) return Boolean is
792 begin
793 return Ada_Version >= Ada_2022
794 and then Is_Homogeneous_Aggregate (N)
795 and then Is_Empty_List (Expressions (N))
796 and then Is_Empty_List (Component_Associations (N));
797 end Is_Null_Aggregate;
799 ----------------------------------------
800 -- Is_Null_Array_Aggregate_High_Bound --
801 ----------------------------------------
803 function Is_Null_Array_Aggregate_High_Bound (N : Node_Id) return Boolean is
804 Original_N : constant Node_Id := Original_Node (N);
805 begin
806 return Ada_Version >= Ada_2022
807 and then not Comes_From_Source (Original_N)
808 and then Nkind (Original_N) = N_Attribute_Reference
809 and then
810 Get_Attribute_Id (Attribute_Name (Original_N)) = Attribute_Pred
811 and then Nkind (Parent (N)) in N_Range | N_Op_Le
812 and then not Comes_From_Source (Parent (N));
813 end Is_Null_Array_Aggregate_High_Bound;
815 --------------------------------
816 -- Make_String_Into_Aggregate --
817 --------------------------------
819 procedure Make_String_Into_Aggregate (N : Node_Id) is
820 Exprs : constant List_Id := New_List;
821 Loc : constant Source_Ptr := Sloc (N);
822 Str : constant String_Id := Strval (N);
823 Strlen : constant Nat := String_Length (Str);
824 C : Char_Code;
825 C_Node : Node_Id;
826 New_N : Node_Id;
827 P : Source_Ptr;
829 begin
830 P := Loc + 1;
831 for J in 1 .. Strlen loop
832 C := Get_String_Char (Str, J);
833 Set_Character_Literal_Name (C);
835 C_Node :=
836 Make_Character_Literal (P,
837 Chars => Name_Find,
838 Char_Literal_Value => UI_From_CC (C));
839 Set_Etype (C_Node, Any_Character);
840 Append_To (Exprs, C_Node);
842 P := P + 1;
843 -- Something special for wide strings???
844 end loop;
846 New_N := Make_Aggregate (Loc, Expressions => Exprs);
847 Set_Analyzed (New_N);
848 Set_Etype (New_N, Any_Composite);
850 Rewrite (N, New_N);
851 end Make_String_Into_Aggregate;
853 -----------------------
854 -- Resolve_Aggregate --
855 -----------------------
857 procedure Resolve_Aggregate (N : Node_Id; Typ : Entity_Id) is
858 Loc : constant Source_Ptr := Sloc (N);
860 Aggr_Subtyp : Entity_Id;
861 -- The actual aggregate subtype. This is not necessarily the same as Typ
862 -- which is the subtype of the context in which the aggregate was found.
864 Others_Box : Boolean := False;
865 -- Set to True if N represents a simple aggregate with only
866 -- (others => <>), not nested as part of another aggregate.
868 function Is_Full_Access_Aggregate (N : Node_Id) return Boolean;
869 -- If a full access object is initialized with an aggregate or is
870 -- assigned an aggregate, we have to prevent a piecemeal access or
871 -- assignment to the object, even if the aggregate is to be expanded.
872 -- We create a temporary for the aggregate, and assign the temporary
873 -- instead, so that the back end can generate an atomic move for it.
874 -- This is only done in the context of an object declaration or an
875 -- assignment. Function is a noop and returns false in other contexts.
877 function Within_Aggregate (N : Node_Id) return Boolean;
878 -- Return True if N is part of an N_Aggregate
880 ------------------------------
881 -- Is_Full_Access_Aggregate --
882 ------------------------------
884 function Is_Full_Access_Aggregate (N : Node_Id) return Boolean is
885 Loc : constant Source_Ptr := Sloc (N);
887 New_N : Node_Id;
888 Par : Node_Id;
889 Temp : Entity_Id;
890 Typ : Entity_Id;
892 begin
893 Par := Parent (N);
895 -- Aggregate may be qualified, so find outer context
897 if Nkind (Par) = N_Qualified_Expression then
898 Par := Parent (Par);
899 end if;
901 if not Comes_From_Source (Par) then
902 return False;
903 end if;
905 case Nkind (Par) is
906 when N_Assignment_Statement =>
907 Typ := Etype (Name (Par));
909 if not Is_Full_Access (Typ)
910 and then not Is_Full_Access_Object (Name (Par))
911 then
912 return False;
913 end if;
915 when N_Object_Declaration =>
916 Typ := Etype (Defining_Identifier (Par));
918 if not Is_Full_Access (Typ)
919 and then not Is_Full_Access (Defining_Identifier (Par))
920 then
921 return False;
922 end if;
924 when others =>
925 return False;
926 end case;
928 Temp := Make_Temporary (Loc, 'T', N);
929 New_N :=
930 Make_Object_Declaration (Loc,
931 Defining_Identifier => Temp,
932 Constant_Present => True,
933 Object_Definition => New_Occurrence_Of (Typ, Loc),
934 Expression => Relocate_Node (N));
935 Insert_Action (Par, New_N);
937 Rewrite (N, New_Occurrence_Of (Temp, Loc));
938 Analyze_And_Resolve (N, Typ);
940 return True;
941 end Is_Full_Access_Aggregate;
943 ----------------------
944 -- Within_Aggregate --
945 ----------------------
947 function Within_Aggregate (N : Node_Id) return Boolean is
948 P : Node_Id := Parent (N);
949 begin
950 while Present (P) loop
951 if Nkind (P) = N_Aggregate then
952 return True;
953 end if;
955 P := Parent (P);
956 end loop;
958 return False;
959 end Within_Aggregate;
961 -- Start of processing for Resolve_Aggregate
963 begin
964 -- Ignore junk empty aggregate resulting from parser error
966 if No (Expressions (N))
967 and then No (Component_Associations (N))
968 and then not Null_Record_Present (N)
969 then
970 return;
972 -- If the aggregate is assigned to a full access variable, we have
973 -- to prevent a piecemeal assignment even if the aggregate is to be
974 -- expanded. We create a temporary for the aggregate, and assign the
975 -- temporary instead, so that the back end can generate an atomic move
976 -- for it. This is properly an expansion activity but it must be done
977 -- before resolution because aggregate resolution cannot be done twice.
979 elsif Expander_Active and then Is_Full_Access_Aggregate (N) then
980 return;
981 end if;
983 -- If the aggregate has box-initialized components, its type must be
984 -- frozen so that initialization procedures can properly be called
985 -- in the resolution that follows. The replacement of boxes with
986 -- initialization calls is properly an expansion activity but it must
987 -- be done during resolution.
989 if Expander_Active
990 and then Present (Component_Associations (N))
991 then
992 declare
993 Comp : Node_Id;
994 First_Comp : Boolean := True;
996 begin
997 Comp := First (Component_Associations (N));
998 while Present (Comp) loop
999 if Box_Present (Comp) then
1000 if First_Comp
1001 and then No (Expressions (N))
1002 and then Nkind (First (Choices (Comp))) = N_Others_Choice
1003 and then not Within_Aggregate (N)
1004 then
1005 Others_Box := True;
1006 end if;
1008 Insert_Actions (N, Freeze_Entity (Typ, N));
1009 exit;
1010 end if;
1012 First_Comp := False;
1013 Next (Comp);
1014 end loop;
1015 end;
1016 end if;
1018 -- Check for aggregates not allowed in configurable run-time mode.
1019 -- We allow all cases of aggregates that do not come from source, since
1020 -- these are all assumed to be small (e.g. bounds of a string literal).
1021 -- We also allow aggregates of types we know to be small.
1023 if not Support_Aggregates_On_Target
1024 and then Comes_From_Source (N)
1025 and then (not Known_Static_Esize (Typ)
1026 or else Esize (Typ) > System_Max_Integer_Size)
1027 then
1028 Error_Msg_CRT ("aggregate", N);
1029 end if;
1031 -- Ada 2005 (AI-287): Limited aggregates allowed
1033 -- In an instance, ignore aggregate subcomponents that may be limited,
1034 -- because they originate in view conflicts. If the original aggregate
1035 -- is legal and the actuals are legal, the aggregate itself is legal.
1037 if Is_Limited_Type (Typ)
1038 and then Ada_Version < Ada_2005
1039 and then not In_Instance
1040 then
1041 Error_Msg_N ("aggregate type cannot be limited", N);
1042 Explain_Limited_Type (Typ, N);
1044 elsif Is_Class_Wide_Type (Typ) then
1045 Error_Msg_N ("type of aggregate cannot be class-wide", N);
1047 elsif Typ = Any_String
1048 or else Typ = Any_Composite
1049 then
1050 Error_Msg_N ("no unique type for aggregate", N);
1051 Set_Etype (N, Any_Composite);
1053 elsif Is_Array_Type (Typ) and then Null_Record_Present (N) then
1054 Error_Msg_N ("null record forbidden in array aggregate", N);
1056 elsif Has_Aspect (Typ, Aspect_Aggregate)
1057 and then Ekind (Typ) /= E_Record_Type
1058 and then Ada_Version >= Ada_2022
1059 then
1060 -- Check for Ada 2022 and () aggregate.
1062 if not Is_Homogeneous_Aggregate (N) then
1063 Error_Msg_N ("container aggregate must use '['], not ()", N);
1064 end if;
1066 Resolve_Container_Aggregate (N, Typ);
1068 elsif Is_Record_Type (Typ) then
1069 Resolve_Record_Aggregate (N, Typ);
1071 elsif Is_Array_Type (Typ) then
1073 -- First a special test, for the case of a positional aggregate of
1074 -- characters which can be replaced by a string literal.
1076 -- Do not perform this transformation if this was a string literal
1077 -- to start with, whose components needed constraint checks, or if
1078 -- the component type is non-static, because it will require those
1079 -- checks and be transformed back into an aggregate. If the index
1080 -- type is not Integer the aggregate may represent a user-defined
1081 -- string type but the context might need the original type so we
1082 -- do not perform the transformation at this point.
1084 if Number_Dimensions (Typ) = 1
1085 and then Is_Standard_Character_Type (Component_Type (Typ))
1086 and then No (Component_Associations (N))
1087 and then not Is_Limited_Composite (Typ)
1088 and then not Is_Private_Composite (Typ)
1089 and then not Is_Bit_Packed_Array (Typ)
1090 and then Nkind (Original_Node (Parent (N))) /= N_String_Literal
1091 and then Is_OK_Static_Subtype (Component_Type (Typ))
1092 and then Base_Type (Etype (First_Index (Typ))) =
1093 Base_Type (Standard_Integer)
1094 then
1095 declare
1096 Expr : Node_Id;
1098 begin
1099 Expr := First (Expressions (N));
1100 while Present (Expr) loop
1101 exit when Nkind (Expr) /= N_Character_Literal;
1102 Next (Expr);
1103 end loop;
1105 if No (Expr) then
1106 Start_String;
1108 Expr := First (Expressions (N));
1109 while Present (Expr) loop
1110 Store_String_Char (UI_To_CC (Char_Literal_Value (Expr)));
1111 Next (Expr);
1112 end loop;
1114 Rewrite (N, Make_String_Literal (Loc, End_String));
1116 Analyze_And_Resolve (N, Typ);
1117 return;
1118 end if;
1119 end;
1120 end if;
1122 -- Here if we have a real aggregate to deal with
1124 Array_Aggregate : declare
1125 Aggr_Resolved : Boolean;
1126 Aggr_Typ : constant Entity_Id := Etype (Typ);
1127 -- This is the unconstrained array type, which is the type against
1128 -- which the aggregate is to be resolved. Typ itself is the array
1129 -- type of the context which may not be the same subtype as the
1130 -- subtype for the final aggregate.
1132 Is_Null_Aggr : constant Boolean := Is_Null_Aggregate (N);
1134 begin
1135 -- In the following we determine whether an OTHERS choice is
1136 -- allowed inside the array aggregate. The test checks the context
1137 -- in which the array aggregate occurs. If the context does not
1138 -- permit it, or the aggregate type is unconstrained, an OTHERS
1139 -- choice is not allowed (except that it is always allowed on the
1140 -- right-hand side of an assignment statement; in this case the
1141 -- constrainedness of the type doesn't matter, because an array
1142 -- object is always constrained).
1144 -- If expansion is disabled (generic context, or semantics-only
1145 -- mode) actual subtypes cannot be constructed, and the type of an
1146 -- object may be its unconstrained nominal type. However, if the
1147 -- context is an assignment statement, OTHERS is allowed, because
1148 -- the target of the assignment will have a constrained subtype
1149 -- when fully compiled. Ditto if the context is an initialization
1150 -- procedure where a component may have a predicate function that
1151 -- carries the base type.
1153 -- Note that there is no node for Explicit_Actual_Parameter.
1154 -- To test for this context we therefore have to test for node
1155 -- N_Parameter_Association which itself appears only if there is a
1156 -- formal parameter. Consequently we also need to test for
1157 -- N_Procedure_Call_Statement or N_Function_Call.
1159 -- The context may be an N_Reference node, created by expansion.
1160 -- Legality of the others clause was established in the source,
1161 -- so the context is legal.
1163 Set_Etype (N, Aggr_Typ); -- May be overridden later on
1165 if Is_Null_Aggr then
1166 Set_Etype (N, Typ);
1167 Aggr_Resolved := Resolve_Null_Array_Aggregate (N);
1169 elsif Nkind (Parent (N)) = N_Assignment_Statement
1170 or else Inside_Init_Proc
1171 or else (Is_Constrained (Typ)
1172 and then Nkind (Parent (N)) in
1173 N_Parameter_Association
1174 | N_Function_Call
1175 | N_Procedure_Call_Statement
1176 | N_Generic_Association
1177 | N_Formal_Object_Declaration
1178 | N_Simple_Return_Statement
1179 | N_Object_Declaration
1180 | N_Component_Declaration
1181 | N_Parameter_Specification
1182 | N_Qualified_Expression
1183 | N_Unchecked_Type_Conversion
1184 | N_Reference
1185 | N_Aggregate
1186 | N_Extension_Aggregate
1187 | N_Component_Association
1188 | N_Case_Expression_Alternative
1189 | N_If_Expression
1190 | N_Expression_With_Actions)
1191 then
1192 Aggr_Resolved :=
1193 Resolve_Array_Aggregate
1195 Index => First_Index (Aggr_Typ),
1196 Index_Constr => First_Index (Typ),
1197 Component_Typ => Component_Type (Typ),
1198 Others_Allowed => True);
1199 else
1200 Aggr_Resolved :=
1201 Resolve_Array_Aggregate
1203 Index => First_Index (Aggr_Typ),
1204 Index_Constr => First_Index (Aggr_Typ),
1205 Component_Typ => Component_Type (Typ),
1206 Others_Allowed => False);
1207 end if;
1209 if not Aggr_Resolved then
1211 -- A parenthesized expression may have been intended as an
1212 -- aggregate, leading to a type error when analyzing the
1213 -- component. This can also happen for a nested component
1214 -- (see Analyze_Aggr_Expr).
1216 if Paren_Count (N) > 0 then
1217 Error_Msg_N
1218 ("positional aggregate cannot have one component", N);
1219 end if;
1221 Aggr_Subtyp := Any_Composite;
1223 else
1224 Aggr_Subtyp := Array_Aggr_Subtype (N, Typ);
1225 end if;
1227 Set_Etype (N, Aggr_Subtyp);
1228 end Array_Aggregate;
1230 elsif Is_Private_Type (Typ)
1231 and then Present (Full_View (Typ))
1232 and then (In_Inlined_Body or In_Instance_Body)
1233 and then Is_Composite_Type (Full_View (Typ))
1234 then
1235 Resolve (N, Full_View (Typ));
1237 else
1238 Error_Msg_N ("illegal context for aggregate", N);
1239 end if;
1241 -- If we can determine statically that the evaluation of the aggregate
1242 -- raises Constraint_Error, then replace the aggregate with an
1243 -- N_Raise_Constraint_Error node, but set the Etype to the right
1244 -- aggregate subtype. Gigi needs this.
1246 if Raises_Constraint_Error (N) then
1247 Aggr_Subtyp := Etype (N);
1248 Rewrite (N,
1249 Make_Raise_Constraint_Error (Loc, Reason => CE_Range_Check_Failed));
1250 Set_Raises_Constraint_Error (N);
1251 Set_Etype (N, Aggr_Subtyp);
1252 Set_Analyzed (N);
1253 end if;
1255 if Warn_On_No_Value_Assigned
1256 and then Others_Box
1257 and then not Is_Fully_Initialized_Type (Etype (N))
1258 then
1259 Error_Msg_N ("?v?aggregate not fully initialized", N);
1260 end if;
1262 Check_Function_Writable_Actuals (N);
1263 end Resolve_Aggregate;
1265 -----------------------------
1266 -- Resolve_Array_Aggregate --
1267 -----------------------------
1269 function Resolve_Array_Aggregate
1270 (N : Node_Id;
1271 Index : Node_Id;
1272 Index_Constr : Node_Id;
1273 Component_Typ : Entity_Id;
1274 Others_Allowed : Boolean) return Boolean
1276 Loc : constant Source_Ptr := Sloc (N);
1278 Failure : constant Boolean := False;
1279 Success : constant Boolean := True;
1281 Index_Typ : constant Entity_Id := Etype (Index);
1282 Index_Typ_Low : constant Node_Id := Type_Low_Bound (Index_Typ);
1283 Index_Typ_High : constant Node_Id := Type_High_Bound (Index_Typ);
1284 -- The type of the index corresponding to the array sub-aggregate along
1285 -- with its low and upper bounds.
1287 Index_Base : constant Entity_Id := Base_Type (Index_Typ);
1288 Index_Base_Low : constant Node_Id := Type_Low_Bound (Index_Base);
1289 Index_Base_High : constant Node_Id := Type_High_Bound (Index_Base);
1290 -- Ditto for the base type
1292 Others_Present : Boolean := False;
1294 Nb_Choices : Nat := 0;
1295 -- Contains the overall number of named choices in this sub-aggregate
1297 function Add (Val : Uint; To : Node_Id) return Node_Id;
1298 -- Creates a new expression node where Val is added to expression To.
1299 -- Tries to constant fold whenever possible. To must be an already
1300 -- analyzed expression.
1302 procedure Check_Bound (BH : Node_Id; AH : in out Node_Id);
1303 -- Checks that AH (the upper bound of an array aggregate) is less than
1304 -- or equal to BH (the upper bound of the index base type). If the check
1305 -- fails, a warning is emitted, the Raises_Constraint_Error flag of N is
1306 -- set, and AH is replaced with a duplicate of BH.
1308 procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id);
1309 -- Checks that range AL .. AH is compatible with range L .. H. Emits a
1310 -- warning if not and sets the Raises_Constraint_Error flag in N.
1312 procedure Check_Length (L, H : Node_Id; Len : Uint);
1313 -- Checks that range L .. H contains at least Len elements. Emits a
1314 -- warning if not and sets the Raises_Constraint_Error flag in N.
1316 function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean;
1317 -- Returns True if range L .. H is dynamic or null
1319 procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean);
1320 -- Given expression node From, this routine sets OK to False if it
1321 -- cannot statically evaluate From. Otherwise it stores this static
1322 -- value into Value.
1324 function Resolve_Aggr_Expr
1325 (Expr : Node_Id;
1326 Single_Elmt : Boolean) return Boolean;
1327 -- Resolves aggregate expression Expr. Returns False if resolution
1328 -- fails. If Single_Elmt is set to False, the expression Expr may be
1329 -- used to initialize several array aggregate elements (this can happen
1330 -- for discrete choices such as "L .. H => Expr" or the OTHERS choice).
1331 -- In this event we do not resolve Expr unless expansion is disabled.
1332 -- To know why, see the DELAYED COMPONENT RESOLUTION note above.
1334 -- NOTE: In the case of "... => <>", we pass the N_Component_Association
1335 -- node as Expr, since there is no Expression and we need a Sloc for the
1336 -- error message.
1338 procedure Resolve_Iterated_Component_Association
1339 (N : Node_Id;
1340 Index_Typ : Entity_Id);
1341 -- For AI12-061
1343 procedure Warn_On_Null_Component_Association (Expr : Node_Id);
1344 -- Expr is either a conditional expression or a case expression of an
1345 -- iterated component association initializing the aggregate N with
1346 -- components that can never be null. Report warning on associations
1347 -- that may initialize some component with a null value.
1349 ---------
1350 -- Add --
1351 ---------
1353 function Add (Val : Uint; To : Node_Id) return Node_Id is
1354 Expr_Pos : Node_Id;
1355 Expr : Node_Id;
1356 To_Pos : Node_Id;
1358 begin
1359 if Raises_Constraint_Error (To) then
1360 return To;
1361 end if;
1363 -- First test if we can do constant folding
1365 if Compile_Time_Known_Value (To)
1366 or else Nkind (To) = N_Integer_Literal
1367 then
1368 Expr_Pos := Make_Integer_Literal (Loc, Expr_Value (To) + Val);
1369 Set_Is_Static_Expression (Expr_Pos);
1370 Set_Etype (Expr_Pos, Etype (To));
1371 Set_Analyzed (Expr_Pos, Analyzed (To));
1373 if not Is_Enumeration_Type (Index_Typ) then
1374 Expr := Expr_Pos;
1376 -- If we are dealing with enumeration return
1377 -- Index_Typ'Val (Expr_Pos)
1379 else
1380 Expr :=
1381 Make_Attribute_Reference
1382 (Loc,
1383 Prefix => New_Occurrence_Of (Index_Typ, Loc),
1384 Attribute_Name => Name_Val,
1385 Expressions => New_List (Expr_Pos));
1386 end if;
1388 return Expr;
1389 end if;
1391 -- If we are here no constant folding possible
1393 if not Is_Enumeration_Type (Index_Base) then
1394 Expr :=
1395 Make_Op_Add (Loc,
1396 Left_Opnd => Duplicate_Subexpr (To),
1397 Right_Opnd => Make_Integer_Literal (Loc, Val));
1399 -- If we are dealing with enumeration return
1400 -- Index_Typ'Val (Index_Typ'Pos (To) + Val)
1402 else
1403 To_Pos :=
1404 Make_Attribute_Reference
1405 (Loc,
1406 Prefix => New_Occurrence_Of (Index_Typ, Loc),
1407 Attribute_Name => Name_Pos,
1408 Expressions => New_List (Duplicate_Subexpr (To)));
1410 Expr_Pos :=
1411 Make_Op_Add (Loc,
1412 Left_Opnd => To_Pos,
1413 Right_Opnd => Make_Integer_Literal (Loc, Val));
1415 Expr :=
1416 Make_Attribute_Reference
1417 (Loc,
1418 Prefix => New_Occurrence_Of (Index_Typ, Loc),
1419 Attribute_Name => Name_Val,
1420 Expressions => New_List (Expr_Pos));
1422 -- If the index type has a non standard representation, the
1423 -- attributes 'Val and 'Pos expand into function calls and the
1424 -- resulting expression is considered non-safe for reevaluation
1425 -- by the backend. Relocate it into a constant temporary in order
1426 -- to make it safe for reevaluation.
1428 if Has_Non_Standard_Rep (Etype (N)) then
1429 declare
1430 Def_Id : Entity_Id;
1432 begin
1433 Def_Id := Make_Temporary (Loc, 'R', Expr);
1434 Set_Etype (Def_Id, Index_Typ);
1435 Insert_Action (N,
1436 Make_Object_Declaration (Loc,
1437 Defining_Identifier => Def_Id,
1438 Object_Definition =>
1439 New_Occurrence_Of (Index_Typ, Loc),
1440 Constant_Present => True,
1441 Expression => Relocate_Node (Expr)));
1443 Expr := New_Occurrence_Of (Def_Id, Loc);
1444 end;
1445 end if;
1446 end if;
1448 return Expr;
1449 end Add;
1451 -----------------
1452 -- Check_Bound --
1453 -----------------
1455 procedure Check_Bound (BH : Node_Id; AH : in out Node_Id) is
1456 Val_BH : Uint;
1457 Val_AH : Uint;
1459 OK_BH : Boolean;
1460 OK_AH : Boolean;
1462 begin
1463 Get (Value => Val_BH, From => BH, OK => OK_BH);
1464 Get (Value => Val_AH, From => AH, OK => OK_AH);
1466 if OK_BH and then OK_AH and then Val_BH < Val_AH then
1467 Set_Raises_Constraint_Error (N);
1468 Error_Msg_Warn := SPARK_Mode /= On;
1469 Error_Msg_N ("upper bound out of range<<", AH);
1470 Error_Msg_N ("\Constraint_Error [<<", AH);
1472 -- You need to set AH to BH or else in the case of enumerations
1473 -- indexes we will not be able to resolve the aggregate bounds.
1475 AH := Duplicate_Subexpr (BH);
1476 end if;
1477 end Check_Bound;
1479 ------------------
1480 -- Check_Bounds --
1481 ------------------
1483 procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id) is
1484 Val_L : Uint;
1485 Val_H : Uint;
1486 Val_AL : Uint;
1487 Val_AH : Uint;
1489 OK_L : Boolean;
1490 OK_H : Boolean;
1492 OK_AL : Boolean;
1493 OK_AH : Boolean;
1494 pragma Warnings (Off, OK_AL);
1495 pragma Warnings (Off, OK_AH);
1497 begin
1498 if Raises_Constraint_Error (N)
1499 or else Dynamic_Or_Null_Range (AL, AH)
1500 then
1501 return;
1502 end if;
1504 Get (Value => Val_L, From => L, OK => OK_L);
1505 Get (Value => Val_H, From => H, OK => OK_H);
1507 Get (Value => Val_AL, From => AL, OK => OK_AL);
1508 Get (Value => Val_AH, From => AH, OK => OK_AH);
1510 if OK_L and then Val_L > Val_AL then
1511 Set_Raises_Constraint_Error (N);
1512 Error_Msg_Warn := SPARK_Mode /= On;
1513 Error_Msg_N ("lower bound of aggregate out of range<<", N);
1514 Error_Msg_N ("\Constraint_Error [<<", N);
1515 end if;
1517 if OK_H and then Val_H < Val_AH then
1518 Set_Raises_Constraint_Error (N);
1519 Error_Msg_Warn := SPARK_Mode /= On;
1520 Error_Msg_N ("upper bound of aggregate out of range<<", N);
1521 Error_Msg_N ("\Constraint_Error [<<", N);
1522 end if;
1523 end Check_Bounds;
1525 ------------------
1526 -- Check_Length --
1527 ------------------
1529 procedure Check_Length (L, H : Node_Id; Len : Uint) is
1530 Val_L : Uint;
1531 Val_H : Uint;
1533 OK_L : Boolean;
1534 OK_H : Boolean;
1536 Range_Len : Uint;
1538 begin
1539 if Raises_Constraint_Error (N) then
1540 return;
1541 end if;
1543 Get (Value => Val_L, From => L, OK => OK_L);
1544 Get (Value => Val_H, From => H, OK => OK_H);
1546 if not OK_L or else not OK_H then
1547 return;
1548 end if;
1550 -- If null range length is zero
1552 if Val_L > Val_H then
1553 Range_Len := Uint_0;
1554 else
1555 Range_Len := Val_H - Val_L + 1;
1556 end if;
1558 if Range_Len < Len then
1559 Set_Raises_Constraint_Error (N);
1560 Error_Msg_Warn := SPARK_Mode /= On;
1561 Error_Msg_N ("too many elements<<", N);
1562 Error_Msg_N ("\Constraint_Error [<<", N);
1563 end if;
1564 end Check_Length;
1566 ---------------------------
1567 -- Dynamic_Or_Null_Range --
1568 ---------------------------
1570 function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean is
1571 Val_L : Uint;
1572 Val_H : Uint;
1574 OK_L : Boolean;
1575 OK_H : Boolean;
1577 begin
1578 Get (Value => Val_L, From => L, OK => OK_L);
1579 Get (Value => Val_H, From => H, OK => OK_H);
1581 return not OK_L or else not OK_H
1582 or else not Is_OK_Static_Expression (L)
1583 or else not Is_OK_Static_Expression (H)
1584 or else Val_L > Val_H;
1585 end Dynamic_Or_Null_Range;
1587 ---------
1588 -- Get --
1589 ---------
1591 procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean) is
1592 begin
1593 OK := True;
1595 if Compile_Time_Known_Value (From) then
1596 Value := Expr_Value (From);
1598 -- If expression From is something like Some_Type'Val (10) then
1599 -- Value = 10.
1601 elsif Nkind (From) = N_Attribute_Reference
1602 and then Attribute_Name (From) = Name_Val
1603 and then Compile_Time_Known_Value (First (Expressions (From)))
1604 then
1605 Value := Expr_Value (First (Expressions (From)));
1606 else
1607 Value := Uint_0;
1608 OK := False;
1609 end if;
1610 end Get;
1612 -----------------------
1613 -- Resolve_Aggr_Expr --
1614 -----------------------
1616 function Resolve_Aggr_Expr
1617 (Expr : Node_Id;
1618 Single_Elmt : Boolean) return Boolean
1620 Nxt_Ind : constant Node_Id := Next_Index (Index);
1621 Nxt_Ind_Constr : constant Node_Id := Next_Index (Index_Constr);
1622 -- Index is the current index corresponding to the expression
1624 Resolution_OK : Boolean := True;
1625 -- Set to False if resolution of the expression failed
1627 begin
1628 -- Defend against previous errors
1630 if Nkind (Expr) = N_Error
1631 or else Error_Posted (Expr)
1632 then
1633 return True;
1634 end if;
1636 -- If the array type against which we are resolving the aggregate
1637 -- has several dimensions, the expressions nested inside the
1638 -- aggregate must be further aggregates (or strings).
1640 if Present (Nxt_Ind) then
1641 if Nkind (Expr) /= N_Aggregate then
1643 -- A string literal can appear where a one-dimensional array
1644 -- of characters is expected. If the literal looks like an
1645 -- operator, it is still an operator symbol, which will be
1646 -- transformed into a string when analyzed.
1648 if Is_Character_Type (Component_Typ)
1649 and then No (Next_Index (Nxt_Ind))
1650 and then Nkind (Expr) in N_String_Literal | N_Operator_Symbol
1651 then
1652 -- A string literal used in a multidimensional array
1653 -- aggregate in place of the final one-dimensional
1654 -- aggregate must not be enclosed in parentheses.
1656 if Paren_Count (Expr) /= 0 then
1657 Error_Msg_N ("no parenthesis allowed here", Expr);
1658 end if;
1660 Make_String_Into_Aggregate (Expr);
1662 else
1663 Error_Msg_N ("nested array aggregate expected", Expr);
1665 -- If the expression is parenthesized, this may be
1666 -- a missing component association for a 1-aggregate.
1668 if Paren_Count (Expr) > 0 then
1669 Error_Msg_N
1670 ("\if single-component aggregate is intended, "
1671 & "write e.g. (1 ='> ...)", Expr);
1672 end if;
1674 return Failure;
1675 end if;
1676 end if;
1678 -- If it's "... => <>", nothing to resolve
1680 if Nkind (Expr) = N_Component_Association then
1681 pragma Assert (Box_Present (Expr));
1682 return Success;
1683 end if;
1685 -- Ada 2005 (AI-231): Propagate the type to the nested aggregate.
1686 -- Required to check the null-exclusion attribute (if present).
1687 -- This value may be overridden later on.
1689 Set_Etype (Expr, Etype (N));
1691 Resolution_OK := Resolve_Array_Aggregate
1692 (Expr, Nxt_Ind, Nxt_Ind_Constr, Component_Typ, Others_Allowed);
1694 else
1695 -- If it's "... => <>", nothing to resolve
1697 if Nkind (Expr) = N_Component_Association then
1698 pragma Assert (Box_Present (Expr));
1699 return Success;
1700 end if;
1702 -- Do not resolve the expressions of discrete or others choices
1703 -- unless the expression covers a single component, or the
1704 -- expander is inactive.
1706 -- In SPARK mode, expressions that can perform side effects will
1707 -- be recognized by the gnat2why back-end, and the whole
1708 -- subprogram will be ignored. So semantic analysis can be
1709 -- performed safely.
1711 if Single_Elmt
1712 or else not Expander_Active
1713 or else In_Spec_Expression
1714 then
1715 Analyze_And_Resolve (Expr, Component_Typ);
1716 Check_Expr_OK_In_Limited_Aggregate (Expr);
1717 Check_Non_Static_Context (Expr);
1718 Aggregate_Constraint_Checks (Expr, Component_Typ);
1719 Check_Unset_Reference (Expr);
1720 end if;
1721 end if;
1723 -- If an aggregate component has a type with predicates, an explicit
1724 -- predicate check must be applied, as for an assignment statement,
1725 -- because the aggregate might not be expanded into individual
1726 -- component assignments. If the expression covers several components
1727 -- the analysis and the predicate check take place later.
1729 if Has_Predicates (Component_Typ)
1730 and then Analyzed (Expr)
1731 then
1732 Apply_Predicate_Check (Expr, Component_Typ);
1733 end if;
1735 if Raises_Constraint_Error (Expr)
1736 and then Nkind (Parent (Expr)) /= N_Component_Association
1737 then
1738 Set_Raises_Constraint_Error (N);
1739 end if;
1741 -- If the expression has been marked as requiring a range check,
1742 -- then generate it here. It's a bit odd to be generating such
1743 -- checks in the analyzer, but harmless since Generate_Range_Check
1744 -- does nothing (other than making sure Do_Range_Check is set) if
1745 -- the expander is not active.
1747 if Do_Range_Check (Expr) then
1748 Generate_Range_Check (Expr, Component_Typ, CE_Range_Check_Failed);
1749 end if;
1751 return Resolution_OK;
1752 end Resolve_Aggr_Expr;
1754 --------------------------------------------
1755 -- Resolve_Iterated_Component_Association --
1756 --------------------------------------------
1758 procedure Resolve_Iterated_Component_Association
1759 (N : Node_Id;
1760 Index_Typ : Entity_Id)
1762 Loc : constant Source_Ptr := Sloc (N);
1763 Id : constant Entity_Id := Defining_Identifier (N);
1765 -----------------------
1766 -- Remove_References --
1767 -----------------------
1769 function Remove_Reference (N : Node_Id) return Traverse_Result;
1770 -- Remove reference to the entity Id after analysis, so it can be
1771 -- properly reanalyzed after construct is expanded into a loop.
1773 function Remove_Reference (N : Node_Id) return Traverse_Result is
1774 begin
1775 if Nkind (N) = N_Identifier
1776 and then Present (Entity (N))
1777 and then Entity (N) = Id
1778 then
1779 Set_Entity (N, Empty);
1780 Set_Etype (N, Empty);
1781 end if;
1782 Set_Analyzed (N, False);
1783 return OK;
1784 end Remove_Reference;
1786 procedure Remove_References is new Traverse_Proc (Remove_Reference);
1788 -- Local variables
1790 Choice : Node_Id;
1791 Dummy : Boolean;
1792 Scop : Entity_Id;
1793 Expr : constant Node_Id := Expression (N);
1795 -- Start of processing for Resolve_Iterated_Component_Association
1797 begin
1798 Error_Msg_Ada_2022_Feature ("iterated component", Loc);
1800 -- Create a scope in which to introduce an index, to make it visible
1801 -- for the analysis of component expression.
1803 Scop := New_Internal_Entity (E_Loop, Current_Scope, Loc, 'L');
1804 Set_Etype (Scop, Standard_Void_Type);
1805 Set_Parent (Scop, Parent (N));
1806 Push_Scope (Scop);
1808 -- If there is iterator specification, then its preanalysis will make
1809 -- the index visible.
1811 if Present (Iterator_Specification (N)) then
1812 Preanalyze (Iterator_Specification (N));
1814 -- Otherwise, analyze discrete choices and make the index visible
1816 else
1817 -- Insert index name into current scope but don't decorate it yet,
1818 -- so that a premature usage of this name in discrete choices will
1819 -- be nicely diagnosed.
1821 Enter_Name (Id);
1823 Choice := First (Discrete_Choices (N));
1825 while Present (Choice) loop
1826 if Nkind (Choice) = N_Others_Choice then
1827 Others_Present := True;
1829 else
1830 Analyze (Choice);
1832 -- Choice can be a subtype name, a range, or an expression
1834 if Is_Entity_Name (Choice)
1835 and then Is_Type (Entity (Choice))
1836 and then
1837 Base_Type (Entity (Choice)) = Base_Type (Index_Typ)
1838 then
1839 null;
1841 else
1842 Analyze_And_Resolve (Choice, Index_Typ);
1843 end if;
1844 end if;
1846 Next (Choice);
1847 end loop;
1849 -- Decorate the index variable
1851 Set_Etype (Id, Index_Typ);
1852 Mutate_Ekind (Id, E_Variable);
1853 Set_Is_Not_Self_Hidden (Id);
1854 Set_Scope (Id, Scop);
1855 end if;
1857 -- Analyze expression without expansion, to verify legality.
1858 -- When generating code, we then remove references to the index
1859 -- variable, because the expression will be analyzed anew after
1860 -- rewritting as a loop with a new index variable; when not
1861 -- generating code we leave the analyzed expression as it is.
1863 Dummy := Resolve_Aggr_Expr (Expr, Single_Elmt => False);
1865 if Operating_Mode /= Check_Semantics then
1866 Remove_References (Expr);
1867 end if;
1869 -- An iterated_component_association may appear in a nested
1870 -- aggregate for a multidimensional structure: preserve the bounds
1871 -- computed for the expression, as well as the anonymous array
1872 -- type generated for it; both are needed during array expansion.
1874 if Nkind (Expr) = N_Aggregate then
1875 Set_Aggregate_Bounds (Expression (N), Aggregate_Bounds (Expr));
1876 Set_Etype (Expression (N), Etype (Expr));
1877 end if;
1879 End_Scope;
1880 end Resolve_Iterated_Component_Association;
1882 ----------------------------------------
1883 -- Warn_On_Null_Component_Association --
1884 ----------------------------------------
1886 procedure Warn_On_Null_Component_Association (Expr : Node_Id) is
1887 Comp_Typ : constant Entity_Id := Component_Type (Etype (N));
1889 procedure Check_Case_Expr (N : Node_Id);
1890 -- Check if a case expression may initialize some component with a
1891 -- null value.
1893 procedure Check_Cond_Expr (N : Node_Id);
1894 -- Check if a conditional expression may initialize some component
1895 -- with a null value.
1897 procedure Check_Expr (Expr : Node_Id);
1898 -- Check if an expression may initialize some component with a
1899 -- null value.
1901 procedure Warn_On_Null_Expression_And_Rewrite (Null_Expr : Node_Id);
1902 -- Report warning on known null expression and replace the expression
1903 -- by a raise constraint error node.
1905 ---------------------
1906 -- Check_Case_Expr --
1907 ---------------------
1909 procedure Check_Case_Expr (N : Node_Id) is
1910 Alt_Node : Node_Id := First (Alternatives (N));
1912 begin
1913 while Present (Alt_Node) loop
1914 Check_Expr (Expression (Alt_Node));
1915 Next (Alt_Node);
1916 end loop;
1917 end Check_Case_Expr;
1919 ---------------------
1920 -- Check_Cond_Expr --
1921 ---------------------
1923 procedure Check_Cond_Expr (N : Node_Id) is
1924 If_Expr : Node_Id := N;
1925 Then_Expr : Node_Id;
1926 Else_Expr : Node_Id;
1928 begin
1929 Then_Expr := Next (First (Expressions (If_Expr)));
1930 Else_Expr := Next (Then_Expr);
1932 Check_Expr (Then_Expr);
1934 -- Process elsif parts (if any)
1936 while Nkind (Else_Expr) = N_If_Expression loop
1937 If_Expr := Else_Expr;
1938 Then_Expr := Next (First (Expressions (If_Expr)));
1939 Else_Expr := Next (Then_Expr);
1941 Check_Expr (Then_Expr);
1942 end loop;
1944 if Known_Null (Else_Expr) then
1945 Warn_On_Null_Expression_And_Rewrite (Else_Expr);
1946 end if;
1947 end Check_Cond_Expr;
1949 ----------------
1950 -- Check_Expr --
1951 ----------------
1953 procedure Check_Expr (Expr : Node_Id) is
1954 begin
1955 if Known_Null (Expr) then
1956 Warn_On_Null_Expression_And_Rewrite (Expr);
1958 elsif Nkind (Expr) = N_If_Expression then
1959 Check_Cond_Expr (Expr);
1961 elsif Nkind (Expr) = N_Case_Expression then
1962 Check_Case_Expr (Expr);
1963 end if;
1964 end Check_Expr;
1966 -----------------------------------------
1967 -- Warn_On_Null_Expression_And_Rewrite --
1968 -----------------------------------------
1970 procedure Warn_On_Null_Expression_And_Rewrite (Null_Expr : Node_Id) is
1971 begin
1972 Error_Msg_N
1973 ("(Ada 2005) NULL not allowed in null-excluding component??",
1974 Null_Expr);
1975 Error_Msg_N
1976 ("\Constraint_Error might be raised at run time??", Null_Expr);
1978 -- We cannot use Apply_Compile_Time_Constraint_Error because in
1979 -- some cases the components are rewritten and the runtime error
1980 -- would be missed.
1982 Rewrite (Null_Expr,
1983 Make_Raise_Constraint_Error (Sloc (Null_Expr),
1984 Reason => CE_Access_Check_Failed));
1986 Set_Etype (Null_Expr, Comp_Typ);
1987 Set_Analyzed (Null_Expr);
1988 end Warn_On_Null_Expression_And_Rewrite;
1990 -- Start of processing for Warn_On_Null_Component_Association
1992 begin
1993 pragma Assert (Can_Never_Be_Null (Comp_Typ));
1995 case Nkind (Expr) is
1996 when N_If_Expression =>
1997 Check_Cond_Expr (Expr);
1999 when N_Case_Expression =>
2000 Check_Case_Expr (Expr);
2002 when others =>
2003 pragma Assert (False);
2004 null;
2005 end case;
2006 end Warn_On_Null_Component_Association;
2008 -- Local variables
2010 Assoc : Node_Id;
2011 Choice : Node_Id;
2012 Expr : Node_Id;
2013 Discard : Node_Id;
2015 Aggr_Low : Node_Id := Empty;
2016 Aggr_High : Node_Id := Empty;
2017 -- The actual low and high bounds of this sub-aggregate
2019 Case_Table_Size : Nat;
2020 -- Contains the size of the case table needed to sort aggregate choices
2022 Choices_Low : Node_Id := Empty;
2023 Choices_High : Node_Id := Empty;
2024 -- The lowest and highest discrete choices values for a named aggregate
2026 Delete_Choice : Boolean;
2027 -- Used when replacing a subtype choice with predicate by a list
2029 Has_Iterator_Specifications : Boolean := False;
2030 -- Flag to indicate that all named associations are iterated component
2031 -- associations with iterator specifications, in which case the
2032 -- expansion will create two loops: one to evaluate the size and one
2033 -- to generate the elements (4.3.3 (20.2/5)).
2035 Nb_Elements : Uint := Uint_0;
2036 -- The number of elements in a positional aggregate
2038 Nb_Discrete_Choices : Nat := 0;
2039 -- The overall number of discrete choices (not counting others choice)
2041 -- Start of processing for Resolve_Array_Aggregate
2043 begin
2044 -- Ignore junk empty aggregate resulting from parser error
2046 if No (Expressions (N))
2047 and then No (Component_Associations (N))
2048 and then not Null_Record_Present (N)
2049 then
2050 return Failure;
2051 end if;
2053 -- Disable the warning for GNAT Mode to allow for easier transition.
2055 if Ada_Version_Explicit >= Ada_2022
2056 and then Warn_On_Obsolescent_Feature
2057 and then not GNAT_Mode
2058 and then not Is_Homogeneous_Aggregate (N)
2059 and then not Is_Enum_Array_Aggregate (N)
2060 and then Is_Parenthesis_Aggregate (N)
2061 and then Nkind (Parent (N)) /= N_Qualified_Expression
2062 and then Comes_From_Source (N)
2063 then
2064 Error_Msg_N
2065 ("?j?array aggregate using () is an" &
2066 " obsolescent syntax, use '['] instead", N);
2067 end if;
2069 -- STEP 1: make sure the aggregate is correctly formatted
2071 if Present (Component_Associations (N)) then
2073 -- Verify that all or none of the component associations
2074 -- include an iterator specification.
2076 Assoc := First (Component_Associations (N));
2077 if Nkind (Assoc) = N_Iterated_Component_Association
2078 and then Present (Iterator_Specification (Assoc))
2079 then
2080 -- All other component associations must have an iterator spec.
2082 Next (Assoc);
2083 while Present (Assoc) loop
2084 if Nkind (Assoc) /= N_Iterated_Component_Association
2085 or else No (Iterator_Specification (Assoc))
2086 then
2087 Error_Msg_N ("mixed iterated component association"
2088 & " (RM 4.3.3 (17.1/5))",
2089 Assoc);
2090 return Failure;
2091 end if;
2093 Next (Assoc);
2094 end loop;
2096 Has_Iterator_Specifications := True;
2098 else
2099 -- or none of them do.
2101 Next (Assoc);
2102 while Present (Assoc) loop
2103 if Nkind (Assoc) = N_Iterated_Component_Association
2104 and then Present (Iterator_Specification (Assoc))
2105 then
2106 Error_Msg_N ("mixed iterated component association"
2107 & " (RM 4.3.3 (17.1/5))",
2108 Assoc);
2109 return Failure;
2110 end if;
2112 Next (Assoc);
2113 end loop;
2115 end if;
2117 Assoc := First (Component_Associations (N));
2118 while Present (Assoc) loop
2119 if Nkind (Assoc) = N_Iterated_Component_Association then
2120 Resolve_Iterated_Component_Association (Assoc, Index_Typ);
2122 elsif Nkind (Assoc) /= N_Component_Association then
2123 Error_Msg_N
2124 ("invalid component association for aggregate", Assoc);
2125 return Failure;
2126 end if;
2128 Choice := First (Choice_List (Assoc));
2129 Delete_Choice := False;
2130 while Present (Choice) loop
2131 if Nkind (Choice) = N_Others_Choice then
2132 Others_Present := True;
2134 if Choice /= First (Choice_List (Assoc))
2135 or else Present (Next (Choice))
2136 then
2137 Error_Msg_N
2138 ("OTHERS must appear alone in a choice list", Choice);
2139 return Failure;
2140 end if;
2142 if Present (Next (Assoc)) then
2143 Error_Msg_N
2144 ("OTHERS must appear last in an aggregate", Choice);
2145 return Failure;
2146 end if;
2148 if Ada_Version = Ada_83
2149 and then Assoc /= First (Component_Associations (N))
2150 and then Nkind (Parent (N)) in
2151 N_Assignment_Statement | N_Object_Declaration
2152 then
2153 Error_Msg_N
2154 ("(Ada 83) illegal context for OTHERS choice", N);
2155 end if;
2157 elsif Is_Entity_Name (Choice) then
2158 Analyze (Choice);
2160 declare
2161 E : constant Entity_Id := Entity (Choice);
2162 New_Cs : List_Id;
2163 P : Node_Id;
2164 C : Node_Id;
2166 begin
2167 if Is_Type (E) and then Has_Predicates (E) then
2168 Freeze_Before (N, E);
2170 if Has_Dynamic_Predicate_Aspect (E)
2171 or else Has_Ghost_Predicate_Aspect (E)
2172 then
2173 Error_Msg_NE
2174 ("subtype& has non-static predicate, not allowed "
2175 & "in aggregate choice", Choice, E);
2177 elsif not Is_OK_Static_Subtype (E) then
2178 Error_Msg_NE
2179 ("non-static subtype& has predicate, not allowed "
2180 & "in aggregate choice", Choice, E);
2181 end if;
2183 -- If the subtype has a static predicate, replace the
2184 -- original choice with the list of individual values
2185 -- covered by the predicate.
2186 -- This should be deferred to expansion time ???
2188 if Present (Static_Discrete_Predicate (E)) then
2189 Delete_Choice := True;
2191 New_Cs := New_List;
2192 P := First (Static_Discrete_Predicate (E));
2193 while Present (P) loop
2194 C := New_Copy (P);
2195 Set_Sloc (C, Sloc (Choice));
2196 Append_To (New_Cs, C);
2197 Next (P);
2198 end loop;
2200 Insert_List_After (Choice, New_Cs);
2201 end if;
2202 end if;
2203 end;
2204 end if;
2206 Nb_Choices := Nb_Choices + 1;
2208 declare
2209 C : constant Node_Id := Choice;
2211 begin
2212 Next (Choice);
2214 if Delete_Choice then
2215 Remove (C);
2216 Nb_Choices := Nb_Choices - 1;
2217 Delete_Choice := False;
2218 end if;
2219 end;
2220 end loop;
2222 Next (Assoc);
2223 end loop;
2224 end if;
2226 -- At this point we know that the others choice, if present, is by
2227 -- itself and appears last in the aggregate. Check if we have mixed
2228 -- positional and discrete associations (other than the others choice).
2230 if Present (Expressions (N))
2231 and then (Nb_Choices > 1
2232 or else (Nb_Choices = 1 and then not Others_Present))
2233 then
2234 Error_Msg_N
2235 ("cannot mix named and positional associations in array aggregate",
2236 First (Choice_List (First (Component_Associations (N)))));
2237 return Failure;
2238 end if;
2240 -- Test for the validity of an others choice if present
2242 if Others_Present and then not Others_Allowed then
2243 declare
2244 Others_N : constant Node_Id :=
2245 First (Choice_List (First (Component_Associations (N))));
2246 begin
2247 Error_Msg_N ("OTHERS choice not allowed here", Others_N);
2248 Error_Msg_N ("\qualify the aggregate with a constrained subtype "
2249 & "to provide bounds for it", Others_N);
2250 return Failure;
2251 end;
2252 end if;
2254 -- Protect against cascaded errors
2256 if Etype (Index_Typ) = Any_Type then
2257 return Failure;
2258 end if;
2260 -- STEP 2: Process named components
2262 if No (Expressions (N)) then
2263 if Others_Present then
2264 Case_Table_Size := Nb_Choices - 1;
2265 else
2266 Case_Table_Size := Nb_Choices;
2267 end if;
2269 Step_2 : declare
2270 function Empty_Range (A : Node_Id) return Boolean;
2271 -- If an association covers an empty range, some warnings on the
2272 -- expression of the association can be disabled.
2274 -----------------
2275 -- Empty_Range --
2276 -----------------
2278 function Empty_Range (A : Node_Id) return Boolean is
2279 R : Node_Id;
2281 begin
2282 if Nkind (A) = N_Iterated_Component_Association then
2283 R := First (Discrete_Choices (A));
2284 else
2285 R := First (Choices (A));
2286 end if;
2288 return No (Next (R))
2289 and then Nkind (R) = N_Range
2290 and then Compile_Time_Compare
2291 (Low_Bound (R), High_Bound (R), False) = GT;
2292 end Empty_Range;
2294 -- Local variables
2296 Low : Node_Id;
2297 High : Node_Id;
2298 -- Denote the lowest and highest values in an aggregate choice
2300 S_Low : Node_Id := Empty;
2301 S_High : Node_Id := Empty;
2302 -- if a choice in an aggregate is a subtype indication these
2303 -- denote the lowest and highest values of the subtype
2305 Table : Case_Table_Type (1 .. Case_Table_Size);
2306 -- Used to sort all the different choice values
2308 Single_Choice : Boolean;
2309 -- Set to true every time there is a single discrete choice in a
2310 -- discrete association
2312 Prev_Nb_Discrete_Choices : Nat;
2313 -- Used to keep track of the number of discrete choices in the
2314 -- current association.
2316 Errors_Posted_On_Choices : Boolean := False;
2317 -- Keeps track of whether any choices have semantic errors
2319 -- Start of processing for Step_2
2321 begin
2322 -- STEP 2 (A): Check discrete choices validity
2323 -- No need if this is an element iteration.
2325 Assoc := First (Component_Associations (N));
2326 while Present (Assoc)
2327 and then Present (Choice_List (Assoc))
2328 loop
2329 Prev_Nb_Discrete_Choices := Nb_Discrete_Choices;
2330 Choice := First (Choice_List (Assoc));
2332 loop
2333 Analyze (Choice);
2335 if Nkind (Choice) = N_Others_Choice then
2336 Single_Choice := False;
2337 exit;
2339 -- Test for subtype mark without constraint
2341 elsif Is_Entity_Name (Choice) and then
2342 Is_Type (Entity (Choice))
2343 then
2344 if Base_Type (Entity (Choice)) /= Index_Base then
2345 Error_Msg_N
2346 ("invalid subtype mark in aggregate choice",
2347 Choice);
2348 return Failure;
2349 end if;
2351 -- Case of subtype indication
2353 elsif Nkind (Choice) = N_Subtype_Indication then
2354 Resolve_Discrete_Subtype_Indication (Choice, Index_Base);
2356 if Has_Dynamic_Predicate_Aspect
2357 (Entity (Subtype_Mark (Choice)))
2358 or else Has_Ghost_Predicate_Aspect
2359 (Entity (Subtype_Mark (Choice)))
2360 then
2361 Error_Msg_NE
2362 ("subtype& has non-static predicate, "
2363 & "not allowed in aggregate choice",
2364 Choice, Entity (Subtype_Mark (Choice)));
2365 end if;
2367 -- Does the subtype indication evaluation raise CE?
2369 Get_Index_Bounds (Subtype_Mark (Choice), S_Low, S_High);
2370 Get_Index_Bounds (Choice, Low, High);
2371 Check_Bounds (S_Low, S_High, Low, High);
2373 -- Case of range or expression
2375 else
2376 Resolve (Choice, Index_Base);
2377 Check_Unset_Reference (Choice);
2378 Check_Non_Static_Context (Choice);
2380 -- If semantic errors were posted on the choice, then
2381 -- record that for possible early return from later
2382 -- processing (see handling of enumeration choices).
2384 if Error_Posted (Choice) then
2385 Errors_Posted_On_Choices := True;
2386 end if;
2388 -- Do not range check a choice. This check is redundant
2389 -- since this test is already done when we check that the
2390 -- bounds of the array aggregate are within range.
2392 Set_Do_Range_Check (Choice, False);
2393 end if;
2395 -- If we could not resolve the discrete choice stop here
2397 if Etype (Choice) = Any_Type then
2398 return Failure;
2400 -- If the discrete choice raises CE get its original bounds
2402 elsif Nkind (Choice) = N_Raise_Constraint_Error then
2403 Set_Raises_Constraint_Error (N);
2404 Get_Index_Bounds (Original_Node (Choice), Low, High);
2406 -- Otherwise get its bounds as usual
2408 else
2409 Get_Index_Bounds (Choice, Low, High);
2410 end if;
2412 if (Dynamic_Or_Null_Range (Low, High)
2413 or else (Nkind (Choice) = N_Subtype_Indication
2414 and then
2415 Dynamic_Or_Null_Range (S_Low, S_High)))
2416 and then Nb_Choices /= 1
2417 then
2418 Error_Msg_N
2419 ("dynamic or empty choice in aggregate "
2420 & "must be the only choice", Choice);
2421 return Failure;
2422 end if;
2424 if not (All_Composite_Constraints_Static (Low)
2425 and then All_Composite_Constraints_Static (High)
2426 and then All_Composite_Constraints_Static (S_Low)
2427 and then All_Composite_Constraints_Static (S_High))
2428 then
2429 Check_Restriction (No_Dynamic_Sized_Objects, Choice);
2430 end if;
2432 Nb_Discrete_Choices := Nb_Discrete_Choices + 1;
2433 Table (Nb_Discrete_Choices).Lo := Low;
2434 Table (Nb_Discrete_Choices).Hi := High;
2435 Table (Nb_Discrete_Choices).Choice := Choice;
2437 Next (Choice);
2439 if No (Choice) then
2441 -- Check if we have a single discrete choice and whether
2442 -- this discrete choice specifies a single value.
2444 Single_Choice :=
2445 Nb_Discrete_Choices = Prev_Nb_Discrete_Choices + 1
2446 and then Low = High;
2448 exit;
2449 end if;
2450 end loop;
2452 -- Ada 2005 (AI-231)
2454 if Ada_Version >= Ada_2005
2455 and then not Empty_Range (Assoc)
2456 then
2457 if Known_Null (Expression (Assoc)) then
2458 Check_Can_Never_Be_Null (Etype (N), Expression (Assoc));
2460 -- Report warning on iterated component association that may
2461 -- initialize some component of an array of null-excluding
2462 -- access type components with a null value. For example:
2464 -- type AList is array (...) of not null access Integer;
2465 -- L : AList :=
2466 -- [for J in A'Range =>
2467 -- (if Func (J) = 0 then A(J)'Access else Null)];
2469 elsif Ada_Version >= Ada_2022
2470 and then Can_Never_Be_Null (Component_Type (Etype (N)))
2471 and then Nkind (Assoc) = N_Iterated_Component_Association
2472 and then Nkind (Expression (Assoc)) in N_If_Expression
2473 | N_Case_Expression
2474 then
2475 Warn_On_Null_Component_Association (Expression (Assoc));
2476 end if;
2477 end if;
2479 -- Ada 2005 (AI-287): In case of default initialized component
2480 -- we delay the resolution to the expansion phase.
2482 if Box_Present (Assoc) then
2484 -- Ada 2005 (AI-287): In case of default initialization of a
2485 -- component the expander will generate calls to the
2486 -- corresponding initialization subprogram. We need to call
2487 -- Resolve_Aggr_Expr to check the rules about
2488 -- dimensionality.
2490 if not Resolve_Aggr_Expr
2491 (Assoc, Single_Elmt => Single_Choice)
2492 then
2493 return Failure;
2494 end if;
2496 -- ??? Checks for dynamically tagged expressions below will
2497 -- be only applied to iterated_component_association after
2498 -- expansion; in particular, errors might not be reported when
2499 -- -gnatc switch is used.
2501 elsif Nkind (Assoc) = N_Iterated_Component_Association then
2502 null; -- handled above, in a loop context
2504 elsif not Resolve_Aggr_Expr
2505 (Expression (Assoc), Single_Elmt => Single_Choice)
2506 then
2507 return Failure;
2509 -- Check incorrect use of dynamically tagged expression
2511 -- We differentiate here two cases because the expression may
2512 -- not be decorated. For example, the analysis and resolution
2513 -- of the expression associated with the others choice will be
2514 -- done later with the full aggregate. In such case we
2515 -- duplicate the expression tree to analyze the copy and
2516 -- perform the required check.
2518 elsif No (Etype (Expression (Assoc))) then
2519 declare
2520 Save_Analysis : constant Boolean := Full_Analysis;
2521 Expr : constant Node_Id :=
2522 New_Copy_Tree (Expression (Assoc));
2524 begin
2525 Expander_Mode_Save_And_Set (False);
2526 Full_Analysis := False;
2528 -- Analyze the expression, making sure it is properly
2529 -- attached to the tree before we do the analysis.
2531 Set_Parent (Expr, Parent (Expression (Assoc)));
2532 Analyze (Expr);
2534 -- Compute its dimensions now, rather than at the end of
2535 -- resolution, because in the case of multidimensional
2536 -- aggregates subsequent expansion may lead to spurious
2537 -- errors.
2539 Check_Expression_Dimensions (Expr, Component_Typ);
2541 -- If the expression is a literal, propagate this info
2542 -- to the expression in the association, to enable some
2543 -- optimizations downstream.
2545 if Is_Entity_Name (Expr)
2546 and then Present (Entity (Expr))
2547 and then Ekind (Entity (Expr)) = E_Enumeration_Literal
2548 then
2549 Analyze_And_Resolve
2550 (Expression (Assoc), Component_Typ);
2551 end if;
2553 Full_Analysis := Save_Analysis;
2554 Expander_Mode_Restore;
2556 if Is_Tagged_Type (Etype (Expr)) then
2557 Check_Dynamically_Tagged_Expression
2558 (Expr => Expr,
2559 Typ => Component_Type (Etype (N)),
2560 Related_Nod => N);
2561 end if;
2562 end;
2564 elsif Is_Tagged_Type (Etype (Expression (Assoc))) then
2565 Check_Dynamically_Tagged_Expression
2566 (Expr => Expression (Assoc),
2567 Typ => Component_Type (Etype (N)),
2568 Related_Nod => N);
2569 end if;
2571 Next (Assoc);
2572 end loop;
2574 -- If aggregate contains more than one choice then these must be
2575 -- static. Check for duplicate and missing values.
2577 -- Note: there is duplicated code here wrt Check_Choice_Set in
2578 -- the body of Sem_Case, and it is possible we could just reuse
2579 -- that procedure. To be checked ???
2581 if Nb_Discrete_Choices > 1 then
2582 Check_Choices : declare
2583 Choice : Node_Id;
2584 -- Location of choice for messages
2586 Hi_Val : Uint;
2587 Lo_Val : Uint;
2588 -- High end of one range and Low end of the next. Should be
2589 -- contiguous if there is no hole in the list of values.
2591 Lo_Dup : Uint;
2592 Hi_Dup : Uint;
2593 -- End points of duplicated range
2595 Missing_Or_Duplicates : Boolean := False;
2596 -- Set True if missing or duplicate choices found
2598 procedure Output_Bad_Choices (Lo, Hi : Uint; C : Node_Id);
2599 -- Output continuation message with a representation of the
2600 -- bounds (just Lo if Lo = Hi, else Lo .. Hi). C is the
2601 -- choice node where the message is to be posted.
2603 ------------------------
2604 -- Output_Bad_Choices --
2605 ------------------------
2607 procedure Output_Bad_Choices (Lo, Hi : Uint; C : Node_Id) is
2608 begin
2609 -- Enumeration type case
2611 if Is_Enumeration_Type (Index_Typ) then
2612 Error_Msg_Name_1 :=
2613 Chars (Get_Enum_Lit_From_Pos (Index_Typ, Lo, Loc));
2614 Error_Msg_Name_2 :=
2615 Chars (Get_Enum_Lit_From_Pos (Index_Typ, Hi, Loc));
2617 if Lo = Hi then
2618 Error_Msg_N ("\\ %!", C);
2619 else
2620 Error_Msg_N ("\\ % .. %!", C);
2621 end if;
2623 -- Integer types case
2625 else
2626 Error_Msg_Uint_1 := Lo;
2627 Error_Msg_Uint_2 := Hi;
2629 if Lo = Hi then
2630 Error_Msg_N ("\\ ^!", C);
2631 else
2632 Error_Msg_N ("\\ ^ .. ^!", C);
2633 end if;
2634 end if;
2635 end Output_Bad_Choices;
2637 -- Start of processing for Check_Choices
2639 begin
2640 Sort_Case_Table (Table);
2642 -- First we do a quick linear loop to find out if we have
2643 -- any duplicates or missing entries (usually we have a
2644 -- legal aggregate, so this will get us out quickly).
2646 for J in 1 .. Nb_Discrete_Choices - 1 loop
2647 Hi_Val := Expr_Value (Table (J).Hi);
2648 Lo_Val := Expr_Value (Table (J + 1).Lo);
2650 if Lo_Val <= Hi_Val
2651 or else (Lo_Val > Hi_Val + 1
2652 and then not Others_Present)
2653 then
2654 Missing_Or_Duplicates := True;
2655 exit;
2656 end if;
2657 end loop;
2659 -- If we have missing or duplicate entries, first fill in
2660 -- the Highest entries to make life easier in the following
2661 -- loops to detect bad entries.
2663 if Missing_Or_Duplicates then
2664 Table (1).Highest := Expr_Value (Table (1).Hi);
2666 for J in 2 .. Nb_Discrete_Choices loop
2667 Table (J).Highest :=
2668 UI_Max
2669 (Table (J - 1).Highest, Expr_Value (Table (J).Hi));
2670 end loop;
2672 -- Loop through table entries to find duplicate indexes
2674 for J in 2 .. Nb_Discrete_Choices loop
2675 Lo_Val := Expr_Value (Table (J).Lo);
2676 Hi_Val := Expr_Value (Table (J).Hi);
2678 -- Case where we have duplicates (the lower bound of
2679 -- this choice is less than or equal to the highest
2680 -- high bound found so far).
2682 if Lo_Val <= Table (J - 1).Highest then
2684 -- We move backwards looking for duplicates. We can
2685 -- abandon this loop as soon as we reach a choice
2686 -- highest value that is less than Lo_Val.
2688 for K in reverse 1 .. J - 1 loop
2689 exit when Table (K).Highest < Lo_Val;
2691 -- Here we may have duplicates between entries
2692 -- for K and J. Get range of duplicates.
2694 Lo_Dup :=
2695 UI_Max (Lo_Val, Expr_Value (Table (K).Lo));
2696 Hi_Dup :=
2697 UI_Min (Hi_Val, Expr_Value (Table (K).Hi));
2699 -- Nothing to do if duplicate range is null
2701 if Lo_Dup > Hi_Dup then
2702 null;
2704 -- Otherwise place proper message
2706 else
2707 -- We place message on later choice, with a
2708 -- line reference to the earlier choice.
2710 if Sloc (Table (J).Choice) <
2711 Sloc (Table (K).Choice)
2712 then
2713 Choice := Table (K).Choice;
2714 Error_Msg_Sloc := Sloc (Table (J).Choice);
2715 else
2716 Choice := Table (J).Choice;
2717 Error_Msg_Sloc := Sloc (Table (K).Choice);
2718 end if;
2720 if Lo_Dup = Hi_Dup then
2721 Error_Msg_N
2722 ("index value in array aggregate "
2723 & "duplicates the one given#!", Choice);
2724 else
2725 Error_Msg_N
2726 ("index values in array aggregate "
2727 & "duplicate those given#!", Choice);
2728 end if;
2730 Output_Bad_Choices (Lo_Dup, Hi_Dup, Choice);
2731 end if;
2732 end loop;
2733 end if;
2734 end loop;
2736 -- Loop through entries in table to find missing indexes.
2737 -- Not needed if others, since missing impossible.
2739 if not Others_Present then
2740 for J in 2 .. Nb_Discrete_Choices loop
2741 Lo_Val := Expr_Value (Table (J).Lo);
2742 Hi_Val := Table (J - 1).Highest;
2744 if Lo_Val > Hi_Val + 1 then
2746 declare
2747 Error_Node : Node_Id;
2749 begin
2750 -- If the choice is the bound of a range in
2751 -- a subtype indication, it is not in the
2752 -- source lists for the aggregate itself, so
2753 -- post the error on the aggregate. Otherwise
2754 -- post it on choice itself.
2756 Choice := Table (J).Choice;
2758 if Is_List_Member (Choice) then
2759 Error_Node := Choice;
2760 else
2761 Error_Node := N;
2762 end if;
2764 if Hi_Val + 1 = Lo_Val - 1 then
2765 Error_Msg_N
2766 ("missing index value "
2767 & "in array aggregate!", Error_Node);
2768 else
2769 Error_Msg_N
2770 ("missing index values "
2771 & "in array aggregate!", Error_Node);
2772 end if;
2774 Output_Bad_Choices
2775 (Hi_Val + 1, Lo_Val - 1, Error_Node);
2776 end;
2777 end if;
2778 end loop;
2779 end if;
2781 -- If either missing or duplicate values, return failure
2783 Set_Etype (N, Any_Composite);
2784 return Failure;
2785 end if;
2786 end Check_Choices;
2787 end if;
2789 if Has_Iterator_Specifications then
2790 -- Bounds will be determined dynamically.
2792 return Success;
2793 end if;
2795 -- STEP 2 (B): Compute aggregate bounds and min/max choices values
2797 if Nb_Discrete_Choices > 0 then
2798 Choices_Low := Table (1).Lo;
2799 Choices_High := Table (Nb_Discrete_Choices).Hi;
2800 end if;
2802 -- If Others is present, then bounds of aggregate come from the
2803 -- index constraint (not the choices in the aggregate itself).
2805 if Others_Present then
2806 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2808 -- Abandon processing if either bound is already signalled as
2809 -- an error (prevents junk cascaded messages and blow ups).
2811 if Nkind (Aggr_Low) = N_Error
2812 or else
2813 Nkind (Aggr_High) = N_Error
2814 then
2815 return False;
2816 end if;
2818 -- No others clause present
2820 else
2821 -- Special processing if others allowed and not present. This
2822 -- means that the bounds of the aggregate come from the index
2823 -- constraint (and the length must match).
2825 if Others_Allowed then
2826 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2828 -- Abandon processing if either bound is already signalled
2829 -- as an error (stop junk cascaded messages and blow ups).
2831 if Nkind (Aggr_Low) = N_Error
2832 or else
2833 Nkind (Aggr_High) = N_Error
2834 then
2835 return False;
2836 end if;
2838 -- If others allowed, and no others present, then the array
2839 -- should cover all index values. If it does not, we will
2840 -- get a length check warning, but there is two cases where
2841 -- an additional warning is useful:
2843 -- If we have no positional components, and the length is
2844 -- wrong (which we can tell by others being allowed with
2845 -- missing components), and the index type is an enumeration
2846 -- type, then issue appropriate warnings about these missing
2847 -- components. They are only warnings, since the aggregate
2848 -- is fine, it's just the wrong length. We skip this check
2849 -- for standard character types (since there are no literals
2850 -- and it is too much trouble to concoct them), and also if
2851 -- any of the bounds have values that are not known at
2852 -- compile time.
2854 -- Another case warranting a warning is when the length
2855 -- is right, but as above we have an index type that is
2856 -- an enumeration, and the bounds do not match. This is a
2857 -- case where dubious sliding is allowed and we generate a
2858 -- warning that the bounds do not match.
2860 if No (Expressions (N))
2861 and then Nkind (Index) = N_Range
2862 and then Is_Enumeration_Type (Etype (Index))
2863 and then not Is_Standard_Character_Type (Etype (Index))
2864 and then Compile_Time_Known_Value (Aggr_Low)
2865 and then Compile_Time_Known_Value (Aggr_High)
2866 and then Compile_Time_Known_Value (Choices_Low)
2867 and then Compile_Time_Known_Value (Choices_High)
2868 then
2869 -- If any of the expressions or range bounds in choices
2870 -- have semantic errors, then do not attempt further
2871 -- resolution, to prevent cascaded errors.
2873 if Errors_Posted_On_Choices then
2874 return Failure;
2875 end if;
2877 declare
2878 ALo : constant Node_Id := Expr_Value_E (Aggr_Low);
2879 AHi : constant Node_Id := Expr_Value_E (Aggr_High);
2880 CLo : constant Node_Id := Expr_Value_E (Choices_Low);
2881 CHi : constant Node_Id := Expr_Value_E (Choices_High);
2883 Ent : Entity_Id;
2885 begin
2886 -- Warning case 1, missing values at start/end. Only
2887 -- do the check if the number of entries is too small.
2889 if (Enumeration_Pos (CHi) - Enumeration_Pos (CLo))
2891 (Enumeration_Pos (AHi) - Enumeration_Pos (ALo))
2892 then
2893 Error_Msg_N
2894 ("missing index value(s) in array aggregate??",
2897 -- Output missing value(s) at start
2899 if Chars (ALo) /= Chars (CLo) then
2900 Ent := Prev (CLo);
2902 if Chars (ALo) = Chars (Ent) then
2903 Error_Msg_Name_1 := Chars (ALo);
2904 Error_Msg_N ("\ %??", N);
2905 else
2906 Error_Msg_Name_1 := Chars (ALo);
2907 Error_Msg_Name_2 := Chars (Ent);
2908 Error_Msg_N ("\ % .. %??", N);
2909 end if;
2910 end if;
2912 -- Output missing value(s) at end
2914 if Chars (AHi) /= Chars (CHi) then
2915 Ent := Next (CHi);
2917 if Chars (AHi) = Chars (Ent) then
2918 Error_Msg_Name_1 := Chars (Ent);
2919 Error_Msg_N ("\ %??", N);
2920 else
2921 Error_Msg_Name_1 := Chars (Ent);
2922 Error_Msg_Name_2 := Chars (AHi);
2923 Error_Msg_N ("\ % .. %??", N);
2924 end if;
2925 end if;
2927 -- Warning case 2, dubious sliding. The First_Subtype
2928 -- test distinguishes between a constrained type where
2929 -- sliding is not allowed (so we will get a warning
2930 -- later that Constraint_Error will be raised), and
2931 -- the unconstrained case where sliding is permitted.
2933 elsif (Enumeration_Pos (CHi) - Enumeration_Pos (CLo))
2935 (Enumeration_Pos (AHi) - Enumeration_Pos (ALo))
2936 and then Chars (ALo) /= Chars (CLo)
2937 and then
2938 not Is_Constrained (First_Subtype (Etype (N)))
2939 then
2940 Error_Msg_N
2941 ("bounds of aggregate do not match target??", N);
2942 end if;
2943 end;
2944 end if;
2945 end if;
2947 -- If no others, aggregate bounds come from aggregate
2949 Aggr_Low := Choices_Low;
2950 Aggr_High := Choices_High;
2951 end if;
2952 end Step_2;
2954 -- STEP 3: Process positional components
2956 else
2957 -- STEP 3 (A): Process positional elements
2959 Expr := First (Expressions (N));
2960 Nb_Elements := Uint_0;
2961 while Present (Expr) loop
2962 Nb_Elements := Nb_Elements + 1;
2964 -- Ada 2005 (AI-231)
2966 if Ada_Version >= Ada_2005 and then Known_Null (Expr) then
2967 Check_Can_Never_Be_Null (Etype (N), Expr);
2968 end if;
2970 if not Resolve_Aggr_Expr (Expr, Single_Elmt => True) then
2971 return Failure;
2972 end if;
2974 -- Check incorrect use of dynamically tagged expression
2976 if Is_Tagged_Type (Etype (Expr)) then
2977 Check_Dynamically_Tagged_Expression
2978 (Expr => Expr,
2979 Typ => Component_Type (Etype (N)),
2980 Related_Nod => N);
2981 end if;
2983 Next (Expr);
2984 end loop;
2986 if Others_Present then
2987 Assoc := Last (Component_Associations (N));
2989 -- Ada 2005 (AI-231)
2991 if Ada_Version >= Ada_2005 and then Known_Null (Assoc) then
2992 Check_Can_Never_Be_Null (Etype (N), Expression (Assoc));
2993 end if;
2995 -- Ada 2005 (AI-287): In case of default initialized component,
2996 -- we delay the resolution to the expansion phase.
2998 if Box_Present (Assoc) then
3000 -- Ada 2005 (AI-287): In case of default initialization of a
3001 -- component the expander will generate calls to the
3002 -- corresponding initialization subprogram. We need to call
3003 -- Resolve_Aggr_Expr to check the rules about
3004 -- dimensionality.
3006 if not Resolve_Aggr_Expr (Assoc, Single_Elmt => False) then
3007 return Failure;
3008 end if;
3010 elsif not Resolve_Aggr_Expr (Expression (Assoc),
3011 Single_Elmt => False)
3012 then
3013 return Failure;
3015 -- Check incorrect use of dynamically tagged expression. The
3016 -- expression of the others choice has not been resolved yet.
3017 -- In order to diagnose the semantic error we create a duplicate
3018 -- tree to analyze it and perform the check.
3020 elsif Nkind (Assoc) /= N_Iterated_Component_Association then
3021 declare
3022 Save_Analysis : constant Boolean := Full_Analysis;
3023 Expr : constant Node_Id :=
3024 New_Copy_Tree (Expression (Assoc));
3026 begin
3027 Expander_Mode_Save_And_Set (False);
3028 Full_Analysis := False;
3029 Analyze (Expr);
3030 Full_Analysis := Save_Analysis;
3031 Expander_Mode_Restore;
3033 if Is_Tagged_Type (Etype (Expr)) then
3034 Check_Dynamically_Tagged_Expression
3035 (Expr => Expr,
3036 Typ => Component_Type (Etype (N)),
3037 Related_Nod => N);
3038 end if;
3039 end;
3040 end if;
3041 end if;
3043 -- STEP 3 (B): Compute the aggregate bounds
3045 if Others_Present then
3046 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
3048 else
3049 if Others_Allowed then
3050 Get_Index_Bounds (Index_Constr, Aggr_Low, Discard);
3051 else
3052 Aggr_Low := Index_Typ_Low;
3053 end if;
3055 Aggr_High := Add (Nb_Elements - 1, To => Aggr_Low);
3056 Check_Bound (Index_Base_High, Aggr_High);
3057 end if;
3058 end if;
3060 -- STEP 4: Perform static aggregate checks and save the bounds
3062 -- Check (A)
3064 Check_Bounds (Index_Typ_Low, Index_Typ_High, Aggr_Low, Aggr_High);
3065 Check_Bounds (Index_Base_Low, Index_Base_High, Aggr_Low, Aggr_High);
3067 -- Check (B)
3069 if Others_Present and then Nb_Discrete_Choices > 0 then
3070 Check_Bounds (Aggr_Low, Aggr_High, Choices_Low, Choices_High);
3071 Check_Bounds (Index_Typ_Low, Index_Typ_High,
3072 Choices_Low, Choices_High);
3073 Check_Bounds (Index_Base_Low, Index_Base_High,
3074 Choices_Low, Choices_High);
3076 -- Check (C)
3078 elsif Others_Present and then Nb_Elements > 0 then
3079 Check_Length (Aggr_Low, Aggr_High, Nb_Elements);
3080 Check_Length (Index_Typ_Low, Index_Typ_High, Nb_Elements);
3081 Check_Length (Index_Base_Low, Index_Base_High, Nb_Elements);
3082 end if;
3084 if Raises_Constraint_Error (Aggr_Low)
3085 or else Raises_Constraint_Error (Aggr_High)
3086 then
3087 Set_Raises_Constraint_Error (N);
3088 end if;
3090 Aggr_Low := Duplicate_Subexpr (Aggr_Low);
3092 -- Do not duplicate Aggr_High if Aggr_High = Aggr_Low + Nb_Elements
3093 -- since the addition node returned by Add is not yet analyzed. Attach
3094 -- to tree and analyze first. Reset analyzed flag to ensure it will get
3095 -- analyzed when it is a literal bound whose type must be properly set.
3097 if Others_Present or else Nb_Discrete_Choices > 0 then
3098 Aggr_High := Duplicate_Subexpr (Aggr_High);
3100 if Etype (Aggr_High) = Universal_Integer then
3101 Set_Analyzed (Aggr_High, False);
3102 end if;
3103 end if;
3105 -- If the aggregate already has bounds attached to it, it means this is
3106 -- a positional aggregate created as an optimization by
3107 -- Exp_Aggr.Convert_To_Positional, so we don't want to change those
3108 -- bounds.
3110 if Present (Aggregate_Bounds (N))
3111 and then not Others_Allowed
3112 and then not Comes_From_Source (N)
3113 then
3114 Aggr_Low := Low_Bound (Aggregate_Bounds (N));
3115 Aggr_High := High_Bound (Aggregate_Bounds (N));
3116 end if;
3118 Set_Aggregate_Bounds
3119 (N, Make_Range (Loc, Low_Bound => Aggr_Low, High_Bound => Aggr_High));
3121 -- The bounds may contain expressions that must be inserted upwards.
3122 -- Attach them fully to the tree. After analysis, remove side effects
3123 -- from upper bound, if still needed.
3125 Set_Parent (Aggregate_Bounds (N), N);
3126 Analyze_And_Resolve (Aggregate_Bounds (N), Index_Typ);
3127 Check_Unset_Reference (Aggregate_Bounds (N));
3129 if not Others_Present and then Nb_Discrete_Choices = 0 then
3130 Set_High_Bound
3131 (Aggregate_Bounds (N),
3132 Duplicate_Subexpr (High_Bound (Aggregate_Bounds (N))));
3133 end if;
3135 -- Check the dimensions of each component in the array aggregate
3137 Analyze_Dimension_Array_Aggregate (N, Component_Typ);
3139 return Success;
3140 end Resolve_Array_Aggregate;
3142 ---------------------------------
3143 -- Resolve_Container_Aggregate --
3144 ---------------------------------
3146 procedure Resolve_Container_Aggregate (N : Node_Id; Typ : Entity_Id) is
3147 procedure Resolve_Iterated_Association
3148 (Comp : Node_Id;
3149 Key_Type : Entity_Id;
3150 Elmt_Type : Entity_Id);
3151 -- Resolve choices and expression in an iterated component association
3152 -- or an iterated element association, which has a key_expression.
3153 -- This is similar but not identical to the handling of this construct
3154 -- in an array aggregate.
3155 -- For a named container, the type of each choice must be compatible
3156 -- with the key type. For a positional container, the choice must be
3157 -- a subtype indication or an iterator specification that determines
3158 -- an element type.
3160 Asp : constant Node_Id := Find_Value_Of_Aspect (Typ, Aspect_Aggregate);
3162 Empty_Subp : Node_Id := Empty;
3163 Add_Named_Subp : Node_Id := Empty;
3164 Add_Unnamed_Subp : Node_Id := Empty;
3165 New_Indexed_Subp : Node_Id := Empty;
3166 Assign_Indexed_Subp : Node_Id := Empty;
3168 ----------------------------------
3169 -- Resolve_Iterated_Association --
3170 ----------------------------------
3172 procedure Resolve_Iterated_Association
3173 (Comp : Node_Id;
3174 Key_Type : Entity_Id;
3175 Elmt_Type : Entity_Id)
3177 Loc : constant Source_Ptr := Sloc (N);
3178 Choice : Node_Id;
3179 Copy : Node_Id;
3180 Ent : Entity_Id;
3181 Expr : Node_Id;
3182 Key_Expr : Node_Id;
3183 Id : Entity_Id;
3184 Id_Name : Name_Id;
3185 Typ : Entity_Id := Empty;
3187 begin
3188 Error_Msg_Ada_2022_Feature ("iterated component", Loc);
3190 -- If this is an Iterated_Element_Association then either a
3191 -- an Iterator_Specification or a Loop_Parameter specification
3192 -- is present. In both cases a Key_Expression is present.
3194 if Nkind (Comp) = N_Iterated_Element_Association then
3196 -- Create a temporary scope to avoid some modifications from
3197 -- escaping the Analyze call below. The original Tree will be
3198 -- reanalyzed later.
3200 Ent := New_Internal_Entity
3201 (E_Loop, Current_Scope, Sloc (Comp), 'L');
3202 Set_Etype (Ent, Standard_Void_Type);
3203 Set_Parent (Ent, Parent (Comp));
3204 Push_Scope (Ent);
3206 if Present (Loop_Parameter_Specification (Comp)) then
3207 Copy := Copy_Separate_Tree (Comp);
3209 Analyze
3210 (Loop_Parameter_Specification (Copy));
3212 Id_Name := Chars (Defining_Identifier
3213 (Loop_Parameter_Specification (Comp)));
3214 else
3215 Copy := Copy_Separate_Tree (Iterator_Specification (Comp));
3216 Analyze (Copy);
3218 Id_Name := Chars (Defining_Identifier
3219 (Iterator_Specification (Comp)));
3220 end if;
3222 -- Key expression must have the type of the key. We analyze
3223 -- a copy of the original expression, because it will be
3224 -- reanalyzed and copied as needed during expansion of the
3225 -- corresponding loop.
3227 Key_Expr := Key_Expression (Comp);
3228 Analyze_And_Resolve (New_Copy_Tree (Key_Expr), Key_Type);
3229 End_Scope;
3231 Typ := Key_Type;
3233 elsif Present (Iterator_Specification (Comp)) then
3234 -- Create a temporary scope to avoid some modifications from
3235 -- escaping the Analyze call below. The original Tree will be
3236 -- reanalyzed later.
3238 Ent := New_Internal_Entity
3239 (E_Loop, Current_Scope, Sloc (Comp), 'L');
3240 Set_Etype (Ent, Standard_Void_Type);
3241 Set_Parent (Ent, Parent (Comp));
3242 Push_Scope (Ent);
3244 Copy := Copy_Separate_Tree (Iterator_Specification (Comp));
3245 Id_Name :=
3246 Chars (Defining_Identifier (Iterator_Specification (Comp)));
3248 Preanalyze (Copy);
3250 End_Scope;
3252 Typ := Etype (Defining_Identifier (Copy));
3254 else
3255 Choice := First (Discrete_Choices (Comp));
3257 while Present (Choice) loop
3258 Analyze (Choice);
3260 -- Choice can be a subtype name, a range, or an expression
3262 if Is_Entity_Name (Choice)
3263 and then Is_Type (Entity (Choice))
3264 and then Base_Type (Entity (Choice)) = Base_Type (Key_Type)
3265 then
3266 null;
3268 elsif Present (Key_Type) then
3269 Analyze_And_Resolve (Choice, Key_Type);
3270 Typ := Key_Type;
3271 else
3272 Typ := Etype (Choice); -- assume unique for now
3273 end if;
3275 Next (Choice);
3276 end loop;
3278 Id_Name := Chars (Defining_Identifier (Comp));
3279 end if;
3281 -- Create a scope in which to introduce an index, which is usually
3282 -- visible in the expression for the component, and needed for its
3283 -- analysis.
3285 Id := Make_Defining_Identifier (Sloc (Comp), Id_Name);
3286 Ent := New_Internal_Entity (E_Loop,
3287 Current_Scope, Sloc (Comp), 'L');
3288 Set_Etype (Ent, Standard_Void_Type);
3289 Set_Parent (Ent, Parent (Comp));
3290 Push_Scope (Ent);
3292 -- Insert and decorate the loop variable in the current scope.
3293 -- The expression has to be analyzed once the loop variable is
3294 -- directly visible. Mark the variable as referenced to prevent
3295 -- spurious warnings, given that subsequent uses of its name in the
3296 -- expression will reference the internal (synonym) loop variable.
3298 Enter_Name (Id);
3300 pragma Assert (Present (Typ));
3301 Set_Etype (Id, Typ);
3303 Mutate_Ekind (Id, E_Variable);
3304 Set_Is_Not_Self_Hidden (Id);
3305 Set_Scope (Id, Ent);
3306 Set_Referenced (Id);
3308 -- Analyze a copy of the expression, to verify legality. We use
3309 -- a copy because the expression will be analyzed anew when the
3310 -- enclosing aggregate is expanded, and the construct is rewritten
3311 -- as a loop with a new index variable.
3313 Expr := New_Copy_Tree (Expression (Comp));
3314 Preanalyze_And_Resolve (Expr, Elmt_Type);
3315 End_Scope;
3317 end Resolve_Iterated_Association;
3319 -- Start of processing for Resolve_Container_Aggregate
3321 begin
3322 pragma Assert (Nkind (Asp) = N_Aggregate);
3324 Set_Etype (N, Typ);
3325 Parse_Aspect_Aggregate (Asp,
3326 Empty_Subp, Add_Named_Subp, Add_Unnamed_Subp,
3327 New_Indexed_Subp, Assign_Indexed_Subp);
3329 if Present (Add_Unnamed_Subp)
3330 and then No (New_Indexed_Subp)
3331 and then Etype (Add_Unnamed_Subp) /= Any_Type
3332 then
3333 declare
3334 Elmt_Type : constant Entity_Id :=
3335 Etype (Next_Formal
3336 (First_Formal (Entity (Add_Unnamed_Subp))));
3337 Comp : Node_Id;
3339 begin
3340 if Present (Expressions (N)) then
3341 -- positional aggregate
3343 Comp := First (Expressions (N));
3344 while Present (Comp) loop
3345 Analyze_And_Resolve (Comp, Elmt_Type);
3346 Next (Comp);
3347 end loop;
3348 end if;
3350 -- Empty aggregate, to be replaced by Empty during
3351 -- expansion, or iterated component association.
3353 if Present (Component_Associations (N)) then
3354 declare
3355 Comp : Node_Id := First (Component_Associations (N));
3356 begin
3357 while Present (Comp) loop
3358 if Nkind (Comp) /=
3359 N_Iterated_Component_Association
3360 then
3361 Error_Msg_N ("illegal component association "
3362 & "for unnamed container aggregate", Comp);
3363 return;
3364 else
3365 Resolve_Iterated_Association
3366 (Comp, Empty, Elmt_Type);
3367 end if;
3369 Next (Comp);
3370 end loop;
3371 end;
3372 end if;
3373 end;
3375 elsif Present (Add_Named_Subp)
3376 and then Etype (Add_Named_Subp) /= Any_Type
3377 then
3378 declare
3379 -- Retrieves types of container, key, and element from the
3380 -- specified insertion procedure.
3382 Container : constant Entity_Id :=
3383 First_Formal (Entity (Add_Named_Subp));
3384 Key_Type : constant Entity_Id := Etype (Next_Formal (Container));
3385 Elmt_Type : constant Entity_Id :=
3386 Etype (Next_Formal (Next_Formal (Container)));
3387 Comp : Node_Id;
3388 Choice : Node_Id;
3390 begin
3391 Comp := First (Component_Associations (N));
3392 while Present (Comp) loop
3393 if Nkind (Comp) = N_Component_Association then
3394 Choice := First (Choices (Comp));
3396 while Present (Choice) loop
3397 Analyze_And_Resolve (Choice, Key_Type);
3398 if not Is_Static_Expression (Choice) then
3399 Error_Msg_N ("choice must be static", Choice);
3400 end if;
3402 Next (Choice);
3403 end loop;
3405 Analyze_And_Resolve (Expression (Comp), Elmt_Type);
3407 elsif Nkind (Comp) in
3408 N_Iterated_Component_Association |
3409 N_Iterated_Element_Association
3410 then
3411 Resolve_Iterated_Association
3412 (Comp, Key_Type, Elmt_Type);
3413 end if;
3415 Next (Comp);
3416 end loop;
3417 end;
3419 elsif Present (Assign_Indexed_Subp)
3420 and then Etype (Assign_Indexed_Subp) /= Any_Type
3421 then
3422 -- Indexed Aggregate. Positional or indexed component
3423 -- can be present, but not both. Choices must be static
3424 -- values or ranges with static bounds.
3426 declare
3427 Container : constant Entity_Id :=
3428 First_Formal (Entity (Assign_Indexed_Subp));
3429 Index_Type : constant Entity_Id := Etype (Next_Formal (Container));
3430 Comp_Type : constant Entity_Id :=
3431 Etype (Next_Formal (Next_Formal (Container)));
3432 Comp : Node_Id;
3433 Choice : Node_Id;
3434 Num_Choices : Nat := 0;
3436 Hi_Val : Uint;
3437 Lo_Val : Uint;
3438 begin
3439 if Present (Expressions (N)) then
3440 Comp := First (Expressions (N));
3441 while Present (Comp) loop
3442 Analyze_And_Resolve (Comp, Comp_Type);
3443 Next (Comp);
3444 end loop;
3445 end if;
3447 if Present (Component_Associations (N))
3448 and then not Is_Empty_List (Component_Associations (N))
3449 then
3450 if Present (Expressions (N))
3451 and then not Is_Empty_List (Expressions (N))
3452 then
3453 Error_Msg_N ("container aggregate cannot be "
3454 & "both positional and named", N);
3455 return;
3456 end if;
3458 Comp := First (Component_Associations (N));
3460 while Present (Comp) loop
3461 if Nkind (Comp) = N_Component_Association then
3462 Choice := First (Choices (Comp));
3464 while Present (Choice) loop
3465 Analyze_And_Resolve (Choice, Index_Type);
3466 Num_Choices := Num_Choices + 1;
3467 Next (Choice);
3468 end loop;
3470 Analyze_And_Resolve (Expression (Comp), Comp_Type);
3472 elsif Nkind (Comp) in
3473 N_Iterated_Component_Association |
3474 N_Iterated_Element_Association
3475 then
3476 Resolve_Iterated_Association
3477 (Comp, Index_Type, Comp_Type);
3478 Num_Choices := Num_Choices + 1;
3479 end if;
3481 Next (Comp);
3482 end loop;
3484 -- The component associations in an indexed aggregate
3485 -- must denote a contiguous set of static values. We
3486 -- build a table of values/ranges and sort it, as is done
3487 -- elsewhere for case statements and array aggregates.
3488 -- If the aggregate has a single iterated association it
3489 -- is allowed to be nonstatic and there is nothing to check.
3491 if Num_Choices > 1 then
3492 declare
3493 Table : Case_Table_Type (1 .. Num_Choices);
3494 No_Choice : Pos := 1;
3495 Lo, Hi : Node_Id;
3497 -- Traverse aggregate to determine size of needed table.
3498 -- Verify that bounds are static and that loops have no
3499 -- filters or key expressions.
3501 begin
3502 Comp := First (Component_Associations (N));
3503 while Present (Comp) loop
3504 if Nkind (Comp) = N_Iterated_Element_Association then
3505 if Present
3506 (Loop_Parameter_Specification (Comp))
3507 then
3508 if Present (Iterator_Filter
3509 (Loop_Parameter_Specification (Comp)))
3510 then
3511 Error_Msg_N
3512 ("iterator filter not allowed " &
3513 "in indexed aggregate", Comp);
3514 return;
3516 elsif Present (Key_Expression
3517 (Loop_Parameter_Specification (Comp)))
3518 then
3519 Error_Msg_N
3520 ("key expression not allowed " &
3521 "in indexed aggregate", Comp);
3522 return;
3523 end if;
3524 end if;
3525 else
3526 Choice := First (Choices (Comp));
3528 while Present (Choice) loop
3529 Get_Index_Bounds (Choice, Lo, Hi);
3530 Table (No_Choice).Choice := Choice;
3531 Table (No_Choice).Lo := Lo;
3532 Table (No_Choice).Hi := Hi;
3534 -- Verify staticness of value or range
3536 if not Is_Static_Expression (Lo)
3537 or else not Is_Static_Expression (Hi)
3538 then
3539 Error_Msg_N
3540 ("nonstatic expression for index " &
3541 "for indexed aggregate", Choice);
3542 return;
3543 end if;
3545 No_Choice := No_Choice + 1;
3546 Next (Choice);
3547 end loop;
3548 end if;
3550 Next (Comp);
3551 end loop;
3553 Sort_Case_Table (Table);
3555 for J in 1 .. Num_Choices - 1 loop
3556 Hi_Val := Expr_Value (Table (J).Hi);
3557 Lo_Val := Expr_Value (Table (J + 1).Lo);
3559 if Lo_Val = Hi_Val then
3560 Error_Msg_N
3561 ("duplicate index in indexed aggregate",
3562 Table (J + 1).Choice);
3563 exit;
3565 elsif Lo_Val < Hi_Val then
3566 Error_Msg_N
3567 ("overlapping indices in indexed aggregate",
3568 Table (J + 1).Choice);
3569 exit;
3571 elsif Lo_Val > Hi_Val + 1 then
3572 Error_Msg_N
3573 ("missing index values", Table (J + 1).Choice);
3574 exit;
3575 end if;
3576 end loop;
3577 end;
3578 end if;
3579 end if;
3580 end;
3581 end if;
3582 end Resolve_Container_Aggregate;
3584 -----------------------------
3585 -- Resolve_Delta_Aggregate --
3586 -----------------------------
3588 procedure Resolve_Delta_Aggregate (N : Node_Id; Typ : Entity_Id) is
3589 Base : constant Node_Id := Expression (N);
3591 begin
3592 Error_Msg_Ada_2022_Feature ("delta aggregate", Sloc (N));
3594 if not Is_Composite_Type (Typ) then
3595 Error_Msg_N ("not a composite type", N);
3596 end if;
3598 Analyze_And_Resolve (Base, Typ);
3600 if Is_Array_Type (Typ) then
3601 -- For an array_delta_aggregate, the base_expression and each
3602 -- expression in every array_component_association shall be of a
3603 -- nonlimited type; RM 4.3.4(13/5). However, to prevent repeated
3604 -- errors we only check the base expression and not array component
3605 -- associations.
3607 if Is_Limited_Type (Etype (Base)) then
3608 Error_Msg_N
3609 ("array delta aggregate shall be of a nonlimited type", Base);
3610 Explain_Limited_Type (Etype (Base), Base);
3611 end if;
3613 Resolve_Delta_Array_Aggregate (N, Typ);
3614 else
3616 -- Delta aggregates for record types must use parentheses,
3617 -- not square brackets.
3619 if Is_Homogeneous_Aggregate (N) then
3620 Error_Msg_N
3621 ("delta aggregates for record types must use (), not '[']", N);
3622 end if;
3624 -- The base_expression of a record_delta_aggregate can be of a
3625 -- limited type only if it is newly constructed; RM 7.5(2.1/5).
3627 Check_Expr_OK_In_Limited_Aggregate (Base);
3629 Resolve_Delta_Record_Aggregate (N, Typ);
3630 end if;
3632 Set_Etype (N, Typ);
3633 end Resolve_Delta_Aggregate;
3635 -----------------------------------
3636 -- Resolve_Delta_Array_Aggregate --
3637 -----------------------------------
3639 procedure Resolve_Delta_Array_Aggregate (N : Node_Id; Typ : Entity_Id) is
3640 Deltas : constant List_Id := Component_Associations (N);
3641 Index_Type : constant Entity_Id := Etype (First_Index (Typ));
3643 Assoc : Node_Id;
3644 Choice : Node_Id;
3645 Expr : Node_Id;
3647 begin
3648 Assoc := First (Deltas);
3649 while Present (Assoc) loop
3650 if Nkind (Assoc) = N_Iterated_Component_Association then
3651 Choice := First (Choice_List (Assoc));
3652 while Present (Choice) loop
3653 if Nkind (Choice) = N_Others_Choice then
3654 Error_Msg_N
3655 ("OTHERS not allowed in delta aggregate", Choice);
3657 elsif Nkind (Choice) = N_Subtype_Indication then
3658 Resolve_Discrete_Subtype_Indication
3659 (Choice, Base_Type (Index_Type));
3661 else
3662 Analyze_And_Resolve (Choice, Index_Type);
3663 end if;
3665 Next (Choice);
3666 end loop;
3668 declare
3669 Id : constant Entity_Id := Defining_Identifier (Assoc);
3670 Ent : constant Entity_Id :=
3671 New_Internal_Entity
3672 (E_Loop, Current_Scope, Sloc (Assoc), 'L');
3674 begin
3675 Set_Etype (Ent, Standard_Void_Type);
3676 Set_Parent (Ent, Assoc);
3677 Push_Scope (Ent);
3679 if No (Scope (Id)) then
3680 Set_Etype (Id, Index_Type);
3681 Mutate_Ekind (Id, E_Variable);
3682 Set_Is_Not_Self_Hidden (Id);
3683 Set_Scope (Id, Ent);
3684 end if;
3685 Enter_Name (Id);
3687 -- Resolve a copy of the expression, after setting
3688 -- its parent properly to preserve its context.
3690 Expr := New_Copy_Tree (Expression (Assoc));
3691 Set_Parent (Expr, Assoc);
3692 Analyze_And_Resolve (Expr, Component_Type (Typ));
3693 End_Scope;
3694 end;
3696 else
3697 Choice := First (Choice_List (Assoc));
3698 while Present (Choice) loop
3699 Analyze (Choice);
3701 if Nkind (Choice) = N_Others_Choice then
3702 Error_Msg_N
3703 ("OTHERS not allowed in delta aggregate", Choice);
3705 elsif Is_Entity_Name (Choice)
3706 and then Is_Type (Entity (Choice))
3707 then
3708 -- Choice covers a range of values
3710 if Base_Type (Entity (Choice)) /=
3711 Base_Type (Index_Type)
3712 then
3713 Error_Msg_NE
3714 ("choice does not match index type of &",
3715 Choice, Typ);
3716 end if;
3718 elsif Nkind (Choice) = N_Subtype_Indication then
3719 Resolve_Discrete_Subtype_Indication
3720 (Choice, Base_Type (Index_Type));
3722 else
3723 Resolve (Choice, Index_Type);
3724 end if;
3726 Next (Choice);
3727 end loop;
3729 -- For an array_delta_aggregate, the array_component_association
3730 -- shall not use the box symbol <>; RM 4.3.4(11/5).
3732 pragma Assert
3733 (Box_Present (Assoc) xor Present (Expression (Assoc)));
3735 if Box_Present (Assoc) then
3736 Error_Msg_N
3737 ("'<'> in array delta aggregate is not allowed", Assoc);
3738 else
3739 Analyze_And_Resolve (Expression (Assoc), Component_Type (Typ));
3740 end if;
3741 end if;
3743 Next (Assoc);
3744 end loop;
3745 end Resolve_Delta_Array_Aggregate;
3747 ------------------------------------
3748 -- Resolve_Delta_Record_Aggregate --
3749 ------------------------------------
3751 procedure Resolve_Delta_Record_Aggregate (N : Node_Id; Typ : Entity_Id) is
3753 -- Variables used to verify that discriminant-dependent components
3754 -- appear in the same variant.
3756 Comp_Ref : Entity_Id := Empty; -- init to avoid warning
3757 Variant : Node_Id;
3759 procedure Check_Variant (Id : Entity_Id);
3760 -- If a given component of the delta aggregate appears in a variant
3761 -- part, verify that it is within the same variant as that of previous
3762 -- specified variant components of the delta.
3764 function Get_Component (Nam : Node_Id) return Entity_Id;
3765 -- Locate component with a given name and return it. If none found then
3766 -- report error and return Empty.
3768 function Nested_In (V1 : Node_Id; V2 : Node_Id) return Boolean;
3769 -- Determine whether variant V1 is within variant V2
3771 function Variant_Depth (N : Node_Id) return Natural;
3772 -- Determine the distance of a variant to the enclosing type declaration
3774 --------------------
3775 -- Check_Variant --
3776 --------------------
3778 procedure Check_Variant (Id : Entity_Id) is
3779 Comp : Entity_Id;
3780 Comp_Variant : Node_Id;
3782 begin
3783 if not Has_Discriminants (Typ) then
3784 return;
3785 end if;
3787 Comp := First_Entity (Typ);
3788 while Present (Comp) loop
3789 exit when Chars (Comp) = Chars (Id);
3790 Next_Component (Comp);
3791 end loop;
3793 -- Find the variant, if any, whose component list includes the
3794 -- component declaration.
3796 Comp_Variant := Parent (Parent (List_Containing (Parent (Comp))));
3797 if Nkind (Comp_Variant) = N_Variant then
3798 if No (Variant) then
3799 Variant := Comp_Variant;
3800 Comp_Ref := Comp;
3802 elsif Variant /= Comp_Variant then
3803 declare
3804 D1 : constant Integer := Variant_Depth (Variant);
3805 D2 : constant Integer := Variant_Depth (Comp_Variant);
3807 begin
3808 if D1 = D2
3809 or else
3810 (D1 > D2 and then not Nested_In (Variant, Comp_Variant))
3811 or else
3812 (D2 > D1 and then not Nested_In (Comp_Variant, Variant))
3813 then
3814 pragma Assert (Present (Comp_Ref));
3815 Error_Msg_Node_2 := Comp_Ref;
3816 Error_Msg_NE
3817 ("& and & appear in different variants", Id, Comp);
3819 -- Otherwise retain the deeper variant for subsequent tests
3821 elsif D2 > D1 then
3822 Variant := Comp_Variant;
3823 end if;
3824 end;
3825 end if;
3826 end if;
3827 end Check_Variant;
3829 -------------------
3830 -- Get_Component --
3831 -------------------
3833 function Get_Component (Nam : Node_Id) return Entity_Id is
3834 Comp : Entity_Id;
3836 begin
3837 Comp := First_Entity (Typ);
3838 while Present (Comp) loop
3839 if Chars (Comp) = Chars (Nam) then
3840 if Ekind (Comp) = E_Discriminant then
3841 Error_Msg_N ("delta cannot apply to discriminant", Nam);
3842 end if;
3844 return Comp;
3845 end if;
3847 Next_Entity (Comp);
3848 end loop;
3850 Error_Msg_NE ("type& has no component with this name", Nam, Typ);
3851 return Empty;
3852 end Get_Component;
3854 ---------------
3855 -- Nested_In --
3856 ---------------
3858 function Nested_In (V1, V2 : Node_Id) return Boolean is
3859 Par : Node_Id;
3861 begin
3862 Par := Parent (V1);
3863 while Nkind (Par) /= N_Full_Type_Declaration loop
3864 if Par = V2 then
3865 return True;
3866 end if;
3868 Par := Parent (Par);
3869 end loop;
3871 return False;
3872 end Nested_In;
3874 -------------------
3875 -- Variant_Depth --
3876 -------------------
3878 function Variant_Depth (N : Node_Id) return Natural is
3879 Depth : Natural;
3880 Par : Node_Id;
3882 begin
3883 Depth := 0;
3884 Par := Parent (N);
3885 while Nkind (Par) /= N_Full_Type_Declaration loop
3886 Depth := Depth + 1;
3887 Par := Parent (Par);
3888 end loop;
3890 return Depth;
3891 end Variant_Depth;
3893 -- Local variables
3895 Deltas : constant List_Id := Component_Associations (N);
3897 Assoc : Node_Id;
3898 Choice : Node_Id;
3899 Comp : Entity_Id;
3900 Comp_Type : Entity_Id := Empty; -- init to avoid warning
3902 -- Start of processing for Resolve_Delta_Record_Aggregate
3904 begin
3905 Variant := Empty;
3907 Assoc := First (Deltas);
3908 while Present (Assoc) loop
3909 Choice := First (Choice_List (Assoc));
3910 while Present (Choice) loop
3911 Comp := Get_Component (Choice);
3913 if Present (Comp) then
3914 Check_Variant (Choice);
3916 Comp_Type := Etype (Comp);
3918 -- Decorate the component reference by setting its entity and
3919 -- type, as otherwise backends like GNATprove would have to
3920 -- rediscover this information by themselves.
3922 Set_Entity (Choice, Comp);
3923 Set_Etype (Choice, Comp_Type);
3924 else
3925 Comp_Type := Any_Type;
3926 end if;
3928 Next (Choice);
3929 end loop;
3931 pragma Assert (Present (Comp_Type));
3933 -- A record_component_association in record_delta_aggregate shall not
3934 -- use the box compound delimiter <> rather than an expression; see
3935 -- RM 4.3.1(17.3/5).
3937 pragma Assert (Present (Expression (Assoc)) xor Box_Present (Assoc));
3939 if Box_Present (Assoc) then
3940 Error_Msg_N
3941 ("'<'> in record delta aggregate is not allowed", Assoc);
3942 else
3943 Analyze_And_Resolve (Expression (Assoc), Comp_Type);
3945 -- The expression must not be of a limited type; RM 4.3.1(17.4/5)
3947 if Is_Limited_Type (Etype (Expression (Assoc))) then
3948 Error_Msg_N
3949 ("expression of a limited type in record delta aggregate " &
3950 "is not allowed",
3951 Expression (Assoc));
3952 end if;
3953 end if;
3955 Next (Assoc);
3956 end loop;
3957 end Resolve_Delta_Record_Aggregate;
3959 ---------------------------------
3960 -- Resolve_Extension_Aggregate --
3961 ---------------------------------
3963 -- There are two cases to consider:
3965 -- a) If the ancestor part is a type mark, the components needed are the
3966 -- difference between the components of the expected type and the
3967 -- components of the given type mark.
3969 -- b) If the ancestor part is an expression, it must be unambiguous, and
3970 -- once we have its type we can also compute the needed components as in
3971 -- the previous case. In both cases, if the ancestor type is not the
3972 -- immediate ancestor, we have to build this ancestor recursively.
3974 -- In both cases, discriminants of the ancestor type do not play a role in
3975 -- the resolution of the needed components, because inherited discriminants
3976 -- cannot be used in a type extension. As a result we can compute
3977 -- independently the list of components of the ancestor type and of the
3978 -- expected type.
3980 procedure Resolve_Extension_Aggregate (N : Node_Id; Typ : Entity_Id) is
3981 A : constant Node_Id := Ancestor_Part (N);
3982 A_Type : Entity_Id;
3983 I : Interp_Index;
3984 It : Interp;
3986 function Valid_Limited_Ancestor (Anc : Node_Id) return Boolean;
3987 -- If the type is limited, verify that the ancestor part is a legal
3988 -- expression (aggregate or function call, including 'Input)) that does
3989 -- not require a copy, as specified in 7.5(2).
3991 function Valid_Ancestor_Type return Boolean;
3992 -- Verify that the type of the ancestor part is a non-private ancestor
3993 -- of the expected type, which must be a type extension.
3995 procedure Transform_BIP_Assignment (Typ : Entity_Id);
3996 -- For an extension aggregate whose ancestor part is a build-in-place
3997 -- call returning a nonlimited type, this is used to transform the
3998 -- assignment to the ancestor part to use a temp.
4000 ----------------------------
4001 -- Valid_Limited_Ancestor --
4002 ----------------------------
4004 function Valid_Limited_Ancestor (Anc : Node_Id) return Boolean is
4005 begin
4006 if Is_Entity_Name (Anc) and then Is_Type (Entity (Anc)) then
4007 return True;
4009 -- The ancestor must be a call or an aggregate, but a call may
4010 -- have been expanded into a temporary, so check original node.
4012 elsif Nkind (Anc) in N_Aggregate
4013 | N_Extension_Aggregate
4014 | N_Function_Call
4015 then
4016 return True;
4018 elsif Nkind (Original_Node (Anc)) = N_Function_Call then
4019 return True;
4021 elsif Nkind (Anc) = N_Attribute_Reference
4022 and then Attribute_Name (Anc) = Name_Input
4023 then
4024 return True;
4026 elsif Nkind (Anc) = N_Qualified_Expression then
4027 return Valid_Limited_Ancestor (Expression (Anc));
4029 elsif Nkind (Anc) = N_Raise_Expression then
4030 return True;
4032 else
4033 return False;
4034 end if;
4035 end Valid_Limited_Ancestor;
4037 -------------------------
4038 -- Valid_Ancestor_Type --
4039 -------------------------
4041 function Valid_Ancestor_Type return Boolean is
4042 Imm_Type : Entity_Id;
4044 begin
4045 Imm_Type := Base_Type (Typ);
4046 while Is_Derived_Type (Imm_Type) loop
4047 if Etype (Imm_Type) = Base_Type (A_Type) then
4048 return True;
4050 -- The base type of the parent type may appear as a private
4051 -- extension if it is declared as such in a parent unit of the
4052 -- current one. For consistency of the subsequent analysis use
4053 -- the partial view for the ancestor part.
4055 elsif Is_Private_Type (Etype (Imm_Type))
4056 and then Present (Full_View (Etype (Imm_Type)))
4057 and then Base_Type (A_Type) = Full_View (Etype (Imm_Type))
4058 then
4059 A_Type := Etype (Imm_Type);
4060 return True;
4062 -- The parent type may be a private extension. The aggregate is
4063 -- legal if the type of the aggregate is an extension of it that
4064 -- is not a private extension.
4066 elsif Is_Private_Type (A_Type)
4067 and then not Is_Private_Type (Imm_Type)
4068 and then Present (Full_View (A_Type))
4069 and then Base_Type (Full_View (A_Type)) = Etype (Imm_Type)
4070 then
4071 return True;
4073 -- The parent type may be a raise expression (which is legal in
4074 -- any expression context).
4076 elsif A_Type = Raise_Type then
4077 A_Type := Etype (Imm_Type);
4078 return True;
4080 else
4081 Imm_Type := Etype (Base_Type (Imm_Type));
4082 end if;
4083 end loop;
4085 -- If previous loop did not find a proper ancestor, report error
4087 Error_Msg_NE ("expect ancestor type of &", A, Typ);
4088 return False;
4089 end Valid_Ancestor_Type;
4091 ------------------------------
4092 -- Transform_BIP_Assignment --
4093 ------------------------------
4095 procedure Transform_BIP_Assignment (Typ : Entity_Id) is
4096 Loc : constant Source_Ptr := Sloc (N);
4097 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'Y', A);
4098 Obj_Decl : constant Node_Id :=
4099 Make_Object_Declaration (Loc,
4100 Defining_Identifier => Def_Id,
4101 Constant_Present => True,
4102 Object_Definition => New_Occurrence_Of (Typ, Loc),
4103 Expression => A,
4104 Has_Init_Expression => True);
4105 begin
4106 Set_Etype (Def_Id, Typ);
4107 Set_Ancestor_Part (N, New_Occurrence_Of (Def_Id, Loc));
4108 Insert_Action (N, Obj_Decl);
4109 end Transform_BIP_Assignment;
4111 -- Start of processing for Resolve_Extension_Aggregate
4113 begin
4114 -- Analyze the ancestor part and account for the case where it is a
4115 -- parameterless function call.
4117 Analyze (A);
4118 Check_Parameterless_Call (A);
4120 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
4122 -- AI05-0115: If the ancestor part is a subtype mark, the ancestor
4123 -- must not have unknown discriminants. To catch cases where the
4124 -- aggregate occurs at a place where the full view of the ancestor
4125 -- type is visible and doesn't have unknown discriminants, but the
4126 -- aggregate type was derived from a partial view that has unknown
4127 -- discriminants, we check whether the aggregate type has unknown
4128 -- discriminants (unknown discriminants were inherited), along
4129 -- with checking that the partial view of the ancestor has unknown
4130 -- discriminants. (It might be sufficient to replace the entire
4131 -- condition with Has_Unknown_Discriminants (Typ), but that might
4132 -- miss some cases, not clear, and causes error changes in some tests
4133 -- such as class-wide cases, that aren't clearly improvements. ???)
4135 if Has_Unknown_Discriminants (Entity (A))
4136 or else (Has_Unknown_Discriminants (Typ)
4137 and then Partial_View_Has_Unknown_Discr (Entity (A)))
4138 then
4139 Error_Msg_NE
4140 ("aggregate not available for type& whose ancestor "
4141 & "has unknown discriminants", N, Typ);
4142 end if;
4143 end if;
4145 if not Is_Tagged_Type (Typ) then
4146 Error_Msg_N ("type of extension aggregate must be tagged", N);
4147 return;
4149 elsif Is_Limited_Type (Typ) then
4151 -- Ada 2005 (AI-287): Limited aggregates are allowed
4153 if Ada_Version < Ada_2005 then
4154 Error_Msg_N ("aggregate type cannot be limited", N);
4155 Explain_Limited_Type (Typ, N);
4156 return;
4158 elsif Valid_Limited_Ancestor (A) then
4159 null;
4161 else
4162 Error_Msg_N
4163 ("limited ancestor part must be aggregate or function call", A);
4164 end if;
4166 elsif Is_Class_Wide_Type (Typ) then
4167 Error_Msg_N ("aggregate cannot be of a class-wide type", N);
4168 return;
4169 end if;
4171 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
4172 A_Type := Get_Full_View (Entity (A));
4174 if Valid_Ancestor_Type then
4175 Set_Entity (A, A_Type);
4176 Set_Etype (A, A_Type);
4178 Validate_Ancestor_Part (N);
4179 Resolve_Record_Aggregate (N, Typ);
4180 end if;
4182 elsif Nkind (A) /= N_Aggregate then
4183 if Is_Overloaded (A) then
4184 A_Type := Any_Type;
4186 Get_First_Interp (A, I, It);
4187 while Present (It.Typ) loop
4189 -- Consider limited interpretations if Ada 2005 or higher
4191 if Is_Tagged_Type (It.Typ)
4192 and then (Ada_Version >= Ada_2005
4193 or else not Is_Limited_Type (It.Typ))
4194 then
4195 if A_Type /= Any_Type then
4196 Error_Msg_N ("cannot resolve expression", A);
4197 return;
4198 else
4199 A_Type := It.Typ;
4200 end if;
4201 end if;
4203 Get_Next_Interp (I, It);
4204 end loop;
4206 if A_Type = Any_Type then
4207 if Ada_Version >= Ada_2005 then
4208 Error_Msg_N
4209 ("ancestor part must be of a tagged type", A);
4210 else
4211 Error_Msg_N
4212 ("ancestor part must be of a nonlimited tagged type", A);
4213 end if;
4215 return;
4216 end if;
4218 else
4219 A_Type := Etype (A);
4220 end if;
4222 if Valid_Ancestor_Type then
4223 Resolve (A, A_Type);
4224 Check_Unset_Reference (A);
4225 Check_Non_Static_Context (A);
4227 -- The aggregate is illegal if the ancestor expression is a call
4228 -- to a function with a limited unconstrained result, unless the
4229 -- type of the aggregate is a null extension. This restriction
4230 -- was added in AI05-67 to simplify implementation.
4232 if Nkind (A) = N_Function_Call
4233 and then Is_Limited_Type (A_Type)
4234 and then not Is_Null_Extension (Typ)
4235 and then not Is_Constrained (A_Type)
4236 then
4237 Error_Msg_N
4238 ("type of limited ancestor part must be constrained", A);
4240 -- Reject the use of CPP constructors that leave objects partially
4241 -- initialized. For example:
4243 -- type CPP_Root is tagged limited record ...
4244 -- pragma Import (CPP, CPP_Root);
4246 -- type CPP_DT is new CPP_Root and Iface ...
4247 -- pragma Import (CPP, CPP_DT);
4249 -- type Ada_DT is new CPP_DT with ...
4251 -- Obj : Ada_DT := Ada_DT'(New_CPP_Root with others => <>);
4253 -- Using the constructor of CPP_Root the slots of the dispatch
4254 -- table of CPP_DT cannot be set, and the secondary tag of
4255 -- CPP_DT is unknown.
4257 elsif Nkind (A) = N_Function_Call
4258 and then Is_CPP_Constructor_Call (A)
4259 and then Enclosing_CPP_Parent (Typ) /= A_Type
4260 then
4261 Error_Msg_NE
4262 ("??must use 'C'P'P constructor for type &", A,
4263 Enclosing_CPP_Parent (Typ));
4265 -- The following call is not needed if the previous warning
4266 -- is promoted to an error.
4268 Resolve_Record_Aggregate (N, Typ);
4270 elsif Is_Class_Wide_Type (Etype (A))
4271 and then Nkind (Original_Node (A)) = N_Function_Call
4272 then
4273 -- If the ancestor part is a dispatching call, it appears
4274 -- statically to be a legal ancestor, but it yields any member
4275 -- of the class, and it is not possible to determine whether
4276 -- it is an ancestor of the extension aggregate (much less
4277 -- which ancestor). It is not possible to determine the
4278 -- components of the extension part.
4280 -- This check implements AI-306, which in fact was motivated by
4281 -- an AdaCore query to the ARG after this test was added.
4283 Error_Msg_N ("ancestor part must be statically tagged", A);
4284 else
4285 -- We are using the build-in-place protocol, but we can't build
4286 -- in place, because we need to call the function before
4287 -- allocating the aggregate. Could do better for null
4288 -- extensions, and maybe for nondiscriminated types.
4289 -- This is wrong for limited, but those were wrong already.
4291 if not Is_Limited_View (A_Type)
4292 and then Is_Build_In_Place_Function_Call (A)
4293 then
4294 Transform_BIP_Assignment (A_Type);
4295 end if;
4297 Resolve_Record_Aggregate (N, Typ);
4298 end if;
4299 end if;
4301 else
4302 Error_Msg_N ("no unique type for this aggregate", A);
4303 end if;
4305 Check_Function_Writable_Actuals (N);
4306 end Resolve_Extension_Aggregate;
4308 ----------------------------------
4309 -- Resolve_Null_Array_Aggregate --
4310 ----------------------------------
4312 function Resolve_Null_Array_Aggregate (N : Node_Id) return Boolean is
4313 -- Never returns False, but declared as a function to match
4314 -- other Resolve_Mumble functions.
4316 Loc : constant Source_Ptr := Sloc (N);
4317 Typ : constant Entity_Id := Etype (N);
4319 Index : Node_Id;
4320 Lo, Hi : Node_Id;
4321 Constr : constant List_Id := New_List;
4323 begin
4324 -- Attach the list of constraints at the location of the aggregate, so
4325 -- the individual constraints can be analyzed.
4327 Set_Parent (Constr, N);
4329 -- Create a constrained subtype with null dimensions
4331 Index := First_Index (Typ);
4332 while Present (Index) loop
4333 Get_Index_Bounds (Index, L => Lo, H => Hi);
4335 -- The upper bound is the predecessor of the lower bound
4337 Hi := Make_Attribute_Reference
4338 (Loc,
4339 Prefix => New_Occurrence_Of (Etype (Index), Loc),
4340 Attribute_Name => Name_Pred,
4341 Expressions => New_List (New_Copy_Tree (Lo)));
4343 Append (Make_Range (Loc, New_Copy_Tree (Lo), Hi), Constr);
4344 Analyze_And_Resolve (Last (Constr), Etype (Index));
4346 Next_Index (Index);
4347 end loop;
4349 Set_Compile_Time_Known_Aggregate (N);
4350 Set_Aggregate_Bounds (N, First (Constr));
4352 return True;
4353 end Resolve_Null_Array_Aggregate;
4355 ------------------------------
4356 -- Resolve_Record_Aggregate --
4357 ------------------------------
4359 procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id) is
4360 New_Assoc_List : constant List_Id := New_List;
4361 -- New_Assoc_List is the newly built list of N_Component_Association
4362 -- nodes.
4364 Others_Etype : Entity_Id := Empty;
4365 -- This variable is used to save the Etype of the last record component
4366 -- that takes its value from the others choice. Its purpose is:
4368 -- (a) make sure the others choice is useful
4370 -- (b) make sure the type of all the components whose value is
4371 -- subsumed by the others choice are the same.
4373 -- This variable is updated as a side effect of function Get_Value.
4375 Box_Node : Node_Id := Empty;
4376 Is_Box_Present : Boolean := False;
4377 Is_Box_Init_By_Default : Boolean := False;
4378 Others_Box : Natural := 0;
4379 -- Ada 2005 (AI-287): Variables used in case of default initialization
4380 -- to provide a functionality similar to Others_Etype. Box_Present
4381 -- indicates that the component takes its default initialization;
4382 -- Others_Box counts the number of components of the current aggregate
4383 -- (which may be a sub-aggregate of a larger one) that are default-
4384 -- initialized. A value of One indicates that an others_box is present.
4385 -- Any larger value indicates that the others_box is not redundant.
4386 -- These variables, similar to Others_Etype, are also updated as a side
4387 -- effect of function Get_Value. Box_Node is used to place a warning on
4388 -- a redundant others_box.
4390 procedure Add_Association
4391 (Component : Entity_Id;
4392 Expr : Node_Id;
4393 Assoc_List : List_Id;
4394 Is_Box_Present : Boolean := False);
4395 -- Builds a new N_Component_Association node which associates Component
4396 -- to expression Expr and adds it to the association list being built,
4397 -- either New_Assoc_List, or the association being built for an inner
4398 -- aggregate.
4400 procedure Add_Discriminant_Values
4401 (New_Aggr : Node_Id;
4402 Assoc_List : List_Id);
4403 -- The constraint to a component may be given by a discriminant of the
4404 -- enclosing type, in which case we have to retrieve its value, which is
4405 -- part of the enclosing aggregate. Assoc_List provides the discriminant
4406 -- associations of the current type or of some enclosing record.
4408 function Discriminant_Present (Input_Discr : Entity_Id) return Boolean;
4409 -- If aggregate N is a regular aggregate this routine will return True.
4410 -- Otherwise, if N is an extension aggregate, then Input_Discr denotes
4411 -- a discriminant whose value may already have been specified by N's
4412 -- ancestor part. This routine checks whether this is indeed the case
4413 -- and if so returns False, signaling that no value for Input_Discr
4414 -- should appear in N's aggregate part. Also, in this case, the routine
4415 -- appends to New_Assoc_List the discriminant value specified in the
4416 -- ancestor part.
4418 -- If the aggregate is in a context with expansion delayed, it will be
4419 -- reanalyzed. The inherited discriminant values must not be reinserted
4420 -- in the component list to prevent spurious errors, but they must be
4421 -- present on first analysis to build the proper subtype indications.
4422 -- The flag Inherited_Discriminant is used to prevent the re-insertion.
4424 function Find_Private_Ancestor (Typ : Entity_Id) return Entity_Id;
4425 -- AI05-0115: Find earlier ancestor in the derivation chain that is
4426 -- derived from private view Typ. Whether the aggregate is legal depends
4427 -- on the current visibility of the type as well as that of the parent
4428 -- of the ancestor.
4430 function Get_Value
4431 (Compon : Entity_Id;
4432 From : List_Id;
4433 Consider_Others_Choice : Boolean := False) return Node_Id;
4434 -- Given a record component stored in parameter Compon, this function
4435 -- returns its value as it appears in the list From, which is a list
4436 -- of N_Component_Association nodes.
4438 -- If no component association has a choice for the searched component,
4439 -- the value provided by the others choice is returned, if there is one,
4440 -- and Consider_Others_Choice is set to true. Otherwise Empty is
4441 -- returned. If there is more than one component association giving a
4442 -- value for the searched record component, an error message is emitted
4443 -- and the first found value is returned.
4445 -- If Consider_Others_Choice is set and the returned expression comes
4446 -- from the others choice, then Others_Etype is set as a side effect.
4447 -- An error message is emitted if the components taking their value from
4448 -- the others choice do not have same type.
4450 procedure Propagate_Discriminants
4451 (Aggr : Node_Id;
4452 Assoc_List : List_Id);
4453 -- Nested components may themselves be discriminated types constrained
4454 -- by outer discriminants, whose values must be captured before the
4455 -- aggregate is expanded into assignments.
4457 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Entity_Id);
4458 -- Analyzes and resolves expression Expr against the Etype of the
4459 -- Component. This routine also applies all appropriate checks to Expr.
4460 -- It finally saves a Expr in the newly created association list that
4461 -- will be attached to the final record aggregate. Note that if the
4462 -- Parent pointer of Expr is not set then Expr was produced with a
4463 -- New_Copy_Tree or some such.
4465 procedure Rewrite_Range (Root_Type : Entity_Id; Rge : Node_Id);
4466 -- Rewrite a range node Rge when its bounds refer to non-stored
4467 -- discriminants from Root_Type, to replace them with the stored
4468 -- discriminant values. This is required in GNATprove mode, and is
4469 -- adopted in all modes to avoid special-casing GNATprove mode.
4471 ---------------------
4472 -- Add_Association --
4473 ---------------------
4475 procedure Add_Association
4476 (Component : Entity_Id;
4477 Expr : Node_Id;
4478 Assoc_List : List_Id;
4479 Is_Box_Present : Boolean := False)
4481 Choice_List : constant List_Id := New_List;
4482 Loc : Source_Ptr;
4484 begin
4485 -- If this is a box association the expression is missing, so use the
4486 -- Sloc of the aggregate itself for the new association.
4488 pragma Assert (Present (Expr) xor Is_Box_Present);
4490 if Present (Expr) then
4491 Loc := Sloc (Expr);
4492 else
4493 Loc := Sloc (N);
4494 end if;
4496 Append_To (Choice_List, New_Occurrence_Of (Component, Loc));
4498 Append_To (Assoc_List,
4499 Make_Component_Association (Loc,
4500 Choices => Choice_List,
4501 Expression => Expr,
4502 Box_Present => Is_Box_Present));
4504 -- If this association has a box for a component that is initialized
4505 -- by default, then set flag on the new association to indicate that
4506 -- the original association was for such a box-initialized component.
4508 if Is_Box_Init_By_Default then
4509 Set_Was_Default_Init_Box_Association (Last (Assoc_List));
4510 end if;
4511 end Add_Association;
4513 -----------------------------
4514 -- Add_Discriminant_Values --
4515 -----------------------------
4517 procedure Add_Discriminant_Values
4518 (New_Aggr : Node_Id;
4519 Assoc_List : List_Id)
4521 Assoc : Node_Id;
4522 Discr : Entity_Id;
4523 Discr_Elmt : Elmt_Id;
4524 Discr_Val : Node_Id;
4525 Val : Entity_Id;
4527 begin
4528 Discr := First_Discriminant (Etype (New_Aggr));
4529 Discr_Elmt := First_Elmt (Discriminant_Constraint (Etype (New_Aggr)));
4530 while Present (Discr_Elmt) loop
4531 Discr_Val := Node (Discr_Elmt);
4533 -- If the constraint is given by a discriminant then it is a
4534 -- discriminant of an enclosing record, and its value has already
4535 -- been placed in the association list.
4537 if Is_Entity_Name (Discr_Val)
4538 and then Ekind (Entity (Discr_Val)) = E_Discriminant
4539 then
4540 Val := Entity (Discr_Val);
4542 Assoc := First (Assoc_List);
4543 while Present (Assoc) loop
4544 if Present (Entity (First (Choices (Assoc))))
4545 and then Entity (First (Choices (Assoc))) = Val
4546 then
4547 Discr_Val := Expression (Assoc);
4548 exit;
4549 end if;
4551 Next (Assoc);
4552 end loop;
4553 end if;
4555 Add_Association
4556 (Discr, New_Copy_Tree (Discr_Val),
4557 Component_Associations (New_Aggr));
4559 -- If the discriminant constraint is a current instance, mark the
4560 -- current aggregate so that the self-reference can be expanded by
4561 -- Build_Record_Aggr_Code.Replace_Type later.
4563 if Nkind (Discr_Val) = N_Attribute_Reference
4564 and then Is_Entity_Name (Prefix (Discr_Val))
4565 and then Is_Type (Entity (Prefix (Discr_Val)))
4566 and then
4567 Is_Ancestor
4568 (Entity (Prefix (Discr_Val)),
4569 Etype (N),
4570 Use_Full_View => True)
4571 then
4572 Set_Has_Self_Reference (N);
4573 end if;
4575 Next_Elmt (Discr_Elmt);
4576 Next_Discriminant (Discr);
4577 end loop;
4578 end Add_Discriminant_Values;
4580 --------------------------
4581 -- Discriminant_Present --
4582 --------------------------
4584 function Discriminant_Present (Input_Discr : Entity_Id) return Boolean is
4585 Regular_Aggr : constant Boolean := Nkind (N) /= N_Extension_Aggregate;
4587 Ancestor_Is_Subtyp : Boolean;
4589 Loc : Source_Ptr;
4591 Ancestor : Node_Id;
4592 Ancestor_Typ : Entity_Id;
4593 Comp_Assoc : Node_Id;
4594 Discr : Entity_Id;
4595 Discr_Expr : Node_Id;
4596 Discr_Val : Elmt_Id := No_Elmt;
4597 Orig_Discr : Entity_Id;
4599 begin
4600 if Regular_Aggr then
4601 return True;
4602 end if;
4604 -- Check whether inherited discriminant values have already been
4605 -- inserted in the aggregate. This will be the case if we are
4606 -- re-analyzing an aggregate whose expansion was delayed.
4608 if Present (Component_Associations (N)) then
4609 Comp_Assoc := First (Component_Associations (N));
4610 while Present (Comp_Assoc) loop
4611 if Inherited_Discriminant (Comp_Assoc) then
4612 return True;
4613 end if;
4615 Next (Comp_Assoc);
4616 end loop;
4617 end if;
4619 Ancestor := Ancestor_Part (N);
4620 Ancestor_Typ := Etype (Ancestor);
4621 Loc := Sloc (Ancestor);
4623 -- For a private type with unknown discriminants, use the underlying
4624 -- record view if it is available.
4626 if Has_Unknown_Discriminants (Ancestor_Typ)
4627 and then Present (Full_View (Ancestor_Typ))
4628 and then Present (Underlying_Record_View (Full_View (Ancestor_Typ)))
4629 then
4630 Ancestor_Typ := Underlying_Record_View (Full_View (Ancestor_Typ));
4631 end if;
4633 Ancestor_Is_Subtyp :=
4634 Is_Entity_Name (Ancestor) and then Is_Type (Entity (Ancestor));
4636 -- If the ancestor part has no discriminants clearly N's aggregate
4637 -- part must provide a value for Discr.
4639 if not Has_Discriminants (Ancestor_Typ) then
4640 return True;
4642 -- If the ancestor part is an unconstrained subtype mark then the
4643 -- Discr must be present in N's aggregate part.
4645 elsif Ancestor_Is_Subtyp
4646 and then not Is_Constrained (Entity (Ancestor))
4647 then
4648 return True;
4649 end if;
4651 -- Now look to see if Discr was specified in the ancestor part
4653 if Ancestor_Is_Subtyp then
4654 Discr_Val :=
4655 First_Elmt (Discriminant_Constraint (Entity (Ancestor)));
4656 end if;
4658 Orig_Discr := Original_Record_Component (Input_Discr);
4660 Discr := First_Discriminant (Ancestor_Typ);
4661 while Present (Discr) loop
4663 -- If Ancestor has already specified Disc value then insert its
4664 -- value in the final aggregate.
4666 if Original_Record_Component (Discr) = Orig_Discr then
4667 if Ancestor_Is_Subtyp then
4668 Discr_Expr := New_Copy_Tree (Node (Discr_Val));
4669 else
4670 Discr_Expr :=
4671 Make_Selected_Component (Loc,
4672 Prefix => Duplicate_Subexpr (Ancestor),
4673 Selector_Name => New_Occurrence_Of (Input_Discr, Loc));
4674 end if;
4676 Resolve_Aggr_Expr (Discr_Expr, Input_Discr);
4677 Set_Inherited_Discriminant (Last (New_Assoc_List));
4678 return False;
4679 end if;
4681 Next_Discriminant (Discr);
4683 if Ancestor_Is_Subtyp then
4684 Next_Elmt (Discr_Val);
4685 end if;
4686 end loop;
4688 return True;
4689 end Discriminant_Present;
4691 ---------------------------
4692 -- Find_Private_Ancestor --
4693 ---------------------------
4695 function Find_Private_Ancestor (Typ : Entity_Id) return Entity_Id is
4696 Par : Entity_Id;
4698 begin
4699 Par := Typ;
4700 loop
4701 if Has_Private_Ancestor (Par)
4702 and then not Has_Private_Ancestor (Etype (Base_Type (Par)))
4703 then
4704 return Par;
4706 elsif not Is_Derived_Type (Par) then
4707 return Empty;
4709 else
4710 Par := Etype (Base_Type (Par));
4711 end if;
4712 end loop;
4713 end Find_Private_Ancestor;
4715 ---------------
4716 -- Get_Value --
4717 ---------------
4719 function Get_Value
4720 (Compon : Entity_Id;
4721 From : List_Id;
4722 Consider_Others_Choice : Boolean := False) return Node_Id
4724 Typ : constant Entity_Id := Etype (Compon);
4725 Assoc : Node_Id;
4726 Expr : Node_Id := Empty;
4727 Selector_Name : Node_Id;
4729 begin
4730 Is_Box_Present := False;
4731 Is_Box_Init_By_Default := False;
4733 if No (From) then
4734 return Empty;
4735 end if;
4737 Assoc := First (From);
4738 while Present (Assoc) loop
4739 Selector_Name := First (Choices (Assoc));
4740 while Present (Selector_Name) loop
4741 if Nkind (Selector_Name) = N_Others_Choice then
4742 if Consider_Others_Choice and then No (Expr) then
4744 -- We need to duplicate the expression for each
4745 -- successive component covered by the others choice.
4746 -- This is redundant if the others_choice covers only
4747 -- one component (small optimization possible???), but
4748 -- indispensable otherwise, because each one must be
4749 -- expanded individually to preserve side effects.
4751 -- Ada 2005 (AI-287): In case of default initialization
4752 -- of components, we duplicate the corresponding default
4753 -- expression (from the record type declaration). The
4754 -- copy must carry the sloc of the association (not the
4755 -- original expression) to prevent spurious elaboration
4756 -- checks when the default includes function calls.
4758 if Box_Present (Assoc) then
4759 Others_Box := Others_Box + 1;
4760 Is_Box_Present := True;
4762 if Expander_Active then
4763 return
4764 New_Copy_Tree_And_Copy_Dimensions
4765 (Expression (Parent (Compon)),
4766 New_Sloc => Sloc (Assoc));
4767 else
4768 return Expression (Parent (Compon));
4769 end if;
4771 else
4772 if Present (Others_Etype)
4773 and then Base_Type (Others_Etype) /= Base_Type (Typ)
4774 then
4775 -- If the components are of an anonymous access
4776 -- type they are distinct, but this is legal in
4777 -- Ada 2012 as long as designated types match.
4779 if (Ekind (Typ) = E_Anonymous_Access_Type
4780 or else Ekind (Typ) =
4781 E_Anonymous_Access_Subprogram_Type)
4782 and then Designated_Type (Typ) =
4783 Designated_Type (Others_Etype)
4784 then
4785 null;
4786 else
4787 Error_Msg_N
4788 ("components in OTHERS choice must have same "
4789 & "type", Selector_Name);
4790 end if;
4791 end if;
4793 Others_Etype := Typ;
4795 -- Copy the expression so that it is resolved
4796 -- independently for each component, This is needed
4797 -- for accessibility checks on components of anonymous
4798 -- access types, even in compile_only mode.
4800 if not Inside_A_Generic then
4801 return
4802 New_Copy_Tree_And_Copy_Dimensions
4803 (Expression (Assoc));
4804 else
4805 return Expression (Assoc);
4806 end if;
4807 end if;
4808 end if;
4810 elsif Chars (Compon) = Chars (Selector_Name) then
4811 if No (Expr) then
4813 -- Ada 2005 (AI-231)
4815 if Ada_Version >= Ada_2005
4816 and then Known_Null (Expression (Assoc))
4817 then
4818 Check_Can_Never_Be_Null (Compon, Expression (Assoc));
4819 end if;
4821 -- We need to duplicate the expression when several
4822 -- components are grouped together with a "|" choice.
4823 -- For instance "filed1 | filed2 => Expr"
4825 -- Ada 2005 (AI-287)
4827 if Box_Present (Assoc) then
4828 Is_Box_Present := True;
4830 -- Duplicate the default expression of the component
4831 -- from the record type declaration, so a new copy
4832 -- can be attached to the association.
4834 -- Note that we always copy the default expression,
4835 -- even when the association has a single choice, in
4836 -- order to create a proper association for the
4837 -- expanded aggregate.
4839 -- Component may have no default, in which case the
4840 -- expression is empty and the component is default-
4841 -- initialized, but an association for the component
4842 -- exists, and it is not covered by an others clause.
4844 -- Scalar and private types have no initialization
4845 -- procedure, so they remain uninitialized. If the
4846 -- target of the aggregate is a constant this
4847 -- deserves a warning.
4849 if No (Expression (Parent (Compon)))
4850 and then not Has_Non_Null_Base_Init_Proc (Typ)
4851 and then not Has_Aspect (Typ, Aspect_Default_Value)
4852 and then not Is_Concurrent_Type (Typ)
4853 and then Nkind (Parent (N)) = N_Object_Declaration
4854 and then Constant_Present (Parent (N))
4855 then
4856 Error_Msg_Node_2 := Typ;
4857 Error_Msg_NE
4858 ("??component& of type& is uninitialized",
4859 Assoc, Selector_Name);
4861 -- An additional reminder if the component type
4862 -- is a generic formal.
4864 if Is_Generic_Type (Base_Type (Typ)) then
4865 Error_Msg_NE
4866 ("\instance should provide actual type with "
4867 & "initialization for&", Assoc, Typ);
4868 end if;
4869 end if;
4871 return
4872 New_Copy_Tree_And_Copy_Dimensions
4873 (Expression (Parent (Compon)));
4875 else
4876 if Present (Next (Selector_Name)) then
4877 Expr := New_Copy_Tree_And_Copy_Dimensions
4878 (Expression (Assoc));
4879 else
4880 Expr := Expression (Assoc);
4881 end if;
4882 end if;
4884 Generate_Reference (Compon, Selector_Name, 'm');
4886 else
4887 Error_Msg_NE
4888 ("more than one value supplied for &",
4889 Selector_Name, Compon);
4891 end if;
4892 end if;
4894 Next (Selector_Name);
4895 end loop;
4897 Next (Assoc);
4898 end loop;
4900 return Expr;
4901 end Get_Value;
4903 -----------------------------
4904 -- Propagate_Discriminants --
4905 -----------------------------
4907 procedure Propagate_Discriminants
4908 (Aggr : Node_Id;
4909 Assoc_List : List_Id)
4911 Loc : constant Source_Ptr := Sloc (N);
4913 procedure Process_Component (Comp : Entity_Id);
4914 -- Add one component with a box association to the inner aggregate,
4915 -- and recurse if component is itself composite.
4917 -----------------------
4918 -- Process_Component --
4919 -----------------------
4921 procedure Process_Component (Comp : Entity_Id) is
4922 T : constant Entity_Id := Etype (Comp);
4923 New_Aggr : Node_Id;
4925 begin
4926 if Is_Record_Type (T) and then Has_Discriminants (T) then
4927 New_Aggr := Make_Aggregate (Loc, No_List, New_List);
4928 Set_Etype (New_Aggr, T);
4930 Add_Association
4931 (Comp, New_Aggr, Component_Associations (Aggr));
4933 -- Collect discriminant values and recurse
4935 Add_Discriminant_Values (New_Aggr, Assoc_List);
4936 Propagate_Discriminants (New_Aggr, Assoc_List);
4938 Build_Constrained_Itype
4939 (New_Aggr, T, Component_Associations (New_Aggr));
4940 else
4941 Add_Association
4942 (Comp, Empty, Component_Associations (Aggr),
4943 Is_Box_Present => True);
4944 end if;
4945 end Process_Component;
4947 -- Local variables
4949 Aggr_Type : constant Entity_Id := Base_Type (Etype (Aggr));
4950 Components : constant Elist_Id := New_Elmt_List;
4951 Def_Node : constant Node_Id :=
4952 Type_Definition (Declaration_Node (Aggr_Type));
4954 Comp : Node_Id;
4955 Comp_Elmt : Elmt_Id;
4956 Errors : Boolean;
4958 -- Start of processing for Propagate_Discriminants
4960 begin
4961 -- The component type may be a variant type. Collect the components
4962 -- that are ruled by the known values of the discriminants. Their
4963 -- values have already been inserted into the component list of the
4964 -- current aggregate.
4966 if Nkind (Def_Node) = N_Record_Definition
4967 and then Present (Component_List (Def_Node))
4968 and then Present (Variant_Part (Component_List (Def_Node)))
4969 then
4970 Gather_Components (Aggr_Type,
4971 Component_List (Def_Node),
4972 Governed_By => Component_Associations (Aggr),
4973 Into => Components,
4974 Report_Errors => Errors);
4976 Comp_Elmt := First_Elmt (Components);
4977 while Present (Comp_Elmt) loop
4978 if Ekind (Node (Comp_Elmt)) /= E_Discriminant then
4979 Process_Component (Node (Comp_Elmt));
4980 end if;
4982 Next_Elmt (Comp_Elmt);
4983 end loop;
4985 -- No variant part, iterate over all components
4987 else
4988 Comp := First_Component (Etype (Aggr));
4989 while Present (Comp) loop
4990 Process_Component (Comp);
4991 Next_Component (Comp);
4992 end loop;
4993 end if;
4994 end Propagate_Discriminants;
4996 -----------------------
4997 -- Resolve_Aggr_Expr --
4998 -----------------------
5000 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Entity_Id) is
5001 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean;
5002 -- If the expression is an aggregate (possibly qualified) then its
5003 -- expansion is delayed until the enclosing aggregate is expanded
5004 -- into assignments. In that case, do not generate checks on the
5005 -- expression, because they will be generated later, and will other-
5006 -- wise force a copy (to remove side effects) that would leave a
5007 -- dynamic-sized aggregate in the code, something that gigi cannot
5008 -- handle.
5010 ---------------------------
5011 -- Has_Expansion_Delayed --
5012 ---------------------------
5014 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean is
5015 begin
5016 return
5017 (Nkind (Expr) in N_Aggregate | N_Extension_Aggregate
5018 and then Present (Etype (Expr))
5019 and then Is_Record_Type (Etype (Expr))
5020 and then Expansion_Delayed (Expr))
5021 or else
5022 (Nkind (Expr) = N_Qualified_Expression
5023 and then Has_Expansion_Delayed (Expression (Expr)));
5024 end Has_Expansion_Delayed;
5026 -- Local variables
5028 Expr_Type : Entity_Id := Empty;
5029 New_C : Entity_Id := Component;
5030 New_Expr : Node_Id;
5032 Relocate : Boolean;
5033 -- Set to True if the resolved Expr node needs to be relocated when
5034 -- attached to the newly created association list. This node need not
5035 -- be relocated if its parent pointer is not set. In fact in this
5036 -- case Expr is the output of a New_Copy_Tree call. If Relocate is
5037 -- True then we have analyzed the expression node in the original
5038 -- aggregate and hence it needs to be relocated when moved over to
5039 -- the new association list.
5041 -- Start of processing for Resolve_Aggr_Expr
5043 begin
5044 -- If the type of the component is elementary or the type of the
5045 -- aggregate does not contain discriminants, use the type of the
5046 -- component to resolve Expr.
5048 if Is_Elementary_Type (Etype (Component))
5049 or else not Has_Discriminants (Etype (N))
5050 then
5051 Expr_Type := Etype (Component);
5053 -- Otherwise we have to pick up the new type of the component from
5054 -- the new constrained subtype of the aggregate. In fact components
5055 -- which are of a composite type might be constrained by a
5056 -- discriminant, and we want to resolve Expr against the subtype were
5057 -- all discriminant occurrences are replaced with their actual value.
5059 else
5060 New_C := First_Component (Etype (N));
5061 while Present (New_C) loop
5062 if Chars (New_C) = Chars (Component) then
5063 Expr_Type := Etype (New_C);
5064 exit;
5065 end if;
5067 Next_Component (New_C);
5068 end loop;
5070 pragma Assert (Present (Expr_Type));
5072 -- For each range in an array type where a discriminant has been
5073 -- replaced with the constraint, check that this range is within
5074 -- the range of the base type. This checks is done in the init
5075 -- proc for regular objects, but has to be done here for
5076 -- aggregates since no init proc is called for them.
5078 if Is_Array_Type (Expr_Type) then
5079 declare
5080 Index : Node_Id;
5081 -- Range of the current constrained index in the array
5083 Orig_Index : Node_Id := First_Index (Etype (Component));
5084 -- Range corresponding to the range Index above in the
5085 -- original unconstrained record type. The bounds of this
5086 -- range may be governed by discriminants.
5088 Unconstr_Index : Node_Id := First_Index (Etype (Expr_Type));
5089 -- Range corresponding to the range Index above for the
5090 -- unconstrained array type. This range is needed to apply
5091 -- range checks.
5093 begin
5094 Index := First_Index (Expr_Type);
5095 while Present (Index) loop
5096 if Depends_On_Discriminant (Orig_Index) then
5097 Apply_Range_Check (Index, Etype (Unconstr_Index));
5098 end if;
5100 Next_Index (Index);
5101 Next_Index (Orig_Index);
5102 Next_Index (Unconstr_Index);
5103 end loop;
5104 end;
5105 end if;
5106 end if;
5108 -- If the Parent pointer of Expr is not set, Expr is an expression
5109 -- duplicated by New_Tree_Copy (this happens for record aggregates
5110 -- that look like (Field1 | Filed2 => Expr) or (others => Expr)).
5111 -- Such a duplicated expression must be attached to the tree
5112 -- before analysis and resolution to enforce the rule that a tree
5113 -- fragment should never be analyzed or resolved unless it is
5114 -- attached to the current compilation unit.
5116 if No (Parent (Expr)) then
5117 Set_Parent (Expr, N);
5118 Relocate := False;
5119 else
5120 Relocate := True;
5121 end if;
5123 Analyze_And_Resolve (Expr, Expr_Type);
5124 Check_Expr_OK_In_Limited_Aggregate (Expr);
5125 Check_Non_Static_Context (Expr);
5126 Check_Unset_Reference (Expr);
5128 -- Check wrong use of class-wide types
5130 if Is_Class_Wide_Type (Etype (Expr)) then
5131 Error_Msg_N ("dynamically tagged expression not allowed", Expr);
5132 end if;
5134 if not Has_Expansion_Delayed (Expr) then
5135 Aggregate_Constraint_Checks (Expr, Expr_Type);
5136 end if;
5138 -- If an aggregate component has a type with predicates, an explicit
5139 -- predicate check must be applied, as for an assignment statement,
5140 -- because the aggregate might not be expanded into individual
5141 -- component assignments.
5143 if Has_Predicates (Expr_Type)
5144 and then Analyzed (Expr)
5145 then
5146 Apply_Predicate_Check (Expr, Expr_Type);
5147 end if;
5149 if Raises_Constraint_Error (Expr) then
5150 Set_Raises_Constraint_Error (N);
5151 end if;
5153 -- If the expression has been marked as requiring a range check, then
5154 -- generate it here. It's a bit odd to be generating such checks in
5155 -- the analyzer, but harmless since Generate_Range_Check does nothing
5156 -- (other than making sure Do_Range_Check is set) if the expander is
5157 -- not active.
5159 if Do_Range_Check (Expr) then
5160 Generate_Range_Check (Expr, Expr_Type, CE_Range_Check_Failed);
5161 end if;
5163 -- Add association Component => Expr if the caller requests it
5165 if Relocate then
5166 New_Expr := Relocate_Node (Expr);
5168 -- Since New_Expr is not gonna be analyzed later on, we need to
5169 -- propagate here the dimensions form Expr to New_Expr.
5171 Copy_Dimensions (Expr, New_Expr);
5173 else
5174 New_Expr := Expr;
5175 end if;
5177 Add_Association (New_C, New_Expr, New_Assoc_List);
5178 end Resolve_Aggr_Expr;
5180 -------------------
5181 -- Rewrite_Range --
5182 -------------------
5184 procedure Rewrite_Range (Root_Type : Entity_Id; Rge : Node_Id) is
5185 procedure Rewrite_Bound
5186 (Bound : Node_Id;
5187 Disc : Entity_Id;
5188 Expr_Disc : Node_Id);
5189 -- Rewrite a bound of the range Bound, when it is equal to the
5190 -- non-stored discriminant Disc, into the stored discriminant
5191 -- value Expr_Disc.
5193 -------------------
5194 -- Rewrite_Bound --
5195 -------------------
5197 procedure Rewrite_Bound
5198 (Bound : Node_Id;
5199 Disc : Entity_Id;
5200 Expr_Disc : Node_Id)
5202 begin
5203 if Nkind (Bound) /= N_Identifier then
5204 return;
5205 end if;
5207 -- We expect either the discriminant or the discriminal
5209 if Entity (Bound) = Disc
5210 or else (Ekind (Entity (Bound)) = E_In_Parameter
5211 and then Discriminal_Link (Entity (Bound)) = Disc)
5212 then
5213 Rewrite (Bound, New_Copy_Tree (Expr_Disc));
5214 end if;
5215 end Rewrite_Bound;
5217 -- Local variables
5219 Low, High : Node_Id;
5220 Disc : Entity_Id;
5221 Expr_Disc : Elmt_Id;
5223 -- Start of processing for Rewrite_Range
5225 begin
5226 if Has_Discriminants (Root_Type) and then Nkind (Rge) = N_Range then
5227 Low := Low_Bound (Rge);
5228 High := High_Bound (Rge);
5230 Disc := First_Discriminant (Root_Type);
5231 Expr_Disc := First_Elmt (Stored_Constraint (Etype (N)));
5232 while Present (Disc) loop
5233 Rewrite_Bound (Low, Disc, Node (Expr_Disc));
5234 Rewrite_Bound (High, Disc, Node (Expr_Disc));
5235 Next_Discriminant (Disc);
5236 Next_Elmt (Expr_Disc);
5237 end loop;
5238 end if;
5239 end Rewrite_Range;
5241 -- Local variables
5243 Components : constant Elist_Id := New_Elmt_List;
5244 -- Components is the list of the record components whose value must be
5245 -- provided in the aggregate. This list does include discriminants.
5247 Component : Entity_Id;
5248 Component_Elmt : Elmt_Id;
5249 Expr : Node_Id;
5250 Positional_Expr : Node_Id;
5252 -- Start of processing for Resolve_Record_Aggregate
5254 begin
5255 -- A record aggregate is restricted in SPARK:
5257 -- Each named association can have only a single choice.
5258 -- OTHERS cannot be used.
5259 -- Positional and named associations cannot be mixed.
5261 if Present (Component_Associations (N)) then
5262 declare
5263 Assoc : Node_Id;
5265 begin
5266 Assoc := First (Component_Associations (N));
5267 while Present (Assoc) loop
5268 if Nkind (Assoc) = N_Iterated_Component_Association then
5269 Error_Msg_N
5270 ("iterated component association can only appear in an "
5271 & "array aggregate", N);
5272 raise Unrecoverable_Error;
5273 end if;
5275 Next (Assoc);
5276 end loop;
5277 end;
5278 end if;
5280 -- We may end up calling Duplicate_Subexpr on expressions that are
5281 -- attached to New_Assoc_List. For this reason we need to attach it
5282 -- to the tree by setting its parent pointer to N. This parent point
5283 -- will change in STEP 8 below.
5285 Set_Parent (New_Assoc_List, N);
5287 -- STEP 1: abstract type and null record verification
5289 if Is_Abstract_Type (Typ) then
5290 Error_Msg_N ("type of aggregate cannot be abstract", N);
5291 end if;
5293 if No (First_Entity (Typ)) and then Null_Record_Present (N) then
5294 Set_Etype (N, Typ);
5295 return;
5297 elsif Present (First_Entity (Typ))
5298 and then Null_Record_Present (N)
5299 and then not Is_Tagged_Type (Typ)
5300 then
5301 Error_Msg_N ("record aggregate cannot be null", N);
5302 return;
5304 -- If the type has no components, then the aggregate should either
5305 -- have "null record", or in Ada 2005 it could instead have a single
5306 -- component association given by "others => <>". For Ada 95 we flag an
5307 -- error at this point, but for Ada 2005 we proceed with checking the
5308 -- associations below, which will catch the case where it's not an
5309 -- aggregate with "others => <>". Note that the legality of a <>
5310 -- aggregate for a null record type was established by AI05-016.
5312 elsif No (First_Entity (Typ))
5313 and then Ada_Version < Ada_2005
5314 then
5315 Error_Msg_N ("record aggregate must be null", N);
5316 return;
5317 end if;
5319 -- A record aggregate can only use parentheses
5321 if Nkind (N) = N_Aggregate
5322 and then Is_Homogeneous_Aggregate (N)
5323 then
5324 Error_Msg_N ("record aggregate must use (), not '[']", N);
5325 return;
5326 end if;
5328 -- STEP 2: Verify aggregate structure
5330 Step_2 : declare
5331 Assoc : Node_Id;
5332 Bad_Aggregate : Boolean := False;
5333 Selector_Name : Node_Id;
5335 begin
5336 if Present (Component_Associations (N)) then
5337 Assoc := First (Component_Associations (N));
5338 else
5339 Assoc := Empty;
5340 end if;
5342 while Present (Assoc) loop
5343 Selector_Name := First (Choices (Assoc));
5344 while Present (Selector_Name) loop
5345 if Nkind (Selector_Name) = N_Identifier then
5346 null;
5348 elsif Nkind (Selector_Name) = N_Others_Choice then
5349 if Selector_Name /= First (Choices (Assoc))
5350 or else Present (Next (Selector_Name))
5351 then
5352 Error_Msg_N
5353 ("OTHERS must appear alone in a choice list",
5354 Selector_Name);
5355 return;
5357 elsif Present (Next (Assoc)) then
5358 Error_Msg_N
5359 ("OTHERS must appear last in an aggregate",
5360 Selector_Name);
5361 return;
5363 -- (Ada 2005): If this is an association with a box,
5364 -- indicate that the association need not represent
5365 -- any component.
5367 elsif Box_Present (Assoc) then
5368 Others_Box := 1;
5369 Box_Node := Assoc;
5370 end if;
5372 else
5373 Error_Msg_N
5374 ("selector name should be identifier or OTHERS",
5375 Selector_Name);
5376 Bad_Aggregate := True;
5377 end if;
5379 Next (Selector_Name);
5380 end loop;
5382 Next (Assoc);
5383 end loop;
5385 if Bad_Aggregate then
5386 return;
5387 end if;
5388 end Step_2;
5390 -- STEP 3: Find discriminant Values
5392 Step_3 : declare
5393 Discrim : Entity_Id;
5394 Missing_Discriminants : Boolean := False;
5396 begin
5397 if Present (Expressions (N)) then
5398 Positional_Expr := First (Expressions (N));
5399 else
5400 Positional_Expr := Empty;
5401 end if;
5403 -- AI05-0115: if the ancestor part is a subtype mark, the ancestor
5404 -- must not have unknown discriminants.
5405 -- ??? We are not checking any subtype mark here and this code is not
5406 -- exercised by any test, so it's likely wrong (in particular
5407 -- we should not use Root_Type here but the subtype mark, if any),
5408 -- and possibly not needed.
5410 if Is_Derived_Type (Typ)
5411 and then Has_Unknown_Discriminants (Root_Type (Typ))
5412 and then Nkind (N) /= N_Extension_Aggregate
5413 then
5414 Error_Msg_NE
5415 ("aggregate not available for type& whose ancestor "
5416 & "has unknown discriminants", N, Typ);
5417 end if;
5419 if Has_Unknown_Discriminants (Typ)
5420 and then Present (Underlying_Record_View (Typ))
5421 then
5422 Discrim := First_Discriminant (Underlying_Record_View (Typ));
5423 elsif Has_Discriminants (Typ) then
5424 Discrim := First_Discriminant (Typ);
5425 else
5426 Discrim := Empty;
5427 end if;
5429 -- First find the discriminant values in the positional components
5431 while Present (Discrim) and then Present (Positional_Expr) loop
5432 if Discriminant_Present (Discrim) then
5433 Resolve_Aggr_Expr (Positional_Expr, Discrim);
5435 -- Ada 2005 (AI-231)
5437 if Ada_Version >= Ada_2005
5438 and then Known_Null (Positional_Expr)
5439 then
5440 Check_Can_Never_Be_Null (Discrim, Positional_Expr);
5441 end if;
5443 Next (Positional_Expr);
5444 end if;
5446 if Present (Get_Value (Discrim, Component_Associations (N))) then
5447 Error_Msg_NE
5448 ("more than one value supplied for discriminant&",
5449 N, Discrim);
5450 end if;
5452 Next_Discriminant (Discrim);
5453 end loop;
5455 -- Find remaining discriminant values if any among named components
5457 while Present (Discrim) loop
5458 Expr := Get_Value (Discrim, Component_Associations (N), True);
5460 if not Discriminant_Present (Discrim) then
5461 if Present (Expr) then
5462 Error_Msg_NE
5463 ("more than one value supplied for discriminant &",
5464 N, Discrim);
5465 end if;
5467 elsif No (Expr) then
5468 Error_Msg_NE
5469 ("no value supplied for discriminant &", N, Discrim);
5470 Missing_Discriminants := True;
5472 else
5473 Resolve_Aggr_Expr (Expr, Discrim);
5474 end if;
5476 Next_Discriminant (Discrim);
5477 end loop;
5479 if Missing_Discriminants then
5480 return;
5481 end if;
5483 -- At this point and until the beginning of STEP 6, New_Assoc_List
5484 -- contains only the discriminants and their values.
5486 end Step_3;
5488 -- STEP 4: Set the Etype of the record aggregate
5490 if Has_Discriminants (Typ)
5491 or else (Has_Unknown_Discriminants (Typ)
5492 and then Present (Underlying_Record_View (Typ)))
5493 then
5494 Build_Constrained_Itype (N, Typ, New_Assoc_List);
5495 else
5496 Set_Etype (N, Typ);
5497 end if;
5499 -- STEP 5: Get remaining components according to discriminant values
5501 Step_5 : declare
5502 Dnode : Node_Id;
5503 Errors_Found : Boolean := False;
5504 Record_Def : Node_Id;
5505 Parent_Typ : Entity_Id;
5506 Parent_Typ_List : Elist_Id;
5507 Parent_Elmt : Elmt_Id;
5508 Root_Typ : Entity_Id;
5510 begin
5511 if Is_Derived_Type (Typ) and then Is_Tagged_Type (Typ) then
5512 Parent_Typ_List := New_Elmt_List;
5514 -- If this is an extension aggregate, the component list must
5515 -- include all components that are not in the given ancestor type.
5516 -- Otherwise, the component list must include components of all
5517 -- ancestors, starting with the root.
5519 if Nkind (N) = N_Extension_Aggregate then
5520 Root_Typ := Base_Type (Etype (Ancestor_Part (N)));
5522 else
5523 -- AI05-0115: check legality of aggregate for type with a
5524 -- private ancestor.
5526 Root_Typ := Root_Type (Typ);
5527 if Has_Private_Ancestor (Typ) then
5528 declare
5529 Ancestor : constant Entity_Id :=
5530 Find_Private_Ancestor (Typ);
5531 Ancestor_Unit : constant Entity_Id :=
5532 Cunit_Entity
5533 (Get_Source_Unit (Ancestor));
5534 Parent_Unit : constant Entity_Id :=
5535 Cunit_Entity (Get_Source_Unit
5536 (Base_Type (Etype (Ancestor))));
5537 begin
5538 -- Check whether we are in a scope that has full view
5539 -- over the private ancestor and its parent. This can
5540 -- only happen if the derivation takes place in a child
5541 -- unit of the unit that declares the parent, and we are
5542 -- in the private part or body of that child unit, else
5543 -- the aggregate is illegal.
5545 if Is_Child_Unit (Ancestor_Unit)
5546 and then Scope (Ancestor_Unit) = Parent_Unit
5547 and then In_Open_Scopes (Scope (Ancestor))
5548 and then
5549 (In_Private_Part (Scope (Ancestor))
5550 or else In_Package_Body (Scope (Ancestor)))
5551 then
5552 null;
5554 else
5555 Error_Msg_NE
5556 ("type of aggregate has private ancestor&!",
5557 N, Root_Typ);
5558 Error_Msg_N ("must use extension aggregate!", N);
5559 return;
5560 end if;
5561 end;
5562 end if;
5564 Dnode := Declaration_Node (Base_Type (Root_Typ));
5566 -- If we don't get a full declaration, then we have some error
5567 -- which will get signalled later so skip this part. Otherwise
5568 -- gather components of root that apply to the aggregate type.
5569 -- We use the base type in case there is an applicable stored
5570 -- constraint that renames the discriminants of the root.
5572 if Nkind (Dnode) = N_Full_Type_Declaration then
5573 Record_Def := Type_Definition (Dnode);
5574 Gather_Components
5575 (Base_Type (Typ),
5576 Component_List (Record_Def),
5577 Governed_By => New_Assoc_List,
5578 Into => Components,
5579 Report_Errors => Errors_Found);
5581 if Errors_Found then
5582 Error_Msg_N
5583 ("discriminant controlling variant part is not static",
5585 return;
5586 end if;
5587 end if;
5588 end if;
5590 Parent_Typ := Base_Type (Typ);
5591 while Parent_Typ /= Root_Typ loop
5592 Prepend_Elmt (Parent_Typ, To => Parent_Typ_List);
5593 Parent_Typ := Etype (Parent_Typ);
5595 -- Check whether a private parent requires the use of
5596 -- an extension aggregate. This test does not apply in
5597 -- an instantiation: if the generic unit is legal so is
5598 -- the instance.
5600 if Nkind (Parent (Base_Type (Parent_Typ))) =
5601 N_Private_Type_Declaration
5602 or else Nkind (Parent (Base_Type (Parent_Typ))) =
5603 N_Private_Extension_Declaration
5604 then
5605 if Nkind (N) /= N_Extension_Aggregate
5606 and then not In_Instance
5607 then
5608 Error_Msg_NE
5609 ("type of aggregate has private ancestor&!",
5610 N, Parent_Typ);
5611 Error_Msg_N ("must use extension aggregate!", N);
5612 return;
5614 elsif Parent_Typ /= Root_Typ then
5615 Error_Msg_NE
5616 ("ancestor part of aggregate must be private type&",
5617 Ancestor_Part (N), Parent_Typ);
5618 return;
5619 end if;
5621 -- The current view of ancestor part may be a private type,
5622 -- while the context type is always non-private.
5624 elsif Is_Private_Type (Root_Typ)
5625 and then Present (Full_View (Root_Typ))
5626 and then Nkind (N) = N_Extension_Aggregate
5627 then
5628 exit when Base_Type (Full_View (Root_Typ)) = Parent_Typ;
5629 end if;
5630 end loop;
5632 -- Now collect components from all other ancestors, beginning
5633 -- with the current type. If the type has unknown discriminants
5634 -- use the component list of the Underlying_Record_View, which
5635 -- needs to be used for the subsequent expansion of the aggregate
5636 -- into assignments.
5638 Parent_Elmt := First_Elmt (Parent_Typ_List);
5639 while Present (Parent_Elmt) loop
5640 Parent_Typ := Node (Parent_Elmt);
5642 if Has_Unknown_Discriminants (Parent_Typ)
5643 and then Present (Underlying_Record_View (Typ))
5644 then
5645 Parent_Typ := Underlying_Record_View (Parent_Typ);
5646 end if;
5648 Record_Def := Type_Definition (Parent (Base_Type (Parent_Typ)));
5649 Gather_Components (Parent_Typ,
5650 Component_List (Record_Extension_Part (Record_Def)),
5651 Governed_By => New_Assoc_List,
5652 Into => Components,
5653 Report_Errors => Errors_Found);
5655 Next_Elmt (Parent_Elmt);
5656 end loop;
5658 -- Typ is not a derived tagged type
5660 else
5661 Record_Def := Type_Definition (Parent (Base_Type (Typ)));
5663 if Null_Present (Record_Def) then
5664 null;
5666 elsif not Has_Unknown_Discriminants (Typ) then
5667 Gather_Components
5668 (Base_Type (Typ),
5669 Component_List (Record_Def),
5670 Governed_By => New_Assoc_List,
5671 Into => Components,
5672 Report_Errors => Errors_Found);
5674 else
5675 Gather_Components
5676 (Base_Type (Underlying_Record_View (Typ)),
5677 Component_List (Record_Def),
5678 Governed_By => New_Assoc_List,
5679 Into => Components,
5680 Report_Errors => Errors_Found);
5681 end if;
5682 end if;
5684 if Errors_Found then
5685 return;
5686 end if;
5687 end Step_5;
5689 -- STEP 6: Find component Values
5691 Component_Elmt := First_Elmt (Components);
5693 -- First scan the remaining positional associations in the aggregate.
5694 -- Remember that at this point Positional_Expr contains the current
5695 -- positional association if any is left after looking for discriminant
5696 -- values in step 3.
5698 while Present (Positional_Expr) and then Present (Component_Elmt) loop
5699 Component := Node (Component_Elmt);
5700 Resolve_Aggr_Expr (Positional_Expr, Component);
5702 -- Ada 2005 (AI-231)
5704 if Ada_Version >= Ada_2005 and then Known_Null (Positional_Expr) then
5705 Check_Can_Never_Be_Null (Component, Positional_Expr);
5706 end if;
5708 if Present (Get_Value (Component, Component_Associations (N))) then
5709 Error_Msg_NE
5710 ("more than one value supplied for component &", N, Component);
5711 end if;
5713 Next (Positional_Expr);
5714 Next_Elmt (Component_Elmt);
5715 end loop;
5717 if Present (Positional_Expr) then
5718 Error_Msg_N
5719 ("too many components for record aggregate", Positional_Expr);
5720 end if;
5722 -- Now scan for the named arguments of the aggregate
5724 while Present (Component_Elmt) loop
5725 Component := Node (Component_Elmt);
5726 Expr := Get_Value (Component, Component_Associations (N), True);
5728 -- Note: The previous call to Get_Value sets the value of the
5729 -- variable Is_Box_Present.
5731 -- Ada 2005 (AI-287): Handle components with default initialization.
5732 -- Note: This feature was originally added to Ada 2005 for limited
5733 -- but it was finally allowed with any type.
5735 if Is_Box_Present then
5736 Check_Box_Component : declare
5737 Ctyp : constant Entity_Id := Etype (Component);
5739 begin
5740 -- Initially assume that the box is for a default-initialized
5741 -- component and reset to False in cases where that's not true.
5743 Is_Box_Init_By_Default := True;
5745 -- If there is a default expression for the aggregate, copy
5746 -- it into a new association. This copy must modify the scopes
5747 -- of internal types that may be attached to the expression
5748 -- (e.g. index subtypes of arrays) because in general the type
5749 -- declaration and the aggregate appear in different scopes,
5750 -- and the backend requires the scope of the type to match the
5751 -- point at which it is elaborated.
5753 -- If the component has an initialization procedure (IP) we
5754 -- pass the component to the expander, which will generate
5755 -- the call to such IP.
5757 -- If the component has discriminants, their values must
5758 -- be taken from their subtype. This is indispensable for
5759 -- constraints that are given by the current instance of an
5760 -- enclosing type, to allow the expansion of the aggregate to
5761 -- replace the reference to the current instance by the target
5762 -- object of the aggregate.
5764 if Is_Case_Choice_Pattern (N) then
5766 -- Do not transform box component values in a case-choice
5767 -- aggregate.
5769 Add_Association
5770 (Component => Component,
5771 Expr => Empty,
5772 Assoc_List => New_Assoc_List,
5773 Is_Box_Present => True);
5775 elsif Present (Parent (Component))
5776 and then Nkind (Parent (Component)) = N_Component_Declaration
5777 and then Present (Expression (Parent (Component)))
5778 then
5779 -- If component declaration has an initialization expression
5780 -- then this is not a case of default initialization.
5782 Is_Box_Init_By_Default := False;
5784 Expr :=
5785 New_Copy_Tree_And_Copy_Dimensions
5786 (Expression (Parent (Component)),
5787 New_Scope => Current_Scope,
5788 New_Sloc => Sloc (N));
5790 -- As the type of the copied default expression may refer
5791 -- to discriminants of the record type declaration, these
5792 -- non-stored discriminants need to be rewritten into stored
5793 -- discriminant values for the aggregate. This is required
5794 -- in GNATprove mode, and is adopted in all modes to avoid
5795 -- special-casing GNATprove mode.
5797 if Is_Array_Type (Etype (Expr)) then
5798 declare
5799 Rec_Typ : constant Entity_Id := Scope (Component);
5800 -- Root record type whose discriminants may be used as
5801 -- bounds in range nodes.
5803 Assoc : Node_Id;
5804 Choice : Node_Id;
5805 Index : Node_Id;
5807 begin
5808 -- Rewrite the range nodes occurring in the indexes
5809 -- and their types.
5811 Index := First_Index (Etype (Expr));
5812 while Present (Index) loop
5813 Rewrite_Range (Rec_Typ, Index);
5814 Rewrite_Range
5815 (Rec_Typ, Scalar_Range (Etype (Index)));
5817 Next_Index (Index);
5818 end loop;
5820 -- Rewrite the range nodes occurring as aggregate
5821 -- bounds and component associations.
5823 if Nkind (Expr) = N_Aggregate then
5824 if Present (Aggregate_Bounds (Expr)) then
5825 Rewrite_Range (Rec_Typ, Aggregate_Bounds (Expr));
5826 end if;
5828 if Present (Component_Associations (Expr)) then
5829 Assoc := First (Component_Associations (Expr));
5830 while Present (Assoc) loop
5831 Choice := First (Choices (Assoc));
5832 while Present (Choice) loop
5833 Rewrite_Range (Rec_Typ, Choice);
5835 Next (Choice);
5836 end loop;
5838 Next (Assoc);
5839 end loop;
5840 end if;
5841 end if;
5842 end;
5843 end if;
5845 Add_Association
5846 (Component => Component,
5847 Expr => Expr,
5848 Assoc_List => New_Assoc_List);
5849 Set_Has_Self_Reference (N);
5851 elsif Needs_Simple_Initialization (Ctyp) then
5852 Add_Association
5853 (Component => Component,
5854 Expr => Empty,
5855 Assoc_List => New_Assoc_List,
5856 Is_Box_Present => True);
5858 elsif Has_Non_Null_Base_Init_Proc (Ctyp)
5859 or else not Expander_Active
5860 then
5861 if Is_Record_Type (Ctyp)
5862 and then Has_Discriminants (Ctyp)
5863 and then not Is_Private_Type (Ctyp)
5864 then
5865 -- We build a partially initialized aggregate with the
5866 -- values of the discriminants and box initialization
5867 -- for the rest, if other components are present.
5869 -- The type of the aggregate is the known subtype of
5870 -- the component. The capture of discriminants must be
5871 -- recursive because subcomponents may be constrained
5872 -- (transitively) by discriminants of enclosing types.
5873 -- For a private type with discriminants, a call to the
5874 -- initialization procedure will be generated, and no
5875 -- subaggregate is needed.
5877 Capture_Discriminants : declare
5878 Loc : constant Source_Ptr := Sloc (N);
5879 Expr : Node_Id;
5881 begin
5882 Expr := Make_Aggregate (Loc, No_List, New_List);
5883 Set_Etype (Expr, Ctyp);
5885 -- If the enclosing type has discriminants, they have
5886 -- been collected in the aggregate earlier, and they
5887 -- may appear as constraints of subcomponents.
5889 -- Similarly if this component has discriminants, they
5890 -- might in turn be propagated to their components.
5892 if Has_Discriminants (Typ) then
5893 Add_Discriminant_Values (Expr, New_Assoc_List);
5894 Propagate_Discriminants (Expr, New_Assoc_List);
5896 elsif Has_Discriminants (Ctyp) then
5897 Add_Discriminant_Values
5898 (Expr, Component_Associations (Expr));
5899 Propagate_Discriminants
5900 (Expr, Component_Associations (Expr));
5902 Build_Constrained_Itype
5903 (Expr, Ctyp, Component_Associations (Expr));
5905 else
5906 declare
5907 Comp : Entity_Id;
5909 begin
5910 -- If the type has additional components, create
5911 -- an OTHERS box association for them.
5913 Comp := First_Component (Ctyp);
5914 while Present (Comp) loop
5915 if Ekind (Comp) = E_Component then
5916 if not Is_Record_Type (Etype (Comp)) then
5917 Append_To
5918 (Component_Associations (Expr),
5919 Make_Component_Association (Loc,
5920 Choices =>
5921 New_List (
5922 Make_Others_Choice (Loc)),
5923 Expression => Empty,
5924 Box_Present => True));
5925 end if;
5927 exit;
5928 end if;
5930 Next_Component (Comp);
5931 end loop;
5932 end;
5933 end if;
5935 Add_Association
5936 (Component => Component,
5937 Expr => Expr,
5938 Assoc_List => New_Assoc_List);
5939 end Capture_Discriminants;
5941 -- Otherwise the component type is not a record, or it has
5942 -- not discriminants, or it is private.
5944 else
5945 Add_Association
5946 (Component => Component,
5947 Expr => Empty,
5948 Assoc_List => New_Assoc_List,
5949 Is_Box_Present => True);
5950 end if;
5952 -- Otherwise we only need to resolve the expression if the
5953 -- component has partially initialized values (required to
5954 -- expand the corresponding assignments and run-time checks).
5956 elsif Present (Expr)
5957 and then Is_Partially_Initialized_Type (Ctyp)
5958 then
5959 Resolve_Aggr_Expr (Expr, Component);
5960 end if;
5961 end Check_Box_Component;
5963 elsif No (Expr) then
5965 -- Ignore hidden components associated with the position of the
5966 -- interface tags: these are initialized dynamically.
5968 if No (Related_Type (Component)) then
5969 Error_Msg_NE
5970 ("no value supplied for component &!", N, Component);
5971 end if;
5973 else
5974 Resolve_Aggr_Expr (Expr, Component);
5975 end if;
5977 Next_Elmt (Component_Elmt);
5978 end loop;
5980 -- STEP 7: check for invalid components + check type in choice list
5982 Step_7 : declare
5983 Assoc : Node_Id;
5984 New_Assoc : Node_Id;
5986 Selectr : Node_Id;
5987 -- Selector name
5989 Typech : Entity_Id;
5990 -- Type of first component in choice list
5992 begin
5993 if Present (Component_Associations (N)) then
5994 Assoc := First (Component_Associations (N));
5995 else
5996 Assoc := Empty;
5997 end if;
5999 Verification : while Present (Assoc) loop
6000 Selectr := First (Choices (Assoc));
6001 Typech := Empty;
6003 if Nkind (Selectr) = N_Others_Choice then
6005 -- Ada 2005 (AI-287): others choice may have expression or box
6007 if No (Others_Etype) and then Others_Box = 0 then
6008 Error_Msg_N
6009 ("OTHERS must represent at least one component", Selectr);
6011 elsif Others_Box = 1 and then Warn_On_Redundant_Constructs then
6012 Error_Msg_N ("OTHERS choice is redundant?r?", Box_Node);
6013 Error_Msg_N
6014 ("\previous choices cover all components?r?", Box_Node);
6015 end if;
6017 exit Verification;
6018 end if;
6020 while Present (Selectr) loop
6021 Component := Empty;
6022 New_Assoc := First (New_Assoc_List);
6023 while Present (New_Assoc) loop
6024 Component := First (Choices (New_Assoc));
6026 if Chars (Selectr) = Chars (Component) then
6027 if Style_Check then
6028 Check_Identifier (Selectr, Entity (Component));
6029 end if;
6031 exit;
6032 end if;
6034 Next (New_Assoc);
6035 end loop;
6037 -- If we found an association, then this is a legal component
6038 -- of the type in question.
6040 pragma Assert (if Present (New_Assoc) then Present (Component));
6042 -- If no association, this is not a legal component of the type
6043 -- in question, unless its association is provided with a box.
6045 if No (New_Assoc) then
6046 if Box_Present (Parent (Selectr)) then
6048 -- This may still be a bogus component with a box. Scan
6049 -- list of components to verify that a component with
6050 -- that name exists.
6052 declare
6053 C : Entity_Id;
6055 begin
6056 C := First_Component (Typ);
6057 while Present (C) loop
6058 if Chars (C) = Chars (Selectr) then
6060 -- If the context is an extension aggregate,
6061 -- the component must not be inherited from
6062 -- the ancestor part of the aggregate.
6064 if Nkind (N) /= N_Extension_Aggregate
6065 or else
6066 Scope (Original_Record_Component (C)) /=
6067 Etype (Ancestor_Part (N))
6068 then
6069 exit;
6070 end if;
6071 end if;
6073 Next_Component (C);
6074 end loop;
6076 if No (C) then
6077 Error_Msg_Node_2 := Typ;
6078 Error_Msg_N ("& is not a component of}", Selectr);
6079 end if;
6080 end;
6082 elsif Chars (Selectr) /= Name_uTag
6083 and then Chars (Selectr) /= Name_uParent
6084 then
6085 if not Has_Discriminants (Typ) then
6086 Error_Msg_Node_2 := Typ;
6087 Error_Msg_N ("& is not a component of}", Selectr);
6088 else
6089 Error_Msg_N
6090 ("& is not a component of the aggregate subtype",
6091 Selectr);
6092 end if;
6094 Check_Misspelled_Component (Components, Selectr);
6095 end if;
6097 elsif No (Typech) then
6098 Typech := Base_Type (Etype (Component));
6100 -- AI05-0199: In Ada 2012, several components of anonymous
6101 -- access types can appear in a choice list, as long as the
6102 -- designated types match.
6104 elsif Typech /= Base_Type (Etype (Component)) then
6105 if Ada_Version >= Ada_2012
6106 and then Ekind (Typech) = E_Anonymous_Access_Type
6107 and then
6108 Ekind (Etype (Component)) = E_Anonymous_Access_Type
6109 and then Base_Type (Designated_Type (Typech)) =
6110 Base_Type (Designated_Type (Etype (Component)))
6111 and then
6112 Subtypes_Statically_Match (Typech, (Etype (Component)))
6113 then
6114 null;
6116 elsif not Box_Present (Parent (Selectr)) then
6117 Error_Msg_N
6118 ("components in choice list must have same type",
6119 Selectr);
6120 end if;
6121 end if;
6123 Next (Selectr);
6124 end loop;
6126 Next (Assoc);
6127 end loop Verification;
6128 end Step_7;
6130 -- STEP 8: replace the original aggregate
6132 Step_8 : declare
6133 New_Aggregate : constant Node_Id := New_Copy (N);
6135 begin
6136 Set_Expressions (New_Aggregate, No_List);
6137 Set_Etype (New_Aggregate, Etype (N));
6138 Set_Component_Associations (New_Aggregate, New_Assoc_List);
6139 Set_Check_Actuals (New_Aggregate, Check_Actuals (N));
6141 Rewrite (N, New_Aggregate);
6142 end Step_8;
6144 -- Check the dimensions of the components in the record aggregate
6146 Analyze_Dimension_Extension_Or_Record_Aggregate (N);
6147 end Resolve_Record_Aggregate;
6149 -----------------------------
6150 -- Check_Can_Never_Be_Null --
6151 -----------------------------
6153 procedure Check_Can_Never_Be_Null (Typ : Entity_Id; Expr : Node_Id) is
6154 Comp_Typ : Entity_Id;
6156 begin
6157 pragma Assert
6158 (Ada_Version >= Ada_2005
6159 and then Present (Expr)
6160 and then Known_Null (Expr));
6162 case Ekind (Typ) is
6163 when E_Array_Type =>
6164 Comp_Typ := Component_Type (Typ);
6166 when E_Component
6167 | E_Discriminant
6169 Comp_Typ := Etype (Typ);
6171 when others =>
6172 return;
6173 end case;
6175 if Can_Never_Be_Null (Comp_Typ) then
6177 -- Here we know we have a constraint error. Note that we do not use
6178 -- Apply_Compile_Time_Constraint_Error here to the Expr, which might
6179 -- seem the more natural approach. That's because in some cases the
6180 -- components are rewritten, and the replacement would be missed.
6181 -- We do not mark the whole aggregate as raising a constraint error,
6182 -- because the association may be a null array range.
6184 Error_Msg_N
6185 ("(Ada 2005) NULL not allowed in null-excluding component??", Expr);
6186 Error_Msg_N
6187 ("\Constraint_Error will be raised at run time??", Expr);
6189 Rewrite (Expr,
6190 Make_Raise_Constraint_Error
6191 (Sloc (Expr), Reason => CE_Access_Check_Failed));
6192 Set_Etype (Expr, Comp_Typ);
6193 Set_Analyzed (Expr);
6194 end if;
6195 end Check_Can_Never_Be_Null;
6197 ---------------------
6198 -- Sort_Case_Table --
6199 ---------------------
6201 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is
6202 U : constant Int := Case_Table'Last;
6203 K : Int;
6204 J : Int;
6205 T : Case_Bounds;
6207 begin
6208 K := 1;
6209 while K < U loop
6210 T := Case_Table (K + 1);
6212 J := K + 1;
6213 while J > 1
6214 and then Expr_Value (Case_Table (J - 1).Lo) > Expr_Value (T.Lo)
6215 loop
6216 Case_Table (J) := Case_Table (J - 1);
6217 J := J - 1;
6218 end loop;
6220 Case_Table (J) := T;
6221 K := K + 1;
6222 end loop;
6223 end Sort_Case_Table;
6225 end Sem_Aggr;