1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2002 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 2, 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 COPYING. If not, write --
19 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
27 with Atree
; use Atree
;
28 with Checks
; use Checks
;
29 with Einfo
; use Einfo
;
30 with Elists
; use Elists
;
31 with Errout
; use Errout
;
32 with Exp_Util
; use Exp_Util
;
33 with Freeze
; use Freeze
;
34 with Itypes
; use Itypes
;
35 with Namet
; use Namet
;
36 with Nmake
; use Nmake
;
37 with Nlists
; use Nlists
;
40 with Sem_Cat
; use Sem_Cat
;
41 with Sem_Ch8
; use Sem_Ch8
;
42 with Sem_Ch13
; use Sem_Ch13
;
43 with Sem_Eval
; use Sem_Eval
;
44 with Sem_Res
; use Sem_Res
;
45 with Sem_Util
; use Sem_Util
;
46 with Sem_Type
; use Sem_Type
;
47 with Sinfo
; use Sinfo
;
48 with Snames
; use Snames
;
49 with Stringt
; use Stringt
;
50 with Stand
; use Stand
;
51 with Tbuild
; use Tbuild
;
52 with Uintp
; use Uintp
;
54 with GNAT
.Spelling_Checker
; use GNAT
.Spelling_Checker
;
56 package body Sem_Aggr
is
58 type Case_Bounds
is record
61 Choice_Node
: Node_Id
;
64 type Case_Table_Type
is array (Nat
range <>) of Case_Bounds
;
65 -- Table type used by Check_Case_Choices procedure
67 -----------------------
68 -- Local Subprograms --
69 -----------------------
71 procedure Sort_Case_Table
(Case_Table
: in out Case_Table_Type
);
72 -- Sort the Case Table using the Lower Bound of each Choice as the key.
73 -- A simple insertion sort is used since the number of choices in a case
74 -- statement of variant part will usually be small and probably in near
77 ------------------------------------------------------
78 -- Subprograms used for RECORD AGGREGATE Processing --
79 ------------------------------------------------------
81 procedure Resolve_Record_Aggregate
(N
: Node_Id
; Typ
: Entity_Id
);
82 -- This procedure performs all the semantic checks required for record
83 -- aggregates. Note that for aggregates analysis and resolution go
84 -- hand in hand. Aggregate analysis has been delayed up to here and
85 -- it is done while resolving the aggregate.
87 -- N is the N_Aggregate node.
88 -- Typ is the record type for the aggregate resolution
90 -- While performing the semantic checks, this procedure
91 -- builds a new Component_Association_List where each record field
92 -- appears alone in a Component_Choice_List along with its corresponding
93 -- expression. The record fields in the Component_Association_List
94 -- appear in the same order in which they appear in the record type Typ.
96 -- Once this new Component_Association_List is built and all the
97 -- semantic checks performed, the original aggregate subtree is replaced
98 -- with the new named record aggregate just built. Note that the subtree
99 -- substitution is performed with Rewrite so as to be
100 -- able to retrieve the original aggregate.
102 -- The aggregate subtree manipulation performed by Resolve_Record_Aggregate
103 -- yields the aggregate format expected by Gigi. Typically, this kind of
104 -- tree manipulations are done in the expander. However, because the
105 -- semantic checks that need to be performed on record aggregates really
106 -- go hand in hand with the record aggreagate normalization, the aggregate
107 -- subtree transformation is performed during resolution rather than
108 -- expansion. Had we decided otherwise we would have had to duplicate
109 -- most of the code in the expansion procedure Expand_Record_Aggregate.
110 -- Note, however, that all the expansion concerning aggegates for tagged
111 -- records is done in Expand_Record_Aggregate.
113 -- The algorithm of Resolve_Record_Aggregate proceeds as follows:
115 -- 1. Make sure that the record type against which the record aggregate
116 -- has to be resolved is not abstract. Furthermore if the type is
117 -- a null aggregate make sure the input aggregate N is also null.
119 -- 2. Verify that the structure of the aggregate is that of a record
120 -- aggregate. Specifically, look for component associations and ensure
121 -- that each choice list only has identifiers or the N_Others_Choice
122 -- node. Also make sure that if present, the N_Others_Choice occurs
123 -- last and by itself.
125 -- 3. If Typ contains discriminants, the values for each discriminant
126 -- is looked for. If the record type Typ has variants, we check
127 -- that the expressions corresponding to each discriminant ruling
128 -- the (possibly nested) variant parts of Typ, are static. This
129 -- allows us to determine the variant parts to which the rest of
130 -- the aggregate must conform. The names of discriminants with their
131 -- values are saved in a new association list, New_Assoc_List which
132 -- is later augmented with the names and values of the remaining
133 -- components in the record type.
135 -- During this phase we also make sure that every discriminant is
136 -- assigned exactly one value. Note that when several values
137 -- for a given discriminant are found, semantic processing continues
138 -- looking for further errors. In this case it's the first
139 -- discriminant value found which we will be recorded.
141 -- IMPORTANT NOTE: For derived tagged types this procedure expects
142 -- First_Discriminant and Next_Discriminant to give the correct list
143 -- of discriminants, in the correct order.
145 -- 4. After all the discriminant values have been gathered, we can
146 -- set the Etype of the record aggregate. If Typ contains no
147 -- discriminants this is straightforward: the Etype of N is just
148 -- Typ, otherwise a new implicit constrained subtype of Typ is
149 -- built to be the Etype of N.
151 -- 5. Gather the remaining record components according to the discriminant
152 -- values. This involves recursively traversing the record type
153 -- structure to see what variants are selected by the given discriminant
154 -- values. This processing is a little more convoluted if Typ is a
155 -- derived tagged types since we need to retrieve the record structure
156 -- of all the ancestors of Typ.
158 -- 6. After gathering the record components we look for their values
159 -- in the record aggregate and emit appropriate error messages
160 -- should we not find such values or should they be duplicated.
162 -- 7. We then make sure no illegal component names appear in the
163 -- record aggegate and make sure that the type of the record
164 -- components appearing in a same choice list is the same.
165 -- Finally we ensure that the others choice, if present, is
166 -- used to provide the value of at least a record component.
168 -- 8. The original aggregate node is replaced with the new named
169 -- aggregate built in steps 3 through 6, as explained earlier.
171 -- Given the complexity of record aggregate resolution, the primary
172 -- goal of this routine is clarity and simplicity rather than execution
173 -- and storage efficiency. If there are only positional components in the
174 -- aggregate the running time is linear. If there are associations
175 -- the running time is still linear as long as the order of the
176 -- associations is not too far off the order of the components in the
177 -- record type. If this is not the case the running time is at worst
178 -- quadratic in the size of the association list.
180 procedure Check_Misspelled_Component
181 (Elements
: Elist_Id
;
182 Component
: Node_Id
);
183 -- Give possible misspelling diagnostic if Component is likely to be
184 -- a misspelling of one of the components of the Assoc_List.
185 -- This is called by Resolv_Aggr_Expr after producing
186 -- an invalid component error message.
188 procedure Check_Static_Discriminated_Subtype
(T
: Entity_Id
; V
: Node_Id
);
189 -- An optimization: determine whether a discriminated subtype has a
190 -- static constraint, and contains array components whose length is also
191 -- static, either because they are constrained by the discriminant, or
192 -- because the original component bounds are static.
194 -----------------------------------------------------
195 -- Subprograms used for ARRAY AGGREGATE Processing --
196 -----------------------------------------------------
198 function Resolve_Array_Aggregate
201 Index_Constr
: Node_Id
;
202 Component_Typ
: Entity_Id
;
203 Others_Allowed
: Boolean)
205 -- This procedure performs the semantic checks for an array aggregate.
206 -- True is returned if the aggregate resolution succeeds.
207 -- The procedure works by recursively checking each nested aggregate.
208 -- Specifically, after checking a sub-aggreate nested at the i-th level
209 -- we recursively check all the subaggregates at the i+1-st level (if any).
210 -- Note that for aggregates analysis and resolution go hand in hand.
211 -- Aggregate analysis has been delayed up to here and it is done while
212 -- resolving the aggregate.
214 -- N is the current N_Aggregate node to be checked.
216 -- Index is the index node corresponding to the array sub-aggregate that
217 -- we are currently checking (RM 4.3.3 (8)). Its Etype is the
218 -- corresponding index type (or subtype).
220 -- Index_Constr is the node giving the applicable index constraint if
221 -- any (RM 4.3.3 (10)). It "is a constraint provided by certain
222 -- contexts [...] that can be used to determine the bounds of the array
223 -- value specified by the aggregate". If Others_Allowed below is False
224 -- there is no applicable index constraint and this node is set to Index.
226 -- Component_Typ is the array component type.
228 -- Others_Allowed indicates whether an others choice is allowed
229 -- in the context where the top-level aggregate appeared.
231 -- The algorithm of Resolve_Array_Aggregate proceeds as follows:
233 -- 1. Make sure that the others choice, if present, is by itself and
234 -- appears last in the sub-aggregate. Check that we do not have
235 -- positional and named components in the array sub-aggregate (unless
236 -- the named association is an others choice). Finally if an others
237 -- choice is present, make sure it is allowed in the aggregate contex.
239 -- 2. If the array sub-aggregate contains discrete_choices:
241 -- (A) Verify their validity. Specifically verify that:
243 -- (a) If a null range is present it must be the only possible
244 -- choice in the array aggregate.
246 -- (b) Ditto for a non static range.
248 -- (c) Ditto for a non static expression.
250 -- In addition this step analyzes and resolves each discrete_choice,
251 -- making sure that its type is the type of the corresponding Index.
252 -- If we are not at the lowest array aggregate level (in the case of
253 -- multi-dimensional aggregates) then invoke Resolve_Array_Aggregate
254 -- recursively on each component expression. Otherwise, resolve the
255 -- bottom level component expressions against the expected component
256 -- type ONLY IF the component corresponds to a single discrete choice
257 -- which is not an others choice (to see why read the DELAYED
258 -- COMPONENT RESOLUTION below).
260 -- (B) Determine the bounds of the sub-aggregate and lowest and
261 -- highest choice values.
263 -- 3. For positional aggregates:
265 -- (A) Loop over the component expressions either recursively invoking
266 -- Resolve_Array_Aggregate on each of these for multi-dimensional
267 -- array aggregates or resolving the bottom level component
268 -- expressions against the expected component type.
270 -- (B) Determine the bounds of the positional sub-aggregates.
272 -- 4. Try to determine statically whether the evaluation of the array
273 -- sub-aggregate raises Constraint_Error. If yes emit proper
274 -- warnings. The precise checks are the following:
276 -- (A) Check that the index range defined by aggregate bounds is
277 -- compatible with corresponding index subtype.
278 -- We also check against the base type. In fact it could be that
279 -- Low/High bounds of the base type are static whereas those of
280 -- the index subtype are not. Thus if we can statically catch
281 -- a problem with respect to the base type we are guaranteed
282 -- that the same problem will arise with the index subtype
284 -- (B) If we are dealing with a named aggregate containing an others
285 -- choice and at least one discrete choice then make sure the range
286 -- specified by the discrete choices does not overflow the
287 -- aggregate bounds. We also check against the index type and base
288 -- type bounds for the same reasons given in (A).
290 -- (C) If we are dealing with a positional aggregate with an others
291 -- choice make sure the number of positional elements specified
292 -- does not overflow the aggregate bounds. We also check against
293 -- the index type and base type bounds as mentioned in (A).
295 -- Finally construct an N_Range node giving the sub-aggregate bounds.
296 -- Set the Aggregate_Bounds field of the sub-aggregate to be this
297 -- N_Range. The routine Array_Aggr_Subtype below uses such N_Ranges
298 -- to build the appropriate aggregate subtype. Aggregate_Bounds
299 -- information is needed during expansion.
301 -- DELAYED COMPONENT RESOLUTION: The resolution of bottom level component
302 -- expressions in an array aggregate may call Duplicate_Subexpr or some
303 -- other routine that inserts code just outside the outermost aggregate.
304 -- If the array aggregate contains discrete choices or an others choice,
305 -- this may be wrong. Consider for instance the following example.
307 -- type Rec is record
311 -- type Acc_Rec is access Rec;
312 -- Arr : array (1..3) of Acc_Rec := (1 .. 3 => new Rec);
314 -- Then the transformation of "new Rec" that occurs during resolution
315 -- entails the following code modifications
317 -- P7b : constant Acc_Rec := new Rec;
318 -- Rec_init_proc (P7b.all);
319 -- Arr : array (1..3) of Acc_Rec := (1 .. 3 => P7b);
321 -- This code transformation is clearly wrong, since we need to call
322 -- "new Rec" for each of the 3 array elements. To avoid this problem we
323 -- delay resolution of the components of non positional array aggregates
324 -- to the expansion phase. As an optimization, if the discrete choice
325 -- specifies a single value we do not delay resolution.
327 function Array_Aggr_Subtype
(N
: Node_Id
; Typ
: Node_Id
) return Entity_Id
;
328 -- This routine returns the type or subtype of an array aggregate.
330 -- N is the array aggregate node whose type we return.
332 -- Typ is the context type in which N occurs.
334 -- This routine creates an implicit array subtype whose bouds are
335 -- those defined by the aggregate. When this routine is invoked
336 -- Resolve_Array_Aggregate has already processed aggregate N. Thus the
337 -- Aggregate_Bounds of each sub-aggregate, is an N_Range node giving the
338 -- sub-aggregate bounds. When building the aggegate itype, this function
339 -- traverses the array aggregate N collecting such Aggregate_Bounds and
340 -- constructs the proper array aggregate itype.
342 -- Note that in the case of multidimensional aggregates each inner
343 -- sub-aggregate corresponding to a given array dimension, may provide a
344 -- different bounds. If it is possible to determine statically that
345 -- some sub-aggregates corresponding to the same index do not have the
346 -- same bounds, then a warning is emitted. If such check is not possible
347 -- statically (because some sub-aggregate bounds are dynamic expressions)
348 -- then this job is left to the expander. In all cases the particular
349 -- bounds that this function will chose for a given dimension is the first
350 -- N_Range node for a sub-aggregate corresponding to that dimension.
352 -- Note that the Raises_Constraint_Error flag of an array aggregate
353 -- whose evaluation is determined to raise CE by Resolve_Array_Aggregate,
354 -- is set in Resolve_Array_Aggregate but the aggregate is not
355 -- immediately replaced with a raise CE. In fact, Array_Aggr_Subtype must
356 -- first construct the proper itype for the aggregate (Gigi needs
357 -- this). After constructing the proper itype we will eventually replace
358 -- the top-level aggregate with a raise CE (done in Resolve_Aggregate).
359 -- Of course in cases such as:
361 -- type Arr is array (integer range <>) of Integer;
362 -- A : Arr := (positive range -1 .. 2 => 0);
364 -- The bounds of the aggregate itype are cooked up to look reasonable
365 -- (in this particular case the bounds will be 1 .. 2).
367 procedure Aggregate_Constraint_Checks
369 Check_Typ
: Entity_Id
);
370 -- Checks expression Exp against subtype Check_Typ. If Exp is an
371 -- aggregate and Check_Typ a constrained record type with discriminants,
372 -- we generate the appropriate discriminant checks. If Exp is an array
373 -- aggregate then emit the appropriate length checks. If Exp is a scalar
374 -- type, or a string literal, Exp is changed into Check_Typ'(Exp) to
375 -- ensure that range checks are performed at run time.
377 procedure Make_String_Into_Aggregate
(N
: Node_Id
);
378 -- A string literal can appear in a context in which a one dimensional
379 -- array of characters is expected. This procedure simply rewrites the
380 -- string as an aggregate, prior to resolution.
382 ---------------------------------
383 -- Aggregate_Constraint_Checks --
384 ---------------------------------
386 procedure Aggregate_Constraint_Checks
388 Check_Typ
: Entity_Id
)
390 Exp_Typ
: constant Entity_Id
:= Etype
(Exp
);
393 if Raises_Constraint_Error
(Exp
) then
397 -- This is really expansion activity, so make sure that expansion
398 -- is on and is allowed.
400 if not Expander_Active
or else In_Default_Expression
then
404 -- First check if we have to insert discriminant checks
406 if Has_Discriminants
(Exp_Typ
) then
407 Apply_Discriminant_Check
(Exp
, Check_Typ
);
409 -- Next emit length checks for array aggregates
411 elsif Is_Array_Type
(Exp_Typ
) then
412 Apply_Length_Check
(Exp
, Check_Typ
);
414 -- Finally emit scalar and string checks. If we are dealing with a
415 -- scalar literal we need to check by hand because the Etype of
416 -- literals is not necessarily correct.
418 elsif Is_Scalar_Type
(Exp_Typ
)
419 and then Compile_Time_Known_Value
(Exp
)
421 if Is_Out_Of_Range
(Exp
, Base_Type
(Check_Typ
)) then
422 Apply_Compile_Time_Constraint_Error
423 (Exp
, "value not in range of}?", CE_Range_Check_Failed
,
424 Ent
=> Base_Type
(Check_Typ
),
425 Typ
=> Base_Type
(Check_Typ
));
427 elsif Is_Out_Of_Range
(Exp
, Check_Typ
) then
428 Apply_Compile_Time_Constraint_Error
429 (Exp
, "value not in range of}?", CE_Range_Check_Failed
,
433 elsif not Range_Checks_Suppressed
(Check_Typ
) then
434 Apply_Scalar_Range_Check
(Exp
, Check_Typ
);
437 elsif (Is_Scalar_Type
(Exp_Typ
)
438 or else Nkind
(Exp
) = N_String_Literal
)
439 and then Exp_Typ
/= Check_Typ
441 if Is_Entity_Name
(Exp
)
442 and then Ekind
(Entity
(Exp
)) = E_Constant
444 -- If expression is a constant, it is worthwhile checking whether
445 -- it is a bound of the type.
447 if (Is_Entity_Name
(Type_Low_Bound
(Check_Typ
))
448 and then Entity
(Exp
) = Entity
(Type_Low_Bound
(Check_Typ
)))
449 or else (Is_Entity_Name
(Type_High_Bound
(Check_Typ
))
450 and then Entity
(Exp
) = Entity
(Type_High_Bound
(Check_Typ
)))
455 Rewrite
(Exp
, Convert_To
(Check_Typ
, Relocate_Node
(Exp
)));
456 Analyze_And_Resolve
(Exp
, Check_Typ
);
459 Rewrite
(Exp
, Convert_To
(Check_Typ
, Relocate_Node
(Exp
)));
460 Analyze_And_Resolve
(Exp
, Check_Typ
);
464 end Aggregate_Constraint_Checks
;
466 ------------------------
467 -- Array_Aggr_Subtype --
468 ------------------------
470 function Array_Aggr_Subtype
475 Aggr_Dimension
: constant Pos
:= Number_Dimensions
(Typ
);
476 -- Number of aggregate index dimensions.
478 Aggr_Range
: array (1 .. Aggr_Dimension
) of Node_Id
:= (others => Empty
);
479 -- Constrained N_Range of each index dimension in our aggregate itype.
481 Aggr_Low
: array (1 .. Aggr_Dimension
) of Node_Id
:= (others => Empty
);
482 Aggr_High
: array (1 .. Aggr_Dimension
) of Node_Id
:= (others => Empty
);
483 -- Low and High bounds for each index dimension in our aggregate itype.
485 Is_Fully_Positional
: Boolean := True;
487 procedure Collect_Aggr_Bounds
(N
: Node_Id
; Dim
: Pos
);
488 -- N is an array (sub-)aggregate. Dim is the dimension corresponding to
489 -- (sub-)aggregate N. This procedure collects the constrained N_Range
490 -- nodes corresponding to each index dimension of our aggregate itype.
491 -- These N_Range nodes are collected in Aggr_Range above.
492 -- Likewise collect in Aggr_Low & Aggr_High above the low and high
493 -- bounds of each index dimension. If, when collecting, two bounds
494 -- corresponding to the same dimension are static and found to differ,
495 -- then emit a warning, and mark N as raising Constraint_Error.
497 -------------------------
498 -- Collect_Aggr_Bounds --
499 -------------------------
501 procedure Collect_Aggr_Bounds
(N
: Node_Id
; Dim
: Pos
) is
502 This_Range
: constant Node_Id
:= Aggregate_Bounds
(N
);
503 -- The aggregate range node of this specific sub-aggregate.
505 This_Low
: constant Node_Id
:= Low_Bound
(Aggregate_Bounds
(N
));
506 This_High
: constant Node_Id
:= High_Bound
(Aggregate_Bounds
(N
));
507 -- The aggregate bounds of this specific sub-aggregate.
513 -- Collect the first N_Range for a given dimension that you find.
514 -- For a given dimension they must be all equal anyway.
516 if No
(Aggr_Range
(Dim
)) then
517 Aggr_Low
(Dim
) := This_Low
;
518 Aggr_High
(Dim
) := This_High
;
519 Aggr_Range
(Dim
) := This_Range
;
522 if Compile_Time_Known_Value
(This_Low
) then
523 if not Compile_Time_Known_Value
(Aggr_Low
(Dim
)) then
524 Aggr_Low
(Dim
) := This_Low
;
526 elsif Expr_Value
(This_Low
) /= Expr_Value
(Aggr_Low
(Dim
)) then
527 Set_Raises_Constraint_Error
(N
);
528 Error_Msg_N
("Sub-aggregate low bound mismatch?", N
);
529 Error_Msg_N
("Constraint_Error will be raised at run-time?",
534 if Compile_Time_Known_Value
(This_High
) then
535 if not Compile_Time_Known_Value
(Aggr_High
(Dim
)) then
536 Aggr_High
(Dim
) := This_High
;
539 Expr_Value
(This_High
) /= Expr_Value
(Aggr_High
(Dim
))
541 Set_Raises_Constraint_Error
(N
);
542 Error_Msg_N
("Sub-aggregate high bound mismatch?", N
);
543 Error_Msg_N
("Constraint_Error will be raised at run-time?",
549 if Dim
< Aggr_Dimension
then
551 -- Process positional components
553 if Present
(Expressions
(N
)) then
554 Expr
:= First
(Expressions
(N
));
555 while Present
(Expr
) loop
556 Collect_Aggr_Bounds
(Expr
, Dim
+ 1);
561 -- Process component associations
563 if Present
(Component_Associations
(N
)) then
564 Is_Fully_Positional
:= False;
566 Assoc
:= First
(Component_Associations
(N
));
567 while Present
(Assoc
) loop
568 Expr
:= Expression
(Assoc
);
569 Collect_Aggr_Bounds
(Expr
, Dim
+ 1);
574 end Collect_Aggr_Bounds
;
576 -- Array_Aggr_Subtype variables
579 -- the final itype of the overall aggregate
581 Index_Constraints
: List_Id
:= New_List
;
582 -- The list of index constraints of the aggregate itype.
584 -- Start of processing for Array_Aggr_Subtype
587 -- Make sure that the list of index constraints is properly attached
588 -- to the tree, and then collect the aggregate bounds.
590 Set_Parent
(Index_Constraints
, N
);
591 Collect_Aggr_Bounds
(N
, 1);
593 -- Build the list of constrained indices of our aggregate itype.
595 for J
in 1 .. Aggr_Dimension
loop
596 Create_Index
: declare
597 Index_Base
: Entity_Id
:= Base_Type
(Etype
(Aggr_Range
(J
)));
598 Index_Typ
: Entity_Id
;
601 -- Construct the Index subtype
603 Index_Typ
:= Create_Itype
(Subtype_Kind
(Ekind
(Index_Base
)), N
);
605 Set_Etype
(Index_Typ
, Index_Base
);
607 if Is_Character_Type
(Index_Base
) then
608 Set_Is_Character_Type
(Index_Typ
);
611 Set_Size_Info
(Index_Typ
, (Index_Base
));
612 Set_RM_Size
(Index_Typ
, RM_Size
(Index_Base
));
613 Set_First_Rep_Item
(Index_Typ
, First_Rep_Item
(Index_Base
));
614 Set_Scalar_Range
(Index_Typ
, Aggr_Range
(J
));
616 if Is_Discrete_Or_Fixed_Point_Type
(Index_Typ
) then
617 Set_RM_Size
(Index_Typ
, UI_From_Int
(Minimum_Size
(Index_Typ
)));
620 Set_Etype
(Aggr_Range
(J
), Index_Typ
);
622 Append
(Aggr_Range
(J
), To
=> Index_Constraints
);
626 -- Now build the Itype
628 Itype
:= Create_Itype
(E_Array_Subtype
, N
);
630 Set_First_Rep_Item
(Itype
, First_Rep_Item
(Typ
));
631 Set_Convention
(Itype
, Convention
(Typ
));
632 Set_Depends_On_Private
(Itype
, Has_Private_Component
(Typ
));
633 Set_Etype
(Itype
, Base_Type
(Typ
));
634 Set_Has_Alignment_Clause
(Itype
, Has_Alignment_Clause
(Typ
));
635 Set_Is_Aliased
(Itype
, Is_Aliased
(Typ
));
636 Set_Suppress_Index_Checks
(Itype
, Suppress_Index_Checks
(Typ
));
637 Set_Suppress_Length_Checks
(Itype
, Suppress_Length_Checks
(Typ
));
638 Set_Depends_On_Private
(Itype
, Depends_On_Private
(Typ
));
640 Set_First_Index
(Itype
, First
(Index_Constraints
));
641 Set_Is_Constrained
(Itype
, True);
642 Set_Is_Internal
(Itype
, True);
643 Init_Size_Align
(Itype
);
645 -- A simple optimization: purely positional aggregates of static
646 -- components should be passed to gigi unexpanded whenever possible,
647 -- and regardless of the staticness of the bounds themselves. Subse-
648 -- quent checks in exp_aggr verify that type is not packed, etc.
650 Set_Size_Known_At_Compile_Time
(Itype
,
652 and then Comes_From_Source
(N
)
653 and then Size_Known_At_Compile_Time
(Component_Type
(Typ
)));
655 -- We always need a freeze node for a packed array subtype, so that
656 -- we can build the Packed_Array_Type corresponding to the subtype.
657 -- If expansion is disabled, the packed array subtype is not built,
658 -- and we must not generate a freeze node for the type, or else it
659 -- will appear incomplete to gigi.
661 if Is_Packed
(Itype
) and then not In_Default_Expression
662 and then Expander_Active
664 Freeze_Itype
(Itype
, N
);
668 end Array_Aggr_Subtype
;
670 --------------------------------
671 -- Check_Misspelled_Component --
672 --------------------------------
674 procedure Check_Misspelled_Component
675 (Elements
: Elist_Id
;
678 Max_Suggestions
: constant := 2;
680 Nr_Of_Suggestions
: Natural := 0;
681 Suggestion_1
: Entity_Id
:= Empty
;
682 Suggestion_2
: Entity_Id
:= Empty
;
683 Component_Elmt
: Elmt_Id
;
686 -- All the components of List are matched against Component and
687 -- a count is maintained of possible misspellings. When at the
688 -- end of the analysis there are one or two (not more!) possible
689 -- misspellings, these misspellings will be suggested as
690 -- possible correction.
692 Get_Name_String
(Chars
(Component
));
695 S
: constant String (1 .. Name_Len
) :=
696 Name_Buffer
(1 .. Name_Len
);
700 Component_Elmt
:= First_Elmt
(Elements
);
702 while Nr_Of_Suggestions
<= Max_Suggestions
703 and then Present
(Component_Elmt
)
706 Get_Name_String
(Chars
(Node
(Component_Elmt
)));
708 if Is_Bad_Spelling_Of
(Name_Buffer
(1 .. Name_Len
), S
) then
709 Nr_Of_Suggestions
:= Nr_Of_Suggestions
+ 1;
711 case Nr_Of_Suggestions
is
712 when 1 => Suggestion_1
:= Node
(Component_Elmt
);
713 when 2 => Suggestion_2
:= Node
(Component_Elmt
);
718 Next_Elmt
(Component_Elmt
);
721 -- Report at most two suggestions
723 if Nr_Of_Suggestions
= 1 then
724 Error_Msg_NE
("\possible misspelling of&",
725 Component
, Suggestion_1
);
727 elsif Nr_Of_Suggestions
= 2 then
728 Error_Msg_Node_2
:= Suggestion_2
;
729 Error_Msg_NE
("\possible misspelling of& or&",
730 Component
, Suggestion_1
);
733 end Check_Misspelled_Component
;
735 ----------------------------------------
736 -- Check_Static_Discriminated_Subtype --
737 ----------------------------------------
739 procedure Check_Static_Discriminated_Subtype
(T
: Entity_Id
; V
: Node_Id
) is
740 Disc
: constant Entity_Id
:= First_Discriminant
(T
);
745 if Has_Record_Rep_Clause
(T
) then
748 elsif Present
(Next_Discriminant
(Disc
)) then
751 elsif Nkind
(V
) /= N_Integer_Literal
then
755 Comp
:= First_Component
(T
);
757 while Present
(Comp
) loop
759 if Is_Scalar_Type
(Etype
(Comp
)) then
762 elsif Is_Private_Type
(Etype
(Comp
))
763 and then Present
(Full_View
(Etype
(Comp
)))
764 and then Is_Scalar_Type
(Full_View
(Etype
(Comp
)))
768 elsif Is_Array_Type
(Etype
(Comp
)) then
770 if Is_Bit_Packed_Array
(Etype
(Comp
)) then
774 Ind
:= First_Index
(Etype
(Comp
));
776 while Present
(Ind
) loop
778 if Nkind
(Ind
) /= N_Range
779 or else Nkind
(Low_Bound
(Ind
)) /= N_Integer_Literal
780 or else Nkind
(High_Bound
(Ind
)) /= N_Integer_Literal
792 Next_Component
(Comp
);
795 -- On exit, all components have statically known sizes.
797 Set_Size_Known_At_Compile_Time
(T
);
798 end Check_Static_Discriminated_Subtype
;
800 --------------------------------
801 -- Make_String_Into_Aggregate --
802 --------------------------------
804 procedure Make_String_Into_Aggregate
(N
: Node_Id
) is
807 Exprs
: List_Id
:= New_List
;
808 Loc
: constant Source_Ptr
:= Sloc
(N
);
810 P
: Source_Ptr
:= Loc
+ 1;
811 Str
: constant String_Id
:= Strval
(N
);
812 Strlen
: constant Nat
:= String_Length
(Str
);
815 for J
in 1 .. Strlen
loop
816 C
:= Get_String_Char
(Str
, J
);
817 Set_Character_Literal_Name
(C
);
819 C_Node
:= Make_Character_Literal
(P
, Name_Find
, C
);
820 Set_Etype
(C_Node
, Any_Character
);
821 Append_To
(Exprs
, C_Node
);
824 -- something special for wide strings ?
827 New_N
:= Make_Aggregate
(Loc
, Expressions
=> Exprs
);
828 Set_Analyzed
(New_N
);
829 Set_Etype
(New_N
, Any_Composite
);
832 end Make_String_Into_Aggregate
;
834 -----------------------
835 -- Resolve_Aggregate --
836 -----------------------
838 procedure Resolve_Aggregate
(N
: Node_Id
; Typ
: Entity_Id
) is
839 Pkind
: constant Node_Kind
:= Nkind
(Parent
(N
));
841 Aggr_Subtyp
: Entity_Id
;
842 -- The actual aggregate subtype. This is not necessarily the same as Typ
843 -- which is the subtype of the context in which the aggregate was found.
846 if Is_Limited_Type
(Typ
) then
847 Error_Msg_N
("aggregate type cannot be limited", N
);
849 elsif Is_Limited_Composite
(Typ
) then
850 Error_Msg_N
("aggregate type cannot have limited component", N
);
852 elsif Is_Class_Wide_Type
(Typ
) then
853 Error_Msg_N
("type of aggregate cannot be class-wide", N
);
855 elsif Typ
= Any_String
856 or else Typ
= Any_Composite
858 Error_Msg_N
("no unique type for aggregate", N
);
859 Set_Etype
(N
, Any_Composite
);
861 elsif Is_Array_Type
(Typ
) and then Null_Record_Present
(N
) then
862 Error_Msg_N
("null record forbidden in array aggregate", N
);
864 elsif Is_Record_Type
(Typ
) then
865 Resolve_Record_Aggregate
(N
, Typ
);
867 elsif Is_Array_Type
(Typ
) then
869 -- First a special test, for the case of a positional aggregate
870 -- of characters which can be replaced by a string literal.
871 -- Do not perform this transformation if this was a string literal
872 -- to start with, whose components needed constraint checks, or if
873 -- the component type is non-static, because it will require those
874 -- checks and be transformed back into an aggregate.
876 if Number_Dimensions
(Typ
) = 1
878 (Root_Type
(Component_Type
(Typ
)) = Standard_Character
880 Root_Type
(Component_Type
(Typ
)) = Standard_Wide_Character
)
881 and then No
(Component_Associations
(N
))
882 and then not Is_Limited_Composite
(Typ
)
883 and then not Is_Private_Composite
(Typ
)
884 and then not Is_Bit_Packed_Array
(Typ
)
885 and then Nkind
(Original_Node
(Parent
(N
))) /= N_String_Literal
886 and then Is_Static_Subtype
(Component_Type
(Typ
))
892 Expr
:= First
(Expressions
(N
));
893 while Present
(Expr
) loop
894 exit when Nkind
(Expr
) /= N_Character_Literal
;
901 Expr
:= First
(Expressions
(N
));
902 while Present
(Expr
) loop
903 Store_String_Char
(Char_Literal_Value
(Expr
));
908 Make_String_Literal
(Sloc
(N
), End_String
));
910 Analyze_And_Resolve
(N
, Typ
);
916 -- Here if we have a real aggregate to deal with
918 Array_Aggregate
: declare
919 Aggr_Resolved
: Boolean;
920 Aggr_Typ
: Entity_Id
:= Etype
(Typ
);
921 -- This is the unconstrained array type, which is the type
922 -- against which the aggregate is to be resoved. Typ itself
923 -- is the array type of the context which may not be the same
924 -- subtype as the subtype for the final aggregate.
927 -- In the following we determine whether an others choice is
928 -- allowed inside the array aggregate. The test checks the context
929 -- in which the array aggregate occurs. If the context does not
930 -- permit it, or the aggregate type is unconstrained, an others
931 -- choice is not allowed.
933 -- Note that there is no node for Explicit_Actual_Parameter.
934 -- To test for this context we therefore have to test for node
935 -- N_Parameter_Association which itself appears only if there is a
936 -- formal parameter. Consequently we also need to test for
937 -- N_Procedure_Call_Statement or N_Function_Call.
939 if Is_Constrained
(Typ
) and then
940 (Pkind
= N_Assignment_Statement
or else
941 Pkind
= N_Parameter_Association
or else
942 Pkind
= N_Function_Call
or else
943 Pkind
= N_Procedure_Call_Statement
or else
944 Pkind
= N_Generic_Association
or else
945 Pkind
= N_Formal_Object_Declaration
or else
946 Pkind
= N_Return_Statement
or else
947 Pkind
= N_Object_Declaration
or else
948 Pkind
= N_Component_Declaration
or else
949 Pkind
= N_Parameter_Specification
or else
950 Pkind
= N_Qualified_Expression
or else
951 Pkind
= N_Aggregate
or else
952 Pkind
= N_Extension_Aggregate
or else
953 Pkind
= N_Component_Association
)
956 Resolve_Array_Aggregate
958 Index
=> First_Index
(Aggr_Typ
),
959 Index_Constr
=> First_Index
(Typ
),
960 Component_Typ
=> Component_Type
(Typ
),
961 Others_Allowed
=> True);
965 Resolve_Array_Aggregate
967 Index
=> First_Index
(Aggr_Typ
),
968 Index_Constr
=> First_Index
(Aggr_Typ
),
969 Component_Typ
=> Component_Type
(Typ
),
970 Others_Allowed
=> False);
973 if not Aggr_Resolved
then
974 Aggr_Subtyp
:= Any_Composite
;
976 Aggr_Subtyp
:= Array_Aggr_Subtype
(N
, Typ
);
979 Set_Etype
(N
, Aggr_Subtyp
);
983 Error_Msg_N
("illegal context for aggregate", N
);
987 -- If we can determine statically that the evaluation of the
988 -- aggregate raises Constraint_Error, then replace the
989 -- aggregate with an N_Raise_Constraint_Error node, but set the
990 -- Etype to the right aggregate subtype. Gigi needs this.
992 if Raises_Constraint_Error
(N
) then
993 Aggr_Subtyp
:= Etype
(N
);
995 Make_Raise_Constraint_Error
(Sloc
(N
),
996 Reason
=> CE_Range_Check_Failed
));
997 Set_Raises_Constraint_Error
(N
);
998 Set_Etype
(N
, Aggr_Subtyp
);
1002 end Resolve_Aggregate
;
1004 -----------------------------
1005 -- Resolve_Array_Aggregate --
1006 -----------------------------
1008 function Resolve_Array_Aggregate
1011 Index_Constr
: Node_Id
;
1012 Component_Typ
: Entity_Id
;
1013 Others_Allowed
: Boolean)
1016 Loc
: constant Source_Ptr
:= Sloc
(N
);
1018 Failure
: constant Boolean := False;
1019 Success
: constant Boolean := True;
1021 Index_Typ
: constant Entity_Id
:= Etype
(Index
);
1022 Index_Typ_Low
: constant Node_Id
:= Type_Low_Bound
(Index_Typ
);
1023 Index_Typ_High
: constant Node_Id
:= Type_High_Bound
(Index_Typ
);
1024 -- The type of the index corresponding to the array sub-aggregate
1025 -- along with its low and upper bounds
1027 Index_Base
: constant Entity_Id
:= Base_Type
(Index_Typ
);
1028 Index_Base_Low
: constant Node_Id
:= Type_Low_Bound
(Index_Base
);
1029 Index_Base_High
: constant Node_Id
:= Type_High_Bound
(Index_Base
);
1030 -- ditto for the base type
1032 function Add
(Val
: Uint
; To
: Node_Id
) return Node_Id
;
1033 -- Creates a new expression node where Val is added to expression To.
1034 -- Tries to constant fold whenever possible. To must be an already
1035 -- analyzed expression.
1037 procedure Check_Bound
(BH
: Node_Id
; AH
: in out Node_Id
);
1038 -- Checks that AH (the upper bound of an array aggregate) is <= BH
1039 -- (the upper bound of the index base type). If the check fails a
1040 -- warning is emitted, the Raises_Constraint_Error Flag of N is set,
1041 -- and AH is replaced with a duplicate of BH.
1043 procedure Check_Bounds
(L
, H
: Node_Id
; AL
, AH
: Node_Id
);
1044 -- Checks that range AL .. AH is compatible with range L .. H. Emits a
1045 -- warning if not and sets the Raises_Constraint_Error Flag in N.
1047 procedure Check_Length
(L
, H
: Node_Id
; Len
: Uint
);
1048 -- Checks that range L .. H contains at least Len elements. Emits a
1049 -- warning if not and sets the Raises_Constraint_Error Flag in N.
1051 function Dynamic_Or_Null_Range
(L
, H
: Node_Id
) return Boolean;
1052 -- Returns True if range L .. H is dynamic or null.
1054 procedure Get
(Value
: out Uint
; From
: Node_Id
; OK
: out Boolean);
1055 -- Given expression node From, this routine sets OK to False if it
1056 -- cannot statically evaluate From. Otherwise it stores this static
1057 -- value into Value.
1059 function Resolve_Aggr_Expr
1061 Single_Elmt
: Boolean)
1063 -- Resolves aggregate expression Expr. Returs False if resolution
1064 -- fails. If Single_Elmt is set to False, the expression Expr may be
1065 -- used to initialize several array aggregate elements (this can
1066 -- happen for discrete choices such as "L .. H => Expr" or the others
1067 -- choice). In this event we do not resolve Expr unless expansion is
1068 -- disabled. To know why, see the DELAYED COMPONENT RESOLUTION
1075 function Add
(Val
: Uint
; To
: Node_Id
) return Node_Id
is
1081 if Raises_Constraint_Error
(To
) then
1085 -- First test if we can do constant folding
1087 if Compile_Time_Known_Value
(To
)
1088 or else Nkind
(To
) = N_Integer_Literal
1090 Expr_Pos
:= Make_Integer_Literal
(Loc
, Expr_Value
(To
) + Val
);
1091 Set_Is_Static_Expression
(Expr_Pos
);
1092 Set_Etype
(Expr_Pos
, Etype
(To
));
1093 Set_Analyzed
(Expr_Pos
, Analyzed
(To
));
1095 if not Is_Enumeration_Type
(Index_Typ
) then
1098 -- If we are dealing with enumeration return
1099 -- Index_Typ'Val (Expr_Pos)
1103 Make_Attribute_Reference
1105 Prefix
=> New_Reference_To
(Index_Typ
, Loc
),
1106 Attribute_Name
=> Name_Val
,
1107 Expressions
=> New_List
(Expr_Pos
));
1113 -- If we are here no constant folding possible
1115 if not Is_Enumeration_Type
(Index_Base
) then
1118 Left_Opnd
=> Duplicate_Subexpr
(To
),
1119 Right_Opnd
=> Make_Integer_Literal
(Loc
, Val
));
1121 -- If we are dealing with enumeration return
1122 -- Index_Typ'Val (Index_Typ'Pos (To) + Val)
1126 Make_Attribute_Reference
1128 Prefix
=> New_Reference_To
(Index_Typ
, Loc
),
1129 Attribute_Name
=> Name_Pos
,
1130 Expressions
=> New_List
(Duplicate_Subexpr
(To
)));
1134 Left_Opnd
=> To_Pos
,
1135 Right_Opnd
=> Make_Integer_Literal
(Loc
, Val
));
1138 Make_Attribute_Reference
1140 Prefix
=> New_Reference_To
(Index_Typ
, Loc
),
1141 Attribute_Name
=> Name_Val
,
1142 Expressions
=> New_List
(Expr_Pos
));
1152 procedure Check_Bound
(BH
: Node_Id
; AH
: in out Node_Id
) is
1160 Get
(Value
=> Val_BH
, From
=> BH
, OK
=> OK_BH
);
1161 Get
(Value
=> Val_AH
, From
=> AH
, OK
=> OK_AH
);
1163 if OK_BH
and then OK_AH
and then Val_BH
< Val_AH
then
1164 Set_Raises_Constraint_Error
(N
);
1165 Error_Msg_N
("upper bound out of range?", AH
);
1166 Error_Msg_N
("Constraint_Error will be raised at run-time?", AH
);
1168 -- You need to set AH to BH or else in the case of enumerations
1169 -- indices we will not be able to resolve the aggregate bounds.
1171 AH
:= Duplicate_Subexpr
(BH
);
1179 procedure Check_Bounds
(L
, H
: Node_Id
; AL
, AH
: Node_Id
) is
1191 if Raises_Constraint_Error
(N
)
1192 or else Dynamic_Or_Null_Range
(AL
, AH
)
1197 Get
(Value
=> Val_L
, From
=> L
, OK
=> OK_L
);
1198 Get
(Value
=> Val_H
, From
=> H
, OK
=> OK_H
);
1200 Get
(Value
=> Val_AL
, From
=> AL
, OK
=> OK_AL
);
1201 Get
(Value
=> Val_AH
, From
=> AH
, OK
=> OK_AH
);
1203 if OK_L
and then Val_L
> Val_AL
then
1204 Set_Raises_Constraint_Error
(N
);
1205 Error_Msg_N
("lower bound of aggregate out of range?", N
);
1206 Error_Msg_N
("Constraint_Error will be raised at run-time?", N
);
1209 if OK_H
and then Val_H
< Val_AH
then
1210 Set_Raises_Constraint_Error
(N
);
1211 Error_Msg_N
("upper bound of aggregate out of range?", N
);
1212 Error_Msg_N
("Constraint_Error will be raised at run-time?", N
);
1220 procedure Check_Length
(L
, H
: Node_Id
; Len
: Uint
) is
1230 if Raises_Constraint_Error
(N
) then
1234 Get
(Value
=> Val_L
, From
=> L
, OK
=> OK_L
);
1235 Get
(Value
=> Val_H
, From
=> H
, OK
=> OK_H
);
1237 if not OK_L
or else not OK_H
then
1241 -- If null range length is zero
1243 if Val_L
> Val_H
then
1244 Range_Len
:= Uint_0
;
1246 Range_Len
:= Val_H
- Val_L
+ 1;
1249 if Range_Len
< Len
then
1250 Set_Raises_Constraint_Error
(N
);
1251 Error_Msg_N
("Too many elements?", N
);
1252 Error_Msg_N
("Constraint_Error will be raised at run-time?", N
);
1256 ---------------------------
1257 -- Dynamic_Or_Null_Range --
1258 ---------------------------
1260 function Dynamic_Or_Null_Range
(L
, H
: Node_Id
) return Boolean is
1268 Get
(Value
=> Val_L
, From
=> L
, OK
=> OK_L
);
1269 Get
(Value
=> Val_H
, From
=> H
, OK
=> OK_H
);
1271 return not OK_L
or else not OK_H
1272 or else not Is_OK_Static_Expression
(L
)
1273 or else not Is_OK_Static_Expression
(H
)
1274 or else Val_L
> Val_H
;
1275 end Dynamic_Or_Null_Range
;
1281 procedure Get
(Value
: out Uint
; From
: Node_Id
; OK
: out Boolean) is
1285 if Compile_Time_Known_Value
(From
) then
1286 Value
:= Expr_Value
(From
);
1288 -- If expression From is something like Some_Type'Val (10) then
1291 elsif Nkind
(From
) = N_Attribute_Reference
1292 and then Attribute_Name
(From
) = Name_Val
1293 and then Compile_Time_Known_Value
(First
(Expressions
(From
)))
1295 Value
:= Expr_Value
(First
(Expressions
(From
)));
1303 -----------------------
1304 -- Resolve_Aggr_Expr --
1305 -----------------------
1307 function Resolve_Aggr_Expr
1309 Single_Elmt
: Boolean)
1312 Nxt_Ind
: Node_Id
:= Next_Index
(Index
);
1313 Nxt_Ind_Constr
: Node_Id
:= Next_Index
(Index_Constr
);
1314 -- Index is the current index corresponding to the expression.
1316 Resolution_OK
: Boolean := True;
1317 -- Set to False if resolution of the expression failed.
1320 -- If the array type against which we are resolving the aggregate
1321 -- has several dimensions, the expressions nested inside the
1322 -- aggregate must be further aggregates (or strings).
1324 if Present
(Nxt_Ind
) then
1325 if Nkind
(Expr
) /= N_Aggregate
then
1327 -- A string literal can appear where a one-dimensional array
1328 -- of characters is expected. If the literal looks like an
1329 -- operator, it is still an operator symbol, which will be
1330 -- transformed into a string when analyzed.
1332 if Is_Character_Type
(Component_Typ
)
1333 and then No
(Next_Index
(Nxt_Ind
))
1334 and then (Nkind
(Expr
) = N_String_Literal
1335 or else Nkind
(Expr
) = N_Operator_Symbol
)
1337 -- A string literal used in a multidimensional array
1338 -- aggregate in place of the final one-dimensional
1339 -- aggregate must not be enclosed in parentheses.
1341 if Paren_Count
(Expr
) /= 0 then
1342 Error_Msg_N
("No parenthesis allowed here", Expr
);
1345 Make_String_Into_Aggregate
(Expr
);
1348 Error_Msg_N
("nested array aggregate expected", Expr
);
1353 Resolution_OK
:= Resolve_Array_Aggregate
1354 (Expr
, Nxt_Ind
, Nxt_Ind_Constr
, Component_Typ
, Others_Allowed
);
1356 -- Do not resolve the expressions of discrete or others choices
1357 -- unless the expression covers a single component, or the expander
1361 or else not Expander_Active
1362 or else In_Default_Expression
1364 Analyze_And_Resolve
(Expr
, Component_Typ
);
1365 Check_Non_Static_Context
(Expr
);
1366 Aggregate_Constraint_Checks
(Expr
, Component_Typ
);
1369 if Raises_Constraint_Error
(Expr
)
1370 and then Nkind
(Parent
(Expr
)) /= N_Component_Association
1372 Set_Raises_Constraint_Error
(N
);
1375 return Resolution_OK
;
1376 end Resolve_Aggr_Expr
;
1378 -- Variables local to Resolve_Array_Aggregate
1384 Who_Cares
: Node_Id
;
1386 Aggr_Low
: Node_Id
:= Empty
;
1387 Aggr_High
: Node_Id
:= Empty
;
1388 -- The actual low and high bounds of this sub-aggegate
1390 Choices_Low
: Node_Id
:= Empty
;
1391 Choices_High
: Node_Id
:= Empty
;
1392 -- The lowest and highest discrete choices values for a named aggregate
1394 Nb_Elements
: Uint
:= Uint_0
;
1395 -- The number of elements in a positional aggegate
1397 Others_Present
: Boolean := False;
1399 Nb_Choices
: Nat
:= 0;
1400 -- Contains the overall number of named choices in this sub-aggregate
1402 Nb_Discrete_Choices
: Nat
:= 0;
1403 -- The overall number of discrete choices (not counting others choice)
1405 Case_Table_Size
: Nat
;
1406 -- Contains the size of the case table needed to sort aggregate choices
1408 -- Start of processing for Resolve_Array_Aggregate
1411 -- STEP 1: make sure the aggregate is correctly formatted
1413 if Present
(Component_Associations
(N
)) then
1414 Assoc
:= First
(Component_Associations
(N
));
1415 while Present
(Assoc
) loop
1416 Choice
:= First
(Choices
(Assoc
));
1417 while Present
(Choice
) loop
1418 if Nkind
(Choice
) = N_Others_Choice
then
1419 Others_Present
:= True;
1421 if Choice
/= First
(Choices
(Assoc
))
1422 or else Present
(Next
(Choice
))
1425 ("OTHERS must appear alone in a choice list", Choice
);
1429 if Present
(Next
(Assoc
)) then
1431 ("OTHERS must appear last in an aggregate", Choice
);
1436 and then Assoc
/= First
(Component_Associations
(N
))
1437 and then (Nkind
(Parent
(N
)) = N_Assignment_Statement
1439 Nkind
(Parent
(N
)) = N_Object_Declaration
)
1442 ("(Ada 83) illegal context for OTHERS choice", N
);
1446 Nb_Choices
:= Nb_Choices
+ 1;
1454 -- At this point we know that the others choice, if present, is by
1455 -- itself and appears last in the aggregate. Check if we have mixed
1456 -- positional and discrete associations (other than the others choice).
1458 if Present
(Expressions
(N
))
1459 and then (Nb_Choices
> 1
1460 or else (Nb_Choices
= 1 and then not Others_Present
))
1463 ("named association cannot follow positional association",
1464 First
(Choices
(First
(Component_Associations
(N
)))));
1468 -- Test for the validity of an others choice if present
1470 if Others_Present
and then not Others_Allowed
then
1472 ("OTHERS choice not allowed here",
1473 First
(Choices
(First
(Component_Associations
(N
)))));
1477 -- Protect against cascaded errors
1479 if Etype
(Index_Typ
) = Any_Type
then
1483 -- STEP 2: Process named components
1485 if No
(Expressions
(N
)) then
1487 if Others_Present
then
1488 Case_Table_Size
:= Nb_Choices
- 1;
1490 Case_Table_Size
:= Nb_Choices
;
1496 -- Denote the lowest and highest values in an aggregate choice
1500 -- High end of one range and Low end of the next. Should be
1501 -- contiguous if there is no hole in the list of values.
1503 Missing_Values
: Boolean;
1504 -- Set True if missing index values
1506 S_Low
: Node_Id
:= Empty
;
1507 S_High
: Node_Id
:= Empty
;
1508 -- if a choice in an aggregate is a subtype indication these
1509 -- denote the lowest and highest values of the subtype
1511 Table
: Case_Table_Type
(1 .. Case_Table_Size
);
1512 -- Used to sort all the different choice values
1514 Single_Choice
: Boolean;
1515 -- Set to true every time there is a single discrete choice in a
1516 -- discrete association
1518 Prev_Nb_Discrete_Choices
: Nat
;
1519 -- Used to keep track of the number of discrete choices
1520 -- in the current association.
1523 -- STEP 2 (A): Check discrete choices validity.
1525 Assoc
:= First
(Component_Associations
(N
));
1526 while Present
(Assoc
) loop
1528 Prev_Nb_Discrete_Choices
:= Nb_Discrete_Choices
;
1529 Choice
:= First
(Choices
(Assoc
));
1533 if Nkind
(Choice
) = N_Others_Choice
then
1534 Single_Choice
:= False;
1537 -- Test for subtype mark without constraint
1539 elsif Is_Entity_Name
(Choice
) and then
1540 Is_Type
(Entity
(Choice
))
1542 if Base_Type
(Entity
(Choice
)) /= Index_Base
then
1544 ("invalid subtype mark in aggregate choice",
1549 elsif Nkind
(Choice
) = N_Subtype_Indication
then
1550 Resolve_Discrete_Subtype_Indication
(Choice
, Index_Base
);
1552 -- Does the subtype indication evaluation raise CE ?
1554 Get_Index_Bounds
(Subtype_Mark
(Choice
), S_Low
, S_High
);
1555 Get_Index_Bounds
(Choice
, Low
, High
);
1556 Check_Bounds
(S_Low
, S_High
, Low
, High
);
1558 else -- Choice is a range or an expression
1559 Resolve
(Choice
, Index_Base
);
1560 Check_Non_Static_Context
(Choice
);
1562 -- Do not range check a choice. This check is redundant
1563 -- since this test is already performed when we check
1564 -- that the bounds of the array aggregate are within
1567 Set_Do_Range_Check
(Choice
, False);
1570 -- If we could not resolve the discrete choice stop here
1572 if Etype
(Choice
) = Any_Type
then
1575 -- If the discrete choice raises CE get its original bounds.
1577 elsif Nkind
(Choice
) = N_Raise_Constraint_Error
then
1578 Set_Raises_Constraint_Error
(N
);
1579 Get_Index_Bounds
(Original_Node
(Choice
), Low
, High
);
1581 -- Otherwise get its bounds as usual
1584 Get_Index_Bounds
(Choice
, Low
, High
);
1587 if (Dynamic_Or_Null_Range
(Low
, High
)
1588 or else (Nkind
(Choice
) = N_Subtype_Indication
1590 Dynamic_Or_Null_Range
(S_Low
, S_High
)))
1591 and then Nb_Choices
/= 1
1594 ("dynamic or empty choice in aggregate " &
1595 "must be the only choice", Choice
);
1599 Nb_Discrete_Choices
:= Nb_Discrete_Choices
+ 1;
1600 Table
(Nb_Discrete_Choices
).Choice_Lo
:= Low
;
1601 Table
(Nb_Discrete_Choices
).Choice_Hi
:= High
;
1606 -- Check if we have a single discrete choice and whether
1607 -- this discrete choice specifies a single value.
1610 (Nb_Discrete_Choices
= Prev_Nb_Discrete_Choices
+ 1)
1611 and then (Low
= High
);
1619 (Expression
(Assoc
), Single_Elmt
=> Single_Choice
)
1627 -- If aggregate contains more than one choice then these must be
1628 -- static. Sort them and check that they are contiguous
1630 if Nb_Discrete_Choices
> 1 then
1631 Sort_Case_Table
(Table
);
1632 Missing_Values
:= False;
1634 Outer
: for J
in 1 .. Nb_Discrete_Choices
- 1 loop
1635 if Expr_Value
(Table
(J
).Choice_Hi
) >=
1636 Expr_Value
(Table
(J
+ 1).Choice_Lo
)
1639 ("duplicate choice values in array aggregate",
1640 Table
(J
).Choice_Hi
);
1643 elsif not Others_Present
then
1645 Hi_Val
:= Expr_Value
(Table
(J
).Choice_Hi
);
1646 Lo_Val
:= Expr_Value
(Table
(J
+ 1).Choice_Lo
);
1648 -- If missing values, output error messages
1650 if Lo_Val
- Hi_Val
> 1 then
1652 -- Header message if not first missing value
1654 if not Missing_Values
then
1656 ("missing index value(s) in array aggregate", N
);
1657 Missing_Values
:= True;
1660 -- Output values of missing indexes
1662 Lo_Val
:= Lo_Val
- 1;
1663 Hi_Val
:= Hi_Val
+ 1;
1665 -- Enumeration type case
1667 if Is_Enumeration_Type
(Index_Typ
) then
1670 (Get_Enum_Lit_From_Pos
1671 (Index_Typ
, Hi_Val
, Loc
));
1673 if Lo_Val
= Hi_Val
then
1674 Error_Msg_N
("\ %", N
);
1678 (Get_Enum_Lit_From_Pos
1679 (Index_Typ
, Lo_Val
, Loc
));
1680 Error_Msg_N
("\ % .. %", N
);
1683 -- Integer types case
1686 Error_Msg_Uint_1
:= Hi_Val
;
1688 if Lo_Val
= Hi_Val
then
1689 Error_Msg_N
("\ ^", N
);
1691 Error_Msg_Uint_2
:= Lo_Val
;
1692 Error_Msg_N
("\ ^ .. ^", N
);
1699 if Missing_Values
then
1700 Set_Etype
(N
, Any_Composite
);
1705 -- STEP 2 (B): Compute aggregate bounds and min/max choices values
1707 if Nb_Discrete_Choices
> 0 then
1708 Choices_Low
:= Table
(1).Choice_Lo
;
1709 Choices_High
:= Table
(Nb_Discrete_Choices
).Choice_Hi
;
1712 if Others_Present
then
1713 Get_Index_Bounds
(Index_Constr
, Aggr_Low
, Aggr_High
);
1716 Aggr_Low
:= Choices_Low
;
1717 Aggr_High
:= Choices_High
;
1721 -- STEP 3: Process positional components
1724 -- STEP 3 (A): Process positional elements
1726 Expr
:= First
(Expressions
(N
));
1727 Nb_Elements
:= Uint_0
;
1728 while Present
(Expr
) loop
1729 Nb_Elements
:= Nb_Elements
+ 1;
1731 if not Resolve_Aggr_Expr
(Expr
, Single_Elmt
=> True) then
1738 if Others_Present
then
1739 Assoc
:= Last
(Component_Associations
(N
));
1740 if not Resolve_Aggr_Expr
(Expression
(Assoc
),
1741 Single_Elmt
=> False)
1747 -- STEP 3 (B): Compute the aggregate bounds
1749 if Others_Present
then
1750 Get_Index_Bounds
(Index_Constr
, Aggr_Low
, Aggr_High
);
1753 if Others_Allowed
then
1754 Get_Index_Bounds
(Index_Constr
, Aggr_Low
, Who_Cares
);
1756 Aggr_Low
:= Index_Typ_Low
;
1759 Aggr_High
:= Add
(Nb_Elements
- 1, To
=> Aggr_Low
);
1760 Check_Bound
(Index_Base_High
, Aggr_High
);
1764 -- STEP 4: Perform static aggregate checks and save the bounds
1768 Check_Bounds
(Index_Typ_Low
, Index_Typ_High
, Aggr_Low
, Aggr_High
);
1769 Check_Bounds
(Index_Base_Low
, Index_Base_High
, Aggr_Low
, Aggr_High
);
1773 if Others_Present
and then Nb_Discrete_Choices
> 0 then
1774 Check_Bounds
(Aggr_Low
, Aggr_High
, Choices_Low
, Choices_High
);
1775 Check_Bounds
(Index_Typ_Low
, Index_Typ_High
,
1776 Choices_Low
, Choices_High
);
1777 Check_Bounds
(Index_Base_Low
, Index_Base_High
,
1778 Choices_Low
, Choices_High
);
1782 elsif Others_Present
and then Nb_Elements
> 0 then
1783 Check_Length
(Aggr_Low
, Aggr_High
, Nb_Elements
);
1784 Check_Length
(Index_Typ_Low
, Index_Typ_High
, Nb_Elements
);
1785 Check_Length
(Index_Base_Low
, Index_Base_High
, Nb_Elements
);
1789 if Raises_Constraint_Error
(Aggr_Low
)
1790 or else Raises_Constraint_Error
(Aggr_High
)
1792 Set_Raises_Constraint_Error
(N
);
1795 Aggr_Low
:= Duplicate_Subexpr
(Aggr_Low
);
1797 -- Do not duplicate Aggr_High if Aggr_High = Aggr_Low + Nb_Elements
1798 -- since the addition node returned by Add is not yet analyzed. Attach
1799 -- to tree and analyze first. Reset analyzed flag to insure it will get
1800 -- analyzed when it is a literal bound whose type must be properly
1803 if Others_Present
or else Nb_Discrete_Choices
> 0 then
1804 Aggr_High
:= Duplicate_Subexpr
(Aggr_High
);
1806 if Etype
(Aggr_High
) = Universal_Integer
then
1807 Set_Analyzed
(Aggr_High
, False);
1811 Set_Aggregate_Bounds
1812 (N
, Make_Range
(Loc
, Low_Bound
=> Aggr_Low
, High_Bound
=> Aggr_High
));
1814 -- The bounds may contain expressions that must be inserted upwards.
1815 -- Attach them fully to the tree. After analysis, remove side effects
1816 -- from upper bound, if still needed.
1818 Set_Parent
(Aggregate_Bounds
(N
), N
);
1819 Analyze_And_Resolve
(Aggregate_Bounds
(N
), Index_Typ
);
1821 if not Others_Present
and then Nb_Discrete_Choices
= 0 then
1822 Set_High_Bound
(Aggregate_Bounds
(N
),
1823 Duplicate_Subexpr
(High_Bound
(Aggregate_Bounds
(N
))));
1827 end Resolve_Array_Aggregate
;
1829 ---------------------------------
1830 -- Resolve_Extension_Aggregate --
1831 ---------------------------------
1833 -- There are two cases to consider:
1835 -- a) If the ancestor part is a type mark, the components needed are
1836 -- the difference between the components of the expected type and the
1837 -- components of the given type mark.
1839 -- b) If the ancestor part is an expression, it must be unambiguous,
1840 -- and once we have its type we can also compute the needed components
1841 -- as in the previous case. In both cases, if the ancestor type is not
1842 -- the immediate ancestor, we have to build this ancestor recursively.
1844 -- In both cases discriminants of the ancestor type do not play a
1845 -- role in the resolution of the needed components, because inherited
1846 -- discriminants cannot be used in a type extension. As a result we can
1847 -- compute independently the list of components of the ancestor type and
1848 -- of the expected type.
1850 procedure Resolve_Extension_Aggregate
(N
: Node_Id
; Typ
: Entity_Id
) is
1851 A
: constant Node_Id
:= Ancestor_Part
(N
);
1855 Imm_Type
: Entity_Id
;
1857 function Valid_Ancestor_Type
return Boolean;
1858 -- Verify that the type of the ancestor part is a non-private ancestor
1859 -- of the expected type.
1861 function Valid_Ancestor_Type
return Boolean is
1862 Imm_Type
: Entity_Id
;
1865 Imm_Type
:= Base_Type
(Typ
);
1866 while Is_Derived_Type
(Imm_Type
)
1867 and then Etype
(Imm_Type
) /= Base_Type
(A_Type
)
1869 Imm_Type
:= Etype
(Base_Type
(Imm_Type
));
1872 if Etype
(Imm_Type
) /= Base_Type
(A_Type
) then
1873 Error_Msg_NE
("expect ancestor type of &", A
, Typ
);
1878 end Valid_Ancestor_Type
;
1880 -- Start of processing for Resolve_Extension_Aggregate
1885 if not Is_Tagged_Type
(Typ
) then
1886 Error_Msg_N
("type of extension aggregate must be tagged", N
);
1889 elsif Is_Limited_Type
(Typ
) then
1890 Error_Msg_N
("aggregate type cannot be limited", N
);
1893 elsif Is_Class_Wide_Type
(Typ
) then
1894 Error_Msg_N
("aggregate cannot be of a class-wide type", N
);
1898 if Is_Entity_Name
(A
)
1899 and then Is_Type
(Entity
(A
))
1901 A_Type
:= Get_Full_View
(Entity
(A
));
1902 Imm_Type
:= Base_Type
(Typ
);
1904 if Valid_Ancestor_Type
then
1905 Set_Entity
(A
, A_Type
);
1906 Set_Etype
(A
, A_Type
);
1908 Validate_Ancestor_Part
(N
);
1909 Resolve_Record_Aggregate
(N
, Typ
);
1912 elsif Nkind
(A
) /= N_Aggregate
then
1913 if Is_Overloaded
(A
) then
1915 Get_First_Interp
(A
, I
, It
);
1917 while Present
(It
.Typ
) loop
1919 if Is_Tagged_Type
(It
.Typ
)
1920 and then not Is_Limited_Type
(It
.Typ
)
1922 if A_Type
/= Any_Type
then
1923 Error_Msg_N
("cannot resolve expression", A
);
1930 Get_Next_Interp
(I
, It
);
1933 if A_Type
= Any_Type
then
1935 ("ancestor part must be non-limited tagged type", A
);
1940 A_Type
:= Etype
(A
);
1943 if Valid_Ancestor_Type
then
1944 Resolve
(A
, A_Type
);
1945 Check_Non_Static_Context
(A
);
1946 Resolve_Record_Aggregate
(N
, Typ
);
1950 Error_Msg_N
(" No unique type for this aggregate", A
);
1953 end Resolve_Extension_Aggregate
;
1955 ------------------------------
1956 -- Resolve_Record_Aggregate --
1957 ------------------------------
1959 procedure Resolve_Record_Aggregate
(N
: Node_Id
; Typ
: Entity_Id
) is
1960 Regular_Aggr
: constant Boolean := Nkind
(N
) /= N_Extension_Aggregate
;
1962 New_Assoc_List
: List_Id
:= New_List
;
1963 New_Assoc
: Node_Id
;
1964 -- New_Assoc_List is the newly built list of N_Component_Association
1965 -- nodes. New_Assoc is one such N_Component_Association node in it.
1966 -- Please note that while Assoc and New_Assoc contain the same
1967 -- kind of nodes, they are used to iterate over two different
1968 -- N_Component_Association lists.
1970 Others_Etype
: Entity_Id
:= Empty
;
1971 -- This variable is used to save the Etype of the last record component
1972 -- that takes its value from the others choice. Its purpose is:
1974 -- (a) make sure the others choice is useful
1976 -- (b) make sure the type of all the components whose value is
1977 -- subsumed by the others choice are the same.
1979 -- This variable is updated as a side effect of function Get_Value
1981 procedure Add_Association
(Component
: Entity_Id
; Expr
: Node_Id
);
1982 -- Builds a new N_Component_Association node which associates
1983 -- Component to expression Expr and adds it to the new association
1984 -- list New_Assoc_List being built.
1986 function Discr_Present
(Discr
: Entity_Id
) return Boolean;
1987 -- If aggregate N is a regular aggregate this routine will return True.
1988 -- Otherwise, if N is an extension aggreagte, Discr is a discriminant
1989 -- whose value may already have been specified by N's ancestor part,
1990 -- this routine checks whether this is indeed the case and if so
1991 -- returns False, signaling that no value for Discr should appear in the
1992 -- N's aggregate part. Also, in this case, the routine appends to
1993 -- New_Assoc_List Discr the discriminant value specified in the ancestor
1999 Consider_Others_Choice
: Boolean := False)
2001 -- Given a record component stored in parameter Compon, the
2002 -- following function returns its value as it appears in the list
2003 -- From, which is a list of N_Component_Association nodes. If no
2004 -- component association has a choice for the searched component,
2005 -- the value provided by the others choice is returned, if there
2006 -- is one and Consider_Others_Choice is set to true. Otherwise
2007 -- Empty is returned. If there is more than one component association
2008 -- giving a value for the searched record component, an error message
2009 -- is emitted and the first found value is returned.
2011 -- If Consider_Others_Choice is set and the returned expression comes
2012 -- from the others choice, then Others_Etype is set as a side effect.
2013 -- An error message is emitted if the components taking their value
2014 -- from the others choice do not have same type.
2016 procedure Resolve_Aggr_Expr
(Expr
: Node_Id
; Component
: Node_Id
);
2017 -- Analyzes and resolves expression Expr against the Etype of the
2018 -- Component. This routine also applies all appropriate checks to Expr.
2019 -- It finally saves a Expr in the newly created association list that
2020 -- will be attached to the final record aggregate. Note that if the
2021 -- Parent pointer of Expr is not set then Expr was produced with a
2022 -- New_copy_Tree or some such.
2024 ---------------------
2025 -- Add_Association --
2026 ---------------------
2028 procedure Add_Association
(Component
: Entity_Id
; Expr
: Node_Id
) is
2029 New_Assoc
: Node_Id
;
2030 Choice_List
: List_Id
:= New_List
;
2033 Append
(New_Occurrence_Of
(Component
, Sloc
(Expr
)), Choice_List
);
2035 Make_Component_Association
(Sloc
(Expr
),
2036 Choices
=> Choice_List
,
2037 Expression
=> Expr
);
2038 Append
(New_Assoc
, New_Assoc_List
);
2039 end Add_Association
;
2045 function Discr_Present
(Discr
: Entity_Id
) return Boolean is
2049 Discr_Expr
: Node_Id
;
2051 Ancestor_Typ
: Entity_Id
;
2052 Orig_Discr
: Entity_Id
;
2054 D_Val
: Elmt_Id
:= No_Elmt
; -- stop junk warning
2056 Ancestor_Is_Subtyp
: Boolean;
2059 if Regular_Aggr
then
2063 Ancestor
:= Ancestor_Part
(N
);
2064 Ancestor_Typ
:= Etype
(Ancestor
);
2065 Loc
:= Sloc
(Ancestor
);
2067 Ancestor_Is_Subtyp
:=
2068 Is_Entity_Name
(Ancestor
) and then Is_Type
(Entity
(Ancestor
));
2070 -- If the ancestor part has no discriminants clearly N's aggregate
2071 -- part must provide a value for Discr.
2073 if not Has_Discriminants
(Ancestor_Typ
) then
2076 -- If the ancestor part is an unconstrained subtype mark then the
2077 -- Discr must be present in N's aggregate part.
2079 elsif Ancestor_Is_Subtyp
2080 and then not Is_Constrained
(Entity
(Ancestor
))
2085 -- Now look to see if Discr was specified in the ancestor part.
2087 Orig_Discr
:= Original_Record_Component
(Discr
);
2088 D
:= First_Discriminant
(Ancestor_Typ
);
2090 if Ancestor_Is_Subtyp
then
2091 D_Val
:= First_Elmt
(Discriminant_Constraint
(Entity
(Ancestor
)));
2094 while Present
(D
) loop
2095 -- If Ancestor has already specified Disc value than
2096 -- insert its value in the final aggregate.
2098 if Original_Record_Component
(D
) = Orig_Discr
then
2099 if Ancestor_Is_Subtyp
then
2100 Discr_Expr
:= New_Copy_Tree
(Node
(D_Val
));
2103 Make_Selected_Component
(Loc
,
2104 Prefix
=> Duplicate_Subexpr
(Ancestor
),
2105 Selector_Name
=> New_Occurrence_Of
(Discr
, Loc
));
2108 Resolve_Aggr_Expr
(Discr_Expr
, Discr
);
2112 Next_Discriminant
(D
);
2114 if Ancestor_Is_Subtyp
then
2129 Consider_Others_Choice
: Boolean := False)
2133 Expr
: Node_Id
:= Empty
;
2134 Selector_Name
: Node_Id
;
2137 if Present
(From
) then
2138 Assoc
:= First
(From
);
2143 while Present
(Assoc
) loop
2144 Selector_Name
:= First
(Choices
(Assoc
));
2145 while Present
(Selector_Name
) loop
2146 if Nkind
(Selector_Name
) = N_Others_Choice
then
2147 if Consider_Others_Choice
and then No
(Expr
) then
2148 if Present
(Others_Etype
) and then
2149 Base_Type
(Others_Etype
) /= Base_Type
(Etype
(Compon
))
2151 Error_Msg_N
("components in OTHERS choice must " &
2152 "have same type", Selector_Name
);
2155 Others_Etype
:= Etype
(Compon
);
2157 -- We need to duplicate the expression for each
2158 -- successive component covered by the others choice.
2159 -- If the expression is itself an array aggregate with
2160 -- "others", its subtype must be obtained from the
2161 -- current component, and therefore it must be (at least
2162 -- partly) reanalyzed.
2164 if Analyzed
(Expression
(Assoc
)) then
2165 Expr
:= New_Copy_Tree
(Expression
(Assoc
));
2167 if Nkind
(Expr
) = N_Aggregate
2168 and then Is_Array_Type
(Etype
(Expr
))
2169 and then No
(Expressions
(Expr
))
2171 Nkind
(First
(Choices
2172 (First
(Component_Associations
(Expr
)))))
2175 Set_Analyzed
(Expr
, False);
2181 return Expression
(Assoc
);
2185 elsif Chars
(Compon
) = Chars
(Selector_Name
) then
2187 -- We need to duplicate the expression when several
2188 -- components are grouped together with a "|" choice.
2189 -- For instance "filed1 | filed2 => Expr"
2191 if Present
(Next
(Selector_Name
)) then
2192 Expr
:= New_Copy_Tree
(Expression
(Assoc
));
2194 Expr
:= Expression
(Assoc
);
2199 ("more than one value supplied for &",
2200 Selector_Name
, Compon
);
2205 Next
(Selector_Name
);
2214 -----------------------
2215 -- Resolve_Aggr_Expr --
2216 -----------------------
2218 procedure Resolve_Aggr_Expr
(Expr
: Node_Id
; Component
: Node_Id
) is
2219 New_C
: Entity_Id
:= Component
;
2220 Expr_Type
: Entity_Id
:= Empty
;
2222 function Has_Expansion_Delayed
(Expr
: Node_Id
) return Boolean;
2223 -- If the expression is an aggregate (possibly qualified) then its
2224 -- expansion is delayed until the enclosing aggregate is expanded
2225 -- into assignments. In that case, do not generate checks on the
2226 -- expression, because they will be generated later, and will other-
2227 -- wise force a copy (to remove side-effects) that would leave a
2228 -- dynamic-sized aggregate in the code, something that gigi cannot
2232 -- Set to True if the resolved Expr node needs to be relocated
2233 -- when attached to the newly created association list. This node
2234 -- need not be relocated if its parent pointer is not set.
2235 -- In fact in this case Expr is the output of a New_Copy_Tree call.
2236 -- if Relocate is True then we have analyzed the expression node
2237 -- in the original aggregate and hence it needs to be relocated
2238 -- when moved over the new association list.
2240 function Has_Expansion_Delayed
(Expr
: Node_Id
) return Boolean is
2241 Kind
: constant Node_Kind
:= Nkind
(Expr
);
2244 return ((Kind
= N_Aggregate
2245 or else Kind
= N_Extension_Aggregate
)
2246 and then Present
(Etype
(Expr
))
2247 and then Is_Record_Type
(Etype
(Expr
))
2248 and then Expansion_Delayed
(Expr
))
2250 or else (Kind
= N_Qualified_Expression
2251 and then Has_Expansion_Delayed
(Expression
(Expr
)));
2252 end Has_Expansion_Delayed
;
2254 -- Start of processing for Resolve_Aggr_Expr
2257 -- If the type of the component is elementary or the type of the
2258 -- aggregate does not contain discriminants, use the type of the
2259 -- component to resolve Expr.
2261 if Is_Elementary_Type
(Etype
(Component
))
2262 or else not Has_Discriminants
(Etype
(N
))
2264 Expr_Type
:= Etype
(Component
);
2266 -- Otherwise we have to pick up the new type of the component from
2267 -- the new costrained subtype of the aggregate. In fact components
2268 -- which are of a composite type might be constrained by a
2269 -- discriminant, and we want to resolve Expr against the subtype were
2270 -- all discriminant occurrences are replaced with their actual value.
2273 New_C
:= First_Component
(Etype
(N
));
2274 while Present
(New_C
) loop
2275 if Chars
(New_C
) = Chars
(Component
) then
2276 Expr_Type
:= Etype
(New_C
);
2280 Next_Component
(New_C
);
2283 pragma Assert
(Present
(Expr_Type
));
2285 -- For each range in an array type where a discriminant has been
2286 -- replaced with the constraint, check that this range is within
2287 -- the range of the base type. This checks is done in the
2288 -- _init_proc for regular objects, but has to be done here for
2289 -- aggregates since no _init_proc is called for them.
2291 if Is_Array_Type
(Expr_Type
) then
2293 Index
: Node_Id
:= First_Index
(Expr_Type
);
2294 -- Range of the current constrained index in the array.
2296 Orig_Index
: Node_Id
:= First_Index
(Etype
(Component
));
2297 -- Range corresponding to the range Index above in the
2298 -- original unconstrained record type. The bounds of this
2299 -- range may be governed by discriminants.
2301 Unconstr_Index
: Node_Id
:= First_Index
(Etype
(Expr_Type
));
2302 -- Range corresponding to the range Index above for the
2303 -- unconstrained array type. This range is needed to apply
2307 while Present
(Index
) loop
2308 if Depends_On_Discriminant
(Orig_Index
) then
2309 Apply_Range_Check
(Index
, Etype
(Unconstr_Index
));
2313 Next_Index
(Orig_Index
);
2314 Next_Index
(Unconstr_Index
);
2320 -- If the Parent pointer of Expr is not set, Expr is an expression
2321 -- duplicated by New_Tree_Copy (this happens for record aggregates
2322 -- that look like (Field1 | Filed2 => Expr) or (others => Expr)).
2323 -- Such a duplicated expression must be attached to the tree
2324 -- before analysis and resolution to enforce the rule that a tree
2325 -- fragment should never be analyzed or resolved unless it is
2326 -- attached to the current compilation unit.
2328 if No
(Parent
(Expr
)) then
2329 Set_Parent
(Expr
, N
);
2335 Analyze_And_Resolve
(Expr
, Expr_Type
);
2336 Check_Non_Static_Context
(Expr
);
2338 if not Has_Expansion_Delayed
(Expr
) then
2339 Aggregate_Constraint_Checks
(Expr
, Expr_Type
);
2342 if Raises_Constraint_Error
(Expr
) then
2343 Set_Raises_Constraint_Error
(N
);
2347 Add_Association
(New_C
, Relocate_Node
(Expr
));
2349 Add_Association
(New_C
, Expr
);
2352 end Resolve_Aggr_Expr
;
2354 -- Resolve_Record_Aggregate local variables
2357 -- N_Component_Association node belonging to the input aggregate N
2360 Positional_Expr
: Node_Id
;
2362 Component
: Entity_Id
;
2363 Component_Elmt
: Elmt_Id
;
2364 Components
: Elist_Id
:= New_Elmt_List
;
2365 -- Components is the list of the record components whose value must
2366 -- be provided in the aggregate. This list does include discriminants.
2368 -- Start of processing for Resolve_Record_Aggregate
2371 -- We may end up calling Duplicate_Subexpr on expressions that are
2372 -- attached to New_Assoc_List. For this reason we need to attach it
2373 -- to the tree by setting its parent pointer to N. This parent point
2374 -- will change in STEP 8 below.
2376 Set_Parent
(New_Assoc_List
, N
);
2378 -- STEP 1: abstract type and null record verification
2380 if Is_Abstract
(Typ
) then
2381 Error_Msg_N
("type of aggregate cannot be abstract", N
);
2384 if No
(First_Entity
(Typ
)) and then Null_Record_Present
(N
) then
2388 elsif Present
(First_Entity
(Typ
))
2389 and then Null_Record_Present
(N
)
2390 and then not Is_Tagged_Type
(Typ
)
2392 Error_Msg_N
("record aggregate cannot be null", N
);
2395 elsif No
(First_Entity
(Typ
)) then
2396 Error_Msg_N
("record aggregate must be null", N
);
2400 -- STEP 2: Verify aggregate structure
2403 Selector_Name
: Node_Id
;
2404 Bad_Aggregate
: Boolean := False;
2407 if Present
(Component_Associations
(N
)) then
2408 Assoc
:= First
(Component_Associations
(N
));
2413 while Present
(Assoc
) loop
2414 Selector_Name
:= First
(Choices
(Assoc
));
2415 while Present
(Selector_Name
) loop
2416 if Nkind
(Selector_Name
) = N_Identifier
then
2419 elsif Nkind
(Selector_Name
) = N_Others_Choice
then
2420 if Selector_Name
/= First
(Choices
(Assoc
))
2421 or else Present
(Next
(Selector_Name
))
2423 Error_Msg_N
("OTHERS must appear alone in a choice list",
2427 elsif Present
(Next
(Assoc
)) then
2428 Error_Msg_N
("OTHERS must appear last in an aggregate",
2435 ("selector name should be identifier or OTHERS",
2437 Bad_Aggregate
:= True;
2440 Next
(Selector_Name
);
2446 if Bad_Aggregate
then
2451 -- STEP 3: Find discriminant Values
2454 Discrim
: Entity_Id
;
2455 Missing_Discriminants
: Boolean := False;
2458 if Present
(Expressions
(N
)) then
2459 Positional_Expr
:= First
(Expressions
(N
));
2461 Positional_Expr
:= Empty
;
2464 if Has_Discriminants
(Typ
) then
2465 Discrim
:= First_Discriminant
(Typ
);
2470 -- First find the discriminant values in the positional components
2472 while Present
(Discrim
) and then Present
(Positional_Expr
) loop
2473 if Discr_Present
(Discrim
) then
2474 Resolve_Aggr_Expr
(Positional_Expr
, Discrim
);
2475 Next
(Positional_Expr
);
2478 if Present
(Get_Value
(Discrim
, Component_Associations
(N
))) then
2480 ("more than one value supplied for discriminant&",
2484 Next_Discriminant
(Discrim
);
2487 -- Find remaining discriminant values, if any, among named components
2489 while Present
(Discrim
) loop
2490 Expr
:= Get_Value
(Discrim
, Component_Associations
(N
), True);
2492 if not Discr_Present
(Discrim
) then
2493 if Present
(Expr
) then
2495 ("more than one value supplied for discriminant&",
2499 elsif No
(Expr
) then
2501 ("no value supplied for discriminant &", N
, Discrim
);
2502 Missing_Discriminants
:= True;
2505 Resolve_Aggr_Expr
(Expr
, Discrim
);
2508 Next_Discriminant
(Discrim
);
2511 if Missing_Discriminants
then
2515 -- At this point and until the beginning of STEP 6, New_Assoc_List
2516 -- contains only the discriminants and their values.
2520 -- STEP 4: Set the Etype of the record aggregate
2522 -- ??? This code is pretty much a copy of Sem_Ch3.Build_Subtype. That
2523 -- routine should really be exported in sem_util or some such and used
2524 -- in sem_ch3 and here rather than have a copy of the code which is a
2525 -- maintenance nightmare.
2527 -- ??? Performace WARNING. The current implementation creates a new
2528 -- itype for all aggregates whose base type is discriminated.
2529 -- This means that for record aggregates nested inside an array
2530 -- aggregate we will create a new itype for each record aggregate
2531 -- if the array cmponent type has discriminants. For large aggregates
2532 -- this may be a problem. What should be done in this case is
2533 -- to reuse itypes as much as possible.
2535 if Has_Discriminants
(Typ
) then
2536 Build_Constrained_Itype
: declare
2537 Loc
: constant Source_Ptr
:= Sloc
(N
);
2539 Subtyp_Decl
: Node_Id
;
2542 C
: List_Id
:= New_List
;
2545 New_Assoc
:= First
(New_Assoc_List
);
2546 while Present
(New_Assoc
) loop
2547 Append
(Duplicate_Subexpr
(Expression
(New_Assoc
)), To
=> C
);
2552 Make_Subtype_Indication
(Loc
,
2553 Subtype_Mark
=> New_Occurrence_Of
(Base_Type
(Typ
), Loc
),
2554 Constraint
=> Make_Index_Or_Discriminant_Constraint
(Loc
, C
));
2556 Def_Id
:= Create_Itype
(Ekind
(Typ
), N
);
2559 Make_Subtype_Declaration
(Loc
,
2560 Defining_Identifier
=> Def_Id
,
2561 Subtype_Indication
=> Indic
);
2562 Set_Parent
(Subtyp_Decl
, Parent
(N
));
2564 -- Itypes must be analyzed with checks off (see itypes.ads).
2566 Analyze
(Subtyp_Decl
, Suppress
=> All_Checks
);
2568 Set_Etype
(N
, Def_Id
);
2569 Check_Static_Discriminated_Subtype
2570 (Def_Id
, Expression
(First
(New_Assoc_List
)));
2571 end Build_Constrained_Itype
;
2577 -- STEP 5: Get remaining components according to discriminant values
2580 Record_Def
: Node_Id
;
2581 Parent_Typ
: Entity_Id
;
2582 Root_Typ
: Entity_Id
;
2583 Parent_Typ_List
: Elist_Id
;
2584 Parent_Elmt
: Elmt_Id
;
2585 Errors_Found
: Boolean := False;
2589 if Is_Derived_Type
(Typ
) and then Is_Tagged_Type
(Typ
) then
2590 Parent_Typ_List
:= New_Elmt_List
;
2592 -- If this is an extension aggregate, the component list must
2593 -- include all components that are not in the given ancestor
2594 -- type. Otherwise, the component list must include components
2595 -- of all ancestors, starting with the root.
2597 if Nkind
(N
) = N_Extension_Aggregate
then
2598 Root_Typ
:= Base_Type
(Etype
(Ancestor_Part
(N
)));
2600 Root_Typ
:= Root_Type
(Typ
);
2602 if Nkind
(Parent
(Base_Type
(Root_Typ
)))
2603 = N_Private_Type_Declaration
2606 ("type of aggregate has private ancestor&!",
2608 Error_Msg_N
("must use extension aggregate!", N
);
2612 Dnode
:= Declaration_Node
(Base_Type
(Root_Typ
));
2614 -- If we don't get a full declaration, then we have some
2615 -- error which will get signalled later so skip this part.
2616 -- Otherwise, gather components of root that apply to the
2617 -- aggregate type. We use the base type in case there is an
2618 -- applicable girder constraint that renames the discriminants
2621 if Nkind
(Dnode
) = N_Full_Type_Declaration
then
2622 Record_Def
:= Type_Definition
(Dnode
);
2623 Gather_Components
(Base_Type
(Typ
),
2624 Component_List
(Record_Def
),
2625 Governed_By
=> New_Assoc_List
,
2627 Report_Errors
=> Errors_Found
);
2631 Parent_Typ
:= Base_Type
(Typ
);
2632 while Parent_Typ
/= Root_Typ
loop
2634 Prepend_Elmt
(Parent_Typ
, To
=> Parent_Typ_List
);
2635 Parent_Typ
:= Etype
(Parent_Typ
);
2637 if (Nkind
(Parent
(Base_Type
(Parent_Typ
))) =
2638 N_Private_Type_Declaration
2639 or else Nkind
(Parent
(Base_Type
(Parent_Typ
))) =
2640 N_Private_Extension_Declaration
)
2642 if Nkind
(N
) /= N_Extension_Aggregate
then
2644 ("type of aggregate has private ancestor&!",
2646 Error_Msg_N
("must use extension aggregate!", N
);
2649 elsif Parent_Typ
/= Root_Typ
then
2651 ("ancestor part of aggregate must be private type&",
2652 Ancestor_Part
(N
), Parent_Typ
);
2658 -- Now collect components from all other ancestors.
2660 Parent_Elmt
:= First_Elmt
(Parent_Typ_List
);
2661 while Present
(Parent_Elmt
) loop
2662 Parent_Typ
:= Node
(Parent_Elmt
);
2663 Record_Def
:= Type_Definition
(Parent
(Base_Type
(Parent_Typ
)));
2664 Gather_Components
(Empty
,
2665 Component_List
(Record_Extension_Part
(Record_Def
)),
2666 Governed_By
=> New_Assoc_List
,
2668 Report_Errors
=> Errors_Found
);
2670 Next_Elmt
(Parent_Elmt
);
2674 Record_Def
:= Type_Definition
(Parent
(Base_Type
(Typ
)));
2676 if Null_Present
(Record_Def
) then
2679 Gather_Components
(Base_Type
(Typ
),
2680 Component_List
(Record_Def
),
2681 Governed_By
=> New_Assoc_List
,
2683 Report_Errors
=> Errors_Found
);
2687 if Errors_Found
then
2692 -- STEP 6: Find component Values
2695 Component_Elmt
:= First_Elmt
(Components
);
2697 -- First scan the remaining positional associations in the aggregate.
2698 -- Remember that at this point Positional_Expr contains the current
2699 -- positional association if any is left after looking for discriminant
2700 -- values in step 3.
2702 while Present
(Positional_Expr
) and then Present
(Component_Elmt
) loop
2703 Component
:= Node
(Component_Elmt
);
2704 Resolve_Aggr_Expr
(Positional_Expr
, Component
);
2706 if Present
(Get_Value
(Component
, Component_Associations
(N
))) then
2708 ("more than one value supplied for Component &", N
, Component
);
2711 Next
(Positional_Expr
);
2712 Next_Elmt
(Component_Elmt
);
2715 if Present
(Positional_Expr
) then
2717 ("too many components for record aggregate", Positional_Expr
);
2720 -- Now scan for the named arguments of the aggregate
2722 while Present
(Component_Elmt
) loop
2723 Component
:= Node
(Component_Elmt
);
2724 Expr
:= Get_Value
(Component
, Component_Associations
(N
), True);
2727 Error_Msg_NE
("no value supplied for component &!", N
, Component
);
2729 Resolve_Aggr_Expr
(Expr
, Component
);
2732 Next_Elmt
(Component_Elmt
);
2735 -- STEP 7: check for invalid components + check type in choice list
2742 -- Type of first component in choice list
2745 if Present
(Component_Associations
(N
)) then
2746 Assoc
:= First
(Component_Associations
(N
));
2751 Verification
: while Present
(Assoc
) loop
2752 Selectr
:= First
(Choices
(Assoc
));
2755 if Nkind
(Selectr
) = N_Others_Choice
then
2756 if No
(Others_Etype
) then
2758 ("OTHERS must represent at least one component", Selectr
);
2764 while Present
(Selectr
) loop
2765 New_Assoc
:= First
(New_Assoc_List
);
2766 while Present
(New_Assoc
) loop
2767 Component
:= First
(Choices
(New_Assoc
));
2768 exit when Chars
(Selectr
) = Chars
(Component
);
2772 -- If no association, this is not a legal component of
2773 -- of the type in question, except if this is an internal
2774 -- component supplied by a previous expansion.
2776 if No
(New_Assoc
) then
2778 if Chars
(Selectr
) /= Name_uTag
2779 and then Chars
(Selectr
) /= Name_uParent
2780 and then Chars
(Selectr
) /= Name_uController
2782 if not Has_Discriminants
(Typ
) then
2783 Error_Msg_Node_2
:= Typ
;
2785 ("& is not a component of}",
2789 ("& is not a component of the aggregate subtype",
2793 Check_Misspelled_Component
(Components
, Selectr
);
2796 elsif No
(Typech
) then
2797 Typech
:= Base_Type
(Etype
(Component
));
2799 elsif Typech
/= Base_Type
(Etype
(Component
)) then
2801 ("components in choice list must have same type", Selectr
);
2808 end loop Verification
;
2811 -- STEP 8: replace the original aggregate
2814 New_Aggregate
: Node_Id
:= New_Copy
(N
);
2817 Set_Expressions
(New_Aggregate
, No_List
);
2818 Set_Etype
(New_Aggregate
, Etype
(N
));
2819 Set_Component_Associations
(New_Aggregate
, New_Assoc_List
);
2821 Rewrite
(N
, New_Aggregate
);
2823 end Resolve_Record_Aggregate
;
2825 ---------------------
2826 -- Sort_Case_Table --
2827 ---------------------
2829 procedure Sort_Case_Table
(Case_Table
: in out Case_Table_Type
) is
2830 L
: Int
:= Case_Table
'First;
2831 U
: Int
:= Case_Table
'Last;
2840 T
:= Case_Table
(K
+ 1);
2844 and then Expr_Value
(Case_Table
(J
- 1).Choice_Lo
) >
2845 Expr_Value
(T
.Choice_Lo
)
2847 Case_Table
(J
) := Case_Table
(J
- 1);
2851 Case_Table
(J
) := T
;
2854 end Sort_Case_Table
;