include/ChangeLog:
[official-gcc.git] / gcc / ada / sem_aggr.adb
blob6ddeaab6c91d2bf30915df03bdb33906d5e003c0
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ A G G R --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2002 Free Software Foundation, Inc. --
10 -- --
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. --
21 -- --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 -- --
25 ------------------------------------------------------------------------------
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Einfo; use Einfo;
30 with Elists; use Elists;
31 with Errout; use Errout;
32 with Exp_Util; use Exp_Util;
33 with Freeze; use Freeze;
34 with Itypes; use Itypes;
35 with Namet; use Namet;
36 with Nmake; use Nmake;
37 with Nlists; use Nlists;
38 with Opt; use Opt;
39 with Sem; use Sem;
40 with Sem_Cat; use Sem_Cat;
41 with Sem_Ch8; use Sem_Ch8;
42 with Sem_Ch13; use Sem_Ch13;
43 with Sem_Eval; use Sem_Eval;
44 with Sem_Res; use Sem_Res;
45 with Sem_Util; use Sem_Util;
46 with Sem_Type; use Sem_Type;
47 with Sinfo; use Sinfo;
48 with Snames; use Snames;
49 with Stringt; use Stringt;
50 with Stand; use Stand;
51 with Tbuild; use Tbuild;
52 with Uintp; use Uintp;
54 with GNAT.Spelling_Checker; use GNAT.Spelling_Checker;
56 package body Sem_Aggr is
58 type Case_Bounds is record
59 Choice_Lo : Node_Id;
60 Choice_Hi : Node_Id;
61 Choice_Node : Node_Id;
62 end record;
64 type Case_Table_Type is array (Nat range <>) of Case_Bounds;
65 -- Table type used by Check_Case_Choices procedure
67 -----------------------
68 -- Local Subprograms --
69 -----------------------
71 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type);
72 -- Sort the Case Table using the Lower Bound of each Choice as the key.
73 -- A simple insertion sort is used since the number of choices in a case
74 -- statement of variant part will usually be small and probably in near
75 -- sorted order.
77 ------------------------------------------------------
78 -- Subprograms used for RECORD AGGREGATE Processing --
79 ------------------------------------------------------
81 procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id);
82 -- This procedure performs all the semantic checks required for record
83 -- aggregates. Note that for aggregates analysis and resolution go
84 -- hand in hand. Aggregate analysis has been delayed up to here and
85 -- it is done while resolving the aggregate.
87 -- N is the N_Aggregate node.
88 -- Typ is the record type for the aggregate resolution
90 -- While performing the semantic checks, this procedure
91 -- builds a new Component_Association_List where each record field
92 -- appears alone in a Component_Choice_List along with its corresponding
93 -- expression. The record fields in the Component_Association_List
94 -- appear in the same order in which they appear in the record type Typ.
96 -- Once this new Component_Association_List is built and all the
97 -- semantic checks performed, the original aggregate subtree is replaced
98 -- with the new named record aggregate just built. Note that the subtree
99 -- substitution is performed with Rewrite so as to be
100 -- able to retrieve the original aggregate.
102 -- The aggregate subtree manipulation performed by Resolve_Record_Aggregate
103 -- yields the aggregate format expected by Gigi. Typically, this kind of
104 -- tree manipulations are done in the expander. However, because the
105 -- semantic checks that need to be performed on record aggregates really
106 -- go hand in hand with the record aggreagate normalization, the aggregate
107 -- subtree transformation is performed during resolution rather than
108 -- expansion. Had we decided otherwise we would have had to duplicate
109 -- most of the code in the expansion procedure Expand_Record_Aggregate.
110 -- Note, however, that all the expansion concerning aggegates for tagged
111 -- records is done in Expand_Record_Aggregate.
113 -- The algorithm of Resolve_Record_Aggregate proceeds as follows:
115 -- 1. Make sure that the record type against which the record aggregate
116 -- has to be resolved is not abstract. Furthermore if the type is
117 -- a null aggregate make sure the input aggregate N is also null.
119 -- 2. Verify that the structure of the aggregate is that of a record
120 -- aggregate. Specifically, look for component associations and ensure
121 -- that each choice list only has identifiers or the N_Others_Choice
122 -- node. Also make sure that if present, the N_Others_Choice occurs
123 -- last and by itself.
125 -- 3. If Typ contains discriminants, the values for each discriminant
126 -- is looked for. If the record type Typ has variants, we check
127 -- that the expressions corresponding to each discriminant ruling
128 -- the (possibly nested) variant parts of Typ, are static. This
129 -- allows us to determine the variant parts to which the rest of
130 -- the aggregate must conform. The names of discriminants with their
131 -- values are saved in a new association list, New_Assoc_List which
132 -- is later augmented with the names and values of the remaining
133 -- components in the record type.
135 -- During this phase we also make sure that every discriminant is
136 -- assigned exactly one value. Note that when several values
137 -- for a given discriminant are found, semantic processing continues
138 -- looking for further errors. In this case it's the first
139 -- discriminant value found which we will be recorded.
141 -- IMPORTANT NOTE: For derived tagged types this procedure expects
142 -- First_Discriminant and Next_Discriminant to give the correct list
143 -- of discriminants, in the correct order.
145 -- 4. After all the discriminant values have been gathered, we can
146 -- set the Etype of the record aggregate. If Typ contains no
147 -- discriminants this is straightforward: the Etype of N is just
148 -- Typ, otherwise a new implicit constrained subtype of Typ is
149 -- built to be the Etype of N.
151 -- 5. Gather the remaining record components according to the discriminant
152 -- values. This involves recursively traversing the record type
153 -- structure to see what variants are selected by the given discriminant
154 -- values. This processing is a little more convoluted if Typ is a
155 -- derived tagged types since we need to retrieve the record structure
156 -- of all the ancestors of Typ.
158 -- 6. After gathering the record components we look for their values
159 -- in the record aggregate and emit appropriate error messages
160 -- should we not find such values or should they be duplicated.
162 -- 7. We then make sure no illegal component names appear in the
163 -- record aggegate and make sure that the type of the record
164 -- components appearing in a same choice list is the same.
165 -- Finally we ensure that the others choice, if present, is
166 -- used to provide the value of at least a record component.
168 -- 8. The original aggregate node is replaced with the new named
169 -- aggregate built in steps 3 through 6, as explained earlier.
171 -- Given the complexity of record aggregate resolution, the primary
172 -- goal of this routine is clarity and simplicity rather than execution
173 -- and storage efficiency. If there are only positional components in the
174 -- aggregate the running time is linear. If there are associations
175 -- the running time is still linear as long as the order of the
176 -- associations is not too far off the order of the components in the
177 -- record type. If this is not the case the running time is at worst
178 -- quadratic in the size of the association list.
180 procedure Check_Misspelled_Component
181 (Elements : Elist_Id;
182 Component : Node_Id);
183 -- Give possible misspelling diagnostic if Component is likely to be
184 -- a misspelling of one of the components of the Assoc_List.
185 -- This is called by Resolv_Aggr_Expr after producing
186 -- an invalid component error message.
188 procedure Check_Static_Discriminated_Subtype (T : Entity_Id; V : Node_Id);
189 -- An optimization: determine whether a discriminated subtype has a
190 -- static constraint, and contains array components whose length is also
191 -- static, either because they are constrained by the discriminant, or
192 -- because the original component bounds are static.
194 -----------------------------------------------------
195 -- Subprograms used for ARRAY AGGREGATE Processing --
196 -----------------------------------------------------
198 function Resolve_Array_Aggregate
199 (N : Node_Id;
200 Index : Node_Id;
201 Index_Constr : Node_Id;
202 Component_Typ : Entity_Id;
203 Others_Allowed : Boolean)
204 return Boolean;
205 -- This procedure performs the semantic checks for an array aggregate.
206 -- True is returned if the aggregate resolution succeeds.
207 -- The procedure works by recursively checking each nested aggregate.
208 -- Specifically, after checking a sub-aggreate nested at the i-th level
209 -- we recursively check all the subaggregates at the i+1-st level (if any).
210 -- Note that for aggregates analysis and resolution go hand in hand.
211 -- Aggregate analysis has been delayed up to here and it is done while
212 -- resolving the aggregate.
214 -- N is the current N_Aggregate node to be checked.
216 -- Index is the index node corresponding to the array sub-aggregate that
217 -- we are currently checking (RM 4.3.3 (8)). Its Etype is the
218 -- corresponding index type (or subtype).
220 -- Index_Constr is the node giving the applicable index constraint if
221 -- any (RM 4.3.3 (10)). It "is a constraint provided by certain
222 -- contexts [...] that can be used to determine the bounds of the array
223 -- value specified by the aggregate". If Others_Allowed below is False
224 -- there is no applicable index constraint and this node is set to Index.
226 -- Component_Typ is the array component type.
228 -- Others_Allowed indicates whether an others choice is allowed
229 -- in the context where the top-level aggregate appeared.
231 -- The algorithm of Resolve_Array_Aggregate proceeds as follows:
233 -- 1. Make sure that the others choice, if present, is by itself and
234 -- appears last in the sub-aggregate. Check that we do not have
235 -- positional and named components in the array sub-aggregate (unless
236 -- the named association is an others choice). Finally if an others
237 -- choice is present, make sure it is allowed in the aggregate contex.
239 -- 2. If the array sub-aggregate contains discrete_choices:
241 -- (A) Verify their validity. Specifically verify that:
243 -- (a) If a null range is present it must be the only possible
244 -- choice in the array aggregate.
246 -- (b) Ditto for a non static range.
248 -- (c) Ditto for a non static expression.
250 -- In addition this step analyzes and resolves each discrete_choice,
251 -- making sure that its type is the type of the corresponding Index.
252 -- If we are not at the lowest array aggregate level (in the case of
253 -- multi-dimensional aggregates) then invoke Resolve_Array_Aggregate
254 -- recursively on each component expression. Otherwise, resolve the
255 -- bottom level component expressions against the expected component
256 -- type ONLY IF the component corresponds to a single discrete choice
257 -- which is not an others choice (to see why read the DELAYED
258 -- COMPONENT RESOLUTION below).
260 -- (B) Determine the bounds of the sub-aggregate and lowest and
261 -- highest choice values.
263 -- 3. For positional aggregates:
265 -- (A) Loop over the component expressions either recursively invoking
266 -- Resolve_Array_Aggregate on each of these for multi-dimensional
267 -- array aggregates or resolving the bottom level component
268 -- expressions against the expected component type.
270 -- (B) Determine the bounds of the positional sub-aggregates.
272 -- 4. Try to determine statically whether the evaluation of the array
273 -- sub-aggregate raises Constraint_Error. If yes emit proper
274 -- warnings. The precise checks are the following:
276 -- (A) Check that the index range defined by aggregate bounds is
277 -- compatible with corresponding index subtype.
278 -- We also check against the base type. In fact it could be that
279 -- Low/High bounds of the base type are static whereas those of
280 -- the index subtype are not. Thus if we can statically catch
281 -- a problem with respect to the base type we are guaranteed
282 -- that the same problem will arise with the index subtype
284 -- (B) If we are dealing with a named aggregate containing an others
285 -- choice and at least one discrete choice then make sure the range
286 -- specified by the discrete choices does not overflow the
287 -- aggregate bounds. We also check against the index type and base
288 -- type bounds for the same reasons given in (A).
290 -- (C) If we are dealing with a positional aggregate with an others
291 -- choice make sure the number of positional elements specified
292 -- does not overflow the aggregate bounds. We also check against
293 -- the index type and base type bounds as mentioned in (A).
295 -- Finally construct an N_Range node giving the sub-aggregate bounds.
296 -- Set the Aggregate_Bounds field of the sub-aggregate to be this
297 -- N_Range. The routine Array_Aggr_Subtype below uses such N_Ranges
298 -- to build the appropriate aggregate subtype. Aggregate_Bounds
299 -- information is needed during expansion.
301 -- DELAYED COMPONENT RESOLUTION: The resolution of bottom level component
302 -- expressions in an array aggregate may call Duplicate_Subexpr or some
303 -- other routine that inserts code just outside the outermost aggregate.
304 -- If the array aggregate contains discrete choices or an others choice,
305 -- this may be wrong. Consider for instance the following example.
307 -- type Rec is record
308 -- V : Integer := 0;
309 -- end record;
311 -- type Acc_Rec is access Rec;
312 -- Arr : array (1..3) of Acc_Rec := (1 .. 3 => new Rec);
314 -- Then the transformation of "new Rec" that occurs during resolution
315 -- entails the following code modifications
317 -- P7b : constant Acc_Rec := new Rec;
318 -- Rec_init_proc (P7b.all);
319 -- Arr : array (1..3) of Acc_Rec := (1 .. 3 => P7b);
321 -- This code transformation is clearly wrong, since we need to call
322 -- "new Rec" for each of the 3 array elements. To avoid this problem we
323 -- delay resolution of the components of non positional array aggregates
324 -- to the expansion phase. As an optimization, if the discrete choice
325 -- specifies a single value we do not delay resolution.
327 function Array_Aggr_Subtype (N : Node_Id; Typ : Node_Id) return Entity_Id;
328 -- This routine returns the type or subtype of an array aggregate.
330 -- N is the array aggregate node whose type we return.
332 -- Typ is the context type in which N occurs.
334 -- This routine creates an implicit array subtype whose bouds are
335 -- those defined by the aggregate. When this routine is invoked
336 -- Resolve_Array_Aggregate has already processed aggregate N. Thus the
337 -- Aggregate_Bounds of each sub-aggregate, is an N_Range node giving the
338 -- sub-aggregate bounds. When building the aggegate itype, this function
339 -- traverses the array aggregate N collecting such Aggregate_Bounds and
340 -- constructs the proper array aggregate itype.
342 -- Note that in the case of multidimensional aggregates each inner
343 -- sub-aggregate corresponding to a given array dimension, may provide a
344 -- different bounds. If it is possible to determine statically that
345 -- some sub-aggregates corresponding to the same index do not have the
346 -- same bounds, then a warning is emitted. If such check is not possible
347 -- statically (because some sub-aggregate bounds are dynamic expressions)
348 -- then this job is left to the expander. In all cases the particular
349 -- bounds that this function will chose for a given dimension is the first
350 -- N_Range node for a sub-aggregate corresponding to that dimension.
352 -- Note that the Raises_Constraint_Error flag of an array aggregate
353 -- whose evaluation is determined to raise CE by Resolve_Array_Aggregate,
354 -- is set in Resolve_Array_Aggregate but the aggregate is not
355 -- immediately replaced with a raise CE. In fact, Array_Aggr_Subtype must
356 -- first construct the proper itype for the aggregate (Gigi needs
357 -- this). After constructing the proper itype we will eventually replace
358 -- the top-level aggregate with a raise CE (done in Resolve_Aggregate).
359 -- Of course in cases such as:
361 -- type Arr is array (integer range <>) of Integer;
362 -- A : Arr := (positive range -1 .. 2 => 0);
364 -- The bounds of the aggregate itype are cooked up to look reasonable
365 -- (in this particular case the bounds will be 1 .. 2).
367 procedure Aggregate_Constraint_Checks
368 (Exp : Node_Id;
369 Check_Typ : Entity_Id);
370 -- Checks expression Exp against subtype Check_Typ. If Exp is an
371 -- aggregate and Check_Typ a constrained record type with discriminants,
372 -- we generate the appropriate discriminant checks. If Exp is an array
373 -- aggregate then emit the appropriate length checks. If Exp is a scalar
374 -- type, or a string literal, Exp is changed into Check_Typ'(Exp) to
375 -- ensure that range checks are performed at run time.
377 procedure Make_String_Into_Aggregate (N : Node_Id);
378 -- A string literal can appear in a context in which a one dimensional
379 -- array of characters is expected. This procedure simply rewrites the
380 -- string as an aggregate, prior to resolution.
382 ---------------------------------
383 -- Aggregate_Constraint_Checks --
384 ---------------------------------
386 procedure Aggregate_Constraint_Checks
387 (Exp : Node_Id;
388 Check_Typ : Entity_Id)
390 Exp_Typ : constant Entity_Id := Etype (Exp);
392 begin
393 if Raises_Constraint_Error (Exp) then
394 return;
395 end if;
397 -- This is really expansion activity, so make sure that expansion
398 -- is on and is allowed.
400 if not Expander_Active or else In_Default_Expression then
401 return;
402 end if;
404 -- First check if we have to insert discriminant checks
406 if Has_Discriminants (Exp_Typ) then
407 Apply_Discriminant_Check (Exp, Check_Typ);
409 -- Next emit length checks for array aggregates
411 elsif Is_Array_Type (Exp_Typ) then
412 Apply_Length_Check (Exp, Check_Typ);
414 -- Finally emit scalar and string checks. If we are dealing with a
415 -- scalar literal we need to check by hand because the Etype of
416 -- literals is not necessarily correct.
418 elsif Is_Scalar_Type (Exp_Typ)
419 and then Compile_Time_Known_Value (Exp)
420 then
421 if Is_Out_Of_Range (Exp, Base_Type (Check_Typ)) then
422 Apply_Compile_Time_Constraint_Error
423 (Exp, "value not in range of}?", CE_Range_Check_Failed,
424 Ent => Base_Type (Check_Typ),
425 Typ => Base_Type (Check_Typ));
427 elsif Is_Out_Of_Range (Exp, Check_Typ) then
428 Apply_Compile_Time_Constraint_Error
429 (Exp, "value not in range of}?", CE_Range_Check_Failed,
430 Ent => Check_Typ,
431 Typ => Check_Typ);
433 elsif not Range_Checks_Suppressed (Check_Typ) then
434 Apply_Scalar_Range_Check (Exp, Check_Typ);
435 end if;
437 elsif (Is_Scalar_Type (Exp_Typ)
438 or else Nkind (Exp) = N_String_Literal)
439 and then Exp_Typ /= Check_Typ
440 then
441 if Is_Entity_Name (Exp)
442 and then Ekind (Entity (Exp)) = E_Constant
443 then
444 -- If expression is a constant, it is worthwhile checking whether
445 -- it is a bound of the type.
447 if (Is_Entity_Name (Type_Low_Bound (Check_Typ))
448 and then Entity (Exp) = Entity (Type_Low_Bound (Check_Typ)))
449 or else (Is_Entity_Name (Type_High_Bound (Check_Typ))
450 and then Entity (Exp) = Entity (Type_High_Bound (Check_Typ)))
451 then
452 return;
454 else
455 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
456 Analyze_And_Resolve (Exp, Check_Typ);
457 end if;
458 else
459 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
460 Analyze_And_Resolve (Exp, Check_Typ);
461 end if;
463 end if;
464 end Aggregate_Constraint_Checks;
466 ------------------------
467 -- Array_Aggr_Subtype --
468 ------------------------
470 function Array_Aggr_Subtype
471 (N : Node_Id;
472 Typ : Entity_Id)
473 return Entity_Id
475 Aggr_Dimension : constant Pos := Number_Dimensions (Typ);
476 -- Number of aggregate index dimensions.
478 Aggr_Range : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
479 -- Constrained N_Range of each index dimension in our aggregate itype.
481 Aggr_Low : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
482 Aggr_High : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
483 -- Low and High bounds for each index dimension in our aggregate itype.
485 Is_Fully_Positional : Boolean := True;
487 procedure Collect_Aggr_Bounds (N : Node_Id; Dim : Pos);
488 -- N is an array (sub-)aggregate. Dim is the dimension corresponding to
489 -- (sub-)aggregate N. This procedure collects the constrained N_Range
490 -- nodes corresponding to each index dimension of our aggregate itype.
491 -- These N_Range nodes are collected in Aggr_Range above.
492 -- Likewise collect in Aggr_Low & Aggr_High above the low and high
493 -- bounds of each index dimension. If, when collecting, two bounds
494 -- corresponding to the same dimension are static and found to differ,
495 -- then emit a warning, and mark N as raising Constraint_Error.
497 -------------------------
498 -- Collect_Aggr_Bounds --
499 -------------------------
501 procedure Collect_Aggr_Bounds (N : Node_Id; Dim : Pos) is
502 This_Range : constant Node_Id := Aggregate_Bounds (N);
503 -- The aggregate range node of this specific sub-aggregate.
505 This_Low : constant Node_Id := Low_Bound (Aggregate_Bounds (N));
506 This_High : constant Node_Id := High_Bound (Aggregate_Bounds (N));
507 -- The aggregate bounds of this specific sub-aggregate.
509 Assoc : Node_Id;
510 Expr : Node_Id;
512 begin
513 -- Collect the first N_Range for a given dimension that you find.
514 -- For a given dimension they must be all equal anyway.
516 if No (Aggr_Range (Dim)) then
517 Aggr_Low (Dim) := This_Low;
518 Aggr_High (Dim) := This_High;
519 Aggr_Range (Dim) := This_Range;
521 else
522 if Compile_Time_Known_Value (This_Low) then
523 if not Compile_Time_Known_Value (Aggr_Low (Dim)) then
524 Aggr_Low (Dim) := This_Low;
526 elsif Expr_Value (This_Low) /= Expr_Value (Aggr_Low (Dim)) then
527 Set_Raises_Constraint_Error (N);
528 Error_Msg_N ("Sub-aggregate low bound mismatch?", N);
529 Error_Msg_N ("Constraint_Error will be raised at run-time?",
531 end if;
532 end if;
534 if Compile_Time_Known_Value (This_High) then
535 if not Compile_Time_Known_Value (Aggr_High (Dim)) then
536 Aggr_High (Dim) := This_High;
538 elsif
539 Expr_Value (This_High) /= Expr_Value (Aggr_High (Dim))
540 then
541 Set_Raises_Constraint_Error (N);
542 Error_Msg_N ("Sub-aggregate high bound mismatch?", N);
543 Error_Msg_N ("Constraint_Error will be raised at run-time?",
545 end if;
546 end if;
547 end if;
549 if Dim < Aggr_Dimension then
551 -- Process positional components
553 if Present (Expressions (N)) then
554 Expr := First (Expressions (N));
555 while Present (Expr) loop
556 Collect_Aggr_Bounds (Expr, Dim + 1);
557 Next (Expr);
558 end loop;
559 end if;
561 -- Process component associations
563 if Present (Component_Associations (N)) then
564 Is_Fully_Positional := False;
566 Assoc := First (Component_Associations (N));
567 while Present (Assoc) loop
568 Expr := Expression (Assoc);
569 Collect_Aggr_Bounds (Expr, Dim + 1);
570 Next (Assoc);
571 end loop;
572 end if;
573 end if;
574 end Collect_Aggr_Bounds;
576 -- Array_Aggr_Subtype variables
578 Itype : Entity_Id;
579 -- the final itype of the overall aggregate
581 Index_Constraints : List_Id := New_List;
582 -- The list of index constraints of the aggregate itype.
584 -- Start of processing for Array_Aggr_Subtype
586 begin
587 -- Make sure that the list of index constraints is properly attached
588 -- to the tree, and then collect the aggregate bounds.
590 Set_Parent (Index_Constraints, N);
591 Collect_Aggr_Bounds (N, 1);
593 -- Build the list of constrained indices of our aggregate itype.
595 for J in 1 .. Aggr_Dimension loop
596 Create_Index : declare
597 Index_Base : Entity_Id := Base_Type (Etype (Aggr_Range (J)));
598 Index_Typ : Entity_Id;
600 begin
601 -- Construct the Index subtype
603 Index_Typ := Create_Itype (Subtype_Kind (Ekind (Index_Base)), N);
605 Set_Etype (Index_Typ, Index_Base);
607 if Is_Character_Type (Index_Base) then
608 Set_Is_Character_Type (Index_Typ);
609 end if;
611 Set_Size_Info (Index_Typ, (Index_Base));
612 Set_RM_Size (Index_Typ, RM_Size (Index_Base));
613 Set_First_Rep_Item (Index_Typ, First_Rep_Item (Index_Base));
614 Set_Scalar_Range (Index_Typ, Aggr_Range (J));
616 if Is_Discrete_Or_Fixed_Point_Type (Index_Typ) then
617 Set_RM_Size (Index_Typ, UI_From_Int (Minimum_Size (Index_Typ)));
618 end if;
620 Set_Etype (Aggr_Range (J), Index_Typ);
622 Append (Aggr_Range (J), To => Index_Constraints);
623 end Create_Index;
624 end loop;
626 -- Now build the Itype
628 Itype := Create_Itype (E_Array_Subtype, N);
630 Set_First_Rep_Item (Itype, First_Rep_Item (Typ));
631 Set_Convention (Itype, Convention (Typ));
632 Set_Depends_On_Private (Itype, Has_Private_Component (Typ));
633 Set_Etype (Itype, Base_Type (Typ));
634 Set_Has_Alignment_Clause (Itype, Has_Alignment_Clause (Typ));
635 Set_Is_Aliased (Itype, Is_Aliased (Typ));
636 Set_Suppress_Index_Checks (Itype, Suppress_Index_Checks (Typ));
637 Set_Suppress_Length_Checks (Itype, Suppress_Length_Checks (Typ));
638 Set_Depends_On_Private (Itype, Depends_On_Private (Typ));
640 Set_First_Index (Itype, First (Index_Constraints));
641 Set_Is_Constrained (Itype, True);
642 Set_Is_Internal (Itype, True);
643 Init_Size_Align (Itype);
645 -- A simple optimization: purely positional aggregates of static
646 -- components should be passed to gigi unexpanded whenever possible,
647 -- and regardless of the staticness of the bounds themselves. Subse-
648 -- quent checks in exp_aggr verify that type is not packed, etc.
650 Set_Size_Known_At_Compile_Time (Itype,
651 Is_Fully_Positional
652 and then Comes_From_Source (N)
653 and then Size_Known_At_Compile_Time (Component_Type (Typ)));
655 -- We always need a freeze node for a packed array subtype, so that
656 -- we can build the Packed_Array_Type corresponding to the subtype.
657 -- If expansion is disabled, the packed array subtype is not built,
658 -- and we must not generate a freeze node for the type, or else it
659 -- will appear incomplete to gigi.
661 if Is_Packed (Itype) and then not In_Default_Expression
662 and then Expander_Active
663 then
664 Freeze_Itype (Itype, N);
665 end if;
667 return Itype;
668 end Array_Aggr_Subtype;
670 --------------------------------
671 -- Check_Misspelled_Component --
672 --------------------------------
674 procedure Check_Misspelled_Component
675 (Elements : Elist_Id;
676 Component : Node_Id)
678 Max_Suggestions : constant := 2;
680 Nr_Of_Suggestions : Natural := 0;
681 Suggestion_1 : Entity_Id := Empty;
682 Suggestion_2 : Entity_Id := Empty;
683 Component_Elmt : Elmt_Id;
685 begin
686 -- All the components of List are matched against Component and
687 -- a count is maintained of possible misspellings. When at the
688 -- end of the analysis there are one or two (not more!) possible
689 -- misspellings, these misspellings will be suggested as
690 -- possible correction.
692 Get_Name_String (Chars (Component));
694 declare
695 S : constant String (1 .. Name_Len) :=
696 Name_Buffer (1 .. Name_Len);
698 begin
700 Component_Elmt := First_Elmt (Elements);
702 while Nr_Of_Suggestions <= Max_Suggestions
703 and then Present (Component_Elmt)
704 loop
706 Get_Name_String (Chars (Node (Component_Elmt)));
708 if Is_Bad_Spelling_Of (Name_Buffer (1 .. Name_Len), S) then
709 Nr_Of_Suggestions := Nr_Of_Suggestions + 1;
711 case Nr_Of_Suggestions is
712 when 1 => Suggestion_1 := Node (Component_Elmt);
713 when 2 => Suggestion_2 := Node (Component_Elmt);
714 when others => exit;
715 end case;
716 end if;
718 Next_Elmt (Component_Elmt);
719 end loop;
721 -- Report at most two suggestions
723 if Nr_Of_Suggestions = 1 then
724 Error_Msg_NE ("\possible misspelling of&",
725 Component, Suggestion_1);
727 elsif Nr_Of_Suggestions = 2 then
728 Error_Msg_Node_2 := Suggestion_2;
729 Error_Msg_NE ("\possible misspelling of& or&",
730 Component, Suggestion_1);
731 end if;
732 end;
733 end Check_Misspelled_Component;
735 ----------------------------------------
736 -- Check_Static_Discriminated_Subtype --
737 ----------------------------------------
739 procedure Check_Static_Discriminated_Subtype (T : Entity_Id; V : Node_Id) is
740 Disc : constant Entity_Id := First_Discriminant (T);
741 Comp : Entity_Id;
742 Ind : Entity_Id;
744 begin
745 if Has_Record_Rep_Clause (T) then
746 return;
748 elsif Present (Next_Discriminant (Disc)) then
749 return;
751 elsif Nkind (V) /= N_Integer_Literal then
752 return;
753 end if;
755 Comp := First_Component (T);
757 while Present (Comp) loop
759 if Is_Scalar_Type (Etype (Comp)) then
760 null;
762 elsif Is_Private_Type (Etype (Comp))
763 and then Present (Full_View (Etype (Comp)))
764 and then Is_Scalar_Type (Full_View (Etype (Comp)))
765 then
766 null;
768 elsif Is_Array_Type (Etype (Comp)) then
770 if Is_Bit_Packed_Array (Etype (Comp)) then
771 return;
772 end if;
774 Ind := First_Index (Etype (Comp));
776 while Present (Ind) loop
778 if Nkind (Ind) /= N_Range
779 or else Nkind (Low_Bound (Ind)) /= N_Integer_Literal
780 or else Nkind (High_Bound (Ind)) /= N_Integer_Literal
781 then
782 return;
783 end if;
785 Next_Index (Ind);
786 end loop;
788 else
789 return;
790 end if;
792 Next_Component (Comp);
793 end loop;
795 -- On exit, all components have statically known sizes.
797 Set_Size_Known_At_Compile_Time (T);
798 end Check_Static_Discriminated_Subtype;
800 --------------------------------
801 -- Make_String_Into_Aggregate --
802 --------------------------------
804 procedure Make_String_Into_Aggregate (N : Node_Id) is
805 C : Char_Code;
806 C_Node : Node_Id;
807 Exprs : List_Id := New_List;
808 Loc : constant Source_Ptr := Sloc (N);
809 New_N : Node_Id;
810 P : Source_Ptr := Loc + 1;
811 Str : constant String_Id := Strval (N);
812 Strlen : constant Nat := String_Length (Str);
814 begin
815 for J in 1 .. Strlen loop
816 C := Get_String_Char (Str, J);
817 Set_Character_Literal_Name (C);
819 C_Node := Make_Character_Literal (P, Name_Find, C);
820 Set_Etype (C_Node, Any_Character);
821 Append_To (Exprs, C_Node);
823 P := P + 1;
824 -- something special for wide strings ?
825 end loop;
827 New_N := Make_Aggregate (Loc, Expressions => Exprs);
828 Set_Analyzed (New_N);
829 Set_Etype (New_N, Any_Composite);
831 Rewrite (N, New_N);
832 end Make_String_Into_Aggregate;
834 -----------------------
835 -- Resolve_Aggregate --
836 -----------------------
838 procedure Resolve_Aggregate (N : Node_Id; Typ : Entity_Id) is
839 Pkind : constant Node_Kind := Nkind (Parent (N));
841 Aggr_Subtyp : Entity_Id;
842 -- The actual aggregate subtype. This is not necessarily the same as Typ
843 -- which is the subtype of the context in which the aggregate was found.
845 begin
846 if Is_Limited_Type (Typ) then
847 Error_Msg_N ("aggregate type cannot be limited", N);
849 elsif Is_Limited_Composite (Typ) then
850 Error_Msg_N ("aggregate type cannot have limited component", N);
852 elsif Is_Class_Wide_Type (Typ) then
853 Error_Msg_N ("type of aggregate cannot be class-wide", N);
855 elsif Typ = Any_String
856 or else Typ = Any_Composite
857 then
858 Error_Msg_N ("no unique type for aggregate", N);
859 Set_Etype (N, Any_Composite);
861 elsif Is_Array_Type (Typ) and then Null_Record_Present (N) then
862 Error_Msg_N ("null record forbidden in array aggregate", N);
864 elsif Is_Record_Type (Typ) then
865 Resolve_Record_Aggregate (N, Typ);
867 elsif Is_Array_Type (Typ) then
869 -- First a special test, for the case of a positional aggregate
870 -- of characters which can be replaced by a string literal.
871 -- Do not perform this transformation if this was a string literal
872 -- to start with, whose components needed constraint checks, or if
873 -- the component type is non-static, because it will require those
874 -- checks and be transformed back into an aggregate.
876 if Number_Dimensions (Typ) = 1
877 and then
878 (Root_Type (Component_Type (Typ)) = Standard_Character
879 or else
880 Root_Type (Component_Type (Typ)) = Standard_Wide_Character)
881 and then No (Component_Associations (N))
882 and then not Is_Limited_Composite (Typ)
883 and then not Is_Private_Composite (Typ)
884 and then not Is_Bit_Packed_Array (Typ)
885 and then Nkind (Original_Node (Parent (N))) /= N_String_Literal
886 and then Is_Static_Subtype (Component_Type (Typ))
887 then
888 declare
889 Expr : Node_Id;
891 begin
892 Expr := First (Expressions (N));
893 while Present (Expr) loop
894 exit when Nkind (Expr) /= N_Character_Literal;
895 Next (Expr);
896 end loop;
898 if No (Expr) then
899 Start_String;
901 Expr := First (Expressions (N));
902 while Present (Expr) loop
903 Store_String_Char (Char_Literal_Value (Expr));
904 Next (Expr);
905 end loop;
907 Rewrite (N,
908 Make_String_Literal (Sloc (N), End_String));
910 Analyze_And_Resolve (N, Typ);
911 return;
912 end if;
913 end;
914 end if;
916 -- Here if we have a real aggregate to deal with
918 Array_Aggregate : declare
919 Aggr_Resolved : Boolean;
920 Aggr_Typ : Entity_Id := Etype (Typ);
921 -- This is the unconstrained array type, which is the type
922 -- against which the aggregate is to be resoved. Typ itself
923 -- is the array type of the context which may not be the same
924 -- subtype as the subtype for the final aggregate.
926 begin
927 -- In the following we determine whether an others choice is
928 -- allowed inside the array aggregate. The test checks the context
929 -- in which the array aggregate occurs. If the context does not
930 -- permit it, or the aggregate type is unconstrained, an others
931 -- choice is not allowed.
933 -- Note that there is no node for Explicit_Actual_Parameter.
934 -- To test for this context we therefore have to test for node
935 -- N_Parameter_Association which itself appears only if there is a
936 -- formal parameter. Consequently we also need to test for
937 -- N_Procedure_Call_Statement or N_Function_Call.
939 if Is_Constrained (Typ) and then
940 (Pkind = N_Assignment_Statement or else
941 Pkind = N_Parameter_Association or else
942 Pkind = N_Function_Call or else
943 Pkind = N_Procedure_Call_Statement or else
944 Pkind = N_Generic_Association or else
945 Pkind = N_Formal_Object_Declaration or else
946 Pkind = N_Return_Statement or else
947 Pkind = N_Object_Declaration or else
948 Pkind = N_Component_Declaration or else
949 Pkind = N_Parameter_Specification or else
950 Pkind = N_Qualified_Expression or else
951 Pkind = N_Aggregate or else
952 Pkind = N_Extension_Aggregate or else
953 Pkind = N_Component_Association)
954 then
955 Aggr_Resolved :=
956 Resolve_Array_Aggregate
958 Index => First_Index (Aggr_Typ),
959 Index_Constr => First_Index (Typ),
960 Component_Typ => Component_Type (Typ),
961 Others_Allowed => True);
963 else
964 Aggr_Resolved :=
965 Resolve_Array_Aggregate
967 Index => First_Index (Aggr_Typ),
968 Index_Constr => First_Index (Aggr_Typ),
969 Component_Typ => Component_Type (Typ),
970 Others_Allowed => False);
971 end if;
973 if not Aggr_Resolved then
974 Aggr_Subtyp := Any_Composite;
975 else
976 Aggr_Subtyp := Array_Aggr_Subtype (N, Typ);
977 end if;
979 Set_Etype (N, Aggr_Subtyp);
980 end Array_Aggregate;
982 else
983 Error_Msg_N ("illegal context for aggregate", N);
985 end if;
987 -- If we can determine statically that the evaluation of the
988 -- aggregate raises Constraint_Error, then replace the
989 -- aggregate with an N_Raise_Constraint_Error node, but set the
990 -- Etype to the right aggregate subtype. Gigi needs this.
992 if Raises_Constraint_Error (N) then
993 Aggr_Subtyp := Etype (N);
994 Rewrite (N,
995 Make_Raise_Constraint_Error (Sloc (N),
996 Reason => CE_Range_Check_Failed));
997 Set_Raises_Constraint_Error (N);
998 Set_Etype (N, Aggr_Subtyp);
999 Set_Analyzed (N);
1000 end if;
1002 end Resolve_Aggregate;
1004 -----------------------------
1005 -- Resolve_Array_Aggregate --
1006 -----------------------------
1008 function Resolve_Array_Aggregate
1009 (N : Node_Id;
1010 Index : Node_Id;
1011 Index_Constr : Node_Id;
1012 Component_Typ : Entity_Id;
1013 Others_Allowed : Boolean)
1014 return Boolean
1016 Loc : constant Source_Ptr := Sloc (N);
1018 Failure : constant Boolean := False;
1019 Success : constant Boolean := True;
1021 Index_Typ : constant Entity_Id := Etype (Index);
1022 Index_Typ_Low : constant Node_Id := Type_Low_Bound (Index_Typ);
1023 Index_Typ_High : constant Node_Id := Type_High_Bound (Index_Typ);
1024 -- The type of the index corresponding to the array sub-aggregate
1025 -- along with its low and upper bounds
1027 Index_Base : constant Entity_Id := Base_Type (Index_Typ);
1028 Index_Base_Low : constant Node_Id := Type_Low_Bound (Index_Base);
1029 Index_Base_High : constant Node_Id := Type_High_Bound (Index_Base);
1030 -- ditto for the base type
1032 function Add (Val : Uint; To : Node_Id) return Node_Id;
1033 -- Creates a new expression node where Val is added to expression To.
1034 -- Tries to constant fold whenever possible. To must be an already
1035 -- analyzed expression.
1037 procedure Check_Bound (BH : Node_Id; AH : in out Node_Id);
1038 -- Checks that AH (the upper bound of an array aggregate) is <= BH
1039 -- (the upper bound of the index base type). If the check fails a
1040 -- warning is emitted, the Raises_Constraint_Error Flag of N is set,
1041 -- and AH is replaced with a duplicate of BH.
1043 procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id);
1044 -- Checks that range AL .. AH is compatible with range L .. H. Emits a
1045 -- warning if not and sets the Raises_Constraint_Error Flag in N.
1047 procedure Check_Length (L, H : Node_Id; Len : Uint);
1048 -- Checks that range L .. H contains at least Len elements. Emits a
1049 -- warning if not and sets the Raises_Constraint_Error Flag in N.
1051 function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean;
1052 -- Returns True if range L .. H is dynamic or null.
1054 procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean);
1055 -- Given expression node From, this routine sets OK to False if it
1056 -- cannot statically evaluate From. Otherwise it stores this static
1057 -- value into Value.
1059 function Resolve_Aggr_Expr
1060 (Expr : Node_Id;
1061 Single_Elmt : Boolean)
1062 return Boolean;
1063 -- Resolves aggregate expression Expr. Returs False if resolution
1064 -- fails. If Single_Elmt is set to False, the expression Expr may be
1065 -- used to initialize several array aggregate elements (this can
1066 -- happen for discrete choices such as "L .. H => Expr" or the others
1067 -- choice). In this event we do not resolve Expr unless expansion is
1068 -- disabled. To know why, see the DELAYED COMPONENT RESOLUTION
1069 -- note above.
1071 ---------
1072 -- Add --
1073 ---------
1075 function Add (Val : Uint; To : Node_Id) return Node_Id is
1076 Expr_Pos : Node_Id;
1077 Expr : Node_Id;
1078 To_Pos : Node_Id;
1080 begin
1081 if Raises_Constraint_Error (To) then
1082 return To;
1083 end if;
1085 -- First test if we can do constant folding
1087 if Compile_Time_Known_Value (To)
1088 or else Nkind (To) = N_Integer_Literal
1089 then
1090 Expr_Pos := Make_Integer_Literal (Loc, Expr_Value (To) + Val);
1091 Set_Is_Static_Expression (Expr_Pos);
1092 Set_Etype (Expr_Pos, Etype (To));
1093 Set_Analyzed (Expr_Pos, Analyzed (To));
1095 if not Is_Enumeration_Type (Index_Typ) then
1096 Expr := Expr_Pos;
1098 -- If we are dealing with enumeration return
1099 -- Index_Typ'Val (Expr_Pos)
1101 else
1102 Expr :=
1103 Make_Attribute_Reference
1104 (Loc,
1105 Prefix => New_Reference_To (Index_Typ, Loc),
1106 Attribute_Name => Name_Val,
1107 Expressions => New_List (Expr_Pos));
1108 end if;
1110 return Expr;
1111 end if;
1113 -- If we are here no constant folding possible
1115 if not Is_Enumeration_Type (Index_Base) then
1116 Expr :=
1117 Make_Op_Add (Loc,
1118 Left_Opnd => Duplicate_Subexpr (To),
1119 Right_Opnd => Make_Integer_Literal (Loc, Val));
1121 -- If we are dealing with enumeration return
1122 -- Index_Typ'Val (Index_Typ'Pos (To) + Val)
1124 else
1125 To_Pos :=
1126 Make_Attribute_Reference
1127 (Loc,
1128 Prefix => New_Reference_To (Index_Typ, Loc),
1129 Attribute_Name => Name_Pos,
1130 Expressions => New_List (Duplicate_Subexpr (To)));
1132 Expr_Pos :=
1133 Make_Op_Add (Loc,
1134 Left_Opnd => To_Pos,
1135 Right_Opnd => Make_Integer_Literal (Loc, Val));
1137 Expr :=
1138 Make_Attribute_Reference
1139 (Loc,
1140 Prefix => New_Reference_To (Index_Typ, Loc),
1141 Attribute_Name => Name_Val,
1142 Expressions => New_List (Expr_Pos));
1143 end if;
1145 return Expr;
1146 end Add;
1148 -----------------
1149 -- Check_Bound --
1150 -----------------
1152 procedure Check_Bound (BH : Node_Id; AH : in out Node_Id) is
1153 Val_BH : Uint;
1154 Val_AH : Uint;
1156 OK_BH : Boolean;
1157 OK_AH : Boolean;
1159 begin
1160 Get (Value => Val_BH, From => BH, OK => OK_BH);
1161 Get (Value => Val_AH, From => AH, OK => OK_AH);
1163 if OK_BH and then OK_AH and then Val_BH < Val_AH then
1164 Set_Raises_Constraint_Error (N);
1165 Error_Msg_N ("upper bound out of range?", AH);
1166 Error_Msg_N ("Constraint_Error will be raised at run-time?", AH);
1168 -- You need to set AH to BH or else in the case of enumerations
1169 -- indices we will not be able to resolve the aggregate bounds.
1171 AH := Duplicate_Subexpr (BH);
1172 end if;
1173 end Check_Bound;
1175 ------------------
1176 -- Check_Bounds --
1177 ------------------
1179 procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id) is
1180 Val_L : Uint;
1181 Val_H : Uint;
1182 Val_AL : Uint;
1183 Val_AH : Uint;
1185 OK_L : Boolean;
1186 OK_H : Boolean;
1187 OK_AL : Boolean;
1188 OK_AH : Boolean;
1190 begin
1191 if Raises_Constraint_Error (N)
1192 or else Dynamic_Or_Null_Range (AL, AH)
1193 then
1194 return;
1195 end if;
1197 Get (Value => Val_L, From => L, OK => OK_L);
1198 Get (Value => Val_H, From => H, OK => OK_H);
1200 Get (Value => Val_AL, From => AL, OK => OK_AL);
1201 Get (Value => Val_AH, From => AH, OK => OK_AH);
1203 if OK_L and then Val_L > Val_AL then
1204 Set_Raises_Constraint_Error (N);
1205 Error_Msg_N ("lower bound of aggregate out of range?", N);
1206 Error_Msg_N ("Constraint_Error will be raised at run-time?", N);
1207 end if;
1209 if OK_H and then Val_H < Val_AH then
1210 Set_Raises_Constraint_Error (N);
1211 Error_Msg_N ("upper bound of aggregate out of range?", N);
1212 Error_Msg_N ("Constraint_Error will be raised at run-time?", N);
1213 end if;
1214 end Check_Bounds;
1216 ------------------
1217 -- Check_Length --
1218 ------------------
1220 procedure Check_Length (L, H : Node_Id; Len : Uint) is
1221 Val_L : Uint;
1222 Val_H : Uint;
1224 OK_L : Boolean;
1225 OK_H : Boolean;
1227 Range_Len : Uint;
1229 begin
1230 if Raises_Constraint_Error (N) then
1231 return;
1232 end if;
1234 Get (Value => Val_L, From => L, OK => OK_L);
1235 Get (Value => Val_H, From => H, OK => OK_H);
1237 if not OK_L or else not OK_H then
1238 return;
1239 end if;
1241 -- If null range length is zero
1243 if Val_L > Val_H then
1244 Range_Len := Uint_0;
1245 else
1246 Range_Len := Val_H - Val_L + 1;
1247 end if;
1249 if Range_Len < Len then
1250 Set_Raises_Constraint_Error (N);
1251 Error_Msg_N ("Too many elements?", N);
1252 Error_Msg_N ("Constraint_Error will be raised at run-time?", N);
1253 end if;
1254 end Check_Length;
1256 ---------------------------
1257 -- Dynamic_Or_Null_Range --
1258 ---------------------------
1260 function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean is
1261 Val_L : Uint;
1262 Val_H : Uint;
1264 OK_L : Boolean;
1265 OK_H : Boolean;
1267 begin
1268 Get (Value => Val_L, From => L, OK => OK_L);
1269 Get (Value => Val_H, From => H, OK => OK_H);
1271 return not OK_L or else not OK_H
1272 or else not Is_OK_Static_Expression (L)
1273 or else not Is_OK_Static_Expression (H)
1274 or else Val_L > Val_H;
1275 end Dynamic_Or_Null_Range;
1277 ---------
1278 -- Get --
1279 ---------
1281 procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean) is
1282 begin
1283 OK := True;
1285 if Compile_Time_Known_Value (From) then
1286 Value := Expr_Value (From);
1288 -- If expression From is something like Some_Type'Val (10) then
1289 -- Value = 10
1291 elsif Nkind (From) = N_Attribute_Reference
1292 and then Attribute_Name (From) = Name_Val
1293 and then Compile_Time_Known_Value (First (Expressions (From)))
1294 then
1295 Value := Expr_Value (First (Expressions (From)));
1297 else
1298 Value := Uint_0;
1299 OK := False;
1300 end if;
1301 end Get;
1303 -----------------------
1304 -- Resolve_Aggr_Expr --
1305 -----------------------
1307 function Resolve_Aggr_Expr
1308 (Expr : Node_Id;
1309 Single_Elmt : Boolean)
1310 return Boolean
1312 Nxt_Ind : Node_Id := Next_Index (Index);
1313 Nxt_Ind_Constr : Node_Id := Next_Index (Index_Constr);
1314 -- Index is the current index corresponding to the expression.
1316 Resolution_OK : Boolean := True;
1317 -- Set to False if resolution of the expression failed.
1319 begin
1320 -- If the array type against which we are resolving the aggregate
1321 -- has several dimensions, the expressions nested inside the
1322 -- aggregate must be further aggregates (or strings).
1324 if Present (Nxt_Ind) then
1325 if Nkind (Expr) /= N_Aggregate then
1327 -- A string literal can appear where a one-dimensional array
1328 -- of characters is expected. If the literal looks like an
1329 -- operator, it is still an operator symbol, which will be
1330 -- transformed into a string when analyzed.
1332 if Is_Character_Type (Component_Typ)
1333 and then No (Next_Index (Nxt_Ind))
1334 and then (Nkind (Expr) = N_String_Literal
1335 or else Nkind (Expr) = N_Operator_Symbol)
1336 then
1337 -- A string literal used in a multidimensional array
1338 -- aggregate in place of the final one-dimensional
1339 -- aggregate must not be enclosed in parentheses.
1341 if Paren_Count (Expr) /= 0 then
1342 Error_Msg_N ("No parenthesis allowed here", Expr);
1343 end if;
1345 Make_String_Into_Aggregate (Expr);
1347 else
1348 Error_Msg_N ("nested array aggregate expected", Expr);
1349 return Failure;
1350 end if;
1351 end if;
1353 Resolution_OK := Resolve_Array_Aggregate
1354 (Expr, Nxt_Ind, Nxt_Ind_Constr, Component_Typ, Others_Allowed);
1356 -- Do not resolve the expressions of discrete or others choices
1357 -- unless the expression covers a single component, or the expander
1358 -- is inactive.
1360 elsif Single_Elmt
1361 or else not Expander_Active
1362 or else In_Default_Expression
1363 then
1364 Analyze_And_Resolve (Expr, Component_Typ);
1365 Check_Non_Static_Context (Expr);
1366 Aggregate_Constraint_Checks (Expr, Component_Typ);
1367 end if;
1369 if Raises_Constraint_Error (Expr)
1370 and then Nkind (Parent (Expr)) /= N_Component_Association
1371 then
1372 Set_Raises_Constraint_Error (N);
1373 end if;
1375 return Resolution_OK;
1376 end Resolve_Aggr_Expr;
1378 -- Variables local to Resolve_Array_Aggregate
1380 Assoc : Node_Id;
1381 Choice : Node_Id;
1382 Expr : Node_Id;
1384 Who_Cares : Node_Id;
1386 Aggr_Low : Node_Id := Empty;
1387 Aggr_High : Node_Id := Empty;
1388 -- The actual low and high bounds of this sub-aggegate
1390 Choices_Low : Node_Id := Empty;
1391 Choices_High : Node_Id := Empty;
1392 -- The lowest and highest discrete choices values for a named aggregate
1394 Nb_Elements : Uint := Uint_0;
1395 -- The number of elements in a positional aggegate
1397 Others_Present : Boolean := False;
1399 Nb_Choices : Nat := 0;
1400 -- Contains the overall number of named choices in this sub-aggregate
1402 Nb_Discrete_Choices : Nat := 0;
1403 -- The overall number of discrete choices (not counting others choice)
1405 Case_Table_Size : Nat;
1406 -- Contains the size of the case table needed to sort aggregate choices
1408 -- Start of processing for Resolve_Array_Aggregate
1410 begin
1411 -- STEP 1: make sure the aggregate is correctly formatted
1413 if Present (Component_Associations (N)) then
1414 Assoc := First (Component_Associations (N));
1415 while Present (Assoc) loop
1416 Choice := First (Choices (Assoc));
1417 while Present (Choice) loop
1418 if Nkind (Choice) = N_Others_Choice then
1419 Others_Present := True;
1421 if Choice /= First (Choices (Assoc))
1422 or else Present (Next (Choice))
1423 then
1424 Error_Msg_N
1425 ("OTHERS must appear alone in a choice list", Choice);
1426 return Failure;
1427 end if;
1429 if Present (Next (Assoc)) then
1430 Error_Msg_N
1431 ("OTHERS must appear last in an aggregate", Choice);
1432 return Failure;
1433 end if;
1435 if Ada_83
1436 and then Assoc /= First (Component_Associations (N))
1437 and then (Nkind (Parent (N)) = N_Assignment_Statement
1438 or else
1439 Nkind (Parent (N)) = N_Object_Declaration)
1440 then
1441 Error_Msg_N
1442 ("(Ada 83) illegal context for OTHERS choice", N);
1443 end if;
1444 end if;
1446 Nb_Choices := Nb_Choices + 1;
1447 Next (Choice);
1448 end loop;
1450 Next (Assoc);
1451 end loop;
1452 end if;
1454 -- At this point we know that the others choice, if present, is by
1455 -- itself and appears last in the aggregate. Check if we have mixed
1456 -- positional and discrete associations (other than the others choice).
1458 if Present (Expressions (N))
1459 and then (Nb_Choices > 1
1460 or else (Nb_Choices = 1 and then not Others_Present))
1461 then
1462 Error_Msg_N
1463 ("named association cannot follow positional association",
1464 First (Choices (First (Component_Associations (N)))));
1465 return Failure;
1466 end if;
1468 -- Test for the validity of an others choice if present
1470 if Others_Present and then not Others_Allowed then
1471 Error_Msg_N
1472 ("OTHERS choice not allowed here",
1473 First (Choices (First (Component_Associations (N)))));
1474 return Failure;
1475 end if;
1477 -- Protect against cascaded errors
1479 if Etype (Index_Typ) = Any_Type then
1480 return Failure;
1481 end if;
1483 -- STEP 2: Process named components
1485 if No (Expressions (N)) then
1487 if Others_Present then
1488 Case_Table_Size := Nb_Choices - 1;
1489 else
1490 Case_Table_Size := Nb_Choices;
1491 end if;
1493 Step_2 : declare
1494 Low : Node_Id;
1495 High : Node_Id;
1496 -- Denote the lowest and highest values in an aggregate choice
1498 Hi_Val : Uint;
1499 Lo_Val : Uint;
1500 -- High end of one range and Low end of the next. Should be
1501 -- contiguous if there is no hole in the list of values.
1503 Missing_Values : Boolean;
1504 -- Set True if missing index values
1506 S_Low : Node_Id := Empty;
1507 S_High : Node_Id := Empty;
1508 -- if a choice in an aggregate is a subtype indication these
1509 -- denote the lowest and highest values of the subtype
1511 Table : Case_Table_Type (1 .. Case_Table_Size);
1512 -- Used to sort all the different choice values
1514 Single_Choice : Boolean;
1515 -- Set to true every time there is a single discrete choice in a
1516 -- discrete association
1518 Prev_Nb_Discrete_Choices : Nat;
1519 -- Used to keep track of the number of discrete choices
1520 -- in the current association.
1522 begin
1523 -- STEP 2 (A): Check discrete choices validity.
1525 Assoc := First (Component_Associations (N));
1526 while Present (Assoc) loop
1528 Prev_Nb_Discrete_Choices := Nb_Discrete_Choices;
1529 Choice := First (Choices (Assoc));
1530 loop
1531 Analyze (Choice);
1533 if Nkind (Choice) = N_Others_Choice then
1534 Single_Choice := False;
1535 exit;
1537 -- Test for subtype mark without constraint
1539 elsif Is_Entity_Name (Choice) and then
1540 Is_Type (Entity (Choice))
1541 then
1542 if Base_Type (Entity (Choice)) /= Index_Base then
1543 Error_Msg_N
1544 ("invalid subtype mark in aggregate choice",
1545 Choice);
1546 return Failure;
1547 end if;
1549 elsif Nkind (Choice) = N_Subtype_Indication then
1550 Resolve_Discrete_Subtype_Indication (Choice, Index_Base);
1552 -- Does the subtype indication evaluation raise CE ?
1554 Get_Index_Bounds (Subtype_Mark (Choice), S_Low, S_High);
1555 Get_Index_Bounds (Choice, Low, High);
1556 Check_Bounds (S_Low, S_High, Low, High);
1558 else -- Choice is a range or an expression
1559 Resolve (Choice, Index_Base);
1560 Check_Non_Static_Context (Choice);
1562 -- Do not range check a choice. This check is redundant
1563 -- since this test is already performed when we check
1564 -- that the bounds of the array aggregate are within
1565 -- range.
1567 Set_Do_Range_Check (Choice, False);
1568 end if;
1570 -- If we could not resolve the discrete choice stop here
1572 if Etype (Choice) = Any_Type then
1573 return Failure;
1575 -- If the discrete choice raises CE get its original bounds.
1577 elsif Nkind (Choice) = N_Raise_Constraint_Error then
1578 Set_Raises_Constraint_Error (N);
1579 Get_Index_Bounds (Original_Node (Choice), Low, High);
1581 -- Otherwise get its bounds as usual
1583 else
1584 Get_Index_Bounds (Choice, Low, High);
1585 end if;
1587 if (Dynamic_Or_Null_Range (Low, High)
1588 or else (Nkind (Choice) = N_Subtype_Indication
1589 and then
1590 Dynamic_Or_Null_Range (S_Low, S_High)))
1591 and then Nb_Choices /= 1
1592 then
1593 Error_Msg_N
1594 ("dynamic or empty choice in aggregate " &
1595 "must be the only choice", Choice);
1596 return Failure;
1597 end if;
1599 Nb_Discrete_Choices := Nb_Discrete_Choices + 1;
1600 Table (Nb_Discrete_Choices).Choice_Lo := Low;
1601 Table (Nb_Discrete_Choices).Choice_Hi := High;
1603 Next (Choice);
1605 if No (Choice) then
1606 -- Check if we have a single discrete choice and whether
1607 -- this discrete choice specifies a single value.
1609 Single_Choice :=
1610 (Nb_Discrete_Choices = Prev_Nb_Discrete_Choices + 1)
1611 and then (Low = High);
1613 exit;
1614 end if;
1615 end loop;
1617 if not
1618 Resolve_Aggr_Expr
1619 (Expression (Assoc), Single_Elmt => Single_Choice)
1620 then
1621 return Failure;
1622 end if;
1624 Next (Assoc);
1625 end loop;
1627 -- If aggregate contains more than one choice then these must be
1628 -- static. Sort them and check that they are contiguous
1630 if Nb_Discrete_Choices > 1 then
1631 Sort_Case_Table (Table);
1632 Missing_Values := False;
1634 Outer : for J in 1 .. Nb_Discrete_Choices - 1 loop
1635 if Expr_Value (Table (J).Choice_Hi) >=
1636 Expr_Value (Table (J + 1).Choice_Lo)
1637 then
1638 Error_Msg_N
1639 ("duplicate choice values in array aggregate",
1640 Table (J).Choice_Hi);
1641 return Failure;
1643 elsif not Others_Present then
1645 Hi_Val := Expr_Value (Table (J).Choice_Hi);
1646 Lo_Val := Expr_Value (Table (J + 1).Choice_Lo);
1648 -- If missing values, output error messages
1650 if Lo_Val - Hi_Val > 1 then
1652 -- Header message if not first missing value
1654 if not Missing_Values then
1655 Error_Msg_N
1656 ("missing index value(s) in array aggregate", N);
1657 Missing_Values := True;
1658 end if;
1660 -- Output values of missing indexes
1662 Lo_Val := Lo_Val - 1;
1663 Hi_Val := Hi_Val + 1;
1665 -- Enumeration type case
1667 if Is_Enumeration_Type (Index_Typ) then
1668 Error_Msg_Name_1 :=
1669 Chars
1670 (Get_Enum_Lit_From_Pos
1671 (Index_Typ, Hi_Val, Loc));
1673 if Lo_Val = Hi_Val then
1674 Error_Msg_N ("\ %", N);
1675 else
1676 Error_Msg_Name_2 :=
1677 Chars
1678 (Get_Enum_Lit_From_Pos
1679 (Index_Typ, Lo_Val, Loc));
1680 Error_Msg_N ("\ % .. %", N);
1681 end if;
1683 -- Integer types case
1685 else
1686 Error_Msg_Uint_1 := Hi_Val;
1688 if Lo_Val = Hi_Val then
1689 Error_Msg_N ("\ ^", N);
1690 else
1691 Error_Msg_Uint_2 := Lo_Val;
1692 Error_Msg_N ("\ ^ .. ^", N);
1693 end if;
1694 end if;
1695 end if;
1696 end if;
1697 end loop Outer;
1699 if Missing_Values then
1700 Set_Etype (N, Any_Composite);
1701 return Failure;
1702 end if;
1703 end if;
1705 -- STEP 2 (B): Compute aggregate bounds and min/max choices values
1707 if Nb_Discrete_Choices > 0 then
1708 Choices_Low := Table (1).Choice_Lo;
1709 Choices_High := Table (Nb_Discrete_Choices).Choice_Hi;
1710 end if;
1712 if Others_Present then
1713 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
1715 else
1716 Aggr_Low := Choices_Low;
1717 Aggr_High := Choices_High;
1718 end if;
1719 end Step_2;
1721 -- STEP 3: Process positional components
1723 else
1724 -- STEP 3 (A): Process positional elements
1726 Expr := First (Expressions (N));
1727 Nb_Elements := Uint_0;
1728 while Present (Expr) loop
1729 Nb_Elements := Nb_Elements + 1;
1731 if not Resolve_Aggr_Expr (Expr, Single_Elmt => True) then
1732 return Failure;
1733 end if;
1735 Next (Expr);
1736 end loop;
1738 if Others_Present then
1739 Assoc := Last (Component_Associations (N));
1740 if not Resolve_Aggr_Expr (Expression (Assoc),
1741 Single_Elmt => False)
1742 then
1743 return Failure;
1744 end if;
1745 end if;
1747 -- STEP 3 (B): Compute the aggregate bounds
1749 if Others_Present then
1750 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
1752 else
1753 if Others_Allowed then
1754 Get_Index_Bounds (Index_Constr, Aggr_Low, Who_Cares);
1755 else
1756 Aggr_Low := Index_Typ_Low;
1757 end if;
1759 Aggr_High := Add (Nb_Elements - 1, To => Aggr_Low);
1760 Check_Bound (Index_Base_High, Aggr_High);
1761 end if;
1762 end if;
1764 -- STEP 4: Perform static aggregate checks and save the bounds
1766 -- Check (A)
1768 Check_Bounds (Index_Typ_Low, Index_Typ_High, Aggr_Low, Aggr_High);
1769 Check_Bounds (Index_Base_Low, Index_Base_High, Aggr_Low, Aggr_High);
1771 -- Check (B)
1773 if Others_Present and then Nb_Discrete_Choices > 0 then
1774 Check_Bounds (Aggr_Low, Aggr_High, Choices_Low, Choices_High);
1775 Check_Bounds (Index_Typ_Low, Index_Typ_High,
1776 Choices_Low, Choices_High);
1777 Check_Bounds (Index_Base_Low, Index_Base_High,
1778 Choices_Low, Choices_High);
1780 -- Check (C)
1782 elsif Others_Present and then Nb_Elements > 0 then
1783 Check_Length (Aggr_Low, Aggr_High, Nb_Elements);
1784 Check_Length (Index_Typ_Low, Index_Typ_High, Nb_Elements);
1785 Check_Length (Index_Base_Low, Index_Base_High, Nb_Elements);
1787 end if;
1789 if Raises_Constraint_Error (Aggr_Low)
1790 or else Raises_Constraint_Error (Aggr_High)
1791 then
1792 Set_Raises_Constraint_Error (N);
1793 end if;
1795 Aggr_Low := Duplicate_Subexpr (Aggr_Low);
1797 -- Do not duplicate Aggr_High if Aggr_High = Aggr_Low + Nb_Elements
1798 -- since the addition node returned by Add is not yet analyzed. Attach
1799 -- to tree and analyze first. Reset analyzed flag to insure it will get
1800 -- analyzed when it is a literal bound whose type must be properly
1801 -- set.
1803 if Others_Present or else Nb_Discrete_Choices > 0 then
1804 Aggr_High := Duplicate_Subexpr (Aggr_High);
1806 if Etype (Aggr_High) = Universal_Integer then
1807 Set_Analyzed (Aggr_High, False);
1808 end if;
1809 end if;
1811 Set_Aggregate_Bounds
1812 (N, Make_Range (Loc, Low_Bound => Aggr_Low, High_Bound => Aggr_High));
1814 -- The bounds may contain expressions that must be inserted upwards.
1815 -- Attach them fully to the tree. After analysis, remove side effects
1816 -- from upper bound, if still needed.
1818 Set_Parent (Aggregate_Bounds (N), N);
1819 Analyze_And_Resolve (Aggregate_Bounds (N), Index_Typ);
1821 if not Others_Present and then Nb_Discrete_Choices = 0 then
1822 Set_High_Bound (Aggregate_Bounds (N),
1823 Duplicate_Subexpr (High_Bound (Aggregate_Bounds (N))));
1824 end if;
1826 return Success;
1827 end Resolve_Array_Aggregate;
1829 ---------------------------------
1830 -- Resolve_Extension_Aggregate --
1831 ---------------------------------
1833 -- There are two cases to consider:
1835 -- a) If the ancestor part is a type mark, the components needed are
1836 -- the difference between the components of the expected type and the
1837 -- components of the given type mark.
1839 -- b) If the ancestor part is an expression, it must be unambiguous,
1840 -- and once we have its type we can also compute the needed components
1841 -- as in the previous case. In both cases, if the ancestor type is not
1842 -- the immediate ancestor, we have to build this ancestor recursively.
1844 -- In both cases discriminants of the ancestor type do not play a
1845 -- role in the resolution of the needed components, because inherited
1846 -- discriminants cannot be used in a type extension. As a result we can
1847 -- compute independently the list of components of the ancestor type and
1848 -- of the expected type.
1850 procedure Resolve_Extension_Aggregate (N : Node_Id; Typ : Entity_Id) is
1851 A : constant Node_Id := Ancestor_Part (N);
1852 A_Type : Entity_Id;
1853 I : Interp_Index;
1854 It : Interp;
1855 Imm_Type : Entity_Id;
1857 function Valid_Ancestor_Type return Boolean;
1858 -- Verify that the type of the ancestor part is a non-private ancestor
1859 -- of the expected type.
1861 function Valid_Ancestor_Type return Boolean is
1862 Imm_Type : Entity_Id;
1864 begin
1865 Imm_Type := Base_Type (Typ);
1866 while Is_Derived_Type (Imm_Type)
1867 and then Etype (Imm_Type) /= Base_Type (A_Type)
1868 loop
1869 Imm_Type := Etype (Base_Type (Imm_Type));
1870 end loop;
1872 if Etype (Imm_Type) /= Base_Type (A_Type) then
1873 Error_Msg_NE ("expect ancestor type of &", A, Typ);
1874 return False;
1875 else
1876 return True;
1877 end if;
1878 end Valid_Ancestor_Type;
1880 -- Start of processing for Resolve_Extension_Aggregate
1882 begin
1883 Analyze (A);
1885 if not Is_Tagged_Type (Typ) then
1886 Error_Msg_N ("type of extension aggregate must be tagged", N);
1887 return;
1889 elsif Is_Limited_Type (Typ) then
1890 Error_Msg_N ("aggregate type cannot be limited", N);
1891 return;
1893 elsif Is_Class_Wide_Type (Typ) then
1894 Error_Msg_N ("aggregate cannot be of a class-wide type", N);
1895 return;
1896 end if;
1898 if Is_Entity_Name (A)
1899 and then Is_Type (Entity (A))
1900 then
1901 A_Type := Get_Full_View (Entity (A));
1902 Imm_Type := Base_Type (Typ);
1904 if Valid_Ancestor_Type then
1905 Set_Entity (A, A_Type);
1906 Set_Etype (A, A_Type);
1908 Validate_Ancestor_Part (N);
1909 Resolve_Record_Aggregate (N, Typ);
1910 end if;
1912 elsif Nkind (A) /= N_Aggregate then
1913 if Is_Overloaded (A) then
1914 A_Type := Any_Type;
1915 Get_First_Interp (A, I, It);
1917 while Present (It.Typ) loop
1919 if Is_Tagged_Type (It.Typ)
1920 and then not Is_Limited_Type (It.Typ)
1921 then
1922 if A_Type /= Any_Type then
1923 Error_Msg_N ("cannot resolve expression", A);
1924 return;
1925 else
1926 A_Type := It.Typ;
1927 end if;
1928 end if;
1930 Get_Next_Interp (I, It);
1931 end loop;
1933 if A_Type = Any_Type then
1934 Error_Msg_N
1935 ("ancestor part must be non-limited tagged type", A);
1936 return;
1937 end if;
1939 else
1940 A_Type := Etype (A);
1941 end if;
1943 if Valid_Ancestor_Type then
1944 Resolve (A, A_Type);
1945 Check_Non_Static_Context (A);
1946 Resolve_Record_Aggregate (N, Typ);
1947 end if;
1949 else
1950 Error_Msg_N (" No unique type for this aggregate", A);
1951 end if;
1953 end Resolve_Extension_Aggregate;
1955 ------------------------------
1956 -- Resolve_Record_Aggregate --
1957 ------------------------------
1959 procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id) is
1960 Regular_Aggr : constant Boolean := Nkind (N) /= N_Extension_Aggregate;
1962 New_Assoc_List : List_Id := New_List;
1963 New_Assoc : Node_Id;
1964 -- New_Assoc_List is the newly built list of N_Component_Association
1965 -- nodes. New_Assoc is one such N_Component_Association node in it.
1966 -- Please note that while Assoc and New_Assoc contain the same
1967 -- kind of nodes, they are used to iterate over two different
1968 -- N_Component_Association lists.
1970 Others_Etype : Entity_Id := Empty;
1971 -- This variable is used to save the Etype of the last record component
1972 -- that takes its value from the others choice. Its purpose is:
1974 -- (a) make sure the others choice is useful
1976 -- (b) make sure the type of all the components whose value is
1977 -- subsumed by the others choice are the same.
1979 -- This variable is updated as a side effect of function Get_Value
1981 procedure Add_Association (Component : Entity_Id; Expr : Node_Id);
1982 -- Builds a new N_Component_Association node which associates
1983 -- Component to expression Expr and adds it to the new association
1984 -- list New_Assoc_List being built.
1986 function Discr_Present (Discr : Entity_Id) return Boolean;
1987 -- If aggregate N is a regular aggregate this routine will return True.
1988 -- Otherwise, if N is an extension aggreagte, Discr is a discriminant
1989 -- whose value may already have been specified by N's ancestor part,
1990 -- this routine checks whether this is indeed the case and if so
1991 -- returns False, signaling that no value for Discr should appear in the
1992 -- N's aggregate part. Also, in this case, the routine appends to
1993 -- New_Assoc_List Discr the discriminant value specified in the ancestor
1994 -- part.
1996 function Get_Value
1997 (Compon : Node_Id;
1998 From : List_Id;
1999 Consider_Others_Choice : Boolean := False)
2000 return Node_Id;
2001 -- Given a record component stored in parameter Compon, the
2002 -- following function returns its value as it appears in the list
2003 -- From, which is a list of N_Component_Association nodes. If no
2004 -- component association has a choice for the searched component,
2005 -- the value provided by the others choice is returned, if there
2006 -- is one and Consider_Others_Choice is set to true. Otherwise
2007 -- Empty is returned. If there is more than one component association
2008 -- giving a value for the searched record component, an error message
2009 -- is emitted and the first found value is returned.
2011 -- If Consider_Others_Choice is set and the returned expression comes
2012 -- from the others choice, then Others_Etype is set as a side effect.
2013 -- An error message is emitted if the components taking their value
2014 -- from the others choice do not have same type.
2016 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Node_Id);
2017 -- Analyzes and resolves expression Expr against the Etype of the
2018 -- Component. This routine also applies all appropriate checks to Expr.
2019 -- It finally saves a Expr in the newly created association list that
2020 -- will be attached to the final record aggregate. Note that if the
2021 -- Parent pointer of Expr is not set then Expr was produced with a
2022 -- New_copy_Tree or some such.
2024 ---------------------
2025 -- Add_Association --
2026 ---------------------
2028 procedure Add_Association (Component : Entity_Id; Expr : Node_Id) is
2029 New_Assoc : Node_Id;
2030 Choice_List : List_Id := New_List;
2032 begin
2033 Append (New_Occurrence_Of (Component, Sloc (Expr)), Choice_List);
2034 New_Assoc :=
2035 Make_Component_Association (Sloc (Expr),
2036 Choices => Choice_List,
2037 Expression => Expr);
2038 Append (New_Assoc, New_Assoc_List);
2039 end Add_Association;
2041 -------------------
2042 -- Discr_Present --
2043 -------------------
2045 function Discr_Present (Discr : Entity_Id) return Boolean is
2046 Loc : Source_Ptr;
2048 Ancestor : Node_Id;
2049 Discr_Expr : Node_Id;
2051 Ancestor_Typ : Entity_Id;
2052 Orig_Discr : Entity_Id;
2053 D : Entity_Id;
2054 D_Val : Elmt_Id := No_Elmt; -- stop junk warning
2056 Ancestor_Is_Subtyp : Boolean;
2058 begin
2059 if Regular_Aggr then
2060 return True;
2061 end if;
2063 Ancestor := Ancestor_Part (N);
2064 Ancestor_Typ := Etype (Ancestor);
2065 Loc := Sloc (Ancestor);
2067 Ancestor_Is_Subtyp :=
2068 Is_Entity_Name (Ancestor) and then Is_Type (Entity (Ancestor));
2070 -- If the ancestor part has no discriminants clearly N's aggregate
2071 -- part must provide a value for Discr.
2073 if not Has_Discriminants (Ancestor_Typ) then
2074 return True;
2076 -- If the ancestor part is an unconstrained subtype mark then the
2077 -- Discr must be present in N's aggregate part.
2079 elsif Ancestor_Is_Subtyp
2080 and then not Is_Constrained (Entity (Ancestor))
2081 then
2082 return True;
2083 end if;
2085 -- Now look to see if Discr was specified in the ancestor part.
2087 Orig_Discr := Original_Record_Component (Discr);
2088 D := First_Discriminant (Ancestor_Typ);
2090 if Ancestor_Is_Subtyp then
2091 D_Val := First_Elmt (Discriminant_Constraint (Entity (Ancestor)));
2092 end if;
2094 while Present (D) loop
2095 -- If Ancestor has already specified Disc value than
2096 -- insert its value in the final aggregate.
2098 if Original_Record_Component (D) = Orig_Discr then
2099 if Ancestor_Is_Subtyp then
2100 Discr_Expr := New_Copy_Tree (Node (D_Val));
2101 else
2102 Discr_Expr :=
2103 Make_Selected_Component (Loc,
2104 Prefix => Duplicate_Subexpr (Ancestor),
2105 Selector_Name => New_Occurrence_Of (Discr, Loc));
2106 end if;
2108 Resolve_Aggr_Expr (Discr_Expr, Discr);
2109 return False;
2110 end if;
2112 Next_Discriminant (D);
2114 if Ancestor_Is_Subtyp then
2115 Next_Elmt (D_Val);
2116 end if;
2117 end loop;
2119 return True;
2120 end Discr_Present;
2122 ---------------
2123 -- Get_Value --
2124 ---------------
2126 function Get_Value
2127 (Compon : Node_Id;
2128 From : List_Id;
2129 Consider_Others_Choice : Boolean := False)
2130 return Node_Id
2132 Assoc : Node_Id;
2133 Expr : Node_Id := Empty;
2134 Selector_Name : Node_Id;
2136 begin
2137 if Present (From) then
2138 Assoc := First (From);
2139 else
2140 return Empty;
2141 end if;
2143 while Present (Assoc) loop
2144 Selector_Name := First (Choices (Assoc));
2145 while Present (Selector_Name) loop
2146 if Nkind (Selector_Name) = N_Others_Choice then
2147 if Consider_Others_Choice and then No (Expr) then
2148 if Present (Others_Etype) and then
2149 Base_Type (Others_Etype) /= Base_Type (Etype (Compon))
2150 then
2151 Error_Msg_N ("components in OTHERS choice must " &
2152 "have same type", Selector_Name);
2153 end if;
2155 Others_Etype := Etype (Compon);
2157 -- We need to duplicate the expression for each
2158 -- successive component covered by the others choice.
2159 -- If the expression is itself an array aggregate with
2160 -- "others", its subtype must be obtained from the
2161 -- current component, and therefore it must be (at least
2162 -- partly) reanalyzed.
2164 if Analyzed (Expression (Assoc)) then
2165 Expr := New_Copy_Tree (Expression (Assoc));
2167 if Nkind (Expr) = N_Aggregate
2168 and then Is_Array_Type (Etype (Expr))
2169 and then No (Expressions (Expr))
2170 and then
2171 Nkind (First (Choices
2172 (First (Component_Associations (Expr)))))
2173 = N_Others_Choice
2174 then
2175 Set_Analyzed (Expr, False);
2176 end if;
2178 return Expr;
2180 else
2181 return Expression (Assoc);
2182 end if;
2183 end if;
2185 elsif Chars (Compon) = Chars (Selector_Name) then
2186 if No (Expr) then
2187 -- We need to duplicate the expression when several
2188 -- components are grouped together with a "|" choice.
2189 -- For instance "filed1 | filed2 => Expr"
2191 if Present (Next (Selector_Name)) then
2192 Expr := New_Copy_Tree (Expression (Assoc));
2193 else
2194 Expr := Expression (Assoc);
2195 end if;
2197 else
2198 Error_Msg_NE
2199 ("more than one value supplied for &",
2200 Selector_Name, Compon);
2202 end if;
2203 end if;
2205 Next (Selector_Name);
2206 end loop;
2208 Next (Assoc);
2209 end loop;
2211 return Expr;
2212 end Get_Value;
2214 -----------------------
2215 -- Resolve_Aggr_Expr --
2216 -----------------------
2218 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Node_Id) is
2219 New_C : Entity_Id := Component;
2220 Expr_Type : Entity_Id := Empty;
2222 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean;
2223 -- If the expression is an aggregate (possibly qualified) then its
2224 -- expansion is delayed until the enclosing aggregate is expanded
2225 -- into assignments. In that case, do not generate checks on the
2226 -- expression, because they will be generated later, and will other-
2227 -- wise force a copy (to remove side-effects) that would leave a
2228 -- dynamic-sized aggregate in the code, something that gigi cannot
2229 -- handle.
2231 Relocate : Boolean;
2232 -- Set to True if the resolved Expr node needs to be relocated
2233 -- when attached to the newly created association list. This node
2234 -- need not be relocated if its parent pointer is not set.
2235 -- In fact in this case Expr is the output of a New_Copy_Tree call.
2236 -- if Relocate is True then we have analyzed the expression node
2237 -- in the original aggregate and hence it needs to be relocated
2238 -- when moved over the new association list.
2240 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean is
2241 Kind : constant Node_Kind := Nkind (Expr);
2243 begin
2244 return ((Kind = N_Aggregate
2245 or else Kind = N_Extension_Aggregate)
2246 and then Present (Etype (Expr))
2247 and then Is_Record_Type (Etype (Expr))
2248 and then Expansion_Delayed (Expr))
2250 or else (Kind = N_Qualified_Expression
2251 and then Has_Expansion_Delayed (Expression (Expr)));
2252 end Has_Expansion_Delayed;
2254 -- Start of processing for Resolve_Aggr_Expr
2256 begin
2257 -- If the type of the component is elementary or the type of the
2258 -- aggregate does not contain discriminants, use the type of the
2259 -- component to resolve Expr.
2261 if Is_Elementary_Type (Etype (Component))
2262 or else not Has_Discriminants (Etype (N))
2263 then
2264 Expr_Type := Etype (Component);
2266 -- Otherwise we have to pick up the new type of the component from
2267 -- the new costrained subtype of the aggregate. In fact components
2268 -- which are of a composite type might be constrained by a
2269 -- discriminant, and we want to resolve Expr against the subtype were
2270 -- all discriminant occurrences are replaced with their actual value.
2272 else
2273 New_C := First_Component (Etype (N));
2274 while Present (New_C) loop
2275 if Chars (New_C) = Chars (Component) then
2276 Expr_Type := Etype (New_C);
2277 exit;
2278 end if;
2280 Next_Component (New_C);
2281 end loop;
2283 pragma Assert (Present (Expr_Type));
2285 -- For each range in an array type where a discriminant has been
2286 -- replaced with the constraint, check that this range is within
2287 -- the range of the base type. This checks is done in the
2288 -- _init_proc for regular objects, but has to be done here for
2289 -- aggregates since no _init_proc is called for them.
2291 if Is_Array_Type (Expr_Type) then
2292 declare
2293 Index : Node_Id := First_Index (Expr_Type);
2294 -- Range of the current constrained index in the array.
2296 Orig_Index : Node_Id := First_Index (Etype (Component));
2297 -- Range corresponding to the range Index above in the
2298 -- original unconstrained record type. The bounds of this
2299 -- range may be governed by discriminants.
2301 Unconstr_Index : Node_Id := First_Index (Etype (Expr_Type));
2302 -- Range corresponding to the range Index above for the
2303 -- unconstrained array type. This range is needed to apply
2304 -- range checks.
2306 begin
2307 while Present (Index) loop
2308 if Depends_On_Discriminant (Orig_Index) then
2309 Apply_Range_Check (Index, Etype (Unconstr_Index));
2310 end if;
2312 Next_Index (Index);
2313 Next_Index (Orig_Index);
2314 Next_Index (Unconstr_Index);
2315 end loop;
2316 end;
2317 end if;
2318 end if;
2320 -- If the Parent pointer of Expr is not set, Expr is an expression
2321 -- duplicated by New_Tree_Copy (this happens for record aggregates
2322 -- that look like (Field1 | Filed2 => Expr) or (others => Expr)).
2323 -- Such a duplicated expression must be attached to the tree
2324 -- before analysis and resolution to enforce the rule that a tree
2325 -- fragment should never be analyzed or resolved unless it is
2326 -- attached to the current compilation unit.
2328 if No (Parent (Expr)) then
2329 Set_Parent (Expr, N);
2330 Relocate := False;
2331 else
2332 Relocate := True;
2333 end if;
2335 Analyze_And_Resolve (Expr, Expr_Type);
2336 Check_Non_Static_Context (Expr);
2338 if not Has_Expansion_Delayed (Expr) then
2339 Aggregate_Constraint_Checks (Expr, Expr_Type);
2340 end if;
2342 if Raises_Constraint_Error (Expr) then
2343 Set_Raises_Constraint_Error (N);
2344 end if;
2346 if Relocate then
2347 Add_Association (New_C, Relocate_Node (Expr));
2348 else
2349 Add_Association (New_C, Expr);
2350 end if;
2352 end Resolve_Aggr_Expr;
2354 -- Resolve_Record_Aggregate local variables
2356 Assoc : Node_Id;
2357 -- N_Component_Association node belonging to the input aggregate N
2359 Expr : Node_Id;
2360 Positional_Expr : Node_Id;
2362 Component : Entity_Id;
2363 Component_Elmt : Elmt_Id;
2364 Components : Elist_Id := New_Elmt_List;
2365 -- Components is the list of the record components whose value must
2366 -- be provided in the aggregate. This list does include discriminants.
2368 -- Start of processing for Resolve_Record_Aggregate
2370 begin
2371 -- We may end up calling Duplicate_Subexpr on expressions that are
2372 -- attached to New_Assoc_List. For this reason we need to attach it
2373 -- to the tree by setting its parent pointer to N. This parent point
2374 -- will change in STEP 8 below.
2376 Set_Parent (New_Assoc_List, N);
2378 -- STEP 1: abstract type and null record verification
2380 if Is_Abstract (Typ) then
2381 Error_Msg_N ("type of aggregate cannot be abstract", N);
2382 end if;
2384 if No (First_Entity (Typ)) and then Null_Record_Present (N) then
2385 Set_Etype (N, Typ);
2386 return;
2388 elsif Present (First_Entity (Typ))
2389 and then Null_Record_Present (N)
2390 and then not Is_Tagged_Type (Typ)
2391 then
2392 Error_Msg_N ("record aggregate cannot be null", N);
2393 return;
2395 elsif No (First_Entity (Typ)) then
2396 Error_Msg_N ("record aggregate must be null", N);
2397 return;
2398 end if;
2400 -- STEP 2: Verify aggregate structure
2402 Step_2 : declare
2403 Selector_Name : Node_Id;
2404 Bad_Aggregate : Boolean := False;
2406 begin
2407 if Present (Component_Associations (N)) then
2408 Assoc := First (Component_Associations (N));
2409 else
2410 Assoc := Empty;
2411 end if;
2413 while Present (Assoc) loop
2414 Selector_Name := First (Choices (Assoc));
2415 while Present (Selector_Name) loop
2416 if Nkind (Selector_Name) = N_Identifier then
2417 null;
2419 elsif Nkind (Selector_Name) = N_Others_Choice then
2420 if Selector_Name /= First (Choices (Assoc))
2421 or else Present (Next (Selector_Name))
2422 then
2423 Error_Msg_N ("OTHERS must appear alone in a choice list",
2424 Selector_Name);
2425 return;
2427 elsif Present (Next (Assoc)) then
2428 Error_Msg_N ("OTHERS must appear last in an aggregate",
2429 Selector_Name);
2430 return;
2431 end if;
2433 else
2434 Error_Msg_N
2435 ("selector name should be identifier or OTHERS",
2436 Selector_Name);
2437 Bad_Aggregate := True;
2438 end if;
2440 Next (Selector_Name);
2441 end loop;
2443 Next (Assoc);
2444 end loop;
2446 if Bad_Aggregate then
2447 return;
2448 end if;
2449 end Step_2;
2451 -- STEP 3: Find discriminant Values
2453 Step_3 : declare
2454 Discrim : Entity_Id;
2455 Missing_Discriminants : Boolean := False;
2457 begin
2458 if Present (Expressions (N)) then
2459 Positional_Expr := First (Expressions (N));
2460 else
2461 Positional_Expr := Empty;
2462 end if;
2464 if Has_Discriminants (Typ) then
2465 Discrim := First_Discriminant (Typ);
2466 else
2467 Discrim := Empty;
2468 end if;
2470 -- First find the discriminant values in the positional components
2472 while Present (Discrim) and then Present (Positional_Expr) loop
2473 if Discr_Present (Discrim) then
2474 Resolve_Aggr_Expr (Positional_Expr, Discrim);
2475 Next (Positional_Expr);
2476 end if;
2478 if Present (Get_Value (Discrim, Component_Associations (N))) then
2479 Error_Msg_NE
2480 ("more than one value supplied for discriminant&",
2481 N, Discrim);
2482 end if;
2484 Next_Discriminant (Discrim);
2485 end loop;
2487 -- Find remaining discriminant values, if any, among named components
2489 while Present (Discrim) loop
2490 Expr := Get_Value (Discrim, Component_Associations (N), True);
2492 if not Discr_Present (Discrim) then
2493 if Present (Expr) then
2494 Error_Msg_NE
2495 ("more than one value supplied for discriminant&",
2496 N, Discrim);
2497 end if;
2499 elsif No (Expr) then
2500 Error_Msg_NE
2501 ("no value supplied for discriminant &", N, Discrim);
2502 Missing_Discriminants := True;
2504 else
2505 Resolve_Aggr_Expr (Expr, Discrim);
2506 end if;
2508 Next_Discriminant (Discrim);
2509 end loop;
2511 if Missing_Discriminants then
2512 return;
2513 end if;
2515 -- At this point and until the beginning of STEP 6, New_Assoc_List
2516 -- contains only the discriminants and their values.
2518 end Step_3;
2520 -- STEP 4: Set the Etype of the record aggregate
2522 -- ??? This code is pretty much a copy of Sem_Ch3.Build_Subtype. That
2523 -- routine should really be exported in sem_util or some such and used
2524 -- in sem_ch3 and here rather than have a copy of the code which is a
2525 -- maintenance nightmare.
2527 -- ??? Performace WARNING. The current implementation creates a new
2528 -- itype for all aggregates whose base type is discriminated.
2529 -- This means that for record aggregates nested inside an array
2530 -- aggregate we will create a new itype for each record aggregate
2531 -- if the array cmponent type has discriminants. For large aggregates
2532 -- this may be a problem. What should be done in this case is
2533 -- to reuse itypes as much as possible.
2535 if Has_Discriminants (Typ) then
2536 Build_Constrained_Itype : declare
2537 Loc : constant Source_Ptr := Sloc (N);
2538 Indic : Node_Id;
2539 Subtyp_Decl : Node_Id;
2540 Def_Id : Entity_Id;
2542 C : List_Id := New_List;
2544 begin
2545 New_Assoc := First (New_Assoc_List);
2546 while Present (New_Assoc) loop
2547 Append (Duplicate_Subexpr (Expression (New_Assoc)), To => C);
2548 Next (New_Assoc);
2549 end loop;
2551 Indic :=
2552 Make_Subtype_Indication (Loc,
2553 Subtype_Mark => New_Occurrence_Of (Base_Type (Typ), Loc),
2554 Constraint => Make_Index_Or_Discriminant_Constraint (Loc, C));
2556 Def_Id := Create_Itype (Ekind (Typ), N);
2558 Subtyp_Decl :=
2559 Make_Subtype_Declaration (Loc,
2560 Defining_Identifier => Def_Id,
2561 Subtype_Indication => Indic);
2562 Set_Parent (Subtyp_Decl, Parent (N));
2564 -- Itypes must be analyzed with checks off (see itypes.ads).
2566 Analyze (Subtyp_Decl, Suppress => All_Checks);
2568 Set_Etype (N, Def_Id);
2569 Check_Static_Discriminated_Subtype
2570 (Def_Id, Expression (First (New_Assoc_List)));
2571 end Build_Constrained_Itype;
2573 else
2574 Set_Etype (N, Typ);
2575 end if;
2577 -- STEP 5: Get remaining components according to discriminant values
2579 Step_5 : declare
2580 Record_Def : Node_Id;
2581 Parent_Typ : Entity_Id;
2582 Root_Typ : Entity_Id;
2583 Parent_Typ_List : Elist_Id;
2584 Parent_Elmt : Elmt_Id;
2585 Errors_Found : Boolean := False;
2586 Dnode : Node_Id;
2588 begin
2589 if Is_Derived_Type (Typ) and then Is_Tagged_Type (Typ) then
2590 Parent_Typ_List := New_Elmt_List;
2592 -- If this is an extension aggregate, the component list must
2593 -- include all components that are not in the given ancestor
2594 -- type. Otherwise, the component list must include components
2595 -- of all ancestors, starting with the root.
2597 if Nkind (N) = N_Extension_Aggregate then
2598 Root_Typ := Base_Type (Etype (Ancestor_Part (N)));
2599 else
2600 Root_Typ := Root_Type (Typ);
2602 if Nkind (Parent (Base_Type (Root_Typ)))
2603 = N_Private_Type_Declaration
2604 then
2605 Error_Msg_NE
2606 ("type of aggregate has private ancestor&!",
2607 N, Root_Typ);
2608 Error_Msg_N ("must use extension aggregate!", N);
2609 return;
2610 end if;
2612 Dnode := Declaration_Node (Base_Type (Root_Typ));
2614 -- If we don't get a full declaration, then we have some
2615 -- error which will get signalled later so skip this part.
2616 -- Otherwise, gather components of root that apply to the
2617 -- aggregate type. We use the base type in case there is an
2618 -- applicable girder constraint that renames the discriminants
2619 -- of the root.
2621 if Nkind (Dnode) = N_Full_Type_Declaration then
2622 Record_Def := Type_Definition (Dnode);
2623 Gather_Components (Base_Type (Typ),
2624 Component_List (Record_Def),
2625 Governed_By => New_Assoc_List,
2626 Into => Components,
2627 Report_Errors => Errors_Found);
2628 end if;
2629 end if;
2631 Parent_Typ := Base_Type (Typ);
2632 while Parent_Typ /= Root_Typ loop
2634 Prepend_Elmt (Parent_Typ, To => Parent_Typ_List);
2635 Parent_Typ := Etype (Parent_Typ);
2637 if (Nkind (Parent (Base_Type (Parent_Typ))) =
2638 N_Private_Type_Declaration
2639 or else Nkind (Parent (Base_Type (Parent_Typ))) =
2640 N_Private_Extension_Declaration)
2641 then
2642 if Nkind (N) /= N_Extension_Aggregate then
2643 Error_Msg_NE
2644 ("type of aggregate has private ancestor&!",
2645 N, Parent_Typ);
2646 Error_Msg_N ("must use extension aggregate!", N);
2647 return;
2649 elsif Parent_Typ /= Root_Typ then
2650 Error_Msg_NE
2651 ("ancestor part of aggregate must be private type&",
2652 Ancestor_Part (N), Parent_Typ);
2653 return;
2654 end if;
2655 end if;
2656 end loop;
2658 -- Now collect components from all other ancestors.
2660 Parent_Elmt := First_Elmt (Parent_Typ_List);
2661 while Present (Parent_Elmt) loop
2662 Parent_Typ := Node (Parent_Elmt);
2663 Record_Def := Type_Definition (Parent (Base_Type (Parent_Typ)));
2664 Gather_Components (Empty,
2665 Component_List (Record_Extension_Part (Record_Def)),
2666 Governed_By => New_Assoc_List,
2667 Into => Components,
2668 Report_Errors => Errors_Found);
2670 Next_Elmt (Parent_Elmt);
2671 end loop;
2673 else
2674 Record_Def := Type_Definition (Parent (Base_Type (Typ)));
2676 if Null_Present (Record_Def) then
2677 null;
2678 else
2679 Gather_Components (Base_Type (Typ),
2680 Component_List (Record_Def),
2681 Governed_By => New_Assoc_List,
2682 Into => Components,
2683 Report_Errors => Errors_Found);
2684 end if;
2685 end if;
2687 if Errors_Found then
2688 return;
2689 end if;
2690 end Step_5;
2692 -- STEP 6: Find component Values
2694 Component := Empty;
2695 Component_Elmt := First_Elmt (Components);
2697 -- First scan the remaining positional associations in the aggregate.
2698 -- Remember that at this point Positional_Expr contains the current
2699 -- positional association if any is left after looking for discriminant
2700 -- values in step 3.
2702 while Present (Positional_Expr) and then Present (Component_Elmt) loop
2703 Component := Node (Component_Elmt);
2704 Resolve_Aggr_Expr (Positional_Expr, Component);
2706 if Present (Get_Value (Component, Component_Associations (N))) then
2707 Error_Msg_NE
2708 ("more than one value supplied for Component &", N, Component);
2709 end if;
2711 Next (Positional_Expr);
2712 Next_Elmt (Component_Elmt);
2713 end loop;
2715 if Present (Positional_Expr) then
2716 Error_Msg_N
2717 ("too many components for record aggregate", Positional_Expr);
2718 end if;
2720 -- Now scan for the named arguments of the aggregate
2722 while Present (Component_Elmt) loop
2723 Component := Node (Component_Elmt);
2724 Expr := Get_Value (Component, Component_Associations (N), True);
2726 if No (Expr) then
2727 Error_Msg_NE ("no value supplied for component &!", N, Component);
2728 else
2729 Resolve_Aggr_Expr (Expr, Component);
2730 end if;
2732 Next_Elmt (Component_Elmt);
2733 end loop;
2735 -- STEP 7: check for invalid components + check type in choice list
2737 Step_7 : declare
2738 Selectr : Node_Id;
2739 -- Selector name
2741 Typech : Entity_Id;
2742 -- Type of first component in choice list
2744 begin
2745 if Present (Component_Associations (N)) then
2746 Assoc := First (Component_Associations (N));
2747 else
2748 Assoc := Empty;
2749 end if;
2751 Verification : while Present (Assoc) loop
2752 Selectr := First (Choices (Assoc));
2753 Typech := Empty;
2755 if Nkind (Selectr) = N_Others_Choice then
2756 if No (Others_Etype) then
2757 Error_Msg_N
2758 ("OTHERS must represent at least one component", Selectr);
2759 end if;
2761 exit Verification;
2762 end if;
2764 while Present (Selectr) loop
2765 New_Assoc := First (New_Assoc_List);
2766 while Present (New_Assoc) loop
2767 Component := First (Choices (New_Assoc));
2768 exit when Chars (Selectr) = Chars (Component);
2769 Next (New_Assoc);
2770 end loop;
2772 -- If no association, this is not a legal component of
2773 -- of the type in question, except if this is an internal
2774 -- component supplied by a previous expansion.
2776 if No (New_Assoc) then
2778 if Chars (Selectr) /= Name_uTag
2779 and then Chars (Selectr) /= Name_uParent
2780 and then Chars (Selectr) /= Name_uController
2781 then
2782 if not Has_Discriminants (Typ) then
2783 Error_Msg_Node_2 := Typ;
2784 Error_Msg_N
2785 ("& is not a component of}",
2786 Selectr);
2787 else
2788 Error_Msg_N
2789 ("& is not a component of the aggregate subtype",
2790 Selectr);
2791 end if;
2793 Check_Misspelled_Component (Components, Selectr);
2794 end if;
2796 elsif No (Typech) then
2797 Typech := Base_Type (Etype (Component));
2799 elsif Typech /= Base_Type (Etype (Component)) then
2800 Error_Msg_N
2801 ("components in choice list must have same type", Selectr);
2802 end if;
2804 Next (Selectr);
2805 end loop;
2807 Next (Assoc);
2808 end loop Verification;
2809 end Step_7;
2811 -- STEP 8: replace the original aggregate
2813 Step_8 : declare
2814 New_Aggregate : Node_Id := New_Copy (N);
2816 begin
2817 Set_Expressions (New_Aggregate, No_List);
2818 Set_Etype (New_Aggregate, Etype (N));
2819 Set_Component_Associations (New_Aggregate, New_Assoc_List);
2821 Rewrite (N, New_Aggregate);
2822 end Step_8;
2823 end Resolve_Record_Aggregate;
2825 ---------------------
2826 -- Sort_Case_Table --
2827 ---------------------
2829 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is
2830 L : Int := Case_Table'First;
2831 U : Int := Case_Table'Last;
2832 K : Int;
2833 J : Int;
2834 T : Case_Bounds;
2836 begin
2837 K := L;
2839 while K /= U loop
2840 T := Case_Table (K + 1);
2841 J := K + 1;
2843 while J /= L
2844 and then Expr_Value (Case_Table (J - 1).Choice_Lo) >
2845 Expr_Value (T.Choice_Lo)
2846 loop
2847 Case_Table (J) := Case_Table (J - 1);
2848 J := J - 1;
2849 end loop;
2851 Case_Table (J) := T;
2852 K := K + 1;
2853 end loop;
2854 end Sort_Case_Table;
2856 end Sem_Aggr;