1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
11 -- Copyright (C) 1992-2002 Free Software Foundation, Inc. --
13 -- GNAT is free software; you can redistribute it and/or modify it under --
14 -- terms of the GNU General Public License as published by the Free Soft- --
15 -- ware Foundation; either version 2, or (at your option) any later ver- --
16 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
19 -- for more details. You should have received a copy of the GNU General --
20 -- Public License distributed with GNAT; see file COPYING. If not, write --
21 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
22 -- MA 02111-1307, USA. --
24 -- GNAT was originally developed by the GNAT team at New York University. --
25 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
27 ------------------------------------------------------------------------------
29 with Atree; use Atree;
30 with Checks; use Checks;
31 with Einfo; use Einfo;
32 with Elists; use Elists;
33 with Errout; use Errout;
34 with Exp_Util; use Exp_Util;
35 with Freeze; use Freeze;
36 with Itypes; use Itypes;
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 Sinfo; use Sinfo;
50 with Snames; use Snames;
51 with Stringt; use Stringt;
52 with Stand; use Stand;
53 with Tbuild; use Tbuild;
54 with Uintp; use Uintp;
56 with GNAT.Spelling_Checker; use GNAT.Spelling_Checker;
58 package body Sem_Aggr is
60 type Case_Bounds is record
63 Choice_Node : Node_Id;
66 type Case_Table_Type is array (Nat range <>) of Case_Bounds;
67 -- Table type used by Check_Case_Choices procedure
69 -----------------------
70 -- Local Subprograms --
71 -----------------------
73 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type);
74 -- Sort the Case Table using the Lower Bound of each Choice as the key.
75 -- A simple insertion sort is used since the number of choices in a case
76 -- statement of variant part will usually be small and probably in near
79 ------------------------------------------------------
80 -- Subprograms used for RECORD AGGREGATE Processing --
81 ------------------------------------------------------
83 procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id);
84 -- This procedure performs all the semantic checks required for record
85 -- aggregates. Note that for aggregates analysis and resolution go
86 -- hand in hand. Aggregate analysis has been delayed up to here and
87 -- it is done while resolving the aggregate.
89 -- N is the N_Aggregate node.
90 -- Typ is the record type for the aggregate resolution
92 -- While performing the semantic checks, this procedure
93 -- builds a new Component_Association_List where each record field
94 -- appears alone in a Component_Choice_List along with its corresponding
95 -- expression. The record fields in the Component_Association_List
96 -- appear in the same order in which they appear in the record type Typ.
98 -- Once this new Component_Association_List is built and all the
99 -- semantic checks performed, the original aggregate subtree is replaced
100 -- with the new named record aggregate just built. Note that the subtree
101 -- substitution is performed with Rewrite so as to be
102 -- able to retrieve the original aggregate.
104 -- The aggregate subtree manipulation performed by Resolve_Record_Aggregate
105 -- yields the aggregate format expected by Gigi. Typically, this kind of
106 -- tree manipulations are done in the expander. However, because the
107 -- semantic checks that need to be performed on record aggregates really
108 -- go hand in hand with the record aggreagate normalization, the aggregate
109 -- subtree transformation is performed during resolution rather than
110 -- expansion. Had we decided otherwise we would have had to duplicate
111 -- most of the code in the expansion procedure Expand_Record_Aggregate.
112 -- Note, however, that all the expansion concerning aggegates for tagged
113 -- records is done in Expand_Record_Aggregate.
115 -- The algorithm of Resolve_Record_Aggregate proceeds as follows:
117 -- 1. Make sure that the record type against which the record aggregate
118 -- has to be resolved is not abstract. Furthermore if the type is
119 -- a null aggregate make sure the input aggregate N is also null.
121 -- 2. Verify that the structure of the aggregate is that of a record
122 -- aggregate. Specifically, look for component associations and ensure
123 -- that each choice list only has identifiers or the N_Others_Choice
124 -- node. Also make sure that if present, the N_Others_Choice occurs
125 -- last and by itself.
127 -- 3. If Typ contains discriminants, the values for each discriminant
128 -- is looked for. If the record type Typ has variants, we check
129 -- that the expressions corresponding to each discriminant ruling
130 -- the (possibly nested) variant parts of Typ, are static. This
131 -- allows us to determine the variant parts to which the rest of
132 -- the aggregate must conform. The names of discriminants with their
133 -- values are saved in a new association list, New_Assoc_List which
134 -- is later augmented with the names and values of the remaining
135 -- components in the record type.
137 -- During this phase we also make sure that every discriminant is
138 -- assigned exactly one value. Note that when several values
139 -- for a given discriminant are found, semantic processing continues
140 -- looking for further errors. In this case it's the first
141 -- discriminant value found which we will be recorded.
143 -- IMPORTANT NOTE: For derived tagged types this procedure expects
144 -- First_Discriminant and Next_Discriminant to give the correct list
145 -- of discriminants, in the correct order.
147 -- 4. After all the discriminant values have been gathered, we can
148 -- set the Etype of the record aggregate. If Typ contains no
149 -- discriminants this is straightforward: the Etype of N is just
150 -- Typ, otherwise a new implicit constrained subtype of Typ is
151 -- built to be the Etype of N.
153 -- 5. Gather the remaining record components according to the discriminant
154 -- values. This involves recursively traversing the record type
155 -- structure to see what variants are selected by the given discriminant
156 -- values. This processing is a little more convoluted if Typ is a
157 -- derived tagged types since we need to retrieve the record structure
158 -- of all the ancestors of Typ.
160 -- 6. After gathering the record components we look for their values
161 -- in the record aggregate and emit appropriate error messages
162 -- should we not find such values or should they be duplicated.
164 -- 7. We then make sure no illegal component names appear in the
165 -- record aggegate and make sure that the type of the record
166 -- components appearing in a same choice list is the same.
167 -- Finally we ensure that the others choice, if present, is
168 -- used to provide the value of at least a record component.
170 -- 8. The original aggregate node is replaced with the new named
171 -- aggregate built in steps 3 through 6, as explained earlier.
173 -- Given the complexity of record aggregate resolution, the primary
174 -- goal of this routine is clarity and simplicity rather than execution
175 -- and storage efficiency. If there are only positional components in the
176 -- aggregate the running time is linear. If there are associations
177 -- the running time is still linear as long as the order of the
178 -- associations is not too far off the order of the components in the
179 -- record type. If this is not the case the running time is at worst
180 -- quadratic in the size of the association list.
182 procedure Check_Misspelled_Component
183 (Elements : Elist_Id;
184 Component : Node_Id);
185 -- Give possible misspelling diagnostic if Component is likely to be
186 -- a misspelling of one of the components of the Assoc_List.
187 -- This is called by Resolv_Aggr_Expr after producing
188 -- an invalid component error message.
190 procedure Check_Static_Discriminated_Subtype (T : Entity_Id; V : Node_Id);
191 -- An optimization: determine whether a discriminated subtype has a
192 -- static constraint, and contains array components whose length is also
193 -- static, either because they are constrained by the discriminant, or
194 -- because the original component bounds are static.
196 -----------------------------------------------------
197 -- Subprograms used for ARRAY AGGREGATE Processing --
198 -----------------------------------------------------
200 function Resolve_Array_Aggregate
203 Index_Constr : Node_Id;
204 Component_Typ : Entity_Id;
205 Others_Allowed : Boolean)
207 -- This procedure performs the semantic checks for an array aggregate.
208 -- True is returned if the aggregate resolution succeeds.
209 -- The procedure works by recursively checking each nested aggregate.
210 -- Specifically, after checking a sub-aggreate nested at the i-th level
211 -- we recursively check all the subaggregates at the i+1-st level (if any).
212 -- Note that for aggregates analysis and resolution go hand in hand.
213 -- Aggregate analysis has been delayed up to here and it is done while
214 -- resolving the aggregate.
216 -- N is the current N_Aggregate node to be checked.
218 -- Index is the index node corresponding to the array sub-aggregate that
219 -- we are currently checking (RM 4.3.3 (8)). Its Etype is the
220 -- corresponding index type (or subtype).
222 -- Index_Constr is the node giving the applicable index constraint if
223 -- any (RM 4.3.3 (10)). It "is a constraint provided by certain
224 -- contexts [...] that can be used to determine the bounds of the array
225 -- value specified by the aggregate". If Others_Allowed below is False
226 -- there is no applicable index constraint and this node is set to Index.
228 -- Component_Typ is the array component type.
230 -- Others_Allowed indicates whether an others choice is allowed
231 -- in the context where the top-level aggregate appeared.
233 -- The algorithm of Resolve_Array_Aggregate proceeds as follows:
235 -- 1. Make sure that the others choice, if present, is by itself and
236 -- appears last in the sub-aggregate. Check that we do not have
237 -- positional and named components in the array sub-aggregate (unless
238 -- the named association is an others choice). Finally if an others
239 -- choice is present, make sure it is allowed in the aggregate contex.
241 -- 2. If the array sub-aggregate contains discrete_choices:
243 -- (A) Verify their validity. Specifically verify that:
245 -- (a) If a null range is present it must be the only possible
246 -- choice in the array aggregate.
248 -- (b) Ditto for a non static range.
250 -- (c) Ditto for a non static expression.
252 -- In addition this step analyzes and resolves each discrete_choice,
253 -- making sure that its type is the type of the corresponding Index.
254 -- If we are not at the lowest array aggregate level (in the case of
255 -- multi-dimensional aggregates) then invoke Resolve_Array_Aggregate
256 -- recursively on each component expression. Otherwise, resolve the
257 -- bottom level component expressions against the expected component
258 -- type ONLY IF the component corresponds to a single discrete choice
259 -- which is not an others choice (to see why read the DELAYED
260 -- COMPONENT RESOLUTION below).
262 -- (B) Determine the bounds of the sub-aggregate and lowest and
263 -- highest choice values.
265 -- 3. For positional aggregates:
267 -- (A) Loop over the component expressions either recursively invoking
268 -- Resolve_Array_Aggregate on each of these for multi-dimensional
269 -- array aggregates or resolving the bottom level component
270 -- expressions against the expected component type.
272 -- (B) Determine the bounds of the positional sub-aggregates.
274 -- 4. Try to determine statically whether the evaluation of the array
275 -- sub-aggregate raises Constraint_Error. If yes emit proper
276 -- warnings. The precise checks are the following:
278 -- (A) Check that the index range defined by aggregate bounds is
279 -- compatible with corresponding index subtype.
280 -- We also check against the base type. In fact it could be that
281 -- Low/High bounds of the base type are static whereas those of
282 -- the index subtype are not. Thus if we can statically catch
283 -- a problem with respect to the base type we are guaranteed
284 -- that the same problem will arise with the index subtype
286 -- (B) If we are dealing with a named aggregate containing an others
287 -- choice and at least one discrete choice then make sure the range
288 -- specified by the discrete choices does not overflow the
289 -- aggregate bounds. We also check against the index type and base
290 -- type bounds for the same reasons given in (A).
292 -- (C) If we are dealing with a positional aggregate with an others
293 -- choice make sure the number of positional elements specified
294 -- does not overflow the aggregate bounds. We also check against
295 -- the index type and base type bounds as mentioned in (A).
297 -- Finally construct an N_Range node giving the sub-aggregate bounds.
298 -- Set the Aggregate_Bounds field of the sub-aggregate to be this
299 -- N_Range. The routine Array_Aggr_Subtype below uses such N_Ranges
300 -- to build the appropriate aggregate subtype. Aggregate_Bounds
301 -- information is needed during expansion.
303 -- DELAYED COMPONENT RESOLUTION: The resolution of bottom level component
304 -- expressions in an array aggregate may call Duplicate_Subexpr or some
305 -- other routine that inserts code just outside the outermost aggregate.
306 -- If the array aggregate contains discrete choices or an others choice,
307 -- this may be wrong. Consider for instance the following example.
309 -- type Rec is record
313 -- type Acc_Rec is access Rec;
314 -- Arr : array (1..3) of Acc_Rec := (1 .. 3 => new Rec);
316 -- Then the transformation of "new Rec" that occurs during resolution
317 -- entails the following code modifications
319 -- P7b : constant Acc_Rec := new Rec;
320 -- Rec_init_proc (P7b.all);
321 -- Arr : array (1..3) of Acc_Rec := (1 .. 3 => P7b);
323 -- This code transformation is clearly wrong, since we need to call
324 -- "new Rec" for each of the 3 array elements. To avoid this problem we
325 -- delay resolution of the components of non positional array aggregates
326 -- to the expansion phase. As an optimization, if the discrete choice
327 -- specifies a single value we do not delay resolution.
329 function Array_Aggr_Subtype (N : Node_Id; Typ : Node_Id) return Entity_Id;
330 -- This routine returns the type or subtype of an array aggregate.
332 -- N is the array aggregate node whose type we return.
334 -- Typ is the context type in which N occurs.
336 -- This routine creates an implicit array subtype whose bouds are
337 -- those defined by the aggregate. When this routine is invoked
338 -- Resolve_Array_Aggregate has already processed aggregate N. Thus the
339 -- Aggregate_Bounds of each sub-aggregate, is an N_Range node giving the
340 -- sub-aggregate bounds. When building the aggegate itype, this function
341 -- traverses the array aggregate N collecting such Aggregate_Bounds and
342 -- constructs the proper array aggregate itype.
344 -- Note that in the case of multidimensional aggregates each inner
345 -- sub-aggregate corresponding to a given array dimension, may provide a
346 -- different bounds. If it is possible to determine statically that
347 -- some sub-aggregates corresponding to the same index do not have the
348 -- same bounds, then a warning is emitted. If such check is not possible
349 -- statically (because some sub-aggregate bounds are dynamic expressions)
350 -- then this job is left to the expander. In all cases the particular
351 -- bounds that this function will chose for a given dimension is the first
352 -- N_Range node for a sub-aggregate corresponding to that dimension.
354 -- Note that the Raises_Constraint_Error flag of an array aggregate
355 -- whose evaluation is determined to raise CE by Resolve_Array_Aggregate,
356 -- is set in Resolve_Array_Aggregate but the aggregate is not
357 -- immediately replaced with a raise CE. In fact, Array_Aggr_Subtype must
358 -- first construct the proper itype for the aggregate (Gigi needs
359 -- this). After constructing the proper itype we will eventually replace
360 -- the top-level aggregate with a raise CE (done in Resolve_Aggregate).
361 -- Of course in cases such as:
363 -- type Arr is array (integer range <>) of Integer;
364 -- A : Arr := (positive range -1 .. 2 => 0);
366 -- The bounds of the aggregate itype are cooked up to look reasonable
367 -- (in this particular case the bounds will be 1 .. 2).
369 procedure Aggregate_Constraint_Checks
371 Check_Typ : Entity_Id);
372 -- Checks expression Exp against subtype Check_Typ. If Exp is an
373 -- aggregate and Check_Typ a constrained record type with discriminants,
374 -- we generate the appropriate discriminant checks. If Exp is an array
375 -- aggregate then emit the appropriate length checks. If Exp is a scalar
376 -- type, or a string literal, Exp is changed into Check_Typ'(Exp) to
377 -- ensure that range checks are performed at run time.
379 procedure Make_String_Into_Aggregate (N : Node_Id);
380 -- A string literal can appear in a context in which a one dimensional
381 -- array of characters is expected. This procedure simply rewrites the
382 -- string as an aggregate, prior to resolution.
384 ---------------------------------
385 -- Aggregate_Constraint_Checks --
386 ---------------------------------
388 procedure Aggregate_Constraint_Checks
390 Check_Typ : Entity_Id)
392 Exp_Typ : constant Entity_Id := Etype (Exp);
395 if Raises_Constraint_Error (Exp) then
399 -- This is really expansion activity, so make sure that expansion
400 -- is on and is allowed.
402 if not Expander_Active or else In_Default_Expression then
406 -- First check if we have to insert discriminant checks
408 if Has_Discriminants (Exp_Typ) then
409 Apply_Discriminant_Check (Exp, Check_Typ);
411 -- Next emit length checks for array aggregates
413 elsif Is_Array_Type (Exp_Typ) then
414 Apply_Length_Check (Exp, Check_Typ);
416 -- Finally emit scalar and string checks. If we are dealing with a
417 -- scalar literal we need to check by hand because the Etype of
418 -- literals is not necessarily correct.
420 elsif Is_Scalar_Type (Exp_Typ)
421 and then Compile_Time_Known_Value (Exp)
423 if Is_Out_Of_Range (Exp, Base_Type (Check_Typ)) then
424 Apply_Compile_Time_Constraint_Error
425 (Exp, "value not in range of}?", CE_Range_Check_Failed,
426 Ent => Base_Type (Check_Typ),
427 Typ => Base_Type (Check_Typ));
429 elsif Is_Out_Of_Range (Exp, Check_Typ) then
430 Apply_Compile_Time_Constraint_Error
431 (Exp, "value not in range of}?", CE_Range_Check_Failed,
435 elsif not Range_Checks_Suppressed (Check_Typ) then
436 Apply_Scalar_Range_Check (Exp, Check_Typ);
439 elsif (Is_Scalar_Type (Exp_Typ)
440 or else Nkind (Exp) = N_String_Literal)
441 and then Exp_Typ /= Check_Typ
443 if Is_Entity_Name (Exp)
444 and then Ekind (Entity (Exp)) = E_Constant
446 -- If expression is a constant, it is worthwhile checking whether
447 -- it is a bound of the type.
449 if (Is_Entity_Name (Type_Low_Bound (Check_Typ))
450 and then Entity (Exp) = Entity (Type_Low_Bound (Check_Typ)))
451 or else (Is_Entity_Name (Type_High_Bound (Check_Typ))
452 and then Entity (Exp) = Entity (Type_High_Bound (Check_Typ)))
457 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
458 Analyze_And_Resolve (Exp, Check_Typ);
461 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
462 Analyze_And_Resolve (Exp, Check_Typ);
466 end Aggregate_Constraint_Checks;
468 ------------------------
469 -- Array_Aggr_Subtype --
470 ------------------------
472 function Array_Aggr_Subtype
477 Aggr_Dimension : constant Pos := Number_Dimensions (Typ);
478 -- Number of aggregate index dimensions.
480 Aggr_Range : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
481 -- Constrained N_Range of each index dimension in our aggregate itype.
483 Aggr_Low : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
484 Aggr_High : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
485 -- Low and High bounds for each index dimension in our aggregate itype.
487 Is_Fully_Positional : Boolean := True;
489 procedure Collect_Aggr_Bounds (N : Node_Id; Dim : Pos);
490 -- N is an array (sub-)aggregate. Dim is the dimension corresponding to
491 -- (sub-)aggregate N. This procedure collects the constrained N_Range
492 -- nodes corresponding to each index dimension of our aggregate itype.
493 -- These N_Range nodes are collected in Aggr_Range above.
494 -- Likewise collect in Aggr_Low & Aggr_High above the low and high
495 -- bounds of each index dimension. If, when collecting, two bounds
496 -- corresponding to the same dimension are static and found to differ,
497 -- then emit a warning, and mark N as raising Constraint_Error.
499 -------------------------
500 -- Collect_Aggr_Bounds --
501 -------------------------
503 procedure Collect_Aggr_Bounds (N : Node_Id; Dim : Pos) is
504 This_Range : constant Node_Id := Aggregate_Bounds (N);
505 -- The aggregate range node of this specific sub-aggregate.
507 This_Low : constant Node_Id := Low_Bound (Aggregate_Bounds (N));
508 This_High : constant Node_Id := High_Bound (Aggregate_Bounds (N));
509 -- The aggregate bounds of this specific sub-aggregate.
515 -- Collect the first N_Range for a given dimension that you find.
516 -- For a given dimension they must be all equal anyway.
518 if No (Aggr_Range (Dim)) then
519 Aggr_Low (Dim) := This_Low;
520 Aggr_High (Dim) := This_High;
521 Aggr_Range (Dim) := This_Range;
524 if Compile_Time_Known_Value (This_Low) then
525 if not Compile_Time_Known_Value (Aggr_Low (Dim)) then
526 Aggr_Low (Dim) := This_Low;
528 elsif Expr_Value (This_Low) /= Expr_Value (Aggr_Low (Dim)) then
529 Set_Raises_Constraint_Error (N);
530 Error_Msg_N ("Sub-aggregate low bound mismatch?", N);
531 Error_Msg_N ("Constraint_Error will be raised at run-time?",
536 if Compile_Time_Known_Value (This_High) then
537 if not Compile_Time_Known_Value (Aggr_High (Dim)) then
538 Aggr_High (Dim) := This_High;
541 Expr_Value (This_High) /= Expr_Value (Aggr_High (Dim))
543 Set_Raises_Constraint_Error (N);
544 Error_Msg_N ("Sub-aggregate high bound mismatch?", N);
545 Error_Msg_N ("Constraint_Error will be raised at run-time?",
551 if Dim < Aggr_Dimension then
553 -- Process positional components
555 if Present (Expressions (N)) then
556 Expr := First (Expressions (N));
557 while Present (Expr) loop
558 Collect_Aggr_Bounds (Expr, Dim + 1);
563 -- Process component associations
565 if Present (Component_Associations (N)) then
566 Is_Fully_Positional := False;
568 Assoc := First (Component_Associations (N));
569 while Present (Assoc) loop
570 Expr := Expression (Assoc);
571 Collect_Aggr_Bounds (Expr, Dim + 1);
576 end Collect_Aggr_Bounds;
578 -- Array_Aggr_Subtype variables
581 -- the final itype of the overall aggregate
583 Index_Constraints : List_Id := New_List;
584 -- The list of index constraints of the aggregate itype.
586 -- Start of processing for Array_Aggr_Subtype
589 -- Make sure that the list of index constraints is properly attached
590 -- to the tree, and then collect the aggregate bounds.
592 Set_Parent (Index_Constraints, N);
593 Collect_Aggr_Bounds (N, 1);
595 -- Build the list of constrained indices of our aggregate itype.
597 for J in 1 .. Aggr_Dimension loop
598 Create_Index : declare
599 Index_Base : Entity_Id := Base_Type (Etype (Aggr_Range (J)));
600 Index_Typ : Entity_Id;
603 -- Construct the Index subtype
605 Index_Typ := Create_Itype (Subtype_Kind (Ekind (Index_Base)), N);
607 Set_Etype (Index_Typ, Index_Base);
609 if Is_Character_Type (Index_Base) then
610 Set_Is_Character_Type (Index_Typ);
613 Set_Size_Info (Index_Typ, (Index_Base));
614 Set_RM_Size (Index_Typ, RM_Size (Index_Base));
615 Set_First_Rep_Item (Index_Typ, First_Rep_Item (Index_Base));
616 Set_Scalar_Range (Index_Typ, Aggr_Range (J));
618 if Is_Discrete_Or_Fixed_Point_Type (Index_Typ) then
619 Set_RM_Size (Index_Typ, UI_From_Int (Minimum_Size (Index_Typ)));
622 Set_Etype (Aggr_Range (J), Index_Typ);
624 Append (Aggr_Range (J), To => Index_Constraints);
628 -- Now build the Itype
630 Itype := Create_Itype (E_Array_Subtype, N);
632 Set_First_Rep_Item (Itype, First_Rep_Item (Typ));
633 Set_Convention (Itype, Convention (Typ));
634 Set_Depends_On_Private (Itype, Has_Private_Component (Typ));
635 Set_Etype (Itype, Base_Type (Typ));
636 Set_Has_Alignment_Clause (Itype, Has_Alignment_Clause (Typ));
637 Set_Is_Aliased (Itype, Is_Aliased (Typ));
638 Set_Suppress_Index_Checks (Itype, Suppress_Index_Checks (Typ));
639 Set_Suppress_Length_Checks (Itype, Suppress_Length_Checks (Typ));
640 Set_Depends_On_Private (Itype, Depends_On_Private (Typ));
642 Set_First_Index (Itype, First (Index_Constraints));
643 Set_Is_Constrained (Itype, True);
644 Set_Is_Internal (Itype, True);
645 Init_Size_Align (Itype);
647 -- A simple optimization: purely positional aggregates of static
648 -- components should be passed to gigi unexpanded whenever possible,
649 -- and regardless of the staticness of the bounds themselves. Subse-
650 -- quent checks in exp_aggr verify that type is not packed, etc.
652 Set_Size_Known_At_Compile_Time (Itype,
654 and then Comes_From_Source (N)
655 and then Size_Known_At_Compile_Time (Component_Type (Typ)));
657 -- We always need a freeze node for a packed array subtype, so that
658 -- we can build the Packed_Array_Type corresponding to the subtype.
659 -- If expansion is disabled, the packed array subtype is not built,
660 -- and we must not generate a freeze node for the type, or else it
661 -- will appear incomplete to gigi.
663 if Is_Packed (Itype) and then not In_Default_Expression
664 and then Expander_Active
666 Freeze_Itype (Itype, N);
670 end Array_Aggr_Subtype;
672 --------------------------------
673 -- Check_Misspelled_Component --
674 --------------------------------
676 procedure Check_Misspelled_Component
677 (Elements : Elist_Id;
680 Max_Suggestions : constant := 2;
682 Nr_Of_Suggestions : Natural := 0;
683 Suggestion_1 : Entity_Id := Empty;
684 Suggestion_2 : Entity_Id := Empty;
685 Component_Elmt : Elmt_Id;
688 -- All the components of List are matched against Component and
689 -- a count is maintained of possible misspellings. When at the
690 -- end of the analysis there are one or two (not more!) possible
691 -- misspellings, these misspellings will be suggested as
692 -- possible correction.
694 Get_Name_String (Chars (Component));
697 S : constant String (1 .. Name_Len) :=
698 Name_Buffer (1 .. Name_Len);
702 Component_Elmt := First_Elmt (Elements);
704 while Nr_Of_Suggestions <= Max_Suggestions
705 and then Present (Component_Elmt)
708 Get_Name_String (Chars (Node (Component_Elmt)));
710 if Is_Bad_Spelling_Of (Name_Buffer (1 .. Name_Len), S) then
711 Nr_Of_Suggestions := Nr_Of_Suggestions + 1;
713 case Nr_Of_Suggestions is
714 when 1 => Suggestion_1 := Node (Component_Elmt);
715 when 2 => Suggestion_2 := Node (Component_Elmt);
720 Next_Elmt (Component_Elmt);
723 -- Report at most two suggestions
725 if Nr_Of_Suggestions = 1 then
726 Error_Msg_NE ("\possible misspelling of&",
727 Component, Suggestion_1);
729 elsif Nr_Of_Suggestions = 2 then
730 Error_Msg_Node_2 := Suggestion_2;
731 Error_Msg_NE ("\possible misspelling of& or&",
732 Component, Suggestion_1);
735 end Check_Misspelled_Component;
737 ----------------------------------------
738 -- Check_Static_Discriminated_Subtype --
739 ----------------------------------------
741 procedure Check_Static_Discriminated_Subtype (T : Entity_Id; V : Node_Id) is
742 Disc : constant Entity_Id := First_Discriminant (T);
747 if Has_Record_Rep_Clause (T) then
750 elsif Present (Next_Discriminant (Disc)) then
753 elsif Nkind (V) /= N_Integer_Literal then
757 Comp := First_Component (T);
759 while Present (Comp) loop
761 if Is_Scalar_Type (Etype (Comp)) then
764 elsif Is_Private_Type (Etype (Comp))
765 and then Present (Full_View (Etype (Comp)))
766 and then Is_Scalar_Type (Full_View (Etype (Comp)))
770 elsif Is_Array_Type (Etype (Comp)) then
772 if Is_Bit_Packed_Array (Etype (Comp)) then
776 Ind := First_Index (Etype (Comp));
778 while Present (Ind) loop
780 if Nkind (Ind) /= N_Range
781 or else Nkind (Low_Bound (Ind)) /= N_Integer_Literal
782 or else Nkind (High_Bound (Ind)) /= N_Integer_Literal
794 Next_Component (Comp);
797 -- On exit, all components have statically known sizes.
799 Set_Size_Known_At_Compile_Time (T);
800 end Check_Static_Discriminated_Subtype;
802 --------------------------------
803 -- Make_String_Into_Aggregate --
804 --------------------------------
806 procedure Make_String_Into_Aggregate (N : Node_Id) is
809 Exprs : List_Id := New_List;
810 Loc : constant Source_Ptr := Sloc (N);
812 P : Source_Ptr := Loc + 1;
813 Str : constant String_Id := Strval (N);
814 Strlen : constant Nat := String_Length (Str);
817 for J in 1 .. Strlen loop
818 C := Get_String_Char (Str, J);
819 Set_Character_Literal_Name (C);
821 C_Node := Make_Character_Literal (P, Name_Find, C);
822 Set_Etype (C_Node, Any_Character);
823 Append_To (Exprs, C_Node);
826 -- something special for wide strings ?
829 New_N := Make_Aggregate (Loc, Expressions => Exprs);
830 Set_Analyzed (New_N);
831 Set_Etype (New_N, Any_Composite);
834 end Make_String_Into_Aggregate;
836 -----------------------
837 -- Resolve_Aggregate --
838 -----------------------
840 procedure Resolve_Aggregate (N : Node_Id; Typ : Entity_Id) is
841 Pkind : constant Node_Kind := Nkind (Parent (N));
843 Aggr_Subtyp : Entity_Id;
844 -- The actual aggregate subtype. This is not necessarily the same as Typ
845 -- which is the subtype of the context in which the aggregate was found.
848 if Is_Limited_Type (Typ) then
849 Error_Msg_N ("aggregate type cannot be limited", N);
851 elsif Is_Limited_Composite (Typ) then
852 Error_Msg_N ("aggregate type cannot have limited component", N);
854 elsif Is_Class_Wide_Type (Typ) then
855 Error_Msg_N ("type of aggregate cannot be class-wide", N);
857 elsif Typ = Any_String
858 or else Typ = Any_Composite
860 Error_Msg_N ("no unique type for aggregate", N);
861 Set_Etype (N, Any_Composite);
863 elsif Is_Array_Type (Typ) and then Null_Record_Present (N) then
864 Error_Msg_N ("null record forbidden in array aggregate", N);
866 elsif Is_Record_Type (Typ) then
867 Resolve_Record_Aggregate (N, Typ);
869 elsif Is_Array_Type (Typ) then
871 -- First a special test, for the case of a positional aggregate
872 -- of characters which can be replaced by a string literal.
873 -- Do not perform this transformation if this was a string literal
874 -- to start with, whose components needed constraint checks, or if
875 -- the component type is non-static, because it will require those
876 -- checks and be transformed back into an aggregate.
878 if Number_Dimensions (Typ) = 1
880 (Root_Type (Component_Type (Typ)) = Standard_Character
882 Root_Type (Component_Type (Typ)) = Standard_Wide_Character)
883 and then No (Component_Associations (N))
884 and then not Is_Limited_Composite (Typ)
885 and then not Is_Private_Composite (Typ)
886 and then not Is_Bit_Packed_Array (Typ)
887 and then Nkind (Original_Node (Parent (N))) /= N_String_Literal
888 and then Is_Static_Subtype (Component_Type (Typ))
894 Expr := First (Expressions (N));
895 while Present (Expr) loop
896 exit when Nkind (Expr) /= N_Character_Literal;
903 Expr := First (Expressions (N));
904 while Present (Expr) loop
905 Store_String_Char (Char_Literal_Value (Expr));
910 Make_String_Literal (Sloc (N), End_String));
912 Analyze_And_Resolve (N, Typ);
918 -- Here if we have a real aggregate to deal with
920 Array_Aggregate : declare
921 Aggr_Resolved : Boolean;
922 Aggr_Typ : Entity_Id := Etype (Typ);
923 -- This is the unconstrained array type, which is the type
924 -- against which the aggregate is to be resoved. Typ itself
925 -- is the array type of the context which may not be the same
926 -- subtype as the subtype for the final aggregate.
929 -- In the following we determine whether an others choice is
930 -- allowed inside the array aggregate. The test checks the context
931 -- in which the array aggregate occurs. If the context does not
932 -- permit it, or the aggregate type is unconstrained, an others
933 -- choice is not allowed.
935 -- Note that there is no node for Explicit_Actual_Parameter.
936 -- To test for this context we therefore have to test for node
937 -- N_Parameter_Association which itself appears only if there is a
938 -- formal parameter. Consequently we also need to test for
939 -- N_Procedure_Call_Statement or N_Function_Call.
941 if Is_Constrained (Typ) and then
942 (Pkind = N_Assignment_Statement or else
943 Pkind = N_Parameter_Association or else
944 Pkind = N_Function_Call or else
945 Pkind = N_Procedure_Call_Statement or else
946 Pkind = N_Generic_Association or else
947 Pkind = N_Formal_Object_Declaration or else
948 Pkind = N_Return_Statement or else
949 Pkind = N_Object_Declaration or else
950 Pkind = N_Component_Declaration or else
951 Pkind = N_Parameter_Specification or else
952 Pkind = N_Qualified_Expression or else
953 Pkind = N_Aggregate or else
954 Pkind = N_Extension_Aggregate or else
955 Pkind = N_Component_Association)
958 Resolve_Array_Aggregate
960 Index => First_Index (Aggr_Typ),
961 Index_Constr => First_Index (Typ),
962 Component_Typ => Component_Type (Typ),
963 Others_Allowed => True);
967 Resolve_Array_Aggregate
969 Index => First_Index (Aggr_Typ),
970 Index_Constr => First_Index (Aggr_Typ),
971 Component_Typ => Component_Type (Typ),
972 Others_Allowed => False);
975 if not Aggr_Resolved then
976 Aggr_Subtyp := Any_Composite;
978 Aggr_Subtyp := Array_Aggr_Subtype (N, Typ);
981 Set_Etype (N, Aggr_Subtyp);
985 Error_Msg_N ("illegal context for aggregate", N);
989 -- If we can determine statically that the evaluation of the
990 -- aggregate raises Constraint_Error, then replace the
991 -- aggregate with an N_Raise_Constraint_Error node, but set the
992 -- Etype to the right aggregate subtype. Gigi needs this.
994 if Raises_Constraint_Error (N) then
995 Aggr_Subtyp := Etype (N);
997 Make_Raise_Constraint_Error (Sloc (N),
998 Reason => CE_Range_Check_Failed));
999 Set_Raises_Constraint_Error (N);
1000 Set_Etype (N, Aggr_Subtyp);
1004 end Resolve_Aggregate;
1006 -----------------------------
1007 -- Resolve_Array_Aggregate --
1008 -----------------------------
1010 function Resolve_Array_Aggregate
1013 Index_Constr : Node_Id;
1014 Component_Typ : Entity_Id;
1015 Others_Allowed : Boolean)
1018 Loc : constant Source_Ptr := Sloc (N);
1020 Failure : constant Boolean := False;
1021 Success : constant Boolean := True;
1023 Index_Typ : constant Entity_Id := Etype (Index);
1024 Index_Typ_Low : constant Node_Id := Type_Low_Bound (Index_Typ);
1025 Index_Typ_High : constant Node_Id := Type_High_Bound (Index_Typ);
1026 -- The type of the index corresponding to the array sub-aggregate
1027 -- along with its low and upper bounds
1029 Index_Base : constant Entity_Id := Base_Type (Index_Typ);
1030 Index_Base_Low : constant Node_Id := Type_Low_Bound (Index_Base);
1031 Index_Base_High : constant Node_Id := Type_High_Bound (Index_Base);
1032 -- ditto for the base type
1034 function Add (Val : Uint; To : Node_Id) return Node_Id;
1035 -- Creates a new expression node where Val is added to expression To.
1036 -- Tries to constant fold whenever possible. To must be an already
1037 -- analyzed expression.
1039 procedure Check_Bound (BH : Node_Id; AH : in out Node_Id);
1040 -- Checks that AH (the upper bound of an array aggregate) is <= BH
1041 -- (the upper bound of the index base type). If the check fails a
1042 -- warning is emitted, the Raises_Constraint_Error Flag of N is set,
1043 -- and AH is replaced with a duplicate of BH.
1045 procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id);
1046 -- Checks that range AL .. AH is compatible with range L .. H. Emits a
1047 -- warning if not and sets the Raises_Constraint_Error Flag in N.
1049 procedure Check_Length (L, H : Node_Id; Len : Uint);
1050 -- Checks that range L .. H contains at least Len elements. Emits a
1051 -- warning if not and sets the Raises_Constraint_Error Flag in N.
1053 function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean;
1054 -- Returns True if range L .. H is dynamic or null.
1056 procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean);
1057 -- Given expression node From, this routine sets OK to False if it
1058 -- cannot statically evaluate From. Otherwise it stores this static
1059 -- value into Value.
1061 function Resolve_Aggr_Expr
1063 Single_Elmt : Boolean)
1065 -- Resolves aggregate expression Expr. Returs False if resolution
1066 -- fails. If Single_Elmt is set to False, the expression Expr may be
1067 -- used to initialize several array aggregate elements (this can
1068 -- happen for discrete choices such as "L .. H => Expr" or the others
1069 -- choice). In this event we do not resolve Expr unless expansion is
1070 -- disabled. To know why, see the DELAYED COMPONENT RESOLUTION
1077 function Add (Val : Uint; To : Node_Id) return Node_Id is
1083 if Raises_Constraint_Error (To) then
1087 -- First test if we can do constant folding
1089 if Compile_Time_Known_Value (To)
1090 or else Nkind (To) = N_Integer_Literal
1092 Expr_Pos := Make_Integer_Literal (Loc, Expr_Value (To) + Val);
1093 Set_Is_Static_Expression (Expr_Pos);
1094 Set_Etype (Expr_Pos, Etype (To));
1095 Set_Analyzed (Expr_Pos, Analyzed (To));
1097 if not Is_Enumeration_Type (Index_Typ) then
1100 -- If we are dealing with enumeration return
1101 -- Index_Typ'Val (Expr_Pos)
1105 Make_Attribute_Reference
1107 Prefix => New_Reference_To (Index_Typ, Loc),
1108 Attribute_Name => Name_Val,
1109 Expressions => New_List (Expr_Pos));
1115 -- If we are here no constant folding possible
1117 if not Is_Enumeration_Type (Index_Base) then
1120 Left_Opnd => Duplicate_Subexpr (To),
1121 Right_Opnd => Make_Integer_Literal (Loc, Val));
1123 -- If we are dealing with enumeration return
1124 -- Index_Typ'Val (Index_Typ'Pos (To) + Val)
1128 Make_Attribute_Reference
1130 Prefix => New_Reference_To (Index_Typ, Loc),
1131 Attribute_Name => Name_Pos,
1132 Expressions => New_List (Duplicate_Subexpr (To)));
1136 Left_Opnd => To_Pos,
1137 Right_Opnd => Make_Integer_Literal (Loc, Val));
1140 Make_Attribute_Reference
1142 Prefix => New_Reference_To (Index_Typ, Loc),
1143 Attribute_Name => Name_Val,
1144 Expressions => New_List (Expr_Pos));
1154 procedure Check_Bound (BH : Node_Id; AH : in out Node_Id) is
1162 Get (Value => Val_BH, From => BH, OK => OK_BH);
1163 Get (Value => Val_AH, From => AH, OK => OK_AH);
1165 if OK_BH and then OK_AH and then Val_BH < Val_AH then
1166 Set_Raises_Constraint_Error (N);
1167 Error_Msg_N ("upper bound out of range?", AH);
1168 Error_Msg_N ("Constraint_Error will be raised at run-time?", AH);
1170 -- You need to set AH to BH or else in the case of enumerations
1171 -- indices we will not be able to resolve the aggregate bounds.
1173 AH := Duplicate_Subexpr (BH);
1181 procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id) is
1193 if Raises_Constraint_Error (N)
1194 or else Dynamic_Or_Null_Range (AL, AH)
1199 Get (Value => Val_L, From => L, OK => OK_L);
1200 Get (Value => Val_H, From => H, OK => OK_H);
1202 Get (Value => Val_AL, From => AL, OK => OK_AL);
1203 Get (Value => Val_AH, From => AH, OK => OK_AH);
1205 if OK_L and then Val_L > Val_AL then
1206 Set_Raises_Constraint_Error (N);
1207 Error_Msg_N ("lower bound of aggregate out of range?", N);
1208 Error_Msg_N ("Constraint_Error will be raised at run-time?", N);
1211 if OK_H and then Val_H < Val_AH then
1212 Set_Raises_Constraint_Error (N);
1213 Error_Msg_N ("upper bound of aggregate out of range?", N);
1214 Error_Msg_N ("Constraint_Error will be raised at run-time?", N);
1222 procedure Check_Length (L, H : Node_Id; Len : Uint) is
1232 if Raises_Constraint_Error (N) then
1236 Get (Value => Val_L, From => L, OK => OK_L);
1237 Get (Value => Val_H, From => H, OK => OK_H);
1239 if not OK_L or else not OK_H then
1243 -- If null range length is zero
1245 if Val_L > Val_H then
1246 Range_Len := Uint_0;
1248 Range_Len := Val_H - Val_L + 1;
1251 if Range_Len < Len then
1252 Set_Raises_Constraint_Error (N);
1253 Error_Msg_N ("Too many elements?", N);
1254 Error_Msg_N ("Constraint_Error will be raised at run-time?", N);
1258 ---------------------------
1259 -- Dynamic_Or_Null_Range --
1260 ---------------------------
1262 function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean is
1270 Get (Value => Val_L, From => L, OK => OK_L);
1271 Get (Value => Val_H, From => H, OK => OK_H);
1273 return not OK_L or else not OK_H
1274 or else not Is_OK_Static_Expression (L)
1275 or else not Is_OK_Static_Expression (H)
1276 or else Val_L > Val_H;
1277 end Dynamic_Or_Null_Range;
1283 procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean) is
1287 if Compile_Time_Known_Value (From) then
1288 Value := Expr_Value (From);
1290 -- If expression From is something like Some_Type'Val (10) then
1293 elsif Nkind (From) = N_Attribute_Reference
1294 and then Attribute_Name (From) = Name_Val
1295 and then Compile_Time_Known_Value (First (Expressions (From)))
1297 Value := Expr_Value (First (Expressions (From)));
1305 -----------------------
1306 -- Resolve_Aggr_Expr --
1307 -----------------------
1309 function Resolve_Aggr_Expr
1311 Single_Elmt : Boolean)
1314 Nxt_Ind : Node_Id := Next_Index (Index);
1315 Nxt_Ind_Constr : Node_Id := Next_Index (Index_Constr);
1316 -- Index is the current index corresponding to the expression.
1318 Resolution_OK : Boolean := True;
1319 -- Set to False if resolution of the expression failed.
1322 -- If the array type against which we are resolving the aggregate
1323 -- has several dimensions, the expressions nested inside the
1324 -- aggregate must be further aggregates (or strings).
1326 if Present (Nxt_Ind) then
1327 if Nkind (Expr) /= N_Aggregate then
1329 -- A string literal can appear where a one-dimensional array
1330 -- of characters is expected. If the literal looks like an
1331 -- operator, it is still an operator symbol, which will be
1332 -- transformed into a string when analyzed.
1334 if Is_Character_Type (Component_Typ)
1335 and then No (Next_Index (Nxt_Ind))
1336 and then (Nkind (Expr) = N_String_Literal
1337 or else Nkind (Expr) = N_Operator_Symbol)
1339 -- A string literal used in a multidimensional array
1340 -- aggregate in place of the final one-dimensional
1341 -- aggregate must not be enclosed in parentheses.
1343 if Paren_Count (Expr) /= 0 then
1344 Error_Msg_N ("No parenthesis allowed here", Expr);
1347 Make_String_Into_Aggregate (Expr);
1350 Error_Msg_N ("nested array aggregate expected", Expr);
1355 Resolution_OK := Resolve_Array_Aggregate
1356 (Expr, Nxt_Ind, Nxt_Ind_Constr, Component_Typ, Others_Allowed);
1358 -- Do not resolve the expressions of discrete or others choices
1359 -- unless the expression covers a single component, or the expander
1363 or else not Expander_Active
1364 or else In_Default_Expression
1366 Analyze_And_Resolve (Expr, Component_Typ);
1367 Check_Non_Static_Context (Expr);
1368 Aggregate_Constraint_Checks (Expr, Component_Typ);
1371 if Raises_Constraint_Error (Expr)
1372 and then Nkind (Parent (Expr)) /= N_Component_Association
1374 Set_Raises_Constraint_Error (N);
1377 return Resolution_OK;
1378 end Resolve_Aggr_Expr;
1380 -- Variables local to Resolve_Array_Aggregate
1386 Who_Cares : Node_Id;
1388 Aggr_Low : Node_Id := Empty;
1389 Aggr_High : Node_Id := Empty;
1390 -- The actual low and high bounds of this sub-aggegate
1392 Choices_Low : Node_Id := Empty;
1393 Choices_High : Node_Id := Empty;
1394 -- The lowest and highest discrete choices values for a named aggregate
1396 Nb_Elements : Uint := Uint_0;
1397 -- The number of elements in a positional aggegate
1399 Others_Present : Boolean := False;
1401 Nb_Choices : Nat := 0;
1402 -- Contains the overall number of named choices in this sub-aggregate
1404 Nb_Discrete_Choices : Nat := 0;
1405 -- The overall number of discrete choices (not counting others choice)
1407 Case_Table_Size : Nat;
1408 -- Contains the size of the case table needed to sort aggregate choices
1410 -- Start of processing for Resolve_Array_Aggregate
1413 -- STEP 1: make sure the aggregate is correctly formatted
1415 if Present (Component_Associations (N)) then
1416 Assoc := First (Component_Associations (N));
1417 while Present (Assoc) loop
1418 Choice := First (Choices (Assoc));
1419 while Present (Choice) loop
1420 if Nkind (Choice) = N_Others_Choice then
1421 Others_Present := True;
1423 if Choice /= First (Choices (Assoc))
1424 or else Present (Next (Choice))
1427 ("OTHERS must appear alone in a choice list", Choice);
1431 if Present (Next (Assoc)) then
1433 ("OTHERS must appear last in an aggregate", Choice);
1438 and then Assoc /= First (Component_Associations (N))
1439 and then (Nkind (Parent (N)) = N_Assignment_Statement
1441 Nkind (Parent (N)) = N_Object_Declaration)
1444 ("(Ada 83) illegal context for OTHERS choice", N);
1448 Nb_Choices := Nb_Choices + 1;
1456 -- At this point we know that the others choice, if present, is by
1457 -- itself and appears last in the aggregate. Check if we have mixed
1458 -- positional and discrete associations (other than the others choice).
1460 if Present (Expressions (N))
1461 and then (Nb_Choices > 1
1462 or else (Nb_Choices = 1 and then not Others_Present))
1465 ("named association cannot follow positional association",
1466 First (Choices (First (Component_Associations (N)))));
1470 -- Test for the validity of an others choice if present
1472 if Others_Present and then not Others_Allowed then
1474 ("OTHERS choice not allowed here",
1475 First (Choices (First (Component_Associations (N)))));
1479 -- Protect against cascaded errors
1481 if Etype (Index_Typ) = Any_Type then
1485 -- STEP 2: Process named components
1487 if No (Expressions (N)) then
1489 if Others_Present then
1490 Case_Table_Size := Nb_Choices - 1;
1492 Case_Table_Size := Nb_Choices;
1498 -- Denote the lowest and highest values in an aggregate choice
1502 -- High end of one range and Low end of the next. Should be
1503 -- contiguous if there is no hole in the list of values.
1505 Missing_Values : Boolean;
1506 -- Set True if missing index values
1508 S_Low : Node_Id := Empty;
1509 S_High : Node_Id := Empty;
1510 -- if a choice in an aggregate is a subtype indication these
1511 -- denote the lowest and highest values of the subtype
1513 Table : Case_Table_Type (1 .. Case_Table_Size);
1514 -- Used to sort all the different choice values
1516 Single_Choice : Boolean;
1517 -- Set to true every time there is a single discrete choice in a
1518 -- discrete association
1520 Prev_Nb_Discrete_Choices : Nat;
1521 -- Used to keep track of the number of discrete choices
1522 -- in the current association.
1525 -- STEP 2 (A): Check discrete choices validity.
1527 Assoc := First (Component_Associations (N));
1528 while Present (Assoc) loop
1530 Prev_Nb_Discrete_Choices := Nb_Discrete_Choices;
1531 Choice := First (Choices (Assoc));
1535 if Nkind (Choice) = N_Others_Choice then
1536 Single_Choice := False;
1539 -- Test for subtype mark without constraint
1541 elsif Is_Entity_Name (Choice) and then
1542 Is_Type (Entity (Choice))
1544 if Base_Type (Entity (Choice)) /= Index_Base then
1546 ("invalid subtype mark in aggregate choice",
1551 elsif Nkind (Choice) = N_Subtype_Indication then
1552 Resolve_Discrete_Subtype_Indication (Choice, Index_Base);
1554 -- Does the subtype indication evaluation raise CE ?
1556 Get_Index_Bounds (Subtype_Mark (Choice), S_Low, S_High);
1557 Get_Index_Bounds (Choice, Low, High);
1558 Check_Bounds (S_Low, S_High, Low, High);
1560 else -- Choice is a range or an expression
1561 Resolve (Choice, Index_Base);
1562 Check_Non_Static_Context (Choice);
1564 -- Do not range check a choice. This check is redundant
1565 -- since this test is already performed when we check
1566 -- that the bounds of the array aggregate are within
1569 Set_Do_Range_Check (Choice, False);
1572 -- If we could not resolve the discrete choice stop here
1574 if Etype (Choice) = Any_Type then
1577 -- If the discrete choice raises CE get its original bounds.
1579 elsif Nkind (Choice) = N_Raise_Constraint_Error then
1580 Set_Raises_Constraint_Error (N);
1581 Get_Index_Bounds (Original_Node (Choice), Low, High);
1583 -- Otherwise get its bounds as usual
1586 Get_Index_Bounds (Choice, Low, High);
1589 if (Dynamic_Or_Null_Range (Low, High)
1590 or else (Nkind (Choice) = N_Subtype_Indication
1592 Dynamic_Or_Null_Range (S_Low, S_High)))
1593 and then Nb_Choices /= 1
1596 ("dynamic or empty choice in aggregate " &
1597 "must be the only choice", Choice);
1601 Nb_Discrete_Choices := Nb_Discrete_Choices + 1;
1602 Table (Nb_Discrete_Choices).Choice_Lo := Low;
1603 Table (Nb_Discrete_Choices).Choice_Hi := High;
1608 -- Check if we have a single discrete choice and whether
1609 -- this discrete choice specifies a single value.
1612 (Nb_Discrete_Choices = Prev_Nb_Discrete_Choices + 1)
1613 and then (Low = High);
1621 (Expression (Assoc), Single_Elmt => Single_Choice)
1629 -- If aggregate contains more than one choice then these must be
1630 -- static. Sort them and check that they are contiguous
1632 if Nb_Discrete_Choices > 1 then
1633 Sort_Case_Table (Table);
1634 Missing_Values := False;
1636 Outer : for J in 1 .. Nb_Discrete_Choices - 1 loop
1637 if Expr_Value (Table (J).Choice_Hi) >=
1638 Expr_Value (Table (J + 1).Choice_Lo)
1641 ("duplicate choice values in array aggregate",
1642 Table (J).Choice_Hi);
1645 elsif not Others_Present then
1647 Hi_Val := Expr_Value (Table (J).Choice_Hi);
1648 Lo_Val := Expr_Value (Table (J + 1).Choice_Lo);
1650 -- If missing values, output error messages
1652 if Lo_Val - Hi_Val > 1 then
1654 -- Header message if not first missing value
1656 if not Missing_Values then
1658 ("missing index value(s) in array aggregate", N);
1659 Missing_Values := True;
1662 -- Output values of missing indexes
1664 Lo_Val := Lo_Val - 1;
1665 Hi_Val := Hi_Val + 1;
1667 -- Enumeration type case
1669 if Is_Enumeration_Type (Index_Typ) then
1672 (Get_Enum_Lit_From_Pos
1673 (Index_Typ, Hi_Val, Loc));
1675 if Lo_Val = Hi_Val then
1676 Error_Msg_N ("\ %", N);
1680 (Get_Enum_Lit_From_Pos
1681 (Index_Typ, Lo_Val, Loc));
1682 Error_Msg_N ("\ % .. %", N);
1685 -- Integer types case
1688 Error_Msg_Uint_1 := Hi_Val;
1690 if Lo_Val = Hi_Val then
1691 Error_Msg_N ("\ ^", N);
1693 Error_Msg_Uint_2 := Lo_Val;
1694 Error_Msg_N ("\ ^ .. ^", N);
1701 if Missing_Values then
1702 Set_Etype (N, Any_Composite);
1707 -- STEP 2 (B): Compute aggregate bounds and min/max choices values
1709 if Nb_Discrete_Choices > 0 then
1710 Choices_Low := Table (1).Choice_Lo;
1711 Choices_High := Table (Nb_Discrete_Choices).Choice_Hi;
1714 if Others_Present then
1715 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
1718 Aggr_Low := Choices_Low;
1719 Aggr_High := Choices_High;
1723 -- STEP 3: Process positional components
1726 -- STEP 3 (A): Process positional elements
1728 Expr := First (Expressions (N));
1729 Nb_Elements := Uint_0;
1730 while Present (Expr) loop
1731 Nb_Elements := Nb_Elements + 1;
1733 if not Resolve_Aggr_Expr (Expr, Single_Elmt => True) then
1740 if Others_Present then
1741 Assoc := Last (Component_Associations (N));
1742 if not Resolve_Aggr_Expr (Expression (Assoc),
1743 Single_Elmt => False)
1749 -- STEP 3 (B): Compute the aggregate bounds
1751 if Others_Present then
1752 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
1755 if Others_Allowed then
1756 Get_Index_Bounds (Index_Constr, Aggr_Low, Who_Cares);
1758 Aggr_Low := Index_Typ_Low;
1761 Aggr_High := Add (Nb_Elements - 1, To => Aggr_Low);
1762 Check_Bound (Index_Base_High, Aggr_High);
1766 -- STEP 4: Perform static aggregate checks and save the bounds
1770 Check_Bounds (Index_Typ_Low, Index_Typ_High, Aggr_Low, Aggr_High);
1771 Check_Bounds (Index_Base_Low, Index_Base_High, Aggr_Low, Aggr_High);
1775 if Others_Present and then Nb_Discrete_Choices > 0 then
1776 Check_Bounds (Aggr_Low, Aggr_High, Choices_Low, Choices_High);
1777 Check_Bounds (Index_Typ_Low, Index_Typ_High,
1778 Choices_Low, Choices_High);
1779 Check_Bounds (Index_Base_Low, Index_Base_High,
1780 Choices_Low, Choices_High);
1784 elsif Others_Present and then Nb_Elements > 0 then
1785 Check_Length (Aggr_Low, Aggr_High, Nb_Elements);
1786 Check_Length (Index_Typ_Low, Index_Typ_High, Nb_Elements);
1787 Check_Length (Index_Base_Low, Index_Base_High, Nb_Elements);
1791 if Raises_Constraint_Error (Aggr_Low)
1792 or else Raises_Constraint_Error (Aggr_High)
1794 Set_Raises_Constraint_Error (N);
1797 Aggr_Low := Duplicate_Subexpr (Aggr_Low);
1799 -- Do not duplicate Aggr_High if Aggr_High = Aggr_Low + Nb_Elements
1800 -- since the addition node returned by Add is not yet analyzed. Attach
1801 -- to tree and analyze first. Reset analyzed flag to insure it will get
1802 -- analyzed when it is a literal bound whose type must be properly
1805 if Others_Present or else Nb_Discrete_Choices > 0 then
1806 Aggr_High := Duplicate_Subexpr (Aggr_High);
1808 if Etype (Aggr_High) = Universal_Integer then
1809 Set_Analyzed (Aggr_High, False);
1813 Set_Aggregate_Bounds
1814 (N, Make_Range (Loc, Low_Bound => Aggr_Low, High_Bound => Aggr_High));
1816 -- The bounds may contain expressions that must be inserted upwards.
1817 -- Attach them fully to the tree. After analysis, remove side effects
1818 -- from upper bound, if still needed.
1820 Set_Parent (Aggregate_Bounds (N), N);
1821 Analyze_And_Resolve (Aggregate_Bounds (N), Index_Typ);
1823 if not Others_Present and then Nb_Discrete_Choices = 0 then
1824 Set_High_Bound (Aggregate_Bounds (N),
1825 Duplicate_Subexpr (High_Bound (Aggregate_Bounds (N))));
1829 end Resolve_Array_Aggregate;
1831 ---------------------------------
1832 -- Resolve_Extension_Aggregate --
1833 ---------------------------------
1835 -- There are two cases to consider:
1837 -- a) If the ancestor part is a type mark, the components needed are
1838 -- the difference between the components of the expected type and the
1839 -- components of the given type mark.
1841 -- b) If the ancestor part is an expression, it must be unambiguous,
1842 -- and once we have its type we can also compute the needed components
1843 -- as in the previous case. In both cases, if the ancestor type is not
1844 -- the immediate ancestor, we have to build this ancestor recursively.
1846 -- In both cases discriminants of the ancestor type do not play a
1847 -- role in the resolution of the needed components, because inherited
1848 -- discriminants cannot be used in a type extension. As a result we can
1849 -- compute independently the list of components of the ancestor type and
1850 -- of the expected type.
1852 procedure Resolve_Extension_Aggregate (N : Node_Id; Typ : Entity_Id) is
1853 A : constant Node_Id := Ancestor_Part (N);
1857 Imm_Type : Entity_Id;
1859 function Valid_Ancestor_Type return Boolean;
1860 -- Verify that the type of the ancestor part is a non-private ancestor
1861 -- of the expected type.
1863 function Valid_Ancestor_Type return Boolean is
1864 Imm_Type : Entity_Id;
1867 Imm_Type := Base_Type (Typ);
1868 while Is_Derived_Type (Imm_Type)
1869 and then Etype (Imm_Type) /= Base_Type (A_Type)
1871 Imm_Type := Etype (Base_Type (Imm_Type));
1874 if Etype (Imm_Type) /= Base_Type (A_Type) then
1875 Error_Msg_NE ("expect ancestor type of &", A, Typ);
1880 end Valid_Ancestor_Type;
1882 -- Start of processing for Resolve_Extension_Aggregate
1887 if not Is_Tagged_Type (Typ) then
1888 Error_Msg_N ("type of extension aggregate must be tagged", N);
1891 elsif Is_Limited_Type (Typ) then
1892 Error_Msg_N ("aggregate type cannot be limited", N);
1895 elsif Is_Class_Wide_Type (Typ) then
1896 Error_Msg_N ("aggregate cannot be of a class-wide type", N);
1900 if Is_Entity_Name (A)
1901 and then Is_Type (Entity (A))
1903 A_Type := Get_Full_View (Entity (A));
1904 Imm_Type := Base_Type (Typ);
1906 if Valid_Ancestor_Type then
1907 Set_Entity (A, A_Type);
1908 Set_Etype (A, A_Type);
1910 Validate_Ancestor_Part (N);
1911 Resolve_Record_Aggregate (N, Typ);
1914 elsif Nkind (A) /= N_Aggregate then
1915 if Is_Overloaded (A) then
1917 Get_First_Interp (A, I, It);
1919 while Present (It.Typ) loop
1921 if Is_Tagged_Type (It.Typ)
1922 and then not Is_Limited_Type (It.Typ)
1924 if A_Type /= Any_Type then
1925 Error_Msg_N ("cannot resolve expression", A);
1932 Get_Next_Interp (I, It);
1935 if A_Type = Any_Type then
1937 ("ancestor part must be non-limited tagged type", A);
1942 A_Type := Etype (A);
1945 if Valid_Ancestor_Type then
1946 Resolve (A, A_Type);
1947 Check_Non_Static_Context (A);
1948 Resolve_Record_Aggregate (N, Typ);
1952 Error_Msg_N (" No unique type for this aggregate", A);
1955 end Resolve_Extension_Aggregate;
1957 ------------------------------
1958 -- Resolve_Record_Aggregate --
1959 ------------------------------
1961 procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id) is
1962 Regular_Aggr : constant Boolean := Nkind (N) /= N_Extension_Aggregate;
1964 New_Assoc_List : List_Id := New_List;
1965 New_Assoc : Node_Id;
1966 -- New_Assoc_List is the newly built list of N_Component_Association
1967 -- nodes. New_Assoc is one such N_Component_Association node in it.
1968 -- Please note that while Assoc and New_Assoc contain the same
1969 -- kind of nodes, they are used to iterate over two different
1970 -- N_Component_Association lists.
1972 Others_Etype : Entity_Id := Empty;
1973 -- This variable is used to save the Etype of the last record component
1974 -- that takes its value from the others choice. Its purpose is:
1976 -- (a) make sure the others choice is useful
1978 -- (b) make sure the type of all the components whose value is
1979 -- subsumed by the others choice are the same.
1981 -- This variable is updated as a side effect of function Get_Value
1983 procedure Add_Association (Component : Entity_Id; Expr : Node_Id);
1984 -- Builds a new N_Component_Association node which associates
1985 -- Component to expression Expr and adds it to the new association
1986 -- list New_Assoc_List being built.
1988 function Discr_Present (Discr : Entity_Id) return Boolean;
1989 -- If aggregate N is a regular aggregate this routine will return True.
1990 -- Otherwise, if N is an extension aggreagte, Discr is a discriminant
1991 -- whose value may already have been specified by N's ancestor part,
1992 -- this routine checks whether this is indeed the case and if so
1993 -- returns False, signaling that no value for Discr should appear in the
1994 -- N's aggregate part. Also, in this case, the routine appends to
1995 -- New_Assoc_List Discr the discriminant value specified in the ancestor
2001 Consider_Others_Choice : Boolean := False)
2003 -- Given a record component stored in parameter Compon, the
2004 -- following function returns its value as it appears in the list
2005 -- From, which is a list of N_Component_Association nodes. If no
2006 -- component association has a choice for the searched component,
2007 -- the value provided by the others choice is returned, if there
2008 -- is one and Consider_Others_Choice is set to true. Otherwise
2009 -- Empty is returned. If there is more than one component association
2010 -- giving a value for the searched record component, an error message
2011 -- is emitted and the first found value is returned.
2013 -- If Consider_Others_Choice is set and the returned expression comes
2014 -- from the others choice, then Others_Etype is set as a side effect.
2015 -- An error message is emitted if the components taking their value
2016 -- from the others choice do not have same type.
2018 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Node_Id);
2019 -- Analyzes and resolves expression Expr against the Etype of the
2020 -- Component. This routine also applies all appropriate checks to Expr.
2021 -- It finally saves a Expr in the newly created association list that
2022 -- will be attached to the final record aggregate. Note that if the
2023 -- Parent pointer of Expr is not set then Expr was produced with a
2024 -- New_copy_Tree or some such.
2026 ---------------------
2027 -- Add_Association --
2028 ---------------------
2030 procedure Add_Association (Component : Entity_Id; Expr : Node_Id) is
2031 New_Assoc : Node_Id;
2032 Choice_List : List_Id := New_List;
2035 Append (New_Occurrence_Of (Component, Sloc (Expr)), Choice_List);
2037 Make_Component_Association (Sloc (Expr),
2038 Choices => Choice_List,
2039 Expression => Expr);
2040 Append (New_Assoc, New_Assoc_List);
2041 end Add_Association;
2047 function Discr_Present (Discr : Entity_Id) return Boolean is
2051 Discr_Expr : Node_Id;
2053 Ancestor_Typ : Entity_Id;
2054 Orig_Discr : Entity_Id;
2056 D_Val : Elmt_Id := No_Elmt; -- stop junk warning
2058 Ancestor_Is_Subtyp : Boolean;
2061 if Regular_Aggr then
2065 Ancestor := Ancestor_Part (N);
2066 Ancestor_Typ := Etype (Ancestor);
2067 Loc := Sloc (Ancestor);
2069 Ancestor_Is_Subtyp :=
2070 Is_Entity_Name (Ancestor) and then Is_Type (Entity (Ancestor));
2072 -- If the ancestor part has no discriminants clearly N's aggregate
2073 -- part must provide a value for Discr.
2075 if not Has_Discriminants (Ancestor_Typ) then
2078 -- If the ancestor part is an unconstrained subtype mark then the
2079 -- Discr must be present in N's aggregate part.
2081 elsif Ancestor_Is_Subtyp
2082 and then not Is_Constrained (Entity (Ancestor))
2087 -- Now look to see if Discr was specified in the ancestor part.
2089 Orig_Discr := Original_Record_Component (Discr);
2090 D := First_Discriminant (Ancestor_Typ);
2092 if Ancestor_Is_Subtyp then
2093 D_Val := First_Elmt (Discriminant_Constraint (Entity (Ancestor)));
2096 while Present (D) loop
2097 -- If Ancestor has already specified Disc value than
2098 -- insert its value in the final aggregate.
2100 if Original_Record_Component (D) = Orig_Discr then
2101 if Ancestor_Is_Subtyp then
2102 Discr_Expr := New_Copy_Tree (Node (D_Val));
2105 Make_Selected_Component (Loc,
2106 Prefix => Duplicate_Subexpr (Ancestor),
2107 Selector_Name => New_Occurrence_Of (Discr, Loc));
2110 Resolve_Aggr_Expr (Discr_Expr, Discr);
2114 Next_Discriminant (D);
2116 if Ancestor_Is_Subtyp then
2131 Consider_Others_Choice : Boolean := False)
2135 Expr : Node_Id := Empty;
2136 Selector_Name : Node_Id;
2139 if Present (From) then
2140 Assoc := First (From);
2145 while Present (Assoc) loop
2146 Selector_Name := First (Choices (Assoc));
2147 while Present (Selector_Name) loop
2148 if Nkind (Selector_Name) = N_Others_Choice then
2149 if Consider_Others_Choice and then No (Expr) then
2150 if Present (Others_Etype) and then
2151 Base_Type (Others_Etype) /= Base_Type (Etype (Compon))
2153 Error_Msg_N ("components in OTHERS choice must " &
2154 "have same type", Selector_Name);
2157 Others_Etype := Etype (Compon);
2159 -- We need to duplicate the expression for each
2160 -- successive component covered by the others choice.
2161 -- If the expression is itself an array aggregate with
2162 -- "others", its subtype must be obtained from the
2163 -- current component, and therefore it must be (at least
2164 -- partly) reanalyzed.
2166 if Analyzed (Expression (Assoc)) then
2167 Expr := New_Copy_Tree (Expression (Assoc));
2169 if Nkind (Expr) = N_Aggregate
2170 and then Is_Array_Type (Etype (Expr))
2171 and then No (Expressions (Expr))
2173 Nkind (First (Choices
2174 (First (Component_Associations (Expr)))))
2177 Set_Analyzed (Expr, False);
2183 return Expression (Assoc);
2187 elsif Chars (Compon) = Chars (Selector_Name) then
2189 -- We need to duplicate the expression when several
2190 -- components are grouped together with a "|" choice.
2191 -- For instance "filed1 | filed2 => Expr"
2193 if Present (Next (Selector_Name)) then
2194 Expr := New_Copy_Tree (Expression (Assoc));
2196 Expr := Expression (Assoc);
2201 ("more than one value supplied for &",
2202 Selector_Name, Compon);
2207 Next (Selector_Name);
2216 -----------------------
2217 -- Resolve_Aggr_Expr --
2218 -----------------------
2220 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Node_Id) is
2221 New_C : Entity_Id := Component;
2222 Expr_Type : Entity_Id := Empty;
2224 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean;
2225 -- If the expression is an aggregate (possibly qualified) then its
2226 -- expansion is delayed until the enclosing aggregate is expanded
2227 -- into assignments. In that case, do not generate checks on the
2228 -- expression, because they will be generated later, and will other-
2229 -- wise force a copy (to remove side-effects) that would leave a
2230 -- dynamic-sized aggregate in the code, something that gigi cannot
2234 -- Set to True if the resolved Expr node needs to be relocated
2235 -- when attached to the newly created association list. This node
2236 -- need not be relocated if its parent pointer is not set.
2237 -- In fact in this case Expr is the output of a New_Copy_Tree call.
2238 -- if Relocate is True then we have analyzed the expression node
2239 -- in the original aggregate and hence it needs to be relocated
2240 -- when moved over the new association list.
2242 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean is
2243 Kind : constant Node_Kind := Nkind (Expr);
2246 return ((Kind = N_Aggregate
2247 or else Kind = N_Extension_Aggregate)
2248 and then Present (Etype (Expr))
2249 and then Is_Record_Type (Etype (Expr))
2250 and then Expansion_Delayed (Expr))
2252 or else (Kind = N_Qualified_Expression
2253 and then Has_Expansion_Delayed (Expression (Expr)));
2254 end Has_Expansion_Delayed;
2256 -- Start of processing for Resolve_Aggr_Expr
2259 -- If the type of the component is elementary or the type of the
2260 -- aggregate does not contain discriminants, use the type of the
2261 -- component to resolve Expr.
2263 if Is_Elementary_Type (Etype (Component))
2264 or else not Has_Discriminants (Etype (N))
2266 Expr_Type := Etype (Component);
2268 -- Otherwise we have to pick up the new type of the component from
2269 -- the new costrained subtype of the aggregate. In fact components
2270 -- which are of a composite type might be constrained by a
2271 -- discriminant, and we want to resolve Expr against the subtype were
2272 -- all discriminant occurrences are replaced with their actual value.
2275 New_C := First_Component (Etype (N));
2276 while Present (New_C) loop
2277 if Chars (New_C) = Chars (Component) then
2278 Expr_Type := Etype (New_C);
2282 Next_Component (New_C);
2285 pragma Assert (Present (Expr_Type));
2287 -- For each range in an array type where a discriminant has been
2288 -- replaced with the constraint, check that this range is within
2289 -- the range of the base type. This checks is done in the
2290 -- _init_proc for regular objects, but has to be done here for
2291 -- aggregates since no _init_proc is called for them.
2293 if Is_Array_Type (Expr_Type) then
2295 Index : Node_Id := First_Index (Expr_Type);
2296 -- Range of the current constrained index in the array.
2298 Orig_Index : Node_Id := First_Index (Etype (Component));
2299 -- Range corresponding to the range Index above in the
2300 -- original unconstrained record type. The bounds of this
2301 -- range may be governed by discriminants.
2303 Unconstr_Index : Node_Id := First_Index (Etype (Expr_Type));
2304 -- Range corresponding to the range Index above for the
2305 -- unconstrained array type. This range is needed to apply
2309 while Present (Index) loop
2310 if Depends_On_Discriminant (Orig_Index) then
2311 Apply_Range_Check (Index, Etype (Unconstr_Index));
2315 Next_Index (Orig_Index);
2316 Next_Index (Unconstr_Index);
2322 -- If the Parent pointer of Expr is not set, Expr is an expression
2323 -- duplicated by New_Tree_Copy (this happens for record aggregates
2324 -- that look like (Field1 | Filed2 => Expr) or (others => Expr)).
2325 -- Such a duplicated expression must be attached to the tree
2326 -- before analysis and resolution to enforce the rule that a tree
2327 -- fragment should never be analyzed or resolved unless it is
2328 -- attached to the current compilation unit.
2330 if No (Parent (Expr)) then
2331 Set_Parent (Expr, N);
2337 Analyze_And_Resolve (Expr, Expr_Type);
2338 Check_Non_Static_Context (Expr);
2340 if not Has_Expansion_Delayed (Expr) then
2341 Aggregate_Constraint_Checks (Expr, Expr_Type);
2344 if Raises_Constraint_Error (Expr) then
2345 Set_Raises_Constraint_Error (N);
2349 Add_Association (New_C, Relocate_Node (Expr));
2351 Add_Association (New_C, Expr);
2354 end Resolve_Aggr_Expr;
2356 -- Resolve_Record_Aggregate local variables
2359 -- N_Component_Association node belonging to the input aggregate N
2362 Positional_Expr : Node_Id;
2364 Component : Entity_Id;
2365 Component_Elmt : Elmt_Id;
2366 Components : Elist_Id := New_Elmt_List;
2367 -- Components is the list of the record components whose value must
2368 -- be provided in the aggregate. This list does include discriminants.
2370 -- Start of processing for Resolve_Record_Aggregate
2373 -- We may end up calling Duplicate_Subexpr on expressions that are
2374 -- attached to New_Assoc_List. For this reason we need to attach it
2375 -- to the tree by setting its parent pointer to N. This parent point
2376 -- will change in STEP 8 below.
2378 Set_Parent (New_Assoc_List, N);
2380 -- STEP 1: abstract type and null record verification
2382 if Is_Abstract (Typ) then
2383 Error_Msg_N ("type of aggregate cannot be abstract", N);
2386 if No (First_Entity (Typ)) and then Null_Record_Present (N) then
2390 elsif Present (First_Entity (Typ))
2391 and then Null_Record_Present (N)
2392 and then not Is_Tagged_Type (Typ)
2394 Error_Msg_N ("record aggregate cannot be null", N);
2397 elsif No (First_Entity (Typ)) then
2398 Error_Msg_N ("record aggregate must be null", N);
2402 -- STEP 2: Verify aggregate structure
2405 Selector_Name : Node_Id;
2406 Bad_Aggregate : Boolean := False;
2409 if Present (Component_Associations (N)) then
2410 Assoc := First (Component_Associations (N));
2415 while Present (Assoc) loop
2416 Selector_Name := First (Choices (Assoc));
2417 while Present (Selector_Name) loop
2418 if Nkind (Selector_Name) = N_Identifier then
2421 elsif Nkind (Selector_Name) = N_Others_Choice then
2422 if Selector_Name /= First (Choices (Assoc))
2423 or else Present (Next (Selector_Name))
2425 Error_Msg_N ("OTHERS must appear alone in a choice list",
2429 elsif Present (Next (Assoc)) then
2430 Error_Msg_N ("OTHERS must appear last in an aggregate",
2437 ("selector name should be identifier or OTHERS",
2439 Bad_Aggregate := True;
2442 Next (Selector_Name);
2448 if Bad_Aggregate then
2453 -- STEP 3: Find discriminant Values
2456 Discrim : Entity_Id;
2457 Missing_Discriminants : Boolean := False;
2460 if Present (Expressions (N)) then
2461 Positional_Expr := First (Expressions (N));
2463 Positional_Expr := Empty;
2466 if Has_Discriminants (Typ) then
2467 Discrim := First_Discriminant (Typ);
2472 -- First find the discriminant values in the positional components
2474 while Present (Discrim) and then Present (Positional_Expr) loop
2475 if Discr_Present (Discrim) then
2476 Resolve_Aggr_Expr (Positional_Expr, Discrim);
2477 Next (Positional_Expr);
2480 if Present (Get_Value (Discrim, Component_Associations (N))) then
2482 ("more than one value supplied for discriminant&",
2486 Next_Discriminant (Discrim);
2489 -- Find remaining discriminant values, if any, among named components
2491 while Present (Discrim) loop
2492 Expr := Get_Value (Discrim, Component_Associations (N), True);
2494 if not Discr_Present (Discrim) then
2495 if Present (Expr) then
2497 ("more than one value supplied for discriminant&",
2501 elsif No (Expr) then
2503 ("no value supplied for discriminant &", N, Discrim);
2504 Missing_Discriminants := True;
2507 Resolve_Aggr_Expr (Expr, Discrim);
2510 Next_Discriminant (Discrim);
2513 if Missing_Discriminants then
2517 -- At this point and until the beginning of STEP 6, New_Assoc_List
2518 -- contains only the discriminants and their values.
2522 -- STEP 4: Set the Etype of the record aggregate
2524 -- ??? This code is pretty much a copy of Sem_Ch3.Build_Subtype. That
2525 -- routine should really be exported in sem_util or some such and used
2526 -- in sem_ch3 and here rather than have a copy of the code which is a
2527 -- maintenance nightmare.
2529 -- ??? Performace WARNING. The current implementation creates a new
2530 -- itype for all aggregates whose base type is discriminated.
2531 -- This means that for record aggregates nested inside an array
2532 -- aggregate we will create a new itype for each record aggregate
2533 -- if the array cmponent type has discriminants. For large aggregates
2534 -- this may be a problem. What should be done in this case is
2535 -- to reuse itypes as much as possible.
2537 if Has_Discriminants (Typ) then
2538 Build_Constrained_Itype : declare
2539 Loc : constant Source_Ptr := Sloc (N);
2541 Subtyp_Decl : Node_Id;
2544 C : List_Id := New_List;
2547 New_Assoc := First (New_Assoc_List);
2548 while Present (New_Assoc) loop
2549 Append (Duplicate_Subexpr (Expression (New_Assoc)), To => C);
2554 Make_Subtype_Indication (Loc,
2555 Subtype_Mark => New_Occurrence_Of (Base_Type (Typ), Loc),
2556 Constraint => Make_Index_Or_Discriminant_Constraint (Loc, C));
2558 Def_Id := Create_Itype (Ekind (Typ), N);
2561 Make_Subtype_Declaration (Loc,
2562 Defining_Identifier => Def_Id,
2563 Subtype_Indication => Indic);
2564 Set_Parent (Subtyp_Decl, Parent (N));
2566 -- Itypes must be analyzed with checks off (see itypes.ads).
2568 Analyze (Subtyp_Decl, Suppress => All_Checks);
2570 Set_Etype (N, Def_Id);
2571 Check_Static_Discriminated_Subtype
2572 (Def_Id, Expression (First (New_Assoc_List)));
2573 end Build_Constrained_Itype;
2579 -- STEP 5: Get remaining components according to discriminant values
2582 Record_Def : Node_Id;
2583 Parent_Typ : Entity_Id;
2584 Root_Typ : Entity_Id;
2585 Parent_Typ_List : Elist_Id;
2586 Parent_Elmt : Elmt_Id;
2587 Errors_Found : Boolean := False;
2591 if Is_Derived_Type (Typ) and then Is_Tagged_Type (Typ) then
2592 Parent_Typ_List := New_Elmt_List;
2594 -- If this is an extension aggregate, the component list must
2595 -- include all components that are not in the given ancestor
2596 -- type. Otherwise, the component list must include components
2597 -- of all ancestors, starting with the root.
2599 if Nkind (N) = N_Extension_Aggregate then
2600 Root_Typ := Base_Type (Etype (Ancestor_Part (N)));
2602 Root_Typ := Root_Type (Typ);
2604 if Nkind (Parent (Base_Type (Root_Typ)))
2605 = N_Private_Type_Declaration
2608 ("type of aggregate has private ancestor&!",
2610 Error_Msg_N ("must use extension aggregate!", N);
2614 Dnode := Declaration_Node (Base_Type (Root_Typ));
2616 -- If we don't get a full declaration, then we have some
2617 -- error which will get signalled later so skip this part.
2618 -- Otherwise, gather components of root that apply to the
2619 -- aggregate type. We use the base type in case there is an
2620 -- applicable girder constraint that renames the discriminants
2623 if Nkind (Dnode) = N_Full_Type_Declaration then
2624 Record_Def := Type_Definition (Dnode);
2625 Gather_Components (Base_Type (Typ),
2626 Component_List (Record_Def),
2627 Governed_By => New_Assoc_List,
2629 Report_Errors => Errors_Found);
2633 Parent_Typ := Base_Type (Typ);
2634 while Parent_Typ /= Root_Typ loop
2636 Prepend_Elmt (Parent_Typ, To => Parent_Typ_List);
2637 Parent_Typ := Etype (Parent_Typ);
2639 if (Nkind (Parent (Base_Type (Parent_Typ))) =
2640 N_Private_Type_Declaration
2641 or else Nkind (Parent (Base_Type (Parent_Typ))) =
2642 N_Private_Extension_Declaration)
2644 if Nkind (N) /= N_Extension_Aggregate then
2646 ("type of aggregate has private ancestor&!",
2648 Error_Msg_N ("must use extension aggregate!", N);
2651 elsif Parent_Typ /= Root_Typ then
2653 ("ancestor part of aggregate must be private type&",
2654 Ancestor_Part (N), Parent_Typ);
2660 -- Now collect components from all other ancestors.
2662 Parent_Elmt := First_Elmt (Parent_Typ_List);
2663 while Present (Parent_Elmt) loop
2664 Parent_Typ := Node (Parent_Elmt);
2665 Record_Def := Type_Definition (Parent (Base_Type (Parent_Typ)));
2666 Gather_Components (Empty,
2667 Component_List (Record_Extension_Part (Record_Def)),
2668 Governed_By => New_Assoc_List,
2670 Report_Errors => Errors_Found);
2672 Next_Elmt (Parent_Elmt);
2676 Record_Def := Type_Definition (Parent (Base_Type (Typ)));
2678 if Null_Present (Record_Def) then
2681 Gather_Components (Base_Type (Typ),
2682 Component_List (Record_Def),
2683 Governed_By => New_Assoc_List,
2685 Report_Errors => Errors_Found);
2689 if Errors_Found then
2694 -- STEP 6: Find component Values
2697 Component_Elmt := First_Elmt (Components);
2699 -- First scan the remaining positional associations in the aggregate.
2700 -- Remember that at this point Positional_Expr contains the current
2701 -- positional association if any is left after looking for discriminant
2702 -- values in step 3.
2704 while Present (Positional_Expr) and then Present (Component_Elmt) loop
2705 Component := Node (Component_Elmt);
2706 Resolve_Aggr_Expr (Positional_Expr, Component);
2708 if Present (Get_Value (Component, Component_Associations (N))) then
2710 ("more than one value supplied for Component &", N, Component);
2713 Next (Positional_Expr);
2714 Next_Elmt (Component_Elmt);
2717 if Present (Positional_Expr) then
2719 ("too many components for record aggregate", Positional_Expr);
2722 -- Now scan for the named arguments of the aggregate
2724 while Present (Component_Elmt) loop
2725 Component := Node (Component_Elmt);
2726 Expr := Get_Value (Component, Component_Associations (N), True);
2729 Error_Msg_NE ("no value supplied for component &!", N, Component);
2731 Resolve_Aggr_Expr (Expr, Component);
2734 Next_Elmt (Component_Elmt);
2737 -- STEP 7: check for invalid components + check type in choice list
2744 -- Type of first component in choice list
2747 if Present (Component_Associations (N)) then
2748 Assoc := First (Component_Associations (N));
2753 Verification : while Present (Assoc) loop
2754 Selectr := First (Choices (Assoc));
2757 if Nkind (Selectr) = N_Others_Choice then
2758 if No (Others_Etype) then
2760 ("OTHERS must represent at least one component", Selectr);
2766 while Present (Selectr) loop
2767 New_Assoc := First (New_Assoc_List);
2768 while Present (New_Assoc) loop
2769 Component := First (Choices (New_Assoc));
2770 exit when Chars (Selectr) = Chars (Component);
2774 -- If no association, this is not a legal component of
2775 -- of the type in question, except if this is an internal
2776 -- component supplied by a previous expansion.
2778 if No (New_Assoc) then
2780 if Chars (Selectr) /= Name_uTag
2781 and then Chars (Selectr) /= Name_uParent
2782 and then Chars (Selectr) /= Name_uController
2784 if not Has_Discriminants (Typ) then
2785 Error_Msg_Node_2 := Typ;
2787 ("& is not a component of}",
2791 ("& is not a component of the aggregate subtype",
2795 Check_Misspelled_Component (Components, Selectr);
2798 elsif No (Typech) then
2799 Typech := Base_Type (Etype (Component));
2801 elsif Typech /= Base_Type (Etype (Component)) then
2803 ("components in choice list must have same type", Selectr);
2810 end loop Verification;
2813 -- STEP 8: replace the original aggregate
2816 New_Aggregate : Node_Id := New_Copy (N);
2819 Set_Expressions (New_Aggregate, No_List);
2820 Set_Etype (New_Aggregate, Etype (N));
2821 Set_Component_Associations (New_Aggregate, New_Assoc_List);
2823 Rewrite (N, New_Aggregate);
2825 end Resolve_Record_Aggregate;
2827 ---------------------
2828 -- Sort_Case_Table --
2829 ---------------------
2831 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is
2832 L : Int := Case_Table'First;
2833 U : Int := Case_Table'Last;
2842 T := Case_Table (K + 1);
2846 and then Expr_Value (Case_Table (J - 1).Choice_Lo) >
2847 Expr_Value (T.Choice_Lo)
2849 Case_Table (J) := Case_Table (J - 1);
2853 Case_Table (J) := T;
2856 end Sort_Case_Table;