1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2020, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Einfo; use Einfo;
30 with Elists; use Elists;
31 with Errout; use Errout;
32 with Expander; use Expander;
33 with Exp_Ch6; use Exp_Ch6;
34 with Exp_Tss; use Exp_Tss;
35 with Exp_Util; use Exp_Util;
36 with Freeze; use Freeze;
37 with Itypes; use Itypes;
39 with Lib.Xref; use Lib.Xref;
40 with Namet; use Namet;
41 with Namet.Sp; use Namet.Sp;
42 with Nmake; use Nmake;
43 with Nlists; use Nlists;
45 with Restrict; use Restrict;
46 with Rident; use Rident;
48 with Sem_Aux; use Sem_Aux;
49 with Sem_Cat; use Sem_Cat;
50 with Sem_Ch3; use Sem_Ch3;
51 with Sem_Ch5; use Sem_Ch5;
52 with Sem_Ch8; use Sem_Ch8;
53 with Sem_Ch13; use Sem_Ch13;
54 with Sem_Dim; use Sem_Dim;
55 with Sem_Eval; use Sem_Eval;
56 with Sem_Res; use Sem_Res;
57 with Sem_Util; use Sem_Util;
58 with Sem_Type; use Sem_Type;
59 with Sem_Warn; use Sem_Warn;
60 with Sinfo; use Sinfo;
61 with Snames; use Snames;
62 with Stringt; use Stringt;
63 with Stand; use Stand;
64 with Style; use Style;
65 with Targparm; use Targparm;
66 with Tbuild; use Tbuild;
67 with Ttypes; use Ttypes;
68 with Uintp; use Uintp;
70 package body Sem_Aggr is
72 type Case_Bounds is record
74 -- Low bound of choice. Once we sort the Case_Table, then entries
75 -- will be in order of ascending Choice_Lo values.
78 -- High Bound of choice. The sort does not pay any attention to the
79 -- high bound, so choices 1 .. 4 and 1 .. 5 could be in either order.
82 -- If there are duplicates or missing entries, then in the sorted
83 -- table, this records the highest value among Choice_Hi values
84 -- seen so far, including this entry.
87 -- The node of the choice
90 type Case_Table_Type is array (Pos range <>) of Case_Bounds;
91 -- Table type used by Check_Case_Choices procedure
93 -----------------------
94 -- Local Subprograms --
95 -----------------------
97 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type);
98 -- Sort the Case Table using the Lower Bound of each Choice as the key. A
99 -- simple insertion sort is used since the choices in a case statement will
100 -- usually be in near sorted order.
102 procedure Check_Can_Never_Be_Null (Typ : Entity_Id; Expr : Node_Id);
103 -- Ada 2005 (AI-231): Check bad usage of null for a component for which
104 -- null exclusion (NOT NULL) is specified. Typ can be an E_Array_Type for
105 -- the array case (the component type of the array will be used) or an
106 -- E_Component/E_Discriminant entity in the record case, in which case the
107 -- type of the component will be used for the test. If Typ is any other
108 -- kind of entity, the call is ignored. Expr is the component node in the
109 -- aggregate which is known to have a null value. A warning message will be
110 -- issued if the component is null excluding.
112 -- It would be better to pass the proper type for Typ ???
114 procedure Check_Expr_OK_In_Limited_Aggregate (Expr : Node_Id);
115 -- Check that Expr is either not limited or else is one of the cases of
116 -- expressions allowed for a limited component association (namely, an
117 -- aggregate, function call, or <> notation). Report error for violations.
118 -- Expression is also OK in an instance or inlining context, because we
119 -- have already preanalyzed and it is known to be type correct.
121 ------------------------------------------------------
122 -- Subprograms used for RECORD AGGREGATE Processing --
123 ------------------------------------------------------
125 procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id);
126 -- This procedure performs all the semantic checks required for record
127 -- aggregates. Note that for aggregates analysis and resolution go
128 -- hand in hand. Aggregate analysis has been delayed up to here and
129 -- it is done while resolving the aggregate.
131 -- N is the N_Aggregate node.
132 -- Typ is the record type for the aggregate resolution
134 -- While performing the semantic checks, this procedure builds a new
135 -- Component_Association_List where each record field appears alone in a
136 -- Component_Choice_List along with its corresponding expression. The
137 -- record fields in the Component_Association_List appear in the same order
138 -- in which they appear in the record type Typ.
140 -- Once this new Component_Association_List is built and all the semantic
141 -- checks performed, the original aggregate subtree is replaced with the
142 -- new named record aggregate just built. This new record aggregate has no
143 -- positional associations, so its Expressions field is set to No_List.
144 -- Note that subtree substitution is performed with Rewrite so as to be
145 -- able to retrieve the original aggregate.
147 -- The aggregate subtree manipulation performed by Resolve_Record_Aggregate
148 -- yields the aggregate format expected by Gigi. Typically, this kind of
149 -- tree manipulations are done in the expander. However, because the
150 -- semantic checks that need to be performed on record aggregates really go
151 -- hand in hand with the record aggregate normalization, the aggregate
152 -- subtree transformation is performed during resolution rather than
153 -- expansion. Had we decided otherwise we would have had to duplicate most
154 -- of the code in the expansion procedure Expand_Record_Aggregate. Note,
155 -- however, that all the expansion concerning aggregates for tagged records
156 -- is done in Expand_Record_Aggregate.
158 -- The algorithm of Resolve_Record_Aggregate proceeds as follows:
160 -- 1. Make sure that the record type against which the record aggregate
161 -- has to be resolved is not abstract. Furthermore if the type is a
162 -- null aggregate make sure the input aggregate N is also null.
164 -- 2. Verify that the structure of the aggregate is that of a record
165 -- aggregate. Specifically, look for component associations and ensure
166 -- that each choice list only has identifiers or the N_Others_Choice
167 -- node. Also make sure that if present, the N_Others_Choice occurs
168 -- last and by itself.
170 -- 3. If Typ contains discriminants, the values for each discriminant is
171 -- looked for. If the record type Typ has variants, we check that the
172 -- expressions corresponding to each discriminant ruling the (possibly
173 -- nested) variant parts of Typ, are static. This allows us to determine
174 -- the variant parts to which the rest of the aggregate must conform.
175 -- The names of discriminants with their values are saved in a new
176 -- association list, New_Assoc_List which is later augmented with the
177 -- names and values of the remaining components in the record type.
179 -- During this phase we also make sure that every discriminant is
180 -- assigned exactly one value. Note that when several values for a given
181 -- discriminant are found, semantic processing continues looking for
182 -- further errors. In this case it's the first discriminant value found
183 -- which we will be recorded.
185 -- IMPORTANT NOTE: For derived tagged types this procedure expects
186 -- First_Discriminant and Next_Discriminant to give the correct list
187 -- of discriminants, in the correct order.
189 -- 4. After all the discriminant values have been gathered, we can set the
190 -- Etype of the record aggregate. If Typ contains no discriminants this
191 -- is straightforward: the Etype of N is just Typ, otherwise a new
192 -- implicit constrained subtype of Typ is built to be the Etype of N.
194 -- 5. Gather the remaining record components according to the discriminant
195 -- values. This involves recursively traversing the record type
196 -- structure to see what variants are selected by the given discriminant
197 -- values. This processing is a little more convoluted if Typ is a
198 -- derived tagged types since we need to retrieve the record structure
199 -- of all the ancestors of Typ.
201 -- 6. After gathering the record components we look for their values in the
202 -- record aggregate and emit appropriate error messages should we not
203 -- find such values or should they be duplicated.
205 -- 7. We then make sure no illegal component names appear in the record
206 -- aggregate and make sure that the type of the record components
207 -- appearing in a same choice list is the same. Finally we ensure that
208 -- the others choice, if present, is used to provide the value of at
209 -- least a record component.
211 -- 8. The original aggregate node is replaced with the new named aggregate
212 -- built in steps 3 through 6, as explained earlier.
214 -- Given the complexity of record aggregate resolution, the primary goal of
215 -- this routine is clarity and simplicity rather than execution and storage
216 -- efficiency. If there are only positional components in the aggregate the
217 -- running time is linear. If there are associations the running time is
218 -- still linear as long as the order of the associations is not too far off
219 -- the order of the components in the record type. If this is not the case
220 -- the running time is at worst quadratic in the size of the association
223 procedure Check_Misspelled_Component
224 (Elements : Elist_Id;
225 Component : Node_Id);
226 -- Give possible misspelling diagnostic if Component is likely to be a
227 -- misspelling of one of the components of the Assoc_List. This is called
228 -- by Resolve_Aggr_Expr after producing an invalid component error message.
230 -----------------------------------------------------
231 -- Subprograms used for ARRAY AGGREGATE Processing --
232 -----------------------------------------------------
234 function Resolve_Array_Aggregate
237 Index_Constr : Node_Id;
238 Component_Typ : Entity_Id;
239 Others_Allowed : Boolean) return Boolean;
240 -- This procedure performs the semantic checks for an array aggregate.
241 -- True is returned if the aggregate resolution succeeds.
243 -- The procedure works by recursively checking each nested aggregate.
244 -- Specifically, after checking a sub-aggregate nested at the i-th level
245 -- we recursively check all the subaggregates at the i+1-st level (if any).
246 -- Note that for aggregates analysis and resolution go hand in hand.
247 -- Aggregate analysis has been delayed up to here and it is done while
248 -- resolving the aggregate.
250 -- N is the current N_Aggregate node to be checked.
252 -- Index is the index node corresponding to the array sub-aggregate that
253 -- we are currently checking (RM 4.3.3 (8)). Its Etype is the
254 -- corresponding index type (or subtype).
256 -- Index_Constr is the node giving the applicable index constraint if
257 -- any (RM 4.3.3 (10)). It "is a constraint provided by certain
258 -- contexts [...] that can be used to determine the bounds of the array
259 -- value specified by the aggregate". If Others_Allowed below is False
260 -- there is no applicable index constraint and this node is set to Index.
262 -- Component_Typ is the array component type.
264 -- Others_Allowed indicates whether an others choice is allowed
265 -- in the context where the top-level aggregate appeared.
267 -- The algorithm of Resolve_Array_Aggregate proceeds as follows:
269 -- 1. Make sure that the others choice, if present, is by itself and
270 -- appears last in the sub-aggregate. Check that we do not have
271 -- positional and named components in the array sub-aggregate (unless
272 -- the named association is an others choice). Finally if an others
273 -- choice is present, make sure it is allowed in the aggregate context.
275 -- 2. If the array sub-aggregate contains discrete_choices:
277 -- (A) Verify their validity. Specifically verify that:
279 -- (a) If a null range is present it must be the only possible
280 -- choice in the array aggregate.
282 -- (b) Ditto for a non static range.
284 -- (c) Ditto for a non static expression.
286 -- In addition this step analyzes and resolves each discrete_choice,
287 -- making sure that its type is the type of the corresponding Index.
288 -- If we are not at the lowest array aggregate level (in the case of
289 -- multi-dimensional aggregates) then invoke Resolve_Array_Aggregate
290 -- recursively on each component expression. Otherwise, resolve the
291 -- bottom level component expressions against the expected component
292 -- type ONLY IF the component corresponds to a single discrete choice
293 -- which is not an others choice (to see why read the DELAYED
294 -- COMPONENT RESOLUTION below).
296 -- (B) Determine the bounds of the sub-aggregate and lowest and
297 -- highest choice values.
299 -- 3. For positional aggregates:
301 -- (A) Loop over the component expressions either recursively invoking
302 -- Resolve_Array_Aggregate on each of these for multi-dimensional
303 -- array aggregates or resolving the bottom level component
304 -- expressions against the expected component type.
306 -- (B) Determine the bounds of the positional sub-aggregates.
308 -- 4. Try to determine statically whether the evaluation of the array
309 -- sub-aggregate raises Constraint_Error. If yes emit proper
310 -- warnings. The precise checks are the following:
312 -- (A) Check that the index range defined by aggregate bounds is
313 -- compatible with corresponding index subtype.
314 -- We also check against the base type. In fact it could be that
315 -- Low/High bounds of the base type are static whereas those of
316 -- the index subtype are not. Thus if we can statically catch
317 -- a problem with respect to the base type we are guaranteed
318 -- that the same problem will arise with the index subtype
320 -- (B) If we are dealing with a named aggregate containing an others
321 -- choice and at least one discrete choice then make sure the range
322 -- specified by the discrete choices does not overflow the
323 -- aggregate bounds. We also check against the index type and base
324 -- type bounds for the same reasons given in (A).
326 -- (C) If we are dealing with a positional aggregate with an others
327 -- choice make sure the number of positional elements specified
328 -- does not overflow the aggregate bounds. We also check against
329 -- the index type and base type bounds as mentioned in (A).
331 -- Finally construct an N_Range node giving the sub-aggregate bounds.
332 -- Set the Aggregate_Bounds field of the sub-aggregate to be this
333 -- N_Range. The routine Array_Aggr_Subtype below uses such N_Ranges
334 -- to build the appropriate aggregate subtype. Aggregate_Bounds
335 -- information is needed during expansion.
337 -- DELAYED COMPONENT RESOLUTION: The resolution of bottom level component
338 -- expressions in an array aggregate may call Duplicate_Subexpr or some
339 -- other routine that inserts code just outside the outermost aggregate.
340 -- If the array aggregate contains discrete choices or an others choice,
341 -- this may be wrong. Consider for instance the following example.
343 -- type Rec is record
347 -- type Acc_Rec is access Rec;
348 -- Arr : array (1..3) of Acc_Rec := (1 .. 3 => new Rec);
350 -- Then the transformation of "new Rec" that occurs during resolution
351 -- entails the following code modifications
353 -- P7b : constant Acc_Rec := new Rec;
355 -- Arr : array (1..3) of Acc_Rec := (1 .. 3 => P7b);
357 -- This code transformation is clearly wrong, since we need to call
358 -- "new Rec" for each of the 3 array elements. To avoid this problem we
359 -- delay resolution of the components of non positional array aggregates
360 -- to the expansion phase. As an optimization, if the discrete choice
361 -- specifies a single value we do not delay resolution.
363 function Array_Aggr_Subtype (N : Node_Id; Typ : Node_Id) return Entity_Id;
364 -- This routine returns the type or subtype of an array aggregate.
366 -- N is the array aggregate node whose type we return.
368 -- Typ is the context type in which N occurs.
370 -- This routine creates an implicit array subtype whose bounds are
371 -- those defined by the aggregate. When this routine is invoked
372 -- Resolve_Array_Aggregate has already processed aggregate N. Thus the
373 -- Aggregate_Bounds of each sub-aggregate, is an N_Range node giving the
374 -- sub-aggregate bounds. When building the aggregate itype, this function
375 -- traverses the array aggregate N collecting such Aggregate_Bounds and
376 -- constructs the proper array aggregate itype.
378 -- Note that in the case of multidimensional aggregates each inner
379 -- sub-aggregate corresponding to a given array dimension, may provide a
380 -- different bounds. If it is possible to determine statically that
381 -- some sub-aggregates corresponding to the same index do not have the
382 -- same bounds, then a warning is emitted. If such check is not possible
383 -- statically (because some sub-aggregate bounds are dynamic expressions)
384 -- then this job is left to the expander. In all cases the particular
385 -- bounds that this function will chose for a given dimension is the first
386 -- N_Range node for a sub-aggregate corresponding to that dimension.
388 -- Note that the Raises_Constraint_Error flag of an array aggregate
389 -- whose evaluation is determined to raise CE by Resolve_Array_Aggregate,
390 -- is set in Resolve_Array_Aggregate but the aggregate is not
391 -- immediately replaced with a raise CE. In fact, Array_Aggr_Subtype must
392 -- first construct the proper itype for the aggregate (Gigi needs
393 -- this). After constructing the proper itype we will eventually replace
394 -- the top-level aggregate with a raise CE (done in Resolve_Aggregate).
395 -- Of course in cases such as:
397 -- type Arr is array (integer range <>) of Integer;
398 -- A : Arr := (positive range -1 .. 2 => 0);
400 -- The bounds of the aggregate itype are cooked up to look reasonable
401 -- (in this particular case the bounds will be 1 .. 2).
403 procedure Make_String_Into_Aggregate (N : Node_Id);
404 -- A string literal can appear in a context in which a one dimensional
405 -- array of characters is expected. This procedure simply rewrites the
406 -- string as an aggregate, prior to resolution.
408 ---------------------------------
409 -- Delta aggregate processing --
410 ---------------------------------
412 procedure Resolve_Delta_Array_Aggregate (N : Node_Id; Typ : Entity_Id);
413 procedure Resolve_Delta_Record_Aggregate (N : Node_Id; Typ : Entity_Id);
415 ------------------------
416 -- Array_Aggr_Subtype --
417 ------------------------
419 function Array_Aggr_Subtype
421 Typ : Entity_Id) return Entity_Id
423 Aggr_Dimension : constant Pos := Number_Dimensions (Typ);
424 -- Number of aggregate index dimensions
426 Aggr_Range : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
427 -- Constrained N_Range of each index dimension in our aggregate itype
429 Aggr_Low : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
430 Aggr_High : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
431 -- Low and High bounds for each index dimension in our aggregate itype
433 Is_Fully_Positional : Boolean := True;
435 procedure Collect_Aggr_Bounds (N : Node_Id; Dim : Pos);
436 -- N is an array (sub-)aggregate. Dim is the dimension corresponding
437 -- to (sub-)aggregate N. This procedure collects and removes the side
438 -- effects of the constrained N_Range nodes corresponding to each index
439 -- dimension of our aggregate itype. These N_Range nodes are collected
440 -- in Aggr_Range above.
442 -- Likewise collect in Aggr_Low & Aggr_High above the low and high
443 -- bounds of each index dimension. If, when collecting, two bounds
444 -- corresponding to the same dimension are static and found to differ,
445 -- then emit a warning, and mark N as raising Constraint_Error.
447 -------------------------
448 -- Collect_Aggr_Bounds --
449 -------------------------
451 procedure Collect_Aggr_Bounds (N : Node_Id; Dim : Pos) is
452 This_Range : constant Node_Id := Aggregate_Bounds (N);
453 -- The aggregate range node of this specific sub-aggregate
455 This_Low : constant Node_Id := Low_Bound (Aggregate_Bounds (N));
456 This_High : constant Node_Id := High_Bound (Aggregate_Bounds (N));
457 -- The aggregate bounds of this specific sub-aggregate
463 Remove_Side_Effects (This_Low, Variable_Ref => True);
464 Remove_Side_Effects (This_High, Variable_Ref => True);
466 -- Collect the first N_Range for a given dimension that you find.
467 -- For a given dimension they must be all equal anyway.
469 if No (Aggr_Range (Dim)) then
470 Aggr_Low (Dim) := This_Low;
471 Aggr_High (Dim) := This_High;
472 Aggr_Range (Dim) := This_Range;
475 if Compile_Time_Known_Value (This_Low) then
476 if not Compile_Time_Known_Value (Aggr_Low (Dim)) then
477 Aggr_Low (Dim) := This_Low;
479 elsif Expr_Value (This_Low) /= Expr_Value (Aggr_Low (Dim)) then
480 Set_Raises_Constraint_Error (N);
481 Error_Msg_Warn := SPARK_Mode /= On;
482 Error_Msg_N ("sub-aggregate low bound mismatch<<", N);
483 Error_Msg_N ("\Constraint_Error [<<", N);
487 if Compile_Time_Known_Value (This_High) then
488 if not Compile_Time_Known_Value (Aggr_High (Dim)) then
489 Aggr_High (Dim) := This_High;
492 Expr_Value (This_High) /= Expr_Value (Aggr_High (Dim))
494 Set_Raises_Constraint_Error (N);
495 Error_Msg_Warn := SPARK_Mode /= On;
496 Error_Msg_N ("sub-aggregate high bound mismatch<<", N);
497 Error_Msg_N ("\Constraint_Error [<<", N);
502 if Dim < Aggr_Dimension then
504 -- Process positional components
506 if Present (Expressions (N)) then
507 Expr := First (Expressions (N));
508 while Present (Expr) loop
509 Collect_Aggr_Bounds (Expr, Dim + 1);
514 -- Process component associations
516 if Present (Component_Associations (N)) then
517 Is_Fully_Positional := False;
519 Assoc := First (Component_Associations (N));
520 while Present (Assoc) loop
521 Expr := Expression (Assoc);
522 Collect_Aggr_Bounds (Expr, Dim + 1);
527 end Collect_Aggr_Bounds;
529 -- Array_Aggr_Subtype variables
532 -- The final itype of the overall aggregate
534 Index_Constraints : constant List_Id := New_List;
535 -- The list of index constraints of the aggregate itype
537 -- Start of processing for Array_Aggr_Subtype
540 -- Make sure that the list of index constraints is properly attached to
541 -- the tree, and then collect the aggregate bounds.
543 Set_Parent (Index_Constraints, N);
544 Collect_Aggr_Bounds (N, 1);
546 -- Build the list of constrained indexes of our aggregate itype
548 for J in 1 .. Aggr_Dimension loop
549 Create_Index : declare
550 Index_Base : constant Entity_Id :=
551 Base_Type (Etype (Aggr_Range (J)));
552 Index_Typ : Entity_Id;
555 -- Construct the Index subtype, and associate it with the range
556 -- construct that generates it.
559 Create_Itype (Subtype_Kind (Ekind (Index_Base)), Aggr_Range (J));
561 Set_Etype (Index_Typ, Index_Base);
563 if Is_Character_Type (Index_Base) then
564 Set_Is_Character_Type (Index_Typ);
567 Set_Size_Info (Index_Typ, (Index_Base));
568 Set_RM_Size (Index_Typ, RM_Size (Index_Base));
569 Set_First_Rep_Item (Index_Typ, First_Rep_Item (Index_Base));
570 Set_Scalar_Range (Index_Typ, Aggr_Range (J));
572 if Is_Discrete_Or_Fixed_Point_Type (Index_Typ) then
573 Set_RM_Size (Index_Typ, UI_From_Int (Minimum_Size (Index_Typ)));
576 Set_Etype (Aggr_Range (J), Index_Typ);
578 Append (Aggr_Range (J), To => Index_Constraints);
582 -- Now build the Itype
584 Itype := Create_Itype (E_Array_Subtype, N);
586 Set_First_Rep_Item (Itype, First_Rep_Item (Typ));
587 Set_Convention (Itype, Convention (Typ));
588 Set_Depends_On_Private (Itype, Has_Private_Component (Typ));
589 Set_Etype (Itype, Base_Type (Typ));
590 Set_Has_Alignment_Clause (Itype, Has_Alignment_Clause (Typ));
591 Set_Is_Aliased (Itype, Is_Aliased (Typ));
592 Set_Is_Independent (Itype, Is_Independent (Typ));
593 Set_Depends_On_Private (Itype, Depends_On_Private (Typ));
595 Copy_Suppress_Status (Index_Check, Typ, Itype);
596 Copy_Suppress_Status (Length_Check, Typ, Itype);
598 Set_First_Index (Itype, First (Index_Constraints));
599 Set_Is_Constrained (Itype, True);
600 Set_Is_Internal (Itype, True);
602 if Has_Predicates (Typ) then
603 Set_Has_Predicates (Itype);
605 -- If the base type has a predicate, capture the predicated parent
606 -- or the existing predicate function for SPARK use.
608 if Present (Predicate_Function (Typ)) then
609 Set_Predicate_Function (Itype, Predicate_Function (Typ));
611 elsif Is_Itype (Typ) then
612 Set_Predicated_Parent (Itype, Predicated_Parent (Typ));
615 Set_Predicated_Parent (Itype, Typ);
619 -- A simple optimization: purely positional aggregates of static
620 -- components should be passed to gigi unexpanded whenever possible, and
621 -- regardless of the staticness of the bounds themselves. Subsequent
622 -- checks in exp_aggr verify that type is not packed, etc.
624 Set_Size_Known_At_Compile_Time
627 and then Comes_From_Source (N)
628 and then Size_Known_At_Compile_Time (Component_Type (Typ)));
630 -- We always need a freeze node for a packed array subtype, so that we
631 -- can build the Packed_Array_Impl_Type corresponding to the subtype. If
632 -- expansion is disabled, the packed array subtype is not built, and we
633 -- must not generate a freeze node for the type, or else it will appear
634 -- incomplete to gigi.
637 and then not In_Spec_Expression
638 and then Expander_Active
640 Freeze_Itype (Itype, N);
644 end Array_Aggr_Subtype;
646 --------------------------------
647 -- Check_Misspelled_Component --
648 --------------------------------
650 procedure Check_Misspelled_Component
651 (Elements : Elist_Id;
654 Max_Suggestions : constant := 2;
656 Nr_Of_Suggestions : Natural := 0;
657 Suggestion_1 : Entity_Id := Empty;
658 Suggestion_2 : Entity_Id := Empty;
659 Component_Elmt : Elmt_Id;
662 -- All the components of List are matched against Component and a count
663 -- is maintained of possible misspellings. When at the end of the
664 -- analysis there are one or two (not more) possible misspellings,
665 -- these misspellings will be suggested as possible corrections.
667 Component_Elmt := First_Elmt (Elements);
668 while Nr_Of_Suggestions <= Max_Suggestions
669 and then Present (Component_Elmt)
671 if Is_Bad_Spelling_Of
672 (Chars (Node (Component_Elmt)),
675 Nr_Of_Suggestions := Nr_Of_Suggestions + 1;
677 case Nr_Of_Suggestions is
678 when 1 => Suggestion_1 := Node (Component_Elmt);
679 when 2 => Suggestion_2 := Node (Component_Elmt);
684 Next_Elmt (Component_Elmt);
687 -- Report at most two suggestions
689 if Nr_Of_Suggestions = 1 then
690 Error_Msg_NE -- CODEFIX
691 ("\possible misspelling of&", Component, Suggestion_1);
693 elsif Nr_Of_Suggestions = 2 then
694 Error_Msg_Node_2 := Suggestion_2;
695 Error_Msg_NE -- CODEFIX
696 ("\possible misspelling of& or&", Component, Suggestion_1);
698 end Check_Misspelled_Component;
700 ----------------------------------------
701 -- Check_Expr_OK_In_Limited_Aggregate --
702 ----------------------------------------
704 procedure Check_Expr_OK_In_Limited_Aggregate (Expr : Node_Id) is
706 if Is_Limited_Type (Etype (Expr))
707 and then Comes_From_Source (Expr)
709 if In_Instance_Body or else In_Inlined_Body then
712 elsif not OK_For_Limited_Init (Etype (Expr), Expr) then
714 ("initialization not allowed for limited types", Expr);
715 Explain_Limited_Type (Etype (Expr), Expr);
718 end Check_Expr_OK_In_Limited_Aggregate;
720 -------------------------
721 -- Is_Others_Aggregate --
722 -------------------------
724 function Is_Others_Aggregate (Aggr : Node_Id) return Boolean is
725 Assoc : constant List_Id := Component_Associations (Aggr);
728 return No (Expressions (Aggr))
729 and then Nkind (First (Choice_List (First (Assoc)))) = N_Others_Choice;
730 end Is_Others_Aggregate;
732 -------------------------
733 -- Is_Single_Aggregate --
734 -------------------------
736 function Is_Single_Aggregate (Aggr : Node_Id) return Boolean is
737 Assoc : constant List_Id := Component_Associations (Aggr);
740 return No (Expressions (Aggr))
741 and then No (Next (First (Assoc)))
742 and then No (Next (First (Choice_List (First (Assoc)))));
743 end Is_Single_Aggregate;
745 --------------------------------
746 -- Make_String_Into_Aggregate --
747 --------------------------------
749 procedure Make_String_Into_Aggregate (N : Node_Id) is
750 Exprs : constant List_Id := New_List;
751 Loc : constant Source_Ptr := Sloc (N);
752 Str : constant String_Id := Strval (N);
753 Strlen : constant Nat := String_Length (Str);
761 for J in 1 .. Strlen loop
762 C := Get_String_Char (Str, J);
763 Set_Character_Literal_Name (C);
766 Make_Character_Literal (P,
768 Char_Literal_Value => UI_From_CC (C));
769 Set_Etype (C_Node, Any_Character);
770 Append_To (Exprs, C_Node);
773 -- Something special for wide strings???
776 New_N := Make_Aggregate (Loc, Expressions => Exprs);
777 Set_Analyzed (New_N);
778 Set_Etype (New_N, Any_Composite);
781 end Make_String_Into_Aggregate;
783 -----------------------
784 -- Resolve_Aggregate --
785 -----------------------
787 procedure Resolve_Aggregate (N : Node_Id; Typ : Entity_Id) is
788 Loc : constant Source_Ptr := Sloc (N);
790 Aggr_Subtyp : Entity_Id;
791 -- The actual aggregate subtype. This is not necessarily the same as Typ
792 -- which is the subtype of the context in which the aggregate was found.
795 -- Ignore junk empty aggregate resulting from parser error
797 if No (Expressions (N))
798 and then No (Component_Associations (N))
799 and then not Null_Record_Present (N)
804 -- If the aggregate has box-initialized components, its type must be
805 -- frozen so that initialization procedures can properly be called
806 -- in the resolution that follows. The replacement of boxes with
807 -- initialization calls is properly an expansion activity but it must
808 -- be done during resolution.
811 and then Present (Component_Associations (N))
817 Comp := First (Component_Associations (N));
818 while Present (Comp) loop
819 if Box_Present (Comp) then
820 Insert_Actions (N, Freeze_Entity (Typ, N));
829 -- Check for aggregates not allowed in configurable run-time mode.
830 -- We allow all cases of aggregates that do not come from source, since
831 -- these are all assumed to be small (e.g. bounds of a string literal).
832 -- We also allow aggregates of types we know to be small.
834 if not Support_Aggregates_On_Target
835 and then Comes_From_Source (N)
836 and then (not Known_Static_Esize (Typ)
837 or else Esize (Typ) > System_Max_Integer_Size)
839 Error_Msg_CRT ("aggregate", N);
842 -- Ada 2005 (AI-287): Limited aggregates allowed
844 -- In an instance, ignore aggregate subcomponents tnat may be limited,
845 -- because they originate in view conflicts. If the original aggregate
846 -- is legal and the actuals are legal, the aggregate itself is legal.
848 if Is_Limited_Type (Typ)
849 and then Ada_Version < Ada_2005
850 and then not In_Instance
852 Error_Msg_N ("aggregate type cannot be limited", N);
853 Explain_Limited_Type (Typ, N);
855 elsif Is_Class_Wide_Type (Typ) then
856 Error_Msg_N ("type of aggregate cannot be class-wide", N);
858 elsif Typ = Any_String
859 or else Typ = Any_Composite
861 Error_Msg_N ("no unique type for aggregate", N);
862 Set_Etype (N, Any_Composite);
864 elsif Is_Array_Type (Typ) and then Null_Record_Present (N) then
865 Error_Msg_N ("null record forbidden in array aggregate", N);
867 elsif Is_Record_Type (Typ) then
868 Resolve_Record_Aggregate (N, Typ);
870 elsif Is_Array_Type (Typ) then
872 -- First a special test, for the case of a positional aggregate of
873 -- characters which can be replaced by a string literal.
875 -- Do not perform this transformation if this was a string literal
876 -- to start with, whose components needed constraint checks, or if
877 -- the component type is non-static, because it will require those
878 -- checks and be transformed back into an aggregate. If the index
879 -- type is not Integer the aggregate may represent a user-defined
880 -- string type but the context might need the original type so we
881 -- do not perform the transformation at this point.
883 if Number_Dimensions (Typ) = 1
884 and then Is_Standard_Character_Type (Component_Type (Typ))
885 and then No (Component_Associations (N))
886 and then not Is_Limited_Composite (Typ)
887 and then not Is_Private_Composite (Typ)
888 and then not Is_Bit_Packed_Array (Typ)
889 and then Nkind (Original_Node (Parent (N))) /= N_String_Literal
890 and then Is_OK_Static_Subtype (Component_Type (Typ))
891 and then Base_Type (Etype (First_Index (Typ))) =
892 Base_Type (Standard_Integer)
898 Expr := First (Expressions (N));
899 while Present (Expr) loop
900 exit when Nkind (Expr) /= N_Character_Literal;
907 Expr := First (Expressions (N));
908 while Present (Expr) loop
909 Store_String_Char (UI_To_CC (Char_Literal_Value (Expr)));
913 Rewrite (N, Make_String_Literal (Loc, End_String));
915 Analyze_And_Resolve (N, Typ);
921 -- Here if we have a real aggregate to deal with
923 Array_Aggregate : declare
924 Aggr_Resolved : Boolean;
926 Aggr_Typ : constant Entity_Id := Etype (Typ);
927 -- This is the unconstrained array type, which is the type against
928 -- which the aggregate is to be resolved. Typ itself is the array
929 -- type of the context which may not be the same subtype as the
930 -- subtype for the final aggregate.
933 -- In the following we determine whether an OTHERS choice is
934 -- allowed inside the array aggregate. The test checks the context
935 -- in which the array aggregate occurs. If the context does not
936 -- permit it, or the aggregate type is unconstrained, an OTHERS
937 -- choice is not allowed (except that it is always allowed on the
938 -- right-hand side of an assignment statement; in this case the
939 -- constrainedness of the type doesn't matter, because an array
940 -- object is always constrained).
942 -- If expansion is disabled (generic context, or semantics-only
943 -- mode) actual subtypes cannot be constructed, and the type of an
944 -- object may be its unconstrained nominal type. However, if the
945 -- context is an assignment statement, OTHERS is allowed, because
946 -- the target of the assignment will have a constrained subtype
947 -- when fully compiled. Ditto if the context is an initialization
948 -- procedure where a component may have a predicate function that
949 -- carries the base type.
951 -- Note that there is no node for Explicit_Actual_Parameter.
952 -- To test for this context we therefore have to test for node
953 -- N_Parameter_Association which itself appears only if there is a
954 -- formal parameter. Consequently we also need to test for
955 -- N_Procedure_Call_Statement or N_Function_Call.
957 -- The context may be an N_Reference node, created by expansion.
958 -- Legality of the others clause was established in the source,
959 -- so the context is legal.
961 Set_Etype (N, Aggr_Typ); -- May be overridden later on
963 if Nkind (Parent (N)) = N_Assignment_Statement
964 or else Inside_Init_Proc
965 or else (Is_Constrained (Typ)
966 and then Nkind (Parent (N)) in
967 N_Parameter_Association
969 | N_Procedure_Call_Statement
970 | N_Generic_Association
971 | N_Formal_Object_Declaration
972 | N_Simple_Return_Statement
973 | N_Object_Declaration
974 | N_Component_Declaration
975 | N_Parameter_Specification
976 | N_Qualified_Expression
979 | N_Extension_Aggregate
980 | N_Component_Association
981 | N_Case_Expression_Alternative
983 | N_Expression_With_Actions)
986 Resolve_Array_Aggregate
988 Index => First_Index (Aggr_Typ),
989 Index_Constr => First_Index (Typ),
990 Component_Typ => Component_Type (Typ),
991 Others_Allowed => True);
994 Resolve_Array_Aggregate
996 Index => First_Index (Aggr_Typ),
997 Index_Constr => First_Index (Aggr_Typ),
998 Component_Typ => Component_Type (Typ),
999 Others_Allowed => False);
1002 if not Aggr_Resolved then
1004 -- A parenthesized expression may have been intended as an
1005 -- aggregate, leading to a type error when analyzing the
1006 -- component. This can also happen for a nested component
1007 -- (see Analyze_Aggr_Expr).
1009 if Paren_Count (N) > 0 then
1011 ("positional aggregate cannot have one component", N);
1014 Aggr_Subtyp := Any_Composite;
1017 Aggr_Subtyp := Array_Aggr_Subtype (N, Typ);
1020 Set_Etype (N, Aggr_Subtyp);
1021 end Array_Aggregate;
1023 elsif Is_Private_Type (Typ)
1024 and then Present (Full_View (Typ))
1025 and then (In_Inlined_Body or In_Instance_Body)
1026 and then Is_Composite_Type (Full_View (Typ))
1028 Resolve (N, Full_View (Typ));
1031 Error_Msg_N ("illegal context for aggregate", N);
1034 -- If we can determine statically that the evaluation of the aggregate
1035 -- raises Constraint_Error, then replace the aggregate with an
1036 -- N_Raise_Constraint_Error node, but set the Etype to the right
1037 -- aggregate subtype. Gigi needs this.
1039 if Raises_Constraint_Error (N) then
1040 Aggr_Subtyp := Etype (N);
1042 Make_Raise_Constraint_Error (Loc, Reason => CE_Range_Check_Failed));
1043 Set_Raises_Constraint_Error (N);
1044 Set_Etype (N, Aggr_Subtyp);
1048 Check_Function_Writable_Actuals (N);
1049 end Resolve_Aggregate;
1051 -----------------------------
1052 -- Resolve_Array_Aggregate --
1053 -----------------------------
1055 function Resolve_Array_Aggregate
1058 Index_Constr : Node_Id;
1059 Component_Typ : Entity_Id;
1060 Others_Allowed : Boolean) return Boolean
1062 Loc : constant Source_Ptr := Sloc (N);
1064 Failure : constant Boolean := False;
1065 Success : constant Boolean := True;
1067 Index_Typ : constant Entity_Id := Etype (Index);
1068 Index_Typ_Low : constant Node_Id := Type_Low_Bound (Index_Typ);
1069 Index_Typ_High : constant Node_Id := Type_High_Bound (Index_Typ);
1070 -- The type of the index corresponding to the array sub-aggregate along
1071 -- with its low and upper bounds.
1073 Index_Base : constant Entity_Id := Base_Type (Index_Typ);
1074 Index_Base_Low : constant Node_Id := Type_Low_Bound (Index_Base);
1075 Index_Base_High : constant Node_Id := Type_High_Bound (Index_Base);
1076 -- Ditto for the base type
1078 Others_Present : Boolean := False;
1080 Nb_Choices : Nat := 0;
1081 -- Contains the overall number of named choices in this sub-aggregate
1083 function Add (Val : Uint; To : Node_Id) return Node_Id;
1084 -- Creates a new expression node where Val is added to expression To.
1085 -- Tries to constant fold whenever possible. To must be an already
1086 -- analyzed expression.
1088 procedure Check_Bound (BH : Node_Id; AH : in out Node_Id);
1089 -- Checks that AH (the upper bound of an array aggregate) is less than
1090 -- or equal to BH (the upper bound of the index base type). If the check
1091 -- fails, a warning is emitted, the Raises_Constraint_Error flag of N is
1092 -- set, and AH is replaced with a duplicate of BH.
1094 procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id);
1095 -- Checks that range AL .. AH is compatible with range L .. H. Emits a
1096 -- warning if not and sets the Raises_Constraint_Error flag in N.
1098 procedure Check_Length (L, H : Node_Id; Len : Uint);
1099 -- Checks that range L .. H contains at least Len elements. Emits a
1100 -- warning if not and sets the Raises_Constraint_Error flag in N.
1102 function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean;
1103 -- Returns True if range L .. H is dynamic or null
1105 procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean);
1106 -- Given expression node From, this routine sets OK to False if it
1107 -- cannot statically evaluate From. Otherwise it stores this static
1108 -- value into Value.
1110 function Resolve_Aggr_Expr
1112 Single_Elmt : Boolean) return Boolean;
1113 -- Resolves aggregate expression Expr. Returns False if resolution
1114 -- fails. If Single_Elmt is set to False, the expression Expr may be
1115 -- used to initialize several array aggregate elements (this can happen
1116 -- for discrete choices such as "L .. H => Expr" or the OTHERS choice).
1117 -- In this event we do not resolve Expr unless expansion is disabled.
1118 -- To know why, see the DELAYED COMPONENT RESOLUTION note above.
1120 -- NOTE: In the case of "... => <>", we pass the in the
1121 -- N_Component_Association node as Expr, since there is no Expression in
1122 -- that case, and we need a Sloc for the error message.
1124 procedure Resolve_Iterated_Component_Association
1126 Index_Typ : Entity_Id);
1133 function Add (Val : Uint; To : Node_Id) return Node_Id is
1139 if Raises_Constraint_Error (To) then
1143 -- First test if we can do constant folding
1145 if Compile_Time_Known_Value (To)
1146 or else Nkind (To) = N_Integer_Literal
1148 Expr_Pos := Make_Integer_Literal (Loc, Expr_Value (To) + Val);
1149 Set_Is_Static_Expression (Expr_Pos);
1150 Set_Etype (Expr_Pos, Etype (To));
1151 Set_Analyzed (Expr_Pos, Analyzed (To));
1153 if not Is_Enumeration_Type (Index_Typ) then
1156 -- If we are dealing with enumeration return
1157 -- Index_Typ'Val (Expr_Pos)
1161 Make_Attribute_Reference
1163 Prefix => New_Occurrence_Of (Index_Typ, Loc),
1164 Attribute_Name => Name_Val,
1165 Expressions => New_List (Expr_Pos));
1171 -- If we are here no constant folding possible
1173 if not Is_Enumeration_Type (Index_Base) then
1176 Left_Opnd => Duplicate_Subexpr (To),
1177 Right_Opnd => Make_Integer_Literal (Loc, Val));
1179 -- If we are dealing with enumeration return
1180 -- Index_Typ'Val (Index_Typ'Pos (To) + Val)
1184 Make_Attribute_Reference
1186 Prefix => New_Occurrence_Of (Index_Typ, Loc),
1187 Attribute_Name => Name_Pos,
1188 Expressions => New_List (Duplicate_Subexpr (To)));
1192 Left_Opnd => To_Pos,
1193 Right_Opnd => Make_Integer_Literal (Loc, Val));
1196 Make_Attribute_Reference
1198 Prefix => New_Occurrence_Of (Index_Typ, Loc),
1199 Attribute_Name => Name_Val,
1200 Expressions => New_List (Expr_Pos));
1202 -- If the index type has a non standard representation, the
1203 -- attributes 'Val and 'Pos expand into function calls and the
1204 -- resulting expression is considered non-safe for reevaluation
1205 -- by the backend. Relocate it into a constant temporary in order
1206 -- to make it safe for reevaluation.
1208 if Has_Non_Standard_Rep (Etype (N)) then
1213 Def_Id := Make_Temporary (Loc, 'R', Expr);
1214 Set_Etype (Def_Id, Index_Typ);
1216 Make_Object_Declaration (Loc,
1217 Defining_Identifier => Def_Id,
1218 Object_Definition =>
1219 New_Occurrence_Of (Index_Typ, Loc),
1220 Constant_Present => True,
1221 Expression => Relocate_Node (Expr)));
1223 Expr := New_Occurrence_Of (Def_Id, Loc);
1235 procedure Check_Bound (BH : Node_Id; AH : in out Node_Id) is
1243 Get (Value => Val_BH, From => BH, OK => OK_BH);
1244 Get (Value => Val_AH, From => AH, OK => OK_AH);
1246 if OK_BH and then OK_AH and then Val_BH < Val_AH then
1247 Set_Raises_Constraint_Error (N);
1248 Error_Msg_Warn := SPARK_Mode /= On;
1249 Error_Msg_N ("upper bound out of range<<", AH);
1250 Error_Msg_N ("\Constraint_Error [<<", AH);
1252 -- You need to set AH to BH or else in the case of enumerations
1253 -- indexes we will not be able to resolve the aggregate bounds.
1255 AH := Duplicate_Subexpr (BH);
1263 procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id) is
1274 pragma Warnings (Off, OK_AL);
1275 pragma Warnings (Off, OK_AH);
1278 if Raises_Constraint_Error (N)
1279 or else Dynamic_Or_Null_Range (AL, AH)
1284 Get (Value => Val_L, From => L, OK => OK_L);
1285 Get (Value => Val_H, From => H, OK => OK_H);
1287 Get (Value => Val_AL, From => AL, OK => OK_AL);
1288 Get (Value => Val_AH, From => AH, OK => OK_AH);
1290 if OK_L and then Val_L > Val_AL then
1291 Set_Raises_Constraint_Error (N);
1292 Error_Msg_Warn := SPARK_Mode /= On;
1293 Error_Msg_N ("lower bound of aggregate out of range<<", N);
1294 Error_Msg_N ("\Constraint_Error [<<", N);
1297 if OK_H and then Val_H < Val_AH then
1298 Set_Raises_Constraint_Error (N);
1299 Error_Msg_Warn := SPARK_Mode /= On;
1300 Error_Msg_N ("upper bound of aggregate out of range<<", N);
1301 Error_Msg_N ("\Constraint_Error [<<", N);
1309 procedure Check_Length (L, H : Node_Id; Len : Uint) is
1319 if Raises_Constraint_Error (N) then
1323 Get (Value => Val_L, From => L, OK => OK_L);
1324 Get (Value => Val_H, From => H, OK => OK_H);
1326 if not OK_L or else not OK_H then
1330 -- If null range length is zero
1332 if Val_L > Val_H then
1333 Range_Len := Uint_0;
1335 Range_Len := Val_H - Val_L + 1;
1338 if Range_Len < Len then
1339 Set_Raises_Constraint_Error (N);
1340 Error_Msg_Warn := SPARK_Mode /= On;
1341 Error_Msg_N ("too many elements<<", N);
1342 Error_Msg_N ("\Constraint_Error [<<", N);
1346 ---------------------------
1347 -- Dynamic_Or_Null_Range --
1348 ---------------------------
1350 function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean is
1358 Get (Value => Val_L, From => L, OK => OK_L);
1359 Get (Value => Val_H, From => H, OK => OK_H);
1361 return not OK_L or else not OK_H
1362 or else not Is_OK_Static_Expression (L)
1363 or else not Is_OK_Static_Expression (H)
1364 or else Val_L > Val_H;
1365 end Dynamic_Or_Null_Range;
1371 procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean) is
1375 if Compile_Time_Known_Value (From) then
1376 Value := Expr_Value (From);
1378 -- If expression From is something like Some_Type'Val (10) then
1381 elsif Nkind (From) = N_Attribute_Reference
1382 and then Attribute_Name (From) = Name_Val
1383 and then Compile_Time_Known_Value (First (Expressions (From)))
1385 Value := Expr_Value (First (Expressions (From)));
1392 -----------------------
1393 -- Resolve_Aggr_Expr --
1394 -----------------------
1396 function Resolve_Aggr_Expr
1398 Single_Elmt : Boolean) return Boolean
1400 Nxt_Ind : constant Node_Id := Next_Index (Index);
1401 Nxt_Ind_Constr : constant Node_Id := Next_Index (Index_Constr);
1402 -- Index is the current index corresponding to the expression
1404 Resolution_OK : Boolean := True;
1405 -- Set to False if resolution of the expression failed
1408 -- Defend against previous errors
1410 if Nkind (Expr) = N_Error
1411 or else Error_Posted (Expr)
1416 -- If the array type against which we are resolving the aggregate
1417 -- has several dimensions, the expressions nested inside the
1418 -- aggregate must be further aggregates (or strings).
1420 if Present (Nxt_Ind) then
1421 if Nkind (Expr) /= N_Aggregate then
1423 -- A string literal can appear where a one-dimensional array
1424 -- of characters is expected. If the literal looks like an
1425 -- operator, it is still an operator symbol, which will be
1426 -- transformed into a string when analyzed.
1428 if Is_Character_Type (Component_Typ)
1429 and then No (Next_Index (Nxt_Ind))
1430 and then Nkind (Expr) in N_String_Literal | N_Operator_Symbol
1432 -- A string literal used in a multidimensional array
1433 -- aggregate in place of the final one-dimensional
1434 -- aggregate must not be enclosed in parentheses.
1436 if Paren_Count (Expr) /= 0 then
1437 Error_Msg_N ("no parenthesis allowed here", Expr);
1440 Make_String_Into_Aggregate (Expr);
1443 Error_Msg_N ("nested array aggregate expected", Expr);
1445 -- If the expression is parenthesized, this may be
1446 -- a missing component association for a 1-aggregate.
1448 if Paren_Count (Expr) > 0 then
1450 ("\if single-component aggregate is intended, "
1451 & "write e.g. (1 ='> ...)", Expr);
1458 -- If it's "... => <>", nothing to resolve
1460 if Nkind (Expr) = N_Component_Association then
1461 pragma Assert (Box_Present (Expr));
1465 -- Ada 2005 (AI-231): Propagate the type to the nested aggregate.
1466 -- Required to check the null-exclusion attribute (if present).
1467 -- This value may be overridden later on.
1469 Set_Etype (Expr, Etype (N));
1471 Resolution_OK := Resolve_Array_Aggregate
1472 (Expr, Nxt_Ind, Nxt_Ind_Constr, Component_Typ, Others_Allowed);
1475 -- If it's "... => <>", nothing to resolve
1477 if Nkind (Expr) = N_Component_Association then
1478 pragma Assert (Box_Present (Expr));
1482 -- Do not resolve the expressions of discrete or others choices
1483 -- unless the expression covers a single component, or the
1484 -- expander is inactive.
1486 -- In SPARK mode, expressions that can perform side effects will
1487 -- be recognized by the gnat2why back-end, and the whole
1488 -- subprogram will be ignored. So semantic analysis can be
1489 -- performed safely.
1492 or else not Expander_Active
1493 or else In_Spec_Expression
1495 Analyze_And_Resolve (Expr, Component_Typ);
1496 Check_Expr_OK_In_Limited_Aggregate (Expr);
1497 Check_Non_Static_Context (Expr);
1498 Aggregate_Constraint_Checks (Expr, Component_Typ);
1499 Check_Unset_Reference (Expr);
1503 -- If an aggregate component has a type with predicates, an explicit
1504 -- predicate check must be applied, as for an assignment statement,
1505 -- because the aggregate might not be expanded into individual
1506 -- component assignments. If the expression covers several components
1507 -- the analysis and the predicate check take place later.
1509 if Has_Predicates (Component_Typ)
1510 and then Analyzed (Expr)
1512 Apply_Predicate_Check (Expr, Component_Typ);
1515 if Raises_Constraint_Error (Expr)
1516 and then Nkind (Parent (Expr)) /= N_Component_Association
1518 Set_Raises_Constraint_Error (N);
1521 -- If the expression has been marked as requiring a range check,
1522 -- then generate it here. It's a bit odd to be generating such
1523 -- checks in the analyzer, but harmless since Generate_Range_Check
1524 -- does nothing (other than making sure Do_Range_Check is set) if
1525 -- the expander is not active.
1527 if Do_Range_Check (Expr) then
1528 Generate_Range_Check (Expr, Component_Typ, CE_Range_Check_Failed);
1531 return Resolution_OK;
1532 end Resolve_Aggr_Expr;
1534 --------------------------------------------
1535 -- Resolve_Iterated_Component_Association --
1536 --------------------------------------------
1538 procedure Resolve_Iterated_Component_Association
1540 Index_Typ : Entity_Id)
1542 Loc : constant Source_Ptr := Sloc (N);
1551 -- An element iterator specification cannot appear in
1552 -- an array aggregate because it does not provide index
1553 -- values for the association. This must be a semantic
1554 -- check because the parser cannot tell whether this is
1555 -- an array aggregate or a container aggregate.
1557 if Present (Iterator_Specification (N)) then
1558 Error_Msg_N ("container element Iterator cannot appear "
1559 & "in an array aggregate", N);
1563 Choice := First (Discrete_Choices (N));
1565 while Present (Choice) loop
1566 if Nkind (Choice) = N_Others_Choice then
1567 Others_Present := True;
1572 -- Choice can be a subtype name, a range, or an expression
1574 if Is_Entity_Name (Choice)
1575 and then Is_Type (Entity (Choice))
1576 and then Base_Type (Entity (Choice)) = Base_Type (Index_Typ)
1581 Analyze_And_Resolve (Choice, Index_Typ);
1588 -- Create a scope in which to introduce an index, which is usually
1589 -- visible in the expression for the component, and needed for its
1592 Ent := New_Internal_Entity (E_Loop, Current_Scope, Loc, 'L');
1593 Set_Etype (Ent, Standard_Void_Type);
1594 Set_Parent (Ent, Parent (N));
1597 Make_Defining_Identifier (Loc,
1598 Chars => Chars (Defining_Identifier (N)));
1600 -- Insert and decorate the index variable in the current scope.
1601 -- The expression has to be analyzed once the index variable is
1602 -- directly visible. Mark the variable as referenced to prevent
1603 -- spurious warnings, given that subsequent uses of its name in the
1604 -- expression will reference the internal (synonym) loop variable.
1607 Set_Etype (Id, Index_Typ);
1608 Set_Ekind (Id, E_Variable);
1609 Set_Scope (Id, Ent);
1610 Set_Referenced (Id);
1612 -- Analyze a copy of the expression, to verify legality. We use
1613 -- a copy because the expression will be analyzed anew when the
1614 -- enclosing aggregate is expanded, and the construct is rewritten
1615 -- as a loop with a new index variable.
1617 Expr := New_Copy_Tree (Expression (N));
1618 Dummy := Resolve_Aggr_Expr (Expr, False);
1620 -- An iterated_component_association may appear in a nested
1621 -- aggregate for a multidimensional structure: preserve the bounds
1622 -- computed for the expression, as well as the anonymous array
1623 -- type generated for it; both are needed during array expansion.
1624 -- This does not work for more than two levels of nesting. ???
1626 if Nkind (Expr) = N_Aggregate then
1627 Set_Aggregate_Bounds (Expression (N), Aggregate_Bounds (Expr));
1628 Set_Etype (Expression (N), Etype (Expr));
1632 end Resolve_Iterated_Component_Association;
1641 Aggr_Low : Node_Id := Empty;
1642 Aggr_High : Node_Id := Empty;
1643 -- The actual low and high bounds of this sub-aggregate
1645 Case_Table_Size : Nat;
1646 -- Contains the size of the case table needed to sort aggregate choices
1648 Choices_Low : Node_Id := Empty;
1649 Choices_High : Node_Id := Empty;
1650 -- The lowest and highest discrete choices values for a named aggregate
1652 Delete_Choice : Boolean;
1653 -- Used when replacing a subtype choice with predicate by a list
1655 Nb_Elements : Uint := Uint_0;
1656 -- The number of elements in a positional aggregate
1658 Nb_Discrete_Choices : Nat := 0;
1659 -- The overall number of discrete choices (not counting others choice)
1661 -- Start of processing for Resolve_Array_Aggregate
1664 -- Ignore junk empty aggregate resulting from parser error
1666 if No (Expressions (N))
1667 and then No (Component_Associations (N))
1668 and then not Null_Record_Present (N)
1673 -- STEP 1: make sure the aggregate is correctly formatted
1675 if Present (Component_Associations (N)) then
1676 Assoc := First (Component_Associations (N));
1677 while Present (Assoc) loop
1678 if Nkind (Assoc) = N_Iterated_Component_Association then
1679 Resolve_Iterated_Component_Association (Assoc, Index_Typ);
1682 Choice := First (Choice_List (Assoc));
1683 Delete_Choice := False;
1684 while Present (Choice) loop
1685 if Nkind (Choice) = N_Others_Choice then
1686 Others_Present := True;
1688 if Choice /= First (Choice_List (Assoc))
1689 or else Present (Next (Choice))
1692 ("OTHERS must appear alone in a choice list", Choice);
1696 if Present (Next (Assoc)) then
1698 ("OTHERS must appear last in an aggregate", Choice);
1702 if Ada_Version = Ada_83
1703 and then Assoc /= First (Component_Associations (N))
1704 and then Nkind (Parent (N)) in
1705 N_Assignment_Statement | N_Object_Declaration
1708 ("(Ada 83) illegal context for OTHERS choice", N);
1711 elsif Is_Entity_Name (Choice) then
1715 E : constant Entity_Id := Entity (Choice);
1721 if Is_Type (E) and then Has_Predicates (E) then
1722 Freeze_Before (N, E);
1724 if Has_Dynamic_Predicate_Aspect (E) then
1726 ("subtype& has dynamic predicate, not allowed "
1727 & "in aggregate choice", Choice, E);
1729 elsif not Is_OK_Static_Subtype (E) then
1731 ("non-static subtype& has predicate, not allowed "
1732 & "in aggregate choice", Choice, E);
1735 -- If the subtype has a static predicate, replace the
1736 -- original choice with the list of individual values
1737 -- covered by the predicate.
1738 -- This should be deferred to expansion time ???
1740 if Present (Static_Discrete_Predicate (E)) then
1741 Delete_Choice := True;
1744 P := First (Static_Discrete_Predicate (E));
1745 while Present (P) loop
1747 Set_Sloc (C, Sloc (Choice));
1748 Append_To (New_Cs, C);
1752 Insert_List_After (Choice, New_Cs);
1758 Nb_Choices := Nb_Choices + 1;
1761 C : constant Node_Id := Choice;
1766 if Delete_Choice then
1768 Nb_Choices := Nb_Choices - 1;
1769 Delete_Choice := False;
1778 -- At this point we know that the others choice, if present, is by
1779 -- itself and appears last in the aggregate. Check if we have mixed
1780 -- positional and discrete associations (other than the others choice).
1782 if Present (Expressions (N))
1783 and then (Nb_Choices > 1
1784 or else (Nb_Choices = 1 and then not Others_Present))
1787 ("named association cannot follow positional association",
1788 First (Choice_List (First (Component_Associations (N)))));
1792 -- Test for the validity of an others choice if present
1794 if Others_Present and then not Others_Allowed then
1796 ("OTHERS choice not allowed here",
1797 First (Choices (First (Component_Associations (N)))));
1801 -- Protect against cascaded errors
1803 if Etype (Index_Typ) = Any_Type then
1807 -- STEP 2: Process named components
1809 if No (Expressions (N)) then
1810 if Others_Present then
1811 Case_Table_Size := Nb_Choices - 1;
1813 Case_Table_Size := Nb_Choices;
1817 function Empty_Range (A : Node_Id) return Boolean;
1818 -- If an association covers an empty range, some warnings on the
1819 -- expression of the association can be disabled.
1825 function Empty_Range (A : Node_Id) return Boolean is
1826 R : constant Node_Id := First (Choices (A));
1828 return No (Next (R))
1829 and then Nkind (R) = N_Range
1830 and then Compile_Time_Compare
1831 (Low_Bound (R), High_Bound (R), False) = GT;
1838 -- Denote the lowest and highest values in an aggregate choice
1840 S_Low : Node_Id := Empty;
1841 S_High : Node_Id := Empty;
1842 -- if a choice in an aggregate is a subtype indication these
1843 -- denote the lowest and highest values of the subtype
1845 Table : Case_Table_Type (1 .. Case_Table_Size);
1846 -- Used to sort all the different choice values
1848 Single_Choice : Boolean;
1849 -- Set to true every time there is a single discrete choice in a
1850 -- discrete association
1852 Prev_Nb_Discrete_Choices : Nat;
1853 -- Used to keep track of the number of discrete choices in the
1854 -- current association.
1856 Errors_Posted_On_Choices : Boolean := False;
1857 -- Keeps track of whether any choices have semantic errors
1859 -- Start of processing for Step_2
1862 -- STEP 2 (A): Check discrete choices validity
1864 Assoc := First (Component_Associations (N));
1865 while Present (Assoc) loop
1866 Prev_Nb_Discrete_Choices := Nb_Discrete_Choices;
1867 Choice := First (Choice_List (Assoc));
1872 if Nkind (Choice) = N_Others_Choice then
1873 Single_Choice := False;
1876 -- Test for subtype mark without constraint
1878 elsif Is_Entity_Name (Choice) and then
1879 Is_Type (Entity (Choice))
1881 if Base_Type (Entity (Choice)) /= Index_Base then
1883 ("invalid subtype mark in aggregate choice",
1888 -- Case of subtype indication
1890 elsif Nkind (Choice) = N_Subtype_Indication then
1891 Resolve_Discrete_Subtype_Indication (Choice, Index_Base);
1893 if Has_Dynamic_Predicate_Aspect
1894 (Entity (Subtype_Mark (Choice)))
1897 ("subtype& has dynamic predicate, "
1898 & "not allowed in aggregate choice",
1899 Choice, Entity (Subtype_Mark (Choice)));
1902 -- Does the subtype indication evaluation raise CE?
1904 Get_Index_Bounds (Subtype_Mark (Choice), S_Low, S_High);
1905 Get_Index_Bounds (Choice, Low, High);
1906 Check_Bounds (S_Low, S_High, Low, High);
1908 -- Case of range or expression
1911 Resolve (Choice, Index_Base);
1912 Check_Unset_Reference (Choice);
1913 Check_Non_Static_Context (Choice);
1915 -- If semantic errors were posted on the choice, then
1916 -- record that for possible early return from later
1917 -- processing (see handling of enumeration choices).
1919 if Error_Posted (Choice) then
1920 Errors_Posted_On_Choices := True;
1923 -- Do not range check a choice. This check is redundant
1924 -- since this test is already done when we check that the
1925 -- bounds of the array aggregate are within range.
1927 Set_Do_Range_Check (Choice, False);
1930 -- If we could not resolve the discrete choice stop here
1932 if Etype (Choice) = Any_Type then
1935 -- If the discrete choice raises CE get its original bounds
1937 elsif Nkind (Choice) = N_Raise_Constraint_Error then
1938 Set_Raises_Constraint_Error (N);
1939 Get_Index_Bounds (Original_Node (Choice), Low, High);
1941 -- Otherwise get its bounds as usual
1944 Get_Index_Bounds (Choice, Low, High);
1947 if (Dynamic_Or_Null_Range (Low, High)
1948 or else (Nkind (Choice) = N_Subtype_Indication
1950 Dynamic_Or_Null_Range (S_Low, S_High)))
1951 and then Nb_Choices /= 1
1954 ("dynamic or empty choice in aggregate "
1955 & "must be the only choice", Choice);
1959 if not (All_Composite_Constraints_Static (Low)
1960 and then All_Composite_Constraints_Static (High)
1961 and then All_Composite_Constraints_Static (S_Low)
1962 and then All_Composite_Constraints_Static (S_High))
1964 Check_Restriction (No_Dynamic_Sized_Objects, Choice);
1967 Nb_Discrete_Choices := Nb_Discrete_Choices + 1;
1968 Table (Nb_Discrete_Choices).Lo := Low;
1969 Table (Nb_Discrete_Choices).Hi := High;
1970 Table (Nb_Discrete_Choices).Choice := Choice;
1976 -- Check if we have a single discrete choice and whether
1977 -- this discrete choice specifies a single value.
1980 (Nb_Discrete_Choices = Prev_Nb_Discrete_Choices + 1)
1981 and then (Low = High);
1987 -- Ada 2005 (AI-231)
1989 if Ada_Version >= Ada_2005
1990 and then Known_Null (Expression (Assoc))
1991 and then not Empty_Range (Assoc)
1993 Check_Can_Never_Be_Null (Etype (N), Expression (Assoc));
1996 -- Ada 2005 (AI-287): In case of default initialized component
1997 -- we delay the resolution to the expansion phase.
1999 if Box_Present (Assoc) then
2001 -- Ada 2005 (AI-287): In case of default initialization of a
2002 -- component the expander will generate calls to the
2003 -- corresponding initialization subprogram. We need to call
2004 -- Resolve_Aggr_Expr to check the rules about
2007 if not Resolve_Aggr_Expr
2008 (Assoc, Single_Elmt => Single_Choice)
2013 elsif Nkind (Assoc) = N_Iterated_Component_Association then
2014 null; -- handled above, in a loop context.
2016 elsif not Resolve_Aggr_Expr
2017 (Expression (Assoc), Single_Elmt => Single_Choice)
2021 -- Check incorrect use of dynamically tagged expression
2023 -- We differentiate here two cases because the expression may
2024 -- not be decorated. For example, the analysis and resolution
2025 -- of the expression associated with the others choice will be
2026 -- done later with the full aggregate. In such case we
2027 -- duplicate the expression tree to analyze the copy and
2028 -- perform the required check.
2030 elsif not Present (Etype (Expression (Assoc))) then
2032 Save_Analysis : constant Boolean := Full_Analysis;
2033 Expr : constant Node_Id :=
2034 New_Copy_Tree (Expression (Assoc));
2037 Expander_Mode_Save_And_Set (False);
2038 Full_Analysis := False;
2040 -- Analyze the expression, making sure it is properly
2041 -- attached to the tree before we do the analysis.
2043 Set_Parent (Expr, Parent (Expression (Assoc)));
2046 -- Compute its dimensions now, rather than at the end of
2047 -- resolution, because in the case of multidimensional
2048 -- aggregates subsequent expansion may lead to spurious
2051 Check_Expression_Dimensions (Expr, Component_Typ);
2053 -- If the expression is a literal, propagate this info
2054 -- to the expression in the association, to enable some
2055 -- optimizations downstream.
2057 if Is_Entity_Name (Expr)
2058 and then Present (Entity (Expr))
2059 and then Ekind (Entity (Expr)) = E_Enumeration_Literal
2062 (Expression (Assoc), Component_Typ);
2065 Full_Analysis := Save_Analysis;
2066 Expander_Mode_Restore;
2068 if Is_Tagged_Type (Etype (Expr)) then
2069 Check_Dynamically_Tagged_Expression
2071 Typ => Component_Type (Etype (N)),
2076 elsif Is_Tagged_Type (Etype (Expression (Assoc))) then
2077 Check_Dynamically_Tagged_Expression
2078 (Expr => Expression (Assoc),
2079 Typ => Component_Type (Etype (N)),
2086 -- If aggregate contains more than one choice then these must be
2087 -- static. Check for duplicate and missing values.
2089 -- Note: there is duplicated code here wrt Check_Choice_Set in
2090 -- the body of Sem_Case, and it is possible we could just reuse
2091 -- that procedure. To be checked ???
2093 if Nb_Discrete_Choices > 1 then
2094 Check_Choices : declare
2096 -- Location of choice for messages
2100 -- High end of one range and Low end of the next. Should be
2101 -- contiguous if there is no hole in the list of values.
2105 -- End points of duplicated range
2107 Missing_Or_Duplicates : Boolean := False;
2108 -- Set True if missing or duplicate choices found
2110 procedure Output_Bad_Choices (Lo, Hi : Uint; C : Node_Id);
2111 -- Output continuation message with a representation of the
2112 -- bounds (just Lo if Lo = Hi, else Lo .. Hi). C is the
2113 -- choice node where the message is to be posted.
2115 ------------------------
2116 -- Output_Bad_Choices --
2117 ------------------------
2119 procedure Output_Bad_Choices (Lo, Hi : Uint; C : Node_Id) is
2121 -- Enumeration type case
2123 if Is_Enumeration_Type (Index_Typ) then
2125 Chars (Get_Enum_Lit_From_Pos (Index_Typ, Lo, Loc));
2127 Chars (Get_Enum_Lit_From_Pos (Index_Typ, Hi, Loc));
2130 Error_Msg_N ("\\ %!", C);
2132 Error_Msg_N ("\\ % .. %!", C);
2135 -- Integer types case
2138 Error_Msg_Uint_1 := Lo;
2139 Error_Msg_Uint_2 := Hi;
2142 Error_Msg_N ("\\ ^!", C);
2144 Error_Msg_N ("\\ ^ .. ^!", C);
2147 end Output_Bad_Choices;
2149 -- Start of processing for Check_Choices
2152 Sort_Case_Table (Table);
2154 -- First we do a quick linear loop to find out if we have
2155 -- any duplicates or missing entries (usually we have a
2156 -- legal aggregate, so this will get us out quickly).
2158 for J in 1 .. Nb_Discrete_Choices - 1 loop
2159 Hi_Val := Expr_Value (Table (J).Hi);
2160 Lo_Val := Expr_Value (Table (J + 1).Lo);
2163 or else (Lo_Val > Hi_Val + 1
2164 and then not Others_Present)
2166 Missing_Or_Duplicates := True;
2171 -- If we have missing or duplicate entries, first fill in
2172 -- the Highest entries to make life easier in the following
2173 -- loops to detect bad entries.
2175 if Missing_Or_Duplicates then
2176 Table (1).Highest := Expr_Value (Table (1).Hi);
2178 for J in 2 .. Nb_Discrete_Choices loop
2179 Table (J).Highest :=
2181 (Table (J - 1).Highest, Expr_Value (Table (J).Hi));
2184 -- Loop through table entries to find duplicate indexes
2186 for J in 2 .. Nb_Discrete_Choices loop
2187 Lo_Val := Expr_Value (Table (J).Lo);
2188 Hi_Val := Expr_Value (Table (J).Hi);
2190 -- Case where we have duplicates (the lower bound of
2191 -- this choice is less than or equal to the highest
2192 -- high bound found so far).
2194 if Lo_Val <= Table (J - 1).Highest then
2196 -- We move backwards looking for duplicates. We can
2197 -- abandon this loop as soon as we reach a choice
2198 -- highest value that is less than Lo_Val.
2200 for K in reverse 1 .. J - 1 loop
2201 exit when Table (K).Highest < Lo_Val;
2203 -- Here we may have duplicates between entries
2204 -- for K and J. Get range of duplicates.
2207 UI_Max (Lo_Val, Expr_Value (Table (K).Lo));
2209 UI_Min (Hi_Val, Expr_Value (Table (K).Hi));
2211 -- Nothing to do if duplicate range is null
2213 if Lo_Dup > Hi_Dup then
2216 -- Otherwise place proper message
2219 -- We place message on later choice, with a
2220 -- line reference to the earlier choice.
2222 if Sloc (Table (J).Choice) <
2223 Sloc (Table (K).Choice)
2225 Choice := Table (K).Choice;
2226 Error_Msg_Sloc := Sloc (Table (J).Choice);
2228 Choice := Table (J).Choice;
2229 Error_Msg_Sloc := Sloc (Table (K).Choice);
2232 if Lo_Dup = Hi_Dup then
2234 ("index value in array aggregate "
2235 & "duplicates the one given#!", Choice);
2238 ("index values in array aggregate "
2239 & "duplicate those given#!", Choice);
2242 Output_Bad_Choices (Lo_Dup, Hi_Dup, Choice);
2248 -- Loop through entries in table to find missing indexes.
2249 -- Not needed if others, since missing impossible.
2251 if not Others_Present then
2252 for J in 2 .. Nb_Discrete_Choices loop
2253 Lo_Val := Expr_Value (Table (J).Lo);
2254 Hi_Val := Table (J - 1).Highest;
2256 if Lo_Val > Hi_Val + 1 then
2259 Error_Node : Node_Id;
2262 -- If the choice is the bound of a range in
2263 -- a subtype indication, it is not in the
2264 -- source lists for the aggregate itself, so
2265 -- post the error on the aggregate. Otherwise
2266 -- post it on choice itself.
2268 Choice := Table (J).Choice;
2270 if Is_List_Member (Choice) then
2271 Error_Node := Choice;
2276 if Hi_Val + 1 = Lo_Val - 1 then
2278 ("missing index value "
2279 & "in array aggregate!", Error_Node);
2282 ("missing index values "
2283 & "in array aggregate!", Error_Node);
2287 (Hi_Val + 1, Lo_Val - 1, Error_Node);
2293 -- If either missing or duplicate values, return failure
2295 Set_Etype (N, Any_Composite);
2301 -- STEP 2 (B): Compute aggregate bounds and min/max choices values
2303 if Nb_Discrete_Choices > 0 then
2304 Choices_Low := Table (1).Lo;
2305 Choices_High := Table (Nb_Discrete_Choices).Hi;
2308 -- If Others is present, then bounds of aggregate come from the
2309 -- index constraint (not the choices in the aggregate itself).
2311 if Others_Present then
2312 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2314 -- Abandon processing if either bound is already signalled as
2315 -- an error (prevents junk cascaded messages and blow ups).
2317 if Nkind (Aggr_Low) = N_Error
2319 Nkind (Aggr_High) = N_Error
2324 -- No others clause present
2327 -- Special processing if others allowed and not present. This
2328 -- means that the bounds of the aggregate come from the index
2329 -- constraint (and the length must match).
2331 if Others_Allowed then
2332 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2334 -- Abandon processing if either bound is already signalled
2335 -- as an error (stop junk cascaded messages and blow ups).
2337 if Nkind (Aggr_Low) = N_Error
2339 Nkind (Aggr_High) = N_Error
2344 -- If others allowed, and no others present, then the array
2345 -- should cover all index values. If it does not, we will
2346 -- get a length check warning, but there is two cases where
2347 -- an additional warning is useful:
2349 -- If we have no positional components, and the length is
2350 -- wrong (which we can tell by others being allowed with
2351 -- missing components), and the index type is an enumeration
2352 -- type, then issue appropriate warnings about these missing
2353 -- components. They are only warnings, since the aggregate
2354 -- is fine, it's just the wrong length. We skip this check
2355 -- for standard character types (since there are no literals
2356 -- and it is too much trouble to concoct them), and also if
2357 -- any of the bounds have values that are not known at
2360 -- Another case warranting a warning is when the length
2361 -- is right, but as above we have an index type that is
2362 -- an enumeration, and the bounds do not match. This is a
2363 -- case where dubious sliding is allowed and we generate a
2364 -- warning that the bounds do not match.
2366 if No (Expressions (N))
2367 and then Nkind (Index) = N_Range
2368 and then Is_Enumeration_Type (Etype (Index))
2369 and then not Is_Standard_Character_Type (Etype (Index))
2370 and then Compile_Time_Known_Value (Aggr_Low)
2371 and then Compile_Time_Known_Value (Aggr_High)
2372 and then Compile_Time_Known_Value (Choices_Low)
2373 and then Compile_Time_Known_Value (Choices_High)
2375 -- If any of the expressions or range bounds in choices
2376 -- have semantic errors, then do not attempt further
2377 -- resolution, to prevent cascaded errors.
2379 if Errors_Posted_On_Choices then
2384 ALo : constant Node_Id := Expr_Value_E (Aggr_Low);
2385 AHi : constant Node_Id := Expr_Value_E (Aggr_High);
2386 CLo : constant Node_Id := Expr_Value_E (Choices_Low);
2387 CHi : constant Node_Id := Expr_Value_E (Choices_High);
2392 -- Warning case 1, missing values at start/end. Only
2393 -- do the check if the number of entries is too small.
2395 if (Enumeration_Pos (CHi) - Enumeration_Pos (CLo))
2397 (Enumeration_Pos (AHi) - Enumeration_Pos (ALo))
2400 ("missing index value(s) in array aggregate??",
2403 -- Output missing value(s) at start
2405 if Chars (ALo) /= Chars (CLo) then
2408 if Chars (ALo) = Chars (Ent) then
2409 Error_Msg_Name_1 := Chars (ALo);
2410 Error_Msg_N ("\ %??", N);
2412 Error_Msg_Name_1 := Chars (ALo);
2413 Error_Msg_Name_2 := Chars (Ent);
2414 Error_Msg_N ("\ % .. %??", N);
2418 -- Output missing value(s) at end
2420 if Chars (AHi) /= Chars (CHi) then
2423 if Chars (AHi) = Chars (Ent) then
2424 Error_Msg_Name_1 := Chars (Ent);
2425 Error_Msg_N ("\ %??", N);
2427 Error_Msg_Name_1 := Chars (Ent);
2428 Error_Msg_Name_2 := Chars (AHi);
2429 Error_Msg_N ("\ % .. %??", N);
2433 -- Warning case 2, dubious sliding. The First_Subtype
2434 -- test distinguishes between a constrained type where
2435 -- sliding is not allowed (so we will get a warning
2436 -- later that Constraint_Error will be raised), and
2437 -- the unconstrained case where sliding is permitted.
2439 elsif (Enumeration_Pos (CHi) - Enumeration_Pos (CLo))
2441 (Enumeration_Pos (AHi) - Enumeration_Pos (ALo))
2442 and then Chars (ALo) /= Chars (CLo)
2444 not Is_Constrained (First_Subtype (Etype (N)))
2447 ("bounds of aggregate do not match target??", N);
2453 -- If no others, aggregate bounds come from aggregate
2455 Aggr_Low := Choices_Low;
2456 Aggr_High := Choices_High;
2460 -- STEP 3: Process positional components
2463 -- STEP 3 (A): Process positional elements
2465 Expr := First (Expressions (N));
2466 Nb_Elements := Uint_0;
2467 while Present (Expr) loop
2468 Nb_Elements := Nb_Elements + 1;
2470 -- Ada 2005 (AI-231)
2472 if Ada_Version >= Ada_2005 and then Known_Null (Expr) then
2473 Check_Can_Never_Be_Null (Etype (N), Expr);
2476 if not Resolve_Aggr_Expr (Expr, Single_Elmt => True) then
2480 -- Check incorrect use of dynamically tagged expression
2482 if Is_Tagged_Type (Etype (Expr)) then
2483 Check_Dynamically_Tagged_Expression
2485 Typ => Component_Type (Etype (N)),
2492 if Others_Present then
2493 Assoc := Last (Component_Associations (N));
2495 -- Ada 2005 (AI-231)
2497 if Ada_Version >= Ada_2005 and then Known_Null (Assoc) then
2498 Check_Can_Never_Be_Null (Etype (N), Expression (Assoc));
2501 -- Ada 2005 (AI-287): In case of default initialized component,
2502 -- we delay the resolution to the expansion phase.
2504 if Box_Present (Assoc) then
2506 -- Ada 2005 (AI-287): In case of default initialization of a
2507 -- component the expander will generate calls to the
2508 -- corresponding initialization subprogram. We need to call
2509 -- Resolve_Aggr_Expr to check the rules about
2512 if not Resolve_Aggr_Expr (Assoc, Single_Elmt => False) then
2516 elsif not Resolve_Aggr_Expr (Expression (Assoc),
2517 Single_Elmt => False)
2521 -- Check incorrect use of dynamically tagged expression. The
2522 -- expression of the others choice has not been resolved yet.
2523 -- In order to diagnose the semantic error we create a duplicate
2524 -- tree to analyze it and perform the check.
2528 Save_Analysis : constant Boolean := Full_Analysis;
2529 Expr : constant Node_Id :=
2530 New_Copy_Tree (Expression (Assoc));
2533 Expander_Mode_Save_And_Set (False);
2534 Full_Analysis := False;
2536 Full_Analysis := Save_Analysis;
2537 Expander_Mode_Restore;
2539 if Is_Tagged_Type (Etype (Expr)) then
2540 Check_Dynamically_Tagged_Expression
2542 Typ => Component_Type (Etype (N)),
2549 -- STEP 3 (B): Compute the aggregate bounds
2551 if Others_Present then
2552 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2555 if Others_Allowed then
2556 Get_Index_Bounds (Index_Constr, Aggr_Low, Discard);
2558 Aggr_Low := Index_Typ_Low;
2561 Aggr_High := Add (Nb_Elements - 1, To => Aggr_Low);
2562 Check_Bound (Index_Base_High, Aggr_High);
2566 -- STEP 4: Perform static aggregate checks and save the bounds
2570 Check_Bounds (Index_Typ_Low, Index_Typ_High, Aggr_Low, Aggr_High);
2571 Check_Bounds (Index_Base_Low, Index_Base_High, Aggr_Low, Aggr_High);
2575 if Others_Present and then Nb_Discrete_Choices > 0 then
2576 Check_Bounds (Aggr_Low, Aggr_High, Choices_Low, Choices_High);
2577 Check_Bounds (Index_Typ_Low, Index_Typ_High,
2578 Choices_Low, Choices_High);
2579 Check_Bounds (Index_Base_Low, Index_Base_High,
2580 Choices_Low, Choices_High);
2584 elsif Others_Present and then Nb_Elements > 0 then
2585 Check_Length (Aggr_Low, Aggr_High, Nb_Elements);
2586 Check_Length (Index_Typ_Low, Index_Typ_High, Nb_Elements);
2587 Check_Length (Index_Base_Low, Index_Base_High, Nb_Elements);
2590 if Raises_Constraint_Error (Aggr_Low)
2591 or else Raises_Constraint_Error (Aggr_High)
2593 Set_Raises_Constraint_Error (N);
2596 Aggr_Low := Duplicate_Subexpr (Aggr_Low);
2598 -- Do not duplicate Aggr_High if Aggr_High = Aggr_Low + Nb_Elements
2599 -- since the addition node returned by Add is not yet analyzed. Attach
2600 -- to tree and analyze first. Reset analyzed flag to ensure it will get
2601 -- analyzed when it is a literal bound whose type must be properly set.
2603 if Others_Present or else Nb_Discrete_Choices > 0 then
2604 Aggr_High := Duplicate_Subexpr (Aggr_High);
2606 if Etype (Aggr_High) = Universal_Integer then
2607 Set_Analyzed (Aggr_High, False);
2611 -- If the aggregate already has bounds attached to it, it means this is
2612 -- a positional aggregate created as an optimization by
2613 -- Exp_Aggr.Convert_To_Positional, so we don't want to change those
2616 if Present (Aggregate_Bounds (N)) and then not Others_Allowed then
2617 Aggr_Low := Low_Bound (Aggregate_Bounds (N));
2618 Aggr_High := High_Bound (Aggregate_Bounds (N));
2621 Set_Aggregate_Bounds
2622 (N, Make_Range (Loc, Low_Bound => Aggr_Low, High_Bound => Aggr_High));
2624 -- The bounds may contain expressions that must be inserted upwards.
2625 -- Attach them fully to the tree. After analysis, remove side effects
2626 -- from upper bound, if still needed.
2628 Set_Parent (Aggregate_Bounds (N), N);
2629 Analyze_And_Resolve (Aggregate_Bounds (N), Index_Typ);
2630 Check_Unset_Reference (Aggregate_Bounds (N));
2632 if not Others_Present and then Nb_Discrete_Choices = 0 then
2634 (Aggregate_Bounds (N),
2635 Duplicate_Subexpr (High_Bound (Aggregate_Bounds (N))));
2638 -- Check the dimensions of each component in the array aggregate
2640 Analyze_Dimension_Array_Aggregate (N, Component_Typ);
2643 end Resolve_Array_Aggregate;
2645 ---------------------------------
2646 -- Resolve_Container_Aggregate --
2647 ---------------------------------
2649 procedure Resolve_Container_Aggregate (N : Node_Id; Typ : Entity_Id) is
2650 procedure Resolve_Iterated_Association
2652 Key_Type : Entity_Id;
2653 Elmt_Type : Entity_Id);
2654 -- Resolve choices and expression in an iterated component association
2655 -- or an iterated element association, which has a key_expression.
2656 -- This is similar but not identical to the handling of this construct
2657 -- in an array aggregate.
2658 -- For a named container, the type of each choice must be compatible
2659 -- with the key type. For a positional container, the choice must be
2660 -- a subtype indication or an iterator specification that determines
2663 Asp : constant Node_Id := Find_Value_Of_Aspect (Typ, Aspect_Aggregate);
2665 Empty_Subp : Node_Id := Empty;
2666 Add_Named_Subp : Node_Id := Empty;
2667 Add_Unnamed_Subp : Node_Id := Empty;
2668 New_Indexed_Subp : Node_Id := Empty;
2669 Assign_Indexed_Subp : Node_Id := Empty;
2671 ----------------------------------
2672 -- Resolve_Iterated_Association --
2673 ----------------------------------
2675 procedure Resolve_Iterated_Association
2677 Key_Type : Entity_Id;
2678 Elmt_Type : Entity_Id)
2687 Typ : Entity_Id := Empty;
2690 -- If this is an Iterated_Element_Association then either a
2691 -- an Iterator_Specification or a Loop_Parameter specification
2692 -- is present. In both cases a Key_Expression is present.
2694 if Nkind (Comp) = N_Iterated_Element_Association then
2695 if Present (Loop_Parameter_Specification (Comp)) then
2696 Analyze_Loop_Parameter_Specification
2697 (Loop_Parameter_Specification (Comp));
2698 Id_Name := Chars (Defining_Identifier
2699 (Loop_Parameter_Specification (Comp)));
2701 Iter := Copy_Separate_Tree (Iterator_Specification (Comp));
2703 Typ := Etype (Defining_Identifier (Iter));
2704 Id_Name := Chars (Defining_Identifier
2705 (Iterator_Specification (Comp)));
2708 -- Key expression must have the type of the key. We analyze
2709 -- a copy of the original expression, because it will be
2710 -- reanalyzed and copied as needed during expansion of the
2711 -- corresponding loop.
2713 Key_Expr := Key_Expression (Comp);
2714 Analyze_And_Resolve (New_Copy_Tree (Key_Expr), Key_Type);
2716 elsif Present (Iterator_Specification (Comp)) then
2717 Iter := Copy_Separate_Tree (Iterator_Specification (Comp));
2718 Id_Name := Chars (Defining_Identifier (Comp));
2720 Typ := Etype (Defining_Identifier (Iter));
2723 Choice := First (Discrete_Choices (Comp));
2725 while Present (Choice) loop
2728 -- Choice can be a subtype name, a range, or an expression
2730 if Is_Entity_Name (Choice)
2731 and then Is_Type (Entity (Choice))
2732 and then Base_Type (Entity (Choice)) = Base_Type (Key_Type)
2736 elsif Present (Key_Type) then
2737 Analyze_And_Resolve (Choice, Key_Type);
2740 Typ := Etype (Choice); -- assume unique for now
2746 Id_Name := Chars (Defining_Identifier (Comp));
2749 -- Create a scope in which to introduce an index, which is usually
2750 -- visible in the expression for the component, and needed for its
2753 Id := Make_Defining_Identifier (Sloc (Comp), Id_Name);
2754 Ent := New_Internal_Entity (E_Loop, Current_Scope, Sloc (Comp), 'L');
2755 Set_Etype (Ent, Standard_Void_Type);
2756 Set_Parent (Ent, Parent (Comp));
2759 -- Insert and decorate the loop variable in the current scope.
2760 -- The expression has to be analyzed once the loop variable is
2761 -- directly visible. Mark the variable as referenced to prevent
2762 -- spurious warnings, given that subsequent uses of its name in the
2763 -- expression will reference the internal (synonym) loop variable.
2767 if No (Key_Type) then
2768 pragma Assert (Present (Typ));
2769 Set_Etype (Id, Typ);
2771 Set_Etype (Id, Key_Type);
2774 Set_Ekind (Id, E_Variable);
2775 Set_Scope (Id, Ent);
2776 Set_Referenced (Id);
2778 -- Analyze a copy of the expression, to verify legality. We use
2779 -- a copy because the expression will be analyzed anew when the
2780 -- enclosing aggregate is expanded, and the construct is rewritten
2781 -- as a loop with a new index variable.
2783 Expr := New_Copy_Tree (Expression (Comp));
2784 Preanalyze_And_Resolve (Expr, Elmt_Type);
2787 end Resolve_Iterated_Association;
2790 pragma Assert (Nkind (Asp) = N_Aggregate);
2793 Parse_Aspect_Aggregate (Asp,
2794 Empty_Subp, Add_Named_Subp, Add_Unnamed_Subp,
2795 New_Indexed_Subp, Assign_Indexed_Subp);
2797 if Present (Add_Unnamed_Subp)
2798 and then No (New_Indexed_Subp)
2801 Elmt_Type : constant Entity_Id :=
2803 (First_Formal (Entity (Add_Unnamed_Subp))));
2807 if Present (Expressions (N)) then
2808 -- positional aggregate
2810 Comp := First (Expressions (N));
2811 while Present (Comp) loop
2812 Analyze_And_Resolve (Comp, Elmt_Type);
2817 -- Empty aggregate, to be replaced by Empty during
2818 -- expansion, or iterated component association.
2820 if Present (Component_Associations (N)) then
2822 Comp : Node_Id := First (Component_Associations (N));
2824 while Present (Comp) loop
2826 N_Iterated_Component_Association
2828 Error_Msg_N ("illegal component association "
2829 & "for unnamed container aggregate", Comp);
2832 Resolve_Iterated_Association
2833 (Comp, Empty, Elmt_Type);
2842 elsif Present (Add_Named_Subp) then
2844 -- Retrieves types of container, key, and element from the
2845 -- specified insertion procedure.
2847 Container : constant Entity_Id :=
2848 First_Formal (Entity (Add_Named_Subp));
2849 Key_Type : constant Entity_Id := Etype (Next_Formal (Container));
2850 Elmt_Type : constant Entity_Id :=
2851 Etype (Next_Formal (Next_Formal (Container)));
2856 Comp := First (Component_Associations (N));
2857 while Present (Comp) loop
2858 if Nkind (Comp) = N_Component_Association then
2859 Choice := First (Choices (Comp));
2861 while Present (Choice) loop
2862 Analyze_And_Resolve (Choice, Key_Type);
2863 if not Is_Static_Expression (Choice) then
2864 Error_Msg_N ("Choice must be static", Choice);
2870 Analyze_And_Resolve (Expression (Comp), Elmt_Type);
2872 elsif Nkind (Comp) in
2873 N_Iterated_Component_Association |
2874 N_Iterated_Element_Association
2876 Resolve_Iterated_Association
2877 (Comp, Key_Type, Elmt_Type);
2885 -- Indexed Aggregate. Both positional and indexed component
2886 -- can be present. Choices must be static values or ranges
2887 -- with static bounds.
2890 Container : constant Entity_Id :=
2891 First_Formal (Entity (Assign_Indexed_Subp));
2892 Index_Type : constant Entity_Id := Etype (Next_Formal (Container));
2893 Comp_Type : constant Entity_Id :=
2894 Etype (Next_Formal (Next_Formal (Container)));
2899 if Present (Expressions (N)) then
2900 Comp := First (Expressions (N));
2901 while Present (Comp) loop
2902 Analyze_And_Resolve (Comp, Comp_Type);
2907 if Present (Component_Associations (N)) then
2908 Comp := First (Expressions (N));
2910 while Present (Comp) loop
2911 if Nkind (Comp) = N_Component_Association then
2912 Choice := First (Choices (Comp));
2914 while Present (Choice) loop
2915 Analyze_And_Resolve (Choice, Index_Type);
2919 Analyze_And_Resolve (Expression (Comp), Comp_Type);
2921 elsif Nkind (Comp) in
2922 N_Iterated_Component_Association |
2923 N_Iterated_Element_Association
2925 Resolve_Iterated_Association
2926 (Comp, Index_Type, Comp_Type);
2934 end Resolve_Container_Aggregate;
2936 -----------------------------
2937 -- Resolve_Delta_Aggregate --
2938 -----------------------------
2940 procedure Resolve_Delta_Aggregate (N : Node_Id; Typ : Entity_Id) is
2941 Base : constant Node_Id := Expression (N);
2944 if Ada_Version < Ada_2020 then
2945 Error_Msg_N ("delta_aggregate is an Ada 202x feature", N);
2946 Error_Msg_N ("\compile with -gnat2020", N);
2949 if not Is_Composite_Type (Typ) then
2950 Error_Msg_N ("not a composite type", N);
2953 Analyze_And_Resolve (Base, Typ);
2955 if Is_Array_Type (Typ) then
2956 Resolve_Delta_Array_Aggregate (N, Typ);
2958 Resolve_Delta_Record_Aggregate (N, Typ);
2962 end Resolve_Delta_Aggregate;
2964 -----------------------------------
2965 -- Resolve_Delta_Array_Aggregate --
2966 -----------------------------------
2968 procedure Resolve_Delta_Array_Aggregate (N : Node_Id; Typ : Entity_Id) is
2969 Deltas : constant List_Id := Component_Associations (N);
2970 Index_Type : constant Entity_Id := Etype (First_Index (Typ));
2976 Assoc := First (Deltas);
2977 while Present (Assoc) loop
2978 if Nkind (Assoc) = N_Iterated_Component_Association then
2979 Choice := First (Choice_List (Assoc));
2980 while Present (Choice) loop
2981 if Nkind (Choice) = N_Others_Choice then
2983 ("others not allowed in delta aggregate", Choice);
2986 Analyze_And_Resolve (Choice, Index_Type);
2993 Id : constant Entity_Id := Defining_Identifier (Assoc);
2994 Ent : constant Entity_Id :=
2996 (E_Loop, Current_Scope, Sloc (Assoc), 'L');
2999 Set_Etype (Ent, Standard_Void_Type);
3000 Set_Parent (Ent, Assoc);
3002 if No (Scope (Id)) then
3004 Set_Etype (Id, Index_Type);
3005 Set_Ekind (Id, E_Variable);
3006 Set_Scope (Id, Ent);
3011 (New_Copy_Tree (Expression (Assoc)), Component_Type (Typ));
3016 Choice := First (Choice_List (Assoc));
3017 while Present (Choice) loop
3018 if Nkind (Choice) = N_Others_Choice then
3020 ("others not allowed in delta aggregate", Choice);
3025 if Is_Entity_Name (Choice)
3026 and then Is_Type (Entity (Choice))
3028 -- Choice covers a range of values
3030 if Base_Type (Entity (Choice)) /=
3031 Base_Type (Index_Type)
3034 ("choice does mat match index type of",
3038 Resolve (Choice, Index_Type);
3045 Analyze_And_Resolve (Expression (Assoc), Component_Type (Typ));
3050 end Resolve_Delta_Array_Aggregate;
3052 ------------------------------------
3053 -- Resolve_Delta_Record_Aggregate --
3054 ------------------------------------
3056 procedure Resolve_Delta_Record_Aggregate (N : Node_Id; Typ : Entity_Id) is
3058 -- Variables used to verify that discriminant-dependent components
3059 -- appear in the same variant.
3061 Comp_Ref : Entity_Id := Empty; -- init to avoid warning
3064 procedure Check_Variant (Id : Entity_Id);
3065 -- If a given component of the delta aggregate appears in a variant
3066 -- part, verify that it is within the same variant as that of previous
3067 -- specified variant components of the delta.
3069 function Get_Component (Nam : Node_Id) return Entity_Id;
3070 -- Locate component with a given name and return it. If none found then
3071 -- report error and return Empty.
3073 function Nested_In (V1 : Node_Id; V2 : Node_Id) return Boolean;
3074 -- Determine whether variant V1 is within variant V2
3076 function Variant_Depth (N : Node_Id) return Integer;
3077 -- Determine the distance of a variant to the enclosing type
3080 --------------------
3082 --------------------
3084 procedure Check_Variant (Id : Entity_Id) is
3086 Comp_Variant : Node_Id;
3089 if not Has_Discriminants (Typ) then
3093 Comp := First_Entity (Typ);
3094 while Present (Comp) loop
3095 exit when Chars (Comp) = Chars (Id);
3096 Next_Component (Comp);
3099 -- Find the variant, if any, whose component list includes the
3100 -- component declaration.
3102 Comp_Variant := Parent (Parent (List_Containing (Parent (Comp))));
3103 if Nkind (Comp_Variant) = N_Variant then
3104 if No (Variant) then
3105 Variant := Comp_Variant;
3108 elsif Variant /= Comp_Variant then
3110 D1 : constant Integer := Variant_Depth (Variant);
3111 D2 : constant Integer := Variant_Depth (Comp_Variant);
3116 (D1 > D2 and then not Nested_In (Variant, Comp_Variant))
3118 (D2 > D1 and then not Nested_In (Comp_Variant, Variant))
3120 pragma Assert (Present (Comp_Ref));
3121 Error_Msg_Node_2 := Comp_Ref;
3123 ("& and & appear in different variants", Id, Comp);
3125 -- Otherwise retain the deeper variant for subsequent tests
3128 Variant := Comp_Variant;
3139 function Get_Component (Nam : Node_Id) return Entity_Id is
3143 Comp := First_Entity (Typ);
3144 while Present (Comp) loop
3145 if Chars (Comp) = Chars (Nam) then
3146 if Ekind (Comp) = E_Discriminant then
3147 Error_Msg_N ("delta cannot apply to discriminant", Nam);
3156 Error_Msg_NE ("type& has no component with this name", Nam, Typ);
3164 function Nested_In (V1, V2 : Node_Id) return Boolean is
3169 while Nkind (Par) /= N_Full_Type_Declaration loop
3174 Par := Parent (Par);
3184 function Variant_Depth (N : Node_Id) return Integer is
3191 while Nkind (Par) /= N_Full_Type_Declaration loop
3193 Par := Parent (Par);
3201 Deltas : constant List_Id := Component_Associations (N);
3206 Comp_Type : Entity_Id := Empty; -- init to avoid warning
3208 -- Start of processing for Resolve_Delta_Record_Aggregate
3213 Assoc := First (Deltas);
3214 while Present (Assoc) loop
3215 Choice := First (Choice_List (Assoc));
3216 while Present (Choice) loop
3217 Comp := Get_Component (Choice);
3219 if Present (Comp) then
3220 Check_Variant (Choice);
3222 Comp_Type := Etype (Comp);
3224 -- Decorate the component reference by setting its entity and
3225 -- type, as otherwise backends like GNATprove would have to
3226 -- rediscover this information by themselves.
3228 Set_Entity (Choice, Comp);
3229 Set_Etype (Choice, Comp_Type);
3231 Comp_Type := Any_Type;
3237 pragma Assert (Present (Comp_Type));
3238 Analyze_And_Resolve (Expression (Assoc), Comp_Type);
3241 end Resolve_Delta_Record_Aggregate;
3243 ---------------------------------
3244 -- Resolve_Extension_Aggregate --
3245 ---------------------------------
3247 -- There are two cases to consider:
3249 -- a) If the ancestor part is a type mark, the components needed are the
3250 -- difference between the components of the expected type and the
3251 -- components of the given type mark.
3253 -- b) If the ancestor part is an expression, it must be unambiguous, and
3254 -- once we have its type we can also compute the needed components as in
3255 -- the previous case. In both cases, if the ancestor type is not the
3256 -- immediate ancestor, we have to build this ancestor recursively.
3258 -- In both cases, discriminants of the ancestor type do not play a role in
3259 -- the resolution of the needed components, because inherited discriminants
3260 -- cannot be used in a type extension. As a result we can compute
3261 -- independently the list of components of the ancestor type and of the
3264 procedure Resolve_Extension_Aggregate (N : Node_Id; Typ : Entity_Id) is
3265 A : constant Node_Id := Ancestor_Part (N);
3270 function Valid_Limited_Ancestor (Anc : Node_Id) return Boolean;
3271 -- If the type is limited, verify that the ancestor part is a legal
3272 -- expression (aggregate or function call, including 'Input)) that does
3273 -- not require a copy, as specified in 7.5(2).
3275 function Valid_Ancestor_Type return Boolean;
3276 -- Verify that the type of the ancestor part is a non-private ancestor
3277 -- of the expected type, which must be a type extension.
3279 procedure Transform_BIP_Assignment (Typ : Entity_Id);
3280 -- For an extension aggregate whose ancestor part is a build-in-place
3281 -- call returning a nonlimited type, this is used to transform the
3282 -- assignment to the ancestor part to use a temp.
3284 ----------------------------
3285 -- Valid_Limited_Ancestor --
3286 ----------------------------
3288 function Valid_Limited_Ancestor (Anc : Node_Id) return Boolean is
3290 if Is_Entity_Name (Anc) and then Is_Type (Entity (Anc)) then
3293 -- The ancestor must be a call or an aggregate, but a call may
3294 -- have been expanded into a temporary, so check original node.
3296 elsif Nkind (Anc) in N_Aggregate
3297 | N_Extension_Aggregate
3302 elsif Nkind (Original_Node (Anc)) = N_Function_Call then
3305 elsif Nkind (Anc) = N_Attribute_Reference
3306 and then Attribute_Name (Anc) = Name_Input
3310 elsif Nkind (Anc) = N_Qualified_Expression then
3311 return Valid_Limited_Ancestor (Expression (Anc));
3313 elsif Nkind (Anc) = N_Raise_Expression then
3319 end Valid_Limited_Ancestor;
3321 -------------------------
3322 -- Valid_Ancestor_Type --
3323 -------------------------
3325 function Valid_Ancestor_Type return Boolean is
3326 Imm_Type : Entity_Id;
3329 Imm_Type := Base_Type (Typ);
3330 while Is_Derived_Type (Imm_Type) loop
3331 if Etype (Imm_Type) = Base_Type (A_Type) then
3334 -- The base type of the parent type may appear as a private
3335 -- extension if it is declared as such in a parent unit of the
3336 -- current one. For consistency of the subsequent analysis use
3337 -- the partial view for the ancestor part.
3339 elsif Is_Private_Type (Etype (Imm_Type))
3340 and then Present (Full_View (Etype (Imm_Type)))
3341 and then Base_Type (A_Type) = Full_View (Etype (Imm_Type))
3343 A_Type := Etype (Imm_Type);
3346 -- The parent type may be a private extension. The aggregate is
3347 -- legal if the type of the aggregate is an extension of it that
3348 -- is not a private extension.
3350 elsif Is_Private_Type (A_Type)
3351 and then not Is_Private_Type (Imm_Type)
3352 and then Present (Full_View (A_Type))
3353 and then Base_Type (Full_View (A_Type)) = Etype (Imm_Type)
3357 -- The parent type may be a raise expression (which is legal in
3358 -- any expression context).
3360 elsif A_Type = Raise_Type then
3361 A_Type := Etype (Imm_Type);
3365 Imm_Type := Etype (Base_Type (Imm_Type));
3369 -- If previous loop did not find a proper ancestor, report error
3371 Error_Msg_NE ("expect ancestor type of &", A, Typ);
3373 end Valid_Ancestor_Type;
3375 ------------------------------
3376 -- Transform_BIP_Assignment --
3377 ------------------------------
3379 procedure Transform_BIP_Assignment (Typ : Entity_Id) is
3380 Loc : constant Source_Ptr := Sloc (N);
3381 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'Y', A);
3382 Obj_Decl : constant Node_Id :=
3383 Make_Object_Declaration (Loc,
3384 Defining_Identifier => Def_Id,
3385 Constant_Present => True,
3386 Object_Definition => New_Occurrence_Of (Typ, Loc),
3388 Has_Init_Expression => True);
3390 Set_Etype (Def_Id, Typ);
3391 Set_Ancestor_Part (N, New_Occurrence_Of (Def_Id, Loc));
3392 Insert_Action (N, Obj_Decl);
3393 end Transform_BIP_Assignment;
3395 -- Start of processing for Resolve_Extension_Aggregate
3398 -- Analyze the ancestor part and account for the case where it is a
3399 -- parameterless function call.
3402 Check_Parameterless_Call (A);
3404 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
3406 -- AI05-0115: if the ancestor part is a subtype mark, the ancestor
3407 -- must not have unknown discriminants.
3409 if Has_Unknown_Discriminants (Entity (A)) then
3411 ("aggregate not available for type& whose ancestor "
3412 & "has unknown discriminants", N, Typ);
3416 if not Is_Tagged_Type (Typ) then
3417 Error_Msg_N ("type of extension aggregate must be tagged", N);
3420 elsif Is_Limited_Type (Typ) then
3422 -- Ada 2005 (AI-287): Limited aggregates are allowed
3424 if Ada_Version < Ada_2005 then
3425 Error_Msg_N ("aggregate type cannot be limited", N);
3426 Explain_Limited_Type (Typ, N);
3429 elsif Valid_Limited_Ancestor (A) then
3434 ("limited ancestor part must be aggregate or function call", A);
3437 elsif Is_Class_Wide_Type (Typ) then
3438 Error_Msg_N ("aggregate cannot be of a class-wide type", N);
3442 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
3443 A_Type := Get_Full_View (Entity (A));
3445 if Valid_Ancestor_Type then
3446 Set_Entity (A, A_Type);
3447 Set_Etype (A, A_Type);
3449 Validate_Ancestor_Part (N);
3450 Resolve_Record_Aggregate (N, Typ);
3453 elsif Nkind (A) /= N_Aggregate then
3454 if Is_Overloaded (A) then
3457 Get_First_Interp (A, I, It);
3458 while Present (It.Typ) loop
3460 -- Consider limited interpretations if Ada 2005 or higher
3462 if Is_Tagged_Type (It.Typ)
3463 and then (Ada_Version >= Ada_2005
3464 or else not Is_Limited_Type (It.Typ))
3466 if A_Type /= Any_Type then
3467 Error_Msg_N ("cannot resolve expression", A);
3474 Get_Next_Interp (I, It);
3477 if A_Type = Any_Type then
3478 if Ada_Version >= Ada_2005 then
3480 ("ancestor part must be of a tagged type", A);
3483 ("ancestor part must be of a nonlimited tagged type", A);
3490 A_Type := Etype (A);
3493 if Valid_Ancestor_Type then
3494 Resolve (A, A_Type);
3495 Check_Unset_Reference (A);
3496 Check_Non_Static_Context (A);
3498 -- The aggregate is illegal if the ancestor expression is a call
3499 -- to a function with a limited unconstrained result, unless the
3500 -- type of the aggregate is a null extension. This restriction
3501 -- was added in AI05-67 to simplify implementation.
3503 if Nkind (A) = N_Function_Call
3504 and then Is_Limited_Type (A_Type)
3505 and then not Is_Null_Extension (Typ)
3506 and then not Is_Constrained (A_Type)
3509 ("type of limited ancestor part must be constrained", A);
3511 -- Reject the use of CPP constructors that leave objects partially
3512 -- initialized. For example:
3514 -- type CPP_Root is tagged limited record ...
3515 -- pragma Import (CPP, CPP_Root);
3517 -- type CPP_DT is new CPP_Root and Iface ...
3518 -- pragma Import (CPP, CPP_DT);
3520 -- type Ada_DT is new CPP_DT with ...
3522 -- Obj : Ada_DT := Ada_DT'(New_CPP_Root with others => <>);
3524 -- Using the constructor of CPP_Root the slots of the dispatch
3525 -- table of CPP_DT cannot be set, and the secondary tag of
3526 -- CPP_DT is unknown.
3528 elsif Nkind (A) = N_Function_Call
3529 and then Is_CPP_Constructor_Call (A)
3530 and then Enclosing_CPP_Parent (Typ) /= A_Type
3533 ("??must use 'C'P'P constructor for type &", A,
3534 Enclosing_CPP_Parent (Typ));
3536 -- The following call is not needed if the previous warning
3537 -- is promoted to an error.
3539 Resolve_Record_Aggregate (N, Typ);
3541 elsif Is_Class_Wide_Type (Etype (A))
3542 and then Nkind (Original_Node (A)) = N_Function_Call
3544 -- If the ancestor part is a dispatching call, it appears
3545 -- statically to be a legal ancestor, but it yields any member
3546 -- of the class, and it is not possible to determine whether
3547 -- it is an ancestor of the extension aggregate (much less
3548 -- which ancestor). It is not possible to determine the
3549 -- components of the extension part.
3551 -- This check implements AI-306, which in fact was motivated by
3552 -- an AdaCore query to the ARG after this test was added.
3554 Error_Msg_N ("ancestor part must be statically tagged", A);
3556 -- We are using the build-in-place protocol, but we can't build
3557 -- in place, because we need to call the function before
3558 -- allocating the aggregate. Could do better for null
3559 -- extensions, and maybe for nondiscriminated types.
3560 -- This is wrong for limited, but those were wrong already.
3562 if not Is_Limited_View (A_Type)
3563 and then Is_Build_In_Place_Function_Call (A)
3565 Transform_BIP_Assignment (A_Type);
3568 Resolve_Record_Aggregate (N, Typ);
3573 Error_Msg_N ("no unique type for this aggregate", A);
3576 Check_Function_Writable_Actuals (N);
3577 end Resolve_Extension_Aggregate;
3579 ------------------------------
3580 -- Resolve_Record_Aggregate --
3581 ------------------------------
3583 procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id) is
3584 New_Assoc_List : constant List_Id := New_List;
3585 -- New_Assoc_List is the newly built list of N_Component_Association
3588 Others_Etype : Entity_Id := Empty;
3589 -- This variable is used to save the Etype of the last record component
3590 -- that takes its value from the others choice. Its purpose is:
3592 -- (a) make sure the others choice is useful
3594 -- (b) make sure the type of all the components whose value is
3595 -- subsumed by the others choice are the same.
3597 -- This variable is updated as a side effect of function Get_Value.
3599 Box_Node : Node_Id := Empty;
3600 Is_Box_Present : Boolean := False;
3601 Others_Box : Natural := 0;
3602 -- Ada 2005 (AI-287): Variables used in case of default initialization
3603 -- to provide a functionality similar to Others_Etype. Box_Present
3604 -- indicates that the component takes its default initialization;
3605 -- Others_Box counts the number of components of the current aggregate
3606 -- (which may be a sub-aggregate of a larger one) that are default-
3607 -- initialized. A value of One indicates that an others_box is present.
3608 -- Any larger value indicates that the others_box is not redundant.
3609 -- These variables, similar to Others_Etype, are also updated as a side
3610 -- effect of function Get_Value. Box_Node is used to place a warning on
3611 -- a redundant others_box.
3613 procedure Add_Association
3614 (Component : Entity_Id;
3616 Assoc_List : List_Id;
3617 Is_Box_Present : Boolean := False);
3618 -- Builds a new N_Component_Association node which associates Component
3619 -- to expression Expr and adds it to the association list being built,
3620 -- either New_Assoc_List, or the association being built for an inner
3623 procedure Add_Discriminant_Values
3624 (New_Aggr : Node_Id;
3625 Assoc_List : List_Id);
3626 -- The constraint to a component may be given by a discriminant of the
3627 -- enclosing type, in which case we have to retrieve its value, which is
3628 -- part of the enclosing aggregate. Assoc_List provides the discriminant
3629 -- associations of the current type or of some enclosing record.
3631 function Discriminant_Present (Input_Discr : Entity_Id) return Boolean;
3632 -- If aggregate N is a regular aggregate this routine will return True.
3633 -- Otherwise, if N is an extension aggregate, then Input_Discr denotes
3634 -- a discriminant whose value may already have been specified by N's
3635 -- ancestor part. This routine checks whether this is indeed the case
3636 -- and if so returns False, signaling that no value for Input_Discr
3637 -- should appear in N's aggregate part. Also, in this case, the routine
3638 -- appends to New_Assoc_List the discriminant value specified in the
3641 -- If the aggregate is in a context with expansion delayed, it will be
3642 -- reanalyzed. The inherited discriminant values must not be reinserted
3643 -- in the component list to prevent spurious errors, but they must be
3644 -- present on first analysis to build the proper subtype indications.
3645 -- The flag Inherited_Discriminant is used to prevent the re-insertion.
3647 function Find_Private_Ancestor (Typ : Entity_Id) return Entity_Id;
3648 -- AI05-0115: Find earlier ancestor in the derivation chain that is
3649 -- derived from private view Typ. Whether the aggregate is legal depends
3650 -- on the current visibility of the type as well as that of the parent
3654 (Compon : Entity_Id;
3656 Consider_Others_Choice : Boolean := False) return Node_Id;
3657 -- Given a record component stored in parameter Compon, this function
3658 -- returns its value as it appears in the list From, which is a list
3659 -- of N_Component_Association nodes.
3661 -- If no component association has a choice for the searched component,
3662 -- the value provided by the others choice is returned, if there is one,
3663 -- and Consider_Others_Choice is set to true. Otherwise Empty is
3664 -- returned. If there is more than one component association giving a
3665 -- value for the searched record component, an error message is emitted
3666 -- and the first found value is returned.
3668 -- If Consider_Others_Choice is set and the returned expression comes
3669 -- from the others choice, then Others_Etype is set as a side effect.
3670 -- An error message is emitted if the components taking their value from
3671 -- the others choice do not have same type.
3673 procedure Propagate_Discriminants
3675 Assoc_List : List_Id);
3676 -- Nested components may themselves be discriminated types constrained
3677 -- by outer discriminants, whose values must be captured before the
3678 -- aggregate is expanded into assignments.
3680 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Entity_Id);
3681 -- Analyzes and resolves expression Expr against the Etype of the
3682 -- Component. This routine also applies all appropriate checks to Expr.
3683 -- It finally saves a Expr in the newly created association list that
3684 -- will be attached to the final record aggregate. Note that if the
3685 -- Parent pointer of Expr is not set then Expr was produced with a
3686 -- New_Copy_Tree or some such.
3688 procedure Rewrite_Range (Root_Type : Entity_Id; Rge : Node_Id);
3689 -- Rewrite a range node Rge when its bounds refer to non-stored
3690 -- discriminants from Root_Type, to replace them with the stored
3691 -- discriminant values. This is required in GNATprove mode, and is
3692 -- adopted in all modes to avoid special-casing GNATprove mode.
3694 ---------------------
3695 -- Add_Association --
3696 ---------------------
3698 procedure Add_Association
3699 (Component : Entity_Id;
3701 Assoc_List : List_Id;
3702 Is_Box_Present : Boolean := False)
3704 Choice_List : constant List_Id := New_List;
3708 -- If this is a box association the expression is missing, so use the
3709 -- Sloc of the aggregate itself for the new association.
3711 pragma Assert (Present (Expr) xor Is_Box_Present);
3713 if Present (Expr) then
3719 Append_To (Choice_List, New_Occurrence_Of (Component, Loc));
3721 Append_To (Assoc_List,
3722 Make_Component_Association (Loc,
3723 Choices => Choice_List,
3725 Box_Present => Is_Box_Present));
3726 end Add_Association;
3728 -----------------------------
3729 -- Add_Discriminant_Values --
3730 -----------------------------
3732 procedure Add_Discriminant_Values
3733 (New_Aggr : Node_Id;
3734 Assoc_List : List_Id)
3738 Discr_Elmt : Elmt_Id;
3739 Discr_Val : Node_Id;
3743 Discr := First_Discriminant (Etype (New_Aggr));
3744 Discr_Elmt := First_Elmt (Discriminant_Constraint (Etype (New_Aggr)));
3745 while Present (Discr_Elmt) loop
3746 Discr_Val := Node (Discr_Elmt);
3748 -- If the constraint is given by a discriminant then it is a
3749 -- discriminant of an enclosing record, and its value has already
3750 -- been placed in the association list.
3752 if Is_Entity_Name (Discr_Val)
3753 and then Ekind (Entity (Discr_Val)) = E_Discriminant
3755 Val := Entity (Discr_Val);
3757 Assoc := First (Assoc_List);
3758 while Present (Assoc) loop
3759 if Present (Entity (First (Choices (Assoc))))
3760 and then Entity (First (Choices (Assoc))) = Val
3762 Discr_Val := Expression (Assoc);
3771 (Discr, New_Copy_Tree (Discr_Val),
3772 Component_Associations (New_Aggr));
3774 -- If the discriminant constraint is a current instance, mark the
3775 -- current aggregate so that the self-reference can be expanded
3776 -- later. The constraint may refer to the subtype of aggregate, so
3777 -- use base type for comparison.
3779 if Nkind (Discr_Val) = N_Attribute_Reference
3780 and then Is_Entity_Name (Prefix (Discr_Val))
3781 and then Is_Type (Entity (Prefix (Discr_Val)))
3782 and then Base_Type (Etype (N)) = Entity (Prefix (Discr_Val))
3784 Set_Has_Self_Reference (N);
3787 Next_Elmt (Discr_Elmt);
3788 Next_Discriminant (Discr);
3790 end Add_Discriminant_Values;
3792 --------------------------
3793 -- Discriminant_Present --
3794 --------------------------
3796 function Discriminant_Present (Input_Discr : Entity_Id) return Boolean is
3797 Regular_Aggr : constant Boolean := Nkind (N) /= N_Extension_Aggregate;
3799 Ancestor_Is_Subtyp : Boolean;
3804 Ancestor_Typ : Entity_Id;
3805 Comp_Assoc : Node_Id;
3807 Discr_Expr : Node_Id;
3808 Discr_Val : Elmt_Id := No_Elmt;
3809 Orig_Discr : Entity_Id;
3812 if Regular_Aggr then
3816 -- Check whether inherited discriminant values have already been
3817 -- inserted in the aggregate. This will be the case if we are
3818 -- re-analyzing an aggregate whose expansion was delayed.
3820 if Present (Component_Associations (N)) then
3821 Comp_Assoc := First (Component_Associations (N));
3822 while Present (Comp_Assoc) loop
3823 if Inherited_Discriminant (Comp_Assoc) then
3831 Ancestor := Ancestor_Part (N);
3832 Ancestor_Typ := Etype (Ancestor);
3833 Loc := Sloc (Ancestor);
3835 -- For a private type with unknown discriminants, use the underlying
3836 -- record view if it is available.
3838 if Has_Unknown_Discriminants (Ancestor_Typ)
3839 and then Present (Full_View (Ancestor_Typ))
3840 and then Present (Underlying_Record_View (Full_View (Ancestor_Typ)))
3842 Ancestor_Typ := Underlying_Record_View (Full_View (Ancestor_Typ));
3845 Ancestor_Is_Subtyp :=
3846 Is_Entity_Name (Ancestor) and then Is_Type (Entity (Ancestor));
3848 -- If the ancestor part has no discriminants clearly N's aggregate
3849 -- part must provide a value for Discr.
3851 if not Has_Discriminants (Ancestor_Typ) then
3854 -- If the ancestor part is an unconstrained subtype mark then the
3855 -- Discr must be present in N's aggregate part.
3857 elsif Ancestor_Is_Subtyp
3858 and then not Is_Constrained (Entity (Ancestor))
3863 -- Now look to see if Discr was specified in the ancestor part
3865 if Ancestor_Is_Subtyp then
3867 First_Elmt (Discriminant_Constraint (Entity (Ancestor)));
3870 Orig_Discr := Original_Record_Component (Input_Discr);
3872 Discr := First_Discriminant (Ancestor_Typ);
3873 while Present (Discr) loop
3875 -- If Ancestor has already specified Disc value then insert its
3876 -- value in the final aggregate.
3878 if Original_Record_Component (Discr) = Orig_Discr then
3879 if Ancestor_Is_Subtyp then
3880 Discr_Expr := New_Copy_Tree (Node (Discr_Val));
3883 Make_Selected_Component (Loc,
3884 Prefix => Duplicate_Subexpr (Ancestor),
3885 Selector_Name => New_Occurrence_Of (Input_Discr, Loc));
3888 Resolve_Aggr_Expr (Discr_Expr, Input_Discr);
3889 Set_Inherited_Discriminant (Last (New_Assoc_List));
3893 Next_Discriminant (Discr);
3895 if Ancestor_Is_Subtyp then
3896 Next_Elmt (Discr_Val);
3901 end Discriminant_Present;
3903 ---------------------------
3904 -- Find_Private_Ancestor --
3905 ---------------------------
3907 function Find_Private_Ancestor (Typ : Entity_Id) return Entity_Id is
3913 if Has_Private_Ancestor (Par)
3914 and then not Has_Private_Ancestor (Etype (Base_Type (Par)))
3918 elsif not Is_Derived_Type (Par) then
3922 Par := Etype (Base_Type (Par));
3925 end Find_Private_Ancestor;
3932 (Compon : Entity_Id;
3934 Consider_Others_Choice : Boolean := False) return Node_Id
3936 Typ : constant Entity_Id := Etype (Compon);
3938 Expr : Node_Id := Empty;
3939 Selector_Name : Node_Id;
3942 Is_Box_Present := False;
3948 Assoc := First (From);
3949 while Present (Assoc) loop
3950 Selector_Name := First (Choices (Assoc));
3951 while Present (Selector_Name) loop
3952 if Nkind (Selector_Name) = N_Others_Choice then
3953 if Consider_Others_Choice and then No (Expr) then
3955 -- We need to duplicate the expression for each
3956 -- successive component covered by the others choice.
3957 -- This is redundant if the others_choice covers only
3958 -- one component (small optimization possible???), but
3959 -- indispensable otherwise, because each one must be
3960 -- expanded individually to preserve side effects.
3962 -- Ada 2005 (AI-287): In case of default initialization
3963 -- of components, we duplicate the corresponding default
3964 -- expression (from the record type declaration). The
3965 -- copy must carry the sloc of the association (not the
3966 -- original expression) to prevent spurious elaboration
3967 -- checks when the default includes function calls.
3969 if Box_Present (Assoc) then
3970 Others_Box := Others_Box + 1;
3971 Is_Box_Present := True;
3973 if Expander_Active then
3975 New_Copy_Tree_And_Copy_Dimensions
3976 (Expression (Parent (Compon)),
3977 New_Sloc => Sloc (Assoc));
3979 return Expression (Parent (Compon));
3983 if Present (Others_Etype)
3984 and then Base_Type (Others_Etype) /= Base_Type (Typ)
3986 -- If the components are of an anonymous access
3987 -- type they are distinct, but this is legal in
3988 -- Ada 2012 as long as designated types match.
3990 if (Ekind (Typ) = E_Anonymous_Access_Type
3991 or else Ekind (Typ) =
3992 E_Anonymous_Access_Subprogram_Type)
3993 and then Designated_Type (Typ) =
3994 Designated_Type (Others_Etype)
3999 ("components in OTHERS choice must have same "
4000 & "type", Selector_Name);
4004 Others_Etype := Typ;
4006 -- Copy the expression so that it is resolved
4007 -- independently for each component, This is needed
4008 -- for accessibility checks on components of anonymous
4009 -- access types, even in compile_only mode.
4011 if not Inside_A_Generic then
4013 New_Copy_Tree_And_Copy_Dimensions
4014 (Expression (Assoc));
4016 return Expression (Assoc);
4021 elsif Chars (Compon) = Chars (Selector_Name) then
4024 -- Ada 2005 (AI-231)
4026 if Ada_Version >= Ada_2005
4027 and then Known_Null (Expression (Assoc))
4029 Check_Can_Never_Be_Null (Compon, Expression (Assoc));
4032 -- We need to duplicate the expression when several
4033 -- components are grouped together with a "|" choice.
4034 -- For instance "filed1 | filed2 => Expr"
4036 -- Ada 2005 (AI-287)
4038 if Box_Present (Assoc) then
4039 Is_Box_Present := True;
4041 -- Duplicate the default expression of the component
4042 -- from the record type declaration, so a new copy
4043 -- can be attached to the association.
4045 -- Note that we always copy the default expression,
4046 -- even when the association has a single choice, in
4047 -- order to create a proper association for the
4048 -- expanded aggregate.
4050 -- Component may have no default, in which case the
4051 -- expression is empty and the component is default-
4052 -- initialized, but an association for the component
4053 -- exists, and it is not covered by an others clause.
4055 -- Scalar and private types have no initialization
4056 -- procedure, so they remain uninitialized. If the
4057 -- target of the aggregate is a constant this
4058 -- deserves a warning.
4060 if No (Expression (Parent (Compon)))
4061 and then not Has_Non_Null_Base_Init_Proc (Typ)
4062 and then not Has_Aspect (Typ, Aspect_Default_Value)
4063 and then not Is_Concurrent_Type (Typ)
4064 and then Nkind (Parent (N)) = N_Object_Declaration
4065 and then Constant_Present (Parent (N))
4067 Error_Msg_Node_2 := Typ;
4069 ("component&? of type& is uninitialized",
4070 Assoc, Selector_Name);
4072 -- An additional reminder if the component type
4073 -- is a generic formal.
4075 if Is_Generic_Type (Base_Type (Typ)) then
4077 ("\instance should provide actual type with "
4078 & "initialization for&", Assoc, Typ);
4083 New_Copy_Tree_And_Copy_Dimensions
4084 (Expression (Parent (Compon)));
4087 if Present (Next (Selector_Name)) then
4088 Expr := New_Copy_Tree_And_Copy_Dimensions
4089 (Expression (Assoc));
4091 Expr := Expression (Assoc);
4095 Generate_Reference (Compon, Selector_Name, 'm');
4099 ("more than one value supplied for &",
4100 Selector_Name, Compon);
4105 Next (Selector_Name);
4114 -----------------------------
4115 -- Propagate_Discriminants --
4116 -----------------------------
4118 procedure Propagate_Discriminants
4120 Assoc_List : List_Id)
4122 Loc : constant Source_Ptr := Sloc (N);
4124 procedure Process_Component (Comp : Entity_Id);
4125 -- Add one component with a box association to the inner aggregate,
4126 -- and recurse if component is itself composite.
4128 -----------------------
4129 -- Process_Component --
4130 -----------------------
4132 procedure Process_Component (Comp : Entity_Id) is
4133 T : constant Entity_Id := Etype (Comp);
4137 if Is_Record_Type (T) and then Has_Discriminants (T) then
4138 New_Aggr := Make_Aggregate (Loc, No_List, New_List);
4139 Set_Etype (New_Aggr, T);
4142 (Comp, New_Aggr, Component_Associations (Aggr));
4144 -- Collect discriminant values and recurse
4146 Add_Discriminant_Values (New_Aggr, Assoc_List);
4147 Propagate_Discriminants (New_Aggr, Assoc_List);
4149 Build_Constrained_Itype
4150 (New_Aggr, T, Component_Associations (New_Aggr));
4153 (Comp, Empty, Component_Associations (Aggr),
4154 Is_Box_Present => True);
4156 end Process_Component;
4160 Aggr_Type : constant Entity_Id := Base_Type (Etype (Aggr));
4161 Components : constant Elist_Id := New_Elmt_List;
4162 Def_Node : constant Node_Id :=
4163 Type_Definition (Declaration_Node (Aggr_Type));
4166 Comp_Elmt : Elmt_Id;
4169 -- Start of processing for Propagate_Discriminants
4172 -- The component type may be a variant type. Collect the components
4173 -- that are ruled by the known values of the discriminants. Their
4174 -- values have already been inserted into the component list of the
4175 -- current aggregate.
4177 if Nkind (Def_Node) = N_Record_Definition
4178 and then Present (Component_List (Def_Node))
4179 and then Present (Variant_Part (Component_List (Def_Node)))
4181 Gather_Components (Aggr_Type,
4182 Component_List (Def_Node),
4183 Governed_By => Component_Associations (Aggr),
4185 Report_Errors => Errors);
4187 Comp_Elmt := First_Elmt (Components);
4188 while Present (Comp_Elmt) loop
4189 if Ekind (Node (Comp_Elmt)) /= E_Discriminant then
4190 Process_Component (Node (Comp_Elmt));
4193 Next_Elmt (Comp_Elmt);
4196 -- No variant part, iterate over all components
4199 Comp := First_Component (Etype (Aggr));
4200 while Present (Comp) loop
4201 Process_Component (Comp);
4202 Next_Component (Comp);
4205 end Propagate_Discriminants;
4207 -----------------------
4208 -- Resolve_Aggr_Expr --
4209 -----------------------
4211 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Entity_Id) is
4212 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean;
4213 -- If the expression is an aggregate (possibly qualified) then its
4214 -- expansion is delayed until the enclosing aggregate is expanded
4215 -- into assignments. In that case, do not generate checks on the
4216 -- expression, because they will be generated later, and will other-
4217 -- wise force a copy (to remove side effects) that would leave a
4218 -- dynamic-sized aggregate in the code, something that gigi cannot
4221 ---------------------------
4222 -- Has_Expansion_Delayed --
4223 ---------------------------
4225 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean is
4228 (Nkind (Expr) in N_Aggregate | N_Extension_Aggregate
4229 and then Present (Etype (Expr))
4230 and then Is_Record_Type (Etype (Expr))
4231 and then Expansion_Delayed (Expr))
4233 (Nkind (Expr) = N_Qualified_Expression
4234 and then Has_Expansion_Delayed (Expression (Expr)));
4235 end Has_Expansion_Delayed;
4239 Expr_Type : Entity_Id := Empty;
4240 New_C : Entity_Id := Component;
4244 -- Set to True if the resolved Expr node needs to be relocated when
4245 -- attached to the newly created association list. This node need not
4246 -- be relocated if its parent pointer is not set. In fact in this
4247 -- case Expr is the output of a New_Copy_Tree call. If Relocate is
4248 -- True then we have analyzed the expression node in the original
4249 -- aggregate and hence it needs to be relocated when moved over to
4250 -- the new association list.
4252 -- Start of processing for Resolve_Aggr_Expr
4255 -- If the type of the component is elementary or the type of the
4256 -- aggregate does not contain discriminants, use the type of the
4257 -- component to resolve Expr.
4259 if Is_Elementary_Type (Etype (Component))
4260 or else not Has_Discriminants (Etype (N))
4262 Expr_Type := Etype (Component);
4264 -- Otherwise we have to pick up the new type of the component from
4265 -- the new constrained subtype of the aggregate. In fact components
4266 -- which are of a composite type might be constrained by a
4267 -- discriminant, and we want to resolve Expr against the subtype were
4268 -- all discriminant occurrences are replaced with their actual value.
4271 New_C := First_Component (Etype (N));
4272 while Present (New_C) loop
4273 if Chars (New_C) = Chars (Component) then
4274 Expr_Type := Etype (New_C);
4278 Next_Component (New_C);
4281 pragma Assert (Present (Expr_Type));
4283 -- For each range in an array type where a discriminant has been
4284 -- replaced with the constraint, check that this range is within
4285 -- the range of the base type. This checks is done in the init
4286 -- proc for regular objects, but has to be done here for
4287 -- aggregates since no init proc is called for them.
4289 if Is_Array_Type (Expr_Type) then
4292 -- Range of the current constrained index in the array
4294 Orig_Index : Node_Id := First_Index (Etype (Component));
4295 -- Range corresponding to the range Index above in the
4296 -- original unconstrained record type. The bounds of this
4297 -- range may be governed by discriminants.
4299 Unconstr_Index : Node_Id := First_Index (Etype (Expr_Type));
4300 -- Range corresponding to the range Index above for the
4301 -- unconstrained array type. This range is needed to apply
4305 Index := First_Index (Expr_Type);
4306 while Present (Index) loop
4307 if Depends_On_Discriminant (Orig_Index) then
4308 Apply_Range_Check (Index, Etype (Unconstr_Index));
4312 Next_Index (Orig_Index);
4313 Next_Index (Unconstr_Index);
4319 -- If the Parent pointer of Expr is not set, Expr is an expression
4320 -- duplicated by New_Tree_Copy (this happens for record aggregates
4321 -- that look like (Field1 | Filed2 => Expr) or (others => Expr)).
4322 -- Such a duplicated expression must be attached to the tree
4323 -- before analysis and resolution to enforce the rule that a tree
4324 -- fragment should never be analyzed or resolved unless it is
4325 -- attached to the current compilation unit.
4327 if No (Parent (Expr)) then
4328 Set_Parent (Expr, N);
4334 Analyze_And_Resolve (Expr, Expr_Type);
4335 Check_Expr_OK_In_Limited_Aggregate (Expr);
4336 Check_Non_Static_Context (Expr);
4337 Check_Unset_Reference (Expr);
4339 -- Check wrong use of class-wide types
4341 if Is_Class_Wide_Type (Etype (Expr)) then
4342 Error_Msg_N ("dynamically tagged expression not allowed", Expr);
4345 if not Has_Expansion_Delayed (Expr) then
4346 Aggregate_Constraint_Checks (Expr, Expr_Type);
4349 -- If an aggregate component has a type with predicates, an explicit
4350 -- predicate check must be applied, as for an assignment statement,
4351 -- because the aggregate might not be expanded into individual
4352 -- component assignments.
4354 if Has_Predicates (Expr_Type)
4355 and then Analyzed (Expr)
4357 Apply_Predicate_Check (Expr, Expr_Type);
4360 if Raises_Constraint_Error (Expr) then
4361 Set_Raises_Constraint_Error (N);
4364 -- If the expression has been marked as requiring a range check, then
4365 -- generate it here. It's a bit odd to be generating such checks in
4366 -- the analyzer, but harmless since Generate_Range_Check does nothing
4367 -- (other than making sure Do_Range_Check is set) if the expander is
4370 if Do_Range_Check (Expr) then
4371 Generate_Range_Check (Expr, Expr_Type, CE_Range_Check_Failed);
4374 -- Add association Component => Expr if the caller requests it
4377 New_Expr := Relocate_Node (Expr);
4379 -- Since New_Expr is not gonna be analyzed later on, we need to
4380 -- propagate here the dimensions form Expr to New_Expr.
4382 Copy_Dimensions (Expr, New_Expr);
4388 Add_Association (New_C, New_Expr, New_Assoc_List);
4389 end Resolve_Aggr_Expr;
4395 procedure Rewrite_Range (Root_Type : Entity_Id; Rge : Node_Id) is
4396 procedure Rewrite_Bound
4399 Expr_Disc : Node_Id);
4400 -- Rewrite a bound of the range Bound, when it is equal to the
4401 -- non-stored discriminant Disc, into the stored discriminant
4408 procedure Rewrite_Bound
4411 Expr_Disc : Node_Id)
4414 if Nkind (Bound) /= N_Identifier then
4418 -- We expect either the discriminant or the discriminal
4420 if Entity (Bound) = Disc
4421 or else (Ekind (Entity (Bound)) = E_In_Parameter
4422 and then Discriminal_Link (Entity (Bound)) = Disc)
4424 Rewrite (Bound, New_Copy_Tree (Expr_Disc));
4430 Low, High : Node_Id;
4432 Expr_Disc : Elmt_Id;
4434 -- Start of processing for Rewrite_Range
4437 if Has_Discriminants (Root_Type) and then Nkind (Rge) = N_Range then
4438 Low := Low_Bound (Rge);
4439 High := High_Bound (Rge);
4441 Disc := First_Discriminant (Root_Type);
4442 Expr_Disc := First_Elmt (Stored_Constraint (Etype (N)));
4443 while Present (Disc) loop
4444 Rewrite_Bound (Low, Disc, Node (Expr_Disc));
4445 Rewrite_Bound (High, Disc, Node (Expr_Disc));
4446 Next_Discriminant (Disc);
4447 Next_Elmt (Expr_Disc);
4454 Components : constant Elist_Id := New_Elmt_List;
4455 -- Components is the list of the record components whose value must be
4456 -- provided in the aggregate. This list does include discriminants.
4458 Component : Entity_Id;
4459 Component_Elmt : Elmt_Id;
4461 Positional_Expr : Node_Id;
4463 -- Start of processing for Resolve_Record_Aggregate
4466 -- A record aggregate is restricted in SPARK:
4468 -- Each named association can have only a single choice.
4469 -- OTHERS cannot be used.
4470 -- Positional and named associations cannot be mixed.
4472 if Present (Component_Associations (N))
4473 and then Present (First (Component_Associations (N)))
4479 Assoc := First (Component_Associations (N));
4480 while Present (Assoc) loop
4481 if Nkind (Assoc) = N_Iterated_Component_Association then
4483 ("iterated component association can only appear in an "
4484 & "array aggregate", N);
4485 raise Unrecoverable_Error;
4493 -- We may end up calling Duplicate_Subexpr on expressions that are
4494 -- attached to New_Assoc_List. For this reason we need to attach it
4495 -- to the tree by setting its parent pointer to N. This parent point
4496 -- will change in STEP 8 below.
4498 Set_Parent (New_Assoc_List, N);
4500 -- STEP 1: abstract type and null record verification
4502 if Is_Abstract_Type (Typ) then
4503 Error_Msg_N ("type of aggregate cannot be abstract", N);
4506 if No (First_Entity (Typ)) and then Null_Record_Present (N) then
4510 elsif Present (First_Entity (Typ))
4511 and then Null_Record_Present (N)
4512 and then not Is_Tagged_Type (Typ)
4514 Error_Msg_N ("record aggregate cannot be null", N);
4517 -- If the type has no components, then the aggregate should either
4518 -- have "null record", or in Ada 2005 it could instead have a single
4519 -- component association given by "others => <>". For Ada 95 we flag an
4520 -- error at this point, but for Ada 2005 we proceed with checking the
4521 -- associations below, which will catch the case where it's not an
4522 -- aggregate with "others => <>". Note that the legality of a <>
4523 -- aggregate for a null record type was established by AI05-016.
4525 elsif No (First_Entity (Typ))
4526 and then Ada_Version < Ada_2005
4528 Error_Msg_N ("record aggregate must be null", N);
4532 -- STEP 2: Verify aggregate structure
4536 Bad_Aggregate : Boolean := False;
4537 Selector_Name : Node_Id;
4540 if Present (Component_Associations (N)) then
4541 Assoc := First (Component_Associations (N));
4546 while Present (Assoc) loop
4547 Selector_Name := First (Choices (Assoc));
4548 while Present (Selector_Name) loop
4549 if Nkind (Selector_Name) = N_Identifier then
4552 elsif Nkind (Selector_Name) = N_Others_Choice then
4553 if Selector_Name /= First (Choices (Assoc))
4554 or else Present (Next (Selector_Name))
4557 ("OTHERS must appear alone in a choice list",
4561 elsif Present (Next (Assoc)) then
4563 ("OTHERS must appear last in an aggregate",
4567 -- (Ada 2005): If this is an association with a box,
4568 -- indicate that the association need not represent
4571 elsif Box_Present (Assoc) then
4578 ("selector name should be identifier or OTHERS",
4580 Bad_Aggregate := True;
4583 Next (Selector_Name);
4589 if Bad_Aggregate then
4594 -- STEP 3: Find discriminant Values
4597 Discrim : Entity_Id;
4598 Missing_Discriminants : Boolean := False;
4601 if Present (Expressions (N)) then
4602 Positional_Expr := First (Expressions (N));
4604 Positional_Expr := Empty;
4607 -- AI05-0115: if the ancestor part is a subtype mark, the ancestor
4608 -- must not have unknown discriminants.
4609 -- ??? We are not checking any subtype mark here and this code is not
4610 -- exercised by any test, so it's likely wrong (in particular
4611 -- we should not use Root_Type here but the subtype mark, if any),
4612 -- and possibly not needed.
4614 if Is_Derived_Type (Typ)
4615 and then Has_Unknown_Discriminants (Root_Type (Typ))
4616 and then Nkind (N) /= N_Extension_Aggregate
4619 ("aggregate not available for type& whose ancestor "
4620 & "has unknown discriminants ", N, Typ);
4623 if Has_Unknown_Discriminants (Typ)
4624 and then Present (Underlying_Record_View (Typ))
4626 Discrim := First_Discriminant (Underlying_Record_View (Typ));
4627 elsif Has_Discriminants (Typ) then
4628 Discrim := First_Discriminant (Typ);
4633 -- First find the discriminant values in the positional components
4635 while Present (Discrim) and then Present (Positional_Expr) loop
4636 if Discriminant_Present (Discrim) then
4637 Resolve_Aggr_Expr (Positional_Expr, Discrim);
4639 -- Ada 2005 (AI-231)
4641 if Ada_Version >= Ada_2005
4642 and then Known_Null (Positional_Expr)
4644 Check_Can_Never_Be_Null (Discrim, Positional_Expr);
4647 Next (Positional_Expr);
4650 if Present (Get_Value (Discrim, Component_Associations (N))) then
4652 ("more than one value supplied for discriminant&",
4656 Next_Discriminant (Discrim);
4659 -- Find remaining discriminant values if any among named components
4661 while Present (Discrim) loop
4662 Expr := Get_Value (Discrim, Component_Associations (N), True);
4664 if not Discriminant_Present (Discrim) then
4665 if Present (Expr) then
4667 ("more than one value supplied for discriminant &",
4671 elsif No (Expr) then
4673 ("no value supplied for discriminant &", N, Discrim);
4674 Missing_Discriminants := True;
4677 Resolve_Aggr_Expr (Expr, Discrim);
4680 Next_Discriminant (Discrim);
4683 if Missing_Discriminants then
4687 -- At this point and until the beginning of STEP 6, New_Assoc_List
4688 -- contains only the discriminants and their values.
4692 -- STEP 4: Set the Etype of the record aggregate
4694 if Has_Discriminants (Typ)
4695 or else (Has_Unknown_Discriminants (Typ)
4696 and then Present (Underlying_Record_View (Typ)))
4698 Build_Constrained_Itype (N, Typ, New_Assoc_List);
4703 -- STEP 5: Get remaining components according to discriminant values
4707 Errors_Found : Boolean := False;
4708 Record_Def : Node_Id;
4709 Parent_Typ : Entity_Id;
4710 Parent_Typ_List : Elist_Id;
4711 Parent_Elmt : Elmt_Id;
4712 Root_Typ : Entity_Id;
4715 if Is_Derived_Type (Typ) and then Is_Tagged_Type (Typ) then
4716 Parent_Typ_List := New_Elmt_List;
4718 -- If this is an extension aggregate, the component list must
4719 -- include all components that are not in the given ancestor type.
4720 -- Otherwise, the component list must include components of all
4721 -- ancestors, starting with the root.
4723 if Nkind (N) = N_Extension_Aggregate then
4724 Root_Typ := Base_Type (Etype (Ancestor_Part (N)));
4727 -- AI05-0115: check legality of aggregate for type with a
4728 -- private ancestor.
4730 Root_Typ := Root_Type (Typ);
4731 if Has_Private_Ancestor (Typ) then
4733 Ancestor : constant Entity_Id :=
4734 Find_Private_Ancestor (Typ);
4735 Ancestor_Unit : constant Entity_Id :=
4737 (Get_Source_Unit (Ancestor));
4738 Parent_Unit : constant Entity_Id :=
4739 Cunit_Entity (Get_Source_Unit
4740 (Base_Type (Etype (Ancestor))));
4742 -- Check whether we are in a scope that has full view
4743 -- over the private ancestor and its parent. This can
4744 -- only happen if the derivation takes place in a child
4745 -- unit of the unit that declares the parent, and we are
4746 -- in the private part or body of that child unit, else
4747 -- the aggregate is illegal.
4749 if Is_Child_Unit (Ancestor_Unit)
4750 and then Scope (Ancestor_Unit) = Parent_Unit
4751 and then In_Open_Scopes (Scope (Ancestor))
4753 (In_Private_Part (Scope (Ancestor))
4754 or else In_Package_Body (Scope (Ancestor)))
4760 ("type of aggregate has private ancestor&!",
4762 Error_Msg_N ("must use extension aggregate!", N);
4768 Dnode := Declaration_Node (Base_Type (Root_Typ));
4770 -- If we don't get a full declaration, then we have some error
4771 -- which will get signalled later so skip this part. Otherwise
4772 -- gather components of root that apply to the aggregate type.
4773 -- We use the base type in case there is an applicable stored
4774 -- constraint that renames the discriminants of the root.
4776 if Nkind (Dnode) = N_Full_Type_Declaration then
4777 Record_Def := Type_Definition (Dnode);
4780 Component_List (Record_Def),
4781 Governed_By => New_Assoc_List,
4783 Report_Errors => Errors_Found);
4785 if Errors_Found then
4787 ("discriminant controlling variant part is not static",
4794 Parent_Typ := Base_Type (Typ);
4795 while Parent_Typ /= Root_Typ loop
4796 Prepend_Elmt (Parent_Typ, To => Parent_Typ_List);
4797 Parent_Typ := Etype (Parent_Typ);
4799 if Nkind (Parent (Base_Type (Parent_Typ))) =
4800 N_Private_Type_Declaration
4801 or else Nkind (Parent (Base_Type (Parent_Typ))) =
4802 N_Private_Extension_Declaration
4804 if Nkind (N) /= N_Extension_Aggregate then
4806 ("type of aggregate has private ancestor&!",
4808 Error_Msg_N ("must use extension aggregate!", N);
4811 elsif Parent_Typ /= Root_Typ then
4813 ("ancestor part of aggregate must be private type&",
4814 Ancestor_Part (N), Parent_Typ);
4818 -- The current view of ancestor part may be a private type,
4819 -- while the context type is always non-private.
4821 elsif Is_Private_Type (Root_Typ)
4822 and then Present (Full_View (Root_Typ))
4823 and then Nkind (N) = N_Extension_Aggregate
4825 exit when Base_Type (Full_View (Root_Typ)) = Parent_Typ;
4829 -- Now collect components from all other ancestors, beginning
4830 -- with the current type. If the type has unknown discriminants
4831 -- use the component list of the Underlying_Record_View, which
4832 -- needs to be used for the subsequent expansion of the aggregate
4833 -- into assignments.
4835 Parent_Elmt := First_Elmt (Parent_Typ_List);
4836 while Present (Parent_Elmt) loop
4837 Parent_Typ := Node (Parent_Elmt);
4839 if Has_Unknown_Discriminants (Parent_Typ)
4840 and then Present (Underlying_Record_View (Typ))
4842 Parent_Typ := Underlying_Record_View (Parent_Typ);
4845 Record_Def := Type_Definition (Parent (Base_Type (Parent_Typ)));
4846 Gather_Components (Empty,
4847 Component_List (Record_Extension_Part (Record_Def)),
4848 Governed_By => New_Assoc_List,
4850 Report_Errors => Errors_Found);
4852 Next_Elmt (Parent_Elmt);
4855 -- Typ is not a derived tagged type
4858 Record_Def := Type_Definition (Parent (Base_Type (Typ)));
4860 if Null_Present (Record_Def) then
4863 elsif not Has_Unknown_Discriminants (Typ) then
4866 Component_List (Record_Def),
4867 Governed_By => New_Assoc_List,
4869 Report_Errors => Errors_Found);
4873 (Base_Type (Underlying_Record_View (Typ)),
4874 Component_List (Record_Def),
4875 Governed_By => New_Assoc_List,
4877 Report_Errors => Errors_Found);
4881 if Errors_Found then
4886 -- STEP 6: Find component Values
4889 Component_Elmt := First_Elmt (Components);
4891 -- First scan the remaining positional associations in the aggregate.
4892 -- Remember that at this point Positional_Expr contains the current
4893 -- positional association if any is left after looking for discriminant
4894 -- values in step 3.
4896 while Present (Positional_Expr) and then Present (Component_Elmt) loop
4897 Component := Node (Component_Elmt);
4898 Resolve_Aggr_Expr (Positional_Expr, Component);
4900 -- Ada 2005 (AI-231)
4902 if Ada_Version >= Ada_2005 and then Known_Null (Positional_Expr) then
4903 Check_Can_Never_Be_Null (Component, Positional_Expr);
4906 if Present (Get_Value (Component, Component_Associations (N))) then
4908 ("more than one value supplied for Component &", N, Component);
4911 Next (Positional_Expr);
4912 Next_Elmt (Component_Elmt);
4915 if Present (Positional_Expr) then
4917 ("too many components for record aggregate", Positional_Expr);
4920 -- Now scan for the named arguments of the aggregate
4922 while Present (Component_Elmt) loop
4923 Component := Node (Component_Elmt);
4924 Expr := Get_Value (Component, Component_Associations (N), True);
4926 -- Note: The previous call to Get_Value sets the value of the
4927 -- variable Is_Box_Present.
4929 -- Ada 2005 (AI-287): Handle components with default initialization.
4930 -- Note: This feature was originally added to Ada 2005 for limited
4931 -- but it was finally allowed with any type.
4933 if Is_Box_Present then
4934 Check_Box_Component : declare
4935 Ctyp : constant Entity_Id := Etype (Component);
4938 -- If there is a default expression for the aggregate, copy
4939 -- it into a new association. This copy must modify the scopes
4940 -- of internal types that may be attached to the expression
4941 -- (e.g. index subtypes of arrays) because in general the type
4942 -- declaration and the aggregate appear in different scopes,
4943 -- and the backend requires the scope of the type to match the
4944 -- point at which it is elaborated.
4946 -- If the component has an initialization procedure (IP) we
4947 -- pass the component to the expander, which will generate
4948 -- the call to such IP.
4950 -- If the component has discriminants, their values must
4951 -- be taken from their subtype. This is indispensable for
4952 -- constraints that are given by the current instance of an
4953 -- enclosing type, to allow the expansion of the aggregate to
4954 -- replace the reference to the current instance by the target
4955 -- object of the aggregate.
4957 if Present (Parent (Component))
4958 and then Nkind (Parent (Component)) = N_Component_Declaration
4959 and then Present (Expression (Parent (Component)))
4962 New_Copy_Tree_And_Copy_Dimensions
4963 (Expression (Parent (Component)),
4964 New_Scope => Current_Scope,
4965 New_Sloc => Sloc (N));
4967 -- As the type of the copied default expression may refer
4968 -- to discriminants of the record type declaration, these
4969 -- non-stored discriminants need to be rewritten into stored
4970 -- discriminant values for the aggregate. This is required
4971 -- in GNATprove mode, and is adopted in all modes to avoid
4972 -- special-casing GNATprove mode.
4974 if Is_Array_Type (Etype (Expr)) then
4976 Rec_Typ : constant Entity_Id := Scope (Component);
4977 -- Root record type whose discriminants may be used as
4978 -- bounds in range nodes.
4985 -- Rewrite the range nodes occurring in the indexes
4988 Index := First_Index (Etype (Expr));
4989 while Present (Index) loop
4990 Rewrite_Range (Rec_Typ, Index);
4992 (Rec_Typ, Scalar_Range (Etype (Index)));
4997 -- Rewrite the range nodes occurring as aggregate
4998 -- bounds and component associations.
5000 if Nkind (Expr) = N_Aggregate then
5001 if Present (Aggregate_Bounds (Expr)) then
5002 Rewrite_Range (Rec_Typ, Aggregate_Bounds (Expr));
5005 if Present (Component_Associations (Expr)) then
5006 Assoc := First (Component_Associations (Expr));
5007 while Present (Assoc) loop
5008 Choice := First (Choices (Assoc));
5009 while Present (Choice) loop
5010 Rewrite_Range (Rec_Typ, Choice);
5023 (Component => Component,
5025 Assoc_List => New_Assoc_List);
5026 Set_Has_Self_Reference (N);
5028 -- A box-defaulted access component gets the value null. Also
5029 -- included are components of private types whose underlying
5030 -- type is an access type. In either case set the type of the
5031 -- literal, for subsequent use in semantic checks.
5033 elsif Present (Underlying_Type (Ctyp))
5034 and then Is_Access_Type (Underlying_Type (Ctyp))
5036 -- If the component's type is private with an access type as
5037 -- its underlying type then we have to create an unchecked
5038 -- conversion to satisfy type checking.
5040 if Is_Private_Type (Ctyp) then
5042 Qual_Null : constant Node_Id :=
5043 Make_Qualified_Expression (Sloc (N),
5046 (Underlying_Type (Ctyp), Sloc (N)),
5047 Expression => Make_Null (Sloc (N)));
5049 Convert_Null : constant Node_Id :=
5050 Unchecked_Convert_To
5054 Analyze_And_Resolve (Convert_Null, Ctyp);
5056 (Component => Component,
5057 Expr => Convert_Null,
5058 Assoc_List => New_Assoc_List);
5061 -- Otherwise the component type is non-private
5064 Expr := Make_Null (Sloc (N));
5065 Set_Etype (Expr, Ctyp);
5068 (Component => Component,
5070 Assoc_List => New_Assoc_List);
5073 -- Ada 2012: If component is scalar with default value, use it
5074 -- by converting it to Ctyp, so that subtype constraints are
5077 elsif Is_Scalar_Type (Ctyp)
5078 and then Has_Default_Aspect (Ctyp)
5081 Conv : constant Node_Id :=
5086 (Default_Aspect_Value
5087 (First_Subtype (Underlying_Type (Ctyp)))));
5090 Analyze_And_Resolve (Conv, Ctyp);
5092 (Component => Component,
5094 Assoc_List => New_Assoc_List);
5097 elsif Has_Non_Null_Base_Init_Proc (Ctyp)
5098 or else not Expander_Active
5100 if Is_Record_Type (Ctyp)
5101 and then Has_Discriminants (Ctyp)
5102 and then not Is_Private_Type (Ctyp)
5104 -- We build a partially initialized aggregate with the
5105 -- values of the discriminants and box initialization
5106 -- for the rest, if other components are present.
5108 -- The type of the aggregate is the known subtype of
5109 -- the component. The capture of discriminants must be
5110 -- recursive because subcomponents may be constrained
5111 -- (transitively) by discriminants of enclosing types.
5112 -- For a private type with discriminants, a call to the
5113 -- initialization procedure will be generated, and no
5114 -- subaggregate is needed.
5116 Capture_Discriminants : declare
5117 Loc : constant Source_Ptr := Sloc (N);
5121 Expr := Make_Aggregate (Loc, No_List, New_List);
5122 Set_Etype (Expr, Ctyp);
5124 -- If the enclosing type has discriminants, they have
5125 -- been collected in the aggregate earlier, and they
5126 -- may appear as constraints of subcomponents.
5128 -- Similarly if this component has discriminants, they
5129 -- might in turn be propagated to their components.
5131 if Has_Discriminants (Typ) then
5132 Add_Discriminant_Values (Expr, New_Assoc_List);
5133 Propagate_Discriminants (Expr, New_Assoc_List);
5135 elsif Has_Discriminants (Ctyp) then
5136 Add_Discriminant_Values
5137 (Expr, Component_Associations (Expr));
5138 Propagate_Discriminants
5139 (Expr, Component_Associations (Expr));
5141 Build_Constrained_Itype
5142 (Expr, Ctyp, Component_Associations (Expr));
5149 -- If the type has additional components, create
5150 -- an OTHERS box association for them.
5152 Comp := First_Component (Ctyp);
5153 while Present (Comp) loop
5154 if Ekind (Comp) = E_Component then
5155 if not Is_Record_Type (Etype (Comp)) then
5157 (Component_Associations (Expr),
5158 Make_Component_Association (Loc,
5161 Make_Others_Choice (Loc)),
5162 Expression => Empty,
5163 Box_Present => True));
5169 Next_Component (Comp);
5175 (Component => Component,
5177 Assoc_List => New_Assoc_List);
5178 end Capture_Discriminants;
5180 -- Otherwise the component type is not a record, or it has
5181 -- not discriminants, or it is private.
5185 (Component => Component,
5187 Assoc_List => New_Assoc_List,
5188 Is_Box_Present => True);
5191 -- Otherwise we only need to resolve the expression if the
5192 -- component has partially initialized values (required to
5193 -- expand the corresponding assignments and run-time checks).
5195 elsif Present (Expr)
5196 and then Is_Partially_Initialized_Type (Ctyp)
5198 Resolve_Aggr_Expr (Expr, Component);
5200 end Check_Box_Component;
5202 elsif No (Expr) then
5204 -- Ignore hidden components associated with the position of the
5205 -- interface tags: these are initialized dynamically.
5207 if not Present (Related_Type (Component)) then
5209 ("no value supplied for component &!", N, Component);
5213 Resolve_Aggr_Expr (Expr, Component);
5216 Next_Elmt (Component_Elmt);
5219 -- STEP 7: check for invalid components + check type in choice list
5223 New_Assoc : Node_Id;
5229 -- Type of first component in choice list
5232 if Present (Component_Associations (N)) then
5233 Assoc := First (Component_Associations (N));
5238 Verification : while Present (Assoc) loop
5239 Selectr := First (Choices (Assoc));
5242 if Nkind (Selectr) = N_Others_Choice then
5244 -- Ada 2005 (AI-287): others choice may have expression or box
5246 if No (Others_Etype) and then Others_Box = 0 then
5248 ("OTHERS must represent at least one component", Selectr);
5250 elsif Others_Box = 1 and then Warn_On_Redundant_Constructs then
5251 Error_Msg_N ("others choice is redundant?", Box_Node);
5253 ("\previous choices cover all components?", Box_Node);
5259 while Present (Selectr) loop
5260 New_Assoc := First (New_Assoc_List);
5261 while Present (New_Assoc) loop
5262 Component := First (Choices (New_Assoc));
5264 if Chars (Selectr) = Chars (Component) then
5266 Check_Identifier (Selectr, Entity (Component));
5275 -- If no association, this is not a legal component of the type
5276 -- in question, unless its association is provided with a box.
5278 if No (New_Assoc) then
5279 if Box_Present (Parent (Selectr)) then
5281 -- This may still be a bogus component with a box. Scan
5282 -- list of components to verify that a component with
5283 -- that name exists.
5289 C := First_Component (Typ);
5290 while Present (C) loop
5291 if Chars (C) = Chars (Selectr) then
5293 -- If the context is an extension aggregate,
5294 -- the component must not be inherited from
5295 -- the ancestor part of the aggregate.
5297 if Nkind (N) /= N_Extension_Aggregate
5299 Scope (Original_Record_Component (C)) /=
5300 Etype (Ancestor_Part (N))
5310 Error_Msg_Node_2 := Typ;
5311 Error_Msg_N ("& is not a component of}", Selectr);
5315 elsif Chars (Selectr) /= Name_uTag
5316 and then Chars (Selectr) /= Name_uParent
5318 if not Has_Discriminants (Typ) then
5319 Error_Msg_Node_2 := Typ;
5320 Error_Msg_N ("& is not a component of}", Selectr);
5323 ("& is not a component of the aggregate subtype",
5327 Check_Misspelled_Component (Components, Selectr);
5330 elsif No (Typech) then
5331 Typech := Base_Type (Etype (Component));
5333 -- AI05-0199: In Ada 2012, several components of anonymous
5334 -- access types can appear in a choice list, as long as the
5335 -- designated types match.
5337 elsif Typech /= Base_Type (Etype (Component)) then
5338 if Ada_Version >= Ada_2012
5339 and then Ekind (Typech) = E_Anonymous_Access_Type
5341 Ekind (Etype (Component)) = E_Anonymous_Access_Type
5342 and then Base_Type (Designated_Type (Typech)) =
5343 Base_Type (Designated_Type (Etype (Component)))
5345 Subtypes_Statically_Match (Typech, (Etype (Component)))
5349 elsif not Box_Present (Parent (Selectr)) then
5351 ("components in choice list must have same type",
5360 end loop Verification;
5363 -- STEP 8: replace the original aggregate
5366 New_Aggregate : constant Node_Id := New_Copy (N);
5369 Set_Expressions (New_Aggregate, No_List);
5370 Set_Etype (New_Aggregate, Etype (N));
5371 Set_Component_Associations (New_Aggregate, New_Assoc_List);
5372 Set_Check_Actuals (New_Aggregate, Check_Actuals (N));
5374 Rewrite (N, New_Aggregate);
5377 -- Check the dimensions of the components in the record aggregate
5379 Analyze_Dimension_Extension_Or_Record_Aggregate (N);
5380 end Resolve_Record_Aggregate;
5382 -----------------------------
5383 -- Check_Can_Never_Be_Null --
5384 -----------------------------
5386 procedure Check_Can_Never_Be_Null (Typ : Entity_Id; Expr : Node_Id) is
5387 Comp_Typ : Entity_Id;
5391 (Ada_Version >= Ada_2005
5392 and then Present (Expr)
5393 and then Known_Null (Expr));
5396 when E_Array_Type =>
5397 Comp_Typ := Component_Type (Typ);
5402 Comp_Typ := Etype (Typ);
5408 if Can_Never_Be_Null (Comp_Typ) then
5410 -- Here we know we have a constraint error. Note that we do not use
5411 -- Apply_Compile_Time_Constraint_Error here to the Expr, which might
5412 -- seem the more natural approach. That's because in some cases the
5413 -- components are rewritten, and the replacement would be missed.
5414 -- We do not mark the whole aggregate as raising a constraint error,
5415 -- because the association may be a null array range.
5418 ("(Ada 2005) null not allowed in null-excluding component??", Expr);
5420 ("\Constraint_Error will be raised at run time??", Expr);
5423 Make_Raise_Constraint_Error
5424 (Sloc (Expr), Reason => CE_Access_Check_Failed));
5425 Set_Etype (Expr, Comp_Typ);
5426 Set_Analyzed (Expr);
5428 end Check_Can_Never_Be_Null;
5430 ---------------------
5431 -- Sort_Case_Table --
5432 ---------------------
5434 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is
5435 U : constant Int := Case_Table'Last;
5443 T := Case_Table (K + 1);
5447 and then Expr_Value (Case_Table (J - 1).Lo) > Expr_Value (T.Lo)
5449 Case_Table (J) := Case_Table (J - 1);
5453 Case_Table (J) := T;
5456 end Sort_Case_Table;