1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2005 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 2, 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 COPYING. If not, write --
19 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Einfo; use Einfo;
30 with Elists; use Elists;
31 with Errout; use Errout;
32 with Exp_Util; use Exp_Util;
33 with Freeze; use Freeze;
34 with Itypes; use Itypes;
35 with Lib.Xref; use Lib.Xref;
36 with Namet; use Namet;
37 with Nmake; use Nmake;
38 with Nlists; use Nlists;
41 with Sem_Cat; use Sem_Cat;
42 with Sem_Ch8; use Sem_Ch8;
43 with Sem_Ch13; use Sem_Ch13;
44 with Sem_Eval; use Sem_Eval;
45 with Sem_Res; use Sem_Res;
46 with Sem_Util; use Sem_Util;
47 with Sem_Type; use Sem_Type;
48 with Sem_Warn; use Sem_Warn;
49 with Sinfo; use Sinfo;
50 with Snames; use Snames;
51 with Stringt; use Stringt;
52 with Stand; use Stand;
53 with Targparm; use Targparm;
54 with Tbuild; use Tbuild;
55 with Uintp; use Uintp;
57 with GNAT.Spelling_Checker; use GNAT.Spelling_Checker;
59 package body Sem_Aggr is
61 type Case_Bounds is record
64 Choice_Node : Node_Id;
67 type Case_Table_Type is array (Nat range <>) of Case_Bounds;
68 -- Table type used by Check_Case_Choices procedure
70 -----------------------
71 -- Local Subprograms --
72 -----------------------
74 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type);
75 -- Sort the Case Table using the Lower Bound of each Choice as the key.
76 -- A simple insertion sort is used since the number of choices in a case
77 -- statement of variant part will usually be small and probably in near
80 procedure Check_Can_Never_Be_Null (N : Node_Id; Expr : Node_Id);
81 -- Ada 2005 (AI-231): Check bad usage of the null-exclusion issue
83 ------------------------------------------------------
84 -- Subprograms used for RECORD AGGREGATE Processing --
85 ------------------------------------------------------
87 procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id);
88 -- This procedure performs all the semantic checks required for record
89 -- aggregates. Note that for aggregates analysis and resolution go
90 -- hand in hand. Aggregate analysis has been delayed up to here and
91 -- it is done while resolving the aggregate.
93 -- N is the N_Aggregate node.
94 -- Typ is the record type for the aggregate resolution
96 -- While performing the semantic checks, this procedure
97 -- builds a new Component_Association_List where each record field
98 -- appears alone in a Component_Choice_List along with its corresponding
99 -- expression. The record fields in the Component_Association_List
100 -- appear in the same order in which they appear in the record type Typ.
102 -- Once this new Component_Association_List is built and all the
103 -- semantic checks performed, the original aggregate subtree is replaced
104 -- with the new named record aggregate just built. Note that the subtree
105 -- substitution is performed with Rewrite so as to be
106 -- able to retrieve the original aggregate.
108 -- The aggregate subtree manipulation performed by Resolve_Record_Aggregate
109 -- yields the aggregate format expected by Gigi. Typically, this kind of
110 -- tree manipulations are done in the expander. However, because the
111 -- semantic checks that need to be performed on record aggregates really
112 -- go hand in hand with the record aggregate normalization, the aggregate
113 -- subtree transformation is performed during resolution rather than
114 -- expansion. Had we decided otherwise we would have had to duplicate
115 -- most of the code in the expansion procedure Expand_Record_Aggregate.
116 -- Note, however, that all the expansion concerning aggegates for tagged
117 -- records is done in Expand_Record_Aggregate.
119 -- The algorithm of Resolve_Record_Aggregate proceeds as follows:
121 -- 1. Make sure that the record type against which the record aggregate
122 -- has to be resolved is not abstract. Furthermore if the type is
123 -- a null aggregate make sure the input aggregate N is also null.
125 -- 2. Verify that the structure of the aggregate is that of a record
126 -- aggregate. Specifically, look for component associations and ensure
127 -- that each choice list only has identifiers or the N_Others_Choice
128 -- node. Also make sure that if present, the N_Others_Choice occurs
129 -- last and by itself.
131 -- 3. If Typ contains discriminants, the values for each discriminant
132 -- is looked for. If the record type Typ has variants, we check
133 -- that the expressions corresponding to each discriminant ruling
134 -- the (possibly nested) variant parts of Typ, are static. This
135 -- allows us to determine the variant parts to which the rest of
136 -- the aggregate must conform. The names of discriminants with their
137 -- values are saved in a new association list, New_Assoc_List which
138 -- is later augmented with the names and values of the remaining
139 -- components in the record type.
141 -- During this phase we also make sure that every discriminant is
142 -- assigned exactly one value. Note that when several values
143 -- for a given discriminant are found, semantic processing continues
144 -- looking for further errors. In this case it's the first
145 -- discriminant value found which we will be recorded.
147 -- IMPORTANT NOTE: For derived tagged types this procedure expects
148 -- First_Discriminant and Next_Discriminant to give the correct list
149 -- of discriminants, in the correct order.
151 -- 4. After all the discriminant values have been gathered, we can
152 -- set the Etype of the record aggregate. If Typ contains no
153 -- discriminants this is straightforward: the Etype of N is just
154 -- Typ, otherwise a new implicit constrained subtype of Typ is
155 -- built to be the Etype of N.
157 -- 5. Gather the remaining record components according to the discriminant
158 -- values. This involves recursively traversing the record type
159 -- structure to see what variants are selected by the given discriminant
160 -- values. This processing is a little more convoluted if Typ is a
161 -- derived tagged types since we need to retrieve the record structure
162 -- of all the ancestors of Typ.
164 -- 6. After gathering the record components we look for their values
165 -- in the record aggregate and emit appropriate error messages
166 -- should we not find such values or should they be duplicated.
168 -- 7. We then make sure no illegal component names appear in the
169 -- record aggegate and make sure that the type of the record
170 -- components appearing in a same choice list is the same.
171 -- Finally we ensure that the others choice, if present, is
172 -- used to provide the value of at least a record component.
174 -- 8. The original aggregate node is replaced with the new named
175 -- aggregate built in steps 3 through 6, as explained earlier.
177 -- Given the complexity of record aggregate resolution, the primary
178 -- goal of this routine is clarity and simplicity rather than execution
179 -- and storage efficiency. If there are only positional components in the
180 -- aggregate the running time is linear. If there are associations
181 -- the running time is still linear as long as the order of the
182 -- associations is not too far off the order of the components in the
183 -- record type. If this is not the case the running time is at worst
184 -- quadratic in the size of the association list.
186 procedure Check_Misspelled_Component
187 (Elements : Elist_Id;
188 Component : Node_Id);
189 -- Give possible misspelling diagnostic if Component is likely to be
190 -- a misspelling of one of the components of the Assoc_List.
191 -- This is called by Resolv_Aggr_Expr after producing
192 -- an invalid component error message.
194 procedure Check_Static_Discriminated_Subtype (T : Entity_Id; V : Node_Id);
195 -- An optimization: determine whether a discriminated subtype has a
196 -- static constraint, and contains array components whose length is also
197 -- static, either because they are constrained by the discriminant, or
198 -- because the original component bounds are static.
200 -----------------------------------------------------
201 -- Subprograms used for ARRAY AGGREGATE Processing --
202 -----------------------------------------------------
204 function Resolve_Array_Aggregate
207 Index_Constr : Node_Id;
208 Component_Typ : Entity_Id;
209 Others_Allowed : Boolean)
211 -- This procedure performs the semantic checks for an array aggregate.
212 -- True is returned if the aggregate resolution succeeds.
213 -- The procedure works by recursively checking each nested aggregate.
214 -- Specifically, after checking a sub-aggregate nested at the i-th level
215 -- we recursively check all the subaggregates at the i+1-st level (if any).
216 -- Note that for aggregates analysis and resolution go hand in hand.
217 -- Aggregate analysis has been delayed up to here and it is done while
218 -- resolving the aggregate.
220 -- N is the current N_Aggregate node to be checked.
222 -- Index is the index node corresponding to the array sub-aggregate that
223 -- we are currently checking (RM 4.3.3 (8)). Its Etype is the
224 -- corresponding index type (or subtype).
226 -- Index_Constr is the node giving the applicable index constraint if
227 -- any (RM 4.3.3 (10)). It "is a constraint provided by certain
228 -- contexts [...] that can be used to determine the bounds of the array
229 -- value specified by the aggregate". If Others_Allowed below is False
230 -- there is no applicable index constraint and this node is set to Index.
232 -- Component_Typ is the array component type.
234 -- Others_Allowed indicates whether an others choice is allowed
235 -- in the context where the top-level aggregate appeared.
237 -- The algorithm of Resolve_Array_Aggregate proceeds as follows:
239 -- 1. Make sure that the others choice, if present, is by itself and
240 -- appears last in the sub-aggregate. Check that we do not have
241 -- positional and named components in the array sub-aggregate (unless
242 -- the named association is an others choice). Finally if an others
243 -- choice is present, make sure it is allowed in the aggregate contex.
245 -- 2. If the array sub-aggregate contains discrete_choices:
247 -- (A) Verify their validity. Specifically verify that:
249 -- (a) If a null range is present it must be the only possible
250 -- choice in the array aggregate.
252 -- (b) Ditto for a non static range.
254 -- (c) Ditto for a non static expression.
256 -- In addition this step analyzes and resolves each discrete_choice,
257 -- making sure that its type is the type of the corresponding Index.
258 -- If we are not at the lowest array aggregate level (in the case of
259 -- multi-dimensional aggregates) then invoke Resolve_Array_Aggregate
260 -- recursively on each component expression. Otherwise, resolve the
261 -- bottom level component expressions against the expected component
262 -- type ONLY IF the component corresponds to a single discrete choice
263 -- which is not an others choice (to see why read the DELAYED
264 -- COMPONENT RESOLUTION below).
266 -- (B) Determine the bounds of the sub-aggregate and lowest and
267 -- highest choice values.
269 -- 3. For positional aggregates:
271 -- (A) Loop over the component expressions either recursively invoking
272 -- Resolve_Array_Aggregate on each of these for multi-dimensional
273 -- array aggregates or resolving the bottom level component
274 -- expressions against the expected component type.
276 -- (B) Determine the bounds of the positional sub-aggregates.
278 -- 4. Try to determine statically whether the evaluation of the array
279 -- sub-aggregate raises Constraint_Error. If yes emit proper
280 -- warnings. The precise checks are the following:
282 -- (A) Check that the index range defined by aggregate bounds is
283 -- compatible with corresponding index subtype.
284 -- We also check against the base type. In fact it could be that
285 -- Low/High bounds of the base type are static whereas those of
286 -- the index subtype are not. Thus if we can statically catch
287 -- a problem with respect to the base type we are guaranteed
288 -- that the same problem will arise with the index subtype
290 -- (B) If we are dealing with a named aggregate containing an others
291 -- choice and at least one discrete choice then make sure the range
292 -- specified by the discrete choices does not overflow the
293 -- aggregate bounds. We also check against the index type and base
294 -- type bounds for the same reasons given in (A).
296 -- (C) If we are dealing with a positional aggregate with an others
297 -- choice make sure the number of positional elements specified
298 -- does not overflow the aggregate bounds. We also check against
299 -- the index type and base type bounds as mentioned in (A).
301 -- Finally construct an N_Range node giving the sub-aggregate bounds.
302 -- Set the Aggregate_Bounds field of the sub-aggregate to be this
303 -- N_Range. The routine Array_Aggr_Subtype below uses such N_Ranges
304 -- to build the appropriate aggregate subtype. Aggregate_Bounds
305 -- information is needed during expansion.
307 -- DELAYED COMPONENT RESOLUTION: The resolution of bottom level component
308 -- expressions in an array aggregate may call Duplicate_Subexpr or some
309 -- other routine that inserts code just outside the outermost aggregate.
310 -- If the array aggregate contains discrete choices or an others choice,
311 -- this may be wrong. Consider for instance the following example.
313 -- type Rec is record
317 -- type Acc_Rec is access Rec;
318 -- Arr : array (1..3) of Acc_Rec := (1 .. 3 => new Rec);
320 -- Then the transformation of "new Rec" that occurs during resolution
321 -- entails the following code modifications
323 -- P7b : constant Acc_Rec := new Rec;
325 -- Arr : array (1..3) of Acc_Rec := (1 .. 3 => P7b);
327 -- This code transformation is clearly wrong, since we need to call
328 -- "new Rec" for each of the 3 array elements. To avoid this problem we
329 -- delay resolution of the components of non positional array aggregates
330 -- to the expansion phase. As an optimization, if the discrete choice
331 -- specifies a single value we do not delay resolution.
333 function Array_Aggr_Subtype (N : Node_Id; Typ : Node_Id) return Entity_Id;
334 -- This routine returns the type or subtype of an array aggregate.
336 -- N is the array aggregate node whose type we return.
338 -- Typ is the context type in which N occurs.
340 -- This routine creates an implicit array subtype whose bounds are
341 -- those defined by the aggregate. When this routine is invoked
342 -- Resolve_Array_Aggregate has already processed aggregate N. Thus the
343 -- Aggregate_Bounds of each sub-aggregate, is an N_Range node giving the
344 -- sub-aggregate bounds. When building the aggegate itype, this function
345 -- traverses the array aggregate N collecting such Aggregate_Bounds and
346 -- constructs the proper array aggregate itype.
348 -- Note that in the case of multidimensional aggregates each inner
349 -- sub-aggregate corresponding to a given array dimension, may provide a
350 -- different bounds. If it is possible to determine statically that
351 -- some sub-aggregates corresponding to the same index do not have the
352 -- same bounds, then a warning is emitted. If such check is not possible
353 -- statically (because some sub-aggregate bounds are dynamic expressions)
354 -- then this job is left to the expander. In all cases the particular
355 -- bounds that this function will chose for a given dimension is the first
356 -- N_Range node for a sub-aggregate corresponding to that dimension.
358 -- Note that the Raises_Constraint_Error flag of an array aggregate
359 -- whose evaluation is determined to raise CE by Resolve_Array_Aggregate,
360 -- is set in Resolve_Array_Aggregate but the aggregate is not
361 -- immediately replaced with a raise CE. In fact, Array_Aggr_Subtype must
362 -- first construct the proper itype for the aggregate (Gigi needs
363 -- this). After constructing the proper itype we will eventually replace
364 -- the top-level aggregate with a raise CE (done in Resolve_Aggregate).
365 -- Of course in cases such as:
367 -- type Arr is array (integer range <>) of Integer;
368 -- A : Arr := (positive range -1 .. 2 => 0);
370 -- The bounds of the aggregate itype are cooked up to look reasonable
371 -- (in this particular case the bounds will be 1 .. 2).
373 procedure Aggregate_Constraint_Checks
375 Check_Typ : Entity_Id);
376 -- Checks expression Exp against subtype Check_Typ. If Exp is an
377 -- aggregate and Check_Typ a constrained record type with discriminants,
378 -- we generate the appropriate discriminant checks. If Exp is an array
379 -- aggregate then emit the appropriate length checks. If Exp is a scalar
380 -- type, or a string literal, Exp is changed into Check_Typ'(Exp) to
381 -- ensure that range checks are performed at run time.
383 procedure Make_String_Into_Aggregate (N : Node_Id);
384 -- A string literal can appear in a context in which a one dimensional
385 -- array of characters is expected. This procedure simply rewrites the
386 -- string as an aggregate, prior to resolution.
388 ---------------------------------
389 -- Aggregate_Constraint_Checks --
390 ---------------------------------
392 procedure Aggregate_Constraint_Checks
394 Check_Typ : Entity_Id)
396 Exp_Typ : constant Entity_Id := Etype (Exp);
399 if Raises_Constraint_Error (Exp) then
403 -- This is really expansion activity, so make sure that expansion
404 -- is on and is allowed.
406 if not Expander_Active or else In_Default_Expression then
410 -- First check if we have to insert discriminant checks
412 if Has_Discriminants (Exp_Typ) then
413 Apply_Discriminant_Check (Exp, Check_Typ);
415 -- Next emit length checks for array aggregates
417 elsif Is_Array_Type (Exp_Typ) then
418 Apply_Length_Check (Exp, Check_Typ);
420 -- Finally emit scalar and string checks. If we are dealing with a
421 -- scalar literal we need to check by hand because the Etype of
422 -- literals is not necessarily correct.
424 elsif Is_Scalar_Type (Exp_Typ)
425 and then Compile_Time_Known_Value (Exp)
427 if Is_Out_Of_Range (Exp, Base_Type (Check_Typ)) then
428 Apply_Compile_Time_Constraint_Error
429 (Exp, "value not in range of}?", CE_Range_Check_Failed,
430 Ent => Base_Type (Check_Typ),
431 Typ => Base_Type (Check_Typ));
433 elsif Is_Out_Of_Range (Exp, Check_Typ) then
434 Apply_Compile_Time_Constraint_Error
435 (Exp, "value not in range of}?", CE_Range_Check_Failed,
439 elsif not Range_Checks_Suppressed (Check_Typ) then
440 Apply_Scalar_Range_Check (Exp, Check_Typ);
443 elsif (Is_Scalar_Type (Exp_Typ)
444 or else Nkind (Exp) = N_String_Literal)
445 and then Exp_Typ /= Check_Typ
447 if Is_Entity_Name (Exp)
448 and then Ekind (Entity (Exp)) = E_Constant
450 -- If expression is a constant, it is worthwhile checking whether
451 -- it is a bound of the type.
453 if (Is_Entity_Name (Type_Low_Bound (Check_Typ))
454 and then Entity (Exp) = Entity (Type_Low_Bound (Check_Typ)))
455 or else (Is_Entity_Name (Type_High_Bound (Check_Typ))
456 and then Entity (Exp) = Entity (Type_High_Bound (Check_Typ)))
461 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
462 Analyze_And_Resolve (Exp, Check_Typ);
463 Check_Unset_Reference (Exp);
466 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
467 Analyze_And_Resolve (Exp, Check_Typ);
468 Check_Unset_Reference (Exp);
471 -- Ada 2005 (AI-230): Generate a conversion to an anonymous access
472 -- component's type to force the appropriate accessibility checks.
474 -- Ada 2005 (AI-231): Generate conversion to the null-excluding
475 -- type to force the corresponding run-time check
477 elsif Is_Access_Type (Check_Typ)
478 and then ((Is_Local_Anonymous_Access (Check_Typ))
479 or else (Can_Never_Be_Null (Check_Typ)
480 and then not Can_Never_Be_Null (Exp_Typ)))
482 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
483 Analyze_And_Resolve (Exp, Check_Typ);
484 Check_Unset_Reference (Exp);
486 end Aggregate_Constraint_Checks;
488 ------------------------
489 -- Array_Aggr_Subtype --
490 ------------------------
492 function Array_Aggr_Subtype
497 Aggr_Dimension : constant Pos := Number_Dimensions (Typ);
498 -- Number of aggregate index dimensions.
500 Aggr_Range : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
501 -- Constrained N_Range of each index dimension in our aggregate itype.
503 Aggr_Low : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
504 Aggr_High : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
505 -- Low and High bounds for each index dimension in our aggregate itype.
507 Is_Fully_Positional : Boolean := True;
509 procedure Collect_Aggr_Bounds (N : Node_Id; Dim : Pos);
510 -- N is an array (sub-)aggregate. Dim is the dimension corresponding to
511 -- (sub-)aggregate N. This procedure collects the constrained N_Range
512 -- nodes corresponding to each index dimension of our aggregate itype.
513 -- These N_Range nodes are collected in Aggr_Range above.
514 -- Likewise collect in Aggr_Low & Aggr_High above the low and high
515 -- bounds of each index dimension. If, when collecting, two bounds
516 -- corresponding to the same dimension are static and found to differ,
517 -- then emit a warning, and mark N as raising Constraint_Error.
519 -------------------------
520 -- Collect_Aggr_Bounds --
521 -------------------------
523 procedure Collect_Aggr_Bounds (N : Node_Id; Dim : Pos) is
524 This_Range : constant Node_Id := Aggregate_Bounds (N);
525 -- The aggregate range node of this specific sub-aggregate.
527 This_Low : constant Node_Id := Low_Bound (Aggregate_Bounds (N));
528 This_High : constant Node_Id := High_Bound (Aggregate_Bounds (N));
529 -- The aggregate bounds of this specific sub-aggregate.
535 -- Collect the first N_Range for a given dimension that you find.
536 -- For a given dimension they must be all equal anyway.
538 if No (Aggr_Range (Dim)) then
539 Aggr_Low (Dim) := This_Low;
540 Aggr_High (Dim) := This_High;
541 Aggr_Range (Dim) := This_Range;
544 if Compile_Time_Known_Value (This_Low) then
545 if not Compile_Time_Known_Value (Aggr_Low (Dim)) then
546 Aggr_Low (Dim) := This_Low;
548 elsif Expr_Value (This_Low) /= Expr_Value (Aggr_Low (Dim)) then
549 Set_Raises_Constraint_Error (N);
550 Error_Msg_N ("sub-aggregate low bound mismatch?", N);
551 Error_Msg_N ("Constraint_Error will be raised at run-time?",
556 if Compile_Time_Known_Value (This_High) then
557 if not Compile_Time_Known_Value (Aggr_High (Dim)) then
558 Aggr_High (Dim) := This_High;
561 Expr_Value (This_High) /= Expr_Value (Aggr_High (Dim))
563 Set_Raises_Constraint_Error (N);
564 Error_Msg_N ("sub-aggregate high bound mismatch?", N);
565 Error_Msg_N ("Constraint_Error will be raised at run-time?",
571 if Dim < Aggr_Dimension then
573 -- Process positional components
575 if Present (Expressions (N)) then
576 Expr := First (Expressions (N));
577 while Present (Expr) loop
578 Collect_Aggr_Bounds (Expr, Dim + 1);
583 -- Process component associations
585 if Present (Component_Associations (N)) then
586 Is_Fully_Positional := False;
588 Assoc := First (Component_Associations (N));
589 while Present (Assoc) loop
590 Expr := Expression (Assoc);
591 Collect_Aggr_Bounds (Expr, Dim + 1);
596 end Collect_Aggr_Bounds;
598 -- Array_Aggr_Subtype variables
601 -- the final itype of the overall aggregate
603 Index_Constraints : constant List_Id := New_List;
604 -- The list of index constraints of the aggregate itype.
606 -- Start of processing for Array_Aggr_Subtype
609 -- Make sure that the list of index constraints is properly attached
610 -- to the tree, and then collect the aggregate bounds.
612 Set_Parent (Index_Constraints, N);
613 Collect_Aggr_Bounds (N, 1);
615 -- Build the list of constrained indices of our aggregate itype.
617 for J in 1 .. Aggr_Dimension loop
618 Create_Index : declare
619 Index_Base : constant Entity_Id :=
620 Base_Type (Etype (Aggr_Range (J)));
621 Index_Typ : Entity_Id;
624 -- Construct the Index subtype
626 Index_Typ := Create_Itype (Subtype_Kind (Ekind (Index_Base)), N);
628 Set_Etype (Index_Typ, Index_Base);
630 if Is_Character_Type (Index_Base) then
631 Set_Is_Character_Type (Index_Typ);
634 Set_Size_Info (Index_Typ, (Index_Base));
635 Set_RM_Size (Index_Typ, RM_Size (Index_Base));
636 Set_First_Rep_Item (Index_Typ, First_Rep_Item (Index_Base));
637 Set_Scalar_Range (Index_Typ, Aggr_Range (J));
639 if Is_Discrete_Or_Fixed_Point_Type (Index_Typ) then
640 Set_RM_Size (Index_Typ, UI_From_Int (Minimum_Size (Index_Typ)));
643 Set_Etype (Aggr_Range (J), Index_Typ);
645 Append (Aggr_Range (J), To => Index_Constraints);
649 -- Now build the Itype
651 Itype := Create_Itype (E_Array_Subtype, N);
653 Set_First_Rep_Item (Itype, First_Rep_Item (Typ));
654 Set_Convention (Itype, Convention (Typ));
655 Set_Depends_On_Private (Itype, Has_Private_Component (Typ));
656 Set_Etype (Itype, Base_Type (Typ));
657 Set_Has_Alignment_Clause (Itype, Has_Alignment_Clause (Typ));
658 Set_Is_Aliased (Itype, Is_Aliased (Typ));
659 Set_Depends_On_Private (Itype, Depends_On_Private (Typ));
661 Copy_Suppress_Status (Index_Check, Typ, Itype);
662 Copy_Suppress_Status (Length_Check, Typ, Itype);
664 Set_First_Index (Itype, First (Index_Constraints));
665 Set_Is_Constrained (Itype, True);
666 Set_Is_Internal (Itype, True);
667 Init_Size_Align (Itype);
669 -- A simple optimization: purely positional aggregates of static
670 -- components should be passed to gigi unexpanded whenever possible,
671 -- and regardless of the staticness of the bounds themselves. Subse-
672 -- quent checks in exp_aggr verify that type is not packed, etc.
674 Set_Size_Known_At_Compile_Time (Itype,
676 and then Comes_From_Source (N)
677 and then Size_Known_At_Compile_Time (Component_Type (Typ)));
679 -- We always need a freeze node for a packed array subtype, so that
680 -- we can build the Packed_Array_Type corresponding to the subtype.
681 -- If expansion is disabled, the packed array subtype is not built,
682 -- and we must not generate a freeze node for the type, or else it
683 -- will appear incomplete to gigi.
685 if Is_Packed (Itype) and then not In_Default_Expression
686 and then Expander_Active
688 Freeze_Itype (Itype, N);
692 end Array_Aggr_Subtype;
694 --------------------------------
695 -- Check_Misspelled_Component --
696 --------------------------------
698 procedure Check_Misspelled_Component
699 (Elements : Elist_Id;
702 Max_Suggestions : constant := 2;
704 Nr_Of_Suggestions : Natural := 0;
705 Suggestion_1 : Entity_Id := Empty;
706 Suggestion_2 : Entity_Id := Empty;
707 Component_Elmt : Elmt_Id;
710 -- All the components of List are matched against Component and
711 -- a count is maintained of possible misspellings. When at the
712 -- end of the analysis there are one or two (not more!) possible
713 -- misspellings, these misspellings will be suggested as
714 -- possible correction.
716 Get_Name_String (Chars (Component));
719 S : constant String (1 .. Name_Len) :=
720 Name_Buffer (1 .. Name_Len);
724 Component_Elmt := First_Elmt (Elements);
726 while Nr_Of_Suggestions <= Max_Suggestions
727 and then Present (Component_Elmt)
730 Get_Name_String (Chars (Node (Component_Elmt)));
732 if Is_Bad_Spelling_Of (Name_Buffer (1 .. Name_Len), S) then
733 Nr_Of_Suggestions := Nr_Of_Suggestions + 1;
735 case Nr_Of_Suggestions is
736 when 1 => Suggestion_1 := Node (Component_Elmt);
737 when 2 => Suggestion_2 := Node (Component_Elmt);
742 Next_Elmt (Component_Elmt);
745 -- Report at most two suggestions
747 if Nr_Of_Suggestions = 1 then
748 Error_Msg_NE ("\possible misspelling of&",
749 Component, Suggestion_1);
751 elsif Nr_Of_Suggestions = 2 then
752 Error_Msg_Node_2 := Suggestion_2;
753 Error_Msg_NE ("\possible misspelling of& or&",
754 Component, Suggestion_1);
757 end Check_Misspelled_Component;
759 ----------------------------------------
760 -- Check_Static_Discriminated_Subtype --
761 ----------------------------------------
763 procedure Check_Static_Discriminated_Subtype (T : Entity_Id; V : Node_Id) is
764 Disc : constant Entity_Id := First_Discriminant (T);
769 if Has_Record_Rep_Clause (T) then
772 elsif Present (Next_Discriminant (Disc)) then
775 elsif Nkind (V) /= N_Integer_Literal then
779 Comp := First_Component (T);
781 while Present (Comp) loop
783 if Is_Scalar_Type (Etype (Comp)) then
786 elsif Is_Private_Type (Etype (Comp))
787 and then Present (Full_View (Etype (Comp)))
788 and then Is_Scalar_Type (Full_View (Etype (Comp)))
792 elsif Is_Array_Type (Etype (Comp)) then
794 if Is_Bit_Packed_Array (Etype (Comp)) then
798 Ind := First_Index (Etype (Comp));
800 while Present (Ind) loop
802 if Nkind (Ind) /= N_Range
803 or else Nkind (Low_Bound (Ind)) /= N_Integer_Literal
804 or else Nkind (High_Bound (Ind)) /= N_Integer_Literal
816 Next_Component (Comp);
819 -- On exit, all components have statically known sizes.
821 Set_Size_Known_At_Compile_Time (T);
822 end Check_Static_Discriminated_Subtype;
824 --------------------------------
825 -- Make_String_Into_Aggregate --
826 --------------------------------
828 procedure Make_String_Into_Aggregate (N : Node_Id) is
829 Exprs : constant List_Id := New_List;
830 Loc : constant Source_Ptr := Sloc (N);
831 Str : constant String_Id := Strval (N);
832 Strlen : constant Nat := String_Length (Str);
840 for J in 1 .. Strlen loop
841 C := Get_String_Char (Str, J);
842 Set_Character_Literal_Name (C);
845 Make_Character_Literal (P,
847 Char_Literal_Value => UI_From_CC (C));
848 Set_Etype (C_Node, Any_Character);
849 Append_To (Exprs, C_Node);
852 -- something special for wide strings ???
855 New_N := Make_Aggregate (Loc, Expressions => Exprs);
856 Set_Analyzed (New_N);
857 Set_Etype (New_N, Any_Composite);
860 end Make_String_Into_Aggregate;
862 -----------------------
863 -- Resolve_Aggregate --
864 -----------------------
866 procedure Resolve_Aggregate (N : Node_Id; Typ : Entity_Id) is
867 Pkind : constant Node_Kind := Nkind (Parent (N));
869 Aggr_Subtyp : Entity_Id;
870 -- The actual aggregate subtype. This is not necessarily the same as Typ
871 -- which is the subtype of the context in which the aggregate was found.
874 -- Check for aggregates not allowed in configurable run-time mode.
875 -- We allow all cases of aggregates that do not come from source,
876 -- since these are all assumed to be small (e.g. bounds of a string
877 -- literal). We also allow aggregates of types we know to be small.
879 if not Support_Aggregates_On_Target
880 and then Comes_From_Source (N)
881 and then (not Known_Static_Esize (Typ) or else Esize (Typ) > 64)
883 Error_Msg_CRT ("aggregate", N);
886 if Is_Limited_Composite (Typ) then
887 Error_Msg_N ("aggregate type cannot have limited component", N);
888 Explain_Limited_Type (Typ, N);
890 -- Ada 2005 (AI-287): Limited aggregates allowed
892 elsif Is_Limited_Type (Typ)
893 and Ada_Version < Ada_05
895 Error_Msg_N ("aggregate type cannot be limited", N);
896 Explain_Limited_Type (Typ, N);
898 elsif Is_Class_Wide_Type (Typ) then
899 Error_Msg_N ("type of aggregate cannot be class-wide", N);
901 elsif Typ = Any_String
902 or else Typ = Any_Composite
904 Error_Msg_N ("no unique type for aggregate", N);
905 Set_Etype (N, Any_Composite);
907 elsif Is_Array_Type (Typ) and then Null_Record_Present (N) then
908 Error_Msg_N ("null record forbidden in array aggregate", N);
910 elsif Is_Record_Type (Typ) then
911 Resolve_Record_Aggregate (N, Typ);
913 elsif Is_Array_Type (Typ) then
915 -- First a special test, for the case of a positional aggregate
916 -- of characters which can be replaced by a string literal.
917 -- Do not perform this transformation if this was a string literal
918 -- to start with, whose components needed constraint checks, or if
919 -- the component type is non-static, because it will require those
920 -- checks and be transformed back into an aggregate.
922 if Number_Dimensions (Typ) = 1
924 (Root_Type (Component_Type (Typ)) = Standard_Character
926 Root_Type (Component_Type (Typ)) = Standard_Wide_Character
928 Root_Type (Component_Type (Typ)) = Standard_Wide_Wide_Character)
929 and then No (Component_Associations (N))
930 and then not Is_Limited_Composite (Typ)
931 and then not Is_Private_Composite (Typ)
932 and then not Is_Bit_Packed_Array (Typ)
933 and then Nkind (Original_Node (Parent (N))) /= N_String_Literal
934 and then Is_Static_Subtype (Component_Type (Typ))
940 Expr := First (Expressions (N));
941 while Present (Expr) loop
942 exit when Nkind (Expr) /= N_Character_Literal;
949 Expr := First (Expressions (N));
950 while Present (Expr) loop
951 Store_String_Char (UI_To_CC (Char_Literal_Value (Expr)));
956 Make_String_Literal (Sloc (N), End_String));
958 Analyze_And_Resolve (N, Typ);
964 -- Here if we have a real aggregate to deal with
966 Array_Aggregate : declare
967 Aggr_Resolved : Boolean;
969 Aggr_Typ : constant Entity_Id := Etype (Typ);
970 -- This is the unconstrained array type, which is the type
971 -- against which the aggregate is to be resolved. Typ itself
972 -- is the array type of the context which may not be the same
973 -- subtype as the subtype for the final aggregate.
976 -- In the following we determine whether an others choice is
977 -- allowed inside the array aggregate. The test checks the context
978 -- in which the array aggregate occurs. If the context does not
979 -- permit it, or the aggregate type is unconstrained, an others
980 -- choice is not allowed.
982 -- Note that there is no node for Explicit_Actual_Parameter.
983 -- To test for this context we therefore have to test for node
984 -- N_Parameter_Association which itself appears only if there is a
985 -- formal parameter. Consequently we also need to test for
986 -- N_Procedure_Call_Statement or N_Function_Call.
988 Set_Etype (N, Aggr_Typ); -- may be overridden later on
990 -- Ada 2005 (AI-231): Propagate the null_exclusion attribute to
991 -- the components of the array aggregate
993 if Ada_Version >= Ada_05 then
994 Set_Can_Never_Be_Null (Aggr_Typ, Can_Never_Be_Null (Typ));
997 if Is_Constrained (Typ) and then
998 (Pkind = N_Assignment_Statement or else
999 Pkind = N_Parameter_Association or else
1000 Pkind = N_Function_Call or else
1001 Pkind = N_Procedure_Call_Statement or else
1002 Pkind = N_Generic_Association or else
1003 Pkind = N_Formal_Object_Declaration or else
1004 Pkind = N_Return_Statement or else
1005 Pkind = N_Object_Declaration or else
1006 Pkind = N_Component_Declaration or else
1007 Pkind = N_Parameter_Specification or else
1008 Pkind = N_Qualified_Expression or else
1009 Pkind = N_Aggregate or else
1010 Pkind = N_Extension_Aggregate or else
1011 Pkind = N_Component_Association)
1014 Resolve_Array_Aggregate
1016 Index => First_Index (Aggr_Typ),
1017 Index_Constr => First_Index (Typ),
1018 Component_Typ => Component_Type (Typ),
1019 Others_Allowed => True);
1023 Resolve_Array_Aggregate
1025 Index => First_Index (Aggr_Typ),
1026 Index_Constr => First_Index (Aggr_Typ),
1027 Component_Typ => Component_Type (Typ),
1028 Others_Allowed => False);
1031 if not Aggr_Resolved then
1032 Aggr_Subtyp := Any_Composite;
1034 Aggr_Subtyp := Array_Aggr_Subtype (N, Typ);
1037 Set_Etype (N, Aggr_Subtyp);
1038 end Array_Aggregate;
1041 Error_Msg_N ("illegal context for aggregate", N);
1045 -- If we can determine statically that the evaluation of the
1046 -- aggregate raises Constraint_Error, then replace the
1047 -- aggregate with an N_Raise_Constraint_Error node, but set the
1048 -- Etype to the right aggregate subtype. Gigi needs this.
1050 if Raises_Constraint_Error (N) then
1051 Aggr_Subtyp := Etype (N);
1053 Make_Raise_Constraint_Error (Sloc (N),
1054 Reason => CE_Range_Check_Failed));
1055 Set_Raises_Constraint_Error (N);
1056 Set_Etype (N, Aggr_Subtyp);
1059 end Resolve_Aggregate;
1061 -----------------------------
1062 -- Resolve_Array_Aggregate --
1063 -----------------------------
1065 function Resolve_Array_Aggregate
1068 Index_Constr : Node_Id;
1069 Component_Typ : Entity_Id;
1070 Others_Allowed : Boolean)
1073 Loc : constant Source_Ptr := Sloc (N);
1075 Failure : constant Boolean := False;
1076 Success : constant Boolean := True;
1078 Index_Typ : constant Entity_Id := Etype (Index);
1079 Index_Typ_Low : constant Node_Id := Type_Low_Bound (Index_Typ);
1080 Index_Typ_High : constant Node_Id := Type_High_Bound (Index_Typ);
1081 -- The type of the index corresponding to the array sub-aggregate
1082 -- along with its low and upper bounds
1084 Index_Base : constant Entity_Id := Base_Type (Index_Typ);
1085 Index_Base_Low : constant Node_Id := Type_Low_Bound (Index_Base);
1086 Index_Base_High : constant Node_Id := Type_High_Bound (Index_Base);
1087 -- ditto for the base type
1089 function Add (Val : Uint; To : Node_Id) return Node_Id;
1090 -- Creates a new expression node where Val is added to expression To.
1091 -- Tries to constant fold whenever possible. To must be an already
1092 -- analyzed expression.
1094 procedure Check_Bound (BH : Node_Id; AH : in out Node_Id);
1095 -- Checks that AH (the upper bound of an array aggregate) is <= BH
1096 -- (the upper bound of the index base type). If the check fails a
1097 -- warning is emitted, the Raises_Constraint_Error Flag of N is set,
1098 -- and AH is replaced with a duplicate of BH.
1100 procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id);
1101 -- Checks that range AL .. AH is compatible with range L .. H. Emits a
1102 -- warning if not and sets the Raises_Constraint_Error Flag in N.
1104 procedure Check_Length (L, H : Node_Id; Len : Uint);
1105 -- Checks that range L .. H contains at least Len elements. Emits a
1106 -- warning if not and sets the Raises_Constraint_Error Flag in N.
1108 function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean;
1109 -- Returns True if range L .. H is dynamic or null.
1111 procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean);
1112 -- Given expression node From, this routine sets OK to False if it
1113 -- cannot statically evaluate From. Otherwise it stores this static
1114 -- value into Value.
1116 function Resolve_Aggr_Expr
1118 Single_Elmt : Boolean)
1120 -- Resolves aggregate expression Expr. Returs False if resolution
1121 -- fails. If Single_Elmt is set to False, the expression Expr may be
1122 -- used to initialize several array aggregate elements (this can
1123 -- happen for discrete choices such as "L .. H => Expr" or the others
1124 -- choice). In this event we do not resolve Expr unless expansion is
1125 -- disabled. To know why, see the DELAYED COMPONENT RESOLUTION
1132 function Add (Val : Uint; To : Node_Id) return Node_Id is
1138 if Raises_Constraint_Error (To) then
1142 -- First test if we can do constant folding
1144 if Compile_Time_Known_Value (To)
1145 or else Nkind (To) = N_Integer_Literal
1147 Expr_Pos := Make_Integer_Literal (Loc, Expr_Value (To) + Val);
1148 Set_Is_Static_Expression (Expr_Pos);
1149 Set_Etype (Expr_Pos, Etype (To));
1150 Set_Analyzed (Expr_Pos, Analyzed (To));
1152 if not Is_Enumeration_Type (Index_Typ) then
1155 -- If we are dealing with enumeration return
1156 -- Index_Typ'Val (Expr_Pos)
1160 Make_Attribute_Reference
1162 Prefix => New_Reference_To (Index_Typ, Loc),
1163 Attribute_Name => Name_Val,
1164 Expressions => New_List (Expr_Pos));
1170 -- If we are here no constant folding possible
1172 if not Is_Enumeration_Type (Index_Base) then
1175 Left_Opnd => Duplicate_Subexpr (To),
1176 Right_Opnd => Make_Integer_Literal (Loc, Val));
1178 -- If we are dealing with enumeration return
1179 -- Index_Typ'Val (Index_Typ'Pos (To) + Val)
1183 Make_Attribute_Reference
1185 Prefix => New_Reference_To (Index_Typ, Loc),
1186 Attribute_Name => Name_Pos,
1187 Expressions => New_List (Duplicate_Subexpr (To)));
1191 Left_Opnd => To_Pos,
1192 Right_Opnd => Make_Integer_Literal (Loc, Val));
1195 Make_Attribute_Reference
1197 Prefix => New_Reference_To (Index_Typ, Loc),
1198 Attribute_Name => Name_Val,
1199 Expressions => New_List (Expr_Pos));
1209 procedure Check_Bound (BH : Node_Id; AH : in out Node_Id) is
1217 Get (Value => Val_BH, From => BH, OK => OK_BH);
1218 Get (Value => Val_AH, From => AH, OK => OK_AH);
1220 if OK_BH and then OK_AH and then Val_BH < Val_AH then
1221 Set_Raises_Constraint_Error (N);
1222 Error_Msg_N ("upper bound out of range?", AH);
1223 Error_Msg_N ("Constraint_Error will be raised at run-time?", AH);
1225 -- You need to set AH to BH or else in the case of enumerations
1226 -- indices we will not be able to resolve the aggregate bounds.
1228 AH := Duplicate_Subexpr (BH);
1236 procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id) is
1248 if Raises_Constraint_Error (N)
1249 or else Dynamic_Or_Null_Range (AL, AH)
1254 Get (Value => Val_L, From => L, OK => OK_L);
1255 Get (Value => Val_H, From => H, OK => OK_H);
1257 Get (Value => Val_AL, From => AL, OK => OK_AL);
1258 Get (Value => Val_AH, From => AH, OK => OK_AH);
1260 if OK_L and then Val_L > Val_AL then
1261 Set_Raises_Constraint_Error (N);
1262 Error_Msg_N ("lower bound of aggregate out of range?", N);
1263 Error_Msg_N ("\Constraint_Error will be raised at run-time?", N);
1266 if OK_H and then Val_H < Val_AH then
1267 Set_Raises_Constraint_Error (N);
1268 Error_Msg_N ("upper bound of aggregate out of range?", N);
1269 Error_Msg_N ("\Constraint_Error will be raised at run-time?", N);
1277 procedure Check_Length (L, H : Node_Id; Len : Uint) is
1287 if Raises_Constraint_Error (N) then
1291 Get (Value => Val_L, From => L, OK => OK_L);
1292 Get (Value => Val_H, From => H, OK => OK_H);
1294 if not OK_L or else not OK_H then
1298 -- If null range length is zero
1300 if Val_L > Val_H then
1301 Range_Len := Uint_0;
1303 Range_Len := Val_H - Val_L + 1;
1306 if Range_Len < Len then
1307 Set_Raises_Constraint_Error (N);
1308 Error_Msg_N ("too many elements?", N);
1309 Error_Msg_N ("Constraint_Error will be raised at run-time?", N);
1313 ---------------------------
1314 -- Dynamic_Or_Null_Range --
1315 ---------------------------
1317 function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean is
1325 Get (Value => Val_L, From => L, OK => OK_L);
1326 Get (Value => Val_H, From => H, OK => OK_H);
1328 return not OK_L or else not OK_H
1329 or else not Is_OK_Static_Expression (L)
1330 or else not Is_OK_Static_Expression (H)
1331 or else Val_L > Val_H;
1332 end Dynamic_Or_Null_Range;
1338 procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean) is
1342 if Compile_Time_Known_Value (From) then
1343 Value := Expr_Value (From);
1345 -- If expression From is something like Some_Type'Val (10) then
1348 elsif Nkind (From) = N_Attribute_Reference
1349 and then Attribute_Name (From) = Name_Val
1350 and then Compile_Time_Known_Value (First (Expressions (From)))
1352 Value := Expr_Value (First (Expressions (From)));
1360 -----------------------
1361 -- Resolve_Aggr_Expr --
1362 -----------------------
1364 function Resolve_Aggr_Expr
1366 Single_Elmt : Boolean)
1369 Nxt_Ind : constant Node_Id := Next_Index (Index);
1370 Nxt_Ind_Constr : constant Node_Id := Next_Index (Index_Constr);
1371 -- Index is the current index corresponding to the expresion.
1373 Resolution_OK : Boolean := True;
1374 -- Set to False if resolution of the expression failed.
1377 -- If the array type against which we are resolving the aggregate
1378 -- has several dimensions, the expressions nested inside the
1379 -- aggregate must be further aggregates (or strings).
1381 if Present (Nxt_Ind) then
1382 if Nkind (Expr) /= N_Aggregate then
1384 -- A string literal can appear where a one-dimensional array
1385 -- of characters is expected. If the literal looks like an
1386 -- operator, it is still an operator symbol, which will be
1387 -- transformed into a string when analyzed.
1389 if Is_Character_Type (Component_Typ)
1390 and then No (Next_Index (Nxt_Ind))
1391 and then (Nkind (Expr) = N_String_Literal
1392 or else Nkind (Expr) = N_Operator_Symbol)
1394 -- A string literal used in a multidimensional array
1395 -- aggregate in place of the final one-dimensional
1396 -- aggregate must not be enclosed in parentheses.
1398 if Paren_Count (Expr) /= 0 then
1399 Error_Msg_N ("no parenthesis allowed here", Expr);
1402 Make_String_Into_Aggregate (Expr);
1405 Error_Msg_N ("nested array aggregate expected", Expr);
1410 -- Ada 2005 (AI-231): Propagate the type to the nested aggregate.
1411 -- Required to check the null-exclusion attribute (if present).
1412 -- This value may be overridden later on.
1414 Set_Etype (Expr, Etype (N));
1416 Resolution_OK := Resolve_Array_Aggregate
1417 (Expr, Nxt_Ind, Nxt_Ind_Constr, Component_Typ, Others_Allowed);
1419 -- Do not resolve the expressions of discrete or others choices
1420 -- unless the expression covers a single component, or the expander
1424 or else not Expander_Active
1425 or else In_Default_Expression
1427 Analyze_And_Resolve (Expr, Component_Typ);
1428 Check_Non_Static_Context (Expr);
1429 Aggregate_Constraint_Checks (Expr, Component_Typ);
1430 Check_Unset_Reference (Expr);
1433 if Raises_Constraint_Error (Expr)
1434 and then Nkind (Parent (Expr)) /= N_Component_Association
1436 Set_Raises_Constraint_Error (N);
1439 return Resolution_OK;
1440 end Resolve_Aggr_Expr;
1442 -- Variables local to Resolve_Array_Aggregate
1448 Who_Cares : Node_Id;
1450 Aggr_Low : Node_Id := Empty;
1451 Aggr_High : Node_Id := Empty;
1452 -- The actual low and high bounds of this sub-aggegate
1454 Choices_Low : Node_Id := Empty;
1455 Choices_High : Node_Id := Empty;
1456 -- The lowest and highest discrete choices values for a named aggregate
1458 Nb_Elements : Uint := Uint_0;
1459 -- The number of elements in a positional aggegate
1461 Others_Present : Boolean := False;
1463 Nb_Choices : Nat := 0;
1464 -- Contains the overall number of named choices in this sub-aggregate
1466 Nb_Discrete_Choices : Nat := 0;
1467 -- The overall number of discrete choices (not counting others choice)
1469 Case_Table_Size : Nat;
1470 -- Contains the size of the case table needed to sort aggregate choices
1472 -- Start of processing for Resolve_Array_Aggregate
1475 -- STEP 1: make sure the aggregate is correctly formatted
1477 if Present (Component_Associations (N)) then
1478 Assoc := First (Component_Associations (N));
1479 while Present (Assoc) loop
1480 Choice := First (Choices (Assoc));
1481 while Present (Choice) loop
1482 if Nkind (Choice) = N_Others_Choice then
1483 Others_Present := True;
1485 if Choice /= First (Choices (Assoc))
1486 or else Present (Next (Choice))
1489 ("OTHERS must appear alone in a choice list", Choice);
1493 if Present (Next (Assoc)) then
1495 ("OTHERS must appear last in an aggregate", Choice);
1499 if Ada_Version = Ada_83
1500 and then Assoc /= First (Component_Associations (N))
1501 and then (Nkind (Parent (N)) = N_Assignment_Statement
1503 Nkind (Parent (N)) = N_Object_Declaration)
1506 ("(Ada 83) illegal context for OTHERS choice", N);
1510 Nb_Choices := Nb_Choices + 1;
1518 -- At this point we know that the others choice, if present, is by
1519 -- itself and appears last in the aggregate. Check if we have mixed
1520 -- positional and discrete associations (other than the others choice).
1522 if Present (Expressions (N))
1523 and then (Nb_Choices > 1
1524 or else (Nb_Choices = 1 and then not Others_Present))
1527 ("named association cannot follow positional association",
1528 First (Choices (First (Component_Associations (N)))));
1532 -- Test for the validity of an others choice if present
1534 if Others_Present and then not Others_Allowed then
1536 ("OTHERS choice not allowed here",
1537 First (Choices (First (Component_Associations (N)))));
1541 -- Protect against cascaded errors
1543 if Etype (Index_Typ) = Any_Type then
1547 -- STEP 2: Process named components
1549 if No (Expressions (N)) then
1551 if Others_Present then
1552 Case_Table_Size := Nb_Choices - 1;
1554 Case_Table_Size := Nb_Choices;
1560 -- Denote the lowest and highest values in an aggregate choice
1564 -- High end of one range and Low end of the next. Should be
1565 -- contiguous if there is no hole in the list of values.
1567 Missing_Values : Boolean;
1568 -- Set True if missing index values
1570 S_Low : Node_Id := Empty;
1571 S_High : Node_Id := Empty;
1572 -- if a choice in an aggregate is a subtype indication these
1573 -- denote the lowest and highest values of the subtype
1575 Table : Case_Table_Type (1 .. Case_Table_Size);
1576 -- Used to sort all the different choice values
1578 Single_Choice : Boolean;
1579 -- Set to true every time there is a single discrete choice in a
1580 -- discrete association
1582 Prev_Nb_Discrete_Choices : Nat;
1583 -- Used to keep track of the number of discrete choices
1584 -- in the current association.
1587 -- STEP 2 (A): Check discrete choices validity.
1589 Assoc := First (Component_Associations (N));
1590 while Present (Assoc) loop
1592 Prev_Nb_Discrete_Choices := Nb_Discrete_Choices;
1593 Choice := First (Choices (Assoc));
1597 if Nkind (Choice) = N_Others_Choice then
1598 Single_Choice := False;
1601 -- Test for subtype mark without constraint
1603 elsif Is_Entity_Name (Choice) and then
1604 Is_Type (Entity (Choice))
1606 if Base_Type (Entity (Choice)) /= Index_Base then
1608 ("invalid subtype mark in aggregate choice",
1613 elsif Nkind (Choice) = N_Subtype_Indication then
1614 Resolve_Discrete_Subtype_Indication (Choice, Index_Base);
1616 -- Does the subtype indication evaluation raise CE ?
1618 Get_Index_Bounds (Subtype_Mark (Choice), S_Low, S_High);
1619 Get_Index_Bounds (Choice, Low, High);
1620 Check_Bounds (S_Low, S_High, Low, High);
1622 else -- Choice is a range or an expression
1623 Resolve (Choice, Index_Base);
1624 Check_Unset_Reference (Choice);
1625 Check_Non_Static_Context (Choice);
1627 -- Do not range check a choice. This check is redundant
1628 -- since this test is already performed when we check
1629 -- that the bounds of the array aggregate are within
1632 Set_Do_Range_Check (Choice, False);
1635 -- If we could not resolve the discrete choice stop here
1637 if Etype (Choice) = Any_Type then
1640 -- If the discrete choice raises CE get its original bounds.
1642 elsif Nkind (Choice) = N_Raise_Constraint_Error then
1643 Set_Raises_Constraint_Error (N);
1644 Get_Index_Bounds (Original_Node (Choice), Low, High);
1646 -- Otherwise get its bounds as usual
1649 Get_Index_Bounds (Choice, Low, High);
1652 if (Dynamic_Or_Null_Range (Low, High)
1653 or else (Nkind (Choice) = N_Subtype_Indication
1655 Dynamic_Or_Null_Range (S_Low, S_High)))
1656 and then Nb_Choices /= 1
1659 ("dynamic or empty choice in aggregate " &
1660 "must be the only choice", Choice);
1664 Nb_Discrete_Choices := Nb_Discrete_Choices + 1;
1665 Table (Nb_Discrete_Choices).Choice_Lo := Low;
1666 Table (Nb_Discrete_Choices).Choice_Hi := High;
1671 -- Check if we have a single discrete choice and whether
1672 -- this discrete choice specifies a single value.
1675 (Nb_Discrete_Choices = Prev_Nb_Discrete_Choices + 1)
1676 and then (Low = High);
1682 -- Ada 2005 (AI-231)
1684 if Ada_Version >= Ada_05 then
1685 Check_Can_Never_Be_Null (Etype (N), Expression (Assoc));
1688 -- Ada 2005 (AI-287): In case of default initialized component
1689 -- we delay the resolution to the expansion phase
1691 if Box_Present (Assoc) then
1693 -- Ada 2005 (AI-287): In case of default initialization
1694 -- of a component the expander will generate calls to
1695 -- the corresponding initialization subprogram.
1699 elsif not Resolve_Aggr_Expr (Expression (Assoc),
1700 Single_Elmt => Single_Choice)
1708 -- If aggregate contains more than one choice then these must be
1709 -- static. Sort them and check that they are contiguous
1711 if Nb_Discrete_Choices > 1 then
1712 Sort_Case_Table (Table);
1713 Missing_Values := False;
1715 Outer : for J in 1 .. Nb_Discrete_Choices - 1 loop
1716 if Expr_Value (Table (J).Choice_Hi) >=
1717 Expr_Value (Table (J + 1).Choice_Lo)
1720 ("duplicate choice values in array aggregate",
1721 Table (J).Choice_Hi);
1724 elsif not Others_Present then
1726 Hi_Val := Expr_Value (Table (J).Choice_Hi);
1727 Lo_Val := Expr_Value (Table (J + 1).Choice_Lo);
1729 -- If missing values, output error messages
1731 if Lo_Val - Hi_Val > 1 then
1733 -- Header message if not first missing value
1735 if not Missing_Values then
1737 ("missing index value(s) in array aggregate", N);
1738 Missing_Values := True;
1741 -- Output values of missing indexes
1743 Lo_Val := Lo_Val - 1;
1744 Hi_Val := Hi_Val + 1;
1746 -- Enumeration type case
1748 if Is_Enumeration_Type (Index_Typ) then
1751 (Get_Enum_Lit_From_Pos
1752 (Index_Typ, Hi_Val, Loc));
1754 if Lo_Val = Hi_Val then
1755 Error_Msg_N ("\ %", N);
1759 (Get_Enum_Lit_From_Pos
1760 (Index_Typ, Lo_Val, Loc));
1761 Error_Msg_N ("\ % .. %", N);
1764 -- Integer types case
1767 Error_Msg_Uint_1 := Hi_Val;
1769 if Lo_Val = Hi_Val then
1770 Error_Msg_N ("\ ^", N);
1772 Error_Msg_Uint_2 := Lo_Val;
1773 Error_Msg_N ("\ ^ .. ^", N);
1780 if Missing_Values then
1781 Set_Etype (N, Any_Composite);
1786 -- STEP 2 (B): Compute aggregate bounds and min/max choices values
1788 if Nb_Discrete_Choices > 0 then
1789 Choices_Low := Table (1).Choice_Lo;
1790 Choices_High := Table (Nb_Discrete_Choices).Choice_Hi;
1793 if Others_Present then
1794 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
1797 Aggr_Low := Choices_Low;
1798 Aggr_High := Choices_High;
1802 -- STEP 3: Process positional components
1805 -- STEP 3 (A): Process positional elements
1807 Expr := First (Expressions (N));
1808 Nb_Elements := Uint_0;
1809 while Present (Expr) loop
1810 Nb_Elements := Nb_Elements + 1;
1812 -- Ada 2005 (AI-231)
1814 if Ada_Version >= Ada_05 then
1815 Check_Can_Never_Be_Null (Etype (N), Expr);
1818 if not Resolve_Aggr_Expr (Expr, Single_Elmt => True) then
1825 if Others_Present then
1826 Assoc := Last (Component_Associations (N));
1828 -- Ada 2005 (AI-231)
1830 if Ada_Version >= Ada_05 then
1831 Check_Can_Never_Be_Null
1832 (Etype (N), Expression (Assoc));
1835 -- Ada 2005 (AI-287): In case of default initialized component
1836 -- we delay the resolution to the expansion phase.
1838 if Box_Present (Assoc) then
1840 -- Ada 2005 (AI-287): In case of default initialization
1841 -- of a component the expander will generate calls to
1842 -- the corresponding initialization subprogram.
1846 elsif not Resolve_Aggr_Expr (Expression (Assoc),
1847 Single_Elmt => False)
1853 -- STEP 3 (B): Compute the aggregate bounds
1855 if Others_Present then
1856 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
1859 if Others_Allowed then
1860 Get_Index_Bounds (Index_Constr, Aggr_Low, Who_Cares);
1862 Aggr_Low := Index_Typ_Low;
1865 Aggr_High := Add (Nb_Elements - 1, To => Aggr_Low);
1866 Check_Bound (Index_Base_High, Aggr_High);
1870 -- STEP 4: Perform static aggregate checks and save the bounds
1874 Check_Bounds (Index_Typ_Low, Index_Typ_High, Aggr_Low, Aggr_High);
1875 Check_Bounds (Index_Base_Low, Index_Base_High, Aggr_Low, Aggr_High);
1879 if Others_Present and then Nb_Discrete_Choices > 0 then
1880 Check_Bounds (Aggr_Low, Aggr_High, Choices_Low, Choices_High);
1881 Check_Bounds (Index_Typ_Low, Index_Typ_High,
1882 Choices_Low, Choices_High);
1883 Check_Bounds (Index_Base_Low, Index_Base_High,
1884 Choices_Low, Choices_High);
1888 elsif Others_Present and then Nb_Elements > 0 then
1889 Check_Length (Aggr_Low, Aggr_High, Nb_Elements);
1890 Check_Length (Index_Typ_Low, Index_Typ_High, Nb_Elements);
1891 Check_Length (Index_Base_Low, Index_Base_High, Nb_Elements);
1895 if Raises_Constraint_Error (Aggr_Low)
1896 or else Raises_Constraint_Error (Aggr_High)
1898 Set_Raises_Constraint_Error (N);
1901 Aggr_Low := Duplicate_Subexpr (Aggr_Low);
1903 -- Do not duplicate Aggr_High if Aggr_High = Aggr_Low + Nb_Elements
1904 -- since the addition node returned by Add is not yet analyzed. Attach
1905 -- to tree and analyze first. Reset analyzed flag to insure it will get
1906 -- analyzed when it is a literal bound whose type must be properly
1909 if Others_Present or else Nb_Discrete_Choices > 0 then
1910 Aggr_High := Duplicate_Subexpr (Aggr_High);
1912 if Etype (Aggr_High) = Universal_Integer then
1913 Set_Analyzed (Aggr_High, False);
1917 Set_Aggregate_Bounds
1918 (N, Make_Range (Loc, Low_Bound => Aggr_Low, High_Bound => Aggr_High));
1920 -- The bounds may contain expressions that must be inserted upwards.
1921 -- Attach them fully to the tree. After analysis, remove side effects
1922 -- from upper bound, if still needed.
1924 Set_Parent (Aggregate_Bounds (N), N);
1925 Analyze_And_Resolve (Aggregate_Bounds (N), Index_Typ);
1926 Check_Unset_Reference (Aggregate_Bounds (N));
1928 if not Others_Present and then Nb_Discrete_Choices = 0 then
1929 Set_High_Bound (Aggregate_Bounds (N),
1930 Duplicate_Subexpr (High_Bound (Aggregate_Bounds (N))));
1934 end Resolve_Array_Aggregate;
1936 ---------------------------------
1937 -- Resolve_Extension_Aggregate --
1938 ---------------------------------
1940 -- There are two cases to consider:
1942 -- a) If the ancestor part is a type mark, the components needed are
1943 -- the difference between the components of the expected type and the
1944 -- components of the given type mark.
1946 -- b) If the ancestor part is an expression, it must be unambiguous,
1947 -- and once we have its type we can also compute the needed components
1948 -- as in the previous case. In both cases, if the ancestor type is not
1949 -- the immediate ancestor, we have to build this ancestor recursively.
1951 -- In both cases discriminants of the ancestor type do not play a
1952 -- role in the resolution of the needed components, because inherited
1953 -- discriminants cannot be used in a type extension. As a result we can
1954 -- compute independently the list of components of the ancestor type and
1955 -- of the expected type.
1957 procedure Resolve_Extension_Aggregate (N : Node_Id; Typ : Entity_Id) is
1958 A : constant Node_Id := Ancestor_Part (N);
1963 function Valid_Ancestor_Type return Boolean;
1964 -- Verify that the type of the ancestor part is a non-private ancestor
1965 -- of the expected type.
1967 -------------------------
1968 -- Valid_Ancestor_Type --
1969 -------------------------
1971 function Valid_Ancestor_Type return Boolean is
1972 Imm_Type : Entity_Id;
1975 Imm_Type := Base_Type (Typ);
1976 while Is_Derived_Type (Imm_Type)
1977 and then Etype (Imm_Type) /= Base_Type (A_Type)
1979 Imm_Type := Etype (Base_Type (Imm_Type));
1982 if Etype (Imm_Type) /= Base_Type (A_Type) then
1983 Error_Msg_NE ("expect ancestor type of &", A, Typ);
1988 end Valid_Ancestor_Type;
1990 -- Start of processing for Resolve_Extension_Aggregate
1995 if not Is_Tagged_Type (Typ) then
1996 Error_Msg_N ("type of extension aggregate must be tagged", N);
1999 elsif Is_Limited_Type (Typ) then
2001 -- Ada 2005 (AI-287): Limited aggregates are allowed
2003 if Ada_Version < Ada_05 then
2004 Error_Msg_N ("aggregate type cannot be limited", N);
2005 Explain_Limited_Type (Typ, N);
2009 elsif Is_Class_Wide_Type (Typ) then
2010 Error_Msg_N ("aggregate cannot be of a class-wide type", N);
2014 if Is_Entity_Name (A)
2015 and then Is_Type (Entity (A))
2017 A_Type := Get_Full_View (Entity (A));
2019 if Valid_Ancestor_Type then
2020 Set_Entity (A, A_Type);
2021 Set_Etype (A, A_Type);
2023 Validate_Ancestor_Part (N);
2024 Resolve_Record_Aggregate (N, Typ);
2027 elsif Nkind (A) /= N_Aggregate then
2028 if Is_Overloaded (A) then
2030 Get_First_Interp (A, I, It);
2032 while Present (It.Typ) loop
2034 if Is_Tagged_Type (It.Typ)
2035 and then not Is_Limited_Type (It.Typ)
2037 if A_Type /= Any_Type then
2038 Error_Msg_N ("cannot resolve expression", A);
2045 Get_Next_Interp (I, It);
2048 if A_Type = Any_Type then
2050 ("ancestor part must be non-limited tagged type", A);
2055 A_Type := Etype (A);
2058 if Valid_Ancestor_Type then
2059 Resolve (A, A_Type);
2060 Check_Unset_Reference (A);
2061 Check_Non_Static_Context (A);
2063 if Is_Class_Wide_Type (Etype (A))
2064 and then Nkind (Original_Node (A)) = N_Function_Call
2066 -- If the ancestor part is a dispatching call, it appears
2067 -- statically to be a legal ancestor, but it yields any
2068 -- member of the class, and it is not possible to determine
2069 -- whether it is an ancestor of the extension aggregate (much
2070 -- less which ancestor). It is not possible to determine the
2071 -- required components of the extension part.
2073 -- This check implements AI-306, which in fact was motivated
2074 -- by an ACT query to the ARG after this test was added.
2076 Error_Msg_N ("ancestor part must be statically tagged", A);
2078 Resolve_Record_Aggregate (N, Typ);
2083 Error_Msg_N (" No unique type for this aggregate", A);
2085 end Resolve_Extension_Aggregate;
2087 ------------------------------
2088 -- Resolve_Record_Aggregate --
2089 ------------------------------
2091 procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id) is
2092 New_Assoc_List : constant List_Id := New_List;
2093 New_Assoc : Node_Id;
2094 -- New_Assoc_List is the newly built list of N_Component_Association
2095 -- nodes. New_Assoc is one such N_Component_Association node in it.
2096 -- Please note that while Assoc and New_Assoc contain the same
2097 -- kind of nodes, they are used to iterate over two different
2098 -- N_Component_Association lists.
2100 Others_Etype : Entity_Id := Empty;
2101 -- This variable is used to save the Etype of the last record component
2102 -- that takes its value from the others choice. Its purpose is:
2104 -- (a) make sure the others choice is useful
2106 -- (b) make sure the type of all the components whose value is
2107 -- subsumed by the others choice are the same.
2109 -- This variable is updated as a side effect of function Get_Value
2111 Mbox_Present : Boolean := False;
2112 Others_Mbox : Boolean := False;
2113 -- Ada 2005 (AI-287): Variables used in case of default initialization
2114 -- to provide a functionality similar to Others_Etype. Mbox_Present
2115 -- indicates that the component takes its default initialization;
2116 -- Others_Mbox indicates that at least one component takes its default
2117 -- initialization. Similar to Others_Etype, they are also updated as a
2118 -- side effect of function Get_Value.
2120 procedure Add_Association
2121 (Component : Entity_Id;
2123 Box_Present : Boolean := False);
2124 -- Builds a new N_Component_Association node which associates
2125 -- Component to expression Expr and adds it to the new association
2126 -- list New_Assoc_List being built.
2128 function Discr_Present (Discr : Entity_Id) return Boolean;
2129 -- If aggregate N is a regular aggregate this routine will return True.
2130 -- Otherwise, if N is an extension aggregate, Discr is a discriminant
2131 -- whose value may already have been specified by N's ancestor part,
2132 -- this routine checks whether this is indeed the case and if so
2133 -- returns False, signaling that no value for Discr should appear in the
2134 -- N's aggregate part. Also, in this case, the routine appends to
2135 -- New_Assoc_List Discr the discriminant value specified in the ancestor
2141 Consider_Others_Choice : Boolean := False)
2143 -- Given a record component stored in parameter Compon, the
2144 -- following function returns its value as it appears in the list
2145 -- From, which is a list of N_Component_Association nodes. If no
2146 -- component association has a choice for the searched component,
2147 -- the value provided by the others choice is returned, if there
2148 -- is one and Consider_Others_Choice is set to true. Otherwise
2149 -- Empty is returned. If there is more than one component association
2150 -- giving a value for the searched record component, an error message
2151 -- is emitted and the first found value is returned.
2153 -- If Consider_Others_Choice is set and the returned expression comes
2154 -- from the others choice, then Others_Etype is set as a side effect.
2155 -- An error message is emitted if the components taking their value
2156 -- from the others choice do not have same type.
2158 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Node_Id);
2159 -- Analyzes and resolves expression Expr against the Etype of the
2160 -- Component. This routine also applies all appropriate checks to Expr.
2161 -- It finally saves a Expr in the newly created association list that
2162 -- will be attached to the final record aggregate. Note that if the
2163 -- Parent pointer of Expr is not set then Expr was produced with a
2164 -- New_Copy_Tree or some such.
2166 ---------------------
2167 -- Add_Association --
2168 ---------------------
2170 procedure Add_Association
2171 (Component : Entity_Id;
2173 Box_Present : Boolean := False)
2175 Choice_List : constant List_Id := New_List;
2176 New_Assoc : Node_Id;
2179 Append (New_Occurrence_Of (Component, Sloc (Expr)), Choice_List);
2181 Make_Component_Association (Sloc (Expr),
2182 Choices => Choice_List,
2184 Box_Present => Box_Present);
2185 Append (New_Assoc, New_Assoc_List);
2186 end Add_Association;
2192 function Discr_Present (Discr : Entity_Id) return Boolean is
2193 Regular_Aggr : constant Boolean := Nkind (N) /= N_Extension_Aggregate;
2198 Discr_Expr : Node_Id;
2200 Ancestor_Typ : Entity_Id;
2201 Orig_Discr : Entity_Id;
2203 D_Val : Elmt_Id := No_Elmt; -- stop junk warning
2205 Ancestor_Is_Subtyp : Boolean;
2208 if Regular_Aggr then
2212 Ancestor := Ancestor_Part (N);
2213 Ancestor_Typ := Etype (Ancestor);
2214 Loc := Sloc (Ancestor);
2216 Ancestor_Is_Subtyp :=
2217 Is_Entity_Name (Ancestor) and then Is_Type (Entity (Ancestor));
2219 -- If the ancestor part has no discriminants clearly N's aggregate
2220 -- part must provide a value for Discr.
2222 if not Has_Discriminants (Ancestor_Typ) then
2225 -- If the ancestor part is an unconstrained subtype mark then the
2226 -- Discr must be present in N's aggregate part.
2228 elsif Ancestor_Is_Subtyp
2229 and then not Is_Constrained (Entity (Ancestor))
2234 -- Now look to see if Discr was specified in the ancestor part.
2236 Orig_Discr := Original_Record_Component (Discr);
2237 D := First_Discriminant (Ancestor_Typ);
2239 if Ancestor_Is_Subtyp then
2240 D_Val := First_Elmt (Discriminant_Constraint (Entity (Ancestor)));
2243 while Present (D) loop
2244 -- If Ancestor has already specified Disc value than
2245 -- insert its value in the final aggregate.
2247 if Original_Record_Component (D) = Orig_Discr then
2248 if Ancestor_Is_Subtyp then
2249 Discr_Expr := New_Copy_Tree (Node (D_Val));
2252 Make_Selected_Component (Loc,
2253 Prefix => Duplicate_Subexpr (Ancestor),
2254 Selector_Name => New_Occurrence_Of (Discr, Loc));
2257 Resolve_Aggr_Expr (Discr_Expr, Discr);
2261 Next_Discriminant (D);
2263 if Ancestor_Is_Subtyp then
2278 Consider_Others_Choice : Boolean := False)
2282 Expr : Node_Id := Empty;
2283 Selector_Name : Node_Id;
2285 procedure Check_Non_Limited_Type;
2286 -- Relax check to allow the default initialization of limited types.
2289 -- C : Lim := (..., others => <>);
2292 ----------------------------
2293 -- Check_Non_Limited_Type --
2294 ----------------------------
2296 procedure Check_Non_Limited_Type is
2298 if Is_Limited_Type (Etype (Compon))
2299 and then Comes_From_Source (Compon)
2300 and then not In_Instance_Body
2302 -- Ada 2005 (AI-287): Limited aggregates are allowed
2304 if Ada_Version >= Ada_05
2305 and then Present (Expression (Assoc))
2306 and then Nkind (Expression (Assoc)) = N_Aggregate
2311 ("initialization not allowed for limited types", N);
2312 Explain_Limited_Type (Etype (Compon), Compon);
2316 end Check_Non_Limited_Type;
2318 -- Start of processing for Get_Value
2321 Mbox_Present := False;
2323 if Present (From) then
2324 Assoc := First (From);
2329 while Present (Assoc) loop
2330 Selector_Name := First (Choices (Assoc));
2331 while Present (Selector_Name) loop
2332 if Nkind (Selector_Name) = N_Others_Choice then
2333 if Consider_Others_Choice and then No (Expr) then
2335 -- We need to duplicate the expression for each
2336 -- successive component covered by the others choice.
2337 -- This is redundant if the others_choice covers only
2338 -- one component (small optimization possible???), but
2339 -- indispensable otherwise, because each one must be
2340 -- expanded individually to preserve side-effects.
2342 -- Ada 2005 (AI-287): In case of default initialization
2343 -- of components, we duplicate the corresponding default
2344 -- expression (from the record type declaration).
2346 if Box_Present (Assoc) then
2347 Others_Mbox := True;
2348 Mbox_Present := True;
2350 if Expander_Active then
2351 return New_Copy_Tree (Expression (Parent (Compon)));
2353 return Expression (Parent (Compon));
2357 Check_Non_Limited_Type;
2359 if Present (Others_Etype) and then
2360 Base_Type (Others_Etype) /= Base_Type (Etype
2363 Error_Msg_N ("components in OTHERS choice must " &
2364 "have same type", Selector_Name);
2367 Others_Etype := Etype (Compon);
2369 if Expander_Active then
2370 return New_Copy_Tree (Expression (Assoc));
2372 return Expression (Assoc);
2377 elsif Chars (Compon) = Chars (Selector_Name) then
2380 -- Ada 2005 (AI-231)
2382 if Ada_Version >= Ada_05
2383 and then Nkind (Expression (Assoc)) = N_Null
2385 Check_Can_Never_Be_Null (Compon, Expression (Assoc));
2388 -- We need to duplicate the expression when several
2389 -- components are grouped together with a "|" choice.
2390 -- For instance "filed1 | filed2 => Expr"
2392 -- Ada 2005 (AI-287)
2394 if Box_Present (Assoc) then
2395 Mbox_Present := True;
2397 -- Duplicate the default expression of the component
2398 -- from the record type declaration
2400 if Present (Next (Selector_Name)) then
2402 New_Copy_Tree (Expression (Parent (Compon)));
2404 Expr := Expression (Parent (Compon));
2408 Check_Non_Limited_Type;
2410 if Present (Next (Selector_Name)) then
2411 Expr := New_Copy_Tree (Expression (Assoc));
2413 Expr := Expression (Assoc);
2417 Generate_Reference (Compon, Selector_Name);
2421 ("more than one value supplied for &",
2422 Selector_Name, Compon);
2427 Next (Selector_Name);
2436 -----------------------
2437 -- Resolve_Aggr_Expr --
2438 -----------------------
2440 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Node_Id) is
2441 New_C : Entity_Id := Component;
2442 Expr_Type : Entity_Id := Empty;
2444 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean;
2445 -- If the expression is an aggregate (possibly qualified) then its
2446 -- expansion is delayed until the enclosing aggregate is expanded
2447 -- into assignments. In that case, do not generate checks on the
2448 -- expression, because they will be generated later, and will other-
2449 -- wise force a copy (to remove side-effects) that would leave a
2450 -- dynamic-sized aggregate in the code, something that gigi cannot
2454 -- Set to True if the resolved Expr node needs to be relocated
2455 -- when attached to the newly created association list. This node
2456 -- need not be relocated if its parent pointer is not set.
2457 -- In fact in this case Expr is the output of a New_Copy_Tree call.
2458 -- if Relocate is True then we have analyzed the expression node
2459 -- in the original aggregate and hence it needs to be relocated
2460 -- when moved over the new association list.
2462 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean is
2463 Kind : constant Node_Kind := Nkind (Expr);
2466 return ((Kind = N_Aggregate
2467 or else Kind = N_Extension_Aggregate)
2468 and then Present (Etype (Expr))
2469 and then Is_Record_Type (Etype (Expr))
2470 and then Expansion_Delayed (Expr))
2472 or else (Kind = N_Qualified_Expression
2473 and then Has_Expansion_Delayed (Expression (Expr)));
2474 end Has_Expansion_Delayed;
2476 -- Start of processing for Resolve_Aggr_Expr
2479 -- If the type of the component is elementary or the type of the
2480 -- aggregate does not contain discriminants, use the type of the
2481 -- component to resolve Expr.
2483 if Is_Elementary_Type (Etype (Component))
2484 or else not Has_Discriminants (Etype (N))
2486 Expr_Type := Etype (Component);
2488 -- Otherwise we have to pick up the new type of the component from
2489 -- the new costrained subtype of the aggregate. In fact components
2490 -- which are of a composite type might be constrained by a
2491 -- discriminant, and we want to resolve Expr against the subtype were
2492 -- all discriminant occurrences are replaced with their actual value.
2495 New_C := First_Component (Etype (N));
2496 while Present (New_C) loop
2497 if Chars (New_C) = Chars (Component) then
2498 Expr_Type := Etype (New_C);
2502 Next_Component (New_C);
2505 pragma Assert (Present (Expr_Type));
2507 -- For each range in an array type where a discriminant has been
2508 -- replaced with the constraint, check that this range is within
2509 -- the range of the base type. This checks is done in the
2510 -- init proc for regular objects, but has to be done here for
2511 -- aggregates since no init proc is called for them.
2513 if Is_Array_Type (Expr_Type) then
2515 Index : Node_Id := First_Index (Expr_Type);
2516 -- Range of the current constrained index in the array.
2518 Orig_Index : Node_Id := First_Index (Etype (Component));
2519 -- Range corresponding to the range Index above in the
2520 -- original unconstrained record type. The bounds of this
2521 -- range may be governed by discriminants.
2523 Unconstr_Index : Node_Id := First_Index (Etype (Expr_Type));
2524 -- Range corresponding to the range Index above for the
2525 -- unconstrained array type. This range is needed to apply
2529 while Present (Index) loop
2530 if Depends_On_Discriminant (Orig_Index) then
2531 Apply_Range_Check (Index, Etype (Unconstr_Index));
2535 Next_Index (Orig_Index);
2536 Next_Index (Unconstr_Index);
2542 -- If the Parent pointer of Expr is not set, Expr is an expression
2543 -- duplicated by New_Tree_Copy (this happens for record aggregates
2544 -- that look like (Field1 | Filed2 => Expr) or (others => Expr)).
2545 -- Such a duplicated expression must be attached to the tree
2546 -- before analysis and resolution to enforce the rule that a tree
2547 -- fragment should never be analyzed or resolved unless it is
2548 -- attached to the current compilation unit.
2550 if No (Parent (Expr)) then
2551 Set_Parent (Expr, N);
2557 Analyze_And_Resolve (Expr, Expr_Type);
2558 Check_Non_Static_Context (Expr);
2559 Check_Unset_Reference (Expr);
2561 if not Has_Expansion_Delayed (Expr) then
2562 Aggregate_Constraint_Checks (Expr, Expr_Type);
2565 if Raises_Constraint_Error (Expr) then
2566 Set_Raises_Constraint_Error (N);
2570 Add_Association (New_C, Relocate_Node (Expr));
2572 Add_Association (New_C, Expr);
2574 end Resolve_Aggr_Expr;
2576 -- Resolve_Record_Aggregate local variables
2579 -- N_Component_Association node belonging to the input aggregate N
2582 Positional_Expr : Node_Id;
2583 Component : Entity_Id;
2584 Component_Elmt : Elmt_Id;
2586 Components : constant Elist_Id := New_Elmt_List;
2587 -- Components is the list of the record components whose value must
2588 -- be provided in the aggregate. This list does include discriminants.
2590 -- Start of processing for Resolve_Record_Aggregate
2593 -- We may end up calling Duplicate_Subexpr on expressions that are
2594 -- attached to New_Assoc_List. For this reason we need to attach it
2595 -- to the tree by setting its parent pointer to N. This parent point
2596 -- will change in STEP 8 below.
2598 Set_Parent (New_Assoc_List, N);
2600 -- STEP 1: abstract type and null record verification
2602 if Is_Abstract (Typ) then
2603 Error_Msg_N ("type of aggregate cannot be abstract", N);
2606 if No (First_Entity (Typ)) and then Null_Record_Present (N) then
2610 elsif Present (First_Entity (Typ))
2611 and then Null_Record_Present (N)
2612 and then not Is_Tagged_Type (Typ)
2614 Error_Msg_N ("record aggregate cannot be null", N);
2617 elsif No (First_Entity (Typ)) then
2618 Error_Msg_N ("record aggregate must be null", N);
2622 -- STEP 2: Verify aggregate structure
2625 Selector_Name : Node_Id;
2626 Bad_Aggregate : Boolean := False;
2629 if Present (Component_Associations (N)) then
2630 Assoc := First (Component_Associations (N));
2635 while Present (Assoc) loop
2636 Selector_Name := First (Choices (Assoc));
2637 while Present (Selector_Name) loop
2638 if Nkind (Selector_Name) = N_Identifier then
2641 elsif Nkind (Selector_Name) = N_Others_Choice then
2642 if Selector_Name /= First (Choices (Assoc))
2643 or else Present (Next (Selector_Name))
2645 Error_Msg_N ("OTHERS must appear alone in a choice list",
2649 elsif Present (Next (Assoc)) then
2650 Error_Msg_N ("OTHERS must appear last in an aggregate",
2657 ("selector name should be identifier or OTHERS",
2659 Bad_Aggregate := True;
2662 Next (Selector_Name);
2668 if Bad_Aggregate then
2673 -- STEP 3: Find discriminant Values
2676 Discrim : Entity_Id;
2677 Missing_Discriminants : Boolean := False;
2680 if Present (Expressions (N)) then
2681 Positional_Expr := First (Expressions (N));
2683 Positional_Expr := Empty;
2686 if Has_Discriminants (Typ) then
2687 Discrim := First_Discriminant (Typ);
2692 -- First find the discriminant values in the positional components
2694 while Present (Discrim) and then Present (Positional_Expr) loop
2695 if Discr_Present (Discrim) then
2696 Resolve_Aggr_Expr (Positional_Expr, Discrim);
2698 -- Ada 2005 (AI-231)
2700 if Ada_Version >= Ada_05 then
2701 Check_Can_Never_Be_Null (Discrim, Positional_Expr);
2704 Next (Positional_Expr);
2707 if Present (Get_Value (Discrim, Component_Associations (N))) then
2709 ("more than one value supplied for discriminant&",
2713 Next_Discriminant (Discrim);
2716 -- Find remaining discriminant values, if any, among named components
2718 while Present (Discrim) loop
2719 Expr := Get_Value (Discrim, Component_Associations (N), True);
2721 if not Discr_Present (Discrim) then
2722 if Present (Expr) then
2724 ("more than one value supplied for discriminant&",
2728 elsif No (Expr) then
2730 ("no value supplied for discriminant &", N, Discrim);
2731 Missing_Discriminants := True;
2734 Resolve_Aggr_Expr (Expr, Discrim);
2737 Next_Discriminant (Discrim);
2740 if Missing_Discriminants then
2744 -- At this point and until the beginning of STEP 6, New_Assoc_List
2745 -- contains only the discriminants and their values.
2749 -- STEP 4: Set the Etype of the record aggregate
2751 -- ??? This code is pretty much a copy of Sem_Ch3.Build_Subtype. That
2752 -- routine should really be exported in sem_util or some such and used
2753 -- in sem_ch3 and here rather than have a copy of the code which is a
2754 -- maintenance nightmare.
2756 -- ??? Performace WARNING. The current implementation creates a new
2757 -- itype for all aggregates whose base type is discriminated.
2758 -- This means that for record aggregates nested inside an array
2759 -- aggregate we will create a new itype for each record aggregate
2760 -- if the array cmponent type has discriminants. For large aggregates
2761 -- this may be a problem. What should be done in this case is
2762 -- to reuse itypes as much as possible.
2764 if Has_Discriminants (Typ) then
2765 Build_Constrained_Itype : declare
2766 Loc : constant Source_Ptr := Sloc (N);
2768 Subtyp_Decl : Node_Id;
2771 C : constant List_Id := New_List;
2774 New_Assoc := First (New_Assoc_List);
2775 while Present (New_Assoc) loop
2776 Append (Duplicate_Subexpr (Expression (New_Assoc)), To => C);
2781 Make_Subtype_Indication (Loc,
2782 Subtype_Mark => New_Occurrence_Of (Base_Type (Typ), Loc),
2783 Constraint => Make_Index_Or_Discriminant_Constraint (Loc, C));
2785 Def_Id := Create_Itype (Ekind (Typ), N);
2788 Make_Subtype_Declaration (Loc,
2789 Defining_Identifier => Def_Id,
2790 Subtype_Indication => Indic);
2791 Set_Parent (Subtyp_Decl, Parent (N));
2793 -- Itypes must be analyzed with checks off (see itypes.ads).
2795 Analyze (Subtyp_Decl, Suppress => All_Checks);
2797 Set_Etype (N, Def_Id);
2798 Check_Static_Discriminated_Subtype
2799 (Def_Id, Expression (First (New_Assoc_List)));
2800 end Build_Constrained_Itype;
2806 -- STEP 5: Get remaining components according to discriminant values
2809 Record_Def : Node_Id;
2810 Parent_Typ : Entity_Id;
2811 Root_Typ : Entity_Id;
2812 Parent_Typ_List : Elist_Id;
2813 Parent_Elmt : Elmt_Id;
2814 Errors_Found : Boolean := False;
2818 if Is_Derived_Type (Typ) and then Is_Tagged_Type (Typ) then
2819 Parent_Typ_List := New_Elmt_List;
2821 -- If this is an extension aggregate, the component list must
2822 -- include all components that are not in the given ancestor
2823 -- type. Otherwise, the component list must include components
2824 -- of all ancestors, starting with the root.
2826 if Nkind (N) = N_Extension_Aggregate then
2827 Root_Typ := Base_Type (Etype (Ancestor_Part (N)));
2829 Root_Typ := Root_Type (Typ);
2831 if Nkind (Parent (Base_Type (Root_Typ)))
2832 = N_Private_Type_Declaration
2835 ("type of aggregate has private ancestor&!",
2837 Error_Msg_N ("must use extension aggregate!", N);
2841 Dnode := Declaration_Node (Base_Type (Root_Typ));
2843 -- If we don't get a full declaration, then we have some
2844 -- error which will get signalled later so skip this part.
2845 -- Otherwise, gather components of root that apply to the
2846 -- aggregate type. We use the base type in case there is an
2847 -- applicable stored constraint that renames the discriminants
2850 if Nkind (Dnode) = N_Full_Type_Declaration then
2851 Record_Def := Type_Definition (Dnode);
2852 Gather_Components (Base_Type (Typ),
2853 Component_List (Record_Def),
2854 Governed_By => New_Assoc_List,
2856 Report_Errors => Errors_Found);
2860 Parent_Typ := Base_Type (Typ);
2861 while Parent_Typ /= Root_Typ loop
2863 Prepend_Elmt (Parent_Typ, To => Parent_Typ_List);
2864 Parent_Typ := Etype (Parent_Typ);
2866 if Nkind (Parent (Base_Type (Parent_Typ))) =
2867 N_Private_Type_Declaration
2868 or else Nkind (Parent (Base_Type (Parent_Typ))) =
2869 N_Private_Extension_Declaration
2871 if Nkind (N) /= N_Extension_Aggregate then
2873 ("type of aggregate has private ancestor&!",
2875 Error_Msg_N ("must use extension aggregate!", N);
2878 elsif Parent_Typ /= Root_Typ then
2880 ("ancestor part of aggregate must be private type&",
2881 Ancestor_Part (N), Parent_Typ);
2887 -- Now collect components from all other ancestors.
2889 Parent_Elmt := First_Elmt (Parent_Typ_List);
2890 while Present (Parent_Elmt) loop
2891 Parent_Typ := Node (Parent_Elmt);
2892 Record_Def := Type_Definition (Parent (Base_Type (Parent_Typ)));
2893 Gather_Components (Empty,
2894 Component_List (Record_Extension_Part (Record_Def)),
2895 Governed_By => New_Assoc_List,
2897 Report_Errors => Errors_Found);
2899 Next_Elmt (Parent_Elmt);
2903 Record_Def := Type_Definition (Parent (Base_Type (Typ)));
2905 if Null_Present (Record_Def) then
2908 Gather_Components (Base_Type (Typ),
2909 Component_List (Record_Def),
2910 Governed_By => New_Assoc_List,
2912 Report_Errors => Errors_Found);
2916 if Errors_Found then
2921 -- STEP 6: Find component Values
2924 Component_Elmt := First_Elmt (Components);
2926 -- First scan the remaining positional associations in the aggregate.
2927 -- Remember that at this point Positional_Expr contains the current
2928 -- positional association if any is left after looking for discriminant
2929 -- values in step 3.
2931 while Present (Positional_Expr) and then Present (Component_Elmt) loop
2932 Component := Node (Component_Elmt);
2933 Resolve_Aggr_Expr (Positional_Expr, Component);
2935 -- Ada 2005 (AI-231)
2937 if Ada_Version >= Ada_05 then
2938 Check_Can_Never_Be_Null (Component, Positional_Expr);
2941 if Present (Get_Value (Component, Component_Associations (N))) then
2943 ("more than one value supplied for Component &", N, Component);
2946 Next (Positional_Expr);
2947 Next_Elmt (Component_Elmt);
2950 if Present (Positional_Expr) then
2952 ("too many components for record aggregate", Positional_Expr);
2955 -- Now scan for the named arguments of the aggregate
2957 while Present (Component_Elmt) loop
2958 Component := Node (Component_Elmt);
2959 Expr := Get_Value (Component, Component_Associations (N), True);
2961 -- Ada 2005 (AI-287): Default initialized limited component are
2962 -- passed to the expander, that will generate calls to the
2963 -- corresponding IP.
2965 if Mbox_Present and then Is_Limited_Type (Etype (Component)) then
2967 (Component => Component,
2969 Box_Present => True);
2971 -- Ada 2005 (AI-287): No value supplied for component
2973 elsif Mbox_Present and No (Expr) then
2976 elsif No (Expr) then
2977 Error_Msg_NE ("no value supplied for component &!", N, Component);
2980 Resolve_Aggr_Expr (Expr, Component);
2983 Next_Elmt (Component_Elmt);
2986 -- STEP 7: check for invalid components + check type in choice list
2993 -- Type of first component in choice list
2996 if Present (Component_Associations (N)) then
2997 Assoc := First (Component_Associations (N));
3002 Verification : while Present (Assoc) loop
3003 Selectr := First (Choices (Assoc));
3006 if Nkind (Selectr) = N_Others_Choice then
3008 -- Ada 2005 (AI-287): others choice may have expression or mbox
3010 if No (Others_Etype)
3011 and then not Others_Mbox
3014 ("OTHERS must represent at least one component", Selectr);
3020 while Present (Selectr) loop
3021 New_Assoc := First (New_Assoc_List);
3022 while Present (New_Assoc) loop
3023 Component := First (Choices (New_Assoc));
3024 exit when Chars (Selectr) = Chars (Component);
3028 -- If no association, this is not a legal component of
3029 -- of the type in question, except if this is an internal
3030 -- component supplied by a previous expansion.
3032 if No (New_Assoc) then
3033 if Box_Present (Parent (Selectr)) then
3036 elsif Chars (Selectr) /= Name_uTag
3037 and then Chars (Selectr) /= Name_uParent
3038 and then Chars (Selectr) /= Name_uController
3040 if not Has_Discriminants (Typ) then
3041 Error_Msg_Node_2 := Typ;
3043 ("& is not a component of}",
3047 ("& is not a component of the aggregate subtype",
3051 Check_Misspelled_Component (Components, Selectr);
3054 elsif No (Typech) then
3055 Typech := Base_Type (Etype (Component));
3057 elsif Typech /= Base_Type (Etype (Component)) then
3058 if not Box_Present (Parent (Selectr)) then
3060 ("components in choice list must have same type",
3069 end loop Verification;
3072 -- STEP 8: replace the original aggregate
3075 New_Aggregate : constant Node_Id := New_Copy (N);
3078 Set_Expressions (New_Aggregate, No_List);
3079 Set_Etype (New_Aggregate, Etype (N));
3080 Set_Component_Associations (New_Aggregate, New_Assoc_List);
3082 Rewrite (N, New_Aggregate);
3084 end Resolve_Record_Aggregate;
3086 -----------------------------
3087 -- Check_Can_Never_Be_Null --
3088 -----------------------------
3090 procedure Check_Can_Never_Be_Null (N : Node_Id; Expr : Node_Id) is
3092 pragma Assert (Ada_Version >= Ada_05);
3094 if Nkind (Expr) = N_Null
3095 and then Can_Never_Be_Null (N)
3097 Apply_Compile_Time_Constraint_Error
3099 Msg => "(Ada 2005) NULL not allowed in"
3100 & " null-excluding components?",
3101 Reason => CE_Null_Not_Allowed,
3104 end Check_Can_Never_Be_Null;
3106 ---------------------
3107 -- Sort_Case_Table --
3108 ---------------------
3110 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is
3111 L : constant Int := Case_Table'First;
3112 U : constant Int := Case_Table'Last;
3121 T := Case_Table (K + 1);
3125 and then Expr_Value (Case_Table (J - 1).Choice_Lo) >
3126 Expr_Value (T.Choice_Lo)
3128 Case_Table (J) := Case_Table (J - 1);
3132 Case_Table (J) := T;
3135 end Sort_Case_Table;