1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2013, 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 Expander
; use Expander
;
32 with Exp_Tss
; use Exp_Tss
;
33 with Exp_Util
; use Exp_Util
;
34 with Freeze
; use Freeze
;
35 with Itypes
; use Itypes
;
37 with Lib
.Xref
; use Lib
.Xref
;
38 with Namet
; use Namet
;
39 with Namet
.Sp
; use Namet
.Sp
;
40 with Nmake
; use Nmake
;
41 with Nlists
; use Nlists
;
43 with Restrict
; use Restrict
;
45 with Sem_Aux
; use Sem_Aux
;
46 with Sem_Cat
; use Sem_Cat
;
47 with Sem_Ch3
; use Sem_Ch3
;
48 with Sem_Ch8
; use Sem_Ch8
;
49 with Sem_Ch13
; use Sem_Ch13
;
50 with Sem_Dim
; use Sem_Dim
;
51 with Sem_Eval
; use Sem_Eval
;
52 with Sem_Res
; use Sem_Res
;
53 with Sem_Util
; use Sem_Util
;
54 with Sem_Type
; use Sem_Type
;
55 with Sem_Warn
; use Sem_Warn
;
56 with Sinfo
; use Sinfo
;
57 with Snames
; use Snames
;
58 with Stringt
; use Stringt
;
59 with Stand
; use Stand
;
60 with Style
; use Style
;
61 with Targparm
; use Targparm
;
62 with Tbuild
; use Tbuild
;
63 with Uintp
; use Uintp
;
65 package body Sem_Aggr
is
67 type Case_Bounds
is record
69 -- Low bound of choice. Once we sort the Case_Table, then entries
70 -- will be in order of ascending Choice_Lo values.
73 -- High Bound of choice. The sort does not pay any attention to the
74 -- high bound, so choices 1 .. 4 and 1 .. 5 could be in either order.
77 -- If there are duplicates or missing entries, then in the sorted
78 -- table, this records the highest value among Choice_Hi values
79 -- seen so far, including this entry.
82 -- The node of the choice
85 type Case_Table_Type
is array (Nat
range <>) of Case_Bounds
;
86 -- Table type used by Check_Case_Choices procedure. Entry zero is not
87 -- used (reserved for the sort). Real entries start at one.
89 -----------------------
90 -- Local Subprograms --
91 -----------------------
93 procedure Sort_Case_Table
(Case_Table
: in out Case_Table_Type
);
94 -- Sort the Case Table using the Lower Bound of each Choice as the key. A
95 -- simple insertion sort is used since the choices in a case statement will
96 -- usually be in near sorted order.
98 procedure Check_Can_Never_Be_Null
(Typ
: Entity_Id
; Expr
: Node_Id
);
99 -- Ada 2005 (AI-231): Check bad usage of null for a component for which
100 -- null exclusion (NOT NULL) is specified. Typ can be an E_Array_Type for
101 -- the array case (the component type of the array will be used) or an
102 -- E_Component/E_Discriminant entity in the record case, in which case the
103 -- type of the component will be used for the test. If Typ is any other
104 -- kind of entity, the call is ignored. Expr is the component node in the
105 -- aggregate which is known to have a null value. A warning message will be
106 -- issued if the component is null excluding.
108 -- It would be better to pass the proper type for Typ ???
110 procedure Check_Expr_OK_In_Limited_Aggregate
(Expr
: Node_Id
);
111 -- Check that Expr is either not limited or else is one of the cases of
112 -- expressions allowed for a limited component association (namely, an
113 -- aggregate, function call, or <> notation). Report error for violations.
115 procedure Check_Qualified_Aggregate
(Level
: Nat
; Expr
: Node_Id
);
116 -- Given aggregate Expr, check that sub-aggregates of Expr that are nested
117 -- at Level are qualified. If Level = 0, this applies to Expr directly.
118 -- Only issue errors in formal verification mode.
120 function Is_Top_Level_Aggregate
(Expr
: Node_Id
) return Boolean;
121 -- Return True of Expr is an aggregate not contained directly in another
124 ------------------------------------------------------
125 -- Subprograms used for RECORD AGGREGATE Processing --
126 ------------------------------------------------------
128 procedure Resolve_Record_Aggregate
(N
: Node_Id
; Typ
: Entity_Id
);
129 -- This procedure performs all the semantic checks required for record
130 -- aggregates. Note that for aggregates analysis and resolution go
131 -- hand in hand. Aggregate analysis has been delayed up to here and
132 -- it is done while resolving the aggregate.
134 -- N is the N_Aggregate node.
135 -- Typ is the record type for the aggregate resolution
137 -- While performing the semantic checks, this procedure builds a new
138 -- Component_Association_List where each record field appears alone in a
139 -- Component_Choice_List along with its corresponding expression. The
140 -- record fields in the Component_Association_List appear in the same order
141 -- in which they appear in the record type Typ.
143 -- Once this new Component_Association_List is built and all the semantic
144 -- checks performed, the original aggregate subtree is replaced with the
145 -- new named record aggregate just built. Note that subtree substitution is
146 -- performed with Rewrite so as to be able to retrieve the original
149 -- The aggregate subtree manipulation performed by Resolve_Record_Aggregate
150 -- yields the aggregate format expected by Gigi. Typically, this kind of
151 -- tree manipulations are done in the expander. However, because the
152 -- semantic checks that need to be performed on record aggregates really go
153 -- hand in hand with the record aggregate normalization, the aggregate
154 -- subtree transformation is performed during resolution rather than
155 -- expansion. Had we decided otherwise we would have had to duplicate most
156 -- of the code in the expansion procedure Expand_Record_Aggregate. Note,
157 -- however, that all the expansion concerning aggregates for tagged records
158 -- is done in Expand_Record_Aggregate.
160 -- The algorithm of Resolve_Record_Aggregate proceeds as follows:
162 -- 1. Make sure that the record type against which the record aggregate
163 -- has to be resolved is not abstract. Furthermore if the type is a
164 -- null aggregate make sure the input aggregate N is also null.
166 -- 2. Verify that the structure of the aggregate is that of a record
167 -- aggregate. Specifically, look for component associations and ensure
168 -- that each choice list only has identifiers or the N_Others_Choice
169 -- node. Also make sure that if present, the N_Others_Choice occurs
170 -- last and by itself.
172 -- 3. If Typ contains discriminants, the values for each discriminant is
173 -- looked for. If the record type Typ has variants, we check that the
174 -- expressions corresponding to each discriminant ruling the (possibly
175 -- nested) variant parts of Typ, are static. This allows us to determine
176 -- the variant parts to which the rest of the aggregate must conform.
177 -- The names of discriminants with their values are saved in a new
178 -- association list, New_Assoc_List which is later augmented with the
179 -- names and values of the remaining components in the record type.
181 -- During this phase we also make sure that every discriminant is
182 -- assigned exactly one value. Note that when several values for a given
183 -- discriminant are found, semantic processing continues looking for
184 -- further errors. In this case it's the first discriminant value found
185 -- which we will be recorded.
187 -- IMPORTANT NOTE: For derived tagged types this procedure expects
188 -- First_Discriminant and Next_Discriminant to give the correct list
189 -- of discriminants, in the correct order.
191 -- 4. After all the discriminant values have been gathered, we can set the
192 -- Etype of the record aggregate. If Typ contains no discriminants this
193 -- is straightforward: the Etype of N is just Typ, otherwise a new
194 -- implicit constrained subtype of Typ is built to be the Etype of N.
196 -- 5. Gather the remaining record components according to the discriminant
197 -- values. This involves recursively traversing the record type
198 -- structure to see what variants are selected by the given discriminant
199 -- values. This processing is a little more convoluted if Typ is a
200 -- derived tagged types since we need to retrieve the record structure
201 -- of all the ancestors of Typ.
203 -- 6. After gathering the record components we look for their values in the
204 -- record aggregate and emit appropriate error messages should we not
205 -- find such values or should they be duplicated.
207 -- 7. We then make sure no illegal component names appear in the record
208 -- aggregate and make sure that the type of the record components
209 -- appearing in a same choice list is the same. Finally we ensure that
210 -- the others choice, if present, is used to provide the value of at
211 -- least a record component.
213 -- 8. The original aggregate node is replaced with the new named aggregate
214 -- built in steps 3 through 6, as explained earlier.
216 -- Given the complexity of record aggregate resolution, the primary goal of
217 -- this routine is clarity and simplicity rather than execution and storage
218 -- efficiency. If there are only positional components in the aggregate the
219 -- running time is linear. If there are associations the running time is
220 -- still linear as long as the order of the associations is not too far off
221 -- the order of the components in the record type. If this is not the case
222 -- the running time is at worst quadratic in the size of the association
225 procedure Check_Misspelled_Component
226 (Elements
: Elist_Id
;
227 Component
: Node_Id
);
228 -- Give possible misspelling diagnostic if Component is likely to be a
229 -- misspelling of one of the components of the Assoc_List. This is called
230 -- by Resolve_Aggr_Expr after producing an invalid component error message.
232 procedure Check_Static_Discriminated_Subtype
(T
: Entity_Id
; V
: Node_Id
);
233 -- An optimization: determine whether a discriminated subtype has a static
234 -- constraint, and contains array components whose length is also static,
235 -- either because they are constrained by the discriminant, or because the
236 -- original component bounds are static.
238 -----------------------------------------------------
239 -- Subprograms used for ARRAY AGGREGATE Processing --
240 -----------------------------------------------------
242 function Resolve_Array_Aggregate
245 Index_Constr
: Node_Id
;
246 Component_Typ
: Entity_Id
;
247 Others_Allowed
: Boolean) return Boolean;
248 -- This procedure performs the semantic checks for an array aggregate.
249 -- True is returned if the aggregate resolution succeeds.
251 -- The procedure works by recursively checking each nested aggregate.
252 -- Specifically, after checking a sub-aggregate nested at the i-th level
253 -- we recursively check all the subaggregates at the i+1-st level (if any).
254 -- Note that for aggregates analysis and resolution go hand in hand.
255 -- Aggregate analysis has been delayed up to here and it is done while
256 -- resolving the aggregate.
258 -- N is the current N_Aggregate node to be checked.
260 -- Index is the index node corresponding to the array sub-aggregate that
261 -- we are currently checking (RM 4.3.3 (8)). Its Etype is the
262 -- corresponding index type (or subtype).
264 -- Index_Constr is the node giving the applicable index constraint if
265 -- any (RM 4.3.3 (10)). It "is a constraint provided by certain
266 -- contexts [...] that can be used to determine the bounds of the array
267 -- value specified by the aggregate". If Others_Allowed below is False
268 -- there is no applicable index constraint and this node is set to Index.
270 -- Component_Typ is the array component type.
272 -- Others_Allowed indicates whether an others choice is allowed
273 -- in the context where the top-level aggregate appeared.
275 -- The algorithm of Resolve_Array_Aggregate proceeds as follows:
277 -- 1. Make sure that the others choice, if present, is by itself and
278 -- appears last in the sub-aggregate. Check that we do not have
279 -- positional and named components in the array sub-aggregate (unless
280 -- the named association is an others choice). Finally if an others
281 -- choice is present, make sure it is allowed in the aggregate context.
283 -- 2. If the array sub-aggregate contains discrete_choices:
285 -- (A) Verify their validity. Specifically verify that:
287 -- (a) If a null range is present it must be the only possible
288 -- choice in the array aggregate.
290 -- (b) Ditto for a non static range.
292 -- (c) Ditto for a non static expression.
294 -- In addition this step analyzes and resolves each discrete_choice,
295 -- making sure that its type is the type of the corresponding Index.
296 -- If we are not at the lowest array aggregate level (in the case of
297 -- multi-dimensional aggregates) then invoke Resolve_Array_Aggregate
298 -- recursively on each component expression. Otherwise, resolve the
299 -- bottom level component expressions against the expected component
300 -- type ONLY IF the component corresponds to a single discrete choice
301 -- which is not an others choice (to see why read the DELAYED
302 -- COMPONENT RESOLUTION below).
304 -- (B) Determine the bounds of the sub-aggregate and lowest and
305 -- highest choice values.
307 -- 3. For positional aggregates:
309 -- (A) Loop over the component expressions either recursively invoking
310 -- Resolve_Array_Aggregate on each of these for multi-dimensional
311 -- array aggregates or resolving the bottom level component
312 -- expressions against the expected component type.
314 -- (B) Determine the bounds of the positional sub-aggregates.
316 -- 4. Try to determine statically whether the evaluation of the array
317 -- sub-aggregate raises Constraint_Error. If yes emit proper
318 -- warnings. The precise checks are the following:
320 -- (A) Check that the index range defined by aggregate bounds is
321 -- compatible with corresponding index subtype.
322 -- We also check against the base type. In fact it could be that
323 -- Low/High bounds of the base type are static whereas those of
324 -- the index subtype are not. Thus if we can statically catch
325 -- a problem with respect to the base type we are guaranteed
326 -- that the same problem will arise with the index subtype
328 -- (B) If we are dealing with a named aggregate containing an others
329 -- choice and at least one discrete choice then make sure the range
330 -- specified by the discrete choices does not overflow the
331 -- aggregate bounds. We also check against the index type and base
332 -- type bounds for the same reasons given in (A).
334 -- (C) If we are dealing with a positional aggregate with an others
335 -- choice make sure the number of positional elements specified
336 -- does not overflow the aggregate bounds. We also check against
337 -- the index type and base type bounds as mentioned in (A).
339 -- Finally construct an N_Range node giving the sub-aggregate bounds.
340 -- Set the Aggregate_Bounds field of the sub-aggregate to be this
341 -- N_Range. The routine Array_Aggr_Subtype below uses such N_Ranges
342 -- to build the appropriate aggregate subtype. Aggregate_Bounds
343 -- information is needed during expansion.
345 -- DELAYED COMPONENT RESOLUTION: The resolution of bottom level component
346 -- expressions in an array aggregate may call Duplicate_Subexpr or some
347 -- other routine that inserts code just outside the outermost aggregate.
348 -- If the array aggregate contains discrete choices or an others choice,
349 -- this may be wrong. Consider for instance the following example.
351 -- type Rec is record
355 -- type Acc_Rec is access Rec;
356 -- Arr : array (1..3) of Acc_Rec := (1 .. 3 => new Rec);
358 -- Then the transformation of "new Rec" that occurs during resolution
359 -- entails the following code modifications
361 -- P7b : constant Acc_Rec := new Rec;
363 -- Arr : array (1..3) of Acc_Rec := (1 .. 3 => P7b);
365 -- This code transformation is clearly wrong, since we need to call
366 -- "new Rec" for each of the 3 array elements. To avoid this problem we
367 -- delay resolution of the components of non positional array aggregates
368 -- to the expansion phase. As an optimization, if the discrete choice
369 -- specifies a single value we do not delay resolution.
371 function Array_Aggr_Subtype
(N
: Node_Id
; Typ
: Node_Id
) return Entity_Id
;
372 -- This routine returns the type or subtype of an array aggregate.
374 -- N is the array aggregate node whose type we return.
376 -- Typ is the context type in which N occurs.
378 -- This routine creates an implicit array subtype whose bounds are
379 -- those defined by the aggregate. When this routine is invoked
380 -- Resolve_Array_Aggregate has already processed aggregate N. Thus the
381 -- Aggregate_Bounds of each sub-aggregate, is an N_Range node giving the
382 -- sub-aggregate bounds. When building the aggregate itype, this function
383 -- traverses the array aggregate N collecting such Aggregate_Bounds and
384 -- constructs the proper array aggregate itype.
386 -- Note that in the case of multidimensional aggregates each inner
387 -- sub-aggregate corresponding to a given array dimension, may provide a
388 -- different bounds. If it is possible to determine statically that
389 -- some sub-aggregates corresponding to the same index do not have the
390 -- same bounds, then a warning is emitted. If such check is not possible
391 -- statically (because some sub-aggregate bounds are dynamic expressions)
392 -- then this job is left to the expander. In all cases the particular
393 -- bounds that this function will chose for a given dimension is the first
394 -- N_Range node for a sub-aggregate corresponding to that dimension.
396 -- Note that the Raises_Constraint_Error flag of an array aggregate
397 -- whose evaluation is determined to raise CE by Resolve_Array_Aggregate,
398 -- is set in Resolve_Array_Aggregate but the aggregate is not
399 -- immediately replaced with a raise CE. In fact, Array_Aggr_Subtype must
400 -- first construct the proper itype for the aggregate (Gigi needs
401 -- this). After constructing the proper itype we will eventually replace
402 -- the top-level aggregate with a raise CE (done in Resolve_Aggregate).
403 -- Of course in cases such as:
405 -- type Arr is array (integer range <>) of Integer;
406 -- A : Arr := (positive range -1 .. 2 => 0);
408 -- The bounds of the aggregate itype are cooked up to look reasonable
409 -- (in this particular case the bounds will be 1 .. 2).
411 procedure Aggregate_Constraint_Checks
413 Check_Typ
: Entity_Id
);
414 -- Checks expression Exp against subtype Check_Typ. If Exp is an
415 -- aggregate and Check_Typ a constrained record type with discriminants,
416 -- we generate the appropriate discriminant checks. If Exp is an array
417 -- aggregate then emit the appropriate length checks. If Exp is a scalar
418 -- type, or a string literal, Exp is changed into Check_Typ'(Exp) to
419 -- ensure that range checks are performed at run time.
421 procedure Make_String_Into_Aggregate
(N
: Node_Id
);
422 -- A string literal can appear in a context in which a one dimensional
423 -- array of characters is expected. This procedure simply rewrites the
424 -- string as an aggregate, prior to resolution.
426 ---------------------------------
427 -- Aggregate_Constraint_Checks --
428 ---------------------------------
430 procedure Aggregate_Constraint_Checks
432 Check_Typ
: Entity_Id
)
434 Exp_Typ
: constant Entity_Id
:= Etype
(Exp
);
437 if Raises_Constraint_Error
(Exp
) then
441 -- Ada 2005 (AI-230): Generate a conversion to an anonymous access
442 -- component's type to force the appropriate accessibility checks.
444 -- Ada 2005 (AI-231): Generate conversion to the null-excluding
445 -- type to force the corresponding run-time check
447 if Is_Access_Type
(Check_Typ
)
448 and then ((Is_Local_Anonymous_Access
(Check_Typ
))
449 or else (Can_Never_Be_Null
(Check_Typ
)
450 and then not Can_Never_Be_Null
(Exp_Typ
)))
452 Rewrite
(Exp
, Convert_To
(Check_Typ
, Relocate_Node
(Exp
)));
453 Analyze_And_Resolve
(Exp
, Check_Typ
);
454 Check_Unset_Reference
(Exp
);
457 -- This is really expansion activity, so make sure that expansion
458 -- is on and is allowed.
460 if not Expander_Active
or else In_Spec_Expression
then
464 -- First check if we have to insert discriminant checks
466 if Has_Discriminants
(Exp_Typ
) then
467 Apply_Discriminant_Check
(Exp
, Check_Typ
);
469 -- Next emit length checks for array aggregates
471 elsif Is_Array_Type
(Exp_Typ
) then
472 Apply_Length_Check
(Exp
, Check_Typ
);
474 -- Finally emit scalar and string checks. If we are dealing with a
475 -- scalar literal we need to check by hand because the Etype of
476 -- literals is not necessarily correct.
478 elsif Is_Scalar_Type
(Exp_Typ
)
479 and then Compile_Time_Known_Value
(Exp
)
481 if Is_Out_Of_Range
(Exp
, Base_Type
(Check_Typ
)) then
482 Apply_Compile_Time_Constraint_Error
483 (Exp
, "value not in range of}??", CE_Range_Check_Failed
,
484 Ent
=> Base_Type
(Check_Typ
),
485 Typ
=> Base_Type
(Check_Typ
));
487 elsif Is_Out_Of_Range
(Exp
, Check_Typ
) then
488 Apply_Compile_Time_Constraint_Error
489 (Exp
, "value not in range of}??", CE_Range_Check_Failed
,
493 elsif not Range_Checks_Suppressed
(Check_Typ
) then
494 Apply_Scalar_Range_Check
(Exp
, Check_Typ
);
497 -- Verify that target type is also scalar, to prevent view anomalies
498 -- in instantiations.
500 elsif (Is_Scalar_Type
(Exp_Typ
)
501 or else Nkind
(Exp
) = N_String_Literal
)
502 and then Is_Scalar_Type
(Check_Typ
)
503 and then Exp_Typ
/= Check_Typ
505 if Is_Entity_Name
(Exp
)
506 and then Ekind
(Entity
(Exp
)) = E_Constant
508 -- If expression is a constant, it is worthwhile checking whether
509 -- it is a bound of the type.
511 if (Is_Entity_Name
(Type_Low_Bound
(Check_Typ
))
512 and then Entity
(Exp
) = Entity
(Type_Low_Bound
(Check_Typ
)))
513 or else (Is_Entity_Name
(Type_High_Bound
(Check_Typ
))
514 and then Entity
(Exp
) = Entity
(Type_High_Bound
(Check_Typ
)))
519 Rewrite
(Exp
, Convert_To
(Check_Typ
, Relocate_Node
(Exp
)));
520 Analyze_And_Resolve
(Exp
, Check_Typ
);
521 Check_Unset_Reference
(Exp
);
524 Rewrite
(Exp
, Convert_To
(Check_Typ
, Relocate_Node
(Exp
)));
525 Analyze_And_Resolve
(Exp
, Check_Typ
);
526 Check_Unset_Reference
(Exp
);
530 end Aggregate_Constraint_Checks
;
532 ------------------------
533 -- Array_Aggr_Subtype --
534 ------------------------
536 function Array_Aggr_Subtype
538 Typ
: Entity_Id
) return Entity_Id
540 Aggr_Dimension
: constant Pos
:= Number_Dimensions
(Typ
);
541 -- Number of aggregate index dimensions
543 Aggr_Range
: array (1 .. Aggr_Dimension
) of Node_Id
:= (others => Empty
);
544 -- Constrained N_Range of each index dimension in our aggregate itype
546 Aggr_Low
: array (1 .. Aggr_Dimension
) of Node_Id
:= (others => Empty
);
547 Aggr_High
: array (1 .. Aggr_Dimension
) of Node_Id
:= (others => Empty
);
548 -- Low and High bounds for each index dimension in our aggregate itype
550 Is_Fully_Positional
: Boolean := True;
552 procedure Collect_Aggr_Bounds
(N
: Node_Id
; Dim
: Pos
);
553 -- N is an array (sub-)aggregate. Dim is the dimension corresponding
554 -- to (sub-)aggregate N. This procedure collects and removes the side
555 -- effects of the constrained N_Range nodes corresponding to each index
556 -- dimension of our aggregate itype. These N_Range nodes are collected
557 -- in Aggr_Range above.
559 -- Likewise collect in Aggr_Low & Aggr_High above the low and high
560 -- bounds of each index dimension. If, when collecting, two bounds
561 -- corresponding to the same dimension are static and found to differ,
562 -- then emit a warning, and mark N as raising Constraint_Error.
564 -------------------------
565 -- Collect_Aggr_Bounds --
566 -------------------------
568 procedure Collect_Aggr_Bounds
(N
: Node_Id
; Dim
: Pos
) is
569 This_Range
: constant Node_Id
:= Aggregate_Bounds
(N
);
570 -- The aggregate range node of this specific sub-aggregate
572 This_Low
: constant Node_Id
:= Low_Bound
(Aggregate_Bounds
(N
));
573 This_High
: constant Node_Id
:= High_Bound
(Aggregate_Bounds
(N
));
574 -- The aggregate bounds of this specific sub-aggregate
580 Remove_Side_Effects
(This_Low
, Variable_Ref
=> True);
581 Remove_Side_Effects
(This_High
, Variable_Ref
=> True);
583 -- Collect the first N_Range for a given dimension that you find.
584 -- For a given dimension they must be all equal anyway.
586 if No
(Aggr_Range
(Dim
)) then
587 Aggr_Low
(Dim
) := This_Low
;
588 Aggr_High
(Dim
) := This_High
;
589 Aggr_Range
(Dim
) := This_Range
;
592 if Compile_Time_Known_Value
(This_Low
) then
593 if not Compile_Time_Known_Value
(Aggr_Low
(Dim
)) then
594 Aggr_Low
(Dim
) := This_Low
;
596 elsif Expr_Value
(This_Low
) /= Expr_Value
(Aggr_Low
(Dim
)) then
597 Set_Raises_Constraint_Error
(N
);
598 Error_Msg_N
("sub-aggregate low bound mismatch??", N
);
600 ("\Constraint_Error will be raised at run time??", N
);
604 if Compile_Time_Known_Value
(This_High
) then
605 if not Compile_Time_Known_Value
(Aggr_High
(Dim
)) then
606 Aggr_High
(Dim
) := This_High
;
609 Expr_Value
(This_High
) /= Expr_Value
(Aggr_High
(Dim
))
611 Set_Raises_Constraint_Error
(N
);
612 Error_Msg_N
("sub-aggregate high bound mismatch??", N
);
614 ("\Constraint_Error will be raised at run time??", N
);
619 if Dim
< Aggr_Dimension
then
621 -- Process positional components
623 if Present
(Expressions
(N
)) then
624 Expr
:= First
(Expressions
(N
));
625 while Present
(Expr
) loop
626 Collect_Aggr_Bounds
(Expr
, Dim
+ 1);
631 -- Process component associations
633 if Present
(Component_Associations
(N
)) then
634 Is_Fully_Positional
:= False;
636 Assoc
:= First
(Component_Associations
(N
));
637 while Present
(Assoc
) loop
638 Expr
:= Expression
(Assoc
);
639 Collect_Aggr_Bounds
(Expr
, Dim
+ 1);
644 end Collect_Aggr_Bounds
;
646 -- Array_Aggr_Subtype variables
649 -- The final itype of the overall aggregate
651 Index_Constraints
: constant List_Id
:= New_List
;
652 -- The list of index constraints of the aggregate itype
654 -- Start of processing for Array_Aggr_Subtype
657 -- Make sure that the list of index constraints is properly attached to
658 -- the tree, and then collect the aggregate bounds.
660 Set_Parent
(Index_Constraints
, N
);
661 Collect_Aggr_Bounds
(N
, 1);
663 -- Build the list of constrained indexes of our aggregate itype
665 for J
in 1 .. Aggr_Dimension
loop
666 Create_Index
: declare
667 Index_Base
: constant Entity_Id
:=
668 Base_Type
(Etype
(Aggr_Range
(J
)));
669 Index_Typ
: Entity_Id
;
672 -- Construct the Index subtype, and associate it with the range
673 -- construct that generates it.
676 Create_Itype
(Subtype_Kind
(Ekind
(Index_Base
)), Aggr_Range
(J
));
678 Set_Etype
(Index_Typ
, Index_Base
);
680 if Is_Character_Type
(Index_Base
) then
681 Set_Is_Character_Type
(Index_Typ
);
684 Set_Size_Info
(Index_Typ
, (Index_Base
));
685 Set_RM_Size
(Index_Typ
, RM_Size
(Index_Base
));
686 Set_First_Rep_Item
(Index_Typ
, First_Rep_Item
(Index_Base
));
687 Set_Scalar_Range
(Index_Typ
, Aggr_Range
(J
));
689 if Is_Discrete_Or_Fixed_Point_Type
(Index_Typ
) then
690 Set_RM_Size
(Index_Typ
, UI_From_Int
(Minimum_Size
(Index_Typ
)));
693 Set_Etype
(Aggr_Range
(J
), Index_Typ
);
695 Append
(Aggr_Range
(J
), To
=> Index_Constraints
);
699 -- Now build the Itype
701 Itype
:= Create_Itype
(E_Array_Subtype
, N
);
703 Set_First_Rep_Item
(Itype
, First_Rep_Item
(Typ
));
704 Set_Convention
(Itype
, Convention
(Typ
));
705 Set_Depends_On_Private
(Itype
, Has_Private_Component
(Typ
));
706 Set_Etype
(Itype
, Base_Type
(Typ
));
707 Set_Has_Alignment_Clause
(Itype
, Has_Alignment_Clause
(Typ
));
708 Set_Is_Aliased
(Itype
, Is_Aliased
(Typ
));
709 Set_Depends_On_Private
(Itype
, Depends_On_Private
(Typ
));
711 Copy_Suppress_Status
(Index_Check
, Typ
, Itype
);
712 Copy_Suppress_Status
(Length_Check
, Typ
, Itype
);
714 Set_First_Index
(Itype
, First
(Index_Constraints
));
715 Set_Is_Constrained
(Itype
, True);
716 Set_Is_Internal
(Itype
, True);
718 -- A simple optimization: purely positional aggregates of static
719 -- components should be passed to gigi unexpanded whenever possible, and
720 -- regardless of the staticness of the bounds themselves. Subsequent
721 -- checks in exp_aggr verify that type is not packed, etc.
723 Set_Size_Known_At_Compile_Time
(Itype
,
725 and then Comes_From_Source
(N
)
726 and then Size_Known_At_Compile_Time
(Component_Type
(Typ
)));
728 -- We always need a freeze node for a packed array subtype, so that we
729 -- can build the Packed_Array_Type corresponding to the subtype. If
730 -- expansion is disabled, the packed array subtype is not built, and we
731 -- must not generate a freeze node for the type, or else it will appear
732 -- incomplete to gigi.
735 and then not In_Spec_Expression
736 and then Expander_Active
738 Freeze_Itype
(Itype
, N
);
742 end Array_Aggr_Subtype
;
744 --------------------------------
745 -- Check_Misspelled_Component --
746 --------------------------------
748 procedure Check_Misspelled_Component
749 (Elements
: Elist_Id
;
752 Max_Suggestions
: constant := 2;
754 Nr_Of_Suggestions
: Natural := 0;
755 Suggestion_1
: Entity_Id
:= Empty
;
756 Suggestion_2
: Entity_Id
:= Empty
;
757 Component_Elmt
: Elmt_Id
;
760 -- All the components of List are matched against Component and a count
761 -- is maintained of possible misspellings. When at the end of the the
762 -- analysis there are one or two (not more!) possible misspellings,
763 -- these misspellings will be suggested as possible correction.
765 Component_Elmt
:= First_Elmt
(Elements
);
766 while Nr_Of_Suggestions
<= Max_Suggestions
767 and then Present
(Component_Elmt
)
769 if Is_Bad_Spelling_Of
770 (Chars
(Node
(Component_Elmt
)),
773 Nr_Of_Suggestions
:= Nr_Of_Suggestions
+ 1;
775 case Nr_Of_Suggestions
is
776 when 1 => Suggestion_1
:= Node
(Component_Elmt
);
777 when 2 => Suggestion_2
:= Node
(Component_Elmt
);
782 Next_Elmt
(Component_Elmt
);
785 -- Report at most two suggestions
787 if Nr_Of_Suggestions
= 1 then
788 Error_Msg_NE
-- CODEFIX
789 ("\possible misspelling of&", Component
, Suggestion_1
);
791 elsif Nr_Of_Suggestions
= 2 then
792 Error_Msg_Node_2
:= Suggestion_2
;
793 Error_Msg_NE
-- CODEFIX
794 ("\possible misspelling of& or&", Component
, Suggestion_1
);
796 end Check_Misspelled_Component
;
798 ----------------------------------------
799 -- Check_Expr_OK_In_Limited_Aggregate --
800 ----------------------------------------
802 procedure Check_Expr_OK_In_Limited_Aggregate
(Expr
: Node_Id
) is
804 if Is_Limited_Type
(Etype
(Expr
))
805 and then Comes_From_Source
(Expr
)
806 and then not In_Instance_Body
808 if not OK_For_Limited_Init
(Etype
(Expr
), Expr
) then
809 Error_Msg_N
("initialization not allowed for limited types", Expr
);
810 Explain_Limited_Type
(Etype
(Expr
), Expr
);
813 end Check_Expr_OK_In_Limited_Aggregate
;
815 -------------------------------
816 -- Check_Qualified_Aggregate --
817 -------------------------------
819 procedure Check_Qualified_Aggregate
(Level
: Nat
; Expr
: Node_Id
) is
825 if Nkind
(Parent
(Expr
)) /= N_Qualified_Expression
then
826 Check_SPARK_Restriction
("aggregate should be qualified", Expr
);
830 Comp_Expr
:= First
(Expressions
(Expr
));
831 while Present
(Comp_Expr
) loop
832 if Nkind
(Comp_Expr
) = N_Aggregate
then
833 Check_Qualified_Aggregate
(Level
- 1, Comp_Expr
);
836 Comp_Expr
:= Next
(Comp_Expr
);
839 Comp_Assn
:= First
(Component_Associations
(Expr
));
840 while Present
(Comp_Assn
) loop
841 Comp_Expr
:= Expression
(Comp_Assn
);
843 if Nkind
(Comp_Expr
) = N_Aggregate
then
844 Check_Qualified_Aggregate
(Level
- 1, Comp_Expr
);
847 Comp_Assn
:= Next
(Comp_Assn
);
850 end Check_Qualified_Aggregate
;
852 ----------------------------------------
853 -- Check_Static_Discriminated_Subtype --
854 ----------------------------------------
856 procedure Check_Static_Discriminated_Subtype
(T
: Entity_Id
; V
: Node_Id
) is
857 Disc
: constant Entity_Id
:= First_Discriminant
(T
);
862 if Has_Record_Rep_Clause
(T
) then
865 elsif Present
(Next_Discriminant
(Disc
)) then
868 elsif Nkind
(V
) /= N_Integer_Literal
then
872 Comp
:= First_Component
(T
);
873 while Present
(Comp
) loop
874 if Is_Scalar_Type
(Etype
(Comp
)) then
877 elsif Is_Private_Type
(Etype
(Comp
))
878 and then Present
(Full_View
(Etype
(Comp
)))
879 and then Is_Scalar_Type
(Full_View
(Etype
(Comp
)))
883 elsif Is_Array_Type
(Etype
(Comp
)) then
884 if Is_Bit_Packed_Array
(Etype
(Comp
)) then
888 Ind
:= First_Index
(Etype
(Comp
));
889 while Present
(Ind
) loop
890 if Nkind
(Ind
) /= N_Range
891 or else Nkind
(Low_Bound
(Ind
)) /= N_Integer_Literal
892 or else Nkind
(High_Bound
(Ind
)) /= N_Integer_Literal
904 Next_Component
(Comp
);
907 -- On exit, all components have statically known sizes
909 Set_Size_Known_At_Compile_Time
(T
);
910 end Check_Static_Discriminated_Subtype
;
912 -------------------------
913 -- Is_Others_Aggregate --
914 -------------------------
916 function Is_Others_Aggregate
(Aggr
: Node_Id
) return Boolean is
918 return No
(Expressions
(Aggr
))
920 Nkind
(First
(Choices
(First
(Component_Associations
(Aggr
)))))
922 end Is_Others_Aggregate
;
924 ----------------------------
925 -- Is_Top_Level_Aggregate --
926 ----------------------------
928 function Is_Top_Level_Aggregate
(Expr
: Node_Id
) return Boolean is
930 return Nkind
(Parent
(Expr
)) /= N_Aggregate
931 and then (Nkind
(Parent
(Expr
)) /= N_Component_Association
932 or else Nkind
(Parent
(Parent
(Expr
))) /= N_Aggregate
);
933 end Is_Top_Level_Aggregate
;
935 --------------------------------
936 -- Make_String_Into_Aggregate --
937 --------------------------------
939 procedure Make_String_Into_Aggregate
(N
: Node_Id
) is
940 Exprs
: constant List_Id
:= New_List
;
941 Loc
: constant Source_Ptr
:= Sloc
(N
);
942 Str
: constant String_Id
:= Strval
(N
);
943 Strlen
: constant Nat
:= String_Length
(Str
);
951 for J
in 1 .. Strlen
loop
952 C
:= Get_String_Char
(Str
, J
);
953 Set_Character_Literal_Name
(C
);
956 Make_Character_Literal
(P
,
958 Char_Literal_Value
=> UI_From_CC
(C
));
959 Set_Etype
(C_Node
, Any_Character
);
960 Append_To
(Exprs
, C_Node
);
963 -- Something special for wide strings???
966 New_N
:= Make_Aggregate
(Loc
, Expressions
=> Exprs
);
967 Set_Analyzed
(New_N
);
968 Set_Etype
(New_N
, Any_Composite
);
971 end Make_String_Into_Aggregate
;
973 -----------------------
974 -- Resolve_Aggregate --
975 -----------------------
977 procedure Resolve_Aggregate
(N
: Node_Id
; Typ
: Entity_Id
) is
978 Loc
: constant Source_Ptr
:= Sloc
(N
);
979 Pkind
: constant Node_Kind
:= Nkind
(Parent
(N
));
981 Aggr_Subtyp
: Entity_Id
;
982 -- The actual aggregate subtype. This is not necessarily the same as Typ
983 -- which is the subtype of the context in which the aggregate was found.
986 -- Ignore junk empty aggregate resulting from parser error
988 if No
(Expressions
(N
))
989 and then No
(Component_Associations
(N
))
990 and then not Null_Record_Present
(N
)
995 -- If the aggregate has box-initialized components, its type must be
996 -- frozen so that initialization procedures can properly be called
997 -- in the resolution that follows. The replacement of boxes with
998 -- initialization calls is properly an expansion activity but it must
999 -- be done during revolution.
1002 and then Present
(Component_Associations
(N
))
1008 Comp
:= First
(Component_Associations
(N
));
1009 while Present
(Comp
) loop
1010 if Box_Present
(Comp
) then
1011 Insert_Actions
(N
, Freeze_Entity
(Typ
, N
));
1020 -- An unqualified aggregate is restricted in SPARK to:
1022 -- An aggregate item inside an aggregate for a multi-dimensional array
1024 -- An expression being assigned to an unconstrained array, but only if
1025 -- the aggregate specifies a value for OTHERS only.
1027 if Nkind
(Parent
(N
)) = N_Qualified_Expression
then
1028 if Is_Array_Type
(Typ
) then
1029 Check_Qualified_Aggregate
(Number_Dimensions
(Typ
), N
);
1031 Check_Qualified_Aggregate
(1, N
);
1034 if Is_Array_Type
(Typ
)
1035 and then Nkind
(Parent
(N
)) = N_Assignment_Statement
1036 and then not Is_Constrained
(Etype
(Name
(Parent
(N
))))
1038 if not Is_Others_Aggregate
(N
) then
1039 Check_SPARK_Restriction
1040 ("array aggregate should have only OTHERS", N
);
1043 elsif Is_Top_Level_Aggregate
(N
) then
1044 Check_SPARK_Restriction
("aggregate should be qualified", N
);
1046 -- The legality of this unqualified aggregate is checked by calling
1047 -- Check_Qualified_Aggregate from one of its enclosing aggregate,
1048 -- unless one of these already causes an error to be issued.
1055 -- Check for aggregates not allowed in configurable run-time mode.
1056 -- We allow all cases of aggregates that do not come from source, since
1057 -- these are all assumed to be small (e.g. bounds of a string literal).
1058 -- We also allow aggregates of types we know to be small.
1060 if not Support_Aggregates_On_Target
1061 and then Comes_From_Source
(N
)
1062 and then (not Known_Static_Esize
(Typ
) or else Esize
(Typ
) > 64)
1064 Error_Msg_CRT
("aggregate", N
);
1067 -- Ada 2005 (AI-287): Limited aggregates allowed
1069 -- In an instance, ignore aggregate subcomponents tnat may be limited,
1070 -- because they originate in view conflicts. If the original aggregate
1071 -- is legal and the actuals are legal, the aggregate itself is legal.
1073 if Is_Limited_Type
(Typ
)
1074 and then Ada_Version
< Ada_2005
1075 and then not In_Instance
1077 Error_Msg_N
("aggregate type cannot be limited", N
);
1078 Explain_Limited_Type
(Typ
, N
);
1080 elsif Is_Class_Wide_Type
(Typ
) then
1081 Error_Msg_N
("type of aggregate cannot be class-wide", N
);
1083 elsif Typ
= Any_String
1084 or else Typ
= Any_Composite
1086 Error_Msg_N
("no unique type for aggregate", N
);
1087 Set_Etype
(N
, Any_Composite
);
1089 elsif Is_Array_Type
(Typ
) and then Null_Record_Present
(N
) then
1090 Error_Msg_N
("null record forbidden in array aggregate", N
);
1092 elsif Is_Record_Type
(Typ
) then
1093 Resolve_Record_Aggregate
(N
, Typ
);
1095 elsif Is_Array_Type
(Typ
) then
1097 -- First a special test, for the case of a positional aggregate
1098 -- of characters which can be replaced by a string literal.
1100 -- Do not perform this transformation if this was a string literal to
1101 -- start with, whose components needed constraint checks, or if the
1102 -- component type is non-static, because it will require those checks
1103 -- and be transformed back into an aggregate.
1105 if Number_Dimensions
(Typ
) = 1
1106 and then Is_Standard_Character_Type
(Component_Type
(Typ
))
1107 and then No
(Component_Associations
(N
))
1108 and then not Is_Limited_Composite
(Typ
)
1109 and then not Is_Private_Composite
(Typ
)
1110 and then not Is_Bit_Packed_Array
(Typ
)
1111 and then Nkind
(Original_Node
(Parent
(N
))) /= N_String_Literal
1112 and then Is_Static_Subtype
(Component_Type
(Typ
))
1118 Expr
:= First
(Expressions
(N
));
1119 while Present
(Expr
) loop
1120 exit when Nkind
(Expr
) /= N_Character_Literal
;
1127 Expr
:= First
(Expressions
(N
));
1128 while Present
(Expr
) loop
1129 Store_String_Char
(UI_To_CC
(Char_Literal_Value
(Expr
)));
1133 Rewrite
(N
, Make_String_Literal
(Loc
, End_String
));
1135 Analyze_And_Resolve
(N
, Typ
);
1141 -- Here if we have a real aggregate to deal with
1143 Array_Aggregate
: declare
1144 Aggr_Resolved
: Boolean;
1146 Aggr_Typ
: constant Entity_Id
:= Etype
(Typ
);
1147 -- This is the unconstrained array type, which is the type against
1148 -- which the aggregate is to be resolved. Typ itself is the array
1149 -- type of the context which may not be the same subtype as the
1150 -- subtype for the final aggregate.
1153 -- In the following we determine whether an OTHERS choice is
1154 -- allowed inside the array aggregate. The test checks the context
1155 -- in which the array aggregate occurs. If the context does not
1156 -- permit it, or the aggregate type is unconstrained, an OTHERS
1157 -- choice is not allowed (except that it is always allowed on the
1158 -- right-hand side of an assignment statement; in this case the
1159 -- constrainedness of the type doesn't matter).
1161 -- If expansion is disabled (generic context, or semantics-only
1162 -- mode) actual subtypes cannot be constructed, and the type of an
1163 -- object may be its unconstrained nominal type. However, if the
1164 -- context is an assignment, we assume that OTHERS is allowed,
1165 -- because the target of the assignment will have a constrained
1166 -- subtype when fully compiled.
1168 -- Note that there is no node for Explicit_Actual_Parameter.
1169 -- To test for this context we therefore have to test for node
1170 -- N_Parameter_Association which itself appears only if there is a
1171 -- formal parameter. Consequently we also need to test for
1172 -- N_Procedure_Call_Statement or N_Function_Call.
1174 Set_Etype
(N
, Aggr_Typ
); -- May be overridden later on
1176 if Pkind
= N_Assignment_Statement
1177 or else (Is_Constrained
(Typ
)
1179 (Pkind
= N_Parameter_Association
or else
1180 Pkind
= N_Function_Call
or else
1181 Pkind
= N_Procedure_Call_Statement
or else
1182 Pkind
= N_Generic_Association
or else
1183 Pkind
= N_Formal_Object_Declaration
or else
1184 Pkind
= N_Simple_Return_Statement
or else
1185 Pkind
= N_Object_Declaration
or else
1186 Pkind
= N_Component_Declaration
or else
1187 Pkind
= N_Parameter_Specification
or else
1188 Pkind
= N_Qualified_Expression
or else
1189 Pkind
= N_Aggregate
or else
1190 Pkind
= N_Extension_Aggregate
or else
1191 Pkind
= N_Component_Association
))
1194 Resolve_Array_Aggregate
1196 Index
=> First_Index
(Aggr_Typ
),
1197 Index_Constr
=> First_Index
(Typ
),
1198 Component_Typ
=> Component_Type
(Typ
),
1199 Others_Allowed
=> True);
1201 elsif not Expander_Active
1202 and then Pkind
= N_Assignment_Statement
1205 Resolve_Array_Aggregate
1207 Index
=> First_Index
(Aggr_Typ
),
1208 Index_Constr
=> First_Index
(Typ
),
1209 Component_Typ
=> Component_Type
(Typ
),
1210 Others_Allowed
=> True);
1214 Resolve_Array_Aggregate
1216 Index
=> First_Index
(Aggr_Typ
),
1217 Index_Constr
=> First_Index
(Aggr_Typ
),
1218 Component_Typ
=> Component_Type
(Typ
),
1219 Others_Allowed
=> False);
1222 if not Aggr_Resolved
then
1224 -- A parenthesized expression may have been intended as an
1225 -- aggregate, leading to a type error when analyzing the
1226 -- component. This can also happen for a nested component
1227 -- (see Analyze_Aggr_Expr).
1229 if Paren_Count
(N
) > 0 then
1231 ("positional aggregate cannot have one component", N
);
1234 Aggr_Subtyp
:= Any_Composite
;
1237 Aggr_Subtyp
:= Array_Aggr_Subtype
(N
, Typ
);
1240 Set_Etype
(N
, Aggr_Subtyp
);
1241 end Array_Aggregate
;
1243 elsif Is_Private_Type
(Typ
)
1244 and then Present
(Full_View
(Typ
))
1245 and then (In_Inlined_Body
or In_Instance_Body
)
1246 and then Is_Composite_Type
(Full_View
(Typ
))
1248 Resolve
(N
, Full_View
(Typ
));
1251 Error_Msg_N
("illegal context for aggregate", N
);
1254 -- If we can determine statically that the evaluation of the aggregate
1255 -- raises Constraint_Error, then replace the aggregate with an
1256 -- N_Raise_Constraint_Error node, but set the Etype to the right
1257 -- aggregate subtype. Gigi needs this.
1259 if Raises_Constraint_Error
(N
) then
1260 Aggr_Subtyp
:= Etype
(N
);
1262 Make_Raise_Constraint_Error
(Loc
, Reason
=> CE_Range_Check_Failed
));
1263 Set_Raises_Constraint_Error
(N
);
1264 Set_Etype
(N
, Aggr_Subtyp
);
1268 Check_Function_Writable_Actuals
(N
);
1269 end Resolve_Aggregate
;
1271 -----------------------------
1272 -- Resolve_Array_Aggregate --
1273 -----------------------------
1275 function Resolve_Array_Aggregate
1278 Index_Constr
: Node_Id
;
1279 Component_Typ
: Entity_Id
;
1280 Others_Allowed
: Boolean) return Boolean
1282 Loc
: constant Source_Ptr
:= Sloc
(N
);
1284 Failure
: constant Boolean := False;
1285 Success
: constant Boolean := True;
1287 Index_Typ
: constant Entity_Id
:= Etype
(Index
);
1288 Index_Typ_Low
: constant Node_Id
:= Type_Low_Bound
(Index_Typ
);
1289 Index_Typ_High
: constant Node_Id
:= Type_High_Bound
(Index_Typ
);
1290 -- The type of the index corresponding to the array sub-aggregate along
1291 -- with its low and upper bounds.
1293 Index_Base
: constant Entity_Id
:= Base_Type
(Index_Typ
);
1294 Index_Base_Low
: constant Node_Id
:= Type_Low_Bound
(Index_Base
);
1295 Index_Base_High
: constant Node_Id
:= Type_High_Bound
(Index_Base
);
1296 -- Ditto for the base type
1298 function Add
(Val
: Uint
; To
: Node_Id
) return Node_Id
;
1299 -- Creates a new expression node where Val is added to expression To.
1300 -- Tries to constant fold whenever possible. To must be an already
1301 -- analyzed expression.
1303 procedure Check_Bound
(BH
: Node_Id
; AH
: in out Node_Id
);
1304 -- Checks that AH (the upper bound of an array aggregate) is less than
1305 -- or equal to BH (the upper bound of the index base type). If the check
1306 -- fails, a warning is emitted, the Raises_Constraint_Error flag of N is
1307 -- set, and AH is replaced with a duplicate of BH.
1309 procedure Check_Bounds
(L
, H
: Node_Id
; AL
, AH
: Node_Id
);
1310 -- Checks that range AL .. AH is compatible with range L .. H. Emits a
1311 -- warning if not and sets the Raises_Constraint_Error flag in N.
1313 procedure Check_Length
(L
, H
: Node_Id
; Len
: Uint
);
1314 -- Checks that range L .. H contains at least Len elements. Emits a
1315 -- warning if not and sets the Raises_Constraint_Error flag in N.
1317 function Dynamic_Or_Null_Range
(L
, H
: Node_Id
) return Boolean;
1318 -- Returns True if range L .. H is dynamic or null
1320 procedure Get
(Value
: out Uint
; From
: Node_Id
; OK
: out Boolean);
1321 -- Given expression node From, this routine sets OK to False if it
1322 -- cannot statically evaluate From. Otherwise it stores this static
1323 -- value into Value.
1325 function Resolve_Aggr_Expr
1327 Single_Elmt
: Boolean) return Boolean;
1328 -- Resolves aggregate expression Expr. Returns False if resolution
1329 -- fails. If Single_Elmt is set to False, the expression Expr may be
1330 -- used to initialize several array aggregate elements (this can happen
1331 -- for discrete choices such as "L .. H => Expr" or the OTHERS choice).
1332 -- In this event we do not resolve Expr unless expansion is disabled.
1333 -- To know why, see the DELAYED COMPONENT RESOLUTION note above.
1335 -- NOTE: In the case of "... => <>", we pass the in the
1336 -- N_Component_Association node as Expr, since there is no Expression in
1337 -- that case, and we need a Sloc for the error message.
1343 function Add
(Val
: Uint
; To
: Node_Id
) return Node_Id
is
1349 if Raises_Constraint_Error
(To
) then
1353 -- First test if we can do constant folding
1355 if Compile_Time_Known_Value
(To
)
1356 or else Nkind
(To
) = N_Integer_Literal
1358 Expr_Pos
:= Make_Integer_Literal
(Loc
, Expr_Value
(To
) + Val
);
1359 Set_Is_Static_Expression
(Expr_Pos
);
1360 Set_Etype
(Expr_Pos
, Etype
(To
));
1361 Set_Analyzed
(Expr_Pos
, Analyzed
(To
));
1363 if not Is_Enumeration_Type
(Index_Typ
) then
1366 -- If we are dealing with enumeration return
1367 -- Index_Typ'Val (Expr_Pos)
1371 Make_Attribute_Reference
1373 Prefix
=> New_Reference_To
(Index_Typ
, Loc
),
1374 Attribute_Name
=> Name_Val
,
1375 Expressions
=> New_List
(Expr_Pos
));
1381 -- If we are here no constant folding possible
1383 if not Is_Enumeration_Type
(Index_Base
) then
1386 Left_Opnd
=> Duplicate_Subexpr
(To
),
1387 Right_Opnd
=> Make_Integer_Literal
(Loc
, Val
));
1389 -- If we are dealing with enumeration return
1390 -- Index_Typ'Val (Index_Typ'Pos (To) + Val)
1394 Make_Attribute_Reference
1396 Prefix
=> New_Reference_To
(Index_Typ
, Loc
),
1397 Attribute_Name
=> Name_Pos
,
1398 Expressions
=> New_List
(Duplicate_Subexpr
(To
)));
1402 Left_Opnd
=> To_Pos
,
1403 Right_Opnd
=> Make_Integer_Literal
(Loc
, Val
));
1406 Make_Attribute_Reference
1408 Prefix
=> New_Reference_To
(Index_Typ
, Loc
),
1409 Attribute_Name
=> Name_Val
,
1410 Expressions
=> New_List
(Expr_Pos
));
1412 -- If the index type has a non standard representation, the
1413 -- attributes 'Val and 'Pos expand into function calls and the
1414 -- resulting expression is considered non-safe for reevaluation
1415 -- by the backend. Relocate it into a constant temporary in order
1416 -- to make it safe for reevaluation.
1418 if Has_Non_Standard_Rep
(Etype
(N
)) then
1423 Def_Id
:= Make_Temporary
(Loc
, 'R', Expr
);
1424 Set_Etype
(Def_Id
, Index_Typ
);
1426 Make_Object_Declaration
(Loc
,
1427 Defining_Identifier
=> Def_Id
,
1428 Object_Definition
=> New_Reference_To
(Index_Typ
, Loc
),
1429 Constant_Present
=> True,
1430 Expression
=> Relocate_Node
(Expr
)));
1432 Expr
:= New_Reference_To
(Def_Id
, Loc
);
1444 procedure Check_Bound
(BH
: Node_Id
; AH
: in out Node_Id
) is
1452 Get
(Value
=> Val_BH
, From
=> BH
, OK
=> OK_BH
);
1453 Get
(Value
=> Val_AH
, From
=> AH
, OK
=> OK_AH
);
1455 if OK_BH
and then OK_AH
and then Val_BH
< Val_AH
then
1456 Set_Raises_Constraint_Error
(N
);
1457 Error_Msg_N
("upper bound out of range??", AH
);
1458 Error_Msg_N
("\Constraint_Error will be raised at run time??", AH
);
1460 -- You need to set AH to BH or else in the case of enumerations
1461 -- indexes we will not be able to resolve the aggregate bounds.
1463 AH
:= Duplicate_Subexpr
(BH
);
1471 procedure Check_Bounds
(L
, H
: Node_Id
; AL
, AH
: Node_Id
) is
1482 pragma Warnings
(Off
, OK_AL
);
1483 pragma Warnings
(Off
, OK_AH
);
1486 if Raises_Constraint_Error
(N
)
1487 or else Dynamic_Or_Null_Range
(AL
, AH
)
1492 Get
(Value
=> Val_L
, From
=> L
, OK
=> OK_L
);
1493 Get
(Value
=> Val_H
, From
=> H
, OK
=> OK_H
);
1495 Get
(Value
=> Val_AL
, From
=> AL
, OK
=> OK_AL
);
1496 Get
(Value
=> Val_AH
, From
=> AH
, OK
=> OK_AH
);
1498 if OK_L
and then Val_L
> Val_AL
then
1499 Set_Raises_Constraint_Error
(N
);
1500 Error_Msg_N
("lower bound of aggregate out of range??", N
);
1501 Error_Msg_N
("\Constraint_Error will be raised at run time??", N
);
1504 if OK_H
and then Val_H
< Val_AH
then
1505 Set_Raises_Constraint_Error
(N
);
1506 Error_Msg_N
("upper bound of aggregate out of range??", N
);
1507 Error_Msg_N
("\Constraint_Error will be raised at run time??", N
);
1515 procedure Check_Length
(L
, H
: Node_Id
; Len
: Uint
) is
1525 if Raises_Constraint_Error
(N
) then
1529 Get
(Value
=> Val_L
, From
=> L
, OK
=> OK_L
);
1530 Get
(Value
=> Val_H
, From
=> H
, OK
=> OK_H
);
1532 if not OK_L
or else not OK_H
then
1536 -- If null range length is zero
1538 if Val_L
> Val_H
then
1539 Range_Len
:= Uint_0
;
1541 Range_Len
:= Val_H
- Val_L
+ 1;
1544 if Range_Len
< Len
then
1545 Set_Raises_Constraint_Error
(N
);
1546 Error_Msg_N
("too many elements??", N
);
1547 Error_Msg_N
("\Constraint_Error will be raised at run time??", N
);
1551 ---------------------------
1552 -- Dynamic_Or_Null_Range --
1553 ---------------------------
1555 function Dynamic_Or_Null_Range
(L
, H
: Node_Id
) return Boolean is
1563 Get
(Value
=> Val_L
, From
=> L
, OK
=> OK_L
);
1564 Get
(Value
=> Val_H
, From
=> H
, OK
=> OK_H
);
1566 return not OK_L
or else not OK_H
1567 or else not Is_OK_Static_Expression
(L
)
1568 or else not Is_OK_Static_Expression
(H
)
1569 or else Val_L
> Val_H
;
1570 end Dynamic_Or_Null_Range
;
1576 procedure Get
(Value
: out Uint
; From
: Node_Id
; OK
: out Boolean) is
1580 if Compile_Time_Known_Value
(From
) then
1581 Value
:= Expr_Value
(From
);
1583 -- If expression From is something like Some_Type'Val (10) then
1586 elsif Nkind
(From
) = N_Attribute_Reference
1587 and then Attribute_Name
(From
) = Name_Val
1588 and then Compile_Time_Known_Value
(First
(Expressions
(From
)))
1590 Value
:= Expr_Value
(First
(Expressions
(From
)));
1598 -----------------------
1599 -- Resolve_Aggr_Expr --
1600 -----------------------
1602 function Resolve_Aggr_Expr
1604 Single_Elmt
: Boolean) return Boolean
1606 Nxt_Ind
: constant Node_Id
:= Next_Index
(Index
);
1607 Nxt_Ind_Constr
: constant Node_Id
:= Next_Index
(Index_Constr
);
1608 -- Index is the current index corresponding to the expression
1610 Resolution_OK
: Boolean := True;
1611 -- Set to False if resolution of the expression failed
1614 -- Defend against previous errors
1616 if Nkind
(Expr
) = N_Error
1617 or else Error_Posted
(Expr
)
1622 -- If the array type against which we are resolving the aggregate
1623 -- has several dimensions, the expressions nested inside the
1624 -- aggregate must be further aggregates (or strings).
1626 if Present
(Nxt_Ind
) then
1627 if Nkind
(Expr
) /= N_Aggregate
then
1629 -- A string literal can appear where a one-dimensional array
1630 -- of characters is expected. If the literal looks like an
1631 -- operator, it is still an operator symbol, which will be
1632 -- transformed into a string when analyzed.
1634 if Is_Character_Type
(Component_Typ
)
1635 and then No
(Next_Index
(Nxt_Ind
))
1636 and then Nkind_In
(Expr
, N_String_Literal
, N_Operator_Symbol
)
1638 -- A string literal used in a multidimensional array
1639 -- aggregate in place of the final one-dimensional
1640 -- aggregate must not be enclosed in parentheses.
1642 if Paren_Count
(Expr
) /= 0 then
1643 Error_Msg_N
("no parenthesis allowed here", Expr
);
1646 Make_String_Into_Aggregate
(Expr
);
1649 Error_Msg_N
("nested array aggregate expected", Expr
);
1651 -- If the expression is parenthesized, this may be
1652 -- a missing component association for a 1-aggregate.
1654 if Paren_Count
(Expr
) > 0 then
1656 ("\if single-component aggregate is intended,"
1657 & " write e.g. (1 ='> ...)", Expr
);
1664 -- If it's "... => <>", nothing to resolve
1666 if Nkind
(Expr
) = N_Component_Association
then
1667 pragma Assert
(Box_Present
(Expr
));
1671 -- Ada 2005 (AI-231): Propagate the type to the nested aggregate.
1672 -- Required to check the null-exclusion attribute (if present).
1673 -- This value may be overridden later on.
1675 Set_Etype
(Expr
, Etype
(N
));
1677 Resolution_OK
:= Resolve_Array_Aggregate
1678 (Expr
, Nxt_Ind
, Nxt_Ind_Constr
, Component_Typ
, Others_Allowed
);
1682 -- If it's "... => <>", nothing to resolve
1684 if Nkind
(Expr
) = N_Component_Association
then
1685 pragma Assert
(Box_Present
(Expr
));
1689 -- Do not resolve the expressions of discrete or others choices
1690 -- unless the expression covers a single component, or the
1691 -- expander is inactive.
1693 -- In SPARK mode, expressions that can perform side-effects will
1694 -- be recognized by the gnat2why back-end, and the whole
1695 -- subprogram will be ignored. So semantic analysis can be
1696 -- performed safely.
1699 or else not Full_Expander_Active
1700 or else In_Spec_Expression
1702 Analyze_And_Resolve
(Expr
, Component_Typ
);
1703 Check_Expr_OK_In_Limited_Aggregate
(Expr
);
1704 Check_Non_Static_Context
(Expr
);
1705 Aggregate_Constraint_Checks
(Expr
, Component_Typ
);
1706 Check_Unset_Reference
(Expr
);
1710 -- If an aggregate component has a type with predicates, an explicit
1711 -- predicate check must be applied, as for an assignment statement,
1712 -- because the aggegate might not be expanded into individual
1713 -- component assignments.
1715 if Present
(Predicate_Function
(Component_Typ
)) then
1716 Apply_Predicate_Check
(Expr
, Component_Typ
);
1719 if Raises_Constraint_Error
(Expr
)
1720 and then Nkind
(Parent
(Expr
)) /= N_Component_Association
1722 Set_Raises_Constraint_Error
(N
);
1725 -- If the expression has been marked as requiring a range check,
1726 -- then generate it here.
1728 if Do_Range_Check
(Expr
) then
1729 Set_Do_Range_Check
(Expr
, False);
1730 Generate_Range_Check
(Expr
, Component_Typ
, CE_Range_Check_Failed
);
1733 return Resolution_OK
;
1734 end Resolve_Aggr_Expr
;
1736 -- Variables local to Resolve_Array_Aggregate
1743 pragma Warnings
(Off
, Discard
);
1745 Delete_Choice
: Boolean;
1746 -- Used when replacing a subtype choice with predicate by a list
1748 Aggr_Low
: Node_Id
:= Empty
;
1749 Aggr_High
: Node_Id
:= Empty
;
1750 -- The actual low and high bounds of this sub-aggregate
1752 Choices_Low
: Node_Id
:= Empty
;
1753 Choices_High
: Node_Id
:= Empty
;
1754 -- The lowest and highest discrete choices values for a named aggregate
1756 Nb_Elements
: Uint
:= Uint_0
;
1757 -- The number of elements in a positional aggregate
1759 Others_Present
: Boolean := False;
1761 Nb_Choices
: Nat
:= 0;
1762 -- Contains the overall number of named choices in this sub-aggregate
1764 Nb_Discrete_Choices
: Nat
:= 0;
1765 -- The overall number of discrete choices (not counting others choice)
1767 Case_Table_Size
: Nat
;
1768 -- Contains the size of the case table needed to sort aggregate choices
1770 -- Start of processing for Resolve_Array_Aggregate
1773 -- Ignore junk empty aggregate resulting from parser error
1775 if No
(Expressions
(N
))
1776 and then No
(Component_Associations
(N
))
1777 and then not Null_Record_Present
(N
)
1782 -- STEP 1: make sure the aggregate is correctly formatted
1784 if Present
(Component_Associations
(N
)) then
1785 Assoc
:= First
(Component_Associations
(N
));
1786 while Present
(Assoc
) loop
1787 Choice
:= First
(Choices
(Assoc
));
1788 Delete_Choice
:= False;
1790 while Present
(Choice
) loop
1791 if Nkind
(Choice
) = N_Others_Choice
then
1792 Others_Present
:= True;
1794 if Choice
/= First
(Choices
(Assoc
))
1795 or else Present
(Next
(Choice
))
1798 ("OTHERS must appear alone in a choice list", Choice
);
1802 if Present
(Next
(Assoc
)) then
1804 ("OTHERS must appear last in an aggregate", Choice
);
1808 if Ada_Version
= Ada_83
1809 and then Assoc
/= First
(Component_Associations
(N
))
1810 and then Nkind_In
(Parent
(N
), N_Assignment_Statement
,
1811 N_Object_Declaration
)
1814 ("(Ada 83) illegal context for OTHERS choice", N
);
1817 elsif Is_Entity_Name
(Choice
) then
1821 E
: constant Entity_Id
:= Entity
(Choice
);
1827 if Is_Type
(E
) and then Has_Predicates
(E
) then
1828 Freeze_Before
(N
, E
);
1830 -- If the subtype has a static predicate, replace the
1831 -- original choice with the list of individual values
1832 -- covered by the predicate.
1834 if Present
(Static_Predicate
(E
)) then
1835 Delete_Choice
:= True;
1838 P
:= First
(Static_Predicate
(E
));
1839 while Present
(P
) loop
1841 Set_Sloc
(C
, Sloc
(Choice
));
1842 Append_To
(New_Cs
, C
);
1846 Insert_List_After
(Choice
, New_Cs
);
1852 Nb_Choices
:= Nb_Choices
+ 1;
1855 C
: constant Node_Id
:= Choice
;
1860 if Delete_Choice
then
1862 Nb_Choices
:= Nb_Choices
- 1;
1863 Delete_Choice
:= False;
1872 -- At this point we know that the others choice, if present, is by
1873 -- itself and appears last in the aggregate. Check if we have mixed
1874 -- positional and discrete associations (other than the others choice).
1876 if Present
(Expressions
(N
))
1877 and then (Nb_Choices
> 1
1878 or else (Nb_Choices
= 1 and then not Others_Present
))
1881 ("named association cannot follow positional association",
1882 First
(Choices
(First
(Component_Associations
(N
)))));
1886 -- Test for the validity of an others choice if present
1888 if Others_Present
and then not Others_Allowed
then
1890 ("OTHERS choice not allowed here",
1891 First
(Choices
(First
(Component_Associations
(N
)))));
1895 -- Protect against cascaded errors
1897 if Etype
(Index_Typ
) = Any_Type
then
1901 -- STEP 2: Process named components
1903 if No
(Expressions
(N
)) then
1904 if Others_Present
then
1905 Case_Table_Size
:= Nb_Choices
- 1;
1907 Case_Table_Size
:= Nb_Choices
;
1913 -- Denote the lowest and highest values in an aggregate choice
1915 S_Low
: Node_Id
:= Empty
;
1916 S_High
: Node_Id
:= Empty
;
1917 -- if a choice in an aggregate is a subtype indication these
1918 -- denote the lowest and highest values of the subtype
1920 Table
: Case_Table_Type
(0 .. Case_Table_Size
);
1921 -- Used to sort all the different choice values. Entry zero is
1922 -- reserved for sorting purposes.
1924 Single_Choice
: Boolean;
1925 -- Set to true every time there is a single discrete choice in a
1926 -- discrete association
1928 Prev_Nb_Discrete_Choices
: Nat
;
1929 -- Used to keep track of the number of discrete choices in the
1930 -- current association.
1932 Errors_Posted_On_Choices
: Boolean := False;
1933 -- Keeps track of whether any choices have semantic errors
1936 -- STEP 2 (A): Check discrete choices validity
1938 Assoc
:= First
(Component_Associations
(N
));
1939 while Present
(Assoc
) loop
1940 Prev_Nb_Discrete_Choices
:= Nb_Discrete_Choices
;
1941 Choice
:= First
(Choices
(Assoc
));
1945 if Nkind
(Choice
) = N_Others_Choice
then
1946 Single_Choice
:= False;
1949 -- Test for subtype mark without constraint
1951 elsif Is_Entity_Name
(Choice
) and then
1952 Is_Type
(Entity
(Choice
))
1954 if Base_Type
(Entity
(Choice
)) /= Index_Base
then
1956 ("invalid subtype mark in aggregate choice",
1961 -- Case of subtype indication
1963 elsif Nkind
(Choice
) = N_Subtype_Indication
then
1964 Resolve_Discrete_Subtype_Indication
(Choice
, Index_Base
);
1966 -- Does the subtype indication evaluation raise CE?
1968 Get_Index_Bounds
(Subtype_Mark
(Choice
), S_Low
, S_High
);
1969 Get_Index_Bounds
(Choice
, Low
, High
);
1970 Check_Bounds
(S_Low
, S_High
, Low
, High
);
1972 -- Case of range or expression
1975 Resolve
(Choice
, Index_Base
);
1976 Check_Unset_Reference
(Choice
);
1977 Check_Non_Static_Context
(Choice
);
1979 -- If semantic errors were posted on the choice, then
1980 -- record that for possible early return from later
1981 -- processing (see handling of enumeration choices).
1983 if Error_Posted
(Choice
) then
1984 Errors_Posted_On_Choices
:= True;
1987 -- Do not range check a choice. This check is redundant
1988 -- since this test is already done when we check that the
1989 -- bounds of the array aggregate are within range.
1991 Set_Do_Range_Check
(Choice
, False);
1993 -- In SPARK, the choice must be static
1995 if not (Is_Static_Expression
(Choice
)
1996 or else (Nkind
(Choice
) = N_Range
1997 and then Is_Static_Range
(Choice
)))
1999 Check_SPARK_Restriction
2000 ("choice should be static", Choice
);
2004 -- If we could not resolve the discrete choice stop here
2006 if Etype
(Choice
) = Any_Type
then
2009 -- If the discrete choice raises CE get its original bounds
2011 elsif Nkind
(Choice
) = N_Raise_Constraint_Error
then
2012 Set_Raises_Constraint_Error
(N
);
2013 Get_Index_Bounds
(Original_Node
(Choice
), Low
, High
);
2015 -- Otherwise get its bounds as usual
2018 Get_Index_Bounds
(Choice
, Low
, High
);
2021 if (Dynamic_Or_Null_Range
(Low
, High
)
2022 or else (Nkind
(Choice
) = N_Subtype_Indication
2024 Dynamic_Or_Null_Range
(S_Low
, S_High
)))
2025 and then Nb_Choices
/= 1
2028 ("dynamic or empty choice in aggregate " &
2029 "must be the only choice", Choice
);
2033 Nb_Discrete_Choices
:= Nb_Discrete_Choices
+ 1;
2034 Table
(Nb_Discrete_Choices
).Lo
:= Low
;
2035 Table
(Nb_Discrete_Choices
).Hi
:= High
;
2036 Table
(Nb_Discrete_Choices
).Choice
:= Choice
;
2042 -- Check if we have a single discrete choice and whether
2043 -- this discrete choice specifies a single value.
2046 (Nb_Discrete_Choices
= Prev_Nb_Discrete_Choices
+ 1)
2047 and then (Low
= High
);
2053 -- Ada 2005 (AI-231)
2055 if Ada_Version
>= Ada_2005
2056 and then Known_Null
(Expression
(Assoc
))
2058 Check_Can_Never_Be_Null
(Etype
(N
), Expression
(Assoc
));
2061 -- Ada 2005 (AI-287): In case of default initialized component
2062 -- we delay the resolution to the expansion phase.
2064 if Box_Present
(Assoc
) then
2066 -- Ada 2005 (AI-287): In case of default initialization of a
2067 -- component the expander will generate calls to the
2068 -- corresponding initialization subprogram. We need to call
2069 -- Resolve_Aggr_Expr to check the rules about
2072 if not Resolve_Aggr_Expr
2073 (Assoc
, Single_Elmt
=> Single_Choice
)
2078 elsif not Resolve_Aggr_Expr
2079 (Expression
(Assoc
), Single_Elmt
=> Single_Choice
)
2083 -- Check incorrect use of dynamically tagged expression
2085 -- We differentiate here two cases because the expression may
2086 -- not be decorated. For example, the analysis and resolution
2087 -- of the expression associated with the others choice will be
2088 -- done later with the full aggregate. In such case we
2089 -- duplicate the expression tree to analyze the copy and
2090 -- perform the required check.
2092 elsif not Present
(Etype
(Expression
(Assoc
))) then
2094 Save_Analysis
: constant Boolean := Full_Analysis
;
2095 Expr
: constant Node_Id
:=
2096 New_Copy_Tree
(Expression
(Assoc
));
2099 Expander_Mode_Save_And_Set
(False);
2100 Full_Analysis
:= False;
2102 -- Analyze the expression, making sure it is properly
2103 -- attached to the tree before we do the analysis.
2105 Set_Parent
(Expr
, Parent
(Expression
(Assoc
)));
2108 -- If the expression is a literal, propagate this info
2109 -- to the expression in the association, to enable some
2110 -- optimizations downstream.
2112 if Is_Entity_Name
(Expr
)
2113 and then Present
(Entity
(Expr
))
2114 and then Ekind
(Entity
(Expr
)) = E_Enumeration_Literal
2117 (Expression
(Assoc
), Component_Typ
);
2120 Full_Analysis
:= Save_Analysis
;
2121 Expander_Mode_Restore
;
2123 if Is_Tagged_Type
(Etype
(Expr
)) then
2124 Check_Dynamically_Tagged_Expression
2126 Typ
=> Component_Type
(Etype
(N
)),
2131 elsif Is_Tagged_Type
(Etype
(Expression
(Assoc
))) then
2132 Check_Dynamically_Tagged_Expression
2133 (Expr
=> Expression
(Assoc
),
2134 Typ
=> Component_Type
(Etype
(N
)),
2141 -- If aggregate contains more than one choice then these must be
2142 -- static. Check for duplicate and missing values.
2144 -- Note: there is duplicated code here wrt Check_Choice_Set in
2145 -- the body of Sem_Case, and it is possible we could just reuse
2146 -- that procedure. To be checked ???
2148 if Nb_Discrete_Choices
> 1 then
2149 Check_Choices
: declare
2151 -- Location of choice for messages
2155 -- High end of one range and Low end of the next. Should be
2156 -- contiguous if there is no hole in the list of values.
2160 -- End points of duplicated range
2162 Missing_Or_Duplicates
: Boolean := False;
2163 -- Set True if missing or duplicate choices found
2165 procedure Output_Bad_Choices
(Lo
, Hi
: Uint
; C
: Node_Id
);
2166 -- Output continuation message with a representation of the
2167 -- bounds (just Lo if Lo = Hi, else Lo .. Hi). C is the
2168 -- choice node where the message is to be posted.
2170 ------------------------
2171 -- Output_Bad_Choices --
2172 ------------------------
2174 procedure Output_Bad_Choices
(Lo
, Hi
: Uint
; C
: Node_Id
) is
2176 -- Enumeration type case
2178 if Is_Enumeration_Type
(Index_Typ
) then
2180 Chars
(Get_Enum_Lit_From_Pos
(Index_Typ
, Lo
, Loc
));
2182 Chars
(Get_Enum_Lit_From_Pos
(Index_Typ
, Hi
, Loc
));
2185 Error_Msg_N
("\\ %!", C
);
2187 Error_Msg_N
("\\ % .. %!", C
);
2190 -- Integer types case
2193 Error_Msg_Uint_1
:= Lo
;
2194 Error_Msg_Uint_2
:= Hi
;
2197 Error_Msg_N
("\\ ^!", C
);
2199 Error_Msg_N
("\\ ^ .. ^!", C
);
2202 end Output_Bad_Choices
;
2204 -- Start of processing for Check_Choices
2207 Sort_Case_Table
(Table
);
2209 -- First we do a quick linear loop to find out if we have
2210 -- any duplicates or missing entries (usually we have a
2211 -- legal aggregate, so this will get us out quickly).
2213 for J
in 1 .. Nb_Discrete_Choices
- 1 loop
2214 Hi_Val
:= Expr_Value
(Table
(J
).Hi
);
2215 Lo_Val
:= Expr_Value
(Table
(J
+ 1).Lo
);
2218 or else (Lo_Val
> Hi_Val
+ 1
2219 and then not Others_Present
)
2221 Missing_Or_Duplicates
:= True;
2226 -- If we have missing or duplicate entries, first fill in
2227 -- the Highest entries to make life easier in the following
2228 -- loops to detect bad entries.
2230 if Missing_Or_Duplicates
then
2231 Table
(1).Highest
:= Expr_Value
(Table
(1).Hi
);
2233 for J
in 2 .. Nb_Discrete_Choices
loop
2234 Table
(J
).Highest
:=
2236 (Table
(J
- 1).Highest
, Expr_Value
(Table
(J
).Hi
));
2239 -- Loop through table entries to find duplicate indexes
2241 for J
in 2 .. Nb_Discrete_Choices
loop
2242 Lo_Val
:= Expr_Value
(Table
(J
).Lo
);
2243 Hi_Val
:= Expr_Value
(Table
(J
).Hi
);
2245 -- Case where we have duplicates (the lower bound of
2246 -- this choice is less than or equal to the highest
2247 -- high bound found so far).
2249 if Lo_Val
<= Table
(J
- 1).Highest
then
2251 -- We move backwards looking for duplicates. We can
2252 -- abandon this loop as soon as we reach a choice
2253 -- highest value that is less than Lo_Val.
2255 for K
in reverse 1 .. J
- 1 loop
2256 exit when Table
(K
).Highest
< Lo_Val
;
2258 -- Here we may have duplicates between entries
2259 -- for K and J. Get range of duplicates.
2262 UI_Max
(Lo_Val
, Expr_Value
(Table
(K
).Lo
));
2264 UI_Min
(Hi_Val
, Expr_Value
(Table
(K
).Hi
));
2266 -- Nothing to do if duplicate range is null
2268 if Lo_Dup
> Hi_Dup
then
2271 -- Otherwise place proper message
2274 -- We place message on later choice, with a
2275 -- line reference to the earlier choice.
2277 if Sloc
(Table
(J
).Choice
) <
2278 Sloc
(Table
(K
).Choice
)
2280 Choice
:= Table
(K
).Choice
;
2281 Error_Msg_Sloc
:= Sloc
(Table
(J
).Choice
);
2283 Choice
:= Table
(J
).Choice
;
2284 Error_Msg_Sloc
:= Sloc
(Table
(K
).Choice
);
2287 if Lo_Dup
= Hi_Dup
then
2289 ("index value in array aggregate "
2290 & "duplicates the one given#!", Choice
);
2293 ("index values in array aggregate "
2294 & "duplicate those given#!", Choice
);
2297 Output_Bad_Choices
(Lo_Dup
, Hi_Dup
, Choice
);
2303 -- Loop through entries in table to find missing indexes.
2304 -- Not needed if others, since missing impossible.
2306 if not Others_Present
then
2307 for J
in 2 .. Nb_Discrete_Choices
loop
2308 Lo_Val
:= Expr_Value
(Table
(J
).Lo
);
2309 Hi_Val
:= Table
(J
- 1).Highest
;
2311 if Lo_Val
> Hi_Val
+ 1 then
2312 Choice
:= Table
(J
).Lo
;
2314 if Hi_Val
+ 1 = Lo_Val
- 1 then
2316 ("missing index value in array aggregate!",
2320 ("missing index values in array aggregate!",
2325 (Hi_Val
+ 1, Lo_Val
- 1, Choice
);
2330 -- If either missing or duplicate values, return failure
2332 Set_Etype
(N
, Any_Composite
);
2338 -- STEP 2 (B): Compute aggregate bounds and min/max choices values
2340 if Nb_Discrete_Choices
> 0 then
2341 Choices_Low
:= Table
(1).Lo
;
2342 Choices_High
:= Table
(Nb_Discrete_Choices
).Hi
;
2345 -- If Others is present, then bounds of aggregate come from the
2346 -- index constraint (not the choices in the aggregate itself).
2348 if Others_Present
then
2349 Get_Index_Bounds
(Index_Constr
, Aggr_Low
, Aggr_High
);
2351 -- No others clause present
2354 -- Special processing if others allowed and not present. This
2355 -- means that the bounds of the aggregate come from the index
2356 -- constraint (and the length must match).
2358 if Others_Allowed
then
2359 Get_Index_Bounds
(Index_Constr
, Aggr_Low
, Aggr_High
);
2361 -- If others allowed, and no others present, then the array
2362 -- should cover all index values. If it does not, we will
2363 -- get a length check warning, but there is two cases where
2364 -- an additional warning is useful:
2366 -- If we have no positional components, and the length is
2367 -- wrong (which we can tell by others being allowed with
2368 -- missing components), and the index type is an enumeration
2369 -- type, then issue appropriate warnings about these missing
2370 -- components. They are only warnings, since the aggregate
2371 -- is fine, it's just the wrong length. We skip this check
2372 -- for standard character types (since there are no literals
2373 -- and it is too much trouble to concoct them), and also if
2374 -- any of the bounds have not-known-at-compile-time values.
2376 -- Another case warranting a warning is when the length is
2377 -- right, but as above we have an index type that is an
2378 -- enumeration, and the bounds do not match. This is a
2379 -- case where dubious sliding is allowed and we generate
2380 -- a warning that the bounds do not match.
2382 if No
(Expressions
(N
))
2383 and then Nkind
(Index
) = N_Range
2384 and then Is_Enumeration_Type
(Etype
(Index
))
2385 and then not Is_Standard_Character_Type
(Etype
(Index
))
2386 and then Compile_Time_Known_Value
(Aggr_Low
)
2387 and then Compile_Time_Known_Value
(Aggr_High
)
2388 and then Compile_Time_Known_Value
(Choices_Low
)
2389 and then Compile_Time_Known_Value
(Choices_High
)
2391 -- If any of the expressions or range bounds in choices
2392 -- have semantic errors, then do not attempt further
2393 -- resolution, to prevent cascaded errors.
2395 if Errors_Posted_On_Choices
then
2400 ALo
: constant Node_Id
:= Expr_Value_E
(Aggr_Low
);
2401 AHi
: constant Node_Id
:= Expr_Value_E
(Aggr_High
);
2402 CLo
: constant Node_Id
:= Expr_Value_E
(Choices_Low
);
2403 CHi
: constant Node_Id
:= Expr_Value_E
(Choices_High
);
2408 -- Warning case 1, missing values at start/end. Only
2409 -- do the check if the number of entries is too small.
2411 if (Enumeration_Pos
(CHi
) - Enumeration_Pos
(CLo
))
2413 (Enumeration_Pos
(AHi
) - Enumeration_Pos
(ALo
))
2416 ("missing index value(s) in array aggregate??",
2419 -- Output missing value(s) at start
2421 if Chars
(ALo
) /= Chars
(CLo
) then
2424 if Chars
(ALo
) = Chars
(Ent
) then
2425 Error_Msg_Name_1
:= Chars
(ALo
);
2426 Error_Msg_N
("\ %??", N
);
2428 Error_Msg_Name_1
:= Chars
(ALo
);
2429 Error_Msg_Name_2
:= Chars
(Ent
);
2430 Error_Msg_N
("\ % .. %??", N
);
2434 -- Output missing value(s) at end
2436 if Chars
(AHi
) /= Chars
(CHi
) then
2439 if Chars
(AHi
) = Chars
(Ent
) then
2440 Error_Msg_Name_1
:= Chars
(Ent
);
2441 Error_Msg_N
("\ %??", N
);
2443 Error_Msg_Name_1
:= Chars
(Ent
);
2444 Error_Msg_Name_2
:= Chars
(AHi
);
2445 Error_Msg_N
("\ % .. %??", N
);
2449 -- Warning case 2, dubious sliding. The First_Subtype
2450 -- test distinguishes between a constrained type where
2451 -- sliding is not allowed (so we will get a warning
2452 -- later that Constraint_Error will be raised), and
2453 -- the unconstrained case where sliding is permitted.
2455 elsif (Enumeration_Pos
(CHi
) - Enumeration_Pos
(CLo
))
2457 (Enumeration_Pos
(AHi
) - Enumeration_Pos
(ALo
))
2458 and then Chars
(ALo
) /= Chars
(CLo
)
2460 not Is_Constrained
(First_Subtype
(Etype
(N
)))
2463 ("bounds of aggregate do not match target??", N
);
2469 -- If no others, aggregate bounds come from aggregate
2471 Aggr_Low
:= Choices_Low
;
2472 Aggr_High
:= Choices_High
;
2476 -- STEP 3: Process positional components
2479 -- STEP 3 (A): Process positional elements
2481 Expr
:= First
(Expressions
(N
));
2482 Nb_Elements
:= Uint_0
;
2483 while Present
(Expr
) loop
2484 Nb_Elements
:= Nb_Elements
+ 1;
2486 -- Ada 2005 (AI-231)
2488 if Ada_Version
>= Ada_2005
2489 and then Known_Null
(Expr
)
2491 Check_Can_Never_Be_Null
(Etype
(N
), Expr
);
2494 if not Resolve_Aggr_Expr
(Expr
, Single_Elmt
=> True) then
2498 -- Check incorrect use of dynamically tagged expression
2500 if Is_Tagged_Type
(Etype
(Expr
)) then
2501 Check_Dynamically_Tagged_Expression
2503 Typ
=> Component_Type
(Etype
(N
)),
2510 if Others_Present
then
2511 Assoc
:= Last
(Component_Associations
(N
));
2513 -- Ada 2005 (AI-231)
2515 if Ada_Version
>= Ada_2005
2516 and then Known_Null
(Assoc
)
2518 Check_Can_Never_Be_Null
(Etype
(N
), Expression
(Assoc
));
2521 -- Ada 2005 (AI-287): In case of default initialized component,
2522 -- we delay the resolution to the expansion phase.
2524 if Box_Present
(Assoc
) then
2526 -- Ada 2005 (AI-287): In case of default initialization of a
2527 -- component the expander will generate calls to the
2528 -- corresponding initialization subprogram. We need to call
2529 -- Resolve_Aggr_Expr to check the rules about
2532 if not Resolve_Aggr_Expr
(Assoc
, Single_Elmt
=> False) then
2536 elsif not Resolve_Aggr_Expr
(Expression
(Assoc
),
2537 Single_Elmt
=> False)
2541 -- Check incorrect use of dynamically tagged expression. The
2542 -- expression of the others choice has not been resolved yet.
2543 -- In order to diagnose the semantic error we create a duplicate
2544 -- tree to analyze it and perform the check.
2548 Save_Analysis
: constant Boolean := Full_Analysis
;
2549 Expr
: constant Node_Id
:=
2550 New_Copy_Tree
(Expression
(Assoc
));
2553 Expander_Mode_Save_And_Set
(False);
2554 Full_Analysis
:= False;
2556 Full_Analysis
:= Save_Analysis
;
2557 Expander_Mode_Restore
;
2559 if Is_Tagged_Type
(Etype
(Expr
)) then
2560 Check_Dynamically_Tagged_Expression
2562 Typ
=> Component_Type
(Etype
(N
)),
2569 -- STEP 3 (B): Compute the aggregate bounds
2571 if Others_Present
then
2572 Get_Index_Bounds
(Index_Constr
, Aggr_Low
, Aggr_High
);
2575 if Others_Allowed
then
2576 Get_Index_Bounds
(Index_Constr
, Aggr_Low
, Discard
);
2578 Aggr_Low
:= Index_Typ_Low
;
2581 Aggr_High
:= Add
(Nb_Elements
- 1, To
=> Aggr_Low
);
2582 Check_Bound
(Index_Base_High
, Aggr_High
);
2586 -- STEP 4: Perform static aggregate checks and save the bounds
2590 Check_Bounds
(Index_Typ_Low
, Index_Typ_High
, Aggr_Low
, Aggr_High
);
2591 Check_Bounds
(Index_Base_Low
, Index_Base_High
, Aggr_Low
, Aggr_High
);
2595 if Others_Present
and then Nb_Discrete_Choices
> 0 then
2596 Check_Bounds
(Aggr_Low
, Aggr_High
, Choices_Low
, Choices_High
);
2597 Check_Bounds
(Index_Typ_Low
, Index_Typ_High
,
2598 Choices_Low
, Choices_High
);
2599 Check_Bounds
(Index_Base_Low
, Index_Base_High
,
2600 Choices_Low
, Choices_High
);
2604 elsif Others_Present
and then Nb_Elements
> 0 then
2605 Check_Length
(Aggr_Low
, Aggr_High
, Nb_Elements
);
2606 Check_Length
(Index_Typ_Low
, Index_Typ_High
, Nb_Elements
);
2607 Check_Length
(Index_Base_Low
, Index_Base_High
, Nb_Elements
);
2610 if Raises_Constraint_Error
(Aggr_Low
)
2611 or else Raises_Constraint_Error
(Aggr_High
)
2613 Set_Raises_Constraint_Error
(N
);
2616 Aggr_Low
:= Duplicate_Subexpr
(Aggr_Low
);
2618 -- Do not duplicate Aggr_High if Aggr_High = Aggr_Low + Nb_Elements
2619 -- since the addition node returned by Add is not yet analyzed. Attach
2620 -- to tree and analyze first. Reset analyzed flag to ensure it will get
2621 -- analyzed when it is a literal bound whose type must be properly set.
2623 if Others_Present
or else Nb_Discrete_Choices
> 0 then
2624 Aggr_High
:= Duplicate_Subexpr
(Aggr_High
);
2626 if Etype
(Aggr_High
) = Universal_Integer
then
2627 Set_Analyzed
(Aggr_High
, False);
2631 -- If the aggregate already has bounds attached to it, it means this is
2632 -- a positional aggregate created as an optimization by
2633 -- Exp_Aggr.Convert_To_Positional, so we don't want to change those
2636 if Present
(Aggregate_Bounds
(N
)) and then not Others_Allowed
then
2637 Aggr_Low
:= Low_Bound
(Aggregate_Bounds
(N
));
2638 Aggr_High
:= High_Bound
(Aggregate_Bounds
(N
));
2641 Set_Aggregate_Bounds
2642 (N
, Make_Range
(Loc
, Low_Bound
=> Aggr_Low
, High_Bound
=> Aggr_High
));
2644 -- The bounds may contain expressions that must be inserted upwards.
2645 -- Attach them fully to the tree. After analysis, remove side effects
2646 -- from upper bound, if still needed.
2648 Set_Parent
(Aggregate_Bounds
(N
), N
);
2649 Analyze_And_Resolve
(Aggregate_Bounds
(N
), Index_Typ
);
2650 Check_Unset_Reference
(Aggregate_Bounds
(N
));
2652 if not Others_Present
and then Nb_Discrete_Choices
= 0 then
2654 (Aggregate_Bounds
(N
),
2655 Duplicate_Subexpr
(High_Bound
(Aggregate_Bounds
(N
))));
2658 -- Check the dimensions of each component in the array aggregate
2660 Analyze_Dimension_Array_Aggregate
(N
, Component_Typ
);
2663 end Resolve_Array_Aggregate
;
2665 ---------------------------------
2666 -- Resolve_Extension_Aggregate --
2667 ---------------------------------
2669 -- There are two cases to consider:
2671 -- a) If the ancestor part is a type mark, the components needed are the
2672 -- difference between the components of the expected type and the
2673 -- components of the given type mark.
2675 -- b) If the ancestor part is an expression, it must be unambiguous, and
2676 -- once we have its type we can also compute the needed components as in
2677 -- the previous case. In both cases, if the ancestor type is not the
2678 -- immediate ancestor, we have to build this ancestor recursively.
2680 -- In both cases, discriminants of the ancestor type do not play a role in
2681 -- the resolution of the needed components, because inherited discriminants
2682 -- cannot be used in a type extension. As a result we can compute
2683 -- independently the list of components of the ancestor type and of the
2686 procedure Resolve_Extension_Aggregate
(N
: Node_Id
; Typ
: Entity_Id
) is
2687 A
: constant Node_Id
:= Ancestor_Part
(N
);
2692 function Valid_Limited_Ancestor
(Anc
: Node_Id
) return Boolean;
2693 -- If the type is limited, verify that the ancestor part is a legal
2694 -- expression (aggregate or function call, including 'Input)) that does
2695 -- not require a copy, as specified in 7.5(2).
2697 function Valid_Ancestor_Type
return Boolean;
2698 -- Verify that the type of the ancestor part is a non-private ancestor
2699 -- of the expected type, which must be a type extension.
2701 ----------------------------
2702 -- Valid_Limited_Ancestor --
2703 ----------------------------
2705 function Valid_Limited_Ancestor
(Anc
: Node_Id
) return Boolean is
2707 if Is_Entity_Name
(Anc
)
2708 and then Is_Type
(Entity
(Anc
))
2712 elsif Nkind_In
(Anc
, N_Aggregate
, N_Function_Call
) then
2715 elsif Nkind
(Anc
) = N_Attribute_Reference
2716 and then Attribute_Name
(Anc
) = Name_Input
2720 elsif Nkind
(Anc
) = N_Qualified_Expression
then
2721 return Valid_Limited_Ancestor
(Expression
(Anc
));
2726 end Valid_Limited_Ancestor
;
2728 -------------------------
2729 -- Valid_Ancestor_Type --
2730 -------------------------
2732 function Valid_Ancestor_Type
return Boolean is
2733 Imm_Type
: Entity_Id
;
2736 Imm_Type
:= Base_Type
(Typ
);
2737 while Is_Derived_Type
(Imm_Type
) loop
2738 if Etype
(Imm_Type
) = Base_Type
(A_Type
) then
2741 -- The base type of the parent type may appear as a private
2742 -- extension if it is declared as such in a parent unit of the
2743 -- current one. For consistency of the subsequent analysis use
2744 -- the partial view for the ancestor part.
2746 elsif Is_Private_Type
(Etype
(Imm_Type
))
2747 and then Present
(Full_View
(Etype
(Imm_Type
)))
2748 and then Base_Type
(A_Type
) = Full_View
(Etype
(Imm_Type
))
2750 A_Type
:= Etype
(Imm_Type
);
2753 -- The parent type may be a private extension. The aggregate is
2754 -- legal if the type of the aggregate is an extension of it that
2755 -- is not a private extension.
2757 elsif Is_Private_Type
(A_Type
)
2758 and then not Is_Private_Type
(Imm_Type
)
2759 and then Present
(Full_View
(A_Type
))
2760 and then Base_Type
(Full_View
(A_Type
)) = Etype
(Imm_Type
)
2765 Imm_Type
:= Etype
(Base_Type
(Imm_Type
));
2769 -- If previous loop did not find a proper ancestor, report error
2771 Error_Msg_NE
("expect ancestor type of &", A
, Typ
);
2773 end Valid_Ancestor_Type
;
2775 -- Start of processing for Resolve_Extension_Aggregate
2778 -- Analyze the ancestor part and account for the case where it is a
2779 -- parameterless function call.
2782 Check_Parameterless_Call
(A
);
2784 -- In SPARK, the ancestor part cannot be a type mark
2786 if Is_Entity_Name
(A
)
2787 and then Is_Type
(Entity
(A
))
2789 Check_SPARK_Restriction
("ancestor part cannot be a type mark", A
);
2791 -- AI05-0115: if the ancestor part is a subtype mark, the ancestor
2792 -- must not have unknown discriminants.
2794 if Has_Unknown_Discriminants
(Root_Type
(Typ
)) then
2796 ("aggregate not available for type& whose ancestor "
2797 & "has unknown discriminants", N
, Typ
);
2801 if not Is_Tagged_Type
(Typ
) then
2802 Error_Msg_N
("type of extension aggregate must be tagged", N
);
2805 elsif Is_Limited_Type
(Typ
) then
2807 -- Ada 2005 (AI-287): Limited aggregates are allowed
2809 if Ada_Version
< Ada_2005
then
2810 Error_Msg_N
("aggregate type cannot be limited", N
);
2811 Explain_Limited_Type
(Typ
, N
);
2814 elsif Valid_Limited_Ancestor
(A
) then
2819 ("limited ancestor part must be aggregate or function call", A
);
2822 elsif Is_Class_Wide_Type
(Typ
) then
2823 Error_Msg_N
("aggregate cannot be of a class-wide type", N
);
2827 if Is_Entity_Name
(A
)
2828 and then Is_Type
(Entity
(A
))
2830 A_Type
:= Get_Full_View
(Entity
(A
));
2832 if Valid_Ancestor_Type
then
2833 Set_Entity
(A
, A_Type
);
2834 Set_Etype
(A
, A_Type
);
2836 Validate_Ancestor_Part
(N
);
2837 Resolve_Record_Aggregate
(N
, Typ
);
2840 elsif Nkind
(A
) /= N_Aggregate
then
2841 if Is_Overloaded
(A
) then
2844 Get_First_Interp
(A
, I
, It
);
2845 while Present
(It
.Typ
) loop
2846 -- Only consider limited interpretations in the Ada 2005 case
2848 if Is_Tagged_Type
(It
.Typ
)
2849 and then (Ada_Version
>= Ada_2005
2850 or else not Is_Limited_Type
(It
.Typ
))
2852 if A_Type
/= Any_Type
then
2853 Error_Msg_N
("cannot resolve expression", A
);
2860 Get_Next_Interp
(I
, It
);
2863 if A_Type
= Any_Type
then
2864 if Ada_Version
>= Ada_2005
then
2865 Error_Msg_N
("ancestor part must be of a tagged type", A
);
2868 ("ancestor part must be of a nonlimited tagged type", A
);
2875 A_Type
:= Etype
(A
);
2878 if Valid_Ancestor_Type
then
2879 Resolve
(A
, A_Type
);
2880 Check_Unset_Reference
(A
);
2881 Check_Non_Static_Context
(A
);
2883 -- The aggregate is illegal if the ancestor expression is a call
2884 -- to a function with a limited unconstrained result, unless the
2885 -- type of the aggregate is a null extension. This restriction
2886 -- was added in AI05-67 to simplify implementation.
2888 if Nkind
(A
) = N_Function_Call
2889 and then Is_Limited_Type
(A_Type
)
2890 and then not Is_Null_Extension
(Typ
)
2891 and then not Is_Constrained
(A_Type
)
2894 ("type of limited ancestor part must be constrained", A
);
2896 -- Reject the use of CPP constructors that leave objects partially
2897 -- initialized. For example:
2899 -- type CPP_Root is tagged limited record ...
2900 -- pragma Import (CPP, CPP_Root);
2902 -- type CPP_DT is new CPP_Root and Iface ...
2903 -- pragma Import (CPP, CPP_DT);
2905 -- type Ada_DT is new CPP_DT with ...
2907 -- Obj : Ada_DT := Ada_DT'(New_CPP_Root with others => <>);
2909 -- Using the constructor of CPP_Root the slots of the dispatch
2910 -- table of CPP_DT cannot be set, and the secondary tag of
2911 -- CPP_DT is unknown.
2913 elsif Nkind
(A
) = N_Function_Call
2914 and then Is_CPP_Constructor_Call
(A
)
2915 and then Enclosing_CPP_Parent
(Typ
) /= A_Type
2918 ("??must use 'C'P'P constructor for type &", A
,
2919 Enclosing_CPP_Parent
(Typ
));
2921 -- The following call is not needed if the previous warning
2922 -- is promoted to an error.
2924 Resolve_Record_Aggregate
(N
, Typ
);
2926 elsif Is_Class_Wide_Type
(Etype
(A
))
2927 and then Nkind
(Original_Node
(A
)) = N_Function_Call
2929 -- If the ancestor part is a dispatching call, it appears
2930 -- statically to be a legal ancestor, but it yields any member
2931 -- of the class, and it is not possible to determine whether
2932 -- it is an ancestor of the extension aggregate (much less
2933 -- which ancestor). It is not possible to determine the
2934 -- components of the extension part.
2936 -- This check implements AI-306, which in fact was motivated by
2937 -- an AdaCore query to the ARG after this test was added.
2939 Error_Msg_N
("ancestor part must be statically tagged", A
);
2941 Resolve_Record_Aggregate
(N
, Typ
);
2946 Error_Msg_N
("no unique type for this aggregate", A
);
2949 Check_Function_Writable_Actuals
(N
);
2950 end Resolve_Extension_Aggregate
;
2952 ------------------------------
2953 -- Resolve_Record_Aggregate --
2954 ------------------------------
2956 procedure Resolve_Record_Aggregate
(N
: Node_Id
; Typ
: Entity_Id
) is
2958 -- N_Component_Association node belonging to the input aggregate N
2961 Positional_Expr
: Node_Id
;
2962 Component
: Entity_Id
;
2963 Component_Elmt
: Elmt_Id
;
2965 Components
: constant Elist_Id
:= New_Elmt_List
;
2966 -- Components is the list of the record components whose value must be
2967 -- provided in the aggregate. This list does include discriminants.
2969 New_Assoc_List
: constant List_Id
:= New_List
;
2970 New_Assoc
: Node_Id
;
2971 -- New_Assoc_List is the newly built list of N_Component_Association
2972 -- nodes. New_Assoc is one such N_Component_Association node in it.
2973 -- Note that while Assoc and New_Assoc contain the same kind of nodes,
2974 -- they are used to iterate over two different N_Component_Association
2977 Others_Etype
: Entity_Id
:= Empty
;
2978 -- This variable is used to save the Etype of the last record component
2979 -- that takes its value from the others choice. Its purpose is:
2981 -- (a) make sure the others choice is useful
2983 -- (b) make sure the type of all the components whose value is
2984 -- subsumed by the others choice are the same.
2986 -- This variable is updated as a side effect of function Get_Value.
2988 Is_Box_Present
: Boolean := False;
2989 Others_Box
: Boolean := False;
2990 -- Ada 2005 (AI-287): Variables used in case of default initialization
2991 -- to provide a functionality similar to Others_Etype. Box_Present
2992 -- indicates that the component takes its default initialization;
2993 -- Others_Box indicates that at least one component takes its default
2994 -- initialization. Similar to Others_Etype, they are also updated as a
2995 -- side effect of function Get_Value.
2997 procedure Add_Association
2998 (Component
: Entity_Id
;
3000 Assoc_List
: List_Id
;
3001 Is_Box_Present
: Boolean := False);
3002 -- Builds a new N_Component_Association node which associates Component
3003 -- to expression Expr and adds it to the association list being built,
3004 -- either New_Assoc_List, or the association being built for an inner
3007 function Discr_Present
(Discr
: Entity_Id
) return Boolean;
3008 -- If aggregate N is a regular aggregate this routine will return True.
3009 -- Otherwise, if N is an extension aggregate, Discr is a discriminant
3010 -- whose value may already have been specified by N's ancestor part.
3011 -- This routine checks whether this is indeed the case and if so returns
3012 -- False, signaling that no value for Discr should appear in N's
3013 -- aggregate part. Also, in this case, the routine appends to
3014 -- New_Assoc_List the discriminant value specified in the ancestor part.
3016 -- If the aggregate is in a context with expansion delayed, it will be
3017 -- reanalyzed. The inherited discriminant values must not be reinserted
3018 -- in the component list to prevent spurious errors, but they must be
3019 -- present on first analysis to build the proper subtype indications.
3020 -- The flag Inherited_Discriminant is used to prevent the re-insertion.
3025 Consider_Others_Choice
: Boolean := False)
3027 -- Given a record component stored in parameter Compon, this function
3028 -- returns its value as it appears in the list From, which is a list
3029 -- of N_Component_Association nodes.
3031 -- If no component association has a choice for the searched component,
3032 -- the value provided by the others choice is returned, if there is one,
3033 -- and Consider_Others_Choice is set to true. Otherwise Empty is
3034 -- returned. If there is more than one component association giving a
3035 -- value for the searched record component, an error message is emitted
3036 -- and the first found value is returned.
3038 -- If Consider_Others_Choice is set and the returned expression comes
3039 -- from the others choice, then Others_Etype is set as a side effect.
3040 -- An error message is emitted if the components taking their value from
3041 -- the others choice do not have same type.
3043 function New_Copy_Tree_And_Copy_Dimensions
3045 Map
: Elist_Id
:= No_Elist
;
3046 New_Sloc
: Source_Ptr
:= No_Location
;
3047 New_Scope
: Entity_Id
:= Empty
) return Node_Id
;
3048 -- Same as New_Copy_Tree (defined in Sem_Util), except that this routine
3049 -- also copies the dimensions of Source to the returned node.
3051 procedure Resolve_Aggr_Expr
(Expr
: Node_Id
; Component
: Node_Id
);
3052 -- Analyzes and resolves expression Expr against the Etype of the
3053 -- Component. This routine also applies all appropriate checks to Expr.
3054 -- It finally saves a Expr in the newly created association list that
3055 -- will be attached to the final record aggregate. Note that if the
3056 -- Parent pointer of Expr is not set then Expr was produced with a
3057 -- New_Copy_Tree or some such.
3059 ---------------------
3060 -- Add_Association --
3061 ---------------------
3063 procedure Add_Association
3064 (Component
: Entity_Id
;
3066 Assoc_List
: List_Id
;
3067 Is_Box_Present
: Boolean := False)
3070 Choice_List
: constant List_Id
:= New_List
;
3071 New_Assoc
: Node_Id
;
3074 -- If this is a box association the expression is missing, so
3075 -- use the Sloc of the aggregate itself for the new association.
3077 if Present
(Expr
) then
3083 Append
(New_Occurrence_Of
(Component
, Loc
), Choice_List
);
3085 Make_Component_Association
(Loc
,
3086 Choices
=> Choice_List
,
3088 Box_Present
=> Is_Box_Present
);
3089 Append
(New_Assoc
, Assoc_List
);
3090 end Add_Association
;
3096 function Discr_Present
(Discr
: Entity_Id
) return Boolean is
3097 Regular_Aggr
: constant Boolean := Nkind
(N
) /= N_Extension_Aggregate
;
3102 Comp_Assoc
: Node_Id
;
3103 Discr_Expr
: Node_Id
;
3105 Ancestor_Typ
: Entity_Id
;
3106 Orig_Discr
: Entity_Id
;
3108 D_Val
: Elmt_Id
:= No_Elmt
; -- stop junk warning
3110 Ancestor_Is_Subtyp
: Boolean;
3113 if Regular_Aggr
then
3117 -- Check whether inherited discriminant values have already been
3118 -- inserted in the aggregate. This will be the case if we are
3119 -- re-analyzing an aggregate whose expansion was delayed.
3121 if Present
(Component_Associations
(N
)) then
3122 Comp_Assoc
:= First
(Component_Associations
(N
));
3123 while Present
(Comp_Assoc
) loop
3124 if Inherited_Discriminant
(Comp_Assoc
) then
3132 Ancestor
:= Ancestor_Part
(N
);
3133 Ancestor_Typ
:= Etype
(Ancestor
);
3134 Loc
:= Sloc
(Ancestor
);
3136 -- For a private type with unknown discriminants, use the underlying
3137 -- record view if it is available.
3139 if Has_Unknown_Discriminants
(Ancestor_Typ
)
3140 and then Present
(Full_View
(Ancestor_Typ
))
3141 and then Present
(Underlying_Record_View
(Full_View
(Ancestor_Typ
)))
3143 Ancestor_Typ
:= Underlying_Record_View
(Full_View
(Ancestor_Typ
));
3146 Ancestor_Is_Subtyp
:=
3147 Is_Entity_Name
(Ancestor
) and then Is_Type
(Entity
(Ancestor
));
3149 -- If the ancestor part has no discriminants clearly N's aggregate
3150 -- part must provide a value for Discr.
3152 if not Has_Discriminants
(Ancestor_Typ
) then
3155 -- If the ancestor part is an unconstrained subtype mark then the
3156 -- Discr must be present in N's aggregate part.
3158 elsif Ancestor_Is_Subtyp
3159 and then not Is_Constrained
(Entity
(Ancestor
))
3164 -- Now look to see if Discr was specified in the ancestor part
3166 if Ancestor_Is_Subtyp
then
3167 D_Val
:= First_Elmt
(Discriminant_Constraint
(Entity
(Ancestor
)));
3170 Orig_Discr
:= Original_Record_Component
(Discr
);
3172 D
:= First_Discriminant
(Ancestor_Typ
);
3173 while Present
(D
) loop
3175 -- If Ancestor has already specified Disc value then insert its
3176 -- value in the final aggregate.
3178 if Original_Record_Component
(D
) = Orig_Discr
then
3179 if Ancestor_Is_Subtyp
then
3180 Discr_Expr
:= New_Copy_Tree
(Node
(D_Val
));
3183 Make_Selected_Component
(Loc
,
3184 Prefix
=> Duplicate_Subexpr
(Ancestor
),
3185 Selector_Name
=> New_Occurrence_Of
(Discr
, Loc
));
3188 Resolve_Aggr_Expr
(Discr_Expr
, Discr
);
3189 Set_Inherited_Discriminant
(Last
(New_Assoc_List
));
3193 Next_Discriminant
(D
);
3195 if Ancestor_Is_Subtyp
then
3210 Consider_Others_Choice
: Boolean := False)
3214 Expr
: Node_Id
:= Empty
;
3215 Selector_Name
: Node_Id
;
3218 Is_Box_Present
:= False;
3220 if Present
(From
) then
3221 Assoc
:= First
(From
);
3226 while Present
(Assoc
) loop
3227 Selector_Name
:= First
(Choices
(Assoc
));
3228 while Present
(Selector_Name
) loop
3229 if Nkind
(Selector_Name
) = N_Others_Choice
then
3230 if Consider_Others_Choice
and then No
(Expr
) then
3232 -- We need to duplicate the expression for each
3233 -- successive component covered by the others choice.
3234 -- This is redundant if the others_choice covers only
3235 -- one component (small optimization possible???), but
3236 -- indispensable otherwise, because each one must be
3237 -- expanded individually to preserve side-effects.
3239 -- Ada 2005 (AI-287): In case of default initialization
3240 -- of components, we duplicate the corresponding default
3241 -- expression (from the record type declaration). The
3242 -- copy must carry the sloc of the association (not the
3243 -- original expression) to prevent spurious elaboration
3244 -- checks when the default includes function calls.
3246 if Box_Present
(Assoc
) then
3248 Is_Box_Present
:= True;
3250 if Expander_Active
then
3252 New_Copy_Tree_And_Copy_Dimensions
3253 (Expression
(Parent
(Compon
)),
3254 New_Sloc
=> Sloc
(Assoc
));
3256 return Expression
(Parent
(Compon
));
3260 if Present
(Others_Etype
) and then
3261 Base_Type
(Others_Etype
) /= Base_Type
(Etype
3264 Error_Msg_N
("components in OTHERS choice must " &
3265 "have same type", Selector_Name
);
3268 Others_Etype
:= Etype
(Compon
);
3270 if Expander_Active
then
3272 New_Copy_Tree_And_Copy_Dimensions
3273 (Expression
(Assoc
));
3275 return Expression
(Assoc
);
3280 elsif Chars
(Compon
) = Chars
(Selector_Name
) then
3283 -- Ada 2005 (AI-231)
3285 if Ada_Version
>= Ada_2005
3286 and then Known_Null
(Expression
(Assoc
))
3288 Check_Can_Never_Be_Null
(Compon
, Expression
(Assoc
));
3291 -- We need to duplicate the expression when several
3292 -- components are grouped together with a "|" choice.
3293 -- For instance "filed1 | filed2 => Expr"
3295 -- Ada 2005 (AI-287)
3297 if Box_Present
(Assoc
) then
3298 Is_Box_Present
:= True;
3300 -- Duplicate the default expression of the component
3301 -- from the record type declaration, so a new copy
3302 -- can be attached to the association.
3304 -- Note that we always copy the default expression,
3305 -- even when the association has a single choice, in
3306 -- order to create a proper association for the
3307 -- expanded aggregate.
3309 -- Component may have no default, in which case the
3310 -- expression is empty and the component is default-
3311 -- initialized, but an association for the component
3312 -- exists, and it is not covered by an others clause.
3315 New_Copy_Tree_And_Copy_Dimensions
3316 (Expression
(Parent
(Compon
)));
3319 if Present
(Next
(Selector_Name
)) then
3321 New_Copy_Tree_And_Copy_Dimensions
3322 (Expression
(Assoc
));
3324 Expr
:= Expression
(Assoc
);
3328 Generate_Reference
(Compon
, Selector_Name
, 'm');
3332 ("more than one value supplied for &",
3333 Selector_Name
, Compon
);
3338 Next
(Selector_Name
);
3347 ---------------------------------------
3348 -- New_Copy_Tree_And_Copy_Dimensions --
3349 ---------------------------------------
3351 function New_Copy_Tree_And_Copy_Dimensions
3353 Map
: Elist_Id
:= No_Elist
;
3354 New_Sloc
: Source_Ptr
:= No_Location
;
3355 New_Scope
: Entity_Id
:= Empty
) return Node_Id
3357 New_Copy
: constant Node_Id
:=
3358 New_Copy_Tree
(Source
, Map
, New_Sloc
, New_Scope
);
3360 -- Move the dimensions of Source to New_Copy
3362 Copy_Dimensions
(Source
, New_Copy
);
3364 end New_Copy_Tree_And_Copy_Dimensions
;
3366 -----------------------
3367 -- Resolve_Aggr_Expr --
3368 -----------------------
3370 procedure Resolve_Aggr_Expr
(Expr
: Node_Id
; Component
: Node_Id
) is
3371 Expr_Type
: Entity_Id
:= Empty
;
3372 New_C
: Entity_Id
:= Component
;
3375 function Has_Expansion_Delayed
(Expr
: Node_Id
) return Boolean;
3376 -- If the expression is an aggregate (possibly qualified) then its
3377 -- expansion is delayed until the enclosing aggregate is expanded
3378 -- into assignments. In that case, do not generate checks on the
3379 -- expression, because they will be generated later, and will other-
3380 -- wise force a copy (to remove side-effects) that would leave a
3381 -- dynamic-sized aggregate in the code, something that gigi cannot
3385 -- Set to True if the resolved Expr node needs to be relocated when
3386 -- attached to the newly created association list. This node need not
3387 -- be relocated if its parent pointer is not set. In fact in this
3388 -- case Expr is the output of a New_Copy_Tree call. If Relocate is
3389 -- True then we have analyzed the expression node in the original
3390 -- aggregate and hence it needs to be relocated when moved over to
3391 -- the new association list.
3393 ---------------------------
3394 -- Has_Expansion_Delayed --
3395 ---------------------------
3397 function Has_Expansion_Delayed
(Expr
: Node_Id
) return Boolean is
3398 Kind
: constant Node_Kind
:= Nkind
(Expr
);
3400 return (Nkind_In
(Kind
, N_Aggregate
, N_Extension_Aggregate
)
3401 and then Present
(Etype
(Expr
))
3402 and then Is_Record_Type
(Etype
(Expr
))
3403 and then Expansion_Delayed
(Expr
))
3404 or else (Kind
= N_Qualified_Expression
3405 and then Has_Expansion_Delayed
(Expression
(Expr
)));
3406 end Has_Expansion_Delayed
;
3408 -- Start of processing for Resolve_Aggr_Expr
3411 -- If the type of the component is elementary or the type of the
3412 -- aggregate does not contain discriminants, use the type of the
3413 -- component to resolve Expr.
3415 if Is_Elementary_Type
(Etype
(Component
))
3416 or else not Has_Discriminants
(Etype
(N
))
3418 Expr_Type
:= Etype
(Component
);
3420 -- Otherwise we have to pick up the new type of the component from
3421 -- the new constrained subtype of the aggregate. In fact components
3422 -- which are of a composite type might be constrained by a
3423 -- discriminant, and we want to resolve Expr against the subtype were
3424 -- all discriminant occurrences are replaced with their actual value.
3427 New_C
:= First_Component
(Etype
(N
));
3428 while Present
(New_C
) loop
3429 if Chars
(New_C
) = Chars
(Component
) then
3430 Expr_Type
:= Etype
(New_C
);
3434 Next_Component
(New_C
);
3437 pragma Assert
(Present
(Expr_Type
));
3439 -- For each range in an array type where a discriminant has been
3440 -- replaced with the constraint, check that this range is within
3441 -- the range of the base type. This checks is done in the init
3442 -- proc for regular objects, but has to be done here for
3443 -- aggregates since no init proc is called for them.
3445 if Is_Array_Type
(Expr_Type
) then
3448 -- Range of the current constrained index in the array
3450 Orig_Index
: Node_Id
:= First_Index
(Etype
(Component
));
3451 -- Range corresponding to the range Index above in the
3452 -- original unconstrained record type. The bounds of this
3453 -- range may be governed by discriminants.
3455 Unconstr_Index
: Node_Id
:= First_Index
(Etype
(Expr_Type
));
3456 -- Range corresponding to the range Index above for the
3457 -- unconstrained array type. This range is needed to apply
3461 Index
:= First_Index
(Expr_Type
);
3462 while Present
(Index
) loop
3463 if Depends_On_Discriminant
(Orig_Index
) then
3464 Apply_Range_Check
(Index
, Etype
(Unconstr_Index
));
3468 Next_Index
(Orig_Index
);
3469 Next_Index
(Unconstr_Index
);
3475 -- If the Parent pointer of Expr is not set, Expr is an expression
3476 -- duplicated by New_Tree_Copy (this happens for record aggregates
3477 -- that look like (Field1 | Filed2 => Expr) or (others => Expr)).
3478 -- Such a duplicated expression must be attached to the tree
3479 -- before analysis and resolution to enforce the rule that a tree
3480 -- fragment should never be analyzed or resolved unless it is
3481 -- attached to the current compilation unit.
3483 if No
(Parent
(Expr
)) then
3484 Set_Parent
(Expr
, N
);
3490 Analyze_And_Resolve
(Expr
, Expr_Type
);
3491 Check_Expr_OK_In_Limited_Aggregate
(Expr
);
3492 Check_Non_Static_Context
(Expr
);
3493 Check_Unset_Reference
(Expr
);
3495 -- Check wrong use of class-wide types
3497 if Is_Class_Wide_Type
(Etype
(Expr
)) then
3498 Error_Msg_N
("dynamically tagged expression not allowed", Expr
);
3501 if not Has_Expansion_Delayed
(Expr
) then
3502 Aggregate_Constraint_Checks
(Expr
, Expr_Type
);
3505 -- If an aggregate component has a type with predicates, an explicit
3506 -- predicate check must be applied, as for an assignment statement,
3507 -- because the aggegate might not be expanded into individual
3508 -- component assignments.
3510 if Present
(Predicate_Function
(Expr_Type
)) then
3511 Apply_Predicate_Check
(Expr
, Expr_Type
);
3514 if Raises_Constraint_Error
(Expr
) then
3515 Set_Raises_Constraint_Error
(N
);
3518 -- If the expression has been marked as requiring a range check, then
3519 -- generate it here.
3521 if Do_Range_Check
(Expr
) then
3522 Set_Do_Range_Check
(Expr
, False);
3523 Generate_Range_Check
(Expr
, Expr_Type
, CE_Range_Check_Failed
);
3527 New_Expr
:= Relocate_Node
(Expr
);
3529 -- Since New_Expr is not gonna be analyzed later on, we need to
3530 -- propagate here the dimensions form Expr to New_Expr.
3532 Copy_Dimensions
(Expr
, New_Expr
);
3538 Add_Association
(New_C
, New_Expr
, New_Assoc_List
);
3539 end Resolve_Aggr_Expr
;
3541 -- Start of processing for Resolve_Record_Aggregate
3544 -- A record aggregate is restricted in SPARK:
3546 -- Each named association can have only a single choice.
3547 -- OTHERS cannot be used.
3548 -- Positional and named associations cannot be mixed.
3550 if Present
(Component_Associations
(N
))
3551 and then Present
(First
(Component_Associations
(N
)))
3554 if Present
(Expressions
(N
)) then
3555 Check_SPARK_Restriction
3556 ("named association cannot follow positional one",
3557 First
(Choices
(First
(Component_Associations
(N
)))));
3564 Assoc
:= First
(Component_Associations
(N
));
3565 while Present
(Assoc
) loop
3566 if List_Length
(Choices
(Assoc
)) > 1 then
3567 Check_SPARK_Restriction
3568 ("component association in record aggregate must "
3569 & "contain a single choice", Assoc
);
3572 if Nkind
(First
(Choices
(Assoc
))) = N_Others_Choice
then
3573 Check_SPARK_Restriction
3574 ("record aggregate cannot contain OTHERS", Assoc
);
3577 Assoc
:= Next
(Assoc
);
3582 -- We may end up calling Duplicate_Subexpr on expressions that are
3583 -- attached to New_Assoc_List. For this reason we need to attach it
3584 -- to the tree by setting its parent pointer to N. This parent point
3585 -- will change in STEP 8 below.
3587 Set_Parent
(New_Assoc_List
, N
);
3589 -- STEP 1: abstract type and null record verification
3591 if Is_Abstract_Type
(Typ
) then
3592 Error_Msg_N
("type of aggregate cannot be abstract", N
);
3595 if No
(First_Entity
(Typ
)) and then Null_Record_Present
(N
) then
3599 elsif Present
(First_Entity
(Typ
))
3600 and then Null_Record_Present
(N
)
3601 and then not Is_Tagged_Type
(Typ
)
3603 Error_Msg_N
("record aggregate cannot be null", N
);
3606 -- If the type has no components, then the aggregate should either
3607 -- have "null record", or in Ada 2005 it could instead have a single
3608 -- component association given by "others => <>". For Ada 95 we flag an
3609 -- error at this point, but for Ada 2005 we proceed with checking the
3610 -- associations below, which will catch the case where it's not an
3611 -- aggregate with "others => <>". Note that the legality of a <>
3612 -- aggregate for a null record type was established by AI05-016.
3614 elsif No
(First_Entity
(Typ
))
3615 and then Ada_Version
< Ada_2005
3617 Error_Msg_N
("record aggregate must be null", N
);
3621 -- STEP 2: Verify aggregate structure
3624 Selector_Name
: Node_Id
;
3625 Bad_Aggregate
: Boolean := False;
3628 if Present
(Component_Associations
(N
)) then
3629 Assoc
:= First
(Component_Associations
(N
));
3634 while Present
(Assoc
) loop
3635 Selector_Name
:= First
(Choices
(Assoc
));
3636 while Present
(Selector_Name
) loop
3637 if Nkind
(Selector_Name
) = N_Identifier
then
3640 elsif Nkind
(Selector_Name
) = N_Others_Choice
then
3641 if Selector_Name
/= First
(Choices
(Assoc
))
3642 or else Present
(Next
(Selector_Name
))
3645 ("OTHERS must appear alone in a choice list",
3649 elsif Present
(Next
(Assoc
)) then
3651 ("OTHERS must appear last in an aggregate",
3655 -- (Ada 2005): If this is an association with a box,
3656 -- indicate that the association need not represent
3659 elsif Box_Present
(Assoc
) then
3665 ("selector name should be identifier or OTHERS",
3667 Bad_Aggregate
:= True;
3670 Next
(Selector_Name
);
3676 if Bad_Aggregate
then
3681 -- STEP 3: Find discriminant Values
3684 Discrim
: Entity_Id
;
3685 Missing_Discriminants
: Boolean := False;
3688 if Present
(Expressions
(N
)) then
3689 Positional_Expr
:= First
(Expressions
(N
));
3691 Positional_Expr
:= Empty
;
3694 -- AI05-0115: if the ancestor part is a subtype mark, the ancestor
3695 -- must not have unknown discriminants.
3697 if Is_Derived_Type
(Typ
)
3698 and then Has_Unknown_Discriminants
(Root_Type
(Typ
))
3699 and then Nkind
(N
) /= N_Extension_Aggregate
3702 ("aggregate not available for type& whose ancestor "
3703 & "has unknown discriminants ", N
, Typ
);
3706 if Has_Unknown_Discriminants
(Typ
)
3707 and then Present
(Underlying_Record_View
(Typ
))
3709 Discrim
:= First_Discriminant
(Underlying_Record_View
(Typ
));
3710 elsif Has_Discriminants
(Typ
) then
3711 Discrim
:= First_Discriminant
(Typ
);
3716 -- First find the discriminant values in the positional components
3718 while Present
(Discrim
) and then Present
(Positional_Expr
) loop
3719 if Discr_Present
(Discrim
) then
3720 Resolve_Aggr_Expr
(Positional_Expr
, Discrim
);
3722 -- Ada 2005 (AI-231)
3724 if Ada_Version
>= Ada_2005
3725 and then Known_Null
(Positional_Expr
)
3727 Check_Can_Never_Be_Null
(Discrim
, Positional_Expr
);
3730 Next
(Positional_Expr
);
3733 if Present
(Get_Value
(Discrim
, Component_Associations
(N
))) then
3735 ("more than one value supplied for discriminant&",
3739 Next_Discriminant
(Discrim
);
3742 -- Find remaining discriminant values if any among named components
3744 while Present
(Discrim
) loop
3745 Expr
:= Get_Value
(Discrim
, Component_Associations
(N
), True);
3747 if not Discr_Present
(Discrim
) then
3748 if Present
(Expr
) then
3750 ("more than one value supplied for discriminant&",
3754 elsif No
(Expr
) then
3756 ("no value supplied for discriminant &", N
, Discrim
);
3757 Missing_Discriminants
:= True;
3760 Resolve_Aggr_Expr
(Expr
, Discrim
);
3763 Next_Discriminant
(Discrim
);
3766 if Missing_Discriminants
then
3770 -- At this point and until the beginning of STEP 6, New_Assoc_List
3771 -- contains only the discriminants and their values.
3775 -- STEP 4: Set the Etype of the record aggregate
3777 -- ??? This code is pretty much a copy of Sem_Ch3.Build_Subtype. That
3778 -- routine should really be exported in sem_util or some such and used
3779 -- in sem_ch3 and here rather than have a copy of the code which is a
3780 -- maintenance nightmare.
3782 -- ??? Performance WARNING. The current implementation creates a new
3783 -- itype for all aggregates whose base type is discriminated. This means
3784 -- that for record aggregates nested inside an array aggregate we will
3785 -- create a new itype for each record aggregate if the array component
3786 -- type has discriminants. For large aggregates this may be a problem.
3787 -- What should be done in this case is to reuse itypes as much as
3790 if Has_Discriminants
(Typ
)
3791 or else (Has_Unknown_Discriminants
(Typ
)
3792 and then Present
(Underlying_Record_View
(Typ
)))
3794 Build_Constrained_Itype
: declare
3795 Loc
: constant Source_Ptr
:= Sloc
(N
);
3797 Subtyp_Decl
: Node_Id
;
3800 C
: constant List_Id
:= New_List
;
3803 New_Assoc
:= First
(New_Assoc_List
);
3804 while Present
(New_Assoc
) loop
3805 Append
(Duplicate_Subexpr
(Expression
(New_Assoc
)), To
=> C
);
3809 if Has_Unknown_Discriminants
(Typ
)
3810 and then Present
(Underlying_Record_View
(Typ
))
3813 Make_Subtype_Indication
(Loc
,
3815 New_Occurrence_Of
(Underlying_Record_View
(Typ
), Loc
),
3817 Make_Index_Or_Discriminant_Constraint
(Loc
, C
));
3820 Make_Subtype_Indication
(Loc
,
3822 New_Occurrence_Of
(Base_Type
(Typ
), Loc
),
3824 Make_Index_Or_Discriminant_Constraint
(Loc
, C
));
3827 Def_Id
:= Create_Itype
(Ekind
(Typ
), N
);
3830 Make_Subtype_Declaration
(Loc
,
3831 Defining_Identifier
=> Def_Id
,
3832 Subtype_Indication
=> Indic
);
3833 Set_Parent
(Subtyp_Decl
, Parent
(N
));
3835 -- Itypes must be analyzed with checks off (see itypes.ads)
3837 Analyze
(Subtyp_Decl
, Suppress
=> All_Checks
);
3839 Set_Etype
(N
, Def_Id
);
3840 Check_Static_Discriminated_Subtype
3841 (Def_Id
, Expression
(First
(New_Assoc_List
)));
3842 end Build_Constrained_Itype
;
3848 -- STEP 5: Get remaining components according to discriminant values
3851 Record_Def
: Node_Id
;
3852 Parent_Typ
: Entity_Id
;
3853 Root_Typ
: Entity_Id
;
3854 Parent_Typ_List
: Elist_Id
;
3855 Parent_Elmt
: Elmt_Id
;
3856 Errors_Found
: Boolean := False;
3859 function Find_Private_Ancestor
return Entity_Id
;
3860 -- AI05-0115: Find earlier ancestor in the derivation chain that is
3861 -- derived from a private view. Whether the aggregate is legal
3862 -- depends on the current visibility of the type as well as that
3863 -- of the parent of the ancestor.
3865 ---------------------------
3866 -- Find_Private_Ancestor --
3867 ---------------------------
3869 function Find_Private_Ancestor
return Entity_Id
is
3874 if Has_Private_Ancestor
(Par
)
3875 and then not Has_Private_Ancestor
(Etype
(Base_Type
(Par
)))
3879 elsif not Is_Derived_Type
(Par
) then
3883 Par
:= Etype
(Base_Type
(Par
));
3886 end Find_Private_Ancestor
;
3888 -- Start of processing for Step_5
3891 if Is_Derived_Type
(Typ
) and then Is_Tagged_Type
(Typ
) then
3892 Parent_Typ_List
:= New_Elmt_List
;
3894 -- If this is an extension aggregate, the component list must
3895 -- include all components that are not in the given ancestor type.
3896 -- Otherwise, the component list must include components of all
3897 -- ancestors, starting with the root.
3899 if Nkind
(N
) = N_Extension_Aggregate
then
3900 Root_Typ
:= Base_Type
(Etype
(Ancestor_Part
(N
)));
3903 -- AI05-0115: check legality of aggregate for type with
3904 -- aa private ancestor.
3906 Root_Typ
:= Root_Type
(Typ
);
3907 if Has_Private_Ancestor
(Typ
) then
3909 Ancestor
: constant Entity_Id
:=
3910 Find_Private_Ancestor
;
3911 Ancestor_Unit
: constant Entity_Id
:=
3912 Cunit_Entity
(Get_Source_Unit
(Ancestor
));
3913 Parent_Unit
: constant Entity_Id
:=
3915 (Get_Source_Unit
(Base_Type
(Etype
(Ancestor
))));
3918 -- check whether we are in a scope that has full view
3919 -- over the private ancestor and its parent. This can
3920 -- only happen if the derivation takes place in a child
3921 -- unit of the unit that declares the parent, and we are
3922 -- in the private part or body of that child unit, else
3923 -- the aggregate is illegal.
3925 if Is_Child_Unit
(Ancestor_Unit
)
3926 and then Scope
(Ancestor_Unit
) = Parent_Unit
3927 and then In_Open_Scopes
(Scope
(Ancestor
))
3929 (In_Private_Part
(Scope
(Ancestor
))
3930 or else In_Package_Body
(Scope
(Ancestor
)))
3936 ("type of aggregate has private ancestor&!",
3938 Error_Msg_N
("must use extension aggregate!", N
);
3944 Dnode
:= Declaration_Node
(Base_Type
(Root_Typ
));
3946 -- If we don't get a full declaration, then we have some error
3947 -- which will get signalled later so skip this part. Otherwise
3948 -- gather components of root that apply to the aggregate type.
3949 -- We use the base type in case there is an applicable stored
3950 -- constraint that renames the discriminants of the root.
3952 if Nkind
(Dnode
) = N_Full_Type_Declaration
then
3953 Record_Def
:= Type_Definition
(Dnode
);
3956 Component_List
(Record_Def
),
3957 Governed_By
=> New_Assoc_List
,
3959 Report_Errors
=> Errors_Found
);
3963 Parent_Typ
:= Base_Type
(Typ
);
3964 while Parent_Typ
/= Root_Typ
loop
3965 Prepend_Elmt
(Parent_Typ
, To
=> Parent_Typ_List
);
3966 Parent_Typ
:= Etype
(Parent_Typ
);
3968 if Nkind
(Parent
(Base_Type
(Parent_Typ
))) =
3969 N_Private_Type_Declaration
3970 or else Nkind
(Parent
(Base_Type
(Parent_Typ
))) =
3971 N_Private_Extension_Declaration
3973 if Nkind
(N
) /= N_Extension_Aggregate
then
3975 ("type of aggregate has private ancestor&!",
3977 Error_Msg_N
("must use extension aggregate!", N
);
3980 elsif Parent_Typ
/= Root_Typ
then
3982 ("ancestor part of aggregate must be private type&",
3983 Ancestor_Part
(N
), Parent_Typ
);
3987 -- The current view of ancestor part may be a private type,
3988 -- while the context type is always non-private.
3990 elsif Is_Private_Type
(Root_Typ
)
3991 and then Present
(Full_View
(Root_Typ
))
3992 and then Nkind
(N
) = N_Extension_Aggregate
3994 exit when Base_Type
(Full_View
(Root_Typ
)) = Parent_Typ
;
3998 -- Now collect components from all other ancestors, beginning
3999 -- with the current type. If the type has unknown discriminants
4000 -- use the component list of the Underlying_Record_View, which
4001 -- needs to be used for the subsequent expansion of the aggregate
4002 -- into assignments.
4004 Parent_Elmt
:= First_Elmt
(Parent_Typ_List
);
4005 while Present
(Parent_Elmt
) loop
4006 Parent_Typ
:= Node
(Parent_Elmt
);
4008 if Has_Unknown_Discriminants
(Parent_Typ
)
4009 and then Present
(Underlying_Record_View
(Typ
))
4011 Parent_Typ
:= Underlying_Record_View
(Parent_Typ
);
4014 Record_Def
:= Type_Definition
(Parent
(Base_Type
(Parent_Typ
)));
4015 Gather_Components
(Empty
,
4016 Component_List
(Record_Extension_Part
(Record_Def
)),
4017 Governed_By
=> New_Assoc_List
,
4019 Report_Errors
=> Errors_Found
);
4021 Next_Elmt
(Parent_Elmt
);
4024 -- Typ is not a derived tagged type
4027 -- A type derived from an untagged private type whose full view
4028 -- has discriminants is constructed as a record type but there
4029 -- are no legal aggregates for it.
4031 if Is_Derived_Type
(Typ
)
4032 and then Has_Private_Ancestor
(Typ
)
4033 and then Nkind
(N
) /= N_Extension_Aggregate
4035 Error_Msg_Node_2
:= Base_Type
(Etype
(Typ
));
4037 ("no aggregate available for type& derived from "
4038 & "private type&", N
, Typ
);
4042 Record_Def
:= Type_Definition
(Parent
(Base_Type
(Typ
)));
4044 if Null_Present
(Record_Def
) then
4047 elsif not Has_Unknown_Discriminants
(Typ
) then
4050 Component_List
(Record_Def
),
4051 Governed_By
=> New_Assoc_List
,
4053 Report_Errors
=> Errors_Found
);
4057 (Base_Type
(Underlying_Record_View
(Typ
)),
4058 Component_List
(Record_Def
),
4059 Governed_By
=> New_Assoc_List
,
4061 Report_Errors
=> Errors_Found
);
4065 if Errors_Found
then
4070 -- STEP 6: Find component Values
4073 Component_Elmt
:= First_Elmt
(Components
);
4075 -- First scan the remaining positional associations in the aggregate.
4076 -- Remember that at this point Positional_Expr contains the current
4077 -- positional association if any is left after looking for discriminant
4078 -- values in step 3.
4080 while Present
(Positional_Expr
) and then Present
(Component_Elmt
) loop
4081 Component
:= Node
(Component_Elmt
);
4082 Resolve_Aggr_Expr
(Positional_Expr
, Component
);
4084 -- Ada 2005 (AI-231)
4086 if Ada_Version
>= Ada_2005
4087 and then Known_Null
(Positional_Expr
)
4089 Check_Can_Never_Be_Null
(Component
, Positional_Expr
);
4092 if Present
(Get_Value
(Component
, Component_Associations
(N
))) then
4094 ("more than one value supplied for Component &", N
, Component
);
4097 Next
(Positional_Expr
);
4098 Next_Elmt
(Component_Elmt
);
4101 if Present
(Positional_Expr
) then
4103 ("too many components for record aggregate", Positional_Expr
);
4106 -- Now scan for the named arguments of the aggregate
4108 while Present
(Component_Elmt
) loop
4109 Component
:= Node
(Component_Elmt
);
4110 Expr
:= Get_Value
(Component
, Component_Associations
(N
), True);
4112 -- Note: The previous call to Get_Value sets the value of the
4113 -- variable Is_Box_Present.
4115 -- Ada 2005 (AI-287): Handle components with default initialization.
4116 -- Note: This feature was originally added to Ada 2005 for limited
4117 -- but it was finally allowed with any type.
4119 if Is_Box_Present
then
4120 Check_Box_Component
: declare
4121 Ctyp
: constant Entity_Id
:= Etype
(Component
);
4124 -- If there is a default expression for the aggregate, copy
4125 -- it into a new association. This copy must modify the scopes
4126 -- of internal types that may be attached to the expression
4127 -- (e.g. index subtypes of arrays) because in general the type
4128 -- declaration and the aggregate appear in different scopes,
4129 -- and the backend requires the scope of the type to match the
4130 -- point at which it is elaborated.
4132 -- If the component has an initialization procedure (IP) we
4133 -- pass the component to the expander, which will generate
4134 -- the call to such IP.
4136 -- If the component has discriminants, their values must
4137 -- be taken from their subtype. This is indispensable for
4138 -- constraints that are given by the current instance of an
4139 -- enclosing type, to allow the expansion of the aggregate to
4140 -- replace the reference to the current instance by the target
4141 -- object of the aggregate.
4143 if Present
(Parent
(Component
))
4145 Nkind
(Parent
(Component
)) = N_Component_Declaration
4146 and then Present
(Expression
(Parent
(Component
)))
4149 New_Copy_Tree_And_Copy_Dimensions
4150 (Expression
(Parent
(Component
)),
4151 New_Scope
=> Current_Scope
,
4152 New_Sloc
=> Sloc
(N
));
4155 (Component
=> Component
,
4157 Assoc_List
=> New_Assoc_List
);
4158 Set_Has_Self_Reference
(N
);
4160 -- A box-defaulted access component gets the value null. Also
4161 -- included are components of private types whose underlying
4162 -- type is an access type. In either case set the type of the
4163 -- literal, for subsequent use in semantic checks.
4165 elsif Present
(Underlying_Type
(Ctyp
))
4166 and then Is_Access_Type
(Underlying_Type
(Ctyp
))
4168 if not Is_Private_Type
(Ctyp
) then
4169 Expr
:= Make_Null
(Sloc
(N
));
4170 Set_Etype
(Expr
, Ctyp
);
4172 (Component
=> Component
,
4174 Assoc_List
=> New_Assoc_List
);
4176 -- If the component's type is private with an access type as
4177 -- its underlying type then we have to create an unchecked
4178 -- conversion to satisfy type checking.
4182 Qual_Null
: constant Node_Id
:=
4183 Make_Qualified_Expression
(Sloc
(N
),
4186 (Underlying_Type
(Ctyp
), Sloc
(N
)),
4187 Expression
=> Make_Null
(Sloc
(N
)));
4189 Convert_Null
: constant Node_Id
:=
4190 Unchecked_Convert_To
4194 Analyze_And_Resolve
(Convert_Null
, Ctyp
);
4196 (Component
=> Component
,
4197 Expr
=> Convert_Null
,
4198 Assoc_List
=> New_Assoc_List
);
4202 elsif Has_Non_Null_Base_Init_Proc
(Ctyp
)
4203 or else not Expander_Active
4205 if Is_Record_Type
(Ctyp
)
4206 and then Has_Discriminants
(Ctyp
)
4207 and then not Is_Private_Type
(Ctyp
)
4209 -- We build a partially initialized aggregate with the
4210 -- values of the discriminants and box initialization
4211 -- for the rest, if other components are present.
4213 -- The type of the aggregate is the known subtype of
4214 -- the component. The capture of discriminants must
4215 -- be recursive because subcomponents may be constrained
4216 -- (transitively) by discriminants of enclosing types.
4217 -- For a private type with discriminants, a call to the
4218 -- initialization procedure will be generated, and no
4219 -- subaggregate is needed.
4221 Capture_Discriminants
: declare
4222 Loc
: constant Source_Ptr
:= Sloc
(N
);
4225 procedure Add_Discriminant_Values
4226 (New_Aggr
: Node_Id
;
4227 Assoc_List
: List_Id
);
4228 -- The constraint to a component may be given by a
4229 -- discriminant of the enclosing type, in which case
4230 -- we have to retrieve its value, which is part of the
4231 -- enclosing aggregate. Assoc_List provides the
4232 -- discriminant associations of the current type or
4233 -- of some enclosing record.
4235 procedure Propagate_Discriminants
4237 Assoc_List
: List_Id
);
4238 -- Nested components may themselves be discriminated
4239 -- types constrained by outer discriminants, whose
4240 -- values must be captured before the aggregate is
4241 -- expanded into assignments.
4243 -----------------------------
4244 -- Add_Discriminant_Values --
4245 -----------------------------
4247 procedure Add_Discriminant_Values
4248 (New_Aggr
: Node_Id
;
4249 Assoc_List
: List_Id
)
4253 Discr_Elmt
: Elmt_Id
;
4254 Discr_Val
: Node_Id
;
4258 Discr
:= First_Discriminant
(Etype
(New_Aggr
));
4261 (Discriminant_Constraint
(Etype
(New_Aggr
)));
4262 while Present
(Discr_Elmt
) loop
4263 Discr_Val
:= Node
(Discr_Elmt
);
4265 -- If the constraint is given by a discriminant
4266 -- it is a discriminant of an enclosing record,
4267 -- and its value has already been placed in the
4268 -- association list.
4270 if Is_Entity_Name
(Discr_Val
)
4272 Ekind
(Entity
(Discr_Val
)) = E_Discriminant
4274 Val
:= Entity
(Discr_Val
);
4276 Assoc
:= First
(Assoc_List
);
4277 while Present
(Assoc
) loop
4279 (Entity
(First
(Choices
(Assoc
))))
4281 Entity
(First
(Choices
(Assoc
)))
4284 Discr_Val
:= Expression
(Assoc
);
4292 (Discr
, New_Copy_Tree
(Discr_Val
),
4293 Component_Associations
(New_Aggr
));
4295 -- If the discriminant constraint is a current
4296 -- instance, mark the current aggregate so that
4297 -- the self-reference can be expanded later.
4299 if Nkind
(Discr_Val
) = N_Attribute_Reference
4300 and then Is_Entity_Name
(Prefix
(Discr_Val
))
4301 and then Is_Type
(Entity
(Prefix
(Discr_Val
)))
4302 and then Etype
(N
) =
4303 Entity
(Prefix
(Discr_Val
))
4305 Set_Has_Self_Reference
(N
);
4308 Next_Elmt
(Discr_Elmt
);
4309 Next_Discriminant
(Discr
);
4311 end Add_Discriminant_Values
;
4313 ------------------------------
4314 -- Propagate_Discriminants --
4315 ------------------------------
4317 procedure Propagate_Discriminants
4319 Assoc_List
: List_Id
)
4321 Aggr_Type
: constant Entity_Id
:=
4322 Base_Type
(Etype
(Aggr
));
4323 Def_Node
: constant Node_Id
:=
4325 (Declaration_Node
(Aggr_Type
));
4328 Comp_Elmt
: Elmt_Id
;
4329 Components
: constant Elist_Id
:= New_Elmt_List
;
4330 Needs_Box
: Boolean := False;
4333 procedure Process_Component
(Comp
: Entity_Id
);
4334 -- Add one component with a box association to the
4335 -- inner aggregate, and recurse if component is
4336 -- itself composite.
4338 ------------------------
4339 -- Process_Component --
4340 ------------------------
4342 procedure Process_Component
(Comp
: Entity_Id
) is
4343 T
: constant Entity_Id
:= Etype
(Comp
);
4347 if Is_Record_Type
(T
)
4348 and then Has_Discriminants
(T
)
4351 Make_Aggregate
(Loc
, New_List
, New_List
);
4352 Set_Etype
(New_Aggr
, T
);
4355 Component_Associations
(Aggr
));
4357 -- Collect discriminant values and recurse
4359 Add_Discriminant_Values
4360 (New_Aggr
, Assoc_List
);
4361 Propagate_Discriminants
4362 (New_Aggr
, Assoc_List
);
4367 end Process_Component
;
4369 -- Start of processing for Propagate_Discriminants
4372 -- The component type may be a variant type, so
4373 -- collect the components that are ruled by the
4374 -- known values of the discriminants. Their values
4375 -- have already been inserted into the component
4376 -- list of the current aggregate.
4378 if Nkind
(Def_Node
) = N_Record_Definition
4380 Present
(Component_List
(Def_Node
))
4383 (Variant_Part
(Component_List
(Def_Node
)))
4385 Gather_Components
(Aggr_Type
,
4386 Component_List
(Def_Node
),
4387 Governed_By
=> Component_Associations
(Aggr
),
4389 Report_Errors
=> Errors
);
4391 Comp_Elmt
:= First_Elmt
(Components
);
4392 while Present
(Comp_Elmt
) loop
4394 Ekind
(Node
(Comp_Elmt
)) /= E_Discriminant
4396 Process_Component
(Node
(Comp_Elmt
));
4399 Next_Elmt
(Comp_Elmt
);
4402 -- No variant part, iterate over all components
4405 Comp
:= First_Component
(Etype
(Aggr
));
4406 while Present
(Comp
) loop
4407 Process_Component
(Comp
);
4408 Next_Component
(Comp
);
4414 (Make_Component_Association
(Loc
,
4416 New_List
(Make_Others_Choice
(Loc
)),
4417 Expression
=> Empty
,
4418 Box_Present
=> True),
4419 Component_Associations
(Aggr
));
4421 end Propagate_Discriminants
;
4423 -- Start of processing for Capture_Discriminants
4426 Expr
:= Make_Aggregate
(Loc
, New_List
, New_List
);
4427 Set_Etype
(Expr
, Ctyp
);
4429 -- If the enclosing type has discriminants, they have
4430 -- been collected in the aggregate earlier, and they
4431 -- may appear as constraints of subcomponents.
4433 -- Similarly if this component has discriminants, they
4434 -- might in turn be propagated to their components.
4436 if Has_Discriminants
(Typ
) then
4437 Add_Discriminant_Values
(Expr
, New_Assoc_List
);
4438 Propagate_Discriminants
(Expr
, New_Assoc_List
);
4440 elsif Has_Discriminants
(Ctyp
) then
4441 Add_Discriminant_Values
4442 (Expr
, Component_Associations
(Expr
));
4443 Propagate_Discriminants
4444 (Expr
, Component_Associations
(Expr
));
4451 -- If the type has additional components, create
4452 -- an OTHERS box association for them.
4454 Comp
:= First_Component
(Ctyp
);
4455 while Present
(Comp
) loop
4456 if Ekind
(Comp
) = E_Component
then
4457 if not Is_Record_Type
(Etype
(Comp
)) then
4459 (Make_Component_Association
(Loc
,
4462 (Make_Others_Choice
(Loc
)),
4463 Expression
=> Empty
,
4464 Box_Present
=> True),
4465 Component_Associations
(Expr
));
4470 Next_Component
(Comp
);
4476 (Component
=> Component
,
4478 Assoc_List
=> New_Assoc_List
);
4479 end Capture_Discriminants
;
4483 (Component
=> Component
,
4485 Assoc_List
=> New_Assoc_List
,
4486 Is_Box_Present
=> True);
4489 -- Otherwise we only need to resolve the expression if the
4490 -- component has partially initialized values (required to
4491 -- expand the corresponding assignments and run-time checks).
4493 elsif Present
(Expr
)
4494 and then Is_Partially_Initialized_Type
(Ctyp
)
4496 Resolve_Aggr_Expr
(Expr
, Component
);
4498 end Check_Box_Component
;
4500 elsif No
(Expr
) then
4502 -- Ignore hidden components associated with the position of the
4503 -- interface tags: these are initialized dynamically.
4505 if not Present
(Related_Type
(Component
)) then
4507 ("no value supplied for component &!", N
, Component
);
4511 Resolve_Aggr_Expr
(Expr
, Component
);
4514 Next_Elmt
(Component_Elmt
);
4517 -- STEP 7: check for invalid components + check type in choice list
4524 -- Type of first component in choice list
4527 if Present
(Component_Associations
(N
)) then
4528 Assoc
:= First
(Component_Associations
(N
));
4533 Verification
: while Present
(Assoc
) loop
4534 Selectr
:= First
(Choices
(Assoc
));
4537 if Nkind
(Selectr
) = N_Others_Choice
then
4539 -- Ada 2005 (AI-287): others choice may have expression or box
4541 if No
(Others_Etype
)
4542 and then not Others_Box
4545 ("OTHERS must represent at least one component", Selectr
);
4551 while Present
(Selectr
) loop
4552 New_Assoc
:= First
(New_Assoc_List
);
4553 while Present
(New_Assoc
) loop
4554 Component
:= First
(Choices
(New_Assoc
));
4556 if Chars
(Selectr
) = Chars
(Component
) then
4558 Check_Identifier
(Selectr
, Entity
(Component
));
4567 -- If no association, this is not a legal component of the type
4568 -- in question, unless its association is provided with a box.
4570 if No
(New_Assoc
) then
4571 if Box_Present
(Parent
(Selectr
)) then
4573 -- This may still be a bogus component with a box. Scan
4574 -- list of components to verify that a component with
4575 -- that name exists.
4581 C
:= First_Component
(Typ
);
4582 while Present
(C
) loop
4583 if Chars
(C
) = Chars
(Selectr
) then
4585 -- If the context is an extension aggregate,
4586 -- the component must not be inherited from
4587 -- the ancestor part of the aggregate.
4589 if Nkind
(N
) /= N_Extension_Aggregate
4591 Scope
(Original_Record_Component
(C
)) /=
4592 Etype
(Ancestor_Part
(N
))
4602 Error_Msg_Node_2
:= Typ
;
4603 Error_Msg_N
("& is not a component of}", Selectr
);
4607 elsif Chars
(Selectr
) /= Name_uTag
4608 and then Chars
(Selectr
) /= Name_uParent
4610 if not Has_Discriminants
(Typ
) then
4611 Error_Msg_Node_2
:= Typ
;
4612 Error_Msg_N
("& is not a component of}", Selectr
);
4615 ("& is not a component of the aggregate subtype",
4619 Check_Misspelled_Component
(Components
, Selectr
);
4622 elsif No
(Typech
) then
4623 Typech
:= Base_Type
(Etype
(Component
));
4625 -- AI05-0199: In Ada 2012, several components of anonymous
4626 -- access types can appear in a choice list, as long as the
4627 -- designated types match.
4629 elsif Typech
/= Base_Type
(Etype
(Component
)) then
4630 if Ada_Version
>= Ada_2012
4631 and then Ekind
(Typech
) = E_Anonymous_Access_Type
4633 Ekind
(Etype
(Component
)) = E_Anonymous_Access_Type
4634 and then Base_Type
(Designated_Type
(Typech
)) =
4635 Base_Type
(Designated_Type
(Etype
(Component
)))
4637 Subtypes_Statically_Match
(Typech
, (Etype
(Component
)))
4641 elsif not Box_Present
(Parent
(Selectr
)) then
4643 ("components in choice list must have same type",
4652 end loop Verification
;
4655 -- STEP 8: replace the original aggregate
4658 New_Aggregate
: constant Node_Id
:= New_Copy
(N
);
4661 Set_Expressions
(New_Aggregate
, No_List
);
4662 Set_Etype
(New_Aggregate
, Etype
(N
));
4663 Set_Component_Associations
(New_Aggregate
, New_Assoc_List
);
4665 Rewrite
(N
, New_Aggregate
);
4668 -- Check the dimensions of the components in the record aggregate
4670 Analyze_Dimension_Extension_Or_Record_Aggregate
(N
);
4671 end Resolve_Record_Aggregate
;
4673 -----------------------------
4674 -- Check_Can_Never_Be_Null --
4675 -----------------------------
4677 procedure Check_Can_Never_Be_Null
(Typ
: Entity_Id
; Expr
: Node_Id
) is
4678 Comp_Typ
: Entity_Id
;
4682 (Ada_Version
>= Ada_2005
4683 and then Present
(Expr
)
4684 and then Known_Null
(Expr
));
4687 when E_Array_Type
=>
4688 Comp_Typ
:= Component_Type
(Typ
);
4692 Comp_Typ
:= Etype
(Typ
);
4698 if Can_Never_Be_Null
(Comp_Typ
) then
4700 -- Here we know we have a constraint error. Note that we do not use
4701 -- Apply_Compile_Time_Constraint_Error here to the Expr, which might
4702 -- seem the more natural approach. That's because in some cases the
4703 -- components are rewritten, and the replacement would be missed.
4706 (Compile_Time_Constraint_Error
4708 "(Ada 2005) null not allowed in null-excluding component??"),
4709 Make_Raise_Constraint_Error
4710 (Sloc
(Expr
), Reason
=> CE_Access_Check_Failed
));
4712 -- Set proper type for bogus component (why is this needed???)
4714 Set_Etype
(Expr
, Comp_Typ
);
4715 Set_Analyzed
(Expr
);
4717 end Check_Can_Never_Be_Null
;
4719 ---------------------
4720 -- Sort_Case_Table --
4721 ---------------------
4723 procedure Sort_Case_Table
(Case_Table
: in out Case_Table_Type
) is
4724 U
: constant Int
:= Case_Table
'Last;
4732 T
:= Case_Table
(K
+ 1);
4736 and then Expr_Value
(Case_Table
(J
- 1).Lo
) > Expr_Value
(T
.Lo
)
4738 Case_Table
(J
) := Case_Table
(J
- 1);
4742 Case_Table
(J
) := T
;
4745 end Sort_Case_Table
;