1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2023, Free Software Foundation, Inc. --
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. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
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
;
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
;
46 with Restrict
; use Restrict
;
47 with Rident
; use Rident
;
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
78 -- Low bound of choice. Once we sort the Case_Table, then entries
79 -- will be in order of ascending Choice_Lo values.
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.
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.
91 -- The node of the choice
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
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
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
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;
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
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
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
;
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
);
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
;
506 Expr_Value
(This_High
) /= Expr_Value
(Aggr_High
(Dim
))
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
);
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);
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);
541 end Collect_Aggr_Bounds
;
543 -- Array_Aggr_Subtype variables
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
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
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
573 This_Range
: Node_Id
:= Aggregate_Bounds
(N
);
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
));
587 Collect_Aggr_Bounds
(N
, 1);
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
;
599 -- Construct the Index subtype, and associate it with the range
600 -- construct that generates it.
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
);
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
)));
620 Set_Etype
(Aggr_Range
(J
), Index_Typ
);
622 Append
(Aggr_Range
(J
), To
=> Index_Constraints
);
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
));
659 Set_Predicated_Parent
(Itype
, Typ
);
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
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.
681 and then not In_Spec_Expression
682 and then Expander_Active
684 Freeze_Itype
(Itype
, N
);
688 end Array_Aggr_Subtype
;
690 --------------------------------
691 -- Check_Misspelled_Component --
692 --------------------------------
694 procedure Check_Misspelled_Component
695 (Elements
: Elist_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
;
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
)
715 if Is_Bad_Spelling_Of
716 (Chars
(Node
(Component_Elmt
)),
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
);
728 Next_Elmt
(Component_Elmt
);
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
);
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
750 if Is_Limited_Type
(Etype
(Expr
))
751 and then Comes_From_Source
(Expr
)
753 if In_Instance_Body
or else In_Inlined_Body
then
756 elsif not OK_For_Limited_Init
(Etype
(Expr
), Expr
) then
758 ("initialization not allowed for limited types", Expr
);
759 Explain_Limited_Type
(Etype
(Expr
), Expr
);
762 end Check_Expr_OK_In_Limited_Aggregate
;
768 function Is_Deep_Choice
770 Aggr_Type
: Type_Kind_Id
) return Boolean
772 Pref
: Node_Id
:= Choice
;
774 while not Is_Root_Prefix_Of_Deep_Choice
(Pref
) loop
775 Pref
:= Prefix
(Pref
);
778 if Is_Array_Type
(Aggr_Type
) then
779 return Paren_Count
(Pref
) > 0
780 and then Pref
/= Choice
;
782 return Pref
/= 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
);
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
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
);
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
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
);
841 return Ada_Version
>= Ada_2022
842 and then not Comes_From_Source
(Original_N
)
843 and then Nkind
(Original_N
) = N_Attribute_Reference
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
);
866 for J
in 1 .. Strlen
loop
867 C
:= Get_String_Char
(Str
, J
);
868 Set_Character_Literal_Name
(C
);
871 Make_Character_Literal
(P
,
873 Char_Literal_Value
=> UI_From_CC
(C
));
874 Set_Etype
(C_Node
, Any_Character
);
875 Append_To
(Exprs
, C_Node
);
878 -- Something special for wide strings???
881 New_N
:= Make_Aggregate
(Loc
, Expressions
=> Exprs
);
882 Set_Analyzed
(New_N
);
883 Set_Etype
(New_N
, Any_Composite
);
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
);
930 -- Aggregate may be qualified, so find outer context
932 if Nkind
(Par
) = N_Qualified_Expression
then
936 if not Comes_From_Source
(Par
) then
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
))
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
))
963 Temp
:= Make_Temporary
(Loc
, 'T', 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
);
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
);
985 while Present
(P
) loop
986 if Nkind
(P
) = N_Aggregate
then
994 end Within_Aggregate
;
996 -- Start of processing for Resolve_Aggregate
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
)
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
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.
1025 and then Present
(Component_Associations
(N
))
1029 First_Comp
: Boolean := True;
1032 Comp
:= First
(Component_Associations
(N
));
1033 while Present
(Comp
) loop
1034 if Box_Present
(Comp
) then
1036 and then No
(Expressions
(N
))
1037 and then Nkind
(First
(Choices
(Comp
))) = N_Others_Choice
1038 and then not Within_Aggregate
(N
)
1043 Insert_Actions
(N
, Freeze_Entity
(Typ
, N
));
1047 First_Comp
:= False;
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
)
1063 Error_Msg_CRT
("aggregate", N
);
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
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
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
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
);
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
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
)
1147 Expr
:= First
(Expressions
(N
));
1148 while Present
(Expr
) loop
1149 exit when Nkind
(Expr
) /= N_Character_Literal
;
1156 Expr
:= First
(Expressions
(N
));
1157 while Present
(Expr
) loop
1158 Store_String_Char
(UI_To_CC
(Char_Literal_Value
(Expr
)));
1162 Rewrite
(N
, Make_String_Literal
(Loc
, End_String
));
1164 Analyze_And_Resolve
(N
, Typ
);
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
);
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
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
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
1234 | N_Extension_Aggregate
1235 | N_Component_Association
1236 | N_Case_Expression_Alternative
1238 | N_Expression_With_Actions
)
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);
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);
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
1266 ("positional aggregate cannot have one component", N
);
1269 Aggr_Subtyp
:= Any_Composite
;
1272 Aggr_Subtyp
:= Array_Aggr_Subtype
(N
, Typ
);
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
))
1283 Resolve
(N
, Full_View
(Typ
));
1286 Error_Msg_N
("illegal context for aggregate", N
);
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
);
1297 Make_Raise_Constraint_Error
(Loc
, Reason
=> CE_Range_Check_Failed
));
1298 Set_Raises_Constraint_Error
(N
);
1299 Set_Etype
(N
, Aggr_Subtyp
);
1303 if Warn_On_No_Value_Assigned
1305 and then not Is_Fully_Initialized_Type
(Etype
(N
))
1307 Error_Msg_N
("?v?aggregate not fully initialized", N
);
1310 Check_Function_Writable_Actuals
(N
);
1311 end Resolve_Aggregate
;
1313 -----------------------------
1314 -- Resolve_Array_Aggregate --
1315 -----------------------------
1317 function Resolve_Array_Aggregate
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
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
1386 procedure Resolve_Iterated_Component_Association
1388 Index_Typ
: Entity_Id
);
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.
1401 function Add
(Val
: Uint
; To
: Node_Id
) return Node_Id
is
1407 if Raises_Constraint_Error
(To
) then
1411 -- First test if we can do constant folding
1413 if Compile_Time_Known_Value
(To
)
1414 or else Nkind
(To
) = N_Integer_Literal
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
1424 -- If we are dealing with enumeration return
1425 -- Index_Typ'Val (Expr_Pos)
1429 Make_Attribute_Reference
1431 Prefix
=> New_Occurrence_Of
(Index_Typ
, Loc
),
1432 Attribute_Name
=> Name_Val
,
1433 Expressions
=> New_List
(Expr_Pos
));
1439 -- If we are here no constant folding possible
1441 if not Is_Enumeration_Type
(Index_Base
) then
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)
1452 Make_Attribute_Reference
1454 Prefix
=> New_Occurrence_Of
(Index_Typ
, Loc
),
1455 Attribute_Name
=> Name_Pos
,
1456 Expressions
=> New_List
(Duplicate_Subexpr
(To
)));
1460 Left_Opnd
=> To_Pos
,
1461 Right_Opnd
=> Make_Integer_Literal
(Loc
, Val
));
1464 Make_Attribute_Reference
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
1481 Def_Id
:= Make_Temporary
(Loc
, 'R', Expr
);
1482 Set_Etype
(Def_Id
, Index_Typ
);
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
);
1503 procedure Check_Bound
(BH
: Node_Id
; AH
: in out Node_Id
) is
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
);
1531 procedure Check_Bounds
(L
, H
: Node_Id
; AL
, AH
: Node_Id
) is
1542 pragma Warnings
(Off
, OK_AL
);
1543 pragma Warnings
(Off
, OK_AH
);
1546 if Raises_Constraint_Error
(N
)
1547 or else Dynamic_Or_Null_Range
(AL
, AH
)
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
);
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
);
1577 procedure Check_Length
(L
, H
: Node_Id
; Len
: Uint
) is
1587 if Raises_Constraint_Error
(N
) then
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
1598 -- If null range length is zero
1600 if Val_L
> Val_H
then
1601 Range_Len
:= Uint_0
;
1603 Range_Len
:= Val_H
- Val_L
+ 1;
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
);
1614 ---------------------------
1615 -- Dynamic_Or_Null_Range --
1616 ---------------------------
1618 function Dynamic_Or_Null_Range
(L
, H
: Node_Id
) return Boolean is
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
;
1639 procedure Get
(Value
: out Uint
; From
: Node_Id
; OK
: out Boolean) is
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
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
)))
1653 Value
:= Expr_Value
(First
(Expressions
(From
)));
1660 -----------------------
1661 -- Resolve_Aggr_Expr --
1662 -----------------------
1664 function Resolve_Aggr_Expr
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
1676 -- Defend against previous errors
1678 if Nkind
(Expr
) = N_Error
1679 or else Error_Posted
(Expr
)
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
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
);
1708 Make_String_Into_Aggregate
(Expr
);
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
1718 ("\if single-component aggregate is intended, "
1719 & "write e.g. (1 ='> ...)", Expr
);
1726 -- If it's "... => <>", nothing to resolve
1728 if Nkind
(Expr
) = N_Component_Association
then
1729 pragma Assert
(Box_Present
(Expr
));
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
);
1743 -- If it's "... => <>", nothing to resolve
1745 if Nkind
(Expr
) = N_Component_Association
then
1746 pragma Assert
(Box_Present
(Expr
));
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.
1760 or else not Expander_Active
1761 or else In_Spec_Expression
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
);
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
)
1780 Apply_Predicate_Check
(Expr
, Component_Typ
);
1783 if Raises_Constraint_Error
(Expr
)
1784 and then Nkind
(Parent
(Expr
)) /= N_Component_Association
1786 Set_Raises_Constraint_Error
(N
);
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
);
1799 return Resolution_OK
;
1800 end Resolve_Aggr_Expr
;
1802 --------------------------------------------
1803 -- Resolve_Iterated_Component_Association --
1804 --------------------------------------------
1806 procedure Resolve_Iterated_Component_Association
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
1823 if Nkind
(N
) = N_Identifier
1824 and then Present
(Entity
(N
))
1825 and then Entity
(N
) = Id
1827 Set_Entity
(N
, Empty
);
1828 Set_Etype
(N
, Empty
);
1830 Set_Analyzed
(N
, False);
1832 end Remove_Reference
;
1834 procedure Remove_References
is new Traverse_Proc
(Remove_Reference
);
1841 Expr
: constant Node_Id
:= Expression
(N
);
1843 -- Start of processing for Resolve_Iterated_Component_Association
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
));
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
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.
1871 Choice
:= First
(Discrete_Choices
(N
));
1873 while Present
(Choice
) loop
1874 if Nkind
(Choice
) = N_Others_Choice
then
1875 Others_Present
:= True;
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
))
1885 Base_Type
(Entity
(Choice
)) = Base_Type
(Index_Typ
)
1890 Analyze_And_Resolve
(Choice
, Index_Typ
);
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
);
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
);
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
));
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
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
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
));
1961 while Present
(Alt_Node
) loop
1962 Check_Expr
(Expression
(Alt_Node
));
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
;
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
);
1992 if Known_Null
(Else_Expr
) then
1993 Warn_On_Null_Expression_And_Rewrite
(Else_Expr
);
1995 end Check_Cond_Expr
;
2001 procedure Check_Expr
(Expr
: Node_Id
) is
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
);
2014 -----------------------------------------
2015 -- Warn_On_Null_Expression_And_Rewrite --
2016 -----------------------------------------
2018 procedure Warn_On_Null_Expression_And_Rewrite
(Null_Expr
: Node_Id
) is
2021 ("(Ada 2005) NULL not allowed in null-excluding component??",
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
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
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
);
2051 pragma Assert
(False);
2054 end Warn_On_Null_Component_Association
;
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
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
)
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
)
2113 ("?j?array aggregate using () is an" &
2114 " obsolescent syntax, use '['] instead", N
);
2117 -- STEP 1: make sure the aggregate is correctly formatted
2119 if Is_Null_Aggregate
(N
) then
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
))
2131 -- All other component associations must have an iterator spec.
2134 while Present
(Assoc
) loop
2135 if Nkind
(Assoc
) /= N_Iterated_Component_Association
2136 or else No
(Iterator_Specification
(Assoc
))
2138 Error_Msg_N
("mixed iterated component association"
2139 & " (RM 4.3.3 (17.1/5))",
2147 Has_Iterator_Specifications
:= True;
2150 -- or none of them do.
2153 while Present
(Assoc
) loop
2154 if Nkind
(Assoc
) = N_Iterated_Component_Association
2155 and then Present
(Iterator_Specification
(Assoc
))
2157 Error_Msg_N
("mixed iterated component association"
2158 & " (RM 4.3.3 (17.1/5))",
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
2175 ("invalid component association for aggregate", Assoc
);
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
))
2189 ("OTHERS must appear alone in a choice list", Choice
);
2193 if Present
(Next
(Assoc
)) then
2195 ("OTHERS must appear last in an aggregate", Choice
);
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
2205 ("(Ada 83) illegal context for OTHERS choice", N
);
2208 elsif Is_Entity_Name
(Choice
) then
2212 E
: constant Entity_Id
:= Entity
(Choice
);
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
)
2225 ("subtype& has non-static predicate, not allowed "
2226 & "in aggregate choice", Choice
, E
);
2228 elsif not Is_OK_Static_Subtype
(E
) then
2230 ("non-static subtype& has predicate, not allowed "
2231 & "in aggregate choice", Choice
, E
);
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;
2243 P
:= First
(Static_Discrete_Predicate
(E
));
2244 while Present
(P
) loop
2246 Set_Sloc
(C
, Sloc
(Choice
));
2247 Append_To
(New_Cs
, C
);
2251 Insert_List_After
(Choice
, New_Cs
);
2257 Nb_Choices
:= Nb_Choices
+ 1;
2260 C
: constant Node_Id
:= Choice
;
2265 if Delete_Choice
then
2267 Nb_Choices
:= Nb_Choices
- 1;
2268 Delete_Choice
:= False;
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
))
2286 ("cannot mix named and positional associations in array aggregate",
2287 First
(Choice_List
(First
(Component_Associations
(N
)))));
2291 -- Test for the validity of an others choice if present
2293 if Others_Present
and then not Others_Allowed
then
2295 Others_N
: constant Node_Id
:=
2296 First
(Choice_List
(First
(Component_Associations
(N
))));
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
);
2305 -- Protect against cascaded errors
2307 if Etype
(Index_Typ
) = Any_Type
then
2311 -- STEP 2: Process named components
2313 if No
(Expressions
(N
)) then
2314 if Others_Present
then
2315 Case_Table_Size
:= Nb_Choices
- 1;
2317 Case_Table_Size
:= Nb_Choices
;
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.
2329 function Empty_Range
(A
: Node_Id
) return Boolean is
2333 if Nkind
(A
) = N_Iterated_Component_Association
then
2334 R
:= First
(Discrete_Choices
(A
));
2336 R
:= First
(Choices
(A
));
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
;
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
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
))
2380 Prev_Nb_Discrete_Choices
:= Nb_Discrete_Choices
;
2381 Choice
:= First
(Choice_List
(Assoc
));
2386 if Nkind
(Choice
) = N_Others_Choice
then
2387 Single_Choice
:= False;
2390 -- Test for subtype mark without constraint
2392 elsif Is_Entity_Name
(Choice
) and then
2393 Is_Type
(Entity
(Choice
))
2395 if Base_Type
(Entity
(Choice
)) /= Index_Base
then
2397 ("invalid subtype mark in aggregate choice",
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
)))
2413 ("subtype& has non-static predicate, "
2414 & "not allowed in aggregate choice",
2415 Choice
, Entity
(Subtype_Mark
(Choice
)));
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
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;
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);
2446 -- If we could not resolve the discrete choice stop here
2448 if Etype
(Choice
) = Any_Type
then
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
2460 Get_Index_Bounds
(Choice
, Low
, High
);
2463 if (Dynamic_Or_Null_Range
(Low
, High
)
2464 or else (Nkind
(Choice
) = N_Subtype_Indication
2466 Dynamic_Or_Null_Range
(S_Low
, S_High
)))
2467 and then Nb_Choices
/= 1
2470 ("dynamic or empty choice in aggregate "
2471 & "must be the only choice", Choice
);
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
))
2480 Check_Restriction
(No_Dynamic_Sized_Objects
, Choice
);
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
;
2492 -- Check if we have a single discrete choice and whether
2493 -- this discrete choice specifies a single value.
2496 Nb_Discrete_Choices
= Prev_Nb_Discrete_Choices
+ 1
2497 and then Low
= High
;
2503 -- Ada 2005 (AI-231)
2505 if Ada_Version
>= Ada_2005
2506 and then not Empty_Range
(Assoc
)
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;
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
2526 Warn_On_Null_Component_Association
(Expression
(Assoc
));
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
2541 if not Resolve_Aggr_Expr
2542 (Assoc
, Single_Elmt
=> Single_Choice
)
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
)
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
2571 Save_Analysis
: constant Boolean := Full_Analysis
;
2572 Expr
: constant Node_Id
:=
2573 New_Copy_Tree
(Expression
(Assoc
));
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
)));
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
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
2601 (Expression
(Assoc
), Component_Typ
);
2604 Full_Analysis
:= Save_Analysis
;
2605 Expander_Mode_Restore
;
2607 if Is_Tagged_Type
(Etype
(Expr
)) then
2608 Check_Dynamically_Tagged_Expression
2610 Typ
=> Component_Type
(Etype
(N
)),
2615 elsif Is_Tagged_Type
(Etype
(Expression
(Assoc
))) then
2616 Check_Dynamically_Tagged_Expression
2617 (Expr
=> Expression
(Assoc
),
2618 Typ
=> Component_Type
(Etype
(N
)),
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
2635 -- Location of choice for messages
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.
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
2660 -- Enumeration type case
2662 if Is_Enumeration_Type
(Index_Typ
) then
2664 Chars
(Get_Enum_Lit_From_Pos
(Index_Typ
, Lo
, Loc
));
2666 Chars
(Get_Enum_Lit_From_Pos
(Index_Typ
, Hi
, Loc
));
2669 Error_Msg_N
("\\ %!", C
);
2671 Error_Msg_N
("\\ % .. %!", C
);
2674 -- Integer types case
2677 Error_Msg_Uint_1
:= Lo
;
2678 Error_Msg_Uint_2
:= Hi
;
2681 Error_Msg_N
("\\ ^!", C
);
2683 Error_Msg_N
("\\ ^ .. ^!", C
);
2686 end Output_Bad_Choices
;
2688 -- Start of processing for Check_Choices
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
);
2702 or else (Lo_Val
> Hi_Val
+ 1
2703 and then not Others_Present
)
2705 Missing_Or_Duplicates
:= True;
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
:=
2720 (Table
(J
- 1).Highest
, Expr_Value
(Table
(J
).Hi
));
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.
2746 UI_Max
(Lo_Val
, Expr_Value
(Table
(K
).Lo
));
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
2755 -- Otherwise place proper message
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
)
2764 Choice
:= Table
(K
).Choice
;
2765 Error_Msg_Sloc
:= Sloc
(Table
(J
).Choice
);
2767 Choice
:= Table
(J
).Choice
;
2768 Error_Msg_Sloc
:= Sloc
(Table
(K
).Choice
);
2771 if Lo_Dup
= Hi_Dup
then
2773 ("index value in array aggregate "
2774 & "duplicates the one given#!", Choice
);
2777 ("index values in array aggregate "
2778 & "duplicate those given#!", Choice
);
2781 Output_Bad_Choices
(Lo_Dup
, Hi_Dup
, Choice
);
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
2798 Error_Node
: Node_Id
;
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
;
2815 if Hi_Val
+ 1 = Lo_Val
- 1 then
2817 ("missing index value "
2818 & "in array aggregate!", Error_Node
);
2821 ("missing index values "
2822 & "in array aggregate!", Error_Node
);
2826 (Hi_Val
+ 1, Lo_Val
- 1, Error_Node
);
2832 -- If either missing or duplicate values, return failure
2834 Set_Etype
(N
, Any_Composite
);
2840 if Has_Iterator_Specifications
then
2841 -- Bounds will be determined dynamically.
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
;
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
2864 Nkind
(Aggr_High
) = N_Error
2869 -- No others clause present
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
2884 Nkind
(Aggr_High
) = N_Error
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
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
)
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
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
);
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
))
2945 ("missing index value(s) in array aggregate??",
2948 -- Output missing value(s) at start
2950 if Chars
(ALo
) /= Chars
(CLo
) then
2953 if Chars
(ALo
) = Chars
(Ent
) then
2954 Error_Msg_Name_1
:= Chars
(ALo
);
2955 Error_Msg_N
("\ %??", N
);
2957 Error_Msg_Name_1
:= Chars
(ALo
);
2958 Error_Msg_Name_2
:= Chars
(Ent
);
2959 Error_Msg_N
("\ % .. %??", N
);
2963 -- Output missing value(s) at end
2965 if Chars
(AHi
) /= Chars
(CHi
) then
2968 if Chars
(AHi
) = Chars
(Ent
) then
2969 Error_Msg_Name_1
:= Chars
(Ent
);
2970 Error_Msg_N
("\ %??", N
);
2972 Error_Msg_Name_1
:= Chars
(Ent
);
2973 Error_Msg_Name_2
:= Chars
(AHi
);
2974 Error_Msg_N
("\ % .. %??", N
);
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
)
2989 not Is_Constrained
(First_Subtype
(Etype
(N
)))
2992 ("bounds of aggregate do not match target??", N
);
2998 -- If no others, aggregate bounds come from aggregate
3000 Aggr_Low
:= Choices_Low
;
3001 Aggr_High
:= Choices_High
;
3005 -- STEP 3: Process positional components
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
);
3021 if not Resolve_Aggr_Expr
(Expr
, Single_Elmt
=> True) then
3025 -- Check incorrect use of dynamically tagged expression
3027 if Is_Tagged_Type
(Etype
(Expr
)) then
3028 Check_Dynamically_Tagged_Expression
3030 Typ
=> Component_Type
(Etype
(N
)),
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
));
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
3057 if not Resolve_Aggr_Expr
(Assoc
, Single_Elmt
=> False) then
3061 elsif not Resolve_Aggr_Expr
(Expression
(Assoc
),
3062 Single_Elmt
=> False)
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
3073 Save_Analysis
: constant Boolean := Full_Analysis
;
3074 Expr
: constant Node_Id
:=
3075 New_Copy_Tree
(Expression
(Assoc
));
3078 Expander_Mode_Save_And_Set
(False);
3079 Full_Analysis
:= False;
3081 Full_Analysis
:= Save_Analysis
;
3082 Expander_Mode_Restore
;
3084 if Is_Tagged_Type
(Etype
(Expr
)) then
3085 Check_Dynamically_Tagged_Expression
3087 Typ
=> Component_Type
(Etype
(N
)),
3094 -- STEP 3 (B): Compute the aggregate bounds
3096 if Others_Present
then
3097 Get_Index_Bounds
(Index_Constr
, Aggr_Low
, Aggr_High
);
3100 if Others_Allowed
then
3101 Get_Index_Bounds
(Index_Constr
, Aggr_Low
, Discard
);
3103 Aggr_Low
:= Index_Typ_Low
;
3106 Aggr_High
:= Add
(Nb_Elements
- 1, To
=> Aggr_Low
);
3107 Check_Bound
(Index_Base_High
, Aggr_High
);
3111 -- STEP 4: Perform static aggregate checks and save the bounds
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
);
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
);
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
);
3135 if Raises_Constraint_Error
(Aggr_Low
)
3136 or else Raises_Constraint_Error
(Aggr_High
)
3138 Set_Raises_Constraint_Error
(N
);
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);
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
3161 if Present
(Aggregate_Bounds
(N
))
3162 and then not Others_Allowed
3163 and then not Comes_From_Source
(N
)
3165 Aggr_Low
:= Low_Bound
(Aggregate_Bounds
(N
));
3166 Aggr_High
:= High_Bound
(Aggregate_Bounds
(N
));
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
3182 (Aggregate_Bounds
(N
),
3183 Duplicate_Subexpr
(High_Bound
(Aggregate_Bounds
(N
))));
3186 -- Check the dimensions of each component in the array aggregate
3188 Analyze_Dimension_Array_Aggregate
(N
, Component_Typ
);
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
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
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
3225 Key_Type
: Entity_Id
;
3226 Elmt_Type
: Entity_Id
)
3228 Loc
: constant Source_Ptr
:= Sloc
(N
);
3236 Typ
: Entity_Id
:= Empty
;
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
));
3257 if Present
(Loop_Parameter_Specification
(Comp
)) then
3258 Copy
:= Copy_Separate_Tree
(Comp
);
3261 (Loop_Parameter_Specification
(Copy
));
3263 Id_Name
:= Chars
(Defining_Identifier
3264 (Loop_Parameter_Specification
(Comp
)));
3266 Copy
:= Copy_Separate_Tree
(Iterator_Specification
(Comp
));
3269 Id_Name
:= Chars
(Defining_Identifier
3270 (Iterator_Specification
(Comp
)));
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
);
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
));
3295 Copy
:= Copy_Separate_Tree
(Iterator_Specification
(Comp
));
3297 Chars
(Defining_Identifier
(Iterator_Specification
(Comp
)));
3303 Typ
:= Etype
(Defining_Identifier
(Copy
));
3306 Choice
:= First
(Discrete_Choices
(Comp
));
3308 while Present
(Choice
) loop
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
)
3319 elsif Present
(Key_Type
) then
3320 Analyze_And_Resolve
(Choice
, Key_Type
);
3323 Typ
:= Etype
(Choice
); -- assume unique for now
3329 Id_Name
:= Chars
(Defining_Identifier
(Comp
));
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
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
));
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.
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
);
3368 end Resolve_Iterated_Association
;
3370 -- Start of processing for Resolve_Container_Aggregate
3373 pragma Assert
(Nkind
(Asp
) = N_Aggregate
);
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
3386 Elmt_Type
: constant Entity_Id
:=
3388 (First_Formal
(Entity
(Add_Unnamed_Subp
))));
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
);
3402 -- Empty aggregate, to be replaced by Empty during
3403 -- expansion, or iterated component association.
3405 if Present
(Component_Associations
(N
)) then
3407 Comp
: Node_Id
:= First
(Component_Associations
(N
));
3409 while Present
(Comp
) loop
3411 N_Iterated_Component_Association
3413 Error_Msg_N
("illegal component association "
3414 & "for unnamed container aggregate", Comp
);
3417 Resolve_Iterated_Association
3418 (Comp
, Empty
, Elmt_Type
);
3427 elsif Present
(Add_Named_Subp
)
3428 and then Etype
(Add_Named_Subp
) /= Any_Type
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
)));
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
);
3457 Analyze_And_Resolve
(Expression
(Comp
), Elmt_Type
);
3459 elsif Nkind
(Comp
) in
3460 N_Iterated_Component_Association |
3461 N_Iterated_Element_Association
3463 Resolve_Iterated_Association
3464 (Comp
, Key_Type
, Elmt_Type
);
3471 elsif Present
(Assign_Indexed_Subp
)
3472 and then Etype
(Assign_Indexed_Subp
) /= Any_Type
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.
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
)));
3486 Num_Choices
: Nat
:= 0;
3491 if Present
(Expressions
(N
)) then
3492 Comp
:= First
(Expressions
(N
));
3493 while Present
(Comp
) loop
3494 Analyze_And_Resolve
(Comp
, Comp_Type
);
3499 if Present
(Component_Associations
(N
))
3500 and then not Is_Empty_List
(Component_Associations
(N
))
3502 if Present
(Expressions
(N
))
3503 and then not Is_Empty_List
(Expressions
(N
))
3505 Error_Msg_N
("container aggregate cannot be "
3506 & "both positional and named", N
);
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;
3522 Analyze_And_Resolve
(Expression
(Comp
), Comp_Type
);
3524 elsif Nkind
(Comp
) in
3525 N_Iterated_Component_Association |
3526 N_Iterated_Element_Association
3528 Resolve_Iterated_Association
3529 (Comp
, Index_Type
, Comp_Type
);
3530 Num_Choices
:= Num_Choices
+ 1;
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
3545 Table
: Case_Table_Type
(1 .. Num_Choices
);
3546 No_Choice
: Pos
:= 1;
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.
3554 Comp
:= First
(Component_Associations
(N
));
3555 while Present
(Comp
) loop
3556 if Nkind
(Comp
) = N_Iterated_Element_Association
then
3558 (Loop_Parameter_Specification
(Comp
))
3560 if Present
(Iterator_Filter
3561 (Loop_Parameter_Specification
(Comp
)))
3564 ("iterator filter not allowed " &
3565 "in indexed aggregate", Comp
);
3568 elsif Present
(Key_Expression
3569 (Loop_Parameter_Specification
(Comp
)))
3572 ("key expression not allowed " &
3573 "in indexed aggregate", Comp
);
3578 Choice
:= First
(Choices
(Comp
));
3580 while Present
(Choice
) loop
3581 Get_Index_Bounds
(Choice
, Lo
, Hi
);
3582 Table
(No_Choice
).Choice
:= Choice
;
3583 Table
(No_Choice
).Lo
:= Lo
;
3584 Table
(No_Choice
).Hi
:= Hi
;
3586 -- Verify staticness of value or range
3588 if not Is_Static_Expression
(Lo
)
3589 or else not Is_Static_Expression
(Hi
)
3592 ("nonstatic expression for index " &
3593 "for indexed aggregate", Choice
);
3597 No_Choice
:= No_Choice
+ 1;
3605 Sort_Case_Table
(Table
);
3607 for J
in 1 .. Num_Choices
- 1 loop
3608 Hi_Val
:= Expr_Value
(Table
(J
).Hi
);
3609 Lo_Val
:= Expr_Value
(Table
(J
+ 1).Lo
);
3611 if Lo_Val
= Hi_Val
then
3613 ("duplicate index in indexed aggregate",
3614 Table
(J
+ 1).Choice
);
3617 elsif Lo_Val
< Hi_Val
then
3619 ("overlapping indices in indexed aggregate",
3620 Table
(J
+ 1).Choice
);
3623 elsif Lo_Val
> Hi_Val
+ 1 then
3625 ("missing index values", Table
(J
+ 1).Choice
);
3634 end Resolve_Container_Aggregate
;
3636 -----------------------------
3637 -- Resolve_Delta_Aggregate --
3638 -----------------------------
3640 procedure Resolve_Delta_Aggregate
(N
: Node_Id
; Typ
: Entity_Id
) is
3641 Base
: constant Node_Id
:= Expression
(N
);
3644 Error_Msg_Ada_2022_Feature
("delta aggregate", Sloc
(N
));
3646 if not Is_Composite_Type
(Typ
) then
3647 Error_Msg_N
("not a composite type", N
);
3650 Analyze_And_Resolve
(Base
, Typ
);
3652 if Is_Array_Type
(Typ
) then
3653 -- For an array_delta_aggregate, the base_expression and each
3654 -- expression in every array_component_association shall be of a
3655 -- nonlimited type; RM 4.3.4(13/5). However, to prevent repeated
3656 -- errors we only check the base expression and not array component
3659 if Is_Limited_Type
(Etype
(Base
)) then
3661 ("array delta aggregate shall be of a nonlimited type", Base
);
3662 Explain_Limited_Type
(Etype
(Base
), Base
);
3665 Resolve_Delta_Array_Aggregate
(N
, Typ
);
3668 -- Delta aggregates for record types must use parentheses,
3669 -- not square brackets.
3671 if Is_Homogeneous_Aggregate
(N
) then
3673 ("delta aggregates for record types must use (), not '[']", N
);
3676 -- The base_expression of a record_delta_aggregate can be of a
3677 -- limited type only if it is newly constructed; RM 7.5(2.1/5).
3679 Check_Expr_OK_In_Limited_Aggregate
(Base
);
3681 Resolve_Delta_Record_Aggregate
(N
, Typ
);
3685 end Resolve_Delta_Aggregate
;
3687 -----------------------------------
3688 -- Resolve_Delta_Array_Aggregate --
3689 -----------------------------------
3691 procedure Resolve_Delta_Array_Aggregate
(N
: Node_Id
; Typ
: Entity_Id
) is
3692 Deltas
: constant List_Id
:= Component_Associations
(N
);
3693 Index_Type
: constant Entity_Id
:= Etype
(First_Index
(Typ
));
3699 Deep_Choice_Seen
: Boolean := False;
3702 Assoc
:= First
(Deltas
);
3703 while Present
(Assoc
) loop
3704 if Nkind
(Assoc
) = N_Iterated_Component_Association
then
3705 Choice
:= First
(Choice_List
(Assoc
));
3706 while Present
(Choice
) loop
3707 if Nkind
(Choice
) = N_Others_Choice
then
3709 ("OTHERS not allowed in delta aggregate", Choice
);
3711 elsif Nkind
(Choice
) = N_Subtype_Indication
then
3712 Resolve_Discrete_Subtype_Indication
3713 (Choice
, Base_Type
(Index_Type
));
3716 Analyze_And_Resolve
(Choice
, Index_Type
);
3723 Id
: constant Entity_Id
:= Defining_Identifier
(Assoc
);
3724 Ent
: constant Entity_Id
:=
3726 (E_Loop
, Current_Scope
, Sloc
(Assoc
), 'L');
3729 Set_Etype
(Ent
, Standard_Void_Type
);
3730 Set_Parent
(Ent
, Assoc
);
3733 if No
(Scope
(Id
)) then
3734 Set_Etype
(Id
, Index_Type
);
3735 Mutate_Ekind
(Id
, E_Variable
);
3736 Set_Is_Not_Self_Hidden
(Id
);
3737 Set_Scope
(Id
, Ent
);
3741 -- Resolve a copy of the expression, after setting
3742 -- its parent properly to preserve its context.
3744 Expr
:= New_Copy_Tree
(Expression
(Assoc
));
3745 Set_Parent
(Expr
, Assoc
);
3746 Analyze_And_Resolve
(Expr
, Component_Type
(Typ
));
3751 Choice
:= First
(Choice_List
(Assoc
));
3752 while Present
(Choice
) loop
3753 if Is_Deep_Choice
(Choice
, Typ
) then
3754 pragma Assert
(All_Extensions_Allowed
);
3755 Deep_Choice_Seen
:= True;
3757 -- a deep delta aggregate
3758 Resolve_Deep_Delta_Assoc
(Assoc
, Typ
);
3762 if Nkind
(Choice
) = N_Others_Choice
then
3764 ("OTHERS not allowed in delta aggregate", Choice
);
3766 elsif Is_Entity_Name
(Choice
)
3767 and then Is_Type
(Entity
(Choice
))
3769 -- Choice covers a range of values
3771 if Base_Type
(Entity
(Choice
)) /=
3772 Base_Type
(Index_Type
)
3775 ("choice does not match index type of &",
3779 elsif Nkind
(Choice
) = N_Subtype_Indication
then
3780 Resolve_Discrete_Subtype_Indication
3781 (Choice
, Base_Type
(Index_Type
));
3784 Resolve
(Choice
, Index_Type
);
3791 -- For an array_delta_aggregate, the array_component_association
3792 -- shall not use the box symbol <>; RM 4.3.4(11/5).
3795 (Box_Present
(Assoc
) xor Present
(Expression
(Assoc
)));
3797 if Box_Present
(Assoc
) then
3799 ("'<'> in array delta aggregate is not allowed", Assoc
);
3800 elsif not Deep_Choice_Seen
then
3801 Analyze_And_Resolve
(Expression
(Assoc
), Component_Type
(Typ
));
3807 end Resolve_Delta_Array_Aggregate
;
3809 ------------------------------------
3810 -- Resolve_Delta_Record_Aggregate --
3811 ------------------------------------
3813 procedure Resolve_Delta_Record_Aggregate
(N
: Node_Id
; Typ
: Entity_Id
) is
3815 -- Variables used to verify that discriminant-dependent components
3816 -- appear in the same variant.
3818 Comp_Ref
: Entity_Id
:= Empty
; -- init to avoid warning
3821 procedure Check_Variant
(Id
: Node_Id
);
3822 -- If a given component of the delta aggregate appears in a variant
3823 -- part, verify that it is within the same variant as that of previous
3824 -- specified variant components of the delta.
3826 function Get_Component_Type
3827 (Selector
: Node_Id
; Enclosing_Type
: Entity_Id
) return Entity_Id
;
3828 -- Locate component with a given name and return its type.
3829 -- If none found then report error and return Empty.
3831 function Nested_In
(V1
: Node_Id
; V2
: Node_Id
) return Boolean;
3832 -- Determine whether variant V1 is within variant V2
3834 function Variant_Depth
(N
: Node_Id
) return Natural;
3835 -- Determine the distance of a variant to the enclosing type declaration
3837 --------------------
3839 --------------------
3841 procedure Check_Variant
(Id
: Node_Id
) is
3843 Comp_Variant
: Node_Id
;
3846 if not Has_Discriminants
(Typ
) then
3850 Comp
:= First_Entity
(Typ
);
3851 while Present
(Comp
) loop
3852 exit when Chars
(Comp
) = Chars
(Id
);
3853 Next_Component
(Comp
);
3856 -- Find the variant, if any, whose component list includes the
3857 -- component declaration.
3859 Comp_Variant
:= Parent
(Parent
(List_Containing
(Parent
(Comp
))));
3860 if Nkind
(Comp_Variant
) = N_Variant
then
3861 if No
(Variant
) then
3862 Variant
:= Comp_Variant
;
3865 elsif Variant
/= Comp_Variant
then
3867 D1
: constant Integer := Variant_Depth
(Variant
);
3868 D2
: constant Integer := Variant_Depth
(Comp_Variant
);
3873 (D1
> D2
and then not Nested_In
(Variant
, Comp_Variant
))
3875 (D2
> D1
and then not Nested_In
(Comp_Variant
, Variant
))
3877 pragma Assert
(Present
(Comp_Ref
));
3878 Error_Msg_Node_2
:= Comp_Ref
;
3880 ("& and & appear in different variants", Id
, Comp
);
3882 -- Otherwise retain the deeper variant for subsequent tests
3885 Variant
:= Comp_Variant
;
3892 ------------------------
3893 -- Get_Component_Type --
3894 ------------------------
3896 function Get_Component_Type
3897 (Selector
: Node_Id
; Enclosing_Type
: Entity_Id
) return Entity_Id
3901 case Nkind
(Selector
) is
3902 when N_Selected_Component | N_Indexed_Component
=>
3903 -- a deep delta aggregate choice
3906 Prefix_Type
: constant Entity_Id
:=
3907 Get_Component_Type
(Prefix
(Selector
), Enclosing_Type
);
3909 if No
(Prefix_Type
) then
3910 pragma Assert
(Serious_Errors_Detected
> 0);
3914 -- Set the type of the prefix for GNATprove
3916 Set_Etype
(Prefix
(Selector
), Prefix_Type
);
3918 if Nkind
(Selector
) = N_Selected_Component
then
3919 return Get_Component_Type
3920 (Selector_Name
(Selector
),
3921 Enclosing_Type
=> Prefix_Type
);
3922 elsif not Is_Array_Type
(Prefix_Type
) then
3924 ("type& is not an array type",
3925 Selector
, Prefix_Type
);
3926 elsif Number_Dimensions
(Prefix_Type
) /= 1 then
3928 ("array type& not one-dimensional",
3929 Selector
, Prefix_Type
);
3930 elsif List_Length
(Expressions
(Selector
)) /= 1 then
3932 ("wrong number of indices for array type&",
3933 Selector
, Prefix_Type
);
3936 (First
(Expressions
(Selector
)),
3937 Etype
(First_Index
(Prefix_Type
)));
3938 return Component_Type
(Prefix_Type
);
3946 Comp
:= First_Entity
(Enclosing_Type
);
3947 while Present
(Comp
) loop
3948 if Chars
(Comp
) = Chars
(Selector
) then
3949 if Ekind
(Comp
) = E_Discriminant
then
3950 Error_Msg_N
("delta cannot apply to discriminant", Selector
);
3953 Set_Entity
(Selector
, Comp
);
3954 Set_Etype
(Selector
, Etype
(Comp
));
3956 return Etype
(Comp
);
3963 ("type& has no component with this name", Selector
, Enclosing_Type
);
3965 end Get_Component_Type
;
3971 function Nested_In
(V1
, V2
: Node_Id
) return Boolean is
3976 while Nkind
(Par
) /= N_Full_Type_Declaration
loop
3981 Par
:= Parent
(Par
);
3991 function Variant_Depth
(N
: Node_Id
) return Natural is
3998 while Nkind
(Par
) /= N_Full_Type_Declaration
loop
4000 Par
:= Parent
(Par
);
4008 Deltas
: constant List_Id
:= Component_Associations
(N
);
4012 Comp_Type
: Entity_Id
:= Empty
; -- init to avoid warning
4013 Deep_Choice
: Boolean;
4015 -- Start of processing for Resolve_Delta_Record_Aggregate
4020 Assoc
:= First
(Deltas
);
4021 while Present
(Assoc
) loop
4022 Choice
:= First
(Choice_List
(Assoc
));
4023 while Present
(Choice
) loop
4024 Deep_Choice
:= Nkind
(Choice
) /= N_Identifier
;
4026 Error_Msg_GNAT_Extension
4027 ("deep delta aggregate", Sloc
(Choice
));
4030 Comp_Type
:= Get_Component_Type
4031 (Selector
=> Choice
, Enclosing_Type
=> Typ
);
4033 -- Set the type of the choice for GNATprove
4036 Set_Etype
(Choice
, Comp_Type
);
4039 if Present
(Comp_Type
) then
4040 if not Deep_Choice
then
4041 -- ??? Not clear yet how RM 4.3.1(17.7) applies to a
4042 -- deep delta aggregate.
4043 Check_Variant
(Choice
);
4046 Comp_Type
:= Any_Type
;
4052 pragma Assert
(Present
(Comp_Type
));
4054 -- A record_component_association in record_delta_aggregate shall not
4055 -- use the box compound delimiter <> rather than an expression; see
4056 -- RM 4.3.1(17.3/5).
4058 pragma Assert
(Present
(Expression
(Assoc
)) xor Box_Present
(Assoc
));
4060 if Box_Present
(Assoc
) then
4062 ("'<'> in record delta aggregate is not allowed", Assoc
);
4064 Analyze_And_Resolve
(Expression
(Assoc
), Comp_Type
);
4066 -- The expression must not be of a limited type; RM 4.3.1(17.4/5)
4068 if Is_Limited_Type
(Etype
(Expression
(Assoc
))) then
4070 ("expression of a limited type in record delta aggregate " &
4072 Expression
(Assoc
));
4078 end Resolve_Delta_Record_Aggregate
;
4080 ------------------------------
4081 -- Resolve_Deep_Delta_Assoc --
4082 ------------------------------
4084 procedure Resolve_Deep_Delta_Assoc
(N
: Node_Id
; Typ
: Entity_Id
) is
4085 Choice
: constant Node_Id
:= First
(Choice_List
(N
));
4086 Enclosing_Type
: Entity_Id
:= Typ
;
4088 procedure Resolve_Choice_Prefix
4089 (Choice_Prefix
: Node_Id
; Enclosing_Type
: in out Entity_Id
);
4090 -- Recursively analyze selectors. Enclosing_Type is set to
4091 -- type of the last component.
4093 ---------------------------
4094 -- Resolve_Choice_Prefix --
4095 ---------------------------
4097 procedure Resolve_Choice_Prefix
4098 (Choice_Prefix
: Node_Id
; Enclosing_Type
: in out Entity_Id
)
4100 Selector
: Node_Id
:= Choice_Prefix
;
4102 if not Is_Root_Prefix_Of_Deep_Choice
(Choice_Prefix
) then
4103 Resolve_Choice_Prefix
(Prefix
(Choice_Prefix
), Enclosing_Type
);
4105 if Nkind
(Choice_Prefix
) = N_Selected_Component
then
4106 Selector
:= Selector_Name
(Choice_Prefix
);
4108 pragma Assert
(Nkind
(Choice_Prefix
) = N_Indexed_Component
);
4109 Selector
:= First
(Expressions
(Choice_Prefix
));
4113 if Is_Array_Type
(Enclosing_Type
) then
4114 Analyze_And_Resolve
(Selector
,
4115 Etype
(First_Index
(Enclosing_Type
)));
4116 Enclosing_Type
:= Component_Type
(Enclosing_Type
);
4119 Comp
: Entity_Id
:= First_Entity
(Enclosing_Type
);
4120 Found
: Boolean := False;
4122 while Present
(Comp
) and not Found
loop
4123 if Chars
(Comp
) = Chars
(Selector
) then
4124 if Ekind
(Comp
) = E_Discriminant
then
4125 Error_Msg_N
("delta cannot apply to discriminant",
4129 Set_Entity
(Selector
, Comp
);
4130 Set_Etype
(Selector
, Etype
(Comp
));
4131 Set_Analyzed
(Selector
);
4132 Enclosing_Type
:= Etype
(Comp
);
4139 ("type& has no component with this name",
4140 Selector
, Enclosing_Type
);
4145 -- Set the type of the prefix for GNATprove, except for the root
4146 -- prefix, whose type is already the expected one for a record
4147 -- delta aggregate, or the type of the array index for an
4148 -- array delta aggregate (the only case here really since
4149 -- Resolve_Deep_Delta_Assoc is only called for array delta
4152 if Selector
/= Choice_Prefix
then
4153 Set_Etype
(Choice_Prefix
, Enclosing_Type
);
4155 end Resolve_Choice_Prefix
;
4158 Unimplemented
: exception; -- TEMPORARY
4160 if Present
(Next
(Choice
)) then
4161 raise Unimplemented
;
4165 Resolve_Choice_Prefix
(Choice
, Enclosing_Type
);
4166 Analyze_And_Resolve
(Expression
(N
), Enclosing_Type
);
4167 end Resolve_Deep_Delta_Assoc
;
4169 ---------------------------------
4170 -- Resolve_Extension_Aggregate --
4171 ---------------------------------
4173 -- There are two cases to consider:
4175 -- a) If the ancestor part is a type mark, the components needed are the
4176 -- difference between the components of the expected type and the
4177 -- components of the given type mark.
4179 -- b) If the ancestor part is an expression, it must be unambiguous, and
4180 -- once we have its type we can also compute the needed components as in
4181 -- the previous case. In both cases, if the ancestor type is not the
4182 -- immediate ancestor, we have to build this ancestor recursively.
4184 -- In both cases, discriminants of the ancestor type do not play a role in
4185 -- the resolution of the needed components, because inherited discriminants
4186 -- cannot be used in a type extension. As a result we can compute
4187 -- independently the list of components of the ancestor type and of the
4190 procedure Resolve_Extension_Aggregate
(N
: Node_Id
; Typ
: Entity_Id
) is
4191 A
: constant Node_Id
:= Ancestor_Part
(N
);
4196 function Valid_Limited_Ancestor
(Anc
: Node_Id
) return Boolean;
4197 -- If the type is limited, verify that the ancestor part is a legal
4198 -- expression (aggregate or function call, including 'Input)) that does
4199 -- not require a copy, as specified in 7.5(2).
4201 function Valid_Ancestor_Type
return Boolean;
4202 -- Verify that the type of the ancestor part is a non-private ancestor
4203 -- of the expected type, which must be a type extension.
4205 procedure Transform_BIP_Assignment
(Typ
: Entity_Id
);
4206 -- For an extension aggregate whose ancestor part is a build-in-place
4207 -- call returning a nonlimited type, this is used to transform the
4208 -- assignment to the ancestor part to use a temp.
4210 ----------------------------
4211 -- Valid_Limited_Ancestor --
4212 ----------------------------
4214 function Valid_Limited_Ancestor
(Anc
: Node_Id
) return Boolean is
4216 if Is_Entity_Name
(Anc
) and then Is_Type
(Entity
(Anc
)) then
4219 -- The ancestor must be a call or an aggregate, but a call may
4220 -- have been expanded into a temporary, so check original node.
4222 elsif Nkind
(Anc
) in N_Aggregate
4223 | N_Extension_Aggregate
4228 elsif Nkind
(Original_Node
(Anc
)) = N_Function_Call
then
4231 elsif Nkind
(Anc
) = N_Attribute_Reference
4232 and then Attribute_Name
(Anc
) = Name_Input
4236 elsif Nkind
(Anc
) = N_Qualified_Expression
then
4237 return Valid_Limited_Ancestor
(Expression
(Anc
));
4239 elsif Nkind
(Anc
) = N_Raise_Expression
then
4245 end Valid_Limited_Ancestor
;
4247 -------------------------
4248 -- Valid_Ancestor_Type --
4249 -------------------------
4251 function Valid_Ancestor_Type
return Boolean is
4252 Imm_Type
: Entity_Id
;
4255 Imm_Type
:= Base_Type
(Typ
);
4256 while Is_Derived_Type
(Imm_Type
) loop
4257 if Etype
(Imm_Type
) = Base_Type
(A_Type
) then
4260 -- The base type of the parent type may appear as a private
4261 -- extension if it is declared as such in a parent unit of the
4262 -- current one. For consistency of the subsequent analysis use
4263 -- the partial view for the ancestor part.
4265 elsif Is_Private_Type
(Etype
(Imm_Type
))
4266 and then Present
(Full_View
(Etype
(Imm_Type
)))
4267 and then Base_Type
(A_Type
) = Full_View
(Etype
(Imm_Type
))
4269 A_Type
:= Etype
(Imm_Type
);
4272 -- The parent type may be a private extension. The aggregate is
4273 -- legal if the type of the aggregate is an extension of it that
4274 -- is not a private extension.
4276 elsif Is_Private_Type
(A_Type
)
4277 and then not Is_Private_Type
(Imm_Type
)
4278 and then Present
(Full_View
(A_Type
))
4279 and then Base_Type
(Full_View
(A_Type
)) = Etype
(Imm_Type
)
4283 -- The parent type may be a raise expression (which is legal in
4284 -- any expression context).
4286 elsif A_Type
= Raise_Type
then
4287 A_Type
:= Etype
(Imm_Type
);
4291 Imm_Type
:= Etype
(Base_Type
(Imm_Type
));
4295 -- If previous loop did not find a proper ancestor, report error
4297 Error_Msg_NE
("expect ancestor type of &", A
, Typ
);
4299 end Valid_Ancestor_Type
;
4301 ------------------------------
4302 -- Transform_BIP_Assignment --
4303 ------------------------------
4305 procedure Transform_BIP_Assignment
(Typ
: Entity_Id
) is
4306 Loc
: constant Source_Ptr
:= Sloc
(N
);
4307 Def_Id
: constant Entity_Id
:= Make_Temporary
(Loc
, 'Y', A
);
4308 Obj_Decl
: constant Node_Id
:=
4309 Make_Object_Declaration
(Loc
,
4310 Defining_Identifier
=> Def_Id
,
4311 Constant_Present
=> True,
4312 Object_Definition
=> New_Occurrence_Of
(Typ
, Loc
),
4314 Has_Init_Expression
=> True);
4316 Set_Etype
(Def_Id
, Typ
);
4317 Set_Ancestor_Part
(N
, New_Occurrence_Of
(Def_Id
, Loc
));
4318 Insert_Action
(N
, Obj_Decl
);
4319 end Transform_BIP_Assignment
;
4321 -- Start of processing for Resolve_Extension_Aggregate
4324 -- Analyze the ancestor part and account for the case where it is a
4325 -- parameterless function call.
4328 Check_Parameterless_Call
(A
);
4330 if Is_Entity_Name
(A
) and then Is_Type
(Entity
(A
)) then
4332 -- AI05-0115: If the ancestor part is a subtype mark, the ancestor
4333 -- must not have unknown discriminants. To catch cases where the
4334 -- aggregate occurs at a place where the full view of the ancestor
4335 -- type is visible and doesn't have unknown discriminants, but the
4336 -- aggregate type was derived from a partial view that has unknown
4337 -- discriminants, we check whether the aggregate type has unknown
4338 -- discriminants (unknown discriminants were inherited), along
4339 -- with checking that the partial view of the ancestor has unknown
4340 -- discriminants. (It might be sufficient to replace the entire
4341 -- condition with Has_Unknown_Discriminants (Typ), but that might
4342 -- miss some cases, not clear, and causes error changes in some tests
4343 -- such as class-wide cases, that aren't clearly improvements. ???)
4345 if Has_Unknown_Discriminants
(Entity
(A
))
4346 or else (Has_Unknown_Discriminants
(Typ
)
4347 and then Partial_View_Has_Unknown_Discr
(Entity
(A
)))
4350 ("aggregate not available for type& whose ancestor "
4351 & "has unknown discriminants", N
, Typ
);
4355 if not Is_Tagged_Type
(Typ
) then
4356 Error_Msg_N
("type of extension aggregate must be tagged", N
);
4359 elsif Is_Limited_Type
(Typ
) then
4361 -- Ada 2005 (AI-287): Limited aggregates are allowed
4363 if Ada_Version
< Ada_2005
then
4364 Error_Msg_N
("aggregate type cannot be limited", N
);
4365 Explain_Limited_Type
(Typ
, N
);
4368 elsif Valid_Limited_Ancestor
(A
) then
4373 ("limited ancestor part must be aggregate or function call", A
);
4376 elsif Is_Class_Wide_Type
(Typ
) then
4377 Error_Msg_N
("aggregate cannot be of a class-wide type", N
);
4381 if Is_Entity_Name
(A
) and then Is_Type
(Entity
(A
)) then
4382 A_Type
:= Get_Full_View
(Entity
(A
));
4384 if Valid_Ancestor_Type
then
4385 Set_Entity
(A
, A_Type
);
4386 Set_Etype
(A
, A_Type
);
4388 Validate_Ancestor_Part
(N
);
4389 Resolve_Record_Aggregate
(N
, Typ
);
4392 elsif Nkind
(A
) /= N_Aggregate
then
4393 if Is_Overloaded
(A
) then
4396 Get_First_Interp
(A
, I
, It
);
4397 while Present
(It
.Typ
) loop
4399 -- Consider limited interpretations if Ada 2005 or higher
4401 if Is_Tagged_Type
(It
.Typ
)
4402 and then (Ada_Version
>= Ada_2005
4403 or else not Is_Limited_Type
(It
.Typ
))
4405 if A_Type
/= Any_Type
then
4406 Error_Msg_N
("cannot resolve expression", A
);
4413 Get_Next_Interp
(I
, It
);
4416 if A_Type
= Any_Type
then
4417 if Ada_Version
>= Ada_2005
then
4419 ("ancestor part must be of a tagged type", A
);
4422 ("ancestor part must be of a nonlimited tagged type", A
);
4429 A_Type
:= Etype
(A
);
4432 if Valid_Ancestor_Type
then
4433 Resolve
(A
, A_Type
);
4434 Check_Unset_Reference
(A
);
4435 Check_Non_Static_Context
(A
);
4437 -- The aggregate is illegal if the ancestor expression is a call
4438 -- to a function with a limited unconstrained result, unless the
4439 -- type of the aggregate is a null extension. This restriction
4440 -- was added in AI05-67 to simplify implementation.
4442 if Nkind
(A
) = N_Function_Call
4443 and then Is_Limited_Type
(A_Type
)
4444 and then not Is_Null_Extension
(Typ
)
4445 and then not Is_Constrained
(A_Type
)
4448 ("type of limited ancestor part must be constrained", A
);
4450 -- Reject the use of CPP constructors that leave objects partially
4451 -- initialized. For example:
4453 -- type CPP_Root is tagged limited record ...
4454 -- pragma Import (CPP, CPP_Root);
4456 -- type CPP_DT is new CPP_Root and Iface ...
4457 -- pragma Import (CPP, CPP_DT);
4459 -- type Ada_DT is new CPP_DT with ...
4461 -- Obj : Ada_DT := Ada_DT'(New_CPP_Root with others => <>);
4463 -- Using the constructor of CPP_Root the slots of the dispatch
4464 -- table of CPP_DT cannot be set, and the secondary tag of
4465 -- CPP_DT is unknown.
4467 elsif Nkind
(A
) = N_Function_Call
4468 and then Is_CPP_Constructor_Call
(A
)
4469 and then Enclosing_CPP_Parent
(Typ
) /= A_Type
4472 ("??must use 'C'P'P constructor for type &", A
,
4473 Enclosing_CPP_Parent
(Typ
));
4475 -- The following call is not needed if the previous warning
4476 -- is promoted to an error.
4478 Resolve_Record_Aggregate
(N
, Typ
);
4480 elsif Is_Class_Wide_Type
(Etype
(A
))
4481 and then Nkind
(Original_Node
(A
)) = N_Function_Call
4483 -- If the ancestor part is a dispatching call, it appears
4484 -- statically to be a legal ancestor, but it yields any member
4485 -- of the class, and it is not possible to determine whether
4486 -- it is an ancestor of the extension aggregate (much less
4487 -- which ancestor). It is not possible to determine the
4488 -- components of the extension part.
4490 -- This check implements AI-306, which in fact was motivated by
4491 -- an AdaCore query to the ARG after this test was added.
4493 Error_Msg_N
("ancestor part must be statically tagged", A
);
4495 -- We are using the build-in-place protocol, but we can't build
4496 -- in place, because we need to call the function before
4497 -- allocating the aggregate. Could do better for null
4498 -- extensions, and maybe for nondiscriminated types.
4499 -- This is wrong for limited, but those were wrong already.
4501 if not Is_Inherently_Limited_Type
(A_Type
)
4502 and then Is_Build_In_Place_Function_Call
(A
)
4504 Transform_BIP_Assignment
(A_Type
);
4507 Resolve_Record_Aggregate
(N
, Typ
);
4512 Error_Msg_N
("no unique type for this aggregate", A
);
4515 Check_Function_Writable_Actuals
(N
);
4516 end Resolve_Extension_Aggregate
;
4518 ----------------------------------
4519 -- Resolve_Null_Array_Aggregate --
4520 ----------------------------------
4522 function Resolve_Null_Array_Aggregate
(N
: Node_Id
) return Boolean is
4523 -- Never returns False, but declared as a function to match
4524 -- other Resolve_Mumble functions.
4526 Loc
: constant Source_Ptr
:= Sloc
(N
);
4527 Typ
: constant Entity_Id
:= Etype
(N
);
4531 Constr
: constant List_Id
:= New_List
;
4534 -- Attach the list of constraints at the location of the aggregate, so
4535 -- the individual constraints can be analyzed.
4537 Set_Parent
(Constr
, N
);
4539 -- Create a constrained subtype with null dimensions
4541 Index
:= First_Index
(Typ
);
4542 while Present
(Index
) loop
4543 Get_Index_Bounds
(Index
, L
=> Lo
, H
=> Hi
);
4545 -- The upper bound is the predecessor of the lower bound
4547 Hi
:= Make_Attribute_Reference
4549 Prefix
=> New_Occurrence_Of
(Etype
(Index
), Loc
),
4550 Attribute_Name
=> Name_Pred
,
4551 Expressions
=> New_List
(New_Copy_Tree
(Lo
)));
4553 Append
(Make_Range
(Loc
, New_Copy_Tree
(Lo
), Hi
), Constr
);
4554 Analyze_And_Resolve
(Last
(Constr
), Etype
(Index
));
4559 Set_Compile_Time_Known_Aggregate
(N
);
4560 Set_Aggregate_Bounds
(N
, First
(Constr
));
4563 end Resolve_Null_Array_Aggregate
;
4565 ------------------------------
4566 -- Resolve_Record_Aggregate --
4567 ------------------------------
4569 procedure Resolve_Record_Aggregate
(N
: Node_Id
; Typ
: Entity_Id
) is
4570 New_Assoc_List
: constant List_Id
:= New_List
;
4571 -- New_Assoc_List is the newly built list of N_Component_Association
4574 Others_Etype
: Entity_Id
:= Empty
;
4575 -- This variable is used to save the Etype of the last record component
4576 -- that takes its value from the others choice. Its purpose is:
4578 -- (a) make sure the others choice is useful
4580 -- (b) make sure the type of all the components whose value is
4581 -- subsumed by the others choice are the same.
4583 -- This variable is updated as a side effect of function Get_Value.
4585 Box_Node
: Node_Id
:= Empty
;
4586 Is_Box_Present
: Boolean := False;
4587 Is_Box_Init_By_Default
: Boolean := False;
4588 Others_Box
: Natural := 0;
4589 -- Ada 2005 (AI-287): Variables used in case of default initialization
4590 -- to provide a functionality similar to Others_Etype. Box_Present
4591 -- indicates that the component takes its default initialization;
4592 -- Others_Box counts the number of components of the current aggregate
4593 -- (which may be a sub-aggregate of a larger one) that are default-
4594 -- initialized. A value of One indicates that an others_box is present.
4595 -- Any larger value indicates that the others_box is not redundant.
4596 -- These variables, similar to Others_Etype, are also updated as a side
4597 -- effect of function Get_Value. Box_Node is used to place a warning on
4598 -- a redundant others_box.
4600 procedure Add_Association
4601 (Component
: Entity_Id
;
4603 Assoc_List
: List_Id
;
4604 Is_Box_Present
: Boolean := False);
4605 -- Builds a new N_Component_Association node which associates Component
4606 -- to expression Expr and adds it to the association list being built,
4607 -- either New_Assoc_List, or the association being built for an inner
4610 procedure Add_Discriminant_Values
4611 (New_Aggr
: Node_Id
;
4612 Assoc_List
: List_Id
);
4613 -- The constraint to a component may be given by a discriminant of the
4614 -- enclosing type, in which case we have to retrieve its value, which is
4615 -- part of the enclosing aggregate. Assoc_List provides the discriminant
4616 -- associations of the current type or of some enclosing record.
4618 function Discriminant_Present
(Input_Discr
: Entity_Id
) return Boolean;
4619 -- If aggregate N is a regular aggregate this routine will return True.
4620 -- Otherwise, if N is an extension aggregate, then Input_Discr denotes
4621 -- a discriminant whose value may already have been specified by N's
4622 -- ancestor part. This routine checks whether this is indeed the case
4623 -- and if so returns False, signaling that no value for Input_Discr
4624 -- should appear in N's aggregate part. Also, in this case, the routine
4625 -- appends to New_Assoc_List the discriminant value specified in the
4628 -- If the aggregate is in a context with expansion delayed, it will be
4629 -- reanalyzed. The inherited discriminant values must not be reinserted
4630 -- in the component list to prevent spurious errors, but they must be
4631 -- present on first analysis to build the proper subtype indications.
4632 -- The flag Inherited_Discriminant is used to prevent the re-insertion.
4634 function Find_Private_Ancestor
(Typ
: Entity_Id
) return Entity_Id
;
4635 -- AI05-0115: Find earlier ancestor in the derivation chain that is
4636 -- derived from private view Typ. Whether the aggregate is legal depends
4637 -- on the current visibility of the type as well as that of the parent
4641 (Compon
: Entity_Id
;
4643 Consider_Others_Choice
: Boolean := False) return Node_Id
;
4644 -- Given a record component stored in parameter Compon, this function
4645 -- returns its value as it appears in the list From, which is a list
4646 -- of N_Component_Association nodes.
4648 -- If no component association has a choice for the searched component,
4649 -- the value provided by the others choice is returned, if there is one,
4650 -- and Consider_Others_Choice is set to true. Otherwise Empty is
4651 -- returned. If there is more than one component association giving a
4652 -- value for the searched record component, an error message is emitted
4653 -- and the first found value is returned.
4655 -- If Consider_Others_Choice is set and the returned expression comes
4656 -- from the others choice, then Others_Etype is set as a side effect.
4657 -- An error message is emitted if the components taking their value from
4658 -- the others choice do not have same type.
4660 procedure Propagate_Discriminants
4662 Assoc_List
: List_Id
);
4663 -- Nested components may themselves be discriminated types constrained
4664 -- by outer discriminants, whose values must be captured before the
4665 -- aggregate is expanded into assignments.
4667 procedure Resolve_Aggr_Expr
(Expr
: Node_Id
; Component
: Entity_Id
);
4668 -- Analyzes and resolves expression Expr against the Etype of the
4669 -- Component. This routine also applies all appropriate checks to Expr.
4670 -- It finally saves a Expr in the newly created association list that
4671 -- will be attached to the final record aggregate. Note that if the
4672 -- Parent pointer of Expr is not set then Expr was produced with a
4673 -- New_Copy_Tree or some such.
4675 procedure Rewrite_Range
(Root_Type
: Entity_Id
; Rge
: Node_Id
);
4676 -- Rewrite a range node Rge when its bounds refer to non-stored
4677 -- discriminants from Root_Type, to replace them with the stored
4678 -- discriminant values. This is required in GNATprove mode, and is
4679 -- adopted in all modes to avoid special-casing GNATprove mode.
4681 ---------------------
4682 -- Add_Association --
4683 ---------------------
4685 procedure Add_Association
4686 (Component
: Entity_Id
;
4688 Assoc_List
: List_Id
;
4689 Is_Box_Present
: Boolean := False)
4691 Choice_List
: constant List_Id
:= New_List
;
4695 -- If this is a box association the expression is missing, so use the
4696 -- Sloc of the aggregate itself for the new association.
4698 pragma Assert
(Present
(Expr
) xor Is_Box_Present
);
4700 if Present
(Expr
) then
4706 Append_To
(Choice_List
, New_Occurrence_Of
(Component
, Loc
));
4708 Append_To
(Assoc_List
,
4709 Make_Component_Association
(Loc
,
4710 Choices
=> Choice_List
,
4712 Box_Present
=> Is_Box_Present
));
4714 -- If this association has a box for a component that is initialized
4715 -- by default, then set flag on the new association to indicate that
4716 -- the original association was for such a box-initialized component.
4718 if Is_Box_Init_By_Default
then
4719 Set_Was_Default_Init_Box_Association
(Last
(Assoc_List
));
4721 end Add_Association
;
4723 -----------------------------
4724 -- Add_Discriminant_Values --
4725 -----------------------------
4727 procedure Add_Discriminant_Values
4728 (New_Aggr
: Node_Id
;
4729 Assoc_List
: List_Id
)
4733 Discr_Elmt
: Elmt_Id
;
4734 Discr_Val
: Node_Id
;
4738 Discr
:= First_Discriminant
(Etype
(New_Aggr
));
4739 Discr_Elmt
:= First_Elmt
(Discriminant_Constraint
(Etype
(New_Aggr
)));
4740 while Present
(Discr_Elmt
) loop
4741 Discr_Val
:= Node
(Discr_Elmt
);
4743 -- If the constraint is given by a discriminant then it is a
4744 -- discriminant of an enclosing record, and its value has already
4745 -- been placed in the association list.
4747 if Is_Entity_Name
(Discr_Val
)
4748 and then Ekind
(Entity
(Discr_Val
)) = E_Discriminant
4750 Val
:= Entity
(Discr_Val
);
4752 Assoc
:= First
(Assoc_List
);
4753 while Present
(Assoc
) loop
4754 if Present
(Entity
(First
(Choices
(Assoc
))))
4755 and then Entity
(First
(Choices
(Assoc
))) = Val
4757 Discr_Val
:= Expression
(Assoc
);
4766 (Discr
, New_Copy_Tree
(Discr_Val
),
4767 Component_Associations
(New_Aggr
));
4769 -- If the discriminant constraint is a current instance, mark the
4770 -- current aggregate so that the self-reference can be expanded by
4771 -- Build_Record_Aggr_Code.Replace_Type later.
4773 if Nkind
(Discr_Val
) = N_Attribute_Reference
4774 and then Is_Entity_Name
(Prefix
(Discr_Val
))
4775 and then Is_Type
(Entity
(Prefix
(Discr_Val
)))
4778 (Entity
(Prefix
(Discr_Val
)),
4780 Use_Full_View
=> True)
4782 Set_Has_Self_Reference
(N
);
4785 Next_Elmt
(Discr_Elmt
);
4786 Next_Discriminant
(Discr
);
4788 end Add_Discriminant_Values
;
4790 --------------------------
4791 -- Discriminant_Present --
4792 --------------------------
4794 function Discriminant_Present
(Input_Discr
: Entity_Id
) return Boolean is
4795 Regular_Aggr
: constant Boolean := Nkind
(N
) /= N_Extension_Aggregate
;
4797 Ancestor_Is_Subtyp
: Boolean;
4802 Ancestor_Typ
: Entity_Id
;
4803 Comp_Assoc
: Node_Id
;
4805 Discr_Expr
: Node_Id
;
4806 Discr_Val
: Elmt_Id
:= No_Elmt
;
4807 Orig_Discr
: Entity_Id
;
4810 if Regular_Aggr
then
4814 -- Check whether inherited discriminant values have already been
4815 -- inserted in the aggregate. This will be the case if we are
4816 -- re-analyzing an aggregate whose expansion was delayed.
4818 if Present
(Component_Associations
(N
)) then
4819 Comp_Assoc
:= First
(Component_Associations
(N
));
4820 while Present
(Comp_Assoc
) loop
4821 if Inherited_Discriminant
(Comp_Assoc
) then
4829 Ancestor
:= Ancestor_Part
(N
);
4830 Ancestor_Typ
:= Etype
(Ancestor
);
4831 Loc
:= Sloc
(Ancestor
);
4833 -- For a private type with unknown discriminants, use the underlying
4834 -- record view if it is available.
4836 if Has_Unknown_Discriminants
(Ancestor_Typ
)
4837 and then Present
(Full_View
(Ancestor_Typ
))
4838 and then Present
(Underlying_Record_View
(Full_View
(Ancestor_Typ
)))
4840 Ancestor_Typ
:= Underlying_Record_View
(Full_View
(Ancestor_Typ
));
4843 Ancestor_Is_Subtyp
:=
4844 Is_Entity_Name
(Ancestor
) and then Is_Type
(Entity
(Ancestor
));
4846 -- If the ancestor part has no discriminants clearly N's aggregate
4847 -- part must provide a value for Discr.
4849 if not Has_Discriminants
(Ancestor_Typ
) then
4852 -- If the ancestor part is an unconstrained subtype mark then the
4853 -- Discr must be present in N's aggregate part.
4855 elsif Ancestor_Is_Subtyp
4856 and then not Is_Constrained
(Entity
(Ancestor
))
4861 -- Now look to see if Discr was specified in the ancestor part
4863 if Ancestor_Is_Subtyp
then
4865 First_Elmt
(Discriminant_Constraint
(Entity
(Ancestor
)));
4868 Orig_Discr
:= Original_Record_Component
(Input_Discr
);
4870 Discr
:= First_Discriminant
(Ancestor_Typ
);
4871 while Present
(Discr
) loop
4873 -- If Ancestor has already specified Disc value then insert its
4874 -- value in the final aggregate.
4876 if Original_Record_Component
(Discr
) = Orig_Discr
then
4877 if Ancestor_Is_Subtyp
then
4878 Discr_Expr
:= New_Copy_Tree
(Node
(Discr_Val
));
4881 Make_Selected_Component
(Loc
,
4882 Prefix
=> Duplicate_Subexpr
(Ancestor
),
4883 Selector_Name
=> New_Occurrence_Of
(Input_Discr
, Loc
));
4886 Resolve_Aggr_Expr
(Discr_Expr
, Input_Discr
);
4887 Set_Inherited_Discriminant
(Last
(New_Assoc_List
));
4891 Next_Discriminant
(Discr
);
4893 if Ancestor_Is_Subtyp
then
4894 Next_Elmt
(Discr_Val
);
4899 end Discriminant_Present
;
4901 ---------------------------
4902 -- Find_Private_Ancestor --
4903 ---------------------------
4905 function Find_Private_Ancestor
(Typ
: Entity_Id
) return Entity_Id
is
4911 if Has_Private_Ancestor
(Par
)
4912 and then not Has_Private_Ancestor
(Etype
(Base_Type
(Par
)))
4916 elsif not Is_Derived_Type
(Par
) then
4920 Par
:= Etype
(Base_Type
(Par
));
4923 end Find_Private_Ancestor
;
4930 (Compon
: Entity_Id
;
4932 Consider_Others_Choice
: Boolean := False) return Node_Id
4934 Typ
: constant Entity_Id
:= Etype
(Compon
);
4936 Expr
: Node_Id
:= Empty
;
4937 Selector_Name
: Node_Id
;
4940 Is_Box_Present
:= False;
4941 Is_Box_Init_By_Default
:= False;
4947 Assoc
:= First
(From
);
4948 while Present
(Assoc
) loop
4949 Selector_Name
:= First
(Choices
(Assoc
));
4950 while Present
(Selector_Name
) loop
4951 if Nkind
(Selector_Name
) = N_Others_Choice
then
4952 if Consider_Others_Choice
and then No
(Expr
) then
4954 -- We need to duplicate the expression for each
4955 -- successive component covered by the others choice.
4956 -- This is redundant if the others_choice covers only
4957 -- one component (small optimization possible???), but
4958 -- indispensable otherwise, because each one must be
4959 -- expanded individually to preserve side effects.
4961 -- Ada 2005 (AI-287): In case of default initialization
4962 -- of components, we duplicate the corresponding default
4963 -- expression (from the record type declaration). The
4964 -- copy must carry the sloc of the association (not the
4965 -- original expression) to prevent spurious elaboration
4966 -- checks when the default includes function calls.
4968 if Box_Present
(Assoc
) then
4969 Others_Box
:= Others_Box
+ 1;
4970 Is_Box_Present
:= True;
4972 if Expander_Active
then
4974 New_Copy_Tree_And_Copy_Dimensions
4975 (Expression
(Parent
(Compon
)),
4976 New_Sloc
=> Sloc
(Assoc
));
4978 return Expression
(Parent
(Compon
));
4982 if Present
(Others_Etype
)
4983 and then Base_Type
(Others_Etype
) /= Base_Type
(Typ
)
4985 -- If the components are of an anonymous access
4986 -- type they are distinct, but this is legal in
4987 -- Ada 2012 as long as designated types match.
4989 if (Ekind
(Typ
) = E_Anonymous_Access_Type
4990 or else Ekind
(Typ
) =
4991 E_Anonymous_Access_Subprogram_Type
)
4992 and then Designated_Type
(Typ
) =
4993 Designated_Type
(Others_Etype
)
4998 ("components in OTHERS choice must have same "
4999 & "type", Selector_Name
);
5003 Others_Etype
:= Typ
;
5005 -- Copy the expression so that it is resolved
5006 -- independently for each component, This is needed
5007 -- for accessibility checks on components of anonymous
5008 -- access types, even in compile_only mode.
5010 if not Inside_A_Generic
then
5012 New_Copy_Tree_And_Copy_Dimensions
5013 (Expression
(Assoc
));
5015 return Expression
(Assoc
);
5020 elsif Chars
(Compon
) = Chars
(Selector_Name
) then
5023 -- Ada 2005 (AI-231)
5025 if Ada_Version
>= Ada_2005
5026 and then Known_Null
(Expression
(Assoc
))
5028 Check_Can_Never_Be_Null
(Compon
, Expression
(Assoc
));
5031 -- We need to duplicate the expression when several
5032 -- components are grouped together with a "|" choice.
5033 -- For instance "filed1 | filed2 => Expr"
5035 -- Ada 2005 (AI-287)
5037 if Box_Present
(Assoc
) then
5038 Is_Box_Present
:= True;
5040 -- Duplicate the default expression of the component
5041 -- from the record type declaration, so a new copy
5042 -- can be attached to the association.
5044 -- Note that we always copy the default expression,
5045 -- even when the association has a single choice, in
5046 -- order to create a proper association for the
5047 -- expanded aggregate.
5049 -- Component may have no default, in which case the
5050 -- expression is empty and the component is default-
5051 -- initialized, but an association for the component
5052 -- exists, and it is not covered by an others clause.
5054 -- Scalar and private types have no initialization
5055 -- procedure, so they remain uninitialized. If the
5056 -- target of the aggregate is a constant this
5057 -- deserves a warning.
5059 if No
(Expression
(Parent
(Compon
)))
5060 and then not Has_Non_Null_Base_Init_Proc
(Typ
)
5061 and then not Has_Aspect
(Typ
, Aspect_Default_Value
)
5062 and then not Is_Concurrent_Type
(Typ
)
5063 and then Nkind
(Parent
(N
)) = N_Object_Declaration
5064 and then Constant_Present
(Parent
(N
))
5066 Error_Msg_Node_2
:= Typ
;
5068 ("??component& of type& is uninitialized",
5069 Assoc
, Selector_Name
);
5071 -- An additional reminder if the component type
5072 -- is a generic formal.
5074 if Is_Generic_Type
(Base_Type
(Typ
)) then
5076 ("\instance should provide actual type with "
5077 & "initialization for&", Assoc
, Typ
);
5082 New_Copy_Tree_And_Copy_Dimensions
5083 (Expression
(Parent
(Compon
)));
5086 if Present
(Next
(Selector_Name
)) then
5087 Expr
:= New_Copy_Tree_And_Copy_Dimensions
5088 (Expression
(Assoc
));
5090 Expr
:= Expression
(Assoc
);
5094 Generate_Reference
(Compon
, Selector_Name
, 'm');
5098 ("more than one value supplied for &",
5099 Selector_Name
, Compon
);
5104 Next
(Selector_Name
);
5113 -----------------------------
5114 -- Propagate_Discriminants --
5115 -----------------------------
5117 procedure Propagate_Discriminants
5119 Assoc_List
: List_Id
)
5121 Loc
: constant Source_Ptr
:= Sloc
(N
);
5123 procedure Process_Component
(Comp
: Entity_Id
);
5124 -- Add one component with a box association to the inner aggregate,
5125 -- and recurse if component is itself composite.
5127 -----------------------
5128 -- Process_Component --
5129 -----------------------
5131 procedure Process_Component
(Comp
: Entity_Id
) is
5132 T
: constant Entity_Id
:= Etype
(Comp
);
5136 if Is_Record_Type
(T
) and then Has_Discriminants
(T
) then
5137 New_Aggr
:= Make_Aggregate
(Loc
, No_List
, New_List
);
5138 Set_Etype
(New_Aggr
, T
);
5141 (Comp
, New_Aggr
, Component_Associations
(Aggr
));
5143 -- Collect discriminant values and recurse
5145 Add_Discriminant_Values
(New_Aggr
, Assoc_List
);
5146 Propagate_Discriminants
(New_Aggr
, Assoc_List
);
5148 Build_Constrained_Itype
5149 (New_Aggr
, T
, Component_Associations
(New_Aggr
));
5152 (Comp
, Empty
, Component_Associations
(Aggr
),
5153 Is_Box_Present
=> True);
5155 end Process_Component
;
5159 Aggr_Type
: constant Entity_Id
:= Base_Type
(Etype
(Aggr
));
5160 Components
: constant Elist_Id
:= New_Elmt_List
;
5161 Def_Node
: constant Node_Id
:=
5162 Type_Definition
(Declaration_Node
(Aggr_Type
));
5165 Comp_Elmt
: Elmt_Id
;
5168 -- Start of processing for Propagate_Discriminants
5171 -- The component type may be a variant type. Collect the components
5172 -- that are ruled by the known values of the discriminants. Their
5173 -- values have already been inserted into the component list of the
5174 -- current aggregate.
5176 if Nkind
(Def_Node
) = N_Record_Definition
5177 and then Present
(Component_List
(Def_Node
))
5178 and then Present
(Variant_Part
(Component_List
(Def_Node
)))
5180 Gather_Components
(Aggr_Type
,
5181 Component_List
(Def_Node
),
5182 Governed_By
=> Component_Associations
(Aggr
),
5184 Report_Errors
=> Errors
);
5186 Comp_Elmt
:= First_Elmt
(Components
);
5187 while Present
(Comp_Elmt
) loop
5188 if Ekind
(Node
(Comp_Elmt
)) /= E_Discriminant
then
5189 Process_Component
(Node
(Comp_Elmt
));
5192 Next_Elmt
(Comp_Elmt
);
5195 -- No variant part, iterate over all components
5198 Comp
:= First_Component
(Etype
(Aggr
));
5199 while Present
(Comp
) loop
5200 Process_Component
(Comp
);
5201 Next_Component
(Comp
);
5204 end Propagate_Discriminants
;
5206 -----------------------
5207 -- Resolve_Aggr_Expr --
5208 -----------------------
5210 procedure Resolve_Aggr_Expr
(Expr
: Node_Id
; Component
: Entity_Id
) is
5211 function Has_Expansion_Delayed
(Expr
: Node_Id
) return Boolean;
5212 -- If the expression is an aggregate (possibly qualified) then its
5213 -- expansion is delayed until the enclosing aggregate is expanded
5214 -- into assignments. In that case, do not generate checks on the
5215 -- expression, because they will be generated later, and will other-
5216 -- wise force a copy (to remove side effects) that would leave a
5217 -- dynamic-sized aggregate in the code, something that gigi cannot
5220 ---------------------------
5221 -- Has_Expansion_Delayed --
5222 ---------------------------
5224 function Has_Expansion_Delayed
(Expr
: Node_Id
) return Boolean is
5227 (Nkind
(Expr
) in N_Aggregate | N_Extension_Aggregate
5228 and then Present
(Etype
(Expr
))
5229 and then Is_Record_Type
(Etype
(Expr
))
5230 and then Expansion_Delayed
(Expr
))
5232 (Nkind
(Expr
) = N_Qualified_Expression
5233 and then Has_Expansion_Delayed
(Expression
(Expr
)));
5234 end Has_Expansion_Delayed
;
5238 Expr_Type
: Entity_Id
:= Empty
;
5239 New_C
: Entity_Id
:= Component
;
5243 -- Set to True if the resolved Expr node needs to be relocated when
5244 -- attached to the newly created association list. This node need not
5245 -- be relocated if its parent pointer is not set. In fact in this
5246 -- case Expr is the output of a New_Copy_Tree call. If Relocate is
5247 -- True then we have analyzed the expression node in the original
5248 -- aggregate and hence it needs to be relocated when moved over to
5249 -- the new association list.
5251 -- Start of processing for Resolve_Aggr_Expr
5254 -- If the type of the component is elementary or the type of the
5255 -- aggregate does not contain discriminants, use the type of the
5256 -- component to resolve Expr.
5258 if Is_Elementary_Type
(Etype
(Component
))
5259 or else not Has_Discriminants
(Etype
(N
))
5261 Expr_Type
:= Etype
(Component
);
5263 -- Otherwise we have to pick up the new type of the component from
5264 -- the new constrained subtype of the aggregate. In fact components
5265 -- which are of a composite type might be constrained by a
5266 -- discriminant, and we want to resolve Expr against the subtype were
5267 -- all discriminant occurrences are replaced with their actual value.
5270 New_C
:= First_Component
(Etype
(N
));
5271 while Present
(New_C
) loop
5272 if Chars
(New_C
) = Chars
(Component
) then
5273 Expr_Type
:= Etype
(New_C
);
5277 Next_Component
(New_C
);
5280 pragma Assert
(Present
(Expr_Type
));
5282 -- For each range in an array type where a discriminant has been
5283 -- replaced with the constraint, check that this range is within
5284 -- the range of the base type. This checks is done in the init
5285 -- proc for regular objects, but has to be done here for
5286 -- aggregates since no init proc is called for them.
5288 if Is_Array_Type
(Expr_Type
) then
5291 -- Range of the current constrained index in the array
5293 Orig_Index
: Node_Id
:= First_Index
(Etype
(Component
));
5294 -- Range corresponding to the range Index above in the
5295 -- original unconstrained record type. The bounds of this
5296 -- range may be governed by discriminants.
5298 Unconstr_Index
: Node_Id
:= First_Index
(Etype
(Expr_Type
));
5299 -- Range corresponding to the range Index above for the
5300 -- unconstrained array type. This range is needed to apply
5304 Index
:= First_Index
(Expr_Type
);
5305 while Present
(Index
) loop
5306 if Depends_On_Discriminant
(Orig_Index
) then
5307 Apply_Range_Check
(Index
, Etype
(Unconstr_Index
));
5311 Next_Index
(Orig_Index
);
5312 Next_Index
(Unconstr_Index
);
5318 -- If the Parent pointer of Expr is not set, Expr is an expression
5319 -- duplicated by New_Tree_Copy (this happens for record aggregates
5320 -- that look like (Field1 | Filed2 => Expr) or (others => Expr)).
5321 -- Such a duplicated expression must be attached to the tree
5322 -- before analysis and resolution to enforce the rule that a tree
5323 -- fragment should never be analyzed or resolved unless it is
5324 -- attached to the current compilation unit.
5326 if No
(Parent
(Expr
)) then
5327 Set_Parent
(Expr
, N
);
5333 Analyze_And_Resolve
(Expr
, Expr_Type
);
5334 Check_Expr_OK_In_Limited_Aggregate
(Expr
);
5335 Check_Non_Static_Context
(Expr
);
5336 Check_Unset_Reference
(Expr
);
5338 -- Check wrong use of class-wide types
5340 if Is_Class_Wide_Type
(Etype
(Expr
)) then
5341 Error_Msg_N
("dynamically tagged expression not allowed", Expr
);
5344 if not Has_Expansion_Delayed
(Expr
) then
5345 Aggregate_Constraint_Checks
(Expr
, Expr_Type
);
5348 -- If an aggregate component has a type with predicates, an explicit
5349 -- predicate check must be applied, as for an assignment statement,
5350 -- because the aggregate might not be expanded into individual
5351 -- component assignments.
5353 if Has_Predicates
(Expr_Type
)
5354 and then Analyzed
(Expr
)
5356 Apply_Predicate_Check
(Expr
, Expr_Type
);
5359 if Raises_Constraint_Error
(Expr
) then
5360 Set_Raises_Constraint_Error
(N
);
5363 -- If the expression has been marked as requiring a range check, then
5364 -- generate it here. It's a bit odd to be generating such checks in
5365 -- the analyzer, but harmless since Generate_Range_Check does nothing
5366 -- (other than making sure Do_Range_Check is set) if the expander is
5369 if Do_Range_Check
(Expr
) then
5370 Generate_Range_Check
(Expr
, Expr_Type
, CE_Range_Check_Failed
);
5373 -- Add association Component => Expr if the caller requests it
5376 New_Expr
:= Relocate_Node
(Expr
);
5378 -- Since New_Expr is not gonna be analyzed later on, we need to
5379 -- propagate here the dimensions form Expr to New_Expr.
5381 Copy_Dimensions
(Expr
, New_Expr
);
5387 Add_Association
(New_C
, New_Expr
, New_Assoc_List
);
5388 end Resolve_Aggr_Expr
;
5394 procedure Rewrite_Range
(Root_Type
: Entity_Id
; Rge
: Node_Id
) is
5395 procedure Rewrite_Bound
5398 Expr_Disc
: Node_Id
);
5399 -- Rewrite a bound of the range Bound, when it is equal to the
5400 -- non-stored discriminant Disc, into the stored discriminant
5407 procedure Rewrite_Bound
5410 Expr_Disc
: Node_Id
)
5413 if Nkind
(Bound
) /= N_Identifier
then
5417 -- We expect either the discriminant or the discriminal
5419 if Entity
(Bound
) = Disc
5420 or else (Ekind
(Entity
(Bound
)) = E_In_Parameter
5421 and then Discriminal_Link
(Entity
(Bound
)) = Disc
)
5423 Rewrite
(Bound
, New_Copy_Tree
(Expr_Disc
));
5429 Low
, High
: Node_Id
;
5431 Expr_Disc
: Elmt_Id
;
5433 -- Start of processing for Rewrite_Range
5436 if Has_Discriminants
(Root_Type
) and then Nkind
(Rge
) = N_Range
then
5437 Low
:= Low_Bound
(Rge
);
5438 High
:= High_Bound
(Rge
);
5440 Disc
:= First_Discriminant
(Root_Type
);
5441 Expr_Disc
:= First_Elmt
(Stored_Constraint
(Etype
(N
)));
5442 while Present
(Disc
) loop
5443 Rewrite_Bound
(Low
, Disc
, Node
(Expr_Disc
));
5444 Rewrite_Bound
(High
, Disc
, Node
(Expr_Disc
));
5445 Next_Discriminant
(Disc
);
5446 Next_Elmt
(Expr_Disc
);
5453 Components
: constant Elist_Id
:= New_Elmt_List
;
5454 -- Components is the list of the record components whose value must be
5455 -- provided in the aggregate. This list does include discriminants.
5457 Component
: Entity_Id
;
5458 Component_Elmt
: Elmt_Id
;
5460 Positional_Expr
: Node_Id
;
5462 -- Start of processing for Resolve_Record_Aggregate
5465 -- A record aggregate is restricted in SPARK:
5467 -- Each named association can have only a single choice.
5468 -- OTHERS cannot be used.
5469 -- Positional and named associations cannot be mixed.
5471 if Present
(Component_Associations
(N
)) then
5476 Assoc
:= First
(Component_Associations
(N
));
5477 while Present
(Assoc
) loop
5478 if Nkind
(Assoc
) = N_Iterated_Component_Association
then
5480 ("iterated component association can only appear in an "
5481 & "array aggregate", N
);
5482 raise Unrecoverable_Error
;
5490 -- We may end up calling Duplicate_Subexpr on expressions that are
5491 -- attached to New_Assoc_List. For this reason we need to attach it
5492 -- to the tree by setting its parent pointer to N. This parent point
5493 -- will change in STEP 8 below.
5495 Set_Parent
(New_Assoc_List
, N
);
5497 -- STEP 1: abstract type and null record verification
5499 if Is_Abstract_Type
(Typ
) then
5500 Error_Msg_N
("type of aggregate cannot be abstract", N
);
5503 if No
(First_Entity
(Typ
)) and then Null_Record_Present
(N
) then
5507 elsif Present
(First_Entity
(Typ
))
5508 and then Null_Record_Present
(N
)
5509 and then not Is_Tagged_Type
(Typ
)
5511 Error_Msg_N
("record aggregate cannot be null", N
);
5514 -- If the type has no components, then the aggregate should either
5515 -- have "null record", or in Ada 2005 it could instead have a single
5516 -- component association given by "others => <>". For Ada 95 we flag an
5517 -- error at this point, but for Ada 2005 we proceed with checking the
5518 -- associations below, which will catch the case where it's not an
5519 -- aggregate with "others => <>". Note that the legality of a <>
5520 -- aggregate for a null record type was established by AI05-016.
5522 elsif No
(First_Entity
(Typ
))
5523 and then Ada_Version
< Ada_2005
5525 Error_Msg_N
("record aggregate must be null", N
);
5529 -- A record aggregate can only use parentheses
5531 if Nkind
(N
) = N_Aggregate
5532 and then Is_Homogeneous_Aggregate
(N
)
5534 Error_Msg_N
("record aggregate must use (), not '[']", N
);
5538 -- STEP 2: Verify aggregate structure
5542 Bad_Aggregate
: Boolean := False;
5543 Selector_Name
: Node_Id
;
5546 if Present
(Component_Associations
(N
)) then
5547 Assoc
:= First
(Component_Associations
(N
));
5552 while Present
(Assoc
) loop
5553 Selector_Name
:= First
(Choices
(Assoc
));
5554 while Present
(Selector_Name
) loop
5555 if Nkind
(Selector_Name
) = N_Identifier
then
5558 elsif Nkind
(Selector_Name
) = N_Others_Choice
then
5559 if Selector_Name
/= First
(Choices
(Assoc
))
5560 or else Present
(Next
(Selector_Name
))
5563 ("OTHERS must appear alone in a choice list",
5567 elsif Present
(Next
(Assoc
)) then
5569 ("OTHERS must appear last in an aggregate",
5573 -- (Ada 2005): If this is an association with a box,
5574 -- indicate that the association need not represent
5577 elsif Box_Present
(Assoc
) then
5584 ("selector name should be identifier or OTHERS",
5586 Bad_Aggregate
:= True;
5589 Next
(Selector_Name
);
5595 if Bad_Aggregate
then
5600 -- STEP 3: Find discriminant Values
5603 Discrim
: Entity_Id
;
5604 Missing_Discriminants
: Boolean := False;
5607 if Present
(Expressions
(N
)) then
5608 Positional_Expr
:= First
(Expressions
(N
));
5610 Positional_Expr
:= Empty
;
5613 -- AI05-0115: if the ancestor part is a subtype mark, the ancestor
5614 -- must not have unknown discriminants.
5615 -- ??? We are not checking any subtype mark here and this code is not
5616 -- exercised by any test, so it's likely wrong (in particular
5617 -- we should not use Root_Type here but the subtype mark, if any),
5618 -- and possibly not needed.
5620 if Is_Derived_Type
(Typ
)
5621 and then Has_Unknown_Discriminants
(Root_Type
(Typ
))
5622 and then Nkind
(N
) /= N_Extension_Aggregate
5625 ("aggregate not available for type& whose ancestor "
5626 & "has unknown discriminants", N
, Typ
);
5629 if Has_Unknown_Discriminants
(Typ
)
5630 and then Present
(Underlying_Record_View
(Typ
))
5632 Discrim
:= First_Discriminant
(Underlying_Record_View
(Typ
));
5633 elsif Has_Discriminants
(Typ
) then
5634 Discrim
:= First_Discriminant
(Typ
);
5639 -- First find the discriminant values in the positional components
5641 while Present
(Discrim
) and then Present
(Positional_Expr
) loop
5642 if Discriminant_Present
(Discrim
) then
5643 Resolve_Aggr_Expr
(Positional_Expr
, Discrim
);
5645 -- Ada 2005 (AI-231)
5647 if Ada_Version
>= Ada_2005
5648 and then Known_Null
(Positional_Expr
)
5650 Check_Can_Never_Be_Null
(Discrim
, Positional_Expr
);
5653 Next
(Positional_Expr
);
5656 if Present
(Get_Value
(Discrim
, Component_Associations
(N
))) then
5658 ("more than one value supplied for discriminant&",
5662 Next_Discriminant
(Discrim
);
5665 -- Find remaining discriminant values if any among named components
5667 while Present
(Discrim
) loop
5668 Expr
:= Get_Value
(Discrim
, Component_Associations
(N
), True);
5670 if not Discriminant_Present
(Discrim
) then
5671 if Present
(Expr
) then
5673 ("more than one value supplied for discriminant &",
5677 elsif No
(Expr
) then
5679 ("no value supplied for discriminant &", N
, Discrim
);
5680 Missing_Discriminants
:= True;
5683 Resolve_Aggr_Expr
(Expr
, Discrim
);
5686 Next_Discriminant
(Discrim
);
5689 if Missing_Discriminants
then
5693 -- At this point and until the beginning of STEP 6, New_Assoc_List
5694 -- contains only the discriminants and their values.
5698 -- STEP 4: Set the Etype of the record aggregate
5700 if Has_Discriminants
(Typ
)
5701 or else (Has_Unknown_Discriminants
(Typ
)
5702 and then Present
(Underlying_Record_View
(Typ
)))
5704 Build_Constrained_Itype
(N
, Typ
, New_Assoc_List
);
5709 -- STEP 5: Get remaining components according to discriminant values
5713 Errors_Found
: Boolean := False;
5714 Record_Def
: Node_Id
;
5715 Parent_Typ
: Entity_Id
;
5716 Parent_Typ_List
: Elist_Id
;
5717 Parent_Elmt
: Elmt_Id
;
5718 Root_Typ
: Entity_Id
;
5721 if Is_Derived_Type
(Typ
) and then Is_Tagged_Type
(Typ
) then
5722 Parent_Typ_List
:= New_Elmt_List
;
5724 -- If this is an extension aggregate, the component list must
5725 -- include all components that are not in the given ancestor type.
5726 -- Otherwise, the component list must include components of all
5727 -- ancestors, starting with the root.
5729 if Nkind
(N
) = N_Extension_Aggregate
then
5730 Root_Typ
:= Base_Type
(Etype
(Ancestor_Part
(N
)));
5733 -- AI05-0115: check legality of aggregate for type with a
5734 -- private ancestor.
5736 Root_Typ
:= Root_Type
(Typ
);
5737 if Has_Private_Ancestor
(Typ
) then
5739 Ancestor
: constant Entity_Id
:=
5740 Find_Private_Ancestor
(Typ
);
5741 Ancestor_Unit
: constant Entity_Id
:=
5743 (Get_Source_Unit
(Ancestor
));
5744 Parent_Unit
: constant Entity_Id
:=
5745 Cunit_Entity
(Get_Source_Unit
5746 (Base_Type
(Etype
(Ancestor
))));
5748 -- Check whether we are in a scope that has full view
5749 -- over the private ancestor and its parent. This can
5750 -- only happen if the derivation takes place in a child
5751 -- unit of the unit that declares the parent, and we are
5752 -- in the private part or body of that child unit, else
5753 -- the aggregate is illegal.
5755 if Is_Child_Unit
(Ancestor_Unit
)
5756 and then Scope
(Ancestor_Unit
) = Parent_Unit
5757 and then In_Open_Scopes
(Scope
(Ancestor
))
5759 (In_Private_Part
(Scope
(Ancestor
))
5760 or else In_Package_Body
(Scope
(Ancestor
)))
5766 ("type of aggregate has private ancestor&!",
5768 Error_Msg_N
("must use extension aggregate!", N
);
5774 Dnode
:= Declaration_Node
(Base_Type
(Root_Typ
));
5776 -- If we don't get a full declaration, then we have some error
5777 -- which will get signalled later so skip this part. Otherwise
5778 -- gather components of root that apply to the aggregate type.
5779 -- We use the base type in case there is an applicable stored
5780 -- constraint that renames the discriminants of the root.
5782 if Nkind
(Dnode
) = N_Full_Type_Declaration
then
5783 Record_Def
:= Type_Definition
(Dnode
);
5786 Component_List
(Record_Def
),
5787 Governed_By
=> New_Assoc_List
,
5789 Report_Errors
=> Errors_Found
);
5791 if Errors_Found
then
5793 ("discriminant controlling variant part is not static",
5800 Parent_Typ
:= Base_Type
(Typ
);
5801 while Parent_Typ
/= Root_Typ
loop
5802 Prepend_Elmt
(Parent_Typ
, To
=> Parent_Typ_List
);
5803 Parent_Typ
:= Etype
(Parent_Typ
);
5805 -- Check whether a private parent requires the use of
5806 -- an extension aggregate. This test does not apply in
5807 -- an instantiation: if the generic unit is legal so is
5810 if Nkind
(Parent
(Base_Type
(Parent_Typ
))) =
5811 N_Private_Type_Declaration
5812 or else Nkind
(Parent
(Base_Type
(Parent_Typ
))) =
5813 N_Private_Extension_Declaration
5815 if Nkind
(N
) /= N_Extension_Aggregate
5816 and then not In_Instance
5819 ("type of aggregate has private ancestor&!",
5821 Error_Msg_N
("must use extension aggregate!", N
);
5824 elsif Parent_Typ
/= Root_Typ
then
5826 ("ancestor part of aggregate must be private type&",
5827 Ancestor_Part
(N
), Parent_Typ
);
5831 -- The current view of ancestor part may be a private type,
5832 -- while the context type is always non-private.
5834 elsif Is_Private_Type
(Root_Typ
)
5835 and then Present
(Full_View
(Root_Typ
))
5836 and then Nkind
(N
) = N_Extension_Aggregate
5838 exit when Base_Type
(Full_View
(Root_Typ
)) = Parent_Typ
;
5842 -- Now collect components from all other ancestors, beginning
5843 -- with the current type. If the type has unknown discriminants
5844 -- use the component list of the Underlying_Record_View, which
5845 -- needs to be used for the subsequent expansion of the aggregate
5846 -- into assignments.
5848 Parent_Elmt
:= First_Elmt
(Parent_Typ_List
);
5849 while Present
(Parent_Elmt
) loop
5850 Parent_Typ
:= Node
(Parent_Elmt
);
5852 if Has_Unknown_Discriminants
(Parent_Typ
)
5853 and then Present
(Underlying_Record_View
(Typ
))
5855 Parent_Typ
:= Underlying_Record_View
(Parent_Typ
);
5858 Record_Def
:= Type_Definition
(Parent
(Base_Type
(Parent_Typ
)));
5859 Gather_Components
(Parent_Typ
,
5860 Component_List
(Record_Extension_Part
(Record_Def
)),
5861 Governed_By
=> New_Assoc_List
,
5863 Report_Errors
=> Errors_Found
);
5865 Next_Elmt
(Parent_Elmt
);
5868 -- Typ is not a derived tagged type
5871 Record_Def
:= Type_Definition
(Parent
(Base_Type
(Typ
)));
5873 if Null_Present
(Record_Def
) then
5876 elsif not Has_Unknown_Discriminants
(Typ
) then
5879 Component_List
(Record_Def
),
5880 Governed_By
=> New_Assoc_List
,
5882 Report_Errors
=> Errors_Found
);
5886 (Base_Type
(Underlying_Record_View
(Typ
)),
5887 Component_List
(Record_Def
),
5888 Governed_By
=> New_Assoc_List
,
5890 Report_Errors
=> Errors_Found
);
5894 if Errors_Found
then
5899 -- STEP 6: Find component Values
5901 Component_Elmt
:= First_Elmt
(Components
);
5903 -- First scan the remaining positional associations in the aggregate.
5904 -- Remember that at this point Positional_Expr contains the current
5905 -- positional association if any is left after looking for discriminant
5906 -- values in step 3.
5908 while Present
(Positional_Expr
) and then Present
(Component_Elmt
) loop
5909 Component
:= Node
(Component_Elmt
);
5910 Resolve_Aggr_Expr
(Positional_Expr
, Component
);
5912 -- Ada 2005 (AI-231)
5914 if Ada_Version
>= Ada_2005
and then Known_Null
(Positional_Expr
) then
5915 Check_Can_Never_Be_Null
(Component
, Positional_Expr
);
5918 if Present
(Get_Value
(Component
, Component_Associations
(N
))) then
5920 ("more than one value supplied for component &", N
, Component
);
5923 Next
(Positional_Expr
);
5924 Next_Elmt
(Component_Elmt
);
5927 if Present
(Positional_Expr
) then
5929 ("too many components for record aggregate", Positional_Expr
);
5932 -- Now scan for the named arguments of the aggregate
5934 while Present
(Component_Elmt
) loop
5935 Component
:= Node
(Component_Elmt
);
5936 Expr
:= Get_Value
(Component
, Component_Associations
(N
), True);
5938 -- Note: The previous call to Get_Value sets the value of the
5939 -- variable Is_Box_Present.
5941 -- Ada 2005 (AI-287): Handle components with default initialization.
5942 -- Note: This feature was originally added to Ada 2005 for limited
5943 -- but it was finally allowed with any type.
5945 if Is_Box_Present
then
5946 Check_Box_Component
: declare
5947 Ctyp
: constant Entity_Id
:= Etype
(Component
);
5950 -- Initially assume that the box is for a default-initialized
5951 -- component and reset to False in cases where that's not true.
5953 Is_Box_Init_By_Default
:= True;
5955 -- If there is a default expression for the aggregate, copy
5956 -- it into a new association. This copy must modify the scopes
5957 -- of internal types that may be attached to the expression
5958 -- (e.g. index subtypes of arrays) because in general the type
5959 -- declaration and the aggregate appear in different scopes,
5960 -- and the backend requires the scope of the type to match the
5961 -- point at which it is elaborated.
5963 -- If the component has an initialization procedure (IP) we
5964 -- pass the component to the expander, which will generate
5965 -- the call to such IP.
5967 -- If the component has discriminants, their values must
5968 -- be taken from their subtype. This is indispensable for
5969 -- constraints that are given by the current instance of an
5970 -- enclosing type, to allow the expansion of the aggregate to
5971 -- replace the reference to the current instance by the target
5972 -- object of the aggregate.
5974 if Is_Case_Choice_Pattern
(N
) then
5976 -- Do not transform box component values in a case-choice
5980 (Component
=> Component
,
5982 Assoc_List
=> New_Assoc_List
,
5983 Is_Box_Present
=> True);
5985 elsif Present
(Parent
(Component
))
5986 and then Nkind
(Parent
(Component
)) = N_Component_Declaration
5987 and then Present
(Expression
(Parent
(Component
)))
5989 -- If component declaration has an initialization expression
5990 -- then this is not a case of default initialization.
5992 Is_Box_Init_By_Default
:= False;
5995 New_Copy_Tree_And_Copy_Dimensions
5996 (Expression
(Parent
(Component
)),
5997 New_Scope
=> Current_Scope
,
5998 New_Sloc
=> Sloc
(N
));
6000 -- As the type of the copied default expression may refer
6001 -- to discriminants of the record type declaration, these
6002 -- non-stored discriminants need to be rewritten into stored
6003 -- discriminant values for the aggregate. This is required
6004 -- in GNATprove mode, and is adopted in all modes to avoid
6005 -- special-casing GNATprove mode.
6007 if Is_Array_Type
(Etype
(Expr
)) then
6009 Rec_Typ
: constant Entity_Id
:= Scope
(Component
);
6010 -- Root record type whose discriminants may be used as
6011 -- bounds in range nodes.
6018 -- Rewrite the range nodes occurring in the indexes
6021 Index
:= First_Index
(Etype
(Expr
));
6022 while Present
(Index
) loop
6023 Rewrite_Range
(Rec_Typ
, Index
);
6025 (Rec_Typ
, Scalar_Range
(Etype
(Index
)));
6030 -- Rewrite the range nodes occurring as aggregate
6031 -- bounds and component associations.
6033 if Nkind
(Expr
) = N_Aggregate
then
6034 if Present
(Aggregate_Bounds
(Expr
)) then
6035 Rewrite_Range
(Rec_Typ
, Aggregate_Bounds
(Expr
));
6038 if Present
(Component_Associations
(Expr
)) then
6039 Assoc
:= First
(Component_Associations
(Expr
));
6040 while Present
(Assoc
) loop
6041 Choice
:= First
(Choices
(Assoc
));
6042 while Present
(Choice
) loop
6043 Rewrite_Range
(Rec_Typ
, Choice
);
6056 (Component
=> Component
,
6058 Assoc_List
=> New_Assoc_List
);
6059 Set_Has_Self_Reference
(N
);
6061 elsif Needs_Simple_Initialization
(Ctyp
) then
6063 (Component
=> Component
,
6065 Assoc_List
=> New_Assoc_List
,
6066 Is_Box_Present
=> True);
6068 elsif Has_Non_Null_Base_Init_Proc
(Ctyp
)
6069 or else not Expander_Active
6071 if Is_Record_Type
(Ctyp
)
6072 and then Has_Discriminants
(Ctyp
)
6073 and then not Is_Private_Type
(Ctyp
)
6075 -- We build a partially initialized aggregate with the
6076 -- values of the discriminants and box initialization
6077 -- for the rest, if other components are present.
6079 -- The type of the aggregate is the known subtype of
6080 -- the component. The capture of discriminants must be
6081 -- recursive because subcomponents may be constrained
6082 -- (transitively) by discriminants of enclosing types.
6083 -- For a private type with discriminants, a call to the
6084 -- initialization procedure will be generated, and no
6085 -- subaggregate is needed.
6087 Capture_Discriminants
: declare
6088 Loc
: constant Source_Ptr
:= Sloc
(N
);
6092 Expr
:= Make_Aggregate
(Loc
, No_List
, New_List
);
6093 Set_Etype
(Expr
, Ctyp
);
6095 -- If the enclosing type has discriminants, they have
6096 -- been collected in the aggregate earlier, and they
6097 -- may appear as constraints of subcomponents.
6099 -- Similarly if this component has discriminants, they
6100 -- might in turn be propagated to their components.
6102 if Has_Discriminants
(Typ
) then
6103 Add_Discriminant_Values
(Expr
, New_Assoc_List
);
6104 Propagate_Discriminants
(Expr
, New_Assoc_List
);
6106 elsif Has_Discriminants
(Ctyp
) then
6107 Add_Discriminant_Values
6108 (Expr
, Component_Associations
(Expr
));
6109 Propagate_Discriminants
6110 (Expr
, Component_Associations
(Expr
));
6112 Build_Constrained_Itype
6113 (Expr
, Ctyp
, Component_Associations
(Expr
));
6120 -- If the type has additional components, create
6121 -- an OTHERS box association for them.
6123 Comp
:= First_Component
(Ctyp
);
6124 while Present
(Comp
) loop
6125 if Ekind
(Comp
) = E_Component
then
6126 if not Is_Record_Type
(Etype
(Comp
)) then
6128 (Component_Associations
(Expr
),
6129 Make_Component_Association
(Loc
,
6132 Make_Others_Choice
(Loc
)),
6133 Expression
=> Empty
,
6134 Box_Present
=> True));
6140 Next_Component
(Comp
);
6146 (Component
=> Component
,
6148 Assoc_List
=> New_Assoc_List
);
6149 end Capture_Discriminants
;
6151 -- Otherwise the component type is not a record, or it has
6152 -- not discriminants, or it is private.
6156 (Component
=> Component
,
6158 Assoc_List
=> New_Assoc_List
,
6159 Is_Box_Present
=> True);
6162 -- Otherwise we only need to resolve the expression if the
6163 -- component has partially initialized values (required to
6164 -- expand the corresponding assignments and run-time checks).
6166 elsif Present
(Expr
)
6167 and then Is_Partially_Initialized_Type
(Ctyp
)
6169 Resolve_Aggr_Expr
(Expr
, Component
);
6171 end Check_Box_Component
;
6173 elsif No
(Expr
) then
6175 -- Ignore hidden components associated with the position of the
6176 -- interface tags: these are initialized dynamically.
6178 if No
(Related_Type
(Component
)) then
6180 ("no value supplied for component &!", N
, Component
);
6184 Resolve_Aggr_Expr
(Expr
, Component
);
6187 Next_Elmt
(Component_Elmt
);
6190 -- STEP 7: check for invalid components + check type in choice list
6194 New_Assoc
: Node_Id
;
6200 -- Type of first component in choice list
6203 if Present
(Component_Associations
(N
)) then
6204 Assoc
:= First
(Component_Associations
(N
));
6209 Verification
: while Present
(Assoc
) loop
6210 Selectr
:= First
(Choices
(Assoc
));
6213 if Nkind
(Selectr
) = N_Others_Choice
then
6215 -- Ada 2005 (AI-287): others choice may have expression or box
6217 if No
(Others_Etype
) and then Others_Box
= 0 then
6219 ("OTHERS must represent at least one component", Selectr
);
6221 elsif Others_Box
= 1 and then Warn_On_Redundant_Constructs
then
6222 Error_Msg_N
("OTHERS choice is redundant?r?", Box_Node
);
6224 ("\previous choices cover all components?r?", Box_Node
);
6230 while Present
(Selectr
) loop
6232 New_Assoc
:= First
(New_Assoc_List
);
6233 while Present
(New_Assoc
) loop
6234 Component
:= First
(Choices
(New_Assoc
));
6236 if Chars
(Selectr
) = Chars
(Component
) then
6238 Check_Identifier
(Selectr
, Entity
(Component
));
6247 -- If we found an association, then this is a legal component
6248 -- of the type in question.
6250 pragma Assert
(if Present
(New_Assoc
) then Present
(Component
));
6252 -- If no association, this is not a legal component of the type
6253 -- in question, unless its association is provided with a box.
6255 if No
(New_Assoc
) then
6256 if Box_Present
(Parent
(Selectr
)) then
6258 -- This may still be a bogus component with a box. Scan
6259 -- list of components to verify that a component with
6260 -- that name exists.
6266 C
:= First_Component
(Typ
);
6267 while Present
(C
) loop
6268 if Chars
(C
) = Chars
(Selectr
) then
6270 -- If the context is an extension aggregate,
6271 -- the component must not be inherited from
6272 -- the ancestor part of the aggregate.
6274 if Nkind
(N
) /= N_Extension_Aggregate
6276 Scope
(Original_Record_Component
(C
)) /=
6277 Etype
(Ancestor_Part
(N
))
6287 Error_Msg_Node_2
:= Typ
;
6288 Error_Msg_N
("& is not a component of}", Selectr
);
6292 elsif Chars
(Selectr
) /= Name_uTag
6293 and then Chars
(Selectr
) /= Name_uParent
6295 if not Has_Discriminants
(Typ
) then
6296 Error_Msg_Node_2
:= Typ
;
6297 Error_Msg_N
("& is not a component of}", Selectr
);
6300 ("& is not a component of the aggregate subtype",
6304 Check_Misspelled_Component
(Components
, Selectr
);
6307 elsif No
(Typech
) then
6308 Typech
:= Base_Type
(Etype
(Component
));
6310 -- AI05-0199: In Ada 2012, several components of anonymous
6311 -- access types can appear in a choice list, as long as the
6312 -- designated types match.
6314 elsif Typech
/= Base_Type
(Etype
(Component
)) then
6315 if Ada_Version
>= Ada_2012
6316 and then Ekind
(Typech
) = E_Anonymous_Access_Type
6318 Ekind
(Etype
(Component
)) = E_Anonymous_Access_Type
6319 and then Base_Type
(Designated_Type
(Typech
)) =
6320 Base_Type
(Designated_Type
(Etype
(Component
)))
6322 Subtypes_Statically_Match
(Typech
, (Etype
(Component
)))
6326 elsif not Box_Present
(Parent
(Selectr
)) then
6328 ("components in choice list must have same type",
6337 end loop Verification
;
6340 -- STEP 8: replace the original aggregate
6343 New_Aggregate
: constant Node_Id
:= New_Copy
(N
);
6346 Set_Expressions
(New_Aggregate
, No_List
);
6347 Set_Etype
(New_Aggregate
, Etype
(N
));
6348 Set_Component_Associations
(New_Aggregate
, New_Assoc_List
);
6349 Set_Check_Actuals
(New_Aggregate
, Check_Actuals
(N
));
6351 Rewrite
(N
, New_Aggregate
);
6354 -- Check the dimensions of the components in the record aggregate
6356 Analyze_Dimension_Extension_Or_Record_Aggregate
(N
);
6357 end Resolve_Record_Aggregate
;
6359 -----------------------------
6360 -- Check_Can_Never_Be_Null --
6361 -----------------------------
6363 procedure Check_Can_Never_Be_Null
(Typ
: Entity_Id
; Expr
: Node_Id
) is
6364 Comp_Typ
: Entity_Id
;
6368 (Ada_Version
>= Ada_2005
6369 and then Present
(Expr
)
6370 and then Known_Null
(Expr
));
6373 when E_Array_Type
=>
6374 Comp_Typ
:= Component_Type
(Typ
);
6379 Comp_Typ
:= Etype
(Typ
);
6385 if Can_Never_Be_Null
(Comp_Typ
) then
6387 -- Here we know we have a constraint error. Note that we do not use
6388 -- Apply_Compile_Time_Constraint_Error here to the Expr, which might
6389 -- seem the more natural approach. That's because in some cases the
6390 -- components are rewritten, and the replacement would be missed.
6391 -- We do not mark the whole aggregate as raising a constraint error,
6392 -- because the association may be a null array range.
6395 ("(Ada 2005) NULL not allowed in null-excluding component??", Expr
);
6397 ("\Constraint_Error will be raised at run time??", Expr
);
6400 Make_Raise_Constraint_Error
6401 (Sloc
(Expr
), Reason
=> CE_Access_Check_Failed
));
6402 Set_Etype
(Expr
, Comp_Typ
);
6403 Set_Analyzed
(Expr
);
6405 end Check_Can_Never_Be_Null
;
6407 ---------------------
6408 -- Sort_Case_Table --
6409 ---------------------
6411 procedure Sort_Case_Table
(Case_Table
: in out Case_Table_Type
) is
6412 U
: constant Int
:= Case_Table
'Last;
6420 T
:= Case_Table
(K
+ 1);
6424 and then Expr_Value
(Case_Table
(J
- 1).Lo
) > Expr_Value
(T
.Lo
)
6426 Case_Table
(J
) := Case_Table
(J
- 1);
6430 Case_Table
(J
) := T
;
6433 end Sort_Case_Table
;