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;
820 -- Ignore junk empty aggregate resulting from parser error
822 if No (Expressions (N))
823 and then No (Component_Associations (N))
824 and then not Null_Record_Present (N)
829 -- If the aggregate has box-initialized components, its type must be
830 -- frozen so that initialization procedures can properly be called
831 -- in the resolution that follows. The replacement of boxes with
832 -- initialization calls is properly an expansion activity but it must
833 -- be done during resolution.
836 and then Present (Component_Associations (N))
840 First_Comp : Boolean := True;
843 Comp := First (Component_Associations (N));
844 while Present (Comp) loop
845 if Box_Present (Comp) then
847 and then No (Expressions (N))
848 and then Nkind (First (Choices (Comp))) = N_Others_Choice
849 and then not Within_Aggregate (N)
854 Insert_Actions (N, Freeze_Entity (Typ, N));
864 -- Check for aggregates not allowed in configurable run-time mode.
865 -- We allow all cases of aggregates that do not come from source, since
866 -- these are all assumed to be small (e.g. bounds of a string literal).
867 -- We also allow aggregates of types we know to be small.
869 if not Support_Aggregates_On_Target
870 and then Comes_From_Source (N)
871 and then (not Known_Static_Esize (Typ)
872 or else Esize (Typ) > System_Max_Integer_Size)
874 Error_Msg_CRT ("aggregate", N);
877 -- Ada 2005 (AI-287): Limited aggregates allowed
879 -- In an instance, ignore aggregate subcomponents tnat may be limited,
880 -- because they originate in view conflicts. If the original aggregate
881 -- is legal and the actuals are legal, the aggregate itself is legal.
883 if Is_Limited_Type (Typ)
884 and then Ada_Version < Ada_2005
885 and then not In_Instance
887 Error_Msg_N ("aggregate type cannot be limited", N);
888 Explain_Limited_Type (Typ, N);
890 elsif Is_Class_Wide_Type (Typ) then
891 Error_Msg_N ("type of aggregate cannot be class-wide", N);
893 elsif Typ = Any_String
894 or else Typ = Any_Composite
896 Error_Msg_N ("no unique type for aggregate", N);
897 Set_Etype (N, Any_Composite);
899 elsif Is_Array_Type (Typ) and then Null_Record_Present (N) then
900 Error_Msg_N ("null record forbidden in array aggregate", N);
902 elsif Present (Find_Aspect (Typ, Aspect_Aggregate))
903 and then Ekind (Typ) /= E_Record_Type
905 Resolve_Container_Aggregate (N, Typ);
907 elsif Is_Record_Type (Typ) then
908 Resolve_Record_Aggregate (N, Typ);
910 elsif Is_Array_Type (Typ) then
912 -- First a special test, for the case of a positional aggregate of
913 -- characters which can be replaced by a string literal.
915 -- Do not perform this transformation if this was a string literal
916 -- to start with, whose components needed constraint checks, or if
917 -- the component type is non-static, because it will require those
918 -- checks and be transformed back into an aggregate. If the index
919 -- type is not Integer the aggregate may represent a user-defined
920 -- string type but the context might need the original type so we
921 -- do not perform the transformation at this point.
923 if Number_Dimensions (Typ) = 1
924 and then Is_Standard_Character_Type (Component_Type (Typ))
925 and then No (Component_Associations (N))
926 and then not Is_Limited_Composite (Typ)
927 and then not Is_Private_Composite (Typ)
928 and then not Is_Bit_Packed_Array (Typ)
929 and then Nkind (Original_Node (Parent (N))) /= N_String_Literal
930 and then Is_OK_Static_Subtype (Component_Type (Typ))
931 and then Base_Type (Etype (First_Index (Typ))) =
932 Base_Type (Standard_Integer)
938 Expr := First (Expressions (N));
939 while Present (Expr) loop
940 exit when Nkind (Expr) /= N_Character_Literal;
947 Expr := First (Expressions (N));
948 while Present (Expr) loop
949 Store_String_Char (UI_To_CC (Char_Literal_Value (Expr)));
953 Rewrite (N, Make_String_Literal (Loc, End_String));
955 Analyze_And_Resolve (N, Typ);
961 -- Here if we have a real aggregate to deal with
963 Array_Aggregate : declare
964 Aggr_Resolved : Boolean;
966 Aggr_Typ : constant Entity_Id := Etype (Typ);
967 -- This is the unconstrained array type, which is the type against
968 -- which the aggregate is to be resolved. Typ itself is the array
969 -- type of the context which may not be the same subtype as the
970 -- subtype for the final aggregate.
973 -- In the following we determine whether an OTHERS choice is
974 -- allowed inside the array aggregate. The test checks the context
975 -- in which the array aggregate occurs. If the context does not
976 -- permit it, or the aggregate type is unconstrained, an OTHERS
977 -- choice is not allowed (except that it is always allowed on the
978 -- right-hand side of an assignment statement; in this case the
979 -- constrainedness of the type doesn't matter, because an array
980 -- object is always constrained).
982 -- If expansion is disabled (generic context, or semantics-only
983 -- mode) actual subtypes cannot be constructed, and the type of an
984 -- object may be its unconstrained nominal type. However, if the
985 -- context is an assignment statement, OTHERS is allowed, because
986 -- the target of the assignment will have a constrained subtype
987 -- when fully compiled. Ditto if the context is an initialization
988 -- procedure where a component may have a predicate function that
989 -- carries the base type.
991 -- Note that there is no node for Explicit_Actual_Parameter.
992 -- To test for this context we therefore have to test for node
993 -- N_Parameter_Association which itself appears only if there is a
994 -- formal parameter. Consequently we also need to test for
995 -- N_Procedure_Call_Statement or N_Function_Call.
997 -- The context may be an N_Reference node, created by expansion.
998 -- Legality of the others clause was established in the source,
999 -- so the context is legal.
1001 Set_Etype (N, Aggr_Typ); -- May be overridden later on
1003 if Nkind (Parent (N)) = N_Assignment_Statement
1004 or else Inside_Init_Proc
1005 or else (Is_Constrained (Typ)
1006 and then Nkind (Parent (N)) in
1007 N_Parameter_Association
1009 | N_Procedure_Call_Statement
1010 | N_Generic_Association
1011 | N_Formal_Object_Declaration
1012 | N_Simple_Return_Statement
1013 | N_Object_Declaration
1014 | N_Component_Declaration
1015 | N_Parameter_Specification
1016 | N_Qualified_Expression
1019 | N_Extension_Aggregate
1020 | N_Component_Association
1021 | N_Case_Expression_Alternative
1023 | N_Expression_With_Actions)
1026 Resolve_Array_Aggregate
1028 Index => First_Index (Aggr_Typ),
1029 Index_Constr => First_Index (Typ),
1030 Component_Typ => Component_Type (Typ),
1031 Others_Allowed => True);
1034 Resolve_Array_Aggregate
1036 Index => First_Index (Aggr_Typ),
1037 Index_Constr => First_Index (Aggr_Typ),
1038 Component_Typ => Component_Type (Typ),
1039 Others_Allowed => False);
1042 if not Aggr_Resolved then
1044 -- A parenthesized expression may have been intended as an
1045 -- aggregate, leading to a type error when analyzing the
1046 -- component. This can also happen for a nested component
1047 -- (see Analyze_Aggr_Expr).
1049 if Paren_Count (N) > 0 then
1051 ("positional aggregate cannot have one component", N);
1054 Aggr_Subtyp := Any_Composite;
1057 Aggr_Subtyp := Array_Aggr_Subtype (N, Typ);
1060 Set_Etype (N, Aggr_Subtyp);
1061 end Array_Aggregate;
1063 elsif Is_Private_Type (Typ)
1064 and then Present (Full_View (Typ))
1065 and then (In_Inlined_Body or In_Instance_Body)
1066 and then Is_Composite_Type (Full_View (Typ))
1068 Resolve (N, Full_View (Typ));
1071 Error_Msg_N ("illegal context for aggregate", N);
1074 -- If we can determine statically that the evaluation of the aggregate
1075 -- raises Constraint_Error, then replace the aggregate with an
1076 -- N_Raise_Constraint_Error node, but set the Etype to the right
1077 -- aggregate subtype. Gigi needs this.
1079 if Raises_Constraint_Error (N) then
1080 Aggr_Subtyp := Etype (N);
1082 Make_Raise_Constraint_Error (Loc, Reason => CE_Range_Check_Failed));
1083 Set_Raises_Constraint_Error (N);
1084 Set_Etype (N, Aggr_Subtyp);
1088 if Warn_On_No_Value_Assigned
1090 and then not Is_Fully_Initialized_Type (Etype (N))
1092 Error_Msg_N ("?v?aggregate not fully initialized", N);
1095 Check_Function_Writable_Actuals (N);
1096 end Resolve_Aggregate;
1098 -----------------------------
1099 -- Resolve_Array_Aggregate --
1100 -----------------------------
1102 function Resolve_Array_Aggregate
1105 Index_Constr : Node_Id;
1106 Component_Typ : Entity_Id;
1107 Others_Allowed : Boolean) return Boolean
1109 Loc : constant Source_Ptr := Sloc (N);
1111 Failure : constant Boolean := False;
1112 Success : constant Boolean := True;
1114 Index_Typ : constant Entity_Id := Etype (Index);
1115 Index_Typ_Low : constant Node_Id := Type_Low_Bound (Index_Typ);
1116 Index_Typ_High : constant Node_Id := Type_High_Bound (Index_Typ);
1117 -- The type of the index corresponding to the array sub-aggregate along
1118 -- with its low and upper bounds.
1120 Index_Base : constant Entity_Id := Base_Type (Index_Typ);
1121 Index_Base_Low : constant Node_Id := Type_Low_Bound (Index_Base);
1122 Index_Base_High : constant Node_Id := Type_High_Bound (Index_Base);
1123 -- Ditto for the base type
1125 Others_Present : Boolean := False;
1127 Nb_Choices : Nat := 0;
1128 -- Contains the overall number of named choices in this sub-aggregate
1130 function Add (Val : Uint; To : Node_Id) return Node_Id;
1131 -- Creates a new expression node where Val is added to expression To.
1132 -- Tries to constant fold whenever possible. To must be an already
1133 -- analyzed expression.
1135 procedure Check_Bound (BH : Node_Id; AH : in out Node_Id);
1136 -- Checks that AH (the upper bound of an array aggregate) is less than
1137 -- or equal to BH (the upper bound of the index base type). If the check
1138 -- fails, a warning is emitted, the Raises_Constraint_Error flag of N is
1139 -- set, and AH is replaced with a duplicate of BH.
1141 procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id);
1142 -- Checks that range AL .. AH is compatible with range L .. H. Emits a
1143 -- warning if not and sets the Raises_Constraint_Error flag in N.
1145 procedure Check_Length (L, H : Node_Id; Len : Uint);
1146 -- Checks that range L .. H contains at least Len elements. Emits a
1147 -- warning if not and sets the Raises_Constraint_Error flag in N.
1149 function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean;
1150 -- Returns True if range L .. H is dynamic or null
1152 procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean);
1153 -- Given expression node From, this routine sets OK to False if it
1154 -- cannot statically evaluate From. Otherwise it stores this static
1155 -- value into Value.
1157 function Resolve_Aggr_Expr
1159 Single_Elmt : Boolean) return Boolean;
1160 -- Resolves aggregate expression Expr. Returns False if resolution
1161 -- fails. If Single_Elmt is set to False, the expression Expr may be
1162 -- used to initialize several array aggregate elements (this can happen
1163 -- for discrete choices such as "L .. H => Expr" or the OTHERS choice).
1164 -- In this event we do not resolve Expr unless expansion is disabled.
1165 -- To know why, see the DELAYED COMPONENT RESOLUTION note above.
1167 -- NOTE: In the case of "... => <>", we pass the in the
1168 -- N_Component_Association node as Expr, since there is no Expression in
1169 -- that case, and we need a Sloc for the error message.
1171 procedure Resolve_Iterated_Component_Association
1173 Index_Typ : Entity_Id);
1180 function Add (Val : Uint; To : Node_Id) return Node_Id is
1186 if Raises_Constraint_Error (To) then
1190 -- First test if we can do constant folding
1192 if Compile_Time_Known_Value (To)
1193 or else Nkind (To) = N_Integer_Literal
1195 Expr_Pos := Make_Integer_Literal (Loc, Expr_Value (To) + Val);
1196 Set_Is_Static_Expression (Expr_Pos);
1197 Set_Etype (Expr_Pos, Etype (To));
1198 Set_Analyzed (Expr_Pos, Analyzed (To));
1200 if not Is_Enumeration_Type (Index_Typ) then
1203 -- If we are dealing with enumeration return
1204 -- Index_Typ'Val (Expr_Pos)
1208 Make_Attribute_Reference
1210 Prefix => New_Occurrence_Of (Index_Typ, Loc),
1211 Attribute_Name => Name_Val,
1212 Expressions => New_List (Expr_Pos));
1218 -- If we are here no constant folding possible
1220 if not Is_Enumeration_Type (Index_Base) then
1223 Left_Opnd => Duplicate_Subexpr (To),
1224 Right_Opnd => Make_Integer_Literal (Loc, Val));
1226 -- If we are dealing with enumeration return
1227 -- Index_Typ'Val (Index_Typ'Pos (To) + Val)
1231 Make_Attribute_Reference
1233 Prefix => New_Occurrence_Of (Index_Typ, Loc),
1234 Attribute_Name => Name_Pos,
1235 Expressions => New_List (Duplicate_Subexpr (To)));
1239 Left_Opnd => To_Pos,
1240 Right_Opnd => Make_Integer_Literal (Loc, Val));
1243 Make_Attribute_Reference
1245 Prefix => New_Occurrence_Of (Index_Typ, Loc),
1246 Attribute_Name => Name_Val,
1247 Expressions => New_List (Expr_Pos));
1249 -- If the index type has a non standard representation, the
1250 -- attributes 'Val and 'Pos expand into function calls and the
1251 -- resulting expression is considered non-safe for reevaluation
1252 -- by the backend. Relocate it into a constant temporary in order
1253 -- to make it safe for reevaluation.
1255 if Has_Non_Standard_Rep (Etype (N)) then
1260 Def_Id := Make_Temporary (Loc, 'R', Expr);
1261 Set_Etype (Def_Id, Index_Typ);
1263 Make_Object_Declaration (Loc,
1264 Defining_Identifier => Def_Id,
1265 Object_Definition =>
1266 New_Occurrence_Of (Index_Typ, Loc),
1267 Constant_Present => True,
1268 Expression => Relocate_Node (Expr)));
1270 Expr := New_Occurrence_Of (Def_Id, Loc);
1282 procedure Check_Bound (BH : Node_Id; AH : in out Node_Id) is
1290 Get (Value => Val_BH, From => BH, OK => OK_BH);
1291 Get (Value => Val_AH, From => AH, OK => OK_AH);
1293 if OK_BH and then OK_AH and then Val_BH < Val_AH then
1294 Set_Raises_Constraint_Error (N);
1295 Error_Msg_Warn := SPARK_Mode /= On;
1296 Error_Msg_N ("upper bound out of range<<", AH);
1297 Error_Msg_N ("\Constraint_Error [<<", AH);
1299 -- You need to set AH to BH or else in the case of enumerations
1300 -- indexes we will not be able to resolve the aggregate bounds.
1302 AH := Duplicate_Subexpr (BH);
1310 procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id) is
1321 pragma Warnings (Off, OK_AL);
1322 pragma Warnings (Off, OK_AH);
1325 if Raises_Constraint_Error (N)
1326 or else Dynamic_Or_Null_Range (AL, AH)
1331 Get (Value => Val_L, From => L, OK => OK_L);
1332 Get (Value => Val_H, From => H, OK => OK_H);
1334 Get (Value => Val_AL, From => AL, OK => OK_AL);
1335 Get (Value => Val_AH, From => AH, OK => OK_AH);
1337 if OK_L and then Val_L > Val_AL then
1338 Set_Raises_Constraint_Error (N);
1339 Error_Msg_Warn := SPARK_Mode /= On;
1340 Error_Msg_N ("lower bound of aggregate out of range<<", N);
1341 Error_Msg_N ("\Constraint_Error [<<", N);
1344 if OK_H and then Val_H < Val_AH then
1345 Set_Raises_Constraint_Error (N);
1346 Error_Msg_Warn := SPARK_Mode /= On;
1347 Error_Msg_N ("upper bound of aggregate out of range<<", N);
1348 Error_Msg_N ("\Constraint_Error [<<", N);
1356 procedure Check_Length (L, H : Node_Id; Len : Uint) is
1366 if Raises_Constraint_Error (N) then
1370 Get (Value => Val_L, From => L, OK => OK_L);
1371 Get (Value => Val_H, From => H, OK => OK_H);
1373 if not OK_L or else not OK_H then
1377 -- If null range length is zero
1379 if Val_L > Val_H then
1380 Range_Len := Uint_0;
1382 Range_Len := Val_H - Val_L + 1;
1385 if Range_Len < Len then
1386 Set_Raises_Constraint_Error (N);
1387 Error_Msg_Warn := SPARK_Mode /= On;
1388 Error_Msg_N ("too many elements<<", N);
1389 Error_Msg_N ("\Constraint_Error [<<", N);
1393 ---------------------------
1394 -- Dynamic_Or_Null_Range --
1395 ---------------------------
1397 function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean is
1405 Get (Value => Val_L, From => L, OK => OK_L);
1406 Get (Value => Val_H, From => H, OK => OK_H);
1408 return not OK_L or else not OK_H
1409 or else not Is_OK_Static_Expression (L)
1410 or else not Is_OK_Static_Expression (H)
1411 or else Val_L > Val_H;
1412 end Dynamic_Or_Null_Range;
1418 procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean) is
1422 if Compile_Time_Known_Value (From) then
1423 Value := Expr_Value (From);
1425 -- If expression From is something like Some_Type'Val (10) then
1428 elsif Nkind (From) = N_Attribute_Reference
1429 and then Attribute_Name (From) = Name_Val
1430 and then Compile_Time_Known_Value (First (Expressions (From)))
1432 Value := Expr_Value (First (Expressions (From)));
1439 -----------------------
1440 -- Resolve_Aggr_Expr --
1441 -----------------------
1443 function Resolve_Aggr_Expr
1445 Single_Elmt : Boolean) return Boolean
1447 Nxt_Ind : constant Node_Id := Next_Index (Index);
1448 Nxt_Ind_Constr : constant Node_Id := Next_Index (Index_Constr);
1449 -- Index is the current index corresponding to the expression
1451 Resolution_OK : Boolean := True;
1452 -- Set to False if resolution of the expression failed
1455 -- Defend against previous errors
1457 if Nkind (Expr) = N_Error
1458 or else Error_Posted (Expr)
1463 -- If the array type against which we are resolving the aggregate
1464 -- has several dimensions, the expressions nested inside the
1465 -- aggregate must be further aggregates (or strings).
1467 if Present (Nxt_Ind) then
1468 if Nkind (Expr) /= N_Aggregate then
1470 -- A string literal can appear where a one-dimensional array
1471 -- of characters is expected. If the literal looks like an
1472 -- operator, it is still an operator symbol, which will be
1473 -- transformed into a string when analyzed.
1475 if Is_Character_Type (Component_Typ)
1476 and then No (Next_Index (Nxt_Ind))
1477 and then Nkind (Expr) in N_String_Literal | N_Operator_Symbol
1479 -- A string literal used in a multidimensional array
1480 -- aggregate in place of the final one-dimensional
1481 -- aggregate must not be enclosed in parentheses.
1483 if Paren_Count (Expr) /= 0 then
1484 Error_Msg_N ("no parenthesis allowed here", Expr);
1487 Make_String_Into_Aggregate (Expr);
1490 Error_Msg_N ("nested array aggregate expected", Expr);
1492 -- If the expression is parenthesized, this may be
1493 -- a missing component association for a 1-aggregate.
1495 if Paren_Count (Expr) > 0 then
1497 ("\if single-component aggregate is intended, "
1498 & "write e.g. (1 ='> ...)", Expr);
1505 -- If it's "... => <>", nothing to resolve
1507 if Nkind (Expr) = N_Component_Association then
1508 pragma Assert (Box_Present (Expr));
1512 -- Ada 2005 (AI-231): Propagate the type to the nested aggregate.
1513 -- Required to check the null-exclusion attribute (if present).
1514 -- This value may be overridden later on.
1516 Set_Etype (Expr, Etype (N));
1518 Resolution_OK := Resolve_Array_Aggregate
1519 (Expr, Nxt_Ind, Nxt_Ind_Constr, Component_Typ, Others_Allowed);
1522 -- If it's "... => <>", nothing to resolve
1524 if Nkind (Expr) = N_Component_Association then
1525 pragma Assert (Box_Present (Expr));
1529 -- Do not resolve the expressions of discrete or others choices
1530 -- unless the expression covers a single component, or the
1531 -- expander is inactive.
1533 -- In SPARK mode, expressions that can perform side effects will
1534 -- be recognized by the gnat2why back-end, and the whole
1535 -- subprogram will be ignored. So semantic analysis can be
1536 -- performed safely.
1539 or else not Expander_Active
1540 or else In_Spec_Expression
1542 Analyze_And_Resolve (Expr, Component_Typ);
1543 Check_Expr_OK_In_Limited_Aggregate (Expr);
1544 Check_Non_Static_Context (Expr);
1545 Aggregate_Constraint_Checks (Expr, Component_Typ);
1546 Check_Unset_Reference (Expr);
1550 -- If an aggregate component has a type with predicates, an explicit
1551 -- predicate check must be applied, as for an assignment statement,
1552 -- because the aggregate might not be expanded into individual
1553 -- component assignments. If the expression covers several components
1554 -- the analysis and the predicate check take place later.
1556 if Has_Predicates (Component_Typ)
1557 and then Analyzed (Expr)
1559 Apply_Predicate_Check (Expr, Component_Typ);
1562 if Raises_Constraint_Error (Expr)
1563 and then Nkind (Parent (Expr)) /= N_Component_Association
1565 Set_Raises_Constraint_Error (N);
1568 -- If the expression has been marked as requiring a range check,
1569 -- then generate it here. It's a bit odd to be generating such
1570 -- checks in the analyzer, but harmless since Generate_Range_Check
1571 -- does nothing (other than making sure Do_Range_Check is set) if
1572 -- the expander is not active.
1574 if Do_Range_Check (Expr) then
1575 Generate_Range_Check (Expr, Component_Typ, CE_Range_Check_Failed);
1578 return Resolution_OK;
1579 end Resolve_Aggr_Expr;
1581 --------------------------------------------
1582 -- Resolve_Iterated_Component_Association --
1583 --------------------------------------------
1585 procedure Resolve_Iterated_Component_Association
1587 Index_Typ : Entity_Id)
1589 Loc : constant Source_Ptr := Sloc (N);
1598 -- An element iterator specification cannot appear in
1599 -- an array aggregate because it does not provide index
1600 -- values for the association. This must be a semantic
1601 -- check because the parser cannot tell whether this is
1602 -- an array aggregate or a container aggregate.
1604 if Present (Iterator_Specification (N)) then
1605 Error_Msg_N ("container element Iterator cannot appear "
1606 & "in an array aggregate", N);
1610 Choice := First (Discrete_Choices (N));
1612 while Present (Choice) loop
1613 if Nkind (Choice) = N_Others_Choice then
1614 Others_Present := True;
1619 -- Choice can be a subtype name, a range, or an expression
1621 if Is_Entity_Name (Choice)
1622 and then Is_Type (Entity (Choice))
1623 and then Base_Type (Entity (Choice)) = Base_Type (Index_Typ)
1628 Analyze_And_Resolve (Choice, Index_Typ);
1635 -- Create a scope in which to introduce an index, which is usually
1636 -- visible in the expression for the component, and needed for its
1639 Ent := New_Internal_Entity (E_Loop, Current_Scope, Loc, 'L');
1640 Set_Etype (Ent, Standard_Void_Type);
1641 Set_Parent (Ent, Parent (N));
1644 Make_Defining_Identifier (Loc,
1645 Chars => Chars (Defining_Identifier (N)));
1647 -- Insert and decorate the index variable in the current scope.
1648 -- The expression has to be analyzed once the index variable is
1649 -- directly visible. Mark the variable as referenced to prevent
1650 -- spurious warnings, given that subsequent uses of its name in the
1651 -- expression will reference the internal (synonym) loop variable.
1654 Set_Etype (Id, Index_Typ);
1655 Set_Ekind (Id, E_Variable);
1656 Set_Scope (Id, Ent);
1657 Set_Referenced (Id);
1659 -- Analyze a copy of the expression, to verify legality. We use
1660 -- a copy because the expression will be analyzed anew when the
1661 -- enclosing aggregate is expanded, and the construct is rewritten
1662 -- as a loop with a new index variable.
1664 Expr := New_Copy_Tree (Expression (N));
1665 Dummy := Resolve_Aggr_Expr (Expr, False);
1667 -- An iterated_component_association may appear in a nested
1668 -- aggregate for a multidimensional structure: preserve the bounds
1669 -- computed for the expression, as well as the anonymous array
1670 -- type generated for it; both are needed during array expansion.
1671 -- This does not work for more than two levels of nesting. ???
1673 if Nkind (Expr) = N_Aggregate then
1674 Set_Aggregate_Bounds (Expression (N), Aggregate_Bounds (Expr));
1675 Set_Etype (Expression (N), Etype (Expr));
1679 end Resolve_Iterated_Component_Association;
1688 Aggr_Low : Node_Id := Empty;
1689 Aggr_High : Node_Id := Empty;
1690 -- The actual low and high bounds of this sub-aggregate
1692 Case_Table_Size : Nat;
1693 -- Contains the size of the case table needed to sort aggregate choices
1695 Choices_Low : Node_Id := Empty;
1696 Choices_High : Node_Id := Empty;
1697 -- The lowest and highest discrete choices values for a named aggregate
1699 Delete_Choice : Boolean;
1700 -- Used when replacing a subtype choice with predicate by a list
1702 Nb_Elements : Uint := Uint_0;
1703 -- The number of elements in a positional aggregate
1705 Nb_Discrete_Choices : Nat := 0;
1706 -- The overall number of discrete choices (not counting others choice)
1708 -- Start of processing for Resolve_Array_Aggregate
1711 -- Ignore junk empty aggregate resulting from parser error
1713 if No (Expressions (N))
1714 and then No (Component_Associations (N))
1715 and then not Null_Record_Present (N)
1720 -- STEP 1: make sure the aggregate is correctly formatted
1722 if Present (Component_Associations (N)) then
1723 Assoc := First (Component_Associations (N));
1724 while Present (Assoc) loop
1725 if Nkind (Assoc) = N_Iterated_Component_Association then
1726 Resolve_Iterated_Component_Association (Assoc, Index_Typ);
1729 Choice := First (Choice_List (Assoc));
1730 Delete_Choice := False;
1731 while Present (Choice) loop
1732 if Nkind (Choice) = N_Others_Choice then
1733 Others_Present := True;
1735 if Choice /= First (Choice_List (Assoc))
1736 or else Present (Next (Choice))
1739 ("OTHERS must appear alone in a choice list", Choice);
1743 if Present (Next (Assoc)) then
1745 ("OTHERS must appear last in an aggregate", Choice);
1749 if Ada_Version = Ada_83
1750 and then Assoc /= First (Component_Associations (N))
1751 and then Nkind (Parent (N)) in
1752 N_Assignment_Statement | N_Object_Declaration
1755 ("(Ada 83) illegal context for OTHERS choice", N);
1758 elsif Is_Entity_Name (Choice) then
1762 E : constant Entity_Id := Entity (Choice);
1768 if Is_Type (E) and then Has_Predicates (E) then
1769 Freeze_Before (N, E);
1771 if Has_Dynamic_Predicate_Aspect (E) then
1773 ("subtype& has dynamic predicate, not allowed "
1774 & "in aggregate choice", Choice, E);
1776 elsif not Is_OK_Static_Subtype (E) then
1778 ("non-static subtype& has predicate, not allowed "
1779 & "in aggregate choice", Choice, E);
1782 -- If the subtype has a static predicate, replace the
1783 -- original choice with the list of individual values
1784 -- covered by the predicate.
1785 -- This should be deferred to expansion time ???
1787 if Present (Static_Discrete_Predicate (E)) then
1788 Delete_Choice := True;
1791 P := First (Static_Discrete_Predicate (E));
1792 while Present (P) loop
1794 Set_Sloc (C, Sloc (Choice));
1795 Append_To (New_Cs, C);
1799 Insert_List_After (Choice, New_Cs);
1805 Nb_Choices := Nb_Choices + 1;
1808 C : constant Node_Id := Choice;
1813 if Delete_Choice then
1815 Nb_Choices := Nb_Choices - 1;
1816 Delete_Choice := False;
1825 -- At this point we know that the others choice, if present, is by
1826 -- itself and appears last in the aggregate. Check if we have mixed
1827 -- positional and discrete associations (other than the others choice).
1829 if Present (Expressions (N))
1830 and then (Nb_Choices > 1
1831 or else (Nb_Choices = 1 and then not Others_Present))
1834 ("named association cannot follow positional association",
1835 First (Choice_List (First (Component_Associations (N)))));
1839 -- Test for the validity of an others choice if present
1841 if Others_Present and then not Others_Allowed then
1843 ("OTHERS choice not allowed here",
1844 First (Choices (First (Component_Associations (N)))));
1848 -- Protect against cascaded errors
1850 if Etype (Index_Typ) = Any_Type then
1854 -- STEP 2: Process named components
1856 if No (Expressions (N)) then
1857 if Others_Present then
1858 Case_Table_Size := Nb_Choices - 1;
1860 Case_Table_Size := Nb_Choices;
1864 function Empty_Range (A : Node_Id) return Boolean;
1865 -- If an association covers an empty range, some warnings on the
1866 -- expression of the association can be disabled.
1872 function Empty_Range (A : Node_Id) return Boolean is
1873 R : constant Node_Id := First (Choices (A));
1875 return No (Next (R))
1876 and then Nkind (R) = N_Range
1877 and then Compile_Time_Compare
1878 (Low_Bound (R), High_Bound (R), False) = GT;
1885 -- Denote the lowest and highest values in an aggregate choice
1887 S_Low : Node_Id := Empty;
1888 S_High : Node_Id := Empty;
1889 -- if a choice in an aggregate is a subtype indication these
1890 -- denote the lowest and highest values of the subtype
1892 Table : Case_Table_Type (1 .. Case_Table_Size);
1893 -- Used to sort all the different choice values
1895 Single_Choice : Boolean;
1896 -- Set to true every time there is a single discrete choice in a
1897 -- discrete association
1899 Prev_Nb_Discrete_Choices : Nat;
1900 -- Used to keep track of the number of discrete choices in the
1901 -- current association.
1903 Errors_Posted_On_Choices : Boolean := False;
1904 -- Keeps track of whether any choices have semantic errors
1906 -- Start of processing for Step_2
1909 -- STEP 2 (A): Check discrete choices validity
1911 Assoc := First (Component_Associations (N));
1912 while Present (Assoc) loop
1913 Prev_Nb_Discrete_Choices := Nb_Discrete_Choices;
1914 Choice := First (Choice_List (Assoc));
1919 if Nkind (Choice) = N_Others_Choice then
1920 Single_Choice := False;
1923 -- Test for subtype mark without constraint
1925 elsif Is_Entity_Name (Choice) and then
1926 Is_Type (Entity (Choice))
1928 if Base_Type (Entity (Choice)) /= Index_Base then
1930 ("invalid subtype mark in aggregate choice",
1935 -- Case of subtype indication
1937 elsif Nkind (Choice) = N_Subtype_Indication then
1938 Resolve_Discrete_Subtype_Indication (Choice, Index_Base);
1940 if Has_Dynamic_Predicate_Aspect
1941 (Entity (Subtype_Mark (Choice)))
1944 ("subtype& has dynamic predicate, "
1945 & "not allowed in aggregate choice",
1946 Choice, Entity (Subtype_Mark (Choice)));
1949 -- Does the subtype indication evaluation raise CE?
1951 Get_Index_Bounds (Subtype_Mark (Choice), S_Low, S_High);
1952 Get_Index_Bounds (Choice, Low, High);
1953 Check_Bounds (S_Low, S_High, Low, High);
1955 -- Case of range or expression
1958 Resolve (Choice, Index_Base);
1959 Check_Unset_Reference (Choice);
1960 Check_Non_Static_Context (Choice);
1962 -- If semantic errors were posted on the choice, then
1963 -- record that for possible early return from later
1964 -- processing (see handling of enumeration choices).
1966 if Error_Posted (Choice) then
1967 Errors_Posted_On_Choices := True;
1970 -- Do not range check a choice. This check is redundant
1971 -- since this test is already done when we check that the
1972 -- bounds of the array aggregate are within range.
1974 Set_Do_Range_Check (Choice, False);
1977 -- If we could not resolve the discrete choice stop here
1979 if Etype (Choice) = Any_Type then
1982 -- If the discrete choice raises CE get its original bounds
1984 elsif Nkind (Choice) = N_Raise_Constraint_Error then
1985 Set_Raises_Constraint_Error (N);
1986 Get_Index_Bounds (Original_Node (Choice), Low, High);
1988 -- Otherwise get its bounds as usual
1991 Get_Index_Bounds (Choice, Low, High);
1994 if (Dynamic_Or_Null_Range (Low, High)
1995 or else (Nkind (Choice) = N_Subtype_Indication
1997 Dynamic_Or_Null_Range (S_Low, S_High)))
1998 and then Nb_Choices /= 1
2001 ("dynamic or empty choice in aggregate "
2002 & "must be the only choice", Choice);
2006 if not (All_Composite_Constraints_Static (Low)
2007 and then All_Composite_Constraints_Static (High)
2008 and then All_Composite_Constraints_Static (S_Low)
2009 and then All_Composite_Constraints_Static (S_High))
2011 Check_Restriction (No_Dynamic_Sized_Objects, Choice);
2014 Nb_Discrete_Choices := Nb_Discrete_Choices + 1;
2015 Table (Nb_Discrete_Choices).Lo := Low;
2016 Table (Nb_Discrete_Choices).Hi := High;
2017 Table (Nb_Discrete_Choices).Choice := Choice;
2023 -- Check if we have a single discrete choice and whether
2024 -- this discrete choice specifies a single value.
2027 (Nb_Discrete_Choices = Prev_Nb_Discrete_Choices + 1)
2028 and then (Low = High);
2034 -- Ada 2005 (AI-231)
2036 if Ada_Version >= Ada_2005
2037 and then Known_Null (Expression (Assoc))
2038 and then not Empty_Range (Assoc)
2040 Check_Can_Never_Be_Null (Etype (N), Expression (Assoc));
2043 -- Ada 2005 (AI-287): In case of default initialized component
2044 -- we delay the resolution to the expansion phase.
2046 if Box_Present (Assoc) then
2048 -- Ada 2005 (AI-287): In case of default initialization of a
2049 -- component the expander will generate calls to the
2050 -- corresponding initialization subprogram. We need to call
2051 -- Resolve_Aggr_Expr to check the rules about
2054 if not Resolve_Aggr_Expr
2055 (Assoc, Single_Elmt => Single_Choice)
2060 elsif Nkind (Assoc) = N_Iterated_Component_Association then
2061 null; -- handled above, in a loop context.
2063 elsif not Resolve_Aggr_Expr
2064 (Expression (Assoc), Single_Elmt => Single_Choice)
2068 -- Check incorrect use of dynamically tagged expression
2070 -- We differentiate here two cases because the expression may
2071 -- not be decorated. For example, the analysis and resolution
2072 -- of the expression associated with the others choice will be
2073 -- done later with the full aggregate. In such case we
2074 -- duplicate the expression tree to analyze the copy and
2075 -- perform the required check.
2077 elsif not Present (Etype (Expression (Assoc))) then
2079 Save_Analysis : constant Boolean := Full_Analysis;
2080 Expr : constant Node_Id :=
2081 New_Copy_Tree (Expression (Assoc));
2084 Expander_Mode_Save_And_Set (False);
2085 Full_Analysis := False;
2087 -- Analyze the expression, making sure it is properly
2088 -- attached to the tree before we do the analysis.
2090 Set_Parent (Expr, Parent (Expression (Assoc)));
2093 -- Compute its dimensions now, rather than at the end of
2094 -- resolution, because in the case of multidimensional
2095 -- aggregates subsequent expansion may lead to spurious
2098 Check_Expression_Dimensions (Expr, Component_Typ);
2100 -- If the expression is a literal, propagate this info
2101 -- to the expression in the association, to enable some
2102 -- optimizations downstream.
2104 if Is_Entity_Name (Expr)
2105 and then Present (Entity (Expr))
2106 and then Ekind (Entity (Expr)) = E_Enumeration_Literal
2109 (Expression (Assoc), Component_Typ);
2112 Full_Analysis := Save_Analysis;
2113 Expander_Mode_Restore;
2115 if Is_Tagged_Type (Etype (Expr)) then
2116 Check_Dynamically_Tagged_Expression
2118 Typ => Component_Type (Etype (N)),
2123 elsif Is_Tagged_Type (Etype (Expression (Assoc))) then
2124 Check_Dynamically_Tagged_Expression
2125 (Expr => Expression (Assoc),
2126 Typ => Component_Type (Etype (N)),
2133 -- If aggregate contains more than one choice then these must be
2134 -- static. Check for duplicate and missing values.
2136 -- Note: there is duplicated code here wrt Check_Choice_Set in
2137 -- the body of Sem_Case, and it is possible we could just reuse
2138 -- that procedure. To be checked ???
2140 if Nb_Discrete_Choices > 1 then
2141 Check_Choices : declare
2143 -- Location of choice for messages
2147 -- High end of one range and Low end of the next. Should be
2148 -- contiguous if there is no hole in the list of values.
2152 -- End points of duplicated range
2154 Missing_Or_Duplicates : Boolean := False;
2155 -- Set True if missing or duplicate choices found
2157 procedure Output_Bad_Choices (Lo, Hi : Uint; C : Node_Id);
2158 -- Output continuation message with a representation of the
2159 -- bounds (just Lo if Lo = Hi, else Lo .. Hi). C is the
2160 -- choice node where the message is to be posted.
2162 ------------------------
2163 -- Output_Bad_Choices --
2164 ------------------------
2166 procedure Output_Bad_Choices (Lo, Hi : Uint; C : Node_Id) is
2168 -- Enumeration type case
2170 if Is_Enumeration_Type (Index_Typ) then
2172 Chars (Get_Enum_Lit_From_Pos (Index_Typ, Lo, Loc));
2174 Chars (Get_Enum_Lit_From_Pos (Index_Typ, Hi, Loc));
2177 Error_Msg_N ("\\ %!", C);
2179 Error_Msg_N ("\\ % .. %!", C);
2182 -- Integer types case
2185 Error_Msg_Uint_1 := Lo;
2186 Error_Msg_Uint_2 := Hi;
2189 Error_Msg_N ("\\ ^!", C);
2191 Error_Msg_N ("\\ ^ .. ^!", C);
2194 end Output_Bad_Choices;
2196 -- Start of processing for Check_Choices
2199 Sort_Case_Table (Table);
2201 -- First we do a quick linear loop to find out if we have
2202 -- any duplicates or missing entries (usually we have a
2203 -- legal aggregate, so this will get us out quickly).
2205 for J in 1 .. Nb_Discrete_Choices - 1 loop
2206 Hi_Val := Expr_Value (Table (J).Hi);
2207 Lo_Val := Expr_Value (Table (J + 1).Lo);
2210 or else (Lo_Val > Hi_Val + 1
2211 and then not Others_Present)
2213 Missing_Or_Duplicates := True;
2218 -- If we have missing or duplicate entries, first fill in
2219 -- the Highest entries to make life easier in the following
2220 -- loops to detect bad entries.
2222 if Missing_Or_Duplicates then
2223 Table (1).Highest := Expr_Value (Table (1).Hi);
2225 for J in 2 .. Nb_Discrete_Choices loop
2226 Table (J).Highest :=
2228 (Table (J - 1).Highest, Expr_Value (Table (J).Hi));
2231 -- Loop through table entries to find duplicate indexes
2233 for J in 2 .. Nb_Discrete_Choices loop
2234 Lo_Val := Expr_Value (Table (J).Lo);
2235 Hi_Val := Expr_Value (Table (J).Hi);
2237 -- Case where we have duplicates (the lower bound of
2238 -- this choice is less than or equal to the highest
2239 -- high bound found so far).
2241 if Lo_Val <= Table (J - 1).Highest then
2243 -- We move backwards looking for duplicates. We can
2244 -- abandon this loop as soon as we reach a choice
2245 -- highest value that is less than Lo_Val.
2247 for K in reverse 1 .. J - 1 loop
2248 exit when Table (K).Highest < Lo_Val;
2250 -- Here we may have duplicates between entries
2251 -- for K and J. Get range of duplicates.
2254 UI_Max (Lo_Val, Expr_Value (Table (K).Lo));
2256 UI_Min (Hi_Val, Expr_Value (Table (K).Hi));
2258 -- Nothing to do if duplicate range is null
2260 if Lo_Dup > Hi_Dup then
2263 -- Otherwise place proper message
2266 -- We place message on later choice, with a
2267 -- line reference to the earlier choice.
2269 if Sloc (Table (J).Choice) <
2270 Sloc (Table (K).Choice)
2272 Choice := Table (K).Choice;
2273 Error_Msg_Sloc := Sloc (Table (J).Choice);
2275 Choice := Table (J).Choice;
2276 Error_Msg_Sloc := Sloc (Table (K).Choice);
2279 if Lo_Dup = Hi_Dup then
2281 ("index value in array aggregate "
2282 & "duplicates the one given#!", Choice);
2285 ("index values in array aggregate "
2286 & "duplicate those given#!", Choice);
2289 Output_Bad_Choices (Lo_Dup, Hi_Dup, Choice);
2295 -- Loop through entries in table to find missing indexes.
2296 -- Not needed if others, since missing impossible.
2298 if not Others_Present then
2299 for J in 2 .. Nb_Discrete_Choices loop
2300 Lo_Val := Expr_Value (Table (J).Lo);
2301 Hi_Val := Table (J - 1).Highest;
2303 if Lo_Val > Hi_Val + 1 then
2306 Error_Node : Node_Id;
2309 -- If the choice is the bound of a range in
2310 -- a subtype indication, it is not in the
2311 -- source lists for the aggregate itself, so
2312 -- post the error on the aggregate. Otherwise
2313 -- post it on choice itself.
2315 Choice := Table (J).Choice;
2317 if Is_List_Member (Choice) then
2318 Error_Node := Choice;
2323 if Hi_Val + 1 = Lo_Val - 1 then
2325 ("missing index value "
2326 & "in array aggregate!", Error_Node);
2329 ("missing index values "
2330 & "in array aggregate!", Error_Node);
2334 (Hi_Val + 1, Lo_Val - 1, Error_Node);
2340 -- If either missing or duplicate values, return failure
2342 Set_Etype (N, Any_Composite);
2348 -- STEP 2 (B): Compute aggregate bounds and min/max choices values
2350 if Nb_Discrete_Choices > 0 then
2351 Choices_Low := Table (1).Lo;
2352 Choices_High := Table (Nb_Discrete_Choices).Hi;
2355 -- If Others is present, then bounds of aggregate come from the
2356 -- index constraint (not the choices in the aggregate itself).
2358 if Others_Present then
2359 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2361 -- Abandon processing if either bound is already signalled as
2362 -- an error (prevents junk cascaded messages and blow ups).
2364 if Nkind (Aggr_Low) = N_Error
2366 Nkind (Aggr_High) = N_Error
2371 -- No others clause present
2374 -- Special processing if others allowed and not present. This
2375 -- means that the bounds of the aggregate come from the index
2376 -- constraint (and the length must match).
2378 if Others_Allowed then
2379 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2381 -- Abandon processing if either bound is already signalled
2382 -- as an error (stop junk cascaded messages and blow ups).
2384 if Nkind (Aggr_Low) = N_Error
2386 Nkind (Aggr_High) = N_Error
2391 -- If others allowed, and no others present, then the array
2392 -- should cover all index values. If it does not, we will
2393 -- get a length check warning, but there is two cases where
2394 -- an additional warning is useful:
2396 -- If we have no positional components, and the length is
2397 -- wrong (which we can tell by others being allowed with
2398 -- missing components), and the index type is an enumeration
2399 -- type, then issue appropriate warnings about these missing
2400 -- components. They are only warnings, since the aggregate
2401 -- is fine, it's just the wrong length. We skip this check
2402 -- for standard character types (since there are no literals
2403 -- and it is too much trouble to concoct them), and also if
2404 -- any of the bounds have values that are not known at
2407 -- Another case warranting a warning is when the length
2408 -- is right, but as above we have an index type that is
2409 -- an enumeration, and the bounds do not match. This is a
2410 -- case where dubious sliding is allowed and we generate a
2411 -- warning that the bounds do not match.
2413 if No (Expressions (N))
2414 and then Nkind (Index) = N_Range
2415 and then Is_Enumeration_Type (Etype (Index))
2416 and then not Is_Standard_Character_Type (Etype (Index))
2417 and then Compile_Time_Known_Value (Aggr_Low)
2418 and then Compile_Time_Known_Value (Aggr_High)
2419 and then Compile_Time_Known_Value (Choices_Low)
2420 and then Compile_Time_Known_Value (Choices_High)
2422 -- If any of the expressions or range bounds in choices
2423 -- have semantic errors, then do not attempt further
2424 -- resolution, to prevent cascaded errors.
2426 if Errors_Posted_On_Choices then
2431 ALo : constant Node_Id := Expr_Value_E (Aggr_Low);
2432 AHi : constant Node_Id := Expr_Value_E (Aggr_High);
2433 CLo : constant Node_Id := Expr_Value_E (Choices_Low);
2434 CHi : constant Node_Id := Expr_Value_E (Choices_High);
2439 -- Warning case 1, missing values at start/end. Only
2440 -- do the check if the number of entries is too small.
2442 if (Enumeration_Pos (CHi) - Enumeration_Pos (CLo))
2444 (Enumeration_Pos (AHi) - Enumeration_Pos (ALo))
2447 ("missing index value(s) in array aggregate??",
2450 -- Output missing value(s) at start
2452 if Chars (ALo) /= Chars (CLo) then
2455 if Chars (ALo) = Chars (Ent) then
2456 Error_Msg_Name_1 := Chars (ALo);
2457 Error_Msg_N ("\ %??", N);
2459 Error_Msg_Name_1 := Chars (ALo);
2460 Error_Msg_Name_2 := Chars (Ent);
2461 Error_Msg_N ("\ % .. %??", N);
2465 -- Output missing value(s) at end
2467 if Chars (AHi) /= Chars (CHi) then
2470 if Chars (AHi) = Chars (Ent) then
2471 Error_Msg_Name_1 := Chars (Ent);
2472 Error_Msg_N ("\ %??", N);
2474 Error_Msg_Name_1 := Chars (Ent);
2475 Error_Msg_Name_2 := Chars (AHi);
2476 Error_Msg_N ("\ % .. %??", N);
2480 -- Warning case 2, dubious sliding. The First_Subtype
2481 -- test distinguishes between a constrained type where
2482 -- sliding is not allowed (so we will get a warning
2483 -- later that Constraint_Error will be raised), and
2484 -- the unconstrained case where sliding is permitted.
2486 elsif (Enumeration_Pos (CHi) - Enumeration_Pos (CLo))
2488 (Enumeration_Pos (AHi) - Enumeration_Pos (ALo))
2489 and then Chars (ALo) /= Chars (CLo)
2491 not Is_Constrained (First_Subtype (Etype (N)))
2494 ("bounds of aggregate do not match target??", N);
2500 -- If no others, aggregate bounds come from aggregate
2502 Aggr_Low := Choices_Low;
2503 Aggr_High := Choices_High;
2507 -- STEP 3: Process positional components
2510 -- STEP 3 (A): Process positional elements
2512 Expr := First (Expressions (N));
2513 Nb_Elements := Uint_0;
2514 while Present (Expr) loop
2515 Nb_Elements := Nb_Elements + 1;
2517 -- Ada 2005 (AI-231)
2519 if Ada_Version >= Ada_2005 and then Known_Null (Expr) then
2520 Check_Can_Never_Be_Null (Etype (N), Expr);
2523 if not Resolve_Aggr_Expr (Expr, Single_Elmt => True) then
2527 -- Check incorrect use of dynamically tagged expression
2529 if Is_Tagged_Type (Etype (Expr)) then
2530 Check_Dynamically_Tagged_Expression
2532 Typ => Component_Type (Etype (N)),
2539 if Others_Present then
2540 Assoc := Last (Component_Associations (N));
2542 -- Ada 2005 (AI-231)
2544 if Ada_Version >= Ada_2005 and then Known_Null (Assoc) then
2545 Check_Can_Never_Be_Null (Etype (N), Expression (Assoc));
2548 -- Ada 2005 (AI-287): In case of default initialized component,
2549 -- we delay the resolution to the expansion phase.
2551 if Box_Present (Assoc) then
2553 -- Ada 2005 (AI-287): In case of default initialization of a
2554 -- component the expander will generate calls to the
2555 -- corresponding initialization subprogram. We need to call
2556 -- Resolve_Aggr_Expr to check the rules about
2559 if not Resolve_Aggr_Expr (Assoc, Single_Elmt => False) then
2563 elsif not Resolve_Aggr_Expr (Expression (Assoc),
2564 Single_Elmt => False)
2568 -- Check incorrect use of dynamically tagged expression. The
2569 -- expression of the others choice has not been resolved yet.
2570 -- In order to diagnose the semantic error we create a duplicate
2571 -- tree to analyze it and perform the check.
2575 Save_Analysis : constant Boolean := Full_Analysis;
2576 Expr : constant Node_Id :=
2577 New_Copy_Tree (Expression (Assoc));
2580 Expander_Mode_Save_And_Set (False);
2581 Full_Analysis := False;
2583 Full_Analysis := Save_Analysis;
2584 Expander_Mode_Restore;
2586 if Is_Tagged_Type (Etype (Expr)) then
2587 Check_Dynamically_Tagged_Expression
2589 Typ => Component_Type (Etype (N)),
2596 -- STEP 3 (B): Compute the aggregate bounds
2598 if Others_Present then
2599 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2602 if Others_Allowed then
2603 Get_Index_Bounds (Index_Constr, Aggr_Low, Discard);
2605 Aggr_Low := Index_Typ_Low;
2608 Aggr_High := Add (Nb_Elements - 1, To => Aggr_Low);
2609 Check_Bound (Index_Base_High, Aggr_High);
2613 -- STEP 4: Perform static aggregate checks and save the bounds
2617 Check_Bounds (Index_Typ_Low, Index_Typ_High, Aggr_Low, Aggr_High);
2618 Check_Bounds (Index_Base_Low, Index_Base_High, Aggr_Low, Aggr_High);
2622 if Others_Present and then Nb_Discrete_Choices > 0 then
2623 Check_Bounds (Aggr_Low, Aggr_High, Choices_Low, Choices_High);
2624 Check_Bounds (Index_Typ_Low, Index_Typ_High,
2625 Choices_Low, Choices_High);
2626 Check_Bounds (Index_Base_Low, Index_Base_High,
2627 Choices_Low, Choices_High);
2631 elsif Others_Present and then Nb_Elements > 0 then
2632 Check_Length (Aggr_Low, Aggr_High, Nb_Elements);
2633 Check_Length (Index_Typ_Low, Index_Typ_High, Nb_Elements);
2634 Check_Length (Index_Base_Low, Index_Base_High, Nb_Elements);
2637 if Raises_Constraint_Error (Aggr_Low)
2638 or else Raises_Constraint_Error (Aggr_High)
2640 Set_Raises_Constraint_Error (N);
2643 Aggr_Low := Duplicate_Subexpr (Aggr_Low);
2645 -- Do not duplicate Aggr_High if Aggr_High = Aggr_Low + Nb_Elements
2646 -- since the addition node returned by Add is not yet analyzed. Attach
2647 -- to tree and analyze first. Reset analyzed flag to ensure it will get
2648 -- analyzed when it is a literal bound whose type must be properly set.
2650 if Others_Present or else Nb_Discrete_Choices > 0 then
2651 Aggr_High := Duplicate_Subexpr (Aggr_High);
2653 if Etype (Aggr_High) = Universal_Integer then
2654 Set_Analyzed (Aggr_High, False);
2658 -- If the aggregate already has bounds attached to it, it means this is
2659 -- a positional aggregate created as an optimization by
2660 -- Exp_Aggr.Convert_To_Positional, so we don't want to change those
2663 if Present (Aggregate_Bounds (N)) and then not Others_Allowed then
2664 Aggr_Low := Low_Bound (Aggregate_Bounds (N));
2665 Aggr_High := High_Bound (Aggregate_Bounds (N));
2668 Set_Aggregate_Bounds
2669 (N, Make_Range (Loc, Low_Bound => Aggr_Low, High_Bound => Aggr_High));
2671 -- The bounds may contain expressions that must be inserted upwards.
2672 -- Attach them fully to the tree. After analysis, remove side effects
2673 -- from upper bound, if still needed.
2675 Set_Parent (Aggregate_Bounds (N), N);
2676 Analyze_And_Resolve (Aggregate_Bounds (N), Index_Typ);
2677 Check_Unset_Reference (Aggregate_Bounds (N));
2679 if not Others_Present and then Nb_Discrete_Choices = 0 then
2681 (Aggregate_Bounds (N),
2682 Duplicate_Subexpr (High_Bound (Aggregate_Bounds (N))));
2685 -- Check the dimensions of each component in the array aggregate
2687 Analyze_Dimension_Array_Aggregate (N, Component_Typ);
2690 end Resolve_Array_Aggregate;
2692 ---------------------------------
2693 -- Resolve_Container_Aggregate --
2694 ---------------------------------
2696 procedure Resolve_Container_Aggregate (N : Node_Id; Typ : Entity_Id) is
2697 procedure Resolve_Iterated_Association
2699 Key_Type : Entity_Id;
2700 Elmt_Type : Entity_Id);
2701 -- Resolve choices and expression in an iterated component association
2702 -- or an iterated element association, which has a key_expression.
2703 -- This is similar but not identical to the handling of this construct
2704 -- in an array aggregate.
2705 -- For a named container, the type of each choice must be compatible
2706 -- with the key type. For a positional container, the choice must be
2707 -- a subtype indication or an iterator specification that determines
2710 Asp : constant Node_Id := Find_Value_Of_Aspect (Typ, Aspect_Aggregate);
2712 Empty_Subp : Node_Id := Empty;
2713 Add_Named_Subp : Node_Id := Empty;
2714 Add_Unnamed_Subp : Node_Id := Empty;
2715 New_Indexed_Subp : Node_Id := Empty;
2716 Assign_Indexed_Subp : Node_Id := Empty;
2718 ----------------------------------
2719 -- Resolve_Iterated_Association --
2720 ----------------------------------
2722 procedure Resolve_Iterated_Association
2724 Key_Type : Entity_Id;
2725 Elmt_Type : Entity_Id)
2734 Typ : Entity_Id := Empty;
2737 -- If this is an Iterated_Element_Association then either a
2738 -- an Iterator_Specification or a Loop_Parameter specification
2739 -- is present. In both cases a Key_Expression is present.
2741 if Nkind (Comp) = N_Iterated_Element_Association then
2742 if Present (Loop_Parameter_Specification (Comp)) then
2743 Analyze_Loop_Parameter_Specification
2744 (Loop_Parameter_Specification (Comp));
2745 Id_Name := Chars (Defining_Identifier
2746 (Loop_Parameter_Specification (Comp)));
2748 Iter := Copy_Separate_Tree (Iterator_Specification (Comp));
2750 Typ := Etype (Defining_Identifier (Iter));
2751 Id_Name := Chars (Defining_Identifier
2752 (Iterator_Specification (Comp)));
2755 -- Key expression must have the type of the key. We analyze
2756 -- a copy of the original expression, because it will be
2757 -- reanalyzed and copied as needed during expansion of the
2758 -- corresponding loop.
2760 Key_Expr := Key_Expression (Comp);
2761 Analyze_And_Resolve (New_Copy_Tree (Key_Expr), Key_Type);
2763 elsif Present (Iterator_Specification (Comp)) then
2764 Iter := Copy_Separate_Tree (Iterator_Specification (Comp));
2765 Id_Name := Chars (Defining_Identifier (Comp));
2767 Typ := Etype (Defining_Identifier (Iter));
2770 Choice := First (Discrete_Choices (Comp));
2772 while Present (Choice) loop
2775 -- Choice can be a subtype name, a range, or an expression
2777 if Is_Entity_Name (Choice)
2778 and then Is_Type (Entity (Choice))
2779 and then Base_Type (Entity (Choice)) = Base_Type (Key_Type)
2783 elsif Present (Key_Type) then
2784 Analyze_And_Resolve (Choice, Key_Type);
2787 Typ := Etype (Choice); -- assume unique for now
2793 Id_Name := Chars (Defining_Identifier (Comp));
2796 -- Create a scope in which to introduce an index, which is usually
2797 -- visible in the expression for the component, and needed for its
2800 Id := Make_Defining_Identifier (Sloc (Comp), Id_Name);
2801 Ent := New_Internal_Entity (E_Loop, Current_Scope, Sloc (Comp), 'L');
2802 Set_Etype (Ent, Standard_Void_Type);
2803 Set_Parent (Ent, Parent (Comp));
2806 -- Insert and decorate the loop variable in the current scope.
2807 -- The expression has to be analyzed once the loop variable is
2808 -- directly visible. Mark the variable as referenced to prevent
2809 -- spurious warnings, given that subsequent uses of its name in the
2810 -- expression will reference the internal (synonym) loop variable.
2814 if No (Key_Type) then
2815 pragma Assert (Present (Typ));
2816 Set_Etype (Id, Typ);
2818 Set_Etype (Id, Key_Type);
2821 Set_Ekind (Id, E_Variable);
2822 Set_Scope (Id, Ent);
2823 Set_Referenced (Id);
2825 -- Analyze a copy of the expression, to verify legality. We use
2826 -- a copy because the expression will be analyzed anew when the
2827 -- enclosing aggregate is expanded, and the construct is rewritten
2828 -- as a loop with a new index variable.
2830 Expr := New_Copy_Tree (Expression (Comp));
2831 Preanalyze_And_Resolve (Expr, Elmt_Type);
2834 end Resolve_Iterated_Association;
2837 pragma Assert (Nkind (Asp) = N_Aggregate);
2840 Parse_Aspect_Aggregate (Asp,
2841 Empty_Subp, Add_Named_Subp, Add_Unnamed_Subp,
2842 New_Indexed_Subp, Assign_Indexed_Subp);
2844 if Present (Add_Unnamed_Subp)
2845 and then No (New_Indexed_Subp)
2848 Elmt_Type : constant Entity_Id :=
2850 (First_Formal (Entity (Add_Unnamed_Subp))));
2854 if Present (Expressions (N)) then
2855 -- positional aggregate
2857 Comp := First (Expressions (N));
2858 while Present (Comp) loop
2859 Analyze_And_Resolve (Comp, Elmt_Type);
2864 -- Empty aggregate, to be replaced by Empty during
2865 -- expansion, or iterated component association.
2867 if Present (Component_Associations (N)) then
2869 Comp : Node_Id := First (Component_Associations (N));
2871 while Present (Comp) loop
2873 N_Iterated_Component_Association
2875 Error_Msg_N ("illegal component association "
2876 & "for unnamed container aggregate", Comp);
2879 Resolve_Iterated_Association
2880 (Comp, Empty, Elmt_Type);
2889 elsif Present (Add_Named_Subp) then
2891 -- Retrieves types of container, key, and element from the
2892 -- specified insertion procedure.
2894 Container : constant Entity_Id :=
2895 First_Formal (Entity (Add_Named_Subp));
2896 Key_Type : constant Entity_Id := Etype (Next_Formal (Container));
2897 Elmt_Type : constant Entity_Id :=
2898 Etype (Next_Formal (Next_Formal (Container)));
2903 Comp := First (Component_Associations (N));
2904 while Present (Comp) loop
2905 if Nkind (Comp) = N_Component_Association then
2906 Choice := First (Choices (Comp));
2908 while Present (Choice) loop
2909 Analyze_And_Resolve (Choice, Key_Type);
2910 if not Is_Static_Expression (Choice) then
2911 Error_Msg_N ("Choice must be static", Choice);
2917 Analyze_And_Resolve (Expression (Comp), Elmt_Type);
2919 elsif Nkind (Comp) in
2920 N_Iterated_Component_Association |
2921 N_Iterated_Element_Association
2923 Resolve_Iterated_Association
2924 (Comp, Key_Type, Elmt_Type);
2932 -- Indexed Aggregate. Both positional and indexed component
2933 -- can be present. Choices must be static values or ranges
2934 -- with static bounds.
2937 Container : constant Entity_Id :=
2938 First_Formal (Entity (Assign_Indexed_Subp));
2939 Index_Type : constant Entity_Id := Etype (Next_Formal (Container));
2940 Comp_Type : constant Entity_Id :=
2941 Etype (Next_Formal (Next_Formal (Container)));
2946 if Present (Expressions (N)) then
2947 Comp := First (Expressions (N));
2948 while Present (Comp) loop
2949 Analyze_And_Resolve (Comp, Comp_Type);
2954 if Present (Component_Associations (N)) then
2955 Comp := First (Expressions (N));
2957 while Present (Comp) loop
2958 if Nkind (Comp) = N_Component_Association then
2959 Choice := First (Choices (Comp));
2961 while Present (Choice) loop
2962 Analyze_And_Resolve (Choice, Index_Type);
2966 Analyze_And_Resolve (Expression (Comp), Comp_Type);
2968 elsif Nkind (Comp) in
2969 N_Iterated_Component_Association |
2970 N_Iterated_Element_Association
2972 Resolve_Iterated_Association
2973 (Comp, Index_Type, Comp_Type);
2981 end Resolve_Container_Aggregate;
2983 -----------------------------
2984 -- Resolve_Delta_Aggregate --
2985 -----------------------------
2987 procedure Resolve_Delta_Aggregate (N : Node_Id; Typ : Entity_Id) is
2988 Base : constant Node_Id := Expression (N);
2991 if Ada_Version < Ada_2020 then
2992 Error_Msg_N ("delta_aggregate is an Ada 202x feature", N);
2993 Error_Msg_N ("\compile with -gnat2020", N);
2996 if not Is_Composite_Type (Typ) then
2997 Error_Msg_N ("not a composite type", N);
3000 Analyze_And_Resolve (Base, Typ);
3002 if Is_Array_Type (Typ) then
3003 Resolve_Delta_Array_Aggregate (N, Typ);
3005 Resolve_Delta_Record_Aggregate (N, Typ);
3009 end Resolve_Delta_Aggregate;
3011 -----------------------------------
3012 -- Resolve_Delta_Array_Aggregate --
3013 -----------------------------------
3015 procedure Resolve_Delta_Array_Aggregate (N : Node_Id; Typ : Entity_Id) is
3016 Deltas : constant List_Id := Component_Associations (N);
3017 Index_Type : constant Entity_Id := Etype (First_Index (Typ));
3023 Assoc := First (Deltas);
3024 while Present (Assoc) loop
3025 if Nkind (Assoc) = N_Iterated_Component_Association then
3026 Choice := First (Choice_List (Assoc));
3027 while Present (Choice) loop
3028 if Nkind (Choice) = N_Others_Choice then
3030 ("others not allowed in delta aggregate", Choice);
3033 Analyze_And_Resolve (Choice, Index_Type);
3040 Id : constant Entity_Id := Defining_Identifier (Assoc);
3041 Ent : constant Entity_Id :=
3043 (E_Loop, Current_Scope, Sloc (Assoc), 'L');
3046 Set_Etype (Ent, Standard_Void_Type);
3047 Set_Parent (Ent, Assoc);
3049 if No (Scope (Id)) then
3051 Set_Etype (Id, Index_Type);
3052 Set_Ekind (Id, E_Variable);
3053 Set_Scope (Id, Ent);
3058 (New_Copy_Tree (Expression (Assoc)), Component_Type (Typ));
3063 Choice := First (Choice_List (Assoc));
3064 while Present (Choice) loop
3065 if Nkind (Choice) = N_Others_Choice then
3067 ("others not allowed in delta aggregate", Choice);
3072 if Is_Entity_Name (Choice)
3073 and then Is_Type (Entity (Choice))
3075 -- Choice covers a range of values
3077 if Base_Type (Entity (Choice)) /=
3078 Base_Type (Index_Type)
3081 ("choice does mat match index type of",
3085 Resolve (Choice, Index_Type);
3092 Analyze_And_Resolve (Expression (Assoc), Component_Type (Typ));
3097 end Resolve_Delta_Array_Aggregate;
3099 ------------------------------------
3100 -- Resolve_Delta_Record_Aggregate --
3101 ------------------------------------
3103 procedure Resolve_Delta_Record_Aggregate (N : Node_Id; Typ : Entity_Id) is
3105 -- Variables used to verify that discriminant-dependent components
3106 -- appear in the same variant.
3108 Comp_Ref : Entity_Id := Empty; -- init to avoid warning
3111 procedure Check_Variant (Id : Entity_Id);
3112 -- If a given component of the delta aggregate appears in a variant
3113 -- part, verify that it is within the same variant as that of previous
3114 -- specified variant components of the delta.
3116 function Get_Component (Nam : Node_Id) return Entity_Id;
3117 -- Locate component with a given name and return it. If none found then
3118 -- report error and return Empty.
3120 function Nested_In (V1 : Node_Id; V2 : Node_Id) return Boolean;
3121 -- Determine whether variant V1 is within variant V2
3123 function Variant_Depth (N : Node_Id) return Integer;
3124 -- Determine the distance of a variant to the enclosing type
3127 --------------------
3129 --------------------
3131 procedure Check_Variant (Id : Entity_Id) is
3133 Comp_Variant : Node_Id;
3136 if not Has_Discriminants (Typ) then
3140 Comp := First_Entity (Typ);
3141 while Present (Comp) loop
3142 exit when Chars (Comp) = Chars (Id);
3143 Next_Component (Comp);
3146 -- Find the variant, if any, whose component list includes the
3147 -- component declaration.
3149 Comp_Variant := Parent (Parent (List_Containing (Parent (Comp))));
3150 if Nkind (Comp_Variant) = N_Variant then
3151 if No (Variant) then
3152 Variant := Comp_Variant;
3155 elsif Variant /= Comp_Variant then
3157 D1 : constant Integer := Variant_Depth (Variant);
3158 D2 : constant Integer := Variant_Depth (Comp_Variant);
3163 (D1 > D2 and then not Nested_In (Variant, Comp_Variant))
3165 (D2 > D1 and then not Nested_In (Comp_Variant, Variant))
3167 pragma Assert (Present (Comp_Ref));
3168 Error_Msg_Node_2 := Comp_Ref;
3170 ("& and & appear in different variants", Id, Comp);
3172 -- Otherwise retain the deeper variant for subsequent tests
3175 Variant := Comp_Variant;
3186 function Get_Component (Nam : Node_Id) return Entity_Id is
3190 Comp := First_Entity (Typ);
3191 while Present (Comp) loop
3192 if Chars (Comp) = Chars (Nam) then
3193 if Ekind (Comp) = E_Discriminant then
3194 Error_Msg_N ("delta cannot apply to discriminant", Nam);
3203 Error_Msg_NE ("type& has no component with this name", Nam, Typ);
3211 function Nested_In (V1, V2 : Node_Id) return Boolean is
3216 while Nkind (Par) /= N_Full_Type_Declaration loop
3221 Par := Parent (Par);
3231 function Variant_Depth (N : Node_Id) return Integer is
3238 while Nkind (Par) /= N_Full_Type_Declaration loop
3240 Par := Parent (Par);
3248 Deltas : constant List_Id := Component_Associations (N);
3253 Comp_Type : Entity_Id := Empty; -- init to avoid warning
3255 -- Start of processing for Resolve_Delta_Record_Aggregate
3260 Assoc := First (Deltas);
3261 while Present (Assoc) loop
3262 Choice := First (Choice_List (Assoc));
3263 while Present (Choice) loop
3264 Comp := Get_Component (Choice);
3266 if Present (Comp) then
3267 Check_Variant (Choice);
3269 Comp_Type := Etype (Comp);
3271 -- Decorate the component reference by setting its entity and
3272 -- type, as otherwise backends like GNATprove would have to
3273 -- rediscover this information by themselves.
3275 Set_Entity (Choice, Comp);
3276 Set_Etype (Choice, Comp_Type);
3278 Comp_Type := Any_Type;
3284 pragma Assert (Present (Comp_Type));
3285 Analyze_And_Resolve (Expression (Assoc), Comp_Type);
3288 end Resolve_Delta_Record_Aggregate;
3290 ---------------------------------
3291 -- Resolve_Extension_Aggregate --
3292 ---------------------------------
3294 -- There are two cases to consider:
3296 -- a) If the ancestor part is a type mark, the components needed are the
3297 -- difference between the components of the expected type and the
3298 -- components of the given type mark.
3300 -- b) If the ancestor part is an expression, it must be unambiguous, and
3301 -- once we have its type we can also compute the needed components as in
3302 -- the previous case. In both cases, if the ancestor type is not the
3303 -- immediate ancestor, we have to build this ancestor recursively.
3305 -- In both cases, discriminants of the ancestor type do not play a role in
3306 -- the resolution of the needed components, because inherited discriminants
3307 -- cannot be used in a type extension. As a result we can compute
3308 -- independently the list of components of the ancestor type and of the
3311 procedure Resolve_Extension_Aggregate (N : Node_Id; Typ : Entity_Id) is
3312 A : constant Node_Id := Ancestor_Part (N);
3317 function Valid_Limited_Ancestor (Anc : Node_Id) return Boolean;
3318 -- If the type is limited, verify that the ancestor part is a legal
3319 -- expression (aggregate or function call, including 'Input)) that does
3320 -- not require a copy, as specified in 7.5(2).
3322 function Valid_Ancestor_Type return Boolean;
3323 -- Verify that the type of the ancestor part is a non-private ancestor
3324 -- of the expected type, which must be a type extension.
3326 procedure Transform_BIP_Assignment (Typ : Entity_Id);
3327 -- For an extension aggregate whose ancestor part is a build-in-place
3328 -- call returning a nonlimited type, this is used to transform the
3329 -- assignment to the ancestor part to use a temp.
3331 ----------------------------
3332 -- Valid_Limited_Ancestor --
3333 ----------------------------
3335 function Valid_Limited_Ancestor (Anc : Node_Id) return Boolean is
3337 if Is_Entity_Name (Anc) and then Is_Type (Entity (Anc)) then
3340 -- The ancestor must be a call or an aggregate, but a call may
3341 -- have been expanded into a temporary, so check original node.
3343 elsif Nkind (Anc) in N_Aggregate
3344 | N_Extension_Aggregate
3349 elsif Nkind (Original_Node (Anc)) = N_Function_Call then
3352 elsif Nkind (Anc) = N_Attribute_Reference
3353 and then Attribute_Name (Anc) = Name_Input
3357 elsif Nkind (Anc) = N_Qualified_Expression then
3358 return Valid_Limited_Ancestor (Expression (Anc));
3360 elsif Nkind (Anc) = N_Raise_Expression then
3366 end Valid_Limited_Ancestor;
3368 -------------------------
3369 -- Valid_Ancestor_Type --
3370 -------------------------
3372 function Valid_Ancestor_Type return Boolean is
3373 Imm_Type : Entity_Id;
3376 Imm_Type := Base_Type (Typ);
3377 while Is_Derived_Type (Imm_Type) loop
3378 if Etype (Imm_Type) = Base_Type (A_Type) then
3381 -- The base type of the parent type may appear as a private
3382 -- extension if it is declared as such in a parent unit of the
3383 -- current one. For consistency of the subsequent analysis use
3384 -- the partial view for the ancestor part.
3386 elsif Is_Private_Type (Etype (Imm_Type))
3387 and then Present (Full_View (Etype (Imm_Type)))
3388 and then Base_Type (A_Type) = Full_View (Etype (Imm_Type))
3390 A_Type := Etype (Imm_Type);
3393 -- The parent type may be a private extension. The aggregate is
3394 -- legal if the type of the aggregate is an extension of it that
3395 -- is not a private extension.
3397 elsif Is_Private_Type (A_Type)
3398 and then not Is_Private_Type (Imm_Type)
3399 and then Present (Full_View (A_Type))
3400 and then Base_Type (Full_View (A_Type)) = Etype (Imm_Type)
3404 -- The parent type may be a raise expression (which is legal in
3405 -- any expression context).
3407 elsif A_Type = Raise_Type then
3408 A_Type := Etype (Imm_Type);
3412 Imm_Type := Etype (Base_Type (Imm_Type));
3416 -- If previous loop did not find a proper ancestor, report error
3418 Error_Msg_NE ("expect ancestor type of &", A, Typ);
3420 end Valid_Ancestor_Type;
3422 ------------------------------
3423 -- Transform_BIP_Assignment --
3424 ------------------------------
3426 procedure Transform_BIP_Assignment (Typ : Entity_Id) is
3427 Loc : constant Source_Ptr := Sloc (N);
3428 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'Y', A);
3429 Obj_Decl : constant Node_Id :=
3430 Make_Object_Declaration (Loc,
3431 Defining_Identifier => Def_Id,
3432 Constant_Present => True,
3433 Object_Definition => New_Occurrence_Of (Typ, Loc),
3435 Has_Init_Expression => True);
3437 Set_Etype (Def_Id, Typ);
3438 Set_Ancestor_Part (N, New_Occurrence_Of (Def_Id, Loc));
3439 Insert_Action (N, Obj_Decl);
3440 end Transform_BIP_Assignment;
3442 -- Start of processing for Resolve_Extension_Aggregate
3445 -- Analyze the ancestor part and account for the case where it is a
3446 -- parameterless function call.
3449 Check_Parameterless_Call (A);
3451 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
3453 -- AI05-0115: if the ancestor part is a subtype mark, the ancestor
3454 -- must not have unknown discriminants.
3456 if Has_Unknown_Discriminants (Entity (A)) then
3458 ("aggregate not available for type& whose ancestor "
3459 & "has unknown discriminants", N, Typ);
3463 if not Is_Tagged_Type (Typ) then
3464 Error_Msg_N ("type of extension aggregate must be tagged", N);
3467 elsif Is_Limited_Type (Typ) then
3469 -- Ada 2005 (AI-287): Limited aggregates are allowed
3471 if Ada_Version < Ada_2005 then
3472 Error_Msg_N ("aggregate type cannot be limited", N);
3473 Explain_Limited_Type (Typ, N);
3476 elsif Valid_Limited_Ancestor (A) then
3481 ("limited ancestor part must be aggregate or function call", A);
3484 elsif Is_Class_Wide_Type (Typ) then
3485 Error_Msg_N ("aggregate cannot be of a class-wide type", N);
3489 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
3490 A_Type := Get_Full_View (Entity (A));
3492 if Valid_Ancestor_Type then
3493 Set_Entity (A, A_Type);
3494 Set_Etype (A, A_Type);
3496 Validate_Ancestor_Part (N);
3497 Resolve_Record_Aggregate (N, Typ);
3500 elsif Nkind (A) /= N_Aggregate then
3501 if Is_Overloaded (A) then
3504 Get_First_Interp (A, I, It);
3505 while Present (It.Typ) loop
3507 -- Consider limited interpretations if Ada 2005 or higher
3509 if Is_Tagged_Type (It.Typ)
3510 and then (Ada_Version >= Ada_2005
3511 or else not Is_Limited_Type (It.Typ))
3513 if A_Type /= Any_Type then
3514 Error_Msg_N ("cannot resolve expression", A);
3521 Get_Next_Interp (I, It);
3524 if A_Type = Any_Type then
3525 if Ada_Version >= Ada_2005 then
3527 ("ancestor part must be of a tagged type", A);
3530 ("ancestor part must be of a nonlimited tagged type", A);
3537 A_Type := Etype (A);
3540 if Valid_Ancestor_Type then
3541 Resolve (A, A_Type);
3542 Check_Unset_Reference (A);
3543 Check_Non_Static_Context (A);
3545 -- The aggregate is illegal if the ancestor expression is a call
3546 -- to a function with a limited unconstrained result, unless the
3547 -- type of the aggregate is a null extension. This restriction
3548 -- was added in AI05-67 to simplify implementation.
3550 if Nkind (A) = N_Function_Call
3551 and then Is_Limited_Type (A_Type)
3552 and then not Is_Null_Extension (Typ)
3553 and then not Is_Constrained (A_Type)
3556 ("type of limited ancestor part must be constrained", A);
3558 -- Reject the use of CPP constructors that leave objects partially
3559 -- initialized. For example:
3561 -- type CPP_Root is tagged limited record ...
3562 -- pragma Import (CPP, CPP_Root);
3564 -- type CPP_DT is new CPP_Root and Iface ...
3565 -- pragma Import (CPP, CPP_DT);
3567 -- type Ada_DT is new CPP_DT with ...
3569 -- Obj : Ada_DT := Ada_DT'(New_CPP_Root with others => <>);
3571 -- Using the constructor of CPP_Root the slots of the dispatch
3572 -- table of CPP_DT cannot be set, and the secondary tag of
3573 -- CPP_DT is unknown.
3575 elsif Nkind (A) = N_Function_Call
3576 and then Is_CPP_Constructor_Call (A)
3577 and then Enclosing_CPP_Parent (Typ) /= A_Type
3580 ("??must use 'C'P'P constructor for type &", A,
3581 Enclosing_CPP_Parent (Typ));
3583 -- The following call is not needed if the previous warning
3584 -- is promoted to an error.
3586 Resolve_Record_Aggregate (N, Typ);
3588 elsif Is_Class_Wide_Type (Etype (A))
3589 and then Nkind (Original_Node (A)) = N_Function_Call
3591 -- If the ancestor part is a dispatching call, it appears
3592 -- statically to be a legal ancestor, but it yields any member
3593 -- of the class, and it is not possible to determine whether
3594 -- it is an ancestor of the extension aggregate (much less
3595 -- which ancestor). It is not possible to determine the
3596 -- components of the extension part.
3598 -- This check implements AI-306, which in fact was motivated by
3599 -- an AdaCore query to the ARG after this test was added.
3601 Error_Msg_N ("ancestor part must be statically tagged", A);
3603 -- We are using the build-in-place protocol, but we can't build
3604 -- in place, because we need to call the function before
3605 -- allocating the aggregate. Could do better for null
3606 -- extensions, and maybe for nondiscriminated types.
3607 -- This is wrong for limited, but those were wrong already.
3609 if not Is_Limited_View (A_Type)
3610 and then Is_Build_In_Place_Function_Call (A)
3612 Transform_BIP_Assignment (A_Type);
3615 Resolve_Record_Aggregate (N, Typ);
3620 Error_Msg_N ("no unique type for this aggregate", A);
3623 Check_Function_Writable_Actuals (N);
3624 end Resolve_Extension_Aggregate;
3626 ------------------------------
3627 -- Resolve_Record_Aggregate --
3628 ------------------------------
3630 procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id) is
3631 New_Assoc_List : constant List_Id := New_List;
3632 -- New_Assoc_List is the newly built list of N_Component_Association
3635 Others_Etype : Entity_Id := Empty;
3636 -- This variable is used to save the Etype of the last record component
3637 -- that takes its value from the others choice. Its purpose is:
3639 -- (a) make sure the others choice is useful
3641 -- (b) make sure the type of all the components whose value is
3642 -- subsumed by the others choice are the same.
3644 -- This variable is updated as a side effect of function Get_Value.
3646 Box_Node : Node_Id := Empty;
3647 Is_Box_Present : Boolean := False;
3648 Others_Box : Natural := 0;
3649 -- Ada 2005 (AI-287): Variables used in case of default initialization
3650 -- to provide a functionality similar to Others_Etype. Box_Present
3651 -- indicates that the component takes its default initialization;
3652 -- Others_Box counts the number of components of the current aggregate
3653 -- (which may be a sub-aggregate of a larger one) that are default-
3654 -- initialized. A value of One indicates that an others_box is present.
3655 -- Any larger value indicates that the others_box is not redundant.
3656 -- These variables, similar to Others_Etype, are also updated as a side
3657 -- effect of function Get_Value. Box_Node is used to place a warning on
3658 -- a redundant others_box.
3660 procedure Add_Association
3661 (Component : Entity_Id;
3663 Assoc_List : List_Id;
3664 Is_Box_Present : Boolean := False);
3665 -- Builds a new N_Component_Association node which associates Component
3666 -- to expression Expr and adds it to the association list being built,
3667 -- either New_Assoc_List, or the association being built for an inner
3670 procedure Add_Discriminant_Values
3671 (New_Aggr : Node_Id;
3672 Assoc_List : List_Id);
3673 -- The constraint to a component may be given by a discriminant of the
3674 -- enclosing type, in which case we have to retrieve its value, which is
3675 -- part of the enclosing aggregate. Assoc_List provides the discriminant
3676 -- associations of the current type or of some enclosing record.
3678 function Discriminant_Present (Input_Discr : Entity_Id) return Boolean;
3679 -- If aggregate N is a regular aggregate this routine will return True.
3680 -- Otherwise, if N is an extension aggregate, then Input_Discr denotes
3681 -- a discriminant whose value may already have been specified by N's
3682 -- ancestor part. This routine checks whether this is indeed the case
3683 -- and if so returns False, signaling that no value for Input_Discr
3684 -- should appear in N's aggregate part. Also, in this case, the routine
3685 -- appends to New_Assoc_List the discriminant value specified in the
3688 -- If the aggregate is in a context with expansion delayed, it will be
3689 -- reanalyzed. The inherited discriminant values must not be reinserted
3690 -- in the component list to prevent spurious errors, but they must be
3691 -- present on first analysis to build the proper subtype indications.
3692 -- The flag Inherited_Discriminant is used to prevent the re-insertion.
3694 function Find_Private_Ancestor (Typ : Entity_Id) return Entity_Id;
3695 -- AI05-0115: Find earlier ancestor in the derivation chain that is
3696 -- derived from private view Typ. Whether the aggregate is legal depends
3697 -- on the current visibility of the type as well as that of the parent
3701 (Compon : Entity_Id;
3703 Consider_Others_Choice : Boolean := False) return Node_Id;
3704 -- Given a record component stored in parameter Compon, this function
3705 -- returns its value as it appears in the list From, which is a list
3706 -- of N_Component_Association nodes.
3708 -- If no component association has a choice for the searched component,
3709 -- the value provided by the others choice is returned, if there is one,
3710 -- and Consider_Others_Choice is set to true. Otherwise Empty is
3711 -- returned. If there is more than one component association giving a
3712 -- value for the searched record component, an error message is emitted
3713 -- and the first found value is returned.
3715 -- If Consider_Others_Choice is set and the returned expression comes
3716 -- from the others choice, then Others_Etype is set as a side effect.
3717 -- An error message is emitted if the components taking their value from
3718 -- the others choice do not have same type.
3720 procedure Propagate_Discriminants
3722 Assoc_List : List_Id);
3723 -- Nested components may themselves be discriminated types constrained
3724 -- by outer discriminants, whose values must be captured before the
3725 -- aggregate is expanded into assignments.
3727 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Entity_Id);
3728 -- Analyzes and resolves expression Expr against the Etype of the
3729 -- Component. This routine also applies all appropriate checks to Expr.
3730 -- It finally saves a Expr in the newly created association list that
3731 -- will be attached to the final record aggregate. Note that if the
3732 -- Parent pointer of Expr is not set then Expr was produced with a
3733 -- New_Copy_Tree or some such.
3735 procedure Rewrite_Range (Root_Type : Entity_Id; Rge : Node_Id);
3736 -- Rewrite a range node Rge when its bounds refer to non-stored
3737 -- discriminants from Root_Type, to replace them with the stored
3738 -- discriminant values. This is required in GNATprove mode, and is
3739 -- adopted in all modes to avoid special-casing GNATprove mode.
3741 ---------------------
3742 -- Add_Association --
3743 ---------------------
3745 procedure Add_Association
3746 (Component : Entity_Id;
3748 Assoc_List : List_Id;
3749 Is_Box_Present : Boolean := False)
3751 Choice_List : constant List_Id := New_List;
3755 -- If this is a box association the expression is missing, so use the
3756 -- Sloc of the aggregate itself for the new association.
3758 pragma Assert (Present (Expr) xor Is_Box_Present);
3760 if Present (Expr) then
3766 Append_To (Choice_List, New_Occurrence_Of (Component, Loc));
3768 Append_To (Assoc_List,
3769 Make_Component_Association (Loc,
3770 Choices => Choice_List,
3772 Box_Present => Is_Box_Present));
3773 end Add_Association;
3775 -----------------------------
3776 -- Add_Discriminant_Values --
3777 -----------------------------
3779 procedure Add_Discriminant_Values
3780 (New_Aggr : Node_Id;
3781 Assoc_List : List_Id)
3785 Discr_Elmt : Elmt_Id;
3786 Discr_Val : Node_Id;
3790 Discr := First_Discriminant (Etype (New_Aggr));
3791 Discr_Elmt := First_Elmt (Discriminant_Constraint (Etype (New_Aggr)));
3792 while Present (Discr_Elmt) loop
3793 Discr_Val := Node (Discr_Elmt);
3795 -- If the constraint is given by a discriminant then it is a
3796 -- discriminant of an enclosing record, and its value has already
3797 -- been placed in the association list.
3799 if Is_Entity_Name (Discr_Val)
3800 and then Ekind (Entity (Discr_Val)) = E_Discriminant
3802 Val := Entity (Discr_Val);
3804 Assoc := First (Assoc_List);
3805 while Present (Assoc) loop
3806 if Present (Entity (First (Choices (Assoc))))
3807 and then Entity (First (Choices (Assoc))) = Val
3809 Discr_Val := Expression (Assoc);
3818 (Discr, New_Copy_Tree (Discr_Val),
3819 Component_Associations (New_Aggr));
3821 -- If the discriminant constraint is a current instance, mark the
3822 -- current aggregate so that the self-reference can be expanded
3823 -- later. The constraint may refer to the subtype of aggregate, so
3824 -- use base type for comparison.
3826 if Nkind (Discr_Val) = N_Attribute_Reference
3827 and then Is_Entity_Name (Prefix (Discr_Val))
3828 and then Is_Type (Entity (Prefix (Discr_Val)))
3829 and then Base_Type (Etype (N)) = Entity (Prefix (Discr_Val))
3831 Set_Has_Self_Reference (N);
3834 Next_Elmt (Discr_Elmt);
3835 Next_Discriminant (Discr);
3837 end Add_Discriminant_Values;
3839 --------------------------
3840 -- Discriminant_Present --
3841 --------------------------
3843 function Discriminant_Present (Input_Discr : Entity_Id) return Boolean is
3844 Regular_Aggr : constant Boolean := Nkind (N) /= N_Extension_Aggregate;
3846 Ancestor_Is_Subtyp : Boolean;
3851 Ancestor_Typ : Entity_Id;
3852 Comp_Assoc : Node_Id;
3854 Discr_Expr : Node_Id;
3855 Discr_Val : Elmt_Id := No_Elmt;
3856 Orig_Discr : Entity_Id;
3859 if Regular_Aggr then
3863 -- Check whether inherited discriminant values have already been
3864 -- inserted in the aggregate. This will be the case if we are
3865 -- re-analyzing an aggregate whose expansion was delayed.
3867 if Present (Component_Associations (N)) then
3868 Comp_Assoc := First (Component_Associations (N));
3869 while Present (Comp_Assoc) loop
3870 if Inherited_Discriminant (Comp_Assoc) then
3878 Ancestor := Ancestor_Part (N);
3879 Ancestor_Typ := Etype (Ancestor);
3880 Loc := Sloc (Ancestor);
3882 -- For a private type with unknown discriminants, use the underlying
3883 -- record view if it is available.
3885 if Has_Unknown_Discriminants (Ancestor_Typ)
3886 and then Present (Full_View (Ancestor_Typ))
3887 and then Present (Underlying_Record_View (Full_View (Ancestor_Typ)))
3889 Ancestor_Typ := Underlying_Record_View (Full_View (Ancestor_Typ));
3892 Ancestor_Is_Subtyp :=
3893 Is_Entity_Name (Ancestor) and then Is_Type (Entity (Ancestor));
3895 -- If the ancestor part has no discriminants clearly N's aggregate
3896 -- part must provide a value for Discr.
3898 if not Has_Discriminants (Ancestor_Typ) then
3901 -- If the ancestor part is an unconstrained subtype mark then the
3902 -- Discr must be present in N's aggregate part.
3904 elsif Ancestor_Is_Subtyp
3905 and then not Is_Constrained (Entity (Ancestor))
3910 -- Now look to see if Discr was specified in the ancestor part
3912 if Ancestor_Is_Subtyp then
3914 First_Elmt (Discriminant_Constraint (Entity (Ancestor)));
3917 Orig_Discr := Original_Record_Component (Input_Discr);
3919 Discr := First_Discriminant (Ancestor_Typ);
3920 while Present (Discr) loop
3922 -- If Ancestor has already specified Disc value then insert its
3923 -- value in the final aggregate.
3925 if Original_Record_Component (Discr) = Orig_Discr then
3926 if Ancestor_Is_Subtyp then
3927 Discr_Expr := New_Copy_Tree (Node (Discr_Val));
3930 Make_Selected_Component (Loc,
3931 Prefix => Duplicate_Subexpr (Ancestor),
3932 Selector_Name => New_Occurrence_Of (Input_Discr, Loc));
3935 Resolve_Aggr_Expr (Discr_Expr, Input_Discr);
3936 Set_Inherited_Discriminant (Last (New_Assoc_List));
3940 Next_Discriminant (Discr);
3942 if Ancestor_Is_Subtyp then
3943 Next_Elmt (Discr_Val);
3948 end Discriminant_Present;
3950 ---------------------------
3951 -- Find_Private_Ancestor --
3952 ---------------------------
3954 function Find_Private_Ancestor (Typ : Entity_Id) return Entity_Id is
3960 if Has_Private_Ancestor (Par)
3961 and then not Has_Private_Ancestor (Etype (Base_Type (Par)))
3965 elsif not Is_Derived_Type (Par) then
3969 Par := Etype (Base_Type (Par));
3972 end Find_Private_Ancestor;
3979 (Compon : Entity_Id;
3981 Consider_Others_Choice : Boolean := False) return Node_Id
3983 Typ : constant Entity_Id := Etype (Compon);
3985 Expr : Node_Id := Empty;
3986 Selector_Name : Node_Id;
3989 Is_Box_Present := False;
3995 Assoc := First (From);
3996 while Present (Assoc) loop
3997 Selector_Name := First (Choices (Assoc));
3998 while Present (Selector_Name) loop
3999 if Nkind (Selector_Name) = N_Others_Choice then
4000 if Consider_Others_Choice and then No (Expr) then
4002 -- We need to duplicate the expression for each
4003 -- successive component covered by the others choice.
4004 -- This is redundant if the others_choice covers only
4005 -- one component (small optimization possible???), but
4006 -- indispensable otherwise, because each one must be
4007 -- expanded individually to preserve side effects.
4009 -- Ada 2005 (AI-287): In case of default initialization
4010 -- of components, we duplicate the corresponding default
4011 -- expression (from the record type declaration). The
4012 -- copy must carry the sloc of the association (not the
4013 -- original expression) to prevent spurious elaboration
4014 -- checks when the default includes function calls.
4016 if Box_Present (Assoc) then
4017 Others_Box := Others_Box + 1;
4018 Is_Box_Present := True;
4020 if Expander_Active then
4022 New_Copy_Tree_And_Copy_Dimensions
4023 (Expression (Parent (Compon)),
4024 New_Sloc => Sloc (Assoc));
4026 return Expression (Parent (Compon));
4030 if Present (Others_Etype)
4031 and then Base_Type (Others_Etype) /= Base_Type (Typ)
4033 -- If the components are of an anonymous access
4034 -- type they are distinct, but this is legal in
4035 -- Ada 2012 as long as designated types match.
4037 if (Ekind (Typ) = E_Anonymous_Access_Type
4038 or else Ekind (Typ) =
4039 E_Anonymous_Access_Subprogram_Type)
4040 and then Designated_Type (Typ) =
4041 Designated_Type (Others_Etype)
4046 ("components in OTHERS choice must have same "
4047 & "type", Selector_Name);
4051 Others_Etype := Typ;
4053 -- Copy the expression so that it is resolved
4054 -- independently for each component, This is needed
4055 -- for accessibility checks on components of anonymous
4056 -- access types, even in compile_only mode.
4058 if not Inside_A_Generic then
4060 New_Copy_Tree_And_Copy_Dimensions
4061 (Expression (Assoc));
4063 return Expression (Assoc);
4068 elsif Chars (Compon) = Chars (Selector_Name) then
4071 -- Ada 2005 (AI-231)
4073 if Ada_Version >= Ada_2005
4074 and then Known_Null (Expression (Assoc))
4076 Check_Can_Never_Be_Null (Compon, Expression (Assoc));
4079 -- We need to duplicate the expression when several
4080 -- components are grouped together with a "|" choice.
4081 -- For instance "filed1 | filed2 => Expr"
4083 -- Ada 2005 (AI-287)
4085 if Box_Present (Assoc) then
4086 Is_Box_Present := True;
4088 -- Duplicate the default expression of the component
4089 -- from the record type declaration, so a new copy
4090 -- can be attached to the association.
4092 -- Note that we always copy the default expression,
4093 -- even when the association has a single choice, in
4094 -- order to create a proper association for the
4095 -- expanded aggregate.
4097 -- Component may have no default, in which case the
4098 -- expression is empty and the component is default-
4099 -- initialized, but an association for the component
4100 -- exists, and it is not covered by an others clause.
4102 -- Scalar and private types have no initialization
4103 -- procedure, so they remain uninitialized. If the
4104 -- target of the aggregate is a constant this
4105 -- deserves a warning.
4107 if No (Expression (Parent (Compon)))
4108 and then not Has_Non_Null_Base_Init_Proc (Typ)
4109 and then not Has_Aspect (Typ, Aspect_Default_Value)
4110 and then not Is_Concurrent_Type (Typ)
4111 and then Nkind (Parent (N)) = N_Object_Declaration
4112 and then Constant_Present (Parent (N))
4114 Error_Msg_Node_2 := Typ;
4116 ("component&? of type& is uninitialized",
4117 Assoc, Selector_Name);
4119 -- An additional reminder if the component type
4120 -- is a generic formal.
4122 if Is_Generic_Type (Base_Type (Typ)) then
4124 ("\instance should provide actual type with "
4125 & "initialization for&", Assoc, Typ);
4130 New_Copy_Tree_And_Copy_Dimensions
4131 (Expression (Parent (Compon)));
4134 if Present (Next (Selector_Name)) then
4135 Expr := New_Copy_Tree_And_Copy_Dimensions
4136 (Expression (Assoc));
4138 Expr := Expression (Assoc);
4142 Generate_Reference (Compon, Selector_Name, 'm');
4146 ("more than one value supplied for &",
4147 Selector_Name, Compon);
4152 Next (Selector_Name);
4161 -----------------------------
4162 -- Propagate_Discriminants --
4163 -----------------------------
4165 procedure Propagate_Discriminants
4167 Assoc_List : List_Id)
4169 Loc : constant Source_Ptr := Sloc (N);
4171 procedure Process_Component (Comp : Entity_Id);
4172 -- Add one component with a box association to the inner aggregate,
4173 -- and recurse if component is itself composite.
4175 -----------------------
4176 -- Process_Component --
4177 -----------------------
4179 procedure Process_Component (Comp : Entity_Id) is
4180 T : constant Entity_Id := Etype (Comp);
4184 if Is_Record_Type (T) and then Has_Discriminants (T) then
4185 New_Aggr := Make_Aggregate (Loc, No_List, New_List);
4186 Set_Etype (New_Aggr, T);
4189 (Comp, New_Aggr, Component_Associations (Aggr));
4191 -- Collect discriminant values and recurse
4193 Add_Discriminant_Values (New_Aggr, Assoc_List);
4194 Propagate_Discriminants (New_Aggr, Assoc_List);
4196 Build_Constrained_Itype
4197 (New_Aggr, T, Component_Associations (New_Aggr));
4200 (Comp, Empty, Component_Associations (Aggr),
4201 Is_Box_Present => True);
4203 end Process_Component;
4207 Aggr_Type : constant Entity_Id := Base_Type (Etype (Aggr));
4208 Components : constant Elist_Id := New_Elmt_List;
4209 Def_Node : constant Node_Id :=
4210 Type_Definition (Declaration_Node (Aggr_Type));
4213 Comp_Elmt : Elmt_Id;
4216 -- Start of processing for Propagate_Discriminants
4219 -- The component type may be a variant type. Collect the components
4220 -- that are ruled by the known values of the discriminants. Their
4221 -- values have already been inserted into the component list of the
4222 -- current aggregate.
4224 if Nkind (Def_Node) = N_Record_Definition
4225 and then Present (Component_List (Def_Node))
4226 and then Present (Variant_Part (Component_List (Def_Node)))
4228 Gather_Components (Aggr_Type,
4229 Component_List (Def_Node),
4230 Governed_By => Component_Associations (Aggr),
4232 Report_Errors => Errors);
4234 Comp_Elmt := First_Elmt (Components);
4235 while Present (Comp_Elmt) loop
4236 if Ekind (Node (Comp_Elmt)) /= E_Discriminant then
4237 Process_Component (Node (Comp_Elmt));
4240 Next_Elmt (Comp_Elmt);
4243 -- No variant part, iterate over all components
4246 Comp := First_Component (Etype (Aggr));
4247 while Present (Comp) loop
4248 Process_Component (Comp);
4249 Next_Component (Comp);
4252 end Propagate_Discriminants;
4254 -----------------------
4255 -- Resolve_Aggr_Expr --
4256 -----------------------
4258 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Entity_Id) is
4259 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean;
4260 -- If the expression is an aggregate (possibly qualified) then its
4261 -- expansion is delayed until the enclosing aggregate is expanded
4262 -- into assignments. In that case, do not generate checks on the
4263 -- expression, because they will be generated later, and will other-
4264 -- wise force a copy (to remove side effects) that would leave a
4265 -- dynamic-sized aggregate in the code, something that gigi cannot
4268 ---------------------------
4269 -- Has_Expansion_Delayed --
4270 ---------------------------
4272 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean is
4275 (Nkind (Expr) in N_Aggregate | N_Extension_Aggregate
4276 and then Present (Etype (Expr))
4277 and then Is_Record_Type (Etype (Expr))
4278 and then Expansion_Delayed (Expr))
4280 (Nkind (Expr) = N_Qualified_Expression
4281 and then Has_Expansion_Delayed (Expression (Expr)));
4282 end Has_Expansion_Delayed;
4286 Expr_Type : Entity_Id := Empty;
4287 New_C : Entity_Id := Component;
4291 -- Set to True if the resolved Expr node needs to be relocated when
4292 -- attached to the newly created association list. This node need not
4293 -- be relocated if its parent pointer is not set. In fact in this
4294 -- case Expr is the output of a New_Copy_Tree call. If Relocate is
4295 -- True then we have analyzed the expression node in the original
4296 -- aggregate and hence it needs to be relocated when moved over to
4297 -- the new association list.
4299 -- Start of processing for Resolve_Aggr_Expr
4302 -- If the type of the component is elementary or the type of the
4303 -- aggregate does not contain discriminants, use the type of the
4304 -- component to resolve Expr.
4306 if Is_Elementary_Type (Etype (Component))
4307 or else not Has_Discriminants (Etype (N))
4309 Expr_Type := Etype (Component);
4311 -- Otherwise we have to pick up the new type of the component from
4312 -- the new constrained subtype of the aggregate. In fact components
4313 -- which are of a composite type might be constrained by a
4314 -- discriminant, and we want to resolve Expr against the subtype were
4315 -- all discriminant occurrences are replaced with their actual value.
4318 New_C := First_Component (Etype (N));
4319 while Present (New_C) loop
4320 if Chars (New_C) = Chars (Component) then
4321 Expr_Type := Etype (New_C);
4325 Next_Component (New_C);
4328 pragma Assert (Present (Expr_Type));
4330 -- For each range in an array type where a discriminant has been
4331 -- replaced with the constraint, check that this range is within
4332 -- the range of the base type. This checks is done in the init
4333 -- proc for regular objects, but has to be done here for
4334 -- aggregates since no init proc is called for them.
4336 if Is_Array_Type (Expr_Type) then
4339 -- Range of the current constrained index in the array
4341 Orig_Index : Node_Id := First_Index (Etype (Component));
4342 -- Range corresponding to the range Index above in the
4343 -- original unconstrained record type. The bounds of this
4344 -- range may be governed by discriminants.
4346 Unconstr_Index : Node_Id := First_Index (Etype (Expr_Type));
4347 -- Range corresponding to the range Index above for the
4348 -- unconstrained array type. This range is needed to apply
4352 Index := First_Index (Expr_Type);
4353 while Present (Index) loop
4354 if Depends_On_Discriminant (Orig_Index) then
4355 Apply_Range_Check (Index, Etype (Unconstr_Index));
4359 Next_Index (Orig_Index);
4360 Next_Index (Unconstr_Index);
4366 -- If the Parent pointer of Expr is not set, Expr is an expression
4367 -- duplicated by New_Tree_Copy (this happens for record aggregates
4368 -- that look like (Field1 | Filed2 => Expr) or (others => Expr)).
4369 -- Such a duplicated expression must be attached to the tree
4370 -- before analysis and resolution to enforce the rule that a tree
4371 -- fragment should never be analyzed or resolved unless it is
4372 -- attached to the current compilation unit.
4374 if No (Parent (Expr)) then
4375 Set_Parent (Expr, N);
4381 Analyze_And_Resolve (Expr, Expr_Type);
4382 Check_Expr_OK_In_Limited_Aggregate (Expr);
4383 Check_Non_Static_Context (Expr);
4384 Check_Unset_Reference (Expr);
4386 -- Check wrong use of class-wide types
4388 if Is_Class_Wide_Type (Etype (Expr)) then
4389 Error_Msg_N ("dynamically tagged expression not allowed", Expr);
4392 if not Has_Expansion_Delayed (Expr) then
4393 Aggregate_Constraint_Checks (Expr, Expr_Type);
4396 -- If an aggregate component has a type with predicates, an explicit
4397 -- predicate check must be applied, as for an assignment statement,
4398 -- because the aggregate might not be expanded into individual
4399 -- component assignments.
4401 if Has_Predicates (Expr_Type)
4402 and then Analyzed (Expr)
4404 Apply_Predicate_Check (Expr, Expr_Type);
4407 if Raises_Constraint_Error (Expr) then
4408 Set_Raises_Constraint_Error (N);
4411 -- If the expression has been marked as requiring a range check, then
4412 -- generate it here. It's a bit odd to be generating such checks in
4413 -- the analyzer, but harmless since Generate_Range_Check does nothing
4414 -- (other than making sure Do_Range_Check is set) if the expander is
4417 if Do_Range_Check (Expr) then
4418 Generate_Range_Check (Expr, Expr_Type, CE_Range_Check_Failed);
4421 -- Add association Component => Expr if the caller requests it
4424 New_Expr := Relocate_Node (Expr);
4426 -- Since New_Expr is not gonna be analyzed later on, we need to
4427 -- propagate here the dimensions form Expr to New_Expr.
4429 Copy_Dimensions (Expr, New_Expr);
4435 Add_Association (New_C, New_Expr, New_Assoc_List);
4436 end Resolve_Aggr_Expr;
4442 procedure Rewrite_Range (Root_Type : Entity_Id; Rge : Node_Id) is
4443 procedure Rewrite_Bound
4446 Expr_Disc : Node_Id);
4447 -- Rewrite a bound of the range Bound, when it is equal to the
4448 -- non-stored discriminant Disc, into the stored discriminant
4455 procedure Rewrite_Bound
4458 Expr_Disc : Node_Id)
4461 if Nkind (Bound) /= N_Identifier then
4465 -- We expect either the discriminant or the discriminal
4467 if Entity (Bound) = Disc
4468 or else (Ekind (Entity (Bound)) = E_In_Parameter
4469 and then Discriminal_Link (Entity (Bound)) = Disc)
4471 Rewrite (Bound, New_Copy_Tree (Expr_Disc));
4477 Low, High : Node_Id;
4479 Expr_Disc : Elmt_Id;
4481 -- Start of processing for Rewrite_Range
4484 if Has_Discriminants (Root_Type) and then Nkind (Rge) = N_Range then
4485 Low := Low_Bound (Rge);
4486 High := High_Bound (Rge);
4488 Disc := First_Discriminant (Root_Type);
4489 Expr_Disc := First_Elmt (Stored_Constraint (Etype (N)));
4490 while Present (Disc) loop
4491 Rewrite_Bound (Low, Disc, Node (Expr_Disc));
4492 Rewrite_Bound (High, Disc, Node (Expr_Disc));
4493 Next_Discriminant (Disc);
4494 Next_Elmt (Expr_Disc);
4501 Components : constant Elist_Id := New_Elmt_List;
4502 -- Components is the list of the record components whose value must be
4503 -- provided in the aggregate. This list does include discriminants.
4505 Component : Entity_Id;
4506 Component_Elmt : Elmt_Id;
4508 Positional_Expr : Node_Id;
4510 -- Start of processing for Resolve_Record_Aggregate
4513 -- A record aggregate is restricted in SPARK:
4515 -- Each named association can have only a single choice.
4516 -- OTHERS cannot be used.
4517 -- Positional and named associations cannot be mixed.
4519 if Present (Component_Associations (N))
4520 and then Present (First (Component_Associations (N)))
4526 Assoc := First (Component_Associations (N));
4527 while Present (Assoc) loop
4528 if Nkind (Assoc) = N_Iterated_Component_Association then
4530 ("iterated component association can only appear in an "
4531 & "array aggregate", N);
4532 raise Unrecoverable_Error;
4540 -- We may end up calling Duplicate_Subexpr on expressions that are
4541 -- attached to New_Assoc_List. For this reason we need to attach it
4542 -- to the tree by setting its parent pointer to N. This parent point
4543 -- will change in STEP 8 below.
4545 Set_Parent (New_Assoc_List, N);
4547 -- STEP 1: abstract type and null record verification
4549 if Is_Abstract_Type (Typ) then
4550 Error_Msg_N ("type of aggregate cannot be abstract", N);
4553 if No (First_Entity (Typ)) and then Null_Record_Present (N) then
4557 elsif Present (First_Entity (Typ))
4558 and then Null_Record_Present (N)
4559 and then not Is_Tagged_Type (Typ)
4561 Error_Msg_N ("record aggregate cannot be null", N);
4564 -- If the type has no components, then the aggregate should either
4565 -- have "null record", or in Ada 2005 it could instead have a single
4566 -- component association given by "others => <>". For Ada 95 we flag an
4567 -- error at this point, but for Ada 2005 we proceed with checking the
4568 -- associations below, which will catch the case where it's not an
4569 -- aggregate with "others => <>". Note that the legality of a <>
4570 -- aggregate for a null record type was established by AI05-016.
4572 elsif No (First_Entity (Typ))
4573 and then Ada_Version < Ada_2005
4575 Error_Msg_N ("record aggregate must be null", N);
4579 -- STEP 2: Verify aggregate structure
4583 Bad_Aggregate : Boolean := False;
4584 Selector_Name : Node_Id;
4587 if Present (Component_Associations (N)) then
4588 Assoc := First (Component_Associations (N));
4593 while Present (Assoc) loop
4594 Selector_Name := First (Choices (Assoc));
4595 while Present (Selector_Name) loop
4596 if Nkind (Selector_Name) = N_Identifier then
4599 elsif Nkind (Selector_Name) = N_Others_Choice then
4600 if Selector_Name /= First (Choices (Assoc))
4601 or else Present (Next (Selector_Name))
4604 ("OTHERS must appear alone in a choice list",
4608 elsif Present (Next (Assoc)) then
4610 ("OTHERS must appear last in an aggregate",
4614 -- (Ada 2005): If this is an association with a box,
4615 -- indicate that the association need not represent
4618 elsif Box_Present (Assoc) then
4625 ("selector name should be identifier or OTHERS",
4627 Bad_Aggregate := True;
4630 Next (Selector_Name);
4636 if Bad_Aggregate then
4641 -- STEP 3: Find discriminant Values
4644 Discrim : Entity_Id;
4645 Missing_Discriminants : Boolean := False;
4648 if Present (Expressions (N)) then
4649 Positional_Expr := First (Expressions (N));
4651 Positional_Expr := Empty;
4654 -- AI05-0115: if the ancestor part is a subtype mark, the ancestor
4655 -- must not have unknown discriminants.
4656 -- ??? We are not checking any subtype mark here and this code is not
4657 -- exercised by any test, so it's likely wrong (in particular
4658 -- we should not use Root_Type here but the subtype mark, if any),
4659 -- and possibly not needed.
4661 if Is_Derived_Type (Typ)
4662 and then Has_Unknown_Discriminants (Root_Type (Typ))
4663 and then Nkind (N) /= N_Extension_Aggregate
4666 ("aggregate not available for type& whose ancestor "
4667 & "has unknown discriminants ", N, Typ);
4670 if Has_Unknown_Discriminants (Typ)
4671 and then Present (Underlying_Record_View (Typ))
4673 Discrim := First_Discriminant (Underlying_Record_View (Typ));
4674 elsif Has_Discriminants (Typ) then
4675 Discrim := First_Discriminant (Typ);
4680 -- First find the discriminant values in the positional components
4682 while Present (Discrim) and then Present (Positional_Expr) loop
4683 if Discriminant_Present (Discrim) then
4684 Resolve_Aggr_Expr (Positional_Expr, Discrim);
4686 -- Ada 2005 (AI-231)
4688 if Ada_Version >= Ada_2005
4689 and then Known_Null (Positional_Expr)
4691 Check_Can_Never_Be_Null (Discrim, Positional_Expr);
4694 Next (Positional_Expr);
4697 if Present (Get_Value (Discrim, Component_Associations (N))) then
4699 ("more than one value supplied for discriminant&",
4703 Next_Discriminant (Discrim);
4706 -- Find remaining discriminant values if any among named components
4708 while Present (Discrim) loop
4709 Expr := Get_Value (Discrim, Component_Associations (N), True);
4711 if not Discriminant_Present (Discrim) then
4712 if Present (Expr) then
4714 ("more than one value supplied for discriminant &",
4718 elsif No (Expr) then
4720 ("no value supplied for discriminant &", N, Discrim);
4721 Missing_Discriminants := True;
4724 Resolve_Aggr_Expr (Expr, Discrim);
4727 Next_Discriminant (Discrim);
4730 if Missing_Discriminants then
4734 -- At this point and until the beginning of STEP 6, New_Assoc_List
4735 -- contains only the discriminants and their values.
4739 -- STEP 4: Set the Etype of the record aggregate
4741 if Has_Discriminants (Typ)
4742 or else (Has_Unknown_Discriminants (Typ)
4743 and then Present (Underlying_Record_View (Typ)))
4745 Build_Constrained_Itype (N, Typ, New_Assoc_List);
4750 -- STEP 5: Get remaining components according to discriminant values
4754 Errors_Found : Boolean := False;
4755 Record_Def : Node_Id;
4756 Parent_Typ : Entity_Id;
4757 Parent_Typ_List : Elist_Id;
4758 Parent_Elmt : Elmt_Id;
4759 Root_Typ : Entity_Id;
4762 if Is_Derived_Type (Typ) and then Is_Tagged_Type (Typ) then
4763 Parent_Typ_List := New_Elmt_List;
4765 -- If this is an extension aggregate, the component list must
4766 -- include all components that are not in the given ancestor type.
4767 -- Otherwise, the component list must include components of all
4768 -- ancestors, starting with the root.
4770 if Nkind (N) = N_Extension_Aggregate then
4771 Root_Typ := Base_Type (Etype (Ancestor_Part (N)));
4774 -- AI05-0115: check legality of aggregate for type with a
4775 -- private ancestor.
4777 Root_Typ := Root_Type (Typ);
4778 if Has_Private_Ancestor (Typ) then
4780 Ancestor : constant Entity_Id :=
4781 Find_Private_Ancestor (Typ);
4782 Ancestor_Unit : constant Entity_Id :=
4784 (Get_Source_Unit (Ancestor));
4785 Parent_Unit : constant Entity_Id :=
4786 Cunit_Entity (Get_Source_Unit
4787 (Base_Type (Etype (Ancestor))));
4789 -- Check whether we are in a scope that has full view
4790 -- over the private ancestor and its parent. This can
4791 -- only happen if the derivation takes place in a child
4792 -- unit of the unit that declares the parent, and we are
4793 -- in the private part or body of that child unit, else
4794 -- the aggregate is illegal.
4796 if Is_Child_Unit (Ancestor_Unit)
4797 and then Scope (Ancestor_Unit) = Parent_Unit
4798 and then In_Open_Scopes (Scope (Ancestor))
4800 (In_Private_Part (Scope (Ancestor))
4801 or else In_Package_Body (Scope (Ancestor)))
4807 ("type of aggregate has private ancestor&!",
4809 Error_Msg_N ("must use extension aggregate!", N);
4815 Dnode := Declaration_Node (Base_Type (Root_Typ));
4817 -- If we don't get a full declaration, then we have some error
4818 -- which will get signalled later so skip this part. Otherwise
4819 -- gather components of root that apply to the aggregate type.
4820 -- We use the base type in case there is an applicable stored
4821 -- constraint that renames the discriminants of the root.
4823 if Nkind (Dnode) = N_Full_Type_Declaration then
4824 Record_Def := Type_Definition (Dnode);
4827 Component_List (Record_Def),
4828 Governed_By => New_Assoc_List,
4830 Report_Errors => Errors_Found);
4832 if Errors_Found then
4834 ("discriminant controlling variant part is not static",
4841 Parent_Typ := Base_Type (Typ);
4842 while Parent_Typ /= Root_Typ loop
4843 Prepend_Elmt (Parent_Typ, To => Parent_Typ_List);
4844 Parent_Typ := Etype (Parent_Typ);
4846 if Nkind (Parent (Base_Type (Parent_Typ))) =
4847 N_Private_Type_Declaration
4848 or else Nkind (Parent (Base_Type (Parent_Typ))) =
4849 N_Private_Extension_Declaration
4851 if Nkind (N) /= N_Extension_Aggregate then
4853 ("type of aggregate has private ancestor&!",
4855 Error_Msg_N ("must use extension aggregate!", N);
4858 elsif Parent_Typ /= Root_Typ then
4860 ("ancestor part of aggregate must be private type&",
4861 Ancestor_Part (N), Parent_Typ);
4865 -- The current view of ancestor part may be a private type,
4866 -- while the context type is always non-private.
4868 elsif Is_Private_Type (Root_Typ)
4869 and then Present (Full_View (Root_Typ))
4870 and then Nkind (N) = N_Extension_Aggregate
4872 exit when Base_Type (Full_View (Root_Typ)) = Parent_Typ;
4876 -- Now collect components from all other ancestors, beginning
4877 -- with the current type. If the type has unknown discriminants
4878 -- use the component list of the Underlying_Record_View, which
4879 -- needs to be used for the subsequent expansion of the aggregate
4880 -- into assignments.
4882 Parent_Elmt := First_Elmt (Parent_Typ_List);
4883 while Present (Parent_Elmt) loop
4884 Parent_Typ := Node (Parent_Elmt);
4886 if Has_Unknown_Discriminants (Parent_Typ)
4887 and then Present (Underlying_Record_View (Typ))
4889 Parent_Typ := Underlying_Record_View (Parent_Typ);
4892 Record_Def := Type_Definition (Parent (Base_Type (Parent_Typ)));
4893 Gather_Components (Empty,
4894 Component_List (Record_Extension_Part (Record_Def)),
4895 Governed_By => New_Assoc_List,
4897 Report_Errors => Errors_Found);
4899 Next_Elmt (Parent_Elmt);
4902 -- Typ is not a derived tagged type
4905 Record_Def := Type_Definition (Parent (Base_Type (Typ)));
4907 if Null_Present (Record_Def) then
4910 elsif not Has_Unknown_Discriminants (Typ) then
4913 Component_List (Record_Def),
4914 Governed_By => New_Assoc_List,
4916 Report_Errors => Errors_Found);
4920 (Base_Type (Underlying_Record_View (Typ)),
4921 Component_List (Record_Def),
4922 Governed_By => New_Assoc_List,
4924 Report_Errors => Errors_Found);
4928 if Errors_Found then
4933 -- STEP 6: Find component Values
4936 Component_Elmt := First_Elmt (Components);
4938 -- First scan the remaining positional associations in the aggregate.
4939 -- Remember that at this point Positional_Expr contains the current
4940 -- positional association if any is left after looking for discriminant
4941 -- values in step 3.
4943 while Present (Positional_Expr) and then Present (Component_Elmt) loop
4944 Component := Node (Component_Elmt);
4945 Resolve_Aggr_Expr (Positional_Expr, Component);
4947 -- Ada 2005 (AI-231)
4949 if Ada_Version >= Ada_2005 and then Known_Null (Positional_Expr) then
4950 Check_Can_Never_Be_Null (Component, Positional_Expr);
4953 if Present (Get_Value (Component, Component_Associations (N))) then
4955 ("more than one value supplied for Component &", N, Component);
4958 Next (Positional_Expr);
4959 Next_Elmt (Component_Elmt);
4962 if Present (Positional_Expr) then
4964 ("too many components for record aggregate", Positional_Expr);
4967 -- Now scan for the named arguments of the aggregate
4969 while Present (Component_Elmt) loop
4970 Component := Node (Component_Elmt);
4971 Expr := Get_Value (Component, Component_Associations (N), True);
4973 -- Note: The previous call to Get_Value sets the value of the
4974 -- variable Is_Box_Present.
4976 -- Ada 2005 (AI-287): Handle components with default initialization.
4977 -- Note: This feature was originally added to Ada 2005 for limited
4978 -- but it was finally allowed with any type.
4980 if Is_Box_Present then
4981 Check_Box_Component : declare
4982 Ctyp : constant Entity_Id := Etype (Component);
4985 -- If there is a default expression for the aggregate, copy
4986 -- it into a new association. This copy must modify the scopes
4987 -- of internal types that may be attached to the expression
4988 -- (e.g. index subtypes of arrays) because in general the type
4989 -- declaration and the aggregate appear in different scopes,
4990 -- and the backend requires the scope of the type to match the
4991 -- point at which it is elaborated.
4993 -- If the component has an initialization procedure (IP) we
4994 -- pass the component to the expander, which will generate
4995 -- the call to such IP.
4997 -- If the component has discriminants, their values must
4998 -- be taken from their subtype. This is indispensable for
4999 -- constraints that are given by the current instance of an
5000 -- enclosing type, to allow the expansion of the aggregate to
5001 -- replace the reference to the current instance by the target
5002 -- object of the aggregate.
5004 if Present (Parent (Component))
5005 and then Nkind (Parent (Component)) = N_Component_Declaration
5006 and then Present (Expression (Parent (Component)))
5009 New_Copy_Tree_And_Copy_Dimensions
5010 (Expression (Parent (Component)),
5011 New_Scope => Current_Scope,
5012 New_Sloc => Sloc (N));
5014 -- As the type of the copied default expression may refer
5015 -- to discriminants of the record type declaration, these
5016 -- non-stored discriminants need to be rewritten into stored
5017 -- discriminant values for the aggregate. This is required
5018 -- in GNATprove mode, and is adopted in all modes to avoid
5019 -- special-casing GNATprove mode.
5021 if Is_Array_Type (Etype (Expr)) then
5023 Rec_Typ : constant Entity_Id := Scope (Component);
5024 -- Root record type whose discriminants may be used as
5025 -- bounds in range nodes.
5032 -- Rewrite the range nodes occurring in the indexes
5035 Index := First_Index (Etype (Expr));
5036 while Present (Index) loop
5037 Rewrite_Range (Rec_Typ, Index);
5039 (Rec_Typ, Scalar_Range (Etype (Index)));
5044 -- Rewrite the range nodes occurring as aggregate
5045 -- bounds and component associations.
5047 if Nkind (Expr) = N_Aggregate then
5048 if Present (Aggregate_Bounds (Expr)) then
5049 Rewrite_Range (Rec_Typ, Aggregate_Bounds (Expr));
5052 if Present (Component_Associations (Expr)) then
5053 Assoc := First (Component_Associations (Expr));
5054 while Present (Assoc) loop
5055 Choice := First (Choices (Assoc));
5056 while Present (Choice) loop
5057 Rewrite_Range (Rec_Typ, Choice);
5070 (Component => Component,
5072 Assoc_List => New_Assoc_List);
5073 Set_Has_Self_Reference (N);
5075 -- A box-defaulted access component gets the value null. Also
5076 -- included are components of private types whose underlying
5077 -- type is an access type. In either case set the type of the
5078 -- literal, for subsequent use in semantic checks.
5080 elsif Present (Underlying_Type (Ctyp))
5081 and then Is_Access_Type (Underlying_Type (Ctyp))
5083 -- If the component's type is private with an access type as
5084 -- its underlying type then we have to create an unchecked
5085 -- conversion to satisfy type checking.
5087 if Is_Private_Type (Ctyp) then
5089 Qual_Null : constant Node_Id :=
5090 Make_Qualified_Expression (Sloc (N),
5093 (Underlying_Type (Ctyp), Sloc (N)),
5094 Expression => Make_Null (Sloc (N)));
5096 Convert_Null : constant Node_Id :=
5097 Unchecked_Convert_To
5101 Analyze_And_Resolve (Convert_Null, Ctyp);
5103 (Component => Component,
5104 Expr => Convert_Null,
5105 Assoc_List => New_Assoc_List);
5108 -- Otherwise the component type is non-private
5111 Expr := Make_Null (Sloc (N));
5112 Set_Etype (Expr, Ctyp);
5115 (Component => Component,
5117 Assoc_List => New_Assoc_List);
5120 -- Ada 2012: If component is scalar with default value, use it
5121 -- by converting it to Ctyp, so that subtype constraints are
5124 elsif Is_Scalar_Type (Ctyp)
5125 and then Has_Default_Aspect (Ctyp)
5128 Conv : constant Node_Id :=
5133 (Default_Aspect_Value
5134 (First_Subtype (Underlying_Type (Ctyp)))));
5137 Analyze_And_Resolve (Conv, Ctyp);
5139 (Component => Component,
5141 Assoc_List => New_Assoc_List);
5144 elsif Has_Non_Null_Base_Init_Proc (Ctyp)
5145 or else not Expander_Active
5147 if Is_Record_Type (Ctyp)
5148 and then Has_Discriminants (Ctyp)
5149 and then not Is_Private_Type (Ctyp)
5151 -- We build a partially initialized aggregate with the
5152 -- values of the discriminants and box initialization
5153 -- for the rest, if other components are present.
5155 -- The type of the aggregate is the known subtype of
5156 -- the component. The capture of discriminants must be
5157 -- recursive because subcomponents may be constrained
5158 -- (transitively) by discriminants of enclosing types.
5159 -- For a private type with discriminants, a call to the
5160 -- initialization procedure will be generated, and no
5161 -- subaggregate is needed.
5163 Capture_Discriminants : declare
5164 Loc : constant Source_Ptr := Sloc (N);
5168 Expr := Make_Aggregate (Loc, No_List, New_List);
5169 Set_Etype (Expr, Ctyp);
5171 -- If the enclosing type has discriminants, they have
5172 -- been collected in the aggregate earlier, and they
5173 -- may appear as constraints of subcomponents.
5175 -- Similarly if this component has discriminants, they
5176 -- might in turn be propagated to their components.
5178 if Has_Discriminants (Typ) then
5179 Add_Discriminant_Values (Expr, New_Assoc_List);
5180 Propagate_Discriminants (Expr, New_Assoc_List);
5182 elsif Has_Discriminants (Ctyp) then
5183 Add_Discriminant_Values
5184 (Expr, Component_Associations (Expr));
5185 Propagate_Discriminants
5186 (Expr, Component_Associations (Expr));
5188 Build_Constrained_Itype
5189 (Expr, Ctyp, Component_Associations (Expr));
5196 -- If the type has additional components, create
5197 -- an OTHERS box association for them.
5199 Comp := First_Component (Ctyp);
5200 while Present (Comp) loop
5201 if Ekind (Comp) = E_Component then
5202 if not Is_Record_Type (Etype (Comp)) then
5204 (Component_Associations (Expr),
5205 Make_Component_Association (Loc,
5208 Make_Others_Choice (Loc)),
5209 Expression => Empty,
5210 Box_Present => True));
5216 Next_Component (Comp);
5222 (Component => Component,
5224 Assoc_List => New_Assoc_List);
5225 end Capture_Discriminants;
5227 -- Otherwise the component type is not a record, or it has
5228 -- not discriminants, or it is private.
5232 (Component => Component,
5234 Assoc_List => New_Assoc_List,
5235 Is_Box_Present => True);
5238 -- Otherwise we only need to resolve the expression if the
5239 -- component has partially initialized values (required to
5240 -- expand the corresponding assignments and run-time checks).
5242 elsif Present (Expr)
5243 and then Is_Partially_Initialized_Type (Ctyp)
5245 Resolve_Aggr_Expr (Expr, Component);
5247 end Check_Box_Component;
5249 elsif No (Expr) then
5251 -- Ignore hidden components associated with the position of the
5252 -- interface tags: these are initialized dynamically.
5254 if not Present (Related_Type (Component)) then
5256 ("no value supplied for component &!", N, Component);
5260 Resolve_Aggr_Expr (Expr, Component);
5263 Next_Elmt (Component_Elmt);
5266 -- STEP 7: check for invalid components + check type in choice list
5270 New_Assoc : Node_Id;
5276 -- Type of first component in choice list
5279 if Present (Component_Associations (N)) then
5280 Assoc := First (Component_Associations (N));
5285 Verification : while Present (Assoc) loop
5286 Selectr := First (Choices (Assoc));
5289 if Nkind (Selectr) = N_Others_Choice then
5291 -- Ada 2005 (AI-287): others choice may have expression or box
5293 if No (Others_Etype) and then Others_Box = 0 then
5295 ("OTHERS must represent at least one component", Selectr);
5297 elsif Others_Box = 1 and then Warn_On_Redundant_Constructs then
5298 Error_Msg_N ("others choice is redundant?", Box_Node);
5300 ("\previous choices cover all components?", Box_Node);
5306 while Present (Selectr) loop
5307 New_Assoc := First (New_Assoc_List);
5308 while Present (New_Assoc) loop
5309 Component := First (Choices (New_Assoc));
5311 if Chars (Selectr) = Chars (Component) then
5313 Check_Identifier (Selectr, Entity (Component));
5322 -- If no association, this is not a legal component of the type
5323 -- in question, unless its association is provided with a box.
5325 if No (New_Assoc) then
5326 if Box_Present (Parent (Selectr)) then
5328 -- This may still be a bogus component with a box. Scan
5329 -- list of components to verify that a component with
5330 -- that name exists.
5336 C := First_Component (Typ);
5337 while Present (C) loop
5338 if Chars (C) = Chars (Selectr) then
5340 -- If the context is an extension aggregate,
5341 -- the component must not be inherited from
5342 -- the ancestor part of the aggregate.
5344 if Nkind (N) /= N_Extension_Aggregate
5346 Scope (Original_Record_Component (C)) /=
5347 Etype (Ancestor_Part (N))
5357 Error_Msg_Node_2 := Typ;
5358 Error_Msg_N ("& is not a component of}", Selectr);
5362 elsif Chars (Selectr) /= Name_uTag
5363 and then Chars (Selectr) /= Name_uParent
5365 if not Has_Discriminants (Typ) then
5366 Error_Msg_Node_2 := Typ;
5367 Error_Msg_N ("& is not a component of}", Selectr);
5370 ("& is not a component of the aggregate subtype",
5374 Check_Misspelled_Component (Components, Selectr);
5377 elsif No (Typech) then
5378 Typech := Base_Type (Etype (Component));
5380 -- AI05-0199: In Ada 2012, several components of anonymous
5381 -- access types can appear in a choice list, as long as the
5382 -- designated types match.
5384 elsif Typech /= Base_Type (Etype (Component)) then
5385 if Ada_Version >= Ada_2012
5386 and then Ekind (Typech) = E_Anonymous_Access_Type
5388 Ekind (Etype (Component)) = E_Anonymous_Access_Type
5389 and then Base_Type (Designated_Type (Typech)) =
5390 Base_Type (Designated_Type (Etype (Component)))
5392 Subtypes_Statically_Match (Typech, (Etype (Component)))
5396 elsif not Box_Present (Parent (Selectr)) then
5398 ("components in choice list must have same type",
5407 end loop Verification;
5410 -- STEP 8: replace the original aggregate
5413 New_Aggregate : constant Node_Id := New_Copy (N);
5416 Set_Expressions (New_Aggregate, No_List);
5417 Set_Etype (New_Aggregate, Etype (N));
5418 Set_Component_Associations (New_Aggregate, New_Assoc_List);
5419 Set_Check_Actuals (New_Aggregate, Check_Actuals (N));
5421 Rewrite (N, New_Aggregate);
5424 -- Check the dimensions of the components in the record aggregate
5426 Analyze_Dimension_Extension_Or_Record_Aggregate (N);
5427 end Resolve_Record_Aggregate;
5429 -----------------------------
5430 -- Check_Can_Never_Be_Null --
5431 -----------------------------
5433 procedure Check_Can_Never_Be_Null (Typ : Entity_Id; Expr : Node_Id) is
5434 Comp_Typ : Entity_Id;
5438 (Ada_Version >= Ada_2005
5439 and then Present (Expr)
5440 and then Known_Null (Expr));
5443 when E_Array_Type =>
5444 Comp_Typ := Component_Type (Typ);
5449 Comp_Typ := Etype (Typ);
5455 if Can_Never_Be_Null (Comp_Typ) then
5457 -- Here we know we have a constraint error. Note that we do not use
5458 -- Apply_Compile_Time_Constraint_Error here to the Expr, which might
5459 -- seem the more natural approach. That's because in some cases the
5460 -- components are rewritten, and the replacement would be missed.
5461 -- We do not mark the whole aggregate as raising a constraint error,
5462 -- because the association may be a null array range.
5465 ("(Ada 2005) null not allowed in null-excluding component??", Expr);
5467 ("\Constraint_Error will be raised at run time??", Expr);
5470 Make_Raise_Constraint_Error
5471 (Sloc (Expr), Reason => CE_Access_Check_Failed));
5472 Set_Etype (Expr, Comp_Typ);
5473 Set_Analyzed (Expr);
5475 end Check_Can_Never_Be_Null;
5477 ---------------------
5478 -- Sort_Case_Table --
5479 ---------------------
5481 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is
5482 U : constant Int := Case_Table'Last;
5490 T := Case_Table (K + 1);
5494 and then Expr_Value (Case_Table (J - 1).Lo) > Expr_Value (T.Lo)
5496 Case_Table (J) := Case_Table (J - 1);
5500 Case_Table (J) := T;
5503 end Sort_Case_Table;