1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
11 -- Copyright (C) 1992-2001 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}?",
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}?",
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_Component_Type (Itype, Component_Type (Typ));
634 Set_Convention (Itype, Convention (Typ));
635 Set_Depends_On_Private (Itype, Has_Private_Component (Typ));
636 Set_Etype (Itype, Base_Type (Typ));
637 Set_Has_Alignment_Clause (Itype, Has_Alignment_Clause (Typ));
638 Set_Is_Aliased (Itype, Is_Aliased (Typ));
639 Set_Suppress_Index_Checks (Itype, Suppress_Index_Checks (Typ));
640 Set_Suppress_Length_Checks (Itype, Suppress_Length_Checks (Typ));
641 Set_Depends_On_Private (Itype, Depends_On_Private (Typ));
643 Set_First_Index (Itype, First (Index_Constraints));
644 Set_Is_Constrained (Itype, True);
645 Set_Is_Internal (Itype, True);
646 Init_Size_Align (Itype);
648 -- A simple optimization: purely positional aggregates of static
649 -- components should be passed to gigi unexpanded whenever possible,
650 -- and regardless of the staticness of the bounds themselves. Subse-
651 -- quent checks in exp_aggr verify that type is not packed, etc.
653 Set_Size_Known_At_Compile_Time (Itype,
655 and then Comes_From_Source (N)
656 and then Size_Known_At_Compile_Time (Component_Type (Typ)));
658 -- We always need a freeze node for a packed array subtype, so that
659 -- we can build the Packed_Array_Type corresponding to the subtype.
660 -- If expansion is disabled, the packed array subtype is not built,
661 -- and we must not generate a freeze node for the type, or else it
662 -- will appear incomplete to gigi.
664 if Is_Packed (Itype) and then not In_Default_Expression
665 and then Expander_Active
667 Freeze_Itype (Itype, N);
671 end Array_Aggr_Subtype;
673 --------------------------------
674 -- Check_Misspelled_Component --
675 --------------------------------
677 procedure Check_Misspelled_Component
678 (Elements : Elist_Id;
681 Max_Suggestions : constant := 2;
683 Nr_Of_Suggestions : Natural := 0;
684 Suggestion_1 : Entity_Id := Empty;
685 Suggestion_2 : Entity_Id := Empty;
686 Component_Elmt : Elmt_Id;
689 -- All the components of List are matched against Component and
690 -- a count is maintained of possible misspellings. When at the
691 -- end of the analysis there are one or two (not more!) possible
692 -- misspellings, these misspellings will be suggested as
693 -- possible correction.
695 Get_Name_String (Chars (Component));
698 S : constant String (1 .. Name_Len) :=
699 Name_Buffer (1 .. Name_Len);
703 Component_Elmt := First_Elmt (Elements);
705 while Nr_Of_Suggestions <= Max_Suggestions
706 and then Present (Component_Elmt)
709 Get_Name_String (Chars (Node (Component_Elmt)));
711 if Is_Bad_Spelling_Of (Name_Buffer (1 .. Name_Len), S) then
712 Nr_Of_Suggestions := Nr_Of_Suggestions + 1;
714 case Nr_Of_Suggestions is
715 when 1 => Suggestion_1 := Node (Component_Elmt);
716 when 2 => Suggestion_2 := Node (Component_Elmt);
721 Next_Elmt (Component_Elmt);
724 -- Report at most two suggestions
726 if Nr_Of_Suggestions = 1 then
727 Error_Msg_NE ("\possible misspelling of&",
728 Component, Suggestion_1);
730 elsif Nr_Of_Suggestions = 2 then
731 Error_Msg_Node_2 := Suggestion_2;
732 Error_Msg_NE ("\possible misspelling of& or&",
733 Component, Suggestion_1);
736 end Check_Misspelled_Component;
738 ----------------------------------------
739 -- Check_Static_Discriminated_Subtype --
740 ----------------------------------------
742 procedure Check_Static_Discriminated_Subtype (T : Entity_Id; V : Node_Id) is
743 Disc : constant Entity_Id := First_Discriminant (T);
748 if Has_Record_Rep_Clause (Base_Type (T)) then
751 elsif Present (Next_Discriminant (Disc)) then
754 elsif Nkind (V) /= N_Integer_Literal then
758 Comp := First_Component (T);
760 while Present (Comp) loop
762 if Is_Scalar_Type (Etype (Comp)) then
765 elsif Is_Private_Type (Etype (Comp))
766 and then Present (Full_View (Etype (Comp)))
767 and then Is_Scalar_Type (Full_View (Etype (Comp)))
771 elsif Is_Array_Type (Etype (Comp)) then
773 if Is_Bit_Packed_Array (Etype (Comp)) then
777 Ind := First_Index (Etype (Comp));
779 while Present (Ind) loop
781 if Nkind (Ind) /= N_Range
782 or else Nkind (Low_Bound (Ind)) /= N_Integer_Literal
783 or else Nkind (High_Bound (Ind)) /= N_Integer_Literal
795 Next_Component (Comp);
798 -- On exit, all components have statically known sizes.
800 Set_Size_Known_At_Compile_Time (T);
801 end Check_Static_Discriminated_Subtype;
803 --------------------------------
804 -- Make_String_Into_Aggregate --
805 --------------------------------
807 procedure Make_String_Into_Aggregate (N : Node_Id) is
810 Exprs : List_Id := New_List;
811 Loc : constant Source_Ptr := Sloc (N);
813 P : Source_Ptr := Loc + 1;
814 Str : constant String_Id := Strval (N);
815 Strlen : constant Nat := String_Length (Str);
818 for J in 1 .. Strlen loop
819 C := Get_String_Char (Str, J);
820 Set_Character_Literal_Name (C);
822 C_Node := Make_Character_Literal (P, Name_Find, C);
823 Set_Etype (C_Node, Any_Character);
824 Set_Analyzed (C_Node);
825 Append_To (Exprs, C_Node);
828 -- something special for wide strings ?
831 New_N := Make_Aggregate (Loc, Expressions => Exprs);
832 Set_Analyzed (New_N);
833 Set_Etype (New_N, Any_Composite);
836 end Make_String_Into_Aggregate;
838 -----------------------
839 -- Resolve_Aggregate --
840 -----------------------
842 procedure Resolve_Aggregate (N : Node_Id; Typ : Entity_Id) is
843 Pkind : constant Node_Kind := Nkind (Parent (N));
845 Aggr_Subtyp : Entity_Id;
846 -- The actual aggregate subtype. This is not necessarily the same as Typ
847 -- which is the subtype of the context in which the aggregate was found.
850 if Is_Limited_Type (Typ) then
851 Error_Msg_N ("aggregate type cannot be limited", N);
853 elsif Is_Limited_Composite (Typ) then
854 Error_Msg_N ("aggregate type cannot have limited component", N);
856 elsif Is_Class_Wide_Type (Typ) then
857 Error_Msg_N ("type of aggregate cannot be class-wide", N);
859 elsif Typ = Any_String
860 or else Typ = Any_Composite
862 Error_Msg_N ("no unique type for aggregate", N);
863 Set_Etype (N, Any_Composite);
865 elsif Is_Array_Type (Typ) and then Null_Record_Present (N) then
866 Error_Msg_N ("null record forbidden in array aggregate", N);
868 elsif Is_Record_Type (Typ) then
869 Resolve_Record_Aggregate (N, Typ);
871 elsif Is_Array_Type (Typ) then
873 -- First a special test, for the case of a positional aggregate
874 -- of characters which can be replaced by a string literal.
875 -- Do not perform this transformation if this was a string literal
876 -- to start with, whose components needed constraint checks, or if
877 -- the component type is non-static, because it will require those
878 -- checks and be transformed back into an aggregate.
880 if Number_Dimensions (Typ) = 1
882 (Root_Type (Component_Type (Typ)) = Standard_Character
884 Root_Type (Component_Type (Typ)) = Standard_Wide_Character)
885 and then No (Component_Associations (N))
886 and then not Is_Limited_Composite (Typ)
887 and then not Is_Private_Composite (Typ)
888 and then not Is_Bit_Packed_Array (Typ)
889 and then Nkind (Original_Node (Parent (N))) /= N_String_Literal
890 and then Is_Static_Subtype (Component_Type (Typ))
896 Expr := First (Expressions (N));
897 while Present (Expr) loop
898 exit when Nkind (Expr) /= N_Character_Literal;
905 Expr := First (Expressions (N));
906 while Present (Expr) loop
907 Store_String_Char (Char_Literal_Value (Expr));
912 Make_String_Literal (Sloc (N), End_String));
914 Analyze_And_Resolve (N, Typ);
920 -- Here if we have a real aggregate to deal with
922 Array_Aggregate : declare
923 Aggr_Resolved : Boolean;
924 Aggr_Typ : Entity_Id := Etype (Typ);
925 -- This is the unconstrained array type, which is the type
926 -- against which the aggregate is to be resoved. Typ itself
927 -- is the array type of the context which may not be the same
928 -- subtype as the subtype for the final aggregate.
931 -- In the following we determine whether an others choice is
932 -- allowed inside the array aggregate. The test checks the context
933 -- in which the array aggregate occurs. If the context does not
934 -- permit it, or the aggregate type is unconstrained, an others
935 -- choice is not allowed.
937 -- Note that there is no node for Explicit_Actual_Parameter.
938 -- To test for this context we therefore have to test for node
939 -- N_Parameter_Association which itself appears only if there is a
940 -- formal parameter. Consequently we also need to test for
941 -- N_Procedure_Call_Statement or N_Function_Call.
943 if Is_Constrained (Typ) and then
944 (Pkind = N_Assignment_Statement or else
945 Pkind = N_Parameter_Association or else
946 Pkind = N_Function_Call or else
947 Pkind = N_Procedure_Call_Statement or else
948 Pkind = N_Generic_Association or else
949 Pkind = N_Formal_Object_Declaration or else
950 Pkind = N_Return_Statement or else
951 Pkind = N_Object_Declaration or else
952 Pkind = N_Component_Declaration or else
953 Pkind = N_Parameter_Specification or else
954 Pkind = N_Qualified_Expression or else
955 Pkind = N_Aggregate or else
956 Pkind = N_Extension_Aggregate or else
957 Pkind = N_Component_Association)
960 Resolve_Array_Aggregate
962 Index => First_Index (Aggr_Typ),
963 Index_Constr => First_Index (Typ),
964 Component_Typ => Component_Type (Typ),
965 Others_Allowed => True);
969 Resolve_Array_Aggregate
971 Index => First_Index (Aggr_Typ),
972 Index_Constr => First_Index (Aggr_Typ),
973 Component_Typ => Component_Type (Typ),
974 Others_Allowed => False);
977 if not Aggr_Resolved then
978 Aggr_Subtyp := Any_Composite;
980 Aggr_Subtyp := Array_Aggr_Subtype (N, Typ);
983 Set_Etype (N, Aggr_Subtyp);
987 Error_Msg_N ("illegal context for aggregate", N);
991 -- If we can determine statically that the evaluation of the
992 -- aggregate raises Constraint_Error, then replace the
993 -- aggregate with an N_Raise_Constraint_Error node, but set the
994 -- Etype to the right aggregate subtype. Gigi needs this.
996 if Raises_Constraint_Error (N) then
997 Aggr_Subtyp := Etype (N);
998 Rewrite (N, Make_Raise_Constraint_Error (Sloc (N)));
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 -- STEP 2: Process named components
1481 if No (Expressions (N)) then
1483 if Others_Present then
1484 Case_Table_Size := Nb_Choices - 1;
1486 Case_Table_Size := Nb_Choices;
1492 -- Denote the lowest and highest values in an aggregate choice
1496 -- High end of one range and Low end of the next. Should be
1497 -- contiguous if there is no hole in the list of values.
1499 Missing_Values : Boolean;
1500 -- Set True if missing index values
1502 S_Low : Node_Id := Empty;
1503 S_High : Node_Id := Empty;
1504 -- if a choice in an aggregate is a subtype indication these
1505 -- denote the lowest and highest values of the subtype
1507 Table : Case_Table_Type (1 .. Case_Table_Size);
1508 -- Used to sort all the different choice values
1510 Single_Choice : Boolean;
1511 -- Set to true every time there is a single discrete choice in a
1512 -- discrete association
1514 Prev_Nb_Discrete_Choices : Nat;
1515 -- Used to keep track of the number of discrete choices
1516 -- in the current association.
1519 -- STEP 2 (A): Check discrete choices validity.
1521 Assoc := First (Component_Associations (N));
1522 while Present (Assoc) loop
1524 Prev_Nb_Discrete_Choices := Nb_Discrete_Choices;
1525 Choice := First (Choices (Assoc));
1529 if Nkind (Choice) = N_Others_Choice then
1530 Single_Choice := False;
1533 -- Test for subtype mark without constraint
1535 elsif Is_Entity_Name (Choice) and then
1536 Is_Type (Entity (Choice))
1538 if Base_Type (Entity (Choice)) /= Index_Base then
1540 ("invalid subtype mark in aggregate choice",
1545 elsif Nkind (Choice) = N_Subtype_Indication then
1546 Resolve_Discrete_Subtype_Indication (Choice, Index_Base);
1548 -- Does the subtype indication evaluation raise CE ?
1550 Get_Index_Bounds (Subtype_Mark (Choice), S_Low, S_High);
1551 Get_Index_Bounds (Choice, Low, High);
1552 Check_Bounds (S_Low, S_High, Low, High);
1554 else -- Choice is a range or an expression
1555 Resolve (Choice, Index_Base);
1556 Check_Non_Static_Context (Choice);
1558 -- Do not range check a choice. This check is redundant
1559 -- since this test is already performed when we check
1560 -- that the bounds of the array aggregate are within
1563 Set_Do_Range_Check (Choice, False);
1566 -- If we could not resolve the discrete choice stop here
1568 if Etype (Choice) = Any_Type then
1571 -- If the discrete choice raises CE get its original bounds.
1573 elsif Nkind (Choice) = N_Raise_Constraint_Error then
1574 Set_Raises_Constraint_Error (N);
1575 Get_Index_Bounds (Original_Node (Choice), Low, High);
1577 -- Otherwise get its bounds as usual
1580 Get_Index_Bounds (Choice, Low, High);
1583 if (Dynamic_Or_Null_Range (Low, High)
1584 or else (Nkind (Choice) = N_Subtype_Indication
1586 Dynamic_Or_Null_Range (S_Low, S_High)))
1587 and then Nb_Choices /= 1
1590 ("dynamic or empty choice in aggregate " &
1591 "must be the only choice", Choice);
1595 Nb_Discrete_Choices := Nb_Discrete_Choices + 1;
1596 Table (Nb_Discrete_Choices).Choice_Lo := Low;
1597 Table (Nb_Discrete_Choices).Choice_Hi := High;
1602 -- Check if we have a single discrete choice and whether
1603 -- this discrete choice specifies a single value.
1606 (Nb_Discrete_Choices = Prev_Nb_Discrete_Choices + 1)
1607 and then (Low = High);
1615 (Expression (Assoc), Single_Elmt => Single_Choice)
1623 -- If aggregate contains more than one choice then these must be
1624 -- static. Sort them and check that they are contiguous
1626 if Nb_Discrete_Choices > 1 then
1627 Sort_Case_Table (Table);
1628 Missing_Values := False;
1630 Outer : for J in 1 .. Nb_Discrete_Choices - 1 loop
1631 if Expr_Value (Table (J).Choice_Hi) >=
1632 Expr_Value (Table (J + 1).Choice_Lo)
1635 ("duplicate choice values in array aggregate",
1636 Table (J).Choice_Hi);
1639 elsif not Others_Present then
1641 Hi_Val := Expr_Value (Table (J).Choice_Hi);
1642 Lo_Val := Expr_Value (Table (J + 1).Choice_Lo);
1644 -- If missing values, output error messages
1646 if Lo_Val - Hi_Val > 1 then
1648 -- Header message if not first missing value
1650 if not Missing_Values then
1652 ("missing index value(s) in array aggregate", N);
1653 Missing_Values := True;
1656 -- Output values of missing indexes
1658 Lo_Val := Lo_Val - 1;
1659 Hi_Val := Hi_Val + 1;
1661 -- Enumeration type case
1663 if Is_Enumeration_Type (Index_Typ) then
1666 (Get_Enum_Lit_From_Pos
1667 (Index_Typ, Hi_Val, Loc));
1669 if Lo_Val = Hi_Val then
1670 Error_Msg_N ("\ %", N);
1674 (Get_Enum_Lit_From_Pos
1675 (Index_Typ, Lo_Val, Loc));
1676 Error_Msg_N ("\ % .. %", N);
1679 -- Integer types case
1682 Error_Msg_Uint_1 := Hi_Val;
1684 if Lo_Val = Hi_Val then
1685 Error_Msg_N ("\ ^", N);
1687 Error_Msg_Uint_2 := Lo_Val;
1688 Error_Msg_N ("\ ^ .. ^", N);
1695 if Missing_Values then
1696 Set_Etype (N, Any_Composite);
1701 -- STEP 2 (B): Compute aggregate bounds and min/max choices values
1703 if Nb_Discrete_Choices > 0 then
1704 Choices_Low := Table (1).Choice_Lo;
1705 Choices_High := Table (Nb_Discrete_Choices).Choice_Hi;
1708 if Others_Present then
1709 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
1712 Aggr_Low := Choices_Low;
1713 Aggr_High := Choices_High;
1717 -- STEP 3: Process positional components
1720 -- STEP 3 (A): Process positional elements
1722 Expr := First (Expressions (N));
1723 Nb_Elements := Uint_0;
1724 while Present (Expr) loop
1725 Nb_Elements := Nb_Elements + 1;
1727 if not Resolve_Aggr_Expr (Expr, Single_Elmt => True) then
1734 if Others_Present then
1735 Assoc := Last (Component_Associations (N));
1736 if not Resolve_Aggr_Expr (Expression (Assoc),
1737 Single_Elmt => False)
1743 -- STEP 3 (B): Compute the aggregate bounds
1745 if Others_Present then
1746 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
1749 if Others_Allowed then
1750 Get_Index_Bounds (Index_Constr, Aggr_Low, Who_Cares);
1752 Aggr_Low := Index_Typ_Low;
1755 Aggr_High := Add (Nb_Elements - 1, To => Aggr_Low);
1756 Check_Bound (Index_Base_High, Aggr_High);
1760 -- STEP 4: Perform static aggregate checks and save the bounds
1764 Check_Bounds (Index_Typ_Low, Index_Typ_High, Aggr_Low, Aggr_High);
1765 Check_Bounds (Index_Base_Low, Index_Base_High, Aggr_Low, Aggr_High);
1769 if Others_Present and then Nb_Discrete_Choices > 0 then
1770 Check_Bounds (Aggr_Low, Aggr_High, Choices_Low, Choices_High);
1771 Check_Bounds (Index_Typ_Low, Index_Typ_High,
1772 Choices_Low, Choices_High);
1773 Check_Bounds (Index_Base_Low, Index_Base_High,
1774 Choices_Low, Choices_High);
1778 elsif Others_Present and then Nb_Elements > 0 then
1779 Check_Length (Aggr_Low, Aggr_High, Nb_Elements);
1780 Check_Length (Index_Typ_Low, Index_Typ_High, Nb_Elements);
1781 Check_Length (Index_Base_Low, Index_Base_High, Nb_Elements);
1785 if Raises_Constraint_Error (Aggr_Low)
1786 or else Raises_Constraint_Error (Aggr_High)
1788 Set_Raises_Constraint_Error (N);
1791 Aggr_Low := Duplicate_Subexpr (Aggr_Low);
1793 -- Do not duplicate Aggr_High if Aggr_High = Aggr_Low + Nb_Elements
1794 -- since the addition node returned by Add is not yet analyzed. Attach
1795 -- to tree and analyze first. Reset analyzed flag to insure it will get
1796 -- analyzed when it is a literal bound whose type must be properly
1799 if Others_Present or else Nb_Discrete_Choices > 0 then
1800 Aggr_High := Duplicate_Subexpr (Aggr_High);
1802 if Etype (Aggr_High) = Universal_Integer then
1803 Set_Analyzed (Aggr_High, False);
1807 Set_Aggregate_Bounds
1808 (N, Make_Range (Loc, Low_Bound => Aggr_Low, High_Bound => Aggr_High));
1810 -- The bounds may contain expressions that must be inserted upwards.
1811 -- Attach them fully to the tree. After analysis, remove side effects
1812 -- from upper bound, if still needed.
1814 Set_Parent (Aggregate_Bounds (N), N);
1815 Analyze_And_Resolve (Aggregate_Bounds (N), Index_Typ);
1817 if not Others_Present and then Nb_Discrete_Choices = 0 then
1818 Set_High_Bound (Aggregate_Bounds (N),
1819 Duplicate_Subexpr (High_Bound (Aggregate_Bounds (N))));
1823 end Resolve_Array_Aggregate;
1825 ---------------------------------
1826 -- Resolve_Extension_Aggregate --
1827 ---------------------------------
1829 -- There are two cases to consider:
1831 -- a) If the ancestor part is a type mark, the components needed are
1832 -- the difference between the components of the expected type and the
1833 -- components of the given type mark.
1835 -- b) If the ancestor part is an expression, it must be unambiguous,
1836 -- and once we have its type we can also compute the needed components
1837 -- as in the previous case. In both cases, if the ancestor type is not
1838 -- the immediate ancestor, we have to build this ancestor recursively.
1840 -- In both cases discriminants of the ancestor type do not play a
1841 -- role in the resolution of the needed components, because inherited
1842 -- discriminants cannot be used in a type extension. As a result we can
1843 -- compute independently the list of components of the ancestor type and
1844 -- of the expected type.
1846 procedure Resolve_Extension_Aggregate (N : Node_Id; Typ : Entity_Id) is
1847 A : constant Node_Id := Ancestor_Part (N);
1851 Imm_Type : Entity_Id;
1853 function Valid_Ancestor_Type return Boolean;
1854 -- Verify that the type of the ancestor part is a non-private ancestor
1855 -- of the expected type.
1857 function Valid_Ancestor_Type return Boolean is
1858 Imm_Type : Entity_Id;
1861 Imm_Type := Base_Type (Typ);
1862 while Is_Derived_Type (Imm_Type)
1863 and then Etype (Imm_Type) /= Base_Type (A_Type)
1865 Imm_Type := Etype (Base_Type (Imm_Type));
1868 if Etype (Imm_Type) /= Base_Type (A_Type) then
1869 Error_Msg_NE ("expect ancestor type of &", A, Typ);
1874 end Valid_Ancestor_Type;
1876 -- Start of processing for Resolve_Extension_Aggregate
1881 if not Is_Tagged_Type (Typ) then
1882 Error_Msg_N ("type of extension aggregate must be tagged", N);
1885 elsif Is_Limited_Type (Typ) then
1886 Error_Msg_N ("aggregate type cannot be limited", N);
1889 elsif Is_Class_Wide_Type (Typ) then
1890 Error_Msg_N ("aggregate cannot be of a class-wide type", N);
1894 if Is_Entity_Name (A)
1895 and then Is_Type (Entity (A))
1897 A_Type := Get_Full_View (Entity (A));
1898 Imm_Type := Base_Type (Typ);
1900 if Valid_Ancestor_Type then
1901 Set_Entity (A, A_Type);
1902 Set_Etype (A, A_Type);
1904 Validate_Ancestor_Part (N);
1905 Resolve_Record_Aggregate (N, Typ);
1908 elsif Nkind (A) /= N_Aggregate then
1909 if Is_Overloaded (A) then
1911 Get_First_Interp (A, I, It);
1913 while Present (It.Typ) loop
1915 if Is_Tagged_Type (It.Typ)
1916 and then not Is_Limited_Type (It.Typ)
1918 if A_Type /= Any_Type then
1919 Error_Msg_N ("cannot resolve expression", A);
1926 Get_Next_Interp (I, It);
1929 if A_Type = Any_Type then
1931 ("ancestor part must be non-limited tagged type", A);
1936 A_Type := Etype (A);
1939 if Valid_Ancestor_Type then
1940 Resolve (A, A_Type);
1941 Check_Non_Static_Context (A);
1942 Resolve_Record_Aggregate (N, Typ);
1946 Error_Msg_N (" No unique type for this aggregate", A);
1949 end Resolve_Extension_Aggregate;
1951 ------------------------------
1952 -- Resolve_Record_Aggregate --
1953 ------------------------------
1955 procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id) is
1956 Regular_Aggr : constant Boolean := Nkind (N) /= N_Extension_Aggregate;
1958 New_Assoc_List : List_Id := New_List;
1959 New_Assoc : Node_Id;
1960 -- New_Assoc_List is the newly built list of N_Component_Association
1961 -- nodes. New_Assoc is one such N_Component_Association node in it.
1962 -- Please note that while Assoc and New_Assoc contain the same
1963 -- kind of nodes, they are used to iterate over two different
1964 -- N_Component_Association lists.
1966 Others_Etype : Entity_Id := Empty;
1967 -- This variable is used to save the Etype of the last record component
1968 -- that takes its value from the others choice. Its purpose is:
1970 -- (a) make sure the others choice is useful
1972 -- (b) make sure the type of all the components whose value is
1973 -- subsumed by the others choice are the same.
1975 -- This variable is updated as a side effect of function Get_Value
1977 procedure Add_Association (Component : Entity_Id; Expr : Node_Id);
1978 -- Builds a new N_Component_Association node which associates
1979 -- Component to expression Expr and adds it to the new association
1980 -- list New_Assoc_List being built.
1982 function Discr_Present (Discr : Entity_Id) return Boolean;
1983 -- If aggregate N is a regular aggregate this routine will return True.
1984 -- Otherwise, if N is an extension aggreagte, Discr is a discriminant
1985 -- whose value may already have been specified by N's ancestor part,
1986 -- this routine checks whether this is indeed the case and if so
1987 -- returns False, signaling that no value for Discr should appear in the
1988 -- N's aggregate part. Also, in this case, the routine appends to
1989 -- New_Assoc_List Discr the discriminant value specified in the ancestor
1995 Consider_Others_Choice : Boolean := False)
1997 -- Given a record component stored in parameter Compon, the
1998 -- following function returns its value as it appears in the list
1999 -- From, which is a list of N_Component_Association nodes. If no
2000 -- component association has a choice for the searched component,
2001 -- the value provided by the others choice is returned, if there
2002 -- is one and Consider_Others_Choice is set to true. Otherwise
2003 -- Empty is returned. If there is more than one component association
2004 -- giving a value for the searched record component, an error message
2005 -- is emitted and the first found value is returned.
2007 -- If Consider_Others_Choice is set and the returned expression comes
2008 -- from the others choice, then Others_Etype is set as a side effect.
2009 -- An error message is emitted if the components taking their value
2010 -- from the others choice do not have same type.
2012 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Node_Id);
2013 -- Analyzes and resolves expression Expr against the Etype of the
2014 -- Component. This routine also applies all appropriate checks to Expr.
2015 -- It finally saves a Expr in the newly created association list that
2016 -- will be attached to the final record aggregate. Note that if the
2017 -- Parent pointer of Expr is not set then Expr was produced with a
2018 -- New_copy_Tree or some such.
2020 ---------------------
2021 -- Add_Association --
2022 ---------------------
2024 procedure Add_Association (Component : Entity_Id; Expr : Node_Id) is
2025 New_Assoc : Node_Id;
2026 Choice_List : List_Id := New_List;
2029 Append (New_Occurrence_Of (Component, Sloc (Expr)), Choice_List);
2031 Make_Component_Association (Sloc (Expr),
2032 Choices => Choice_List,
2033 Expression => Expr);
2034 Append (New_Assoc, New_Assoc_List);
2035 end Add_Association;
2041 function Discr_Present (Discr : Entity_Id) return Boolean is
2045 Discr_Expr : Node_Id;
2047 Ancestor_Typ : Entity_Id;
2048 Orig_Discr : Entity_Id;
2050 D_Val : Elmt_Id := No_Elmt; -- stop junk warning
2052 Ancestor_Is_Subtyp : Boolean;
2055 if Regular_Aggr then
2059 Ancestor := Ancestor_Part (N);
2060 Ancestor_Typ := Etype (Ancestor);
2061 Loc := Sloc (Ancestor);
2063 Ancestor_Is_Subtyp :=
2064 Is_Entity_Name (Ancestor) and then Is_Type (Entity (Ancestor));
2066 -- If the ancestor part has no discriminants clearly N's aggregate
2067 -- part must provide a value for Discr.
2069 if not Has_Discriminants (Ancestor_Typ) then
2072 -- If the ancestor part is an unconstrained subtype mark then the
2073 -- Discr must be present in N's aggregate part.
2075 elsif Ancestor_Is_Subtyp
2076 and then not Is_Constrained (Entity (Ancestor))
2081 -- Now look to see if Discr was specified in the ancestor part.
2083 Orig_Discr := Original_Record_Component (Discr);
2084 D := First_Discriminant (Ancestor_Typ);
2086 if Ancestor_Is_Subtyp then
2087 D_Val := First_Elmt (Discriminant_Constraint (Entity (Ancestor)));
2090 while Present (D) loop
2091 -- If Ancestor has already specified Disc value than
2092 -- insert its value in the final aggregate.
2094 if Original_Record_Component (D) = Orig_Discr then
2095 if Ancestor_Is_Subtyp then
2096 Discr_Expr := New_Copy_Tree (Node (D_Val));
2099 Make_Selected_Component (Loc,
2100 Prefix => Duplicate_Subexpr (Ancestor),
2101 Selector_Name => New_Occurrence_Of (Discr, Loc));
2104 Resolve_Aggr_Expr (Discr_Expr, Discr);
2108 Next_Discriminant (D);
2110 if Ancestor_Is_Subtyp then
2125 Consider_Others_Choice : Boolean := False)
2129 Expr : Node_Id := Empty;
2130 Selector_Name : Node_Id;
2133 if Present (From) then
2134 Assoc := First (From);
2139 while Present (Assoc) loop
2140 Selector_Name := First (Choices (Assoc));
2141 while Present (Selector_Name) loop
2142 if Nkind (Selector_Name) = N_Others_Choice then
2143 if Consider_Others_Choice and then No (Expr) then
2144 if Present (Others_Etype) and then
2145 Base_Type (Others_Etype) /= Base_Type (Etype (Compon))
2147 Error_Msg_N ("components in OTHERS choice must " &
2148 "have same type", Selector_Name);
2151 Others_Etype := Etype (Compon);
2153 -- We need to duplicate the expression for each
2154 -- successive component covered by the others choice.
2155 -- If the expression is itself an array aggregate with
2156 -- "others", its subtype must be obtained from the
2157 -- current component, and therefore it must be (at least
2158 -- partly) reanalyzed.
2160 if Analyzed (Expression (Assoc)) then
2161 Expr := New_Copy_Tree (Expression (Assoc));
2163 if Nkind (Expr) = N_Aggregate
2164 and then Is_Array_Type (Etype (Expr))
2165 and then No (Expressions (Expr))
2167 Nkind (First (Choices
2168 (First (Component_Associations (Expr)))))
2171 Set_Analyzed (Expr, False);
2177 return Expression (Assoc);
2181 elsif Chars (Compon) = Chars (Selector_Name) then
2183 -- We need to duplicate the expression when several
2184 -- components are grouped together with a "|" choice.
2185 -- For instance "filed1 | filed2 => Expr"
2187 if Present (Next (Selector_Name)) then
2188 Expr := New_Copy_Tree (Expression (Assoc));
2190 Expr := Expression (Assoc);
2195 ("more than one value supplied for &",
2196 Selector_Name, Compon);
2201 Next (Selector_Name);
2210 -----------------------
2211 -- Resolve_Aggr_Expr --
2212 -----------------------
2214 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Node_Id) is
2215 New_C : Entity_Id := Component;
2216 Expr_Type : Entity_Id := Empty;
2218 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean;
2219 -- If the expression is an aggregate (possibly qualified) then its
2220 -- expansion is delayed until the enclosing aggregate is expanded
2221 -- into assignments. In that case, do not generate checks on the
2222 -- expression, because they will be generated later, and will other-
2223 -- wise force a copy (to remove side-effects) that would leave a
2224 -- dynamic-sized aggregate in the code, something that gigi cannot
2228 -- Set to True if the resolved Expr node needs to be relocated
2229 -- when attached to the newly created association list. This node
2230 -- need not be relocated if its parent pointer is not set.
2231 -- In fact in this case Expr is the output of a New_Copy_Tree call.
2232 -- if Relocate is True then we have analyzed the expression node
2233 -- in the original aggregate and hence it needs to be relocated
2234 -- when moved over the new association list.
2236 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean is
2237 Kind : constant Node_Kind := Nkind (Expr);
2240 return ((Kind = N_Aggregate
2241 or else Kind = N_Extension_Aggregate)
2242 and then Present (Etype (Expr))
2243 and then Is_Record_Type (Etype (Expr))
2244 and then Expansion_Delayed (Expr))
2246 or else (Kind = N_Qualified_Expression
2247 and then Has_Expansion_Delayed (Expression (Expr)));
2248 end Has_Expansion_Delayed;
2250 -- Start of processing for Resolve_Aggr_Expr
2253 -- If the type of the component is elementary or the type of the
2254 -- aggregate does not contain discriminants, use the type of the
2255 -- component to resolve Expr.
2257 if Is_Elementary_Type (Etype (Component))
2258 or else not Has_Discriminants (Etype (N))
2260 Expr_Type := Etype (Component);
2262 -- Otherwise we have to pick up the new type of the component from
2263 -- the new costrained subtype of the aggregate. In fact components
2264 -- which are of a composite type might be constrained by a
2265 -- discriminant, and we want to resolve Expr against the subtype were
2266 -- all discriminant occurrences are replaced with their actual value.
2269 New_C := First_Component (Etype (N));
2270 while Present (New_C) loop
2271 if Chars (New_C) = Chars (Component) then
2272 Expr_Type := Etype (New_C);
2276 Next_Component (New_C);
2279 pragma Assert (Present (Expr_Type));
2281 -- For each range in an array type where a discriminant has been
2282 -- replaced with the constraint, check that this range is within
2283 -- the range of the base type. This checks is done in the
2284 -- _init_proc for regular objects, but has to be done here for
2285 -- aggregates since no _init_proc is called for them.
2287 if Is_Array_Type (Expr_Type) then
2289 Index : Node_Id := First_Index (Expr_Type);
2290 -- Range of the current constrained index in the array.
2292 Orig_Index : Node_Id := First_Index (Etype (Component));
2293 -- Range corresponding to the range Index above in the
2294 -- original unconstrained record type. The bounds of this
2295 -- range may be governed by discriminants.
2297 Unconstr_Index : Node_Id := First_Index (Etype (Expr_Type));
2298 -- Range corresponding to the range Index above for the
2299 -- unconstrained array type. This range is needed to apply
2303 while Present (Index) loop
2304 if Depends_On_Discriminant (Orig_Index) then
2305 Apply_Range_Check (Index, Etype (Unconstr_Index));
2309 Next_Index (Orig_Index);
2310 Next_Index (Unconstr_Index);
2316 -- If the Parent pointer of Expr is not set, Expr is an expression
2317 -- duplicated by New_Tree_Copy (this happens for record aggregates
2318 -- that look like (Field1 | Filed2 => Expr) or (others => Expr)).
2319 -- Such a duplicated expression must be attached to the tree
2320 -- before analysis and resolution to enforce the rule that a tree
2321 -- fragment should never be analyzed or resolved unless it is
2322 -- attached to the current compilation unit.
2324 if No (Parent (Expr)) then
2325 Set_Parent (Expr, N);
2331 Analyze_And_Resolve (Expr, Expr_Type);
2332 Check_Non_Static_Context (Expr);
2334 if not Has_Expansion_Delayed (Expr) then
2335 Aggregate_Constraint_Checks (Expr, Expr_Type);
2338 if Raises_Constraint_Error (Expr) then
2339 Set_Raises_Constraint_Error (N);
2343 Add_Association (New_C, Relocate_Node (Expr));
2345 Add_Association (New_C, Expr);
2348 end Resolve_Aggr_Expr;
2350 -- Resolve_Record_Aggregate local variables
2353 -- N_Component_Association node belonging to the input aggregate N
2356 Positional_Expr : Node_Id;
2358 Component : Entity_Id;
2359 Component_Elmt : Elmt_Id;
2360 Components : Elist_Id := New_Elmt_List;
2361 -- Components is the list of the record components whose value must
2362 -- be provided in the aggregate. This list does include discriminants.
2364 -- Start of processing for Resolve_Record_Aggregate
2367 -- We may end up calling Duplicate_Subexpr on expressions that are
2368 -- attached to New_Assoc_List. For this reason we need to attach it
2369 -- to the tree by setting its parent pointer to N. This parent point
2370 -- will change in STEP 8 below.
2372 Set_Parent (New_Assoc_List, N);
2374 -- STEP 1: abstract type and null record verification
2376 if Is_Abstract (Typ) then
2377 Error_Msg_N ("type of aggregate cannot be abstract", N);
2380 if No (First_Entity (Typ)) and then Null_Record_Present (N) then
2384 elsif Present (First_Entity (Typ))
2385 and then Null_Record_Present (N)
2386 and then not Is_Tagged_Type (Typ)
2388 Error_Msg_N ("record aggregate cannot be null", N);
2391 elsif No (First_Entity (Typ)) then
2392 Error_Msg_N ("record aggregate must be null", N);
2396 -- STEP 2: Verify aggregate structure
2399 Selector_Name : Node_Id;
2400 Bad_Aggregate : Boolean := False;
2403 if Present (Component_Associations (N)) then
2404 Assoc := First (Component_Associations (N));
2409 while Present (Assoc) loop
2410 Selector_Name := First (Choices (Assoc));
2411 while Present (Selector_Name) loop
2412 if Nkind (Selector_Name) = N_Identifier then
2415 elsif Nkind (Selector_Name) = N_Others_Choice then
2416 if Selector_Name /= First (Choices (Assoc))
2417 or else Present (Next (Selector_Name))
2419 Error_Msg_N ("OTHERS must appear alone in a choice list",
2423 elsif Present (Next (Assoc)) then
2424 Error_Msg_N ("OTHERS must appear last in an aggregate",
2431 ("selector name should be identifier or OTHERS",
2433 Bad_Aggregate := True;
2436 Next (Selector_Name);
2442 if Bad_Aggregate then
2447 -- STEP 3: Find discriminant Values
2450 Discrim : Entity_Id;
2451 Missing_Discriminants : Boolean := False;
2454 if Present (Expressions (N)) then
2455 Positional_Expr := First (Expressions (N));
2457 Positional_Expr := Empty;
2460 if Has_Discriminants (Typ) then
2461 Discrim := First_Discriminant (Typ);
2466 -- First find the discriminant values in the positional components
2468 while Present (Discrim) and then Present (Positional_Expr) loop
2469 if Discr_Present (Discrim) then
2470 Resolve_Aggr_Expr (Positional_Expr, Discrim);
2471 Next (Positional_Expr);
2474 if Present (Get_Value (Discrim, Component_Associations (N))) then
2476 ("more than one value supplied for discriminant&",
2480 Next_Discriminant (Discrim);
2483 -- Find remaining discriminant values, if any, among named components
2485 while Present (Discrim) loop
2486 Expr := Get_Value (Discrim, Component_Associations (N), True);
2488 if not Discr_Present (Discrim) then
2489 if Present (Expr) then
2491 ("more than one value supplied for discriminant&",
2495 elsif No (Expr) then
2497 ("no value supplied for discriminant &", N, Discrim);
2498 Missing_Discriminants := True;
2501 Resolve_Aggr_Expr (Expr, Discrim);
2504 Next_Discriminant (Discrim);
2507 if Missing_Discriminants then
2511 -- At this point and until the beginning of STEP 6, New_Assoc_List
2512 -- contains only the discriminants and their values.
2516 -- STEP 4: Set the Etype of the record aggregate
2518 -- ??? This code is pretty much a copy of Sem_Ch3.Build_Subtype. That
2519 -- routine should really be exported in sem_util or some such and used
2520 -- in sem_ch3 and here rather than have a copy of the code which is a
2521 -- maintenance nightmare.
2523 -- ??? Performace WARNING. The current implementation creates a new
2524 -- itype for all aggregates whose base type is discriminated.
2525 -- This means that for record aggregates nested inside an array
2526 -- aggregate we will create a new itype for each record aggregate
2527 -- if the array cmponent type has discriminants. For large aggregates
2528 -- this may be a problem. What should be done in this case is
2529 -- to reuse itypes as much as possible.
2531 if Has_Discriminants (Typ) then
2532 Build_Constrained_Itype : declare
2533 Loc : constant Source_Ptr := Sloc (N);
2535 Subtyp_Decl : Node_Id;
2538 C : List_Id := New_List;
2541 New_Assoc := First (New_Assoc_List);
2542 while Present (New_Assoc) loop
2543 Append (Duplicate_Subexpr (Expression (New_Assoc)), To => C);
2548 Make_Subtype_Indication (Loc,
2549 Subtype_Mark => New_Occurrence_Of (Base_Type (Typ), Loc),
2550 Constraint => Make_Index_Or_Discriminant_Constraint (Loc, C));
2552 Def_Id := Create_Itype (Ekind (Typ), N);
2555 Make_Subtype_Declaration (Loc,
2556 Defining_Identifier => Def_Id,
2557 Subtype_Indication => Indic);
2558 Set_Parent (Subtyp_Decl, Parent (N));
2560 -- Itypes must be analyzed with checks off (see itypes.ads).
2562 Analyze (Subtyp_Decl, Suppress => All_Checks);
2564 Set_Etype (N, Def_Id);
2565 Check_Static_Discriminated_Subtype
2566 (Def_Id, Expression (First (New_Assoc_List)));
2567 end Build_Constrained_Itype;
2573 -- STEP 5: Get remaining components according to discriminant values
2576 Record_Def : Node_Id;
2577 Parent_Typ : Entity_Id;
2578 Root_Typ : Entity_Id;
2579 Parent_Typ_List : Elist_Id;
2580 Parent_Elmt : Elmt_Id;
2581 Errors_Found : Boolean := False;
2585 if Is_Derived_Type (Typ) and then Is_Tagged_Type (Typ) then
2586 Parent_Typ_List := New_Elmt_List;
2588 -- If this is an extension aggregate, the component list must
2589 -- include all components that are not in the given ancestor
2590 -- type. Otherwise, the component list must include components
2591 -- of all ancestors.
2593 if Nkind (N) = N_Extension_Aggregate then
2594 Root_Typ := Base_Type (Etype (Ancestor_Part (N)));
2596 Root_Typ := Root_Type (Typ);
2598 if Nkind (Parent (Base_Type (Root_Typ)))
2599 = N_Private_Type_Declaration
2602 ("type of aggregate has private ancestor&!",
2604 Error_Msg_N ("must use extension aggregate!", N);
2608 Dnode := Declaration_Node (Base_Type (Root_Typ));
2610 -- If we don't get a full declaration, then we have some
2611 -- error which will get signalled later so skip this part.
2613 if Nkind (Dnode) = N_Full_Type_Declaration then
2614 Record_Def := Type_Definition (Dnode);
2615 Gather_Components (Typ,
2616 Component_List (Record_Def),
2617 Governed_By => New_Assoc_List,
2619 Report_Errors => Errors_Found);
2623 Parent_Typ := Base_Type (Typ);
2624 while Parent_Typ /= Root_Typ loop
2626 Prepend_Elmt (Parent_Typ, To => Parent_Typ_List);
2627 Parent_Typ := Etype (Parent_Typ);
2629 if (Nkind (Parent (Base_Type (Parent_Typ))) =
2630 N_Private_Type_Declaration
2631 or else Nkind (Parent (Base_Type (Parent_Typ))) =
2632 N_Private_Extension_Declaration)
2634 if Nkind (N) /= N_Extension_Aggregate then
2636 ("type of aggregate has private ancestor&!",
2638 Error_Msg_N ("must use extension aggregate!", N);
2641 elsif Parent_Typ /= Root_Typ then
2643 ("ancestor part of aggregate must be private type&",
2644 Ancestor_Part (N), Parent_Typ);
2650 -- Now collect components from all other ancestors.
2652 Parent_Elmt := First_Elmt (Parent_Typ_List);
2653 while Present (Parent_Elmt) loop
2654 Parent_Typ := Node (Parent_Elmt);
2655 Record_Def := Type_Definition (Parent (Base_Type (Parent_Typ)));
2656 Gather_Components (Empty,
2657 Component_List (Record_Extension_Part (Record_Def)),
2658 Governed_By => New_Assoc_List,
2660 Report_Errors => Errors_Found);
2662 Next_Elmt (Parent_Elmt);
2666 Record_Def := Type_Definition (Parent (Base_Type (Typ)));
2668 if Null_Present (Record_Def) then
2671 Gather_Components (Typ,
2672 Component_List (Record_Def),
2673 Governed_By => New_Assoc_List,
2675 Report_Errors => Errors_Found);
2679 if Errors_Found then
2684 -- STEP 6: Find component Values
2687 Component_Elmt := First_Elmt (Components);
2689 -- First scan the remaining positional associations in the aggregate.
2690 -- Remember that at this point Positional_Expr contains the current
2691 -- positional association if any is left after looking for discriminant
2692 -- values in step 3.
2694 while Present (Positional_Expr) and then Present (Component_Elmt) loop
2695 Component := Node (Component_Elmt);
2696 Resolve_Aggr_Expr (Positional_Expr, Component);
2698 if Present (Get_Value (Component, Component_Associations (N))) then
2700 ("more than one value supplied for Component &", N, Component);
2703 Next (Positional_Expr);
2704 Next_Elmt (Component_Elmt);
2707 if Present (Positional_Expr) then
2709 ("too many components for record aggregate", Positional_Expr);
2712 -- Now scan for the named arguments of the aggregate
2714 while Present (Component_Elmt) loop
2715 Component := Node (Component_Elmt);
2716 Expr := Get_Value (Component, Component_Associations (N), True);
2719 Error_Msg_NE ("no value supplied for component &!", N, Component);
2721 Resolve_Aggr_Expr (Expr, Component);
2724 Next_Elmt (Component_Elmt);
2727 -- STEP 7: check for invalid components + check type in choice list
2734 -- Type of first component in choice list
2737 if Present (Component_Associations (N)) then
2738 Assoc := First (Component_Associations (N));
2743 Verification : while Present (Assoc) loop
2744 Selectr := First (Choices (Assoc));
2747 if Nkind (Selectr) = N_Others_Choice then
2748 if No (Others_Etype) then
2750 ("OTHERS must represent at least one component", Selectr);
2756 while Present (Selectr) loop
2757 New_Assoc := First (New_Assoc_List);
2758 while Present (New_Assoc) loop
2759 Component := First (Choices (New_Assoc));
2760 exit when Chars (Selectr) = Chars (Component);
2764 -- If no association, this is not a legal component of
2765 -- of the type in question, except if this is an internal
2766 -- component supplied by a previous expansion.
2768 if No (New_Assoc) then
2770 if Chars (Selectr) /= Name_uTag
2771 and then Chars (Selectr) /= Name_uParent
2772 and then Chars (Selectr) /= Name_uController
2774 if not Has_Discriminants (Typ) then
2775 Error_Msg_Node_2 := Typ;
2777 ("& is not a component of}",
2781 ("& is not a component of the aggregate subtype",
2785 Check_Misspelled_Component (Components, Selectr);
2788 elsif No (Typech) then
2789 Typech := Base_Type (Etype (Component));
2791 elsif Typech /= Base_Type (Etype (Component)) then
2793 ("components in choice list must have same type", Selectr);
2800 end loop Verification;
2803 -- STEP 8: replace the original aggregate
2806 New_Aggregate : Node_Id := New_Copy (N);
2809 Set_Expressions (New_Aggregate, No_List);
2810 Set_Etype (New_Aggregate, Etype (N));
2811 Set_Component_Associations (New_Aggregate, New_Assoc_List);
2813 Rewrite (N, New_Aggregate);
2815 end Resolve_Record_Aggregate;
2817 ---------------------
2818 -- Sort_Case_Table --
2819 ---------------------
2821 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is
2822 L : Int := Case_Table'First;
2823 U : Int := Case_Table'Last;
2832 T := Case_Table (K + 1);
2836 and then Expr_Value (Case_Table (J - 1).Choice_Lo) >
2837 Expr_Value (T.Choice_Lo)
2839 Case_Table (J) := Case_Table (J - 1);
2843 Case_Table (J) := T;
2846 end Sort_Case_Table;