1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2020, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Einfo; use Einfo;
30 with Elists; use Elists;
31 with Errout; use Errout;
32 with Expander; use Expander;
33 with Exp_Ch6; use Exp_Ch6;
34 with Exp_Tss; use Exp_Tss;
35 with Exp_Util; use Exp_Util;
36 with Freeze; use Freeze;
37 with Itypes; use Itypes;
39 with Lib.Xref; use Lib.Xref;
40 with Namet; use Namet;
41 with Namet.Sp; use Namet.Sp;
42 with Nmake; use Nmake;
43 with Nlists; use Nlists;
45 with Restrict; use Restrict;
46 with Rident; use Rident;
48 with Sem_Aux; use Sem_Aux;
49 with Sem_Cat; use Sem_Cat;
50 with Sem_Ch3; use Sem_Ch3;
51 with Sem_Ch8; use Sem_Ch8;
52 with Sem_Ch13; use Sem_Ch13;
53 with Sem_Dim; use Sem_Dim;
54 with Sem_Eval; use Sem_Eval;
55 with Sem_Res; use Sem_Res;
56 with Sem_Util; use Sem_Util;
57 with Sem_Type; use Sem_Type;
58 with Sem_Warn; use Sem_Warn;
59 with Sinfo; use Sinfo;
60 with Snames; use Snames;
61 with Stringt; use Stringt;
62 with Stand; use Stand;
63 with Style; use Style;
64 with Targparm; use Targparm;
65 with Tbuild; use Tbuild;
66 with Uintp; use Uintp;
68 package body Sem_Aggr is
70 type Case_Bounds is record
72 -- Low bound of choice. Once we sort the Case_Table, then entries
73 -- will be in order of ascending Choice_Lo values.
76 -- High Bound of choice. The sort does not pay any attention to the
77 -- high bound, so choices 1 .. 4 and 1 .. 5 could be in either order.
80 -- If there are duplicates or missing entries, then in the sorted
81 -- table, this records the highest value among Choice_Hi values
82 -- seen so far, including this entry.
85 -- The node of the choice
88 type Case_Table_Type is array (Nat range <>) of Case_Bounds;
89 -- Table type used by Check_Case_Choices procedure. Entry zero is not
90 -- used (reserved for the sort). Real entries start at one.
92 -----------------------
93 -- Local Subprograms --
94 -----------------------
96 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type);
97 -- Sort the Case Table using the Lower Bound of each Choice as the key. A
98 -- simple insertion sort is used since the choices in a case statement will
99 -- usually be in near sorted order.
101 procedure Check_Can_Never_Be_Null (Typ : Entity_Id; Expr : Node_Id);
102 -- Ada 2005 (AI-231): Check bad usage of null for a component for which
103 -- null exclusion (NOT NULL) is specified. Typ can be an E_Array_Type for
104 -- the array case (the component type of the array will be used) or an
105 -- E_Component/E_Discriminant entity in the record case, in which case the
106 -- type of the component will be used for the test. If Typ is any other
107 -- kind of entity, the call is ignored. Expr is the component node in the
108 -- aggregate which is known to have a null value. A warning message will be
109 -- issued if the component is null excluding.
111 -- It would be better to pass the proper type for Typ ???
113 procedure Check_Expr_OK_In_Limited_Aggregate (Expr : Node_Id);
114 -- Check that Expr is either not limited or else is one of the cases of
115 -- expressions allowed for a limited component association (namely, an
116 -- aggregate, function call, or <> notation). Report error for violations.
117 -- Expression is also OK in an instance or inlining context, because we
118 -- have already preanalyzed and it is known to be type correct.
120 procedure Check_Qualified_Aggregate (Level : Nat; Expr : Node_Id);
121 -- Given aggregate Expr, check that sub-aggregates of Expr that are nested
122 -- at Level are qualified. If Level = 0, this applies to Expr directly.
123 -- Only issue errors in formal verification mode.
125 function Is_Top_Level_Aggregate (Expr : Node_Id) return Boolean;
126 -- Return True of Expr is an aggregate not contained directly in another
129 ------------------------------------------------------
130 -- Subprograms used for RECORD AGGREGATE Processing --
131 ------------------------------------------------------
133 procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id);
134 -- This procedure performs all the semantic checks required for record
135 -- aggregates. Note that for aggregates analysis and resolution go
136 -- hand in hand. Aggregate analysis has been delayed up to here and
137 -- it is done while resolving the aggregate.
139 -- N is the N_Aggregate node.
140 -- Typ is the record type for the aggregate resolution
142 -- While performing the semantic checks, this procedure builds a new
143 -- Component_Association_List where each record field appears alone in a
144 -- Component_Choice_List along with its corresponding expression. The
145 -- record fields in the Component_Association_List appear in the same order
146 -- in which they appear in the record type Typ.
148 -- Once this new Component_Association_List is built and all the semantic
149 -- checks performed, the original aggregate subtree is replaced with the
150 -- new named record aggregate just built. Note that subtree substitution is
151 -- performed with Rewrite so as to be able to retrieve the original
154 -- The aggregate subtree manipulation performed by Resolve_Record_Aggregate
155 -- yields the aggregate format expected by Gigi. Typically, this kind of
156 -- tree manipulations are done in the expander. However, because the
157 -- semantic checks that need to be performed on record aggregates really go
158 -- hand in hand with the record aggregate normalization, the aggregate
159 -- subtree transformation is performed during resolution rather than
160 -- expansion. Had we decided otherwise we would have had to duplicate most
161 -- of the code in the expansion procedure Expand_Record_Aggregate. Note,
162 -- however, that all the expansion concerning aggregates for tagged records
163 -- is done in Expand_Record_Aggregate.
165 -- The algorithm of Resolve_Record_Aggregate proceeds as follows:
167 -- 1. Make sure that the record type against which the record aggregate
168 -- has to be resolved is not abstract. Furthermore if the type is a
169 -- null aggregate make sure the input aggregate N is also null.
171 -- 2. Verify that the structure of the aggregate is that of a record
172 -- aggregate. Specifically, look for component associations and ensure
173 -- that each choice list only has identifiers or the N_Others_Choice
174 -- node. Also make sure that if present, the N_Others_Choice occurs
175 -- last and by itself.
177 -- 3. If Typ contains discriminants, the values for each discriminant is
178 -- looked for. If the record type Typ has variants, we check that the
179 -- expressions corresponding to each discriminant ruling the (possibly
180 -- nested) variant parts of Typ, are static. This allows us to determine
181 -- the variant parts to which the rest of the aggregate must conform.
182 -- The names of discriminants with their values are saved in a new
183 -- association list, New_Assoc_List which is later augmented with the
184 -- names and values of the remaining components in the record type.
186 -- During this phase we also make sure that every discriminant is
187 -- assigned exactly one value. Note that when several values for a given
188 -- discriminant are found, semantic processing continues looking for
189 -- further errors. In this case it's the first discriminant value found
190 -- which we will be recorded.
192 -- IMPORTANT NOTE: For derived tagged types this procedure expects
193 -- First_Discriminant and Next_Discriminant to give the correct list
194 -- of discriminants, in the correct order.
196 -- 4. After all the discriminant values have been gathered, we can set the
197 -- Etype of the record aggregate. If Typ contains no discriminants this
198 -- is straightforward: the Etype of N is just Typ, otherwise a new
199 -- implicit constrained subtype of Typ is built to be the Etype of N.
201 -- 5. Gather the remaining record components according to the discriminant
202 -- values. This involves recursively traversing the record type
203 -- structure to see what variants are selected by the given discriminant
204 -- values. This processing is a little more convoluted if Typ is a
205 -- derived tagged types since we need to retrieve the record structure
206 -- of all the ancestors of Typ.
208 -- 6. After gathering the record components we look for their values in the
209 -- record aggregate and emit appropriate error messages should we not
210 -- find such values or should they be duplicated.
212 -- 7. We then make sure no illegal component names appear in the record
213 -- aggregate and make sure that the type of the record components
214 -- appearing in a same choice list is the same. Finally we ensure that
215 -- the others choice, if present, is used to provide the value of at
216 -- least a record component.
218 -- 8. The original aggregate node is replaced with the new named aggregate
219 -- built in steps 3 through 6, as explained earlier.
221 -- Given the complexity of record aggregate resolution, the primary goal of
222 -- this routine is clarity and simplicity rather than execution and storage
223 -- efficiency. If there are only positional components in the aggregate the
224 -- running time is linear. If there are associations the running time is
225 -- still linear as long as the order of the associations is not too far off
226 -- the order of the components in the record type. If this is not the case
227 -- the running time is at worst quadratic in the size of the association
230 procedure Check_Misspelled_Component
231 (Elements : Elist_Id;
232 Component : Node_Id);
233 -- Give possible misspelling diagnostic if Component is likely to be a
234 -- misspelling of one of the components of the Assoc_List. This is called
235 -- by Resolve_Aggr_Expr after producing an invalid component error message.
237 procedure Check_Static_Discriminated_Subtype (T : Entity_Id; V : Node_Id);
238 -- An optimization: determine whether a discriminated subtype has a static
239 -- constraint, and contains array components whose length is also static,
240 -- either because they are constrained by the discriminant, or because the
241 -- original component bounds are static.
243 -----------------------------------------------------
244 -- Subprograms used for ARRAY AGGREGATE Processing --
245 -----------------------------------------------------
247 function Resolve_Array_Aggregate
250 Index_Constr : Node_Id;
251 Component_Typ : Entity_Id;
252 Others_Allowed : Boolean) return Boolean;
253 -- This procedure performs the semantic checks for an array aggregate.
254 -- True is returned if the aggregate resolution succeeds.
256 -- The procedure works by recursively checking each nested aggregate.
257 -- Specifically, after checking a sub-aggregate nested at the i-th level
258 -- we recursively check all the subaggregates at the i+1-st level (if any).
259 -- Note that for aggregates analysis and resolution go hand in hand.
260 -- Aggregate analysis has been delayed up to here and it is done while
261 -- resolving the aggregate.
263 -- N is the current N_Aggregate node to be checked.
265 -- Index is the index node corresponding to the array sub-aggregate that
266 -- we are currently checking (RM 4.3.3 (8)). Its Etype is the
267 -- corresponding index type (or subtype).
269 -- Index_Constr is the node giving the applicable index constraint if
270 -- any (RM 4.3.3 (10)). It "is a constraint provided by certain
271 -- contexts [...] that can be used to determine the bounds of the array
272 -- value specified by the aggregate". If Others_Allowed below is False
273 -- there is no applicable index constraint and this node is set to Index.
275 -- Component_Typ is the array component type.
277 -- Others_Allowed indicates whether an others choice is allowed
278 -- in the context where the top-level aggregate appeared.
280 -- The algorithm of Resolve_Array_Aggregate proceeds as follows:
282 -- 1. Make sure that the others choice, if present, is by itself and
283 -- appears last in the sub-aggregate. Check that we do not have
284 -- positional and named components in the array sub-aggregate (unless
285 -- the named association is an others choice). Finally if an others
286 -- choice is present, make sure it is allowed in the aggregate context.
288 -- 2. If the array sub-aggregate contains discrete_choices:
290 -- (A) Verify their validity. Specifically verify that:
292 -- (a) If a null range is present it must be the only possible
293 -- choice in the array aggregate.
295 -- (b) Ditto for a non static range.
297 -- (c) Ditto for a non static expression.
299 -- In addition this step analyzes and resolves each discrete_choice,
300 -- making sure that its type is the type of the corresponding Index.
301 -- If we are not at the lowest array aggregate level (in the case of
302 -- multi-dimensional aggregates) then invoke Resolve_Array_Aggregate
303 -- recursively on each component expression. Otherwise, resolve the
304 -- bottom level component expressions against the expected component
305 -- type ONLY IF the component corresponds to a single discrete choice
306 -- which is not an others choice (to see why read the DELAYED
307 -- COMPONENT RESOLUTION below).
309 -- (B) Determine the bounds of the sub-aggregate and lowest and
310 -- highest choice values.
312 -- 3. For positional aggregates:
314 -- (A) Loop over the component expressions either recursively invoking
315 -- Resolve_Array_Aggregate on each of these for multi-dimensional
316 -- array aggregates or resolving the bottom level component
317 -- expressions against the expected component type.
319 -- (B) Determine the bounds of the positional sub-aggregates.
321 -- 4. Try to determine statically whether the evaluation of the array
322 -- sub-aggregate raises Constraint_Error. If yes emit proper
323 -- warnings. The precise checks are the following:
325 -- (A) Check that the index range defined by aggregate bounds is
326 -- compatible with corresponding index subtype.
327 -- We also check against the base type. In fact it could be that
328 -- Low/High bounds of the base type are static whereas those of
329 -- the index subtype are not. Thus if we can statically catch
330 -- a problem with respect to the base type we are guaranteed
331 -- that the same problem will arise with the index subtype
333 -- (B) If we are dealing with a named aggregate containing an others
334 -- choice and at least one discrete choice then make sure the range
335 -- specified by the discrete choices does not overflow the
336 -- aggregate bounds. We also check against the index type and base
337 -- type bounds for the same reasons given in (A).
339 -- (C) If we are dealing with a positional aggregate with an others
340 -- choice make sure the number of positional elements specified
341 -- does not overflow the aggregate bounds. We also check against
342 -- the index type and base type bounds as mentioned in (A).
344 -- Finally construct an N_Range node giving the sub-aggregate bounds.
345 -- Set the Aggregate_Bounds field of the sub-aggregate to be this
346 -- N_Range. The routine Array_Aggr_Subtype below uses such N_Ranges
347 -- to build the appropriate aggregate subtype. Aggregate_Bounds
348 -- information is needed during expansion.
350 -- DELAYED COMPONENT RESOLUTION: The resolution of bottom level component
351 -- expressions in an array aggregate may call Duplicate_Subexpr or some
352 -- other routine that inserts code just outside the outermost aggregate.
353 -- If the array aggregate contains discrete choices or an others choice,
354 -- this may be wrong. Consider for instance the following example.
356 -- type Rec is record
360 -- type Acc_Rec is access Rec;
361 -- Arr : array (1..3) of Acc_Rec := (1 .. 3 => new Rec);
363 -- Then the transformation of "new Rec" that occurs during resolution
364 -- entails the following code modifications
366 -- P7b : constant Acc_Rec := new Rec;
368 -- Arr : array (1..3) of Acc_Rec := (1 .. 3 => P7b);
370 -- This code transformation is clearly wrong, since we need to call
371 -- "new Rec" for each of the 3 array elements. To avoid this problem we
372 -- delay resolution of the components of non positional array aggregates
373 -- to the expansion phase. As an optimization, if the discrete choice
374 -- specifies a single value we do not delay resolution.
376 function Array_Aggr_Subtype (N : Node_Id; Typ : Node_Id) return Entity_Id;
377 -- This routine returns the type or subtype of an array aggregate.
379 -- N is the array aggregate node whose type we return.
381 -- Typ is the context type in which N occurs.
383 -- This routine creates an implicit array subtype whose bounds are
384 -- those defined by the aggregate. When this routine is invoked
385 -- Resolve_Array_Aggregate has already processed aggregate N. Thus the
386 -- Aggregate_Bounds of each sub-aggregate, is an N_Range node giving the
387 -- sub-aggregate bounds. When building the aggregate itype, this function
388 -- traverses the array aggregate N collecting such Aggregate_Bounds and
389 -- constructs the proper array aggregate itype.
391 -- Note that in the case of multidimensional aggregates each inner
392 -- sub-aggregate corresponding to a given array dimension, may provide a
393 -- different bounds. If it is possible to determine statically that
394 -- some sub-aggregates corresponding to the same index do not have the
395 -- same bounds, then a warning is emitted. If such check is not possible
396 -- statically (because some sub-aggregate bounds are dynamic expressions)
397 -- then this job is left to the expander. In all cases the particular
398 -- bounds that this function will chose for a given dimension is the first
399 -- N_Range node for a sub-aggregate corresponding to that dimension.
401 -- Note that the Raises_Constraint_Error flag of an array aggregate
402 -- whose evaluation is determined to raise CE by Resolve_Array_Aggregate,
403 -- is set in Resolve_Array_Aggregate but the aggregate is not
404 -- immediately replaced with a raise CE. In fact, Array_Aggr_Subtype must
405 -- first construct the proper itype for the aggregate (Gigi needs
406 -- this). After constructing the proper itype we will eventually replace
407 -- the top-level aggregate with a raise CE (done in Resolve_Aggregate).
408 -- Of course in cases such as:
410 -- type Arr is array (integer range <>) of Integer;
411 -- A : Arr := (positive range -1 .. 2 => 0);
413 -- The bounds of the aggregate itype are cooked up to look reasonable
414 -- (in this particular case the bounds will be 1 .. 2).
416 procedure Make_String_Into_Aggregate (N : Node_Id);
417 -- A string literal can appear in a context in which a one dimensional
418 -- array of characters is expected. This procedure simply rewrites the
419 -- string as an aggregate, prior to resolution.
421 ---------------------------------
422 -- Delta aggregate processing --
423 ---------------------------------
425 procedure Resolve_Delta_Array_Aggregate (N : Node_Id; Typ : Entity_Id);
426 procedure Resolve_Delta_Record_Aggregate (N : Node_Id; Typ : Entity_Id);
428 ------------------------
429 -- Array_Aggr_Subtype --
430 ------------------------
432 function Array_Aggr_Subtype
434 Typ : Entity_Id) return Entity_Id
436 Aggr_Dimension : constant Pos := Number_Dimensions (Typ);
437 -- Number of aggregate index dimensions
439 Aggr_Range : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
440 -- Constrained N_Range of each index dimension in our aggregate itype
442 Aggr_Low : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
443 Aggr_High : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
444 -- Low and High bounds for each index dimension in our aggregate itype
446 Is_Fully_Positional : Boolean := True;
448 procedure Collect_Aggr_Bounds (N : Node_Id; Dim : Pos);
449 -- N is an array (sub-)aggregate. Dim is the dimension corresponding
450 -- to (sub-)aggregate N. This procedure collects and removes the side
451 -- effects of the constrained N_Range nodes corresponding to each index
452 -- dimension of our aggregate itype. These N_Range nodes are collected
453 -- in Aggr_Range above.
455 -- Likewise collect in Aggr_Low & Aggr_High above the low and high
456 -- bounds of each index dimension. If, when collecting, two bounds
457 -- corresponding to the same dimension are static and found to differ,
458 -- then emit a warning, and mark N as raising Constraint_Error.
460 -------------------------
461 -- Collect_Aggr_Bounds --
462 -------------------------
464 procedure Collect_Aggr_Bounds (N : Node_Id; Dim : Pos) is
465 This_Range : constant Node_Id := Aggregate_Bounds (N);
466 -- The aggregate range node of this specific sub-aggregate
468 This_Low : constant Node_Id := Low_Bound (Aggregate_Bounds (N));
469 This_High : constant Node_Id := High_Bound (Aggregate_Bounds (N));
470 -- The aggregate bounds of this specific sub-aggregate
476 Remove_Side_Effects (This_Low, Variable_Ref => True);
477 Remove_Side_Effects (This_High, Variable_Ref => True);
479 -- Collect the first N_Range for a given dimension that you find.
480 -- For a given dimension they must be all equal anyway.
482 if No (Aggr_Range (Dim)) then
483 Aggr_Low (Dim) := This_Low;
484 Aggr_High (Dim) := This_High;
485 Aggr_Range (Dim) := This_Range;
488 if Compile_Time_Known_Value (This_Low) then
489 if not Compile_Time_Known_Value (Aggr_Low (Dim)) then
490 Aggr_Low (Dim) := This_Low;
492 elsif Expr_Value (This_Low) /= Expr_Value (Aggr_Low (Dim)) then
493 Set_Raises_Constraint_Error (N);
494 Error_Msg_Warn := SPARK_Mode /= On;
495 Error_Msg_N ("sub-aggregate low bound mismatch<<", N);
496 Error_Msg_N ("\Constraint_Error [<<", N);
500 if Compile_Time_Known_Value (This_High) then
501 if not Compile_Time_Known_Value (Aggr_High (Dim)) then
502 Aggr_High (Dim) := This_High;
505 Expr_Value (This_High) /= Expr_Value (Aggr_High (Dim))
507 Set_Raises_Constraint_Error (N);
508 Error_Msg_Warn := SPARK_Mode /= On;
509 Error_Msg_N ("sub-aggregate high bound mismatch<<", N);
510 Error_Msg_N ("\Constraint_Error [<<", N);
515 if Dim < Aggr_Dimension then
517 -- Process positional components
519 if Present (Expressions (N)) then
520 Expr := First (Expressions (N));
521 while Present (Expr) loop
522 Collect_Aggr_Bounds (Expr, Dim + 1);
527 -- Process component associations
529 if Present (Component_Associations (N)) then
530 Is_Fully_Positional := False;
532 Assoc := First (Component_Associations (N));
533 while Present (Assoc) loop
534 Expr := Expression (Assoc);
535 Collect_Aggr_Bounds (Expr, Dim + 1);
540 end Collect_Aggr_Bounds;
542 -- Array_Aggr_Subtype variables
545 -- The final itype of the overall aggregate
547 Index_Constraints : constant List_Id := New_List;
548 -- The list of index constraints of the aggregate itype
550 -- Start of processing for Array_Aggr_Subtype
553 -- Make sure that the list of index constraints is properly attached to
554 -- the tree, and then collect the aggregate bounds.
556 Set_Parent (Index_Constraints, N);
557 Collect_Aggr_Bounds (N, 1);
559 -- Build the list of constrained indexes of our aggregate itype
561 for J in 1 .. Aggr_Dimension loop
562 Create_Index : declare
563 Index_Base : constant Entity_Id :=
564 Base_Type (Etype (Aggr_Range (J)));
565 Index_Typ : Entity_Id;
568 -- Construct the Index subtype, and associate it with the range
569 -- construct that generates it.
572 Create_Itype (Subtype_Kind (Ekind (Index_Base)), Aggr_Range (J));
574 Set_Etype (Index_Typ, Index_Base);
576 if Is_Character_Type (Index_Base) then
577 Set_Is_Character_Type (Index_Typ);
580 Set_Size_Info (Index_Typ, (Index_Base));
581 Set_RM_Size (Index_Typ, RM_Size (Index_Base));
582 Set_First_Rep_Item (Index_Typ, First_Rep_Item (Index_Base));
583 Set_Scalar_Range (Index_Typ, Aggr_Range (J));
585 if Is_Discrete_Or_Fixed_Point_Type (Index_Typ) then
586 Set_RM_Size (Index_Typ, UI_From_Int (Minimum_Size (Index_Typ)));
589 Set_Etype (Aggr_Range (J), Index_Typ);
591 Append (Aggr_Range (J), To => Index_Constraints);
595 -- Now build the Itype
597 Itype := Create_Itype (E_Array_Subtype, N);
599 Set_First_Rep_Item (Itype, First_Rep_Item (Typ));
600 Set_Convention (Itype, Convention (Typ));
601 Set_Depends_On_Private (Itype, Has_Private_Component (Typ));
602 Set_Etype (Itype, Base_Type (Typ));
603 Set_Has_Alignment_Clause (Itype, Has_Alignment_Clause (Typ));
604 Set_Is_Aliased (Itype, Is_Aliased (Typ));
605 Set_Is_Independent (Itype, Is_Independent (Typ));
606 Set_Depends_On_Private (Itype, Depends_On_Private (Typ));
608 Copy_Suppress_Status (Index_Check, Typ, Itype);
609 Copy_Suppress_Status (Length_Check, Typ, Itype);
611 Set_First_Index (Itype, First (Index_Constraints));
612 Set_Is_Constrained (Itype, True);
613 Set_Is_Internal (Itype, True);
615 if Has_Predicates (Typ) then
616 Set_Has_Predicates (Itype);
618 -- If the base type has a predicate, capture the predicated parent
619 -- or the existing predicate function for SPARK use.
621 if Present (Predicate_Function (Typ)) then
622 Set_Predicate_Function (Itype, Predicate_Function (Typ));
624 elsif Is_Itype (Typ) then
625 Set_Predicated_Parent (Itype, Predicated_Parent (Typ));
628 Set_Predicated_Parent (Itype, Typ);
632 -- A simple optimization: purely positional aggregates of static
633 -- components should be passed to gigi unexpanded whenever possible, and
634 -- regardless of the staticness of the bounds themselves. Subsequent
635 -- checks in exp_aggr verify that type is not packed, etc.
637 Set_Size_Known_At_Compile_Time
640 and then Comes_From_Source (N)
641 and then Size_Known_At_Compile_Time (Component_Type (Typ)));
643 -- We always need a freeze node for a packed array subtype, so that we
644 -- can build the Packed_Array_Impl_Type corresponding to the subtype. If
645 -- expansion is disabled, the packed array subtype is not built, and we
646 -- must not generate a freeze node for the type, or else it will appear
647 -- incomplete to gigi.
650 and then not In_Spec_Expression
651 and then Expander_Active
653 Freeze_Itype (Itype, N);
657 end Array_Aggr_Subtype;
659 --------------------------------
660 -- Check_Misspelled_Component --
661 --------------------------------
663 procedure Check_Misspelled_Component
664 (Elements : Elist_Id;
667 Max_Suggestions : constant := 2;
669 Nr_Of_Suggestions : Natural := 0;
670 Suggestion_1 : Entity_Id := Empty;
671 Suggestion_2 : Entity_Id := Empty;
672 Component_Elmt : Elmt_Id;
675 -- All the components of List are matched against Component and a count
676 -- is maintained of possible misspellings. When at the end of the
677 -- analysis there are one or two (not more) possible misspellings,
678 -- these misspellings will be suggested as possible corrections.
680 Component_Elmt := First_Elmt (Elements);
681 while Nr_Of_Suggestions <= Max_Suggestions
682 and then Present (Component_Elmt)
684 if Is_Bad_Spelling_Of
685 (Chars (Node (Component_Elmt)),
688 Nr_Of_Suggestions := Nr_Of_Suggestions + 1;
690 case Nr_Of_Suggestions is
691 when 1 => Suggestion_1 := Node (Component_Elmt);
692 when 2 => Suggestion_2 := Node (Component_Elmt);
697 Next_Elmt (Component_Elmt);
700 -- Report at most two suggestions
702 if Nr_Of_Suggestions = 1 then
703 Error_Msg_NE -- CODEFIX
704 ("\possible misspelling of&", Component, Suggestion_1);
706 elsif Nr_Of_Suggestions = 2 then
707 Error_Msg_Node_2 := Suggestion_2;
708 Error_Msg_NE -- CODEFIX
709 ("\possible misspelling of& or&", Component, Suggestion_1);
711 end Check_Misspelled_Component;
713 ----------------------------------------
714 -- Check_Expr_OK_In_Limited_Aggregate --
715 ----------------------------------------
717 procedure Check_Expr_OK_In_Limited_Aggregate (Expr : Node_Id) is
719 if Is_Limited_Type (Etype (Expr))
720 and then Comes_From_Source (Expr)
722 if In_Instance_Body or else In_Inlined_Body then
725 elsif not OK_For_Limited_Init (Etype (Expr), Expr) then
727 ("initialization not allowed for limited types", Expr);
728 Explain_Limited_Type (Etype (Expr), Expr);
731 end Check_Expr_OK_In_Limited_Aggregate;
733 -------------------------------
734 -- Check_Qualified_Aggregate --
735 -------------------------------
737 procedure Check_Qualified_Aggregate (Level : Nat; Expr : Node_Id) is
743 if Nkind (Parent (Expr)) /= N_Qualified_Expression then
744 Check_SPARK_05_Restriction ("aggregate should be qualified", Expr);
748 Comp_Expr := First (Expressions (Expr));
749 while Present (Comp_Expr) loop
750 if Nkind (Comp_Expr) = N_Aggregate then
751 Check_Qualified_Aggregate (Level - 1, Comp_Expr);
754 Comp_Expr := Next (Comp_Expr);
757 Comp_Assn := First (Component_Associations (Expr));
758 while Present (Comp_Assn) loop
759 Comp_Expr := Expression (Comp_Assn);
761 if Nkind (Comp_Expr) = N_Aggregate then
762 Check_Qualified_Aggregate (Level - 1, Comp_Expr);
765 Comp_Assn := Next (Comp_Assn);
768 end Check_Qualified_Aggregate;
770 ----------------------------------------
771 -- Check_Static_Discriminated_Subtype --
772 ----------------------------------------
774 procedure Check_Static_Discriminated_Subtype (T : Entity_Id; V : Node_Id) is
775 Disc : constant Entity_Id := First_Discriminant (T);
780 if Has_Record_Rep_Clause (T) then
783 elsif Present (Next_Discriminant (Disc)) then
786 elsif Nkind (V) /= N_Integer_Literal then
790 Comp := First_Component (T);
791 while Present (Comp) loop
792 if Is_Scalar_Type (Etype (Comp)) then
795 elsif Is_Private_Type (Etype (Comp))
796 and then Present (Full_View (Etype (Comp)))
797 and then Is_Scalar_Type (Full_View (Etype (Comp)))
801 elsif Is_Array_Type (Etype (Comp)) then
802 if Is_Bit_Packed_Array (Etype (Comp)) then
806 Ind := First_Index (Etype (Comp));
807 while Present (Ind) loop
808 if Nkind (Ind) /= N_Range
809 or else Nkind (Low_Bound (Ind)) /= N_Integer_Literal
810 or else Nkind (High_Bound (Ind)) /= N_Integer_Literal
822 Next_Component (Comp);
825 -- On exit, all components have statically known sizes
827 Set_Size_Known_At_Compile_Time (T);
828 end Check_Static_Discriminated_Subtype;
830 -------------------------
831 -- Is_Others_Aggregate --
832 -------------------------
834 function Is_Others_Aggregate (Aggr : Node_Id) return Boolean is
835 Assoc : constant List_Id := Component_Associations (Aggr);
838 return No (Expressions (Aggr))
839 and then Nkind (First (Choice_List (First (Assoc)))) = N_Others_Choice;
840 end Is_Others_Aggregate;
842 -------------------------
843 -- Is_Single_Aggregate --
844 -------------------------
846 function Is_Single_Aggregate (Aggr : Node_Id) return Boolean is
847 Assoc : constant List_Id := Component_Associations (Aggr);
850 return No (Expressions (Aggr))
851 and then No (Next (First (Assoc)))
852 and then No (Next (First (Choice_List (First (Assoc)))));
853 end Is_Single_Aggregate;
855 ----------------------------
856 -- Is_Top_Level_Aggregate --
857 ----------------------------
859 function Is_Top_Level_Aggregate (Expr : Node_Id) return Boolean is
861 return Nkind (Parent (Expr)) /= N_Aggregate
862 and then (Nkind (Parent (Expr)) /= N_Component_Association
863 or else Nkind (Parent (Parent (Expr))) /= N_Aggregate);
864 end Is_Top_Level_Aggregate;
866 --------------------------------
867 -- Make_String_Into_Aggregate --
868 --------------------------------
870 procedure Make_String_Into_Aggregate (N : Node_Id) is
871 Exprs : constant List_Id := New_List;
872 Loc : constant Source_Ptr := Sloc (N);
873 Str : constant String_Id := Strval (N);
874 Strlen : constant Nat := String_Length (Str);
882 for J in 1 .. Strlen loop
883 C := Get_String_Char (Str, J);
884 Set_Character_Literal_Name (C);
887 Make_Character_Literal (P,
889 Char_Literal_Value => UI_From_CC (C));
890 Set_Etype (C_Node, Any_Character);
891 Append_To (Exprs, C_Node);
894 -- Something special for wide strings???
897 New_N := Make_Aggregate (Loc, Expressions => Exprs);
898 Set_Analyzed (New_N);
899 Set_Etype (New_N, Any_Composite);
902 end Make_String_Into_Aggregate;
904 -----------------------
905 -- Resolve_Aggregate --
906 -----------------------
908 procedure Resolve_Aggregate (N : Node_Id; Typ : Entity_Id) is
909 Loc : constant Source_Ptr := Sloc (N);
911 Aggr_Subtyp : Entity_Id;
912 -- The actual aggregate subtype. This is not necessarily the same as Typ
913 -- which is the subtype of the context in which the aggregate was found.
916 -- Ignore junk empty aggregate resulting from parser error
918 if No (Expressions (N))
919 and then No (Component_Associations (N))
920 and then not Null_Record_Present (N)
925 -- If the aggregate has box-initialized components, its type must be
926 -- frozen so that initialization procedures can properly be called
927 -- in the resolution that follows. The replacement of boxes with
928 -- initialization calls is properly an expansion activity but it must
929 -- be done during resolution.
932 and then Present (Component_Associations (N))
938 Comp := First (Component_Associations (N));
939 while Present (Comp) loop
940 if Box_Present (Comp) then
941 Insert_Actions (N, Freeze_Entity (Typ, N));
950 -- An unqualified aggregate is restricted in SPARK to:
952 -- An aggregate item inside an aggregate for a multi-dimensional array
954 -- An expression being assigned to an unconstrained array, but only if
955 -- the aggregate specifies a value for OTHERS only.
957 if Nkind (Parent (N)) = N_Qualified_Expression then
958 if Is_Array_Type (Typ) then
959 Check_Qualified_Aggregate (Number_Dimensions (Typ), N);
961 Check_Qualified_Aggregate (1, N);
964 if Is_Array_Type (Typ)
965 and then Nkind (Parent (N)) = N_Assignment_Statement
966 and then not Is_Constrained (Etype (Name (Parent (N))))
968 if not Is_Others_Aggregate (N) then
969 Check_SPARK_05_Restriction
970 ("array aggregate should have only OTHERS", N);
973 elsif Is_Top_Level_Aggregate (N) then
974 Check_SPARK_05_Restriction ("aggregate should be qualified", N);
976 -- The legality of this unqualified aggregate is checked by calling
977 -- Check_Qualified_Aggregate from one of its enclosing aggregate,
978 -- unless one of these already causes an error to be issued.
985 -- Check for aggregates not allowed in configurable run-time mode.
986 -- We allow all cases of aggregates that do not come from source, since
987 -- these are all assumed to be small (e.g. bounds of a string literal).
988 -- We also allow aggregates of types we know to be small.
990 if not Support_Aggregates_On_Target
991 and then Comes_From_Source (N)
992 and then (not Known_Static_Esize (Typ) or else Esize (Typ) > 64)
994 Error_Msg_CRT ("aggregate", N);
997 -- Ada 2005 (AI-287): Limited aggregates allowed
999 -- In an instance, ignore aggregate subcomponents tnat may be limited,
1000 -- because they originate in view conflicts. If the original aggregate
1001 -- is legal and the actuals are legal, the aggregate itself is legal.
1003 if Is_Limited_Type (Typ)
1004 and then Ada_Version < Ada_2005
1005 and then not In_Instance
1007 Error_Msg_N ("aggregate type cannot be limited", N);
1008 Explain_Limited_Type (Typ, N);
1010 elsif Is_Class_Wide_Type (Typ) then
1011 Error_Msg_N ("type of aggregate cannot be class-wide", N);
1013 elsif Typ = Any_String
1014 or else Typ = Any_Composite
1016 Error_Msg_N ("no unique type for aggregate", N);
1017 Set_Etype (N, Any_Composite);
1019 elsif Is_Array_Type (Typ) and then Null_Record_Present (N) then
1020 Error_Msg_N ("null record forbidden in array aggregate", N);
1022 elsif Is_Record_Type (Typ) then
1023 Resolve_Record_Aggregate (N, Typ);
1025 elsif Is_Array_Type (Typ) then
1027 -- First a special test, for the case of a positional aggregate of
1028 -- characters which can be replaced by a string literal.
1030 -- Do not perform this transformation if this was a string literal
1031 -- to start with, whose components needed constraint checks, or if
1032 -- the component type is non-static, because it will require those
1033 -- checks and be transformed back into an aggregate. If the index
1034 -- type is not Integer the aggregate may represent a user-defined
1035 -- string type but the context might need the original type so we
1036 -- do not perform the transformation at this point.
1038 if Number_Dimensions (Typ) = 1
1039 and then Is_Standard_Character_Type (Component_Type (Typ))
1040 and then No (Component_Associations (N))
1041 and then not Is_Limited_Composite (Typ)
1042 and then not Is_Private_Composite (Typ)
1043 and then not Is_Bit_Packed_Array (Typ)
1044 and then Nkind (Original_Node (Parent (N))) /= N_String_Literal
1045 and then Is_OK_Static_Subtype (Component_Type (Typ))
1046 and then Base_Type (Etype (First_Index (Typ))) =
1047 Base_Type (Standard_Integer)
1053 Expr := First (Expressions (N));
1054 while Present (Expr) loop
1055 exit when Nkind (Expr) /= N_Character_Literal;
1062 Expr := First (Expressions (N));
1063 while Present (Expr) loop
1064 Store_String_Char (UI_To_CC (Char_Literal_Value (Expr)));
1068 Rewrite (N, Make_String_Literal (Loc, End_String));
1070 Analyze_And_Resolve (N, Typ);
1076 -- Here if we have a real aggregate to deal with
1078 Array_Aggregate : declare
1079 Aggr_Resolved : Boolean;
1081 Aggr_Typ : constant Entity_Id := Etype (Typ);
1082 -- This is the unconstrained array type, which is the type against
1083 -- which the aggregate is to be resolved. Typ itself is the array
1084 -- type of the context which may not be the same subtype as the
1085 -- subtype for the final aggregate.
1088 -- In the following we determine whether an OTHERS choice is
1089 -- allowed inside the array aggregate. The test checks the context
1090 -- in which the array aggregate occurs. If the context does not
1091 -- permit it, or the aggregate type is unconstrained, an OTHERS
1092 -- choice is not allowed (except that it is always allowed on the
1093 -- right-hand side of an assignment statement; in this case the
1094 -- constrainedness of the type doesn't matter, because an array
1095 -- object is always constrained).
1097 -- If expansion is disabled (generic context, or semantics-only
1098 -- mode) actual subtypes cannot be constructed, and the type of an
1099 -- object may be its unconstrained nominal type. However, if the
1100 -- context is an assignment statement, OTHERS is allowed, because
1101 -- the target of the assignment will have a constrained subtype
1102 -- when fully compiled. Ditto if the context is an initialization
1103 -- procedure where a component may have a predicate function that
1104 -- carries the base type.
1106 -- Note that there is no node for Explicit_Actual_Parameter.
1107 -- To test for this context we therefore have to test for node
1108 -- N_Parameter_Association which itself appears only if there is a
1109 -- formal parameter. Consequently we also need to test for
1110 -- N_Procedure_Call_Statement or N_Function_Call.
1112 -- The context may be an N_Reference node, created by expansion.
1113 -- Legality of the others clause was established in the source,
1114 -- so the context is legal.
1116 Set_Etype (N, Aggr_Typ); -- May be overridden later on
1118 if Nkind (Parent (N)) = N_Assignment_Statement
1119 or else Inside_Init_Proc
1120 or else (Is_Constrained (Typ)
1121 and then Nkind_In (Parent (N),
1122 N_Parameter_Association,
1124 N_Procedure_Call_Statement,
1125 N_Generic_Association,
1126 N_Formal_Object_Declaration,
1127 N_Simple_Return_Statement,
1128 N_Object_Declaration,
1129 N_Component_Declaration,
1130 N_Parameter_Specification,
1131 N_Qualified_Expression,
1134 N_Extension_Aggregate,
1135 N_Component_Association,
1136 N_Case_Expression_Alternative,
1140 Resolve_Array_Aggregate
1142 Index => First_Index (Aggr_Typ),
1143 Index_Constr => First_Index (Typ),
1144 Component_Typ => Component_Type (Typ),
1145 Others_Allowed => True);
1148 Resolve_Array_Aggregate
1150 Index => First_Index (Aggr_Typ),
1151 Index_Constr => First_Index (Aggr_Typ),
1152 Component_Typ => Component_Type (Typ),
1153 Others_Allowed => False);
1156 if not Aggr_Resolved then
1158 -- A parenthesized expression may have been intended as an
1159 -- aggregate, leading to a type error when analyzing the
1160 -- component. This can also happen for a nested component
1161 -- (see Analyze_Aggr_Expr).
1163 if Paren_Count (N) > 0 then
1165 ("positional aggregate cannot have one component", N);
1168 Aggr_Subtyp := Any_Composite;
1171 Aggr_Subtyp := Array_Aggr_Subtype (N, Typ);
1174 Set_Etype (N, Aggr_Subtyp);
1175 end Array_Aggregate;
1177 elsif Is_Private_Type (Typ)
1178 and then Present (Full_View (Typ))
1179 and then (In_Inlined_Body or In_Instance_Body)
1180 and then Is_Composite_Type (Full_View (Typ))
1182 Resolve (N, Full_View (Typ));
1185 Error_Msg_N ("illegal context for aggregate", N);
1188 -- If we can determine statically that the evaluation of the aggregate
1189 -- raises Constraint_Error, then replace the aggregate with an
1190 -- N_Raise_Constraint_Error node, but set the Etype to the right
1191 -- aggregate subtype. Gigi needs this.
1193 if Raises_Constraint_Error (N) then
1194 Aggr_Subtyp := Etype (N);
1196 Make_Raise_Constraint_Error (Loc, Reason => CE_Range_Check_Failed));
1197 Set_Raises_Constraint_Error (N);
1198 Set_Etype (N, Aggr_Subtyp);
1202 Check_Function_Writable_Actuals (N);
1203 end Resolve_Aggregate;
1205 -----------------------------
1206 -- Resolve_Array_Aggregate --
1207 -----------------------------
1209 function Resolve_Array_Aggregate
1212 Index_Constr : Node_Id;
1213 Component_Typ : Entity_Id;
1214 Others_Allowed : Boolean) return Boolean
1216 Loc : constant Source_Ptr := Sloc (N);
1218 Failure : constant Boolean := False;
1219 Success : constant Boolean := True;
1221 Index_Typ : constant Entity_Id := Etype (Index);
1222 Index_Typ_Low : constant Node_Id := Type_Low_Bound (Index_Typ);
1223 Index_Typ_High : constant Node_Id := Type_High_Bound (Index_Typ);
1224 -- The type of the index corresponding to the array sub-aggregate along
1225 -- with its low and upper bounds.
1227 Index_Base : constant Entity_Id := Base_Type (Index_Typ);
1228 Index_Base_Low : constant Node_Id := Type_Low_Bound (Index_Base);
1229 Index_Base_High : constant Node_Id := Type_High_Bound (Index_Base);
1230 -- Ditto for the base type
1232 Others_Present : Boolean := False;
1234 Nb_Choices : Nat := 0;
1235 -- Contains the overall number of named choices in this sub-aggregate
1237 function Add (Val : Uint; To : Node_Id) return Node_Id;
1238 -- Creates a new expression node where Val is added to expression To.
1239 -- Tries to constant fold whenever possible. To must be an already
1240 -- analyzed expression.
1242 procedure Check_Bound (BH : Node_Id; AH : in out Node_Id);
1243 -- Checks that AH (the upper bound of an array aggregate) is less than
1244 -- or equal to BH (the upper bound of the index base type). If the check
1245 -- fails, a warning is emitted, the Raises_Constraint_Error flag of N is
1246 -- set, and AH is replaced with a duplicate of BH.
1248 procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id);
1249 -- Checks that range AL .. AH is compatible with range L .. H. Emits a
1250 -- warning if not and sets the Raises_Constraint_Error flag in N.
1252 procedure Check_Length (L, H : Node_Id; Len : Uint);
1253 -- Checks that range L .. H contains at least Len elements. Emits a
1254 -- warning if not and sets the Raises_Constraint_Error flag in N.
1256 function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean;
1257 -- Returns True if range L .. H is dynamic or null
1259 procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean);
1260 -- Given expression node From, this routine sets OK to False if it
1261 -- cannot statically evaluate From. Otherwise it stores this static
1262 -- value into Value.
1264 function Resolve_Aggr_Expr
1266 Single_Elmt : Boolean) return Boolean;
1267 -- Resolves aggregate expression Expr. Returns False if resolution
1268 -- fails. If Single_Elmt is set to False, the expression Expr may be
1269 -- used to initialize several array aggregate elements (this can happen
1270 -- for discrete choices such as "L .. H => Expr" or the OTHERS choice).
1271 -- In this event we do not resolve Expr unless expansion is disabled.
1272 -- To know why, see the DELAYED COMPONENT RESOLUTION note above.
1274 -- NOTE: In the case of "... => <>", we pass the in the
1275 -- N_Component_Association node as Expr, since there is no Expression in
1276 -- that case, and we need a Sloc for the error message.
1278 procedure Resolve_Iterated_Component_Association
1280 Index_Typ : Entity_Id);
1287 function Add (Val : Uint; To : Node_Id) return Node_Id is
1293 if Raises_Constraint_Error (To) then
1297 -- First test if we can do constant folding
1299 if Compile_Time_Known_Value (To)
1300 or else Nkind (To) = N_Integer_Literal
1302 Expr_Pos := Make_Integer_Literal (Loc, Expr_Value (To) + Val);
1303 Set_Is_Static_Expression (Expr_Pos);
1304 Set_Etype (Expr_Pos, Etype (To));
1305 Set_Analyzed (Expr_Pos, Analyzed (To));
1307 if not Is_Enumeration_Type (Index_Typ) then
1310 -- If we are dealing with enumeration return
1311 -- Index_Typ'Val (Expr_Pos)
1315 Make_Attribute_Reference
1317 Prefix => New_Occurrence_Of (Index_Typ, Loc),
1318 Attribute_Name => Name_Val,
1319 Expressions => New_List (Expr_Pos));
1325 -- If we are here no constant folding possible
1327 if not Is_Enumeration_Type (Index_Base) then
1330 Left_Opnd => Duplicate_Subexpr (To),
1331 Right_Opnd => Make_Integer_Literal (Loc, Val));
1333 -- If we are dealing with enumeration return
1334 -- Index_Typ'Val (Index_Typ'Pos (To) + Val)
1338 Make_Attribute_Reference
1340 Prefix => New_Occurrence_Of (Index_Typ, Loc),
1341 Attribute_Name => Name_Pos,
1342 Expressions => New_List (Duplicate_Subexpr (To)));
1346 Left_Opnd => To_Pos,
1347 Right_Opnd => Make_Integer_Literal (Loc, Val));
1350 Make_Attribute_Reference
1352 Prefix => New_Occurrence_Of (Index_Typ, Loc),
1353 Attribute_Name => Name_Val,
1354 Expressions => New_List (Expr_Pos));
1356 -- If the index type has a non standard representation, the
1357 -- attributes 'Val and 'Pos expand into function calls and the
1358 -- resulting expression is considered non-safe for reevaluation
1359 -- by the backend. Relocate it into a constant temporary in order
1360 -- to make it safe for reevaluation.
1362 if Has_Non_Standard_Rep (Etype (N)) then
1367 Def_Id := Make_Temporary (Loc, 'R', Expr);
1368 Set_Etype (Def_Id, Index_Typ);
1370 Make_Object_Declaration (Loc,
1371 Defining_Identifier => Def_Id,
1372 Object_Definition =>
1373 New_Occurrence_Of (Index_Typ, Loc),
1374 Constant_Present => True,
1375 Expression => Relocate_Node (Expr)));
1377 Expr := New_Occurrence_Of (Def_Id, Loc);
1389 procedure Check_Bound (BH : Node_Id; AH : in out Node_Id) is
1397 Get (Value => Val_BH, From => BH, OK => OK_BH);
1398 Get (Value => Val_AH, From => AH, OK => OK_AH);
1400 if OK_BH and then OK_AH and then Val_BH < Val_AH then
1401 Set_Raises_Constraint_Error (N);
1402 Error_Msg_Warn := SPARK_Mode /= On;
1403 Error_Msg_N ("upper bound out of range<<", AH);
1404 Error_Msg_N ("\Constraint_Error [<<", AH);
1406 -- You need to set AH to BH or else in the case of enumerations
1407 -- indexes we will not be able to resolve the aggregate bounds.
1409 AH := Duplicate_Subexpr (BH);
1417 procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id) is
1428 pragma Warnings (Off, OK_AL);
1429 pragma Warnings (Off, OK_AH);
1432 if Raises_Constraint_Error (N)
1433 or else Dynamic_Or_Null_Range (AL, AH)
1438 Get (Value => Val_L, From => L, OK => OK_L);
1439 Get (Value => Val_H, From => H, OK => OK_H);
1441 Get (Value => Val_AL, From => AL, OK => OK_AL);
1442 Get (Value => Val_AH, From => AH, OK => OK_AH);
1444 if OK_L and then Val_L > Val_AL then
1445 Set_Raises_Constraint_Error (N);
1446 Error_Msg_Warn := SPARK_Mode /= On;
1447 Error_Msg_N ("lower bound of aggregate out of range<<", N);
1448 Error_Msg_N ("\Constraint_Error [<<", N);
1451 if OK_H and then Val_H < Val_AH then
1452 Set_Raises_Constraint_Error (N);
1453 Error_Msg_Warn := SPARK_Mode /= On;
1454 Error_Msg_N ("upper bound of aggregate out of range<<", N);
1455 Error_Msg_N ("\Constraint_Error [<<", N);
1463 procedure Check_Length (L, H : Node_Id; Len : Uint) is
1473 if Raises_Constraint_Error (N) then
1477 Get (Value => Val_L, From => L, OK => OK_L);
1478 Get (Value => Val_H, From => H, OK => OK_H);
1480 if not OK_L or else not OK_H then
1484 -- If null range length is zero
1486 if Val_L > Val_H then
1487 Range_Len := Uint_0;
1489 Range_Len := Val_H - Val_L + 1;
1492 if Range_Len < Len then
1493 Set_Raises_Constraint_Error (N);
1494 Error_Msg_Warn := SPARK_Mode /= On;
1495 Error_Msg_N ("too many elements<<", N);
1496 Error_Msg_N ("\Constraint_Error [<<", N);
1500 ---------------------------
1501 -- Dynamic_Or_Null_Range --
1502 ---------------------------
1504 function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean is
1512 Get (Value => Val_L, From => L, OK => OK_L);
1513 Get (Value => Val_H, From => H, OK => OK_H);
1515 return not OK_L or else not OK_H
1516 or else not Is_OK_Static_Expression (L)
1517 or else not Is_OK_Static_Expression (H)
1518 or else Val_L > Val_H;
1519 end Dynamic_Or_Null_Range;
1525 procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean) is
1529 if Compile_Time_Known_Value (From) then
1530 Value := Expr_Value (From);
1532 -- If expression From is something like Some_Type'Val (10) then
1535 elsif Nkind (From) = N_Attribute_Reference
1536 and then Attribute_Name (From) = Name_Val
1537 and then Compile_Time_Known_Value (First (Expressions (From)))
1539 Value := Expr_Value (First (Expressions (From)));
1546 -----------------------
1547 -- Resolve_Aggr_Expr --
1548 -----------------------
1550 function Resolve_Aggr_Expr
1552 Single_Elmt : Boolean) return Boolean
1554 Nxt_Ind : constant Node_Id := Next_Index (Index);
1555 Nxt_Ind_Constr : constant Node_Id := Next_Index (Index_Constr);
1556 -- Index is the current index corresponding to the expression
1558 Resolution_OK : Boolean := True;
1559 -- Set to False if resolution of the expression failed
1562 -- Defend against previous errors
1564 if Nkind (Expr) = N_Error
1565 or else Error_Posted (Expr)
1570 -- If the array type against which we are resolving the aggregate
1571 -- has several dimensions, the expressions nested inside the
1572 -- aggregate must be further aggregates (or strings).
1574 if Present (Nxt_Ind) then
1575 if Nkind (Expr) /= N_Aggregate then
1577 -- A string literal can appear where a one-dimensional array
1578 -- of characters is expected. If the literal looks like an
1579 -- operator, it is still an operator symbol, which will be
1580 -- transformed into a string when analyzed.
1582 if Is_Character_Type (Component_Typ)
1583 and then No (Next_Index (Nxt_Ind))
1584 and then Nkind_In (Expr, N_String_Literal, N_Operator_Symbol)
1586 -- A string literal used in a multidimensional array
1587 -- aggregate in place of the final one-dimensional
1588 -- aggregate must not be enclosed in parentheses.
1590 if Paren_Count (Expr) /= 0 then
1591 Error_Msg_N ("no parenthesis allowed here", Expr);
1594 Make_String_Into_Aggregate (Expr);
1597 Error_Msg_N ("nested array aggregate expected", Expr);
1599 -- If the expression is parenthesized, this may be
1600 -- a missing component association for a 1-aggregate.
1602 if Paren_Count (Expr) > 0 then
1604 ("\if single-component aggregate is intended, "
1605 & "write e.g. (1 ='> ...)", Expr);
1612 -- If it's "... => <>", nothing to resolve
1614 if Nkind (Expr) = N_Component_Association then
1615 pragma Assert (Box_Present (Expr));
1619 -- Ada 2005 (AI-231): Propagate the type to the nested aggregate.
1620 -- Required to check the null-exclusion attribute (if present).
1621 -- This value may be overridden later on.
1623 Set_Etype (Expr, Etype (N));
1625 Resolution_OK := Resolve_Array_Aggregate
1626 (Expr, Nxt_Ind, Nxt_Ind_Constr, Component_Typ, Others_Allowed);
1629 -- If it's "... => <>", nothing to resolve
1631 if Nkind (Expr) = N_Component_Association then
1632 pragma Assert (Box_Present (Expr));
1636 -- Do not resolve the expressions of discrete or others choices
1637 -- unless the expression covers a single component, or the
1638 -- expander is inactive.
1640 -- In SPARK mode, expressions that can perform side effects will
1641 -- be recognized by the gnat2why back-end, and the whole
1642 -- subprogram will be ignored. So semantic analysis can be
1643 -- performed safely.
1646 or else not Expander_Active
1647 or else In_Spec_Expression
1649 Analyze_And_Resolve (Expr, Component_Typ);
1650 Check_Expr_OK_In_Limited_Aggregate (Expr);
1651 Check_Non_Static_Context (Expr);
1652 Aggregate_Constraint_Checks (Expr, Component_Typ);
1653 Check_Unset_Reference (Expr);
1657 -- If an aggregate component has a type with predicates, an explicit
1658 -- predicate check must be applied, as for an assignment statement,
1659 -- because the aggegate might not be expanded into individual
1660 -- component assignments. If the expression covers several components
1661 -- the analysis and the predicate check take place later.
1663 if Has_Predicates (Component_Typ)
1664 and then Analyzed (Expr)
1666 Apply_Predicate_Check (Expr, Component_Typ);
1669 if Raises_Constraint_Error (Expr)
1670 and then Nkind (Parent (Expr)) /= N_Component_Association
1672 Set_Raises_Constraint_Error (N);
1675 -- If the expression has been marked as requiring a range check,
1676 -- then generate it here. It's a bit odd to be generating such
1677 -- checks in the analyzer, but harmless since Generate_Range_Check
1678 -- does nothing (other than making sure Do_Range_Check is set) if
1679 -- the expander is not active.
1681 if Do_Range_Check (Expr) then
1682 Generate_Range_Check (Expr, Component_Typ, CE_Range_Check_Failed);
1685 return Resolution_OK;
1686 end Resolve_Aggr_Expr;
1688 --------------------------------------------
1689 -- Resolve_Iterated_Component_Association --
1690 --------------------------------------------
1692 procedure Resolve_Iterated_Component_Association
1694 Index_Typ : Entity_Id)
1696 Loc : constant Source_Ptr := Sloc (N);
1705 Choice := First (Discrete_Choices (N));
1707 while Present (Choice) loop
1708 if Nkind (Choice) = N_Others_Choice then
1709 Others_Present := True;
1714 -- Choice can be a subtype name, a range, or an expression
1716 if Is_Entity_Name (Choice)
1717 and then Is_Type (Entity (Choice))
1718 and then Base_Type (Entity (Choice)) = Base_Type (Index_Typ)
1723 Analyze_And_Resolve (Choice, Index_Typ);
1730 -- Create a scope in which to introduce an index, which is usually
1731 -- visible in the expression for the component, and needed for its
1734 Ent := New_Internal_Entity (E_Loop, Current_Scope, Loc, 'L');
1735 Set_Etype (Ent, Standard_Void_Type);
1736 Set_Parent (Ent, Parent (N));
1739 Make_Defining_Identifier (Loc,
1740 Chars => Chars (Defining_Identifier (N)));
1742 -- Insert and decorate the index variable in the current scope.
1743 -- The expression has to be analyzed once the index variable is
1744 -- directly visible. Mark the variable as referenced to prevent
1745 -- spurious warnings, given that subsequent uses of its name in the
1746 -- expression will reference the internal (synonym) loop variable.
1749 Set_Etype (Id, Index_Typ);
1750 Set_Ekind (Id, E_Variable);
1751 Set_Scope (Id, Ent);
1752 Set_Referenced (Id);
1754 -- Analyze a copy of the expression, to verify legality. We use
1755 -- a copy because the expression will be analyzed anew when the
1756 -- enclosing aggregate is expanded, and the construct is rewritten
1757 -- as a loop with a new index variable.
1759 Expr := New_Copy_Tree (Expression (N));
1760 Dummy := Resolve_Aggr_Expr (Expr, False);
1762 -- An iterated_component_association may appear in a nested
1763 -- aggregate for a multidimensional structure: preserve the bounds
1764 -- computed for the expression, as well as the anonymous array
1765 -- type generated for it; both are needed during array expansion.
1766 -- This does not work for more than two levels of nesting. ???
1768 if Nkind (Expr) = N_Aggregate then
1769 Set_Aggregate_Bounds (Expression (N), Aggregate_Bounds (Expr));
1770 Set_Etype (Expression (N), Etype (Expr));
1774 end Resolve_Iterated_Component_Association;
1783 Aggr_Low : Node_Id := Empty;
1784 Aggr_High : Node_Id := Empty;
1785 -- The actual low and high bounds of this sub-aggregate
1787 Case_Table_Size : Nat;
1788 -- Contains the size of the case table needed to sort aggregate choices
1790 Choices_Low : Node_Id := Empty;
1791 Choices_High : Node_Id := Empty;
1792 -- The lowest and highest discrete choices values for a named aggregate
1794 Delete_Choice : Boolean;
1795 -- Used when replacing a subtype choice with predicate by a list
1797 Nb_Elements : Uint := Uint_0;
1798 -- The number of elements in a positional aggregate
1800 Nb_Discrete_Choices : Nat := 0;
1801 -- The overall number of discrete choices (not counting others choice)
1803 -- Start of processing for Resolve_Array_Aggregate
1806 -- Ignore junk empty aggregate resulting from parser error
1808 if No (Expressions (N))
1809 and then No (Component_Associations (N))
1810 and then not Null_Record_Present (N)
1815 -- STEP 1: make sure the aggregate is correctly formatted
1817 if Present (Component_Associations (N)) then
1818 Assoc := First (Component_Associations (N));
1819 while Present (Assoc) loop
1820 if Nkind (Assoc) = N_Iterated_Component_Association then
1821 Resolve_Iterated_Component_Association (Assoc, Index_Typ);
1824 Choice := First (Choice_List (Assoc));
1825 Delete_Choice := False;
1826 while Present (Choice) loop
1827 if Nkind (Choice) = N_Others_Choice then
1828 Others_Present := True;
1830 if Choice /= First (Choice_List (Assoc))
1831 or else Present (Next (Choice))
1834 ("OTHERS must appear alone in a choice list", Choice);
1838 if Present (Next (Assoc)) then
1840 ("OTHERS must appear last in an aggregate", Choice);
1844 if Ada_Version = Ada_83
1845 and then Assoc /= First (Component_Associations (N))
1846 and then Nkind_In (Parent (N), N_Assignment_Statement,
1847 N_Object_Declaration)
1850 ("(Ada 83) illegal context for OTHERS choice", N);
1853 elsif Is_Entity_Name (Choice) then
1857 E : constant Entity_Id := Entity (Choice);
1863 if Is_Type (E) and then Has_Predicates (E) then
1864 Freeze_Before (N, E);
1866 if Has_Dynamic_Predicate_Aspect (E) then
1868 ("subtype& has dynamic predicate, not allowed "
1869 & "in aggregate choice", Choice, E);
1871 elsif not Is_OK_Static_Subtype (E) then
1873 ("non-static subtype& has predicate, not allowed "
1874 & "in aggregate choice", Choice, E);
1877 -- If the subtype has a static predicate, replace the
1878 -- original choice with the list of individual values
1879 -- covered by the predicate.
1880 -- This should be deferred to expansion time ???
1882 if Present (Static_Discrete_Predicate (E)) then
1883 Delete_Choice := True;
1886 P := First (Static_Discrete_Predicate (E));
1887 while Present (P) loop
1889 Set_Sloc (C, Sloc (Choice));
1890 Append_To (New_Cs, C);
1894 Insert_List_After (Choice, New_Cs);
1900 Nb_Choices := Nb_Choices + 1;
1903 C : constant Node_Id := Choice;
1908 if Delete_Choice then
1910 Nb_Choices := Nb_Choices - 1;
1911 Delete_Choice := False;
1920 -- At this point we know that the others choice, if present, is by
1921 -- itself and appears last in the aggregate. Check if we have mixed
1922 -- positional and discrete associations (other than the others choice).
1924 if Present (Expressions (N))
1925 and then (Nb_Choices > 1
1926 or else (Nb_Choices = 1 and then not Others_Present))
1929 ("named association cannot follow positional association",
1930 First (Choice_List (First (Component_Associations (N)))));
1934 -- Test for the validity of an others choice if present
1936 if Others_Present and then not Others_Allowed then
1938 ("OTHERS choice not allowed here",
1939 First (Choices (First (Component_Associations (N)))));
1943 -- Protect against cascaded errors
1945 if Etype (Index_Typ) = Any_Type then
1949 -- STEP 2: Process named components
1951 if No (Expressions (N)) then
1952 if Others_Present then
1953 Case_Table_Size := Nb_Choices - 1;
1955 Case_Table_Size := Nb_Choices;
1959 function Empty_Range (A : Node_Id) return Boolean;
1960 -- If an association covers an empty range, some warnings on the
1961 -- expression of the association can be disabled.
1967 function Empty_Range (A : Node_Id) return Boolean is
1968 R : constant Node_Id := First (Choices (A));
1970 return No (Next (R))
1971 and then Nkind (R) = N_Range
1972 and then Compile_Time_Compare
1973 (Low_Bound (R), High_Bound (R), False) = GT;
1980 -- Denote the lowest and highest values in an aggregate choice
1982 S_Low : Node_Id := Empty;
1983 S_High : Node_Id := Empty;
1984 -- if a choice in an aggregate is a subtype indication these
1985 -- denote the lowest and highest values of the subtype
1987 Table : Case_Table_Type (0 .. Case_Table_Size);
1988 -- Used to sort all the different choice values. Entry zero is
1989 -- reserved for sorting purposes.
1991 Single_Choice : Boolean;
1992 -- Set to true every time there is a single discrete choice in a
1993 -- discrete association
1995 Prev_Nb_Discrete_Choices : Nat;
1996 -- Used to keep track of the number of discrete choices in the
1997 -- current association.
1999 Errors_Posted_On_Choices : Boolean := False;
2000 -- Keeps track of whether any choices have semantic errors
2002 -- Start of processing for Step_2
2005 -- STEP 2 (A): Check discrete choices validity
2007 Assoc := First (Component_Associations (N));
2008 while Present (Assoc) loop
2009 Prev_Nb_Discrete_Choices := Nb_Discrete_Choices;
2010 Choice := First (Choice_List (Assoc));
2015 if Nkind (Choice) = N_Others_Choice then
2016 Single_Choice := False;
2019 -- Test for subtype mark without constraint
2021 elsif Is_Entity_Name (Choice) and then
2022 Is_Type (Entity (Choice))
2024 if Base_Type (Entity (Choice)) /= Index_Base then
2026 ("invalid subtype mark in aggregate choice",
2031 -- Case of subtype indication
2033 elsif Nkind (Choice) = N_Subtype_Indication then
2034 Resolve_Discrete_Subtype_Indication (Choice, Index_Base);
2036 if Has_Dynamic_Predicate_Aspect
2037 (Entity (Subtype_Mark (Choice)))
2040 ("subtype& has dynamic predicate, "
2041 & "not allowed in aggregate choice",
2042 Choice, Entity (Subtype_Mark (Choice)));
2045 -- Does the subtype indication evaluation raise CE?
2047 Get_Index_Bounds (Subtype_Mark (Choice), S_Low, S_High);
2048 Get_Index_Bounds (Choice, Low, High);
2049 Check_Bounds (S_Low, S_High, Low, High);
2051 -- Case of range or expression
2054 Resolve (Choice, Index_Base);
2055 Check_Unset_Reference (Choice);
2056 Check_Non_Static_Context (Choice);
2058 -- If semantic errors were posted on the choice, then
2059 -- record that for possible early return from later
2060 -- processing (see handling of enumeration choices).
2062 if Error_Posted (Choice) then
2063 Errors_Posted_On_Choices := True;
2066 -- Do not range check a choice. This check is redundant
2067 -- since this test is already done when we check that the
2068 -- bounds of the array aggregate are within range.
2070 Set_Do_Range_Check (Choice, False);
2072 -- In SPARK, the choice must be static
2074 if not (Is_OK_Static_Expression (Choice)
2075 or else (Nkind (Choice) = N_Range
2076 and then Is_OK_Static_Range (Choice)))
2078 Check_SPARK_05_Restriction
2079 ("choice should be static", Choice);
2083 -- If we could not resolve the discrete choice stop here
2085 if Etype (Choice) = Any_Type then
2088 -- If the discrete choice raises CE get its original bounds
2090 elsif Nkind (Choice) = N_Raise_Constraint_Error then
2091 Set_Raises_Constraint_Error (N);
2092 Get_Index_Bounds (Original_Node (Choice), Low, High);
2094 -- Otherwise get its bounds as usual
2097 Get_Index_Bounds (Choice, Low, High);
2100 if (Dynamic_Or_Null_Range (Low, High)
2101 or else (Nkind (Choice) = N_Subtype_Indication
2103 Dynamic_Or_Null_Range (S_Low, S_High)))
2104 and then Nb_Choices /= 1
2107 ("dynamic or empty choice in aggregate "
2108 & "must be the only choice", Choice);
2112 if not (All_Composite_Constraints_Static (Low)
2113 and then All_Composite_Constraints_Static (High)
2114 and then All_Composite_Constraints_Static (S_Low)
2115 and then All_Composite_Constraints_Static (S_High))
2117 Check_Restriction (No_Dynamic_Sized_Objects, Choice);
2120 Nb_Discrete_Choices := Nb_Discrete_Choices + 1;
2121 Table (Nb_Discrete_Choices).Lo := Low;
2122 Table (Nb_Discrete_Choices).Hi := High;
2123 Table (Nb_Discrete_Choices).Choice := Choice;
2129 -- Check if we have a single discrete choice and whether
2130 -- this discrete choice specifies a single value.
2133 (Nb_Discrete_Choices = Prev_Nb_Discrete_Choices + 1)
2134 and then (Low = High);
2140 -- Ada 2005 (AI-231)
2142 if Ada_Version >= Ada_2005
2143 and then Known_Null (Expression (Assoc))
2144 and then not Empty_Range (Assoc)
2146 Check_Can_Never_Be_Null (Etype (N), Expression (Assoc));
2149 -- Ada 2005 (AI-287): In case of default initialized component
2150 -- we delay the resolution to the expansion phase.
2152 if Box_Present (Assoc) then
2154 -- Ada 2005 (AI-287): In case of default initialization of a
2155 -- component the expander will generate calls to the
2156 -- corresponding initialization subprogram. We need to call
2157 -- Resolve_Aggr_Expr to check the rules about
2160 if not Resolve_Aggr_Expr
2161 (Assoc, Single_Elmt => Single_Choice)
2166 elsif Nkind (Assoc) = N_Iterated_Component_Association then
2167 null; -- handled above, in a loop context.
2169 elsif not Resolve_Aggr_Expr
2170 (Expression (Assoc), Single_Elmt => Single_Choice)
2174 -- Check incorrect use of dynamically tagged expression
2176 -- We differentiate here two cases because the expression may
2177 -- not be decorated. For example, the analysis and resolution
2178 -- of the expression associated with the others choice will be
2179 -- done later with the full aggregate. In such case we
2180 -- duplicate the expression tree to analyze the copy and
2181 -- perform the required check.
2183 elsif not Present (Etype (Expression (Assoc))) then
2185 Save_Analysis : constant Boolean := Full_Analysis;
2186 Expr : constant Node_Id :=
2187 New_Copy_Tree (Expression (Assoc));
2190 Expander_Mode_Save_And_Set (False);
2191 Full_Analysis := False;
2193 -- Analyze the expression, making sure it is properly
2194 -- attached to the tree before we do the analysis.
2196 Set_Parent (Expr, Parent (Expression (Assoc)));
2199 -- Compute its dimensions now, rather than at the end of
2200 -- resolution, because in the case of multidimensional
2201 -- aggregates subsequent expansion may lead to spurious
2204 Check_Expression_Dimensions (Expr, Component_Typ);
2206 -- If the expression is a literal, propagate this info
2207 -- to the expression in the association, to enable some
2208 -- optimizations downstream.
2210 if Is_Entity_Name (Expr)
2211 and then Present (Entity (Expr))
2212 and then Ekind (Entity (Expr)) = E_Enumeration_Literal
2215 (Expression (Assoc), Component_Typ);
2218 Full_Analysis := Save_Analysis;
2219 Expander_Mode_Restore;
2221 if Is_Tagged_Type (Etype (Expr)) then
2222 Check_Dynamically_Tagged_Expression
2224 Typ => Component_Type (Etype (N)),
2229 elsif Is_Tagged_Type (Etype (Expression (Assoc))) then
2230 Check_Dynamically_Tagged_Expression
2231 (Expr => Expression (Assoc),
2232 Typ => Component_Type (Etype (N)),
2239 -- If aggregate contains more than one choice then these must be
2240 -- static. Check for duplicate and missing values.
2242 -- Note: there is duplicated code here wrt Check_Choice_Set in
2243 -- the body of Sem_Case, and it is possible we could just reuse
2244 -- that procedure. To be checked ???
2246 if Nb_Discrete_Choices > 1 then
2247 Check_Choices : declare
2249 -- Location of choice for messages
2253 -- High end of one range and Low end of the next. Should be
2254 -- contiguous if there is no hole in the list of values.
2258 -- End points of duplicated range
2260 Missing_Or_Duplicates : Boolean := False;
2261 -- Set True if missing or duplicate choices found
2263 procedure Output_Bad_Choices (Lo, Hi : Uint; C : Node_Id);
2264 -- Output continuation message with a representation of the
2265 -- bounds (just Lo if Lo = Hi, else Lo .. Hi). C is the
2266 -- choice node where the message is to be posted.
2268 ------------------------
2269 -- Output_Bad_Choices --
2270 ------------------------
2272 procedure Output_Bad_Choices (Lo, Hi : Uint; C : Node_Id) is
2274 -- Enumeration type case
2276 if Is_Enumeration_Type (Index_Typ) then
2278 Chars (Get_Enum_Lit_From_Pos (Index_Typ, Lo, Loc));
2280 Chars (Get_Enum_Lit_From_Pos (Index_Typ, Hi, Loc));
2283 Error_Msg_N ("\\ %!", C);
2285 Error_Msg_N ("\\ % .. %!", C);
2288 -- Integer types case
2291 Error_Msg_Uint_1 := Lo;
2292 Error_Msg_Uint_2 := Hi;
2295 Error_Msg_N ("\\ ^!", C);
2297 Error_Msg_N ("\\ ^ .. ^!", C);
2300 end Output_Bad_Choices;
2302 -- Start of processing for Check_Choices
2305 Sort_Case_Table (Table);
2307 -- First we do a quick linear loop to find out if we have
2308 -- any duplicates or missing entries (usually we have a
2309 -- legal aggregate, so this will get us out quickly).
2311 for J in 1 .. Nb_Discrete_Choices - 1 loop
2312 Hi_Val := Expr_Value (Table (J).Hi);
2313 Lo_Val := Expr_Value (Table (J + 1).Lo);
2316 or else (Lo_Val > Hi_Val + 1
2317 and then not Others_Present)
2319 Missing_Or_Duplicates := True;
2324 -- If we have missing or duplicate entries, first fill in
2325 -- the Highest entries to make life easier in the following
2326 -- loops to detect bad entries.
2328 if Missing_Or_Duplicates then
2329 Table (1).Highest := Expr_Value (Table (1).Hi);
2331 for J in 2 .. Nb_Discrete_Choices loop
2332 Table (J).Highest :=
2334 (Table (J - 1).Highest, Expr_Value (Table (J).Hi));
2337 -- Loop through table entries to find duplicate indexes
2339 for J in 2 .. Nb_Discrete_Choices loop
2340 Lo_Val := Expr_Value (Table (J).Lo);
2341 Hi_Val := Expr_Value (Table (J).Hi);
2343 -- Case where we have duplicates (the lower bound of
2344 -- this choice is less than or equal to the highest
2345 -- high bound found so far).
2347 if Lo_Val <= Table (J - 1).Highest then
2349 -- We move backwards looking for duplicates. We can
2350 -- abandon this loop as soon as we reach a choice
2351 -- highest value that is less than Lo_Val.
2353 for K in reverse 1 .. J - 1 loop
2354 exit when Table (K).Highest < Lo_Val;
2356 -- Here we may have duplicates between entries
2357 -- for K and J. Get range of duplicates.
2360 UI_Max (Lo_Val, Expr_Value (Table (K).Lo));
2362 UI_Min (Hi_Val, Expr_Value (Table (K).Hi));
2364 -- Nothing to do if duplicate range is null
2366 if Lo_Dup > Hi_Dup then
2369 -- Otherwise place proper message
2372 -- We place message on later choice, with a
2373 -- line reference to the earlier choice.
2375 if Sloc (Table (J).Choice) <
2376 Sloc (Table (K).Choice)
2378 Choice := Table (K).Choice;
2379 Error_Msg_Sloc := Sloc (Table (J).Choice);
2381 Choice := Table (J).Choice;
2382 Error_Msg_Sloc := Sloc (Table (K).Choice);
2385 if Lo_Dup = Hi_Dup then
2387 ("index value in array aggregate "
2388 & "duplicates the one given#!", Choice);
2391 ("index values in array aggregate "
2392 & "duplicate those given#!", Choice);
2395 Output_Bad_Choices (Lo_Dup, Hi_Dup, Choice);
2401 -- Loop through entries in table to find missing indexes.
2402 -- Not needed if others, since missing impossible.
2404 if not Others_Present then
2405 for J in 2 .. Nb_Discrete_Choices loop
2406 Lo_Val := Expr_Value (Table (J).Lo);
2407 Hi_Val := Table (J - 1).Highest;
2409 if Lo_Val > Hi_Val + 1 then
2412 Error_Node : Node_Id;
2415 -- If the choice is the bound of a range in
2416 -- a subtype indication, it is not in the
2417 -- source lists for the aggregate itself, so
2418 -- post the error on the aggregate. Otherwise
2419 -- post it on choice itself.
2421 Choice := Table (J).Choice;
2423 if Is_List_Member (Choice) then
2424 Error_Node := Choice;
2429 if Hi_Val + 1 = Lo_Val - 1 then
2431 ("missing index value "
2432 & "in array aggregate!", Error_Node);
2435 ("missing index values "
2436 & "in array aggregate!", Error_Node);
2440 (Hi_Val + 1, Lo_Val - 1, Error_Node);
2446 -- If either missing or duplicate values, return failure
2448 Set_Etype (N, Any_Composite);
2454 -- STEP 2 (B): Compute aggregate bounds and min/max choices values
2456 if Nb_Discrete_Choices > 0 then
2457 Choices_Low := Table (1).Lo;
2458 Choices_High := Table (Nb_Discrete_Choices).Hi;
2461 -- If Others is present, then bounds of aggregate come from the
2462 -- index constraint (not the choices in the aggregate itself).
2464 if Others_Present then
2465 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2467 -- Abandon processing if either bound is already signalled as
2468 -- an error (prevents junk cascaded messages and blow ups).
2470 if Nkind (Aggr_Low) = N_Error
2472 Nkind (Aggr_High) = N_Error
2477 -- No others clause present
2480 -- Special processing if others allowed and not present. This
2481 -- means that the bounds of the aggregate come from the index
2482 -- constraint (and the length must match).
2484 if Others_Allowed then
2485 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2487 -- Abandon processing if either bound is already signalled
2488 -- as an error (stop junk cascaded messages and blow ups).
2490 if Nkind (Aggr_Low) = N_Error
2492 Nkind (Aggr_High) = N_Error
2497 -- If others allowed, and no others present, then the array
2498 -- should cover all index values. If it does not, we will
2499 -- get a length check warning, but there is two cases where
2500 -- an additional warning is useful:
2502 -- If we have no positional components, and the length is
2503 -- wrong (which we can tell by others being allowed with
2504 -- missing components), and the index type is an enumeration
2505 -- type, then issue appropriate warnings about these missing
2506 -- components. They are only warnings, since the aggregate
2507 -- is fine, it's just the wrong length. We skip this check
2508 -- for standard character types (since there are no literals
2509 -- and it is too much trouble to concoct them), and also if
2510 -- any of the bounds have values that are not known at
2513 -- Another case warranting a warning is when the length
2514 -- is right, but as above we have an index type that is
2515 -- an enumeration, and the bounds do not match. This is a
2516 -- case where dubious sliding is allowed and we generate a
2517 -- warning that the bounds do not match.
2519 if No (Expressions (N))
2520 and then Nkind (Index) = N_Range
2521 and then Is_Enumeration_Type (Etype (Index))
2522 and then not Is_Standard_Character_Type (Etype (Index))
2523 and then Compile_Time_Known_Value (Aggr_Low)
2524 and then Compile_Time_Known_Value (Aggr_High)
2525 and then Compile_Time_Known_Value (Choices_Low)
2526 and then Compile_Time_Known_Value (Choices_High)
2528 -- If any of the expressions or range bounds in choices
2529 -- have semantic errors, then do not attempt further
2530 -- resolution, to prevent cascaded errors.
2532 if Errors_Posted_On_Choices then
2537 ALo : constant Node_Id := Expr_Value_E (Aggr_Low);
2538 AHi : constant Node_Id := Expr_Value_E (Aggr_High);
2539 CLo : constant Node_Id := Expr_Value_E (Choices_Low);
2540 CHi : constant Node_Id := Expr_Value_E (Choices_High);
2545 -- Warning case 1, missing values at start/end. Only
2546 -- do the check if the number of entries is too small.
2548 if (Enumeration_Pos (CHi) - Enumeration_Pos (CLo))
2550 (Enumeration_Pos (AHi) - Enumeration_Pos (ALo))
2553 ("missing index value(s) in array aggregate??",
2556 -- Output missing value(s) at start
2558 if Chars (ALo) /= Chars (CLo) then
2561 if Chars (ALo) = Chars (Ent) then
2562 Error_Msg_Name_1 := Chars (ALo);
2563 Error_Msg_N ("\ %??", N);
2565 Error_Msg_Name_1 := Chars (ALo);
2566 Error_Msg_Name_2 := Chars (Ent);
2567 Error_Msg_N ("\ % .. %??", N);
2571 -- Output missing value(s) at end
2573 if Chars (AHi) /= Chars (CHi) then
2576 if Chars (AHi) = Chars (Ent) then
2577 Error_Msg_Name_1 := Chars (Ent);
2578 Error_Msg_N ("\ %??", N);
2580 Error_Msg_Name_1 := Chars (Ent);
2581 Error_Msg_Name_2 := Chars (AHi);
2582 Error_Msg_N ("\ % .. %??", N);
2586 -- Warning case 2, dubious sliding. The First_Subtype
2587 -- test distinguishes between a constrained type where
2588 -- sliding is not allowed (so we will get a warning
2589 -- later that Constraint_Error will be raised), and
2590 -- the unconstrained case where sliding is permitted.
2592 elsif (Enumeration_Pos (CHi) - Enumeration_Pos (CLo))
2594 (Enumeration_Pos (AHi) - Enumeration_Pos (ALo))
2595 and then Chars (ALo) /= Chars (CLo)
2597 not Is_Constrained (First_Subtype (Etype (N)))
2600 ("bounds of aggregate do not match target??", N);
2606 -- If no others, aggregate bounds come from aggregate
2608 Aggr_Low := Choices_Low;
2609 Aggr_High := Choices_High;
2613 -- STEP 3: Process positional components
2616 -- STEP 3 (A): Process positional elements
2618 Expr := First (Expressions (N));
2619 Nb_Elements := Uint_0;
2620 while Present (Expr) loop
2621 Nb_Elements := Nb_Elements + 1;
2623 -- Ada 2005 (AI-231)
2625 if Ada_Version >= Ada_2005 and then Known_Null (Expr) then
2626 Check_Can_Never_Be_Null (Etype (N), Expr);
2629 if not Resolve_Aggr_Expr (Expr, Single_Elmt => True) then
2633 -- Check incorrect use of dynamically tagged expression
2635 if Is_Tagged_Type (Etype (Expr)) then
2636 Check_Dynamically_Tagged_Expression
2638 Typ => Component_Type (Etype (N)),
2645 if Others_Present then
2646 Assoc := Last (Component_Associations (N));
2648 -- Ada 2005 (AI-231)
2650 if Ada_Version >= Ada_2005 and then Known_Null (Assoc) then
2651 Check_Can_Never_Be_Null (Etype (N), Expression (Assoc));
2654 -- Ada 2005 (AI-287): In case of default initialized component,
2655 -- we delay the resolution to the expansion phase.
2657 if Box_Present (Assoc) then
2659 -- Ada 2005 (AI-287): In case of default initialization of a
2660 -- component the expander will generate calls to the
2661 -- corresponding initialization subprogram. We need to call
2662 -- Resolve_Aggr_Expr to check the rules about
2665 if not Resolve_Aggr_Expr (Assoc, Single_Elmt => False) then
2669 elsif not Resolve_Aggr_Expr (Expression (Assoc),
2670 Single_Elmt => False)
2674 -- Check incorrect use of dynamically tagged expression. The
2675 -- expression of the others choice has not been resolved yet.
2676 -- In order to diagnose the semantic error we create a duplicate
2677 -- tree to analyze it and perform the check.
2681 Save_Analysis : constant Boolean := Full_Analysis;
2682 Expr : constant Node_Id :=
2683 New_Copy_Tree (Expression (Assoc));
2686 Expander_Mode_Save_And_Set (False);
2687 Full_Analysis := False;
2689 Full_Analysis := Save_Analysis;
2690 Expander_Mode_Restore;
2692 if Is_Tagged_Type (Etype (Expr)) then
2693 Check_Dynamically_Tagged_Expression
2695 Typ => Component_Type (Etype (N)),
2702 -- STEP 3 (B): Compute the aggregate bounds
2704 if Others_Present then
2705 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2708 if Others_Allowed then
2709 Get_Index_Bounds (Index_Constr, Aggr_Low, Discard);
2711 Aggr_Low := Index_Typ_Low;
2714 Aggr_High := Add (Nb_Elements - 1, To => Aggr_Low);
2715 Check_Bound (Index_Base_High, Aggr_High);
2719 -- STEP 4: Perform static aggregate checks and save the bounds
2723 Check_Bounds (Index_Typ_Low, Index_Typ_High, Aggr_Low, Aggr_High);
2724 Check_Bounds (Index_Base_Low, Index_Base_High, Aggr_Low, Aggr_High);
2728 if Others_Present and then Nb_Discrete_Choices > 0 then
2729 Check_Bounds (Aggr_Low, Aggr_High, Choices_Low, Choices_High);
2730 Check_Bounds (Index_Typ_Low, Index_Typ_High,
2731 Choices_Low, Choices_High);
2732 Check_Bounds (Index_Base_Low, Index_Base_High,
2733 Choices_Low, Choices_High);
2737 elsif Others_Present and then Nb_Elements > 0 then
2738 Check_Length (Aggr_Low, Aggr_High, Nb_Elements);
2739 Check_Length (Index_Typ_Low, Index_Typ_High, Nb_Elements);
2740 Check_Length (Index_Base_Low, Index_Base_High, Nb_Elements);
2743 if Raises_Constraint_Error (Aggr_Low)
2744 or else Raises_Constraint_Error (Aggr_High)
2746 Set_Raises_Constraint_Error (N);
2749 Aggr_Low := Duplicate_Subexpr (Aggr_Low);
2751 -- Do not duplicate Aggr_High if Aggr_High = Aggr_Low + Nb_Elements
2752 -- since the addition node returned by Add is not yet analyzed. Attach
2753 -- to tree and analyze first. Reset analyzed flag to ensure it will get
2754 -- analyzed when it is a literal bound whose type must be properly set.
2756 if Others_Present or else Nb_Discrete_Choices > 0 then
2757 Aggr_High := Duplicate_Subexpr (Aggr_High);
2759 if Etype (Aggr_High) = Universal_Integer then
2760 Set_Analyzed (Aggr_High, False);
2764 -- If the aggregate already has bounds attached to it, it means this is
2765 -- a positional aggregate created as an optimization by
2766 -- Exp_Aggr.Convert_To_Positional, so we don't want to change those
2769 if Present (Aggregate_Bounds (N)) and then not Others_Allowed then
2770 Aggr_Low := Low_Bound (Aggregate_Bounds (N));
2771 Aggr_High := High_Bound (Aggregate_Bounds (N));
2774 Set_Aggregate_Bounds
2775 (N, Make_Range (Loc, Low_Bound => Aggr_Low, High_Bound => Aggr_High));
2777 -- The bounds may contain expressions that must be inserted upwards.
2778 -- Attach them fully to the tree. After analysis, remove side effects
2779 -- from upper bound, if still needed.
2781 Set_Parent (Aggregate_Bounds (N), N);
2782 Analyze_And_Resolve (Aggregate_Bounds (N), Index_Typ);
2783 Check_Unset_Reference (Aggregate_Bounds (N));
2785 if not Others_Present and then Nb_Discrete_Choices = 0 then
2787 (Aggregate_Bounds (N),
2788 Duplicate_Subexpr (High_Bound (Aggregate_Bounds (N))));
2791 -- Check the dimensions of each component in the array aggregate
2793 Analyze_Dimension_Array_Aggregate (N, Component_Typ);
2796 end Resolve_Array_Aggregate;
2798 -----------------------------
2799 -- Resolve_Delta_Aggregate --
2800 -----------------------------
2802 procedure Resolve_Delta_Aggregate (N : Node_Id; Typ : Entity_Id) is
2803 Base : constant Node_Id := Expression (N);
2806 if Ada_Version < Ada_2020 then
2807 Error_Msg_N ("delta_aggregate is an Ada 202x feature", N);
2808 Error_Msg_N ("\compile with -gnat2020", N);
2811 if not Is_Composite_Type (Typ) then
2812 Error_Msg_N ("not a composite type", N);
2815 Analyze_And_Resolve (Base, Typ);
2817 if Is_Array_Type (Typ) then
2818 Resolve_Delta_Array_Aggregate (N, Typ);
2820 Resolve_Delta_Record_Aggregate (N, Typ);
2824 end Resolve_Delta_Aggregate;
2826 -----------------------------------
2827 -- Resolve_Delta_Array_Aggregate --
2828 -----------------------------------
2830 procedure Resolve_Delta_Array_Aggregate (N : Node_Id; Typ : Entity_Id) is
2831 Deltas : constant List_Id := Component_Associations (N);
2835 Index_Type : Entity_Id;
2838 Index_Type := Etype (First_Index (Typ));
2840 Assoc := First (Deltas);
2841 while Present (Assoc) loop
2842 if Nkind (Assoc) = N_Iterated_Component_Association then
2843 Choice := First (Choice_List (Assoc));
2844 while Present (Choice) loop
2845 if Nkind (Choice) = N_Others_Choice then
2847 ("others not allowed in delta aggregate", Choice);
2850 Analyze_And_Resolve (Choice, Index_Type);
2857 Id : constant Entity_Id := Defining_Identifier (Assoc);
2858 Ent : constant Entity_Id :=
2860 (E_Loop, Current_Scope, Sloc (Assoc), 'L');
2863 Set_Etype (Ent, Standard_Void_Type);
2864 Set_Parent (Ent, Assoc);
2866 if No (Scope (Id)) then
2868 Set_Etype (Id, Index_Type);
2869 Set_Ekind (Id, E_Variable);
2870 Set_Scope (Id, Ent);
2875 (New_Copy_Tree (Expression (Assoc)), Component_Type (Typ));
2880 Choice := First (Choice_List (Assoc));
2881 while Present (Choice) loop
2882 if Nkind (Choice) = N_Others_Choice then
2884 ("others not allowed in delta aggregate", Choice);
2889 if Is_Entity_Name (Choice)
2890 and then Is_Type (Entity (Choice))
2892 -- Choice covers a range of values
2894 if Base_Type (Entity (Choice)) /=
2895 Base_Type (Index_Type)
2898 ("choice does mat match index type of",
2902 Resolve (Choice, Index_Type);
2909 Analyze_And_Resolve (Expression (Assoc), Component_Type (Typ));
2914 end Resolve_Delta_Array_Aggregate;
2916 ------------------------------------
2917 -- Resolve_Delta_Record_Aggregate --
2918 ------------------------------------
2920 procedure Resolve_Delta_Record_Aggregate (N : Node_Id; Typ : Entity_Id) is
2922 -- Variables used to verify that discriminant-dependent components
2923 -- appear in the same variant.
2925 Comp_Ref : Entity_Id := Empty; -- init to avoid warning
2928 procedure Check_Variant (Id : Entity_Id);
2929 -- If a given component of the delta aggregate appears in a variant
2930 -- part, verify that it is within the same variant as that of previous
2931 -- specified variant components of the delta.
2933 function Get_Component_Type (Nam : Node_Id) return Entity_Id;
2934 -- Locate component with a given name and return its type. If none found
2937 function Nested_In (V1 : Node_Id; V2 : Node_Id) return Boolean;
2938 -- Determine whether variant V1 is within variant V2
2940 function Variant_Depth (N : Node_Id) return Integer;
2941 -- Determine the distance of a variant to the enclosing type
2944 --------------------
2946 --------------------
2948 procedure Check_Variant (Id : Entity_Id) is
2950 Comp_Variant : Node_Id;
2953 if not Has_Discriminants (Typ) then
2957 Comp := First_Entity (Typ);
2958 while Present (Comp) loop
2959 exit when Chars (Comp) = Chars (Id);
2960 Next_Component (Comp);
2963 -- Find the variant, if any, whose component list includes the
2964 -- component declaration.
2966 Comp_Variant := Parent (Parent (List_Containing (Parent (Comp))));
2967 if Nkind (Comp_Variant) = N_Variant then
2968 if No (Variant) then
2969 Variant := Comp_Variant;
2972 elsif Variant /= Comp_Variant then
2974 D1 : constant Integer := Variant_Depth (Variant);
2975 D2 : constant Integer := Variant_Depth (Comp_Variant);
2980 (D1 > D2 and then not Nested_In (Variant, Comp_Variant))
2982 (D2 > D1 and then not Nested_In (Comp_Variant, Variant))
2984 pragma Assert (Present (Comp_Ref));
2985 Error_Msg_Node_2 := Comp_Ref;
2987 ("& and & appear in different variants", Id, Comp);
2989 -- Otherwise retain the deeper variant for subsequent tests
2992 Variant := Comp_Variant;
2999 ------------------------
3000 -- Get_Component_Type --
3001 ------------------------
3003 function Get_Component_Type (Nam : Node_Id) return Entity_Id is
3007 Comp := First_Entity (Typ);
3008 while Present (Comp) loop
3009 if Chars (Comp) = Chars (Nam) then
3010 if Ekind (Comp) = E_Discriminant then
3011 Error_Msg_N ("delta cannot apply to discriminant", Nam);
3014 return Etype (Comp);
3020 Error_Msg_NE ("type& has no component with this name", Nam, Typ);
3022 end Get_Component_Type;
3028 function Nested_In (V1, V2 : Node_Id) return Boolean is
3033 while Nkind (Par) /= N_Full_Type_Declaration loop
3038 Par := Parent (Par);
3048 function Variant_Depth (N : Node_Id) return Integer is
3055 while Nkind (Par) /= N_Full_Type_Declaration loop
3057 Par := Parent (Par);
3065 Deltas : constant List_Id := Component_Associations (N);
3069 Comp_Type : Entity_Id := Empty; -- init to avoid warning
3071 -- Start of processing for Resolve_Delta_Record_Aggregate
3076 Assoc := First (Deltas);
3077 while Present (Assoc) loop
3078 Choice := First (Choice_List (Assoc));
3079 while Present (Choice) loop
3080 Comp_Type := Get_Component_Type (Choice);
3082 if Comp_Type /= Any_Type then
3083 Check_Variant (Choice);
3089 pragma Assert (Present (Comp_Type));
3090 Analyze_And_Resolve (Expression (Assoc), Comp_Type);
3093 end Resolve_Delta_Record_Aggregate;
3095 ---------------------------------
3096 -- Resolve_Extension_Aggregate --
3097 ---------------------------------
3099 -- There are two cases to consider:
3101 -- a) If the ancestor part is a type mark, the components needed are the
3102 -- difference between the components of the expected type and the
3103 -- components of the given type mark.
3105 -- b) If the ancestor part is an expression, it must be unambiguous, and
3106 -- once we have its type we can also compute the needed components as in
3107 -- the previous case. In both cases, if the ancestor type is not the
3108 -- immediate ancestor, we have to build this ancestor recursively.
3110 -- In both cases, discriminants of the ancestor type do not play a role in
3111 -- the resolution of the needed components, because inherited discriminants
3112 -- cannot be used in a type extension. As a result we can compute
3113 -- independently the list of components of the ancestor type and of the
3116 procedure Resolve_Extension_Aggregate (N : Node_Id; Typ : Entity_Id) is
3117 A : constant Node_Id := Ancestor_Part (N);
3122 function Valid_Limited_Ancestor (Anc : Node_Id) return Boolean;
3123 -- If the type is limited, verify that the ancestor part is a legal
3124 -- expression (aggregate or function call, including 'Input)) that does
3125 -- not require a copy, as specified in 7.5(2).
3127 function Valid_Ancestor_Type return Boolean;
3128 -- Verify that the type of the ancestor part is a non-private ancestor
3129 -- of the expected type, which must be a type extension.
3131 procedure Transform_BIP_Assignment (Typ : Entity_Id);
3132 -- For an extension aggregate whose ancestor part is a build-in-place
3133 -- call returning a nonlimited type, this is used to transform the
3134 -- assignment to the ancestor part to use a temp.
3136 ----------------------------
3137 -- Valid_Limited_Ancestor --
3138 ----------------------------
3140 function Valid_Limited_Ancestor (Anc : Node_Id) return Boolean is
3142 if Is_Entity_Name (Anc) and then Is_Type (Entity (Anc)) then
3145 -- The ancestor must be a call or an aggregate, but a call may
3146 -- have been expanded into a temporary, so check original node.
3148 elsif Nkind_In (Anc, N_Aggregate,
3149 N_Extension_Aggregate,
3154 elsif Nkind (Original_Node (Anc)) = N_Function_Call then
3157 elsif Nkind (Anc) = N_Attribute_Reference
3158 and then Attribute_Name (Anc) = Name_Input
3162 elsif Nkind (Anc) = N_Qualified_Expression then
3163 return Valid_Limited_Ancestor (Expression (Anc));
3165 elsif Nkind (Anc) = N_Raise_Expression then
3171 end Valid_Limited_Ancestor;
3173 -------------------------
3174 -- Valid_Ancestor_Type --
3175 -------------------------
3177 function Valid_Ancestor_Type return Boolean is
3178 Imm_Type : Entity_Id;
3181 Imm_Type := Base_Type (Typ);
3182 while Is_Derived_Type (Imm_Type) loop
3183 if Etype (Imm_Type) = Base_Type (A_Type) then
3186 -- The base type of the parent type may appear as a private
3187 -- extension if it is declared as such in a parent unit of the
3188 -- current one. For consistency of the subsequent analysis use
3189 -- the partial view for the ancestor part.
3191 elsif Is_Private_Type (Etype (Imm_Type))
3192 and then Present (Full_View (Etype (Imm_Type)))
3193 and then Base_Type (A_Type) = Full_View (Etype (Imm_Type))
3195 A_Type := Etype (Imm_Type);
3198 -- The parent type may be a private extension. The aggregate is
3199 -- legal if the type of the aggregate is an extension of it that
3200 -- is not a private extension.
3202 elsif Is_Private_Type (A_Type)
3203 and then not Is_Private_Type (Imm_Type)
3204 and then Present (Full_View (A_Type))
3205 and then Base_Type (Full_View (A_Type)) = Etype (Imm_Type)
3209 -- The parent type may be a raise expression (which is legal in
3210 -- any expression context).
3212 elsif A_Type = Raise_Type then
3213 A_Type := Etype (Imm_Type);
3217 Imm_Type := Etype (Base_Type (Imm_Type));
3221 -- If previous loop did not find a proper ancestor, report error
3223 Error_Msg_NE ("expect ancestor type of &", A, Typ);
3225 end Valid_Ancestor_Type;
3227 ------------------------------
3228 -- Transform_BIP_Assignment --
3229 ------------------------------
3231 procedure Transform_BIP_Assignment (Typ : Entity_Id) is
3232 Loc : constant Source_Ptr := Sloc (N);
3233 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'Y', A);
3234 Obj_Decl : constant Node_Id :=
3235 Make_Object_Declaration (Loc,
3236 Defining_Identifier => Def_Id,
3237 Constant_Present => True,
3238 Object_Definition => New_Occurrence_Of (Typ, Loc),
3240 Has_Init_Expression => True);
3242 Set_Etype (Def_Id, Typ);
3243 Set_Ancestor_Part (N, New_Occurrence_Of (Def_Id, Loc));
3244 Insert_Action (N, Obj_Decl);
3245 end Transform_BIP_Assignment;
3247 -- Start of processing for Resolve_Extension_Aggregate
3250 -- Analyze the ancestor part and account for the case where it is a
3251 -- parameterless function call.
3254 Check_Parameterless_Call (A);
3256 -- In SPARK, the ancestor part cannot be a type mark
3258 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
3259 Check_SPARK_05_Restriction ("ancestor part cannot be a type mark", A);
3261 -- AI05-0115: if the ancestor part is a subtype mark, the ancestor
3262 -- must not have unknown discriminants.
3264 if Has_Unknown_Discriminants (Root_Type (Typ)) then
3266 ("aggregate not available for type& whose ancestor "
3267 & "has unknown discriminants", N, Typ);
3271 if not Is_Tagged_Type (Typ) then
3272 Error_Msg_N ("type of extension aggregate must be tagged", N);
3275 elsif Is_Limited_Type (Typ) then
3277 -- Ada 2005 (AI-287): Limited aggregates are allowed
3279 if Ada_Version < Ada_2005 then
3280 Error_Msg_N ("aggregate type cannot be limited", N);
3281 Explain_Limited_Type (Typ, N);
3284 elsif Valid_Limited_Ancestor (A) then
3289 ("limited ancestor part must be aggregate or function call", A);
3292 elsif Is_Class_Wide_Type (Typ) then
3293 Error_Msg_N ("aggregate cannot be of a class-wide type", N);
3297 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
3298 A_Type := Get_Full_View (Entity (A));
3300 if Valid_Ancestor_Type then
3301 Set_Entity (A, A_Type);
3302 Set_Etype (A, A_Type);
3304 Validate_Ancestor_Part (N);
3305 Resolve_Record_Aggregate (N, Typ);
3308 elsif Nkind (A) /= N_Aggregate then
3309 if Is_Overloaded (A) then
3312 Get_First_Interp (A, I, It);
3313 while Present (It.Typ) loop
3315 -- Consider limited interpretations if Ada 2005 or higher
3317 if Is_Tagged_Type (It.Typ)
3318 and then (Ada_Version >= Ada_2005
3319 or else not Is_Limited_Type (It.Typ))
3321 if A_Type /= Any_Type then
3322 Error_Msg_N ("cannot resolve expression", A);
3329 Get_Next_Interp (I, It);
3332 if A_Type = Any_Type then
3333 if Ada_Version >= Ada_2005 then
3335 ("ancestor part must be of a tagged type", A);
3338 ("ancestor part must be of a nonlimited tagged type", A);
3345 A_Type := Etype (A);
3348 if Valid_Ancestor_Type then
3349 Resolve (A, A_Type);
3350 Check_Unset_Reference (A);
3351 Check_Non_Static_Context (A);
3353 -- The aggregate is illegal if the ancestor expression is a call
3354 -- to a function with a limited unconstrained result, unless the
3355 -- type of the aggregate is a null extension. This restriction
3356 -- was added in AI05-67 to simplify implementation.
3358 if Nkind (A) = N_Function_Call
3359 and then Is_Limited_Type (A_Type)
3360 and then not Is_Null_Extension (Typ)
3361 and then not Is_Constrained (A_Type)
3364 ("type of limited ancestor part must be constrained", A);
3366 -- Reject the use of CPP constructors that leave objects partially
3367 -- initialized. For example:
3369 -- type CPP_Root is tagged limited record ...
3370 -- pragma Import (CPP, CPP_Root);
3372 -- type CPP_DT is new CPP_Root and Iface ...
3373 -- pragma Import (CPP, CPP_DT);
3375 -- type Ada_DT is new CPP_DT with ...
3377 -- Obj : Ada_DT := Ada_DT'(New_CPP_Root with others => <>);
3379 -- Using the constructor of CPP_Root the slots of the dispatch
3380 -- table of CPP_DT cannot be set, and the secondary tag of
3381 -- CPP_DT is unknown.
3383 elsif Nkind (A) = N_Function_Call
3384 and then Is_CPP_Constructor_Call (A)
3385 and then Enclosing_CPP_Parent (Typ) /= A_Type
3388 ("??must use 'C'P'P constructor for type &", A,
3389 Enclosing_CPP_Parent (Typ));
3391 -- The following call is not needed if the previous warning
3392 -- is promoted to an error.
3394 Resolve_Record_Aggregate (N, Typ);
3396 elsif Is_Class_Wide_Type (Etype (A))
3397 and then Nkind (Original_Node (A)) = N_Function_Call
3399 -- If the ancestor part is a dispatching call, it appears
3400 -- statically to be a legal ancestor, but it yields any member
3401 -- of the class, and it is not possible to determine whether
3402 -- it is an ancestor of the extension aggregate (much less
3403 -- which ancestor). It is not possible to determine the
3404 -- components of the extension part.
3406 -- This check implements AI-306, which in fact was motivated by
3407 -- an AdaCore query to the ARG after this test was added.
3409 Error_Msg_N ("ancestor part must be statically tagged", A);
3411 -- We are using the build-in-place protocol, but we can't build
3412 -- in place, because we need to call the function before
3413 -- allocating the aggregate. Could do better for null
3414 -- extensions, and maybe for nondiscriminated types.
3415 -- This is wrong for limited, but those were wrong already.
3417 if not Is_Limited_View (A_Type)
3418 and then Is_Build_In_Place_Function_Call (A)
3420 Transform_BIP_Assignment (A_Type);
3423 Resolve_Record_Aggregate (N, Typ);
3428 Error_Msg_N ("no unique type for this aggregate", A);
3431 Check_Function_Writable_Actuals (N);
3432 end Resolve_Extension_Aggregate;
3434 ------------------------------
3435 -- Resolve_Record_Aggregate --
3436 ------------------------------
3438 procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id) is
3439 New_Assoc_List : constant List_Id := New_List;
3440 -- New_Assoc_List is the newly built list of N_Component_Association
3443 Others_Etype : Entity_Id := Empty;
3444 -- This variable is used to save the Etype of the last record component
3445 -- that takes its value from the others choice. Its purpose is:
3447 -- (a) make sure the others choice is useful
3449 -- (b) make sure the type of all the components whose value is
3450 -- subsumed by the others choice are the same.
3452 -- This variable is updated as a side effect of function Get_Value.
3454 Box_Node : Node_Id := Empty;
3455 Is_Box_Present : Boolean := False;
3456 Others_Box : Integer := 0;
3457 -- Ada 2005 (AI-287): Variables used in case of default initialization
3458 -- to provide a functionality similar to Others_Etype. Box_Present
3459 -- indicates that the component takes its default initialization;
3460 -- Others_Box counts the number of components of the current aggregate
3461 -- (which may be a sub-aggregate of a larger one) that are default-
3462 -- initialized. A value of One indicates that an others_box is present.
3463 -- Any larger value indicates that the others_box is not redundant.
3464 -- These variables, similar to Others_Etype, are also updated as a side
3465 -- effect of function Get_Value. Box_Node is used to place a warning on
3466 -- a redundant others_box.
3468 procedure Add_Association
3469 (Component : Entity_Id;
3471 Assoc_List : List_Id;
3472 Is_Box_Present : Boolean := False);
3473 -- Builds a new N_Component_Association node which associates Component
3474 -- to expression Expr and adds it to the association list being built,
3475 -- either New_Assoc_List, or the association being built for an inner
3478 procedure Add_Discriminant_Values
3479 (New_Aggr : Node_Id;
3480 Assoc_List : List_Id);
3481 -- The constraint to a component may be given by a discriminant of the
3482 -- enclosing type, in which case we have to retrieve its value, which is
3483 -- part of the enclosing aggregate. Assoc_List provides the discriminant
3484 -- associations of the current type or of some enclosing record.
3486 function Discriminant_Present (Input_Discr : Entity_Id) return Boolean;
3487 -- If aggregate N is a regular aggregate this routine will return True.
3488 -- Otherwise, if N is an extension aggregate, then Input_Discr denotes
3489 -- a discriminant whose value may already have been specified by N's
3490 -- ancestor part. This routine checks whether this is indeed the case
3491 -- and if so returns False, signaling that no value for Input_Discr
3492 -- should appear in N's aggregate part. Also, in this case, the routine
3493 -- appends to New_Assoc_List the discriminant value specified in the
3496 -- If the aggregate is in a context with expansion delayed, it will be
3497 -- reanalyzed. The inherited discriminant values must not be reinserted
3498 -- in the component list to prevent spurious errors, but they must be
3499 -- present on first analysis to build the proper subtype indications.
3500 -- The flag Inherited_Discriminant is used to prevent the re-insertion.
3502 function Find_Private_Ancestor (Typ : Entity_Id) return Entity_Id;
3503 -- AI05-0115: Find earlier ancestor in the derivation chain that is
3504 -- derived from private view Typ. Whether the aggregate is legal depends
3505 -- on the current visibility of the type as well as that of the parent
3511 Consider_Others_Choice : Boolean := False) return Node_Id;
3512 -- Given a record component stored in parameter Compon, this function
3513 -- returns its value as it appears in the list From, which is a list
3514 -- of N_Component_Association nodes.
3516 -- If no component association has a choice for the searched component,
3517 -- the value provided by the others choice is returned, if there is one,
3518 -- and Consider_Others_Choice is set to true. Otherwise Empty is
3519 -- returned. If there is more than one component association giving a
3520 -- value for the searched record component, an error message is emitted
3521 -- and the first found value is returned.
3523 -- If Consider_Others_Choice is set and the returned expression comes
3524 -- from the others choice, then Others_Etype is set as a side effect.
3525 -- An error message is emitted if the components taking their value from
3526 -- the others choice do not have same type.
3528 procedure Propagate_Discriminants
3530 Assoc_List : List_Id);
3531 -- Nested components may themselves be discriminated types constrained
3532 -- by outer discriminants, whose values must be captured before the
3533 -- aggregate is expanded into assignments.
3535 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Entity_Id);
3536 -- Analyzes and resolves expression Expr against the Etype of the
3537 -- Component. This routine also applies all appropriate checks to Expr.
3538 -- It finally saves a Expr in the newly created association list that
3539 -- will be attached to the final record aggregate. Note that if the
3540 -- Parent pointer of Expr is not set then Expr was produced with a
3541 -- New_Copy_Tree or some such.
3543 procedure Rewrite_Range (Root_Type : Entity_Id; Rge : Node_Id);
3544 -- Rewrite a range node Rge when its bounds refer to non-stored
3545 -- discriminants from Root_Type, to replace them with the stored
3546 -- discriminant values. This is required in GNATprove mode, and is
3547 -- adopted in all modes to avoid special-casing GNATprove mode.
3549 ---------------------
3550 -- Add_Association --
3551 ---------------------
3553 procedure Add_Association
3554 (Component : Entity_Id;
3556 Assoc_List : List_Id;
3557 Is_Box_Present : Boolean := False)
3559 Choice_List : constant List_Id := New_List;
3563 -- If this is a box association the expression is missing, so use the
3564 -- Sloc of the aggregate itself for the new association.
3566 if Present (Expr) then
3572 Append_To (Choice_List, New_Occurrence_Of (Component, Loc));
3574 Append_To (Assoc_List,
3575 Make_Component_Association (Loc,
3576 Choices => Choice_List,
3578 Box_Present => Is_Box_Present));
3579 end Add_Association;
3581 -----------------------------
3582 -- Add_Discriminant_Values --
3583 -----------------------------
3585 procedure Add_Discriminant_Values
3586 (New_Aggr : Node_Id;
3587 Assoc_List : List_Id)
3591 Discr_Elmt : Elmt_Id;
3592 Discr_Val : Node_Id;
3596 Discr := First_Discriminant (Etype (New_Aggr));
3597 Discr_Elmt := First_Elmt (Discriminant_Constraint (Etype (New_Aggr)));
3598 while Present (Discr_Elmt) loop
3599 Discr_Val := Node (Discr_Elmt);
3601 -- If the constraint is given by a discriminant then it is a
3602 -- discriminant of an enclosing record, and its value has already
3603 -- been placed in the association list.
3605 if Is_Entity_Name (Discr_Val)
3606 and then Ekind (Entity (Discr_Val)) = E_Discriminant
3608 Val := Entity (Discr_Val);
3610 Assoc := First (Assoc_List);
3611 while Present (Assoc) loop
3612 if Present (Entity (First (Choices (Assoc))))
3613 and then Entity (First (Choices (Assoc))) = Val
3615 Discr_Val := Expression (Assoc);
3624 (Discr, New_Copy_Tree (Discr_Val),
3625 Component_Associations (New_Aggr));
3627 -- If the discriminant constraint is a current instance, mark the
3628 -- current aggregate so that the self-reference can be expanded
3629 -- later. The constraint may refer to the subtype of aggregate, so
3630 -- use base type for comparison.
3632 if Nkind (Discr_Val) = N_Attribute_Reference
3633 and then Is_Entity_Name (Prefix (Discr_Val))
3634 and then Is_Type (Entity (Prefix (Discr_Val)))
3635 and then Base_Type (Etype (N)) = Entity (Prefix (Discr_Val))
3637 Set_Has_Self_Reference (N);
3640 Next_Elmt (Discr_Elmt);
3641 Next_Discriminant (Discr);
3643 end Add_Discriminant_Values;
3645 --------------------------
3646 -- Discriminant_Present --
3647 --------------------------
3649 function Discriminant_Present (Input_Discr : Entity_Id) return Boolean is
3650 Regular_Aggr : constant Boolean := Nkind (N) /= N_Extension_Aggregate;
3652 Ancestor_Is_Subtyp : Boolean;
3657 Ancestor_Typ : Entity_Id;
3658 Comp_Assoc : Node_Id;
3660 Discr_Expr : Node_Id;
3661 Discr_Val : Elmt_Id := No_Elmt;
3662 Orig_Discr : Entity_Id;
3665 if Regular_Aggr then
3669 -- Check whether inherited discriminant values have already been
3670 -- inserted in the aggregate. This will be the case if we are
3671 -- re-analyzing an aggregate whose expansion was delayed.
3673 if Present (Component_Associations (N)) then
3674 Comp_Assoc := First (Component_Associations (N));
3675 while Present (Comp_Assoc) loop
3676 if Inherited_Discriminant (Comp_Assoc) then
3684 Ancestor := Ancestor_Part (N);
3685 Ancestor_Typ := Etype (Ancestor);
3686 Loc := Sloc (Ancestor);
3688 -- For a private type with unknown discriminants, use the underlying
3689 -- record view if it is available.
3691 if Has_Unknown_Discriminants (Ancestor_Typ)
3692 and then Present (Full_View (Ancestor_Typ))
3693 and then Present (Underlying_Record_View (Full_View (Ancestor_Typ)))
3695 Ancestor_Typ := Underlying_Record_View (Full_View (Ancestor_Typ));
3698 Ancestor_Is_Subtyp :=
3699 Is_Entity_Name (Ancestor) and then Is_Type (Entity (Ancestor));
3701 -- If the ancestor part has no discriminants clearly N's aggregate
3702 -- part must provide a value for Discr.
3704 if not Has_Discriminants (Ancestor_Typ) then
3707 -- If the ancestor part is an unconstrained subtype mark then the
3708 -- Discr must be present in N's aggregate part.
3710 elsif Ancestor_Is_Subtyp
3711 and then not Is_Constrained (Entity (Ancestor))
3716 -- Now look to see if Discr was specified in the ancestor part
3718 if Ancestor_Is_Subtyp then
3720 First_Elmt (Discriminant_Constraint (Entity (Ancestor)));
3723 Orig_Discr := Original_Record_Component (Input_Discr);
3725 Discr := First_Discriminant (Ancestor_Typ);
3726 while Present (Discr) loop
3728 -- If Ancestor has already specified Disc value then insert its
3729 -- value in the final aggregate.
3731 if Original_Record_Component (Discr) = Orig_Discr then
3732 if Ancestor_Is_Subtyp then
3733 Discr_Expr := New_Copy_Tree (Node (Discr_Val));
3736 Make_Selected_Component (Loc,
3737 Prefix => Duplicate_Subexpr (Ancestor),
3738 Selector_Name => New_Occurrence_Of (Input_Discr, Loc));
3741 Resolve_Aggr_Expr (Discr_Expr, Input_Discr);
3742 Set_Inherited_Discriminant (Last (New_Assoc_List));
3746 Next_Discriminant (Discr);
3748 if Ancestor_Is_Subtyp then
3749 Next_Elmt (Discr_Val);
3754 end Discriminant_Present;
3756 ---------------------------
3757 -- Find_Private_Ancestor --
3758 ---------------------------
3760 function Find_Private_Ancestor (Typ : Entity_Id) return Entity_Id is
3766 if Has_Private_Ancestor (Par)
3767 and then not Has_Private_Ancestor (Etype (Base_Type (Par)))
3771 elsif not Is_Derived_Type (Par) then
3775 Par := Etype (Base_Type (Par));
3778 end Find_Private_Ancestor;
3787 Consider_Others_Choice : Boolean := False) return Node_Id
3789 Typ : constant Entity_Id := Etype (Compon);
3791 Expr : Node_Id := Empty;
3792 Selector_Name : Node_Id;
3795 Is_Box_Present := False;
3801 Assoc := First (From);
3802 while Present (Assoc) loop
3803 Selector_Name := First (Choices (Assoc));
3804 while Present (Selector_Name) loop
3805 if Nkind (Selector_Name) = N_Others_Choice then
3806 if Consider_Others_Choice and then No (Expr) then
3808 -- We need to duplicate the expression for each
3809 -- successive component covered by the others choice.
3810 -- This is redundant if the others_choice covers only
3811 -- one component (small optimization possible???), but
3812 -- indispensable otherwise, because each one must be
3813 -- expanded individually to preserve side effects.
3815 -- Ada 2005 (AI-287): In case of default initialization
3816 -- of components, we duplicate the corresponding default
3817 -- expression (from the record type declaration). The
3818 -- copy must carry the sloc of the association (not the
3819 -- original expression) to prevent spurious elaboration
3820 -- checks when the default includes function calls.
3822 if Box_Present (Assoc) then
3823 Others_Box := Others_Box + 1;
3824 Is_Box_Present := True;
3826 if Expander_Active then
3828 New_Copy_Tree_And_Copy_Dimensions
3829 (Expression (Parent (Compon)),
3830 New_Sloc => Sloc (Assoc));
3832 return Expression (Parent (Compon));
3836 if Present (Others_Etype)
3837 and then Base_Type (Others_Etype) /= Base_Type (Typ)
3839 -- If the components are of an anonymous access
3840 -- type they are distinct, but this is legal in
3841 -- Ada 2012 as long as designated types match.
3843 if (Ekind (Typ) = E_Anonymous_Access_Type
3844 or else Ekind (Typ) =
3845 E_Anonymous_Access_Subprogram_Type)
3846 and then Designated_Type (Typ) =
3847 Designated_Type (Others_Etype)
3852 ("components in OTHERS choice must have same "
3853 & "type", Selector_Name);
3857 Others_Etype := Typ;
3859 -- Copy the expression so that it is resolved
3860 -- independently for each component, This is needed
3861 -- for accessibility checks on compoents of anonymous
3862 -- access types, even in compile_only mode.
3864 if not Inside_A_Generic then
3866 New_Copy_Tree_And_Copy_Dimensions
3867 (Expression (Assoc));
3869 return Expression (Assoc);
3874 elsif Chars (Compon) = Chars (Selector_Name) then
3877 -- Ada 2005 (AI-231)
3879 if Ada_Version >= Ada_2005
3880 and then Known_Null (Expression (Assoc))
3882 Check_Can_Never_Be_Null (Compon, Expression (Assoc));
3885 -- We need to duplicate the expression when several
3886 -- components are grouped together with a "|" choice.
3887 -- For instance "filed1 | filed2 => Expr"
3889 -- Ada 2005 (AI-287)
3891 if Box_Present (Assoc) then
3892 Is_Box_Present := True;
3894 -- Duplicate the default expression of the component
3895 -- from the record type declaration, so a new copy
3896 -- can be attached to the association.
3898 -- Note that we always copy the default expression,
3899 -- even when the association has a single choice, in
3900 -- order to create a proper association for the
3901 -- expanded aggregate.
3903 -- Component may have no default, in which case the
3904 -- expression is empty and the component is default-
3905 -- initialized, but an association for the component
3906 -- exists, and it is not covered by an others clause.
3908 -- Scalar and private types have no initialization
3909 -- procedure, so they remain uninitialized. If the
3910 -- target of the aggregate is a constant this
3911 -- deserves a warning.
3913 if No (Expression (Parent (Compon)))
3914 and then not Has_Non_Null_Base_Init_Proc (Typ)
3915 and then not Has_Aspect (Typ, Aspect_Default_Value)
3916 and then not Is_Concurrent_Type (Typ)
3917 and then Nkind (Parent (N)) = N_Object_Declaration
3918 and then Constant_Present (Parent (N))
3920 Error_Msg_Node_2 := Typ;
3922 ("component&? of type& is uninitialized",
3923 Assoc, Selector_Name);
3925 -- An additional reminder if the component type
3926 -- is a generic formal.
3928 if Is_Generic_Type (Base_Type (Typ)) then
3930 ("\instance should provide actual type with "
3931 & "initialization for&", Assoc, Typ);
3936 New_Copy_Tree_And_Copy_Dimensions
3937 (Expression (Parent (Compon)));
3940 if Present (Next (Selector_Name)) then
3941 Expr := New_Copy_Tree_And_Copy_Dimensions
3942 (Expression (Assoc));
3944 Expr := Expression (Assoc);
3948 Generate_Reference (Compon, Selector_Name, 'm');
3952 ("more than one value supplied for &",
3953 Selector_Name, Compon);
3958 Next (Selector_Name);
3967 -----------------------------
3968 -- Propagate_Discriminants --
3969 -----------------------------
3971 procedure Propagate_Discriminants
3973 Assoc_List : List_Id)
3975 Loc : constant Source_Ptr := Sloc (N);
3977 Needs_Box : Boolean := False;
3979 procedure Process_Component (Comp : Entity_Id);
3980 -- Add one component with a box association to the inner aggregate,
3981 -- and recurse if component is itself composite.
3983 -----------------------
3984 -- Process_Component --
3985 -----------------------
3987 procedure Process_Component (Comp : Entity_Id) is
3988 T : constant Entity_Id := Etype (Comp);
3992 if Is_Record_Type (T) and then Has_Discriminants (T) then
3993 New_Aggr := Make_Aggregate (Loc, New_List, New_List);
3994 Set_Etype (New_Aggr, T);
3997 (Comp, New_Aggr, Component_Associations (Aggr));
3999 -- Collect discriminant values and recurse
4001 Add_Discriminant_Values (New_Aggr, Assoc_List);
4002 Propagate_Discriminants (New_Aggr, Assoc_List);
4007 end Process_Component;
4011 Aggr_Type : constant Entity_Id := Base_Type (Etype (Aggr));
4012 Components : constant Elist_Id := New_Elmt_List;
4013 Def_Node : constant Node_Id :=
4014 Type_Definition (Declaration_Node (Aggr_Type));
4017 Comp_Elmt : Elmt_Id;
4020 -- Start of processing for Propagate_Discriminants
4023 -- The component type may be a variant type. Collect the components
4024 -- that are ruled by the known values of the discriminants. Their
4025 -- values have already been inserted into the component list of the
4026 -- current aggregate.
4028 if Nkind (Def_Node) = N_Record_Definition
4029 and then Present (Component_List (Def_Node))
4030 and then Present (Variant_Part (Component_List (Def_Node)))
4032 Gather_Components (Aggr_Type,
4033 Component_List (Def_Node),
4034 Governed_By => Component_Associations (Aggr),
4036 Report_Errors => Errors);
4038 Comp_Elmt := First_Elmt (Components);
4039 while Present (Comp_Elmt) loop
4040 if Ekind (Node (Comp_Elmt)) /= E_Discriminant then
4041 Process_Component (Node (Comp_Elmt));
4044 Next_Elmt (Comp_Elmt);
4047 -- No variant part, iterate over all components
4050 Comp := First_Component (Etype (Aggr));
4051 while Present (Comp) loop
4052 Process_Component (Comp);
4053 Next_Component (Comp);
4058 Append_To (Component_Associations (Aggr),
4059 Make_Component_Association (Loc,
4060 Choices => New_List (Make_Others_Choice (Loc)),
4061 Expression => Empty,
4062 Box_Present => True));
4064 end Propagate_Discriminants;
4066 -----------------------
4067 -- Resolve_Aggr_Expr --
4068 -----------------------
4070 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Entity_Id) is
4071 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean;
4072 -- If the expression is an aggregate (possibly qualified) then its
4073 -- expansion is delayed until the enclosing aggregate is expanded
4074 -- into assignments. In that case, do not generate checks on the
4075 -- expression, because they will be generated later, and will other-
4076 -- wise force a copy (to remove side effects) that would leave a
4077 -- dynamic-sized aggregate in the code, something that gigi cannot
4080 ---------------------------
4081 -- Has_Expansion_Delayed --
4082 ---------------------------
4084 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean is
4087 (Nkind_In (Expr, N_Aggregate, N_Extension_Aggregate)
4088 and then Present (Etype (Expr))
4089 and then Is_Record_Type (Etype (Expr))
4090 and then Expansion_Delayed (Expr))
4092 (Nkind (Expr) = N_Qualified_Expression
4093 and then Has_Expansion_Delayed (Expression (Expr)));
4094 end Has_Expansion_Delayed;
4098 Expr_Type : Entity_Id := Empty;
4099 New_C : Entity_Id := Component;
4103 -- Set to True if the resolved Expr node needs to be relocated when
4104 -- attached to the newly created association list. This node need not
4105 -- be relocated if its parent pointer is not set. In fact in this
4106 -- case Expr is the output of a New_Copy_Tree call. If Relocate is
4107 -- True then we have analyzed the expression node in the original
4108 -- aggregate and hence it needs to be relocated when moved over to
4109 -- the new association list.
4111 -- Start of processing for Resolve_Aggr_Expr
4114 -- If the type of the component is elementary or the type of the
4115 -- aggregate does not contain discriminants, use the type of the
4116 -- component to resolve Expr.
4118 if Is_Elementary_Type (Etype (Component))
4119 or else not Has_Discriminants (Etype (N))
4121 Expr_Type := Etype (Component);
4123 -- Otherwise we have to pick up the new type of the component from
4124 -- the new constrained subtype of the aggregate. In fact components
4125 -- which are of a composite type might be constrained by a
4126 -- discriminant, and we want to resolve Expr against the subtype were
4127 -- all discriminant occurrences are replaced with their actual value.
4130 New_C := First_Component (Etype (N));
4131 while Present (New_C) loop
4132 if Chars (New_C) = Chars (Component) then
4133 Expr_Type := Etype (New_C);
4137 Next_Component (New_C);
4140 pragma Assert (Present (Expr_Type));
4142 -- For each range in an array type where a discriminant has been
4143 -- replaced with the constraint, check that this range is within
4144 -- the range of the base type. This checks is done in the init
4145 -- proc for regular objects, but has to be done here for
4146 -- aggregates since no init proc is called for them.
4148 if Is_Array_Type (Expr_Type) then
4151 -- Range of the current constrained index in the array
4153 Orig_Index : Node_Id := First_Index (Etype (Component));
4154 -- Range corresponding to the range Index above in the
4155 -- original unconstrained record type. The bounds of this
4156 -- range may be governed by discriminants.
4158 Unconstr_Index : Node_Id := First_Index (Etype (Expr_Type));
4159 -- Range corresponding to the range Index above for the
4160 -- unconstrained array type. This range is needed to apply
4164 Index := First_Index (Expr_Type);
4165 while Present (Index) loop
4166 if Depends_On_Discriminant (Orig_Index) then
4167 Apply_Range_Check (Index, Etype (Unconstr_Index));
4171 Next_Index (Orig_Index);
4172 Next_Index (Unconstr_Index);
4178 -- If the Parent pointer of Expr is not set, Expr is an expression
4179 -- duplicated by New_Tree_Copy (this happens for record aggregates
4180 -- that look like (Field1 | Filed2 => Expr) or (others => Expr)).
4181 -- Such a duplicated expression must be attached to the tree
4182 -- before analysis and resolution to enforce the rule that a tree
4183 -- fragment should never be analyzed or resolved unless it is
4184 -- attached to the current compilation unit.
4186 if No (Parent (Expr)) then
4187 Set_Parent (Expr, N);
4193 Analyze_And_Resolve (Expr, Expr_Type);
4194 Check_Expr_OK_In_Limited_Aggregate (Expr);
4195 Check_Non_Static_Context (Expr);
4196 Check_Unset_Reference (Expr);
4198 -- Check wrong use of class-wide types
4200 if Is_Class_Wide_Type (Etype (Expr)) then
4201 Error_Msg_N ("dynamically tagged expression not allowed", Expr);
4204 if not Has_Expansion_Delayed (Expr) then
4205 Aggregate_Constraint_Checks (Expr, Expr_Type);
4208 -- If an aggregate component has a type with predicates, an explicit
4209 -- predicate check must be applied, as for an assignment statement,
4210 -- because the aggegate might not be expanded into individual
4211 -- component assignments.
4213 if Has_Predicates (Expr_Type)
4214 and then Analyzed (Expr)
4216 Apply_Predicate_Check (Expr, Expr_Type);
4219 if Raises_Constraint_Error (Expr) then
4220 Set_Raises_Constraint_Error (N);
4223 -- If the expression has been marked as requiring a range check, then
4224 -- generate it here. It's a bit odd to be generating such checks in
4225 -- the analyzer, but harmless since Generate_Range_Check does nothing
4226 -- (other than making sure Do_Range_Check is set) if the expander is
4229 if Do_Range_Check (Expr) then
4230 Generate_Range_Check (Expr, Expr_Type, CE_Range_Check_Failed);
4233 -- Add association Component => Expr if the caller requests it
4236 New_Expr := Relocate_Node (Expr);
4238 -- Since New_Expr is not gonna be analyzed later on, we need to
4239 -- propagate here the dimensions form Expr to New_Expr.
4241 Copy_Dimensions (Expr, New_Expr);
4247 Add_Association (New_C, New_Expr, New_Assoc_List);
4248 end Resolve_Aggr_Expr;
4254 procedure Rewrite_Range (Root_Type : Entity_Id; Rge : Node_Id) is
4255 procedure Rewrite_Bound
4258 Expr_Disc : Node_Id);
4259 -- Rewrite a bound of the range Bound, when it is equal to the
4260 -- non-stored discriminant Disc, into the stored discriminant
4267 procedure Rewrite_Bound
4270 Expr_Disc : Node_Id)
4273 if Nkind (Bound) /= N_Identifier then
4277 -- We expect either the discriminant or the discriminal
4279 if Entity (Bound) = Disc
4280 or else (Ekind (Entity (Bound)) = E_In_Parameter
4281 and then Discriminal_Link (Entity (Bound)) = Disc)
4283 Rewrite (Bound, New_Copy_Tree (Expr_Disc));
4289 Low, High : Node_Id;
4291 Expr_Disc : Elmt_Id;
4293 -- Start of processing for Rewrite_Range
4296 if Has_Discriminants (Root_Type) and then Nkind (Rge) = N_Range then
4297 Low := Low_Bound (Rge);
4298 High := High_Bound (Rge);
4300 Disc := First_Discriminant (Root_Type);
4301 Expr_Disc := First_Elmt (Stored_Constraint (Etype (N)));
4302 while Present (Disc) loop
4303 Rewrite_Bound (Low, Disc, Node (Expr_Disc));
4304 Rewrite_Bound (High, Disc, Node (Expr_Disc));
4305 Next_Discriminant (Disc);
4306 Next_Elmt (Expr_Disc);
4313 Components : constant Elist_Id := New_Elmt_List;
4314 -- Components is the list of the record components whose value must be
4315 -- provided in the aggregate. This list does include discriminants.
4317 Component : Entity_Id;
4318 Component_Elmt : Elmt_Id;
4320 Positional_Expr : Node_Id;
4322 -- Start of processing for Resolve_Record_Aggregate
4325 -- A record aggregate is restricted in SPARK:
4327 -- Each named association can have only a single choice.
4328 -- OTHERS cannot be used.
4329 -- Positional and named associations cannot be mixed.
4331 if Present (Component_Associations (N))
4332 and then Present (First (Component_Associations (N)))
4334 if Present (Expressions (N)) then
4335 Check_SPARK_05_Restriction
4336 ("named association cannot follow positional one",
4337 First (Choices (First (Component_Associations (N)))));
4344 Assoc := First (Component_Associations (N));
4345 while Present (Assoc) loop
4346 if Nkind (Assoc) = N_Iterated_Component_Association then
4348 ("iterated component association can only appear in an "
4349 & "array aggregate", N);
4350 raise Unrecoverable_Error;
4353 if List_Length (Choices (Assoc)) > 1 then
4354 Check_SPARK_05_Restriction
4355 ("component association in record aggregate must "
4356 & "contain a single choice", Assoc);
4359 if Nkind (First (Choices (Assoc))) = N_Others_Choice then
4360 Check_SPARK_05_Restriction
4361 ("record aggregate cannot contain OTHERS", Assoc);
4365 Assoc := Next (Assoc);
4370 -- We may end up calling Duplicate_Subexpr on expressions that are
4371 -- attached to New_Assoc_List. For this reason we need to attach it
4372 -- to the tree by setting its parent pointer to N. This parent point
4373 -- will change in STEP 8 below.
4375 Set_Parent (New_Assoc_List, N);
4377 -- STEP 1: abstract type and null record verification
4379 if Is_Abstract_Type (Typ) then
4380 Error_Msg_N ("type of aggregate cannot be abstract", N);
4383 if No (First_Entity (Typ)) and then Null_Record_Present (N) then
4387 elsif Present (First_Entity (Typ))
4388 and then Null_Record_Present (N)
4389 and then not Is_Tagged_Type (Typ)
4391 Error_Msg_N ("record aggregate cannot be null", N);
4394 -- If the type has no components, then the aggregate should either
4395 -- have "null record", or in Ada 2005 it could instead have a single
4396 -- component association given by "others => <>". For Ada 95 we flag an
4397 -- error at this point, but for Ada 2005 we proceed with checking the
4398 -- associations below, which will catch the case where it's not an
4399 -- aggregate with "others => <>". Note that the legality of a <>
4400 -- aggregate for a null record type was established by AI05-016.
4402 elsif No (First_Entity (Typ))
4403 and then Ada_Version < Ada_2005
4405 Error_Msg_N ("record aggregate must be null", N);
4409 -- STEP 2: Verify aggregate structure
4413 Bad_Aggregate : Boolean := False;
4414 Selector_Name : Node_Id;
4417 if Present (Component_Associations (N)) then
4418 Assoc := First (Component_Associations (N));
4423 while Present (Assoc) loop
4424 Selector_Name := First (Choices (Assoc));
4425 while Present (Selector_Name) loop
4426 if Nkind (Selector_Name) = N_Identifier then
4429 elsif Nkind (Selector_Name) = N_Others_Choice then
4430 if Selector_Name /= First (Choices (Assoc))
4431 or else Present (Next (Selector_Name))
4434 ("OTHERS must appear alone in a choice list",
4438 elsif Present (Next (Assoc)) then
4440 ("OTHERS must appear last in an aggregate",
4444 -- (Ada 2005): If this is an association with a box,
4445 -- indicate that the association need not represent
4448 elsif Box_Present (Assoc) then
4455 ("selector name should be identifier or OTHERS",
4457 Bad_Aggregate := True;
4460 Next (Selector_Name);
4466 if Bad_Aggregate then
4471 -- STEP 3: Find discriminant Values
4474 Discrim : Entity_Id;
4475 Missing_Discriminants : Boolean := False;
4478 if Present (Expressions (N)) then
4479 Positional_Expr := First (Expressions (N));
4481 Positional_Expr := Empty;
4484 -- AI05-0115: if the ancestor part is a subtype mark, the ancestor
4485 -- must not have unknown discriminants.
4487 if Is_Derived_Type (Typ)
4488 and then Has_Unknown_Discriminants (Root_Type (Typ))
4489 and then Nkind (N) /= N_Extension_Aggregate
4492 ("aggregate not available for type& whose ancestor "
4493 & "has unknown discriminants ", N, Typ);
4496 if Has_Unknown_Discriminants (Typ)
4497 and then Present (Underlying_Record_View (Typ))
4499 Discrim := First_Discriminant (Underlying_Record_View (Typ));
4500 elsif Has_Discriminants (Typ) then
4501 Discrim := First_Discriminant (Typ);
4506 -- First find the discriminant values in the positional components
4508 while Present (Discrim) and then Present (Positional_Expr) loop
4509 if Discriminant_Present (Discrim) then
4510 Resolve_Aggr_Expr (Positional_Expr, Discrim);
4512 -- Ada 2005 (AI-231)
4514 if Ada_Version >= Ada_2005
4515 and then Known_Null (Positional_Expr)
4517 Check_Can_Never_Be_Null (Discrim, Positional_Expr);
4520 Next (Positional_Expr);
4523 if Present (Get_Value (Discrim, Component_Associations (N))) then
4525 ("more than one value supplied for discriminant&",
4529 Next_Discriminant (Discrim);
4532 -- Find remaining discriminant values if any among named components
4534 while Present (Discrim) loop
4535 Expr := Get_Value (Discrim, Component_Associations (N), True);
4537 if not Discriminant_Present (Discrim) then
4538 if Present (Expr) then
4540 ("more than one value supplied for discriminant &",
4544 elsif No (Expr) then
4546 ("no value supplied for discriminant &", N, Discrim);
4547 Missing_Discriminants := True;
4550 Resolve_Aggr_Expr (Expr, Discrim);
4553 Next_Discriminant (Discrim);
4556 if Missing_Discriminants then
4560 -- At this point and until the beginning of STEP 6, New_Assoc_List
4561 -- contains only the discriminants and their values.
4565 -- STEP 4: Set the Etype of the record aggregate
4567 -- ??? This code is pretty much a copy of Sem_Ch3.Build_Subtype. That
4568 -- routine should really be exported in sem_util or some such and used
4569 -- in sem_ch3 and here rather than have a copy of the code which is a
4570 -- maintenance nightmare.
4572 -- ??? Performance WARNING. The current implementation creates a new
4573 -- itype for all aggregates whose base type is discriminated. This means
4574 -- that for record aggregates nested inside an array aggregate we will
4575 -- create a new itype for each record aggregate if the array component
4576 -- type has discriminants. For large aggregates this may be a problem.
4577 -- What should be done in this case is to reuse itypes as much as
4580 if Has_Discriminants (Typ)
4581 or else (Has_Unknown_Discriminants (Typ)
4582 and then Present (Underlying_Record_View (Typ)))
4584 Build_Constrained_Itype : declare
4585 Constrs : constant List_Id := New_List;
4586 Loc : constant Source_Ptr := Sloc (N);
4589 New_Assoc : Node_Id;
4590 Subtyp_Decl : Node_Id;
4593 New_Assoc := First (New_Assoc_List);
4594 while Present (New_Assoc) loop
4595 Append_To (Constrs, Duplicate_Subexpr (Expression (New_Assoc)));
4599 if Has_Unknown_Discriminants (Typ)
4600 and then Present (Underlying_Record_View (Typ))
4603 Make_Subtype_Indication (Loc,
4605 New_Occurrence_Of (Underlying_Record_View (Typ), Loc),
4607 Make_Index_Or_Discriminant_Constraint (Loc,
4608 Constraints => Constrs));
4611 Make_Subtype_Indication (Loc,
4613 New_Occurrence_Of (Base_Type (Typ), Loc),
4615 Make_Index_Or_Discriminant_Constraint (Loc,
4616 Constraints => Constrs));
4619 Def_Id := Create_Itype (Ekind (Typ), N);
4622 Make_Subtype_Declaration (Loc,
4623 Defining_Identifier => Def_Id,
4624 Subtype_Indication => Indic);
4625 Set_Parent (Subtyp_Decl, Parent (N));
4627 -- Itypes must be analyzed with checks off (see itypes.ads)
4629 Analyze (Subtyp_Decl, Suppress => All_Checks);
4631 Set_Etype (N, Def_Id);
4632 Check_Static_Discriminated_Subtype
4633 (Def_Id, Expression (First (New_Assoc_List)));
4634 end Build_Constrained_Itype;
4640 -- STEP 5: Get remaining components according to discriminant values
4644 Errors_Found : Boolean := False;
4645 Record_Def : Node_Id;
4646 Parent_Typ : Entity_Id;
4647 Parent_Typ_List : Elist_Id;
4648 Parent_Elmt : Elmt_Id;
4649 Root_Typ : Entity_Id;
4652 if Is_Derived_Type (Typ) and then Is_Tagged_Type (Typ) then
4653 Parent_Typ_List := New_Elmt_List;
4655 -- If this is an extension aggregate, the component list must
4656 -- include all components that are not in the given ancestor type.
4657 -- Otherwise, the component list must include components of all
4658 -- ancestors, starting with the root.
4660 if Nkind (N) = N_Extension_Aggregate then
4661 Root_Typ := Base_Type (Etype (Ancestor_Part (N)));
4664 -- AI05-0115: check legality of aggregate for type with a
4665 -- private ancestor.
4667 Root_Typ := Root_Type (Typ);
4668 if Has_Private_Ancestor (Typ) then
4670 Ancestor : constant Entity_Id :=
4671 Find_Private_Ancestor (Typ);
4672 Ancestor_Unit : constant Entity_Id :=
4674 (Get_Source_Unit (Ancestor));
4675 Parent_Unit : constant Entity_Id :=
4676 Cunit_Entity (Get_Source_Unit
4677 (Base_Type (Etype (Ancestor))));
4679 -- Check whether we are in a scope that has full view
4680 -- over the private ancestor and its parent. This can
4681 -- only happen if the derivation takes place in a child
4682 -- unit of the unit that declares the parent, and we are
4683 -- in the private part or body of that child unit, else
4684 -- the aggregate is illegal.
4686 if Is_Child_Unit (Ancestor_Unit)
4687 and then Scope (Ancestor_Unit) = Parent_Unit
4688 and then In_Open_Scopes (Scope (Ancestor))
4690 (In_Private_Part (Scope (Ancestor))
4691 or else In_Package_Body (Scope (Ancestor)))
4697 ("type of aggregate has private ancestor&!",
4699 Error_Msg_N ("must use extension aggregate!", N);
4705 Dnode := Declaration_Node (Base_Type (Root_Typ));
4707 -- If we don't get a full declaration, then we have some error
4708 -- which will get signalled later so skip this part. Otherwise
4709 -- gather components of root that apply to the aggregate type.
4710 -- We use the base type in case there is an applicable stored
4711 -- constraint that renames the discriminants of the root.
4713 if Nkind (Dnode) = N_Full_Type_Declaration then
4714 Record_Def := Type_Definition (Dnode);
4717 Component_List (Record_Def),
4718 Governed_By => New_Assoc_List,
4720 Report_Errors => Errors_Found);
4722 if Errors_Found then
4724 ("discriminant controlling variant part is not static",
4731 Parent_Typ := Base_Type (Typ);
4732 while Parent_Typ /= Root_Typ loop
4733 Prepend_Elmt (Parent_Typ, To => Parent_Typ_List);
4734 Parent_Typ := Etype (Parent_Typ);
4736 if Nkind (Parent (Base_Type (Parent_Typ))) =
4737 N_Private_Type_Declaration
4738 or else Nkind (Parent (Base_Type (Parent_Typ))) =
4739 N_Private_Extension_Declaration
4741 if Nkind (N) /= N_Extension_Aggregate then
4743 ("type of aggregate has private ancestor&!",
4745 Error_Msg_N ("must use extension aggregate!", N);
4748 elsif Parent_Typ /= Root_Typ then
4750 ("ancestor part of aggregate must be private type&",
4751 Ancestor_Part (N), Parent_Typ);
4755 -- The current view of ancestor part may be a private type,
4756 -- while the context type is always non-private.
4758 elsif Is_Private_Type (Root_Typ)
4759 and then Present (Full_View (Root_Typ))
4760 and then Nkind (N) = N_Extension_Aggregate
4762 exit when Base_Type (Full_View (Root_Typ)) = Parent_Typ;
4766 -- Now collect components from all other ancestors, beginning
4767 -- with the current type. If the type has unknown discriminants
4768 -- use the component list of the Underlying_Record_View, which
4769 -- needs to be used for the subsequent expansion of the aggregate
4770 -- into assignments.
4772 Parent_Elmt := First_Elmt (Parent_Typ_List);
4773 while Present (Parent_Elmt) loop
4774 Parent_Typ := Node (Parent_Elmt);
4776 if Has_Unknown_Discriminants (Parent_Typ)
4777 and then Present (Underlying_Record_View (Typ))
4779 Parent_Typ := Underlying_Record_View (Parent_Typ);
4782 Record_Def := Type_Definition (Parent (Base_Type (Parent_Typ)));
4783 Gather_Components (Empty,
4784 Component_List (Record_Extension_Part (Record_Def)),
4785 Governed_By => New_Assoc_List,
4787 Report_Errors => Errors_Found);
4789 Next_Elmt (Parent_Elmt);
4792 -- Typ is not a derived tagged type
4795 Record_Def := Type_Definition (Parent (Base_Type (Typ)));
4797 if Null_Present (Record_Def) then
4800 elsif not Has_Unknown_Discriminants (Typ) then
4803 Component_List (Record_Def),
4804 Governed_By => New_Assoc_List,
4806 Report_Errors => Errors_Found);
4810 (Base_Type (Underlying_Record_View (Typ)),
4811 Component_List (Record_Def),
4812 Governed_By => New_Assoc_List,
4814 Report_Errors => Errors_Found);
4818 if Errors_Found then
4823 -- STEP 6: Find component Values
4826 Component_Elmt := First_Elmt (Components);
4828 -- First scan the remaining positional associations in the aggregate.
4829 -- Remember that at this point Positional_Expr contains the current
4830 -- positional association if any is left after looking for discriminant
4831 -- values in step 3.
4833 while Present (Positional_Expr) and then Present (Component_Elmt) loop
4834 Component := Node (Component_Elmt);
4835 Resolve_Aggr_Expr (Positional_Expr, Component);
4837 -- Ada 2005 (AI-231)
4839 if Ada_Version >= Ada_2005 and then Known_Null (Positional_Expr) then
4840 Check_Can_Never_Be_Null (Component, Positional_Expr);
4843 if Present (Get_Value (Component, Component_Associations (N))) then
4845 ("more than one value supplied for Component &", N, Component);
4848 Next (Positional_Expr);
4849 Next_Elmt (Component_Elmt);
4852 if Present (Positional_Expr) then
4854 ("too many components for record aggregate", Positional_Expr);
4857 -- Now scan for the named arguments of the aggregate
4859 while Present (Component_Elmt) loop
4860 Component := Node (Component_Elmt);
4861 Expr := Get_Value (Component, Component_Associations (N), True);
4863 -- Note: The previous call to Get_Value sets the value of the
4864 -- variable Is_Box_Present.
4866 -- Ada 2005 (AI-287): Handle components with default initialization.
4867 -- Note: This feature was originally added to Ada 2005 for limited
4868 -- but it was finally allowed with any type.
4870 if Is_Box_Present then
4871 Check_Box_Component : declare
4872 Ctyp : constant Entity_Id := Etype (Component);
4875 -- If there is a default expression for the aggregate, copy
4876 -- it into a new association. This copy must modify the scopes
4877 -- of internal types that may be attached to the expression
4878 -- (e.g. index subtypes of arrays) because in general the type
4879 -- declaration and the aggregate appear in different scopes,
4880 -- and the backend requires the scope of the type to match the
4881 -- point at which it is elaborated.
4883 -- If the component has an initialization procedure (IP) we
4884 -- pass the component to the expander, which will generate
4885 -- the call to such IP.
4887 -- If the component has discriminants, their values must
4888 -- be taken from their subtype. This is indispensable for
4889 -- constraints that are given by the current instance of an
4890 -- enclosing type, to allow the expansion of the aggregate to
4891 -- replace the reference to the current instance by the target
4892 -- object of the aggregate.
4894 if Present (Parent (Component))
4895 and then Nkind (Parent (Component)) = N_Component_Declaration
4896 and then Present (Expression (Parent (Component)))
4899 New_Copy_Tree_And_Copy_Dimensions
4900 (Expression (Parent (Component)),
4901 New_Scope => Current_Scope,
4902 New_Sloc => Sloc (N));
4904 -- As the type of the copied default expression may refer
4905 -- to discriminants of the record type declaration, these
4906 -- non-stored discriminants need to be rewritten into stored
4907 -- discriminant values for the aggregate. This is required
4908 -- in GNATprove mode, and is adopted in all modes to avoid
4909 -- special-casing GNATprove mode.
4911 if Is_Array_Type (Etype (Expr)) then
4913 Rec_Typ : constant Entity_Id := Scope (Component);
4914 -- Root record type whose discriminants may be used as
4915 -- bounds in range nodes.
4922 -- Rewrite the range nodes occurring in the indexes
4925 Index := First_Index (Etype (Expr));
4926 while Present (Index) loop
4927 Rewrite_Range (Rec_Typ, Index);
4929 (Rec_Typ, Scalar_Range (Etype (Index)));
4934 -- Rewrite the range nodes occurring as aggregate
4935 -- bounds and component associations.
4937 if Nkind (Expr) = N_Aggregate then
4938 if Present (Aggregate_Bounds (Expr)) then
4939 Rewrite_Range (Rec_Typ, Aggregate_Bounds (Expr));
4942 if Present (Component_Associations (Expr)) then
4943 Assoc := First (Component_Associations (Expr));
4944 while Present (Assoc) loop
4945 Choice := First (Choices (Assoc));
4946 while Present (Choice) loop
4947 Rewrite_Range (Rec_Typ, Choice);
4960 (Component => Component,
4962 Assoc_List => New_Assoc_List);
4963 Set_Has_Self_Reference (N);
4965 -- A box-defaulted access component gets the value null. Also
4966 -- included are components of private types whose underlying
4967 -- type is an access type. In either case set the type of the
4968 -- literal, for subsequent use in semantic checks.
4970 elsif Present (Underlying_Type (Ctyp))
4971 and then Is_Access_Type (Underlying_Type (Ctyp))
4973 -- If the component's type is private with an access type as
4974 -- its underlying type then we have to create an unchecked
4975 -- conversion to satisfy type checking.
4977 if Is_Private_Type (Ctyp) then
4979 Qual_Null : constant Node_Id :=
4980 Make_Qualified_Expression (Sloc (N),
4983 (Underlying_Type (Ctyp), Sloc (N)),
4984 Expression => Make_Null (Sloc (N)));
4986 Convert_Null : constant Node_Id :=
4987 Unchecked_Convert_To
4991 Analyze_And_Resolve (Convert_Null, Ctyp);
4993 (Component => Component,
4994 Expr => Convert_Null,
4995 Assoc_List => New_Assoc_List);
4998 -- Otherwise the component type is non-private
5001 Expr := Make_Null (Sloc (N));
5002 Set_Etype (Expr, Ctyp);
5005 (Component => Component,
5007 Assoc_List => New_Assoc_List);
5010 -- Ada 2012: If component is scalar with default value, use it
5012 elsif Is_Scalar_Type (Ctyp)
5013 and then Has_Default_Aspect (Ctyp)
5016 (Component => Component,
5018 Default_Aspect_Value
5019 (First_Subtype (Underlying_Type (Ctyp))),
5020 Assoc_List => New_Assoc_List);
5022 elsif Has_Non_Null_Base_Init_Proc (Ctyp)
5023 or else not Expander_Active
5025 if Is_Record_Type (Ctyp)
5026 and then Has_Discriminants (Ctyp)
5027 and then not Is_Private_Type (Ctyp)
5029 -- We build a partially initialized aggregate with the
5030 -- values of the discriminants and box initialization
5031 -- for the rest, if other components are present.
5033 -- The type of the aggregate is the known subtype of
5034 -- the component. The capture of discriminants must be
5035 -- recursive because subcomponents may be constrained
5036 -- (transitively) by discriminants of enclosing types.
5037 -- For a private type with discriminants, a call to the
5038 -- initialization procedure will be generated, and no
5039 -- subaggregate is needed.
5041 Capture_Discriminants : declare
5042 Loc : constant Source_Ptr := Sloc (N);
5046 Expr := Make_Aggregate (Loc, New_List, New_List);
5047 Set_Etype (Expr, Ctyp);
5049 -- If the enclosing type has discriminants, they have
5050 -- been collected in the aggregate earlier, and they
5051 -- may appear as constraints of subcomponents.
5053 -- Similarly if this component has discriminants, they
5054 -- might in turn be propagated to their components.
5056 if Has_Discriminants (Typ) then
5057 Add_Discriminant_Values (Expr, New_Assoc_List);
5058 Propagate_Discriminants (Expr, New_Assoc_List);
5060 elsif Has_Discriminants (Ctyp) then
5061 Add_Discriminant_Values
5062 (Expr, Component_Associations (Expr));
5063 Propagate_Discriminants
5064 (Expr, Component_Associations (Expr));
5071 -- If the type has additional components, create
5072 -- an OTHERS box association for them.
5074 Comp := First_Component (Ctyp);
5075 while Present (Comp) loop
5076 if Ekind (Comp) = E_Component then
5077 if not Is_Record_Type (Etype (Comp)) then
5079 (Component_Associations (Expr),
5080 Make_Component_Association (Loc,
5083 Make_Others_Choice (Loc)),
5084 Expression => Empty,
5085 Box_Present => True));
5091 Next_Component (Comp);
5097 (Component => Component,
5099 Assoc_List => New_Assoc_List);
5100 end Capture_Discriminants;
5102 -- Otherwise the component type is not a record, or it has
5103 -- not discriminants, or it is private.
5107 (Component => Component,
5109 Assoc_List => New_Assoc_List,
5110 Is_Box_Present => True);
5113 -- Otherwise we only need to resolve the expression if the
5114 -- component has partially initialized values (required to
5115 -- expand the corresponding assignments and run-time checks).
5117 elsif Present (Expr)
5118 and then Is_Partially_Initialized_Type (Ctyp)
5120 Resolve_Aggr_Expr (Expr, Component);
5122 end Check_Box_Component;
5124 elsif No (Expr) then
5126 -- Ignore hidden components associated with the position of the
5127 -- interface tags: these are initialized dynamically.
5129 if not Present (Related_Type (Component)) then
5131 ("no value supplied for component &!", N, Component);
5135 Resolve_Aggr_Expr (Expr, Component);
5138 Next_Elmt (Component_Elmt);
5141 -- STEP 7: check for invalid components + check type in choice list
5145 New_Assoc : Node_Id;
5151 -- Type of first component in choice list
5154 if Present (Component_Associations (N)) then
5155 Assoc := First (Component_Associations (N));
5160 Verification : while Present (Assoc) loop
5161 Selectr := First (Choices (Assoc));
5164 if Nkind (Selectr) = N_Others_Choice then
5166 -- Ada 2005 (AI-287): others choice may have expression or box
5168 if No (Others_Etype) and then Others_Box = 0 then
5170 ("OTHERS must represent at least one component", Selectr);
5172 elsif Others_Box = 1 and then Warn_On_Redundant_Constructs then
5173 Error_Msg_N ("others choice is redundant?", Box_Node);
5175 ("\previous choices cover all components?", Box_Node);
5181 while Present (Selectr) loop
5182 New_Assoc := First (New_Assoc_List);
5183 while Present (New_Assoc) loop
5184 Component := First (Choices (New_Assoc));
5186 if Chars (Selectr) = Chars (Component) then
5188 Check_Identifier (Selectr, Entity (Component));
5197 -- If no association, this is not a legal component of the type
5198 -- in question, unless its association is provided with a box.
5200 if No (New_Assoc) then
5201 if Box_Present (Parent (Selectr)) then
5203 -- This may still be a bogus component with a box. Scan
5204 -- list of components to verify that a component with
5205 -- that name exists.
5211 C := First_Component (Typ);
5212 while Present (C) loop
5213 if Chars (C) = Chars (Selectr) then
5215 -- If the context is an extension aggregate,
5216 -- the component must not be inherited from
5217 -- the ancestor part of the aggregate.
5219 if Nkind (N) /= N_Extension_Aggregate
5221 Scope (Original_Record_Component (C)) /=
5222 Etype (Ancestor_Part (N))
5232 Error_Msg_Node_2 := Typ;
5233 Error_Msg_N ("& is not a component of}", Selectr);
5237 elsif Chars (Selectr) /= Name_uTag
5238 and then Chars (Selectr) /= Name_uParent
5240 if not Has_Discriminants (Typ) then
5241 Error_Msg_Node_2 := Typ;
5242 Error_Msg_N ("& is not a component of}", Selectr);
5245 ("& is not a component of the aggregate subtype",
5249 Check_Misspelled_Component (Components, Selectr);
5252 elsif No (Typech) then
5253 Typech := Base_Type (Etype (Component));
5255 -- AI05-0199: In Ada 2012, several components of anonymous
5256 -- access types can appear in a choice list, as long as the
5257 -- designated types match.
5259 elsif Typech /= Base_Type (Etype (Component)) then
5260 if Ada_Version >= Ada_2012
5261 and then Ekind (Typech) = E_Anonymous_Access_Type
5263 Ekind (Etype (Component)) = E_Anonymous_Access_Type
5264 and then Base_Type (Designated_Type (Typech)) =
5265 Base_Type (Designated_Type (Etype (Component)))
5267 Subtypes_Statically_Match (Typech, (Etype (Component)))
5271 elsif not Box_Present (Parent (Selectr)) then
5273 ("components in choice list must have same type",
5282 end loop Verification;
5285 -- STEP 8: replace the original aggregate
5288 New_Aggregate : constant Node_Id := New_Copy (N);
5291 Set_Expressions (New_Aggregate, No_List);
5292 Set_Etype (New_Aggregate, Etype (N));
5293 Set_Component_Associations (New_Aggregate, New_Assoc_List);
5294 Set_Check_Actuals (New_Aggregate, Check_Actuals (N));
5296 Rewrite (N, New_Aggregate);
5299 -- Check the dimensions of the components in the record aggregate
5301 Analyze_Dimension_Extension_Or_Record_Aggregate (N);
5302 end Resolve_Record_Aggregate;
5304 -----------------------------
5305 -- Check_Can_Never_Be_Null --
5306 -----------------------------
5308 procedure Check_Can_Never_Be_Null (Typ : Entity_Id; Expr : Node_Id) is
5309 Comp_Typ : Entity_Id;
5313 (Ada_Version >= Ada_2005
5314 and then Present (Expr)
5315 and then Known_Null (Expr));
5318 when E_Array_Type =>
5319 Comp_Typ := Component_Type (Typ);
5324 Comp_Typ := Etype (Typ);
5330 if Can_Never_Be_Null (Comp_Typ) then
5332 -- Here we know we have a constraint error. Note that we do not use
5333 -- Apply_Compile_Time_Constraint_Error here to the Expr, which might
5334 -- seem the more natural approach. That's because in some cases the
5335 -- components are rewritten, and the replacement would be missed.
5336 -- We do not mark the whole aggregate as raising a constraint error,
5337 -- because the association may be a null array range.
5340 ("(Ada 2005) null not allowed in null-excluding component??", Expr);
5342 ("\Constraint_Error will be raised at run time??", Expr);
5345 Make_Raise_Constraint_Error
5346 (Sloc (Expr), Reason => CE_Access_Check_Failed));
5347 Set_Etype (Expr, Comp_Typ);
5348 Set_Analyzed (Expr);
5350 end Check_Can_Never_Be_Null;
5352 ---------------------
5353 -- Sort_Case_Table --
5354 ---------------------
5356 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is
5357 U : constant Int := Case_Table'Last;
5365 T := Case_Table (K + 1);
5369 and then Expr_Value (Case_Table (J - 1).Lo) > Expr_Value (T.Lo)
5371 Case_Table (J) := Case_Table (J - 1);
5375 Case_Table (J) := T;
5378 end Sort_Case_Table;