1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2009, 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 Targparm; use Targparm;
58 with Tbuild; use Tbuild;
59 with Uintp; use Uintp;
61 package body Sem_Aggr is
63 type Case_Bounds is record
66 Choice_Node : Node_Id;
69 type Case_Table_Type is array (Nat range <>) of Case_Bounds;
70 -- Table type used by Check_Case_Choices procedure
72 -----------------------
73 -- Local Subprograms --
74 -----------------------
76 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type);
77 -- Sort the Case Table using the Lower Bound of each Choice as the key.
78 -- A simple insertion sort is used since the number of choices in a case
79 -- statement of variant part will usually be small and probably in near
82 procedure Check_Can_Never_Be_Null (Typ : Entity_Id; Expr : Node_Id);
83 -- Ada 2005 (AI-231): Check bad usage of null for a component for which
84 -- null exclusion (NOT NULL) is specified. Typ can be an E_Array_Type for
85 -- the array case (the component type of the array will be used) or an
86 -- E_Component/E_Discriminant entity in the record case, in which case the
87 -- type of the component will be used for the test. If Typ is any other
88 -- kind of entity, the call is ignored. Expr is the component node in the
89 -- aggregate which is known to have a null value. A warning message will be
90 -- issued if the component is null excluding.
92 -- It would be better to pass the proper type for Typ ???
94 procedure Check_Expr_OK_In_Limited_Aggregate (Expr : Node_Id);
95 -- Check that Expr is either not limited or else is one of the cases of
96 -- expressions allowed for a limited component association (namely, an
97 -- aggregate, function call, or <> notation). Report error for violations.
99 ------------------------------------------------------
100 -- Subprograms used for RECORD AGGREGATE Processing --
101 ------------------------------------------------------
103 procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id);
104 -- This procedure performs all the semantic checks required for record
105 -- aggregates. Note that for aggregates analysis and resolution go
106 -- hand in hand. Aggregate analysis has been delayed up to here and
107 -- it is done while resolving the aggregate.
109 -- N is the N_Aggregate node.
110 -- Typ is the record type for the aggregate resolution
112 -- While performing the semantic checks, this procedure builds a new
113 -- Component_Association_List where each record field appears alone in a
114 -- Component_Choice_List along with its corresponding expression. The
115 -- record fields in the Component_Association_List appear in the same order
116 -- in which they appear in the record type Typ.
118 -- Once this new Component_Association_List is built and all the semantic
119 -- checks performed, the original aggregate subtree is replaced with the
120 -- new named record aggregate just built. Note that subtree substitution is
121 -- performed with Rewrite so as to be able to retrieve the original
124 -- The aggregate subtree manipulation performed by Resolve_Record_Aggregate
125 -- yields the aggregate format expected by Gigi. Typically, this kind of
126 -- tree manipulations are done in the expander. However, because the
127 -- semantic checks that need to be performed on record aggregates really go
128 -- hand in hand with the record aggregate normalization, the aggregate
129 -- subtree transformation is performed during resolution rather than
130 -- expansion. Had we decided otherwise we would have had to duplicate most
131 -- of the code in the expansion procedure Expand_Record_Aggregate. Note,
132 -- however, that all the expansion concerning aggregates for tagged records
133 -- is done in Expand_Record_Aggregate.
135 -- The algorithm of Resolve_Record_Aggregate proceeds as follows:
137 -- 1. Make sure that the record type against which the record aggregate
138 -- has to be resolved is not abstract. Furthermore if the type is a
139 -- null aggregate make sure the input aggregate N is also null.
141 -- 2. Verify that the structure of the aggregate is that of a record
142 -- aggregate. Specifically, look for component associations and ensure
143 -- that each choice list only has identifiers or the N_Others_Choice
144 -- node. Also make sure that if present, the N_Others_Choice occurs
145 -- last and by itself.
147 -- 3. If Typ contains discriminants, the values for each discriminant is
148 -- looked for. If the record type Typ has variants, we check that the
149 -- expressions corresponding to each discriminant ruling the (possibly
150 -- nested) variant parts of Typ, are static. This allows us to determine
151 -- the variant parts to which the rest of the aggregate must conform.
152 -- The names of discriminants with their values are saved in a new
153 -- association list, New_Assoc_List which is later augmented with the
154 -- names and values of the remaining components in the record type.
156 -- During this phase we also make sure that every discriminant is
157 -- assigned exactly one value. Note that when several values for a given
158 -- discriminant are found, semantic processing continues looking for
159 -- further errors. In this case it's the first discriminant value found
160 -- which we will be recorded.
162 -- IMPORTANT NOTE: For derived tagged types this procedure expects
163 -- First_Discriminant and Next_Discriminant to give the correct list
164 -- of discriminants, in the correct order.
166 -- 4. After all the discriminant values have been gathered, we can set the
167 -- Etype of the record aggregate. If Typ contains no discriminants this
168 -- is straightforward: the Etype of N is just Typ, otherwise a new
169 -- implicit constrained subtype of Typ is built to be the Etype of N.
171 -- 5. Gather the remaining record components according to the discriminant
172 -- values. This involves recursively traversing the record type
173 -- structure to see what variants are selected by the given discriminant
174 -- values. This processing is a little more convoluted if Typ is a
175 -- derived tagged types since we need to retrieve the record structure
176 -- of all the ancestors of Typ.
178 -- 6. After gathering the record components we look for their values in the
179 -- record aggregate and emit appropriate error messages should we not
180 -- find such values or should they be duplicated.
182 -- 7. We then make sure no illegal component names appear in the record
183 -- aggregate and make sure that the type of the record components
184 -- appearing in a same choice list is the same. Finally we ensure that
185 -- the others choice, if present, is used to provide the value of at
186 -- least a record component.
188 -- 8. The original aggregate node is replaced with the new named aggregate
189 -- built in steps 3 through 6, as explained earlier.
191 -- Given the complexity of record aggregate resolution, the primary goal of
192 -- this routine is clarity and simplicity rather than execution and storage
193 -- efficiency. If there are only positional components in the aggregate the
194 -- running time is linear. If there are associations the running time is
195 -- still linear as long as the order of the associations is not too far off
196 -- the order of the components in the record type. If this is not the case
197 -- the running time is at worst quadratic in the size of the association
200 procedure Check_Misspelled_Component
201 (Elements : Elist_Id;
202 Component : Node_Id);
203 -- Give possible misspelling diagnostic if Component is likely to be a
204 -- misspelling of one of the components of the Assoc_List. This is called
205 -- by Resolve_Aggr_Expr after producing an invalid component error message.
207 procedure Check_Static_Discriminated_Subtype (T : Entity_Id; V : Node_Id);
208 -- An optimization: determine whether a discriminated subtype has a static
209 -- constraint, and contains array components whose length is also static,
210 -- either because they are constrained by the discriminant, or because the
211 -- original component bounds are static.
213 -----------------------------------------------------
214 -- Subprograms used for ARRAY AGGREGATE Processing --
215 -----------------------------------------------------
217 function Resolve_Array_Aggregate
220 Index_Constr : Node_Id;
221 Component_Typ : Entity_Id;
222 Others_Allowed : Boolean) return Boolean;
223 -- This procedure performs the semantic checks for an array aggregate.
224 -- True is returned if the aggregate resolution succeeds.
226 -- The procedure works by recursively checking each nested aggregate.
227 -- Specifically, after checking a sub-aggregate nested at the i-th level
228 -- we recursively check all the subaggregates at the i+1-st level (if any).
229 -- Note that for aggregates analysis and resolution go hand in hand.
230 -- Aggregate analysis has been delayed up to here and it is done while
231 -- resolving the aggregate.
233 -- N is the current N_Aggregate node to be checked.
235 -- Index is the index node corresponding to the array sub-aggregate that
236 -- we are currently checking (RM 4.3.3 (8)). Its Etype is the
237 -- corresponding index type (or subtype).
239 -- Index_Constr is the node giving the applicable index constraint if
240 -- any (RM 4.3.3 (10)). It "is a constraint provided by certain
241 -- contexts [...] that can be used to determine the bounds of the array
242 -- value specified by the aggregate". If Others_Allowed below is False
243 -- there is no applicable index constraint and this node is set to Index.
245 -- Component_Typ is the array component type.
247 -- Others_Allowed indicates whether an others choice is allowed
248 -- in the context where the top-level aggregate appeared.
250 -- The algorithm of Resolve_Array_Aggregate proceeds as follows:
252 -- 1. Make sure that the others choice, if present, is by itself and
253 -- appears last in the sub-aggregate. Check that we do not have
254 -- positional and named components in the array sub-aggregate (unless
255 -- the named association is an others choice). Finally if an others
256 -- choice is present, make sure it is allowed in the aggregate context.
258 -- 2. If the array sub-aggregate contains discrete_choices:
260 -- (A) Verify their validity. Specifically verify that:
262 -- (a) If a null range is present it must be the only possible
263 -- choice in the array aggregate.
265 -- (b) Ditto for a non static range.
267 -- (c) Ditto for a non static expression.
269 -- In addition this step analyzes and resolves each discrete_choice,
270 -- making sure that its type is the type of the corresponding Index.
271 -- If we are not at the lowest array aggregate level (in the case of
272 -- multi-dimensional aggregates) then invoke Resolve_Array_Aggregate
273 -- recursively on each component expression. Otherwise, resolve the
274 -- bottom level component expressions against the expected component
275 -- type ONLY IF the component corresponds to a single discrete choice
276 -- which is not an others choice (to see why read the DELAYED
277 -- COMPONENT RESOLUTION below).
279 -- (B) Determine the bounds of the sub-aggregate and lowest and
280 -- highest choice values.
282 -- 3. For positional aggregates:
284 -- (A) Loop over the component expressions either recursively invoking
285 -- Resolve_Array_Aggregate on each of these for multi-dimensional
286 -- array aggregates or resolving the bottom level component
287 -- expressions against the expected component type.
289 -- (B) Determine the bounds of the positional sub-aggregates.
291 -- 4. Try to determine statically whether the evaluation of the array
292 -- sub-aggregate raises Constraint_Error. If yes emit proper
293 -- warnings. The precise checks are the following:
295 -- (A) Check that the index range defined by aggregate bounds is
296 -- compatible with corresponding index subtype.
297 -- We also check against the base type. In fact it could be that
298 -- Low/High bounds of the base type are static whereas those of
299 -- the index subtype are not. Thus if we can statically catch
300 -- a problem with respect to the base type we are guaranteed
301 -- that the same problem will arise with the index subtype
303 -- (B) If we are dealing with a named aggregate containing an others
304 -- choice and at least one discrete choice then make sure the range
305 -- specified by the discrete choices does not overflow the
306 -- aggregate bounds. We also check against the index type and base
307 -- type bounds for the same reasons given in (A).
309 -- (C) If we are dealing with a positional aggregate with an others
310 -- choice make sure the number of positional elements specified
311 -- does not overflow the aggregate bounds. We also check against
312 -- the index type and base type bounds as mentioned in (A).
314 -- Finally construct an N_Range node giving the sub-aggregate bounds.
315 -- Set the Aggregate_Bounds field of the sub-aggregate to be this
316 -- N_Range. The routine Array_Aggr_Subtype below uses such N_Ranges
317 -- to build the appropriate aggregate subtype. Aggregate_Bounds
318 -- information is needed during expansion.
320 -- DELAYED COMPONENT RESOLUTION: The resolution of bottom level component
321 -- expressions in an array aggregate may call Duplicate_Subexpr or some
322 -- other routine that inserts code just outside the outermost aggregate.
323 -- If the array aggregate contains discrete choices or an others choice,
324 -- this may be wrong. Consider for instance the following example.
326 -- type Rec is record
330 -- type Acc_Rec is access Rec;
331 -- Arr : array (1..3) of Acc_Rec := (1 .. 3 => new Rec);
333 -- Then the transformation of "new Rec" that occurs during resolution
334 -- entails the following code modifications
336 -- P7b : constant Acc_Rec := new Rec;
338 -- Arr : array (1..3) of Acc_Rec := (1 .. 3 => P7b);
340 -- This code transformation is clearly wrong, since we need to call
341 -- "new Rec" for each of the 3 array elements. To avoid this problem we
342 -- delay resolution of the components of non positional array aggregates
343 -- to the expansion phase. As an optimization, if the discrete choice
344 -- specifies a single value we do not delay resolution.
346 function Array_Aggr_Subtype (N : Node_Id; Typ : Node_Id) return Entity_Id;
347 -- This routine returns the type or subtype of an array aggregate.
349 -- N is the array aggregate node whose type we return.
351 -- Typ is the context type in which N occurs.
353 -- This routine creates an implicit array subtype whose bounds are
354 -- those defined by the aggregate. When this routine is invoked
355 -- Resolve_Array_Aggregate has already processed aggregate N. Thus the
356 -- Aggregate_Bounds of each sub-aggregate, is an N_Range node giving the
357 -- sub-aggregate bounds. When building the aggregate itype, this function
358 -- traverses the array aggregate N collecting such Aggregate_Bounds and
359 -- constructs the proper array aggregate itype.
361 -- Note that in the case of multidimensional aggregates each inner
362 -- sub-aggregate corresponding to a given array dimension, may provide a
363 -- different bounds. If it is possible to determine statically that
364 -- some sub-aggregates corresponding to the same index do not have the
365 -- same bounds, then a warning is emitted. If such check is not possible
366 -- statically (because some sub-aggregate bounds are dynamic expressions)
367 -- then this job is left to the expander. In all cases the particular
368 -- bounds that this function will chose for a given dimension is the first
369 -- N_Range node for a sub-aggregate corresponding to that dimension.
371 -- Note that the Raises_Constraint_Error flag of an array aggregate
372 -- whose evaluation is determined to raise CE by Resolve_Array_Aggregate,
373 -- is set in Resolve_Array_Aggregate but the aggregate is not
374 -- immediately replaced with a raise CE. In fact, Array_Aggr_Subtype must
375 -- first construct the proper itype for the aggregate (Gigi needs
376 -- this). After constructing the proper itype we will eventually replace
377 -- the top-level aggregate with a raise CE (done in Resolve_Aggregate).
378 -- Of course in cases such as:
380 -- type Arr is array (integer range <>) of Integer;
381 -- A : Arr := (positive range -1 .. 2 => 0);
383 -- The bounds of the aggregate itype are cooked up to look reasonable
384 -- (in this particular case the bounds will be 1 .. 2).
386 procedure Aggregate_Constraint_Checks
388 Check_Typ : Entity_Id);
389 -- Checks expression Exp against subtype Check_Typ. If Exp is an
390 -- aggregate and Check_Typ a constrained record type with discriminants,
391 -- we generate the appropriate discriminant checks. If Exp is an array
392 -- aggregate then emit the appropriate length checks. If Exp is a scalar
393 -- type, or a string literal, Exp is changed into Check_Typ'(Exp) to
394 -- ensure that range checks are performed at run time.
396 procedure Make_String_Into_Aggregate (N : Node_Id);
397 -- A string literal can appear in a context in which a one dimensional
398 -- array of characters is expected. This procedure simply rewrites the
399 -- string as an aggregate, prior to resolution.
401 ---------------------------------
402 -- Aggregate_Constraint_Checks --
403 ---------------------------------
405 procedure Aggregate_Constraint_Checks
407 Check_Typ : Entity_Id)
409 Exp_Typ : constant Entity_Id := Etype (Exp);
412 if Raises_Constraint_Error (Exp) then
416 -- Ada 2005 (AI-230): Generate a conversion to an anonymous access
417 -- component's type to force the appropriate accessibility checks.
419 -- Ada 2005 (AI-231): Generate conversion to the null-excluding
420 -- type to force the corresponding run-time check
422 if Is_Access_Type (Check_Typ)
423 and then ((Is_Local_Anonymous_Access (Check_Typ))
424 or else (Can_Never_Be_Null (Check_Typ)
425 and then not Can_Never_Be_Null (Exp_Typ)))
427 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
428 Analyze_And_Resolve (Exp, Check_Typ);
429 Check_Unset_Reference (Exp);
432 -- This is really expansion activity, so make sure that expansion
433 -- is on and is allowed.
435 if not Expander_Active or else In_Spec_Expression then
439 -- First check if we have to insert discriminant checks
441 if Has_Discriminants (Exp_Typ) then
442 Apply_Discriminant_Check (Exp, Check_Typ);
444 -- Next emit length checks for array aggregates
446 elsif Is_Array_Type (Exp_Typ) then
447 Apply_Length_Check (Exp, Check_Typ);
449 -- Finally emit scalar and string checks. If we are dealing with a
450 -- scalar literal we need to check by hand because the Etype of
451 -- literals is not necessarily correct.
453 elsif Is_Scalar_Type (Exp_Typ)
454 and then Compile_Time_Known_Value (Exp)
456 if Is_Out_Of_Range (Exp, Base_Type (Check_Typ)) then
457 Apply_Compile_Time_Constraint_Error
458 (Exp, "value not in range of}?", CE_Range_Check_Failed,
459 Ent => Base_Type (Check_Typ),
460 Typ => Base_Type (Check_Typ));
462 elsif Is_Out_Of_Range (Exp, Check_Typ) then
463 Apply_Compile_Time_Constraint_Error
464 (Exp, "value not in range of}?", CE_Range_Check_Failed,
468 elsif not Range_Checks_Suppressed (Check_Typ) then
469 Apply_Scalar_Range_Check (Exp, Check_Typ);
472 -- Verify that target type is also scalar, to prevent view anomalies
473 -- in instantiations.
475 elsif (Is_Scalar_Type (Exp_Typ)
476 or else Nkind (Exp) = N_String_Literal)
477 and then Is_Scalar_Type (Check_Typ)
478 and then Exp_Typ /= Check_Typ
480 if Is_Entity_Name (Exp)
481 and then Ekind (Entity (Exp)) = E_Constant
483 -- If expression is a constant, it is worthwhile checking whether
484 -- it is a bound of the type.
486 if (Is_Entity_Name (Type_Low_Bound (Check_Typ))
487 and then Entity (Exp) = Entity (Type_Low_Bound (Check_Typ)))
488 or else (Is_Entity_Name (Type_High_Bound (Check_Typ))
489 and then Entity (Exp) = Entity (Type_High_Bound (Check_Typ)))
494 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
495 Analyze_And_Resolve (Exp, Check_Typ);
496 Check_Unset_Reference (Exp);
499 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
500 Analyze_And_Resolve (Exp, Check_Typ);
501 Check_Unset_Reference (Exp);
505 end Aggregate_Constraint_Checks;
507 ------------------------
508 -- Array_Aggr_Subtype --
509 ------------------------
511 function Array_Aggr_Subtype
513 Typ : Entity_Id) return Entity_Id
515 Aggr_Dimension : constant Pos := Number_Dimensions (Typ);
516 -- Number of aggregate index dimensions
518 Aggr_Range : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
519 -- Constrained N_Range of each index dimension in our aggregate itype
521 Aggr_Low : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
522 Aggr_High : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
523 -- Low and High bounds for each index dimension in our aggregate itype
525 Is_Fully_Positional : Boolean := True;
527 procedure Collect_Aggr_Bounds (N : Node_Id; Dim : Pos);
528 -- N is an array (sub-)aggregate. Dim is the dimension corresponding to
529 -- (sub-)aggregate N. This procedure collects the constrained N_Range
530 -- nodes corresponding to each index dimension of our aggregate itype.
531 -- These N_Range nodes are collected in Aggr_Range above.
533 -- Likewise collect in Aggr_Low & Aggr_High above the low and high
534 -- bounds of each index dimension. If, when collecting, two bounds
535 -- corresponding to the same dimension are static and found to differ,
536 -- then emit a warning, and mark N as raising Constraint_Error.
538 -------------------------
539 -- Collect_Aggr_Bounds --
540 -------------------------
542 procedure Collect_Aggr_Bounds (N : Node_Id; Dim : Pos) is
543 This_Range : constant Node_Id := Aggregate_Bounds (N);
544 -- The aggregate range node of this specific sub-aggregate
546 This_Low : constant Node_Id := Low_Bound (Aggregate_Bounds (N));
547 This_High : constant Node_Id := High_Bound (Aggregate_Bounds (N));
548 -- The aggregate bounds of this specific sub-aggregate
554 -- Collect the first N_Range for a given dimension that you find.
555 -- For a given dimension they must be all equal anyway.
557 if No (Aggr_Range (Dim)) then
558 Aggr_Low (Dim) := This_Low;
559 Aggr_High (Dim) := This_High;
560 Aggr_Range (Dim) := This_Range;
563 if Compile_Time_Known_Value (This_Low) then
564 if not Compile_Time_Known_Value (Aggr_Low (Dim)) then
565 Aggr_Low (Dim) := This_Low;
567 elsif Expr_Value (This_Low) /= Expr_Value (Aggr_Low (Dim)) then
568 Set_Raises_Constraint_Error (N);
569 Error_Msg_N ("sub-aggregate low bound mismatch?", N);
571 ("\Constraint_Error will be raised at run-time?", N);
575 if Compile_Time_Known_Value (This_High) then
576 if not Compile_Time_Known_Value (Aggr_High (Dim)) then
577 Aggr_High (Dim) := This_High;
580 Expr_Value (This_High) /= Expr_Value (Aggr_High (Dim))
582 Set_Raises_Constraint_Error (N);
583 Error_Msg_N ("sub-aggregate high bound mismatch?", N);
585 ("\Constraint_Error will be raised at run-time?", N);
590 if Dim < Aggr_Dimension then
592 -- Process positional components
594 if Present (Expressions (N)) then
595 Expr := First (Expressions (N));
596 while Present (Expr) loop
597 Collect_Aggr_Bounds (Expr, Dim + 1);
602 -- Process component associations
604 if Present (Component_Associations (N)) then
605 Is_Fully_Positional := False;
607 Assoc := First (Component_Associations (N));
608 while Present (Assoc) loop
609 Expr := Expression (Assoc);
610 Collect_Aggr_Bounds (Expr, Dim + 1);
615 end Collect_Aggr_Bounds;
617 -- Array_Aggr_Subtype variables
620 -- The final itype of the overall aggregate
622 Index_Constraints : constant List_Id := New_List;
623 -- The list of index constraints of the aggregate itype
625 -- Start of processing for Array_Aggr_Subtype
628 -- Make sure that the list of index constraints is properly attached to
629 -- the tree, and then collect the aggregate bounds.
631 Set_Parent (Index_Constraints, N);
632 Collect_Aggr_Bounds (N, 1);
634 -- Build the list of constrained indices of our aggregate itype
636 for J in 1 .. Aggr_Dimension loop
637 Create_Index : declare
638 Index_Base : constant Entity_Id :=
639 Base_Type (Etype (Aggr_Range (J)));
640 Index_Typ : Entity_Id;
643 -- Construct the Index subtype, and associate it with the range
644 -- construct that generates it.
647 Create_Itype (Subtype_Kind (Ekind (Index_Base)), Aggr_Range (J));
649 Set_Etype (Index_Typ, Index_Base);
651 if Is_Character_Type (Index_Base) then
652 Set_Is_Character_Type (Index_Typ);
655 Set_Size_Info (Index_Typ, (Index_Base));
656 Set_RM_Size (Index_Typ, RM_Size (Index_Base));
657 Set_First_Rep_Item (Index_Typ, First_Rep_Item (Index_Base));
658 Set_Scalar_Range (Index_Typ, Aggr_Range (J));
660 if Is_Discrete_Or_Fixed_Point_Type (Index_Typ) then
661 Set_RM_Size (Index_Typ, UI_From_Int (Minimum_Size (Index_Typ)));
664 Set_Etype (Aggr_Range (J), Index_Typ);
666 Append (Aggr_Range (J), To => Index_Constraints);
670 -- Now build the Itype
672 Itype := Create_Itype (E_Array_Subtype, N);
674 Set_First_Rep_Item (Itype, First_Rep_Item (Typ));
675 Set_Convention (Itype, Convention (Typ));
676 Set_Depends_On_Private (Itype, Has_Private_Component (Typ));
677 Set_Etype (Itype, Base_Type (Typ));
678 Set_Has_Alignment_Clause (Itype, Has_Alignment_Clause (Typ));
679 Set_Is_Aliased (Itype, Is_Aliased (Typ));
680 Set_Depends_On_Private (Itype, Depends_On_Private (Typ));
682 Copy_Suppress_Status (Index_Check, Typ, Itype);
683 Copy_Suppress_Status (Length_Check, Typ, Itype);
685 Set_First_Index (Itype, First (Index_Constraints));
686 Set_Is_Constrained (Itype, True);
687 Set_Is_Internal (Itype, True);
689 -- A simple optimization: purely positional aggregates of static
690 -- components should be passed to gigi unexpanded whenever possible, and
691 -- regardless of the staticness of the bounds themselves. Subsequent
692 -- checks in exp_aggr verify that type is not packed, etc.
694 Set_Size_Known_At_Compile_Time (Itype,
696 and then Comes_From_Source (N)
697 and then Size_Known_At_Compile_Time (Component_Type (Typ)));
699 -- We always need a freeze node for a packed array subtype, so that we
700 -- can build the Packed_Array_Type corresponding to the subtype. If
701 -- expansion is disabled, the packed array subtype is not built, and we
702 -- must not generate a freeze node for the type, or else it will appear
703 -- incomplete to gigi.
706 and then not In_Spec_Expression
707 and then Expander_Active
709 Freeze_Itype (Itype, N);
713 end Array_Aggr_Subtype;
715 --------------------------------
716 -- Check_Misspelled_Component --
717 --------------------------------
719 procedure Check_Misspelled_Component
720 (Elements : Elist_Id;
723 Max_Suggestions : constant := 2;
725 Nr_Of_Suggestions : Natural := 0;
726 Suggestion_1 : Entity_Id := Empty;
727 Suggestion_2 : Entity_Id := Empty;
728 Component_Elmt : Elmt_Id;
731 -- All the components of List are matched against Component and a count
732 -- is maintained of possible misspellings. When at the end of the
733 -- the analysis there are one or two (not more!) possible misspellings,
734 -- these misspellings will be suggested as possible correction.
736 Component_Elmt := First_Elmt (Elements);
737 while Nr_Of_Suggestions <= Max_Suggestions
738 and then Present (Component_Elmt)
740 if Is_Bad_Spelling_Of
741 (Chars (Node (Component_Elmt)),
744 Nr_Of_Suggestions := Nr_Of_Suggestions + 1;
746 case Nr_Of_Suggestions is
747 when 1 => Suggestion_1 := Node (Component_Elmt);
748 when 2 => Suggestion_2 := Node (Component_Elmt);
753 Next_Elmt (Component_Elmt);
756 -- Report at most two suggestions
758 if Nr_Of_Suggestions = 1 then
759 Error_Msg_NE -- CODEFIX
760 ("\possible misspelling of&", Component, Suggestion_1);
762 elsif Nr_Of_Suggestions = 2 then
763 Error_Msg_Node_2 := Suggestion_2;
764 Error_Msg_NE -- CODEFIX
765 ("\possible misspelling of& or&", Component, Suggestion_1);
767 end Check_Misspelled_Component;
769 ----------------------------------------
770 -- Check_Expr_OK_In_Limited_Aggregate --
771 ----------------------------------------
773 procedure Check_Expr_OK_In_Limited_Aggregate (Expr : Node_Id) is
775 if Is_Limited_Type (Etype (Expr))
776 and then Comes_From_Source (Expr)
777 and then not In_Instance_Body
779 if not OK_For_Limited_Init (Etype (Expr), Expr) then
780 Error_Msg_N ("initialization not allowed for limited types", Expr);
781 Explain_Limited_Type (Etype (Expr), Expr);
784 end Check_Expr_OK_In_Limited_Aggregate;
786 ----------------------------------------
787 -- Check_Static_Discriminated_Subtype --
788 ----------------------------------------
790 procedure Check_Static_Discriminated_Subtype (T : Entity_Id; V : Node_Id) is
791 Disc : constant Entity_Id := First_Discriminant (T);
796 if Has_Record_Rep_Clause (T) then
799 elsif Present (Next_Discriminant (Disc)) then
802 elsif Nkind (V) /= N_Integer_Literal then
806 Comp := First_Component (T);
807 while Present (Comp) loop
808 if Is_Scalar_Type (Etype (Comp)) then
811 elsif Is_Private_Type (Etype (Comp))
812 and then Present (Full_View (Etype (Comp)))
813 and then Is_Scalar_Type (Full_View (Etype (Comp)))
817 elsif Is_Array_Type (Etype (Comp)) then
818 if Is_Bit_Packed_Array (Etype (Comp)) then
822 Ind := First_Index (Etype (Comp));
823 while Present (Ind) loop
824 if Nkind (Ind) /= N_Range
825 or else Nkind (Low_Bound (Ind)) /= N_Integer_Literal
826 or else Nkind (High_Bound (Ind)) /= N_Integer_Literal
838 Next_Component (Comp);
841 -- On exit, all components have statically known sizes
843 Set_Size_Known_At_Compile_Time (T);
844 end Check_Static_Discriminated_Subtype;
846 --------------------------------
847 -- Make_String_Into_Aggregate --
848 --------------------------------
850 procedure Make_String_Into_Aggregate (N : Node_Id) is
851 Exprs : constant List_Id := New_List;
852 Loc : constant Source_Ptr := Sloc (N);
853 Str : constant String_Id := Strval (N);
854 Strlen : constant Nat := String_Length (Str);
862 for J in 1 .. Strlen loop
863 C := Get_String_Char (Str, J);
864 Set_Character_Literal_Name (C);
867 Make_Character_Literal (P,
869 Char_Literal_Value => UI_From_CC (C));
870 Set_Etype (C_Node, Any_Character);
871 Append_To (Exprs, C_Node);
874 -- Something special for wide strings???
877 New_N := Make_Aggregate (Loc, Expressions => Exprs);
878 Set_Analyzed (New_N);
879 Set_Etype (New_N, Any_Composite);
882 end Make_String_Into_Aggregate;
884 -----------------------
885 -- Resolve_Aggregate --
886 -----------------------
888 procedure Resolve_Aggregate (N : Node_Id; Typ : Entity_Id) is
889 Pkind : constant Node_Kind := Nkind (Parent (N));
891 Aggr_Subtyp : Entity_Id;
892 -- The actual aggregate subtype. This is not necessarily the same as Typ
893 -- which is the subtype of the context in which the aggregate was found.
896 -- Ignore junk empty aggregate resulting from parser error
898 if No (Expressions (N))
899 and then No (Component_Associations (N))
900 and then not Null_Record_Present (N)
905 -- Check for aggregates not allowed in configurable run-time mode.
906 -- We allow all cases of aggregates that do not come from source, since
907 -- these are all assumed to be small (e.g. bounds of a string literal).
908 -- We also allow aggregates of types we know to be small.
910 if not Support_Aggregates_On_Target
911 and then Comes_From_Source (N)
912 and then (not Known_Static_Esize (Typ) or else Esize (Typ) > 64)
914 Error_Msg_CRT ("aggregate", N);
917 -- Ada 2005 (AI-287): Limited aggregates allowed
919 if Is_Limited_Type (Typ) and then Ada_Version < Ada_05 then
920 Error_Msg_N ("aggregate type cannot be limited", N);
921 Explain_Limited_Type (Typ, N);
923 elsif Is_Class_Wide_Type (Typ) then
924 Error_Msg_N ("type of aggregate cannot be class-wide", N);
926 elsif Typ = Any_String
927 or else Typ = Any_Composite
929 Error_Msg_N ("no unique type for aggregate", N);
930 Set_Etype (N, Any_Composite);
932 elsif Is_Array_Type (Typ) and then Null_Record_Present (N) then
933 Error_Msg_N ("null record forbidden in array aggregate", N);
935 elsif Is_Record_Type (Typ) then
936 Resolve_Record_Aggregate (N, Typ);
938 elsif Is_Array_Type (Typ) then
940 -- First a special test, for the case of a positional aggregate
941 -- of characters which can be replaced by a string literal.
943 -- Do not perform this transformation if this was a string literal to
944 -- start with, whose components needed constraint checks, or if the
945 -- component type is non-static, because it will require those checks
946 -- and be transformed back into an aggregate.
948 if Number_Dimensions (Typ) = 1
949 and then Is_Standard_Character_Type (Component_Type (Typ))
950 and then No (Component_Associations (N))
951 and then not Is_Limited_Composite (Typ)
952 and then not Is_Private_Composite (Typ)
953 and then not Is_Bit_Packed_Array (Typ)
954 and then Nkind (Original_Node (Parent (N))) /= N_String_Literal
955 and then Is_Static_Subtype (Component_Type (Typ))
961 Expr := First (Expressions (N));
962 while Present (Expr) loop
963 exit when Nkind (Expr) /= N_Character_Literal;
970 Expr := First (Expressions (N));
971 while Present (Expr) loop
972 Store_String_Char (UI_To_CC (Char_Literal_Value (Expr)));
977 Make_String_Literal (Sloc (N), End_String));
979 Analyze_And_Resolve (N, Typ);
985 -- Here if we have a real aggregate to deal with
987 Array_Aggregate : declare
988 Aggr_Resolved : Boolean;
990 Aggr_Typ : constant Entity_Id := Etype (Typ);
991 -- This is the unconstrained array type, which is the type against
992 -- which the aggregate is to be resolved. Typ itself is the array
993 -- type of the context which may not be the same subtype as the
994 -- subtype for the final aggregate.
997 -- In the following we determine whether an others choice is
998 -- allowed inside the array aggregate. The test checks the context
999 -- in which the array aggregate occurs. If the context does not
1000 -- permit it, or the aggregate type is unconstrained, an others
1001 -- choice is not allowed.
1003 -- If expansion is disabled (generic context, or semantics-only
1004 -- mode) actual subtypes cannot be constructed, and the type of an
1005 -- object may be its unconstrained nominal type. However, if the
1006 -- context is an assignment, we assume that "others" is allowed,
1007 -- because the target of the assignment will have a constrained
1008 -- subtype when fully compiled.
1010 -- Note that there is no node for Explicit_Actual_Parameter.
1011 -- To test for this context we therefore have to test for node
1012 -- N_Parameter_Association which itself appears only if there is a
1013 -- formal parameter. Consequently we also need to test for
1014 -- N_Procedure_Call_Statement or N_Function_Call.
1016 Set_Etype (N, Aggr_Typ); -- May be overridden later on
1018 if Is_Constrained (Typ) and then
1019 (Pkind = N_Assignment_Statement or else
1020 Pkind = N_Parameter_Association or else
1021 Pkind = N_Function_Call or else
1022 Pkind = N_Procedure_Call_Statement or else
1023 Pkind = N_Generic_Association or else
1024 Pkind = N_Formal_Object_Declaration or else
1025 Pkind = N_Simple_Return_Statement or else
1026 Pkind = N_Object_Declaration or else
1027 Pkind = N_Component_Declaration or else
1028 Pkind = N_Parameter_Specification or else
1029 Pkind = N_Qualified_Expression or else
1030 Pkind = N_Aggregate or else
1031 Pkind = N_Extension_Aggregate or else
1032 Pkind = N_Component_Association)
1035 Resolve_Array_Aggregate
1037 Index => First_Index (Aggr_Typ),
1038 Index_Constr => First_Index (Typ),
1039 Component_Typ => Component_Type (Typ),
1040 Others_Allowed => True);
1042 elsif not Expander_Active
1043 and then Pkind = N_Assignment_Statement
1046 Resolve_Array_Aggregate
1048 Index => First_Index (Aggr_Typ),
1049 Index_Constr => First_Index (Typ),
1050 Component_Typ => Component_Type (Typ),
1051 Others_Allowed => True);
1054 Resolve_Array_Aggregate
1056 Index => First_Index (Aggr_Typ),
1057 Index_Constr => First_Index (Aggr_Typ),
1058 Component_Typ => Component_Type (Typ),
1059 Others_Allowed => False);
1062 if not Aggr_Resolved then
1063 Aggr_Subtyp := Any_Composite;
1065 Aggr_Subtyp := Array_Aggr_Subtype (N, Typ);
1068 Set_Etype (N, Aggr_Subtyp);
1069 end Array_Aggregate;
1071 elsif Is_Private_Type (Typ)
1072 and then Present (Full_View (Typ))
1073 and then In_Inlined_Body
1074 and then Is_Composite_Type (Full_View (Typ))
1076 Resolve (N, Full_View (Typ));
1079 Error_Msg_N ("illegal context for aggregate", N);
1082 -- If we can determine statically that the evaluation of the aggregate
1083 -- raises Constraint_Error, then replace the aggregate with an
1084 -- N_Raise_Constraint_Error node, but set the Etype to the right
1085 -- aggregate subtype. Gigi needs this.
1087 if Raises_Constraint_Error (N) then
1088 Aggr_Subtyp := Etype (N);
1090 Make_Raise_Constraint_Error (Sloc (N),
1091 Reason => CE_Range_Check_Failed));
1092 Set_Raises_Constraint_Error (N);
1093 Set_Etype (N, Aggr_Subtyp);
1096 end Resolve_Aggregate;
1098 -----------------------------
1099 -- Resolve_Array_Aggregate --
1100 -----------------------------
1102 function Resolve_Array_Aggregate
1105 Index_Constr : Node_Id;
1106 Component_Typ : Entity_Id;
1107 Others_Allowed : Boolean) return Boolean
1109 Loc : constant Source_Ptr := Sloc (N);
1111 Failure : constant Boolean := False;
1112 Success : constant Boolean := True;
1114 Index_Typ : constant Entity_Id := Etype (Index);
1115 Index_Typ_Low : constant Node_Id := Type_Low_Bound (Index_Typ);
1116 Index_Typ_High : constant Node_Id := Type_High_Bound (Index_Typ);
1117 -- The type of the index corresponding to the array sub-aggregate along
1118 -- with its low and upper bounds.
1120 Index_Base : constant Entity_Id := Base_Type (Index_Typ);
1121 Index_Base_Low : constant Node_Id := Type_Low_Bound (Index_Base);
1122 Index_Base_High : constant Node_Id := Type_High_Bound (Index_Base);
1123 -- Ditto for the base type
1125 function Add (Val : Uint; To : Node_Id) return Node_Id;
1126 -- Creates a new expression node where Val is added to expression To.
1127 -- Tries to constant fold whenever possible. To must be an already
1128 -- analyzed expression.
1130 procedure Check_Bound (BH : Node_Id; AH : in out Node_Id);
1131 -- Checks that AH (the upper bound of an array aggregate) is <= BH
1132 -- (the upper bound of the index base type). If the check fails a
1133 -- warning is emitted, the Raises_Constraint_Error flag of N is set,
1134 -- and AH is replaced with a duplicate of BH.
1136 procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id);
1137 -- Checks that range AL .. AH is compatible with range L .. H. Emits a
1138 -- warning if not and sets the Raises_Constraint_Error flag in N.
1140 procedure Check_Length (L, H : Node_Id; Len : Uint);
1141 -- Checks that range L .. H contains at least Len elements. Emits a
1142 -- warning if not and sets the Raises_Constraint_Error flag in N.
1144 function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean;
1145 -- Returns True if range L .. H is dynamic or null
1147 procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean);
1148 -- Given expression node From, this routine sets OK to False if it
1149 -- cannot statically evaluate From. Otherwise it stores this static
1150 -- value into Value.
1152 function Resolve_Aggr_Expr
1154 Single_Elmt : Boolean) return Boolean;
1155 -- Resolves aggregate expression Expr. Returns False if resolution
1156 -- fails. If Single_Elmt is set to False, the expression Expr may be
1157 -- used to initialize several array aggregate elements (this can happen
1158 -- for discrete choices such as "L .. H => Expr" or the others choice).
1159 -- In this event we do not resolve Expr unless expansion is disabled.
1160 -- To know why, see the DELAYED COMPONENT RESOLUTION note above.
1166 function Add (Val : Uint; To : Node_Id) return Node_Id is
1172 if Raises_Constraint_Error (To) then
1176 -- First test if we can do constant folding
1178 if Compile_Time_Known_Value (To)
1179 or else Nkind (To) = N_Integer_Literal
1181 Expr_Pos := Make_Integer_Literal (Loc, Expr_Value (To) + Val);
1182 Set_Is_Static_Expression (Expr_Pos);
1183 Set_Etype (Expr_Pos, Etype (To));
1184 Set_Analyzed (Expr_Pos, Analyzed (To));
1186 if not Is_Enumeration_Type (Index_Typ) then
1189 -- If we are dealing with enumeration return
1190 -- Index_Typ'Val (Expr_Pos)
1194 Make_Attribute_Reference
1196 Prefix => New_Reference_To (Index_Typ, Loc),
1197 Attribute_Name => Name_Val,
1198 Expressions => New_List (Expr_Pos));
1204 -- If we are here no constant folding possible
1206 if not Is_Enumeration_Type (Index_Base) then
1209 Left_Opnd => Duplicate_Subexpr (To),
1210 Right_Opnd => Make_Integer_Literal (Loc, Val));
1212 -- If we are dealing with enumeration return
1213 -- Index_Typ'Val (Index_Typ'Pos (To) + Val)
1217 Make_Attribute_Reference
1219 Prefix => New_Reference_To (Index_Typ, Loc),
1220 Attribute_Name => Name_Pos,
1221 Expressions => New_List (Duplicate_Subexpr (To)));
1225 Left_Opnd => To_Pos,
1226 Right_Opnd => Make_Integer_Literal (Loc, Val));
1229 Make_Attribute_Reference
1231 Prefix => New_Reference_To (Index_Typ, Loc),
1232 Attribute_Name => Name_Val,
1233 Expressions => New_List (Expr_Pos));
1243 procedure Check_Bound (BH : Node_Id; AH : in out Node_Id) is
1251 Get (Value => Val_BH, From => BH, OK => OK_BH);
1252 Get (Value => Val_AH, From => AH, OK => OK_AH);
1254 if OK_BH and then OK_AH and then Val_BH < Val_AH then
1255 Set_Raises_Constraint_Error (N);
1256 Error_Msg_N ("upper bound out of range?", AH);
1257 Error_Msg_N ("\Constraint_Error will be raised at run-time?", AH);
1259 -- You need to set AH to BH or else in the case of enumerations
1260 -- indices we will not be able to resolve the aggregate bounds.
1262 AH := Duplicate_Subexpr (BH);
1270 procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id) is
1281 pragma Warnings (Off, OK_AL);
1282 pragma Warnings (Off, OK_AH);
1285 if Raises_Constraint_Error (N)
1286 or else Dynamic_Or_Null_Range (AL, AH)
1291 Get (Value => Val_L, From => L, OK => OK_L);
1292 Get (Value => Val_H, From => H, OK => OK_H);
1294 Get (Value => Val_AL, From => AL, OK => OK_AL);
1295 Get (Value => Val_AH, From => AH, OK => OK_AH);
1297 if OK_L and then Val_L > Val_AL then
1298 Set_Raises_Constraint_Error (N);
1299 Error_Msg_N ("lower bound of aggregate out of range?", N);
1300 Error_Msg_N ("\Constraint_Error will be raised at run-time?", N);
1303 if OK_H and then Val_H < Val_AH then
1304 Set_Raises_Constraint_Error (N);
1305 Error_Msg_N ("upper bound of aggregate out of range?", N);
1306 Error_Msg_N ("\Constraint_Error will be raised at run-time?", N);
1314 procedure Check_Length (L, H : Node_Id; Len : Uint) is
1324 if Raises_Constraint_Error (N) then
1328 Get (Value => Val_L, From => L, OK => OK_L);
1329 Get (Value => Val_H, From => H, OK => OK_H);
1331 if not OK_L or else not OK_H then
1335 -- If null range length is zero
1337 if Val_L > Val_H then
1338 Range_Len := Uint_0;
1340 Range_Len := Val_H - Val_L + 1;
1343 if Range_Len < Len then
1344 Set_Raises_Constraint_Error (N);
1345 Error_Msg_N ("too many elements?", N);
1346 Error_Msg_N ("\Constraint_Error will be raised at run-time?", N);
1350 ---------------------------
1351 -- Dynamic_Or_Null_Range --
1352 ---------------------------
1354 function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean is
1362 Get (Value => Val_L, From => L, OK => OK_L);
1363 Get (Value => Val_H, From => H, OK => OK_H);
1365 return not OK_L or else not OK_H
1366 or else not Is_OK_Static_Expression (L)
1367 or else not Is_OK_Static_Expression (H)
1368 or else Val_L > Val_H;
1369 end Dynamic_Or_Null_Range;
1375 procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean) is
1379 if Compile_Time_Known_Value (From) then
1380 Value := Expr_Value (From);
1382 -- If expression From is something like Some_Type'Val (10) then
1385 elsif Nkind (From) = N_Attribute_Reference
1386 and then Attribute_Name (From) = Name_Val
1387 and then Compile_Time_Known_Value (First (Expressions (From)))
1389 Value := Expr_Value (First (Expressions (From)));
1397 -----------------------
1398 -- Resolve_Aggr_Expr --
1399 -----------------------
1401 function Resolve_Aggr_Expr
1403 Single_Elmt : Boolean) return Boolean
1405 Nxt_Ind : constant Node_Id := Next_Index (Index);
1406 Nxt_Ind_Constr : constant Node_Id := Next_Index (Index_Constr);
1407 -- Index is the current index corresponding to the expression
1409 Resolution_OK : Boolean := True;
1410 -- Set to False if resolution of the expression failed
1413 -- If the array type against which we are resolving the aggregate
1414 -- has several dimensions, the expressions nested inside the
1415 -- aggregate must be further aggregates (or strings).
1417 if Present (Nxt_Ind) then
1418 if Nkind (Expr) /= N_Aggregate then
1420 -- A string literal can appear where a one-dimensional array
1421 -- of characters is expected. If the literal looks like an
1422 -- operator, it is still an operator symbol, which will be
1423 -- transformed into a string when analyzed.
1425 if Is_Character_Type (Component_Typ)
1426 and then No (Next_Index (Nxt_Ind))
1427 and then Nkind_In (Expr, N_String_Literal, N_Operator_Symbol)
1429 -- A string literal used in a multidimensional array
1430 -- aggregate in place of the final one-dimensional
1431 -- aggregate must not be enclosed in parentheses.
1433 if Paren_Count (Expr) /= 0 then
1434 Error_Msg_N ("no parenthesis allowed here", Expr);
1437 Make_String_Into_Aggregate (Expr);
1440 Error_Msg_N ("nested array aggregate expected", Expr);
1442 -- If the expression is parenthesized, this may be
1443 -- a missing component association for a 1-aggregate.
1445 if Paren_Count (Expr) > 0 then
1446 Error_Msg_N ("\if single-component aggregate is intended,"
1447 & " write e.g. (1 ='> ...)", Expr);
1453 -- Ada 2005 (AI-231): Propagate the type to the nested aggregate.
1454 -- Required to check the null-exclusion attribute (if present).
1455 -- This value may be overridden later on.
1457 Set_Etype (Expr, Etype (N));
1459 Resolution_OK := Resolve_Array_Aggregate
1460 (Expr, Nxt_Ind, Nxt_Ind_Constr, Component_Typ, Others_Allowed);
1462 -- Do not resolve the expressions of discrete or others choices
1463 -- unless the expression covers a single component, or the expander
1467 or else not Expander_Active
1468 or else In_Spec_Expression
1470 Analyze_And_Resolve (Expr, Component_Typ);
1471 Check_Expr_OK_In_Limited_Aggregate (Expr);
1472 Check_Non_Static_Context (Expr);
1473 Aggregate_Constraint_Checks (Expr, Component_Typ);
1474 Check_Unset_Reference (Expr);
1477 if Raises_Constraint_Error (Expr)
1478 and then Nkind (Parent (Expr)) /= N_Component_Association
1480 Set_Raises_Constraint_Error (N);
1483 -- If the expression has been marked as requiring a range check,
1484 -- then generate it here.
1486 if Do_Range_Check (Expr) then
1487 Set_Do_Range_Check (Expr, False);
1488 Generate_Range_Check (Expr, Component_Typ, CE_Range_Check_Failed);
1491 return Resolution_OK;
1492 end Resolve_Aggr_Expr;
1494 -- Variables local to Resolve_Array_Aggregate
1501 pragma Warnings (Off, Discard);
1503 Aggr_Low : Node_Id := Empty;
1504 Aggr_High : Node_Id := Empty;
1505 -- The actual low and high bounds of this sub-aggregate
1507 Choices_Low : Node_Id := Empty;
1508 Choices_High : Node_Id := Empty;
1509 -- The lowest and highest discrete choices values for a named aggregate
1511 Nb_Elements : Uint := Uint_0;
1512 -- The number of elements in a positional aggregate
1514 Others_Present : Boolean := False;
1516 Nb_Choices : Nat := 0;
1517 -- Contains the overall number of named choices in this sub-aggregate
1519 Nb_Discrete_Choices : Nat := 0;
1520 -- The overall number of discrete choices (not counting others choice)
1522 Case_Table_Size : Nat;
1523 -- Contains the size of the case table needed to sort aggregate choices
1525 -- Start of processing for Resolve_Array_Aggregate
1528 -- Ignore junk empty aggregate resulting from parser error
1530 if No (Expressions (N))
1531 and then No (Component_Associations (N))
1532 and then not Null_Record_Present (N)
1537 -- STEP 1: make sure the aggregate is correctly formatted
1539 if Present (Component_Associations (N)) then
1540 Assoc := First (Component_Associations (N));
1541 while Present (Assoc) loop
1542 Choice := First (Choices (Assoc));
1543 while Present (Choice) loop
1544 if Nkind (Choice) = N_Others_Choice then
1545 Others_Present := True;
1547 if Choice /= First (Choices (Assoc))
1548 or else Present (Next (Choice))
1551 ("OTHERS must appear alone in a choice list", Choice);
1555 if Present (Next (Assoc)) then
1557 ("OTHERS must appear last in an aggregate", Choice);
1561 if Ada_Version = Ada_83
1562 and then Assoc /= First (Component_Associations (N))
1563 and then Nkind_In (Parent (N), N_Assignment_Statement,
1564 N_Object_Declaration)
1567 ("(Ada 83) illegal context for OTHERS choice", N);
1571 Nb_Choices := Nb_Choices + 1;
1579 -- At this point we know that the others choice, if present, is by
1580 -- itself and appears last in the aggregate. Check if we have mixed
1581 -- positional and discrete associations (other than the others choice).
1583 if Present (Expressions (N))
1584 and then (Nb_Choices > 1
1585 or else (Nb_Choices = 1 and then not Others_Present))
1588 ("named association cannot follow positional association",
1589 First (Choices (First (Component_Associations (N)))));
1593 -- Test for the validity of an others choice if present
1595 if Others_Present and then not Others_Allowed then
1597 ("OTHERS choice not allowed here",
1598 First (Choices (First (Component_Associations (N)))));
1602 -- Protect against cascaded errors
1604 if Etype (Index_Typ) = Any_Type then
1608 -- STEP 2: Process named components
1610 if No (Expressions (N)) then
1611 if Others_Present then
1612 Case_Table_Size := Nb_Choices - 1;
1614 Case_Table_Size := Nb_Choices;
1620 -- Denote the lowest and highest values in an aggregate choice
1624 -- High end of one range and Low end of the next. Should be
1625 -- contiguous if there is no hole in the list of values.
1627 Missing_Values : Boolean;
1628 -- Set True if missing index values
1630 S_Low : Node_Id := Empty;
1631 S_High : Node_Id := Empty;
1632 -- if a choice in an aggregate is a subtype indication these
1633 -- denote the lowest and highest values of the subtype
1635 Table : Case_Table_Type (1 .. Case_Table_Size);
1636 -- Used to sort all the different choice values
1638 Single_Choice : Boolean;
1639 -- Set to true every time there is a single discrete choice in a
1640 -- discrete association
1642 Prev_Nb_Discrete_Choices : Nat;
1643 -- Used to keep track of the number of discrete choices in the
1644 -- current association.
1647 -- STEP 2 (A): Check discrete choices validity
1649 Assoc := First (Component_Associations (N));
1650 while Present (Assoc) loop
1651 Prev_Nb_Discrete_Choices := Nb_Discrete_Choices;
1652 Choice := First (Choices (Assoc));
1656 if Nkind (Choice) = N_Others_Choice then
1657 Single_Choice := False;
1660 -- Test for subtype mark without constraint
1662 elsif Is_Entity_Name (Choice) and then
1663 Is_Type (Entity (Choice))
1665 if Base_Type (Entity (Choice)) /= Index_Base then
1667 ("invalid subtype mark in aggregate choice",
1672 -- Case of subtype indication
1674 elsif Nkind (Choice) = N_Subtype_Indication then
1675 Resolve_Discrete_Subtype_Indication (Choice, Index_Base);
1677 -- Does the subtype indication evaluation raise CE ?
1679 Get_Index_Bounds (Subtype_Mark (Choice), S_Low, S_High);
1680 Get_Index_Bounds (Choice, Low, High);
1681 Check_Bounds (S_Low, S_High, Low, High);
1683 -- Case of range or expression
1686 Resolve (Choice, Index_Base);
1687 Check_Unset_Reference (Choice);
1688 Check_Non_Static_Context (Choice);
1690 -- Do not range check a choice. This check is redundant
1691 -- since this test is already done when we check that the
1692 -- bounds of the array aggregate are within range.
1694 Set_Do_Range_Check (Choice, False);
1697 -- If we could not resolve the discrete choice stop here
1699 if Etype (Choice) = Any_Type then
1702 -- If the discrete choice raises CE get its original bounds
1704 elsif Nkind (Choice) = N_Raise_Constraint_Error then
1705 Set_Raises_Constraint_Error (N);
1706 Get_Index_Bounds (Original_Node (Choice), Low, High);
1708 -- Otherwise get its bounds as usual
1711 Get_Index_Bounds (Choice, Low, High);
1714 if (Dynamic_Or_Null_Range (Low, High)
1715 or else (Nkind (Choice) = N_Subtype_Indication
1717 Dynamic_Or_Null_Range (S_Low, S_High)))
1718 and then Nb_Choices /= 1
1721 ("dynamic or empty choice in aggregate " &
1722 "must be the only choice", Choice);
1726 Nb_Discrete_Choices := Nb_Discrete_Choices + 1;
1727 Table (Nb_Discrete_Choices).Choice_Lo := Low;
1728 Table (Nb_Discrete_Choices).Choice_Hi := High;
1734 -- Check if we have a single discrete choice and whether
1735 -- this discrete choice specifies a single value.
1738 (Nb_Discrete_Choices = Prev_Nb_Discrete_Choices + 1)
1739 and then (Low = High);
1745 -- Ada 2005 (AI-231)
1747 if Ada_Version >= Ada_05
1748 and then Known_Null (Expression (Assoc))
1750 Check_Can_Never_Be_Null (Etype (N), Expression (Assoc));
1753 -- Ada 2005 (AI-287): In case of default initialized component
1754 -- we delay the resolution to the expansion phase.
1756 if Box_Present (Assoc) then
1758 -- Ada 2005 (AI-287): In case of default initialization of a
1759 -- component the expander will generate calls to the
1760 -- corresponding initialization subprogram.
1764 elsif not Resolve_Aggr_Expr (Expression (Assoc),
1765 Single_Elmt => Single_Choice)
1769 -- Check incorrect use of dynamically tagged expression
1771 -- We differentiate here two cases because the expression may
1772 -- not be decorated. For example, the analysis and resolution
1773 -- of the expression associated with the others choice will be
1774 -- done later with the full aggregate. In such case we
1775 -- duplicate the expression tree to analyze the copy and
1776 -- perform the required check.
1778 elsif not Present (Etype (Expression (Assoc))) then
1780 Save_Analysis : constant Boolean := Full_Analysis;
1781 Expr : constant Node_Id :=
1782 New_Copy_Tree (Expression (Assoc));
1785 Expander_Mode_Save_And_Set (False);
1786 Full_Analysis := False;
1788 Full_Analysis := Save_Analysis;
1789 Expander_Mode_Restore;
1791 if Is_Tagged_Type (Etype (Expr)) then
1792 Check_Dynamically_Tagged_Expression
1794 Typ => Component_Type (Etype (N)),
1799 elsif Is_Tagged_Type (Etype (Expression (Assoc))) then
1800 Check_Dynamically_Tagged_Expression
1801 (Expr => Expression (Assoc),
1802 Typ => Component_Type (Etype (N)),
1809 -- If aggregate contains more than one choice then these must be
1810 -- static. Sort them and check that they are contiguous.
1812 if Nb_Discrete_Choices > 1 then
1813 Sort_Case_Table (Table);
1814 Missing_Values := False;
1816 Outer : for J in 1 .. Nb_Discrete_Choices - 1 loop
1817 if Expr_Value (Table (J).Choice_Hi) >=
1818 Expr_Value (Table (J + 1).Choice_Lo)
1821 ("duplicate choice values in array aggregate",
1822 Table (J).Choice_Hi);
1825 elsif not Others_Present then
1826 Hi_Val := Expr_Value (Table (J).Choice_Hi);
1827 Lo_Val := Expr_Value (Table (J + 1).Choice_Lo);
1829 -- If missing values, output error messages
1831 if Lo_Val - Hi_Val > 1 then
1833 -- Header message if not first missing value
1835 if not Missing_Values then
1837 ("missing index value(s) in array aggregate", N);
1838 Missing_Values := True;
1841 -- Output values of missing indexes
1843 Lo_Val := Lo_Val - 1;
1844 Hi_Val := Hi_Val + 1;
1846 -- Enumeration type case
1848 if Is_Enumeration_Type (Index_Typ) then
1851 (Get_Enum_Lit_From_Pos
1852 (Index_Typ, Hi_Val, Loc));
1854 if Lo_Val = Hi_Val then
1855 Error_Msg_N ("\ %", N);
1859 (Get_Enum_Lit_From_Pos
1860 (Index_Typ, Lo_Val, Loc));
1861 Error_Msg_N ("\ % .. %", N);
1864 -- Integer types case
1867 Error_Msg_Uint_1 := Hi_Val;
1869 if Lo_Val = Hi_Val then
1870 Error_Msg_N ("\ ^", N);
1872 Error_Msg_Uint_2 := Lo_Val;
1873 Error_Msg_N ("\ ^ .. ^", N);
1880 if Missing_Values then
1881 Set_Etype (N, Any_Composite);
1886 -- STEP 2 (B): Compute aggregate bounds and min/max choices values
1888 if Nb_Discrete_Choices > 0 then
1889 Choices_Low := Table (1).Choice_Lo;
1890 Choices_High := Table (Nb_Discrete_Choices).Choice_Hi;
1893 -- If Others is present, then bounds of aggregate come from the
1894 -- index constraint (not the choices in the aggregate itself).
1896 if Others_Present then
1897 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
1899 -- No others clause present
1902 -- Special processing if others allowed and not present. This
1903 -- means that the bounds of the aggregate come from the index
1904 -- constraint (and the length must match).
1906 if Others_Allowed then
1907 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
1909 -- If others allowed, and no others present, then the array
1910 -- should cover all index values. If it does not, we will
1911 -- get a length check warning, but there is two cases where
1912 -- an additional warning is useful:
1914 -- If we have no positional components, and the length is
1915 -- wrong (which we can tell by others being allowed with
1916 -- missing components), and the index type is an enumeration
1917 -- type, then issue appropriate warnings about these missing
1918 -- components. They are only warnings, since the aggregate
1919 -- is fine, it's just the wrong length. We skip this check
1920 -- for standard character types (since there are no literals
1921 -- and it is too much trouble to concoct them), and also if
1922 -- any of the bounds have not-known-at-compile-time values.
1924 -- Another case warranting a warning is when the length is
1925 -- right, but as above we have an index type that is an
1926 -- enumeration, and the bounds do not match. This is a
1927 -- case where dubious sliding is allowed and we generate
1928 -- a warning that the bounds do not match.
1930 if No (Expressions (N))
1931 and then Nkind (Index) = N_Range
1932 and then Is_Enumeration_Type (Etype (Index))
1933 and then not Is_Standard_Character_Type (Etype (Index))
1934 and then Compile_Time_Known_Value (Aggr_Low)
1935 and then Compile_Time_Known_Value (Aggr_High)
1936 and then Compile_Time_Known_Value (Choices_Low)
1937 and then Compile_Time_Known_Value (Choices_High)
1939 -- If the bounds have semantic errors, do not attempt
1940 -- further resolution to prevent cascaded errors.
1942 if Error_Posted (Choices_Low)
1943 or else Error_Posted (Choices_High)
1949 ALo : constant Node_Id := Expr_Value_E (Aggr_Low);
1950 AHi : constant Node_Id := Expr_Value_E (Aggr_High);
1951 CLo : constant Node_Id := Expr_Value_E (Choices_Low);
1952 CHi : constant Node_Id := Expr_Value_E (Choices_High);
1957 -- Warning case 1, missing values at start/end. Only
1958 -- do the check if the number of entries is too small.
1960 if (Enumeration_Pos (CHi) - Enumeration_Pos (CLo))
1962 (Enumeration_Pos (AHi) - Enumeration_Pos (ALo))
1965 ("missing index value(s) in array aggregate?", N);
1967 -- Output missing value(s) at start
1969 if Chars (ALo) /= Chars (CLo) then
1972 if Chars (ALo) = Chars (Ent) then
1973 Error_Msg_Name_1 := Chars (ALo);
1974 Error_Msg_N ("\ %?", N);
1976 Error_Msg_Name_1 := Chars (ALo);
1977 Error_Msg_Name_2 := Chars (Ent);
1978 Error_Msg_N ("\ % .. %?", N);
1982 -- Output missing value(s) at end
1984 if Chars (AHi) /= Chars (CHi) then
1987 if Chars (AHi) = Chars (Ent) then
1988 Error_Msg_Name_1 := Chars (Ent);
1989 Error_Msg_N ("\ %?", N);
1991 Error_Msg_Name_1 := Chars (Ent);
1992 Error_Msg_Name_2 := Chars (AHi);
1993 Error_Msg_N ("\ % .. %?", N);
1997 -- Warning case 2, dubious sliding. The First_Subtype
1998 -- test distinguishes between a constrained type where
1999 -- sliding is not allowed (so we will get a warning
2000 -- later that Constraint_Error will be raised), and
2001 -- the unconstrained case where sliding is permitted.
2003 elsif (Enumeration_Pos (CHi) - Enumeration_Pos (CLo))
2005 (Enumeration_Pos (AHi) - Enumeration_Pos (ALo))
2006 and then Chars (ALo) /= Chars (CLo)
2008 not Is_Constrained (First_Subtype (Etype (N)))
2011 ("bounds of aggregate do not match target?", N);
2017 -- If no others, aggregate bounds come from aggregate
2019 Aggr_Low := Choices_Low;
2020 Aggr_High := Choices_High;
2024 -- STEP 3: Process positional components
2027 -- STEP 3 (A): Process positional elements
2029 Expr := First (Expressions (N));
2030 Nb_Elements := Uint_0;
2031 while Present (Expr) loop
2032 Nb_Elements := Nb_Elements + 1;
2034 -- Ada 2005 (AI-231)
2036 if Ada_Version >= Ada_05
2037 and then Known_Null (Expr)
2039 Check_Can_Never_Be_Null (Etype (N), Expr);
2042 if not Resolve_Aggr_Expr (Expr, Single_Elmt => True) then
2046 -- Check incorrect use of dynamically tagged expression
2048 if Is_Tagged_Type (Etype (Expr)) then
2049 Check_Dynamically_Tagged_Expression
2051 Typ => Component_Type (Etype (N)),
2058 if Others_Present then
2059 Assoc := Last (Component_Associations (N));
2061 -- Ada 2005 (AI-231)
2063 if Ada_Version >= Ada_05
2064 and then Known_Null (Assoc)
2066 Check_Can_Never_Be_Null (Etype (N), Expression (Assoc));
2069 -- Ada 2005 (AI-287): In case of default initialized component,
2070 -- we delay the resolution to the expansion phase.
2072 if Box_Present (Assoc) then
2074 -- Ada 2005 (AI-287): In case of default initialization of a
2075 -- component the expander will generate calls to the
2076 -- corresponding initialization subprogram.
2080 elsif not Resolve_Aggr_Expr (Expression (Assoc),
2081 Single_Elmt => False)
2085 -- Check incorrect use of dynamically tagged expression. The
2086 -- expression of the others choice has not been resolved yet.
2087 -- In order to diagnose the semantic error we create a duplicate
2088 -- tree to analyze it and perform the check.
2092 Save_Analysis : constant Boolean := Full_Analysis;
2093 Expr : constant Node_Id :=
2094 New_Copy_Tree (Expression (Assoc));
2097 Expander_Mode_Save_And_Set (False);
2098 Full_Analysis := False;
2100 Full_Analysis := Save_Analysis;
2101 Expander_Mode_Restore;
2103 if Is_Tagged_Type (Etype (Expr)) then
2104 Check_Dynamically_Tagged_Expression
2106 Typ => Component_Type (Etype (N)),
2113 -- STEP 3 (B): Compute the aggregate bounds
2115 if Others_Present then
2116 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2119 if Others_Allowed then
2120 Get_Index_Bounds (Index_Constr, Aggr_Low, Discard);
2122 Aggr_Low := Index_Typ_Low;
2125 Aggr_High := Add (Nb_Elements - 1, To => Aggr_Low);
2126 Check_Bound (Index_Base_High, Aggr_High);
2130 -- STEP 4: Perform static aggregate checks and save the bounds
2134 Check_Bounds (Index_Typ_Low, Index_Typ_High, Aggr_Low, Aggr_High);
2135 Check_Bounds (Index_Base_Low, Index_Base_High, Aggr_Low, Aggr_High);
2139 if Others_Present and then Nb_Discrete_Choices > 0 then
2140 Check_Bounds (Aggr_Low, Aggr_High, Choices_Low, Choices_High);
2141 Check_Bounds (Index_Typ_Low, Index_Typ_High,
2142 Choices_Low, Choices_High);
2143 Check_Bounds (Index_Base_Low, Index_Base_High,
2144 Choices_Low, Choices_High);
2148 elsif Others_Present and then Nb_Elements > 0 then
2149 Check_Length (Aggr_Low, Aggr_High, Nb_Elements);
2150 Check_Length (Index_Typ_Low, Index_Typ_High, Nb_Elements);
2151 Check_Length (Index_Base_Low, Index_Base_High, Nb_Elements);
2154 if Raises_Constraint_Error (Aggr_Low)
2155 or else Raises_Constraint_Error (Aggr_High)
2157 Set_Raises_Constraint_Error (N);
2160 Aggr_Low := Duplicate_Subexpr (Aggr_Low);
2162 -- Do not duplicate Aggr_High if Aggr_High = Aggr_Low + Nb_Elements
2163 -- since the addition node returned by Add is not yet analyzed. Attach
2164 -- to tree and analyze first. Reset analyzed flag to ensure it will get
2165 -- analyzed when it is a literal bound whose type must be properly set.
2167 if Others_Present or else Nb_Discrete_Choices > 0 then
2168 Aggr_High := Duplicate_Subexpr (Aggr_High);
2170 if Etype (Aggr_High) = Universal_Integer then
2171 Set_Analyzed (Aggr_High, False);
2175 -- If the aggregate already has bounds attached to it, it means this is
2176 -- a positional aggregate created as an optimization by
2177 -- Exp_Aggr.Convert_To_Positional, so we don't want to change those
2180 if Present (Aggregate_Bounds (N)) and then not Others_Allowed then
2181 Aggr_Low := Low_Bound (Aggregate_Bounds (N));
2182 Aggr_High := High_Bound (Aggregate_Bounds (N));
2185 Set_Aggregate_Bounds
2186 (N, Make_Range (Loc, Low_Bound => Aggr_Low, High_Bound => Aggr_High));
2188 -- The bounds may contain expressions that must be inserted upwards.
2189 -- Attach them fully to the tree. After analysis, remove side effects
2190 -- from upper bound, if still needed.
2192 Set_Parent (Aggregate_Bounds (N), N);
2193 Analyze_And_Resolve (Aggregate_Bounds (N), Index_Typ);
2194 Check_Unset_Reference (Aggregate_Bounds (N));
2196 if not Others_Present and then Nb_Discrete_Choices = 0 then
2197 Set_High_Bound (Aggregate_Bounds (N),
2198 Duplicate_Subexpr (High_Bound (Aggregate_Bounds (N))));
2202 end Resolve_Array_Aggregate;
2204 ---------------------------------
2205 -- Resolve_Extension_Aggregate --
2206 ---------------------------------
2208 -- There are two cases to consider:
2210 -- a) If the ancestor part is a type mark, the components needed are the
2211 -- difference between the components of the expected type and the
2212 -- components of the given type mark.
2214 -- b) If the ancestor part is an expression, it must be unambiguous, and
2215 -- once we have its type we can also compute the needed components as in
2216 -- the previous case. In both cases, if the ancestor type is not the
2217 -- immediate ancestor, we have to build this ancestor recursively.
2219 -- In both cases discriminants of the ancestor type do not play a role in
2220 -- the resolution of the needed components, because inherited discriminants
2221 -- cannot be used in a type extension. As a result we can compute
2222 -- independently the list of components of the ancestor type and of the
2225 procedure Resolve_Extension_Aggregate (N : Node_Id; Typ : Entity_Id) is
2226 A : constant Node_Id := Ancestor_Part (N);
2231 function Valid_Limited_Ancestor (Anc : Node_Id) return Boolean;
2232 -- If the type is limited, verify that the ancestor part is a legal
2233 -- expression (aggregate or function call, including 'Input)) that does
2234 -- not require a copy, as specified in 7.5(2).
2236 function Valid_Ancestor_Type return Boolean;
2237 -- Verify that the type of the ancestor part is a non-private ancestor
2238 -- of the expected type, which must be a type extension.
2240 ----------------------------
2241 -- Valid_Limited_Ancestor --
2242 ----------------------------
2244 function Valid_Limited_Ancestor (Anc : Node_Id) return Boolean is
2246 if Is_Entity_Name (Anc)
2247 and then Is_Type (Entity (Anc))
2251 elsif Nkind_In (Anc, N_Aggregate, N_Function_Call) then
2254 elsif Nkind (Anc) = N_Attribute_Reference
2255 and then Attribute_Name (Anc) = Name_Input
2259 elsif Nkind (Anc) = N_Qualified_Expression then
2260 return Valid_Limited_Ancestor (Expression (Anc));
2265 end Valid_Limited_Ancestor;
2267 -------------------------
2268 -- Valid_Ancestor_Type --
2269 -------------------------
2271 function Valid_Ancestor_Type return Boolean is
2272 Imm_Type : Entity_Id;
2275 Imm_Type := Base_Type (Typ);
2276 while Is_Derived_Type (Imm_Type) loop
2277 if Etype (Imm_Type) = Base_Type (A_Type) then
2280 -- The base type of the parent type may appear as a private
2281 -- extension if it is declared as such in a parent unit of the
2282 -- current one. For consistency of the subsequent analysis use
2283 -- the partial view for the ancestor part.
2285 elsif Is_Private_Type (Etype (Imm_Type))
2286 and then Present (Full_View (Etype (Imm_Type)))
2287 and then Base_Type (A_Type) = Full_View (Etype (Imm_Type))
2289 A_Type := Etype (Imm_Type);
2292 Imm_Type := Etype (Base_Type (Imm_Type));
2296 -- If previous loop did not find a proper ancestor, report error
2298 Error_Msg_NE ("expect ancestor type of &", A, Typ);
2300 end Valid_Ancestor_Type;
2302 -- Start of processing for Resolve_Extension_Aggregate
2305 -- Analyze the ancestor part and account for the case where it is a
2306 -- parameterless function call.
2309 Check_Parameterless_Call (A);
2311 if not Is_Tagged_Type (Typ) then
2312 Error_Msg_N ("type of extension aggregate must be tagged", N);
2315 elsif Is_Limited_Type (Typ) then
2317 -- Ada 2005 (AI-287): Limited aggregates are allowed
2319 if Ada_Version < Ada_05 then
2320 Error_Msg_N ("aggregate type cannot be limited", N);
2321 Explain_Limited_Type (Typ, N);
2324 elsif Valid_Limited_Ancestor (A) then
2329 ("limited ancestor part must be aggregate or function call", A);
2332 elsif Is_Class_Wide_Type (Typ) then
2333 Error_Msg_N ("aggregate cannot be of a class-wide type", N);
2337 if Is_Entity_Name (A)
2338 and then Is_Type (Entity (A))
2340 A_Type := Get_Full_View (Entity (A));
2342 if Valid_Ancestor_Type then
2343 Set_Entity (A, A_Type);
2344 Set_Etype (A, A_Type);
2346 Validate_Ancestor_Part (N);
2347 Resolve_Record_Aggregate (N, Typ);
2350 elsif Nkind (A) /= N_Aggregate then
2351 if Is_Overloaded (A) then
2354 Get_First_Interp (A, I, It);
2355 while Present (It.Typ) loop
2356 -- Only consider limited interpretations in the Ada 2005 case
2358 if Is_Tagged_Type (It.Typ)
2359 and then (Ada_Version >= Ada_05
2360 or else not Is_Limited_Type (It.Typ))
2362 if A_Type /= Any_Type then
2363 Error_Msg_N ("cannot resolve expression", A);
2370 Get_Next_Interp (I, It);
2373 if A_Type = Any_Type then
2374 if Ada_Version >= Ada_05 then
2375 Error_Msg_N ("ancestor part must be of a tagged type", A);
2378 ("ancestor part must be of a nonlimited tagged type", A);
2385 A_Type := Etype (A);
2388 if Valid_Ancestor_Type then
2389 Resolve (A, A_Type);
2390 Check_Unset_Reference (A);
2391 Check_Non_Static_Context (A);
2393 -- The aggregate is illegal if the ancestor expression is a call
2394 -- to a function with a limited unconstrained result, unless the
2395 -- type of the aggregate is a null extension. This restriction
2396 -- was added in AI05-67 to simplify implementation.
2398 if Nkind (A) = N_Function_Call
2399 and then Is_Limited_Type (A_Type)
2400 and then not Is_Null_Extension (Typ)
2401 and then not Is_Constrained (A_Type)
2404 ("type of limited ancestor part must be constrained", A);
2406 elsif Is_Class_Wide_Type (Etype (A))
2407 and then Nkind (Original_Node (A)) = N_Function_Call
2409 -- If the ancestor part is a dispatching call, it appears
2410 -- statically to be a legal ancestor, but it yields any member
2411 -- of the class, and it is not possible to determine whether
2412 -- it is an ancestor of the extension aggregate (much less
2413 -- which ancestor). It is not possible to determine the
2414 -- components of the extension part.
2416 -- This check implements AI-306, which in fact was motivated by
2417 -- an AdaCore query to the ARG after this test was added.
2419 Error_Msg_N ("ancestor part must be statically tagged", A);
2421 Resolve_Record_Aggregate (N, Typ);
2426 Error_Msg_N ("no unique type for this aggregate", A);
2428 end Resolve_Extension_Aggregate;
2430 ------------------------------
2431 -- Resolve_Record_Aggregate --
2432 ------------------------------
2434 procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id) is
2436 -- N_Component_Association node belonging to the input aggregate N
2439 Positional_Expr : Node_Id;
2440 Component : Entity_Id;
2441 Component_Elmt : Elmt_Id;
2443 Components : constant Elist_Id := New_Elmt_List;
2444 -- Components is the list of the record components whose value must be
2445 -- provided in the aggregate. This list does include discriminants.
2447 New_Assoc_List : constant List_Id := New_List;
2448 New_Assoc : Node_Id;
2449 -- New_Assoc_List is the newly built list of N_Component_Association
2450 -- nodes. New_Assoc is one such N_Component_Association node in it.
2451 -- Note that while Assoc and New_Assoc contain the same kind of nodes,
2452 -- they are used to iterate over two different N_Component_Association
2455 Others_Etype : Entity_Id := Empty;
2456 -- This variable is used to save the Etype of the last record component
2457 -- that takes its value from the others choice. Its purpose is:
2459 -- (a) make sure the others choice is useful
2461 -- (b) make sure the type of all the components whose value is
2462 -- subsumed by the others choice are the same.
2464 -- This variable is updated as a side effect of function Get_Value.
2466 Is_Box_Present : Boolean := False;
2467 Others_Box : Boolean := False;
2468 -- Ada 2005 (AI-287): Variables used in case of default initialization
2469 -- to provide a functionality similar to Others_Etype. Box_Present
2470 -- indicates that the component takes its default initialization;
2471 -- Others_Box indicates that at least one component takes its default
2472 -- initialization. Similar to Others_Etype, they are also updated as a
2473 -- side effect of function Get_Value.
2475 procedure Add_Association
2476 (Component : Entity_Id;
2478 Assoc_List : List_Id;
2479 Is_Box_Present : Boolean := False);
2480 -- Builds a new N_Component_Association node which associates Component
2481 -- to expression Expr and adds it to the association list being built,
2482 -- either New_Assoc_List, or the association being built for an inner
2485 function Discr_Present (Discr : Entity_Id) return Boolean;
2486 -- If aggregate N is a regular aggregate this routine will return True.
2487 -- Otherwise, if N is an extension aggregate, Discr is a discriminant
2488 -- whose value may already have been specified by N's ancestor part.
2489 -- This routine checks whether this is indeed the case and if so returns
2490 -- False, signaling that no value for Discr should appear in N's
2491 -- aggregate part. Also, in this case, the routine appends
2492 -- New_Assoc_List Discr the discriminant value specified in the ancestor
2494 -- Can't parse previous sentence, appends what where???
2499 Consider_Others_Choice : Boolean := False)
2501 -- Given a record component stored in parameter Compon, the following
2502 -- function returns its value as it appears in the list From, which is
2503 -- a list of N_Component_Association nodes.
2504 -- What is this referring to??? There is no "following function" in
2506 -- If no component association has a choice for the searched component,
2507 -- the value provided by the others choice is returned, if there is one,
2508 -- and Consider_Others_Choice is set to true. Otherwise Empty is
2509 -- returned. If there is more than one component association giving a
2510 -- value for the searched record component, an error message is emitted
2511 -- and the first found value is returned.
2513 -- If Consider_Others_Choice is set and the returned expression comes
2514 -- from the others choice, then Others_Etype is set as a side effect.
2515 -- An error message is emitted if the components taking their value from
2516 -- the others choice do not have same type.
2518 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Node_Id);
2519 -- Analyzes and resolves expression Expr against the Etype of the
2520 -- Component. This routine also applies all appropriate checks to Expr.
2521 -- It finally saves a Expr in the newly created association list that
2522 -- will be attached to the final record aggregate. Note that if the
2523 -- Parent pointer of Expr is not set then Expr was produced with a
2524 -- New_Copy_Tree or some such.
2526 ---------------------
2527 -- Add_Association --
2528 ---------------------
2530 procedure Add_Association
2531 (Component : Entity_Id;
2533 Assoc_List : List_Id;
2534 Is_Box_Present : Boolean := False)
2536 Choice_List : constant List_Id := New_List;
2537 New_Assoc : Node_Id;
2540 Append (New_Occurrence_Of (Component, Sloc (Expr)), Choice_List);
2542 Make_Component_Association (Sloc (Expr),
2543 Choices => Choice_List,
2545 Box_Present => Is_Box_Present);
2546 Append (New_Assoc, Assoc_List);
2547 end Add_Association;
2553 function Discr_Present (Discr : Entity_Id) return Boolean is
2554 Regular_Aggr : constant Boolean := Nkind (N) /= N_Extension_Aggregate;
2559 Discr_Expr : Node_Id;
2561 Ancestor_Typ : Entity_Id;
2562 Orig_Discr : Entity_Id;
2564 D_Val : Elmt_Id := No_Elmt; -- stop junk warning
2566 Ancestor_Is_Subtyp : Boolean;
2569 if Regular_Aggr then
2573 Ancestor := Ancestor_Part (N);
2574 Ancestor_Typ := Etype (Ancestor);
2575 Loc := Sloc (Ancestor);
2577 -- For a private type with unknown discriminants, use the underlying
2578 -- record view if it is available.
2580 if Has_Unknown_Discriminants (Ancestor_Typ)
2581 and then Present (Full_View (Ancestor_Typ))
2582 and then Present (Underlying_Record_View (Full_View (Ancestor_Typ)))
2584 Ancestor_Typ := Underlying_Record_View (Full_View (Ancestor_Typ));
2587 Ancestor_Is_Subtyp :=
2588 Is_Entity_Name (Ancestor) and then Is_Type (Entity (Ancestor));
2590 -- If the ancestor part has no discriminants clearly N's aggregate
2591 -- part must provide a value for Discr.
2593 if not Has_Discriminants (Ancestor_Typ) then
2596 -- If the ancestor part is an unconstrained subtype mark then the
2597 -- Discr must be present in N's aggregate part.
2599 elsif Ancestor_Is_Subtyp
2600 and then not Is_Constrained (Entity (Ancestor))
2605 -- Now look to see if Discr was specified in the ancestor part
2607 if Ancestor_Is_Subtyp then
2608 D_Val := First_Elmt (Discriminant_Constraint (Entity (Ancestor)));
2611 Orig_Discr := Original_Record_Component (Discr);
2613 D := First_Discriminant (Ancestor_Typ);
2614 while Present (D) loop
2616 -- If Ancestor has already specified Disc value then insert its
2617 -- value in the final aggregate.
2619 if Original_Record_Component (D) = Orig_Discr then
2620 if Ancestor_Is_Subtyp then
2621 Discr_Expr := New_Copy_Tree (Node (D_Val));
2624 Make_Selected_Component (Loc,
2625 Prefix => Duplicate_Subexpr (Ancestor),
2626 Selector_Name => New_Occurrence_Of (Discr, Loc));
2629 Resolve_Aggr_Expr (Discr_Expr, Discr);
2633 Next_Discriminant (D);
2635 if Ancestor_Is_Subtyp then
2650 Consider_Others_Choice : Boolean := False)
2654 Expr : Node_Id := Empty;
2655 Selector_Name : Node_Id;
2658 Is_Box_Present := False;
2660 if Present (From) then
2661 Assoc := First (From);
2666 while Present (Assoc) loop
2667 Selector_Name := First (Choices (Assoc));
2668 while Present (Selector_Name) loop
2669 if Nkind (Selector_Name) = N_Others_Choice then
2670 if Consider_Others_Choice and then No (Expr) then
2672 -- We need to duplicate the expression for each
2673 -- successive component covered by the others choice.
2674 -- This is redundant if the others_choice covers only
2675 -- one component (small optimization possible???), but
2676 -- indispensable otherwise, because each one must be
2677 -- expanded individually to preserve side-effects.
2679 -- Ada 2005 (AI-287): In case of default initialization
2680 -- of components, we duplicate the corresponding default
2681 -- expression (from the record type declaration). The
2682 -- copy must carry the sloc of the association (not the
2683 -- original expression) to prevent spurious elaboration
2684 -- checks when the default includes function calls.
2686 if Box_Present (Assoc) then
2688 Is_Box_Present := True;
2690 if Expander_Active then
2693 (Expression (Parent (Compon)),
2694 New_Sloc => Sloc (Assoc));
2696 return Expression (Parent (Compon));
2700 if Present (Others_Etype) and then
2701 Base_Type (Others_Etype) /= Base_Type (Etype
2704 Error_Msg_N ("components in OTHERS choice must " &
2705 "have same type", Selector_Name);
2708 Others_Etype := Etype (Compon);
2710 if Expander_Active then
2711 return New_Copy_Tree (Expression (Assoc));
2713 return Expression (Assoc);
2718 elsif Chars (Compon) = Chars (Selector_Name) then
2721 -- Ada 2005 (AI-231)
2723 if Ada_Version >= Ada_05
2724 and then Known_Null (Expression (Assoc))
2726 Check_Can_Never_Be_Null (Compon, Expression (Assoc));
2729 -- We need to duplicate the expression when several
2730 -- components are grouped together with a "|" choice.
2731 -- For instance "filed1 | filed2 => Expr"
2733 -- Ada 2005 (AI-287)
2735 if Box_Present (Assoc) then
2736 Is_Box_Present := True;
2738 -- Duplicate the default expression of the component
2739 -- from the record type declaration, so a new copy
2740 -- can be attached to the association.
2742 -- Note that we always copy the default expression,
2743 -- even when the association has a single choice, in
2744 -- order to create a proper association for the
2745 -- expanded aggregate.
2747 Expr := New_Copy_Tree (Expression (Parent (Compon)));
2750 if Present (Next (Selector_Name)) then
2751 Expr := New_Copy_Tree (Expression (Assoc));
2753 Expr := Expression (Assoc);
2757 Generate_Reference (Compon, Selector_Name, 'm');
2761 ("more than one value supplied for &",
2762 Selector_Name, Compon);
2767 Next (Selector_Name);
2776 -----------------------
2777 -- Resolve_Aggr_Expr --
2778 -----------------------
2780 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Node_Id) is
2781 New_C : Entity_Id := Component;
2782 Expr_Type : Entity_Id := Empty;
2784 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean;
2785 -- If the expression is an aggregate (possibly qualified) then its
2786 -- expansion is delayed until the enclosing aggregate is expanded
2787 -- into assignments. In that case, do not generate checks on the
2788 -- expression, because they will be generated later, and will other-
2789 -- wise force a copy (to remove side-effects) that would leave a
2790 -- dynamic-sized aggregate in the code, something that gigi cannot
2794 -- Set to True if the resolved Expr node needs to be relocated
2795 -- when attached to the newly created association list. This node
2796 -- need not be relocated if its parent pointer is not set.
2797 -- In fact in this case Expr is the output of a New_Copy_Tree call.
2798 -- if Relocate is True then we have analyzed the expression node
2799 -- in the original aggregate and hence it needs to be relocated
2800 -- when moved over the new association list.
2802 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean is
2803 Kind : constant Node_Kind := Nkind (Expr);
2805 return (Nkind_In (Kind, N_Aggregate, N_Extension_Aggregate)
2806 and then Present (Etype (Expr))
2807 and then Is_Record_Type (Etype (Expr))
2808 and then Expansion_Delayed (Expr))
2809 or else (Kind = N_Qualified_Expression
2810 and then Has_Expansion_Delayed (Expression (Expr)));
2811 end Has_Expansion_Delayed;
2813 -- Start of processing for Resolve_Aggr_Expr
2816 -- If the type of the component is elementary or the type of the
2817 -- aggregate does not contain discriminants, use the type of the
2818 -- component to resolve Expr.
2820 if Is_Elementary_Type (Etype (Component))
2821 or else not Has_Discriminants (Etype (N))
2823 Expr_Type := Etype (Component);
2825 -- Otherwise we have to pick up the new type of the component from
2826 -- the new constrained subtype of the aggregate. In fact components
2827 -- which are of a composite type might be constrained by a
2828 -- discriminant, and we want to resolve Expr against the subtype were
2829 -- all discriminant occurrences are replaced with their actual value.
2832 New_C := First_Component (Etype (N));
2833 while Present (New_C) loop
2834 if Chars (New_C) = Chars (Component) then
2835 Expr_Type := Etype (New_C);
2839 Next_Component (New_C);
2842 pragma Assert (Present (Expr_Type));
2844 -- For each range in an array type where a discriminant has been
2845 -- replaced with the constraint, check that this range is within
2846 -- the range of the base type. This checks is done in the init
2847 -- proc for regular objects, but has to be done here for
2848 -- aggregates since no init proc is called for them.
2850 if Is_Array_Type (Expr_Type) then
2853 -- Range of the current constrained index in the array
2855 Orig_Index : Node_Id := First_Index (Etype (Component));
2856 -- Range corresponding to the range Index above in the
2857 -- original unconstrained record type. The bounds of this
2858 -- range may be governed by discriminants.
2860 Unconstr_Index : Node_Id := First_Index (Etype (Expr_Type));
2861 -- Range corresponding to the range Index above for the
2862 -- unconstrained array type. This range is needed to apply
2866 Index := First_Index (Expr_Type);
2867 while Present (Index) loop
2868 if Depends_On_Discriminant (Orig_Index) then
2869 Apply_Range_Check (Index, Etype (Unconstr_Index));
2873 Next_Index (Orig_Index);
2874 Next_Index (Unconstr_Index);
2880 -- If the Parent pointer of Expr is not set, Expr is an expression
2881 -- duplicated by New_Tree_Copy (this happens for record aggregates
2882 -- that look like (Field1 | Filed2 => Expr) or (others => Expr)).
2883 -- Such a duplicated expression must be attached to the tree
2884 -- before analysis and resolution to enforce the rule that a tree
2885 -- fragment should never be analyzed or resolved unless it is
2886 -- attached to the current compilation unit.
2888 if No (Parent (Expr)) then
2889 Set_Parent (Expr, N);
2895 Analyze_And_Resolve (Expr, Expr_Type);
2896 Check_Expr_OK_In_Limited_Aggregate (Expr);
2897 Check_Non_Static_Context (Expr);
2898 Check_Unset_Reference (Expr);
2900 -- Check wrong use of class-wide types
2902 if Is_Class_Wide_Type (Etype (Expr)) then
2903 Error_Msg_N ("dynamically tagged expression not allowed", Expr);
2906 if not Has_Expansion_Delayed (Expr) then
2907 Aggregate_Constraint_Checks (Expr, Expr_Type);
2910 if Raises_Constraint_Error (Expr) then
2911 Set_Raises_Constraint_Error (N);
2914 -- If the expression has been marked as requiring a range check,
2915 -- then generate it here.
2917 if Do_Range_Check (Expr) then
2918 Set_Do_Range_Check (Expr, False);
2919 Generate_Range_Check (Expr, Expr_Type, CE_Range_Check_Failed);
2923 Add_Association (New_C, Relocate_Node (Expr), New_Assoc_List);
2925 Add_Association (New_C, Expr, New_Assoc_List);
2927 end Resolve_Aggr_Expr;
2929 -- Start of processing for Resolve_Record_Aggregate
2932 -- We may end up calling Duplicate_Subexpr on expressions that are
2933 -- attached to New_Assoc_List. For this reason we need to attach it
2934 -- to the tree by setting its parent pointer to N. This parent point
2935 -- will change in STEP 8 below.
2937 Set_Parent (New_Assoc_List, N);
2939 -- STEP 1: abstract type and null record verification
2941 if Is_Abstract_Type (Typ) then
2942 Error_Msg_N ("type of aggregate cannot be abstract", N);
2945 if No (First_Entity (Typ)) and then Null_Record_Present (N) then
2949 elsif Present (First_Entity (Typ))
2950 and then Null_Record_Present (N)
2951 and then not Is_Tagged_Type (Typ)
2953 Error_Msg_N ("record aggregate cannot be null", N);
2956 -- If the type has no components, then the aggregate should either
2957 -- have "null record", or in Ada 2005 it could instead have a single
2958 -- component association given by "others => <>". For Ada 95 we flag
2959 -- an error at this point, but for Ada 2005 we proceed with checking
2960 -- the associations below, which will catch the case where it's not
2961 -- an aggregate with "others => <>". Note that the legality of a <>
2962 -- aggregate for a null record type was established by AI05-016.
2964 elsif No (First_Entity (Typ))
2965 and then Ada_Version < Ada_05
2967 Error_Msg_N ("record aggregate must be null", N);
2971 -- STEP 2: Verify aggregate structure
2974 Selector_Name : Node_Id;
2975 Bad_Aggregate : Boolean := False;
2978 if Present (Component_Associations (N)) then
2979 Assoc := First (Component_Associations (N));
2984 while Present (Assoc) loop
2985 Selector_Name := First (Choices (Assoc));
2986 while Present (Selector_Name) loop
2987 if Nkind (Selector_Name) = N_Identifier then
2990 elsif Nkind (Selector_Name) = N_Others_Choice then
2991 if Selector_Name /= First (Choices (Assoc))
2992 or else Present (Next (Selector_Name))
2994 Error_Msg_N ("OTHERS must appear alone in a choice list",
2998 elsif Present (Next (Assoc)) then
2999 Error_Msg_N ("OTHERS must appear last in an aggregate",
3003 -- (Ada2005): If this is an association with a box,
3004 -- indicate that the association need not represent
3007 elsif Box_Present (Assoc) then
3013 ("selector name should be identifier or OTHERS",
3015 Bad_Aggregate := True;
3018 Next (Selector_Name);
3024 if Bad_Aggregate then
3029 -- STEP 3: Find discriminant Values
3032 Discrim : Entity_Id;
3033 Missing_Discriminants : Boolean := False;
3036 if Present (Expressions (N)) then
3037 Positional_Expr := First (Expressions (N));
3039 Positional_Expr := Empty;
3042 if Has_Unknown_Discriminants (Typ)
3043 and then Present (Underlying_Record_View (Typ))
3045 Discrim := First_Discriminant (Underlying_Record_View (Typ));
3046 elsif Has_Discriminants (Typ) then
3047 Discrim := First_Discriminant (Typ);
3052 -- First find the discriminant values in the positional components
3054 while Present (Discrim) and then Present (Positional_Expr) loop
3055 if Discr_Present (Discrim) then
3056 Resolve_Aggr_Expr (Positional_Expr, Discrim);
3058 -- Ada 2005 (AI-231)
3060 if Ada_Version >= Ada_05
3061 and then Known_Null (Positional_Expr)
3063 Check_Can_Never_Be_Null (Discrim, Positional_Expr);
3066 Next (Positional_Expr);
3069 if Present (Get_Value (Discrim, Component_Associations (N))) then
3071 ("more than one value supplied for discriminant&",
3075 Next_Discriminant (Discrim);
3078 -- Find remaining discriminant values, if any, among named components
3080 while Present (Discrim) loop
3081 Expr := Get_Value (Discrim, Component_Associations (N), True);
3083 if not Discr_Present (Discrim) then
3084 if Present (Expr) then
3086 ("more than one value supplied for discriminant&",
3090 elsif No (Expr) then
3092 ("no value supplied for discriminant &", N, Discrim);
3093 Missing_Discriminants := True;
3096 Resolve_Aggr_Expr (Expr, Discrim);
3099 Next_Discriminant (Discrim);
3102 if Missing_Discriminants then
3106 -- At this point and until the beginning of STEP 6, New_Assoc_List
3107 -- contains only the discriminants and their values.
3111 -- STEP 4: Set the Etype of the record aggregate
3113 -- ??? This code is pretty much a copy of Sem_Ch3.Build_Subtype. That
3114 -- routine should really be exported in sem_util or some such and used
3115 -- in sem_ch3 and here rather than have a copy of the code which is a
3116 -- maintenance nightmare.
3118 -- ??? Performance WARNING. The current implementation creates a new
3119 -- itype for all aggregates whose base type is discriminated.
3120 -- This means that for record aggregates nested inside an array
3121 -- aggregate we will create a new itype for each record aggregate
3122 -- if the array component type has discriminants. For large aggregates
3123 -- this may be a problem. What should be done in this case is
3124 -- to reuse itypes as much as possible.
3126 if Has_Discriminants (Typ)
3127 or else (Has_Unknown_Discriminants (Typ)
3128 and then Present (Underlying_Record_View (Typ)))
3130 Build_Constrained_Itype : declare
3131 Loc : constant Source_Ptr := Sloc (N);
3133 Subtyp_Decl : Node_Id;
3136 C : constant List_Id := New_List;
3139 New_Assoc := First (New_Assoc_List);
3140 while Present (New_Assoc) loop
3141 Append (Duplicate_Subexpr (Expression (New_Assoc)), To => C);
3145 if Has_Unknown_Discriminants (Typ)
3146 and then Present (Underlying_Record_View (Typ))
3149 Make_Subtype_Indication (Loc,
3151 New_Occurrence_Of (Underlying_Record_View (Typ), Loc),
3153 Make_Index_Or_Discriminant_Constraint (Loc, C));
3156 Make_Subtype_Indication (Loc,
3158 New_Occurrence_Of (Base_Type (Typ), Loc),
3160 Make_Index_Or_Discriminant_Constraint (Loc, C));
3163 Def_Id := Create_Itype (Ekind (Typ), N);
3166 Make_Subtype_Declaration (Loc,
3167 Defining_Identifier => Def_Id,
3168 Subtype_Indication => Indic);
3169 Set_Parent (Subtyp_Decl, Parent (N));
3171 -- Itypes must be analyzed with checks off (see itypes.ads)
3173 Analyze (Subtyp_Decl, Suppress => All_Checks);
3175 Set_Etype (N, Def_Id);
3176 Check_Static_Discriminated_Subtype
3177 (Def_Id, Expression (First (New_Assoc_List)));
3178 end Build_Constrained_Itype;
3184 -- STEP 5: Get remaining components according to discriminant values
3187 Record_Def : Node_Id;
3188 Parent_Typ : Entity_Id;
3189 Root_Typ : Entity_Id;
3190 Parent_Typ_List : Elist_Id;
3191 Parent_Elmt : Elmt_Id;
3192 Errors_Found : Boolean := False;
3196 if Is_Derived_Type (Typ) and then Is_Tagged_Type (Typ) then
3197 Parent_Typ_List := New_Elmt_List;
3199 -- If this is an extension aggregate, the component list must
3200 -- include all components that are not in the given ancestor type.
3201 -- Otherwise, the component list must include components of all
3202 -- ancestors, starting with the root.
3204 if Nkind (N) = N_Extension_Aggregate then
3205 Root_Typ := Base_Type (Etype (Ancestor_Part (N)));
3208 Root_Typ := Root_Type (Typ);
3210 if Nkind (Parent (Base_Type (Root_Typ))) =
3211 N_Private_Type_Declaration
3214 ("type of aggregate has private ancestor&!",
3216 Error_Msg_N ("must use extension aggregate!", N);
3220 Dnode := Declaration_Node (Base_Type (Root_Typ));
3222 -- If we don't get a full declaration, then we have some
3223 -- error which will get signalled later so skip this part.
3224 -- Otherwise, gather components of root that apply to the
3225 -- aggregate type. We use the base type in case there is an
3226 -- applicable stored constraint that renames the discriminants
3229 if Nkind (Dnode) = N_Full_Type_Declaration then
3230 Record_Def := Type_Definition (Dnode);
3231 Gather_Components (Base_Type (Typ),
3232 Component_List (Record_Def),
3233 Governed_By => New_Assoc_List,
3235 Report_Errors => Errors_Found);
3239 Parent_Typ := Base_Type (Typ);
3240 while Parent_Typ /= Root_Typ loop
3241 Prepend_Elmt (Parent_Typ, To => Parent_Typ_List);
3242 Parent_Typ := Etype (Parent_Typ);
3244 if Nkind (Parent (Base_Type (Parent_Typ))) =
3245 N_Private_Type_Declaration
3246 or else Nkind (Parent (Base_Type (Parent_Typ))) =
3247 N_Private_Extension_Declaration
3249 if Nkind (N) /= N_Extension_Aggregate then
3251 ("type of aggregate has private ancestor&!",
3253 Error_Msg_N ("must use extension aggregate!", N);
3256 elsif Parent_Typ /= Root_Typ then
3258 ("ancestor part of aggregate must be private type&",
3259 Ancestor_Part (N), Parent_Typ);
3265 -- Now collect components from all other ancestors, beginning
3266 -- with the current type. If the type has unknown discriminants
3267 -- use the component list of the Underlying_Record_View, which
3268 -- needs to be used for the subsequent expansion of the aggregate
3269 -- into assignments.
3271 Parent_Elmt := First_Elmt (Parent_Typ_List);
3272 while Present (Parent_Elmt) loop
3273 Parent_Typ := Node (Parent_Elmt);
3275 if Has_Unknown_Discriminants (Parent_Typ)
3276 and then Present (Underlying_Record_View (Typ))
3278 Parent_Typ := Underlying_Record_View (Parent_Typ);
3281 Record_Def := Type_Definition (Parent (Base_Type (Parent_Typ)));
3282 Gather_Components (Empty,
3283 Component_List (Record_Extension_Part (Record_Def)),
3284 Governed_By => New_Assoc_List,
3286 Report_Errors => Errors_Found);
3288 Next_Elmt (Parent_Elmt);
3292 Record_Def := Type_Definition (Parent (Base_Type (Typ)));
3294 if Null_Present (Record_Def) then
3297 elsif not Has_Unknown_Discriminants (Typ) then
3298 Gather_Components (Base_Type (Typ),
3299 Component_List (Record_Def),
3300 Governed_By => New_Assoc_List,
3302 Report_Errors => Errors_Found);
3306 (Base_Type (Underlying_Record_View (Typ)),
3307 Component_List (Record_Def),
3308 Governed_By => New_Assoc_List,
3310 Report_Errors => Errors_Found);
3314 if Errors_Found then
3319 -- STEP 6: Find component Values
3322 Component_Elmt := First_Elmt (Components);
3324 -- First scan the remaining positional associations in the aggregate.
3325 -- Remember that at this point Positional_Expr contains the current
3326 -- positional association if any is left after looking for discriminant
3327 -- values in step 3.
3329 while Present (Positional_Expr) and then Present (Component_Elmt) loop
3330 Component := Node (Component_Elmt);
3331 Resolve_Aggr_Expr (Positional_Expr, Component);
3333 -- Ada 2005 (AI-231)
3335 if Ada_Version >= Ada_05
3336 and then Known_Null (Positional_Expr)
3338 Check_Can_Never_Be_Null (Component, Positional_Expr);
3341 if Present (Get_Value (Component, Component_Associations (N))) then
3343 ("more than one value supplied for Component &", N, Component);
3346 Next (Positional_Expr);
3347 Next_Elmt (Component_Elmt);
3350 if Present (Positional_Expr) then
3352 ("too many components for record aggregate", Positional_Expr);
3355 -- Now scan for the named arguments of the aggregate
3357 while Present (Component_Elmt) loop
3358 Component := Node (Component_Elmt);
3359 Expr := Get_Value (Component, Component_Associations (N), True);
3361 -- Note: The previous call to Get_Value sets the value of the
3362 -- variable Is_Box_Present.
3364 -- Ada 2005 (AI-287): Handle components with default initialization.
3365 -- Note: This feature was originally added to Ada 2005 for limited
3366 -- but it was finally allowed with any type.
3368 if Is_Box_Present then
3369 Check_Box_Component : declare
3370 Ctyp : constant Entity_Id := Etype (Component);
3373 -- If there is a default expression for the aggregate, copy
3374 -- it into a new association.
3376 -- If the component has an initialization procedure (IP) we
3377 -- pass the component to the expander, which will generate
3378 -- the call to such IP.
3380 -- If the component has discriminants, their values must
3381 -- be taken from their subtype. This is indispensable for
3382 -- constraints that are given by the current instance of an
3383 -- enclosing type, to allow the expansion of the aggregate
3384 -- to replace the reference to the current instance by the
3385 -- target object of the aggregate.
3387 if Present (Parent (Component))
3389 Nkind (Parent (Component)) = N_Component_Declaration
3390 and then Present (Expression (Parent (Component)))
3393 New_Copy_Tree (Expression (Parent (Component)),
3394 New_Sloc => Sloc (N));
3397 (Component => Component,
3399 Assoc_List => New_Assoc_List);
3400 Set_Has_Self_Reference (N);
3402 -- A box-defaulted access component gets the value null. Also
3403 -- included are components of private types whose underlying
3404 -- type is an access type. In either case set the type of the
3405 -- literal, for subsequent use in semantic checks.
3407 elsif Present (Underlying_Type (Ctyp))
3408 and then Is_Access_Type (Underlying_Type (Ctyp))
3410 if not Is_Private_Type (Ctyp) then
3411 Expr := Make_Null (Sloc (N));
3412 Set_Etype (Expr, Ctyp);
3414 (Component => Component,
3416 Assoc_List => New_Assoc_List);
3418 -- If the component's type is private with an access type as
3419 -- its underlying type then we have to create an unchecked
3420 -- conversion to satisfy type checking.
3424 Qual_Null : constant Node_Id :=
3425 Make_Qualified_Expression (Sloc (N),
3428 (Underlying_Type (Ctyp), Sloc (N)),
3429 Expression => Make_Null (Sloc (N)));
3431 Convert_Null : constant Node_Id :=
3432 Unchecked_Convert_To
3436 Analyze_And_Resolve (Convert_Null, Ctyp);
3438 (Component => Component,
3439 Expr => Convert_Null,
3440 Assoc_List => New_Assoc_List);
3444 elsif Has_Non_Null_Base_Init_Proc (Ctyp)
3445 or else not Expander_Active
3447 if Is_Record_Type (Ctyp)
3448 and then Has_Discriminants (Ctyp)
3449 and then not Is_Private_Type (Ctyp)
3451 -- We build a partially initialized aggregate with the
3452 -- values of the discriminants and box initialization
3453 -- for the rest, if other components are present.
3454 -- The type of the aggregate is the known subtype of
3455 -- the component. The capture of discriminants must
3456 -- be recursive because subcomponents may be contrained
3457 -- (transitively) by discriminants of enclosing types.
3458 -- For a private type with discriminants, a call to the
3459 -- initialization procedure will be generated, and no
3460 -- subaggregate is needed.
3462 Capture_Discriminants : declare
3463 Loc : constant Source_Ptr := Sloc (N);
3466 procedure Add_Discriminant_Values
3467 (New_Aggr : Node_Id;
3468 Assoc_List : List_Id);
3469 -- The constraint to a component may be given by a
3470 -- discriminant of the enclosing type, in which case
3471 -- we have to retrieve its value, which is part of the
3472 -- enclosing aggregate. Assoc_List provides the
3473 -- discriminant associations of the current type or
3474 -- of some enclosing record.
3476 procedure Propagate_Discriminants
3478 Assoc_List : List_Id;
3480 -- Nested components may themselves be discriminated
3481 -- types constrained by outer discriminants, whose
3482 -- values must be captured before the aggregate is
3483 -- expanded into assignments.
3485 -----------------------------
3486 -- Add_Discriminant_Values --
3487 -----------------------------
3489 procedure Add_Discriminant_Values
3490 (New_Aggr : Node_Id;
3491 Assoc_List : List_Id)
3495 Discr_Elmt : Elmt_Id;
3496 Discr_Val : Node_Id;
3500 Discr := First_Discriminant (Etype (New_Aggr));
3503 (Discriminant_Constraint (Etype (New_Aggr)));
3504 while Present (Discr_Elmt) loop
3505 Discr_Val := Node (Discr_Elmt);
3507 -- If the constraint is given by a discriminant
3508 -- it is a discriminant of an enclosing record,
3509 -- and its value has already been placed in the
3510 -- association list.
3512 if Is_Entity_Name (Discr_Val)
3514 Ekind (Entity (Discr_Val)) = E_Discriminant
3516 Val := Entity (Discr_Val);
3518 Assoc := First (Assoc_List);
3519 while Present (Assoc) loop
3521 (Entity (First (Choices (Assoc))))
3523 Entity (First (Choices (Assoc)))
3526 Discr_Val := Expression (Assoc);
3534 (Discr, New_Copy_Tree (Discr_Val),
3535 Component_Associations (New_Aggr));
3537 -- If the discriminant constraint is a current
3538 -- instance, mark the current aggregate so that
3539 -- the self-reference can be expanded later.
3541 if Nkind (Discr_Val) = N_Attribute_Reference
3542 and then Is_Entity_Name (Prefix (Discr_Val))
3543 and then Is_Type (Entity (Prefix (Discr_Val)))
3544 and then Etype (N) =
3545 Entity (Prefix (Discr_Val))
3547 Set_Has_Self_Reference (N);
3550 Next_Elmt (Discr_Elmt);
3551 Next_Discriminant (Discr);
3553 end Add_Discriminant_Values;
3555 ------------------------------
3556 -- Propagate_Discriminants --
3557 ------------------------------
3559 procedure Propagate_Discriminants
3561 Assoc_List : List_Id;
3564 Inner_Comp : Entity_Id;
3565 Comp_Type : Entity_Id;
3566 Needs_Box : Boolean := False;
3571 Inner_Comp := First_Component (Etype (Comp));
3572 while Present (Inner_Comp) loop
3573 Comp_Type := Etype (Inner_Comp);
3575 if Is_Record_Type (Comp_Type)
3576 and then Has_Discriminants (Comp_Type)
3579 Make_Aggregate (Loc, New_List, New_List);
3580 Set_Etype (New_Aggr, Comp_Type);
3582 (Inner_Comp, New_Aggr,
3583 Component_Associations (Aggr));
3585 -- Collect discriminant values and recurse
3587 Add_Discriminant_Values
3588 (New_Aggr, Assoc_List);
3589 Propagate_Discriminants
3590 (New_Aggr, Assoc_List, Inner_Comp);
3596 Next_Component (Inner_Comp);
3601 (Make_Component_Association (Loc,
3603 New_List (Make_Others_Choice (Loc)),
3604 Expression => Empty,
3605 Box_Present => True),
3606 Component_Associations (Aggr));
3608 end Propagate_Discriminants;
3611 Expr := Make_Aggregate (Loc, New_List, New_List);
3612 Set_Etype (Expr, Ctyp);
3614 -- If the enclosing type has discriminants, they
3615 -- have been collected in the aggregate earlier, and
3616 -- they may appear as constraints of subcomponents.
3617 -- Similarly if this component has discriminants, they
3618 -- might in turn be propagated to their components.
3620 if Has_Discriminants (Typ) then
3621 Add_Discriminant_Values (Expr, New_Assoc_List);
3622 Propagate_Discriminants
3623 (Expr, New_Assoc_List, Component);
3625 elsif Has_Discriminants (Ctyp) then
3626 Add_Discriminant_Values
3627 (Expr, Component_Associations (Expr));
3628 Propagate_Discriminants
3629 (Expr, Component_Associations (Expr), Component);
3636 -- If the type has additional components, create
3637 -- an OTHERS box association for them.
3639 Comp := First_Component (Ctyp);
3640 while Present (Comp) loop
3641 if Ekind (Comp) = E_Component then
3642 if not Is_Record_Type (Etype (Comp)) then
3644 (Make_Component_Association (Loc,
3647 (Make_Others_Choice (Loc)),
3648 Expression => Empty,
3649 Box_Present => True),
3650 Component_Associations (Expr));
3655 Next_Component (Comp);
3661 (Component => Component,
3663 Assoc_List => New_Assoc_List);
3664 end Capture_Discriminants;
3668 (Component => Component,
3670 Assoc_List => New_Assoc_List,
3671 Is_Box_Present => True);
3674 -- Otherwise we only need to resolve the expression if the
3675 -- component has partially initialized values (required to
3676 -- expand the corresponding assignments and run-time checks).
3678 elsif Present (Expr)
3679 and then Is_Partially_Initialized_Type (Ctyp)
3681 Resolve_Aggr_Expr (Expr, Component);
3683 end Check_Box_Component;
3685 elsif No (Expr) then
3687 -- Ignore hidden components associated with the position of the
3688 -- interface tags: these are initialized dynamically.
3690 if not Present (Related_Type (Component)) then
3692 ("no value supplied for component &!", N, Component);
3696 Resolve_Aggr_Expr (Expr, Component);
3699 Next_Elmt (Component_Elmt);
3702 -- STEP 7: check for invalid components + check type in choice list
3709 -- Type of first component in choice list
3712 if Present (Component_Associations (N)) then
3713 Assoc := First (Component_Associations (N));
3718 Verification : while Present (Assoc) loop
3719 Selectr := First (Choices (Assoc));
3722 if Nkind (Selectr) = N_Others_Choice then
3724 -- Ada 2005 (AI-287): others choice may have expression or box
3726 if No (Others_Etype)
3727 and then not Others_Box
3730 ("OTHERS must represent at least one component", Selectr);
3736 while Present (Selectr) loop
3737 New_Assoc := First (New_Assoc_List);
3738 while Present (New_Assoc) loop
3739 Component := First (Choices (New_Assoc));
3740 exit when Chars (Selectr) = Chars (Component);
3744 -- If no association, this is not a legal component of
3745 -- of the type in question, except if its association
3746 -- is provided with a box.
3748 if No (New_Assoc) then
3749 if Box_Present (Parent (Selectr)) then
3751 -- This may still be a bogus component with a box. Scan
3752 -- list of components to verify that a component with
3753 -- that name exists.
3759 C := First_Component (Typ);
3760 while Present (C) loop
3761 if Chars (C) = Chars (Selectr) then
3763 -- If the context is an extension aggregate,
3764 -- the component must not be inherited from
3765 -- the ancestor part of the aggregate.
3767 if Nkind (N) /= N_Extension_Aggregate
3769 Scope (Original_Record_Component (C)) /=
3770 Etype (Ancestor_Part (N))
3780 Error_Msg_Node_2 := Typ;
3781 Error_Msg_N ("& is not a component of}", Selectr);
3785 elsif Chars (Selectr) /= Name_uTag
3786 and then Chars (Selectr) /= Name_uParent
3787 and then Chars (Selectr) /= Name_uController
3789 if not Has_Discriminants (Typ) then
3790 Error_Msg_Node_2 := Typ;
3791 Error_Msg_N ("& is not a component of}", Selectr);
3794 ("& is not a component of the aggregate subtype",
3798 Check_Misspelled_Component (Components, Selectr);
3801 elsif No (Typech) then
3802 Typech := Base_Type (Etype (Component));
3804 elsif Typech /= Base_Type (Etype (Component)) then
3805 if not Box_Present (Parent (Selectr)) then
3807 ("components in choice list must have same type",
3816 end loop Verification;
3819 -- STEP 8: replace the original aggregate
3822 New_Aggregate : constant Node_Id := New_Copy (N);
3825 Set_Expressions (New_Aggregate, No_List);
3826 Set_Etype (New_Aggregate, Etype (N));
3827 Set_Component_Associations (New_Aggregate, New_Assoc_List);
3829 Rewrite (N, New_Aggregate);
3831 end Resolve_Record_Aggregate;
3833 -----------------------------
3834 -- Check_Can_Never_Be_Null --
3835 -----------------------------
3837 procedure Check_Can_Never_Be_Null (Typ : Entity_Id; Expr : Node_Id) is
3838 Comp_Typ : Entity_Id;
3842 (Ada_Version >= Ada_05
3843 and then Present (Expr)
3844 and then Known_Null (Expr));
3847 when E_Array_Type =>
3848 Comp_Typ := Component_Type (Typ);
3852 Comp_Typ := Etype (Typ);
3858 if Can_Never_Be_Null (Comp_Typ) then
3860 -- Here we know we have a constraint error. Note that we do not use
3861 -- Apply_Compile_Time_Constraint_Error here to the Expr, which might
3862 -- seem the more natural approach. That's because in some cases the
3863 -- components are rewritten, and the replacement would be missed.
3866 (Compile_Time_Constraint_Error
3868 "(Ada 2005) null not allowed in null-excluding component?"),
3869 Make_Raise_Constraint_Error (Sloc (Expr),
3870 Reason => CE_Access_Check_Failed));
3872 -- Set proper type for bogus component (why is this needed???)
3874 Set_Etype (Expr, Comp_Typ);
3875 Set_Analyzed (Expr);
3877 end Check_Can_Never_Be_Null;
3879 ---------------------
3880 -- Sort_Case_Table --
3881 ---------------------
3883 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is
3884 L : constant Int := Case_Table'First;
3885 U : constant Int := Case_Table'Last;
3893 T := Case_Table (K + 1);
3897 and then Expr_Value (Case_Table (J - 1).Choice_Lo) >
3898 Expr_Value (T.Choice_Lo)
3900 Case_Table (J) := Case_Table (J - 1);
3904 Case_Table (J) := T;
3907 end Sort_Case_Table;