1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
10 -- Copyright (C) 1992-2002 Free Software Foundation, Inc. --
12 -- GNAT is free software; you can redistribute it and/or modify it under --
13 -- terms of the GNU General Public License as published by the Free Soft- --
14 -- ware Foundation; either version 2, or (at your option) any later ver- --
15 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
18 -- for more details. You should have received a copy of the GNU General --
19 -- Public License distributed with GNAT; see file COPYING. If not, write --
20 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
21 -- MA 02111-1307, USA. --
23 -- GNAT was originally developed by the GNAT team at New York University. --
24 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
26 ------------------------------------------------------------------------------
28 with Atree
; use Atree
;
29 with Checks
; use Checks
;
30 with Einfo
; use Einfo
;
31 with Elists
; use Elists
;
32 with Errout
; use Errout
;
33 with Exp_Util
; use Exp_Util
;
34 with Freeze
; use Freeze
;
35 with Itypes
; use Itypes
;
36 with Namet
; use Namet
;
37 with Nmake
; use Nmake
;
38 with Nlists
; use Nlists
;
41 with Sem_Cat
; use Sem_Cat
;
42 with Sem_Ch8
; use Sem_Ch8
;
43 with Sem_Ch13
; use Sem_Ch13
;
44 with Sem_Eval
; use Sem_Eval
;
45 with Sem_Res
; use Sem_Res
;
46 with Sem_Util
; use Sem_Util
;
47 with Sem_Type
; use Sem_Type
;
48 with Sinfo
; use Sinfo
;
49 with Snames
; use Snames
;
50 with Stringt
; use Stringt
;
51 with Stand
; use Stand
;
52 with Tbuild
; use Tbuild
;
53 with Uintp
; use Uintp
;
55 with GNAT
.Spelling_Checker
; use GNAT
.Spelling_Checker
;
57 package body Sem_Aggr
is
59 type Case_Bounds
is record
62 Choice_Node
: Node_Id
;
65 type Case_Table_Type
is array (Nat
range <>) of Case_Bounds
;
66 -- Table type used by Check_Case_Choices procedure
68 -----------------------
69 -- Local Subprograms --
70 -----------------------
72 procedure Sort_Case_Table
(Case_Table
: in out Case_Table_Type
);
73 -- Sort the Case Table using the Lower Bound of each Choice as the key.
74 -- A simple insertion sort is used since the number of choices in a case
75 -- statement of variant part will usually be small and probably in near
78 ------------------------------------------------------
79 -- Subprograms used for RECORD AGGREGATE Processing --
80 ------------------------------------------------------
82 procedure Resolve_Record_Aggregate
(N
: Node_Id
; Typ
: Entity_Id
);
83 -- This procedure performs all the semantic checks required for record
84 -- aggregates. Note that for aggregates analysis and resolution go
85 -- hand in hand. Aggregate analysis has been delayed up to here and
86 -- it is done while resolving the aggregate.
88 -- N is the N_Aggregate node.
89 -- Typ is the record type for the aggregate resolution
91 -- While performing the semantic checks, this procedure
92 -- builds a new Component_Association_List where each record field
93 -- appears alone in a Component_Choice_List along with its corresponding
94 -- expression. The record fields in the Component_Association_List
95 -- appear in the same order in which they appear in the record type Typ.
97 -- Once this new Component_Association_List is built and all the
98 -- semantic checks performed, the original aggregate subtree is replaced
99 -- with the new named record aggregate just built. Note that the subtree
100 -- substitution is performed with Rewrite so as to be
101 -- able to retrieve the original aggregate.
103 -- The aggregate subtree manipulation performed by Resolve_Record_Aggregate
104 -- yields the aggregate format expected by Gigi. Typically, this kind of
105 -- tree manipulations are done in the expander. However, because the
106 -- semantic checks that need to be performed on record aggregates really
107 -- go hand in hand with the record aggreagate normalization, the aggregate
108 -- subtree transformation is performed during resolution rather than
109 -- expansion. Had we decided otherwise we would have had to duplicate
110 -- most of the code in the expansion procedure Expand_Record_Aggregate.
111 -- Note, however, that all the expansion concerning aggegates for tagged
112 -- records is done in Expand_Record_Aggregate.
114 -- The algorithm of Resolve_Record_Aggregate proceeds as follows:
116 -- 1. Make sure that the record type against which the record aggregate
117 -- has to be resolved is not abstract. Furthermore if the type is
118 -- a null aggregate make sure the input aggregate N is also null.
120 -- 2. Verify that the structure of the aggregate is that of a record
121 -- aggregate. Specifically, look for component associations and ensure
122 -- that each choice list only has identifiers or the N_Others_Choice
123 -- node. Also make sure that if present, the N_Others_Choice occurs
124 -- last and by itself.
126 -- 3. If Typ contains discriminants, the values for each discriminant
127 -- is looked for. If the record type Typ has variants, we check
128 -- that the expressions corresponding to each discriminant ruling
129 -- the (possibly nested) variant parts of Typ, are static. This
130 -- allows us to determine the variant parts to which the rest of
131 -- the aggregate must conform. The names of discriminants with their
132 -- values are saved in a new association list, New_Assoc_List which
133 -- is later augmented with the names and values of the remaining
134 -- components in the record type.
136 -- During this phase we also make sure that every discriminant is
137 -- assigned exactly one value. Note that when several values
138 -- for a given discriminant are found, semantic processing continues
139 -- looking for further errors. In this case it's the first
140 -- discriminant value found which we will be recorded.
142 -- IMPORTANT NOTE: For derived tagged types this procedure expects
143 -- First_Discriminant and Next_Discriminant to give the correct list
144 -- of discriminants, in the correct order.
146 -- 4. After all the discriminant values have been gathered, we can
147 -- set the Etype of the record aggregate. If Typ contains no
148 -- discriminants this is straightforward: the Etype of N is just
149 -- Typ, otherwise a new implicit constrained subtype of Typ is
150 -- built to be the Etype of N.
152 -- 5. Gather the remaining record components according to the discriminant
153 -- values. This involves recursively traversing the record type
154 -- structure to see what variants are selected by the given discriminant
155 -- values. This processing is a little more convoluted if Typ is a
156 -- derived tagged types since we need to retrieve the record structure
157 -- of all the ancestors of Typ.
159 -- 6. After gathering the record components we look for their values
160 -- in the record aggregate and emit appropriate error messages
161 -- should we not find such values or should they be duplicated.
163 -- 7. We then make sure no illegal component names appear in the
164 -- record aggegate and make sure that the type of the record
165 -- components appearing in a same choice list is the same.
166 -- Finally we ensure that the others choice, if present, is
167 -- used to provide the value of at least a record component.
169 -- 8. The original aggregate node is replaced with the new named
170 -- aggregate built in steps 3 through 6, as explained earlier.
172 -- Given the complexity of record aggregate resolution, the primary
173 -- goal of this routine is clarity and simplicity rather than execution
174 -- and storage efficiency. If there are only positional components in the
175 -- aggregate the running time is linear. If there are associations
176 -- the running time is still linear as long as the order of the
177 -- associations is not too far off the order of the components in the
178 -- record type. If this is not the case the running time is at worst
179 -- quadratic in the size of the association list.
181 procedure Check_Misspelled_Component
182 (Elements
: Elist_Id
;
183 Component
: Node_Id
);
184 -- Give possible misspelling diagnostic if Component is likely to be
185 -- a misspelling of one of the components of the Assoc_List.
186 -- This is called by Resolv_Aggr_Expr after producing
187 -- an invalid component error message.
189 procedure Check_Static_Discriminated_Subtype
(T
: Entity_Id
; V
: Node_Id
);
190 -- An optimization: determine whether a discriminated subtype has a
191 -- static constraint, and contains array components whose length is also
192 -- static, either because they are constrained by the discriminant, or
193 -- because the original component bounds are static.
195 -----------------------------------------------------
196 -- Subprograms used for ARRAY AGGREGATE Processing --
197 -----------------------------------------------------
199 function Resolve_Array_Aggregate
202 Index_Constr
: Node_Id
;
203 Component_Typ
: Entity_Id
;
204 Others_Allowed
: Boolean)
206 -- This procedure performs the semantic checks for an array aggregate.
207 -- True is returned if the aggregate resolution succeeds.
208 -- The procedure works by recursively checking each nested aggregate.
209 -- Specifically, after checking a sub-aggreate nested at the i-th level
210 -- we recursively check all the subaggregates at the i+1-st level (if any).
211 -- Note that for aggregates analysis and resolution go hand in hand.
212 -- Aggregate analysis has been delayed up to here and it is done while
213 -- resolving the aggregate.
215 -- N is the current N_Aggregate node to be checked.
217 -- Index is the index node corresponding to the array sub-aggregate that
218 -- we are currently checking (RM 4.3.3 (8)). Its Etype is the
219 -- corresponding index type (or subtype).
221 -- Index_Constr is the node giving the applicable index constraint if
222 -- any (RM 4.3.3 (10)). It "is a constraint provided by certain
223 -- contexts [...] that can be used to determine the bounds of the array
224 -- value specified by the aggregate". If Others_Allowed below is False
225 -- there is no applicable index constraint and this node is set to Index.
227 -- Component_Typ is the array component type.
229 -- Others_Allowed indicates whether an others choice is allowed
230 -- in the context where the top-level aggregate appeared.
232 -- The algorithm of Resolve_Array_Aggregate proceeds as follows:
234 -- 1. Make sure that the others choice, if present, is by itself and
235 -- appears last in the sub-aggregate. Check that we do not have
236 -- positional and named components in the array sub-aggregate (unless
237 -- the named association is an others choice). Finally if an others
238 -- choice is present, make sure it is allowed in the aggregate contex.
240 -- 2. If the array sub-aggregate contains discrete_choices:
242 -- (A) Verify their validity. Specifically verify that:
244 -- (a) If a null range is present it must be the only possible
245 -- choice in the array aggregate.
247 -- (b) Ditto for a non static range.
249 -- (c) Ditto for a non static expression.
251 -- In addition this step analyzes and resolves each discrete_choice,
252 -- making sure that its type is the type of the corresponding Index.
253 -- If we are not at the lowest array aggregate level (in the case of
254 -- multi-dimensional aggregates) then invoke Resolve_Array_Aggregate
255 -- recursively on each component expression. Otherwise, resolve the
256 -- bottom level component expressions against the expected component
257 -- type ONLY IF the component corresponds to a single discrete choice
258 -- which is not an others choice (to see why read the DELAYED
259 -- COMPONENT RESOLUTION below).
261 -- (B) Determine the bounds of the sub-aggregate and lowest and
262 -- highest choice values.
264 -- 3. For positional aggregates:
266 -- (A) Loop over the component expressions either recursively invoking
267 -- Resolve_Array_Aggregate on each of these for multi-dimensional
268 -- array aggregates or resolving the bottom level component
269 -- expressions against the expected component type.
271 -- (B) Determine the bounds of the positional sub-aggregates.
273 -- 4. Try to determine statically whether the evaluation of the array
274 -- sub-aggregate raises Constraint_Error. If yes emit proper
275 -- warnings. The precise checks are the following:
277 -- (A) Check that the index range defined by aggregate bounds is
278 -- compatible with corresponding index subtype.
279 -- We also check against the base type. In fact it could be that
280 -- Low/High bounds of the base type are static whereas those of
281 -- the index subtype are not. Thus if we can statically catch
282 -- a problem with respect to the base type we are guaranteed
283 -- that the same problem will arise with the index subtype
285 -- (B) If we are dealing with a named aggregate containing an others
286 -- choice and at least one discrete choice then make sure the range
287 -- specified by the discrete choices does not overflow the
288 -- aggregate bounds. We also check against the index type and base
289 -- type bounds for the same reasons given in (A).
291 -- (C) If we are dealing with a positional aggregate with an others
292 -- choice make sure the number of positional elements specified
293 -- does not overflow the aggregate bounds. We also check against
294 -- the index type and base type bounds as mentioned in (A).
296 -- Finally construct an N_Range node giving the sub-aggregate bounds.
297 -- Set the Aggregate_Bounds field of the sub-aggregate to be this
298 -- N_Range. The routine Array_Aggr_Subtype below uses such N_Ranges
299 -- to build the appropriate aggregate subtype. Aggregate_Bounds
300 -- information is needed during expansion.
302 -- DELAYED COMPONENT RESOLUTION: The resolution of bottom level component
303 -- expressions in an array aggregate may call Duplicate_Subexpr or some
304 -- other routine that inserts code just outside the outermost aggregate.
305 -- If the array aggregate contains discrete choices or an others choice,
306 -- this may be wrong. Consider for instance the following example.
308 -- type Rec is record
312 -- type Acc_Rec is access Rec;
313 -- Arr : array (1..3) of Acc_Rec := (1 .. 3 => new Rec);
315 -- Then the transformation of "new Rec" that occurs during resolution
316 -- entails the following code modifications
318 -- P7b : constant Acc_Rec := new Rec;
319 -- Rec_init_proc (P7b.all);
320 -- Arr : array (1..3) of Acc_Rec := (1 .. 3 => P7b);
322 -- This code transformation is clearly wrong, since we need to call
323 -- "new Rec" for each of the 3 array elements. To avoid this problem we
324 -- delay resolution of the components of non positional array aggregates
325 -- to the expansion phase. As an optimization, if the discrete choice
326 -- specifies a single value we do not delay resolution.
328 function Array_Aggr_Subtype
(N
: Node_Id
; Typ
: Node_Id
) return Entity_Id
;
329 -- This routine returns the type or subtype of an array aggregate.
331 -- N is the array aggregate node whose type we return.
333 -- Typ is the context type in which N occurs.
335 -- This routine creates an implicit array subtype whose bouds are
336 -- those defined by the aggregate. When this routine is invoked
337 -- Resolve_Array_Aggregate has already processed aggregate N. Thus the
338 -- Aggregate_Bounds of each sub-aggregate, is an N_Range node giving the
339 -- sub-aggregate bounds. When building the aggegate itype, this function
340 -- traverses the array aggregate N collecting such Aggregate_Bounds and
341 -- constructs the proper array aggregate itype.
343 -- Note that in the case of multidimensional aggregates each inner
344 -- sub-aggregate corresponding to a given array dimension, may provide a
345 -- different bounds. If it is possible to determine statically that
346 -- some sub-aggregates corresponding to the same index do not have the
347 -- same bounds, then a warning is emitted. If such check is not possible
348 -- statically (because some sub-aggregate bounds are dynamic expressions)
349 -- then this job is left to the expander. In all cases the particular
350 -- bounds that this function will chose for a given dimension is the first
351 -- N_Range node for a sub-aggregate corresponding to that dimension.
353 -- Note that the Raises_Constraint_Error flag of an array aggregate
354 -- whose evaluation is determined to raise CE by Resolve_Array_Aggregate,
355 -- is set in Resolve_Array_Aggregate but the aggregate is not
356 -- immediately replaced with a raise CE. In fact, Array_Aggr_Subtype must
357 -- first construct the proper itype for the aggregate (Gigi needs
358 -- this). After constructing the proper itype we will eventually replace
359 -- the top-level aggregate with a raise CE (done in Resolve_Aggregate).
360 -- Of course in cases such as:
362 -- type Arr is array (integer range <>) of Integer;
363 -- A : Arr := (positive range -1 .. 2 => 0);
365 -- The bounds of the aggregate itype are cooked up to look reasonable
366 -- (in this particular case the bounds will be 1 .. 2).
368 procedure Aggregate_Constraint_Checks
370 Check_Typ
: Entity_Id
);
371 -- Checks expression Exp against subtype Check_Typ. If Exp is an
372 -- aggregate and Check_Typ a constrained record type with discriminants,
373 -- we generate the appropriate discriminant checks. If Exp is an array
374 -- aggregate then emit the appropriate length checks. If Exp is a scalar
375 -- type, or a string literal, Exp is changed into Check_Typ'(Exp) to
376 -- ensure that range checks are performed at run time.
378 procedure Make_String_Into_Aggregate
(N
: Node_Id
);
379 -- A string literal can appear in a context in which a one dimensional
380 -- array of characters is expected. This procedure simply rewrites the
381 -- string as an aggregate, prior to resolution.
383 ---------------------------------
384 -- Aggregate_Constraint_Checks --
385 ---------------------------------
387 procedure Aggregate_Constraint_Checks
389 Check_Typ
: Entity_Id
)
391 Exp_Typ
: constant Entity_Id
:= Etype
(Exp
);
394 if Raises_Constraint_Error
(Exp
) then
398 -- This is really expansion activity, so make sure that expansion
399 -- is on and is allowed.
401 if not Expander_Active
or else In_Default_Expression
then
405 -- First check if we have to insert discriminant checks
407 if Has_Discriminants
(Exp_Typ
) then
408 Apply_Discriminant_Check
(Exp
, Check_Typ
);
410 -- Next emit length checks for array aggregates
412 elsif Is_Array_Type
(Exp_Typ
) then
413 Apply_Length_Check
(Exp
, Check_Typ
);
415 -- Finally emit scalar and string checks. If we are dealing with a
416 -- scalar literal we need to check by hand because the Etype of
417 -- literals is not necessarily correct.
419 elsif Is_Scalar_Type
(Exp_Typ
)
420 and then Compile_Time_Known_Value
(Exp
)
422 if Is_Out_Of_Range
(Exp
, Base_Type
(Check_Typ
)) then
423 Apply_Compile_Time_Constraint_Error
424 (Exp
, "value not in range of}?", CE_Range_Check_Failed
,
425 Ent
=> Base_Type
(Check_Typ
),
426 Typ
=> Base_Type
(Check_Typ
));
428 elsif Is_Out_Of_Range
(Exp
, Check_Typ
) then
429 Apply_Compile_Time_Constraint_Error
430 (Exp
, "value not in range of}?", CE_Range_Check_Failed
,
434 elsif not Range_Checks_Suppressed
(Check_Typ
) then
435 Apply_Scalar_Range_Check
(Exp
, Check_Typ
);
438 elsif (Is_Scalar_Type
(Exp_Typ
)
439 or else Nkind
(Exp
) = N_String_Literal
)
440 and then Exp_Typ
/= Check_Typ
442 if Is_Entity_Name
(Exp
)
443 and then Ekind
(Entity
(Exp
)) = E_Constant
445 -- If expression is a constant, it is worthwhile checking whether
446 -- it is a bound of the type.
448 if (Is_Entity_Name
(Type_Low_Bound
(Check_Typ
))
449 and then Entity
(Exp
) = Entity
(Type_Low_Bound
(Check_Typ
)))
450 or else (Is_Entity_Name
(Type_High_Bound
(Check_Typ
))
451 and then Entity
(Exp
) = Entity
(Type_High_Bound
(Check_Typ
)))
456 Rewrite
(Exp
, Convert_To
(Check_Typ
, Relocate_Node
(Exp
)));
457 Analyze_And_Resolve
(Exp
, Check_Typ
);
460 Rewrite
(Exp
, Convert_To
(Check_Typ
, Relocate_Node
(Exp
)));
461 Analyze_And_Resolve
(Exp
, Check_Typ
);
465 end Aggregate_Constraint_Checks
;
467 ------------------------
468 -- Array_Aggr_Subtype --
469 ------------------------
471 function Array_Aggr_Subtype
476 Aggr_Dimension
: constant Pos
:= Number_Dimensions
(Typ
);
477 -- Number of aggregate index dimensions.
479 Aggr_Range
: array (1 .. Aggr_Dimension
) of Node_Id
:= (others => Empty
);
480 -- Constrained N_Range of each index dimension in our aggregate itype.
482 Aggr_Low
: array (1 .. Aggr_Dimension
) of Node_Id
:= (others => Empty
);
483 Aggr_High
: array (1 .. Aggr_Dimension
) of Node_Id
:= (others => Empty
);
484 -- Low and High bounds for each index dimension in our aggregate itype.
486 Is_Fully_Positional
: Boolean := True;
488 procedure Collect_Aggr_Bounds
(N
: Node_Id
; Dim
: Pos
);
489 -- N is an array (sub-)aggregate. Dim is the dimension corresponding to
490 -- (sub-)aggregate N. This procedure collects the constrained N_Range
491 -- nodes corresponding to each index dimension of our aggregate itype.
492 -- These N_Range nodes are collected in Aggr_Range above.
493 -- Likewise collect in Aggr_Low & Aggr_High above the low and high
494 -- bounds of each index dimension. If, when collecting, two bounds
495 -- corresponding to the same dimension are static and found to differ,
496 -- then emit a warning, and mark N as raising Constraint_Error.
498 -------------------------
499 -- Collect_Aggr_Bounds --
500 -------------------------
502 procedure Collect_Aggr_Bounds
(N
: Node_Id
; Dim
: Pos
) is
503 This_Range
: constant Node_Id
:= Aggregate_Bounds
(N
);
504 -- The aggregate range node of this specific sub-aggregate.
506 This_Low
: constant Node_Id
:= Low_Bound
(Aggregate_Bounds
(N
));
507 This_High
: constant Node_Id
:= High_Bound
(Aggregate_Bounds
(N
));
508 -- The aggregate bounds of this specific sub-aggregate.
514 -- Collect the first N_Range for a given dimension that you find.
515 -- For a given dimension they must be all equal anyway.
517 if No
(Aggr_Range
(Dim
)) then
518 Aggr_Low
(Dim
) := This_Low
;
519 Aggr_High
(Dim
) := This_High
;
520 Aggr_Range
(Dim
) := This_Range
;
523 if Compile_Time_Known_Value
(This_Low
) then
524 if not Compile_Time_Known_Value
(Aggr_Low
(Dim
)) then
525 Aggr_Low
(Dim
) := This_Low
;
527 elsif Expr_Value
(This_Low
) /= Expr_Value
(Aggr_Low
(Dim
)) then
528 Set_Raises_Constraint_Error
(N
);
529 Error_Msg_N
("Sub-aggregate low bound mismatch?", N
);
530 Error_Msg_N
("Constraint_Error will be raised at run-time?",
535 if Compile_Time_Known_Value
(This_High
) then
536 if not Compile_Time_Known_Value
(Aggr_High
(Dim
)) then
537 Aggr_High
(Dim
) := This_High
;
540 Expr_Value
(This_High
) /= Expr_Value
(Aggr_High
(Dim
))
542 Set_Raises_Constraint_Error
(N
);
543 Error_Msg_N
("Sub-aggregate high bound mismatch?", N
);
544 Error_Msg_N
("Constraint_Error will be raised at run-time?",
550 if Dim
< Aggr_Dimension
then
552 -- Process positional components
554 if Present
(Expressions
(N
)) then
555 Expr
:= First
(Expressions
(N
));
556 while Present
(Expr
) loop
557 Collect_Aggr_Bounds
(Expr
, Dim
+ 1);
562 -- Process component associations
564 if Present
(Component_Associations
(N
)) then
565 Is_Fully_Positional
:= False;
567 Assoc
:= First
(Component_Associations
(N
));
568 while Present
(Assoc
) loop
569 Expr
:= Expression
(Assoc
);
570 Collect_Aggr_Bounds
(Expr
, Dim
+ 1);
575 end Collect_Aggr_Bounds
;
577 -- Array_Aggr_Subtype variables
580 -- the final itype of the overall aggregate
582 Index_Constraints
: List_Id
:= New_List
;
583 -- The list of index constraints of the aggregate itype.
585 -- Start of processing for Array_Aggr_Subtype
588 -- Make sure that the list of index constraints is properly attached
589 -- to the tree, and then collect the aggregate bounds.
591 Set_Parent
(Index_Constraints
, N
);
592 Collect_Aggr_Bounds
(N
, 1);
594 -- Build the list of constrained indices of our aggregate itype.
596 for J
in 1 .. Aggr_Dimension
loop
597 Create_Index
: declare
598 Index_Base
: Entity_Id
:= Base_Type
(Etype
(Aggr_Range
(J
)));
599 Index_Typ
: Entity_Id
;
602 -- Construct the Index subtype
604 Index_Typ
:= Create_Itype
(Subtype_Kind
(Ekind
(Index_Base
)), N
);
606 Set_Etype
(Index_Typ
, Index_Base
);
608 if Is_Character_Type
(Index_Base
) then
609 Set_Is_Character_Type
(Index_Typ
);
612 Set_Size_Info
(Index_Typ
, (Index_Base
));
613 Set_RM_Size
(Index_Typ
, RM_Size
(Index_Base
));
614 Set_First_Rep_Item
(Index_Typ
, First_Rep_Item
(Index_Base
));
615 Set_Scalar_Range
(Index_Typ
, Aggr_Range
(J
));
617 if Is_Discrete_Or_Fixed_Point_Type
(Index_Typ
) then
618 Set_RM_Size
(Index_Typ
, UI_From_Int
(Minimum_Size
(Index_Typ
)));
621 Set_Etype
(Aggr_Range
(J
), Index_Typ
);
623 Append
(Aggr_Range
(J
), To
=> Index_Constraints
);
627 -- Now build the Itype
629 Itype
:= Create_Itype
(E_Array_Subtype
, N
);
631 Set_First_Rep_Item
(Itype
, First_Rep_Item
(Typ
));
632 Set_Convention
(Itype
, Convention
(Typ
));
633 Set_Depends_On_Private
(Itype
, Has_Private_Component
(Typ
));
634 Set_Etype
(Itype
, Base_Type
(Typ
));
635 Set_Has_Alignment_Clause
(Itype
, Has_Alignment_Clause
(Typ
));
636 Set_Is_Aliased
(Itype
, Is_Aliased
(Typ
));
637 Set_Suppress_Index_Checks
(Itype
, Suppress_Index_Checks
(Typ
));
638 Set_Suppress_Length_Checks
(Itype
, Suppress_Length_Checks
(Typ
));
639 Set_Depends_On_Private
(Itype
, Depends_On_Private
(Typ
));
641 Set_First_Index
(Itype
, First
(Index_Constraints
));
642 Set_Is_Constrained
(Itype
, True);
643 Set_Is_Internal
(Itype
, True);
644 Init_Size_Align
(Itype
);
646 -- A simple optimization: purely positional aggregates of static
647 -- components should be passed to gigi unexpanded whenever possible,
648 -- and regardless of the staticness of the bounds themselves. Subse-
649 -- quent checks in exp_aggr verify that type is not packed, etc.
651 Set_Size_Known_At_Compile_Time
(Itype
,
653 and then Comes_From_Source
(N
)
654 and then Size_Known_At_Compile_Time
(Component_Type
(Typ
)));
656 -- We always need a freeze node for a packed array subtype, so that
657 -- we can build the Packed_Array_Type corresponding to the subtype.
658 -- If expansion is disabled, the packed array subtype is not built,
659 -- and we must not generate a freeze node for the type, or else it
660 -- will appear incomplete to gigi.
662 if Is_Packed
(Itype
) and then not In_Default_Expression
663 and then Expander_Active
665 Freeze_Itype
(Itype
, N
);
669 end Array_Aggr_Subtype
;
671 --------------------------------
672 -- Check_Misspelled_Component --
673 --------------------------------
675 procedure Check_Misspelled_Component
676 (Elements
: Elist_Id
;
679 Max_Suggestions
: constant := 2;
681 Nr_Of_Suggestions
: Natural := 0;
682 Suggestion_1
: Entity_Id
:= Empty
;
683 Suggestion_2
: Entity_Id
:= Empty
;
684 Component_Elmt
: Elmt_Id
;
687 -- All the components of List are matched against Component and
688 -- a count is maintained of possible misspellings. When at the
689 -- end of the analysis there are one or two (not more!) possible
690 -- misspellings, these misspellings will be suggested as
691 -- possible correction.
693 Get_Name_String
(Chars
(Component
));
696 S
: constant String (1 .. Name_Len
) :=
697 Name_Buffer
(1 .. Name_Len
);
701 Component_Elmt
:= First_Elmt
(Elements
);
703 while Nr_Of_Suggestions
<= Max_Suggestions
704 and then Present
(Component_Elmt
)
707 Get_Name_String
(Chars
(Node
(Component_Elmt
)));
709 if Is_Bad_Spelling_Of
(Name_Buffer
(1 .. Name_Len
), S
) then
710 Nr_Of_Suggestions
:= Nr_Of_Suggestions
+ 1;
712 case Nr_Of_Suggestions
is
713 when 1 => Suggestion_1
:= Node
(Component_Elmt
);
714 when 2 => Suggestion_2
:= Node
(Component_Elmt
);
719 Next_Elmt
(Component_Elmt
);
722 -- Report at most two suggestions
724 if Nr_Of_Suggestions
= 1 then
725 Error_Msg_NE
("\possible misspelling of&",
726 Component
, Suggestion_1
);
728 elsif Nr_Of_Suggestions
= 2 then
729 Error_Msg_Node_2
:= Suggestion_2
;
730 Error_Msg_NE
("\possible misspelling of& or&",
731 Component
, Suggestion_1
);
734 end Check_Misspelled_Component
;
736 ----------------------------------------
737 -- Check_Static_Discriminated_Subtype --
738 ----------------------------------------
740 procedure Check_Static_Discriminated_Subtype
(T
: Entity_Id
; V
: Node_Id
) is
741 Disc
: constant Entity_Id
:= First_Discriminant
(T
);
746 if Has_Record_Rep_Clause
(T
) then
749 elsif Present
(Next_Discriminant
(Disc
)) then
752 elsif Nkind
(V
) /= N_Integer_Literal
then
756 Comp
:= First_Component
(T
);
758 while Present
(Comp
) loop
760 if Is_Scalar_Type
(Etype
(Comp
)) then
763 elsif Is_Private_Type
(Etype
(Comp
))
764 and then Present
(Full_View
(Etype
(Comp
)))
765 and then Is_Scalar_Type
(Full_View
(Etype
(Comp
)))
769 elsif Is_Array_Type
(Etype
(Comp
)) then
771 if Is_Bit_Packed_Array
(Etype
(Comp
)) then
775 Ind
:= First_Index
(Etype
(Comp
));
777 while Present
(Ind
) loop
779 if Nkind
(Ind
) /= N_Range
780 or else Nkind
(Low_Bound
(Ind
)) /= N_Integer_Literal
781 or else Nkind
(High_Bound
(Ind
)) /= N_Integer_Literal
793 Next_Component
(Comp
);
796 -- On exit, all components have statically known sizes.
798 Set_Size_Known_At_Compile_Time
(T
);
799 end Check_Static_Discriminated_Subtype
;
801 --------------------------------
802 -- Make_String_Into_Aggregate --
803 --------------------------------
805 procedure Make_String_Into_Aggregate
(N
: Node_Id
) is
808 Exprs
: List_Id
:= New_List
;
809 Loc
: constant Source_Ptr
:= Sloc
(N
);
811 P
: Source_Ptr
:= Loc
+ 1;
812 Str
: constant String_Id
:= Strval
(N
);
813 Strlen
: constant Nat
:= String_Length
(Str
);
816 for J
in 1 .. Strlen
loop
817 C
:= Get_String_Char
(Str
, J
);
818 Set_Character_Literal_Name
(C
);
820 C_Node
:= Make_Character_Literal
(P
, Name_Find
, C
);
821 Set_Etype
(C_Node
, Any_Character
);
822 Append_To
(Exprs
, C_Node
);
825 -- something special for wide strings ?
828 New_N
:= Make_Aggregate
(Loc
, Expressions
=> Exprs
);
829 Set_Analyzed
(New_N
);
830 Set_Etype
(New_N
, Any_Composite
);
833 end Make_String_Into_Aggregate
;
835 -----------------------
836 -- Resolve_Aggregate --
837 -----------------------
839 procedure Resolve_Aggregate
(N
: Node_Id
; Typ
: Entity_Id
) is
840 Pkind
: constant Node_Kind
:= Nkind
(Parent
(N
));
842 Aggr_Subtyp
: Entity_Id
;
843 -- The actual aggregate subtype. This is not necessarily the same as Typ
844 -- which is the subtype of the context in which the aggregate was found.
847 if Is_Limited_Type
(Typ
) then
848 Error_Msg_N
("aggregate type cannot be limited", N
);
850 elsif Is_Limited_Composite
(Typ
) then
851 Error_Msg_N
("aggregate type cannot have limited component", N
);
853 elsif Is_Class_Wide_Type
(Typ
) then
854 Error_Msg_N
("type of aggregate cannot be class-wide", N
);
856 elsif Typ
= Any_String
857 or else Typ
= Any_Composite
859 Error_Msg_N
("no unique type for aggregate", N
);
860 Set_Etype
(N
, Any_Composite
);
862 elsif Is_Array_Type
(Typ
) and then Null_Record_Present
(N
) then
863 Error_Msg_N
("null record forbidden in array aggregate", N
);
865 elsif Is_Record_Type
(Typ
) then
866 Resolve_Record_Aggregate
(N
, Typ
);
868 elsif Is_Array_Type
(Typ
) then
870 -- First a special test, for the case of a positional aggregate
871 -- of characters which can be replaced by a string literal.
872 -- Do not perform this transformation if this was a string literal
873 -- to start with, whose components needed constraint checks, or if
874 -- the component type is non-static, because it will require those
875 -- checks and be transformed back into an aggregate.
877 if Number_Dimensions
(Typ
) = 1
879 (Root_Type
(Component_Type
(Typ
)) = Standard_Character
881 Root_Type
(Component_Type
(Typ
)) = Standard_Wide_Character
)
882 and then No
(Component_Associations
(N
))
883 and then not Is_Limited_Composite
(Typ
)
884 and then not Is_Private_Composite
(Typ
)
885 and then not Is_Bit_Packed_Array
(Typ
)
886 and then Nkind
(Original_Node
(Parent
(N
))) /= N_String_Literal
887 and then Is_Static_Subtype
(Component_Type
(Typ
))
893 Expr
:= First
(Expressions
(N
));
894 while Present
(Expr
) loop
895 exit when Nkind
(Expr
) /= N_Character_Literal
;
902 Expr
:= First
(Expressions
(N
));
903 while Present
(Expr
) loop
904 Store_String_Char
(Char_Literal_Value
(Expr
));
909 Make_String_Literal
(Sloc
(N
), End_String
));
911 Analyze_And_Resolve
(N
, Typ
);
917 -- Here if we have a real aggregate to deal with
919 Array_Aggregate
: declare
920 Aggr_Resolved
: Boolean;
921 Aggr_Typ
: Entity_Id
:= Etype
(Typ
);
922 -- This is the unconstrained array type, which is the type
923 -- against which the aggregate is to be resoved. Typ itself
924 -- is the array type of the context which may not be the same
925 -- subtype as the subtype for the final aggregate.
928 -- In the following we determine whether an others choice is
929 -- allowed inside the array aggregate. The test checks the context
930 -- in which the array aggregate occurs. If the context does not
931 -- permit it, or the aggregate type is unconstrained, an others
932 -- choice is not allowed.
934 -- Note that there is no node for Explicit_Actual_Parameter.
935 -- To test for this context we therefore have to test for node
936 -- N_Parameter_Association which itself appears only if there is a
937 -- formal parameter. Consequently we also need to test for
938 -- N_Procedure_Call_Statement or N_Function_Call.
940 if Is_Constrained
(Typ
) and then
941 (Pkind
= N_Assignment_Statement
or else
942 Pkind
= N_Parameter_Association
or else
943 Pkind
= N_Function_Call
or else
944 Pkind
= N_Procedure_Call_Statement
or else
945 Pkind
= N_Generic_Association
or else
946 Pkind
= N_Formal_Object_Declaration
or else
947 Pkind
= N_Return_Statement
or else
948 Pkind
= N_Object_Declaration
or else
949 Pkind
= N_Component_Declaration
or else
950 Pkind
= N_Parameter_Specification
or else
951 Pkind
= N_Qualified_Expression
or else
952 Pkind
= N_Aggregate
or else
953 Pkind
= N_Extension_Aggregate
or else
954 Pkind
= N_Component_Association
)
957 Resolve_Array_Aggregate
959 Index
=> First_Index
(Aggr_Typ
),
960 Index_Constr
=> First_Index
(Typ
),
961 Component_Typ
=> Component_Type
(Typ
),
962 Others_Allowed
=> True);
966 Resolve_Array_Aggregate
968 Index
=> First_Index
(Aggr_Typ
),
969 Index_Constr
=> First_Index
(Aggr_Typ
),
970 Component_Typ
=> Component_Type
(Typ
),
971 Others_Allowed
=> False);
974 if not Aggr_Resolved
then
975 Aggr_Subtyp
:= Any_Composite
;
977 Aggr_Subtyp
:= Array_Aggr_Subtype
(N
, Typ
);
980 Set_Etype
(N
, Aggr_Subtyp
);
984 Error_Msg_N
("illegal context for aggregate", N
);
988 -- If we can determine statically that the evaluation of the
989 -- aggregate raises Constraint_Error, then replace the
990 -- aggregate with an N_Raise_Constraint_Error node, but set the
991 -- Etype to the right aggregate subtype. Gigi needs this.
993 if Raises_Constraint_Error
(N
) then
994 Aggr_Subtyp
:= Etype
(N
);
996 Make_Raise_Constraint_Error
(Sloc
(N
),
997 Reason
=> CE_Range_Check_Failed
));
998 Set_Raises_Constraint_Error
(N
);
999 Set_Etype
(N
, Aggr_Subtyp
);
1003 end Resolve_Aggregate
;
1005 -----------------------------
1006 -- Resolve_Array_Aggregate --
1007 -----------------------------
1009 function Resolve_Array_Aggregate
1012 Index_Constr
: Node_Id
;
1013 Component_Typ
: Entity_Id
;
1014 Others_Allowed
: Boolean)
1017 Loc
: constant Source_Ptr
:= Sloc
(N
);
1019 Failure
: constant Boolean := False;
1020 Success
: constant Boolean := True;
1022 Index_Typ
: constant Entity_Id
:= Etype
(Index
);
1023 Index_Typ_Low
: constant Node_Id
:= Type_Low_Bound
(Index_Typ
);
1024 Index_Typ_High
: constant Node_Id
:= Type_High_Bound
(Index_Typ
);
1025 -- The type of the index corresponding to the array sub-aggregate
1026 -- along with its low and upper bounds
1028 Index_Base
: constant Entity_Id
:= Base_Type
(Index_Typ
);
1029 Index_Base_Low
: constant Node_Id
:= Type_Low_Bound
(Index_Base
);
1030 Index_Base_High
: constant Node_Id
:= Type_High_Bound
(Index_Base
);
1031 -- ditto for the base type
1033 function Add
(Val
: Uint
; To
: Node_Id
) return Node_Id
;
1034 -- Creates a new expression node where Val is added to expression To.
1035 -- Tries to constant fold whenever possible. To must be an already
1036 -- analyzed expression.
1038 procedure Check_Bound
(BH
: Node_Id
; AH
: in out Node_Id
);
1039 -- Checks that AH (the upper bound of an array aggregate) is <= BH
1040 -- (the upper bound of the index base type). If the check fails a
1041 -- warning is emitted, the Raises_Constraint_Error Flag of N is set,
1042 -- and AH is replaced with a duplicate of BH.
1044 procedure Check_Bounds
(L
, H
: Node_Id
; AL
, AH
: Node_Id
);
1045 -- Checks that range AL .. AH is compatible with range L .. H. Emits a
1046 -- warning if not and sets the Raises_Constraint_Error Flag in N.
1048 procedure Check_Length
(L
, H
: Node_Id
; Len
: Uint
);
1049 -- Checks that range L .. H contains at least Len elements. Emits a
1050 -- warning if not and sets the Raises_Constraint_Error Flag in N.
1052 function Dynamic_Or_Null_Range
(L
, H
: Node_Id
) return Boolean;
1053 -- Returns True if range L .. H is dynamic or null.
1055 procedure Get
(Value
: out Uint
; From
: Node_Id
; OK
: out Boolean);
1056 -- Given expression node From, this routine sets OK to False if it
1057 -- cannot statically evaluate From. Otherwise it stores this static
1058 -- value into Value.
1060 function Resolve_Aggr_Expr
1062 Single_Elmt
: Boolean)
1064 -- Resolves aggregate expression Expr. Returs False if resolution
1065 -- fails. If Single_Elmt is set to False, the expression Expr may be
1066 -- used to initialize several array aggregate elements (this can
1067 -- happen for discrete choices such as "L .. H => Expr" or the others
1068 -- choice). In this event we do not resolve Expr unless expansion is
1069 -- disabled. To know why, see the DELAYED COMPONENT RESOLUTION
1076 function Add
(Val
: Uint
; To
: Node_Id
) return Node_Id
is
1082 if Raises_Constraint_Error
(To
) then
1086 -- First test if we can do constant folding
1088 if Compile_Time_Known_Value
(To
)
1089 or else Nkind
(To
) = N_Integer_Literal
1091 Expr_Pos
:= Make_Integer_Literal
(Loc
, Expr_Value
(To
) + Val
);
1092 Set_Is_Static_Expression
(Expr_Pos
);
1093 Set_Etype
(Expr_Pos
, Etype
(To
));
1094 Set_Analyzed
(Expr_Pos
, Analyzed
(To
));
1096 if not Is_Enumeration_Type
(Index_Typ
) then
1099 -- If we are dealing with enumeration return
1100 -- Index_Typ'Val (Expr_Pos)
1104 Make_Attribute_Reference
1106 Prefix
=> New_Reference_To
(Index_Typ
, Loc
),
1107 Attribute_Name
=> Name_Val
,
1108 Expressions
=> New_List
(Expr_Pos
));
1114 -- If we are here no constant folding possible
1116 if not Is_Enumeration_Type
(Index_Base
) then
1119 Left_Opnd
=> Duplicate_Subexpr
(To
),
1120 Right_Opnd
=> Make_Integer_Literal
(Loc
, Val
));
1122 -- If we are dealing with enumeration return
1123 -- Index_Typ'Val (Index_Typ'Pos (To) + Val)
1127 Make_Attribute_Reference
1129 Prefix
=> New_Reference_To
(Index_Typ
, Loc
),
1130 Attribute_Name
=> Name_Pos
,
1131 Expressions
=> New_List
(Duplicate_Subexpr
(To
)));
1135 Left_Opnd
=> To_Pos
,
1136 Right_Opnd
=> Make_Integer_Literal
(Loc
, Val
));
1139 Make_Attribute_Reference
1141 Prefix
=> New_Reference_To
(Index_Typ
, Loc
),
1142 Attribute_Name
=> Name_Val
,
1143 Expressions
=> New_List
(Expr_Pos
));
1153 procedure Check_Bound
(BH
: Node_Id
; AH
: in out Node_Id
) is
1161 Get
(Value
=> Val_BH
, From
=> BH
, OK
=> OK_BH
);
1162 Get
(Value
=> Val_AH
, From
=> AH
, OK
=> OK_AH
);
1164 if OK_BH
and then OK_AH
and then Val_BH
< Val_AH
then
1165 Set_Raises_Constraint_Error
(N
);
1166 Error_Msg_N
("upper bound out of range?", AH
);
1167 Error_Msg_N
("Constraint_Error will be raised at run-time?", AH
);
1169 -- You need to set AH to BH or else in the case of enumerations
1170 -- indices we will not be able to resolve the aggregate bounds.
1172 AH
:= Duplicate_Subexpr
(BH
);
1180 procedure Check_Bounds
(L
, H
: Node_Id
; AL
, AH
: Node_Id
) is
1192 if Raises_Constraint_Error
(N
)
1193 or else Dynamic_Or_Null_Range
(AL
, AH
)
1198 Get
(Value
=> Val_L
, From
=> L
, OK
=> OK_L
);
1199 Get
(Value
=> Val_H
, From
=> H
, OK
=> OK_H
);
1201 Get
(Value
=> Val_AL
, From
=> AL
, OK
=> OK_AL
);
1202 Get
(Value
=> Val_AH
, From
=> AH
, OK
=> OK_AH
);
1204 if OK_L
and then Val_L
> Val_AL
then
1205 Set_Raises_Constraint_Error
(N
);
1206 Error_Msg_N
("lower bound of aggregate out of range?", N
);
1207 Error_Msg_N
("Constraint_Error will be raised at run-time?", N
);
1210 if OK_H
and then Val_H
< Val_AH
then
1211 Set_Raises_Constraint_Error
(N
);
1212 Error_Msg_N
("upper bound of aggregate out of range?", N
);
1213 Error_Msg_N
("Constraint_Error will be raised at run-time?", N
);
1221 procedure Check_Length
(L
, H
: Node_Id
; Len
: Uint
) is
1231 if Raises_Constraint_Error
(N
) then
1235 Get
(Value
=> Val_L
, From
=> L
, OK
=> OK_L
);
1236 Get
(Value
=> Val_H
, From
=> H
, OK
=> OK_H
);
1238 if not OK_L
or else not OK_H
then
1242 -- If null range length is zero
1244 if Val_L
> Val_H
then
1245 Range_Len
:= Uint_0
;
1247 Range_Len
:= Val_H
- Val_L
+ 1;
1250 if Range_Len
< Len
then
1251 Set_Raises_Constraint_Error
(N
);
1252 Error_Msg_N
("Too many elements?", N
);
1253 Error_Msg_N
("Constraint_Error will be raised at run-time?", N
);
1257 ---------------------------
1258 -- Dynamic_Or_Null_Range --
1259 ---------------------------
1261 function Dynamic_Or_Null_Range
(L
, H
: Node_Id
) return Boolean is
1269 Get
(Value
=> Val_L
, From
=> L
, OK
=> OK_L
);
1270 Get
(Value
=> Val_H
, From
=> H
, OK
=> OK_H
);
1272 return not OK_L
or else not OK_H
1273 or else not Is_OK_Static_Expression
(L
)
1274 or else not Is_OK_Static_Expression
(H
)
1275 or else Val_L
> Val_H
;
1276 end Dynamic_Or_Null_Range
;
1282 procedure Get
(Value
: out Uint
; From
: Node_Id
; OK
: out Boolean) is
1286 if Compile_Time_Known_Value
(From
) then
1287 Value
:= Expr_Value
(From
);
1289 -- If expression From is something like Some_Type'Val (10) then
1292 elsif Nkind
(From
) = N_Attribute_Reference
1293 and then Attribute_Name
(From
) = Name_Val
1294 and then Compile_Time_Known_Value
(First
(Expressions
(From
)))
1296 Value
:= Expr_Value
(First
(Expressions
(From
)));
1304 -----------------------
1305 -- Resolve_Aggr_Expr --
1306 -----------------------
1308 function Resolve_Aggr_Expr
1310 Single_Elmt
: Boolean)
1313 Nxt_Ind
: Node_Id
:= Next_Index
(Index
);
1314 Nxt_Ind_Constr
: Node_Id
:= Next_Index
(Index_Constr
);
1315 -- Index is the current index corresponding to the expression.
1317 Resolution_OK
: Boolean := True;
1318 -- Set to False if resolution of the expression failed.
1321 -- If the array type against which we are resolving the aggregate
1322 -- has several dimensions, the expressions nested inside the
1323 -- aggregate must be further aggregates (or strings).
1325 if Present
(Nxt_Ind
) then
1326 if Nkind
(Expr
) /= N_Aggregate
then
1328 -- A string literal can appear where a one-dimensional array
1329 -- of characters is expected. If the literal looks like an
1330 -- operator, it is still an operator symbol, which will be
1331 -- transformed into a string when analyzed.
1333 if Is_Character_Type
(Component_Typ
)
1334 and then No
(Next_Index
(Nxt_Ind
))
1335 and then (Nkind
(Expr
) = N_String_Literal
1336 or else Nkind
(Expr
) = N_Operator_Symbol
)
1338 -- A string literal used in a multidimensional array
1339 -- aggregate in place of the final one-dimensional
1340 -- aggregate must not be enclosed in parentheses.
1342 if Paren_Count
(Expr
) /= 0 then
1343 Error_Msg_N
("No parenthesis allowed here", Expr
);
1346 Make_String_Into_Aggregate
(Expr
);
1349 Error_Msg_N
("nested array aggregate expected", Expr
);
1354 Resolution_OK
:= Resolve_Array_Aggregate
1355 (Expr
, Nxt_Ind
, Nxt_Ind_Constr
, Component_Typ
, Others_Allowed
);
1357 -- Do not resolve the expressions of discrete or others choices
1358 -- unless the expression covers a single component, or the expander
1362 or else not Expander_Active
1363 or else In_Default_Expression
1365 Analyze_And_Resolve
(Expr
, Component_Typ
);
1366 Check_Non_Static_Context
(Expr
);
1367 Aggregate_Constraint_Checks
(Expr
, Component_Typ
);
1370 if Raises_Constraint_Error
(Expr
)
1371 and then Nkind
(Parent
(Expr
)) /= N_Component_Association
1373 Set_Raises_Constraint_Error
(N
);
1376 return Resolution_OK
;
1377 end Resolve_Aggr_Expr
;
1379 -- Variables local to Resolve_Array_Aggregate
1385 Who_Cares
: Node_Id
;
1387 Aggr_Low
: Node_Id
:= Empty
;
1388 Aggr_High
: Node_Id
:= Empty
;
1389 -- The actual low and high bounds of this sub-aggegate
1391 Choices_Low
: Node_Id
:= Empty
;
1392 Choices_High
: Node_Id
:= Empty
;
1393 -- The lowest and highest discrete choices values for a named aggregate
1395 Nb_Elements
: Uint
:= Uint_0
;
1396 -- The number of elements in a positional aggegate
1398 Others_Present
: Boolean := False;
1400 Nb_Choices
: Nat
:= 0;
1401 -- Contains the overall number of named choices in this sub-aggregate
1403 Nb_Discrete_Choices
: Nat
:= 0;
1404 -- The overall number of discrete choices (not counting others choice)
1406 Case_Table_Size
: Nat
;
1407 -- Contains the size of the case table needed to sort aggregate choices
1409 -- Start of processing for Resolve_Array_Aggregate
1412 -- STEP 1: make sure the aggregate is correctly formatted
1414 if Present
(Component_Associations
(N
)) then
1415 Assoc
:= First
(Component_Associations
(N
));
1416 while Present
(Assoc
) loop
1417 Choice
:= First
(Choices
(Assoc
));
1418 while Present
(Choice
) loop
1419 if Nkind
(Choice
) = N_Others_Choice
then
1420 Others_Present
:= True;
1422 if Choice
/= First
(Choices
(Assoc
))
1423 or else Present
(Next
(Choice
))
1426 ("OTHERS must appear alone in a choice list", Choice
);
1430 if Present
(Next
(Assoc
)) then
1432 ("OTHERS must appear last in an aggregate", Choice
);
1437 and then Assoc
/= First
(Component_Associations
(N
))
1438 and then (Nkind
(Parent
(N
)) = N_Assignment_Statement
1440 Nkind
(Parent
(N
)) = N_Object_Declaration
)
1443 ("(Ada 83) illegal context for OTHERS choice", N
);
1447 Nb_Choices
:= Nb_Choices
+ 1;
1455 -- At this point we know that the others choice, if present, is by
1456 -- itself and appears last in the aggregate. Check if we have mixed
1457 -- positional and discrete associations (other than the others choice).
1459 if Present
(Expressions
(N
))
1460 and then (Nb_Choices
> 1
1461 or else (Nb_Choices
= 1 and then not Others_Present
))
1464 ("named association cannot follow positional association",
1465 First
(Choices
(First
(Component_Associations
(N
)))));
1469 -- Test for the validity of an others choice if present
1471 if Others_Present
and then not Others_Allowed
then
1473 ("OTHERS choice not allowed here",
1474 First
(Choices
(First
(Component_Associations
(N
)))));
1478 -- Protect against cascaded errors
1480 if Etype
(Index_Typ
) = Any_Type
then
1484 -- STEP 2: Process named components
1486 if No
(Expressions
(N
)) then
1488 if Others_Present
then
1489 Case_Table_Size
:= Nb_Choices
- 1;
1491 Case_Table_Size
:= Nb_Choices
;
1497 -- Denote the lowest and highest values in an aggregate choice
1501 -- High end of one range and Low end of the next. Should be
1502 -- contiguous if there is no hole in the list of values.
1504 Missing_Values
: Boolean;
1505 -- Set True if missing index values
1507 S_Low
: Node_Id
:= Empty
;
1508 S_High
: Node_Id
:= Empty
;
1509 -- if a choice in an aggregate is a subtype indication these
1510 -- denote the lowest and highest values of the subtype
1512 Table
: Case_Table_Type
(1 .. Case_Table_Size
);
1513 -- Used to sort all the different choice values
1515 Single_Choice
: Boolean;
1516 -- Set to true every time there is a single discrete choice in a
1517 -- discrete association
1519 Prev_Nb_Discrete_Choices
: Nat
;
1520 -- Used to keep track of the number of discrete choices
1521 -- in the current association.
1524 -- STEP 2 (A): Check discrete choices validity.
1526 Assoc
:= First
(Component_Associations
(N
));
1527 while Present
(Assoc
) loop
1529 Prev_Nb_Discrete_Choices
:= Nb_Discrete_Choices
;
1530 Choice
:= First
(Choices
(Assoc
));
1534 if Nkind
(Choice
) = N_Others_Choice
then
1535 Single_Choice
:= False;
1538 -- Test for subtype mark without constraint
1540 elsif Is_Entity_Name
(Choice
) and then
1541 Is_Type
(Entity
(Choice
))
1543 if Base_Type
(Entity
(Choice
)) /= Index_Base
then
1545 ("invalid subtype mark in aggregate choice",
1550 elsif Nkind
(Choice
) = N_Subtype_Indication
then
1551 Resolve_Discrete_Subtype_Indication
(Choice
, Index_Base
);
1553 -- Does the subtype indication evaluation raise CE ?
1555 Get_Index_Bounds
(Subtype_Mark
(Choice
), S_Low
, S_High
);
1556 Get_Index_Bounds
(Choice
, Low
, High
);
1557 Check_Bounds
(S_Low
, S_High
, Low
, High
);
1559 else -- Choice is a range or an expression
1560 Resolve
(Choice
, Index_Base
);
1561 Check_Non_Static_Context
(Choice
);
1563 -- Do not range check a choice. This check is redundant
1564 -- since this test is already performed when we check
1565 -- that the bounds of the array aggregate are within
1568 Set_Do_Range_Check
(Choice
, False);
1571 -- If we could not resolve the discrete choice stop here
1573 if Etype
(Choice
) = Any_Type
then
1576 -- If the discrete choice raises CE get its original bounds.
1578 elsif Nkind
(Choice
) = N_Raise_Constraint_Error
then
1579 Set_Raises_Constraint_Error
(N
);
1580 Get_Index_Bounds
(Original_Node
(Choice
), Low
, High
);
1582 -- Otherwise get its bounds as usual
1585 Get_Index_Bounds
(Choice
, Low
, High
);
1588 if (Dynamic_Or_Null_Range
(Low
, High
)
1589 or else (Nkind
(Choice
) = N_Subtype_Indication
1591 Dynamic_Or_Null_Range
(S_Low
, S_High
)))
1592 and then Nb_Choices
/= 1
1595 ("dynamic or empty choice in aggregate " &
1596 "must be the only choice", Choice
);
1600 Nb_Discrete_Choices
:= Nb_Discrete_Choices
+ 1;
1601 Table
(Nb_Discrete_Choices
).Choice_Lo
:= Low
;
1602 Table
(Nb_Discrete_Choices
).Choice_Hi
:= High
;
1607 -- Check if we have a single discrete choice and whether
1608 -- this discrete choice specifies a single value.
1611 (Nb_Discrete_Choices
= Prev_Nb_Discrete_Choices
+ 1)
1612 and then (Low
= High
);
1620 (Expression
(Assoc
), Single_Elmt
=> Single_Choice
)
1628 -- If aggregate contains more than one choice then these must be
1629 -- static. Sort them and check that they are contiguous
1631 if Nb_Discrete_Choices
> 1 then
1632 Sort_Case_Table
(Table
);
1633 Missing_Values
:= False;
1635 Outer
: for J
in 1 .. Nb_Discrete_Choices
- 1 loop
1636 if Expr_Value
(Table
(J
).Choice_Hi
) >=
1637 Expr_Value
(Table
(J
+ 1).Choice_Lo
)
1640 ("duplicate choice values in array aggregate",
1641 Table
(J
).Choice_Hi
);
1644 elsif not Others_Present
then
1646 Hi_Val
:= Expr_Value
(Table
(J
).Choice_Hi
);
1647 Lo_Val
:= Expr_Value
(Table
(J
+ 1).Choice_Lo
);
1649 -- If missing values, output error messages
1651 if Lo_Val
- Hi_Val
> 1 then
1653 -- Header message if not first missing value
1655 if not Missing_Values
then
1657 ("missing index value(s) in array aggregate", N
);
1658 Missing_Values
:= True;
1661 -- Output values of missing indexes
1663 Lo_Val
:= Lo_Val
- 1;
1664 Hi_Val
:= Hi_Val
+ 1;
1666 -- Enumeration type case
1668 if Is_Enumeration_Type
(Index_Typ
) then
1671 (Get_Enum_Lit_From_Pos
1672 (Index_Typ
, Hi_Val
, Loc
));
1674 if Lo_Val
= Hi_Val
then
1675 Error_Msg_N
("\ %", N
);
1679 (Get_Enum_Lit_From_Pos
1680 (Index_Typ
, Lo_Val
, Loc
));
1681 Error_Msg_N
("\ % .. %", N
);
1684 -- Integer types case
1687 Error_Msg_Uint_1
:= Hi_Val
;
1689 if Lo_Val
= Hi_Val
then
1690 Error_Msg_N
("\ ^", N
);
1692 Error_Msg_Uint_2
:= Lo_Val
;
1693 Error_Msg_N
("\ ^ .. ^", N
);
1700 if Missing_Values
then
1701 Set_Etype
(N
, Any_Composite
);
1706 -- STEP 2 (B): Compute aggregate bounds and min/max choices values
1708 if Nb_Discrete_Choices
> 0 then
1709 Choices_Low
:= Table
(1).Choice_Lo
;
1710 Choices_High
:= Table
(Nb_Discrete_Choices
).Choice_Hi
;
1713 if Others_Present
then
1714 Get_Index_Bounds
(Index_Constr
, Aggr_Low
, Aggr_High
);
1717 Aggr_Low
:= Choices_Low
;
1718 Aggr_High
:= Choices_High
;
1722 -- STEP 3: Process positional components
1725 -- STEP 3 (A): Process positional elements
1727 Expr
:= First
(Expressions
(N
));
1728 Nb_Elements
:= Uint_0
;
1729 while Present
(Expr
) loop
1730 Nb_Elements
:= Nb_Elements
+ 1;
1732 if not Resolve_Aggr_Expr
(Expr
, Single_Elmt
=> True) then
1739 if Others_Present
then
1740 Assoc
:= Last
(Component_Associations
(N
));
1741 if not Resolve_Aggr_Expr
(Expression
(Assoc
),
1742 Single_Elmt
=> False)
1748 -- STEP 3 (B): Compute the aggregate bounds
1750 if Others_Present
then
1751 Get_Index_Bounds
(Index_Constr
, Aggr_Low
, Aggr_High
);
1754 if Others_Allowed
then
1755 Get_Index_Bounds
(Index_Constr
, Aggr_Low
, Who_Cares
);
1757 Aggr_Low
:= Index_Typ_Low
;
1760 Aggr_High
:= Add
(Nb_Elements
- 1, To
=> Aggr_Low
);
1761 Check_Bound
(Index_Base_High
, Aggr_High
);
1765 -- STEP 4: Perform static aggregate checks and save the bounds
1769 Check_Bounds
(Index_Typ_Low
, Index_Typ_High
, Aggr_Low
, Aggr_High
);
1770 Check_Bounds
(Index_Base_Low
, Index_Base_High
, Aggr_Low
, Aggr_High
);
1774 if Others_Present
and then Nb_Discrete_Choices
> 0 then
1775 Check_Bounds
(Aggr_Low
, Aggr_High
, Choices_Low
, Choices_High
);
1776 Check_Bounds
(Index_Typ_Low
, Index_Typ_High
,
1777 Choices_Low
, Choices_High
);
1778 Check_Bounds
(Index_Base_Low
, Index_Base_High
,
1779 Choices_Low
, Choices_High
);
1783 elsif Others_Present
and then Nb_Elements
> 0 then
1784 Check_Length
(Aggr_Low
, Aggr_High
, Nb_Elements
);
1785 Check_Length
(Index_Typ_Low
, Index_Typ_High
, Nb_Elements
);
1786 Check_Length
(Index_Base_Low
, Index_Base_High
, Nb_Elements
);
1790 if Raises_Constraint_Error
(Aggr_Low
)
1791 or else Raises_Constraint_Error
(Aggr_High
)
1793 Set_Raises_Constraint_Error
(N
);
1796 Aggr_Low
:= Duplicate_Subexpr
(Aggr_Low
);
1798 -- Do not duplicate Aggr_High if Aggr_High = Aggr_Low + Nb_Elements
1799 -- since the addition node returned by Add is not yet analyzed. Attach
1800 -- to tree and analyze first. Reset analyzed flag to insure it will get
1801 -- analyzed when it is a literal bound whose type must be properly
1804 if Others_Present
or else Nb_Discrete_Choices
> 0 then
1805 Aggr_High
:= Duplicate_Subexpr
(Aggr_High
);
1807 if Etype
(Aggr_High
) = Universal_Integer
then
1808 Set_Analyzed
(Aggr_High
, False);
1812 Set_Aggregate_Bounds
1813 (N
, Make_Range
(Loc
, Low_Bound
=> Aggr_Low
, High_Bound
=> Aggr_High
));
1815 -- The bounds may contain expressions that must be inserted upwards.
1816 -- Attach them fully to the tree. After analysis, remove side effects
1817 -- from upper bound, if still needed.
1819 Set_Parent
(Aggregate_Bounds
(N
), N
);
1820 Analyze_And_Resolve
(Aggregate_Bounds
(N
), Index_Typ
);
1822 if not Others_Present
and then Nb_Discrete_Choices
= 0 then
1823 Set_High_Bound
(Aggregate_Bounds
(N
),
1824 Duplicate_Subexpr
(High_Bound
(Aggregate_Bounds
(N
))));
1828 end Resolve_Array_Aggregate
;
1830 ---------------------------------
1831 -- Resolve_Extension_Aggregate --
1832 ---------------------------------
1834 -- There are two cases to consider:
1836 -- a) If the ancestor part is a type mark, the components needed are
1837 -- the difference between the components of the expected type and the
1838 -- components of the given type mark.
1840 -- b) If the ancestor part is an expression, it must be unambiguous,
1841 -- and once we have its type we can also compute the needed components
1842 -- as in the previous case. In both cases, if the ancestor type is not
1843 -- the immediate ancestor, we have to build this ancestor recursively.
1845 -- In both cases discriminants of the ancestor type do not play a
1846 -- role in the resolution of the needed components, because inherited
1847 -- discriminants cannot be used in a type extension. As a result we can
1848 -- compute independently the list of components of the ancestor type and
1849 -- of the expected type.
1851 procedure Resolve_Extension_Aggregate
(N
: Node_Id
; Typ
: Entity_Id
) is
1852 A
: constant Node_Id
:= Ancestor_Part
(N
);
1856 Imm_Type
: Entity_Id
;
1858 function Valid_Ancestor_Type
return Boolean;
1859 -- Verify that the type of the ancestor part is a non-private ancestor
1860 -- of the expected type.
1862 function Valid_Ancestor_Type
return Boolean is
1863 Imm_Type
: Entity_Id
;
1866 Imm_Type
:= Base_Type
(Typ
);
1867 while Is_Derived_Type
(Imm_Type
)
1868 and then Etype
(Imm_Type
) /= Base_Type
(A_Type
)
1870 Imm_Type
:= Etype
(Base_Type
(Imm_Type
));
1873 if Etype
(Imm_Type
) /= Base_Type
(A_Type
) then
1874 Error_Msg_NE
("expect ancestor type of &", A
, Typ
);
1879 end Valid_Ancestor_Type
;
1881 -- Start of processing for Resolve_Extension_Aggregate
1886 if not Is_Tagged_Type
(Typ
) then
1887 Error_Msg_N
("type of extension aggregate must be tagged", N
);
1890 elsif Is_Limited_Type
(Typ
) then
1891 Error_Msg_N
("aggregate type cannot be limited", N
);
1894 elsif Is_Class_Wide_Type
(Typ
) then
1895 Error_Msg_N
("aggregate cannot be of a class-wide type", N
);
1899 if Is_Entity_Name
(A
)
1900 and then Is_Type
(Entity
(A
))
1902 A_Type
:= Get_Full_View
(Entity
(A
));
1903 Imm_Type
:= Base_Type
(Typ
);
1905 if Valid_Ancestor_Type
then
1906 Set_Entity
(A
, A_Type
);
1907 Set_Etype
(A
, A_Type
);
1909 Validate_Ancestor_Part
(N
);
1910 Resolve_Record_Aggregate
(N
, Typ
);
1913 elsif Nkind
(A
) /= N_Aggregate
then
1914 if Is_Overloaded
(A
) then
1916 Get_First_Interp
(A
, I
, It
);
1918 while Present
(It
.Typ
) loop
1920 if Is_Tagged_Type
(It
.Typ
)
1921 and then not Is_Limited_Type
(It
.Typ
)
1923 if A_Type
/= Any_Type
then
1924 Error_Msg_N
("cannot resolve expression", A
);
1931 Get_Next_Interp
(I
, It
);
1934 if A_Type
= Any_Type
then
1936 ("ancestor part must be non-limited tagged type", A
);
1941 A_Type
:= Etype
(A
);
1944 if Valid_Ancestor_Type
then
1945 Resolve
(A
, A_Type
);
1946 Check_Non_Static_Context
(A
);
1947 Resolve_Record_Aggregate
(N
, Typ
);
1951 Error_Msg_N
(" No unique type for this aggregate", A
);
1954 end Resolve_Extension_Aggregate
;
1956 ------------------------------
1957 -- Resolve_Record_Aggregate --
1958 ------------------------------
1960 procedure Resolve_Record_Aggregate
(N
: Node_Id
; Typ
: Entity_Id
) is
1961 Regular_Aggr
: constant Boolean := Nkind
(N
) /= N_Extension_Aggregate
;
1963 New_Assoc_List
: List_Id
:= New_List
;
1964 New_Assoc
: Node_Id
;
1965 -- New_Assoc_List is the newly built list of N_Component_Association
1966 -- nodes. New_Assoc is one such N_Component_Association node in it.
1967 -- Please note that while Assoc and New_Assoc contain the same
1968 -- kind of nodes, they are used to iterate over two different
1969 -- N_Component_Association lists.
1971 Others_Etype
: Entity_Id
:= Empty
;
1972 -- This variable is used to save the Etype of the last record component
1973 -- that takes its value from the others choice. Its purpose is:
1975 -- (a) make sure the others choice is useful
1977 -- (b) make sure the type of all the components whose value is
1978 -- subsumed by the others choice are the same.
1980 -- This variable is updated as a side effect of function Get_Value
1982 procedure Add_Association
(Component
: Entity_Id
; Expr
: Node_Id
);
1983 -- Builds a new N_Component_Association node which associates
1984 -- Component to expression Expr and adds it to the new association
1985 -- list New_Assoc_List being built.
1987 function Discr_Present
(Discr
: Entity_Id
) return Boolean;
1988 -- If aggregate N is a regular aggregate this routine will return True.
1989 -- Otherwise, if N is an extension aggreagte, Discr is a discriminant
1990 -- whose value may already have been specified by N's ancestor part,
1991 -- this routine checks whether this is indeed the case and if so
1992 -- returns False, signaling that no value for Discr should appear in the
1993 -- N's aggregate part. Also, in this case, the routine appends to
1994 -- New_Assoc_List Discr the discriminant value specified in the ancestor
2000 Consider_Others_Choice
: Boolean := False)
2002 -- Given a record component stored in parameter Compon, the
2003 -- following function returns its value as it appears in the list
2004 -- From, which is a list of N_Component_Association nodes. If no
2005 -- component association has a choice for the searched component,
2006 -- the value provided by the others choice is returned, if there
2007 -- is one and Consider_Others_Choice is set to true. Otherwise
2008 -- Empty is returned. If there is more than one component association
2009 -- giving a value for the searched record component, an error message
2010 -- is emitted and the first found value is returned.
2012 -- If Consider_Others_Choice is set and the returned expression comes
2013 -- from the others choice, then Others_Etype is set as a side effect.
2014 -- An error message is emitted if the components taking their value
2015 -- from the others choice do not have same type.
2017 procedure Resolve_Aggr_Expr
(Expr
: Node_Id
; Component
: Node_Id
);
2018 -- Analyzes and resolves expression Expr against the Etype of the
2019 -- Component. This routine also applies all appropriate checks to Expr.
2020 -- It finally saves a Expr in the newly created association list that
2021 -- will be attached to the final record aggregate. Note that if the
2022 -- Parent pointer of Expr is not set then Expr was produced with a
2023 -- New_copy_Tree or some such.
2025 ---------------------
2026 -- Add_Association --
2027 ---------------------
2029 procedure Add_Association
(Component
: Entity_Id
; Expr
: Node_Id
) is
2030 New_Assoc
: Node_Id
;
2031 Choice_List
: List_Id
:= New_List
;
2034 Append
(New_Occurrence_Of
(Component
, Sloc
(Expr
)), Choice_List
);
2036 Make_Component_Association
(Sloc
(Expr
),
2037 Choices
=> Choice_List
,
2038 Expression
=> Expr
);
2039 Append
(New_Assoc
, New_Assoc_List
);
2040 end Add_Association
;
2046 function Discr_Present
(Discr
: Entity_Id
) return Boolean is
2050 Discr_Expr
: Node_Id
;
2052 Ancestor_Typ
: Entity_Id
;
2053 Orig_Discr
: Entity_Id
;
2055 D_Val
: Elmt_Id
:= No_Elmt
; -- stop junk warning
2057 Ancestor_Is_Subtyp
: Boolean;
2060 if Regular_Aggr
then
2064 Ancestor
:= Ancestor_Part
(N
);
2065 Ancestor_Typ
:= Etype
(Ancestor
);
2066 Loc
:= Sloc
(Ancestor
);
2068 Ancestor_Is_Subtyp
:=
2069 Is_Entity_Name
(Ancestor
) and then Is_Type
(Entity
(Ancestor
));
2071 -- If the ancestor part has no discriminants clearly N's aggregate
2072 -- part must provide a value for Discr.
2074 if not Has_Discriminants
(Ancestor_Typ
) then
2077 -- If the ancestor part is an unconstrained subtype mark then the
2078 -- Discr must be present in N's aggregate part.
2080 elsif Ancestor_Is_Subtyp
2081 and then not Is_Constrained
(Entity
(Ancestor
))
2086 -- Now look to see if Discr was specified in the ancestor part.
2088 Orig_Discr
:= Original_Record_Component
(Discr
);
2089 D
:= First_Discriminant
(Ancestor_Typ
);
2091 if Ancestor_Is_Subtyp
then
2092 D_Val
:= First_Elmt
(Discriminant_Constraint
(Entity
(Ancestor
)));
2095 while Present
(D
) loop
2096 -- If Ancestor has already specified Disc value than
2097 -- insert its value in the final aggregate.
2099 if Original_Record_Component
(D
) = Orig_Discr
then
2100 if Ancestor_Is_Subtyp
then
2101 Discr_Expr
:= New_Copy_Tree
(Node
(D_Val
));
2104 Make_Selected_Component
(Loc
,
2105 Prefix
=> Duplicate_Subexpr
(Ancestor
),
2106 Selector_Name
=> New_Occurrence_Of
(Discr
, Loc
));
2109 Resolve_Aggr_Expr
(Discr_Expr
, Discr
);
2113 Next_Discriminant
(D
);
2115 if Ancestor_Is_Subtyp
then
2130 Consider_Others_Choice
: Boolean := False)
2134 Expr
: Node_Id
:= Empty
;
2135 Selector_Name
: Node_Id
;
2138 if Present
(From
) then
2139 Assoc
:= First
(From
);
2144 while Present
(Assoc
) loop
2145 Selector_Name
:= First
(Choices
(Assoc
));
2146 while Present
(Selector_Name
) loop
2147 if Nkind
(Selector_Name
) = N_Others_Choice
then
2148 if Consider_Others_Choice
and then No
(Expr
) then
2149 if Present
(Others_Etype
) and then
2150 Base_Type
(Others_Etype
) /= Base_Type
(Etype
(Compon
))
2152 Error_Msg_N
("components in OTHERS choice must " &
2153 "have same type", Selector_Name
);
2156 Others_Etype
:= Etype
(Compon
);
2158 -- We need to duplicate the expression for each
2159 -- successive component covered by the others choice.
2160 -- If the expression is itself an array aggregate with
2161 -- "others", its subtype must be obtained from the
2162 -- current component, and therefore it must be (at least
2163 -- partly) reanalyzed.
2165 if Analyzed
(Expression
(Assoc
)) then
2166 Expr
:= New_Copy_Tree
(Expression
(Assoc
));
2168 if Nkind
(Expr
) = N_Aggregate
2169 and then Is_Array_Type
(Etype
(Expr
))
2170 and then No
(Expressions
(Expr
))
2172 Nkind
(First
(Choices
2173 (First
(Component_Associations
(Expr
)))))
2176 Set_Analyzed
(Expr
, False);
2182 return Expression
(Assoc
);
2186 elsif Chars
(Compon
) = Chars
(Selector_Name
) then
2188 -- We need to duplicate the expression when several
2189 -- components are grouped together with a "|" choice.
2190 -- For instance "filed1 | filed2 => Expr"
2192 if Present
(Next
(Selector_Name
)) then
2193 Expr
:= New_Copy_Tree
(Expression
(Assoc
));
2195 Expr
:= Expression
(Assoc
);
2200 ("more than one value supplied for &",
2201 Selector_Name
, Compon
);
2206 Next
(Selector_Name
);
2215 -----------------------
2216 -- Resolve_Aggr_Expr --
2217 -----------------------
2219 procedure Resolve_Aggr_Expr
(Expr
: Node_Id
; Component
: Node_Id
) is
2220 New_C
: Entity_Id
:= Component
;
2221 Expr_Type
: Entity_Id
:= Empty
;
2223 function Has_Expansion_Delayed
(Expr
: Node_Id
) return Boolean;
2224 -- If the expression is an aggregate (possibly qualified) then its
2225 -- expansion is delayed until the enclosing aggregate is expanded
2226 -- into assignments. In that case, do not generate checks on the
2227 -- expression, because they will be generated later, and will other-
2228 -- wise force a copy (to remove side-effects) that would leave a
2229 -- dynamic-sized aggregate in the code, something that gigi cannot
2233 -- Set to True if the resolved Expr node needs to be relocated
2234 -- when attached to the newly created association list. This node
2235 -- need not be relocated if its parent pointer is not set.
2236 -- In fact in this case Expr is the output of a New_Copy_Tree call.
2237 -- if Relocate is True then we have analyzed the expression node
2238 -- in the original aggregate and hence it needs to be relocated
2239 -- when moved over the new association list.
2241 function Has_Expansion_Delayed
(Expr
: Node_Id
) return Boolean is
2242 Kind
: constant Node_Kind
:= Nkind
(Expr
);
2245 return ((Kind
= N_Aggregate
2246 or else Kind
= N_Extension_Aggregate
)
2247 and then Present
(Etype
(Expr
))
2248 and then Is_Record_Type
(Etype
(Expr
))
2249 and then Expansion_Delayed
(Expr
))
2251 or else (Kind
= N_Qualified_Expression
2252 and then Has_Expansion_Delayed
(Expression
(Expr
)));
2253 end Has_Expansion_Delayed
;
2255 -- Start of processing for Resolve_Aggr_Expr
2258 -- If the type of the component is elementary or the type of the
2259 -- aggregate does not contain discriminants, use the type of the
2260 -- component to resolve Expr.
2262 if Is_Elementary_Type
(Etype
(Component
))
2263 or else not Has_Discriminants
(Etype
(N
))
2265 Expr_Type
:= Etype
(Component
);
2267 -- Otherwise we have to pick up the new type of the component from
2268 -- the new costrained subtype of the aggregate. In fact components
2269 -- which are of a composite type might be constrained by a
2270 -- discriminant, and we want to resolve Expr against the subtype were
2271 -- all discriminant occurrences are replaced with their actual value.
2274 New_C
:= First_Component
(Etype
(N
));
2275 while Present
(New_C
) loop
2276 if Chars
(New_C
) = Chars
(Component
) then
2277 Expr_Type
:= Etype
(New_C
);
2281 Next_Component
(New_C
);
2284 pragma Assert
(Present
(Expr_Type
));
2286 -- For each range in an array type where a discriminant has been
2287 -- replaced with the constraint, check that this range is within
2288 -- the range of the base type. This checks is done in the
2289 -- _init_proc for regular objects, but has to be done here for
2290 -- aggregates since no _init_proc is called for them.
2292 if Is_Array_Type
(Expr_Type
) then
2294 Index
: Node_Id
:= First_Index
(Expr_Type
);
2295 -- Range of the current constrained index in the array.
2297 Orig_Index
: Node_Id
:= First_Index
(Etype
(Component
));
2298 -- Range corresponding to the range Index above in the
2299 -- original unconstrained record type. The bounds of this
2300 -- range may be governed by discriminants.
2302 Unconstr_Index
: Node_Id
:= First_Index
(Etype
(Expr_Type
));
2303 -- Range corresponding to the range Index above for the
2304 -- unconstrained array type. This range is needed to apply
2308 while Present
(Index
) loop
2309 if Depends_On_Discriminant
(Orig_Index
) then
2310 Apply_Range_Check
(Index
, Etype
(Unconstr_Index
));
2314 Next_Index
(Orig_Index
);
2315 Next_Index
(Unconstr_Index
);
2321 -- If the Parent pointer of Expr is not set, Expr is an expression
2322 -- duplicated by New_Tree_Copy (this happens for record aggregates
2323 -- that look like (Field1 | Filed2 => Expr) or (others => Expr)).
2324 -- Such a duplicated expression must be attached to the tree
2325 -- before analysis and resolution to enforce the rule that a tree
2326 -- fragment should never be analyzed or resolved unless it is
2327 -- attached to the current compilation unit.
2329 if No
(Parent
(Expr
)) then
2330 Set_Parent
(Expr
, N
);
2336 Analyze_And_Resolve
(Expr
, Expr_Type
);
2337 Check_Non_Static_Context
(Expr
);
2339 if not Has_Expansion_Delayed
(Expr
) then
2340 Aggregate_Constraint_Checks
(Expr
, Expr_Type
);
2343 if Raises_Constraint_Error
(Expr
) then
2344 Set_Raises_Constraint_Error
(N
);
2348 Add_Association
(New_C
, Relocate_Node
(Expr
));
2350 Add_Association
(New_C
, Expr
);
2353 end Resolve_Aggr_Expr
;
2355 -- Resolve_Record_Aggregate local variables
2358 -- N_Component_Association node belonging to the input aggregate N
2361 Positional_Expr
: Node_Id
;
2363 Component
: Entity_Id
;
2364 Component_Elmt
: Elmt_Id
;
2365 Components
: Elist_Id
:= New_Elmt_List
;
2366 -- Components is the list of the record components whose value must
2367 -- be provided in the aggregate. This list does include discriminants.
2369 -- Start of processing for Resolve_Record_Aggregate
2372 -- We may end up calling Duplicate_Subexpr on expressions that are
2373 -- attached to New_Assoc_List. For this reason we need to attach it
2374 -- to the tree by setting its parent pointer to N. This parent point
2375 -- will change in STEP 8 below.
2377 Set_Parent
(New_Assoc_List
, N
);
2379 -- STEP 1: abstract type and null record verification
2381 if Is_Abstract
(Typ
) then
2382 Error_Msg_N
("type of aggregate cannot be abstract", N
);
2385 if No
(First_Entity
(Typ
)) and then Null_Record_Present
(N
) then
2389 elsif Present
(First_Entity
(Typ
))
2390 and then Null_Record_Present
(N
)
2391 and then not Is_Tagged_Type
(Typ
)
2393 Error_Msg_N
("record aggregate cannot be null", N
);
2396 elsif No
(First_Entity
(Typ
)) then
2397 Error_Msg_N
("record aggregate must be null", N
);
2401 -- STEP 2: Verify aggregate structure
2404 Selector_Name
: Node_Id
;
2405 Bad_Aggregate
: Boolean := False;
2408 if Present
(Component_Associations
(N
)) then
2409 Assoc
:= First
(Component_Associations
(N
));
2414 while Present
(Assoc
) loop
2415 Selector_Name
:= First
(Choices
(Assoc
));
2416 while Present
(Selector_Name
) loop
2417 if Nkind
(Selector_Name
) = N_Identifier
then
2420 elsif Nkind
(Selector_Name
) = N_Others_Choice
then
2421 if Selector_Name
/= First
(Choices
(Assoc
))
2422 or else Present
(Next
(Selector_Name
))
2424 Error_Msg_N
("OTHERS must appear alone in a choice list",
2428 elsif Present
(Next
(Assoc
)) then
2429 Error_Msg_N
("OTHERS must appear last in an aggregate",
2436 ("selector name should be identifier or OTHERS",
2438 Bad_Aggregate
:= True;
2441 Next
(Selector_Name
);
2447 if Bad_Aggregate
then
2452 -- STEP 3: Find discriminant Values
2455 Discrim
: Entity_Id
;
2456 Missing_Discriminants
: Boolean := False;
2459 if Present
(Expressions
(N
)) then
2460 Positional_Expr
:= First
(Expressions
(N
));
2462 Positional_Expr
:= Empty
;
2465 if Has_Discriminants
(Typ
) then
2466 Discrim
:= First_Discriminant
(Typ
);
2471 -- First find the discriminant values in the positional components
2473 while Present
(Discrim
) and then Present
(Positional_Expr
) loop
2474 if Discr_Present
(Discrim
) then
2475 Resolve_Aggr_Expr
(Positional_Expr
, Discrim
);
2476 Next
(Positional_Expr
);
2479 if Present
(Get_Value
(Discrim
, Component_Associations
(N
))) then
2481 ("more than one value supplied for discriminant&",
2485 Next_Discriminant
(Discrim
);
2488 -- Find remaining discriminant values, if any, among named components
2490 while Present
(Discrim
) loop
2491 Expr
:= Get_Value
(Discrim
, Component_Associations
(N
), True);
2493 if not Discr_Present
(Discrim
) then
2494 if Present
(Expr
) then
2496 ("more than one value supplied for discriminant&",
2500 elsif No
(Expr
) then
2502 ("no value supplied for discriminant &", N
, Discrim
);
2503 Missing_Discriminants
:= True;
2506 Resolve_Aggr_Expr
(Expr
, Discrim
);
2509 Next_Discriminant
(Discrim
);
2512 if Missing_Discriminants
then
2516 -- At this point and until the beginning of STEP 6, New_Assoc_List
2517 -- contains only the discriminants and their values.
2521 -- STEP 4: Set the Etype of the record aggregate
2523 -- ??? This code is pretty much a copy of Sem_Ch3.Build_Subtype. That
2524 -- routine should really be exported in sem_util or some such and used
2525 -- in sem_ch3 and here rather than have a copy of the code which is a
2526 -- maintenance nightmare.
2528 -- ??? Performace WARNING. The current implementation creates a new
2529 -- itype for all aggregates whose base type is discriminated.
2530 -- This means that for record aggregates nested inside an array
2531 -- aggregate we will create a new itype for each record aggregate
2532 -- if the array cmponent type has discriminants. For large aggregates
2533 -- this may be a problem. What should be done in this case is
2534 -- to reuse itypes as much as possible.
2536 if Has_Discriminants
(Typ
) then
2537 Build_Constrained_Itype
: declare
2538 Loc
: constant Source_Ptr
:= Sloc
(N
);
2540 Subtyp_Decl
: Node_Id
;
2543 C
: List_Id
:= New_List
;
2546 New_Assoc
:= First
(New_Assoc_List
);
2547 while Present
(New_Assoc
) loop
2548 Append
(Duplicate_Subexpr
(Expression
(New_Assoc
)), To
=> C
);
2553 Make_Subtype_Indication
(Loc
,
2554 Subtype_Mark
=> New_Occurrence_Of
(Base_Type
(Typ
), Loc
),
2555 Constraint
=> Make_Index_Or_Discriminant_Constraint
(Loc
, C
));
2557 Def_Id
:= Create_Itype
(Ekind
(Typ
), N
);
2560 Make_Subtype_Declaration
(Loc
,
2561 Defining_Identifier
=> Def_Id
,
2562 Subtype_Indication
=> Indic
);
2563 Set_Parent
(Subtyp_Decl
, Parent
(N
));
2565 -- Itypes must be analyzed with checks off (see itypes.ads).
2567 Analyze
(Subtyp_Decl
, Suppress
=> All_Checks
);
2569 Set_Etype
(N
, Def_Id
);
2570 Check_Static_Discriminated_Subtype
2571 (Def_Id
, Expression
(First
(New_Assoc_List
)));
2572 end Build_Constrained_Itype
;
2578 -- STEP 5: Get remaining components according to discriminant values
2581 Record_Def
: Node_Id
;
2582 Parent_Typ
: Entity_Id
;
2583 Root_Typ
: Entity_Id
;
2584 Parent_Typ_List
: Elist_Id
;
2585 Parent_Elmt
: Elmt_Id
;
2586 Errors_Found
: Boolean := False;
2590 if Is_Derived_Type
(Typ
) and then Is_Tagged_Type
(Typ
) then
2591 Parent_Typ_List
:= New_Elmt_List
;
2593 -- If this is an extension aggregate, the component list must
2594 -- include all components that are not in the given ancestor
2595 -- type. Otherwise, the component list must include components
2596 -- of all ancestors, starting with the root.
2598 if Nkind
(N
) = N_Extension_Aggregate
then
2599 Root_Typ
:= Base_Type
(Etype
(Ancestor_Part
(N
)));
2601 Root_Typ
:= Root_Type
(Typ
);
2603 if Nkind
(Parent
(Base_Type
(Root_Typ
)))
2604 = N_Private_Type_Declaration
2607 ("type of aggregate has private ancestor&!",
2609 Error_Msg_N
("must use extension aggregate!", N
);
2613 Dnode
:= Declaration_Node
(Base_Type
(Root_Typ
));
2615 -- If we don't get a full declaration, then we have some
2616 -- error which will get signalled later so skip this part.
2617 -- Otherwise, gather components of root that apply to the
2618 -- aggregate type. We use the base type in case there is an
2619 -- applicable girder constraint that renames the discriminants
2622 if Nkind
(Dnode
) = N_Full_Type_Declaration
then
2623 Record_Def
:= Type_Definition
(Dnode
);
2624 Gather_Components
(Base_Type
(Typ
),
2625 Component_List
(Record_Def
),
2626 Governed_By
=> New_Assoc_List
,
2628 Report_Errors
=> Errors_Found
);
2632 Parent_Typ
:= Base_Type
(Typ
);
2633 while Parent_Typ
/= Root_Typ
loop
2635 Prepend_Elmt
(Parent_Typ
, To
=> Parent_Typ_List
);
2636 Parent_Typ
:= Etype
(Parent_Typ
);
2638 if (Nkind
(Parent
(Base_Type
(Parent_Typ
))) =
2639 N_Private_Type_Declaration
2640 or else Nkind
(Parent
(Base_Type
(Parent_Typ
))) =
2641 N_Private_Extension_Declaration
)
2643 if Nkind
(N
) /= N_Extension_Aggregate
then
2645 ("type of aggregate has private ancestor&!",
2647 Error_Msg_N
("must use extension aggregate!", N
);
2650 elsif Parent_Typ
/= Root_Typ
then
2652 ("ancestor part of aggregate must be private type&",
2653 Ancestor_Part
(N
), Parent_Typ
);
2659 -- Now collect components from all other ancestors.
2661 Parent_Elmt
:= First_Elmt
(Parent_Typ_List
);
2662 while Present
(Parent_Elmt
) loop
2663 Parent_Typ
:= Node
(Parent_Elmt
);
2664 Record_Def
:= Type_Definition
(Parent
(Base_Type
(Parent_Typ
)));
2665 Gather_Components
(Empty
,
2666 Component_List
(Record_Extension_Part
(Record_Def
)),
2667 Governed_By
=> New_Assoc_List
,
2669 Report_Errors
=> Errors_Found
);
2671 Next_Elmt
(Parent_Elmt
);
2675 Record_Def
:= Type_Definition
(Parent
(Base_Type
(Typ
)));
2677 if Null_Present
(Record_Def
) then
2680 Gather_Components
(Base_Type
(Typ
),
2681 Component_List
(Record_Def
),
2682 Governed_By
=> New_Assoc_List
,
2684 Report_Errors
=> Errors_Found
);
2688 if Errors_Found
then
2693 -- STEP 6: Find component Values
2696 Component_Elmt
:= First_Elmt
(Components
);
2698 -- First scan the remaining positional associations in the aggregate.
2699 -- Remember that at this point Positional_Expr contains the current
2700 -- positional association if any is left after looking for discriminant
2701 -- values in step 3.
2703 while Present
(Positional_Expr
) and then Present
(Component_Elmt
) loop
2704 Component
:= Node
(Component_Elmt
);
2705 Resolve_Aggr_Expr
(Positional_Expr
, Component
);
2707 if Present
(Get_Value
(Component
, Component_Associations
(N
))) then
2709 ("more than one value supplied for Component &", N
, Component
);
2712 Next
(Positional_Expr
);
2713 Next_Elmt
(Component_Elmt
);
2716 if Present
(Positional_Expr
) then
2718 ("too many components for record aggregate", Positional_Expr
);
2721 -- Now scan for the named arguments of the aggregate
2723 while Present
(Component_Elmt
) loop
2724 Component
:= Node
(Component_Elmt
);
2725 Expr
:= Get_Value
(Component
, Component_Associations
(N
), True);
2728 Error_Msg_NE
("no value supplied for component &!", N
, Component
);
2730 Resolve_Aggr_Expr
(Expr
, Component
);
2733 Next_Elmt
(Component_Elmt
);
2736 -- STEP 7: check for invalid components + check type in choice list
2743 -- Type of first component in choice list
2746 if Present
(Component_Associations
(N
)) then
2747 Assoc
:= First
(Component_Associations
(N
));
2752 Verification
: while Present
(Assoc
) loop
2753 Selectr
:= First
(Choices
(Assoc
));
2756 if Nkind
(Selectr
) = N_Others_Choice
then
2757 if No
(Others_Etype
) then
2759 ("OTHERS must represent at least one component", Selectr
);
2765 while Present
(Selectr
) loop
2766 New_Assoc
:= First
(New_Assoc_List
);
2767 while Present
(New_Assoc
) loop
2768 Component
:= First
(Choices
(New_Assoc
));
2769 exit when Chars
(Selectr
) = Chars
(Component
);
2773 -- If no association, this is not a legal component of
2774 -- of the type in question, except if this is an internal
2775 -- component supplied by a previous expansion.
2777 if No
(New_Assoc
) then
2779 if Chars
(Selectr
) /= Name_uTag
2780 and then Chars
(Selectr
) /= Name_uParent
2781 and then Chars
(Selectr
) /= Name_uController
2783 if not Has_Discriminants
(Typ
) then
2784 Error_Msg_Node_2
:= Typ
;
2786 ("& is not a component of}",
2790 ("& is not a component of the aggregate subtype",
2794 Check_Misspelled_Component
(Components
, Selectr
);
2797 elsif No
(Typech
) then
2798 Typech
:= Base_Type
(Etype
(Component
));
2800 elsif Typech
/= Base_Type
(Etype
(Component
)) then
2802 ("components in choice list must have same type", Selectr
);
2809 end loop Verification
;
2812 -- STEP 8: replace the original aggregate
2815 New_Aggregate
: Node_Id
:= New_Copy
(N
);
2818 Set_Expressions
(New_Aggregate
, No_List
);
2819 Set_Etype
(New_Aggregate
, Etype
(N
));
2820 Set_Component_Associations
(New_Aggregate
, New_Assoc_List
);
2822 Rewrite
(N
, New_Aggregate
);
2824 end Resolve_Record_Aggregate
;
2826 ---------------------
2827 -- Sort_Case_Table --
2828 ---------------------
2830 procedure Sort_Case_Table
(Case_Table
: in out Case_Table_Type
) is
2831 L
: Int
:= Case_Table
'First;
2832 U
: Int
:= Case_Table
'Last;
2841 T
:= Case_Table
(K
+ 1);
2845 and then Expr_Value
(Case_Table
(J
- 1).Choice_Lo
) >
2846 Expr_Value
(T
.Choice_Lo
)
2848 Case_Table
(J
) := Case_Table
(J
- 1);
2852 Case_Table
(J
) := T
;
2855 end Sort_Case_Table
;