1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2010, 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;
44 with Sem_Aux; use Sem_Aux;
45 with Sem_Cat; use Sem_Cat;
46 with Sem_Ch3; use Sem_Ch3;
47 with Sem_Ch13; use Sem_Ch13;
48 with Sem_Eval; use Sem_Eval;
49 with Sem_Res; use Sem_Res;
50 with Sem_Util; use Sem_Util;
51 with Sem_Type; use Sem_Type;
52 with Sem_Warn; use Sem_Warn;
53 with Sinfo; use Sinfo;
54 with Snames; use Snames;
55 with Stringt; use Stringt;
56 with Stand; use Stand;
57 with Style; use Style;
58 with Targparm; use Targparm;
59 with Tbuild; use Tbuild;
60 with Uintp; use Uintp;
62 package body Sem_Aggr is
64 type Case_Bounds is record
67 Choice_Node : Node_Id;
70 type Case_Table_Type is array (Nat range <>) of Case_Bounds;
71 -- Table type used by Check_Case_Choices procedure
73 -----------------------
74 -- Local Subprograms --
75 -----------------------
77 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type);
78 -- Sort the Case Table using the Lower Bound of each Choice as the key.
79 -- A simple insertion sort is used since the number of choices in a case
80 -- statement of variant part will usually be small and probably in near
83 procedure Check_Can_Never_Be_Null (Typ : Entity_Id; Expr : Node_Id);
84 -- Ada 2005 (AI-231): Check bad usage of null for a component for which
85 -- null exclusion (NOT NULL) is specified. Typ can be an E_Array_Type for
86 -- the array case (the component type of the array will be used) or an
87 -- E_Component/E_Discriminant entity in the record case, in which case the
88 -- type of the component will be used for the test. If Typ is any other
89 -- kind of entity, the call is ignored. Expr is the component node in the
90 -- aggregate which is known to have a null value. A warning message will be
91 -- issued if the component is null excluding.
93 -- It would be better to pass the proper type for Typ ???
95 procedure Check_Expr_OK_In_Limited_Aggregate (Expr : Node_Id);
96 -- Check that Expr is either not limited or else is one of the cases of
97 -- expressions allowed for a limited component association (namely, an
98 -- aggregate, function call, or <> notation). Report error for violations.
100 ------------------------------------------------------
101 -- Subprograms used for RECORD AGGREGATE Processing --
102 ------------------------------------------------------
104 procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id);
105 -- This procedure performs all the semantic checks required for record
106 -- aggregates. Note that for aggregates analysis and resolution go
107 -- hand in hand. Aggregate analysis has been delayed up to here and
108 -- it is done while resolving the aggregate.
110 -- N is the N_Aggregate node.
111 -- Typ is the record type for the aggregate resolution
113 -- While performing the semantic checks, this procedure builds a new
114 -- Component_Association_List where each record field appears alone in a
115 -- Component_Choice_List along with its corresponding expression. The
116 -- record fields in the Component_Association_List appear in the same order
117 -- in which they appear in the record type Typ.
119 -- Once this new Component_Association_List is built and all the semantic
120 -- checks performed, the original aggregate subtree is replaced with the
121 -- new named record aggregate just built. Note that subtree substitution is
122 -- performed with Rewrite so as to be able to retrieve the original
125 -- The aggregate subtree manipulation performed by Resolve_Record_Aggregate
126 -- yields the aggregate format expected by Gigi. Typically, this kind of
127 -- tree manipulations are done in the expander. However, because the
128 -- semantic checks that need to be performed on record aggregates really go
129 -- hand in hand with the record aggregate normalization, the aggregate
130 -- subtree transformation is performed during resolution rather than
131 -- expansion. Had we decided otherwise we would have had to duplicate most
132 -- of the code in the expansion procedure Expand_Record_Aggregate. Note,
133 -- however, that all the expansion concerning aggregates for tagged records
134 -- is done in Expand_Record_Aggregate.
136 -- The algorithm of Resolve_Record_Aggregate proceeds as follows:
138 -- 1. Make sure that the record type against which the record aggregate
139 -- has to be resolved is not abstract. Furthermore if the type is a
140 -- null aggregate make sure the input aggregate N is also null.
142 -- 2. Verify that the structure of the aggregate is that of a record
143 -- aggregate. Specifically, look for component associations and ensure
144 -- that each choice list only has identifiers or the N_Others_Choice
145 -- node. Also make sure that if present, the N_Others_Choice occurs
146 -- last and by itself.
148 -- 3. If Typ contains discriminants, the values for each discriminant is
149 -- looked for. If the record type Typ has variants, we check that the
150 -- expressions corresponding to each discriminant ruling the (possibly
151 -- nested) variant parts of Typ, are static. This allows us to determine
152 -- the variant parts to which the rest of the aggregate must conform.
153 -- The names of discriminants with their values are saved in a new
154 -- association list, New_Assoc_List which is later augmented with the
155 -- names and values of the remaining components in the record type.
157 -- During this phase we also make sure that every discriminant is
158 -- assigned exactly one value. Note that when several values for a given
159 -- discriminant are found, semantic processing continues looking for
160 -- further errors. In this case it's the first discriminant value found
161 -- which we will be recorded.
163 -- IMPORTANT NOTE: For derived tagged types this procedure expects
164 -- First_Discriminant and Next_Discriminant to give the correct list
165 -- of discriminants, in the correct order.
167 -- 4. After all the discriminant values have been gathered, we can set the
168 -- Etype of the record aggregate. If Typ contains no discriminants this
169 -- is straightforward: the Etype of N is just Typ, otherwise a new
170 -- implicit constrained subtype of Typ is built to be the Etype of N.
172 -- 5. Gather the remaining record components according to the discriminant
173 -- values. This involves recursively traversing the record type
174 -- structure to see what variants are selected by the given discriminant
175 -- values. This processing is a little more convoluted if Typ is a
176 -- derived tagged types since we need to retrieve the record structure
177 -- of all the ancestors of Typ.
179 -- 6. After gathering the record components we look for their values in the
180 -- record aggregate and emit appropriate error messages should we not
181 -- find such values or should they be duplicated.
183 -- 7. We then make sure no illegal component names appear in the record
184 -- aggregate and make sure that the type of the record components
185 -- appearing in a same choice list is the same. Finally we ensure that
186 -- the others choice, if present, is used to provide the value of at
187 -- least a record component.
189 -- 8. The original aggregate node is replaced with the new named aggregate
190 -- built in steps 3 through 6, as explained earlier.
192 -- Given the complexity of record aggregate resolution, the primary goal of
193 -- this routine is clarity and simplicity rather than execution and storage
194 -- efficiency. If there are only positional components in the aggregate the
195 -- running time is linear. If there are associations the running time is
196 -- still linear as long as the order of the associations is not too far off
197 -- the order of the components in the record type. If this is not the case
198 -- the running time is at worst quadratic in the size of the association
201 procedure Check_Misspelled_Component
202 (Elements : Elist_Id;
203 Component : Node_Id);
204 -- Give possible misspelling diagnostic if Component is likely to be a
205 -- misspelling of one of the components of the Assoc_List. This is called
206 -- by Resolve_Aggr_Expr after producing an invalid component error message.
208 procedure Check_Static_Discriminated_Subtype (T : Entity_Id; V : Node_Id);
209 -- An optimization: determine whether a discriminated subtype has a static
210 -- constraint, and contains array components whose length is also static,
211 -- either because they are constrained by the discriminant, or because the
212 -- original component bounds are static.
214 -----------------------------------------------------
215 -- Subprograms used for ARRAY AGGREGATE Processing --
216 -----------------------------------------------------
218 function Resolve_Array_Aggregate
221 Index_Constr : Node_Id;
222 Component_Typ : Entity_Id;
223 Others_Allowed : Boolean) return Boolean;
224 -- This procedure performs the semantic checks for an array aggregate.
225 -- True is returned if the aggregate resolution succeeds.
227 -- The procedure works by recursively checking each nested aggregate.
228 -- Specifically, after checking a sub-aggregate nested at the i-th level
229 -- we recursively check all the subaggregates at the i+1-st level (if any).
230 -- Note that for aggregates analysis and resolution go hand in hand.
231 -- Aggregate analysis has been delayed up to here and it is done while
232 -- resolving the aggregate.
234 -- N is the current N_Aggregate node to be checked.
236 -- Index is the index node corresponding to the array sub-aggregate that
237 -- we are currently checking (RM 4.3.3 (8)). Its Etype is the
238 -- corresponding index type (or subtype).
240 -- Index_Constr is the node giving the applicable index constraint if
241 -- any (RM 4.3.3 (10)). It "is a constraint provided by certain
242 -- contexts [...] that can be used to determine the bounds of the array
243 -- value specified by the aggregate". If Others_Allowed below is False
244 -- there is no applicable index constraint and this node is set to Index.
246 -- Component_Typ is the array component type.
248 -- Others_Allowed indicates whether an others choice is allowed
249 -- in the context where the top-level aggregate appeared.
251 -- The algorithm of Resolve_Array_Aggregate proceeds as follows:
253 -- 1. Make sure that the others choice, if present, is by itself and
254 -- appears last in the sub-aggregate. Check that we do not have
255 -- positional and named components in the array sub-aggregate (unless
256 -- the named association is an others choice). Finally if an others
257 -- choice is present, make sure it is allowed in the aggregate context.
259 -- 2. If the array sub-aggregate contains discrete_choices:
261 -- (A) Verify their validity. Specifically verify that:
263 -- (a) If a null range is present it must be the only possible
264 -- choice in the array aggregate.
266 -- (b) Ditto for a non static range.
268 -- (c) Ditto for a non static expression.
270 -- In addition this step analyzes and resolves each discrete_choice,
271 -- making sure that its type is the type of the corresponding Index.
272 -- If we are not at the lowest array aggregate level (in the case of
273 -- multi-dimensional aggregates) then invoke Resolve_Array_Aggregate
274 -- recursively on each component expression. Otherwise, resolve the
275 -- bottom level component expressions against the expected component
276 -- type ONLY IF the component corresponds to a single discrete choice
277 -- which is not an others choice (to see why read the DELAYED
278 -- COMPONENT RESOLUTION below).
280 -- (B) Determine the bounds of the sub-aggregate and lowest and
281 -- highest choice values.
283 -- 3. For positional aggregates:
285 -- (A) Loop over the component expressions either recursively invoking
286 -- Resolve_Array_Aggregate on each of these for multi-dimensional
287 -- array aggregates or resolving the bottom level component
288 -- expressions against the expected component type.
290 -- (B) Determine the bounds of the positional sub-aggregates.
292 -- 4. Try to determine statically whether the evaluation of the array
293 -- sub-aggregate raises Constraint_Error. If yes emit proper
294 -- warnings. The precise checks are the following:
296 -- (A) Check that the index range defined by aggregate bounds is
297 -- compatible with corresponding index subtype.
298 -- We also check against the base type. In fact it could be that
299 -- Low/High bounds of the base type are static whereas those of
300 -- the index subtype are not. Thus if we can statically catch
301 -- a problem with respect to the base type we are guaranteed
302 -- that the same problem will arise with the index subtype
304 -- (B) If we are dealing with a named aggregate containing an others
305 -- choice and at least one discrete choice then make sure the range
306 -- specified by the discrete choices does not overflow the
307 -- aggregate bounds. We also check against the index type and base
308 -- type bounds for the same reasons given in (A).
310 -- (C) If we are dealing with a positional aggregate with an others
311 -- choice make sure the number of positional elements specified
312 -- does not overflow the aggregate bounds. We also check against
313 -- the index type and base type bounds as mentioned in (A).
315 -- Finally construct an N_Range node giving the sub-aggregate bounds.
316 -- Set the Aggregate_Bounds field of the sub-aggregate to be this
317 -- N_Range. The routine Array_Aggr_Subtype below uses such N_Ranges
318 -- to build the appropriate aggregate subtype. Aggregate_Bounds
319 -- information is needed during expansion.
321 -- DELAYED COMPONENT RESOLUTION: The resolution of bottom level component
322 -- expressions in an array aggregate may call Duplicate_Subexpr or some
323 -- other routine that inserts code just outside the outermost aggregate.
324 -- If the array aggregate contains discrete choices or an others choice,
325 -- this may be wrong. Consider for instance the following example.
327 -- type Rec is record
331 -- type Acc_Rec is access Rec;
332 -- Arr : array (1..3) of Acc_Rec := (1 .. 3 => new Rec);
334 -- Then the transformation of "new Rec" that occurs during resolution
335 -- entails the following code modifications
337 -- P7b : constant Acc_Rec := new Rec;
339 -- Arr : array (1..3) of Acc_Rec := (1 .. 3 => P7b);
341 -- This code transformation is clearly wrong, since we need to call
342 -- "new Rec" for each of the 3 array elements. To avoid this problem we
343 -- delay resolution of the components of non positional array aggregates
344 -- to the expansion phase. As an optimization, if the discrete choice
345 -- specifies a single value we do not delay resolution.
347 function Array_Aggr_Subtype (N : Node_Id; Typ : Node_Id) return Entity_Id;
348 -- This routine returns the type or subtype of an array aggregate.
350 -- N is the array aggregate node whose type we return.
352 -- Typ is the context type in which N occurs.
354 -- This routine creates an implicit array subtype whose bounds are
355 -- those defined by the aggregate. When this routine is invoked
356 -- Resolve_Array_Aggregate has already processed aggregate N. Thus the
357 -- Aggregate_Bounds of each sub-aggregate, is an N_Range node giving the
358 -- sub-aggregate bounds. When building the aggregate itype, this function
359 -- traverses the array aggregate N collecting such Aggregate_Bounds and
360 -- constructs the proper array aggregate itype.
362 -- Note that in the case of multidimensional aggregates each inner
363 -- sub-aggregate corresponding to a given array dimension, may provide a
364 -- different bounds. If it is possible to determine statically that
365 -- some sub-aggregates corresponding to the same index do not have the
366 -- same bounds, then a warning is emitted. If such check is not possible
367 -- statically (because some sub-aggregate bounds are dynamic expressions)
368 -- then this job is left to the expander. In all cases the particular
369 -- bounds that this function will chose for a given dimension is the first
370 -- N_Range node for a sub-aggregate corresponding to that dimension.
372 -- Note that the Raises_Constraint_Error flag of an array aggregate
373 -- whose evaluation is determined to raise CE by Resolve_Array_Aggregate,
374 -- is set in Resolve_Array_Aggregate but the aggregate is not
375 -- immediately replaced with a raise CE. In fact, Array_Aggr_Subtype must
376 -- first construct the proper itype for the aggregate (Gigi needs
377 -- this). After constructing the proper itype we will eventually replace
378 -- the top-level aggregate with a raise CE (done in Resolve_Aggregate).
379 -- Of course in cases such as:
381 -- type Arr is array (integer range <>) of Integer;
382 -- A : Arr := (positive range -1 .. 2 => 0);
384 -- The bounds of the aggregate itype are cooked up to look reasonable
385 -- (in this particular case the bounds will be 1 .. 2).
387 procedure Aggregate_Constraint_Checks
389 Check_Typ : Entity_Id);
390 -- Checks expression Exp against subtype Check_Typ. If Exp is an
391 -- aggregate and Check_Typ a constrained record type with discriminants,
392 -- we generate the appropriate discriminant checks. If Exp is an array
393 -- aggregate then emit the appropriate length checks. If Exp is a scalar
394 -- type, or a string literal, Exp is changed into Check_Typ'(Exp) to
395 -- ensure that range checks are performed at run time.
397 procedure Make_String_Into_Aggregate (N : Node_Id);
398 -- A string literal can appear in a context in which a one dimensional
399 -- array of characters is expected. This procedure simply rewrites the
400 -- string as an aggregate, prior to resolution.
402 ---------------------------------
403 -- Aggregate_Constraint_Checks --
404 ---------------------------------
406 procedure Aggregate_Constraint_Checks
408 Check_Typ : Entity_Id)
410 Exp_Typ : constant Entity_Id := Etype (Exp);
413 if Raises_Constraint_Error (Exp) then
417 -- Ada 2005 (AI-230): Generate a conversion to an anonymous access
418 -- component's type to force the appropriate accessibility checks.
420 -- Ada 2005 (AI-231): Generate conversion to the null-excluding
421 -- type to force the corresponding run-time check
423 if Is_Access_Type (Check_Typ)
424 and then ((Is_Local_Anonymous_Access (Check_Typ))
425 or else (Can_Never_Be_Null (Check_Typ)
426 and then not Can_Never_Be_Null (Exp_Typ)))
428 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
429 Analyze_And_Resolve (Exp, Check_Typ);
430 Check_Unset_Reference (Exp);
433 -- This is really expansion activity, so make sure that expansion
434 -- is on and is allowed.
436 if not Expander_Active or else In_Spec_Expression then
440 -- First check if we have to insert discriminant checks
442 if Has_Discriminants (Exp_Typ) then
443 Apply_Discriminant_Check (Exp, Check_Typ);
445 -- Next emit length checks for array aggregates
447 elsif Is_Array_Type (Exp_Typ) then
448 Apply_Length_Check (Exp, Check_Typ);
450 -- Finally emit scalar and string checks. If we are dealing with a
451 -- scalar literal we need to check by hand because the Etype of
452 -- literals is not necessarily correct.
454 elsif Is_Scalar_Type (Exp_Typ)
455 and then Compile_Time_Known_Value (Exp)
457 if Is_Out_Of_Range (Exp, Base_Type (Check_Typ)) then
458 Apply_Compile_Time_Constraint_Error
459 (Exp, "value not in range of}?", CE_Range_Check_Failed,
460 Ent => Base_Type (Check_Typ),
461 Typ => Base_Type (Check_Typ));
463 elsif Is_Out_Of_Range (Exp, Check_Typ) then
464 Apply_Compile_Time_Constraint_Error
465 (Exp, "value not in range of}?", CE_Range_Check_Failed,
469 elsif not Range_Checks_Suppressed (Check_Typ) then
470 Apply_Scalar_Range_Check (Exp, Check_Typ);
473 -- Verify that target type is also scalar, to prevent view anomalies
474 -- in instantiations.
476 elsif (Is_Scalar_Type (Exp_Typ)
477 or else Nkind (Exp) = N_String_Literal)
478 and then Is_Scalar_Type (Check_Typ)
479 and then Exp_Typ /= Check_Typ
481 if Is_Entity_Name (Exp)
482 and then Ekind (Entity (Exp)) = E_Constant
484 -- If expression is a constant, it is worthwhile checking whether
485 -- it is a bound of the type.
487 if (Is_Entity_Name (Type_Low_Bound (Check_Typ))
488 and then Entity (Exp) = Entity (Type_Low_Bound (Check_Typ)))
489 or else (Is_Entity_Name (Type_High_Bound (Check_Typ))
490 and then Entity (Exp) = Entity (Type_High_Bound (Check_Typ)))
495 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
496 Analyze_And_Resolve (Exp, Check_Typ);
497 Check_Unset_Reference (Exp);
500 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
501 Analyze_And_Resolve (Exp, Check_Typ);
502 Check_Unset_Reference (Exp);
506 end Aggregate_Constraint_Checks;
508 ------------------------
509 -- Array_Aggr_Subtype --
510 ------------------------
512 function Array_Aggr_Subtype
514 Typ : Entity_Id) return Entity_Id
516 Aggr_Dimension : constant Pos := Number_Dimensions (Typ);
517 -- Number of aggregate index dimensions
519 Aggr_Range : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
520 -- Constrained N_Range of each index dimension in our aggregate itype
522 Aggr_Low : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
523 Aggr_High : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
524 -- Low and High bounds for each index dimension in our aggregate itype
526 Is_Fully_Positional : Boolean := True;
528 procedure Collect_Aggr_Bounds (N : Node_Id; Dim : Pos);
529 -- N is an array (sub-)aggregate. Dim is the dimension corresponding to
530 -- (sub-)aggregate N. This procedure collects the constrained N_Range
531 -- nodes corresponding to each index dimension of our aggregate itype.
532 -- These N_Range nodes are collected in Aggr_Range above.
534 -- Likewise collect in Aggr_Low & Aggr_High above the low and high
535 -- bounds of each index dimension. If, when collecting, two bounds
536 -- corresponding to the same dimension are static and found to differ,
537 -- then emit a warning, and mark N as raising Constraint_Error.
539 -------------------------
540 -- Collect_Aggr_Bounds --
541 -------------------------
543 procedure Collect_Aggr_Bounds (N : Node_Id; Dim : Pos) is
544 This_Range : constant Node_Id := Aggregate_Bounds (N);
545 -- The aggregate range node of this specific sub-aggregate
547 This_Low : constant Node_Id := Low_Bound (Aggregate_Bounds (N));
548 This_High : constant Node_Id := High_Bound (Aggregate_Bounds (N));
549 -- The aggregate bounds of this specific sub-aggregate
555 -- Collect the first N_Range for a given dimension that you find.
556 -- For a given dimension they must be all equal anyway.
558 if No (Aggr_Range (Dim)) then
559 Aggr_Low (Dim) := This_Low;
560 Aggr_High (Dim) := This_High;
561 Aggr_Range (Dim) := This_Range;
564 if Compile_Time_Known_Value (This_Low) then
565 if not Compile_Time_Known_Value (Aggr_Low (Dim)) then
566 Aggr_Low (Dim) := This_Low;
568 elsif Expr_Value (This_Low) /= Expr_Value (Aggr_Low (Dim)) then
569 Set_Raises_Constraint_Error (N);
570 Error_Msg_N ("sub-aggregate low bound mismatch?", N);
572 ("\Constraint_Error will be raised at run time?", N);
576 if Compile_Time_Known_Value (This_High) then
577 if not Compile_Time_Known_Value (Aggr_High (Dim)) then
578 Aggr_High (Dim) := This_High;
581 Expr_Value (This_High) /= Expr_Value (Aggr_High (Dim))
583 Set_Raises_Constraint_Error (N);
584 Error_Msg_N ("sub-aggregate high bound mismatch?", N);
586 ("\Constraint_Error will be raised at run time?", N);
591 if Dim < Aggr_Dimension then
593 -- Process positional components
595 if Present (Expressions (N)) then
596 Expr := First (Expressions (N));
597 while Present (Expr) loop
598 Collect_Aggr_Bounds (Expr, Dim + 1);
603 -- Process component associations
605 if Present (Component_Associations (N)) then
606 Is_Fully_Positional := False;
608 Assoc := First (Component_Associations (N));
609 while Present (Assoc) loop
610 Expr := Expression (Assoc);
611 Collect_Aggr_Bounds (Expr, Dim + 1);
616 end Collect_Aggr_Bounds;
618 -- Array_Aggr_Subtype variables
621 -- The final itype of the overall aggregate
623 Index_Constraints : constant List_Id := New_List;
624 -- The list of index constraints of the aggregate itype
626 -- Start of processing for Array_Aggr_Subtype
629 -- Make sure that the list of index constraints is properly attached to
630 -- the tree, and then collect the aggregate bounds.
632 Set_Parent (Index_Constraints, N);
633 Collect_Aggr_Bounds (N, 1);
635 -- Build the list of constrained indices of our aggregate itype
637 for J in 1 .. Aggr_Dimension loop
638 Create_Index : declare
639 Index_Base : constant Entity_Id :=
640 Base_Type (Etype (Aggr_Range (J)));
641 Index_Typ : Entity_Id;
644 -- Construct the Index subtype, and associate it with the range
645 -- construct that generates it.
648 Create_Itype (Subtype_Kind (Ekind (Index_Base)), Aggr_Range (J));
650 Set_Etype (Index_Typ, Index_Base);
652 if Is_Character_Type (Index_Base) then
653 Set_Is_Character_Type (Index_Typ);
656 Set_Size_Info (Index_Typ, (Index_Base));
657 Set_RM_Size (Index_Typ, RM_Size (Index_Base));
658 Set_First_Rep_Item (Index_Typ, First_Rep_Item (Index_Base));
659 Set_Scalar_Range (Index_Typ, Aggr_Range (J));
661 if Is_Discrete_Or_Fixed_Point_Type (Index_Typ) then
662 Set_RM_Size (Index_Typ, UI_From_Int (Minimum_Size (Index_Typ)));
665 Set_Etype (Aggr_Range (J), Index_Typ);
667 Append (Aggr_Range (J), To => Index_Constraints);
671 -- Now build the Itype
673 Itype := Create_Itype (E_Array_Subtype, N);
675 Set_First_Rep_Item (Itype, First_Rep_Item (Typ));
676 Set_Convention (Itype, Convention (Typ));
677 Set_Depends_On_Private (Itype, Has_Private_Component (Typ));
678 Set_Etype (Itype, Base_Type (Typ));
679 Set_Has_Alignment_Clause (Itype, Has_Alignment_Clause (Typ));
680 Set_Is_Aliased (Itype, Is_Aliased (Typ));
681 Set_Depends_On_Private (Itype, Depends_On_Private (Typ));
683 Copy_Suppress_Status (Index_Check, Typ, Itype);
684 Copy_Suppress_Status (Length_Check, Typ, Itype);
686 Set_First_Index (Itype, First (Index_Constraints));
687 Set_Is_Constrained (Itype, True);
688 Set_Is_Internal (Itype, True);
690 -- A simple optimization: purely positional aggregates of static
691 -- components should be passed to gigi unexpanded whenever possible, and
692 -- regardless of the staticness of the bounds themselves. Subsequent
693 -- checks in exp_aggr verify that type is not packed, etc.
695 Set_Size_Known_At_Compile_Time (Itype,
697 and then Comes_From_Source (N)
698 and then Size_Known_At_Compile_Time (Component_Type (Typ)));
700 -- We always need a freeze node for a packed array subtype, so that we
701 -- can build the Packed_Array_Type corresponding to the subtype. If
702 -- expansion is disabled, the packed array subtype is not built, and we
703 -- must not generate a freeze node for the type, or else it will appear
704 -- incomplete to gigi.
707 and then not In_Spec_Expression
708 and then Expander_Active
710 Freeze_Itype (Itype, N);
714 end Array_Aggr_Subtype;
716 --------------------------------
717 -- Check_Misspelled_Component --
718 --------------------------------
720 procedure Check_Misspelled_Component
721 (Elements : Elist_Id;
724 Max_Suggestions : constant := 2;
726 Nr_Of_Suggestions : Natural := 0;
727 Suggestion_1 : Entity_Id := Empty;
728 Suggestion_2 : Entity_Id := Empty;
729 Component_Elmt : Elmt_Id;
732 -- All the components of List are matched against Component and a count
733 -- is maintained of possible misspellings. When at the end of the
734 -- the analysis there are one or two (not more!) possible misspellings,
735 -- these misspellings will be suggested as possible correction.
737 Component_Elmt := First_Elmt (Elements);
738 while Nr_Of_Suggestions <= Max_Suggestions
739 and then Present (Component_Elmt)
741 if Is_Bad_Spelling_Of
742 (Chars (Node (Component_Elmt)),
745 Nr_Of_Suggestions := Nr_Of_Suggestions + 1;
747 case Nr_Of_Suggestions is
748 when 1 => Suggestion_1 := Node (Component_Elmt);
749 when 2 => Suggestion_2 := Node (Component_Elmt);
754 Next_Elmt (Component_Elmt);
757 -- Report at most two suggestions
759 if Nr_Of_Suggestions = 1 then
760 Error_Msg_NE -- CODEFIX
761 ("\possible misspelling of&", Component, Suggestion_1);
763 elsif Nr_Of_Suggestions = 2 then
764 Error_Msg_Node_2 := Suggestion_2;
765 Error_Msg_NE -- CODEFIX
766 ("\possible misspelling of& or&", Component, Suggestion_1);
768 end Check_Misspelled_Component;
770 ----------------------------------------
771 -- Check_Expr_OK_In_Limited_Aggregate --
772 ----------------------------------------
774 procedure Check_Expr_OK_In_Limited_Aggregate (Expr : Node_Id) is
776 if Is_Limited_Type (Etype (Expr))
777 and then Comes_From_Source (Expr)
778 and then not In_Instance_Body
780 if not OK_For_Limited_Init (Etype (Expr), Expr) then
781 Error_Msg_N ("initialization not allowed for limited types", Expr);
782 Explain_Limited_Type (Etype (Expr), Expr);
785 end Check_Expr_OK_In_Limited_Aggregate;
787 ----------------------------------------
788 -- Check_Static_Discriminated_Subtype --
789 ----------------------------------------
791 procedure Check_Static_Discriminated_Subtype (T : Entity_Id; V : Node_Id) is
792 Disc : constant Entity_Id := First_Discriminant (T);
797 if Has_Record_Rep_Clause (T) then
800 elsif Present (Next_Discriminant (Disc)) then
803 elsif Nkind (V) /= N_Integer_Literal then
807 Comp := First_Component (T);
808 while Present (Comp) loop
809 if Is_Scalar_Type (Etype (Comp)) then
812 elsif Is_Private_Type (Etype (Comp))
813 and then Present (Full_View (Etype (Comp)))
814 and then Is_Scalar_Type (Full_View (Etype (Comp)))
818 elsif Is_Array_Type (Etype (Comp)) then
819 if Is_Bit_Packed_Array (Etype (Comp)) then
823 Ind := First_Index (Etype (Comp));
824 while Present (Ind) loop
825 if Nkind (Ind) /= N_Range
826 or else Nkind (Low_Bound (Ind)) /= N_Integer_Literal
827 or else Nkind (High_Bound (Ind)) /= N_Integer_Literal
839 Next_Component (Comp);
842 -- On exit, all components have statically known sizes
844 Set_Size_Known_At_Compile_Time (T);
845 end Check_Static_Discriminated_Subtype;
847 --------------------------------
848 -- Make_String_Into_Aggregate --
849 --------------------------------
851 procedure Make_String_Into_Aggregate (N : Node_Id) is
852 Exprs : constant List_Id := New_List;
853 Loc : constant Source_Ptr := Sloc (N);
854 Str : constant String_Id := Strval (N);
855 Strlen : constant Nat := String_Length (Str);
863 for J in 1 .. Strlen loop
864 C := Get_String_Char (Str, J);
865 Set_Character_Literal_Name (C);
868 Make_Character_Literal (P,
870 Char_Literal_Value => UI_From_CC (C));
871 Set_Etype (C_Node, Any_Character);
872 Append_To (Exprs, C_Node);
875 -- Something special for wide strings???
878 New_N := Make_Aggregate (Loc, Expressions => Exprs);
879 Set_Analyzed (New_N);
880 Set_Etype (New_N, Any_Composite);
883 end Make_String_Into_Aggregate;
885 -----------------------
886 -- Resolve_Aggregate --
887 -----------------------
889 procedure Resolve_Aggregate (N : Node_Id; Typ : Entity_Id) is
890 Pkind : constant Node_Kind := Nkind (Parent (N));
892 Aggr_Subtyp : Entity_Id;
893 -- The actual aggregate subtype. This is not necessarily the same as Typ
894 -- which is the subtype of the context in which the aggregate was found.
897 -- Ignore junk empty aggregate resulting from parser error
899 if No (Expressions (N))
900 and then No (Component_Associations (N))
901 and then not Null_Record_Present (N)
906 -- Check for aggregates not allowed in configurable run-time mode.
907 -- We allow all cases of aggregates that do not come from source, since
908 -- these are all assumed to be small (e.g. bounds of a string literal).
909 -- We also allow aggregates of types we know to be small.
911 if not Support_Aggregates_On_Target
912 and then Comes_From_Source (N)
913 and then (not Known_Static_Esize (Typ) or else Esize (Typ) > 64)
915 Error_Msg_CRT ("aggregate", N);
918 -- Ada 2005 (AI-287): Limited aggregates allowed
920 if Is_Limited_Type (Typ) and then Ada_Version < Ada_05 then
921 Error_Msg_N ("aggregate type cannot be limited", N);
922 Explain_Limited_Type (Typ, N);
924 elsif Is_Class_Wide_Type (Typ) then
925 Error_Msg_N ("type of aggregate cannot be class-wide", N);
927 elsif Typ = Any_String
928 or else Typ = Any_Composite
930 Error_Msg_N ("no unique type for aggregate", N);
931 Set_Etype (N, Any_Composite);
933 elsif Is_Array_Type (Typ) and then Null_Record_Present (N) then
934 Error_Msg_N ("null record forbidden in array aggregate", N);
936 elsif Is_Record_Type (Typ) then
937 Resolve_Record_Aggregate (N, Typ);
939 elsif Is_Array_Type (Typ) then
941 -- First a special test, for the case of a positional aggregate
942 -- of characters which can be replaced by a string literal.
944 -- Do not perform this transformation if this was a string literal to
945 -- start with, whose components needed constraint checks, or if the
946 -- component type is non-static, because it will require those checks
947 -- and be transformed back into an aggregate.
949 if Number_Dimensions (Typ) = 1
950 and then Is_Standard_Character_Type (Component_Type (Typ))
951 and then No (Component_Associations (N))
952 and then not Is_Limited_Composite (Typ)
953 and then not Is_Private_Composite (Typ)
954 and then not Is_Bit_Packed_Array (Typ)
955 and then Nkind (Original_Node (Parent (N))) /= N_String_Literal
956 and then Is_Static_Subtype (Component_Type (Typ))
962 Expr := First (Expressions (N));
963 while Present (Expr) loop
964 exit when Nkind (Expr) /= N_Character_Literal;
971 Expr := First (Expressions (N));
972 while Present (Expr) loop
973 Store_String_Char (UI_To_CC (Char_Literal_Value (Expr)));
978 Make_String_Literal (Sloc (N), End_String));
980 Analyze_And_Resolve (N, Typ);
986 -- Here if we have a real aggregate to deal with
988 Array_Aggregate : declare
989 Aggr_Resolved : Boolean;
991 Aggr_Typ : constant Entity_Id := Etype (Typ);
992 -- This is the unconstrained array type, which is the type against
993 -- which the aggregate is to be resolved. Typ itself is the array
994 -- type of the context which may not be the same subtype as the
995 -- subtype for the final aggregate.
998 -- In the following we determine whether an others choice is
999 -- allowed inside the array aggregate. The test checks the context
1000 -- in which the array aggregate occurs. If the context does not
1001 -- permit it, or the aggregate type is unconstrained, an others
1002 -- choice is not allowed.
1004 -- If expansion is disabled (generic context, or semantics-only
1005 -- mode) actual subtypes cannot be constructed, and the type of an
1006 -- object may be its unconstrained nominal type. However, if the
1007 -- context is an assignment, we assume that "others" is allowed,
1008 -- because the target of the assignment will have a constrained
1009 -- subtype when fully compiled.
1011 -- Note that there is no node for Explicit_Actual_Parameter.
1012 -- To test for this context we therefore have to test for node
1013 -- N_Parameter_Association which itself appears only if there is a
1014 -- formal parameter. Consequently we also need to test for
1015 -- N_Procedure_Call_Statement or N_Function_Call.
1017 Set_Etype (N, Aggr_Typ); -- May be overridden later on
1019 if Is_Constrained (Typ) and then
1020 (Pkind = N_Assignment_Statement or else
1021 Pkind = N_Parameter_Association or else
1022 Pkind = N_Function_Call or else
1023 Pkind = N_Procedure_Call_Statement or else
1024 Pkind = N_Generic_Association or else
1025 Pkind = N_Formal_Object_Declaration or else
1026 Pkind = N_Simple_Return_Statement or else
1027 Pkind = N_Object_Declaration or else
1028 Pkind = N_Component_Declaration or else
1029 Pkind = N_Parameter_Specification or else
1030 Pkind = N_Qualified_Expression or else
1031 Pkind = N_Aggregate or else
1032 Pkind = N_Extension_Aggregate or else
1033 Pkind = N_Component_Association)
1036 Resolve_Array_Aggregate
1038 Index => First_Index (Aggr_Typ),
1039 Index_Constr => First_Index (Typ),
1040 Component_Typ => Component_Type (Typ),
1041 Others_Allowed => True);
1043 elsif not Expander_Active
1044 and then Pkind = N_Assignment_Statement
1047 Resolve_Array_Aggregate
1049 Index => First_Index (Aggr_Typ),
1050 Index_Constr => First_Index (Typ),
1051 Component_Typ => Component_Type (Typ),
1052 Others_Allowed => True);
1055 Resolve_Array_Aggregate
1057 Index => First_Index (Aggr_Typ),
1058 Index_Constr => First_Index (Aggr_Typ),
1059 Component_Typ => Component_Type (Typ),
1060 Others_Allowed => False);
1063 if not Aggr_Resolved then
1064 Aggr_Subtyp := Any_Composite;
1066 Aggr_Subtyp := Array_Aggr_Subtype (N, Typ);
1069 Set_Etype (N, Aggr_Subtyp);
1070 end Array_Aggregate;
1072 elsif Is_Private_Type (Typ)
1073 and then Present (Full_View (Typ))
1074 and then In_Inlined_Body
1075 and then Is_Composite_Type (Full_View (Typ))
1077 Resolve (N, Full_View (Typ));
1080 Error_Msg_N ("illegal context for aggregate", N);
1083 -- If we can determine statically that the evaluation of the aggregate
1084 -- raises Constraint_Error, then replace the aggregate with an
1085 -- N_Raise_Constraint_Error node, but set the Etype to the right
1086 -- aggregate subtype. Gigi needs this.
1088 if Raises_Constraint_Error (N) then
1089 Aggr_Subtyp := Etype (N);
1091 Make_Raise_Constraint_Error (Sloc (N),
1092 Reason => CE_Range_Check_Failed));
1093 Set_Raises_Constraint_Error (N);
1094 Set_Etype (N, Aggr_Subtyp);
1097 end Resolve_Aggregate;
1099 -----------------------------
1100 -- Resolve_Array_Aggregate --
1101 -----------------------------
1103 function Resolve_Array_Aggregate
1106 Index_Constr : Node_Id;
1107 Component_Typ : Entity_Id;
1108 Others_Allowed : Boolean) return Boolean
1110 Loc : constant Source_Ptr := Sloc (N);
1112 Failure : constant Boolean := False;
1113 Success : constant Boolean := True;
1115 Index_Typ : constant Entity_Id := Etype (Index);
1116 Index_Typ_Low : constant Node_Id := Type_Low_Bound (Index_Typ);
1117 Index_Typ_High : constant Node_Id := Type_High_Bound (Index_Typ);
1118 -- The type of the index corresponding to the array sub-aggregate along
1119 -- with its low and upper bounds.
1121 Index_Base : constant Entity_Id := Base_Type (Index_Typ);
1122 Index_Base_Low : constant Node_Id := Type_Low_Bound (Index_Base);
1123 Index_Base_High : constant Node_Id := Type_High_Bound (Index_Base);
1124 -- Ditto for the base type
1126 function Add (Val : Uint; To : Node_Id) return Node_Id;
1127 -- Creates a new expression node where Val is added to expression To.
1128 -- Tries to constant fold whenever possible. To must be an already
1129 -- analyzed expression.
1131 procedure Check_Bound (BH : Node_Id; AH : in out Node_Id);
1132 -- Checks that AH (the upper bound of an array aggregate) is <= BH
1133 -- (the upper bound of the index base type). If the check fails a
1134 -- warning is emitted, the Raises_Constraint_Error flag of N is set,
1135 -- and AH is replaced with a duplicate of BH.
1137 procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id);
1138 -- Checks that range AL .. AH is compatible with range L .. H. Emits a
1139 -- warning if not and sets the Raises_Constraint_Error flag in N.
1141 procedure Check_Length (L, H : Node_Id; Len : Uint);
1142 -- Checks that range L .. H contains at least Len elements. Emits a
1143 -- warning if not and sets the Raises_Constraint_Error flag in N.
1145 function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean;
1146 -- Returns True if range L .. H is dynamic or null
1148 procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean);
1149 -- Given expression node From, this routine sets OK to False if it
1150 -- cannot statically evaluate From. Otherwise it stores this static
1151 -- value into Value.
1153 function Resolve_Aggr_Expr
1155 Single_Elmt : Boolean) return Boolean;
1156 -- Resolves aggregate expression Expr. Returns False if resolution
1157 -- fails. If Single_Elmt is set to False, the expression Expr may be
1158 -- used to initialize several array aggregate elements (this can happen
1159 -- for discrete choices such as "L .. H => Expr" or the others choice).
1160 -- In this event we do not resolve Expr unless expansion is disabled.
1161 -- To know why, see the DELAYED COMPONENT RESOLUTION note above.
1167 function Add (Val : Uint; To : Node_Id) return Node_Id is
1173 if Raises_Constraint_Error (To) then
1177 -- First test if we can do constant folding
1179 if Compile_Time_Known_Value (To)
1180 or else Nkind (To) = N_Integer_Literal
1182 Expr_Pos := Make_Integer_Literal (Loc, Expr_Value (To) + Val);
1183 Set_Is_Static_Expression (Expr_Pos);
1184 Set_Etype (Expr_Pos, Etype (To));
1185 Set_Analyzed (Expr_Pos, Analyzed (To));
1187 if not Is_Enumeration_Type (Index_Typ) then
1190 -- If we are dealing with enumeration return
1191 -- Index_Typ'Val (Expr_Pos)
1195 Make_Attribute_Reference
1197 Prefix => New_Reference_To (Index_Typ, Loc),
1198 Attribute_Name => Name_Val,
1199 Expressions => New_List (Expr_Pos));
1205 -- If we are here no constant folding possible
1207 if not Is_Enumeration_Type (Index_Base) then
1210 Left_Opnd => Duplicate_Subexpr (To),
1211 Right_Opnd => Make_Integer_Literal (Loc, Val));
1213 -- If we are dealing with enumeration return
1214 -- Index_Typ'Val (Index_Typ'Pos (To) + Val)
1218 Make_Attribute_Reference
1220 Prefix => New_Reference_To (Index_Typ, Loc),
1221 Attribute_Name => Name_Pos,
1222 Expressions => New_List (Duplicate_Subexpr (To)));
1226 Left_Opnd => To_Pos,
1227 Right_Opnd => Make_Integer_Literal (Loc, Val));
1230 Make_Attribute_Reference
1232 Prefix => New_Reference_To (Index_Typ, Loc),
1233 Attribute_Name => Name_Val,
1234 Expressions => New_List (Expr_Pos));
1244 procedure Check_Bound (BH : Node_Id; AH : in out Node_Id) is
1252 Get (Value => Val_BH, From => BH, OK => OK_BH);
1253 Get (Value => Val_AH, From => AH, OK => OK_AH);
1255 if OK_BH and then OK_AH and then Val_BH < Val_AH then
1256 Set_Raises_Constraint_Error (N);
1257 Error_Msg_N ("upper bound out of range?", AH);
1258 Error_Msg_N ("\Constraint_Error will be raised at run time?", AH);
1260 -- You need to set AH to BH or else in the case of enumerations
1261 -- indices we will not be able to resolve the aggregate bounds.
1263 AH := Duplicate_Subexpr (BH);
1271 procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id) is
1282 pragma Warnings (Off, OK_AL);
1283 pragma Warnings (Off, OK_AH);
1286 if Raises_Constraint_Error (N)
1287 or else Dynamic_Or_Null_Range (AL, AH)
1292 Get (Value => Val_L, From => L, OK => OK_L);
1293 Get (Value => Val_H, From => H, OK => OK_H);
1295 Get (Value => Val_AL, From => AL, OK => OK_AL);
1296 Get (Value => Val_AH, From => AH, OK => OK_AH);
1298 if OK_L and then Val_L > Val_AL then
1299 Set_Raises_Constraint_Error (N);
1300 Error_Msg_N ("lower bound of aggregate out of range?", N);
1301 Error_Msg_N ("\Constraint_Error will be raised at run time?", N);
1304 if OK_H and then Val_H < Val_AH then
1305 Set_Raises_Constraint_Error (N);
1306 Error_Msg_N ("upper bound of aggregate out of range?", N);
1307 Error_Msg_N ("\Constraint_Error will be raised at run time?", N);
1315 procedure Check_Length (L, H : Node_Id; Len : Uint) is
1325 if Raises_Constraint_Error (N) then
1329 Get (Value => Val_L, From => L, OK => OK_L);
1330 Get (Value => Val_H, From => H, OK => OK_H);
1332 if not OK_L or else not OK_H then
1336 -- If null range length is zero
1338 if Val_L > Val_H then
1339 Range_Len := Uint_0;
1341 Range_Len := Val_H - Val_L + 1;
1344 if Range_Len < Len then
1345 Set_Raises_Constraint_Error (N);
1346 Error_Msg_N ("too many elements?", N);
1347 Error_Msg_N ("\Constraint_Error will be raised at run time?", N);
1351 ---------------------------
1352 -- Dynamic_Or_Null_Range --
1353 ---------------------------
1355 function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean is
1363 Get (Value => Val_L, From => L, OK => OK_L);
1364 Get (Value => Val_H, From => H, OK => OK_H);
1366 return not OK_L or else not OK_H
1367 or else not Is_OK_Static_Expression (L)
1368 or else not Is_OK_Static_Expression (H)
1369 or else Val_L > Val_H;
1370 end Dynamic_Or_Null_Range;
1376 procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean) is
1380 if Compile_Time_Known_Value (From) then
1381 Value := Expr_Value (From);
1383 -- If expression From is something like Some_Type'Val (10) then
1386 elsif Nkind (From) = N_Attribute_Reference
1387 and then Attribute_Name (From) = Name_Val
1388 and then Compile_Time_Known_Value (First (Expressions (From)))
1390 Value := Expr_Value (First (Expressions (From)));
1398 -----------------------
1399 -- Resolve_Aggr_Expr --
1400 -----------------------
1402 function Resolve_Aggr_Expr
1404 Single_Elmt : Boolean) return Boolean
1406 Nxt_Ind : constant Node_Id := Next_Index (Index);
1407 Nxt_Ind_Constr : constant Node_Id := Next_Index (Index_Constr);
1408 -- Index is the current index corresponding to the expression
1410 Resolution_OK : Boolean := True;
1411 -- Set to False if resolution of the expression failed
1414 -- Defend against previous errors
1416 if Nkind (Expr) = N_Error
1417 or else Error_Posted (Expr)
1422 -- If the array type against which we are resolving the aggregate
1423 -- has several dimensions, the expressions nested inside the
1424 -- aggregate must be further aggregates (or strings).
1426 if Present (Nxt_Ind) then
1427 if Nkind (Expr) /= N_Aggregate then
1429 -- A string literal can appear where a one-dimensional array
1430 -- of characters is expected. If the literal looks like an
1431 -- operator, it is still an operator symbol, which will be
1432 -- transformed into a string when analyzed.
1434 if Is_Character_Type (Component_Typ)
1435 and then No (Next_Index (Nxt_Ind))
1436 and then Nkind_In (Expr, N_String_Literal, N_Operator_Symbol)
1438 -- A string literal used in a multidimensional array
1439 -- aggregate in place of the final one-dimensional
1440 -- aggregate must not be enclosed in parentheses.
1442 if Paren_Count (Expr) /= 0 then
1443 Error_Msg_N ("no parenthesis allowed here", Expr);
1446 Make_String_Into_Aggregate (Expr);
1449 Error_Msg_N ("nested array aggregate expected", Expr);
1451 -- If the expression is parenthesized, this may be
1452 -- a missing component association for a 1-aggregate.
1454 if Paren_Count (Expr) > 0 then
1456 ("\if single-component aggregate is intended,"
1457 & " write e.g. (1 ='> ...)", Expr);
1463 -- Ada 2005 (AI-231): Propagate the type to the nested aggregate.
1464 -- Required to check the null-exclusion attribute (if present).
1465 -- This value may be overridden later on.
1467 Set_Etype (Expr, Etype (N));
1469 Resolution_OK := Resolve_Array_Aggregate
1470 (Expr, Nxt_Ind, Nxt_Ind_Constr, Component_Typ, Others_Allowed);
1472 -- Do not resolve the expressions of discrete or others choices
1473 -- unless the expression covers a single component, or the expander
1477 or else not Expander_Active
1478 or else In_Spec_Expression
1480 Analyze_And_Resolve (Expr, Component_Typ);
1481 Check_Expr_OK_In_Limited_Aggregate (Expr);
1482 Check_Non_Static_Context (Expr);
1483 Aggregate_Constraint_Checks (Expr, Component_Typ);
1484 Check_Unset_Reference (Expr);
1487 if Raises_Constraint_Error (Expr)
1488 and then Nkind (Parent (Expr)) /= N_Component_Association
1490 Set_Raises_Constraint_Error (N);
1493 -- If the expression has been marked as requiring a range check,
1494 -- then generate it here.
1496 if Do_Range_Check (Expr) then
1497 Set_Do_Range_Check (Expr, False);
1498 Generate_Range_Check (Expr, Component_Typ, CE_Range_Check_Failed);
1501 return Resolution_OK;
1502 end Resolve_Aggr_Expr;
1504 -- Variables local to Resolve_Array_Aggregate
1511 pragma Warnings (Off, Discard);
1513 Aggr_Low : Node_Id := Empty;
1514 Aggr_High : Node_Id := Empty;
1515 -- The actual low and high bounds of this sub-aggregate
1517 Choices_Low : Node_Id := Empty;
1518 Choices_High : Node_Id := Empty;
1519 -- The lowest and highest discrete choices values for a named aggregate
1521 Nb_Elements : Uint := Uint_0;
1522 -- The number of elements in a positional aggregate
1524 Others_Present : Boolean := False;
1526 Nb_Choices : Nat := 0;
1527 -- Contains the overall number of named choices in this sub-aggregate
1529 Nb_Discrete_Choices : Nat := 0;
1530 -- The overall number of discrete choices (not counting others choice)
1532 Case_Table_Size : Nat;
1533 -- Contains the size of the case table needed to sort aggregate choices
1535 -- Start of processing for Resolve_Array_Aggregate
1538 -- Ignore junk empty aggregate resulting from parser error
1540 if No (Expressions (N))
1541 and then No (Component_Associations (N))
1542 and then not Null_Record_Present (N)
1547 -- STEP 1: make sure the aggregate is correctly formatted
1549 if Present (Component_Associations (N)) then
1550 Assoc := First (Component_Associations (N));
1551 while Present (Assoc) loop
1552 Choice := First (Choices (Assoc));
1553 while Present (Choice) loop
1554 if Nkind (Choice) = N_Others_Choice then
1555 Others_Present := True;
1557 if Choice /= First (Choices (Assoc))
1558 or else Present (Next (Choice))
1561 ("OTHERS must appear alone in a choice list", Choice);
1565 if Present (Next (Assoc)) then
1567 ("OTHERS must appear last in an aggregate", Choice);
1571 if Ada_Version = Ada_83
1572 and then Assoc /= First (Component_Associations (N))
1573 and then Nkind_In (Parent (N), N_Assignment_Statement,
1574 N_Object_Declaration)
1577 ("(Ada 83) illegal context for OTHERS choice", N);
1581 Nb_Choices := Nb_Choices + 1;
1589 -- At this point we know that the others choice, if present, is by
1590 -- itself and appears last in the aggregate. Check if we have mixed
1591 -- positional and discrete associations (other than the others choice).
1593 if Present (Expressions (N))
1594 and then (Nb_Choices > 1
1595 or else (Nb_Choices = 1 and then not Others_Present))
1598 ("named association cannot follow positional association",
1599 First (Choices (First (Component_Associations (N)))));
1603 -- Test for the validity of an others choice if present
1605 if Others_Present and then not Others_Allowed then
1607 ("OTHERS choice not allowed here",
1608 First (Choices (First (Component_Associations (N)))));
1612 -- Protect against cascaded errors
1614 if Etype (Index_Typ) = Any_Type then
1618 -- STEP 2: Process named components
1620 if No (Expressions (N)) then
1621 if Others_Present then
1622 Case_Table_Size := Nb_Choices - 1;
1624 Case_Table_Size := Nb_Choices;
1630 -- Denote the lowest and highest values in an aggregate choice
1634 -- High end of one range and Low end of the next. Should be
1635 -- contiguous if there is no hole in the list of values.
1637 Missing_Values : Boolean;
1638 -- Set True if missing index values
1640 S_Low : Node_Id := Empty;
1641 S_High : Node_Id := Empty;
1642 -- if a choice in an aggregate is a subtype indication these
1643 -- denote the lowest and highest values of the subtype
1645 Table : Case_Table_Type (1 .. Case_Table_Size);
1646 -- Used to sort all the different choice values
1648 Single_Choice : Boolean;
1649 -- Set to true every time there is a single discrete choice in a
1650 -- discrete association
1652 Prev_Nb_Discrete_Choices : Nat;
1653 -- Used to keep track of the number of discrete choices in the
1654 -- current association.
1657 -- STEP 2 (A): Check discrete choices validity
1659 Assoc := First (Component_Associations (N));
1660 while Present (Assoc) loop
1661 Prev_Nb_Discrete_Choices := Nb_Discrete_Choices;
1662 Choice := First (Choices (Assoc));
1666 if Nkind (Choice) = N_Others_Choice then
1667 Single_Choice := False;
1670 -- Test for subtype mark without constraint
1672 elsif Is_Entity_Name (Choice) and then
1673 Is_Type (Entity (Choice))
1675 if Base_Type (Entity (Choice)) /= Index_Base then
1677 ("invalid subtype mark in aggregate choice",
1682 -- Case of subtype indication
1684 elsif Nkind (Choice) = N_Subtype_Indication then
1685 Resolve_Discrete_Subtype_Indication (Choice, Index_Base);
1687 -- Does the subtype indication evaluation raise CE ?
1689 Get_Index_Bounds (Subtype_Mark (Choice), S_Low, S_High);
1690 Get_Index_Bounds (Choice, Low, High);
1691 Check_Bounds (S_Low, S_High, Low, High);
1693 -- Case of range or expression
1696 Resolve (Choice, Index_Base);
1697 Check_Unset_Reference (Choice);
1698 Check_Non_Static_Context (Choice);
1700 -- Do not range check a choice. This check is redundant
1701 -- since this test is already done when we check that the
1702 -- bounds of the array aggregate are within range.
1704 Set_Do_Range_Check (Choice, False);
1707 -- If we could not resolve the discrete choice stop here
1709 if Etype (Choice) = Any_Type then
1712 -- If the discrete choice raises CE get its original bounds
1714 elsif Nkind (Choice) = N_Raise_Constraint_Error then
1715 Set_Raises_Constraint_Error (N);
1716 Get_Index_Bounds (Original_Node (Choice), Low, High);
1718 -- Otherwise get its bounds as usual
1721 Get_Index_Bounds (Choice, Low, High);
1724 if (Dynamic_Or_Null_Range (Low, High)
1725 or else (Nkind (Choice) = N_Subtype_Indication
1727 Dynamic_Or_Null_Range (S_Low, S_High)))
1728 and then Nb_Choices /= 1
1731 ("dynamic or empty choice in aggregate " &
1732 "must be the only choice", Choice);
1736 Nb_Discrete_Choices := Nb_Discrete_Choices + 1;
1737 Table (Nb_Discrete_Choices).Choice_Lo := Low;
1738 Table (Nb_Discrete_Choices).Choice_Hi := High;
1744 -- Check if we have a single discrete choice and whether
1745 -- this discrete choice specifies a single value.
1748 (Nb_Discrete_Choices = Prev_Nb_Discrete_Choices + 1)
1749 and then (Low = High);
1755 -- Ada 2005 (AI-231)
1757 if Ada_Version >= Ada_05
1758 and then Known_Null (Expression (Assoc))
1760 Check_Can_Never_Be_Null (Etype (N), Expression (Assoc));
1763 -- Ada 2005 (AI-287): In case of default initialized component
1764 -- we delay the resolution to the expansion phase.
1766 if Box_Present (Assoc) then
1768 -- Ada 2005 (AI-287): In case of default initialization of a
1769 -- component the expander will generate calls to the
1770 -- corresponding initialization subprogram.
1774 elsif not Resolve_Aggr_Expr (Expression (Assoc),
1775 Single_Elmt => Single_Choice)
1779 -- Check incorrect use of dynamically tagged expression
1781 -- We differentiate here two cases because the expression may
1782 -- not be decorated. For example, the analysis and resolution
1783 -- of the expression associated with the others choice will be
1784 -- done later with the full aggregate. In such case we
1785 -- duplicate the expression tree to analyze the copy and
1786 -- perform the required check.
1788 elsif not Present (Etype (Expression (Assoc))) then
1790 Save_Analysis : constant Boolean := Full_Analysis;
1791 Expr : constant Node_Id :=
1792 New_Copy_Tree (Expression (Assoc));
1795 Expander_Mode_Save_And_Set (False);
1796 Full_Analysis := False;
1798 Full_Analysis := Save_Analysis;
1799 Expander_Mode_Restore;
1801 if Is_Tagged_Type (Etype (Expr)) then
1802 Check_Dynamically_Tagged_Expression
1804 Typ => Component_Type (Etype (N)),
1809 elsif Is_Tagged_Type (Etype (Expression (Assoc))) then
1810 Check_Dynamically_Tagged_Expression
1811 (Expr => Expression (Assoc),
1812 Typ => Component_Type (Etype (N)),
1819 -- If aggregate contains more than one choice then these must be
1820 -- static. Sort them and check that they are contiguous.
1822 if Nb_Discrete_Choices > 1 then
1823 Sort_Case_Table (Table);
1824 Missing_Values := False;
1826 Outer : for J in 1 .. Nb_Discrete_Choices - 1 loop
1827 if Expr_Value (Table (J).Choice_Hi) >=
1828 Expr_Value (Table (J + 1).Choice_Lo)
1831 ("duplicate choice values in array aggregate",
1832 Table (J).Choice_Hi);
1835 elsif not Others_Present then
1836 Hi_Val := Expr_Value (Table (J).Choice_Hi);
1837 Lo_Val := Expr_Value (Table (J + 1).Choice_Lo);
1839 -- If missing values, output error messages
1841 if Lo_Val - Hi_Val > 1 then
1843 -- Header message if not first missing value
1845 if not Missing_Values then
1847 ("missing index value(s) in array aggregate", N);
1848 Missing_Values := True;
1851 -- Output values of missing indexes
1853 Lo_Val := Lo_Val - 1;
1854 Hi_Val := Hi_Val + 1;
1856 -- Enumeration type case
1858 if Is_Enumeration_Type (Index_Typ) then
1861 (Get_Enum_Lit_From_Pos
1862 (Index_Typ, Hi_Val, Loc));
1864 if Lo_Val = Hi_Val then
1865 Error_Msg_N ("\ %", N);
1869 (Get_Enum_Lit_From_Pos
1870 (Index_Typ, Lo_Val, Loc));
1871 Error_Msg_N ("\ % .. %", N);
1874 -- Integer types case
1877 Error_Msg_Uint_1 := Hi_Val;
1879 if Lo_Val = Hi_Val then
1880 Error_Msg_N ("\ ^", N);
1882 Error_Msg_Uint_2 := Lo_Val;
1883 Error_Msg_N ("\ ^ .. ^", N);
1890 if Missing_Values then
1891 Set_Etype (N, Any_Composite);
1896 -- STEP 2 (B): Compute aggregate bounds and min/max choices values
1898 if Nb_Discrete_Choices > 0 then
1899 Choices_Low := Table (1).Choice_Lo;
1900 Choices_High := Table (Nb_Discrete_Choices).Choice_Hi;
1903 -- If Others is present, then bounds of aggregate come from the
1904 -- index constraint (not the choices in the aggregate itself).
1906 if Others_Present then
1907 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
1909 -- No others clause present
1912 -- Special processing if others allowed and not present. This
1913 -- means that the bounds of the aggregate come from the index
1914 -- constraint (and the length must match).
1916 if Others_Allowed then
1917 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
1919 -- If others allowed, and no others present, then the array
1920 -- should cover all index values. If it does not, we will
1921 -- get a length check warning, but there is two cases where
1922 -- an additional warning is useful:
1924 -- If we have no positional components, and the length is
1925 -- wrong (which we can tell by others being allowed with
1926 -- missing components), and the index type is an enumeration
1927 -- type, then issue appropriate warnings about these missing
1928 -- components. They are only warnings, since the aggregate
1929 -- is fine, it's just the wrong length. We skip this check
1930 -- for standard character types (since there are no literals
1931 -- and it is too much trouble to concoct them), and also if
1932 -- any of the bounds have not-known-at-compile-time values.
1934 -- Another case warranting a warning is when the length is
1935 -- right, but as above we have an index type that is an
1936 -- enumeration, and the bounds do not match. This is a
1937 -- case where dubious sliding is allowed and we generate
1938 -- a warning that the bounds do not match.
1940 if No (Expressions (N))
1941 and then Nkind (Index) = N_Range
1942 and then Is_Enumeration_Type (Etype (Index))
1943 and then not Is_Standard_Character_Type (Etype (Index))
1944 and then Compile_Time_Known_Value (Aggr_Low)
1945 and then Compile_Time_Known_Value (Aggr_High)
1946 and then Compile_Time_Known_Value (Choices_Low)
1947 and then Compile_Time_Known_Value (Choices_High)
1949 -- If the bounds have semantic errors, do not attempt
1950 -- further resolution to prevent cascaded errors.
1952 if Error_Posted (Choices_Low)
1953 or else Error_Posted (Choices_High)
1959 ALo : constant Node_Id := Expr_Value_E (Aggr_Low);
1960 AHi : constant Node_Id := Expr_Value_E (Aggr_High);
1961 CLo : constant Node_Id := Expr_Value_E (Choices_Low);
1962 CHi : constant Node_Id := Expr_Value_E (Choices_High);
1967 -- Warning case 1, missing values at start/end. Only
1968 -- do the check if the number of entries is too small.
1970 if (Enumeration_Pos (CHi) - Enumeration_Pos (CLo))
1972 (Enumeration_Pos (AHi) - Enumeration_Pos (ALo))
1975 ("missing index value(s) in array aggregate?", N);
1977 -- Output missing value(s) at start
1979 if Chars (ALo) /= Chars (CLo) then
1982 if Chars (ALo) = Chars (Ent) then
1983 Error_Msg_Name_1 := Chars (ALo);
1984 Error_Msg_N ("\ %?", N);
1986 Error_Msg_Name_1 := Chars (ALo);
1987 Error_Msg_Name_2 := Chars (Ent);
1988 Error_Msg_N ("\ % .. %?", N);
1992 -- Output missing value(s) at end
1994 if Chars (AHi) /= Chars (CHi) then
1997 if Chars (AHi) = Chars (Ent) then
1998 Error_Msg_Name_1 := Chars (Ent);
1999 Error_Msg_N ("\ %?", N);
2001 Error_Msg_Name_1 := Chars (Ent);
2002 Error_Msg_Name_2 := Chars (AHi);
2003 Error_Msg_N ("\ % .. %?", N);
2007 -- Warning case 2, dubious sliding. The First_Subtype
2008 -- test distinguishes between a constrained type where
2009 -- sliding is not allowed (so we will get a warning
2010 -- later that Constraint_Error will be raised), and
2011 -- the unconstrained case where sliding is permitted.
2013 elsif (Enumeration_Pos (CHi) - Enumeration_Pos (CLo))
2015 (Enumeration_Pos (AHi) - Enumeration_Pos (ALo))
2016 and then Chars (ALo) /= Chars (CLo)
2018 not Is_Constrained (First_Subtype (Etype (N)))
2021 ("bounds of aggregate do not match target?", N);
2027 -- If no others, aggregate bounds come from aggregate
2029 Aggr_Low := Choices_Low;
2030 Aggr_High := Choices_High;
2034 -- STEP 3: Process positional components
2037 -- STEP 3 (A): Process positional elements
2039 Expr := First (Expressions (N));
2040 Nb_Elements := Uint_0;
2041 while Present (Expr) loop
2042 Nb_Elements := Nb_Elements + 1;
2044 -- Ada 2005 (AI-231)
2046 if Ada_Version >= Ada_05
2047 and then Known_Null (Expr)
2049 Check_Can_Never_Be_Null (Etype (N), Expr);
2052 if not Resolve_Aggr_Expr (Expr, Single_Elmt => True) then
2056 -- Check incorrect use of dynamically tagged expression
2058 if Is_Tagged_Type (Etype (Expr)) then
2059 Check_Dynamically_Tagged_Expression
2061 Typ => Component_Type (Etype (N)),
2068 if Others_Present then
2069 Assoc := Last (Component_Associations (N));
2071 -- Ada 2005 (AI-231)
2073 if Ada_Version >= Ada_05
2074 and then Known_Null (Assoc)
2076 Check_Can_Never_Be_Null (Etype (N), Expression (Assoc));
2079 -- Ada 2005 (AI-287): In case of default initialized component,
2080 -- we delay the resolution to the expansion phase.
2082 if Box_Present (Assoc) then
2084 -- Ada 2005 (AI-287): In case of default initialization of a
2085 -- component the expander will generate calls to the
2086 -- corresponding initialization subprogram.
2090 elsif not Resolve_Aggr_Expr (Expression (Assoc),
2091 Single_Elmt => False)
2095 -- Check incorrect use of dynamically tagged expression. The
2096 -- expression of the others choice has not been resolved yet.
2097 -- In order to diagnose the semantic error we create a duplicate
2098 -- tree to analyze it and perform the check.
2102 Save_Analysis : constant Boolean := Full_Analysis;
2103 Expr : constant Node_Id :=
2104 New_Copy_Tree (Expression (Assoc));
2107 Expander_Mode_Save_And_Set (False);
2108 Full_Analysis := False;
2110 Full_Analysis := Save_Analysis;
2111 Expander_Mode_Restore;
2113 if Is_Tagged_Type (Etype (Expr)) then
2114 Check_Dynamically_Tagged_Expression
2116 Typ => Component_Type (Etype (N)),
2123 -- STEP 3 (B): Compute the aggregate bounds
2125 if Others_Present then
2126 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2129 if Others_Allowed then
2130 Get_Index_Bounds (Index_Constr, Aggr_Low, Discard);
2132 Aggr_Low := Index_Typ_Low;
2135 Aggr_High := Add (Nb_Elements - 1, To => Aggr_Low);
2136 Check_Bound (Index_Base_High, Aggr_High);
2140 -- STEP 4: Perform static aggregate checks and save the bounds
2144 Check_Bounds (Index_Typ_Low, Index_Typ_High, Aggr_Low, Aggr_High);
2145 Check_Bounds (Index_Base_Low, Index_Base_High, Aggr_Low, Aggr_High);
2149 if Others_Present and then Nb_Discrete_Choices > 0 then
2150 Check_Bounds (Aggr_Low, Aggr_High, Choices_Low, Choices_High);
2151 Check_Bounds (Index_Typ_Low, Index_Typ_High,
2152 Choices_Low, Choices_High);
2153 Check_Bounds (Index_Base_Low, Index_Base_High,
2154 Choices_Low, Choices_High);
2158 elsif Others_Present and then Nb_Elements > 0 then
2159 Check_Length (Aggr_Low, Aggr_High, Nb_Elements);
2160 Check_Length (Index_Typ_Low, Index_Typ_High, Nb_Elements);
2161 Check_Length (Index_Base_Low, Index_Base_High, Nb_Elements);
2164 if Raises_Constraint_Error (Aggr_Low)
2165 or else Raises_Constraint_Error (Aggr_High)
2167 Set_Raises_Constraint_Error (N);
2170 Aggr_Low := Duplicate_Subexpr (Aggr_Low);
2172 -- Do not duplicate Aggr_High if Aggr_High = Aggr_Low + Nb_Elements
2173 -- since the addition node returned by Add is not yet analyzed. Attach
2174 -- to tree and analyze first. Reset analyzed flag to ensure it will get
2175 -- analyzed when it is a literal bound whose type must be properly set.
2177 if Others_Present or else Nb_Discrete_Choices > 0 then
2178 Aggr_High := Duplicate_Subexpr (Aggr_High);
2180 if Etype (Aggr_High) = Universal_Integer then
2181 Set_Analyzed (Aggr_High, False);
2185 -- If the aggregate already has bounds attached to it, it means this is
2186 -- a positional aggregate created as an optimization by
2187 -- Exp_Aggr.Convert_To_Positional, so we don't want to change those
2190 if Present (Aggregate_Bounds (N)) and then not Others_Allowed then
2191 Aggr_Low := Low_Bound (Aggregate_Bounds (N));
2192 Aggr_High := High_Bound (Aggregate_Bounds (N));
2195 Set_Aggregate_Bounds
2196 (N, Make_Range (Loc, Low_Bound => Aggr_Low, High_Bound => Aggr_High));
2198 -- The bounds may contain expressions that must be inserted upwards.
2199 -- Attach them fully to the tree. After analysis, remove side effects
2200 -- from upper bound, if still needed.
2202 Set_Parent (Aggregate_Bounds (N), N);
2203 Analyze_And_Resolve (Aggregate_Bounds (N), Index_Typ);
2204 Check_Unset_Reference (Aggregate_Bounds (N));
2206 if not Others_Present and then Nb_Discrete_Choices = 0 then
2207 Set_High_Bound (Aggregate_Bounds (N),
2208 Duplicate_Subexpr (High_Bound (Aggregate_Bounds (N))));
2212 end Resolve_Array_Aggregate;
2214 ---------------------------------
2215 -- Resolve_Extension_Aggregate --
2216 ---------------------------------
2218 -- There are two cases to consider:
2220 -- a) If the ancestor part is a type mark, the components needed are the
2221 -- difference between the components of the expected type and the
2222 -- components of the given type mark.
2224 -- b) If the ancestor part is an expression, it must be unambiguous, and
2225 -- once we have its type we can also compute the needed components as in
2226 -- the previous case. In both cases, if the ancestor type is not the
2227 -- immediate ancestor, we have to build this ancestor recursively.
2229 -- In both cases discriminants of the ancestor type do not play a role in
2230 -- the resolution of the needed components, because inherited discriminants
2231 -- cannot be used in a type extension. As a result we can compute
2232 -- independently the list of components of the ancestor type and of the
2235 procedure Resolve_Extension_Aggregate (N : Node_Id; Typ : Entity_Id) is
2236 A : constant Node_Id := Ancestor_Part (N);
2241 function Valid_Limited_Ancestor (Anc : Node_Id) return Boolean;
2242 -- If the type is limited, verify that the ancestor part is a legal
2243 -- expression (aggregate or function call, including 'Input)) that does
2244 -- not require a copy, as specified in 7.5(2).
2246 function Valid_Ancestor_Type return Boolean;
2247 -- Verify that the type of the ancestor part is a non-private ancestor
2248 -- of the expected type, which must be a type extension.
2250 ----------------------------
2251 -- Valid_Limited_Ancestor --
2252 ----------------------------
2254 function Valid_Limited_Ancestor (Anc : Node_Id) return Boolean is
2256 if Is_Entity_Name (Anc)
2257 and then Is_Type (Entity (Anc))
2261 elsif Nkind_In (Anc, N_Aggregate, N_Function_Call) then
2264 elsif Nkind (Anc) = N_Attribute_Reference
2265 and then Attribute_Name (Anc) = Name_Input
2269 elsif Nkind (Anc) = N_Qualified_Expression then
2270 return Valid_Limited_Ancestor (Expression (Anc));
2275 end Valid_Limited_Ancestor;
2277 -------------------------
2278 -- Valid_Ancestor_Type --
2279 -------------------------
2281 function Valid_Ancestor_Type return Boolean is
2282 Imm_Type : Entity_Id;
2285 Imm_Type := Base_Type (Typ);
2286 while Is_Derived_Type (Imm_Type) loop
2287 if Etype (Imm_Type) = Base_Type (A_Type) then
2290 -- The base type of the parent type may appear as a private
2291 -- extension if it is declared as such in a parent unit of the
2292 -- current one. For consistency of the subsequent analysis use
2293 -- the partial view for the ancestor part.
2295 elsif Is_Private_Type (Etype (Imm_Type))
2296 and then Present (Full_View (Etype (Imm_Type)))
2297 and then Base_Type (A_Type) = Full_View (Etype (Imm_Type))
2299 A_Type := Etype (Imm_Type);
2302 -- The parent type may be a private extension. The aggregate is
2303 -- legal if the type of the aggregate is an extension of it that
2304 -- is not a private extension.
2306 elsif Is_Private_Type (A_Type)
2307 and then not Is_Private_Type (Imm_Type)
2308 and then Present (Full_View (A_Type))
2309 and then Base_Type (Full_View (A_Type)) = Etype (Imm_Type)
2314 Imm_Type := Etype (Base_Type (Imm_Type));
2318 -- If previous loop did not find a proper ancestor, report error
2320 Error_Msg_NE ("expect ancestor type of &", A, Typ);
2322 end Valid_Ancestor_Type;
2324 -- Start of processing for Resolve_Extension_Aggregate
2327 -- Analyze the ancestor part and account for the case where it is a
2328 -- parameterless function call.
2331 Check_Parameterless_Call (A);
2333 if not Is_Tagged_Type (Typ) then
2334 Error_Msg_N ("type of extension aggregate must be tagged", N);
2337 elsif Is_Limited_Type (Typ) then
2339 -- Ada 2005 (AI-287): Limited aggregates are allowed
2341 if Ada_Version < Ada_05 then
2342 Error_Msg_N ("aggregate type cannot be limited", N);
2343 Explain_Limited_Type (Typ, N);
2346 elsif Valid_Limited_Ancestor (A) then
2351 ("limited ancestor part must be aggregate or function call", A);
2354 elsif Is_Class_Wide_Type (Typ) then
2355 Error_Msg_N ("aggregate cannot be of a class-wide type", N);
2359 if Is_Entity_Name (A)
2360 and then Is_Type (Entity (A))
2362 A_Type := Get_Full_View (Entity (A));
2364 if Valid_Ancestor_Type then
2365 Set_Entity (A, A_Type);
2366 Set_Etype (A, A_Type);
2368 Validate_Ancestor_Part (N);
2369 Resolve_Record_Aggregate (N, Typ);
2372 elsif Nkind (A) /= N_Aggregate then
2373 if Is_Overloaded (A) then
2376 Get_First_Interp (A, I, It);
2377 while Present (It.Typ) loop
2378 -- Only consider limited interpretations in the Ada 2005 case
2380 if Is_Tagged_Type (It.Typ)
2381 and then (Ada_Version >= Ada_05
2382 or else not Is_Limited_Type (It.Typ))
2384 if A_Type /= Any_Type then
2385 Error_Msg_N ("cannot resolve expression", A);
2392 Get_Next_Interp (I, It);
2395 if A_Type = Any_Type then
2396 if Ada_Version >= Ada_05 then
2397 Error_Msg_N ("ancestor part must be of a tagged type", A);
2400 ("ancestor part must be of a nonlimited tagged type", A);
2407 A_Type := Etype (A);
2410 if Valid_Ancestor_Type then
2411 Resolve (A, A_Type);
2412 Check_Unset_Reference (A);
2413 Check_Non_Static_Context (A);
2415 -- The aggregate is illegal if the ancestor expression is a call
2416 -- to a function with a limited unconstrained result, unless the
2417 -- type of the aggregate is a null extension. This restriction
2418 -- was added in AI05-67 to simplify implementation.
2420 if Nkind (A) = N_Function_Call
2421 and then Is_Limited_Type (A_Type)
2422 and then not Is_Null_Extension (Typ)
2423 and then not Is_Constrained (A_Type)
2426 ("type of limited ancestor part must be constrained", A);
2428 -- Reject the use of CPP constructors that leave objects partially
2429 -- initialized. For example:
2431 -- type CPP_Root is tagged limited record ...
2432 -- pragma Import (CPP, CPP_Root);
2434 -- type CPP_DT is new CPP_Root and Iface ...
2435 -- pragma Import (CPP, CPP_DT);
2437 -- type Ada_DT is new CPP_DT with ...
2439 -- Obj : Ada_DT := Ada_DT'(New_CPP_Root with others => <>);
2441 -- Using the constructor of CPP_Root the slots of the dispatch
2442 -- table of CPP_DT cannot be set, and the secondary tag of
2443 -- CPP_DT is unknown.
2445 elsif Nkind (A) = N_Function_Call
2446 and then Is_CPP_Constructor_Call (A)
2447 and then Enclosing_CPP_Parent (Typ) /= A_Type
2450 ("?must use 'C'P'P constructor for type &", A,
2451 Enclosing_CPP_Parent (Typ));
2453 -- The following call is not needed if the previous warning
2454 -- is promoted to an error.
2456 Resolve_Record_Aggregate (N, Typ);
2458 elsif Is_Class_Wide_Type (Etype (A))
2459 and then Nkind (Original_Node (A)) = N_Function_Call
2461 -- If the ancestor part is a dispatching call, it appears
2462 -- statically to be a legal ancestor, but it yields any member
2463 -- of the class, and it is not possible to determine whether
2464 -- it is an ancestor of the extension aggregate (much less
2465 -- which ancestor). It is not possible to determine the
2466 -- components of the extension part.
2468 -- This check implements AI-306, which in fact was motivated by
2469 -- an AdaCore query to the ARG after this test was added.
2471 Error_Msg_N ("ancestor part must be statically tagged", A);
2473 Resolve_Record_Aggregate (N, Typ);
2478 Error_Msg_N ("no unique type for this aggregate", A);
2480 end Resolve_Extension_Aggregate;
2482 ------------------------------
2483 -- Resolve_Record_Aggregate --
2484 ------------------------------
2486 procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id) is
2488 -- N_Component_Association node belonging to the input aggregate N
2491 Positional_Expr : Node_Id;
2492 Component : Entity_Id;
2493 Component_Elmt : Elmt_Id;
2495 Components : constant Elist_Id := New_Elmt_List;
2496 -- Components is the list of the record components whose value must be
2497 -- provided in the aggregate. This list does include discriminants.
2499 New_Assoc_List : constant List_Id := New_List;
2500 New_Assoc : Node_Id;
2501 -- New_Assoc_List is the newly built list of N_Component_Association
2502 -- nodes. New_Assoc is one such N_Component_Association node in it.
2503 -- Note that while Assoc and New_Assoc contain the same kind of nodes,
2504 -- they are used to iterate over two different N_Component_Association
2507 Others_Etype : Entity_Id := Empty;
2508 -- This variable is used to save the Etype of the last record component
2509 -- that takes its value from the others choice. Its purpose is:
2511 -- (a) make sure the others choice is useful
2513 -- (b) make sure the type of all the components whose value is
2514 -- subsumed by the others choice are the same.
2516 -- This variable is updated as a side effect of function Get_Value.
2518 Is_Box_Present : Boolean := False;
2519 Others_Box : Boolean := False;
2520 -- Ada 2005 (AI-287): Variables used in case of default initialization
2521 -- to provide a functionality similar to Others_Etype. Box_Present
2522 -- indicates that the component takes its default initialization;
2523 -- Others_Box indicates that at least one component takes its default
2524 -- initialization. Similar to Others_Etype, they are also updated as a
2525 -- side effect of function Get_Value.
2527 procedure Add_Association
2528 (Component : Entity_Id;
2530 Assoc_List : List_Id;
2531 Is_Box_Present : Boolean := False);
2532 -- Builds a new N_Component_Association node which associates Component
2533 -- to expression Expr and adds it to the association list being built,
2534 -- either New_Assoc_List, or the association being built for an inner
2537 function Discr_Present (Discr : Entity_Id) return Boolean;
2538 -- If aggregate N is a regular aggregate this routine will return True.
2539 -- Otherwise, if N is an extension aggregate, Discr is a discriminant
2540 -- whose value may already have been specified by N's ancestor part.
2541 -- This routine checks whether this is indeed the case and if so returns
2542 -- False, signaling that no value for Discr should appear in N's
2543 -- aggregate part. Also, in this case, the routine appends to
2544 -- New_Assoc_List the discriminant value specified in the ancestor part.
2546 -- If the aggregate is in a context with expansion delayed, it will be
2547 -- reanalyzed. The inherited discriminant values must not be reinserted
2548 -- in the component list to prevent spurious errors, but they must be
2549 -- present on first analysis to build the proper subtype indications.
2550 -- The flag Inherited_Discriminant is used to prevent the re-insertion.
2555 Consider_Others_Choice : Boolean := False)
2557 -- Given a record component stored in parameter Compon, this function
2558 -- returns its value as it appears in the list From, which is a list
2559 -- of N_Component_Association nodes.
2561 -- If no component association has a choice for the searched component,
2562 -- the value provided by the others choice is returned, if there is one,
2563 -- and Consider_Others_Choice is set to true. Otherwise Empty is
2564 -- returned. If there is more than one component association giving a
2565 -- value for the searched record component, an error message is emitted
2566 -- and the first found value is returned.
2568 -- If Consider_Others_Choice is set and the returned expression comes
2569 -- from the others choice, then Others_Etype is set as a side effect.
2570 -- An error message is emitted if the components taking their value from
2571 -- the others choice do not have same type.
2573 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Node_Id);
2574 -- Analyzes and resolves expression Expr against the Etype of the
2575 -- Component. This routine also applies all appropriate checks to Expr.
2576 -- It finally saves a Expr in the newly created association list that
2577 -- will be attached to the final record aggregate. Note that if the
2578 -- Parent pointer of Expr is not set then Expr was produced with a
2579 -- New_Copy_Tree or some such.
2581 ---------------------
2582 -- Add_Association --
2583 ---------------------
2585 procedure Add_Association
2586 (Component : Entity_Id;
2588 Assoc_List : List_Id;
2589 Is_Box_Present : Boolean := False)
2591 Choice_List : constant List_Id := New_List;
2592 New_Assoc : Node_Id;
2595 Append (New_Occurrence_Of (Component, Sloc (Expr)), Choice_List);
2597 Make_Component_Association (Sloc (Expr),
2598 Choices => Choice_List,
2600 Box_Present => Is_Box_Present);
2601 Append (New_Assoc, Assoc_List);
2602 end Add_Association;
2608 function Discr_Present (Discr : Entity_Id) return Boolean is
2609 Regular_Aggr : constant Boolean := Nkind (N) /= N_Extension_Aggregate;
2614 Comp_Assoc : Node_Id;
2615 Discr_Expr : Node_Id;
2617 Ancestor_Typ : Entity_Id;
2618 Orig_Discr : Entity_Id;
2620 D_Val : Elmt_Id := No_Elmt; -- stop junk warning
2622 Ancestor_Is_Subtyp : Boolean;
2625 if Regular_Aggr then
2629 -- Check whether inherited discriminant values have already been
2630 -- inserted in the aggregate. This will be the case if we are
2631 -- re-analyzing an aggregate whose expansion was delayed.
2633 if Present (Component_Associations (N)) then
2634 Comp_Assoc := First (Component_Associations (N));
2635 while Present (Comp_Assoc) loop
2636 if Inherited_Discriminant (Comp_Assoc) then
2644 Ancestor := Ancestor_Part (N);
2645 Ancestor_Typ := Etype (Ancestor);
2646 Loc := Sloc (Ancestor);
2648 -- For a private type with unknown discriminants, use the underlying
2649 -- record view if it is available.
2651 if Has_Unknown_Discriminants (Ancestor_Typ)
2652 and then Present (Full_View (Ancestor_Typ))
2653 and then Present (Underlying_Record_View (Full_View (Ancestor_Typ)))
2655 Ancestor_Typ := Underlying_Record_View (Full_View (Ancestor_Typ));
2658 Ancestor_Is_Subtyp :=
2659 Is_Entity_Name (Ancestor) and then Is_Type (Entity (Ancestor));
2661 -- If the ancestor part has no discriminants clearly N's aggregate
2662 -- part must provide a value for Discr.
2664 if not Has_Discriminants (Ancestor_Typ) then
2667 -- If the ancestor part is an unconstrained subtype mark then the
2668 -- Discr must be present in N's aggregate part.
2670 elsif Ancestor_Is_Subtyp
2671 and then not Is_Constrained (Entity (Ancestor))
2676 -- Now look to see if Discr was specified in the ancestor part
2678 if Ancestor_Is_Subtyp then
2679 D_Val := First_Elmt (Discriminant_Constraint (Entity (Ancestor)));
2682 Orig_Discr := Original_Record_Component (Discr);
2684 D := First_Discriminant (Ancestor_Typ);
2685 while Present (D) loop
2687 -- If Ancestor has already specified Disc value then insert its
2688 -- value in the final aggregate.
2690 if Original_Record_Component (D) = Orig_Discr then
2691 if Ancestor_Is_Subtyp then
2692 Discr_Expr := New_Copy_Tree (Node (D_Val));
2695 Make_Selected_Component (Loc,
2696 Prefix => Duplicate_Subexpr (Ancestor),
2697 Selector_Name => New_Occurrence_Of (Discr, Loc));
2700 Resolve_Aggr_Expr (Discr_Expr, Discr);
2701 Set_Inherited_Discriminant (Last (New_Assoc_List));
2705 Next_Discriminant (D);
2707 if Ancestor_Is_Subtyp then
2722 Consider_Others_Choice : Boolean := False)
2726 Expr : Node_Id := Empty;
2727 Selector_Name : Node_Id;
2730 Is_Box_Present := False;
2732 if Present (From) then
2733 Assoc := First (From);
2738 while Present (Assoc) loop
2739 Selector_Name := First (Choices (Assoc));
2740 while Present (Selector_Name) loop
2741 if Nkind (Selector_Name) = N_Others_Choice then
2742 if Consider_Others_Choice and then No (Expr) then
2744 -- We need to duplicate the expression for each
2745 -- successive component covered by the others choice.
2746 -- This is redundant if the others_choice covers only
2747 -- one component (small optimization possible???), but
2748 -- indispensable otherwise, because each one must be
2749 -- expanded individually to preserve side-effects.
2751 -- Ada 2005 (AI-287): In case of default initialization
2752 -- of components, we duplicate the corresponding default
2753 -- expression (from the record type declaration). The
2754 -- copy must carry the sloc of the association (not the
2755 -- original expression) to prevent spurious elaboration
2756 -- checks when the default includes function calls.
2758 if Box_Present (Assoc) then
2760 Is_Box_Present := True;
2762 if Expander_Active then
2765 (Expression (Parent (Compon)),
2766 New_Sloc => Sloc (Assoc));
2768 return Expression (Parent (Compon));
2772 if Present (Others_Etype) and then
2773 Base_Type (Others_Etype) /= Base_Type (Etype
2776 Error_Msg_N ("components in OTHERS choice must " &
2777 "have same type", Selector_Name);
2780 Others_Etype := Etype (Compon);
2782 if Expander_Active then
2783 return New_Copy_Tree (Expression (Assoc));
2785 return Expression (Assoc);
2790 elsif Chars (Compon) = Chars (Selector_Name) then
2793 -- Ada 2005 (AI-231)
2795 if Ada_Version >= Ada_05
2796 and then Known_Null (Expression (Assoc))
2798 Check_Can_Never_Be_Null (Compon, Expression (Assoc));
2801 -- We need to duplicate the expression when several
2802 -- components are grouped together with a "|" choice.
2803 -- For instance "filed1 | filed2 => Expr"
2805 -- Ada 2005 (AI-287)
2807 if Box_Present (Assoc) then
2808 Is_Box_Present := True;
2810 -- Duplicate the default expression of the component
2811 -- from the record type declaration, so a new copy
2812 -- can be attached to the association.
2814 -- Note that we always copy the default expression,
2815 -- even when the association has a single choice, in
2816 -- order to create a proper association for the
2817 -- expanded aggregate.
2819 Expr := New_Copy_Tree (Expression (Parent (Compon)));
2822 if Present (Next (Selector_Name)) then
2823 Expr := New_Copy_Tree (Expression (Assoc));
2825 Expr := Expression (Assoc);
2829 Generate_Reference (Compon, Selector_Name, 'm');
2833 ("more than one value supplied for &",
2834 Selector_Name, Compon);
2839 Next (Selector_Name);
2848 -----------------------
2849 -- Resolve_Aggr_Expr --
2850 -----------------------
2852 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Node_Id) is
2853 New_C : Entity_Id := Component;
2854 Expr_Type : Entity_Id := Empty;
2856 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean;
2857 -- If the expression is an aggregate (possibly qualified) then its
2858 -- expansion is delayed until the enclosing aggregate is expanded
2859 -- into assignments. In that case, do not generate checks on the
2860 -- expression, because they will be generated later, and will other-
2861 -- wise force a copy (to remove side-effects) that would leave a
2862 -- dynamic-sized aggregate in the code, something that gigi cannot
2866 -- Set to True if the resolved Expr node needs to be relocated
2867 -- when attached to the newly created association list. This node
2868 -- need not be relocated if its parent pointer is not set.
2869 -- In fact in this case Expr is the output of a New_Copy_Tree call.
2870 -- if Relocate is True then we have analyzed the expression node
2871 -- in the original aggregate and hence it needs to be relocated
2872 -- when moved over the new association list.
2874 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean is
2875 Kind : constant Node_Kind := Nkind (Expr);
2877 return (Nkind_In (Kind, N_Aggregate, N_Extension_Aggregate)
2878 and then Present (Etype (Expr))
2879 and then Is_Record_Type (Etype (Expr))
2880 and then Expansion_Delayed (Expr))
2881 or else (Kind = N_Qualified_Expression
2882 and then Has_Expansion_Delayed (Expression (Expr)));
2883 end Has_Expansion_Delayed;
2885 -- Start of processing for Resolve_Aggr_Expr
2888 -- If the type of the component is elementary or the type of the
2889 -- aggregate does not contain discriminants, use the type of the
2890 -- component to resolve Expr.
2892 if Is_Elementary_Type (Etype (Component))
2893 or else not Has_Discriminants (Etype (N))
2895 Expr_Type := Etype (Component);
2897 -- Otherwise we have to pick up the new type of the component from
2898 -- the new constrained subtype of the aggregate. In fact components
2899 -- which are of a composite type might be constrained by a
2900 -- discriminant, and we want to resolve Expr against the subtype were
2901 -- all discriminant occurrences are replaced with their actual value.
2904 New_C := First_Component (Etype (N));
2905 while Present (New_C) loop
2906 if Chars (New_C) = Chars (Component) then
2907 Expr_Type := Etype (New_C);
2911 Next_Component (New_C);
2914 pragma Assert (Present (Expr_Type));
2916 -- For each range in an array type where a discriminant has been
2917 -- replaced with the constraint, check that this range is within
2918 -- the range of the base type. This checks is done in the init
2919 -- proc for regular objects, but has to be done here for
2920 -- aggregates since no init proc is called for them.
2922 if Is_Array_Type (Expr_Type) then
2925 -- Range of the current constrained index in the array
2927 Orig_Index : Node_Id := First_Index (Etype (Component));
2928 -- Range corresponding to the range Index above in the
2929 -- original unconstrained record type. The bounds of this
2930 -- range may be governed by discriminants.
2932 Unconstr_Index : Node_Id := First_Index (Etype (Expr_Type));
2933 -- Range corresponding to the range Index above for the
2934 -- unconstrained array type. This range is needed to apply
2938 Index := First_Index (Expr_Type);
2939 while Present (Index) loop
2940 if Depends_On_Discriminant (Orig_Index) then
2941 Apply_Range_Check (Index, Etype (Unconstr_Index));
2945 Next_Index (Orig_Index);
2946 Next_Index (Unconstr_Index);
2952 -- If the Parent pointer of Expr is not set, Expr is an expression
2953 -- duplicated by New_Tree_Copy (this happens for record aggregates
2954 -- that look like (Field1 | Filed2 => Expr) or (others => Expr)).
2955 -- Such a duplicated expression must be attached to the tree
2956 -- before analysis and resolution to enforce the rule that a tree
2957 -- fragment should never be analyzed or resolved unless it is
2958 -- attached to the current compilation unit.
2960 if No (Parent (Expr)) then
2961 Set_Parent (Expr, N);
2967 Analyze_And_Resolve (Expr, Expr_Type);
2968 Check_Expr_OK_In_Limited_Aggregate (Expr);
2969 Check_Non_Static_Context (Expr);
2970 Check_Unset_Reference (Expr);
2972 -- Check wrong use of class-wide types
2974 if Is_Class_Wide_Type (Etype (Expr)) then
2975 Error_Msg_N ("dynamically tagged expression not allowed", Expr);
2978 if not Has_Expansion_Delayed (Expr) then
2979 Aggregate_Constraint_Checks (Expr, Expr_Type);
2982 if Raises_Constraint_Error (Expr) then
2983 Set_Raises_Constraint_Error (N);
2986 -- If the expression has been marked as requiring a range check,
2987 -- then generate it here.
2989 if Do_Range_Check (Expr) then
2990 Set_Do_Range_Check (Expr, False);
2991 Generate_Range_Check (Expr, Expr_Type, CE_Range_Check_Failed);
2995 Add_Association (New_C, Relocate_Node (Expr), New_Assoc_List);
2997 Add_Association (New_C, Expr, New_Assoc_List);
2999 end Resolve_Aggr_Expr;
3001 -- Start of processing for Resolve_Record_Aggregate
3004 -- We may end up calling Duplicate_Subexpr on expressions that are
3005 -- attached to New_Assoc_List. For this reason we need to attach it
3006 -- to the tree by setting its parent pointer to N. This parent point
3007 -- will change in STEP 8 below.
3009 Set_Parent (New_Assoc_List, N);
3011 -- STEP 1: abstract type and null record verification
3013 if Is_Abstract_Type (Typ) then
3014 Error_Msg_N ("type of aggregate cannot be abstract", N);
3017 if No (First_Entity (Typ)) and then Null_Record_Present (N) then
3021 elsif Present (First_Entity (Typ))
3022 and then Null_Record_Present (N)
3023 and then not Is_Tagged_Type (Typ)
3025 Error_Msg_N ("record aggregate cannot be null", N);
3028 -- If the type has no components, then the aggregate should either
3029 -- have "null record", or in Ada 2005 it could instead have a single
3030 -- component association given by "others => <>". For Ada 95 we flag
3031 -- an error at this point, but for Ada 2005 we proceed with checking
3032 -- the associations below, which will catch the case where it's not
3033 -- an aggregate with "others => <>". Note that the legality of a <>
3034 -- aggregate for a null record type was established by AI05-016.
3036 elsif No (First_Entity (Typ))
3037 and then Ada_Version < Ada_05
3039 Error_Msg_N ("record aggregate must be null", N);
3043 -- STEP 2: Verify aggregate structure
3046 Selector_Name : Node_Id;
3047 Bad_Aggregate : Boolean := False;
3050 if Present (Component_Associations (N)) then
3051 Assoc := First (Component_Associations (N));
3056 while Present (Assoc) loop
3057 Selector_Name := First (Choices (Assoc));
3058 while Present (Selector_Name) loop
3059 if Nkind (Selector_Name) = N_Identifier then
3062 elsif Nkind (Selector_Name) = N_Others_Choice then
3063 if Selector_Name /= First (Choices (Assoc))
3064 or else Present (Next (Selector_Name))
3067 ("OTHERS must appear alone in a choice list",
3071 elsif Present (Next (Assoc)) then
3073 ("OTHERS must appear last in an aggregate",
3077 -- (Ada2005): If this is an association with a box,
3078 -- indicate that the association need not represent
3081 elsif Box_Present (Assoc) then
3087 ("selector name should be identifier or OTHERS",
3089 Bad_Aggregate := True;
3092 Next (Selector_Name);
3098 if Bad_Aggregate then
3103 -- STEP 3: Find discriminant Values
3106 Discrim : Entity_Id;
3107 Missing_Discriminants : Boolean := False;
3110 if Present (Expressions (N)) then
3111 Positional_Expr := First (Expressions (N));
3113 Positional_Expr := Empty;
3116 if Has_Unknown_Discriminants (Typ)
3117 and then Present (Underlying_Record_View (Typ))
3119 Discrim := First_Discriminant (Underlying_Record_View (Typ));
3120 elsif Has_Discriminants (Typ) then
3121 Discrim := First_Discriminant (Typ);
3126 -- First find the discriminant values in the positional components
3128 while Present (Discrim) and then Present (Positional_Expr) loop
3129 if Discr_Present (Discrim) then
3130 Resolve_Aggr_Expr (Positional_Expr, Discrim);
3132 -- Ada 2005 (AI-231)
3134 if Ada_Version >= Ada_05
3135 and then Known_Null (Positional_Expr)
3137 Check_Can_Never_Be_Null (Discrim, Positional_Expr);
3140 Next (Positional_Expr);
3143 if Present (Get_Value (Discrim, Component_Associations (N))) then
3145 ("more than one value supplied for discriminant&",
3149 Next_Discriminant (Discrim);
3152 -- Find remaining discriminant values, if any, among named components
3154 while Present (Discrim) loop
3155 Expr := Get_Value (Discrim, Component_Associations (N), True);
3157 if not Discr_Present (Discrim) then
3158 if Present (Expr) then
3160 ("more than one value supplied for discriminant&",
3164 elsif No (Expr) then
3166 ("no value supplied for discriminant &", N, Discrim);
3167 Missing_Discriminants := True;
3170 Resolve_Aggr_Expr (Expr, Discrim);
3173 Next_Discriminant (Discrim);
3176 if Missing_Discriminants then
3180 -- At this point and until the beginning of STEP 6, New_Assoc_List
3181 -- contains only the discriminants and their values.
3185 -- STEP 4: Set the Etype of the record aggregate
3187 -- ??? This code is pretty much a copy of Sem_Ch3.Build_Subtype. That
3188 -- routine should really be exported in sem_util or some such and used
3189 -- in sem_ch3 and here rather than have a copy of the code which is a
3190 -- maintenance nightmare.
3192 -- ??? Performance WARNING. The current implementation creates a new
3193 -- itype for all aggregates whose base type is discriminated.
3194 -- This means that for record aggregates nested inside an array
3195 -- aggregate we will create a new itype for each record aggregate
3196 -- if the array component type has discriminants. For large aggregates
3197 -- this may be a problem. What should be done in this case is
3198 -- to reuse itypes as much as possible.
3200 if Has_Discriminants (Typ)
3201 or else (Has_Unknown_Discriminants (Typ)
3202 and then Present (Underlying_Record_View (Typ)))
3204 Build_Constrained_Itype : declare
3205 Loc : constant Source_Ptr := Sloc (N);
3207 Subtyp_Decl : Node_Id;
3210 C : constant List_Id := New_List;
3213 New_Assoc := First (New_Assoc_List);
3214 while Present (New_Assoc) loop
3215 Append (Duplicate_Subexpr (Expression (New_Assoc)), To => C);
3219 if Has_Unknown_Discriminants (Typ)
3220 and then Present (Underlying_Record_View (Typ))
3223 Make_Subtype_Indication (Loc,
3225 New_Occurrence_Of (Underlying_Record_View (Typ), Loc),
3227 Make_Index_Or_Discriminant_Constraint (Loc, C));
3230 Make_Subtype_Indication (Loc,
3232 New_Occurrence_Of (Base_Type (Typ), Loc),
3234 Make_Index_Or_Discriminant_Constraint (Loc, C));
3237 Def_Id := Create_Itype (Ekind (Typ), N);
3240 Make_Subtype_Declaration (Loc,
3241 Defining_Identifier => Def_Id,
3242 Subtype_Indication => Indic);
3243 Set_Parent (Subtyp_Decl, Parent (N));
3245 -- Itypes must be analyzed with checks off (see itypes.ads)
3247 Analyze (Subtyp_Decl, Suppress => All_Checks);
3249 Set_Etype (N, Def_Id);
3250 Check_Static_Discriminated_Subtype
3251 (Def_Id, Expression (First (New_Assoc_List)));
3252 end Build_Constrained_Itype;
3258 -- STEP 5: Get remaining components according to discriminant values
3261 Record_Def : Node_Id;
3262 Parent_Typ : Entity_Id;
3263 Root_Typ : Entity_Id;
3264 Parent_Typ_List : Elist_Id;
3265 Parent_Elmt : Elmt_Id;
3266 Errors_Found : Boolean := False;
3270 if Is_Derived_Type (Typ) and then Is_Tagged_Type (Typ) then
3271 Parent_Typ_List := New_Elmt_List;
3273 -- If this is an extension aggregate, the component list must
3274 -- include all components that are not in the given ancestor type.
3275 -- Otherwise, the component list must include components of all
3276 -- ancestors, starting with the root.
3278 if Nkind (N) = N_Extension_Aggregate then
3279 Root_Typ := Base_Type (Etype (Ancestor_Part (N)));
3282 Root_Typ := Root_Type (Typ);
3284 if Nkind (Parent (Base_Type (Root_Typ))) =
3285 N_Private_Type_Declaration
3288 ("type of aggregate has private ancestor&!",
3290 Error_Msg_N ("must use extension aggregate!", N);
3294 Dnode := Declaration_Node (Base_Type (Root_Typ));
3296 -- If we don't get a full declaration, then we have some error
3297 -- which will get signalled later so skip this part. Otherwise
3298 -- gather components of root that apply to the aggregate type.
3299 -- We use the base type in case there is an applicable stored
3300 -- constraint that renames the discriminants of the root.
3302 if Nkind (Dnode) = N_Full_Type_Declaration then
3303 Record_Def := Type_Definition (Dnode);
3304 Gather_Components (Base_Type (Typ),
3305 Component_List (Record_Def),
3306 Governed_By => New_Assoc_List,
3308 Report_Errors => Errors_Found);
3312 Parent_Typ := Base_Type (Typ);
3313 while Parent_Typ /= Root_Typ loop
3314 Prepend_Elmt (Parent_Typ, To => Parent_Typ_List);
3315 Parent_Typ := Etype (Parent_Typ);
3317 if Nkind (Parent (Base_Type (Parent_Typ))) =
3318 N_Private_Type_Declaration
3319 or else Nkind (Parent (Base_Type (Parent_Typ))) =
3320 N_Private_Extension_Declaration
3322 if Nkind (N) /= N_Extension_Aggregate then
3324 ("type of aggregate has private ancestor&!",
3326 Error_Msg_N ("must use extension aggregate!", N);
3329 elsif Parent_Typ /= Root_Typ then
3331 ("ancestor part of aggregate must be private type&",
3332 Ancestor_Part (N), Parent_Typ);
3336 -- The current view of ancestor part may be a private type,
3337 -- while the context type is always non-private.
3339 elsif Is_Private_Type (Root_Typ)
3340 and then Present (Full_View (Root_Typ))
3341 and then Nkind (N) = N_Extension_Aggregate
3343 exit when Base_Type (Full_View (Root_Typ)) = Parent_Typ;
3347 -- Now collect components from all other ancestors, beginning
3348 -- with the current type. If the type has unknown discriminants
3349 -- use the component list of the Underlying_Record_View, which
3350 -- needs to be used for the subsequent expansion of the aggregate
3351 -- into assignments.
3353 Parent_Elmt := First_Elmt (Parent_Typ_List);
3354 while Present (Parent_Elmt) loop
3355 Parent_Typ := Node (Parent_Elmt);
3357 if Has_Unknown_Discriminants (Parent_Typ)
3358 and then Present (Underlying_Record_View (Typ))
3360 Parent_Typ := Underlying_Record_View (Parent_Typ);
3363 Record_Def := Type_Definition (Parent (Base_Type (Parent_Typ)));
3364 Gather_Components (Empty,
3365 Component_List (Record_Extension_Part (Record_Def)),
3366 Governed_By => New_Assoc_List,
3368 Report_Errors => Errors_Found);
3370 Next_Elmt (Parent_Elmt);
3374 Record_Def := Type_Definition (Parent (Base_Type (Typ)));
3376 if Null_Present (Record_Def) then
3379 elsif not Has_Unknown_Discriminants (Typ) then
3380 Gather_Components (Base_Type (Typ),
3381 Component_List (Record_Def),
3382 Governed_By => New_Assoc_List,
3384 Report_Errors => Errors_Found);
3388 (Base_Type (Underlying_Record_View (Typ)),
3389 Component_List (Record_Def),
3390 Governed_By => New_Assoc_List,
3392 Report_Errors => Errors_Found);
3396 if Errors_Found then
3401 -- STEP 6: Find component Values
3404 Component_Elmt := First_Elmt (Components);
3406 -- First scan the remaining positional associations in the aggregate.
3407 -- Remember that at this point Positional_Expr contains the current
3408 -- positional association if any is left after looking for discriminant
3409 -- values in step 3.
3411 while Present (Positional_Expr) and then Present (Component_Elmt) loop
3412 Component := Node (Component_Elmt);
3413 Resolve_Aggr_Expr (Positional_Expr, Component);
3415 -- Ada 2005 (AI-231)
3417 if Ada_Version >= Ada_05
3418 and then Known_Null (Positional_Expr)
3420 Check_Can_Never_Be_Null (Component, Positional_Expr);
3423 if Present (Get_Value (Component, Component_Associations (N))) then
3425 ("more than one value supplied for Component &", N, Component);
3428 Next (Positional_Expr);
3429 Next_Elmt (Component_Elmt);
3432 if Present (Positional_Expr) then
3434 ("too many components for record aggregate", Positional_Expr);
3437 -- Now scan for the named arguments of the aggregate
3439 while Present (Component_Elmt) loop
3440 Component := Node (Component_Elmt);
3441 Expr := Get_Value (Component, Component_Associations (N), True);
3443 -- Note: The previous call to Get_Value sets the value of the
3444 -- variable Is_Box_Present.
3446 -- Ada 2005 (AI-287): Handle components with default initialization.
3447 -- Note: This feature was originally added to Ada 2005 for limited
3448 -- but it was finally allowed with any type.
3450 if Is_Box_Present then
3451 Check_Box_Component : declare
3452 Ctyp : constant Entity_Id := Etype (Component);
3455 -- If there is a default expression for the aggregate, copy
3456 -- it into a new association.
3458 -- If the component has an initialization procedure (IP) we
3459 -- pass the component to the expander, which will generate
3460 -- the call to such IP.
3462 -- If the component has discriminants, their values must
3463 -- be taken from their subtype. This is indispensable for
3464 -- constraints that are given by the current instance of an
3465 -- enclosing type, to allow the expansion of the aggregate
3466 -- to replace the reference to the current instance by the
3467 -- target object of the aggregate.
3469 if Present (Parent (Component))
3471 Nkind (Parent (Component)) = N_Component_Declaration
3472 and then Present (Expression (Parent (Component)))
3475 New_Copy_Tree (Expression (Parent (Component)),
3476 New_Sloc => Sloc (N));
3479 (Component => Component,
3481 Assoc_List => New_Assoc_List);
3482 Set_Has_Self_Reference (N);
3484 -- A box-defaulted access component gets the value null. Also
3485 -- included are components of private types whose underlying
3486 -- type is an access type. In either case set the type of the
3487 -- literal, for subsequent use in semantic checks.
3489 elsif Present (Underlying_Type (Ctyp))
3490 and then Is_Access_Type (Underlying_Type (Ctyp))
3492 if not Is_Private_Type (Ctyp) then
3493 Expr := Make_Null (Sloc (N));
3494 Set_Etype (Expr, Ctyp);
3496 (Component => Component,
3498 Assoc_List => New_Assoc_List);
3500 -- If the component's type is private with an access type as
3501 -- its underlying type then we have to create an unchecked
3502 -- conversion to satisfy type checking.
3506 Qual_Null : constant Node_Id :=
3507 Make_Qualified_Expression (Sloc (N),
3510 (Underlying_Type (Ctyp), Sloc (N)),
3511 Expression => Make_Null (Sloc (N)));
3513 Convert_Null : constant Node_Id :=
3514 Unchecked_Convert_To
3518 Analyze_And_Resolve (Convert_Null, Ctyp);
3520 (Component => Component,
3521 Expr => Convert_Null,
3522 Assoc_List => New_Assoc_List);
3526 elsif Has_Non_Null_Base_Init_Proc (Ctyp)
3527 or else not Expander_Active
3529 if Is_Record_Type (Ctyp)
3530 and then Has_Discriminants (Ctyp)
3531 and then not Is_Private_Type (Ctyp)
3533 -- We build a partially initialized aggregate with the
3534 -- values of the discriminants and box initialization
3535 -- for the rest, if other components are present.
3536 -- The type of the aggregate is the known subtype of
3537 -- the component. The capture of discriminants must
3538 -- be recursive because subcomponents may be contrained
3539 -- (transitively) by discriminants of enclosing types.
3540 -- For a private type with discriminants, a call to the
3541 -- initialization procedure will be generated, and no
3542 -- subaggregate is needed.
3544 Capture_Discriminants : declare
3545 Loc : constant Source_Ptr := Sloc (N);
3548 procedure Add_Discriminant_Values
3549 (New_Aggr : Node_Id;
3550 Assoc_List : List_Id);
3551 -- The constraint to a component may be given by a
3552 -- discriminant of the enclosing type, in which case
3553 -- we have to retrieve its value, which is part of the
3554 -- enclosing aggregate. Assoc_List provides the
3555 -- discriminant associations of the current type or
3556 -- of some enclosing record.
3558 procedure Propagate_Discriminants
3560 Assoc_List : List_Id;
3562 -- Nested components may themselves be discriminated
3563 -- types constrained by outer discriminants, whose
3564 -- values must be captured before the aggregate is
3565 -- expanded into assignments.
3567 -----------------------------
3568 -- Add_Discriminant_Values --
3569 -----------------------------
3571 procedure Add_Discriminant_Values
3572 (New_Aggr : Node_Id;
3573 Assoc_List : List_Id)
3577 Discr_Elmt : Elmt_Id;
3578 Discr_Val : Node_Id;
3582 Discr := First_Discriminant (Etype (New_Aggr));
3585 (Discriminant_Constraint (Etype (New_Aggr)));
3586 while Present (Discr_Elmt) loop
3587 Discr_Val := Node (Discr_Elmt);
3589 -- If the constraint is given by a discriminant
3590 -- it is a discriminant of an enclosing record,
3591 -- and its value has already been placed in the
3592 -- association list.
3594 if Is_Entity_Name (Discr_Val)
3596 Ekind (Entity (Discr_Val)) = E_Discriminant
3598 Val := Entity (Discr_Val);
3600 Assoc := First (Assoc_List);
3601 while Present (Assoc) loop
3603 (Entity (First (Choices (Assoc))))
3605 Entity (First (Choices (Assoc)))
3608 Discr_Val := Expression (Assoc);
3616 (Discr, New_Copy_Tree (Discr_Val),
3617 Component_Associations (New_Aggr));
3619 -- If the discriminant constraint is a current
3620 -- instance, mark the current aggregate so that
3621 -- the self-reference can be expanded later.
3623 if Nkind (Discr_Val) = N_Attribute_Reference
3624 and then Is_Entity_Name (Prefix (Discr_Val))
3625 and then Is_Type (Entity (Prefix (Discr_Val)))
3626 and then Etype (N) =
3627 Entity (Prefix (Discr_Val))
3629 Set_Has_Self_Reference (N);
3632 Next_Elmt (Discr_Elmt);
3633 Next_Discriminant (Discr);
3635 end Add_Discriminant_Values;
3637 ------------------------------
3638 -- Propagate_Discriminants --
3639 ------------------------------
3641 procedure Propagate_Discriminants
3643 Assoc_List : List_Id;
3646 Inner_Comp : Entity_Id;
3647 Comp_Type : Entity_Id;
3648 Needs_Box : Boolean := False;
3652 Inner_Comp := First_Component (Etype (Comp));
3653 while Present (Inner_Comp) loop
3654 Comp_Type := Etype (Inner_Comp);
3656 if Is_Record_Type (Comp_Type)
3657 and then Has_Discriminants (Comp_Type)
3660 Make_Aggregate (Loc, New_List, New_List);
3661 Set_Etype (New_Aggr, Comp_Type);
3663 (Inner_Comp, New_Aggr,
3664 Component_Associations (Aggr));
3666 -- Collect discriminant values and recurse
3668 Add_Discriminant_Values
3669 (New_Aggr, Assoc_List);
3670 Propagate_Discriminants
3671 (New_Aggr, Assoc_List, Inner_Comp);
3677 Next_Component (Inner_Comp);
3682 (Make_Component_Association (Loc,
3684 New_List (Make_Others_Choice (Loc)),
3685 Expression => Empty,
3686 Box_Present => True),
3687 Component_Associations (Aggr));
3689 end Propagate_Discriminants;
3692 Expr := Make_Aggregate (Loc, New_List, New_List);
3693 Set_Etype (Expr, Ctyp);
3695 -- If the enclosing type has discriminants, they
3696 -- have been collected in the aggregate earlier, and
3697 -- they may appear as constraints of subcomponents.
3698 -- Similarly if this component has discriminants, they
3699 -- might in turn be propagated to their components.
3701 if Has_Discriminants (Typ) then
3702 Add_Discriminant_Values (Expr, New_Assoc_List);
3703 Propagate_Discriminants
3704 (Expr, New_Assoc_List, Component);
3706 elsif Has_Discriminants (Ctyp) then
3707 Add_Discriminant_Values
3708 (Expr, Component_Associations (Expr));
3709 Propagate_Discriminants
3710 (Expr, Component_Associations (Expr), Component);
3717 -- If the type has additional components, create
3718 -- an OTHERS box association for them.
3720 Comp := First_Component (Ctyp);
3721 while Present (Comp) loop
3722 if Ekind (Comp) = E_Component then
3723 if not Is_Record_Type (Etype (Comp)) then
3725 (Make_Component_Association (Loc,
3728 (Make_Others_Choice (Loc)),
3729 Expression => Empty,
3730 Box_Present => True),
3731 Component_Associations (Expr));
3736 Next_Component (Comp);
3742 (Component => Component,
3744 Assoc_List => New_Assoc_List);
3745 end Capture_Discriminants;
3749 (Component => Component,
3751 Assoc_List => New_Assoc_List,
3752 Is_Box_Present => True);
3755 -- Otherwise we only need to resolve the expression if the
3756 -- component has partially initialized values (required to
3757 -- expand the corresponding assignments and run-time checks).
3759 elsif Present (Expr)
3760 and then Is_Partially_Initialized_Type (Ctyp)
3762 Resolve_Aggr_Expr (Expr, Component);
3764 end Check_Box_Component;
3766 elsif No (Expr) then
3768 -- Ignore hidden components associated with the position of the
3769 -- interface tags: these are initialized dynamically.
3771 if not Present (Related_Type (Component)) then
3773 ("no value supplied for component &!", N, Component);
3777 Resolve_Aggr_Expr (Expr, Component);
3780 Next_Elmt (Component_Elmt);
3783 -- STEP 7: check for invalid components + check type in choice list
3790 -- Type of first component in choice list
3793 if Present (Component_Associations (N)) then
3794 Assoc := First (Component_Associations (N));
3799 Verification : while Present (Assoc) loop
3800 Selectr := First (Choices (Assoc));
3803 if Nkind (Selectr) = N_Others_Choice then
3805 -- Ada 2005 (AI-287): others choice may have expression or box
3807 if No (Others_Etype)
3808 and then not Others_Box
3811 ("OTHERS must represent at least one component", Selectr);
3817 while Present (Selectr) loop
3818 New_Assoc := First (New_Assoc_List);
3819 while Present (New_Assoc) loop
3820 Component := First (Choices (New_Assoc));
3822 if Chars (Selectr) = Chars (Component) then
3824 Check_Identifier (Selectr, Entity (Component));
3833 -- If no association, this is not a legal component of
3834 -- of the type in question, except if its association
3835 -- is provided with a box.
3837 if No (New_Assoc) then
3838 if Box_Present (Parent (Selectr)) then
3840 -- This may still be a bogus component with a box. Scan
3841 -- list of components to verify that a component with
3842 -- that name exists.
3848 C := First_Component (Typ);
3849 while Present (C) loop
3850 if Chars (C) = Chars (Selectr) then
3852 -- If the context is an extension aggregate,
3853 -- the component must not be inherited from
3854 -- the ancestor part of the aggregate.
3856 if Nkind (N) /= N_Extension_Aggregate
3858 Scope (Original_Record_Component (C)) /=
3859 Etype (Ancestor_Part (N))
3869 Error_Msg_Node_2 := Typ;
3870 Error_Msg_N ("& is not a component of}", Selectr);
3874 elsif Chars (Selectr) /= Name_uTag
3875 and then Chars (Selectr) /= Name_uParent
3876 and then Chars (Selectr) /= Name_uController
3878 if not Has_Discriminants (Typ) then
3879 Error_Msg_Node_2 := Typ;
3880 Error_Msg_N ("& is not a component of}", Selectr);
3883 ("& is not a component of the aggregate subtype",
3887 Check_Misspelled_Component (Components, Selectr);
3890 elsif No (Typech) then
3891 Typech := Base_Type (Etype (Component));
3893 -- AI05-0199: In Ada2012, several components of anonymous
3894 -- access types can appear in a choice list, as long as the
3895 -- designated types match.
3897 elsif Typech /= Base_Type (Etype (Component)) then
3898 if Ada_Version >= Ada_12
3899 and then Ekind (Typech) = E_Anonymous_Access_Type
3901 Ekind (Etype (Component)) = E_Anonymous_Access_Type
3902 and then Base_Type (Designated_Type (Typech)) =
3903 Base_Type (Designated_Type (Etype (Component)))
3905 Subtypes_Statically_Match (Typech, (Etype (Component)))
3909 elsif not Box_Present (Parent (Selectr)) then
3911 ("components in choice list must have same type",
3920 end loop Verification;
3923 -- STEP 8: replace the original aggregate
3926 New_Aggregate : constant Node_Id := New_Copy (N);
3929 Set_Expressions (New_Aggregate, No_List);
3930 Set_Etype (New_Aggregate, Etype (N));
3931 Set_Component_Associations (New_Aggregate, New_Assoc_List);
3933 Rewrite (N, New_Aggregate);
3935 end Resolve_Record_Aggregate;
3937 -----------------------------
3938 -- Check_Can_Never_Be_Null --
3939 -----------------------------
3941 procedure Check_Can_Never_Be_Null (Typ : Entity_Id; Expr : Node_Id) is
3942 Comp_Typ : Entity_Id;
3946 (Ada_Version >= Ada_05
3947 and then Present (Expr)
3948 and then Known_Null (Expr));
3951 when E_Array_Type =>
3952 Comp_Typ := Component_Type (Typ);
3956 Comp_Typ := Etype (Typ);
3962 if Can_Never_Be_Null (Comp_Typ) then
3964 -- Here we know we have a constraint error. Note that we do not use
3965 -- Apply_Compile_Time_Constraint_Error here to the Expr, which might
3966 -- seem the more natural approach. That's because in some cases the
3967 -- components are rewritten, and the replacement would be missed.
3970 (Compile_Time_Constraint_Error
3972 "(Ada 2005) null not allowed in null-excluding component?"),
3973 Make_Raise_Constraint_Error (Sloc (Expr),
3974 Reason => CE_Access_Check_Failed));
3976 -- Set proper type for bogus component (why is this needed???)
3978 Set_Etype (Expr, Comp_Typ);
3979 Set_Analyzed (Expr);
3981 end Check_Can_Never_Be_Null;
3983 ---------------------
3984 -- Sort_Case_Table --
3985 ---------------------
3987 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is
3988 L : constant Int := Case_Table'First;
3989 U : constant Int := Case_Table'Last;
3997 T := Case_Table (K + 1);
4001 and then Expr_Value (Case_Table (J - 1).Choice_Lo) >
4002 Expr_Value (T.Choice_Lo)
4004 Case_Table (J) := Case_Table (J - 1);
4008 Case_Table (J) := T;
4011 end Sort_Case_Table;