[PATCH v4 1/3] RISC-V: Add support for XCVelw extension in CV32E40P
[official-gcc.git] / gcc / ada / sem_aggr.adb
blobe1e7b8bac37e26af903979cc109fa1024c2d741d
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ A G G R --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2023, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Einfo; use Einfo;
30 with Einfo.Utils; use Einfo.Utils;
31 with Elists; use Elists;
32 with Errout; use Errout;
33 with Expander; use Expander;
34 with Exp_Ch6; use Exp_Ch6;
35 with Exp_Tss; use Exp_Tss;
36 with Exp_Util; use Exp_Util;
37 with Freeze; use Freeze;
38 with Itypes; use Itypes;
39 with Lib; use Lib;
40 with Lib.Xref; use Lib.Xref;
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.Nodes; use Sinfo.Nodes;
63 with Sinfo.Utils; use Sinfo.Utils;
64 with Snames; use Snames;
65 with Stringt; use Stringt;
66 with Stand; use Stand;
67 with Style; use Style;
68 with Targparm; use Targparm;
69 with Tbuild; use Tbuild;
70 with Ttypes; use Ttypes;
71 with Uintp; use Uintp;
72 with Warnsw; use Warnsw;
74 package body Sem_Aggr is
76 type Case_Bounds is record
77 Lo : Node_Id;
78 -- Low bound of choice. Once we sort the Case_Table, then entries
79 -- will be in order of ascending Choice_Lo values.
81 Hi : Node_Id;
82 -- High Bound of choice. The sort does not pay any attention to the
83 -- high bound, so choices 1 .. 4 and 1 .. 5 could be in either order.
85 Highest : Uint;
86 -- If there are duplicates or missing entries, then in the sorted
87 -- table, this records the highest value among Choice_Hi values
88 -- seen so far, including this entry.
90 Choice : Node_Id;
91 -- The node of the choice
92 end record;
94 type Case_Table_Type is array (Pos range <>) of Case_Bounds;
95 -- Table type used by Check_Case_Choices procedure
97 -----------------------
98 -- Local Subprograms --
99 -----------------------
101 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type);
102 -- Sort the Case Table using the Lower Bound of each Choice as the key. A
103 -- simple insertion sort is used since the choices in a case statement will
104 -- usually be in near sorted order.
106 procedure Check_Can_Never_Be_Null (Typ : Entity_Id; Expr : Node_Id);
107 -- Ada 2005 (AI-231): Check bad usage of null for a component for which
108 -- null exclusion (NOT NULL) is specified. Typ can be an E_Array_Type for
109 -- the array case (the component type of the array will be used) or an
110 -- E_Component/E_Discriminant entity in the record case, in which case the
111 -- type of the component will be used for the test. If Typ is any other
112 -- kind of entity, the call is ignored. Expr is the component node in the
113 -- aggregate which is known to have a null value. A warning message will be
114 -- issued if the component is null excluding.
116 -- It would be better to pass the proper type for Typ ???
118 procedure Check_Expr_OK_In_Limited_Aggregate (Expr : Node_Id);
119 -- Check that Expr is either not limited or else is one of the cases of
120 -- expressions allowed for a limited component association (namely, an
121 -- aggregate, function call, or <> notation). Report error for violations.
122 -- Expression is also OK in an instance or inlining context, because we
123 -- have already preanalyzed and it is known to be type correct.
125 ------------------------------------------------------
126 -- Subprograms used for RECORD AGGREGATE Processing --
127 ------------------------------------------------------
129 procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id);
130 -- This procedure performs all the semantic checks required for record
131 -- aggregates. Note that for aggregates analysis and resolution go
132 -- hand in hand. Aggregate analysis has been delayed up to here and
133 -- it is done while resolving the aggregate.
135 -- N is the N_Aggregate node.
136 -- Typ is the record type for the aggregate resolution
138 -- While performing the semantic checks, this procedure builds a new
139 -- Component_Association_List where each record field appears alone in a
140 -- Component_Choice_List along with its corresponding expression. The
141 -- record fields in the Component_Association_List appear in the same order
142 -- in which they appear in the record type Typ.
144 -- Once this new Component_Association_List is built and all the semantic
145 -- checks performed, the original aggregate subtree is replaced with the
146 -- new named record aggregate just built. This new record aggregate has no
147 -- positional associations, so its Expressions field is set to No_List.
148 -- Note that subtree substitution is performed with Rewrite so as to be
149 -- able to retrieve the original aggregate.
151 -- The aggregate subtree manipulation performed by Resolve_Record_Aggregate
152 -- yields the aggregate format expected by Gigi. Typically, this kind of
153 -- tree manipulations are done in the expander. However, because the
154 -- semantic checks that need to be performed on record aggregates really go
155 -- hand in hand with the record aggregate normalization, the aggregate
156 -- subtree transformation is performed during resolution rather than
157 -- expansion. Had we decided otherwise we would have had to duplicate most
158 -- of the code in the expansion procedure Expand_Record_Aggregate. Note,
159 -- however, that all the expansion concerning aggregates for tagged records
160 -- is done in Expand_Record_Aggregate.
162 -- The algorithm of Resolve_Record_Aggregate proceeds as follows:
164 -- 1. Make sure that the record type against which the record aggregate
165 -- has to be resolved is not abstract. Furthermore if the type is a
166 -- null aggregate make sure the input aggregate N is also null.
168 -- 2. Verify that the structure of the aggregate is that of a record
169 -- aggregate. Specifically, look for component associations and ensure
170 -- that each choice list only has identifiers or the N_Others_Choice
171 -- node. Also make sure that if present, the N_Others_Choice occurs
172 -- last and by itself.
174 -- 3. If Typ contains discriminants, the values for each discriminant is
175 -- looked for. If the record type Typ has variants, we check that the
176 -- expressions corresponding to each discriminant ruling the (possibly
177 -- nested) variant parts of Typ, are static. This allows us to determine
178 -- the variant parts to which the rest of the aggregate must conform.
179 -- The names of discriminants with their values are saved in a new
180 -- association list, New_Assoc_List which is later augmented with the
181 -- names and values of the remaining components in the record type.
183 -- During this phase we also make sure that every discriminant is
184 -- assigned exactly one value. Note that when several values for a given
185 -- discriminant are found, semantic processing continues looking for
186 -- further errors. In this case it's the first discriminant value found
187 -- which we will be recorded.
189 -- IMPORTANT NOTE: For derived tagged types this procedure expects
190 -- First_Discriminant and Next_Discriminant to give the correct list
191 -- of discriminants, in the correct order.
193 -- 4. After all the discriminant values have been gathered, we can set the
194 -- Etype of the record aggregate. If Typ contains no discriminants this
195 -- is straightforward: the Etype of N is just Typ, otherwise a new
196 -- implicit constrained subtype of Typ is built to be the Etype of N.
198 -- 5. Gather the remaining record components according to the discriminant
199 -- values. This involves recursively traversing the record type
200 -- structure to see what variants are selected by the given discriminant
201 -- values. This processing is a little more convoluted if Typ is a
202 -- derived tagged types since we need to retrieve the record structure
203 -- of all the ancestors of Typ.
205 -- 6. After gathering the record components we look for their values in the
206 -- record aggregate and emit appropriate error messages should we not
207 -- find such values or should they be duplicated.
209 -- 7. We then make sure no illegal component names appear in the record
210 -- aggregate and make sure that the type of the record components
211 -- appearing in a same choice list is the same. Finally we ensure that
212 -- the others choice, if present, is used to provide the value of at
213 -- least a record component.
215 -- 8. The original aggregate node is replaced with the new named aggregate
216 -- built in steps 3 through 6, as explained earlier.
218 -- Given the complexity of record aggregate resolution, the primary goal of
219 -- this routine is clarity and simplicity rather than execution and storage
220 -- efficiency. If there are only positional components in the aggregate the
221 -- running time is linear. If there are associations the running time is
222 -- still linear as long as the order of the associations is not too far off
223 -- the order of the components in the record type. If this is not the case
224 -- the running time is at worst quadratic in the size of the association
225 -- list.
227 procedure Check_Misspelled_Component
228 (Elements : Elist_Id;
229 Component : Node_Id);
230 -- Give possible misspelling diagnostic if Component is likely to be a
231 -- misspelling of one of the components of the Assoc_List. This is called
232 -- by Resolve_Aggr_Expr after producing an invalid component error message.
234 -----------------------------------------------------
235 -- Subprograms used for ARRAY AGGREGATE Processing --
236 -----------------------------------------------------
238 function Resolve_Array_Aggregate
239 (N : Node_Id;
240 Index : Node_Id;
241 Index_Constr : Node_Id;
242 Component_Typ : Entity_Id;
243 Others_Allowed : Boolean) return Boolean;
244 -- This procedure performs the semantic checks for an array aggregate.
245 -- True is returned if the aggregate resolution succeeds.
247 -- The procedure works by recursively checking each nested aggregate.
248 -- Specifically, after checking a sub-aggregate nested at the i-th level
249 -- we recursively check all the subaggregates at the i+1-st level (if any).
250 -- Note that aggregates analysis and resolution go hand in hand.
251 -- Aggregate analysis has been delayed up to here and it is done while
252 -- resolving the aggregate.
254 -- N is the current N_Aggregate node to be checked.
256 -- Index is the index node corresponding to the array sub-aggregate that
257 -- we are currently checking (RM 4.3.3 (8)). Its Etype is the
258 -- corresponding index type (or subtype).
260 -- Index_Constr is the node giving the applicable index constraint if
261 -- any (RM 4.3.3 (10)). It "is a constraint provided by certain
262 -- contexts [...] that can be used to determine the bounds of the array
263 -- value specified by the aggregate". If Others_Allowed below is False
264 -- there is no applicable index constraint and this node is set to Index.
266 -- Component_Typ is the array component type.
268 -- Others_Allowed indicates whether an others choice is allowed
269 -- in the context where the top-level aggregate appeared.
271 -- The algorithm of Resolve_Array_Aggregate proceeds as follows:
273 -- 1. Make sure that the others choice, if present, is by itself and
274 -- appears last in the sub-aggregate. Check that we do not have
275 -- positional and named components in the array sub-aggregate (unless
276 -- the named association is an others choice). Finally if an others
277 -- choice is present, make sure it is allowed in the aggregate context.
279 -- 2. If the array sub-aggregate contains discrete_choices:
281 -- (A) Verify their validity. Specifically verify that:
283 -- (a) If a null range is present it must be the only possible
284 -- choice in the array aggregate.
286 -- (b) Ditto for a non static range.
288 -- (c) Ditto for a non static expression.
290 -- In addition this step analyzes and resolves each discrete_choice,
291 -- making sure that its type is the type of the corresponding Index.
292 -- If we are not at the lowest array aggregate level (in the case of
293 -- multi-dimensional aggregates) then invoke Resolve_Array_Aggregate
294 -- recursively on each component expression. Otherwise, resolve the
295 -- bottom level component expressions against the expected component
296 -- type ONLY IF the component corresponds to a single discrete choice
297 -- which is not an others choice (to see why read the DELAYED
298 -- COMPONENT RESOLUTION below).
300 -- (B) Determine the bounds of the sub-aggregate and lowest and
301 -- highest choice values.
303 -- 3. For positional aggregates:
305 -- (A) Loop over the component expressions either recursively invoking
306 -- Resolve_Array_Aggregate on each of these for multi-dimensional
307 -- array aggregates or resolving the bottom level component
308 -- expressions against the expected component type.
310 -- (B) Determine the bounds of the positional sub-aggregates.
312 -- 4. Try to determine statically whether the evaluation of the array
313 -- sub-aggregate raises Constraint_Error. If yes emit proper
314 -- warnings. The precise checks are the following:
316 -- (A) Check that the index range defined by aggregate bounds is
317 -- compatible with corresponding index subtype.
318 -- We also check against the base type. In fact it could be that
319 -- Low/High bounds of the base type are static whereas those of
320 -- the index subtype are not. Thus if we can statically catch
321 -- a problem with respect to the base type we are guaranteed
322 -- that the same problem will arise with the index subtype
324 -- (B) If we are dealing with a named aggregate containing an others
325 -- choice and at least one discrete choice then make sure the range
326 -- specified by the discrete choices does not overflow the
327 -- aggregate bounds. We also check against the index type and base
328 -- type bounds for the same reasons given in (A).
330 -- (C) If we are dealing with a positional aggregate with an others
331 -- choice make sure the number of positional elements specified
332 -- does not overflow the aggregate bounds. We also check against
333 -- the index type and base type bounds as mentioned in (A).
335 -- Finally construct an N_Range node giving the sub-aggregate bounds.
336 -- Set the Aggregate_Bounds field of the sub-aggregate to be this
337 -- N_Range. The routine Array_Aggr_Subtype below uses such N_Ranges
338 -- to build the appropriate aggregate subtype. Aggregate_Bounds
339 -- information is needed during expansion.
341 -- DELAYED COMPONENT RESOLUTION: The resolution of bottom level component
342 -- expressions in an array aggregate may call Duplicate_Subexpr or some
343 -- other routine that inserts code just outside the outermost aggregate.
344 -- If the array aggregate contains discrete choices or an others choice,
345 -- this may be wrong. Consider for instance the following example.
347 -- type Rec is record
348 -- V : Integer := 0;
349 -- end record;
351 -- type Acc_Rec is access Rec;
352 -- Arr : array (1..3) of Acc_Rec := (1 .. 3 => new Rec);
354 -- Then the transformation of "new Rec" that occurs during resolution
355 -- entails the following code modifications
357 -- P7b : constant Acc_Rec := new Rec;
358 -- RecIP (P7b.all);
359 -- Arr : array (1..3) of Acc_Rec := (1 .. 3 => P7b);
361 -- This code transformation is clearly wrong, since we need to call
362 -- "new Rec" for each of the 3 array elements. To avoid this problem we
363 -- delay resolution of the components of non positional array aggregates
364 -- to the expansion phase. As an optimization, if the discrete choice
365 -- specifies a single value we do not delay resolution.
367 function Array_Aggr_Subtype (N : Node_Id; Typ : Entity_Id) return Entity_Id;
368 -- This routine returns the type or subtype of an array aggregate.
370 -- N is the array aggregate node whose type we return.
372 -- Typ is the context type in which N occurs.
374 -- This routine creates an implicit array subtype whose bounds are
375 -- those defined by the aggregate. When this routine is invoked
376 -- Resolve_Array_Aggregate has already processed aggregate N. Thus the
377 -- Aggregate_Bounds of each sub-aggregate, is an N_Range node giving the
378 -- sub-aggregate bounds. When building the aggregate itype, this function
379 -- traverses the array aggregate N collecting such Aggregate_Bounds and
380 -- constructs the proper array aggregate itype.
382 -- Note that in the case of multidimensional aggregates each inner
383 -- sub-aggregate corresponding to a given array dimension, may provide a
384 -- different bounds. If it is possible to determine statically that
385 -- some sub-aggregates corresponding to the same index do not have the
386 -- same bounds, then a warning is emitted. If such check is not possible
387 -- statically (because some sub-aggregate bounds are dynamic expressions)
388 -- then this job is left to the expander. In all cases the particular
389 -- bounds that this function will chose for a given dimension is the first
390 -- N_Range node for a sub-aggregate corresponding to that dimension.
392 -- Note that the Raises_Constraint_Error flag of an array aggregate
393 -- whose evaluation is determined to raise CE by Resolve_Array_Aggregate,
394 -- is set in Resolve_Array_Aggregate but the aggregate is not
395 -- immediately replaced with a raise CE. In fact, Array_Aggr_Subtype must
396 -- first construct the proper itype for the aggregate (Gigi needs
397 -- this). After constructing the proper itype we will eventually replace
398 -- the top-level aggregate with a raise CE (done in Resolve_Aggregate).
399 -- Of course in cases such as:
401 -- type Arr is array (integer range <>) of Integer;
402 -- A : Arr := (positive range -1 .. 2 => 0);
404 -- The bounds of the aggregate itype are cooked up to look reasonable
405 -- (in this particular case the bounds will be 1 .. 2).
407 procedure Make_String_Into_Aggregate (N : Node_Id);
408 -- A string literal can appear in a context in which a one dimensional
409 -- array of characters is expected. This procedure simply rewrites the
410 -- string as an aggregate, prior to resolution.
412 function Resolve_Null_Array_Aggregate (N : Node_Id) return Boolean;
413 -- For the Ada 2022 construct, build a subtype with a null range for each
414 -- dimension, using the bounds from the context subtype (if the subtype
415 -- is constrained). If the subtype is unconstrained, then the bounds
416 -- are determined in much the same way as the bounds for a null string
417 -- literal with no applicable index constraint.
419 ---------------------------------
420 -- Delta aggregate processing --
421 ---------------------------------
423 procedure Resolve_Delta_Array_Aggregate (N : Node_Id; Typ : Entity_Id);
424 procedure Resolve_Delta_Record_Aggregate (N : Node_Id; Typ : Entity_Id);
425 procedure Resolve_Deep_Delta_Assoc (N : Node_Id; Typ : Entity_Id);
426 -- Resolve the names/expressions in a component association for
427 -- a deep delta aggregate. Typ is the type of the enclosing object.
429 ------------------------
430 -- Array_Aggr_Subtype --
431 ------------------------
433 function Array_Aggr_Subtype
434 (N : Node_Id;
435 Typ : Entity_Id) return Entity_Id
437 Aggr_Dimension : constant Pos := Number_Dimensions (Typ);
438 -- Number of aggregate index dimensions
440 Aggr_Range : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
441 -- Constrained N_Range of each index dimension in our aggregate itype
443 Aggr_Low : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
444 Aggr_High : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
445 -- Low and High bounds for each index dimension in our aggregate itype
447 Is_Fully_Positional : Boolean := True;
449 procedure Collect_Aggr_Bounds (N : Node_Id; Dim : Pos);
450 -- N is an array (sub-)aggregate. Dim is the dimension corresponding
451 -- to (sub-)aggregate N. This procedure collects and removes the side
452 -- effects of the constrained N_Range nodes corresponding to each index
453 -- dimension of our aggregate itype. These N_Range nodes are collected
454 -- in Aggr_Range above.
456 -- Likewise collect in Aggr_Low & Aggr_High above the low and high
457 -- bounds of each index dimension. If, when collecting, two bounds
458 -- corresponding to the same dimension are static and found to differ,
459 -- then emit a warning, and mark N as raising Constraint_Error.
461 -------------------------
462 -- Collect_Aggr_Bounds --
463 -------------------------
465 procedure Collect_Aggr_Bounds (N : Node_Id; Dim : Pos) is
466 This_Range : constant Node_Id := Aggregate_Bounds (N);
467 -- The aggregate range node of this specific sub-aggregate
469 This_Low : constant Node_Id := Low_Bound (This_Range);
470 This_High : constant Node_Id := High_Bound (This_Range);
471 -- The aggregate bounds of this specific sub-aggregate
473 Assoc : Node_Id;
474 Expr : Node_Id;
476 begin
477 Remove_Side_Effects (This_Low, Variable_Ref => True);
478 Remove_Side_Effects (This_High, Variable_Ref => True);
480 -- Collect the first N_Range for a given dimension that you find.
481 -- For a given dimension they must be all equal anyway.
483 if No (Aggr_Range (Dim)) then
484 Aggr_Low (Dim) := This_Low;
485 Aggr_High (Dim) := This_High;
486 Aggr_Range (Dim) := This_Range;
488 else
489 if Compile_Time_Known_Value (This_Low) then
490 if not Compile_Time_Known_Value (Aggr_Low (Dim)) then
491 Aggr_Low (Dim) := This_Low;
493 elsif Expr_Value (This_Low) /= Expr_Value (Aggr_Low (Dim)) then
494 Set_Raises_Constraint_Error (N);
495 Error_Msg_Warn := SPARK_Mode /= On;
496 Error_Msg_N ("sub-aggregate low bound mismatch<<", N);
497 Error_Msg_N ("\Constraint_Error [<<", N);
498 end if;
499 end if;
501 if Compile_Time_Known_Value (This_High) then
502 if not Compile_Time_Known_Value (Aggr_High (Dim)) then
503 Aggr_High (Dim) := This_High;
505 elsif
506 Expr_Value (This_High) /= Expr_Value (Aggr_High (Dim))
507 then
508 Set_Raises_Constraint_Error (N);
509 Error_Msg_Warn := SPARK_Mode /= On;
510 Error_Msg_N ("sub-aggregate high bound mismatch<<", N);
511 Error_Msg_N ("\Constraint_Error [<<", N);
512 end if;
513 end if;
514 end if;
516 if Dim < Aggr_Dimension then
518 -- Process positional components
520 if Present (Expressions (N)) then
521 Expr := First (Expressions (N));
522 while Present (Expr) loop
523 Collect_Aggr_Bounds (Expr, Dim + 1);
524 Next (Expr);
525 end loop;
526 end if;
528 -- Process component associations
530 if Present (Component_Associations (N)) then
531 Is_Fully_Positional := False;
533 Assoc := First (Component_Associations (N));
534 while Present (Assoc) loop
535 Expr := Expression (Assoc);
536 Collect_Aggr_Bounds (Expr, Dim + 1);
537 Next (Assoc);
538 end loop;
539 end if;
540 end if;
541 end Collect_Aggr_Bounds;
543 -- Array_Aggr_Subtype variables
545 Itype : Entity_Id;
546 -- The final itype of the overall aggregate
548 Index_Constraints : constant List_Id := New_List;
549 -- The list of index constraints of the aggregate itype
551 -- Start of processing for Array_Aggr_Subtype
553 begin
554 -- Make sure that the list of index constraints is properly attached to
555 -- the tree, and then collect the aggregate bounds.
557 -- If no aggregaate bounds have been set, this is an aggregate with
558 -- iterator specifications and a dynamic size to be determined by
559 -- first pass of expanded code.
561 if No (Aggregate_Bounds (N)) then
562 return Typ;
563 end if;
565 Set_Parent (Index_Constraints, N);
567 -- When resolving a null aggregate we created a list of aggregate bounds
568 -- for the consecutive dimensions. The bounds for the first dimension
569 -- are attached as the Aggregate_Bounds of the aggregate node.
571 if Is_Null_Aggregate (N) then
572 declare
573 This_Range : Node_Id := Aggregate_Bounds (N);
574 begin
575 for J in 1 .. Aggr_Dimension loop
576 Aggr_Range (J) := This_Range;
577 Next_Index (This_Range);
579 -- Remove bounds from the list, so they can be reattached as
580 -- the First_Index/Next_Index again by the code that also
581 -- handles non-null aggregates.
583 Remove (Aggr_Range (J));
584 end loop;
585 end;
586 else
587 Collect_Aggr_Bounds (N, 1);
588 end if;
590 -- Build the list of constrained indexes of our aggregate itype
592 for J in 1 .. Aggr_Dimension loop
593 Create_Index : declare
594 Index_Base : constant Entity_Id :=
595 Base_Type (Etype (Aggr_Range (J)));
596 Index_Typ : Entity_Id;
598 begin
599 -- Construct the Index subtype, and associate it with the range
600 -- construct that generates it.
602 Index_Typ :=
603 Create_Itype (Subtype_Kind (Ekind (Index_Base)), Aggr_Range (J));
605 Set_Etype (Index_Typ, Index_Base);
607 if Is_Character_Type (Index_Base) then
608 Set_Is_Character_Type (Index_Typ);
609 end if;
611 Set_Size_Info (Index_Typ, (Index_Base));
612 Set_RM_Size (Index_Typ, RM_Size (Index_Base));
613 Set_First_Rep_Item (Index_Typ, First_Rep_Item (Index_Base));
614 Set_Scalar_Range (Index_Typ, Aggr_Range (J));
616 if Is_Discrete_Or_Fixed_Point_Type (Index_Typ) then
617 Set_RM_Size (Index_Typ, UI_From_Int (Minimum_Size (Index_Typ)));
618 end if;
620 Set_Etype (Aggr_Range (J), Index_Typ);
622 Append (Aggr_Range (J), To => Index_Constraints);
623 end Create_Index;
624 end loop;
626 -- Now build the Itype
628 Itype := Create_Itype (E_Array_Subtype, N);
630 Set_First_Rep_Item (Itype, First_Rep_Item (Typ));
631 Set_Convention (Itype, Convention (Typ));
632 Set_Depends_On_Private (Itype, Has_Private_Component (Typ));
633 Set_Etype (Itype, Base_Type (Typ));
634 Set_Has_Alignment_Clause (Itype, Has_Alignment_Clause (Typ));
635 Set_Is_Aliased (Itype, Is_Aliased (Typ));
636 Set_Is_Independent (Itype, Is_Independent (Typ));
637 Set_Depends_On_Private (Itype, Depends_On_Private (Typ));
639 Copy_Suppress_Status (Index_Check, Typ, Itype);
640 Copy_Suppress_Status (Length_Check, Typ, Itype);
642 Set_First_Index (Itype, First (Index_Constraints));
643 Set_Is_Constrained (Itype, True);
644 Set_Is_Internal (Itype, True);
646 if Has_Predicates (Typ) then
647 Set_Has_Predicates (Itype);
649 -- If the base type has a predicate, capture the predicated parent
650 -- or the existing predicate function for SPARK use.
652 if Present (Predicate_Function (Typ)) then
653 Set_Predicate_Function (Itype, Predicate_Function (Typ));
655 elsif Is_Itype (Typ) then
656 Set_Predicated_Parent (Itype, Predicated_Parent (Typ));
658 else
659 Set_Predicated_Parent (Itype, Typ);
660 end if;
661 end if;
663 -- A simple optimization: purely positional aggregates of static
664 -- components should be passed to gigi unexpanded whenever possible, and
665 -- regardless of the staticness of the bounds themselves. Subsequent
666 -- checks in exp_aggr verify that type is not packed, etc.
668 Set_Size_Known_At_Compile_Time
669 (Itype,
670 Is_Fully_Positional
671 and then Comes_From_Source (N)
672 and then Size_Known_At_Compile_Time (Component_Type (Typ)));
674 -- We always need a freeze node for a packed array subtype, so that we
675 -- can build the Packed_Array_Impl_Type corresponding to the subtype. If
676 -- expansion is disabled, the packed array subtype is not built, and we
677 -- must not generate a freeze node for the type, or else it will appear
678 -- incomplete to gigi.
680 if Is_Packed (Itype)
681 and then not In_Spec_Expression
682 and then Expander_Active
683 then
684 Freeze_Itype (Itype, N);
685 end if;
687 return Itype;
688 end Array_Aggr_Subtype;
690 --------------------------------
691 -- Check_Misspelled_Component --
692 --------------------------------
694 procedure Check_Misspelled_Component
695 (Elements : Elist_Id;
696 Component : Node_Id)
698 Max_Suggestions : constant := 2;
700 Nr_Of_Suggestions : Natural := 0;
701 Suggestion_1 : Entity_Id := Empty;
702 Suggestion_2 : Entity_Id := Empty;
703 Component_Elmt : Elmt_Id;
705 begin
706 -- All the components of List are matched against Component and a count
707 -- is maintained of possible misspellings. When at the end of the
708 -- analysis there are one or two (not more) possible misspellings,
709 -- these misspellings will be suggested as possible corrections.
711 Component_Elmt := First_Elmt (Elements);
712 while Nr_Of_Suggestions <= Max_Suggestions
713 and then Present (Component_Elmt)
714 loop
715 if Is_Bad_Spelling_Of
716 (Chars (Node (Component_Elmt)),
717 Chars (Component))
718 then
719 Nr_Of_Suggestions := Nr_Of_Suggestions + 1;
721 case Nr_Of_Suggestions is
722 when 1 => Suggestion_1 := Node (Component_Elmt);
723 when 2 => Suggestion_2 := Node (Component_Elmt);
724 when others => null;
725 end case;
726 end if;
728 Next_Elmt (Component_Elmt);
729 end loop;
731 -- Report at most two suggestions
733 if Nr_Of_Suggestions = 1 then
734 Error_Msg_NE -- CODEFIX
735 ("\possible misspelling of&", Component, Suggestion_1);
737 elsif Nr_Of_Suggestions = 2 then
738 Error_Msg_Node_2 := Suggestion_2;
739 Error_Msg_NE -- CODEFIX
740 ("\possible misspelling of& or&", Component, Suggestion_1);
741 end if;
742 end Check_Misspelled_Component;
744 ----------------------------------------
745 -- Check_Expr_OK_In_Limited_Aggregate --
746 ----------------------------------------
748 procedure Check_Expr_OK_In_Limited_Aggregate (Expr : Node_Id) is
749 begin
750 if Is_Limited_Type (Etype (Expr))
751 and then Comes_From_Source (Expr)
752 then
753 if In_Instance_Body or else In_Inlined_Body then
754 null;
756 elsif not OK_For_Limited_Init (Etype (Expr), Expr) then
757 Error_Msg_N
758 ("initialization not allowed for limited types", Expr);
759 Explain_Limited_Type (Etype (Expr), Expr);
760 end if;
761 end if;
762 end Check_Expr_OK_In_Limited_Aggregate;
764 --------------------
765 -- Is_Deep_Choice --
766 --------------------
768 function Is_Deep_Choice
769 (Choice : Node_Id;
770 Aggr_Type : Type_Kind_Id) return Boolean
772 Pref : Node_Id := Choice;
773 begin
774 while not Is_Root_Prefix_Of_Deep_Choice (Pref) loop
775 Pref := Prefix (Pref);
776 end loop;
778 if Is_Array_Type (Aggr_Type) then
779 return Paren_Count (Pref) > 0
780 and then Pref /= Choice;
781 else
782 return Pref /= Choice;
783 end if;
784 end Is_Deep_Choice;
786 -------------------------
787 -- Is_Others_Aggregate --
788 -------------------------
790 function Is_Others_Aggregate (Aggr : Node_Id) return Boolean is
791 Assoc : constant List_Id := Component_Associations (Aggr);
793 begin
794 return No (Expressions (Aggr))
795 and then Nkind (First (Choice_List (First (Assoc)))) = N_Others_Choice;
796 end Is_Others_Aggregate;
798 -----------------------------------
799 -- Is_Root_Prefix_Of_Deep_Choice --
800 -----------------------------------
802 function Is_Root_Prefix_Of_Deep_Choice (Pref : Node_Id) return Boolean is
803 begin
804 return Paren_Count (Pref) > 0
805 or else Nkind (Pref) not in N_Indexed_Component
806 | N_Selected_Component;
807 end Is_Root_Prefix_Of_Deep_Choice;
809 -------------------------
810 -- Is_Single_Aggregate --
811 -------------------------
813 function Is_Single_Aggregate (Aggr : Node_Id) return Boolean is
814 Assoc : constant List_Id := Component_Associations (Aggr);
816 begin
817 return No (Expressions (Aggr))
818 and then No (Next (First (Assoc)))
819 and then No (Next (First (Choice_List (First (Assoc)))));
820 end Is_Single_Aggregate;
822 -----------------------
823 -- Is_Null_Aggregate --
824 -----------------------
826 function Is_Null_Aggregate (N : Node_Id) return Boolean is
827 begin
828 return Ada_Version >= Ada_2022
829 and then Is_Homogeneous_Aggregate (N)
830 and then Is_Empty_List (Expressions (N))
831 and then Is_Empty_List (Component_Associations (N));
832 end Is_Null_Aggregate;
834 ----------------------------------------
835 -- Is_Null_Array_Aggregate_High_Bound --
836 ----------------------------------------
838 function Is_Null_Array_Aggregate_High_Bound (N : Node_Id) return Boolean is
839 Original_N : constant Node_Id := Original_Node (N);
840 begin
841 return Ada_Version >= Ada_2022
842 and then not Comes_From_Source (Original_N)
843 and then Nkind (Original_N) = N_Attribute_Reference
844 and then
845 Get_Attribute_Id (Attribute_Name (Original_N)) = Attribute_Pred
846 and then Nkind (Parent (N)) in N_Range | N_Op_Le
847 and then not Comes_From_Source (Parent (N));
848 end Is_Null_Array_Aggregate_High_Bound;
850 --------------------------------
851 -- Make_String_Into_Aggregate --
852 --------------------------------
854 procedure Make_String_Into_Aggregate (N : Node_Id) is
855 Exprs : constant List_Id := New_List;
856 Loc : constant Source_Ptr := Sloc (N);
857 Str : constant String_Id := Strval (N);
858 Strlen : constant Nat := String_Length (Str);
859 C : Char_Code;
860 C_Node : Node_Id;
861 New_N : Node_Id;
862 P : Source_Ptr;
864 begin
865 P := Loc + 1;
866 for J in 1 .. Strlen loop
867 C := Get_String_Char (Str, J);
868 Set_Character_Literal_Name (C);
870 C_Node :=
871 Make_Character_Literal (P,
872 Chars => Name_Find,
873 Char_Literal_Value => UI_From_CC (C));
874 Set_Etype (C_Node, Any_Character);
875 Append_To (Exprs, C_Node);
877 P := P + 1;
878 -- Something special for wide strings???
879 end loop;
881 New_N := Make_Aggregate (Loc, Expressions => Exprs);
882 Set_Analyzed (New_N);
883 Set_Etype (New_N, Any_Composite);
885 Rewrite (N, New_N);
886 end Make_String_Into_Aggregate;
888 -----------------------
889 -- Resolve_Aggregate --
890 -----------------------
892 procedure Resolve_Aggregate (N : Node_Id; Typ : Entity_Id) is
893 Loc : constant Source_Ptr := Sloc (N);
895 Aggr_Subtyp : Entity_Id;
896 -- The actual aggregate subtype. This is not necessarily the same as Typ
897 -- which is the subtype of the context in which the aggregate was found.
899 Others_Box : Boolean := False;
900 -- Set to True if N represents a simple aggregate with only
901 -- (others => <>), not nested as part of another aggregate.
903 function Is_Full_Access_Aggregate (N : Node_Id) return Boolean;
904 -- If a full access object is initialized with an aggregate or is
905 -- assigned an aggregate, we have to prevent a piecemeal access or
906 -- assignment to the object, even if the aggregate is to be expanded.
907 -- We create a temporary for the aggregate, and assign the temporary
908 -- instead, so that the back end can generate an atomic move for it.
909 -- This is only done in the context of an object declaration or an
910 -- assignment. Function is a noop and returns false in other contexts.
912 function Within_Aggregate (N : Node_Id) return Boolean;
913 -- Return True if N is part of an N_Aggregate
915 ------------------------------
916 -- Is_Full_Access_Aggregate --
917 ------------------------------
919 function Is_Full_Access_Aggregate (N : Node_Id) return Boolean is
920 Loc : constant Source_Ptr := Sloc (N);
922 New_N : Node_Id;
923 Par : Node_Id;
924 Temp : Entity_Id;
925 Typ : Entity_Id;
927 begin
928 Par := Parent (N);
930 -- Aggregate may be qualified, so find outer context
932 if Nkind (Par) = N_Qualified_Expression then
933 Par := Parent (Par);
934 end if;
936 if not Comes_From_Source (Par) then
937 return False;
938 end if;
940 case Nkind (Par) is
941 when N_Assignment_Statement =>
942 Typ := Etype (Name (Par));
944 if not Is_Full_Access (Typ)
945 and then not Is_Full_Access_Object (Name (Par))
946 then
947 return False;
948 end if;
950 when N_Object_Declaration =>
951 Typ := Etype (Defining_Identifier (Par));
953 if not Is_Full_Access (Typ)
954 and then not Is_Full_Access (Defining_Identifier (Par))
955 then
956 return False;
957 end if;
959 when others =>
960 return False;
961 end case;
963 Temp := Make_Temporary (Loc, 'T', N);
964 New_N :=
965 Make_Object_Declaration (Loc,
966 Defining_Identifier => Temp,
967 Constant_Present => True,
968 Object_Definition => New_Occurrence_Of (Typ, Loc),
969 Expression => Relocate_Node (N));
970 Insert_Action (Par, New_N);
972 Rewrite (N, New_Occurrence_Of (Temp, Loc));
973 Analyze_And_Resolve (N, Typ);
975 return True;
976 end Is_Full_Access_Aggregate;
978 ----------------------
979 -- Within_Aggregate --
980 ----------------------
982 function Within_Aggregate (N : Node_Id) return Boolean is
983 P : Node_Id := Parent (N);
984 begin
985 while Present (P) loop
986 if Nkind (P) = N_Aggregate then
987 return True;
988 end if;
990 P := Parent (P);
991 end loop;
993 return False;
994 end Within_Aggregate;
996 -- Start of processing for Resolve_Aggregate
998 begin
999 -- Ignore junk empty aggregate resulting from parser error
1001 if No (Expressions (N))
1002 and then No (Component_Associations (N))
1003 and then not Null_Record_Present (N)
1004 then
1005 return;
1007 -- If the aggregate is assigned to a full access variable, we have
1008 -- to prevent a piecemeal assignment even if the aggregate is to be
1009 -- expanded. We create a temporary for the aggregate, and assign the
1010 -- temporary instead, so that the back end can generate an atomic move
1011 -- for it. This is properly an expansion activity but it must be done
1012 -- before resolution because aggregate resolution cannot be done twice.
1014 elsif Expander_Active and then Is_Full_Access_Aggregate (N) then
1015 return;
1016 end if;
1018 -- If the aggregate has box-initialized components, its type must be
1019 -- frozen so that initialization procedures can properly be called
1020 -- in the resolution that follows. The replacement of boxes with
1021 -- initialization calls is properly an expansion activity but it must
1022 -- be done during resolution.
1024 if Expander_Active
1025 and then Present (Component_Associations (N))
1026 then
1027 declare
1028 Comp : Node_Id;
1029 First_Comp : Boolean := True;
1031 begin
1032 Comp := First (Component_Associations (N));
1033 while Present (Comp) loop
1034 if Box_Present (Comp) then
1035 if First_Comp
1036 and then No (Expressions (N))
1037 and then Nkind (First (Choices (Comp))) = N_Others_Choice
1038 and then not Within_Aggregate (N)
1039 then
1040 Others_Box := True;
1041 end if;
1043 Insert_Actions (N, Freeze_Entity (Typ, N));
1044 exit;
1045 end if;
1047 First_Comp := False;
1048 Next (Comp);
1049 end loop;
1050 end;
1051 end if;
1053 -- Check for aggregates not allowed in configurable run-time mode.
1054 -- We allow all cases of aggregates that do not come from source, since
1055 -- these are all assumed to be small (e.g. bounds of a string literal).
1056 -- We also allow aggregates of types we know to be small.
1058 if not Support_Aggregates_On_Target
1059 and then Comes_From_Source (N)
1060 and then (not Known_Static_Esize (Typ)
1061 or else Esize (Typ) > System_Max_Integer_Size)
1062 then
1063 Error_Msg_CRT ("aggregate", N);
1064 end if;
1066 -- Ada 2005 (AI-287): Limited aggregates allowed
1068 -- In an instance, ignore aggregate subcomponents that may be limited,
1069 -- because they originate in view conflicts. If the original aggregate
1070 -- is legal and the actuals are legal, the aggregate itself is legal.
1072 if Is_Limited_Type (Typ)
1073 and then Ada_Version < Ada_2005
1074 and then not In_Instance
1075 then
1076 Error_Msg_N ("aggregate type cannot be limited", N);
1077 Explain_Limited_Type (Typ, N);
1079 elsif Is_Class_Wide_Type (Typ) then
1080 Error_Msg_N ("type of aggregate cannot be class-wide", N);
1082 elsif Typ = Any_String
1083 or else Typ = Any_Composite
1084 then
1085 Error_Msg_N ("no unique type for aggregate", N);
1086 Set_Etype (N, Any_Composite);
1088 elsif Is_Array_Type (Typ) and then Null_Record_Present (N) then
1089 Error_Msg_N ("null record forbidden in array aggregate", N);
1091 elsif Has_Aspect (Typ, Aspect_Aggregate)
1092 and then Ekind (Typ) /= E_Record_Type
1093 and then Ada_Version >= Ada_2022
1094 then
1095 -- Check for Ada 2022 and () aggregate.
1097 if not Is_Homogeneous_Aggregate (N) then
1098 Error_Msg_N ("container aggregate must use '['], not ()", N);
1099 end if;
1101 Resolve_Container_Aggregate (N, Typ);
1103 -- Check Ada 2022 empty aggregate [] initializing a record type that has
1104 -- aspect aggregate; the empty aggregate will be expanded into a call to
1105 -- the empty function specified in the aspect aggregate.
1107 elsif Has_Aspect (Typ, Aspect_Aggregate)
1108 and then Ekind (Typ) = E_Record_Type
1109 and then Is_Homogeneous_Aggregate (N)
1110 and then Is_Empty_List (Expressions (N))
1111 and then Is_Empty_List (Component_Associations (N))
1112 and then Ada_Version >= Ada_2022
1113 then
1114 Resolve_Container_Aggregate (N, Typ);
1116 elsif Is_Record_Type (Typ) then
1117 Resolve_Record_Aggregate (N, Typ);
1119 elsif Is_Array_Type (Typ) then
1121 -- First a special test, for the case of a positional aggregate of
1122 -- characters which can be replaced by a string literal.
1124 -- Do not perform this transformation if this was a string literal
1125 -- to start with, whose components needed constraint checks, or if
1126 -- the component type is non-static, because it will require those
1127 -- checks and be transformed back into an aggregate. If the index
1128 -- type is not Integer the aggregate may represent a user-defined
1129 -- string type but the context might need the original type so we
1130 -- do not perform the transformation at this point.
1132 if Number_Dimensions (Typ) = 1
1133 and then Is_Standard_Character_Type (Component_Type (Typ))
1134 and then No (Component_Associations (N))
1135 and then not Is_Limited_Composite (Typ)
1136 and then not Is_Private_Composite (Typ)
1137 and then not Is_Bit_Packed_Array (Typ)
1138 and then Nkind (Original_Node (Parent (N))) /= N_String_Literal
1139 and then Is_OK_Static_Subtype (Component_Type (Typ))
1140 and then Base_Type (Etype (First_Index (Typ))) =
1141 Base_Type (Standard_Integer)
1142 then
1143 declare
1144 Expr : Node_Id;
1146 begin
1147 Expr := First (Expressions (N));
1148 while Present (Expr) loop
1149 exit when Nkind (Expr) /= N_Character_Literal;
1150 Next (Expr);
1151 end loop;
1153 if No (Expr) then
1154 Start_String;
1156 Expr := First (Expressions (N));
1157 while Present (Expr) loop
1158 Store_String_Char (UI_To_CC (Char_Literal_Value (Expr)));
1159 Next (Expr);
1160 end loop;
1162 Rewrite (N, Make_String_Literal (Loc, End_String));
1164 Analyze_And_Resolve (N, Typ);
1165 return;
1166 end if;
1167 end;
1168 end if;
1170 -- Here if we have a real aggregate to deal with
1172 Array_Aggregate : declare
1173 Aggr_Resolved : Boolean;
1174 Aggr_Typ : constant Entity_Id := Etype (Typ);
1175 -- This is the unconstrained array type, which is the type against
1176 -- which the aggregate is to be resolved. Typ itself is the array
1177 -- type of the context which may not be the same subtype as the
1178 -- subtype for the final aggregate.
1180 Is_Null_Aggr : constant Boolean := Is_Null_Aggregate (N);
1182 begin
1183 -- In the following we determine whether an OTHERS choice is
1184 -- allowed inside the array aggregate. The test checks the context
1185 -- in which the array aggregate occurs. If the context does not
1186 -- permit it, or the aggregate type is unconstrained, an OTHERS
1187 -- choice is not allowed (except that it is always allowed on the
1188 -- right-hand side of an assignment statement; in this case the
1189 -- constrainedness of the type doesn't matter, because an array
1190 -- object is always constrained).
1192 -- If expansion is disabled (generic context, or semantics-only
1193 -- mode) actual subtypes cannot be constructed, and the type of an
1194 -- object may be its unconstrained nominal type. However, if the
1195 -- context is an assignment statement, OTHERS is allowed, because
1196 -- the target of the assignment will have a constrained subtype
1197 -- when fully compiled. Ditto if the context is an initialization
1198 -- procedure where a component may have a predicate function that
1199 -- carries the base type.
1201 -- Note that there is no node for Explicit_Actual_Parameter.
1202 -- To test for this context we therefore have to test for node
1203 -- N_Parameter_Association which itself appears only if there is a
1204 -- formal parameter. Consequently we also need to test for
1205 -- N_Procedure_Call_Statement or N_Function_Call.
1207 -- The context may be an N_Reference node, created by expansion.
1208 -- Legality of the others clause was established in the source,
1209 -- so the context is legal.
1211 Set_Etype (N, Aggr_Typ); -- May be overridden later on
1213 if Is_Null_Aggr then
1214 Set_Etype (N, Typ);
1215 Aggr_Resolved := Resolve_Null_Array_Aggregate (N);
1217 elsif Nkind (Parent (N)) = N_Assignment_Statement
1218 or else Inside_Init_Proc
1219 or else (Is_Constrained (Typ)
1220 and then Nkind (Parent (N)) in
1221 N_Parameter_Association
1222 | N_Function_Call
1223 | N_Procedure_Call_Statement
1224 | N_Generic_Association
1225 | N_Formal_Object_Declaration
1226 | N_Simple_Return_Statement
1227 | N_Object_Declaration
1228 | N_Component_Declaration
1229 | N_Parameter_Specification
1230 | N_Qualified_Expression
1231 | N_Unchecked_Type_Conversion
1232 | N_Reference
1233 | N_Aggregate
1234 | N_Extension_Aggregate
1235 | N_Component_Association
1236 | N_Case_Expression_Alternative
1237 | N_If_Expression
1238 | N_Expression_With_Actions)
1239 then
1240 Aggr_Resolved :=
1241 Resolve_Array_Aggregate
1243 Index => First_Index (Aggr_Typ),
1244 Index_Constr => First_Index (Typ),
1245 Component_Typ => Component_Type (Typ),
1246 Others_Allowed => True);
1247 else
1248 Aggr_Resolved :=
1249 Resolve_Array_Aggregate
1251 Index => First_Index (Aggr_Typ),
1252 Index_Constr => First_Index (Aggr_Typ),
1253 Component_Typ => Component_Type (Typ),
1254 Others_Allowed => False);
1255 end if;
1257 if not Aggr_Resolved then
1259 -- A parenthesized expression may have been intended as an
1260 -- aggregate, leading to a type error when analyzing the
1261 -- component. This can also happen for a nested component
1262 -- (see Analyze_Aggr_Expr).
1264 if Paren_Count (N) > 0 then
1265 Error_Msg_N
1266 ("positional aggregate cannot have one component", N);
1267 end if;
1269 Aggr_Subtyp := Any_Composite;
1271 else
1272 Aggr_Subtyp := Array_Aggr_Subtype (N, Typ);
1273 end if;
1275 Set_Etype (N, Aggr_Subtyp);
1276 end Array_Aggregate;
1278 elsif Is_Private_Type (Typ)
1279 and then Present (Full_View (Typ))
1280 and then (In_Inlined_Body or In_Instance_Body)
1281 and then Is_Composite_Type (Full_View (Typ))
1282 then
1283 Resolve (N, Full_View (Typ));
1285 else
1286 Error_Msg_N ("illegal context for aggregate", N);
1287 end if;
1289 -- If we can determine statically that the evaluation of the aggregate
1290 -- raises Constraint_Error, then replace the aggregate with an
1291 -- N_Raise_Constraint_Error node, but set the Etype to the right
1292 -- aggregate subtype. Gigi needs this.
1294 if Raises_Constraint_Error (N) then
1295 Aggr_Subtyp := Etype (N);
1296 Rewrite (N,
1297 Make_Raise_Constraint_Error (Loc, Reason => CE_Range_Check_Failed));
1298 Set_Raises_Constraint_Error (N);
1299 Set_Etype (N, Aggr_Subtyp);
1300 Set_Analyzed (N);
1301 end if;
1303 if Warn_On_No_Value_Assigned
1304 and then Others_Box
1305 and then not Is_Fully_Initialized_Type (Etype (N))
1306 then
1307 Error_Msg_N ("?v?aggregate not fully initialized", N);
1308 end if;
1310 Check_Function_Writable_Actuals (N);
1311 end Resolve_Aggregate;
1313 -----------------------------
1314 -- Resolve_Array_Aggregate --
1315 -----------------------------
1317 function Resolve_Array_Aggregate
1318 (N : Node_Id;
1319 Index : Node_Id;
1320 Index_Constr : Node_Id;
1321 Component_Typ : Entity_Id;
1322 Others_Allowed : Boolean) return Boolean
1324 Loc : constant Source_Ptr := Sloc (N);
1326 Failure : constant Boolean := False;
1327 Success : constant Boolean := True;
1329 Index_Typ : constant Entity_Id := Etype (Index);
1330 Index_Typ_Low : constant Node_Id := Type_Low_Bound (Index_Typ);
1331 Index_Typ_High : constant Node_Id := Type_High_Bound (Index_Typ);
1332 -- The type of the index corresponding to the array sub-aggregate along
1333 -- with its low and upper bounds.
1335 Index_Base : constant Entity_Id := Base_Type (Index_Typ);
1336 Index_Base_Low : constant Node_Id := Type_Low_Bound (Index_Base);
1337 Index_Base_High : constant Node_Id := Type_High_Bound (Index_Base);
1338 -- Ditto for the base type
1340 Others_Present : Boolean := False;
1342 Nb_Choices : Nat := 0;
1343 -- Contains the overall number of named choices in this sub-aggregate
1345 function Add (Val : Uint; To : Node_Id) return Node_Id;
1346 -- Creates a new expression node where Val is added to expression To.
1347 -- Tries to constant fold whenever possible. To must be an already
1348 -- analyzed expression.
1350 procedure Check_Bound (BH : Node_Id; AH : in out Node_Id);
1351 -- Checks that AH (the upper bound of an array aggregate) is less than
1352 -- or equal to BH (the upper bound of the index base type). If the check
1353 -- fails, a warning is emitted, the Raises_Constraint_Error flag of N is
1354 -- set, and AH is replaced with a duplicate of BH.
1356 procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id);
1357 -- Checks that range AL .. AH is compatible with range L .. H. Emits a
1358 -- warning if not and sets the Raises_Constraint_Error flag in N.
1360 procedure Check_Length (L, H : Node_Id; Len : Uint);
1361 -- Checks that range L .. H contains at least Len elements. Emits a
1362 -- warning if not and sets the Raises_Constraint_Error flag in N.
1364 function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean;
1365 -- Returns True if range L .. H is dynamic or null
1367 procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean);
1368 -- Given expression node From, this routine sets OK to False if it
1369 -- cannot statically evaluate From. Otherwise it stores this static
1370 -- value into Value.
1372 function Resolve_Aggr_Expr
1373 (Expr : Node_Id;
1374 Single_Elmt : Boolean) return Boolean;
1375 -- Resolves aggregate expression Expr. Returns False if resolution
1376 -- fails. If Single_Elmt is set to False, the expression Expr may be
1377 -- used to initialize several array aggregate elements (this can happen
1378 -- for discrete choices such as "L .. H => Expr" or the OTHERS choice).
1379 -- In this event we do not resolve Expr unless expansion is disabled.
1380 -- To know why, see the DELAYED COMPONENT RESOLUTION note above.
1382 -- NOTE: In the case of "... => <>", we pass the N_Component_Association
1383 -- node as Expr, since there is no Expression and we need a Sloc for the
1384 -- error message.
1386 procedure Resolve_Iterated_Component_Association
1387 (N : Node_Id;
1388 Index_Typ : Entity_Id);
1389 -- For AI12-061
1391 procedure Warn_On_Null_Component_Association (Expr : Node_Id);
1392 -- Expr is either a conditional expression or a case expression of an
1393 -- iterated component association initializing the aggregate N with
1394 -- components that can never be null. Report warning on associations
1395 -- that may initialize some component with a null value.
1397 ---------
1398 -- Add --
1399 ---------
1401 function Add (Val : Uint; To : Node_Id) return Node_Id is
1402 Expr_Pos : Node_Id;
1403 Expr : Node_Id;
1404 To_Pos : Node_Id;
1406 begin
1407 if Raises_Constraint_Error (To) then
1408 return To;
1409 end if;
1411 -- First test if we can do constant folding
1413 if Compile_Time_Known_Value (To)
1414 or else Nkind (To) = N_Integer_Literal
1415 then
1416 Expr_Pos := Make_Integer_Literal (Loc, Expr_Value (To) + Val);
1417 Set_Is_Static_Expression (Expr_Pos);
1418 Set_Etype (Expr_Pos, Etype (To));
1419 Set_Analyzed (Expr_Pos, Analyzed (To));
1421 if not Is_Enumeration_Type (Index_Typ) then
1422 Expr := Expr_Pos;
1424 -- If we are dealing with enumeration return
1425 -- Index_Typ'Val (Expr_Pos)
1427 else
1428 Expr :=
1429 Make_Attribute_Reference
1430 (Loc,
1431 Prefix => New_Occurrence_Of (Index_Typ, Loc),
1432 Attribute_Name => Name_Val,
1433 Expressions => New_List (Expr_Pos));
1434 end if;
1436 return Expr;
1437 end if;
1439 -- If we are here no constant folding possible
1441 if not Is_Enumeration_Type (Index_Base) then
1442 Expr :=
1443 Make_Op_Add (Loc,
1444 Left_Opnd => Duplicate_Subexpr (To),
1445 Right_Opnd => Make_Integer_Literal (Loc, Val));
1447 -- If we are dealing with enumeration return
1448 -- Index_Typ'Val (Index_Typ'Pos (To) + Val)
1450 else
1451 To_Pos :=
1452 Make_Attribute_Reference
1453 (Loc,
1454 Prefix => New_Occurrence_Of (Index_Typ, Loc),
1455 Attribute_Name => Name_Pos,
1456 Expressions => New_List (Duplicate_Subexpr (To)));
1458 Expr_Pos :=
1459 Make_Op_Add (Loc,
1460 Left_Opnd => To_Pos,
1461 Right_Opnd => Make_Integer_Literal (Loc, Val));
1463 Expr :=
1464 Make_Attribute_Reference
1465 (Loc,
1466 Prefix => New_Occurrence_Of (Index_Typ, Loc),
1467 Attribute_Name => Name_Val,
1468 Expressions => New_List (Expr_Pos));
1470 -- If the index type has a non standard representation, the
1471 -- attributes 'Val and 'Pos expand into function calls and the
1472 -- resulting expression is considered non-safe for reevaluation
1473 -- by the backend. Relocate it into a constant temporary in order
1474 -- to make it safe for reevaluation.
1476 if Has_Non_Standard_Rep (Etype (N)) then
1477 declare
1478 Def_Id : Entity_Id;
1480 begin
1481 Def_Id := Make_Temporary (Loc, 'R', Expr);
1482 Set_Etype (Def_Id, Index_Typ);
1483 Insert_Action (N,
1484 Make_Object_Declaration (Loc,
1485 Defining_Identifier => Def_Id,
1486 Object_Definition =>
1487 New_Occurrence_Of (Index_Typ, Loc),
1488 Constant_Present => True,
1489 Expression => Relocate_Node (Expr)));
1491 Expr := New_Occurrence_Of (Def_Id, Loc);
1492 end;
1493 end if;
1494 end if;
1496 return Expr;
1497 end Add;
1499 -----------------
1500 -- Check_Bound --
1501 -----------------
1503 procedure Check_Bound (BH : Node_Id; AH : in out Node_Id) is
1504 Val_BH : Uint;
1505 Val_AH : Uint;
1507 OK_BH : Boolean;
1508 OK_AH : Boolean;
1510 begin
1511 Get (Value => Val_BH, From => BH, OK => OK_BH);
1512 Get (Value => Val_AH, From => AH, OK => OK_AH);
1514 if OK_BH and then OK_AH and then Val_BH < Val_AH then
1515 Set_Raises_Constraint_Error (N);
1516 Error_Msg_Warn := SPARK_Mode /= On;
1517 Error_Msg_N ("upper bound out of range<<", AH);
1518 Error_Msg_N ("\Constraint_Error [<<", AH);
1520 -- You need to set AH to BH or else in the case of enumerations
1521 -- indexes we will not be able to resolve the aggregate bounds.
1523 AH := Duplicate_Subexpr (BH);
1524 end if;
1525 end Check_Bound;
1527 ------------------
1528 -- Check_Bounds --
1529 ------------------
1531 procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id) is
1532 Val_L : Uint;
1533 Val_H : Uint;
1534 Val_AL : Uint;
1535 Val_AH : Uint;
1537 OK_L : Boolean;
1538 OK_H : Boolean;
1540 OK_AL : Boolean;
1541 OK_AH : Boolean;
1542 pragma Warnings (Off, OK_AL);
1543 pragma Warnings (Off, OK_AH);
1545 begin
1546 if Raises_Constraint_Error (N)
1547 or else Dynamic_Or_Null_Range (AL, AH)
1548 then
1549 return;
1550 end if;
1552 Get (Value => Val_L, From => L, OK => OK_L);
1553 Get (Value => Val_H, From => H, OK => OK_H);
1555 Get (Value => Val_AL, From => AL, OK => OK_AL);
1556 Get (Value => Val_AH, From => AH, OK => OK_AH);
1558 if OK_L and then Val_L > Val_AL then
1559 Set_Raises_Constraint_Error (N);
1560 Error_Msg_Warn := SPARK_Mode /= On;
1561 Error_Msg_N ("lower bound of aggregate out of range<<", N);
1562 Error_Msg_N ("\Constraint_Error [<<", N);
1563 end if;
1565 if OK_H and then Val_H < Val_AH then
1566 Set_Raises_Constraint_Error (N);
1567 Error_Msg_Warn := SPARK_Mode /= On;
1568 Error_Msg_N ("upper bound of aggregate out of range<<", N);
1569 Error_Msg_N ("\Constraint_Error [<<", N);
1570 end if;
1571 end Check_Bounds;
1573 ------------------
1574 -- Check_Length --
1575 ------------------
1577 procedure Check_Length (L, H : Node_Id; Len : Uint) is
1578 Val_L : Uint;
1579 Val_H : Uint;
1581 OK_L : Boolean;
1582 OK_H : Boolean;
1584 Range_Len : Uint;
1586 begin
1587 if Raises_Constraint_Error (N) then
1588 return;
1589 end if;
1591 Get (Value => Val_L, From => L, OK => OK_L);
1592 Get (Value => Val_H, From => H, OK => OK_H);
1594 if not OK_L or else not OK_H then
1595 return;
1596 end if;
1598 -- If null range length is zero
1600 if Val_L > Val_H then
1601 Range_Len := Uint_0;
1602 else
1603 Range_Len := Val_H - Val_L + 1;
1604 end if;
1606 if Range_Len < Len then
1607 Set_Raises_Constraint_Error (N);
1608 Error_Msg_Warn := SPARK_Mode /= On;
1609 Error_Msg_N ("too many elements<<", N);
1610 Error_Msg_N ("\Constraint_Error [<<", N);
1611 end if;
1612 end Check_Length;
1614 ---------------------------
1615 -- Dynamic_Or_Null_Range --
1616 ---------------------------
1618 function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean is
1619 Val_L : Uint;
1620 Val_H : Uint;
1622 OK_L : Boolean;
1623 OK_H : Boolean;
1625 begin
1626 Get (Value => Val_L, From => L, OK => OK_L);
1627 Get (Value => Val_H, From => H, OK => OK_H);
1629 return not OK_L or else not OK_H
1630 or else not Is_OK_Static_Expression (L)
1631 or else not Is_OK_Static_Expression (H)
1632 or else Val_L > Val_H;
1633 end Dynamic_Or_Null_Range;
1635 ---------
1636 -- Get --
1637 ---------
1639 procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean) is
1640 begin
1641 OK := True;
1643 if Compile_Time_Known_Value (From) then
1644 Value := Expr_Value (From);
1646 -- If expression From is something like Some_Type'Val (10) then
1647 -- Value = 10.
1649 elsif Nkind (From) = N_Attribute_Reference
1650 and then Attribute_Name (From) = Name_Val
1651 and then Compile_Time_Known_Value (First (Expressions (From)))
1652 then
1653 Value := Expr_Value (First (Expressions (From)));
1654 else
1655 Value := Uint_0;
1656 OK := False;
1657 end if;
1658 end Get;
1660 -----------------------
1661 -- Resolve_Aggr_Expr --
1662 -----------------------
1664 function Resolve_Aggr_Expr
1665 (Expr : Node_Id;
1666 Single_Elmt : Boolean) return Boolean
1668 Nxt_Ind : constant Node_Id := Next_Index (Index);
1669 Nxt_Ind_Constr : constant Node_Id := Next_Index (Index_Constr);
1670 -- Index is the current index corresponding to the expression
1672 Resolution_OK : Boolean := True;
1673 -- Set to False if resolution of the expression failed
1675 begin
1676 -- Defend against previous errors
1678 if Nkind (Expr) = N_Error
1679 or else Error_Posted (Expr)
1680 then
1681 return True;
1682 end if;
1684 -- If the array type against which we are resolving the aggregate
1685 -- has several dimensions, the expressions nested inside the
1686 -- aggregate must be further aggregates (or strings).
1688 if Present (Nxt_Ind) then
1689 if Nkind (Expr) /= N_Aggregate then
1691 -- A string literal can appear where a one-dimensional array
1692 -- of characters is expected. If the literal looks like an
1693 -- operator, it is still an operator symbol, which will be
1694 -- transformed into a string when analyzed.
1696 if Is_Character_Type (Component_Typ)
1697 and then No (Next_Index (Nxt_Ind))
1698 and then Nkind (Expr) in N_String_Literal | N_Operator_Symbol
1699 then
1700 -- A string literal used in a multidimensional array
1701 -- aggregate in place of the final one-dimensional
1702 -- aggregate must not be enclosed in parentheses.
1704 if Paren_Count (Expr) /= 0 then
1705 Error_Msg_N ("no parenthesis allowed here", Expr);
1706 end if;
1708 Make_String_Into_Aggregate (Expr);
1710 else
1711 Error_Msg_N ("nested array aggregate expected", Expr);
1713 -- If the expression is parenthesized, this may be
1714 -- a missing component association for a 1-aggregate.
1716 if Paren_Count (Expr) > 0 then
1717 Error_Msg_N
1718 ("\if single-component aggregate is intended, "
1719 & "write e.g. (1 ='> ...)", Expr);
1720 end if;
1722 return Failure;
1723 end if;
1724 end if;
1726 -- If it's "... => <>", nothing to resolve
1728 if Nkind (Expr) = N_Component_Association then
1729 pragma Assert (Box_Present (Expr));
1730 return Success;
1731 end if;
1733 -- Ada 2005 (AI-231): Propagate the type to the nested aggregate.
1734 -- Required to check the null-exclusion attribute (if present).
1735 -- This value may be overridden later on.
1737 Set_Etype (Expr, Etype (N));
1739 Resolution_OK := Resolve_Array_Aggregate
1740 (Expr, Nxt_Ind, Nxt_Ind_Constr, Component_Typ, Others_Allowed);
1742 else
1743 -- If it's "... => <>", nothing to resolve
1745 if Nkind (Expr) = N_Component_Association then
1746 pragma Assert (Box_Present (Expr));
1747 return Success;
1748 end if;
1750 -- Do not resolve the expressions of discrete or others choices
1751 -- unless the expression covers a single component, or the
1752 -- expander is inactive.
1754 -- In SPARK mode, expressions that can perform side effects will
1755 -- be recognized by the gnat2why back-end, and the whole
1756 -- subprogram will be ignored. So semantic analysis can be
1757 -- performed safely.
1759 if Single_Elmt
1760 or else not Expander_Active
1761 or else In_Spec_Expression
1762 then
1763 Analyze_And_Resolve (Expr, Component_Typ);
1764 Check_Expr_OK_In_Limited_Aggregate (Expr);
1765 Check_Non_Static_Context (Expr);
1766 Aggregate_Constraint_Checks (Expr, Component_Typ);
1767 Check_Unset_Reference (Expr);
1768 end if;
1769 end if;
1771 -- If an aggregate component has a type with predicates, an explicit
1772 -- predicate check must be applied, as for an assignment statement,
1773 -- because the aggregate might not be expanded into individual
1774 -- component assignments. If the expression covers several components
1775 -- the analysis and the predicate check take place later.
1777 if Has_Predicates (Component_Typ)
1778 and then Analyzed (Expr)
1779 then
1780 Apply_Predicate_Check (Expr, Component_Typ);
1781 end if;
1783 if Raises_Constraint_Error (Expr)
1784 and then Nkind (Parent (Expr)) /= N_Component_Association
1785 then
1786 Set_Raises_Constraint_Error (N);
1787 end if;
1789 -- If the expression has been marked as requiring a range check,
1790 -- then generate it here. It's a bit odd to be generating such
1791 -- checks in the analyzer, but harmless since Generate_Range_Check
1792 -- does nothing (other than making sure Do_Range_Check is set) if
1793 -- the expander is not active.
1795 if Do_Range_Check (Expr) then
1796 Generate_Range_Check (Expr, Component_Typ, CE_Range_Check_Failed);
1797 end if;
1799 return Resolution_OK;
1800 end Resolve_Aggr_Expr;
1802 --------------------------------------------
1803 -- Resolve_Iterated_Component_Association --
1804 --------------------------------------------
1806 procedure Resolve_Iterated_Component_Association
1807 (N : Node_Id;
1808 Index_Typ : Entity_Id)
1810 Loc : constant Source_Ptr := Sloc (N);
1811 Id : constant Entity_Id := Defining_Identifier (N);
1813 -----------------------
1814 -- Remove_References --
1815 -----------------------
1817 function Remove_Reference (N : Node_Id) return Traverse_Result;
1818 -- Remove reference to the entity Id after analysis, so it can be
1819 -- properly reanalyzed after construct is expanded into a loop.
1821 function Remove_Reference (N : Node_Id) return Traverse_Result is
1822 begin
1823 if Nkind (N) = N_Identifier
1824 and then Present (Entity (N))
1825 and then Entity (N) = Id
1826 then
1827 Set_Entity (N, Empty);
1828 Set_Etype (N, Empty);
1829 end if;
1830 Set_Analyzed (N, False);
1831 return OK;
1832 end Remove_Reference;
1834 procedure Remove_References is new Traverse_Proc (Remove_Reference);
1836 -- Local variables
1838 Choice : Node_Id;
1839 Dummy : Boolean;
1840 Scop : Entity_Id;
1841 Expr : constant Node_Id := Expression (N);
1843 -- Start of processing for Resolve_Iterated_Component_Association
1845 begin
1846 Error_Msg_Ada_2022_Feature ("iterated component", Loc);
1848 -- Create a scope in which to introduce an index, to make it visible
1849 -- for the analysis of component expression.
1851 Scop := New_Internal_Entity (E_Loop, Current_Scope, Loc, 'L');
1852 Set_Etype (Scop, Standard_Void_Type);
1853 Set_Parent (Scop, Parent (N));
1854 Push_Scope (Scop);
1856 -- If there is iterator specification, then its preanalysis will make
1857 -- the index visible.
1859 if Present (Iterator_Specification (N)) then
1860 Preanalyze (Iterator_Specification (N));
1862 -- Otherwise, analyze discrete choices and make the index visible
1864 else
1865 -- Insert index name into current scope but don't decorate it yet,
1866 -- so that a premature usage of this name in discrete choices will
1867 -- be nicely diagnosed.
1869 Enter_Name (Id);
1871 Choice := First (Discrete_Choices (N));
1873 while Present (Choice) loop
1874 if Nkind (Choice) = N_Others_Choice then
1875 Others_Present := True;
1877 else
1878 Analyze (Choice);
1880 -- Choice can be a subtype name, a range, or an expression
1882 if Is_Entity_Name (Choice)
1883 and then Is_Type (Entity (Choice))
1884 and then
1885 Base_Type (Entity (Choice)) = Base_Type (Index_Typ)
1886 then
1887 null;
1889 else
1890 Analyze_And_Resolve (Choice, Index_Typ);
1891 end if;
1892 end if;
1894 Next (Choice);
1895 end loop;
1897 -- Decorate the index variable
1899 Set_Etype (Id, Index_Typ);
1900 Mutate_Ekind (Id, E_Variable);
1901 Set_Is_Not_Self_Hidden (Id);
1902 Set_Scope (Id, Scop);
1903 end if;
1905 -- Analyze expression without expansion, to verify legality.
1906 -- When generating code, we then remove references to the index
1907 -- variable, because the expression will be analyzed anew after
1908 -- rewritting as a loop with a new index variable; when not
1909 -- generating code we leave the analyzed expression as it is.
1911 Dummy := Resolve_Aggr_Expr (Expr, Single_Elmt => False);
1913 if Operating_Mode /= Check_Semantics then
1914 Remove_References (Expr);
1915 end if;
1917 -- An iterated_component_association may appear in a nested
1918 -- aggregate for a multidimensional structure: preserve the bounds
1919 -- computed for the expression, as well as the anonymous array
1920 -- type generated for it; both are needed during array expansion.
1922 if Nkind (Expr) = N_Aggregate then
1923 Set_Aggregate_Bounds (Expression (N), Aggregate_Bounds (Expr));
1924 Set_Etype (Expression (N), Etype (Expr));
1925 end if;
1927 End_Scope;
1928 end Resolve_Iterated_Component_Association;
1930 ----------------------------------------
1931 -- Warn_On_Null_Component_Association --
1932 ----------------------------------------
1934 procedure Warn_On_Null_Component_Association (Expr : Node_Id) is
1935 Comp_Typ : constant Entity_Id := Component_Type (Etype (N));
1937 procedure Check_Case_Expr (N : Node_Id);
1938 -- Check if a case expression may initialize some component with a
1939 -- null value.
1941 procedure Check_Cond_Expr (N : Node_Id);
1942 -- Check if a conditional expression may initialize some component
1943 -- with a null value.
1945 procedure Check_Expr (Expr : Node_Id);
1946 -- Check if an expression may initialize some component with a
1947 -- null value.
1949 procedure Warn_On_Null_Expression_And_Rewrite (Null_Expr : Node_Id);
1950 -- Report warning on known null expression and replace the expression
1951 -- by a raise constraint error node.
1953 ---------------------
1954 -- Check_Case_Expr --
1955 ---------------------
1957 procedure Check_Case_Expr (N : Node_Id) is
1958 Alt_Node : Node_Id := First (Alternatives (N));
1960 begin
1961 while Present (Alt_Node) loop
1962 Check_Expr (Expression (Alt_Node));
1963 Next (Alt_Node);
1964 end loop;
1965 end Check_Case_Expr;
1967 ---------------------
1968 -- Check_Cond_Expr --
1969 ---------------------
1971 procedure Check_Cond_Expr (N : Node_Id) is
1972 If_Expr : Node_Id := N;
1973 Then_Expr : Node_Id;
1974 Else_Expr : Node_Id;
1976 begin
1977 Then_Expr := Next (First (Expressions (If_Expr)));
1978 Else_Expr := Next (Then_Expr);
1980 Check_Expr (Then_Expr);
1982 -- Process elsif parts (if any)
1984 while Nkind (Else_Expr) = N_If_Expression loop
1985 If_Expr := Else_Expr;
1986 Then_Expr := Next (First (Expressions (If_Expr)));
1987 Else_Expr := Next (Then_Expr);
1989 Check_Expr (Then_Expr);
1990 end loop;
1992 if Known_Null (Else_Expr) then
1993 Warn_On_Null_Expression_And_Rewrite (Else_Expr);
1994 end if;
1995 end Check_Cond_Expr;
1997 ----------------
1998 -- Check_Expr --
1999 ----------------
2001 procedure Check_Expr (Expr : Node_Id) is
2002 begin
2003 if Known_Null (Expr) then
2004 Warn_On_Null_Expression_And_Rewrite (Expr);
2006 elsif Nkind (Expr) = N_If_Expression then
2007 Check_Cond_Expr (Expr);
2009 elsif Nkind (Expr) = N_Case_Expression then
2010 Check_Case_Expr (Expr);
2011 end if;
2012 end Check_Expr;
2014 -----------------------------------------
2015 -- Warn_On_Null_Expression_And_Rewrite --
2016 -----------------------------------------
2018 procedure Warn_On_Null_Expression_And_Rewrite (Null_Expr : Node_Id) is
2019 begin
2020 Error_Msg_N
2021 ("(Ada 2005) NULL not allowed in null-excluding component??",
2022 Null_Expr);
2023 Error_Msg_N
2024 ("\Constraint_Error might be raised at run time??", Null_Expr);
2026 -- We cannot use Apply_Compile_Time_Constraint_Error because in
2027 -- some cases the components are rewritten and the runtime error
2028 -- would be missed.
2030 Rewrite (Null_Expr,
2031 Make_Raise_Constraint_Error (Sloc (Null_Expr),
2032 Reason => CE_Access_Check_Failed));
2034 Set_Etype (Null_Expr, Comp_Typ);
2035 Set_Analyzed (Null_Expr);
2036 end Warn_On_Null_Expression_And_Rewrite;
2038 -- Start of processing for Warn_On_Null_Component_Association
2040 begin
2041 pragma Assert (Can_Never_Be_Null (Comp_Typ));
2043 case Nkind (Expr) is
2044 when N_If_Expression =>
2045 Check_Cond_Expr (Expr);
2047 when N_Case_Expression =>
2048 Check_Case_Expr (Expr);
2050 when others =>
2051 pragma Assert (False);
2052 null;
2053 end case;
2054 end Warn_On_Null_Component_Association;
2056 -- Local variables
2058 Assoc : Node_Id;
2059 Choice : Node_Id;
2060 Expr : Node_Id;
2061 Discard : Node_Id;
2063 Aggr_Low : Node_Id := Empty;
2064 Aggr_High : Node_Id := Empty;
2065 -- The actual low and high bounds of this sub-aggregate
2067 Case_Table_Size : Nat;
2068 -- Contains the size of the case table needed to sort aggregate choices
2070 Choices_Low : Node_Id := Empty;
2071 Choices_High : Node_Id := Empty;
2072 -- The lowest and highest discrete choices values for a named aggregate
2074 Delete_Choice : Boolean;
2075 -- Used when replacing a subtype choice with predicate by a list
2077 Has_Iterator_Specifications : Boolean := False;
2078 -- Flag to indicate that all named associations are iterated component
2079 -- associations with iterator specifications, in which case the
2080 -- expansion will create two loops: one to evaluate the size and one
2081 -- to generate the elements (4.3.3 (20.2/5)).
2083 Nb_Elements : Uint := Uint_0;
2084 -- The number of elements in a positional aggregate
2086 Nb_Discrete_Choices : Nat := 0;
2087 -- The overall number of discrete choices (not counting others choice)
2089 -- Start of processing for Resolve_Array_Aggregate
2091 begin
2092 -- Ignore junk empty aggregate resulting from parser error
2094 if No (Expressions (N))
2095 and then No (Component_Associations (N))
2096 and then not Null_Record_Present (N)
2097 then
2098 return Failure;
2099 end if;
2101 -- Disable the warning for GNAT Mode to allow for easier transition.
2103 if Ada_Version_Explicit >= Ada_2022
2104 and then Warn_On_Obsolescent_Feature
2105 and then not GNAT_Mode
2106 and then not Is_Homogeneous_Aggregate (N)
2107 and then not Is_Enum_Array_Aggregate (N)
2108 and then Is_Parenthesis_Aggregate (N)
2109 and then Nkind (Parent (N)) /= N_Qualified_Expression
2110 and then Comes_From_Source (N)
2111 then
2112 Error_Msg_N
2113 ("?j?array aggregate using () is an" &
2114 " obsolescent syntax, use '['] instead", N);
2115 end if;
2117 -- STEP 1: make sure the aggregate is correctly formatted
2119 if Is_Null_Aggregate (N) then
2120 null;
2122 elsif Present (Component_Associations (N)) then
2124 -- Verify that all or none of the component associations
2125 -- include an iterator specification.
2127 Assoc := First (Component_Associations (N));
2128 if Nkind (Assoc) = N_Iterated_Component_Association
2129 and then Present (Iterator_Specification (Assoc))
2130 then
2131 -- All other component associations must have an iterator spec.
2133 Next (Assoc);
2134 while Present (Assoc) loop
2135 if Nkind (Assoc) /= N_Iterated_Component_Association
2136 or else No (Iterator_Specification (Assoc))
2137 then
2138 Error_Msg_N ("mixed iterated component association"
2139 & " (RM 4.3.3 (17.1/5))",
2140 Assoc);
2141 return Failure;
2142 end if;
2144 Next (Assoc);
2145 end loop;
2147 Has_Iterator_Specifications := True;
2149 else
2150 -- or none of them do.
2152 Next (Assoc);
2153 while Present (Assoc) loop
2154 if Nkind (Assoc) = N_Iterated_Component_Association
2155 and then Present (Iterator_Specification (Assoc))
2156 then
2157 Error_Msg_N ("mixed iterated component association"
2158 & " (RM 4.3.3 (17.1/5))",
2159 Assoc);
2160 return Failure;
2161 end if;
2163 Next (Assoc);
2164 end loop;
2166 end if;
2168 Assoc := First (Component_Associations (N));
2169 while Present (Assoc) loop
2170 if Nkind (Assoc) = N_Iterated_Component_Association then
2171 Resolve_Iterated_Component_Association (Assoc, Index_Typ);
2173 elsif Nkind (Assoc) /= N_Component_Association then
2174 Error_Msg_N
2175 ("invalid component association for aggregate", Assoc);
2176 return Failure;
2177 end if;
2179 Choice := First (Choice_List (Assoc));
2180 Delete_Choice := False;
2181 while Present (Choice) loop
2182 if Nkind (Choice) = N_Others_Choice then
2183 Others_Present := True;
2185 if Choice /= First (Choice_List (Assoc))
2186 or else Present (Next (Choice))
2187 then
2188 Error_Msg_N
2189 ("OTHERS must appear alone in a choice list", Choice);
2190 return Failure;
2191 end if;
2193 if Present (Next (Assoc)) then
2194 Error_Msg_N
2195 ("OTHERS must appear last in an aggregate", Choice);
2196 return Failure;
2197 end if;
2199 if Ada_Version = Ada_83
2200 and then Assoc /= First (Component_Associations (N))
2201 and then Nkind (Parent (N)) in
2202 N_Assignment_Statement | N_Object_Declaration
2203 then
2204 Error_Msg_N
2205 ("(Ada 83) illegal context for OTHERS choice", N);
2206 end if;
2208 elsif Is_Entity_Name (Choice) then
2209 Analyze (Choice);
2211 declare
2212 E : constant Entity_Id := Entity (Choice);
2213 New_Cs : List_Id;
2214 P : Node_Id;
2215 C : Node_Id;
2217 begin
2218 if Is_Type (E) and then Has_Predicates (E) then
2219 Freeze_Before (N, E);
2221 if Has_Dynamic_Predicate_Aspect (E)
2222 or else Has_Ghost_Predicate_Aspect (E)
2223 then
2224 Error_Msg_NE
2225 ("subtype& has non-static predicate, not allowed "
2226 & "in aggregate choice", Choice, E);
2228 elsif not Is_OK_Static_Subtype (E) then
2229 Error_Msg_NE
2230 ("non-static subtype& has predicate, not allowed "
2231 & "in aggregate choice", Choice, E);
2232 end if;
2234 -- If the subtype has a static predicate, replace the
2235 -- original choice with the list of individual values
2236 -- covered by the predicate.
2237 -- This should be deferred to expansion time ???
2239 if Present (Static_Discrete_Predicate (E)) then
2240 Delete_Choice := True;
2242 New_Cs := New_List;
2243 P := First (Static_Discrete_Predicate (E));
2244 while Present (P) loop
2245 C := New_Copy (P);
2246 Set_Sloc (C, Sloc (Choice));
2247 Append_To (New_Cs, C);
2248 Next (P);
2249 end loop;
2251 Insert_List_After (Choice, New_Cs);
2252 end if;
2253 end if;
2254 end;
2255 end if;
2257 Nb_Choices := Nb_Choices + 1;
2259 declare
2260 C : constant Node_Id := Choice;
2262 begin
2263 Next (Choice);
2265 if Delete_Choice then
2266 Remove (C);
2267 Nb_Choices := Nb_Choices - 1;
2268 Delete_Choice := False;
2269 end if;
2270 end;
2271 end loop;
2273 Next (Assoc);
2274 end loop;
2275 end if;
2277 -- At this point we know that the others choice, if present, is by
2278 -- itself and appears last in the aggregate. Check if we have mixed
2279 -- positional and discrete associations (other than the others choice).
2281 if Present (Expressions (N))
2282 and then (Nb_Choices > 1
2283 or else (Nb_Choices = 1 and then not Others_Present))
2284 then
2285 Error_Msg_N
2286 ("cannot mix named and positional associations in array aggregate",
2287 First (Choice_List (First (Component_Associations (N)))));
2288 return Failure;
2289 end if;
2291 -- Test for the validity of an others choice if present
2293 if Others_Present and then not Others_Allowed then
2294 declare
2295 Others_N : constant Node_Id :=
2296 First (Choice_List (First (Component_Associations (N))));
2297 begin
2298 Error_Msg_N ("OTHERS choice not allowed here", Others_N);
2299 Error_Msg_N ("\qualify the aggregate with a constrained subtype "
2300 & "to provide bounds for it", Others_N);
2301 return Failure;
2302 end;
2303 end if;
2305 -- Protect against cascaded errors
2307 if Etype (Index_Typ) = Any_Type then
2308 return Failure;
2309 end if;
2311 -- STEP 2: Process named components
2313 if No (Expressions (N)) then
2314 if Others_Present then
2315 Case_Table_Size := Nb_Choices - 1;
2316 else
2317 Case_Table_Size := Nb_Choices;
2318 end if;
2320 Step_2 : declare
2321 function Empty_Range (A : Node_Id) return Boolean;
2322 -- If an association covers an empty range, some warnings on the
2323 -- expression of the association can be disabled.
2325 -----------------
2326 -- Empty_Range --
2327 -----------------
2329 function Empty_Range (A : Node_Id) return Boolean is
2330 R : Node_Id;
2332 begin
2333 if Nkind (A) = N_Iterated_Component_Association then
2334 R := First (Discrete_Choices (A));
2335 else
2336 R := First (Choices (A));
2337 end if;
2339 return No (Next (R))
2340 and then Nkind (R) = N_Range
2341 and then Compile_Time_Compare
2342 (Low_Bound (R), High_Bound (R), False) = GT;
2343 end Empty_Range;
2345 -- Local variables
2347 Low : Node_Id;
2348 High : Node_Id;
2349 -- Denote the lowest and highest values in an aggregate choice
2351 S_Low : Node_Id := Empty;
2352 S_High : Node_Id := Empty;
2353 -- if a choice in an aggregate is a subtype indication these
2354 -- denote the lowest and highest values of the subtype
2356 Table : Case_Table_Type (1 .. Case_Table_Size);
2357 -- Used to sort all the different choice values
2359 Single_Choice : Boolean;
2360 -- Set to true every time there is a single discrete choice in a
2361 -- discrete association
2363 Prev_Nb_Discrete_Choices : Nat;
2364 -- Used to keep track of the number of discrete choices in the
2365 -- current association.
2367 Errors_Posted_On_Choices : Boolean := False;
2368 -- Keeps track of whether any choices have semantic errors
2370 -- Start of processing for Step_2
2372 begin
2373 -- STEP 2 (A): Check discrete choices validity
2374 -- No need if this is an element iteration.
2376 Assoc := First (Component_Associations (N));
2377 while Present (Assoc)
2378 and then Present (Choice_List (Assoc))
2379 loop
2380 Prev_Nb_Discrete_Choices := Nb_Discrete_Choices;
2381 Choice := First (Choice_List (Assoc));
2383 loop
2384 Analyze (Choice);
2386 if Nkind (Choice) = N_Others_Choice then
2387 Single_Choice := False;
2388 exit;
2390 -- Test for subtype mark without constraint
2392 elsif Is_Entity_Name (Choice) and then
2393 Is_Type (Entity (Choice))
2394 then
2395 if Base_Type (Entity (Choice)) /= Index_Base then
2396 Error_Msg_N
2397 ("invalid subtype mark in aggregate choice",
2398 Choice);
2399 return Failure;
2400 end if;
2402 -- Case of subtype indication
2404 elsif Nkind (Choice) = N_Subtype_Indication then
2405 Resolve_Discrete_Subtype_Indication (Choice, Index_Base);
2407 if Has_Dynamic_Predicate_Aspect
2408 (Entity (Subtype_Mark (Choice)))
2409 or else Has_Ghost_Predicate_Aspect
2410 (Entity (Subtype_Mark (Choice)))
2411 then
2412 Error_Msg_NE
2413 ("subtype& has non-static predicate, "
2414 & "not allowed in aggregate choice",
2415 Choice, Entity (Subtype_Mark (Choice)));
2416 end if;
2418 -- Does the subtype indication evaluation raise CE?
2420 Get_Index_Bounds (Subtype_Mark (Choice), S_Low, S_High);
2421 Get_Index_Bounds (Choice, Low, High);
2422 Check_Bounds (S_Low, S_High, Low, High);
2424 -- Case of range or expression
2426 else
2427 Resolve (Choice, Index_Base);
2428 Check_Unset_Reference (Choice);
2429 Check_Non_Static_Context (Choice);
2431 -- If semantic errors were posted on the choice, then
2432 -- record that for possible early return from later
2433 -- processing (see handling of enumeration choices).
2435 if Error_Posted (Choice) then
2436 Errors_Posted_On_Choices := True;
2437 end if;
2439 -- Do not range check a choice. This check is redundant
2440 -- since this test is already done when we check that the
2441 -- bounds of the array aggregate are within range.
2443 Set_Do_Range_Check (Choice, False);
2444 end if;
2446 -- If we could not resolve the discrete choice stop here
2448 if Etype (Choice) = Any_Type then
2449 return Failure;
2451 -- If the discrete choice raises CE get its original bounds
2453 elsif Nkind (Choice) = N_Raise_Constraint_Error then
2454 Set_Raises_Constraint_Error (N);
2455 Get_Index_Bounds (Original_Node (Choice), Low, High);
2457 -- Otherwise get its bounds as usual
2459 else
2460 Get_Index_Bounds (Choice, Low, High);
2461 end if;
2463 if (Dynamic_Or_Null_Range (Low, High)
2464 or else (Nkind (Choice) = N_Subtype_Indication
2465 and then
2466 Dynamic_Or_Null_Range (S_Low, S_High)))
2467 and then Nb_Choices /= 1
2468 then
2469 Error_Msg_N
2470 ("dynamic or empty choice in aggregate "
2471 & "must be the only choice", Choice);
2472 return Failure;
2473 end if;
2475 if not (All_Composite_Constraints_Static (Low)
2476 and then All_Composite_Constraints_Static (High)
2477 and then All_Composite_Constraints_Static (S_Low)
2478 and then All_Composite_Constraints_Static (S_High))
2479 then
2480 Check_Restriction (No_Dynamic_Sized_Objects, Choice);
2481 end if;
2483 Nb_Discrete_Choices := Nb_Discrete_Choices + 1;
2484 Table (Nb_Discrete_Choices).Lo := Low;
2485 Table (Nb_Discrete_Choices).Hi := High;
2486 Table (Nb_Discrete_Choices).Choice := Choice;
2488 Next (Choice);
2490 if No (Choice) then
2492 -- Check if we have a single discrete choice and whether
2493 -- this discrete choice specifies a single value.
2495 Single_Choice :=
2496 Nb_Discrete_Choices = Prev_Nb_Discrete_Choices + 1
2497 and then Low = High;
2499 exit;
2500 end if;
2501 end loop;
2503 -- Ada 2005 (AI-231)
2505 if Ada_Version >= Ada_2005
2506 and then not Empty_Range (Assoc)
2507 then
2508 if Known_Null (Expression (Assoc)) then
2509 Check_Can_Never_Be_Null (Etype (N), Expression (Assoc));
2511 -- Report warning on iterated component association that may
2512 -- initialize some component of an array of null-excluding
2513 -- access type components with a null value. For example:
2515 -- type AList is array (...) of not null access Integer;
2516 -- L : AList :=
2517 -- [for J in A'Range =>
2518 -- (if Func (J) = 0 then A(J)'Access else Null)];
2520 elsif Ada_Version >= Ada_2022
2521 and then Can_Never_Be_Null (Component_Type (Etype (N)))
2522 and then Nkind (Assoc) = N_Iterated_Component_Association
2523 and then Nkind (Expression (Assoc)) in N_If_Expression
2524 | N_Case_Expression
2525 then
2526 Warn_On_Null_Component_Association (Expression (Assoc));
2527 end if;
2528 end if;
2530 -- Ada 2005 (AI-287): In case of default initialized component
2531 -- we delay the resolution to the expansion phase.
2533 if Box_Present (Assoc) then
2535 -- Ada 2005 (AI-287): In case of default initialization of a
2536 -- component the expander will generate calls to the
2537 -- corresponding initialization subprogram. We need to call
2538 -- Resolve_Aggr_Expr to check the rules about
2539 -- dimensionality.
2541 if not Resolve_Aggr_Expr
2542 (Assoc, Single_Elmt => Single_Choice)
2543 then
2544 return Failure;
2545 end if;
2547 -- ??? Checks for dynamically tagged expressions below will
2548 -- be only applied to iterated_component_association after
2549 -- expansion; in particular, errors might not be reported when
2550 -- -gnatc switch is used.
2552 elsif Nkind (Assoc) = N_Iterated_Component_Association then
2553 null; -- handled above, in a loop context
2555 elsif not Resolve_Aggr_Expr
2556 (Expression (Assoc), Single_Elmt => Single_Choice)
2557 then
2558 return Failure;
2560 -- Check incorrect use of dynamically tagged expression
2562 -- We differentiate here two cases because the expression may
2563 -- not be decorated. For example, the analysis and resolution
2564 -- of the expression associated with the others choice will be
2565 -- done later with the full aggregate. In such case we
2566 -- duplicate the expression tree to analyze the copy and
2567 -- perform the required check.
2569 elsif No (Etype (Expression (Assoc))) then
2570 declare
2571 Save_Analysis : constant Boolean := Full_Analysis;
2572 Expr : constant Node_Id :=
2573 New_Copy_Tree (Expression (Assoc));
2575 begin
2576 Expander_Mode_Save_And_Set (False);
2577 Full_Analysis := False;
2579 -- Analyze the expression, making sure it is properly
2580 -- attached to the tree before we do the analysis.
2582 Set_Parent (Expr, Parent (Expression (Assoc)));
2583 Analyze (Expr);
2585 -- Compute its dimensions now, rather than at the end of
2586 -- resolution, because in the case of multidimensional
2587 -- aggregates subsequent expansion may lead to spurious
2588 -- errors.
2590 Check_Expression_Dimensions (Expr, Component_Typ);
2592 -- If the expression is a literal, propagate this info
2593 -- to the expression in the association, to enable some
2594 -- optimizations downstream.
2596 if Is_Entity_Name (Expr)
2597 and then Present (Entity (Expr))
2598 and then Ekind (Entity (Expr)) = E_Enumeration_Literal
2599 then
2600 Analyze_And_Resolve
2601 (Expression (Assoc), Component_Typ);
2602 end if;
2604 Full_Analysis := Save_Analysis;
2605 Expander_Mode_Restore;
2607 if Is_Tagged_Type (Etype (Expr)) then
2608 Check_Dynamically_Tagged_Expression
2609 (Expr => Expr,
2610 Typ => Component_Type (Etype (N)),
2611 Related_Nod => N);
2612 end if;
2613 end;
2615 elsif Is_Tagged_Type (Etype (Expression (Assoc))) then
2616 Check_Dynamically_Tagged_Expression
2617 (Expr => Expression (Assoc),
2618 Typ => Component_Type (Etype (N)),
2619 Related_Nod => N);
2620 end if;
2622 Next (Assoc);
2623 end loop;
2625 -- If aggregate contains more than one choice then these must be
2626 -- static. Check for duplicate and missing values.
2628 -- Note: there is duplicated code here wrt Check_Choice_Set in
2629 -- the body of Sem_Case, and it is possible we could just reuse
2630 -- that procedure. To be checked ???
2632 if Nb_Discrete_Choices > 1 then
2633 Check_Choices : declare
2634 Choice : Node_Id;
2635 -- Location of choice for messages
2637 Hi_Val : Uint;
2638 Lo_Val : Uint;
2639 -- High end of one range and Low end of the next. Should be
2640 -- contiguous if there is no hole in the list of values.
2642 Lo_Dup : Uint;
2643 Hi_Dup : Uint;
2644 -- End points of duplicated range
2646 Missing_Or_Duplicates : Boolean := False;
2647 -- Set True if missing or duplicate choices found
2649 procedure Output_Bad_Choices (Lo, Hi : Uint; C : Node_Id);
2650 -- Output continuation message with a representation of the
2651 -- bounds (just Lo if Lo = Hi, else Lo .. Hi). C is the
2652 -- choice node where the message is to be posted.
2654 ------------------------
2655 -- Output_Bad_Choices --
2656 ------------------------
2658 procedure Output_Bad_Choices (Lo, Hi : Uint; C : Node_Id) is
2659 begin
2660 -- Enumeration type case
2662 if Is_Enumeration_Type (Index_Typ) then
2663 Error_Msg_Name_1 :=
2664 Chars (Get_Enum_Lit_From_Pos (Index_Typ, Lo, Loc));
2665 Error_Msg_Name_2 :=
2666 Chars (Get_Enum_Lit_From_Pos (Index_Typ, Hi, Loc));
2668 if Lo = Hi then
2669 Error_Msg_N ("\\ %!", C);
2670 else
2671 Error_Msg_N ("\\ % .. %!", C);
2672 end if;
2674 -- Integer types case
2676 else
2677 Error_Msg_Uint_1 := Lo;
2678 Error_Msg_Uint_2 := Hi;
2680 if Lo = Hi then
2681 Error_Msg_N ("\\ ^!", C);
2682 else
2683 Error_Msg_N ("\\ ^ .. ^!", C);
2684 end if;
2685 end if;
2686 end Output_Bad_Choices;
2688 -- Start of processing for Check_Choices
2690 begin
2691 Sort_Case_Table (Table);
2693 -- First we do a quick linear loop to find out if we have
2694 -- any duplicates or missing entries (usually we have a
2695 -- legal aggregate, so this will get us out quickly).
2697 for J in 1 .. Nb_Discrete_Choices - 1 loop
2698 Hi_Val := Expr_Value (Table (J).Hi);
2699 Lo_Val := Expr_Value (Table (J + 1).Lo);
2701 if Lo_Val <= Hi_Val
2702 or else (Lo_Val > Hi_Val + 1
2703 and then not Others_Present)
2704 then
2705 Missing_Or_Duplicates := True;
2706 exit;
2707 end if;
2708 end loop;
2710 -- If we have missing or duplicate entries, first fill in
2711 -- the Highest entries to make life easier in the following
2712 -- loops to detect bad entries.
2714 if Missing_Or_Duplicates then
2715 Table (1).Highest := Expr_Value (Table (1).Hi);
2717 for J in 2 .. Nb_Discrete_Choices loop
2718 Table (J).Highest :=
2719 UI_Max
2720 (Table (J - 1).Highest, Expr_Value (Table (J).Hi));
2721 end loop;
2723 -- Loop through table entries to find duplicate indexes
2725 for J in 2 .. Nb_Discrete_Choices loop
2726 Lo_Val := Expr_Value (Table (J).Lo);
2727 Hi_Val := Expr_Value (Table (J).Hi);
2729 -- Case where we have duplicates (the lower bound of
2730 -- this choice is less than or equal to the highest
2731 -- high bound found so far).
2733 if Lo_Val <= Table (J - 1).Highest then
2735 -- We move backwards looking for duplicates. We can
2736 -- abandon this loop as soon as we reach a choice
2737 -- highest value that is less than Lo_Val.
2739 for K in reverse 1 .. J - 1 loop
2740 exit when Table (K).Highest < Lo_Val;
2742 -- Here we may have duplicates between entries
2743 -- for K and J. Get range of duplicates.
2745 Lo_Dup :=
2746 UI_Max (Lo_Val, Expr_Value (Table (K).Lo));
2747 Hi_Dup :=
2748 UI_Min (Hi_Val, Expr_Value (Table (K).Hi));
2750 -- Nothing to do if duplicate range is null
2752 if Lo_Dup > Hi_Dup then
2753 null;
2755 -- Otherwise place proper message
2757 else
2758 -- We place message on later choice, with a
2759 -- line reference to the earlier choice.
2761 if Sloc (Table (J).Choice) <
2762 Sloc (Table (K).Choice)
2763 then
2764 Choice := Table (K).Choice;
2765 Error_Msg_Sloc := Sloc (Table (J).Choice);
2766 else
2767 Choice := Table (J).Choice;
2768 Error_Msg_Sloc := Sloc (Table (K).Choice);
2769 end if;
2771 if Lo_Dup = Hi_Dup then
2772 Error_Msg_N
2773 ("index value in array aggregate "
2774 & "duplicates the one given#!", Choice);
2775 else
2776 Error_Msg_N
2777 ("index values in array aggregate "
2778 & "duplicate those given#!", Choice);
2779 end if;
2781 Output_Bad_Choices (Lo_Dup, Hi_Dup, Choice);
2782 end if;
2783 end loop;
2784 end if;
2785 end loop;
2787 -- Loop through entries in table to find missing indexes.
2788 -- Not needed if others, since missing impossible.
2790 if not Others_Present then
2791 for J in 2 .. Nb_Discrete_Choices loop
2792 Lo_Val := Expr_Value (Table (J).Lo);
2793 Hi_Val := Table (J - 1).Highest;
2795 if Lo_Val > Hi_Val + 1 then
2797 declare
2798 Error_Node : Node_Id;
2800 begin
2801 -- If the choice is the bound of a range in
2802 -- a subtype indication, it is not in the
2803 -- source lists for the aggregate itself, so
2804 -- post the error on the aggregate. Otherwise
2805 -- post it on choice itself.
2807 Choice := Table (J).Choice;
2809 if Is_List_Member (Choice) then
2810 Error_Node := Choice;
2811 else
2812 Error_Node := N;
2813 end if;
2815 if Hi_Val + 1 = Lo_Val - 1 then
2816 Error_Msg_N
2817 ("missing index value "
2818 & "in array aggregate!", Error_Node);
2819 else
2820 Error_Msg_N
2821 ("missing index values "
2822 & "in array aggregate!", Error_Node);
2823 end if;
2825 Output_Bad_Choices
2826 (Hi_Val + 1, Lo_Val - 1, Error_Node);
2827 end;
2828 end if;
2829 end loop;
2830 end if;
2832 -- If either missing or duplicate values, return failure
2834 Set_Etype (N, Any_Composite);
2835 return Failure;
2836 end if;
2837 end Check_Choices;
2838 end if;
2840 if Has_Iterator_Specifications then
2841 -- Bounds will be determined dynamically.
2843 return Success;
2844 end if;
2846 -- STEP 2 (B): Compute aggregate bounds and min/max choices values
2848 if Nb_Discrete_Choices > 0 then
2849 Choices_Low := Table (1).Lo;
2850 Choices_High := Table (Nb_Discrete_Choices).Hi;
2851 end if;
2853 -- If Others is present, then bounds of aggregate come from the
2854 -- index constraint (not the choices in the aggregate itself).
2856 if Others_Present then
2857 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2859 -- Abandon processing if either bound is already signalled as
2860 -- an error (prevents junk cascaded messages and blow ups).
2862 if Nkind (Aggr_Low) = N_Error
2863 or else
2864 Nkind (Aggr_High) = N_Error
2865 then
2866 return False;
2867 end if;
2869 -- No others clause present
2871 else
2872 -- Special processing if others allowed and not present. This
2873 -- means that the bounds of the aggregate come from the index
2874 -- constraint (and the length must match).
2876 if Others_Allowed then
2877 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2879 -- Abandon processing if either bound is already signalled
2880 -- as an error (stop junk cascaded messages and blow ups).
2882 if Nkind (Aggr_Low) = N_Error
2883 or else
2884 Nkind (Aggr_High) = N_Error
2885 then
2886 return False;
2887 end if;
2889 -- If others allowed, and no others present, then the array
2890 -- should cover all index values. If it does not, we will
2891 -- get a length check warning, but there is two cases where
2892 -- an additional warning is useful:
2894 -- If we have no positional components, and the length is
2895 -- wrong (which we can tell by others being allowed with
2896 -- missing components), and the index type is an enumeration
2897 -- type, then issue appropriate warnings about these missing
2898 -- components. They are only warnings, since the aggregate
2899 -- is fine, it's just the wrong length. We skip this check
2900 -- for standard character types (since there are no literals
2901 -- and it is too much trouble to concoct them), and also if
2902 -- any of the bounds have values that are not known at
2903 -- compile time.
2905 -- Another case warranting a warning is when the length
2906 -- is right, but as above we have an index type that is
2907 -- an enumeration, and the bounds do not match. This is a
2908 -- case where dubious sliding is allowed and we generate a
2909 -- warning that the bounds do not match.
2911 if No (Expressions (N))
2912 and then Nkind (Index) = N_Range
2913 and then Is_Enumeration_Type (Etype (Index))
2914 and then not Is_Standard_Character_Type (Etype (Index))
2915 and then Compile_Time_Known_Value (Aggr_Low)
2916 and then Compile_Time_Known_Value (Aggr_High)
2917 and then Compile_Time_Known_Value (Choices_Low)
2918 and then Compile_Time_Known_Value (Choices_High)
2919 then
2920 -- If any of the expressions or range bounds in choices
2921 -- have semantic errors, then do not attempt further
2922 -- resolution, to prevent cascaded errors.
2924 if Errors_Posted_On_Choices then
2925 return Failure;
2926 end if;
2928 declare
2929 ALo : constant Node_Id := Expr_Value_E (Aggr_Low);
2930 AHi : constant Node_Id := Expr_Value_E (Aggr_High);
2931 CLo : constant Node_Id := Expr_Value_E (Choices_Low);
2932 CHi : constant Node_Id := Expr_Value_E (Choices_High);
2934 Ent : Entity_Id;
2936 begin
2937 -- Warning case 1, missing values at start/end. Only
2938 -- do the check if the number of entries is too small.
2940 if (Enumeration_Pos (CHi) - Enumeration_Pos (CLo))
2942 (Enumeration_Pos (AHi) - Enumeration_Pos (ALo))
2943 then
2944 Error_Msg_N
2945 ("missing index value(s) in array aggregate??",
2948 -- Output missing value(s) at start
2950 if Chars (ALo) /= Chars (CLo) then
2951 Ent := Prev (CLo);
2953 if Chars (ALo) = Chars (Ent) then
2954 Error_Msg_Name_1 := Chars (ALo);
2955 Error_Msg_N ("\ %??", N);
2956 else
2957 Error_Msg_Name_1 := Chars (ALo);
2958 Error_Msg_Name_2 := Chars (Ent);
2959 Error_Msg_N ("\ % .. %??", N);
2960 end if;
2961 end if;
2963 -- Output missing value(s) at end
2965 if Chars (AHi) /= Chars (CHi) then
2966 Ent := Next (CHi);
2968 if Chars (AHi) = Chars (Ent) then
2969 Error_Msg_Name_1 := Chars (Ent);
2970 Error_Msg_N ("\ %??", N);
2971 else
2972 Error_Msg_Name_1 := Chars (Ent);
2973 Error_Msg_Name_2 := Chars (AHi);
2974 Error_Msg_N ("\ % .. %??", N);
2975 end if;
2976 end if;
2978 -- Warning case 2, dubious sliding. The First_Subtype
2979 -- test distinguishes between a constrained type where
2980 -- sliding is not allowed (so we will get a warning
2981 -- later that Constraint_Error will be raised), and
2982 -- the unconstrained case where sliding is permitted.
2984 elsif (Enumeration_Pos (CHi) - Enumeration_Pos (CLo))
2986 (Enumeration_Pos (AHi) - Enumeration_Pos (ALo))
2987 and then Chars (ALo) /= Chars (CLo)
2988 and then
2989 not Is_Constrained (First_Subtype (Etype (N)))
2990 then
2991 Error_Msg_N
2992 ("bounds of aggregate do not match target??", N);
2993 end if;
2994 end;
2995 end if;
2996 end if;
2998 -- If no others, aggregate bounds come from aggregate
3000 Aggr_Low := Choices_Low;
3001 Aggr_High := Choices_High;
3002 end if;
3003 end Step_2;
3005 -- STEP 3: Process positional components
3007 else
3008 -- STEP 3 (A): Process positional elements
3010 Expr := First (Expressions (N));
3011 Nb_Elements := Uint_0;
3012 while Present (Expr) loop
3013 Nb_Elements := Nb_Elements + 1;
3015 -- Ada 2005 (AI-231)
3017 if Ada_Version >= Ada_2005 and then Known_Null (Expr) then
3018 Check_Can_Never_Be_Null (Etype (N), Expr);
3019 end if;
3021 if not Resolve_Aggr_Expr (Expr, Single_Elmt => True) then
3022 return Failure;
3023 end if;
3025 -- Check incorrect use of dynamically tagged expression
3027 if Is_Tagged_Type (Etype (Expr)) then
3028 Check_Dynamically_Tagged_Expression
3029 (Expr => Expr,
3030 Typ => Component_Type (Etype (N)),
3031 Related_Nod => N);
3032 end if;
3034 Next (Expr);
3035 end loop;
3037 if Others_Present then
3038 Assoc := Last (Component_Associations (N));
3040 -- Ada 2005 (AI-231)
3042 if Ada_Version >= Ada_2005 and then Known_Null (Assoc) then
3043 Check_Can_Never_Be_Null (Etype (N), Expression (Assoc));
3044 end if;
3046 -- Ada 2005 (AI-287): In case of default initialized component,
3047 -- we delay the resolution to the expansion phase.
3049 if Box_Present (Assoc) then
3051 -- Ada 2005 (AI-287): In case of default initialization of a
3052 -- component the expander will generate calls to the
3053 -- corresponding initialization subprogram. We need to call
3054 -- Resolve_Aggr_Expr to check the rules about
3055 -- dimensionality.
3057 if not Resolve_Aggr_Expr (Assoc, Single_Elmt => False) then
3058 return Failure;
3059 end if;
3061 elsif not Resolve_Aggr_Expr (Expression (Assoc),
3062 Single_Elmt => False)
3063 then
3064 return Failure;
3066 -- Check incorrect use of dynamically tagged expression. The
3067 -- expression of the others choice has not been resolved yet.
3068 -- In order to diagnose the semantic error we create a duplicate
3069 -- tree to analyze it and perform the check.
3071 elsif Nkind (Assoc) /= N_Iterated_Component_Association then
3072 declare
3073 Save_Analysis : constant Boolean := Full_Analysis;
3074 Expr : constant Node_Id :=
3075 New_Copy_Tree (Expression (Assoc));
3077 begin
3078 Expander_Mode_Save_And_Set (False);
3079 Full_Analysis := False;
3080 Analyze (Expr);
3081 Full_Analysis := Save_Analysis;
3082 Expander_Mode_Restore;
3084 if Is_Tagged_Type (Etype (Expr)) then
3085 Check_Dynamically_Tagged_Expression
3086 (Expr => Expr,
3087 Typ => Component_Type (Etype (N)),
3088 Related_Nod => N);
3089 end if;
3090 end;
3091 end if;
3092 end if;
3094 -- STEP 3 (B): Compute the aggregate bounds
3096 if Others_Present then
3097 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
3099 else
3100 if Others_Allowed then
3101 Get_Index_Bounds (Index_Constr, Aggr_Low, Discard);
3102 else
3103 Aggr_Low := Index_Typ_Low;
3104 end if;
3106 Aggr_High := Add (Nb_Elements - 1, To => Aggr_Low);
3107 Check_Bound (Index_Base_High, Aggr_High);
3108 end if;
3109 end if;
3111 -- STEP 4: Perform static aggregate checks and save the bounds
3113 -- Check (A)
3115 Check_Bounds (Index_Typ_Low, Index_Typ_High, Aggr_Low, Aggr_High);
3116 Check_Bounds (Index_Base_Low, Index_Base_High, Aggr_Low, Aggr_High);
3118 -- Check (B)
3120 if Others_Present and then Nb_Discrete_Choices > 0 then
3121 Check_Bounds (Aggr_Low, Aggr_High, Choices_Low, Choices_High);
3122 Check_Bounds (Index_Typ_Low, Index_Typ_High,
3123 Choices_Low, Choices_High);
3124 Check_Bounds (Index_Base_Low, Index_Base_High,
3125 Choices_Low, Choices_High);
3127 -- Check (C)
3129 elsif Others_Present and then Nb_Elements > 0 then
3130 Check_Length (Aggr_Low, Aggr_High, Nb_Elements);
3131 Check_Length (Index_Typ_Low, Index_Typ_High, Nb_Elements);
3132 Check_Length (Index_Base_Low, Index_Base_High, Nb_Elements);
3133 end if;
3135 if Raises_Constraint_Error (Aggr_Low)
3136 or else Raises_Constraint_Error (Aggr_High)
3137 then
3138 Set_Raises_Constraint_Error (N);
3139 end if;
3141 Aggr_Low := Duplicate_Subexpr (Aggr_Low);
3143 -- Do not duplicate Aggr_High if Aggr_High = Aggr_Low + Nb_Elements
3144 -- since the addition node returned by Add is not yet analyzed. Attach
3145 -- to tree and analyze first. Reset analyzed flag to ensure it will get
3146 -- analyzed when it is a literal bound whose type must be properly set.
3148 if Others_Present or else Nb_Discrete_Choices > 0 then
3149 Aggr_High := Duplicate_Subexpr (Aggr_High);
3151 if Etype (Aggr_High) = Universal_Integer then
3152 Set_Analyzed (Aggr_High, False);
3153 end if;
3154 end if;
3156 -- If the aggregate already has bounds attached to it, it means this is
3157 -- a positional aggregate created as an optimization by
3158 -- Exp_Aggr.Convert_To_Positional, so we don't want to change those
3159 -- bounds.
3161 if Present (Aggregate_Bounds (N))
3162 and then not Others_Allowed
3163 and then not Comes_From_Source (N)
3164 then
3165 Aggr_Low := Low_Bound (Aggregate_Bounds (N));
3166 Aggr_High := High_Bound (Aggregate_Bounds (N));
3167 end if;
3169 Set_Aggregate_Bounds
3170 (N, Make_Range (Loc, Low_Bound => Aggr_Low, High_Bound => Aggr_High));
3172 -- The bounds may contain expressions that must be inserted upwards.
3173 -- Attach them fully to the tree. After analysis, remove side effects
3174 -- from upper bound, if still needed.
3176 Set_Parent (Aggregate_Bounds (N), N);
3177 Analyze_And_Resolve (Aggregate_Bounds (N), Index_Typ);
3178 Check_Unset_Reference (Aggregate_Bounds (N));
3180 if not Others_Present and then Nb_Discrete_Choices = 0 then
3181 Set_High_Bound
3182 (Aggregate_Bounds (N),
3183 Duplicate_Subexpr (High_Bound (Aggregate_Bounds (N))));
3184 end if;
3186 -- Check the dimensions of each component in the array aggregate
3188 Analyze_Dimension_Array_Aggregate (N, Component_Typ);
3190 return Success;
3191 end Resolve_Array_Aggregate;
3193 ---------------------------------
3194 -- Resolve_Container_Aggregate --
3195 ---------------------------------
3197 procedure Resolve_Container_Aggregate (N : Node_Id; Typ : Entity_Id) is
3198 procedure Resolve_Iterated_Association
3199 (Comp : Node_Id;
3200 Key_Type : Entity_Id;
3201 Elmt_Type : Entity_Id);
3202 -- Resolve choices and expression in an iterated component association
3203 -- or an iterated element association, which has a key_expression.
3204 -- This is similar but not identical to the handling of this construct
3205 -- in an array aggregate.
3206 -- For a named container, the type of each choice must be compatible
3207 -- with the key type. For a positional container, the choice must be
3208 -- a subtype indication or an iterator specification that determines
3209 -- an element type.
3211 Asp : constant Node_Id := Find_Value_Of_Aspect (Typ, Aspect_Aggregate);
3213 Empty_Subp : Node_Id := Empty;
3214 Add_Named_Subp : Node_Id := Empty;
3215 Add_Unnamed_Subp : Node_Id := Empty;
3216 New_Indexed_Subp : Node_Id := Empty;
3217 Assign_Indexed_Subp : Node_Id := Empty;
3219 ----------------------------------
3220 -- Resolve_Iterated_Association --
3221 ----------------------------------
3223 procedure Resolve_Iterated_Association
3224 (Comp : Node_Id;
3225 Key_Type : Entity_Id;
3226 Elmt_Type : Entity_Id)
3228 Loc : constant Source_Ptr := Sloc (N);
3229 Choice : Node_Id;
3230 Copy : Node_Id;
3231 Ent : Entity_Id;
3232 Expr : Node_Id;
3233 Key_Expr : Node_Id;
3234 Id : Entity_Id;
3235 Id_Name : Name_Id;
3236 Typ : Entity_Id := Empty;
3238 begin
3239 Error_Msg_Ada_2022_Feature ("iterated component", Loc);
3241 -- If this is an Iterated_Element_Association then either a
3242 -- an Iterator_Specification or a Loop_Parameter specification
3243 -- is present. In both cases a Key_Expression is present.
3245 if Nkind (Comp) = N_Iterated_Element_Association then
3247 -- Create a temporary scope to avoid some modifications from
3248 -- escaping the Analyze call below. The original Tree will be
3249 -- reanalyzed later.
3251 Ent := New_Internal_Entity
3252 (E_Loop, Current_Scope, Sloc (Comp), 'L');
3253 Set_Etype (Ent, Standard_Void_Type);
3254 Set_Parent (Ent, Parent (Comp));
3255 Push_Scope (Ent);
3257 if Present (Loop_Parameter_Specification (Comp)) then
3258 Copy := Copy_Separate_Tree (Comp);
3260 Analyze
3261 (Loop_Parameter_Specification (Copy));
3263 Id_Name := Chars (Defining_Identifier
3264 (Loop_Parameter_Specification (Comp)));
3265 else
3266 Copy := Copy_Separate_Tree (Iterator_Specification (Comp));
3267 Analyze (Copy);
3269 Id_Name := Chars (Defining_Identifier
3270 (Iterator_Specification (Comp)));
3271 end if;
3273 -- Key expression must have the type of the key. We analyze
3274 -- a copy of the original expression, because it will be
3275 -- reanalyzed and copied as needed during expansion of the
3276 -- corresponding loop.
3278 Key_Expr := Key_Expression (Comp);
3279 Analyze_And_Resolve (New_Copy_Tree (Key_Expr), Key_Type);
3280 End_Scope;
3282 Typ := Key_Type;
3284 elsif Present (Iterator_Specification (Comp)) then
3285 -- Create a temporary scope to avoid some modifications from
3286 -- escaping the Analyze call below. The original Tree will be
3287 -- reanalyzed later.
3289 Ent := New_Internal_Entity
3290 (E_Loop, Current_Scope, Sloc (Comp), 'L');
3291 Set_Etype (Ent, Standard_Void_Type);
3292 Set_Parent (Ent, Parent (Comp));
3293 Push_Scope (Ent);
3295 Copy := Copy_Separate_Tree (Iterator_Specification (Comp));
3296 Id_Name :=
3297 Chars (Defining_Identifier (Iterator_Specification (Comp)));
3299 Preanalyze (Copy);
3301 End_Scope;
3303 Typ := Etype (Defining_Identifier (Copy));
3305 else
3306 Choice := First (Discrete_Choices (Comp));
3308 while Present (Choice) loop
3309 Analyze (Choice);
3311 -- Choice can be a subtype name, a range, or an expression
3313 if Is_Entity_Name (Choice)
3314 and then Is_Type (Entity (Choice))
3315 and then Base_Type (Entity (Choice)) = Base_Type (Key_Type)
3316 then
3317 null;
3319 elsif Present (Key_Type) then
3320 Analyze_And_Resolve (Choice, Key_Type);
3321 Typ := Key_Type;
3322 else
3323 Typ := Etype (Choice); -- assume unique for now
3324 end if;
3326 Next (Choice);
3327 end loop;
3329 Id_Name := Chars (Defining_Identifier (Comp));
3330 end if;
3332 -- Create a scope in which to introduce an index, which is usually
3333 -- visible in the expression for the component, and needed for its
3334 -- analysis.
3336 Id := Make_Defining_Identifier (Sloc (Comp), Id_Name);
3337 Ent := New_Internal_Entity (E_Loop,
3338 Current_Scope, Sloc (Comp), 'L');
3339 Set_Etype (Ent, Standard_Void_Type);
3340 Set_Parent (Ent, Parent (Comp));
3341 Push_Scope (Ent);
3343 -- Insert and decorate the loop variable in the current scope.
3344 -- The expression has to be analyzed once the loop variable is
3345 -- directly visible. Mark the variable as referenced to prevent
3346 -- spurious warnings, given that subsequent uses of its name in the
3347 -- expression will reference the internal (synonym) loop variable.
3349 Enter_Name (Id);
3351 pragma Assert (Present (Typ));
3352 Set_Etype (Id, Typ);
3354 Mutate_Ekind (Id, E_Variable);
3355 Set_Is_Not_Self_Hidden (Id);
3356 Set_Scope (Id, Ent);
3357 Set_Referenced (Id);
3359 -- Analyze a copy of the expression, to verify legality. We use
3360 -- a copy because the expression will be analyzed anew when the
3361 -- enclosing aggregate is expanded, and the construct is rewritten
3362 -- as a loop with a new index variable.
3364 Expr := New_Copy_Tree (Expression (Comp));
3365 Preanalyze_And_Resolve (Expr, Elmt_Type);
3366 End_Scope;
3368 end Resolve_Iterated_Association;
3370 -- Start of processing for Resolve_Container_Aggregate
3372 begin
3373 pragma Assert (Nkind (Asp) = N_Aggregate);
3375 Set_Etype (N, Typ);
3376 Parse_Aspect_Aggregate (Asp,
3377 Empty_Subp, Add_Named_Subp, Add_Unnamed_Subp,
3378 New_Indexed_Subp, Assign_Indexed_Subp);
3380 if Present (Add_Unnamed_Subp)
3381 and then No (New_Indexed_Subp)
3382 and then Present (Etype (Add_Unnamed_Subp))
3383 and then Etype (Add_Unnamed_Subp) /= Any_Type
3384 then
3385 declare
3386 Elmt_Type : constant Entity_Id :=
3387 Etype (Next_Formal
3388 (First_Formal (Entity (Add_Unnamed_Subp))));
3389 Comp : Node_Id;
3391 begin
3392 if Present (Expressions (N)) then
3393 -- positional aggregate
3395 Comp := First (Expressions (N));
3396 while Present (Comp) loop
3397 Analyze_And_Resolve (Comp, Elmt_Type);
3398 Next (Comp);
3399 end loop;
3400 end if;
3402 -- Empty aggregate, to be replaced by Empty during
3403 -- expansion, or iterated component association.
3405 if Present (Component_Associations (N)) then
3406 declare
3407 Comp : Node_Id := First (Component_Associations (N));
3408 begin
3409 while Present (Comp) loop
3410 if Nkind (Comp) /=
3411 N_Iterated_Component_Association
3412 then
3413 Error_Msg_N ("illegal component association "
3414 & "for unnamed container aggregate", Comp);
3415 return;
3416 else
3417 Resolve_Iterated_Association
3418 (Comp, Empty, Elmt_Type);
3419 end if;
3421 Next (Comp);
3422 end loop;
3423 end;
3424 end if;
3425 end;
3427 elsif Present (Add_Named_Subp)
3428 and then Etype (Add_Named_Subp) /= Any_Type
3429 then
3430 declare
3431 -- Retrieves types of container, key, and element from the
3432 -- specified insertion procedure.
3434 Container : constant Entity_Id :=
3435 First_Formal (Entity (Add_Named_Subp));
3436 Key_Type : constant Entity_Id := Etype (Next_Formal (Container));
3437 Elmt_Type : constant Entity_Id :=
3438 Etype (Next_Formal (Next_Formal (Container)));
3439 Comp : Node_Id;
3440 Choice : Node_Id;
3442 begin
3443 Comp := First (Component_Associations (N));
3444 while Present (Comp) loop
3445 if Nkind (Comp) = N_Component_Association then
3446 Choice := First (Choices (Comp));
3448 while Present (Choice) loop
3449 Analyze_And_Resolve (Choice, Key_Type);
3450 if not Is_Static_Expression (Choice) then
3451 Error_Msg_N ("choice must be static", Choice);
3452 end if;
3454 Next (Choice);
3455 end loop;
3457 Analyze_And_Resolve (Expression (Comp), Elmt_Type);
3459 elsif Nkind (Comp) in
3460 N_Iterated_Component_Association |
3461 N_Iterated_Element_Association
3462 then
3463 Resolve_Iterated_Association
3464 (Comp, Key_Type, Elmt_Type);
3465 end if;
3467 Next (Comp);
3468 end loop;
3469 end;
3471 elsif Present (Assign_Indexed_Subp)
3472 and then Etype (Assign_Indexed_Subp) /= Any_Type
3473 then
3474 -- Indexed Aggregate. Positional or indexed component
3475 -- can be present, but not both. Choices must be static
3476 -- values or ranges with static bounds.
3478 declare
3479 Container : constant Entity_Id :=
3480 First_Formal (Entity (Assign_Indexed_Subp));
3481 Index_Type : constant Entity_Id := Etype (Next_Formal (Container));
3482 Comp_Type : constant Entity_Id :=
3483 Etype (Next_Formal (Next_Formal (Container)));
3484 Comp : Node_Id;
3485 Choice : Node_Id;
3486 Num_Choices : Nat := 0;
3488 Hi_Val : Uint;
3489 Lo_Val : Uint;
3490 begin
3491 if Present (Expressions (N)) then
3492 Comp := First (Expressions (N));
3493 while Present (Comp) loop
3494 Analyze_And_Resolve (Comp, Comp_Type);
3495 Next (Comp);
3496 end loop;
3497 end if;
3499 if Present (Component_Associations (N))
3500 and then not Is_Empty_List (Component_Associations (N))
3501 then
3502 if Present (Expressions (N))
3503 and then not Is_Empty_List (Expressions (N))
3504 then
3505 Error_Msg_N ("container aggregate cannot be "
3506 & "both positional and named", N);
3507 return;
3508 end if;
3510 Comp := First (Component_Associations (N));
3512 while Present (Comp) loop
3513 if Nkind (Comp) = N_Component_Association then
3514 Choice := First (Choices (Comp));
3516 while Present (Choice) loop
3517 Analyze_And_Resolve (Choice, Index_Type);
3518 Num_Choices := Num_Choices + 1;
3519 Next (Choice);
3520 end loop;
3522 Analyze_And_Resolve (Expression (Comp), Comp_Type);
3524 elsif Nkind (Comp) in
3525 N_Iterated_Component_Association |
3526 N_Iterated_Element_Association
3527 then
3528 Resolve_Iterated_Association
3529 (Comp, Index_Type, Comp_Type);
3530 Num_Choices := Num_Choices + 1;
3531 end if;
3533 Next (Comp);
3534 end loop;
3536 -- The component associations in an indexed aggregate
3537 -- must denote a contiguous set of static values. We
3538 -- build a table of values/ranges and sort it, as is done
3539 -- elsewhere for case statements and array aggregates.
3540 -- If the aggregate has a single iterated association it
3541 -- is allowed to be nonstatic and there is nothing to check.
3543 if Num_Choices > 1 then
3544 declare
3545 Table : Case_Table_Type (1 .. Num_Choices);
3546 No_Choice : Pos := 1;
3547 Lo, Hi : Node_Id;
3549 -- Traverse aggregate to determine size of needed table.
3550 -- Verify that bounds are static and that loops have no
3551 -- filters or key expressions.
3553 begin
3554 Comp := First (Component_Associations (N));
3555 while Present (Comp) loop
3556 if Nkind (Comp) = N_Iterated_Element_Association then
3557 if Present
3558 (Loop_Parameter_Specification (Comp))
3559 then
3560 if Present (Iterator_Filter
3561 (Loop_Parameter_Specification (Comp)))
3562 then
3563 Error_Msg_N
3564 ("iterator filter not allowed " &
3565 "in indexed aggregate", Comp);
3566 return;
3568 elsif Present (Key_Expression
3569 (Loop_Parameter_Specification (Comp)))
3570 then
3571 Error_Msg_N
3572 ("key expression not allowed " &
3573 "in indexed aggregate", Comp);
3574 return;
3575 end if;
3576 end if;
3577 else
3579 -- If Nkind is N_Iterated_Component_Association,
3580 -- this corresponds to an iterator_specification
3581 -- with a loop_parameter_specification, and we
3582 -- have to pick up Discrete_Choices. In this case
3583 -- there will be just one "choice", which will
3584 -- typically be a range.
3586 if Nkind (Comp) = N_Iterated_Component_Association
3587 then
3588 Choice := First (Discrete_Choices (Comp));
3590 -- Case where there's a list of choices
3592 else
3593 Choice := First (Choices (Comp));
3594 end if;
3596 while Present (Choice) loop
3597 Get_Index_Bounds (Choice, Lo, Hi);
3598 Table (No_Choice).Choice := Choice;
3599 Table (No_Choice).Lo := Lo;
3600 Table (No_Choice).Hi := Hi;
3602 -- Verify staticness of value or range
3604 if not Is_Static_Expression (Lo)
3605 or else not Is_Static_Expression (Hi)
3606 then
3607 Error_Msg_N
3608 ("nonstatic expression for index " &
3609 "for indexed aggregate", Choice);
3610 return;
3611 end if;
3613 No_Choice := No_Choice + 1;
3614 Next (Choice);
3615 end loop;
3616 end if;
3618 Next (Comp);
3619 end loop;
3621 Sort_Case_Table (Table);
3623 for J in 1 .. Num_Choices - 1 loop
3624 Hi_Val := Expr_Value (Table (J).Hi);
3625 Lo_Val := Expr_Value (Table (J + 1).Lo);
3627 if Lo_Val = Hi_Val then
3628 Error_Msg_N
3629 ("duplicate index in indexed aggregate",
3630 Table (J + 1).Choice);
3631 exit;
3633 elsif Lo_Val < Hi_Val then
3634 Error_Msg_N
3635 ("overlapping indices in indexed aggregate",
3636 Table (J + 1).Choice);
3637 exit;
3639 elsif Lo_Val > Hi_Val + 1 then
3640 Error_Msg_N
3641 ("missing index values", Table (J + 1).Choice);
3642 exit;
3643 end if;
3644 end loop;
3645 end;
3646 end if;
3647 end if;
3648 end;
3649 end if;
3650 end Resolve_Container_Aggregate;
3652 -----------------------------
3653 -- Resolve_Delta_Aggregate --
3654 -----------------------------
3656 procedure Resolve_Delta_Aggregate (N : Node_Id; Typ : Entity_Id) is
3657 Base : constant Node_Id := Expression (N);
3659 begin
3660 Error_Msg_Ada_2022_Feature ("delta aggregate", Sloc (N));
3662 if not Is_Composite_Type (Typ) then
3663 Error_Msg_N ("not a composite type", N);
3664 end if;
3666 Analyze_And_Resolve (Base, Typ);
3668 if Is_Array_Type (Typ) then
3669 -- For an array_delta_aggregate, the base_expression and each
3670 -- expression in every array_component_association shall be of a
3671 -- nonlimited type; RM 4.3.4(13/5). However, to prevent repeated
3672 -- errors we only check the base expression and not array component
3673 -- associations.
3675 if Is_Limited_Type (Etype (Base)) then
3676 Error_Msg_N
3677 ("array delta aggregate shall be of a nonlimited type", Base);
3678 Explain_Limited_Type (Etype (Base), Base);
3679 end if;
3681 Resolve_Delta_Array_Aggregate (N, Typ);
3682 else
3684 -- Delta aggregates for record types must use parentheses,
3685 -- not square brackets.
3687 if Is_Homogeneous_Aggregate (N) then
3688 Error_Msg_N
3689 ("delta aggregates for record types must use (), not '[']", N);
3690 end if;
3692 -- The base_expression of a record_delta_aggregate can be of a
3693 -- limited type only if it is newly constructed; RM 7.5(2.1/5).
3695 Check_Expr_OK_In_Limited_Aggregate (Base);
3697 Resolve_Delta_Record_Aggregate (N, Typ);
3698 end if;
3700 Set_Etype (N, Typ);
3701 end Resolve_Delta_Aggregate;
3703 -----------------------------------
3704 -- Resolve_Delta_Array_Aggregate --
3705 -----------------------------------
3707 procedure Resolve_Delta_Array_Aggregate (N : Node_Id; Typ : Entity_Id) is
3708 Deltas : constant List_Id := Component_Associations (N);
3709 Index_Type : constant Entity_Id := Etype (First_Index (Typ));
3711 Assoc : Node_Id;
3712 Choice : Node_Id;
3713 Expr : Node_Id;
3715 Deep_Choice_Seen : Boolean := False;
3717 begin
3718 Assoc := First (Deltas);
3719 while Present (Assoc) loop
3720 if Nkind (Assoc) = N_Iterated_Component_Association then
3721 Choice := First (Choice_List (Assoc));
3722 while Present (Choice) loop
3723 if Nkind (Choice) = N_Others_Choice then
3724 Error_Msg_N
3725 ("OTHERS not allowed in delta aggregate", Choice);
3727 elsif Nkind (Choice) = N_Subtype_Indication then
3728 Resolve_Discrete_Subtype_Indication
3729 (Choice, Base_Type (Index_Type));
3731 else
3732 Analyze_And_Resolve (Choice, Index_Type);
3733 end if;
3735 Next (Choice);
3736 end loop;
3738 declare
3739 Id : constant Entity_Id := Defining_Identifier (Assoc);
3740 Ent : constant Entity_Id :=
3741 New_Internal_Entity
3742 (E_Loop, Current_Scope, Sloc (Assoc), 'L');
3744 begin
3745 Set_Etype (Ent, Standard_Void_Type);
3746 Set_Parent (Ent, Assoc);
3747 Push_Scope (Ent);
3749 if No (Scope (Id)) then
3750 Set_Etype (Id, Index_Type);
3751 Mutate_Ekind (Id, E_Variable);
3752 Set_Is_Not_Self_Hidden (Id);
3753 Set_Scope (Id, Ent);
3754 end if;
3755 Enter_Name (Id);
3757 -- Resolve a copy of the expression, after setting
3758 -- its parent properly to preserve its context.
3760 Expr := New_Copy_Tree (Expression (Assoc));
3761 Set_Parent (Expr, Assoc);
3762 Analyze_And_Resolve (Expr, Component_Type (Typ));
3763 End_Scope;
3764 end;
3766 else
3767 Choice := First (Choice_List (Assoc));
3768 while Present (Choice) loop
3769 if Is_Deep_Choice (Choice, Typ) then
3770 pragma Assert (All_Extensions_Allowed);
3771 Deep_Choice_Seen := True;
3773 -- a deep delta aggregate
3774 Resolve_Deep_Delta_Assoc (Assoc, Typ);
3775 else
3776 Analyze (Choice);
3778 if Nkind (Choice) = N_Others_Choice then
3779 Error_Msg_N
3780 ("OTHERS not allowed in delta aggregate", Choice);
3782 elsif Is_Entity_Name (Choice)
3783 and then Is_Type (Entity (Choice))
3784 then
3785 -- Choice covers a range of values
3787 if Base_Type (Entity (Choice)) /=
3788 Base_Type (Index_Type)
3789 then
3790 Error_Msg_NE
3791 ("choice does not match index type of &",
3792 Choice, Typ);
3793 end if;
3795 elsif Nkind (Choice) = N_Subtype_Indication then
3796 Resolve_Discrete_Subtype_Indication
3797 (Choice, Base_Type (Index_Type));
3799 else
3800 Resolve (Choice, Index_Type);
3801 end if;
3802 end if;
3804 Next (Choice);
3805 end loop;
3807 -- For an array_delta_aggregate, the array_component_association
3808 -- shall not use the box symbol <>; RM 4.3.4(11/5).
3810 pragma Assert
3811 (Box_Present (Assoc) xor Present (Expression (Assoc)));
3813 if Box_Present (Assoc) then
3814 Error_Msg_N
3815 ("'<'> in array delta aggregate is not allowed", Assoc);
3816 elsif not Deep_Choice_Seen then
3817 Analyze_And_Resolve (Expression (Assoc), Component_Type (Typ));
3818 end if;
3819 end if;
3821 Next (Assoc);
3822 end loop;
3823 end Resolve_Delta_Array_Aggregate;
3825 ------------------------------------
3826 -- Resolve_Delta_Record_Aggregate --
3827 ------------------------------------
3829 procedure Resolve_Delta_Record_Aggregate (N : Node_Id; Typ : Entity_Id) is
3831 -- Variables used to verify that discriminant-dependent components
3832 -- appear in the same variant.
3834 Comp_Ref : Entity_Id := Empty; -- init to avoid warning
3835 Variant : Node_Id;
3837 procedure Check_Variant (Id : Node_Id);
3838 -- If a given component of the delta aggregate appears in a variant
3839 -- part, verify that it is within the same variant as that of previous
3840 -- specified variant components of the delta.
3842 function Get_Component_Type
3843 (Selector : Node_Id; Enclosing_Type : Entity_Id) return Entity_Id;
3844 -- Locate component with a given name and return its type.
3845 -- If none found then report error and return Empty.
3847 function Nested_In (V1 : Node_Id; V2 : Node_Id) return Boolean;
3848 -- Determine whether variant V1 is within variant V2
3850 function Variant_Depth (N : Node_Id) return Natural;
3851 -- Determine the distance of a variant to the enclosing type declaration
3853 --------------------
3854 -- Check_Variant --
3855 --------------------
3857 procedure Check_Variant (Id : Node_Id) is
3858 Comp : Entity_Id;
3859 Comp_Variant : Node_Id;
3861 begin
3862 if not Has_Discriminants (Typ) then
3863 return;
3864 end if;
3866 Comp := First_Entity (Typ);
3867 while Present (Comp) loop
3868 exit when Chars (Comp) = Chars (Id);
3869 Next_Component (Comp);
3870 end loop;
3872 -- Find the variant, if any, whose component list includes the
3873 -- component declaration.
3875 Comp_Variant := Parent (Parent (List_Containing (Parent (Comp))));
3876 if Nkind (Comp_Variant) = N_Variant then
3877 if No (Variant) then
3878 Variant := Comp_Variant;
3879 Comp_Ref := Comp;
3881 elsif Variant /= Comp_Variant then
3882 declare
3883 D1 : constant Integer := Variant_Depth (Variant);
3884 D2 : constant Integer := Variant_Depth (Comp_Variant);
3886 begin
3887 if D1 = D2
3888 or else
3889 (D1 > D2 and then not Nested_In (Variant, Comp_Variant))
3890 or else
3891 (D2 > D1 and then not Nested_In (Comp_Variant, Variant))
3892 then
3893 pragma Assert (Present (Comp_Ref));
3894 Error_Msg_Node_2 := Comp_Ref;
3895 Error_Msg_NE
3896 ("& and & appear in different variants", Id, Comp);
3898 -- Otherwise retain the deeper variant for subsequent tests
3900 elsif D2 > D1 then
3901 Variant := Comp_Variant;
3902 end if;
3903 end;
3904 end if;
3905 end if;
3906 end Check_Variant;
3908 ------------------------
3909 -- Get_Component_Type --
3910 ------------------------
3912 function Get_Component_Type
3913 (Selector : Node_Id; Enclosing_Type : Entity_Id) return Entity_Id
3915 Comp : Entity_Id;
3916 begin
3917 case Nkind (Selector) is
3918 when N_Selected_Component | N_Indexed_Component =>
3919 -- a deep delta aggregate choice
3921 declare
3922 Prefix_Type : constant Entity_Id :=
3923 Get_Component_Type (Prefix (Selector), Enclosing_Type);
3924 begin
3925 if No (Prefix_Type) then
3926 pragma Assert (Serious_Errors_Detected > 0);
3927 return Empty;
3928 end if;
3930 -- Set the type of the prefix for GNATprove
3932 Set_Etype (Prefix (Selector), Prefix_Type);
3934 if Nkind (Selector) = N_Selected_Component then
3935 return Get_Component_Type
3936 (Selector_Name (Selector),
3937 Enclosing_Type => Prefix_Type);
3938 elsif not Is_Array_Type (Prefix_Type) then
3939 Error_Msg_NE
3940 ("type& is not an array type",
3941 Selector, Prefix_Type);
3942 elsif Number_Dimensions (Prefix_Type) /= 1 then
3943 Error_Msg_NE
3944 ("array type& not one-dimensional",
3945 Selector, Prefix_Type);
3946 elsif List_Length (Expressions (Selector)) /= 1 then
3947 Error_Msg_NE
3948 ("wrong number of indices for array type&",
3949 Selector, Prefix_Type);
3950 else
3951 Analyze_And_Resolve
3952 (First (Expressions (Selector)),
3953 Etype (First_Index (Prefix_Type)));
3954 return Component_Type (Prefix_Type);
3955 end if;
3956 end;
3958 when others =>
3959 null;
3960 end case;
3962 Comp := First_Entity (Enclosing_Type);
3963 while Present (Comp) loop
3964 if Chars (Comp) = Chars (Selector) then
3965 if Ekind (Comp) = E_Discriminant then
3966 Error_Msg_N ("delta cannot apply to discriminant", Selector);
3967 end if;
3969 Set_Entity (Selector, Comp);
3970 Set_Etype (Selector, Etype (Comp));
3972 return Etype (Comp);
3973 end if;
3975 Next_Entity (Comp);
3976 end loop;
3978 Error_Msg_NE
3979 ("type& has no component with this name", Selector, Enclosing_Type);
3980 return Empty;
3981 end Get_Component_Type;
3983 ---------------
3984 -- Nested_In --
3985 ---------------
3987 function Nested_In (V1, V2 : Node_Id) return Boolean is
3988 Par : Node_Id;
3990 begin
3991 Par := Parent (V1);
3992 while Nkind (Par) /= N_Full_Type_Declaration loop
3993 if Par = V2 then
3994 return True;
3995 end if;
3997 Par := Parent (Par);
3998 end loop;
4000 return False;
4001 end Nested_In;
4003 -------------------
4004 -- Variant_Depth --
4005 -------------------
4007 function Variant_Depth (N : Node_Id) return Natural is
4008 Depth : Natural;
4009 Par : Node_Id;
4011 begin
4012 Depth := 0;
4013 Par := Parent (N);
4014 while Nkind (Par) /= N_Full_Type_Declaration loop
4015 Depth := Depth + 1;
4016 Par := Parent (Par);
4017 end loop;
4019 return Depth;
4020 end Variant_Depth;
4022 -- Local variables
4024 Deltas : constant List_Id := Component_Associations (N);
4026 Assoc : Node_Id;
4027 Choice : Node_Id;
4028 Comp_Type : Entity_Id := Empty; -- init to avoid warning
4029 Deep_Choice : Boolean;
4031 -- Start of processing for Resolve_Delta_Record_Aggregate
4033 begin
4034 Variant := Empty;
4036 Assoc := First (Deltas);
4037 while Present (Assoc) loop
4038 Choice := First (Choice_List (Assoc));
4039 while Present (Choice) loop
4040 Deep_Choice := Nkind (Choice) /= N_Identifier;
4041 if Deep_Choice then
4042 Error_Msg_GNAT_Extension
4043 ("deep delta aggregate", Sloc (Choice));
4044 end if;
4046 Comp_Type := Get_Component_Type
4047 (Selector => Choice, Enclosing_Type => Typ);
4049 -- Set the type of the choice for GNATprove
4051 if Deep_Choice then
4052 Set_Etype (Choice, Comp_Type);
4053 end if;
4055 if Present (Comp_Type) then
4056 if not Deep_Choice then
4057 -- ??? Not clear yet how RM 4.3.1(17.7) applies to a
4058 -- deep delta aggregate.
4059 Check_Variant (Choice);
4060 end if;
4061 else
4062 Comp_Type := Any_Type;
4063 end if;
4065 Next (Choice);
4066 end loop;
4068 pragma Assert (Present (Comp_Type));
4070 -- A record_component_association in record_delta_aggregate shall not
4071 -- use the box compound delimiter <> rather than an expression; see
4072 -- RM 4.3.1(17.3/5).
4074 pragma Assert (Present (Expression (Assoc)) xor Box_Present (Assoc));
4076 if Box_Present (Assoc) then
4077 Error_Msg_N
4078 ("'<'> in record delta aggregate is not allowed", Assoc);
4079 else
4080 Analyze_And_Resolve (Expression (Assoc), Comp_Type);
4082 -- The expression must not be of a limited type; RM 4.3.1(17.4/5)
4084 if Is_Limited_Type (Etype (Expression (Assoc))) then
4085 Error_Msg_N
4086 ("expression of a limited type in record delta aggregate " &
4087 "is not allowed",
4088 Expression (Assoc));
4089 end if;
4090 end if;
4092 Next (Assoc);
4093 end loop;
4094 end Resolve_Delta_Record_Aggregate;
4096 ------------------------------
4097 -- Resolve_Deep_Delta_Assoc --
4098 ------------------------------
4100 procedure Resolve_Deep_Delta_Assoc (N : Node_Id; Typ : Entity_Id) is
4101 Choice : constant Node_Id := First (Choice_List (N));
4102 Enclosing_Type : Entity_Id := Typ;
4104 procedure Resolve_Choice_Prefix
4105 (Choice_Prefix : Node_Id; Enclosing_Type : in out Entity_Id);
4106 -- Recursively analyze selectors. Enclosing_Type is set to
4107 -- type of the last component.
4109 ---------------------------
4110 -- Resolve_Choice_Prefix --
4111 ---------------------------
4113 procedure Resolve_Choice_Prefix
4114 (Choice_Prefix : Node_Id; Enclosing_Type : in out Entity_Id)
4116 Selector : Node_Id := Choice_Prefix;
4117 begin
4118 if not Is_Root_Prefix_Of_Deep_Choice (Choice_Prefix) then
4119 Resolve_Choice_Prefix (Prefix (Choice_Prefix), Enclosing_Type);
4121 if Nkind (Choice_Prefix) = N_Selected_Component then
4122 Selector := Selector_Name (Choice_Prefix);
4123 else
4124 pragma Assert (Nkind (Choice_Prefix) = N_Indexed_Component);
4125 Selector := First (Expressions (Choice_Prefix));
4126 end if;
4127 end if;
4129 if Is_Array_Type (Enclosing_Type) then
4130 Analyze_And_Resolve (Selector,
4131 Etype (First_Index (Enclosing_Type)));
4132 Enclosing_Type := Component_Type (Enclosing_Type);
4133 else
4134 declare
4135 Comp : Entity_Id := First_Entity (Enclosing_Type);
4136 Found : Boolean := False;
4137 begin
4138 while Present (Comp) and not Found loop
4139 if Chars (Comp) = Chars (Selector) then
4140 if Ekind (Comp) = E_Discriminant then
4141 Error_Msg_N ("delta cannot apply to discriminant",
4142 Selector);
4143 end if;
4144 Found := True;
4145 Set_Entity (Selector, Comp);
4146 Set_Etype (Selector, Etype (Comp));
4147 Set_Analyzed (Selector);
4148 Enclosing_Type := Etype (Comp);
4149 else
4150 Next_Entity (Comp);
4151 end if;
4152 end loop;
4153 if not Found then
4154 Error_Msg_NE
4155 ("type& has no component with this name",
4156 Selector, Enclosing_Type);
4157 end if;
4158 end;
4159 end if;
4161 -- Set the type of the prefix for GNATprove, except for the root
4162 -- prefix, whose type is already the expected one for a record
4163 -- delta aggregate, or the type of the array index for an
4164 -- array delta aggregate (the only case here really since
4165 -- Resolve_Deep_Delta_Assoc is only called for array delta
4166 -- aggregates).
4168 if Selector /= Choice_Prefix then
4169 Set_Etype (Choice_Prefix, Enclosing_Type);
4170 end if;
4171 end Resolve_Choice_Prefix;
4172 begin
4173 declare
4174 Unimplemented : exception; -- TEMPORARY
4175 begin
4176 if Present (Next (Choice)) then
4177 raise Unimplemented;
4178 end if;
4179 end;
4181 Resolve_Choice_Prefix (Choice, Enclosing_Type);
4182 Analyze_And_Resolve (Expression (N), Enclosing_Type);
4183 end Resolve_Deep_Delta_Assoc;
4185 ---------------------------------
4186 -- Resolve_Extension_Aggregate --
4187 ---------------------------------
4189 -- There are two cases to consider:
4191 -- a) If the ancestor part is a type mark, the components needed are the
4192 -- difference between the components of the expected type and the
4193 -- components of the given type mark.
4195 -- b) If the ancestor part is an expression, it must be unambiguous, and
4196 -- once we have its type we can also compute the needed components as in
4197 -- the previous case. In both cases, if the ancestor type is not the
4198 -- immediate ancestor, we have to build this ancestor recursively.
4200 -- In both cases, discriminants of the ancestor type do not play a role in
4201 -- the resolution of the needed components, because inherited discriminants
4202 -- cannot be used in a type extension. As a result we can compute
4203 -- independently the list of components of the ancestor type and of the
4204 -- expected type.
4206 procedure Resolve_Extension_Aggregate (N : Node_Id; Typ : Entity_Id) is
4207 A : constant Node_Id := Ancestor_Part (N);
4208 A_Type : Entity_Id;
4209 I : Interp_Index;
4210 It : Interp;
4212 function Valid_Limited_Ancestor (Anc : Node_Id) return Boolean;
4213 -- If the type is limited, verify that the ancestor part is a legal
4214 -- expression (aggregate or function call, including 'Input)) that does
4215 -- not require a copy, as specified in 7.5(2).
4217 function Valid_Ancestor_Type return Boolean;
4218 -- Verify that the type of the ancestor part is a non-private ancestor
4219 -- of the expected type, which must be a type extension.
4221 procedure Transform_BIP_Assignment (Typ : Entity_Id);
4222 -- For an extension aggregate whose ancestor part is a build-in-place
4223 -- call returning a nonlimited type, this is used to transform the
4224 -- assignment to the ancestor part to use a temp.
4226 ----------------------------
4227 -- Valid_Limited_Ancestor --
4228 ----------------------------
4230 function Valid_Limited_Ancestor (Anc : Node_Id) return Boolean is
4231 begin
4232 if Is_Entity_Name (Anc) and then Is_Type (Entity (Anc)) then
4233 return True;
4235 -- The ancestor must be a call or an aggregate, but a call may
4236 -- have been expanded into a temporary, so check original node.
4238 elsif Nkind (Anc) in N_Aggregate
4239 | N_Extension_Aggregate
4240 | N_Function_Call
4241 then
4242 return True;
4244 elsif Nkind (Original_Node (Anc)) = N_Function_Call then
4245 return True;
4247 elsif Nkind (Anc) = N_Attribute_Reference
4248 and then Attribute_Name (Anc) = Name_Input
4249 then
4250 return True;
4252 elsif Nkind (Anc) = N_Qualified_Expression then
4253 return Valid_Limited_Ancestor (Expression (Anc));
4255 elsif Nkind (Anc) = N_Raise_Expression then
4256 return True;
4258 else
4259 return False;
4260 end if;
4261 end Valid_Limited_Ancestor;
4263 -------------------------
4264 -- Valid_Ancestor_Type --
4265 -------------------------
4267 function Valid_Ancestor_Type return Boolean is
4268 Imm_Type : Entity_Id;
4270 begin
4271 Imm_Type := Base_Type (Typ);
4272 while Is_Derived_Type (Imm_Type) loop
4273 if Etype (Imm_Type) = Base_Type (A_Type) then
4274 return True;
4276 -- The base type of the parent type may appear as a private
4277 -- extension if it is declared as such in a parent unit of the
4278 -- current one. For consistency of the subsequent analysis use
4279 -- the partial view for the ancestor part.
4281 elsif Is_Private_Type (Etype (Imm_Type))
4282 and then Present (Full_View (Etype (Imm_Type)))
4283 and then Base_Type (A_Type) = Full_View (Etype (Imm_Type))
4284 then
4285 A_Type := Etype (Imm_Type);
4286 return True;
4288 -- The parent type may be a private extension. The aggregate is
4289 -- legal if the type of the aggregate is an extension of it that
4290 -- is not a private extension.
4292 elsif Is_Private_Type (A_Type)
4293 and then not Is_Private_Type (Imm_Type)
4294 and then Present (Full_View (A_Type))
4295 and then Base_Type (Full_View (A_Type)) = Etype (Imm_Type)
4296 then
4297 return True;
4299 -- The parent type may be a raise expression (which is legal in
4300 -- any expression context).
4302 elsif A_Type = Raise_Type then
4303 A_Type := Etype (Imm_Type);
4304 return True;
4306 else
4307 Imm_Type := Etype (Base_Type (Imm_Type));
4308 end if;
4309 end loop;
4311 -- If previous loop did not find a proper ancestor, report error
4313 Error_Msg_NE ("expect ancestor type of &", A, Typ);
4314 return False;
4315 end Valid_Ancestor_Type;
4317 ------------------------------
4318 -- Transform_BIP_Assignment --
4319 ------------------------------
4321 procedure Transform_BIP_Assignment (Typ : Entity_Id) is
4322 Loc : constant Source_Ptr := Sloc (N);
4323 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'Y', A);
4324 Obj_Decl : constant Node_Id :=
4325 Make_Object_Declaration (Loc,
4326 Defining_Identifier => Def_Id,
4327 Constant_Present => True,
4328 Object_Definition => New_Occurrence_Of (Typ, Loc),
4329 Expression => A,
4330 Has_Init_Expression => True);
4331 begin
4332 Set_Etype (Def_Id, Typ);
4333 Set_Ancestor_Part (N, New_Occurrence_Of (Def_Id, Loc));
4334 Insert_Action (N, Obj_Decl);
4335 end Transform_BIP_Assignment;
4337 -- Start of processing for Resolve_Extension_Aggregate
4339 begin
4340 -- Analyze the ancestor part and account for the case where it is a
4341 -- parameterless function call.
4343 Analyze (A);
4344 Check_Parameterless_Call (A);
4346 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
4348 -- AI05-0115: If the ancestor part is a subtype mark, the ancestor
4349 -- must not have unknown discriminants. To catch cases where the
4350 -- aggregate occurs at a place where the full view of the ancestor
4351 -- type is visible and doesn't have unknown discriminants, but the
4352 -- aggregate type was derived from a partial view that has unknown
4353 -- discriminants, we check whether the aggregate type has unknown
4354 -- discriminants (unknown discriminants were inherited), along
4355 -- with checking that the partial view of the ancestor has unknown
4356 -- discriminants. (It might be sufficient to replace the entire
4357 -- condition with Has_Unknown_Discriminants (Typ), but that might
4358 -- miss some cases, not clear, and causes error changes in some tests
4359 -- such as class-wide cases, that aren't clearly improvements. ???)
4361 if Has_Unknown_Discriminants (Entity (A))
4362 or else (Has_Unknown_Discriminants (Typ)
4363 and then Partial_View_Has_Unknown_Discr (Entity (A)))
4364 then
4365 Error_Msg_NE
4366 ("aggregate not available for type& whose ancestor "
4367 & "has unknown discriminants", N, Typ);
4368 end if;
4369 end if;
4371 if not Is_Tagged_Type (Typ) then
4372 Error_Msg_N ("type of extension aggregate must be tagged", N);
4373 return;
4375 elsif Is_Limited_Type (Typ) then
4377 -- Ada 2005 (AI-287): Limited aggregates are allowed
4379 if Ada_Version < Ada_2005 then
4380 Error_Msg_N ("aggregate type cannot be limited", N);
4381 Explain_Limited_Type (Typ, N);
4382 return;
4384 elsif Valid_Limited_Ancestor (A) then
4385 null;
4387 else
4388 Error_Msg_N
4389 ("limited ancestor part must be aggregate or function call", A);
4390 end if;
4392 elsif Is_Class_Wide_Type (Typ) then
4393 Error_Msg_N ("aggregate cannot be of a class-wide type", N);
4394 return;
4395 end if;
4397 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
4398 A_Type := Get_Full_View (Entity (A));
4400 if Valid_Ancestor_Type then
4401 Set_Entity (A, A_Type);
4402 Set_Etype (A, A_Type);
4404 Validate_Ancestor_Part (N);
4405 Resolve_Record_Aggregate (N, Typ);
4406 end if;
4408 elsif Nkind (A) /= N_Aggregate then
4409 if Is_Overloaded (A) then
4410 A_Type := Any_Type;
4412 Get_First_Interp (A, I, It);
4413 while Present (It.Typ) loop
4415 -- Consider limited interpretations if Ada 2005 or higher
4417 if Is_Tagged_Type (It.Typ)
4418 and then (Ada_Version >= Ada_2005
4419 or else not Is_Limited_Type (It.Typ))
4420 then
4421 if A_Type /= Any_Type then
4422 Error_Msg_N ("cannot resolve expression", A);
4423 return;
4424 else
4425 A_Type := It.Typ;
4426 end if;
4427 end if;
4429 Get_Next_Interp (I, It);
4430 end loop;
4432 if A_Type = Any_Type then
4433 if Ada_Version >= Ada_2005 then
4434 Error_Msg_N
4435 ("ancestor part must be of a tagged type", A);
4436 else
4437 Error_Msg_N
4438 ("ancestor part must be of a nonlimited tagged type", A);
4439 end if;
4441 return;
4442 end if;
4444 else
4445 A_Type := Etype (A);
4446 end if;
4448 if Valid_Ancestor_Type then
4449 Resolve (A, A_Type);
4450 Check_Unset_Reference (A);
4451 Check_Non_Static_Context (A);
4453 -- The aggregate is illegal if the ancestor expression is a call
4454 -- to a function with a limited unconstrained result, unless the
4455 -- type of the aggregate is a null extension. This restriction
4456 -- was added in AI05-67 to simplify implementation.
4458 if Nkind (A) = N_Function_Call
4459 and then Is_Limited_Type (A_Type)
4460 and then not Is_Null_Extension (Typ)
4461 and then not Is_Constrained (A_Type)
4462 then
4463 Error_Msg_N
4464 ("type of limited ancestor part must be constrained", A);
4466 -- Reject the use of CPP constructors that leave objects partially
4467 -- initialized. For example:
4469 -- type CPP_Root is tagged limited record ...
4470 -- pragma Import (CPP, CPP_Root);
4472 -- type CPP_DT is new CPP_Root and Iface ...
4473 -- pragma Import (CPP, CPP_DT);
4475 -- type Ada_DT is new CPP_DT with ...
4477 -- Obj : Ada_DT := Ada_DT'(New_CPP_Root with others => <>);
4479 -- Using the constructor of CPP_Root the slots of the dispatch
4480 -- table of CPP_DT cannot be set, and the secondary tag of
4481 -- CPP_DT is unknown.
4483 elsif Nkind (A) = N_Function_Call
4484 and then Is_CPP_Constructor_Call (A)
4485 and then Enclosing_CPP_Parent (Typ) /= A_Type
4486 then
4487 Error_Msg_NE
4488 ("??must use 'C'P'P constructor for type &", A,
4489 Enclosing_CPP_Parent (Typ));
4491 -- The following call is not needed if the previous warning
4492 -- is promoted to an error.
4494 Resolve_Record_Aggregate (N, Typ);
4496 elsif Is_Class_Wide_Type (Etype (A))
4497 and then Nkind (Original_Node (A)) = N_Function_Call
4498 then
4499 -- If the ancestor part is a dispatching call, it appears
4500 -- statically to be a legal ancestor, but it yields any member
4501 -- of the class, and it is not possible to determine whether
4502 -- it is an ancestor of the extension aggregate (much less
4503 -- which ancestor). It is not possible to determine the
4504 -- components of the extension part.
4506 -- This check implements AI-306, which in fact was motivated by
4507 -- an AdaCore query to the ARG after this test was added.
4509 Error_Msg_N ("ancestor part must be statically tagged", A);
4510 else
4511 -- We are using the build-in-place protocol, but we can't build
4512 -- in place, because we need to call the function before
4513 -- allocating the aggregate. Could do better for null
4514 -- extensions, and maybe for nondiscriminated types.
4515 -- This is wrong for limited, but those were wrong already.
4517 if not Is_Inherently_Limited_Type (A_Type)
4518 and then Is_Build_In_Place_Function_Call (A)
4519 then
4520 Transform_BIP_Assignment (A_Type);
4521 end if;
4523 Resolve_Record_Aggregate (N, Typ);
4524 end if;
4525 end if;
4527 else
4528 Error_Msg_N ("no unique type for this aggregate", A);
4529 end if;
4531 Check_Function_Writable_Actuals (N);
4532 end Resolve_Extension_Aggregate;
4534 ----------------------------------
4535 -- Resolve_Null_Array_Aggregate --
4536 ----------------------------------
4538 function Resolve_Null_Array_Aggregate (N : Node_Id) return Boolean is
4539 -- Never returns False, but declared as a function to match
4540 -- other Resolve_Mumble functions.
4542 Loc : constant Source_Ptr := Sloc (N);
4543 Typ : constant Entity_Id := Etype (N);
4545 Index : Node_Id;
4546 Lo, Hi : Node_Id;
4547 Constr : constant List_Id := New_List;
4549 begin
4550 -- Attach the list of constraints at the location of the aggregate, so
4551 -- the individual constraints can be analyzed.
4553 Set_Parent (Constr, N);
4555 -- Create a constrained subtype with null dimensions
4557 Index := First_Index (Typ);
4558 while Present (Index) loop
4559 Get_Index_Bounds (Index, L => Lo, H => Hi);
4561 -- The upper bound is the predecessor of the lower bound
4563 Hi := Make_Attribute_Reference
4564 (Loc,
4565 Prefix => New_Occurrence_Of (Etype (Index), Loc),
4566 Attribute_Name => Name_Pred,
4567 Expressions => New_List (New_Copy_Tree (Lo)));
4569 Append (Make_Range (Loc, New_Copy_Tree (Lo), Hi), Constr);
4570 Analyze_And_Resolve (Last (Constr), Etype (Index));
4572 Next_Index (Index);
4573 end loop;
4575 Set_Compile_Time_Known_Aggregate (N);
4576 Set_Aggregate_Bounds (N, First (Constr));
4578 return True;
4579 end Resolve_Null_Array_Aggregate;
4581 ------------------------------
4582 -- Resolve_Record_Aggregate --
4583 ------------------------------
4585 procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id) is
4586 New_Assoc_List : constant List_Id := New_List;
4587 -- New_Assoc_List is the newly built list of N_Component_Association
4588 -- nodes.
4590 Others_Etype : Entity_Id := Empty;
4591 -- This variable is used to save the Etype of the last record component
4592 -- that takes its value from the others choice. Its purpose is:
4594 -- (a) make sure the others choice is useful
4596 -- (b) make sure the type of all the components whose value is
4597 -- subsumed by the others choice are the same.
4599 -- This variable is updated as a side effect of function Get_Value.
4601 Box_Node : Node_Id := Empty;
4602 Is_Box_Present : Boolean := False;
4603 Is_Box_Init_By_Default : Boolean := False;
4604 Others_Box : Natural := 0;
4605 -- Ada 2005 (AI-287): Variables used in case of default initialization
4606 -- to provide a functionality similar to Others_Etype. Box_Present
4607 -- indicates that the component takes its default initialization;
4608 -- Others_Box counts the number of components of the current aggregate
4609 -- (which may be a sub-aggregate of a larger one) that are default-
4610 -- initialized. A value of One indicates that an others_box is present.
4611 -- Any larger value indicates that the others_box is not redundant.
4612 -- These variables, similar to Others_Etype, are also updated as a side
4613 -- effect of function Get_Value. Box_Node is used to place a warning on
4614 -- a redundant others_box.
4616 procedure Add_Association
4617 (Component : Entity_Id;
4618 Expr : Node_Id;
4619 Assoc_List : List_Id;
4620 Is_Box_Present : Boolean := False);
4621 -- Builds a new N_Component_Association node which associates Component
4622 -- to expression Expr and adds it to the association list being built,
4623 -- either New_Assoc_List, or the association being built for an inner
4624 -- aggregate.
4626 function Discriminant_Present (Input_Discr : Entity_Id) return Boolean;
4627 -- If aggregate N is a regular aggregate this routine will return True.
4628 -- Otherwise, if N is an extension aggregate, then Input_Discr denotes
4629 -- a discriminant whose value may already have been specified by N's
4630 -- ancestor part. This routine checks whether this is indeed the case
4631 -- and if so returns False, signaling that no value for Input_Discr
4632 -- should appear in N's aggregate part. Also, in this case, the routine
4633 -- appends to New_Assoc_List the discriminant value specified in the
4634 -- ancestor part.
4636 -- If the aggregate is in a context with expansion delayed, it will be
4637 -- reanalyzed. The inherited discriminant values must not be reinserted
4638 -- in the component list to prevent spurious errors, but they must be
4639 -- present on first analysis to build the proper subtype indications.
4640 -- The flag Inherited_Discriminant is used to prevent the re-insertion.
4642 function Find_Private_Ancestor (Typ : Entity_Id) return Entity_Id;
4643 -- AI05-0115: Find earlier ancestor in the derivation chain that is
4644 -- derived from private view Typ. Whether the aggregate is legal depends
4645 -- on the current visibility of the type as well as that of the parent
4646 -- of the ancestor.
4648 function Get_Value
4649 (Compon : Entity_Id;
4650 From : List_Id;
4651 Consider_Others_Choice : Boolean := False) return Node_Id;
4652 -- Given a record component stored in parameter Compon, this function
4653 -- returns its value as it appears in the list From, which is a list
4654 -- of N_Component_Association nodes.
4656 -- If no component association has a choice for the searched component,
4657 -- the value provided by the others choice is returned, if there is one,
4658 -- and Consider_Others_Choice is set to true. Otherwise Empty is
4659 -- returned. If there is more than one component association giving a
4660 -- value for the searched record component, an error message is emitted
4661 -- and the first found value is returned.
4663 -- If Consider_Others_Choice is set and the returned expression comes
4664 -- from the others choice, then Others_Etype is set as a side effect.
4665 -- An error message is emitted if the components taking their value from
4666 -- the others choice do not have same type.
4668 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Entity_Id);
4669 -- Analyzes and resolves expression Expr against the Etype of the
4670 -- Component. This routine also applies all appropriate checks to Expr.
4671 -- It finally saves a Expr in the newly created association list that
4672 -- will be attached to the final record aggregate. Note that if the
4673 -- Parent pointer of Expr is not set then Expr was produced with a
4674 -- New_Copy_Tree or some such.
4676 procedure Rewrite_Range (Root_Type : Entity_Id; Rge : Node_Id);
4677 -- Rewrite a range node Rge when its bounds refer to non-stored
4678 -- discriminants from Root_Type, to replace them with the stored
4679 -- discriminant values. This is required in GNATprove mode, and is
4680 -- adopted in all modes to avoid special-casing GNATprove mode.
4682 ---------------------
4683 -- Add_Association --
4684 ---------------------
4686 procedure Add_Association
4687 (Component : Entity_Id;
4688 Expr : Node_Id;
4689 Assoc_List : List_Id;
4690 Is_Box_Present : Boolean := False)
4692 Choice_List : constant List_Id := New_List;
4693 Loc : Source_Ptr;
4695 begin
4696 -- If this is a box association the expression is missing, so use the
4697 -- Sloc of the aggregate itself for the new association.
4699 pragma Assert (Present (Expr) xor Is_Box_Present);
4701 if Present (Expr) then
4702 Loc := Sloc (Expr);
4703 else
4704 Loc := Sloc (N);
4705 end if;
4707 Append_To (Choice_List, New_Occurrence_Of (Component, Loc));
4709 Append_To (Assoc_List,
4710 Make_Component_Association (Loc,
4711 Choices => Choice_List,
4712 Expression => Expr,
4713 Box_Present => Is_Box_Present));
4715 -- If this association has a box for a component that is initialized
4716 -- by default, then set flag on the new association to indicate that
4717 -- the original association was for such a box-initialized component.
4719 if Is_Box_Init_By_Default then
4720 Set_Was_Default_Init_Box_Association (Last (Assoc_List));
4721 end if;
4722 end Add_Association;
4724 --------------------------
4725 -- Discriminant_Present --
4726 --------------------------
4728 function Discriminant_Present (Input_Discr : Entity_Id) return Boolean is
4729 Regular_Aggr : constant Boolean := Nkind (N) /= N_Extension_Aggregate;
4731 Ancestor_Is_Subtyp : Boolean;
4733 Loc : Source_Ptr;
4735 Ancestor : Node_Id;
4736 Ancestor_Typ : Entity_Id;
4737 Comp_Assoc : Node_Id;
4738 Discr : Entity_Id;
4739 Discr_Expr : Node_Id;
4740 Discr_Val : Elmt_Id := No_Elmt;
4741 Orig_Discr : Entity_Id;
4743 begin
4744 if Regular_Aggr then
4745 return True;
4746 end if;
4748 -- Check whether inherited discriminant values have already been
4749 -- inserted in the aggregate. This will be the case if we are
4750 -- re-analyzing an aggregate whose expansion was delayed.
4752 if Present (Component_Associations (N)) then
4753 Comp_Assoc := First (Component_Associations (N));
4754 while Present (Comp_Assoc) loop
4755 if Inherited_Discriminant (Comp_Assoc) then
4756 return True;
4757 end if;
4759 Next (Comp_Assoc);
4760 end loop;
4761 end if;
4763 Ancestor := Ancestor_Part (N);
4764 Ancestor_Typ := Etype (Ancestor);
4765 Loc := Sloc (Ancestor);
4767 -- For a private type with unknown discriminants, use the underlying
4768 -- record view if it is available.
4770 if Has_Unknown_Discriminants (Ancestor_Typ)
4771 and then Present (Full_View (Ancestor_Typ))
4772 and then Present (Underlying_Record_View (Full_View (Ancestor_Typ)))
4773 then
4774 Ancestor_Typ := Underlying_Record_View (Full_View (Ancestor_Typ));
4775 end if;
4777 Ancestor_Is_Subtyp :=
4778 Is_Entity_Name (Ancestor) and then Is_Type (Entity (Ancestor));
4780 -- If the ancestor part has no discriminants clearly N's aggregate
4781 -- part must provide a value for Discr.
4783 if not Has_Discriminants (Ancestor_Typ) then
4784 return True;
4786 -- If the ancestor part is an unconstrained subtype mark then the
4787 -- Discr must be present in N's aggregate part.
4789 elsif Ancestor_Is_Subtyp
4790 and then not Is_Constrained (Entity (Ancestor))
4791 then
4792 return True;
4793 end if;
4795 -- Now look to see if Discr was specified in the ancestor part
4797 if Ancestor_Is_Subtyp then
4798 Discr_Val :=
4799 First_Elmt (Discriminant_Constraint (Entity (Ancestor)));
4800 end if;
4802 Orig_Discr := Original_Record_Component (Input_Discr);
4804 Discr := First_Discriminant (Ancestor_Typ);
4805 while Present (Discr) loop
4807 -- If Ancestor has already specified Disc value then insert its
4808 -- value in the final aggregate.
4810 if Original_Record_Component (Discr) = Orig_Discr then
4811 if Ancestor_Is_Subtyp then
4812 Discr_Expr := New_Copy_Tree (Node (Discr_Val));
4813 else
4814 Discr_Expr :=
4815 Make_Selected_Component (Loc,
4816 Prefix => Duplicate_Subexpr (Ancestor),
4817 Selector_Name => New_Occurrence_Of (Input_Discr, Loc));
4818 end if;
4820 Resolve_Aggr_Expr (Discr_Expr, Input_Discr);
4821 Set_Inherited_Discriminant (Last (New_Assoc_List));
4822 return False;
4823 end if;
4825 Next_Discriminant (Discr);
4827 if Ancestor_Is_Subtyp then
4828 Next_Elmt (Discr_Val);
4829 end if;
4830 end loop;
4832 return True;
4833 end Discriminant_Present;
4835 ---------------------------
4836 -- Find_Private_Ancestor --
4837 ---------------------------
4839 function Find_Private_Ancestor (Typ : Entity_Id) return Entity_Id is
4840 Par : Entity_Id;
4842 begin
4843 Par := Typ;
4844 loop
4845 if Has_Private_Ancestor (Par)
4846 and then not Has_Private_Ancestor (Etype (Base_Type (Par)))
4847 then
4848 return Par;
4850 elsif not Is_Derived_Type (Par) then
4851 return Empty;
4853 else
4854 Par := Etype (Base_Type (Par));
4855 end if;
4856 end loop;
4857 end Find_Private_Ancestor;
4859 ---------------
4860 -- Get_Value --
4861 ---------------
4863 function Get_Value
4864 (Compon : Entity_Id;
4865 From : List_Id;
4866 Consider_Others_Choice : Boolean := False) return Node_Id
4868 Typ : constant Entity_Id := Etype (Compon);
4869 Assoc : Node_Id;
4870 Expr : Node_Id := Empty;
4871 Selector_Name : Node_Id;
4873 begin
4874 Is_Box_Present := False;
4875 Is_Box_Init_By_Default := False;
4877 if No (From) then
4878 return Empty;
4879 end if;
4881 Assoc := First (From);
4882 while Present (Assoc) loop
4883 Selector_Name := First (Choices (Assoc));
4884 while Present (Selector_Name) loop
4885 if Nkind (Selector_Name) = N_Others_Choice then
4886 if Consider_Others_Choice and then No (Expr) then
4888 -- We need to duplicate the expression for each
4889 -- successive component covered by the others choice.
4890 -- This is redundant if the others_choice covers only
4891 -- one component (small optimization possible???), but
4892 -- indispensable otherwise, because each one must be
4893 -- expanded individually to preserve side effects.
4895 -- Ada 2005 (AI-287): In case of default initialization
4896 -- of components, we duplicate the corresponding default
4897 -- expression (from the record type declaration). The
4898 -- copy must carry the sloc of the association (not the
4899 -- original expression) to prevent spurious elaboration
4900 -- checks when the default includes function calls.
4902 if Box_Present (Assoc) then
4903 Others_Box := Others_Box + 1;
4904 Is_Box_Present := True;
4906 if Expander_Active then
4907 return
4908 New_Copy_Tree_And_Copy_Dimensions
4909 (Expression (Parent (Compon)),
4910 New_Sloc => Sloc (Assoc));
4911 else
4912 return Expression (Parent (Compon));
4913 end if;
4915 else
4916 if Present (Others_Etype)
4917 and then Base_Type (Others_Etype) /= Base_Type (Typ)
4918 then
4919 -- If the components are of an anonymous access
4920 -- type they are distinct, but this is legal in
4921 -- Ada 2012 as long as designated types match.
4923 if (Ekind (Typ) = E_Anonymous_Access_Type
4924 or else Ekind (Typ) =
4925 E_Anonymous_Access_Subprogram_Type)
4926 and then Designated_Type (Typ) =
4927 Designated_Type (Others_Etype)
4928 then
4929 null;
4930 else
4931 Error_Msg_N
4932 ("components in OTHERS choice must have same "
4933 & "type", Selector_Name);
4934 end if;
4935 end if;
4937 Others_Etype := Typ;
4939 -- Copy the expression so that it is resolved
4940 -- independently for each component, This is needed
4941 -- for accessibility checks on components of anonymous
4942 -- access types, even in compile_only mode.
4944 if not Inside_A_Generic then
4945 return
4946 New_Copy_Tree_And_Copy_Dimensions
4947 (Expression (Assoc));
4948 else
4949 return Expression (Assoc);
4950 end if;
4951 end if;
4952 end if;
4954 elsif Chars (Compon) = Chars (Selector_Name) then
4955 if No (Expr) then
4957 -- Ada 2005 (AI-231)
4959 if Ada_Version >= Ada_2005
4960 and then Known_Null (Expression (Assoc))
4961 then
4962 Check_Can_Never_Be_Null (Compon, Expression (Assoc));
4963 end if;
4965 -- We need to duplicate the expression when several
4966 -- components are grouped together with a "|" choice.
4967 -- For instance "filed1 | filed2 => Expr"
4969 -- Ada 2005 (AI-287)
4971 if Box_Present (Assoc) then
4972 Is_Box_Present := True;
4974 -- Duplicate the default expression of the component
4975 -- from the record type declaration, so a new copy
4976 -- can be attached to the association.
4978 -- Note that we always copy the default expression,
4979 -- even when the association has a single choice, in
4980 -- order to create a proper association for the
4981 -- expanded aggregate.
4983 -- Component may have no default, in which case the
4984 -- expression is empty and the component is default-
4985 -- initialized, but an association for the component
4986 -- exists, and it is not covered by an others clause.
4988 -- Scalar and private types have no initialization
4989 -- procedure, so they remain uninitialized. If the
4990 -- target of the aggregate is a constant this
4991 -- deserves a warning.
4993 if No (Expression (Parent (Compon)))
4994 and then not Has_Non_Null_Base_Init_Proc (Typ)
4995 and then not Has_Aspect (Typ, Aspect_Default_Value)
4996 and then not Is_Concurrent_Type (Typ)
4997 and then Nkind (Parent (N)) = N_Object_Declaration
4998 and then Constant_Present (Parent (N))
4999 then
5000 Error_Msg_Node_2 := Typ;
5001 Error_Msg_NE
5002 ("??component& of type& is uninitialized",
5003 Assoc, Selector_Name);
5005 -- An additional reminder if the component type
5006 -- is a generic formal.
5008 if Is_Generic_Type (Base_Type (Typ)) then
5009 Error_Msg_NE
5010 ("\instance should provide actual type with "
5011 & "initialization for&", Assoc, Typ);
5012 end if;
5013 end if;
5015 return
5016 New_Copy_Tree_And_Copy_Dimensions
5017 (Expression (Parent (Compon)));
5019 else
5020 if Present (Next (Selector_Name)) then
5021 Expr := New_Copy_Tree_And_Copy_Dimensions
5022 (Expression (Assoc));
5023 else
5024 Expr := Expression (Assoc);
5025 end if;
5026 end if;
5028 Generate_Reference (Compon, Selector_Name, 'm');
5030 else
5031 Error_Msg_NE
5032 ("more than one value supplied for &",
5033 Selector_Name, Compon);
5035 end if;
5036 end if;
5038 Next (Selector_Name);
5039 end loop;
5041 Next (Assoc);
5042 end loop;
5044 return Expr;
5045 end Get_Value;
5047 -----------------------
5048 -- Resolve_Aggr_Expr --
5049 -----------------------
5051 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Entity_Id) is
5052 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean;
5053 -- If the expression is an aggregate (possibly qualified) then its
5054 -- expansion is delayed until the enclosing aggregate is expanded
5055 -- into assignments. In that case, do not generate checks on the
5056 -- expression, because they will be generated later, and will other-
5057 -- wise force a copy (to remove side effects) that would leave a
5058 -- dynamic-sized aggregate in the code, something that gigi cannot
5059 -- handle.
5061 ---------------------------
5062 -- Has_Expansion_Delayed --
5063 ---------------------------
5065 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean is
5066 begin
5067 return
5068 (Nkind (Expr) in N_Aggregate | N_Extension_Aggregate
5069 and then Present (Etype (Expr))
5070 and then Is_Record_Type (Etype (Expr))
5071 and then Expansion_Delayed (Expr))
5072 or else
5073 (Nkind (Expr) = N_Qualified_Expression
5074 and then Has_Expansion_Delayed (Expression (Expr)));
5075 end Has_Expansion_Delayed;
5077 -- Local variables
5079 Expr_Type : Entity_Id := Empty;
5080 New_C : Entity_Id := Component;
5081 New_Expr : Node_Id;
5083 Relocate : Boolean;
5084 -- Set to True if the resolved Expr node needs to be relocated when
5085 -- attached to the newly created association list. This node need not
5086 -- be relocated if its parent pointer is not set. In fact in this
5087 -- case Expr is the output of a New_Copy_Tree call. If Relocate is
5088 -- True then we have analyzed the expression node in the original
5089 -- aggregate and hence it needs to be relocated when moved over to
5090 -- the new association list.
5092 -- Start of processing for Resolve_Aggr_Expr
5094 begin
5095 -- If the type of the component is elementary or the type of the
5096 -- aggregate does not contain discriminants, use the type of the
5097 -- component to resolve Expr.
5099 if Is_Elementary_Type (Etype (Component))
5100 or else not Has_Discriminants (Etype (N))
5101 then
5102 Expr_Type := Etype (Component);
5104 -- Otherwise we have to pick up the new type of the component from
5105 -- the new constrained subtype of the aggregate. In fact components
5106 -- which are of a composite type might be constrained by a
5107 -- discriminant, and we want to resolve Expr against the subtype were
5108 -- all discriminant occurrences are replaced with their actual value.
5110 else
5111 New_C := First_Component (Etype (N));
5112 while Present (New_C) loop
5113 if Chars (New_C) = Chars (Component) then
5114 Expr_Type := Etype (New_C);
5115 exit;
5116 end if;
5118 Next_Component (New_C);
5119 end loop;
5121 pragma Assert (Present (Expr_Type));
5123 -- For each range in an array type where a discriminant has been
5124 -- replaced with the constraint, check that this range is within
5125 -- the range of the base type. This checks is done in the init
5126 -- proc for regular objects, but has to be done here for
5127 -- aggregates since no init proc is called for them.
5129 if Is_Array_Type (Expr_Type) then
5130 declare
5131 Index : Node_Id;
5132 -- Range of the current constrained index in the array
5134 Orig_Index : Node_Id := First_Index (Etype (Component));
5135 -- Range corresponding to the range Index above in the
5136 -- original unconstrained record type. The bounds of this
5137 -- range may be governed by discriminants.
5139 Unconstr_Index : Node_Id := First_Index (Etype (Expr_Type));
5140 -- Range corresponding to the range Index above for the
5141 -- unconstrained array type. This range is needed to apply
5142 -- range checks.
5144 begin
5145 Index := First_Index (Expr_Type);
5146 while Present (Index) loop
5147 if Depends_On_Discriminant (Orig_Index) then
5148 Apply_Range_Check (Index, Etype (Unconstr_Index));
5149 end if;
5151 Next_Index (Index);
5152 Next_Index (Orig_Index);
5153 Next_Index (Unconstr_Index);
5154 end loop;
5155 end;
5156 end if;
5157 end if;
5159 -- If the Parent pointer of Expr is not set, Expr is an expression
5160 -- duplicated by New_Tree_Copy (this happens for record aggregates
5161 -- that look like (Field1 | Filed2 => Expr) or (others => Expr)).
5162 -- Such a duplicated expression must be attached to the tree
5163 -- before analysis and resolution to enforce the rule that a tree
5164 -- fragment should never be analyzed or resolved unless it is
5165 -- attached to the current compilation unit.
5167 if No (Parent (Expr)) then
5168 Set_Parent (Expr, N);
5169 Relocate := False;
5170 else
5171 Relocate := True;
5172 end if;
5174 Analyze_And_Resolve (Expr, Expr_Type);
5175 Check_Expr_OK_In_Limited_Aggregate (Expr);
5176 Check_Non_Static_Context (Expr);
5177 Check_Unset_Reference (Expr);
5179 -- Check wrong use of class-wide types
5181 if Is_Class_Wide_Type (Etype (Expr)) then
5182 Error_Msg_N ("dynamically tagged expression not allowed", Expr);
5183 end if;
5185 if not Has_Expansion_Delayed (Expr) then
5186 Aggregate_Constraint_Checks (Expr, Expr_Type);
5187 end if;
5189 -- If an aggregate component has a type with predicates, an explicit
5190 -- predicate check must be applied, as for an assignment statement,
5191 -- because the aggregate might not be expanded into individual
5192 -- component assignments.
5194 if Has_Predicates (Expr_Type)
5195 and then Analyzed (Expr)
5196 then
5197 Apply_Predicate_Check (Expr, Expr_Type);
5198 end if;
5200 if Raises_Constraint_Error (Expr) then
5201 Set_Raises_Constraint_Error (N);
5202 end if;
5204 -- If the expression has been marked as requiring a range check, then
5205 -- generate it here. It's a bit odd to be generating such checks in
5206 -- the analyzer, but harmless since Generate_Range_Check does nothing
5207 -- (other than making sure Do_Range_Check is set) if the expander is
5208 -- not active.
5210 if Do_Range_Check (Expr) then
5211 Generate_Range_Check (Expr, Expr_Type, CE_Range_Check_Failed);
5212 end if;
5214 -- Add association Component => Expr if the caller requests it
5216 if Relocate then
5217 New_Expr := Relocate_Node (Expr);
5219 -- Since New_Expr is not gonna be analyzed later on, we need to
5220 -- propagate here the dimensions form Expr to New_Expr.
5222 Copy_Dimensions (Expr, New_Expr);
5224 else
5225 New_Expr := Expr;
5226 end if;
5228 Add_Association (New_C, New_Expr, New_Assoc_List);
5229 end Resolve_Aggr_Expr;
5231 -------------------
5232 -- Rewrite_Range --
5233 -------------------
5235 procedure Rewrite_Range (Root_Type : Entity_Id; Rge : Node_Id) is
5236 procedure Rewrite_Bound
5237 (Bound : Node_Id;
5238 Disc : Entity_Id;
5239 Expr_Disc : Node_Id);
5240 -- Rewrite a bound of the range Bound, when it is equal to the
5241 -- non-stored discriminant Disc, into the stored discriminant
5242 -- value Expr_Disc.
5244 -------------------
5245 -- Rewrite_Bound --
5246 -------------------
5248 procedure Rewrite_Bound
5249 (Bound : Node_Id;
5250 Disc : Entity_Id;
5251 Expr_Disc : Node_Id)
5253 begin
5254 if Nkind (Bound) /= N_Identifier then
5255 return;
5256 end if;
5258 -- We expect either the discriminant or the discriminal
5260 if Entity (Bound) = Disc
5261 or else (Ekind (Entity (Bound)) = E_In_Parameter
5262 and then Discriminal_Link (Entity (Bound)) = Disc)
5263 then
5264 Rewrite (Bound, New_Copy_Tree (Expr_Disc));
5265 end if;
5266 end Rewrite_Bound;
5268 -- Local variables
5270 Low, High : Node_Id;
5271 Disc : Entity_Id;
5272 Expr_Disc : Elmt_Id;
5274 -- Start of processing for Rewrite_Range
5276 begin
5277 if Has_Discriminants (Root_Type) and then Nkind (Rge) = N_Range then
5278 Low := Low_Bound (Rge);
5279 High := High_Bound (Rge);
5281 Disc := First_Discriminant (Root_Type);
5282 Expr_Disc := First_Elmt (Stored_Constraint (Etype (N)));
5283 while Present (Disc) loop
5284 Rewrite_Bound (Low, Disc, Node (Expr_Disc));
5285 Rewrite_Bound (High, Disc, Node (Expr_Disc));
5286 Next_Discriminant (Disc);
5287 Next_Elmt (Expr_Disc);
5288 end loop;
5289 end if;
5290 end Rewrite_Range;
5292 -- Local variables
5294 Components : constant Elist_Id := New_Elmt_List;
5295 -- Components is the list of the record components whose value must be
5296 -- provided in the aggregate. This list does include discriminants.
5298 Component : Entity_Id;
5299 Component_Elmt : Elmt_Id;
5300 Expr : Node_Id;
5301 Positional_Expr : Node_Id;
5303 -- Start of processing for Resolve_Record_Aggregate
5305 begin
5306 -- A record aggregate is restricted in SPARK:
5308 -- Each named association can have only a single choice.
5309 -- OTHERS cannot be used.
5310 -- Positional and named associations cannot be mixed.
5312 if Present (Component_Associations (N)) then
5313 declare
5314 Assoc : Node_Id;
5316 begin
5317 Assoc := First (Component_Associations (N));
5318 while Present (Assoc) loop
5319 if Nkind (Assoc) = N_Iterated_Component_Association then
5320 Error_Msg_N
5321 ("iterated component association can only appear in an "
5322 & "array aggregate", N);
5323 raise Unrecoverable_Error;
5324 end if;
5326 Next (Assoc);
5327 end loop;
5328 end;
5329 end if;
5331 -- We may end up calling Duplicate_Subexpr on expressions that are
5332 -- attached to New_Assoc_List. For this reason we need to attach it
5333 -- to the tree by setting its parent pointer to N. This parent point
5334 -- will change in STEP 8 below.
5336 Set_Parent (New_Assoc_List, N);
5338 -- STEP 1: abstract type and null record verification
5340 if Is_Abstract_Type (Typ) then
5341 Error_Msg_N ("type of aggregate cannot be abstract", N);
5342 end if;
5344 if No (First_Entity (Typ)) and then Null_Record_Present (N) then
5345 Set_Etype (N, Typ);
5346 return;
5348 elsif Present (First_Entity (Typ))
5349 and then Null_Record_Present (N)
5350 and then not Is_Tagged_Type (Typ)
5351 then
5352 Error_Msg_N ("record aggregate cannot be null", N);
5353 return;
5355 -- If the type has no components, then the aggregate should either
5356 -- have "null record", or in Ada 2005 it could instead have a single
5357 -- component association given by "others => <>". For Ada 95 we flag an
5358 -- error at this point, but for Ada 2005 we proceed with checking the
5359 -- associations below, which will catch the case where it's not an
5360 -- aggregate with "others => <>". Note that the legality of a <>
5361 -- aggregate for a null record type was established by AI05-016.
5363 elsif No (First_Entity (Typ))
5364 and then Ada_Version < Ada_2005
5365 then
5366 Error_Msg_N ("record aggregate must be null", N);
5367 return;
5368 end if;
5370 -- A record aggregate can only use parentheses
5372 if Nkind (N) = N_Aggregate
5373 and then Is_Homogeneous_Aggregate (N)
5374 then
5375 Error_Msg_N ("record aggregate must use (), not '[']", N);
5376 return;
5377 end if;
5379 -- STEP 2: Verify aggregate structure
5381 Step_2 : declare
5382 Assoc : Node_Id;
5383 Bad_Aggregate : Boolean := False;
5384 Selector_Name : Node_Id;
5386 begin
5387 if Present (Component_Associations (N)) then
5388 Assoc := First (Component_Associations (N));
5389 else
5390 Assoc := Empty;
5391 end if;
5393 while Present (Assoc) loop
5394 Selector_Name := First (Choices (Assoc));
5395 while Present (Selector_Name) loop
5396 if Nkind (Selector_Name) = N_Identifier then
5397 null;
5399 elsif Nkind (Selector_Name) = N_Others_Choice then
5400 if Selector_Name /= First (Choices (Assoc))
5401 or else Present (Next (Selector_Name))
5402 then
5403 Error_Msg_N
5404 ("OTHERS must appear alone in a choice list",
5405 Selector_Name);
5406 return;
5408 elsif Present (Next (Assoc)) then
5409 Error_Msg_N
5410 ("OTHERS must appear last in an aggregate",
5411 Selector_Name);
5412 return;
5414 -- (Ada 2005): If this is an association with a box,
5415 -- indicate that the association need not represent
5416 -- any component.
5418 elsif Box_Present (Assoc) then
5419 Others_Box := 1;
5420 Box_Node := Assoc;
5421 end if;
5423 else
5424 Error_Msg_N
5425 ("selector name should be identifier or OTHERS",
5426 Selector_Name);
5427 Bad_Aggregate := True;
5428 end if;
5430 Next (Selector_Name);
5431 end loop;
5433 Next (Assoc);
5434 end loop;
5436 if Bad_Aggregate then
5437 return;
5438 end if;
5439 end Step_2;
5441 -- STEP 3: Find discriminant Values
5443 Step_3 : declare
5444 Discrim : Entity_Id;
5445 Missing_Discriminants : Boolean := False;
5447 begin
5448 if Present (Expressions (N)) then
5449 Positional_Expr := First (Expressions (N));
5450 else
5451 Positional_Expr := Empty;
5452 end if;
5454 -- AI05-0115: if the ancestor part is a subtype mark, the ancestor
5455 -- must not have unknown discriminants.
5456 -- ??? We are not checking any subtype mark here and this code is not
5457 -- exercised by any test, so it's likely wrong (in particular
5458 -- we should not use Root_Type here but the subtype mark, if any),
5459 -- and possibly not needed.
5461 if Is_Derived_Type (Typ)
5462 and then Has_Unknown_Discriminants (Root_Type (Typ))
5463 and then Nkind (N) /= N_Extension_Aggregate
5464 then
5465 Error_Msg_NE
5466 ("aggregate not available for type& whose ancestor "
5467 & "has unknown discriminants", N, Typ);
5468 end if;
5470 if Has_Unknown_Discriminants (Typ)
5471 and then Present (Underlying_Record_View (Typ))
5472 then
5473 Discrim := First_Discriminant (Underlying_Record_View (Typ));
5474 elsif Has_Discriminants (Typ) then
5475 Discrim := First_Discriminant (Typ);
5476 else
5477 Discrim := Empty;
5478 end if;
5480 -- First find the discriminant values in the positional components
5482 while Present (Discrim) and then Present (Positional_Expr) loop
5483 if Discriminant_Present (Discrim) then
5484 Resolve_Aggr_Expr (Positional_Expr, Discrim);
5486 -- Ada 2005 (AI-231)
5488 if Ada_Version >= Ada_2005
5489 and then Known_Null (Positional_Expr)
5490 then
5491 Check_Can_Never_Be_Null (Discrim, Positional_Expr);
5492 end if;
5494 Next (Positional_Expr);
5495 end if;
5497 if Present (Get_Value (Discrim, Component_Associations (N))) then
5498 Error_Msg_NE
5499 ("more than one value supplied for discriminant&",
5500 N, Discrim);
5501 end if;
5503 Next_Discriminant (Discrim);
5504 end loop;
5506 -- Find remaining discriminant values if any among named components
5508 while Present (Discrim) loop
5509 Expr := Get_Value (Discrim, Component_Associations (N), True);
5511 if not Discriminant_Present (Discrim) then
5512 if Present (Expr) then
5513 Error_Msg_NE
5514 ("more than one value supplied for discriminant &",
5515 N, Discrim);
5516 end if;
5518 elsif No (Expr) then
5519 Error_Msg_NE
5520 ("no value supplied for discriminant &", N, Discrim);
5521 Missing_Discriminants := True;
5523 else
5524 Resolve_Aggr_Expr (Expr, Discrim);
5525 end if;
5527 Next_Discriminant (Discrim);
5528 end loop;
5530 if Missing_Discriminants then
5531 return;
5532 end if;
5534 -- At this point and until the beginning of STEP 6, New_Assoc_List
5535 -- contains only the discriminants and their values.
5537 end Step_3;
5539 -- STEP 4: Set the Etype of the record aggregate
5541 if Has_Discriminants (Typ)
5542 or else (Has_Unknown_Discriminants (Typ)
5543 and then Present (Underlying_Record_View (Typ)))
5544 then
5545 Build_Constrained_Itype (N, Typ, New_Assoc_List);
5546 else
5547 Set_Etype (N, Typ);
5548 end if;
5550 -- STEP 5: Get remaining components according to discriminant values
5552 Step_5 : declare
5553 Dnode : Node_Id;
5554 Errors_Found : Boolean := False;
5555 Record_Def : Node_Id;
5556 Parent_Typ : Entity_Id;
5557 Parent_Typ_List : Elist_Id;
5558 Parent_Elmt : Elmt_Id;
5559 Root_Typ : Entity_Id;
5561 begin
5562 if Is_Derived_Type (Typ) and then Is_Tagged_Type (Typ) then
5563 Parent_Typ_List := New_Elmt_List;
5565 -- If this is an extension aggregate, the component list must
5566 -- include all components that are not in the given ancestor type.
5567 -- Otherwise, the component list must include components of all
5568 -- ancestors, starting with the root.
5570 if Nkind (N) = N_Extension_Aggregate then
5571 Root_Typ := Base_Type (Etype (Ancestor_Part (N)));
5573 else
5574 -- AI05-0115: check legality of aggregate for type with a
5575 -- private ancestor.
5577 Root_Typ := Root_Type (Typ);
5578 if Has_Private_Ancestor (Typ) then
5579 declare
5580 Ancestor : constant Entity_Id :=
5581 Find_Private_Ancestor (Typ);
5582 Ancestor_Unit : constant Entity_Id :=
5583 Cunit_Entity
5584 (Get_Source_Unit (Ancestor));
5585 Parent_Unit : constant Entity_Id :=
5586 Cunit_Entity (Get_Source_Unit
5587 (Base_Type (Etype (Ancestor))));
5588 begin
5589 -- Check whether we are in a scope that has full view
5590 -- over the private ancestor and its parent. This can
5591 -- only happen if the derivation takes place in a child
5592 -- unit of the unit that declares the parent, and we are
5593 -- in the private part or body of that child unit, else
5594 -- the aggregate is illegal.
5596 if Is_Child_Unit (Ancestor_Unit)
5597 and then Scope (Ancestor_Unit) = Parent_Unit
5598 and then In_Open_Scopes (Scope (Ancestor))
5599 and then
5600 (In_Private_Part (Scope (Ancestor))
5601 or else In_Package_Body (Scope (Ancestor)))
5602 then
5603 null;
5605 else
5606 Error_Msg_NE
5607 ("type of aggregate has private ancestor&!",
5608 N, Root_Typ);
5609 Error_Msg_N ("must use extension aggregate!", N);
5610 return;
5611 end if;
5612 end;
5613 end if;
5615 Dnode := Declaration_Node (Base_Type (Root_Typ));
5617 -- If we don't get a full declaration, then we have some error
5618 -- which will get signalled later so skip this part. Otherwise
5619 -- gather components of root that apply to the aggregate type.
5620 -- We use the base type in case there is an applicable stored
5621 -- constraint that renames the discriminants of the root.
5623 if Nkind (Dnode) = N_Full_Type_Declaration then
5624 Record_Def := Type_Definition (Dnode);
5625 Gather_Components
5626 (Base_Type (Typ),
5627 Component_List (Record_Def),
5628 Governed_By => New_Assoc_List,
5629 Into => Components,
5630 Report_Errors => Errors_Found);
5632 if Errors_Found then
5633 Error_Msg_N
5634 ("discriminant controlling variant part is not static",
5636 return;
5637 end if;
5638 end if;
5639 end if;
5641 Parent_Typ := Base_Type (Typ);
5642 while Parent_Typ /= Root_Typ loop
5643 Prepend_Elmt (Parent_Typ, To => Parent_Typ_List);
5644 Parent_Typ := Etype (Parent_Typ);
5646 -- Check whether a private parent requires the use of
5647 -- an extension aggregate. This test does not apply in
5648 -- an instantiation: if the generic unit is legal so is
5649 -- the instance.
5651 if Nkind (Parent (Base_Type (Parent_Typ))) =
5652 N_Private_Type_Declaration
5653 or else Nkind (Parent (Base_Type (Parent_Typ))) =
5654 N_Private_Extension_Declaration
5655 then
5656 if Nkind (N) /= N_Extension_Aggregate
5657 and then not In_Instance
5658 then
5659 Error_Msg_NE
5660 ("type of aggregate has private ancestor&!",
5661 N, Parent_Typ);
5662 Error_Msg_N ("must use extension aggregate!", N);
5663 return;
5665 elsif Parent_Typ /= Root_Typ then
5666 Error_Msg_NE
5667 ("ancestor part of aggregate must be private type&",
5668 Ancestor_Part (N), Parent_Typ);
5669 return;
5670 end if;
5672 -- The current view of ancestor part may be a private type,
5673 -- while the context type is always non-private.
5675 elsif Is_Private_Type (Root_Typ)
5676 and then Present (Full_View (Root_Typ))
5677 and then Nkind (N) = N_Extension_Aggregate
5678 then
5679 exit when Base_Type (Full_View (Root_Typ)) = Parent_Typ;
5680 end if;
5681 end loop;
5683 -- Now collect components from all other ancestors, beginning
5684 -- with the current type. If the type has unknown discriminants
5685 -- use the component list of the Underlying_Record_View, which
5686 -- needs to be used for the subsequent expansion of the aggregate
5687 -- into assignments.
5689 Parent_Elmt := First_Elmt (Parent_Typ_List);
5690 while Present (Parent_Elmt) loop
5691 Parent_Typ := Node (Parent_Elmt);
5693 if Has_Unknown_Discriminants (Parent_Typ)
5694 and then Present (Underlying_Record_View (Typ))
5695 then
5696 Parent_Typ := Underlying_Record_View (Parent_Typ);
5697 end if;
5699 Record_Def := Type_Definition (Parent (Base_Type (Parent_Typ)));
5700 Gather_Components (Parent_Typ,
5701 Component_List (Record_Extension_Part (Record_Def)),
5702 Governed_By => New_Assoc_List,
5703 Into => Components,
5704 Report_Errors => Errors_Found);
5706 Next_Elmt (Parent_Elmt);
5707 end loop;
5709 -- Typ is not a derived tagged type
5711 else
5712 Record_Def := Type_Definition (Parent (Base_Type (Typ)));
5714 if Null_Present (Record_Def) then
5715 null;
5717 elsif not Has_Unknown_Discriminants (Typ) then
5718 Gather_Components
5719 (Base_Type (Typ),
5720 Component_List (Record_Def),
5721 Governed_By => New_Assoc_List,
5722 Into => Components,
5723 Report_Errors => Errors_Found);
5725 else
5726 Gather_Components
5727 (Base_Type (Underlying_Record_View (Typ)),
5728 Component_List (Record_Def),
5729 Governed_By => New_Assoc_List,
5730 Into => Components,
5731 Report_Errors => Errors_Found);
5732 end if;
5733 end if;
5735 if Errors_Found then
5736 return;
5737 end if;
5738 end Step_5;
5740 -- STEP 6: Find component Values
5742 Component_Elmt := First_Elmt (Components);
5744 -- First scan the remaining positional associations in the aggregate.
5745 -- Remember that at this point Positional_Expr contains the current
5746 -- positional association if any is left after looking for discriminant
5747 -- values in step 3.
5749 while Present (Positional_Expr) and then Present (Component_Elmt) loop
5750 Component := Node (Component_Elmt);
5751 Resolve_Aggr_Expr (Positional_Expr, Component);
5753 -- Ada 2005 (AI-231)
5755 if Ada_Version >= Ada_2005 and then Known_Null (Positional_Expr) then
5756 Check_Can_Never_Be_Null (Component, Positional_Expr);
5757 end if;
5759 if Present (Get_Value (Component, Component_Associations (N))) then
5760 Error_Msg_NE
5761 ("more than one value supplied for component &", N, Component);
5762 end if;
5764 Next (Positional_Expr);
5765 Next_Elmt (Component_Elmt);
5766 end loop;
5768 if Present (Positional_Expr) then
5769 Error_Msg_N
5770 ("too many components for record aggregate", Positional_Expr);
5771 end if;
5773 -- Now scan for the named arguments of the aggregate
5775 while Present (Component_Elmt) loop
5776 Component := Node (Component_Elmt);
5777 Expr := Get_Value (Component, Component_Associations (N), True);
5779 -- Note: The previous call to Get_Value sets the value of the
5780 -- variable Is_Box_Present.
5782 -- Ada 2005 (AI-287): Handle components with default initialization.
5783 -- Note: This feature was originally added to Ada 2005 for limited
5784 -- but it was finally allowed with any type.
5786 if Is_Box_Present then
5787 Check_Box_Component : declare
5788 Ctyp : constant Entity_Id := Etype (Component);
5790 begin
5791 -- Initially assume that the box is for a default-initialized
5792 -- component and reset to False in cases where that's not true.
5794 Is_Box_Init_By_Default := True;
5796 -- If there is a default expression for the aggregate, copy
5797 -- it into a new association. This copy must modify the scopes
5798 -- of internal types that may be attached to the expression
5799 -- (e.g. index subtypes of arrays) because in general the type
5800 -- declaration and the aggregate appear in different scopes,
5801 -- and the backend requires the scope of the type to match the
5802 -- point at which it is elaborated.
5804 -- If the component has an initialization procedure (IP) we
5805 -- pass the component to the expander, which will generate
5806 -- the call to such IP.
5808 -- If the component has discriminants, their values must
5809 -- be taken from their subtype. This is indispensable for
5810 -- constraints that are given by the current instance of an
5811 -- enclosing type, to allow the expansion of the aggregate to
5812 -- replace the reference to the current instance by the target
5813 -- object of the aggregate.
5815 if Is_Case_Choice_Pattern (N) then
5817 -- Do not transform box component values in a case-choice
5818 -- aggregate.
5820 Add_Association
5821 (Component => Component,
5822 Expr => Empty,
5823 Assoc_List => New_Assoc_List,
5824 Is_Box_Present => True);
5826 elsif Present (Parent (Component))
5827 and then Nkind (Parent (Component)) = N_Component_Declaration
5828 and then Present (Expression (Parent (Component)))
5829 then
5830 -- If component declaration has an initialization expression
5831 -- then this is not a case of default initialization.
5833 Is_Box_Init_By_Default := False;
5835 Expr :=
5836 New_Copy_Tree_And_Copy_Dimensions
5837 (Expression (Parent (Component)),
5838 New_Scope => Current_Scope,
5839 New_Sloc => Sloc (N));
5841 -- As the type of the copied default expression may refer
5842 -- to discriminants of the record type declaration, these
5843 -- non-stored discriminants need to be rewritten into stored
5844 -- discriminant values for the aggregate. This is required
5845 -- in GNATprove mode, and is adopted in all modes to avoid
5846 -- special-casing GNATprove mode.
5848 if Is_Array_Type (Etype (Expr)) then
5849 declare
5850 Rec_Typ : constant Entity_Id := Scope (Component);
5851 -- Root record type whose discriminants may be used as
5852 -- bounds in range nodes.
5854 Assoc : Node_Id;
5855 Choice : Node_Id;
5856 Index : Node_Id;
5858 begin
5859 -- Rewrite the range nodes occurring in the indexes
5860 -- and their types.
5862 Index := First_Index (Etype (Expr));
5863 while Present (Index) loop
5864 Rewrite_Range (Rec_Typ, Index);
5865 Rewrite_Range
5866 (Rec_Typ, Scalar_Range (Etype (Index)));
5868 Next_Index (Index);
5869 end loop;
5871 -- Rewrite the range nodes occurring as aggregate
5872 -- bounds and component associations.
5874 if Nkind (Expr) = N_Aggregate then
5875 if Present (Aggregate_Bounds (Expr)) then
5876 Rewrite_Range (Rec_Typ, Aggregate_Bounds (Expr));
5877 end if;
5879 if Present (Component_Associations (Expr)) then
5880 Assoc := First (Component_Associations (Expr));
5881 while Present (Assoc) loop
5882 Choice := First (Choices (Assoc));
5883 while Present (Choice) loop
5884 Rewrite_Range (Rec_Typ, Choice);
5886 Next (Choice);
5887 end loop;
5889 Next (Assoc);
5890 end loop;
5891 end if;
5892 end if;
5893 end;
5894 end if;
5896 Add_Association
5897 (Component => Component,
5898 Expr => Expr,
5899 Assoc_List => New_Assoc_List);
5900 Set_Has_Self_Reference (N);
5902 elsif Needs_Simple_Initialization (Ctyp)
5903 or else Has_Non_Null_Base_Init_Proc (Ctyp)
5904 or else not Expander_Active
5905 then
5906 Add_Association
5907 (Component => Component,
5908 Expr => Empty,
5909 Assoc_List => New_Assoc_List,
5910 Is_Box_Present => True);
5912 -- Otherwise we only need to resolve the expression if the
5913 -- component has partially initialized values (required to
5914 -- expand the corresponding assignments and run-time checks).
5916 elsif Present (Expr)
5917 and then Is_Partially_Initialized_Type (Ctyp)
5918 then
5919 Resolve_Aggr_Expr (Expr, Component);
5920 end if;
5921 end Check_Box_Component;
5923 elsif No (Expr) then
5925 -- Ignore hidden components associated with the position of the
5926 -- interface tags: these are initialized dynamically.
5928 if No (Related_Type (Component)) then
5929 Error_Msg_NE
5930 ("no value supplied for component &!", N, Component);
5931 end if;
5933 else
5934 Resolve_Aggr_Expr (Expr, Component);
5935 end if;
5937 Next_Elmt (Component_Elmt);
5938 end loop;
5940 -- STEP 7: check for invalid components + check type in choice list
5942 Step_7 : declare
5943 Assoc : Node_Id;
5944 New_Assoc : Node_Id;
5946 Selectr : Node_Id;
5947 -- Selector name
5949 Typech : Entity_Id;
5950 -- Type of first component in choice list
5952 begin
5953 if Present (Component_Associations (N)) then
5954 Assoc := First (Component_Associations (N));
5955 else
5956 Assoc := Empty;
5957 end if;
5959 Verification : while Present (Assoc) loop
5960 Selectr := First (Choices (Assoc));
5961 Typech := Empty;
5963 if Nkind (Selectr) = N_Others_Choice then
5965 -- Ada 2005 (AI-287): others choice may have expression or box
5967 if No (Others_Etype) and then Others_Box = 0 then
5968 Error_Msg_N
5969 ("OTHERS must represent at least one component", Selectr);
5971 elsif Others_Box = 1 and then Warn_On_Redundant_Constructs then
5972 Error_Msg_N ("OTHERS choice is redundant?r?", Box_Node);
5973 Error_Msg_N
5974 ("\previous choices cover all components?r?", Box_Node);
5975 end if;
5977 exit Verification;
5978 end if;
5980 while Present (Selectr) loop
5981 Component := Empty;
5982 New_Assoc := First (New_Assoc_List);
5983 while Present (New_Assoc) loop
5984 Component := First (Choices (New_Assoc));
5986 if Chars (Selectr) = Chars (Component) then
5987 if Style_Check then
5988 Check_Identifier (Selectr, Entity (Component));
5989 end if;
5991 exit;
5992 end if;
5994 Next (New_Assoc);
5995 end loop;
5997 -- If we found an association, then this is a legal component
5998 -- of the type in question.
6000 pragma Assert (if Present (New_Assoc) then Present (Component));
6002 -- If no association, this is not a legal component of the type
6003 -- in question, unless its association is provided with a box.
6005 if No (New_Assoc) then
6006 if Box_Present (Parent (Selectr)) then
6008 -- This may still be a bogus component with a box. Scan
6009 -- list of components to verify that a component with
6010 -- that name exists.
6012 declare
6013 C : Entity_Id;
6015 begin
6016 C := First_Component (Typ);
6017 while Present (C) loop
6018 if Chars (C) = Chars (Selectr) then
6020 -- If the context is an extension aggregate,
6021 -- the component must not be inherited from
6022 -- the ancestor part of the aggregate.
6024 if Nkind (N) /= N_Extension_Aggregate
6025 or else
6026 Scope (Original_Record_Component (C)) /=
6027 Etype (Ancestor_Part (N))
6028 then
6029 exit;
6030 end if;
6031 end if;
6033 Next_Component (C);
6034 end loop;
6036 if No (C) then
6037 Error_Msg_Node_2 := Typ;
6038 Error_Msg_N ("& is not a component of}", Selectr);
6039 end if;
6040 end;
6042 elsif Chars (Selectr) /= Name_uTag
6043 and then Chars (Selectr) /= Name_uParent
6044 then
6045 if not Has_Discriminants (Typ) then
6046 Error_Msg_Node_2 := Typ;
6047 Error_Msg_N ("& is not a component of}", Selectr);
6048 else
6049 Error_Msg_N
6050 ("& is not a component of the aggregate subtype",
6051 Selectr);
6052 end if;
6054 Check_Misspelled_Component (Components, Selectr);
6055 end if;
6057 elsif No (Typech) then
6058 Typech := Base_Type (Etype (Component));
6060 -- AI05-0199: In Ada 2012, several components of anonymous
6061 -- access types can appear in a choice list, as long as the
6062 -- designated types match.
6064 elsif Typech /= Base_Type (Etype (Component)) then
6065 if Ada_Version >= Ada_2012
6066 and then Ekind (Typech) = E_Anonymous_Access_Type
6067 and then
6068 Ekind (Etype (Component)) = E_Anonymous_Access_Type
6069 and then Base_Type (Designated_Type (Typech)) =
6070 Base_Type (Designated_Type (Etype (Component)))
6071 and then
6072 Subtypes_Statically_Match (Typech, (Etype (Component)))
6073 then
6074 null;
6076 elsif not Box_Present (Parent (Selectr)) then
6077 Error_Msg_N
6078 ("components in choice list must have same type",
6079 Selectr);
6080 end if;
6081 end if;
6083 Next (Selectr);
6084 end loop;
6086 Next (Assoc);
6087 end loop Verification;
6088 end Step_7;
6090 -- STEP 8: replace the original aggregate
6092 Step_8 : declare
6093 New_Aggregate : constant Node_Id := New_Copy (N);
6095 begin
6096 Set_Expressions (New_Aggregate, No_List);
6097 Set_Etype (New_Aggregate, Etype (N));
6098 Set_Component_Associations (New_Aggregate, New_Assoc_List);
6099 Set_Check_Actuals (New_Aggregate, Check_Actuals (N));
6101 Rewrite (N, New_Aggregate);
6102 end Step_8;
6104 -- Check the dimensions of the components in the record aggregate
6106 Analyze_Dimension_Extension_Or_Record_Aggregate (N);
6107 end Resolve_Record_Aggregate;
6109 -----------------------------
6110 -- Check_Can_Never_Be_Null --
6111 -----------------------------
6113 procedure Check_Can_Never_Be_Null (Typ : Entity_Id; Expr : Node_Id) is
6114 Comp_Typ : Entity_Id;
6116 begin
6117 pragma Assert
6118 (Ada_Version >= Ada_2005
6119 and then Present (Expr)
6120 and then Known_Null (Expr));
6122 case Ekind (Typ) is
6123 when E_Array_Type =>
6124 Comp_Typ := Component_Type (Typ);
6126 when E_Component
6127 | E_Discriminant
6129 Comp_Typ := Etype (Typ);
6131 when others =>
6132 return;
6133 end case;
6135 if Can_Never_Be_Null (Comp_Typ) then
6137 -- Here we know we have a constraint error. Note that we do not use
6138 -- Apply_Compile_Time_Constraint_Error here to the Expr, which might
6139 -- seem the more natural approach. That's because in some cases the
6140 -- components are rewritten, and the replacement would be missed.
6141 -- We do not mark the whole aggregate as raising a constraint error,
6142 -- because the association may be a null array range.
6144 Error_Msg_N
6145 ("(Ada 2005) NULL not allowed in null-excluding component??", Expr);
6146 Error_Msg_N
6147 ("\Constraint_Error will be raised at run time??", Expr);
6149 Rewrite (Expr,
6150 Make_Raise_Constraint_Error
6151 (Sloc (Expr), Reason => CE_Access_Check_Failed));
6152 Set_Etype (Expr, Comp_Typ);
6153 Set_Analyzed (Expr);
6154 end if;
6155 end Check_Can_Never_Be_Null;
6157 ---------------------
6158 -- Sort_Case_Table --
6159 ---------------------
6161 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is
6162 U : constant Int := Case_Table'Last;
6163 K : Int;
6164 J : Int;
6165 T : Case_Bounds;
6167 begin
6168 K := 1;
6169 while K < U loop
6170 T := Case_Table (K + 1);
6172 J := K + 1;
6173 while J > 1
6174 and then Expr_Value (Case_Table (J - 1).Lo) > Expr_Value (T.Lo)
6175 loop
6176 Case_Table (J) := Case_Table (J - 1);
6177 J := J - 1;
6178 end loop;
6180 Case_Table (J) := T;
6181 K := K + 1;
6182 end loop;
6183 end Sort_Case_Table;
6185 end Sem_Aggr;