1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2011, 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 Atree; use Atree;
27 with Checks; use Checks;
28 with Einfo; use Einfo;
29 with Elists; use Elists;
30 with Errout; use Errout;
31 with Expander; use Expander;
32 with Exp_Tss; use Exp_Tss;
33 with Exp_Util; use Exp_Util;
34 with Freeze; use Freeze;
35 with Itypes; use Itypes;
37 with Lib.Xref; use Lib.Xref;
38 with Namet; use Namet;
39 with Namet.Sp; use Namet.Sp;
40 with Nmake; use Nmake;
41 with Nlists; use Nlists;
43 with Restrict; use Restrict;
45 with Sem_Aux; use Sem_Aux;
46 with Sem_Cat; use Sem_Cat;
47 with Sem_Ch3; use Sem_Ch3;
48 with Sem_Ch8; use Sem_Ch8;
49 with Sem_Ch13; use Sem_Ch13;
50 with Sem_Eval; use Sem_Eval;
51 with Sem_Res; use Sem_Res;
52 with Sem_Util; use Sem_Util;
53 with Sem_Type; use Sem_Type;
54 with Sem_Warn; use Sem_Warn;
55 with Sinfo; use Sinfo;
56 with Snames; use Snames;
57 with Stringt; use Stringt;
58 with Stand; use Stand;
59 with Style; use Style;
60 with Targparm; use Targparm;
61 with Tbuild; use Tbuild;
62 with Uintp; use Uintp;
64 package body Sem_Aggr is
66 type Case_Bounds is record
69 Choice_Node : Node_Id;
72 type Case_Table_Type is array (Nat range <>) of Case_Bounds;
73 -- Table type used by Check_Case_Choices procedure
75 -----------------------
76 -- Local Subprograms --
77 -----------------------
79 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type);
80 -- Sort the Case Table using the Lower Bound of each Choice as the key.
81 -- A simple insertion sort is used since the number of choices in a case
82 -- statement of variant part will usually be small and probably in near
85 procedure Check_Can_Never_Be_Null (Typ : Entity_Id; Expr : Node_Id);
86 -- Ada 2005 (AI-231): Check bad usage of null for a component for which
87 -- null exclusion (NOT NULL) is specified. Typ can be an E_Array_Type for
88 -- the array case (the component type of the array will be used) or an
89 -- E_Component/E_Discriminant entity in the record case, in which case the
90 -- type of the component will be used for the test. If Typ is any other
91 -- kind of entity, the call is ignored. Expr is the component node in the
92 -- aggregate which is known to have a null value. A warning message will be
93 -- issued if the component is null excluding.
95 -- It would be better to pass the proper type for Typ ???
97 procedure Check_Expr_OK_In_Limited_Aggregate (Expr : Node_Id);
98 -- Check that Expr is either not limited or else is one of the cases of
99 -- expressions allowed for a limited component association (namely, an
100 -- aggregate, function call, or <> notation). Report error for violations.
102 procedure Check_Qualified_Aggregate (Level : Nat; Expr : Node_Id);
103 -- Given aggregate Expr, check that sub-aggregates of Expr that are nested
104 -- at Level are qualified. If Level = 0, this applies to Expr directly.
105 -- Only issue errors in formal verification mode.
107 function Is_Top_Level_Aggregate (Expr : Node_Id) return Boolean;
108 -- Return True of Expr is an aggregate not contained directly in another
111 ------------------------------------------------------
112 -- Subprograms used for RECORD AGGREGATE Processing --
113 ------------------------------------------------------
115 procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id);
116 -- This procedure performs all the semantic checks required for record
117 -- aggregates. Note that for aggregates analysis and resolution go
118 -- hand in hand. Aggregate analysis has been delayed up to here and
119 -- it is done while resolving the aggregate.
121 -- N is the N_Aggregate node.
122 -- Typ is the record type for the aggregate resolution
124 -- While performing the semantic checks, this procedure builds a new
125 -- Component_Association_List where each record field appears alone in a
126 -- Component_Choice_List along with its corresponding expression. The
127 -- record fields in the Component_Association_List appear in the same order
128 -- in which they appear in the record type Typ.
130 -- Once this new Component_Association_List is built and all the semantic
131 -- checks performed, the original aggregate subtree is replaced with the
132 -- new named record aggregate just built. Note that subtree substitution is
133 -- performed with Rewrite so as to be able to retrieve the original
136 -- The aggregate subtree manipulation performed by Resolve_Record_Aggregate
137 -- yields the aggregate format expected by Gigi. Typically, this kind of
138 -- tree manipulations are done in the expander. However, because the
139 -- semantic checks that need to be performed on record aggregates really go
140 -- hand in hand with the record aggregate normalization, the aggregate
141 -- subtree transformation is performed during resolution rather than
142 -- expansion. Had we decided otherwise we would have had to duplicate most
143 -- of the code in the expansion procedure Expand_Record_Aggregate. Note,
144 -- however, that all the expansion concerning aggregates for tagged records
145 -- is done in Expand_Record_Aggregate.
147 -- The algorithm of Resolve_Record_Aggregate proceeds as follows:
149 -- 1. Make sure that the record type against which the record aggregate
150 -- has to be resolved is not abstract. Furthermore if the type is a
151 -- null aggregate make sure the input aggregate N is also null.
153 -- 2. Verify that the structure of the aggregate is that of a record
154 -- aggregate. Specifically, look for component associations and ensure
155 -- that each choice list only has identifiers or the N_Others_Choice
156 -- node. Also make sure that if present, the N_Others_Choice occurs
157 -- last and by itself.
159 -- 3. If Typ contains discriminants, the values for each discriminant is
160 -- looked for. If the record type Typ has variants, we check that the
161 -- expressions corresponding to each discriminant ruling the (possibly
162 -- nested) variant parts of Typ, are static. This allows us to determine
163 -- the variant parts to which the rest of the aggregate must conform.
164 -- The names of discriminants with their values are saved in a new
165 -- association list, New_Assoc_List which is later augmented with the
166 -- names and values of the remaining components in the record type.
168 -- During this phase we also make sure that every discriminant is
169 -- assigned exactly one value. Note that when several values for a given
170 -- discriminant are found, semantic processing continues looking for
171 -- further errors. In this case it's the first discriminant value found
172 -- which we will be recorded.
174 -- IMPORTANT NOTE: For derived tagged types this procedure expects
175 -- First_Discriminant and Next_Discriminant to give the correct list
176 -- of discriminants, in the correct order.
178 -- 4. After all the discriminant values have been gathered, we can set the
179 -- Etype of the record aggregate. If Typ contains no discriminants this
180 -- is straightforward: the Etype of N is just Typ, otherwise a new
181 -- implicit constrained subtype of Typ is built to be the Etype of N.
183 -- 5. Gather the remaining record components according to the discriminant
184 -- values. This involves recursively traversing the record type
185 -- structure to see what variants are selected by the given discriminant
186 -- values. This processing is a little more convoluted if Typ is a
187 -- derived tagged types since we need to retrieve the record structure
188 -- of all the ancestors of Typ.
190 -- 6. After gathering the record components we look for their values in the
191 -- record aggregate and emit appropriate error messages should we not
192 -- find such values or should they be duplicated.
194 -- 7. We then make sure no illegal component names appear in the record
195 -- aggregate and make sure that the type of the record components
196 -- appearing in a same choice list is the same. Finally we ensure that
197 -- the others choice, if present, is used to provide the value of at
198 -- least a record component.
200 -- 8. The original aggregate node is replaced with the new named aggregate
201 -- built in steps 3 through 6, as explained earlier.
203 -- Given the complexity of record aggregate resolution, the primary goal of
204 -- this routine is clarity and simplicity rather than execution and storage
205 -- efficiency. If there are only positional components in the aggregate the
206 -- running time is linear. If there are associations the running time is
207 -- still linear as long as the order of the associations is not too far off
208 -- the order of the components in the record type. If this is not the case
209 -- the running time is at worst quadratic in the size of the association
212 procedure Check_Misspelled_Component
213 (Elements : Elist_Id;
214 Component : Node_Id);
215 -- Give possible misspelling diagnostic if Component is likely to be a
216 -- misspelling of one of the components of the Assoc_List. This is called
217 -- by Resolve_Aggr_Expr after producing an invalid component error message.
219 procedure Check_Static_Discriminated_Subtype (T : Entity_Id; V : Node_Id);
220 -- An optimization: determine whether a discriminated subtype has a static
221 -- constraint, and contains array components whose length is also static,
222 -- either because they are constrained by the discriminant, or because the
223 -- original component bounds are static.
225 -----------------------------------------------------
226 -- Subprograms used for ARRAY AGGREGATE Processing --
227 -----------------------------------------------------
229 function Resolve_Array_Aggregate
232 Index_Constr : Node_Id;
233 Component_Typ : Entity_Id;
234 Others_Allowed : Boolean) return Boolean;
235 -- This procedure performs the semantic checks for an array aggregate.
236 -- True is returned if the aggregate resolution succeeds.
238 -- The procedure works by recursively checking each nested aggregate.
239 -- Specifically, after checking a sub-aggregate nested at the i-th level
240 -- we recursively check all the subaggregates at the i+1-st level (if any).
241 -- Note that for aggregates analysis and resolution go hand in hand.
242 -- Aggregate analysis has been delayed up to here and it is done while
243 -- resolving the aggregate.
245 -- N is the current N_Aggregate node to be checked.
247 -- Index is the index node corresponding to the array sub-aggregate that
248 -- we are currently checking (RM 4.3.3 (8)). Its Etype is the
249 -- corresponding index type (or subtype).
251 -- Index_Constr is the node giving the applicable index constraint if
252 -- any (RM 4.3.3 (10)). It "is a constraint provided by certain
253 -- contexts [...] that can be used to determine the bounds of the array
254 -- value specified by the aggregate". If Others_Allowed below is False
255 -- there is no applicable index constraint and this node is set to Index.
257 -- Component_Typ is the array component type.
259 -- Others_Allowed indicates whether an others choice is allowed
260 -- in the context where the top-level aggregate appeared.
262 -- The algorithm of Resolve_Array_Aggregate proceeds as follows:
264 -- 1. Make sure that the others choice, if present, is by itself and
265 -- appears last in the sub-aggregate. Check that we do not have
266 -- positional and named components in the array sub-aggregate (unless
267 -- the named association is an others choice). Finally if an others
268 -- choice is present, make sure it is allowed in the aggregate context.
270 -- 2. If the array sub-aggregate contains discrete_choices:
272 -- (A) Verify their validity. Specifically verify that:
274 -- (a) If a null range is present it must be the only possible
275 -- choice in the array aggregate.
277 -- (b) Ditto for a non static range.
279 -- (c) Ditto for a non static expression.
281 -- In addition this step analyzes and resolves each discrete_choice,
282 -- making sure that its type is the type of the corresponding Index.
283 -- If we are not at the lowest array aggregate level (in the case of
284 -- multi-dimensional aggregates) then invoke Resolve_Array_Aggregate
285 -- recursively on each component expression. Otherwise, resolve the
286 -- bottom level component expressions against the expected component
287 -- type ONLY IF the component corresponds to a single discrete choice
288 -- which is not an others choice (to see why read the DELAYED
289 -- COMPONENT RESOLUTION below).
291 -- (B) Determine the bounds of the sub-aggregate and lowest and
292 -- highest choice values.
294 -- 3. For positional aggregates:
296 -- (A) Loop over the component expressions either recursively invoking
297 -- Resolve_Array_Aggregate on each of these for multi-dimensional
298 -- array aggregates or resolving the bottom level component
299 -- expressions against the expected component type.
301 -- (B) Determine the bounds of the positional sub-aggregates.
303 -- 4. Try to determine statically whether the evaluation of the array
304 -- sub-aggregate raises Constraint_Error. If yes emit proper
305 -- warnings. The precise checks are the following:
307 -- (A) Check that the index range defined by aggregate bounds is
308 -- compatible with corresponding index subtype.
309 -- We also check against the base type. In fact it could be that
310 -- Low/High bounds of the base type are static whereas those of
311 -- the index subtype are not. Thus if we can statically catch
312 -- a problem with respect to the base type we are guaranteed
313 -- that the same problem will arise with the index subtype
315 -- (B) If we are dealing with a named aggregate containing an others
316 -- choice and at least one discrete choice then make sure the range
317 -- specified by the discrete choices does not overflow the
318 -- aggregate bounds. We also check against the index type and base
319 -- type bounds for the same reasons given in (A).
321 -- (C) If we are dealing with a positional aggregate with an others
322 -- choice make sure the number of positional elements specified
323 -- does not overflow the aggregate bounds. We also check against
324 -- the index type and base type bounds as mentioned in (A).
326 -- Finally construct an N_Range node giving the sub-aggregate bounds.
327 -- Set the Aggregate_Bounds field of the sub-aggregate to be this
328 -- N_Range. The routine Array_Aggr_Subtype below uses such N_Ranges
329 -- to build the appropriate aggregate subtype. Aggregate_Bounds
330 -- information is needed during expansion.
332 -- DELAYED COMPONENT RESOLUTION: The resolution of bottom level component
333 -- expressions in an array aggregate may call Duplicate_Subexpr or some
334 -- other routine that inserts code just outside the outermost aggregate.
335 -- If the array aggregate contains discrete choices or an others choice,
336 -- this may be wrong. Consider for instance the following example.
338 -- type Rec is record
342 -- type Acc_Rec is access Rec;
343 -- Arr : array (1..3) of Acc_Rec := (1 .. 3 => new Rec);
345 -- Then the transformation of "new Rec" that occurs during resolution
346 -- entails the following code modifications
348 -- P7b : constant Acc_Rec := new Rec;
350 -- Arr : array (1..3) of Acc_Rec := (1 .. 3 => P7b);
352 -- This code transformation is clearly wrong, since we need to call
353 -- "new Rec" for each of the 3 array elements. To avoid this problem we
354 -- delay resolution of the components of non positional array aggregates
355 -- to the expansion phase. As an optimization, if the discrete choice
356 -- specifies a single value we do not delay resolution.
358 function Array_Aggr_Subtype (N : Node_Id; Typ : Node_Id) return Entity_Id;
359 -- This routine returns the type or subtype of an array aggregate.
361 -- N is the array aggregate node whose type we return.
363 -- Typ is the context type in which N occurs.
365 -- This routine creates an implicit array subtype whose bounds are
366 -- those defined by the aggregate. When this routine is invoked
367 -- Resolve_Array_Aggregate has already processed aggregate N. Thus the
368 -- Aggregate_Bounds of each sub-aggregate, is an N_Range node giving the
369 -- sub-aggregate bounds. When building the aggregate itype, this function
370 -- traverses the array aggregate N collecting such Aggregate_Bounds and
371 -- constructs the proper array aggregate itype.
373 -- Note that in the case of multidimensional aggregates each inner
374 -- sub-aggregate corresponding to a given array dimension, may provide a
375 -- different bounds. If it is possible to determine statically that
376 -- some sub-aggregates corresponding to the same index do not have the
377 -- same bounds, then a warning is emitted. If such check is not possible
378 -- statically (because some sub-aggregate bounds are dynamic expressions)
379 -- then this job is left to the expander. In all cases the particular
380 -- bounds that this function will chose for a given dimension is the first
381 -- N_Range node for a sub-aggregate corresponding to that dimension.
383 -- Note that the Raises_Constraint_Error flag of an array aggregate
384 -- whose evaluation is determined to raise CE by Resolve_Array_Aggregate,
385 -- is set in Resolve_Array_Aggregate but the aggregate is not
386 -- immediately replaced with a raise CE. In fact, Array_Aggr_Subtype must
387 -- first construct the proper itype for the aggregate (Gigi needs
388 -- this). After constructing the proper itype we will eventually replace
389 -- the top-level aggregate with a raise CE (done in Resolve_Aggregate).
390 -- Of course in cases such as:
392 -- type Arr is array (integer range <>) of Integer;
393 -- A : Arr := (positive range -1 .. 2 => 0);
395 -- The bounds of the aggregate itype are cooked up to look reasonable
396 -- (in this particular case the bounds will be 1 .. 2).
398 procedure Aggregate_Constraint_Checks
400 Check_Typ : Entity_Id);
401 -- Checks expression Exp against subtype Check_Typ. If Exp is an
402 -- aggregate and Check_Typ a constrained record type with discriminants,
403 -- we generate the appropriate discriminant checks. If Exp is an array
404 -- aggregate then emit the appropriate length checks. If Exp is a scalar
405 -- type, or a string literal, Exp is changed into Check_Typ'(Exp) to
406 -- ensure that range checks are performed at run time.
408 procedure Make_String_Into_Aggregate (N : Node_Id);
409 -- A string literal can appear in a context in which a one dimensional
410 -- array of characters is expected. This procedure simply rewrites the
411 -- string as an aggregate, prior to resolution.
413 ---------------------------------
414 -- Aggregate_Constraint_Checks --
415 ---------------------------------
417 procedure Aggregate_Constraint_Checks
419 Check_Typ : Entity_Id)
421 Exp_Typ : constant Entity_Id := Etype (Exp);
424 if Raises_Constraint_Error (Exp) then
428 -- Ada 2005 (AI-230): Generate a conversion to an anonymous access
429 -- component's type to force the appropriate accessibility checks.
431 -- Ada 2005 (AI-231): Generate conversion to the null-excluding
432 -- type to force the corresponding run-time check
434 if Is_Access_Type (Check_Typ)
435 and then ((Is_Local_Anonymous_Access (Check_Typ))
436 or else (Can_Never_Be_Null (Check_Typ)
437 and then not Can_Never_Be_Null (Exp_Typ)))
439 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
440 Analyze_And_Resolve (Exp, Check_Typ);
441 Check_Unset_Reference (Exp);
444 -- This is really expansion activity, so make sure that expansion
445 -- is on and is allowed.
447 if not Expander_Active or else In_Spec_Expression then
451 -- First check if we have to insert discriminant checks
453 if Has_Discriminants (Exp_Typ) then
454 Apply_Discriminant_Check (Exp, Check_Typ);
456 -- Next emit length checks for array aggregates
458 elsif Is_Array_Type (Exp_Typ) then
459 Apply_Length_Check (Exp, Check_Typ);
461 -- Finally emit scalar and string checks. If we are dealing with a
462 -- scalar literal we need to check by hand because the Etype of
463 -- literals is not necessarily correct.
465 elsif Is_Scalar_Type (Exp_Typ)
466 and then Compile_Time_Known_Value (Exp)
468 if Is_Out_Of_Range (Exp, Base_Type (Check_Typ)) then
469 Apply_Compile_Time_Constraint_Error
470 (Exp, "value not in range of}?", CE_Range_Check_Failed,
471 Ent => Base_Type (Check_Typ),
472 Typ => Base_Type (Check_Typ));
474 elsif Is_Out_Of_Range (Exp, Check_Typ) then
475 Apply_Compile_Time_Constraint_Error
476 (Exp, "value not in range of}?", CE_Range_Check_Failed,
480 elsif not Range_Checks_Suppressed (Check_Typ) then
481 Apply_Scalar_Range_Check (Exp, Check_Typ);
484 -- Verify that target type is also scalar, to prevent view anomalies
485 -- in instantiations.
487 elsif (Is_Scalar_Type (Exp_Typ)
488 or else Nkind (Exp) = N_String_Literal)
489 and then Is_Scalar_Type (Check_Typ)
490 and then Exp_Typ /= Check_Typ
492 if Is_Entity_Name (Exp)
493 and then Ekind (Entity (Exp)) = E_Constant
495 -- If expression is a constant, it is worthwhile checking whether
496 -- it is a bound of the type.
498 if (Is_Entity_Name (Type_Low_Bound (Check_Typ))
499 and then Entity (Exp) = Entity (Type_Low_Bound (Check_Typ)))
500 or else (Is_Entity_Name (Type_High_Bound (Check_Typ))
501 and then Entity (Exp) = Entity (Type_High_Bound (Check_Typ)))
506 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
507 Analyze_And_Resolve (Exp, Check_Typ);
508 Check_Unset_Reference (Exp);
511 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
512 Analyze_And_Resolve (Exp, Check_Typ);
513 Check_Unset_Reference (Exp);
517 end Aggregate_Constraint_Checks;
519 ------------------------
520 -- Array_Aggr_Subtype --
521 ------------------------
523 function Array_Aggr_Subtype
525 Typ : Entity_Id) return Entity_Id
527 Aggr_Dimension : constant Pos := Number_Dimensions (Typ);
528 -- Number of aggregate index dimensions
530 Aggr_Range : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
531 -- Constrained N_Range of each index dimension in our aggregate itype
533 Aggr_Low : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
534 Aggr_High : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
535 -- Low and High bounds for each index dimension in our aggregate itype
537 Is_Fully_Positional : Boolean := True;
539 procedure Collect_Aggr_Bounds (N : Node_Id; Dim : Pos);
540 -- N is an array (sub-)aggregate. Dim is the dimension corresponding
541 -- to (sub-)aggregate N. This procedure collects and removes the side
542 -- effects of the constrained N_Range nodes corresponding to each index
543 -- dimension of our aggregate itype. These N_Range nodes are collected
544 -- in Aggr_Range above.
546 -- Likewise collect in Aggr_Low & Aggr_High above the low and high
547 -- bounds of each index dimension. If, when collecting, two bounds
548 -- corresponding to the same dimension are static and found to differ,
549 -- then emit a warning, and mark N as raising Constraint_Error.
551 -------------------------
552 -- Collect_Aggr_Bounds --
553 -------------------------
555 procedure Collect_Aggr_Bounds (N : Node_Id; Dim : Pos) is
556 This_Range : constant Node_Id := Aggregate_Bounds (N);
557 -- The aggregate range node of this specific sub-aggregate
559 This_Low : constant Node_Id := Low_Bound (Aggregate_Bounds (N));
560 This_High : constant Node_Id := High_Bound (Aggregate_Bounds (N));
561 -- The aggregate bounds of this specific sub-aggregate
567 Remove_Side_Effects (This_Low, Variable_Ref => True);
568 Remove_Side_Effects (This_High, Variable_Ref => True);
570 -- Collect the first N_Range for a given dimension that you find.
571 -- For a given dimension they must be all equal anyway.
573 if No (Aggr_Range (Dim)) then
574 Aggr_Low (Dim) := This_Low;
575 Aggr_High (Dim) := This_High;
576 Aggr_Range (Dim) := This_Range;
579 if Compile_Time_Known_Value (This_Low) then
580 if not Compile_Time_Known_Value (Aggr_Low (Dim)) then
581 Aggr_Low (Dim) := This_Low;
583 elsif Expr_Value (This_Low) /= Expr_Value (Aggr_Low (Dim)) then
584 Set_Raises_Constraint_Error (N);
585 Error_Msg_N ("sub-aggregate low bound mismatch?", N);
587 ("\Constraint_Error will be raised at run time?", N);
591 if Compile_Time_Known_Value (This_High) then
592 if not Compile_Time_Known_Value (Aggr_High (Dim)) then
593 Aggr_High (Dim) := This_High;
596 Expr_Value (This_High) /= Expr_Value (Aggr_High (Dim))
598 Set_Raises_Constraint_Error (N);
599 Error_Msg_N ("sub-aggregate high bound mismatch?", N);
601 ("\Constraint_Error will be raised at run time?", N);
606 if Dim < Aggr_Dimension then
608 -- Process positional components
610 if Present (Expressions (N)) then
611 Expr := First (Expressions (N));
612 while Present (Expr) loop
613 Collect_Aggr_Bounds (Expr, Dim + 1);
618 -- Process component associations
620 if Present (Component_Associations (N)) then
621 Is_Fully_Positional := False;
623 Assoc := First (Component_Associations (N));
624 while Present (Assoc) loop
625 Expr := Expression (Assoc);
626 Collect_Aggr_Bounds (Expr, Dim + 1);
631 end Collect_Aggr_Bounds;
633 -- Array_Aggr_Subtype variables
636 -- The final itype of the overall aggregate
638 Index_Constraints : constant List_Id := New_List;
639 -- The list of index constraints of the aggregate itype
641 -- Start of processing for Array_Aggr_Subtype
644 -- Make sure that the list of index constraints is properly attached to
645 -- the tree, and then collect the aggregate bounds.
647 Set_Parent (Index_Constraints, N);
648 Collect_Aggr_Bounds (N, 1);
650 -- Build the list of constrained indexes of our aggregate itype
652 for J in 1 .. Aggr_Dimension loop
653 Create_Index : declare
654 Index_Base : constant Entity_Id :=
655 Base_Type (Etype (Aggr_Range (J)));
656 Index_Typ : Entity_Id;
659 -- Construct the Index subtype, and associate it with the range
660 -- construct that generates it.
663 Create_Itype (Subtype_Kind (Ekind (Index_Base)), Aggr_Range (J));
665 Set_Etype (Index_Typ, Index_Base);
667 if Is_Character_Type (Index_Base) then
668 Set_Is_Character_Type (Index_Typ);
671 Set_Size_Info (Index_Typ, (Index_Base));
672 Set_RM_Size (Index_Typ, RM_Size (Index_Base));
673 Set_First_Rep_Item (Index_Typ, First_Rep_Item (Index_Base));
674 Set_Scalar_Range (Index_Typ, Aggr_Range (J));
676 if Is_Discrete_Or_Fixed_Point_Type (Index_Typ) then
677 Set_RM_Size (Index_Typ, UI_From_Int (Minimum_Size (Index_Typ)));
680 Set_Etype (Aggr_Range (J), Index_Typ);
682 Append (Aggr_Range (J), To => Index_Constraints);
686 -- Now build the Itype
688 Itype := Create_Itype (E_Array_Subtype, N);
690 Set_First_Rep_Item (Itype, First_Rep_Item (Typ));
691 Set_Convention (Itype, Convention (Typ));
692 Set_Depends_On_Private (Itype, Has_Private_Component (Typ));
693 Set_Etype (Itype, Base_Type (Typ));
694 Set_Has_Alignment_Clause (Itype, Has_Alignment_Clause (Typ));
695 Set_Is_Aliased (Itype, Is_Aliased (Typ));
696 Set_Depends_On_Private (Itype, Depends_On_Private (Typ));
698 Copy_Suppress_Status (Index_Check, Typ, Itype);
699 Copy_Suppress_Status (Length_Check, Typ, Itype);
701 Set_First_Index (Itype, First (Index_Constraints));
702 Set_Is_Constrained (Itype, True);
703 Set_Is_Internal (Itype, True);
705 -- A simple optimization: purely positional aggregates of static
706 -- components should be passed to gigi unexpanded whenever possible, and
707 -- regardless of the staticness of the bounds themselves. Subsequent
708 -- checks in exp_aggr verify that type is not packed, etc.
710 Set_Size_Known_At_Compile_Time (Itype,
712 and then Comes_From_Source (N)
713 and then Size_Known_At_Compile_Time (Component_Type (Typ)));
715 -- We always need a freeze node for a packed array subtype, so that we
716 -- can build the Packed_Array_Type corresponding to the subtype. If
717 -- expansion is disabled, the packed array subtype is not built, and we
718 -- must not generate a freeze node for the type, or else it will appear
719 -- incomplete to gigi.
722 and then not In_Spec_Expression
723 and then Expander_Active
725 Freeze_Itype (Itype, N);
729 end Array_Aggr_Subtype;
731 --------------------------------
732 -- Check_Misspelled_Component --
733 --------------------------------
735 procedure Check_Misspelled_Component
736 (Elements : Elist_Id;
739 Max_Suggestions : constant := 2;
741 Nr_Of_Suggestions : Natural := 0;
742 Suggestion_1 : Entity_Id := Empty;
743 Suggestion_2 : Entity_Id := Empty;
744 Component_Elmt : Elmt_Id;
747 -- All the components of List are matched against Component and a count
748 -- is maintained of possible misspellings. When at the end of the
749 -- the analysis there are one or two (not more!) possible misspellings,
750 -- these misspellings will be suggested as possible correction.
752 Component_Elmt := First_Elmt (Elements);
753 while Nr_Of_Suggestions <= Max_Suggestions
754 and then Present (Component_Elmt)
756 if Is_Bad_Spelling_Of
757 (Chars (Node (Component_Elmt)),
760 Nr_Of_Suggestions := Nr_Of_Suggestions + 1;
762 case Nr_Of_Suggestions is
763 when 1 => Suggestion_1 := Node (Component_Elmt);
764 when 2 => Suggestion_2 := Node (Component_Elmt);
769 Next_Elmt (Component_Elmt);
772 -- Report at most two suggestions
774 if Nr_Of_Suggestions = 1 then
775 Error_Msg_NE -- CODEFIX
776 ("\possible misspelling of&", Component, Suggestion_1);
778 elsif Nr_Of_Suggestions = 2 then
779 Error_Msg_Node_2 := Suggestion_2;
780 Error_Msg_NE -- CODEFIX
781 ("\possible misspelling of& or&", Component, Suggestion_1);
783 end Check_Misspelled_Component;
785 ----------------------------------------
786 -- Check_Expr_OK_In_Limited_Aggregate --
787 ----------------------------------------
789 procedure Check_Expr_OK_In_Limited_Aggregate (Expr : Node_Id) is
791 if Is_Limited_Type (Etype (Expr))
792 and then Comes_From_Source (Expr)
793 and then not In_Instance_Body
795 if not OK_For_Limited_Init (Etype (Expr), Expr) then
796 Error_Msg_N ("initialization not allowed for limited types", Expr);
797 Explain_Limited_Type (Etype (Expr), Expr);
800 end Check_Expr_OK_In_Limited_Aggregate;
802 -------------------------------
803 -- Check_Qualified_Aggregate --
804 -------------------------------
806 procedure Check_Qualified_Aggregate (Level : Nat; Expr : Node_Id) is
812 if Nkind (Parent (Expr)) /= N_Qualified_Expression then
813 Check_SPARK_Restriction ("aggregate should be qualified", Expr);
817 Comp_Expr := First (Expressions (Expr));
818 while Present (Comp_Expr) loop
819 if Nkind (Comp_Expr) = N_Aggregate then
820 Check_Qualified_Aggregate (Level - 1, Comp_Expr);
823 Comp_Expr := Next (Comp_Expr);
826 Comp_Assn := First (Component_Associations (Expr));
827 while Present (Comp_Assn) loop
828 Comp_Expr := Expression (Comp_Assn);
830 if Nkind (Comp_Expr) = N_Aggregate then
831 Check_Qualified_Aggregate (Level - 1, Comp_Expr);
834 Comp_Assn := Next (Comp_Assn);
837 end Check_Qualified_Aggregate;
839 ----------------------------------------
840 -- Check_Static_Discriminated_Subtype --
841 ----------------------------------------
843 procedure Check_Static_Discriminated_Subtype (T : Entity_Id; V : Node_Id) is
844 Disc : constant Entity_Id := First_Discriminant (T);
849 if Has_Record_Rep_Clause (T) then
852 elsif Present (Next_Discriminant (Disc)) then
855 elsif Nkind (V) /= N_Integer_Literal then
859 Comp := First_Component (T);
860 while Present (Comp) loop
861 if Is_Scalar_Type (Etype (Comp)) then
864 elsif Is_Private_Type (Etype (Comp))
865 and then Present (Full_View (Etype (Comp)))
866 and then Is_Scalar_Type (Full_View (Etype (Comp)))
870 elsif Is_Array_Type (Etype (Comp)) then
871 if Is_Bit_Packed_Array (Etype (Comp)) then
875 Ind := First_Index (Etype (Comp));
876 while Present (Ind) loop
877 if Nkind (Ind) /= N_Range
878 or else Nkind (Low_Bound (Ind)) /= N_Integer_Literal
879 or else Nkind (High_Bound (Ind)) /= N_Integer_Literal
891 Next_Component (Comp);
894 -- On exit, all components have statically known sizes
896 Set_Size_Known_At_Compile_Time (T);
897 end Check_Static_Discriminated_Subtype;
899 -------------------------
900 -- Is_Others_Aggregate --
901 -------------------------
903 function Is_Others_Aggregate (Aggr : Node_Id) return Boolean is
905 return No (Expressions (Aggr))
907 Nkind (First (Choices (First (Component_Associations (Aggr)))))
909 end Is_Others_Aggregate;
911 ----------------------------
912 -- Is_Top_Level_Aggregate --
913 ----------------------------
915 function Is_Top_Level_Aggregate (Expr : Node_Id) return Boolean is
917 return Nkind (Parent (Expr)) /= N_Aggregate
918 and then (Nkind (Parent (Expr)) /= N_Component_Association
919 or else Nkind (Parent (Parent (Expr))) /= N_Aggregate);
920 end Is_Top_Level_Aggregate;
922 --------------------------------
923 -- Make_String_Into_Aggregate --
924 --------------------------------
926 procedure Make_String_Into_Aggregate (N : Node_Id) is
927 Exprs : constant List_Id := New_List;
928 Loc : constant Source_Ptr := Sloc (N);
929 Str : constant String_Id := Strval (N);
930 Strlen : constant Nat := String_Length (Str);
938 for J in 1 .. Strlen loop
939 C := Get_String_Char (Str, J);
940 Set_Character_Literal_Name (C);
943 Make_Character_Literal (P,
945 Char_Literal_Value => UI_From_CC (C));
946 Set_Etype (C_Node, Any_Character);
947 Append_To (Exprs, C_Node);
950 -- Something special for wide strings???
953 New_N := Make_Aggregate (Loc, Expressions => Exprs);
954 Set_Analyzed (New_N);
955 Set_Etype (New_N, Any_Composite);
958 end Make_String_Into_Aggregate;
960 -----------------------
961 -- Resolve_Aggregate --
962 -----------------------
964 procedure Resolve_Aggregate (N : Node_Id; Typ : Entity_Id) is
965 Loc : constant Source_Ptr := Sloc (N);
966 Pkind : constant Node_Kind := Nkind (Parent (N));
968 Aggr_Subtyp : Entity_Id;
969 -- The actual aggregate subtype. This is not necessarily the same as Typ
970 -- which is the subtype of the context in which the aggregate was found.
973 -- Ignore junk empty aggregate resulting from parser error
975 if No (Expressions (N))
976 and then No (Component_Associations (N))
977 and then not Null_Record_Present (N)
982 -- If the aggregate has box-initialized components, its type must be
983 -- frozen so that initialization procedures can properly be called
984 -- in the resolution that follows. The replacement of boxes with
985 -- initialization calls is properly an expansion activity but it must
986 -- be done during revolution.
989 and then Present (Component_Associations (N))
995 Comp := First (Component_Associations (N));
996 while Present (Comp) loop
997 if Box_Present (Comp) then
998 Insert_Actions (N, Freeze_Entity (Typ, N));
1007 -- An unqualified aggregate is restricted in SPARK to:
1009 -- An aggregate item inside an aggregate for a multi-dimensional array
1011 -- An expression being assigned to an unconstrained array, but only if
1012 -- the aggregate specifies a value for OTHERS only.
1014 if Nkind (Parent (N)) = N_Qualified_Expression then
1015 if Is_Array_Type (Typ) then
1016 Check_Qualified_Aggregate (Number_Dimensions (Typ), N);
1018 Check_Qualified_Aggregate (1, N);
1021 if Is_Array_Type (Typ)
1022 and then Nkind (Parent (N)) = N_Assignment_Statement
1023 and then not Is_Constrained (Etype (Name (Parent (N))))
1025 if not Is_Others_Aggregate (N) then
1026 Check_SPARK_Restriction
1027 ("array aggregate should have only OTHERS", N);
1030 elsif Is_Top_Level_Aggregate (N) then
1031 Check_SPARK_Restriction ("aggregate should be qualified", N);
1033 -- The legality of this unqualified aggregate is checked by calling
1034 -- Check_Qualified_Aggregate from one of its enclosing aggregate,
1035 -- unless one of these already causes an error to be issued.
1042 -- Check for aggregates not allowed in configurable run-time mode.
1043 -- We allow all cases of aggregates that do not come from source, since
1044 -- these are all assumed to be small (e.g. bounds of a string literal).
1045 -- We also allow aggregates of types we know to be small.
1047 if not Support_Aggregates_On_Target
1048 and then Comes_From_Source (N)
1049 and then (not Known_Static_Esize (Typ) or else Esize (Typ) > 64)
1051 Error_Msg_CRT ("aggregate", N);
1054 -- Ada 2005 (AI-287): Limited aggregates allowed
1056 if Is_Limited_Type (Typ) and then Ada_Version < Ada_2005 then
1057 Error_Msg_N ("aggregate type cannot be limited", N);
1058 Explain_Limited_Type (Typ, N);
1060 elsif Is_Class_Wide_Type (Typ) then
1061 Error_Msg_N ("type of aggregate cannot be class-wide", N);
1063 elsif Typ = Any_String
1064 or else Typ = Any_Composite
1066 Error_Msg_N ("no unique type for aggregate", N);
1067 Set_Etype (N, Any_Composite);
1069 elsif Is_Array_Type (Typ) and then Null_Record_Present (N) then
1070 Error_Msg_N ("null record forbidden in array aggregate", N);
1072 elsif Is_Record_Type (Typ) then
1073 Resolve_Record_Aggregate (N, Typ);
1075 elsif Is_Array_Type (Typ) then
1077 -- First a special test, for the case of a positional aggregate
1078 -- of characters which can be replaced by a string literal.
1080 -- Do not perform this transformation if this was a string literal to
1081 -- start with, whose components needed constraint checks, or if the
1082 -- component type is non-static, because it will require those checks
1083 -- and be transformed back into an aggregate.
1085 if Number_Dimensions (Typ) = 1
1086 and then Is_Standard_Character_Type (Component_Type (Typ))
1087 and then No (Component_Associations (N))
1088 and then not Is_Limited_Composite (Typ)
1089 and then not Is_Private_Composite (Typ)
1090 and then not Is_Bit_Packed_Array (Typ)
1091 and then Nkind (Original_Node (Parent (N))) /= N_String_Literal
1092 and then Is_Static_Subtype (Component_Type (Typ))
1098 Expr := First (Expressions (N));
1099 while Present (Expr) loop
1100 exit when Nkind (Expr) /= N_Character_Literal;
1107 Expr := First (Expressions (N));
1108 while Present (Expr) loop
1109 Store_String_Char (UI_To_CC (Char_Literal_Value (Expr)));
1113 Rewrite (N, Make_String_Literal (Loc, End_String));
1115 Analyze_And_Resolve (N, Typ);
1121 -- Here if we have a real aggregate to deal with
1123 Array_Aggregate : declare
1124 Aggr_Resolved : Boolean;
1126 Aggr_Typ : constant Entity_Id := Etype (Typ);
1127 -- This is the unconstrained array type, which is the type against
1128 -- which the aggregate is to be resolved. Typ itself is the array
1129 -- type of the context which may not be the same subtype as the
1130 -- subtype for the final aggregate.
1133 -- In the following we determine whether an OTHERS choice is
1134 -- allowed inside the array aggregate. The test checks the context
1135 -- in which the array aggregate occurs. If the context does not
1136 -- permit it, or the aggregate type is unconstrained, an OTHERS
1137 -- choice is not allowed (except that it is always allowed on the
1138 -- right-hand side of an assignment statement; in this case the
1139 -- constrainedness of the type doesn't matter).
1141 -- If expansion is disabled (generic context, or semantics-only
1142 -- mode) actual subtypes cannot be constructed, and the type of an
1143 -- object may be its unconstrained nominal type. However, if the
1144 -- context is an assignment, we assume that OTHERS is allowed,
1145 -- because the target of the assignment will have a constrained
1146 -- subtype when fully compiled.
1148 -- Note that there is no node for Explicit_Actual_Parameter.
1149 -- To test for this context we therefore have to test for node
1150 -- N_Parameter_Association which itself appears only if there is a
1151 -- formal parameter. Consequently we also need to test for
1152 -- N_Procedure_Call_Statement or N_Function_Call.
1154 Set_Etype (N, Aggr_Typ); -- May be overridden later on
1156 if Pkind = N_Assignment_Statement
1157 or else (Is_Constrained (Typ)
1159 (Pkind = N_Parameter_Association or else
1160 Pkind = N_Function_Call or else
1161 Pkind = N_Procedure_Call_Statement or else
1162 Pkind = N_Generic_Association or else
1163 Pkind = N_Formal_Object_Declaration or else
1164 Pkind = N_Simple_Return_Statement or else
1165 Pkind = N_Object_Declaration or else
1166 Pkind = N_Component_Declaration or else
1167 Pkind = N_Parameter_Specification or else
1168 Pkind = N_Qualified_Expression or else
1169 Pkind = N_Aggregate or else
1170 Pkind = N_Extension_Aggregate or else
1171 Pkind = N_Component_Association))
1174 Resolve_Array_Aggregate
1176 Index => First_Index (Aggr_Typ),
1177 Index_Constr => First_Index (Typ),
1178 Component_Typ => Component_Type (Typ),
1179 Others_Allowed => True);
1181 elsif not Expander_Active
1182 and then Pkind = N_Assignment_Statement
1185 Resolve_Array_Aggregate
1187 Index => First_Index (Aggr_Typ),
1188 Index_Constr => First_Index (Typ),
1189 Component_Typ => Component_Type (Typ),
1190 Others_Allowed => True);
1194 Resolve_Array_Aggregate
1196 Index => First_Index (Aggr_Typ),
1197 Index_Constr => First_Index (Aggr_Typ),
1198 Component_Typ => Component_Type (Typ),
1199 Others_Allowed => False);
1202 if not Aggr_Resolved then
1204 -- A parenthesized expression may have been intended as an
1205 -- aggregate, leading to a type error when analyzing the
1206 -- component. This can also happen for a nested component
1207 -- (see Analyze_Aggr_Expr).
1209 if Paren_Count (N) > 0 then
1211 ("positional aggregate cannot have one component", N);
1214 Aggr_Subtyp := Any_Composite;
1217 Aggr_Subtyp := Array_Aggr_Subtype (N, Typ);
1220 Set_Etype (N, Aggr_Subtyp);
1221 end Array_Aggregate;
1223 elsif Is_Private_Type (Typ)
1224 and then Present (Full_View (Typ))
1225 and then In_Inlined_Body
1226 and then Is_Composite_Type (Full_View (Typ))
1228 Resolve (N, Full_View (Typ));
1231 Error_Msg_N ("illegal context for aggregate", N);
1234 -- If we can determine statically that the evaluation of the aggregate
1235 -- raises Constraint_Error, then replace the aggregate with an
1236 -- N_Raise_Constraint_Error node, but set the Etype to the right
1237 -- aggregate subtype. Gigi needs this.
1239 if Raises_Constraint_Error (N) then
1240 Aggr_Subtyp := Etype (N);
1242 Make_Raise_Constraint_Error (Loc, Reason => CE_Range_Check_Failed));
1243 Set_Raises_Constraint_Error (N);
1244 Set_Etype (N, Aggr_Subtyp);
1247 end Resolve_Aggregate;
1249 -----------------------------
1250 -- Resolve_Array_Aggregate --
1251 -----------------------------
1253 function Resolve_Array_Aggregate
1256 Index_Constr : Node_Id;
1257 Component_Typ : Entity_Id;
1258 Others_Allowed : Boolean) return Boolean
1260 Loc : constant Source_Ptr := Sloc (N);
1262 Failure : constant Boolean := False;
1263 Success : constant Boolean := True;
1265 Index_Typ : constant Entity_Id := Etype (Index);
1266 Index_Typ_Low : constant Node_Id := Type_Low_Bound (Index_Typ);
1267 Index_Typ_High : constant Node_Id := Type_High_Bound (Index_Typ);
1268 -- The type of the index corresponding to the array sub-aggregate along
1269 -- with its low and upper bounds.
1271 Index_Base : constant Entity_Id := Base_Type (Index_Typ);
1272 Index_Base_Low : constant Node_Id := Type_Low_Bound (Index_Base);
1273 Index_Base_High : constant Node_Id := Type_High_Bound (Index_Base);
1274 -- Ditto for the base type
1276 function Add (Val : Uint; To : Node_Id) return Node_Id;
1277 -- Creates a new expression node where Val is added to expression To.
1278 -- Tries to constant fold whenever possible. To must be an already
1279 -- analyzed expression.
1281 procedure Check_Bound (BH : Node_Id; AH : in out Node_Id);
1282 -- Checks that AH (the upper bound of an array aggregate) is less than
1283 -- or equal to BH (the upper bound of the index base type). If the check
1284 -- fails, a warning is emitted, the Raises_Constraint_Error flag of N is
1285 -- set, and AH is replaced with a duplicate of BH.
1287 procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id);
1288 -- Checks that range AL .. AH is compatible with range L .. H. Emits a
1289 -- warning if not and sets the Raises_Constraint_Error flag in N.
1291 procedure Check_Length (L, H : Node_Id; Len : Uint);
1292 -- Checks that range L .. H contains at least Len elements. Emits a
1293 -- warning if not and sets the Raises_Constraint_Error flag in N.
1295 function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean;
1296 -- Returns True if range L .. H is dynamic or null
1298 procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean);
1299 -- Given expression node From, this routine sets OK to False if it
1300 -- cannot statically evaluate From. Otherwise it stores this static
1301 -- value into Value.
1303 function Resolve_Aggr_Expr
1305 Single_Elmt : Boolean) return Boolean;
1306 -- Resolves aggregate expression Expr. Returns False if resolution
1307 -- fails. If Single_Elmt is set to False, the expression Expr may be
1308 -- used to initialize several array aggregate elements (this can happen
1309 -- for discrete choices such as "L .. H => Expr" or the OTHERS choice).
1310 -- In this event we do not resolve Expr unless expansion is disabled.
1311 -- To know why, see the DELAYED COMPONENT RESOLUTION note above.
1313 -- NOTE: In the case of "... => <>", we pass the in the
1314 -- N_Component_Association node as Expr, since there is no Expression in
1315 -- that case, and we need a Sloc for the error message.
1321 function Add (Val : Uint; To : Node_Id) return Node_Id is
1327 if Raises_Constraint_Error (To) then
1331 -- First test if we can do constant folding
1333 if Compile_Time_Known_Value (To)
1334 or else Nkind (To) = N_Integer_Literal
1336 Expr_Pos := Make_Integer_Literal (Loc, Expr_Value (To) + Val);
1337 Set_Is_Static_Expression (Expr_Pos);
1338 Set_Etype (Expr_Pos, Etype (To));
1339 Set_Analyzed (Expr_Pos, Analyzed (To));
1341 if not Is_Enumeration_Type (Index_Typ) then
1344 -- If we are dealing with enumeration return
1345 -- Index_Typ'Val (Expr_Pos)
1349 Make_Attribute_Reference
1351 Prefix => New_Reference_To (Index_Typ, Loc),
1352 Attribute_Name => Name_Val,
1353 Expressions => New_List (Expr_Pos));
1359 -- If we are here no constant folding possible
1361 if not Is_Enumeration_Type (Index_Base) then
1364 Left_Opnd => Duplicate_Subexpr (To),
1365 Right_Opnd => Make_Integer_Literal (Loc, Val));
1367 -- If we are dealing with enumeration return
1368 -- Index_Typ'Val (Index_Typ'Pos (To) + Val)
1372 Make_Attribute_Reference
1374 Prefix => New_Reference_To (Index_Typ, Loc),
1375 Attribute_Name => Name_Pos,
1376 Expressions => New_List (Duplicate_Subexpr (To)));
1380 Left_Opnd => To_Pos,
1381 Right_Opnd => Make_Integer_Literal (Loc, Val));
1384 Make_Attribute_Reference
1386 Prefix => New_Reference_To (Index_Typ, Loc),
1387 Attribute_Name => Name_Val,
1388 Expressions => New_List (Expr_Pos));
1390 -- If the index type has a non standard representation, the
1391 -- attributes 'Val and 'Pos expand into function calls and the
1392 -- resulting expression is considered non-safe for reevaluation
1393 -- by the backend. Relocate it into a constant temporary in order
1394 -- to make it safe for reevaluation.
1396 if Has_Non_Standard_Rep (Etype (N)) then
1401 Def_Id := Make_Temporary (Loc, 'R', Expr);
1402 Set_Etype (Def_Id, Index_Typ);
1404 Make_Object_Declaration (Loc,
1405 Defining_Identifier => Def_Id,
1406 Object_Definition => New_Reference_To (Index_Typ, Loc),
1407 Constant_Present => True,
1408 Expression => Relocate_Node (Expr)));
1410 Expr := New_Reference_To (Def_Id, Loc);
1422 procedure Check_Bound (BH : Node_Id; AH : in out Node_Id) is
1430 Get (Value => Val_BH, From => BH, OK => OK_BH);
1431 Get (Value => Val_AH, From => AH, OK => OK_AH);
1433 if OK_BH and then OK_AH and then Val_BH < Val_AH then
1434 Set_Raises_Constraint_Error (N);
1435 Error_Msg_N ("upper bound out of range?", AH);
1436 Error_Msg_N ("\Constraint_Error will be raised at run time?", AH);
1438 -- You need to set AH to BH or else in the case of enumerations
1439 -- indexes we will not be able to resolve the aggregate bounds.
1441 AH := Duplicate_Subexpr (BH);
1449 procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id) is
1460 pragma Warnings (Off, OK_AL);
1461 pragma Warnings (Off, OK_AH);
1464 if Raises_Constraint_Error (N)
1465 or else Dynamic_Or_Null_Range (AL, AH)
1470 Get (Value => Val_L, From => L, OK => OK_L);
1471 Get (Value => Val_H, From => H, OK => OK_H);
1473 Get (Value => Val_AL, From => AL, OK => OK_AL);
1474 Get (Value => Val_AH, From => AH, OK => OK_AH);
1476 if OK_L and then Val_L > Val_AL then
1477 Set_Raises_Constraint_Error (N);
1478 Error_Msg_N ("lower bound of aggregate out of range?", N);
1479 Error_Msg_N ("\Constraint_Error will be raised at run time?", N);
1482 if OK_H and then Val_H < Val_AH then
1483 Set_Raises_Constraint_Error (N);
1484 Error_Msg_N ("upper bound of aggregate out of range?", N);
1485 Error_Msg_N ("\Constraint_Error will be raised at run time?", N);
1493 procedure Check_Length (L, H : Node_Id; Len : Uint) is
1503 if Raises_Constraint_Error (N) then
1507 Get (Value => Val_L, From => L, OK => OK_L);
1508 Get (Value => Val_H, From => H, OK => OK_H);
1510 if not OK_L or else not OK_H then
1514 -- If null range length is zero
1516 if Val_L > Val_H then
1517 Range_Len := Uint_0;
1519 Range_Len := Val_H - Val_L + 1;
1522 if Range_Len < Len then
1523 Set_Raises_Constraint_Error (N);
1524 Error_Msg_N ("too many elements?", N);
1525 Error_Msg_N ("\Constraint_Error will be raised at run time?", N);
1529 ---------------------------
1530 -- Dynamic_Or_Null_Range --
1531 ---------------------------
1533 function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean is
1541 Get (Value => Val_L, From => L, OK => OK_L);
1542 Get (Value => Val_H, From => H, OK => OK_H);
1544 return not OK_L or else not OK_H
1545 or else not Is_OK_Static_Expression (L)
1546 or else not Is_OK_Static_Expression (H)
1547 or else Val_L > Val_H;
1548 end Dynamic_Or_Null_Range;
1554 procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean) is
1558 if Compile_Time_Known_Value (From) then
1559 Value := Expr_Value (From);
1561 -- If expression From is something like Some_Type'Val (10) then
1564 elsif Nkind (From) = N_Attribute_Reference
1565 and then Attribute_Name (From) = Name_Val
1566 and then Compile_Time_Known_Value (First (Expressions (From)))
1568 Value := Expr_Value (First (Expressions (From)));
1576 -----------------------
1577 -- Resolve_Aggr_Expr --
1578 -----------------------
1580 function Resolve_Aggr_Expr
1582 Single_Elmt : Boolean) return Boolean
1584 Nxt_Ind : constant Node_Id := Next_Index (Index);
1585 Nxt_Ind_Constr : constant Node_Id := Next_Index (Index_Constr);
1586 -- Index is the current index corresponding to the expression
1588 Resolution_OK : Boolean := True;
1589 -- Set to False if resolution of the expression failed
1592 -- Defend against previous errors
1594 if Nkind (Expr) = N_Error
1595 or else Error_Posted (Expr)
1600 -- If the array type against which we are resolving the aggregate
1601 -- has several dimensions, the expressions nested inside the
1602 -- aggregate must be further aggregates (or strings).
1604 if Present (Nxt_Ind) then
1605 if Nkind (Expr) /= N_Aggregate then
1607 -- A string literal can appear where a one-dimensional array
1608 -- of characters is expected. If the literal looks like an
1609 -- operator, it is still an operator symbol, which will be
1610 -- transformed into a string when analyzed.
1612 if Is_Character_Type (Component_Typ)
1613 and then No (Next_Index (Nxt_Ind))
1614 and then Nkind_In (Expr, N_String_Literal, N_Operator_Symbol)
1616 -- A string literal used in a multidimensional array
1617 -- aggregate in place of the final one-dimensional
1618 -- aggregate must not be enclosed in parentheses.
1620 if Paren_Count (Expr) /= 0 then
1621 Error_Msg_N ("no parenthesis allowed here", Expr);
1624 Make_String_Into_Aggregate (Expr);
1627 Error_Msg_N ("nested array aggregate expected", Expr);
1629 -- If the expression is parenthesized, this may be
1630 -- a missing component association for a 1-aggregate.
1632 if Paren_Count (Expr) > 0 then
1634 ("\if single-component aggregate is intended,"
1635 & " write e.g. (1 ='> ...)", Expr);
1642 -- If it's "... => <>", nothing to resolve
1644 if Nkind (Expr) = N_Component_Association then
1645 pragma Assert (Box_Present (Expr));
1649 -- Ada 2005 (AI-231): Propagate the type to the nested aggregate.
1650 -- Required to check the null-exclusion attribute (if present).
1651 -- This value may be overridden later on.
1653 Set_Etype (Expr, Etype (N));
1655 Resolution_OK := Resolve_Array_Aggregate
1656 (Expr, Nxt_Ind, Nxt_Ind_Constr, Component_Typ, Others_Allowed);
1660 -- If it's "... => <>", nothing to resolve
1662 if Nkind (Expr) = N_Component_Association then
1663 pragma Assert (Box_Present (Expr));
1667 -- Do not resolve the expressions of discrete or others choices
1668 -- unless the expression covers a single component, or the
1669 -- expander is inactive.
1672 or else not Expander_Active
1673 or else In_Spec_Expression
1675 Analyze_And_Resolve (Expr, Component_Typ);
1676 Check_Expr_OK_In_Limited_Aggregate (Expr);
1677 Check_Non_Static_Context (Expr);
1678 Aggregate_Constraint_Checks (Expr, Component_Typ);
1679 Check_Unset_Reference (Expr);
1683 if Raises_Constraint_Error (Expr)
1684 and then Nkind (Parent (Expr)) /= N_Component_Association
1686 Set_Raises_Constraint_Error (N);
1689 -- If the expression has been marked as requiring a range check,
1690 -- then generate it here.
1692 if Do_Range_Check (Expr) then
1693 Set_Do_Range_Check (Expr, False);
1694 Generate_Range_Check (Expr, Component_Typ, CE_Range_Check_Failed);
1697 return Resolution_OK;
1698 end Resolve_Aggr_Expr;
1700 -- Variables local to Resolve_Array_Aggregate
1707 pragma Warnings (Off, Discard);
1709 Aggr_Low : Node_Id := Empty;
1710 Aggr_High : Node_Id := Empty;
1711 -- The actual low and high bounds of this sub-aggregate
1713 Choices_Low : Node_Id := Empty;
1714 Choices_High : Node_Id := Empty;
1715 -- The lowest and highest discrete choices values for a named aggregate
1717 Nb_Elements : Uint := Uint_0;
1718 -- The number of elements in a positional aggregate
1720 Others_Present : Boolean := False;
1722 Nb_Choices : Nat := 0;
1723 -- Contains the overall number of named choices in this sub-aggregate
1725 Nb_Discrete_Choices : Nat := 0;
1726 -- The overall number of discrete choices (not counting others choice)
1728 Case_Table_Size : Nat;
1729 -- Contains the size of the case table needed to sort aggregate choices
1731 -- Start of processing for Resolve_Array_Aggregate
1734 -- Ignore junk empty aggregate resulting from parser error
1736 if No (Expressions (N))
1737 and then No (Component_Associations (N))
1738 and then not Null_Record_Present (N)
1743 -- STEP 1: make sure the aggregate is correctly formatted
1745 if Present (Component_Associations (N)) then
1746 Assoc := First (Component_Associations (N));
1747 while Present (Assoc) loop
1748 Choice := First (Choices (Assoc));
1749 while Present (Choice) loop
1750 if Nkind (Choice) = N_Others_Choice then
1751 Others_Present := True;
1753 if Choice /= First (Choices (Assoc))
1754 or else Present (Next (Choice))
1757 ("OTHERS must appear alone in a choice list", Choice);
1761 if Present (Next (Assoc)) then
1763 ("OTHERS must appear last in an aggregate", Choice);
1767 if Ada_Version = Ada_83
1768 and then Assoc /= First (Component_Associations (N))
1769 and then Nkind_In (Parent (N), N_Assignment_Statement,
1770 N_Object_Declaration)
1773 ("(Ada 83) illegal context for OTHERS choice", N);
1777 Nb_Choices := Nb_Choices + 1;
1785 -- At this point we know that the others choice, if present, is by
1786 -- itself and appears last in the aggregate. Check if we have mixed
1787 -- positional and discrete associations (other than the others choice).
1789 if Present (Expressions (N))
1790 and then (Nb_Choices > 1
1791 or else (Nb_Choices = 1 and then not Others_Present))
1794 ("named association cannot follow positional association",
1795 First (Choices (First (Component_Associations (N)))));
1799 -- Test for the validity of an others choice if present
1801 if Others_Present and then not Others_Allowed then
1803 ("OTHERS choice not allowed here",
1804 First (Choices (First (Component_Associations (N)))));
1809 and then Nkind (Parent (N)) /= N_Component_Association
1810 and then No (Expressions (N))
1812 Nkind (First (Choices (First (Component_Associations (N)))))
1814 and then Is_Elementary_Type (Component_Typ)
1818 Assoc : constant Node_Id := First (Component_Associations (N));
1821 Make_Component_Association (Loc,
1824 Make_Attribute_Reference (Loc,
1825 Prefix => New_Occurrence_Of (Index_Typ, Loc),
1826 Attribute_Name => Name_Range)),
1827 Expression => Relocate_Node (Expression (Assoc))));
1828 return Resolve_Array_Aggregate
1829 (N, Index, Index_Constr, Component_Typ, Others_Allowed);
1833 -- Protect against cascaded errors
1835 if Etype (Index_Typ) = Any_Type then
1839 -- STEP 2: Process named components
1841 if No (Expressions (N)) then
1842 if Others_Present then
1843 Case_Table_Size := Nb_Choices - 1;
1845 Case_Table_Size := Nb_Choices;
1851 -- Denote the lowest and highest values in an aggregate choice
1855 -- High end of one range and Low end of the next. Should be
1856 -- contiguous if there is no hole in the list of values.
1858 Missing_Values : Boolean;
1859 -- Set True if missing index values
1861 S_Low : Node_Id := Empty;
1862 S_High : Node_Id := Empty;
1863 -- if a choice in an aggregate is a subtype indication these
1864 -- denote the lowest and highest values of the subtype
1866 Table : Case_Table_Type (1 .. Case_Table_Size);
1867 -- Used to sort all the different choice values
1869 Single_Choice : Boolean;
1870 -- Set to true every time there is a single discrete choice in a
1871 -- discrete association
1873 Prev_Nb_Discrete_Choices : Nat;
1874 -- Used to keep track of the number of discrete choices in the
1875 -- current association.
1877 Errors_Posted_On_Choices : Boolean := False;
1878 -- Keeps track of whether any choices have semantic errors
1881 -- STEP 2 (A): Check discrete choices validity
1883 Assoc := First (Component_Associations (N));
1884 while Present (Assoc) loop
1885 Prev_Nb_Discrete_Choices := Nb_Discrete_Choices;
1886 Choice := First (Choices (Assoc));
1890 if Nkind (Choice) = N_Others_Choice then
1891 Single_Choice := False;
1894 -- Test for subtype mark without constraint
1896 elsif Is_Entity_Name (Choice) and then
1897 Is_Type (Entity (Choice))
1899 if Base_Type (Entity (Choice)) /= Index_Base then
1901 ("invalid subtype mark in aggregate choice",
1906 -- Case of subtype indication
1908 elsif Nkind (Choice) = N_Subtype_Indication then
1909 Resolve_Discrete_Subtype_Indication (Choice, Index_Base);
1911 -- Does the subtype indication evaluation raise CE ?
1913 Get_Index_Bounds (Subtype_Mark (Choice), S_Low, S_High);
1914 Get_Index_Bounds (Choice, Low, High);
1915 Check_Bounds (S_Low, S_High, Low, High);
1917 -- Case of range or expression
1920 Resolve (Choice, Index_Base);
1921 Check_Unset_Reference (Choice);
1922 Check_Non_Static_Context (Choice);
1924 -- If semantic errors were posted on the choice, then
1925 -- record that for possible early return from later
1926 -- processing (see handling of enumeration choices).
1928 if Error_Posted (Choice) then
1929 Errors_Posted_On_Choices := True;
1932 -- Do not range check a choice. This check is redundant
1933 -- since this test is already done when we check that the
1934 -- bounds of the array aggregate are within range.
1936 Set_Do_Range_Check (Choice, False);
1938 -- In SPARK, the choice must be static
1940 if not (Is_Static_Expression (Choice)
1941 or else (Nkind (Choice) = N_Range
1942 and then Is_Static_Range (Choice)))
1944 Check_SPARK_Restriction
1945 ("choice should be static", Choice);
1949 -- If we could not resolve the discrete choice stop here
1951 if Etype (Choice) = Any_Type then
1954 -- If the discrete choice raises CE get its original bounds
1956 elsif Nkind (Choice) = N_Raise_Constraint_Error then
1957 Set_Raises_Constraint_Error (N);
1958 Get_Index_Bounds (Original_Node (Choice), Low, High);
1960 -- Otherwise get its bounds as usual
1963 Get_Index_Bounds (Choice, Low, High);
1966 if (Dynamic_Or_Null_Range (Low, High)
1967 or else (Nkind (Choice) = N_Subtype_Indication
1969 Dynamic_Or_Null_Range (S_Low, S_High)))
1970 and then Nb_Choices /= 1
1973 ("dynamic or empty choice in aggregate " &
1974 "must be the only choice", Choice);
1978 Nb_Discrete_Choices := Nb_Discrete_Choices + 1;
1979 Table (Nb_Discrete_Choices).Choice_Lo := Low;
1980 Table (Nb_Discrete_Choices).Choice_Hi := High;
1986 -- Check if we have a single discrete choice and whether
1987 -- this discrete choice specifies a single value.
1990 (Nb_Discrete_Choices = Prev_Nb_Discrete_Choices + 1)
1991 and then (Low = High);
1997 -- Ada 2005 (AI-231)
1999 if Ada_Version >= Ada_2005
2000 and then Known_Null (Expression (Assoc))
2002 Check_Can_Never_Be_Null (Etype (N), Expression (Assoc));
2005 -- Ada 2005 (AI-287): In case of default initialized component
2006 -- we delay the resolution to the expansion phase.
2008 if Box_Present (Assoc) then
2010 -- Ada 2005 (AI-287): In case of default initialization of a
2011 -- component the expander will generate calls to the
2012 -- corresponding initialization subprogram. We need to call
2013 -- Resolve_Aggr_Expr to check the rules about
2016 if not Resolve_Aggr_Expr (Assoc,
2017 Single_Elmt => Single_Choice)
2022 elsif not Resolve_Aggr_Expr (Expression (Assoc),
2023 Single_Elmt => Single_Choice)
2027 -- Check incorrect use of dynamically tagged expression
2029 -- We differentiate here two cases because the expression may
2030 -- not be decorated. For example, the analysis and resolution
2031 -- of the expression associated with the others choice will be
2032 -- done later with the full aggregate. In such case we
2033 -- duplicate the expression tree to analyze the copy and
2034 -- perform the required check.
2036 elsif not Present (Etype (Expression (Assoc))) then
2038 Save_Analysis : constant Boolean := Full_Analysis;
2039 Expr : constant Node_Id :=
2040 New_Copy_Tree (Expression (Assoc));
2043 Expander_Mode_Save_And_Set (False);
2044 Full_Analysis := False;
2046 -- Analyze the expression, making sure it is properly
2047 -- attached to the tree before we do the analysis.
2049 Set_Parent (Expr, Parent (Expression (Assoc)));
2052 -- If the expression is a literal, propagate this info
2053 -- to the expression in the association, to enable some
2054 -- optimizations downstream.
2056 if Is_Entity_Name (Expr)
2057 and then Present (Entity (Expr))
2058 and then Ekind (Entity (Expr)) = E_Enumeration_Literal
2061 (Expression (Assoc), Component_Typ);
2064 Full_Analysis := Save_Analysis;
2065 Expander_Mode_Restore;
2067 if Is_Tagged_Type (Etype (Expr)) then
2068 Check_Dynamically_Tagged_Expression
2070 Typ => Component_Type (Etype (N)),
2075 elsif Is_Tagged_Type (Etype (Expression (Assoc))) then
2076 Check_Dynamically_Tagged_Expression
2077 (Expr => Expression (Assoc),
2078 Typ => Component_Type (Etype (N)),
2085 -- If aggregate contains more than one choice then these must be
2086 -- static. Sort them and check that they are contiguous.
2088 if Nb_Discrete_Choices > 1 then
2089 Sort_Case_Table (Table);
2090 Missing_Values := False;
2092 Outer : for J in 1 .. Nb_Discrete_Choices - 1 loop
2093 if Expr_Value (Table (J).Choice_Hi) >=
2094 Expr_Value (Table (J + 1).Choice_Lo)
2097 ("duplicate choice values in array aggregate",
2098 Table (J).Choice_Hi);
2101 elsif not Others_Present then
2102 Hi_Val := Expr_Value (Table (J).Choice_Hi);
2103 Lo_Val := Expr_Value (Table (J + 1).Choice_Lo);
2105 -- If missing values, output error messages
2107 if Lo_Val - Hi_Val > 1 then
2109 -- Header message if not first missing value
2111 if not Missing_Values then
2113 ("missing index value(s) in array aggregate", N);
2114 Missing_Values := True;
2117 -- Output values of missing indexes
2119 Lo_Val := Lo_Val - 1;
2120 Hi_Val := Hi_Val + 1;
2122 -- Enumeration type case
2124 if Is_Enumeration_Type (Index_Typ) then
2127 (Get_Enum_Lit_From_Pos
2128 (Index_Typ, Hi_Val, Loc));
2130 if Lo_Val = Hi_Val then
2131 Error_Msg_N ("\ %", N);
2135 (Get_Enum_Lit_From_Pos
2136 (Index_Typ, Lo_Val, Loc));
2137 Error_Msg_N ("\ % .. %", N);
2140 -- Integer types case
2143 Error_Msg_Uint_1 := Hi_Val;
2145 if Lo_Val = Hi_Val then
2146 Error_Msg_N ("\ ^", N);
2148 Error_Msg_Uint_2 := Lo_Val;
2149 Error_Msg_N ("\ ^ .. ^", N);
2156 if Missing_Values then
2157 Set_Etype (N, Any_Composite);
2162 -- STEP 2 (B): Compute aggregate bounds and min/max choices values
2164 if Nb_Discrete_Choices > 0 then
2165 Choices_Low := Table (1).Choice_Lo;
2166 Choices_High := Table (Nb_Discrete_Choices).Choice_Hi;
2169 -- If Others is present, then bounds of aggregate come from the
2170 -- index constraint (not the choices in the aggregate itself).
2172 if Others_Present then
2173 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2175 -- No others clause present
2178 -- Special processing if others allowed and not present. This
2179 -- means that the bounds of the aggregate come from the index
2180 -- constraint (and the length must match).
2182 if Others_Allowed then
2183 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2185 -- If others allowed, and no others present, then the array
2186 -- should cover all index values. If it does not, we will
2187 -- get a length check warning, but there is two cases where
2188 -- an additional warning is useful:
2190 -- If we have no positional components, and the length is
2191 -- wrong (which we can tell by others being allowed with
2192 -- missing components), and the index type is an enumeration
2193 -- type, then issue appropriate warnings about these missing
2194 -- components. They are only warnings, since the aggregate
2195 -- is fine, it's just the wrong length. We skip this check
2196 -- for standard character types (since there are no literals
2197 -- and it is too much trouble to concoct them), and also if
2198 -- any of the bounds have not-known-at-compile-time values.
2200 -- Another case warranting a warning is when the length is
2201 -- right, but as above we have an index type that is an
2202 -- enumeration, and the bounds do not match. This is a
2203 -- case where dubious sliding is allowed and we generate
2204 -- a warning that the bounds do not match.
2206 if No (Expressions (N))
2207 and then Nkind (Index) = N_Range
2208 and then Is_Enumeration_Type (Etype (Index))
2209 and then not Is_Standard_Character_Type (Etype (Index))
2210 and then Compile_Time_Known_Value (Aggr_Low)
2211 and then Compile_Time_Known_Value (Aggr_High)
2212 and then Compile_Time_Known_Value (Choices_Low)
2213 and then Compile_Time_Known_Value (Choices_High)
2215 -- If any of the expressions or range bounds in choices
2216 -- have semantic errors, then do not attempt further
2217 -- resolution, to prevent cascaded errors.
2219 if Errors_Posted_On_Choices then
2224 ALo : constant Node_Id := Expr_Value_E (Aggr_Low);
2225 AHi : constant Node_Id := Expr_Value_E (Aggr_High);
2226 CLo : constant Node_Id := Expr_Value_E (Choices_Low);
2227 CHi : constant Node_Id := Expr_Value_E (Choices_High);
2232 -- Warning case 1, missing values at start/end. Only
2233 -- do the check if the number of entries is too small.
2235 if (Enumeration_Pos (CHi) - Enumeration_Pos (CLo))
2237 (Enumeration_Pos (AHi) - Enumeration_Pos (ALo))
2240 ("missing index value(s) in array aggregate?", N);
2242 -- Output missing value(s) at start
2244 if Chars (ALo) /= Chars (CLo) then
2247 if Chars (ALo) = Chars (Ent) then
2248 Error_Msg_Name_1 := Chars (ALo);
2249 Error_Msg_N ("\ %?", N);
2251 Error_Msg_Name_1 := Chars (ALo);
2252 Error_Msg_Name_2 := Chars (Ent);
2253 Error_Msg_N ("\ % .. %?", N);
2257 -- Output missing value(s) at end
2259 if Chars (AHi) /= Chars (CHi) then
2262 if Chars (AHi) = Chars (Ent) then
2263 Error_Msg_Name_1 := Chars (Ent);
2264 Error_Msg_N ("\ %?", N);
2266 Error_Msg_Name_1 := Chars (Ent);
2267 Error_Msg_Name_2 := Chars (AHi);
2268 Error_Msg_N ("\ % .. %?", N);
2272 -- Warning case 2, dubious sliding. The First_Subtype
2273 -- test distinguishes between a constrained type where
2274 -- sliding is not allowed (so we will get a warning
2275 -- later that Constraint_Error will be raised), and
2276 -- the unconstrained case where sliding is permitted.
2278 elsif (Enumeration_Pos (CHi) - Enumeration_Pos (CLo))
2280 (Enumeration_Pos (AHi) - Enumeration_Pos (ALo))
2281 and then Chars (ALo) /= Chars (CLo)
2283 not Is_Constrained (First_Subtype (Etype (N)))
2286 ("bounds of aggregate do not match target?", N);
2292 -- If no others, aggregate bounds come from aggregate
2294 Aggr_Low := Choices_Low;
2295 Aggr_High := Choices_High;
2299 -- STEP 3: Process positional components
2302 -- STEP 3 (A): Process positional elements
2304 Expr := First (Expressions (N));
2305 Nb_Elements := Uint_0;
2306 while Present (Expr) loop
2307 Nb_Elements := Nb_Elements + 1;
2309 -- Ada 2005 (AI-231)
2311 if Ada_Version >= Ada_2005
2312 and then Known_Null (Expr)
2314 Check_Can_Never_Be_Null (Etype (N), Expr);
2317 if not Resolve_Aggr_Expr (Expr, Single_Elmt => True) then
2321 -- Check incorrect use of dynamically tagged expression
2323 if Is_Tagged_Type (Etype (Expr)) then
2324 Check_Dynamically_Tagged_Expression
2326 Typ => Component_Type (Etype (N)),
2333 if Others_Present then
2334 Assoc := Last (Component_Associations (N));
2336 -- Ada 2005 (AI-231)
2338 if Ada_Version >= Ada_2005
2339 and then Known_Null (Assoc)
2341 Check_Can_Never_Be_Null (Etype (N), Expression (Assoc));
2344 -- Ada 2005 (AI-287): In case of default initialized component,
2345 -- we delay the resolution to the expansion phase.
2347 if Box_Present (Assoc) then
2349 -- Ada 2005 (AI-287): In case of default initialization of a
2350 -- component the expander will generate calls to the
2351 -- corresponding initialization subprogram. We need to call
2352 -- Resolve_Aggr_Expr to check the rules about
2355 if not Resolve_Aggr_Expr (Assoc, Single_Elmt => False) then
2359 elsif not Resolve_Aggr_Expr (Expression (Assoc),
2360 Single_Elmt => False)
2364 -- Check incorrect use of dynamically tagged expression. The
2365 -- expression of the others choice has not been resolved yet.
2366 -- In order to diagnose the semantic error we create a duplicate
2367 -- tree to analyze it and perform the check.
2371 Save_Analysis : constant Boolean := Full_Analysis;
2372 Expr : constant Node_Id :=
2373 New_Copy_Tree (Expression (Assoc));
2376 Expander_Mode_Save_And_Set (False);
2377 Full_Analysis := False;
2379 Full_Analysis := Save_Analysis;
2380 Expander_Mode_Restore;
2382 if Is_Tagged_Type (Etype (Expr)) then
2383 Check_Dynamically_Tagged_Expression
2385 Typ => Component_Type (Etype (N)),
2392 -- STEP 3 (B): Compute the aggregate bounds
2394 if Others_Present then
2395 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2398 if Others_Allowed then
2399 Get_Index_Bounds (Index_Constr, Aggr_Low, Discard);
2401 Aggr_Low := Index_Typ_Low;
2404 Aggr_High := Add (Nb_Elements - 1, To => Aggr_Low);
2405 Check_Bound (Index_Base_High, Aggr_High);
2409 -- STEP 4: Perform static aggregate checks and save the bounds
2413 Check_Bounds (Index_Typ_Low, Index_Typ_High, Aggr_Low, Aggr_High);
2414 Check_Bounds (Index_Base_Low, Index_Base_High, Aggr_Low, Aggr_High);
2418 if Others_Present and then Nb_Discrete_Choices > 0 then
2419 Check_Bounds (Aggr_Low, Aggr_High, Choices_Low, Choices_High);
2420 Check_Bounds (Index_Typ_Low, Index_Typ_High,
2421 Choices_Low, Choices_High);
2422 Check_Bounds (Index_Base_Low, Index_Base_High,
2423 Choices_Low, Choices_High);
2427 elsif Others_Present and then Nb_Elements > 0 then
2428 Check_Length (Aggr_Low, Aggr_High, Nb_Elements);
2429 Check_Length (Index_Typ_Low, Index_Typ_High, Nb_Elements);
2430 Check_Length (Index_Base_Low, Index_Base_High, Nb_Elements);
2433 if Raises_Constraint_Error (Aggr_Low)
2434 or else Raises_Constraint_Error (Aggr_High)
2436 Set_Raises_Constraint_Error (N);
2439 Aggr_Low := Duplicate_Subexpr (Aggr_Low);
2441 -- Do not duplicate Aggr_High if Aggr_High = Aggr_Low + Nb_Elements
2442 -- since the addition node returned by Add is not yet analyzed. Attach
2443 -- to tree and analyze first. Reset analyzed flag to ensure it will get
2444 -- analyzed when it is a literal bound whose type must be properly set.
2446 if Others_Present or else Nb_Discrete_Choices > 0 then
2447 Aggr_High := Duplicate_Subexpr (Aggr_High);
2449 if Etype (Aggr_High) = Universal_Integer then
2450 Set_Analyzed (Aggr_High, False);
2454 -- If the aggregate already has bounds attached to it, it means this is
2455 -- a positional aggregate created as an optimization by
2456 -- Exp_Aggr.Convert_To_Positional, so we don't want to change those
2459 if Present (Aggregate_Bounds (N)) and then not Others_Allowed then
2460 Aggr_Low := Low_Bound (Aggregate_Bounds (N));
2461 Aggr_High := High_Bound (Aggregate_Bounds (N));
2464 Set_Aggregate_Bounds
2465 (N, Make_Range (Loc, Low_Bound => Aggr_Low, High_Bound => Aggr_High));
2467 -- The bounds may contain expressions that must be inserted upwards.
2468 -- Attach them fully to the tree. After analysis, remove side effects
2469 -- from upper bound, if still needed.
2471 Set_Parent (Aggregate_Bounds (N), N);
2472 Analyze_And_Resolve (Aggregate_Bounds (N), Index_Typ);
2473 Check_Unset_Reference (Aggregate_Bounds (N));
2475 if not Others_Present and then Nb_Discrete_Choices = 0 then
2476 Set_High_Bound (Aggregate_Bounds (N),
2477 Duplicate_Subexpr (High_Bound (Aggregate_Bounds (N))));
2481 end Resolve_Array_Aggregate;
2483 ---------------------------------
2484 -- Resolve_Extension_Aggregate --
2485 ---------------------------------
2487 -- There are two cases to consider:
2489 -- a) If the ancestor part is a type mark, the components needed are the
2490 -- difference between the components of the expected type and the
2491 -- components of the given type mark.
2493 -- b) If the ancestor part is an expression, it must be unambiguous, and
2494 -- once we have its type we can also compute the needed components as in
2495 -- the previous case. In both cases, if the ancestor type is not the
2496 -- immediate ancestor, we have to build this ancestor recursively.
2498 -- In both cases, discriminants of the ancestor type do not play a role in
2499 -- the resolution of the needed components, because inherited discriminants
2500 -- cannot be used in a type extension. As a result we can compute
2501 -- independently the list of components of the ancestor type and of the
2504 procedure Resolve_Extension_Aggregate (N : Node_Id; Typ : Entity_Id) is
2505 A : constant Node_Id := Ancestor_Part (N);
2510 function Valid_Limited_Ancestor (Anc : Node_Id) return Boolean;
2511 -- If the type is limited, verify that the ancestor part is a legal
2512 -- expression (aggregate or function call, including 'Input)) that does
2513 -- not require a copy, as specified in 7.5(2).
2515 function Valid_Ancestor_Type return Boolean;
2516 -- Verify that the type of the ancestor part is a non-private ancestor
2517 -- of the expected type, which must be a type extension.
2519 ----------------------------
2520 -- Valid_Limited_Ancestor --
2521 ----------------------------
2523 function Valid_Limited_Ancestor (Anc : Node_Id) return Boolean is
2525 if Is_Entity_Name (Anc)
2526 and then Is_Type (Entity (Anc))
2530 elsif Nkind_In (Anc, N_Aggregate, N_Function_Call) then
2533 elsif Nkind (Anc) = N_Attribute_Reference
2534 and then Attribute_Name (Anc) = Name_Input
2538 elsif Nkind (Anc) = N_Qualified_Expression then
2539 return Valid_Limited_Ancestor (Expression (Anc));
2544 end Valid_Limited_Ancestor;
2546 -------------------------
2547 -- Valid_Ancestor_Type --
2548 -------------------------
2550 function Valid_Ancestor_Type return Boolean is
2551 Imm_Type : Entity_Id;
2554 Imm_Type := Base_Type (Typ);
2555 while Is_Derived_Type (Imm_Type) loop
2556 if Etype (Imm_Type) = Base_Type (A_Type) then
2559 -- The base type of the parent type may appear as a private
2560 -- extension if it is declared as such in a parent unit of the
2561 -- current one. For consistency of the subsequent analysis use
2562 -- the partial view for the ancestor part.
2564 elsif Is_Private_Type (Etype (Imm_Type))
2565 and then Present (Full_View (Etype (Imm_Type)))
2566 and then Base_Type (A_Type) = Full_View (Etype (Imm_Type))
2568 A_Type := Etype (Imm_Type);
2571 -- The parent type may be a private extension. The aggregate is
2572 -- legal if the type of the aggregate is an extension of it that
2573 -- is not a private extension.
2575 elsif Is_Private_Type (A_Type)
2576 and then not Is_Private_Type (Imm_Type)
2577 and then Present (Full_View (A_Type))
2578 and then Base_Type (Full_View (A_Type)) = Etype (Imm_Type)
2583 Imm_Type := Etype (Base_Type (Imm_Type));
2587 -- If previous loop did not find a proper ancestor, report error
2589 Error_Msg_NE ("expect ancestor type of &", A, Typ);
2591 end Valid_Ancestor_Type;
2593 -- Start of processing for Resolve_Extension_Aggregate
2596 -- Analyze the ancestor part and account for the case where it is a
2597 -- parameterless function call.
2600 Check_Parameterless_Call (A);
2602 -- In SPARK, the ancestor part cannot be a type mark
2604 if Is_Entity_Name (A)
2605 and then Is_Type (Entity (A))
2607 Check_SPARK_Restriction ("ancestor part cannot be a type mark", A);
2609 -- AI05-0115: if the ancestor part is a subtype mark, the ancestor
2610 -- must not have unknown discriminants.
2612 if Has_Unknown_Discriminants (Root_Type (Typ)) then
2614 ("aggregate not available for type& whose ancestor "
2615 & "has unknown discriminants", N, Typ);
2619 if not Is_Tagged_Type (Typ) then
2620 Error_Msg_N ("type of extension aggregate must be tagged", N);
2623 elsif Is_Limited_Type (Typ) then
2625 -- Ada 2005 (AI-287): Limited aggregates are allowed
2627 if Ada_Version < Ada_2005 then
2628 Error_Msg_N ("aggregate type cannot be limited", N);
2629 Explain_Limited_Type (Typ, N);
2632 elsif Valid_Limited_Ancestor (A) then
2637 ("limited ancestor part must be aggregate or function call", A);
2640 elsif Is_Class_Wide_Type (Typ) then
2641 Error_Msg_N ("aggregate cannot be of a class-wide type", N);
2645 if Is_Entity_Name (A)
2646 and then Is_Type (Entity (A))
2648 A_Type := Get_Full_View (Entity (A));
2650 if Valid_Ancestor_Type then
2651 Set_Entity (A, A_Type);
2652 Set_Etype (A, A_Type);
2654 Validate_Ancestor_Part (N);
2655 Resolve_Record_Aggregate (N, Typ);
2658 elsif Nkind (A) /= N_Aggregate then
2659 if Is_Overloaded (A) then
2662 Get_First_Interp (A, I, It);
2663 while Present (It.Typ) loop
2664 -- Only consider limited interpretations in the Ada 2005 case
2666 if Is_Tagged_Type (It.Typ)
2667 and then (Ada_Version >= Ada_2005
2668 or else not Is_Limited_Type (It.Typ))
2670 if A_Type /= Any_Type then
2671 Error_Msg_N ("cannot resolve expression", A);
2678 Get_Next_Interp (I, It);
2681 if A_Type = Any_Type then
2682 if Ada_Version >= Ada_2005 then
2683 Error_Msg_N ("ancestor part must be of a tagged type", A);
2686 ("ancestor part must be of a nonlimited tagged type", A);
2693 A_Type := Etype (A);
2696 if Valid_Ancestor_Type then
2697 Resolve (A, A_Type);
2698 Check_Unset_Reference (A);
2699 Check_Non_Static_Context (A);
2701 -- The aggregate is illegal if the ancestor expression is a call
2702 -- to a function with a limited unconstrained result, unless the
2703 -- type of the aggregate is a null extension. This restriction
2704 -- was added in AI05-67 to simplify implementation.
2706 if Nkind (A) = N_Function_Call
2707 and then Is_Limited_Type (A_Type)
2708 and then not Is_Null_Extension (Typ)
2709 and then not Is_Constrained (A_Type)
2712 ("type of limited ancestor part must be constrained", A);
2714 -- Reject the use of CPP constructors that leave objects partially
2715 -- initialized. For example:
2717 -- type CPP_Root is tagged limited record ...
2718 -- pragma Import (CPP, CPP_Root);
2720 -- type CPP_DT is new CPP_Root and Iface ...
2721 -- pragma Import (CPP, CPP_DT);
2723 -- type Ada_DT is new CPP_DT with ...
2725 -- Obj : Ada_DT := Ada_DT'(New_CPP_Root with others => <>);
2727 -- Using the constructor of CPP_Root the slots of the dispatch
2728 -- table of CPP_DT cannot be set, and the secondary tag of
2729 -- CPP_DT is unknown.
2731 elsif Nkind (A) = N_Function_Call
2732 and then Is_CPP_Constructor_Call (A)
2733 and then Enclosing_CPP_Parent (Typ) /= A_Type
2736 ("?must use 'C'P'P constructor for type &", A,
2737 Enclosing_CPP_Parent (Typ));
2739 -- The following call is not needed if the previous warning
2740 -- is promoted to an error.
2742 Resolve_Record_Aggregate (N, Typ);
2744 elsif Is_Class_Wide_Type (Etype (A))
2745 and then Nkind (Original_Node (A)) = N_Function_Call
2747 -- If the ancestor part is a dispatching call, it appears
2748 -- statically to be a legal ancestor, but it yields any member
2749 -- of the class, and it is not possible to determine whether
2750 -- it is an ancestor of the extension aggregate (much less
2751 -- which ancestor). It is not possible to determine the
2752 -- components of the extension part.
2754 -- This check implements AI-306, which in fact was motivated by
2755 -- an AdaCore query to the ARG after this test was added.
2757 Error_Msg_N ("ancestor part must be statically tagged", A);
2759 Resolve_Record_Aggregate (N, Typ);
2764 Error_Msg_N ("no unique type for this aggregate", A);
2766 end Resolve_Extension_Aggregate;
2768 ------------------------------
2769 -- Resolve_Record_Aggregate --
2770 ------------------------------
2772 procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id) is
2774 -- N_Component_Association node belonging to the input aggregate N
2777 Positional_Expr : Node_Id;
2778 Component : Entity_Id;
2779 Component_Elmt : Elmt_Id;
2781 Components : constant Elist_Id := New_Elmt_List;
2782 -- Components is the list of the record components whose value must be
2783 -- provided in the aggregate. This list does include discriminants.
2785 New_Assoc_List : constant List_Id := New_List;
2786 New_Assoc : Node_Id;
2787 -- New_Assoc_List is the newly built list of N_Component_Association
2788 -- nodes. New_Assoc is one such N_Component_Association node in it.
2789 -- Note that while Assoc and New_Assoc contain the same kind of nodes,
2790 -- they are used to iterate over two different N_Component_Association
2793 Others_Etype : Entity_Id := Empty;
2794 -- This variable is used to save the Etype of the last record component
2795 -- that takes its value from the others choice. Its purpose is:
2797 -- (a) make sure the others choice is useful
2799 -- (b) make sure the type of all the components whose value is
2800 -- subsumed by the others choice are the same.
2802 -- This variable is updated as a side effect of function Get_Value.
2804 Is_Box_Present : Boolean := False;
2805 Others_Box : Boolean := False;
2806 -- Ada 2005 (AI-287): Variables used in case of default initialization
2807 -- to provide a functionality similar to Others_Etype. Box_Present
2808 -- indicates that the component takes its default initialization;
2809 -- Others_Box indicates that at least one component takes its default
2810 -- initialization. Similar to Others_Etype, they are also updated as a
2811 -- side effect of function Get_Value.
2813 procedure Add_Association
2814 (Component : Entity_Id;
2816 Assoc_List : List_Id;
2817 Is_Box_Present : Boolean := False);
2818 -- Builds a new N_Component_Association node which associates Component
2819 -- to expression Expr and adds it to the association list being built,
2820 -- either New_Assoc_List, or the association being built for an inner
2823 function Discr_Present (Discr : Entity_Id) return Boolean;
2824 -- If aggregate N is a regular aggregate this routine will return True.
2825 -- Otherwise, if N is an extension aggregate, Discr is a discriminant
2826 -- whose value may already have been specified by N's ancestor part.
2827 -- This routine checks whether this is indeed the case and if so returns
2828 -- False, signaling that no value for Discr should appear in N's
2829 -- aggregate part. Also, in this case, the routine appends to
2830 -- New_Assoc_List the discriminant value specified in the ancestor part.
2832 -- If the aggregate is in a context with expansion delayed, it will be
2833 -- reanalyzed. The inherited discriminant values must not be reinserted
2834 -- in the component list to prevent spurious errors, but they must be
2835 -- present on first analysis to build the proper subtype indications.
2836 -- The flag Inherited_Discriminant is used to prevent the re-insertion.
2841 Consider_Others_Choice : Boolean := False)
2843 -- Given a record component stored in parameter Compon, this function
2844 -- returns its value as it appears in the list From, which is a list
2845 -- of N_Component_Association nodes.
2847 -- If no component association has a choice for the searched component,
2848 -- the value provided by the others choice is returned, if there is one,
2849 -- and Consider_Others_Choice is set to true. Otherwise Empty is
2850 -- returned. If there is more than one component association giving a
2851 -- value for the searched record component, an error message is emitted
2852 -- and the first found value is returned.
2854 -- If Consider_Others_Choice is set and the returned expression comes
2855 -- from the others choice, then Others_Etype is set as a side effect.
2856 -- An error message is emitted if the components taking their value from
2857 -- the others choice do not have same type.
2859 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Node_Id);
2860 -- Analyzes and resolves expression Expr against the Etype of the
2861 -- Component. This routine also applies all appropriate checks to Expr.
2862 -- It finally saves a Expr in the newly created association list that
2863 -- will be attached to the final record aggregate. Note that if the
2864 -- Parent pointer of Expr is not set then Expr was produced with a
2865 -- New_Copy_Tree or some such.
2867 ---------------------
2868 -- Add_Association --
2869 ---------------------
2871 procedure Add_Association
2872 (Component : Entity_Id;
2874 Assoc_List : List_Id;
2875 Is_Box_Present : Boolean := False)
2878 Choice_List : constant List_Id := New_List;
2879 New_Assoc : Node_Id;
2882 -- If this is a box association the expression is missing, so
2883 -- use the Sloc of the aggregate itself for the new association.
2885 if Present (Expr) then
2891 Append (New_Occurrence_Of (Component, Loc), Choice_List);
2893 Make_Component_Association (Loc,
2894 Choices => Choice_List,
2896 Box_Present => Is_Box_Present);
2897 Append (New_Assoc, Assoc_List);
2898 end Add_Association;
2904 function Discr_Present (Discr : Entity_Id) return Boolean is
2905 Regular_Aggr : constant Boolean := Nkind (N) /= N_Extension_Aggregate;
2910 Comp_Assoc : Node_Id;
2911 Discr_Expr : Node_Id;
2913 Ancestor_Typ : Entity_Id;
2914 Orig_Discr : Entity_Id;
2916 D_Val : Elmt_Id := No_Elmt; -- stop junk warning
2918 Ancestor_Is_Subtyp : Boolean;
2921 if Regular_Aggr then
2925 -- Check whether inherited discriminant values have already been
2926 -- inserted in the aggregate. This will be the case if we are
2927 -- re-analyzing an aggregate whose expansion was delayed.
2929 if Present (Component_Associations (N)) then
2930 Comp_Assoc := First (Component_Associations (N));
2931 while Present (Comp_Assoc) loop
2932 if Inherited_Discriminant (Comp_Assoc) then
2940 Ancestor := Ancestor_Part (N);
2941 Ancestor_Typ := Etype (Ancestor);
2942 Loc := Sloc (Ancestor);
2944 -- For a private type with unknown discriminants, use the underlying
2945 -- record view if it is available.
2947 if Has_Unknown_Discriminants (Ancestor_Typ)
2948 and then Present (Full_View (Ancestor_Typ))
2949 and then Present (Underlying_Record_View (Full_View (Ancestor_Typ)))
2951 Ancestor_Typ := Underlying_Record_View (Full_View (Ancestor_Typ));
2954 Ancestor_Is_Subtyp :=
2955 Is_Entity_Name (Ancestor) and then Is_Type (Entity (Ancestor));
2957 -- If the ancestor part has no discriminants clearly N's aggregate
2958 -- part must provide a value for Discr.
2960 if not Has_Discriminants (Ancestor_Typ) then
2963 -- If the ancestor part is an unconstrained subtype mark then the
2964 -- Discr must be present in N's aggregate part.
2966 elsif Ancestor_Is_Subtyp
2967 and then not Is_Constrained (Entity (Ancestor))
2972 -- Now look to see if Discr was specified in the ancestor part
2974 if Ancestor_Is_Subtyp then
2975 D_Val := First_Elmt (Discriminant_Constraint (Entity (Ancestor)));
2978 Orig_Discr := Original_Record_Component (Discr);
2980 D := First_Discriminant (Ancestor_Typ);
2981 while Present (D) loop
2983 -- If Ancestor has already specified Disc value then insert its
2984 -- value in the final aggregate.
2986 if Original_Record_Component (D) = Orig_Discr then
2987 if Ancestor_Is_Subtyp then
2988 Discr_Expr := New_Copy_Tree (Node (D_Val));
2991 Make_Selected_Component (Loc,
2992 Prefix => Duplicate_Subexpr (Ancestor),
2993 Selector_Name => New_Occurrence_Of (Discr, Loc));
2996 Resolve_Aggr_Expr (Discr_Expr, Discr);
2997 Set_Inherited_Discriminant (Last (New_Assoc_List));
3001 Next_Discriminant (D);
3003 if Ancestor_Is_Subtyp then
3018 Consider_Others_Choice : Boolean := False)
3022 Expr : Node_Id := Empty;
3023 Selector_Name : Node_Id;
3026 Is_Box_Present := False;
3028 if Present (From) then
3029 Assoc := First (From);
3034 while Present (Assoc) loop
3035 Selector_Name := First (Choices (Assoc));
3036 while Present (Selector_Name) loop
3037 if Nkind (Selector_Name) = N_Others_Choice then
3038 if Consider_Others_Choice and then No (Expr) then
3040 -- We need to duplicate the expression for each
3041 -- successive component covered by the others choice.
3042 -- This is redundant if the others_choice covers only
3043 -- one component (small optimization possible???), but
3044 -- indispensable otherwise, because each one must be
3045 -- expanded individually to preserve side-effects.
3047 -- Ada 2005 (AI-287): In case of default initialization
3048 -- of components, we duplicate the corresponding default
3049 -- expression (from the record type declaration). The
3050 -- copy must carry the sloc of the association (not the
3051 -- original expression) to prevent spurious elaboration
3052 -- checks when the default includes function calls.
3054 if Box_Present (Assoc) then
3056 Is_Box_Present := True;
3058 if Expander_Active then
3061 (Expression (Parent (Compon)),
3062 New_Sloc => Sloc (Assoc));
3064 return Expression (Parent (Compon));
3068 if Present (Others_Etype) and then
3069 Base_Type (Others_Etype) /= Base_Type (Etype
3072 Error_Msg_N ("components in OTHERS choice must " &
3073 "have same type", Selector_Name);
3076 Others_Etype := Etype (Compon);
3078 if Expander_Active then
3079 return New_Copy_Tree (Expression (Assoc));
3081 return Expression (Assoc);
3086 elsif Chars (Compon) = Chars (Selector_Name) then
3089 -- Ada 2005 (AI-231)
3091 if Ada_Version >= Ada_2005
3092 and then Known_Null (Expression (Assoc))
3094 Check_Can_Never_Be_Null (Compon, Expression (Assoc));
3097 -- We need to duplicate the expression when several
3098 -- components are grouped together with a "|" choice.
3099 -- For instance "filed1 | filed2 => Expr"
3101 -- Ada 2005 (AI-287)
3103 if Box_Present (Assoc) then
3104 Is_Box_Present := True;
3106 -- Duplicate the default expression of the component
3107 -- from the record type declaration, so a new copy
3108 -- can be attached to the association.
3110 -- Note that we always copy the default expression,
3111 -- even when the association has a single choice, in
3112 -- order to create a proper association for the
3113 -- expanded aggregate.
3115 Expr := New_Copy_Tree (Expression (Parent (Compon)));
3118 if Present (Next (Selector_Name)) then
3119 Expr := New_Copy_Tree (Expression (Assoc));
3121 Expr := Expression (Assoc);
3125 Generate_Reference (Compon, Selector_Name, 'm');
3129 ("more than one value supplied for &",
3130 Selector_Name, Compon);
3135 Next (Selector_Name);
3144 -----------------------
3145 -- Resolve_Aggr_Expr --
3146 -----------------------
3148 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Node_Id) is
3149 New_C : Entity_Id := Component;
3150 Expr_Type : Entity_Id := Empty;
3152 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean;
3153 -- If the expression is an aggregate (possibly qualified) then its
3154 -- expansion is delayed until the enclosing aggregate is expanded
3155 -- into assignments. In that case, do not generate checks on the
3156 -- expression, because they will be generated later, and will other-
3157 -- wise force a copy (to remove side-effects) that would leave a
3158 -- dynamic-sized aggregate in the code, something that gigi cannot
3162 -- Set to True if the resolved Expr node needs to be relocated
3163 -- when attached to the newly created association list. This node
3164 -- need not be relocated if its parent pointer is not set.
3165 -- In fact in this case Expr is the output of a New_Copy_Tree call.
3166 -- if Relocate is True then we have analyzed the expression node
3167 -- in the original aggregate and hence it needs to be relocated
3168 -- when moved over the new association list.
3170 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean is
3171 Kind : constant Node_Kind := Nkind (Expr);
3173 return (Nkind_In (Kind, N_Aggregate, N_Extension_Aggregate)
3174 and then Present (Etype (Expr))
3175 and then Is_Record_Type (Etype (Expr))
3176 and then Expansion_Delayed (Expr))
3177 or else (Kind = N_Qualified_Expression
3178 and then Has_Expansion_Delayed (Expression (Expr)));
3179 end Has_Expansion_Delayed;
3181 -- Start of processing for Resolve_Aggr_Expr
3184 -- If the type of the component is elementary or the type of the
3185 -- aggregate does not contain discriminants, use the type of the
3186 -- component to resolve Expr.
3188 if Is_Elementary_Type (Etype (Component))
3189 or else not Has_Discriminants (Etype (N))
3191 Expr_Type := Etype (Component);
3193 -- Otherwise we have to pick up the new type of the component from
3194 -- the new constrained subtype of the aggregate. In fact components
3195 -- which are of a composite type might be constrained by a
3196 -- discriminant, and we want to resolve Expr against the subtype were
3197 -- all discriminant occurrences are replaced with their actual value.
3200 New_C := First_Component (Etype (N));
3201 while Present (New_C) loop
3202 if Chars (New_C) = Chars (Component) then
3203 Expr_Type := Etype (New_C);
3207 Next_Component (New_C);
3210 pragma Assert (Present (Expr_Type));
3212 -- For each range in an array type where a discriminant has been
3213 -- replaced with the constraint, check that this range is within
3214 -- the range of the base type. This checks is done in the init
3215 -- proc for regular objects, but has to be done here for
3216 -- aggregates since no init proc is called for them.
3218 if Is_Array_Type (Expr_Type) then
3221 -- Range of the current constrained index in the array
3223 Orig_Index : Node_Id := First_Index (Etype (Component));
3224 -- Range corresponding to the range Index above in the
3225 -- original unconstrained record type. The bounds of this
3226 -- range may be governed by discriminants.
3228 Unconstr_Index : Node_Id := First_Index (Etype (Expr_Type));
3229 -- Range corresponding to the range Index above for the
3230 -- unconstrained array type. This range is needed to apply
3234 Index := First_Index (Expr_Type);
3235 while Present (Index) loop
3236 if Depends_On_Discriminant (Orig_Index) then
3237 Apply_Range_Check (Index, Etype (Unconstr_Index));
3241 Next_Index (Orig_Index);
3242 Next_Index (Unconstr_Index);
3248 -- If the Parent pointer of Expr is not set, Expr is an expression
3249 -- duplicated by New_Tree_Copy (this happens for record aggregates
3250 -- that look like (Field1 | Filed2 => Expr) or (others => Expr)).
3251 -- Such a duplicated expression must be attached to the tree
3252 -- before analysis and resolution to enforce the rule that a tree
3253 -- fragment should never be analyzed or resolved unless it is
3254 -- attached to the current compilation unit.
3256 if No (Parent (Expr)) then
3257 Set_Parent (Expr, N);
3263 Analyze_And_Resolve (Expr, Expr_Type);
3264 Check_Expr_OK_In_Limited_Aggregate (Expr);
3265 Check_Non_Static_Context (Expr);
3266 Check_Unset_Reference (Expr);
3268 -- Check wrong use of class-wide types
3270 if Is_Class_Wide_Type (Etype (Expr)) then
3271 Error_Msg_N ("dynamically tagged expression not allowed", Expr);
3274 if not Has_Expansion_Delayed (Expr) then
3275 Aggregate_Constraint_Checks (Expr, Expr_Type);
3278 if Raises_Constraint_Error (Expr) then
3279 Set_Raises_Constraint_Error (N);
3282 -- If the expression has been marked as requiring a range check,
3283 -- then generate it here.
3285 if Do_Range_Check (Expr) then
3286 Set_Do_Range_Check (Expr, False);
3287 Generate_Range_Check (Expr, Expr_Type, CE_Range_Check_Failed);
3291 Add_Association (New_C, Relocate_Node (Expr), New_Assoc_List);
3293 Add_Association (New_C, Expr, New_Assoc_List);
3295 end Resolve_Aggr_Expr;
3297 -- Start of processing for Resolve_Record_Aggregate
3300 -- A record aggregate is restricted in SPARK:
3301 -- Each named association can have only a single choice.
3302 -- OTHERS cannot be used.
3303 -- Positional and named associations cannot be mixed.
3305 if Present (Component_Associations (N))
3306 and then Present (First (Component_Associations (N)))
3309 if Present (Expressions (N)) then
3310 Check_SPARK_Restriction
3311 ("named association cannot follow positional one",
3312 First (Choices (First (Component_Associations (N)))));
3319 Assoc := First (Component_Associations (N));
3320 while Present (Assoc) loop
3321 if List_Length (Choices (Assoc)) > 1 then
3322 Check_SPARK_Restriction
3323 ("component association in record aggregate must "
3324 & "contain a single choice", Assoc);
3327 if Nkind (First (Choices (Assoc))) = N_Others_Choice then
3328 Check_SPARK_Restriction
3329 ("record aggregate cannot contain OTHERS", Assoc);
3332 Assoc := Next (Assoc);
3337 -- We may end up calling Duplicate_Subexpr on expressions that are
3338 -- attached to New_Assoc_List. For this reason we need to attach it
3339 -- to the tree by setting its parent pointer to N. This parent point
3340 -- will change in STEP 8 below.
3342 Set_Parent (New_Assoc_List, N);
3344 -- STEP 1: abstract type and null record verification
3346 if Is_Abstract_Type (Typ) then
3347 Error_Msg_N ("type of aggregate cannot be abstract", N);
3350 if No (First_Entity (Typ)) and then Null_Record_Present (N) then
3354 elsif Present (First_Entity (Typ))
3355 and then Null_Record_Present (N)
3356 and then not Is_Tagged_Type (Typ)
3358 Error_Msg_N ("record aggregate cannot be null", N);
3361 -- If the type has no components, then the aggregate should either
3362 -- have "null record", or in Ada 2005 it could instead have a single
3363 -- component association given by "others => <>". For Ada 95 we flag
3364 -- an error at this point, but for Ada 2005 we proceed with checking
3365 -- the associations below, which will catch the case where it's not
3366 -- an aggregate with "others => <>". Note that the legality of a <>
3367 -- aggregate for a null record type was established by AI05-016.
3369 elsif No (First_Entity (Typ))
3370 and then Ada_Version < Ada_2005
3372 Error_Msg_N ("record aggregate must be null", N);
3376 -- STEP 2: Verify aggregate structure
3379 Selector_Name : Node_Id;
3380 Bad_Aggregate : Boolean := False;
3383 if Present (Component_Associations (N)) then
3384 Assoc := First (Component_Associations (N));
3389 while Present (Assoc) loop
3390 Selector_Name := First (Choices (Assoc));
3391 while Present (Selector_Name) loop
3392 if Nkind (Selector_Name) = N_Identifier then
3395 elsif Nkind (Selector_Name) = N_Others_Choice then
3396 if Selector_Name /= First (Choices (Assoc))
3397 or else Present (Next (Selector_Name))
3400 ("OTHERS must appear alone in a choice list",
3404 elsif Present (Next (Assoc)) then
3406 ("OTHERS must appear last in an aggregate",
3410 -- (Ada2005): If this is an association with a box,
3411 -- indicate that the association need not represent
3414 elsif Box_Present (Assoc) then
3420 ("selector name should be identifier or OTHERS",
3422 Bad_Aggregate := True;
3425 Next (Selector_Name);
3431 if Bad_Aggregate then
3436 -- STEP 3: Find discriminant Values
3439 Discrim : Entity_Id;
3440 Missing_Discriminants : Boolean := False;
3443 if Present (Expressions (N)) then
3444 Positional_Expr := First (Expressions (N));
3446 Positional_Expr := Empty;
3449 -- AI05-0115: if the ancestor part is a subtype mark, the ancestor
3450 -- must npt have unknown discriminants.
3452 if Is_Derived_Type (Typ)
3453 and then Has_Unknown_Discriminants (Root_Type (Typ))
3454 and then Nkind (N) /= N_Extension_Aggregate
3457 ("aggregate not available for type& whose ancestor "
3458 & "has unknown discriminants ", N, Typ);
3461 if Has_Unknown_Discriminants (Typ)
3462 and then Present (Underlying_Record_View (Typ))
3464 Discrim := First_Discriminant (Underlying_Record_View (Typ));
3465 elsif Has_Discriminants (Typ) then
3466 Discrim := First_Discriminant (Typ);
3471 -- First find the discriminant values in the positional components
3473 while Present (Discrim) and then Present (Positional_Expr) loop
3474 if Discr_Present (Discrim) then
3475 Resolve_Aggr_Expr (Positional_Expr, Discrim);
3477 -- Ada 2005 (AI-231)
3479 if Ada_Version >= Ada_2005
3480 and then Known_Null (Positional_Expr)
3482 Check_Can_Never_Be_Null (Discrim, Positional_Expr);
3485 Next (Positional_Expr);
3488 if Present (Get_Value (Discrim, Component_Associations (N))) then
3490 ("more than one value supplied for discriminant&",
3494 Next_Discriminant (Discrim);
3497 -- Find remaining discriminant values, if any, among named components
3499 while Present (Discrim) loop
3500 Expr := Get_Value (Discrim, Component_Associations (N), True);
3502 if not Discr_Present (Discrim) then
3503 if Present (Expr) then
3505 ("more than one value supplied for discriminant&",
3509 elsif No (Expr) then
3511 ("no value supplied for discriminant &", N, Discrim);
3512 Missing_Discriminants := True;
3515 Resolve_Aggr_Expr (Expr, Discrim);
3518 Next_Discriminant (Discrim);
3521 if Missing_Discriminants then
3525 -- At this point and until the beginning of STEP 6, New_Assoc_List
3526 -- contains only the discriminants and their values.
3530 -- STEP 4: Set the Etype of the record aggregate
3532 -- ??? This code is pretty much a copy of Sem_Ch3.Build_Subtype. That
3533 -- routine should really be exported in sem_util or some such and used
3534 -- in sem_ch3 and here rather than have a copy of the code which is a
3535 -- maintenance nightmare.
3537 -- ??? Performance WARNING. The current implementation creates a new
3538 -- itype for all aggregates whose base type is discriminated.
3539 -- This means that for record aggregates nested inside an array
3540 -- aggregate we will create a new itype for each record aggregate
3541 -- if the array component type has discriminants. For large aggregates
3542 -- this may be a problem. What should be done in this case is
3543 -- to reuse itypes as much as possible.
3545 if Has_Discriminants (Typ)
3546 or else (Has_Unknown_Discriminants (Typ)
3547 and then Present (Underlying_Record_View (Typ)))
3549 Build_Constrained_Itype : declare
3550 Loc : constant Source_Ptr := Sloc (N);
3552 Subtyp_Decl : Node_Id;
3555 C : constant List_Id := New_List;
3558 New_Assoc := First (New_Assoc_List);
3559 while Present (New_Assoc) loop
3560 Append (Duplicate_Subexpr (Expression (New_Assoc)), To => C);
3564 if Has_Unknown_Discriminants (Typ)
3565 and then Present (Underlying_Record_View (Typ))
3568 Make_Subtype_Indication (Loc,
3570 New_Occurrence_Of (Underlying_Record_View (Typ), Loc),
3572 Make_Index_Or_Discriminant_Constraint (Loc, C));
3575 Make_Subtype_Indication (Loc,
3577 New_Occurrence_Of (Base_Type (Typ), Loc),
3579 Make_Index_Or_Discriminant_Constraint (Loc, C));
3582 Def_Id := Create_Itype (Ekind (Typ), N);
3585 Make_Subtype_Declaration (Loc,
3586 Defining_Identifier => Def_Id,
3587 Subtype_Indication => Indic);
3588 Set_Parent (Subtyp_Decl, Parent (N));
3590 -- Itypes must be analyzed with checks off (see itypes.ads)
3592 Analyze (Subtyp_Decl, Suppress => All_Checks);
3594 Set_Etype (N, Def_Id);
3595 Check_Static_Discriminated_Subtype
3596 (Def_Id, Expression (First (New_Assoc_List)));
3597 end Build_Constrained_Itype;
3603 -- STEP 5: Get remaining components according to discriminant values
3606 Record_Def : Node_Id;
3607 Parent_Typ : Entity_Id;
3608 Root_Typ : Entity_Id;
3609 Parent_Typ_List : Elist_Id;
3610 Parent_Elmt : Elmt_Id;
3611 Errors_Found : Boolean := False;
3614 function Find_Private_Ancestor return Entity_Id;
3615 -- AI05-0115: Find earlier ancestor in the derivation chain that is
3616 -- derived from a private view. Whether the aggregate is legal
3617 -- depends on the current visibility of the type as well as that
3618 -- of the parent of the ancestor.
3620 ---------------------------
3621 -- Find_Private_Ancestor --
3622 ---------------------------
3624 function Find_Private_Ancestor return Entity_Id is
3629 if Has_Private_Ancestor (Par)
3630 and then not Has_Private_Ancestor (Etype (Base_Type (Par)))
3634 elsif not Is_Derived_Type (Par) then
3638 Par := Etype (Base_Type (Par));
3641 end Find_Private_Ancestor;
3644 if Is_Derived_Type (Typ) and then Is_Tagged_Type (Typ) then
3645 Parent_Typ_List := New_Elmt_List;
3647 -- If this is an extension aggregate, the component list must
3648 -- include all components that are not in the given ancestor type.
3649 -- Otherwise, the component list must include components of all
3650 -- ancestors, starting with the root.
3652 if Nkind (N) = N_Extension_Aggregate then
3653 Root_Typ := Base_Type (Etype (Ancestor_Part (N)));
3656 -- AI05-0115: check legality of aggregate for type with
3657 -- aa private ancestor.
3659 Root_Typ := Root_Type (Typ);
3660 if Has_Private_Ancestor (Typ) then
3662 Ancestor : constant Entity_Id :=
3663 Find_Private_Ancestor;
3664 Ancestor_Unit : constant Entity_Id :=
3665 Cunit_Entity (Get_Source_Unit (Ancestor));
3666 Parent_Unit : constant Entity_Id :=
3668 (Get_Source_Unit (Base_Type (Etype (Ancestor))));
3671 -- check whether we are in a scope that has full view
3672 -- over the private ancestor and its parent. This can
3673 -- only happen if the derivation takes place in a child
3674 -- unit of the unit that declares the parent, and we are
3675 -- in the private part or body of that child unit, else
3676 -- the aggregate is illegal.
3678 if Is_Child_Unit (Ancestor_Unit)
3679 and then Scope (Ancestor_Unit) = Parent_Unit
3680 and then In_Open_Scopes (Scope (Ancestor))
3682 (In_Private_Part (Scope (Ancestor))
3683 or else In_Package_Body (Scope (Ancestor)))
3689 ("type of aggregate has private ancestor&!",
3691 Error_Msg_N ("must use extension aggregate!", N);
3697 Dnode := Declaration_Node (Base_Type (Root_Typ));
3699 -- If we don't get a full declaration, then we have some error
3700 -- which will get signalled later so skip this part. Otherwise
3701 -- gather components of root that apply to the aggregate type.
3702 -- We use the base type in case there is an applicable stored
3703 -- constraint that renames the discriminants of the root.
3705 if Nkind (Dnode) = N_Full_Type_Declaration then
3706 Record_Def := Type_Definition (Dnode);
3707 Gather_Components (Base_Type (Typ),
3708 Component_List (Record_Def),
3709 Governed_By => New_Assoc_List,
3711 Report_Errors => Errors_Found);
3715 Parent_Typ := Base_Type (Typ);
3716 while Parent_Typ /= Root_Typ loop
3717 Prepend_Elmt (Parent_Typ, To => Parent_Typ_List);
3718 Parent_Typ := Etype (Parent_Typ);
3720 if Nkind (Parent (Base_Type (Parent_Typ))) =
3721 N_Private_Type_Declaration
3722 or else Nkind (Parent (Base_Type (Parent_Typ))) =
3723 N_Private_Extension_Declaration
3725 if Nkind (N) /= N_Extension_Aggregate then
3727 ("type of aggregate has private ancestor&!",
3729 Error_Msg_N ("must use extension aggregate!", N);
3732 elsif Parent_Typ /= Root_Typ then
3734 ("ancestor part of aggregate must be private type&",
3735 Ancestor_Part (N), Parent_Typ);
3739 -- The current view of ancestor part may be a private type,
3740 -- while the context type is always non-private.
3742 elsif Is_Private_Type (Root_Typ)
3743 and then Present (Full_View (Root_Typ))
3744 and then Nkind (N) = N_Extension_Aggregate
3746 exit when Base_Type (Full_View (Root_Typ)) = Parent_Typ;
3750 -- Now collect components from all other ancestors, beginning
3751 -- with the current type. If the type has unknown discriminants
3752 -- use the component list of the Underlying_Record_View, which
3753 -- needs to be used for the subsequent expansion of the aggregate
3754 -- into assignments.
3756 Parent_Elmt := First_Elmt (Parent_Typ_List);
3757 while Present (Parent_Elmt) loop
3758 Parent_Typ := Node (Parent_Elmt);
3760 if Has_Unknown_Discriminants (Parent_Typ)
3761 and then Present (Underlying_Record_View (Typ))
3763 Parent_Typ := Underlying_Record_View (Parent_Typ);
3766 Record_Def := Type_Definition (Parent (Base_Type (Parent_Typ)));
3767 Gather_Components (Empty,
3768 Component_List (Record_Extension_Part (Record_Def)),
3769 Governed_By => New_Assoc_List,
3771 Report_Errors => Errors_Found);
3773 Next_Elmt (Parent_Elmt);
3777 Record_Def := Type_Definition (Parent (Base_Type (Typ)));
3779 if Null_Present (Record_Def) then
3782 elsif not Has_Unknown_Discriminants (Typ) then
3783 Gather_Components (Base_Type (Typ),
3784 Component_List (Record_Def),
3785 Governed_By => New_Assoc_List,
3787 Report_Errors => Errors_Found);
3791 (Base_Type (Underlying_Record_View (Typ)),
3792 Component_List (Record_Def),
3793 Governed_By => New_Assoc_List,
3795 Report_Errors => Errors_Found);
3799 if Errors_Found then
3804 -- STEP 6: Find component Values
3807 Component_Elmt := First_Elmt (Components);
3809 -- First scan the remaining positional associations in the aggregate.
3810 -- Remember that at this point Positional_Expr contains the current
3811 -- positional association if any is left after looking for discriminant
3812 -- values in step 3.
3814 while Present (Positional_Expr) and then Present (Component_Elmt) loop
3815 Component := Node (Component_Elmt);
3816 Resolve_Aggr_Expr (Positional_Expr, Component);
3818 -- Ada 2005 (AI-231)
3820 if Ada_Version >= Ada_2005
3821 and then Known_Null (Positional_Expr)
3823 Check_Can_Never_Be_Null (Component, Positional_Expr);
3826 if Present (Get_Value (Component, Component_Associations (N))) then
3828 ("more than one value supplied for Component &", N, Component);
3831 Next (Positional_Expr);
3832 Next_Elmt (Component_Elmt);
3835 if Present (Positional_Expr) then
3837 ("too many components for record aggregate", Positional_Expr);
3840 -- Now scan for the named arguments of the aggregate
3842 while Present (Component_Elmt) loop
3843 Component := Node (Component_Elmt);
3844 Expr := Get_Value (Component, Component_Associations (N), True);
3846 -- Note: The previous call to Get_Value sets the value of the
3847 -- variable Is_Box_Present.
3849 -- Ada 2005 (AI-287): Handle components with default initialization.
3850 -- Note: This feature was originally added to Ada 2005 for limited
3851 -- but it was finally allowed with any type.
3853 if Is_Box_Present then
3854 Check_Box_Component : declare
3855 Ctyp : constant Entity_Id := Etype (Component);
3858 -- If there is a default expression for the aggregate, copy
3859 -- it into a new association.
3861 -- If the component has an initialization procedure (IP) we
3862 -- pass the component to the expander, which will generate
3863 -- the call to such IP.
3865 -- If the component has discriminants, their values must
3866 -- be taken from their subtype. This is indispensable for
3867 -- constraints that are given by the current instance of an
3868 -- enclosing type, to allow the expansion of the aggregate
3869 -- to replace the reference to the current instance by the
3870 -- target object of the aggregate.
3872 if Present (Parent (Component))
3874 Nkind (Parent (Component)) = N_Component_Declaration
3875 and then Present (Expression (Parent (Component)))
3878 New_Copy_Tree (Expression (Parent (Component)),
3879 New_Sloc => Sloc (N));
3882 (Component => Component,
3884 Assoc_List => New_Assoc_List);
3885 Set_Has_Self_Reference (N);
3887 -- A box-defaulted access component gets the value null. Also
3888 -- included are components of private types whose underlying
3889 -- type is an access type. In either case set the type of the
3890 -- literal, for subsequent use in semantic checks.
3892 elsif Present (Underlying_Type (Ctyp))
3893 and then Is_Access_Type (Underlying_Type (Ctyp))
3895 if not Is_Private_Type (Ctyp) then
3896 Expr := Make_Null (Sloc (N));
3897 Set_Etype (Expr, Ctyp);
3899 (Component => Component,
3901 Assoc_List => New_Assoc_List);
3903 -- If the component's type is private with an access type as
3904 -- its underlying type then we have to create an unchecked
3905 -- conversion to satisfy type checking.
3909 Qual_Null : constant Node_Id :=
3910 Make_Qualified_Expression (Sloc (N),
3913 (Underlying_Type (Ctyp), Sloc (N)),
3914 Expression => Make_Null (Sloc (N)));
3916 Convert_Null : constant Node_Id :=
3917 Unchecked_Convert_To
3921 Analyze_And_Resolve (Convert_Null, Ctyp);
3923 (Component => Component,
3924 Expr => Convert_Null,
3925 Assoc_List => New_Assoc_List);
3929 elsif Has_Non_Null_Base_Init_Proc (Ctyp)
3930 or else not Expander_Active
3932 if Is_Record_Type (Ctyp)
3933 and then Has_Discriminants (Ctyp)
3934 and then not Is_Private_Type (Ctyp)
3936 -- We build a partially initialized aggregate with the
3937 -- values of the discriminants and box initialization
3938 -- for the rest, if other components are present.
3939 -- The type of the aggregate is the known subtype of
3940 -- the component. The capture of discriminants must
3941 -- be recursive because subcomponents may be constrained
3942 -- (transitively) by discriminants of enclosing types.
3943 -- For a private type with discriminants, a call to the
3944 -- initialization procedure will be generated, and no
3945 -- subaggregate is needed.
3947 Capture_Discriminants : declare
3948 Loc : constant Source_Ptr := Sloc (N);
3951 procedure Add_Discriminant_Values
3952 (New_Aggr : Node_Id;
3953 Assoc_List : List_Id);
3954 -- The constraint to a component may be given by a
3955 -- discriminant of the enclosing type, in which case
3956 -- we have to retrieve its value, which is part of the
3957 -- enclosing aggregate. Assoc_List provides the
3958 -- discriminant associations of the current type or
3959 -- of some enclosing record.
3961 procedure Propagate_Discriminants
3963 Assoc_List : List_Id);
3964 -- Nested components may themselves be discriminated
3965 -- types constrained by outer discriminants, whose
3966 -- values must be captured before the aggregate is
3967 -- expanded into assignments.
3969 -----------------------------
3970 -- Add_Discriminant_Values --
3971 -----------------------------
3973 procedure Add_Discriminant_Values
3974 (New_Aggr : Node_Id;
3975 Assoc_List : List_Id)
3979 Discr_Elmt : Elmt_Id;
3980 Discr_Val : Node_Id;
3984 Discr := First_Discriminant (Etype (New_Aggr));
3987 (Discriminant_Constraint (Etype (New_Aggr)));
3988 while Present (Discr_Elmt) loop
3989 Discr_Val := Node (Discr_Elmt);
3991 -- If the constraint is given by a discriminant
3992 -- it is a discriminant of an enclosing record,
3993 -- and its value has already been placed in the
3994 -- association list.
3996 if Is_Entity_Name (Discr_Val)
3998 Ekind (Entity (Discr_Val)) = E_Discriminant
4000 Val := Entity (Discr_Val);
4002 Assoc := First (Assoc_List);
4003 while Present (Assoc) loop
4005 (Entity (First (Choices (Assoc))))
4007 Entity (First (Choices (Assoc)))
4010 Discr_Val := Expression (Assoc);
4018 (Discr, New_Copy_Tree (Discr_Val),
4019 Component_Associations (New_Aggr));
4021 -- If the discriminant constraint is a current
4022 -- instance, mark the current aggregate so that
4023 -- the self-reference can be expanded later.
4025 if Nkind (Discr_Val) = N_Attribute_Reference
4026 and then Is_Entity_Name (Prefix (Discr_Val))
4027 and then Is_Type (Entity (Prefix (Discr_Val)))
4028 and then Etype (N) =
4029 Entity (Prefix (Discr_Val))
4031 Set_Has_Self_Reference (N);
4034 Next_Elmt (Discr_Elmt);
4035 Next_Discriminant (Discr);
4037 end Add_Discriminant_Values;
4039 ------------------------------
4040 -- Propagate_Discriminants --
4041 ------------------------------
4043 procedure Propagate_Discriminants
4045 Assoc_List : List_Id)
4047 Aggr_Type : constant Entity_Id :=
4048 Base_Type (Etype (Aggr));
4049 Def_Node : constant Node_Id :=
4051 (Declaration_Node (Aggr_Type));
4054 Comp_Elmt : Elmt_Id;
4055 Components : constant Elist_Id := New_Elmt_List;
4056 Needs_Box : Boolean := False;
4059 procedure Process_Component (Comp : Entity_Id);
4060 -- Add one component with a box association to the
4061 -- inner aggregate, and recurse if component is
4062 -- itself composite.
4064 ------------------------
4065 -- Process_Component --
4066 ------------------------
4068 procedure Process_Component (Comp : Entity_Id) is
4069 T : constant Entity_Id := Etype (Comp);
4073 if Is_Record_Type (T)
4074 and then Has_Discriminants (T)
4077 Make_Aggregate (Loc, New_List, New_List);
4078 Set_Etype (New_Aggr, T);
4081 Component_Associations (Aggr));
4083 -- Collect discriminant values and recurse
4085 Add_Discriminant_Values
4086 (New_Aggr, Assoc_List);
4087 Propagate_Discriminants
4088 (New_Aggr, Assoc_List);
4093 end Process_Component;
4095 -- Start of processing for Propagate_Discriminants
4098 -- The component type may be a variant type, so
4099 -- collect the components that are ruled by the
4100 -- known values of the discriminants. Their values
4101 -- have already been inserted into the component
4102 -- list of the current aggregate.
4104 if Nkind (Def_Node) = N_Record_Definition
4106 Present (Component_List (Def_Node))
4109 (Variant_Part (Component_List (Def_Node)))
4111 Gather_Components (Aggr_Type,
4112 Component_List (Def_Node),
4113 Governed_By => Component_Associations (Aggr),
4115 Report_Errors => Errors);
4117 Comp_Elmt := First_Elmt (Components);
4118 while Present (Comp_Elmt) loop
4120 Ekind (Node (Comp_Elmt)) /= E_Discriminant
4122 Process_Component (Node (Comp_Elmt));
4125 Next_Elmt (Comp_Elmt);
4128 -- No variant part, iterate over all components
4131 Comp := First_Component (Etype (Aggr));
4132 while Present (Comp) loop
4133 Process_Component (Comp);
4134 Next_Component (Comp);
4140 (Make_Component_Association (Loc,
4142 New_List (Make_Others_Choice (Loc)),
4143 Expression => Empty,
4144 Box_Present => True),
4145 Component_Associations (Aggr));
4147 end Propagate_Discriminants;
4149 -- Start of processing for Capture_Discriminants
4152 Expr := Make_Aggregate (Loc, New_List, New_List);
4153 Set_Etype (Expr, Ctyp);
4155 -- If the enclosing type has discriminants, they have
4156 -- been collected in the aggregate earlier, and they
4157 -- may appear as constraints of subcomponents.
4159 -- Similarly if this component has discriminants, they
4160 -- might in turn be propagated to their components.
4162 if Has_Discriminants (Typ) then
4163 Add_Discriminant_Values (Expr, New_Assoc_List);
4164 Propagate_Discriminants (Expr, New_Assoc_List);
4166 elsif Has_Discriminants (Ctyp) then
4167 Add_Discriminant_Values
4168 (Expr, Component_Associations (Expr));
4169 Propagate_Discriminants
4170 (Expr, Component_Associations (Expr));
4177 -- If the type has additional components, create
4178 -- an OTHERS box association for them.
4180 Comp := First_Component (Ctyp);
4181 while Present (Comp) loop
4182 if Ekind (Comp) = E_Component then
4183 if not Is_Record_Type (Etype (Comp)) then
4185 (Make_Component_Association (Loc,
4188 (Make_Others_Choice (Loc)),
4189 Expression => Empty,
4190 Box_Present => True),
4191 Component_Associations (Expr));
4196 Next_Component (Comp);
4202 (Component => Component,
4204 Assoc_List => New_Assoc_List);
4205 end Capture_Discriminants;
4209 (Component => Component,
4211 Assoc_List => New_Assoc_List,
4212 Is_Box_Present => True);
4215 -- Otherwise we only need to resolve the expression if the
4216 -- component has partially initialized values (required to
4217 -- expand the corresponding assignments and run-time checks).
4219 elsif Present (Expr)
4220 and then Is_Partially_Initialized_Type (Ctyp)
4222 Resolve_Aggr_Expr (Expr, Component);
4224 end Check_Box_Component;
4226 elsif No (Expr) then
4228 -- Ignore hidden components associated with the position of the
4229 -- interface tags: these are initialized dynamically.
4231 if not Present (Related_Type (Component)) then
4233 ("no value supplied for component &!", N, Component);
4237 Resolve_Aggr_Expr (Expr, Component);
4240 Next_Elmt (Component_Elmt);
4243 -- STEP 7: check for invalid components + check type in choice list
4250 -- Type of first component in choice list
4253 if Present (Component_Associations (N)) then
4254 Assoc := First (Component_Associations (N));
4259 Verification : while Present (Assoc) loop
4260 Selectr := First (Choices (Assoc));
4263 if Nkind (Selectr) = N_Others_Choice then
4265 -- Ada 2005 (AI-287): others choice may have expression or box
4267 if No (Others_Etype)
4268 and then not Others_Box
4271 ("OTHERS must represent at least one component", Selectr);
4277 while Present (Selectr) loop
4278 New_Assoc := First (New_Assoc_List);
4279 while Present (New_Assoc) loop
4280 Component := First (Choices (New_Assoc));
4282 if Chars (Selectr) = Chars (Component) then
4284 Check_Identifier (Selectr, Entity (Component));
4293 -- If no association, this is not a legal component of
4294 -- of the type in question, except if its association
4295 -- is provided with a box.
4297 if No (New_Assoc) then
4298 if Box_Present (Parent (Selectr)) then
4300 -- This may still be a bogus component with a box. Scan
4301 -- list of components to verify that a component with
4302 -- that name exists.
4308 C := First_Component (Typ);
4309 while Present (C) loop
4310 if Chars (C) = Chars (Selectr) then
4312 -- If the context is an extension aggregate,
4313 -- the component must not be inherited from
4314 -- the ancestor part of the aggregate.
4316 if Nkind (N) /= N_Extension_Aggregate
4318 Scope (Original_Record_Component (C)) /=
4319 Etype (Ancestor_Part (N))
4329 Error_Msg_Node_2 := Typ;
4330 Error_Msg_N ("& is not a component of}", Selectr);
4334 elsif Chars (Selectr) /= Name_uTag
4335 and then Chars (Selectr) /= Name_uParent
4337 if not Has_Discriminants (Typ) then
4338 Error_Msg_Node_2 := Typ;
4339 Error_Msg_N ("& is not a component of}", Selectr);
4342 ("& is not a component of the aggregate subtype",
4346 Check_Misspelled_Component (Components, Selectr);
4349 elsif No (Typech) then
4350 Typech := Base_Type (Etype (Component));
4352 -- AI05-0199: In Ada 2012, several components of anonymous
4353 -- access types can appear in a choice list, as long as the
4354 -- designated types match.
4356 elsif Typech /= Base_Type (Etype (Component)) then
4357 if Ada_Version >= Ada_2012
4358 and then Ekind (Typech) = E_Anonymous_Access_Type
4360 Ekind (Etype (Component)) = E_Anonymous_Access_Type
4361 and then Base_Type (Designated_Type (Typech)) =
4362 Base_Type (Designated_Type (Etype (Component)))
4364 Subtypes_Statically_Match (Typech, (Etype (Component)))
4368 elsif not Box_Present (Parent (Selectr)) then
4370 ("components in choice list must have same type",
4379 end loop Verification;
4382 -- STEP 8: replace the original aggregate
4385 New_Aggregate : constant Node_Id := New_Copy (N);
4388 Set_Expressions (New_Aggregate, No_List);
4389 Set_Etype (New_Aggregate, Etype (N));
4390 Set_Component_Associations (New_Aggregate, New_Assoc_List);
4392 Rewrite (N, New_Aggregate);
4394 end Resolve_Record_Aggregate;
4396 -----------------------------
4397 -- Check_Can_Never_Be_Null --
4398 -----------------------------
4400 procedure Check_Can_Never_Be_Null (Typ : Entity_Id; Expr : Node_Id) is
4401 Comp_Typ : Entity_Id;
4405 (Ada_Version >= Ada_2005
4406 and then Present (Expr)
4407 and then Known_Null (Expr));
4410 when E_Array_Type =>
4411 Comp_Typ := Component_Type (Typ);
4415 Comp_Typ := Etype (Typ);
4421 if Can_Never_Be_Null (Comp_Typ) then
4423 -- Here we know we have a constraint error. Note that we do not use
4424 -- Apply_Compile_Time_Constraint_Error here to the Expr, which might
4425 -- seem the more natural approach. That's because in some cases the
4426 -- components are rewritten, and the replacement would be missed.
4429 (Compile_Time_Constraint_Error
4431 "(Ada 2005) null not allowed in null-excluding component?"),
4432 Make_Raise_Constraint_Error (Sloc (Expr),
4433 Reason => CE_Access_Check_Failed));
4435 -- Set proper type for bogus component (why is this needed???)
4437 Set_Etype (Expr, Comp_Typ);
4438 Set_Analyzed (Expr);
4440 end Check_Can_Never_Be_Null;
4442 ---------------------
4443 -- Sort_Case_Table --
4444 ---------------------
4446 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is
4447 L : constant Int := Case_Table'First;
4448 U : constant Int := Case_Table'Last;
4456 T := Case_Table (K + 1);
4460 and then Expr_Value (Case_Table (J - 1).Choice_Lo) >
4461 Expr_Value (T.Choice_Lo)
4463 Case_Table (J) := Case_Table (J - 1);
4467 Case_Table (J) := T;
4470 end Sort_Case_Table;