ada: Deep delta aggregates in postconditions
[official-gcc.git] / gcc / ada / sem_aggr.adb
blobd3f9a7731919c8dde1f55b4db195f56d6b7937d1
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ A G G R --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2023, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 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. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Einfo; use Einfo;
30 with Einfo.Utils; use Einfo.Utils;
31 with Elists; use Elists;
32 with Errout; use Errout;
33 with Expander; use Expander;
34 with Exp_Ch6; use Exp_Ch6;
35 with Exp_Tss; use Exp_Tss;
36 with Exp_Util; use Exp_Util;
37 with Freeze; use Freeze;
38 with Itypes; use Itypes;
39 with Lib; use Lib;
40 with Lib.Xref; use Lib.Xref;
41 with Namet; use Namet;
42 with Namet.Sp; use Namet.Sp;
43 with Nmake; use Nmake;
44 with Nlists; use Nlists;
45 with Opt; use Opt;
46 with Restrict; use Restrict;
47 with Rident; use Rident;
48 with Sem; use Sem;
49 with Sem_Aux; use Sem_Aux;
50 with Sem_Case; use Sem_Case;
51 with Sem_Cat; use Sem_Cat;
52 with Sem_Ch3; use Sem_Ch3;
53 with Sem_Ch8; use Sem_Ch8;
54 with Sem_Ch13; use Sem_Ch13;
55 with Sem_Dim; use Sem_Dim;
56 with Sem_Eval; use Sem_Eval;
57 with Sem_Res; use Sem_Res;
58 with Sem_Util; use Sem_Util;
59 with Sem_Type; use Sem_Type;
60 with Sem_Warn; use Sem_Warn;
61 with Sinfo; use Sinfo;
62 with Sinfo.Nodes; use Sinfo.Nodes;
63 with Sinfo.Utils; use Sinfo.Utils;
64 with Snames; use Snames;
65 with Stringt; use Stringt;
66 with Stand; use Stand;
67 with Style; use Style;
68 with Targparm; use Targparm;
69 with Tbuild; use Tbuild;
70 with Ttypes; use Ttypes;
71 with Uintp; use Uintp;
72 with Warnsw; use Warnsw;
74 package body Sem_Aggr is
76 type Case_Bounds is record
77 Lo : Node_Id;
78 -- Low bound of choice. Once we sort the Case_Table, then entries
79 -- will be in order of ascending Choice_Lo values.
81 Hi : Node_Id;
82 -- High Bound of choice. The sort does not pay any attention to the
83 -- high bound, so choices 1 .. 4 and 1 .. 5 could be in either order.
85 Highest : Uint;
86 -- If there are duplicates or missing entries, then in the sorted
87 -- table, this records the highest value among Choice_Hi values
88 -- seen so far, including this entry.
90 Choice : Node_Id;
91 -- The node of the choice
92 end record;
94 type Case_Table_Type is array (Pos range <>) of Case_Bounds;
95 -- Table type used by Check_Case_Choices procedure
97 -----------------------
98 -- Local Subprograms --
99 -----------------------
101 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type);
102 -- Sort the Case Table using the Lower Bound of each Choice as the key. A
103 -- simple insertion sort is used since the choices in a case statement will
104 -- usually be in near sorted order.
106 procedure Check_Can_Never_Be_Null (Typ : Entity_Id; Expr : Node_Id);
107 -- Ada 2005 (AI-231): Check bad usage of null for a component for which
108 -- null exclusion (NOT NULL) is specified. Typ can be an E_Array_Type for
109 -- the array case (the component type of the array will be used) or an
110 -- E_Component/E_Discriminant entity in the record case, in which case the
111 -- type of the component will be used for the test. If Typ is any other
112 -- kind of entity, the call is ignored. Expr is the component node in the
113 -- aggregate which is known to have a null value. A warning message will be
114 -- issued if the component is null excluding.
116 -- It would be better to pass the proper type for Typ ???
118 procedure Check_Expr_OK_In_Limited_Aggregate (Expr : Node_Id);
119 -- Check that Expr is either not limited or else is one of the cases of
120 -- expressions allowed for a limited component association (namely, an
121 -- aggregate, function call, or <> notation). Report error for violations.
122 -- Expression is also OK in an instance or inlining context, because we
123 -- have already preanalyzed and it is known to be type correct.
125 ------------------------------------------------------
126 -- Subprograms used for RECORD AGGREGATE Processing --
127 ------------------------------------------------------
129 procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id);
130 -- This procedure performs all the semantic checks required for record
131 -- aggregates. Note that for aggregates analysis and resolution go
132 -- hand in hand. Aggregate analysis has been delayed up to here and
133 -- it is done while resolving the aggregate.
135 -- N is the N_Aggregate node.
136 -- Typ is the record type for the aggregate resolution
138 -- While performing the semantic checks, this procedure builds a new
139 -- Component_Association_List where each record field appears alone in a
140 -- Component_Choice_List along with its corresponding expression. The
141 -- record fields in the Component_Association_List appear in the same order
142 -- in which they appear in the record type Typ.
144 -- Once this new Component_Association_List is built and all the semantic
145 -- checks performed, the original aggregate subtree is replaced with the
146 -- new named record aggregate just built. This new record aggregate has no
147 -- positional associations, so its Expressions field is set to No_List.
148 -- Note that subtree substitution is performed with Rewrite so as to be
149 -- able to retrieve the original aggregate.
151 -- The aggregate subtree manipulation performed by Resolve_Record_Aggregate
152 -- yields the aggregate format expected by Gigi. Typically, this kind of
153 -- tree manipulations are done in the expander. However, because the
154 -- semantic checks that need to be performed on record aggregates really go
155 -- hand in hand with the record aggregate normalization, the aggregate
156 -- subtree transformation is performed during resolution rather than
157 -- expansion. Had we decided otherwise we would have had to duplicate most
158 -- of the code in the expansion procedure Expand_Record_Aggregate. Note,
159 -- however, that all the expansion concerning aggregates for tagged records
160 -- is done in Expand_Record_Aggregate.
162 -- The algorithm of Resolve_Record_Aggregate proceeds as follows:
164 -- 1. Make sure that the record type against which the record aggregate
165 -- has to be resolved is not abstract. Furthermore if the type is a
166 -- null aggregate make sure the input aggregate N is also null.
168 -- 2. Verify that the structure of the aggregate is that of a record
169 -- aggregate. Specifically, look for component associations and ensure
170 -- that each choice list only has identifiers or the N_Others_Choice
171 -- node. Also make sure that if present, the N_Others_Choice occurs
172 -- last and by itself.
174 -- 3. If Typ contains discriminants, the values for each discriminant is
175 -- looked for. If the record type Typ has variants, we check that the
176 -- expressions corresponding to each discriminant ruling the (possibly
177 -- nested) variant parts of Typ, are static. This allows us to determine
178 -- the variant parts to which the rest of the aggregate must conform.
179 -- The names of discriminants with their values are saved in a new
180 -- association list, New_Assoc_List which is later augmented with the
181 -- names and values of the remaining components in the record type.
183 -- During this phase we also make sure that every discriminant is
184 -- assigned exactly one value. Note that when several values for a given
185 -- discriminant are found, semantic processing continues looking for
186 -- further errors. In this case it's the first discriminant value found
187 -- which we will be recorded.
189 -- IMPORTANT NOTE: For derived tagged types this procedure expects
190 -- First_Discriminant and Next_Discriminant to give the correct list
191 -- of discriminants, in the correct order.
193 -- 4. After all the discriminant values have been gathered, we can set the
194 -- Etype of the record aggregate. If Typ contains no discriminants this
195 -- is straightforward: the Etype of N is just Typ, otherwise a new
196 -- implicit constrained subtype of Typ is built to be the Etype of N.
198 -- 5. Gather the remaining record components according to the discriminant
199 -- values. This involves recursively traversing the record type
200 -- structure to see what variants are selected by the given discriminant
201 -- values. This processing is a little more convoluted if Typ is a
202 -- derived tagged types since we need to retrieve the record structure
203 -- of all the ancestors of Typ.
205 -- 6. After gathering the record components we look for their values in the
206 -- record aggregate and emit appropriate error messages should we not
207 -- find such values or should they be duplicated.
209 -- 7. We then make sure no illegal component names appear in the record
210 -- aggregate and make sure that the type of the record components
211 -- appearing in a same choice list is the same. Finally we ensure that
212 -- the others choice, if present, is used to provide the value of at
213 -- least a record component.
215 -- 8. The original aggregate node is replaced with the new named aggregate
216 -- built in steps 3 through 6, as explained earlier.
218 -- Given the complexity of record aggregate resolution, the primary goal of
219 -- this routine is clarity and simplicity rather than execution and storage
220 -- efficiency. If there are only positional components in the aggregate the
221 -- running time is linear. If there are associations the running time is
222 -- still linear as long as the order of the associations is not too far off
223 -- the order of the components in the record type. If this is not the case
224 -- the running time is at worst quadratic in the size of the association
225 -- list.
227 procedure Check_Misspelled_Component
228 (Elements : Elist_Id;
229 Component : Node_Id);
230 -- Give possible misspelling diagnostic if Component is likely to be a
231 -- misspelling of one of the components of the Assoc_List. This is called
232 -- by Resolve_Aggr_Expr after producing an invalid component error message.
234 -----------------------------------------------------
235 -- Subprograms used for ARRAY AGGREGATE Processing --
236 -----------------------------------------------------
238 function Resolve_Array_Aggregate
239 (N : Node_Id;
240 Index : Node_Id;
241 Index_Constr : Node_Id;
242 Component_Typ : Entity_Id;
243 Others_Allowed : Boolean) return Boolean;
244 -- This procedure performs the semantic checks for an array aggregate.
245 -- True is returned if the aggregate resolution succeeds.
247 -- The procedure works by recursively checking each nested aggregate.
248 -- Specifically, after checking a sub-aggregate nested at the i-th level
249 -- we recursively check all the subaggregates at the i+1-st level (if any).
250 -- Note that aggregates analysis and resolution go hand in hand.
251 -- Aggregate analysis has been delayed up to here and it is done while
252 -- resolving the aggregate.
254 -- N is the current N_Aggregate node to be checked.
256 -- Index is the index node corresponding to the array sub-aggregate that
257 -- we are currently checking (RM 4.3.3 (8)). Its Etype is the
258 -- corresponding index type (or subtype).
260 -- Index_Constr is the node giving the applicable index constraint if
261 -- any (RM 4.3.3 (10)). It "is a constraint provided by certain
262 -- contexts [...] that can be used to determine the bounds of the array
263 -- value specified by the aggregate". If Others_Allowed below is False
264 -- there is no applicable index constraint and this node is set to Index.
266 -- Component_Typ is the array component type.
268 -- Others_Allowed indicates whether an others choice is allowed
269 -- in the context where the top-level aggregate appeared.
271 -- The algorithm of Resolve_Array_Aggregate proceeds as follows:
273 -- 1. Make sure that the others choice, if present, is by itself and
274 -- appears last in the sub-aggregate. Check that we do not have
275 -- positional and named components in the array sub-aggregate (unless
276 -- the named association is an others choice). Finally if an others
277 -- choice is present, make sure it is allowed in the aggregate context.
279 -- 2. If the array sub-aggregate contains discrete_choices:
281 -- (A) Verify their validity. Specifically verify that:
283 -- (a) If a null range is present it must be the only possible
284 -- choice in the array aggregate.
286 -- (b) Ditto for a non static range.
288 -- (c) Ditto for a non static expression.
290 -- In addition this step analyzes and resolves each discrete_choice,
291 -- making sure that its type is the type of the corresponding Index.
292 -- If we are not at the lowest array aggregate level (in the case of
293 -- multi-dimensional aggregates) then invoke Resolve_Array_Aggregate
294 -- recursively on each component expression. Otherwise, resolve the
295 -- bottom level component expressions against the expected component
296 -- type ONLY IF the component corresponds to a single discrete choice
297 -- which is not an others choice (to see why read the DELAYED
298 -- COMPONENT RESOLUTION below).
300 -- (B) Determine the bounds of the sub-aggregate and lowest and
301 -- highest choice values.
303 -- 3. For positional aggregates:
305 -- (A) Loop over the component expressions either recursively invoking
306 -- Resolve_Array_Aggregate on each of these for multi-dimensional
307 -- array aggregates or resolving the bottom level component
308 -- expressions against the expected component type.
310 -- (B) Determine the bounds of the positional sub-aggregates.
312 -- 4. Try to determine statically whether the evaluation of the array
313 -- sub-aggregate raises Constraint_Error. If yes emit proper
314 -- warnings. The precise checks are the following:
316 -- (A) Check that the index range defined by aggregate bounds is
317 -- compatible with corresponding index subtype.
318 -- We also check against the base type. In fact it could be that
319 -- Low/High bounds of the base type are static whereas those of
320 -- the index subtype are not. Thus if we can statically catch
321 -- a problem with respect to the base type we are guaranteed
322 -- that the same problem will arise with the index subtype
324 -- (B) If we are dealing with a named aggregate containing an others
325 -- choice and at least one discrete choice then make sure the range
326 -- specified by the discrete choices does not overflow the
327 -- aggregate bounds. We also check against the index type and base
328 -- type bounds for the same reasons given in (A).
330 -- (C) If we are dealing with a positional aggregate with an others
331 -- choice make sure the number of positional elements specified
332 -- does not overflow the aggregate bounds. We also check against
333 -- the index type and base type bounds as mentioned in (A).
335 -- Finally construct an N_Range node giving the sub-aggregate bounds.
336 -- Set the Aggregate_Bounds field of the sub-aggregate to be this
337 -- N_Range. The routine Array_Aggr_Subtype below uses such N_Ranges
338 -- to build the appropriate aggregate subtype. Aggregate_Bounds
339 -- information is needed during expansion.
341 -- DELAYED COMPONENT RESOLUTION: The resolution of bottom level component
342 -- expressions in an array aggregate may call Duplicate_Subexpr or some
343 -- other routine that inserts code just outside the outermost aggregate.
344 -- If the array aggregate contains discrete choices or an others choice,
345 -- this may be wrong. Consider for instance the following example.
347 -- type Rec is record
348 -- V : Integer := 0;
349 -- end record;
351 -- type Acc_Rec is access Rec;
352 -- Arr : array (1..3) of Acc_Rec := (1 .. 3 => new Rec);
354 -- Then the transformation of "new Rec" that occurs during resolution
355 -- entails the following code modifications
357 -- P7b : constant Acc_Rec := new Rec;
358 -- RecIP (P7b.all);
359 -- Arr : array (1..3) of Acc_Rec := (1 .. 3 => P7b);
361 -- This code transformation is clearly wrong, since we need to call
362 -- "new Rec" for each of the 3 array elements. To avoid this problem we
363 -- delay resolution of the components of non positional array aggregates
364 -- to the expansion phase. As an optimization, if the discrete choice
365 -- specifies a single value we do not delay resolution.
367 function Array_Aggr_Subtype (N : Node_Id; Typ : Entity_Id) return Entity_Id;
368 -- This routine returns the type or subtype of an array aggregate.
370 -- N is the array aggregate node whose type we return.
372 -- Typ is the context type in which N occurs.
374 -- This routine creates an implicit array subtype whose bounds are
375 -- those defined by the aggregate. When this routine is invoked
376 -- Resolve_Array_Aggregate has already processed aggregate N. Thus the
377 -- Aggregate_Bounds of each sub-aggregate, is an N_Range node giving the
378 -- sub-aggregate bounds. When building the aggregate itype, this function
379 -- traverses the array aggregate N collecting such Aggregate_Bounds and
380 -- constructs the proper array aggregate itype.
382 -- Note that in the case of multidimensional aggregates each inner
383 -- sub-aggregate corresponding to a given array dimension, may provide a
384 -- different bounds. If it is possible to determine statically that
385 -- some sub-aggregates corresponding to the same index do not have the
386 -- same bounds, then a warning is emitted. If such check is not possible
387 -- statically (because some sub-aggregate bounds are dynamic expressions)
388 -- then this job is left to the expander. In all cases the particular
389 -- bounds that this function will chose for a given dimension is the first
390 -- N_Range node for a sub-aggregate corresponding to that dimension.
392 -- Note that the Raises_Constraint_Error flag of an array aggregate
393 -- whose evaluation is determined to raise CE by Resolve_Array_Aggregate,
394 -- is set in Resolve_Array_Aggregate but the aggregate is not
395 -- immediately replaced with a raise CE. In fact, Array_Aggr_Subtype must
396 -- first construct the proper itype for the aggregate (Gigi needs
397 -- this). After constructing the proper itype we will eventually replace
398 -- the top-level aggregate with a raise CE (done in Resolve_Aggregate).
399 -- Of course in cases such as:
401 -- type Arr is array (integer range <>) of Integer;
402 -- A : Arr := (positive range -1 .. 2 => 0);
404 -- The bounds of the aggregate itype are cooked up to look reasonable
405 -- (in this particular case the bounds will be 1 .. 2).
407 procedure Make_String_Into_Aggregate (N : Node_Id);
408 -- A string literal can appear in a context in which a one dimensional
409 -- array of characters is expected. This procedure simply rewrites the
410 -- string as an aggregate, prior to resolution.
412 function Resolve_Null_Array_Aggregate (N : Node_Id) return Boolean;
413 -- For the Ada 2022 construct, build a subtype with a null range for each
414 -- dimension, using the bounds from the context subtype (if the subtype
415 -- is constrained). If the subtype is unconstrained, then the bounds
416 -- are determined in much the same way as the bounds for a null string
417 -- literal with no applicable index constraint.
419 ---------------------------------
420 -- Delta aggregate processing --
421 ---------------------------------
423 procedure Resolve_Delta_Array_Aggregate (N : Node_Id; Typ : Entity_Id);
424 procedure Resolve_Delta_Record_Aggregate (N : Node_Id; Typ : Entity_Id);
425 procedure Resolve_Deep_Delta_Assoc (N : Node_Id; Typ : Entity_Id);
426 -- Resolve the names/expressions in a component association for
427 -- a deep delta aggregate. Typ is the type of the enclosing object.
429 ------------------------
430 -- Array_Aggr_Subtype --
431 ------------------------
433 function Array_Aggr_Subtype
434 (N : Node_Id;
435 Typ : Entity_Id) return Entity_Id
437 Aggr_Dimension : constant Pos := Number_Dimensions (Typ);
438 -- Number of aggregate index dimensions
440 Aggr_Range : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
441 -- Constrained N_Range of each index dimension in our aggregate itype
443 Aggr_Low : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
444 Aggr_High : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
445 -- Low and High bounds for each index dimension in our aggregate itype
447 Is_Fully_Positional : Boolean := True;
449 procedure Collect_Aggr_Bounds (N : Node_Id; Dim : Pos);
450 -- N is an array (sub-)aggregate. Dim is the dimension corresponding
451 -- to (sub-)aggregate N. This procedure collects and removes the side
452 -- effects of the constrained N_Range nodes corresponding to each index
453 -- dimension of our aggregate itype. These N_Range nodes are collected
454 -- in Aggr_Range above.
456 -- Likewise collect in Aggr_Low & Aggr_High above the low and high
457 -- bounds of each index dimension. If, when collecting, two bounds
458 -- corresponding to the same dimension are static and found to differ,
459 -- then emit a warning, and mark N as raising Constraint_Error.
461 -------------------------
462 -- Collect_Aggr_Bounds --
463 -------------------------
465 procedure Collect_Aggr_Bounds (N : Node_Id; Dim : Pos) is
466 This_Range : constant Node_Id := Aggregate_Bounds (N);
467 -- The aggregate range node of this specific sub-aggregate
469 This_Low : constant Node_Id := Low_Bound (This_Range);
470 This_High : constant Node_Id := High_Bound (This_Range);
471 -- The aggregate bounds of this specific sub-aggregate
473 Assoc : Node_Id;
474 Expr : Node_Id;
476 begin
477 Remove_Side_Effects (This_Low, Variable_Ref => True);
478 Remove_Side_Effects (This_High, Variable_Ref => True);
480 -- Collect the first N_Range for a given dimension that you find.
481 -- For a given dimension they must be all equal anyway.
483 if No (Aggr_Range (Dim)) then
484 Aggr_Low (Dim) := This_Low;
485 Aggr_High (Dim) := This_High;
486 Aggr_Range (Dim) := This_Range;
488 else
489 if Compile_Time_Known_Value (This_Low) then
490 if not Compile_Time_Known_Value (Aggr_Low (Dim)) then
491 Aggr_Low (Dim) := This_Low;
493 elsif Expr_Value (This_Low) /= Expr_Value (Aggr_Low (Dim)) then
494 Set_Raises_Constraint_Error (N);
495 Error_Msg_Warn := SPARK_Mode /= On;
496 Error_Msg_N ("sub-aggregate low bound mismatch<<", N);
497 Error_Msg_N ("\Constraint_Error [<<", N);
498 end if;
499 end if;
501 if Compile_Time_Known_Value (This_High) then
502 if not Compile_Time_Known_Value (Aggr_High (Dim)) then
503 Aggr_High (Dim) := This_High;
505 elsif
506 Expr_Value (This_High) /= Expr_Value (Aggr_High (Dim))
507 then
508 Set_Raises_Constraint_Error (N);
509 Error_Msg_Warn := SPARK_Mode /= On;
510 Error_Msg_N ("sub-aggregate high bound mismatch<<", N);
511 Error_Msg_N ("\Constraint_Error [<<", N);
512 end if;
513 end if;
514 end if;
516 if Dim < Aggr_Dimension then
518 -- Process positional components
520 if Present (Expressions (N)) then
521 Expr := First (Expressions (N));
522 while Present (Expr) loop
523 Collect_Aggr_Bounds (Expr, Dim + 1);
524 Next (Expr);
525 end loop;
526 end if;
528 -- Process component associations
530 if Present (Component_Associations (N)) then
531 Is_Fully_Positional := False;
533 Assoc := First (Component_Associations (N));
534 while Present (Assoc) loop
535 Expr := Expression (Assoc);
536 Collect_Aggr_Bounds (Expr, Dim + 1);
537 Next (Assoc);
538 end loop;
539 end if;
540 end if;
541 end Collect_Aggr_Bounds;
543 -- Array_Aggr_Subtype variables
545 Itype : Entity_Id;
546 -- The final itype of the overall aggregate
548 Index_Constraints : constant List_Id := New_List;
549 -- The list of index constraints of the aggregate itype
551 -- Start of processing for Array_Aggr_Subtype
553 begin
554 -- Make sure that the list of index constraints is properly attached to
555 -- the tree, and then collect the aggregate bounds.
557 -- If no aggregaate bounds have been set, this is an aggregate with
558 -- iterator specifications and a dynamic size to be determined by
559 -- first pass of expanded code.
561 if No (Aggregate_Bounds (N)) then
562 return Typ;
563 end if;
565 Set_Parent (Index_Constraints, N);
567 -- When resolving a null aggregate we created a list of aggregate bounds
568 -- for the consecutive dimensions. The bounds for the first dimension
569 -- are attached as the Aggregate_Bounds of the aggregate node.
571 if Is_Null_Aggregate (N) then
572 declare
573 This_Range : Node_Id := Aggregate_Bounds (N);
574 begin
575 for J in 1 .. Aggr_Dimension loop
576 Aggr_Range (J) := This_Range;
577 Next_Index (This_Range);
579 -- Remove bounds from the list, so they can be reattached as
580 -- the First_Index/Next_Index again by the code that also
581 -- handles non-null aggregates.
583 Remove (Aggr_Range (J));
584 end loop;
585 end;
586 else
587 Collect_Aggr_Bounds (N, 1);
588 end if;
590 -- Build the list of constrained indexes of our aggregate itype
592 for J in 1 .. Aggr_Dimension loop
593 Create_Index : declare
594 Index_Base : constant Entity_Id :=
595 Base_Type (Etype (Aggr_Range (J)));
596 Index_Typ : Entity_Id;
598 begin
599 -- Construct the Index subtype, and associate it with the range
600 -- construct that generates it.
602 Index_Typ :=
603 Create_Itype (Subtype_Kind (Ekind (Index_Base)), Aggr_Range (J));
605 Set_Etype (Index_Typ, Index_Base);
607 if Is_Character_Type (Index_Base) then
608 Set_Is_Character_Type (Index_Typ);
609 end if;
611 Set_Size_Info (Index_Typ, (Index_Base));
612 Set_RM_Size (Index_Typ, RM_Size (Index_Base));
613 Set_First_Rep_Item (Index_Typ, First_Rep_Item (Index_Base));
614 Set_Scalar_Range (Index_Typ, Aggr_Range (J));
616 if Is_Discrete_Or_Fixed_Point_Type (Index_Typ) then
617 Set_RM_Size (Index_Typ, UI_From_Int (Minimum_Size (Index_Typ)));
618 end if;
620 Set_Etype (Aggr_Range (J), Index_Typ);
622 Append (Aggr_Range (J), To => Index_Constraints);
623 end Create_Index;
624 end loop;
626 -- Now build the Itype
628 Itype := Create_Itype (E_Array_Subtype, N);
630 Set_First_Rep_Item (Itype, First_Rep_Item (Typ));
631 Set_Convention (Itype, Convention (Typ));
632 Set_Depends_On_Private (Itype, Has_Private_Component (Typ));
633 Set_Etype (Itype, Base_Type (Typ));
634 Set_Has_Alignment_Clause (Itype, Has_Alignment_Clause (Typ));
635 Set_Is_Aliased (Itype, Is_Aliased (Typ));
636 Set_Is_Independent (Itype, Is_Independent (Typ));
637 Set_Depends_On_Private (Itype, Depends_On_Private (Typ));
639 Copy_Suppress_Status (Index_Check, Typ, Itype);
640 Copy_Suppress_Status (Length_Check, Typ, Itype);
642 Set_First_Index (Itype, First (Index_Constraints));
643 Set_Is_Constrained (Itype, True);
644 Set_Is_Internal (Itype, True);
646 if Has_Predicates (Typ) then
647 Set_Has_Predicates (Itype);
649 -- If the base type has a predicate, capture the predicated parent
650 -- or the existing predicate function for SPARK use.
652 if Present (Predicate_Function (Typ)) then
653 Set_Predicate_Function (Itype, Predicate_Function (Typ));
655 elsif Is_Itype (Typ) then
656 Set_Predicated_Parent (Itype, Predicated_Parent (Typ));
658 else
659 Set_Predicated_Parent (Itype, Typ);
660 end if;
661 end if;
663 -- A simple optimization: purely positional aggregates of static
664 -- components should be passed to gigi unexpanded whenever possible, and
665 -- regardless of the staticness of the bounds themselves. Subsequent
666 -- checks in exp_aggr verify that type is not packed, etc.
668 Set_Size_Known_At_Compile_Time
669 (Itype,
670 Is_Fully_Positional
671 and then Comes_From_Source (N)
672 and then Size_Known_At_Compile_Time (Component_Type (Typ)));
674 -- We always need a freeze node for a packed array subtype, so that we
675 -- can build the Packed_Array_Impl_Type corresponding to the subtype. If
676 -- expansion is disabled, the packed array subtype is not built, and we
677 -- must not generate a freeze node for the type, or else it will appear
678 -- incomplete to gigi.
680 if Is_Packed (Itype)
681 and then not In_Spec_Expression
682 and then Expander_Active
683 then
684 Freeze_Itype (Itype, N);
685 end if;
687 return Itype;
688 end Array_Aggr_Subtype;
690 --------------------------------
691 -- Check_Misspelled_Component --
692 --------------------------------
694 procedure Check_Misspelled_Component
695 (Elements : Elist_Id;
696 Component : Node_Id)
698 Max_Suggestions : constant := 2;
700 Nr_Of_Suggestions : Natural := 0;
701 Suggestion_1 : Entity_Id := Empty;
702 Suggestion_2 : Entity_Id := Empty;
703 Component_Elmt : Elmt_Id;
705 begin
706 -- All the components of List are matched against Component and a count
707 -- is maintained of possible misspellings. When at the end of the
708 -- analysis there are one or two (not more) possible misspellings,
709 -- these misspellings will be suggested as possible corrections.
711 Component_Elmt := First_Elmt (Elements);
712 while Nr_Of_Suggestions <= Max_Suggestions
713 and then Present (Component_Elmt)
714 loop
715 if Is_Bad_Spelling_Of
716 (Chars (Node (Component_Elmt)),
717 Chars (Component))
718 then
719 Nr_Of_Suggestions := Nr_Of_Suggestions + 1;
721 case Nr_Of_Suggestions is
722 when 1 => Suggestion_1 := Node (Component_Elmt);
723 when 2 => Suggestion_2 := Node (Component_Elmt);
724 when others => null;
725 end case;
726 end if;
728 Next_Elmt (Component_Elmt);
729 end loop;
731 -- Report at most two suggestions
733 if Nr_Of_Suggestions = 1 then
734 Error_Msg_NE -- CODEFIX
735 ("\possible misspelling of&", Component, Suggestion_1);
737 elsif Nr_Of_Suggestions = 2 then
738 Error_Msg_Node_2 := Suggestion_2;
739 Error_Msg_NE -- CODEFIX
740 ("\possible misspelling of& or&", Component, Suggestion_1);
741 end if;
742 end Check_Misspelled_Component;
744 ----------------------------------------
745 -- Check_Expr_OK_In_Limited_Aggregate --
746 ----------------------------------------
748 procedure Check_Expr_OK_In_Limited_Aggregate (Expr : Node_Id) is
749 begin
750 if Is_Limited_Type (Etype (Expr))
751 and then Comes_From_Source (Expr)
752 then
753 if In_Instance_Body or else In_Inlined_Body then
754 null;
756 elsif not OK_For_Limited_Init (Etype (Expr), Expr) then
757 Error_Msg_N
758 ("initialization not allowed for limited types", Expr);
759 Explain_Limited_Type (Etype (Expr), Expr);
760 end if;
761 end if;
762 end Check_Expr_OK_In_Limited_Aggregate;
764 --------------------
765 -- Is_Deep_Choice --
766 --------------------
768 function Is_Deep_Choice
769 (Choice : Node_Id;
770 Aggr_Type : Type_Kind_Id) return Boolean
772 Pref : Node_Id := Choice;
773 begin
774 while not Is_Root_Prefix_Of_Deep_Choice (Pref) loop
775 Pref := Prefix (Pref);
776 end loop;
778 if Is_Array_Type (Aggr_Type) then
779 return Paren_Count (Pref) > 0
780 and then Pref /= Choice;
781 else
782 return Pref /= Choice;
783 end if;
784 end Is_Deep_Choice;
786 -------------------------
787 -- Is_Others_Aggregate --
788 -------------------------
790 function Is_Others_Aggregate (Aggr : Node_Id) return Boolean is
791 Assoc : constant List_Id := Component_Associations (Aggr);
793 begin
794 return No (Expressions (Aggr))
795 and then Nkind (First (Choice_List (First (Assoc)))) = N_Others_Choice;
796 end Is_Others_Aggregate;
798 -----------------------------------
799 -- Is_Root_Prefix_Of_Deep_Choice --
800 -----------------------------------
802 function Is_Root_Prefix_Of_Deep_Choice (Pref : Node_Id) return Boolean is
803 begin
804 return Paren_Count (Pref) > 0
805 or else Nkind (Pref) not in N_Indexed_Component
806 | N_Selected_Component;
807 end Is_Root_Prefix_Of_Deep_Choice;
809 -------------------------
810 -- Is_Single_Aggregate --
811 -------------------------
813 function Is_Single_Aggregate (Aggr : Node_Id) return Boolean is
814 Assoc : constant List_Id := Component_Associations (Aggr);
816 begin
817 return No (Expressions (Aggr))
818 and then No (Next (First (Assoc)))
819 and then No (Next (First (Choice_List (First (Assoc)))));
820 end Is_Single_Aggregate;
822 -----------------------
823 -- Is_Null_Aggregate --
824 -----------------------
826 function Is_Null_Aggregate (N : Node_Id) return Boolean is
827 begin
828 return Ada_Version >= Ada_2022
829 and then Is_Homogeneous_Aggregate (N)
830 and then Is_Empty_List (Expressions (N))
831 and then Is_Empty_List (Component_Associations (N));
832 end Is_Null_Aggregate;
834 ----------------------------------------
835 -- Is_Null_Array_Aggregate_High_Bound --
836 ----------------------------------------
838 function Is_Null_Array_Aggregate_High_Bound (N : Node_Id) return Boolean is
839 Original_N : constant Node_Id := Original_Node (N);
840 begin
841 return Ada_Version >= Ada_2022
842 and then not Comes_From_Source (Original_N)
843 and then Nkind (Original_N) = N_Attribute_Reference
844 and then
845 Get_Attribute_Id (Attribute_Name (Original_N)) = Attribute_Pred
846 and then Nkind (Parent (N)) in N_Range | N_Op_Le
847 and then not Comes_From_Source (Parent (N));
848 end Is_Null_Array_Aggregate_High_Bound;
850 --------------------------------
851 -- Make_String_Into_Aggregate --
852 --------------------------------
854 procedure Make_String_Into_Aggregate (N : Node_Id) is
855 Exprs : constant List_Id := New_List;
856 Loc : constant Source_Ptr := Sloc (N);
857 Str : constant String_Id := Strval (N);
858 Strlen : constant Nat := String_Length (Str);
859 C : Char_Code;
860 C_Node : Node_Id;
861 New_N : Node_Id;
862 P : Source_Ptr;
864 begin
865 P := Loc + 1;
866 for J in 1 .. Strlen loop
867 C := Get_String_Char (Str, J);
868 Set_Character_Literal_Name (C);
870 C_Node :=
871 Make_Character_Literal (P,
872 Chars => Name_Find,
873 Char_Literal_Value => UI_From_CC (C));
874 Set_Etype (C_Node, Any_Character);
875 Append_To (Exprs, C_Node);
877 P := P + 1;
878 -- Something special for wide strings???
879 end loop;
881 New_N := Make_Aggregate (Loc, Expressions => Exprs);
882 Set_Analyzed (New_N);
883 Set_Etype (New_N, Any_Composite);
885 Rewrite (N, New_N);
886 end Make_String_Into_Aggregate;
888 -----------------------
889 -- Resolve_Aggregate --
890 -----------------------
892 procedure Resolve_Aggregate (N : Node_Id; Typ : Entity_Id) is
893 Loc : constant Source_Ptr := Sloc (N);
895 Aggr_Subtyp : Entity_Id;
896 -- The actual aggregate subtype. This is not necessarily the same as Typ
897 -- which is the subtype of the context in which the aggregate was found.
899 Others_Box : Boolean := False;
900 -- Set to True if N represents a simple aggregate with only
901 -- (others => <>), not nested as part of another aggregate.
903 function Is_Full_Access_Aggregate (N : Node_Id) return Boolean;
904 -- If a full access object is initialized with an aggregate or is
905 -- assigned an aggregate, we have to prevent a piecemeal access or
906 -- assignment to the object, even if the aggregate is to be expanded.
907 -- We create a temporary for the aggregate, and assign the temporary
908 -- instead, so that the back end can generate an atomic move for it.
909 -- This is only done in the context of an object declaration or an
910 -- assignment. Function is a noop and returns false in other contexts.
912 function Within_Aggregate (N : Node_Id) return Boolean;
913 -- Return True if N is part of an N_Aggregate
915 ------------------------------
916 -- Is_Full_Access_Aggregate --
917 ------------------------------
919 function Is_Full_Access_Aggregate (N : Node_Id) return Boolean is
920 Loc : constant Source_Ptr := Sloc (N);
922 New_N : Node_Id;
923 Par : Node_Id;
924 Temp : Entity_Id;
925 Typ : Entity_Id;
927 begin
928 Par := Parent (N);
930 -- Aggregate may be qualified, so find outer context
932 if Nkind (Par) = N_Qualified_Expression then
933 Par := Parent (Par);
934 end if;
936 if not Comes_From_Source (Par) then
937 return False;
938 end if;
940 case Nkind (Par) is
941 when N_Assignment_Statement =>
942 Typ := Etype (Name (Par));
944 if not Is_Full_Access (Typ)
945 and then not Is_Full_Access_Object (Name (Par))
946 then
947 return False;
948 end if;
950 when N_Object_Declaration =>
951 Typ := Etype (Defining_Identifier (Par));
953 if not Is_Full_Access (Typ)
954 and then not Is_Full_Access (Defining_Identifier (Par))
955 then
956 return False;
957 end if;
959 when others =>
960 return False;
961 end case;
963 Temp := Make_Temporary (Loc, 'T', N);
964 New_N :=
965 Make_Object_Declaration (Loc,
966 Defining_Identifier => Temp,
967 Constant_Present => True,
968 Object_Definition => New_Occurrence_Of (Typ, Loc),
969 Expression => Relocate_Node (N));
970 Insert_Action (Par, New_N);
972 Rewrite (N, New_Occurrence_Of (Temp, Loc));
973 Analyze_And_Resolve (N, Typ);
975 return True;
976 end Is_Full_Access_Aggregate;
978 ----------------------
979 -- Within_Aggregate --
980 ----------------------
982 function Within_Aggregate (N : Node_Id) return Boolean is
983 P : Node_Id := Parent (N);
984 begin
985 while Present (P) loop
986 if Nkind (P) = N_Aggregate then
987 return True;
988 end if;
990 P := Parent (P);
991 end loop;
993 return False;
994 end Within_Aggregate;
996 -- Start of processing for Resolve_Aggregate
998 begin
999 -- Ignore junk empty aggregate resulting from parser error
1001 if No (Expressions (N))
1002 and then No (Component_Associations (N))
1003 and then not Null_Record_Present (N)
1004 then
1005 return;
1007 -- If the aggregate is assigned to a full access variable, we have
1008 -- to prevent a piecemeal assignment even if the aggregate is to be
1009 -- expanded. We create a temporary for the aggregate, and assign the
1010 -- temporary instead, so that the back end can generate an atomic move
1011 -- for it. This is properly an expansion activity but it must be done
1012 -- before resolution because aggregate resolution cannot be done twice.
1014 elsif Expander_Active and then Is_Full_Access_Aggregate (N) then
1015 return;
1016 end if;
1018 -- If the aggregate has box-initialized components, its type must be
1019 -- frozen so that initialization procedures can properly be called
1020 -- in the resolution that follows. The replacement of boxes with
1021 -- initialization calls is properly an expansion activity but it must
1022 -- be done during resolution.
1024 if Expander_Active
1025 and then Present (Component_Associations (N))
1026 then
1027 declare
1028 Comp : Node_Id;
1029 First_Comp : Boolean := True;
1031 begin
1032 Comp := First (Component_Associations (N));
1033 while Present (Comp) loop
1034 if Box_Present (Comp) then
1035 if First_Comp
1036 and then No (Expressions (N))
1037 and then Nkind (First (Choices (Comp))) = N_Others_Choice
1038 and then not Within_Aggregate (N)
1039 then
1040 Others_Box := True;
1041 end if;
1043 Insert_Actions (N, Freeze_Entity (Typ, N));
1044 exit;
1045 end if;
1047 First_Comp := False;
1048 Next (Comp);
1049 end loop;
1050 end;
1051 end if;
1053 -- Check for aggregates not allowed in configurable run-time mode.
1054 -- We allow all cases of aggregates that do not come from source, since
1055 -- these are all assumed to be small (e.g. bounds of a string literal).
1056 -- We also allow aggregates of types we know to be small.
1058 if not Support_Aggregates_On_Target
1059 and then Comes_From_Source (N)
1060 and then (not Known_Static_Esize (Typ)
1061 or else Esize (Typ) > System_Max_Integer_Size)
1062 then
1063 Error_Msg_CRT ("aggregate", N);
1064 end if;
1066 -- Ada 2005 (AI-287): Limited aggregates allowed
1068 -- In an instance, ignore aggregate subcomponents that may be limited,
1069 -- because they originate in view conflicts. If the original aggregate
1070 -- is legal and the actuals are legal, the aggregate itself is legal.
1072 if Is_Limited_Type (Typ)
1073 and then Ada_Version < Ada_2005
1074 and then not In_Instance
1075 then
1076 Error_Msg_N ("aggregate type cannot be limited", N);
1077 Explain_Limited_Type (Typ, N);
1079 elsif Is_Class_Wide_Type (Typ) then
1080 Error_Msg_N ("type of aggregate cannot be class-wide", N);
1082 elsif Typ = Any_String
1083 or else Typ = Any_Composite
1084 then
1085 Error_Msg_N ("no unique type for aggregate", N);
1086 Set_Etype (N, Any_Composite);
1088 elsif Is_Array_Type (Typ) and then Null_Record_Present (N) then
1089 Error_Msg_N ("null record forbidden in array aggregate", N);
1091 elsif Has_Aspect (Typ, Aspect_Aggregate)
1092 and then Ekind (Typ) /= E_Record_Type
1093 and then Ada_Version >= Ada_2022
1094 then
1095 -- Check for Ada 2022 and () aggregate.
1097 if not Is_Homogeneous_Aggregate (N) then
1098 Error_Msg_N ("container aggregate must use '['], not ()", N);
1099 end if;
1101 Resolve_Container_Aggregate (N, Typ);
1103 -- Check Ada 2022 empty aggregate [] initializing a record type that has
1104 -- aspect aggregate; the empty aggregate will be expanded into a call to
1105 -- the empty function specified in the aspect aggregate.
1107 elsif Has_Aspect (Typ, Aspect_Aggregate)
1108 and then Ekind (Typ) = E_Record_Type
1109 and then Is_Homogeneous_Aggregate (N)
1110 and then Is_Empty_List (Expressions (N))
1111 and then Is_Empty_List (Component_Associations (N))
1112 and then Ada_Version >= Ada_2022
1113 then
1114 Resolve_Container_Aggregate (N, Typ);
1116 elsif Is_Record_Type (Typ) then
1117 Resolve_Record_Aggregate (N, Typ);
1119 elsif Is_Array_Type (Typ) then
1121 -- First a special test, for the case of a positional aggregate of
1122 -- characters which can be replaced by a string literal.
1124 -- Do not perform this transformation if this was a string literal
1125 -- to start with, whose components needed constraint checks, or if
1126 -- the component type is non-static, because it will require those
1127 -- checks and be transformed back into an aggregate. If the index
1128 -- type is not Integer the aggregate may represent a user-defined
1129 -- string type but the context might need the original type so we
1130 -- do not perform the transformation at this point.
1132 if Number_Dimensions (Typ) = 1
1133 and then Is_Standard_Character_Type (Component_Type (Typ))
1134 and then No (Component_Associations (N))
1135 and then not Is_Limited_Composite (Typ)
1136 and then not Is_Private_Composite (Typ)
1137 and then not Is_Bit_Packed_Array (Typ)
1138 and then Nkind (Original_Node (Parent (N))) /= N_String_Literal
1139 and then Is_OK_Static_Subtype (Component_Type (Typ))
1140 and then Base_Type (Etype (First_Index (Typ))) =
1141 Base_Type (Standard_Integer)
1142 then
1143 declare
1144 Expr : Node_Id;
1146 begin
1147 Expr := First (Expressions (N));
1148 while Present (Expr) loop
1149 exit when Nkind (Expr) /= N_Character_Literal;
1150 Next (Expr);
1151 end loop;
1153 if No (Expr) then
1154 Start_String;
1156 Expr := First (Expressions (N));
1157 while Present (Expr) loop
1158 Store_String_Char (UI_To_CC (Char_Literal_Value (Expr)));
1159 Next (Expr);
1160 end loop;
1162 Rewrite (N, Make_String_Literal (Loc, End_String));
1164 Analyze_And_Resolve (N, Typ);
1165 return;
1166 end if;
1167 end;
1168 end if;
1170 -- Here if we have a real aggregate to deal with
1172 Array_Aggregate : declare
1173 Aggr_Resolved : Boolean;
1174 Aggr_Typ : constant Entity_Id := Etype (Typ);
1175 -- This is the unconstrained array type, which is the type against
1176 -- which the aggregate is to be resolved. Typ itself is the array
1177 -- type of the context which may not be the same subtype as the
1178 -- subtype for the final aggregate.
1180 Is_Null_Aggr : constant Boolean := Is_Null_Aggregate (N);
1182 begin
1183 -- In the following we determine whether an OTHERS choice is
1184 -- allowed inside the array aggregate. The test checks the context
1185 -- in which the array aggregate occurs. If the context does not
1186 -- permit it, or the aggregate type is unconstrained, an OTHERS
1187 -- choice is not allowed (except that it is always allowed on the
1188 -- right-hand side of an assignment statement; in this case the
1189 -- constrainedness of the type doesn't matter, because an array
1190 -- object is always constrained).
1192 -- If expansion is disabled (generic context, or semantics-only
1193 -- mode) actual subtypes cannot be constructed, and the type of an
1194 -- object may be its unconstrained nominal type. However, if the
1195 -- context is an assignment statement, OTHERS is allowed, because
1196 -- the target of the assignment will have a constrained subtype
1197 -- when fully compiled. Ditto if the context is an initialization
1198 -- procedure where a component may have a predicate function that
1199 -- carries the base type.
1201 -- Note that there is no node for Explicit_Actual_Parameter.
1202 -- To test for this context we therefore have to test for node
1203 -- N_Parameter_Association which itself appears only if there is a
1204 -- formal parameter. Consequently we also need to test for
1205 -- N_Procedure_Call_Statement or N_Function_Call.
1207 -- The context may be an N_Reference node, created by expansion.
1208 -- Legality of the others clause was established in the source,
1209 -- so the context is legal.
1211 Set_Etype (N, Aggr_Typ); -- May be overridden later on
1213 if Is_Null_Aggr then
1214 Set_Etype (N, Typ);
1215 Aggr_Resolved := Resolve_Null_Array_Aggregate (N);
1217 elsif Nkind (Parent (N)) = N_Assignment_Statement
1218 or else Inside_Init_Proc
1219 or else (Is_Constrained (Typ)
1220 and then Nkind (Parent (N)) in
1221 N_Parameter_Association
1222 | N_Function_Call
1223 | N_Procedure_Call_Statement
1224 | N_Generic_Association
1225 | N_Formal_Object_Declaration
1226 | N_Simple_Return_Statement
1227 | N_Object_Declaration
1228 | N_Component_Declaration
1229 | N_Parameter_Specification
1230 | N_Qualified_Expression
1231 | N_Unchecked_Type_Conversion
1232 | N_Reference
1233 | N_Aggregate
1234 | N_Extension_Aggregate
1235 | N_Component_Association
1236 | N_Case_Expression_Alternative
1237 | N_If_Expression
1238 | N_Expression_With_Actions)
1239 then
1240 Aggr_Resolved :=
1241 Resolve_Array_Aggregate
1243 Index => First_Index (Aggr_Typ),
1244 Index_Constr => First_Index (Typ),
1245 Component_Typ => Component_Type (Typ),
1246 Others_Allowed => True);
1247 else
1248 Aggr_Resolved :=
1249 Resolve_Array_Aggregate
1251 Index => First_Index (Aggr_Typ),
1252 Index_Constr => First_Index (Aggr_Typ),
1253 Component_Typ => Component_Type (Typ),
1254 Others_Allowed => False);
1255 end if;
1257 if not Aggr_Resolved then
1259 -- A parenthesized expression may have been intended as an
1260 -- aggregate, leading to a type error when analyzing the
1261 -- component. This can also happen for a nested component
1262 -- (see Analyze_Aggr_Expr).
1264 if Paren_Count (N) > 0 then
1265 Error_Msg_N
1266 ("positional aggregate cannot have one component", N);
1267 end if;
1269 Aggr_Subtyp := Any_Composite;
1271 else
1272 Aggr_Subtyp := Array_Aggr_Subtype (N, Typ);
1273 end if;
1275 Set_Etype (N, Aggr_Subtyp);
1276 end Array_Aggregate;
1278 elsif Is_Private_Type (Typ)
1279 and then Present (Full_View (Typ))
1280 and then (In_Inlined_Body or In_Instance_Body)
1281 and then Is_Composite_Type (Full_View (Typ))
1282 then
1283 Resolve (N, Full_View (Typ));
1285 else
1286 Error_Msg_N ("illegal context for aggregate", N);
1287 end if;
1289 -- If we can determine statically that the evaluation of the aggregate
1290 -- raises Constraint_Error, then replace the aggregate with an
1291 -- N_Raise_Constraint_Error node, but set the Etype to the right
1292 -- aggregate subtype. Gigi needs this.
1294 if Raises_Constraint_Error (N) then
1295 Aggr_Subtyp := Etype (N);
1296 Rewrite (N,
1297 Make_Raise_Constraint_Error (Loc, Reason => CE_Range_Check_Failed));
1298 Set_Raises_Constraint_Error (N);
1299 Set_Etype (N, Aggr_Subtyp);
1300 Set_Analyzed (N);
1301 end if;
1303 if Warn_On_No_Value_Assigned
1304 and then Others_Box
1305 and then not Is_Fully_Initialized_Type (Etype (N))
1306 then
1307 Error_Msg_N ("?v?aggregate not fully initialized", N);
1308 end if;
1310 Check_Function_Writable_Actuals (N);
1311 end Resolve_Aggregate;
1313 -----------------------------
1314 -- Resolve_Array_Aggregate --
1315 -----------------------------
1317 function Resolve_Array_Aggregate
1318 (N : Node_Id;
1319 Index : Node_Id;
1320 Index_Constr : Node_Id;
1321 Component_Typ : Entity_Id;
1322 Others_Allowed : Boolean) return Boolean
1324 Loc : constant Source_Ptr := Sloc (N);
1326 Failure : constant Boolean := False;
1327 Success : constant Boolean := True;
1329 Index_Typ : constant Entity_Id := Etype (Index);
1330 Index_Typ_Low : constant Node_Id := Type_Low_Bound (Index_Typ);
1331 Index_Typ_High : constant Node_Id := Type_High_Bound (Index_Typ);
1332 -- The type of the index corresponding to the array sub-aggregate along
1333 -- with its low and upper bounds.
1335 Index_Base : constant Entity_Id := Base_Type (Index_Typ);
1336 Index_Base_Low : constant Node_Id := Type_Low_Bound (Index_Base);
1337 Index_Base_High : constant Node_Id := Type_High_Bound (Index_Base);
1338 -- Ditto for the base type
1340 Others_Present : Boolean := False;
1342 Nb_Choices : Nat := 0;
1343 -- Contains the overall number of named choices in this sub-aggregate
1345 function Add (Val : Uint; To : Node_Id) return Node_Id;
1346 -- Creates a new expression node where Val is added to expression To.
1347 -- Tries to constant fold whenever possible. To must be an already
1348 -- analyzed expression.
1350 procedure Check_Bound (BH : Node_Id; AH : in out Node_Id);
1351 -- Checks that AH (the upper bound of an array aggregate) is less than
1352 -- or equal to BH (the upper bound of the index base type). If the check
1353 -- fails, a warning is emitted, the Raises_Constraint_Error flag of N is
1354 -- set, and AH is replaced with a duplicate of BH.
1356 procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id);
1357 -- Checks that range AL .. AH is compatible with range L .. H. Emits a
1358 -- warning if not and sets the Raises_Constraint_Error flag in N.
1360 procedure Check_Length (L, H : Node_Id; Len : Uint);
1361 -- Checks that range L .. H contains at least Len elements. Emits a
1362 -- warning if not and sets the Raises_Constraint_Error flag in N.
1364 function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean;
1365 -- Returns True if range L .. H is dynamic or null
1367 procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean);
1368 -- Given expression node From, this routine sets OK to False if it
1369 -- cannot statically evaluate From. Otherwise it stores this static
1370 -- value into Value.
1372 function Resolve_Aggr_Expr
1373 (Expr : Node_Id;
1374 Single_Elmt : Boolean) return Boolean;
1375 -- Resolves aggregate expression Expr. Returns False if resolution
1376 -- fails. If Single_Elmt is set to False, the expression Expr may be
1377 -- used to initialize several array aggregate elements (this can happen
1378 -- for discrete choices such as "L .. H => Expr" or the OTHERS choice).
1379 -- In this event we do not resolve Expr unless expansion is disabled.
1380 -- To know why, see the DELAYED COMPONENT RESOLUTION note above.
1382 -- NOTE: In the case of "... => <>", we pass the N_Component_Association
1383 -- node as Expr, since there is no Expression and we need a Sloc for the
1384 -- error message.
1386 procedure Resolve_Iterated_Component_Association
1387 (N : Node_Id;
1388 Index_Typ : Entity_Id);
1389 -- For AI12-061
1391 procedure Warn_On_Null_Component_Association (Expr : Node_Id);
1392 -- Expr is either a conditional expression or a case expression of an
1393 -- iterated component association initializing the aggregate N with
1394 -- components that can never be null. Report warning on associations
1395 -- that may initialize some component with a null value.
1397 ---------
1398 -- Add --
1399 ---------
1401 function Add (Val : Uint; To : Node_Id) return Node_Id is
1402 Expr_Pos : Node_Id;
1403 Expr : Node_Id;
1404 To_Pos : Node_Id;
1406 begin
1407 if Raises_Constraint_Error (To) then
1408 return To;
1409 end if;
1411 -- First test if we can do constant folding
1413 if Compile_Time_Known_Value (To)
1414 or else Nkind (To) = N_Integer_Literal
1415 then
1416 Expr_Pos := Make_Integer_Literal (Loc, Expr_Value (To) + Val);
1417 Set_Is_Static_Expression (Expr_Pos);
1418 Set_Etype (Expr_Pos, Etype (To));
1419 Set_Analyzed (Expr_Pos, Analyzed (To));
1421 if not Is_Enumeration_Type (Index_Typ) then
1422 Expr := Expr_Pos;
1424 -- If we are dealing with enumeration return
1425 -- Index_Typ'Val (Expr_Pos)
1427 else
1428 Expr :=
1429 Make_Attribute_Reference
1430 (Loc,
1431 Prefix => New_Occurrence_Of (Index_Typ, Loc),
1432 Attribute_Name => Name_Val,
1433 Expressions => New_List (Expr_Pos));
1434 end if;
1436 return Expr;
1437 end if;
1439 -- If we are here no constant folding possible
1441 if not Is_Enumeration_Type (Index_Base) then
1442 Expr :=
1443 Make_Op_Add (Loc,
1444 Left_Opnd => Duplicate_Subexpr (To),
1445 Right_Opnd => Make_Integer_Literal (Loc, Val));
1447 -- If we are dealing with enumeration return
1448 -- Index_Typ'Val (Index_Typ'Pos (To) + Val)
1450 else
1451 To_Pos :=
1452 Make_Attribute_Reference
1453 (Loc,
1454 Prefix => New_Occurrence_Of (Index_Typ, Loc),
1455 Attribute_Name => Name_Pos,
1456 Expressions => New_List (Duplicate_Subexpr (To)));
1458 Expr_Pos :=
1459 Make_Op_Add (Loc,
1460 Left_Opnd => To_Pos,
1461 Right_Opnd => Make_Integer_Literal (Loc, Val));
1463 Expr :=
1464 Make_Attribute_Reference
1465 (Loc,
1466 Prefix => New_Occurrence_Of (Index_Typ, Loc),
1467 Attribute_Name => Name_Val,
1468 Expressions => New_List (Expr_Pos));
1470 -- If the index type has a non standard representation, the
1471 -- attributes 'Val and 'Pos expand into function calls and the
1472 -- resulting expression is considered non-safe for reevaluation
1473 -- by the backend. Relocate it into a constant temporary in order
1474 -- to make it safe for reevaluation.
1476 if Has_Non_Standard_Rep (Etype (N)) then
1477 declare
1478 Def_Id : Entity_Id;
1480 begin
1481 Def_Id := Make_Temporary (Loc, 'R', Expr);
1482 Set_Etype (Def_Id, Index_Typ);
1483 Insert_Action (N,
1484 Make_Object_Declaration (Loc,
1485 Defining_Identifier => Def_Id,
1486 Object_Definition =>
1487 New_Occurrence_Of (Index_Typ, Loc),
1488 Constant_Present => True,
1489 Expression => Relocate_Node (Expr)));
1491 Expr := New_Occurrence_Of (Def_Id, Loc);
1492 end;
1493 end if;
1494 end if;
1496 return Expr;
1497 end Add;
1499 -----------------
1500 -- Check_Bound --
1501 -----------------
1503 procedure Check_Bound (BH : Node_Id; AH : in out Node_Id) is
1504 Val_BH : Uint;
1505 Val_AH : Uint;
1507 OK_BH : Boolean;
1508 OK_AH : Boolean;
1510 begin
1511 Get (Value => Val_BH, From => BH, OK => OK_BH);
1512 Get (Value => Val_AH, From => AH, OK => OK_AH);
1514 if OK_BH and then OK_AH and then Val_BH < Val_AH then
1515 Set_Raises_Constraint_Error (N);
1516 Error_Msg_Warn := SPARK_Mode /= On;
1517 Error_Msg_N ("upper bound out of range<<", AH);
1518 Error_Msg_N ("\Constraint_Error [<<", AH);
1520 -- You need to set AH to BH or else in the case of enumerations
1521 -- indexes we will not be able to resolve the aggregate bounds.
1523 AH := Duplicate_Subexpr (BH);
1524 end if;
1525 end Check_Bound;
1527 ------------------
1528 -- Check_Bounds --
1529 ------------------
1531 procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id) is
1532 Val_L : Uint;
1533 Val_H : Uint;
1534 Val_AL : Uint;
1535 Val_AH : Uint;
1537 OK_L : Boolean;
1538 OK_H : Boolean;
1540 OK_AL : Boolean;
1541 OK_AH : Boolean;
1542 pragma Warnings (Off, OK_AL);
1543 pragma Warnings (Off, OK_AH);
1545 begin
1546 if Raises_Constraint_Error (N)
1547 or else Dynamic_Or_Null_Range (AL, AH)
1548 then
1549 return;
1550 end if;
1552 Get (Value => Val_L, From => L, OK => OK_L);
1553 Get (Value => Val_H, From => H, OK => OK_H);
1555 Get (Value => Val_AL, From => AL, OK => OK_AL);
1556 Get (Value => Val_AH, From => AH, OK => OK_AH);
1558 if OK_L and then Val_L > Val_AL then
1559 Set_Raises_Constraint_Error (N);
1560 Error_Msg_Warn := SPARK_Mode /= On;
1561 Error_Msg_N ("lower bound of aggregate out of range<<", N);
1562 Error_Msg_N ("\Constraint_Error [<<", N);
1563 end if;
1565 if OK_H and then Val_H < Val_AH then
1566 Set_Raises_Constraint_Error (N);
1567 Error_Msg_Warn := SPARK_Mode /= On;
1568 Error_Msg_N ("upper bound of aggregate out of range<<", N);
1569 Error_Msg_N ("\Constraint_Error [<<", N);
1570 end if;
1571 end Check_Bounds;
1573 ------------------
1574 -- Check_Length --
1575 ------------------
1577 procedure Check_Length (L, H : Node_Id; Len : Uint) is
1578 Val_L : Uint;
1579 Val_H : Uint;
1581 OK_L : Boolean;
1582 OK_H : Boolean;
1584 Range_Len : Uint;
1586 begin
1587 if Raises_Constraint_Error (N) then
1588 return;
1589 end if;
1591 Get (Value => Val_L, From => L, OK => OK_L);
1592 Get (Value => Val_H, From => H, OK => OK_H);
1594 if not OK_L or else not OK_H then
1595 return;
1596 end if;
1598 -- If null range length is zero
1600 if Val_L > Val_H then
1601 Range_Len := Uint_0;
1602 else
1603 Range_Len := Val_H - Val_L + 1;
1604 end if;
1606 if Range_Len < Len then
1607 Set_Raises_Constraint_Error (N);
1608 Error_Msg_Warn := SPARK_Mode /= On;
1609 Error_Msg_N ("too many elements<<", N);
1610 Error_Msg_N ("\Constraint_Error [<<", N);
1611 end if;
1612 end Check_Length;
1614 ---------------------------
1615 -- Dynamic_Or_Null_Range --
1616 ---------------------------
1618 function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean is
1619 Val_L : Uint;
1620 Val_H : Uint;
1622 OK_L : Boolean;
1623 OK_H : Boolean;
1625 begin
1626 Get (Value => Val_L, From => L, OK => OK_L);
1627 Get (Value => Val_H, From => H, OK => OK_H);
1629 return not OK_L or else not OK_H
1630 or else not Is_OK_Static_Expression (L)
1631 or else not Is_OK_Static_Expression (H)
1632 or else Val_L > Val_H;
1633 end Dynamic_Or_Null_Range;
1635 ---------
1636 -- Get --
1637 ---------
1639 procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean) is
1640 begin
1641 OK := True;
1643 if Compile_Time_Known_Value (From) then
1644 Value := Expr_Value (From);
1646 -- If expression From is something like Some_Type'Val (10) then
1647 -- Value = 10.
1649 elsif Nkind (From) = N_Attribute_Reference
1650 and then Attribute_Name (From) = Name_Val
1651 and then Compile_Time_Known_Value (First (Expressions (From)))
1652 then
1653 Value := Expr_Value (First (Expressions (From)));
1654 else
1655 Value := Uint_0;
1656 OK := False;
1657 end if;
1658 end Get;
1660 -----------------------
1661 -- Resolve_Aggr_Expr --
1662 -----------------------
1664 function Resolve_Aggr_Expr
1665 (Expr : Node_Id;
1666 Single_Elmt : Boolean) return Boolean
1668 Nxt_Ind : constant Node_Id := Next_Index (Index);
1669 Nxt_Ind_Constr : constant Node_Id := Next_Index (Index_Constr);
1670 -- Index is the current index corresponding to the expression
1672 Resolution_OK : Boolean := True;
1673 -- Set to False if resolution of the expression failed
1675 begin
1676 -- Defend against previous errors
1678 if Nkind (Expr) = N_Error
1679 or else Error_Posted (Expr)
1680 then
1681 return True;
1682 end if;
1684 -- If the array type against which we are resolving the aggregate
1685 -- has several dimensions, the expressions nested inside the
1686 -- aggregate must be further aggregates (or strings).
1688 if Present (Nxt_Ind) then
1689 if Nkind (Expr) /= N_Aggregate then
1691 -- A string literal can appear where a one-dimensional array
1692 -- of characters is expected. If the literal looks like an
1693 -- operator, it is still an operator symbol, which will be
1694 -- transformed into a string when analyzed.
1696 if Is_Character_Type (Component_Typ)
1697 and then No (Next_Index (Nxt_Ind))
1698 and then Nkind (Expr) in N_String_Literal | N_Operator_Symbol
1699 then
1700 -- A string literal used in a multidimensional array
1701 -- aggregate in place of the final one-dimensional
1702 -- aggregate must not be enclosed in parentheses.
1704 if Paren_Count (Expr) /= 0 then
1705 Error_Msg_N ("no parenthesis allowed here", Expr);
1706 end if;
1708 Make_String_Into_Aggregate (Expr);
1710 else
1711 Error_Msg_N ("nested array aggregate expected", Expr);
1713 -- If the expression is parenthesized, this may be
1714 -- a missing component association for a 1-aggregate.
1716 if Paren_Count (Expr) > 0 then
1717 Error_Msg_N
1718 ("\if single-component aggregate is intended, "
1719 & "write e.g. (1 ='> ...)", Expr);
1720 end if;
1722 return Failure;
1723 end if;
1724 end if;
1726 -- If it's "... => <>", nothing to resolve
1728 if Nkind (Expr) = N_Component_Association then
1729 pragma Assert (Box_Present (Expr));
1730 return Success;
1731 end if;
1733 -- Ada 2005 (AI-231): Propagate the type to the nested aggregate.
1734 -- Required to check the null-exclusion attribute (if present).
1735 -- This value may be overridden later on.
1737 Set_Etype (Expr, Etype (N));
1739 Resolution_OK := Resolve_Array_Aggregate
1740 (Expr, Nxt_Ind, Nxt_Ind_Constr, Component_Typ, Others_Allowed);
1742 else
1743 -- If it's "... => <>", nothing to resolve
1745 if Nkind (Expr) = N_Component_Association then
1746 pragma Assert (Box_Present (Expr));
1747 return Success;
1748 end if;
1750 -- Do not resolve the expressions of discrete or others choices
1751 -- unless the expression covers a single component, or the
1752 -- expander is inactive.
1754 -- In SPARK mode, expressions that can perform side effects will
1755 -- be recognized by the gnat2why back-end, and the whole
1756 -- subprogram will be ignored. So semantic analysis can be
1757 -- performed safely.
1759 if Single_Elmt
1760 or else not Expander_Active
1761 or else In_Spec_Expression
1762 then
1763 Analyze_And_Resolve (Expr, Component_Typ);
1764 Check_Expr_OK_In_Limited_Aggregate (Expr);
1765 Check_Non_Static_Context (Expr);
1766 Aggregate_Constraint_Checks (Expr, Component_Typ);
1767 Check_Unset_Reference (Expr);
1768 end if;
1769 end if;
1771 -- If an aggregate component has a type with predicates, an explicit
1772 -- predicate check must be applied, as for an assignment statement,
1773 -- because the aggregate might not be expanded into individual
1774 -- component assignments. If the expression covers several components
1775 -- the analysis and the predicate check take place later.
1777 if Has_Predicates (Component_Typ)
1778 and then Analyzed (Expr)
1779 then
1780 Apply_Predicate_Check (Expr, Component_Typ);
1781 end if;
1783 if Raises_Constraint_Error (Expr)
1784 and then Nkind (Parent (Expr)) /= N_Component_Association
1785 then
1786 Set_Raises_Constraint_Error (N);
1787 end if;
1789 -- If the expression has been marked as requiring a range check,
1790 -- then generate it here. It's a bit odd to be generating such
1791 -- checks in the analyzer, but harmless since Generate_Range_Check
1792 -- does nothing (other than making sure Do_Range_Check is set) if
1793 -- the expander is not active.
1795 if Do_Range_Check (Expr) then
1796 Generate_Range_Check (Expr, Component_Typ, CE_Range_Check_Failed);
1797 end if;
1799 return Resolution_OK;
1800 end Resolve_Aggr_Expr;
1802 --------------------------------------------
1803 -- Resolve_Iterated_Component_Association --
1804 --------------------------------------------
1806 procedure Resolve_Iterated_Component_Association
1807 (N : Node_Id;
1808 Index_Typ : Entity_Id)
1810 Loc : constant Source_Ptr := Sloc (N);
1811 Id : constant Entity_Id := Defining_Identifier (N);
1813 -----------------------
1814 -- Remove_References --
1815 -----------------------
1817 function Remove_Reference (N : Node_Id) return Traverse_Result;
1818 -- Remove reference to the entity Id after analysis, so it can be
1819 -- properly reanalyzed after construct is expanded into a loop.
1821 function Remove_Reference (N : Node_Id) return Traverse_Result is
1822 begin
1823 if Nkind (N) = N_Identifier
1824 and then Present (Entity (N))
1825 and then Entity (N) = Id
1826 then
1827 Set_Entity (N, Empty);
1828 Set_Etype (N, Empty);
1829 end if;
1830 Set_Analyzed (N, False);
1831 return OK;
1832 end Remove_Reference;
1834 procedure Remove_References is new Traverse_Proc (Remove_Reference);
1836 -- Local variables
1838 Choice : Node_Id;
1839 Dummy : Boolean;
1840 Scop : Entity_Id;
1841 Expr : constant Node_Id := Expression (N);
1843 -- Start of processing for Resolve_Iterated_Component_Association
1845 begin
1846 Error_Msg_Ada_2022_Feature ("iterated component", Loc);
1848 -- Create a scope in which to introduce an index, to make it visible
1849 -- for the analysis of component expression.
1851 Scop := New_Internal_Entity (E_Loop, Current_Scope, Loc, 'L');
1852 Set_Etype (Scop, Standard_Void_Type);
1853 Set_Parent (Scop, Parent (N));
1854 Push_Scope (Scop);
1856 -- If there is iterator specification, then its preanalysis will make
1857 -- the index visible.
1859 if Present (Iterator_Specification (N)) then
1860 Preanalyze (Iterator_Specification (N));
1862 -- Otherwise, analyze discrete choices and make the index visible
1864 else
1865 -- Insert index name into current scope but don't decorate it yet,
1866 -- so that a premature usage of this name in discrete choices will
1867 -- be nicely diagnosed.
1869 Enter_Name (Id);
1871 Choice := First (Discrete_Choices (N));
1873 while Present (Choice) loop
1874 if Nkind (Choice) = N_Others_Choice then
1875 Others_Present := True;
1877 else
1878 Analyze (Choice);
1880 -- Choice can be a subtype name, a range, or an expression
1882 if Is_Entity_Name (Choice)
1883 and then Is_Type (Entity (Choice))
1884 and then
1885 Base_Type (Entity (Choice)) = Base_Type (Index_Typ)
1886 then
1887 null;
1889 else
1890 Analyze_And_Resolve (Choice, Index_Typ);
1891 end if;
1892 end if;
1894 Next (Choice);
1895 end loop;
1897 -- Decorate the index variable
1899 Set_Etype (Id, Index_Typ);
1900 Mutate_Ekind (Id, E_Variable);
1901 Set_Is_Not_Self_Hidden (Id);
1902 Set_Scope (Id, Scop);
1903 end if;
1905 -- Analyze expression without expansion, to verify legality.
1906 -- When generating code, we then remove references to the index
1907 -- variable, because the expression will be analyzed anew after
1908 -- rewritting as a loop with a new index variable; when not
1909 -- generating code we leave the analyzed expression as it is.
1911 Dummy := Resolve_Aggr_Expr (Expr, Single_Elmt => False);
1913 if Operating_Mode /= Check_Semantics then
1914 Remove_References (Expr);
1915 end if;
1917 -- An iterated_component_association may appear in a nested
1918 -- aggregate for a multidimensional structure: preserve the bounds
1919 -- computed for the expression, as well as the anonymous array
1920 -- type generated for it; both are needed during array expansion.
1922 if Nkind (Expr) = N_Aggregate then
1923 Set_Aggregate_Bounds (Expression (N), Aggregate_Bounds (Expr));
1924 Set_Etype (Expression (N), Etype (Expr));
1925 end if;
1927 End_Scope;
1928 end Resolve_Iterated_Component_Association;
1930 ----------------------------------------
1931 -- Warn_On_Null_Component_Association --
1932 ----------------------------------------
1934 procedure Warn_On_Null_Component_Association (Expr : Node_Id) is
1935 Comp_Typ : constant Entity_Id := Component_Type (Etype (N));
1937 procedure Check_Case_Expr (N : Node_Id);
1938 -- Check if a case expression may initialize some component with a
1939 -- null value.
1941 procedure Check_Cond_Expr (N : Node_Id);
1942 -- Check if a conditional expression may initialize some component
1943 -- with a null value.
1945 procedure Check_Expr (Expr : Node_Id);
1946 -- Check if an expression may initialize some component with a
1947 -- null value.
1949 procedure Warn_On_Null_Expression_And_Rewrite (Null_Expr : Node_Id);
1950 -- Report warning on known null expression and replace the expression
1951 -- by a raise constraint error node.
1953 ---------------------
1954 -- Check_Case_Expr --
1955 ---------------------
1957 procedure Check_Case_Expr (N : Node_Id) is
1958 Alt_Node : Node_Id := First (Alternatives (N));
1960 begin
1961 while Present (Alt_Node) loop
1962 Check_Expr (Expression (Alt_Node));
1963 Next (Alt_Node);
1964 end loop;
1965 end Check_Case_Expr;
1967 ---------------------
1968 -- Check_Cond_Expr --
1969 ---------------------
1971 procedure Check_Cond_Expr (N : Node_Id) is
1972 If_Expr : Node_Id := N;
1973 Then_Expr : Node_Id;
1974 Else_Expr : Node_Id;
1976 begin
1977 Then_Expr := Next (First (Expressions (If_Expr)));
1978 Else_Expr := Next (Then_Expr);
1980 Check_Expr (Then_Expr);
1982 -- Process elsif parts (if any)
1984 while Nkind (Else_Expr) = N_If_Expression loop
1985 If_Expr := Else_Expr;
1986 Then_Expr := Next (First (Expressions (If_Expr)));
1987 Else_Expr := Next (Then_Expr);
1989 Check_Expr (Then_Expr);
1990 end loop;
1992 if Known_Null (Else_Expr) then
1993 Warn_On_Null_Expression_And_Rewrite (Else_Expr);
1994 end if;
1995 end Check_Cond_Expr;
1997 ----------------
1998 -- Check_Expr --
1999 ----------------
2001 procedure Check_Expr (Expr : Node_Id) is
2002 begin
2003 if Known_Null (Expr) then
2004 Warn_On_Null_Expression_And_Rewrite (Expr);
2006 elsif Nkind (Expr) = N_If_Expression then
2007 Check_Cond_Expr (Expr);
2009 elsif Nkind (Expr) = N_Case_Expression then
2010 Check_Case_Expr (Expr);
2011 end if;
2012 end Check_Expr;
2014 -----------------------------------------
2015 -- Warn_On_Null_Expression_And_Rewrite --
2016 -----------------------------------------
2018 procedure Warn_On_Null_Expression_And_Rewrite (Null_Expr : Node_Id) is
2019 begin
2020 Error_Msg_N
2021 ("(Ada 2005) NULL not allowed in null-excluding component??",
2022 Null_Expr);
2023 Error_Msg_N
2024 ("\Constraint_Error might be raised at run time??", Null_Expr);
2026 -- We cannot use Apply_Compile_Time_Constraint_Error because in
2027 -- some cases the components are rewritten and the runtime error
2028 -- would be missed.
2030 Rewrite (Null_Expr,
2031 Make_Raise_Constraint_Error (Sloc (Null_Expr),
2032 Reason => CE_Access_Check_Failed));
2034 Set_Etype (Null_Expr, Comp_Typ);
2035 Set_Analyzed (Null_Expr);
2036 end Warn_On_Null_Expression_And_Rewrite;
2038 -- Start of processing for Warn_On_Null_Component_Association
2040 begin
2041 pragma Assert (Can_Never_Be_Null (Comp_Typ));
2043 case Nkind (Expr) is
2044 when N_If_Expression =>
2045 Check_Cond_Expr (Expr);
2047 when N_Case_Expression =>
2048 Check_Case_Expr (Expr);
2050 when others =>
2051 pragma Assert (False);
2052 null;
2053 end case;
2054 end Warn_On_Null_Component_Association;
2056 -- Local variables
2058 Assoc : Node_Id;
2059 Choice : Node_Id;
2060 Expr : Node_Id;
2061 Discard : Node_Id;
2063 Aggr_Low : Node_Id := Empty;
2064 Aggr_High : Node_Id := Empty;
2065 -- The actual low and high bounds of this sub-aggregate
2067 Case_Table_Size : Nat;
2068 -- Contains the size of the case table needed to sort aggregate choices
2070 Choices_Low : Node_Id := Empty;
2071 Choices_High : Node_Id := Empty;
2072 -- The lowest and highest discrete choices values for a named aggregate
2074 Delete_Choice : Boolean;
2075 -- Used when replacing a subtype choice with predicate by a list
2077 Has_Iterator_Specifications : Boolean := False;
2078 -- Flag to indicate that all named associations are iterated component
2079 -- associations with iterator specifications, in which case the
2080 -- expansion will create two loops: one to evaluate the size and one
2081 -- to generate the elements (4.3.3 (20.2/5)).
2083 Nb_Elements : Uint := Uint_0;
2084 -- The number of elements in a positional aggregate
2086 Nb_Discrete_Choices : Nat := 0;
2087 -- The overall number of discrete choices (not counting others choice)
2089 -- Start of processing for Resolve_Array_Aggregate
2091 begin
2092 -- Ignore junk empty aggregate resulting from parser error
2094 if No (Expressions (N))
2095 and then No (Component_Associations (N))
2096 and then not Null_Record_Present (N)
2097 then
2098 return Failure;
2099 end if;
2101 -- Disable the warning for GNAT Mode to allow for easier transition.
2103 if Ada_Version_Explicit >= Ada_2022
2104 and then Warn_On_Obsolescent_Feature
2105 and then not GNAT_Mode
2106 and then not Is_Homogeneous_Aggregate (N)
2107 and then not Is_Enum_Array_Aggregate (N)
2108 and then Is_Parenthesis_Aggregate (N)
2109 and then Nkind (Parent (N)) /= N_Qualified_Expression
2110 and then Comes_From_Source (N)
2111 then
2112 Error_Msg_N
2113 ("?j?array aggregate using () is an" &
2114 " obsolescent syntax, use '['] instead", N);
2115 end if;
2117 -- STEP 1: make sure the aggregate is correctly formatted
2119 if Is_Null_Aggregate (N) then
2120 null;
2122 elsif Present (Component_Associations (N)) then
2124 -- Verify that all or none of the component associations
2125 -- include an iterator specification.
2127 Assoc := First (Component_Associations (N));
2128 if Nkind (Assoc) = N_Iterated_Component_Association
2129 and then Present (Iterator_Specification (Assoc))
2130 then
2131 -- All other component associations must have an iterator spec.
2133 Next (Assoc);
2134 while Present (Assoc) loop
2135 if Nkind (Assoc) /= N_Iterated_Component_Association
2136 or else No (Iterator_Specification (Assoc))
2137 then
2138 Error_Msg_N ("mixed iterated component association"
2139 & " (RM 4.3.3 (17.1/5))",
2140 Assoc);
2141 return Failure;
2142 end if;
2144 Next (Assoc);
2145 end loop;
2147 Has_Iterator_Specifications := True;
2149 else
2150 -- or none of them do.
2152 Next (Assoc);
2153 while Present (Assoc) loop
2154 if Nkind (Assoc) = N_Iterated_Component_Association
2155 and then Present (Iterator_Specification (Assoc))
2156 then
2157 Error_Msg_N ("mixed iterated component association"
2158 & " (RM 4.3.3 (17.1/5))",
2159 Assoc);
2160 return Failure;
2161 end if;
2163 Next (Assoc);
2164 end loop;
2166 end if;
2168 Assoc := First (Component_Associations (N));
2169 while Present (Assoc) loop
2170 if Nkind (Assoc) = N_Iterated_Component_Association then
2171 Resolve_Iterated_Component_Association (Assoc, Index_Typ);
2173 elsif Nkind (Assoc) /= N_Component_Association then
2174 Error_Msg_N
2175 ("invalid component association for aggregate", Assoc);
2176 return Failure;
2177 end if;
2179 Choice := First (Choice_List (Assoc));
2180 Delete_Choice := False;
2181 while Present (Choice) loop
2182 if Nkind (Choice) = N_Others_Choice then
2183 Others_Present := True;
2185 if Choice /= First (Choice_List (Assoc))
2186 or else Present (Next (Choice))
2187 then
2188 Error_Msg_N
2189 ("OTHERS must appear alone in a choice list", Choice);
2190 return Failure;
2191 end if;
2193 if Present (Next (Assoc)) then
2194 Error_Msg_N
2195 ("OTHERS must appear last in an aggregate", Choice);
2196 return Failure;
2197 end if;
2199 if Ada_Version = Ada_83
2200 and then Assoc /= First (Component_Associations (N))
2201 and then Nkind (Parent (N)) in
2202 N_Assignment_Statement | N_Object_Declaration
2203 then
2204 Error_Msg_N
2205 ("(Ada 83) illegal context for OTHERS choice", N);
2206 end if;
2208 elsif Is_Entity_Name (Choice) then
2209 Analyze (Choice);
2211 declare
2212 E : constant Entity_Id := Entity (Choice);
2213 New_Cs : List_Id;
2214 P : Node_Id;
2215 C : Node_Id;
2217 begin
2218 if Is_Type (E) and then Has_Predicates (E) then
2219 Freeze_Before (N, E);
2221 if Has_Dynamic_Predicate_Aspect (E)
2222 or else Has_Ghost_Predicate_Aspect (E)
2223 then
2224 Error_Msg_NE
2225 ("subtype& has non-static predicate, not allowed "
2226 & "in aggregate choice", Choice, E);
2228 elsif not Is_OK_Static_Subtype (E) then
2229 Error_Msg_NE
2230 ("non-static subtype& has predicate, not allowed "
2231 & "in aggregate choice", Choice, E);
2232 end if;
2234 -- If the subtype has a static predicate, replace the
2235 -- original choice with the list of individual values
2236 -- covered by the predicate.
2237 -- This should be deferred to expansion time ???
2239 if Present (Static_Discrete_Predicate (E)) then
2240 Delete_Choice := True;
2242 New_Cs := New_List;
2243 P := First (Static_Discrete_Predicate (E));
2244 while Present (P) loop
2245 C := New_Copy (P);
2246 Set_Sloc (C, Sloc (Choice));
2247 Append_To (New_Cs, C);
2248 Next (P);
2249 end loop;
2251 Insert_List_After (Choice, New_Cs);
2252 end if;
2253 end if;
2254 end;
2255 end if;
2257 Nb_Choices := Nb_Choices + 1;
2259 declare
2260 C : constant Node_Id := Choice;
2262 begin
2263 Next (Choice);
2265 if Delete_Choice then
2266 Remove (C);
2267 Nb_Choices := Nb_Choices - 1;
2268 Delete_Choice := False;
2269 end if;
2270 end;
2271 end loop;
2273 Next (Assoc);
2274 end loop;
2275 end if;
2277 -- At this point we know that the others choice, if present, is by
2278 -- itself and appears last in the aggregate. Check if we have mixed
2279 -- positional and discrete associations (other than the others choice).
2281 if Present (Expressions (N))
2282 and then (Nb_Choices > 1
2283 or else (Nb_Choices = 1 and then not Others_Present))
2284 then
2285 Error_Msg_N
2286 ("cannot mix named and positional associations in array aggregate",
2287 First (Choice_List (First (Component_Associations (N)))));
2288 return Failure;
2289 end if;
2291 -- Test for the validity of an others choice if present
2293 if Others_Present and then not Others_Allowed then
2294 declare
2295 Others_N : constant Node_Id :=
2296 First (Choice_List (First (Component_Associations (N))));
2297 begin
2298 Error_Msg_N ("OTHERS choice not allowed here", Others_N);
2299 Error_Msg_N ("\qualify the aggregate with a constrained subtype "
2300 & "to provide bounds for it", Others_N);
2301 return Failure;
2302 end;
2303 end if;
2305 -- Protect against cascaded errors
2307 if Etype (Index_Typ) = Any_Type then
2308 return Failure;
2309 end if;
2311 -- STEP 2: Process named components
2313 if No (Expressions (N)) then
2314 if Others_Present then
2315 Case_Table_Size := Nb_Choices - 1;
2316 else
2317 Case_Table_Size := Nb_Choices;
2318 end if;
2320 Step_2 : declare
2321 function Empty_Range (A : Node_Id) return Boolean;
2322 -- If an association covers an empty range, some warnings on the
2323 -- expression of the association can be disabled.
2325 -----------------
2326 -- Empty_Range --
2327 -----------------
2329 function Empty_Range (A : Node_Id) return Boolean is
2330 R : Node_Id;
2332 begin
2333 if Nkind (A) = N_Iterated_Component_Association then
2334 R := First (Discrete_Choices (A));
2335 else
2336 R := First (Choices (A));
2337 end if;
2339 return No (Next (R))
2340 and then Nkind (R) = N_Range
2341 and then Compile_Time_Compare
2342 (Low_Bound (R), High_Bound (R), False) = GT;
2343 end Empty_Range;
2345 -- Local variables
2347 Low : Node_Id;
2348 High : Node_Id;
2349 -- Denote the lowest and highest values in an aggregate choice
2351 S_Low : Node_Id := Empty;
2352 S_High : Node_Id := Empty;
2353 -- if a choice in an aggregate is a subtype indication these
2354 -- denote the lowest and highest values of the subtype
2356 Table : Case_Table_Type (1 .. Case_Table_Size);
2357 -- Used to sort all the different choice values
2359 Single_Choice : Boolean;
2360 -- Set to true every time there is a single discrete choice in a
2361 -- discrete association
2363 Prev_Nb_Discrete_Choices : Nat;
2364 -- Used to keep track of the number of discrete choices in the
2365 -- current association.
2367 Errors_Posted_On_Choices : Boolean := False;
2368 -- Keeps track of whether any choices have semantic errors
2370 -- Start of processing for Step_2
2372 begin
2373 -- STEP 2 (A): Check discrete choices validity
2374 -- No need if this is an element iteration.
2376 Assoc := First (Component_Associations (N));
2377 while Present (Assoc)
2378 and then Present (Choice_List (Assoc))
2379 loop
2380 Prev_Nb_Discrete_Choices := Nb_Discrete_Choices;
2381 Choice := First (Choice_List (Assoc));
2383 loop
2384 Analyze (Choice);
2386 if Nkind (Choice) = N_Others_Choice then
2387 Single_Choice := False;
2388 exit;
2390 -- Test for subtype mark without constraint
2392 elsif Is_Entity_Name (Choice) and then
2393 Is_Type (Entity (Choice))
2394 then
2395 if Base_Type (Entity (Choice)) /= Index_Base then
2396 Error_Msg_N
2397 ("invalid subtype mark in aggregate choice",
2398 Choice);
2399 return Failure;
2400 end if;
2402 -- Case of subtype indication
2404 elsif Nkind (Choice) = N_Subtype_Indication then
2405 Resolve_Discrete_Subtype_Indication (Choice, Index_Base);
2407 if Has_Dynamic_Predicate_Aspect
2408 (Entity (Subtype_Mark (Choice)))
2409 or else Has_Ghost_Predicate_Aspect
2410 (Entity (Subtype_Mark (Choice)))
2411 then
2412 Error_Msg_NE
2413 ("subtype& has non-static predicate, "
2414 & "not allowed in aggregate choice",
2415 Choice, Entity (Subtype_Mark (Choice)));
2416 end if;
2418 -- Does the subtype indication evaluation raise CE?
2420 Get_Index_Bounds (Subtype_Mark (Choice), S_Low, S_High);
2421 Get_Index_Bounds (Choice, Low, High);
2422 Check_Bounds (S_Low, S_High, Low, High);
2424 -- Case of range or expression
2426 else
2427 Resolve (Choice, Index_Base);
2428 Check_Unset_Reference (Choice);
2429 Check_Non_Static_Context (Choice);
2431 -- If semantic errors were posted on the choice, then
2432 -- record that for possible early return from later
2433 -- processing (see handling of enumeration choices).
2435 if Error_Posted (Choice) then
2436 Errors_Posted_On_Choices := True;
2437 end if;
2439 -- Do not range check a choice. This check is redundant
2440 -- since this test is already done when we check that the
2441 -- bounds of the array aggregate are within range.
2443 Set_Do_Range_Check (Choice, False);
2444 end if;
2446 -- If we could not resolve the discrete choice stop here
2448 if Etype (Choice) = Any_Type then
2449 return Failure;
2451 -- If the discrete choice raises CE get its original bounds
2453 elsif Nkind (Choice) = N_Raise_Constraint_Error then
2454 Set_Raises_Constraint_Error (N);
2455 Get_Index_Bounds (Original_Node (Choice), Low, High);
2457 -- Otherwise get its bounds as usual
2459 else
2460 Get_Index_Bounds (Choice, Low, High);
2461 end if;
2463 if (Dynamic_Or_Null_Range (Low, High)
2464 or else (Nkind (Choice) = N_Subtype_Indication
2465 and then
2466 Dynamic_Or_Null_Range (S_Low, S_High)))
2467 and then Nb_Choices /= 1
2468 then
2469 Error_Msg_N
2470 ("dynamic or empty choice in aggregate "
2471 & "must be the only choice", Choice);
2472 return Failure;
2473 end if;
2475 if not (All_Composite_Constraints_Static (Low)
2476 and then All_Composite_Constraints_Static (High)
2477 and then All_Composite_Constraints_Static (S_Low)
2478 and then All_Composite_Constraints_Static (S_High))
2479 then
2480 Check_Restriction (No_Dynamic_Sized_Objects, Choice);
2481 end if;
2483 Nb_Discrete_Choices := Nb_Discrete_Choices + 1;
2484 Table (Nb_Discrete_Choices).Lo := Low;
2485 Table (Nb_Discrete_Choices).Hi := High;
2486 Table (Nb_Discrete_Choices).Choice := Choice;
2488 Next (Choice);
2490 if No (Choice) then
2492 -- Check if we have a single discrete choice and whether
2493 -- this discrete choice specifies a single value.
2495 Single_Choice :=
2496 Nb_Discrete_Choices = Prev_Nb_Discrete_Choices + 1
2497 and then Low = High;
2499 exit;
2500 end if;
2501 end loop;
2503 -- Ada 2005 (AI-231)
2505 if Ada_Version >= Ada_2005
2506 and then not Empty_Range (Assoc)
2507 then
2508 if Known_Null (Expression (Assoc)) then
2509 Check_Can_Never_Be_Null (Etype (N), Expression (Assoc));
2511 -- Report warning on iterated component association that may
2512 -- initialize some component of an array of null-excluding
2513 -- access type components with a null value. For example:
2515 -- type AList is array (...) of not null access Integer;
2516 -- L : AList :=
2517 -- [for J in A'Range =>
2518 -- (if Func (J) = 0 then A(J)'Access else Null)];
2520 elsif Ada_Version >= Ada_2022
2521 and then Can_Never_Be_Null (Component_Type (Etype (N)))
2522 and then Nkind (Assoc) = N_Iterated_Component_Association
2523 and then Nkind (Expression (Assoc)) in N_If_Expression
2524 | N_Case_Expression
2525 then
2526 Warn_On_Null_Component_Association (Expression (Assoc));
2527 end if;
2528 end if;
2530 -- Ada 2005 (AI-287): In case of default initialized component
2531 -- we delay the resolution to the expansion phase.
2533 if Box_Present (Assoc) then
2535 -- Ada 2005 (AI-287): In case of default initialization of a
2536 -- component the expander will generate calls to the
2537 -- corresponding initialization subprogram. We need to call
2538 -- Resolve_Aggr_Expr to check the rules about
2539 -- dimensionality.
2541 if not Resolve_Aggr_Expr
2542 (Assoc, Single_Elmt => Single_Choice)
2543 then
2544 return Failure;
2545 end if;
2547 -- ??? Checks for dynamically tagged expressions below will
2548 -- be only applied to iterated_component_association after
2549 -- expansion; in particular, errors might not be reported when
2550 -- -gnatc switch is used.
2552 elsif Nkind (Assoc) = N_Iterated_Component_Association then
2553 null; -- handled above, in a loop context
2555 elsif not Resolve_Aggr_Expr
2556 (Expression (Assoc), Single_Elmt => Single_Choice)
2557 then
2558 return Failure;
2560 -- Check incorrect use of dynamically tagged expression
2562 -- We differentiate here two cases because the expression may
2563 -- not be decorated. For example, the analysis and resolution
2564 -- of the expression associated with the others choice will be
2565 -- done later with the full aggregate. In such case we
2566 -- duplicate the expression tree to analyze the copy and
2567 -- perform the required check.
2569 elsif No (Etype (Expression (Assoc))) then
2570 declare
2571 Save_Analysis : constant Boolean := Full_Analysis;
2572 Expr : constant Node_Id :=
2573 New_Copy_Tree (Expression (Assoc));
2575 begin
2576 Expander_Mode_Save_And_Set (False);
2577 Full_Analysis := False;
2579 -- Analyze the expression, making sure it is properly
2580 -- attached to the tree before we do the analysis.
2582 Set_Parent (Expr, Parent (Expression (Assoc)));
2583 Analyze (Expr);
2585 -- Compute its dimensions now, rather than at the end of
2586 -- resolution, because in the case of multidimensional
2587 -- aggregates subsequent expansion may lead to spurious
2588 -- errors.
2590 Check_Expression_Dimensions (Expr, Component_Typ);
2592 -- If the expression is a literal, propagate this info
2593 -- to the expression in the association, to enable some
2594 -- optimizations downstream.
2596 if Is_Entity_Name (Expr)
2597 and then Present (Entity (Expr))
2598 and then Ekind (Entity (Expr)) = E_Enumeration_Literal
2599 then
2600 Analyze_And_Resolve
2601 (Expression (Assoc), Component_Typ);
2602 end if;
2604 Full_Analysis := Save_Analysis;
2605 Expander_Mode_Restore;
2607 if Is_Tagged_Type (Etype (Expr)) then
2608 Check_Dynamically_Tagged_Expression
2609 (Expr => Expr,
2610 Typ => Component_Type (Etype (N)),
2611 Related_Nod => N);
2612 end if;
2613 end;
2615 elsif Is_Tagged_Type (Etype (Expression (Assoc))) then
2616 Check_Dynamically_Tagged_Expression
2617 (Expr => Expression (Assoc),
2618 Typ => Component_Type (Etype (N)),
2619 Related_Nod => N);
2620 end if;
2622 Next (Assoc);
2623 end loop;
2625 -- If aggregate contains more than one choice then these must be
2626 -- static. Check for duplicate and missing values.
2628 -- Note: there is duplicated code here wrt Check_Choice_Set in
2629 -- the body of Sem_Case, and it is possible we could just reuse
2630 -- that procedure. To be checked ???
2632 if Nb_Discrete_Choices > 1 then
2633 Check_Choices : declare
2634 Choice : Node_Id;
2635 -- Location of choice for messages
2637 Hi_Val : Uint;
2638 Lo_Val : Uint;
2639 -- High end of one range and Low end of the next. Should be
2640 -- contiguous if there is no hole in the list of values.
2642 Lo_Dup : Uint;
2643 Hi_Dup : Uint;
2644 -- End points of duplicated range
2646 Missing_Or_Duplicates : Boolean := False;
2647 -- Set True if missing or duplicate choices found
2649 procedure Output_Bad_Choices (Lo, Hi : Uint; C : Node_Id);
2650 -- Output continuation message with a representation of the
2651 -- bounds (just Lo if Lo = Hi, else Lo .. Hi). C is the
2652 -- choice node where the message is to be posted.
2654 ------------------------
2655 -- Output_Bad_Choices --
2656 ------------------------
2658 procedure Output_Bad_Choices (Lo, Hi : Uint; C : Node_Id) is
2659 begin
2660 -- Enumeration type case
2662 if Is_Enumeration_Type (Index_Typ) then
2663 Error_Msg_Name_1 :=
2664 Chars (Get_Enum_Lit_From_Pos (Index_Typ, Lo, Loc));
2665 Error_Msg_Name_2 :=
2666 Chars (Get_Enum_Lit_From_Pos (Index_Typ, Hi, Loc));
2668 if Lo = Hi then
2669 Error_Msg_N ("\\ %!", C);
2670 else
2671 Error_Msg_N ("\\ % .. %!", C);
2672 end if;
2674 -- Integer types case
2676 else
2677 Error_Msg_Uint_1 := Lo;
2678 Error_Msg_Uint_2 := Hi;
2680 if Lo = Hi then
2681 Error_Msg_N ("\\ ^!", C);
2682 else
2683 Error_Msg_N ("\\ ^ .. ^!", C);
2684 end if;
2685 end if;
2686 end Output_Bad_Choices;
2688 -- Start of processing for Check_Choices
2690 begin
2691 Sort_Case_Table (Table);
2693 -- First we do a quick linear loop to find out if we have
2694 -- any duplicates or missing entries (usually we have a
2695 -- legal aggregate, so this will get us out quickly).
2697 for J in 1 .. Nb_Discrete_Choices - 1 loop
2698 Hi_Val := Expr_Value (Table (J).Hi);
2699 Lo_Val := Expr_Value (Table (J + 1).Lo);
2701 if Lo_Val <= Hi_Val
2702 or else (Lo_Val > Hi_Val + 1
2703 and then not Others_Present)
2704 then
2705 Missing_Or_Duplicates := True;
2706 exit;
2707 end if;
2708 end loop;
2710 -- If we have missing or duplicate entries, first fill in
2711 -- the Highest entries to make life easier in the following
2712 -- loops to detect bad entries.
2714 if Missing_Or_Duplicates then
2715 Table (1).Highest := Expr_Value (Table (1).Hi);
2717 for J in 2 .. Nb_Discrete_Choices loop
2718 Table (J).Highest :=
2719 UI_Max
2720 (Table (J - 1).Highest, Expr_Value (Table (J).Hi));
2721 end loop;
2723 -- Loop through table entries to find duplicate indexes
2725 for J in 2 .. Nb_Discrete_Choices loop
2726 Lo_Val := Expr_Value (Table (J).Lo);
2727 Hi_Val := Expr_Value (Table (J).Hi);
2729 -- Case where we have duplicates (the lower bound of
2730 -- this choice is less than or equal to the highest
2731 -- high bound found so far).
2733 if Lo_Val <= Table (J - 1).Highest then
2735 -- We move backwards looking for duplicates. We can
2736 -- abandon this loop as soon as we reach a choice
2737 -- highest value that is less than Lo_Val.
2739 for K in reverse 1 .. J - 1 loop
2740 exit when Table (K).Highest < Lo_Val;
2742 -- Here we may have duplicates between entries
2743 -- for K and J. Get range of duplicates.
2745 Lo_Dup :=
2746 UI_Max (Lo_Val, Expr_Value (Table (K).Lo));
2747 Hi_Dup :=
2748 UI_Min (Hi_Val, Expr_Value (Table (K).Hi));
2750 -- Nothing to do if duplicate range is null
2752 if Lo_Dup > Hi_Dup then
2753 null;
2755 -- Otherwise place proper message
2757 else
2758 -- We place message on later choice, with a
2759 -- line reference to the earlier choice.
2761 if Sloc (Table (J).Choice) <
2762 Sloc (Table (K).Choice)
2763 then
2764 Choice := Table (K).Choice;
2765 Error_Msg_Sloc := Sloc (Table (J).Choice);
2766 else
2767 Choice := Table (J).Choice;
2768 Error_Msg_Sloc := Sloc (Table (K).Choice);
2769 end if;
2771 if Lo_Dup = Hi_Dup then
2772 Error_Msg_N
2773 ("index value in array aggregate "
2774 & "duplicates the one given#!", Choice);
2775 else
2776 Error_Msg_N
2777 ("index values in array aggregate "
2778 & "duplicate those given#!", Choice);
2779 end if;
2781 Output_Bad_Choices (Lo_Dup, Hi_Dup, Choice);
2782 end if;
2783 end loop;
2784 end if;
2785 end loop;
2787 -- Loop through entries in table to find missing indexes.
2788 -- Not needed if others, since missing impossible.
2790 if not Others_Present then
2791 for J in 2 .. Nb_Discrete_Choices loop
2792 Lo_Val := Expr_Value (Table (J).Lo);
2793 Hi_Val := Table (J - 1).Highest;
2795 if Lo_Val > Hi_Val + 1 then
2797 declare
2798 Error_Node : Node_Id;
2800 begin
2801 -- If the choice is the bound of a range in
2802 -- a subtype indication, it is not in the
2803 -- source lists for the aggregate itself, so
2804 -- post the error on the aggregate. Otherwise
2805 -- post it on choice itself.
2807 Choice := Table (J).Choice;
2809 if Is_List_Member (Choice) then
2810 Error_Node := Choice;
2811 else
2812 Error_Node := N;
2813 end if;
2815 if Hi_Val + 1 = Lo_Val - 1 then
2816 Error_Msg_N
2817 ("missing index value "
2818 & "in array aggregate!", Error_Node);
2819 else
2820 Error_Msg_N
2821 ("missing index values "
2822 & "in array aggregate!", Error_Node);
2823 end if;
2825 Output_Bad_Choices
2826 (Hi_Val + 1, Lo_Val - 1, Error_Node);
2827 end;
2828 end if;
2829 end loop;
2830 end if;
2832 -- If either missing or duplicate values, return failure
2834 Set_Etype (N, Any_Composite);
2835 return Failure;
2836 end if;
2837 end Check_Choices;
2838 end if;
2840 if Has_Iterator_Specifications then
2841 -- Bounds will be determined dynamically.
2843 return Success;
2844 end if;
2846 -- STEP 2 (B): Compute aggregate bounds and min/max choices values
2848 if Nb_Discrete_Choices > 0 then
2849 Choices_Low := Table (1).Lo;
2850 Choices_High := Table (Nb_Discrete_Choices).Hi;
2851 end if;
2853 -- If Others is present, then bounds of aggregate come from the
2854 -- index constraint (not the choices in the aggregate itself).
2856 if Others_Present then
2857 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2859 -- Abandon processing if either bound is already signalled as
2860 -- an error (prevents junk cascaded messages and blow ups).
2862 if Nkind (Aggr_Low) = N_Error
2863 or else
2864 Nkind (Aggr_High) = N_Error
2865 then
2866 return False;
2867 end if;
2869 -- No others clause present
2871 else
2872 -- Special processing if others allowed and not present. This
2873 -- means that the bounds of the aggregate come from the index
2874 -- constraint (and the length must match).
2876 if Others_Allowed then
2877 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2879 -- Abandon processing if either bound is already signalled
2880 -- as an error (stop junk cascaded messages and blow ups).
2882 if Nkind (Aggr_Low) = N_Error
2883 or else
2884 Nkind (Aggr_High) = N_Error
2885 then
2886 return False;
2887 end if;
2889 -- If others allowed, and no others present, then the array
2890 -- should cover all index values. If it does not, we will
2891 -- get a length check warning, but there is two cases where
2892 -- an additional warning is useful:
2894 -- If we have no positional components, and the length is
2895 -- wrong (which we can tell by others being allowed with
2896 -- missing components), and the index type is an enumeration
2897 -- type, then issue appropriate warnings about these missing
2898 -- components. They are only warnings, since the aggregate
2899 -- is fine, it's just the wrong length. We skip this check
2900 -- for standard character types (since there are no literals
2901 -- and it is too much trouble to concoct them), and also if
2902 -- any of the bounds have values that are not known at
2903 -- compile time.
2905 -- Another case warranting a warning is when the length
2906 -- is right, but as above we have an index type that is
2907 -- an enumeration, and the bounds do not match. This is a
2908 -- case where dubious sliding is allowed and we generate a
2909 -- warning that the bounds do not match.
2911 if No (Expressions (N))
2912 and then Nkind (Index) = N_Range
2913 and then Is_Enumeration_Type (Etype (Index))
2914 and then not Is_Standard_Character_Type (Etype (Index))
2915 and then Compile_Time_Known_Value (Aggr_Low)
2916 and then Compile_Time_Known_Value (Aggr_High)
2917 and then Compile_Time_Known_Value (Choices_Low)
2918 and then Compile_Time_Known_Value (Choices_High)
2919 then
2920 -- If any of the expressions or range bounds in choices
2921 -- have semantic errors, then do not attempt further
2922 -- resolution, to prevent cascaded errors.
2924 if Errors_Posted_On_Choices then
2925 return Failure;
2926 end if;
2928 declare
2929 ALo : constant Node_Id := Expr_Value_E (Aggr_Low);
2930 AHi : constant Node_Id := Expr_Value_E (Aggr_High);
2931 CLo : constant Node_Id := Expr_Value_E (Choices_Low);
2932 CHi : constant Node_Id := Expr_Value_E (Choices_High);
2934 Ent : Entity_Id;
2936 begin
2937 -- Warning case 1, missing values at start/end. Only
2938 -- do the check if the number of entries is too small.
2940 if (Enumeration_Pos (CHi) - Enumeration_Pos (CLo))
2942 (Enumeration_Pos (AHi) - Enumeration_Pos (ALo))
2943 then
2944 Error_Msg_N
2945 ("missing index value(s) in array aggregate??",
2948 -- Output missing value(s) at start
2950 if Chars (ALo) /= Chars (CLo) then
2951 Ent := Prev (CLo);
2953 if Chars (ALo) = Chars (Ent) then
2954 Error_Msg_Name_1 := Chars (ALo);
2955 Error_Msg_N ("\ %??", N);
2956 else
2957 Error_Msg_Name_1 := Chars (ALo);
2958 Error_Msg_Name_2 := Chars (Ent);
2959 Error_Msg_N ("\ % .. %??", N);
2960 end if;
2961 end if;
2963 -- Output missing value(s) at end
2965 if Chars (AHi) /= Chars (CHi) then
2966 Ent := Next (CHi);
2968 if Chars (AHi) = Chars (Ent) then
2969 Error_Msg_Name_1 := Chars (Ent);
2970 Error_Msg_N ("\ %??", N);
2971 else
2972 Error_Msg_Name_1 := Chars (Ent);
2973 Error_Msg_Name_2 := Chars (AHi);
2974 Error_Msg_N ("\ % .. %??", N);
2975 end if;
2976 end if;
2978 -- Warning case 2, dubious sliding. The First_Subtype
2979 -- test distinguishes between a constrained type where
2980 -- sliding is not allowed (so we will get a warning
2981 -- later that Constraint_Error will be raised), and
2982 -- the unconstrained case where sliding is permitted.
2984 elsif (Enumeration_Pos (CHi) - Enumeration_Pos (CLo))
2986 (Enumeration_Pos (AHi) - Enumeration_Pos (ALo))
2987 and then Chars (ALo) /= Chars (CLo)
2988 and then
2989 not Is_Constrained (First_Subtype (Etype (N)))
2990 then
2991 Error_Msg_N
2992 ("bounds of aggregate do not match target??", N);
2993 end if;
2994 end;
2995 end if;
2996 end if;
2998 -- If no others, aggregate bounds come from aggregate
3000 Aggr_Low := Choices_Low;
3001 Aggr_High := Choices_High;
3002 end if;
3003 end Step_2;
3005 -- STEP 3: Process positional components
3007 else
3008 -- STEP 3 (A): Process positional elements
3010 Expr := First (Expressions (N));
3011 Nb_Elements := Uint_0;
3012 while Present (Expr) loop
3013 Nb_Elements := Nb_Elements + 1;
3015 -- Ada 2005 (AI-231)
3017 if Ada_Version >= Ada_2005 and then Known_Null (Expr) then
3018 Check_Can_Never_Be_Null (Etype (N), Expr);
3019 end if;
3021 if not Resolve_Aggr_Expr (Expr, Single_Elmt => True) then
3022 return Failure;
3023 end if;
3025 -- Check incorrect use of dynamically tagged expression
3027 if Is_Tagged_Type (Etype (Expr)) then
3028 Check_Dynamically_Tagged_Expression
3029 (Expr => Expr,
3030 Typ => Component_Type (Etype (N)),
3031 Related_Nod => N);
3032 end if;
3034 Next (Expr);
3035 end loop;
3037 if Others_Present then
3038 Assoc := Last (Component_Associations (N));
3040 -- Ada 2005 (AI-231)
3042 if Ada_Version >= Ada_2005 and then Known_Null (Assoc) then
3043 Check_Can_Never_Be_Null (Etype (N), Expression (Assoc));
3044 end if;
3046 -- Ada 2005 (AI-287): In case of default initialized component,
3047 -- we delay the resolution to the expansion phase.
3049 if Box_Present (Assoc) then
3051 -- Ada 2005 (AI-287): In case of default initialization of a
3052 -- component the expander will generate calls to the
3053 -- corresponding initialization subprogram. We need to call
3054 -- Resolve_Aggr_Expr to check the rules about
3055 -- dimensionality.
3057 if not Resolve_Aggr_Expr (Assoc, Single_Elmt => False) then
3058 return Failure;
3059 end if;
3061 elsif not Resolve_Aggr_Expr (Expression (Assoc),
3062 Single_Elmt => False)
3063 then
3064 return Failure;
3066 -- Check incorrect use of dynamically tagged expression. The
3067 -- expression of the others choice has not been resolved yet.
3068 -- In order to diagnose the semantic error we create a duplicate
3069 -- tree to analyze it and perform the check.
3071 elsif Nkind (Assoc) /= N_Iterated_Component_Association then
3072 declare
3073 Save_Analysis : constant Boolean := Full_Analysis;
3074 Expr : constant Node_Id :=
3075 New_Copy_Tree (Expression (Assoc));
3077 begin
3078 Expander_Mode_Save_And_Set (False);
3079 Full_Analysis := False;
3080 Analyze (Expr);
3081 Full_Analysis := Save_Analysis;
3082 Expander_Mode_Restore;
3084 if Is_Tagged_Type (Etype (Expr)) then
3085 Check_Dynamically_Tagged_Expression
3086 (Expr => Expr,
3087 Typ => Component_Type (Etype (N)),
3088 Related_Nod => N);
3089 end if;
3090 end;
3091 end if;
3092 end if;
3094 -- STEP 3 (B): Compute the aggregate bounds
3096 if Others_Present then
3097 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
3099 else
3100 if Others_Allowed then
3101 Get_Index_Bounds (Index_Constr, Aggr_Low, Discard);
3102 else
3103 Aggr_Low := Index_Typ_Low;
3104 end if;
3106 Aggr_High := Add (Nb_Elements - 1, To => Aggr_Low);
3107 Check_Bound (Index_Base_High, Aggr_High);
3108 end if;
3109 end if;
3111 -- STEP 4: Perform static aggregate checks and save the bounds
3113 -- Check (A)
3115 Check_Bounds (Index_Typ_Low, Index_Typ_High, Aggr_Low, Aggr_High);
3116 Check_Bounds (Index_Base_Low, Index_Base_High, Aggr_Low, Aggr_High);
3118 -- Check (B)
3120 if Others_Present and then Nb_Discrete_Choices > 0 then
3121 Check_Bounds (Aggr_Low, Aggr_High, Choices_Low, Choices_High);
3122 Check_Bounds (Index_Typ_Low, Index_Typ_High,
3123 Choices_Low, Choices_High);
3124 Check_Bounds (Index_Base_Low, Index_Base_High,
3125 Choices_Low, Choices_High);
3127 -- Check (C)
3129 elsif Others_Present and then Nb_Elements > 0 then
3130 Check_Length (Aggr_Low, Aggr_High, Nb_Elements);
3131 Check_Length (Index_Typ_Low, Index_Typ_High, Nb_Elements);
3132 Check_Length (Index_Base_Low, Index_Base_High, Nb_Elements);
3133 end if;
3135 if Raises_Constraint_Error (Aggr_Low)
3136 or else Raises_Constraint_Error (Aggr_High)
3137 then
3138 Set_Raises_Constraint_Error (N);
3139 end if;
3141 Aggr_Low := Duplicate_Subexpr (Aggr_Low);
3143 -- Do not duplicate Aggr_High if Aggr_High = Aggr_Low + Nb_Elements
3144 -- since the addition node returned by Add is not yet analyzed. Attach
3145 -- to tree and analyze first. Reset analyzed flag to ensure it will get
3146 -- analyzed when it is a literal bound whose type must be properly set.
3148 if Others_Present or else Nb_Discrete_Choices > 0 then
3149 Aggr_High := Duplicate_Subexpr (Aggr_High);
3151 if Etype (Aggr_High) = Universal_Integer then
3152 Set_Analyzed (Aggr_High, False);
3153 end if;
3154 end if;
3156 -- If the aggregate already has bounds attached to it, it means this is
3157 -- a positional aggregate created as an optimization by
3158 -- Exp_Aggr.Convert_To_Positional, so we don't want to change those
3159 -- bounds.
3161 if Present (Aggregate_Bounds (N))
3162 and then not Others_Allowed
3163 and then not Comes_From_Source (N)
3164 then
3165 Aggr_Low := Low_Bound (Aggregate_Bounds (N));
3166 Aggr_High := High_Bound (Aggregate_Bounds (N));
3167 end if;
3169 Set_Aggregate_Bounds
3170 (N, Make_Range (Loc, Low_Bound => Aggr_Low, High_Bound => Aggr_High));
3172 -- The bounds may contain expressions that must be inserted upwards.
3173 -- Attach them fully to the tree. After analysis, remove side effects
3174 -- from upper bound, if still needed.
3176 Set_Parent (Aggregate_Bounds (N), N);
3177 Analyze_And_Resolve (Aggregate_Bounds (N), Index_Typ);
3178 Check_Unset_Reference (Aggregate_Bounds (N));
3180 if not Others_Present and then Nb_Discrete_Choices = 0 then
3181 Set_High_Bound
3182 (Aggregate_Bounds (N),
3183 Duplicate_Subexpr (High_Bound (Aggregate_Bounds (N))));
3184 end if;
3186 -- Check the dimensions of each component in the array aggregate
3188 Analyze_Dimension_Array_Aggregate (N, Component_Typ);
3190 return Success;
3191 end Resolve_Array_Aggregate;
3193 ---------------------------------
3194 -- Resolve_Container_Aggregate --
3195 ---------------------------------
3197 procedure Resolve_Container_Aggregate (N : Node_Id; Typ : Entity_Id) is
3198 procedure Resolve_Iterated_Association
3199 (Comp : Node_Id;
3200 Key_Type : Entity_Id;
3201 Elmt_Type : Entity_Id);
3202 -- Resolve choices and expression in an iterated component association
3203 -- or an iterated element association, which has a key_expression.
3204 -- This is similar but not identical to the handling of this construct
3205 -- in an array aggregate.
3206 -- For a named container, the type of each choice must be compatible
3207 -- with the key type. For a positional container, the choice must be
3208 -- a subtype indication or an iterator specification that determines
3209 -- an element type.
3211 Asp : constant Node_Id := Find_Value_Of_Aspect (Typ, Aspect_Aggregate);
3213 Empty_Subp : Node_Id := Empty;
3214 Add_Named_Subp : Node_Id := Empty;
3215 Add_Unnamed_Subp : Node_Id := Empty;
3216 New_Indexed_Subp : Node_Id := Empty;
3217 Assign_Indexed_Subp : Node_Id := Empty;
3219 ----------------------------------
3220 -- Resolve_Iterated_Association --
3221 ----------------------------------
3223 procedure Resolve_Iterated_Association
3224 (Comp : Node_Id;
3225 Key_Type : Entity_Id;
3226 Elmt_Type : Entity_Id)
3228 Loc : constant Source_Ptr := Sloc (N);
3229 Choice : Node_Id;
3230 Copy : Node_Id;
3231 Ent : Entity_Id;
3232 Expr : Node_Id;
3233 Key_Expr : Node_Id;
3234 Id : Entity_Id;
3235 Id_Name : Name_Id;
3236 Typ : Entity_Id := Empty;
3238 begin
3239 Error_Msg_Ada_2022_Feature ("iterated component", Loc);
3241 -- If this is an Iterated_Element_Association then either a
3242 -- an Iterator_Specification or a Loop_Parameter specification
3243 -- is present. In both cases a Key_Expression is present.
3245 if Nkind (Comp) = N_Iterated_Element_Association then
3247 -- Create a temporary scope to avoid some modifications from
3248 -- escaping the Analyze call below. The original Tree will be
3249 -- reanalyzed later.
3251 Ent := New_Internal_Entity
3252 (E_Loop, Current_Scope, Sloc (Comp), 'L');
3253 Set_Etype (Ent, Standard_Void_Type);
3254 Set_Parent (Ent, Parent (Comp));
3255 Push_Scope (Ent);
3257 if Present (Loop_Parameter_Specification (Comp)) then
3258 Copy := Copy_Separate_Tree (Comp);
3260 Analyze
3261 (Loop_Parameter_Specification (Copy));
3263 Id_Name := Chars (Defining_Identifier
3264 (Loop_Parameter_Specification (Comp)));
3265 else
3266 Copy := Copy_Separate_Tree (Iterator_Specification (Comp));
3267 Analyze (Copy);
3269 Id_Name := Chars (Defining_Identifier
3270 (Iterator_Specification (Comp)));
3271 end if;
3273 -- Key expression must have the type of the key. We analyze
3274 -- a copy of the original expression, because it will be
3275 -- reanalyzed and copied as needed during expansion of the
3276 -- corresponding loop.
3278 Key_Expr := Key_Expression (Comp);
3279 Analyze_And_Resolve (New_Copy_Tree (Key_Expr), Key_Type);
3280 End_Scope;
3282 Typ := Key_Type;
3284 elsif Present (Iterator_Specification (Comp)) then
3285 -- Create a temporary scope to avoid some modifications from
3286 -- escaping the Analyze call below. The original Tree will be
3287 -- reanalyzed later.
3289 Ent := New_Internal_Entity
3290 (E_Loop, Current_Scope, Sloc (Comp), 'L');
3291 Set_Etype (Ent, Standard_Void_Type);
3292 Set_Parent (Ent, Parent (Comp));
3293 Push_Scope (Ent);
3295 Copy := Copy_Separate_Tree (Iterator_Specification (Comp));
3296 Id_Name :=
3297 Chars (Defining_Identifier (Iterator_Specification (Comp)));
3299 Preanalyze (Copy);
3301 End_Scope;
3303 Typ := Etype (Defining_Identifier (Copy));
3305 else
3306 Choice := First (Discrete_Choices (Comp));
3308 while Present (Choice) loop
3309 Analyze (Choice);
3311 -- Choice can be a subtype name, a range, or an expression
3313 if Is_Entity_Name (Choice)
3314 and then Is_Type (Entity (Choice))
3315 and then Base_Type (Entity (Choice)) = Base_Type (Key_Type)
3316 then
3317 null;
3319 elsif Present (Key_Type) then
3320 Analyze_And_Resolve (Choice, Key_Type);
3321 Typ := Key_Type;
3322 else
3323 Typ := Etype (Choice); -- assume unique for now
3324 end if;
3326 Next (Choice);
3327 end loop;
3329 Id_Name := Chars (Defining_Identifier (Comp));
3330 end if;
3332 -- Create a scope in which to introduce an index, which is usually
3333 -- visible in the expression for the component, and needed for its
3334 -- analysis.
3336 Id := Make_Defining_Identifier (Sloc (Comp), Id_Name);
3337 Ent := New_Internal_Entity (E_Loop,
3338 Current_Scope, Sloc (Comp), 'L');
3339 Set_Etype (Ent, Standard_Void_Type);
3340 Set_Parent (Ent, Parent (Comp));
3341 Push_Scope (Ent);
3343 -- Insert and decorate the loop variable in the current scope.
3344 -- The expression has to be analyzed once the loop variable is
3345 -- directly visible. Mark the variable as referenced to prevent
3346 -- spurious warnings, given that subsequent uses of its name in the
3347 -- expression will reference the internal (synonym) loop variable.
3349 Enter_Name (Id);
3351 pragma Assert (Present (Typ));
3352 Set_Etype (Id, Typ);
3354 Mutate_Ekind (Id, E_Variable);
3355 Set_Is_Not_Self_Hidden (Id);
3356 Set_Scope (Id, Ent);
3357 Set_Referenced (Id);
3359 -- Analyze a copy of the expression, to verify legality. We use
3360 -- a copy because the expression will be analyzed anew when the
3361 -- enclosing aggregate is expanded, and the construct is rewritten
3362 -- as a loop with a new index variable.
3364 Expr := New_Copy_Tree (Expression (Comp));
3365 Preanalyze_And_Resolve (Expr, Elmt_Type);
3366 End_Scope;
3368 end Resolve_Iterated_Association;
3370 -- Start of processing for Resolve_Container_Aggregate
3372 begin
3373 pragma Assert (Nkind (Asp) = N_Aggregate);
3375 Set_Etype (N, Typ);
3376 Parse_Aspect_Aggregate (Asp,
3377 Empty_Subp, Add_Named_Subp, Add_Unnamed_Subp,
3378 New_Indexed_Subp, Assign_Indexed_Subp);
3380 if Present (Add_Unnamed_Subp)
3381 and then No (New_Indexed_Subp)
3382 and then Present (Etype (Add_Unnamed_Subp))
3383 and then Etype (Add_Unnamed_Subp) /= Any_Type
3384 then
3385 declare
3386 Elmt_Type : constant Entity_Id :=
3387 Etype (Next_Formal
3388 (First_Formal (Entity (Add_Unnamed_Subp))));
3389 Comp : Node_Id;
3391 begin
3392 if Present (Expressions (N)) then
3393 -- positional aggregate
3395 Comp := First (Expressions (N));
3396 while Present (Comp) loop
3397 Analyze_And_Resolve (Comp, Elmt_Type);
3398 Next (Comp);
3399 end loop;
3400 end if;
3402 -- Empty aggregate, to be replaced by Empty during
3403 -- expansion, or iterated component association.
3405 if Present (Component_Associations (N)) then
3406 declare
3407 Comp : Node_Id := First (Component_Associations (N));
3408 begin
3409 while Present (Comp) loop
3410 if Nkind (Comp) /=
3411 N_Iterated_Component_Association
3412 then
3413 Error_Msg_N ("illegal component association "
3414 & "for unnamed container aggregate", Comp);
3415 return;
3416 else
3417 Resolve_Iterated_Association
3418 (Comp, Empty, Elmt_Type);
3419 end if;
3421 Next (Comp);
3422 end loop;
3423 end;
3424 end if;
3425 end;
3427 elsif Present (Add_Named_Subp)
3428 and then Etype (Add_Named_Subp) /= Any_Type
3429 then
3430 declare
3431 -- Retrieves types of container, key, and element from the
3432 -- specified insertion procedure.
3434 Container : constant Entity_Id :=
3435 First_Formal (Entity (Add_Named_Subp));
3436 Key_Type : constant Entity_Id := Etype (Next_Formal (Container));
3437 Elmt_Type : constant Entity_Id :=
3438 Etype (Next_Formal (Next_Formal (Container)));
3439 Comp : Node_Id;
3440 Choice : Node_Id;
3442 begin
3443 Comp := First (Component_Associations (N));
3444 while Present (Comp) loop
3445 if Nkind (Comp) = N_Component_Association then
3446 Choice := First (Choices (Comp));
3448 while Present (Choice) loop
3449 Analyze_And_Resolve (Choice, Key_Type);
3450 if not Is_Static_Expression (Choice) then
3451 Error_Msg_N ("choice must be static", Choice);
3452 end if;
3454 Next (Choice);
3455 end loop;
3457 Analyze_And_Resolve (Expression (Comp), Elmt_Type);
3459 elsif Nkind (Comp) in
3460 N_Iterated_Component_Association |
3461 N_Iterated_Element_Association
3462 then
3463 Resolve_Iterated_Association
3464 (Comp, Key_Type, Elmt_Type);
3465 end if;
3467 Next (Comp);
3468 end loop;
3469 end;
3471 elsif Present (Assign_Indexed_Subp)
3472 and then Etype (Assign_Indexed_Subp) /= Any_Type
3473 then
3474 -- Indexed Aggregate. Positional or indexed component
3475 -- can be present, but not both. Choices must be static
3476 -- values or ranges with static bounds.
3478 declare
3479 Container : constant Entity_Id :=
3480 First_Formal (Entity (Assign_Indexed_Subp));
3481 Index_Type : constant Entity_Id := Etype (Next_Formal (Container));
3482 Comp_Type : constant Entity_Id :=
3483 Etype (Next_Formal (Next_Formal (Container)));
3484 Comp : Node_Id;
3485 Choice : Node_Id;
3486 Num_Choices : Nat := 0;
3488 Hi_Val : Uint;
3489 Lo_Val : Uint;
3490 begin
3491 if Present (Expressions (N)) then
3492 Comp := First (Expressions (N));
3493 while Present (Comp) loop
3494 Analyze_And_Resolve (Comp, Comp_Type);
3495 Next (Comp);
3496 end loop;
3497 end if;
3499 if Present (Component_Associations (N))
3500 and then not Is_Empty_List (Component_Associations (N))
3501 then
3502 if Present (Expressions (N))
3503 and then not Is_Empty_List (Expressions (N))
3504 then
3505 Error_Msg_N ("container aggregate cannot be "
3506 & "both positional and named", N);
3507 return;
3508 end if;
3510 Comp := First (Component_Associations (N));
3512 while Present (Comp) loop
3513 if Nkind (Comp) = N_Component_Association then
3514 Choice := First (Choices (Comp));
3516 while Present (Choice) loop
3517 Analyze_And_Resolve (Choice, Index_Type);
3518 Num_Choices := Num_Choices + 1;
3519 Next (Choice);
3520 end loop;
3522 Analyze_And_Resolve (Expression (Comp), Comp_Type);
3524 elsif Nkind (Comp) in
3525 N_Iterated_Component_Association |
3526 N_Iterated_Element_Association
3527 then
3528 Resolve_Iterated_Association
3529 (Comp, Index_Type, Comp_Type);
3530 Num_Choices := Num_Choices + 1;
3531 end if;
3533 Next (Comp);
3534 end loop;
3536 -- The component associations in an indexed aggregate
3537 -- must denote a contiguous set of static values. We
3538 -- build a table of values/ranges and sort it, as is done
3539 -- elsewhere for case statements and array aggregates.
3540 -- If the aggregate has a single iterated association it
3541 -- is allowed to be nonstatic and there is nothing to check.
3543 if Num_Choices > 1 then
3544 declare
3545 Table : Case_Table_Type (1 .. Num_Choices);
3546 No_Choice : Pos := 1;
3547 Lo, Hi : Node_Id;
3549 -- Traverse aggregate to determine size of needed table.
3550 -- Verify that bounds are static and that loops have no
3551 -- filters or key expressions.
3553 begin
3554 Comp := First (Component_Associations (N));
3555 while Present (Comp) loop
3556 if Nkind (Comp) = N_Iterated_Element_Association then
3557 if Present
3558 (Loop_Parameter_Specification (Comp))
3559 then
3560 if Present (Iterator_Filter
3561 (Loop_Parameter_Specification (Comp)))
3562 then
3563 Error_Msg_N
3564 ("iterator filter not allowed " &
3565 "in indexed aggregate", Comp);
3566 return;
3568 elsif Present (Key_Expression
3569 (Loop_Parameter_Specification (Comp)))
3570 then
3571 Error_Msg_N
3572 ("key expression not allowed " &
3573 "in indexed aggregate", Comp);
3574 return;
3575 end if;
3576 end if;
3577 else
3578 Choice := First (Choices (Comp));
3580 while Present (Choice) loop
3581 Get_Index_Bounds (Choice, Lo, Hi);
3582 Table (No_Choice).Choice := Choice;
3583 Table (No_Choice).Lo := Lo;
3584 Table (No_Choice).Hi := Hi;
3586 -- Verify staticness of value or range
3588 if not Is_Static_Expression (Lo)
3589 or else not Is_Static_Expression (Hi)
3590 then
3591 Error_Msg_N
3592 ("nonstatic expression for index " &
3593 "for indexed aggregate", Choice);
3594 return;
3595 end if;
3597 No_Choice := No_Choice + 1;
3598 Next (Choice);
3599 end loop;
3600 end if;
3602 Next (Comp);
3603 end loop;
3605 Sort_Case_Table (Table);
3607 for J in 1 .. Num_Choices - 1 loop
3608 Hi_Val := Expr_Value (Table (J).Hi);
3609 Lo_Val := Expr_Value (Table (J + 1).Lo);
3611 if Lo_Val = Hi_Val then
3612 Error_Msg_N
3613 ("duplicate index in indexed aggregate",
3614 Table (J + 1).Choice);
3615 exit;
3617 elsif Lo_Val < Hi_Val then
3618 Error_Msg_N
3619 ("overlapping indices in indexed aggregate",
3620 Table (J + 1).Choice);
3621 exit;
3623 elsif Lo_Val > Hi_Val + 1 then
3624 Error_Msg_N
3625 ("missing index values", Table (J + 1).Choice);
3626 exit;
3627 end if;
3628 end loop;
3629 end;
3630 end if;
3631 end if;
3632 end;
3633 end if;
3634 end Resolve_Container_Aggregate;
3636 -----------------------------
3637 -- Resolve_Delta_Aggregate --
3638 -----------------------------
3640 procedure Resolve_Delta_Aggregate (N : Node_Id; Typ : Entity_Id) is
3641 Base : constant Node_Id := Expression (N);
3643 begin
3644 Error_Msg_Ada_2022_Feature ("delta aggregate", Sloc (N));
3646 if not Is_Composite_Type (Typ) then
3647 Error_Msg_N ("not a composite type", N);
3648 end if;
3650 Analyze_And_Resolve (Base, Typ);
3652 if Is_Array_Type (Typ) then
3653 -- For an array_delta_aggregate, the base_expression and each
3654 -- expression in every array_component_association shall be of a
3655 -- nonlimited type; RM 4.3.4(13/5). However, to prevent repeated
3656 -- errors we only check the base expression and not array component
3657 -- associations.
3659 if Is_Limited_Type (Etype (Base)) then
3660 Error_Msg_N
3661 ("array delta aggregate shall be of a nonlimited type", Base);
3662 Explain_Limited_Type (Etype (Base), Base);
3663 end if;
3665 Resolve_Delta_Array_Aggregate (N, Typ);
3666 else
3668 -- Delta aggregates for record types must use parentheses,
3669 -- not square brackets.
3671 if Is_Homogeneous_Aggregate (N) then
3672 Error_Msg_N
3673 ("delta aggregates for record types must use (), not '[']", N);
3674 end if;
3676 -- The base_expression of a record_delta_aggregate can be of a
3677 -- limited type only if it is newly constructed; RM 7.5(2.1/5).
3679 Check_Expr_OK_In_Limited_Aggregate (Base);
3681 Resolve_Delta_Record_Aggregate (N, Typ);
3682 end if;
3684 Set_Etype (N, Typ);
3685 end Resolve_Delta_Aggregate;
3687 -----------------------------------
3688 -- Resolve_Delta_Array_Aggregate --
3689 -----------------------------------
3691 procedure Resolve_Delta_Array_Aggregate (N : Node_Id; Typ : Entity_Id) is
3692 Deltas : constant List_Id := Component_Associations (N);
3693 Index_Type : constant Entity_Id := Etype (First_Index (Typ));
3695 Assoc : Node_Id;
3696 Choice : Node_Id;
3697 Expr : Node_Id;
3699 Deep_Choice_Seen : Boolean := False;
3701 begin
3702 Assoc := First (Deltas);
3703 while Present (Assoc) loop
3704 if Nkind (Assoc) = N_Iterated_Component_Association then
3705 Choice := First (Choice_List (Assoc));
3706 while Present (Choice) loop
3707 if Nkind (Choice) = N_Others_Choice then
3708 Error_Msg_N
3709 ("OTHERS not allowed in delta aggregate", Choice);
3711 elsif Nkind (Choice) = N_Subtype_Indication then
3712 Resolve_Discrete_Subtype_Indication
3713 (Choice, Base_Type (Index_Type));
3715 else
3716 Analyze_And_Resolve (Choice, Index_Type);
3717 end if;
3719 Next (Choice);
3720 end loop;
3722 declare
3723 Id : constant Entity_Id := Defining_Identifier (Assoc);
3724 Ent : constant Entity_Id :=
3725 New_Internal_Entity
3726 (E_Loop, Current_Scope, Sloc (Assoc), 'L');
3728 begin
3729 Set_Etype (Ent, Standard_Void_Type);
3730 Set_Parent (Ent, Assoc);
3731 Push_Scope (Ent);
3733 if No (Scope (Id)) then
3734 Set_Etype (Id, Index_Type);
3735 Mutate_Ekind (Id, E_Variable);
3736 Set_Is_Not_Self_Hidden (Id);
3737 Set_Scope (Id, Ent);
3738 end if;
3739 Enter_Name (Id);
3741 -- Resolve a copy of the expression, after setting
3742 -- its parent properly to preserve its context.
3744 Expr := New_Copy_Tree (Expression (Assoc));
3745 Set_Parent (Expr, Assoc);
3746 Analyze_And_Resolve (Expr, Component_Type (Typ));
3747 End_Scope;
3748 end;
3750 else
3751 Choice := First (Choice_List (Assoc));
3752 while Present (Choice) loop
3753 if Is_Deep_Choice (Choice, Typ) then
3754 pragma Assert (All_Extensions_Allowed);
3755 Deep_Choice_Seen := True;
3757 -- a deep delta aggregate
3758 Resolve_Deep_Delta_Assoc (Assoc, Typ);
3759 else
3760 Analyze (Choice);
3762 if Nkind (Choice) = N_Others_Choice then
3763 Error_Msg_N
3764 ("OTHERS not allowed in delta aggregate", Choice);
3766 elsif Is_Entity_Name (Choice)
3767 and then Is_Type (Entity (Choice))
3768 then
3769 -- Choice covers a range of values
3771 if Base_Type (Entity (Choice)) /=
3772 Base_Type (Index_Type)
3773 then
3774 Error_Msg_NE
3775 ("choice does not match index type of &",
3776 Choice, Typ);
3777 end if;
3779 elsif Nkind (Choice) = N_Subtype_Indication then
3780 Resolve_Discrete_Subtype_Indication
3781 (Choice, Base_Type (Index_Type));
3783 else
3784 Resolve (Choice, Index_Type);
3785 end if;
3786 end if;
3788 Next (Choice);
3789 end loop;
3791 -- For an array_delta_aggregate, the array_component_association
3792 -- shall not use the box symbol <>; RM 4.3.4(11/5).
3794 pragma Assert
3795 (Box_Present (Assoc) xor Present (Expression (Assoc)));
3797 if Box_Present (Assoc) then
3798 Error_Msg_N
3799 ("'<'> in array delta aggregate is not allowed", Assoc);
3800 elsif not Deep_Choice_Seen then
3801 Analyze_And_Resolve (Expression (Assoc), Component_Type (Typ));
3802 end if;
3803 end if;
3805 Next (Assoc);
3806 end loop;
3807 end Resolve_Delta_Array_Aggregate;
3809 ------------------------------------
3810 -- Resolve_Delta_Record_Aggregate --
3811 ------------------------------------
3813 procedure Resolve_Delta_Record_Aggregate (N : Node_Id; Typ : Entity_Id) is
3815 -- Variables used to verify that discriminant-dependent components
3816 -- appear in the same variant.
3818 Comp_Ref : Entity_Id := Empty; -- init to avoid warning
3819 Variant : Node_Id;
3821 procedure Check_Variant (Id : Node_Id);
3822 -- If a given component of the delta aggregate appears in a variant
3823 -- part, verify that it is within the same variant as that of previous
3824 -- specified variant components of the delta.
3826 function Get_Component_Type
3827 (Selector : Node_Id; Enclosing_Type : Entity_Id) return Entity_Id;
3828 -- Locate component with a given name and return its type.
3829 -- If none found then report error and return Empty.
3831 function Nested_In (V1 : Node_Id; V2 : Node_Id) return Boolean;
3832 -- Determine whether variant V1 is within variant V2
3834 function Variant_Depth (N : Node_Id) return Natural;
3835 -- Determine the distance of a variant to the enclosing type declaration
3837 --------------------
3838 -- Check_Variant --
3839 --------------------
3841 procedure Check_Variant (Id : Node_Id) is
3842 Comp : Entity_Id;
3843 Comp_Variant : Node_Id;
3845 begin
3846 if not Has_Discriminants (Typ) then
3847 return;
3848 end if;
3850 Comp := First_Entity (Typ);
3851 while Present (Comp) loop
3852 exit when Chars (Comp) = Chars (Id);
3853 Next_Component (Comp);
3854 end loop;
3856 -- Find the variant, if any, whose component list includes the
3857 -- component declaration.
3859 Comp_Variant := Parent (Parent (List_Containing (Parent (Comp))));
3860 if Nkind (Comp_Variant) = N_Variant then
3861 if No (Variant) then
3862 Variant := Comp_Variant;
3863 Comp_Ref := Comp;
3865 elsif Variant /= Comp_Variant then
3866 declare
3867 D1 : constant Integer := Variant_Depth (Variant);
3868 D2 : constant Integer := Variant_Depth (Comp_Variant);
3870 begin
3871 if D1 = D2
3872 or else
3873 (D1 > D2 and then not Nested_In (Variant, Comp_Variant))
3874 or else
3875 (D2 > D1 and then not Nested_In (Comp_Variant, Variant))
3876 then
3877 pragma Assert (Present (Comp_Ref));
3878 Error_Msg_Node_2 := Comp_Ref;
3879 Error_Msg_NE
3880 ("& and & appear in different variants", Id, Comp);
3882 -- Otherwise retain the deeper variant for subsequent tests
3884 elsif D2 > D1 then
3885 Variant := Comp_Variant;
3886 end if;
3887 end;
3888 end if;
3889 end if;
3890 end Check_Variant;
3892 ------------------------
3893 -- Get_Component_Type --
3894 ------------------------
3896 function Get_Component_Type
3897 (Selector : Node_Id; Enclosing_Type : Entity_Id) return Entity_Id
3899 Comp : Entity_Id;
3900 begin
3901 case Nkind (Selector) is
3902 when N_Selected_Component | N_Indexed_Component =>
3903 -- a deep delta aggregate choice
3905 declare
3906 Prefix_Type : constant Entity_Id :=
3907 Get_Component_Type (Prefix (Selector), Enclosing_Type);
3908 begin
3909 if No (Prefix_Type) then
3910 pragma Assert (Serious_Errors_Detected > 0);
3911 return Empty;
3912 end if;
3914 -- Set the type of the prefix for GNATprove
3916 Set_Etype (Prefix (Selector), Prefix_Type);
3918 if Nkind (Selector) = N_Selected_Component then
3919 return Get_Component_Type
3920 (Selector_Name (Selector),
3921 Enclosing_Type => Prefix_Type);
3922 elsif not Is_Array_Type (Prefix_Type) then
3923 Error_Msg_NE
3924 ("type& is not an array type",
3925 Selector, Prefix_Type);
3926 elsif Number_Dimensions (Prefix_Type) /= 1 then
3927 Error_Msg_NE
3928 ("array type& not one-dimensional",
3929 Selector, Prefix_Type);
3930 elsif List_Length (Expressions (Selector)) /= 1 then
3931 Error_Msg_NE
3932 ("wrong number of indices for array type&",
3933 Selector, Prefix_Type);
3934 else
3935 Analyze_And_Resolve
3936 (First (Expressions (Selector)),
3937 Etype (First_Index (Prefix_Type)));
3938 return Component_Type (Prefix_Type);
3939 end if;
3940 end;
3942 when others =>
3943 null;
3944 end case;
3946 Comp := First_Entity (Enclosing_Type);
3947 while Present (Comp) loop
3948 if Chars (Comp) = Chars (Selector) then
3949 if Ekind (Comp) = E_Discriminant then
3950 Error_Msg_N ("delta cannot apply to discriminant", Selector);
3951 end if;
3953 Set_Entity (Selector, Comp);
3954 Set_Etype (Selector, Etype (Comp));
3956 return Etype (Comp);
3957 end if;
3959 Next_Entity (Comp);
3960 end loop;
3962 Error_Msg_NE
3963 ("type& has no component with this name", Selector, Enclosing_Type);
3964 return Empty;
3965 end Get_Component_Type;
3967 ---------------
3968 -- Nested_In --
3969 ---------------
3971 function Nested_In (V1, V2 : Node_Id) return Boolean is
3972 Par : Node_Id;
3974 begin
3975 Par := Parent (V1);
3976 while Nkind (Par) /= N_Full_Type_Declaration loop
3977 if Par = V2 then
3978 return True;
3979 end if;
3981 Par := Parent (Par);
3982 end loop;
3984 return False;
3985 end Nested_In;
3987 -------------------
3988 -- Variant_Depth --
3989 -------------------
3991 function Variant_Depth (N : Node_Id) return Natural is
3992 Depth : Natural;
3993 Par : Node_Id;
3995 begin
3996 Depth := 0;
3997 Par := Parent (N);
3998 while Nkind (Par) /= N_Full_Type_Declaration loop
3999 Depth := Depth + 1;
4000 Par := Parent (Par);
4001 end loop;
4003 return Depth;
4004 end Variant_Depth;
4006 -- Local variables
4008 Deltas : constant List_Id := Component_Associations (N);
4010 Assoc : Node_Id;
4011 Choice : Node_Id;
4012 Comp_Type : Entity_Id := Empty; -- init to avoid warning
4013 Deep_Choice : Boolean;
4015 -- Start of processing for Resolve_Delta_Record_Aggregate
4017 begin
4018 Variant := Empty;
4020 Assoc := First (Deltas);
4021 while Present (Assoc) loop
4022 Choice := First (Choice_List (Assoc));
4023 while Present (Choice) loop
4024 Deep_Choice := Nkind (Choice) /= N_Identifier;
4025 if Deep_Choice then
4026 Error_Msg_GNAT_Extension
4027 ("deep delta aggregate", Sloc (Choice));
4028 end if;
4030 Comp_Type := Get_Component_Type
4031 (Selector => Choice, Enclosing_Type => Typ);
4033 -- Set the type of the choice for GNATprove
4035 if Deep_Choice then
4036 Set_Etype (Choice, Comp_Type);
4037 end if;
4039 if Present (Comp_Type) then
4040 if not Deep_Choice then
4041 -- ??? Not clear yet how RM 4.3.1(17.7) applies to a
4042 -- deep delta aggregate.
4043 Check_Variant (Choice);
4044 end if;
4045 else
4046 Comp_Type := Any_Type;
4047 end if;
4049 Next (Choice);
4050 end loop;
4052 pragma Assert (Present (Comp_Type));
4054 -- A record_component_association in record_delta_aggregate shall not
4055 -- use the box compound delimiter <> rather than an expression; see
4056 -- RM 4.3.1(17.3/5).
4058 pragma Assert (Present (Expression (Assoc)) xor Box_Present (Assoc));
4060 if Box_Present (Assoc) then
4061 Error_Msg_N
4062 ("'<'> in record delta aggregate is not allowed", Assoc);
4063 else
4064 Analyze_And_Resolve (Expression (Assoc), Comp_Type);
4066 -- The expression must not be of a limited type; RM 4.3.1(17.4/5)
4068 if Is_Limited_Type (Etype (Expression (Assoc))) then
4069 Error_Msg_N
4070 ("expression of a limited type in record delta aggregate " &
4071 "is not allowed",
4072 Expression (Assoc));
4073 end if;
4074 end if;
4076 Next (Assoc);
4077 end loop;
4078 end Resolve_Delta_Record_Aggregate;
4080 ------------------------------
4081 -- Resolve_Deep_Delta_Assoc --
4082 ------------------------------
4084 procedure Resolve_Deep_Delta_Assoc (N : Node_Id; Typ : Entity_Id) is
4085 Choice : constant Node_Id := First (Choice_List (N));
4086 Enclosing_Type : Entity_Id := Typ;
4088 procedure Resolve_Choice_Prefix
4089 (Choice_Prefix : Node_Id; Enclosing_Type : in out Entity_Id);
4090 -- Recursively analyze selectors. Enclosing_Type is set to
4091 -- type of the last component.
4093 ---------------------------
4094 -- Resolve_Choice_Prefix --
4095 ---------------------------
4097 procedure Resolve_Choice_Prefix
4098 (Choice_Prefix : Node_Id; Enclosing_Type : in out Entity_Id)
4100 Selector : Node_Id := Choice_Prefix;
4101 begin
4102 if not Is_Root_Prefix_Of_Deep_Choice (Choice_Prefix) then
4103 Resolve_Choice_Prefix (Prefix (Choice_Prefix), Enclosing_Type);
4105 if Nkind (Choice_Prefix) = N_Selected_Component then
4106 Selector := Selector_Name (Choice_Prefix);
4107 else
4108 pragma Assert (Nkind (Choice_Prefix) = N_Indexed_Component);
4109 Selector := First (Expressions (Choice_Prefix));
4110 end if;
4111 end if;
4113 if Is_Array_Type (Enclosing_Type) then
4114 Analyze_And_Resolve (Selector,
4115 Etype (First_Index (Enclosing_Type)));
4116 Enclosing_Type := Component_Type (Enclosing_Type);
4117 else
4118 declare
4119 Comp : Entity_Id := First_Entity (Enclosing_Type);
4120 Found : Boolean := False;
4121 begin
4122 while Present (Comp) and not Found loop
4123 if Chars (Comp) = Chars (Selector) then
4124 if Ekind (Comp) = E_Discriminant then
4125 Error_Msg_N ("delta cannot apply to discriminant",
4126 Selector);
4127 end if;
4128 Found := True;
4129 Set_Entity (Selector, Comp);
4130 Set_Etype (Selector, Etype (Comp));
4131 Set_Analyzed (Selector);
4132 Enclosing_Type := Etype (Comp);
4133 else
4134 Next_Entity (Comp);
4135 end if;
4136 end loop;
4137 if not Found then
4138 Error_Msg_NE
4139 ("type& has no component with this name",
4140 Selector, Enclosing_Type);
4141 end if;
4142 end;
4143 end if;
4145 -- Set the type of the prefix for GNATprove, except for the root
4146 -- prefix, whose type is already the expected one for a record
4147 -- delta aggregate, or the type of the array index for an
4148 -- array delta aggregate (the only case here really since
4149 -- Resolve_Deep_Delta_Assoc is only called for array delta
4150 -- aggregates).
4152 if Selector /= Choice_Prefix then
4153 Set_Etype (Choice_Prefix, Enclosing_Type);
4154 end if;
4155 end Resolve_Choice_Prefix;
4156 begin
4157 declare
4158 Unimplemented : exception; -- TEMPORARY
4159 begin
4160 if Present (Next (Choice)) then
4161 raise Unimplemented;
4162 end if;
4163 end;
4165 Resolve_Choice_Prefix (Choice, Enclosing_Type);
4166 Analyze_And_Resolve (Expression (N), Enclosing_Type);
4167 end Resolve_Deep_Delta_Assoc;
4169 ---------------------------------
4170 -- Resolve_Extension_Aggregate --
4171 ---------------------------------
4173 -- There are two cases to consider:
4175 -- a) If the ancestor part is a type mark, the components needed are the
4176 -- difference between the components of the expected type and the
4177 -- components of the given type mark.
4179 -- b) If the ancestor part is an expression, it must be unambiguous, and
4180 -- once we have its type we can also compute the needed components as in
4181 -- the previous case. In both cases, if the ancestor type is not the
4182 -- immediate ancestor, we have to build this ancestor recursively.
4184 -- In both cases, discriminants of the ancestor type do not play a role in
4185 -- the resolution of the needed components, because inherited discriminants
4186 -- cannot be used in a type extension. As a result we can compute
4187 -- independently the list of components of the ancestor type and of the
4188 -- expected type.
4190 procedure Resolve_Extension_Aggregate (N : Node_Id; Typ : Entity_Id) is
4191 A : constant Node_Id := Ancestor_Part (N);
4192 A_Type : Entity_Id;
4193 I : Interp_Index;
4194 It : Interp;
4196 function Valid_Limited_Ancestor (Anc : Node_Id) return Boolean;
4197 -- If the type is limited, verify that the ancestor part is a legal
4198 -- expression (aggregate or function call, including 'Input)) that does
4199 -- not require a copy, as specified in 7.5(2).
4201 function Valid_Ancestor_Type return Boolean;
4202 -- Verify that the type of the ancestor part is a non-private ancestor
4203 -- of the expected type, which must be a type extension.
4205 procedure Transform_BIP_Assignment (Typ : Entity_Id);
4206 -- For an extension aggregate whose ancestor part is a build-in-place
4207 -- call returning a nonlimited type, this is used to transform the
4208 -- assignment to the ancestor part to use a temp.
4210 ----------------------------
4211 -- Valid_Limited_Ancestor --
4212 ----------------------------
4214 function Valid_Limited_Ancestor (Anc : Node_Id) return Boolean is
4215 begin
4216 if Is_Entity_Name (Anc) and then Is_Type (Entity (Anc)) then
4217 return True;
4219 -- The ancestor must be a call or an aggregate, but a call may
4220 -- have been expanded into a temporary, so check original node.
4222 elsif Nkind (Anc) in N_Aggregate
4223 | N_Extension_Aggregate
4224 | N_Function_Call
4225 then
4226 return True;
4228 elsif Nkind (Original_Node (Anc)) = N_Function_Call then
4229 return True;
4231 elsif Nkind (Anc) = N_Attribute_Reference
4232 and then Attribute_Name (Anc) = Name_Input
4233 then
4234 return True;
4236 elsif Nkind (Anc) = N_Qualified_Expression then
4237 return Valid_Limited_Ancestor (Expression (Anc));
4239 elsif Nkind (Anc) = N_Raise_Expression then
4240 return True;
4242 else
4243 return False;
4244 end if;
4245 end Valid_Limited_Ancestor;
4247 -------------------------
4248 -- Valid_Ancestor_Type --
4249 -------------------------
4251 function Valid_Ancestor_Type return Boolean is
4252 Imm_Type : Entity_Id;
4254 begin
4255 Imm_Type := Base_Type (Typ);
4256 while Is_Derived_Type (Imm_Type) loop
4257 if Etype (Imm_Type) = Base_Type (A_Type) then
4258 return True;
4260 -- The base type of the parent type may appear as a private
4261 -- extension if it is declared as such in a parent unit of the
4262 -- current one. For consistency of the subsequent analysis use
4263 -- the partial view for the ancestor part.
4265 elsif Is_Private_Type (Etype (Imm_Type))
4266 and then Present (Full_View (Etype (Imm_Type)))
4267 and then Base_Type (A_Type) = Full_View (Etype (Imm_Type))
4268 then
4269 A_Type := Etype (Imm_Type);
4270 return True;
4272 -- The parent type may be a private extension. The aggregate is
4273 -- legal if the type of the aggregate is an extension of it that
4274 -- is not a private extension.
4276 elsif Is_Private_Type (A_Type)
4277 and then not Is_Private_Type (Imm_Type)
4278 and then Present (Full_View (A_Type))
4279 and then Base_Type (Full_View (A_Type)) = Etype (Imm_Type)
4280 then
4281 return True;
4283 -- The parent type may be a raise expression (which is legal in
4284 -- any expression context).
4286 elsif A_Type = Raise_Type then
4287 A_Type := Etype (Imm_Type);
4288 return True;
4290 else
4291 Imm_Type := Etype (Base_Type (Imm_Type));
4292 end if;
4293 end loop;
4295 -- If previous loop did not find a proper ancestor, report error
4297 Error_Msg_NE ("expect ancestor type of &", A, Typ);
4298 return False;
4299 end Valid_Ancestor_Type;
4301 ------------------------------
4302 -- Transform_BIP_Assignment --
4303 ------------------------------
4305 procedure Transform_BIP_Assignment (Typ : Entity_Id) is
4306 Loc : constant Source_Ptr := Sloc (N);
4307 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'Y', A);
4308 Obj_Decl : constant Node_Id :=
4309 Make_Object_Declaration (Loc,
4310 Defining_Identifier => Def_Id,
4311 Constant_Present => True,
4312 Object_Definition => New_Occurrence_Of (Typ, Loc),
4313 Expression => A,
4314 Has_Init_Expression => True);
4315 begin
4316 Set_Etype (Def_Id, Typ);
4317 Set_Ancestor_Part (N, New_Occurrence_Of (Def_Id, Loc));
4318 Insert_Action (N, Obj_Decl);
4319 end Transform_BIP_Assignment;
4321 -- Start of processing for Resolve_Extension_Aggregate
4323 begin
4324 -- Analyze the ancestor part and account for the case where it is a
4325 -- parameterless function call.
4327 Analyze (A);
4328 Check_Parameterless_Call (A);
4330 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
4332 -- AI05-0115: If the ancestor part is a subtype mark, the ancestor
4333 -- must not have unknown discriminants. To catch cases where the
4334 -- aggregate occurs at a place where the full view of the ancestor
4335 -- type is visible and doesn't have unknown discriminants, but the
4336 -- aggregate type was derived from a partial view that has unknown
4337 -- discriminants, we check whether the aggregate type has unknown
4338 -- discriminants (unknown discriminants were inherited), along
4339 -- with checking that the partial view of the ancestor has unknown
4340 -- discriminants. (It might be sufficient to replace the entire
4341 -- condition with Has_Unknown_Discriminants (Typ), but that might
4342 -- miss some cases, not clear, and causes error changes in some tests
4343 -- such as class-wide cases, that aren't clearly improvements. ???)
4345 if Has_Unknown_Discriminants (Entity (A))
4346 or else (Has_Unknown_Discriminants (Typ)
4347 and then Partial_View_Has_Unknown_Discr (Entity (A)))
4348 then
4349 Error_Msg_NE
4350 ("aggregate not available for type& whose ancestor "
4351 & "has unknown discriminants", N, Typ);
4352 end if;
4353 end if;
4355 if not Is_Tagged_Type (Typ) then
4356 Error_Msg_N ("type of extension aggregate must be tagged", N);
4357 return;
4359 elsif Is_Limited_Type (Typ) then
4361 -- Ada 2005 (AI-287): Limited aggregates are allowed
4363 if Ada_Version < Ada_2005 then
4364 Error_Msg_N ("aggregate type cannot be limited", N);
4365 Explain_Limited_Type (Typ, N);
4366 return;
4368 elsif Valid_Limited_Ancestor (A) then
4369 null;
4371 else
4372 Error_Msg_N
4373 ("limited ancestor part must be aggregate or function call", A);
4374 end if;
4376 elsif Is_Class_Wide_Type (Typ) then
4377 Error_Msg_N ("aggregate cannot be of a class-wide type", N);
4378 return;
4379 end if;
4381 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
4382 A_Type := Get_Full_View (Entity (A));
4384 if Valid_Ancestor_Type then
4385 Set_Entity (A, A_Type);
4386 Set_Etype (A, A_Type);
4388 Validate_Ancestor_Part (N);
4389 Resolve_Record_Aggregate (N, Typ);
4390 end if;
4392 elsif Nkind (A) /= N_Aggregate then
4393 if Is_Overloaded (A) then
4394 A_Type := Any_Type;
4396 Get_First_Interp (A, I, It);
4397 while Present (It.Typ) loop
4399 -- Consider limited interpretations if Ada 2005 or higher
4401 if Is_Tagged_Type (It.Typ)
4402 and then (Ada_Version >= Ada_2005
4403 or else not Is_Limited_Type (It.Typ))
4404 then
4405 if A_Type /= Any_Type then
4406 Error_Msg_N ("cannot resolve expression", A);
4407 return;
4408 else
4409 A_Type := It.Typ;
4410 end if;
4411 end if;
4413 Get_Next_Interp (I, It);
4414 end loop;
4416 if A_Type = Any_Type then
4417 if Ada_Version >= Ada_2005 then
4418 Error_Msg_N
4419 ("ancestor part must be of a tagged type", A);
4420 else
4421 Error_Msg_N
4422 ("ancestor part must be of a nonlimited tagged type", A);
4423 end if;
4425 return;
4426 end if;
4428 else
4429 A_Type := Etype (A);
4430 end if;
4432 if Valid_Ancestor_Type then
4433 Resolve (A, A_Type);
4434 Check_Unset_Reference (A);
4435 Check_Non_Static_Context (A);
4437 -- The aggregate is illegal if the ancestor expression is a call
4438 -- to a function with a limited unconstrained result, unless the
4439 -- type of the aggregate is a null extension. This restriction
4440 -- was added in AI05-67 to simplify implementation.
4442 if Nkind (A) = N_Function_Call
4443 and then Is_Limited_Type (A_Type)
4444 and then not Is_Null_Extension (Typ)
4445 and then not Is_Constrained (A_Type)
4446 then
4447 Error_Msg_N
4448 ("type of limited ancestor part must be constrained", A);
4450 -- Reject the use of CPP constructors that leave objects partially
4451 -- initialized. For example:
4453 -- type CPP_Root is tagged limited record ...
4454 -- pragma Import (CPP, CPP_Root);
4456 -- type CPP_DT is new CPP_Root and Iface ...
4457 -- pragma Import (CPP, CPP_DT);
4459 -- type Ada_DT is new CPP_DT with ...
4461 -- Obj : Ada_DT := Ada_DT'(New_CPP_Root with others => <>);
4463 -- Using the constructor of CPP_Root the slots of the dispatch
4464 -- table of CPP_DT cannot be set, and the secondary tag of
4465 -- CPP_DT is unknown.
4467 elsif Nkind (A) = N_Function_Call
4468 and then Is_CPP_Constructor_Call (A)
4469 and then Enclosing_CPP_Parent (Typ) /= A_Type
4470 then
4471 Error_Msg_NE
4472 ("??must use 'C'P'P constructor for type &", A,
4473 Enclosing_CPP_Parent (Typ));
4475 -- The following call is not needed if the previous warning
4476 -- is promoted to an error.
4478 Resolve_Record_Aggregate (N, Typ);
4480 elsif Is_Class_Wide_Type (Etype (A))
4481 and then Nkind (Original_Node (A)) = N_Function_Call
4482 then
4483 -- If the ancestor part is a dispatching call, it appears
4484 -- statically to be a legal ancestor, but it yields any member
4485 -- of the class, and it is not possible to determine whether
4486 -- it is an ancestor of the extension aggregate (much less
4487 -- which ancestor). It is not possible to determine the
4488 -- components of the extension part.
4490 -- This check implements AI-306, which in fact was motivated by
4491 -- an AdaCore query to the ARG after this test was added.
4493 Error_Msg_N ("ancestor part must be statically tagged", A);
4494 else
4495 -- We are using the build-in-place protocol, but we can't build
4496 -- in place, because we need to call the function before
4497 -- allocating the aggregate. Could do better for null
4498 -- extensions, and maybe for nondiscriminated types.
4499 -- This is wrong for limited, but those were wrong already.
4501 if not Is_Inherently_Limited_Type (A_Type)
4502 and then Is_Build_In_Place_Function_Call (A)
4503 then
4504 Transform_BIP_Assignment (A_Type);
4505 end if;
4507 Resolve_Record_Aggregate (N, Typ);
4508 end if;
4509 end if;
4511 else
4512 Error_Msg_N ("no unique type for this aggregate", A);
4513 end if;
4515 Check_Function_Writable_Actuals (N);
4516 end Resolve_Extension_Aggregate;
4518 ----------------------------------
4519 -- Resolve_Null_Array_Aggregate --
4520 ----------------------------------
4522 function Resolve_Null_Array_Aggregate (N : Node_Id) return Boolean is
4523 -- Never returns False, but declared as a function to match
4524 -- other Resolve_Mumble functions.
4526 Loc : constant Source_Ptr := Sloc (N);
4527 Typ : constant Entity_Id := Etype (N);
4529 Index : Node_Id;
4530 Lo, Hi : Node_Id;
4531 Constr : constant List_Id := New_List;
4533 begin
4534 -- Attach the list of constraints at the location of the aggregate, so
4535 -- the individual constraints can be analyzed.
4537 Set_Parent (Constr, N);
4539 -- Create a constrained subtype with null dimensions
4541 Index := First_Index (Typ);
4542 while Present (Index) loop
4543 Get_Index_Bounds (Index, L => Lo, H => Hi);
4545 -- The upper bound is the predecessor of the lower bound
4547 Hi := Make_Attribute_Reference
4548 (Loc,
4549 Prefix => New_Occurrence_Of (Etype (Index), Loc),
4550 Attribute_Name => Name_Pred,
4551 Expressions => New_List (New_Copy_Tree (Lo)));
4553 Append (Make_Range (Loc, New_Copy_Tree (Lo), Hi), Constr);
4554 Analyze_And_Resolve (Last (Constr), Etype (Index));
4556 Next_Index (Index);
4557 end loop;
4559 Set_Compile_Time_Known_Aggregate (N);
4560 Set_Aggregate_Bounds (N, First (Constr));
4562 return True;
4563 end Resolve_Null_Array_Aggregate;
4565 ------------------------------
4566 -- Resolve_Record_Aggregate --
4567 ------------------------------
4569 procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id) is
4570 New_Assoc_List : constant List_Id := New_List;
4571 -- New_Assoc_List is the newly built list of N_Component_Association
4572 -- nodes.
4574 Others_Etype : Entity_Id := Empty;
4575 -- This variable is used to save the Etype of the last record component
4576 -- that takes its value from the others choice. Its purpose is:
4578 -- (a) make sure the others choice is useful
4580 -- (b) make sure the type of all the components whose value is
4581 -- subsumed by the others choice are the same.
4583 -- This variable is updated as a side effect of function Get_Value.
4585 Box_Node : Node_Id := Empty;
4586 Is_Box_Present : Boolean := False;
4587 Is_Box_Init_By_Default : Boolean := False;
4588 Others_Box : Natural := 0;
4589 -- Ada 2005 (AI-287): Variables used in case of default initialization
4590 -- to provide a functionality similar to Others_Etype. Box_Present
4591 -- indicates that the component takes its default initialization;
4592 -- Others_Box counts the number of components of the current aggregate
4593 -- (which may be a sub-aggregate of a larger one) that are default-
4594 -- initialized. A value of One indicates that an others_box is present.
4595 -- Any larger value indicates that the others_box is not redundant.
4596 -- These variables, similar to Others_Etype, are also updated as a side
4597 -- effect of function Get_Value. Box_Node is used to place a warning on
4598 -- a redundant others_box.
4600 procedure Add_Association
4601 (Component : Entity_Id;
4602 Expr : Node_Id;
4603 Assoc_List : List_Id;
4604 Is_Box_Present : Boolean := False);
4605 -- Builds a new N_Component_Association node which associates Component
4606 -- to expression Expr and adds it to the association list being built,
4607 -- either New_Assoc_List, or the association being built for an inner
4608 -- aggregate.
4610 procedure Add_Discriminant_Values
4611 (New_Aggr : Node_Id;
4612 Assoc_List : List_Id);
4613 -- The constraint to a component may be given by a discriminant of the
4614 -- enclosing type, in which case we have to retrieve its value, which is
4615 -- part of the enclosing aggregate. Assoc_List provides the discriminant
4616 -- associations of the current type or of some enclosing record.
4618 function Discriminant_Present (Input_Discr : Entity_Id) return Boolean;
4619 -- If aggregate N is a regular aggregate this routine will return True.
4620 -- Otherwise, if N is an extension aggregate, then Input_Discr denotes
4621 -- a discriminant whose value may already have been specified by N's
4622 -- ancestor part. This routine checks whether this is indeed the case
4623 -- and if so returns False, signaling that no value for Input_Discr
4624 -- should appear in N's aggregate part. Also, in this case, the routine
4625 -- appends to New_Assoc_List the discriminant value specified in the
4626 -- ancestor part.
4628 -- If the aggregate is in a context with expansion delayed, it will be
4629 -- reanalyzed. The inherited discriminant values must not be reinserted
4630 -- in the component list to prevent spurious errors, but they must be
4631 -- present on first analysis to build the proper subtype indications.
4632 -- The flag Inherited_Discriminant is used to prevent the re-insertion.
4634 function Find_Private_Ancestor (Typ : Entity_Id) return Entity_Id;
4635 -- AI05-0115: Find earlier ancestor in the derivation chain that is
4636 -- derived from private view Typ. Whether the aggregate is legal depends
4637 -- on the current visibility of the type as well as that of the parent
4638 -- of the ancestor.
4640 function Get_Value
4641 (Compon : Entity_Id;
4642 From : List_Id;
4643 Consider_Others_Choice : Boolean := False) return Node_Id;
4644 -- Given a record component stored in parameter Compon, this function
4645 -- returns its value as it appears in the list From, which is a list
4646 -- of N_Component_Association nodes.
4648 -- If no component association has a choice for the searched component,
4649 -- the value provided by the others choice is returned, if there is one,
4650 -- and Consider_Others_Choice is set to true. Otherwise Empty is
4651 -- returned. If there is more than one component association giving a
4652 -- value for the searched record component, an error message is emitted
4653 -- and the first found value is returned.
4655 -- If Consider_Others_Choice is set and the returned expression comes
4656 -- from the others choice, then Others_Etype is set as a side effect.
4657 -- An error message is emitted if the components taking their value from
4658 -- the others choice do not have same type.
4660 procedure Propagate_Discriminants
4661 (Aggr : Node_Id;
4662 Assoc_List : List_Id);
4663 -- Nested components may themselves be discriminated types constrained
4664 -- by outer discriminants, whose values must be captured before the
4665 -- aggregate is expanded into assignments.
4667 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Entity_Id);
4668 -- Analyzes and resolves expression Expr against the Etype of the
4669 -- Component. This routine also applies all appropriate checks to Expr.
4670 -- It finally saves a Expr in the newly created association list that
4671 -- will be attached to the final record aggregate. Note that if the
4672 -- Parent pointer of Expr is not set then Expr was produced with a
4673 -- New_Copy_Tree or some such.
4675 procedure Rewrite_Range (Root_Type : Entity_Id; Rge : Node_Id);
4676 -- Rewrite a range node Rge when its bounds refer to non-stored
4677 -- discriminants from Root_Type, to replace them with the stored
4678 -- discriminant values. This is required in GNATprove mode, and is
4679 -- adopted in all modes to avoid special-casing GNATprove mode.
4681 ---------------------
4682 -- Add_Association --
4683 ---------------------
4685 procedure Add_Association
4686 (Component : Entity_Id;
4687 Expr : Node_Id;
4688 Assoc_List : List_Id;
4689 Is_Box_Present : Boolean := False)
4691 Choice_List : constant List_Id := New_List;
4692 Loc : Source_Ptr;
4694 begin
4695 -- If this is a box association the expression is missing, so use the
4696 -- Sloc of the aggregate itself for the new association.
4698 pragma Assert (Present (Expr) xor Is_Box_Present);
4700 if Present (Expr) then
4701 Loc := Sloc (Expr);
4702 else
4703 Loc := Sloc (N);
4704 end if;
4706 Append_To (Choice_List, New_Occurrence_Of (Component, Loc));
4708 Append_To (Assoc_List,
4709 Make_Component_Association (Loc,
4710 Choices => Choice_List,
4711 Expression => Expr,
4712 Box_Present => Is_Box_Present));
4714 -- If this association has a box for a component that is initialized
4715 -- by default, then set flag on the new association to indicate that
4716 -- the original association was for such a box-initialized component.
4718 if Is_Box_Init_By_Default then
4719 Set_Was_Default_Init_Box_Association (Last (Assoc_List));
4720 end if;
4721 end Add_Association;
4723 -----------------------------
4724 -- Add_Discriminant_Values --
4725 -----------------------------
4727 procedure Add_Discriminant_Values
4728 (New_Aggr : Node_Id;
4729 Assoc_List : List_Id)
4731 Assoc : Node_Id;
4732 Discr : Entity_Id;
4733 Discr_Elmt : Elmt_Id;
4734 Discr_Val : Node_Id;
4735 Val : Entity_Id;
4737 begin
4738 Discr := First_Discriminant (Etype (New_Aggr));
4739 Discr_Elmt := First_Elmt (Discriminant_Constraint (Etype (New_Aggr)));
4740 while Present (Discr_Elmt) loop
4741 Discr_Val := Node (Discr_Elmt);
4743 -- If the constraint is given by a discriminant then it is a
4744 -- discriminant of an enclosing record, and its value has already
4745 -- been placed in the association list.
4747 if Is_Entity_Name (Discr_Val)
4748 and then Ekind (Entity (Discr_Val)) = E_Discriminant
4749 then
4750 Val := Entity (Discr_Val);
4752 Assoc := First (Assoc_List);
4753 while Present (Assoc) loop
4754 if Present (Entity (First (Choices (Assoc))))
4755 and then Entity (First (Choices (Assoc))) = Val
4756 then
4757 Discr_Val := Expression (Assoc);
4758 exit;
4759 end if;
4761 Next (Assoc);
4762 end loop;
4763 end if;
4765 Add_Association
4766 (Discr, New_Copy_Tree (Discr_Val),
4767 Component_Associations (New_Aggr));
4769 -- If the discriminant constraint is a current instance, mark the
4770 -- current aggregate so that the self-reference can be expanded by
4771 -- Build_Record_Aggr_Code.Replace_Type later.
4773 if Nkind (Discr_Val) = N_Attribute_Reference
4774 and then Is_Entity_Name (Prefix (Discr_Val))
4775 and then Is_Type (Entity (Prefix (Discr_Val)))
4776 and then
4777 Is_Ancestor
4778 (Entity (Prefix (Discr_Val)),
4779 Etype (N),
4780 Use_Full_View => True)
4781 then
4782 Set_Has_Self_Reference (N);
4783 end if;
4785 Next_Elmt (Discr_Elmt);
4786 Next_Discriminant (Discr);
4787 end loop;
4788 end Add_Discriminant_Values;
4790 --------------------------
4791 -- Discriminant_Present --
4792 --------------------------
4794 function Discriminant_Present (Input_Discr : Entity_Id) return Boolean is
4795 Regular_Aggr : constant Boolean := Nkind (N) /= N_Extension_Aggregate;
4797 Ancestor_Is_Subtyp : Boolean;
4799 Loc : Source_Ptr;
4801 Ancestor : Node_Id;
4802 Ancestor_Typ : Entity_Id;
4803 Comp_Assoc : Node_Id;
4804 Discr : Entity_Id;
4805 Discr_Expr : Node_Id;
4806 Discr_Val : Elmt_Id := No_Elmt;
4807 Orig_Discr : Entity_Id;
4809 begin
4810 if Regular_Aggr then
4811 return True;
4812 end if;
4814 -- Check whether inherited discriminant values have already been
4815 -- inserted in the aggregate. This will be the case if we are
4816 -- re-analyzing an aggregate whose expansion was delayed.
4818 if Present (Component_Associations (N)) then
4819 Comp_Assoc := First (Component_Associations (N));
4820 while Present (Comp_Assoc) loop
4821 if Inherited_Discriminant (Comp_Assoc) then
4822 return True;
4823 end if;
4825 Next (Comp_Assoc);
4826 end loop;
4827 end if;
4829 Ancestor := Ancestor_Part (N);
4830 Ancestor_Typ := Etype (Ancestor);
4831 Loc := Sloc (Ancestor);
4833 -- For a private type with unknown discriminants, use the underlying
4834 -- record view if it is available.
4836 if Has_Unknown_Discriminants (Ancestor_Typ)
4837 and then Present (Full_View (Ancestor_Typ))
4838 and then Present (Underlying_Record_View (Full_View (Ancestor_Typ)))
4839 then
4840 Ancestor_Typ := Underlying_Record_View (Full_View (Ancestor_Typ));
4841 end if;
4843 Ancestor_Is_Subtyp :=
4844 Is_Entity_Name (Ancestor) and then Is_Type (Entity (Ancestor));
4846 -- If the ancestor part has no discriminants clearly N's aggregate
4847 -- part must provide a value for Discr.
4849 if not Has_Discriminants (Ancestor_Typ) then
4850 return True;
4852 -- If the ancestor part is an unconstrained subtype mark then the
4853 -- Discr must be present in N's aggregate part.
4855 elsif Ancestor_Is_Subtyp
4856 and then not Is_Constrained (Entity (Ancestor))
4857 then
4858 return True;
4859 end if;
4861 -- Now look to see if Discr was specified in the ancestor part
4863 if Ancestor_Is_Subtyp then
4864 Discr_Val :=
4865 First_Elmt (Discriminant_Constraint (Entity (Ancestor)));
4866 end if;
4868 Orig_Discr := Original_Record_Component (Input_Discr);
4870 Discr := First_Discriminant (Ancestor_Typ);
4871 while Present (Discr) loop
4873 -- If Ancestor has already specified Disc value then insert its
4874 -- value in the final aggregate.
4876 if Original_Record_Component (Discr) = Orig_Discr then
4877 if Ancestor_Is_Subtyp then
4878 Discr_Expr := New_Copy_Tree (Node (Discr_Val));
4879 else
4880 Discr_Expr :=
4881 Make_Selected_Component (Loc,
4882 Prefix => Duplicate_Subexpr (Ancestor),
4883 Selector_Name => New_Occurrence_Of (Input_Discr, Loc));
4884 end if;
4886 Resolve_Aggr_Expr (Discr_Expr, Input_Discr);
4887 Set_Inherited_Discriminant (Last (New_Assoc_List));
4888 return False;
4889 end if;
4891 Next_Discriminant (Discr);
4893 if Ancestor_Is_Subtyp then
4894 Next_Elmt (Discr_Val);
4895 end if;
4896 end loop;
4898 return True;
4899 end Discriminant_Present;
4901 ---------------------------
4902 -- Find_Private_Ancestor --
4903 ---------------------------
4905 function Find_Private_Ancestor (Typ : Entity_Id) return Entity_Id is
4906 Par : Entity_Id;
4908 begin
4909 Par := Typ;
4910 loop
4911 if Has_Private_Ancestor (Par)
4912 and then not Has_Private_Ancestor (Etype (Base_Type (Par)))
4913 then
4914 return Par;
4916 elsif not Is_Derived_Type (Par) then
4917 return Empty;
4919 else
4920 Par := Etype (Base_Type (Par));
4921 end if;
4922 end loop;
4923 end Find_Private_Ancestor;
4925 ---------------
4926 -- Get_Value --
4927 ---------------
4929 function Get_Value
4930 (Compon : Entity_Id;
4931 From : List_Id;
4932 Consider_Others_Choice : Boolean := False) return Node_Id
4934 Typ : constant Entity_Id := Etype (Compon);
4935 Assoc : Node_Id;
4936 Expr : Node_Id := Empty;
4937 Selector_Name : Node_Id;
4939 begin
4940 Is_Box_Present := False;
4941 Is_Box_Init_By_Default := False;
4943 if No (From) then
4944 return Empty;
4945 end if;
4947 Assoc := First (From);
4948 while Present (Assoc) loop
4949 Selector_Name := First (Choices (Assoc));
4950 while Present (Selector_Name) loop
4951 if Nkind (Selector_Name) = N_Others_Choice then
4952 if Consider_Others_Choice and then No (Expr) then
4954 -- We need to duplicate the expression for each
4955 -- successive component covered by the others choice.
4956 -- This is redundant if the others_choice covers only
4957 -- one component (small optimization possible???), but
4958 -- indispensable otherwise, because each one must be
4959 -- expanded individually to preserve side effects.
4961 -- Ada 2005 (AI-287): In case of default initialization
4962 -- of components, we duplicate the corresponding default
4963 -- expression (from the record type declaration). The
4964 -- copy must carry the sloc of the association (not the
4965 -- original expression) to prevent spurious elaboration
4966 -- checks when the default includes function calls.
4968 if Box_Present (Assoc) then
4969 Others_Box := Others_Box + 1;
4970 Is_Box_Present := True;
4972 if Expander_Active then
4973 return
4974 New_Copy_Tree_And_Copy_Dimensions
4975 (Expression (Parent (Compon)),
4976 New_Sloc => Sloc (Assoc));
4977 else
4978 return Expression (Parent (Compon));
4979 end if;
4981 else
4982 if Present (Others_Etype)
4983 and then Base_Type (Others_Etype) /= Base_Type (Typ)
4984 then
4985 -- If the components are of an anonymous access
4986 -- type they are distinct, but this is legal in
4987 -- Ada 2012 as long as designated types match.
4989 if (Ekind (Typ) = E_Anonymous_Access_Type
4990 or else Ekind (Typ) =
4991 E_Anonymous_Access_Subprogram_Type)
4992 and then Designated_Type (Typ) =
4993 Designated_Type (Others_Etype)
4994 then
4995 null;
4996 else
4997 Error_Msg_N
4998 ("components in OTHERS choice must have same "
4999 & "type", Selector_Name);
5000 end if;
5001 end if;
5003 Others_Etype := Typ;
5005 -- Copy the expression so that it is resolved
5006 -- independently for each component, This is needed
5007 -- for accessibility checks on components of anonymous
5008 -- access types, even in compile_only mode.
5010 if not Inside_A_Generic then
5011 return
5012 New_Copy_Tree_And_Copy_Dimensions
5013 (Expression (Assoc));
5014 else
5015 return Expression (Assoc);
5016 end if;
5017 end if;
5018 end if;
5020 elsif Chars (Compon) = Chars (Selector_Name) then
5021 if No (Expr) then
5023 -- Ada 2005 (AI-231)
5025 if Ada_Version >= Ada_2005
5026 and then Known_Null (Expression (Assoc))
5027 then
5028 Check_Can_Never_Be_Null (Compon, Expression (Assoc));
5029 end if;
5031 -- We need to duplicate the expression when several
5032 -- components are grouped together with a "|" choice.
5033 -- For instance "filed1 | filed2 => Expr"
5035 -- Ada 2005 (AI-287)
5037 if Box_Present (Assoc) then
5038 Is_Box_Present := True;
5040 -- Duplicate the default expression of the component
5041 -- from the record type declaration, so a new copy
5042 -- can be attached to the association.
5044 -- Note that we always copy the default expression,
5045 -- even when the association has a single choice, in
5046 -- order to create a proper association for the
5047 -- expanded aggregate.
5049 -- Component may have no default, in which case the
5050 -- expression is empty and the component is default-
5051 -- initialized, but an association for the component
5052 -- exists, and it is not covered by an others clause.
5054 -- Scalar and private types have no initialization
5055 -- procedure, so they remain uninitialized. If the
5056 -- target of the aggregate is a constant this
5057 -- deserves a warning.
5059 if No (Expression (Parent (Compon)))
5060 and then not Has_Non_Null_Base_Init_Proc (Typ)
5061 and then not Has_Aspect (Typ, Aspect_Default_Value)
5062 and then not Is_Concurrent_Type (Typ)
5063 and then Nkind (Parent (N)) = N_Object_Declaration
5064 and then Constant_Present (Parent (N))
5065 then
5066 Error_Msg_Node_2 := Typ;
5067 Error_Msg_NE
5068 ("??component& of type& is uninitialized",
5069 Assoc, Selector_Name);
5071 -- An additional reminder if the component type
5072 -- is a generic formal.
5074 if Is_Generic_Type (Base_Type (Typ)) then
5075 Error_Msg_NE
5076 ("\instance should provide actual type with "
5077 & "initialization for&", Assoc, Typ);
5078 end if;
5079 end if;
5081 return
5082 New_Copy_Tree_And_Copy_Dimensions
5083 (Expression (Parent (Compon)));
5085 else
5086 if Present (Next (Selector_Name)) then
5087 Expr := New_Copy_Tree_And_Copy_Dimensions
5088 (Expression (Assoc));
5089 else
5090 Expr := Expression (Assoc);
5091 end if;
5092 end if;
5094 Generate_Reference (Compon, Selector_Name, 'm');
5096 else
5097 Error_Msg_NE
5098 ("more than one value supplied for &",
5099 Selector_Name, Compon);
5101 end if;
5102 end if;
5104 Next (Selector_Name);
5105 end loop;
5107 Next (Assoc);
5108 end loop;
5110 return Expr;
5111 end Get_Value;
5113 -----------------------------
5114 -- Propagate_Discriminants --
5115 -----------------------------
5117 procedure Propagate_Discriminants
5118 (Aggr : Node_Id;
5119 Assoc_List : List_Id)
5121 Loc : constant Source_Ptr := Sloc (N);
5123 procedure Process_Component (Comp : Entity_Id);
5124 -- Add one component with a box association to the inner aggregate,
5125 -- and recurse if component is itself composite.
5127 -----------------------
5128 -- Process_Component --
5129 -----------------------
5131 procedure Process_Component (Comp : Entity_Id) is
5132 T : constant Entity_Id := Etype (Comp);
5133 New_Aggr : Node_Id;
5135 begin
5136 if Is_Record_Type (T) and then Has_Discriminants (T) then
5137 New_Aggr := Make_Aggregate (Loc, No_List, New_List);
5138 Set_Etype (New_Aggr, T);
5140 Add_Association
5141 (Comp, New_Aggr, Component_Associations (Aggr));
5143 -- Collect discriminant values and recurse
5145 Add_Discriminant_Values (New_Aggr, Assoc_List);
5146 Propagate_Discriminants (New_Aggr, Assoc_List);
5148 Build_Constrained_Itype
5149 (New_Aggr, T, Component_Associations (New_Aggr));
5150 else
5151 Add_Association
5152 (Comp, Empty, Component_Associations (Aggr),
5153 Is_Box_Present => True);
5154 end if;
5155 end Process_Component;
5157 -- Local variables
5159 Aggr_Type : constant Entity_Id := Base_Type (Etype (Aggr));
5160 Components : constant Elist_Id := New_Elmt_List;
5161 Def_Node : constant Node_Id :=
5162 Type_Definition (Declaration_Node (Aggr_Type));
5164 Comp : Node_Id;
5165 Comp_Elmt : Elmt_Id;
5166 Errors : Boolean;
5168 -- Start of processing for Propagate_Discriminants
5170 begin
5171 -- The component type may be a variant type. Collect the components
5172 -- that are ruled by the known values of the discriminants. Their
5173 -- values have already been inserted into the component list of the
5174 -- current aggregate.
5176 if Nkind (Def_Node) = N_Record_Definition
5177 and then Present (Component_List (Def_Node))
5178 and then Present (Variant_Part (Component_List (Def_Node)))
5179 then
5180 Gather_Components (Aggr_Type,
5181 Component_List (Def_Node),
5182 Governed_By => Component_Associations (Aggr),
5183 Into => Components,
5184 Report_Errors => Errors);
5186 Comp_Elmt := First_Elmt (Components);
5187 while Present (Comp_Elmt) loop
5188 if Ekind (Node (Comp_Elmt)) /= E_Discriminant then
5189 Process_Component (Node (Comp_Elmt));
5190 end if;
5192 Next_Elmt (Comp_Elmt);
5193 end loop;
5195 -- No variant part, iterate over all components
5197 else
5198 Comp := First_Component (Etype (Aggr));
5199 while Present (Comp) loop
5200 Process_Component (Comp);
5201 Next_Component (Comp);
5202 end loop;
5203 end if;
5204 end Propagate_Discriminants;
5206 -----------------------
5207 -- Resolve_Aggr_Expr --
5208 -----------------------
5210 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Entity_Id) is
5211 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean;
5212 -- If the expression is an aggregate (possibly qualified) then its
5213 -- expansion is delayed until the enclosing aggregate is expanded
5214 -- into assignments. In that case, do not generate checks on the
5215 -- expression, because they will be generated later, and will other-
5216 -- wise force a copy (to remove side effects) that would leave a
5217 -- dynamic-sized aggregate in the code, something that gigi cannot
5218 -- handle.
5220 ---------------------------
5221 -- Has_Expansion_Delayed --
5222 ---------------------------
5224 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean is
5225 begin
5226 return
5227 (Nkind (Expr) in N_Aggregate | N_Extension_Aggregate
5228 and then Present (Etype (Expr))
5229 and then Is_Record_Type (Etype (Expr))
5230 and then Expansion_Delayed (Expr))
5231 or else
5232 (Nkind (Expr) = N_Qualified_Expression
5233 and then Has_Expansion_Delayed (Expression (Expr)));
5234 end Has_Expansion_Delayed;
5236 -- Local variables
5238 Expr_Type : Entity_Id := Empty;
5239 New_C : Entity_Id := Component;
5240 New_Expr : Node_Id;
5242 Relocate : Boolean;
5243 -- Set to True if the resolved Expr node needs to be relocated when
5244 -- attached to the newly created association list. This node need not
5245 -- be relocated if its parent pointer is not set. In fact in this
5246 -- case Expr is the output of a New_Copy_Tree call. If Relocate is
5247 -- True then we have analyzed the expression node in the original
5248 -- aggregate and hence it needs to be relocated when moved over to
5249 -- the new association list.
5251 -- Start of processing for Resolve_Aggr_Expr
5253 begin
5254 -- If the type of the component is elementary or the type of the
5255 -- aggregate does not contain discriminants, use the type of the
5256 -- component to resolve Expr.
5258 if Is_Elementary_Type (Etype (Component))
5259 or else not Has_Discriminants (Etype (N))
5260 then
5261 Expr_Type := Etype (Component);
5263 -- Otherwise we have to pick up the new type of the component from
5264 -- the new constrained subtype of the aggregate. In fact components
5265 -- which are of a composite type might be constrained by a
5266 -- discriminant, and we want to resolve Expr against the subtype were
5267 -- all discriminant occurrences are replaced with their actual value.
5269 else
5270 New_C := First_Component (Etype (N));
5271 while Present (New_C) loop
5272 if Chars (New_C) = Chars (Component) then
5273 Expr_Type := Etype (New_C);
5274 exit;
5275 end if;
5277 Next_Component (New_C);
5278 end loop;
5280 pragma Assert (Present (Expr_Type));
5282 -- For each range in an array type where a discriminant has been
5283 -- replaced with the constraint, check that this range is within
5284 -- the range of the base type. This checks is done in the init
5285 -- proc for regular objects, but has to be done here for
5286 -- aggregates since no init proc is called for them.
5288 if Is_Array_Type (Expr_Type) then
5289 declare
5290 Index : Node_Id;
5291 -- Range of the current constrained index in the array
5293 Orig_Index : Node_Id := First_Index (Etype (Component));
5294 -- Range corresponding to the range Index above in the
5295 -- original unconstrained record type. The bounds of this
5296 -- range may be governed by discriminants.
5298 Unconstr_Index : Node_Id := First_Index (Etype (Expr_Type));
5299 -- Range corresponding to the range Index above for the
5300 -- unconstrained array type. This range is needed to apply
5301 -- range checks.
5303 begin
5304 Index := First_Index (Expr_Type);
5305 while Present (Index) loop
5306 if Depends_On_Discriminant (Orig_Index) then
5307 Apply_Range_Check (Index, Etype (Unconstr_Index));
5308 end if;
5310 Next_Index (Index);
5311 Next_Index (Orig_Index);
5312 Next_Index (Unconstr_Index);
5313 end loop;
5314 end;
5315 end if;
5316 end if;
5318 -- If the Parent pointer of Expr is not set, Expr is an expression
5319 -- duplicated by New_Tree_Copy (this happens for record aggregates
5320 -- that look like (Field1 | Filed2 => Expr) or (others => Expr)).
5321 -- Such a duplicated expression must be attached to the tree
5322 -- before analysis and resolution to enforce the rule that a tree
5323 -- fragment should never be analyzed or resolved unless it is
5324 -- attached to the current compilation unit.
5326 if No (Parent (Expr)) then
5327 Set_Parent (Expr, N);
5328 Relocate := False;
5329 else
5330 Relocate := True;
5331 end if;
5333 Analyze_And_Resolve (Expr, Expr_Type);
5334 Check_Expr_OK_In_Limited_Aggregate (Expr);
5335 Check_Non_Static_Context (Expr);
5336 Check_Unset_Reference (Expr);
5338 -- Check wrong use of class-wide types
5340 if Is_Class_Wide_Type (Etype (Expr)) then
5341 Error_Msg_N ("dynamically tagged expression not allowed", Expr);
5342 end if;
5344 if not Has_Expansion_Delayed (Expr) then
5345 Aggregate_Constraint_Checks (Expr, Expr_Type);
5346 end if;
5348 -- If an aggregate component has a type with predicates, an explicit
5349 -- predicate check must be applied, as for an assignment statement,
5350 -- because the aggregate might not be expanded into individual
5351 -- component assignments.
5353 if Has_Predicates (Expr_Type)
5354 and then Analyzed (Expr)
5355 then
5356 Apply_Predicate_Check (Expr, Expr_Type);
5357 end if;
5359 if Raises_Constraint_Error (Expr) then
5360 Set_Raises_Constraint_Error (N);
5361 end if;
5363 -- If the expression has been marked as requiring a range check, then
5364 -- generate it here. It's a bit odd to be generating such checks in
5365 -- the analyzer, but harmless since Generate_Range_Check does nothing
5366 -- (other than making sure Do_Range_Check is set) if the expander is
5367 -- not active.
5369 if Do_Range_Check (Expr) then
5370 Generate_Range_Check (Expr, Expr_Type, CE_Range_Check_Failed);
5371 end if;
5373 -- Add association Component => Expr if the caller requests it
5375 if Relocate then
5376 New_Expr := Relocate_Node (Expr);
5378 -- Since New_Expr is not gonna be analyzed later on, we need to
5379 -- propagate here the dimensions form Expr to New_Expr.
5381 Copy_Dimensions (Expr, New_Expr);
5383 else
5384 New_Expr := Expr;
5385 end if;
5387 Add_Association (New_C, New_Expr, New_Assoc_List);
5388 end Resolve_Aggr_Expr;
5390 -------------------
5391 -- Rewrite_Range --
5392 -------------------
5394 procedure Rewrite_Range (Root_Type : Entity_Id; Rge : Node_Id) is
5395 procedure Rewrite_Bound
5396 (Bound : Node_Id;
5397 Disc : Entity_Id;
5398 Expr_Disc : Node_Id);
5399 -- Rewrite a bound of the range Bound, when it is equal to the
5400 -- non-stored discriminant Disc, into the stored discriminant
5401 -- value Expr_Disc.
5403 -------------------
5404 -- Rewrite_Bound --
5405 -------------------
5407 procedure Rewrite_Bound
5408 (Bound : Node_Id;
5409 Disc : Entity_Id;
5410 Expr_Disc : Node_Id)
5412 begin
5413 if Nkind (Bound) /= N_Identifier then
5414 return;
5415 end if;
5417 -- We expect either the discriminant or the discriminal
5419 if Entity (Bound) = Disc
5420 or else (Ekind (Entity (Bound)) = E_In_Parameter
5421 and then Discriminal_Link (Entity (Bound)) = Disc)
5422 then
5423 Rewrite (Bound, New_Copy_Tree (Expr_Disc));
5424 end if;
5425 end Rewrite_Bound;
5427 -- Local variables
5429 Low, High : Node_Id;
5430 Disc : Entity_Id;
5431 Expr_Disc : Elmt_Id;
5433 -- Start of processing for Rewrite_Range
5435 begin
5436 if Has_Discriminants (Root_Type) and then Nkind (Rge) = N_Range then
5437 Low := Low_Bound (Rge);
5438 High := High_Bound (Rge);
5440 Disc := First_Discriminant (Root_Type);
5441 Expr_Disc := First_Elmt (Stored_Constraint (Etype (N)));
5442 while Present (Disc) loop
5443 Rewrite_Bound (Low, Disc, Node (Expr_Disc));
5444 Rewrite_Bound (High, Disc, Node (Expr_Disc));
5445 Next_Discriminant (Disc);
5446 Next_Elmt (Expr_Disc);
5447 end loop;
5448 end if;
5449 end Rewrite_Range;
5451 -- Local variables
5453 Components : constant Elist_Id := New_Elmt_List;
5454 -- Components is the list of the record components whose value must be
5455 -- provided in the aggregate. This list does include discriminants.
5457 Component : Entity_Id;
5458 Component_Elmt : Elmt_Id;
5459 Expr : Node_Id;
5460 Positional_Expr : Node_Id;
5462 -- Start of processing for Resolve_Record_Aggregate
5464 begin
5465 -- A record aggregate is restricted in SPARK:
5467 -- Each named association can have only a single choice.
5468 -- OTHERS cannot be used.
5469 -- Positional and named associations cannot be mixed.
5471 if Present (Component_Associations (N)) then
5472 declare
5473 Assoc : Node_Id;
5475 begin
5476 Assoc := First (Component_Associations (N));
5477 while Present (Assoc) loop
5478 if Nkind (Assoc) = N_Iterated_Component_Association then
5479 Error_Msg_N
5480 ("iterated component association can only appear in an "
5481 & "array aggregate", N);
5482 raise Unrecoverable_Error;
5483 end if;
5485 Next (Assoc);
5486 end loop;
5487 end;
5488 end if;
5490 -- We may end up calling Duplicate_Subexpr on expressions that are
5491 -- attached to New_Assoc_List. For this reason we need to attach it
5492 -- to the tree by setting its parent pointer to N. This parent point
5493 -- will change in STEP 8 below.
5495 Set_Parent (New_Assoc_List, N);
5497 -- STEP 1: abstract type and null record verification
5499 if Is_Abstract_Type (Typ) then
5500 Error_Msg_N ("type of aggregate cannot be abstract", N);
5501 end if;
5503 if No (First_Entity (Typ)) and then Null_Record_Present (N) then
5504 Set_Etype (N, Typ);
5505 return;
5507 elsif Present (First_Entity (Typ))
5508 and then Null_Record_Present (N)
5509 and then not Is_Tagged_Type (Typ)
5510 then
5511 Error_Msg_N ("record aggregate cannot be null", N);
5512 return;
5514 -- If the type has no components, then the aggregate should either
5515 -- have "null record", or in Ada 2005 it could instead have a single
5516 -- component association given by "others => <>". For Ada 95 we flag an
5517 -- error at this point, but for Ada 2005 we proceed with checking the
5518 -- associations below, which will catch the case where it's not an
5519 -- aggregate with "others => <>". Note that the legality of a <>
5520 -- aggregate for a null record type was established by AI05-016.
5522 elsif No (First_Entity (Typ))
5523 and then Ada_Version < Ada_2005
5524 then
5525 Error_Msg_N ("record aggregate must be null", N);
5526 return;
5527 end if;
5529 -- A record aggregate can only use parentheses
5531 if Nkind (N) = N_Aggregate
5532 and then Is_Homogeneous_Aggregate (N)
5533 then
5534 Error_Msg_N ("record aggregate must use (), not '[']", N);
5535 return;
5536 end if;
5538 -- STEP 2: Verify aggregate structure
5540 Step_2 : declare
5541 Assoc : Node_Id;
5542 Bad_Aggregate : Boolean := False;
5543 Selector_Name : Node_Id;
5545 begin
5546 if Present (Component_Associations (N)) then
5547 Assoc := First (Component_Associations (N));
5548 else
5549 Assoc := Empty;
5550 end if;
5552 while Present (Assoc) loop
5553 Selector_Name := First (Choices (Assoc));
5554 while Present (Selector_Name) loop
5555 if Nkind (Selector_Name) = N_Identifier then
5556 null;
5558 elsif Nkind (Selector_Name) = N_Others_Choice then
5559 if Selector_Name /= First (Choices (Assoc))
5560 or else Present (Next (Selector_Name))
5561 then
5562 Error_Msg_N
5563 ("OTHERS must appear alone in a choice list",
5564 Selector_Name);
5565 return;
5567 elsif Present (Next (Assoc)) then
5568 Error_Msg_N
5569 ("OTHERS must appear last in an aggregate",
5570 Selector_Name);
5571 return;
5573 -- (Ada 2005): If this is an association with a box,
5574 -- indicate that the association need not represent
5575 -- any component.
5577 elsif Box_Present (Assoc) then
5578 Others_Box := 1;
5579 Box_Node := Assoc;
5580 end if;
5582 else
5583 Error_Msg_N
5584 ("selector name should be identifier or OTHERS",
5585 Selector_Name);
5586 Bad_Aggregate := True;
5587 end if;
5589 Next (Selector_Name);
5590 end loop;
5592 Next (Assoc);
5593 end loop;
5595 if Bad_Aggregate then
5596 return;
5597 end if;
5598 end Step_2;
5600 -- STEP 3: Find discriminant Values
5602 Step_3 : declare
5603 Discrim : Entity_Id;
5604 Missing_Discriminants : Boolean := False;
5606 begin
5607 if Present (Expressions (N)) then
5608 Positional_Expr := First (Expressions (N));
5609 else
5610 Positional_Expr := Empty;
5611 end if;
5613 -- AI05-0115: if the ancestor part is a subtype mark, the ancestor
5614 -- must not have unknown discriminants.
5615 -- ??? We are not checking any subtype mark here and this code is not
5616 -- exercised by any test, so it's likely wrong (in particular
5617 -- we should not use Root_Type here but the subtype mark, if any),
5618 -- and possibly not needed.
5620 if Is_Derived_Type (Typ)
5621 and then Has_Unknown_Discriminants (Root_Type (Typ))
5622 and then Nkind (N) /= N_Extension_Aggregate
5623 then
5624 Error_Msg_NE
5625 ("aggregate not available for type& whose ancestor "
5626 & "has unknown discriminants", N, Typ);
5627 end if;
5629 if Has_Unknown_Discriminants (Typ)
5630 and then Present (Underlying_Record_View (Typ))
5631 then
5632 Discrim := First_Discriminant (Underlying_Record_View (Typ));
5633 elsif Has_Discriminants (Typ) then
5634 Discrim := First_Discriminant (Typ);
5635 else
5636 Discrim := Empty;
5637 end if;
5639 -- First find the discriminant values in the positional components
5641 while Present (Discrim) and then Present (Positional_Expr) loop
5642 if Discriminant_Present (Discrim) then
5643 Resolve_Aggr_Expr (Positional_Expr, Discrim);
5645 -- Ada 2005 (AI-231)
5647 if Ada_Version >= Ada_2005
5648 and then Known_Null (Positional_Expr)
5649 then
5650 Check_Can_Never_Be_Null (Discrim, Positional_Expr);
5651 end if;
5653 Next (Positional_Expr);
5654 end if;
5656 if Present (Get_Value (Discrim, Component_Associations (N))) then
5657 Error_Msg_NE
5658 ("more than one value supplied for discriminant&",
5659 N, Discrim);
5660 end if;
5662 Next_Discriminant (Discrim);
5663 end loop;
5665 -- Find remaining discriminant values if any among named components
5667 while Present (Discrim) loop
5668 Expr := Get_Value (Discrim, Component_Associations (N), True);
5670 if not Discriminant_Present (Discrim) then
5671 if Present (Expr) then
5672 Error_Msg_NE
5673 ("more than one value supplied for discriminant &",
5674 N, Discrim);
5675 end if;
5677 elsif No (Expr) then
5678 Error_Msg_NE
5679 ("no value supplied for discriminant &", N, Discrim);
5680 Missing_Discriminants := True;
5682 else
5683 Resolve_Aggr_Expr (Expr, Discrim);
5684 end if;
5686 Next_Discriminant (Discrim);
5687 end loop;
5689 if Missing_Discriminants then
5690 return;
5691 end if;
5693 -- At this point and until the beginning of STEP 6, New_Assoc_List
5694 -- contains only the discriminants and their values.
5696 end Step_3;
5698 -- STEP 4: Set the Etype of the record aggregate
5700 if Has_Discriminants (Typ)
5701 or else (Has_Unknown_Discriminants (Typ)
5702 and then Present (Underlying_Record_View (Typ)))
5703 then
5704 Build_Constrained_Itype (N, Typ, New_Assoc_List);
5705 else
5706 Set_Etype (N, Typ);
5707 end if;
5709 -- STEP 5: Get remaining components according to discriminant values
5711 Step_5 : declare
5712 Dnode : Node_Id;
5713 Errors_Found : Boolean := False;
5714 Record_Def : Node_Id;
5715 Parent_Typ : Entity_Id;
5716 Parent_Typ_List : Elist_Id;
5717 Parent_Elmt : Elmt_Id;
5718 Root_Typ : Entity_Id;
5720 begin
5721 if Is_Derived_Type (Typ) and then Is_Tagged_Type (Typ) then
5722 Parent_Typ_List := New_Elmt_List;
5724 -- If this is an extension aggregate, the component list must
5725 -- include all components that are not in the given ancestor type.
5726 -- Otherwise, the component list must include components of all
5727 -- ancestors, starting with the root.
5729 if Nkind (N) = N_Extension_Aggregate then
5730 Root_Typ := Base_Type (Etype (Ancestor_Part (N)));
5732 else
5733 -- AI05-0115: check legality of aggregate for type with a
5734 -- private ancestor.
5736 Root_Typ := Root_Type (Typ);
5737 if Has_Private_Ancestor (Typ) then
5738 declare
5739 Ancestor : constant Entity_Id :=
5740 Find_Private_Ancestor (Typ);
5741 Ancestor_Unit : constant Entity_Id :=
5742 Cunit_Entity
5743 (Get_Source_Unit (Ancestor));
5744 Parent_Unit : constant Entity_Id :=
5745 Cunit_Entity (Get_Source_Unit
5746 (Base_Type (Etype (Ancestor))));
5747 begin
5748 -- Check whether we are in a scope that has full view
5749 -- over the private ancestor and its parent. This can
5750 -- only happen if the derivation takes place in a child
5751 -- unit of the unit that declares the parent, and we are
5752 -- in the private part or body of that child unit, else
5753 -- the aggregate is illegal.
5755 if Is_Child_Unit (Ancestor_Unit)
5756 and then Scope (Ancestor_Unit) = Parent_Unit
5757 and then In_Open_Scopes (Scope (Ancestor))
5758 and then
5759 (In_Private_Part (Scope (Ancestor))
5760 or else In_Package_Body (Scope (Ancestor)))
5761 then
5762 null;
5764 else
5765 Error_Msg_NE
5766 ("type of aggregate has private ancestor&!",
5767 N, Root_Typ);
5768 Error_Msg_N ("must use extension aggregate!", N);
5769 return;
5770 end if;
5771 end;
5772 end if;
5774 Dnode := Declaration_Node (Base_Type (Root_Typ));
5776 -- If we don't get a full declaration, then we have some error
5777 -- which will get signalled later so skip this part. Otherwise
5778 -- gather components of root that apply to the aggregate type.
5779 -- We use the base type in case there is an applicable stored
5780 -- constraint that renames the discriminants of the root.
5782 if Nkind (Dnode) = N_Full_Type_Declaration then
5783 Record_Def := Type_Definition (Dnode);
5784 Gather_Components
5785 (Base_Type (Typ),
5786 Component_List (Record_Def),
5787 Governed_By => New_Assoc_List,
5788 Into => Components,
5789 Report_Errors => Errors_Found);
5791 if Errors_Found then
5792 Error_Msg_N
5793 ("discriminant controlling variant part is not static",
5795 return;
5796 end if;
5797 end if;
5798 end if;
5800 Parent_Typ := Base_Type (Typ);
5801 while Parent_Typ /= Root_Typ loop
5802 Prepend_Elmt (Parent_Typ, To => Parent_Typ_List);
5803 Parent_Typ := Etype (Parent_Typ);
5805 -- Check whether a private parent requires the use of
5806 -- an extension aggregate. This test does not apply in
5807 -- an instantiation: if the generic unit is legal so is
5808 -- the instance.
5810 if Nkind (Parent (Base_Type (Parent_Typ))) =
5811 N_Private_Type_Declaration
5812 or else Nkind (Parent (Base_Type (Parent_Typ))) =
5813 N_Private_Extension_Declaration
5814 then
5815 if Nkind (N) /= N_Extension_Aggregate
5816 and then not In_Instance
5817 then
5818 Error_Msg_NE
5819 ("type of aggregate has private ancestor&!",
5820 N, Parent_Typ);
5821 Error_Msg_N ("must use extension aggregate!", N);
5822 return;
5824 elsif Parent_Typ /= Root_Typ then
5825 Error_Msg_NE
5826 ("ancestor part of aggregate must be private type&",
5827 Ancestor_Part (N), Parent_Typ);
5828 return;
5829 end if;
5831 -- The current view of ancestor part may be a private type,
5832 -- while the context type is always non-private.
5834 elsif Is_Private_Type (Root_Typ)
5835 and then Present (Full_View (Root_Typ))
5836 and then Nkind (N) = N_Extension_Aggregate
5837 then
5838 exit when Base_Type (Full_View (Root_Typ)) = Parent_Typ;
5839 end if;
5840 end loop;
5842 -- Now collect components from all other ancestors, beginning
5843 -- with the current type. If the type has unknown discriminants
5844 -- use the component list of the Underlying_Record_View, which
5845 -- needs to be used for the subsequent expansion of the aggregate
5846 -- into assignments.
5848 Parent_Elmt := First_Elmt (Parent_Typ_List);
5849 while Present (Parent_Elmt) loop
5850 Parent_Typ := Node (Parent_Elmt);
5852 if Has_Unknown_Discriminants (Parent_Typ)
5853 and then Present (Underlying_Record_View (Typ))
5854 then
5855 Parent_Typ := Underlying_Record_View (Parent_Typ);
5856 end if;
5858 Record_Def := Type_Definition (Parent (Base_Type (Parent_Typ)));
5859 Gather_Components (Parent_Typ,
5860 Component_List (Record_Extension_Part (Record_Def)),
5861 Governed_By => New_Assoc_List,
5862 Into => Components,
5863 Report_Errors => Errors_Found);
5865 Next_Elmt (Parent_Elmt);
5866 end loop;
5868 -- Typ is not a derived tagged type
5870 else
5871 Record_Def := Type_Definition (Parent (Base_Type (Typ)));
5873 if Null_Present (Record_Def) then
5874 null;
5876 elsif not Has_Unknown_Discriminants (Typ) then
5877 Gather_Components
5878 (Base_Type (Typ),
5879 Component_List (Record_Def),
5880 Governed_By => New_Assoc_List,
5881 Into => Components,
5882 Report_Errors => Errors_Found);
5884 else
5885 Gather_Components
5886 (Base_Type (Underlying_Record_View (Typ)),
5887 Component_List (Record_Def),
5888 Governed_By => New_Assoc_List,
5889 Into => Components,
5890 Report_Errors => Errors_Found);
5891 end if;
5892 end if;
5894 if Errors_Found then
5895 return;
5896 end if;
5897 end Step_5;
5899 -- STEP 6: Find component Values
5901 Component_Elmt := First_Elmt (Components);
5903 -- First scan the remaining positional associations in the aggregate.
5904 -- Remember that at this point Positional_Expr contains the current
5905 -- positional association if any is left after looking for discriminant
5906 -- values in step 3.
5908 while Present (Positional_Expr) and then Present (Component_Elmt) loop
5909 Component := Node (Component_Elmt);
5910 Resolve_Aggr_Expr (Positional_Expr, Component);
5912 -- Ada 2005 (AI-231)
5914 if Ada_Version >= Ada_2005 and then Known_Null (Positional_Expr) then
5915 Check_Can_Never_Be_Null (Component, Positional_Expr);
5916 end if;
5918 if Present (Get_Value (Component, Component_Associations (N))) then
5919 Error_Msg_NE
5920 ("more than one value supplied for component &", N, Component);
5921 end if;
5923 Next (Positional_Expr);
5924 Next_Elmt (Component_Elmt);
5925 end loop;
5927 if Present (Positional_Expr) then
5928 Error_Msg_N
5929 ("too many components for record aggregate", Positional_Expr);
5930 end if;
5932 -- Now scan for the named arguments of the aggregate
5934 while Present (Component_Elmt) loop
5935 Component := Node (Component_Elmt);
5936 Expr := Get_Value (Component, Component_Associations (N), True);
5938 -- Note: The previous call to Get_Value sets the value of the
5939 -- variable Is_Box_Present.
5941 -- Ada 2005 (AI-287): Handle components with default initialization.
5942 -- Note: This feature was originally added to Ada 2005 for limited
5943 -- but it was finally allowed with any type.
5945 if Is_Box_Present then
5946 Check_Box_Component : declare
5947 Ctyp : constant Entity_Id := Etype (Component);
5949 begin
5950 -- Initially assume that the box is for a default-initialized
5951 -- component and reset to False in cases where that's not true.
5953 Is_Box_Init_By_Default := True;
5955 -- If there is a default expression for the aggregate, copy
5956 -- it into a new association. This copy must modify the scopes
5957 -- of internal types that may be attached to the expression
5958 -- (e.g. index subtypes of arrays) because in general the type
5959 -- declaration and the aggregate appear in different scopes,
5960 -- and the backend requires the scope of the type to match the
5961 -- point at which it is elaborated.
5963 -- If the component has an initialization procedure (IP) we
5964 -- pass the component to the expander, which will generate
5965 -- the call to such IP.
5967 -- If the component has discriminants, their values must
5968 -- be taken from their subtype. This is indispensable for
5969 -- constraints that are given by the current instance of an
5970 -- enclosing type, to allow the expansion of the aggregate to
5971 -- replace the reference to the current instance by the target
5972 -- object of the aggregate.
5974 if Is_Case_Choice_Pattern (N) then
5976 -- Do not transform box component values in a case-choice
5977 -- aggregate.
5979 Add_Association
5980 (Component => Component,
5981 Expr => Empty,
5982 Assoc_List => New_Assoc_List,
5983 Is_Box_Present => True);
5985 elsif Present (Parent (Component))
5986 and then Nkind (Parent (Component)) = N_Component_Declaration
5987 and then Present (Expression (Parent (Component)))
5988 then
5989 -- If component declaration has an initialization expression
5990 -- then this is not a case of default initialization.
5992 Is_Box_Init_By_Default := False;
5994 Expr :=
5995 New_Copy_Tree_And_Copy_Dimensions
5996 (Expression (Parent (Component)),
5997 New_Scope => Current_Scope,
5998 New_Sloc => Sloc (N));
6000 -- As the type of the copied default expression may refer
6001 -- to discriminants of the record type declaration, these
6002 -- non-stored discriminants need to be rewritten into stored
6003 -- discriminant values for the aggregate. This is required
6004 -- in GNATprove mode, and is adopted in all modes to avoid
6005 -- special-casing GNATprove mode.
6007 if Is_Array_Type (Etype (Expr)) then
6008 declare
6009 Rec_Typ : constant Entity_Id := Scope (Component);
6010 -- Root record type whose discriminants may be used as
6011 -- bounds in range nodes.
6013 Assoc : Node_Id;
6014 Choice : Node_Id;
6015 Index : Node_Id;
6017 begin
6018 -- Rewrite the range nodes occurring in the indexes
6019 -- and their types.
6021 Index := First_Index (Etype (Expr));
6022 while Present (Index) loop
6023 Rewrite_Range (Rec_Typ, Index);
6024 Rewrite_Range
6025 (Rec_Typ, Scalar_Range (Etype (Index)));
6027 Next_Index (Index);
6028 end loop;
6030 -- Rewrite the range nodes occurring as aggregate
6031 -- bounds and component associations.
6033 if Nkind (Expr) = N_Aggregate then
6034 if Present (Aggregate_Bounds (Expr)) then
6035 Rewrite_Range (Rec_Typ, Aggregate_Bounds (Expr));
6036 end if;
6038 if Present (Component_Associations (Expr)) then
6039 Assoc := First (Component_Associations (Expr));
6040 while Present (Assoc) loop
6041 Choice := First (Choices (Assoc));
6042 while Present (Choice) loop
6043 Rewrite_Range (Rec_Typ, Choice);
6045 Next (Choice);
6046 end loop;
6048 Next (Assoc);
6049 end loop;
6050 end if;
6051 end if;
6052 end;
6053 end if;
6055 Add_Association
6056 (Component => Component,
6057 Expr => Expr,
6058 Assoc_List => New_Assoc_List);
6059 Set_Has_Self_Reference (N);
6061 elsif Needs_Simple_Initialization (Ctyp) then
6062 Add_Association
6063 (Component => Component,
6064 Expr => Empty,
6065 Assoc_List => New_Assoc_List,
6066 Is_Box_Present => True);
6068 elsif Has_Non_Null_Base_Init_Proc (Ctyp)
6069 or else not Expander_Active
6070 then
6071 if Is_Record_Type (Ctyp)
6072 and then Has_Discriminants (Ctyp)
6073 and then not Is_Private_Type (Ctyp)
6074 then
6075 -- We build a partially initialized aggregate with the
6076 -- values of the discriminants and box initialization
6077 -- for the rest, if other components are present.
6079 -- The type of the aggregate is the known subtype of
6080 -- the component. The capture of discriminants must be
6081 -- recursive because subcomponents may be constrained
6082 -- (transitively) by discriminants of enclosing types.
6083 -- For a private type with discriminants, a call to the
6084 -- initialization procedure will be generated, and no
6085 -- subaggregate is needed.
6087 Capture_Discriminants : declare
6088 Loc : constant Source_Ptr := Sloc (N);
6089 Expr : Node_Id;
6091 begin
6092 Expr := Make_Aggregate (Loc, No_List, New_List);
6093 Set_Etype (Expr, Ctyp);
6095 -- If the enclosing type has discriminants, they have
6096 -- been collected in the aggregate earlier, and they
6097 -- may appear as constraints of subcomponents.
6099 -- Similarly if this component has discriminants, they
6100 -- might in turn be propagated to their components.
6102 if Has_Discriminants (Typ) then
6103 Add_Discriminant_Values (Expr, New_Assoc_List);
6104 Propagate_Discriminants (Expr, New_Assoc_List);
6106 elsif Has_Discriminants (Ctyp) then
6107 Add_Discriminant_Values
6108 (Expr, Component_Associations (Expr));
6109 Propagate_Discriminants
6110 (Expr, Component_Associations (Expr));
6112 Build_Constrained_Itype
6113 (Expr, Ctyp, Component_Associations (Expr));
6115 else
6116 declare
6117 Comp : Entity_Id;
6119 begin
6120 -- If the type has additional components, create
6121 -- an OTHERS box association for them.
6123 Comp := First_Component (Ctyp);
6124 while Present (Comp) loop
6125 if Ekind (Comp) = E_Component then
6126 if not Is_Record_Type (Etype (Comp)) then
6127 Append_To
6128 (Component_Associations (Expr),
6129 Make_Component_Association (Loc,
6130 Choices =>
6131 New_List (
6132 Make_Others_Choice (Loc)),
6133 Expression => Empty,
6134 Box_Present => True));
6135 end if;
6137 exit;
6138 end if;
6140 Next_Component (Comp);
6141 end loop;
6142 end;
6143 end if;
6145 Add_Association
6146 (Component => Component,
6147 Expr => Expr,
6148 Assoc_List => New_Assoc_List);
6149 end Capture_Discriminants;
6151 -- Otherwise the component type is not a record, or it has
6152 -- not discriminants, or it is private.
6154 else
6155 Add_Association
6156 (Component => Component,
6157 Expr => Empty,
6158 Assoc_List => New_Assoc_List,
6159 Is_Box_Present => True);
6160 end if;
6162 -- Otherwise we only need to resolve the expression if the
6163 -- component has partially initialized values (required to
6164 -- expand the corresponding assignments and run-time checks).
6166 elsif Present (Expr)
6167 and then Is_Partially_Initialized_Type (Ctyp)
6168 then
6169 Resolve_Aggr_Expr (Expr, Component);
6170 end if;
6171 end Check_Box_Component;
6173 elsif No (Expr) then
6175 -- Ignore hidden components associated with the position of the
6176 -- interface tags: these are initialized dynamically.
6178 if No (Related_Type (Component)) then
6179 Error_Msg_NE
6180 ("no value supplied for component &!", N, Component);
6181 end if;
6183 else
6184 Resolve_Aggr_Expr (Expr, Component);
6185 end if;
6187 Next_Elmt (Component_Elmt);
6188 end loop;
6190 -- STEP 7: check for invalid components + check type in choice list
6192 Step_7 : declare
6193 Assoc : Node_Id;
6194 New_Assoc : Node_Id;
6196 Selectr : Node_Id;
6197 -- Selector name
6199 Typech : Entity_Id;
6200 -- Type of first component in choice list
6202 begin
6203 if Present (Component_Associations (N)) then
6204 Assoc := First (Component_Associations (N));
6205 else
6206 Assoc := Empty;
6207 end if;
6209 Verification : while Present (Assoc) loop
6210 Selectr := First (Choices (Assoc));
6211 Typech := Empty;
6213 if Nkind (Selectr) = N_Others_Choice then
6215 -- Ada 2005 (AI-287): others choice may have expression or box
6217 if No (Others_Etype) and then Others_Box = 0 then
6218 Error_Msg_N
6219 ("OTHERS must represent at least one component", Selectr);
6221 elsif Others_Box = 1 and then Warn_On_Redundant_Constructs then
6222 Error_Msg_N ("OTHERS choice is redundant?r?", Box_Node);
6223 Error_Msg_N
6224 ("\previous choices cover all components?r?", Box_Node);
6225 end if;
6227 exit Verification;
6228 end if;
6230 while Present (Selectr) loop
6231 Component := Empty;
6232 New_Assoc := First (New_Assoc_List);
6233 while Present (New_Assoc) loop
6234 Component := First (Choices (New_Assoc));
6236 if Chars (Selectr) = Chars (Component) then
6237 if Style_Check then
6238 Check_Identifier (Selectr, Entity (Component));
6239 end if;
6241 exit;
6242 end if;
6244 Next (New_Assoc);
6245 end loop;
6247 -- If we found an association, then this is a legal component
6248 -- of the type in question.
6250 pragma Assert (if Present (New_Assoc) then Present (Component));
6252 -- If no association, this is not a legal component of the type
6253 -- in question, unless its association is provided with a box.
6255 if No (New_Assoc) then
6256 if Box_Present (Parent (Selectr)) then
6258 -- This may still be a bogus component with a box. Scan
6259 -- list of components to verify that a component with
6260 -- that name exists.
6262 declare
6263 C : Entity_Id;
6265 begin
6266 C := First_Component (Typ);
6267 while Present (C) loop
6268 if Chars (C) = Chars (Selectr) then
6270 -- If the context is an extension aggregate,
6271 -- the component must not be inherited from
6272 -- the ancestor part of the aggregate.
6274 if Nkind (N) /= N_Extension_Aggregate
6275 or else
6276 Scope (Original_Record_Component (C)) /=
6277 Etype (Ancestor_Part (N))
6278 then
6279 exit;
6280 end if;
6281 end if;
6283 Next_Component (C);
6284 end loop;
6286 if No (C) then
6287 Error_Msg_Node_2 := Typ;
6288 Error_Msg_N ("& is not a component of}", Selectr);
6289 end if;
6290 end;
6292 elsif Chars (Selectr) /= Name_uTag
6293 and then Chars (Selectr) /= Name_uParent
6294 then
6295 if not Has_Discriminants (Typ) then
6296 Error_Msg_Node_2 := Typ;
6297 Error_Msg_N ("& is not a component of}", Selectr);
6298 else
6299 Error_Msg_N
6300 ("& is not a component of the aggregate subtype",
6301 Selectr);
6302 end if;
6304 Check_Misspelled_Component (Components, Selectr);
6305 end if;
6307 elsif No (Typech) then
6308 Typech := Base_Type (Etype (Component));
6310 -- AI05-0199: In Ada 2012, several components of anonymous
6311 -- access types can appear in a choice list, as long as the
6312 -- designated types match.
6314 elsif Typech /= Base_Type (Etype (Component)) then
6315 if Ada_Version >= Ada_2012
6316 and then Ekind (Typech) = E_Anonymous_Access_Type
6317 and then
6318 Ekind (Etype (Component)) = E_Anonymous_Access_Type
6319 and then Base_Type (Designated_Type (Typech)) =
6320 Base_Type (Designated_Type (Etype (Component)))
6321 and then
6322 Subtypes_Statically_Match (Typech, (Etype (Component)))
6323 then
6324 null;
6326 elsif not Box_Present (Parent (Selectr)) then
6327 Error_Msg_N
6328 ("components in choice list must have same type",
6329 Selectr);
6330 end if;
6331 end if;
6333 Next (Selectr);
6334 end loop;
6336 Next (Assoc);
6337 end loop Verification;
6338 end Step_7;
6340 -- STEP 8: replace the original aggregate
6342 Step_8 : declare
6343 New_Aggregate : constant Node_Id := New_Copy (N);
6345 begin
6346 Set_Expressions (New_Aggregate, No_List);
6347 Set_Etype (New_Aggregate, Etype (N));
6348 Set_Component_Associations (New_Aggregate, New_Assoc_List);
6349 Set_Check_Actuals (New_Aggregate, Check_Actuals (N));
6351 Rewrite (N, New_Aggregate);
6352 end Step_8;
6354 -- Check the dimensions of the components in the record aggregate
6356 Analyze_Dimension_Extension_Or_Record_Aggregate (N);
6357 end Resolve_Record_Aggregate;
6359 -----------------------------
6360 -- Check_Can_Never_Be_Null --
6361 -----------------------------
6363 procedure Check_Can_Never_Be_Null (Typ : Entity_Id; Expr : Node_Id) is
6364 Comp_Typ : Entity_Id;
6366 begin
6367 pragma Assert
6368 (Ada_Version >= Ada_2005
6369 and then Present (Expr)
6370 and then Known_Null (Expr));
6372 case Ekind (Typ) is
6373 when E_Array_Type =>
6374 Comp_Typ := Component_Type (Typ);
6376 when E_Component
6377 | E_Discriminant
6379 Comp_Typ := Etype (Typ);
6381 when others =>
6382 return;
6383 end case;
6385 if Can_Never_Be_Null (Comp_Typ) then
6387 -- Here we know we have a constraint error. Note that we do not use
6388 -- Apply_Compile_Time_Constraint_Error here to the Expr, which might
6389 -- seem the more natural approach. That's because in some cases the
6390 -- components are rewritten, and the replacement would be missed.
6391 -- We do not mark the whole aggregate as raising a constraint error,
6392 -- because the association may be a null array range.
6394 Error_Msg_N
6395 ("(Ada 2005) NULL not allowed in null-excluding component??", Expr);
6396 Error_Msg_N
6397 ("\Constraint_Error will be raised at run time??", Expr);
6399 Rewrite (Expr,
6400 Make_Raise_Constraint_Error
6401 (Sloc (Expr), Reason => CE_Access_Check_Failed));
6402 Set_Etype (Expr, Comp_Typ);
6403 Set_Analyzed (Expr);
6404 end if;
6405 end Check_Can_Never_Be_Null;
6407 ---------------------
6408 -- Sort_Case_Table --
6409 ---------------------
6411 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is
6412 U : constant Int := Case_Table'Last;
6413 K : Int;
6414 J : Int;
6415 T : Case_Bounds;
6417 begin
6418 K := 1;
6419 while K < U loop
6420 T := Case_Table (K + 1);
6422 J := K + 1;
6423 while J > 1
6424 and then Expr_Value (Case_Table (J - 1).Lo) > Expr_Value (T.Lo)
6425 loop
6426 Case_Table (J) := Case_Table (J - 1);
6427 J := J - 1;
6428 end loop;
6430 Case_Table (J) := T;
6431 K := K + 1;
6432 end loop;
6433 end Sort_Case_Table;
6435 end Sem_Aggr;