1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2017, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Einfo; use Einfo;
30 with Elists; use Elists;
31 with Errout; use Errout;
32 with Expander; use Expander;
33 with Exp_Ch6; use Exp_Ch6;
34 with Exp_Tss; use Exp_Tss;
35 with Exp_Util; use Exp_Util;
36 with Freeze; use Freeze;
37 with Itypes; use Itypes;
39 with Lib.Xref; use Lib.Xref;
40 with Namet; use Namet;
41 with Namet.Sp; use Namet.Sp;
42 with Nmake; use Nmake;
43 with Nlists; use Nlists;
45 with Restrict; use Restrict;
46 with Rident; use Rident;
48 with Sem_Aux; use Sem_Aux;
49 with Sem_Cat; use Sem_Cat;
50 with Sem_Ch3; use Sem_Ch3;
51 with Sem_Ch8; use Sem_Ch8;
52 with Sem_Ch13; use Sem_Ch13;
53 with Sem_Dim; use Sem_Dim;
54 with Sem_Eval; use Sem_Eval;
55 with Sem_Res; use Sem_Res;
56 with Sem_Util; use Sem_Util;
57 with Sem_Type; use Sem_Type;
58 with Sem_Warn; use Sem_Warn;
59 with Sinfo; use Sinfo;
60 with Snames; use Snames;
61 with Stringt; use Stringt;
62 with Stand; use Stand;
63 with Style; use Style;
64 with Targparm; use Targparm;
65 with Tbuild; use Tbuild;
66 with Uintp; use Uintp;
68 package body Sem_Aggr is
70 type Case_Bounds is record
72 -- Low bound of choice. Once we sort the Case_Table, then entries
73 -- will be in order of ascending Choice_Lo values.
76 -- High Bound of choice. The sort does not pay any attention to the
77 -- high bound, so choices 1 .. 4 and 1 .. 5 could be in either order.
80 -- If there are duplicates or missing entries, then in the sorted
81 -- table, this records the highest value among Choice_Hi values
82 -- seen so far, including this entry.
85 -- The node of the choice
88 type Case_Table_Type is array (Nat range <>) of Case_Bounds;
89 -- Table type used by Check_Case_Choices procedure. Entry zero is not
90 -- used (reserved for the sort). Real entries start at one.
92 -----------------------
93 -- Local Subprograms --
94 -----------------------
96 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type);
97 -- Sort the Case Table using the Lower Bound of each Choice as the key. A
98 -- simple insertion sort is used since the choices in a case statement will
99 -- usually be in near sorted order.
101 procedure Check_Can_Never_Be_Null (Typ : Entity_Id; Expr : Node_Id);
102 -- Ada 2005 (AI-231): Check bad usage of null for a component for which
103 -- null exclusion (NOT NULL) is specified. Typ can be an E_Array_Type for
104 -- the array case (the component type of the array will be used) or an
105 -- E_Component/E_Discriminant entity in the record case, in which case the
106 -- type of the component will be used for the test. If Typ is any other
107 -- kind of entity, the call is ignored. Expr is the component node in the
108 -- aggregate which is known to have a null value. A warning message will be
109 -- issued if the component is null excluding.
111 -- It would be better to pass the proper type for Typ ???
113 procedure Check_Expr_OK_In_Limited_Aggregate (Expr : Node_Id);
114 -- Check that Expr is either not limited or else is one of the cases of
115 -- expressions allowed for a limited component association (namely, an
116 -- aggregate, function call, or <> notation). Report error for violations.
117 -- Expression is also OK in an instance or inlining context, because we
118 -- have already pre-analyzed and it is known to be type correct.
120 procedure Check_Qualified_Aggregate (Level : Nat; Expr : Node_Id);
121 -- Given aggregate Expr, check that sub-aggregates of Expr that are nested
122 -- at Level are qualified. If Level = 0, this applies to Expr directly.
123 -- Only issue errors in formal verification mode.
125 function Is_Top_Level_Aggregate (Expr : Node_Id) return Boolean;
126 -- Return True of Expr is an aggregate not contained directly in another
129 ------------------------------------------------------
130 -- Subprograms used for RECORD AGGREGATE Processing --
131 ------------------------------------------------------
133 procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id);
134 -- This procedure performs all the semantic checks required for record
135 -- aggregates. Note that for aggregates analysis and resolution go
136 -- hand in hand. Aggregate analysis has been delayed up to here and
137 -- it is done while resolving the aggregate.
139 -- N is the N_Aggregate node.
140 -- Typ is the record type for the aggregate resolution
142 -- While performing the semantic checks, this procedure builds a new
143 -- Component_Association_List where each record field appears alone in a
144 -- Component_Choice_List along with its corresponding expression. The
145 -- record fields in the Component_Association_List appear in the same order
146 -- in which they appear in the record type Typ.
148 -- Once this new Component_Association_List is built and all the semantic
149 -- checks performed, the original aggregate subtree is replaced with the
150 -- new named record aggregate just built. Note that subtree substitution is
151 -- performed with Rewrite so as to be able to retrieve the original
154 -- The aggregate subtree manipulation performed by Resolve_Record_Aggregate
155 -- yields the aggregate format expected by Gigi. Typically, this kind of
156 -- tree manipulations are done in the expander. However, because the
157 -- semantic checks that need to be performed on record aggregates really go
158 -- hand in hand with the record aggregate normalization, the aggregate
159 -- subtree transformation is performed during resolution rather than
160 -- expansion. Had we decided otherwise we would have had to duplicate most
161 -- of the code in the expansion procedure Expand_Record_Aggregate. Note,
162 -- however, that all the expansion concerning aggregates for tagged records
163 -- is done in Expand_Record_Aggregate.
165 -- The algorithm of Resolve_Record_Aggregate proceeds as follows:
167 -- 1. Make sure that the record type against which the record aggregate
168 -- has to be resolved is not abstract. Furthermore if the type is a
169 -- null aggregate make sure the input aggregate N is also null.
171 -- 2. Verify that the structure of the aggregate is that of a record
172 -- aggregate. Specifically, look for component associations and ensure
173 -- that each choice list only has identifiers or the N_Others_Choice
174 -- node. Also make sure that if present, the N_Others_Choice occurs
175 -- last and by itself.
177 -- 3. If Typ contains discriminants, the values for each discriminant is
178 -- looked for. If the record type Typ has variants, we check that the
179 -- expressions corresponding to each discriminant ruling the (possibly
180 -- nested) variant parts of Typ, are static. This allows us to determine
181 -- the variant parts to which the rest of the aggregate must conform.
182 -- The names of discriminants with their values are saved in a new
183 -- association list, New_Assoc_List which is later augmented with the
184 -- names and values of the remaining components in the record type.
186 -- During this phase we also make sure that every discriminant is
187 -- assigned exactly one value. Note that when several values for a given
188 -- discriminant are found, semantic processing continues looking for
189 -- further errors. In this case it's the first discriminant value found
190 -- which we will be recorded.
192 -- IMPORTANT NOTE: For derived tagged types this procedure expects
193 -- First_Discriminant and Next_Discriminant to give the correct list
194 -- of discriminants, in the correct order.
196 -- 4. After all the discriminant values have been gathered, we can set the
197 -- Etype of the record aggregate. If Typ contains no discriminants this
198 -- is straightforward: the Etype of N is just Typ, otherwise a new
199 -- implicit constrained subtype of Typ is built to be the Etype of N.
201 -- 5. Gather the remaining record components according to the discriminant
202 -- values. This involves recursively traversing the record type
203 -- structure to see what variants are selected by the given discriminant
204 -- values. This processing is a little more convoluted if Typ is a
205 -- derived tagged types since we need to retrieve the record structure
206 -- of all the ancestors of Typ.
208 -- 6. After gathering the record components we look for their values in the
209 -- record aggregate and emit appropriate error messages should we not
210 -- find such values or should they be duplicated.
212 -- 7. We then make sure no illegal component names appear in the record
213 -- aggregate and make sure that the type of the record components
214 -- appearing in a same choice list is the same. Finally we ensure that
215 -- the others choice, if present, is used to provide the value of at
216 -- least a record component.
218 -- 8. The original aggregate node is replaced with the new named aggregate
219 -- built in steps 3 through 6, as explained earlier.
221 -- Given the complexity of record aggregate resolution, the primary goal of
222 -- this routine is clarity and simplicity rather than execution and storage
223 -- efficiency. If there are only positional components in the aggregate the
224 -- running time is linear. If there are associations the running time is
225 -- still linear as long as the order of the associations is not too far off
226 -- the order of the components in the record type. If this is not the case
227 -- the running time is at worst quadratic in the size of the association
230 procedure Check_Misspelled_Component
231 (Elements : Elist_Id;
232 Component : Node_Id);
233 -- Give possible misspelling diagnostic if Component is likely to be a
234 -- misspelling of one of the components of the Assoc_List. This is called
235 -- by Resolve_Aggr_Expr after producing an invalid component error message.
237 procedure Check_Static_Discriminated_Subtype (T : Entity_Id; V : Node_Id);
238 -- An optimization: determine whether a discriminated subtype has a static
239 -- constraint, and contains array components whose length is also static,
240 -- either because they are constrained by the discriminant, or because the
241 -- original component bounds are static.
243 -----------------------------------------------------
244 -- Subprograms used for ARRAY AGGREGATE Processing --
245 -----------------------------------------------------
247 function Resolve_Array_Aggregate
250 Index_Constr : Node_Id;
251 Component_Typ : Entity_Id;
252 Others_Allowed : Boolean) return Boolean;
253 -- This procedure performs the semantic checks for an array aggregate.
254 -- True is returned if the aggregate resolution succeeds.
256 -- The procedure works by recursively checking each nested aggregate.
257 -- Specifically, after checking a sub-aggregate nested at the i-th level
258 -- we recursively check all the subaggregates at the i+1-st level (if any).
259 -- Note that for aggregates analysis and resolution go hand in hand.
260 -- Aggregate analysis has been delayed up to here and it is done while
261 -- resolving the aggregate.
263 -- N is the current N_Aggregate node to be checked.
265 -- Index is the index node corresponding to the array sub-aggregate that
266 -- we are currently checking (RM 4.3.3 (8)). Its Etype is the
267 -- corresponding index type (or subtype).
269 -- Index_Constr is the node giving the applicable index constraint if
270 -- any (RM 4.3.3 (10)). It "is a constraint provided by certain
271 -- contexts [...] that can be used to determine the bounds of the array
272 -- value specified by the aggregate". If Others_Allowed below is False
273 -- there is no applicable index constraint and this node is set to Index.
275 -- Component_Typ is the array component type.
277 -- Others_Allowed indicates whether an others choice is allowed
278 -- in the context where the top-level aggregate appeared.
280 -- The algorithm of Resolve_Array_Aggregate proceeds as follows:
282 -- 1. Make sure that the others choice, if present, is by itself and
283 -- appears last in the sub-aggregate. Check that we do not have
284 -- positional and named components in the array sub-aggregate (unless
285 -- the named association is an others choice). Finally if an others
286 -- choice is present, make sure it is allowed in the aggregate context.
288 -- 2. If the array sub-aggregate contains discrete_choices:
290 -- (A) Verify their validity. Specifically verify that:
292 -- (a) If a null range is present it must be the only possible
293 -- choice in the array aggregate.
295 -- (b) Ditto for a non static range.
297 -- (c) Ditto for a non static expression.
299 -- In addition this step analyzes and resolves each discrete_choice,
300 -- making sure that its type is the type of the corresponding Index.
301 -- If we are not at the lowest array aggregate level (in the case of
302 -- multi-dimensional aggregates) then invoke Resolve_Array_Aggregate
303 -- recursively on each component expression. Otherwise, resolve the
304 -- bottom level component expressions against the expected component
305 -- type ONLY IF the component corresponds to a single discrete choice
306 -- which is not an others choice (to see why read the DELAYED
307 -- COMPONENT RESOLUTION below).
309 -- (B) Determine the bounds of the sub-aggregate and lowest and
310 -- highest choice values.
312 -- 3. For positional aggregates:
314 -- (A) Loop over the component expressions either recursively invoking
315 -- Resolve_Array_Aggregate on each of these for multi-dimensional
316 -- array aggregates or resolving the bottom level component
317 -- expressions against the expected component type.
319 -- (B) Determine the bounds of the positional sub-aggregates.
321 -- 4. Try to determine statically whether the evaluation of the array
322 -- sub-aggregate raises Constraint_Error. If yes emit proper
323 -- warnings. The precise checks are the following:
325 -- (A) Check that the index range defined by aggregate bounds is
326 -- compatible with corresponding index subtype.
327 -- We also check against the base type. In fact it could be that
328 -- Low/High bounds of the base type are static whereas those of
329 -- the index subtype are not. Thus if we can statically catch
330 -- a problem with respect to the base type we are guaranteed
331 -- that the same problem will arise with the index subtype
333 -- (B) If we are dealing with a named aggregate containing an others
334 -- choice and at least one discrete choice then make sure the range
335 -- specified by the discrete choices does not overflow the
336 -- aggregate bounds. We also check against the index type and base
337 -- type bounds for the same reasons given in (A).
339 -- (C) If we are dealing with a positional aggregate with an others
340 -- choice make sure the number of positional elements specified
341 -- does not overflow the aggregate bounds. We also check against
342 -- the index type and base type bounds as mentioned in (A).
344 -- Finally construct an N_Range node giving the sub-aggregate bounds.
345 -- Set the Aggregate_Bounds field of the sub-aggregate to be this
346 -- N_Range. The routine Array_Aggr_Subtype below uses such N_Ranges
347 -- to build the appropriate aggregate subtype. Aggregate_Bounds
348 -- information is needed during expansion.
350 -- DELAYED COMPONENT RESOLUTION: The resolution of bottom level component
351 -- expressions in an array aggregate may call Duplicate_Subexpr or some
352 -- other routine that inserts code just outside the outermost aggregate.
353 -- If the array aggregate contains discrete choices or an others choice,
354 -- this may be wrong. Consider for instance the following example.
356 -- type Rec is record
360 -- type Acc_Rec is access Rec;
361 -- Arr : array (1..3) of Acc_Rec := (1 .. 3 => new Rec);
363 -- Then the transformation of "new Rec" that occurs during resolution
364 -- entails the following code modifications
366 -- P7b : constant Acc_Rec := new Rec;
368 -- Arr : array (1..3) of Acc_Rec := (1 .. 3 => P7b);
370 -- This code transformation is clearly wrong, since we need to call
371 -- "new Rec" for each of the 3 array elements. To avoid this problem we
372 -- delay resolution of the components of non positional array aggregates
373 -- to the expansion phase. As an optimization, if the discrete choice
374 -- specifies a single value we do not delay resolution.
376 function Array_Aggr_Subtype (N : Node_Id; Typ : Node_Id) return Entity_Id;
377 -- This routine returns the type or subtype of an array aggregate.
379 -- N is the array aggregate node whose type we return.
381 -- Typ is the context type in which N occurs.
383 -- This routine creates an implicit array subtype whose bounds are
384 -- those defined by the aggregate. When this routine is invoked
385 -- Resolve_Array_Aggregate has already processed aggregate N. Thus the
386 -- Aggregate_Bounds of each sub-aggregate, is an N_Range node giving the
387 -- sub-aggregate bounds. When building the aggregate itype, this function
388 -- traverses the array aggregate N collecting such Aggregate_Bounds and
389 -- constructs the proper array aggregate itype.
391 -- Note that in the case of multidimensional aggregates each inner
392 -- sub-aggregate corresponding to a given array dimension, may provide a
393 -- different bounds. If it is possible to determine statically that
394 -- some sub-aggregates corresponding to the same index do not have the
395 -- same bounds, then a warning is emitted. If such check is not possible
396 -- statically (because some sub-aggregate bounds are dynamic expressions)
397 -- then this job is left to the expander. In all cases the particular
398 -- bounds that this function will chose for a given dimension is the first
399 -- N_Range node for a sub-aggregate corresponding to that dimension.
401 -- Note that the Raises_Constraint_Error flag of an array aggregate
402 -- whose evaluation is determined to raise CE by Resolve_Array_Aggregate,
403 -- is set in Resolve_Array_Aggregate but the aggregate is not
404 -- immediately replaced with a raise CE. In fact, Array_Aggr_Subtype must
405 -- first construct the proper itype for the aggregate (Gigi needs
406 -- this). After constructing the proper itype we will eventually replace
407 -- the top-level aggregate with a raise CE (done in Resolve_Aggregate).
408 -- Of course in cases such as:
410 -- type Arr is array (integer range <>) of Integer;
411 -- A : Arr := (positive range -1 .. 2 => 0);
413 -- The bounds of the aggregate itype are cooked up to look reasonable
414 -- (in this particular case the bounds will be 1 .. 2).
416 procedure Make_String_Into_Aggregate (N : Node_Id);
417 -- A string literal can appear in a context in which a one dimensional
418 -- array of characters is expected. This procedure simply rewrites the
419 -- string as an aggregate, prior to resolution.
421 ---------------------------------
422 -- Delta aggregate processing --
423 ---------------------------------
425 procedure Resolve_Delta_Array_Aggregate (N : Node_Id; Typ : Entity_Id);
426 procedure Resolve_Delta_Record_Aggregate (N : Node_Id; Typ : Entity_Id);
428 ------------------------
429 -- Array_Aggr_Subtype --
430 ------------------------
432 function Array_Aggr_Subtype
434 Typ : Entity_Id) return Entity_Id
436 Aggr_Dimension : constant Pos := Number_Dimensions (Typ);
437 -- Number of aggregate index dimensions
439 Aggr_Range : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
440 -- Constrained N_Range of each index dimension in our aggregate itype
442 Aggr_Low : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
443 Aggr_High : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
444 -- Low and High bounds for each index dimension in our aggregate itype
446 Is_Fully_Positional : Boolean := True;
448 procedure Collect_Aggr_Bounds (N : Node_Id; Dim : Pos);
449 -- N is an array (sub-)aggregate. Dim is the dimension corresponding
450 -- to (sub-)aggregate N. This procedure collects and removes the side
451 -- effects of the constrained N_Range nodes corresponding to each index
452 -- dimension of our aggregate itype. These N_Range nodes are collected
453 -- in Aggr_Range above.
455 -- Likewise collect in Aggr_Low & Aggr_High above the low and high
456 -- bounds of each index dimension. If, when collecting, two bounds
457 -- corresponding to the same dimension are static and found to differ,
458 -- then emit a warning, and mark N as raising Constraint_Error.
460 -------------------------
461 -- Collect_Aggr_Bounds --
462 -------------------------
464 procedure Collect_Aggr_Bounds (N : Node_Id; Dim : Pos) is
465 This_Range : constant Node_Id := Aggregate_Bounds (N);
466 -- The aggregate range node of this specific sub-aggregate
468 This_Low : constant Node_Id := Low_Bound (Aggregate_Bounds (N));
469 This_High : constant Node_Id := High_Bound (Aggregate_Bounds (N));
470 -- The aggregate bounds of this specific sub-aggregate
476 Remove_Side_Effects (This_Low, Variable_Ref => True);
477 Remove_Side_Effects (This_High, Variable_Ref => True);
479 -- Collect the first N_Range for a given dimension that you find.
480 -- For a given dimension they must be all equal anyway.
482 if No (Aggr_Range (Dim)) then
483 Aggr_Low (Dim) := This_Low;
484 Aggr_High (Dim) := This_High;
485 Aggr_Range (Dim) := This_Range;
488 if Compile_Time_Known_Value (This_Low) then
489 if not Compile_Time_Known_Value (Aggr_Low (Dim)) then
490 Aggr_Low (Dim) := This_Low;
492 elsif Expr_Value (This_Low) /= Expr_Value (Aggr_Low (Dim)) then
493 Set_Raises_Constraint_Error (N);
494 Error_Msg_Warn := SPARK_Mode /= On;
495 Error_Msg_N ("sub-aggregate low bound mismatch<<", N);
496 Error_Msg_N ("\Constraint_Error [<<", N);
500 if Compile_Time_Known_Value (This_High) then
501 if not Compile_Time_Known_Value (Aggr_High (Dim)) then
502 Aggr_High (Dim) := This_High;
505 Expr_Value (This_High) /= Expr_Value (Aggr_High (Dim))
507 Set_Raises_Constraint_Error (N);
508 Error_Msg_Warn := SPARK_Mode /= On;
509 Error_Msg_N ("sub-aggregate high bound mismatch<<", N);
510 Error_Msg_N ("\Constraint_Error [<<", N);
515 if Dim < Aggr_Dimension then
517 -- Process positional components
519 if Present (Expressions (N)) then
520 Expr := First (Expressions (N));
521 while Present (Expr) loop
522 Collect_Aggr_Bounds (Expr, Dim + 1);
527 -- Process component associations
529 if Present (Component_Associations (N)) then
530 Is_Fully_Positional := False;
532 Assoc := First (Component_Associations (N));
533 while Present (Assoc) loop
534 Expr := Expression (Assoc);
535 Collect_Aggr_Bounds (Expr, Dim + 1);
540 end Collect_Aggr_Bounds;
542 -- Array_Aggr_Subtype variables
545 -- The final itype of the overall aggregate
547 Index_Constraints : constant List_Id := New_List;
548 -- The list of index constraints of the aggregate itype
550 -- Start of processing for Array_Aggr_Subtype
553 -- Make sure that the list of index constraints is properly attached to
554 -- the tree, and then collect the aggregate bounds.
556 Set_Parent (Index_Constraints, N);
557 Collect_Aggr_Bounds (N, 1);
559 -- Build the list of constrained indexes of our aggregate itype
561 for J in 1 .. Aggr_Dimension loop
562 Create_Index : declare
563 Index_Base : constant Entity_Id :=
564 Base_Type (Etype (Aggr_Range (J)));
565 Index_Typ : Entity_Id;
568 -- Construct the Index subtype, and associate it with the range
569 -- construct that generates it.
572 Create_Itype (Subtype_Kind (Ekind (Index_Base)), Aggr_Range (J));
574 Set_Etype (Index_Typ, Index_Base);
576 if Is_Character_Type (Index_Base) then
577 Set_Is_Character_Type (Index_Typ);
580 Set_Size_Info (Index_Typ, (Index_Base));
581 Set_RM_Size (Index_Typ, RM_Size (Index_Base));
582 Set_First_Rep_Item (Index_Typ, First_Rep_Item (Index_Base));
583 Set_Scalar_Range (Index_Typ, Aggr_Range (J));
585 if Is_Discrete_Or_Fixed_Point_Type (Index_Typ) then
586 Set_RM_Size (Index_Typ, UI_From_Int (Minimum_Size (Index_Typ)));
589 Set_Etype (Aggr_Range (J), Index_Typ);
591 Append (Aggr_Range (J), To => Index_Constraints);
595 -- Now build the Itype
597 Itype := Create_Itype (E_Array_Subtype, N);
599 Set_First_Rep_Item (Itype, First_Rep_Item (Typ));
600 Set_Convention (Itype, Convention (Typ));
601 Set_Depends_On_Private (Itype, Has_Private_Component (Typ));
602 Set_Etype (Itype, Base_Type (Typ));
603 Set_Has_Alignment_Clause (Itype, Has_Alignment_Clause (Typ));
604 Set_Is_Aliased (Itype, Is_Aliased (Typ));
605 Set_Depends_On_Private (Itype, Depends_On_Private (Typ));
607 Copy_Suppress_Status (Index_Check, Typ, Itype);
608 Copy_Suppress_Status (Length_Check, Typ, Itype);
610 Set_First_Index (Itype, First (Index_Constraints));
611 Set_Is_Constrained (Itype, True);
612 Set_Is_Internal (Itype, True);
614 -- A simple optimization: purely positional aggregates of static
615 -- components should be passed to gigi unexpanded whenever possible, and
616 -- regardless of the staticness of the bounds themselves. Subsequent
617 -- checks in exp_aggr verify that type is not packed, etc.
619 Set_Size_Known_At_Compile_Time
622 and then Comes_From_Source (N)
623 and then Size_Known_At_Compile_Time (Component_Type (Typ)));
625 -- We always need a freeze node for a packed array subtype, so that we
626 -- can build the Packed_Array_Impl_Type corresponding to the subtype. If
627 -- expansion is disabled, the packed array subtype is not built, and we
628 -- must not generate a freeze node for the type, or else it will appear
629 -- incomplete to gigi.
632 and then not In_Spec_Expression
633 and then Expander_Active
635 Freeze_Itype (Itype, N);
639 end Array_Aggr_Subtype;
641 --------------------------------
642 -- Check_Misspelled_Component --
643 --------------------------------
645 procedure Check_Misspelled_Component
646 (Elements : Elist_Id;
649 Max_Suggestions : constant := 2;
651 Nr_Of_Suggestions : Natural := 0;
652 Suggestion_1 : Entity_Id := Empty;
653 Suggestion_2 : Entity_Id := Empty;
654 Component_Elmt : Elmt_Id;
657 -- All the components of List are matched against Component and a count
658 -- is maintained of possible misspellings. When at the end of the
659 -- analysis there are one or two (not more) possible misspellings,
660 -- these misspellings will be suggested as possible corrections.
662 Component_Elmt := First_Elmt (Elements);
663 while Nr_Of_Suggestions <= Max_Suggestions
664 and then Present (Component_Elmt)
666 if Is_Bad_Spelling_Of
667 (Chars (Node (Component_Elmt)),
670 Nr_Of_Suggestions := Nr_Of_Suggestions + 1;
672 case Nr_Of_Suggestions is
673 when 1 => Suggestion_1 := Node (Component_Elmt);
674 when 2 => Suggestion_2 := Node (Component_Elmt);
679 Next_Elmt (Component_Elmt);
682 -- Report at most two suggestions
684 if Nr_Of_Suggestions = 1 then
685 Error_Msg_NE -- CODEFIX
686 ("\possible misspelling of&", Component, Suggestion_1);
688 elsif Nr_Of_Suggestions = 2 then
689 Error_Msg_Node_2 := Suggestion_2;
690 Error_Msg_NE -- CODEFIX
691 ("\possible misspelling of& or&", Component, Suggestion_1);
693 end Check_Misspelled_Component;
695 ----------------------------------------
696 -- Check_Expr_OK_In_Limited_Aggregate --
697 ----------------------------------------
699 procedure Check_Expr_OK_In_Limited_Aggregate (Expr : Node_Id) is
701 if Is_Limited_Type (Etype (Expr))
702 and then Comes_From_Source (Expr)
704 if In_Instance_Body or else In_Inlined_Body then
707 elsif not OK_For_Limited_Init (Etype (Expr), Expr) then
709 ("initialization not allowed for limited types", Expr);
710 Explain_Limited_Type (Etype (Expr), Expr);
713 end Check_Expr_OK_In_Limited_Aggregate;
715 -------------------------------
716 -- Check_Qualified_Aggregate --
717 -------------------------------
719 procedure Check_Qualified_Aggregate (Level : Nat; Expr : Node_Id) is
725 if Nkind (Parent (Expr)) /= N_Qualified_Expression then
726 Check_SPARK_05_Restriction ("aggregate should be qualified", Expr);
730 Comp_Expr := First (Expressions (Expr));
731 while Present (Comp_Expr) loop
732 if Nkind (Comp_Expr) = N_Aggregate then
733 Check_Qualified_Aggregate (Level - 1, Comp_Expr);
736 Comp_Expr := Next (Comp_Expr);
739 Comp_Assn := First (Component_Associations (Expr));
740 while Present (Comp_Assn) loop
741 Comp_Expr := Expression (Comp_Assn);
743 if Nkind (Comp_Expr) = N_Aggregate then
744 Check_Qualified_Aggregate (Level - 1, Comp_Expr);
747 Comp_Assn := Next (Comp_Assn);
750 end Check_Qualified_Aggregate;
752 ----------------------------------------
753 -- Check_Static_Discriminated_Subtype --
754 ----------------------------------------
756 procedure Check_Static_Discriminated_Subtype (T : Entity_Id; V : Node_Id) is
757 Disc : constant Entity_Id := First_Discriminant (T);
762 if Has_Record_Rep_Clause (T) then
765 elsif Present (Next_Discriminant (Disc)) then
768 elsif Nkind (V) /= N_Integer_Literal then
772 Comp := First_Component (T);
773 while Present (Comp) loop
774 if Is_Scalar_Type (Etype (Comp)) then
777 elsif Is_Private_Type (Etype (Comp))
778 and then Present (Full_View (Etype (Comp)))
779 and then Is_Scalar_Type (Full_View (Etype (Comp)))
783 elsif Is_Array_Type (Etype (Comp)) then
784 if Is_Bit_Packed_Array (Etype (Comp)) then
788 Ind := First_Index (Etype (Comp));
789 while Present (Ind) loop
790 if Nkind (Ind) /= N_Range
791 or else Nkind (Low_Bound (Ind)) /= N_Integer_Literal
792 or else Nkind (High_Bound (Ind)) /= N_Integer_Literal
804 Next_Component (Comp);
807 -- On exit, all components have statically known sizes
809 Set_Size_Known_At_Compile_Time (T);
810 end Check_Static_Discriminated_Subtype;
812 -------------------------
813 -- Is_Others_Aggregate --
814 -------------------------
816 function Is_Others_Aggregate (Aggr : Node_Id) return Boolean is
818 return No (Expressions (Aggr))
820 Nkind (First (Choice_List (First (Component_Associations (Aggr))))) =
822 end Is_Others_Aggregate;
824 ----------------------------
825 -- Is_Top_Level_Aggregate --
826 ----------------------------
828 function Is_Top_Level_Aggregate (Expr : Node_Id) return Boolean is
830 return Nkind (Parent (Expr)) /= N_Aggregate
831 and then (Nkind (Parent (Expr)) /= N_Component_Association
832 or else Nkind (Parent (Parent (Expr))) /= N_Aggregate);
833 end Is_Top_Level_Aggregate;
835 --------------------------------
836 -- Make_String_Into_Aggregate --
837 --------------------------------
839 procedure Make_String_Into_Aggregate (N : Node_Id) is
840 Exprs : constant List_Id := New_List;
841 Loc : constant Source_Ptr := Sloc (N);
842 Str : constant String_Id := Strval (N);
843 Strlen : constant Nat := String_Length (Str);
851 for J in 1 .. Strlen loop
852 C := Get_String_Char (Str, J);
853 Set_Character_Literal_Name (C);
856 Make_Character_Literal (P,
858 Char_Literal_Value => UI_From_CC (C));
859 Set_Etype (C_Node, Any_Character);
860 Append_To (Exprs, C_Node);
863 -- Something special for wide strings???
866 New_N := Make_Aggregate (Loc, Expressions => Exprs);
867 Set_Analyzed (New_N);
868 Set_Etype (New_N, Any_Composite);
871 end Make_String_Into_Aggregate;
873 -----------------------
874 -- Resolve_Aggregate --
875 -----------------------
877 procedure Resolve_Aggregate (N : Node_Id; Typ : Entity_Id) is
878 Loc : constant Source_Ptr := Sloc (N);
879 Pkind : constant Node_Kind := Nkind (Parent (N));
881 Aggr_Subtyp : Entity_Id;
882 -- The actual aggregate subtype. This is not necessarily the same as Typ
883 -- which is the subtype of the context in which the aggregate was found.
886 -- Ignore junk empty aggregate resulting from parser error
888 if No (Expressions (N))
889 and then No (Component_Associations (N))
890 and then not Null_Record_Present (N)
895 -- If the aggregate has box-initialized components, its type must be
896 -- frozen so that initialization procedures can properly be called
897 -- in the resolution that follows. The replacement of boxes with
898 -- initialization calls is properly an expansion activity but it must
899 -- be done during resolution.
902 and then Present (Component_Associations (N))
908 Comp := First (Component_Associations (N));
909 while Present (Comp) loop
910 if Box_Present (Comp) then
911 Insert_Actions (N, Freeze_Entity (Typ, N));
920 -- An unqualified aggregate is restricted in SPARK to:
922 -- An aggregate item inside an aggregate for a multi-dimensional array
924 -- An expression being assigned to an unconstrained array, but only if
925 -- the aggregate specifies a value for OTHERS only.
927 if Nkind (Parent (N)) = N_Qualified_Expression then
928 if Is_Array_Type (Typ) then
929 Check_Qualified_Aggregate (Number_Dimensions (Typ), N);
931 Check_Qualified_Aggregate (1, N);
934 if Is_Array_Type (Typ)
935 and then Nkind (Parent (N)) = N_Assignment_Statement
936 and then not Is_Constrained (Etype (Name (Parent (N))))
938 if not Is_Others_Aggregate (N) then
939 Check_SPARK_05_Restriction
940 ("array aggregate should have only OTHERS", N);
943 elsif Is_Top_Level_Aggregate (N) then
944 Check_SPARK_05_Restriction ("aggregate should be qualified", N);
946 -- The legality of this unqualified aggregate is checked by calling
947 -- Check_Qualified_Aggregate from one of its enclosing aggregate,
948 -- unless one of these already causes an error to be issued.
955 -- Check for aggregates not allowed in configurable run-time mode.
956 -- We allow all cases of aggregates that do not come from source, since
957 -- these are all assumed to be small (e.g. bounds of a string literal).
958 -- We also allow aggregates of types we know to be small.
960 if not Support_Aggregates_On_Target
961 and then Comes_From_Source (N)
962 and then (not Known_Static_Esize (Typ) or else Esize (Typ) > 64)
964 Error_Msg_CRT ("aggregate", N);
967 -- Ada 2005 (AI-287): Limited aggregates allowed
969 -- In an instance, ignore aggregate subcomponents tnat may be limited,
970 -- because they originate in view conflicts. If the original aggregate
971 -- is legal and the actuals are legal, the aggregate itself is legal.
973 if Is_Limited_Type (Typ)
974 and then Ada_Version < Ada_2005
975 and then not In_Instance
977 Error_Msg_N ("aggregate type cannot be limited", N);
978 Explain_Limited_Type (Typ, N);
980 elsif Is_Class_Wide_Type (Typ) then
981 Error_Msg_N ("type of aggregate cannot be class-wide", N);
983 elsif Typ = Any_String
984 or else Typ = Any_Composite
986 Error_Msg_N ("no unique type for aggregate", N);
987 Set_Etype (N, Any_Composite);
989 elsif Is_Array_Type (Typ) and then Null_Record_Present (N) then
990 Error_Msg_N ("null record forbidden in array aggregate", N);
992 elsif Is_Record_Type (Typ) then
993 Resolve_Record_Aggregate (N, Typ);
995 elsif Is_Array_Type (Typ) then
997 -- First a special test, for the case of a positional aggregate of
998 -- characters which can be replaced by a string literal.
1000 -- Do not perform this transformation if this was a string literal
1001 -- to start with, whose components needed constraint checks, or if
1002 -- the component type is non-static, because it will require those
1003 -- checks and be transformed back into an aggregate. If the index
1004 -- type is not Integer the aggregate may represent a user-defined
1005 -- string type but the context might need the original type so we
1006 -- do not perform the transformation at this point.
1008 if Number_Dimensions (Typ) = 1
1009 and then Is_Standard_Character_Type (Component_Type (Typ))
1010 and then No (Component_Associations (N))
1011 and then not Is_Limited_Composite (Typ)
1012 and then not Is_Private_Composite (Typ)
1013 and then not Is_Bit_Packed_Array (Typ)
1014 and then Nkind (Original_Node (Parent (N))) /= N_String_Literal
1015 and then Is_OK_Static_Subtype (Component_Type (Typ))
1016 and then Base_Type (Etype (First_Index (Typ))) =
1017 Base_Type (Standard_Integer)
1023 Expr := First (Expressions (N));
1024 while Present (Expr) loop
1025 exit when Nkind (Expr) /= N_Character_Literal;
1032 Expr := First (Expressions (N));
1033 while Present (Expr) loop
1034 Store_String_Char (UI_To_CC (Char_Literal_Value (Expr)));
1038 Rewrite (N, Make_String_Literal (Loc, End_String));
1040 Analyze_And_Resolve (N, Typ);
1046 -- Here if we have a real aggregate to deal with
1048 Array_Aggregate : declare
1049 Aggr_Resolved : Boolean;
1051 Aggr_Typ : constant Entity_Id := Etype (Typ);
1052 -- This is the unconstrained array type, which is the type against
1053 -- which the aggregate is to be resolved. Typ itself is the array
1054 -- type of the context which may not be the same subtype as the
1055 -- subtype for the final aggregate.
1058 -- In the following we determine whether an OTHERS choice is
1059 -- allowed inside the array aggregate. The test checks the context
1060 -- in which the array aggregate occurs. If the context does not
1061 -- permit it, or the aggregate type is unconstrained, an OTHERS
1062 -- choice is not allowed (except that it is always allowed on the
1063 -- right-hand side of an assignment statement; in this case the
1064 -- constrainedness of the type doesn't matter).
1066 -- If expansion is disabled (generic context, or semantics-only
1067 -- mode) actual subtypes cannot be constructed, and the type of an
1068 -- object may be its unconstrained nominal type. However, if the
1069 -- context is an assignment, we assume that OTHERS is allowed,
1070 -- because the target of the assignment will have a constrained
1071 -- subtype when fully compiled.
1073 -- Note that there is no node for Explicit_Actual_Parameter.
1074 -- To test for this context we therefore have to test for node
1075 -- N_Parameter_Association which itself appears only if there is a
1076 -- formal parameter. Consequently we also need to test for
1077 -- N_Procedure_Call_Statement or N_Function_Call.
1079 -- The context may be an N_Reference node, created by expansion.
1080 -- Legality of the others clause was established in the source,
1081 -- so the context is legal.
1083 Set_Etype (N, Aggr_Typ); -- May be overridden later on
1085 if Pkind = N_Assignment_Statement
1086 or else (Is_Constrained (Typ)
1088 (Pkind = N_Parameter_Association or else
1089 Pkind = N_Function_Call or else
1090 Pkind = N_Procedure_Call_Statement or else
1091 Pkind = N_Generic_Association or else
1092 Pkind = N_Formal_Object_Declaration or else
1093 Pkind = N_Simple_Return_Statement or else
1094 Pkind = N_Object_Declaration or else
1095 Pkind = N_Component_Declaration or else
1096 Pkind = N_Parameter_Specification or else
1097 Pkind = N_Qualified_Expression or else
1098 Pkind = N_Reference or else
1099 Pkind = N_Aggregate or else
1100 Pkind = N_Extension_Aggregate or else
1101 Pkind = N_Component_Association))
1104 Resolve_Array_Aggregate
1106 Index => First_Index (Aggr_Typ),
1107 Index_Constr => First_Index (Typ),
1108 Component_Typ => Component_Type (Typ),
1109 Others_Allowed => True);
1112 Resolve_Array_Aggregate
1114 Index => First_Index (Aggr_Typ),
1115 Index_Constr => First_Index (Aggr_Typ),
1116 Component_Typ => Component_Type (Typ),
1117 Others_Allowed => False);
1120 if not Aggr_Resolved then
1122 -- A parenthesized expression may have been intended as an
1123 -- aggregate, leading to a type error when analyzing the
1124 -- component. This can also happen for a nested component
1125 -- (see Analyze_Aggr_Expr).
1127 if Paren_Count (N) > 0 then
1129 ("positional aggregate cannot have one component", N);
1132 Aggr_Subtyp := Any_Composite;
1135 Aggr_Subtyp := Array_Aggr_Subtype (N, Typ);
1138 Set_Etype (N, Aggr_Subtyp);
1139 end Array_Aggregate;
1141 elsif Is_Private_Type (Typ)
1142 and then Present (Full_View (Typ))
1143 and then (In_Inlined_Body or In_Instance_Body)
1144 and then Is_Composite_Type (Full_View (Typ))
1146 Resolve (N, Full_View (Typ));
1149 Error_Msg_N ("illegal context for aggregate", N);
1152 -- If we can determine statically that the evaluation of the aggregate
1153 -- raises Constraint_Error, then replace the aggregate with an
1154 -- N_Raise_Constraint_Error node, but set the Etype to the right
1155 -- aggregate subtype. Gigi needs this.
1157 if Raises_Constraint_Error (N) then
1158 Aggr_Subtyp := Etype (N);
1160 Make_Raise_Constraint_Error (Loc, Reason => CE_Range_Check_Failed));
1161 Set_Raises_Constraint_Error (N);
1162 Set_Etype (N, Aggr_Subtyp);
1166 Check_Function_Writable_Actuals (N);
1167 end Resolve_Aggregate;
1169 -----------------------------
1170 -- Resolve_Array_Aggregate --
1171 -----------------------------
1173 function Resolve_Array_Aggregate
1176 Index_Constr : Node_Id;
1177 Component_Typ : Entity_Id;
1178 Others_Allowed : Boolean) return Boolean
1180 Loc : constant Source_Ptr := Sloc (N);
1182 Failure : constant Boolean := False;
1183 Success : constant Boolean := True;
1185 Index_Typ : constant Entity_Id := Etype (Index);
1186 Index_Typ_Low : constant Node_Id := Type_Low_Bound (Index_Typ);
1187 Index_Typ_High : constant Node_Id := Type_High_Bound (Index_Typ);
1188 -- The type of the index corresponding to the array sub-aggregate along
1189 -- with its low and upper bounds.
1191 Index_Base : constant Entity_Id := Base_Type (Index_Typ);
1192 Index_Base_Low : constant Node_Id := Type_Low_Bound (Index_Base);
1193 Index_Base_High : constant Node_Id := Type_High_Bound (Index_Base);
1194 -- Ditto for the base type
1196 Others_Present : Boolean := False;
1198 Nb_Choices : Nat := 0;
1199 -- Contains the overall number of named choices in this sub-aggregate
1201 function Add (Val : Uint; To : Node_Id) return Node_Id;
1202 -- Creates a new expression node where Val is added to expression To.
1203 -- Tries to constant fold whenever possible. To must be an already
1204 -- analyzed expression.
1206 procedure Check_Bound (BH : Node_Id; AH : in out Node_Id);
1207 -- Checks that AH (the upper bound of an array aggregate) is less than
1208 -- or equal to BH (the upper bound of the index base type). If the check
1209 -- fails, a warning is emitted, the Raises_Constraint_Error flag of N is
1210 -- set, and AH is replaced with a duplicate of BH.
1212 procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id);
1213 -- Checks that range AL .. AH is compatible with range L .. H. Emits a
1214 -- warning if not and sets the Raises_Constraint_Error flag in N.
1216 procedure Check_Length (L, H : Node_Id; Len : Uint);
1217 -- Checks that range L .. H contains at least Len elements. Emits a
1218 -- warning if not and sets the Raises_Constraint_Error flag in N.
1220 function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean;
1221 -- Returns True if range L .. H is dynamic or null
1223 procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean);
1224 -- Given expression node From, this routine sets OK to False if it
1225 -- cannot statically evaluate From. Otherwise it stores this static
1226 -- value into Value.
1228 function Resolve_Aggr_Expr
1230 Single_Elmt : Boolean) return Boolean;
1231 -- Resolves aggregate expression Expr. Returns False if resolution
1232 -- fails. If Single_Elmt is set to False, the expression Expr may be
1233 -- used to initialize several array aggregate elements (this can happen
1234 -- for discrete choices such as "L .. H => Expr" or the OTHERS choice).
1235 -- In this event we do not resolve Expr unless expansion is disabled.
1236 -- To know why, see the DELAYED COMPONENT RESOLUTION note above.
1238 -- NOTE: In the case of "... => <>", we pass the in the
1239 -- N_Component_Association node as Expr, since there is no Expression in
1240 -- that case, and we need a Sloc for the error message.
1242 procedure Resolve_Iterated_Component_Association
1244 Index_Typ : Entity_Id);
1251 function Add (Val : Uint; To : Node_Id) return Node_Id is
1257 if Raises_Constraint_Error (To) then
1261 -- First test if we can do constant folding
1263 if Compile_Time_Known_Value (To)
1264 or else Nkind (To) = N_Integer_Literal
1266 Expr_Pos := Make_Integer_Literal (Loc, Expr_Value (To) + Val);
1267 Set_Is_Static_Expression (Expr_Pos);
1268 Set_Etype (Expr_Pos, Etype (To));
1269 Set_Analyzed (Expr_Pos, Analyzed (To));
1271 if not Is_Enumeration_Type (Index_Typ) then
1274 -- If we are dealing with enumeration return
1275 -- Index_Typ'Val (Expr_Pos)
1279 Make_Attribute_Reference
1281 Prefix => New_Occurrence_Of (Index_Typ, Loc),
1282 Attribute_Name => Name_Val,
1283 Expressions => New_List (Expr_Pos));
1289 -- If we are here no constant folding possible
1291 if not Is_Enumeration_Type (Index_Base) then
1294 Left_Opnd => Duplicate_Subexpr (To),
1295 Right_Opnd => Make_Integer_Literal (Loc, Val));
1297 -- If we are dealing with enumeration return
1298 -- Index_Typ'Val (Index_Typ'Pos (To) + Val)
1302 Make_Attribute_Reference
1304 Prefix => New_Occurrence_Of (Index_Typ, Loc),
1305 Attribute_Name => Name_Pos,
1306 Expressions => New_List (Duplicate_Subexpr (To)));
1310 Left_Opnd => To_Pos,
1311 Right_Opnd => Make_Integer_Literal (Loc, Val));
1314 Make_Attribute_Reference
1316 Prefix => New_Occurrence_Of (Index_Typ, Loc),
1317 Attribute_Name => Name_Val,
1318 Expressions => New_List (Expr_Pos));
1320 -- If the index type has a non standard representation, the
1321 -- attributes 'Val and 'Pos expand into function calls and the
1322 -- resulting expression is considered non-safe for reevaluation
1323 -- by the backend. Relocate it into a constant temporary in order
1324 -- to make it safe for reevaluation.
1326 if Has_Non_Standard_Rep (Etype (N)) then
1331 Def_Id := Make_Temporary (Loc, 'R', Expr);
1332 Set_Etype (Def_Id, Index_Typ);
1334 Make_Object_Declaration (Loc,
1335 Defining_Identifier => Def_Id,
1336 Object_Definition =>
1337 New_Occurrence_Of (Index_Typ, Loc),
1338 Constant_Present => True,
1339 Expression => Relocate_Node (Expr)));
1341 Expr := New_Occurrence_Of (Def_Id, Loc);
1353 procedure Check_Bound (BH : Node_Id; AH : in out Node_Id) is
1361 Get (Value => Val_BH, From => BH, OK => OK_BH);
1362 Get (Value => Val_AH, From => AH, OK => OK_AH);
1364 if OK_BH and then OK_AH and then Val_BH < Val_AH then
1365 Set_Raises_Constraint_Error (N);
1366 Error_Msg_Warn := SPARK_Mode /= On;
1367 Error_Msg_N ("upper bound out of range<<", AH);
1368 Error_Msg_N ("\Constraint_Error [<<", AH);
1370 -- You need to set AH to BH or else in the case of enumerations
1371 -- indexes we will not be able to resolve the aggregate bounds.
1373 AH := Duplicate_Subexpr (BH);
1381 procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id) is
1392 pragma Warnings (Off, OK_AL);
1393 pragma Warnings (Off, OK_AH);
1396 if Raises_Constraint_Error (N)
1397 or else Dynamic_Or_Null_Range (AL, AH)
1402 Get (Value => Val_L, From => L, OK => OK_L);
1403 Get (Value => Val_H, From => H, OK => OK_H);
1405 Get (Value => Val_AL, From => AL, OK => OK_AL);
1406 Get (Value => Val_AH, From => AH, OK => OK_AH);
1408 if OK_L and then Val_L > Val_AL then
1409 Set_Raises_Constraint_Error (N);
1410 Error_Msg_Warn := SPARK_Mode /= On;
1411 Error_Msg_N ("lower bound of aggregate out of range<<", N);
1412 Error_Msg_N ("\Constraint_Error [<<", N);
1415 if OK_H and then Val_H < Val_AH then
1416 Set_Raises_Constraint_Error (N);
1417 Error_Msg_Warn := SPARK_Mode /= On;
1418 Error_Msg_N ("upper bound of aggregate out of range<<", N);
1419 Error_Msg_N ("\Constraint_Error [<<", N);
1427 procedure Check_Length (L, H : Node_Id; Len : Uint) is
1437 if Raises_Constraint_Error (N) then
1441 Get (Value => Val_L, From => L, OK => OK_L);
1442 Get (Value => Val_H, From => H, OK => OK_H);
1444 if not OK_L or else not OK_H then
1448 -- If null range length is zero
1450 if Val_L > Val_H then
1451 Range_Len := Uint_0;
1453 Range_Len := Val_H - Val_L + 1;
1456 if Range_Len < Len then
1457 Set_Raises_Constraint_Error (N);
1458 Error_Msg_Warn := SPARK_Mode /= On;
1459 Error_Msg_N ("too many elements<<", N);
1460 Error_Msg_N ("\Constraint_Error [<<", N);
1464 ---------------------------
1465 -- Dynamic_Or_Null_Range --
1466 ---------------------------
1468 function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean is
1476 Get (Value => Val_L, From => L, OK => OK_L);
1477 Get (Value => Val_H, From => H, OK => OK_H);
1479 return not OK_L or else not OK_H
1480 or else not Is_OK_Static_Expression (L)
1481 or else not Is_OK_Static_Expression (H)
1482 or else Val_L > Val_H;
1483 end Dynamic_Or_Null_Range;
1489 procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean) is
1493 if Compile_Time_Known_Value (From) then
1494 Value := Expr_Value (From);
1496 -- If expression From is something like Some_Type'Val (10) then
1499 elsif Nkind (From) = N_Attribute_Reference
1500 and then Attribute_Name (From) = Name_Val
1501 and then Compile_Time_Known_Value (First (Expressions (From)))
1503 Value := Expr_Value (First (Expressions (From)));
1510 -----------------------
1511 -- Resolve_Aggr_Expr --
1512 -----------------------
1514 function Resolve_Aggr_Expr
1516 Single_Elmt : Boolean) return Boolean
1518 Nxt_Ind : constant Node_Id := Next_Index (Index);
1519 Nxt_Ind_Constr : constant Node_Id := Next_Index (Index_Constr);
1520 -- Index is the current index corresponding to the expression
1522 Resolution_OK : Boolean := True;
1523 -- Set to False if resolution of the expression failed
1526 -- Defend against previous errors
1528 if Nkind (Expr) = N_Error
1529 or else Error_Posted (Expr)
1534 -- If the array type against which we are resolving the aggregate
1535 -- has several dimensions, the expressions nested inside the
1536 -- aggregate must be further aggregates (or strings).
1538 if Present (Nxt_Ind) then
1539 if Nkind (Expr) /= N_Aggregate then
1541 -- A string literal can appear where a one-dimensional array
1542 -- of characters is expected. If the literal looks like an
1543 -- operator, it is still an operator symbol, which will be
1544 -- transformed into a string when analyzed.
1546 if Is_Character_Type (Component_Typ)
1547 and then No (Next_Index (Nxt_Ind))
1548 and then Nkind_In (Expr, N_String_Literal, N_Operator_Symbol)
1550 -- A string literal used in a multidimensional array
1551 -- aggregate in place of the final one-dimensional
1552 -- aggregate must not be enclosed in parentheses.
1554 if Paren_Count (Expr) /= 0 then
1555 Error_Msg_N ("no parenthesis allowed here", Expr);
1558 Make_String_Into_Aggregate (Expr);
1561 Error_Msg_N ("nested array aggregate expected", Expr);
1563 -- If the expression is parenthesized, this may be
1564 -- a missing component association for a 1-aggregate.
1566 if Paren_Count (Expr) > 0 then
1568 ("\if single-component aggregate is intended, "
1569 & "write e.g. (1 ='> ...)", Expr);
1576 -- If it's "... => <>", nothing to resolve
1578 if Nkind (Expr) = N_Component_Association then
1579 pragma Assert (Box_Present (Expr));
1583 -- Ada 2005 (AI-231): Propagate the type to the nested aggregate.
1584 -- Required to check the null-exclusion attribute (if present).
1585 -- This value may be overridden later on.
1587 Set_Etype (Expr, Etype (N));
1589 Resolution_OK := Resolve_Array_Aggregate
1590 (Expr, Nxt_Ind, Nxt_Ind_Constr, Component_Typ, Others_Allowed);
1593 -- If it's "... => <>", nothing to resolve
1595 if Nkind (Expr) = N_Component_Association then
1596 pragma Assert (Box_Present (Expr));
1600 -- Do not resolve the expressions of discrete or others choices
1601 -- unless the expression covers a single component, or the
1602 -- expander is inactive.
1604 -- In SPARK mode, expressions that can perform side effects will
1605 -- be recognized by the gnat2why back-end, and the whole
1606 -- subprogram will be ignored. So semantic analysis can be
1607 -- performed safely.
1610 or else not Expander_Active
1611 or else In_Spec_Expression
1613 Analyze_And_Resolve (Expr, Component_Typ);
1614 Check_Expr_OK_In_Limited_Aggregate (Expr);
1615 Check_Non_Static_Context (Expr);
1616 Aggregate_Constraint_Checks (Expr, Component_Typ);
1617 Check_Unset_Reference (Expr);
1621 -- If an aggregate component has a type with predicates, an explicit
1622 -- predicate check must be applied, as for an assignment statement,
1623 -- because the aggegate might not be expanded into individual
1624 -- component assignments. If the expression covers several components
1625 -- the analysis and the predicate check take place later.
1627 if Present (Predicate_Function (Component_Typ))
1628 and then Analyzed (Expr)
1630 Apply_Predicate_Check (Expr, Component_Typ);
1633 if Raises_Constraint_Error (Expr)
1634 and then Nkind (Parent (Expr)) /= N_Component_Association
1636 Set_Raises_Constraint_Error (N);
1639 -- If the expression has been marked as requiring a range check,
1640 -- then generate it here. It's a bit odd to be generating such
1641 -- checks in the analyzer, but harmless since Generate_Range_Check
1642 -- does nothing (other than making sure Do_Range_Check is set) if
1643 -- the expander is not active.
1645 if Do_Range_Check (Expr) then
1646 Generate_Range_Check (Expr, Component_Typ, CE_Range_Check_Failed);
1649 return Resolution_OK;
1650 end Resolve_Aggr_Expr;
1652 --------------------------------------------
1653 -- Resolve_Iterated_Component_Association --
1654 --------------------------------------------
1656 procedure Resolve_Iterated_Component_Association
1658 Index_Typ : Entity_Id)
1660 Id : constant Entity_Id := Defining_Identifier (N);
1661 Loc : constant Source_Ptr := Sloc (N);
1668 Choice := First (Discrete_Choices (N));
1670 while Present (Choice) loop
1671 if Nkind (Choice) = N_Others_Choice then
1672 Others_Present := True;
1677 -- Choice can be a subtype name, a range, or an expression
1679 if Is_Entity_Name (Choice)
1680 and then Is_Type (Entity (Choice))
1681 and then Base_Type (Entity (Choice)) = Base_Type (Index_Typ)
1686 Analyze_And_Resolve (Choice, Index_Typ);
1693 -- Create a scope in which to introduce an index, which is usually
1694 -- visible in the expression for the component, and needed for its
1697 Ent := New_Internal_Entity (E_Loop, Current_Scope, Loc, 'L');
1698 Set_Etype (Ent, Standard_Void_Type);
1699 Set_Parent (Ent, Parent (N));
1701 -- Decorate the index variable in the current scope. The association
1702 -- may have several choices, each one leading to a loop, so we create
1703 -- this variable only once to prevent homonyms in this scope.
1704 -- The expression has to be analyzed once the index variable is
1705 -- directly visible. Mark the variable as referenced to prevent
1706 -- spurious warnings, given that subsequent uses of its name in the
1707 -- expression will reference the internal (synonym) loop variable.
1709 if No (Scope (Id)) then
1711 Set_Etype (Id, Index_Typ);
1712 Set_Ekind (Id, E_Variable);
1713 Set_Scope (Id, Ent);
1714 Set_Referenced (Id);
1718 Dummy := Resolve_Aggr_Expr (Expression (N), False);
1720 end Resolve_Iterated_Component_Association;
1729 Aggr_Low : Node_Id := Empty;
1730 Aggr_High : Node_Id := Empty;
1731 -- The actual low and high bounds of this sub-aggregate
1733 Case_Table_Size : Nat;
1734 -- Contains the size of the case table needed to sort aggregate choices
1736 Choices_Low : Node_Id := Empty;
1737 Choices_High : Node_Id := Empty;
1738 -- The lowest and highest discrete choices values for a named aggregate
1740 Delete_Choice : Boolean;
1741 -- Used when replacing a subtype choice with predicate by a list
1743 Nb_Elements : Uint := Uint_0;
1744 -- The number of elements in a positional aggregate
1746 Nb_Discrete_Choices : Nat := 0;
1747 -- The overall number of discrete choices (not counting others choice)
1749 -- Start of processing for Resolve_Array_Aggregate
1752 -- Ignore junk empty aggregate resulting from parser error
1754 if No (Expressions (N))
1755 and then No (Component_Associations (N))
1756 and then not Null_Record_Present (N)
1761 -- STEP 1: make sure the aggregate is correctly formatted
1763 if Present (Component_Associations (N)) then
1764 Assoc := First (Component_Associations (N));
1765 while Present (Assoc) loop
1766 if Nkind (Assoc) = N_Iterated_Component_Association then
1767 Resolve_Iterated_Component_Association (Assoc, Index_Typ);
1770 Choice := First (Choice_List (Assoc));
1771 Delete_Choice := False;
1772 while Present (Choice) loop
1773 if Nkind (Choice) = N_Others_Choice then
1774 Others_Present := True;
1776 if Choice /= First (Choice_List (Assoc))
1777 or else Present (Next (Choice))
1780 ("OTHERS must appear alone in a choice list", Choice);
1784 if Present (Next (Assoc)) then
1786 ("OTHERS must appear last in an aggregate", Choice);
1790 if Ada_Version = Ada_83
1791 and then Assoc /= First (Component_Associations (N))
1792 and then Nkind_In (Parent (N), N_Assignment_Statement,
1793 N_Object_Declaration)
1796 ("(Ada 83) illegal context for OTHERS choice", N);
1799 elsif Is_Entity_Name (Choice) then
1803 E : constant Entity_Id := Entity (Choice);
1809 if Is_Type (E) and then Has_Predicates (E) then
1810 Freeze_Before (N, E);
1812 if Has_Dynamic_Predicate_Aspect (E) then
1814 ("subtype& has dynamic predicate, not allowed "
1815 & "in aggregate choice", Choice, E);
1817 elsif not Is_OK_Static_Subtype (E) then
1819 ("non-static subtype& has predicate, not allowed "
1820 & "in aggregate choice", Choice, E);
1823 -- If the subtype has a static predicate, replace the
1824 -- original choice with the list of individual values
1825 -- covered by the predicate. Do not perform this
1826 -- transformation if we need to preserve the source
1828 -- This should be deferred to expansion time ???
1830 if Present (Static_Discrete_Predicate (E))
1831 and then not ASIS_Mode
1833 Delete_Choice := True;
1836 P := First (Static_Discrete_Predicate (E));
1837 while Present (P) loop
1839 Set_Sloc (C, Sloc (Choice));
1840 Append_To (New_Cs, C);
1844 Insert_List_After (Choice, New_Cs);
1850 Nb_Choices := Nb_Choices + 1;
1853 C : constant Node_Id := Choice;
1858 if Delete_Choice then
1860 Nb_Choices := Nb_Choices - 1;
1861 Delete_Choice := False;
1870 -- At this point we know that the others choice, if present, is by
1871 -- itself and appears last in the aggregate. Check if we have mixed
1872 -- positional and discrete associations (other than the others choice).
1874 if Present (Expressions (N))
1875 and then (Nb_Choices > 1
1876 or else (Nb_Choices = 1 and then not Others_Present))
1879 ("named association cannot follow positional association",
1880 First (Choice_List (First (Component_Associations (N)))));
1884 -- Test for the validity of an others choice if present
1886 if Others_Present and then not Others_Allowed then
1888 ("OTHERS choice not allowed here",
1889 First (Choices (First (Component_Associations (N)))));
1893 -- Protect against cascaded errors
1895 if Etype (Index_Typ) = Any_Type then
1899 -- STEP 2: Process named components
1901 if No (Expressions (N)) then
1902 if Others_Present then
1903 Case_Table_Size := Nb_Choices - 1;
1905 Case_Table_Size := Nb_Choices;
1909 function Empty_Range (A : Node_Id) return Boolean;
1910 -- If an association covers an empty range, some warnings on the
1911 -- expression of the association can be disabled.
1917 function Empty_Range (A : Node_Id) return Boolean is
1918 R : constant Node_Id := First (Choices (A));
1920 return No (Next (R))
1921 and then Nkind (R) = N_Range
1922 and then Compile_Time_Compare
1923 (Low_Bound (R), High_Bound (R), False) = GT;
1930 -- Denote the lowest and highest values in an aggregate choice
1932 S_Low : Node_Id := Empty;
1933 S_High : Node_Id := Empty;
1934 -- if a choice in an aggregate is a subtype indication these
1935 -- denote the lowest and highest values of the subtype
1937 Table : Case_Table_Type (0 .. Case_Table_Size);
1938 -- Used to sort all the different choice values. Entry zero is
1939 -- reserved for sorting purposes.
1941 Single_Choice : Boolean;
1942 -- Set to true every time there is a single discrete choice in a
1943 -- discrete association
1945 Prev_Nb_Discrete_Choices : Nat;
1946 -- Used to keep track of the number of discrete choices in the
1947 -- current association.
1949 Errors_Posted_On_Choices : Boolean := False;
1950 -- Keeps track of whether any choices have semantic errors
1952 -- Start of processing for Step_2
1955 -- STEP 2 (A): Check discrete choices validity
1957 Assoc := First (Component_Associations (N));
1958 while Present (Assoc) loop
1959 Prev_Nb_Discrete_Choices := Nb_Discrete_Choices;
1960 Choice := First (Choice_List (Assoc));
1965 if Nkind (Choice) = N_Others_Choice then
1966 Single_Choice := False;
1969 -- Test for subtype mark without constraint
1971 elsif Is_Entity_Name (Choice) and then
1972 Is_Type (Entity (Choice))
1974 if Base_Type (Entity (Choice)) /= Index_Base then
1976 ("invalid subtype mark in aggregate choice",
1981 -- Case of subtype indication
1983 elsif Nkind (Choice) = N_Subtype_Indication then
1984 Resolve_Discrete_Subtype_Indication (Choice, Index_Base);
1986 if Has_Dynamic_Predicate_Aspect
1987 (Entity (Subtype_Mark (Choice)))
1990 ("subtype& has dynamic predicate, "
1991 & "not allowed in aggregate choice",
1992 Choice, Entity (Subtype_Mark (Choice)));
1995 -- Does the subtype indication evaluation raise CE?
1997 Get_Index_Bounds (Subtype_Mark (Choice), S_Low, S_High);
1998 Get_Index_Bounds (Choice, Low, High);
1999 Check_Bounds (S_Low, S_High, Low, High);
2001 -- Case of range or expression
2004 Resolve (Choice, Index_Base);
2005 Check_Unset_Reference (Choice);
2006 Check_Non_Static_Context (Choice);
2008 -- If semantic errors were posted on the choice, then
2009 -- record that for possible early return from later
2010 -- processing (see handling of enumeration choices).
2012 if Error_Posted (Choice) then
2013 Errors_Posted_On_Choices := True;
2016 -- Do not range check a choice. This check is redundant
2017 -- since this test is already done when we check that the
2018 -- bounds of the array aggregate are within range.
2020 Set_Do_Range_Check (Choice, False);
2022 -- In SPARK, the choice must be static
2024 if not (Is_OK_Static_Expression (Choice)
2025 or else (Nkind (Choice) = N_Range
2026 and then Is_OK_Static_Range (Choice)))
2028 Check_SPARK_05_Restriction
2029 ("choice should be static", Choice);
2033 -- If we could not resolve the discrete choice stop here
2035 if Etype (Choice) = Any_Type then
2038 -- If the discrete choice raises CE get its original bounds
2040 elsif Nkind (Choice) = N_Raise_Constraint_Error then
2041 Set_Raises_Constraint_Error (N);
2042 Get_Index_Bounds (Original_Node (Choice), Low, High);
2044 -- Otherwise get its bounds as usual
2047 Get_Index_Bounds (Choice, Low, High);
2050 if (Dynamic_Or_Null_Range (Low, High)
2051 or else (Nkind (Choice) = N_Subtype_Indication
2053 Dynamic_Or_Null_Range (S_Low, S_High)))
2054 and then Nb_Choices /= 1
2057 ("dynamic or empty choice in aggregate "
2058 & "must be the only choice", Choice);
2062 if not (All_Composite_Constraints_Static (Low)
2063 and then All_Composite_Constraints_Static (High)
2064 and then All_Composite_Constraints_Static (S_Low)
2065 and then All_Composite_Constraints_Static (S_High))
2067 Check_Restriction (No_Dynamic_Sized_Objects, Choice);
2070 Nb_Discrete_Choices := Nb_Discrete_Choices + 1;
2071 Table (Nb_Discrete_Choices).Lo := Low;
2072 Table (Nb_Discrete_Choices).Hi := High;
2073 Table (Nb_Discrete_Choices).Choice := Choice;
2079 -- Check if we have a single discrete choice and whether
2080 -- this discrete choice specifies a single value.
2083 (Nb_Discrete_Choices = Prev_Nb_Discrete_Choices + 1)
2084 and then (Low = High);
2090 -- Ada 2005 (AI-231)
2092 if Ada_Version >= Ada_2005
2093 and then Known_Null (Expression (Assoc))
2094 and then not Empty_Range (Assoc)
2096 Check_Can_Never_Be_Null (Etype (N), Expression (Assoc));
2099 -- Ada 2005 (AI-287): In case of default initialized component
2100 -- we delay the resolution to the expansion phase.
2102 if Box_Present (Assoc) then
2104 -- Ada 2005 (AI-287): In case of default initialization of a
2105 -- component the expander will generate calls to the
2106 -- corresponding initialization subprogram. We need to call
2107 -- Resolve_Aggr_Expr to check the rules about
2110 if not Resolve_Aggr_Expr
2111 (Assoc, Single_Elmt => Single_Choice)
2116 elsif Nkind (Assoc) = N_Iterated_Component_Association then
2117 null; -- handled above, in a loop context.
2119 elsif not Resolve_Aggr_Expr
2120 (Expression (Assoc), Single_Elmt => Single_Choice)
2124 -- Check incorrect use of dynamically tagged expression
2126 -- We differentiate here two cases because the expression may
2127 -- not be decorated. For example, the analysis and resolution
2128 -- of the expression associated with the others choice will be
2129 -- done later with the full aggregate. In such case we
2130 -- duplicate the expression tree to analyze the copy and
2131 -- perform the required check.
2133 elsif not Present (Etype (Expression (Assoc))) then
2135 Save_Analysis : constant Boolean := Full_Analysis;
2136 Expr : constant Node_Id :=
2137 New_Copy_Tree (Expression (Assoc));
2140 Expander_Mode_Save_And_Set (False);
2141 Full_Analysis := False;
2143 -- Analyze the expression, making sure it is properly
2144 -- attached to the tree before we do the analysis.
2146 Set_Parent (Expr, Parent (Expression (Assoc)));
2149 -- Compute its dimensions now, rather than at the end of
2150 -- resolution, because in the case of multidimensional
2151 -- aggregates subsequent expansion may lead to spurious
2154 Check_Expression_Dimensions (Expr, Component_Typ);
2156 -- If the expression is a literal, propagate this info
2157 -- to the expression in the association, to enable some
2158 -- optimizations downstream.
2160 if Is_Entity_Name (Expr)
2161 and then Present (Entity (Expr))
2162 and then Ekind (Entity (Expr)) = E_Enumeration_Literal
2165 (Expression (Assoc), Component_Typ);
2168 Full_Analysis := Save_Analysis;
2169 Expander_Mode_Restore;
2171 if Is_Tagged_Type (Etype (Expr)) then
2172 Check_Dynamically_Tagged_Expression
2174 Typ => Component_Type (Etype (N)),
2179 elsif Is_Tagged_Type (Etype (Expression (Assoc))) then
2180 Check_Dynamically_Tagged_Expression
2181 (Expr => Expression (Assoc),
2182 Typ => Component_Type (Etype (N)),
2189 -- If aggregate contains more than one choice then these must be
2190 -- static. Check for duplicate and missing values.
2192 -- Note: there is duplicated code here wrt Check_Choice_Set in
2193 -- the body of Sem_Case, and it is possible we could just reuse
2194 -- that procedure. To be checked ???
2196 if Nb_Discrete_Choices > 1 then
2197 Check_Choices : declare
2199 -- Location of choice for messages
2203 -- High end of one range and Low end of the next. Should be
2204 -- contiguous if there is no hole in the list of values.
2208 -- End points of duplicated range
2210 Missing_Or_Duplicates : Boolean := False;
2211 -- Set True if missing or duplicate choices found
2213 procedure Output_Bad_Choices (Lo, Hi : Uint; C : Node_Id);
2214 -- Output continuation message with a representation of the
2215 -- bounds (just Lo if Lo = Hi, else Lo .. Hi). C is the
2216 -- choice node where the message is to be posted.
2218 ------------------------
2219 -- Output_Bad_Choices --
2220 ------------------------
2222 procedure Output_Bad_Choices (Lo, Hi : Uint; C : Node_Id) is
2224 -- Enumeration type case
2226 if Is_Enumeration_Type (Index_Typ) then
2228 Chars (Get_Enum_Lit_From_Pos (Index_Typ, Lo, Loc));
2230 Chars (Get_Enum_Lit_From_Pos (Index_Typ, Hi, Loc));
2233 Error_Msg_N ("\\ %!", C);
2235 Error_Msg_N ("\\ % .. %!", C);
2238 -- Integer types case
2241 Error_Msg_Uint_1 := Lo;
2242 Error_Msg_Uint_2 := Hi;
2245 Error_Msg_N ("\\ ^!", C);
2247 Error_Msg_N ("\\ ^ .. ^!", C);
2250 end Output_Bad_Choices;
2252 -- Start of processing for Check_Choices
2255 Sort_Case_Table (Table);
2257 -- First we do a quick linear loop to find out if we have
2258 -- any duplicates or missing entries (usually we have a
2259 -- legal aggregate, so this will get us out quickly).
2261 for J in 1 .. Nb_Discrete_Choices - 1 loop
2262 Hi_Val := Expr_Value (Table (J).Hi);
2263 Lo_Val := Expr_Value (Table (J + 1).Lo);
2266 or else (Lo_Val > Hi_Val + 1
2267 and then not Others_Present)
2269 Missing_Or_Duplicates := True;
2274 -- If we have missing or duplicate entries, first fill in
2275 -- the Highest entries to make life easier in the following
2276 -- loops to detect bad entries.
2278 if Missing_Or_Duplicates then
2279 Table (1).Highest := Expr_Value (Table (1).Hi);
2281 for J in 2 .. Nb_Discrete_Choices loop
2282 Table (J).Highest :=
2284 (Table (J - 1).Highest, Expr_Value (Table (J).Hi));
2287 -- Loop through table entries to find duplicate indexes
2289 for J in 2 .. Nb_Discrete_Choices loop
2290 Lo_Val := Expr_Value (Table (J).Lo);
2291 Hi_Val := Expr_Value (Table (J).Hi);
2293 -- Case where we have duplicates (the lower bound of
2294 -- this choice is less than or equal to the highest
2295 -- high bound found so far).
2297 if Lo_Val <= Table (J - 1).Highest then
2299 -- We move backwards looking for duplicates. We can
2300 -- abandon this loop as soon as we reach a choice
2301 -- highest value that is less than Lo_Val.
2303 for K in reverse 1 .. J - 1 loop
2304 exit when Table (K).Highest < Lo_Val;
2306 -- Here we may have duplicates between entries
2307 -- for K and J. Get range of duplicates.
2310 UI_Max (Lo_Val, Expr_Value (Table (K).Lo));
2312 UI_Min (Hi_Val, Expr_Value (Table (K).Hi));
2314 -- Nothing to do if duplicate range is null
2316 if Lo_Dup > Hi_Dup then
2319 -- Otherwise place proper message. Because
2320 -- of the missing expansion of subtypes with
2321 -- predicates in ASIS mode, do not report
2322 -- spurious overlap errors.
2326 ((Is_Type (Entity (Table (J).Choice))
2327 and then Has_Predicates
2328 (Entity (Table (J).Choice)))
2330 (Is_Type (Entity (Table (K).Choice))
2331 and then Has_Predicates
2332 (Entity (Table (K).Choice))))
2337 -- We place message on later choice, with a
2338 -- line reference to the earlier choice.
2340 if Sloc (Table (J).Choice) <
2341 Sloc (Table (K).Choice)
2343 Choice := Table (K).Choice;
2344 Error_Msg_Sloc := Sloc (Table (J).Choice);
2346 Choice := Table (J).Choice;
2347 Error_Msg_Sloc := Sloc (Table (K).Choice);
2350 if Lo_Dup = Hi_Dup then
2352 ("index value in array aggregate "
2353 & "duplicates the one given#!", Choice);
2356 ("index values in array aggregate "
2357 & "duplicate those given#!", Choice);
2360 Output_Bad_Choices (Lo_Dup, Hi_Dup, Choice);
2366 -- Loop through entries in table to find missing indexes.
2367 -- Not needed if others, since missing impossible.
2369 if not Others_Present then
2370 for J in 2 .. Nb_Discrete_Choices loop
2371 Lo_Val := Expr_Value (Table (J).Lo);
2372 Hi_Val := Table (J - 1).Highest;
2374 if Lo_Val > Hi_Val + 1 then
2377 Error_Node : Node_Id;
2380 -- If the choice is the bound of a range in
2381 -- a subtype indication, it is not in the
2382 -- source lists for the aggregate itself, so
2383 -- post the error on the aggregate. Otherwise
2384 -- post it on choice itself.
2386 Choice := Table (J).Choice;
2388 if Is_List_Member (Choice) then
2389 Error_Node := Choice;
2394 if Hi_Val + 1 = Lo_Val - 1 then
2396 ("missing index value "
2397 & "in array aggregate!", Error_Node);
2400 ("missing index values "
2401 & "in array aggregate!", Error_Node);
2405 (Hi_Val + 1, Lo_Val - 1, Error_Node);
2411 -- If either missing or duplicate values, return failure
2413 Set_Etype (N, Any_Composite);
2419 -- STEP 2 (B): Compute aggregate bounds and min/max choices values
2421 if Nb_Discrete_Choices > 0 then
2422 Choices_Low := Table (1).Lo;
2423 Choices_High := Table (Nb_Discrete_Choices).Hi;
2426 -- If Others is present, then bounds of aggregate come from the
2427 -- index constraint (not the choices in the aggregate itself).
2429 if Others_Present then
2430 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2432 -- Abandon processing if either bound is already signalled as
2433 -- an error (prevents junk cascaded messages and blow ups).
2435 if Nkind (Aggr_Low) = N_Error
2437 Nkind (Aggr_High) = N_Error
2442 -- No others clause present
2445 -- Special processing if others allowed and not present. This
2446 -- means that the bounds of the aggregate come from the index
2447 -- constraint (and the length must match).
2449 if Others_Allowed then
2450 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2452 -- Abandon processing if either bound is already signalled
2453 -- as an error (stop junk cascaded messages and blow ups).
2455 if Nkind (Aggr_Low) = N_Error
2457 Nkind (Aggr_High) = N_Error
2462 -- If others allowed, and no others present, then the array
2463 -- should cover all index values. If it does not, we will
2464 -- get a length check warning, but there is two cases where
2465 -- an additional warning is useful:
2467 -- If we have no positional components, and the length is
2468 -- wrong (which we can tell by others being allowed with
2469 -- missing components), and the index type is an enumeration
2470 -- type, then issue appropriate warnings about these missing
2471 -- components. They are only warnings, since the aggregate
2472 -- is fine, it's just the wrong length. We skip this check
2473 -- for standard character types (since there are no literals
2474 -- and it is too much trouble to concoct them), and also if
2475 -- any of the bounds have values that are not known at
2478 -- Another case warranting a warning is when the length
2479 -- is right, but as above we have an index type that is
2480 -- an enumeration, and the bounds do not match. This is a
2481 -- case where dubious sliding is allowed and we generate a
2482 -- warning that the bounds do not match.
2484 if No (Expressions (N))
2485 and then Nkind (Index) = N_Range
2486 and then Is_Enumeration_Type (Etype (Index))
2487 and then not Is_Standard_Character_Type (Etype (Index))
2488 and then Compile_Time_Known_Value (Aggr_Low)
2489 and then Compile_Time_Known_Value (Aggr_High)
2490 and then Compile_Time_Known_Value (Choices_Low)
2491 and then Compile_Time_Known_Value (Choices_High)
2493 -- If any of the expressions or range bounds in choices
2494 -- have semantic errors, then do not attempt further
2495 -- resolution, to prevent cascaded errors.
2497 if Errors_Posted_On_Choices then
2502 ALo : constant Node_Id := Expr_Value_E (Aggr_Low);
2503 AHi : constant Node_Id := Expr_Value_E (Aggr_High);
2504 CLo : constant Node_Id := Expr_Value_E (Choices_Low);
2505 CHi : constant Node_Id := Expr_Value_E (Choices_High);
2510 -- Warning case 1, missing values at start/end. Only
2511 -- do the check if the number of entries is too small.
2513 if (Enumeration_Pos (CHi) - Enumeration_Pos (CLo))
2515 (Enumeration_Pos (AHi) - Enumeration_Pos (ALo))
2518 ("missing index value(s) in array aggregate??",
2521 -- Output missing value(s) at start
2523 if Chars (ALo) /= Chars (CLo) then
2526 if Chars (ALo) = Chars (Ent) then
2527 Error_Msg_Name_1 := Chars (ALo);
2528 Error_Msg_N ("\ %??", N);
2530 Error_Msg_Name_1 := Chars (ALo);
2531 Error_Msg_Name_2 := Chars (Ent);
2532 Error_Msg_N ("\ % .. %??", N);
2536 -- Output missing value(s) at end
2538 if Chars (AHi) /= Chars (CHi) then
2541 if Chars (AHi) = Chars (Ent) then
2542 Error_Msg_Name_1 := Chars (Ent);
2543 Error_Msg_N ("\ %??", N);
2545 Error_Msg_Name_1 := Chars (Ent);
2546 Error_Msg_Name_2 := Chars (AHi);
2547 Error_Msg_N ("\ % .. %??", N);
2551 -- Warning case 2, dubious sliding. The First_Subtype
2552 -- test distinguishes between a constrained type where
2553 -- sliding is not allowed (so we will get a warning
2554 -- later that Constraint_Error will be raised), and
2555 -- the unconstrained case where sliding is permitted.
2557 elsif (Enumeration_Pos (CHi) - Enumeration_Pos (CLo))
2559 (Enumeration_Pos (AHi) - Enumeration_Pos (ALo))
2560 and then Chars (ALo) /= Chars (CLo)
2562 not Is_Constrained (First_Subtype (Etype (N)))
2565 ("bounds of aggregate do not match target??", N);
2571 -- If no others, aggregate bounds come from aggregate
2573 Aggr_Low := Choices_Low;
2574 Aggr_High := Choices_High;
2578 -- STEP 3: Process positional components
2581 -- STEP 3 (A): Process positional elements
2583 Expr := First (Expressions (N));
2584 Nb_Elements := Uint_0;
2585 while Present (Expr) loop
2586 Nb_Elements := Nb_Elements + 1;
2588 -- Ada 2005 (AI-231)
2590 if Ada_Version >= Ada_2005 and then Known_Null (Expr) then
2591 Check_Can_Never_Be_Null (Etype (N), Expr);
2594 if not Resolve_Aggr_Expr (Expr, Single_Elmt => True) then
2598 -- Check incorrect use of dynamically tagged expression
2600 if Is_Tagged_Type (Etype (Expr)) then
2601 Check_Dynamically_Tagged_Expression
2603 Typ => Component_Type (Etype (N)),
2610 if Others_Present then
2611 Assoc := Last (Component_Associations (N));
2613 -- Ada 2005 (AI-231)
2615 if Ada_Version >= Ada_2005 and then Known_Null (Assoc) then
2616 Check_Can_Never_Be_Null (Etype (N), Expression (Assoc));
2619 -- Ada 2005 (AI-287): In case of default initialized component,
2620 -- we delay the resolution to the expansion phase.
2622 if Box_Present (Assoc) then
2624 -- Ada 2005 (AI-287): In case of default initialization of a
2625 -- component the expander will generate calls to the
2626 -- corresponding initialization subprogram. We need to call
2627 -- Resolve_Aggr_Expr to check the rules about
2630 if not Resolve_Aggr_Expr (Assoc, Single_Elmt => False) then
2634 elsif not Resolve_Aggr_Expr (Expression (Assoc),
2635 Single_Elmt => False)
2639 -- Check incorrect use of dynamically tagged expression. The
2640 -- expression of the others choice has not been resolved yet.
2641 -- In order to diagnose the semantic error we create a duplicate
2642 -- tree to analyze it and perform the check.
2646 Save_Analysis : constant Boolean := Full_Analysis;
2647 Expr : constant Node_Id :=
2648 New_Copy_Tree (Expression (Assoc));
2651 Expander_Mode_Save_And_Set (False);
2652 Full_Analysis := False;
2654 Full_Analysis := Save_Analysis;
2655 Expander_Mode_Restore;
2657 if Is_Tagged_Type (Etype (Expr)) then
2658 Check_Dynamically_Tagged_Expression
2660 Typ => Component_Type (Etype (N)),
2667 -- STEP 3 (B): Compute the aggregate bounds
2669 if Others_Present then
2670 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2673 if Others_Allowed then
2674 Get_Index_Bounds (Index_Constr, Aggr_Low, Discard);
2676 Aggr_Low := Index_Typ_Low;
2679 Aggr_High := Add (Nb_Elements - 1, To => Aggr_Low);
2680 Check_Bound (Index_Base_High, Aggr_High);
2684 -- STEP 4: Perform static aggregate checks and save the bounds
2688 Check_Bounds (Index_Typ_Low, Index_Typ_High, Aggr_Low, Aggr_High);
2689 Check_Bounds (Index_Base_Low, Index_Base_High, Aggr_Low, Aggr_High);
2693 if Others_Present and then Nb_Discrete_Choices > 0 then
2694 Check_Bounds (Aggr_Low, Aggr_High, Choices_Low, Choices_High);
2695 Check_Bounds (Index_Typ_Low, Index_Typ_High,
2696 Choices_Low, Choices_High);
2697 Check_Bounds (Index_Base_Low, Index_Base_High,
2698 Choices_Low, Choices_High);
2702 elsif Others_Present and then Nb_Elements > 0 then
2703 Check_Length (Aggr_Low, Aggr_High, Nb_Elements);
2704 Check_Length (Index_Typ_Low, Index_Typ_High, Nb_Elements);
2705 Check_Length (Index_Base_Low, Index_Base_High, Nb_Elements);
2708 if Raises_Constraint_Error (Aggr_Low)
2709 or else Raises_Constraint_Error (Aggr_High)
2711 Set_Raises_Constraint_Error (N);
2714 Aggr_Low := Duplicate_Subexpr (Aggr_Low);
2716 -- Do not duplicate Aggr_High if Aggr_High = Aggr_Low + Nb_Elements
2717 -- since the addition node returned by Add is not yet analyzed. Attach
2718 -- to tree and analyze first. Reset analyzed flag to ensure it will get
2719 -- analyzed when it is a literal bound whose type must be properly set.
2721 if Others_Present or else Nb_Discrete_Choices > 0 then
2722 Aggr_High := Duplicate_Subexpr (Aggr_High);
2724 if Etype (Aggr_High) = Universal_Integer then
2725 Set_Analyzed (Aggr_High, False);
2729 -- If the aggregate already has bounds attached to it, it means this is
2730 -- a positional aggregate created as an optimization by
2731 -- Exp_Aggr.Convert_To_Positional, so we don't want to change those
2734 if Present (Aggregate_Bounds (N)) and then not Others_Allowed then
2735 Aggr_Low := Low_Bound (Aggregate_Bounds (N));
2736 Aggr_High := High_Bound (Aggregate_Bounds (N));
2739 Set_Aggregate_Bounds
2740 (N, Make_Range (Loc, Low_Bound => Aggr_Low, High_Bound => Aggr_High));
2742 -- The bounds may contain expressions that must be inserted upwards.
2743 -- Attach them fully to the tree. After analysis, remove side effects
2744 -- from upper bound, if still needed.
2746 Set_Parent (Aggregate_Bounds (N), N);
2747 Analyze_And_Resolve (Aggregate_Bounds (N), Index_Typ);
2748 Check_Unset_Reference (Aggregate_Bounds (N));
2750 if not Others_Present and then Nb_Discrete_Choices = 0 then
2752 (Aggregate_Bounds (N),
2753 Duplicate_Subexpr (High_Bound (Aggregate_Bounds (N))));
2756 -- Check the dimensions of each component in the array aggregate
2758 Analyze_Dimension_Array_Aggregate (N, Component_Typ);
2761 end Resolve_Array_Aggregate;
2763 -----------------------------
2764 -- Resolve_Delta_Aggregate --
2765 -----------------------------
2767 procedure Resolve_Delta_Aggregate (N : Node_Id; Typ : Entity_Id) is
2768 Base : constant Node_Id := Expression (N);
2771 if not Is_Composite_Type (Typ) then
2772 Error_Msg_N ("not a composite type", N);
2775 Analyze_And_Resolve (Base, Typ);
2777 if Is_Array_Type (Typ) then
2778 Resolve_Delta_Array_Aggregate (N, Typ);
2780 Resolve_Delta_Record_Aggregate (N, Typ);
2784 end Resolve_Delta_Aggregate;
2786 -----------------------------------
2787 -- Resolve_Delta_Array_Aggregate --
2788 -----------------------------------
2790 procedure Resolve_Delta_Array_Aggregate (N : Node_Id; Typ : Entity_Id) is
2791 Deltas : constant List_Id := Component_Associations (N);
2795 Index_Type : Entity_Id;
2798 Index_Type := Etype (First_Index (Typ));
2800 Assoc := First (Deltas);
2801 while Present (Assoc) loop
2802 if Nkind (Assoc) = N_Iterated_Component_Association then
2803 Choice := First (Choice_List (Assoc));
2804 while Present (Choice) loop
2805 if Nkind (Choice) = N_Others_Choice then
2807 ("others not allowed in delta aggregate", Choice);
2810 Analyze_And_Resolve (Choice, Index_Type);
2817 Id : constant Entity_Id := Defining_Identifier (Assoc);
2818 Ent : constant Entity_Id :=
2820 (E_Loop, Current_Scope, Sloc (Assoc), 'L');
2823 Set_Etype (Ent, Standard_Void_Type);
2824 Set_Parent (Ent, Assoc);
2826 if No (Scope (Id)) then
2828 Set_Etype (Id, Index_Type);
2829 Set_Ekind (Id, E_Variable);
2830 Set_Scope (Id, Ent);
2835 (New_Copy_Tree (Expression (Assoc)), Component_Type (Typ));
2840 Choice := First (Choice_List (Assoc));
2841 while Present (Choice) loop
2842 if Nkind (Choice) = N_Others_Choice then
2844 ("others not allowed in delta aggregate", Choice);
2849 if Is_Entity_Name (Choice)
2850 and then Is_Type (Entity (Choice))
2852 -- Choice covers a range of values
2854 if Base_Type (Entity (Choice)) /=
2855 Base_Type (Index_Type)
2858 ("choice does mat match index type of",
2862 Resolve (Choice, Index_Type);
2869 Analyze_And_Resolve (Expression (Assoc), Component_Type (Typ));
2874 end Resolve_Delta_Array_Aggregate;
2876 ------------------------------------
2877 -- Resolve_Delta_Record_Aggregate --
2878 ------------------------------------
2880 procedure Resolve_Delta_Record_Aggregate (N : Node_Id; Typ : Entity_Id) is
2882 -- Variables used to verify that discriminant-dependent components
2883 -- appear in the same variant.
2885 Comp_Ref : Entity_Id;
2888 procedure Check_Variant (Id : Entity_Id);
2889 -- If a given component of the delta aggregate appears in a variant
2890 -- part, verify that it is within the same variant as that of previous
2891 -- specified variant components of the delta.
2893 function Get_Component_Type (Nam : Node_Id) return Entity_Id;
2894 -- Locate component with a given name and return its type. If none found
2897 function Nested_In (V1 : Node_Id; V2 : Node_Id) return Boolean;
2898 -- Determine whether variant V1 is within variant V2
2900 function Variant_Depth (N : Node_Id) return Integer;
2901 -- Determine the distance of a variant to the enclosing type
2904 --------------------
2906 --------------------
2908 procedure Check_Variant (Id : Entity_Id) is
2910 Comp_Variant : Node_Id;
2913 if not Has_Discriminants (Typ) then
2917 Comp := First_Entity (Typ);
2918 while Present (Comp) loop
2919 exit when Chars (Comp) = Chars (Id);
2920 Next_Component (Comp);
2923 -- Find the variant, if any, whose component list includes the
2924 -- component declaration.
2926 Comp_Variant := Parent (Parent (List_Containing (Parent (Comp))));
2927 if Nkind (Comp_Variant) = N_Variant then
2928 if No (Variant) then
2929 Variant := Comp_Variant;
2932 elsif Variant /= Comp_Variant then
2934 D1 : constant Integer := Variant_Depth (Variant);
2935 D2 : constant Integer := Variant_Depth (Comp_Variant);
2940 (D1 > D2 and then not Nested_In (Variant, Comp_Variant))
2942 (D2 > D1 and then not Nested_In (Comp_Variant, Variant))
2944 Error_Msg_Node_2 := Comp_Ref;
2946 ("& and & appear in different variants", Id, Comp);
2948 -- Otherwise retain the deeper variant for subsequent tests
2951 Variant := Comp_Variant;
2958 ------------------------
2959 -- Get_Component_Type --
2960 ------------------------
2962 function Get_Component_Type (Nam : Node_Id) return Entity_Id is
2966 Comp := First_Entity (Typ);
2967 while Present (Comp) loop
2968 if Chars (Comp) = Chars (Nam) then
2969 if Ekind (Comp) = E_Discriminant then
2970 Error_Msg_N ("delta cannot apply to discriminant", Nam);
2973 return Etype (Comp);
2976 Comp := Next_Entity (Comp);
2979 Error_Msg_NE ("type& has no component with this name", Nam, Typ);
2981 end Get_Component_Type;
2987 function Nested_In (V1, V2 : Node_Id) return Boolean is
2992 while Nkind (Par) /= N_Full_Type_Declaration loop
2997 Par := Parent (Par);
3007 function Variant_Depth (N : Node_Id) return Integer is
3014 while Nkind (Par) /= N_Full_Type_Declaration loop
3016 Par := Parent (Par);
3024 Deltas : constant List_Id := Component_Associations (N);
3028 Comp_Type : Entity_Id;
3030 -- Start of processing for Resolve_Delta_Record_Aggregate
3035 Assoc := First (Deltas);
3036 while Present (Assoc) loop
3037 Choice := First (Choice_List (Assoc));
3038 while Present (Choice) loop
3039 Comp_Type := Get_Component_Type (Choice);
3041 if Comp_Type /= Any_Type then
3042 Check_Variant (Choice);
3048 Analyze_And_Resolve (Expression (Assoc), Comp_Type);
3051 end Resolve_Delta_Record_Aggregate;
3053 ---------------------------------
3054 -- Resolve_Extension_Aggregate --
3055 ---------------------------------
3057 -- There are two cases to consider:
3059 -- a) If the ancestor part is a type mark, the components needed are the
3060 -- difference between the components of the expected type and the
3061 -- components of the given type mark.
3063 -- b) If the ancestor part is an expression, it must be unambiguous, and
3064 -- once we have its type we can also compute the needed components as in
3065 -- the previous case. In both cases, if the ancestor type is not the
3066 -- immediate ancestor, we have to build this ancestor recursively.
3068 -- In both cases, discriminants of the ancestor type do not play a role in
3069 -- the resolution of the needed components, because inherited discriminants
3070 -- cannot be used in a type extension. As a result we can compute
3071 -- independently the list of components of the ancestor type and of the
3074 procedure Resolve_Extension_Aggregate (N : Node_Id; Typ : Entity_Id) is
3075 A : constant Node_Id := Ancestor_Part (N);
3080 function Valid_Limited_Ancestor (Anc : Node_Id) return Boolean;
3081 -- If the type is limited, verify that the ancestor part is a legal
3082 -- expression (aggregate or function call, including 'Input)) that does
3083 -- not require a copy, as specified in 7.5(2).
3085 function Valid_Ancestor_Type return Boolean;
3086 -- Verify that the type of the ancestor part is a non-private ancestor
3087 -- of the expected type, which must be a type extension.
3089 procedure Transform_BIP_Assignment (Typ : Entity_Id);
3090 -- For an extension aggregate whose ancestor part is a build-in-place
3091 -- call returning a nonlimited type, this is used to transform the
3092 -- assignment to the ancestor part to use a temp.
3094 ----------------------------
3095 -- Valid_Limited_Ancestor --
3096 ----------------------------
3098 function Valid_Limited_Ancestor (Anc : Node_Id) return Boolean is
3100 if Is_Entity_Name (Anc) and then Is_Type (Entity (Anc)) then
3103 -- The ancestor must be a call or an aggregate, but a call may
3104 -- have been expanded into a temporary, so check original node.
3106 elsif Nkind_In (Anc, N_Aggregate,
3107 N_Extension_Aggregate,
3112 elsif Nkind (Original_Node (Anc)) = N_Function_Call then
3115 elsif Nkind (Anc) = N_Attribute_Reference
3116 and then Attribute_Name (Anc) = Name_Input
3120 elsif Nkind (Anc) = N_Qualified_Expression then
3121 return Valid_Limited_Ancestor (Expression (Anc));
3126 end Valid_Limited_Ancestor;
3128 -------------------------
3129 -- Valid_Ancestor_Type --
3130 -------------------------
3132 function Valid_Ancestor_Type return Boolean is
3133 Imm_Type : Entity_Id;
3136 Imm_Type := Base_Type (Typ);
3137 while Is_Derived_Type (Imm_Type) loop
3138 if Etype (Imm_Type) = Base_Type (A_Type) then
3141 -- The base type of the parent type may appear as a private
3142 -- extension if it is declared as such in a parent unit of the
3143 -- current one. For consistency of the subsequent analysis use
3144 -- the partial view for the ancestor part.
3146 elsif Is_Private_Type (Etype (Imm_Type))
3147 and then Present (Full_View (Etype (Imm_Type)))
3148 and then Base_Type (A_Type) = Full_View (Etype (Imm_Type))
3150 A_Type := Etype (Imm_Type);
3153 -- The parent type may be a private extension. The aggregate is
3154 -- legal if the type of the aggregate is an extension of it that
3155 -- is not a private extension.
3157 elsif Is_Private_Type (A_Type)
3158 and then not Is_Private_Type (Imm_Type)
3159 and then Present (Full_View (A_Type))
3160 and then Base_Type (Full_View (A_Type)) = Etype (Imm_Type)
3165 Imm_Type := Etype (Base_Type (Imm_Type));
3169 -- If previous loop did not find a proper ancestor, report error
3171 Error_Msg_NE ("expect ancestor type of &", A, Typ);
3173 end Valid_Ancestor_Type;
3175 ------------------------------
3176 -- Transform_BIP_Assignment --
3177 ------------------------------
3179 procedure Transform_BIP_Assignment (Typ : Entity_Id) is
3180 Loc : constant Source_Ptr := Sloc (N);
3181 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'Y', A);
3182 Obj_Decl : constant Node_Id :=
3183 Make_Object_Declaration (Loc,
3184 Defining_Identifier => Def_Id,
3185 Constant_Present => True,
3186 Object_Definition => New_Occurrence_Of (Typ, Loc),
3188 Has_Init_Expression => True);
3190 Set_Etype (Def_Id, Typ);
3191 Set_Ancestor_Part (N, New_Occurrence_Of (Def_Id, Loc));
3192 Insert_Action (N, Obj_Decl);
3193 end Transform_BIP_Assignment;
3195 -- Start of processing for Resolve_Extension_Aggregate
3198 -- Analyze the ancestor part and account for the case where it is a
3199 -- parameterless function call.
3202 Check_Parameterless_Call (A);
3204 -- In SPARK, the ancestor part cannot be a type mark
3206 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
3207 Check_SPARK_05_Restriction ("ancestor part cannot be a type mark", A);
3209 -- AI05-0115: if the ancestor part is a subtype mark, the ancestor
3210 -- must not have unknown discriminants.
3212 if Has_Unknown_Discriminants (Root_Type (Typ)) then
3214 ("aggregate not available for type& whose ancestor "
3215 & "has unknown discriminants", N, Typ);
3219 if not Is_Tagged_Type (Typ) then
3220 Error_Msg_N ("type of extension aggregate must be tagged", N);
3223 elsif Is_Limited_Type (Typ) then
3225 -- Ada 2005 (AI-287): Limited aggregates are allowed
3227 if Ada_Version < Ada_2005 then
3228 Error_Msg_N ("aggregate type cannot be limited", N);
3229 Explain_Limited_Type (Typ, N);
3232 elsif Valid_Limited_Ancestor (A) then
3237 ("limited ancestor part must be aggregate or function call", A);
3240 elsif Is_Class_Wide_Type (Typ) then
3241 Error_Msg_N ("aggregate cannot be of a class-wide type", N);
3245 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
3246 A_Type := Get_Full_View (Entity (A));
3248 if Valid_Ancestor_Type then
3249 Set_Entity (A, A_Type);
3250 Set_Etype (A, A_Type);
3252 Validate_Ancestor_Part (N);
3253 Resolve_Record_Aggregate (N, Typ);
3256 elsif Nkind (A) /= N_Aggregate then
3257 if Is_Overloaded (A) then
3260 Get_First_Interp (A, I, It);
3261 while Present (It.Typ) loop
3263 -- Consider limited interpretations if Ada 2005 or higher
3265 if Is_Tagged_Type (It.Typ)
3266 and then (Ada_Version >= Ada_2005
3267 or else not Is_Limited_Type (It.Typ))
3269 if A_Type /= Any_Type then
3270 Error_Msg_N ("cannot resolve expression", A);
3277 Get_Next_Interp (I, It);
3280 if A_Type = Any_Type then
3281 if Ada_Version >= Ada_2005 then
3283 ("ancestor part must be of a tagged type", A);
3286 ("ancestor part must be of a nonlimited tagged type", A);
3293 A_Type := Etype (A);
3296 if Valid_Ancestor_Type then
3297 Resolve (A, A_Type);
3298 Check_Unset_Reference (A);
3299 Check_Non_Static_Context (A);
3301 -- The aggregate is illegal if the ancestor expression is a call
3302 -- to a function with a limited unconstrained result, unless the
3303 -- type of the aggregate is a null extension. This restriction
3304 -- was added in AI05-67 to simplify implementation.
3306 if Nkind (A) = N_Function_Call
3307 and then Is_Limited_Type (A_Type)
3308 and then not Is_Null_Extension (Typ)
3309 and then not Is_Constrained (A_Type)
3312 ("type of limited ancestor part must be constrained", A);
3314 -- Reject the use of CPP constructors that leave objects partially
3315 -- initialized. For example:
3317 -- type CPP_Root is tagged limited record ...
3318 -- pragma Import (CPP, CPP_Root);
3320 -- type CPP_DT is new CPP_Root and Iface ...
3321 -- pragma Import (CPP, CPP_DT);
3323 -- type Ada_DT is new CPP_DT with ...
3325 -- Obj : Ada_DT := Ada_DT'(New_CPP_Root with others => <>);
3327 -- Using the constructor of CPP_Root the slots of the dispatch
3328 -- table of CPP_DT cannot be set, and the secondary tag of
3329 -- CPP_DT is unknown.
3331 elsif Nkind (A) = N_Function_Call
3332 and then Is_CPP_Constructor_Call (A)
3333 and then Enclosing_CPP_Parent (Typ) /= A_Type
3336 ("??must use 'C'P'P constructor for type &", A,
3337 Enclosing_CPP_Parent (Typ));
3339 -- The following call is not needed if the previous warning
3340 -- is promoted to an error.
3342 Resolve_Record_Aggregate (N, Typ);
3344 elsif Is_Class_Wide_Type (Etype (A))
3345 and then Nkind (Original_Node (A)) = N_Function_Call
3347 -- If the ancestor part is a dispatching call, it appears
3348 -- statically to be a legal ancestor, but it yields any member
3349 -- of the class, and it is not possible to determine whether
3350 -- it is an ancestor of the extension aggregate (much less
3351 -- which ancestor). It is not possible to determine the
3352 -- components of the extension part.
3354 -- This check implements AI-306, which in fact was motivated by
3355 -- an AdaCore query to the ARG after this test was added.
3357 Error_Msg_N ("ancestor part must be statically tagged", A);
3359 -- We are using the build-in-place protocol, but we can't build
3360 -- in place, because we need to call the function before
3361 -- allocating the aggregate. Could do better for null
3362 -- extensions, and maybe for nondiscriminated types.
3363 -- This is wrong for limited, but those were wrong already.
3365 if not Is_Limited_View (A_Type)
3366 and then Is_Build_In_Place_Function_Call (A)
3368 Transform_BIP_Assignment (A_Type);
3371 Resolve_Record_Aggregate (N, Typ);
3376 Error_Msg_N ("no unique type for this aggregate", A);
3379 Check_Function_Writable_Actuals (N);
3380 end Resolve_Extension_Aggregate;
3382 ------------------------------
3383 -- Resolve_Record_Aggregate --
3384 ------------------------------
3386 procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id) is
3387 New_Assoc_List : constant List_Id := New_List;
3388 -- New_Assoc_List is the newly built list of N_Component_Association
3391 Others_Etype : Entity_Id := Empty;
3392 -- This variable is used to save the Etype of the last record component
3393 -- that takes its value from the others choice. Its purpose is:
3395 -- (a) make sure the others choice is useful
3397 -- (b) make sure the type of all the components whose value is
3398 -- subsumed by the others choice are the same.
3400 -- This variable is updated as a side effect of function Get_Value.
3402 Box_Node : Node_Id := Empty;
3403 Is_Box_Present : Boolean := False;
3404 Others_Box : Integer := 0;
3405 -- Ada 2005 (AI-287): Variables used in case of default initialization
3406 -- to provide a functionality similar to Others_Etype. Box_Present
3407 -- indicates that the component takes its default initialization;
3408 -- Others_Box counts the number of components of the current aggregate
3409 -- (which may be a sub-aggregate of a larger one) that are default-
3410 -- initialized. A value of One indicates that an others_box is present.
3411 -- Any larger value indicates that the others_box is not redundant.
3412 -- These variables, similar to Others_Etype, are also updated as a side
3413 -- effect of function Get_Value. Box_Node is used to place a warning on
3414 -- a redundant others_box.
3416 procedure Add_Association
3417 (Component : Entity_Id;
3419 Assoc_List : List_Id;
3420 Is_Box_Present : Boolean := False);
3421 -- Builds a new N_Component_Association node which associates Component
3422 -- to expression Expr and adds it to the association list being built,
3423 -- either New_Assoc_List, or the association being built for an inner
3426 procedure Add_Discriminant_Values
3427 (New_Aggr : Node_Id;
3428 Assoc_List : List_Id);
3429 -- The constraint to a component may be given by a discriminant of the
3430 -- enclosing type, in which case we have to retrieve its value, which is
3431 -- part of the enclosing aggregate. Assoc_List provides the discriminant
3432 -- associations of the current type or of some enclosing record.
3434 function Discriminant_Present (Input_Discr : Entity_Id) return Boolean;
3435 -- If aggregate N is a regular aggregate this routine will return True.
3436 -- Otherwise, if N is an extension aggregate, then Input_Discr denotes
3437 -- a discriminant whose value may already have been specified by N's
3438 -- ancestor part. This routine checks whether this is indeed the case
3439 -- and if so returns False, signaling that no value for Input_Discr
3440 -- should appear in N's aggregate part. Also, in this case, the routine
3441 -- appends to New_Assoc_List the discriminant value specified in the
3444 -- If the aggregate is in a context with expansion delayed, it will be
3445 -- reanalyzed. The inherited discriminant values must not be reinserted
3446 -- in the component list to prevent spurious errors, but they must be
3447 -- present on first analysis to build the proper subtype indications.
3448 -- The flag Inherited_Discriminant is used to prevent the re-insertion.
3450 function Find_Private_Ancestor (Typ : Entity_Id) return Entity_Id;
3451 -- AI05-0115: Find earlier ancestor in the derivation chain that is
3452 -- derived from private view Typ. Whether the aggregate is legal depends
3453 -- on the current visibility of the type as well as that of the parent
3459 Consider_Others_Choice : Boolean := False) return Node_Id;
3460 -- Given a record component stored in parameter Compon, this function
3461 -- returns its value as it appears in the list From, which is a list
3462 -- of N_Component_Association nodes.
3464 -- If no component association has a choice for the searched component,
3465 -- the value provided by the others choice is returned, if there is one,
3466 -- and Consider_Others_Choice is set to true. Otherwise Empty is
3467 -- returned. If there is more than one component association giving a
3468 -- value for the searched record component, an error message is emitted
3469 -- and the first found value is returned.
3471 -- If Consider_Others_Choice is set and the returned expression comes
3472 -- from the others choice, then Others_Etype is set as a side effect.
3473 -- An error message is emitted if the components taking their value from
3474 -- the others choice do not have same type.
3476 procedure Propagate_Discriminants
3478 Assoc_List : List_Id);
3479 -- Nested components may themselves be discriminated types constrained
3480 -- by outer discriminants, whose values must be captured before the
3481 -- aggregate is expanded into assignments.
3483 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Entity_Id);
3484 -- Analyzes and resolves expression Expr against the Etype of the
3485 -- Component. This routine also applies all appropriate checks to Expr.
3486 -- It finally saves a Expr in the newly created association list that
3487 -- will be attached to the final record aggregate. Note that if the
3488 -- Parent pointer of Expr is not set then Expr was produced with a
3489 -- New_Copy_Tree or some such.
3491 procedure Rewrite_Range (Root_Type : Entity_Id; Rge : Node_Id);
3492 -- Rewrite a range node Rge when its bounds refer to non-stored
3493 -- discriminants from Root_Type, to replace them with the stored
3494 -- discriminant values. This is required in GNATprove mode, and is
3495 -- adopted in all modes to avoid special-casing GNATprove mode.
3497 ---------------------
3498 -- Add_Association --
3499 ---------------------
3501 procedure Add_Association
3502 (Component : Entity_Id;
3504 Assoc_List : List_Id;
3505 Is_Box_Present : Boolean := False)
3507 Choice_List : constant List_Id := New_List;
3511 -- If this is a box association the expression is missing, so use the
3512 -- Sloc of the aggregate itself for the new association.
3514 if Present (Expr) then
3520 Append_To (Choice_List, New_Occurrence_Of (Component, Loc));
3522 Append_To (Assoc_List,
3523 Make_Component_Association (Loc,
3524 Choices => Choice_List,
3526 Box_Present => Is_Box_Present));
3527 end Add_Association;
3529 -----------------------------
3530 -- Add_Discriminant_Values --
3531 -----------------------------
3533 procedure Add_Discriminant_Values
3534 (New_Aggr : Node_Id;
3535 Assoc_List : List_Id)
3539 Discr_Elmt : Elmt_Id;
3540 Discr_Val : Node_Id;
3544 Discr := First_Discriminant (Etype (New_Aggr));
3545 Discr_Elmt := First_Elmt (Discriminant_Constraint (Etype (New_Aggr)));
3546 while Present (Discr_Elmt) loop
3547 Discr_Val := Node (Discr_Elmt);
3549 -- If the constraint is given by a discriminant then it is a
3550 -- discriminant of an enclosing record, and its value has already
3551 -- been placed in the association list.
3553 if Is_Entity_Name (Discr_Val)
3554 and then Ekind (Entity (Discr_Val)) = E_Discriminant
3556 Val := Entity (Discr_Val);
3558 Assoc := First (Assoc_List);
3559 while Present (Assoc) loop
3560 if Present (Entity (First (Choices (Assoc))))
3561 and then Entity (First (Choices (Assoc))) = Val
3563 Discr_Val := Expression (Assoc);
3572 (Discr, New_Copy_Tree (Discr_Val),
3573 Component_Associations (New_Aggr));
3575 -- If the discriminant constraint is a current instance, mark the
3576 -- current aggregate so that the self-reference can be expanded
3577 -- later. The constraint may refer to the subtype of aggregate, so
3578 -- use base type for comparison.
3580 if Nkind (Discr_Val) = N_Attribute_Reference
3581 and then Is_Entity_Name (Prefix (Discr_Val))
3582 and then Is_Type (Entity (Prefix (Discr_Val)))
3583 and then Base_Type (Etype (N)) = Entity (Prefix (Discr_Val))
3585 Set_Has_Self_Reference (N);
3588 Next_Elmt (Discr_Elmt);
3589 Next_Discriminant (Discr);
3591 end Add_Discriminant_Values;
3593 --------------------------
3594 -- Discriminant_Present --
3595 --------------------------
3597 function Discriminant_Present (Input_Discr : Entity_Id) return Boolean is
3598 Regular_Aggr : constant Boolean := Nkind (N) /= N_Extension_Aggregate;
3600 Ancestor_Is_Subtyp : Boolean;
3605 Ancestor_Typ : Entity_Id;
3606 Comp_Assoc : Node_Id;
3608 Discr_Expr : Node_Id;
3609 Discr_Val : Elmt_Id := No_Elmt;
3610 Orig_Discr : Entity_Id;
3613 if Regular_Aggr then
3617 -- Check whether inherited discriminant values have already been
3618 -- inserted in the aggregate. This will be the case if we are
3619 -- re-analyzing an aggregate whose expansion was delayed.
3621 if Present (Component_Associations (N)) then
3622 Comp_Assoc := First (Component_Associations (N));
3623 while Present (Comp_Assoc) loop
3624 if Inherited_Discriminant (Comp_Assoc) then
3632 Ancestor := Ancestor_Part (N);
3633 Ancestor_Typ := Etype (Ancestor);
3634 Loc := Sloc (Ancestor);
3636 -- For a private type with unknown discriminants, use the underlying
3637 -- record view if it is available.
3639 if Has_Unknown_Discriminants (Ancestor_Typ)
3640 and then Present (Full_View (Ancestor_Typ))
3641 and then Present (Underlying_Record_View (Full_View (Ancestor_Typ)))
3643 Ancestor_Typ := Underlying_Record_View (Full_View (Ancestor_Typ));
3646 Ancestor_Is_Subtyp :=
3647 Is_Entity_Name (Ancestor) and then Is_Type (Entity (Ancestor));
3649 -- If the ancestor part has no discriminants clearly N's aggregate
3650 -- part must provide a value for Discr.
3652 if not Has_Discriminants (Ancestor_Typ) then
3655 -- If the ancestor part is an unconstrained subtype mark then the
3656 -- Discr must be present in N's aggregate part.
3658 elsif Ancestor_Is_Subtyp
3659 and then not Is_Constrained (Entity (Ancestor))
3664 -- Now look to see if Discr was specified in the ancestor part
3666 if Ancestor_Is_Subtyp then
3668 First_Elmt (Discriminant_Constraint (Entity (Ancestor)));
3671 Orig_Discr := Original_Record_Component (Input_Discr);
3673 Discr := First_Discriminant (Ancestor_Typ);
3674 while Present (Discr) loop
3676 -- If Ancestor has already specified Disc value then insert its
3677 -- value in the final aggregate.
3679 if Original_Record_Component (Discr) = Orig_Discr then
3680 if Ancestor_Is_Subtyp then
3681 Discr_Expr := New_Copy_Tree (Node (Discr_Val));
3684 Make_Selected_Component (Loc,
3685 Prefix => Duplicate_Subexpr (Ancestor),
3686 Selector_Name => New_Occurrence_Of (Input_Discr, Loc));
3689 Resolve_Aggr_Expr (Discr_Expr, Input_Discr);
3690 Set_Inherited_Discriminant (Last (New_Assoc_List));
3694 Next_Discriminant (Discr);
3696 if Ancestor_Is_Subtyp then
3697 Next_Elmt (Discr_Val);
3702 end Discriminant_Present;
3704 ---------------------------
3705 -- Find_Private_Ancestor --
3706 ---------------------------
3708 function Find_Private_Ancestor (Typ : Entity_Id) return Entity_Id is
3714 if Has_Private_Ancestor (Par)
3715 and then not Has_Private_Ancestor (Etype (Base_Type (Par)))
3719 elsif not Is_Derived_Type (Par) then
3723 Par := Etype (Base_Type (Par));
3726 end Find_Private_Ancestor;
3735 Consider_Others_Choice : Boolean := False) return Node_Id
3737 Typ : constant Entity_Id := Etype (Compon);
3739 Expr : Node_Id := Empty;
3740 Selector_Name : Node_Id;
3743 Is_Box_Present := False;
3749 Assoc := First (From);
3750 while Present (Assoc) loop
3751 Selector_Name := First (Choices (Assoc));
3752 while Present (Selector_Name) loop
3753 if Nkind (Selector_Name) = N_Others_Choice then
3754 if Consider_Others_Choice and then No (Expr) then
3756 -- We need to duplicate the expression for each
3757 -- successive component covered by the others choice.
3758 -- This is redundant if the others_choice covers only
3759 -- one component (small optimization possible???), but
3760 -- indispensable otherwise, because each one must be
3761 -- expanded individually to preserve side effects.
3763 -- Ada 2005 (AI-287): In case of default initialization
3764 -- of components, we duplicate the corresponding default
3765 -- expression (from the record type declaration). The
3766 -- copy must carry the sloc of the association (not the
3767 -- original expression) to prevent spurious elaboration
3768 -- checks when the default includes function calls.
3770 if Box_Present (Assoc) then
3771 Others_Box := Others_Box + 1;
3772 Is_Box_Present := True;
3774 if Expander_Active then
3776 New_Copy_Tree_And_Copy_Dimensions
3777 (Expression (Parent (Compon)),
3778 New_Sloc => Sloc (Assoc));
3780 return Expression (Parent (Compon));
3784 if Present (Others_Etype)
3785 and then Base_Type (Others_Etype) /= Base_Type (Typ)
3787 -- If the components are of an anonymous access
3788 -- type they are distinct, but this is legal in
3789 -- Ada 2012 as long as designated types match.
3791 if (Ekind (Typ) = E_Anonymous_Access_Type
3792 or else Ekind (Typ) =
3793 E_Anonymous_Access_Subprogram_Type)
3794 and then Designated_Type (Typ) =
3795 Designated_Type (Others_Etype)
3800 ("components in OTHERS choice must have same "
3801 & "type", Selector_Name);
3805 Others_Etype := Typ;
3807 -- Copy the expression so that it is resolved
3808 -- independently for each component, This is needed
3809 -- for accessibility checks on compoents of anonymous
3810 -- access types, even in compile_only mode.
3812 if not Inside_A_Generic then
3814 -- In ASIS mode, preanalyze the expression in an
3815 -- others association before making copies for
3816 -- separate resolution and accessibility checks.
3817 -- This ensures that the type of the expression is
3818 -- available to ASIS in all cases, in particular if
3819 -- the expression is itself an aggregate.
3822 Preanalyze_And_Resolve (Expression (Assoc), Typ);
3826 New_Copy_Tree_And_Copy_Dimensions
3827 (Expression (Assoc));
3830 return Expression (Assoc);
3835 elsif Chars (Compon) = Chars (Selector_Name) then
3838 -- Ada 2005 (AI-231)
3840 if Ada_Version >= Ada_2005
3841 and then Known_Null (Expression (Assoc))
3843 Check_Can_Never_Be_Null (Compon, Expression (Assoc));
3846 -- We need to duplicate the expression when several
3847 -- components are grouped together with a "|" choice.
3848 -- For instance "filed1 | filed2 => Expr"
3850 -- Ada 2005 (AI-287)
3852 if Box_Present (Assoc) then
3853 Is_Box_Present := True;
3855 -- Duplicate the default expression of the component
3856 -- from the record type declaration, so a new copy
3857 -- can be attached to the association.
3859 -- Note that we always copy the default expression,
3860 -- even when the association has a single choice, in
3861 -- order to create a proper association for the
3862 -- expanded aggregate.
3864 -- Component may have no default, in which case the
3865 -- expression is empty and the component is default-
3866 -- initialized, but an association for the component
3867 -- exists, and it is not covered by an others clause.
3869 -- Scalar and private types have no initialization
3870 -- procedure, so they remain uninitialized. If the
3871 -- target of the aggregate is a constant this
3872 -- deserves a warning.
3874 if No (Expression (Parent (Compon)))
3875 and then not Has_Non_Null_Base_Init_Proc (Typ)
3876 and then not Has_Aspect (Typ, Aspect_Default_Value)
3877 and then not Is_Concurrent_Type (Typ)
3878 and then Nkind (Parent (N)) = N_Object_Declaration
3879 and then Constant_Present (Parent (N))
3881 Error_Msg_Node_2 := Typ;
3883 ("component&? of type& is uninitialized",
3884 Assoc, Selector_Name);
3886 -- An additional reminder if the component type
3887 -- is a generic formal.
3889 if Is_Generic_Type (Base_Type (Typ)) then
3891 ("\instance should provide actual type with "
3892 & "initialization for&", Assoc, Typ);
3897 New_Copy_Tree_And_Copy_Dimensions
3898 (Expression (Parent (Compon)));
3901 if Present (Next (Selector_Name)) then
3902 Expr := New_Copy_Tree_And_Copy_Dimensions
3903 (Expression (Assoc));
3905 Expr := Expression (Assoc);
3909 Generate_Reference (Compon, Selector_Name, 'm');
3913 ("more than one value supplied for &",
3914 Selector_Name, Compon);
3919 Next (Selector_Name);
3928 -----------------------------
3929 -- Propagate_Discriminants --
3930 -----------------------------
3932 procedure Propagate_Discriminants
3934 Assoc_List : List_Id)
3936 Loc : constant Source_Ptr := Sloc (N);
3938 Needs_Box : Boolean := False;
3940 procedure Process_Component (Comp : Entity_Id);
3941 -- Add one component with a box association to the inner aggregate,
3942 -- and recurse if component is itself composite.
3944 -----------------------
3945 -- Process_Component --
3946 -----------------------
3948 procedure Process_Component (Comp : Entity_Id) is
3949 T : constant Entity_Id := Etype (Comp);
3953 if Is_Record_Type (T) and then Has_Discriminants (T) then
3954 New_Aggr := Make_Aggregate (Loc, New_List, New_List);
3955 Set_Etype (New_Aggr, T);
3958 (Comp, New_Aggr, Component_Associations (Aggr));
3960 -- Collect discriminant values and recurse
3962 Add_Discriminant_Values (New_Aggr, Assoc_List);
3963 Propagate_Discriminants (New_Aggr, Assoc_List);
3968 end Process_Component;
3972 Aggr_Type : constant Entity_Id := Base_Type (Etype (Aggr));
3973 Components : constant Elist_Id := New_Elmt_List;
3974 Def_Node : constant Node_Id :=
3975 Type_Definition (Declaration_Node (Aggr_Type));
3978 Comp_Elmt : Elmt_Id;
3981 -- Start of processing for Propagate_Discriminants
3984 -- The component type may be a variant type. Collect the components
3985 -- that are ruled by the known values of the discriminants. Their
3986 -- values have already been inserted into the component list of the
3987 -- current aggregate.
3989 if Nkind (Def_Node) = N_Record_Definition
3990 and then Present (Component_List (Def_Node))
3991 and then Present (Variant_Part (Component_List (Def_Node)))
3993 Gather_Components (Aggr_Type,
3994 Component_List (Def_Node),
3995 Governed_By => Component_Associations (Aggr),
3997 Report_Errors => Errors);
3999 Comp_Elmt := First_Elmt (Components);
4000 while Present (Comp_Elmt) loop
4001 if Ekind (Node (Comp_Elmt)) /= E_Discriminant then
4002 Process_Component (Node (Comp_Elmt));
4005 Next_Elmt (Comp_Elmt);
4008 -- No variant part, iterate over all components
4011 Comp := First_Component (Etype (Aggr));
4012 while Present (Comp) loop
4013 Process_Component (Comp);
4014 Next_Component (Comp);
4019 Append_To (Component_Associations (Aggr),
4020 Make_Component_Association (Loc,
4021 Choices => New_List (Make_Others_Choice (Loc)),
4022 Expression => Empty,
4023 Box_Present => True));
4025 end Propagate_Discriminants;
4027 -----------------------
4028 -- Resolve_Aggr_Expr --
4029 -----------------------
4031 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Entity_Id) is
4032 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean;
4033 -- If the expression is an aggregate (possibly qualified) then its
4034 -- expansion is delayed until the enclosing aggregate is expanded
4035 -- into assignments. In that case, do not generate checks on the
4036 -- expression, because they will be generated later, and will other-
4037 -- wise force a copy (to remove side effects) that would leave a
4038 -- dynamic-sized aggregate in the code, something that gigi cannot
4041 ---------------------------
4042 -- Has_Expansion_Delayed --
4043 ---------------------------
4045 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean is
4048 (Nkind_In (Expr, N_Aggregate, N_Extension_Aggregate)
4049 and then Present (Etype (Expr))
4050 and then Is_Record_Type (Etype (Expr))
4051 and then Expansion_Delayed (Expr))
4053 (Nkind (Expr) = N_Qualified_Expression
4054 and then Has_Expansion_Delayed (Expression (Expr)));
4055 end Has_Expansion_Delayed;
4059 Expr_Type : Entity_Id := Empty;
4060 New_C : Entity_Id := Component;
4064 -- Set to True if the resolved Expr node needs to be relocated when
4065 -- attached to the newly created association list. This node need not
4066 -- be relocated if its parent pointer is not set. In fact in this
4067 -- case Expr is the output of a New_Copy_Tree call. If Relocate is
4068 -- True then we have analyzed the expression node in the original
4069 -- aggregate and hence it needs to be relocated when moved over to
4070 -- the new association list.
4072 -- Start of processing for Resolve_Aggr_Expr
4075 -- If the type of the component is elementary or the type of the
4076 -- aggregate does not contain discriminants, use the type of the
4077 -- component to resolve Expr.
4079 if Is_Elementary_Type (Etype (Component))
4080 or else not Has_Discriminants (Etype (N))
4082 Expr_Type := Etype (Component);
4084 -- Otherwise we have to pick up the new type of the component from
4085 -- the new constrained subtype of the aggregate. In fact components
4086 -- which are of a composite type might be constrained by a
4087 -- discriminant, and we want to resolve Expr against the subtype were
4088 -- all discriminant occurrences are replaced with their actual value.
4091 New_C := First_Component (Etype (N));
4092 while Present (New_C) loop
4093 if Chars (New_C) = Chars (Component) then
4094 Expr_Type := Etype (New_C);
4098 Next_Component (New_C);
4101 pragma Assert (Present (Expr_Type));
4103 -- For each range in an array type where a discriminant has been
4104 -- replaced with the constraint, check that this range is within
4105 -- the range of the base type. This checks is done in the init
4106 -- proc for regular objects, but has to be done here for
4107 -- aggregates since no init proc is called for them.
4109 if Is_Array_Type (Expr_Type) then
4112 -- Range of the current constrained index in the array
4114 Orig_Index : Node_Id := First_Index (Etype (Component));
4115 -- Range corresponding to the range Index above in the
4116 -- original unconstrained record type. The bounds of this
4117 -- range may be governed by discriminants.
4119 Unconstr_Index : Node_Id := First_Index (Etype (Expr_Type));
4120 -- Range corresponding to the range Index above for the
4121 -- unconstrained array type. This range is needed to apply
4125 Index := First_Index (Expr_Type);
4126 while Present (Index) loop
4127 if Depends_On_Discriminant (Orig_Index) then
4128 Apply_Range_Check (Index, Etype (Unconstr_Index));
4132 Next_Index (Orig_Index);
4133 Next_Index (Unconstr_Index);
4139 -- If the Parent pointer of Expr is not set, Expr is an expression
4140 -- duplicated by New_Tree_Copy (this happens for record aggregates
4141 -- that look like (Field1 | Filed2 => Expr) or (others => Expr)).
4142 -- Such a duplicated expression must be attached to the tree
4143 -- before analysis and resolution to enforce the rule that a tree
4144 -- fragment should never be analyzed or resolved unless it is
4145 -- attached to the current compilation unit.
4147 if No (Parent (Expr)) then
4148 Set_Parent (Expr, N);
4154 Analyze_And_Resolve (Expr, Expr_Type);
4155 Check_Expr_OK_In_Limited_Aggregate (Expr);
4156 Check_Non_Static_Context (Expr);
4157 Check_Unset_Reference (Expr);
4159 -- Check wrong use of class-wide types
4161 if Is_Class_Wide_Type (Etype (Expr)) then
4162 Error_Msg_N ("dynamically tagged expression not allowed", Expr);
4165 if not Has_Expansion_Delayed (Expr) then
4166 Aggregate_Constraint_Checks (Expr, Expr_Type);
4169 -- If an aggregate component has a type with predicates, an explicit
4170 -- predicate check must be applied, as for an assignment statement,
4171 -- because the aggegate might not be expanded into individual
4172 -- component assignments.
4174 if Present (Predicate_Function (Expr_Type))
4175 and then Analyzed (Expr)
4177 Apply_Predicate_Check (Expr, Expr_Type);
4180 if Raises_Constraint_Error (Expr) then
4181 Set_Raises_Constraint_Error (N);
4184 -- If the expression has been marked as requiring a range check, then
4185 -- generate it here. It's a bit odd to be generating such checks in
4186 -- the analyzer, but harmless since Generate_Range_Check does nothing
4187 -- (other than making sure Do_Range_Check is set) if the expander is
4190 if Do_Range_Check (Expr) then
4191 Generate_Range_Check (Expr, Expr_Type, CE_Range_Check_Failed);
4194 -- Add association Component => Expr if the caller requests it
4197 New_Expr := Relocate_Node (Expr);
4199 -- Since New_Expr is not gonna be analyzed later on, we need to
4200 -- propagate here the dimensions form Expr to New_Expr.
4202 Copy_Dimensions (Expr, New_Expr);
4208 Add_Association (New_C, New_Expr, New_Assoc_List);
4209 end Resolve_Aggr_Expr;
4215 procedure Rewrite_Range (Root_Type : Entity_Id; Rge : Node_Id) is
4216 procedure Rewrite_Bound
4219 Expr_Disc : Node_Id);
4220 -- Rewrite a bound of the range Bound, when it is equal to the
4221 -- non-stored discriminant Disc, into the stored discriminant
4228 procedure Rewrite_Bound
4231 Expr_Disc : Node_Id)
4234 if Nkind (Bound) = N_Identifier
4235 and then Entity (Bound) = Disc
4237 Rewrite (Bound, New_Copy_Tree (Expr_Disc));
4243 Low, High : Node_Id;
4245 Expr_Disc : Elmt_Id;
4247 -- Start of processing for Rewrite_Range
4250 if Has_Discriminants (Root_Type)
4251 and then Nkind (Rge) = N_Range
4253 Low := Low_Bound (Rge);
4254 High := High_Bound (Rge);
4256 Disc := First_Discriminant (Root_Type);
4257 Expr_Disc := First_Elmt (Stored_Constraint (Etype (N)));
4258 while Present (Disc) loop
4259 Rewrite_Bound (Low, Disc, Node (Expr_Disc));
4260 Rewrite_Bound (High, Disc, Node (Expr_Disc));
4261 Next_Discriminant (Disc);
4262 Next_Elmt (Expr_Disc);
4269 Components : constant Elist_Id := New_Elmt_List;
4270 -- Components is the list of the record components whose value must be
4271 -- provided in the aggregate. This list does include discriminants.
4273 Component : Entity_Id;
4274 Component_Elmt : Elmt_Id;
4276 Positional_Expr : Node_Id;
4278 -- Start of processing for Resolve_Record_Aggregate
4281 -- A record aggregate is restricted in SPARK:
4283 -- Each named association can have only a single choice.
4284 -- OTHERS cannot be used.
4285 -- Positional and named associations cannot be mixed.
4287 if Present (Component_Associations (N))
4288 and then Present (First (Component_Associations (N)))
4290 if Present (Expressions (N)) then
4291 Check_SPARK_05_Restriction
4292 ("named association cannot follow positional one",
4293 First (Choices (First (Component_Associations (N)))));
4300 Assoc := First (Component_Associations (N));
4301 while Present (Assoc) loop
4302 if Nkind (Assoc) = N_Iterated_Component_Association then
4304 ("iterated component association can only appear in an "
4305 & "array aggregate", N);
4306 raise Unrecoverable_Error;
4309 if List_Length (Choices (Assoc)) > 1 then
4310 Check_SPARK_05_Restriction
4311 ("component association in record aggregate must "
4312 & "contain a single choice", Assoc);
4315 if Nkind (First (Choices (Assoc))) = N_Others_Choice then
4316 Check_SPARK_05_Restriction
4317 ("record aggregate cannot contain OTHERS", Assoc);
4321 Assoc := Next (Assoc);
4326 -- We may end up calling Duplicate_Subexpr on expressions that are
4327 -- attached to New_Assoc_List. For this reason we need to attach it
4328 -- to the tree by setting its parent pointer to N. This parent point
4329 -- will change in STEP 8 below.
4331 Set_Parent (New_Assoc_List, N);
4333 -- STEP 1: abstract type and null record verification
4335 if Is_Abstract_Type (Typ) then
4336 Error_Msg_N ("type of aggregate cannot be abstract", N);
4339 if No (First_Entity (Typ)) and then Null_Record_Present (N) then
4343 elsif Present (First_Entity (Typ))
4344 and then Null_Record_Present (N)
4345 and then not Is_Tagged_Type (Typ)
4347 Error_Msg_N ("record aggregate cannot be null", N);
4350 -- If the type has no components, then the aggregate should either
4351 -- have "null record", or in Ada 2005 it could instead have a single
4352 -- component association given by "others => <>". For Ada 95 we flag an
4353 -- error at this point, but for Ada 2005 we proceed with checking the
4354 -- associations below, which will catch the case where it's not an
4355 -- aggregate with "others => <>". Note that the legality of a <>
4356 -- aggregate for a null record type was established by AI05-016.
4358 elsif No (First_Entity (Typ))
4359 and then Ada_Version < Ada_2005
4361 Error_Msg_N ("record aggregate must be null", N);
4365 -- STEP 2: Verify aggregate structure
4369 Bad_Aggregate : Boolean := False;
4370 Selector_Name : Node_Id;
4373 if Present (Component_Associations (N)) then
4374 Assoc := First (Component_Associations (N));
4379 while Present (Assoc) loop
4380 Selector_Name := First (Choices (Assoc));
4381 while Present (Selector_Name) loop
4382 if Nkind (Selector_Name) = N_Identifier then
4385 elsif Nkind (Selector_Name) = N_Others_Choice then
4386 if Selector_Name /= First (Choices (Assoc))
4387 or else Present (Next (Selector_Name))
4390 ("OTHERS must appear alone in a choice list",
4394 elsif Present (Next (Assoc)) then
4396 ("OTHERS must appear last in an aggregate",
4400 -- (Ada 2005): If this is an association with a box,
4401 -- indicate that the association need not represent
4404 elsif Box_Present (Assoc) then
4411 ("selector name should be identifier or OTHERS",
4413 Bad_Aggregate := True;
4416 Next (Selector_Name);
4422 if Bad_Aggregate then
4427 -- STEP 3: Find discriminant Values
4430 Discrim : Entity_Id;
4431 Missing_Discriminants : Boolean := False;
4434 if Present (Expressions (N)) then
4435 Positional_Expr := First (Expressions (N));
4437 Positional_Expr := Empty;
4440 -- AI05-0115: if the ancestor part is a subtype mark, the ancestor
4441 -- must not have unknown discriminants.
4443 if Is_Derived_Type (Typ)
4444 and then Has_Unknown_Discriminants (Root_Type (Typ))
4445 and then Nkind (N) /= N_Extension_Aggregate
4448 ("aggregate not available for type& whose ancestor "
4449 & "has unknown discriminants ", N, Typ);
4452 if Has_Unknown_Discriminants (Typ)
4453 and then Present (Underlying_Record_View (Typ))
4455 Discrim := First_Discriminant (Underlying_Record_View (Typ));
4456 elsif Has_Discriminants (Typ) then
4457 Discrim := First_Discriminant (Typ);
4462 -- First find the discriminant values in the positional components
4464 while Present (Discrim) and then Present (Positional_Expr) loop
4465 if Discriminant_Present (Discrim) then
4466 Resolve_Aggr_Expr (Positional_Expr, Discrim);
4468 -- Ada 2005 (AI-231)
4470 if Ada_Version >= Ada_2005
4471 and then Known_Null (Positional_Expr)
4473 Check_Can_Never_Be_Null (Discrim, Positional_Expr);
4476 Next (Positional_Expr);
4479 if Present (Get_Value (Discrim, Component_Associations (N))) then
4481 ("more than one value supplied for discriminant&",
4485 Next_Discriminant (Discrim);
4488 -- Find remaining discriminant values if any among named components
4490 while Present (Discrim) loop
4491 Expr := Get_Value (Discrim, Component_Associations (N), True);
4493 if not Discriminant_Present (Discrim) then
4494 if Present (Expr) then
4496 ("more than one value supplied for discriminant &",
4500 elsif No (Expr) then
4502 ("no value supplied for discriminant &", N, Discrim);
4503 Missing_Discriminants := True;
4506 Resolve_Aggr_Expr (Expr, Discrim);
4509 Next_Discriminant (Discrim);
4512 if Missing_Discriminants then
4516 -- At this point and until the beginning of STEP 6, New_Assoc_List
4517 -- contains only the discriminants and their values.
4521 -- STEP 4: Set the Etype of the record aggregate
4523 -- ??? This code is pretty much a copy of Sem_Ch3.Build_Subtype. That
4524 -- routine should really be exported in sem_util or some such and used
4525 -- in sem_ch3 and here rather than have a copy of the code which is a
4526 -- maintenance nightmare.
4528 -- ??? Performance WARNING. The current implementation creates a new
4529 -- itype for all aggregates whose base type is discriminated. This means
4530 -- that for record aggregates nested inside an array aggregate we will
4531 -- create a new itype for each record aggregate if the array component
4532 -- type has discriminants. For large aggregates this may be a problem.
4533 -- What should be done in this case is to reuse itypes as much as
4536 if Has_Discriminants (Typ)
4537 or else (Has_Unknown_Discriminants (Typ)
4538 and then Present (Underlying_Record_View (Typ)))
4540 Build_Constrained_Itype : declare
4541 Constrs : constant List_Id := New_List;
4542 Loc : constant Source_Ptr := Sloc (N);
4545 New_Assoc : Node_Id;
4546 Subtyp_Decl : Node_Id;
4549 New_Assoc := First (New_Assoc_List);
4550 while Present (New_Assoc) loop
4551 Append_To (Constrs, Duplicate_Subexpr (Expression (New_Assoc)));
4555 if Has_Unknown_Discriminants (Typ)
4556 and then Present (Underlying_Record_View (Typ))
4559 Make_Subtype_Indication (Loc,
4561 New_Occurrence_Of (Underlying_Record_View (Typ), Loc),
4563 Make_Index_Or_Discriminant_Constraint (Loc,
4564 Constraints => Constrs));
4567 Make_Subtype_Indication (Loc,
4569 New_Occurrence_Of (Base_Type (Typ), Loc),
4571 Make_Index_Or_Discriminant_Constraint (Loc,
4572 Constraints => Constrs));
4575 Def_Id := Create_Itype (Ekind (Typ), N);
4578 Make_Subtype_Declaration (Loc,
4579 Defining_Identifier => Def_Id,
4580 Subtype_Indication => Indic);
4581 Set_Parent (Subtyp_Decl, Parent (N));
4583 -- Itypes must be analyzed with checks off (see itypes.ads)
4585 Analyze (Subtyp_Decl, Suppress => All_Checks);
4587 Set_Etype (N, Def_Id);
4588 Check_Static_Discriminated_Subtype
4589 (Def_Id, Expression (First (New_Assoc_List)));
4590 end Build_Constrained_Itype;
4596 -- STEP 5: Get remaining components according to discriminant values
4600 Errors_Found : Boolean := False;
4601 Record_Def : Node_Id;
4602 Parent_Typ : Entity_Id;
4603 Parent_Typ_List : Elist_Id;
4604 Parent_Elmt : Elmt_Id;
4605 Root_Typ : Entity_Id;
4608 if Is_Derived_Type (Typ) and then Is_Tagged_Type (Typ) then
4609 Parent_Typ_List := New_Elmt_List;
4611 -- If this is an extension aggregate, the component list must
4612 -- include all components that are not in the given ancestor type.
4613 -- Otherwise, the component list must include components of all
4614 -- ancestors, starting with the root.
4616 if Nkind (N) = N_Extension_Aggregate then
4617 Root_Typ := Base_Type (Etype (Ancestor_Part (N)));
4620 -- AI05-0115: check legality of aggregate for type with a
4621 -- private ancestor.
4623 Root_Typ := Root_Type (Typ);
4624 if Has_Private_Ancestor (Typ) then
4626 Ancestor : constant Entity_Id :=
4627 Find_Private_Ancestor (Typ);
4628 Ancestor_Unit : constant Entity_Id :=
4630 (Get_Source_Unit (Ancestor));
4631 Parent_Unit : constant Entity_Id :=
4632 Cunit_Entity (Get_Source_Unit
4633 (Base_Type (Etype (Ancestor))));
4635 -- Check whether we are in a scope that has full view
4636 -- over the private ancestor and its parent. This can
4637 -- only happen if the derivation takes place in a child
4638 -- unit of the unit that declares the parent, and we are
4639 -- in the private part or body of that child unit, else
4640 -- the aggregate is illegal.
4642 if Is_Child_Unit (Ancestor_Unit)
4643 and then Scope (Ancestor_Unit) = Parent_Unit
4644 and then In_Open_Scopes (Scope (Ancestor))
4646 (In_Private_Part (Scope (Ancestor))
4647 or else In_Package_Body (Scope (Ancestor)))
4653 ("type of aggregate has private ancestor&!",
4655 Error_Msg_N ("must use extension aggregate!", N);
4661 Dnode := Declaration_Node (Base_Type (Root_Typ));
4663 -- If we don't get a full declaration, then we have some error
4664 -- which will get signalled later so skip this part. Otherwise
4665 -- gather components of root that apply to the aggregate type.
4666 -- We use the base type in case there is an applicable stored
4667 -- constraint that renames the discriminants of the root.
4669 if Nkind (Dnode) = N_Full_Type_Declaration then
4670 Record_Def := Type_Definition (Dnode);
4673 Component_List (Record_Def),
4674 Governed_By => New_Assoc_List,
4676 Report_Errors => Errors_Found);
4678 if Errors_Found then
4680 ("discriminant controlling variant part is not static",
4687 Parent_Typ := Base_Type (Typ);
4688 while Parent_Typ /= Root_Typ loop
4689 Prepend_Elmt (Parent_Typ, To => Parent_Typ_List);
4690 Parent_Typ := Etype (Parent_Typ);
4692 if Nkind (Parent (Base_Type (Parent_Typ))) =
4693 N_Private_Type_Declaration
4694 or else Nkind (Parent (Base_Type (Parent_Typ))) =
4695 N_Private_Extension_Declaration
4697 if Nkind (N) /= N_Extension_Aggregate then
4699 ("type of aggregate has private ancestor&!",
4701 Error_Msg_N ("must use extension aggregate!", N);
4704 elsif Parent_Typ /= Root_Typ then
4706 ("ancestor part of aggregate must be private type&",
4707 Ancestor_Part (N), Parent_Typ);
4711 -- The current view of ancestor part may be a private type,
4712 -- while the context type is always non-private.
4714 elsif Is_Private_Type (Root_Typ)
4715 and then Present (Full_View (Root_Typ))
4716 and then Nkind (N) = N_Extension_Aggregate
4718 exit when Base_Type (Full_View (Root_Typ)) = Parent_Typ;
4722 -- Now collect components from all other ancestors, beginning
4723 -- with the current type. If the type has unknown discriminants
4724 -- use the component list of the Underlying_Record_View, which
4725 -- needs to be used for the subsequent expansion of the aggregate
4726 -- into assignments.
4728 Parent_Elmt := First_Elmt (Parent_Typ_List);
4729 while Present (Parent_Elmt) loop
4730 Parent_Typ := Node (Parent_Elmt);
4732 if Has_Unknown_Discriminants (Parent_Typ)
4733 and then Present (Underlying_Record_View (Typ))
4735 Parent_Typ := Underlying_Record_View (Parent_Typ);
4738 Record_Def := Type_Definition (Parent (Base_Type (Parent_Typ)));
4739 Gather_Components (Empty,
4740 Component_List (Record_Extension_Part (Record_Def)),
4741 Governed_By => New_Assoc_List,
4743 Report_Errors => Errors_Found);
4745 Next_Elmt (Parent_Elmt);
4748 -- Typ is not a derived tagged type
4751 Record_Def := Type_Definition (Parent (Base_Type (Typ)));
4753 if Null_Present (Record_Def) then
4756 elsif not Has_Unknown_Discriminants (Typ) then
4759 Component_List (Record_Def),
4760 Governed_By => New_Assoc_List,
4762 Report_Errors => Errors_Found);
4766 (Base_Type (Underlying_Record_View (Typ)),
4767 Component_List (Record_Def),
4768 Governed_By => New_Assoc_List,
4770 Report_Errors => Errors_Found);
4774 if Errors_Found then
4779 -- STEP 6: Find component Values
4782 Component_Elmt := First_Elmt (Components);
4784 -- First scan the remaining positional associations in the aggregate.
4785 -- Remember that at this point Positional_Expr contains the current
4786 -- positional association if any is left after looking for discriminant
4787 -- values in step 3.
4789 while Present (Positional_Expr) and then Present (Component_Elmt) loop
4790 Component := Node (Component_Elmt);
4791 Resolve_Aggr_Expr (Positional_Expr, Component);
4793 -- Ada 2005 (AI-231)
4795 if Ada_Version >= Ada_2005 and then Known_Null (Positional_Expr) then
4796 Check_Can_Never_Be_Null (Component, Positional_Expr);
4799 if Present (Get_Value (Component, Component_Associations (N))) then
4801 ("more than one value supplied for Component &", N, Component);
4804 Next (Positional_Expr);
4805 Next_Elmt (Component_Elmt);
4808 if Present (Positional_Expr) then
4810 ("too many components for record aggregate", Positional_Expr);
4813 -- Now scan for the named arguments of the aggregate
4815 while Present (Component_Elmt) loop
4816 Component := Node (Component_Elmt);
4817 Expr := Get_Value (Component, Component_Associations (N), True);
4819 -- Note: The previous call to Get_Value sets the value of the
4820 -- variable Is_Box_Present.
4822 -- Ada 2005 (AI-287): Handle components with default initialization.
4823 -- Note: This feature was originally added to Ada 2005 for limited
4824 -- but it was finally allowed with any type.
4826 if Is_Box_Present then
4827 Check_Box_Component : declare
4828 Ctyp : constant Entity_Id := Etype (Component);
4831 -- If there is a default expression for the aggregate, copy
4832 -- it into a new association. This copy must modify the scopes
4833 -- of internal types that may be attached to the expression
4834 -- (e.g. index subtypes of arrays) because in general the type
4835 -- declaration and the aggregate appear in different scopes,
4836 -- and the backend requires the scope of the type to match the
4837 -- point at which it is elaborated.
4839 -- If the component has an initialization procedure (IP) we
4840 -- pass the component to the expander, which will generate
4841 -- the call to such IP.
4843 -- If the component has discriminants, their values must
4844 -- be taken from their subtype. This is indispensable for
4845 -- constraints that are given by the current instance of an
4846 -- enclosing type, to allow the expansion of the aggregate to
4847 -- replace the reference to the current instance by the target
4848 -- object of the aggregate.
4850 if Present (Parent (Component))
4851 and then Nkind (Parent (Component)) = N_Component_Declaration
4852 and then Present (Expression (Parent (Component)))
4855 New_Copy_Tree_And_Copy_Dimensions
4856 (Expression (Parent (Component)),
4857 New_Scope => Current_Scope,
4858 New_Sloc => Sloc (N));
4860 -- As the type of the copied default expression may refer
4861 -- to discriminants of the record type declaration, these
4862 -- non-stored discriminants need to be rewritten into stored
4863 -- discriminant values for the aggregate. This is required
4864 -- in GNATprove mode, and is adopted in all modes to avoid
4865 -- special-casing GNATprove mode.
4867 if Is_Array_Type (Etype (Expr)) then
4869 Rec_Typ : constant Entity_Id := Scope (Component);
4870 -- Root record type whose discriminants may be used as
4871 -- bounds in range nodes.
4876 -- Rewrite the range nodes occurring in the indexes
4879 Index := First_Index (Etype (Expr));
4880 while Present (Index) loop
4881 Rewrite_Range (Rec_Typ, Index);
4883 (Rec_Typ, Scalar_Range (Etype (Index)));
4888 -- Rewrite the range nodes occurring as aggregate
4891 if Nkind (Expr) = N_Aggregate
4892 and then Present (Aggregate_Bounds (Expr))
4894 Rewrite_Range (Rec_Typ, Aggregate_Bounds (Expr));
4900 (Component => Component,
4902 Assoc_List => New_Assoc_List);
4903 Set_Has_Self_Reference (N);
4905 -- A box-defaulted access component gets the value null. Also
4906 -- included are components of private types whose underlying
4907 -- type is an access type. In either case set the type of the
4908 -- literal, for subsequent use in semantic checks.
4910 elsif Present (Underlying_Type (Ctyp))
4911 and then Is_Access_Type (Underlying_Type (Ctyp))
4913 -- If the component's type is private with an access type as
4914 -- its underlying type then we have to create an unchecked
4915 -- conversion to satisfy type checking.
4917 if Is_Private_Type (Ctyp) then
4919 Qual_Null : constant Node_Id :=
4920 Make_Qualified_Expression (Sloc (N),
4923 (Underlying_Type (Ctyp), Sloc (N)),
4924 Expression => Make_Null (Sloc (N)));
4926 Convert_Null : constant Node_Id :=
4927 Unchecked_Convert_To
4931 Analyze_And_Resolve (Convert_Null, Ctyp);
4933 (Component => Component,
4934 Expr => Convert_Null,
4935 Assoc_List => New_Assoc_List);
4938 -- Otherwise the component type is non-private
4941 Expr := Make_Null (Sloc (N));
4942 Set_Etype (Expr, Ctyp);
4945 (Component => Component,
4947 Assoc_List => New_Assoc_List);
4950 -- Ada 2012: If component is scalar with default value, use it
4952 elsif Is_Scalar_Type (Ctyp)
4953 and then Has_Default_Aspect (Ctyp)
4956 (Component => Component,
4958 Default_Aspect_Value
4959 (First_Subtype (Underlying_Type (Ctyp))),
4960 Assoc_List => New_Assoc_List);
4962 elsif Has_Non_Null_Base_Init_Proc (Ctyp)
4963 or else not Expander_Active
4965 if Is_Record_Type (Ctyp)
4966 and then Has_Discriminants (Ctyp)
4967 and then not Is_Private_Type (Ctyp)
4969 -- We build a partially initialized aggregate with the
4970 -- values of the discriminants and box initialization
4971 -- for the rest, if other components are present.
4973 -- The type of the aggregate is the known subtype of
4974 -- the component. The capture of discriminants must be
4975 -- recursive because subcomponents may be constrained
4976 -- (transitively) by discriminants of enclosing types.
4977 -- For a private type with discriminants, a call to the
4978 -- initialization procedure will be generated, and no
4979 -- subaggregate is needed.
4981 Capture_Discriminants : declare
4982 Loc : constant Source_Ptr := Sloc (N);
4986 Expr := Make_Aggregate (Loc, New_List, New_List);
4987 Set_Etype (Expr, Ctyp);
4989 -- If the enclosing type has discriminants, they have
4990 -- been collected in the aggregate earlier, and they
4991 -- may appear as constraints of subcomponents.
4993 -- Similarly if this component has discriminants, they
4994 -- might in turn be propagated to their components.
4996 if Has_Discriminants (Typ) then
4997 Add_Discriminant_Values (Expr, New_Assoc_List);
4998 Propagate_Discriminants (Expr, New_Assoc_List);
5000 elsif Has_Discriminants (Ctyp) then
5001 Add_Discriminant_Values
5002 (Expr, Component_Associations (Expr));
5003 Propagate_Discriminants
5004 (Expr, Component_Associations (Expr));
5011 -- If the type has additional components, create
5012 -- an OTHERS box association for them.
5014 Comp := First_Component (Ctyp);
5015 while Present (Comp) loop
5016 if Ekind (Comp) = E_Component then
5017 if not Is_Record_Type (Etype (Comp)) then
5019 (Component_Associations (Expr),
5020 Make_Component_Association (Loc,
5023 Make_Others_Choice (Loc)),
5024 Expression => Empty,
5025 Box_Present => True));
5031 Next_Component (Comp);
5037 (Component => Component,
5039 Assoc_List => New_Assoc_List);
5040 end Capture_Discriminants;
5042 -- Otherwise the component type is not a record, or it has
5043 -- not discriminants, or it is private.
5047 (Component => Component,
5049 Assoc_List => New_Assoc_List,
5050 Is_Box_Present => True);
5053 -- Otherwise we only need to resolve the expression if the
5054 -- component has partially initialized values (required to
5055 -- expand the corresponding assignments and run-time checks).
5057 elsif Present (Expr)
5058 and then Is_Partially_Initialized_Type (Ctyp)
5060 Resolve_Aggr_Expr (Expr, Component);
5062 end Check_Box_Component;
5064 elsif No (Expr) then
5066 -- Ignore hidden components associated with the position of the
5067 -- interface tags: these are initialized dynamically.
5069 if not Present (Related_Type (Component)) then
5071 ("no value supplied for component &!", N, Component);
5075 Resolve_Aggr_Expr (Expr, Component);
5078 Next_Elmt (Component_Elmt);
5081 -- STEP 7: check for invalid components + check type in choice list
5085 New_Assoc : Node_Id;
5091 -- Type of first component in choice list
5094 if Present (Component_Associations (N)) then
5095 Assoc := First (Component_Associations (N));
5100 Verification : while Present (Assoc) loop
5101 Selectr := First (Choices (Assoc));
5104 if Nkind (Selectr) = N_Others_Choice then
5106 -- Ada 2005 (AI-287): others choice may have expression or box
5108 if No (Others_Etype) and then Others_Box = 0 then
5110 ("OTHERS must represent at least one component", Selectr);
5112 elsif Others_Box = 1 and then Warn_On_Redundant_Constructs then
5113 Error_Msg_N ("others choice is redundant?", Box_Node);
5115 ("\previous choices cover all components?", Box_Node);
5121 while Present (Selectr) loop
5122 New_Assoc := First (New_Assoc_List);
5123 while Present (New_Assoc) loop
5124 Component := First (Choices (New_Assoc));
5126 if Chars (Selectr) = Chars (Component) then
5128 Check_Identifier (Selectr, Entity (Component));
5137 -- If no association, this is not a legal component of the type
5138 -- in question, unless its association is provided with a box.
5140 if No (New_Assoc) then
5141 if Box_Present (Parent (Selectr)) then
5143 -- This may still be a bogus component with a box. Scan
5144 -- list of components to verify that a component with
5145 -- that name exists.
5151 C := First_Component (Typ);
5152 while Present (C) loop
5153 if Chars (C) = Chars (Selectr) then
5155 -- If the context is an extension aggregate,
5156 -- the component must not be inherited from
5157 -- the ancestor part of the aggregate.
5159 if Nkind (N) /= N_Extension_Aggregate
5161 Scope (Original_Record_Component (C)) /=
5162 Etype (Ancestor_Part (N))
5172 Error_Msg_Node_2 := Typ;
5173 Error_Msg_N ("& is not a component of}", Selectr);
5177 elsif Chars (Selectr) /= Name_uTag
5178 and then Chars (Selectr) /= Name_uParent
5180 if not Has_Discriminants (Typ) then
5181 Error_Msg_Node_2 := Typ;
5182 Error_Msg_N ("& is not a component of}", Selectr);
5185 ("& is not a component of the aggregate subtype",
5189 Check_Misspelled_Component (Components, Selectr);
5192 elsif No (Typech) then
5193 Typech := Base_Type (Etype (Component));
5195 -- AI05-0199: In Ada 2012, several components of anonymous
5196 -- access types can appear in a choice list, as long as the
5197 -- designated types match.
5199 elsif Typech /= Base_Type (Etype (Component)) then
5200 if Ada_Version >= Ada_2012
5201 and then Ekind (Typech) = E_Anonymous_Access_Type
5203 Ekind (Etype (Component)) = E_Anonymous_Access_Type
5204 and then Base_Type (Designated_Type (Typech)) =
5205 Base_Type (Designated_Type (Etype (Component)))
5207 Subtypes_Statically_Match (Typech, (Etype (Component)))
5211 elsif not Box_Present (Parent (Selectr)) then
5213 ("components in choice list must have same type",
5222 end loop Verification;
5225 -- STEP 8: replace the original aggregate
5228 New_Aggregate : constant Node_Id := New_Copy (N);
5231 Set_Expressions (New_Aggregate, No_List);
5232 Set_Etype (New_Aggregate, Etype (N));
5233 Set_Component_Associations (New_Aggregate, New_Assoc_List);
5234 Set_Check_Actuals (New_Aggregate, Check_Actuals (N));
5236 Rewrite (N, New_Aggregate);
5239 -- Check the dimensions of the components in the record aggregate
5241 Analyze_Dimension_Extension_Or_Record_Aggregate (N);
5242 end Resolve_Record_Aggregate;
5244 -----------------------------
5245 -- Check_Can_Never_Be_Null --
5246 -----------------------------
5248 procedure Check_Can_Never_Be_Null (Typ : Entity_Id; Expr : Node_Id) is
5249 Comp_Typ : Entity_Id;
5253 (Ada_Version >= Ada_2005
5254 and then Present (Expr)
5255 and then Known_Null (Expr));
5258 when E_Array_Type =>
5259 Comp_Typ := Component_Type (Typ);
5264 Comp_Typ := Etype (Typ);
5270 if Can_Never_Be_Null (Comp_Typ) then
5272 -- Here we know we have a constraint error. Note that we do not use
5273 -- Apply_Compile_Time_Constraint_Error here to the Expr, which might
5274 -- seem the more natural approach. That's because in some cases the
5275 -- components are rewritten, and the replacement would be missed.
5276 -- We do not mark the whole aggregate as raising a constraint error,
5277 -- because the association may be a null array range.
5280 ("(Ada 2005) null not allowed in null-excluding component??", Expr);
5282 ("\Constraint_Error will be raised at run time??", Expr);
5285 Make_Raise_Constraint_Error
5286 (Sloc (Expr), Reason => CE_Access_Check_Failed));
5287 Set_Etype (Expr, Comp_Typ);
5288 Set_Analyzed (Expr);
5290 end Check_Can_Never_Be_Null;
5292 ---------------------
5293 -- Sort_Case_Table --
5294 ---------------------
5296 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is
5297 U : constant Int := Case_Table'Last;
5305 T := Case_Table (K + 1);
5309 and then Expr_Value (Case_Table (J - 1).Lo) > Expr_Value (T.Lo)
5311 Case_Table (J) := Case_Table (J - 1);
5315 Case_Table (J) := T;
5318 end Sort_Case_Table;