1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2007, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, 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 COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Checks; use Checks;
28 with Einfo; use Einfo;
29 with Elists; use Elists;
30 with Errout; use Errout;
31 with Exp_Tss; use Exp_Tss;
32 with Exp_Util; use Exp_Util;
33 with Freeze; use Freeze;
34 with Itypes; use Itypes;
36 with Lib.Xref; use Lib.Xref;
37 with Namet; use Namet;
38 with Namet.Sp; use Namet.Sp;
39 with Nmake; use Nmake;
40 with Nlists; use Nlists;
43 with Sem_Cat; use Sem_Cat;
44 with Sem_Ch3; use Sem_Ch3;
45 with Sem_Ch13; use Sem_Ch13;
46 with Sem_Eval; use Sem_Eval;
47 with Sem_Res; use Sem_Res;
48 with Sem_Util; use Sem_Util;
49 with Sem_Type; use Sem_Type;
50 with Sem_Warn; use Sem_Warn;
51 with Sinfo; use Sinfo;
52 with Snames; use Snames;
53 with Stringt; use Stringt;
54 with Stand; use Stand;
55 with Targparm; use Targparm;
56 with Tbuild; use Tbuild;
57 with Uintp; use Uintp;
59 package body Sem_Aggr is
61 type Case_Bounds is record
64 Choice_Node : Node_Id;
67 type Case_Table_Type is array (Nat range <>) of Case_Bounds;
68 -- Table type used by Check_Case_Choices procedure
70 -----------------------
71 -- Local Subprograms --
72 -----------------------
74 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type);
75 -- Sort the Case Table using the Lower Bound of each Choice as the key.
76 -- A simple insertion sort is used since the number of choices in a case
77 -- statement of variant part will usually be small and probably in near
80 procedure Check_Can_Never_Be_Null (Typ : Entity_Id; Expr : Node_Id);
81 -- Ada 2005 (AI-231): Check bad usage of null for a component for which
82 -- null exclusion (NOT NULL) is specified. Typ can be an E_Array_Type for
83 -- the array case (the component type of the array will be used) or an
84 -- E_Component/E_Discriminant entity in the record case, in which case the
85 -- type of the component will be used for the test. If Typ is any other
86 -- kind of entity, the call is ignored. Expr is the component node in the
87 -- aggregate which is known to have a null value. A warning message will be
88 -- issued if the component is null excluding.
90 -- It would be better to pass the proper type for Typ ???
92 ------------------------------------------------------
93 -- Subprograms used for RECORD AGGREGATE Processing --
94 ------------------------------------------------------
96 procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id);
97 -- This procedure performs all the semantic checks required for record
98 -- aggregates. Note that for aggregates analysis and resolution go
99 -- hand in hand. Aggregate analysis has been delayed up to here and
100 -- it is done while resolving the aggregate.
102 -- N is the N_Aggregate node.
103 -- Typ is the record type for the aggregate resolution
105 -- While performing the semantic checks, this procedure builds a new
106 -- Component_Association_List where each record field appears alone in a
107 -- Component_Choice_List along with its corresponding expression. The
108 -- record fields in the Component_Association_List appear in the same order
109 -- in which they appear in the record type Typ.
111 -- Once this new Component_Association_List is built and all the semantic
112 -- checks performed, the original aggregate subtree is replaced with the
113 -- new named record aggregate just built. Note that subtree substitution is
114 -- performed with Rewrite so as to be able to retrieve the original
117 -- The aggregate subtree manipulation performed by Resolve_Record_Aggregate
118 -- yields the aggregate format expected by Gigi. Typically, this kind of
119 -- tree manipulations are done in the expander. However, because the
120 -- semantic checks that need to be performed on record aggregates really go
121 -- hand in hand with the record aggregate normalization, the aggregate
122 -- subtree transformation is performed during resolution rather than
123 -- expansion. Had we decided otherwise we would have had to duplicate most
124 -- of the code in the expansion procedure Expand_Record_Aggregate. Note,
125 -- however, that all the expansion concerning aggregates for tagged records
126 -- is done in Expand_Record_Aggregate.
128 -- The algorithm of Resolve_Record_Aggregate proceeds as follows:
130 -- 1. Make sure that the record type against which the record aggregate
131 -- has to be resolved is not abstract. Furthermore if the type is
132 -- a null aggregate make sure the input aggregate N is also null.
134 -- 2. Verify that the structure of the aggregate is that of a record
135 -- aggregate. Specifically, look for component associations and ensure
136 -- that each choice list only has identifiers or the N_Others_Choice
137 -- node. Also make sure that if present, the N_Others_Choice occurs
138 -- last and by itself.
140 -- 3. If Typ contains discriminants, the values for each discriminant
141 -- is looked for. If the record type Typ has variants, we check
142 -- that the expressions corresponding to each discriminant ruling
143 -- the (possibly nested) variant parts of Typ, are static. This
144 -- allows us to determine the variant parts to which the rest of
145 -- the aggregate must conform. The names of discriminants with their
146 -- values are saved in a new association list, New_Assoc_List which
147 -- is later augmented with the names and values of the remaining
148 -- components in the record type.
150 -- During this phase we also make sure that every discriminant is
151 -- assigned exactly one value. Note that when several values
152 -- for a given discriminant are found, semantic processing continues
153 -- looking for further errors. In this case it's the first
154 -- discriminant value found which we will be recorded.
156 -- IMPORTANT NOTE: For derived tagged types this procedure expects
157 -- First_Discriminant and Next_Discriminant to give the correct list
158 -- of discriminants, in the correct order.
160 -- 4. After all the discriminant values have been gathered, we can
161 -- set the Etype of the record aggregate. If Typ contains no
162 -- discriminants this is straightforward: the Etype of N is just
163 -- Typ, otherwise a new implicit constrained subtype of Typ is
164 -- built to be the Etype of N.
166 -- 5. Gather the remaining record components according to the discriminant
167 -- values. This involves recursively traversing the record type
168 -- structure to see what variants are selected by the given discriminant
169 -- values. This processing is a little more convoluted if Typ is a
170 -- derived tagged types since we need to retrieve the record structure
171 -- of all the ancestors of Typ.
173 -- 6. After gathering the record components we look for their values
174 -- in the record aggregate and emit appropriate error messages
175 -- should we not find such values or should they be duplicated.
177 -- 7. We then make sure no illegal component names appear in the
178 -- record aggregate and make sure that the type of the record
179 -- components appearing in a same choice list is the same.
180 -- Finally we ensure that the others choice, if present, is
181 -- used to provide the value of at least a record component.
183 -- 8. The original aggregate node is replaced with the new named
184 -- aggregate built in steps 3 through 6, as explained earlier.
186 -- Given the complexity of record aggregate resolution, the primary
187 -- goal of this routine is clarity and simplicity rather than execution
188 -- and storage efficiency. If there are only positional components in the
189 -- aggregate the running time is linear. If there are associations
190 -- the running time is still linear as long as the order of the
191 -- associations is not too far off the order of the components in the
192 -- record type. If this is not the case the running time is at worst
193 -- quadratic in the size of the association list.
195 procedure Check_Misspelled_Component
196 (Elements : Elist_Id;
197 Component : Node_Id);
198 -- Give possible misspelling diagnostic if Component is likely to be
199 -- a misspelling of one of the components of the Assoc_List.
200 -- This is called by Resolv_Aggr_Expr after producing
201 -- an invalid component error message.
203 procedure Check_Static_Discriminated_Subtype (T : Entity_Id; V : Node_Id);
204 -- An optimization: determine whether a discriminated subtype has a
205 -- static constraint, and contains array components whose length is also
206 -- static, either because they are constrained by the discriminant, or
207 -- because the original component bounds are static.
209 -----------------------------------------------------
210 -- Subprograms used for ARRAY AGGREGATE Processing --
211 -----------------------------------------------------
213 function Resolve_Array_Aggregate
216 Index_Constr : Node_Id;
217 Component_Typ : Entity_Id;
218 Others_Allowed : Boolean)
220 -- This procedure performs the semantic checks for an array aggregate.
221 -- True is returned if the aggregate resolution succeeds.
222 -- The procedure works by recursively checking each nested aggregate.
223 -- Specifically, after checking a sub-aggregate nested at the i-th level
224 -- we recursively check all the subaggregates at the i+1-st level (if any).
225 -- Note that for aggregates analysis and resolution go hand in hand.
226 -- Aggregate analysis has been delayed up to here and it is done while
227 -- resolving the aggregate.
229 -- N is the current N_Aggregate node to be checked.
231 -- Index is the index node corresponding to the array sub-aggregate that
232 -- we are currently checking (RM 4.3.3 (8)). Its Etype is the
233 -- corresponding index type (or subtype).
235 -- Index_Constr is the node giving the applicable index constraint if
236 -- any (RM 4.3.3 (10)). It "is a constraint provided by certain
237 -- contexts [...] that can be used to determine the bounds of the array
238 -- value specified by the aggregate". If Others_Allowed below is False
239 -- there is no applicable index constraint and this node is set to Index.
241 -- Component_Typ is the array component type.
243 -- Others_Allowed indicates whether an others choice is allowed
244 -- in the context where the top-level aggregate appeared.
246 -- The algorithm of Resolve_Array_Aggregate proceeds as follows:
248 -- 1. Make sure that the others choice, if present, is by itself and
249 -- appears last in the sub-aggregate. Check that we do not have
250 -- positional and named components in the array sub-aggregate (unless
251 -- the named association is an others choice). Finally if an others
252 -- choice is present, make sure it is allowed in the aggregate contex.
254 -- 2. If the array sub-aggregate contains discrete_choices:
256 -- (A) Verify their validity. Specifically verify that:
258 -- (a) If a null range is present it must be the only possible
259 -- choice in the array aggregate.
261 -- (b) Ditto for a non static range.
263 -- (c) Ditto for a non static expression.
265 -- In addition this step analyzes and resolves each discrete_choice,
266 -- making sure that its type is the type of the corresponding Index.
267 -- If we are not at the lowest array aggregate level (in the case of
268 -- multi-dimensional aggregates) then invoke Resolve_Array_Aggregate
269 -- recursively on each component expression. Otherwise, resolve the
270 -- bottom level component expressions against the expected component
271 -- type ONLY IF the component corresponds to a single discrete choice
272 -- which is not an others choice (to see why read the DELAYED
273 -- COMPONENT RESOLUTION below).
275 -- (B) Determine the bounds of the sub-aggregate and lowest and
276 -- highest choice values.
278 -- 3. For positional aggregates:
280 -- (A) Loop over the component expressions either recursively invoking
281 -- Resolve_Array_Aggregate on each of these for multi-dimensional
282 -- array aggregates or resolving the bottom level component
283 -- expressions against the expected component type.
285 -- (B) Determine the bounds of the positional sub-aggregates.
287 -- 4. Try to determine statically whether the evaluation of the array
288 -- sub-aggregate raises Constraint_Error. If yes emit proper
289 -- warnings. The precise checks are the following:
291 -- (A) Check that the index range defined by aggregate bounds is
292 -- compatible with corresponding index subtype.
293 -- We also check against the base type. In fact it could be that
294 -- Low/High bounds of the base type are static whereas those of
295 -- the index subtype are not. Thus if we can statically catch
296 -- a problem with respect to the base type we are guaranteed
297 -- that the same problem will arise with the index subtype
299 -- (B) If we are dealing with a named aggregate containing an others
300 -- choice and at least one discrete choice then make sure the range
301 -- specified by the discrete choices does not overflow the
302 -- aggregate bounds. We also check against the index type and base
303 -- type bounds for the same reasons given in (A).
305 -- (C) If we are dealing with a positional aggregate with an others
306 -- choice make sure the number of positional elements specified
307 -- does not overflow the aggregate bounds. We also check against
308 -- the index type and base type bounds as mentioned in (A).
310 -- Finally construct an N_Range node giving the sub-aggregate bounds.
311 -- Set the Aggregate_Bounds field of the sub-aggregate to be this
312 -- N_Range. The routine Array_Aggr_Subtype below uses such N_Ranges
313 -- to build the appropriate aggregate subtype. Aggregate_Bounds
314 -- information is needed during expansion.
316 -- DELAYED COMPONENT RESOLUTION: The resolution of bottom level component
317 -- expressions in an array aggregate may call Duplicate_Subexpr or some
318 -- other routine that inserts code just outside the outermost aggregate.
319 -- If the array aggregate contains discrete choices or an others choice,
320 -- this may be wrong. Consider for instance the following example.
322 -- type Rec is record
326 -- type Acc_Rec is access Rec;
327 -- Arr : array (1..3) of Acc_Rec := (1 .. 3 => new Rec);
329 -- Then the transformation of "new Rec" that occurs during resolution
330 -- entails the following code modifications
332 -- P7b : constant Acc_Rec := new Rec;
334 -- Arr : array (1..3) of Acc_Rec := (1 .. 3 => P7b);
336 -- This code transformation is clearly wrong, since we need to call
337 -- "new Rec" for each of the 3 array elements. To avoid this problem we
338 -- delay resolution of the components of non positional array aggregates
339 -- to the expansion phase. As an optimization, if the discrete choice
340 -- specifies a single value we do not delay resolution.
342 function Array_Aggr_Subtype (N : Node_Id; Typ : Node_Id) return Entity_Id;
343 -- This routine returns the type or subtype of an array aggregate.
345 -- N is the array aggregate node whose type we return.
347 -- Typ is the context type in which N occurs.
349 -- This routine creates an implicit array subtype whose bounds are
350 -- those defined by the aggregate. When this routine is invoked
351 -- Resolve_Array_Aggregate has already processed aggregate N. Thus the
352 -- Aggregate_Bounds of each sub-aggregate, is an N_Range node giving the
353 -- sub-aggregate bounds. When building the aggregate itype, this function
354 -- traverses the array aggregate N collecting such Aggregate_Bounds and
355 -- constructs the proper array aggregate itype.
357 -- Note that in the case of multidimensional aggregates each inner
358 -- sub-aggregate corresponding to a given array dimension, may provide a
359 -- different bounds. If it is possible to determine statically that
360 -- some sub-aggregates corresponding to the same index do not have the
361 -- same bounds, then a warning is emitted. If such check is not possible
362 -- statically (because some sub-aggregate bounds are dynamic expressions)
363 -- then this job is left to the expander. In all cases the particular
364 -- bounds that this function will chose for a given dimension is the first
365 -- N_Range node for a sub-aggregate corresponding to that dimension.
367 -- Note that the Raises_Constraint_Error flag of an array aggregate
368 -- whose evaluation is determined to raise CE by Resolve_Array_Aggregate,
369 -- is set in Resolve_Array_Aggregate but the aggregate is not
370 -- immediately replaced with a raise CE. In fact, Array_Aggr_Subtype must
371 -- first construct the proper itype for the aggregate (Gigi needs
372 -- this). After constructing the proper itype we will eventually replace
373 -- the top-level aggregate with a raise CE (done in Resolve_Aggregate).
374 -- Of course in cases such as:
376 -- type Arr is array (integer range <>) of Integer;
377 -- A : Arr := (positive range -1 .. 2 => 0);
379 -- The bounds of the aggregate itype are cooked up to look reasonable
380 -- (in this particular case the bounds will be 1 .. 2).
382 procedure Aggregate_Constraint_Checks
384 Check_Typ : Entity_Id);
385 -- Checks expression Exp against subtype Check_Typ. If Exp is an
386 -- aggregate and Check_Typ a constrained record type with discriminants,
387 -- we generate the appropriate discriminant checks. If Exp is an array
388 -- aggregate then emit the appropriate length checks. If Exp is a scalar
389 -- type, or a string literal, Exp is changed into Check_Typ'(Exp) to
390 -- ensure that range checks are performed at run time.
392 procedure Make_String_Into_Aggregate (N : Node_Id);
393 -- A string literal can appear in a context in which a one dimensional
394 -- array of characters is expected. This procedure simply rewrites the
395 -- string as an aggregate, prior to resolution.
397 ---------------------------------
398 -- Aggregate_Constraint_Checks --
399 ---------------------------------
401 procedure Aggregate_Constraint_Checks
403 Check_Typ : Entity_Id)
405 Exp_Typ : constant Entity_Id := Etype (Exp);
408 if Raises_Constraint_Error (Exp) then
412 -- This is really expansion activity, so make sure that expansion
413 -- is on and is allowed.
415 if not Expander_Active or else In_Default_Expression then
419 -- First check if we have to insert discriminant checks
421 if Has_Discriminants (Exp_Typ) then
422 Apply_Discriminant_Check (Exp, Check_Typ);
424 -- Next emit length checks for array aggregates
426 elsif Is_Array_Type (Exp_Typ) then
427 Apply_Length_Check (Exp, Check_Typ);
429 -- Finally emit scalar and string checks. If we are dealing with a
430 -- scalar literal we need to check by hand because the Etype of
431 -- literals is not necessarily correct.
433 elsif Is_Scalar_Type (Exp_Typ)
434 and then Compile_Time_Known_Value (Exp)
436 if Is_Out_Of_Range (Exp, Base_Type (Check_Typ)) then
437 Apply_Compile_Time_Constraint_Error
438 (Exp, "value not in range of}?", CE_Range_Check_Failed,
439 Ent => Base_Type (Check_Typ),
440 Typ => Base_Type (Check_Typ));
442 elsif Is_Out_Of_Range (Exp, Check_Typ) then
443 Apply_Compile_Time_Constraint_Error
444 (Exp, "value not in range of}?", CE_Range_Check_Failed,
448 elsif not Range_Checks_Suppressed (Check_Typ) then
449 Apply_Scalar_Range_Check (Exp, Check_Typ);
452 -- Verify that target type is also scalar, to prevent view anomalies
453 -- in instantiations.
455 elsif (Is_Scalar_Type (Exp_Typ)
456 or else Nkind (Exp) = N_String_Literal)
457 and then Is_Scalar_Type (Check_Typ)
458 and then Exp_Typ /= Check_Typ
460 if Is_Entity_Name (Exp)
461 and then Ekind (Entity (Exp)) = E_Constant
463 -- If expression is a constant, it is worthwhile checking whether
464 -- it is a bound of the type.
466 if (Is_Entity_Name (Type_Low_Bound (Check_Typ))
467 and then Entity (Exp) = Entity (Type_Low_Bound (Check_Typ)))
468 or else (Is_Entity_Name (Type_High_Bound (Check_Typ))
469 and then Entity (Exp) = Entity (Type_High_Bound (Check_Typ)))
474 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
475 Analyze_And_Resolve (Exp, Check_Typ);
476 Check_Unset_Reference (Exp);
479 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
480 Analyze_And_Resolve (Exp, Check_Typ);
481 Check_Unset_Reference (Exp);
484 -- Ada 2005 (AI-230): Generate a conversion to an anonymous access
485 -- component's type to force the appropriate accessibility checks.
487 -- Ada 2005 (AI-231): Generate conversion to the null-excluding
488 -- type to force the corresponding run-time check
490 elsif Is_Access_Type (Check_Typ)
491 and then ((Is_Local_Anonymous_Access (Check_Typ))
492 or else (Can_Never_Be_Null (Check_Typ)
493 and then not Can_Never_Be_Null (Exp_Typ)))
495 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
496 Analyze_And_Resolve (Exp, Check_Typ);
497 Check_Unset_Reference (Exp);
499 end Aggregate_Constraint_Checks;
501 ------------------------
502 -- Array_Aggr_Subtype --
503 ------------------------
505 function Array_Aggr_Subtype
510 Aggr_Dimension : constant Pos := Number_Dimensions (Typ);
511 -- Number of aggregate index dimensions
513 Aggr_Range : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
514 -- Constrained N_Range of each index dimension in our aggregate itype
516 Aggr_Low : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
517 Aggr_High : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
518 -- Low and High bounds for each index dimension in our aggregate itype
520 Is_Fully_Positional : Boolean := True;
522 procedure Collect_Aggr_Bounds (N : Node_Id; Dim : Pos);
523 -- N is an array (sub-)aggregate. Dim is the dimension corresponding to
524 -- (sub-)aggregate N. This procedure collects the constrained N_Range
525 -- nodes corresponding to each index dimension of our aggregate itype.
526 -- These N_Range nodes are collected in Aggr_Range above.
528 -- Likewise collect in Aggr_Low & Aggr_High above the low and high
529 -- bounds of each index dimension. If, when collecting, two bounds
530 -- corresponding to the same dimension are static and found to differ,
531 -- then emit a warning, and mark N as raising Constraint_Error.
533 -------------------------
534 -- Collect_Aggr_Bounds --
535 -------------------------
537 procedure Collect_Aggr_Bounds (N : Node_Id; Dim : Pos) is
538 This_Range : constant Node_Id := Aggregate_Bounds (N);
539 -- The aggregate range node of this specific sub-aggregate
541 This_Low : constant Node_Id := Low_Bound (Aggregate_Bounds (N));
542 This_High : constant Node_Id := High_Bound (Aggregate_Bounds (N));
543 -- The aggregate bounds of this specific sub-aggregate
549 -- Collect the first N_Range for a given dimension that you find.
550 -- For a given dimension they must be all equal anyway.
552 if No (Aggr_Range (Dim)) then
553 Aggr_Low (Dim) := This_Low;
554 Aggr_High (Dim) := This_High;
555 Aggr_Range (Dim) := This_Range;
558 if Compile_Time_Known_Value (This_Low) then
559 if not Compile_Time_Known_Value (Aggr_Low (Dim)) then
560 Aggr_Low (Dim) := This_Low;
562 elsif Expr_Value (This_Low) /= Expr_Value (Aggr_Low (Dim)) then
563 Set_Raises_Constraint_Error (N);
564 Error_Msg_N ("sub-aggregate low bound mismatch?", N);
566 ("\Constraint_Error will be raised at run-time?", N);
570 if Compile_Time_Known_Value (This_High) then
571 if not Compile_Time_Known_Value (Aggr_High (Dim)) then
572 Aggr_High (Dim) := This_High;
575 Expr_Value (This_High) /= Expr_Value (Aggr_High (Dim))
577 Set_Raises_Constraint_Error (N);
578 Error_Msg_N ("sub-aggregate high bound mismatch?", N);
580 ("\Constraint_Error will be raised at run-time?", N);
585 if Dim < Aggr_Dimension then
587 -- Process positional components
589 if Present (Expressions (N)) then
590 Expr := First (Expressions (N));
591 while Present (Expr) loop
592 Collect_Aggr_Bounds (Expr, Dim + 1);
597 -- Process component associations
599 if Present (Component_Associations (N)) then
600 Is_Fully_Positional := False;
602 Assoc := First (Component_Associations (N));
603 while Present (Assoc) loop
604 Expr := Expression (Assoc);
605 Collect_Aggr_Bounds (Expr, Dim + 1);
610 end Collect_Aggr_Bounds;
612 -- Array_Aggr_Subtype variables
615 -- the final itype of the overall aggregate
617 Index_Constraints : constant List_Id := New_List;
618 -- The list of index constraints of the aggregate itype
620 -- Start of processing for Array_Aggr_Subtype
623 -- Make sure that the list of index constraints is properly attached
624 -- to the tree, and then collect the aggregate bounds.
626 Set_Parent (Index_Constraints, N);
627 Collect_Aggr_Bounds (N, 1);
629 -- Build the list of constrained indices of our aggregate itype
631 for J in 1 .. Aggr_Dimension loop
632 Create_Index : declare
633 Index_Base : constant Entity_Id :=
634 Base_Type (Etype (Aggr_Range (J)));
635 Index_Typ : Entity_Id;
638 -- Construct the Index subtype, and associate it with the range
639 -- construct that generates it.
642 Create_Itype (Subtype_Kind (Ekind (Index_Base)), Aggr_Range (J));
644 Set_Etype (Index_Typ, Index_Base);
646 if Is_Character_Type (Index_Base) then
647 Set_Is_Character_Type (Index_Typ);
650 Set_Size_Info (Index_Typ, (Index_Base));
651 Set_RM_Size (Index_Typ, RM_Size (Index_Base));
652 Set_First_Rep_Item (Index_Typ, First_Rep_Item (Index_Base));
653 Set_Scalar_Range (Index_Typ, Aggr_Range (J));
655 if Is_Discrete_Or_Fixed_Point_Type (Index_Typ) then
656 Set_RM_Size (Index_Typ, UI_From_Int (Minimum_Size (Index_Typ)));
659 Set_Etype (Aggr_Range (J), Index_Typ);
661 Append (Aggr_Range (J), To => Index_Constraints);
665 -- Now build the Itype
667 Itype := Create_Itype (E_Array_Subtype, N);
669 Set_First_Rep_Item (Itype, First_Rep_Item (Typ));
670 Set_Convention (Itype, Convention (Typ));
671 Set_Depends_On_Private (Itype, Has_Private_Component (Typ));
672 Set_Etype (Itype, Base_Type (Typ));
673 Set_Has_Alignment_Clause (Itype, Has_Alignment_Clause (Typ));
674 Set_Is_Aliased (Itype, Is_Aliased (Typ));
675 Set_Depends_On_Private (Itype, Depends_On_Private (Typ));
677 Copy_Suppress_Status (Index_Check, Typ, Itype);
678 Copy_Suppress_Status (Length_Check, Typ, Itype);
680 Set_First_Index (Itype, First (Index_Constraints));
681 Set_Is_Constrained (Itype, True);
682 Set_Is_Internal (Itype, True);
683 Init_Size_Align (Itype);
685 -- A simple optimization: purely positional aggregates of static
686 -- components should be passed to gigi unexpanded whenever possible,
687 -- and regardless of the staticness of the bounds themselves. Subse-
688 -- quent checks in exp_aggr verify that type is not packed, etc.
690 Set_Size_Known_At_Compile_Time (Itype,
692 and then Comes_From_Source (N)
693 and then Size_Known_At_Compile_Time (Component_Type (Typ)));
695 -- We always need a freeze node for a packed array subtype, so that
696 -- we can build the Packed_Array_Type corresponding to the subtype.
697 -- If expansion is disabled, the packed array subtype is not built,
698 -- and we must not generate a freeze node for the type, or else it
699 -- will appear incomplete to gigi.
701 if Is_Packed (Itype) and then not In_Default_Expression
702 and then Expander_Active
704 Freeze_Itype (Itype, N);
708 end Array_Aggr_Subtype;
710 --------------------------------
711 -- Check_Misspelled_Component --
712 --------------------------------
714 procedure Check_Misspelled_Component
715 (Elements : Elist_Id;
718 Max_Suggestions : constant := 2;
720 Nr_Of_Suggestions : Natural := 0;
721 Suggestion_1 : Entity_Id := Empty;
722 Suggestion_2 : Entity_Id := Empty;
723 Component_Elmt : Elmt_Id;
726 -- All the components of List are matched against Component and
727 -- a count is maintained of possible misspellings. When at the
728 -- end of the analysis there are one or two (not more!) possible
729 -- misspellings, these misspellings will be suggested as
730 -- possible correction.
732 Component_Elmt := First_Elmt (Elements);
733 while Nr_Of_Suggestions <= Max_Suggestions
734 and then Present (Component_Elmt)
736 if Is_Bad_Spelling_Of
737 (Chars (Node (Component_Elmt)),
740 Nr_Of_Suggestions := Nr_Of_Suggestions + 1;
742 case Nr_Of_Suggestions is
743 when 1 => Suggestion_1 := Node (Component_Elmt);
744 when 2 => Suggestion_2 := Node (Component_Elmt);
749 Next_Elmt (Component_Elmt);
752 -- Report at most two suggestions
754 if Nr_Of_Suggestions = 1 then
756 ("\possible misspelling of&", Component, Suggestion_1);
758 elsif Nr_Of_Suggestions = 2 then
759 Error_Msg_Node_2 := Suggestion_2;
761 ("\possible misspelling of& or&", Component, Suggestion_1);
763 end Check_Misspelled_Component;
765 ----------------------------------------
766 -- Check_Static_Discriminated_Subtype --
767 ----------------------------------------
769 procedure Check_Static_Discriminated_Subtype (T : Entity_Id; V : Node_Id) is
770 Disc : constant Entity_Id := First_Discriminant (T);
775 if Has_Record_Rep_Clause (T) then
778 elsif Present (Next_Discriminant (Disc)) then
781 elsif Nkind (V) /= N_Integer_Literal then
785 Comp := First_Component (T);
786 while Present (Comp) loop
787 if Is_Scalar_Type (Etype (Comp)) then
790 elsif Is_Private_Type (Etype (Comp))
791 and then Present (Full_View (Etype (Comp)))
792 and then Is_Scalar_Type (Full_View (Etype (Comp)))
796 elsif Is_Array_Type (Etype (Comp)) then
797 if Is_Bit_Packed_Array (Etype (Comp)) then
801 Ind := First_Index (Etype (Comp));
802 while Present (Ind) loop
803 if Nkind (Ind) /= N_Range
804 or else Nkind (Low_Bound (Ind)) /= N_Integer_Literal
805 or else Nkind (High_Bound (Ind)) /= N_Integer_Literal
817 Next_Component (Comp);
820 -- On exit, all components have statically known sizes
822 Set_Size_Known_At_Compile_Time (T);
823 end Check_Static_Discriminated_Subtype;
825 --------------------------------
826 -- Make_String_Into_Aggregate --
827 --------------------------------
829 procedure Make_String_Into_Aggregate (N : Node_Id) is
830 Exprs : constant List_Id := New_List;
831 Loc : constant Source_Ptr := Sloc (N);
832 Str : constant String_Id := Strval (N);
833 Strlen : constant Nat := String_Length (Str);
841 for J in 1 .. Strlen loop
842 C := Get_String_Char (Str, J);
843 Set_Character_Literal_Name (C);
846 Make_Character_Literal (P,
848 Char_Literal_Value => UI_From_CC (C));
849 Set_Etype (C_Node, Any_Character);
850 Append_To (Exprs, C_Node);
853 -- something special for wide strings ???
856 New_N := Make_Aggregate (Loc, Expressions => Exprs);
857 Set_Analyzed (New_N);
858 Set_Etype (New_N, Any_Composite);
861 end Make_String_Into_Aggregate;
863 -----------------------
864 -- Resolve_Aggregate --
865 -----------------------
867 procedure Resolve_Aggregate (N : Node_Id; Typ : Entity_Id) is
868 Pkind : constant Node_Kind := Nkind (Parent (N));
870 Aggr_Subtyp : Entity_Id;
871 -- The actual aggregate subtype. This is not necessarily the same as Typ
872 -- which is the subtype of the context in which the aggregate was found.
875 -- Check for aggregates not allowed in configurable run-time mode.
876 -- We allow all cases of aggregates that do not come from source,
877 -- since these are all assumed to be small (e.g. bounds of a string
878 -- literal). We also allow aggregates of types we know to be small.
880 if not Support_Aggregates_On_Target
881 and then Comes_From_Source (N)
882 and then (not Known_Static_Esize (Typ) or else Esize (Typ) > 64)
884 Error_Msg_CRT ("aggregate", N);
887 -- Ada 2005 (AI-287): Limited aggregates allowed
889 if Is_Limited_Type (Typ) and then Ada_Version < Ada_05 then
890 Error_Msg_N ("aggregate type cannot be limited", N);
891 Explain_Limited_Type (Typ, N);
893 elsif Is_Class_Wide_Type (Typ) then
894 Error_Msg_N ("type of aggregate cannot be class-wide", N);
896 elsif Typ = Any_String
897 or else Typ = Any_Composite
899 Error_Msg_N ("no unique type for aggregate", N);
900 Set_Etype (N, Any_Composite);
902 elsif Is_Array_Type (Typ) and then Null_Record_Present (N) then
903 Error_Msg_N ("null record forbidden in array aggregate", N);
905 elsif Is_Record_Type (Typ) then
906 Resolve_Record_Aggregate (N, Typ);
908 elsif Is_Array_Type (Typ) then
910 -- First a special test, for the case of a positional aggregate
911 -- of characters which can be replaced by a string literal.
912 -- Do not perform this transformation if this was a string literal
913 -- to start with, whose components needed constraint checks, or if
914 -- the component type is non-static, because it will require those
915 -- checks and be transformed back into an aggregate.
917 if Number_Dimensions (Typ) = 1
919 (Root_Type (Component_Type (Typ)) = Standard_Character
921 Root_Type (Component_Type (Typ)) = Standard_Wide_Character
923 Root_Type (Component_Type (Typ)) = Standard_Wide_Wide_Character)
924 and then No (Component_Associations (N))
925 and then not Is_Limited_Composite (Typ)
926 and then not Is_Private_Composite (Typ)
927 and then not Is_Bit_Packed_Array (Typ)
928 and then Nkind (Original_Node (Parent (N))) /= N_String_Literal
929 and then Is_Static_Subtype (Component_Type (Typ))
935 Expr := First (Expressions (N));
936 while Present (Expr) loop
937 exit when Nkind (Expr) /= N_Character_Literal;
944 Expr := First (Expressions (N));
945 while Present (Expr) loop
946 Store_String_Char (UI_To_CC (Char_Literal_Value (Expr)));
951 Make_String_Literal (Sloc (N), End_String));
953 Analyze_And_Resolve (N, Typ);
959 -- Here if we have a real aggregate to deal with
961 Array_Aggregate : declare
962 Aggr_Resolved : Boolean;
964 Aggr_Typ : constant Entity_Id := Etype (Typ);
965 -- This is the unconstrained array type, which is the type
966 -- against which the aggregate is to be resolved. Typ itself
967 -- is the array type of the context which may not be the same
968 -- subtype as the subtype for the final aggregate.
971 -- In the following we determine whether an others choice is
972 -- allowed inside the array aggregate. The test checks the context
973 -- in which the array aggregate occurs. If the context does not
974 -- permit it, or the aggregate type is unconstrained, an others
975 -- choice is not allowed.
977 -- If expansion is disabled (generic context, or semantics-only
978 -- mode) actual subtypes cannot be constructed, and the type of
979 -- an object may be its unconstrained nominal type. However, if
980 -- the context is an assignment, we assume that "others" is
981 -- allowed, because the target of the assignment will have a
982 -- constrained subtype when fully compiled.
984 -- Note that there is no node for Explicit_Actual_Parameter.
985 -- To test for this context we therefore have to test for node
986 -- N_Parameter_Association which itself appears only if there is a
987 -- formal parameter. Consequently we also need to test for
988 -- N_Procedure_Call_Statement or N_Function_Call.
990 Set_Etype (N, Aggr_Typ); -- may be overridden later on
992 if Is_Constrained (Typ) and then
993 (Pkind = N_Assignment_Statement or else
994 Pkind = N_Parameter_Association or else
995 Pkind = N_Function_Call or else
996 Pkind = N_Procedure_Call_Statement or else
997 Pkind = N_Generic_Association or else
998 Pkind = N_Formal_Object_Declaration or else
999 Pkind = N_Simple_Return_Statement or else
1000 Pkind = N_Object_Declaration or else
1001 Pkind = N_Component_Declaration or else
1002 Pkind = N_Parameter_Specification or else
1003 Pkind = N_Qualified_Expression or else
1004 Pkind = N_Aggregate or else
1005 Pkind = N_Extension_Aggregate or else
1006 Pkind = N_Component_Association)
1009 Resolve_Array_Aggregate
1011 Index => First_Index (Aggr_Typ),
1012 Index_Constr => First_Index (Typ),
1013 Component_Typ => Component_Type (Typ),
1014 Others_Allowed => True);
1016 elsif not Expander_Active
1017 and then Pkind = N_Assignment_Statement
1020 Resolve_Array_Aggregate
1022 Index => First_Index (Aggr_Typ),
1023 Index_Constr => First_Index (Typ),
1024 Component_Typ => Component_Type (Typ),
1025 Others_Allowed => True);
1028 Resolve_Array_Aggregate
1030 Index => First_Index (Aggr_Typ),
1031 Index_Constr => First_Index (Aggr_Typ),
1032 Component_Typ => Component_Type (Typ),
1033 Others_Allowed => False);
1036 if not Aggr_Resolved then
1037 Aggr_Subtyp := Any_Composite;
1039 Aggr_Subtyp := Array_Aggr_Subtype (N, Typ);
1042 Set_Etype (N, Aggr_Subtyp);
1043 end Array_Aggregate;
1045 elsif Is_Private_Type (Typ)
1046 and then Present (Full_View (Typ))
1047 and then In_Inlined_Body
1048 and then Is_Composite_Type (Full_View (Typ))
1050 Resolve (N, Full_View (Typ));
1053 Error_Msg_N ("illegal context for aggregate", N);
1056 -- If we can determine statically that the evaluation of the
1057 -- aggregate raises Constraint_Error, then replace the
1058 -- aggregate with an N_Raise_Constraint_Error node, but set the
1059 -- Etype to the right aggregate subtype. Gigi needs this.
1061 if Raises_Constraint_Error (N) then
1062 Aggr_Subtyp := Etype (N);
1064 Make_Raise_Constraint_Error (Sloc (N),
1065 Reason => CE_Range_Check_Failed));
1066 Set_Raises_Constraint_Error (N);
1067 Set_Etype (N, Aggr_Subtyp);
1070 end Resolve_Aggregate;
1072 -----------------------------
1073 -- Resolve_Array_Aggregate --
1074 -----------------------------
1076 function Resolve_Array_Aggregate
1079 Index_Constr : Node_Id;
1080 Component_Typ : Entity_Id;
1081 Others_Allowed : Boolean)
1084 Loc : constant Source_Ptr := Sloc (N);
1086 Failure : constant Boolean := False;
1087 Success : constant Boolean := True;
1089 Index_Typ : constant Entity_Id := Etype (Index);
1090 Index_Typ_Low : constant Node_Id := Type_Low_Bound (Index_Typ);
1091 Index_Typ_High : constant Node_Id := Type_High_Bound (Index_Typ);
1092 -- The type of the index corresponding to the array sub-aggregate
1093 -- along with its low and upper bounds
1095 Index_Base : constant Entity_Id := Base_Type (Index_Typ);
1096 Index_Base_Low : constant Node_Id := Type_Low_Bound (Index_Base);
1097 Index_Base_High : constant Node_Id := Type_High_Bound (Index_Base);
1098 -- ditto for the base type
1100 function Add (Val : Uint; To : Node_Id) return Node_Id;
1101 -- Creates a new expression node where Val is added to expression To.
1102 -- Tries to constant fold whenever possible. To must be an already
1103 -- analyzed expression.
1105 procedure Check_Bound (BH : Node_Id; AH : in out Node_Id);
1106 -- Checks that AH (the upper bound of an array aggregate) is <= BH
1107 -- (the upper bound of the index base type). If the check fails a
1108 -- warning is emitted, the Raises_Constraint_Error Flag of N is set,
1109 -- and AH is replaced with a duplicate of BH.
1111 procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id);
1112 -- Checks that range AL .. AH is compatible with range L .. H. Emits a
1113 -- warning if not and sets the Raises_Constraint_Error Flag in N.
1115 procedure Check_Length (L, H : Node_Id; Len : Uint);
1116 -- Checks that range L .. H contains at least Len elements. Emits a
1117 -- warning if not and sets the Raises_Constraint_Error Flag in N.
1119 function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean;
1120 -- Returns True if range L .. H is dynamic or null
1122 procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean);
1123 -- Given expression node From, this routine sets OK to False if it
1124 -- cannot statically evaluate From. Otherwise it stores this static
1125 -- value into Value.
1127 function Resolve_Aggr_Expr
1129 Single_Elmt : Boolean)
1131 -- Resolves aggregate expression Expr. Returs False if resolution
1132 -- fails. If Single_Elmt is set to False, the expression Expr may be
1133 -- used to initialize several array aggregate elements (this can
1134 -- happen for discrete choices such as "L .. H => Expr" or the others
1135 -- choice). In this event we do not resolve Expr unless expansion is
1136 -- disabled. To know why, see the DELAYED COMPONENT RESOLUTION
1143 function Add (Val : Uint; To : Node_Id) return Node_Id is
1149 if Raises_Constraint_Error (To) then
1153 -- First test if we can do constant folding
1155 if Compile_Time_Known_Value (To)
1156 or else Nkind (To) = N_Integer_Literal
1158 Expr_Pos := Make_Integer_Literal (Loc, Expr_Value (To) + Val);
1159 Set_Is_Static_Expression (Expr_Pos);
1160 Set_Etype (Expr_Pos, Etype (To));
1161 Set_Analyzed (Expr_Pos, Analyzed (To));
1163 if not Is_Enumeration_Type (Index_Typ) then
1166 -- If we are dealing with enumeration return
1167 -- Index_Typ'Val (Expr_Pos)
1171 Make_Attribute_Reference
1173 Prefix => New_Reference_To (Index_Typ, Loc),
1174 Attribute_Name => Name_Val,
1175 Expressions => New_List (Expr_Pos));
1181 -- If we are here no constant folding possible
1183 if not Is_Enumeration_Type (Index_Base) then
1186 Left_Opnd => Duplicate_Subexpr (To),
1187 Right_Opnd => Make_Integer_Literal (Loc, Val));
1189 -- If we are dealing with enumeration return
1190 -- Index_Typ'Val (Index_Typ'Pos (To) + Val)
1194 Make_Attribute_Reference
1196 Prefix => New_Reference_To (Index_Typ, Loc),
1197 Attribute_Name => Name_Pos,
1198 Expressions => New_List (Duplicate_Subexpr (To)));
1202 Left_Opnd => To_Pos,
1203 Right_Opnd => Make_Integer_Literal (Loc, Val));
1206 Make_Attribute_Reference
1208 Prefix => New_Reference_To (Index_Typ, Loc),
1209 Attribute_Name => Name_Val,
1210 Expressions => New_List (Expr_Pos));
1220 procedure Check_Bound (BH : Node_Id; AH : in out Node_Id) is
1228 Get (Value => Val_BH, From => BH, OK => OK_BH);
1229 Get (Value => Val_AH, From => AH, OK => OK_AH);
1231 if OK_BH and then OK_AH and then Val_BH < Val_AH then
1232 Set_Raises_Constraint_Error (N);
1233 Error_Msg_N ("upper bound out of range?", AH);
1234 Error_Msg_N ("\Constraint_Error will be raised at run-time?", AH);
1236 -- You need to set AH to BH or else in the case of enumerations
1237 -- indices we will not be able to resolve the aggregate bounds.
1239 AH := Duplicate_Subexpr (BH);
1247 procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id) is
1258 pragma Warnings (Off, OK_AL);
1259 pragma Warnings (Off, OK_AH);
1262 if Raises_Constraint_Error (N)
1263 or else Dynamic_Or_Null_Range (AL, AH)
1268 Get (Value => Val_L, From => L, OK => OK_L);
1269 Get (Value => Val_H, From => H, OK => OK_H);
1271 Get (Value => Val_AL, From => AL, OK => OK_AL);
1272 Get (Value => Val_AH, From => AH, OK => OK_AH);
1274 if OK_L and then Val_L > Val_AL then
1275 Set_Raises_Constraint_Error (N);
1276 Error_Msg_N ("lower bound of aggregate out of range?", N);
1277 Error_Msg_N ("\Constraint_Error will be raised at run-time?", N);
1280 if OK_H and then Val_H < Val_AH then
1281 Set_Raises_Constraint_Error (N);
1282 Error_Msg_N ("upper bound of aggregate out of range?", N);
1283 Error_Msg_N ("\Constraint_Error will be raised at run-time?", N);
1291 procedure Check_Length (L, H : Node_Id; Len : Uint) is
1301 if Raises_Constraint_Error (N) then
1305 Get (Value => Val_L, From => L, OK => OK_L);
1306 Get (Value => Val_H, From => H, OK => OK_H);
1308 if not OK_L or else not OK_H then
1312 -- If null range length is zero
1314 if Val_L > Val_H then
1315 Range_Len := Uint_0;
1317 Range_Len := Val_H - Val_L + 1;
1320 if Range_Len < Len then
1321 Set_Raises_Constraint_Error (N);
1322 Error_Msg_N ("too many elements?", N);
1323 Error_Msg_N ("\Constraint_Error will be raised at run-time?", N);
1327 ---------------------------
1328 -- Dynamic_Or_Null_Range --
1329 ---------------------------
1331 function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean is
1339 Get (Value => Val_L, From => L, OK => OK_L);
1340 Get (Value => Val_H, From => H, OK => OK_H);
1342 return not OK_L or else not OK_H
1343 or else not Is_OK_Static_Expression (L)
1344 or else not Is_OK_Static_Expression (H)
1345 or else Val_L > Val_H;
1346 end Dynamic_Or_Null_Range;
1352 procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean) is
1356 if Compile_Time_Known_Value (From) then
1357 Value := Expr_Value (From);
1359 -- If expression From is something like Some_Type'Val (10) then
1362 elsif Nkind (From) = N_Attribute_Reference
1363 and then Attribute_Name (From) = Name_Val
1364 and then Compile_Time_Known_Value (First (Expressions (From)))
1366 Value := Expr_Value (First (Expressions (From)));
1374 -----------------------
1375 -- Resolve_Aggr_Expr --
1376 -----------------------
1378 function Resolve_Aggr_Expr
1380 Single_Elmt : Boolean)
1383 Nxt_Ind : constant Node_Id := Next_Index (Index);
1384 Nxt_Ind_Constr : constant Node_Id := Next_Index (Index_Constr);
1385 -- Index is the current index corresponding to the expresion
1387 Resolution_OK : Boolean := True;
1388 -- Set to False if resolution of the expression failed
1391 -- If the array type against which we are resolving the aggregate
1392 -- has several dimensions, the expressions nested inside the
1393 -- aggregate must be further aggregates (or strings).
1395 if Present (Nxt_Ind) then
1396 if Nkind (Expr) /= N_Aggregate then
1398 -- A string literal can appear where a one-dimensional array
1399 -- of characters is expected. If the literal looks like an
1400 -- operator, it is still an operator symbol, which will be
1401 -- transformed into a string when analyzed.
1403 if Is_Character_Type (Component_Typ)
1404 and then No (Next_Index (Nxt_Ind))
1405 and then Nkind_In (Expr, N_String_Literal, N_Operator_Symbol)
1407 -- A string literal used in a multidimensional array
1408 -- aggregate in place of the final one-dimensional
1409 -- aggregate must not be enclosed in parentheses.
1411 if Paren_Count (Expr) /= 0 then
1412 Error_Msg_N ("no parenthesis allowed here", Expr);
1415 Make_String_Into_Aggregate (Expr);
1418 Error_Msg_N ("nested array aggregate expected", Expr);
1423 -- Ada 2005 (AI-231): Propagate the type to the nested aggregate.
1424 -- Required to check the null-exclusion attribute (if present).
1425 -- This value may be overridden later on.
1427 Set_Etype (Expr, Etype (N));
1429 Resolution_OK := Resolve_Array_Aggregate
1430 (Expr, Nxt_Ind, Nxt_Ind_Constr, Component_Typ, Others_Allowed);
1432 -- Do not resolve the expressions of discrete or others choices
1433 -- unless the expression covers a single component, or the expander
1437 or else not Expander_Active
1438 or else In_Default_Expression
1440 Analyze_And_Resolve (Expr, Component_Typ);
1441 Check_Non_Static_Context (Expr);
1442 Aggregate_Constraint_Checks (Expr, Component_Typ);
1443 Check_Unset_Reference (Expr);
1446 if Raises_Constraint_Error (Expr)
1447 and then Nkind (Parent (Expr)) /= N_Component_Association
1449 Set_Raises_Constraint_Error (N);
1452 return Resolution_OK;
1453 end Resolve_Aggr_Expr;
1455 -- Variables local to Resolve_Array_Aggregate
1462 pragma Warnings (Off, Discard);
1464 Aggr_Low : Node_Id := Empty;
1465 Aggr_High : Node_Id := Empty;
1466 -- The actual low and high bounds of this sub-aggregate
1468 Choices_Low : Node_Id := Empty;
1469 Choices_High : Node_Id := Empty;
1470 -- The lowest and highest discrete choices values for a named aggregate
1472 Nb_Elements : Uint := Uint_0;
1473 -- The number of elements in a positional aggregate
1475 Others_Present : Boolean := False;
1477 Nb_Choices : Nat := 0;
1478 -- Contains the overall number of named choices in this sub-aggregate
1480 Nb_Discrete_Choices : Nat := 0;
1481 -- The overall number of discrete choices (not counting others choice)
1483 Case_Table_Size : Nat;
1484 -- Contains the size of the case table needed to sort aggregate choices
1486 -- Start of processing for Resolve_Array_Aggregate
1489 -- STEP 1: make sure the aggregate is correctly formatted
1491 if Present (Component_Associations (N)) then
1492 Assoc := First (Component_Associations (N));
1493 while Present (Assoc) loop
1494 Choice := First (Choices (Assoc));
1495 while Present (Choice) loop
1496 if Nkind (Choice) = N_Others_Choice then
1497 Others_Present := True;
1499 if Choice /= First (Choices (Assoc))
1500 or else Present (Next (Choice))
1503 ("OTHERS must appear alone in a choice list", Choice);
1507 if Present (Next (Assoc)) then
1509 ("OTHERS must appear last in an aggregate", Choice);
1513 if Ada_Version = Ada_83
1514 and then Assoc /= First (Component_Associations (N))
1515 and then Nkind_In (Parent (N), N_Assignment_Statement,
1516 N_Object_Declaration)
1519 ("(Ada 83) illegal context for OTHERS choice", N);
1523 Nb_Choices := Nb_Choices + 1;
1531 -- At this point we know that the others choice, if present, is by
1532 -- itself and appears last in the aggregate. Check if we have mixed
1533 -- positional and discrete associations (other than the others choice).
1535 if Present (Expressions (N))
1536 and then (Nb_Choices > 1
1537 or else (Nb_Choices = 1 and then not Others_Present))
1540 ("named association cannot follow positional association",
1541 First (Choices (First (Component_Associations (N)))));
1545 -- Test for the validity of an others choice if present
1547 if Others_Present and then not Others_Allowed then
1549 ("OTHERS choice not allowed here",
1550 First (Choices (First (Component_Associations (N)))));
1554 -- Protect against cascaded errors
1556 if Etype (Index_Typ) = Any_Type then
1560 -- STEP 2: Process named components
1562 if No (Expressions (N)) then
1564 if Others_Present then
1565 Case_Table_Size := Nb_Choices - 1;
1567 Case_Table_Size := Nb_Choices;
1573 -- Denote the lowest and highest values in an aggregate choice
1577 -- High end of one range and Low end of the next. Should be
1578 -- contiguous if there is no hole in the list of values.
1580 Missing_Values : Boolean;
1581 -- Set True if missing index values
1583 S_Low : Node_Id := Empty;
1584 S_High : Node_Id := Empty;
1585 -- if a choice in an aggregate is a subtype indication these
1586 -- denote the lowest and highest values of the subtype
1588 Table : Case_Table_Type (1 .. Case_Table_Size);
1589 -- Used to sort all the different choice values
1591 Single_Choice : Boolean;
1592 -- Set to true every time there is a single discrete choice in a
1593 -- discrete association
1595 Prev_Nb_Discrete_Choices : Nat;
1596 -- Used to keep track of the number of discrete choices
1597 -- in the current association.
1600 -- STEP 2 (A): Check discrete choices validity
1602 Assoc := First (Component_Associations (N));
1603 while Present (Assoc) loop
1604 Prev_Nb_Discrete_Choices := Nb_Discrete_Choices;
1605 Choice := First (Choices (Assoc));
1609 if Nkind (Choice) = N_Others_Choice then
1610 Single_Choice := False;
1613 -- Test for subtype mark without constraint
1615 elsif Is_Entity_Name (Choice) and then
1616 Is_Type (Entity (Choice))
1618 if Base_Type (Entity (Choice)) /= Index_Base then
1620 ("invalid subtype mark in aggregate choice",
1625 elsif Nkind (Choice) = N_Subtype_Indication then
1626 Resolve_Discrete_Subtype_Indication (Choice, Index_Base);
1628 -- Does the subtype indication evaluation raise CE ?
1630 Get_Index_Bounds (Subtype_Mark (Choice), S_Low, S_High);
1631 Get_Index_Bounds (Choice, Low, High);
1632 Check_Bounds (S_Low, S_High, Low, High);
1634 else -- Choice is a range or an expression
1635 Resolve (Choice, Index_Base);
1636 Check_Unset_Reference (Choice);
1637 Check_Non_Static_Context (Choice);
1639 -- Do not range check a choice. This check is redundant
1640 -- since this test is already performed when we check
1641 -- that the bounds of the array aggregate are within
1644 Set_Do_Range_Check (Choice, False);
1647 -- If we could not resolve the discrete choice stop here
1649 if Etype (Choice) = Any_Type then
1652 -- If the discrete choice raises CE get its original bounds
1654 elsif Nkind (Choice) = N_Raise_Constraint_Error then
1655 Set_Raises_Constraint_Error (N);
1656 Get_Index_Bounds (Original_Node (Choice), Low, High);
1658 -- Otherwise get its bounds as usual
1661 Get_Index_Bounds (Choice, Low, High);
1664 if (Dynamic_Or_Null_Range (Low, High)
1665 or else (Nkind (Choice) = N_Subtype_Indication
1667 Dynamic_Or_Null_Range (S_Low, S_High)))
1668 and then Nb_Choices /= 1
1671 ("dynamic or empty choice in aggregate " &
1672 "must be the only choice", Choice);
1676 Nb_Discrete_Choices := Nb_Discrete_Choices + 1;
1677 Table (Nb_Discrete_Choices).Choice_Lo := Low;
1678 Table (Nb_Discrete_Choices).Choice_Hi := High;
1684 -- Check if we have a single discrete choice and whether
1685 -- this discrete choice specifies a single value.
1688 (Nb_Discrete_Choices = Prev_Nb_Discrete_Choices + 1)
1689 and then (Low = High);
1695 -- Ada 2005 (AI-231)
1697 if Ada_Version >= Ada_05
1698 and then Known_Null (Expression (Assoc))
1700 Check_Can_Never_Be_Null (Etype (N), Expression (Assoc));
1703 -- Ada 2005 (AI-287): In case of default initialized component
1704 -- we delay the resolution to the expansion phase
1706 if Box_Present (Assoc) then
1708 -- Ada 2005 (AI-287): In case of default initialization
1709 -- of a component the expander will generate calls to
1710 -- the corresponding initialization subprogram.
1714 elsif not Resolve_Aggr_Expr (Expression (Assoc),
1715 Single_Elmt => Single_Choice)
1723 -- If aggregate contains more than one choice then these must be
1724 -- static. Sort them and check that they are contiguous
1726 if Nb_Discrete_Choices > 1 then
1727 Sort_Case_Table (Table);
1728 Missing_Values := False;
1730 Outer : for J in 1 .. Nb_Discrete_Choices - 1 loop
1731 if Expr_Value (Table (J).Choice_Hi) >=
1732 Expr_Value (Table (J + 1).Choice_Lo)
1735 ("duplicate choice values in array aggregate",
1736 Table (J).Choice_Hi);
1739 elsif not Others_Present then
1741 Hi_Val := Expr_Value (Table (J).Choice_Hi);
1742 Lo_Val := Expr_Value (Table (J + 1).Choice_Lo);
1744 -- If missing values, output error messages
1746 if Lo_Val - Hi_Val > 1 then
1748 -- Header message if not first missing value
1750 if not Missing_Values then
1752 ("missing index value(s) in array aggregate", N);
1753 Missing_Values := True;
1756 -- Output values of missing indexes
1758 Lo_Val := Lo_Val - 1;
1759 Hi_Val := Hi_Val + 1;
1761 -- Enumeration type case
1763 if Is_Enumeration_Type (Index_Typ) then
1766 (Get_Enum_Lit_From_Pos
1767 (Index_Typ, Hi_Val, Loc));
1769 if Lo_Val = Hi_Val then
1770 Error_Msg_N ("\ %", N);
1774 (Get_Enum_Lit_From_Pos
1775 (Index_Typ, Lo_Val, Loc));
1776 Error_Msg_N ("\ % .. %", N);
1779 -- Integer types case
1782 Error_Msg_Uint_1 := Hi_Val;
1784 if Lo_Val = Hi_Val then
1785 Error_Msg_N ("\ ^", N);
1787 Error_Msg_Uint_2 := Lo_Val;
1788 Error_Msg_N ("\ ^ .. ^", N);
1795 if Missing_Values then
1796 Set_Etype (N, Any_Composite);
1801 -- STEP 2 (B): Compute aggregate bounds and min/max choices values
1803 if Nb_Discrete_Choices > 0 then
1804 Choices_Low := Table (1).Choice_Lo;
1805 Choices_High := Table (Nb_Discrete_Choices).Choice_Hi;
1808 if Others_Present then
1809 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
1812 Aggr_Low := Choices_Low;
1813 Aggr_High := Choices_High;
1817 -- STEP 3: Process positional components
1820 -- STEP 3 (A): Process positional elements
1822 Expr := First (Expressions (N));
1823 Nb_Elements := Uint_0;
1824 while Present (Expr) loop
1825 Nb_Elements := Nb_Elements + 1;
1827 -- Ada 2005 (AI-231)
1829 if Ada_Version >= Ada_05
1830 and then Known_Null (Expr)
1832 Check_Can_Never_Be_Null (Etype (N), Expr);
1835 if not Resolve_Aggr_Expr (Expr, Single_Elmt => True) then
1842 if Others_Present then
1843 Assoc := Last (Component_Associations (N));
1845 -- Ada 2005 (AI-231)
1847 if Ada_Version >= Ada_05
1848 and then Known_Null (Assoc)
1850 Check_Can_Never_Be_Null (Etype (N), Expression (Assoc));
1853 -- Ada 2005 (AI-287): In case of default initialized component
1854 -- we delay the resolution to the expansion phase.
1856 if Box_Present (Assoc) then
1858 -- Ada 2005 (AI-287): In case of default initialization
1859 -- of a component the expander will generate calls to
1860 -- the corresponding initialization subprogram.
1864 elsif not Resolve_Aggr_Expr (Expression (Assoc),
1865 Single_Elmt => False)
1871 -- STEP 3 (B): Compute the aggregate bounds
1873 if Others_Present then
1874 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
1877 if Others_Allowed then
1878 Get_Index_Bounds (Index_Constr, Aggr_Low, Discard);
1880 Aggr_Low := Index_Typ_Low;
1883 Aggr_High := Add (Nb_Elements - 1, To => Aggr_Low);
1884 Check_Bound (Index_Base_High, Aggr_High);
1888 -- STEP 4: Perform static aggregate checks and save the bounds
1892 Check_Bounds (Index_Typ_Low, Index_Typ_High, Aggr_Low, Aggr_High);
1893 Check_Bounds (Index_Base_Low, Index_Base_High, Aggr_Low, Aggr_High);
1897 if Others_Present and then Nb_Discrete_Choices > 0 then
1898 Check_Bounds (Aggr_Low, Aggr_High, Choices_Low, Choices_High);
1899 Check_Bounds (Index_Typ_Low, Index_Typ_High,
1900 Choices_Low, Choices_High);
1901 Check_Bounds (Index_Base_Low, Index_Base_High,
1902 Choices_Low, Choices_High);
1906 elsif Others_Present and then Nb_Elements > 0 then
1907 Check_Length (Aggr_Low, Aggr_High, Nb_Elements);
1908 Check_Length (Index_Typ_Low, Index_Typ_High, Nb_Elements);
1909 Check_Length (Index_Base_Low, Index_Base_High, Nb_Elements);
1912 if Raises_Constraint_Error (Aggr_Low)
1913 or else Raises_Constraint_Error (Aggr_High)
1915 Set_Raises_Constraint_Error (N);
1918 Aggr_Low := Duplicate_Subexpr (Aggr_Low);
1920 -- Do not duplicate Aggr_High if Aggr_High = Aggr_Low + Nb_Elements
1921 -- since the addition node returned by Add is not yet analyzed. Attach
1922 -- to tree and analyze first. Reset analyzed flag to insure it will get
1923 -- analyzed when it is a literal bound whose type must be properly set.
1925 if Others_Present or else Nb_Discrete_Choices > 0 then
1926 Aggr_High := Duplicate_Subexpr (Aggr_High);
1928 if Etype (Aggr_High) = Universal_Integer then
1929 Set_Analyzed (Aggr_High, False);
1933 Set_Aggregate_Bounds
1934 (N, Make_Range (Loc, Low_Bound => Aggr_Low, High_Bound => Aggr_High));
1936 -- The bounds may contain expressions that must be inserted upwards.
1937 -- Attach them fully to the tree. After analysis, remove side effects
1938 -- from upper bound, if still needed.
1940 Set_Parent (Aggregate_Bounds (N), N);
1941 Analyze_And_Resolve (Aggregate_Bounds (N), Index_Typ);
1942 Check_Unset_Reference (Aggregate_Bounds (N));
1944 if not Others_Present and then Nb_Discrete_Choices = 0 then
1945 Set_High_Bound (Aggregate_Bounds (N),
1946 Duplicate_Subexpr (High_Bound (Aggregate_Bounds (N))));
1950 end Resolve_Array_Aggregate;
1952 ---------------------------------
1953 -- Resolve_Extension_Aggregate --
1954 ---------------------------------
1956 -- There are two cases to consider:
1958 -- a) If the ancestor part is a type mark, the components needed are
1959 -- the difference between the components of the expected type and the
1960 -- components of the given type mark.
1962 -- b) If the ancestor part is an expression, it must be unambiguous,
1963 -- and once we have its type we can also compute the needed components
1964 -- as in the previous case. In both cases, if the ancestor type is not
1965 -- the immediate ancestor, we have to build this ancestor recursively.
1967 -- In both cases discriminants of the ancestor type do not play a
1968 -- role in the resolution of the needed components, because inherited
1969 -- discriminants cannot be used in a type extension. As a result we can
1970 -- compute independently the list of components of the ancestor type and
1971 -- of the expected type.
1973 procedure Resolve_Extension_Aggregate (N : Node_Id; Typ : Entity_Id) is
1974 A : constant Node_Id := Ancestor_Part (N);
1979 function Valid_Ancestor_Type return Boolean;
1980 -- Verify that the type of the ancestor part is a non-private ancestor
1981 -- of the expected type.
1983 -------------------------
1984 -- Valid_Ancestor_Type --
1985 -------------------------
1987 function Valid_Ancestor_Type return Boolean is
1988 Imm_Type : Entity_Id;
1991 Imm_Type := Base_Type (Typ);
1992 while Is_Derived_Type (Imm_Type)
1993 and then Etype (Imm_Type) /= Base_Type (A_Type)
1995 Imm_Type := Etype (Base_Type (Imm_Type));
1998 if Etype (Imm_Type) /= Base_Type (A_Type) then
1999 Error_Msg_NE ("expect ancestor type of &", A, Typ);
2004 end Valid_Ancestor_Type;
2006 -- Start of processing for Resolve_Extension_Aggregate
2011 if not Is_Tagged_Type (Typ) then
2012 Error_Msg_N ("type of extension aggregate must be tagged", N);
2015 elsif Is_Limited_Type (Typ) then
2017 -- Ada 2005 (AI-287): Limited aggregates are allowed
2019 if Ada_Version < Ada_05 then
2020 Error_Msg_N ("aggregate type cannot be limited", N);
2021 Explain_Limited_Type (Typ, N);
2025 elsif Is_Class_Wide_Type (Typ) then
2026 Error_Msg_N ("aggregate cannot be of a class-wide type", N);
2030 if Is_Entity_Name (A)
2031 and then Is_Type (Entity (A))
2033 A_Type := Get_Full_View (Entity (A));
2035 if Valid_Ancestor_Type then
2036 Set_Entity (A, A_Type);
2037 Set_Etype (A, A_Type);
2039 Validate_Ancestor_Part (N);
2040 Resolve_Record_Aggregate (N, Typ);
2043 elsif Nkind (A) /= N_Aggregate then
2044 if Is_Overloaded (A) then
2047 Get_First_Interp (A, I, It);
2048 while Present (It.Typ) loop
2049 if Is_Tagged_Type (It.Typ)
2050 and then not Is_Limited_Type (It.Typ)
2052 if A_Type /= Any_Type then
2053 Error_Msg_N ("cannot resolve expression", A);
2060 Get_Next_Interp (I, It);
2063 if A_Type = Any_Type then
2065 ("ancestor part must be non-limited tagged type", A);
2070 A_Type := Etype (A);
2073 if Valid_Ancestor_Type then
2074 Resolve (A, A_Type);
2075 Check_Unset_Reference (A);
2076 Check_Non_Static_Context (A);
2078 if Is_Class_Wide_Type (Etype (A))
2079 and then Nkind (Original_Node (A)) = N_Function_Call
2081 -- If the ancestor part is a dispatching call, it appears
2082 -- statically to be a legal ancestor, but it yields any
2083 -- member of the class, and it is not possible to determine
2084 -- whether it is an ancestor of the extension aggregate (much
2085 -- less which ancestor). It is not possible to determine the
2086 -- required components of the extension part.
2088 -- This check implements AI-306, which in fact was motivated
2089 -- by an ACT query to the ARG after this test was added.
2091 Error_Msg_N ("ancestor part must be statically tagged", A);
2093 Resolve_Record_Aggregate (N, Typ);
2098 Error_Msg_N ("no unique type for this aggregate", A);
2100 end Resolve_Extension_Aggregate;
2102 ------------------------------
2103 -- Resolve_Record_Aggregate --
2104 ------------------------------
2106 procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id) is
2108 -- N_Component_Association node belonging to the input aggregate N
2111 Positional_Expr : Node_Id;
2112 Component : Entity_Id;
2113 Component_Elmt : Elmt_Id;
2115 Components : constant Elist_Id := New_Elmt_List;
2116 -- Components is the list of the record components whose value must
2117 -- be provided in the aggregate. This list does include discriminants.
2119 New_Assoc_List : constant List_Id := New_List;
2120 New_Assoc : Node_Id;
2121 -- New_Assoc_List is the newly built list of N_Component_Association
2122 -- nodes. New_Assoc is one such N_Component_Association node in it.
2123 -- Please note that while Assoc and New_Assoc contain the same
2124 -- kind of nodes, they are used to iterate over two different
2125 -- N_Component_Association lists.
2127 Others_Etype : Entity_Id := Empty;
2128 -- This variable is used to save the Etype of the last record component
2129 -- that takes its value from the others choice. Its purpose is:
2131 -- (a) make sure the others choice is useful
2133 -- (b) make sure the type of all the components whose value is
2134 -- subsumed by the others choice are the same.
2136 -- This variable is updated as a side effect of function Get_Value
2138 Is_Box_Present : Boolean := False;
2139 Others_Box : Boolean := False;
2140 -- Ada 2005 (AI-287): Variables used in case of default initialization
2141 -- to provide a functionality similar to Others_Etype. Box_Present
2142 -- indicates that the component takes its default initialization;
2143 -- Others_Box indicates that at least one component takes its default
2144 -- initialization. Similar to Others_Etype, they are also updated as a
2145 -- side effect of function Get_Value.
2147 procedure Add_Association
2148 (Component : Entity_Id;
2150 Is_Box_Present : Boolean := False);
2151 -- Builds a new N_Component_Association node which associates
2152 -- Component to expression Expr and adds it to the new association
2153 -- list New_Assoc_List being built.
2155 function Discr_Present (Discr : Entity_Id) return Boolean;
2156 -- If aggregate N is a regular aggregate this routine will return True.
2157 -- Otherwise, if N is an extension aggregate, Discr is a discriminant
2158 -- whose value may already have been specified by N's ancestor part,
2159 -- this routine checks whether this is indeed the case and if so
2160 -- returns False, signaling that no value for Discr should appear in the
2161 -- N's aggregate part. Also, in this case, the routine appends to
2162 -- New_Assoc_List Discr the discriminant value specified in the ancestor
2168 Consider_Others_Choice : Boolean := False)
2170 -- Given a record component stored in parameter Compon, the
2171 -- following function returns its value as it appears in the list
2172 -- From, which is a list of N_Component_Association nodes. If no
2173 -- component association has a choice for the searched component,
2174 -- the value provided by the others choice is returned, if there
2175 -- is one and Consider_Others_Choice is set to true. Otherwise
2176 -- Empty is returned. If there is more than one component association
2177 -- giving a value for the searched record component, an error message
2178 -- is emitted and the first found value is returned.
2180 -- If Consider_Others_Choice is set and the returned expression comes
2181 -- from the others choice, then Others_Etype is set as a side effect.
2182 -- An error message is emitted if the components taking their value
2183 -- from the others choice do not have same type.
2185 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Node_Id);
2186 -- Analyzes and resolves expression Expr against the Etype of the
2187 -- Component. This routine also applies all appropriate checks to Expr.
2188 -- It finally saves a Expr in the newly created association list that
2189 -- will be attached to the final record aggregate. Note that if the
2190 -- Parent pointer of Expr is not set then Expr was produced with a
2191 -- New_Copy_Tree or some such.
2193 ---------------------
2194 -- Add_Association --
2195 ---------------------
2197 procedure Add_Association
2198 (Component : Entity_Id;
2200 Is_Box_Present : Boolean := False)
2202 Choice_List : constant List_Id := New_List;
2203 New_Assoc : Node_Id;
2206 Append (New_Occurrence_Of (Component, Sloc (Expr)), Choice_List);
2208 Make_Component_Association (Sloc (Expr),
2209 Choices => Choice_List,
2211 Box_Present => Is_Box_Present);
2212 Append (New_Assoc, New_Assoc_List);
2213 end Add_Association;
2219 function Discr_Present (Discr : Entity_Id) return Boolean is
2220 Regular_Aggr : constant Boolean := Nkind (N) /= N_Extension_Aggregate;
2225 Discr_Expr : Node_Id;
2227 Ancestor_Typ : Entity_Id;
2228 Orig_Discr : Entity_Id;
2230 D_Val : Elmt_Id := No_Elmt; -- stop junk warning
2232 Ancestor_Is_Subtyp : Boolean;
2235 if Regular_Aggr then
2239 Ancestor := Ancestor_Part (N);
2240 Ancestor_Typ := Etype (Ancestor);
2241 Loc := Sloc (Ancestor);
2243 Ancestor_Is_Subtyp :=
2244 Is_Entity_Name (Ancestor) and then Is_Type (Entity (Ancestor));
2246 -- If the ancestor part has no discriminants clearly N's aggregate
2247 -- part must provide a value for Discr.
2249 if not Has_Discriminants (Ancestor_Typ) then
2252 -- If the ancestor part is an unconstrained subtype mark then the
2253 -- Discr must be present in N's aggregate part.
2255 elsif Ancestor_Is_Subtyp
2256 and then not Is_Constrained (Entity (Ancestor))
2261 -- Now look to see if Discr was specified in the ancestor part
2263 if Ancestor_Is_Subtyp then
2264 D_Val := First_Elmt (Discriminant_Constraint (Entity (Ancestor)));
2267 Orig_Discr := Original_Record_Component (Discr);
2269 D := First_Discriminant (Ancestor_Typ);
2270 while Present (D) loop
2272 -- If Ancestor has already specified Disc value than insert its
2273 -- value in the final aggregate.
2275 if Original_Record_Component (D) = Orig_Discr then
2276 if Ancestor_Is_Subtyp then
2277 Discr_Expr := New_Copy_Tree (Node (D_Val));
2280 Make_Selected_Component (Loc,
2281 Prefix => Duplicate_Subexpr (Ancestor),
2282 Selector_Name => New_Occurrence_Of (Discr, Loc));
2285 Resolve_Aggr_Expr (Discr_Expr, Discr);
2289 Next_Discriminant (D);
2291 if Ancestor_Is_Subtyp then
2306 Consider_Others_Choice : Boolean := False)
2310 Expr : Node_Id := Empty;
2311 Selector_Name : Node_Id;
2314 Is_Box_Present := False;
2316 if Present (From) then
2317 Assoc := First (From);
2322 while Present (Assoc) loop
2323 Selector_Name := First (Choices (Assoc));
2324 while Present (Selector_Name) loop
2325 if Nkind (Selector_Name) = N_Others_Choice then
2326 if Consider_Others_Choice and then No (Expr) then
2328 -- We need to duplicate the expression for each
2329 -- successive component covered by the others choice.
2330 -- This is redundant if the others_choice covers only
2331 -- one component (small optimization possible???), but
2332 -- indispensable otherwise, because each one must be
2333 -- expanded individually to preserve side-effects.
2335 -- Ada 2005 (AI-287): In case of default initialization
2336 -- of components, we duplicate the corresponding default
2337 -- expression (from the record type declaration). The
2338 -- copy must carry the sloc of the association (not the
2339 -- original expression) to prevent spurious elaboration
2340 -- checks when the default includes function calls.
2342 if Box_Present (Assoc) then
2344 Is_Box_Present := True;
2346 if Expander_Active then
2349 (Expression (Parent (Compon)),
2350 New_Sloc => Sloc (Assoc));
2352 return Expression (Parent (Compon));
2356 if Present (Others_Etype) and then
2357 Base_Type (Others_Etype) /= Base_Type (Etype
2360 Error_Msg_N ("components in OTHERS choice must " &
2361 "have same type", Selector_Name);
2364 Others_Etype := Etype (Compon);
2366 if Expander_Active then
2367 return New_Copy_Tree (Expression (Assoc));
2369 return Expression (Assoc);
2374 elsif Chars (Compon) = Chars (Selector_Name) then
2377 -- Ada 2005 (AI-231)
2379 if Ada_Version >= Ada_05
2380 and then Known_Null (Expression (Assoc))
2382 Check_Can_Never_Be_Null (Compon, Expression (Assoc));
2385 -- We need to duplicate the expression when several
2386 -- components are grouped together with a "|" choice.
2387 -- For instance "filed1 | filed2 => Expr"
2389 -- Ada 2005 (AI-287)
2391 if Box_Present (Assoc) then
2392 Is_Box_Present := True;
2394 -- Duplicate the default expression of the component
2395 -- from the record type declaration, so a new copy
2396 -- can be attached to the association.
2398 -- Note that we always copy the default expression,
2399 -- even when the association has a single choice, in
2400 -- order to create a proper association for the
2401 -- expanded aggregate.
2403 Expr := New_Copy_Tree (Expression (Parent (Compon)));
2406 if Present (Next (Selector_Name)) then
2407 Expr := New_Copy_Tree (Expression (Assoc));
2409 Expr := Expression (Assoc);
2413 Generate_Reference (Compon, Selector_Name);
2417 ("more than one value supplied for &",
2418 Selector_Name, Compon);
2423 Next (Selector_Name);
2432 procedure Check_Non_Limited_Type (Expr : Node_Id);
2433 -- Relax check to allow the default initialization of limited types.
2436 -- C : Lim := (..., others => <>);
2439 ----------------------------
2440 -- Check_Non_Limited_Type --
2441 ----------------------------
2443 procedure Check_Non_Limited_Type (Expr : Node_Id) is
2445 if Is_Limited_Type (Etype (Expr))
2446 and then Comes_From_Source (Expr)
2447 and then not In_Instance_Body
2449 if not OK_For_Limited_Init (Expr) then
2451 ("initialization not allowed for limited types", N);
2452 Explain_Limited_Type (Etype (Expr), Expr);
2455 end Check_Non_Limited_Type;
2457 -----------------------
2458 -- Resolve_Aggr_Expr --
2459 -----------------------
2461 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Node_Id) is
2462 New_C : Entity_Id := Component;
2463 Expr_Type : Entity_Id := Empty;
2465 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean;
2466 -- If the expression is an aggregate (possibly qualified) then its
2467 -- expansion is delayed until the enclosing aggregate is expanded
2468 -- into assignments. In that case, do not generate checks on the
2469 -- expression, because they will be generated later, and will other-
2470 -- wise force a copy (to remove side-effects) that would leave a
2471 -- dynamic-sized aggregate in the code, something that gigi cannot
2475 -- Set to True if the resolved Expr node needs to be relocated
2476 -- when attached to the newly created association list. This node
2477 -- need not be relocated if its parent pointer is not set.
2478 -- In fact in this case Expr is the output of a New_Copy_Tree call.
2479 -- if Relocate is True then we have analyzed the expression node
2480 -- in the original aggregate and hence it needs to be relocated
2481 -- when moved over the new association list.
2483 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean is
2484 Kind : constant Node_Kind := Nkind (Expr);
2486 return (Nkind_In (Kind, N_Aggregate, N_Extension_Aggregate)
2487 and then Present (Etype (Expr))
2488 and then Is_Record_Type (Etype (Expr))
2489 and then Expansion_Delayed (Expr))
2490 or else (Kind = N_Qualified_Expression
2491 and then Has_Expansion_Delayed (Expression (Expr)));
2492 end Has_Expansion_Delayed;
2494 -- Start of processing for Resolve_Aggr_Expr
2497 -- If the type of the component is elementary or the type of the
2498 -- aggregate does not contain discriminants, use the type of the
2499 -- component to resolve Expr.
2501 if Is_Elementary_Type (Etype (Component))
2502 or else not Has_Discriminants (Etype (N))
2504 Expr_Type := Etype (Component);
2506 -- Otherwise we have to pick up the new type of the component from
2507 -- the new costrained subtype of the aggregate. In fact components
2508 -- which are of a composite type might be constrained by a
2509 -- discriminant, and we want to resolve Expr against the subtype were
2510 -- all discriminant occurrences are replaced with their actual value.
2513 New_C := First_Component (Etype (N));
2514 while Present (New_C) loop
2515 if Chars (New_C) = Chars (Component) then
2516 Expr_Type := Etype (New_C);
2520 Next_Component (New_C);
2523 pragma Assert (Present (Expr_Type));
2525 -- For each range in an array type where a discriminant has been
2526 -- replaced with the constraint, check that this range is within
2527 -- the range of the base type. This checks is done in the init
2528 -- proc for regular objects, but has to be done here for
2529 -- aggregates since no init proc is called for them.
2531 if Is_Array_Type (Expr_Type) then
2534 -- Range of the current constrained index in the array
2536 Orig_Index : Node_Id := First_Index (Etype (Component));
2537 -- Range corresponding to the range Index above in the
2538 -- original unconstrained record type. The bounds of this
2539 -- range may be governed by discriminants.
2541 Unconstr_Index : Node_Id := First_Index (Etype (Expr_Type));
2542 -- Range corresponding to the range Index above for the
2543 -- unconstrained array type. This range is needed to apply
2547 Index := First_Index (Expr_Type);
2548 while Present (Index) loop
2549 if Depends_On_Discriminant (Orig_Index) then
2550 Apply_Range_Check (Index, Etype (Unconstr_Index));
2554 Next_Index (Orig_Index);
2555 Next_Index (Unconstr_Index);
2561 -- If the Parent pointer of Expr is not set, Expr is an expression
2562 -- duplicated by New_Tree_Copy (this happens for record aggregates
2563 -- that look like (Field1 | Filed2 => Expr) or (others => Expr)).
2564 -- Such a duplicated expression must be attached to the tree
2565 -- before analysis and resolution to enforce the rule that a tree
2566 -- fragment should never be analyzed or resolved unless it is
2567 -- attached to the current compilation unit.
2569 if No (Parent (Expr)) then
2570 Set_Parent (Expr, N);
2576 Analyze_And_Resolve (Expr, Expr_Type);
2577 Check_Non_Limited_Type (Expr);
2578 Check_Non_Static_Context (Expr);
2579 Check_Unset_Reference (Expr);
2581 if not Has_Expansion_Delayed (Expr) then
2582 Aggregate_Constraint_Checks (Expr, Expr_Type);
2585 if Raises_Constraint_Error (Expr) then
2586 Set_Raises_Constraint_Error (N);
2590 Add_Association (New_C, Relocate_Node (Expr));
2592 Add_Association (New_C, Expr);
2594 end Resolve_Aggr_Expr;
2596 -- Start of processing for Resolve_Record_Aggregate
2599 -- We may end up calling Duplicate_Subexpr on expressions that are
2600 -- attached to New_Assoc_List. For this reason we need to attach it
2601 -- to the tree by setting its parent pointer to N. This parent point
2602 -- will change in STEP 8 below.
2604 Set_Parent (New_Assoc_List, N);
2606 -- STEP 1: abstract type and null record verification
2608 if Is_Abstract_Type (Typ) then
2609 Error_Msg_N ("type of aggregate cannot be abstract", N);
2612 if No (First_Entity (Typ)) and then Null_Record_Present (N) then
2616 elsif Present (First_Entity (Typ))
2617 and then Null_Record_Present (N)
2618 and then not Is_Tagged_Type (Typ)
2620 Error_Msg_N ("record aggregate cannot be null", N);
2623 elsif No (First_Entity (Typ)) then
2624 Error_Msg_N ("record aggregate must be null", N);
2628 -- STEP 2: Verify aggregate structure
2631 Selector_Name : Node_Id;
2632 Bad_Aggregate : Boolean := False;
2635 if Present (Component_Associations (N)) then
2636 Assoc := First (Component_Associations (N));
2641 while Present (Assoc) loop
2642 Selector_Name := First (Choices (Assoc));
2643 while Present (Selector_Name) loop
2644 if Nkind (Selector_Name) = N_Identifier then
2647 elsif Nkind (Selector_Name) = N_Others_Choice then
2648 if Selector_Name /= First (Choices (Assoc))
2649 or else Present (Next (Selector_Name))
2651 Error_Msg_N ("OTHERS must appear alone in a choice list",
2655 elsif Present (Next (Assoc)) then
2656 Error_Msg_N ("OTHERS must appear last in an aggregate",
2660 -- (Ada2005): If this is an association with a box,
2661 -- indicate that the association need not represent
2664 elsif Box_Present (Assoc) then
2670 ("selector name should be identifier or OTHERS",
2672 Bad_Aggregate := True;
2675 Next (Selector_Name);
2681 if Bad_Aggregate then
2686 -- STEP 3: Find discriminant Values
2689 Discrim : Entity_Id;
2690 Missing_Discriminants : Boolean := False;
2693 if Present (Expressions (N)) then
2694 Positional_Expr := First (Expressions (N));
2696 Positional_Expr := Empty;
2699 if Has_Discriminants (Typ) then
2700 Discrim := First_Discriminant (Typ);
2705 -- First find the discriminant values in the positional components
2707 while Present (Discrim) and then Present (Positional_Expr) loop
2708 if Discr_Present (Discrim) then
2709 Resolve_Aggr_Expr (Positional_Expr, Discrim);
2711 -- Ada 2005 (AI-231)
2713 if Ada_Version >= Ada_05
2714 and then Known_Null (Positional_Expr)
2716 Check_Can_Never_Be_Null (Discrim, Positional_Expr);
2719 Next (Positional_Expr);
2722 if Present (Get_Value (Discrim, Component_Associations (N))) then
2724 ("more than one value supplied for discriminant&",
2728 Next_Discriminant (Discrim);
2731 -- Find remaining discriminant values, if any, among named components
2733 while Present (Discrim) loop
2734 Expr := Get_Value (Discrim, Component_Associations (N), True);
2736 if not Discr_Present (Discrim) then
2737 if Present (Expr) then
2739 ("more than one value supplied for discriminant&",
2743 elsif No (Expr) then
2745 ("no value supplied for discriminant &", N, Discrim);
2746 Missing_Discriminants := True;
2749 Resolve_Aggr_Expr (Expr, Discrim);
2752 Next_Discriminant (Discrim);
2755 if Missing_Discriminants then
2759 -- At this point and until the beginning of STEP 6, New_Assoc_List
2760 -- contains only the discriminants and their values.
2764 -- STEP 4: Set the Etype of the record aggregate
2766 -- ??? This code is pretty much a copy of Sem_Ch3.Build_Subtype. That
2767 -- routine should really be exported in sem_util or some such and used
2768 -- in sem_ch3 and here rather than have a copy of the code which is a
2769 -- maintenance nightmare.
2771 -- ??? Performace WARNING. The current implementation creates a new
2772 -- itype for all aggregates whose base type is discriminated.
2773 -- This means that for record aggregates nested inside an array
2774 -- aggregate we will create a new itype for each record aggregate
2775 -- if the array cmponent type has discriminants. For large aggregates
2776 -- this may be a problem. What should be done in this case is
2777 -- to reuse itypes as much as possible.
2779 if Has_Discriminants (Typ) then
2780 Build_Constrained_Itype : declare
2781 Loc : constant Source_Ptr := Sloc (N);
2783 Subtyp_Decl : Node_Id;
2786 C : constant List_Id := New_List;
2789 New_Assoc := First (New_Assoc_List);
2790 while Present (New_Assoc) loop
2791 Append (Duplicate_Subexpr (Expression (New_Assoc)), To => C);
2796 Make_Subtype_Indication (Loc,
2797 Subtype_Mark => New_Occurrence_Of (Base_Type (Typ), Loc),
2798 Constraint => Make_Index_Or_Discriminant_Constraint (Loc, C));
2800 Def_Id := Create_Itype (Ekind (Typ), N);
2803 Make_Subtype_Declaration (Loc,
2804 Defining_Identifier => Def_Id,
2805 Subtype_Indication => Indic);
2806 Set_Parent (Subtyp_Decl, Parent (N));
2808 -- Itypes must be analyzed with checks off (see itypes.ads)
2810 Analyze (Subtyp_Decl, Suppress => All_Checks);
2812 Set_Etype (N, Def_Id);
2813 Check_Static_Discriminated_Subtype
2814 (Def_Id, Expression (First (New_Assoc_List)));
2815 end Build_Constrained_Itype;
2821 -- STEP 5: Get remaining components according to discriminant values
2824 Record_Def : Node_Id;
2825 Parent_Typ : Entity_Id;
2826 Root_Typ : Entity_Id;
2827 Parent_Typ_List : Elist_Id;
2828 Parent_Elmt : Elmt_Id;
2829 Errors_Found : Boolean := False;
2833 if Is_Derived_Type (Typ) and then Is_Tagged_Type (Typ) then
2834 Parent_Typ_List := New_Elmt_List;
2836 -- If this is an extension aggregate, the component list must
2837 -- include all components that are not in the given ancestor
2838 -- type. Otherwise, the component list must include components
2839 -- of all ancestors, starting with the root.
2841 if Nkind (N) = N_Extension_Aggregate then
2842 Root_Typ := Base_Type (Etype (Ancestor_Part (N)));
2844 Root_Typ := Root_Type (Typ);
2846 if Nkind (Parent (Base_Type (Root_Typ))) =
2847 N_Private_Type_Declaration
2850 ("type of aggregate has private ancestor&!",
2852 Error_Msg_N ("must use extension aggregate!", N);
2856 Dnode := Declaration_Node (Base_Type (Root_Typ));
2858 -- If we don't get a full declaration, then we have some
2859 -- error which will get signalled later so skip this part.
2860 -- Otherwise, gather components of root that apply to the
2861 -- aggregate type. We use the base type in case there is an
2862 -- applicable stored constraint that renames the discriminants
2865 if Nkind (Dnode) = N_Full_Type_Declaration then
2866 Record_Def := Type_Definition (Dnode);
2867 Gather_Components (Base_Type (Typ),
2868 Component_List (Record_Def),
2869 Governed_By => New_Assoc_List,
2871 Report_Errors => Errors_Found);
2875 Parent_Typ := Base_Type (Typ);
2876 while Parent_Typ /= Root_Typ loop
2877 Prepend_Elmt (Parent_Typ, To => Parent_Typ_List);
2878 Parent_Typ := Etype (Parent_Typ);
2880 if Nkind (Parent (Base_Type (Parent_Typ))) =
2881 N_Private_Type_Declaration
2882 or else Nkind (Parent (Base_Type (Parent_Typ))) =
2883 N_Private_Extension_Declaration
2885 if Nkind (N) /= N_Extension_Aggregate then
2887 ("type of aggregate has private ancestor&!",
2889 Error_Msg_N ("must use extension aggregate!", N);
2892 elsif Parent_Typ /= Root_Typ then
2894 ("ancestor part of aggregate must be private type&",
2895 Ancestor_Part (N), Parent_Typ);
2901 -- Now collect components from all other ancestors
2903 Parent_Elmt := First_Elmt (Parent_Typ_List);
2904 while Present (Parent_Elmt) loop
2905 Parent_Typ := Node (Parent_Elmt);
2906 Record_Def := Type_Definition (Parent (Base_Type (Parent_Typ)));
2907 Gather_Components (Empty,
2908 Component_List (Record_Extension_Part (Record_Def)),
2909 Governed_By => New_Assoc_List,
2911 Report_Errors => Errors_Found);
2913 Next_Elmt (Parent_Elmt);
2917 Record_Def := Type_Definition (Parent (Base_Type (Typ)));
2919 if Null_Present (Record_Def) then
2922 Gather_Components (Base_Type (Typ),
2923 Component_List (Record_Def),
2924 Governed_By => New_Assoc_List,
2926 Report_Errors => Errors_Found);
2930 if Errors_Found then
2935 -- STEP 6: Find component Values
2938 Component_Elmt := First_Elmt (Components);
2940 -- First scan the remaining positional associations in the aggregate.
2941 -- Remember that at this point Positional_Expr contains the current
2942 -- positional association if any is left after looking for discriminant
2943 -- values in step 3.
2945 while Present (Positional_Expr) and then Present (Component_Elmt) loop
2946 Component := Node (Component_Elmt);
2947 Resolve_Aggr_Expr (Positional_Expr, Component);
2949 -- Ada 2005 (AI-231)
2951 if Ada_Version >= Ada_05
2952 and then Known_Null (Positional_Expr)
2954 Check_Can_Never_Be_Null (Component, Positional_Expr);
2957 if Present (Get_Value (Component, Component_Associations (N))) then
2959 ("more than one value supplied for Component &", N, Component);
2962 Next (Positional_Expr);
2963 Next_Elmt (Component_Elmt);
2966 if Present (Positional_Expr) then
2968 ("too many components for record aggregate", Positional_Expr);
2971 -- Now scan for the named arguments of the aggregate
2973 while Present (Component_Elmt) loop
2974 Component := Node (Component_Elmt);
2975 Expr := Get_Value (Component, Component_Associations (N), True);
2977 -- Note: The previous call to Get_Value sets the value of the
2978 -- variable Is_Box_Present.
2980 -- Ada 2005 (AI-287): Handle components with default initialization.
2981 -- Note: This feature was originally added to Ada 2005 for limited
2982 -- but it was finally allowed with any type.
2984 if Is_Box_Present then
2985 Check_Box_Component : declare
2986 Ctyp : constant Entity_Id := Etype (Component);
2989 -- If there is a default expression for the aggregate, copy
2990 -- it into a new association.
2992 -- If the component has an initialization procedure (IP) we
2993 -- pass the component to the expander, which will generate
2994 -- the call to such IP.
2996 -- If the component has discriminants, their values must
2997 -- be taken from their subtype. This is indispensable for
2998 -- constraints that are given by the current instance of an
2999 -- enclosing type, to allow the expansion of the aggregate
3000 -- to replace the reference to the current instance by the
3001 -- target object of the aggregate.
3003 if Present (Parent (Component))
3005 Nkind (Parent (Component)) = N_Component_Declaration
3006 and then Present (Expression (Parent (Component)))
3009 New_Copy_Tree (Expression (Parent (Component)),
3010 New_Sloc => Sloc (N));
3013 (Component => Component,
3015 Set_Has_Self_Reference (N);
3017 -- A box-defaulted access component gets the value null. Also
3018 -- included are components of private types whose underlying
3019 -- type is an access type. In either case set the type of the
3020 -- literal, for subsequent use in semantic checks.
3022 elsif Present (Underlying_Type (Ctyp))
3023 and then Is_Access_Type (Underlying_Type (Ctyp))
3025 if not Is_Private_Type (Ctyp) then
3026 Expr := Make_Null (Sloc (N));
3027 Set_Etype (Expr, Ctyp);
3029 (Component => Component,
3032 -- If the component's type is private with an access type as
3033 -- its underlying type then we have to create an unchecked
3034 -- conversion to satisfy type checking.
3038 Qual_Null : constant Node_Id :=
3039 Make_Qualified_Expression (Sloc (N),
3042 (Underlying_Type (Ctyp), Sloc (N)),
3043 Expression => Make_Null (Sloc (N)));
3045 Convert_Null : constant Node_Id :=
3046 Unchecked_Convert_To
3050 Analyze_And_Resolve (Convert_Null, Ctyp);
3052 (Component => Component, Expr => Convert_Null);
3056 elsif Has_Non_Null_Base_Init_Proc (Ctyp)
3057 or else not Expander_Active
3059 if Is_Record_Type (Ctyp)
3060 and then Has_Discriminants (Ctyp)
3062 -- We build a partially initialized aggregate with the
3063 -- values of the discriminants and box initialization
3064 -- for the rest, if other components are present.
3067 Loc : constant Source_Ptr := Sloc (N);
3070 Discr_Elmt : Elmt_Id;
3071 Discr_Val : Node_Id;
3075 Expr := Make_Aggregate (Loc, New_List, New_List);
3078 First_Elmt (Discriminant_Constraint (Ctyp));
3079 while Present (Discr_Elmt) loop
3080 Discr_Val := Node (Discr_Elmt);
3082 -- The constraint may be given by a discriminant
3083 -- of the enclosing type, in which case we have
3084 -- to retrieve its value, which is part of the
3085 -- current aggregate.
3087 if Is_Entity_Name (Discr_Val)
3089 Ekind (Entity (Discr_Val)) = E_Discriminant
3091 Discr := Entity (Discr_Val);
3093 Assoc := First (New_Assoc_List);
3094 while Present (Assoc) loop
3096 (Entity (First (Choices (Assoc))))
3098 Entity (First (Choices (Assoc))) = Discr
3100 Discr_Val := Expression (Assoc);
3108 (New_Copy_Tree (Discr_Val), Expressions (Expr));
3110 -- If the discriminant constraint is a current
3111 -- instance, mark the current aggregate so that
3112 -- the self-reference can be expanded later.
3114 if Nkind (Discr_Val) = N_Attribute_Reference
3115 and then Is_Entity_Name (Prefix (Discr_Val))
3116 and then Is_Type (Entity (Prefix (Discr_Val)))
3117 and then Etype (N) = Entity (Prefix (Discr_Val))
3119 Set_Has_Self_Reference (N);
3122 Next_Elmt (Discr_Elmt);
3129 -- Look for a component that is not a discriminant
3130 -- before creating an others box association.
3132 Comp := First_Component (Ctyp);
3133 while Present (Comp) loop
3134 if Ekind (Comp) = E_Component then
3136 (Make_Component_Association (Loc,
3138 New_List (Make_Others_Choice (Loc)),
3139 Expression => Empty,
3140 Box_Present => True),
3141 Component_Associations (Expr));
3145 Next_Component (Comp);
3150 (Component => Component,
3156 (Component => Component,
3158 Is_Box_Present => True);
3161 -- Otherwise we only need to resolve the expression if the
3162 -- component has partially initialized values (required to
3163 -- expand the corresponding assignments and run-time checks).
3165 elsif Present (Expr)
3166 and then Is_Partially_Initialized_Type (Ctyp)
3168 Resolve_Aggr_Expr (Expr, Component);
3170 end Check_Box_Component;
3172 elsif No (Expr) then
3174 -- Ignore hidden components associated with the position of the
3175 -- interface tags: these are initialized dynamically.
3177 if not Present (Related_Type (Component)) then
3179 ("no value supplied for component &!", N, Component);
3183 Resolve_Aggr_Expr (Expr, Component);
3186 Next_Elmt (Component_Elmt);
3189 -- STEP 7: check for invalid components + check type in choice list
3196 -- Type of first component in choice list
3199 if Present (Component_Associations (N)) then
3200 Assoc := First (Component_Associations (N));
3205 Verification : while Present (Assoc) loop
3206 Selectr := First (Choices (Assoc));
3209 if Nkind (Selectr) = N_Others_Choice then
3211 -- Ada 2005 (AI-287): others choice may have expression or box
3213 if No (Others_Etype)
3214 and then not Others_Box
3217 ("OTHERS must represent at least one component", Selectr);
3223 while Present (Selectr) loop
3224 New_Assoc := First (New_Assoc_List);
3225 while Present (New_Assoc) loop
3226 Component := First (Choices (New_Assoc));
3227 exit when Chars (Selectr) = Chars (Component);
3231 -- If no association, this is not a legal component of
3232 -- of the type in question, except if its association
3233 -- is provided with a box.
3235 if No (New_Assoc) then
3236 if Box_Present (Parent (Selectr)) then
3238 -- This may still be a bogus component with a box. Scan
3239 -- list of components to verify that a component with
3240 -- that name exists.
3246 C := First_Component (Typ);
3247 while Present (C) loop
3248 if Chars (C) = Chars (Selectr) then
3256 Error_Msg_Node_2 := Typ;
3257 Error_Msg_N ("& is not a component of}", Selectr);
3261 elsif Chars (Selectr) /= Name_uTag
3262 and then Chars (Selectr) /= Name_uParent
3263 and then Chars (Selectr) /= Name_uController
3265 if not Has_Discriminants (Typ) then
3266 Error_Msg_Node_2 := Typ;
3267 Error_Msg_N ("& is not a component of}", Selectr);
3270 ("& is not a component of the aggregate subtype",
3274 Check_Misspelled_Component (Components, Selectr);
3277 elsif No (Typech) then
3278 Typech := Base_Type (Etype (Component));
3280 elsif Typech /= Base_Type (Etype (Component)) then
3281 if not Box_Present (Parent (Selectr)) then
3283 ("components in choice list must have same type",
3292 end loop Verification;
3295 -- STEP 8: replace the original aggregate
3298 New_Aggregate : constant Node_Id := New_Copy (N);
3301 Set_Expressions (New_Aggregate, No_List);
3302 Set_Etype (New_Aggregate, Etype (N));
3303 Set_Component_Associations (New_Aggregate, New_Assoc_List);
3305 Rewrite (N, New_Aggregate);
3307 end Resolve_Record_Aggregate;
3309 -----------------------------
3310 -- Check_Can_Never_Be_Null --
3311 -----------------------------
3313 procedure Check_Can_Never_Be_Null (Typ : Entity_Id; Expr : Node_Id) is
3314 Comp_Typ : Entity_Id;
3318 (Ada_Version >= Ada_05
3319 and then Present (Expr)
3320 and then Known_Null (Expr));
3323 when E_Array_Type =>
3324 Comp_Typ := Component_Type (Typ);
3328 Comp_Typ := Etype (Typ);
3334 if Can_Never_Be_Null (Comp_Typ) then
3336 -- Here we know we have a constraint error. Note that we do not use
3337 -- Apply_Compile_Time_Constraint_Error here to the Expr, which might
3338 -- seem the more natural approach. That's because in some cases the
3339 -- components are rewritten, and the replacement would be missed.
3342 (Compile_Time_Constraint_Error
3344 "(Ada 2005) null not allowed in null-excluding component?"),
3345 Make_Raise_Constraint_Error (Sloc (Expr),
3346 Reason => CE_Access_Check_Failed));
3348 -- Set proper type for bogus component (why is this needed???)
3350 Set_Etype (Expr, Comp_Typ);
3351 Set_Analyzed (Expr);
3353 end Check_Can_Never_Be_Null;
3355 ---------------------
3356 -- Sort_Case_Table --
3357 ---------------------
3359 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is
3360 L : constant Int := Case_Table'First;
3361 U : constant Int := Case_Table'Last;
3369 T := Case_Table (K + 1);
3373 and then Expr_Value (Case_Table (J - 1).Choice_Lo) >
3374 Expr_Value (T.Choice_Lo)
3376 Case_Table (J) := Case_Table (J - 1);
3380 Case_Table (J) := T;
3383 end Sort_Case_Table;