middle-end: check for vector mode before calling get_mask_mode [PR116074]
[official-gcc.git] / gcc / ada / sem_aggr.adb
blobbc53ea904a3b049b1c5a922dd2f88b5bb84122c4
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ A G G R --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2024, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Einfo; use Einfo;
30 with Einfo.Utils; use Einfo.Utils;
31 with Elists; use Elists;
32 with Errout; use Errout;
33 with Expander; use Expander;
34 with Exp_Tss; use Exp_Tss;
35 with Exp_Util; use Exp_Util;
36 with Freeze; use Freeze;
37 with Itypes; use Itypes;
38 with Lib; use Lib;
39 with Lib.Xref; use Lib.Xref;
40 with Mutably_Tagged; use Mutably_Tagged;
41 with Namet; use Namet;
42 with Namet.Sp; use Namet.Sp;
43 with Nmake; use Nmake;
44 with Nlists; use Nlists;
45 with Opt; use Opt;
46 with Restrict; use Restrict;
47 with Rident; use Rident;
48 with Sem; use Sem;
49 with Sem_Aux; use Sem_Aux;
50 with Sem_Case; use Sem_Case;
51 with Sem_Cat; use Sem_Cat;
52 with Sem_Ch3; use Sem_Ch3;
53 with Sem_Ch8; use Sem_Ch8;
54 with Sem_Ch13; use Sem_Ch13;
55 with Sem_Dim; use Sem_Dim;
56 with Sem_Eval; use Sem_Eval;
57 with Sem_Res; use Sem_Res;
58 with Sem_Util; use Sem_Util;
59 with Sem_Type; use Sem_Type;
60 with Sem_Warn; use Sem_Warn;
61 with Sinfo; use Sinfo;
62 with Sinfo.Utils; use Sinfo.Utils;
63 with Snames; use Snames;
64 with Stringt; use Stringt;
65 with Stand; use Stand;
66 with Style; use Style;
67 with Targparm; use Targparm;
68 with Tbuild; use Tbuild;
69 with Ttypes; use Ttypes;
70 with Uintp; use Uintp;
71 with Warnsw; use Warnsw;
73 package body Sem_Aggr is
75 type Case_Bounds is record
76 Lo : Node_Id;
77 -- Low bound of choice. Once we sort the Case_Table, then entries
78 -- will be in order of ascending Choice_Lo values.
80 Hi : Node_Id;
81 -- High Bound of choice. The sort does not pay any attention to the
82 -- high bound, so choices 1 .. 4 and 1 .. 5 could be in either order.
84 Highest : Uint;
85 -- If there are duplicates or missing entries, then in the sorted
86 -- table, this records the highest value among Choice_Hi values
87 -- seen so far, including this entry.
89 Choice : Node_Id;
90 -- The node of the choice
91 end record;
93 type Case_Table_Type is array (Pos range <>) of Case_Bounds;
94 -- Table type used by Check_Case_Choices procedure
96 -----------------------
97 -- Local Subprograms --
98 -----------------------
100 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type);
101 -- Sort the Case Table using the Lower Bound of each Choice as the key. A
102 -- simple insertion sort is used since the choices in a case statement will
103 -- usually be in near sorted order.
105 function Cannot_Compute_High_Bound (Index : Entity_Id) return Boolean;
106 -- Determines if the type of the given array aggregate index is a modular
107 -- type or an enumeration type that will raise CE at runtime when computing
108 -- the high bound of a null aggregate.
110 procedure Check_Can_Never_Be_Null (Typ : Entity_Id; Expr : Node_Id);
111 -- Ada 2005 (AI-231): Check bad usage of null for a component for which
112 -- null exclusion (NOT NULL) is specified. Typ can be an E_Array_Type for
113 -- the array case (the component type of the array will be used) or an
114 -- E_Component/E_Discriminant entity in the record case, in which case the
115 -- type of the component will be used for the test. If Typ is any other
116 -- kind of entity, the call is ignored. Expr is the component node in the
117 -- aggregate which is known to have a null value. A warning message will be
118 -- issued if the component is null excluding.
120 -- It would be better to pass the proper type for Typ ???
122 procedure Check_Expr_OK_In_Limited_Aggregate (Expr : Node_Id);
123 -- Check that Expr is either not limited or else is one of the cases of
124 -- expressions allowed for a limited component association (namely, an
125 -- aggregate, function call, or <> notation). Report error for violations.
126 -- Expression is also OK in an instance or inlining context, because we
127 -- have already preanalyzed and it is known to be type correct.
129 procedure Report_Null_Array_Constraint_Error
130 (N : Node_Id;
131 Index_Typ : Entity_Id);
132 -- N is a null array aggregate indexed by the given enumeration type or
133 -- modular type. Report a warning notifying that CE will be raised at
134 -- runtime. Under SPARK mode an error is reported instead of a warning.
136 ------------------------------------------------------
137 -- Subprograms used for RECORD AGGREGATE Processing --
138 ------------------------------------------------------
140 procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id);
141 -- This procedure performs all the semantic checks required for record
142 -- aggregates. Note that for aggregates analysis and resolution go
143 -- hand in hand. Aggregate analysis has been delayed up to here and
144 -- it is done while resolving the aggregate.
146 -- N is the N_Aggregate node.
147 -- Typ is the record type for the aggregate resolution
149 -- While performing the semantic checks, this procedure builds a new
150 -- Component_Association_List where each record field appears alone in a
151 -- Component_Choice_List along with its corresponding expression. The
152 -- record fields in the Component_Association_List appear in the same order
153 -- in which they appear in the record type Typ.
155 -- Once this new Component_Association_List is built and all the semantic
156 -- checks performed, the original aggregate subtree is replaced with the
157 -- new named record aggregate just built. This new record aggregate has no
158 -- positional associations, so its Expressions field is set to No_List.
159 -- Note that subtree substitution is performed with Rewrite so as to be
160 -- able to retrieve the original aggregate.
162 -- The aggregate subtree manipulation performed by Resolve_Record_Aggregate
163 -- yields the aggregate format expected by Gigi. Typically, this kind of
164 -- tree manipulations are done in the expander. However, because the
165 -- semantic checks that need to be performed on record aggregates really go
166 -- hand in hand with the record aggregate normalization, the aggregate
167 -- subtree transformation is performed during resolution rather than
168 -- expansion. Had we decided otherwise we would have had to duplicate most
169 -- of the code in the expansion procedure Expand_Record_Aggregate. Note,
170 -- however, that all the expansion concerning aggregates for tagged records
171 -- is done in Expand_Record_Aggregate.
173 -- The algorithm of Resolve_Record_Aggregate proceeds as follows:
175 -- 1. Make sure that the record type against which the record aggregate
176 -- has to be resolved is not abstract. Furthermore if the type is a
177 -- null aggregate make sure the input aggregate N is also null.
179 -- 2. Verify that the structure of the aggregate is that of a record
180 -- aggregate. Specifically, look for component associations and ensure
181 -- that each choice list only has identifiers or the N_Others_Choice
182 -- node. Also make sure that if present, the N_Others_Choice occurs
183 -- last and by itself.
185 -- 3. If Typ contains discriminants, the values for each discriminant is
186 -- looked for. If the record type Typ has variants, we check that the
187 -- expressions corresponding to each discriminant ruling the (possibly
188 -- nested) variant parts of Typ, are static. This allows us to determine
189 -- the variant parts to which the rest of the aggregate must conform.
190 -- The names of discriminants with their values are saved in a new
191 -- association list, New_Assoc_List which is later augmented with the
192 -- names and values of the remaining components in the record type.
194 -- During this phase we also make sure that every discriminant is
195 -- assigned exactly one value. Note that when several values for a given
196 -- discriminant are found, semantic processing continues looking for
197 -- further errors. In this case it's the first discriminant value found
198 -- which we will be recorded.
200 -- IMPORTANT NOTE: For derived tagged types this procedure expects
201 -- First_Discriminant and Next_Discriminant to give the correct list
202 -- of discriminants, in the correct order.
204 -- 4. After all the discriminant values have been gathered, we can set the
205 -- Etype of the record aggregate. If Typ contains no discriminants this
206 -- is straightforward: the Etype of N is just Typ, otherwise a new
207 -- implicit constrained subtype of Typ is built to be the Etype of N.
209 -- 5. Gather the remaining record components according to the discriminant
210 -- values. This involves recursively traversing the record type
211 -- structure to see what variants are selected by the given discriminant
212 -- values. This processing is a little more convoluted if Typ is a
213 -- derived tagged types since we need to retrieve the record structure
214 -- of all the ancestors of Typ.
216 -- 6. After gathering the record components we look for their values in the
217 -- record aggregate and emit appropriate error messages should we not
218 -- find such values or should they be duplicated.
220 -- 7. We then make sure no illegal component names appear in the record
221 -- aggregate and make sure that the type of the record components
222 -- appearing in a same choice list is the same. Finally we ensure that
223 -- the others choice, if present, is used to provide the value of at
224 -- least a record component.
226 -- 8. The original aggregate node is replaced with the new named aggregate
227 -- built in steps 3 through 6, as explained earlier.
229 -- Given the complexity of record aggregate resolution, the primary goal of
230 -- this routine is clarity and simplicity rather than execution and storage
231 -- efficiency. If there are only positional components in the aggregate the
232 -- running time is linear. If there are associations the running time is
233 -- still linear as long as the order of the associations is not too far off
234 -- the order of the components in the record type. If this is not the case
235 -- the running time is at worst quadratic in the size of the association
236 -- list.
238 procedure Check_Misspelled_Component
239 (Elements : Elist_Id;
240 Component : Node_Id);
241 -- Give possible misspelling diagnostic if Component is likely to be a
242 -- misspelling of one of the components of the Assoc_List. This is called
243 -- by Resolve_Aggr_Expr after producing an invalid component error message.
245 -----------------------------------------------------
246 -- Subprograms used for ARRAY AGGREGATE Processing --
247 -----------------------------------------------------
249 function Resolve_Array_Aggregate
250 (N : Node_Id;
251 Index : Node_Id;
252 Index_Constr : Node_Id;
253 Component_Typ : Entity_Id;
254 Others_Allowed : Boolean) return Boolean;
255 -- This procedure performs the semantic checks for an array aggregate.
256 -- True is returned if the aggregate resolution succeeds.
258 -- The procedure works by recursively checking each nested aggregate.
259 -- Specifically, after checking a sub-aggregate nested at the i-th level
260 -- we recursively check all the subaggregates at the i+1-st level (if any).
261 -- Note that aggregates analysis and resolution go hand in hand.
262 -- Aggregate analysis has been delayed up to here and it is done while
263 -- resolving the aggregate.
265 -- N is the current N_Aggregate node to be checked.
267 -- Index is the index node corresponding to the array sub-aggregate that
268 -- we are currently checking (RM 4.3.3 (8)). Its Etype is the
269 -- corresponding index type (or subtype).
271 -- Index_Constr is the node giving the applicable index constraint if
272 -- any (RM 4.3.3 (10)). It "is a constraint provided by certain
273 -- contexts [...] that can be used to determine the bounds of the array
274 -- value specified by the aggregate". If Others_Allowed below is False
275 -- there is no applicable index constraint and this node is set to Index.
277 -- Component_Typ is the array component type.
279 -- Others_Allowed indicates whether an others choice is allowed
280 -- in the context where the top-level aggregate appeared.
282 -- The algorithm of Resolve_Array_Aggregate proceeds as follows:
284 -- 1. Make sure that the others choice, if present, is by itself and
285 -- appears last in the sub-aggregate. Check that we do not have
286 -- positional and named components in the array sub-aggregate (unless
287 -- the named association is an others choice). Finally if an others
288 -- choice is present, make sure it is allowed in the aggregate context.
290 -- 2. If the array sub-aggregate contains discrete_choices:
292 -- (A) Verify their validity. Specifically verify that:
294 -- (a) If a null range is present it must be the only possible
295 -- choice in the array aggregate.
297 -- (b) Ditto for a non static range.
299 -- (c) Ditto for a non static expression.
301 -- In addition this step analyzes and resolves each discrete_choice,
302 -- making sure that its type is the type of the corresponding Index.
303 -- If we are not at the lowest array aggregate level (in the case of
304 -- multi-dimensional aggregates) then invoke Resolve_Array_Aggregate
305 -- recursively on each component expression. Otherwise, resolve the
306 -- bottom level component expressions against the expected component
307 -- type ONLY IF the component corresponds to a single discrete choice
308 -- which is not an others choice (to see why read the DELAYED
309 -- COMPONENT RESOLUTION below).
311 -- (B) Determine the bounds of the sub-aggregate and lowest and
312 -- highest choice values.
314 -- 3. For positional aggregates:
316 -- (A) Loop over the component expressions either recursively invoking
317 -- Resolve_Array_Aggregate on each of these for multi-dimensional
318 -- array aggregates or resolving the bottom level component
319 -- expressions against the expected component type.
321 -- (B) Determine the bounds of the positional sub-aggregates.
323 -- 4. Try to determine statically whether the evaluation of the array
324 -- sub-aggregate raises Constraint_Error. If yes emit proper
325 -- warnings. The precise checks are the following:
327 -- (A) Check that the index range defined by aggregate bounds is
328 -- compatible with corresponding index subtype.
329 -- We also check against the base type. In fact it could be that
330 -- Low/High bounds of the base type are static whereas those of
331 -- the index subtype are not. Thus if we can statically catch
332 -- a problem with respect to the base type we are guaranteed
333 -- that the same problem will arise with the index subtype
335 -- (B) If we are dealing with a named aggregate containing an others
336 -- choice and at least one discrete choice then make sure the range
337 -- specified by the discrete choices does not overflow the
338 -- aggregate bounds. We also check against the index type and base
339 -- type bounds for the same reasons given in (A).
341 -- (C) If we are dealing with a positional aggregate with an others
342 -- choice make sure the number of positional elements specified
343 -- does not overflow the aggregate bounds. We also check against
344 -- the index type and base type bounds as mentioned in (A).
346 -- Finally construct an N_Range node giving the sub-aggregate bounds.
347 -- Set the Aggregate_Bounds field of the sub-aggregate to be this
348 -- N_Range. The routine Array_Aggr_Subtype below uses such N_Ranges
349 -- to build the appropriate aggregate subtype. Aggregate_Bounds
350 -- information is needed during expansion.
352 -- DELAYED COMPONENT RESOLUTION: The resolution of bottom level component
353 -- expressions in an array aggregate may call Duplicate_Subexpr or some
354 -- other routine that inserts code just outside the outermost aggregate.
355 -- If the array aggregate contains discrete choices or an others choice,
356 -- this may be wrong. Consider for instance the following example.
358 -- type Rec is record
359 -- V : Integer := 0;
360 -- end record;
362 -- type Acc_Rec is access Rec;
363 -- Arr : array (1..3) of Acc_Rec := (1 .. 3 => new Rec);
365 -- Then the transformation of "new Rec" that occurs during resolution
366 -- entails the following code modifications
368 -- P7b : constant Acc_Rec := new Rec;
369 -- RecIP (P7b.all);
370 -- Arr : array (1..3) of Acc_Rec := (1 .. 3 => P7b);
372 -- This code transformation is clearly wrong, since we need to call
373 -- "new Rec" for each of the 3 array elements. To avoid this problem we
374 -- delay resolution of the components of non positional array aggregates
375 -- to the expansion phase. As an optimization, if the discrete choice
376 -- specifies a single value we do not delay resolution.
378 function Array_Aggr_Subtype (N : Node_Id; Typ : Entity_Id) return Entity_Id;
379 -- This routine returns the type or subtype of an array aggregate.
381 -- N is the array aggregate node whose type we return.
383 -- Typ is the context type in which N occurs.
385 -- This routine creates an implicit array subtype whose bounds are
386 -- those defined by the aggregate. When this routine is invoked
387 -- Resolve_Array_Aggregate has already processed aggregate N. Thus the
388 -- Aggregate_Bounds of each sub-aggregate, is an N_Range node giving the
389 -- sub-aggregate bounds. When building the aggregate itype, this function
390 -- traverses the array aggregate N collecting such Aggregate_Bounds and
391 -- constructs the proper array aggregate itype.
393 -- Note that in the case of multidimensional aggregates each inner
394 -- sub-aggregate corresponding to a given array dimension, may provide a
395 -- different bounds. If it is possible to determine statically that
396 -- some sub-aggregates corresponding to the same index do not have the
397 -- same bounds, then a warning is emitted. If such check is not possible
398 -- statically (because some sub-aggregate bounds are dynamic expressions)
399 -- then this job is left to the expander. In all cases the particular
400 -- bounds that this function will chose for a given dimension is the first
401 -- N_Range node for a sub-aggregate corresponding to that dimension.
403 -- Note that the Raises_Constraint_Error flag of an array aggregate
404 -- whose evaluation is determined to raise CE by Resolve_Array_Aggregate,
405 -- is set in Resolve_Array_Aggregate but the aggregate is not
406 -- immediately replaced with a raise CE. In fact, Array_Aggr_Subtype must
407 -- first construct the proper itype for the aggregate (Gigi needs
408 -- this). After constructing the proper itype we will eventually replace
409 -- the top-level aggregate with a raise CE (done in Resolve_Aggregate).
410 -- Of course in cases such as:
412 -- type Arr is array (integer range <>) of Integer;
413 -- A : Arr := (positive range -1 .. 2 => 0);
415 -- The bounds of the aggregate itype are cooked up to look reasonable
416 -- (in this particular case the bounds will be 1 .. 2).
418 procedure Make_String_Into_Aggregate (N : Node_Id);
419 -- A string literal can appear in a context in which a one dimensional
420 -- array of characters is expected. This procedure simply rewrites the
421 -- string as an aggregate, prior to resolution.
423 function Resolve_Null_Array_Aggregate (N : Node_Id) return Boolean;
424 -- The recursive method used to construct an aggregate's bounds in
425 -- Resolve_Array_Aggregate cannot work for null array aggregates. This
426 -- function constructs an appropriate list of ranges and stores its first
427 -- element in Aggregate_Bounds (N).
429 ---------------------------------
430 -- Delta aggregate processing --
431 ---------------------------------
433 procedure Resolve_Delta_Array_Aggregate (N : Node_Id; Typ : Entity_Id);
434 procedure Resolve_Delta_Record_Aggregate (N : Node_Id; Typ : Entity_Id);
435 procedure Resolve_Deep_Delta_Assoc (N : Node_Id; Typ : Entity_Id);
436 -- Resolve the names/expressions in a component association for
437 -- a deep delta aggregate. Typ is the type of the enclosing object.
439 ------------------------
440 -- Array_Aggr_Subtype --
441 ------------------------
443 function Array_Aggr_Subtype
444 (N : Node_Id;
445 Typ : Entity_Id) return Entity_Id
447 Aggr_Dimension : constant Pos := Number_Dimensions (Typ);
448 -- Number of aggregate index dimensions
450 Aggr_Range : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
451 -- Constrained N_Range of each index dimension in our aggregate itype
453 Aggr_Low : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
454 Aggr_High : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
455 -- Low and High bounds for each index dimension in our aggregate itype
457 Is_Fully_Positional : Boolean := True;
459 procedure Collect_Aggr_Bounds (N : Node_Id; Dim : Pos);
460 -- N is an array (sub-)aggregate. Dim is the dimension corresponding
461 -- to (sub-)aggregate N. This procedure collects and removes the side
462 -- effects of the constrained N_Range nodes corresponding to each index
463 -- dimension of our aggregate itype. These N_Range nodes are collected
464 -- in Aggr_Range above.
466 -- Likewise collect in Aggr_Low & Aggr_High above the low and high
467 -- bounds of each index dimension. If, when collecting, two bounds
468 -- corresponding to the same dimension are static and found to differ,
469 -- then emit a warning, and mark N as raising Constraint_Error.
471 -------------------------
472 -- Collect_Aggr_Bounds --
473 -------------------------
475 procedure Collect_Aggr_Bounds (N : Node_Id; Dim : Pos) is
476 This_Range : constant Node_Id := Aggregate_Bounds (N);
477 -- The aggregate range node of this specific sub-aggregate
479 This_Low : constant Node_Id := Low_Bound (This_Range);
480 This_High : constant Node_Id := High_Bound (This_Range);
481 -- The aggregate bounds of this specific sub-aggregate
483 Assoc : Node_Id;
484 Expr : Node_Id;
486 begin
487 Remove_Side_Effects (This_Low, Variable_Ref => True);
488 Remove_Side_Effects (This_High, Variable_Ref => True);
490 -- Collect the first N_Range for a given dimension that you find.
491 -- For a given dimension they must be all equal anyway.
493 if No (Aggr_Range (Dim)) then
494 Aggr_Low (Dim) := This_Low;
495 Aggr_High (Dim) := This_High;
496 Aggr_Range (Dim) := This_Range;
498 else
499 if Compile_Time_Known_Value (This_Low) then
500 if not Compile_Time_Known_Value (Aggr_Low (Dim)) then
501 Aggr_Low (Dim) := This_Low;
503 elsif Expr_Value (This_Low) /= Expr_Value (Aggr_Low (Dim)) then
504 Set_Raises_Constraint_Error (N);
505 Error_Msg_Warn := SPARK_Mode /= On;
506 Error_Msg_N ("sub-aggregate low bound mismatch<<", N);
507 Error_Msg_N ("\Constraint_Error [<<", N);
508 end if;
509 end if;
511 if Compile_Time_Known_Value (This_High) then
512 if not Compile_Time_Known_Value (Aggr_High (Dim)) then
513 Aggr_High (Dim) := This_High;
515 elsif
516 Expr_Value (This_High) /= Expr_Value (Aggr_High (Dim))
517 then
518 Set_Raises_Constraint_Error (N);
519 Error_Msg_Warn := SPARK_Mode /= On;
520 Error_Msg_N ("sub-aggregate high bound mismatch<<", N);
521 Error_Msg_N ("\Constraint_Error [<<", N);
522 end if;
523 end if;
524 end if;
526 if Dim < Aggr_Dimension then
528 if not Is_Null_Aggregate (N) then
530 -- Process positional components
532 if Present (Expressions (N)) then
533 Expr := First (Expressions (N));
534 while Present (Expr) loop
535 Collect_Aggr_Bounds (Expr, Dim + 1);
536 Next (Expr);
537 end loop;
538 end if;
540 -- Process component associations
542 if Present (Component_Associations (N)) then
543 Is_Fully_Positional := False;
545 Assoc := First (Component_Associations (N));
546 while Present (Assoc) loop
547 Expr := Expression (Assoc);
548 Collect_Aggr_Bounds (Expr, Dim + 1);
550 -- Propagate the error; it is not done in other cases to
551 -- avoid replacing this aggregate by a CE node (required
552 -- to report complementary warnings when the expression
553 -- is resolved).
555 if Is_Null_Aggregate (Expr)
556 and then Raises_Constraint_Error (Expr)
557 then
558 Set_Raises_Constraint_Error (N);
559 end if;
561 Next (Assoc);
562 end loop;
563 end if;
565 -- For null aggregates, build the bounds of their inner dimensions
566 -- (if not previously done). They are required for building the
567 -- aggregate itype.
569 elsif No (Aggr_Range (Dim + 1)) then
570 declare
571 Loc : constant Source_Ptr := Sloc (N);
572 Typ : constant Entity_Id := Etype (N);
573 Index : Node_Id;
574 Index_Typ : Entity_Id;
575 Lo, Hi : Node_Id;
576 Null_Range : Node_Id;
577 Num_Dim : Pos := 1;
579 begin
580 -- Move the index to the first dimension implicitly included
581 -- in this null aggregate.
583 Index := First_Index (Typ);
584 while Num_Dim <= Dim loop
585 Next_Index (Index);
586 Num_Dim := Num_Dim + 1;
587 end loop;
589 while Present (Index) loop
590 Get_Index_Bounds (Index, L => Lo, H => Hi);
591 Index_Typ := Etype (Index);
593 if Cannot_Compute_High_Bound (Index) then
594 -- To avoid reporting spurious errors we use the upper
595 -- bound as the higger bound of this index; this value
596 -- will not be used to generate code because this
597 -- aggregate will be replaced by a raise CE node.
599 Hi := New_Copy_Tree (Lo);
601 if not Raises_Constraint_Error (N) then
602 Report_Null_Array_Constraint_Error (N, Index_Typ);
603 Set_Raises_Constraint_Error (N);
604 end if;
606 else
607 -- The upper bound is the predecessor of the lower
608 -- bound.
610 Hi := Make_Attribute_Reference (Loc,
611 Prefix => New_Occurrence_Of (Index_Typ, Loc),
612 Attribute_Name => Name_Pred,
613 Expressions => New_List (New_Copy_Tree (Lo)));
614 end if;
616 Null_Range := Make_Range (Loc, New_Copy_Tree (Lo), Hi);
617 Analyze_And_Resolve (Null_Range, Index_Typ);
619 pragma Assert (No (Aggr_Range (Num_Dim)));
620 Aggr_Low (Num_Dim) := Low_Bound (Null_Range);
621 Aggr_High (Num_Dim) := High_Bound (Null_Range);
622 Aggr_Range (Num_Dim) := Null_Range;
624 Num_Dim := Num_Dim + 1;
625 Next_Index (Index);
626 end loop;
628 pragma Assert (Num_Dim = Aggr_Dimension + 1);
629 end;
630 end if;
631 end if;
632 end Collect_Aggr_Bounds;
634 -- Array_Aggr_Subtype variables
636 Itype : Entity_Id;
637 -- The final itype of the overall aggregate
639 Index_Constraints : constant List_Id := New_List;
640 -- The list of index constraints of the aggregate itype
642 -- Start of processing for Array_Aggr_Subtype
644 begin
645 -- Make sure that the list of index constraints is properly attached to
646 -- the tree, and then collect the aggregate bounds.
648 -- If no aggregate bounds have been set, this is an aggregate with
649 -- iterator specifications and a dynamic size to be determined by
650 -- first pass of expanded code.
652 if No (Aggregate_Bounds (N)) then
653 return Typ;
654 end if;
656 Set_Parent (Index_Constraints, N);
658 -- When resolving a null aggregate we created a list of aggregate bounds
659 -- for the consecutive dimensions. The bounds for the first dimension
660 -- are attached as the Aggregate_Bounds of the aggregate node.
662 if Is_Null_Aggregate (N) then
663 declare
664 This_Range : Node_Id := Aggregate_Bounds (N);
665 begin
666 for J in 1 .. Aggr_Dimension loop
667 Aggr_Range (J) := This_Range;
668 Next_Index (This_Range);
670 -- Remove bounds from the list, so they can be reattached as
671 -- the First_Index/Next_Index again by the code that also
672 -- handles non-null aggregates.
674 Remove (Aggr_Range (J));
675 end loop;
676 end;
677 else
678 Collect_Aggr_Bounds (N, 1);
679 end if;
681 -- Build the list of constrained indexes of our aggregate itype
683 for J in 1 .. Aggr_Dimension loop
684 Create_Index : declare
685 Index_Base : constant Entity_Id :=
686 Base_Type (Etype (Aggr_Range (J)));
687 Index_Typ : Entity_Id;
689 begin
690 -- Construct the Index subtype, and associate it with the range
691 -- construct that generates it.
693 Index_Typ :=
694 Create_Itype (Subtype_Kind (Ekind (Index_Base)), Aggr_Range (J));
696 Set_Etype (Index_Typ, Index_Base);
698 if Is_Character_Type (Index_Base) then
699 Set_Is_Character_Type (Index_Typ);
700 end if;
702 Set_Size_Info (Index_Typ, (Index_Base));
703 Set_RM_Size (Index_Typ, RM_Size (Index_Base));
704 Set_First_Rep_Item (Index_Typ, First_Rep_Item (Index_Base));
705 Set_Scalar_Range (Index_Typ, Aggr_Range (J));
707 if Is_Discrete_Or_Fixed_Point_Type (Index_Typ) then
708 Set_RM_Size (Index_Typ, UI_From_Int (Minimum_Size (Index_Typ)));
709 end if;
711 Set_Etype (Aggr_Range (J), Index_Typ);
713 Append (Aggr_Range (J), To => Index_Constraints);
714 end Create_Index;
715 end loop;
717 -- Now build the Itype
719 Itype := Create_Itype (E_Array_Subtype, N);
721 Set_First_Rep_Item (Itype, First_Rep_Item (Typ));
722 Set_Convention (Itype, Convention (Typ));
723 Set_Depends_On_Private (Itype, Has_Private_Component (Typ));
724 Set_Etype (Itype, Base_Type (Typ));
725 Set_Has_Alignment_Clause (Itype, Has_Alignment_Clause (Typ));
726 Set_Is_Aliased (Itype, Is_Aliased (Typ));
727 Set_Is_Independent (Itype, Is_Independent (Typ));
728 Set_Depends_On_Private (Itype, Depends_On_Private (Typ));
730 Copy_Suppress_Status (Index_Check, Typ, Itype);
731 Copy_Suppress_Status (Length_Check, Typ, Itype);
733 Set_First_Index (Itype, First (Index_Constraints));
734 Set_Is_Constrained (Itype, True);
735 Set_Is_Internal (Itype, True);
737 if Has_Predicates (Typ) then
738 Set_Has_Predicates (Itype);
740 -- If the base type has a predicate, capture the predicated parent
741 -- or the existing predicate function for SPARK use.
743 if Present (Predicate_Function (Typ)) then
744 Set_Predicate_Function (Itype, Predicate_Function (Typ));
746 elsif Is_Itype (Typ) then
747 Set_Predicated_Parent (Itype, Predicated_Parent (Typ));
749 else
750 Set_Predicated_Parent (Itype, Typ);
751 end if;
752 end if;
754 -- A simple optimization: purely positional aggregates of static
755 -- components should be passed to gigi unexpanded whenever possible, and
756 -- regardless of the staticness of the bounds themselves. Subsequent
757 -- checks in exp_aggr verify that type is not packed, etc.
759 Set_Size_Known_At_Compile_Time
760 (Itype,
761 Is_Fully_Positional
762 and then Comes_From_Source (N)
763 and then Size_Known_At_Compile_Time (Component_Type (Typ)));
765 -- We always need a freeze node for a packed array subtype, so that we
766 -- can build the Packed_Array_Impl_Type corresponding to the subtype. If
767 -- expansion is disabled, the packed array subtype is not built, and we
768 -- must not generate a freeze node for the type, or else it will appear
769 -- incomplete to gigi.
771 if Is_Packed (Itype)
772 and then not In_Spec_Expression
773 and then Expander_Active
774 then
775 Freeze_Itype (Itype, N);
776 end if;
778 return Itype;
779 end Array_Aggr_Subtype;
781 -------------------------------
782 -- Cannot_Compute_High_Bound --
783 -------------------------------
785 function Cannot_Compute_High_Bound (Index : Entity_Id) return Boolean is
786 Index_Type : constant Entity_Id := Etype (Index);
787 Lo, Hi : Node_Id;
789 begin
790 if not Is_Modular_Integer_Type (Index_Type)
791 and then not Is_Enumeration_Type (Index_Type)
792 then
793 return False;
795 elsif Index_Type = Base_Type (Index_Type) then
796 return True;
798 else
799 Get_Index_Bounds (Index, L => Lo, H => Hi);
801 if Compile_Time_Known_Value (Lo) then
802 if Is_Enumeration_Type (Index_Type)
803 and then not Is_Character_Type (Index_Type)
804 then
805 return Enumeration_Pos (Entity (Lo))
806 = Enumeration_Pos (First_Literal (Base_Type (Index_Type)));
807 else
808 return Expr_Value (Lo) = Uint_0;
809 end if;
810 end if;
811 end if;
813 return False;
814 end Cannot_Compute_High_Bound;
816 --------------------------------
817 -- Check_Misspelled_Component --
818 --------------------------------
820 procedure Check_Misspelled_Component
821 (Elements : Elist_Id;
822 Component : Node_Id)
824 Max_Suggestions : constant := 2;
826 Nr_Of_Suggestions : Natural := 0;
827 Suggestion_1 : Entity_Id := Empty;
828 Suggestion_2 : Entity_Id := Empty;
829 Component_Elmt : Elmt_Id;
831 begin
832 -- All the components of List are matched against Component and a count
833 -- is maintained of possible misspellings. When at the end of the
834 -- analysis there are one or two (not more) possible misspellings,
835 -- these misspellings will be suggested as possible corrections.
837 Component_Elmt := First_Elmt (Elements);
838 while Nr_Of_Suggestions <= Max_Suggestions
839 and then Present (Component_Elmt)
840 loop
841 if Is_Bad_Spelling_Of
842 (Chars (Node (Component_Elmt)),
843 Chars (Component))
844 then
845 Nr_Of_Suggestions := Nr_Of_Suggestions + 1;
847 case Nr_Of_Suggestions is
848 when 1 => Suggestion_1 := Node (Component_Elmt);
849 when 2 => Suggestion_2 := Node (Component_Elmt);
850 when others => null;
851 end case;
852 end if;
854 Next_Elmt (Component_Elmt);
855 end loop;
857 -- Report at most two suggestions
859 if Nr_Of_Suggestions = 1 then
860 Error_Msg_NE -- CODEFIX
861 ("\possible misspelling of&", Component, Suggestion_1);
863 elsif Nr_Of_Suggestions = 2 then
864 Error_Msg_Node_2 := Suggestion_2;
865 Error_Msg_NE -- CODEFIX
866 ("\possible misspelling of& or&", Component, Suggestion_1);
867 end if;
868 end Check_Misspelled_Component;
870 ----------------------------------------
871 -- Check_Expr_OK_In_Limited_Aggregate --
872 ----------------------------------------
874 procedure Check_Expr_OK_In_Limited_Aggregate (Expr : Node_Id) is
875 begin
876 if Is_Limited_Type (Etype (Expr))
877 and then Comes_From_Source (Expr)
878 then
879 if In_Instance_Body or else In_Inlined_Body then
880 null;
882 elsif not OK_For_Limited_Init (Etype (Expr), Expr) then
883 Error_Msg_N
884 ("initialization not allowed for limited types", Expr);
885 Explain_Limited_Type (Etype (Expr), Expr);
886 end if;
887 end if;
888 end Check_Expr_OK_In_Limited_Aggregate;
890 --------------------
891 -- Is_Deep_Choice --
892 --------------------
894 function Is_Deep_Choice
895 (Choice : Node_Id;
896 Aggr_Type : Type_Kind_Id) return Boolean
898 Pref : Node_Id := Choice;
899 begin
900 while not Is_Root_Prefix_Of_Deep_Choice (Pref) loop
901 Pref := Prefix (Pref);
902 end loop;
904 if Is_Array_Type (Aggr_Type) then
905 return Paren_Count (Pref) > 0
906 and then Pref /= Choice;
907 else
908 return Pref /= Choice;
909 end if;
910 end Is_Deep_Choice;
912 --------------------------
913 -- Is_Indexed_Aggregate --
914 --------------------------
916 function Is_Indexed_Aggregate
917 (N : N_Aggregate_Id;
918 Add_Unnamed : Node_Id;
919 New_Indexed : Node_Id) return Boolean
921 begin
922 if Present (New_Indexed)
923 and then not Is_Null_Aggregate (N)
924 then
925 if No (Add_Unnamed) then
926 return True;
928 else
929 declare
930 Comp_Assns : constant List_Id := Component_Associations (N);
931 Comp_Assn : Node_Id;
933 begin
934 if not Is_Empty_List (Comp_Assns) then
936 -- It suffices to look at the first association to determine
937 -- whether the aggregate is an indexed aggregate.
939 Comp_Assn := First (Comp_Assns);
941 -- Test for the component association being either:
943 -- 1) an N_Component_Association node, in which case there
944 -- is a list of choices (the "key choices");
946 -- or else:
948 -- 2) an N_Iterated_Component_Association node that has
949 -- a Defining_Identifier, in which case it has
950 -- Discrete_Choices that effectively make it
951 -- equivalent to a Loop_Parameter_Specification;
953 -- or else:
955 -- 3) an N_Iterated_Element_Association node with
956 -- a Loop_Parameter_Specification with a discrete
957 -- subtype or range.
959 -- This basically corresponds to the definition of indexed
960 -- aggregates (in RM22 4.3.5(25/5)), but the GNAT tree
961 -- representation doesn't always directly match the RM
962 -- syntax for various reasons.
964 if Nkind (Comp_Assn) = N_Component_Association
965 or else
966 (Nkind (Comp_Assn) = N_Iterated_Component_Association
967 and then Present (Defining_Identifier (Comp_Assn)))
968 then
969 return True;
971 -- In the case of an iterated_element_association with a
972 -- loop_parameter_specification, we have to look deeper to
973 -- confirm that it is not actually an iterator_specification
974 -- masquerading as a loop_parameter_specification. Those can
975 -- share syntax (for example, having the iterator of form
976 -- "for C in <function-call>") and a rewrite into an
977 -- iterator_specification can happen later.
979 elsif Nkind (Comp_Assn) = N_Iterated_Element_Association
980 and then Present (Loop_Parameter_Specification (Comp_Assn))
981 then
982 declare
983 Loop_Parm_Spec : constant Node_Id :=
984 Loop_Parameter_Specification (Comp_Assn);
985 Discr_Subt_Defn : constant Node_Id :=
986 Discrete_Subtype_Definition (Loop_Parm_Spec);
987 begin
988 if Nkind (Discr_Subt_Defn) = N_Range
989 or else
990 Nkind (Discr_Subt_Defn) = N_Subtype_Indication
991 or else
992 (Is_Entity_Name (Discr_Subt_Defn)
993 and then
994 Is_Type (Entity (Discr_Subt_Defn)))
995 then
996 return True;
997 end if;
998 end;
999 end if;
1000 end if;
1001 end;
1002 end if;
1003 end if;
1005 return False;
1006 end Is_Indexed_Aggregate;
1008 -------------------------
1009 -- Is_Others_Aggregate --
1010 -------------------------
1012 function Is_Others_Aggregate (Aggr : Node_Id) return Boolean is
1013 Assoc : constant List_Id := Component_Associations (Aggr);
1015 begin
1016 return No (Expressions (Aggr))
1017 and then Nkind (First (Choice_List (First (Assoc)))) = N_Others_Choice;
1018 end Is_Others_Aggregate;
1020 -----------------------------------
1021 -- Is_Root_Prefix_Of_Deep_Choice --
1022 -----------------------------------
1024 function Is_Root_Prefix_Of_Deep_Choice (Pref : Node_Id) return Boolean is
1025 begin
1026 return Paren_Count (Pref) > 0
1027 or else Nkind (Pref) not in N_Indexed_Component
1028 | N_Selected_Component;
1029 end Is_Root_Prefix_Of_Deep_Choice;
1031 -------------------------
1032 -- Is_Single_Aggregate --
1033 -------------------------
1035 function Is_Single_Aggregate (Aggr : Node_Id) return Boolean is
1036 Assoc : constant List_Id := Component_Associations (Aggr);
1038 begin
1039 return No (Expressions (Aggr))
1040 and then No (Next (First (Assoc)))
1041 and then No (Next (First (Choice_List (First (Assoc)))));
1042 end Is_Single_Aggregate;
1044 -----------------------
1045 -- Is_Null_Aggregate --
1046 -----------------------
1048 function Is_Null_Aggregate (N : Node_Id) return Boolean is
1049 begin
1050 return Ada_Version >= Ada_2022
1051 and then Is_Homogeneous_Aggregate (N)
1052 and then Is_Empty_List (Expressions (N))
1053 and then Is_Empty_List (Component_Associations (N));
1054 end Is_Null_Aggregate;
1056 ----------------------------------------
1057 -- Is_Null_Array_Aggregate_High_Bound --
1058 ----------------------------------------
1060 function Is_Null_Array_Aggregate_High_Bound (N : Node_Id) return Boolean is
1061 Original_N : constant Node_Id := Original_Node (N);
1062 begin
1063 return Ada_Version >= Ada_2022
1064 and then not Comes_From_Source (Original_N)
1065 and then Nkind (Original_N) = N_Attribute_Reference
1066 and then
1067 Get_Attribute_Id (Attribute_Name (Original_N)) = Attribute_Pred
1068 and then Nkind (Parent (N)) in N_Range | N_Op_Le
1069 and then not Comes_From_Source (Parent (N));
1070 end Is_Null_Array_Aggregate_High_Bound;
1072 --------------------------------
1073 -- Make_String_Into_Aggregate --
1074 --------------------------------
1076 procedure Make_String_Into_Aggregate (N : Node_Id) is
1077 Exprs : constant List_Id := New_List;
1078 Loc : constant Source_Ptr := Sloc (N);
1079 Str : constant String_Id := Strval (N);
1080 Strlen : constant Nat := String_Length (Str);
1081 C : Char_Code;
1082 C_Node : Node_Id;
1083 New_N : Node_Id;
1084 P : Source_Ptr;
1086 begin
1087 P := Loc + 1;
1088 for J in 1 .. Strlen loop
1089 C := Get_String_Char (Str, J);
1090 Set_Character_Literal_Name (C);
1092 C_Node :=
1093 Make_Character_Literal (P,
1094 Chars => Name_Find,
1095 Char_Literal_Value => UI_From_CC (C));
1096 Set_Etype (C_Node, Any_Character);
1097 Append_To (Exprs, C_Node);
1099 P := P + 1;
1100 -- Something special for wide strings???
1101 end loop;
1103 New_N := Make_Aggregate (Loc, Expressions => Exprs);
1104 Set_Analyzed (New_N);
1105 Set_Etype (New_N, Any_Composite);
1107 Rewrite (N, New_N);
1108 end Make_String_Into_Aggregate;
1110 ----------------------------------------
1111 -- Report_Null_Array_Constraint_Error --
1112 ----------------------------------------
1114 procedure Report_Null_Array_Constraint_Error
1115 (N : Node_Id;
1116 Index_Typ : Entity_Id) is
1117 begin
1118 Error_Msg_Warn := SPARK_Mode /= On;
1120 if Is_Modular_Integer_Type (Index_Typ) then
1121 Error_Msg_N
1122 ("null array aggregate indexed by a modular type<<", N);
1123 else
1124 Error_Msg_N
1125 ("null array aggregate indexed by an enumeration type<<", N);
1126 end if;
1128 Error_Msg_N ("\Constraint_Error [<<", N);
1129 end Report_Null_Array_Constraint_Error;
1131 -----------------------
1132 -- Resolve_Aggregate --
1133 -----------------------
1135 procedure Resolve_Aggregate (N : Node_Id; Typ : Entity_Id) is
1136 Loc : constant Source_Ptr := Sloc (N);
1138 Aggr_Subtyp : Entity_Id;
1139 -- The actual aggregate subtype. This is not necessarily the same as Typ
1140 -- which is the subtype of the context in which the aggregate was found.
1142 Others_Box : Boolean := False;
1143 -- Set to True if N represents a simple aggregate with only
1144 -- (others => <>), not nested as part of another aggregate.
1146 function Is_Full_Access_Aggregate (N : Node_Id) return Boolean;
1147 -- If a full access object is initialized with an aggregate or is
1148 -- assigned an aggregate, we have to prevent a piecemeal access or
1149 -- assignment to the object, even if the aggregate is to be expanded.
1150 -- We create a temporary for the aggregate, and assign the temporary
1151 -- instead, so that the back end can generate an atomic move for it.
1152 -- This is only done in the context of an object declaration or an
1153 -- assignment. Function is a noop and returns false in other contexts.
1155 function Within_Aggregate (N : Node_Id) return Boolean;
1156 -- Return True if N is part of an N_Aggregate
1158 ------------------------------
1159 -- Is_Full_Access_Aggregate --
1160 ------------------------------
1162 function Is_Full_Access_Aggregate (N : Node_Id) return Boolean is
1163 Loc : constant Source_Ptr := Sloc (N);
1165 New_N : Node_Id;
1166 Par : Node_Id;
1167 Temp : Entity_Id;
1168 Typ : Entity_Id;
1170 begin
1171 Par := Parent (N);
1173 -- Aggregate may be qualified, so find outer context
1175 if Nkind (Par) = N_Qualified_Expression then
1176 Par := Parent (Par);
1177 end if;
1179 if not Comes_From_Source (Par) then
1180 return False;
1181 end if;
1183 case Nkind (Par) is
1184 when N_Assignment_Statement =>
1185 Typ := Etype (Name (Par));
1187 if not Is_Full_Access (Typ)
1188 and then not Is_Full_Access_Object (Name (Par))
1189 then
1190 return False;
1191 end if;
1193 when N_Object_Declaration =>
1194 Typ := Etype (Defining_Identifier (Par));
1196 if not Is_Full_Access (Typ)
1197 and then not Is_Full_Access (Defining_Identifier (Par))
1198 then
1199 return False;
1200 end if;
1202 when others =>
1203 return False;
1204 end case;
1206 Temp := Make_Temporary (Loc, 'T', N);
1207 New_N :=
1208 Make_Object_Declaration (Loc,
1209 Defining_Identifier => Temp,
1210 Constant_Present => True,
1211 Object_Definition => New_Occurrence_Of (Typ, Loc),
1212 Expression => Relocate_Node (N));
1213 Insert_Action (Par, New_N);
1215 Rewrite (N, New_Occurrence_Of (Temp, Loc));
1216 Analyze_And_Resolve (N, Typ);
1218 return True;
1219 end Is_Full_Access_Aggregate;
1221 ----------------------
1222 -- Within_Aggregate --
1223 ----------------------
1225 function Within_Aggregate (N : Node_Id) return Boolean is
1226 P : Node_Id := Parent (N);
1227 begin
1228 while Present (P) loop
1229 if Nkind (P) = N_Aggregate then
1230 return True;
1231 end if;
1233 P := Parent (P);
1234 end loop;
1236 return False;
1237 end Within_Aggregate;
1239 -- Start of processing for Resolve_Aggregate
1241 begin
1242 -- Ignore junk empty aggregate resulting from parser error
1244 if No (Expressions (N))
1245 and then No (Component_Associations (N))
1246 and then not Null_Record_Present (N)
1247 then
1248 return;
1250 -- If the aggregate is assigned to a full access variable, we have
1251 -- to prevent a piecemeal assignment even if the aggregate is to be
1252 -- expanded. We create a temporary for the aggregate, and assign the
1253 -- temporary instead, so that the back end can generate an atomic move
1254 -- for it. This is properly an expansion activity but it must be done
1255 -- before resolution because aggregate resolution cannot be done twice.
1257 elsif Expander_Active and then Is_Full_Access_Aggregate (N) then
1258 return;
1259 end if;
1261 -- If the aggregate has box-initialized components, its type must be
1262 -- frozen so that initialization procedures can properly be called
1263 -- in the resolution that follows. The replacement of boxes with
1264 -- initialization calls is properly an expansion activity but it must
1265 -- be done during resolution.
1267 if Expander_Active
1268 and then Present (Component_Associations (N))
1269 then
1270 declare
1271 Comp : Node_Id;
1272 First_Comp : Boolean := True;
1274 begin
1275 Comp := First (Component_Associations (N));
1276 while Present (Comp) loop
1277 if Box_Present (Comp) then
1278 if First_Comp
1279 and then No (Expressions (N))
1280 and then Nkind (First (Choices (Comp))) = N_Others_Choice
1281 and then not Within_Aggregate (N)
1282 then
1283 Others_Box := True;
1284 end if;
1286 Insert_Actions (N, Freeze_Entity (Typ, N));
1287 exit;
1288 end if;
1290 First_Comp := False;
1291 Next (Comp);
1292 end loop;
1293 end;
1294 end if;
1296 -- Check for aggregates not allowed in configurable run-time mode.
1297 -- We allow all cases of aggregates that do not come from source, since
1298 -- these are all assumed to be small (e.g. bounds of a string literal).
1299 -- We also allow aggregates of types we know to be small.
1301 if not Support_Aggregates_On_Target
1302 and then Comes_From_Source (N)
1303 and then (not Known_Static_Esize (Typ)
1304 or else Esize (Typ) > System_Max_Integer_Size)
1305 then
1306 Error_Msg_CRT ("aggregate", N);
1307 end if;
1309 -- Ada 2005 (AI-287): Limited aggregates allowed
1311 -- In an instance, ignore aggregate subcomponents that may be limited,
1312 -- because they originate in view conflicts. If the original aggregate
1313 -- is legal and the actuals are legal, the aggregate itself is legal.
1315 if Is_Limited_Type (Typ)
1316 and then Ada_Version < Ada_2005
1317 and then not In_Instance
1318 then
1319 Error_Msg_N ("aggregate type cannot be limited", N);
1320 Explain_Limited_Type (Typ, N);
1322 elsif Is_Class_Wide_Type (Typ) then
1323 Error_Msg_N ("type of aggregate cannot be class-wide", N);
1325 elsif Typ = Any_String
1326 or else Typ = Any_Composite
1327 then
1328 Error_Msg_N ("no unique type for aggregate", N);
1329 Set_Etype (N, Any_Composite);
1331 elsif Is_Array_Type (Typ) and then Null_Record_Present (N) then
1332 Error_Msg_N ("null record forbidden in array aggregate", N);
1334 elsif Is_Record_Type (Typ)
1335 and then not Is_Homogeneous_Aggregate (N)
1336 then
1337 Resolve_Record_Aggregate (N, Typ);
1339 elsif Has_Aspect (Typ, Aspect_Aggregate)
1340 and then Ada_Version >= Ada_2022
1341 then
1342 -- Check for Ada 2022 and () aggregate.
1344 if not Is_Homogeneous_Aggregate (N) then
1345 Error_Msg_N ("container aggregate must use '['], not ()", N);
1346 end if;
1348 Resolve_Container_Aggregate (N, Typ);
1350 -- Check for an attempt to use "[]" for an aggregate of a record type
1351 -- after handling the case where the type has an Aggregate aspect,
1352 -- because the aspect can be specified for record types, but if it
1353 -- wasn't specified, then this is an error.
1355 elsif Is_Record_Type (Typ) and then Is_Homogeneous_Aggregate (N) then
1356 Error_Msg_N ("record aggregate must use (), not '[']", N);
1358 elsif Is_Array_Type (Typ) then
1360 -- First a special test, for the case of a positional aggregate of
1361 -- characters which can be replaced by a string literal.
1363 -- Do not perform this transformation if this was a string literal
1364 -- to start with, whose components needed constraint checks, or if
1365 -- the component type is non-static, because it will require those
1366 -- checks and be transformed back into an aggregate. If the index
1367 -- type is not Integer the aggregate may represent a user-defined
1368 -- string type but the context might need the original type so we
1369 -- do not perform the transformation at this point.
1371 if Number_Dimensions (Typ) = 1
1372 and then Is_Standard_Character_Type (Component_Type (Typ))
1373 and then No (Component_Associations (N))
1374 and then not Is_Limited_Composite (Typ)
1375 and then not Is_Private_Composite (Typ)
1376 and then not Is_Bit_Packed_Array (Typ)
1377 and then Nkind (Original_Node (Parent (N))) /= N_String_Literal
1378 and then Is_OK_Static_Subtype (Component_Type (Typ))
1379 and then Base_Type (Etype (First_Index (Typ))) =
1380 Base_Type (Standard_Integer)
1381 then
1382 declare
1383 Expr : Node_Id;
1385 begin
1386 Expr := First (Expressions (N));
1387 while Present (Expr) loop
1388 exit when Nkind (Expr) /= N_Character_Literal;
1389 Next (Expr);
1390 end loop;
1392 if No (Expr) then
1393 Start_String;
1395 Expr := First (Expressions (N));
1396 while Present (Expr) loop
1397 Store_String_Char (UI_To_CC (Char_Literal_Value (Expr)));
1398 Next (Expr);
1399 end loop;
1401 Rewrite (N, Make_String_Literal (Loc, End_String));
1403 Analyze_And_Resolve (N, Typ);
1404 return;
1405 end if;
1406 end;
1407 end if;
1409 -- Here if we have a real aggregate to deal with
1411 Array_Aggregate : declare
1412 Aggr_Resolved : Boolean;
1413 Aggr_Typ : constant Entity_Id := Etype (Typ);
1414 -- This is the unconstrained array type, which is the type against
1415 -- which the aggregate is to be resolved. Typ itself is the array
1416 -- type of the context which may not be the same subtype as the
1417 -- subtype for the final aggregate.
1419 Is_Null_Aggr : constant Boolean := Is_Null_Aggregate (N);
1421 begin
1422 -- In the following we determine whether an OTHERS choice is
1423 -- allowed inside the array aggregate. The test checks the context
1424 -- in which the array aggregate occurs. If the context does not
1425 -- permit it, or the aggregate type is unconstrained, an OTHERS
1426 -- choice is not allowed (except that it is always allowed on the
1427 -- right-hand side of an assignment statement; in this case the
1428 -- constrainedness of the type doesn't matter, because an array
1429 -- object is always constrained).
1431 -- If expansion is disabled (generic context, or semantics-only
1432 -- mode) actual subtypes cannot be constructed, and the type of an
1433 -- object may be its unconstrained nominal type. However, if the
1434 -- context is an assignment statement, OTHERS is allowed, because
1435 -- the target of the assignment will have a constrained subtype
1436 -- when fully compiled. Ditto if the context is an initialization
1437 -- procedure where a component may have a predicate function that
1438 -- carries the base type.
1440 -- Note that there is no node for Explicit_Actual_Parameter.
1441 -- To test for this context we therefore have to test for node
1442 -- N_Parameter_Association which itself appears only if there is a
1443 -- formal parameter. Consequently we also need to test for
1444 -- N_Procedure_Call_Statement or N_Function_Call.
1446 -- The context may be an N_Reference node, created by expansion.
1447 -- Legality of the others clause was established in the source,
1448 -- so the context is legal.
1450 Set_Etype (N, Aggr_Typ); -- May be overridden later on
1452 if Is_Null_Aggr then
1453 Set_Etype (N, Typ);
1454 Aggr_Resolved := Resolve_Null_Array_Aggregate (N);
1456 elsif Nkind (Parent (N)) = N_Assignment_Statement
1457 or else Inside_Init_Proc
1458 or else (Is_Constrained (Typ)
1459 and then Nkind (Parent (N)) in
1460 N_Parameter_Association
1461 | N_Function_Call
1462 | N_Procedure_Call_Statement
1463 | N_Generic_Association
1464 | N_Formal_Object_Declaration
1465 | N_Simple_Return_Statement
1466 | N_Object_Declaration
1467 | N_Component_Declaration
1468 | N_Parameter_Specification
1469 | N_Qualified_Expression
1470 | N_Unchecked_Type_Conversion
1471 | N_Reference
1472 | N_Aggregate
1473 | N_Extension_Aggregate
1474 | N_Component_Association
1475 | N_Case_Expression_Alternative
1476 | N_If_Expression
1477 | N_Expression_With_Actions)
1478 then
1479 Aggr_Resolved :=
1480 Resolve_Array_Aggregate
1482 Index => First_Index (Aggr_Typ),
1483 Index_Constr => First_Index (Typ),
1484 Component_Typ => Component_Type (Typ),
1485 Others_Allowed => True);
1486 else
1487 Aggr_Resolved :=
1488 Resolve_Array_Aggregate
1490 Index => First_Index (Aggr_Typ),
1491 Index_Constr => First_Index (Aggr_Typ),
1492 Component_Typ => Component_Type (Typ),
1493 Others_Allowed => False);
1494 end if;
1496 if not Aggr_Resolved then
1498 -- A parenthesized expression may have been intended as an
1499 -- aggregate, leading to a type error when analyzing the
1500 -- component. This can also happen for a nested component
1501 -- (see Analyze_Aggr_Expr).
1503 if Paren_Count (N) > 0 then
1504 Error_Msg_N
1505 ("positional aggregate cannot have one component", N);
1506 end if;
1508 Aggr_Subtyp := Any_Composite;
1510 else
1511 Aggr_Subtyp := Array_Aggr_Subtype (N, Typ);
1512 end if;
1514 Set_Etype (N, Aggr_Subtyp);
1515 end Array_Aggregate;
1517 elsif Is_Private_Type (Typ)
1518 and then Present (Full_View (Typ))
1519 and then (In_Inlined_Body or In_Instance_Body)
1520 and then Is_Composite_Type (Full_View (Typ))
1521 then
1522 Resolve (N, Full_View (Typ));
1524 else
1525 Error_Msg_N ("illegal context for aggregate", N);
1526 end if;
1528 -- If we can determine statically that the evaluation of the aggregate
1529 -- raises Constraint_Error, then replace the aggregate with an
1530 -- N_Raise_Constraint_Error node, but set the Etype to the right
1531 -- aggregate subtype. Gigi needs this.
1533 if Raises_Constraint_Error (N) then
1534 Aggr_Subtyp := Etype (N);
1535 Rewrite (N,
1536 Make_Raise_Constraint_Error (Loc, Reason => CE_Range_Check_Failed));
1537 Set_Raises_Constraint_Error (N);
1538 Set_Etype (N, Aggr_Subtyp);
1539 Set_Analyzed (N);
1540 end if;
1542 if Warn_On_No_Value_Assigned
1543 and then Others_Box
1544 and then not Is_Fully_Initialized_Type (Etype (N))
1545 then
1546 Error_Msg_N ("?v?aggregate not fully initialized", N);
1547 end if;
1549 Check_Function_Writable_Actuals (N);
1550 end Resolve_Aggregate;
1552 -----------------------------
1553 -- Resolve_Array_Aggregate --
1554 -----------------------------
1556 function Resolve_Array_Aggregate
1557 (N : Node_Id;
1558 Index : Node_Id;
1559 Index_Constr : Node_Id;
1560 Component_Typ : Entity_Id;
1561 Others_Allowed : Boolean) return Boolean
1563 Loc : constant Source_Ptr := Sloc (N);
1565 Failure : constant Boolean := False;
1566 Success : constant Boolean := True;
1568 Index_Typ : constant Entity_Id := Etype (Index);
1569 Index_Typ_Low : constant Node_Id := Type_Low_Bound (Index_Typ);
1570 Index_Typ_High : constant Node_Id := Type_High_Bound (Index_Typ);
1571 -- The type of the index corresponding to the array sub-aggregate along
1572 -- with its low and upper bounds.
1574 Index_Base : constant Entity_Id := Base_Type (Index_Typ);
1575 Index_Base_Low : constant Node_Id := Type_Low_Bound (Index_Base);
1576 Index_Base_High : constant Node_Id := Type_High_Bound (Index_Base);
1577 -- Ditto for the base type
1579 Others_N : Node_Id := Empty;
1581 Nb_Choices : Nat := 0;
1582 -- Contains the overall number of named choices in this sub-aggregate
1584 function Add (Val : Uint; To : Node_Id) return Node_Id;
1585 -- Creates a new expression node where Val is added to expression To.
1586 -- Tries to constant fold whenever possible. To must be an already
1587 -- analyzed expression.
1589 procedure Check_Bound (BH : Node_Id; AH : in out Node_Id);
1590 -- Checks that AH (the upper bound of an array aggregate) is less than
1591 -- or equal to BH (the upper bound of the index base type). If the check
1592 -- fails, a warning is emitted, the Raises_Constraint_Error flag of N is
1593 -- set, and AH is replaced with a duplicate of BH.
1595 procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id);
1596 -- Checks that range AL .. AH is compatible with range L .. H. Emits a
1597 -- warning if not and sets the Raises_Constraint_Error flag in N.
1599 procedure Check_Length (L, H : Node_Id; Len : Uint);
1600 -- Checks that range L .. H contains at least Len elements. Emits a
1601 -- warning if not and sets the Raises_Constraint_Error flag in N.
1603 function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean;
1604 -- Returns True if range L .. H is dynamic or null
1606 procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean);
1607 -- Given expression node From, this routine sets OK to False if it
1608 -- cannot statically evaluate From. Otherwise it stores this static
1609 -- value into Value.
1611 function Has_Null_Aggregate_Raising_Constraint_Error
1612 (Expr : Node_Id) return Boolean;
1613 -- Determines if the given expression has some null aggregate that will
1614 -- cause raising CE at runtime.
1616 function Resolve_Aggr_Expr
1617 (Expr : Node_Id;
1618 Single_Elmt : Boolean) return Boolean;
1619 -- Resolves aggregate expression Expr. Returns False if resolution
1620 -- fails. If Single_Elmt is set to False, the expression Expr may be
1621 -- used to initialize several array aggregate elements (this can happen
1622 -- for discrete choices such as "L .. H => Expr" or the OTHERS choice).
1623 -- In this event we do not resolve Expr unless expansion is disabled.
1624 -- To know why, see the DELAYED COMPONENT RESOLUTION note above.
1626 -- NOTE: In the case of "... => <>", we pass the N_Component_Association
1627 -- node as Expr, since there is no Expression and we need a Sloc for the
1628 -- error message.
1630 procedure Resolve_Iterated_Component_Association
1631 (N : Node_Id;
1632 Index_Typ : Entity_Id);
1633 -- For AI12-061
1635 function Subtract (Val : Uint; To : Node_Id) return Node_Id;
1636 -- Creates a new expression node where Val is subtracted to expression
1637 -- To. Tries to constant fold whenever possible. To must be an already
1638 -- analyzed expression.
1640 procedure Warn_On_Null_Component_Association (Expr : Node_Id);
1641 -- Expr is either a conditional expression or a case expression of an
1642 -- iterated component association initializing the aggregate N with
1643 -- components that can never be null. Report warning on associations
1644 -- that may initialize some component with a null value.
1646 ---------
1647 -- Add --
1648 ---------
1650 function Add (Val : Uint; To : Node_Id) return Node_Id is
1651 Expr_Pos : Node_Id;
1652 Expr : Node_Id;
1653 To_Pos : Node_Id;
1655 begin
1656 if Raises_Constraint_Error (To) then
1657 return To;
1658 end if;
1660 -- First test if we can do constant folding
1662 if Compile_Time_Known_Value (To)
1663 or else Nkind (To) = N_Integer_Literal
1664 then
1665 Expr_Pos := Make_Integer_Literal (Loc, Expr_Value (To) + Val);
1666 Set_Is_Static_Expression (Expr_Pos);
1667 Set_Etype (Expr_Pos, Etype (To));
1668 Set_Analyzed (Expr_Pos, Analyzed (To));
1670 if not Is_Enumeration_Type (Index_Typ) then
1671 Expr := Expr_Pos;
1673 -- If we are dealing with enumeration return
1674 -- Index_Typ'Val (Expr_Pos)
1676 else
1677 Expr :=
1678 Make_Attribute_Reference
1679 (Loc,
1680 Prefix => New_Occurrence_Of (Index_Typ, Loc),
1681 Attribute_Name => Name_Val,
1682 Expressions => New_List (Expr_Pos));
1683 end if;
1685 return Expr;
1686 end if;
1688 -- If we are here no constant folding possible
1690 if not Is_Enumeration_Type (Index_Base) then
1691 Expr :=
1692 Make_Op_Add (Loc,
1693 Left_Opnd => Duplicate_Subexpr (To),
1694 Right_Opnd => Make_Integer_Literal (Loc, Val));
1696 -- If we are dealing with enumeration return
1697 -- Index_Typ'Val (Index_Typ'Pos (To) + Val)
1699 else
1700 To_Pos :=
1701 Make_Attribute_Reference
1702 (Loc,
1703 Prefix => New_Occurrence_Of (Index_Typ, Loc),
1704 Attribute_Name => Name_Pos,
1705 Expressions => New_List (Duplicate_Subexpr (To)));
1707 Expr_Pos :=
1708 Make_Op_Add (Loc,
1709 Left_Opnd => To_Pos,
1710 Right_Opnd => Make_Integer_Literal (Loc, Val));
1712 Expr :=
1713 Make_Attribute_Reference
1714 (Loc,
1715 Prefix => New_Occurrence_Of (Index_Typ, Loc),
1716 Attribute_Name => Name_Val,
1717 Expressions => New_List (Expr_Pos));
1719 -- If the index type has a non standard representation, the
1720 -- attributes 'Val and 'Pos expand into function calls and the
1721 -- resulting expression is considered non-safe for reevaluation
1722 -- by the backend. Relocate it into a constant temporary in order
1723 -- to make it safe for reevaluation.
1725 if Has_Non_Standard_Rep (Etype (N)) then
1726 declare
1727 Def_Id : Entity_Id;
1729 begin
1730 Def_Id := Make_Temporary (Loc, 'R', Expr);
1731 Set_Etype (Def_Id, Index_Typ);
1732 Insert_Action (N,
1733 Make_Object_Declaration (Loc,
1734 Defining_Identifier => Def_Id,
1735 Object_Definition =>
1736 New_Occurrence_Of (Index_Typ, Loc),
1737 Constant_Present => True,
1738 Expression => Relocate_Node (Expr)));
1740 Expr := New_Occurrence_Of (Def_Id, Loc);
1741 end;
1742 end if;
1743 end if;
1745 return Expr;
1746 end Add;
1748 -----------------
1749 -- Check_Bound --
1750 -----------------
1752 procedure Check_Bound (BH : Node_Id; AH : in out Node_Id) is
1753 Val_BH : Uint;
1754 Val_AH : Uint;
1756 OK_BH : Boolean;
1757 OK_AH : Boolean;
1759 begin
1760 Get (Value => Val_BH, From => BH, OK => OK_BH);
1761 Get (Value => Val_AH, From => AH, OK => OK_AH);
1763 if OK_BH and then OK_AH and then Val_BH < Val_AH then
1764 Set_Raises_Constraint_Error (N);
1765 Error_Msg_Warn := SPARK_Mode /= On;
1766 Error_Msg_N ("upper bound out of range<<", AH);
1767 Error_Msg_N ("\Constraint_Error [<<", AH);
1769 -- You need to set AH to BH or else in the case of enumerations
1770 -- indexes we will not be able to resolve the aggregate bounds.
1772 AH := Duplicate_Subexpr (BH);
1773 end if;
1774 end Check_Bound;
1776 ------------------
1777 -- Check_Bounds --
1778 ------------------
1780 procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id) is
1781 Val_L : Uint;
1782 Val_H : Uint;
1783 Val_AL : Uint;
1784 Val_AH : Uint;
1786 OK_L : Boolean;
1787 OK_H : Boolean;
1789 OK_AL : Boolean;
1790 OK_AH : Boolean;
1791 pragma Warnings (Off, OK_AL);
1792 pragma Warnings (Off, OK_AH);
1794 begin
1795 if Raises_Constraint_Error (N)
1796 or else Dynamic_Or_Null_Range (AL, AH)
1797 then
1798 return;
1799 end if;
1801 Get (Value => Val_L, From => L, OK => OK_L);
1802 Get (Value => Val_H, From => H, OK => OK_H);
1804 Get (Value => Val_AL, From => AL, OK => OK_AL);
1805 Get (Value => Val_AH, From => AH, OK => OK_AH);
1807 if OK_L and then Val_L > Val_AL then
1808 Set_Raises_Constraint_Error (N);
1809 Error_Msg_Warn := SPARK_Mode /= On;
1810 Error_Msg_N ("lower bound of aggregate out of range<<", N);
1811 Error_Msg_N ("\Constraint_Error [<<", N);
1812 end if;
1814 if OK_H and then Val_H < Val_AH then
1815 Set_Raises_Constraint_Error (N);
1816 Error_Msg_Warn := SPARK_Mode /= On;
1817 Error_Msg_N ("upper bound of aggregate out of range<<", N);
1818 Error_Msg_N ("\Constraint_Error [<<", N);
1819 end if;
1820 end Check_Bounds;
1822 ------------------
1823 -- Check_Length --
1824 ------------------
1826 procedure Check_Length (L, H : Node_Id; Len : Uint) is
1827 Val_L : Uint;
1828 Val_H : Uint;
1830 OK_L : Boolean;
1831 OK_H : Boolean;
1833 Range_Len : Uint;
1835 begin
1836 if Raises_Constraint_Error (N) then
1837 return;
1838 end if;
1840 Get (Value => Val_L, From => L, OK => OK_L);
1841 Get (Value => Val_H, From => H, OK => OK_H);
1843 if not OK_L or else not OK_H then
1844 return;
1845 end if;
1847 -- If null range length is zero
1849 if Val_L > Val_H then
1850 Range_Len := Uint_0;
1851 else
1852 Range_Len := Val_H - Val_L + 1;
1853 end if;
1855 if Range_Len < Len then
1856 Set_Raises_Constraint_Error (N);
1857 Error_Msg_Warn := SPARK_Mode /= On;
1858 Error_Msg_N ("too many elements<<", N);
1859 Error_Msg_N ("\Constraint_Error [<<", N);
1860 end if;
1861 end Check_Length;
1863 ---------------------------
1864 -- Dynamic_Or_Null_Range --
1865 ---------------------------
1867 function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean is
1868 Val_L : Uint;
1869 Val_H : Uint;
1871 OK_L : Boolean;
1872 OK_H : Boolean;
1874 begin
1875 Get (Value => Val_L, From => L, OK => OK_L);
1876 Get (Value => Val_H, From => H, OK => OK_H);
1878 return not OK_L or else not OK_H
1879 or else not Is_OK_Static_Expression (L)
1880 or else not Is_OK_Static_Expression (H)
1881 or else Val_L > Val_H;
1882 end Dynamic_Or_Null_Range;
1884 ---------
1885 -- Get --
1886 ---------
1888 procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean) is
1889 begin
1890 OK := True;
1892 if Compile_Time_Known_Value (From) then
1893 Value := Expr_Value (From);
1895 -- If expression From is something like Some_Type'Val (10) then
1896 -- Value = 10.
1898 elsif Nkind (From) = N_Attribute_Reference
1899 and then Attribute_Name (From) = Name_Val
1900 and then Compile_Time_Known_Value (First (Expressions (From)))
1901 then
1902 Value := Expr_Value (First (Expressions (From)));
1903 else
1904 Value := Uint_0;
1905 OK := False;
1906 end if;
1907 end Get;
1909 -------------------------------------------------
1910 -- Has_Null_Aggregate_Raising_Constraint_Error --
1911 -------------------------------------------------
1913 function Has_Null_Aggregate_Raising_Constraint_Error
1914 (Expr : Node_Id) return Boolean
1916 function Process (N : Node_Id) return Traverse_Result;
1917 -- Process one node in search for generic formal type
1919 -------------
1920 -- Process --
1921 -------------
1923 function Process (N : Node_Id) return Traverse_Result is
1924 begin
1925 if Nkind (N) = N_Aggregate
1926 and then Is_Null_Aggregate (N)
1927 and then Raises_Constraint_Error (N)
1928 then
1929 return Abandon;
1930 end if;
1932 return OK;
1933 end Process;
1935 function Traverse is new Traverse_Func (Process);
1936 -- Traverse tree to look for null aggregates that will raise CE
1938 -- Start of processing for Has_Null_Aggregate_Raising_Constraint_Error
1940 begin
1941 return Traverse (Expr) = Abandon;
1942 end Has_Null_Aggregate_Raising_Constraint_Error;
1944 -----------------------
1945 -- Resolve_Aggr_Expr --
1946 -----------------------
1948 function Resolve_Aggr_Expr
1949 (Expr : Node_Id;
1950 Single_Elmt : Boolean) return Boolean
1952 Nxt_Ind : constant Node_Id := Next_Index (Index);
1953 Nxt_Ind_Constr : constant Node_Id := Next_Index (Index_Constr);
1954 -- Index is the current index corresponding to the expression
1956 Resolution_OK : Boolean := True;
1957 -- Set to False if resolution of the expression failed
1959 begin
1960 -- Defend against previous errors
1962 if Nkind (Expr) = N_Error
1963 or else Error_Posted (Expr)
1964 then
1965 return True;
1966 end if;
1968 -- If the array type against which we are resolving the aggregate
1969 -- has several dimensions, the expressions nested inside the
1970 -- aggregate must be further aggregates (or strings).
1972 if Present (Nxt_Ind) then
1973 if Nkind (Expr) /= N_Aggregate then
1975 -- A string literal can appear where a one-dimensional array
1976 -- of characters is expected. If the literal looks like an
1977 -- operator, it is still an operator symbol, which will be
1978 -- transformed into a string when analyzed.
1980 if Is_Character_Type (Component_Typ)
1981 and then No (Next_Index (Nxt_Ind))
1982 and then Nkind (Expr) in N_String_Literal | N_Operator_Symbol
1983 then
1984 -- A string literal used in a multidimensional array
1985 -- aggregate in place of the final one-dimensional
1986 -- aggregate must not be enclosed in parentheses.
1988 if Paren_Count (Expr) /= 0 then
1989 Error_Msg_N ("no parenthesis allowed here", Expr);
1990 end if;
1992 Make_String_Into_Aggregate (Expr);
1994 else
1995 Error_Msg_N ("nested array aggregate expected", Expr);
1997 -- If the expression is parenthesized, this may be
1998 -- a missing component association for a 1-aggregate.
2000 if Paren_Count (Expr) > 0 then
2001 Error_Msg_N
2002 ("\if single-component aggregate is intended, "
2003 & "write e.g. (1 ='> ...)", Expr);
2004 end if;
2006 return Failure;
2007 end if;
2008 end if;
2010 -- If it's "... => <>", nothing to resolve
2012 if Nkind (Expr) = N_Component_Association then
2013 pragma Assert (Box_Present (Expr));
2014 return Success;
2015 end if;
2017 -- Ada 2005 (AI-231): Propagate the type to the nested aggregate.
2018 -- Required to check the null-exclusion attribute (if present).
2019 -- This value may be overridden later on.
2021 Set_Etype (Expr, Etype (N));
2023 Resolution_OK := Resolve_Array_Aggregate
2024 (Expr, Nxt_Ind, Nxt_Ind_Constr, Component_Typ, Others_Allowed);
2026 else
2027 -- If it's "... => <>", nothing to resolve
2029 if Nkind (Expr) = N_Component_Association then
2030 pragma Assert (Box_Present (Expr));
2031 return Success;
2032 end if;
2034 -- Do not resolve the expressions of discrete or others choices
2035 -- unless the expression covers a single component, or the
2036 -- expander is inactive.
2038 -- In SPARK mode, expressions that can perform side effects will
2039 -- be recognized by the gnat2why back-end, and the whole
2040 -- subprogram will be ignored. So semantic analysis can be
2041 -- performed safely.
2043 if Single_Elmt
2044 or else not Expander_Active
2045 or else In_Spec_Expression
2046 then
2047 Analyze_And_Resolve (Expr, Component_Typ);
2048 Check_Expr_OK_In_Limited_Aggregate (Expr);
2049 Check_Non_Static_Context (Expr);
2050 Aggregate_Constraint_Checks (Expr, Component_Typ);
2051 Check_Unset_Reference (Expr);
2052 end if;
2053 end if;
2055 -- If an aggregate component has a type with predicates, an explicit
2056 -- predicate check must be applied, as for an assignment statement,
2057 -- because the aggregate might not be expanded into individual
2058 -- component assignments. If the expression covers several components
2059 -- the analysis and the predicate check take place later.
2061 if Has_Predicates (Component_Typ)
2062 and then Analyzed (Expr)
2063 then
2064 Apply_Predicate_Check (Expr, Component_Typ);
2065 end if;
2067 if Raises_Constraint_Error (Expr)
2068 and then (Nkind (Parent (Expr)) /= N_Component_Association
2069 or else Is_Null_Aggregate (Expr))
2070 then
2071 Set_Raises_Constraint_Error (N);
2072 end if;
2074 -- If the expression has been marked as requiring a range check,
2075 -- then generate it here. It's a bit odd to be generating such
2076 -- checks in the analyzer, but harmless since Generate_Range_Check
2077 -- does nothing (other than making sure Do_Range_Check is set) if
2078 -- the expander is not active.
2080 if Do_Range_Check (Expr) then
2081 Generate_Range_Check (Expr, Component_Typ, CE_Range_Check_Failed);
2082 end if;
2084 return Resolution_OK;
2085 end Resolve_Aggr_Expr;
2087 --------------------------------------------
2088 -- Resolve_Iterated_Component_Association --
2089 --------------------------------------------
2091 procedure Resolve_Iterated_Component_Association
2092 (N : Node_Id;
2093 Index_Typ : Entity_Id)
2095 Loc : constant Source_Ptr := Sloc (N);
2096 Id : constant Entity_Id := Defining_Identifier (N);
2098 -----------------------
2099 -- Remove_References --
2100 -----------------------
2102 function Remove_Reference (N : Node_Id) return Traverse_Result;
2103 -- Remove reference to the entity Id after analysis, so it can be
2104 -- properly reanalyzed after construct is expanded into a loop.
2106 function Remove_Reference (N : Node_Id) return Traverse_Result is
2107 begin
2108 if Nkind (N) = N_Identifier
2109 and then Present (Entity (N))
2110 and then Entity (N) = Id
2111 then
2112 Set_Entity (N, Empty);
2113 Set_Etype (N, Empty);
2114 end if;
2115 Set_Analyzed (N, False);
2116 return OK;
2117 end Remove_Reference;
2119 procedure Remove_References is new Traverse_Proc (Remove_Reference);
2121 -- Local variables
2123 Choice : Node_Id;
2124 Dummy : Boolean;
2125 Scop : Entity_Id;
2126 Expr : constant Node_Id := Expression (N);
2128 -- Start of processing for Resolve_Iterated_Component_Association
2130 begin
2131 Error_Msg_Ada_2022_Feature ("iterated component", Loc);
2133 -- Create a scope in which to introduce an index, to make it visible
2134 -- for the analysis of component expression.
2136 Scop := New_Internal_Entity (E_Loop, Current_Scope, Loc, 'L');
2137 Set_Etype (Scop, Standard_Void_Type);
2138 Set_Parent (Scop, Parent (N));
2139 Push_Scope (Scop);
2141 -- If there is iterator specification, then its preanalysis will make
2142 -- the index visible.
2144 if Present (Iterator_Specification (N)) then
2145 Preanalyze (Iterator_Specification (N));
2147 -- Otherwise, analyze discrete choices and make the index visible
2149 else
2150 -- Insert index name into current scope but don't decorate it yet,
2151 -- so that a premature usage of this name in discrete choices will
2152 -- be nicely diagnosed.
2154 Enter_Name (Id);
2156 Choice := First (Discrete_Choices (N));
2158 while Present (Choice) loop
2159 if Nkind (Choice) = N_Others_Choice then
2160 Others_N := Choice;
2162 else
2163 Analyze (Choice);
2165 -- Choice can be a subtype name, a range, or an expression
2167 if Is_Entity_Name (Choice)
2168 and then Is_Type (Entity (Choice))
2169 and then
2170 Base_Type (Entity (Choice)) = Base_Type (Index_Typ)
2171 then
2172 null;
2174 else
2175 Analyze_And_Resolve (Choice, Index_Typ);
2176 end if;
2177 end if;
2179 Next (Choice);
2180 end loop;
2182 -- Decorate the index variable
2184 Set_Etype (Id, Index_Typ);
2185 Mutate_Ekind (Id, E_Variable);
2186 Set_Is_Not_Self_Hidden (Id);
2187 Set_Scope (Id, Scop);
2188 end if;
2190 -- Analyze expression without expansion, to verify legality.
2191 -- When generating code, we then remove references to the index
2192 -- variable, because the expression will be analyzed anew after
2193 -- rewritting as a loop with a new index variable; when not
2194 -- generating code we leave the analyzed expression as it is.
2196 Dummy := Resolve_Aggr_Expr (Expr, Single_Elmt => False);
2198 if Operating_Mode /= Check_Semantics then
2199 Remove_References (Expr);
2200 end if;
2202 -- An iterated_component_association may appear in a nested
2203 -- aggregate for a multidimensional structure: preserve the bounds
2204 -- computed for the expression, as well as the anonymous array
2205 -- type generated for it; both are needed during array expansion.
2207 if Nkind (Expr) = N_Aggregate then
2208 Set_Aggregate_Bounds (Expression (N), Aggregate_Bounds (Expr));
2209 Set_Etype (Expression (N), Etype (Expr));
2210 end if;
2212 End_Scope;
2213 end Resolve_Iterated_Component_Association;
2215 --------------
2216 -- Subtract --
2217 --------------
2219 function Subtract (Val : Uint; To : Node_Id) return Node_Id is
2220 Expr_Pos : Node_Id;
2221 Expr : Node_Id;
2222 To_Pos : Node_Id;
2224 begin
2225 if Raises_Constraint_Error (To) then
2226 return To;
2227 end if;
2229 -- First test if we can do constant folding
2231 if Compile_Time_Known_Value (To)
2232 or else Nkind (To) = N_Integer_Literal
2233 then
2234 Expr_Pos := Make_Integer_Literal (Loc, Expr_Value (To) - Val);
2235 Set_Is_Static_Expression (Expr_Pos);
2236 Set_Etype (Expr_Pos, Etype (To));
2237 Set_Analyzed (Expr_Pos, Analyzed (To));
2239 if not Is_Enumeration_Type (Index_Typ) then
2240 Expr := Expr_Pos;
2242 -- If we are dealing with enumeration return
2243 -- Index_Typ'Val (Expr_Pos)
2245 else
2246 Expr :=
2247 Make_Attribute_Reference
2248 (Loc,
2249 Prefix => New_Occurrence_Of (Index_Typ, Loc),
2250 Attribute_Name => Name_Val,
2251 Expressions => New_List (Expr_Pos));
2252 end if;
2254 return Expr;
2255 end if;
2257 -- If we are here no constant folding possible
2259 if not Is_Enumeration_Type (Index_Base) then
2260 Expr :=
2261 Make_Op_Subtract (Loc,
2262 Left_Opnd => Duplicate_Subexpr (To),
2263 Right_Opnd => Make_Integer_Literal (Loc, Val));
2265 -- If we are dealing with enumeration return
2266 -- Index_Typ'Val (Index_Typ'Pos (To) - Val)
2268 else
2269 To_Pos :=
2270 Make_Attribute_Reference
2271 (Loc,
2272 Prefix => New_Occurrence_Of (Index_Typ, Loc),
2273 Attribute_Name => Name_Pos,
2274 Expressions => New_List (Duplicate_Subexpr (To)));
2276 Expr_Pos :=
2277 Make_Op_Subtract (Loc,
2278 Left_Opnd => To_Pos,
2279 Right_Opnd => Make_Integer_Literal (Loc, Val));
2281 Expr :=
2282 Make_Attribute_Reference
2283 (Loc,
2284 Prefix => New_Occurrence_Of (Index_Typ, Loc),
2285 Attribute_Name => Name_Val,
2286 Expressions => New_List (Expr_Pos));
2288 -- If the index type has a non standard representation, the
2289 -- attributes 'Val and 'Pos expand into function calls and the
2290 -- resulting expression is considered non-safe for reevaluation
2291 -- by the backend. Relocate it into a constant temporary in order
2292 -- to make it safe for reevaluation.
2294 if Has_Non_Standard_Rep (Etype (N)) then
2295 declare
2296 Def_Id : Entity_Id;
2298 begin
2299 Def_Id := Make_Temporary (Loc, 'R', Expr);
2300 Set_Etype (Def_Id, Index_Typ);
2301 Insert_Action (N,
2302 Make_Object_Declaration (Loc,
2303 Defining_Identifier => Def_Id,
2304 Object_Definition =>
2305 New_Occurrence_Of (Index_Typ, Loc),
2306 Constant_Present => True,
2307 Expression => Relocate_Node (Expr)));
2309 Expr := New_Occurrence_Of (Def_Id, Loc);
2310 end;
2311 end if;
2312 end if;
2314 return Expr;
2315 end Subtract;
2317 ----------------------------------------
2318 -- Warn_On_Null_Component_Association --
2319 ----------------------------------------
2321 procedure Warn_On_Null_Component_Association (Expr : Node_Id) is
2322 Comp_Typ : constant Entity_Id := Component_Type (Etype (N));
2324 procedure Check_Case_Expr (N : Node_Id);
2325 -- Check if a case expression may initialize some component with a
2326 -- null value.
2328 procedure Check_Cond_Expr (N : Node_Id);
2329 -- Check if a conditional expression may initialize some component
2330 -- with a null value.
2332 procedure Check_Expr (Expr : Node_Id);
2333 -- Check if an expression may initialize some component with a
2334 -- null value.
2336 procedure Warn_On_Null_Expression_And_Rewrite (Null_Expr : Node_Id);
2337 -- Report warning on known null expression and replace the expression
2338 -- by a raise constraint error node.
2340 ---------------------
2341 -- Check_Case_Expr --
2342 ---------------------
2344 procedure Check_Case_Expr (N : Node_Id) is
2345 Alt_Node : Node_Id := First (Alternatives (N));
2347 begin
2348 while Present (Alt_Node) loop
2349 Check_Expr (Expression (Alt_Node));
2350 Next (Alt_Node);
2351 end loop;
2352 end Check_Case_Expr;
2354 ---------------------
2355 -- Check_Cond_Expr --
2356 ---------------------
2358 procedure Check_Cond_Expr (N : Node_Id) is
2359 If_Expr : Node_Id := N;
2360 Then_Expr : Node_Id;
2361 Else_Expr : Node_Id;
2363 begin
2364 Then_Expr := Next (First (Expressions (If_Expr)));
2365 Else_Expr := Next (Then_Expr);
2367 Check_Expr (Then_Expr);
2369 -- Process elsif parts (if any)
2371 while Nkind (Else_Expr) = N_If_Expression loop
2372 If_Expr := Else_Expr;
2373 Then_Expr := Next (First (Expressions (If_Expr)));
2374 Else_Expr := Next (Then_Expr);
2376 Check_Expr (Then_Expr);
2377 end loop;
2379 if Known_Null (Else_Expr) then
2380 Warn_On_Null_Expression_And_Rewrite (Else_Expr);
2381 end if;
2382 end Check_Cond_Expr;
2384 ----------------
2385 -- Check_Expr --
2386 ----------------
2388 procedure Check_Expr (Expr : Node_Id) is
2389 begin
2390 if Known_Null (Expr) then
2391 Warn_On_Null_Expression_And_Rewrite (Expr);
2393 elsif Nkind (Expr) = N_If_Expression then
2394 Check_Cond_Expr (Expr);
2396 elsif Nkind (Expr) = N_Case_Expression then
2397 Check_Case_Expr (Expr);
2398 end if;
2399 end Check_Expr;
2401 -----------------------------------------
2402 -- Warn_On_Null_Expression_And_Rewrite --
2403 -----------------------------------------
2405 procedure Warn_On_Null_Expression_And_Rewrite (Null_Expr : Node_Id) is
2406 begin
2407 Error_Msg_N
2408 ("(Ada 2005) NULL not allowed in null-excluding component??",
2409 Null_Expr);
2410 Error_Msg_N
2411 ("\Constraint_Error might be raised at run time??", Null_Expr);
2413 -- We cannot use Apply_Compile_Time_Constraint_Error because in
2414 -- some cases the components are rewritten and the runtime error
2415 -- would be missed.
2417 Rewrite (Null_Expr,
2418 Make_Raise_Constraint_Error (Sloc (Null_Expr),
2419 Reason => CE_Access_Check_Failed));
2421 Set_Etype (Null_Expr, Comp_Typ);
2422 Set_Analyzed (Null_Expr);
2423 end Warn_On_Null_Expression_And_Rewrite;
2425 -- Start of processing for Warn_On_Null_Component_Association
2427 begin
2428 pragma Assert (Can_Never_Be_Null (Comp_Typ));
2430 case Nkind (Expr) is
2431 when N_If_Expression =>
2432 Check_Cond_Expr (Expr);
2434 when N_Case_Expression =>
2435 Check_Case_Expr (Expr);
2437 when others =>
2438 pragma Assert (False);
2439 null;
2440 end case;
2441 end Warn_On_Null_Component_Association;
2443 -- Local variables
2445 Assoc : Node_Id;
2446 Choice : Node_Id;
2447 Expr : Node_Id;
2448 Discard : Node_Id;
2450 Aggr_Low : Node_Id := Empty;
2451 Aggr_High : Node_Id := Empty;
2452 -- The actual low and high bounds of this sub-aggregate
2454 Case_Table_Size : Nat;
2455 -- Contains the size of the case table needed to sort aggregate choices
2457 Choices_Low : Node_Id := Empty;
2458 Choices_High : Node_Id := Empty;
2459 -- The lowest and highest discrete choices values for a named aggregate
2461 Delete_Choice : Boolean;
2462 -- Used when replacing a subtype choice with predicate by a list
2464 Has_Iterator_Specifications : Boolean := False;
2465 -- Flag to indicate that all named associations are iterated component
2466 -- associations with iterator specifications, in which case the
2467 -- expansion will create two loops: one to evaluate the size and one
2468 -- to generate the elements (4.3.3 (20.2/5)).
2470 Nb_Elements : Uint := Uint_0;
2471 -- The number of elements in a positional aggregate
2473 Nb_Discrete_Choices : Nat := 0;
2474 -- The overall number of discrete choices (not counting others choice)
2476 -- Start of processing for Resolve_Array_Aggregate
2478 begin
2479 -- Ignore junk empty aggregate resulting from parser error
2481 if No (Expressions (N))
2482 and then No (Component_Associations (N))
2483 and then not Null_Record_Present (N)
2484 then
2485 return Failure;
2486 end if;
2488 -- Disable the warning for GNAT Mode to allow for easier transition.
2490 -- We don't warn about obsolescent usage of parentheses in generic
2491 -- instances for two reasons:
2493 -- 1. An equivalent warning has been emitted in the corresponding
2494 -- definition.
2495 -- 2. In cases where a generic definition specifies a version older than
2496 -- Ada 2022 through a pragma and rightfully uses parentheses for
2497 -- an array aggregate, an incorrect warning would be raised in
2498 -- instances of that generic that are in Ada 2022 or later if we
2499 -- didn't filter out the instance case.
2501 if Ada_Version_Explicit >= Ada_2022
2502 and then Warn_On_Obsolescent_Feature
2503 and then not GNAT_Mode
2504 and then not Is_Homogeneous_Aggregate (N)
2505 and then Is_Parenthesis_Aggregate (N)
2506 and then Nkind (Parent (N)) /= N_Qualified_Expression
2507 and then Comes_From_Source (N)
2508 and then not In_Instance
2509 then
2510 Error_Msg_N
2511 ("?j?array aggregate using () is an" &
2512 " obsolescent syntax, use '['] instead", N);
2513 end if;
2515 -- STEP 1: make sure the aggregate is correctly formatted
2517 if Is_Null_Aggregate (N) then
2518 null;
2520 elsif Present (Component_Associations (N)) then
2522 -- Verify that all or none of the component associations
2523 -- include an iterator specification.
2525 Assoc := First (Component_Associations (N));
2526 if Nkind (Assoc) = N_Iterated_Component_Association
2527 and then Present (Iterator_Specification (Assoc))
2528 then
2529 -- All other component associations must have an iterator spec.
2531 Next (Assoc);
2532 while Present (Assoc) loop
2533 if Nkind (Assoc) /= N_Iterated_Component_Association
2534 or else No (Iterator_Specification (Assoc))
2535 then
2536 Error_Msg_N ("mixed iterated component association"
2537 & " (RM 4.3.3 (17.1/5))",
2538 Assoc);
2539 return Failure;
2540 end if;
2542 Next (Assoc);
2543 end loop;
2545 Has_Iterator_Specifications := True;
2547 else
2548 -- or none of them do.
2550 Next (Assoc);
2551 while Present (Assoc) loop
2552 if Nkind (Assoc) = N_Iterated_Component_Association
2553 and then Present (Iterator_Specification (Assoc))
2554 then
2555 Error_Msg_N ("mixed iterated component association"
2556 & " (RM 4.3.3 (17.1/5))",
2557 Assoc);
2558 return Failure;
2559 end if;
2561 Next (Assoc);
2562 end loop;
2564 end if;
2566 Assoc := First (Component_Associations (N));
2567 while Present (Assoc) loop
2568 if Nkind (Assoc) = N_Iterated_Component_Association then
2569 Resolve_Iterated_Component_Association (Assoc, Index_Typ);
2571 elsif Nkind (Assoc) /= N_Component_Association then
2572 Error_Msg_N
2573 ("invalid component association for aggregate", Assoc);
2574 return Failure;
2575 end if;
2577 Choice := First (Choice_List (Assoc));
2578 Delete_Choice := False;
2579 while Present (Choice) loop
2580 if Nkind (Choice) = N_Others_Choice then
2581 Others_N := Choice;
2583 if Choice /= First (Choice_List (Assoc))
2584 or else Present (Next (Choice))
2585 then
2586 Error_Msg_N
2587 ("OTHERS must appear alone in a choice list", Choice);
2588 return Failure;
2589 end if;
2591 if Present (Next (Assoc)) then
2592 Error_Msg_N
2593 ("OTHERS must appear last in an aggregate", Choice);
2594 return Failure;
2595 end if;
2597 if Ada_Version = Ada_83
2598 and then Assoc /= First (Component_Associations (N))
2599 and then Nkind (Parent (N)) in
2600 N_Assignment_Statement | N_Object_Declaration
2601 then
2602 Error_Msg_N
2603 ("(Ada 83) illegal context for OTHERS choice", N);
2604 end if;
2606 elsif Is_Entity_Name (Choice) then
2607 Analyze (Choice);
2609 declare
2610 E : constant Entity_Id := Entity (Choice);
2611 New_Cs : List_Id;
2612 P : Node_Id;
2613 C : Node_Id;
2615 begin
2616 if Is_Type (E) and then Has_Predicates (E) then
2617 Freeze_Before (N, E);
2619 if Has_Dynamic_Predicate_Aspect (E)
2620 or else Has_Ghost_Predicate_Aspect (E)
2621 then
2622 Error_Msg_NE
2623 ("subtype& has non-static predicate, not allowed "
2624 & "in aggregate choice", Choice, E);
2626 elsif not Is_OK_Static_Subtype (E) then
2627 Error_Msg_NE
2628 ("non-static subtype& has predicate, not allowed "
2629 & "in aggregate choice", Choice, E);
2630 end if;
2632 -- If the subtype has a static predicate, replace the
2633 -- original choice with the list of individual values
2634 -- covered by the predicate.
2635 -- This should be deferred to expansion time ???
2637 if Present (Static_Discrete_Predicate (E)) then
2638 Delete_Choice := True;
2640 New_Cs := New_List;
2641 P := First (Static_Discrete_Predicate (E));
2642 while Present (P) loop
2643 C := New_Copy (P);
2644 Set_Sloc (C, Sloc (Choice));
2645 Append_To (New_Cs, C);
2646 Next (P);
2647 end loop;
2649 Insert_List_After (Choice, New_Cs);
2650 end if;
2651 end if;
2652 end;
2653 end if;
2655 Nb_Choices := Nb_Choices + 1;
2657 declare
2658 C : constant Node_Id := Choice;
2660 begin
2661 Next (Choice);
2663 if Delete_Choice then
2664 Remove (C);
2665 Nb_Choices := Nb_Choices - 1;
2666 Delete_Choice := False;
2667 end if;
2668 end;
2669 end loop;
2671 Next (Assoc);
2672 end loop;
2673 end if;
2675 -- At this point we know that the others choice, if present, is by
2676 -- itself and appears last in the aggregate. Check if we have mixed
2677 -- positional and discrete associations (other than the others choice).
2679 if Present (Expressions (N))
2680 and then (Nb_Choices > 1
2681 or else (Nb_Choices = 1 and then No (Others_N)))
2682 then
2683 Error_Msg_N
2684 ("cannot mix named and positional associations in array aggregate",
2685 First (Choice_List (First (Component_Associations (N)))));
2686 return Failure;
2687 end if;
2689 -- Test for the validity of an others choice if present
2691 if Present (Others_N) and then not Others_Allowed then
2692 Error_Msg_N ("OTHERS choice not allowed here", Others_N);
2693 Error_Msg_N ("\qualify the aggregate with a constrained subtype "
2694 & "to provide bounds for it", Others_N);
2695 return Failure;
2696 end if;
2698 -- Protect against cascaded errors
2700 if Etype (Index_Typ) = Any_Type then
2701 return Failure;
2702 end if;
2704 -- STEP 2: Process named components
2706 if No (Expressions (N)) then
2707 if Present (Others_N) then
2708 Case_Table_Size := Nb_Choices - 1;
2709 else
2710 Case_Table_Size := Nb_Choices;
2711 end if;
2713 Step_2 : declare
2714 function Empty_Range (A : Node_Id) return Boolean;
2715 -- If an association covers an empty range, some warnings on the
2716 -- expression of the association can be disabled.
2718 -----------------
2719 -- Empty_Range --
2720 -----------------
2722 function Empty_Range (A : Node_Id) return Boolean is
2723 R : Node_Id;
2725 begin
2726 if Nkind (A) = N_Iterated_Component_Association then
2727 R := First (Discrete_Choices (A));
2728 else
2729 R := First (Choices (A));
2730 end if;
2732 return No (Next (R))
2733 and then Nkind (R) = N_Range
2734 and then Compile_Time_Compare
2735 (Low_Bound (R), High_Bound (R), False) = GT;
2736 end Empty_Range;
2738 -- Local variables
2740 Low : Node_Id;
2741 High : Node_Id;
2742 -- Denote the lowest and highest values in an aggregate choice
2744 S_Low : Node_Id := Empty;
2745 S_High : Node_Id := Empty;
2746 -- if a choice in an aggregate is a subtype indication these
2747 -- denote the lowest and highest values of the subtype
2749 Table : Case_Table_Type (1 .. Case_Table_Size);
2750 -- Used to sort all the different choice values
2752 Single_Choice : Boolean;
2753 -- Set to true every time there is a single discrete choice in a
2754 -- discrete association
2756 Prev_Nb_Discrete_Choices : Nat;
2757 -- Used to keep track of the number of discrete choices in the
2758 -- current association.
2760 Errors_Posted_On_Choices : Boolean := False;
2761 -- Keeps track of whether any choices have semantic errors
2763 -- Start of processing for Step_2
2765 begin
2766 -- STEP 2 (A): Check discrete choices validity
2767 -- No need if this is an element iteration.
2769 Assoc := First (Component_Associations (N));
2770 while Present (Assoc)
2771 and then Present (Choice_List (Assoc))
2772 loop
2773 Prev_Nb_Discrete_Choices := Nb_Discrete_Choices;
2774 Choice := First (Choice_List (Assoc));
2776 loop
2777 Analyze (Choice);
2779 if Nkind (Choice) = N_Others_Choice then
2780 Single_Choice := False;
2781 exit;
2783 -- Test for subtype mark without constraint
2785 elsif Is_Entity_Name (Choice) and then
2786 Is_Type (Entity (Choice))
2787 then
2788 if Base_Type (Entity (Choice)) /= Index_Base then
2789 Error_Msg_N
2790 ("invalid subtype mark in aggregate choice",
2791 Choice);
2792 return Failure;
2793 end if;
2795 -- Case of subtype indication
2797 elsif Nkind (Choice) = N_Subtype_Indication then
2798 Resolve_Discrete_Subtype_Indication (Choice, Index_Base);
2800 if Has_Dynamic_Predicate_Aspect
2801 (Entity (Subtype_Mark (Choice)))
2802 or else Has_Ghost_Predicate_Aspect
2803 (Entity (Subtype_Mark (Choice)))
2804 then
2805 Error_Msg_NE
2806 ("subtype& has non-static predicate, "
2807 & "not allowed in aggregate choice",
2808 Choice, Entity (Subtype_Mark (Choice)));
2809 end if;
2811 -- Does the subtype indication evaluation raise CE?
2813 Get_Index_Bounds (Subtype_Mark (Choice), S_Low, S_High);
2814 Get_Index_Bounds (Choice, Low, High);
2815 Check_Bounds (S_Low, S_High, Low, High);
2817 -- Case of range or expression
2819 else
2820 Resolve (Choice, Index_Base);
2821 Check_Unset_Reference (Choice);
2822 Check_Non_Static_Context (Choice);
2824 -- If semantic errors were posted on the choice, then
2825 -- record that for possible early return from later
2826 -- processing (see handling of enumeration choices).
2828 if Error_Posted (Choice) then
2829 Errors_Posted_On_Choices := True;
2830 end if;
2832 -- Do not range check a choice. This check is redundant
2833 -- since this test is already done when we check that the
2834 -- bounds of the array aggregate are within range.
2836 Set_Do_Range_Check (Choice, False);
2837 end if;
2839 -- If we could not resolve the discrete choice stop here
2841 if Etype (Choice) = Any_Type then
2842 return Failure;
2844 -- If the discrete choice raises CE get its original bounds
2846 elsif Nkind (Choice) = N_Raise_Constraint_Error then
2847 Set_Raises_Constraint_Error (N);
2848 Get_Index_Bounds (Original_Node (Choice), Low, High);
2850 -- Otherwise get its bounds as usual
2852 else
2853 Get_Index_Bounds (Choice, Low, High);
2854 end if;
2856 if (Dynamic_Or_Null_Range (Low, High)
2857 or else (Nkind (Choice) = N_Subtype_Indication
2858 and then
2859 Dynamic_Or_Null_Range (S_Low, S_High)))
2860 and then Nb_Choices /= 1
2861 then
2862 Error_Msg_N
2863 ("dynamic or empty choice in aggregate "
2864 & "must be the only choice", Choice);
2865 return Failure;
2866 end if;
2868 if not (All_Composite_Constraints_Static (Low)
2869 and then All_Composite_Constraints_Static (High)
2870 and then All_Composite_Constraints_Static (S_Low)
2871 and then All_Composite_Constraints_Static (S_High))
2872 then
2873 Check_Restriction (No_Dynamic_Sized_Objects, Choice);
2874 end if;
2876 Nb_Discrete_Choices := Nb_Discrete_Choices + 1;
2877 Table (Nb_Discrete_Choices).Lo := Low;
2878 Table (Nb_Discrete_Choices).Hi := High;
2879 Table (Nb_Discrete_Choices).Choice := Choice;
2881 Next (Choice);
2883 if No (Choice) then
2885 -- Check if we have a single discrete choice and whether
2886 -- this discrete choice specifies a single value.
2888 Single_Choice :=
2889 Nb_Discrete_Choices = Prev_Nb_Discrete_Choices + 1
2890 and then Low = High;
2892 exit;
2893 end if;
2894 end loop;
2896 -- Ada 2005 (AI-231)
2898 if Ada_Version >= Ada_2005
2899 and then not Empty_Range (Assoc)
2900 then
2901 if Known_Null (Expression (Assoc)) then
2902 Check_Can_Never_Be_Null (Etype (N), Expression (Assoc));
2904 -- Report warning on iterated component association that may
2905 -- initialize some component of an array of null-excluding
2906 -- access type components with a null value. For example:
2908 -- type AList is array (...) of not null access Integer;
2909 -- L : AList :=
2910 -- [for J in A'Range =>
2911 -- (if Func (J) = 0 then A(J)'Access else Null)];
2913 elsif Ada_Version >= Ada_2022
2914 and then Can_Never_Be_Null (Component_Type (Etype (N)))
2915 and then Nkind (Assoc) = N_Iterated_Component_Association
2916 and then Nkind (Expression (Assoc)) in N_If_Expression
2917 | N_Case_Expression
2918 then
2919 Warn_On_Null_Component_Association (Expression (Assoc));
2920 end if;
2921 end if;
2923 -- Ada 2005 (AI-287): In case of default initialized component
2924 -- we delay the resolution to the expansion phase.
2926 if Box_Present (Assoc) then
2928 -- Ada 2005 (AI-287): In case of default initialization of a
2929 -- component the expander will generate calls to the
2930 -- corresponding initialization subprogram. We need to call
2931 -- Resolve_Aggr_Expr to check the rules about
2932 -- dimensionality.
2934 if not Resolve_Aggr_Expr
2935 (Assoc, Single_Elmt => Single_Choice)
2936 then
2937 return Failure;
2938 end if;
2940 -- ??? Checks for dynamically tagged expressions below will
2941 -- be only applied to iterated_component_association after
2942 -- expansion; in particular, errors might not be reported when
2943 -- -gnatc switch is used.
2945 elsif Nkind (Assoc) = N_Iterated_Component_Association then
2946 null; -- handled above, in a loop context
2948 elsif not Resolve_Aggr_Expr
2949 (Expression (Assoc), Single_Elmt => Single_Choice)
2950 then
2951 return Failure;
2953 -- Check incorrect use of dynamically tagged expression
2955 -- We differentiate here two cases because the expression may
2956 -- not be decorated. For example, the analysis and resolution
2957 -- of the expression associated with the others choice will be
2958 -- done later with the full aggregate. In such case we
2959 -- duplicate the expression tree to analyze the copy and
2960 -- perform the required check.
2962 elsif No (Etype (Expression (Assoc))) then
2963 declare
2964 Save_Analysis : constant Boolean := Full_Analysis;
2965 Expr : constant Node_Id :=
2966 New_Copy_Tree (Expression (Assoc));
2968 begin
2969 Expander_Mode_Save_And_Set (False);
2970 Full_Analysis := False;
2972 -- Analyze the expression, making sure it is properly
2973 -- attached to the tree before we do the analysis.
2975 Set_Parent (Expr, Parent (Expression (Assoc)));
2976 Analyze (Expr);
2978 -- Compute its dimensions now, rather than at the end of
2979 -- resolution, because in the case of multidimensional
2980 -- aggregates subsequent expansion may lead to spurious
2981 -- errors.
2983 Check_Expression_Dimensions (Expr, Component_Typ);
2985 -- If the expression is a literal, propagate this info
2986 -- to the expression in the association, to enable some
2987 -- optimizations downstream.
2989 if Is_Entity_Name (Expr)
2990 and then Present (Entity (Expr))
2991 and then Ekind (Entity (Expr)) = E_Enumeration_Literal
2992 then
2993 Analyze_And_Resolve
2994 (Expression (Assoc), Component_Typ);
2995 end if;
2997 Full_Analysis := Save_Analysis;
2998 Expander_Mode_Restore;
3000 -- Skip tagged checking for mutably tagged CW equivalent
3001 -- types.
3003 if Is_Tagged_Type (Etype (Expr))
3004 and then Is_Class_Wide_Equivalent_Type
3005 (Component_Type (Etype (N)))
3006 then
3007 null;
3009 -- Otherwise perform the dynamic tag check
3011 elsif Is_Tagged_Type (Etype (Expr)) then
3012 Check_Dynamically_Tagged_Expression
3013 (Expr => Expr,
3014 Typ => Component_Type (Etype (N)),
3015 Related_Nod => N);
3016 end if;
3017 end;
3019 elsif Is_Tagged_Type (Etype (Expression (Assoc))) then
3020 Check_Dynamically_Tagged_Expression
3021 (Expr => Expression (Assoc),
3022 Typ => Component_Type (Etype (N)),
3023 Related_Nod => N);
3024 end if;
3026 -- Propagate the attribute Raises_CE when it was reported on a
3027 -- null aggregate. This will cause replacing the aggregate by a
3028 -- raise CE node; it is not done in other cases to avoid such
3029 -- replacement and report complementary warnings when the
3030 -- expression is resolved.
3032 if Present (Expression (Assoc))
3033 and then Has_Null_Aggregate_Raising_Constraint_Error
3034 (Expression (Assoc))
3035 then
3036 Set_Raises_Constraint_Error (N);
3037 end if;
3039 Next (Assoc);
3040 end loop;
3042 -- If aggregate contains more than one choice then these must be
3043 -- static. Check for duplicate and missing values.
3045 -- Note: there is duplicated code here wrt Check_Choice_Set in
3046 -- the body of Sem_Case, and it is possible we could just reuse
3047 -- that procedure. To be checked ???
3049 if Nb_Discrete_Choices > 1 then
3050 Check_Choices : declare
3051 Choice : Node_Id;
3052 -- Location of choice for messages
3054 Hi_Val : Uint;
3055 Lo_Val : Uint;
3056 -- High end of one range and Low end of the next. Should be
3057 -- contiguous if there is no hole in the list of values.
3059 Lo_Dup : Uint;
3060 Hi_Dup : Uint;
3061 -- End points of duplicated range
3063 Missing_Or_Duplicates : Boolean := False;
3064 -- Set True if missing or duplicate choices found
3066 procedure Output_Bad_Choices (Lo, Hi : Uint; C : Node_Id);
3067 -- Output continuation message with a representation of the
3068 -- bounds (just Lo if Lo = Hi, else Lo .. Hi). C is the
3069 -- choice node where the message is to be posted.
3071 ------------------------
3072 -- Output_Bad_Choices --
3073 ------------------------
3075 procedure Output_Bad_Choices (Lo, Hi : Uint; C : Node_Id) is
3076 begin
3077 -- Enumeration type case
3079 if Is_Enumeration_Type (Index_Typ) then
3080 Error_Msg_Name_1 :=
3081 Chars (Get_Enum_Lit_From_Pos (Index_Typ, Lo, Loc));
3082 Error_Msg_Name_2 :=
3083 Chars (Get_Enum_Lit_From_Pos (Index_Typ, Hi, Loc));
3085 if Lo = Hi then
3086 Error_Msg_N ("\\ %!", C);
3087 else
3088 Error_Msg_N ("\\ % .. %!", C);
3089 end if;
3091 -- Integer types case
3093 else
3094 Error_Msg_Uint_1 := Lo;
3095 Error_Msg_Uint_2 := Hi;
3097 if Lo = Hi then
3098 Error_Msg_N ("\\ ^!", C);
3099 else
3100 Error_Msg_N ("\\ ^ .. ^!", C);
3101 end if;
3102 end if;
3103 end Output_Bad_Choices;
3105 -- Start of processing for Check_Choices
3107 begin
3108 Sort_Case_Table (Table);
3110 -- First we do a quick linear loop to find out if we have
3111 -- any duplicates or missing entries (usually we have a
3112 -- legal aggregate, so this will get us out quickly).
3114 for J in 1 .. Nb_Discrete_Choices - 1 loop
3115 Hi_Val := Expr_Value (Table (J).Hi);
3116 Lo_Val := Expr_Value (Table (J + 1).Lo);
3118 if Lo_Val <= Hi_Val
3119 or else (Lo_Val > Hi_Val + 1
3120 and then No (Others_N))
3121 then
3122 Missing_Or_Duplicates := True;
3123 exit;
3124 end if;
3125 end loop;
3127 -- If we have missing or duplicate entries, first fill in
3128 -- the Highest entries to make life easier in the following
3129 -- loops to detect bad entries.
3131 if Missing_Or_Duplicates then
3132 Table (1).Highest := Expr_Value (Table (1).Hi);
3134 for J in 2 .. Nb_Discrete_Choices loop
3135 Table (J).Highest :=
3136 UI_Max
3137 (Table (J - 1).Highest, Expr_Value (Table (J).Hi));
3138 end loop;
3140 -- Loop through table entries to find duplicate indexes
3142 for J in 2 .. Nb_Discrete_Choices loop
3143 Lo_Val := Expr_Value (Table (J).Lo);
3144 Hi_Val := Expr_Value (Table (J).Hi);
3146 -- Case where we have duplicates (the lower bound of
3147 -- this choice is less than or equal to the highest
3148 -- high bound found so far).
3150 if Lo_Val <= Table (J - 1).Highest then
3152 -- We move backwards looking for duplicates. We can
3153 -- abandon this loop as soon as we reach a choice
3154 -- highest value that is less than Lo_Val.
3156 for K in reverse 1 .. J - 1 loop
3157 exit when Table (K).Highest < Lo_Val;
3159 -- Here we may have duplicates between entries
3160 -- for K and J. Get range of duplicates.
3162 Lo_Dup :=
3163 UI_Max (Lo_Val, Expr_Value (Table (K).Lo));
3164 Hi_Dup :=
3165 UI_Min (Hi_Val, Expr_Value (Table (K).Hi));
3167 -- Nothing to do if duplicate range is null
3169 if Lo_Dup > Hi_Dup then
3170 null;
3172 -- Otherwise place proper message
3174 else
3175 -- We place message on later choice, with a
3176 -- line reference to the earlier choice.
3178 if Sloc (Table (J).Choice) <
3179 Sloc (Table (K).Choice)
3180 then
3181 Choice := Table (K).Choice;
3182 Error_Msg_Sloc := Sloc (Table (J).Choice);
3183 else
3184 Choice := Table (J).Choice;
3185 Error_Msg_Sloc := Sloc (Table (K).Choice);
3186 end if;
3188 if Lo_Dup = Hi_Dup then
3189 Error_Msg_N
3190 ("index value in array aggregate "
3191 & "duplicates the one given#!", Choice);
3192 else
3193 Error_Msg_N
3194 ("index values in array aggregate "
3195 & "duplicate those given#!", Choice);
3196 end if;
3198 Output_Bad_Choices (Lo_Dup, Hi_Dup, Choice);
3199 end if;
3200 end loop;
3201 end if;
3202 end loop;
3204 -- Loop through entries in table to find missing indexes.
3205 -- Not needed if others, since missing impossible.
3207 if No (Others_N) then
3208 for J in 2 .. Nb_Discrete_Choices loop
3209 Lo_Val := Expr_Value (Table (J).Lo);
3210 Hi_Val := Table (J - 1).Highest;
3212 if Lo_Val > Hi_Val + 1 then
3214 declare
3215 Error_Node : Node_Id;
3217 begin
3218 -- If the choice is the bound of a range in
3219 -- a subtype indication, it is not in the
3220 -- source lists for the aggregate itself, so
3221 -- post the error on the aggregate. Otherwise
3222 -- post it on choice itself.
3224 Choice := Table (J).Choice;
3226 if Is_List_Member (Choice) then
3227 Error_Node := Choice;
3228 else
3229 Error_Node := N;
3230 end if;
3232 if Hi_Val + 1 = Lo_Val - 1 then
3233 Error_Msg_N
3234 ("missing index value "
3235 & "in array aggregate!", Error_Node);
3236 else
3237 Error_Msg_N
3238 ("missing index values "
3239 & "in array aggregate!", Error_Node);
3240 end if;
3242 Output_Bad_Choices
3243 (Hi_Val + 1, Lo_Val - 1, Error_Node);
3244 end;
3245 end if;
3246 end loop;
3247 end if;
3249 -- If either missing or duplicate values, return failure
3251 Set_Etype (N, Any_Composite);
3252 return Failure;
3253 end if;
3254 end Check_Choices;
3255 end if;
3257 if Has_Iterator_Specifications then
3258 -- Bounds will be determined dynamically.
3260 return Success;
3261 end if;
3263 -- STEP 2 (B): Compute aggregate bounds and min/max choices values
3265 if Nb_Discrete_Choices > 0 then
3266 Choices_Low := Table (1).Lo;
3267 Choices_High := Table (Nb_Discrete_Choices).Hi;
3268 end if;
3270 -- If Others is present, then bounds of aggregate come from the
3271 -- index constraint (not the choices in the aggregate itself).
3273 if Present (Others_N) then
3274 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
3276 -- Abandon processing if either bound is already signalled as
3277 -- an error (prevents junk cascaded messages and blow ups).
3279 if Nkind (Aggr_Low) = N_Error
3280 or else
3281 Nkind (Aggr_High) = N_Error
3282 then
3283 return False;
3284 end if;
3286 -- No others clause present
3288 else
3289 -- Special processing if others allowed and not present. In
3290 -- this case, the bounds of the aggregate come from the
3291 -- choices (RM 4.3.3 (27)).
3293 if Others_Allowed then
3294 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
3296 -- Abandon processing if either bound is already signalled
3297 -- as an error (stop junk cascaded messages and blow ups).
3299 if Nkind (Aggr_Low) = N_Error
3300 or else
3301 Nkind (Aggr_High) = N_Error
3302 then
3303 return False;
3304 end if;
3306 -- If there is an applicable index constraint and others is
3307 -- not present, then sliding is allowed and only a length
3308 -- check will be performed. However, additional warnings are
3309 -- useful if the index type is an enumeration type, as
3310 -- sliding is dubious in this case. We emit two kinds of
3311 -- warnings:
3313 -- 1. If the length is wrong then there are missing
3314 -- components; we issue appropriate warnings about
3315 -- these missing components. They are only warnings,
3316 -- since the aggregate is fine, it's just the wrong
3317 -- length. We skip this check for standard character
3318 -- types (since there are no literals and it is too
3319 -- much trouble to concoct them), and also if any of
3320 -- the bounds have values that are not known at compile
3321 -- time.
3323 -- 2. If the length is right but the bounds do not match,
3324 -- we issue a warning, as we consider sliding dubious
3325 -- when the index type is an enumeration type.
3327 if Nkind (Index) = N_Range
3328 and then Is_Enumeration_Type (Etype (Index))
3329 and then not Is_Standard_Character_Type (Etype (Index))
3330 and then Compile_Time_Known_Value (Aggr_Low)
3331 and then Compile_Time_Known_Value (Aggr_High)
3332 and then Compile_Time_Known_Value (Choices_Low)
3333 and then Compile_Time_Known_Value (Choices_High)
3334 then
3335 -- If any of the expressions or range bounds in choices
3336 -- have semantic errors, then do not attempt further
3337 -- resolution, to prevent cascaded errors.
3339 if Errors_Posted_On_Choices then
3340 return Failure;
3341 end if;
3343 declare
3344 ALo : constant Node_Id := Expr_Value_E (Aggr_Low);
3345 AHi : constant Node_Id := Expr_Value_E (Aggr_High);
3346 CLo : constant Node_Id := Expr_Value_E (Choices_Low);
3347 CHi : constant Node_Id := Expr_Value_E (Choices_High);
3349 Ent : Entity_Id;
3351 begin
3352 -- Warning case 1, missing values at start/end. Only
3353 -- do the check if the number of entries is too small.
3355 if (Enumeration_Pos (CHi) - Enumeration_Pos (CLo))
3357 (Enumeration_Pos (AHi) - Enumeration_Pos (ALo))
3358 then
3359 Error_Msg_N
3360 ("missing index value(s) in array aggregate??",
3363 -- Output missing value(s) at start
3365 if Chars (ALo) /= Chars (CLo) then
3366 Ent := Prev (CLo);
3368 if Chars (ALo) = Chars (Ent) then
3369 Error_Msg_Name_1 := Chars (ALo);
3370 Error_Msg_N ("\ %??", N);
3371 else
3372 Error_Msg_Name_1 := Chars (ALo);
3373 Error_Msg_Name_2 := Chars (Ent);
3374 Error_Msg_N ("\ % .. %??", N);
3375 end if;
3376 end if;
3378 -- Output missing value(s) at end
3380 if Chars (AHi) /= Chars (CHi) then
3381 Ent := Next (CHi);
3383 if Chars (AHi) = Chars (Ent) then
3384 Error_Msg_Name_1 := Chars (Ent);
3385 Error_Msg_N ("\ %??", N);
3386 else
3387 Error_Msg_Name_1 := Chars (Ent);
3388 Error_Msg_Name_2 := Chars (AHi);
3389 Error_Msg_N ("\ % .. %??", N);
3390 end if;
3391 end if;
3393 -- Warning case 2, dubious sliding. The First_Subtype
3394 -- test distinguishes between a constrained type where
3395 -- sliding is not allowed (so we will get a warning
3396 -- later that Constraint_Error will be raised), and
3397 -- the unconstrained case where sliding is permitted.
3399 elsif (Enumeration_Pos (CHi) - Enumeration_Pos (CLo))
3401 (Enumeration_Pos (AHi) - Enumeration_Pos (ALo))
3402 and then Chars (ALo) /= Chars (CLo)
3403 and then
3404 not Is_Constrained (First_Subtype (Etype (N)))
3405 then
3406 Error_Msg_N
3407 ("bounds of aggregate do not match target??", N);
3408 end if;
3409 end;
3410 end if;
3411 end if;
3413 -- If no others, aggregate bounds come from aggregate
3415 Aggr_Low := Choices_Low;
3416 Aggr_High := Choices_High;
3417 end if;
3418 end Step_2;
3420 -- STEP 3: Process positional components
3422 else
3423 -- STEP 3 (A): Process positional elements
3425 Expr := First (Expressions (N));
3426 Nb_Elements := Uint_0;
3427 while Present (Expr) loop
3428 Nb_Elements := Nb_Elements + 1;
3430 -- Ada 2005 (AI-231)
3432 if Ada_Version >= Ada_2005 and then Known_Null (Expr) then
3433 Check_Can_Never_Be_Null (Etype (N), Expr);
3434 end if;
3436 if not Resolve_Aggr_Expr (Expr, Single_Elmt => True) then
3437 return Failure;
3438 end if;
3440 -- Check incorrect use of dynamically tagged expression
3442 if Is_Tagged_Type (Etype (Expr)) then
3443 Check_Dynamically_Tagged_Expression
3444 (Expr => Expr,
3445 Typ => Component_Type (Etype (N)),
3446 Related_Nod => N);
3447 end if;
3449 Next (Expr);
3450 end loop;
3452 if Present (Others_N) then
3453 Assoc := Last (Component_Associations (N));
3455 -- Ada 2005 (AI-231)
3457 if Ada_Version >= Ada_2005 and then Known_Null (Assoc) then
3458 Check_Can_Never_Be_Null (Etype (N), Expression (Assoc));
3459 end if;
3461 -- Ada 2005 (AI-287): In case of default initialized component,
3462 -- we delay the resolution to the expansion phase.
3464 if Box_Present (Assoc) then
3466 -- Ada 2005 (AI-287): In case of default initialization of a
3467 -- component the expander will generate calls to the
3468 -- corresponding initialization subprogram. We need to call
3469 -- Resolve_Aggr_Expr to check the rules about
3470 -- dimensionality.
3472 if not Resolve_Aggr_Expr (Assoc, Single_Elmt => False) then
3473 return Failure;
3474 end if;
3476 elsif not Resolve_Aggr_Expr (Expression (Assoc),
3477 Single_Elmt => False)
3478 then
3479 return Failure;
3481 -- Check incorrect use of dynamically tagged expression. The
3482 -- expression of the others choice has not been resolved yet.
3483 -- In order to diagnose the semantic error we create a duplicate
3484 -- tree to analyze it and perform the check.
3486 elsif Nkind (Assoc) /= N_Iterated_Component_Association then
3487 declare
3488 Save_Analysis : constant Boolean := Full_Analysis;
3489 Expr : constant Node_Id :=
3490 New_Copy_Tree (Expression (Assoc));
3492 begin
3493 Expander_Mode_Save_And_Set (False);
3494 Full_Analysis := False;
3495 Analyze (Expr);
3496 Full_Analysis := Save_Analysis;
3497 Expander_Mode_Restore;
3499 if Is_Tagged_Type (Etype (Expr)) then
3500 Check_Dynamically_Tagged_Expression
3501 (Expr => Expr,
3502 Typ => Component_Type (Etype (N)),
3503 Related_Nod => N);
3504 end if;
3505 end;
3506 end if;
3507 end if;
3509 -- STEP 3 (B): Compute the aggregate bounds
3511 if Present (Others_N) then
3512 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
3514 else
3515 if Others_Allowed then
3516 Get_Index_Bounds (Index_Constr, Aggr_Low, Discard);
3517 else
3518 Aggr_Low := Index_Typ_Low;
3519 end if;
3521 -- Report a warning when the index type of a null array aggregate
3522 -- is a modular type or an enumeration type, and we know that
3523 -- we will not be able to compute its high bound at runtime
3524 -- (AI22-0100-2).
3526 if Nb_Elements = Uint_0
3527 and then Cannot_Compute_High_Bound (Index_Constr)
3528 then
3529 -- Use the low bound value for the high-bound value to avoid
3530 -- reporting spurious errors; this value will not be used at
3531 -- runtime because this aggregate will be replaced by a raise
3532 -- CE node.
3534 Aggr_High := Aggr_Low;
3536 Report_Null_Array_Constraint_Error (N, Index_Typ);
3537 Set_Raises_Constraint_Error (N);
3539 elsif Nb_Elements = Uint_0 then
3540 Aggr_High := Subtract (Uint_1, To => Aggr_Low);
3541 Check_Bound (Index_Base_High, Aggr_High);
3543 else
3544 Aggr_High := Add (Nb_Elements - 1, To => Aggr_Low);
3545 Check_Bound (Index_Base_High, Aggr_High);
3546 end if;
3547 end if;
3548 end if;
3550 -- STEP 4: Perform static aggregate checks and save the bounds
3552 -- Check (A)
3554 Check_Bounds (Index_Typ_Low, Index_Typ_High, Aggr_Low, Aggr_High);
3555 Check_Bounds (Index_Base_Low, Index_Base_High, Aggr_Low, Aggr_High);
3557 -- Check (B)
3559 if Present (Others_N) and then Nb_Discrete_Choices > 0 then
3560 Check_Bounds (Aggr_Low, Aggr_High, Choices_Low, Choices_High);
3561 Check_Bounds (Index_Typ_Low, Index_Typ_High,
3562 Choices_Low, Choices_High);
3563 Check_Bounds (Index_Base_Low, Index_Base_High,
3564 Choices_Low, Choices_High);
3566 -- Check (C)
3568 elsif Present (Others_N) and then Nb_Elements > 0 then
3569 Check_Length (Aggr_Low, Aggr_High, Nb_Elements);
3570 Check_Length (Index_Typ_Low, Index_Typ_High, Nb_Elements);
3571 Check_Length (Index_Base_Low, Index_Base_High, Nb_Elements);
3572 end if;
3574 if Raises_Constraint_Error (Aggr_Low)
3575 or else Raises_Constraint_Error (Aggr_High)
3576 then
3577 Set_Raises_Constraint_Error (N);
3578 end if;
3580 Aggr_Low := Duplicate_Subexpr (Aggr_Low);
3582 -- Do not duplicate Aggr_High if Aggr_High = Aggr_Low + Nb_Elements
3583 -- since the addition node returned by Add is not yet analyzed. Attach
3584 -- to tree and analyze first. Reset analyzed flag to ensure it will get
3585 -- analyzed when it is a literal bound whose type must be properly set.
3587 if Present (Others_N) or else Nb_Discrete_Choices > 0 then
3588 Aggr_High := Duplicate_Subexpr (Aggr_High);
3590 if Etype (Aggr_High) = Universal_Integer then
3591 Set_Analyzed (Aggr_High, False);
3592 end if;
3593 end if;
3595 -- If the aggregate already has bounds attached to it, it means this is
3596 -- a positional aggregate created as an optimization by
3597 -- Exp_Aggr.Convert_To_Positional, so we don't want to change those
3598 -- bounds.
3600 if Present (Aggregate_Bounds (N))
3601 and then not Others_Allowed
3602 and then not Comes_From_Source (N)
3603 then
3604 Aggr_Low := Low_Bound (Aggregate_Bounds (N));
3605 Aggr_High := High_Bound (Aggregate_Bounds (N));
3606 end if;
3608 Set_Aggregate_Bounds
3609 (N, Make_Range (Loc, Low_Bound => Aggr_Low, High_Bound => Aggr_High));
3611 -- The bounds may contain expressions that must be inserted upwards.
3612 -- Attach them fully to the tree. After analysis, remove side effects
3613 -- from upper bound, if still needed.
3615 Set_Parent (Aggregate_Bounds (N), N);
3616 Analyze_And_Resolve (Aggregate_Bounds (N), Index_Typ);
3617 Check_Unset_Reference (Aggregate_Bounds (N));
3619 if No (Others_N) and then Nb_Discrete_Choices = 0 then
3620 Set_High_Bound
3621 (Aggregate_Bounds (N),
3622 Duplicate_Subexpr (High_Bound (Aggregate_Bounds (N))));
3623 end if;
3625 -- Check the dimensions of each component in the array aggregate
3627 Analyze_Dimension_Array_Aggregate (N, Component_Typ);
3629 return Success;
3630 end Resolve_Array_Aggregate;
3632 ---------------------------------
3633 -- Resolve_Container_Aggregate --
3634 ---------------------------------
3636 procedure Resolve_Container_Aggregate (N : Node_Id; Typ : Entity_Id) is
3637 procedure Resolve_Iterated_Association
3638 (Comp : Node_Id;
3639 Key_Type : Entity_Id;
3640 Elmt_Type : Entity_Id);
3641 -- Resolve choices and expression in an iterated component association
3642 -- or an iterated element association, which has a key_expression.
3643 -- This is similar but not identical to the handling of this construct
3644 -- in an array aggregate.
3645 -- For a named container, the type of each choice must be compatible
3646 -- with the key type. For a positional container, the choice must be
3647 -- a subtype indication or an iterator specification that determines
3648 -- an element type.
3650 Asp : constant Node_Id := Find_Value_Of_Aspect (Typ, Aspect_Aggregate);
3652 Empty_Subp : Node_Id := Empty;
3653 Add_Named_Subp : Node_Id := Empty;
3654 Add_Unnamed_Subp : Node_Id := Empty;
3655 New_Indexed_Subp : Node_Id := Empty;
3656 Assign_Indexed_Subp : Node_Id := Empty;
3658 ----------------------------------
3659 -- Resolve_Iterated_Association --
3660 ----------------------------------
3662 procedure Resolve_Iterated_Association
3663 (Comp : Node_Id;
3664 Key_Type : Entity_Id;
3665 Elmt_Type : Entity_Id)
3667 Loc : constant Source_Ptr := Sloc (N);
3668 Choice : Node_Id;
3669 Copy : Node_Id;
3670 Ent : Entity_Id;
3671 Expr : Node_Id;
3672 Key_Expr : Node_Id := Empty;
3673 Id : Entity_Id;
3674 Id_Name : Name_Id;
3675 Typ : Entity_Id := Empty;
3676 Loop_Param_Id : Entity_Id := Empty;
3678 begin
3679 Error_Msg_Ada_2022_Feature ("iterated component", Loc);
3681 -- If this is an Iterated_Element_Association then either a
3682 -- an Iterator_Specification or a Loop_Parameter specification
3683 -- is present.
3685 if Nkind (Comp) = N_Iterated_Element_Association then
3687 -- Create a temporary scope to avoid some modifications from
3688 -- escaping the Analyze call below. The original Tree will be
3689 -- reanalyzed later.
3691 Ent := New_Internal_Entity
3692 (E_Loop, Current_Scope, Sloc (Comp), 'L');
3693 Set_Etype (Ent, Standard_Void_Type);
3694 Set_Parent (Ent, Parent (Comp));
3695 Push_Scope (Ent);
3697 if Present (Loop_Parameter_Specification (Comp)) then
3698 Copy := Copy_Separate_Tree (Comp);
3699 Set_Parent (Copy, Parent (Comp));
3701 Analyze
3702 (Loop_Parameter_Specification (Copy));
3704 if Present (Iterator_Specification (Copy)) then
3705 Loop_Param_Id :=
3706 Defining_Identifier (Iterator_Specification (Copy));
3707 else
3708 Loop_Param_Id :=
3709 Defining_Identifier (Loop_Parameter_Specification (Copy));
3710 end if;
3712 Id_Name := Chars (Loop_Param_Id);
3713 else
3714 Copy := Copy_Separate_Tree (Iterator_Specification (Comp));
3715 Analyze (Copy);
3717 Loop_Param_Id := Defining_Identifier (Copy);
3719 Id_Name := Chars (Loop_Param_Id);
3720 end if;
3722 -- Key expression must have the type of the key. We preanalyze
3723 -- a copy of the original expression, because it will be
3724 -- reanalyzed and copied as needed during expansion of the
3725 -- corresponding loop.
3727 Key_Expr := Key_Expression (Comp);
3728 if Present (Key_Expr) then
3729 if No (Add_Named_Subp) then
3730 Error_Msg_N
3731 ("iterated_element_association with key_expression only "
3732 & "allowed for container type with Add_Named operation "
3733 & "(RM22 4.3.5(24))",
3734 Comp);
3735 else
3736 Preanalyze_And_Resolve (New_Copy_Tree (Key_Expr), Key_Type);
3737 end if;
3738 end if;
3739 End_Scope;
3741 Typ := Etype (Loop_Param_Id);
3743 elsif Present (Iterator_Specification (Comp)) then
3744 -- Create a temporary scope to avoid some modifications from
3745 -- escaping the Analyze call below. The original Tree will be
3746 -- reanalyzed later.
3748 Ent := New_Internal_Entity
3749 (E_Loop, Current_Scope, Sloc (Comp), 'L');
3750 Set_Etype (Ent, Standard_Void_Type);
3751 Set_Parent (Ent, Parent (Comp));
3752 Push_Scope (Ent);
3754 Copy := Copy_Separate_Tree (Iterator_Specification (Comp));
3756 Loop_Param_Id :=
3757 Defining_Identifier (Iterator_Specification (Comp));
3759 Id_Name := Chars (Loop_Param_Id);
3761 Preanalyze (Copy);
3763 End_Scope;
3765 Typ := Etype (Defining_Identifier (Copy));
3767 else
3768 Choice := First (Discrete_Choices (Comp));
3770 -- A copy of Choice is made before it's analyzed, to preserve
3771 -- prefixed calls in their original form, because otherwise the
3772 -- analysis of Choice can transform such calls to normal form,
3773 -- and the later analysis of an iterator_specification created
3774 -- below in the case of a function-call choice may trigger an
3775 -- error on the call (in the case where the function is not
3776 -- directly visible).
3778 Copy := Copy_Separate_Tree (Choice);
3780 -- This is an N_Component_Association with a Defining_Identifier
3781 -- and Discrete_Choice_List, but the latter can only have a single
3782 -- choice, as it's a stand-in for a Loop_Parameter_Specification
3783 -- (or possibly even an Iterator_Specification, see below).
3785 pragma Assert (No (Next (Choice)));
3787 Analyze (Choice);
3789 -- Choice can be a subtype name, a range, or an expression
3791 if Is_Entity_Name (Choice)
3792 and then Is_Type (Entity (Choice))
3793 and then Base_Type (Entity (Choice)) = Base_Type (Key_Type)
3794 then
3795 null;
3797 elsif Nkind (Choice) = N_Function_Call then
3798 declare
3799 I_Spec : constant Node_Id :=
3800 Make_Iterator_Specification (Sloc (N),
3801 Defining_Identifier =>
3802 Relocate_Node (Defining_Identifier (Comp)),
3803 Name => Copy,
3804 Reverse_Present => False,
3805 Iterator_Filter => Empty,
3806 Subtype_Indication => Empty);
3807 begin
3808 Set_Iterator_Specification (Comp, I_Spec);
3809 Set_Defining_Identifier (Comp, Empty);
3811 Resolve_Iterated_Association (Comp, Key_Type, Elmt_Type);
3812 -- Recursive call to expand association as iterator_spec
3814 return;
3815 end;
3817 elsif Present (Key_Type) then
3818 Analyze_And_Resolve (Choice, Key_Type);
3819 Typ := Key_Type;
3821 else
3822 Typ := Etype (Choice); -- assume unique for now
3823 end if;
3825 Loop_Param_Id :=
3826 Defining_Identifier (Comp);
3828 Id_Name := Chars (Loop_Param_Id);
3829 end if;
3831 -- Create a scope in which to introduce an index, which is usually
3832 -- visible in the expression for the component, and needed for its
3833 -- analysis.
3835 Id := Make_Defining_Identifier (Sloc (Comp), Id_Name);
3836 Ent := New_Internal_Entity (E_Loop,
3837 Current_Scope, Sloc (Comp), 'L');
3838 Set_Etype (Ent, Standard_Void_Type);
3839 Set_Parent (Ent, Parent (Comp));
3840 Push_Scope (Ent);
3842 -- Insert and decorate the loop variable in the current scope.
3843 -- The expression has to be analyzed once the loop variable is
3844 -- directly visible. Mark the variable as referenced to prevent
3845 -- spurious warnings, given that subsequent uses of its name in the
3846 -- expression will reference the internal (synonym) loop variable.
3848 Enter_Name (Id);
3850 pragma Assert (Present (Typ));
3851 Set_Etype (Id, Typ);
3853 Mutate_Ekind (Id, E_Variable);
3854 Set_Is_Not_Self_Hidden (Id);
3855 Set_Scope (Id, Ent);
3856 Set_Referenced (Id);
3858 -- Check for violation of 4.3.5(27/5)
3860 if No (Key_Expr)
3861 and then Present (Key_Type)
3862 and then
3863 (Is_Indexed_Aggregate (N, Add_Unnamed_Subp, New_Indexed_Subp)
3864 or else Present (Add_Named_Subp))
3865 and then Base_Type (Key_Type) /= Base_Type (Typ)
3866 then
3867 Error_Msg_Node_2 := Key_Type;
3868 Error_Msg_NE
3869 ("loop parameter type & must be same as key type & " &
3870 "(RM22 4.3.5(27))", Loop_Param_Id, Typ);
3871 end if;
3873 -- Analyze a copy of the expression, to verify legality. We use
3874 -- a copy because the expression will be analyzed anew when the
3875 -- enclosing aggregate is expanded, and the construct is rewritten
3876 -- as a loop with a new index variable.
3878 Expr := New_Copy_Tree (Expression (Comp));
3879 Preanalyze_And_Resolve (Expr, Elmt_Type);
3880 End_Scope;
3882 end Resolve_Iterated_Association;
3884 -- Start of processing for Resolve_Container_Aggregate
3886 begin
3887 pragma Assert (Nkind (Asp) = N_Aggregate);
3889 Set_Etype (N, Typ);
3890 Parse_Aspect_Aggregate (Asp,
3891 Empty_Subp, Add_Named_Subp, Add_Unnamed_Subp,
3892 New_Indexed_Subp, Assign_Indexed_Subp);
3894 if Present (Add_Unnamed_Subp)
3895 and then No (New_Indexed_Subp)
3896 and then Present (Etype (Add_Unnamed_Subp))
3897 and then Etype (Add_Unnamed_Subp) /= Any_Type
3898 then
3899 declare
3900 Elmt_Type : constant Entity_Id :=
3901 Etype (Next_Formal
3902 (First_Formal (Entity (Add_Unnamed_Subp))));
3903 Comp : Node_Id;
3905 begin
3906 if Present (Expressions (N)) then
3907 -- positional aggregate
3909 Comp := First (Expressions (N));
3910 while Present (Comp) loop
3911 Analyze_And_Resolve (Comp, Elmt_Type);
3912 Next (Comp);
3913 end loop;
3914 end if;
3916 -- Empty aggregate, to be replaced by Empty during
3917 -- expansion, or iterated component association.
3919 if Present (Component_Associations (N)) then
3920 declare
3921 Comp : Node_Id := First (Component_Associations (N));
3922 begin
3923 while Present (Comp) loop
3924 if Nkind (Comp) in
3925 N_Iterated_Component_Association |
3926 N_Iterated_Element_Association
3927 then
3928 Resolve_Iterated_Association
3929 (Comp, Empty, Elmt_Type);
3930 else
3931 Error_Msg_N ("illegal component association "
3932 & "for unnamed container aggregate", Comp);
3933 return;
3934 end if;
3936 Next (Comp);
3937 end loop;
3938 end;
3939 end if;
3940 end;
3942 elsif Present (Add_Named_Subp)
3943 and then Etype (Add_Named_Subp) /= Any_Type
3944 then
3945 declare
3946 -- Retrieves types of container, key, and element from the
3947 -- specified insertion procedure.
3949 Container : constant Entity_Id :=
3950 First_Formal (Entity (Add_Named_Subp));
3951 Key_Type : constant Entity_Id := Etype (Next_Formal (Container));
3952 Elmt_Type : constant Entity_Id :=
3953 Etype (Next_Formal (Next_Formal (Container)));
3955 Comp_Assocs : constant List_Id := Component_Associations (N);
3956 Comp : Node_Id;
3957 Choice : Node_Id;
3959 begin
3960 -- In the Add_Named case, the aggregate must consist of named
3961 -- associations (Add_Unnnamed is not allowed), so we issue an
3962 -- error if there are positional associations.
3964 if No (Comp_Assocs)
3965 and then Present (Expressions (N))
3966 then
3967 Error_Msg_N ("container aggregate must be "
3968 & "named, not positional", N);
3969 return;
3970 end if;
3972 Comp := First (Comp_Assocs);
3973 while Present (Comp) loop
3974 if Nkind (Comp) = N_Component_Association then
3975 Choice := First (Choices (Comp));
3977 while Present (Choice) loop
3978 Analyze_And_Resolve (Choice, Key_Type);
3979 Next (Choice);
3980 end loop;
3982 Analyze_And_Resolve (Expression (Comp), Elmt_Type);
3984 elsif Nkind (Comp) in
3985 N_Iterated_Component_Association |
3986 N_Iterated_Element_Association
3987 then
3988 Resolve_Iterated_Association
3989 (Comp, Key_Type, Elmt_Type);
3990 end if;
3992 Next (Comp);
3993 end loop;
3994 end;
3996 elsif Present (Assign_Indexed_Subp)
3997 and then Etype (Assign_Indexed_Subp) /= Any_Type
3998 then
3999 -- Indexed Aggregate. Positional or indexed component
4000 -- can be present, but not both. Choices must be static
4001 -- values or ranges with static bounds.
4003 declare
4004 Container : constant Entity_Id :=
4005 First_Formal (Entity (Assign_Indexed_Subp));
4006 Index_Type : constant Entity_Id := Etype (Next_Formal (Container));
4007 Comp_Type : constant Entity_Id :=
4008 Etype (Next_Formal (Next_Formal (Container)));
4009 Comp : Node_Id;
4010 Choice : Node_Id;
4011 Num_Choices : Nat := 0;
4013 Hi_Val : Uint;
4014 Lo_Val : Uint;
4015 begin
4016 if Present (Expressions (N)) then
4017 Comp := First (Expressions (N));
4018 while Present (Comp) loop
4019 Analyze_And_Resolve (Comp, Comp_Type);
4020 Next (Comp);
4021 end loop;
4022 end if;
4024 if Present (Component_Associations (N))
4025 and then not Is_Empty_List (Component_Associations (N))
4026 then
4027 if Present (Expressions (N))
4028 and then not Is_Empty_List (Expressions (N))
4029 then
4030 Error_Msg_N ("container aggregate cannot be "
4031 & "both positional and named", N);
4032 return;
4033 end if;
4035 Comp := First (Component_Associations (N));
4037 while Present (Comp) loop
4038 if Nkind (Comp) = N_Component_Association then
4039 Choice := First (Choices (Comp));
4041 while Present (Choice) loop
4042 Analyze_And_Resolve (Choice, Index_Type);
4043 Num_Choices := Num_Choices + 1;
4044 Next (Choice);
4045 end loop;
4047 if not Box_Present (Comp) then
4048 Analyze_And_Resolve (Expression (Comp), Comp_Type);
4049 end if;
4051 elsif Nkind (Comp) in
4052 N_Iterated_Component_Association |
4053 N_Iterated_Element_Association
4054 then
4055 Resolve_Iterated_Association
4056 (Comp, Index_Type, Comp_Type);
4058 -- Check the legality rule of RM22 4.3.5(28/5). Note that
4059 -- Is_Indexed_Aggregate can change its status (to False)
4060 -- as a result of calling Resolve_Iterated_Association,
4061 -- due to possible expansion of iterator_specifications
4062 -- there.
4064 if Is_Indexed_Aggregate
4065 (N, Add_Unnamed_Subp, New_Indexed_Subp)
4066 then
4067 if Nkind (Comp) = N_Iterated_Element_Association then
4068 if Present (Loop_Parameter_Specification (Comp))
4069 then
4070 if Present (Iterator_Filter
4071 (Loop_Parameter_Specification (Comp)))
4072 then
4073 Error_Msg_N
4074 ("iterator filter not allowed " &
4075 "in indexed aggregate (RM22 4.3.5(28))",
4076 Iterator_Filter
4077 (Loop_Parameter_Specification (Comp)));
4078 return;
4080 elsif Present (Key_Expression (Comp)) then
4081 Error_Msg_N
4082 ("key expression not allowed " &
4083 "in indexed aggregate (RM22 4.3.5(28))",
4084 Key_Expression (Comp));
4085 return;
4086 end if;
4088 elsif Present (Iterator_Specification (Comp)) then
4089 Error_Msg_N
4090 ("iterator specification not allowed " &
4091 "in indexed aggregate (RM22 4.3.5(28))",
4092 Iterator_Specification (Comp));
4093 return;
4094 end if;
4096 elsif Nkind (Comp) = N_Iterated_Component_Association
4097 and then Present (Iterator_Specification (Comp))
4098 then
4099 Error_Msg_N
4100 ("iterator specification not allowed " &
4101 "in indexed aggregate (RM22 4.3.5(28))",
4102 Iterator_Specification (Comp));
4103 return;
4104 end if;
4105 end if;
4107 Num_Choices := Num_Choices + 1;
4108 end if;
4110 Next (Comp);
4111 end loop;
4113 -- The component associations in an indexed aggregate
4114 -- must denote a contiguous set of static values. We
4115 -- build a table of values/ranges and sort it, as is done
4116 -- elsewhere for case statements and array aggregates.
4117 -- If the aggregate has a single iterated association it
4118 -- is allowed to be nonstatic and there is nothing to check.
4120 if Num_Choices > 1 then
4121 declare
4122 Table : Case_Table_Type (1 .. Num_Choices);
4123 No_Choice : Pos := 1;
4124 Lo, Hi : Node_Id;
4126 -- Traverse aggregate to determine size of needed table.
4127 -- Verify that bounds are static and that loops have no
4128 -- filters or key expressions.
4130 begin
4131 Comp := First (Component_Associations (N));
4132 while Present (Comp) loop
4134 -- If Nkind is N_Iterated_Component_Association,
4135 -- this corresponds to an iterator_specification
4136 -- with a loop_parameter_specification, and we
4137 -- have to pick up Discrete_Choices. In this case
4138 -- there will be just one "choice", which will
4139 -- typically be a range.
4141 if Nkind (Comp) = N_Iterated_Component_Association
4142 then
4143 Choice := First (Discrete_Choices (Comp));
4145 -- Case where there's a list of choices
4147 else
4148 Choice := First (Choices (Comp));
4149 end if;
4151 while Present (Choice) loop
4152 Get_Index_Bounds (Choice, Lo, Hi);
4153 Table (No_Choice).Choice := Choice;
4154 Table (No_Choice).Lo := Lo;
4155 Table (No_Choice).Hi := Hi;
4157 -- Verify staticness of value or range
4159 if not Is_Static_Expression (Lo)
4160 or else not Is_Static_Expression (Hi)
4161 then
4162 Error_Msg_N
4163 ("nonstatic expression for index " &
4164 "for indexed aggregate", Choice);
4165 return;
4166 end if;
4168 No_Choice := No_Choice + 1;
4169 Next (Choice);
4170 end loop;
4172 Next (Comp);
4173 end loop;
4175 Sort_Case_Table (Table);
4177 for J in 1 .. Num_Choices - 1 loop
4178 Hi_Val := Expr_Value (Table (J).Hi);
4179 Lo_Val := Expr_Value (Table (J + 1).Lo);
4181 if Lo_Val = Hi_Val then
4182 Error_Msg_N
4183 ("duplicate index in indexed aggregate",
4184 Table (J + 1).Choice);
4185 exit;
4187 elsif Lo_Val < Hi_Val then
4188 Error_Msg_N
4189 ("overlapping indices in indexed aggregate",
4190 Table (J + 1).Choice);
4191 exit;
4193 elsif Lo_Val > Hi_Val + 1 then
4194 Error_Msg_N
4195 ("missing index values", Table (J + 1).Choice);
4196 exit;
4197 end if;
4198 end loop;
4199 end;
4200 end if;
4201 end if;
4202 end;
4203 end if;
4204 end Resolve_Container_Aggregate;
4206 -----------------------------
4207 -- Resolve_Delta_Aggregate --
4208 -----------------------------
4210 procedure Resolve_Delta_Aggregate (N : Node_Id; Typ : Entity_Id) is
4211 Base : constant Node_Id := Expression (N);
4213 begin
4214 Error_Msg_Ada_2022_Feature ("delta aggregate", Sloc (N));
4216 if not Is_Composite_Type (Typ) then
4217 Error_Msg_N ("not a composite type", N);
4218 end if;
4220 Analyze_And_Resolve (Base, Typ);
4222 if Is_Array_Type (Typ) then
4223 -- For an array_delta_aggregate, the base_expression and each
4224 -- expression in every array_component_association shall be of a
4225 -- nonlimited type; RM 4.3.4(13/5). However, to prevent repeated
4226 -- errors we only check the base expression and not array component
4227 -- associations.
4229 if Is_Limited_Type (Etype (Base)) then
4230 Error_Msg_N
4231 ("array delta aggregate shall be of a nonlimited type", Base);
4232 Explain_Limited_Type (Etype (Base), Base);
4233 end if;
4235 Resolve_Delta_Array_Aggregate (N, Typ);
4236 else
4238 -- Delta aggregates for record types must use parentheses,
4239 -- not square brackets.
4241 if Is_Homogeneous_Aggregate (N) then
4242 Error_Msg_N
4243 ("delta aggregates for record types must use (), not '[']", N);
4244 end if;
4246 -- The base_expression of a record_delta_aggregate can be of a
4247 -- limited type only if it is newly constructed; RM 7.5(2.1/5).
4249 Check_Expr_OK_In_Limited_Aggregate (Base);
4251 Resolve_Delta_Record_Aggregate (N, Typ);
4252 end if;
4254 Set_Etype (N, Typ);
4255 end Resolve_Delta_Aggregate;
4257 -----------------------------------
4258 -- Resolve_Delta_Array_Aggregate --
4259 -----------------------------------
4261 procedure Resolve_Delta_Array_Aggregate (N : Node_Id; Typ : Entity_Id) is
4262 Deltas : constant List_Id := Component_Associations (N);
4263 Index_Type : constant Entity_Id := Etype (First_Index (Typ));
4265 Assoc : Node_Id;
4266 Choice : Node_Id;
4267 Expr : Node_Id;
4269 Deep_Choice_Seen : Boolean := False;
4271 begin
4272 Assoc := First (Deltas);
4273 while Present (Assoc) loop
4274 if Nkind (Assoc) = N_Iterated_Component_Association then
4275 Choice := First (Choice_List (Assoc));
4276 while Present (Choice) loop
4277 if Nkind (Choice) = N_Others_Choice then
4278 Error_Msg_N
4279 ("OTHERS not allowed in delta aggregate", Choice);
4281 elsif Nkind (Choice) = N_Subtype_Indication then
4282 Resolve_Discrete_Subtype_Indication
4283 (Choice, Base_Type (Index_Type));
4285 else
4286 Analyze_And_Resolve (Choice, Index_Type);
4287 end if;
4289 Next (Choice);
4290 end loop;
4292 declare
4293 Id : constant Entity_Id := Defining_Identifier (Assoc);
4294 Ent : constant Entity_Id :=
4295 New_Internal_Entity
4296 (E_Loop, Current_Scope, Sloc (Assoc), 'L');
4298 begin
4299 Set_Etype (Ent, Standard_Void_Type);
4300 Set_Parent (Ent, Assoc);
4301 Push_Scope (Ent);
4303 if No (Scope (Id)) then
4304 Set_Etype (Id, Index_Type);
4305 Mutate_Ekind (Id, E_Variable);
4306 Set_Is_Not_Self_Hidden (Id);
4307 Set_Scope (Id, Ent);
4308 end if;
4309 Enter_Name (Id);
4311 -- Resolve a copy of the expression, after setting
4312 -- its parent properly to preserve its context.
4314 Expr := New_Copy_Tree (Expression (Assoc));
4315 Set_Parent (Expr, Assoc);
4316 Analyze_And_Resolve (Expr, Component_Type (Typ));
4317 End_Scope;
4318 end;
4320 else
4321 Choice := First (Choice_List (Assoc));
4322 while Present (Choice) loop
4323 if Is_Deep_Choice (Choice, Typ) then
4324 pragma Assert (All_Extensions_Allowed);
4325 Deep_Choice_Seen := True;
4327 -- a deep delta aggregate
4328 Resolve_Deep_Delta_Assoc (Assoc, Typ);
4329 else
4330 Analyze (Choice);
4332 if Nkind (Choice) = N_Others_Choice then
4333 Error_Msg_N
4334 ("OTHERS not allowed in delta aggregate", Choice);
4336 elsif Is_Entity_Name (Choice)
4337 and then Is_Type (Entity (Choice))
4338 then
4339 -- Choice covers a range of values
4341 if Base_Type (Entity (Choice)) /=
4342 Base_Type (Index_Type)
4343 then
4344 Error_Msg_NE
4345 ("choice does not match index type of &",
4346 Choice, Typ);
4347 end if;
4349 elsif Nkind (Choice) = N_Subtype_Indication then
4350 Resolve_Discrete_Subtype_Indication
4351 (Choice, Base_Type (Index_Type));
4353 else
4354 Resolve (Choice, Index_Type);
4355 end if;
4356 end if;
4358 Next (Choice);
4359 end loop;
4361 -- For an array_delta_aggregate, the array_component_association
4362 -- shall not use the box symbol <>; RM 4.3.4(11/5).
4364 pragma Assert
4365 (Box_Present (Assoc) xor Present (Expression (Assoc)));
4367 if Box_Present (Assoc) then
4368 Error_Msg_N
4369 ("'<'> in array delta aggregate is not allowed", Assoc);
4370 elsif not Deep_Choice_Seen then
4371 Analyze_And_Resolve (Expression (Assoc), Component_Type (Typ));
4372 end if;
4373 end if;
4375 Next (Assoc);
4376 end loop;
4377 end Resolve_Delta_Array_Aggregate;
4379 ------------------------------------
4380 -- Resolve_Delta_Record_Aggregate --
4381 ------------------------------------
4383 procedure Resolve_Delta_Record_Aggregate (N : Node_Id; Typ : Entity_Id) is
4385 -- Variables used to verify that discriminant-dependent components
4386 -- appear in the same variant.
4388 Comp_Ref : Entity_Id := Empty; -- init to avoid warning
4389 Variant : Node_Id;
4391 procedure Check_Variant (Id : Node_Id);
4392 -- If a given component of the delta aggregate appears in a variant
4393 -- part, verify that it is within the same variant as that of previous
4394 -- specified variant components of the delta.
4396 function Get_Component_Type
4397 (Selector : Node_Id; Enclosing_Type : Entity_Id) return Entity_Id;
4398 -- Locate component with a given name and return its type.
4399 -- If none found then report error and return Empty.
4401 function Nested_In (V1 : Node_Id; V2 : Node_Id) return Boolean;
4402 -- Determine whether variant V1 is within variant V2
4404 function Variant_Depth (N : Node_Id) return Natural;
4405 -- Determine the distance of a variant to the enclosing type declaration
4407 --------------------
4408 -- Check_Variant --
4409 --------------------
4411 procedure Check_Variant (Id : Node_Id) is
4412 Comp : Entity_Id;
4413 Comp_Variant : Node_Id;
4415 begin
4416 if not Has_Discriminants (Typ) then
4417 return;
4418 end if;
4420 Comp := First_Entity (Typ);
4421 while Present (Comp) loop
4422 exit when Chars (Comp) = Chars (Id);
4423 Next_Component (Comp);
4424 end loop;
4426 -- Find the variant, if any, whose component list includes the
4427 -- component declaration.
4429 Comp_Variant := Parent (Parent (List_Containing (Parent (Comp))));
4430 if Nkind (Comp_Variant) = N_Variant then
4431 if No (Variant) then
4432 Variant := Comp_Variant;
4433 Comp_Ref := Comp;
4435 elsif Variant /= Comp_Variant then
4436 declare
4437 D1 : constant Integer := Variant_Depth (Variant);
4438 D2 : constant Integer := Variant_Depth (Comp_Variant);
4440 begin
4441 if D1 = D2
4442 or else
4443 (D1 > D2 and then not Nested_In (Variant, Comp_Variant))
4444 or else
4445 (D2 > D1 and then not Nested_In (Comp_Variant, Variant))
4446 then
4447 pragma Assert (Present (Comp_Ref));
4448 Error_Msg_Node_2 := Comp_Ref;
4449 Error_Msg_NE
4450 ("& and & appear in different variants", Id, Comp);
4452 -- Otherwise retain the deeper variant for subsequent tests
4454 elsif D2 > D1 then
4455 Variant := Comp_Variant;
4456 end if;
4457 end;
4458 end if;
4459 end if;
4460 end Check_Variant;
4462 ------------------------
4463 -- Get_Component_Type --
4464 ------------------------
4466 function Get_Component_Type
4467 (Selector : Node_Id; Enclosing_Type : Entity_Id) return Entity_Id
4469 Comp : Entity_Id;
4470 begin
4471 case Nkind (Selector) is
4472 when N_Selected_Component | N_Indexed_Component =>
4473 -- a deep delta aggregate choice
4475 declare
4476 Prefix_Type : constant Entity_Id :=
4477 Get_Component_Type (Prefix (Selector), Enclosing_Type);
4478 begin
4479 if No (Prefix_Type) then
4480 pragma Assert (Serious_Errors_Detected > 0);
4481 return Empty;
4482 end if;
4484 -- Set the type of the prefix for GNATprove
4486 Set_Etype (Prefix (Selector), Prefix_Type);
4488 if Nkind (Selector) = N_Selected_Component then
4489 return Get_Component_Type
4490 (Selector_Name (Selector),
4491 Enclosing_Type => Prefix_Type);
4492 elsif not Is_Array_Type (Prefix_Type) then
4493 Error_Msg_NE
4494 ("type& is not an array type",
4495 Selector, Prefix_Type);
4496 elsif Number_Dimensions (Prefix_Type) /= 1 then
4497 Error_Msg_NE
4498 ("array type& not one-dimensional",
4499 Selector, Prefix_Type);
4500 elsif List_Length (Expressions (Selector)) /= 1 then
4501 Error_Msg_NE
4502 ("wrong number of indices for array type&",
4503 Selector, Prefix_Type);
4504 else
4505 Analyze_And_Resolve
4506 (First (Expressions (Selector)),
4507 Etype (First_Index (Prefix_Type)));
4508 return Component_Type (Prefix_Type);
4509 end if;
4510 end;
4512 when others =>
4513 null;
4514 end case;
4516 Comp := First_Entity (Enclosing_Type);
4517 while Present (Comp) loop
4518 if Chars (Comp) = Chars (Selector) then
4519 if Ekind (Comp) = E_Discriminant then
4520 Error_Msg_N ("delta cannot apply to discriminant", Selector);
4521 end if;
4523 Set_Entity (Selector, Comp);
4524 Set_Etype (Selector, Etype (Comp));
4526 return Etype (Comp);
4527 end if;
4529 Next_Entity (Comp);
4530 end loop;
4532 Error_Msg_NE
4533 ("type& has no component with this name", Selector, Enclosing_Type);
4534 return Empty;
4535 end Get_Component_Type;
4537 ---------------
4538 -- Nested_In --
4539 ---------------
4541 function Nested_In (V1, V2 : Node_Id) return Boolean is
4542 Par : Node_Id;
4544 begin
4545 Par := Parent (V1);
4546 while Nkind (Par) /= N_Full_Type_Declaration loop
4547 if Par = V2 then
4548 return True;
4549 end if;
4551 Par := Parent (Par);
4552 end loop;
4554 return False;
4555 end Nested_In;
4557 -------------------
4558 -- Variant_Depth --
4559 -------------------
4561 function Variant_Depth (N : Node_Id) return Natural is
4562 Depth : Natural;
4563 Par : Node_Id;
4565 begin
4566 Depth := 0;
4567 Par := Parent (N);
4568 while Nkind (Par) /= N_Full_Type_Declaration loop
4569 Depth := Depth + 1;
4570 Par := Parent (Par);
4571 end loop;
4573 return Depth;
4574 end Variant_Depth;
4576 -- Local variables
4578 Deltas : constant List_Id := Component_Associations (N);
4580 Assoc : Node_Id;
4581 Choice : Node_Id;
4582 Comp_Type : Entity_Id := Empty; -- init to avoid warning
4583 Deep_Choice : Boolean;
4585 -- Start of processing for Resolve_Delta_Record_Aggregate
4587 begin
4588 Variant := Empty;
4590 Assoc := First (Deltas);
4591 while Present (Assoc) loop
4592 Choice := First (Choice_List (Assoc));
4593 while Present (Choice) loop
4594 Deep_Choice := Nkind (Choice) /= N_Identifier;
4595 if Deep_Choice then
4596 Error_Msg_GNAT_Extension
4597 ("deep delta aggregate", Sloc (Choice));
4598 end if;
4600 Comp_Type := Get_Component_Type
4601 (Selector => Choice, Enclosing_Type => Typ);
4603 -- Set the type of the choice for GNATprove
4605 if Deep_Choice then
4606 Set_Etype (Choice, Comp_Type);
4607 end if;
4609 if Present (Comp_Type) then
4610 if not Deep_Choice then
4611 -- ??? Not clear yet how RM 4.3.1(17.7) applies to a
4612 -- deep delta aggregate.
4613 Check_Variant (Choice);
4614 end if;
4615 else
4616 Comp_Type := Any_Type;
4617 end if;
4619 Next (Choice);
4620 end loop;
4622 pragma Assert (Present (Comp_Type));
4624 -- A record_component_association in record_delta_aggregate shall not
4625 -- use the box compound delimiter <> rather than an expression; see
4626 -- RM 4.3.1(17.3/5).
4628 pragma Assert (Present (Expression (Assoc)) xor Box_Present (Assoc));
4630 if Box_Present (Assoc) then
4631 Error_Msg_N
4632 ("'<'> in record delta aggregate is not allowed", Assoc);
4633 else
4634 Analyze_And_Resolve (Expression (Assoc), Comp_Type);
4636 -- The expression must not be of a limited type; RM 4.3.1(17.4/5)
4638 if Is_Limited_Type (Etype (Expression (Assoc))) then
4639 Error_Msg_N
4640 ("expression of a limited type in record delta aggregate " &
4641 "is not allowed",
4642 Expression (Assoc));
4643 end if;
4644 end if;
4646 Next (Assoc);
4647 end loop;
4648 end Resolve_Delta_Record_Aggregate;
4650 ------------------------------
4651 -- Resolve_Deep_Delta_Assoc --
4652 ------------------------------
4654 procedure Resolve_Deep_Delta_Assoc (N : Node_Id; Typ : Entity_Id) is
4655 Choice : constant Node_Id := First (Choice_List (N));
4656 Enclosing_Type : Entity_Id := Typ;
4658 procedure Resolve_Choice_Prefix
4659 (Choice_Prefix : Node_Id; Enclosing_Type : in out Entity_Id);
4660 -- Recursively analyze selectors. Enclosing_Type is set to
4661 -- type of the last component.
4663 ---------------------------
4664 -- Resolve_Choice_Prefix --
4665 ---------------------------
4667 procedure Resolve_Choice_Prefix
4668 (Choice_Prefix : Node_Id; Enclosing_Type : in out Entity_Id)
4670 Selector : Node_Id := Choice_Prefix;
4671 begin
4672 if not Is_Root_Prefix_Of_Deep_Choice (Choice_Prefix) then
4673 Resolve_Choice_Prefix (Prefix (Choice_Prefix), Enclosing_Type);
4675 if Nkind (Choice_Prefix) = N_Selected_Component then
4676 Selector := Selector_Name (Choice_Prefix);
4677 else
4678 pragma Assert (Nkind (Choice_Prefix) = N_Indexed_Component);
4679 Selector := First (Expressions (Choice_Prefix));
4680 end if;
4681 end if;
4683 if Is_Array_Type (Enclosing_Type) then
4684 Analyze_And_Resolve (Selector,
4685 Etype (First_Index (Enclosing_Type)));
4686 Enclosing_Type := Component_Type (Enclosing_Type);
4687 else
4688 declare
4689 Comp : Entity_Id := First_Entity (Enclosing_Type);
4690 Found : Boolean := False;
4691 begin
4692 while Present (Comp) and not Found loop
4693 if Chars (Comp) = Chars (Selector) then
4694 if Ekind (Comp) = E_Discriminant then
4695 Error_Msg_N ("delta cannot apply to discriminant",
4696 Selector);
4697 end if;
4698 Found := True;
4699 Set_Entity (Selector, Comp);
4700 Set_Etype (Selector, Etype (Comp));
4701 Set_Analyzed (Selector);
4702 Enclosing_Type := Etype (Comp);
4703 else
4704 Next_Entity (Comp);
4705 end if;
4706 end loop;
4707 if not Found then
4708 Error_Msg_NE
4709 ("type& has no component with this name",
4710 Selector, Enclosing_Type);
4711 end if;
4712 end;
4713 end if;
4715 -- Set the type of the prefix for GNATprove, except for the root
4716 -- prefix, whose type is already the expected one for a record
4717 -- delta aggregate, or the type of the array index for an
4718 -- array delta aggregate (the only case here really since
4719 -- Resolve_Deep_Delta_Assoc is only called for array delta
4720 -- aggregates).
4722 if Selector /= Choice_Prefix then
4723 Set_Etype (Choice_Prefix, Enclosing_Type);
4724 end if;
4725 end Resolve_Choice_Prefix;
4726 begin
4727 declare
4728 Unimplemented : exception; -- TEMPORARY
4729 begin
4730 if Present (Next (Choice)) then
4731 raise Unimplemented;
4732 end if;
4733 end;
4735 Resolve_Choice_Prefix (Choice, Enclosing_Type);
4736 Analyze_And_Resolve (Expression (N), Enclosing_Type);
4737 end Resolve_Deep_Delta_Assoc;
4739 ---------------------------------
4740 -- Resolve_Extension_Aggregate --
4741 ---------------------------------
4743 -- There are two cases to consider:
4745 -- a) If the ancestor part is a type mark, the components needed are the
4746 -- difference between the components of the expected type and the
4747 -- components of the given type mark.
4749 -- b) If the ancestor part is an expression, it must be unambiguous, and
4750 -- once we have its type we can also compute the needed components as in
4751 -- the previous case. In both cases, if the ancestor type is not the
4752 -- immediate ancestor, we have to build this ancestor recursively.
4754 -- In both cases, discriminants of the ancestor type do not play a role in
4755 -- the resolution of the needed components, because inherited discriminants
4756 -- cannot be used in a type extension. As a result we can compute
4757 -- independently the list of components of the ancestor type and of the
4758 -- expected type.
4760 procedure Resolve_Extension_Aggregate (N : Node_Id; Typ : Entity_Id) is
4761 A : constant Node_Id := Ancestor_Part (N);
4762 A_Type : Entity_Id;
4763 I : Interp_Index;
4764 It : Interp;
4766 function Valid_Limited_Ancestor (Anc : Node_Id) return Boolean;
4767 -- If the type is limited, verify that the ancestor part is a legal
4768 -- expression (aggregate or function call, including 'Input)) that does
4769 -- not require a copy, as specified in 7.5(2).
4771 function Valid_Ancestor_Type return Boolean;
4772 -- Verify that the type of the ancestor part is a non-private ancestor
4773 -- of the expected type, which must be a type extension.
4775 ----------------------------
4776 -- Valid_Limited_Ancestor --
4777 ----------------------------
4779 function Valid_Limited_Ancestor (Anc : Node_Id) return Boolean is
4780 begin
4781 if Is_Entity_Name (Anc) and then Is_Type (Entity (Anc)) then
4782 return True;
4784 -- The ancestor must be a call or an aggregate, but a call may
4785 -- have been expanded into a temporary, so check original node.
4787 elsif Nkind (Anc) in N_Aggregate
4788 | N_Extension_Aggregate
4789 | N_Function_Call
4790 then
4791 return True;
4793 elsif Nkind (Original_Node (Anc)) = N_Function_Call then
4794 return True;
4796 elsif Nkind (Anc) = N_Attribute_Reference
4797 and then Attribute_Name (Anc) = Name_Input
4798 then
4799 return True;
4801 elsif Nkind (Anc) = N_Qualified_Expression then
4802 return Valid_Limited_Ancestor (Expression (Anc));
4804 elsif Nkind (Anc) = N_Raise_Expression then
4805 return True;
4807 else
4808 return False;
4809 end if;
4810 end Valid_Limited_Ancestor;
4812 -------------------------
4813 -- Valid_Ancestor_Type --
4814 -------------------------
4816 function Valid_Ancestor_Type return Boolean is
4817 Imm_Type : Entity_Id;
4819 begin
4820 Imm_Type := Base_Type (Typ);
4821 while Is_Derived_Type (Imm_Type) loop
4822 if Etype (Imm_Type) = Base_Type (A_Type) then
4823 return True;
4825 -- The base type of the parent type may appear as a private
4826 -- extension if it is declared as such in a parent unit of the
4827 -- current one. For consistency of the subsequent analysis use
4828 -- the partial view for the ancestor part.
4830 elsif Is_Private_Type (Etype (Imm_Type))
4831 and then Present (Full_View (Etype (Imm_Type)))
4832 and then Base_Type (A_Type) = Full_View (Etype (Imm_Type))
4833 then
4834 A_Type := Etype (Imm_Type);
4835 return True;
4837 -- The parent type may be a private extension. The aggregate is
4838 -- legal if the type of the aggregate is an extension of it that
4839 -- is not a private extension.
4841 elsif Is_Private_Type (A_Type)
4842 and then not Is_Private_Type (Imm_Type)
4843 and then Present (Full_View (A_Type))
4844 and then Base_Type (Full_View (A_Type)) = Etype (Imm_Type)
4845 then
4846 return True;
4848 -- The parent type may be a raise expression (which is legal in
4849 -- any expression context).
4851 elsif A_Type = Raise_Type then
4852 A_Type := Etype (Imm_Type);
4853 return True;
4855 else
4856 Imm_Type := Etype (Base_Type (Imm_Type));
4857 end if;
4858 end loop;
4860 -- If previous loop did not find a proper ancestor, report error
4862 Error_Msg_NE ("expect ancestor type of &", A, Typ);
4863 return False;
4864 end Valid_Ancestor_Type;
4866 -- Start of processing for Resolve_Extension_Aggregate
4868 begin
4869 -- Analyze the ancestor part and account for the case where it is a
4870 -- parameterless function call.
4872 Analyze (A);
4873 Check_Parameterless_Call (A);
4875 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
4877 -- AI05-0115: If the ancestor part is a subtype mark, the ancestor
4878 -- must not have unknown discriminants. To catch cases where the
4879 -- aggregate occurs at a place where the full view of the ancestor
4880 -- type is visible and doesn't have unknown discriminants, but the
4881 -- aggregate type was derived from a partial view that has unknown
4882 -- discriminants, we check whether the aggregate type has unknown
4883 -- discriminants (unknown discriminants were inherited), along
4884 -- with checking that the partial view of the ancestor has unknown
4885 -- discriminants. (It might be sufficient to replace the entire
4886 -- condition with Has_Unknown_Discriminants (Typ), but that might
4887 -- miss some cases, not clear, and causes error changes in some tests
4888 -- such as class-wide cases, that aren't clearly improvements. ???)
4890 if Has_Unknown_Discriminants (Entity (A))
4891 or else (Has_Unknown_Discriminants (Typ)
4892 and then Partial_View_Has_Unknown_Discr (Entity (A)))
4893 then
4894 Error_Msg_NE
4895 ("aggregate not available for type& whose ancestor "
4896 & "has unknown discriminants", N, Typ);
4897 end if;
4898 end if;
4900 if not Is_Tagged_Type (Typ) then
4901 Error_Msg_N ("type of extension aggregate must be tagged", N);
4902 return;
4904 elsif Is_Limited_Type (Typ) then
4906 -- Ada 2005 (AI-287): Limited aggregates are allowed
4908 if Ada_Version < Ada_2005 then
4909 Error_Msg_N ("aggregate type cannot be limited", N);
4910 Explain_Limited_Type (Typ, N);
4911 return;
4913 elsif Valid_Limited_Ancestor (A) then
4914 null;
4916 else
4917 Error_Msg_N
4918 ("limited ancestor part must be aggregate or function call", A);
4919 end if;
4921 elsif Is_Class_Wide_Type (Typ) then
4922 Error_Msg_N ("aggregate cannot be of a class-wide type", N);
4923 return;
4924 end if;
4926 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
4927 A_Type := Get_Full_View (Entity (A));
4929 if Valid_Ancestor_Type then
4930 Set_Entity (A, A_Type);
4931 Set_Etype (A, A_Type);
4933 Validate_Ancestor_Part (N);
4934 Resolve_Record_Aggregate (N, Typ);
4935 end if;
4937 elsif Nkind (A) /= N_Aggregate then
4938 if Is_Overloaded (A) then
4939 A_Type := Any_Type;
4941 Get_First_Interp (A, I, It);
4942 while Present (It.Typ) loop
4944 -- Consider limited interpretations if Ada 2005 or higher
4946 if Is_Tagged_Type (It.Typ)
4947 and then (Ada_Version >= Ada_2005
4948 or else not Is_Limited_Type (It.Typ))
4949 then
4950 if A_Type /= Any_Type then
4951 Error_Msg_N ("cannot resolve expression", A);
4952 return;
4953 else
4954 A_Type := It.Typ;
4955 end if;
4956 end if;
4958 Get_Next_Interp (I, It);
4959 end loop;
4961 if A_Type = Any_Type then
4962 if Ada_Version >= Ada_2005 then
4963 Error_Msg_N
4964 ("ancestor part must be of a tagged type", A);
4965 else
4966 Error_Msg_N
4967 ("ancestor part must be of a nonlimited tagged type", A);
4968 end if;
4970 return;
4971 end if;
4973 else
4974 A_Type := Etype (A);
4975 end if;
4977 if Valid_Ancestor_Type then
4978 Resolve (A, A_Type);
4979 Check_Unset_Reference (A);
4980 Check_Non_Static_Context (A);
4982 -- The aggregate is illegal if the ancestor expression is a call
4983 -- to a function with a limited unconstrained result, unless the
4984 -- type of the aggregate is a null extension. This restriction
4985 -- was added in AI05-67 to simplify implementation.
4987 if Nkind (A) = N_Function_Call
4988 and then Is_Limited_Type (A_Type)
4989 and then not Is_Null_Extension (Typ)
4990 and then not Is_Constrained (A_Type)
4991 then
4992 Error_Msg_N
4993 ("type of limited ancestor part must be constrained", A);
4995 -- Reject the use of CPP constructors that leave objects partially
4996 -- initialized. For example:
4998 -- type CPP_Root is tagged limited record ...
4999 -- pragma Import (CPP, CPP_Root);
5001 -- type CPP_DT is new CPP_Root and Iface ...
5002 -- pragma Import (CPP, CPP_DT);
5004 -- type Ada_DT is new CPP_DT with ...
5006 -- Obj : Ada_DT := Ada_DT'(New_CPP_Root with others => <>);
5008 -- Using the constructor of CPP_Root the slots of the dispatch
5009 -- table of CPP_DT cannot be set, and the secondary tag of
5010 -- CPP_DT is unknown.
5012 elsif Nkind (A) = N_Function_Call
5013 and then Is_CPP_Constructor_Call (A)
5014 and then Enclosing_CPP_Parent (Typ) /= A_Type
5015 then
5016 Error_Msg_NE
5017 ("??must use 'C'P'P constructor for type &", A,
5018 Enclosing_CPP_Parent (Typ));
5020 -- The following call is not needed if the previous warning
5021 -- is promoted to an error.
5023 Resolve_Record_Aggregate (N, Typ);
5025 elsif Is_Class_Wide_Type (Etype (A))
5026 and then Nkind (Original_Node (A)) = N_Function_Call
5027 then
5028 -- If the ancestor part is a dispatching call, it appears
5029 -- statically to be a legal ancestor, but it yields any member
5030 -- of the class, and it is not possible to determine whether
5031 -- it is an ancestor of the extension aggregate (much less
5032 -- which ancestor). It is not possible to determine the
5033 -- components of the extension part.
5035 -- This check implements AI-306, which in fact was motivated by
5036 -- an AdaCore query to the ARG after this test was added.
5038 Error_Msg_N ("ancestor part must be statically tagged", A);
5040 else
5041 Resolve_Record_Aggregate (N, Typ);
5042 end if;
5043 end if;
5045 else
5046 Error_Msg_N ("no unique type for this aggregate", A);
5047 end if;
5049 Check_Function_Writable_Actuals (N);
5050 end Resolve_Extension_Aggregate;
5052 ----------------------------------
5053 -- Resolve_Null_Array_Aggregate --
5054 ----------------------------------
5056 function Resolve_Null_Array_Aggregate (N : Node_Id) return Boolean is
5057 -- Never returns False, but declared as a function to match
5058 -- other Resolve_Mumble functions.
5060 Loc : constant Source_Ptr := Sloc (N);
5061 Typ : constant Entity_Id := Etype (N);
5063 Constr : constant List_Id := New_List;
5064 Index : Node_Id;
5065 Index_Typ : Node_Id;
5066 Known_Bounds : Boolean := True;
5067 Lo, Hi : Node_Id;
5069 begin
5070 -- Attach the list of constraints at the location of the aggregate, so
5071 -- the individual constraints can be analyzed.
5073 Set_Parent (Constr, N);
5075 -- Populate the list with null ranges. The relevant RM clauses are
5076 -- RM 4.3.3 (26.1) and RM 4.3.3 (26).
5078 Index := First_Index (Typ);
5079 while Present (Index) loop
5080 Get_Index_Bounds (Index, L => Lo, H => Hi);
5081 Index_Typ := Etype (Index);
5083 Known_Bounds := Known_Bounds
5084 and Compile_Time_Known_Value (Lo)
5085 and Compile_Time_Known_Value (Hi);
5087 if Cannot_Compute_High_Bound (Index) then
5088 -- The upper bound is the higger bound to avoid reporting
5089 -- spurious errors; this value will not be used at runtime
5090 -- because this aggregate will be replaced by a raise CE node,
5091 -- or the index type is formal of a generic unit.
5093 Hi := New_Copy_Tree (Lo);
5095 Report_Null_Array_Constraint_Error (N, Index_Typ);
5096 Set_Raises_Constraint_Error (N);
5098 else
5099 -- The upper bound is the predecessor of the lower bound
5101 Hi := Make_Attribute_Reference (Loc,
5102 Prefix => New_Occurrence_Of (Etype (Index), Loc),
5103 Attribute_Name => Name_Pred,
5104 Expressions => New_List (New_Copy_Tree (Lo)));
5105 end if;
5107 Append (Make_Range (Loc, New_Copy_Tree (Lo), Hi), Constr);
5108 Analyze_And_Resolve (Last (Constr), Etype (Index));
5110 Next_Index (Index);
5111 end loop;
5113 Set_Compile_Time_Known_Aggregate (N, Known_Bounds);
5114 Set_Aggregate_Bounds (N, First (Constr));
5116 return True;
5117 end Resolve_Null_Array_Aggregate;
5119 ------------------------------
5120 -- Resolve_Record_Aggregate --
5121 ------------------------------
5123 procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id) is
5124 New_Assoc_List : constant List_Id := New_List;
5125 -- New_Assoc_List is the newly built list of N_Component_Association
5126 -- nodes.
5128 Others_Etype : Entity_Id := Empty;
5129 -- This variable is used to save the Etype of the last record component
5130 -- that takes its value from the others choice. Its purpose is:
5132 -- (a) make sure the others choice is useful
5134 -- (b) make sure the type of all the components whose value is
5135 -- subsumed by the others choice are the same.
5137 -- This variable is updated as a side effect of function Get_Value.
5139 Box_Node : Node_Id := Empty;
5140 Is_Box_Present : Boolean := False;
5141 Is_Box_Init_By_Default : Boolean := False;
5142 Others_Box : Natural := 0;
5143 -- Ada 2005 (AI-287): Variables used in case of default initialization
5144 -- to provide a functionality similar to Others_Etype. Box_Present
5145 -- indicates that the component takes its default initialization;
5146 -- Others_Box counts the number of components of the current aggregate
5147 -- (which may be a sub-aggregate of a larger one) that are default-
5148 -- initialized. A value of One indicates that an others_box is present.
5149 -- Any larger value indicates that the others_box is not redundant.
5150 -- These variables, similar to Others_Etype, are also updated as a side
5151 -- effect of function Get_Value. Box_Node is used to place a warning on
5152 -- a redundant others_box.
5154 procedure Add_Association
5155 (Component : Entity_Id;
5156 Expr : Node_Id;
5157 Assoc_List : List_Id;
5158 Is_Box_Present : Boolean := False);
5159 -- Builds a new N_Component_Association node which associates Component
5160 -- to expression Expr and adds it to the association list being built,
5161 -- either New_Assoc_List, or the association being built for an inner
5162 -- aggregate.
5164 function Discriminant_Present (Input_Discr : Entity_Id) return Boolean;
5165 -- If aggregate N is a regular aggregate this routine will return True.
5166 -- Otherwise, if N is an extension aggregate, then Input_Discr denotes
5167 -- a discriminant whose value may already have been specified by N's
5168 -- ancestor part. This routine checks whether this is indeed the case
5169 -- and if so returns False, signaling that no value for Input_Discr
5170 -- should appear in N's aggregate part. Also, in this case, the routine
5171 -- appends to New_Assoc_List the discriminant value specified in the
5172 -- ancestor part.
5174 -- If the aggregate is in a context with expansion delayed, it will be
5175 -- reanalyzed. The inherited discriminant values must not be reinserted
5176 -- in the component list to prevent spurious errors, but they must be
5177 -- present on first analysis to build the proper subtype indications.
5178 -- The flag Inherited_Discriminant is used to prevent the re-insertion.
5180 function Find_Private_Ancestor (Typ : Entity_Id) return Entity_Id;
5181 -- AI05-0115: Find earlier ancestor in the derivation chain that is
5182 -- derived from private view Typ. Whether the aggregate is legal depends
5183 -- on the current visibility of the type as well as that of the parent
5184 -- of the ancestor.
5186 function Get_Value
5187 (Compon : Entity_Id;
5188 From : List_Id;
5189 Consider_Others_Choice : Boolean := False) return Node_Id;
5190 -- Given a record component stored in parameter Compon, this function
5191 -- returns its value as it appears in the list From, which is a list
5192 -- of N_Component_Association nodes.
5194 -- If no component association has a choice for the searched component,
5195 -- the value provided by the others choice is returned, if there is one,
5196 -- and Consider_Others_Choice is set to true. Otherwise Empty is
5197 -- returned. If there is more than one component association giving a
5198 -- value for the searched record component, an error message is emitted
5199 -- and the first found value is returned.
5201 -- If Consider_Others_Choice is set and the returned expression comes
5202 -- from the others choice, then Others_Etype is set as a side effect.
5203 -- An error message is emitted if the components taking their value from
5204 -- the others choice do not have same type.
5206 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Entity_Id);
5207 -- Analyzes and resolves expression Expr against the Etype of the
5208 -- Component. This routine also applies all appropriate checks to Expr.
5209 -- It finally saves a Expr in the newly created association list that
5210 -- will be attached to the final record aggregate. Note that if the
5211 -- Parent pointer of Expr is not set then Expr was produced with a
5212 -- New_Copy_Tree or some such.
5214 procedure Rewrite_Range (Root_Type : Entity_Id; Rge : Node_Id);
5215 -- Rewrite a range node Rge when its bounds refer to non-stored
5216 -- discriminants from Root_Type, to replace them with the stored
5217 -- discriminant values. This is required in GNATprove mode, and is
5218 -- adopted in all modes to avoid special-casing GNATprove mode.
5220 ---------------------
5221 -- Add_Association --
5222 ---------------------
5224 procedure Add_Association
5225 (Component : Entity_Id;
5226 Expr : Node_Id;
5227 Assoc_List : List_Id;
5228 Is_Box_Present : Boolean := False)
5230 Choice_List : constant List_Id := New_List;
5231 Loc : Source_Ptr;
5233 begin
5234 -- If this is a box association the expression is missing, so use the
5235 -- Sloc of the aggregate itself for the new association.
5237 pragma Assert (Present (Expr) xor Is_Box_Present);
5239 if Present (Expr) then
5240 Loc := Sloc (Expr);
5241 else
5242 Loc := Sloc (N);
5243 end if;
5245 Append_To (Choice_List, New_Occurrence_Of (Component, Loc));
5247 Append_To (Assoc_List,
5248 Make_Component_Association (Loc,
5249 Choices => Choice_List,
5250 Expression => Expr,
5251 Box_Present => Is_Box_Present));
5253 -- If this association has a box for a component that is initialized
5254 -- by default, then set flag on the new association to indicate that
5255 -- the original association was for such a box-initialized component.
5257 if Is_Box_Init_By_Default then
5258 Set_Was_Default_Init_Box_Association (Last (Assoc_List));
5259 end if;
5260 end Add_Association;
5262 --------------------------
5263 -- Discriminant_Present --
5264 --------------------------
5266 function Discriminant_Present (Input_Discr : Entity_Id) return Boolean is
5267 Regular_Aggr : constant Boolean := Nkind (N) /= N_Extension_Aggregate;
5269 Ancestor_Is_Subtyp : Boolean;
5271 Loc : Source_Ptr;
5273 Ancestor : Node_Id;
5274 Ancestor_Typ : Entity_Id;
5275 Comp_Assoc : Node_Id;
5276 Discr : Entity_Id;
5277 Discr_Expr : Node_Id;
5278 Discr_Val : Elmt_Id := No_Elmt;
5279 Orig_Discr : Entity_Id;
5281 begin
5282 if Regular_Aggr then
5283 return True;
5284 end if;
5286 -- Check whether inherited discriminant values have already been
5287 -- inserted in the aggregate. This will be the case if we are
5288 -- re-analyzing an aggregate whose expansion was delayed.
5290 if Present (Component_Associations (N)) then
5291 Comp_Assoc := First (Component_Associations (N));
5292 while Present (Comp_Assoc) loop
5293 if Inherited_Discriminant (Comp_Assoc) then
5294 return True;
5295 end if;
5297 Next (Comp_Assoc);
5298 end loop;
5299 end if;
5301 Ancestor := Ancestor_Part (N);
5302 Ancestor_Typ := Etype (Ancestor);
5303 Loc := Sloc (Ancestor);
5305 -- For a private type with unknown discriminants, use the underlying
5306 -- record view if it is available.
5308 if Has_Unknown_Discriminants (Ancestor_Typ)
5309 and then Present (Full_View (Ancestor_Typ))
5310 and then Present (Underlying_Record_View (Full_View (Ancestor_Typ)))
5311 then
5312 Ancestor_Typ := Underlying_Record_View (Full_View (Ancestor_Typ));
5313 end if;
5315 Ancestor_Is_Subtyp :=
5316 Is_Entity_Name (Ancestor) and then Is_Type (Entity (Ancestor));
5318 -- If the ancestor part has no discriminants clearly N's aggregate
5319 -- part must provide a value for Discr.
5321 if not Has_Discriminants (Ancestor_Typ) then
5322 return True;
5324 -- If the ancestor part is an unconstrained subtype mark then the
5325 -- Discr must be present in N's aggregate part.
5327 elsif Ancestor_Is_Subtyp
5328 and then not Is_Constrained (Entity (Ancestor))
5329 then
5330 return True;
5331 end if;
5333 -- Now look to see if Discr was specified in the ancestor part
5335 if Ancestor_Is_Subtyp then
5336 Discr_Val :=
5337 First_Elmt (Discriminant_Constraint (Entity (Ancestor)));
5338 end if;
5340 Orig_Discr := Original_Record_Component (Input_Discr);
5342 Discr := First_Discriminant (Ancestor_Typ);
5343 while Present (Discr) loop
5345 -- If Ancestor has already specified Disc value then insert its
5346 -- value in the final aggregate.
5348 if Original_Record_Component (Discr) = Orig_Discr then
5349 if Ancestor_Is_Subtyp then
5350 Discr_Expr := New_Copy_Tree (Node (Discr_Val));
5351 else
5352 Discr_Expr :=
5353 Make_Selected_Component (Loc,
5354 Prefix => Duplicate_Subexpr (Ancestor),
5355 Selector_Name => New_Occurrence_Of (Input_Discr, Loc));
5356 end if;
5358 Resolve_Aggr_Expr (Discr_Expr, Input_Discr);
5359 Set_Inherited_Discriminant (Last (New_Assoc_List));
5360 return False;
5361 end if;
5363 Next_Discriminant (Discr);
5365 if Ancestor_Is_Subtyp then
5366 Next_Elmt (Discr_Val);
5367 end if;
5368 end loop;
5370 return True;
5371 end Discriminant_Present;
5373 ---------------------------
5374 -- Find_Private_Ancestor --
5375 ---------------------------
5377 function Find_Private_Ancestor (Typ : Entity_Id) return Entity_Id is
5378 Par : Entity_Id;
5380 begin
5381 Par := Typ;
5382 loop
5383 if Has_Private_Ancestor (Par)
5384 and then not Has_Private_Ancestor (Etype (Base_Type (Par)))
5385 then
5386 return Par;
5388 elsif not Is_Derived_Type (Par) then
5389 return Empty;
5391 else
5392 Par := Etype (Base_Type (Par));
5393 end if;
5394 end loop;
5395 end Find_Private_Ancestor;
5397 ---------------
5398 -- Get_Value --
5399 ---------------
5401 function Get_Value
5402 (Compon : Entity_Id;
5403 From : List_Id;
5404 Consider_Others_Choice : Boolean := False) return Node_Id
5406 Typ : constant Entity_Id := Etype (Compon);
5407 Assoc : Node_Id;
5408 Expr : Node_Id := Empty;
5409 Selector_Name : Node_Id;
5411 begin
5412 Is_Box_Present := False;
5413 Is_Box_Init_By_Default := False;
5415 if No (From) then
5416 return Empty;
5417 end if;
5419 Assoc := First (From);
5420 while Present (Assoc) loop
5421 Selector_Name := First (Choices (Assoc));
5422 while Present (Selector_Name) loop
5423 if Nkind (Selector_Name) = N_Others_Choice then
5424 if Consider_Others_Choice and then No (Expr) then
5426 -- We need to duplicate the expression for each
5427 -- successive component covered by the others choice.
5428 -- This is redundant if the others_choice covers only
5429 -- one component (small optimization possible???), but
5430 -- indispensable otherwise, because each one must be
5431 -- expanded individually to preserve side effects.
5433 -- Ada 2005 (AI-287): In case of default initialization
5434 -- of components, we duplicate the corresponding default
5435 -- expression (from the record type declaration). The
5436 -- copy must carry the sloc of the association (not the
5437 -- original expression) to prevent spurious elaboration
5438 -- checks when the default includes function calls.
5440 if Box_Present (Assoc) then
5441 Others_Box := Others_Box + 1;
5442 Is_Box_Present := True;
5444 if Expander_Active then
5445 return
5446 New_Copy_Tree_And_Copy_Dimensions
5447 (Expression (Parent (Compon)),
5448 New_Sloc => Sloc (Assoc));
5449 else
5450 return Expression (Parent (Compon));
5451 end if;
5453 else
5454 if Present (Others_Etype)
5455 and then Base_Type (Others_Etype) /= Base_Type (Typ)
5456 then
5457 -- If the components are of an anonymous access
5458 -- type they are distinct, but this is legal in
5459 -- Ada 2012 as long as designated types match.
5461 if (Ekind (Typ) = E_Anonymous_Access_Type
5462 or else Ekind (Typ) =
5463 E_Anonymous_Access_Subprogram_Type)
5464 and then Designated_Type (Typ) =
5465 Designated_Type (Others_Etype)
5466 then
5467 null;
5468 else
5469 Error_Msg_N
5470 ("components in OTHERS choice must have same "
5471 & "type", Selector_Name);
5472 end if;
5473 end if;
5475 Others_Etype := Typ;
5477 -- Copy the expression so that it is resolved
5478 -- independently for each component, This is needed
5479 -- for accessibility checks on components of anonymous
5480 -- access types, even in compile_only mode.
5482 if not Inside_A_Generic then
5483 return
5484 New_Copy_Tree_And_Copy_Dimensions
5485 (Expression (Assoc));
5486 else
5487 return Expression (Assoc);
5488 end if;
5489 end if;
5490 end if;
5492 elsif Chars (Compon) = Chars (Selector_Name) then
5493 if No (Expr) then
5495 -- Ada 2005 (AI-231)
5497 if Ada_Version >= Ada_2005
5498 and then Known_Null (Expression (Assoc))
5499 then
5500 Check_Can_Never_Be_Null (Compon, Expression (Assoc));
5501 end if;
5503 -- We need to duplicate the expression when several
5504 -- components are grouped together with a "|" choice.
5505 -- For instance "filed1 | filed2 => Expr"
5507 -- Ada 2005 (AI-287)
5509 if Box_Present (Assoc) then
5510 Is_Box_Present := True;
5512 -- Duplicate the default expression of the component
5513 -- from the record type declaration, so a new copy
5514 -- can be attached to the association.
5516 -- Note that we always copy the default expression,
5517 -- even when the association has a single choice, in
5518 -- order to create a proper association for the
5519 -- expanded aggregate.
5521 -- Component may have no default, in which case the
5522 -- expression is empty and the component is default-
5523 -- initialized, but an association for the component
5524 -- exists, and it is not covered by an others clause.
5526 -- Scalar and private types have no initialization
5527 -- procedure, so they remain uninitialized. If the
5528 -- target of the aggregate is a constant this
5529 -- deserves a warning.
5531 if No (Expression (Parent (Compon)))
5532 and then not Has_Non_Null_Base_Init_Proc (Typ)
5533 and then not Has_Aspect (Typ, Aspect_Default_Value)
5534 and then not Is_Concurrent_Type (Typ)
5535 and then Nkind (Parent (N)) = N_Object_Declaration
5536 and then Constant_Present (Parent (N))
5537 then
5538 Error_Msg_Node_2 := Typ;
5539 Error_Msg_NE
5540 ("??component& of type& is uninitialized",
5541 Assoc, Selector_Name);
5543 -- An additional reminder if the component type
5544 -- is a generic formal.
5546 if Is_Generic_Type (Base_Type (Typ)) then
5547 Error_Msg_NE
5548 ("\instance should provide actual type with "
5549 & "initialization for&", Assoc, Typ);
5550 end if;
5551 end if;
5553 return
5554 New_Copy_Tree_And_Copy_Dimensions
5555 (Expression (Parent (Compon)));
5557 else
5558 if Present (Next (Selector_Name)) then
5559 Expr := New_Copy_Tree_And_Copy_Dimensions
5560 (Expression (Assoc));
5561 else
5562 Expr := Expression (Assoc);
5563 end if;
5564 end if;
5566 Generate_Reference (Compon, Selector_Name, 'm');
5568 else
5569 Error_Msg_NE
5570 ("more than one value supplied for &",
5571 Selector_Name, Compon);
5573 end if;
5574 end if;
5576 Next (Selector_Name);
5577 end loop;
5579 Next (Assoc);
5580 end loop;
5582 return Expr;
5583 end Get_Value;
5585 -----------------------
5586 -- Resolve_Aggr_Expr --
5587 -----------------------
5589 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Entity_Id) is
5590 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean;
5591 -- If the expression is an aggregate (possibly qualified) then its
5592 -- expansion is delayed until the enclosing aggregate is expanded
5593 -- into assignments. In that case, do not generate checks on the
5594 -- expression, because they will be generated later, and will other-
5595 -- wise force a copy (to remove side effects) that would leave a
5596 -- dynamic-sized aggregate in the code, something that gigi cannot
5597 -- handle.
5599 ---------------------------
5600 -- Has_Expansion_Delayed --
5601 ---------------------------
5603 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean is
5604 begin
5605 return
5606 (Nkind (Expr) in N_Aggregate | N_Extension_Aggregate
5607 and then Present (Etype (Expr))
5608 and then Is_Record_Type (Etype (Expr))
5609 and then Expansion_Delayed (Expr))
5610 or else
5611 (Nkind (Expr) = N_Qualified_Expression
5612 and then Has_Expansion_Delayed (Expression (Expr)));
5613 end Has_Expansion_Delayed;
5615 -- Local variables
5617 Expr_Type : Entity_Id := Empty;
5618 New_C : Entity_Id := Component;
5619 New_Expr : Node_Id;
5621 Relocate : Boolean;
5622 -- Set to True if the resolved Expr node needs to be relocated when
5623 -- attached to the newly created association list. This node need not
5624 -- be relocated if its parent pointer is not set. In fact in this
5625 -- case Expr is the output of a New_Copy_Tree call. If Relocate is
5626 -- True then we have analyzed the expression node in the original
5627 -- aggregate and hence it needs to be relocated when moved over to
5628 -- the new association list.
5630 -- Start of processing for Resolve_Aggr_Expr
5632 begin
5633 -- If the type of the component is elementary or the type of the
5634 -- aggregate does not contain discriminants, use the type of the
5635 -- component to resolve Expr.
5637 if Is_Elementary_Type (Etype (Component))
5638 or else not Has_Discriminants (Etype (N))
5639 then
5640 Expr_Type := Etype (Component);
5642 -- Otherwise we have to pick up the new type of the component from
5643 -- the new constrained subtype of the aggregate. In fact components
5644 -- which are of a composite type might be constrained by a
5645 -- discriminant, and we want to resolve Expr against the subtype were
5646 -- all discriminant occurrences are replaced with their actual value.
5648 else
5649 New_C := First_Component (Etype (N));
5650 while Present (New_C) loop
5651 if Chars (New_C) = Chars (Component) then
5652 Expr_Type := Etype (New_C);
5653 exit;
5654 end if;
5656 Next_Component (New_C);
5657 end loop;
5659 pragma Assert (Present (Expr_Type));
5661 -- For each range in an array type where a discriminant has been
5662 -- replaced with the constraint, check that this range is within
5663 -- the range of the base type. This checks is done in the init
5664 -- proc for regular objects, but has to be done here for
5665 -- aggregates since no init proc is called for them.
5667 if Is_Array_Type (Expr_Type) then
5668 declare
5669 Index : Node_Id;
5670 -- Range of the current constrained index in the array
5672 Orig_Index : Node_Id := First_Index (Etype (Component));
5673 -- Range corresponding to the range Index above in the
5674 -- original unconstrained record type. The bounds of this
5675 -- range may be governed by discriminants.
5677 Unconstr_Index : Node_Id := First_Index (Etype (Expr_Type));
5678 -- Range corresponding to the range Index above for the
5679 -- unconstrained array type. This range is needed to apply
5680 -- range checks.
5682 begin
5683 Index := First_Index (Expr_Type);
5684 while Present (Index) loop
5685 if Depends_On_Discriminant (Orig_Index) then
5686 Apply_Range_Check (Index, Etype (Unconstr_Index));
5687 end if;
5689 Next_Index (Index);
5690 Next_Index (Orig_Index);
5691 Next_Index (Unconstr_Index);
5692 end loop;
5693 end;
5694 end if;
5695 end if;
5697 -- If the Parent pointer of Expr is not set, Expr is an expression
5698 -- duplicated by New_Tree_Copy (this happens for record aggregates
5699 -- that look like (Field1 | Filed2 => Expr) or (others => Expr)).
5700 -- Such a duplicated expression must be attached to the tree
5701 -- before analysis and resolution to enforce the rule that a tree
5702 -- fragment should never be analyzed or resolved unless it is
5703 -- attached to the current compilation unit.
5705 if No (Parent (Expr)) then
5706 Set_Parent (Expr, N);
5707 Relocate := False;
5708 else
5709 Relocate := True;
5710 end if;
5712 -- Obtain the corresponding mutably tagged types if we are looking
5713 -- at a special internally generated class-wide equivalent type.
5715 Expr_Type :=
5716 Get_Corresponding_Mutably_Tagged_Type_If_Present (Expr_Type);
5718 Analyze_And_Resolve (Expr, Expr_Type);
5719 Check_Expr_OK_In_Limited_Aggregate (Expr);
5720 Check_Non_Static_Context (Expr);
5721 Check_Unset_Reference (Expr);
5723 -- Check wrong use of class-wide types
5725 if Is_Class_Wide_Type (Etype (Expr))
5726 and then not Is_Mutably_Tagged_Type (Expr_Type)
5727 then
5728 Error_Msg_N ("dynamically tagged expression not allowed", Expr);
5729 end if;
5731 if not Has_Expansion_Delayed (Expr) then
5732 Aggregate_Constraint_Checks (Expr, Expr_Type);
5733 end if;
5735 -- If an aggregate component has a type with predicates, an explicit
5736 -- predicate check must be applied, as for an assignment statement,
5737 -- because the aggregate might not be expanded into individual
5738 -- component assignments.
5740 if Has_Predicates (Expr_Type)
5741 and then Analyzed (Expr)
5742 then
5743 Apply_Predicate_Check (Expr, Expr_Type);
5744 end if;
5746 if Raises_Constraint_Error (Expr) then
5747 Set_Raises_Constraint_Error (N);
5748 end if;
5750 -- If the expression has been marked as requiring a range check, then
5751 -- generate it here. It's a bit odd to be generating such checks in
5752 -- the analyzer, but harmless since Generate_Range_Check does nothing
5753 -- (other than making sure Do_Range_Check is set) if the expander is
5754 -- not active.
5756 if Do_Range_Check (Expr) then
5757 Generate_Range_Check (Expr, Expr_Type, CE_Range_Check_Failed);
5758 end if;
5760 -- Add association Component => Expr if the caller requests it
5762 if Relocate then
5763 New_Expr := Relocate_Node (Expr);
5765 -- Since New_Expr is not gonna be analyzed later on, we need to
5766 -- propagate here the dimensions form Expr to New_Expr.
5768 Copy_Dimensions (Expr, New_Expr);
5770 else
5771 New_Expr := Expr;
5772 end if;
5774 Add_Association (New_C, New_Expr, New_Assoc_List);
5775 end Resolve_Aggr_Expr;
5777 -------------------
5778 -- Rewrite_Range --
5779 -------------------
5781 procedure Rewrite_Range (Root_Type : Entity_Id; Rge : Node_Id) is
5782 procedure Rewrite_Bound
5783 (Bound : Node_Id;
5784 Disc : Entity_Id;
5785 Expr_Disc : Node_Id);
5786 -- Rewrite a bound of the range Bound, when it is equal to the
5787 -- non-stored discriminant Disc, into the stored discriminant
5788 -- value Expr_Disc.
5790 -------------------
5791 -- Rewrite_Bound --
5792 -------------------
5794 procedure Rewrite_Bound
5795 (Bound : Node_Id;
5796 Disc : Entity_Id;
5797 Expr_Disc : Node_Id)
5799 begin
5800 if Nkind (Bound) /= N_Identifier then
5801 return;
5802 end if;
5804 -- We expect either the discriminant or the discriminal
5806 if Entity (Bound) = Disc
5807 or else (Ekind (Entity (Bound)) = E_In_Parameter
5808 and then Discriminal_Link (Entity (Bound)) = Disc)
5809 then
5810 Rewrite (Bound, New_Copy_Tree (Expr_Disc));
5811 end if;
5812 end Rewrite_Bound;
5814 -- Local variables
5816 Low, High : Node_Id;
5817 Disc : Entity_Id;
5818 Expr_Disc : Elmt_Id;
5820 -- Start of processing for Rewrite_Range
5822 begin
5823 if Has_Discriminants (Root_Type) and then Nkind (Rge) = N_Range then
5824 Low := Low_Bound (Rge);
5825 High := High_Bound (Rge);
5827 Disc := First_Discriminant (Root_Type);
5828 Expr_Disc := First_Elmt (Stored_Constraint (Etype (N)));
5829 while Present (Disc) loop
5830 Rewrite_Bound (Low, Disc, Node (Expr_Disc));
5831 Rewrite_Bound (High, Disc, Node (Expr_Disc));
5832 Next_Discriminant (Disc);
5833 Next_Elmt (Expr_Disc);
5834 end loop;
5835 end if;
5836 end Rewrite_Range;
5838 -- Local variables
5840 Components : constant Elist_Id := New_Elmt_List;
5841 -- Components is the list of the record components whose value must be
5842 -- provided in the aggregate. This list does include discriminants.
5844 Component : Entity_Id;
5845 Component_Elmt : Elmt_Id;
5846 Expr : Node_Id;
5847 Positional_Expr : Node_Id;
5849 -- Start of processing for Resolve_Record_Aggregate
5851 begin
5852 -- A record aggregate is restricted in SPARK:
5854 -- Each named association can have only a single choice.
5855 -- OTHERS cannot be used.
5856 -- Positional and named associations cannot be mixed.
5858 if Present (Component_Associations (N)) then
5859 declare
5860 Assoc : Node_Id;
5862 begin
5863 Assoc := First (Component_Associations (N));
5864 while Present (Assoc) loop
5865 if Nkind (Assoc) = N_Iterated_Component_Association then
5866 Error_Msg_N
5867 ("iterated component association can only appear in an "
5868 & "array aggregate", N);
5869 raise Unrecoverable_Error;
5870 end if;
5872 Next (Assoc);
5873 end loop;
5874 end;
5875 end if;
5877 -- We may end up calling Duplicate_Subexpr on expressions that are
5878 -- attached to New_Assoc_List. For this reason we need to attach it
5879 -- to the tree by setting its parent pointer to N. This parent point
5880 -- will change in STEP 8 below.
5882 Set_Parent (New_Assoc_List, N);
5884 -- STEP 1: abstract type and null record verification
5886 if Is_Abstract_Type (Typ) then
5887 Error_Msg_N ("type of aggregate cannot be abstract", N);
5888 end if;
5890 if No (First_Entity (Typ)) and then Null_Record_Present (N) then
5891 Set_Etype (N, Typ);
5892 return;
5894 elsif Present (First_Entity (Typ))
5895 and then Null_Record_Present (N)
5896 and then not Is_Tagged_Type (Typ)
5897 then
5898 Error_Msg_N ("record aggregate cannot be null", N);
5899 return;
5901 -- If the type has no components, then the aggregate should either
5902 -- have "null record", or in Ada 2005 it could instead have a single
5903 -- component association given by "others => <>". For Ada 95 we flag an
5904 -- error at this point, but for Ada 2005 we proceed with checking the
5905 -- associations below, which will catch the case where it's not an
5906 -- aggregate with "others => <>". Note that the legality of a <>
5907 -- aggregate for a null record type was established by AI05-016.
5909 elsif No (First_Entity (Typ))
5910 and then Ada_Version < Ada_2005
5911 then
5912 Error_Msg_N ("record aggregate must be null", N);
5913 return;
5914 end if;
5916 -- STEP 2: Verify aggregate structure
5918 Step_2 : declare
5919 Assoc : Node_Id;
5920 Bad_Aggregate : Boolean := False;
5921 Selector_Name : Node_Id;
5923 begin
5924 if Present (Component_Associations (N)) then
5925 Assoc := First (Component_Associations (N));
5926 else
5927 Assoc := Empty;
5928 end if;
5930 while Present (Assoc) loop
5931 Selector_Name := First (Choices (Assoc));
5932 while Present (Selector_Name) loop
5933 if Nkind (Selector_Name) = N_Identifier then
5934 null;
5936 elsif Nkind (Selector_Name) = N_Others_Choice then
5937 if Selector_Name /= First (Choices (Assoc))
5938 or else Present (Next (Selector_Name))
5939 then
5940 Error_Msg_N
5941 ("OTHERS must appear alone in a choice list",
5942 Selector_Name);
5943 return;
5945 elsif Present (Next (Assoc)) then
5946 Error_Msg_N
5947 ("OTHERS must appear last in an aggregate",
5948 Selector_Name);
5949 return;
5951 -- (Ada 2005): If this is an association with a box,
5952 -- indicate that the association need not represent
5953 -- any component.
5955 elsif Box_Present (Assoc) then
5956 Others_Box := 1;
5957 Box_Node := Assoc;
5958 end if;
5960 else
5961 Error_Msg_N
5962 ("selector name should be identifier or OTHERS",
5963 Selector_Name);
5964 Bad_Aggregate := True;
5965 end if;
5967 Next (Selector_Name);
5968 end loop;
5970 Next (Assoc);
5971 end loop;
5973 if Bad_Aggregate then
5974 return;
5975 end if;
5976 end Step_2;
5978 -- STEP 3: Find discriminant Values
5980 Step_3 : declare
5981 Discrim : Entity_Id;
5982 Missing_Discriminants : Boolean := False;
5984 begin
5985 if Present (Expressions (N)) then
5986 Positional_Expr := First (Expressions (N));
5987 else
5988 Positional_Expr := Empty;
5989 end if;
5991 -- AI05-0115: if the ancestor part is a subtype mark, the ancestor
5992 -- must not have unknown discriminants.
5993 -- ??? We are not checking any subtype mark here and this code is not
5994 -- exercised by any test, so it's likely wrong (in particular
5995 -- we should not use Root_Type here but the subtype mark, if any),
5996 -- and possibly not needed.
5998 if Is_Derived_Type (Typ)
5999 and then Has_Unknown_Discriminants (Root_Type (Typ))
6000 and then Nkind (N) /= N_Extension_Aggregate
6001 then
6002 Error_Msg_NE
6003 ("aggregate not available for type& whose ancestor "
6004 & "has unknown discriminants", N, Typ);
6005 end if;
6007 if Has_Unknown_Discriminants (Typ)
6008 and then Present (Underlying_Record_View (Typ))
6009 then
6010 Discrim := First_Discriminant (Underlying_Record_View (Typ));
6011 elsif Has_Discriminants (Typ) then
6012 Discrim := First_Discriminant (Typ);
6013 else
6014 Discrim := Empty;
6015 end if;
6017 -- First find the discriminant values in the positional components
6019 while Present (Discrim) and then Present (Positional_Expr) loop
6020 if Discriminant_Present (Discrim) then
6021 Resolve_Aggr_Expr (Positional_Expr, Discrim);
6023 -- Ada 2005 (AI-231)
6025 if Ada_Version >= Ada_2005
6026 and then Known_Null (Positional_Expr)
6027 then
6028 Check_Can_Never_Be_Null (Discrim, Positional_Expr);
6029 end if;
6031 Next (Positional_Expr);
6032 end if;
6034 if Present (Get_Value (Discrim, Component_Associations (N))) then
6035 Error_Msg_NE
6036 ("more than one value supplied for discriminant&",
6037 N, Discrim);
6038 end if;
6040 Next_Discriminant (Discrim);
6041 end loop;
6043 -- Find remaining discriminant values if any among named components
6045 while Present (Discrim) loop
6046 Expr := Get_Value (Discrim, Component_Associations (N), True);
6048 if not Discriminant_Present (Discrim) then
6049 if Present (Expr) then
6050 Error_Msg_NE
6051 ("more than one value supplied for discriminant &",
6052 N, Discrim);
6053 end if;
6055 elsif No (Expr) then
6056 Error_Msg_NE
6057 ("no value supplied for discriminant &", N, Discrim);
6058 Missing_Discriminants := True;
6060 else
6061 Resolve_Aggr_Expr (Expr, Discrim);
6062 end if;
6064 Next_Discriminant (Discrim);
6065 end loop;
6067 if Missing_Discriminants then
6068 return;
6069 end if;
6071 -- At this point and until the beginning of STEP 6, New_Assoc_List
6072 -- contains only the discriminants and their values.
6074 end Step_3;
6076 -- STEP 4: Set the Etype of the record aggregate
6078 if Has_Discriminants (Typ)
6079 or else (Has_Unknown_Discriminants (Typ)
6080 and then Present (Underlying_Record_View (Typ)))
6081 then
6082 Build_Constrained_Itype (N, Typ, New_Assoc_List);
6083 else
6084 Set_Etype (N, Typ);
6085 end if;
6087 -- STEP 5: Get remaining components according to discriminant values
6089 Step_5 : declare
6090 Dnode : Node_Id;
6091 Errors_Found : Boolean := False;
6092 Record_Def : Node_Id;
6093 Parent_Typ : Entity_Id;
6094 Parent_Typ_List : Elist_Id;
6095 Parent_Elmt : Elmt_Id;
6096 Root_Typ : Entity_Id;
6098 begin
6099 if Is_Derived_Type (Typ) and then Is_Tagged_Type (Typ) then
6100 Parent_Typ_List := New_Elmt_List;
6102 -- If this is an extension aggregate, the component list must
6103 -- include all components that are not in the given ancestor type.
6104 -- Otherwise, the component list must include components of all
6105 -- ancestors, starting with the root.
6107 if Nkind (N) = N_Extension_Aggregate then
6108 Root_Typ := Base_Type (Etype (Ancestor_Part (N)));
6110 else
6111 -- AI05-0115: check legality of aggregate for type with a
6112 -- private ancestor.
6114 Root_Typ := Root_Type (Typ);
6115 if Has_Private_Ancestor (Typ) then
6116 declare
6117 Ancestor : constant Entity_Id :=
6118 Find_Private_Ancestor (Typ);
6119 Ancestor_Unit : constant Entity_Id :=
6120 Cunit_Entity
6121 (Get_Source_Unit (Ancestor));
6122 Parent_Unit : constant Entity_Id :=
6123 Cunit_Entity (Get_Source_Unit
6124 (Base_Type (Etype (Ancestor))));
6125 begin
6126 -- Check whether we are in a scope that has full view
6127 -- over the private ancestor and its parent. This can
6128 -- only happen if the derivation takes place in a child
6129 -- unit of the unit that declares the parent, and we are
6130 -- in the private part or body of that child unit, else
6131 -- the aggregate is illegal.
6133 if Is_Child_Unit (Ancestor_Unit)
6134 and then Scope (Ancestor_Unit) = Parent_Unit
6135 and then In_Open_Scopes (Scope (Ancestor))
6136 and then
6137 (In_Private_Part (Scope (Ancestor))
6138 or else In_Package_Body (Scope (Ancestor)))
6139 then
6140 null;
6142 else
6143 Error_Msg_NE
6144 ("type of aggregate has private ancestor&!",
6145 N, Root_Typ);
6146 Error_Msg_N ("must use extension aggregate!", N);
6147 return;
6148 end if;
6149 end;
6150 end if;
6152 Dnode := Declaration_Node (Base_Type (Root_Typ));
6154 -- If we don't get a full declaration, then we have some error
6155 -- which will get signalled later so skip this part. Otherwise
6156 -- gather components of root that apply to the aggregate type.
6157 -- We use the base type in case there is an applicable stored
6158 -- constraint that renames the discriminants of the root.
6160 if Nkind (Dnode) = N_Full_Type_Declaration then
6161 Record_Def := Type_Definition (Dnode);
6162 Gather_Components
6163 (Base_Type (Typ),
6164 Component_List (Record_Def),
6165 Governed_By => New_Assoc_List,
6166 Into => Components,
6167 Report_Errors => Errors_Found);
6169 if Errors_Found then
6170 Error_Msg_N
6171 ("discriminant controlling variant part is not static",
6173 return;
6174 end if;
6175 end if;
6176 end if;
6178 Parent_Typ := Base_Type (Typ);
6179 while Parent_Typ /= Root_Typ loop
6180 Prepend_Elmt (Parent_Typ, To => Parent_Typ_List);
6181 Parent_Typ := Etype (Parent_Typ);
6183 -- Check whether a private parent requires the use of
6184 -- an extension aggregate.
6186 if Nkind (Parent (Base_Type (Parent_Typ))) =
6187 N_Private_Type_Declaration
6188 or else Nkind (Parent (Base_Type (Parent_Typ))) =
6189 N_Private_Extension_Declaration
6190 then
6191 if Nkind (N) /= N_Extension_Aggregate then
6192 Error_Msg_NE
6193 ("type of aggregate has private ancestor&!",
6194 N, Parent_Typ);
6195 Error_Msg_N ("must use extension aggregate!", N);
6196 return;
6198 elsif Parent_Typ /= Root_Typ then
6199 Error_Msg_NE
6200 ("ancestor part of aggregate must be private type&",
6201 Ancestor_Part (N), Parent_Typ);
6202 return;
6203 end if;
6205 -- The current view of ancestor part may be a private type,
6206 -- while the context type is always non-private.
6208 elsif Is_Private_Type (Root_Typ)
6209 and then Present (Full_View (Root_Typ))
6210 and then Nkind (N) = N_Extension_Aggregate
6211 then
6212 exit when Base_Type (Full_View (Root_Typ)) = Parent_Typ;
6213 end if;
6214 end loop;
6216 -- Now collect components from all other ancestors, beginning
6217 -- with the current type. If the type has unknown discriminants
6218 -- use the component list of the Underlying_Record_View, which
6219 -- needs to be used for the subsequent expansion of the aggregate
6220 -- into assignments.
6222 Parent_Elmt := First_Elmt (Parent_Typ_List);
6223 while Present (Parent_Elmt) loop
6224 Parent_Typ := Node (Parent_Elmt);
6226 if Has_Unknown_Discriminants (Parent_Typ)
6227 and then Present (Underlying_Record_View (Typ))
6228 then
6229 Parent_Typ := Underlying_Record_View (Parent_Typ);
6230 end if;
6232 Record_Def := Type_Definition (Parent (Base_Type (Parent_Typ)));
6233 Gather_Components (Parent_Typ,
6234 Component_List (Record_Extension_Part (Record_Def)),
6235 Governed_By => New_Assoc_List,
6236 Into => Components,
6237 Report_Errors => Errors_Found);
6239 Next_Elmt (Parent_Elmt);
6240 end loop;
6242 -- Typ is not a derived tagged type
6244 else
6245 Record_Def := Type_Definition (Parent (Base_Type (Typ)));
6247 if Null_Present (Record_Def) then
6248 null;
6250 elsif not Has_Unknown_Discriminants (Typ) then
6251 Gather_Components
6252 (Base_Type (Typ),
6253 Component_List (Record_Def),
6254 Governed_By => New_Assoc_List,
6255 Into => Components,
6256 Report_Errors => Errors_Found);
6258 else
6259 Gather_Components
6260 (Base_Type (Underlying_Record_View (Typ)),
6261 Component_List (Record_Def),
6262 Governed_By => New_Assoc_List,
6263 Into => Components,
6264 Report_Errors => Errors_Found);
6265 end if;
6266 end if;
6268 if Errors_Found then
6269 return;
6270 end if;
6271 end Step_5;
6273 -- STEP 6: Find component Values
6275 Component_Elmt := First_Elmt (Components);
6277 -- First scan the remaining positional associations in the aggregate.
6278 -- Remember that at this point Positional_Expr contains the current
6279 -- positional association if any is left after looking for discriminant
6280 -- values in step 3.
6282 while Present (Positional_Expr) and then Present (Component_Elmt) loop
6283 Component := Node (Component_Elmt);
6284 Resolve_Aggr_Expr (Positional_Expr, Component);
6286 -- Ada 2005 (AI-231)
6288 if Ada_Version >= Ada_2005 and then Known_Null (Positional_Expr) then
6289 Check_Can_Never_Be_Null (Component, Positional_Expr);
6290 end if;
6292 if Present (Get_Value (Component, Component_Associations (N))) then
6293 Error_Msg_NE
6294 ("more than one value supplied for component &", N, Component);
6295 end if;
6297 Next (Positional_Expr);
6298 Next_Elmt (Component_Elmt);
6299 end loop;
6301 if Present (Positional_Expr) then
6302 Error_Msg_N
6303 ("too many components for record aggregate", Positional_Expr);
6304 end if;
6306 -- Now scan for the named arguments of the aggregate
6308 while Present (Component_Elmt) loop
6309 Component := Node (Component_Elmt);
6310 Expr := Get_Value (Component, Component_Associations (N), True);
6312 -- Note: The previous call to Get_Value sets the value of the
6313 -- variable Is_Box_Present.
6315 -- Ada 2005 (AI-287): Handle components with default initialization.
6316 -- Note: This feature was originally added to Ada 2005 for limited
6317 -- but it was finally allowed with any type.
6319 if Is_Box_Present then
6320 Check_Box_Component : declare
6321 Ctyp : constant Entity_Id := Etype (Component);
6323 begin
6324 -- Initially assume that the box is for a default-initialized
6325 -- component and reset to False in cases where that's not true.
6327 Is_Box_Init_By_Default := True;
6329 -- If there is a default expression for the aggregate, copy
6330 -- it into a new association. This copy must modify the scopes
6331 -- of internal types that may be attached to the expression
6332 -- (e.g. index subtypes of arrays) because in general the type
6333 -- declaration and the aggregate appear in different scopes,
6334 -- and the backend requires the scope of the type to match the
6335 -- point at which it is elaborated.
6337 -- If the component has an initialization procedure (IP) we
6338 -- pass the component to the expander, which will generate
6339 -- the call to such IP.
6341 -- If the component has discriminants, their values must
6342 -- be taken from their subtype. This is indispensable for
6343 -- constraints that are given by the current instance of an
6344 -- enclosing type, to allow the expansion of the aggregate to
6345 -- replace the reference to the current instance by the target
6346 -- object of the aggregate.
6348 if Is_Case_Choice_Pattern (N) then
6350 -- Do not transform box component values in a case-choice
6351 -- aggregate.
6353 Add_Association
6354 (Component => Component,
6355 Expr => Empty,
6356 Assoc_List => New_Assoc_List,
6357 Is_Box_Present => True);
6359 elsif Present (Parent (Component))
6360 and then Nkind (Parent (Component)) = N_Component_Declaration
6361 and then Present (Expression (Parent (Component)))
6362 then
6363 -- If component declaration has an initialization expression
6364 -- then this is not a case of default initialization.
6366 Is_Box_Init_By_Default := False;
6368 Expr :=
6369 New_Copy_Tree_And_Copy_Dimensions
6370 (Expression (Parent (Component)),
6371 New_Scope => Current_Scope,
6372 New_Sloc => Sloc (N));
6374 -- As the type of the copied default expression may refer
6375 -- to discriminants of the record type declaration, these
6376 -- non-stored discriminants need to be rewritten into stored
6377 -- discriminant values for the aggregate. This is required
6378 -- in GNATprove mode, and is adopted in all modes to avoid
6379 -- special-casing GNATprove mode.
6381 if Is_Array_Type (Etype (Expr)) then
6382 declare
6383 Rec_Typ : constant Entity_Id := Scope (Component);
6384 -- Root record type whose discriminants may be used as
6385 -- bounds in range nodes.
6387 Assoc : Node_Id;
6388 Choice : Node_Id;
6389 Index : Node_Id;
6391 begin
6392 -- Rewrite the range nodes occurring in the indexes
6393 -- and their types.
6395 Index := First_Index (Etype (Expr));
6396 while Present (Index) loop
6397 Rewrite_Range (Rec_Typ, Index);
6398 Rewrite_Range
6399 (Rec_Typ, Scalar_Range (Etype (Index)));
6401 Next_Index (Index);
6402 end loop;
6404 -- Rewrite the range nodes occurring as aggregate
6405 -- bounds and component associations.
6407 if Nkind (Expr) = N_Aggregate then
6408 if Present (Aggregate_Bounds (Expr)) then
6409 Rewrite_Range (Rec_Typ, Aggregate_Bounds (Expr));
6410 end if;
6412 if Present (Component_Associations (Expr)) then
6413 Assoc := First (Component_Associations (Expr));
6414 while Present (Assoc) loop
6415 Choice := First (Choices (Assoc));
6416 while Present (Choice) loop
6417 Rewrite_Range (Rec_Typ, Choice);
6419 Next (Choice);
6420 end loop;
6422 Next (Assoc);
6423 end loop;
6424 end if;
6425 end if;
6426 end;
6427 end if;
6429 Add_Association
6430 (Component => Component,
6431 Expr => Expr,
6432 Assoc_List => New_Assoc_List);
6433 Set_Has_Self_Reference (N);
6435 elsif Needs_Simple_Initialization (Ctyp)
6436 or else Has_Non_Null_Base_Init_Proc (Ctyp)
6437 or else not Expander_Active
6438 then
6439 Add_Association
6440 (Component => Component,
6441 Expr => Empty,
6442 Assoc_List => New_Assoc_List,
6443 Is_Box_Present => True);
6445 -- Otherwise we only need to resolve the expression if the
6446 -- component has partially initialized values (required to
6447 -- expand the corresponding assignments and run-time checks).
6449 elsif Present (Expr)
6450 and then Is_Partially_Initialized_Type (Ctyp)
6451 then
6452 Resolve_Aggr_Expr (Expr, Component);
6453 end if;
6454 end Check_Box_Component;
6456 elsif No (Expr) then
6458 -- Ignore hidden components associated with the position of the
6459 -- interface tags: these are initialized dynamically.
6461 if No (Related_Type (Component)) then
6462 Error_Msg_NE
6463 ("no value supplied for component &!", N, Component);
6464 end if;
6466 else
6467 Resolve_Aggr_Expr (Expr, Component);
6468 end if;
6470 Next_Elmt (Component_Elmt);
6471 end loop;
6473 -- STEP 7: check for invalid components + check type in choice list
6475 Step_7 : declare
6476 Assoc : Node_Id;
6477 New_Assoc : Node_Id;
6479 Selectr : Node_Id;
6480 -- Selector name
6482 Typech : Entity_Id;
6483 -- Type of first component in choice list
6485 begin
6486 if Present (Component_Associations (N)) then
6487 Assoc := First (Component_Associations (N));
6488 else
6489 Assoc := Empty;
6490 end if;
6492 Verification : while Present (Assoc) loop
6493 Selectr := First (Choices (Assoc));
6494 Typech := Empty;
6496 if Nkind (Selectr) = N_Others_Choice then
6498 -- Ada 2005 (AI-287): others choice may have expression or box
6500 if No (Others_Etype) and then Others_Box = 0 then
6501 Error_Msg_N
6502 ("OTHERS must represent at least one component", Selectr);
6504 elsif Others_Box = 1 and then Warn_On_Redundant_Constructs then
6505 Error_Msg_N ("OTHERS choice is redundant?r?", Box_Node);
6506 Error_Msg_N
6507 ("\previous choices cover all components?r?", Box_Node);
6508 end if;
6510 exit Verification;
6511 end if;
6513 while Present (Selectr) loop
6514 Component := Empty;
6515 New_Assoc := First (New_Assoc_List);
6516 while Present (New_Assoc) loop
6517 Component := First (Choices (New_Assoc));
6519 if Chars (Selectr) = Chars (Component) then
6520 if Style_Check then
6521 Check_Identifier (Selectr, Entity (Component));
6522 end if;
6524 exit;
6525 end if;
6527 Next (New_Assoc);
6528 end loop;
6530 -- If we found an association, then this is a legal component
6531 -- of the type in question.
6533 pragma Assert (if Present (New_Assoc) then Present (Component));
6535 -- If no association, this is not a legal component of the type
6536 -- in question, unless its association is provided with a box.
6538 if No (New_Assoc) then
6539 if Box_Present (Parent (Selectr)) then
6541 -- This may still be a bogus component with a box. Scan
6542 -- list of components to verify that a component with
6543 -- that name exists.
6545 declare
6546 C : Entity_Id;
6548 begin
6549 C := First_Component (Typ);
6550 while Present (C) loop
6551 if Chars (C) = Chars (Selectr) then
6553 -- If the context is an extension aggregate,
6554 -- the component must not be inherited from
6555 -- the ancestor part of the aggregate.
6557 if Nkind (N) /= N_Extension_Aggregate
6558 or else
6559 Scope (Original_Record_Component (C)) /=
6560 Etype (Ancestor_Part (N))
6561 then
6562 exit;
6563 end if;
6564 end if;
6566 Next_Component (C);
6567 end loop;
6569 if No (C) then
6570 Error_Msg_Node_2 := Typ;
6571 Error_Msg_N ("& is not a component of}", Selectr);
6572 end if;
6573 end;
6575 elsif Chars (Selectr) /= Name_uTag
6576 and then Chars (Selectr) /= Name_uParent
6577 then
6578 if not Has_Discriminants (Typ) then
6579 Error_Msg_Node_2 := Typ;
6580 Error_Msg_N ("& is not a component of}", Selectr);
6581 else
6582 Error_Msg_N
6583 ("& is not a component of the aggregate subtype",
6584 Selectr);
6585 end if;
6587 Check_Misspelled_Component (Components, Selectr);
6588 end if;
6590 elsif No (Typech) then
6591 Typech := Base_Type (Etype (Component));
6593 -- AI05-0199: In Ada 2012, several components of anonymous
6594 -- access types can appear in a choice list, as long as the
6595 -- designated types match.
6597 elsif Typech /= Base_Type (Etype (Component)) then
6598 if Ada_Version >= Ada_2012
6599 and then Ekind (Typech) = E_Anonymous_Access_Type
6600 and then
6601 Ekind (Etype (Component)) = E_Anonymous_Access_Type
6602 and then Base_Type (Designated_Type (Typech)) =
6603 Base_Type (Designated_Type (Etype (Component)))
6604 and then
6605 Subtypes_Statically_Match (Typech, (Etype (Component)))
6606 then
6607 null;
6609 elsif not Box_Present (Parent (Selectr)) then
6610 Error_Msg_N
6611 ("components in choice list must have same type",
6612 Selectr);
6613 end if;
6614 end if;
6616 Next (Selectr);
6617 end loop;
6619 Next (Assoc);
6620 end loop Verification;
6621 end Step_7;
6623 -- STEP 8: replace the original aggregate
6625 Step_8 : declare
6626 New_Aggregate : constant Node_Id := New_Copy (N);
6628 begin
6629 Set_Expressions (New_Aggregate, No_List);
6630 Set_Etype (New_Aggregate, Etype (N));
6631 Set_Component_Associations (New_Aggregate, New_Assoc_List);
6632 Set_Check_Actuals (New_Aggregate, Check_Actuals (N));
6634 Rewrite (N, New_Aggregate);
6635 end Step_8;
6637 -- Check the dimensions of the components in the record aggregate
6639 Analyze_Dimension_Extension_Or_Record_Aggregate (N);
6640 end Resolve_Record_Aggregate;
6642 -----------------------------
6643 -- Check_Can_Never_Be_Null --
6644 -----------------------------
6646 procedure Check_Can_Never_Be_Null (Typ : Entity_Id; Expr : Node_Id) is
6647 Comp_Typ : Entity_Id;
6649 begin
6650 pragma Assert
6651 (Ada_Version >= Ada_2005
6652 and then Present (Expr)
6653 and then Known_Null (Expr));
6655 case Ekind (Typ) is
6656 when E_Array_Type =>
6657 Comp_Typ := Component_Type (Typ);
6659 when E_Component
6660 | E_Discriminant
6662 Comp_Typ := Etype (Typ);
6664 when others =>
6665 return;
6666 end case;
6668 if Can_Never_Be_Null (Comp_Typ) then
6670 -- Here we know we have a constraint error. Note that we do not use
6671 -- Apply_Compile_Time_Constraint_Error here to the Expr, which might
6672 -- seem the more natural approach. That's because in some cases the
6673 -- components are rewritten, and the replacement would be missed.
6674 -- We do not mark the whole aggregate as raising a constraint error,
6675 -- because the association may be a null array range.
6677 Error_Msg_N
6678 ("(Ada 2005) NULL not allowed in null-excluding component??", Expr);
6679 Error_Msg_N
6680 ("\Constraint_Error will be raised at run time??", Expr);
6682 Rewrite (Expr,
6683 Make_Raise_Constraint_Error
6684 (Sloc (Expr), Reason => CE_Access_Check_Failed));
6685 Set_Etype (Expr, Comp_Typ);
6686 Set_Analyzed (Expr);
6687 end if;
6688 end Check_Can_Never_Be_Null;
6690 ---------------------
6691 -- Sort_Case_Table --
6692 ---------------------
6694 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is
6695 U : constant Int := Case_Table'Last;
6696 K : Int;
6697 J : Int;
6698 T : Case_Bounds;
6700 begin
6701 K := 1;
6702 while K < U loop
6703 T := Case_Table (K + 1);
6705 J := K + 1;
6706 while J > 1
6707 and then Expr_Value (Case_Table (J - 1).Lo) > Expr_Value (T.Lo)
6708 loop
6709 Case_Table (J) := Case_Table (J - 1);
6710 J := J - 1;
6711 end loop;
6713 Case_Table (J) := T;
6714 K := K + 1;
6715 end loop;
6716 end Sort_Case_Table;
6718 end Sem_Aggr;