1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2007, 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 Atree
; use Atree
;
27 with Checks
; use Checks
;
28 with Einfo
; use Einfo
;
29 with Elists
; use Elists
;
30 with Errout
; use Errout
;
31 with Exp_Tss
; use Exp_Tss
;
32 with Exp_Util
; use Exp_Util
;
33 with Freeze
; use Freeze
;
34 with Itypes
; use Itypes
;
36 with Lib
.Xref
; use Lib
.Xref
;
37 with Namet
; use Namet
;
38 with Nmake
; use Nmake
;
39 with Nlists
; use Nlists
;
42 with Sem_Cat
; use Sem_Cat
;
43 with Sem_Ch3
; use Sem_Ch3
;
44 with Sem_Ch13
; use Sem_Ch13
;
45 with Sem_Eval
; use Sem_Eval
;
46 with Sem_Res
; use Sem_Res
;
47 with Sem_Util
; use Sem_Util
;
48 with Sem_Type
; use Sem_Type
;
49 with Sem_Warn
; use Sem_Warn
;
50 with Sinfo
; use Sinfo
;
51 with Snames
; use Snames
;
52 with Stringt
; use Stringt
;
53 with Stand
; use Stand
;
54 with Targparm
; use Targparm
;
55 with Tbuild
; use Tbuild
;
56 with Uintp
; use Uintp
;
58 with GNAT
.Spelling_Checker
; use GNAT
.Spelling_Checker
;
60 package body Sem_Aggr
is
62 type Case_Bounds
is record
65 Choice_Node
: Node_Id
;
68 type Case_Table_Type
is array (Nat
range <>) of Case_Bounds
;
69 -- Table type used by Check_Case_Choices procedure
71 -----------------------
72 -- Local Subprograms --
73 -----------------------
75 procedure Sort_Case_Table
(Case_Table
: in out Case_Table_Type
);
76 -- Sort the Case Table using the Lower Bound of each Choice as the key.
77 -- A simple insertion sort is used since the number of choices in a case
78 -- statement of variant part will usually be small and probably in near
81 procedure Check_Can_Never_Be_Null
(Typ
: Entity_Id
; Expr
: Node_Id
);
82 -- Ada 2005 (AI-231): Check bad usage of null for a component for which
83 -- null exclusion (NOT NULL) is specified. Typ can be an E_Array_Type for
84 -- the array case (the component type of the array will be used) or an
85 -- E_Component/E_Discriminant entity in the record case, in which case the
86 -- type of the component will be used for the test. If Typ is any other
87 -- kind of entity, the call is ignored. Expr is the component node in the
88 -- aggregate which is known to have a null value. A warning message will be
89 -- issued if the component is null excluding.
91 -- It would be better to pass the proper type for Typ ???
93 ------------------------------------------------------
94 -- Subprograms used for RECORD AGGREGATE Processing --
95 ------------------------------------------------------
97 procedure Resolve_Record_Aggregate
(N
: Node_Id
; Typ
: Entity_Id
);
98 -- This procedure performs all the semantic checks required for record
99 -- aggregates. Note that for aggregates analysis and resolution go
100 -- hand in hand. Aggregate analysis has been delayed up to here and
101 -- it is done while resolving the aggregate.
103 -- N is the N_Aggregate node.
104 -- Typ is the record type for the aggregate resolution
106 -- While performing the semantic checks, this procedure builds a new
107 -- Component_Association_List where each record field appears alone in a
108 -- Component_Choice_List along with its corresponding expression. The
109 -- record fields in the Component_Association_List appear in the same order
110 -- in which they appear in the record type Typ.
112 -- Once this new Component_Association_List is built and all the semantic
113 -- checks performed, the original aggregate subtree is replaced with the
114 -- new named record aggregate just built. Note that subtree substitution is
115 -- performed with Rewrite so as to be able to retrieve the original
118 -- The aggregate subtree manipulation performed by Resolve_Record_Aggregate
119 -- yields the aggregate format expected by Gigi. Typically, this kind of
120 -- tree manipulations are done in the expander. However, because the
121 -- semantic checks that need to be performed on record aggregates really go
122 -- hand in hand with the record aggregate normalization, the aggregate
123 -- subtree transformation is performed during resolution rather than
124 -- expansion. Had we decided otherwise we would have had to duplicate most
125 -- of the code in the expansion procedure Expand_Record_Aggregate. Note,
126 -- however, that all the expansion concerning aggregates for tagged records
127 -- is done in Expand_Record_Aggregate.
129 -- The algorithm of Resolve_Record_Aggregate proceeds as follows:
131 -- 1. Make sure that the record type against which the record aggregate
132 -- has to be resolved is not abstract. Furthermore if the type is
133 -- a null aggregate make sure the input aggregate N is also null.
135 -- 2. Verify that the structure of the aggregate is that of a record
136 -- aggregate. Specifically, look for component associations and ensure
137 -- that each choice list only has identifiers or the N_Others_Choice
138 -- node. Also make sure that if present, the N_Others_Choice occurs
139 -- last and by itself.
141 -- 3. If Typ contains discriminants, the values for each discriminant
142 -- is looked for. If the record type Typ has variants, we check
143 -- that the expressions corresponding to each discriminant ruling
144 -- the (possibly nested) variant parts of Typ, are static. This
145 -- allows us to determine the variant parts to which the rest of
146 -- the aggregate must conform. The names of discriminants with their
147 -- values are saved in a new association list, New_Assoc_List which
148 -- is later augmented with the names and values of the remaining
149 -- components in the record type.
151 -- During this phase we also make sure that every discriminant is
152 -- assigned exactly one value. Note that when several values
153 -- for a given discriminant are found, semantic processing continues
154 -- looking for further errors. In this case it's the first
155 -- discriminant value found which we will be recorded.
157 -- IMPORTANT NOTE: For derived tagged types this procedure expects
158 -- First_Discriminant and Next_Discriminant to give the correct list
159 -- of discriminants, in the correct order.
161 -- 4. After all the discriminant values have been gathered, we can
162 -- set the Etype of the record aggregate. If Typ contains no
163 -- discriminants this is straightforward: the Etype of N is just
164 -- Typ, otherwise a new implicit constrained subtype of Typ is
165 -- built to be the Etype of N.
167 -- 5. Gather the remaining record components according to the discriminant
168 -- values. This involves recursively traversing the record type
169 -- structure to see what variants are selected by the given discriminant
170 -- values. This processing is a little more convoluted if Typ is a
171 -- derived tagged types since we need to retrieve the record structure
172 -- of all the ancestors of Typ.
174 -- 6. After gathering the record components we look for their values
175 -- in the record aggregate and emit appropriate error messages
176 -- should we not find such values or should they be duplicated.
178 -- 7. We then make sure no illegal component names appear in the
179 -- record aggregate and make sure that the type of the record
180 -- components appearing in a same choice list is the same.
181 -- Finally we ensure that the others choice, if present, is
182 -- used to provide the value of at least a record component.
184 -- 8. The original aggregate node is replaced with the new named
185 -- aggregate built in steps 3 through 6, as explained earlier.
187 -- Given the complexity of record aggregate resolution, the primary
188 -- goal of this routine is clarity and simplicity rather than execution
189 -- and storage efficiency. If there are only positional components in the
190 -- aggregate the running time is linear. If there are associations
191 -- the running time is still linear as long as the order of the
192 -- associations is not too far off the order of the components in the
193 -- record type. If this is not the case the running time is at worst
194 -- quadratic in the size of the association list.
196 procedure Check_Misspelled_Component
197 (Elements
: Elist_Id
;
198 Component
: Node_Id
);
199 -- Give possible misspelling diagnostic if Component is likely to be
200 -- a misspelling of one of the components of the Assoc_List.
201 -- This is called by Resolv_Aggr_Expr after producing
202 -- an invalid component error message.
204 procedure Check_Static_Discriminated_Subtype
(T
: Entity_Id
; V
: Node_Id
);
205 -- An optimization: determine whether a discriminated subtype has a
206 -- static constraint, and contains array components whose length is also
207 -- static, either because they are constrained by the discriminant, or
208 -- because the original component bounds are static.
210 -----------------------------------------------------
211 -- Subprograms used for ARRAY AGGREGATE Processing --
212 -----------------------------------------------------
214 function Resolve_Array_Aggregate
217 Index_Constr
: Node_Id
;
218 Component_Typ
: Entity_Id
;
219 Others_Allowed
: Boolean)
221 -- This procedure performs the semantic checks for an array aggregate.
222 -- True is returned if the aggregate resolution succeeds.
223 -- The procedure works by recursively checking each nested aggregate.
224 -- Specifically, after checking a sub-aggregate nested at the i-th level
225 -- we recursively check all the subaggregates at the i+1-st level (if any).
226 -- Note that for aggregates analysis and resolution go hand in hand.
227 -- Aggregate analysis has been delayed up to here and it is done while
228 -- resolving the aggregate.
230 -- N is the current N_Aggregate node to be checked.
232 -- Index is the index node corresponding to the array sub-aggregate that
233 -- we are currently checking (RM 4.3.3 (8)). Its Etype is the
234 -- corresponding index type (or subtype).
236 -- Index_Constr is the node giving the applicable index constraint if
237 -- any (RM 4.3.3 (10)). It "is a constraint provided by certain
238 -- contexts [...] that can be used to determine the bounds of the array
239 -- value specified by the aggregate". If Others_Allowed below is False
240 -- there is no applicable index constraint and this node is set to Index.
242 -- Component_Typ is the array component type.
244 -- Others_Allowed indicates whether an others choice is allowed
245 -- in the context where the top-level aggregate appeared.
247 -- The algorithm of Resolve_Array_Aggregate proceeds as follows:
249 -- 1. Make sure that the others choice, if present, is by itself and
250 -- appears last in the sub-aggregate. Check that we do not have
251 -- positional and named components in the array sub-aggregate (unless
252 -- the named association is an others choice). Finally if an others
253 -- choice is present, make sure it is allowed in the aggregate contex.
255 -- 2. If the array sub-aggregate contains discrete_choices:
257 -- (A) Verify their validity. Specifically verify that:
259 -- (a) If a null range is present it must be the only possible
260 -- choice in the array aggregate.
262 -- (b) Ditto for a non static range.
264 -- (c) Ditto for a non static expression.
266 -- In addition this step analyzes and resolves each discrete_choice,
267 -- making sure that its type is the type of the corresponding Index.
268 -- If we are not at the lowest array aggregate level (in the case of
269 -- multi-dimensional aggregates) then invoke Resolve_Array_Aggregate
270 -- recursively on each component expression. Otherwise, resolve the
271 -- bottom level component expressions against the expected component
272 -- type ONLY IF the component corresponds to a single discrete choice
273 -- which is not an others choice (to see why read the DELAYED
274 -- COMPONENT RESOLUTION below).
276 -- (B) Determine the bounds of the sub-aggregate and lowest and
277 -- highest choice values.
279 -- 3. For positional aggregates:
281 -- (A) Loop over the component expressions either recursively invoking
282 -- Resolve_Array_Aggregate on each of these for multi-dimensional
283 -- array aggregates or resolving the bottom level component
284 -- expressions against the expected component type.
286 -- (B) Determine the bounds of the positional sub-aggregates.
288 -- 4. Try to determine statically whether the evaluation of the array
289 -- sub-aggregate raises Constraint_Error. If yes emit proper
290 -- warnings. The precise checks are the following:
292 -- (A) Check that the index range defined by aggregate bounds is
293 -- compatible with corresponding index subtype.
294 -- We also check against the base type. In fact it could be that
295 -- Low/High bounds of the base type are static whereas those of
296 -- the index subtype are not. Thus if we can statically catch
297 -- a problem with respect to the base type we are guaranteed
298 -- that the same problem will arise with the index subtype
300 -- (B) If we are dealing with a named aggregate containing an others
301 -- choice and at least one discrete choice then make sure the range
302 -- specified by the discrete choices does not overflow the
303 -- aggregate bounds. We also check against the index type and base
304 -- type bounds for the same reasons given in (A).
306 -- (C) If we are dealing with a positional aggregate with an others
307 -- choice make sure the number of positional elements specified
308 -- does not overflow the aggregate bounds. We also check against
309 -- the index type and base type bounds as mentioned in (A).
311 -- Finally construct an N_Range node giving the sub-aggregate bounds.
312 -- Set the Aggregate_Bounds field of the sub-aggregate to be this
313 -- N_Range. The routine Array_Aggr_Subtype below uses such N_Ranges
314 -- to build the appropriate aggregate subtype. Aggregate_Bounds
315 -- information is needed during expansion.
317 -- DELAYED COMPONENT RESOLUTION: The resolution of bottom level component
318 -- expressions in an array aggregate may call Duplicate_Subexpr or some
319 -- other routine that inserts code just outside the outermost aggregate.
320 -- If the array aggregate contains discrete choices or an others choice,
321 -- this may be wrong. Consider for instance the following example.
323 -- type Rec is record
327 -- type Acc_Rec is access Rec;
328 -- Arr : array (1..3) of Acc_Rec := (1 .. 3 => new Rec);
330 -- Then the transformation of "new Rec" that occurs during resolution
331 -- entails the following code modifications
333 -- P7b : constant Acc_Rec := new Rec;
335 -- Arr : array (1..3) of Acc_Rec := (1 .. 3 => P7b);
337 -- This code transformation is clearly wrong, since we need to call
338 -- "new Rec" for each of the 3 array elements. To avoid this problem we
339 -- delay resolution of the components of non positional array aggregates
340 -- to the expansion phase. As an optimization, if the discrete choice
341 -- specifies a single value we do not delay resolution.
343 function Array_Aggr_Subtype
(N
: Node_Id
; Typ
: Node_Id
) return Entity_Id
;
344 -- This routine returns the type or subtype of an array aggregate.
346 -- N is the array aggregate node whose type we return.
348 -- Typ is the context type in which N occurs.
350 -- This routine creates an implicit array subtype whose bounds are
351 -- those defined by the aggregate. When this routine is invoked
352 -- Resolve_Array_Aggregate has already processed aggregate N. Thus the
353 -- Aggregate_Bounds of each sub-aggregate, is an N_Range node giving the
354 -- sub-aggregate bounds. When building the aggregate itype, this function
355 -- traverses the array aggregate N collecting such Aggregate_Bounds and
356 -- constructs the proper array aggregate itype.
358 -- Note that in the case of multidimensional aggregates each inner
359 -- sub-aggregate corresponding to a given array dimension, may provide a
360 -- different bounds. If it is possible to determine statically that
361 -- some sub-aggregates corresponding to the same index do not have the
362 -- same bounds, then a warning is emitted. If such check is not possible
363 -- statically (because some sub-aggregate bounds are dynamic expressions)
364 -- then this job is left to the expander. In all cases the particular
365 -- bounds that this function will chose for a given dimension is the first
366 -- N_Range node for a sub-aggregate corresponding to that dimension.
368 -- Note that the Raises_Constraint_Error flag of an array aggregate
369 -- whose evaluation is determined to raise CE by Resolve_Array_Aggregate,
370 -- is set in Resolve_Array_Aggregate but the aggregate is not
371 -- immediately replaced with a raise CE. In fact, Array_Aggr_Subtype must
372 -- first construct the proper itype for the aggregate (Gigi needs
373 -- this). After constructing the proper itype we will eventually replace
374 -- the top-level aggregate with a raise CE (done in Resolve_Aggregate).
375 -- Of course in cases such as:
377 -- type Arr is array (integer range <>) of Integer;
378 -- A : Arr := (positive range -1 .. 2 => 0);
380 -- The bounds of the aggregate itype are cooked up to look reasonable
381 -- (in this particular case the bounds will be 1 .. 2).
383 procedure Aggregate_Constraint_Checks
385 Check_Typ
: Entity_Id
);
386 -- Checks expression Exp against subtype Check_Typ. If Exp is an
387 -- aggregate and Check_Typ a constrained record type with discriminants,
388 -- we generate the appropriate discriminant checks. If Exp is an array
389 -- aggregate then emit the appropriate length checks. If Exp is a scalar
390 -- type, or a string literal, Exp is changed into Check_Typ'(Exp) to
391 -- ensure that range checks are performed at run time.
393 procedure Make_String_Into_Aggregate
(N
: Node_Id
);
394 -- A string literal can appear in a context in which a one dimensional
395 -- array of characters is expected. This procedure simply rewrites the
396 -- string as an aggregate, prior to resolution.
398 ---------------------------------
399 -- Aggregate_Constraint_Checks --
400 ---------------------------------
402 procedure Aggregate_Constraint_Checks
404 Check_Typ
: Entity_Id
)
406 Exp_Typ
: constant Entity_Id
:= Etype
(Exp
);
409 if Raises_Constraint_Error
(Exp
) then
413 -- This is really expansion activity, so make sure that expansion
414 -- is on and is allowed.
416 if not Expander_Active
or else In_Default_Expression
then
420 -- First check if we have to insert discriminant checks
422 if Has_Discriminants
(Exp_Typ
) then
423 Apply_Discriminant_Check
(Exp
, Check_Typ
);
425 -- Next emit length checks for array aggregates
427 elsif Is_Array_Type
(Exp_Typ
) then
428 Apply_Length_Check
(Exp
, Check_Typ
);
430 -- Finally emit scalar and string checks. If we are dealing with a
431 -- scalar literal we need to check by hand because the Etype of
432 -- literals is not necessarily correct.
434 elsif Is_Scalar_Type
(Exp_Typ
)
435 and then Compile_Time_Known_Value
(Exp
)
437 if Is_Out_Of_Range
(Exp
, Base_Type
(Check_Typ
)) then
438 Apply_Compile_Time_Constraint_Error
439 (Exp
, "value not in range of}?", CE_Range_Check_Failed
,
440 Ent
=> Base_Type
(Check_Typ
),
441 Typ
=> Base_Type
(Check_Typ
));
443 elsif Is_Out_Of_Range
(Exp
, Check_Typ
) then
444 Apply_Compile_Time_Constraint_Error
445 (Exp
, "value not in range of}?", CE_Range_Check_Failed
,
449 elsif not Range_Checks_Suppressed
(Check_Typ
) then
450 Apply_Scalar_Range_Check
(Exp
, Check_Typ
);
453 -- Verify that target type is also scalar, to prevent view anomalies
454 -- in instantiations.
456 elsif (Is_Scalar_Type
(Exp_Typ
)
457 or else Nkind
(Exp
) = N_String_Literal
)
458 and then Is_Scalar_Type
(Check_Typ
)
459 and then Exp_Typ
/= Check_Typ
461 if Is_Entity_Name
(Exp
)
462 and then Ekind
(Entity
(Exp
)) = E_Constant
464 -- If expression is a constant, it is worthwhile checking whether
465 -- it is a bound of the type.
467 if (Is_Entity_Name
(Type_Low_Bound
(Check_Typ
))
468 and then Entity
(Exp
) = Entity
(Type_Low_Bound
(Check_Typ
)))
469 or else (Is_Entity_Name
(Type_High_Bound
(Check_Typ
))
470 and then Entity
(Exp
) = Entity
(Type_High_Bound
(Check_Typ
)))
475 Rewrite
(Exp
, Convert_To
(Check_Typ
, Relocate_Node
(Exp
)));
476 Analyze_And_Resolve
(Exp
, Check_Typ
);
477 Check_Unset_Reference
(Exp
);
480 Rewrite
(Exp
, Convert_To
(Check_Typ
, Relocate_Node
(Exp
)));
481 Analyze_And_Resolve
(Exp
, Check_Typ
);
482 Check_Unset_Reference
(Exp
);
485 -- Ada 2005 (AI-230): Generate a conversion to an anonymous access
486 -- component's type to force the appropriate accessibility checks.
488 -- Ada 2005 (AI-231): Generate conversion to the null-excluding
489 -- type to force the corresponding run-time check
491 elsif Is_Access_Type
(Check_Typ
)
492 and then ((Is_Local_Anonymous_Access
(Check_Typ
))
493 or else (Can_Never_Be_Null
(Check_Typ
)
494 and then not Can_Never_Be_Null
(Exp_Typ
)))
496 Rewrite
(Exp
, Convert_To
(Check_Typ
, Relocate_Node
(Exp
)));
497 Analyze_And_Resolve
(Exp
, Check_Typ
);
498 Check_Unset_Reference
(Exp
);
500 end Aggregate_Constraint_Checks
;
502 ------------------------
503 -- Array_Aggr_Subtype --
504 ------------------------
506 function Array_Aggr_Subtype
511 Aggr_Dimension
: constant Pos
:= Number_Dimensions
(Typ
);
512 -- Number of aggregate index dimensions
514 Aggr_Range
: array (1 .. Aggr_Dimension
) of Node_Id
:= (others => Empty
);
515 -- Constrained N_Range of each index dimension in our aggregate itype
517 Aggr_Low
: array (1 .. Aggr_Dimension
) of Node_Id
:= (others => Empty
);
518 Aggr_High
: array (1 .. Aggr_Dimension
) of Node_Id
:= (others => Empty
);
519 -- Low and High bounds for each index dimension in our aggregate itype
521 Is_Fully_Positional
: Boolean := True;
523 procedure Collect_Aggr_Bounds
(N
: Node_Id
; Dim
: Pos
);
524 -- N is an array (sub-)aggregate. Dim is the dimension corresponding to
525 -- (sub-)aggregate N. This procedure collects the constrained N_Range
526 -- nodes corresponding to each index dimension of our aggregate itype.
527 -- These N_Range nodes are collected in Aggr_Range above.
529 -- Likewise collect in Aggr_Low & Aggr_High above the low and high
530 -- bounds of each index dimension. If, when collecting, two bounds
531 -- corresponding to the same dimension are static and found to differ,
532 -- then emit a warning, and mark N as raising Constraint_Error.
534 -------------------------
535 -- Collect_Aggr_Bounds --
536 -------------------------
538 procedure Collect_Aggr_Bounds
(N
: Node_Id
; Dim
: Pos
) is
539 This_Range
: constant Node_Id
:= Aggregate_Bounds
(N
);
540 -- The aggregate range node of this specific sub-aggregate
542 This_Low
: constant Node_Id
:= Low_Bound
(Aggregate_Bounds
(N
));
543 This_High
: constant Node_Id
:= High_Bound
(Aggregate_Bounds
(N
));
544 -- The aggregate bounds of this specific sub-aggregate
550 -- Collect the first N_Range for a given dimension that you find.
551 -- For a given dimension they must be all equal anyway.
553 if No
(Aggr_Range
(Dim
)) then
554 Aggr_Low
(Dim
) := This_Low
;
555 Aggr_High
(Dim
) := This_High
;
556 Aggr_Range
(Dim
) := This_Range
;
559 if Compile_Time_Known_Value
(This_Low
) then
560 if not Compile_Time_Known_Value
(Aggr_Low
(Dim
)) then
561 Aggr_Low
(Dim
) := This_Low
;
563 elsif Expr_Value
(This_Low
) /= Expr_Value
(Aggr_Low
(Dim
)) then
564 Set_Raises_Constraint_Error
(N
);
565 Error_Msg_N
("sub-aggregate low bound mismatch?", N
);
567 ("\Constraint_Error will be raised at run-time?", N
);
571 if Compile_Time_Known_Value
(This_High
) then
572 if not Compile_Time_Known_Value
(Aggr_High
(Dim
)) then
573 Aggr_High
(Dim
) := This_High
;
576 Expr_Value
(This_High
) /= Expr_Value
(Aggr_High
(Dim
))
578 Set_Raises_Constraint_Error
(N
);
579 Error_Msg_N
("sub-aggregate high bound mismatch?", N
);
581 ("\Constraint_Error will be raised at run-time?", N
);
586 if Dim
< Aggr_Dimension
then
588 -- Process positional components
590 if Present
(Expressions
(N
)) then
591 Expr
:= First
(Expressions
(N
));
592 while Present
(Expr
) loop
593 Collect_Aggr_Bounds
(Expr
, Dim
+ 1);
598 -- Process component associations
600 if Present
(Component_Associations
(N
)) then
601 Is_Fully_Positional
:= False;
603 Assoc
:= First
(Component_Associations
(N
));
604 while Present
(Assoc
) loop
605 Expr
:= Expression
(Assoc
);
606 Collect_Aggr_Bounds
(Expr
, Dim
+ 1);
611 end Collect_Aggr_Bounds
;
613 -- Array_Aggr_Subtype variables
616 -- the final itype of the overall aggregate
618 Index_Constraints
: constant List_Id
:= New_List
;
619 -- The list of index constraints of the aggregate itype
621 -- Start of processing for Array_Aggr_Subtype
624 -- Make sure that the list of index constraints is properly attached
625 -- to the tree, and then collect the aggregate bounds.
627 Set_Parent
(Index_Constraints
, N
);
628 Collect_Aggr_Bounds
(N
, 1);
630 -- Build the list of constrained indices of our aggregate itype
632 for J
in 1 .. Aggr_Dimension
loop
633 Create_Index
: declare
634 Index_Base
: constant Entity_Id
:=
635 Base_Type
(Etype
(Aggr_Range
(J
)));
636 Index_Typ
: Entity_Id
;
639 -- Construct the Index subtype, and associate it with the range
640 -- construct that generates it.
643 Create_Itype
(Subtype_Kind
(Ekind
(Index_Base
)), Aggr_Range
(J
));
645 Set_Etype
(Index_Typ
, Index_Base
);
647 if Is_Character_Type
(Index_Base
) then
648 Set_Is_Character_Type
(Index_Typ
);
651 Set_Size_Info
(Index_Typ
, (Index_Base
));
652 Set_RM_Size
(Index_Typ
, RM_Size
(Index_Base
));
653 Set_First_Rep_Item
(Index_Typ
, First_Rep_Item
(Index_Base
));
654 Set_Scalar_Range
(Index_Typ
, Aggr_Range
(J
));
656 if Is_Discrete_Or_Fixed_Point_Type
(Index_Typ
) then
657 Set_RM_Size
(Index_Typ
, UI_From_Int
(Minimum_Size
(Index_Typ
)));
660 Set_Etype
(Aggr_Range
(J
), Index_Typ
);
662 Append
(Aggr_Range
(J
), To
=> Index_Constraints
);
666 -- Now build the Itype
668 Itype
:= Create_Itype
(E_Array_Subtype
, N
);
670 Set_First_Rep_Item
(Itype
, First_Rep_Item
(Typ
));
671 Set_Convention
(Itype
, Convention
(Typ
));
672 Set_Depends_On_Private
(Itype
, Has_Private_Component
(Typ
));
673 Set_Etype
(Itype
, Base_Type
(Typ
));
674 Set_Has_Alignment_Clause
(Itype
, Has_Alignment_Clause
(Typ
));
675 Set_Is_Aliased
(Itype
, Is_Aliased
(Typ
));
676 Set_Depends_On_Private
(Itype
, Depends_On_Private
(Typ
));
678 Copy_Suppress_Status
(Index_Check
, Typ
, Itype
);
679 Copy_Suppress_Status
(Length_Check
, Typ
, Itype
);
681 Set_First_Index
(Itype
, First
(Index_Constraints
));
682 Set_Is_Constrained
(Itype
, True);
683 Set_Is_Internal
(Itype
, True);
684 Init_Size_Align
(Itype
);
686 -- A simple optimization: purely positional aggregates of static
687 -- components should be passed to gigi unexpanded whenever possible,
688 -- and regardless of the staticness of the bounds themselves. Subse-
689 -- quent checks in exp_aggr verify that type is not packed, etc.
691 Set_Size_Known_At_Compile_Time
(Itype
,
693 and then Comes_From_Source
(N
)
694 and then Size_Known_At_Compile_Time
(Component_Type
(Typ
)));
696 -- We always need a freeze node for a packed array subtype, so that
697 -- we can build the Packed_Array_Type corresponding to the subtype.
698 -- If expansion is disabled, the packed array subtype is not built,
699 -- and we must not generate a freeze node for the type, or else it
700 -- will appear incomplete to gigi.
702 if Is_Packed
(Itype
) and then not In_Default_Expression
703 and then Expander_Active
705 Freeze_Itype
(Itype
, N
);
709 end Array_Aggr_Subtype
;
711 --------------------------------
712 -- Check_Misspelled_Component --
713 --------------------------------
715 procedure Check_Misspelled_Component
716 (Elements
: Elist_Id
;
719 Max_Suggestions
: constant := 2;
721 Nr_Of_Suggestions
: Natural := 0;
722 Suggestion_1
: Entity_Id
:= Empty
;
723 Suggestion_2
: Entity_Id
:= Empty
;
724 Component_Elmt
: Elmt_Id
;
727 -- All the components of List are matched against Component and
728 -- a count is maintained of possible misspellings. When at the
729 -- end of the analysis there are one or two (not more!) possible
730 -- misspellings, these misspellings will be suggested as
731 -- possible correction.
733 Get_Name_String
(Chars
(Component
));
736 S
: constant String (1 .. Name_Len
) :=
737 Name_Buffer
(1 .. Name_Len
);
740 Component_Elmt
:= First_Elmt
(Elements
);
741 while Nr_Of_Suggestions
<= Max_Suggestions
742 and then Present
(Component_Elmt
)
744 Get_Name_String
(Chars
(Node
(Component_Elmt
)));
746 if Is_Bad_Spelling_Of
(Name_Buffer
(1 .. Name_Len
), S
) then
747 Nr_Of_Suggestions
:= Nr_Of_Suggestions
+ 1;
749 case Nr_Of_Suggestions
is
750 when 1 => Suggestion_1
:= Node
(Component_Elmt
);
751 when 2 => Suggestion_2
:= Node
(Component_Elmt
);
756 Next_Elmt
(Component_Elmt
);
759 -- Report at most two suggestions
761 if Nr_Of_Suggestions
= 1 then
762 Error_Msg_NE
("\possible misspelling of&",
763 Component
, Suggestion_1
);
765 elsif Nr_Of_Suggestions
= 2 then
766 Error_Msg_Node_2
:= Suggestion_2
;
767 Error_Msg_NE
("\possible misspelling of& or&",
768 Component
, Suggestion_1
);
771 end Check_Misspelled_Component
;
773 ----------------------------------------
774 -- Check_Static_Discriminated_Subtype --
775 ----------------------------------------
777 procedure Check_Static_Discriminated_Subtype
(T
: Entity_Id
; V
: Node_Id
) is
778 Disc
: constant Entity_Id
:= First_Discriminant
(T
);
783 if Has_Record_Rep_Clause
(T
) then
786 elsif Present
(Next_Discriminant
(Disc
)) then
789 elsif Nkind
(V
) /= N_Integer_Literal
then
793 Comp
:= First_Component
(T
);
794 while Present
(Comp
) loop
795 if Is_Scalar_Type
(Etype
(Comp
)) then
798 elsif Is_Private_Type
(Etype
(Comp
))
799 and then Present
(Full_View
(Etype
(Comp
)))
800 and then Is_Scalar_Type
(Full_View
(Etype
(Comp
)))
804 elsif Is_Array_Type
(Etype
(Comp
)) then
805 if Is_Bit_Packed_Array
(Etype
(Comp
)) then
809 Ind
:= First_Index
(Etype
(Comp
));
810 while Present
(Ind
) loop
811 if Nkind
(Ind
) /= N_Range
812 or else Nkind
(Low_Bound
(Ind
)) /= N_Integer_Literal
813 or else Nkind
(High_Bound
(Ind
)) /= N_Integer_Literal
825 Next_Component
(Comp
);
828 -- On exit, all components have statically known sizes
830 Set_Size_Known_At_Compile_Time
(T
);
831 end Check_Static_Discriminated_Subtype
;
833 --------------------------------
834 -- Make_String_Into_Aggregate --
835 --------------------------------
837 procedure Make_String_Into_Aggregate
(N
: Node_Id
) is
838 Exprs
: constant List_Id
:= New_List
;
839 Loc
: constant Source_Ptr
:= Sloc
(N
);
840 Str
: constant String_Id
:= Strval
(N
);
841 Strlen
: constant Nat
:= String_Length
(Str
);
849 for J
in 1 .. Strlen
loop
850 C
:= Get_String_Char
(Str
, J
);
851 Set_Character_Literal_Name
(C
);
854 Make_Character_Literal
(P
,
856 Char_Literal_Value
=> UI_From_CC
(C
));
857 Set_Etype
(C_Node
, Any_Character
);
858 Append_To
(Exprs
, C_Node
);
861 -- something special for wide strings ???
864 New_N
:= Make_Aggregate
(Loc
, Expressions
=> Exprs
);
865 Set_Analyzed
(New_N
);
866 Set_Etype
(New_N
, Any_Composite
);
869 end Make_String_Into_Aggregate
;
871 -----------------------
872 -- Resolve_Aggregate --
873 -----------------------
875 procedure Resolve_Aggregate
(N
: Node_Id
; Typ
: Entity_Id
) is
876 Pkind
: constant Node_Kind
:= Nkind
(Parent
(N
));
878 Aggr_Subtyp
: Entity_Id
;
879 -- The actual aggregate subtype. This is not necessarily the same as Typ
880 -- which is the subtype of the context in which the aggregate was found.
883 -- Check for aggregates not allowed in configurable run-time mode.
884 -- We allow all cases of aggregates that do not come from source,
885 -- since these are all assumed to be small (e.g. bounds of a string
886 -- literal). We also allow aggregates of types we know to be small.
888 if not Support_Aggregates_On_Target
889 and then Comes_From_Source
(N
)
890 and then (not Known_Static_Esize
(Typ
) or else Esize
(Typ
) > 64)
892 Error_Msg_CRT
("aggregate", N
);
895 -- Ada 2005 (AI-287): Limited aggregates allowed
897 if Is_Limited_Type
(Typ
) and then Ada_Version
< Ada_05
then
898 Error_Msg_N
("aggregate type cannot be limited", N
);
899 Explain_Limited_Type
(Typ
, N
);
901 elsif Is_Class_Wide_Type
(Typ
) then
902 Error_Msg_N
("type of aggregate cannot be class-wide", N
);
904 elsif Typ
= Any_String
905 or else Typ
= Any_Composite
907 Error_Msg_N
("no unique type for aggregate", N
);
908 Set_Etype
(N
, Any_Composite
);
910 elsif Is_Array_Type
(Typ
) and then Null_Record_Present
(N
) then
911 Error_Msg_N
("null record forbidden in array aggregate", N
);
913 elsif Is_Record_Type
(Typ
) then
914 Resolve_Record_Aggregate
(N
, Typ
);
916 elsif Is_Array_Type
(Typ
) then
918 -- First a special test, for the case of a positional aggregate
919 -- of characters which can be replaced by a string literal.
920 -- Do not perform this transformation if this was a string literal
921 -- to start with, whose components needed constraint checks, or if
922 -- the component type is non-static, because it will require those
923 -- checks and be transformed back into an aggregate.
925 if Number_Dimensions
(Typ
) = 1
927 (Root_Type
(Component_Type
(Typ
)) = Standard_Character
929 Root_Type
(Component_Type
(Typ
)) = Standard_Wide_Character
931 Root_Type
(Component_Type
(Typ
)) = Standard_Wide_Wide_Character
)
932 and then No
(Component_Associations
(N
))
933 and then not Is_Limited_Composite
(Typ
)
934 and then not Is_Private_Composite
(Typ
)
935 and then not Is_Bit_Packed_Array
(Typ
)
936 and then Nkind
(Original_Node
(Parent
(N
))) /= N_String_Literal
937 and then Is_Static_Subtype
(Component_Type
(Typ
))
943 Expr
:= First
(Expressions
(N
));
944 while Present
(Expr
) loop
945 exit when Nkind
(Expr
) /= N_Character_Literal
;
952 Expr
:= First
(Expressions
(N
));
953 while Present
(Expr
) loop
954 Store_String_Char
(UI_To_CC
(Char_Literal_Value
(Expr
)));
959 Make_String_Literal
(Sloc
(N
), End_String
));
961 Analyze_And_Resolve
(N
, Typ
);
967 -- Here if we have a real aggregate to deal with
969 Array_Aggregate
: declare
970 Aggr_Resolved
: Boolean;
972 Aggr_Typ
: constant Entity_Id
:= Etype
(Typ
);
973 -- This is the unconstrained array type, which is the type
974 -- against which the aggregate is to be resolved. Typ itself
975 -- is the array type of the context which may not be the same
976 -- subtype as the subtype for the final aggregate.
979 -- In the following we determine whether an others choice is
980 -- allowed inside the array aggregate. The test checks the context
981 -- in which the array aggregate occurs. If the context does not
982 -- permit it, or the aggregate type is unconstrained, an others
983 -- choice is not allowed.
985 -- If expansion is disabled (generic context, or semantics-only
986 -- mode) actual subtypes cannot be constructed, and the type of
987 -- an object may be its unconstrained nominal type. However, if
988 -- the context is an assignment, we assume that "others" is
989 -- allowed, because the target of the assignment will have a
990 -- constrained subtype when fully compiled.
992 -- Note that there is no node for Explicit_Actual_Parameter.
993 -- To test for this context we therefore have to test for node
994 -- N_Parameter_Association which itself appears only if there is a
995 -- formal parameter. Consequently we also need to test for
996 -- N_Procedure_Call_Statement or N_Function_Call.
998 Set_Etype
(N
, Aggr_Typ
); -- may be overridden later on
1000 if Is_Constrained
(Typ
) and then
1001 (Pkind
= N_Assignment_Statement
or else
1002 Pkind
= N_Parameter_Association
or else
1003 Pkind
= N_Function_Call
or else
1004 Pkind
= N_Procedure_Call_Statement
or else
1005 Pkind
= N_Generic_Association
or else
1006 Pkind
= N_Formal_Object_Declaration
or else
1007 Pkind
= N_Simple_Return_Statement
or else
1008 Pkind
= N_Object_Declaration
or else
1009 Pkind
= N_Component_Declaration
or else
1010 Pkind
= N_Parameter_Specification
or else
1011 Pkind
= N_Qualified_Expression
or else
1012 Pkind
= N_Aggregate
or else
1013 Pkind
= N_Extension_Aggregate
or else
1014 Pkind
= N_Component_Association
)
1017 Resolve_Array_Aggregate
1019 Index
=> First_Index
(Aggr_Typ
),
1020 Index_Constr
=> First_Index
(Typ
),
1021 Component_Typ
=> Component_Type
(Typ
),
1022 Others_Allowed
=> True);
1024 elsif not Expander_Active
1025 and then Pkind
= N_Assignment_Statement
1028 Resolve_Array_Aggregate
1030 Index
=> First_Index
(Aggr_Typ
),
1031 Index_Constr
=> First_Index
(Typ
),
1032 Component_Typ
=> Component_Type
(Typ
),
1033 Others_Allowed
=> True);
1036 Resolve_Array_Aggregate
1038 Index
=> First_Index
(Aggr_Typ
),
1039 Index_Constr
=> First_Index
(Aggr_Typ
),
1040 Component_Typ
=> Component_Type
(Typ
),
1041 Others_Allowed
=> False);
1044 if not Aggr_Resolved
then
1045 Aggr_Subtyp
:= Any_Composite
;
1047 Aggr_Subtyp
:= Array_Aggr_Subtype
(N
, Typ
);
1050 Set_Etype
(N
, Aggr_Subtyp
);
1051 end Array_Aggregate
;
1053 elsif Is_Private_Type
(Typ
)
1054 and then Present
(Full_View
(Typ
))
1055 and then In_Inlined_Body
1056 and then Is_Composite_Type
(Full_View
(Typ
))
1058 Resolve
(N
, Full_View
(Typ
));
1061 Error_Msg_N
("illegal context for aggregate", N
);
1064 -- If we can determine statically that the evaluation of the
1065 -- aggregate raises Constraint_Error, then replace the
1066 -- aggregate with an N_Raise_Constraint_Error node, but set the
1067 -- Etype to the right aggregate subtype. Gigi needs this.
1069 if Raises_Constraint_Error
(N
) then
1070 Aggr_Subtyp
:= Etype
(N
);
1072 Make_Raise_Constraint_Error
(Sloc
(N
),
1073 Reason
=> CE_Range_Check_Failed
));
1074 Set_Raises_Constraint_Error
(N
);
1075 Set_Etype
(N
, Aggr_Subtyp
);
1078 end Resolve_Aggregate
;
1080 -----------------------------
1081 -- Resolve_Array_Aggregate --
1082 -----------------------------
1084 function Resolve_Array_Aggregate
1087 Index_Constr
: Node_Id
;
1088 Component_Typ
: Entity_Id
;
1089 Others_Allowed
: Boolean)
1092 Loc
: constant Source_Ptr
:= Sloc
(N
);
1094 Failure
: constant Boolean := False;
1095 Success
: constant Boolean := True;
1097 Index_Typ
: constant Entity_Id
:= Etype
(Index
);
1098 Index_Typ_Low
: constant Node_Id
:= Type_Low_Bound
(Index_Typ
);
1099 Index_Typ_High
: constant Node_Id
:= Type_High_Bound
(Index_Typ
);
1100 -- The type of the index corresponding to the array sub-aggregate
1101 -- along with its low and upper bounds
1103 Index_Base
: constant Entity_Id
:= Base_Type
(Index_Typ
);
1104 Index_Base_Low
: constant Node_Id
:= Type_Low_Bound
(Index_Base
);
1105 Index_Base_High
: constant Node_Id
:= Type_High_Bound
(Index_Base
);
1106 -- ditto for the base type
1108 function Add
(Val
: Uint
; To
: Node_Id
) return Node_Id
;
1109 -- Creates a new expression node where Val is added to expression To.
1110 -- Tries to constant fold whenever possible. To must be an already
1111 -- analyzed expression.
1113 procedure Check_Bound
(BH
: Node_Id
; AH
: in out Node_Id
);
1114 -- Checks that AH (the upper bound of an array aggregate) is <= BH
1115 -- (the upper bound of the index base type). If the check fails a
1116 -- warning is emitted, the Raises_Constraint_Error Flag of N is set,
1117 -- and AH is replaced with a duplicate of BH.
1119 procedure Check_Bounds
(L
, H
: Node_Id
; AL
, AH
: Node_Id
);
1120 -- Checks that range AL .. AH is compatible with range L .. H. Emits a
1121 -- warning if not and sets the Raises_Constraint_Error Flag in N.
1123 procedure Check_Length
(L
, H
: Node_Id
; Len
: Uint
);
1124 -- Checks that range L .. H contains at least Len elements. Emits a
1125 -- warning if not and sets the Raises_Constraint_Error Flag in N.
1127 function Dynamic_Or_Null_Range
(L
, H
: Node_Id
) return Boolean;
1128 -- Returns True if range L .. H is dynamic or null
1130 procedure Get
(Value
: out Uint
; From
: Node_Id
; OK
: out Boolean);
1131 -- Given expression node From, this routine sets OK to False if it
1132 -- cannot statically evaluate From. Otherwise it stores this static
1133 -- value into Value.
1135 function Resolve_Aggr_Expr
1137 Single_Elmt
: Boolean)
1139 -- Resolves aggregate expression Expr. Returs False if resolution
1140 -- fails. If Single_Elmt is set to False, the expression Expr may be
1141 -- used to initialize several array aggregate elements (this can
1142 -- happen for discrete choices such as "L .. H => Expr" or the others
1143 -- choice). In this event we do not resolve Expr unless expansion is
1144 -- disabled. To know why, see the DELAYED COMPONENT RESOLUTION
1151 function Add
(Val
: Uint
; To
: Node_Id
) return Node_Id
is
1157 if Raises_Constraint_Error
(To
) then
1161 -- First test if we can do constant folding
1163 if Compile_Time_Known_Value
(To
)
1164 or else Nkind
(To
) = N_Integer_Literal
1166 Expr_Pos
:= Make_Integer_Literal
(Loc
, Expr_Value
(To
) + Val
);
1167 Set_Is_Static_Expression
(Expr_Pos
);
1168 Set_Etype
(Expr_Pos
, Etype
(To
));
1169 Set_Analyzed
(Expr_Pos
, Analyzed
(To
));
1171 if not Is_Enumeration_Type
(Index_Typ
) then
1174 -- If we are dealing with enumeration return
1175 -- Index_Typ'Val (Expr_Pos)
1179 Make_Attribute_Reference
1181 Prefix
=> New_Reference_To
(Index_Typ
, Loc
),
1182 Attribute_Name
=> Name_Val
,
1183 Expressions
=> New_List
(Expr_Pos
));
1189 -- If we are here no constant folding possible
1191 if not Is_Enumeration_Type
(Index_Base
) then
1194 Left_Opnd
=> Duplicate_Subexpr
(To
),
1195 Right_Opnd
=> Make_Integer_Literal
(Loc
, Val
));
1197 -- If we are dealing with enumeration return
1198 -- Index_Typ'Val (Index_Typ'Pos (To) + Val)
1202 Make_Attribute_Reference
1204 Prefix
=> New_Reference_To
(Index_Typ
, Loc
),
1205 Attribute_Name
=> Name_Pos
,
1206 Expressions
=> New_List
(Duplicate_Subexpr
(To
)));
1210 Left_Opnd
=> To_Pos
,
1211 Right_Opnd
=> Make_Integer_Literal
(Loc
, Val
));
1214 Make_Attribute_Reference
1216 Prefix
=> New_Reference_To
(Index_Typ
, Loc
),
1217 Attribute_Name
=> Name_Val
,
1218 Expressions
=> New_List
(Expr_Pos
));
1228 procedure Check_Bound
(BH
: Node_Id
; AH
: in out Node_Id
) is
1236 Get
(Value
=> Val_BH
, From
=> BH
, OK
=> OK_BH
);
1237 Get
(Value
=> Val_AH
, From
=> AH
, OK
=> OK_AH
);
1239 if OK_BH
and then OK_AH
and then Val_BH
< Val_AH
then
1240 Set_Raises_Constraint_Error
(N
);
1241 Error_Msg_N
("upper bound out of range?", AH
);
1242 Error_Msg_N
("\Constraint_Error will be raised at run-time?", AH
);
1244 -- You need to set AH to BH or else in the case of enumerations
1245 -- indices we will not be able to resolve the aggregate bounds.
1247 AH
:= Duplicate_Subexpr
(BH
);
1255 procedure Check_Bounds
(L
, H
: Node_Id
; AL
, AH
: Node_Id
) is
1266 pragma Warnings
(Off
, OK_AL
);
1267 pragma Warnings
(Off
, OK_AH
);
1270 if Raises_Constraint_Error
(N
)
1271 or else Dynamic_Or_Null_Range
(AL
, AH
)
1276 Get
(Value
=> Val_L
, From
=> L
, OK
=> OK_L
);
1277 Get
(Value
=> Val_H
, From
=> H
, OK
=> OK_H
);
1279 Get
(Value
=> Val_AL
, From
=> AL
, OK
=> OK_AL
);
1280 Get
(Value
=> Val_AH
, From
=> AH
, OK
=> OK_AH
);
1282 if OK_L
and then Val_L
> Val_AL
then
1283 Set_Raises_Constraint_Error
(N
);
1284 Error_Msg_N
("lower bound of aggregate out of range?", N
);
1285 Error_Msg_N
("\Constraint_Error will be raised at run-time?", N
);
1288 if OK_H
and then Val_H
< Val_AH
then
1289 Set_Raises_Constraint_Error
(N
);
1290 Error_Msg_N
("upper bound of aggregate out of range?", N
);
1291 Error_Msg_N
("\Constraint_Error will be raised at run-time?", N
);
1299 procedure Check_Length
(L
, H
: Node_Id
; Len
: Uint
) is
1309 if Raises_Constraint_Error
(N
) then
1313 Get
(Value
=> Val_L
, From
=> L
, OK
=> OK_L
);
1314 Get
(Value
=> Val_H
, From
=> H
, OK
=> OK_H
);
1316 if not OK_L
or else not OK_H
then
1320 -- If null range length is zero
1322 if Val_L
> Val_H
then
1323 Range_Len
:= Uint_0
;
1325 Range_Len
:= Val_H
- Val_L
+ 1;
1328 if Range_Len
< Len
then
1329 Set_Raises_Constraint_Error
(N
);
1330 Error_Msg_N
("too many elements?", N
);
1331 Error_Msg_N
("\Constraint_Error will be raised at run-time?", N
);
1335 ---------------------------
1336 -- Dynamic_Or_Null_Range --
1337 ---------------------------
1339 function Dynamic_Or_Null_Range
(L
, H
: Node_Id
) return Boolean is
1347 Get
(Value
=> Val_L
, From
=> L
, OK
=> OK_L
);
1348 Get
(Value
=> Val_H
, From
=> H
, OK
=> OK_H
);
1350 return not OK_L
or else not OK_H
1351 or else not Is_OK_Static_Expression
(L
)
1352 or else not Is_OK_Static_Expression
(H
)
1353 or else Val_L
> Val_H
;
1354 end Dynamic_Or_Null_Range
;
1360 procedure Get
(Value
: out Uint
; From
: Node_Id
; OK
: out Boolean) is
1364 if Compile_Time_Known_Value
(From
) then
1365 Value
:= Expr_Value
(From
);
1367 -- If expression From is something like Some_Type'Val (10) then
1370 elsif Nkind
(From
) = N_Attribute_Reference
1371 and then Attribute_Name
(From
) = Name_Val
1372 and then Compile_Time_Known_Value
(First
(Expressions
(From
)))
1374 Value
:= Expr_Value
(First
(Expressions
(From
)));
1382 -----------------------
1383 -- Resolve_Aggr_Expr --
1384 -----------------------
1386 function Resolve_Aggr_Expr
1388 Single_Elmt
: Boolean)
1391 Nxt_Ind
: constant Node_Id
:= Next_Index
(Index
);
1392 Nxt_Ind_Constr
: constant Node_Id
:= Next_Index
(Index_Constr
);
1393 -- Index is the current index corresponding to the expresion
1395 Resolution_OK
: Boolean := True;
1396 -- Set to False if resolution of the expression failed
1399 -- If the array type against which we are resolving the aggregate
1400 -- has several dimensions, the expressions nested inside the
1401 -- aggregate must be further aggregates (or strings).
1403 if Present
(Nxt_Ind
) then
1404 if Nkind
(Expr
) /= N_Aggregate
then
1406 -- A string literal can appear where a one-dimensional array
1407 -- of characters is expected. If the literal looks like an
1408 -- operator, it is still an operator symbol, which will be
1409 -- transformed into a string when analyzed.
1411 if Is_Character_Type
(Component_Typ
)
1412 and then No
(Next_Index
(Nxt_Ind
))
1413 and then (Nkind
(Expr
) = N_String_Literal
1414 or else Nkind
(Expr
) = N_Operator_Symbol
)
1416 -- A string literal used in a multidimensional array
1417 -- aggregate in place of the final one-dimensional
1418 -- aggregate must not be enclosed in parentheses.
1420 if Paren_Count
(Expr
) /= 0 then
1421 Error_Msg_N
("no parenthesis allowed here", Expr
);
1424 Make_String_Into_Aggregate
(Expr
);
1427 Error_Msg_N
("nested array aggregate expected", Expr
);
1432 -- Ada 2005 (AI-231): Propagate the type to the nested aggregate.
1433 -- Required to check the null-exclusion attribute (if present).
1434 -- This value may be overridden later on.
1436 Set_Etype
(Expr
, Etype
(N
));
1438 Resolution_OK
:= Resolve_Array_Aggregate
1439 (Expr
, Nxt_Ind
, Nxt_Ind_Constr
, Component_Typ
, Others_Allowed
);
1441 -- Do not resolve the expressions of discrete or others choices
1442 -- unless the expression covers a single component, or the expander
1446 or else not Expander_Active
1447 or else In_Default_Expression
1449 Analyze_And_Resolve
(Expr
, Component_Typ
);
1450 Check_Non_Static_Context
(Expr
);
1451 Aggregate_Constraint_Checks
(Expr
, Component_Typ
);
1452 Check_Unset_Reference
(Expr
);
1455 if Raises_Constraint_Error
(Expr
)
1456 and then Nkind
(Parent
(Expr
)) /= N_Component_Association
1458 Set_Raises_Constraint_Error
(N
);
1461 return Resolution_OK
;
1462 end Resolve_Aggr_Expr
;
1464 -- Variables local to Resolve_Array_Aggregate
1471 pragma Warnings
(Off
, Discard
);
1473 Aggr_Low
: Node_Id
:= Empty
;
1474 Aggr_High
: Node_Id
:= Empty
;
1475 -- The actual low and high bounds of this sub-aggregate
1477 Choices_Low
: Node_Id
:= Empty
;
1478 Choices_High
: Node_Id
:= Empty
;
1479 -- The lowest and highest discrete choices values for a named aggregate
1481 Nb_Elements
: Uint
:= Uint_0
;
1482 -- The number of elements in a positional aggregate
1484 Others_Present
: Boolean := False;
1486 Nb_Choices
: Nat
:= 0;
1487 -- Contains the overall number of named choices in this sub-aggregate
1489 Nb_Discrete_Choices
: Nat
:= 0;
1490 -- The overall number of discrete choices (not counting others choice)
1492 Case_Table_Size
: Nat
;
1493 -- Contains the size of the case table needed to sort aggregate choices
1495 -- Start of processing for Resolve_Array_Aggregate
1498 -- STEP 1: make sure the aggregate is correctly formatted
1500 if Present
(Component_Associations
(N
)) then
1501 Assoc
:= First
(Component_Associations
(N
));
1502 while Present
(Assoc
) loop
1503 Choice
:= First
(Choices
(Assoc
));
1504 while Present
(Choice
) loop
1505 if Nkind
(Choice
) = N_Others_Choice
then
1506 Others_Present
:= True;
1508 if Choice
/= First
(Choices
(Assoc
))
1509 or else Present
(Next
(Choice
))
1512 ("OTHERS must appear alone in a choice list", Choice
);
1516 if Present
(Next
(Assoc
)) then
1518 ("OTHERS must appear last in an aggregate", Choice
);
1522 if Ada_Version
= Ada_83
1523 and then Assoc
/= First
(Component_Associations
(N
))
1524 and then (Nkind
(Parent
(N
)) = N_Assignment_Statement
1526 Nkind
(Parent
(N
)) = N_Object_Declaration
)
1529 ("(Ada 83) illegal context for OTHERS choice", N
);
1533 Nb_Choices
:= Nb_Choices
+ 1;
1541 -- At this point we know that the others choice, if present, is by
1542 -- itself and appears last in the aggregate. Check if we have mixed
1543 -- positional and discrete associations (other than the others choice).
1545 if Present
(Expressions
(N
))
1546 and then (Nb_Choices
> 1
1547 or else (Nb_Choices
= 1 and then not Others_Present
))
1550 ("named association cannot follow positional association",
1551 First
(Choices
(First
(Component_Associations
(N
)))));
1555 -- Test for the validity of an others choice if present
1557 if Others_Present
and then not Others_Allowed
then
1559 ("OTHERS choice not allowed here",
1560 First
(Choices
(First
(Component_Associations
(N
)))));
1564 -- Protect against cascaded errors
1566 if Etype
(Index_Typ
) = Any_Type
then
1570 -- STEP 2: Process named components
1572 if No
(Expressions
(N
)) then
1574 if Others_Present
then
1575 Case_Table_Size
:= Nb_Choices
- 1;
1577 Case_Table_Size
:= Nb_Choices
;
1583 -- Denote the lowest and highest values in an aggregate choice
1587 -- High end of one range and Low end of the next. Should be
1588 -- contiguous if there is no hole in the list of values.
1590 Missing_Values
: Boolean;
1591 -- Set True if missing index values
1593 S_Low
: Node_Id
:= Empty
;
1594 S_High
: Node_Id
:= Empty
;
1595 -- if a choice in an aggregate is a subtype indication these
1596 -- denote the lowest and highest values of the subtype
1598 Table
: Case_Table_Type
(1 .. Case_Table_Size
);
1599 -- Used to sort all the different choice values
1601 Single_Choice
: Boolean;
1602 -- Set to true every time there is a single discrete choice in a
1603 -- discrete association
1605 Prev_Nb_Discrete_Choices
: Nat
;
1606 -- Used to keep track of the number of discrete choices
1607 -- in the current association.
1610 -- STEP 2 (A): Check discrete choices validity
1612 Assoc
:= First
(Component_Associations
(N
));
1613 while Present
(Assoc
) loop
1614 Prev_Nb_Discrete_Choices
:= Nb_Discrete_Choices
;
1615 Choice
:= First
(Choices
(Assoc
));
1619 if Nkind
(Choice
) = N_Others_Choice
then
1620 Single_Choice
:= False;
1623 -- Test for subtype mark without constraint
1625 elsif Is_Entity_Name
(Choice
) and then
1626 Is_Type
(Entity
(Choice
))
1628 if Base_Type
(Entity
(Choice
)) /= Index_Base
then
1630 ("invalid subtype mark in aggregate choice",
1635 elsif Nkind
(Choice
) = N_Subtype_Indication
then
1636 Resolve_Discrete_Subtype_Indication
(Choice
, Index_Base
);
1638 -- Does the subtype indication evaluation raise CE ?
1640 Get_Index_Bounds
(Subtype_Mark
(Choice
), S_Low
, S_High
);
1641 Get_Index_Bounds
(Choice
, Low
, High
);
1642 Check_Bounds
(S_Low
, S_High
, Low
, High
);
1644 else -- Choice is a range or an expression
1645 Resolve
(Choice
, Index_Base
);
1646 Check_Unset_Reference
(Choice
);
1647 Check_Non_Static_Context
(Choice
);
1649 -- Do not range check a choice. This check is redundant
1650 -- since this test is already performed when we check
1651 -- that the bounds of the array aggregate are within
1654 Set_Do_Range_Check
(Choice
, False);
1657 -- If we could not resolve the discrete choice stop here
1659 if Etype
(Choice
) = Any_Type
then
1662 -- If the discrete choice raises CE get its original bounds
1664 elsif Nkind
(Choice
) = N_Raise_Constraint_Error
then
1665 Set_Raises_Constraint_Error
(N
);
1666 Get_Index_Bounds
(Original_Node
(Choice
), Low
, High
);
1668 -- Otherwise get its bounds as usual
1671 Get_Index_Bounds
(Choice
, Low
, High
);
1674 if (Dynamic_Or_Null_Range
(Low
, High
)
1675 or else (Nkind
(Choice
) = N_Subtype_Indication
1677 Dynamic_Or_Null_Range
(S_Low
, S_High
)))
1678 and then Nb_Choices
/= 1
1681 ("dynamic or empty choice in aggregate " &
1682 "must be the only choice", Choice
);
1686 Nb_Discrete_Choices
:= Nb_Discrete_Choices
+ 1;
1687 Table
(Nb_Discrete_Choices
).Choice_Lo
:= Low
;
1688 Table
(Nb_Discrete_Choices
).Choice_Hi
:= High
;
1694 -- Check if we have a single discrete choice and whether
1695 -- this discrete choice specifies a single value.
1698 (Nb_Discrete_Choices
= Prev_Nb_Discrete_Choices
+ 1)
1699 and then (Low
= High
);
1705 -- Ada 2005 (AI-231)
1707 if Ada_Version
>= Ada_05
1708 and then Known_Null
(Expression
(Assoc
))
1710 Check_Can_Never_Be_Null
(Etype
(N
), Expression
(Assoc
));
1713 -- Ada 2005 (AI-287): In case of default initialized component
1714 -- we delay the resolution to the expansion phase
1716 if Box_Present
(Assoc
) then
1718 -- Ada 2005 (AI-287): In case of default initialization
1719 -- of a component the expander will generate calls to
1720 -- the corresponding initialization subprogram.
1724 elsif not Resolve_Aggr_Expr
(Expression
(Assoc
),
1725 Single_Elmt
=> Single_Choice
)
1733 -- If aggregate contains more than one choice then these must be
1734 -- static. Sort them and check that they are contiguous
1736 if Nb_Discrete_Choices
> 1 then
1737 Sort_Case_Table
(Table
);
1738 Missing_Values
:= False;
1740 Outer
: for J
in 1 .. Nb_Discrete_Choices
- 1 loop
1741 if Expr_Value
(Table
(J
).Choice_Hi
) >=
1742 Expr_Value
(Table
(J
+ 1).Choice_Lo
)
1745 ("duplicate choice values in array aggregate",
1746 Table
(J
).Choice_Hi
);
1749 elsif not Others_Present
then
1751 Hi_Val
:= Expr_Value
(Table
(J
).Choice_Hi
);
1752 Lo_Val
:= Expr_Value
(Table
(J
+ 1).Choice_Lo
);
1754 -- If missing values, output error messages
1756 if Lo_Val
- Hi_Val
> 1 then
1758 -- Header message if not first missing value
1760 if not Missing_Values
then
1762 ("missing index value(s) in array aggregate", N
);
1763 Missing_Values
:= True;
1766 -- Output values of missing indexes
1768 Lo_Val
:= Lo_Val
- 1;
1769 Hi_Val
:= Hi_Val
+ 1;
1771 -- Enumeration type case
1773 if Is_Enumeration_Type
(Index_Typ
) then
1776 (Get_Enum_Lit_From_Pos
1777 (Index_Typ
, Hi_Val
, Loc
));
1779 if Lo_Val
= Hi_Val
then
1780 Error_Msg_N
("\ %", N
);
1784 (Get_Enum_Lit_From_Pos
1785 (Index_Typ
, Lo_Val
, Loc
));
1786 Error_Msg_N
("\ % .. %", N
);
1789 -- Integer types case
1792 Error_Msg_Uint_1
:= Hi_Val
;
1794 if Lo_Val
= Hi_Val
then
1795 Error_Msg_N
("\ ^", N
);
1797 Error_Msg_Uint_2
:= Lo_Val
;
1798 Error_Msg_N
("\ ^ .. ^", N
);
1805 if Missing_Values
then
1806 Set_Etype
(N
, Any_Composite
);
1811 -- STEP 2 (B): Compute aggregate bounds and min/max choices values
1813 if Nb_Discrete_Choices
> 0 then
1814 Choices_Low
:= Table
(1).Choice_Lo
;
1815 Choices_High
:= Table
(Nb_Discrete_Choices
).Choice_Hi
;
1818 if Others_Present
then
1819 Get_Index_Bounds
(Index_Constr
, Aggr_Low
, Aggr_High
);
1822 Aggr_Low
:= Choices_Low
;
1823 Aggr_High
:= Choices_High
;
1827 -- STEP 3: Process positional components
1830 -- STEP 3 (A): Process positional elements
1832 Expr
:= First
(Expressions
(N
));
1833 Nb_Elements
:= Uint_0
;
1834 while Present
(Expr
) loop
1835 Nb_Elements
:= Nb_Elements
+ 1;
1837 -- Ada 2005 (AI-231)
1839 if Ada_Version
>= Ada_05
1840 and then Known_Null
(Expr
)
1842 Check_Can_Never_Be_Null
(Etype
(N
), Expr
);
1845 if not Resolve_Aggr_Expr
(Expr
, Single_Elmt
=> True) then
1852 if Others_Present
then
1853 Assoc
:= Last
(Component_Associations
(N
));
1855 -- Ada 2005 (AI-231)
1857 if Ada_Version
>= Ada_05
1858 and then Known_Null
(Assoc
)
1860 Check_Can_Never_Be_Null
(Etype
(N
), Expression
(Assoc
));
1863 -- Ada 2005 (AI-287): In case of default initialized component
1864 -- we delay the resolution to the expansion phase.
1866 if Box_Present
(Assoc
) then
1868 -- Ada 2005 (AI-287): In case of default initialization
1869 -- of a component the expander will generate calls to
1870 -- the corresponding initialization subprogram.
1874 elsif not Resolve_Aggr_Expr
(Expression
(Assoc
),
1875 Single_Elmt
=> False)
1881 -- STEP 3 (B): Compute the aggregate bounds
1883 if Others_Present
then
1884 Get_Index_Bounds
(Index_Constr
, Aggr_Low
, Aggr_High
);
1887 if Others_Allowed
then
1888 Get_Index_Bounds
(Index_Constr
, Aggr_Low
, Discard
);
1890 Aggr_Low
:= Index_Typ_Low
;
1893 Aggr_High
:= Add
(Nb_Elements
- 1, To
=> Aggr_Low
);
1894 Check_Bound
(Index_Base_High
, Aggr_High
);
1898 -- STEP 4: Perform static aggregate checks and save the bounds
1902 Check_Bounds
(Index_Typ_Low
, Index_Typ_High
, Aggr_Low
, Aggr_High
);
1903 Check_Bounds
(Index_Base_Low
, Index_Base_High
, Aggr_Low
, Aggr_High
);
1907 if Others_Present
and then Nb_Discrete_Choices
> 0 then
1908 Check_Bounds
(Aggr_Low
, Aggr_High
, Choices_Low
, Choices_High
);
1909 Check_Bounds
(Index_Typ_Low
, Index_Typ_High
,
1910 Choices_Low
, Choices_High
);
1911 Check_Bounds
(Index_Base_Low
, Index_Base_High
,
1912 Choices_Low
, Choices_High
);
1916 elsif Others_Present
and then Nb_Elements
> 0 then
1917 Check_Length
(Aggr_Low
, Aggr_High
, Nb_Elements
);
1918 Check_Length
(Index_Typ_Low
, Index_Typ_High
, Nb_Elements
);
1919 Check_Length
(Index_Base_Low
, Index_Base_High
, Nb_Elements
);
1922 if Raises_Constraint_Error
(Aggr_Low
)
1923 or else Raises_Constraint_Error
(Aggr_High
)
1925 Set_Raises_Constraint_Error
(N
);
1928 Aggr_Low
:= Duplicate_Subexpr
(Aggr_Low
);
1930 -- Do not duplicate Aggr_High if Aggr_High = Aggr_Low + Nb_Elements
1931 -- since the addition node returned by Add is not yet analyzed. Attach
1932 -- to tree and analyze first. Reset analyzed flag to insure it will get
1933 -- analyzed when it is a literal bound whose type must be properly set.
1935 if Others_Present
or else Nb_Discrete_Choices
> 0 then
1936 Aggr_High
:= Duplicate_Subexpr
(Aggr_High
);
1938 if Etype
(Aggr_High
) = Universal_Integer
then
1939 Set_Analyzed
(Aggr_High
, False);
1943 Set_Aggregate_Bounds
1944 (N
, Make_Range
(Loc
, Low_Bound
=> Aggr_Low
, High_Bound
=> Aggr_High
));
1946 -- The bounds may contain expressions that must be inserted upwards.
1947 -- Attach them fully to the tree. After analysis, remove side effects
1948 -- from upper bound, if still needed.
1950 Set_Parent
(Aggregate_Bounds
(N
), N
);
1951 Analyze_And_Resolve
(Aggregate_Bounds
(N
), Index_Typ
);
1952 Check_Unset_Reference
(Aggregate_Bounds
(N
));
1954 if not Others_Present
and then Nb_Discrete_Choices
= 0 then
1955 Set_High_Bound
(Aggregate_Bounds
(N
),
1956 Duplicate_Subexpr
(High_Bound
(Aggregate_Bounds
(N
))));
1960 end Resolve_Array_Aggregate
;
1962 ---------------------------------
1963 -- Resolve_Extension_Aggregate --
1964 ---------------------------------
1966 -- There are two cases to consider:
1968 -- a) If the ancestor part is a type mark, the components needed are
1969 -- the difference between the components of the expected type and the
1970 -- components of the given type mark.
1972 -- b) If the ancestor part is an expression, it must be unambiguous,
1973 -- and once we have its type we can also compute the needed components
1974 -- as in the previous case. In both cases, if the ancestor type is not
1975 -- the immediate ancestor, we have to build this ancestor recursively.
1977 -- In both cases discriminants of the ancestor type do not play a
1978 -- role in the resolution of the needed components, because inherited
1979 -- discriminants cannot be used in a type extension. As a result we can
1980 -- compute independently the list of components of the ancestor type and
1981 -- of the expected type.
1983 procedure Resolve_Extension_Aggregate
(N
: Node_Id
; Typ
: Entity_Id
) is
1984 A
: constant Node_Id
:= Ancestor_Part
(N
);
1989 function Valid_Ancestor_Type
return Boolean;
1990 -- Verify that the type of the ancestor part is a non-private ancestor
1991 -- of the expected type.
1993 -------------------------
1994 -- Valid_Ancestor_Type --
1995 -------------------------
1997 function Valid_Ancestor_Type
return Boolean is
1998 Imm_Type
: Entity_Id
;
2001 Imm_Type
:= Base_Type
(Typ
);
2002 while Is_Derived_Type
(Imm_Type
)
2003 and then Etype
(Imm_Type
) /= Base_Type
(A_Type
)
2005 Imm_Type
:= Etype
(Base_Type
(Imm_Type
));
2008 if Etype
(Imm_Type
) /= Base_Type
(A_Type
) then
2009 Error_Msg_NE
("expect ancestor type of &", A
, Typ
);
2014 end Valid_Ancestor_Type
;
2016 -- Start of processing for Resolve_Extension_Aggregate
2021 if not Is_Tagged_Type
(Typ
) then
2022 Error_Msg_N
("type of extension aggregate must be tagged", N
);
2025 elsif Is_Limited_Type
(Typ
) then
2027 -- Ada 2005 (AI-287): Limited aggregates are allowed
2029 if Ada_Version
< Ada_05
then
2030 Error_Msg_N
("aggregate type cannot be limited", N
);
2031 Explain_Limited_Type
(Typ
, N
);
2035 elsif Is_Class_Wide_Type
(Typ
) then
2036 Error_Msg_N
("aggregate cannot be of a class-wide type", N
);
2040 if Is_Entity_Name
(A
)
2041 and then Is_Type
(Entity
(A
))
2043 A_Type
:= Get_Full_View
(Entity
(A
));
2045 if Valid_Ancestor_Type
then
2046 Set_Entity
(A
, A_Type
);
2047 Set_Etype
(A
, A_Type
);
2049 Validate_Ancestor_Part
(N
);
2050 Resolve_Record_Aggregate
(N
, Typ
);
2053 elsif Nkind
(A
) /= N_Aggregate
then
2054 if Is_Overloaded
(A
) then
2057 Get_First_Interp
(A
, I
, It
);
2058 while Present
(It
.Typ
) loop
2059 if Is_Tagged_Type
(It
.Typ
)
2060 and then not Is_Limited_Type
(It
.Typ
)
2062 if A_Type
/= Any_Type
then
2063 Error_Msg_N
("cannot resolve expression", A
);
2070 Get_Next_Interp
(I
, It
);
2073 if A_Type
= Any_Type
then
2075 ("ancestor part must be non-limited tagged type", A
);
2080 A_Type
:= Etype
(A
);
2083 if Valid_Ancestor_Type
then
2084 Resolve
(A
, A_Type
);
2085 Check_Unset_Reference
(A
);
2086 Check_Non_Static_Context
(A
);
2088 if Is_Class_Wide_Type
(Etype
(A
))
2089 and then Nkind
(Original_Node
(A
)) = N_Function_Call
2091 -- If the ancestor part is a dispatching call, it appears
2092 -- statically to be a legal ancestor, but it yields any
2093 -- member of the class, and it is not possible to determine
2094 -- whether it is an ancestor of the extension aggregate (much
2095 -- less which ancestor). It is not possible to determine the
2096 -- required components of the extension part.
2098 -- This check implements AI-306, which in fact was motivated
2099 -- by an ACT query to the ARG after this test was added.
2101 Error_Msg_N
("ancestor part must be statically tagged", A
);
2103 Resolve_Record_Aggregate
(N
, Typ
);
2108 Error_Msg_N
("no unique type for this aggregate", A
);
2110 end Resolve_Extension_Aggregate
;
2112 ------------------------------
2113 -- Resolve_Record_Aggregate --
2114 ------------------------------
2116 procedure Resolve_Record_Aggregate
(N
: Node_Id
; Typ
: Entity_Id
) is
2118 -- N_Component_Association node belonging to the input aggregate N
2121 Positional_Expr
: Node_Id
;
2122 Component
: Entity_Id
;
2123 Component_Elmt
: Elmt_Id
;
2125 Components
: constant Elist_Id
:= New_Elmt_List
;
2126 -- Components is the list of the record components whose value must
2127 -- be provided in the aggregate. This list does include discriminants.
2129 New_Assoc_List
: constant List_Id
:= New_List
;
2130 New_Assoc
: Node_Id
;
2131 -- New_Assoc_List is the newly built list of N_Component_Association
2132 -- nodes. New_Assoc is one such N_Component_Association node in it.
2133 -- Please note that while Assoc and New_Assoc contain the same
2134 -- kind of nodes, they are used to iterate over two different
2135 -- N_Component_Association lists.
2137 Others_Etype
: Entity_Id
:= Empty
;
2138 -- This variable is used to save the Etype of the last record component
2139 -- that takes its value from the others choice. Its purpose is:
2141 -- (a) make sure the others choice is useful
2143 -- (b) make sure the type of all the components whose value is
2144 -- subsumed by the others choice are the same.
2146 -- This variable is updated as a side effect of function Get_Value
2148 Is_Box_Present
: Boolean := False;
2149 Others_Box
: Boolean := False;
2150 -- Ada 2005 (AI-287): Variables used in case of default initialization
2151 -- to provide a functionality similar to Others_Etype. Box_Present
2152 -- indicates that the component takes its default initialization;
2153 -- Others_Box indicates that at least one component takes its default
2154 -- initialization. Similar to Others_Etype, they are also updated as a
2155 -- side effect of function Get_Value.
2157 procedure Add_Association
2158 (Component
: Entity_Id
;
2160 Is_Box_Present
: Boolean := False);
2161 -- Builds a new N_Component_Association node which associates
2162 -- Component to expression Expr and adds it to the new association
2163 -- list New_Assoc_List being built.
2165 function Discr_Present
(Discr
: Entity_Id
) return Boolean;
2166 -- If aggregate N is a regular aggregate this routine will return True.
2167 -- Otherwise, if N is an extension aggregate, Discr is a discriminant
2168 -- whose value may already have been specified by N's ancestor part,
2169 -- this routine checks whether this is indeed the case and if so
2170 -- returns False, signaling that no value for Discr should appear in the
2171 -- N's aggregate part. Also, in this case, the routine appends to
2172 -- New_Assoc_List Discr the discriminant value specified in the ancestor
2178 Consider_Others_Choice
: Boolean := False)
2180 -- Given a record component stored in parameter Compon, the
2181 -- following function returns its value as it appears in the list
2182 -- From, which is a list of N_Component_Association nodes. If no
2183 -- component association has a choice for the searched component,
2184 -- the value provided by the others choice is returned, if there
2185 -- is one and Consider_Others_Choice is set to true. Otherwise
2186 -- Empty is returned. If there is more than one component association
2187 -- giving a value for the searched record component, an error message
2188 -- is emitted and the first found value is returned.
2190 -- If Consider_Others_Choice is set and the returned expression comes
2191 -- from the others choice, then Others_Etype is set as a side effect.
2192 -- An error message is emitted if the components taking their value
2193 -- from the others choice do not have same type.
2195 procedure Resolve_Aggr_Expr
(Expr
: Node_Id
; Component
: Node_Id
);
2196 -- Analyzes and resolves expression Expr against the Etype of the
2197 -- Component. This routine also applies all appropriate checks to Expr.
2198 -- It finally saves a Expr in the newly created association list that
2199 -- will be attached to the final record aggregate. Note that if the
2200 -- Parent pointer of Expr is not set then Expr was produced with a
2201 -- New_Copy_Tree or some such.
2203 ---------------------
2204 -- Add_Association --
2205 ---------------------
2207 procedure Add_Association
2208 (Component
: Entity_Id
;
2210 Is_Box_Present
: Boolean := False)
2212 Choice_List
: constant List_Id
:= New_List
;
2213 New_Assoc
: Node_Id
;
2216 Append
(New_Occurrence_Of
(Component
, Sloc
(Expr
)), Choice_List
);
2218 Make_Component_Association
(Sloc
(Expr
),
2219 Choices
=> Choice_List
,
2221 Box_Present
=> Is_Box_Present
);
2222 Append
(New_Assoc
, New_Assoc_List
);
2223 end Add_Association
;
2229 function Discr_Present
(Discr
: Entity_Id
) return Boolean is
2230 Regular_Aggr
: constant Boolean := Nkind
(N
) /= N_Extension_Aggregate
;
2235 Discr_Expr
: Node_Id
;
2237 Ancestor_Typ
: Entity_Id
;
2238 Orig_Discr
: Entity_Id
;
2240 D_Val
: Elmt_Id
:= No_Elmt
; -- stop junk warning
2242 Ancestor_Is_Subtyp
: Boolean;
2245 if Regular_Aggr
then
2249 Ancestor
:= Ancestor_Part
(N
);
2250 Ancestor_Typ
:= Etype
(Ancestor
);
2251 Loc
:= Sloc
(Ancestor
);
2253 Ancestor_Is_Subtyp
:=
2254 Is_Entity_Name
(Ancestor
) and then Is_Type
(Entity
(Ancestor
));
2256 -- If the ancestor part has no discriminants clearly N's aggregate
2257 -- part must provide a value for Discr.
2259 if not Has_Discriminants
(Ancestor_Typ
) then
2262 -- If the ancestor part is an unconstrained subtype mark then the
2263 -- Discr must be present in N's aggregate part.
2265 elsif Ancestor_Is_Subtyp
2266 and then not Is_Constrained
(Entity
(Ancestor
))
2271 -- Now look to see if Discr was specified in the ancestor part
2273 if Ancestor_Is_Subtyp
then
2274 D_Val
:= First_Elmt
(Discriminant_Constraint
(Entity
(Ancestor
)));
2277 Orig_Discr
:= Original_Record_Component
(Discr
);
2279 D
:= First_Discriminant
(Ancestor_Typ
);
2280 while Present
(D
) loop
2282 -- If Ancestor has already specified Disc value than insert its
2283 -- value in the final aggregate.
2285 if Original_Record_Component
(D
) = Orig_Discr
then
2286 if Ancestor_Is_Subtyp
then
2287 Discr_Expr
:= New_Copy_Tree
(Node
(D_Val
));
2290 Make_Selected_Component
(Loc
,
2291 Prefix
=> Duplicate_Subexpr
(Ancestor
),
2292 Selector_Name
=> New_Occurrence_Of
(Discr
, Loc
));
2295 Resolve_Aggr_Expr
(Discr_Expr
, Discr
);
2299 Next_Discriminant
(D
);
2301 if Ancestor_Is_Subtyp
then
2316 Consider_Others_Choice
: Boolean := False)
2320 Expr
: Node_Id
:= Empty
;
2321 Selector_Name
: Node_Id
;
2324 Is_Box_Present
:= False;
2326 if Present
(From
) then
2327 Assoc
:= First
(From
);
2332 while Present
(Assoc
) loop
2333 Selector_Name
:= First
(Choices
(Assoc
));
2334 while Present
(Selector_Name
) loop
2335 if Nkind
(Selector_Name
) = N_Others_Choice
then
2336 if Consider_Others_Choice
and then No
(Expr
) then
2338 -- We need to duplicate the expression for each
2339 -- successive component covered by the others choice.
2340 -- This is redundant if the others_choice covers only
2341 -- one component (small optimization possible???), but
2342 -- indispensable otherwise, because each one must be
2343 -- expanded individually to preserve side-effects.
2345 -- Ada 2005 (AI-287): In case of default initialization
2346 -- of components, we duplicate the corresponding default
2347 -- expression (from the record type declaration). The
2348 -- copy must carry the sloc of the association (not the
2349 -- original expression) to prevent spurious elaboration
2350 -- checks when the default includes function calls.
2352 if Box_Present
(Assoc
) then
2354 Is_Box_Present
:= True;
2356 if Expander_Active
then
2359 (Expression
(Parent
(Compon
)),
2360 New_Sloc
=> Sloc
(Assoc
));
2362 return Expression
(Parent
(Compon
));
2366 if Present
(Others_Etype
) and then
2367 Base_Type
(Others_Etype
) /= Base_Type
(Etype
2370 Error_Msg_N
("components in OTHERS choice must " &
2371 "have same type", Selector_Name
);
2374 Others_Etype
:= Etype
(Compon
);
2376 if Expander_Active
then
2377 return New_Copy_Tree
(Expression
(Assoc
));
2379 return Expression
(Assoc
);
2384 elsif Chars
(Compon
) = Chars
(Selector_Name
) then
2387 -- Ada 2005 (AI-231)
2389 if Ada_Version
>= Ada_05
2390 and then Known_Null
(Expression
(Assoc
))
2392 Check_Can_Never_Be_Null
(Compon
, Expression
(Assoc
));
2395 -- We need to duplicate the expression when several
2396 -- components are grouped together with a "|" choice.
2397 -- For instance "filed1 | filed2 => Expr"
2399 -- Ada 2005 (AI-287)
2401 if Box_Present
(Assoc
) then
2402 Is_Box_Present
:= True;
2404 -- Duplicate the default expression of the component
2405 -- from the record type declaration, so a new copy
2406 -- can be attached to the association.
2408 -- Note that we always copy the default expression,
2409 -- even when the association has a single choice, in
2410 -- order to create a proper association for the
2411 -- expanded aggregate.
2413 Expr
:= New_Copy_Tree
(Expression
(Parent
(Compon
)));
2416 if Present
(Next
(Selector_Name
)) then
2417 Expr
:= New_Copy_Tree
(Expression
(Assoc
));
2419 Expr
:= Expression
(Assoc
);
2423 Generate_Reference
(Compon
, Selector_Name
);
2427 ("more than one value supplied for &",
2428 Selector_Name
, Compon
);
2433 Next
(Selector_Name
);
2442 procedure Check_Non_Limited_Type
(Expr
: Node_Id
);
2443 -- Relax check to allow the default initialization of limited types.
2446 -- C : Lim := (..., others => <>);
2449 ----------------------------
2450 -- Check_Non_Limited_Type --
2451 ----------------------------
2453 procedure Check_Non_Limited_Type
(Expr
: Node_Id
) is
2455 if Is_Limited_Type
(Etype
(Expr
))
2456 and then Comes_From_Source
(Expr
)
2457 and then not In_Instance_Body
2459 if not OK_For_Limited_Init
(Expr
) then
2461 ("initialization not allowed for limited types", N
);
2462 Explain_Limited_Type
(Etype
(Expr
), Expr
);
2465 end Check_Non_Limited_Type
;
2467 -----------------------
2468 -- Resolve_Aggr_Expr --
2469 -----------------------
2471 procedure Resolve_Aggr_Expr
(Expr
: Node_Id
; Component
: Node_Id
) is
2472 New_C
: Entity_Id
:= Component
;
2473 Expr_Type
: Entity_Id
:= Empty
;
2475 function Has_Expansion_Delayed
(Expr
: Node_Id
) return Boolean;
2476 -- If the expression is an aggregate (possibly qualified) then its
2477 -- expansion is delayed until the enclosing aggregate is expanded
2478 -- into assignments. In that case, do not generate checks on the
2479 -- expression, because they will be generated later, and will other-
2480 -- wise force a copy (to remove side-effects) that would leave a
2481 -- dynamic-sized aggregate in the code, something that gigi cannot
2485 -- Set to True if the resolved Expr node needs to be relocated
2486 -- when attached to the newly created association list. This node
2487 -- need not be relocated if its parent pointer is not set.
2488 -- In fact in this case Expr is the output of a New_Copy_Tree call.
2489 -- if Relocate is True then we have analyzed the expression node
2490 -- in the original aggregate and hence it needs to be relocated
2491 -- when moved over the new association list.
2493 function Has_Expansion_Delayed
(Expr
: Node_Id
) return Boolean is
2494 Kind
: constant Node_Kind
:= Nkind
(Expr
);
2497 return ((Kind
= N_Aggregate
2498 or else Kind
= N_Extension_Aggregate
)
2499 and then Present
(Etype
(Expr
))
2500 and then Is_Record_Type
(Etype
(Expr
))
2501 and then Expansion_Delayed
(Expr
))
2503 or else (Kind
= N_Qualified_Expression
2504 and then Has_Expansion_Delayed
(Expression
(Expr
)));
2505 end Has_Expansion_Delayed
;
2507 -- Start of processing for Resolve_Aggr_Expr
2510 -- If the type of the component is elementary or the type of the
2511 -- aggregate does not contain discriminants, use the type of the
2512 -- component to resolve Expr.
2514 if Is_Elementary_Type
(Etype
(Component
))
2515 or else not Has_Discriminants
(Etype
(N
))
2517 Expr_Type
:= Etype
(Component
);
2519 -- Otherwise we have to pick up the new type of the component from
2520 -- the new costrained subtype of the aggregate. In fact components
2521 -- which are of a composite type might be constrained by a
2522 -- discriminant, and we want to resolve Expr against the subtype were
2523 -- all discriminant occurrences are replaced with their actual value.
2526 New_C
:= First_Component
(Etype
(N
));
2527 while Present
(New_C
) loop
2528 if Chars
(New_C
) = Chars
(Component
) then
2529 Expr_Type
:= Etype
(New_C
);
2533 Next_Component
(New_C
);
2536 pragma Assert
(Present
(Expr_Type
));
2538 -- For each range in an array type where a discriminant has been
2539 -- replaced with the constraint, check that this range is within
2540 -- the range of the base type. This checks is done in the init
2541 -- proc for regular objects, but has to be done here for
2542 -- aggregates since no init proc is called for them.
2544 if Is_Array_Type
(Expr_Type
) then
2547 -- Range of the current constrained index in the array
2549 Orig_Index
: Node_Id
:= First_Index
(Etype
(Component
));
2550 -- Range corresponding to the range Index above in the
2551 -- original unconstrained record type. The bounds of this
2552 -- range may be governed by discriminants.
2554 Unconstr_Index
: Node_Id
:= First_Index
(Etype
(Expr_Type
));
2555 -- Range corresponding to the range Index above for the
2556 -- unconstrained array type. This range is needed to apply
2560 Index
:= First_Index
(Expr_Type
);
2561 while Present
(Index
) loop
2562 if Depends_On_Discriminant
(Orig_Index
) then
2563 Apply_Range_Check
(Index
, Etype
(Unconstr_Index
));
2567 Next_Index
(Orig_Index
);
2568 Next_Index
(Unconstr_Index
);
2574 -- If the Parent pointer of Expr is not set, Expr is an expression
2575 -- duplicated by New_Tree_Copy (this happens for record aggregates
2576 -- that look like (Field1 | Filed2 => Expr) or (others => Expr)).
2577 -- Such a duplicated expression must be attached to the tree
2578 -- before analysis and resolution to enforce the rule that a tree
2579 -- fragment should never be analyzed or resolved unless it is
2580 -- attached to the current compilation unit.
2582 if No
(Parent
(Expr
)) then
2583 Set_Parent
(Expr
, N
);
2589 Analyze_And_Resolve
(Expr
, Expr_Type
);
2590 Check_Non_Limited_Type
(Expr
);
2591 Check_Non_Static_Context
(Expr
);
2592 Check_Unset_Reference
(Expr
);
2594 if not Has_Expansion_Delayed
(Expr
) then
2595 Aggregate_Constraint_Checks
(Expr
, Expr_Type
);
2598 if Raises_Constraint_Error
(Expr
) then
2599 Set_Raises_Constraint_Error
(N
);
2603 Add_Association
(New_C
, Relocate_Node
(Expr
));
2605 Add_Association
(New_C
, Expr
);
2607 end Resolve_Aggr_Expr
;
2609 -- Start of processing for Resolve_Record_Aggregate
2612 -- We may end up calling Duplicate_Subexpr on expressions that are
2613 -- attached to New_Assoc_List. For this reason we need to attach it
2614 -- to the tree by setting its parent pointer to N. This parent point
2615 -- will change in STEP 8 below.
2617 Set_Parent
(New_Assoc_List
, N
);
2619 -- STEP 1: abstract type and null record verification
2621 if Is_Abstract_Type
(Typ
) then
2622 Error_Msg_N
("type of aggregate cannot be abstract", N
);
2625 if No
(First_Entity
(Typ
)) and then Null_Record_Present
(N
) then
2629 elsif Present
(First_Entity
(Typ
))
2630 and then Null_Record_Present
(N
)
2631 and then not Is_Tagged_Type
(Typ
)
2633 Error_Msg_N
("record aggregate cannot be null", N
);
2636 elsif No
(First_Entity
(Typ
)) then
2637 Error_Msg_N
("record aggregate must be null", N
);
2641 -- STEP 2: Verify aggregate structure
2644 Selector_Name
: Node_Id
;
2645 Bad_Aggregate
: Boolean := False;
2648 if Present
(Component_Associations
(N
)) then
2649 Assoc
:= First
(Component_Associations
(N
));
2654 while Present
(Assoc
) loop
2655 Selector_Name
:= First
(Choices
(Assoc
));
2656 while Present
(Selector_Name
) loop
2657 if Nkind
(Selector_Name
) = N_Identifier
then
2660 elsif Nkind
(Selector_Name
) = N_Others_Choice
then
2661 if Selector_Name
/= First
(Choices
(Assoc
))
2662 or else Present
(Next
(Selector_Name
))
2664 Error_Msg_N
("OTHERS must appear alone in a choice list",
2668 elsif Present
(Next
(Assoc
)) then
2669 Error_Msg_N
("OTHERS must appear last in an aggregate",
2673 -- (Ada2005): If this is an association with a box,
2674 -- indicate that the association need not represent
2677 elsif Box_Present
(Assoc
) then
2683 ("selector name should be identifier or OTHERS",
2685 Bad_Aggregate
:= True;
2688 Next
(Selector_Name
);
2694 if Bad_Aggregate
then
2699 -- STEP 3: Find discriminant Values
2702 Discrim
: Entity_Id
;
2703 Missing_Discriminants
: Boolean := False;
2706 if Present
(Expressions
(N
)) then
2707 Positional_Expr
:= First
(Expressions
(N
));
2709 Positional_Expr
:= Empty
;
2712 if Has_Discriminants
(Typ
) then
2713 Discrim
:= First_Discriminant
(Typ
);
2718 -- First find the discriminant values in the positional components
2720 while Present
(Discrim
) and then Present
(Positional_Expr
) loop
2721 if Discr_Present
(Discrim
) then
2722 Resolve_Aggr_Expr
(Positional_Expr
, Discrim
);
2724 -- Ada 2005 (AI-231)
2726 if Ada_Version
>= Ada_05
2727 and then Known_Null
(Positional_Expr
)
2729 Check_Can_Never_Be_Null
(Discrim
, Positional_Expr
);
2732 Next
(Positional_Expr
);
2735 if Present
(Get_Value
(Discrim
, Component_Associations
(N
))) then
2737 ("more than one value supplied for discriminant&",
2741 Next_Discriminant
(Discrim
);
2744 -- Find remaining discriminant values, if any, among named components
2746 while Present
(Discrim
) loop
2747 Expr
:= Get_Value
(Discrim
, Component_Associations
(N
), True);
2749 if not Discr_Present
(Discrim
) then
2750 if Present
(Expr
) then
2752 ("more than one value supplied for discriminant&",
2756 elsif No
(Expr
) then
2758 ("no value supplied for discriminant &", N
, Discrim
);
2759 Missing_Discriminants
:= True;
2762 Resolve_Aggr_Expr
(Expr
, Discrim
);
2765 Next_Discriminant
(Discrim
);
2768 if Missing_Discriminants
then
2772 -- At this point and until the beginning of STEP 6, New_Assoc_List
2773 -- contains only the discriminants and their values.
2777 -- STEP 4: Set the Etype of the record aggregate
2779 -- ??? This code is pretty much a copy of Sem_Ch3.Build_Subtype. That
2780 -- routine should really be exported in sem_util or some such and used
2781 -- in sem_ch3 and here rather than have a copy of the code which is a
2782 -- maintenance nightmare.
2784 -- ??? Performace WARNING. The current implementation creates a new
2785 -- itype for all aggregates whose base type is discriminated.
2786 -- This means that for record aggregates nested inside an array
2787 -- aggregate we will create a new itype for each record aggregate
2788 -- if the array cmponent type has discriminants. For large aggregates
2789 -- this may be a problem. What should be done in this case is
2790 -- to reuse itypes as much as possible.
2792 if Has_Discriminants
(Typ
) then
2793 Build_Constrained_Itype
: declare
2794 Loc
: constant Source_Ptr
:= Sloc
(N
);
2796 Subtyp_Decl
: Node_Id
;
2799 C
: constant List_Id
:= New_List
;
2802 New_Assoc
:= First
(New_Assoc_List
);
2803 while Present
(New_Assoc
) loop
2804 Append
(Duplicate_Subexpr
(Expression
(New_Assoc
)), To
=> C
);
2809 Make_Subtype_Indication
(Loc
,
2810 Subtype_Mark
=> New_Occurrence_Of
(Base_Type
(Typ
), Loc
),
2811 Constraint
=> Make_Index_Or_Discriminant_Constraint
(Loc
, C
));
2813 Def_Id
:= Create_Itype
(Ekind
(Typ
), N
);
2816 Make_Subtype_Declaration
(Loc
,
2817 Defining_Identifier
=> Def_Id
,
2818 Subtype_Indication
=> Indic
);
2819 Set_Parent
(Subtyp_Decl
, Parent
(N
));
2821 -- Itypes must be analyzed with checks off (see itypes.ads)
2823 Analyze
(Subtyp_Decl
, Suppress
=> All_Checks
);
2825 Set_Etype
(N
, Def_Id
);
2826 Check_Static_Discriminated_Subtype
2827 (Def_Id
, Expression
(First
(New_Assoc_List
)));
2828 end Build_Constrained_Itype
;
2834 -- STEP 5: Get remaining components according to discriminant values
2837 Record_Def
: Node_Id
;
2838 Parent_Typ
: Entity_Id
;
2839 Root_Typ
: Entity_Id
;
2840 Parent_Typ_List
: Elist_Id
;
2841 Parent_Elmt
: Elmt_Id
;
2842 Errors_Found
: Boolean := False;
2846 if Is_Derived_Type
(Typ
) and then Is_Tagged_Type
(Typ
) then
2847 Parent_Typ_List
:= New_Elmt_List
;
2849 -- If this is an extension aggregate, the component list must
2850 -- include all components that are not in the given ancestor
2851 -- type. Otherwise, the component list must include components
2852 -- of all ancestors, starting with the root.
2854 if Nkind
(N
) = N_Extension_Aggregate
then
2855 Root_Typ
:= Base_Type
(Etype
(Ancestor_Part
(N
)));
2857 Root_Typ
:= Root_Type
(Typ
);
2859 if Nkind
(Parent
(Base_Type
(Root_Typ
)))
2860 = N_Private_Type_Declaration
2863 ("type of aggregate has private ancestor&!",
2865 Error_Msg_N
("must use extension aggregate!", N
);
2869 Dnode
:= Declaration_Node
(Base_Type
(Root_Typ
));
2871 -- If we don't get a full declaration, then we have some
2872 -- error which will get signalled later so skip this part.
2873 -- Otherwise, gather components of root that apply to the
2874 -- aggregate type. We use the base type in case there is an
2875 -- applicable stored constraint that renames the discriminants
2878 if Nkind
(Dnode
) = N_Full_Type_Declaration
then
2879 Record_Def
:= Type_Definition
(Dnode
);
2880 Gather_Components
(Base_Type
(Typ
),
2881 Component_List
(Record_Def
),
2882 Governed_By
=> New_Assoc_List
,
2884 Report_Errors
=> Errors_Found
);
2888 Parent_Typ
:= Base_Type
(Typ
);
2889 while Parent_Typ
/= Root_Typ
loop
2890 Prepend_Elmt
(Parent_Typ
, To
=> Parent_Typ_List
);
2891 Parent_Typ
:= Etype
(Parent_Typ
);
2893 if Nkind
(Parent
(Base_Type
(Parent_Typ
))) =
2894 N_Private_Type_Declaration
2895 or else Nkind
(Parent
(Base_Type
(Parent_Typ
))) =
2896 N_Private_Extension_Declaration
2898 if Nkind
(N
) /= N_Extension_Aggregate
then
2900 ("type of aggregate has private ancestor&!",
2902 Error_Msg_N
("must use extension aggregate!", N
);
2905 elsif Parent_Typ
/= Root_Typ
then
2907 ("ancestor part of aggregate must be private type&",
2908 Ancestor_Part
(N
), Parent_Typ
);
2914 -- Now collect components from all other ancestors
2916 Parent_Elmt
:= First_Elmt
(Parent_Typ_List
);
2917 while Present
(Parent_Elmt
) loop
2918 Parent_Typ
:= Node
(Parent_Elmt
);
2919 Record_Def
:= Type_Definition
(Parent
(Base_Type
(Parent_Typ
)));
2920 Gather_Components
(Empty
,
2921 Component_List
(Record_Extension_Part
(Record_Def
)),
2922 Governed_By
=> New_Assoc_List
,
2924 Report_Errors
=> Errors_Found
);
2926 Next_Elmt
(Parent_Elmt
);
2930 Record_Def
:= Type_Definition
(Parent
(Base_Type
(Typ
)));
2932 if Null_Present
(Record_Def
) then
2935 Gather_Components
(Base_Type
(Typ
),
2936 Component_List
(Record_Def
),
2937 Governed_By
=> New_Assoc_List
,
2939 Report_Errors
=> Errors_Found
);
2943 if Errors_Found
then
2948 -- STEP 6: Find component Values
2951 Component_Elmt
:= First_Elmt
(Components
);
2953 -- First scan the remaining positional associations in the aggregate.
2954 -- Remember that at this point Positional_Expr contains the current
2955 -- positional association if any is left after looking for discriminant
2956 -- values in step 3.
2958 while Present
(Positional_Expr
) and then Present
(Component_Elmt
) loop
2959 Component
:= Node
(Component_Elmt
);
2960 Resolve_Aggr_Expr
(Positional_Expr
, Component
);
2962 -- Ada 2005 (AI-231)
2964 if Ada_Version
>= Ada_05
2965 and then Known_Null
(Positional_Expr
)
2967 Check_Can_Never_Be_Null
(Component
, Positional_Expr
);
2970 if Present
(Get_Value
(Component
, Component_Associations
(N
))) then
2972 ("more than one value supplied for Component &", N
, Component
);
2975 Next
(Positional_Expr
);
2976 Next_Elmt
(Component_Elmt
);
2979 if Present
(Positional_Expr
) then
2981 ("too many components for record aggregate", Positional_Expr
);
2984 -- Now scan for the named arguments of the aggregate
2986 while Present
(Component_Elmt
) loop
2987 Component
:= Node
(Component_Elmt
);
2988 Expr
:= Get_Value
(Component
, Component_Associations
(N
), True);
2990 -- Note: The previous call to Get_Value sets the value of the
2991 -- variable Is_Box_Present.
2993 -- Ada 2005 (AI-287): Handle components with default initialization.
2994 -- Note: This feature was originally added to Ada 2005 for limited
2995 -- but it was finally allowed with any type.
2997 if Is_Box_Present
then
2998 Check_Box_Component
: declare
2999 Ctyp
: constant Entity_Id
:= Etype
(Component
);
3002 -- If there is a default expression for the aggregate, copy
3003 -- it into a new association.
3005 -- If the component has an initialization procedure (IP) we
3006 -- pass the component to the expander, which will generate
3007 -- the call to such IP.
3009 -- If the component has discriminants, their values must
3010 -- be taken from their subtype. This is indispensable for
3011 -- constraints that are given by the current instance of an
3012 -- enclosing type, to allow the expansion of the aggregate
3013 -- to replace the reference to the current instance by the
3014 -- target object of the aggregate.
3016 if Present
(Parent
(Component
))
3018 Nkind
(Parent
(Component
)) = N_Component_Declaration
3019 and then Present
(Expression
(Parent
(Component
)))
3022 New_Copy_Tree
(Expression
(Parent
(Component
)),
3023 New_Sloc
=> Sloc
(N
));
3026 (Component
=> Component
,
3028 Set_Has_Self_Reference
(N
);
3030 -- A box-defaulted access component gets the value null. Also
3031 -- included are components of private types whose underlying
3032 -- type is an access type.
3034 elsif Present
(Underlying_Type
(Ctyp
))
3035 and then Is_Access_Type
(Underlying_Type
(Ctyp
))
3037 if not Is_Private_Type
(Ctyp
) then
3039 (Component
=> Component
,
3040 Expr
=> Make_Null
(Sloc
(N
)));
3042 -- If the component's type is private with an access type as
3043 -- its underlying type then we have to create an unchecked
3044 -- conversion to satisfy type checking.
3048 Qual_Null
: constant Node_Id
:=
3049 Make_Qualified_Expression
(Sloc
(N
),
3052 (Underlying_Type
(Ctyp
), Sloc
(N
)),
3053 Expression
=> Make_Null
(Sloc
(N
)));
3055 Convert_Null
: constant Node_Id
:=
3056 Unchecked_Convert_To
3060 Analyze_And_Resolve
(Convert_Null
, Ctyp
);
3062 (Component
=> Component
, Expr
=> Convert_Null
);
3066 elsif Has_Non_Null_Base_Init_Proc
(Ctyp
)
3067 or else not Expander_Active
3069 if Is_Record_Type
(Ctyp
)
3070 and then Has_Discriminants
(Ctyp
)
3072 -- We build a partially initialized aggregate with the
3073 -- values of the discriminants and box initialization
3074 -- for the rest, if other components are present.
3077 Loc
: constant Source_Ptr
:= Sloc
(N
);
3080 Discr_Elmt
: Elmt_Id
;
3081 Discr_Val
: Node_Id
;
3085 Expr
:= Make_Aggregate
(Loc
, New_List
, New_List
);
3088 First_Elmt
(Discriminant_Constraint
(Ctyp
));
3089 while Present
(Discr_Elmt
) loop
3090 Discr_Val
:= Node
(Discr_Elmt
);
3092 -- The constraint may be given by a discriminant
3093 -- of the enclosing type, in which case we have
3094 -- to retrieve its value, which is part of the
3095 -- current aggregate.
3097 if Is_Entity_Name
(Discr_Val
)
3099 Ekind
(Entity
(Discr_Val
)) = E_Discriminant
3101 Discr
:= Entity
(Discr_Val
);
3103 Assoc
:= First
(New_Assoc_List
);
3104 while Present
(Assoc
) loop
3106 (Entity
(First
(Choices
(Assoc
))))
3108 Entity
(First
(Choices
(Assoc
))) = Discr
3110 Discr_Val
:= Expression
(Assoc
);
3118 (New_Copy_Tree
(Discr_Val
), Expressions
(Expr
));
3120 -- If the discriminant constraint is a current
3121 -- instance, mark the current aggregate so that
3122 -- the self-reference can be expanded later.
3124 if Nkind
(Discr_Val
) = N_Attribute_Reference
3125 and then Is_Entity_Name
(Prefix
(Discr_Val
))
3126 and then Is_Type
(Entity
(Prefix
(Discr_Val
)))
3127 and then Etype
(N
) = Entity
(Prefix
(Discr_Val
))
3129 Set_Has_Self_Reference
(N
);
3132 Next_Elmt
(Discr_Elmt
);
3139 -- Look for a component that is not a discriminant
3140 -- before creating an others box association.
3142 Comp
:= First_Component
(Ctyp
);
3143 while Present
(Comp
) loop
3144 if Ekind
(Comp
) = E_Component
then
3146 (Make_Component_Association
(Loc
,
3148 New_List
(Make_Others_Choice
(Loc
)),
3149 Expression
=> Empty
,
3150 Box_Present
=> True),
3151 Component_Associations
(Expr
));
3155 Next_Component
(Comp
);
3160 (Component
=> Component
,
3166 (Component
=> Component
,
3168 Is_Box_Present
=> True);
3171 -- Otherwise we only need to resolve the expression if the
3172 -- component has partially initialized values (required to
3173 -- expand the corresponding assignments and run-time checks).
3175 elsif Present
(Expr
)
3176 and then Is_Partially_Initialized_Type
(Ctyp
)
3178 Resolve_Aggr_Expr
(Expr
, Component
);
3180 end Check_Box_Component
;
3182 elsif No
(Expr
) then
3184 -- Ignore hidden components associated with the position of the
3185 -- interface tags: these are initialized dynamically.
3187 if Present
(Related_Interface
(Component
)) then
3191 ("no value supplied for component &!", N
, Component
);
3195 Resolve_Aggr_Expr
(Expr
, Component
);
3198 Next_Elmt
(Component_Elmt
);
3201 -- STEP 7: check for invalid components + check type in choice list
3208 -- Type of first component in choice list
3211 if Present
(Component_Associations
(N
)) then
3212 Assoc
:= First
(Component_Associations
(N
));
3217 Verification
: while Present
(Assoc
) loop
3218 Selectr
:= First
(Choices
(Assoc
));
3221 if Nkind
(Selectr
) = N_Others_Choice
then
3223 -- Ada 2005 (AI-287): others choice may have expression or box
3225 if No
(Others_Etype
)
3226 and then not Others_Box
3229 ("OTHERS must represent at least one component", Selectr
);
3235 while Present
(Selectr
) loop
3236 New_Assoc
:= First
(New_Assoc_List
);
3237 while Present
(New_Assoc
) loop
3238 Component
:= First
(Choices
(New_Assoc
));
3239 exit when Chars
(Selectr
) = Chars
(Component
);
3243 -- If no association, this is not a legal component of
3244 -- of the type in question, except if its association
3245 -- is provided with a box.
3247 if No
(New_Assoc
) then
3248 if Box_Present
(Parent
(Selectr
)) then
3250 -- This may still be a bogus component with a box. Scan
3251 -- list of components to verify that a component with
3252 -- that name exists.
3258 C
:= First_Component
(Typ
);
3259 while Present
(C
) loop
3260 if Chars
(C
) = Chars
(Selectr
) then
3268 Error_Msg_Node_2
:= Typ
;
3269 Error_Msg_N
("& is not a component of}", Selectr
);
3273 elsif Chars
(Selectr
) /= Name_uTag
3274 and then Chars
(Selectr
) /= Name_uParent
3275 and then Chars
(Selectr
) /= Name_uController
3277 if not Has_Discriminants
(Typ
) then
3278 Error_Msg_Node_2
:= Typ
;
3279 Error_Msg_N
("& is not a component of}", Selectr
);
3282 ("& is not a component of the aggregate subtype",
3286 Check_Misspelled_Component
(Components
, Selectr
);
3289 elsif No
(Typech
) then
3290 Typech
:= Base_Type
(Etype
(Component
));
3292 elsif Typech
/= Base_Type
(Etype
(Component
)) then
3293 if not Box_Present
(Parent
(Selectr
)) then
3295 ("components in choice list must have same type",
3304 end loop Verification
;
3307 -- STEP 8: replace the original aggregate
3310 New_Aggregate
: constant Node_Id
:= New_Copy
(N
);
3313 Set_Expressions
(New_Aggregate
, No_List
);
3314 Set_Etype
(New_Aggregate
, Etype
(N
));
3315 Set_Component_Associations
(New_Aggregate
, New_Assoc_List
);
3317 Rewrite
(N
, New_Aggregate
);
3319 end Resolve_Record_Aggregate
;
3321 -----------------------------
3322 -- Check_Can_Never_Be_Null --
3323 -----------------------------
3325 procedure Check_Can_Never_Be_Null
(Typ
: Entity_Id
; Expr
: Node_Id
) is
3326 Comp_Typ
: Entity_Id
;
3330 (Ada_Version
>= Ada_05
3331 and then Present
(Expr
)
3332 and then Known_Null
(Expr
));
3335 when E_Array_Type
=>
3336 Comp_Typ
:= Component_Type
(Typ
);
3340 Comp_Typ
:= Etype
(Typ
);
3346 if Can_Never_Be_Null
(Comp_Typ
) then
3348 -- Here we know we have a constraint error. Note that we do not use
3349 -- Apply_Compile_Time_Constraint_Error here to the Expr, which might
3350 -- seem the more natural approach. That's because in some cases the
3351 -- components are rewritten, and the replacement would be missed.
3354 (Compile_Time_Constraint_Error
3356 "(Ada 2005) null not allowed in null-excluding component?"),
3357 Make_Raise_Constraint_Error
(Sloc
(Expr
),
3358 Reason
=> CE_Access_Check_Failed
));
3360 -- Set proper type for bogus component (why is this needed???)
3362 Set_Etype
(Expr
, Comp_Typ
);
3363 Set_Analyzed
(Expr
);
3365 end Check_Can_Never_Be_Null
;
3367 ---------------------
3368 -- Sort_Case_Table --
3369 ---------------------
3371 procedure Sort_Case_Table
(Case_Table
: in out Case_Table_Type
) is
3372 L
: constant Int
:= Case_Table
'First;
3373 U
: constant Int
:= Case_Table
'Last;
3381 T
:= Case_Table
(K
+ 1);
3385 and then Expr_Value
(Case_Table
(J
- 1).Choice_Lo
) >
3386 Expr_Value
(T
.Choice_Lo
)
3388 Case_Table
(J
) := Case_Table
(J
- 1);
3392 Case_Table
(J
) := T
;
3395 end Sort_Case_Table
;