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_Ch5; use Sem_Ch5;
52 with Sem_Ch8; use Sem_Ch8;
53 with Sem_Ch13; use Sem_Ch13;
54 with Sem_Dim; use Sem_Dim;
55 with Sem_Eval; use Sem_Eval;
56 with Sem_Res; use Sem_Res;
57 with Sem_Util; use Sem_Util;
58 with Sem_Type; use Sem_Type;
59 with Sem_Warn; use Sem_Warn;
60 with Sinfo; use Sinfo;
61 with Snames; use Snames;
62 with Stringt; use Stringt;
63 with Stand; use Stand;
64 with Style; use Style;
65 with Targparm; use Targparm;
66 with Tbuild; use Tbuild;
67 with Ttypes; use Ttypes;
68 with Uintp; use Uintp;
70 package body Sem_Aggr is
72 type Case_Bounds is record
74 -- Low bound of choice. Once we sort the Case_Table, then entries
75 -- will be in order of ascending Choice_Lo values.
78 -- High Bound of choice. The sort does not pay any attention to the
79 -- high bound, so choices 1 .. 4 and 1 .. 5 could be in either order.
82 -- If there are duplicates or missing entries, then in the sorted
83 -- table, this records the highest value among Choice_Hi values
84 -- seen so far, including this entry.
87 -- The node of the choice
90 type Case_Table_Type is array (Pos range <>) of Case_Bounds;
91 -- Table type used by Check_Case_Choices procedure
93 -----------------------
94 -- Local Subprograms --
95 -----------------------
97 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type);
98 -- Sort the Case Table using the Lower Bound of each Choice as the key. A
99 -- simple insertion sort is used since the choices in a case statement will
100 -- usually be in near sorted order.
102 procedure Check_Can_Never_Be_Null (Typ : Entity_Id; Expr : Node_Id);
103 -- Ada 2005 (AI-231): Check bad usage of null for a component for which
104 -- null exclusion (NOT NULL) is specified. Typ can be an E_Array_Type for
105 -- the array case (the component type of the array will be used) or an
106 -- E_Component/E_Discriminant entity in the record case, in which case the
107 -- type of the component will be used for the test. If Typ is any other
108 -- kind of entity, the call is ignored. Expr is the component node in the
109 -- aggregate which is known to have a null value. A warning message will be
110 -- issued if the component is null excluding.
112 -- It would be better to pass the proper type for Typ ???
114 procedure Check_Expr_OK_In_Limited_Aggregate (Expr : Node_Id);
115 -- Check that Expr is either not limited or else is one of the cases of
116 -- expressions allowed for a limited component association (namely, an
117 -- aggregate, function call, or <> notation). Report error for violations.
118 -- Expression is also OK in an instance or inlining context, because we
119 -- have already preanalyzed and it is known to be type correct.
121 ------------------------------------------------------
122 -- Subprograms used for RECORD AGGREGATE Processing --
123 ------------------------------------------------------
125 procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id);
126 -- This procedure performs all the semantic checks required for record
127 -- aggregates. Note that for aggregates analysis and resolution go
128 -- hand in hand. Aggregate analysis has been delayed up to here and
129 -- it is done while resolving the aggregate.
131 -- N is the N_Aggregate node.
132 -- Typ is the record type for the aggregate resolution
134 -- While performing the semantic checks, this procedure builds a new
135 -- Component_Association_List where each record field appears alone in a
136 -- Component_Choice_List along with its corresponding expression. The
137 -- record fields in the Component_Association_List appear in the same order
138 -- in which they appear in the record type Typ.
140 -- Once this new Component_Association_List is built and all the semantic
141 -- checks performed, the original aggregate subtree is replaced with the
142 -- new named record aggregate just built. This new record aggregate has no
143 -- positional associations, so its Expressions field is set to No_List.
144 -- Note that subtree substitution is performed with Rewrite so as to be
145 -- able to retrieve the original aggregate.
147 -- The aggregate subtree manipulation performed by Resolve_Record_Aggregate
148 -- yields the aggregate format expected by Gigi. Typically, this kind of
149 -- tree manipulations are done in the expander. However, because the
150 -- semantic checks that need to be performed on record aggregates really go
151 -- hand in hand with the record aggregate normalization, the aggregate
152 -- subtree transformation is performed during resolution rather than
153 -- expansion. Had we decided otherwise we would have had to duplicate most
154 -- of the code in the expansion procedure Expand_Record_Aggregate. Note,
155 -- however, that all the expansion concerning aggregates for tagged records
156 -- is done in Expand_Record_Aggregate.
158 -- The algorithm of Resolve_Record_Aggregate proceeds as follows:
160 -- 1. Make sure that the record type against which the record aggregate
161 -- has to be resolved is not abstract. Furthermore if the type is a
162 -- null aggregate make sure the input aggregate N is also null.
164 -- 2. Verify that the structure of the aggregate is that of a record
165 -- aggregate. Specifically, look for component associations and ensure
166 -- that each choice list only has identifiers or the N_Others_Choice
167 -- node. Also make sure that if present, the N_Others_Choice occurs
168 -- last and by itself.
170 -- 3. If Typ contains discriminants, the values for each discriminant is
171 -- looked for. If the record type Typ has variants, we check that the
172 -- expressions corresponding to each discriminant ruling the (possibly
173 -- nested) variant parts of Typ, are static. This allows us to determine
174 -- the variant parts to which the rest of the aggregate must conform.
175 -- The names of discriminants with their values are saved in a new
176 -- association list, New_Assoc_List which is later augmented with the
177 -- names and values of the remaining components in the record type.
179 -- During this phase we also make sure that every discriminant is
180 -- assigned exactly one value. Note that when several values for a given
181 -- discriminant are found, semantic processing continues looking for
182 -- further errors. In this case it's the first discriminant value found
183 -- which we will be recorded.
185 -- IMPORTANT NOTE: For derived tagged types this procedure expects
186 -- First_Discriminant and Next_Discriminant to give the correct list
187 -- of discriminants, in the correct order.
189 -- 4. After all the discriminant values have been gathered, we can set the
190 -- Etype of the record aggregate. If Typ contains no discriminants this
191 -- is straightforward: the Etype of N is just Typ, otherwise a new
192 -- implicit constrained subtype of Typ is built to be the Etype of N.
194 -- 5. Gather the remaining record components according to the discriminant
195 -- values. This involves recursively traversing the record type
196 -- structure to see what variants are selected by the given discriminant
197 -- values. This processing is a little more convoluted if Typ is a
198 -- derived tagged types since we need to retrieve the record structure
199 -- of all the ancestors of Typ.
201 -- 6. After gathering the record components we look for their values in the
202 -- record aggregate and emit appropriate error messages should we not
203 -- find such values or should they be duplicated.
205 -- 7. We then make sure no illegal component names appear in the record
206 -- aggregate and make sure that the type of the record components
207 -- appearing in a same choice list is the same. Finally we ensure that
208 -- the others choice, if present, is used to provide the value of at
209 -- least a record component.
211 -- 8. The original aggregate node is replaced with the new named aggregate
212 -- built in steps 3 through 6, as explained earlier.
214 -- Given the complexity of record aggregate resolution, the primary goal of
215 -- this routine is clarity and simplicity rather than execution and storage
216 -- efficiency. If there are only positional components in the aggregate the
217 -- running time is linear. If there are associations the running time is
218 -- still linear as long as the order of the associations is not too far off
219 -- the order of the components in the record type. If this is not the case
220 -- the running time is at worst quadratic in the size of the association
223 procedure Check_Misspelled_Component
224 (Elements : Elist_Id;
225 Component : Node_Id);
226 -- Give possible misspelling diagnostic if Component is likely to be a
227 -- misspelling of one of the components of the Assoc_List. This is called
228 -- by Resolve_Aggr_Expr after producing an invalid component error message.
230 -----------------------------------------------------
231 -- Subprograms used for ARRAY AGGREGATE Processing --
232 -----------------------------------------------------
234 function Resolve_Array_Aggregate
237 Index_Constr : Node_Id;
238 Component_Typ : Entity_Id;
239 Others_Allowed : Boolean) return Boolean;
240 -- This procedure performs the semantic checks for an array aggregate.
241 -- True is returned if the aggregate resolution succeeds.
243 -- The procedure works by recursively checking each nested aggregate.
244 -- Specifically, after checking a sub-aggregate nested at the i-th level
245 -- we recursively check all the subaggregates at the i+1-st level (if any).
246 -- Note that for aggregates analysis and resolution go hand in hand.
247 -- Aggregate analysis has been delayed up to here and it is done while
248 -- resolving the aggregate.
250 -- N is the current N_Aggregate node to be checked.
252 -- Index is the index node corresponding to the array sub-aggregate that
253 -- we are currently checking (RM 4.3.3 (8)). Its Etype is the
254 -- corresponding index type (or subtype).
256 -- Index_Constr is the node giving the applicable index constraint if
257 -- any (RM 4.3.3 (10)). It "is a constraint provided by certain
258 -- contexts [...] that can be used to determine the bounds of the array
259 -- value specified by the aggregate". If Others_Allowed below is False
260 -- there is no applicable index constraint and this node is set to Index.
262 -- Component_Typ is the array component type.
264 -- Others_Allowed indicates whether an others choice is allowed
265 -- in the context where the top-level aggregate appeared.
267 -- The algorithm of Resolve_Array_Aggregate proceeds as follows:
269 -- 1. Make sure that the others choice, if present, is by itself and
270 -- appears last in the sub-aggregate. Check that we do not have
271 -- positional and named components in the array sub-aggregate (unless
272 -- the named association is an others choice). Finally if an others
273 -- choice is present, make sure it is allowed in the aggregate context.
275 -- 2. If the array sub-aggregate contains discrete_choices:
277 -- (A) Verify their validity. Specifically verify that:
279 -- (a) If a null range is present it must be the only possible
280 -- choice in the array aggregate.
282 -- (b) Ditto for a non static range.
284 -- (c) Ditto for a non static expression.
286 -- In addition this step analyzes and resolves each discrete_choice,
287 -- making sure that its type is the type of the corresponding Index.
288 -- If we are not at the lowest array aggregate level (in the case of
289 -- multi-dimensional aggregates) then invoke Resolve_Array_Aggregate
290 -- recursively on each component expression. Otherwise, resolve the
291 -- bottom level component expressions against the expected component
292 -- type ONLY IF the component corresponds to a single discrete choice
293 -- which is not an others choice (to see why read the DELAYED
294 -- COMPONENT RESOLUTION below).
296 -- (B) Determine the bounds of the sub-aggregate and lowest and
297 -- highest choice values.
299 -- 3. For positional aggregates:
301 -- (A) Loop over the component expressions either recursively invoking
302 -- Resolve_Array_Aggregate on each of these for multi-dimensional
303 -- array aggregates or resolving the bottom level component
304 -- expressions against the expected component type.
306 -- (B) Determine the bounds of the positional sub-aggregates.
308 -- 4. Try to determine statically whether the evaluation of the array
309 -- sub-aggregate raises Constraint_Error. If yes emit proper
310 -- warnings. The precise checks are the following:
312 -- (A) Check that the index range defined by aggregate bounds is
313 -- compatible with corresponding index subtype.
314 -- We also check against the base type. In fact it could be that
315 -- Low/High bounds of the base type are static whereas those of
316 -- the index subtype are not. Thus if we can statically catch
317 -- a problem with respect to the base type we are guaranteed
318 -- that the same problem will arise with the index subtype
320 -- (B) If we are dealing with a named aggregate containing an others
321 -- choice and at least one discrete choice then make sure the range
322 -- specified by the discrete choices does not overflow the
323 -- aggregate bounds. We also check against the index type and base
324 -- type bounds for the same reasons given in (A).
326 -- (C) If we are dealing with a positional aggregate with an others
327 -- choice make sure the number of positional elements specified
328 -- does not overflow the aggregate bounds. We also check against
329 -- the index type and base type bounds as mentioned in (A).
331 -- Finally construct an N_Range node giving the sub-aggregate bounds.
332 -- Set the Aggregate_Bounds field of the sub-aggregate to be this
333 -- N_Range. The routine Array_Aggr_Subtype below uses such N_Ranges
334 -- to build the appropriate aggregate subtype. Aggregate_Bounds
335 -- information is needed during expansion.
337 -- DELAYED COMPONENT RESOLUTION: The resolution of bottom level component
338 -- expressions in an array aggregate may call Duplicate_Subexpr or some
339 -- other routine that inserts code just outside the outermost aggregate.
340 -- If the array aggregate contains discrete choices or an others choice,
341 -- this may be wrong. Consider for instance the following example.
343 -- type Rec is record
347 -- type Acc_Rec is access Rec;
348 -- Arr : array (1..3) of Acc_Rec := (1 .. 3 => new Rec);
350 -- Then the transformation of "new Rec" that occurs during resolution
351 -- entails the following code modifications
353 -- P7b : constant Acc_Rec := new Rec;
355 -- Arr : array (1..3) of Acc_Rec := (1 .. 3 => P7b);
357 -- This code transformation is clearly wrong, since we need to call
358 -- "new Rec" for each of the 3 array elements. To avoid this problem we
359 -- delay resolution of the components of non positional array aggregates
360 -- to the expansion phase. As an optimization, if the discrete choice
361 -- specifies a single value we do not delay resolution.
363 function Array_Aggr_Subtype (N : Node_Id; Typ : Node_Id) return Entity_Id;
364 -- This routine returns the type or subtype of an array aggregate.
366 -- N is the array aggregate node whose type we return.
368 -- Typ is the context type in which N occurs.
370 -- This routine creates an implicit array subtype whose bounds are
371 -- those defined by the aggregate. When this routine is invoked
372 -- Resolve_Array_Aggregate has already processed aggregate N. Thus the
373 -- Aggregate_Bounds of each sub-aggregate, is an N_Range node giving the
374 -- sub-aggregate bounds. When building the aggregate itype, this function
375 -- traverses the array aggregate N collecting such Aggregate_Bounds and
376 -- constructs the proper array aggregate itype.
378 -- Note that in the case of multidimensional aggregates each inner
379 -- sub-aggregate corresponding to a given array dimension, may provide a
380 -- different bounds. If it is possible to determine statically that
381 -- some sub-aggregates corresponding to the same index do not have the
382 -- same bounds, then a warning is emitted. If such check is not possible
383 -- statically (because some sub-aggregate bounds are dynamic expressions)
384 -- then this job is left to the expander. In all cases the particular
385 -- bounds that this function will chose for a given dimension is the first
386 -- N_Range node for a sub-aggregate corresponding to that dimension.
388 -- Note that the Raises_Constraint_Error flag of an array aggregate
389 -- whose evaluation is determined to raise CE by Resolve_Array_Aggregate,
390 -- is set in Resolve_Array_Aggregate but the aggregate is not
391 -- immediately replaced with a raise CE. In fact, Array_Aggr_Subtype must
392 -- first construct the proper itype for the aggregate (Gigi needs
393 -- this). After constructing the proper itype we will eventually replace
394 -- the top-level aggregate with a raise CE (done in Resolve_Aggregate).
395 -- Of course in cases such as:
397 -- type Arr is array (integer range <>) of Integer;
398 -- A : Arr := (positive range -1 .. 2 => 0);
400 -- The bounds of the aggregate itype are cooked up to look reasonable
401 -- (in this particular case the bounds will be 1 .. 2).
403 procedure Make_String_Into_Aggregate (N : Node_Id);
404 -- A string literal can appear in a context in which a one dimensional
405 -- array of characters is expected. This procedure simply rewrites the
406 -- string as an aggregate, prior to resolution.
408 ---------------------------------
409 -- Delta aggregate processing --
410 ---------------------------------
412 procedure Resolve_Delta_Array_Aggregate (N : Node_Id; Typ : Entity_Id);
413 procedure Resolve_Delta_Record_Aggregate (N : Node_Id; Typ : Entity_Id);
415 ------------------------
416 -- Array_Aggr_Subtype --
417 ------------------------
419 function Array_Aggr_Subtype
421 Typ : Entity_Id) return Entity_Id
423 Aggr_Dimension : constant Pos := Number_Dimensions (Typ);
424 -- Number of aggregate index dimensions
426 Aggr_Range : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
427 -- Constrained N_Range of each index dimension in our aggregate itype
429 Aggr_Low : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
430 Aggr_High : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
431 -- Low and High bounds for each index dimension in our aggregate itype
433 Is_Fully_Positional : Boolean := True;
435 procedure Collect_Aggr_Bounds (N : Node_Id; Dim : Pos);
436 -- N is an array (sub-)aggregate. Dim is the dimension corresponding
437 -- to (sub-)aggregate N. This procedure collects and removes the side
438 -- effects of the constrained N_Range nodes corresponding to each index
439 -- dimension of our aggregate itype. These N_Range nodes are collected
440 -- in Aggr_Range above.
442 -- Likewise collect in Aggr_Low & Aggr_High above the low and high
443 -- bounds of each index dimension. If, when collecting, two bounds
444 -- corresponding to the same dimension are static and found to differ,
445 -- then emit a warning, and mark N as raising Constraint_Error.
447 -------------------------
448 -- Collect_Aggr_Bounds --
449 -------------------------
451 procedure Collect_Aggr_Bounds (N : Node_Id; Dim : Pos) is
452 This_Range : constant Node_Id := Aggregate_Bounds (N);
453 -- The aggregate range node of this specific sub-aggregate
455 This_Low : constant Node_Id := Low_Bound (Aggregate_Bounds (N));
456 This_High : constant Node_Id := High_Bound (Aggregate_Bounds (N));
457 -- The aggregate bounds of this specific sub-aggregate
463 Remove_Side_Effects (This_Low, Variable_Ref => True);
464 Remove_Side_Effects (This_High, Variable_Ref => True);
466 -- Collect the first N_Range for a given dimension that you find.
467 -- For a given dimension they must be all equal anyway.
469 if No (Aggr_Range (Dim)) then
470 Aggr_Low (Dim) := This_Low;
471 Aggr_High (Dim) := This_High;
472 Aggr_Range (Dim) := This_Range;
475 if Compile_Time_Known_Value (This_Low) then
476 if not Compile_Time_Known_Value (Aggr_Low (Dim)) then
477 Aggr_Low (Dim) := This_Low;
479 elsif Expr_Value (This_Low) /= Expr_Value (Aggr_Low (Dim)) then
480 Set_Raises_Constraint_Error (N);
481 Error_Msg_Warn := SPARK_Mode /= On;
482 Error_Msg_N ("sub-aggregate low bound mismatch<<", N);
483 Error_Msg_N ("\Constraint_Error [<<", N);
487 if Compile_Time_Known_Value (This_High) then
488 if not Compile_Time_Known_Value (Aggr_High (Dim)) then
489 Aggr_High (Dim) := This_High;
492 Expr_Value (This_High) /= Expr_Value (Aggr_High (Dim))
494 Set_Raises_Constraint_Error (N);
495 Error_Msg_Warn := SPARK_Mode /= On;
496 Error_Msg_N ("sub-aggregate high bound mismatch<<", N);
497 Error_Msg_N ("\Constraint_Error [<<", N);
502 if Dim < Aggr_Dimension then
504 -- Process positional components
506 if Present (Expressions (N)) then
507 Expr := First (Expressions (N));
508 while Present (Expr) loop
509 Collect_Aggr_Bounds (Expr, Dim + 1);
514 -- Process component associations
516 if Present (Component_Associations (N)) then
517 Is_Fully_Positional := False;
519 Assoc := First (Component_Associations (N));
520 while Present (Assoc) loop
521 Expr := Expression (Assoc);
522 Collect_Aggr_Bounds (Expr, Dim + 1);
527 end Collect_Aggr_Bounds;
529 -- Array_Aggr_Subtype variables
532 -- The final itype of the overall aggregate
534 Index_Constraints : constant List_Id := New_List;
535 -- The list of index constraints of the aggregate itype
537 -- Start of processing for Array_Aggr_Subtype
540 -- Make sure that the list of index constraints is properly attached to
541 -- the tree, and then collect the aggregate bounds.
543 Set_Parent (Index_Constraints, N);
544 Collect_Aggr_Bounds (N, 1);
546 -- Build the list of constrained indexes of our aggregate itype
548 for J in 1 .. Aggr_Dimension loop
549 Create_Index : declare
550 Index_Base : constant Entity_Id :=
551 Base_Type (Etype (Aggr_Range (J)));
552 Index_Typ : Entity_Id;
555 -- Construct the Index subtype, and associate it with the range
556 -- construct that generates it.
559 Create_Itype (Subtype_Kind (Ekind (Index_Base)), Aggr_Range (J));
561 Set_Etype (Index_Typ, Index_Base);
563 if Is_Character_Type (Index_Base) then
564 Set_Is_Character_Type (Index_Typ);
567 Set_Size_Info (Index_Typ, (Index_Base));
568 Set_RM_Size (Index_Typ, RM_Size (Index_Base));
569 Set_First_Rep_Item (Index_Typ, First_Rep_Item (Index_Base));
570 Set_Scalar_Range (Index_Typ, Aggr_Range (J));
572 if Is_Discrete_Or_Fixed_Point_Type (Index_Typ) then
573 Set_RM_Size (Index_Typ, UI_From_Int (Minimum_Size (Index_Typ)));
576 Set_Etype (Aggr_Range (J), Index_Typ);
578 Append (Aggr_Range (J), To => Index_Constraints);
582 -- Now build the Itype
584 Itype := Create_Itype (E_Array_Subtype, N);
586 Set_First_Rep_Item (Itype, First_Rep_Item (Typ));
587 Set_Convention (Itype, Convention (Typ));
588 Set_Depends_On_Private (Itype, Has_Private_Component (Typ));
589 Set_Etype (Itype, Base_Type (Typ));
590 Set_Has_Alignment_Clause (Itype, Has_Alignment_Clause (Typ));
591 Set_Is_Aliased (Itype, Is_Aliased (Typ));
592 Set_Is_Independent (Itype, Is_Independent (Typ));
593 Set_Depends_On_Private (Itype, Depends_On_Private (Typ));
595 Copy_Suppress_Status (Index_Check, Typ, Itype);
596 Copy_Suppress_Status (Length_Check, Typ, Itype);
598 Set_First_Index (Itype, First (Index_Constraints));
599 Set_Is_Constrained (Itype, True);
600 Set_Is_Internal (Itype, True);
602 if Has_Predicates (Typ) then
603 Set_Has_Predicates (Itype);
605 -- If the base type has a predicate, capture the predicated parent
606 -- or the existing predicate function for SPARK use.
608 if Present (Predicate_Function (Typ)) then
609 Set_Predicate_Function (Itype, Predicate_Function (Typ));
611 elsif Is_Itype (Typ) then
612 Set_Predicated_Parent (Itype, Predicated_Parent (Typ));
615 Set_Predicated_Parent (Itype, Typ);
619 -- A simple optimization: purely positional aggregates of static
620 -- components should be passed to gigi unexpanded whenever possible, and
621 -- regardless of the staticness of the bounds themselves. Subsequent
622 -- checks in exp_aggr verify that type is not packed, etc.
624 Set_Size_Known_At_Compile_Time
627 and then Comes_From_Source (N)
628 and then Size_Known_At_Compile_Time (Component_Type (Typ)));
630 -- We always need a freeze node for a packed array subtype, so that we
631 -- can build the Packed_Array_Impl_Type corresponding to the subtype. If
632 -- expansion is disabled, the packed array subtype is not built, and we
633 -- must not generate a freeze node for the type, or else it will appear
634 -- incomplete to gigi.
637 and then not In_Spec_Expression
638 and then Expander_Active
640 Freeze_Itype (Itype, N);
644 end Array_Aggr_Subtype;
646 --------------------------------
647 -- Check_Misspelled_Component --
648 --------------------------------
650 procedure Check_Misspelled_Component
651 (Elements : Elist_Id;
654 Max_Suggestions : constant := 2;
656 Nr_Of_Suggestions : Natural := 0;
657 Suggestion_1 : Entity_Id := Empty;
658 Suggestion_2 : Entity_Id := Empty;
659 Component_Elmt : Elmt_Id;
662 -- All the components of List are matched against Component and a count
663 -- is maintained of possible misspellings. When at the end of the
664 -- analysis there are one or two (not more) possible misspellings,
665 -- these misspellings will be suggested as possible corrections.
667 Component_Elmt := First_Elmt (Elements);
668 while Nr_Of_Suggestions <= Max_Suggestions
669 and then Present (Component_Elmt)
671 if Is_Bad_Spelling_Of
672 (Chars (Node (Component_Elmt)),
675 Nr_Of_Suggestions := Nr_Of_Suggestions + 1;
677 case Nr_Of_Suggestions is
678 when 1 => Suggestion_1 := Node (Component_Elmt);
679 when 2 => Suggestion_2 := Node (Component_Elmt);
684 Next_Elmt (Component_Elmt);
687 -- Report at most two suggestions
689 if Nr_Of_Suggestions = 1 then
690 Error_Msg_NE -- CODEFIX
691 ("\possible misspelling of&", Component, Suggestion_1);
693 elsif Nr_Of_Suggestions = 2 then
694 Error_Msg_Node_2 := Suggestion_2;
695 Error_Msg_NE -- CODEFIX
696 ("\possible misspelling of& or&", Component, Suggestion_1);
698 end Check_Misspelled_Component;
700 ----------------------------------------
701 -- Check_Expr_OK_In_Limited_Aggregate --
702 ----------------------------------------
704 procedure Check_Expr_OK_In_Limited_Aggregate (Expr : Node_Id) is
706 if Is_Limited_Type (Etype (Expr))
707 and then Comes_From_Source (Expr)
709 if In_Instance_Body or else In_Inlined_Body then
712 elsif not OK_For_Limited_Init (Etype (Expr), Expr) then
714 ("initialization not allowed for limited types", Expr);
715 Explain_Limited_Type (Etype (Expr), Expr);
718 end Check_Expr_OK_In_Limited_Aggregate;
720 -------------------------
721 -- Is_Others_Aggregate --
722 -------------------------
724 function Is_Others_Aggregate (Aggr : Node_Id) return Boolean is
725 Assoc : constant List_Id := Component_Associations (Aggr);
728 return No (Expressions (Aggr))
729 and then Nkind (First (Choice_List (First (Assoc)))) = N_Others_Choice;
730 end Is_Others_Aggregate;
732 -------------------------
733 -- Is_Single_Aggregate --
734 -------------------------
736 function Is_Single_Aggregate (Aggr : Node_Id) return Boolean is
737 Assoc : constant List_Id := Component_Associations (Aggr);
740 return No (Expressions (Aggr))
741 and then No (Next (First (Assoc)))
742 and then No (Next (First (Choice_List (First (Assoc)))));
743 end Is_Single_Aggregate;
745 --------------------------------
746 -- Make_String_Into_Aggregate --
747 --------------------------------
749 procedure Make_String_Into_Aggregate (N : Node_Id) is
750 Exprs : constant List_Id := New_List;
751 Loc : constant Source_Ptr := Sloc (N);
752 Str : constant String_Id := Strval (N);
753 Strlen : constant Nat := String_Length (Str);
761 for J in 1 .. Strlen loop
762 C := Get_String_Char (Str, J);
763 Set_Character_Literal_Name (C);
766 Make_Character_Literal (P,
768 Char_Literal_Value => UI_From_CC (C));
769 Set_Etype (C_Node, Any_Character);
770 Append_To (Exprs, C_Node);
773 -- Something special for wide strings???
776 New_N := Make_Aggregate (Loc, Expressions => Exprs);
777 Set_Analyzed (New_N);
778 Set_Etype (New_N, Any_Composite);
781 end Make_String_Into_Aggregate;
783 -----------------------
784 -- Resolve_Aggregate --
785 -----------------------
787 procedure Resolve_Aggregate (N : Node_Id; Typ : Entity_Id) is
788 Loc : constant Source_Ptr := Sloc (N);
790 Aggr_Subtyp : Entity_Id;
791 -- The actual aggregate subtype. This is not necessarily the same as Typ
792 -- which is the subtype of the context in which the aggregate was found.
794 Others_Box : Boolean := False;
795 -- Set to True if N represents a simple aggregate with only
796 -- (others => <>), not nested as part of another aggregate.
798 function Within_Aggregate (N : Node_Id) return Boolean;
799 -- Return True if N is part of an N_Aggregate
801 ----------------------
802 -- Within_Aggregate --
803 ----------------------
805 function Within_Aggregate (N : Node_Id) return Boolean is
806 P : Node_Id := Parent (N);
808 while Present (P) loop
809 if Nkind (P) = N_Aggregate then
817 end Within_Aggregate;
819 -- Start of processing for Resolve_Aggregate
822 -- Ignore junk empty aggregate resulting from parser error
824 if No (Expressions (N))
825 and then No (Component_Associations (N))
826 and then not Null_Record_Present (N)
831 -- If the aggregate has box-initialized components, its type must be
832 -- frozen so that initialization procedures can properly be called
833 -- in the resolution that follows. The replacement of boxes with
834 -- initialization calls is properly an expansion activity but it must
835 -- be done during resolution.
838 and then Present (Component_Associations (N))
842 First_Comp : Boolean := True;
845 Comp := First (Component_Associations (N));
846 while Present (Comp) loop
847 if Box_Present (Comp) then
849 and then No (Expressions (N))
850 and then Nkind (First (Choices (Comp))) = N_Others_Choice
851 and then not Within_Aggregate (N)
856 Insert_Actions (N, Freeze_Entity (Typ, N));
866 -- Check for aggregates not allowed in configurable run-time mode.
867 -- We allow all cases of aggregates that do not come from source, since
868 -- these are all assumed to be small (e.g. bounds of a string literal).
869 -- We also allow aggregates of types we know to be small.
871 if not Support_Aggregates_On_Target
872 and then Comes_From_Source (N)
873 and then (not Known_Static_Esize (Typ)
874 or else Esize (Typ) > System_Max_Integer_Size)
876 Error_Msg_CRT ("aggregate", N);
879 -- Ada 2005 (AI-287): Limited aggregates allowed
881 -- In an instance, ignore aggregate subcomponents tnat may be limited,
882 -- because they originate in view conflicts. If the original aggregate
883 -- is legal and the actuals are legal, the aggregate itself is legal.
885 if Is_Limited_Type (Typ)
886 and then Ada_Version < Ada_2005
887 and then not In_Instance
889 Error_Msg_N ("aggregate type cannot be limited", N);
890 Explain_Limited_Type (Typ, N);
892 elsif Is_Class_Wide_Type (Typ) then
893 Error_Msg_N ("type of aggregate cannot be class-wide", N);
895 elsif Typ = Any_String
896 or else Typ = Any_Composite
898 Error_Msg_N ("no unique type for aggregate", N);
899 Set_Etype (N, Any_Composite);
901 elsif Is_Array_Type (Typ) and then Null_Record_Present (N) then
902 Error_Msg_N ("null record forbidden in array aggregate", N);
904 elsif Present (Find_Aspect (Typ, Aspect_Aggregate))
905 and then Ekind (Typ) /= E_Record_Type
906 and then Ada_Version >= Ada_2020
908 Resolve_Container_Aggregate (N, Typ);
910 elsif Is_Record_Type (Typ) then
911 Resolve_Record_Aggregate (N, Typ);
913 elsif Is_Array_Type (Typ) then
915 -- First a special test, for the case of a positional aggregate of
916 -- characters which can be replaced by a string literal.
918 -- Do not perform this transformation if this was a string literal
919 -- to start with, whose components needed constraint checks, or if
920 -- the component type is non-static, because it will require those
921 -- checks and be transformed back into an aggregate. If the index
922 -- type is not Integer the aggregate may represent a user-defined
923 -- string type but the context might need the original type so we
924 -- do not perform the transformation at this point.
926 if Number_Dimensions (Typ) = 1
927 and then Is_Standard_Character_Type (Component_Type (Typ))
928 and then No (Component_Associations (N))
929 and then not Is_Limited_Composite (Typ)
930 and then not Is_Private_Composite (Typ)
931 and then not Is_Bit_Packed_Array (Typ)
932 and then Nkind (Original_Node (Parent (N))) /= N_String_Literal
933 and then Is_OK_Static_Subtype (Component_Type (Typ))
934 and then Base_Type (Etype (First_Index (Typ))) =
935 Base_Type (Standard_Integer)
941 Expr := First (Expressions (N));
942 while Present (Expr) loop
943 exit when Nkind (Expr) /= N_Character_Literal;
950 Expr := First (Expressions (N));
951 while Present (Expr) loop
952 Store_String_Char (UI_To_CC (Char_Literal_Value (Expr)));
956 Rewrite (N, Make_String_Literal (Loc, End_String));
958 Analyze_And_Resolve (N, Typ);
964 -- Here if we have a real aggregate to deal with
966 Array_Aggregate : declare
967 Aggr_Resolved : Boolean;
969 Aggr_Typ : constant Entity_Id := Etype (Typ);
970 -- This is the unconstrained array type, which is the type against
971 -- which the aggregate is to be resolved. Typ itself is the array
972 -- type of the context which may not be the same subtype as the
973 -- subtype for the final aggregate.
976 -- In the following we determine whether an OTHERS choice is
977 -- allowed inside the array aggregate. The test checks the context
978 -- in which the array aggregate occurs. If the context does not
979 -- permit it, or the aggregate type is unconstrained, an OTHERS
980 -- choice is not allowed (except that it is always allowed on the
981 -- right-hand side of an assignment statement; in this case the
982 -- constrainedness of the type doesn't matter, because an array
983 -- object is always constrained).
985 -- If expansion is disabled (generic context, or semantics-only
986 -- mode) actual subtypes cannot be constructed, and the type of an
987 -- object may be its unconstrained nominal type. However, if the
988 -- context is an assignment statement, OTHERS is allowed, because
989 -- the target of the assignment will have a constrained subtype
990 -- when fully compiled. Ditto if the context is an initialization
991 -- procedure where a component may have a predicate function that
992 -- carries the base type.
994 -- Note that there is no node for Explicit_Actual_Parameter.
995 -- To test for this context we therefore have to test for node
996 -- N_Parameter_Association which itself appears only if there is a
997 -- formal parameter. Consequently we also need to test for
998 -- N_Procedure_Call_Statement or N_Function_Call.
1000 -- The context may be an N_Reference node, created by expansion.
1001 -- Legality of the others clause was established in the source,
1002 -- so the context is legal.
1004 Set_Etype (N, Aggr_Typ); -- May be overridden later on
1006 if Nkind (Parent (N)) = N_Assignment_Statement
1007 or else Inside_Init_Proc
1008 or else (Is_Constrained (Typ)
1009 and then Nkind (Parent (N)) in
1010 N_Parameter_Association
1012 | N_Procedure_Call_Statement
1013 | N_Generic_Association
1014 | N_Formal_Object_Declaration
1015 | N_Simple_Return_Statement
1016 | N_Object_Declaration
1017 | N_Component_Declaration
1018 | N_Parameter_Specification
1019 | N_Qualified_Expression
1022 | N_Extension_Aggregate
1023 | N_Component_Association
1024 | N_Case_Expression_Alternative
1026 | N_Expression_With_Actions)
1029 Resolve_Array_Aggregate
1031 Index => First_Index (Aggr_Typ),
1032 Index_Constr => First_Index (Typ),
1033 Component_Typ => Component_Type (Typ),
1034 Others_Allowed => True);
1037 Resolve_Array_Aggregate
1039 Index => First_Index (Aggr_Typ),
1040 Index_Constr => First_Index (Aggr_Typ),
1041 Component_Typ => Component_Type (Typ),
1042 Others_Allowed => False);
1045 if not Aggr_Resolved then
1047 -- A parenthesized expression may have been intended as an
1048 -- aggregate, leading to a type error when analyzing the
1049 -- component. This can also happen for a nested component
1050 -- (see Analyze_Aggr_Expr).
1052 if Paren_Count (N) > 0 then
1054 ("positional aggregate cannot have one component", N);
1057 Aggr_Subtyp := Any_Composite;
1060 Aggr_Subtyp := Array_Aggr_Subtype (N, Typ);
1063 Set_Etype (N, Aggr_Subtyp);
1064 end Array_Aggregate;
1066 elsif Is_Private_Type (Typ)
1067 and then Present (Full_View (Typ))
1068 and then (In_Inlined_Body or In_Instance_Body)
1069 and then Is_Composite_Type (Full_View (Typ))
1071 Resolve (N, Full_View (Typ));
1074 Error_Msg_N ("illegal context for aggregate", N);
1077 -- If we can determine statically that the evaluation of the aggregate
1078 -- raises Constraint_Error, then replace the aggregate with an
1079 -- N_Raise_Constraint_Error node, but set the Etype to the right
1080 -- aggregate subtype. Gigi needs this.
1082 if Raises_Constraint_Error (N) then
1083 Aggr_Subtyp := Etype (N);
1085 Make_Raise_Constraint_Error (Loc, Reason => CE_Range_Check_Failed));
1086 Set_Raises_Constraint_Error (N);
1087 Set_Etype (N, Aggr_Subtyp);
1091 if Warn_On_No_Value_Assigned
1093 and then not Is_Fully_Initialized_Type (Etype (N))
1095 Error_Msg_N ("?v?aggregate not fully initialized", N);
1098 Check_Function_Writable_Actuals (N);
1099 end Resolve_Aggregate;
1101 -----------------------------
1102 -- Resolve_Array_Aggregate --
1103 -----------------------------
1105 function Resolve_Array_Aggregate
1108 Index_Constr : Node_Id;
1109 Component_Typ : Entity_Id;
1110 Others_Allowed : Boolean) return Boolean
1112 Loc : constant Source_Ptr := Sloc (N);
1114 Failure : constant Boolean := False;
1115 Success : constant Boolean := True;
1117 Index_Typ : constant Entity_Id := Etype (Index);
1118 Index_Typ_Low : constant Node_Id := Type_Low_Bound (Index_Typ);
1119 Index_Typ_High : constant Node_Id := Type_High_Bound (Index_Typ);
1120 -- The type of the index corresponding to the array sub-aggregate along
1121 -- with its low and upper bounds.
1123 Index_Base : constant Entity_Id := Base_Type (Index_Typ);
1124 Index_Base_Low : constant Node_Id := Type_Low_Bound (Index_Base);
1125 Index_Base_High : constant Node_Id := Type_High_Bound (Index_Base);
1126 -- Ditto for the base type
1128 Others_Present : Boolean := False;
1130 Nb_Choices : Nat := 0;
1131 -- Contains the overall number of named choices in this sub-aggregate
1133 function Add (Val : Uint; To : Node_Id) return Node_Id;
1134 -- Creates a new expression node where Val is added to expression To.
1135 -- Tries to constant fold whenever possible. To must be an already
1136 -- analyzed expression.
1138 procedure Check_Bound (BH : Node_Id; AH : in out Node_Id);
1139 -- Checks that AH (the upper bound of an array aggregate) is less than
1140 -- or equal to BH (the upper bound of the index base type). If the check
1141 -- fails, a warning is emitted, the Raises_Constraint_Error flag of N is
1142 -- set, and AH is replaced with a duplicate of BH.
1144 procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id);
1145 -- Checks that range AL .. AH is compatible with range L .. H. Emits a
1146 -- warning if not and sets the Raises_Constraint_Error flag in N.
1148 procedure Check_Length (L, H : Node_Id; Len : Uint);
1149 -- Checks that range L .. H contains at least Len elements. Emits a
1150 -- warning if not and sets the Raises_Constraint_Error flag in N.
1152 function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean;
1153 -- Returns True if range L .. H is dynamic or null
1155 procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean);
1156 -- Given expression node From, this routine sets OK to False if it
1157 -- cannot statically evaluate From. Otherwise it stores this static
1158 -- value into Value.
1160 function Resolve_Aggr_Expr
1162 Single_Elmt : Boolean) return Boolean;
1163 -- Resolves aggregate expression Expr. Returns False if resolution
1164 -- fails. If Single_Elmt is set to False, the expression Expr may be
1165 -- used to initialize several array aggregate elements (this can happen
1166 -- for discrete choices such as "L .. H => Expr" or the OTHERS choice).
1167 -- In this event we do not resolve Expr unless expansion is disabled.
1168 -- To know why, see the DELAYED COMPONENT RESOLUTION note above.
1170 -- NOTE: In the case of "... => <>", we pass the in the
1171 -- N_Component_Association node as Expr, since there is no Expression in
1172 -- that case, and we need a Sloc for the error message.
1174 procedure Resolve_Iterated_Component_Association
1176 Index_Typ : Entity_Id);
1183 function Add (Val : Uint; To : Node_Id) return Node_Id is
1189 if Raises_Constraint_Error (To) then
1193 -- First test if we can do constant folding
1195 if Compile_Time_Known_Value (To)
1196 or else Nkind (To) = N_Integer_Literal
1198 Expr_Pos := Make_Integer_Literal (Loc, Expr_Value (To) + Val);
1199 Set_Is_Static_Expression (Expr_Pos);
1200 Set_Etype (Expr_Pos, Etype (To));
1201 Set_Analyzed (Expr_Pos, Analyzed (To));
1203 if not Is_Enumeration_Type (Index_Typ) then
1206 -- If we are dealing with enumeration return
1207 -- Index_Typ'Val (Expr_Pos)
1211 Make_Attribute_Reference
1213 Prefix => New_Occurrence_Of (Index_Typ, Loc),
1214 Attribute_Name => Name_Val,
1215 Expressions => New_List (Expr_Pos));
1221 -- If we are here no constant folding possible
1223 if not Is_Enumeration_Type (Index_Base) then
1226 Left_Opnd => Duplicate_Subexpr (To),
1227 Right_Opnd => Make_Integer_Literal (Loc, Val));
1229 -- If we are dealing with enumeration return
1230 -- Index_Typ'Val (Index_Typ'Pos (To) + Val)
1234 Make_Attribute_Reference
1236 Prefix => New_Occurrence_Of (Index_Typ, Loc),
1237 Attribute_Name => Name_Pos,
1238 Expressions => New_List (Duplicate_Subexpr (To)));
1242 Left_Opnd => To_Pos,
1243 Right_Opnd => Make_Integer_Literal (Loc, Val));
1246 Make_Attribute_Reference
1248 Prefix => New_Occurrence_Of (Index_Typ, Loc),
1249 Attribute_Name => Name_Val,
1250 Expressions => New_List (Expr_Pos));
1252 -- If the index type has a non standard representation, the
1253 -- attributes 'Val and 'Pos expand into function calls and the
1254 -- resulting expression is considered non-safe for reevaluation
1255 -- by the backend. Relocate it into a constant temporary in order
1256 -- to make it safe for reevaluation.
1258 if Has_Non_Standard_Rep (Etype (N)) then
1263 Def_Id := Make_Temporary (Loc, 'R', Expr);
1264 Set_Etype (Def_Id, Index_Typ);
1266 Make_Object_Declaration (Loc,
1267 Defining_Identifier => Def_Id,
1268 Object_Definition =>
1269 New_Occurrence_Of (Index_Typ, Loc),
1270 Constant_Present => True,
1271 Expression => Relocate_Node (Expr)));
1273 Expr := New_Occurrence_Of (Def_Id, Loc);
1285 procedure Check_Bound (BH : Node_Id; AH : in out Node_Id) is
1293 Get (Value => Val_BH, From => BH, OK => OK_BH);
1294 Get (Value => Val_AH, From => AH, OK => OK_AH);
1296 if OK_BH and then OK_AH and then Val_BH < Val_AH then
1297 Set_Raises_Constraint_Error (N);
1298 Error_Msg_Warn := SPARK_Mode /= On;
1299 Error_Msg_N ("upper bound out of range<<", AH);
1300 Error_Msg_N ("\Constraint_Error [<<", AH);
1302 -- You need to set AH to BH or else in the case of enumerations
1303 -- indexes we will not be able to resolve the aggregate bounds.
1305 AH := Duplicate_Subexpr (BH);
1313 procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id) is
1324 pragma Warnings (Off, OK_AL);
1325 pragma Warnings (Off, OK_AH);
1328 if Raises_Constraint_Error (N)
1329 or else Dynamic_Or_Null_Range (AL, AH)
1334 Get (Value => Val_L, From => L, OK => OK_L);
1335 Get (Value => Val_H, From => H, OK => OK_H);
1337 Get (Value => Val_AL, From => AL, OK => OK_AL);
1338 Get (Value => Val_AH, From => AH, OK => OK_AH);
1340 if OK_L and then Val_L > Val_AL then
1341 Set_Raises_Constraint_Error (N);
1342 Error_Msg_Warn := SPARK_Mode /= On;
1343 Error_Msg_N ("lower bound of aggregate out of range<<", N);
1344 Error_Msg_N ("\Constraint_Error [<<", N);
1347 if OK_H and then Val_H < Val_AH then
1348 Set_Raises_Constraint_Error (N);
1349 Error_Msg_Warn := SPARK_Mode /= On;
1350 Error_Msg_N ("upper bound of aggregate out of range<<", N);
1351 Error_Msg_N ("\Constraint_Error [<<", N);
1359 procedure Check_Length (L, H : Node_Id; Len : Uint) is
1369 if Raises_Constraint_Error (N) then
1373 Get (Value => Val_L, From => L, OK => OK_L);
1374 Get (Value => Val_H, From => H, OK => OK_H);
1376 if not OK_L or else not OK_H then
1380 -- If null range length is zero
1382 if Val_L > Val_H then
1383 Range_Len := Uint_0;
1385 Range_Len := Val_H - Val_L + 1;
1388 if Range_Len < Len then
1389 Set_Raises_Constraint_Error (N);
1390 Error_Msg_Warn := SPARK_Mode /= On;
1391 Error_Msg_N ("too many elements<<", N);
1392 Error_Msg_N ("\Constraint_Error [<<", N);
1396 ---------------------------
1397 -- Dynamic_Or_Null_Range --
1398 ---------------------------
1400 function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean is
1408 Get (Value => Val_L, From => L, OK => OK_L);
1409 Get (Value => Val_H, From => H, OK => OK_H);
1411 return not OK_L or else not OK_H
1412 or else not Is_OK_Static_Expression (L)
1413 or else not Is_OK_Static_Expression (H)
1414 or else Val_L > Val_H;
1415 end Dynamic_Or_Null_Range;
1421 procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean) is
1425 if Compile_Time_Known_Value (From) then
1426 Value := Expr_Value (From);
1428 -- If expression From is something like Some_Type'Val (10) then
1431 elsif Nkind (From) = N_Attribute_Reference
1432 and then Attribute_Name (From) = Name_Val
1433 and then Compile_Time_Known_Value (First (Expressions (From)))
1435 Value := Expr_Value (First (Expressions (From)));
1442 -----------------------
1443 -- Resolve_Aggr_Expr --
1444 -----------------------
1446 function Resolve_Aggr_Expr
1448 Single_Elmt : Boolean) return Boolean
1450 Nxt_Ind : constant Node_Id := Next_Index (Index);
1451 Nxt_Ind_Constr : constant Node_Id := Next_Index (Index_Constr);
1452 -- Index is the current index corresponding to the expression
1454 Resolution_OK : Boolean := True;
1455 -- Set to False if resolution of the expression failed
1458 -- Defend against previous errors
1460 if Nkind (Expr) = N_Error
1461 or else Error_Posted (Expr)
1466 -- If the array type against which we are resolving the aggregate
1467 -- has several dimensions, the expressions nested inside the
1468 -- aggregate must be further aggregates (or strings).
1470 if Present (Nxt_Ind) then
1471 if Nkind (Expr) /= N_Aggregate then
1473 -- A string literal can appear where a one-dimensional array
1474 -- of characters is expected. If the literal looks like an
1475 -- operator, it is still an operator symbol, which will be
1476 -- transformed into a string when analyzed.
1478 if Is_Character_Type (Component_Typ)
1479 and then No (Next_Index (Nxt_Ind))
1480 and then Nkind (Expr) in N_String_Literal | N_Operator_Symbol
1482 -- A string literal used in a multidimensional array
1483 -- aggregate in place of the final one-dimensional
1484 -- aggregate must not be enclosed in parentheses.
1486 if Paren_Count (Expr) /= 0 then
1487 Error_Msg_N ("no parenthesis allowed here", Expr);
1490 Make_String_Into_Aggregate (Expr);
1493 Error_Msg_N ("nested array aggregate expected", Expr);
1495 -- If the expression is parenthesized, this may be
1496 -- a missing component association for a 1-aggregate.
1498 if Paren_Count (Expr) > 0 then
1500 ("\if single-component aggregate is intended, "
1501 & "write e.g. (1 ='> ...)", Expr);
1508 -- If it's "... => <>", nothing to resolve
1510 if Nkind (Expr) = N_Component_Association then
1511 pragma Assert (Box_Present (Expr));
1515 -- Ada 2005 (AI-231): Propagate the type to the nested aggregate.
1516 -- Required to check the null-exclusion attribute (if present).
1517 -- This value may be overridden later on.
1519 Set_Etype (Expr, Etype (N));
1521 Resolution_OK := Resolve_Array_Aggregate
1522 (Expr, Nxt_Ind, Nxt_Ind_Constr, Component_Typ, Others_Allowed);
1525 -- If it's "... => <>", nothing to resolve
1527 if Nkind (Expr) = N_Component_Association then
1528 pragma Assert (Box_Present (Expr));
1532 -- Do not resolve the expressions of discrete or others choices
1533 -- unless the expression covers a single component, or the
1534 -- expander is inactive.
1536 -- In SPARK mode, expressions that can perform side effects will
1537 -- be recognized by the gnat2why back-end, and the whole
1538 -- subprogram will be ignored. So semantic analysis can be
1539 -- performed safely.
1542 or else not Expander_Active
1543 or else In_Spec_Expression
1545 Analyze_And_Resolve (Expr, Component_Typ);
1546 Check_Expr_OK_In_Limited_Aggregate (Expr);
1547 Check_Non_Static_Context (Expr);
1548 Aggregate_Constraint_Checks (Expr, Component_Typ);
1549 Check_Unset_Reference (Expr);
1553 -- If an aggregate component has a type with predicates, an explicit
1554 -- predicate check must be applied, as for an assignment statement,
1555 -- because the aggregate might not be expanded into individual
1556 -- component assignments. If the expression covers several components
1557 -- the analysis and the predicate check take place later.
1559 if Has_Predicates (Component_Typ)
1560 and then Analyzed (Expr)
1562 Apply_Predicate_Check (Expr, Component_Typ);
1565 if Raises_Constraint_Error (Expr)
1566 and then Nkind (Parent (Expr)) /= N_Component_Association
1568 Set_Raises_Constraint_Error (N);
1571 -- If the expression has been marked as requiring a range check,
1572 -- then generate it here. It's a bit odd to be generating such
1573 -- checks in the analyzer, but harmless since Generate_Range_Check
1574 -- does nothing (other than making sure Do_Range_Check is set) if
1575 -- the expander is not active.
1577 if Do_Range_Check (Expr) then
1578 Generate_Range_Check (Expr, Component_Typ, CE_Range_Check_Failed);
1581 return Resolution_OK;
1582 end Resolve_Aggr_Expr;
1584 --------------------------------------------
1585 -- Resolve_Iterated_Component_Association --
1586 --------------------------------------------
1588 procedure Resolve_Iterated_Component_Association
1590 Index_Typ : Entity_Id)
1592 Loc : constant Source_Ptr := Sloc (N);
1593 Id : constant Entity_Id := Defining_Identifier (N);
1595 -----------------------
1596 -- Remove_References --
1597 -----------------------
1599 function Remove_Ref (N : Node_Id) return Traverse_Result;
1600 -- Remove references to the entity Id after analysis, so it can be
1601 -- properly reanalyzed after construct is expanded into a loop.
1603 function Remove_Ref (N : Node_Id) return Traverse_Result is
1605 if Nkind (N) = N_Identifier
1606 and then Present (Entity (N))
1607 and then Entity (N) = Id
1609 Set_Entity (N, Empty);
1610 Set_Etype (N, Empty);
1612 Set_Analyzed (N, False);
1616 procedure Remove_References is new Traverse_Proc (Remove_Ref);
1625 -- Start of processing for Resolve_Iterated_Component_Association
1628 -- An element iterator specification cannot appear in
1629 -- an array aggregate because it does not provide index
1630 -- values for the association. This must be a semantic
1631 -- check because the parser cannot tell whether this is
1632 -- an array aggregate or a container aggregate.
1634 if Present (Iterator_Specification (N)) then
1635 Error_Msg_N ("container element Iterator cannot appear "
1636 & "in an array aggregate", N);
1640 Choice := First (Discrete_Choices (N));
1642 while Present (Choice) loop
1643 if Nkind (Choice) = N_Others_Choice then
1644 Others_Present := True;
1649 -- Choice can be a subtype name, a range, or an expression
1651 if Is_Entity_Name (Choice)
1652 and then Is_Type (Entity (Choice))
1653 and then Base_Type (Entity (Choice)) = Base_Type (Index_Typ)
1658 Analyze_And_Resolve (Choice, Index_Typ);
1665 -- Create a scope in which to introduce an index, which is usually
1666 -- visible in the expression for the component, and needed for its
1669 Ent := New_Internal_Entity (E_Loop, Current_Scope, Loc, 'L');
1670 Set_Etype (Ent, Standard_Void_Type);
1671 Set_Parent (Ent, Parent (N));
1674 -- Insert and decorate the index variable in the current scope.
1675 -- The expression has to be analyzed once the index variable is
1676 -- directly visible.
1679 Set_Etype (Id, Index_Typ);
1680 Set_Ekind (Id, E_Variable);
1681 Set_Scope (Id, Ent);
1683 -- Analyze the expression without expansion, to verify legality.
1684 -- After analysis we remove references to the index variable because
1685 -- the expression will be analyzed anew when the enclosing aggregate
1686 -- is expanded, and the construct is rewritten as a loop with a new
1689 Expr := Expression (N);
1691 Expander_Mode_Save_And_Set (False);
1692 Dummy := Resolve_Aggr_Expr (Expr, Single_Elmt => False);
1693 Expander_Mode_Restore;
1694 Remove_References (Expr);
1696 -- An iterated_component_association may appear in a nested
1697 -- aggregate for a multidimensional structure: preserve the bounds
1698 -- computed for the expression, as well as the anonymous array
1699 -- type generated for it; both are needed during array expansion.
1701 if Nkind (Expr) = N_Aggregate then
1702 Set_Aggregate_Bounds (Expression (N), Aggregate_Bounds (Expr));
1703 Set_Etype (Expression (N), Etype (Expr));
1707 end Resolve_Iterated_Component_Association;
1716 Aggr_Low : Node_Id := Empty;
1717 Aggr_High : Node_Id := Empty;
1718 -- The actual low and high bounds of this sub-aggregate
1720 Case_Table_Size : Nat;
1721 -- Contains the size of the case table needed to sort aggregate choices
1723 Choices_Low : Node_Id := Empty;
1724 Choices_High : Node_Id := Empty;
1725 -- The lowest and highest discrete choices values for a named aggregate
1727 Delete_Choice : Boolean;
1728 -- Used when replacing a subtype choice with predicate by a list
1730 Nb_Elements : Uint := Uint_0;
1731 -- The number of elements in a positional aggregate
1733 Nb_Discrete_Choices : Nat := 0;
1734 -- The overall number of discrete choices (not counting others choice)
1736 -- Start of processing for Resolve_Array_Aggregate
1739 -- Ignore junk empty aggregate resulting from parser error
1741 if No (Expressions (N))
1742 and then No (Component_Associations (N))
1743 and then not Null_Record_Present (N)
1748 -- STEP 1: make sure the aggregate is correctly formatted
1750 if Present (Component_Associations (N)) then
1751 Assoc := First (Component_Associations (N));
1752 while Present (Assoc) loop
1753 if Nkind (Assoc) = N_Iterated_Component_Association then
1754 Resolve_Iterated_Component_Association (Assoc, Index_Typ);
1757 Choice := First (Choice_List (Assoc));
1758 Delete_Choice := False;
1759 while Present (Choice) loop
1760 if Nkind (Choice) = N_Others_Choice then
1761 Others_Present := True;
1763 if Choice /= First (Choice_List (Assoc))
1764 or else Present (Next (Choice))
1767 ("OTHERS must appear alone in a choice list", Choice);
1771 if Present (Next (Assoc)) then
1773 ("OTHERS must appear last in an aggregate", Choice);
1777 if Ada_Version = Ada_83
1778 and then Assoc /= First (Component_Associations (N))
1779 and then Nkind (Parent (N)) in
1780 N_Assignment_Statement | N_Object_Declaration
1783 ("(Ada 83) illegal context for OTHERS choice", N);
1786 elsif Is_Entity_Name (Choice) then
1790 E : constant Entity_Id := Entity (Choice);
1796 if Is_Type (E) and then Has_Predicates (E) then
1797 Freeze_Before (N, E);
1799 if Has_Dynamic_Predicate_Aspect (E) then
1801 ("subtype& has dynamic predicate, not allowed "
1802 & "in aggregate choice", Choice, E);
1804 elsif not Is_OK_Static_Subtype (E) then
1806 ("non-static subtype& has predicate, not allowed "
1807 & "in aggregate choice", Choice, E);
1810 -- If the subtype has a static predicate, replace the
1811 -- original choice with the list of individual values
1812 -- covered by the predicate.
1813 -- This should be deferred to expansion time ???
1815 if Present (Static_Discrete_Predicate (E)) then
1816 Delete_Choice := True;
1819 P := First (Static_Discrete_Predicate (E));
1820 while Present (P) loop
1822 Set_Sloc (C, Sloc (Choice));
1823 Append_To (New_Cs, C);
1827 Insert_List_After (Choice, New_Cs);
1833 Nb_Choices := Nb_Choices + 1;
1836 C : constant Node_Id := Choice;
1841 if Delete_Choice then
1843 Nb_Choices := Nb_Choices - 1;
1844 Delete_Choice := False;
1853 -- At this point we know that the others choice, if present, is by
1854 -- itself and appears last in the aggregate. Check if we have mixed
1855 -- positional and discrete associations (other than the others choice).
1857 if Present (Expressions (N))
1858 and then (Nb_Choices > 1
1859 or else (Nb_Choices = 1 and then not Others_Present))
1862 ("named association cannot follow positional association",
1863 First (Choice_List (First (Component_Associations (N)))));
1867 -- Test for the validity of an others choice if present
1869 if Others_Present and then not Others_Allowed then
1871 ("OTHERS choice not allowed here",
1872 First (Choice_List (First (Component_Associations (N)))));
1876 -- Protect against cascaded errors
1878 if Etype (Index_Typ) = Any_Type then
1882 -- STEP 2: Process named components
1884 if No (Expressions (N)) then
1885 if Others_Present then
1886 Case_Table_Size := Nb_Choices - 1;
1888 Case_Table_Size := Nb_Choices;
1892 function Empty_Range (A : Node_Id) return Boolean;
1893 -- If an association covers an empty range, some warnings on the
1894 -- expression of the association can be disabled.
1900 function Empty_Range (A : Node_Id) return Boolean is
1901 R : constant Node_Id := First (Choices (A));
1903 return No (Next (R))
1904 and then Nkind (R) = N_Range
1905 and then Compile_Time_Compare
1906 (Low_Bound (R), High_Bound (R), False) = GT;
1913 -- Denote the lowest and highest values in an aggregate choice
1915 S_Low : Node_Id := Empty;
1916 S_High : Node_Id := Empty;
1917 -- if a choice in an aggregate is a subtype indication these
1918 -- denote the lowest and highest values of the subtype
1920 Table : Case_Table_Type (1 .. Case_Table_Size);
1921 -- Used to sort all the different choice values
1923 Single_Choice : Boolean;
1924 -- Set to true every time there is a single discrete choice in a
1925 -- discrete association
1927 Prev_Nb_Discrete_Choices : Nat;
1928 -- Used to keep track of the number of discrete choices in the
1929 -- current association.
1931 Errors_Posted_On_Choices : Boolean := False;
1932 -- Keeps track of whether any choices have semantic errors
1934 -- Start of processing for Step_2
1937 -- STEP 2 (A): Check discrete choices validity
1939 Assoc := First (Component_Associations (N));
1940 while Present (Assoc) loop
1941 Prev_Nb_Discrete_Choices := Nb_Discrete_Choices;
1942 Choice := First (Choice_List (Assoc));
1947 if Nkind (Choice) = N_Others_Choice then
1948 Single_Choice := False;
1951 -- Test for subtype mark without constraint
1953 elsif Is_Entity_Name (Choice) and then
1954 Is_Type (Entity (Choice))
1956 if Base_Type (Entity (Choice)) /= Index_Base then
1958 ("invalid subtype mark in aggregate choice",
1963 -- Case of subtype indication
1965 elsif Nkind (Choice) = N_Subtype_Indication then
1966 Resolve_Discrete_Subtype_Indication (Choice, Index_Base);
1968 if Has_Dynamic_Predicate_Aspect
1969 (Entity (Subtype_Mark (Choice)))
1972 ("subtype& has dynamic predicate, "
1973 & "not allowed in aggregate choice",
1974 Choice, Entity (Subtype_Mark (Choice)));
1977 -- Does the subtype indication evaluation raise CE?
1979 Get_Index_Bounds (Subtype_Mark (Choice), S_Low, S_High);
1980 Get_Index_Bounds (Choice, Low, High);
1981 Check_Bounds (S_Low, S_High, Low, High);
1983 -- Case of range or expression
1986 Resolve (Choice, Index_Base);
1987 Check_Unset_Reference (Choice);
1988 Check_Non_Static_Context (Choice);
1990 -- If semantic errors were posted on the choice, then
1991 -- record that for possible early return from later
1992 -- processing (see handling of enumeration choices).
1994 if Error_Posted (Choice) then
1995 Errors_Posted_On_Choices := True;
1998 -- Do not range check a choice. This check is redundant
1999 -- since this test is already done when we check that the
2000 -- bounds of the array aggregate are within range.
2002 Set_Do_Range_Check (Choice, False);
2005 -- If we could not resolve the discrete choice stop here
2007 if Etype (Choice) = Any_Type then
2010 -- If the discrete choice raises CE get its original bounds
2012 elsif Nkind (Choice) = N_Raise_Constraint_Error then
2013 Set_Raises_Constraint_Error (N);
2014 Get_Index_Bounds (Original_Node (Choice), Low, High);
2016 -- Otherwise get its bounds as usual
2019 Get_Index_Bounds (Choice, Low, High);
2022 if (Dynamic_Or_Null_Range (Low, High)
2023 or else (Nkind (Choice) = N_Subtype_Indication
2025 Dynamic_Or_Null_Range (S_Low, S_High)))
2026 and then Nb_Choices /= 1
2029 ("dynamic or empty choice in aggregate "
2030 & "must be the only choice", Choice);
2034 if not (All_Composite_Constraints_Static (Low)
2035 and then All_Composite_Constraints_Static (High)
2036 and then All_Composite_Constraints_Static (S_Low)
2037 and then All_Composite_Constraints_Static (S_High))
2039 Check_Restriction (No_Dynamic_Sized_Objects, Choice);
2042 Nb_Discrete_Choices := Nb_Discrete_Choices + 1;
2043 Table (Nb_Discrete_Choices).Lo := Low;
2044 Table (Nb_Discrete_Choices).Hi := High;
2045 Table (Nb_Discrete_Choices).Choice := Choice;
2051 -- Check if we have a single discrete choice and whether
2052 -- this discrete choice specifies a single value.
2055 (Nb_Discrete_Choices = Prev_Nb_Discrete_Choices + 1)
2056 and then (Low = High);
2062 -- Ada 2005 (AI-231)
2064 if Ada_Version >= Ada_2005
2065 and then Known_Null (Expression (Assoc))
2066 and then not Empty_Range (Assoc)
2068 Check_Can_Never_Be_Null (Etype (N), Expression (Assoc));
2071 -- Ada 2005 (AI-287): In case of default initialized component
2072 -- we delay the resolution to the expansion phase.
2074 if Box_Present (Assoc) then
2076 -- Ada 2005 (AI-287): In case of default initialization of a
2077 -- component the expander will generate calls to the
2078 -- corresponding initialization subprogram. We need to call
2079 -- Resolve_Aggr_Expr to check the rules about
2082 if not Resolve_Aggr_Expr
2083 (Assoc, Single_Elmt => Single_Choice)
2088 -- ??? Checks for dynamically tagged expressions below will
2089 -- be only applied to iterated_component_association after
2090 -- expansion; in particular, errors might not be reported when
2091 -- -gnatc switch is used.
2093 elsif Nkind (Assoc) = N_Iterated_Component_Association then
2094 null; -- handled above, in a loop context
2096 elsif not Resolve_Aggr_Expr
2097 (Expression (Assoc), Single_Elmt => Single_Choice)
2101 -- Check incorrect use of dynamically tagged expression
2103 -- We differentiate here two cases because the expression may
2104 -- not be decorated. For example, the analysis and resolution
2105 -- of the expression associated with the others choice will be
2106 -- done later with the full aggregate. In such case we
2107 -- duplicate the expression tree to analyze the copy and
2108 -- perform the required check.
2110 elsif not Present (Etype (Expression (Assoc))) then
2112 Save_Analysis : constant Boolean := Full_Analysis;
2113 Expr : constant Node_Id :=
2114 New_Copy_Tree (Expression (Assoc));
2117 Expander_Mode_Save_And_Set (False);
2118 Full_Analysis := False;
2120 -- Analyze the expression, making sure it is properly
2121 -- attached to the tree before we do the analysis.
2123 Set_Parent (Expr, Parent (Expression (Assoc)));
2126 -- Compute its dimensions now, rather than at the end of
2127 -- resolution, because in the case of multidimensional
2128 -- aggregates subsequent expansion may lead to spurious
2131 Check_Expression_Dimensions (Expr, Component_Typ);
2133 -- If the expression is a literal, propagate this info
2134 -- to the expression in the association, to enable some
2135 -- optimizations downstream.
2137 if Is_Entity_Name (Expr)
2138 and then Present (Entity (Expr))
2139 and then Ekind (Entity (Expr)) = E_Enumeration_Literal
2142 (Expression (Assoc), Component_Typ);
2145 Full_Analysis := Save_Analysis;
2146 Expander_Mode_Restore;
2148 if Is_Tagged_Type (Etype (Expr)) then
2149 Check_Dynamically_Tagged_Expression
2151 Typ => Component_Type (Etype (N)),
2156 elsif Is_Tagged_Type (Etype (Expression (Assoc))) then
2157 Check_Dynamically_Tagged_Expression
2158 (Expr => Expression (Assoc),
2159 Typ => Component_Type (Etype (N)),
2166 -- If aggregate contains more than one choice then these must be
2167 -- static. Check for duplicate and missing values.
2169 -- Note: there is duplicated code here wrt Check_Choice_Set in
2170 -- the body of Sem_Case, and it is possible we could just reuse
2171 -- that procedure. To be checked ???
2173 if Nb_Discrete_Choices > 1 then
2174 Check_Choices : declare
2176 -- Location of choice for messages
2180 -- High end of one range and Low end of the next. Should be
2181 -- contiguous if there is no hole in the list of values.
2185 -- End points of duplicated range
2187 Missing_Or_Duplicates : Boolean := False;
2188 -- Set True if missing or duplicate choices found
2190 procedure Output_Bad_Choices (Lo, Hi : Uint; C : Node_Id);
2191 -- Output continuation message with a representation of the
2192 -- bounds (just Lo if Lo = Hi, else Lo .. Hi). C is the
2193 -- choice node where the message is to be posted.
2195 ------------------------
2196 -- Output_Bad_Choices --
2197 ------------------------
2199 procedure Output_Bad_Choices (Lo, Hi : Uint; C : Node_Id) is
2201 -- Enumeration type case
2203 if Is_Enumeration_Type (Index_Typ) then
2205 Chars (Get_Enum_Lit_From_Pos (Index_Typ, Lo, Loc));
2207 Chars (Get_Enum_Lit_From_Pos (Index_Typ, Hi, Loc));
2210 Error_Msg_N ("\\ %!", C);
2212 Error_Msg_N ("\\ % .. %!", C);
2215 -- Integer types case
2218 Error_Msg_Uint_1 := Lo;
2219 Error_Msg_Uint_2 := Hi;
2222 Error_Msg_N ("\\ ^!", C);
2224 Error_Msg_N ("\\ ^ .. ^!", C);
2227 end Output_Bad_Choices;
2229 -- Start of processing for Check_Choices
2232 Sort_Case_Table (Table);
2234 -- First we do a quick linear loop to find out if we have
2235 -- any duplicates or missing entries (usually we have a
2236 -- legal aggregate, so this will get us out quickly).
2238 for J in 1 .. Nb_Discrete_Choices - 1 loop
2239 Hi_Val := Expr_Value (Table (J).Hi);
2240 Lo_Val := Expr_Value (Table (J + 1).Lo);
2243 or else (Lo_Val > Hi_Val + 1
2244 and then not Others_Present)
2246 Missing_Or_Duplicates := True;
2251 -- If we have missing or duplicate entries, first fill in
2252 -- the Highest entries to make life easier in the following
2253 -- loops to detect bad entries.
2255 if Missing_Or_Duplicates then
2256 Table (1).Highest := Expr_Value (Table (1).Hi);
2258 for J in 2 .. Nb_Discrete_Choices loop
2259 Table (J).Highest :=
2261 (Table (J - 1).Highest, Expr_Value (Table (J).Hi));
2264 -- Loop through table entries to find duplicate indexes
2266 for J in 2 .. Nb_Discrete_Choices loop
2267 Lo_Val := Expr_Value (Table (J).Lo);
2268 Hi_Val := Expr_Value (Table (J).Hi);
2270 -- Case where we have duplicates (the lower bound of
2271 -- this choice is less than or equal to the highest
2272 -- high bound found so far).
2274 if Lo_Val <= Table (J - 1).Highest then
2276 -- We move backwards looking for duplicates. We can
2277 -- abandon this loop as soon as we reach a choice
2278 -- highest value that is less than Lo_Val.
2280 for K in reverse 1 .. J - 1 loop
2281 exit when Table (K).Highest < Lo_Val;
2283 -- Here we may have duplicates between entries
2284 -- for K and J. Get range of duplicates.
2287 UI_Max (Lo_Val, Expr_Value (Table (K).Lo));
2289 UI_Min (Hi_Val, Expr_Value (Table (K).Hi));
2291 -- Nothing to do if duplicate range is null
2293 if Lo_Dup > Hi_Dup then
2296 -- Otherwise place proper message
2299 -- We place message on later choice, with a
2300 -- line reference to the earlier choice.
2302 if Sloc (Table (J).Choice) <
2303 Sloc (Table (K).Choice)
2305 Choice := Table (K).Choice;
2306 Error_Msg_Sloc := Sloc (Table (J).Choice);
2308 Choice := Table (J).Choice;
2309 Error_Msg_Sloc := Sloc (Table (K).Choice);
2312 if Lo_Dup = Hi_Dup then
2314 ("index value in array aggregate "
2315 & "duplicates the one given#!", Choice);
2318 ("index values in array aggregate "
2319 & "duplicate those given#!", Choice);
2322 Output_Bad_Choices (Lo_Dup, Hi_Dup, Choice);
2328 -- Loop through entries in table to find missing indexes.
2329 -- Not needed if others, since missing impossible.
2331 if not Others_Present then
2332 for J in 2 .. Nb_Discrete_Choices loop
2333 Lo_Val := Expr_Value (Table (J).Lo);
2334 Hi_Val := Table (J - 1).Highest;
2336 if Lo_Val > Hi_Val + 1 then
2339 Error_Node : Node_Id;
2342 -- If the choice is the bound of a range in
2343 -- a subtype indication, it is not in the
2344 -- source lists for the aggregate itself, so
2345 -- post the error on the aggregate. Otherwise
2346 -- post it on choice itself.
2348 Choice := Table (J).Choice;
2350 if Is_List_Member (Choice) then
2351 Error_Node := Choice;
2356 if Hi_Val + 1 = Lo_Val - 1 then
2358 ("missing index value "
2359 & "in array aggregate!", Error_Node);
2362 ("missing index values "
2363 & "in array aggregate!", Error_Node);
2367 (Hi_Val + 1, Lo_Val - 1, Error_Node);
2373 -- If either missing or duplicate values, return failure
2375 Set_Etype (N, Any_Composite);
2381 -- STEP 2 (B): Compute aggregate bounds and min/max choices values
2383 if Nb_Discrete_Choices > 0 then
2384 Choices_Low := Table (1).Lo;
2385 Choices_High := Table (Nb_Discrete_Choices).Hi;
2388 -- If Others is present, then bounds of aggregate come from the
2389 -- index constraint (not the choices in the aggregate itself).
2391 if Others_Present then
2392 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2394 -- Abandon processing if either bound is already signalled as
2395 -- an error (prevents junk cascaded messages and blow ups).
2397 if Nkind (Aggr_Low) = N_Error
2399 Nkind (Aggr_High) = N_Error
2404 -- No others clause present
2407 -- Special processing if others allowed and not present. This
2408 -- means that the bounds of the aggregate come from the index
2409 -- constraint (and the length must match).
2411 if Others_Allowed then
2412 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2414 -- Abandon processing if either bound is already signalled
2415 -- as an error (stop junk cascaded messages and blow ups).
2417 if Nkind (Aggr_Low) = N_Error
2419 Nkind (Aggr_High) = N_Error
2424 -- If others allowed, and no others present, then the array
2425 -- should cover all index values. If it does not, we will
2426 -- get a length check warning, but there is two cases where
2427 -- an additional warning is useful:
2429 -- If we have no positional components, and the length is
2430 -- wrong (which we can tell by others being allowed with
2431 -- missing components), and the index type is an enumeration
2432 -- type, then issue appropriate warnings about these missing
2433 -- components. They are only warnings, since the aggregate
2434 -- is fine, it's just the wrong length. We skip this check
2435 -- for standard character types (since there are no literals
2436 -- and it is too much trouble to concoct them), and also if
2437 -- any of the bounds have values that are not known at
2440 -- Another case warranting a warning is when the length
2441 -- is right, but as above we have an index type that is
2442 -- an enumeration, and the bounds do not match. This is a
2443 -- case where dubious sliding is allowed and we generate a
2444 -- warning that the bounds do not match.
2446 if No (Expressions (N))
2447 and then Nkind (Index) = N_Range
2448 and then Is_Enumeration_Type (Etype (Index))
2449 and then not Is_Standard_Character_Type (Etype (Index))
2450 and then Compile_Time_Known_Value (Aggr_Low)
2451 and then Compile_Time_Known_Value (Aggr_High)
2452 and then Compile_Time_Known_Value (Choices_Low)
2453 and then Compile_Time_Known_Value (Choices_High)
2455 -- If any of the expressions or range bounds in choices
2456 -- have semantic errors, then do not attempt further
2457 -- resolution, to prevent cascaded errors.
2459 if Errors_Posted_On_Choices then
2464 ALo : constant Node_Id := Expr_Value_E (Aggr_Low);
2465 AHi : constant Node_Id := Expr_Value_E (Aggr_High);
2466 CLo : constant Node_Id := Expr_Value_E (Choices_Low);
2467 CHi : constant Node_Id := Expr_Value_E (Choices_High);
2472 -- Warning case 1, missing values at start/end. Only
2473 -- do the check if the number of entries is too small.
2475 if (Enumeration_Pos (CHi) - Enumeration_Pos (CLo))
2477 (Enumeration_Pos (AHi) - Enumeration_Pos (ALo))
2480 ("missing index value(s) in array aggregate??",
2483 -- Output missing value(s) at start
2485 if Chars (ALo) /= Chars (CLo) then
2488 if Chars (ALo) = Chars (Ent) then
2489 Error_Msg_Name_1 := Chars (ALo);
2490 Error_Msg_N ("\ %??", N);
2492 Error_Msg_Name_1 := Chars (ALo);
2493 Error_Msg_Name_2 := Chars (Ent);
2494 Error_Msg_N ("\ % .. %??", N);
2498 -- Output missing value(s) at end
2500 if Chars (AHi) /= Chars (CHi) then
2503 if Chars (AHi) = Chars (Ent) then
2504 Error_Msg_Name_1 := Chars (Ent);
2505 Error_Msg_N ("\ %??", N);
2507 Error_Msg_Name_1 := Chars (Ent);
2508 Error_Msg_Name_2 := Chars (AHi);
2509 Error_Msg_N ("\ % .. %??", N);
2513 -- Warning case 2, dubious sliding. The First_Subtype
2514 -- test distinguishes between a constrained type where
2515 -- sliding is not allowed (so we will get a warning
2516 -- later that Constraint_Error will be raised), and
2517 -- the unconstrained case where sliding is permitted.
2519 elsif (Enumeration_Pos (CHi) - Enumeration_Pos (CLo))
2521 (Enumeration_Pos (AHi) - Enumeration_Pos (ALo))
2522 and then Chars (ALo) /= Chars (CLo)
2524 not Is_Constrained (First_Subtype (Etype (N)))
2527 ("bounds of aggregate do not match target??", N);
2533 -- If no others, aggregate bounds come from aggregate
2535 Aggr_Low := Choices_Low;
2536 Aggr_High := Choices_High;
2540 -- STEP 3: Process positional components
2543 -- STEP 3 (A): Process positional elements
2545 Expr := First (Expressions (N));
2546 Nb_Elements := Uint_0;
2547 while Present (Expr) loop
2548 Nb_Elements := Nb_Elements + 1;
2550 -- Ada 2005 (AI-231)
2552 if Ada_Version >= Ada_2005 and then Known_Null (Expr) then
2553 Check_Can_Never_Be_Null (Etype (N), Expr);
2556 if not Resolve_Aggr_Expr (Expr, Single_Elmt => True) then
2560 -- Check incorrect use of dynamically tagged expression
2562 if Is_Tagged_Type (Etype (Expr)) then
2563 Check_Dynamically_Tagged_Expression
2565 Typ => Component_Type (Etype (N)),
2572 if Others_Present then
2573 Assoc := Last (Component_Associations (N));
2575 -- Ada 2005 (AI-231)
2577 if Ada_Version >= Ada_2005 and then Known_Null (Assoc) then
2578 Check_Can_Never_Be_Null (Etype (N), Expression (Assoc));
2581 -- Ada 2005 (AI-287): In case of default initialized component,
2582 -- we delay the resolution to the expansion phase.
2584 if Box_Present (Assoc) then
2586 -- Ada 2005 (AI-287): In case of default initialization of a
2587 -- component the expander will generate calls to the
2588 -- corresponding initialization subprogram. We need to call
2589 -- Resolve_Aggr_Expr to check the rules about
2592 if not Resolve_Aggr_Expr (Assoc, Single_Elmt => False) then
2596 elsif not Resolve_Aggr_Expr (Expression (Assoc),
2597 Single_Elmt => False)
2601 -- Check incorrect use of dynamically tagged expression. The
2602 -- expression of the others choice has not been resolved yet.
2603 -- In order to diagnose the semantic error we create a duplicate
2604 -- tree to analyze it and perform the check.
2606 elsif Nkind (Assoc) /= N_Iterated_Component_Association then
2608 Save_Analysis : constant Boolean := Full_Analysis;
2609 Expr : constant Node_Id :=
2610 New_Copy_Tree (Expression (Assoc));
2613 Expander_Mode_Save_And_Set (False);
2614 Full_Analysis := False;
2616 Full_Analysis := Save_Analysis;
2617 Expander_Mode_Restore;
2619 if Is_Tagged_Type (Etype (Expr)) then
2620 Check_Dynamically_Tagged_Expression
2622 Typ => Component_Type (Etype (N)),
2629 -- STEP 3 (B): Compute the aggregate bounds
2631 if Others_Present then
2632 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2635 if Others_Allowed then
2636 Get_Index_Bounds (Index_Constr, Aggr_Low, Discard);
2638 Aggr_Low := Index_Typ_Low;
2641 Aggr_High := Add (Nb_Elements - 1, To => Aggr_Low);
2642 Check_Bound (Index_Base_High, Aggr_High);
2646 -- STEP 4: Perform static aggregate checks and save the bounds
2650 Check_Bounds (Index_Typ_Low, Index_Typ_High, Aggr_Low, Aggr_High);
2651 Check_Bounds (Index_Base_Low, Index_Base_High, Aggr_Low, Aggr_High);
2655 if Others_Present and then Nb_Discrete_Choices > 0 then
2656 Check_Bounds (Aggr_Low, Aggr_High, Choices_Low, Choices_High);
2657 Check_Bounds (Index_Typ_Low, Index_Typ_High,
2658 Choices_Low, Choices_High);
2659 Check_Bounds (Index_Base_Low, Index_Base_High,
2660 Choices_Low, Choices_High);
2664 elsif Others_Present and then Nb_Elements > 0 then
2665 Check_Length (Aggr_Low, Aggr_High, Nb_Elements);
2666 Check_Length (Index_Typ_Low, Index_Typ_High, Nb_Elements);
2667 Check_Length (Index_Base_Low, Index_Base_High, Nb_Elements);
2670 if Raises_Constraint_Error (Aggr_Low)
2671 or else Raises_Constraint_Error (Aggr_High)
2673 Set_Raises_Constraint_Error (N);
2676 Aggr_Low := Duplicate_Subexpr (Aggr_Low);
2678 -- Do not duplicate Aggr_High if Aggr_High = Aggr_Low + Nb_Elements
2679 -- since the addition node returned by Add is not yet analyzed. Attach
2680 -- to tree and analyze first. Reset analyzed flag to ensure it will get
2681 -- analyzed when it is a literal bound whose type must be properly set.
2683 if Others_Present or else Nb_Discrete_Choices > 0 then
2684 Aggr_High := Duplicate_Subexpr (Aggr_High);
2686 if Etype (Aggr_High) = Universal_Integer then
2687 Set_Analyzed (Aggr_High, False);
2691 -- If the aggregate already has bounds attached to it, it means this is
2692 -- a positional aggregate created as an optimization by
2693 -- Exp_Aggr.Convert_To_Positional, so we don't want to change those
2696 if Present (Aggregate_Bounds (N)) and then not Others_Allowed then
2697 Aggr_Low := Low_Bound (Aggregate_Bounds (N));
2698 Aggr_High := High_Bound (Aggregate_Bounds (N));
2701 Set_Aggregate_Bounds
2702 (N, Make_Range (Loc, Low_Bound => Aggr_Low, High_Bound => Aggr_High));
2704 -- The bounds may contain expressions that must be inserted upwards.
2705 -- Attach them fully to the tree. After analysis, remove side effects
2706 -- from upper bound, if still needed.
2708 Set_Parent (Aggregate_Bounds (N), N);
2709 Analyze_And_Resolve (Aggregate_Bounds (N), Index_Typ);
2710 Check_Unset_Reference (Aggregate_Bounds (N));
2712 if not Others_Present and then Nb_Discrete_Choices = 0 then
2714 (Aggregate_Bounds (N),
2715 Duplicate_Subexpr (High_Bound (Aggregate_Bounds (N))));
2718 -- Check the dimensions of each component in the array aggregate
2720 Analyze_Dimension_Array_Aggregate (N, Component_Typ);
2723 end Resolve_Array_Aggregate;
2725 ---------------------------------
2726 -- Resolve_Container_Aggregate --
2727 ---------------------------------
2729 procedure Resolve_Container_Aggregate (N : Node_Id; Typ : Entity_Id) is
2730 procedure Resolve_Iterated_Association
2732 Key_Type : Entity_Id;
2733 Elmt_Type : Entity_Id);
2734 -- Resolve choices and expression in an iterated component association
2735 -- or an iterated element association, which has a key_expression.
2736 -- This is similar but not identical to the handling of this construct
2737 -- in an array aggregate.
2738 -- For a named container, the type of each choice must be compatible
2739 -- with the key type. For a positional container, the choice must be
2740 -- a subtype indication or an iterator specification that determines
2743 Asp : constant Node_Id := Find_Value_Of_Aspect (Typ, Aspect_Aggregate);
2745 Empty_Subp : Node_Id := Empty;
2746 Add_Named_Subp : Node_Id := Empty;
2747 Add_Unnamed_Subp : Node_Id := Empty;
2748 New_Indexed_Subp : Node_Id := Empty;
2749 Assign_Indexed_Subp : Node_Id := Empty;
2751 ----------------------------------
2752 -- Resolve_Iterated_Association --
2753 ----------------------------------
2755 procedure Resolve_Iterated_Association
2757 Key_Type : Entity_Id;
2758 Elmt_Type : Entity_Id)
2767 Typ : Entity_Id := Empty;
2770 -- If this is an Iterated_Element_Association then either a
2771 -- an Iterator_Specification or a Loop_Parameter specification
2772 -- is present. In both cases a Key_Expression is present.
2774 if Nkind (Comp) = N_Iterated_Element_Association then
2775 if Present (Loop_Parameter_Specification (Comp)) then
2776 Analyze_Loop_Parameter_Specification
2777 (Loop_Parameter_Specification (Comp));
2778 Id_Name := Chars (Defining_Identifier
2779 (Loop_Parameter_Specification (Comp)));
2781 Iter := Copy_Separate_Tree (Iterator_Specification (Comp));
2783 Typ := Etype (Defining_Identifier (Iter));
2784 Id_Name := Chars (Defining_Identifier
2785 (Iterator_Specification (Comp)));
2788 -- Key expression must have the type of the key. We analyze
2789 -- a copy of the original expression, because it will be
2790 -- reanalyzed and copied as needed during expansion of the
2791 -- corresponding loop.
2793 Key_Expr := Key_Expression (Comp);
2794 Analyze_And_Resolve (New_Copy_Tree (Key_Expr), Key_Type);
2796 elsif Present (Iterator_Specification (Comp)) then
2797 Iter := Copy_Separate_Tree (Iterator_Specification (Comp));
2798 Id_Name := Chars (Defining_Identifier (Comp));
2800 Typ := Etype (Defining_Identifier (Iter));
2803 Choice := First (Discrete_Choices (Comp));
2805 while Present (Choice) loop
2808 -- Choice can be a subtype name, a range, or an expression
2810 if Is_Entity_Name (Choice)
2811 and then Is_Type (Entity (Choice))
2812 and then Base_Type (Entity (Choice)) = Base_Type (Key_Type)
2816 elsif Present (Key_Type) then
2817 Analyze_And_Resolve (Choice, Key_Type);
2820 Typ := Etype (Choice); -- assume unique for now
2826 Id_Name := Chars (Defining_Identifier (Comp));
2829 -- Create a scope in which to introduce an index, which is usually
2830 -- visible in the expression for the component, and needed for its
2833 Id := Make_Defining_Identifier (Sloc (Comp), Id_Name);
2834 Ent := New_Internal_Entity (E_Loop, Current_Scope, Sloc (Comp), 'L');
2835 Set_Etype (Ent, Standard_Void_Type);
2836 Set_Parent (Ent, Parent (Comp));
2839 -- Insert and decorate the loop variable in the current scope.
2840 -- The expression has to be analyzed once the loop variable is
2841 -- directly visible. Mark the variable as referenced to prevent
2842 -- spurious warnings, given that subsequent uses of its name in the
2843 -- expression will reference the internal (synonym) loop variable.
2847 if No (Key_Type) then
2848 pragma Assert (Present (Typ));
2849 Set_Etype (Id, Typ);
2851 Set_Etype (Id, Key_Type);
2854 Set_Ekind (Id, E_Variable);
2855 Set_Scope (Id, Ent);
2856 Set_Referenced (Id);
2858 -- Analyze a copy of the expression, to verify legality. We use
2859 -- a copy because the expression will be analyzed anew when the
2860 -- enclosing aggregate is expanded, and the construct is rewritten
2861 -- as a loop with a new index variable.
2863 Expr := New_Copy_Tree (Expression (Comp));
2864 Preanalyze_And_Resolve (Expr, Elmt_Type);
2867 end Resolve_Iterated_Association;
2870 pragma Assert (Nkind (Asp) = N_Aggregate);
2873 Parse_Aspect_Aggregate (Asp,
2874 Empty_Subp, Add_Named_Subp, Add_Unnamed_Subp,
2875 New_Indexed_Subp, Assign_Indexed_Subp);
2877 if Present (Add_Unnamed_Subp)
2878 and then No (New_Indexed_Subp)
2881 Elmt_Type : constant Entity_Id :=
2883 (First_Formal (Entity (Add_Unnamed_Subp))));
2887 if Present (Expressions (N)) then
2888 -- positional aggregate
2890 Comp := First (Expressions (N));
2891 while Present (Comp) loop
2892 Analyze_And_Resolve (Comp, Elmt_Type);
2897 -- Empty aggregate, to be replaced by Empty during
2898 -- expansion, or iterated component association.
2900 if Present (Component_Associations (N)) then
2902 Comp : Node_Id := First (Component_Associations (N));
2904 while Present (Comp) loop
2906 N_Iterated_Component_Association
2908 Error_Msg_N ("illegal component association "
2909 & "for unnamed container aggregate", Comp);
2912 Resolve_Iterated_Association
2913 (Comp, Empty, Elmt_Type);
2922 elsif Present (Add_Named_Subp) then
2924 -- Retrieves types of container, key, and element from the
2925 -- specified insertion procedure.
2927 Container : constant Entity_Id :=
2928 First_Formal (Entity (Add_Named_Subp));
2929 Key_Type : constant Entity_Id := Etype (Next_Formal (Container));
2930 Elmt_Type : constant Entity_Id :=
2931 Etype (Next_Formal (Next_Formal (Container)));
2936 Comp := First (Component_Associations (N));
2937 while Present (Comp) loop
2938 if Nkind (Comp) = N_Component_Association then
2939 Choice := First (Choices (Comp));
2941 while Present (Choice) loop
2942 Analyze_And_Resolve (Choice, Key_Type);
2943 if not Is_Static_Expression (Choice) then
2944 Error_Msg_N ("Choice must be static", Choice);
2950 Analyze_And_Resolve (Expression (Comp), Elmt_Type);
2952 elsif Nkind (Comp) in
2953 N_Iterated_Component_Association |
2954 N_Iterated_Element_Association
2956 Resolve_Iterated_Association
2957 (Comp, Key_Type, Elmt_Type);
2965 -- Indexed Aggregate. Positional or indexed component
2966 -- can be present, but not both. Choices must be static
2967 -- values or ranges with static bounds.
2970 Container : constant Entity_Id :=
2971 First_Formal (Entity (Assign_Indexed_Subp));
2972 Index_Type : constant Entity_Id := Etype (Next_Formal (Container));
2973 Comp_Type : constant Entity_Id :=
2974 Etype (Next_Formal (Next_Formal (Container)));
2979 if Present (Expressions (N)) then
2980 Comp := First (Expressions (N));
2981 while Present (Comp) loop
2982 Analyze_And_Resolve (Comp, Comp_Type);
2987 if Present (Component_Associations (N)) then
2988 if Present (Expressions (N)) then
2989 Error_Msg_N ("Container aggregate cannot be "
2990 & "both positional and named", N);
2994 Comp := First (Expressions (N));
2996 while Present (Comp) loop
2997 if Nkind (Comp) = N_Component_Association then
2998 Choice := First (Choices (Comp));
3000 while Present (Choice) loop
3001 Analyze_And_Resolve (Choice, Index_Type);
3005 Analyze_And_Resolve (Expression (Comp), Comp_Type);
3007 elsif Nkind (Comp) in
3008 N_Iterated_Component_Association |
3009 N_Iterated_Element_Association
3011 Resolve_Iterated_Association
3012 (Comp, Index_Type, Comp_Type);
3020 end Resolve_Container_Aggregate;
3022 -----------------------------
3023 -- Resolve_Delta_Aggregate --
3024 -----------------------------
3026 procedure Resolve_Delta_Aggregate (N : Node_Id; Typ : Entity_Id) is
3027 Base : constant Node_Id := Expression (N);
3030 if Ada_Version < Ada_2020 then
3031 Error_Msg_N ("delta_aggregate is an Ada 202x feature", N);
3032 Error_Msg_N ("\compile with -gnat2020", N);
3035 if not Is_Composite_Type (Typ) then
3036 Error_Msg_N ("not a composite type", N);
3039 Analyze_And_Resolve (Base, Typ);
3041 if Is_Array_Type (Typ) then
3042 Resolve_Delta_Array_Aggregate (N, Typ);
3044 Resolve_Delta_Record_Aggregate (N, Typ);
3048 end Resolve_Delta_Aggregate;
3050 -----------------------------------
3051 -- Resolve_Delta_Array_Aggregate --
3052 -----------------------------------
3054 procedure Resolve_Delta_Array_Aggregate (N : Node_Id; Typ : Entity_Id) is
3055 Deltas : constant List_Id := Component_Associations (N);
3056 Index_Type : constant Entity_Id := Etype (First_Index (Typ));
3063 Assoc := First (Deltas);
3064 while Present (Assoc) loop
3065 if Nkind (Assoc) = N_Iterated_Component_Association then
3066 Choice := First (Choice_List (Assoc));
3067 while Present (Choice) loop
3068 if Nkind (Choice) = N_Others_Choice then
3070 ("others not allowed in delta aggregate", Choice);
3073 Analyze_And_Resolve (Choice, Index_Type);
3080 Id : constant Entity_Id := Defining_Identifier (Assoc);
3081 Ent : constant Entity_Id :=
3083 (E_Loop, Current_Scope, Sloc (Assoc), 'L');
3086 Set_Etype (Ent, Standard_Void_Type);
3087 Set_Parent (Ent, Assoc);
3090 if No (Scope (Id)) then
3091 Set_Etype (Id, Index_Type);
3092 Set_Ekind (Id, E_Variable);
3093 Set_Scope (Id, Ent);
3097 -- Resolve a copy of the expression, after setting
3098 -- its parent properly to preserve its context.
3100 Expr := New_Copy_Tree (Expression (Assoc));
3101 Set_Parent (Expr, Assoc);
3102 Analyze_And_Resolve (Expr, Component_Type (Typ));
3107 Choice := First (Choice_List (Assoc));
3108 while Present (Choice) loop
3109 if Nkind (Choice) = N_Others_Choice then
3111 ("others not allowed in delta aggregate", Choice);
3116 if Is_Entity_Name (Choice)
3117 and then Is_Type (Entity (Choice))
3119 -- Choice covers a range of values
3121 if Base_Type (Entity (Choice)) /=
3122 Base_Type (Index_Type)
3125 ("choice does not match index type of &",
3129 Resolve (Choice, Index_Type);
3136 Analyze_And_Resolve (Expression (Assoc), Component_Type (Typ));
3141 end Resolve_Delta_Array_Aggregate;
3143 ------------------------------------
3144 -- Resolve_Delta_Record_Aggregate --
3145 ------------------------------------
3147 procedure Resolve_Delta_Record_Aggregate (N : Node_Id; Typ : Entity_Id) is
3149 -- Variables used to verify that discriminant-dependent components
3150 -- appear in the same variant.
3152 Comp_Ref : Entity_Id := Empty; -- init to avoid warning
3155 procedure Check_Variant (Id : Entity_Id);
3156 -- If a given component of the delta aggregate appears in a variant
3157 -- part, verify that it is within the same variant as that of previous
3158 -- specified variant components of the delta.
3160 function Get_Component (Nam : Node_Id) return Entity_Id;
3161 -- Locate component with a given name and return it. If none found then
3162 -- report error and return Empty.
3164 function Nested_In (V1 : Node_Id; V2 : Node_Id) return Boolean;
3165 -- Determine whether variant V1 is within variant V2
3167 function Variant_Depth (N : Node_Id) return Integer;
3168 -- Determine the distance of a variant to the enclosing type
3171 --------------------
3173 --------------------
3175 procedure Check_Variant (Id : Entity_Id) is
3177 Comp_Variant : Node_Id;
3180 if not Has_Discriminants (Typ) then
3184 Comp := First_Entity (Typ);
3185 while Present (Comp) loop
3186 exit when Chars (Comp) = Chars (Id);
3187 Next_Component (Comp);
3190 -- Find the variant, if any, whose component list includes the
3191 -- component declaration.
3193 Comp_Variant := Parent (Parent (List_Containing (Parent (Comp))));
3194 if Nkind (Comp_Variant) = N_Variant then
3195 if No (Variant) then
3196 Variant := Comp_Variant;
3199 elsif Variant /= Comp_Variant then
3201 D1 : constant Integer := Variant_Depth (Variant);
3202 D2 : constant Integer := Variant_Depth (Comp_Variant);
3207 (D1 > D2 and then not Nested_In (Variant, Comp_Variant))
3209 (D2 > D1 and then not Nested_In (Comp_Variant, Variant))
3211 pragma Assert (Present (Comp_Ref));
3212 Error_Msg_Node_2 := Comp_Ref;
3214 ("& and & appear in different variants", Id, Comp);
3216 -- Otherwise retain the deeper variant for subsequent tests
3219 Variant := Comp_Variant;
3230 function Get_Component (Nam : Node_Id) return Entity_Id is
3234 Comp := First_Entity (Typ);
3235 while Present (Comp) loop
3236 if Chars (Comp) = Chars (Nam) then
3237 if Ekind (Comp) = E_Discriminant then
3238 Error_Msg_N ("delta cannot apply to discriminant", Nam);
3247 Error_Msg_NE ("type& has no component with this name", Nam, Typ);
3255 function Nested_In (V1, V2 : Node_Id) return Boolean is
3260 while Nkind (Par) /= N_Full_Type_Declaration loop
3265 Par := Parent (Par);
3275 function Variant_Depth (N : Node_Id) return Integer is
3282 while Nkind (Par) /= N_Full_Type_Declaration loop
3284 Par := Parent (Par);
3292 Deltas : constant List_Id := Component_Associations (N);
3297 Comp_Type : Entity_Id := Empty; -- init to avoid warning
3299 -- Start of processing for Resolve_Delta_Record_Aggregate
3304 Assoc := First (Deltas);
3305 while Present (Assoc) loop
3306 Choice := First (Choice_List (Assoc));
3307 while Present (Choice) loop
3308 Comp := Get_Component (Choice);
3310 if Present (Comp) then
3311 Check_Variant (Choice);
3313 Comp_Type := Etype (Comp);
3315 -- Decorate the component reference by setting its entity and
3316 -- type, as otherwise backends like GNATprove would have to
3317 -- rediscover this information by themselves.
3319 Set_Entity (Choice, Comp);
3320 Set_Etype (Choice, Comp_Type);
3322 Comp_Type := Any_Type;
3328 pragma Assert (Present (Comp_Type));
3329 Analyze_And_Resolve (Expression (Assoc), Comp_Type);
3332 end Resolve_Delta_Record_Aggregate;
3334 ---------------------------------
3335 -- Resolve_Extension_Aggregate --
3336 ---------------------------------
3338 -- There are two cases to consider:
3340 -- a) If the ancestor part is a type mark, the components needed are the
3341 -- difference between the components of the expected type and the
3342 -- components of the given type mark.
3344 -- b) If the ancestor part is an expression, it must be unambiguous, and
3345 -- once we have its type we can also compute the needed components as in
3346 -- the previous case. In both cases, if the ancestor type is not the
3347 -- immediate ancestor, we have to build this ancestor recursively.
3349 -- In both cases, discriminants of the ancestor type do not play a role in
3350 -- the resolution of the needed components, because inherited discriminants
3351 -- cannot be used in a type extension. As a result we can compute
3352 -- independently the list of components of the ancestor type and of the
3355 procedure Resolve_Extension_Aggregate (N : Node_Id; Typ : Entity_Id) is
3356 A : constant Node_Id := Ancestor_Part (N);
3361 function Valid_Limited_Ancestor (Anc : Node_Id) return Boolean;
3362 -- If the type is limited, verify that the ancestor part is a legal
3363 -- expression (aggregate or function call, including 'Input)) that does
3364 -- not require a copy, as specified in 7.5(2).
3366 function Valid_Ancestor_Type return Boolean;
3367 -- Verify that the type of the ancestor part is a non-private ancestor
3368 -- of the expected type, which must be a type extension.
3370 procedure Transform_BIP_Assignment (Typ : Entity_Id);
3371 -- For an extension aggregate whose ancestor part is a build-in-place
3372 -- call returning a nonlimited type, this is used to transform the
3373 -- assignment to the ancestor part to use a temp.
3375 ----------------------------
3376 -- Valid_Limited_Ancestor --
3377 ----------------------------
3379 function Valid_Limited_Ancestor (Anc : Node_Id) return Boolean is
3381 if Is_Entity_Name (Anc) and then Is_Type (Entity (Anc)) then
3384 -- The ancestor must be a call or an aggregate, but a call may
3385 -- have been expanded into a temporary, so check original node.
3387 elsif Nkind (Anc) in N_Aggregate
3388 | N_Extension_Aggregate
3393 elsif Nkind (Original_Node (Anc)) = N_Function_Call then
3396 elsif Nkind (Anc) = N_Attribute_Reference
3397 and then Attribute_Name (Anc) = Name_Input
3401 elsif Nkind (Anc) = N_Qualified_Expression then
3402 return Valid_Limited_Ancestor (Expression (Anc));
3404 elsif Nkind (Anc) = N_Raise_Expression then
3410 end Valid_Limited_Ancestor;
3412 -------------------------
3413 -- Valid_Ancestor_Type --
3414 -------------------------
3416 function Valid_Ancestor_Type return Boolean is
3417 Imm_Type : Entity_Id;
3420 Imm_Type := Base_Type (Typ);
3421 while Is_Derived_Type (Imm_Type) loop
3422 if Etype (Imm_Type) = Base_Type (A_Type) then
3425 -- The base type of the parent type may appear as a private
3426 -- extension if it is declared as such in a parent unit of the
3427 -- current one. For consistency of the subsequent analysis use
3428 -- the partial view for the ancestor part.
3430 elsif Is_Private_Type (Etype (Imm_Type))
3431 and then Present (Full_View (Etype (Imm_Type)))
3432 and then Base_Type (A_Type) = Full_View (Etype (Imm_Type))
3434 A_Type := Etype (Imm_Type);
3437 -- The parent type may be a private extension. The aggregate is
3438 -- legal if the type of the aggregate is an extension of it that
3439 -- is not a private extension.
3441 elsif Is_Private_Type (A_Type)
3442 and then not Is_Private_Type (Imm_Type)
3443 and then Present (Full_View (A_Type))
3444 and then Base_Type (Full_View (A_Type)) = Etype (Imm_Type)
3448 -- The parent type may be a raise expression (which is legal in
3449 -- any expression context).
3451 elsif A_Type = Raise_Type then
3452 A_Type := Etype (Imm_Type);
3456 Imm_Type := Etype (Base_Type (Imm_Type));
3460 -- If previous loop did not find a proper ancestor, report error
3462 Error_Msg_NE ("expect ancestor type of &", A, Typ);
3464 end Valid_Ancestor_Type;
3466 ------------------------------
3467 -- Transform_BIP_Assignment --
3468 ------------------------------
3470 procedure Transform_BIP_Assignment (Typ : Entity_Id) is
3471 Loc : constant Source_Ptr := Sloc (N);
3472 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'Y', A);
3473 Obj_Decl : constant Node_Id :=
3474 Make_Object_Declaration (Loc,
3475 Defining_Identifier => Def_Id,
3476 Constant_Present => True,
3477 Object_Definition => New_Occurrence_Of (Typ, Loc),
3479 Has_Init_Expression => True);
3481 Set_Etype (Def_Id, Typ);
3482 Set_Ancestor_Part (N, New_Occurrence_Of (Def_Id, Loc));
3483 Insert_Action (N, Obj_Decl);
3484 end Transform_BIP_Assignment;
3486 -- Start of processing for Resolve_Extension_Aggregate
3489 -- Analyze the ancestor part and account for the case where it is a
3490 -- parameterless function call.
3493 Check_Parameterless_Call (A);
3495 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
3497 -- AI05-0115: If the ancestor part is a subtype mark, the ancestor
3498 -- must not have unknown discriminants. To catch cases where the
3499 -- aggregate occurs at a place where the full view of the ancestor
3500 -- type is visible and doesn't have unknown discriminants, but the
3501 -- aggregate type was derived from a partial view that has unknown
3502 -- discriminants, we check whether the aggregate type has unknown
3503 -- discriminants (unknown discriminants were inherited), along
3504 -- with checking that the partial view of the ancestor has unknown
3505 -- discriminants. (It might be sufficient to replace the entire
3506 -- condition with Has_Unknown_Discriminants (Typ), but that might
3507 -- miss some cases, not clear, and causes error changes in some tests
3508 -- such as class-wide cases, that aren't clearly improvements. ???)
3510 if Has_Unknown_Discriminants (Entity (A))
3511 or else (Has_Unknown_Discriminants (Typ)
3512 and then Partial_View_Has_Unknown_Discr (Entity (A)))
3515 ("aggregate not available for type& whose ancestor "
3516 & "has unknown discriminants", N, Typ);
3520 if not Is_Tagged_Type (Typ) then
3521 Error_Msg_N ("type of extension aggregate must be tagged", N);
3524 elsif Is_Limited_Type (Typ) then
3526 -- Ada 2005 (AI-287): Limited aggregates are allowed
3528 if Ada_Version < Ada_2005 then
3529 Error_Msg_N ("aggregate type cannot be limited", N);
3530 Explain_Limited_Type (Typ, N);
3533 elsif Valid_Limited_Ancestor (A) then
3538 ("limited ancestor part must be aggregate or function call", A);
3541 elsif Is_Class_Wide_Type (Typ) then
3542 Error_Msg_N ("aggregate cannot be of a class-wide type", N);
3546 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
3547 A_Type := Get_Full_View (Entity (A));
3549 if Valid_Ancestor_Type then
3550 Set_Entity (A, A_Type);
3551 Set_Etype (A, A_Type);
3553 Validate_Ancestor_Part (N);
3554 Resolve_Record_Aggregate (N, Typ);
3557 elsif Nkind (A) /= N_Aggregate then
3558 if Is_Overloaded (A) then
3561 Get_First_Interp (A, I, It);
3562 while Present (It.Typ) loop
3564 -- Consider limited interpretations if Ada 2005 or higher
3566 if Is_Tagged_Type (It.Typ)
3567 and then (Ada_Version >= Ada_2005
3568 or else not Is_Limited_Type (It.Typ))
3570 if A_Type /= Any_Type then
3571 Error_Msg_N ("cannot resolve expression", A);
3578 Get_Next_Interp (I, It);
3581 if A_Type = Any_Type then
3582 if Ada_Version >= Ada_2005 then
3584 ("ancestor part must be of a tagged type", A);
3587 ("ancestor part must be of a nonlimited tagged type", A);
3594 A_Type := Etype (A);
3597 if Valid_Ancestor_Type then
3598 Resolve (A, A_Type);
3599 Check_Unset_Reference (A);
3600 Check_Non_Static_Context (A);
3602 -- The aggregate is illegal if the ancestor expression is a call
3603 -- to a function with a limited unconstrained result, unless the
3604 -- type of the aggregate is a null extension. This restriction
3605 -- was added in AI05-67 to simplify implementation.
3607 if Nkind (A) = N_Function_Call
3608 and then Is_Limited_Type (A_Type)
3609 and then not Is_Null_Extension (Typ)
3610 and then not Is_Constrained (A_Type)
3613 ("type of limited ancestor part must be constrained", A);
3615 -- Reject the use of CPP constructors that leave objects partially
3616 -- initialized. For example:
3618 -- type CPP_Root is tagged limited record ...
3619 -- pragma Import (CPP, CPP_Root);
3621 -- type CPP_DT is new CPP_Root and Iface ...
3622 -- pragma Import (CPP, CPP_DT);
3624 -- type Ada_DT is new CPP_DT with ...
3626 -- Obj : Ada_DT := Ada_DT'(New_CPP_Root with others => <>);
3628 -- Using the constructor of CPP_Root the slots of the dispatch
3629 -- table of CPP_DT cannot be set, and the secondary tag of
3630 -- CPP_DT is unknown.
3632 elsif Nkind (A) = N_Function_Call
3633 and then Is_CPP_Constructor_Call (A)
3634 and then Enclosing_CPP_Parent (Typ) /= A_Type
3637 ("??must use 'C'P'P constructor for type &", A,
3638 Enclosing_CPP_Parent (Typ));
3640 -- The following call is not needed if the previous warning
3641 -- is promoted to an error.
3643 Resolve_Record_Aggregate (N, Typ);
3645 elsif Is_Class_Wide_Type (Etype (A))
3646 and then Nkind (Original_Node (A)) = N_Function_Call
3648 -- If the ancestor part is a dispatching call, it appears
3649 -- statically to be a legal ancestor, but it yields any member
3650 -- of the class, and it is not possible to determine whether
3651 -- it is an ancestor of the extension aggregate (much less
3652 -- which ancestor). It is not possible to determine the
3653 -- components of the extension part.
3655 -- This check implements AI-306, which in fact was motivated by
3656 -- an AdaCore query to the ARG after this test was added.
3658 Error_Msg_N ("ancestor part must be statically tagged", A);
3660 -- We are using the build-in-place protocol, but we can't build
3661 -- in place, because we need to call the function before
3662 -- allocating the aggregate. Could do better for null
3663 -- extensions, and maybe for nondiscriminated types.
3664 -- This is wrong for limited, but those were wrong already.
3666 if not Is_Limited_View (A_Type)
3667 and then Is_Build_In_Place_Function_Call (A)
3669 Transform_BIP_Assignment (A_Type);
3672 Resolve_Record_Aggregate (N, Typ);
3677 Error_Msg_N ("no unique type for this aggregate", A);
3680 Check_Function_Writable_Actuals (N);
3681 end Resolve_Extension_Aggregate;
3683 ------------------------------
3684 -- Resolve_Record_Aggregate --
3685 ------------------------------
3687 procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id) is
3688 New_Assoc_List : constant List_Id := New_List;
3689 -- New_Assoc_List is the newly built list of N_Component_Association
3692 Others_Etype : Entity_Id := Empty;
3693 -- This variable is used to save the Etype of the last record component
3694 -- that takes its value from the others choice. Its purpose is:
3696 -- (a) make sure the others choice is useful
3698 -- (b) make sure the type of all the components whose value is
3699 -- subsumed by the others choice are the same.
3701 -- This variable is updated as a side effect of function Get_Value.
3703 Box_Node : Node_Id := Empty;
3704 Is_Box_Present : Boolean := False;
3705 Others_Box : Natural := 0;
3706 -- Ada 2005 (AI-287): Variables used in case of default initialization
3707 -- to provide a functionality similar to Others_Etype. Box_Present
3708 -- indicates that the component takes its default initialization;
3709 -- Others_Box counts the number of components of the current aggregate
3710 -- (which may be a sub-aggregate of a larger one) that are default-
3711 -- initialized. A value of One indicates that an others_box is present.
3712 -- Any larger value indicates that the others_box is not redundant.
3713 -- These variables, similar to Others_Etype, are also updated as a side
3714 -- effect of function Get_Value. Box_Node is used to place a warning on
3715 -- a redundant others_box.
3717 procedure Add_Association
3718 (Component : Entity_Id;
3720 Assoc_List : List_Id;
3721 Is_Box_Present : Boolean := False);
3722 -- Builds a new N_Component_Association node which associates Component
3723 -- to expression Expr and adds it to the association list being built,
3724 -- either New_Assoc_List, or the association being built for an inner
3727 procedure Add_Discriminant_Values
3728 (New_Aggr : Node_Id;
3729 Assoc_List : List_Id);
3730 -- The constraint to a component may be given by a discriminant of the
3731 -- enclosing type, in which case we have to retrieve its value, which is
3732 -- part of the enclosing aggregate. Assoc_List provides the discriminant
3733 -- associations of the current type or of some enclosing record.
3735 function Discriminant_Present (Input_Discr : Entity_Id) return Boolean;
3736 -- If aggregate N is a regular aggregate this routine will return True.
3737 -- Otherwise, if N is an extension aggregate, then Input_Discr denotes
3738 -- a discriminant whose value may already have been specified by N's
3739 -- ancestor part. This routine checks whether this is indeed the case
3740 -- and if so returns False, signaling that no value for Input_Discr
3741 -- should appear in N's aggregate part. Also, in this case, the routine
3742 -- appends to New_Assoc_List the discriminant value specified in the
3745 -- If the aggregate is in a context with expansion delayed, it will be
3746 -- reanalyzed. The inherited discriminant values must not be reinserted
3747 -- in the component list to prevent spurious errors, but they must be
3748 -- present on first analysis to build the proper subtype indications.
3749 -- The flag Inherited_Discriminant is used to prevent the re-insertion.
3751 function Find_Private_Ancestor (Typ : Entity_Id) return Entity_Id;
3752 -- AI05-0115: Find earlier ancestor in the derivation chain that is
3753 -- derived from private view Typ. Whether the aggregate is legal depends
3754 -- on the current visibility of the type as well as that of the parent
3758 (Compon : Entity_Id;
3760 Consider_Others_Choice : Boolean := False) return Node_Id;
3761 -- Given a record component stored in parameter Compon, this function
3762 -- returns its value as it appears in the list From, which is a list
3763 -- of N_Component_Association nodes.
3765 -- If no component association has a choice for the searched component,
3766 -- the value provided by the others choice is returned, if there is one,
3767 -- and Consider_Others_Choice is set to true. Otherwise Empty is
3768 -- returned. If there is more than one component association giving a
3769 -- value for the searched record component, an error message is emitted
3770 -- and the first found value is returned.
3772 -- If Consider_Others_Choice is set and the returned expression comes
3773 -- from the others choice, then Others_Etype is set as a side effect.
3774 -- An error message is emitted if the components taking their value from
3775 -- the others choice do not have same type.
3777 procedure Propagate_Discriminants
3779 Assoc_List : List_Id);
3780 -- Nested components may themselves be discriminated types constrained
3781 -- by outer discriminants, whose values must be captured before the
3782 -- aggregate is expanded into assignments.
3784 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Entity_Id);
3785 -- Analyzes and resolves expression Expr against the Etype of the
3786 -- Component. This routine also applies all appropriate checks to Expr.
3787 -- It finally saves a Expr in the newly created association list that
3788 -- will be attached to the final record aggregate. Note that if the
3789 -- Parent pointer of Expr is not set then Expr was produced with a
3790 -- New_Copy_Tree or some such.
3792 procedure Rewrite_Range (Root_Type : Entity_Id; Rge : Node_Id);
3793 -- Rewrite a range node Rge when its bounds refer to non-stored
3794 -- discriminants from Root_Type, to replace them with the stored
3795 -- discriminant values. This is required in GNATprove mode, and is
3796 -- adopted in all modes to avoid special-casing GNATprove mode.
3798 ---------------------
3799 -- Add_Association --
3800 ---------------------
3802 procedure Add_Association
3803 (Component : Entity_Id;
3805 Assoc_List : List_Id;
3806 Is_Box_Present : Boolean := False)
3808 Choice_List : constant List_Id := New_List;
3812 -- If this is a box association the expression is missing, so use the
3813 -- Sloc of the aggregate itself for the new association.
3815 pragma Assert (Present (Expr) xor Is_Box_Present);
3817 if Present (Expr) then
3823 Append_To (Choice_List, New_Occurrence_Of (Component, Loc));
3825 Append_To (Assoc_List,
3826 Make_Component_Association (Loc,
3827 Choices => Choice_List,
3829 Box_Present => Is_Box_Present));
3830 end Add_Association;
3832 -----------------------------
3833 -- Add_Discriminant_Values --
3834 -----------------------------
3836 procedure Add_Discriminant_Values
3837 (New_Aggr : Node_Id;
3838 Assoc_List : List_Id)
3842 Discr_Elmt : Elmt_Id;
3843 Discr_Val : Node_Id;
3847 Discr := First_Discriminant (Etype (New_Aggr));
3848 Discr_Elmt := First_Elmt (Discriminant_Constraint (Etype (New_Aggr)));
3849 while Present (Discr_Elmt) loop
3850 Discr_Val := Node (Discr_Elmt);
3852 -- If the constraint is given by a discriminant then it is a
3853 -- discriminant of an enclosing record, and its value has already
3854 -- been placed in the association list.
3856 if Is_Entity_Name (Discr_Val)
3857 and then Ekind (Entity (Discr_Val)) = E_Discriminant
3859 Val := Entity (Discr_Val);
3861 Assoc := First (Assoc_List);
3862 while Present (Assoc) loop
3863 if Present (Entity (First (Choices (Assoc))))
3864 and then Entity (First (Choices (Assoc))) = Val
3866 Discr_Val := Expression (Assoc);
3875 (Discr, New_Copy_Tree (Discr_Val),
3876 Component_Associations (New_Aggr));
3878 -- If the discriminant constraint is a current instance, mark the
3879 -- current aggregate so that the self-reference can be expanded
3880 -- later. The constraint may refer to the subtype of aggregate, so
3881 -- use base type for comparison.
3883 if Nkind (Discr_Val) = N_Attribute_Reference
3884 and then Is_Entity_Name (Prefix (Discr_Val))
3885 and then Is_Type (Entity (Prefix (Discr_Val)))
3886 and then Base_Type (Etype (N)) = Entity (Prefix (Discr_Val))
3888 Set_Has_Self_Reference (N);
3891 Next_Elmt (Discr_Elmt);
3892 Next_Discriminant (Discr);
3894 end Add_Discriminant_Values;
3896 --------------------------
3897 -- Discriminant_Present --
3898 --------------------------
3900 function Discriminant_Present (Input_Discr : Entity_Id) return Boolean is
3901 Regular_Aggr : constant Boolean := Nkind (N) /= N_Extension_Aggregate;
3903 Ancestor_Is_Subtyp : Boolean;
3908 Ancestor_Typ : Entity_Id;
3909 Comp_Assoc : Node_Id;
3911 Discr_Expr : Node_Id;
3912 Discr_Val : Elmt_Id := No_Elmt;
3913 Orig_Discr : Entity_Id;
3916 if Regular_Aggr then
3920 -- Check whether inherited discriminant values have already been
3921 -- inserted in the aggregate. This will be the case if we are
3922 -- re-analyzing an aggregate whose expansion was delayed.
3924 if Present (Component_Associations (N)) then
3925 Comp_Assoc := First (Component_Associations (N));
3926 while Present (Comp_Assoc) loop
3927 if Inherited_Discriminant (Comp_Assoc) then
3935 Ancestor := Ancestor_Part (N);
3936 Ancestor_Typ := Etype (Ancestor);
3937 Loc := Sloc (Ancestor);
3939 -- For a private type with unknown discriminants, use the underlying
3940 -- record view if it is available.
3942 if Has_Unknown_Discriminants (Ancestor_Typ)
3943 and then Present (Full_View (Ancestor_Typ))
3944 and then Present (Underlying_Record_View (Full_View (Ancestor_Typ)))
3946 Ancestor_Typ := Underlying_Record_View (Full_View (Ancestor_Typ));
3949 Ancestor_Is_Subtyp :=
3950 Is_Entity_Name (Ancestor) and then Is_Type (Entity (Ancestor));
3952 -- If the ancestor part has no discriminants clearly N's aggregate
3953 -- part must provide a value for Discr.
3955 if not Has_Discriminants (Ancestor_Typ) then
3958 -- If the ancestor part is an unconstrained subtype mark then the
3959 -- Discr must be present in N's aggregate part.
3961 elsif Ancestor_Is_Subtyp
3962 and then not Is_Constrained (Entity (Ancestor))
3967 -- Now look to see if Discr was specified in the ancestor part
3969 if Ancestor_Is_Subtyp then
3971 First_Elmt (Discriminant_Constraint (Entity (Ancestor)));
3974 Orig_Discr := Original_Record_Component (Input_Discr);
3976 Discr := First_Discriminant (Ancestor_Typ);
3977 while Present (Discr) loop
3979 -- If Ancestor has already specified Disc value then insert its
3980 -- value in the final aggregate.
3982 if Original_Record_Component (Discr) = Orig_Discr then
3983 if Ancestor_Is_Subtyp then
3984 Discr_Expr := New_Copy_Tree (Node (Discr_Val));
3987 Make_Selected_Component (Loc,
3988 Prefix => Duplicate_Subexpr (Ancestor),
3989 Selector_Name => New_Occurrence_Of (Input_Discr, Loc));
3992 Resolve_Aggr_Expr (Discr_Expr, Input_Discr);
3993 Set_Inherited_Discriminant (Last (New_Assoc_List));
3997 Next_Discriminant (Discr);
3999 if Ancestor_Is_Subtyp then
4000 Next_Elmt (Discr_Val);
4005 end Discriminant_Present;
4007 ---------------------------
4008 -- Find_Private_Ancestor --
4009 ---------------------------
4011 function Find_Private_Ancestor (Typ : Entity_Id) return Entity_Id is
4017 if Has_Private_Ancestor (Par)
4018 and then not Has_Private_Ancestor (Etype (Base_Type (Par)))
4022 elsif not Is_Derived_Type (Par) then
4026 Par := Etype (Base_Type (Par));
4029 end Find_Private_Ancestor;
4036 (Compon : Entity_Id;
4038 Consider_Others_Choice : Boolean := False) return Node_Id
4040 Typ : constant Entity_Id := Etype (Compon);
4042 Expr : Node_Id := Empty;
4043 Selector_Name : Node_Id;
4046 Is_Box_Present := False;
4052 Assoc := First (From);
4053 while Present (Assoc) loop
4054 Selector_Name := First (Choices (Assoc));
4055 while Present (Selector_Name) loop
4056 if Nkind (Selector_Name) = N_Others_Choice then
4057 if Consider_Others_Choice and then No (Expr) then
4059 -- We need to duplicate the expression for each
4060 -- successive component covered by the others choice.
4061 -- This is redundant if the others_choice covers only
4062 -- one component (small optimization possible???), but
4063 -- indispensable otherwise, because each one must be
4064 -- expanded individually to preserve side effects.
4066 -- Ada 2005 (AI-287): In case of default initialization
4067 -- of components, we duplicate the corresponding default
4068 -- expression (from the record type declaration). The
4069 -- copy must carry the sloc of the association (not the
4070 -- original expression) to prevent spurious elaboration
4071 -- checks when the default includes function calls.
4073 if Box_Present (Assoc) then
4074 Others_Box := Others_Box + 1;
4075 Is_Box_Present := True;
4077 if Expander_Active then
4079 New_Copy_Tree_And_Copy_Dimensions
4080 (Expression (Parent (Compon)),
4081 New_Sloc => Sloc (Assoc));
4083 return Expression (Parent (Compon));
4087 if Present (Others_Etype)
4088 and then Base_Type (Others_Etype) /= Base_Type (Typ)
4090 -- If the components are of an anonymous access
4091 -- type they are distinct, but this is legal in
4092 -- Ada 2012 as long as designated types match.
4094 if (Ekind (Typ) = E_Anonymous_Access_Type
4095 or else Ekind (Typ) =
4096 E_Anonymous_Access_Subprogram_Type)
4097 and then Designated_Type (Typ) =
4098 Designated_Type (Others_Etype)
4103 ("components in OTHERS choice must have same "
4104 & "type", Selector_Name);
4108 Others_Etype := Typ;
4110 -- Copy the expression so that it is resolved
4111 -- independently for each component, This is needed
4112 -- for accessibility checks on components of anonymous
4113 -- access types, even in compile_only mode.
4115 if not Inside_A_Generic then
4117 New_Copy_Tree_And_Copy_Dimensions
4118 (Expression (Assoc));
4120 return Expression (Assoc);
4125 elsif Chars (Compon) = Chars (Selector_Name) then
4128 -- Ada 2005 (AI-231)
4130 if Ada_Version >= Ada_2005
4131 and then Known_Null (Expression (Assoc))
4133 Check_Can_Never_Be_Null (Compon, Expression (Assoc));
4136 -- We need to duplicate the expression when several
4137 -- components are grouped together with a "|" choice.
4138 -- For instance "filed1 | filed2 => Expr"
4140 -- Ada 2005 (AI-287)
4142 if Box_Present (Assoc) then
4143 Is_Box_Present := True;
4145 -- Duplicate the default expression of the component
4146 -- from the record type declaration, so a new copy
4147 -- can be attached to the association.
4149 -- Note that we always copy the default expression,
4150 -- even when the association has a single choice, in
4151 -- order to create a proper association for the
4152 -- expanded aggregate.
4154 -- Component may have no default, in which case the
4155 -- expression is empty and the component is default-
4156 -- initialized, but an association for the component
4157 -- exists, and it is not covered by an others clause.
4159 -- Scalar and private types have no initialization
4160 -- procedure, so they remain uninitialized. If the
4161 -- target of the aggregate is a constant this
4162 -- deserves a warning.
4164 if No (Expression (Parent (Compon)))
4165 and then not Has_Non_Null_Base_Init_Proc (Typ)
4166 and then not Has_Aspect (Typ, Aspect_Default_Value)
4167 and then not Is_Concurrent_Type (Typ)
4168 and then Nkind (Parent (N)) = N_Object_Declaration
4169 and then Constant_Present (Parent (N))
4171 Error_Msg_Node_2 := Typ;
4173 ("component&? of type& is uninitialized",
4174 Assoc, Selector_Name);
4176 -- An additional reminder if the component type
4177 -- is a generic formal.
4179 if Is_Generic_Type (Base_Type (Typ)) then
4181 ("\instance should provide actual type with "
4182 & "initialization for&", Assoc, Typ);
4187 New_Copy_Tree_And_Copy_Dimensions
4188 (Expression (Parent (Compon)));
4191 if Present (Next (Selector_Name)) then
4192 Expr := New_Copy_Tree_And_Copy_Dimensions
4193 (Expression (Assoc));
4195 Expr := Expression (Assoc);
4199 Generate_Reference (Compon, Selector_Name, 'm');
4203 ("more than one value supplied for &",
4204 Selector_Name, Compon);
4209 Next (Selector_Name);
4218 -----------------------------
4219 -- Propagate_Discriminants --
4220 -----------------------------
4222 procedure Propagate_Discriminants
4224 Assoc_List : List_Id)
4226 Loc : constant Source_Ptr := Sloc (N);
4228 procedure Process_Component (Comp : Entity_Id);
4229 -- Add one component with a box association to the inner aggregate,
4230 -- and recurse if component is itself composite.
4232 -----------------------
4233 -- Process_Component --
4234 -----------------------
4236 procedure Process_Component (Comp : Entity_Id) is
4237 T : constant Entity_Id := Etype (Comp);
4241 if Is_Record_Type (T) and then Has_Discriminants (T) then
4242 New_Aggr := Make_Aggregate (Loc, No_List, New_List);
4243 Set_Etype (New_Aggr, T);
4246 (Comp, New_Aggr, Component_Associations (Aggr));
4248 -- Collect discriminant values and recurse
4250 Add_Discriminant_Values (New_Aggr, Assoc_List);
4251 Propagate_Discriminants (New_Aggr, Assoc_List);
4253 Build_Constrained_Itype
4254 (New_Aggr, T, Component_Associations (New_Aggr));
4257 (Comp, Empty, Component_Associations (Aggr),
4258 Is_Box_Present => True);
4260 end Process_Component;
4264 Aggr_Type : constant Entity_Id := Base_Type (Etype (Aggr));
4265 Components : constant Elist_Id := New_Elmt_List;
4266 Def_Node : constant Node_Id :=
4267 Type_Definition (Declaration_Node (Aggr_Type));
4270 Comp_Elmt : Elmt_Id;
4273 -- Start of processing for Propagate_Discriminants
4276 -- The component type may be a variant type. Collect the components
4277 -- that are ruled by the known values of the discriminants. Their
4278 -- values have already been inserted into the component list of the
4279 -- current aggregate.
4281 if Nkind (Def_Node) = N_Record_Definition
4282 and then Present (Component_List (Def_Node))
4283 and then Present (Variant_Part (Component_List (Def_Node)))
4285 Gather_Components (Aggr_Type,
4286 Component_List (Def_Node),
4287 Governed_By => Component_Associations (Aggr),
4289 Report_Errors => Errors);
4291 Comp_Elmt := First_Elmt (Components);
4292 while Present (Comp_Elmt) loop
4293 if Ekind (Node (Comp_Elmt)) /= E_Discriminant then
4294 Process_Component (Node (Comp_Elmt));
4297 Next_Elmt (Comp_Elmt);
4300 -- No variant part, iterate over all components
4303 Comp := First_Component (Etype (Aggr));
4304 while Present (Comp) loop
4305 Process_Component (Comp);
4306 Next_Component (Comp);
4309 end Propagate_Discriminants;
4311 -----------------------
4312 -- Resolve_Aggr_Expr --
4313 -----------------------
4315 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Entity_Id) is
4316 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean;
4317 -- If the expression is an aggregate (possibly qualified) then its
4318 -- expansion is delayed until the enclosing aggregate is expanded
4319 -- into assignments. In that case, do not generate checks on the
4320 -- expression, because they will be generated later, and will other-
4321 -- wise force a copy (to remove side effects) that would leave a
4322 -- dynamic-sized aggregate in the code, something that gigi cannot
4325 ---------------------------
4326 -- Has_Expansion_Delayed --
4327 ---------------------------
4329 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean is
4332 (Nkind (Expr) in N_Aggregate | N_Extension_Aggregate
4333 and then Present (Etype (Expr))
4334 and then Is_Record_Type (Etype (Expr))
4335 and then Expansion_Delayed (Expr))
4337 (Nkind (Expr) = N_Qualified_Expression
4338 and then Has_Expansion_Delayed (Expression (Expr)));
4339 end Has_Expansion_Delayed;
4343 Expr_Type : Entity_Id := Empty;
4344 New_C : Entity_Id := Component;
4348 -- Set to True if the resolved Expr node needs to be relocated when
4349 -- attached to the newly created association list. This node need not
4350 -- be relocated if its parent pointer is not set. In fact in this
4351 -- case Expr is the output of a New_Copy_Tree call. If Relocate is
4352 -- True then we have analyzed the expression node in the original
4353 -- aggregate and hence it needs to be relocated when moved over to
4354 -- the new association list.
4356 -- Start of processing for Resolve_Aggr_Expr
4359 -- If the type of the component is elementary or the type of the
4360 -- aggregate does not contain discriminants, use the type of the
4361 -- component to resolve Expr.
4363 if Is_Elementary_Type (Etype (Component))
4364 or else not Has_Discriminants (Etype (N))
4366 Expr_Type := Etype (Component);
4368 -- Otherwise we have to pick up the new type of the component from
4369 -- the new constrained subtype of the aggregate. In fact components
4370 -- which are of a composite type might be constrained by a
4371 -- discriminant, and we want to resolve Expr against the subtype were
4372 -- all discriminant occurrences are replaced with their actual value.
4375 New_C := First_Component (Etype (N));
4376 while Present (New_C) loop
4377 if Chars (New_C) = Chars (Component) then
4378 Expr_Type := Etype (New_C);
4382 Next_Component (New_C);
4385 pragma Assert (Present (Expr_Type));
4387 -- For each range in an array type where a discriminant has been
4388 -- replaced with the constraint, check that this range is within
4389 -- the range of the base type. This checks is done in the init
4390 -- proc for regular objects, but has to be done here for
4391 -- aggregates since no init proc is called for them.
4393 if Is_Array_Type (Expr_Type) then
4396 -- Range of the current constrained index in the array
4398 Orig_Index : Node_Id := First_Index (Etype (Component));
4399 -- Range corresponding to the range Index above in the
4400 -- original unconstrained record type. The bounds of this
4401 -- range may be governed by discriminants.
4403 Unconstr_Index : Node_Id := First_Index (Etype (Expr_Type));
4404 -- Range corresponding to the range Index above for the
4405 -- unconstrained array type. This range is needed to apply
4409 Index := First_Index (Expr_Type);
4410 while Present (Index) loop
4411 if Depends_On_Discriminant (Orig_Index) then
4412 Apply_Range_Check (Index, Etype (Unconstr_Index));
4416 Next_Index (Orig_Index);
4417 Next_Index (Unconstr_Index);
4423 -- If the Parent pointer of Expr is not set, Expr is an expression
4424 -- duplicated by New_Tree_Copy (this happens for record aggregates
4425 -- that look like (Field1 | Filed2 => Expr) or (others => Expr)).
4426 -- Such a duplicated expression must be attached to the tree
4427 -- before analysis and resolution to enforce the rule that a tree
4428 -- fragment should never be analyzed or resolved unless it is
4429 -- attached to the current compilation unit.
4431 if No (Parent (Expr)) then
4432 Set_Parent (Expr, N);
4438 Analyze_And_Resolve (Expr, Expr_Type);
4439 Check_Expr_OK_In_Limited_Aggregate (Expr);
4440 Check_Non_Static_Context (Expr);
4441 Check_Unset_Reference (Expr);
4443 -- Check wrong use of class-wide types
4445 if Is_Class_Wide_Type (Etype (Expr)) then
4446 Error_Msg_N ("dynamically tagged expression not allowed", Expr);
4449 if not Has_Expansion_Delayed (Expr) then
4450 Aggregate_Constraint_Checks (Expr, Expr_Type);
4453 -- If an aggregate component has a type with predicates, an explicit
4454 -- predicate check must be applied, as for an assignment statement,
4455 -- because the aggregate might not be expanded into individual
4456 -- component assignments.
4458 if Has_Predicates (Expr_Type)
4459 and then Analyzed (Expr)
4461 Apply_Predicate_Check (Expr, Expr_Type);
4464 if Raises_Constraint_Error (Expr) then
4465 Set_Raises_Constraint_Error (N);
4468 -- If the expression has been marked as requiring a range check, then
4469 -- generate it here. It's a bit odd to be generating such checks in
4470 -- the analyzer, but harmless since Generate_Range_Check does nothing
4471 -- (other than making sure Do_Range_Check is set) if the expander is
4474 if Do_Range_Check (Expr) then
4475 Generate_Range_Check (Expr, Expr_Type, CE_Range_Check_Failed);
4478 -- Add association Component => Expr if the caller requests it
4481 New_Expr := Relocate_Node (Expr);
4483 -- Since New_Expr is not gonna be analyzed later on, we need to
4484 -- propagate here the dimensions form Expr to New_Expr.
4486 Copy_Dimensions (Expr, New_Expr);
4492 Add_Association (New_C, New_Expr, New_Assoc_List);
4493 end Resolve_Aggr_Expr;
4499 procedure Rewrite_Range (Root_Type : Entity_Id; Rge : Node_Id) is
4500 procedure Rewrite_Bound
4503 Expr_Disc : Node_Id);
4504 -- Rewrite a bound of the range Bound, when it is equal to the
4505 -- non-stored discriminant Disc, into the stored discriminant
4512 procedure Rewrite_Bound
4515 Expr_Disc : Node_Id)
4518 if Nkind (Bound) /= N_Identifier then
4522 -- We expect either the discriminant or the discriminal
4524 if Entity (Bound) = Disc
4525 or else (Ekind (Entity (Bound)) = E_In_Parameter
4526 and then Discriminal_Link (Entity (Bound)) = Disc)
4528 Rewrite (Bound, New_Copy_Tree (Expr_Disc));
4534 Low, High : Node_Id;
4536 Expr_Disc : Elmt_Id;
4538 -- Start of processing for Rewrite_Range
4541 if Has_Discriminants (Root_Type) and then Nkind (Rge) = N_Range then
4542 Low := Low_Bound (Rge);
4543 High := High_Bound (Rge);
4545 Disc := First_Discriminant (Root_Type);
4546 Expr_Disc := First_Elmt (Stored_Constraint (Etype (N)));
4547 while Present (Disc) loop
4548 Rewrite_Bound (Low, Disc, Node (Expr_Disc));
4549 Rewrite_Bound (High, Disc, Node (Expr_Disc));
4550 Next_Discriminant (Disc);
4551 Next_Elmt (Expr_Disc);
4558 Components : constant Elist_Id := New_Elmt_List;
4559 -- Components is the list of the record components whose value must be
4560 -- provided in the aggregate. This list does include discriminants.
4562 Component : Entity_Id;
4563 Component_Elmt : Elmt_Id;
4565 Positional_Expr : Node_Id;
4567 -- Start of processing for Resolve_Record_Aggregate
4570 -- A record aggregate is restricted in SPARK:
4572 -- Each named association can have only a single choice.
4573 -- OTHERS cannot be used.
4574 -- Positional and named associations cannot be mixed.
4576 if Present (Component_Associations (N))
4577 and then Present (First (Component_Associations (N)))
4583 Assoc := First (Component_Associations (N));
4584 while Present (Assoc) loop
4585 if Nkind (Assoc) = N_Iterated_Component_Association then
4587 ("iterated component association can only appear in an "
4588 & "array aggregate", N);
4589 raise Unrecoverable_Error;
4597 -- We may end up calling Duplicate_Subexpr on expressions that are
4598 -- attached to New_Assoc_List. For this reason we need to attach it
4599 -- to the tree by setting its parent pointer to N. This parent point
4600 -- will change in STEP 8 below.
4602 Set_Parent (New_Assoc_List, N);
4604 -- STEP 1: abstract type and null record verification
4606 if Is_Abstract_Type (Typ) then
4607 Error_Msg_N ("type of aggregate cannot be abstract", N);
4610 if No (First_Entity (Typ)) and then Null_Record_Present (N) then
4614 elsif Present (First_Entity (Typ))
4615 and then Null_Record_Present (N)
4616 and then not Is_Tagged_Type (Typ)
4618 Error_Msg_N ("record aggregate cannot be null", N);
4621 -- If the type has no components, then the aggregate should either
4622 -- have "null record", or in Ada 2005 it could instead have a single
4623 -- component association given by "others => <>". For Ada 95 we flag an
4624 -- error at this point, but for Ada 2005 we proceed with checking the
4625 -- associations below, which will catch the case where it's not an
4626 -- aggregate with "others => <>". Note that the legality of a <>
4627 -- aggregate for a null record type was established by AI05-016.
4629 elsif No (First_Entity (Typ))
4630 and then Ada_Version < Ada_2005
4632 Error_Msg_N ("record aggregate must be null", N);
4636 -- STEP 2: Verify aggregate structure
4640 Bad_Aggregate : Boolean := False;
4641 Selector_Name : Node_Id;
4644 if Present (Component_Associations (N)) then
4645 Assoc := First (Component_Associations (N));
4650 while Present (Assoc) loop
4651 Selector_Name := First (Choices (Assoc));
4652 while Present (Selector_Name) loop
4653 if Nkind (Selector_Name) = N_Identifier then
4656 elsif Nkind (Selector_Name) = N_Others_Choice then
4657 if Selector_Name /= First (Choices (Assoc))
4658 or else Present (Next (Selector_Name))
4661 ("OTHERS must appear alone in a choice list",
4665 elsif Present (Next (Assoc)) then
4667 ("OTHERS must appear last in an aggregate",
4671 -- (Ada 2005): If this is an association with a box,
4672 -- indicate that the association need not represent
4675 elsif Box_Present (Assoc) then
4682 ("selector name should be identifier or OTHERS",
4684 Bad_Aggregate := True;
4687 Next (Selector_Name);
4693 if Bad_Aggregate then
4698 -- STEP 3: Find discriminant Values
4701 Discrim : Entity_Id;
4702 Missing_Discriminants : Boolean := False;
4705 if Present (Expressions (N)) then
4706 Positional_Expr := First (Expressions (N));
4708 Positional_Expr := Empty;
4711 -- AI05-0115: if the ancestor part is a subtype mark, the ancestor
4712 -- must not have unknown discriminants.
4713 -- ??? We are not checking any subtype mark here and this code is not
4714 -- exercised by any test, so it's likely wrong (in particular
4715 -- we should not use Root_Type here but the subtype mark, if any),
4716 -- and possibly not needed.
4718 if Is_Derived_Type (Typ)
4719 and then Has_Unknown_Discriminants (Root_Type (Typ))
4720 and then Nkind (N) /= N_Extension_Aggregate
4723 ("aggregate not available for type& whose ancestor "
4724 & "has unknown discriminants ", N, Typ);
4727 if Has_Unknown_Discriminants (Typ)
4728 and then Present (Underlying_Record_View (Typ))
4730 Discrim := First_Discriminant (Underlying_Record_View (Typ));
4731 elsif Has_Discriminants (Typ) then
4732 Discrim := First_Discriminant (Typ);
4737 -- First find the discriminant values in the positional components
4739 while Present (Discrim) and then Present (Positional_Expr) loop
4740 if Discriminant_Present (Discrim) then
4741 Resolve_Aggr_Expr (Positional_Expr, Discrim);
4743 -- Ada 2005 (AI-231)
4745 if Ada_Version >= Ada_2005
4746 and then Known_Null (Positional_Expr)
4748 Check_Can_Never_Be_Null (Discrim, Positional_Expr);
4751 Next (Positional_Expr);
4754 if Present (Get_Value (Discrim, Component_Associations (N))) then
4756 ("more than one value supplied for discriminant&",
4760 Next_Discriminant (Discrim);
4763 -- Find remaining discriminant values if any among named components
4765 while Present (Discrim) loop
4766 Expr := Get_Value (Discrim, Component_Associations (N), True);
4768 if not Discriminant_Present (Discrim) then
4769 if Present (Expr) then
4771 ("more than one value supplied for discriminant &",
4775 elsif No (Expr) then
4777 ("no value supplied for discriminant &", N, Discrim);
4778 Missing_Discriminants := True;
4781 Resolve_Aggr_Expr (Expr, Discrim);
4784 Next_Discriminant (Discrim);
4787 if Missing_Discriminants then
4791 -- At this point and until the beginning of STEP 6, New_Assoc_List
4792 -- contains only the discriminants and their values.
4796 -- STEP 4: Set the Etype of the record aggregate
4798 if Has_Discriminants (Typ)
4799 or else (Has_Unknown_Discriminants (Typ)
4800 and then Present (Underlying_Record_View (Typ)))
4802 Build_Constrained_Itype (N, Typ, New_Assoc_List);
4807 -- STEP 5: Get remaining components according to discriminant values
4811 Errors_Found : Boolean := False;
4812 Record_Def : Node_Id;
4813 Parent_Typ : Entity_Id;
4814 Parent_Typ_List : Elist_Id;
4815 Parent_Elmt : Elmt_Id;
4816 Root_Typ : Entity_Id;
4819 if Is_Derived_Type (Typ) and then Is_Tagged_Type (Typ) then
4820 Parent_Typ_List := New_Elmt_List;
4822 -- If this is an extension aggregate, the component list must
4823 -- include all components that are not in the given ancestor type.
4824 -- Otherwise, the component list must include components of all
4825 -- ancestors, starting with the root.
4827 if Nkind (N) = N_Extension_Aggregate then
4828 Root_Typ := Base_Type (Etype (Ancestor_Part (N)));
4831 -- AI05-0115: check legality of aggregate for type with a
4832 -- private ancestor.
4834 Root_Typ := Root_Type (Typ);
4835 if Has_Private_Ancestor (Typ) then
4837 Ancestor : constant Entity_Id :=
4838 Find_Private_Ancestor (Typ);
4839 Ancestor_Unit : constant Entity_Id :=
4841 (Get_Source_Unit (Ancestor));
4842 Parent_Unit : constant Entity_Id :=
4843 Cunit_Entity (Get_Source_Unit
4844 (Base_Type (Etype (Ancestor))));
4846 -- Check whether we are in a scope that has full view
4847 -- over the private ancestor and its parent. This can
4848 -- only happen if the derivation takes place in a child
4849 -- unit of the unit that declares the parent, and we are
4850 -- in the private part or body of that child unit, else
4851 -- the aggregate is illegal.
4853 if Is_Child_Unit (Ancestor_Unit)
4854 and then Scope (Ancestor_Unit) = Parent_Unit
4855 and then In_Open_Scopes (Scope (Ancestor))
4857 (In_Private_Part (Scope (Ancestor))
4858 or else In_Package_Body (Scope (Ancestor)))
4864 ("type of aggregate has private ancestor&!",
4866 Error_Msg_N ("must use extension aggregate!", N);
4872 Dnode := Declaration_Node (Base_Type (Root_Typ));
4874 -- If we don't get a full declaration, then we have some error
4875 -- which will get signalled later so skip this part. Otherwise
4876 -- gather components of root that apply to the aggregate type.
4877 -- We use the base type in case there is an applicable stored
4878 -- constraint that renames the discriminants of the root.
4880 if Nkind (Dnode) = N_Full_Type_Declaration then
4881 Record_Def := Type_Definition (Dnode);
4884 Component_List (Record_Def),
4885 Governed_By => New_Assoc_List,
4887 Report_Errors => Errors_Found);
4889 if Errors_Found then
4891 ("discriminant controlling variant part is not static",
4898 Parent_Typ := Base_Type (Typ);
4899 while Parent_Typ /= Root_Typ loop
4900 Prepend_Elmt (Parent_Typ, To => Parent_Typ_List);
4901 Parent_Typ := Etype (Parent_Typ);
4903 if Nkind (Parent (Base_Type (Parent_Typ))) =
4904 N_Private_Type_Declaration
4905 or else Nkind (Parent (Base_Type (Parent_Typ))) =
4906 N_Private_Extension_Declaration
4908 if Nkind (N) /= N_Extension_Aggregate then
4910 ("type of aggregate has private ancestor&!",
4912 Error_Msg_N ("must use extension aggregate!", N);
4915 elsif Parent_Typ /= Root_Typ then
4917 ("ancestor part of aggregate must be private type&",
4918 Ancestor_Part (N), Parent_Typ);
4922 -- The current view of ancestor part may be a private type,
4923 -- while the context type is always non-private.
4925 elsif Is_Private_Type (Root_Typ)
4926 and then Present (Full_View (Root_Typ))
4927 and then Nkind (N) = N_Extension_Aggregate
4929 exit when Base_Type (Full_View (Root_Typ)) = Parent_Typ;
4933 -- Now collect components from all other ancestors, beginning
4934 -- with the current type. If the type has unknown discriminants
4935 -- use the component list of the Underlying_Record_View, which
4936 -- needs to be used for the subsequent expansion of the aggregate
4937 -- into assignments.
4939 Parent_Elmt := First_Elmt (Parent_Typ_List);
4940 while Present (Parent_Elmt) loop
4941 Parent_Typ := Node (Parent_Elmt);
4943 if Has_Unknown_Discriminants (Parent_Typ)
4944 and then Present (Underlying_Record_View (Typ))
4946 Parent_Typ := Underlying_Record_View (Parent_Typ);
4949 Record_Def := Type_Definition (Parent (Base_Type (Parent_Typ)));
4950 Gather_Components (Empty,
4951 Component_List (Record_Extension_Part (Record_Def)),
4952 Governed_By => New_Assoc_List,
4954 Report_Errors => Errors_Found);
4956 Next_Elmt (Parent_Elmt);
4959 -- Typ is not a derived tagged type
4962 Record_Def := Type_Definition (Parent (Base_Type (Typ)));
4964 if Null_Present (Record_Def) then
4967 elsif not Has_Unknown_Discriminants (Typ) then
4970 Component_List (Record_Def),
4971 Governed_By => New_Assoc_List,
4973 Report_Errors => Errors_Found);
4977 (Base_Type (Underlying_Record_View (Typ)),
4978 Component_List (Record_Def),
4979 Governed_By => New_Assoc_List,
4981 Report_Errors => Errors_Found);
4985 if Errors_Found then
4990 -- STEP 6: Find component Values
4993 Component_Elmt := First_Elmt (Components);
4995 -- First scan the remaining positional associations in the aggregate.
4996 -- Remember that at this point Positional_Expr contains the current
4997 -- positional association if any is left after looking for discriminant
4998 -- values in step 3.
5000 while Present (Positional_Expr) and then Present (Component_Elmt) loop
5001 Component := Node (Component_Elmt);
5002 Resolve_Aggr_Expr (Positional_Expr, Component);
5004 -- Ada 2005 (AI-231)
5006 if Ada_Version >= Ada_2005 and then Known_Null (Positional_Expr) then
5007 Check_Can_Never_Be_Null (Component, Positional_Expr);
5010 if Present (Get_Value (Component, Component_Associations (N))) then
5012 ("more than one value supplied for Component &", N, Component);
5015 Next (Positional_Expr);
5016 Next_Elmt (Component_Elmt);
5019 if Present (Positional_Expr) then
5021 ("too many components for record aggregate", Positional_Expr);
5024 -- Now scan for the named arguments of the aggregate
5026 while Present (Component_Elmt) loop
5027 Component := Node (Component_Elmt);
5028 Expr := Get_Value (Component, Component_Associations (N), True);
5030 -- Note: The previous call to Get_Value sets the value of the
5031 -- variable Is_Box_Present.
5033 -- Ada 2005 (AI-287): Handle components with default initialization.
5034 -- Note: This feature was originally added to Ada 2005 for limited
5035 -- but it was finally allowed with any type.
5037 if Is_Box_Present then
5038 Check_Box_Component : declare
5039 Ctyp : constant Entity_Id := Etype (Component);
5042 -- If there is a default expression for the aggregate, copy
5043 -- it into a new association. This copy must modify the scopes
5044 -- of internal types that may be attached to the expression
5045 -- (e.g. index subtypes of arrays) because in general the type
5046 -- declaration and the aggregate appear in different scopes,
5047 -- and the backend requires the scope of the type to match the
5048 -- point at which it is elaborated.
5050 -- If the component has an initialization procedure (IP) we
5051 -- pass the component to the expander, which will generate
5052 -- the call to such IP.
5054 -- If the component has discriminants, their values must
5055 -- be taken from their subtype. This is indispensable for
5056 -- constraints that are given by the current instance of an
5057 -- enclosing type, to allow the expansion of the aggregate to
5058 -- replace the reference to the current instance by the target
5059 -- object of the aggregate.
5061 if Present (Parent (Component))
5062 and then Nkind (Parent (Component)) = N_Component_Declaration
5063 and then Present (Expression (Parent (Component)))
5066 New_Copy_Tree_And_Copy_Dimensions
5067 (Expression (Parent (Component)),
5068 New_Scope => Current_Scope,
5069 New_Sloc => Sloc (N));
5071 -- As the type of the copied default expression may refer
5072 -- to discriminants of the record type declaration, these
5073 -- non-stored discriminants need to be rewritten into stored
5074 -- discriminant values for the aggregate. This is required
5075 -- in GNATprove mode, and is adopted in all modes to avoid
5076 -- special-casing GNATprove mode.
5078 if Is_Array_Type (Etype (Expr)) then
5080 Rec_Typ : constant Entity_Id := Scope (Component);
5081 -- Root record type whose discriminants may be used as
5082 -- bounds in range nodes.
5089 -- Rewrite the range nodes occurring in the indexes
5092 Index := First_Index (Etype (Expr));
5093 while Present (Index) loop
5094 Rewrite_Range (Rec_Typ, Index);
5096 (Rec_Typ, Scalar_Range (Etype (Index)));
5101 -- Rewrite the range nodes occurring as aggregate
5102 -- bounds and component associations.
5104 if Nkind (Expr) = N_Aggregate then
5105 if Present (Aggregate_Bounds (Expr)) then
5106 Rewrite_Range (Rec_Typ, Aggregate_Bounds (Expr));
5109 if Present (Component_Associations (Expr)) then
5110 Assoc := First (Component_Associations (Expr));
5111 while Present (Assoc) loop
5112 Choice := First (Choices (Assoc));
5113 while Present (Choice) loop
5114 Rewrite_Range (Rec_Typ, Choice);
5127 (Component => Component,
5129 Assoc_List => New_Assoc_List);
5130 Set_Has_Self_Reference (N);
5132 -- A box-defaulted access component gets the value null. Also
5133 -- included are components of private types whose underlying
5134 -- type is an access type. In either case set the type of the
5135 -- literal, for subsequent use in semantic checks.
5137 elsif Present (Underlying_Type (Ctyp))
5138 and then Is_Access_Type (Underlying_Type (Ctyp))
5140 -- If the component's type is private with an access type as
5141 -- its underlying type then we have to create an unchecked
5142 -- conversion to satisfy type checking.
5144 if Is_Private_Type (Ctyp) then
5146 Qual_Null : constant Node_Id :=
5147 Make_Qualified_Expression (Sloc (N),
5150 (Underlying_Type (Ctyp), Sloc (N)),
5151 Expression => Make_Null (Sloc (N)));
5153 Convert_Null : constant Node_Id :=
5154 Unchecked_Convert_To
5158 Analyze_And_Resolve (Convert_Null, Ctyp);
5160 (Component => Component,
5161 Expr => Convert_Null,
5162 Assoc_List => New_Assoc_List);
5165 -- Otherwise the component type is non-private
5168 Expr := Make_Null (Sloc (N));
5169 Set_Etype (Expr, Ctyp);
5172 (Component => Component,
5174 Assoc_List => New_Assoc_List);
5177 -- Ada 2012: If component is scalar with default value, use it
5178 -- by converting it to Ctyp, so that subtype constraints are
5181 elsif Is_Scalar_Type (Ctyp)
5182 and then Has_Default_Aspect (Ctyp)
5185 Conv : constant Node_Id :=
5190 (Default_Aspect_Value
5191 (First_Subtype (Underlying_Type (Ctyp)))));
5194 Analyze_And_Resolve (Conv, Ctyp);
5196 (Component => Component,
5198 Assoc_List => New_Assoc_List);
5201 elsif Has_Non_Null_Base_Init_Proc (Ctyp)
5202 or else not Expander_Active
5204 if Is_Record_Type (Ctyp)
5205 and then Has_Discriminants (Ctyp)
5206 and then not Is_Private_Type (Ctyp)
5208 -- We build a partially initialized aggregate with the
5209 -- values of the discriminants and box initialization
5210 -- for the rest, if other components are present.
5212 -- The type of the aggregate is the known subtype of
5213 -- the component. The capture of discriminants must be
5214 -- recursive because subcomponents may be constrained
5215 -- (transitively) by discriminants of enclosing types.
5216 -- For a private type with discriminants, a call to the
5217 -- initialization procedure will be generated, and no
5218 -- subaggregate is needed.
5220 Capture_Discriminants : declare
5221 Loc : constant Source_Ptr := Sloc (N);
5225 Expr := Make_Aggregate (Loc, No_List, New_List);
5226 Set_Etype (Expr, Ctyp);
5228 -- If the enclosing type has discriminants, they have
5229 -- been collected in the aggregate earlier, and they
5230 -- may appear as constraints of subcomponents.
5232 -- Similarly if this component has discriminants, they
5233 -- might in turn be propagated to their components.
5235 if Has_Discriminants (Typ) then
5236 Add_Discriminant_Values (Expr, New_Assoc_List);
5237 Propagate_Discriminants (Expr, New_Assoc_List);
5239 elsif Has_Discriminants (Ctyp) then
5240 Add_Discriminant_Values
5241 (Expr, Component_Associations (Expr));
5242 Propagate_Discriminants
5243 (Expr, Component_Associations (Expr));
5245 Build_Constrained_Itype
5246 (Expr, Ctyp, Component_Associations (Expr));
5253 -- If the type has additional components, create
5254 -- an OTHERS box association for them.
5256 Comp := First_Component (Ctyp);
5257 while Present (Comp) loop
5258 if Ekind (Comp) = E_Component then
5259 if not Is_Record_Type (Etype (Comp)) then
5261 (Component_Associations (Expr),
5262 Make_Component_Association (Loc,
5265 Make_Others_Choice (Loc)),
5266 Expression => Empty,
5267 Box_Present => True));
5273 Next_Component (Comp);
5279 (Component => Component,
5281 Assoc_List => New_Assoc_List);
5282 end Capture_Discriminants;
5284 -- Otherwise the component type is not a record, or it has
5285 -- not discriminants, or it is private.
5289 (Component => Component,
5291 Assoc_List => New_Assoc_List,
5292 Is_Box_Present => True);
5295 -- Otherwise we only need to resolve the expression if the
5296 -- component has partially initialized values (required to
5297 -- expand the corresponding assignments and run-time checks).
5299 elsif Present (Expr)
5300 and then Is_Partially_Initialized_Type (Ctyp)
5302 Resolve_Aggr_Expr (Expr, Component);
5304 end Check_Box_Component;
5306 elsif No (Expr) then
5308 -- Ignore hidden components associated with the position of the
5309 -- interface tags: these are initialized dynamically.
5311 if not Present (Related_Type (Component)) then
5313 ("no value supplied for component &!", N, Component);
5317 Resolve_Aggr_Expr (Expr, Component);
5320 Next_Elmt (Component_Elmt);
5323 -- STEP 7: check for invalid components + check type in choice list
5327 New_Assoc : Node_Id;
5333 -- Type of first component in choice list
5336 if Present (Component_Associations (N)) then
5337 Assoc := First (Component_Associations (N));
5342 Verification : while Present (Assoc) loop
5343 Selectr := First (Choices (Assoc));
5346 if Nkind (Selectr) = N_Others_Choice then
5348 -- Ada 2005 (AI-287): others choice may have expression or box
5350 if No (Others_Etype) and then Others_Box = 0 then
5352 ("OTHERS must represent at least one component", Selectr);
5354 elsif Others_Box = 1 and then Warn_On_Redundant_Constructs then
5355 Error_Msg_N ("others choice is redundant?", Box_Node);
5357 ("\previous choices cover all components?", Box_Node);
5363 while Present (Selectr) loop
5364 New_Assoc := First (New_Assoc_List);
5365 while Present (New_Assoc) loop
5366 Component := First (Choices (New_Assoc));
5368 if Chars (Selectr) = Chars (Component) then
5370 Check_Identifier (Selectr, Entity (Component));
5379 -- If no association, this is not a legal component of the type
5380 -- in question, unless its association is provided with a box.
5382 if No (New_Assoc) then
5383 if Box_Present (Parent (Selectr)) then
5385 -- This may still be a bogus component with a box. Scan
5386 -- list of components to verify that a component with
5387 -- that name exists.
5393 C := First_Component (Typ);
5394 while Present (C) loop
5395 if Chars (C) = Chars (Selectr) then
5397 -- If the context is an extension aggregate,
5398 -- the component must not be inherited from
5399 -- the ancestor part of the aggregate.
5401 if Nkind (N) /= N_Extension_Aggregate
5403 Scope (Original_Record_Component (C)) /=
5404 Etype (Ancestor_Part (N))
5414 Error_Msg_Node_2 := Typ;
5415 Error_Msg_N ("& is not a component of}", Selectr);
5419 elsif Chars (Selectr) /= Name_uTag
5420 and then Chars (Selectr) /= Name_uParent
5422 if not Has_Discriminants (Typ) then
5423 Error_Msg_Node_2 := Typ;
5424 Error_Msg_N ("& is not a component of}", Selectr);
5427 ("& is not a component of the aggregate subtype",
5431 Check_Misspelled_Component (Components, Selectr);
5434 elsif No (Typech) then
5435 Typech := Base_Type (Etype (Component));
5437 -- AI05-0199: In Ada 2012, several components of anonymous
5438 -- access types can appear in a choice list, as long as the
5439 -- designated types match.
5441 elsif Typech /= Base_Type (Etype (Component)) then
5442 if Ada_Version >= Ada_2012
5443 and then Ekind (Typech) = E_Anonymous_Access_Type
5445 Ekind (Etype (Component)) = E_Anonymous_Access_Type
5446 and then Base_Type (Designated_Type (Typech)) =
5447 Base_Type (Designated_Type (Etype (Component)))
5449 Subtypes_Statically_Match (Typech, (Etype (Component)))
5453 elsif not Box_Present (Parent (Selectr)) then
5455 ("components in choice list must have same type",
5464 end loop Verification;
5467 -- STEP 8: replace the original aggregate
5470 New_Aggregate : constant Node_Id := New_Copy (N);
5473 Set_Expressions (New_Aggregate, No_List);
5474 Set_Etype (New_Aggregate, Etype (N));
5475 Set_Component_Associations (New_Aggregate, New_Assoc_List);
5476 Set_Check_Actuals (New_Aggregate, Check_Actuals (N));
5478 Rewrite (N, New_Aggregate);
5481 -- Check the dimensions of the components in the record aggregate
5483 Analyze_Dimension_Extension_Or_Record_Aggregate (N);
5484 end Resolve_Record_Aggregate;
5486 -----------------------------
5487 -- Check_Can_Never_Be_Null --
5488 -----------------------------
5490 procedure Check_Can_Never_Be_Null (Typ : Entity_Id; Expr : Node_Id) is
5491 Comp_Typ : Entity_Id;
5495 (Ada_Version >= Ada_2005
5496 and then Present (Expr)
5497 and then Known_Null (Expr));
5500 when E_Array_Type =>
5501 Comp_Typ := Component_Type (Typ);
5506 Comp_Typ := Etype (Typ);
5512 if Can_Never_Be_Null (Comp_Typ) then
5514 -- Here we know we have a constraint error. Note that we do not use
5515 -- Apply_Compile_Time_Constraint_Error here to the Expr, which might
5516 -- seem the more natural approach. That's because in some cases the
5517 -- components are rewritten, and the replacement would be missed.
5518 -- We do not mark the whole aggregate as raising a constraint error,
5519 -- because the association may be a null array range.
5522 ("(Ada 2005) null not allowed in null-excluding component??", Expr);
5524 ("\Constraint_Error will be raised at run time??", Expr);
5527 Make_Raise_Constraint_Error
5528 (Sloc (Expr), Reason => CE_Access_Check_Failed));
5529 Set_Etype (Expr, Comp_Typ);
5530 Set_Analyzed (Expr);
5532 end Check_Can_Never_Be_Null;
5534 ---------------------
5535 -- Sort_Case_Table --
5536 ---------------------
5538 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is
5539 U : constant Int := Case_Table'Last;
5547 T := Case_Table (K + 1);
5551 and then Expr_Value (Case_Table (J - 1).Lo) > Expr_Value (T.Lo)
5553 Case_Table (J) := Case_Table (J - 1);
5557 Case_Table (J) := T;
5560 end Sort_Case_Table;