1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2020, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Einfo; use Einfo;
30 with Elists; use Elists;
31 with Errout; use Errout;
32 with Expander; use Expander;
33 with Exp_Ch6; use Exp_Ch6;
34 with Exp_Tss; use Exp_Tss;
35 with Exp_Util; use Exp_Util;
36 with Freeze; use Freeze;
37 with Itypes; use Itypes;
39 with Lib.Xref; use Lib.Xref;
40 with Namet; use Namet;
41 with Namet.Sp; use Namet.Sp;
42 with Nmake; use Nmake;
43 with Nlists; use Nlists;
45 with Restrict; use Restrict;
46 with Rident; use Rident;
48 with Sem_Aux; use Sem_Aux;
49 with Sem_Cat; use Sem_Cat;
50 with Sem_Ch3; use Sem_Ch3;
51 with Sem_Ch8; use Sem_Ch8;
52 with Sem_Ch13; use Sem_Ch13;
53 with Sem_Dim; use Sem_Dim;
54 with Sem_Eval; use Sem_Eval;
55 with Sem_Res; use Sem_Res;
56 with Sem_Util; use Sem_Util;
57 with Sem_Type; use Sem_Type;
58 with Sem_Warn; use Sem_Warn;
59 with Sinfo; use Sinfo;
60 with Snames; use Snames;
61 with Stringt; use Stringt;
62 with Stand; use Stand;
63 with Style; use Style;
64 with Targparm; use Targparm;
65 with Tbuild; use Tbuild;
66 with Ttypes; use Ttypes;
67 with Uintp; use Uintp;
69 package body Sem_Aggr is
71 type Case_Bounds is record
73 -- Low bound of choice. Once we sort the Case_Table, then entries
74 -- will be in order of ascending Choice_Lo values.
77 -- High Bound of choice. The sort does not pay any attention to the
78 -- high bound, so choices 1 .. 4 and 1 .. 5 could be in either order.
81 -- If there are duplicates or missing entries, then in the sorted
82 -- table, this records the highest value among Choice_Hi values
83 -- seen so far, including this entry.
86 -- The node of the choice
89 type Case_Table_Type is array (Pos range <>) of Case_Bounds;
90 -- Table type used by Check_Case_Choices procedure
92 -----------------------
93 -- Local Subprograms --
94 -----------------------
96 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type);
97 -- Sort the Case Table using the Lower Bound of each Choice as the key. A
98 -- simple insertion sort is used since the choices in a case statement will
99 -- usually be in near sorted order.
101 procedure Check_Can_Never_Be_Null (Typ : Entity_Id; Expr : Node_Id);
102 -- Ada 2005 (AI-231): Check bad usage of null for a component for which
103 -- null exclusion (NOT NULL) is specified. Typ can be an E_Array_Type for
104 -- the array case (the component type of the array will be used) or an
105 -- E_Component/E_Discriminant entity in the record case, in which case the
106 -- type of the component will be used for the test. If Typ is any other
107 -- kind of entity, the call is ignored. Expr is the component node in the
108 -- aggregate which is known to have a null value. A warning message will be
109 -- issued if the component is null excluding.
111 -- It would be better to pass the proper type for Typ ???
113 procedure Check_Expr_OK_In_Limited_Aggregate (Expr : Node_Id);
114 -- Check that Expr is either not limited or else is one of the cases of
115 -- expressions allowed for a limited component association (namely, an
116 -- aggregate, function call, or <> notation). Report error for violations.
117 -- Expression is also OK in an instance or inlining context, because we
118 -- have already preanalyzed and it is known to be type correct.
120 ------------------------------------------------------
121 -- Subprograms used for RECORD AGGREGATE Processing --
122 ------------------------------------------------------
124 procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id);
125 -- This procedure performs all the semantic checks required for record
126 -- aggregates. Note that for aggregates analysis and resolution go
127 -- hand in hand. Aggregate analysis has been delayed up to here and
128 -- it is done while resolving the aggregate.
130 -- N is the N_Aggregate node.
131 -- Typ is the record type for the aggregate resolution
133 -- While performing the semantic checks, this procedure builds a new
134 -- Component_Association_List where each record field appears alone in a
135 -- Component_Choice_List along with its corresponding expression. The
136 -- record fields in the Component_Association_List appear in the same order
137 -- in which they appear in the record type Typ.
139 -- Once this new Component_Association_List is built and all the semantic
140 -- checks performed, the original aggregate subtree is replaced with the
141 -- new named record aggregate just built. This new record aggregate has no
142 -- positional associations, so its Expressions field is set to No_List.
143 -- Note that subtree substitution is performed with Rewrite so as to be
144 -- able to retrieve the original aggregate.
146 -- The aggregate subtree manipulation performed by Resolve_Record_Aggregate
147 -- yields the aggregate format expected by Gigi. Typically, this kind of
148 -- tree manipulations are done in the expander. However, because the
149 -- semantic checks that need to be performed on record aggregates really go
150 -- hand in hand with the record aggregate normalization, the aggregate
151 -- subtree transformation is performed during resolution rather than
152 -- expansion. Had we decided otherwise we would have had to duplicate most
153 -- of the code in the expansion procedure Expand_Record_Aggregate. Note,
154 -- however, that all the expansion concerning aggregates for tagged records
155 -- is done in Expand_Record_Aggregate.
157 -- The algorithm of Resolve_Record_Aggregate proceeds as follows:
159 -- 1. Make sure that the record type against which the record aggregate
160 -- has to be resolved is not abstract. Furthermore if the type is a
161 -- null aggregate make sure the input aggregate N is also null.
163 -- 2. Verify that the structure of the aggregate is that of a record
164 -- aggregate. Specifically, look for component associations and ensure
165 -- that each choice list only has identifiers or the N_Others_Choice
166 -- node. Also make sure that if present, the N_Others_Choice occurs
167 -- last and by itself.
169 -- 3. If Typ contains discriminants, the values for each discriminant is
170 -- looked for. If the record type Typ has variants, we check that the
171 -- expressions corresponding to each discriminant ruling the (possibly
172 -- nested) variant parts of Typ, are static. This allows us to determine
173 -- the variant parts to which the rest of the aggregate must conform.
174 -- The names of discriminants with their values are saved in a new
175 -- association list, New_Assoc_List which is later augmented with the
176 -- names and values of the remaining components in the record type.
178 -- During this phase we also make sure that every discriminant is
179 -- assigned exactly one value. Note that when several values for a given
180 -- discriminant are found, semantic processing continues looking for
181 -- further errors. In this case it's the first discriminant value found
182 -- which we will be recorded.
184 -- IMPORTANT NOTE: For derived tagged types this procedure expects
185 -- First_Discriminant and Next_Discriminant to give the correct list
186 -- of discriminants, in the correct order.
188 -- 4. After all the discriminant values have been gathered, we can set the
189 -- Etype of the record aggregate. If Typ contains no discriminants this
190 -- is straightforward: the Etype of N is just Typ, otherwise a new
191 -- implicit constrained subtype of Typ is built to be the Etype of N.
193 -- 5. Gather the remaining record components according to the discriminant
194 -- values. This involves recursively traversing the record type
195 -- structure to see what variants are selected by the given discriminant
196 -- values. This processing is a little more convoluted if Typ is a
197 -- derived tagged types since we need to retrieve the record structure
198 -- of all the ancestors of Typ.
200 -- 6. After gathering the record components we look for their values in the
201 -- record aggregate and emit appropriate error messages should we not
202 -- find such values or should they be duplicated.
204 -- 7. We then make sure no illegal component names appear in the record
205 -- aggregate and make sure that the type of the record components
206 -- appearing in a same choice list is the same. Finally we ensure that
207 -- the others choice, if present, is used to provide the value of at
208 -- least a record component.
210 -- 8. The original aggregate node is replaced with the new named aggregate
211 -- built in steps 3 through 6, as explained earlier.
213 -- Given the complexity of record aggregate resolution, the primary goal of
214 -- this routine is clarity and simplicity rather than execution and storage
215 -- efficiency. If there are only positional components in the aggregate the
216 -- running time is linear. If there are associations the running time is
217 -- still linear as long as the order of the associations is not too far off
218 -- the order of the components in the record type. If this is not the case
219 -- the running time is at worst quadratic in the size of the association
222 procedure Check_Misspelled_Component
223 (Elements : Elist_Id;
224 Component : Node_Id);
225 -- Give possible misspelling diagnostic if Component is likely to be a
226 -- misspelling of one of the components of the Assoc_List. This is called
227 -- by Resolve_Aggr_Expr after producing an invalid component error message.
229 -----------------------------------------------------
230 -- Subprograms used for ARRAY AGGREGATE Processing --
231 -----------------------------------------------------
233 function Resolve_Array_Aggregate
236 Index_Constr : Node_Id;
237 Component_Typ : Entity_Id;
238 Others_Allowed : Boolean) return Boolean;
239 -- This procedure performs the semantic checks for an array aggregate.
240 -- True is returned if the aggregate resolution succeeds.
242 -- The procedure works by recursively checking each nested aggregate.
243 -- Specifically, after checking a sub-aggregate nested at the i-th level
244 -- we recursively check all the subaggregates at the i+1-st level (if any).
245 -- Note that for aggregates analysis and resolution go hand in hand.
246 -- Aggregate analysis has been delayed up to here and it is done while
247 -- resolving the aggregate.
249 -- N is the current N_Aggregate node to be checked.
251 -- Index is the index node corresponding to the array sub-aggregate that
252 -- we are currently checking (RM 4.3.3 (8)). Its Etype is the
253 -- corresponding index type (or subtype).
255 -- Index_Constr is the node giving the applicable index constraint if
256 -- any (RM 4.3.3 (10)). It "is a constraint provided by certain
257 -- contexts [...] that can be used to determine the bounds of the array
258 -- value specified by the aggregate". If Others_Allowed below is False
259 -- there is no applicable index constraint and this node is set to Index.
261 -- Component_Typ is the array component type.
263 -- Others_Allowed indicates whether an others choice is allowed
264 -- in the context where the top-level aggregate appeared.
266 -- The algorithm of Resolve_Array_Aggregate proceeds as follows:
268 -- 1. Make sure that the others choice, if present, is by itself and
269 -- appears last in the sub-aggregate. Check that we do not have
270 -- positional and named components in the array sub-aggregate (unless
271 -- the named association is an others choice). Finally if an others
272 -- choice is present, make sure it is allowed in the aggregate context.
274 -- 2. If the array sub-aggregate contains discrete_choices:
276 -- (A) Verify their validity. Specifically verify that:
278 -- (a) If a null range is present it must be the only possible
279 -- choice in the array aggregate.
281 -- (b) Ditto for a non static range.
283 -- (c) Ditto for a non static expression.
285 -- In addition this step analyzes and resolves each discrete_choice,
286 -- making sure that its type is the type of the corresponding Index.
287 -- If we are not at the lowest array aggregate level (in the case of
288 -- multi-dimensional aggregates) then invoke Resolve_Array_Aggregate
289 -- recursively on each component expression. Otherwise, resolve the
290 -- bottom level component expressions against the expected component
291 -- type ONLY IF the component corresponds to a single discrete choice
292 -- which is not an others choice (to see why read the DELAYED
293 -- COMPONENT RESOLUTION below).
295 -- (B) Determine the bounds of the sub-aggregate and lowest and
296 -- highest choice values.
298 -- 3. For positional aggregates:
300 -- (A) Loop over the component expressions either recursively invoking
301 -- Resolve_Array_Aggregate on each of these for multi-dimensional
302 -- array aggregates or resolving the bottom level component
303 -- expressions against the expected component type.
305 -- (B) Determine the bounds of the positional sub-aggregates.
307 -- 4. Try to determine statically whether the evaluation of the array
308 -- sub-aggregate raises Constraint_Error. If yes emit proper
309 -- warnings. The precise checks are the following:
311 -- (A) Check that the index range defined by aggregate bounds is
312 -- compatible with corresponding index subtype.
313 -- We also check against the base type. In fact it could be that
314 -- Low/High bounds of the base type are static whereas those of
315 -- the index subtype are not. Thus if we can statically catch
316 -- a problem with respect to the base type we are guaranteed
317 -- that the same problem will arise with the index subtype
319 -- (B) If we are dealing with a named aggregate containing an others
320 -- choice and at least one discrete choice then make sure the range
321 -- specified by the discrete choices does not overflow the
322 -- aggregate bounds. We also check against the index type and base
323 -- type bounds for the same reasons given in (A).
325 -- (C) If we are dealing with a positional aggregate with an others
326 -- choice make sure the number of positional elements specified
327 -- does not overflow the aggregate bounds. We also check against
328 -- the index type and base type bounds as mentioned in (A).
330 -- Finally construct an N_Range node giving the sub-aggregate bounds.
331 -- Set the Aggregate_Bounds field of the sub-aggregate to be this
332 -- N_Range. The routine Array_Aggr_Subtype below uses such N_Ranges
333 -- to build the appropriate aggregate subtype. Aggregate_Bounds
334 -- information is needed during expansion.
336 -- DELAYED COMPONENT RESOLUTION: The resolution of bottom level component
337 -- expressions in an array aggregate may call Duplicate_Subexpr or some
338 -- other routine that inserts code just outside the outermost aggregate.
339 -- If the array aggregate contains discrete choices or an others choice,
340 -- this may be wrong. Consider for instance the following example.
342 -- type Rec is record
346 -- type Acc_Rec is access Rec;
347 -- Arr : array (1..3) of Acc_Rec := (1 .. 3 => new Rec);
349 -- Then the transformation of "new Rec" that occurs during resolution
350 -- entails the following code modifications
352 -- P7b : constant Acc_Rec := new Rec;
354 -- Arr : array (1..3) of Acc_Rec := (1 .. 3 => P7b);
356 -- This code transformation is clearly wrong, since we need to call
357 -- "new Rec" for each of the 3 array elements. To avoid this problem we
358 -- delay resolution of the components of non positional array aggregates
359 -- to the expansion phase. As an optimization, if the discrete choice
360 -- specifies a single value we do not delay resolution.
362 function Array_Aggr_Subtype (N : Node_Id; Typ : Node_Id) return Entity_Id;
363 -- This routine returns the type or subtype of an array aggregate.
365 -- N is the array aggregate node whose type we return.
367 -- Typ is the context type in which N occurs.
369 -- This routine creates an implicit array subtype whose bounds are
370 -- those defined by the aggregate. When this routine is invoked
371 -- Resolve_Array_Aggregate has already processed aggregate N. Thus the
372 -- Aggregate_Bounds of each sub-aggregate, is an N_Range node giving the
373 -- sub-aggregate bounds. When building the aggregate itype, this function
374 -- traverses the array aggregate N collecting such Aggregate_Bounds and
375 -- constructs the proper array aggregate itype.
377 -- Note that in the case of multidimensional aggregates each inner
378 -- sub-aggregate corresponding to a given array dimension, may provide a
379 -- different bounds. If it is possible to determine statically that
380 -- some sub-aggregates corresponding to the same index do not have the
381 -- same bounds, then a warning is emitted. If such check is not possible
382 -- statically (because some sub-aggregate bounds are dynamic expressions)
383 -- then this job is left to the expander. In all cases the particular
384 -- bounds that this function will chose for a given dimension is the first
385 -- N_Range node for a sub-aggregate corresponding to that dimension.
387 -- Note that the Raises_Constraint_Error flag of an array aggregate
388 -- whose evaluation is determined to raise CE by Resolve_Array_Aggregate,
389 -- is set in Resolve_Array_Aggregate but the aggregate is not
390 -- immediately replaced with a raise CE. In fact, Array_Aggr_Subtype must
391 -- first construct the proper itype for the aggregate (Gigi needs
392 -- this). After constructing the proper itype we will eventually replace
393 -- the top-level aggregate with a raise CE (done in Resolve_Aggregate).
394 -- Of course in cases such as:
396 -- type Arr is array (integer range <>) of Integer;
397 -- A : Arr := (positive range -1 .. 2 => 0);
399 -- The bounds of the aggregate itype are cooked up to look reasonable
400 -- (in this particular case the bounds will be 1 .. 2).
402 procedure Make_String_Into_Aggregate (N : Node_Id);
403 -- A string literal can appear in a context in which a one dimensional
404 -- array of characters is expected. This procedure simply rewrites the
405 -- string as an aggregate, prior to resolution.
407 ---------------------------------
408 -- Delta aggregate processing --
409 ---------------------------------
411 procedure Resolve_Delta_Array_Aggregate (N : Node_Id; Typ : Entity_Id);
412 procedure Resolve_Delta_Record_Aggregate (N : Node_Id; Typ : Entity_Id);
414 ------------------------
415 -- Array_Aggr_Subtype --
416 ------------------------
418 function Array_Aggr_Subtype
420 Typ : Entity_Id) return Entity_Id
422 Aggr_Dimension : constant Pos := Number_Dimensions (Typ);
423 -- Number of aggregate index dimensions
425 Aggr_Range : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
426 -- Constrained N_Range of each index dimension in our aggregate itype
428 Aggr_Low : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
429 Aggr_High : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
430 -- Low and High bounds for each index dimension in our aggregate itype
432 Is_Fully_Positional : Boolean := True;
434 procedure Collect_Aggr_Bounds (N : Node_Id; Dim : Pos);
435 -- N is an array (sub-)aggregate. Dim is the dimension corresponding
436 -- to (sub-)aggregate N. This procedure collects and removes the side
437 -- effects of the constrained N_Range nodes corresponding to each index
438 -- dimension of our aggregate itype. These N_Range nodes are collected
439 -- in Aggr_Range above.
441 -- Likewise collect in Aggr_Low & Aggr_High above the low and high
442 -- bounds of each index dimension. If, when collecting, two bounds
443 -- corresponding to the same dimension are static and found to differ,
444 -- then emit a warning, and mark N as raising Constraint_Error.
446 -------------------------
447 -- Collect_Aggr_Bounds --
448 -------------------------
450 procedure Collect_Aggr_Bounds (N : Node_Id; Dim : Pos) is
451 This_Range : constant Node_Id := Aggregate_Bounds (N);
452 -- The aggregate range node of this specific sub-aggregate
454 This_Low : constant Node_Id := Low_Bound (Aggregate_Bounds (N));
455 This_High : constant Node_Id := High_Bound (Aggregate_Bounds (N));
456 -- The aggregate bounds of this specific sub-aggregate
462 Remove_Side_Effects (This_Low, Variable_Ref => True);
463 Remove_Side_Effects (This_High, Variable_Ref => True);
465 -- Collect the first N_Range for a given dimension that you find.
466 -- For a given dimension they must be all equal anyway.
468 if No (Aggr_Range (Dim)) then
469 Aggr_Low (Dim) := This_Low;
470 Aggr_High (Dim) := This_High;
471 Aggr_Range (Dim) := This_Range;
474 if Compile_Time_Known_Value (This_Low) then
475 if not Compile_Time_Known_Value (Aggr_Low (Dim)) then
476 Aggr_Low (Dim) := This_Low;
478 elsif Expr_Value (This_Low) /= Expr_Value (Aggr_Low (Dim)) then
479 Set_Raises_Constraint_Error (N);
480 Error_Msg_Warn := SPARK_Mode /= On;
481 Error_Msg_N ("sub-aggregate low bound mismatch<<", N);
482 Error_Msg_N ("\Constraint_Error [<<", N);
486 if Compile_Time_Known_Value (This_High) then
487 if not Compile_Time_Known_Value (Aggr_High (Dim)) then
488 Aggr_High (Dim) := This_High;
491 Expr_Value (This_High) /= Expr_Value (Aggr_High (Dim))
493 Set_Raises_Constraint_Error (N);
494 Error_Msg_Warn := SPARK_Mode /= On;
495 Error_Msg_N ("sub-aggregate high bound mismatch<<", N);
496 Error_Msg_N ("\Constraint_Error [<<", N);
501 if Dim < Aggr_Dimension then
503 -- Process positional components
505 if Present (Expressions (N)) then
506 Expr := First (Expressions (N));
507 while Present (Expr) loop
508 Collect_Aggr_Bounds (Expr, Dim + 1);
513 -- Process component associations
515 if Present (Component_Associations (N)) then
516 Is_Fully_Positional := False;
518 Assoc := First (Component_Associations (N));
519 while Present (Assoc) loop
520 Expr := Expression (Assoc);
521 Collect_Aggr_Bounds (Expr, Dim + 1);
526 end Collect_Aggr_Bounds;
528 -- Array_Aggr_Subtype variables
531 -- The final itype of the overall aggregate
533 Index_Constraints : constant List_Id := New_List;
534 -- The list of index constraints of the aggregate itype
536 -- Start of processing for Array_Aggr_Subtype
539 -- Make sure that the list of index constraints is properly attached to
540 -- the tree, and then collect the aggregate bounds.
542 Set_Parent (Index_Constraints, N);
543 Collect_Aggr_Bounds (N, 1);
545 -- Build the list of constrained indexes of our aggregate itype
547 for J in 1 .. Aggr_Dimension loop
548 Create_Index : declare
549 Index_Base : constant Entity_Id :=
550 Base_Type (Etype (Aggr_Range (J)));
551 Index_Typ : Entity_Id;
554 -- Construct the Index subtype, and associate it with the range
555 -- construct that generates it.
558 Create_Itype (Subtype_Kind (Ekind (Index_Base)), Aggr_Range (J));
560 Set_Etype (Index_Typ, Index_Base);
562 if Is_Character_Type (Index_Base) then
563 Set_Is_Character_Type (Index_Typ);
566 Set_Size_Info (Index_Typ, (Index_Base));
567 Set_RM_Size (Index_Typ, RM_Size (Index_Base));
568 Set_First_Rep_Item (Index_Typ, First_Rep_Item (Index_Base));
569 Set_Scalar_Range (Index_Typ, Aggr_Range (J));
571 if Is_Discrete_Or_Fixed_Point_Type (Index_Typ) then
572 Set_RM_Size (Index_Typ, UI_From_Int (Minimum_Size (Index_Typ)));
575 Set_Etype (Aggr_Range (J), Index_Typ);
577 Append (Aggr_Range (J), To => Index_Constraints);
581 -- Now build the Itype
583 Itype := Create_Itype (E_Array_Subtype, N);
585 Set_First_Rep_Item (Itype, First_Rep_Item (Typ));
586 Set_Convention (Itype, Convention (Typ));
587 Set_Depends_On_Private (Itype, Has_Private_Component (Typ));
588 Set_Etype (Itype, Base_Type (Typ));
589 Set_Has_Alignment_Clause (Itype, Has_Alignment_Clause (Typ));
590 Set_Is_Aliased (Itype, Is_Aliased (Typ));
591 Set_Is_Independent (Itype, Is_Independent (Typ));
592 Set_Depends_On_Private (Itype, Depends_On_Private (Typ));
594 Copy_Suppress_Status (Index_Check, Typ, Itype);
595 Copy_Suppress_Status (Length_Check, Typ, Itype);
597 Set_First_Index (Itype, First (Index_Constraints));
598 Set_Is_Constrained (Itype, True);
599 Set_Is_Internal (Itype, True);
601 if Has_Predicates (Typ) then
602 Set_Has_Predicates (Itype);
604 -- If the base type has a predicate, capture the predicated parent
605 -- or the existing predicate function for SPARK use.
607 if Present (Predicate_Function (Typ)) then
608 Set_Predicate_Function (Itype, Predicate_Function (Typ));
610 elsif Is_Itype (Typ) then
611 Set_Predicated_Parent (Itype, Predicated_Parent (Typ));
614 Set_Predicated_Parent (Itype, Typ);
618 -- A simple optimization: purely positional aggregates of static
619 -- components should be passed to gigi unexpanded whenever possible, and
620 -- regardless of the staticness of the bounds themselves. Subsequent
621 -- checks in exp_aggr verify that type is not packed, etc.
623 Set_Size_Known_At_Compile_Time
626 and then Comes_From_Source (N)
627 and then Size_Known_At_Compile_Time (Component_Type (Typ)));
629 -- We always need a freeze node for a packed array subtype, so that we
630 -- can build the Packed_Array_Impl_Type corresponding to the subtype. If
631 -- expansion is disabled, the packed array subtype is not built, and we
632 -- must not generate a freeze node for the type, or else it will appear
633 -- incomplete to gigi.
636 and then not In_Spec_Expression
637 and then Expander_Active
639 Freeze_Itype (Itype, N);
643 end Array_Aggr_Subtype;
645 --------------------------------
646 -- Check_Misspelled_Component --
647 --------------------------------
649 procedure Check_Misspelled_Component
650 (Elements : Elist_Id;
653 Max_Suggestions : constant := 2;
655 Nr_Of_Suggestions : Natural := 0;
656 Suggestion_1 : Entity_Id := Empty;
657 Suggestion_2 : Entity_Id := Empty;
658 Component_Elmt : Elmt_Id;
661 -- All the components of List are matched against Component and a count
662 -- is maintained of possible misspellings. When at the end of the
663 -- analysis there are one or two (not more) possible misspellings,
664 -- these misspellings will be suggested as possible corrections.
666 Component_Elmt := First_Elmt (Elements);
667 while Nr_Of_Suggestions <= Max_Suggestions
668 and then Present (Component_Elmt)
670 if Is_Bad_Spelling_Of
671 (Chars (Node (Component_Elmt)),
674 Nr_Of_Suggestions := Nr_Of_Suggestions + 1;
676 case Nr_Of_Suggestions is
677 when 1 => Suggestion_1 := Node (Component_Elmt);
678 when 2 => Suggestion_2 := Node (Component_Elmt);
683 Next_Elmt (Component_Elmt);
686 -- Report at most two suggestions
688 if Nr_Of_Suggestions = 1 then
689 Error_Msg_NE -- CODEFIX
690 ("\possible misspelling of&", Component, Suggestion_1);
692 elsif Nr_Of_Suggestions = 2 then
693 Error_Msg_Node_2 := Suggestion_2;
694 Error_Msg_NE -- CODEFIX
695 ("\possible misspelling of& or&", Component, Suggestion_1);
697 end Check_Misspelled_Component;
699 ----------------------------------------
700 -- Check_Expr_OK_In_Limited_Aggregate --
701 ----------------------------------------
703 procedure Check_Expr_OK_In_Limited_Aggregate (Expr : Node_Id) is
705 if Is_Limited_Type (Etype (Expr))
706 and then Comes_From_Source (Expr)
708 if In_Instance_Body or else In_Inlined_Body then
711 elsif not OK_For_Limited_Init (Etype (Expr), Expr) then
713 ("initialization not allowed for limited types", Expr);
714 Explain_Limited_Type (Etype (Expr), Expr);
717 end Check_Expr_OK_In_Limited_Aggregate;
719 -------------------------
720 -- Is_Others_Aggregate --
721 -------------------------
723 function Is_Others_Aggregate (Aggr : Node_Id) return Boolean is
724 Assoc : constant List_Id := Component_Associations (Aggr);
727 return No (Expressions (Aggr))
728 and then Nkind (First (Choice_List (First (Assoc)))) = N_Others_Choice;
729 end Is_Others_Aggregate;
731 -------------------------
732 -- Is_Single_Aggregate --
733 -------------------------
735 function Is_Single_Aggregate (Aggr : Node_Id) return Boolean is
736 Assoc : constant List_Id := Component_Associations (Aggr);
739 return No (Expressions (Aggr))
740 and then No (Next (First (Assoc)))
741 and then No (Next (First (Choice_List (First (Assoc)))));
742 end Is_Single_Aggregate;
744 --------------------------------
745 -- Make_String_Into_Aggregate --
746 --------------------------------
748 procedure Make_String_Into_Aggregate (N : Node_Id) is
749 Exprs : constant List_Id := New_List;
750 Loc : constant Source_Ptr := Sloc (N);
751 Str : constant String_Id := Strval (N);
752 Strlen : constant Nat := String_Length (Str);
760 for J in 1 .. Strlen loop
761 C := Get_String_Char (Str, J);
762 Set_Character_Literal_Name (C);
765 Make_Character_Literal (P,
767 Char_Literal_Value => UI_From_CC (C));
768 Set_Etype (C_Node, Any_Character);
769 Append_To (Exprs, C_Node);
772 -- Something special for wide strings???
775 New_N := Make_Aggregate (Loc, Expressions => Exprs);
776 Set_Analyzed (New_N);
777 Set_Etype (New_N, Any_Composite);
780 end Make_String_Into_Aggregate;
782 -----------------------
783 -- Resolve_Aggregate --
784 -----------------------
786 procedure Resolve_Aggregate (N : Node_Id; Typ : Entity_Id) is
787 Loc : constant Source_Ptr := Sloc (N);
789 Aggr_Subtyp : Entity_Id;
790 -- The actual aggregate subtype. This is not necessarily the same as Typ
791 -- which is the subtype of the context in which the aggregate was found.
794 -- Ignore junk empty aggregate resulting from parser error
796 if No (Expressions (N))
797 and then No (Component_Associations (N))
798 and then not Null_Record_Present (N)
803 -- If the aggregate has box-initialized components, its type must be
804 -- frozen so that initialization procedures can properly be called
805 -- in the resolution that follows. The replacement of boxes with
806 -- initialization calls is properly an expansion activity but it must
807 -- be done during resolution.
810 and then Present (Component_Associations (N))
816 Comp := First (Component_Associations (N));
817 while Present (Comp) loop
818 if Box_Present (Comp) then
819 Insert_Actions (N, Freeze_Entity (Typ, N));
828 -- Check for aggregates not allowed in configurable run-time mode.
829 -- We allow all cases of aggregates that do not come from source, since
830 -- these are all assumed to be small (e.g. bounds of a string literal).
831 -- We also allow aggregates of types we know to be small.
833 if not Support_Aggregates_On_Target
834 and then Comes_From_Source (N)
835 and then (not Known_Static_Esize (Typ)
836 or else Esize (Typ) > System_Max_Integer_Size)
838 Error_Msg_CRT ("aggregate", N);
841 -- Ada 2005 (AI-287): Limited aggregates allowed
843 -- In an instance, ignore aggregate subcomponents tnat may be limited,
844 -- because they originate in view conflicts. If the original aggregate
845 -- is legal and the actuals are legal, the aggregate itself is legal.
847 if Is_Limited_Type (Typ)
848 and then Ada_Version < Ada_2005
849 and then not In_Instance
851 Error_Msg_N ("aggregate type cannot be limited", N);
852 Explain_Limited_Type (Typ, N);
854 elsif Is_Class_Wide_Type (Typ) then
855 Error_Msg_N ("type of aggregate cannot be class-wide", N);
857 elsif Typ = Any_String
858 or else Typ = Any_Composite
860 Error_Msg_N ("no unique type for aggregate", N);
861 Set_Etype (N, Any_Composite);
863 elsif Is_Array_Type (Typ) and then Null_Record_Present (N) then
864 Error_Msg_N ("null record forbidden in array aggregate", N);
866 elsif Is_Record_Type (Typ) then
867 Resolve_Record_Aggregate (N, Typ);
869 elsif Is_Array_Type (Typ) then
871 -- First a special test, for the case of a positional aggregate of
872 -- characters which can be replaced by a string literal.
874 -- Do not perform this transformation if this was a string literal
875 -- to start with, whose components needed constraint checks, or if
876 -- the component type is non-static, because it will require those
877 -- checks and be transformed back into an aggregate. If the index
878 -- type is not Integer the aggregate may represent a user-defined
879 -- string type but the context might need the original type so we
880 -- do not perform the transformation at this point.
882 if Number_Dimensions (Typ) = 1
883 and then Is_Standard_Character_Type (Component_Type (Typ))
884 and then No (Component_Associations (N))
885 and then not Is_Limited_Composite (Typ)
886 and then not Is_Private_Composite (Typ)
887 and then not Is_Bit_Packed_Array (Typ)
888 and then Nkind (Original_Node (Parent (N))) /= N_String_Literal
889 and then Is_OK_Static_Subtype (Component_Type (Typ))
890 and then Base_Type (Etype (First_Index (Typ))) =
891 Base_Type (Standard_Integer)
897 Expr := First (Expressions (N));
898 while Present (Expr) loop
899 exit when Nkind (Expr) /= N_Character_Literal;
906 Expr := First (Expressions (N));
907 while Present (Expr) loop
908 Store_String_Char (UI_To_CC (Char_Literal_Value (Expr)));
912 Rewrite (N, Make_String_Literal (Loc, End_String));
914 Analyze_And_Resolve (N, Typ);
920 -- Here if we have a real aggregate to deal with
922 Array_Aggregate : declare
923 Aggr_Resolved : Boolean;
925 Aggr_Typ : constant Entity_Id := Etype (Typ);
926 -- This is the unconstrained array type, which is the type against
927 -- which the aggregate is to be resolved. Typ itself is the array
928 -- type of the context which may not be the same subtype as the
929 -- subtype for the final aggregate.
932 -- In the following we determine whether an OTHERS choice is
933 -- allowed inside the array aggregate. The test checks the context
934 -- in which the array aggregate occurs. If the context does not
935 -- permit it, or the aggregate type is unconstrained, an OTHERS
936 -- choice is not allowed (except that it is always allowed on the
937 -- right-hand side of an assignment statement; in this case the
938 -- constrainedness of the type doesn't matter, because an array
939 -- object is always constrained).
941 -- If expansion is disabled (generic context, or semantics-only
942 -- mode) actual subtypes cannot be constructed, and the type of an
943 -- object may be its unconstrained nominal type. However, if the
944 -- context is an assignment statement, OTHERS is allowed, because
945 -- the target of the assignment will have a constrained subtype
946 -- when fully compiled. Ditto if the context is an initialization
947 -- procedure where a component may have a predicate function that
948 -- carries the base type.
950 -- Note that there is no node for Explicit_Actual_Parameter.
951 -- To test for this context we therefore have to test for node
952 -- N_Parameter_Association which itself appears only if there is a
953 -- formal parameter. Consequently we also need to test for
954 -- N_Procedure_Call_Statement or N_Function_Call.
956 -- The context may be an N_Reference node, created by expansion.
957 -- Legality of the others clause was established in the source,
958 -- so the context is legal.
960 Set_Etype (N, Aggr_Typ); -- May be overridden later on
962 if Nkind (Parent (N)) = N_Assignment_Statement
963 or else Inside_Init_Proc
964 or else (Is_Constrained (Typ)
965 and then Nkind (Parent (N)) in
966 N_Parameter_Association
968 | N_Procedure_Call_Statement
969 | N_Generic_Association
970 | N_Formal_Object_Declaration
971 | N_Simple_Return_Statement
972 | N_Object_Declaration
973 | N_Component_Declaration
974 | N_Parameter_Specification
975 | N_Qualified_Expression
978 | N_Extension_Aggregate
979 | N_Component_Association
980 | N_Case_Expression_Alternative
982 | N_Expression_With_Actions)
985 Resolve_Array_Aggregate
987 Index => First_Index (Aggr_Typ),
988 Index_Constr => First_Index (Typ),
989 Component_Typ => Component_Type (Typ),
990 Others_Allowed => True);
993 Resolve_Array_Aggregate
995 Index => First_Index (Aggr_Typ),
996 Index_Constr => First_Index (Aggr_Typ),
997 Component_Typ => Component_Type (Typ),
998 Others_Allowed => False);
1001 if not Aggr_Resolved then
1003 -- A parenthesized expression may have been intended as an
1004 -- aggregate, leading to a type error when analyzing the
1005 -- component. This can also happen for a nested component
1006 -- (see Analyze_Aggr_Expr).
1008 if Paren_Count (N) > 0 then
1010 ("positional aggregate cannot have one component", N);
1013 Aggr_Subtyp := Any_Composite;
1016 Aggr_Subtyp := Array_Aggr_Subtype (N, Typ);
1019 Set_Etype (N, Aggr_Subtyp);
1020 end Array_Aggregate;
1022 elsif Is_Private_Type (Typ)
1023 and then Present (Full_View (Typ))
1024 and then (In_Inlined_Body or In_Instance_Body)
1025 and then Is_Composite_Type (Full_View (Typ))
1027 Resolve (N, Full_View (Typ));
1030 Error_Msg_N ("illegal context for aggregate", N);
1033 -- If we can determine statically that the evaluation of the aggregate
1034 -- raises Constraint_Error, then replace the aggregate with an
1035 -- N_Raise_Constraint_Error node, but set the Etype to the right
1036 -- aggregate subtype. Gigi needs this.
1038 if Raises_Constraint_Error (N) then
1039 Aggr_Subtyp := Etype (N);
1041 Make_Raise_Constraint_Error (Loc, Reason => CE_Range_Check_Failed));
1042 Set_Raises_Constraint_Error (N);
1043 Set_Etype (N, Aggr_Subtyp);
1047 Check_Function_Writable_Actuals (N);
1048 end Resolve_Aggregate;
1050 -----------------------------
1051 -- Resolve_Array_Aggregate --
1052 -----------------------------
1054 function Resolve_Array_Aggregate
1057 Index_Constr : Node_Id;
1058 Component_Typ : Entity_Id;
1059 Others_Allowed : Boolean) return Boolean
1061 Loc : constant Source_Ptr := Sloc (N);
1063 Failure : constant Boolean := False;
1064 Success : constant Boolean := True;
1066 Index_Typ : constant Entity_Id := Etype (Index);
1067 Index_Typ_Low : constant Node_Id := Type_Low_Bound (Index_Typ);
1068 Index_Typ_High : constant Node_Id := Type_High_Bound (Index_Typ);
1069 -- The type of the index corresponding to the array sub-aggregate along
1070 -- with its low and upper bounds.
1072 Index_Base : constant Entity_Id := Base_Type (Index_Typ);
1073 Index_Base_Low : constant Node_Id := Type_Low_Bound (Index_Base);
1074 Index_Base_High : constant Node_Id := Type_High_Bound (Index_Base);
1075 -- Ditto for the base type
1077 Others_Present : Boolean := False;
1079 Nb_Choices : Nat := 0;
1080 -- Contains the overall number of named choices in this sub-aggregate
1082 function Add (Val : Uint; To : Node_Id) return Node_Id;
1083 -- Creates a new expression node where Val is added to expression To.
1084 -- Tries to constant fold whenever possible. To must be an already
1085 -- analyzed expression.
1087 procedure Check_Bound (BH : Node_Id; AH : in out Node_Id);
1088 -- Checks that AH (the upper bound of an array aggregate) is less than
1089 -- or equal to BH (the upper bound of the index base type). If the check
1090 -- fails, a warning is emitted, the Raises_Constraint_Error flag of N is
1091 -- set, and AH is replaced with a duplicate of BH.
1093 procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id);
1094 -- Checks that range AL .. AH is compatible with range L .. H. Emits a
1095 -- warning if not and sets the Raises_Constraint_Error flag in N.
1097 procedure Check_Length (L, H : Node_Id; Len : Uint);
1098 -- Checks that range L .. H contains at least Len elements. Emits a
1099 -- warning if not and sets the Raises_Constraint_Error flag in N.
1101 function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean;
1102 -- Returns True if range L .. H is dynamic or null
1104 procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean);
1105 -- Given expression node From, this routine sets OK to False if it
1106 -- cannot statically evaluate From. Otherwise it stores this static
1107 -- value into Value.
1109 function Resolve_Aggr_Expr
1111 Single_Elmt : Boolean) return Boolean;
1112 -- Resolves aggregate expression Expr. Returns False if resolution
1113 -- fails. If Single_Elmt is set to False, the expression Expr may be
1114 -- used to initialize several array aggregate elements (this can happen
1115 -- for discrete choices such as "L .. H => Expr" or the OTHERS choice).
1116 -- In this event we do not resolve Expr unless expansion is disabled.
1117 -- To know why, see the DELAYED COMPONENT RESOLUTION note above.
1119 -- NOTE: In the case of "... => <>", we pass the in the
1120 -- N_Component_Association node as Expr, since there is no Expression in
1121 -- that case, and we need a Sloc for the error message.
1123 procedure Resolve_Iterated_Component_Association
1125 Index_Typ : Entity_Id);
1132 function Add (Val : Uint; To : Node_Id) return Node_Id is
1138 if Raises_Constraint_Error (To) then
1142 -- First test if we can do constant folding
1144 if Compile_Time_Known_Value (To)
1145 or else Nkind (To) = N_Integer_Literal
1147 Expr_Pos := Make_Integer_Literal (Loc, Expr_Value (To) + Val);
1148 Set_Is_Static_Expression (Expr_Pos);
1149 Set_Etype (Expr_Pos, Etype (To));
1150 Set_Analyzed (Expr_Pos, Analyzed (To));
1152 if not Is_Enumeration_Type (Index_Typ) then
1155 -- If we are dealing with enumeration return
1156 -- Index_Typ'Val (Expr_Pos)
1160 Make_Attribute_Reference
1162 Prefix => New_Occurrence_Of (Index_Typ, Loc),
1163 Attribute_Name => Name_Val,
1164 Expressions => New_List (Expr_Pos));
1170 -- If we are here no constant folding possible
1172 if not Is_Enumeration_Type (Index_Base) then
1175 Left_Opnd => Duplicate_Subexpr (To),
1176 Right_Opnd => Make_Integer_Literal (Loc, Val));
1178 -- If we are dealing with enumeration return
1179 -- Index_Typ'Val (Index_Typ'Pos (To) + Val)
1183 Make_Attribute_Reference
1185 Prefix => New_Occurrence_Of (Index_Typ, Loc),
1186 Attribute_Name => Name_Pos,
1187 Expressions => New_List (Duplicate_Subexpr (To)));
1191 Left_Opnd => To_Pos,
1192 Right_Opnd => Make_Integer_Literal (Loc, Val));
1195 Make_Attribute_Reference
1197 Prefix => New_Occurrence_Of (Index_Typ, Loc),
1198 Attribute_Name => Name_Val,
1199 Expressions => New_List (Expr_Pos));
1201 -- If the index type has a non standard representation, the
1202 -- attributes 'Val and 'Pos expand into function calls and the
1203 -- resulting expression is considered non-safe for reevaluation
1204 -- by the backend. Relocate it into a constant temporary in order
1205 -- to make it safe for reevaluation.
1207 if Has_Non_Standard_Rep (Etype (N)) then
1212 Def_Id := Make_Temporary (Loc, 'R', Expr);
1213 Set_Etype (Def_Id, Index_Typ);
1215 Make_Object_Declaration (Loc,
1216 Defining_Identifier => Def_Id,
1217 Object_Definition =>
1218 New_Occurrence_Of (Index_Typ, Loc),
1219 Constant_Present => True,
1220 Expression => Relocate_Node (Expr)));
1222 Expr := New_Occurrence_Of (Def_Id, Loc);
1234 procedure Check_Bound (BH : Node_Id; AH : in out Node_Id) is
1242 Get (Value => Val_BH, From => BH, OK => OK_BH);
1243 Get (Value => Val_AH, From => AH, OK => OK_AH);
1245 if OK_BH and then OK_AH and then Val_BH < Val_AH then
1246 Set_Raises_Constraint_Error (N);
1247 Error_Msg_Warn := SPARK_Mode /= On;
1248 Error_Msg_N ("upper bound out of range<<", AH);
1249 Error_Msg_N ("\Constraint_Error [<<", AH);
1251 -- You need to set AH to BH or else in the case of enumerations
1252 -- indexes we will not be able to resolve the aggregate bounds.
1254 AH := Duplicate_Subexpr (BH);
1262 procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id) is
1273 pragma Warnings (Off, OK_AL);
1274 pragma Warnings (Off, OK_AH);
1277 if Raises_Constraint_Error (N)
1278 or else Dynamic_Or_Null_Range (AL, AH)
1283 Get (Value => Val_L, From => L, OK => OK_L);
1284 Get (Value => Val_H, From => H, OK => OK_H);
1286 Get (Value => Val_AL, From => AL, OK => OK_AL);
1287 Get (Value => Val_AH, From => AH, OK => OK_AH);
1289 if OK_L and then Val_L > Val_AL then
1290 Set_Raises_Constraint_Error (N);
1291 Error_Msg_Warn := SPARK_Mode /= On;
1292 Error_Msg_N ("lower bound of aggregate out of range<<", N);
1293 Error_Msg_N ("\Constraint_Error [<<", N);
1296 if OK_H and then Val_H < Val_AH then
1297 Set_Raises_Constraint_Error (N);
1298 Error_Msg_Warn := SPARK_Mode /= On;
1299 Error_Msg_N ("upper bound of aggregate out of range<<", N);
1300 Error_Msg_N ("\Constraint_Error [<<", N);
1308 procedure Check_Length (L, H : Node_Id; Len : Uint) is
1318 if Raises_Constraint_Error (N) then
1322 Get (Value => Val_L, From => L, OK => OK_L);
1323 Get (Value => Val_H, From => H, OK => OK_H);
1325 if not OK_L or else not OK_H then
1329 -- If null range length is zero
1331 if Val_L > Val_H then
1332 Range_Len := Uint_0;
1334 Range_Len := Val_H - Val_L + 1;
1337 if Range_Len < Len then
1338 Set_Raises_Constraint_Error (N);
1339 Error_Msg_Warn := SPARK_Mode /= On;
1340 Error_Msg_N ("too many elements<<", N);
1341 Error_Msg_N ("\Constraint_Error [<<", N);
1345 ---------------------------
1346 -- Dynamic_Or_Null_Range --
1347 ---------------------------
1349 function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean is
1357 Get (Value => Val_L, From => L, OK => OK_L);
1358 Get (Value => Val_H, From => H, OK => OK_H);
1360 return not OK_L or else not OK_H
1361 or else not Is_OK_Static_Expression (L)
1362 or else not Is_OK_Static_Expression (H)
1363 or else Val_L > Val_H;
1364 end Dynamic_Or_Null_Range;
1370 procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean) is
1374 if Compile_Time_Known_Value (From) then
1375 Value := Expr_Value (From);
1377 -- If expression From is something like Some_Type'Val (10) then
1380 elsif Nkind (From) = N_Attribute_Reference
1381 and then Attribute_Name (From) = Name_Val
1382 and then Compile_Time_Known_Value (First (Expressions (From)))
1384 Value := Expr_Value (First (Expressions (From)));
1391 -----------------------
1392 -- Resolve_Aggr_Expr --
1393 -----------------------
1395 function Resolve_Aggr_Expr
1397 Single_Elmt : Boolean) return Boolean
1399 Nxt_Ind : constant Node_Id := Next_Index (Index);
1400 Nxt_Ind_Constr : constant Node_Id := Next_Index (Index_Constr);
1401 -- Index is the current index corresponding to the expression
1403 Resolution_OK : Boolean := True;
1404 -- Set to False if resolution of the expression failed
1407 -- Defend against previous errors
1409 if Nkind (Expr) = N_Error
1410 or else Error_Posted (Expr)
1415 -- If the array type against which we are resolving the aggregate
1416 -- has several dimensions, the expressions nested inside the
1417 -- aggregate must be further aggregates (or strings).
1419 if Present (Nxt_Ind) then
1420 if Nkind (Expr) /= N_Aggregate then
1422 -- A string literal can appear where a one-dimensional array
1423 -- of characters is expected. If the literal looks like an
1424 -- operator, it is still an operator symbol, which will be
1425 -- transformed into a string when analyzed.
1427 if Is_Character_Type (Component_Typ)
1428 and then No (Next_Index (Nxt_Ind))
1429 and then Nkind (Expr) in N_String_Literal | N_Operator_Symbol
1431 -- A string literal used in a multidimensional array
1432 -- aggregate in place of the final one-dimensional
1433 -- aggregate must not be enclosed in parentheses.
1435 if Paren_Count (Expr) /= 0 then
1436 Error_Msg_N ("no parenthesis allowed here", Expr);
1439 Make_String_Into_Aggregate (Expr);
1442 Error_Msg_N ("nested array aggregate expected", Expr);
1444 -- If the expression is parenthesized, this may be
1445 -- a missing component association for a 1-aggregate.
1447 if Paren_Count (Expr) > 0 then
1449 ("\if single-component aggregate is intended, "
1450 & "write e.g. (1 ='> ...)", Expr);
1457 -- If it's "... => <>", nothing to resolve
1459 if Nkind (Expr) = N_Component_Association then
1460 pragma Assert (Box_Present (Expr));
1464 -- Ada 2005 (AI-231): Propagate the type to the nested aggregate.
1465 -- Required to check the null-exclusion attribute (if present).
1466 -- This value may be overridden later on.
1468 Set_Etype (Expr, Etype (N));
1470 Resolution_OK := Resolve_Array_Aggregate
1471 (Expr, Nxt_Ind, Nxt_Ind_Constr, Component_Typ, Others_Allowed);
1474 -- If it's "... => <>", nothing to resolve
1476 if Nkind (Expr) = N_Component_Association then
1477 pragma Assert (Box_Present (Expr));
1481 -- Do not resolve the expressions of discrete or others choices
1482 -- unless the expression covers a single component, or the
1483 -- expander is inactive.
1485 -- In SPARK mode, expressions that can perform side effects will
1486 -- be recognized by the gnat2why back-end, and the whole
1487 -- subprogram will be ignored. So semantic analysis can be
1488 -- performed safely.
1491 or else not Expander_Active
1492 or else In_Spec_Expression
1494 Analyze_And_Resolve (Expr, Component_Typ);
1495 Check_Expr_OK_In_Limited_Aggregate (Expr);
1496 Check_Non_Static_Context (Expr);
1497 Aggregate_Constraint_Checks (Expr, Component_Typ);
1498 Check_Unset_Reference (Expr);
1502 -- If an aggregate component has a type with predicates, an explicit
1503 -- predicate check must be applied, as for an assignment statement,
1504 -- because the aggregate might not be expanded into individual
1505 -- component assignments. If the expression covers several components
1506 -- the analysis and the predicate check take place later.
1508 if Has_Predicates (Component_Typ)
1509 and then Analyzed (Expr)
1511 Apply_Predicate_Check (Expr, Component_Typ);
1514 if Raises_Constraint_Error (Expr)
1515 and then Nkind (Parent (Expr)) /= N_Component_Association
1517 Set_Raises_Constraint_Error (N);
1520 -- If the expression has been marked as requiring a range check,
1521 -- then generate it here. It's a bit odd to be generating such
1522 -- checks in the analyzer, but harmless since Generate_Range_Check
1523 -- does nothing (other than making sure Do_Range_Check is set) if
1524 -- the expander is not active.
1526 if Do_Range_Check (Expr) then
1527 Generate_Range_Check (Expr, Component_Typ, CE_Range_Check_Failed);
1530 return Resolution_OK;
1531 end Resolve_Aggr_Expr;
1533 --------------------------------------------
1534 -- Resolve_Iterated_Component_Association --
1535 --------------------------------------------
1537 procedure Resolve_Iterated_Component_Association
1539 Index_Typ : Entity_Id)
1541 Loc : constant Source_Ptr := Sloc (N);
1550 -- An element iterator specification cannot appear in
1551 -- an array aggregate because it does not provide index
1552 -- values for the association. This must be a semantic
1553 -- check because the parser cannot tell whether this is
1554 -- an array aggregate or a container aggregate.
1556 if Present (Iterator_Specification (N)) then
1557 Error_Msg_N ("container element Iterator cannot appear "
1558 & "in an array aggregate", N);
1562 Choice := First (Discrete_Choices (N));
1564 while Present (Choice) loop
1565 if Nkind (Choice) = N_Others_Choice then
1566 Others_Present := True;
1571 -- Choice can be a subtype name, a range, or an expression
1573 if Is_Entity_Name (Choice)
1574 and then Is_Type (Entity (Choice))
1575 and then Base_Type (Entity (Choice)) = Base_Type (Index_Typ)
1580 Analyze_And_Resolve (Choice, Index_Typ);
1587 -- Create a scope in which to introduce an index, which is usually
1588 -- visible in the expression for the component, and needed for its
1591 Ent := New_Internal_Entity (E_Loop, Current_Scope, Loc, 'L');
1592 Set_Etype (Ent, Standard_Void_Type);
1593 Set_Parent (Ent, Parent (N));
1596 Make_Defining_Identifier (Loc,
1597 Chars => Chars (Defining_Identifier (N)));
1599 -- Insert and decorate the index variable in the current scope.
1600 -- The expression has to be analyzed once the index variable is
1601 -- directly visible. Mark the variable as referenced to prevent
1602 -- spurious warnings, given that subsequent uses of its name in the
1603 -- expression will reference the internal (synonym) loop variable.
1606 Set_Etype (Id, Index_Typ);
1607 Set_Ekind (Id, E_Variable);
1608 Set_Scope (Id, Ent);
1609 Set_Referenced (Id);
1611 -- Analyze a copy of the expression, to verify legality. We use
1612 -- a copy because the expression will be analyzed anew when the
1613 -- enclosing aggregate is expanded, and the construct is rewritten
1614 -- as a loop with a new index variable.
1616 Expr := New_Copy_Tree (Expression (N));
1617 Dummy := Resolve_Aggr_Expr (Expr, False);
1619 -- An iterated_component_association may appear in a nested
1620 -- aggregate for a multidimensional structure: preserve the bounds
1621 -- computed for the expression, as well as the anonymous array
1622 -- type generated for it; both are needed during array expansion.
1623 -- This does not work for more than two levels of nesting. ???
1625 if Nkind (Expr) = N_Aggregate then
1626 Set_Aggregate_Bounds (Expression (N), Aggregate_Bounds (Expr));
1627 Set_Etype (Expression (N), Etype (Expr));
1631 end Resolve_Iterated_Component_Association;
1640 Aggr_Low : Node_Id := Empty;
1641 Aggr_High : Node_Id := Empty;
1642 -- The actual low and high bounds of this sub-aggregate
1644 Case_Table_Size : Nat;
1645 -- Contains the size of the case table needed to sort aggregate choices
1647 Choices_Low : Node_Id := Empty;
1648 Choices_High : Node_Id := Empty;
1649 -- The lowest and highest discrete choices values for a named aggregate
1651 Delete_Choice : Boolean;
1652 -- Used when replacing a subtype choice with predicate by a list
1654 Nb_Elements : Uint := Uint_0;
1655 -- The number of elements in a positional aggregate
1657 Nb_Discrete_Choices : Nat := 0;
1658 -- The overall number of discrete choices (not counting others choice)
1660 -- Start of processing for Resolve_Array_Aggregate
1663 -- Ignore junk empty aggregate resulting from parser error
1665 if No (Expressions (N))
1666 and then No (Component_Associations (N))
1667 and then not Null_Record_Present (N)
1672 -- STEP 1: make sure the aggregate is correctly formatted
1674 if Present (Component_Associations (N)) then
1675 Assoc := First (Component_Associations (N));
1676 while Present (Assoc) loop
1677 if Nkind (Assoc) = N_Iterated_Component_Association then
1678 Resolve_Iterated_Component_Association (Assoc, Index_Typ);
1681 Choice := First (Choice_List (Assoc));
1682 Delete_Choice := False;
1683 while Present (Choice) loop
1684 if Nkind (Choice) = N_Others_Choice then
1685 Others_Present := True;
1687 if Choice /= First (Choice_List (Assoc))
1688 or else Present (Next (Choice))
1691 ("OTHERS must appear alone in a choice list", Choice);
1695 if Present (Next (Assoc)) then
1697 ("OTHERS must appear last in an aggregate", Choice);
1701 if Ada_Version = Ada_83
1702 and then Assoc /= First (Component_Associations (N))
1703 and then Nkind (Parent (N)) in
1704 N_Assignment_Statement | N_Object_Declaration
1707 ("(Ada 83) illegal context for OTHERS choice", N);
1710 elsif Is_Entity_Name (Choice) then
1714 E : constant Entity_Id := Entity (Choice);
1720 if Is_Type (E) and then Has_Predicates (E) then
1721 Freeze_Before (N, E);
1723 if Has_Dynamic_Predicate_Aspect (E) then
1725 ("subtype& has dynamic predicate, not allowed "
1726 & "in aggregate choice", Choice, E);
1728 elsif not Is_OK_Static_Subtype (E) then
1730 ("non-static subtype& has predicate, not allowed "
1731 & "in aggregate choice", Choice, E);
1734 -- If the subtype has a static predicate, replace the
1735 -- original choice with the list of individual values
1736 -- covered by the predicate.
1737 -- This should be deferred to expansion time ???
1739 if Present (Static_Discrete_Predicate (E)) then
1740 Delete_Choice := True;
1743 P := First (Static_Discrete_Predicate (E));
1744 while Present (P) loop
1746 Set_Sloc (C, Sloc (Choice));
1747 Append_To (New_Cs, C);
1751 Insert_List_After (Choice, New_Cs);
1757 Nb_Choices := Nb_Choices + 1;
1760 C : constant Node_Id := Choice;
1765 if Delete_Choice then
1767 Nb_Choices := Nb_Choices - 1;
1768 Delete_Choice := False;
1777 -- At this point we know that the others choice, if present, is by
1778 -- itself and appears last in the aggregate. Check if we have mixed
1779 -- positional and discrete associations (other than the others choice).
1781 if Present (Expressions (N))
1782 and then (Nb_Choices > 1
1783 or else (Nb_Choices = 1 and then not Others_Present))
1786 ("named association cannot follow positional association",
1787 First (Choice_List (First (Component_Associations (N)))));
1791 -- Test for the validity of an others choice if present
1793 if Others_Present and then not Others_Allowed then
1795 ("OTHERS choice not allowed here",
1796 First (Choices (First (Component_Associations (N)))));
1800 -- Protect against cascaded errors
1802 if Etype (Index_Typ) = Any_Type then
1806 -- STEP 2: Process named components
1808 if No (Expressions (N)) then
1809 if Others_Present then
1810 Case_Table_Size := Nb_Choices - 1;
1812 Case_Table_Size := Nb_Choices;
1816 function Empty_Range (A : Node_Id) return Boolean;
1817 -- If an association covers an empty range, some warnings on the
1818 -- expression of the association can be disabled.
1824 function Empty_Range (A : Node_Id) return Boolean is
1825 R : constant Node_Id := First (Choices (A));
1827 return No (Next (R))
1828 and then Nkind (R) = N_Range
1829 and then Compile_Time_Compare
1830 (Low_Bound (R), High_Bound (R), False) = GT;
1837 -- Denote the lowest and highest values in an aggregate choice
1839 S_Low : Node_Id := Empty;
1840 S_High : Node_Id := Empty;
1841 -- if a choice in an aggregate is a subtype indication these
1842 -- denote the lowest and highest values of the subtype
1844 Table : Case_Table_Type (1 .. Case_Table_Size);
1845 -- Used to sort all the different choice values
1847 Single_Choice : Boolean;
1848 -- Set to true every time there is a single discrete choice in a
1849 -- discrete association
1851 Prev_Nb_Discrete_Choices : Nat;
1852 -- Used to keep track of the number of discrete choices in the
1853 -- current association.
1855 Errors_Posted_On_Choices : Boolean := False;
1856 -- Keeps track of whether any choices have semantic errors
1858 -- Start of processing for Step_2
1861 -- STEP 2 (A): Check discrete choices validity
1863 Assoc := First (Component_Associations (N));
1864 while Present (Assoc) loop
1865 Prev_Nb_Discrete_Choices := Nb_Discrete_Choices;
1866 Choice := First (Choice_List (Assoc));
1871 if Nkind (Choice) = N_Others_Choice then
1872 Single_Choice := False;
1875 -- Test for subtype mark without constraint
1877 elsif Is_Entity_Name (Choice) and then
1878 Is_Type (Entity (Choice))
1880 if Base_Type (Entity (Choice)) /= Index_Base then
1882 ("invalid subtype mark in aggregate choice",
1887 -- Case of subtype indication
1889 elsif Nkind (Choice) = N_Subtype_Indication then
1890 Resolve_Discrete_Subtype_Indication (Choice, Index_Base);
1892 if Has_Dynamic_Predicate_Aspect
1893 (Entity (Subtype_Mark (Choice)))
1896 ("subtype& has dynamic predicate, "
1897 & "not allowed in aggregate choice",
1898 Choice, Entity (Subtype_Mark (Choice)));
1901 -- Does the subtype indication evaluation raise CE?
1903 Get_Index_Bounds (Subtype_Mark (Choice), S_Low, S_High);
1904 Get_Index_Bounds (Choice, Low, High);
1905 Check_Bounds (S_Low, S_High, Low, High);
1907 -- Case of range or expression
1910 Resolve (Choice, Index_Base);
1911 Check_Unset_Reference (Choice);
1912 Check_Non_Static_Context (Choice);
1914 -- If semantic errors were posted on the choice, then
1915 -- record that for possible early return from later
1916 -- processing (see handling of enumeration choices).
1918 if Error_Posted (Choice) then
1919 Errors_Posted_On_Choices := True;
1922 -- Do not range check a choice. This check is redundant
1923 -- since this test is already done when we check that the
1924 -- bounds of the array aggregate are within range.
1926 Set_Do_Range_Check (Choice, False);
1929 -- If we could not resolve the discrete choice stop here
1931 if Etype (Choice) = Any_Type then
1934 -- If the discrete choice raises CE get its original bounds
1936 elsif Nkind (Choice) = N_Raise_Constraint_Error then
1937 Set_Raises_Constraint_Error (N);
1938 Get_Index_Bounds (Original_Node (Choice), Low, High);
1940 -- Otherwise get its bounds as usual
1943 Get_Index_Bounds (Choice, Low, High);
1946 if (Dynamic_Or_Null_Range (Low, High)
1947 or else (Nkind (Choice) = N_Subtype_Indication
1949 Dynamic_Or_Null_Range (S_Low, S_High)))
1950 and then Nb_Choices /= 1
1953 ("dynamic or empty choice in aggregate "
1954 & "must be the only choice", Choice);
1958 if not (All_Composite_Constraints_Static (Low)
1959 and then All_Composite_Constraints_Static (High)
1960 and then All_Composite_Constraints_Static (S_Low)
1961 and then All_Composite_Constraints_Static (S_High))
1963 Check_Restriction (No_Dynamic_Sized_Objects, Choice);
1966 Nb_Discrete_Choices := Nb_Discrete_Choices + 1;
1967 Table (Nb_Discrete_Choices).Lo := Low;
1968 Table (Nb_Discrete_Choices).Hi := High;
1969 Table (Nb_Discrete_Choices).Choice := Choice;
1975 -- Check if we have a single discrete choice and whether
1976 -- this discrete choice specifies a single value.
1979 (Nb_Discrete_Choices = Prev_Nb_Discrete_Choices + 1)
1980 and then (Low = High);
1986 -- Ada 2005 (AI-231)
1988 if Ada_Version >= Ada_2005
1989 and then Known_Null (Expression (Assoc))
1990 and then not Empty_Range (Assoc)
1992 Check_Can_Never_Be_Null (Etype (N), Expression (Assoc));
1995 -- Ada 2005 (AI-287): In case of default initialized component
1996 -- we delay the resolution to the expansion phase.
1998 if Box_Present (Assoc) then
2000 -- Ada 2005 (AI-287): In case of default initialization of a
2001 -- component the expander will generate calls to the
2002 -- corresponding initialization subprogram. We need to call
2003 -- Resolve_Aggr_Expr to check the rules about
2006 if not Resolve_Aggr_Expr
2007 (Assoc, Single_Elmt => Single_Choice)
2012 elsif Nkind (Assoc) = N_Iterated_Component_Association then
2013 null; -- handled above, in a loop context.
2015 elsif not Resolve_Aggr_Expr
2016 (Expression (Assoc), Single_Elmt => Single_Choice)
2020 -- Check incorrect use of dynamically tagged expression
2022 -- We differentiate here two cases because the expression may
2023 -- not be decorated. For example, the analysis and resolution
2024 -- of the expression associated with the others choice will be
2025 -- done later with the full aggregate. In such case we
2026 -- duplicate the expression tree to analyze the copy and
2027 -- perform the required check.
2029 elsif not Present (Etype (Expression (Assoc))) then
2031 Save_Analysis : constant Boolean := Full_Analysis;
2032 Expr : constant Node_Id :=
2033 New_Copy_Tree (Expression (Assoc));
2036 Expander_Mode_Save_And_Set (False);
2037 Full_Analysis := False;
2039 -- Analyze the expression, making sure it is properly
2040 -- attached to the tree before we do the analysis.
2042 Set_Parent (Expr, Parent (Expression (Assoc)));
2045 -- Compute its dimensions now, rather than at the end of
2046 -- resolution, because in the case of multidimensional
2047 -- aggregates subsequent expansion may lead to spurious
2050 Check_Expression_Dimensions (Expr, Component_Typ);
2052 -- If the expression is a literal, propagate this info
2053 -- to the expression in the association, to enable some
2054 -- optimizations downstream.
2056 if Is_Entity_Name (Expr)
2057 and then Present (Entity (Expr))
2058 and then Ekind (Entity (Expr)) = E_Enumeration_Literal
2061 (Expression (Assoc), Component_Typ);
2064 Full_Analysis := Save_Analysis;
2065 Expander_Mode_Restore;
2067 if Is_Tagged_Type (Etype (Expr)) then
2068 Check_Dynamically_Tagged_Expression
2070 Typ => Component_Type (Etype (N)),
2075 elsif Is_Tagged_Type (Etype (Expression (Assoc))) then
2076 Check_Dynamically_Tagged_Expression
2077 (Expr => Expression (Assoc),
2078 Typ => Component_Type (Etype (N)),
2085 -- If aggregate contains more than one choice then these must be
2086 -- static. Check for duplicate and missing values.
2088 -- Note: there is duplicated code here wrt Check_Choice_Set in
2089 -- the body of Sem_Case, and it is possible we could just reuse
2090 -- that procedure. To be checked ???
2092 if Nb_Discrete_Choices > 1 then
2093 Check_Choices : declare
2095 -- Location of choice for messages
2099 -- High end of one range and Low end of the next. Should be
2100 -- contiguous if there is no hole in the list of values.
2104 -- End points of duplicated range
2106 Missing_Or_Duplicates : Boolean := False;
2107 -- Set True if missing or duplicate choices found
2109 procedure Output_Bad_Choices (Lo, Hi : Uint; C : Node_Id);
2110 -- Output continuation message with a representation of the
2111 -- bounds (just Lo if Lo = Hi, else Lo .. Hi). C is the
2112 -- choice node where the message is to be posted.
2114 ------------------------
2115 -- Output_Bad_Choices --
2116 ------------------------
2118 procedure Output_Bad_Choices (Lo, Hi : Uint; C : Node_Id) is
2120 -- Enumeration type case
2122 if Is_Enumeration_Type (Index_Typ) then
2124 Chars (Get_Enum_Lit_From_Pos (Index_Typ, Lo, Loc));
2126 Chars (Get_Enum_Lit_From_Pos (Index_Typ, Hi, Loc));
2129 Error_Msg_N ("\\ %!", C);
2131 Error_Msg_N ("\\ % .. %!", C);
2134 -- Integer types case
2137 Error_Msg_Uint_1 := Lo;
2138 Error_Msg_Uint_2 := Hi;
2141 Error_Msg_N ("\\ ^!", C);
2143 Error_Msg_N ("\\ ^ .. ^!", C);
2146 end Output_Bad_Choices;
2148 -- Start of processing for Check_Choices
2151 Sort_Case_Table (Table);
2153 -- First we do a quick linear loop to find out if we have
2154 -- any duplicates or missing entries (usually we have a
2155 -- legal aggregate, so this will get us out quickly).
2157 for J in 1 .. Nb_Discrete_Choices - 1 loop
2158 Hi_Val := Expr_Value (Table (J).Hi);
2159 Lo_Val := Expr_Value (Table (J + 1).Lo);
2162 or else (Lo_Val > Hi_Val + 1
2163 and then not Others_Present)
2165 Missing_Or_Duplicates := True;
2170 -- If we have missing or duplicate entries, first fill in
2171 -- the Highest entries to make life easier in the following
2172 -- loops to detect bad entries.
2174 if Missing_Or_Duplicates then
2175 Table (1).Highest := Expr_Value (Table (1).Hi);
2177 for J in 2 .. Nb_Discrete_Choices loop
2178 Table (J).Highest :=
2180 (Table (J - 1).Highest, Expr_Value (Table (J).Hi));
2183 -- Loop through table entries to find duplicate indexes
2185 for J in 2 .. Nb_Discrete_Choices loop
2186 Lo_Val := Expr_Value (Table (J).Lo);
2187 Hi_Val := Expr_Value (Table (J).Hi);
2189 -- Case where we have duplicates (the lower bound of
2190 -- this choice is less than or equal to the highest
2191 -- high bound found so far).
2193 if Lo_Val <= Table (J - 1).Highest then
2195 -- We move backwards looking for duplicates. We can
2196 -- abandon this loop as soon as we reach a choice
2197 -- highest value that is less than Lo_Val.
2199 for K in reverse 1 .. J - 1 loop
2200 exit when Table (K).Highest < Lo_Val;
2202 -- Here we may have duplicates between entries
2203 -- for K and J. Get range of duplicates.
2206 UI_Max (Lo_Val, Expr_Value (Table (K).Lo));
2208 UI_Min (Hi_Val, Expr_Value (Table (K).Hi));
2210 -- Nothing to do if duplicate range is null
2212 if Lo_Dup > Hi_Dup then
2215 -- Otherwise place proper message
2218 -- We place message on later choice, with a
2219 -- line reference to the earlier choice.
2221 if Sloc (Table (J).Choice) <
2222 Sloc (Table (K).Choice)
2224 Choice := Table (K).Choice;
2225 Error_Msg_Sloc := Sloc (Table (J).Choice);
2227 Choice := Table (J).Choice;
2228 Error_Msg_Sloc := Sloc (Table (K).Choice);
2231 if Lo_Dup = Hi_Dup then
2233 ("index value in array aggregate "
2234 & "duplicates the one given#!", Choice);
2237 ("index values in array aggregate "
2238 & "duplicate those given#!", Choice);
2241 Output_Bad_Choices (Lo_Dup, Hi_Dup, Choice);
2247 -- Loop through entries in table to find missing indexes.
2248 -- Not needed if others, since missing impossible.
2250 if not Others_Present then
2251 for J in 2 .. Nb_Discrete_Choices loop
2252 Lo_Val := Expr_Value (Table (J).Lo);
2253 Hi_Val := Table (J - 1).Highest;
2255 if Lo_Val > Hi_Val + 1 then
2258 Error_Node : Node_Id;
2261 -- If the choice is the bound of a range in
2262 -- a subtype indication, it is not in the
2263 -- source lists for the aggregate itself, so
2264 -- post the error on the aggregate. Otherwise
2265 -- post it on choice itself.
2267 Choice := Table (J).Choice;
2269 if Is_List_Member (Choice) then
2270 Error_Node := Choice;
2275 if Hi_Val + 1 = Lo_Val - 1 then
2277 ("missing index value "
2278 & "in array aggregate!", Error_Node);
2281 ("missing index values "
2282 & "in array aggregate!", Error_Node);
2286 (Hi_Val + 1, Lo_Val - 1, Error_Node);
2292 -- If either missing or duplicate values, return failure
2294 Set_Etype (N, Any_Composite);
2300 -- STEP 2 (B): Compute aggregate bounds and min/max choices values
2302 if Nb_Discrete_Choices > 0 then
2303 Choices_Low := Table (1).Lo;
2304 Choices_High := Table (Nb_Discrete_Choices).Hi;
2307 -- If Others is present, then bounds of aggregate come from the
2308 -- index constraint (not the choices in the aggregate itself).
2310 if Others_Present then
2311 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2313 -- Abandon processing if either bound is already signalled as
2314 -- an error (prevents junk cascaded messages and blow ups).
2316 if Nkind (Aggr_Low) = N_Error
2318 Nkind (Aggr_High) = N_Error
2323 -- No others clause present
2326 -- Special processing if others allowed and not present. This
2327 -- means that the bounds of the aggregate come from the index
2328 -- constraint (and the length must match).
2330 if Others_Allowed then
2331 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2333 -- Abandon processing if either bound is already signalled
2334 -- as an error (stop junk cascaded messages and blow ups).
2336 if Nkind (Aggr_Low) = N_Error
2338 Nkind (Aggr_High) = N_Error
2343 -- If others allowed, and no others present, then the array
2344 -- should cover all index values. If it does not, we will
2345 -- get a length check warning, but there is two cases where
2346 -- an additional warning is useful:
2348 -- If we have no positional components, and the length is
2349 -- wrong (which we can tell by others being allowed with
2350 -- missing components), and the index type is an enumeration
2351 -- type, then issue appropriate warnings about these missing
2352 -- components. They are only warnings, since the aggregate
2353 -- is fine, it's just the wrong length. We skip this check
2354 -- for standard character types (since there are no literals
2355 -- and it is too much trouble to concoct them), and also if
2356 -- any of the bounds have values that are not known at
2359 -- Another case warranting a warning is when the length
2360 -- is right, but as above we have an index type that is
2361 -- an enumeration, and the bounds do not match. This is a
2362 -- case where dubious sliding is allowed and we generate a
2363 -- warning that the bounds do not match.
2365 if No (Expressions (N))
2366 and then Nkind (Index) = N_Range
2367 and then Is_Enumeration_Type (Etype (Index))
2368 and then not Is_Standard_Character_Type (Etype (Index))
2369 and then Compile_Time_Known_Value (Aggr_Low)
2370 and then Compile_Time_Known_Value (Aggr_High)
2371 and then Compile_Time_Known_Value (Choices_Low)
2372 and then Compile_Time_Known_Value (Choices_High)
2374 -- If any of the expressions or range bounds in choices
2375 -- have semantic errors, then do not attempt further
2376 -- resolution, to prevent cascaded errors.
2378 if Errors_Posted_On_Choices then
2383 ALo : constant Node_Id := Expr_Value_E (Aggr_Low);
2384 AHi : constant Node_Id := Expr_Value_E (Aggr_High);
2385 CLo : constant Node_Id := Expr_Value_E (Choices_Low);
2386 CHi : constant Node_Id := Expr_Value_E (Choices_High);
2391 -- Warning case 1, missing values at start/end. Only
2392 -- do the check if the number of entries is too small.
2394 if (Enumeration_Pos (CHi) - Enumeration_Pos (CLo))
2396 (Enumeration_Pos (AHi) - Enumeration_Pos (ALo))
2399 ("missing index value(s) in array aggregate??",
2402 -- Output missing value(s) at start
2404 if Chars (ALo) /= Chars (CLo) then
2407 if Chars (ALo) = Chars (Ent) then
2408 Error_Msg_Name_1 := Chars (ALo);
2409 Error_Msg_N ("\ %??", N);
2411 Error_Msg_Name_1 := Chars (ALo);
2412 Error_Msg_Name_2 := Chars (Ent);
2413 Error_Msg_N ("\ % .. %??", N);
2417 -- Output missing value(s) at end
2419 if Chars (AHi) /= Chars (CHi) then
2422 if Chars (AHi) = Chars (Ent) then
2423 Error_Msg_Name_1 := Chars (Ent);
2424 Error_Msg_N ("\ %??", N);
2426 Error_Msg_Name_1 := Chars (Ent);
2427 Error_Msg_Name_2 := Chars (AHi);
2428 Error_Msg_N ("\ % .. %??", N);
2432 -- Warning case 2, dubious sliding. The First_Subtype
2433 -- test distinguishes between a constrained type where
2434 -- sliding is not allowed (so we will get a warning
2435 -- later that Constraint_Error will be raised), and
2436 -- the unconstrained case where sliding is permitted.
2438 elsif (Enumeration_Pos (CHi) - Enumeration_Pos (CLo))
2440 (Enumeration_Pos (AHi) - Enumeration_Pos (ALo))
2441 and then Chars (ALo) /= Chars (CLo)
2443 not Is_Constrained (First_Subtype (Etype (N)))
2446 ("bounds of aggregate do not match target??", N);
2452 -- If no others, aggregate bounds come from aggregate
2454 Aggr_Low := Choices_Low;
2455 Aggr_High := Choices_High;
2459 -- STEP 3: Process positional components
2462 -- STEP 3 (A): Process positional elements
2464 Expr := First (Expressions (N));
2465 Nb_Elements := Uint_0;
2466 while Present (Expr) loop
2467 Nb_Elements := Nb_Elements + 1;
2469 -- Ada 2005 (AI-231)
2471 if Ada_Version >= Ada_2005 and then Known_Null (Expr) then
2472 Check_Can_Never_Be_Null (Etype (N), Expr);
2475 if not Resolve_Aggr_Expr (Expr, Single_Elmt => True) then
2479 -- Check incorrect use of dynamically tagged expression
2481 if Is_Tagged_Type (Etype (Expr)) then
2482 Check_Dynamically_Tagged_Expression
2484 Typ => Component_Type (Etype (N)),
2491 if Others_Present then
2492 Assoc := Last (Component_Associations (N));
2494 -- Ada 2005 (AI-231)
2496 if Ada_Version >= Ada_2005 and then Known_Null (Assoc) then
2497 Check_Can_Never_Be_Null (Etype (N), Expression (Assoc));
2500 -- Ada 2005 (AI-287): In case of default initialized component,
2501 -- we delay the resolution to the expansion phase.
2503 if Box_Present (Assoc) then
2505 -- Ada 2005 (AI-287): In case of default initialization of a
2506 -- component the expander will generate calls to the
2507 -- corresponding initialization subprogram. We need to call
2508 -- Resolve_Aggr_Expr to check the rules about
2511 if not Resolve_Aggr_Expr (Assoc, Single_Elmt => False) then
2515 elsif not Resolve_Aggr_Expr (Expression (Assoc),
2516 Single_Elmt => False)
2520 -- Check incorrect use of dynamically tagged expression. The
2521 -- expression of the others choice has not been resolved yet.
2522 -- In order to diagnose the semantic error we create a duplicate
2523 -- tree to analyze it and perform the check.
2527 Save_Analysis : constant Boolean := Full_Analysis;
2528 Expr : constant Node_Id :=
2529 New_Copy_Tree (Expression (Assoc));
2532 Expander_Mode_Save_And_Set (False);
2533 Full_Analysis := False;
2535 Full_Analysis := Save_Analysis;
2536 Expander_Mode_Restore;
2538 if Is_Tagged_Type (Etype (Expr)) then
2539 Check_Dynamically_Tagged_Expression
2541 Typ => Component_Type (Etype (N)),
2548 -- STEP 3 (B): Compute the aggregate bounds
2550 if Others_Present then
2551 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2554 if Others_Allowed then
2555 Get_Index_Bounds (Index_Constr, Aggr_Low, Discard);
2557 Aggr_Low := Index_Typ_Low;
2560 Aggr_High := Add (Nb_Elements - 1, To => Aggr_Low);
2561 Check_Bound (Index_Base_High, Aggr_High);
2565 -- STEP 4: Perform static aggregate checks and save the bounds
2569 Check_Bounds (Index_Typ_Low, Index_Typ_High, Aggr_Low, Aggr_High);
2570 Check_Bounds (Index_Base_Low, Index_Base_High, Aggr_Low, Aggr_High);
2574 if Others_Present and then Nb_Discrete_Choices > 0 then
2575 Check_Bounds (Aggr_Low, Aggr_High, Choices_Low, Choices_High);
2576 Check_Bounds (Index_Typ_Low, Index_Typ_High,
2577 Choices_Low, Choices_High);
2578 Check_Bounds (Index_Base_Low, Index_Base_High,
2579 Choices_Low, Choices_High);
2583 elsif Others_Present and then Nb_Elements > 0 then
2584 Check_Length (Aggr_Low, Aggr_High, Nb_Elements);
2585 Check_Length (Index_Typ_Low, Index_Typ_High, Nb_Elements);
2586 Check_Length (Index_Base_Low, Index_Base_High, Nb_Elements);
2589 if Raises_Constraint_Error (Aggr_Low)
2590 or else Raises_Constraint_Error (Aggr_High)
2592 Set_Raises_Constraint_Error (N);
2595 Aggr_Low := Duplicate_Subexpr (Aggr_Low);
2597 -- Do not duplicate Aggr_High if Aggr_High = Aggr_Low + Nb_Elements
2598 -- since the addition node returned by Add is not yet analyzed. Attach
2599 -- to tree and analyze first. Reset analyzed flag to ensure it will get
2600 -- analyzed when it is a literal bound whose type must be properly set.
2602 if Others_Present or else Nb_Discrete_Choices > 0 then
2603 Aggr_High := Duplicate_Subexpr (Aggr_High);
2605 if Etype (Aggr_High) = Universal_Integer then
2606 Set_Analyzed (Aggr_High, False);
2610 -- If the aggregate already has bounds attached to it, it means this is
2611 -- a positional aggregate created as an optimization by
2612 -- Exp_Aggr.Convert_To_Positional, so we don't want to change those
2615 if Present (Aggregate_Bounds (N)) and then not Others_Allowed then
2616 Aggr_Low := Low_Bound (Aggregate_Bounds (N));
2617 Aggr_High := High_Bound (Aggregate_Bounds (N));
2620 Set_Aggregate_Bounds
2621 (N, Make_Range (Loc, Low_Bound => Aggr_Low, High_Bound => Aggr_High));
2623 -- The bounds may contain expressions that must be inserted upwards.
2624 -- Attach them fully to the tree. After analysis, remove side effects
2625 -- from upper bound, if still needed.
2627 Set_Parent (Aggregate_Bounds (N), N);
2628 Analyze_And_Resolve (Aggregate_Bounds (N), Index_Typ);
2629 Check_Unset_Reference (Aggregate_Bounds (N));
2631 if not Others_Present and then Nb_Discrete_Choices = 0 then
2633 (Aggregate_Bounds (N),
2634 Duplicate_Subexpr (High_Bound (Aggregate_Bounds (N))));
2637 -- Check the dimensions of each component in the array aggregate
2639 Analyze_Dimension_Array_Aggregate (N, Component_Typ);
2642 end Resolve_Array_Aggregate;
2644 ---------------------------------
2645 -- Resolve_Container_Aggregate --
2646 ---------------------------------
2648 procedure Resolve_Container_Aggregate (N : Node_Id; Typ : Entity_Id) is
2649 procedure Resolve_Iterated_Component_Association
2651 Key_Type : Entity_Id;
2652 Elmt_Type : Entity_Id);
2653 -- Resolve choices and expression in an iterated component association.
2654 -- This is similar but not identical to the handling of this construct
2655 -- in an array aggregate.
2656 -- For a named container, the type of each choice must be compatible
2657 -- with the key type. For a positional container, the choice must be
2658 -- a subtype indication or an iterator specification that determines
2661 Asp : constant Node_Id := Find_Value_Of_Aspect (Typ, Aspect_Aggregate);
2663 Empty_Subp : Node_Id := Empty;
2664 Add_Named_Subp : Node_Id := Empty;
2665 Add_Unnamed_Subp : Node_Id := Empty;
2666 New_Indexed_Subp : Node_Id := Empty;
2667 Assign_Indexed_Subp : Node_Id := Empty;
2669 --------------------------------------------
2670 -- Resolve_Iterated_Component_Association --
2671 --------------------------------------------
2673 procedure Resolve_Iterated_Component_Association
2675 Key_Type : Entity_Id;
2676 Elmt_Type : Entity_Id)
2683 Typ : Entity_Id := Empty;
2686 if Present (Iterator_Specification (Comp)) then
2687 Iter := Copy_Separate_Tree (Iterator_Specification (Comp));
2689 Typ := Etype (Defining_Identifier (Iter));
2692 Choice := First (Discrete_Choices (Comp));
2694 while Present (Choice) loop
2697 -- Choice can be a subtype name, a range, or an expression
2699 if Is_Entity_Name (Choice)
2700 and then Is_Type (Entity (Choice))
2701 and then Base_Type (Entity (Choice)) = Base_Type (Key_Type)
2705 elsif Present (Key_Type) then
2706 Analyze_And_Resolve (Choice, Key_Type);
2709 Typ := Etype (Choice); -- assume unique for now
2716 -- Create a scope in which to introduce an index, which is usually
2717 -- visible in the expression for the component, and needed for its
2720 Ent := New_Internal_Entity (E_Loop, Current_Scope, Sloc (Comp), 'L');
2721 Set_Etype (Ent, Standard_Void_Type);
2722 Set_Parent (Ent, Parent (Comp));
2725 Make_Defining_Identifier (Sloc (Comp),
2726 Chars => Chars (Defining_Identifier (Comp)));
2728 -- Insert and decorate the loop variable in the current scope.
2729 -- The expression has to be analyzed once the loop variable is
2730 -- directly visible. Mark the variable as referenced to prevent
2731 -- spurious warnings, given that subsequent uses of its name in the
2732 -- expression will reference the internal (synonym) loop variable.
2736 if No (Key_Type) then
2737 pragma Assert (Present (Typ));
2738 Set_Etype (Id, Typ);
2740 Set_Etype (Id, Key_Type);
2743 Set_Ekind (Id, E_Variable);
2744 Set_Scope (Id, Ent);
2745 Set_Referenced (Id);
2747 -- Analyze a copy of the expression, to verify legality. We use
2748 -- a copy because the expression will be analyzed anew when the
2749 -- enclosing aggregate is expanded, and the construct is rewritten
2750 -- as a loop with a new index variable.
2752 Expr := New_Copy_Tree (Expression (Comp));
2753 Preanalyze_And_Resolve (Expr, Elmt_Type);
2755 end Resolve_Iterated_Component_Association;
2758 pragma Assert (Nkind (Asp) = N_Aggregate);
2761 Parse_Aspect_Aggregate (Asp,
2762 Empty_Subp, Add_Named_Subp, Add_Unnamed_Subp,
2763 New_Indexed_Subp, Assign_Indexed_Subp);
2765 if Present (Add_Unnamed_Subp)
2766 and then No (New_Indexed_Subp)
2769 Elmt_Type : constant Entity_Id :=
2771 (First_Formal (Entity (Add_Unnamed_Subp))));
2775 if Present (Expressions (N)) then
2776 -- positional aggregate
2778 Comp := First (Expressions (N));
2779 while Present (Comp) loop
2780 Analyze_And_Resolve (Comp, Elmt_Type);
2785 -- Empty aggregate, to be replaced by Empty during
2786 -- expansion, or iterated component association.
2788 if Present (Component_Associations (N)) then
2790 Comp : Node_Id := First (Component_Associations (N));
2792 while Present (Comp) loop
2794 N_Iterated_Component_Association
2796 Error_Msg_N ("illegal component association "
2797 & "for unnamed container aggregate", Comp);
2800 Resolve_Iterated_Component_Association
2801 (Comp, Empty, Elmt_Type);
2810 elsif Present (Add_Named_Subp) then
2812 -- Retrieves types of container, key, and element from the
2813 -- specified insertion procedure.
2815 Container : constant Entity_Id :=
2816 First_Formal (Entity (Add_Named_Subp));
2817 Key_Type : constant Entity_Id := Etype (Next_Formal (Container));
2818 Elmt_Type : constant Entity_Id :=
2819 Etype (Next_Formal (Next_Formal (Container)));
2824 Comp := First (Component_Associations (N));
2825 while Present (Comp) loop
2826 if Nkind (Comp) = N_Component_Association then
2827 Choice := First (Choices (Comp));
2829 while Present (Choice) loop
2830 Analyze_And_Resolve (Choice, Key_Type);
2831 if not Is_Static_Expression (Choice) then
2832 Error_Msg_N ("Choice must be static", Choice);
2838 Analyze_And_Resolve (Expression (Comp), Elmt_Type);
2840 elsif Nkind (Comp) = N_Iterated_Component_Association then
2841 Resolve_Iterated_Component_Association
2842 (Comp, Key_Type, Elmt_Type);
2850 -- Indexed Aggregate. Both positional and indexed component
2851 -- can be present. Choices must be static values or ranges
2852 -- with static bounds.
2855 Container : constant Entity_Id :=
2856 First_Formal (Entity (Assign_Indexed_Subp));
2857 Index_Type : constant Entity_Id := Etype (Next_Formal (Container));
2858 Comp_Type : constant Entity_Id :=
2859 Etype (Next_Formal (Next_Formal (Container)));
2864 if Present (Expressions (N)) then
2865 Comp := First (Expressions (N));
2866 while Present (Comp) loop
2867 Analyze_And_Resolve (Comp, Comp_Type);
2872 if Present (Component_Associations (N)) then
2873 Comp := First (Expressions (N));
2875 while Present (Comp) loop
2876 if Nkind (Comp) = N_Component_Association then
2877 Choice := First (Choices (Comp));
2879 while Present (Choice) loop
2880 Analyze_And_Resolve (Choice, Index_Type);
2884 Analyze_And_Resolve (Expression (Comp), Comp_Type);
2886 elsif Nkind (Comp) = N_Iterated_Component_Association then
2887 Resolve_Iterated_Component_Association
2888 (Comp, Index_Type, Comp_Type);
2896 end Resolve_Container_Aggregate;
2898 -----------------------------
2899 -- Resolve_Delta_Aggregate --
2900 -----------------------------
2902 procedure Resolve_Delta_Aggregate (N : Node_Id; Typ : Entity_Id) is
2903 Base : constant Node_Id := Expression (N);
2906 if Ada_Version < Ada_2020 then
2907 Error_Msg_N ("delta_aggregate is an Ada 202x feature", N);
2908 Error_Msg_N ("\compile with -gnat2020", N);
2911 if not Is_Composite_Type (Typ) then
2912 Error_Msg_N ("not a composite type", N);
2915 Analyze_And_Resolve (Base, Typ);
2917 if Is_Array_Type (Typ) then
2918 Resolve_Delta_Array_Aggregate (N, Typ);
2920 Resolve_Delta_Record_Aggregate (N, Typ);
2924 end Resolve_Delta_Aggregate;
2926 -----------------------------------
2927 -- Resolve_Delta_Array_Aggregate --
2928 -----------------------------------
2930 procedure Resolve_Delta_Array_Aggregate (N : Node_Id; Typ : Entity_Id) is
2931 Deltas : constant List_Id := Component_Associations (N);
2932 Index_Type : constant Entity_Id := Etype (First_Index (Typ));
2938 Assoc := First (Deltas);
2939 while Present (Assoc) loop
2940 if Nkind (Assoc) = N_Iterated_Component_Association then
2941 Choice := First (Choice_List (Assoc));
2942 while Present (Choice) loop
2943 if Nkind (Choice) = N_Others_Choice then
2945 ("others not allowed in delta aggregate", Choice);
2948 Analyze_And_Resolve (Choice, Index_Type);
2955 Id : constant Entity_Id := Defining_Identifier (Assoc);
2956 Ent : constant Entity_Id :=
2958 (E_Loop, Current_Scope, Sloc (Assoc), 'L');
2961 Set_Etype (Ent, Standard_Void_Type);
2962 Set_Parent (Ent, Assoc);
2964 if No (Scope (Id)) then
2966 Set_Etype (Id, Index_Type);
2967 Set_Ekind (Id, E_Variable);
2968 Set_Scope (Id, Ent);
2973 (New_Copy_Tree (Expression (Assoc)), Component_Type (Typ));
2978 Choice := First (Choice_List (Assoc));
2979 while Present (Choice) loop
2980 if Nkind (Choice) = N_Others_Choice then
2982 ("others not allowed in delta aggregate", Choice);
2987 if Is_Entity_Name (Choice)
2988 and then Is_Type (Entity (Choice))
2990 -- Choice covers a range of values
2992 if Base_Type (Entity (Choice)) /=
2993 Base_Type (Index_Type)
2996 ("choice does mat match index type of",
3000 Resolve (Choice, Index_Type);
3007 Analyze_And_Resolve (Expression (Assoc), Component_Type (Typ));
3012 end Resolve_Delta_Array_Aggregate;
3014 ------------------------------------
3015 -- Resolve_Delta_Record_Aggregate --
3016 ------------------------------------
3018 procedure Resolve_Delta_Record_Aggregate (N : Node_Id; Typ : Entity_Id) is
3020 -- Variables used to verify that discriminant-dependent components
3021 -- appear in the same variant.
3023 Comp_Ref : Entity_Id := Empty; -- init to avoid warning
3026 procedure Check_Variant (Id : Entity_Id);
3027 -- If a given component of the delta aggregate appears in a variant
3028 -- part, verify that it is within the same variant as that of previous
3029 -- specified variant components of the delta.
3031 function Get_Component (Nam : Node_Id) return Entity_Id;
3032 -- Locate component with a given name and return it. If none found then
3033 -- report error and return Empty.
3035 function Nested_In (V1 : Node_Id; V2 : Node_Id) return Boolean;
3036 -- Determine whether variant V1 is within variant V2
3038 function Variant_Depth (N : Node_Id) return Integer;
3039 -- Determine the distance of a variant to the enclosing type
3042 --------------------
3044 --------------------
3046 procedure Check_Variant (Id : Entity_Id) is
3048 Comp_Variant : Node_Id;
3051 if not Has_Discriminants (Typ) then
3055 Comp := First_Entity (Typ);
3056 while Present (Comp) loop
3057 exit when Chars (Comp) = Chars (Id);
3058 Next_Component (Comp);
3061 -- Find the variant, if any, whose component list includes the
3062 -- component declaration.
3064 Comp_Variant := Parent (Parent (List_Containing (Parent (Comp))));
3065 if Nkind (Comp_Variant) = N_Variant then
3066 if No (Variant) then
3067 Variant := Comp_Variant;
3070 elsif Variant /= Comp_Variant then
3072 D1 : constant Integer := Variant_Depth (Variant);
3073 D2 : constant Integer := Variant_Depth (Comp_Variant);
3078 (D1 > D2 and then not Nested_In (Variant, Comp_Variant))
3080 (D2 > D1 and then not Nested_In (Comp_Variant, Variant))
3082 pragma Assert (Present (Comp_Ref));
3083 Error_Msg_Node_2 := Comp_Ref;
3085 ("& and & appear in different variants", Id, Comp);
3087 -- Otherwise retain the deeper variant for subsequent tests
3090 Variant := Comp_Variant;
3101 function Get_Component (Nam : Node_Id) return Entity_Id is
3105 Comp := First_Entity (Typ);
3106 while Present (Comp) loop
3107 if Chars (Comp) = Chars (Nam) then
3108 if Ekind (Comp) = E_Discriminant then
3109 Error_Msg_N ("delta cannot apply to discriminant", Nam);
3118 Error_Msg_NE ("type& has no component with this name", Nam, Typ);
3126 function Nested_In (V1, V2 : Node_Id) return Boolean is
3131 while Nkind (Par) /= N_Full_Type_Declaration loop
3136 Par := Parent (Par);
3146 function Variant_Depth (N : Node_Id) return Integer is
3153 while Nkind (Par) /= N_Full_Type_Declaration loop
3155 Par := Parent (Par);
3163 Deltas : constant List_Id := Component_Associations (N);
3168 Comp_Type : Entity_Id := Empty; -- init to avoid warning
3170 -- Start of processing for Resolve_Delta_Record_Aggregate
3175 Assoc := First (Deltas);
3176 while Present (Assoc) loop
3177 Choice := First (Choice_List (Assoc));
3178 while Present (Choice) loop
3179 Comp := Get_Component (Choice);
3181 if Present (Comp) then
3182 Check_Variant (Choice);
3184 Comp_Type := Etype (Comp);
3186 -- Decorate the component reference by setting its entity and
3187 -- type, as otherwise backends like GNATprove would have to
3188 -- rediscover this information by themselves.
3190 Set_Entity (Choice, Comp);
3191 Set_Etype (Choice, Comp_Type);
3193 Comp_Type := Any_Type;
3199 pragma Assert (Present (Comp_Type));
3200 Analyze_And_Resolve (Expression (Assoc), Comp_Type);
3203 end Resolve_Delta_Record_Aggregate;
3205 ---------------------------------
3206 -- Resolve_Extension_Aggregate --
3207 ---------------------------------
3209 -- There are two cases to consider:
3211 -- a) If the ancestor part is a type mark, the components needed are the
3212 -- difference between the components of the expected type and the
3213 -- components of the given type mark.
3215 -- b) If the ancestor part is an expression, it must be unambiguous, and
3216 -- once we have its type we can also compute the needed components as in
3217 -- the previous case. In both cases, if the ancestor type is not the
3218 -- immediate ancestor, we have to build this ancestor recursively.
3220 -- In both cases, discriminants of the ancestor type do not play a role in
3221 -- the resolution of the needed components, because inherited discriminants
3222 -- cannot be used in a type extension. As a result we can compute
3223 -- independently the list of components of the ancestor type and of the
3226 procedure Resolve_Extension_Aggregate (N : Node_Id; Typ : Entity_Id) is
3227 A : constant Node_Id := Ancestor_Part (N);
3232 function Valid_Limited_Ancestor (Anc : Node_Id) return Boolean;
3233 -- If the type is limited, verify that the ancestor part is a legal
3234 -- expression (aggregate or function call, including 'Input)) that does
3235 -- not require a copy, as specified in 7.5(2).
3237 function Valid_Ancestor_Type return Boolean;
3238 -- Verify that the type of the ancestor part is a non-private ancestor
3239 -- of the expected type, which must be a type extension.
3241 procedure Transform_BIP_Assignment (Typ : Entity_Id);
3242 -- For an extension aggregate whose ancestor part is a build-in-place
3243 -- call returning a nonlimited type, this is used to transform the
3244 -- assignment to the ancestor part to use a temp.
3246 ----------------------------
3247 -- Valid_Limited_Ancestor --
3248 ----------------------------
3250 function Valid_Limited_Ancestor (Anc : Node_Id) return Boolean is
3252 if Is_Entity_Name (Anc) and then Is_Type (Entity (Anc)) then
3255 -- The ancestor must be a call or an aggregate, but a call may
3256 -- have been expanded into a temporary, so check original node.
3258 elsif Nkind (Anc) in N_Aggregate
3259 | N_Extension_Aggregate
3264 elsif Nkind (Original_Node (Anc)) = N_Function_Call then
3267 elsif Nkind (Anc) = N_Attribute_Reference
3268 and then Attribute_Name (Anc) = Name_Input
3272 elsif Nkind (Anc) = N_Qualified_Expression then
3273 return Valid_Limited_Ancestor (Expression (Anc));
3275 elsif Nkind (Anc) = N_Raise_Expression then
3281 end Valid_Limited_Ancestor;
3283 -------------------------
3284 -- Valid_Ancestor_Type --
3285 -------------------------
3287 function Valid_Ancestor_Type return Boolean is
3288 Imm_Type : Entity_Id;
3291 Imm_Type := Base_Type (Typ);
3292 while Is_Derived_Type (Imm_Type) loop
3293 if Etype (Imm_Type) = Base_Type (A_Type) then
3296 -- The base type of the parent type may appear as a private
3297 -- extension if it is declared as such in a parent unit of the
3298 -- current one. For consistency of the subsequent analysis use
3299 -- the partial view for the ancestor part.
3301 elsif Is_Private_Type (Etype (Imm_Type))
3302 and then Present (Full_View (Etype (Imm_Type)))
3303 and then Base_Type (A_Type) = Full_View (Etype (Imm_Type))
3305 A_Type := Etype (Imm_Type);
3308 -- The parent type may be a private extension. The aggregate is
3309 -- legal if the type of the aggregate is an extension of it that
3310 -- is not a private extension.
3312 elsif Is_Private_Type (A_Type)
3313 and then not Is_Private_Type (Imm_Type)
3314 and then Present (Full_View (A_Type))
3315 and then Base_Type (Full_View (A_Type)) = Etype (Imm_Type)
3319 -- The parent type may be a raise expression (which is legal in
3320 -- any expression context).
3322 elsif A_Type = Raise_Type then
3323 A_Type := Etype (Imm_Type);
3327 Imm_Type := Etype (Base_Type (Imm_Type));
3331 -- If previous loop did not find a proper ancestor, report error
3333 Error_Msg_NE ("expect ancestor type of &", A, Typ);
3335 end Valid_Ancestor_Type;
3337 ------------------------------
3338 -- Transform_BIP_Assignment --
3339 ------------------------------
3341 procedure Transform_BIP_Assignment (Typ : Entity_Id) is
3342 Loc : constant Source_Ptr := Sloc (N);
3343 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'Y', A);
3344 Obj_Decl : constant Node_Id :=
3345 Make_Object_Declaration (Loc,
3346 Defining_Identifier => Def_Id,
3347 Constant_Present => True,
3348 Object_Definition => New_Occurrence_Of (Typ, Loc),
3350 Has_Init_Expression => True);
3352 Set_Etype (Def_Id, Typ);
3353 Set_Ancestor_Part (N, New_Occurrence_Of (Def_Id, Loc));
3354 Insert_Action (N, Obj_Decl);
3355 end Transform_BIP_Assignment;
3357 -- Start of processing for Resolve_Extension_Aggregate
3360 -- Analyze the ancestor part and account for the case where it is a
3361 -- parameterless function call.
3364 Check_Parameterless_Call (A);
3366 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
3368 -- AI05-0115: if the ancestor part is a subtype mark, the ancestor
3369 -- must not have unknown discriminants.
3371 if Has_Unknown_Discriminants (Entity (A)) then
3373 ("aggregate not available for type& whose ancestor "
3374 & "has unknown discriminants", N, Typ);
3378 if not Is_Tagged_Type (Typ) then
3379 Error_Msg_N ("type of extension aggregate must be tagged", N);
3382 elsif Is_Limited_Type (Typ) then
3384 -- Ada 2005 (AI-287): Limited aggregates are allowed
3386 if Ada_Version < Ada_2005 then
3387 Error_Msg_N ("aggregate type cannot be limited", N);
3388 Explain_Limited_Type (Typ, N);
3391 elsif Valid_Limited_Ancestor (A) then
3396 ("limited ancestor part must be aggregate or function call", A);
3399 elsif Is_Class_Wide_Type (Typ) then
3400 Error_Msg_N ("aggregate cannot be of a class-wide type", N);
3404 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
3405 A_Type := Get_Full_View (Entity (A));
3407 if Valid_Ancestor_Type then
3408 Set_Entity (A, A_Type);
3409 Set_Etype (A, A_Type);
3411 Validate_Ancestor_Part (N);
3412 Resolve_Record_Aggregate (N, Typ);
3415 elsif Nkind (A) /= N_Aggregate then
3416 if Is_Overloaded (A) then
3419 Get_First_Interp (A, I, It);
3420 while Present (It.Typ) loop
3422 -- Consider limited interpretations if Ada 2005 or higher
3424 if Is_Tagged_Type (It.Typ)
3425 and then (Ada_Version >= Ada_2005
3426 or else not Is_Limited_Type (It.Typ))
3428 if A_Type /= Any_Type then
3429 Error_Msg_N ("cannot resolve expression", A);
3436 Get_Next_Interp (I, It);
3439 if A_Type = Any_Type then
3440 if Ada_Version >= Ada_2005 then
3442 ("ancestor part must be of a tagged type", A);
3445 ("ancestor part must be of a nonlimited tagged type", A);
3452 A_Type := Etype (A);
3455 if Valid_Ancestor_Type then
3456 Resolve (A, A_Type);
3457 Check_Unset_Reference (A);
3458 Check_Non_Static_Context (A);
3460 -- The aggregate is illegal if the ancestor expression is a call
3461 -- to a function with a limited unconstrained result, unless the
3462 -- type of the aggregate is a null extension. This restriction
3463 -- was added in AI05-67 to simplify implementation.
3465 if Nkind (A) = N_Function_Call
3466 and then Is_Limited_Type (A_Type)
3467 and then not Is_Null_Extension (Typ)
3468 and then not Is_Constrained (A_Type)
3471 ("type of limited ancestor part must be constrained", A);
3473 -- Reject the use of CPP constructors that leave objects partially
3474 -- initialized. For example:
3476 -- type CPP_Root is tagged limited record ...
3477 -- pragma Import (CPP, CPP_Root);
3479 -- type CPP_DT is new CPP_Root and Iface ...
3480 -- pragma Import (CPP, CPP_DT);
3482 -- type Ada_DT is new CPP_DT with ...
3484 -- Obj : Ada_DT := Ada_DT'(New_CPP_Root with others => <>);
3486 -- Using the constructor of CPP_Root the slots of the dispatch
3487 -- table of CPP_DT cannot be set, and the secondary tag of
3488 -- CPP_DT is unknown.
3490 elsif Nkind (A) = N_Function_Call
3491 and then Is_CPP_Constructor_Call (A)
3492 and then Enclosing_CPP_Parent (Typ) /= A_Type
3495 ("??must use 'C'P'P constructor for type &", A,
3496 Enclosing_CPP_Parent (Typ));
3498 -- The following call is not needed if the previous warning
3499 -- is promoted to an error.
3501 Resolve_Record_Aggregate (N, Typ);
3503 elsif Is_Class_Wide_Type (Etype (A))
3504 and then Nkind (Original_Node (A)) = N_Function_Call
3506 -- If the ancestor part is a dispatching call, it appears
3507 -- statically to be a legal ancestor, but it yields any member
3508 -- of the class, and it is not possible to determine whether
3509 -- it is an ancestor of the extension aggregate (much less
3510 -- which ancestor). It is not possible to determine the
3511 -- components of the extension part.
3513 -- This check implements AI-306, which in fact was motivated by
3514 -- an AdaCore query to the ARG after this test was added.
3516 Error_Msg_N ("ancestor part must be statically tagged", A);
3518 -- We are using the build-in-place protocol, but we can't build
3519 -- in place, because we need to call the function before
3520 -- allocating the aggregate. Could do better for null
3521 -- extensions, and maybe for nondiscriminated types.
3522 -- This is wrong for limited, but those were wrong already.
3524 if not Is_Limited_View (A_Type)
3525 and then Is_Build_In_Place_Function_Call (A)
3527 Transform_BIP_Assignment (A_Type);
3530 Resolve_Record_Aggregate (N, Typ);
3535 Error_Msg_N ("no unique type for this aggregate", A);
3538 Check_Function_Writable_Actuals (N);
3539 end Resolve_Extension_Aggregate;
3541 ------------------------------
3542 -- Resolve_Record_Aggregate --
3543 ------------------------------
3545 procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id) is
3546 New_Assoc_List : constant List_Id := New_List;
3547 -- New_Assoc_List is the newly built list of N_Component_Association
3550 Others_Etype : Entity_Id := Empty;
3551 -- This variable is used to save the Etype of the last record component
3552 -- that takes its value from the others choice. Its purpose is:
3554 -- (a) make sure the others choice is useful
3556 -- (b) make sure the type of all the components whose value is
3557 -- subsumed by the others choice are the same.
3559 -- This variable is updated as a side effect of function Get_Value.
3561 Box_Node : Node_Id := Empty;
3562 Is_Box_Present : Boolean := False;
3563 Others_Box : Natural := 0;
3564 -- Ada 2005 (AI-287): Variables used in case of default initialization
3565 -- to provide a functionality similar to Others_Etype. Box_Present
3566 -- indicates that the component takes its default initialization;
3567 -- Others_Box counts the number of components of the current aggregate
3568 -- (which may be a sub-aggregate of a larger one) that are default-
3569 -- initialized. A value of One indicates that an others_box is present.
3570 -- Any larger value indicates that the others_box is not redundant.
3571 -- These variables, similar to Others_Etype, are also updated as a side
3572 -- effect of function Get_Value. Box_Node is used to place a warning on
3573 -- a redundant others_box.
3575 procedure Add_Association
3576 (Component : Entity_Id;
3578 Assoc_List : List_Id;
3579 Is_Box_Present : Boolean := False);
3580 -- Builds a new N_Component_Association node which associates Component
3581 -- to expression Expr and adds it to the association list being built,
3582 -- either New_Assoc_List, or the association being built for an inner
3585 procedure Add_Discriminant_Values
3586 (New_Aggr : Node_Id;
3587 Assoc_List : List_Id);
3588 -- The constraint to a component may be given by a discriminant of the
3589 -- enclosing type, in which case we have to retrieve its value, which is
3590 -- part of the enclosing aggregate. Assoc_List provides the discriminant
3591 -- associations of the current type or of some enclosing record.
3593 function Discriminant_Present (Input_Discr : Entity_Id) return Boolean;
3594 -- If aggregate N is a regular aggregate this routine will return True.
3595 -- Otherwise, if N is an extension aggregate, then Input_Discr denotes
3596 -- a discriminant whose value may already have been specified by N's
3597 -- ancestor part. This routine checks whether this is indeed the case
3598 -- and if so returns False, signaling that no value for Input_Discr
3599 -- should appear in N's aggregate part. Also, in this case, the routine
3600 -- appends to New_Assoc_List the discriminant value specified in the
3603 -- If the aggregate is in a context with expansion delayed, it will be
3604 -- reanalyzed. The inherited discriminant values must not be reinserted
3605 -- in the component list to prevent spurious errors, but they must be
3606 -- present on first analysis to build the proper subtype indications.
3607 -- The flag Inherited_Discriminant is used to prevent the re-insertion.
3609 function Find_Private_Ancestor (Typ : Entity_Id) return Entity_Id;
3610 -- AI05-0115: Find earlier ancestor in the derivation chain that is
3611 -- derived from private view Typ. Whether the aggregate is legal depends
3612 -- on the current visibility of the type as well as that of the parent
3616 (Compon : Entity_Id;
3618 Consider_Others_Choice : Boolean := False) return Node_Id;
3619 -- Given a record component stored in parameter Compon, this function
3620 -- returns its value as it appears in the list From, which is a list
3621 -- of N_Component_Association nodes.
3623 -- If no component association has a choice for the searched component,
3624 -- the value provided by the others choice is returned, if there is one,
3625 -- and Consider_Others_Choice is set to true. Otherwise Empty is
3626 -- returned. If there is more than one component association giving a
3627 -- value for the searched record component, an error message is emitted
3628 -- and the first found value is returned.
3630 -- If Consider_Others_Choice is set and the returned expression comes
3631 -- from the others choice, then Others_Etype is set as a side effect.
3632 -- An error message is emitted if the components taking their value from
3633 -- the others choice do not have same type.
3635 procedure Propagate_Discriminants
3637 Assoc_List : List_Id);
3638 -- Nested components may themselves be discriminated types constrained
3639 -- by outer discriminants, whose values must be captured before the
3640 -- aggregate is expanded into assignments.
3642 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Entity_Id);
3643 -- Analyzes and resolves expression Expr against the Etype of the
3644 -- Component. This routine also applies all appropriate checks to Expr.
3645 -- It finally saves a Expr in the newly created association list that
3646 -- will be attached to the final record aggregate. Note that if the
3647 -- Parent pointer of Expr is not set then Expr was produced with a
3648 -- New_Copy_Tree or some such.
3650 procedure Rewrite_Range (Root_Type : Entity_Id; Rge : Node_Id);
3651 -- Rewrite a range node Rge when its bounds refer to non-stored
3652 -- discriminants from Root_Type, to replace them with the stored
3653 -- discriminant values. This is required in GNATprove mode, and is
3654 -- adopted in all modes to avoid special-casing GNATprove mode.
3656 ---------------------
3657 -- Add_Association --
3658 ---------------------
3660 procedure Add_Association
3661 (Component : Entity_Id;
3663 Assoc_List : List_Id;
3664 Is_Box_Present : Boolean := False)
3666 Choice_List : constant List_Id := New_List;
3670 -- If this is a box association the expression is missing, so use the
3671 -- Sloc of the aggregate itself for the new association.
3673 pragma Assert (Present (Expr) xor Is_Box_Present);
3675 if Present (Expr) then
3681 Append_To (Choice_List, New_Occurrence_Of (Component, Loc));
3683 Append_To (Assoc_List,
3684 Make_Component_Association (Loc,
3685 Choices => Choice_List,
3687 Box_Present => Is_Box_Present));
3688 end Add_Association;
3690 -----------------------------
3691 -- Add_Discriminant_Values --
3692 -----------------------------
3694 procedure Add_Discriminant_Values
3695 (New_Aggr : Node_Id;
3696 Assoc_List : List_Id)
3700 Discr_Elmt : Elmt_Id;
3701 Discr_Val : Node_Id;
3705 Discr := First_Discriminant (Etype (New_Aggr));
3706 Discr_Elmt := First_Elmt (Discriminant_Constraint (Etype (New_Aggr)));
3707 while Present (Discr_Elmt) loop
3708 Discr_Val := Node (Discr_Elmt);
3710 -- If the constraint is given by a discriminant then it is a
3711 -- discriminant of an enclosing record, and its value has already
3712 -- been placed in the association list.
3714 if Is_Entity_Name (Discr_Val)
3715 and then Ekind (Entity (Discr_Val)) = E_Discriminant
3717 Val := Entity (Discr_Val);
3719 Assoc := First (Assoc_List);
3720 while Present (Assoc) loop
3721 if Present (Entity (First (Choices (Assoc))))
3722 and then Entity (First (Choices (Assoc))) = Val
3724 Discr_Val := Expression (Assoc);
3733 (Discr, New_Copy_Tree (Discr_Val),
3734 Component_Associations (New_Aggr));
3736 -- If the discriminant constraint is a current instance, mark the
3737 -- current aggregate so that the self-reference can be expanded
3738 -- later. The constraint may refer to the subtype of aggregate, so
3739 -- use base type for comparison.
3741 if Nkind (Discr_Val) = N_Attribute_Reference
3742 and then Is_Entity_Name (Prefix (Discr_Val))
3743 and then Is_Type (Entity (Prefix (Discr_Val)))
3744 and then Base_Type (Etype (N)) = Entity (Prefix (Discr_Val))
3746 Set_Has_Self_Reference (N);
3749 Next_Elmt (Discr_Elmt);
3750 Next_Discriminant (Discr);
3752 end Add_Discriminant_Values;
3754 --------------------------
3755 -- Discriminant_Present --
3756 --------------------------
3758 function Discriminant_Present (Input_Discr : Entity_Id) return Boolean is
3759 Regular_Aggr : constant Boolean := Nkind (N) /= N_Extension_Aggregate;
3761 Ancestor_Is_Subtyp : Boolean;
3766 Ancestor_Typ : Entity_Id;
3767 Comp_Assoc : Node_Id;
3769 Discr_Expr : Node_Id;
3770 Discr_Val : Elmt_Id := No_Elmt;
3771 Orig_Discr : Entity_Id;
3774 if Regular_Aggr then
3778 -- Check whether inherited discriminant values have already been
3779 -- inserted in the aggregate. This will be the case if we are
3780 -- re-analyzing an aggregate whose expansion was delayed.
3782 if Present (Component_Associations (N)) then
3783 Comp_Assoc := First (Component_Associations (N));
3784 while Present (Comp_Assoc) loop
3785 if Inherited_Discriminant (Comp_Assoc) then
3793 Ancestor := Ancestor_Part (N);
3794 Ancestor_Typ := Etype (Ancestor);
3795 Loc := Sloc (Ancestor);
3797 -- For a private type with unknown discriminants, use the underlying
3798 -- record view if it is available.
3800 if Has_Unknown_Discriminants (Ancestor_Typ)
3801 and then Present (Full_View (Ancestor_Typ))
3802 and then Present (Underlying_Record_View (Full_View (Ancestor_Typ)))
3804 Ancestor_Typ := Underlying_Record_View (Full_View (Ancestor_Typ));
3807 Ancestor_Is_Subtyp :=
3808 Is_Entity_Name (Ancestor) and then Is_Type (Entity (Ancestor));
3810 -- If the ancestor part has no discriminants clearly N's aggregate
3811 -- part must provide a value for Discr.
3813 if not Has_Discriminants (Ancestor_Typ) then
3816 -- If the ancestor part is an unconstrained subtype mark then the
3817 -- Discr must be present in N's aggregate part.
3819 elsif Ancestor_Is_Subtyp
3820 and then not Is_Constrained (Entity (Ancestor))
3825 -- Now look to see if Discr was specified in the ancestor part
3827 if Ancestor_Is_Subtyp then
3829 First_Elmt (Discriminant_Constraint (Entity (Ancestor)));
3832 Orig_Discr := Original_Record_Component (Input_Discr);
3834 Discr := First_Discriminant (Ancestor_Typ);
3835 while Present (Discr) loop
3837 -- If Ancestor has already specified Disc value then insert its
3838 -- value in the final aggregate.
3840 if Original_Record_Component (Discr) = Orig_Discr then
3841 if Ancestor_Is_Subtyp then
3842 Discr_Expr := New_Copy_Tree (Node (Discr_Val));
3845 Make_Selected_Component (Loc,
3846 Prefix => Duplicate_Subexpr (Ancestor),
3847 Selector_Name => New_Occurrence_Of (Input_Discr, Loc));
3850 Resolve_Aggr_Expr (Discr_Expr, Input_Discr);
3851 Set_Inherited_Discriminant (Last (New_Assoc_List));
3855 Next_Discriminant (Discr);
3857 if Ancestor_Is_Subtyp then
3858 Next_Elmt (Discr_Val);
3863 end Discriminant_Present;
3865 ---------------------------
3866 -- Find_Private_Ancestor --
3867 ---------------------------
3869 function Find_Private_Ancestor (Typ : Entity_Id) return Entity_Id is
3875 if Has_Private_Ancestor (Par)
3876 and then not Has_Private_Ancestor (Etype (Base_Type (Par)))
3880 elsif not Is_Derived_Type (Par) then
3884 Par := Etype (Base_Type (Par));
3887 end Find_Private_Ancestor;
3894 (Compon : Entity_Id;
3896 Consider_Others_Choice : Boolean := False) return Node_Id
3898 Typ : constant Entity_Id := Etype (Compon);
3900 Expr : Node_Id := Empty;
3901 Selector_Name : Node_Id;
3904 Is_Box_Present := False;
3910 Assoc := First (From);
3911 while Present (Assoc) loop
3912 Selector_Name := First (Choices (Assoc));
3913 while Present (Selector_Name) loop
3914 if Nkind (Selector_Name) = N_Others_Choice then
3915 if Consider_Others_Choice and then No (Expr) then
3917 -- We need to duplicate the expression for each
3918 -- successive component covered by the others choice.
3919 -- This is redundant if the others_choice covers only
3920 -- one component (small optimization possible???), but
3921 -- indispensable otherwise, because each one must be
3922 -- expanded individually to preserve side effects.
3924 -- Ada 2005 (AI-287): In case of default initialization
3925 -- of components, we duplicate the corresponding default
3926 -- expression (from the record type declaration). The
3927 -- copy must carry the sloc of the association (not the
3928 -- original expression) to prevent spurious elaboration
3929 -- checks when the default includes function calls.
3931 if Box_Present (Assoc) then
3932 Others_Box := Others_Box + 1;
3933 Is_Box_Present := True;
3935 if Expander_Active then
3937 New_Copy_Tree_And_Copy_Dimensions
3938 (Expression (Parent (Compon)),
3939 New_Sloc => Sloc (Assoc));
3941 return Expression (Parent (Compon));
3945 if Present (Others_Etype)
3946 and then Base_Type (Others_Etype) /= Base_Type (Typ)
3948 -- If the components are of an anonymous access
3949 -- type they are distinct, but this is legal in
3950 -- Ada 2012 as long as designated types match.
3952 if (Ekind (Typ) = E_Anonymous_Access_Type
3953 or else Ekind (Typ) =
3954 E_Anonymous_Access_Subprogram_Type)
3955 and then Designated_Type (Typ) =
3956 Designated_Type (Others_Etype)
3961 ("components in OTHERS choice must have same "
3962 & "type", Selector_Name);
3966 Others_Etype := Typ;
3968 -- Copy the expression so that it is resolved
3969 -- independently for each component, This is needed
3970 -- for accessibility checks on compoents of anonymous
3971 -- access types, even in compile_only mode.
3973 if not Inside_A_Generic then
3975 New_Copy_Tree_And_Copy_Dimensions
3976 (Expression (Assoc));
3978 return Expression (Assoc);
3983 elsif Chars (Compon) = Chars (Selector_Name) then
3986 -- Ada 2005 (AI-231)
3988 if Ada_Version >= Ada_2005
3989 and then Known_Null (Expression (Assoc))
3991 Check_Can_Never_Be_Null (Compon, Expression (Assoc));
3994 -- We need to duplicate the expression when several
3995 -- components are grouped together with a "|" choice.
3996 -- For instance "filed1 | filed2 => Expr"
3998 -- Ada 2005 (AI-287)
4000 if Box_Present (Assoc) then
4001 Is_Box_Present := True;
4003 -- Duplicate the default expression of the component
4004 -- from the record type declaration, so a new copy
4005 -- can be attached to the association.
4007 -- Note that we always copy the default expression,
4008 -- even when the association has a single choice, in
4009 -- order to create a proper association for the
4010 -- expanded aggregate.
4012 -- Component may have no default, in which case the
4013 -- expression is empty and the component is default-
4014 -- initialized, but an association for the component
4015 -- exists, and it is not covered by an others clause.
4017 -- Scalar and private types have no initialization
4018 -- procedure, so they remain uninitialized. If the
4019 -- target of the aggregate is a constant this
4020 -- deserves a warning.
4022 if No (Expression (Parent (Compon)))
4023 and then not Has_Non_Null_Base_Init_Proc (Typ)
4024 and then not Has_Aspect (Typ, Aspect_Default_Value)
4025 and then not Is_Concurrent_Type (Typ)
4026 and then Nkind (Parent (N)) = N_Object_Declaration
4027 and then Constant_Present (Parent (N))
4029 Error_Msg_Node_2 := Typ;
4031 ("component&? of type& is uninitialized",
4032 Assoc, Selector_Name);
4034 -- An additional reminder if the component type
4035 -- is a generic formal.
4037 if Is_Generic_Type (Base_Type (Typ)) then
4039 ("\instance should provide actual type with "
4040 & "initialization for&", Assoc, Typ);
4045 New_Copy_Tree_And_Copy_Dimensions
4046 (Expression (Parent (Compon)));
4049 if Present (Next (Selector_Name)) then
4050 Expr := New_Copy_Tree_And_Copy_Dimensions
4051 (Expression (Assoc));
4053 Expr := Expression (Assoc);
4057 Generate_Reference (Compon, Selector_Name, 'm');
4061 ("more than one value supplied for &",
4062 Selector_Name, Compon);
4067 Next (Selector_Name);
4076 -----------------------------
4077 -- Propagate_Discriminants --
4078 -----------------------------
4080 procedure Propagate_Discriminants
4082 Assoc_List : List_Id)
4084 Loc : constant Source_Ptr := Sloc (N);
4086 procedure Process_Component (Comp : Entity_Id);
4087 -- Add one component with a box association to the inner aggregate,
4088 -- and recurse if component is itself composite.
4090 -----------------------
4091 -- Process_Component --
4092 -----------------------
4094 procedure Process_Component (Comp : Entity_Id) is
4095 T : constant Entity_Id := Etype (Comp);
4099 if Is_Record_Type (T) and then Has_Discriminants (T) then
4100 New_Aggr := Make_Aggregate (Loc, No_List, New_List);
4101 Set_Etype (New_Aggr, T);
4104 (Comp, New_Aggr, Component_Associations (Aggr));
4106 -- Collect discriminant values and recurse
4108 Add_Discriminant_Values (New_Aggr, Assoc_List);
4109 Propagate_Discriminants (New_Aggr, Assoc_List);
4111 Build_Constrained_Itype
4112 (New_Aggr, T, Component_Associations (New_Aggr));
4115 (Comp, Empty, Component_Associations (Aggr),
4116 Is_Box_Present => True);
4118 end Process_Component;
4122 Aggr_Type : constant Entity_Id := Base_Type (Etype (Aggr));
4123 Components : constant Elist_Id := New_Elmt_List;
4124 Def_Node : constant Node_Id :=
4125 Type_Definition (Declaration_Node (Aggr_Type));
4128 Comp_Elmt : Elmt_Id;
4131 -- Start of processing for Propagate_Discriminants
4134 -- The component type may be a variant type. Collect the components
4135 -- that are ruled by the known values of the discriminants. Their
4136 -- values have already been inserted into the component list of the
4137 -- current aggregate.
4139 if Nkind (Def_Node) = N_Record_Definition
4140 and then Present (Component_List (Def_Node))
4141 and then Present (Variant_Part (Component_List (Def_Node)))
4143 Gather_Components (Aggr_Type,
4144 Component_List (Def_Node),
4145 Governed_By => Component_Associations (Aggr),
4147 Report_Errors => Errors);
4149 Comp_Elmt := First_Elmt (Components);
4150 while Present (Comp_Elmt) loop
4151 if Ekind (Node (Comp_Elmt)) /= E_Discriminant then
4152 Process_Component (Node (Comp_Elmt));
4155 Next_Elmt (Comp_Elmt);
4158 -- No variant part, iterate over all components
4161 Comp := First_Component (Etype (Aggr));
4162 while Present (Comp) loop
4163 Process_Component (Comp);
4164 Next_Component (Comp);
4167 end Propagate_Discriminants;
4169 -----------------------
4170 -- Resolve_Aggr_Expr --
4171 -----------------------
4173 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Entity_Id) is
4174 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean;
4175 -- If the expression is an aggregate (possibly qualified) then its
4176 -- expansion is delayed until the enclosing aggregate is expanded
4177 -- into assignments. In that case, do not generate checks on the
4178 -- expression, because they will be generated later, and will other-
4179 -- wise force a copy (to remove side effects) that would leave a
4180 -- dynamic-sized aggregate in the code, something that gigi cannot
4183 ---------------------------
4184 -- Has_Expansion_Delayed --
4185 ---------------------------
4187 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean is
4190 (Nkind (Expr) in N_Aggregate | N_Extension_Aggregate
4191 and then Present (Etype (Expr))
4192 and then Is_Record_Type (Etype (Expr))
4193 and then Expansion_Delayed (Expr))
4195 (Nkind (Expr) = N_Qualified_Expression
4196 and then Has_Expansion_Delayed (Expression (Expr)));
4197 end Has_Expansion_Delayed;
4201 Expr_Type : Entity_Id := Empty;
4202 New_C : Entity_Id := Component;
4206 -- Set to True if the resolved Expr node needs to be relocated when
4207 -- attached to the newly created association list. This node need not
4208 -- be relocated if its parent pointer is not set. In fact in this
4209 -- case Expr is the output of a New_Copy_Tree call. If Relocate is
4210 -- True then we have analyzed the expression node in the original
4211 -- aggregate and hence it needs to be relocated when moved over to
4212 -- the new association list.
4214 -- Start of processing for Resolve_Aggr_Expr
4217 -- If the type of the component is elementary or the type of the
4218 -- aggregate does not contain discriminants, use the type of the
4219 -- component to resolve Expr.
4221 if Is_Elementary_Type (Etype (Component))
4222 or else not Has_Discriminants (Etype (N))
4224 Expr_Type := Etype (Component);
4226 -- Otherwise we have to pick up the new type of the component from
4227 -- the new constrained subtype of the aggregate. In fact components
4228 -- which are of a composite type might be constrained by a
4229 -- discriminant, and we want to resolve Expr against the subtype were
4230 -- all discriminant occurrences are replaced with their actual value.
4233 New_C := First_Component (Etype (N));
4234 while Present (New_C) loop
4235 if Chars (New_C) = Chars (Component) then
4236 Expr_Type := Etype (New_C);
4240 Next_Component (New_C);
4243 pragma Assert (Present (Expr_Type));
4245 -- For each range in an array type where a discriminant has been
4246 -- replaced with the constraint, check that this range is within
4247 -- the range of the base type. This checks is done in the init
4248 -- proc for regular objects, but has to be done here for
4249 -- aggregates since no init proc is called for them.
4251 if Is_Array_Type (Expr_Type) then
4254 -- Range of the current constrained index in the array
4256 Orig_Index : Node_Id := First_Index (Etype (Component));
4257 -- Range corresponding to the range Index above in the
4258 -- original unconstrained record type. The bounds of this
4259 -- range may be governed by discriminants.
4261 Unconstr_Index : Node_Id := First_Index (Etype (Expr_Type));
4262 -- Range corresponding to the range Index above for the
4263 -- unconstrained array type. This range is needed to apply
4267 Index := First_Index (Expr_Type);
4268 while Present (Index) loop
4269 if Depends_On_Discriminant (Orig_Index) then
4270 Apply_Range_Check (Index, Etype (Unconstr_Index));
4274 Next_Index (Orig_Index);
4275 Next_Index (Unconstr_Index);
4281 -- If the Parent pointer of Expr is not set, Expr is an expression
4282 -- duplicated by New_Tree_Copy (this happens for record aggregates
4283 -- that look like (Field1 | Filed2 => Expr) or (others => Expr)).
4284 -- Such a duplicated expression must be attached to the tree
4285 -- before analysis and resolution to enforce the rule that a tree
4286 -- fragment should never be analyzed or resolved unless it is
4287 -- attached to the current compilation unit.
4289 if No (Parent (Expr)) then
4290 Set_Parent (Expr, N);
4296 Analyze_And_Resolve (Expr, Expr_Type);
4297 Check_Expr_OK_In_Limited_Aggregate (Expr);
4298 Check_Non_Static_Context (Expr);
4299 Check_Unset_Reference (Expr);
4301 -- Check wrong use of class-wide types
4303 if Is_Class_Wide_Type (Etype (Expr)) then
4304 Error_Msg_N ("dynamically tagged expression not allowed", Expr);
4307 if not Has_Expansion_Delayed (Expr) then
4308 Aggregate_Constraint_Checks (Expr, Expr_Type);
4311 -- If an aggregate component has a type with predicates, an explicit
4312 -- predicate check must be applied, as for an assignment statement,
4313 -- because the aggregate might not be expanded into individual
4314 -- component assignments.
4316 if Has_Predicates (Expr_Type)
4317 and then Analyzed (Expr)
4319 Apply_Predicate_Check (Expr, Expr_Type);
4322 if Raises_Constraint_Error (Expr) then
4323 Set_Raises_Constraint_Error (N);
4326 -- If the expression has been marked as requiring a range check, then
4327 -- generate it here. It's a bit odd to be generating such checks in
4328 -- the analyzer, but harmless since Generate_Range_Check does nothing
4329 -- (other than making sure Do_Range_Check is set) if the expander is
4332 if Do_Range_Check (Expr) then
4333 Generate_Range_Check (Expr, Expr_Type, CE_Range_Check_Failed);
4336 -- Add association Component => Expr if the caller requests it
4339 New_Expr := Relocate_Node (Expr);
4341 -- Since New_Expr is not gonna be analyzed later on, we need to
4342 -- propagate here the dimensions form Expr to New_Expr.
4344 Copy_Dimensions (Expr, New_Expr);
4350 Add_Association (New_C, New_Expr, New_Assoc_List);
4351 end Resolve_Aggr_Expr;
4357 procedure Rewrite_Range (Root_Type : Entity_Id; Rge : Node_Id) is
4358 procedure Rewrite_Bound
4361 Expr_Disc : Node_Id);
4362 -- Rewrite a bound of the range Bound, when it is equal to the
4363 -- non-stored discriminant Disc, into the stored discriminant
4370 procedure Rewrite_Bound
4373 Expr_Disc : Node_Id)
4376 if Nkind (Bound) /= N_Identifier then
4380 -- We expect either the discriminant or the discriminal
4382 if Entity (Bound) = Disc
4383 or else (Ekind (Entity (Bound)) = E_In_Parameter
4384 and then Discriminal_Link (Entity (Bound)) = Disc)
4386 Rewrite (Bound, New_Copy_Tree (Expr_Disc));
4392 Low, High : Node_Id;
4394 Expr_Disc : Elmt_Id;
4396 -- Start of processing for Rewrite_Range
4399 if Has_Discriminants (Root_Type) and then Nkind (Rge) = N_Range then
4400 Low := Low_Bound (Rge);
4401 High := High_Bound (Rge);
4403 Disc := First_Discriminant (Root_Type);
4404 Expr_Disc := First_Elmt (Stored_Constraint (Etype (N)));
4405 while Present (Disc) loop
4406 Rewrite_Bound (Low, Disc, Node (Expr_Disc));
4407 Rewrite_Bound (High, Disc, Node (Expr_Disc));
4408 Next_Discriminant (Disc);
4409 Next_Elmt (Expr_Disc);
4416 Components : constant Elist_Id := New_Elmt_List;
4417 -- Components is the list of the record components whose value must be
4418 -- provided in the aggregate. This list does include discriminants.
4420 Component : Entity_Id;
4421 Component_Elmt : Elmt_Id;
4423 Positional_Expr : Node_Id;
4425 -- Start of processing for Resolve_Record_Aggregate
4428 -- A record aggregate is restricted in SPARK:
4430 -- Each named association can have only a single choice.
4431 -- OTHERS cannot be used.
4432 -- Positional and named associations cannot be mixed.
4434 if Present (Component_Associations (N))
4435 and then Present (First (Component_Associations (N)))
4441 Assoc := First (Component_Associations (N));
4442 while Present (Assoc) loop
4443 if Nkind (Assoc) = N_Iterated_Component_Association then
4445 ("iterated component association can only appear in an "
4446 & "array aggregate", N);
4447 raise Unrecoverable_Error;
4455 -- We may end up calling Duplicate_Subexpr on expressions that are
4456 -- attached to New_Assoc_List. For this reason we need to attach it
4457 -- to the tree by setting its parent pointer to N. This parent point
4458 -- will change in STEP 8 below.
4460 Set_Parent (New_Assoc_List, N);
4462 -- STEP 1: abstract type and null record verification
4464 if Is_Abstract_Type (Typ) then
4465 Error_Msg_N ("type of aggregate cannot be abstract", N);
4468 if No (First_Entity (Typ)) and then Null_Record_Present (N) then
4472 elsif Present (First_Entity (Typ))
4473 and then Null_Record_Present (N)
4474 and then not Is_Tagged_Type (Typ)
4476 Error_Msg_N ("record aggregate cannot be null", N);
4479 -- If the type has no components, then the aggregate should either
4480 -- have "null record", or in Ada 2005 it could instead have a single
4481 -- component association given by "others => <>". For Ada 95 we flag an
4482 -- error at this point, but for Ada 2005 we proceed with checking the
4483 -- associations below, which will catch the case where it's not an
4484 -- aggregate with "others => <>". Note that the legality of a <>
4485 -- aggregate for a null record type was established by AI05-016.
4487 elsif No (First_Entity (Typ))
4488 and then Ada_Version < Ada_2005
4490 Error_Msg_N ("record aggregate must be null", N);
4494 -- STEP 2: Verify aggregate structure
4498 Bad_Aggregate : Boolean := False;
4499 Selector_Name : Node_Id;
4502 if Present (Component_Associations (N)) then
4503 Assoc := First (Component_Associations (N));
4508 while Present (Assoc) loop
4509 Selector_Name := First (Choices (Assoc));
4510 while Present (Selector_Name) loop
4511 if Nkind (Selector_Name) = N_Identifier then
4514 elsif Nkind (Selector_Name) = N_Others_Choice then
4515 if Selector_Name /= First (Choices (Assoc))
4516 or else Present (Next (Selector_Name))
4519 ("OTHERS must appear alone in a choice list",
4523 elsif Present (Next (Assoc)) then
4525 ("OTHERS must appear last in an aggregate",
4529 -- (Ada 2005): If this is an association with a box,
4530 -- indicate that the association need not represent
4533 elsif Box_Present (Assoc) then
4540 ("selector name should be identifier or OTHERS",
4542 Bad_Aggregate := True;
4545 Next (Selector_Name);
4551 if Bad_Aggregate then
4556 -- STEP 3: Find discriminant Values
4559 Discrim : Entity_Id;
4560 Missing_Discriminants : Boolean := False;
4563 if Present (Expressions (N)) then
4564 Positional_Expr := First (Expressions (N));
4566 Positional_Expr := Empty;
4569 -- AI05-0115: if the ancestor part is a subtype mark, the ancestor
4570 -- must not have unknown discriminants.
4571 -- ??? We are not checking any subtype mark here and this code is not
4572 -- exercised by any test, so it's likely wrong (in particular
4573 -- we should not use Root_Type here but the subtype mark, if any),
4574 -- and possibly not needed.
4576 if Is_Derived_Type (Typ)
4577 and then Has_Unknown_Discriminants (Root_Type (Typ))
4578 and then Nkind (N) /= N_Extension_Aggregate
4581 ("aggregate not available for type& whose ancestor "
4582 & "has unknown discriminants ", N, Typ);
4585 if Has_Unknown_Discriminants (Typ)
4586 and then Present (Underlying_Record_View (Typ))
4588 Discrim := First_Discriminant (Underlying_Record_View (Typ));
4589 elsif Has_Discriminants (Typ) then
4590 Discrim := First_Discriminant (Typ);
4595 -- First find the discriminant values in the positional components
4597 while Present (Discrim) and then Present (Positional_Expr) loop
4598 if Discriminant_Present (Discrim) then
4599 Resolve_Aggr_Expr (Positional_Expr, Discrim);
4601 -- Ada 2005 (AI-231)
4603 if Ada_Version >= Ada_2005
4604 and then Known_Null (Positional_Expr)
4606 Check_Can_Never_Be_Null (Discrim, Positional_Expr);
4609 Next (Positional_Expr);
4612 if Present (Get_Value (Discrim, Component_Associations (N))) then
4614 ("more than one value supplied for discriminant&",
4618 Next_Discriminant (Discrim);
4621 -- Find remaining discriminant values if any among named components
4623 while Present (Discrim) loop
4624 Expr := Get_Value (Discrim, Component_Associations (N), True);
4626 if not Discriminant_Present (Discrim) then
4627 if Present (Expr) then
4629 ("more than one value supplied for discriminant &",
4633 elsif No (Expr) then
4635 ("no value supplied for discriminant &", N, Discrim);
4636 Missing_Discriminants := True;
4639 Resolve_Aggr_Expr (Expr, Discrim);
4642 Next_Discriminant (Discrim);
4645 if Missing_Discriminants then
4649 -- At this point and until the beginning of STEP 6, New_Assoc_List
4650 -- contains only the discriminants and their values.
4654 -- STEP 4: Set the Etype of the record aggregate
4656 if Has_Discriminants (Typ)
4657 or else (Has_Unknown_Discriminants (Typ)
4658 and then Present (Underlying_Record_View (Typ)))
4660 Build_Constrained_Itype (N, Typ, New_Assoc_List);
4665 -- STEP 5: Get remaining components according to discriminant values
4669 Errors_Found : Boolean := False;
4670 Record_Def : Node_Id;
4671 Parent_Typ : Entity_Id;
4672 Parent_Typ_List : Elist_Id;
4673 Parent_Elmt : Elmt_Id;
4674 Root_Typ : Entity_Id;
4677 if Is_Derived_Type (Typ) and then Is_Tagged_Type (Typ) then
4678 Parent_Typ_List := New_Elmt_List;
4680 -- If this is an extension aggregate, the component list must
4681 -- include all components that are not in the given ancestor type.
4682 -- Otherwise, the component list must include components of all
4683 -- ancestors, starting with the root.
4685 if Nkind (N) = N_Extension_Aggregate then
4686 Root_Typ := Base_Type (Etype (Ancestor_Part (N)));
4689 -- AI05-0115: check legality of aggregate for type with a
4690 -- private ancestor.
4692 Root_Typ := Root_Type (Typ);
4693 if Has_Private_Ancestor (Typ) then
4695 Ancestor : constant Entity_Id :=
4696 Find_Private_Ancestor (Typ);
4697 Ancestor_Unit : constant Entity_Id :=
4699 (Get_Source_Unit (Ancestor));
4700 Parent_Unit : constant Entity_Id :=
4701 Cunit_Entity (Get_Source_Unit
4702 (Base_Type (Etype (Ancestor))));
4704 -- Check whether we are in a scope that has full view
4705 -- over the private ancestor and its parent. This can
4706 -- only happen if the derivation takes place in a child
4707 -- unit of the unit that declares the parent, and we are
4708 -- in the private part or body of that child unit, else
4709 -- the aggregate is illegal.
4711 if Is_Child_Unit (Ancestor_Unit)
4712 and then Scope (Ancestor_Unit) = Parent_Unit
4713 and then In_Open_Scopes (Scope (Ancestor))
4715 (In_Private_Part (Scope (Ancestor))
4716 or else In_Package_Body (Scope (Ancestor)))
4722 ("type of aggregate has private ancestor&!",
4724 Error_Msg_N ("must use extension aggregate!", N);
4730 Dnode := Declaration_Node (Base_Type (Root_Typ));
4732 -- If we don't get a full declaration, then we have some error
4733 -- which will get signalled later so skip this part. Otherwise
4734 -- gather components of root that apply to the aggregate type.
4735 -- We use the base type in case there is an applicable stored
4736 -- constraint that renames the discriminants of the root.
4738 if Nkind (Dnode) = N_Full_Type_Declaration then
4739 Record_Def := Type_Definition (Dnode);
4742 Component_List (Record_Def),
4743 Governed_By => New_Assoc_List,
4745 Report_Errors => Errors_Found);
4747 if Errors_Found then
4749 ("discriminant controlling variant part is not static",
4756 Parent_Typ := Base_Type (Typ);
4757 while Parent_Typ /= Root_Typ loop
4758 Prepend_Elmt (Parent_Typ, To => Parent_Typ_List);
4759 Parent_Typ := Etype (Parent_Typ);
4761 if Nkind (Parent (Base_Type (Parent_Typ))) =
4762 N_Private_Type_Declaration
4763 or else Nkind (Parent (Base_Type (Parent_Typ))) =
4764 N_Private_Extension_Declaration
4766 if Nkind (N) /= N_Extension_Aggregate then
4768 ("type of aggregate has private ancestor&!",
4770 Error_Msg_N ("must use extension aggregate!", N);
4773 elsif Parent_Typ /= Root_Typ then
4775 ("ancestor part of aggregate must be private type&",
4776 Ancestor_Part (N), Parent_Typ);
4780 -- The current view of ancestor part may be a private type,
4781 -- while the context type is always non-private.
4783 elsif Is_Private_Type (Root_Typ)
4784 and then Present (Full_View (Root_Typ))
4785 and then Nkind (N) = N_Extension_Aggregate
4787 exit when Base_Type (Full_View (Root_Typ)) = Parent_Typ;
4791 -- Now collect components from all other ancestors, beginning
4792 -- with the current type. If the type has unknown discriminants
4793 -- use the component list of the Underlying_Record_View, which
4794 -- needs to be used for the subsequent expansion of the aggregate
4795 -- into assignments.
4797 Parent_Elmt := First_Elmt (Parent_Typ_List);
4798 while Present (Parent_Elmt) loop
4799 Parent_Typ := Node (Parent_Elmt);
4801 if Has_Unknown_Discriminants (Parent_Typ)
4802 and then Present (Underlying_Record_View (Typ))
4804 Parent_Typ := Underlying_Record_View (Parent_Typ);
4807 Record_Def := Type_Definition (Parent (Base_Type (Parent_Typ)));
4808 Gather_Components (Empty,
4809 Component_List (Record_Extension_Part (Record_Def)),
4810 Governed_By => New_Assoc_List,
4812 Report_Errors => Errors_Found);
4814 Next_Elmt (Parent_Elmt);
4817 -- Typ is not a derived tagged type
4820 Record_Def := Type_Definition (Parent (Base_Type (Typ)));
4822 if Null_Present (Record_Def) then
4825 elsif not Has_Unknown_Discriminants (Typ) then
4828 Component_List (Record_Def),
4829 Governed_By => New_Assoc_List,
4831 Report_Errors => Errors_Found);
4835 (Base_Type (Underlying_Record_View (Typ)),
4836 Component_List (Record_Def),
4837 Governed_By => New_Assoc_List,
4839 Report_Errors => Errors_Found);
4843 if Errors_Found then
4848 -- STEP 6: Find component Values
4851 Component_Elmt := First_Elmt (Components);
4853 -- First scan the remaining positional associations in the aggregate.
4854 -- Remember that at this point Positional_Expr contains the current
4855 -- positional association if any is left after looking for discriminant
4856 -- values in step 3.
4858 while Present (Positional_Expr) and then Present (Component_Elmt) loop
4859 Component := Node (Component_Elmt);
4860 Resolve_Aggr_Expr (Positional_Expr, Component);
4862 -- Ada 2005 (AI-231)
4864 if Ada_Version >= Ada_2005 and then Known_Null (Positional_Expr) then
4865 Check_Can_Never_Be_Null (Component, Positional_Expr);
4868 if Present (Get_Value (Component, Component_Associations (N))) then
4870 ("more than one value supplied for Component &", N, Component);
4873 Next (Positional_Expr);
4874 Next_Elmt (Component_Elmt);
4877 if Present (Positional_Expr) then
4879 ("too many components for record aggregate", Positional_Expr);
4882 -- Now scan for the named arguments of the aggregate
4884 while Present (Component_Elmt) loop
4885 Component := Node (Component_Elmt);
4886 Expr := Get_Value (Component, Component_Associations (N), True);
4888 -- Note: The previous call to Get_Value sets the value of the
4889 -- variable Is_Box_Present.
4891 -- Ada 2005 (AI-287): Handle components with default initialization.
4892 -- Note: This feature was originally added to Ada 2005 for limited
4893 -- but it was finally allowed with any type.
4895 if Is_Box_Present then
4896 Check_Box_Component : declare
4897 Ctyp : constant Entity_Id := Etype (Component);
4900 -- If there is a default expression for the aggregate, copy
4901 -- it into a new association. This copy must modify the scopes
4902 -- of internal types that may be attached to the expression
4903 -- (e.g. index subtypes of arrays) because in general the type
4904 -- declaration and the aggregate appear in different scopes,
4905 -- and the backend requires the scope of the type to match the
4906 -- point at which it is elaborated.
4908 -- If the component has an initialization procedure (IP) we
4909 -- pass the component to the expander, which will generate
4910 -- the call to such IP.
4912 -- If the component has discriminants, their values must
4913 -- be taken from their subtype. This is indispensable for
4914 -- constraints that are given by the current instance of an
4915 -- enclosing type, to allow the expansion of the aggregate to
4916 -- replace the reference to the current instance by the target
4917 -- object of the aggregate.
4919 if Present (Parent (Component))
4920 and then Nkind (Parent (Component)) = N_Component_Declaration
4921 and then Present (Expression (Parent (Component)))
4924 New_Copy_Tree_And_Copy_Dimensions
4925 (Expression (Parent (Component)),
4926 New_Scope => Current_Scope,
4927 New_Sloc => Sloc (N));
4929 -- As the type of the copied default expression may refer
4930 -- to discriminants of the record type declaration, these
4931 -- non-stored discriminants need to be rewritten into stored
4932 -- discriminant values for the aggregate. This is required
4933 -- in GNATprove mode, and is adopted in all modes to avoid
4934 -- special-casing GNATprove mode.
4936 if Is_Array_Type (Etype (Expr)) then
4938 Rec_Typ : constant Entity_Id := Scope (Component);
4939 -- Root record type whose discriminants may be used as
4940 -- bounds in range nodes.
4947 -- Rewrite the range nodes occurring in the indexes
4950 Index := First_Index (Etype (Expr));
4951 while Present (Index) loop
4952 Rewrite_Range (Rec_Typ, Index);
4954 (Rec_Typ, Scalar_Range (Etype (Index)));
4959 -- Rewrite the range nodes occurring as aggregate
4960 -- bounds and component associations.
4962 if Nkind (Expr) = N_Aggregate then
4963 if Present (Aggregate_Bounds (Expr)) then
4964 Rewrite_Range (Rec_Typ, Aggregate_Bounds (Expr));
4967 if Present (Component_Associations (Expr)) then
4968 Assoc := First (Component_Associations (Expr));
4969 while Present (Assoc) loop
4970 Choice := First (Choices (Assoc));
4971 while Present (Choice) loop
4972 Rewrite_Range (Rec_Typ, Choice);
4985 (Component => Component,
4987 Assoc_List => New_Assoc_List);
4988 Set_Has_Self_Reference (N);
4990 -- A box-defaulted access component gets the value null. Also
4991 -- included are components of private types whose underlying
4992 -- type is an access type. In either case set the type of the
4993 -- literal, for subsequent use in semantic checks.
4995 elsif Present (Underlying_Type (Ctyp))
4996 and then Is_Access_Type (Underlying_Type (Ctyp))
4998 -- If the component's type is private with an access type as
4999 -- its underlying type then we have to create an unchecked
5000 -- conversion to satisfy type checking.
5002 if Is_Private_Type (Ctyp) then
5004 Qual_Null : constant Node_Id :=
5005 Make_Qualified_Expression (Sloc (N),
5008 (Underlying_Type (Ctyp), Sloc (N)),
5009 Expression => Make_Null (Sloc (N)));
5011 Convert_Null : constant Node_Id :=
5012 Unchecked_Convert_To
5016 Analyze_And_Resolve (Convert_Null, Ctyp);
5018 (Component => Component,
5019 Expr => Convert_Null,
5020 Assoc_List => New_Assoc_List);
5023 -- Otherwise the component type is non-private
5026 Expr := Make_Null (Sloc (N));
5027 Set_Etype (Expr, Ctyp);
5030 (Component => Component,
5032 Assoc_List => New_Assoc_List);
5035 -- Ada 2012: If component is scalar with default value, use it
5037 elsif Is_Scalar_Type (Ctyp)
5038 and then Has_Default_Aspect (Ctyp)
5041 (Component => Component,
5043 Default_Aspect_Value
5044 (First_Subtype (Underlying_Type (Ctyp))),
5045 Assoc_List => New_Assoc_List);
5047 elsif Has_Non_Null_Base_Init_Proc (Ctyp)
5048 or else not Expander_Active
5050 if Is_Record_Type (Ctyp)
5051 and then Has_Discriminants (Ctyp)
5052 and then not Is_Private_Type (Ctyp)
5054 -- We build a partially initialized aggregate with the
5055 -- values of the discriminants and box initialization
5056 -- for the rest, if other components are present.
5058 -- The type of the aggregate is the known subtype of
5059 -- the component. The capture of discriminants must be
5060 -- recursive because subcomponents may be constrained
5061 -- (transitively) by discriminants of enclosing types.
5062 -- For a private type with discriminants, a call to the
5063 -- initialization procedure will be generated, and no
5064 -- subaggregate is needed.
5066 Capture_Discriminants : declare
5067 Loc : constant Source_Ptr := Sloc (N);
5071 Expr := Make_Aggregate (Loc, No_List, New_List);
5072 Set_Etype (Expr, Ctyp);
5074 -- If the enclosing type has discriminants, they have
5075 -- been collected in the aggregate earlier, and they
5076 -- may appear as constraints of subcomponents.
5078 -- Similarly if this component has discriminants, they
5079 -- might in turn be propagated to their components.
5081 if Has_Discriminants (Typ) then
5082 Add_Discriminant_Values (Expr, New_Assoc_List);
5083 Propagate_Discriminants (Expr, New_Assoc_List);
5085 elsif Has_Discriminants (Ctyp) then
5086 Add_Discriminant_Values
5087 (Expr, Component_Associations (Expr));
5088 Propagate_Discriminants
5089 (Expr, Component_Associations (Expr));
5091 Build_Constrained_Itype
5092 (Expr, Ctyp, Component_Associations (Expr));
5099 -- If the type has additional components, create
5100 -- an OTHERS box association for them.
5102 Comp := First_Component (Ctyp);
5103 while Present (Comp) loop
5104 if Ekind (Comp) = E_Component then
5105 if not Is_Record_Type (Etype (Comp)) then
5107 (Component_Associations (Expr),
5108 Make_Component_Association (Loc,
5111 Make_Others_Choice (Loc)),
5112 Expression => Empty,
5113 Box_Present => True));
5119 Next_Component (Comp);
5125 (Component => Component,
5127 Assoc_List => New_Assoc_List);
5128 end Capture_Discriminants;
5130 -- Otherwise the component type is not a record, or it has
5131 -- not discriminants, or it is private.
5135 (Component => Component,
5137 Assoc_List => New_Assoc_List,
5138 Is_Box_Present => True);
5141 -- Otherwise we only need to resolve the expression if the
5142 -- component has partially initialized values (required to
5143 -- expand the corresponding assignments and run-time checks).
5145 elsif Present (Expr)
5146 and then Is_Partially_Initialized_Type (Ctyp)
5148 Resolve_Aggr_Expr (Expr, Component);
5150 end Check_Box_Component;
5152 elsif No (Expr) then
5154 -- Ignore hidden components associated with the position of the
5155 -- interface tags: these are initialized dynamically.
5157 if not Present (Related_Type (Component)) then
5159 ("no value supplied for component &!", N, Component);
5163 Resolve_Aggr_Expr (Expr, Component);
5166 Next_Elmt (Component_Elmt);
5169 -- STEP 7: check for invalid components + check type in choice list
5173 New_Assoc : Node_Id;
5179 -- Type of first component in choice list
5182 if Present (Component_Associations (N)) then
5183 Assoc := First (Component_Associations (N));
5188 Verification : while Present (Assoc) loop
5189 Selectr := First (Choices (Assoc));
5192 if Nkind (Selectr) = N_Others_Choice then
5194 -- Ada 2005 (AI-287): others choice may have expression or box
5196 if No (Others_Etype) and then Others_Box = 0 then
5198 ("OTHERS must represent at least one component", Selectr);
5200 elsif Others_Box = 1 and then Warn_On_Redundant_Constructs then
5201 Error_Msg_N ("others choice is redundant?", Box_Node);
5203 ("\previous choices cover all components?", Box_Node);
5209 while Present (Selectr) loop
5210 New_Assoc := First (New_Assoc_List);
5211 while Present (New_Assoc) loop
5212 Component := First (Choices (New_Assoc));
5214 if Chars (Selectr) = Chars (Component) then
5216 Check_Identifier (Selectr, Entity (Component));
5225 -- If no association, this is not a legal component of the type
5226 -- in question, unless its association is provided with a box.
5228 if No (New_Assoc) then
5229 if Box_Present (Parent (Selectr)) then
5231 -- This may still be a bogus component with a box. Scan
5232 -- list of components to verify that a component with
5233 -- that name exists.
5239 C := First_Component (Typ);
5240 while Present (C) loop
5241 if Chars (C) = Chars (Selectr) then
5243 -- If the context is an extension aggregate,
5244 -- the component must not be inherited from
5245 -- the ancestor part of the aggregate.
5247 if Nkind (N) /= N_Extension_Aggregate
5249 Scope (Original_Record_Component (C)) /=
5250 Etype (Ancestor_Part (N))
5260 Error_Msg_Node_2 := Typ;
5261 Error_Msg_N ("& is not a component of}", Selectr);
5265 elsif Chars (Selectr) /= Name_uTag
5266 and then Chars (Selectr) /= Name_uParent
5268 if not Has_Discriminants (Typ) then
5269 Error_Msg_Node_2 := Typ;
5270 Error_Msg_N ("& is not a component of}", Selectr);
5273 ("& is not a component of the aggregate subtype",
5277 Check_Misspelled_Component (Components, Selectr);
5280 elsif No (Typech) then
5281 Typech := Base_Type (Etype (Component));
5283 -- AI05-0199: In Ada 2012, several components of anonymous
5284 -- access types can appear in a choice list, as long as the
5285 -- designated types match.
5287 elsif Typech /= Base_Type (Etype (Component)) then
5288 if Ada_Version >= Ada_2012
5289 and then Ekind (Typech) = E_Anonymous_Access_Type
5291 Ekind (Etype (Component)) = E_Anonymous_Access_Type
5292 and then Base_Type (Designated_Type (Typech)) =
5293 Base_Type (Designated_Type (Etype (Component)))
5295 Subtypes_Statically_Match (Typech, (Etype (Component)))
5299 elsif not Box_Present (Parent (Selectr)) then
5301 ("components in choice list must have same type",
5310 end loop Verification;
5313 -- STEP 8: replace the original aggregate
5316 New_Aggregate : constant Node_Id := New_Copy (N);
5319 Set_Expressions (New_Aggregate, No_List);
5320 Set_Etype (New_Aggregate, Etype (N));
5321 Set_Component_Associations (New_Aggregate, New_Assoc_List);
5322 Set_Check_Actuals (New_Aggregate, Check_Actuals (N));
5324 Rewrite (N, New_Aggregate);
5327 -- Check the dimensions of the components in the record aggregate
5329 Analyze_Dimension_Extension_Or_Record_Aggregate (N);
5330 end Resolve_Record_Aggregate;
5332 -----------------------------
5333 -- Check_Can_Never_Be_Null --
5334 -----------------------------
5336 procedure Check_Can_Never_Be_Null (Typ : Entity_Id; Expr : Node_Id) is
5337 Comp_Typ : Entity_Id;
5341 (Ada_Version >= Ada_2005
5342 and then Present (Expr)
5343 and then Known_Null (Expr));
5346 when E_Array_Type =>
5347 Comp_Typ := Component_Type (Typ);
5352 Comp_Typ := Etype (Typ);
5358 if Can_Never_Be_Null (Comp_Typ) then
5360 -- Here we know we have a constraint error. Note that we do not use
5361 -- Apply_Compile_Time_Constraint_Error here to the Expr, which might
5362 -- seem the more natural approach. That's because in some cases the
5363 -- components are rewritten, and the replacement would be missed.
5364 -- We do not mark the whole aggregate as raising a constraint error,
5365 -- because the association may be a null array range.
5368 ("(Ada 2005) null not allowed in null-excluding component??", Expr);
5370 ("\Constraint_Error will be raised at run time??", Expr);
5373 Make_Raise_Constraint_Error
5374 (Sloc (Expr), Reason => CE_Access_Check_Failed));
5375 Set_Etype (Expr, Comp_Typ);
5376 Set_Analyzed (Expr);
5378 end Check_Can_Never_Be_Null;
5380 ---------------------
5381 -- Sort_Case_Table --
5382 ---------------------
5384 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is
5385 U : constant Int := Case_Table'Last;
5393 T := Case_Table (K + 1);
5397 and then Expr_Value (Case_Table (J - 1).Lo) > Expr_Value (T.Lo)
5399 Case_Table (J) := Case_Table (J - 1);
5403 Case_Table (J) := T;
5406 end Sort_Case_Table;