1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2004 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_Tss; use Exp_Tss;
33 with Exp_Util; use Exp_Util;
34 with Freeze; use Freeze;
35 with Itypes; use Itypes;
36 with Lib.Xref; use Lib.Xref;
37 with Namet; use Namet;
38 with Nmake; use Nmake;
39 with Nlists; use Nlists;
42 with Sem_Cat; use Sem_Cat;
43 with Sem_Ch8; use Sem_Ch8;
44 with Sem_Ch13; use Sem_Ch13;
45 with Sem_Eval; use Sem_Eval;
46 with Sem_Res; use Sem_Res;
47 with Sem_Util; use Sem_Util;
48 with Sem_Type; use Sem_Type;
49 with Sem_Warn; use Sem_Warn;
50 with Sinfo; use Sinfo;
51 with Snames; use Snames;
52 with Stringt; use Stringt;
53 with Stand; use Stand;
54 with Targparm; use Targparm;
55 with Tbuild; use Tbuild;
56 with Uintp; use Uintp;
58 with GNAT.Spelling_Checker; use GNAT.Spelling_Checker;
60 package body Sem_Aggr is
62 type Case_Bounds is record
65 Choice_Node : Node_Id;
68 type Case_Table_Type is array (Nat range <>) of Case_Bounds;
69 -- Table type used by Check_Case_Choices procedure
71 -----------------------
72 -- Local Subprograms --
73 -----------------------
75 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type);
76 -- Sort the Case Table using the Lower Bound of each Choice as the key.
77 -- A simple insertion sort is used since the number of choices in a case
78 -- statement of variant part will usually be small and probably in near
81 procedure Check_Can_Never_Be_Null (N : Node_Id; Expr : Node_Id);
82 -- Ada 0Y (AI-231): Check bad usage of the null-exclusion issue
84 ------------------------------------------------------
85 -- Subprograms used for RECORD AGGREGATE Processing --
86 ------------------------------------------------------
88 procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id);
89 -- This procedure performs all the semantic checks required for record
90 -- aggregates. Note that for aggregates analysis and resolution go
91 -- hand in hand. Aggregate analysis has been delayed up to here and
92 -- it is done while resolving the aggregate.
94 -- N is the N_Aggregate node.
95 -- Typ is the record type for the aggregate resolution
97 -- While performing the semantic checks, this procedure
98 -- builds a new Component_Association_List where each record field
99 -- appears alone in a Component_Choice_List along with its corresponding
100 -- expression. The record fields in the Component_Association_List
101 -- appear in the same order in which they appear in the record type Typ.
103 -- Once this new Component_Association_List is built and all the
104 -- semantic checks performed, the original aggregate subtree is replaced
105 -- with the new named record aggregate just built. Note that the subtree
106 -- substitution is performed with Rewrite so as to be
107 -- able to retrieve the original aggregate.
109 -- The aggregate subtree manipulation performed by Resolve_Record_Aggregate
110 -- yields the aggregate format expected by Gigi. Typically, this kind of
111 -- tree manipulations are done in the expander. However, because the
112 -- semantic checks that need to be performed on record aggregates really
113 -- go hand in hand with the record aggregate normalization, the aggregate
114 -- subtree transformation is performed during resolution rather than
115 -- expansion. Had we decided otherwise we would have had to duplicate
116 -- most of the code in the expansion procedure Expand_Record_Aggregate.
117 -- Note, however, that all the expansion concerning aggegates for tagged
118 -- records is done in Expand_Record_Aggregate.
120 -- The algorithm of Resolve_Record_Aggregate proceeds as follows:
122 -- 1. Make sure that the record type against which the record aggregate
123 -- has to be resolved is not abstract. Furthermore if the type is
124 -- a null aggregate make sure the input aggregate N is also null.
126 -- 2. Verify that the structure of the aggregate is that of a record
127 -- aggregate. Specifically, look for component associations and ensure
128 -- that each choice list only has identifiers or the N_Others_Choice
129 -- node. Also make sure that if present, the N_Others_Choice occurs
130 -- last and by itself.
132 -- 3. If Typ contains discriminants, the values for each discriminant
133 -- is looked for. If the record type Typ has variants, we check
134 -- that the expressions corresponding to each discriminant ruling
135 -- the (possibly nested) variant parts of Typ, are static. This
136 -- allows us to determine the variant parts to which the rest of
137 -- the aggregate must conform. The names of discriminants with their
138 -- values are saved in a new association list, New_Assoc_List which
139 -- is later augmented with the names and values of the remaining
140 -- components in the record type.
142 -- During this phase we also make sure that every discriminant is
143 -- assigned exactly one value. Note that when several values
144 -- for a given discriminant are found, semantic processing continues
145 -- looking for further errors. In this case it's the first
146 -- discriminant value found which we will be recorded.
148 -- IMPORTANT NOTE: For derived tagged types this procedure expects
149 -- First_Discriminant and Next_Discriminant to give the correct list
150 -- of discriminants, in the correct order.
152 -- 4. After all the discriminant values have been gathered, we can
153 -- set the Etype of the record aggregate. If Typ contains no
154 -- discriminants this is straightforward: the Etype of N is just
155 -- Typ, otherwise a new implicit constrained subtype of Typ is
156 -- built to be the Etype of N.
158 -- 5. Gather the remaining record components according to the discriminant
159 -- values. This involves recursively traversing the record type
160 -- structure to see what variants are selected by the given discriminant
161 -- values. This processing is a little more convoluted if Typ is a
162 -- derived tagged types since we need to retrieve the record structure
163 -- of all the ancestors of Typ.
165 -- 6. After gathering the record components we look for their values
166 -- in the record aggregate and emit appropriate error messages
167 -- should we not find such values or should they be duplicated.
169 -- 7. We then make sure no illegal component names appear in the
170 -- record aggegate and make sure that the type of the record
171 -- components appearing in a same choice list is the same.
172 -- Finally we ensure that the others choice, if present, is
173 -- used to provide the value of at least a record component.
175 -- 8. The original aggregate node is replaced with the new named
176 -- aggregate built in steps 3 through 6, as explained earlier.
178 -- Given the complexity of record aggregate resolution, the primary
179 -- goal of this routine is clarity and simplicity rather than execution
180 -- and storage efficiency. If there are only positional components in the
181 -- aggregate the running time is linear. If there are associations
182 -- the running time is still linear as long as the order of the
183 -- associations is not too far off the order of the components in the
184 -- record type. If this is not the case the running time is at worst
185 -- quadratic in the size of the association list.
187 procedure Check_Misspelled_Component
188 (Elements : Elist_Id;
189 Component : Node_Id);
190 -- Give possible misspelling diagnostic if Component is likely to be
191 -- a misspelling of one of the components of the Assoc_List.
192 -- This is called by Resolv_Aggr_Expr after producing
193 -- an invalid component error message.
195 procedure Check_Static_Discriminated_Subtype (T : Entity_Id; V : Node_Id);
196 -- An optimization: determine whether a discriminated subtype has a
197 -- static constraint, and contains array components whose length is also
198 -- static, either because they are constrained by the discriminant, or
199 -- because the original component bounds are static.
201 -----------------------------------------------------
202 -- Subprograms used for ARRAY AGGREGATE Processing --
203 -----------------------------------------------------
205 function Resolve_Array_Aggregate
208 Index_Constr : Node_Id;
209 Component_Typ : Entity_Id;
210 Others_Allowed : Boolean)
212 -- This procedure performs the semantic checks for an array aggregate.
213 -- True is returned if the aggregate resolution succeeds.
214 -- The procedure works by recursively checking each nested aggregate.
215 -- Specifically, after checking a sub-aggreate nested at the i-th level
216 -- we recursively check all the subaggregates at the i+1-st level (if any).
217 -- Note that for aggregates analysis and resolution go hand in hand.
218 -- Aggregate analysis has been delayed up to here and it is done while
219 -- resolving the aggregate.
221 -- N is the current N_Aggregate node to be checked.
223 -- Index is the index node corresponding to the array sub-aggregate that
224 -- we are currently checking (RM 4.3.3 (8)). Its Etype is the
225 -- corresponding index type (or subtype).
227 -- Index_Constr is the node giving the applicable index constraint if
228 -- any (RM 4.3.3 (10)). It "is a constraint provided by certain
229 -- contexts [...] that can be used to determine the bounds of the array
230 -- value specified by the aggregate". If Others_Allowed below is False
231 -- there is no applicable index constraint and this node is set to Index.
233 -- Component_Typ is the array component type.
235 -- Others_Allowed indicates whether an others choice is allowed
236 -- in the context where the top-level aggregate appeared.
238 -- The algorithm of Resolve_Array_Aggregate proceeds as follows:
240 -- 1. Make sure that the others choice, if present, is by itself and
241 -- appears last in the sub-aggregate. Check that we do not have
242 -- positional and named components in the array sub-aggregate (unless
243 -- the named association is an others choice). Finally if an others
244 -- choice is present, make sure it is allowed in the aggregate contex.
246 -- 2. If the array sub-aggregate contains discrete_choices:
248 -- (A) Verify their validity. Specifically verify that:
250 -- (a) If a null range is present it must be the only possible
251 -- choice in the array aggregate.
253 -- (b) Ditto for a non static range.
255 -- (c) Ditto for a non static expression.
257 -- In addition this step analyzes and resolves each discrete_choice,
258 -- making sure that its type is the type of the corresponding Index.
259 -- If we are not at the lowest array aggregate level (in the case of
260 -- multi-dimensional aggregates) then invoke Resolve_Array_Aggregate
261 -- recursively on each component expression. Otherwise, resolve the
262 -- bottom level component expressions against the expected component
263 -- type ONLY IF the component corresponds to a single discrete choice
264 -- which is not an others choice (to see why read the DELAYED
265 -- COMPONENT RESOLUTION below).
267 -- (B) Determine the bounds of the sub-aggregate and lowest and
268 -- highest choice values.
270 -- 3. For positional aggregates:
272 -- (A) Loop over the component expressions either recursively invoking
273 -- Resolve_Array_Aggregate on each of these for multi-dimensional
274 -- array aggregates or resolving the bottom level component
275 -- expressions against the expected component type.
277 -- (B) Determine the bounds of the positional sub-aggregates.
279 -- 4. Try to determine statically whether the evaluation of the array
280 -- sub-aggregate raises Constraint_Error. If yes emit proper
281 -- warnings. The precise checks are the following:
283 -- (A) Check that the index range defined by aggregate bounds is
284 -- compatible with corresponding index subtype.
285 -- We also check against the base type. In fact it could be that
286 -- Low/High bounds of the base type are static whereas those of
287 -- the index subtype are not. Thus if we can statically catch
288 -- a problem with respect to the base type we are guaranteed
289 -- that the same problem will arise with the index subtype
291 -- (B) If we are dealing with a named aggregate containing an others
292 -- choice and at least one discrete choice then make sure the range
293 -- specified by the discrete choices does not overflow the
294 -- aggregate bounds. We also check against the index type and base
295 -- type bounds for the same reasons given in (A).
297 -- (C) If we are dealing with a positional aggregate with an others
298 -- choice make sure the number of positional elements specified
299 -- does not overflow the aggregate bounds. We also check against
300 -- the index type and base type bounds as mentioned in (A).
302 -- Finally construct an N_Range node giving the sub-aggregate bounds.
303 -- Set the Aggregate_Bounds field of the sub-aggregate to be this
304 -- N_Range. The routine Array_Aggr_Subtype below uses such N_Ranges
305 -- to build the appropriate aggregate subtype. Aggregate_Bounds
306 -- information is needed during expansion.
308 -- DELAYED COMPONENT RESOLUTION: The resolution of bottom level component
309 -- expressions in an array aggregate may call Duplicate_Subexpr or some
310 -- other routine that inserts code just outside the outermost aggregate.
311 -- If the array aggregate contains discrete choices or an others choice,
312 -- this may be wrong. Consider for instance the following example.
314 -- type Rec is record
318 -- type Acc_Rec is access Rec;
319 -- Arr : array (1..3) of Acc_Rec := (1 .. 3 => new Rec);
321 -- Then the transformation of "new Rec" that occurs during resolution
322 -- entails the following code modifications
324 -- P7b : constant Acc_Rec := new Rec;
326 -- Arr : array (1..3) of Acc_Rec := (1 .. 3 => P7b);
328 -- This code transformation is clearly wrong, since we need to call
329 -- "new Rec" for each of the 3 array elements. To avoid this problem we
330 -- delay resolution of the components of non positional array aggregates
331 -- to the expansion phase. As an optimization, if the discrete choice
332 -- specifies a single value we do not delay resolution.
334 function Array_Aggr_Subtype (N : Node_Id; Typ : Node_Id) return Entity_Id;
335 -- This routine returns the type or subtype of an array aggregate.
337 -- N is the array aggregate node whose type we return.
339 -- Typ is the context type in which N occurs.
341 -- This routine creates an implicit array subtype whose bounds are
342 -- those defined by the aggregate. When this routine is invoked
343 -- Resolve_Array_Aggregate has already processed aggregate N. Thus the
344 -- Aggregate_Bounds of each sub-aggregate, is an N_Range node giving the
345 -- sub-aggregate bounds. When building the aggegate itype, this function
346 -- traverses the array aggregate N collecting such Aggregate_Bounds and
347 -- constructs the proper array aggregate itype.
349 -- Note that in the case of multidimensional aggregates each inner
350 -- sub-aggregate corresponding to a given array dimension, may provide a
351 -- different bounds. If it is possible to determine statically that
352 -- some sub-aggregates corresponding to the same index do not have the
353 -- same bounds, then a warning is emitted. If such check is not possible
354 -- statically (because some sub-aggregate bounds are dynamic expressions)
355 -- then this job is left to the expander. In all cases the particular
356 -- bounds that this function will chose for a given dimension is the first
357 -- N_Range node for a sub-aggregate corresponding to that dimension.
359 -- Note that the Raises_Constraint_Error flag of an array aggregate
360 -- whose evaluation is determined to raise CE by Resolve_Array_Aggregate,
361 -- is set in Resolve_Array_Aggregate but the aggregate is not
362 -- immediately replaced with a raise CE. In fact, Array_Aggr_Subtype must
363 -- first construct the proper itype for the aggregate (Gigi needs
364 -- this). After constructing the proper itype we will eventually replace
365 -- the top-level aggregate with a raise CE (done in Resolve_Aggregate).
366 -- Of course in cases such as:
368 -- type Arr is array (integer range <>) of Integer;
369 -- A : Arr := (positive range -1 .. 2 => 0);
371 -- The bounds of the aggregate itype are cooked up to look reasonable
372 -- (in this particular case the bounds will be 1 .. 2).
374 procedure Aggregate_Constraint_Checks
376 Check_Typ : Entity_Id);
377 -- Checks expression Exp against subtype Check_Typ. If Exp is an
378 -- aggregate and Check_Typ a constrained record type with discriminants,
379 -- we generate the appropriate discriminant checks. If Exp is an array
380 -- aggregate then emit the appropriate length checks. If Exp is a scalar
381 -- type, or a string literal, Exp is changed into Check_Typ'(Exp) to
382 -- ensure that range checks are performed at run time.
384 procedure Make_String_Into_Aggregate (N : Node_Id);
385 -- A string literal can appear in a context in which a one dimensional
386 -- array of characters is expected. This procedure simply rewrites the
387 -- string as an aggregate, prior to resolution.
389 ---------------------------------
390 -- Aggregate_Constraint_Checks --
391 ---------------------------------
393 procedure Aggregate_Constraint_Checks
395 Check_Typ : Entity_Id)
397 Exp_Typ : constant Entity_Id := Etype (Exp);
400 if Raises_Constraint_Error (Exp) then
404 -- This is really expansion activity, so make sure that expansion
405 -- is on and is allowed.
407 if not Expander_Active or else In_Default_Expression then
411 -- First check if we have to insert discriminant checks
413 if Has_Discriminants (Exp_Typ) then
414 Apply_Discriminant_Check (Exp, Check_Typ);
416 -- Next emit length checks for array aggregates
418 elsif Is_Array_Type (Exp_Typ) then
419 Apply_Length_Check (Exp, Check_Typ);
421 -- Finally emit scalar and string checks. If we are dealing with a
422 -- scalar literal we need to check by hand because the Etype of
423 -- literals is not necessarily correct.
425 elsif Is_Scalar_Type (Exp_Typ)
426 and then Compile_Time_Known_Value (Exp)
428 if Is_Out_Of_Range (Exp, Base_Type (Check_Typ)) then
429 Apply_Compile_Time_Constraint_Error
430 (Exp, "value not in range of}?", CE_Range_Check_Failed,
431 Ent => Base_Type (Check_Typ),
432 Typ => Base_Type (Check_Typ));
434 elsif Is_Out_Of_Range (Exp, Check_Typ) then
435 Apply_Compile_Time_Constraint_Error
436 (Exp, "value not in range of}?", CE_Range_Check_Failed,
440 elsif not Range_Checks_Suppressed (Check_Typ) then
441 Apply_Scalar_Range_Check (Exp, Check_Typ);
444 elsif (Is_Scalar_Type (Exp_Typ)
445 or else Nkind (Exp) = N_String_Literal)
446 and then Exp_Typ /= Check_Typ
448 if Is_Entity_Name (Exp)
449 and then Ekind (Entity (Exp)) = E_Constant
451 -- If expression is a constant, it is worthwhile checking whether
452 -- it is a bound of the type.
454 if (Is_Entity_Name (Type_Low_Bound (Check_Typ))
455 and then Entity (Exp) = Entity (Type_Low_Bound (Check_Typ)))
456 or else (Is_Entity_Name (Type_High_Bound (Check_Typ))
457 and then Entity (Exp) = Entity (Type_High_Bound (Check_Typ)))
462 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
463 Analyze_And_Resolve (Exp, Check_Typ);
464 Check_Unset_Reference (Exp);
467 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
468 Analyze_And_Resolve (Exp, Check_Typ);
469 Check_Unset_Reference (Exp);
472 -- Ada 0Y (AI-231): Generate conversion to the null-excluding
473 -- type to force the corresponding run-time check
475 elsif Is_Access_Type (Check_Typ)
476 and then Can_Never_Be_Null (Check_Typ)
477 and then not Can_Never_Be_Null (Exp_Typ)
479 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
480 Analyze_And_Resolve (Exp, Check_Typ);
481 Check_Unset_Reference (Exp);
483 end Aggregate_Constraint_Checks;
485 ------------------------
486 -- Array_Aggr_Subtype --
487 ------------------------
489 function Array_Aggr_Subtype
494 Aggr_Dimension : constant Pos := Number_Dimensions (Typ);
495 -- Number of aggregate index dimensions.
497 Aggr_Range : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
498 -- Constrained N_Range of each index dimension in our aggregate itype.
500 Aggr_Low : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
501 Aggr_High : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
502 -- Low and High bounds for each index dimension in our aggregate itype.
504 Is_Fully_Positional : Boolean := True;
506 procedure Collect_Aggr_Bounds (N : Node_Id; Dim : Pos);
507 -- N is an array (sub-)aggregate. Dim is the dimension corresponding to
508 -- (sub-)aggregate N. This procedure collects the constrained N_Range
509 -- nodes corresponding to each index dimension of our aggregate itype.
510 -- These N_Range nodes are collected in Aggr_Range above.
511 -- Likewise collect in Aggr_Low & Aggr_High above the low and high
512 -- bounds of each index dimension. If, when collecting, two bounds
513 -- corresponding to the same dimension are static and found to differ,
514 -- then emit a warning, and mark N as raising Constraint_Error.
516 -------------------------
517 -- Collect_Aggr_Bounds --
518 -------------------------
520 procedure Collect_Aggr_Bounds (N : Node_Id; Dim : Pos) is
521 This_Range : constant Node_Id := Aggregate_Bounds (N);
522 -- The aggregate range node of this specific sub-aggregate.
524 This_Low : constant Node_Id := Low_Bound (Aggregate_Bounds (N));
525 This_High : constant Node_Id := High_Bound (Aggregate_Bounds (N));
526 -- The aggregate bounds of this specific sub-aggregate.
532 -- Collect the first N_Range for a given dimension that you find.
533 -- For a given dimension they must be all equal anyway.
535 if No (Aggr_Range (Dim)) then
536 Aggr_Low (Dim) := This_Low;
537 Aggr_High (Dim) := This_High;
538 Aggr_Range (Dim) := This_Range;
541 if Compile_Time_Known_Value (This_Low) then
542 if not Compile_Time_Known_Value (Aggr_Low (Dim)) then
543 Aggr_Low (Dim) := This_Low;
545 elsif Expr_Value (This_Low) /= Expr_Value (Aggr_Low (Dim)) then
546 Set_Raises_Constraint_Error (N);
547 Error_Msg_N ("Sub-aggregate low bound mismatch?", N);
548 Error_Msg_N ("Constraint_Error will be raised at run-time?",
553 if Compile_Time_Known_Value (This_High) then
554 if not Compile_Time_Known_Value (Aggr_High (Dim)) then
555 Aggr_High (Dim) := This_High;
558 Expr_Value (This_High) /= Expr_Value (Aggr_High (Dim))
560 Set_Raises_Constraint_Error (N);
561 Error_Msg_N ("Sub-aggregate high bound mismatch?", N);
562 Error_Msg_N ("Constraint_Error will be raised at run-time?",
568 if Dim < Aggr_Dimension then
570 -- Process positional components
572 if Present (Expressions (N)) then
573 Expr := First (Expressions (N));
574 while Present (Expr) loop
575 Collect_Aggr_Bounds (Expr, Dim + 1);
580 -- Process component associations
582 if Present (Component_Associations (N)) then
583 Is_Fully_Positional := False;
585 Assoc := First (Component_Associations (N));
586 while Present (Assoc) loop
587 Expr := Expression (Assoc);
588 Collect_Aggr_Bounds (Expr, Dim + 1);
593 end Collect_Aggr_Bounds;
595 -- Array_Aggr_Subtype variables
598 -- the final itype of the overall aggregate
600 Index_Constraints : constant List_Id := New_List;
601 -- The list of index constraints of the aggregate itype.
603 -- Start of processing for Array_Aggr_Subtype
606 -- Make sure that the list of index constraints is properly attached
607 -- to the tree, and then collect the aggregate bounds.
609 Set_Parent (Index_Constraints, N);
610 Collect_Aggr_Bounds (N, 1);
612 -- Build the list of constrained indices of our aggregate itype.
614 for J in 1 .. Aggr_Dimension loop
615 Create_Index : declare
616 Index_Base : constant Entity_Id :=
617 Base_Type (Etype (Aggr_Range (J)));
618 Index_Typ : Entity_Id;
621 -- Construct the Index subtype
623 Index_Typ := Create_Itype (Subtype_Kind (Ekind (Index_Base)), N);
625 Set_Etype (Index_Typ, Index_Base);
627 if Is_Character_Type (Index_Base) then
628 Set_Is_Character_Type (Index_Typ);
631 Set_Size_Info (Index_Typ, (Index_Base));
632 Set_RM_Size (Index_Typ, RM_Size (Index_Base));
633 Set_First_Rep_Item (Index_Typ, First_Rep_Item (Index_Base));
634 Set_Scalar_Range (Index_Typ, Aggr_Range (J));
636 if Is_Discrete_Or_Fixed_Point_Type (Index_Typ) then
637 Set_RM_Size (Index_Typ, UI_From_Int (Minimum_Size (Index_Typ)));
640 Set_Etype (Aggr_Range (J), Index_Typ);
642 Append (Aggr_Range (J), To => Index_Constraints);
646 -- Now build the Itype
648 Itype := Create_Itype (E_Array_Subtype, N);
650 Set_First_Rep_Item (Itype, First_Rep_Item (Typ));
651 Set_Convention (Itype, Convention (Typ));
652 Set_Depends_On_Private (Itype, Has_Private_Component (Typ));
653 Set_Etype (Itype, Base_Type (Typ));
654 Set_Has_Alignment_Clause (Itype, Has_Alignment_Clause (Typ));
655 Set_Is_Aliased (Itype, Is_Aliased (Typ));
656 Set_Depends_On_Private (Itype, Depends_On_Private (Typ));
658 Copy_Suppress_Status (Index_Check, Typ, Itype);
659 Copy_Suppress_Status (Length_Check, Typ, Itype);
661 Set_First_Index (Itype, First (Index_Constraints));
662 Set_Is_Constrained (Itype, True);
663 Set_Is_Internal (Itype, True);
664 Init_Size_Align (Itype);
666 -- A simple optimization: purely positional aggregates of static
667 -- components should be passed to gigi unexpanded whenever possible,
668 -- and regardless of the staticness of the bounds themselves. Subse-
669 -- quent checks in exp_aggr verify that type is not packed, etc.
671 Set_Size_Known_At_Compile_Time (Itype,
673 and then Comes_From_Source (N)
674 and then Size_Known_At_Compile_Time (Component_Type (Typ)));
676 -- We always need a freeze node for a packed array subtype, so that
677 -- we can build the Packed_Array_Type corresponding to the subtype.
678 -- If expansion is disabled, the packed array subtype is not built,
679 -- and we must not generate a freeze node for the type, or else it
680 -- will appear incomplete to gigi.
682 if Is_Packed (Itype) and then not In_Default_Expression
683 and then Expander_Active
685 Freeze_Itype (Itype, N);
689 end Array_Aggr_Subtype;
691 --------------------------------
692 -- Check_Misspelled_Component --
693 --------------------------------
695 procedure Check_Misspelled_Component
696 (Elements : Elist_Id;
699 Max_Suggestions : constant := 2;
701 Nr_Of_Suggestions : Natural := 0;
702 Suggestion_1 : Entity_Id := Empty;
703 Suggestion_2 : Entity_Id := Empty;
704 Component_Elmt : Elmt_Id;
707 -- All the components of List are matched against Component and
708 -- a count is maintained of possible misspellings. When at the
709 -- end of the analysis there are one or two (not more!) possible
710 -- misspellings, these misspellings will be suggested as
711 -- possible correction.
713 Get_Name_String (Chars (Component));
716 S : constant String (1 .. Name_Len) :=
717 Name_Buffer (1 .. Name_Len);
721 Component_Elmt := First_Elmt (Elements);
723 while Nr_Of_Suggestions <= Max_Suggestions
724 and then Present (Component_Elmt)
727 Get_Name_String (Chars (Node (Component_Elmt)));
729 if Is_Bad_Spelling_Of (Name_Buffer (1 .. Name_Len), S) then
730 Nr_Of_Suggestions := Nr_Of_Suggestions + 1;
732 case Nr_Of_Suggestions is
733 when 1 => Suggestion_1 := Node (Component_Elmt);
734 when 2 => Suggestion_2 := Node (Component_Elmt);
739 Next_Elmt (Component_Elmt);
742 -- Report at most two suggestions
744 if Nr_Of_Suggestions = 1 then
745 Error_Msg_NE ("\possible misspelling of&",
746 Component, Suggestion_1);
748 elsif Nr_Of_Suggestions = 2 then
749 Error_Msg_Node_2 := Suggestion_2;
750 Error_Msg_NE ("\possible misspelling of& or&",
751 Component, Suggestion_1);
754 end Check_Misspelled_Component;
756 ----------------------------------------
757 -- Check_Static_Discriminated_Subtype --
758 ----------------------------------------
760 procedure Check_Static_Discriminated_Subtype (T : Entity_Id; V : Node_Id) is
761 Disc : constant Entity_Id := First_Discriminant (T);
766 if Has_Record_Rep_Clause (T) then
769 elsif Present (Next_Discriminant (Disc)) then
772 elsif Nkind (V) /= N_Integer_Literal then
776 Comp := First_Component (T);
778 while Present (Comp) loop
780 if Is_Scalar_Type (Etype (Comp)) then
783 elsif Is_Private_Type (Etype (Comp))
784 and then Present (Full_View (Etype (Comp)))
785 and then Is_Scalar_Type (Full_View (Etype (Comp)))
789 elsif Is_Array_Type (Etype (Comp)) then
791 if Is_Bit_Packed_Array (Etype (Comp)) then
795 Ind := First_Index (Etype (Comp));
797 while Present (Ind) loop
799 if Nkind (Ind) /= N_Range
800 or else Nkind (Low_Bound (Ind)) /= N_Integer_Literal
801 or else Nkind (High_Bound (Ind)) /= N_Integer_Literal
813 Next_Component (Comp);
816 -- On exit, all components have statically known sizes.
818 Set_Size_Known_At_Compile_Time (T);
819 end Check_Static_Discriminated_Subtype;
821 --------------------------------
822 -- Make_String_Into_Aggregate --
823 --------------------------------
825 procedure Make_String_Into_Aggregate (N : Node_Id) is
826 Exprs : constant List_Id := New_List;
827 Loc : constant Source_Ptr := Sloc (N);
828 Str : constant String_Id := Strval (N);
829 Strlen : constant Nat := String_Length (Str);
837 for J in 1 .. Strlen loop
838 C := Get_String_Char (Str, J);
839 Set_Character_Literal_Name (C);
841 C_Node := Make_Character_Literal (P, Name_Find, C);
842 Set_Etype (C_Node, Any_Character);
843 Append_To (Exprs, C_Node);
846 -- something special for wide strings ???
849 New_N := Make_Aggregate (Loc, Expressions => Exprs);
850 Set_Analyzed (New_N);
851 Set_Etype (New_N, Any_Composite);
854 end Make_String_Into_Aggregate;
856 -----------------------
857 -- Resolve_Aggregate --
858 -----------------------
860 procedure Resolve_Aggregate (N : Node_Id; Typ : Entity_Id) is
861 Pkind : constant Node_Kind := Nkind (Parent (N));
863 Aggr_Subtyp : Entity_Id;
864 -- The actual aggregate subtype. This is not necessarily the same as Typ
865 -- which is the subtype of the context in which the aggregate was found.
868 -- Check for aggregates not allowed in configurable run-time mode.
869 -- We allow all cases of aggregates that do not come from source,
870 -- since these are all assumed to be small (e.g. bounds of a string
871 -- literal). We also allow aggregates of types we know to be small.
873 if not Support_Aggregates_On_Target
874 and then Comes_From_Source (N)
875 and then (not Known_Static_Esize (Typ) or else Esize (Typ) > 64)
877 Error_Msg_CRT ("aggregate", N);
880 if Is_Limited_Composite (Typ) then
881 Error_Msg_N ("aggregate type cannot have limited component", N);
882 Explain_Limited_Type (Typ, N);
884 -- Ada 0Y (AI-287): Limited aggregates allowed
886 elsif Is_Limited_Type (Typ)
887 and not Extensions_Allowed
889 Error_Msg_N ("aggregate type cannot be limited", N);
890 Explain_Limited_Type (Typ, N);
892 elsif Is_Class_Wide_Type (Typ) then
893 Error_Msg_N ("type of aggregate cannot be class-wide", N);
895 elsif Typ = Any_String
896 or else Typ = Any_Composite
898 Error_Msg_N ("no unique type for aggregate", N);
899 Set_Etype (N, Any_Composite);
901 elsif Is_Array_Type (Typ) and then Null_Record_Present (N) then
902 Error_Msg_N ("null record forbidden in array aggregate", N);
904 elsif Is_Record_Type (Typ) then
905 Resolve_Record_Aggregate (N, Typ);
907 elsif Is_Array_Type (Typ) then
909 -- First a special test, for the case of a positional aggregate
910 -- of characters which can be replaced by a string literal.
911 -- Do not perform this transformation if this was a string literal
912 -- to start with, whose components needed constraint checks, or if
913 -- the component type is non-static, because it will require those
914 -- checks and be transformed back into an aggregate.
916 if Number_Dimensions (Typ) = 1
918 (Root_Type (Component_Type (Typ)) = Standard_Character
920 Root_Type (Component_Type (Typ)) = Standard_Wide_Character)
921 and then No (Component_Associations (N))
922 and then not Is_Limited_Composite (Typ)
923 and then not Is_Private_Composite (Typ)
924 and then not Is_Bit_Packed_Array (Typ)
925 and then Nkind (Original_Node (Parent (N))) /= N_String_Literal
926 and then Is_Static_Subtype (Component_Type (Typ))
932 Expr := First (Expressions (N));
933 while Present (Expr) loop
934 exit when Nkind (Expr) /= N_Character_Literal;
941 Expr := First (Expressions (N));
942 while Present (Expr) loop
943 Store_String_Char (Char_Literal_Value (Expr));
948 Make_String_Literal (Sloc (N), End_String));
950 Analyze_And_Resolve (N, Typ);
956 -- Here if we have a real aggregate to deal with
958 Array_Aggregate : declare
959 Aggr_Resolved : Boolean;
961 Aggr_Typ : constant Entity_Id := Etype (Typ);
962 -- This is the unconstrained array type, which is the type
963 -- against which the aggregate is to be resolved. Typ itself
964 -- is the array type of the context which may not be the same
965 -- subtype as the subtype for the final aggregate.
968 -- In the following we determine whether an others choice is
969 -- allowed inside the array aggregate. The test checks the context
970 -- in which the array aggregate occurs. If the context does not
971 -- permit it, or the aggregate type is unconstrained, an others
972 -- choice is not allowed.
974 -- Note that there is no node for Explicit_Actual_Parameter.
975 -- To test for this context we therefore have to test for node
976 -- N_Parameter_Association which itself appears only if there is a
977 -- formal parameter. Consequently we also need to test for
978 -- N_Procedure_Call_Statement or N_Function_Call.
980 Set_Etype (N, Aggr_Typ); -- may be overridden later on
982 -- Ada 0Y (AI-231): Propagate the null_exclusion attribute to the
983 -- components of the array aggregate
985 if Extensions_Allowed then
986 Set_Can_Never_Be_Null (Aggr_Typ, Can_Never_Be_Null (Typ));
989 if Is_Constrained (Typ) and then
990 (Pkind = N_Assignment_Statement or else
991 Pkind = N_Parameter_Association or else
992 Pkind = N_Function_Call or else
993 Pkind = N_Procedure_Call_Statement or else
994 Pkind = N_Generic_Association or else
995 Pkind = N_Formal_Object_Declaration or else
996 Pkind = N_Return_Statement or else
997 Pkind = N_Object_Declaration or else
998 Pkind = N_Component_Declaration or else
999 Pkind = N_Parameter_Specification or else
1000 Pkind = N_Qualified_Expression or else
1001 Pkind = N_Aggregate or else
1002 Pkind = N_Extension_Aggregate or else
1003 Pkind = N_Component_Association)
1006 Resolve_Array_Aggregate
1008 Index => First_Index (Aggr_Typ),
1009 Index_Constr => First_Index (Typ),
1010 Component_Typ => Component_Type (Typ),
1011 Others_Allowed => True);
1015 Resolve_Array_Aggregate
1017 Index => First_Index (Aggr_Typ),
1018 Index_Constr => First_Index (Aggr_Typ),
1019 Component_Typ => Component_Type (Typ),
1020 Others_Allowed => False);
1023 if not Aggr_Resolved then
1024 Aggr_Subtyp := Any_Composite;
1026 Aggr_Subtyp := Array_Aggr_Subtype (N, Typ);
1029 Set_Etype (N, Aggr_Subtyp);
1030 end Array_Aggregate;
1033 Error_Msg_N ("illegal context for aggregate", N);
1037 -- If we can determine statically that the evaluation of the
1038 -- aggregate raises Constraint_Error, then replace the
1039 -- aggregate with an N_Raise_Constraint_Error node, but set the
1040 -- Etype to the right aggregate subtype. Gigi needs this.
1042 if Raises_Constraint_Error (N) then
1043 Aggr_Subtyp := Etype (N);
1045 Make_Raise_Constraint_Error (Sloc (N),
1046 Reason => CE_Range_Check_Failed));
1047 Set_Raises_Constraint_Error (N);
1048 Set_Etype (N, Aggr_Subtyp);
1051 end Resolve_Aggregate;
1053 -----------------------------
1054 -- Resolve_Array_Aggregate --
1055 -----------------------------
1057 function Resolve_Array_Aggregate
1060 Index_Constr : Node_Id;
1061 Component_Typ : Entity_Id;
1062 Others_Allowed : Boolean)
1065 Loc : constant Source_Ptr := Sloc (N);
1067 Failure : constant Boolean := False;
1068 Success : constant Boolean := True;
1070 Index_Typ : constant Entity_Id := Etype (Index);
1071 Index_Typ_Low : constant Node_Id := Type_Low_Bound (Index_Typ);
1072 Index_Typ_High : constant Node_Id := Type_High_Bound (Index_Typ);
1073 -- The type of the index corresponding to the array sub-aggregate
1074 -- along with its low and upper bounds
1076 Index_Base : constant Entity_Id := Base_Type (Index_Typ);
1077 Index_Base_Low : constant Node_Id := Type_Low_Bound (Index_Base);
1078 Index_Base_High : constant Node_Id := Type_High_Bound (Index_Base);
1079 -- ditto for the base type
1081 function Add (Val : Uint; To : Node_Id) return Node_Id;
1082 -- Creates a new expression node where Val is added to expression To.
1083 -- Tries to constant fold whenever possible. To must be an already
1084 -- analyzed expression.
1086 procedure Check_Bound (BH : Node_Id; AH : in out Node_Id);
1087 -- Checks that AH (the upper bound of an array aggregate) is <= BH
1088 -- (the upper bound of the index base type). If the check fails a
1089 -- warning is emitted, the Raises_Constraint_Error Flag of N is set,
1090 -- and AH is replaced with a duplicate of BH.
1092 procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id);
1093 -- Checks that range AL .. AH is compatible with range L .. H. Emits a
1094 -- warning if not and sets the Raises_Constraint_Error Flag in N.
1096 procedure Check_Length (L, H : Node_Id; Len : Uint);
1097 -- Checks that range L .. H contains at least Len elements. Emits a
1098 -- warning if not and sets the Raises_Constraint_Error Flag in N.
1100 function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean;
1101 -- Returns True if range L .. H is dynamic or null.
1103 procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean);
1104 -- Given expression node From, this routine sets OK to False if it
1105 -- cannot statically evaluate From. Otherwise it stores this static
1106 -- value into Value.
1108 function Resolve_Aggr_Expr
1110 Single_Elmt : Boolean)
1112 -- Resolves aggregate expression Expr. Returs False if resolution
1113 -- fails. If Single_Elmt is set to False, the expression Expr may be
1114 -- used to initialize several array aggregate elements (this can
1115 -- happen for discrete choices such as "L .. H => Expr" or the others
1116 -- choice). In this event we do not resolve Expr unless expansion is
1117 -- disabled. To know why, see the DELAYED COMPONENT RESOLUTION
1124 function Add (Val : Uint; To : Node_Id) return Node_Id is
1130 if Raises_Constraint_Error (To) then
1134 -- First test if we can do constant folding
1136 if Compile_Time_Known_Value (To)
1137 or else Nkind (To) = N_Integer_Literal
1139 Expr_Pos := Make_Integer_Literal (Loc, Expr_Value (To) + Val);
1140 Set_Is_Static_Expression (Expr_Pos);
1141 Set_Etype (Expr_Pos, Etype (To));
1142 Set_Analyzed (Expr_Pos, Analyzed (To));
1144 if not Is_Enumeration_Type (Index_Typ) then
1147 -- If we are dealing with enumeration return
1148 -- Index_Typ'Val (Expr_Pos)
1152 Make_Attribute_Reference
1154 Prefix => New_Reference_To (Index_Typ, Loc),
1155 Attribute_Name => Name_Val,
1156 Expressions => New_List (Expr_Pos));
1162 -- If we are here no constant folding possible
1164 if not Is_Enumeration_Type (Index_Base) then
1167 Left_Opnd => Duplicate_Subexpr (To),
1168 Right_Opnd => Make_Integer_Literal (Loc, Val));
1170 -- If we are dealing with enumeration return
1171 -- Index_Typ'Val (Index_Typ'Pos (To) + Val)
1175 Make_Attribute_Reference
1177 Prefix => New_Reference_To (Index_Typ, Loc),
1178 Attribute_Name => Name_Pos,
1179 Expressions => New_List (Duplicate_Subexpr (To)));
1183 Left_Opnd => To_Pos,
1184 Right_Opnd => Make_Integer_Literal (Loc, Val));
1187 Make_Attribute_Reference
1189 Prefix => New_Reference_To (Index_Typ, Loc),
1190 Attribute_Name => Name_Val,
1191 Expressions => New_List (Expr_Pos));
1201 procedure Check_Bound (BH : Node_Id; AH : in out Node_Id) is
1209 Get (Value => Val_BH, From => BH, OK => OK_BH);
1210 Get (Value => Val_AH, From => AH, OK => OK_AH);
1212 if OK_BH and then OK_AH and then Val_BH < Val_AH then
1213 Set_Raises_Constraint_Error (N);
1214 Error_Msg_N ("upper bound out of range?", AH);
1215 Error_Msg_N ("Constraint_Error will be raised at run-time?", AH);
1217 -- You need to set AH to BH or else in the case of enumerations
1218 -- indices we will not be able to resolve the aggregate bounds.
1220 AH := Duplicate_Subexpr (BH);
1228 procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id) is
1240 if Raises_Constraint_Error (N)
1241 or else Dynamic_Or_Null_Range (AL, AH)
1246 Get (Value => Val_L, From => L, OK => OK_L);
1247 Get (Value => Val_H, From => H, OK => OK_H);
1249 Get (Value => Val_AL, From => AL, OK => OK_AL);
1250 Get (Value => Val_AH, From => AH, OK => OK_AH);
1252 if OK_L and then Val_L > Val_AL then
1253 Set_Raises_Constraint_Error (N);
1254 Error_Msg_N ("lower bound of aggregate out of range?", N);
1255 Error_Msg_N ("\Constraint_Error will be raised at run-time?", N);
1258 if OK_H and then Val_H < Val_AH then
1259 Set_Raises_Constraint_Error (N);
1260 Error_Msg_N ("upper bound of aggregate out of range?", N);
1261 Error_Msg_N ("\Constraint_Error will be raised at run-time?", N);
1269 procedure Check_Length (L, H : Node_Id; Len : Uint) is
1279 if Raises_Constraint_Error (N) then
1283 Get (Value => Val_L, From => L, OK => OK_L);
1284 Get (Value => Val_H, From => H, OK => OK_H);
1286 if not OK_L or else not OK_H then
1290 -- If null range length is zero
1292 if Val_L > Val_H then
1293 Range_Len := Uint_0;
1295 Range_Len := Val_H - Val_L + 1;
1298 if Range_Len < Len then
1299 Set_Raises_Constraint_Error (N);
1300 Error_Msg_N ("Too many elements?", N);
1301 Error_Msg_N ("Constraint_Error will be raised at run-time?", N);
1305 ---------------------------
1306 -- Dynamic_Or_Null_Range --
1307 ---------------------------
1309 function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean is
1317 Get (Value => Val_L, From => L, OK => OK_L);
1318 Get (Value => Val_H, From => H, OK => OK_H);
1320 return not OK_L or else not OK_H
1321 or else not Is_OK_Static_Expression (L)
1322 or else not Is_OK_Static_Expression (H)
1323 or else Val_L > Val_H;
1324 end Dynamic_Or_Null_Range;
1330 procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean) is
1334 if Compile_Time_Known_Value (From) then
1335 Value := Expr_Value (From);
1337 -- If expression From is something like Some_Type'Val (10) then
1340 elsif Nkind (From) = N_Attribute_Reference
1341 and then Attribute_Name (From) = Name_Val
1342 and then Compile_Time_Known_Value (First (Expressions (From)))
1344 Value := Expr_Value (First (Expressions (From)));
1352 -----------------------
1353 -- Resolve_Aggr_Expr --
1354 -----------------------
1356 function Resolve_Aggr_Expr
1358 Single_Elmt : Boolean)
1361 Nxt_Ind : constant Node_Id := Next_Index (Index);
1362 Nxt_Ind_Constr : constant Node_Id := Next_Index (Index_Constr);
1363 -- Index is the current index corresponding to the expresion.
1365 Resolution_OK : Boolean := True;
1366 -- Set to False if resolution of the expression failed.
1369 -- If the array type against which we are resolving the aggregate
1370 -- has several dimensions, the expressions nested inside the
1371 -- aggregate must be further aggregates (or strings).
1373 if Present (Nxt_Ind) then
1374 if Nkind (Expr) /= N_Aggregate then
1376 -- A string literal can appear where a one-dimensional array
1377 -- of characters is expected. If the literal looks like an
1378 -- operator, it is still an operator symbol, which will be
1379 -- transformed into a string when analyzed.
1381 if Is_Character_Type (Component_Typ)
1382 and then No (Next_Index (Nxt_Ind))
1383 and then (Nkind (Expr) = N_String_Literal
1384 or else Nkind (Expr) = N_Operator_Symbol)
1386 -- A string literal used in a multidimensional array
1387 -- aggregate in place of the final one-dimensional
1388 -- aggregate must not be enclosed in parentheses.
1390 if Paren_Count (Expr) /= 0 then
1391 Error_Msg_N ("No parenthesis allowed here", Expr);
1394 Make_String_Into_Aggregate (Expr);
1397 Error_Msg_N ("nested array aggregate expected", Expr);
1402 -- Ada 0Y (AI-231): Propagate the type to the nested aggregate.
1403 -- Required to check the null-exclusion attribute (if present).
1404 -- This value may be overridden later on.
1406 Set_Etype (Expr, Etype (N));
1408 Resolution_OK := Resolve_Array_Aggregate
1409 (Expr, Nxt_Ind, Nxt_Ind_Constr, Component_Typ, Others_Allowed);
1411 -- Do not resolve the expressions of discrete or others choices
1412 -- unless the expression covers a single component, or the expander
1416 or else not Expander_Active
1417 or else In_Default_Expression
1419 Analyze_And_Resolve (Expr, Component_Typ);
1420 Check_Non_Static_Context (Expr);
1421 Aggregate_Constraint_Checks (Expr, Component_Typ);
1422 Check_Unset_Reference (Expr);
1425 if Raises_Constraint_Error (Expr)
1426 and then Nkind (Parent (Expr)) /= N_Component_Association
1428 Set_Raises_Constraint_Error (N);
1431 return Resolution_OK;
1432 end Resolve_Aggr_Expr;
1434 -- Variables local to Resolve_Array_Aggregate
1440 Who_Cares : Node_Id;
1442 Aggr_Low : Node_Id := Empty;
1443 Aggr_High : Node_Id := Empty;
1444 -- The actual low and high bounds of this sub-aggegate
1446 Choices_Low : Node_Id := Empty;
1447 Choices_High : Node_Id := Empty;
1448 -- The lowest and highest discrete choices values for a named aggregate
1450 Nb_Elements : Uint := Uint_0;
1451 -- The number of elements in a positional aggegate
1453 Others_Present : Boolean := False;
1455 Nb_Choices : Nat := 0;
1456 -- Contains the overall number of named choices in this sub-aggregate
1458 Nb_Discrete_Choices : Nat := 0;
1459 -- The overall number of discrete choices (not counting others choice)
1461 Case_Table_Size : Nat;
1462 -- Contains the size of the case table needed to sort aggregate choices
1464 -- Start of processing for Resolve_Array_Aggregate
1467 -- STEP 1: make sure the aggregate is correctly formatted
1469 if Present (Component_Associations (N)) then
1470 Assoc := First (Component_Associations (N));
1471 while Present (Assoc) loop
1472 Choice := First (Choices (Assoc));
1473 while Present (Choice) loop
1474 if Nkind (Choice) = N_Others_Choice then
1475 Others_Present := True;
1477 if Choice /= First (Choices (Assoc))
1478 or else Present (Next (Choice))
1481 ("OTHERS must appear alone in a choice list", Choice);
1485 if Present (Next (Assoc)) then
1487 ("OTHERS must appear last in an aggregate", Choice);
1492 and then Assoc /= First (Component_Associations (N))
1493 and then (Nkind (Parent (N)) = N_Assignment_Statement
1495 Nkind (Parent (N)) = N_Object_Declaration)
1498 ("(Ada 83) illegal context for OTHERS choice", N);
1502 Nb_Choices := Nb_Choices + 1;
1510 -- At this point we know that the others choice, if present, is by
1511 -- itself and appears last in the aggregate. Check if we have mixed
1512 -- positional and discrete associations (other than the others choice).
1514 if Present (Expressions (N))
1515 and then (Nb_Choices > 1
1516 or else (Nb_Choices = 1 and then not Others_Present))
1519 ("named association cannot follow positional association",
1520 First (Choices (First (Component_Associations (N)))));
1524 -- Test for the validity of an others choice if present
1526 if Others_Present and then not Others_Allowed then
1528 ("OTHERS choice not allowed here",
1529 First (Choices (First (Component_Associations (N)))));
1533 -- Protect against cascaded errors
1535 if Etype (Index_Typ) = Any_Type then
1539 -- STEP 2: Process named components
1541 if No (Expressions (N)) then
1543 if Others_Present then
1544 Case_Table_Size := Nb_Choices - 1;
1546 Case_Table_Size := Nb_Choices;
1552 -- Denote the lowest and highest values in an aggregate choice
1556 -- High end of one range and Low end of the next. Should be
1557 -- contiguous if there is no hole in the list of values.
1559 Missing_Values : Boolean;
1560 -- Set True if missing index values
1562 S_Low : Node_Id := Empty;
1563 S_High : Node_Id := Empty;
1564 -- if a choice in an aggregate is a subtype indication these
1565 -- denote the lowest and highest values of the subtype
1567 Table : Case_Table_Type (1 .. Case_Table_Size);
1568 -- Used to sort all the different choice values
1570 Single_Choice : Boolean;
1571 -- Set to true every time there is a single discrete choice in a
1572 -- discrete association
1574 Prev_Nb_Discrete_Choices : Nat;
1575 -- Used to keep track of the number of discrete choices
1576 -- in the current association.
1579 -- STEP 2 (A): Check discrete choices validity.
1581 Assoc := First (Component_Associations (N));
1582 while Present (Assoc) loop
1584 Prev_Nb_Discrete_Choices := Nb_Discrete_Choices;
1585 Choice := First (Choices (Assoc));
1589 if Nkind (Choice) = N_Others_Choice then
1590 Single_Choice := False;
1593 -- Test for subtype mark without constraint
1595 elsif Is_Entity_Name (Choice) and then
1596 Is_Type (Entity (Choice))
1598 if Base_Type (Entity (Choice)) /= Index_Base then
1600 ("invalid subtype mark in aggregate choice",
1605 elsif Nkind (Choice) = N_Subtype_Indication then
1606 Resolve_Discrete_Subtype_Indication (Choice, Index_Base);
1608 -- Does the subtype indication evaluation raise CE ?
1610 Get_Index_Bounds (Subtype_Mark (Choice), S_Low, S_High);
1611 Get_Index_Bounds (Choice, Low, High);
1612 Check_Bounds (S_Low, S_High, Low, High);
1614 else -- Choice is a range or an expression
1615 Resolve (Choice, Index_Base);
1616 Check_Unset_Reference (Choice);
1617 Check_Non_Static_Context (Choice);
1619 -- Do not range check a choice. This check is redundant
1620 -- since this test is already performed when we check
1621 -- that the bounds of the array aggregate are within
1624 Set_Do_Range_Check (Choice, False);
1627 -- If we could not resolve the discrete choice stop here
1629 if Etype (Choice) = Any_Type then
1632 -- If the discrete choice raises CE get its original bounds.
1634 elsif Nkind (Choice) = N_Raise_Constraint_Error then
1635 Set_Raises_Constraint_Error (N);
1636 Get_Index_Bounds (Original_Node (Choice), Low, High);
1638 -- Otherwise get its bounds as usual
1641 Get_Index_Bounds (Choice, Low, High);
1644 if (Dynamic_Or_Null_Range (Low, High)
1645 or else (Nkind (Choice) = N_Subtype_Indication
1647 Dynamic_Or_Null_Range (S_Low, S_High)))
1648 and then Nb_Choices /= 1
1651 ("dynamic or empty choice in aggregate " &
1652 "must be the only choice", Choice);
1656 Nb_Discrete_Choices := Nb_Discrete_Choices + 1;
1657 Table (Nb_Discrete_Choices).Choice_Lo := Low;
1658 Table (Nb_Discrete_Choices).Choice_Hi := High;
1663 -- Check if we have a single discrete choice and whether
1664 -- this discrete choice specifies a single value.
1667 (Nb_Discrete_Choices = Prev_Nb_Discrete_Choices + 1)
1668 and then (Low = High);
1676 Check_Can_Never_Be_Null (N, Expression (Assoc));
1678 -- Ada 0Y (AI-287): In case of default initialized component
1679 -- we delay the resolution to the expansion phase
1681 if Box_Present (Assoc) then
1683 -- Ada 0Y (AI-287): In case of default initialization of a
1684 -- component the expander will generate calls to the
1685 -- corresponding initialization subprogram.
1687 if Present (Base_Init_Proc (Etype (Component_Typ)))
1688 or else Has_Task (Base_Type (Component_Typ))
1693 ("(Ada 0Y): no value supplied for this component",
1697 elsif not Resolve_Aggr_Expr (Expression (Assoc),
1698 Single_Elmt => Single_Choice)
1706 -- If aggregate contains more than one choice then these must be
1707 -- static. Sort them and check that they are contiguous
1709 if Nb_Discrete_Choices > 1 then
1710 Sort_Case_Table (Table);
1711 Missing_Values := False;
1713 Outer : for J in 1 .. Nb_Discrete_Choices - 1 loop
1714 if Expr_Value (Table (J).Choice_Hi) >=
1715 Expr_Value (Table (J + 1).Choice_Lo)
1718 ("duplicate choice values in array aggregate",
1719 Table (J).Choice_Hi);
1722 elsif not Others_Present then
1724 Hi_Val := Expr_Value (Table (J).Choice_Hi);
1725 Lo_Val := Expr_Value (Table (J + 1).Choice_Lo);
1727 -- If missing values, output error messages
1729 if Lo_Val - Hi_Val > 1 then
1731 -- Header message if not first missing value
1733 if not Missing_Values then
1735 ("missing index value(s) in array aggregate", N);
1736 Missing_Values := True;
1739 -- Output values of missing indexes
1741 Lo_Val := Lo_Val - 1;
1742 Hi_Val := Hi_Val + 1;
1744 -- Enumeration type case
1746 if Is_Enumeration_Type (Index_Typ) then
1749 (Get_Enum_Lit_From_Pos
1750 (Index_Typ, Hi_Val, Loc));
1752 if Lo_Val = Hi_Val then
1753 Error_Msg_N ("\ %", N);
1757 (Get_Enum_Lit_From_Pos
1758 (Index_Typ, Lo_Val, Loc));
1759 Error_Msg_N ("\ % .. %", N);
1762 -- Integer types case
1765 Error_Msg_Uint_1 := Hi_Val;
1767 if Lo_Val = Hi_Val then
1768 Error_Msg_N ("\ ^", N);
1770 Error_Msg_Uint_2 := Lo_Val;
1771 Error_Msg_N ("\ ^ .. ^", N);
1778 if Missing_Values then
1779 Set_Etype (N, Any_Composite);
1784 -- STEP 2 (B): Compute aggregate bounds and min/max choices values
1786 if Nb_Discrete_Choices > 0 then
1787 Choices_Low := Table (1).Choice_Lo;
1788 Choices_High := Table (Nb_Discrete_Choices).Choice_Hi;
1791 if Others_Present then
1792 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
1795 Aggr_Low := Choices_Low;
1796 Aggr_High := Choices_High;
1800 -- STEP 3: Process positional components
1803 -- STEP 3 (A): Process positional elements
1805 Expr := First (Expressions (N));
1806 Nb_Elements := Uint_0;
1807 while Present (Expr) loop
1808 Nb_Elements := Nb_Elements + 1;
1810 Check_Can_Never_Be_Null (N, Expr); -- Ada 0Y (AI-231)
1812 if not Resolve_Aggr_Expr (Expr, Single_Elmt => True) then
1819 if Others_Present then
1820 Assoc := Last (Component_Associations (N));
1822 Check_Can_Never_Be_Null (N, Expression (Assoc)); -- Ada 0Y (AI-231)
1824 -- Ada 0Y (AI-287): In case of default initialized component
1825 -- we delay the resolution to the expansion phase.
1827 if Box_Present (Assoc) then
1829 -- Ada 0Y (AI-287): In case of default initialization of a
1830 -- component the expander will generate calls to the
1831 -- corresponding initialization subprogram.
1833 if Present (Base_Init_Proc (Etype (Component_Typ))) then
1837 ("(Ada 0Y): no value supplied for these components",
1841 elsif not Resolve_Aggr_Expr (Expression (Assoc),
1842 Single_Elmt => False)
1848 -- STEP 3 (B): Compute the aggregate bounds
1850 if Others_Present then
1851 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
1854 if Others_Allowed then
1855 Get_Index_Bounds (Index_Constr, Aggr_Low, Who_Cares);
1857 Aggr_Low := Index_Typ_Low;
1860 Aggr_High := Add (Nb_Elements - 1, To => Aggr_Low);
1861 Check_Bound (Index_Base_High, Aggr_High);
1865 -- STEP 4: Perform static aggregate checks and save the bounds
1869 Check_Bounds (Index_Typ_Low, Index_Typ_High, Aggr_Low, Aggr_High);
1870 Check_Bounds (Index_Base_Low, Index_Base_High, Aggr_Low, Aggr_High);
1874 if Others_Present and then Nb_Discrete_Choices > 0 then
1875 Check_Bounds (Aggr_Low, Aggr_High, Choices_Low, Choices_High);
1876 Check_Bounds (Index_Typ_Low, Index_Typ_High,
1877 Choices_Low, Choices_High);
1878 Check_Bounds (Index_Base_Low, Index_Base_High,
1879 Choices_Low, Choices_High);
1883 elsif Others_Present and then Nb_Elements > 0 then
1884 Check_Length (Aggr_Low, Aggr_High, Nb_Elements);
1885 Check_Length (Index_Typ_Low, Index_Typ_High, Nb_Elements);
1886 Check_Length (Index_Base_Low, Index_Base_High, Nb_Elements);
1890 if Raises_Constraint_Error (Aggr_Low)
1891 or else Raises_Constraint_Error (Aggr_High)
1893 Set_Raises_Constraint_Error (N);
1896 Aggr_Low := Duplicate_Subexpr (Aggr_Low);
1898 -- Do not duplicate Aggr_High if Aggr_High = Aggr_Low + Nb_Elements
1899 -- since the addition node returned by Add is not yet analyzed. Attach
1900 -- to tree and analyze first. Reset analyzed flag to insure it will get
1901 -- analyzed when it is a literal bound whose type must be properly
1904 if Others_Present or else Nb_Discrete_Choices > 0 then
1905 Aggr_High := Duplicate_Subexpr (Aggr_High);
1907 if Etype (Aggr_High) = Universal_Integer then
1908 Set_Analyzed (Aggr_High, False);
1912 Set_Aggregate_Bounds
1913 (N, Make_Range (Loc, Low_Bound => Aggr_Low, High_Bound => Aggr_High));
1915 -- The bounds may contain expressions that must be inserted upwards.
1916 -- Attach them fully to the tree. After analysis, remove side effects
1917 -- from upper bound, if still needed.
1919 Set_Parent (Aggregate_Bounds (N), N);
1920 Analyze_And_Resolve (Aggregate_Bounds (N), Index_Typ);
1921 Check_Unset_Reference (Aggregate_Bounds (N));
1923 if not Others_Present and then Nb_Discrete_Choices = 0 then
1924 Set_High_Bound (Aggregate_Bounds (N),
1925 Duplicate_Subexpr (High_Bound (Aggregate_Bounds (N))));
1929 end Resolve_Array_Aggregate;
1931 ---------------------------------
1932 -- Resolve_Extension_Aggregate --
1933 ---------------------------------
1935 -- There are two cases to consider:
1937 -- a) If the ancestor part is a type mark, the components needed are
1938 -- the difference between the components of the expected type and the
1939 -- components of the given type mark.
1941 -- b) If the ancestor part is an expression, it must be unambiguous,
1942 -- and once we have its type we can also compute the needed components
1943 -- as in the previous case. In both cases, if the ancestor type is not
1944 -- the immediate ancestor, we have to build this ancestor recursively.
1946 -- In both cases discriminants of the ancestor type do not play a
1947 -- role in the resolution of the needed components, because inherited
1948 -- discriminants cannot be used in a type extension. As a result we can
1949 -- compute independently the list of components of the ancestor type and
1950 -- of the expected type.
1952 procedure Resolve_Extension_Aggregate (N : Node_Id; Typ : Entity_Id) is
1953 A : constant Node_Id := Ancestor_Part (N);
1958 function Valid_Ancestor_Type return Boolean;
1959 -- Verify that the type of the ancestor part is a non-private ancestor
1960 -- of the expected type.
1962 -------------------------
1963 -- Valid_Ancestor_Type --
1964 -------------------------
1966 function Valid_Ancestor_Type return Boolean is
1967 Imm_Type : Entity_Id;
1970 Imm_Type := Base_Type (Typ);
1971 while Is_Derived_Type (Imm_Type)
1972 and then Etype (Imm_Type) /= Base_Type (A_Type)
1974 Imm_Type := Etype (Base_Type (Imm_Type));
1977 if Etype (Imm_Type) /= Base_Type (A_Type) then
1978 Error_Msg_NE ("expect ancestor type of &", A, Typ);
1983 end Valid_Ancestor_Type;
1985 -- Start of processing for Resolve_Extension_Aggregate
1990 if not Is_Tagged_Type (Typ) then
1991 Error_Msg_N ("type of extension aggregate must be tagged", N);
1994 elsif Is_Limited_Type (Typ) then
1996 -- Ada 0Y (AI-287): Limited aggregates are allowed
1998 if Extensions_Allowed then
2001 Error_Msg_N ("aggregate type cannot be limited", N);
2002 Explain_Limited_Type (Typ, N);
2006 elsif Is_Class_Wide_Type (Typ) then
2007 Error_Msg_N ("aggregate cannot be of a class-wide type", N);
2011 if Is_Entity_Name (A)
2012 and then Is_Type (Entity (A))
2014 A_Type := Get_Full_View (Entity (A));
2016 if Valid_Ancestor_Type then
2017 Set_Entity (A, A_Type);
2018 Set_Etype (A, A_Type);
2020 Validate_Ancestor_Part (N);
2021 Resolve_Record_Aggregate (N, Typ);
2024 elsif Nkind (A) /= N_Aggregate then
2025 if Is_Overloaded (A) then
2027 Get_First_Interp (A, I, It);
2029 while Present (It.Typ) loop
2031 if Is_Tagged_Type (It.Typ)
2032 and then not Is_Limited_Type (It.Typ)
2034 if A_Type /= Any_Type then
2035 Error_Msg_N ("cannot resolve expression", A);
2042 Get_Next_Interp (I, It);
2045 if A_Type = Any_Type then
2047 ("ancestor part must be non-limited tagged type", A);
2052 A_Type := Etype (A);
2055 if Valid_Ancestor_Type then
2056 Resolve (A, A_Type);
2057 Check_Unset_Reference (A);
2058 Check_Non_Static_Context (A);
2060 if Is_Class_Wide_Type (Etype (A))
2061 and then Nkind (Original_Node (A)) = N_Function_Call
2063 -- If the ancestor part is a dispatching call, it appears
2064 -- statically to be a legal ancestor, but it yields any
2065 -- member of the class, and it is not possible to determine
2066 -- whether it is an ancestor of the extension aggregate (much
2067 -- less which ancestor). It is not possible to determine the
2068 -- required components of the extension part.
2070 Error_Msg_N ("ancestor part must be statically tagged", A);
2072 Resolve_Record_Aggregate (N, Typ);
2077 Error_Msg_N (" No unique type for this aggregate", A);
2079 end Resolve_Extension_Aggregate;
2081 ------------------------------
2082 -- Resolve_Record_Aggregate --
2083 ------------------------------
2085 procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id) is
2086 New_Assoc_List : constant List_Id := New_List;
2087 New_Assoc : Node_Id;
2088 -- New_Assoc_List is the newly built list of N_Component_Association
2089 -- nodes. New_Assoc is one such N_Component_Association node in it.
2090 -- Please note that while Assoc and New_Assoc contain the same
2091 -- kind of nodes, they are used to iterate over two different
2092 -- N_Component_Association lists.
2094 Others_Etype : Entity_Id := Empty;
2095 -- This variable is used to save the Etype of the last record component
2096 -- that takes its value from the others choice. Its purpose is:
2098 -- (a) make sure the others choice is useful
2100 -- (b) make sure the type of all the components whose value is
2101 -- subsumed by the others choice are the same.
2103 -- This variable is updated as a side effect of function Get_Value
2105 Mbox_Present : Boolean := False;
2106 Others_Mbox : Boolean := False;
2107 -- Ada 0Y (AI-287): Variables used in case of default initialization to
2108 -- provide a functionality similar to Others_Etype. Mbox_Present
2109 -- indicates that the component takes its default initialization;
2110 -- Others_Mbox indicates that at least one component takes its default
2111 -- initialization. Similar to Others_Etype, they are also updated as a
2112 -- side effect of function Get_Value.
2114 procedure Add_Association
2115 (Component : Entity_Id;
2117 Box_Present : Boolean := False);
2118 -- Builds a new N_Component_Association node which associates
2119 -- Component to expression Expr and adds it to the new association
2120 -- list New_Assoc_List being built.
2122 function Discr_Present (Discr : Entity_Id) return Boolean;
2123 -- If aggregate N is a regular aggregate this routine will return True.
2124 -- Otherwise, if N is an extension aggregate, Discr is a discriminant
2125 -- whose value may already have been specified by N's ancestor part,
2126 -- this routine checks whether this is indeed the case and if so
2127 -- returns False, signaling that no value for Discr should appear in the
2128 -- N's aggregate part. Also, in this case, the routine appends to
2129 -- New_Assoc_List Discr the discriminant value specified in the ancestor
2135 Consider_Others_Choice : Boolean := False)
2137 -- Given a record component stored in parameter Compon, the
2138 -- following function returns its value as it appears in the list
2139 -- From, which is a list of N_Component_Association nodes. If no
2140 -- component association has a choice for the searched component,
2141 -- the value provided by the others choice is returned, if there
2142 -- is one and Consider_Others_Choice is set to true. Otherwise
2143 -- Empty is returned. If there is more than one component association
2144 -- giving a value for the searched record component, an error message
2145 -- is emitted and the first found value is returned.
2147 -- If Consider_Others_Choice is set and the returned expression comes
2148 -- from the others choice, then Others_Etype is set as a side effect.
2149 -- An error message is emitted if the components taking their value
2150 -- from the others choice do not have same type.
2152 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Node_Id);
2153 -- Analyzes and resolves expression Expr against the Etype of the
2154 -- Component. This routine also applies all appropriate checks to Expr.
2155 -- It finally saves a Expr in the newly created association list that
2156 -- will be attached to the final record aggregate. Note that if the
2157 -- Parent pointer of Expr is not set then Expr was produced with a
2158 -- New_Copy_Tree or some such.
2160 ---------------------
2161 -- Add_Association --
2162 ---------------------
2164 procedure Add_Association
2165 (Component : Entity_Id;
2167 Box_Present : Boolean := False)
2169 Choice_List : constant List_Id := New_List;
2170 New_Assoc : Node_Id;
2173 Append (New_Occurrence_Of (Component, Sloc (Expr)), Choice_List);
2175 Make_Component_Association (Sloc (Expr),
2176 Choices => Choice_List,
2178 Box_Present => Box_Present);
2179 Append (New_Assoc, New_Assoc_List);
2180 end Add_Association;
2186 function Discr_Present (Discr : Entity_Id) return Boolean is
2187 Regular_Aggr : constant Boolean := Nkind (N) /= N_Extension_Aggregate;
2192 Discr_Expr : Node_Id;
2194 Ancestor_Typ : Entity_Id;
2195 Orig_Discr : Entity_Id;
2197 D_Val : Elmt_Id := No_Elmt; -- stop junk warning
2199 Ancestor_Is_Subtyp : Boolean;
2202 if Regular_Aggr then
2206 Ancestor := Ancestor_Part (N);
2207 Ancestor_Typ := Etype (Ancestor);
2208 Loc := Sloc (Ancestor);
2210 Ancestor_Is_Subtyp :=
2211 Is_Entity_Name (Ancestor) and then Is_Type (Entity (Ancestor));
2213 -- If the ancestor part has no discriminants clearly N's aggregate
2214 -- part must provide a value for Discr.
2216 if not Has_Discriminants (Ancestor_Typ) then
2219 -- If the ancestor part is an unconstrained subtype mark then the
2220 -- Discr must be present in N's aggregate part.
2222 elsif Ancestor_Is_Subtyp
2223 and then not Is_Constrained (Entity (Ancestor))
2228 -- Now look to see if Discr was specified in the ancestor part.
2230 Orig_Discr := Original_Record_Component (Discr);
2231 D := First_Discriminant (Ancestor_Typ);
2233 if Ancestor_Is_Subtyp then
2234 D_Val := First_Elmt (Discriminant_Constraint (Entity (Ancestor)));
2237 while Present (D) loop
2238 -- If Ancestor has already specified Disc value than
2239 -- insert its value in the final aggregate.
2241 if Original_Record_Component (D) = Orig_Discr then
2242 if Ancestor_Is_Subtyp then
2243 Discr_Expr := New_Copy_Tree (Node (D_Val));
2246 Make_Selected_Component (Loc,
2247 Prefix => Duplicate_Subexpr (Ancestor),
2248 Selector_Name => New_Occurrence_Of (Discr, Loc));
2251 Resolve_Aggr_Expr (Discr_Expr, Discr);
2255 Next_Discriminant (D);
2257 if Ancestor_Is_Subtyp then
2272 Consider_Others_Choice : Boolean := False)
2276 Expr : Node_Id := Empty;
2277 Selector_Name : Node_Id;
2279 procedure Check_Non_Limited_Type;
2280 -- Relax check to allow the default initialization of limited types.
2283 -- C : Lim := (..., others => <>);
2286 ----------------------------
2287 -- Check_Non_Limited_Type --
2288 ----------------------------
2290 procedure Check_Non_Limited_Type is
2292 if Is_Limited_Type (Etype (Compon))
2293 and then Comes_From_Source (Compon)
2294 and then not In_Instance_Body
2296 -- Ada 0Y (AI-287): Limited aggregates are allowed
2298 if Extensions_Allowed
2299 and then Present (Expression (Assoc))
2300 and then Nkind (Expression (Assoc)) = N_Aggregate
2305 ("initialization not allowed for limited types", N);
2306 Explain_Limited_Type (Etype (Compon), Compon);
2310 end Check_Non_Limited_Type;
2312 -- Start of processing for Get_Value
2315 Mbox_Present := False;
2317 if Present (From) then
2318 Assoc := First (From);
2323 while Present (Assoc) loop
2324 Selector_Name := First (Choices (Assoc));
2325 while Present (Selector_Name) loop
2326 if Nkind (Selector_Name) = N_Others_Choice then
2327 if Consider_Others_Choice and then No (Expr) then
2329 -- We need to duplicate the expression for each
2330 -- successive component covered by the others choice.
2331 -- This is redundant if the others_choice covers only
2332 -- one component (small optimization possible???), but
2333 -- indispensable otherwise, because each one must be
2334 -- expanded individually to preserve side-effects.
2336 -- Ada 0Y (AI-287): In case of default initialization of
2337 -- components, we duplicate the corresponding default
2338 -- expression (from the record type declaration).
2340 if Box_Present (Assoc) then
2341 Others_Mbox := True;
2342 Mbox_Present := True;
2344 if Expander_Active then
2345 return New_Copy_Tree (Expression (Parent (Compon)));
2347 return Expression (Parent (Compon));
2351 Check_Non_Limited_Type;
2353 if Present (Others_Etype) and then
2354 Base_Type (Others_Etype) /= Base_Type (Etype
2357 Error_Msg_N ("components in OTHERS choice must " &
2358 "have same type", Selector_Name);
2361 Others_Etype := Etype (Compon);
2363 if Expander_Active then
2364 return New_Copy_Tree (Expression (Assoc));
2366 return Expression (Assoc);
2371 elsif Chars (Compon) = Chars (Selector_Name) then
2376 if Extensions_Allowed
2377 and then Present (Expression (Assoc))
2378 and then Nkind (Expression (Assoc)) = N_Null
2379 and then Can_Never_Be_Null (Compon)
2382 ("(Ada 0Y) NULL not allowed in null-excluding " &
2383 "components", Expression (Assoc));
2386 -- We need to duplicate the expression when several
2387 -- components are grouped together with a "|" choice.
2388 -- For instance "filed1 | filed2 => Expr"
2392 if Box_Present (Assoc) then
2393 Mbox_Present := True;
2395 -- Duplicate the default expression of the component
2396 -- from the record type declaration
2398 if Present (Next (Selector_Name)) then
2399 Expr := New_Copy_Tree
2400 (Expression (Parent (Compon)));
2402 Expr := Expression (Parent (Compon));
2406 Check_Non_Limited_Type;
2408 if Present (Next (Selector_Name)) then
2409 Expr := New_Copy_Tree (Expression (Assoc));
2411 Expr := Expression (Assoc);
2415 Generate_Reference (Compon, Selector_Name);
2419 ("more than one value supplied for &",
2420 Selector_Name, Compon);
2425 Next (Selector_Name);
2434 -----------------------
2435 -- Resolve_Aggr_Expr --
2436 -----------------------
2438 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Node_Id) is
2439 New_C : Entity_Id := Component;
2440 Expr_Type : Entity_Id := Empty;
2442 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean;
2443 -- If the expression is an aggregate (possibly qualified) then its
2444 -- expansion is delayed until the enclosing aggregate is expanded
2445 -- into assignments. In that case, do not generate checks on the
2446 -- expression, because they will be generated later, and will other-
2447 -- wise force a copy (to remove side-effects) that would leave a
2448 -- dynamic-sized aggregate in the code, something that gigi cannot
2452 -- Set to True if the resolved Expr node needs to be relocated
2453 -- when attached to the newly created association list. This node
2454 -- need not be relocated if its parent pointer is not set.
2455 -- In fact in this case Expr is the output of a New_Copy_Tree call.
2456 -- if Relocate is True then we have analyzed the expression node
2457 -- in the original aggregate and hence it needs to be relocated
2458 -- when moved over the new association list.
2460 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean is
2461 Kind : constant Node_Kind := Nkind (Expr);
2464 return ((Kind = N_Aggregate
2465 or else Kind = N_Extension_Aggregate)
2466 and then Present (Etype (Expr))
2467 and then Is_Record_Type (Etype (Expr))
2468 and then Expansion_Delayed (Expr))
2470 or else (Kind = N_Qualified_Expression
2471 and then Has_Expansion_Delayed (Expression (Expr)));
2472 end Has_Expansion_Delayed;
2474 -- Start of processing for Resolve_Aggr_Expr
2477 -- If the type of the component is elementary or the type of the
2478 -- aggregate does not contain discriminants, use the type of the
2479 -- component to resolve Expr.
2481 if Is_Elementary_Type (Etype (Component))
2482 or else not Has_Discriminants (Etype (N))
2484 Expr_Type := Etype (Component);
2486 -- Otherwise we have to pick up the new type of the component from
2487 -- the new costrained subtype of the aggregate. In fact components
2488 -- which are of a composite type might be constrained by a
2489 -- discriminant, and we want to resolve Expr against the subtype were
2490 -- all discriminant occurrences are replaced with their actual value.
2493 New_C := First_Component (Etype (N));
2494 while Present (New_C) loop
2495 if Chars (New_C) = Chars (Component) then
2496 Expr_Type := Etype (New_C);
2500 Next_Component (New_C);
2503 pragma Assert (Present (Expr_Type));
2505 -- For each range in an array type where a discriminant has been
2506 -- replaced with the constraint, check that this range is within
2507 -- the range of the base type. This checks is done in the
2508 -- init proc for regular objects, but has to be done here for
2509 -- aggregates since no init proc is called for them.
2511 if Is_Array_Type (Expr_Type) then
2513 Index : Node_Id := First_Index (Expr_Type);
2514 -- Range of the current constrained index in the array.
2516 Orig_Index : Node_Id := First_Index (Etype (Component));
2517 -- Range corresponding to the range Index above in the
2518 -- original unconstrained record type. The bounds of this
2519 -- range may be governed by discriminants.
2521 Unconstr_Index : Node_Id := First_Index (Etype (Expr_Type));
2522 -- Range corresponding to the range Index above for the
2523 -- unconstrained array type. This range is needed to apply
2527 while Present (Index) loop
2528 if Depends_On_Discriminant (Orig_Index) then
2529 Apply_Range_Check (Index, Etype (Unconstr_Index));
2533 Next_Index (Orig_Index);
2534 Next_Index (Unconstr_Index);
2540 -- If the Parent pointer of Expr is not set, Expr is an expression
2541 -- duplicated by New_Tree_Copy (this happens for record aggregates
2542 -- that look like (Field1 | Filed2 => Expr) or (others => Expr)).
2543 -- Such a duplicated expression must be attached to the tree
2544 -- before analysis and resolution to enforce the rule that a tree
2545 -- fragment should never be analyzed or resolved unless it is
2546 -- attached to the current compilation unit.
2548 if No (Parent (Expr)) then
2549 Set_Parent (Expr, N);
2555 Analyze_And_Resolve (Expr, Expr_Type);
2556 Check_Non_Static_Context (Expr);
2557 Check_Unset_Reference (Expr);
2559 if not Has_Expansion_Delayed (Expr) then
2560 Aggregate_Constraint_Checks (Expr, Expr_Type);
2563 if Raises_Constraint_Error (Expr) then
2564 Set_Raises_Constraint_Error (N);
2568 Add_Association (New_C, Relocate_Node (Expr));
2570 Add_Association (New_C, Expr);
2572 end Resolve_Aggr_Expr;
2574 -- Resolve_Record_Aggregate local variables
2577 -- N_Component_Association node belonging to the input aggregate N
2580 Positional_Expr : Node_Id;
2581 Component : Entity_Id;
2582 Component_Elmt : Elmt_Id;
2584 Components : constant Elist_Id := New_Elmt_List;
2585 -- Components is the list of the record components whose value must
2586 -- be provided in the aggregate. This list does include discriminants.
2588 -- Start of processing for Resolve_Record_Aggregate
2591 -- We may end up calling Duplicate_Subexpr on expressions that are
2592 -- attached to New_Assoc_List. For this reason we need to attach it
2593 -- to the tree by setting its parent pointer to N. This parent point
2594 -- will change in STEP 8 below.
2596 Set_Parent (New_Assoc_List, N);
2598 -- STEP 1: abstract type and null record verification
2600 if Is_Abstract (Typ) then
2601 Error_Msg_N ("type of aggregate cannot be abstract", N);
2604 if No (First_Entity (Typ)) and then Null_Record_Present (N) then
2608 elsif Present (First_Entity (Typ))
2609 and then Null_Record_Present (N)
2610 and then not Is_Tagged_Type (Typ)
2612 Error_Msg_N ("record aggregate cannot be null", N);
2615 elsif No (First_Entity (Typ)) then
2616 Error_Msg_N ("record aggregate must be null", N);
2620 -- STEP 2: Verify aggregate structure
2623 Selector_Name : Node_Id;
2624 Bad_Aggregate : Boolean := False;
2627 if Present (Component_Associations (N)) then
2628 Assoc := First (Component_Associations (N));
2633 while Present (Assoc) loop
2634 Selector_Name := First (Choices (Assoc));
2635 while Present (Selector_Name) loop
2636 if Nkind (Selector_Name) = N_Identifier then
2639 elsif Nkind (Selector_Name) = N_Others_Choice then
2640 if Selector_Name /= First (Choices (Assoc))
2641 or else Present (Next (Selector_Name))
2643 Error_Msg_N ("OTHERS must appear alone in a choice list",
2647 elsif Present (Next (Assoc)) then
2648 Error_Msg_N ("OTHERS must appear last in an aggregate",
2655 ("selector name should be identifier or OTHERS",
2657 Bad_Aggregate := True;
2660 Next (Selector_Name);
2666 if Bad_Aggregate then
2671 -- STEP 3: Find discriminant Values
2674 Discrim : Entity_Id;
2675 Missing_Discriminants : Boolean := False;
2678 if Present (Expressions (N)) then
2679 Positional_Expr := First (Expressions (N));
2681 Positional_Expr := Empty;
2684 if Has_Discriminants (Typ) then
2685 Discrim := First_Discriminant (Typ);
2690 -- First find the discriminant values in the positional components
2692 while Present (Discrim) and then Present (Positional_Expr) loop
2693 if Discr_Present (Discrim) then
2694 Resolve_Aggr_Expr (Positional_Expr, Discrim);
2698 if Extensions_Allowed
2699 and then Nkind (Positional_Expr) = N_Null
2700 and then Can_Never_Be_Null (Discrim)
2703 ("(Ada 0Y) NULL not allowed in null-excluding components",
2707 Next (Positional_Expr);
2710 if Present (Get_Value (Discrim, Component_Associations (N))) then
2712 ("more than one value supplied for discriminant&",
2716 Next_Discriminant (Discrim);
2719 -- Find remaining discriminant values, if any, among named components
2721 while Present (Discrim) loop
2722 Expr := Get_Value (Discrim, Component_Associations (N), True);
2724 if not Discr_Present (Discrim) then
2725 if Present (Expr) then
2727 ("more than one value supplied for discriminant&",
2731 elsif No (Expr) then
2733 ("no value supplied for discriminant &", N, Discrim);
2734 Missing_Discriminants := True;
2737 Resolve_Aggr_Expr (Expr, Discrim);
2740 Next_Discriminant (Discrim);
2743 if Missing_Discriminants then
2747 -- At this point and until the beginning of STEP 6, New_Assoc_List
2748 -- contains only the discriminants and their values.
2752 -- STEP 4: Set the Etype of the record aggregate
2754 -- ??? This code is pretty much a copy of Sem_Ch3.Build_Subtype. That
2755 -- routine should really be exported in sem_util or some such and used
2756 -- in sem_ch3 and here rather than have a copy of the code which is a
2757 -- maintenance nightmare.
2759 -- ??? Performace WARNING. The current implementation creates a new
2760 -- itype for all aggregates whose base type is discriminated.
2761 -- This means that for record aggregates nested inside an array
2762 -- aggregate we will create a new itype for each record aggregate
2763 -- if the array cmponent type has discriminants. For large aggregates
2764 -- this may be a problem. What should be done in this case is
2765 -- to reuse itypes as much as possible.
2767 if Has_Discriminants (Typ) then
2768 Build_Constrained_Itype : declare
2769 Loc : constant Source_Ptr := Sloc (N);
2771 Subtyp_Decl : Node_Id;
2774 C : constant List_Id := New_List;
2777 New_Assoc := First (New_Assoc_List);
2778 while Present (New_Assoc) loop
2779 Append (Duplicate_Subexpr (Expression (New_Assoc)), To => C);
2784 Make_Subtype_Indication (Loc,
2785 Subtype_Mark => New_Occurrence_Of (Base_Type (Typ), Loc),
2786 Constraint => Make_Index_Or_Discriminant_Constraint (Loc, C));
2788 Def_Id := Create_Itype (Ekind (Typ), N);
2791 Make_Subtype_Declaration (Loc,
2792 Defining_Identifier => Def_Id,
2793 Subtype_Indication => Indic);
2794 Set_Parent (Subtyp_Decl, Parent (N));
2796 -- Itypes must be analyzed with checks off (see itypes.ads).
2798 Analyze (Subtyp_Decl, Suppress => All_Checks);
2800 Set_Etype (N, Def_Id);
2801 Check_Static_Discriminated_Subtype
2802 (Def_Id, Expression (First (New_Assoc_List)));
2803 end Build_Constrained_Itype;
2809 -- STEP 5: Get remaining components according to discriminant values
2812 Record_Def : Node_Id;
2813 Parent_Typ : Entity_Id;
2814 Root_Typ : Entity_Id;
2815 Parent_Typ_List : Elist_Id;
2816 Parent_Elmt : Elmt_Id;
2817 Errors_Found : Boolean := False;
2821 if Is_Derived_Type (Typ) and then Is_Tagged_Type (Typ) then
2822 Parent_Typ_List := New_Elmt_List;
2824 -- If this is an extension aggregate, the component list must
2825 -- include all components that are not in the given ancestor
2826 -- type. Otherwise, the component list must include components
2827 -- of all ancestors, starting with the root.
2829 if Nkind (N) = N_Extension_Aggregate then
2830 Root_Typ := Base_Type (Etype (Ancestor_Part (N)));
2832 Root_Typ := Root_Type (Typ);
2834 if Nkind (Parent (Base_Type (Root_Typ)))
2835 = N_Private_Type_Declaration
2838 ("type of aggregate has private ancestor&!",
2840 Error_Msg_N ("must use extension aggregate!", N);
2844 Dnode := Declaration_Node (Base_Type (Root_Typ));
2846 -- If we don't get a full declaration, then we have some
2847 -- error which will get signalled later so skip this part.
2848 -- Otherwise, gather components of root that apply to the
2849 -- aggregate type. We use the base type in case there is an
2850 -- applicable stored constraint that renames the discriminants
2853 if Nkind (Dnode) = N_Full_Type_Declaration then
2854 Record_Def := Type_Definition (Dnode);
2855 Gather_Components (Base_Type (Typ),
2856 Component_List (Record_Def),
2857 Governed_By => New_Assoc_List,
2859 Report_Errors => Errors_Found);
2863 Parent_Typ := Base_Type (Typ);
2864 while Parent_Typ /= Root_Typ loop
2866 Prepend_Elmt (Parent_Typ, To => Parent_Typ_List);
2867 Parent_Typ := Etype (Parent_Typ);
2869 if Nkind (Parent (Base_Type (Parent_Typ))) =
2870 N_Private_Type_Declaration
2871 or else Nkind (Parent (Base_Type (Parent_Typ))) =
2872 N_Private_Extension_Declaration
2874 if Nkind (N) /= N_Extension_Aggregate then
2876 ("type of aggregate has private ancestor&!",
2878 Error_Msg_N ("must use extension aggregate!", N);
2881 elsif Parent_Typ /= Root_Typ then
2883 ("ancestor part of aggregate must be private type&",
2884 Ancestor_Part (N), Parent_Typ);
2890 -- Now collect components from all other ancestors.
2892 Parent_Elmt := First_Elmt (Parent_Typ_List);
2893 while Present (Parent_Elmt) loop
2894 Parent_Typ := Node (Parent_Elmt);
2895 Record_Def := Type_Definition (Parent (Base_Type (Parent_Typ)));
2896 Gather_Components (Empty,
2897 Component_List (Record_Extension_Part (Record_Def)),
2898 Governed_By => New_Assoc_List,
2900 Report_Errors => Errors_Found);
2902 Next_Elmt (Parent_Elmt);
2906 Record_Def := Type_Definition (Parent (Base_Type (Typ)));
2908 if Null_Present (Record_Def) then
2911 Gather_Components (Base_Type (Typ),
2912 Component_List (Record_Def),
2913 Governed_By => New_Assoc_List,
2915 Report_Errors => Errors_Found);
2919 if Errors_Found then
2924 -- STEP 6: Find component Values
2927 Component_Elmt := First_Elmt (Components);
2929 -- First scan the remaining positional associations in the aggregate.
2930 -- Remember that at this point Positional_Expr contains the current
2931 -- positional association if any is left after looking for discriminant
2932 -- values in step 3.
2934 while Present (Positional_Expr) and then Present (Component_Elmt) loop
2935 Component := Node (Component_Elmt);
2936 Resolve_Aggr_Expr (Positional_Expr, Component);
2939 if Extensions_Allowed
2940 and then Nkind (Positional_Expr) = N_Null
2941 and then Can_Never_Be_Null (Component)
2944 ("(Ada 0Y) NULL not allowed in null-excluding components",
2948 if Present (Get_Value (Component, Component_Associations (N))) then
2950 ("more than one value supplied for Component &", N, Component);
2953 Next (Positional_Expr);
2954 Next_Elmt (Component_Elmt);
2957 if Present (Positional_Expr) then
2959 ("too many components for record aggregate", Positional_Expr);
2962 -- Now scan for the named arguments of the aggregate
2964 while Present (Component_Elmt) loop
2965 Component := Node (Component_Elmt);
2966 Expr := Get_Value (Component, Component_Associations (N), True);
2968 if Mbox_Present and then Is_Limited_Type (Etype (Component)) then
2970 -- Ada 0Y (AI-287): In case of default initialization of a limited
2971 -- component we pass the limited component to the expander. The
2972 -- expander will generate calls to the corresponding initiali-
2973 -- zation subprograms.
2976 (Component => Component,
2978 Box_Present => True);
2980 elsif No (Expr) then
2981 Error_Msg_NE ("no value supplied for component &!", N, Component);
2983 Resolve_Aggr_Expr (Expr, Component);
2986 Next_Elmt (Component_Elmt);
2989 -- STEP 7: check for invalid components + check type in choice list
2996 -- Type of first component in choice list
2999 if Present (Component_Associations (N)) then
3000 Assoc := First (Component_Associations (N));
3005 Verification : while Present (Assoc) loop
3006 Selectr := First (Choices (Assoc));
3009 if Nkind (Selectr) = N_Others_Choice then
3011 -- Ada 0Y (AI-287): others choice may have expression or mbox
3013 if No (Others_Etype)
3014 and then not Others_Mbox
3017 ("OTHERS must represent at least one component", Selectr);
3023 while Present (Selectr) loop
3024 New_Assoc := First (New_Assoc_List);
3025 while Present (New_Assoc) loop
3026 Component := First (Choices (New_Assoc));
3027 exit when Chars (Selectr) = Chars (Component);
3031 -- If no association, this is not a legal component of
3032 -- of the type in question, except if this is an internal
3033 -- component supplied by a previous expansion.
3035 if No (New_Assoc) then
3036 if Box_Present (Parent (Selectr)) then
3039 elsif Chars (Selectr) /= Name_uTag
3040 and then Chars (Selectr) /= Name_uParent
3041 and then Chars (Selectr) /= Name_uController
3043 if not Has_Discriminants (Typ) then
3044 Error_Msg_Node_2 := Typ;
3046 ("& is not a component of}",
3050 ("& is not a component of the aggregate subtype",
3054 Check_Misspelled_Component (Components, Selectr);
3057 elsif No (Typech) then
3058 Typech := Base_Type (Etype (Component));
3060 elsif Typech /= Base_Type (Etype (Component)) then
3061 if not Box_Present (Parent (Selectr)) then
3063 ("components in choice list must have same type",
3072 end loop Verification;
3075 -- STEP 8: replace the original aggregate
3078 New_Aggregate : constant Node_Id := New_Copy (N);
3081 Set_Expressions (New_Aggregate, No_List);
3082 Set_Etype (New_Aggregate, Etype (N));
3083 Set_Component_Associations (New_Aggregate, New_Assoc_List);
3085 Rewrite (N, New_Aggregate);
3087 end Resolve_Record_Aggregate;
3089 -----------------------------
3090 -- Check_Can_Never_Be_Null --
3091 -----------------------------
3093 procedure Check_Can_Never_Be_Null (N : Node_Id; Expr : Node_Id) is
3095 if Extensions_Allowed
3096 and then Nkind (Expr) = N_Null
3097 and then Can_Never_Be_Null (Etype (N))
3100 ("(Ada 0Y) NULL not allowed in null-excluding components", Expr);
3102 end Check_Can_Never_Be_Null;
3104 ---------------------
3105 -- Sort_Case_Table --
3106 ---------------------
3108 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is
3109 L : constant Int := Case_Table'First;
3110 U : constant Int := Case_Table'Last;
3119 T := Case_Table (K + 1);
3123 and then Expr_Value (Case_Table (J - 1).Choice_Lo) >
3124 Expr_Value (T.Choice_Lo)
3126 Case_Table (J) := Case_Table (J - 1);
3130 Case_Table (J) := T;
3133 end Sort_Case_Table;