compiler: don't generate stubs for ambiguous direct interface methods
[official-gcc.git] / gcc / ada / sem_aggr.adb
blobb85f766f82ca44f1f66d44e53206c285136df6b0
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-2022, 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.Entities; use Einfo.Entities;
31 with Einfo.Utils; use Einfo.Utils;
32 with Elists; use Elists;
33 with Errout; use Errout;
34 with Expander; use Expander;
35 with Exp_Ch6; use Exp_Ch6;
36 with Exp_Tss; use Exp_Tss;
37 with Exp_Util; use Exp_Util;
38 with Freeze; use Freeze;
39 with Itypes; use Itypes;
40 with Lib; use Lib;
41 with Lib.Xref; use Lib.Xref;
42 with Namet; use Namet;
43 with Namet.Sp; use Namet.Sp;
44 with Nmake; use Nmake;
45 with Nlists; use Nlists;
46 with Opt; use Opt;
47 with Restrict; use Restrict;
48 with Rident; use Rident;
49 with Sem; use Sem;
50 with Sem_Aux; use Sem_Aux;
51 with Sem_Case; use Sem_Case;
52 with Sem_Cat; use Sem_Cat;
53 with Sem_Ch3; use Sem_Ch3;
54 with Sem_Ch8; use Sem_Ch8;
55 with Sem_Ch13; use Sem_Ch13;
56 with Sem_Dim; use Sem_Dim;
57 with Sem_Eval; use Sem_Eval;
58 with Sem_Res; use Sem_Res;
59 with Sem_Util; use Sem_Util;
60 with Sem_Type; use Sem_Type;
61 with Sem_Warn; use Sem_Warn;
62 with Sinfo; use Sinfo;
63 with Sinfo.Nodes; use Sinfo.Nodes;
64 with Sinfo.Utils; use Sinfo.Utils;
65 with Snames; use Snames;
66 with Stringt; use Stringt;
67 with Stand; use Stand;
68 with Style; use Style;
69 with Targparm; use Targparm;
70 with Tbuild; use Tbuild;
71 with Ttypes; use Ttypes;
72 with Uintp; use Uintp;
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 function Is_Null_Aggregate (N : Node_Id) return Boolean;
408 -- Returns True for a "[]" aggregate (an Ada 2022 feature), even after
409 -- it has been transformed by expansion. Returns False otherwise.
411 procedure Make_String_Into_Aggregate (N : Node_Id);
412 -- A string literal can appear in a context in which a one dimensional
413 -- array of characters is expected. This procedure simply rewrites the
414 -- string as an aggregate, prior to resolution.
416 function Resolve_Null_Array_Aggregate (N : Node_Id) return Boolean;
417 -- For the Ada 2022 construct, build a subtype with a null range for each
418 -- dimension, using the bounds from the context subtype (if the subtype
419 -- is constrained). If the subtype is unconstrained, then the bounds
420 -- are determined in much the same way as the bounds for a null string
421 -- literal with no applicable index constraint.
422 -- Emit a check that the bounds for each dimension define a null
423 -- range; no check is emitted if it is statically known that the
424 -- check would succeed.
426 ---------------------------------
427 -- Delta aggregate processing --
428 ---------------------------------
430 procedure Resolve_Delta_Array_Aggregate (N : Node_Id; Typ : Entity_Id);
431 procedure Resolve_Delta_Record_Aggregate (N : Node_Id; Typ : Entity_Id);
433 ------------------------
434 -- Array_Aggr_Subtype --
435 ------------------------
437 function Array_Aggr_Subtype
438 (N : Node_Id;
439 Typ : Entity_Id) return Entity_Id
441 Aggr_Dimension : constant Pos := Number_Dimensions (Typ);
442 -- Number of aggregate index dimensions
444 Aggr_Range : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
445 -- Constrained N_Range of each index dimension in our aggregate itype
447 Aggr_Low : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
448 Aggr_High : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
449 -- Low and High bounds for each index dimension in our aggregate itype
451 Is_Fully_Positional : Boolean := True;
453 procedure Collect_Aggr_Bounds (N : Node_Id; Dim : Pos);
454 -- N is an array (sub-)aggregate. Dim is the dimension corresponding
455 -- to (sub-)aggregate N. This procedure collects and removes the side
456 -- effects of the constrained N_Range nodes corresponding to each index
457 -- dimension of our aggregate itype. These N_Range nodes are collected
458 -- in Aggr_Range above.
460 -- Likewise collect in Aggr_Low & Aggr_High above the low and high
461 -- bounds of each index dimension. If, when collecting, two bounds
462 -- corresponding to the same dimension are static and found to differ,
463 -- then emit a warning, and mark N as raising Constraint_Error.
465 -------------------------
466 -- Collect_Aggr_Bounds --
467 -------------------------
469 procedure Collect_Aggr_Bounds (N : Node_Id; Dim : Pos) is
470 This_Range : constant Node_Id := Aggregate_Bounds (N);
471 -- The aggregate range node of this specific sub-aggregate
473 This_Low : constant Node_Id := Low_Bound (Aggregate_Bounds (N));
474 This_High : constant Node_Id := High_Bound (Aggregate_Bounds (N));
475 -- The aggregate bounds of this specific sub-aggregate
477 Assoc : Node_Id;
478 Expr : Node_Id;
480 begin
481 Remove_Side_Effects (This_Low, Variable_Ref => True);
482 Remove_Side_Effects (This_High, Variable_Ref => True);
484 -- Collect the first N_Range for a given dimension that you find.
485 -- For a given dimension they must be all equal anyway.
487 if No (Aggr_Range (Dim)) then
488 Aggr_Low (Dim) := This_Low;
489 Aggr_High (Dim) := This_High;
490 Aggr_Range (Dim) := This_Range;
492 else
493 if Compile_Time_Known_Value (This_Low) then
494 if not Compile_Time_Known_Value (Aggr_Low (Dim)) then
495 Aggr_Low (Dim) := This_Low;
497 elsif Expr_Value (This_Low) /= Expr_Value (Aggr_Low (Dim)) then
498 Set_Raises_Constraint_Error (N);
499 Error_Msg_Warn := SPARK_Mode /= On;
500 Error_Msg_N ("sub-aggregate low bound mismatch<<", N);
501 Error_Msg_N ("\Constraint_Error [<<", N);
502 end if;
503 end if;
505 if Compile_Time_Known_Value (This_High) then
506 if not Compile_Time_Known_Value (Aggr_High (Dim)) then
507 Aggr_High (Dim) := This_High;
509 elsif
510 Expr_Value (This_High) /= Expr_Value (Aggr_High (Dim))
511 then
512 Set_Raises_Constraint_Error (N);
513 Error_Msg_Warn := SPARK_Mode /= On;
514 Error_Msg_N ("sub-aggregate high bound mismatch<<", N);
515 Error_Msg_N ("\Constraint_Error [<<", N);
516 end if;
517 end if;
518 end if;
520 if Dim < Aggr_Dimension then
522 -- Process positional components
524 if Present (Expressions (N)) then
525 Expr := First (Expressions (N));
526 while Present (Expr) loop
527 Collect_Aggr_Bounds (Expr, Dim + 1);
528 Next (Expr);
529 end loop;
530 end if;
532 -- Process component associations
534 if Present (Component_Associations (N)) then
535 Is_Fully_Positional := False;
537 Assoc := First (Component_Associations (N));
538 while Present (Assoc) loop
539 Expr := Expression (Assoc);
540 Collect_Aggr_Bounds (Expr, Dim + 1);
541 Next (Assoc);
542 end loop;
543 end if;
544 end if;
545 end Collect_Aggr_Bounds;
547 -- Array_Aggr_Subtype variables
549 Itype : Entity_Id;
550 -- The final itype of the overall aggregate
552 Index_Constraints : constant List_Id := New_List;
553 -- The list of index constraints of the aggregate itype
555 -- Start of processing for Array_Aggr_Subtype
557 begin
558 -- Make sure that the list of index constraints is properly attached to
559 -- the tree, and then collect the aggregate bounds.
561 -- If no aggregaate bounds have been set, this is an aggregate with
562 -- iterator specifications and a dynamic size to be determined by
563 -- first pass of expanded code.
565 if No (Aggregate_Bounds (N)) then
566 return Typ;
567 end if;
569 Set_Parent (Index_Constraints, N);
570 Collect_Aggr_Bounds (N, 1);
572 -- Build the list of constrained indexes of our aggregate itype
574 for J in 1 .. Aggr_Dimension loop
575 Create_Index : declare
576 Index_Base : constant Entity_Id :=
577 Base_Type (Etype (Aggr_Range (J)));
578 Index_Typ : Entity_Id;
580 begin
581 -- Construct the Index subtype, and associate it with the range
582 -- construct that generates it.
584 Index_Typ :=
585 Create_Itype (Subtype_Kind (Ekind (Index_Base)), Aggr_Range (J));
587 Set_Etype (Index_Typ, Index_Base);
589 if Is_Character_Type (Index_Base) then
590 Set_Is_Character_Type (Index_Typ);
591 end if;
593 Set_Size_Info (Index_Typ, (Index_Base));
594 Set_RM_Size (Index_Typ, RM_Size (Index_Base));
595 Set_First_Rep_Item (Index_Typ, First_Rep_Item (Index_Base));
596 Set_Scalar_Range (Index_Typ, Aggr_Range (J));
598 if Is_Discrete_Or_Fixed_Point_Type (Index_Typ) then
599 Set_RM_Size (Index_Typ, UI_From_Int (Minimum_Size (Index_Typ)));
600 end if;
602 Set_Etype (Aggr_Range (J), Index_Typ);
604 Append (Aggr_Range (J), To => Index_Constraints);
605 end Create_Index;
606 end loop;
608 -- Now build the Itype
610 Itype := Create_Itype (E_Array_Subtype, N);
612 Set_First_Rep_Item (Itype, First_Rep_Item (Typ));
613 Set_Convention (Itype, Convention (Typ));
614 Set_Depends_On_Private (Itype, Has_Private_Component (Typ));
615 Set_Etype (Itype, Base_Type (Typ));
616 Set_Has_Alignment_Clause (Itype, Has_Alignment_Clause (Typ));
617 Set_Is_Aliased (Itype, Is_Aliased (Typ));
618 Set_Is_Independent (Itype, Is_Independent (Typ));
619 Set_Depends_On_Private (Itype, Depends_On_Private (Typ));
621 Copy_Suppress_Status (Index_Check, Typ, Itype);
622 Copy_Suppress_Status (Length_Check, Typ, Itype);
624 Set_First_Index (Itype, First (Index_Constraints));
625 Set_Is_Constrained (Itype, True);
626 Set_Is_Internal (Itype, True);
628 if Has_Predicates (Typ) then
629 Set_Has_Predicates (Itype);
631 -- If the base type has a predicate, capture the predicated parent
632 -- or the existing predicate function for SPARK use.
634 if Present (Predicate_Function (Typ)) then
635 Set_Predicate_Function (Itype, Predicate_Function (Typ));
637 elsif Is_Itype (Typ) then
638 Set_Predicated_Parent (Itype, Predicated_Parent (Typ));
640 else
641 Set_Predicated_Parent (Itype, Typ);
642 end if;
643 end if;
645 -- A simple optimization: purely positional aggregates of static
646 -- components should be passed to gigi unexpanded whenever possible, and
647 -- regardless of the staticness of the bounds themselves. Subsequent
648 -- checks in exp_aggr verify that type is not packed, etc.
650 Set_Size_Known_At_Compile_Time
651 (Itype,
652 Is_Fully_Positional
653 and then Comes_From_Source (N)
654 and then Size_Known_At_Compile_Time (Component_Type (Typ)));
656 -- We always need a freeze node for a packed array subtype, so that we
657 -- can build the Packed_Array_Impl_Type corresponding to the subtype. If
658 -- expansion is disabled, the packed array subtype is not built, and we
659 -- must not generate a freeze node for the type, or else it will appear
660 -- incomplete to gigi.
662 if Is_Packed (Itype)
663 and then not In_Spec_Expression
664 and then Expander_Active
665 then
666 Freeze_Itype (Itype, N);
667 end if;
669 return Itype;
670 end Array_Aggr_Subtype;
672 --------------------------------
673 -- Check_Misspelled_Component --
674 --------------------------------
676 procedure Check_Misspelled_Component
677 (Elements : Elist_Id;
678 Component : Node_Id)
680 Max_Suggestions : constant := 2;
682 Nr_Of_Suggestions : Natural := 0;
683 Suggestion_1 : Entity_Id := Empty;
684 Suggestion_2 : Entity_Id := Empty;
685 Component_Elmt : Elmt_Id;
687 begin
688 -- All the components of List are matched against Component and a count
689 -- is maintained of possible misspellings. When at the end of the
690 -- analysis there are one or two (not more) possible misspellings,
691 -- these misspellings will be suggested as possible corrections.
693 Component_Elmt := First_Elmt (Elements);
694 while Nr_Of_Suggestions <= Max_Suggestions
695 and then Present (Component_Elmt)
696 loop
697 if Is_Bad_Spelling_Of
698 (Chars (Node (Component_Elmt)),
699 Chars (Component))
700 then
701 Nr_Of_Suggestions := Nr_Of_Suggestions + 1;
703 case Nr_Of_Suggestions is
704 when 1 => Suggestion_1 := Node (Component_Elmt);
705 when 2 => Suggestion_2 := Node (Component_Elmt);
706 when others => null;
707 end case;
708 end if;
710 Next_Elmt (Component_Elmt);
711 end loop;
713 -- Report at most two suggestions
715 if Nr_Of_Suggestions = 1 then
716 Error_Msg_NE -- CODEFIX
717 ("\possible misspelling of&", Component, Suggestion_1);
719 elsif Nr_Of_Suggestions = 2 then
720 Error_Msg_Node_2 := Suggestion_2;
721 Error_Msg_NE -- CODEFIX
722 ("\possible misspelling of& or&", Component, Suggestion_1);
723 end if;
724 end Check_Misspelled_Component;
726 ----------------------------------------
727 -- Check_Expr_OK_In_Limited_Aggregate --
728 ----------------------------------------
730 procedure Check_Expr_OK_In_Limited_Aggregate (Expr : Node_Id) is
731 begin
732 if Is_Limited_Type (Etype (Expr))
733 and then Comes_From_Source (Expr)
734 then
735 if In_Instance_Body or else In_Inlined_Body then
736 null;
738 elsif not OK_For_Limited_Init (Etype (Expr), Expr) then
739 Error_Msg_N
740 ("initialization not allowed for limited types", Expr);
741 Explain_Limited_Type (Etype (Expr), Expr);
742 end if;
743 end if;
744 end Check_Expr_OK_In_Limited_Aggregate;
746 -------------------------
747 -- Is_Others_Aggregate --
748 -------------------------
750 function Is_Others_Aggregate (Aggr : Node_Id) return Boolean is
751 Assoc : constant List_Id := Component_Associations (Aggr);
753 begin
754 return No (Expressions (Aggr))
755 and then Nkind (First (Choice_List (First (Assoc)))) = N_Others_Choice;
756 end Is_Others_Aggregate;
758 -------------------------
759 -- Is_Single_Aggregate --
760 -------------------------
762 function Is_Single_Aggregate (Aggr : Node_Id) return Boolean is
763 Assoc : constant List_Id := Component_Associations (Aggr);
765 begin
766 return No (Expressions (Aggr))
767 and then No (Next (First (Assoc)))
768 and then No (Next (First (Choice_List (First (Assoc)))));
769 end Is_Single_Aggregate;
771 -----------------------
772 -- Is_Null_Aggregate --
773 -----------------------
775 function Is_Null_Aggregate (N : Node_Id) return Boolean is
776 begin
777 return Ada_Version >= Ada_2022
778 and then Is_Homogeneous_Aggregate (N)
779 and then Is_Empty_List (Expressions (N))
780 and then Is_Empty_List (Component_Associations (N));
781 end Is_Null_Aggregate;
783 ----------------------------------------
784 -- Is_Null_Array_Aggregate_High_Bound --
785 ----------------------------------------
787 function Is_Null_Array_Aggregate_High_Bound (N : Node_Id) return Boolean is
788 Original_N : constant Node_Id := Original_Node (N);
789 begin
790 return Ada_Version >= Ada_2022
791 and then not Comes_From_Source (Original_N)
792 and then Nkind (Original_N) = N_Attribute_Reference
793 and then
794 Get_Attribute_Id (Attribute_Name (Original_N)) = Attribute_Pred
795 and then Nkind (Parent (N)) in N_Range | N_Op_Le
796 and then not Comes_From_Source (Parent (N));
797 end Is_Null_Array_Aggregate_High_Bound;
799 --------------------------------
800 -- Make_String_Into_Aggregate --
801 --------------------------------
803 procedure Make_String_Into_Aggregate (N : Node_Id) is
804 Exprs : constant List_Id := New_List;
805 Loc : constant Source_Ptr := Sloc (N);
806 Str : constant String_Id := Strval (N);
807 Strlen : constant Nat := String_Length (Str);
808 C : Char_Code;
809 C_Node : Node_Id;
810 New_N : Node_Id;
811 P : Source_Ptr;
813 begin
814 P := Loc + 1;
815 for J in 1 .. Strlen loop
816 C := Get_String_Char (Str, J);
817 Set_Character_Literal_Name (C);
819 C_Node :=
820 Make_Character_Literal (P,
821 Chars => Name_Find,
822 Char_Literal_Value => UI_From_CC (C));
823 Set_Etype (C_Node, Any_Character);
824 Append_To (Exprs, C_Node);
826 P := P + 1;
827 -- Something special for wide strings???
828 end loop;
830 New_N := Make_Aggregate (Loc, Expressions => Exprs);
831 Set_Analyzed (New_N);
832 Set_Etype (New_N, Any_Composite);
834 Rewrite (N, New_N);
835 end Make_String_Into_Aggregate;
837 -----------------------
838 -- Resolve_Aggregate --
839 -----------------------
841 procedure Resolve_Aggregate (N : Node_Id; Typ : Entity_Id) is
842 Loc : constant Source_Ptr := Sloc (N);
844 Aggr_Subtyp : Entity_Id;
845 -- The actual aggregate subtype. This is not necessarily the same as Typ
846 -- which is the subtype of the context in which the aggregate was found.
848 Others_Box : Boolean := False;
849 -- Set to True if N represents a simple aggregate with only
850 -- (others => <>), not nested as part of another aggregate.
852 function Within_Aggregate (N : Node_Id) return Boolean;
853 -- Return True if N is part of an N_Aggregate
855 ----------------------
856 -- Within_Aggregate --
857 ----------------------
859 function Within_Aggregate (N : Node_Id) return Boolean is
860 P : Node_Id := Parent (N);
861 begin
862 while Present (P) loop
863 if Nkind (P) = N_Aggregate then
864 return True;
865 end if;
867 P := Parent (P);
868 end loop;
870 return False;
871 end Within_Aggregate;
873 -- Start of processing for Resolve_Aggregate
875 begin
876 -- Ignore junk empty aggregate resulting from parser error
878 if No (Expressions (N))
879 and then No (Component_Associations (N))
880 and then not Null_Record_Present (N)
881 then
882 return;
883 end if;
885 -- If the aggregate has box-initialized components, its type must be
886 -- frozen so that initialization procedures can properly be called
887 -- in the resolution that follows. The replacement of boxes with
888 -- initialization calls is properly an expansion activity but it must
889 -- be done during resolution.
891 if Expander_Active
892 and then Present (Component_Associations (N))
893 then
894 declare
895 Comp : Node_Id;
896 First_Comp : Boolean := True;
898 begin
899 Comp := First (Component_Associations (N));
900 while Present (Comp) loop
901 if Box_Present (Comp) then
902 if First_Comp
903 and then No (Expressions (N))
904 and then Nkind (First (Choices (Comp))) = N_Others_Choice
905 and then not Within_Aggregate (N)
906 then
907 Others_Box := True;
908 end if;
910 Insert_Actions (N, Freeze_Entity (Typ, N));
911 exit;
912 end if;
914 First_Comp := False;
915 Next (Comp);
916 end loop;
917 end;
918 end if;
920 -- Check for aggregates not allowed in configurable run-time mode.
921 -- We allow all cases of aggregates that do not come from source, since
922 -- these are all assumed to be small (e.g. bounds of a string literal).
923 -- We also allow aggregates of types we know to be small.
925 if not Support_Aggregates_On_Target
926 and then Comes_From_Source (N)
927 and then (not Known_Static_Esize (Typ)
928 or else Esize (Typ) > System_Max_Integer_Size)
929 then
930 Error_Msg_CRT ("aggregate", N);
931 end if;
933 -- Ada 2005 (AI-287): Limited aggregates allowed
935 -- In an instance, ignore aggregate subcomponents that may be limited,
936 -- because they originate in view conflicts. If the original aggregate
937 -- is legal and the actuals are legal, the aggregate itself is legal.
939 if Is_Limited_Type (Typ)
940 and then Ada_Version < Ada_2005
941 and then not In_Instance
942 then
943 Error_Msg_N ("aggregate type cannot be limited", N);
944 Explain_Limited_Type (Typ, N);
946 elsif Is_Class_Wide_Type (Typ) then
947 Error_Msg_N ("type of aggregate cannot be class-wide", N);
949 elsif Typ = Any_String
950 or else Typ = Any_Composite
951 then
952 Error_Msg_N ("no unique type for aggregate", N);
953 Set_Etype (N, Any_Composite);
955 elsif Is_Array_Type (Typ) and then Null_Record_Present (N) then
956 Error_Msg_N ("null record forbidden in array aggregate", N);
958 elsif Present (Find_Aspect (Typ, Aspect_Aggregate))
959 and then Ekind (Typ) /= E_Record_Type
960 and then Ada_Version >= Ada_2022
961 then
962 -- Check for Ada 2022 and () aggregate.
964 if not Is_Homogeneous_Aggregate (N) then
965 Error_Msg_N ("container aggregate must use '['], not ()", N);
966 end if;
968 Resolve_Container_Aggregate (N, Typ);
970 elsif Is_Record_Type (Typ) then
971 Resolve_Record_Aggregate (N, Typ);
973 elsif Is_Array_Type (Typ) then
975 -- First a special test, for the case of a positional aggregate of
976 -- characters which can be replaced by a string literal.
978 -- Do not perform this transformation if this was a string literal
979 -- to start with, whose components needed constraint checks, or if
980 -- the component type is non-static, because it will require those
981 -- checks and be transformed back into an aggregate. If the index
982 -- type is not Integer the aggregate may represent a user-defined
983 -- string type but the context might need the original type so we
984 -- do not perform the transformation at this point.
986 if Number_Dimensions (Typ) = 1
987 and then Is_Standard_Character_Type (Component_Type (Typ))
988 and then No (Component_Associations (N))
989 and then not Is_Limited_Composite (Typ)
990 and then not Is_Private_Composite (Typ)
991 and then not Is_Bit_Packed_Array (Typ)
992 and then Nkind (Original_Node (Parent (N))) /= N_String_Literal
993 and then Is_OK_Static_Subtype (Component_Type (Typ))
994 and then Base_Type (Etype (First_Index (Typ))) =
995 Base_Type (Standard_Integer)
996 then
997 declare
998 Expr : Node_Id;
1000 begin
1001 Expr := First (Expressions (N));
1002 while Present (Expr) loop
1003 exit when Nkind (Expr) /= N_Character_Literal;
1004 Next (Expr);
1005 end loop;
1007 if No (Expr) then
1008 Start_String;
1010 Expr := First (Expressions (N));
1011 while Present (Expr) loop
1012 Store_String_Char (UI_To_CC (Char_Literal_Value (Expr)));
1013 Next (Expr);
1014 end loop;
1016 Rewrite (N, Make_String_Literal (Loc, End_String));
1018 Analyze_And_Resolve (N, Typ);
1019 return;
1020 end if;
1021 end;
1022 end if;
1024 -- Here if we have a real aggregate to deal with
1026 Array_Aggregate : declare
1027 Aggr_Resolved : Boolean;
1028 Aggr_Typ : constant Entity_Id := Etype (Typ);
1029 -- This is the unconstrained array type, which is the type against
1030 -- which the aggregate is to be resolved. Typ itself is the array
1031 -- type of the context which may not be the same subtype as the
1032 -- subtype for the final aggregate.
1034 Is_Null_Aggr : constant Boolean := Is_Null_Aggregate (N);
1036 begin
1037 -- In the following we determine whether an OTHERS choice is
1038 -- allowed inside the array aggregate. The test checks the context
1039 -- in which the array aggregate occurs. If the context does not
1040 -- permit it, or the aggregate type is unconstrained, an OTHERS
1041 -- choice is not allowed (except that it is always allowed on the
1042 -- right-hand side of an assignment statement; in this case the
1043 -- constrainedness of the type doesn't matter, because an array
1044 -- object is always constrained).
1046 -- If expansion is disabled (generic context, or semantics-only
1047 -- mode) actual subtypes cannot be constructed, and the type of an
1048 -- object may be its unconstrained nominal type. However, if the
1049 -- context is an assignment statement, OTHERS is allowed, because
1050 -- the target of the assignment will have a constrained subtype
1051 -- when fully compiled. Ditto if the context is an initialization
1052 -- procedure where a component may have a predicate function that
1053 -- carries the base type.
1055 -- Note that there is no node for Explicit_Actual_Parameter.
1056 -- To test for this context we therefore have to test for node
1057 -- N_Parameter_Association which itself appears only if there is a
1058 -- formal parameter. Consequently we also need to test for
1059 -- N_Procedure_Call_Statement or N_Function_Call.
1061 -- The context may be an N_Reference node, created by expansion.
1062 -- Legality of the others clause was established in the source,
1063 -- so the context is legal.
1065 Set_Etype (N, Aggr_Typ); -- May be overridden later on
1067 if Is_Null_Aggr then
1068 Set_Etype (N, Typ);
1069 Aggr_Resolved := Resolve_Null_Array_Aggregate (N);
1071 elsif Nkind (Parent (N)) = N_Assignment_Statement
1072 or else Inside_Init_Proc
1073 or else (Is_Constrained (Typ)
1074 and then Nkind (Parent (N)) in
1075 N_Parameter_Association
1076 | N_Function_Call
1077 | N_Procedure_Call_Statement
1078 | N_Generic_Association
1079 | N_Formal_Object_Declaration
1080 | N_Simple_Return_Statement
1081 | N_Object_Declaration
1082 | N_Component_Declaration
1083 | N_Parameter_Specification
1084 | N_Qualified_Expression
1085 | N_Reference
1086 | N_Aggregate
1087 | N_Extension_Aggregate
1088 | N_Component_Association
1089 | N_Case_Expression_Alternative
1090 | N_If_Expression
1091 | N_Expression_With_Actions)
1092 then
1093 Aggr_Resolved :=
1094 Resolve_Array_Aggregate
1096 Index => First_Index (Aggr_Typ),
1097 Index_Constr => First_Index (Typ),
1098 Component_Typ => Component_Type (Typ),
1099 Others_Allowed => True);
1100 else
1101 Aggr_Resolved :=
1102 Resolve_Array_Aggregate
1104 Index => First_Index (Aggr_Typ),
1105 Index_Constr => First_Index (Aggr_Typ),
1106 Component_Typ => Component_Type (Typ),
1107 Others_Allowed => False);
1108 end if;
1110 if not Aggr_Resolved then
1112 -- A parenthesized expression may have been intended as an
1113 -- aggregate, leading to a type error when analyzing the
1114 -- component. This can also happen for a nested component
1115 -- (see Analyze_Aggr_Expr).
1117 if Paren_Count (N) > 0 then
1118 Error_Msg_N
1119 ("positional aggregate cannot have one component", N);
1120 end if;
1122 Aggr_Subtyp := Any_Composite;
1124 elsif Is_Null_Aggr then
1125 Aggr_Subtyp := Etype (N);
1127 else
1128 Aggr_Subtyp := Array_Aggr_Subtype (N, Typ);
1129 end if;
1131 Set_Etype (N, Aggr_Subtyp);
1132 end Array_Aggregate;
1134 elsif Is_Private_Type (Typ)
1135 and then Present (Full_View (Typ))
1136 and then (In_Inlined_Body or In_Instance_Body)
1137 and then Is_Composite_Type (Full_View (Typ))
1138 then
1139 Resolve (N, Full_View (Typ));
1141 else
1142 Error_Msg_N ("illegal context for aggregate", N);
1143 end if;
1145 -- If we can determine statically that the evaluation of the aggregate
1146 -- raises Constraint_Error, then replace the aggregate with an
1147 -- N_Raise_Constraint_Error node, but set the Etype to the right
1148 -- aggregate subtype. Gigi needs this.
1150 if Raises_Constraint_Error (N) then
1151 Aggr_Subtyp := Etype (N);
1152 Rewrite (N,
1153 Make_Raise_Constraint_Error (Loc, Reason => CE_Range_Check_Failed));
1154 Set_Raises_Constraint_Error (N);
1155 Set_Etype (N, Aggr_Subtyp);
1156 Set_Analyzed (N);
1157 end if;
1159 if Warn_On_No_Value_Assigned
1160 and then Others_Box
1161 and then not Is_Fully_Initialized_Type (Etype (N))
1162 then
1163 Error_Msg_N ("?v?aggregate not fully initialized", N);
1164 end if;
1166 Check_Function_Writable_Actuals (N);
1167 end Resolve_Aggregate;
1169 -----------------------------
1170 -- Resolve_Array_Aggregate --
1171 -----------------------------
1173 function Resolve_Array_Aggregate
1174 (N : Node_Id;
1175 Index : Node_Id;
1176 Index_Constr : Node_Id;
1177 Component_Typ : Entity_Id;
1178 Others_Allowed : Boolean) return Boolean
1180 Loc : constant Source_Ptr := Sloc (N);
1182 Failure : constant Boolean := False;
1183 Success : constant Boolean := True;
1185 Index_Typ : constant Entity_Id := Etype (Index);
1186 Index_Typ_Low : constant Node_Id := Type_Low_Bound (Index_Typ);
1187 Index_Typ_High : constant Node_Id := Type_High_Bound (Index_Typ);
1188 -- The type of the index corresponding to the array sub-aggregate along
1189 -- with its low and upper bounds.
1191 Index_Base : constant Entity_Id := Base_Type (Index_Typ);
1192 Index_Base_Low : constant Node_Id := Type_Low_Bound (Index_Base);
1193 Index_Base_High : constant Node_Id := Type_High_Bound (Index_Base);
1194 -- Ditto for the base type
1196 Others_Present : Boolean := False;
1198 Nb_Choices : Nat := 0;
1199 -- Contains the overall number of named choices in this sub-aggregate
1201 function Add (Val : Uint; To : Node_Id) return Node_Id;
1202 -- Creates a new expression node where Val is added to expression To.
1203 -- Tries to constant fold whenever possible. To must be an already
1204 -- analyzed expression.
1206 procedure Check_Bound (BH : Node_Id; AH : in out Node_Id);
1207 -- Checks that AH (the upper bound of an array aggregate) is less than
1208 -- or equal to BH (the upper bound of the index base type). If the check
1209 -- fails, a warning is emitted, the Raises_Constraint_Error flag of N is
1210 -- set, and AH is replaced with a duplicate of BH.
1212 procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id);
1213 -- Checks that range AL .. AH is compatible with range L .. H. Emits a
1214 -- warning if not and sets the Raises_Constraint_Error flag in N.
1216 procedure Check_Length (L, H : Node_Id; Len : Uint);
1217 -- Checks that range L .. H contains at least Len elements. Emits a
1218 -- warning if not and sets the Raises_Constraint_Error flag in N.
1220 function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean;
1221 -- Returns True if range L .. H is dynamic or null
1223 procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean);
1224 -- Given expression node From, this routine sets OK to False if it
1225 -- cannot statically evaluate From. Otherwise it stores this static
1226 -- value into Value.
1228 function Resolve_Aggr_Expr
1229 (Expr : Node_Id;
1230 Single_Elmt : Boolean) return Boolean;
1231 -- Resolves aggregate expression Expr. Returns False if resolution
1232 -- fails. If Single_Elmt is set to False, the expression Expr may be
1233 -- used to initialize several array aggregate elements (this can happen
1234 -- for discrete choices such as "L .. H => Expr" or the OTHERS choice).
1235 -- In this event we do not resolve Expr unless expansion is disabled.
1236 -- To know why, see the DELAYED COMPONENT RESOLUTION note above.
1238 -- NOTE: In the case of "... => <>", we pass the in the
1239 -- N_Component_Association node as Expr, since there is no Expression in
1240 -- that case, and we need a Sloc for the error message.
1242 procedure Resolve_Iterated_Component_Association
1243 (N : Node_Id;
1244 Index_Typ : Entity_Id);
1245 -- For AI12-061
1247 ---------
1248 -- Add --
1249 ---------
1251 function Add (Val : Uint; To : Node_Id) return Node_Id is
1252 Expr_Pos : Node_Id;
1253 Expr : Node_Id;
1254 To_Pos : Node_Id;
1256 begin
1257 if Raises_Constraint_Error (To) then
1258 return To;
1259 end if;
1261 -- First test if we can do constant folding
1263 if Compile_Time_Known_Value (To)
1264 or else Nkind (To) = N_Integer_Literal
1265 then
1266 Expr_Pos := Make_Integer_Literal (Loc, Expr_Value (To) + Val);
1267 Set_Is_Static_Expression (Expr_Pos);
1268 Set_Etype (Expr_Pos, Etype (To));
1269 Set_Analyzed (Expr_Pos, Analyzed (To));
1271 if not Is_Enumeration_Type (Index_Typ) then
1272 Expr := Expr_Pos;
1274 -- If we are dealing with enumeration return
1275 -- Index_Typ'Val (Expr_Pos)
1277 else
1278 Expr :=
1279 Make_Attribute_Reference
1280 (Loc,
1281 Prefix => New_Occurrence_Of (Index_Typ, Loc),
1282 Attribute_Name => Name_Val,
1283 Expressions => New_List (Expr_Pos));
1284 end if;
1286 return Expr;
1287 end if;
1289 -- If we are here no constant folding possible
1291 if not Is_Enumeration_Type (Index_Base) then
1292 Expr :=
1293 Make_Op_Add (Loc,
1294 Left_Opnd => Duplicate_Subexpr (To),
1295 Right_Opnd => Make_Integer_Literal (Loc, Val));
1297 -- If we are dealing with enumeration return
1298 -- Index_Typ'Val (Index_Typ'Pos (To) + Val)
1300 else
1301 To_Pos :=
1302 Make_Attribute_Reference
1303 (Loc,
1304 Prefix => New_Occurrence_Of (Index_Typ, Loc),
1305 Attribute_Name => Name_Pos,
1306 Expressions => New_List (Duplicate_Subexpr (To)));
1308 Expr_Pos :=
1309 Make_Op_Add (Loc,
1310 Left_Opnd => To_Pos,
1311 Right_Opnd => Make_Integer_Literal (Loc, Val));
1313 Expr :=
1314 Make_Attribute_Reference
1315 (Loc,
1316 Prefix => New_Occurrence_Of (Index_Typ, Loc),
1317 Attribute_Name => Name_Val,
1318 Expressions => New_List (Expr_Pos));
1320 -- If the index type has a non standard representation, the
1321 -- attributes 'Val and 'Pos expand into function calls and the
1322 -- resulting expression is considered non-safe for reevaluation
1323 -- by the backend. Relocate it into a constant temporary in order
1324 -- to make it safe for reevaluation.
1326 if Has_Non_Standard_Rep (Etype (N)) then
1327 declare
1328 Def_Id : Entity_Id;
1330 begin
1331 Def_Id := Make_Temporary (Loc, 'R', Expr);
1332 Set_Etype (Def_Id, Index_Typ);
1333 Insert_Action (N,
1334 Make_Object_Declaration (Loc,
1335 Defining_Identifier => Def_Id,
1336 Object_Definition =>
1337 New_Occurrence_Of (Index_Typ, Loc),
1338 Constant_Present => True,
1339 Expression => Relocate_Node (Expr)));
1341 Expr := New_Occurrence_Of (Def_Id, Loc);
1342 end;
1343 end if;
1344 end if;
1346 return Expr;
1347 end Add;
1349 -----------------
1350 -- Check_Bound --
1351 -----------------
1353 procedure Check_Bound (BH : Node_Id; AH : in out Node_Id) is
1354 Val_BH : Uint;
1355 Val_AH : Uint;
1357 OK_BH : Boolean;
1358 OK_AH : Boolean;
1360 begin
1361 Get (Value => Val_BH, From => BH, OK => OK_BH);
1362 Get (Value => Val_AH, From => AH, OK => OK_AH);
1364 if OK_BH and then OK_AH and then Val_BH < Val_AH then
1365 Set_Raises_Constraint_Error (N);
1366 Error_Msg_Warn := SPARK_Mode /= On;
1367 Error_Msg_N ("upper bound out of range<<", AH);
1368 Error_Msg_N ("\Constraint_Error [<<", AH);
1370 -- You need to set AH to BH or else in the case of enumerations
1371 -- indexes we will not be able to resolve the aggregate bounds.
1373 AH := Duplicate_Subexpr (BH);
1374 end if;
1375 end Check_Bound;
1377 ------------------
1378 -- Check_Bounds --
1379 ------------------
1381 procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id) is
1382 Val_L : Uint;
1383 Val_H : Uint;
1384 Val_AL : Uint;
1385 Val_AH : Uint;
1387 OK_L : Boolean;
1388 OK_H : Boolean;
1390 OK_AL : Boolean;
1391 OK_AH : Boolean;
1392 pragma Warnings (Off, OK_AL);
1393 pragma Warnings (Off, OK_AH);
1395 begin
1396 if Raises_Constraint_Error (N)
1397 or else Dynamic_Or_Null_Range (AL, AH)
1398 then
1399 return;
1400 end if;
1402 Get (Value => Val_L, From => L, OK => OK_L);
1403 Get (Value => Val_H, From => H, OK => OK_H);
1405 Get (Value => Val_AL, From => AL, OK => OK_AL);
1406 Get (Value => Val_AH, From => AH, OK => OK_AH);
1408 if OK_L and then Val_L > Val_AL then
1409 Set_Raises_Constraint_Error (N);
1410 Error_Msg_Warn := SPARK_Mode /= On;
1411 Error_Msg_N ("lower bound of aggregate out of range<<", N);
1412 Error_Msg_N ("\Constraint_Error [<<", N);
1413 end if;
1415 if OK_H and then Val_H < Val_AH then
1416 Set_Raises_Constraint_Error (N);
1417 Error_Msg_Warn := SPARK_Mode /= On;
1418 Error_Msg_N ("upper bound of aggregate out of range<<", N);
1419 Error_Msg_N ("\Constraint_Error [<<", N);
1420 end if;
1421 end Check_Bounds;
1423 ------------------
1424 -- Check_Length --
1425 ------------------
1427 procedure Check_Length (L, H : Node_Id; Len : Uint) is
1428 Val_L : Uint;
1429 Val_H : Uint;
1431 OK_L : Boolean;
1432 OK_H : Boolean;
1434 Range_Len : Uint;
1436 begin
1437 if Raises_Constraint_Error (N) then
1438 return;
1439 end if;
1441 Get (Value => Val_L, From => L, OK => OK_L);
1442 Get (Value => Val_H, From => H, OK => OK_H);
1444 if not OK_L or else not OK_H then
1445 return;
1446 end if;
1448 -- If null range length is zero
1450 if Val_L > Val_H then
1451 Range_Len := Uint_0;
1452 else
1453 Range_Len := Val_H - Val_L + 1;
1454 end if;
1456 if Range_Len < Len then
1457 Set_Raises_Constraint_Error (N);
1458 Error_Msg_Warn := SPARK_Mode /= On;
1459 Error_Msg_N ("too many elements<<", N);
1460 Error_Msg_N ("\Constraint_Error [<<", N);
1461 end if;
1462 end Check_Length;
1464 ---------------------------
1465 -- Dynamic_Or_Null_Range --
1466 ---------------------------
1468 function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean is
1469 Val_L : Uint;
1470 Val_H : Uint;
1472 OK_L : Boolean;
1473 OK_H : Boolean;
1475 begin
1476 Get (Value => Val_L, From => L, OK => OK_L);
1477 Get (Value => Val_H, From => H, OK => OK_H);
1479 return not OK_L or else not OK_H
1480 or else not Is_OK_Static_Expression (L)
1481 or else not Is_OK_Static_Expression (H)
1482 or else Val_L > Val_H;
1483 end Dynamic_Or_Null_Range;
1485 ---------
1486 -- Get --
1487 ---------
1489 procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean) is
1490 begin
1491 OK := True;
1493 if Compile_Time_Known_Value (From) then
1494 Value := Expr_Value (From);
1496 -- If expression From is something like Some_Type'Val (10) then
1497 -- Value = 10.
1499 elsif Nkind (From) = N_Attribute_Reference
1500 and then Attribute_Name (From) = Name_Val
1501 and then Compile_Time_Known_Value (First (Expressions (From)))
1502 then
1503 Value := Expr_Value (First (Expressions (From)));
1504 else
1505 Value := Uint_0;
1506 OK := False;
1507 end if;
1508 end Get;
1510 -----------------------
1511 -- Resolve_Aggr_Expr --
1512 -----------------------
1514 function Resolve_Aggr_Expr
1515 (Expr : Node_Id;
1516 Single_Elmt : Boolean) return Boolean
1518 Nxt_Ind : constant Node_Id := Next_Index (Index);
1519 Nxt_Ind_Constr : constant Node_Id := Next_Index (Index_Constr);
1520 -- Index is the current index corresponding to the expression
1522 Resolution_OK : Boolean := True;
1523 -- Set to False if resolution of the expression failed
1525 begin
1526 -- Defend against previous errors
1528 if Nkind (Expr) = N_Error
1529 or else Error_Posted (Expr)
1530 then
1531 return True;
1532 end if;
1534 -- If the array type against which we are resolving the aggregate
1535 -- has several dimensions, the expressions nested inside the
1536 -- aggregate must be further aggregates (or strings).
1538 if Present (Nxt_Ind) then
1539 if Nkind (Expr) /= N_Aggregate then
1541 -- A string literal can appear where a one-dimensional array
1542 -- of characters is expected. If the literal looks like an
1543 -- operator, it is still an operator symbol, which will be
1544 -- transformed into a string when analyzed.
1546 if Is_Character_Type (Component_Typ)
1547 and then No (Next_Index (Nxt_Ind))
1548 and then Nkind (Expr) in N_String_Literal | N_Operator_Symbol
1549 then
1550 -- A string literal used in a multidimensional array
1551 -- aggregate in place of the final one-dimensional
1552 -- aggregate must not be enclosed in parentheses.
1554 if Paren_Count (Expr) /= 0 then
1555 Error_Msg_N ("no parenthesis allowed here", Expr);
1556 end if;
1558 Make_String_Into_Aggregate (Expr);
1560 else
1561 Error_Msg_N ("nested array aggregate expected", Expr);
1563 -- If the expression is parenthesized, this may be
1564 -- a missing component association for a 1-aggregate.
1566 if Paren_Count (Expr) > 0 then
1567 Error_Msg_N
1568 ("\if single-component aggregate is intended, "
1569 & "write e.g. (1 ='> ...)", Expr);
1570 end if;
1572 return Failure;
1573 end if;
1574 end if;
1576 -- If it's "... => <>", nothing to resolve
1578 if Nkind (Expr) = N_Component_Association then
1579 pragma Assert (Box_Present (Expr));
1580 return Success;
1581 end if;
1583 -- Ada 2005 (AI-231): Propagate the type to the nested aggregate.
1584 -- Required to check the null-exclusion attribute (if present).
1585 -- This value may be overridden later on.
1587 Set_Etype (Expr, Etype (N));
1589 Resolution_OK := Resolve_Array_Aggregate
1590 (Expr, Nxt_Ind, Nxt_Ind_Constr, Component_Typ, Others_Allowed);
1592 else
1593 -- If it's "... => <>", nothing to resolve
1595 if Nkind (Expr) = N_Component_Association then
1596 pragma Assert (Box_Present (Expr));
1597 return Success;
1598 end if;
1600 -- Do not resolve the expressions of discrete or others choices
1601 -- unless the expression covers a single component, or the
1602 -- expander is inactive.
1604 -- In SPARK mode, expressions that can perform side effects will
1605 -- be recognized by the gnat2why back-end, and the whole
1606 -- subprogram will be ignored. So semantic analysis can be
1607 -- performed safely.
1609 if Single_Elmt
1610 or else not Expander_Active
1611 or else In_Spec_Expression
1612 then
1613 Analyze_And_Resolve (Expr, Component_Typ);
1614 Check_Expr_OK_In_Limited_Aggregate (Expr);
1615 Check_Non_Static_Context (Expr);
1616 Aggregate_Constraint_Checks (Expr, Component_Typ);
1617 Check_Unset_Reference (Expr);
1618 end if;
1619 end if;
1621 -- If an aggregate component has a type with predicates, an explicit
1622 -- predicate check must be applied, as for an assignment statement,
1623 -- because the aggregate might not be expanded into individual
1624 -- component assignments. If the expression covers several components
1625 -- the analysis and the predicate check take place later.
1627 if Has_Predicates (Component_Typ)
1628 and then Analyzed (Expr)
1629 then
1630 Apply_Predicate_Check (Expr, Component_Typ);
1631 end if;
1633 if Raises_Constraint_Error (Expr)
1634 and then Nkind (Parent (Expr)) /= N_Component_Association
1635 then
1636 Set_Raises_Constraint_Error (N);
1637 end if;
1639 -- If the expression has been marked as requiring a range check,
1640 -- then generate it here. It's a bit odd to be generating such
1641 -- checks in the analyzer, but harmless since Generate_Range_Check
1642 -- does nothing (other than making sure Do_Range_Check is set) if
1643 -- the expander is not active.
1645 if Do_Range_Check (Expr) then
1646 Generate_Range_Check (Expr, Component_Typ, CE_Range_Check_Failed);
1647 end if;
1649 return Resolution_OK;
1650 end Resolve_Aggr_Expr;
1652 --------------------------------------------
1653 -- Resolve_Iterated_Component_Association --
1654 --------------------------------------------
1656 procedure Resolve_Iterated_Component_Association
1657 (N : Node_Id;
1658 Index_Typ : Entity_Id)
1660 Loc : constant Source_Ptr := Sloc (N);
1661 Id : constant Entity_Id := Defining_Identifier (N);
1663 Id_Typ : Entity_Id := Any_Type;
1665 -----------------------
1666 -- Remove_References --
1667 -----------------------
1669 function Remove_Ref (N : Node_Id) return Traverse_Result;
1670 -- Remove references to the entity Id after analysis, so it can be
1671 -- properly reanalyzed after construct is expanded into a loop.
1673 function Remove_Ref (N : Node_Id) return Traverse_Result is
1674 begin
1675 if Nkind (N) = N_Identifier
1676 and then Present (Entity (N))
1677 and then Entity (N) = Id
1678 then
1679 Set_Entity (N, Empty);
1680 Set_Etype (N, Empty);
1681 end if;
1682 Set_Analyzed (N, False);
1683 return OK;
1684 end Remove_Ref;
1686 procedure Remove_References is new Traverse_Proc (Remove_Ref);
1688 -- Local variables
1690 Choice : Node_Id;
1691 Dummy : Boolean;
1692 Ent : Entity_Id;
1693 Expr : Node_Id;
1695 -- Start of processing for Resolve_Iterated_Component_Association
1697 begin
1698 Error_Msg_Ada_2022_Feature ("iterated component", Loc);
1700 if Present (Iterator_Specification (N)) then
1701 Analyze (Name (Iterator_Specification (N)));
1703 -- We assume that the domain of iteration cannot be overloaded.
1705 declare
1706 Domain : constant Node_Id := Name (Iterator_Specification (N));
1707 D_Type : constant Entity_Id := Etype (Domain);
1708 Elt : Entity_Id;
1709 begin
1710 if Is_Array_Type (D_Type) then
1711 Id_Typ := Component_Type (D_Type);
1713 else
1714 if Has_Aspect (D_Type, Aspect_Iterable) then
1715 Elt :=
1716 Get_Iterable_Type_Primitive (D_Type, Name_Element);
1717 if No (Elt) then
1718 Error_Msg_N
1719 ("missing Element primitive for iteration", Domain);
1720 else
1721 Id_Typ := Etype (Elt);
1722 end if;
1723 else
1724 Error_Msg_N ("cannot iterate over", Domain);
1725 end if;
1726 end if;
1727 end;
1729 else
1730 Id_Typ := Index_Typ;
1731 Choice := First (Discrete_Choices (N));
1733 while Present (Choice) loop
1734 if Nkind (Choice) = N_Others_Choice then
1735 Others_Present := True;
1737 else
1738 Analyze (Choice);
1740 -- Choice can be a subtype name, a range, or an expression
1742 if Is_Entity_Name (Choice)
1743 and then Is_Type (Entity (Choice))
1744 and then
1745 Base_Type (Entity (Choice)) = Base_Type (Index_Typ)
1746 then
1747 null;
1749 else
1750 Analyze_And_Resolve (Choice, Index_Typ);
1751 end if;
1752 end if;
1754 Next (Choice);
1755 end loop;
1756 end if;
1758 -- Create a scope in which to introduce an index, which is usually
1759 -- visible in the expression for the component, and needed for its
1760 -- analysis.
1762 Ent := New_Internal_Entity (E_Loop, Current_Scope, Loc, 'L');
1763 Set_Etype (Ent, Standard_Void_Type);
1764 Set_Parent (Ent, Parent (N));
1765 Push_Scope (Ent);
1767 -- Insert and decorate the index variable in the current scope.
1768 -- The expression has to be analyzed once the index variable is
1769 -- directly visible.
1771 Enter_Name (Id);
1772 Set_Etype (Id, Id_Typ);
1773 Mutate_Ekind (Id, E_Variable);
1774 Set_Scope (Id, Ent);
1776 -- Analyze expression without expansion, to verify legality.
1777 -- When generating code, we then remove references to the index
1778 -- variable, because the expression will be analyzed anew after
1779 -- rewritting as a loop with a new index variable; when not
1780 -- generating code we leave the analyzed expression as it is.
1782 Expr := Expression (N);
1784 Expander_Mode_Save_And_Set (False);
1785 Dummy := Resolve_Aggr_Expr (Expr, Single_Elmt => False);
1786 Expander_Mode_Restore;
1788 if Operating_Mode /= Check_Semantics then
1789 Remove_References (Expr);
1790 end if;
1792 -- An iterated_component_association may appear in a nested
1793 -- aggregate for a multidimensional structure: preserve the bounds
1794 -- computed for the expression, as well as the anonymous array
1795 -- type generated for it; both are needed during array expansion.
1797 if Nkind (Expr) = N_Aggregate then
1798 Set_Aggregate_Bounds (Expression (N), Aggregate_Bounds (Expr));
1799 Set_Etype (Expression (N), Etype (Expr));
1800 end if;
1802 End_Scope;
1803 end Resolve_Iterated_Component_Association;
1805 -- Local variables
1807 Assoc : Node_Id;
1808 Choice : Node_Id;
1809 Expr : Node_Id;
1810 Discard : Node_Id;
1812 Aggr_Low : Node_Id := Empty;
1813 Aggr_High : Node_Id := Empty;
1814 -- The actual low and high bounds of this sub-aggregate
1816 Case_Table_Size : Nat;
1817 -- Contains the size of the case table needed to sort aggregate choices
1819 Choices_Low : Node_Id := Empty;
1820 Choices_High : Node_Id := Empty;
1821 -- The lowest and highest discrete choices values for a named aggregate
1823 Delete_Choice : Boolean;
1824 -- Used when replacing a subtype choice with predicate by a list
1826 Has_Iterator_Specifications : Boolean := False;
1827 -- Flag to indicate that all named associations are iterated component
1828 -- associations with iterator specifications, in which case the
1829 -- expansion will create two loops: one to evaluate the size and one
1830 -- to generate the elements (4.3.3 (20.2/5)).
1832 Nb_Elements : Uint := Uint_0;
1833 -- The number of elements in a positional aggregate
1835 Nb_Discrete_Choices : Nat := 0;
1836 -- The overall number of discrete choices (not counting others choice)
1838 -- Start of processing for Resolve_Array_Aggregate
1840 begin
1841 -- Ignore junk empty aggregate resulting from parser error
1843 if No (Expressions (N))
1844 and then No (Component_Associations (N))
1845 and then not Null_Record_Present (N)
1846 then
1847 return False;
1848 end if;
1850 -- Disable the warning for GNAT Mode to allow for easier transition.
1852 if Ada_Version >= Ada_2022
1853 and then Warn_On_Obsolescent_Feature
1854 and then not GNAT_Mode
1855 and then not Is_Homogeneous_Aggregate (N)
1856 and then not Is_Enum_Array_Aggregate (N)
1857 and then Is_Parenthesis_Aggregate (N)
1858 and then Nkind (Parent (N)) /= N_Qualified_Expression
1859 and then Comes_From_Source (N)
1860 then
1861 Error_Msg_N
1862 ("?j?array aggregate using () is an" &
1863 " obsolescent syntax, use '['] instead", N);
1864 end if;
1866 -- STEP 1: make sure the aggregate is correctly formatted
1868 if Present (Component_Associations (N)) then
1870 -- Verify that all or none of the component associations
1871 -- include an iterator specification.
1873 Assoc := First (Component_Associations (N));
1874 if Nkind (Assoc) = N_Iterated_Component_Association
1875 and then Present (Iterator_Specification (Assoc))
1876 then
1877 -- All other component associations must have an iterator spec.
1879 Next (Assoc);
1880 while Present (Assoc) loop
1881 if Nkind (Assoc) /= N_Iterated_Component_Association
1882 or else No (Iterator_Specification (Assoc))
1883 then
1884 Error_Msg_N ("mixed iterated component association"
1885 & " (RM 4.3.3 (17.1/5))",
1886 Assoc);
1887 return False;
1888 end if;
1890 Next (Assoc);
1891 end loop;
1893 Has_Iterator_Specifications := True;
1895 else
1896 -- or none of them do.
1898 Next (Assoc);
1899 while Present (Assoc) loop
1900 if Nkind (Assoc) = N_Iterated_Component_Association
1901 and then Present (Iterator_Specification (Assoc))
1902 then
1903 Error_Msg_N ("mixed iterated component association"
1904 & " (RM 4.3.3 (17.1/5))",
1905 Assoc);
1906 return False;
1907 end if;
1909 Next (Assoc);
1910 end loop;
1912 end if;
1914 Assoc := First (Component_Associations (N));
1915 while Present (Assoc) loop
1916 if Nkind (Assoc) = N_Iterated_Component_Association then
1917 Resolve_Iterated_Component_Association (Assoc, Index_Typ);
1918 end if;
1920 Choice := First (Choice_List (Assoc));
1921 Delete_Choice := False;
1922 while Present (Choice) loop
1923 if Nkind (Choice) = N_Others_Choice then
1924 Others_Present := True;
1926 if Choice /= First (Choice_List (Assoc))
1927 or else Present (Next (Choice))
1928 then
1929 Error_Msg_N
1930 ("OTHERS must appear alone in a choice list", Choice);
1931 return Failure;
1932 end if;
1934 if Present (Next (Assoc)) then
1935 Error_Msg_N
1936 ("OTHERS must appear last in an aggregate", Choice);
1937 return Failure;
1938 end if;
1940 if Ada_Version = Ada_83
1941 and then Assoc /= First (Component_Associations (N))
1942 and then Nkind (Parent (N)) in
1943 N_Assignment_Statement | N_Object_Declaration
1944 then
1945 Error_Msg_N
1946 ("(Ada 83) illegal context for OTHERS choice", N);
1947 end if;
1949 elsif Is_Entity_Name (Choice) then
1950 Analyze (Choice);
1952 declare
1953 E : constant Entity_Id := Entity (Choice);
1954 New_Cs : List_Id;
1955 P : Node_Id;
1956 C : Node_Id;
1958 begin
1959 if Is_Type (E) and then Has_Predicates (E) then
1960 Freeze_Before (N, E);
1962 if Has_Dynamic_Predicate_Aspect (E) then
1963 Error_Msg_NE
1964 ("subtype& has dynamic predicate, not allowed "
1965 & "in aggregate choice", Choice, E);
1967 elsif not Is_OK_Static_Subtype (E) then
1968 Error_Msg_NE
1969 ("non-static subtype& has predicate, not allowed "
1970 & "in aggregate choice", Choice, E);
1971 end if;
1973 -- If the subtype has a static predicate, replace the
1974 -- original choice with the list of individual values
1975 -- covered by the predicate.
1976 -- This should be deferred to expansion time ???
1978 if Present (Static_Discrete_Predicate (E)) then
1979 Delete_Choice := True;
1981 New_Cs := New_List;
1982 P := First (Static_Discrete_Predicate (E));
1983 while Present (P) loop
1984 C := New_Copy (P);
1985 Set_Sloc (C, Sloc (Choice));
1986 Append_To (New_Cs, C);
1987 Next (P);
1988 end loop;
1990 Insert_List_After (Choice, New_Cs);
1991 end if;
1992 end if;
1993 end;
1994 end if;
1996 Nb_Choices := Nb_Choices + 1;
1998 declare
1999 C : constant Node_Id := Choice;
2001 begin
2002 Next (Choice);
2004 if Delete_Choice then
2005 Remove (C);
2006 Nb_Choices := Nb_Choices - 1;
2007 Delete_Choice := False;
2008 end if;
2009 end;
2010 end loop;
2012 Next (Assoc);
2013 end loop;
2014 end if;
2016 -- At this point we know that the others choice, if present, is by
2017 -- itself and appears last in the aggregate. Check if we have mixed
2018 -- positional and discrete associations (other than the others choice).
2020 if Present (Expressions (N))
2021 and then (Nb_Choices > 1
2022 or else (Nb_Choices = 1 and then not Others_Present))
2023 then
2024 Error_Msg_N
2025 ("cannot mix named and positional associations in array aggregate",
2026 First (Choice_List (First (Component_Associations (N)))));
2027 return Failure;
2028 end if;
2030 -- Test for the validity of an others choice if present
2032 if Others_Present and then not Others_Allowed then
2033 declare
2034 Others_N : constant Node_Id :=
2035 First (Choice_List (First (Component_Associations (N))));
2036 begin
2037 Error_Msg_N ("OTHERS choice not allowed here", Others_N);
2038 Error_Msg_N ("\qualify the aggregate with a constrained subtype "
2039 & "to provide bounds for it", Others_N);
2040 return Failure;
2041 end;
2042 end if;
2044 -- Protect against cascaded errors
2046 if Etype (Index_Typ) = Any_Type then
2047 return Failure;
2048 end if;
2050 -- STEP 2: Process named components
2052 if No (Expressions (N)) then
2053 if Others_Present then
2054 Case_Table_Size := Nb_Choices - 1;
2055 else
2056 Case_Table_Size := Nb_Choices;
2057 end if;
2059 Step_2 : declare
2060 function Empty_Range (A : Node_Id) return Boolean;
2061 -- If an association covers an empty range, some warnings on the
2062 -- expression of the association can be disabled.
2064 -----------------
2065 -- Empty_Range --
2066 -----------------
2068 function Empty_Range (A : Node_Id) return Boolean is
2069 R : constant Node_Id := First (Choices (A));
2070 begin
2071 return No (Next (R))
2072 and then Nkind (R) = N_Range
2073 and then Compile_Time_Compare
2074 (Low_Bound (R), High_Bound (R), False) = GT;
2075 end Empty_Range;
2077 -- Local variables
2079 Low : Node_Id;
2080 High : Node_Id;
2081 -- Denote the lowest and highest values in an aggregate choice
2083 S_Low : Node_Id := Empty;
2084 S_High : Node_Id := Empty;
2085 -- if a choice in an aggregate is a subtype indication these
2086 -- denote the lowest and highest values of the subtype
2088 Table : Case_Table_Type (1 .. Case_Table_Size);
2089 -- Used to sort all the different choice values
2091 Single_Choice : Boolean;
2092 -- Set to true every time there is a single discrete choice in a
2093 -- discrete association
2095 Prev_Nb_Discrete_Choices : Nat;
2096 -- Used to keep track of the number of discrete choices in the
2097 -- current association.
2099 Errors_Posted_On_Choices : Boolean := False;
2100 -- Keeps track of whether any choices have semantic errors
2102 -- Start of processing for Step_2
2104 begin
2105 -- STEP 2 (A): Check discrete choices validity
2106 -- No need if this is an element iteration.
2108 Assoc := First (Component_Associations (N));
2109 while Present (Assoc)
2110 and then Present (Choice_List (Assoc))
2111 loop
2112 Prev_Nb_Discrete_Choices := Nb_Discrete_Choices;
2113 Choice := First (Choice_List (Assoc));
2115 loop
2116 Analyze (Choice);
2118 if Nkind (Choice) = N_Others_Choice then
2119 Single_Choice := False;
2120 exit;
2122 -- Test for subtype mark without constraint
2124 elsif Is_Entity_Name (Choice) and then
2125 Is_Type (Entity (Choice))
2126 then
2127 if Base_Type (Entity (Choice)) /= Index_Base then
2128 Error_Msg_N
2129 ("invalid subtype mark in aggregate choice",
2130 Choice);
2131 return Failure;
2132 end if;
2134 -- Case of subtype indication
2136 elsif Nkind (Choice) = N_Subtype_Indication then
2137 Resolve_Discrete_Subtype_Indication (Choice, Index_Base);
2139 if Has_Dynamic_Predicate_Aspect
2140 (Entity (Subtype_Mark (Choice)))
2141 then
2142 Error_Msg_NE
2143 ("subtype& has dynamic predicate, "
2144 & "not allowed in aggregate choice",
2145 Choice, Entity (Subtype_Mark (Choice)));
2146 end if;
2148 -- Does the subtype indication evaluation raise CE?
2150 Get_Index_Bounds (Subtype_Mark (Choice), S_Low, S_High);
2151 Get_Index_Bounds (Choice, Low, High);
2152 Check_Bounds (S_Low, S_High, Low, High);
2154 -- Case of range or expression
2156 else
2157 Resolve (Choice, Index_Base);
2158 Check_Unset_Reference (Choice);
2159 Check_Non_Static_Context (Choice);
2161 -- If semantic errors were posted on the choice, then
2162 -- record that for possible early return from later
2163 -- processing (see handling of enumeration choices).
2165 if Error_Posted (Choice) then
2166 Errors_Posted_On_Choices := True;
2167 end if;
2169 -- Do not range check a choice. This check is redundant
2170 -- since this test is already done when we check that the
2171 -- bounds of the array aggregate are within range.
2173 Set_Do_Range_Check (Choice, False);
2174 end if;
2176 -- If we could not resolve the discrete choice stop here
2178 if Etype (Choice) = Any_Type then
2179 return Failure;
2181 -- If the discrete choice raises CE get its original bounds
2183 elsif Nkind (Choice) = N_Raise_Constraint_Error then
2184 Set_Raises_Constraint_Error (N);
2185 Get_Index_Bounds (Original_Node (Choice), Low, High);
2187 -- Otherwise get its bounds as usual
2189 else
2190 Get_Index_Bounds (Choice, Low, High);
2191 end if;
2193 if (Dynamic_Or_Null_Range (Low, High)
2194 or else (Nkind (Choice) = N_Subtype_Indication
2195 and then
2196 Dynamic_Or_Null_Range (S_Low, S_High)))
2197 and then Nb_Choices /= 1
2198 then
2199 Error_Msg_N
2200 ("dynamic or empty choice in aggregate "
2201 & "must be the only choice", Choice);
2202 return Failure;
2203 end if;
2205 if not (All_Composite_Constraints_Static (Low)
2206 and then All_Composite_Constraints_Static (High)
2207 and then All_Composite_Constraints_Static (S_Low)
2208 and then All_Composite_Constraints_Static (S_High))
2209 then
2210 Check_Restriction (No_Dynamic_Sized_Objects, Choice);
2211 end if;
2213 Nb_Discrete_Choices := Nb_Discrete_Choices + 1;
2214 Table (Nb_Discrete_Choices).Lo := Low;
2215 Table (Nb_Discrete_Choices).Hi := High;
2216 Table (Nb_Discrete_Choices).Choice := Choice;
2218 Next (Choice);
2220 if No (Choice) then
2222 -- Check if we have a single discrete choice and whether
2223 -- this discrete choice specifies a single value.
2225 Single_Choice :=
2226 (Nb_Discrete_Choices = Prev_Nb_Discrete_Choices + 1)
2227 and then (Low = High);
2229 exit;
2230 end if;
2231 end loop;
2233 -- Ada 2005 (AI-231)
2235 if Ada_Version >= Ada_2005
2236 and then Known_Null (Expression (Assoc))
2237 and then not Empty_Range (Assoc)
2238 then
2239 Check_Can_Never_Be_Null (Etype (N), Expression (Assoc));
2240 end if;
2242 -- Ada 2005 (AI-287): In case of default initialized component
2243 -- we delay the resolution to the expansion phase.
2245 if Box_Present (Assoc) then
2247 -- Ada 2005 (AI-287): In case of default initialization of a
2248 -- component the expander will generate calls to the
2249 -- corresponding initialization subprogram. We need to call
2250 -- Resolve_Aggr_Expr to check the rules about
2251 -- dimensionality.
2253 if not Resolve_Aggr_Expr
2254 (Assoc, Single_Elmt => Single_Choice)
2255 then
2256 return Failure;
2257 end if;
2259 -- ??? Checks for dynamically tagged expressions below will
2260 -- be only applied to iterated_component_association after
2261 -- expansion; in particular, errors might not be reported when
2262 -- -gnatc switch is used.
2264 elsif Nkind (Assoc) = N_Iterated_Component_Association then
2265 null; -- handled above, in a loop context
2267 elsif not Resolve_Aggr_Expr
2268 (Expression (Assoc), Single_Elmt => Single_Choice)
2269 then
2270 return Failure;
2272 -- Check incorrect use of dynamically tagged expression
2274 -- We differentiate here two cases because the expression may
2275 -- not be decorated. For example, the analysis and resolution
2276 -- of the expression associated with the others choice will be
2277 -- done later with the full aggregate. In such case we
2278 -- duplicate the expression tree to analyze the copy and
2279 -- perform the required check.
2281 elsif not Present (Etype (Expression (Assoc))) then
2282 declare
2283 Save_Analysis : constant Boolean := Full_Analysis;
2284 Expr : constant Node_Id :=
2285 New_Copy_Tree (Expression (Assoc));
2287 begin
2288 Expander_Mode_Save_And_Set (False);
2289 Full_Analysis := False;
2291 -- Analyze the expression, making sure it is properly
2292 -- attached to the tree before we do the analysis.
2294 Set_Parent (Expr, Parent (Expression (Assoc)));
2295 Analyze (Expr);
2297 -- Compute its dimensions now, rather than at the end of
2298 -- resolution, because in the case of multidimensional
2299 -- aggregates subsequent expansion may lead to spurious
2300 -- errors.
2302 Check_Expression_Dimensions (Expr, Component_Typ);
2304 -- If the expression is a literal, propagate this info
2305 -- to the expression in the association, to enable some
2306 -- optimizations downstream.
2308 if Is_Entity_Name (Expr)
2309 and then Present (Entity (Expr))
2310 and then Ekind (Entity (Expr)) = E_Enumeration_Literal
2311 then
2312 Analyze_And_Resolve
2313 (Expression (Assoc), Component_Typ);
2314 end if;
2316 Full_Analysis := Save_Analysis;
2317 Expander_Mode_Restore;
2319 if Is_Tagged_Type (Etype (Expr)) then
2320 Check_Dynamically_Tagged_Expression
2321 (Expr => Expr,
2322 Typ => Component_Type (Etype (N)),
2323 Related_Nod => N);
2324 end if;
2325 end;
2327 elsif Is_Tagged_Type (Etype (Expression (Assoc))) then
2328 Check_Dynamically_Tagged_Expression
2329 (Expr => Expression (Assoc),
2330 Typ => Component_Type (Etype (N)),
2331 Related_Nod => N);
2332 end if;
2334 Next (Assoc);
2335 end loop;
2337 -- If aggregate contains more than one choice then these must be
2338 -- static. Check for duplicate and missing values.
2340 -- Note: there is duplicated code here wrt Check_Choice_Set in
2341 -- the body of Sem_Case, and it is possible we could just reuse
2342 -- that procedure. To be checked ???
2344 if Nb_Discrete_Choices > 1 then
2345 Check_Choices : declare
2346 Choice : Node_Id;
2347 -- Location of choice for messages
2349 Hi_Val : Uint;
2350 Lo_Val : Uint;
2351 -- High end of one range and Low end of the next. Should be
2352 -- contiguous if there is no hole in the list of values.
2354 Lo_Dup : Uint;
2355 Hi_Dup : Uint;
2356 -- End points of duplicated range
2358 Missing_Or_Duplicates : Boolean := False;
2359 -- Set True if missing or duplicate choices found
2361 procedure Output_Bad_Choices (Lo, Hi : Uint; C : Node_Id);
2362 -- Output continuation message with a representation of the
2363 -- bounds (just Lo if Lo = Hi, else Lo .. Hi). C is the
2364 -- choice node where the message is to be posted.
2366 ------------------------
2367 -- Output_Bad_Choices --
2368 ------------------------
2370 procedure Output_Bad_Choices (Lo, Hi : Uint; C : Node_Id) is
2371 begin
2372 -- Enumeration type case
2374 if Is_Enumeration_Type (Index_Typ) then
2375 Error_Msg_Name_1 :=
2376 Chars (Get_Enum_Lit_From_Pos (Index_Typ, Lo, Loc));
2377 Error_Msg_Name_2 :=
2378 Chars (Get_Enum_Lit_From_Pos (Index_Typ, Hi, Loc));
2380 if Lo = Hi then
2381 Error_Msg_N ("\\ %!", C);
2382 else
2383 Error_Msg_N ("\\ % .. %!", C);
2384 end if;
2386 -- Integer types case
2388 else
2389 Error_Msg_Uint_1 := Lo;
2390 Error_Msg_Uint_2 := Hi;
2392 if Lo = Hi then
2393 Error_Msg_N ("\\ ^!", C);
2394 else
2395 Error_Msg_N ("\\ ^ .. ^!", C);
2396 end if;
2397 end if;
2398 end Output_Bad_Choices;
2400 -- Start of processing for Check_Choices
2402 begin
2403 Sort_Case_Table (Table);
2405 -- First we do a quick linear loop to find out if we have
2406 -- any duplicates or missing entries (usually we have a
2407 -- legal aggregate, so this will get us out quickly).
2409 for J in 1 .. Nb_Discrete_Choices - 1 loop
2410 Hi_Val := Expr_Value (Table (J).Hi);
2411 Lo_Val := Expr_Value (Table (J + 1).Lo);
2413 if Lo_Val <= Hi_Val
2414 or else (Lo_Val > Hi_Val + 1
2415 and then not Others_Present)
2416 then
2417 Missing_Or_Duplicates := True;
2418 exit;
2419 end if;
2420 end loop;
2422 -- If we have missing or duplicate entries, first fill in
2423 -- the Highest entries to make life easier in the following
2424 -- loops to detect bad entries.
2426 if Missing_Or_Duplicates then
2427 Table (1).Highest := Expr_Value (Table (1).Hi);
2429 for J in 2 .. Nb_Discrete_Choices loop
2430 Table (J).Highest :=
2431 UI_Max
2432 (Table (J - 1).Highest, Expr_Value (Table (J).Hi));
2433 end loop;
2435 -- Loop through table entries to find duplicate indexes
2437 for J in 2 .. Nb_Discrete_Choices loop
2438 Lo_Val := Expr_Value (Table (J).Lo);
2439 Hi_Val := Expr_Value (Table (J).Hi);
2441 -- Case where we have duplicates (the lower bound of
2442 -- this choice is less than or equal to the highest
2443 -- high bound found so far).
2445 if Lo_Val <= Table (J - 1).Highest then
2447 -- We move backwards looking for duplicates. We can
2448 -- abandon this loop as soon as we reach a choice
2449 -- highest value that is less than Lo_Val.
2451 for K in reverse 1 .. J - 1 loop
2452 exit when Table (K).Highest < Lo_Val;
2454 -- Here we may have duplicates between entries
2455 -- for K and J. Get range of duplicates.
2457 Lo_Dup :=
2458 UI_Max (Lo_Val, Expr_Value (Table (K).Lo));
2459 Hi_Dup :=
2460 UI_Min (Hi_Val, Expr_Value (Table (K).Hi));
2462 -- Nothing to do if duplicate range is null
2464 if Lo_Dup > Hi_Dup then
2465 null;
2467 -- Otherwise place proper message
2469 else
2470 -- We place message on later choice, with a
2471 -- line reference to the earlier choice.
2473 if Sloc (Table (J).Choice) <
2474 Sloc (Table (K).Choice)
2475 then
2476 Choice := Table (K).Choice;
2477 Error_Msg_Sloc := Sloc (Table (J).Choice);
2478 else
2479 Choice := Table (J).Choice;
2480 Error_Msg_Sloc := Sloc (Table (K).Choice);
2481 end if;
2483 if Lo_Dup = Hi_Dup then
2484 Error_Msg_N
2485 ("index value in array aggregate "
2486 & "duplicates the one given#!", Choice);
2487 else
2488 Error_Msg_N
2489 ("index values in array aggregate "
2490 & "duplicate those given#!", Choice);
2491 end if;
2493 Output_Bad_Choices (Lo_Dup, Hi_Dup, Choice);
2494 end if;
2495 end loop;
2496 end if;
2497 end loop;
2499 -- Loop through entries in table to find missing indexes.
2500 -- Not needed if others, since missing impossible.
2502 if not Others_Present then
2503 for J in 2 .. Nb_Discrete_Choices loop
2504 Lo_Val := Expr_Value (Table (J).Lo);
2505 Hi_Val := Table (J - 1).Highest;
2507 if Lo_Val > Hi_Val + 1 then
2509 declare
2510 Error_Node : Node_Id;
2512 begin
2513 -- If the choice is the bound of a range in
2514 -- a subtype indication, it is not in the
2515 -- source lists for the aggregate itself, so
2516 -- post the error on the aggregate. Otherwise
2517 -- post it on choice itself.
2519 Choice := Table (J).Choice;
2521 if Is_List_Member (Choice) then
2522 Error_Node := Choice;
2523 else
2524 Error_Node := N;
2525 end if;
2527 if Hi_Val + 1 = Lo_Val - 1 then
2528 Error_Msg_N
2529 ("missing index value "
2530 & "in array aggregate!", Error_Node);
2531 else
2532 Error_Msg_N
2533 ("missing index values "
2534 & "in array aggregate!", Error_Node);
2535 end if;
2537 Output_Bad_Choices
2538 (Hi_Val + 1, Lo_Val - 1, Error_Node);
2539 end;
2540 end if;
2541 end loop;
2542 end if;
2544 -- If either missing or duplicate values, return failure
2546 Set_Etype (N, Any_Composite);
2547 return Failure;
2548 end if;
2549 end Check_Choices;
2550 end if;
2552 if Has_Iterator_Specifications then
2553 -- Bounds will be determined dynamically.
2555 return Success;
2556 end if;
2558 -- STEP 2 (B): Compute aggregate bounds and min/max choices values
2560 if Nb_Discrete_Choices > 0 then
2561 Choices_Low := Table (1).Lo;
2562 Choices_High := Table (Nb_Discrete_Choices).Hi;
2563 end if;
2565 -- If Others is present, then bounds of aggregate come from the
2566 -- index constraint (not the choices in the aggregate itself).
2568 if Others_Present then
2569 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2571 -- Abandon processing if either bound is already signalled as
2572 -- an error (prevents junk cascaded messages and blow ups).
2574 if Nkind (Aggr_Low) = N_Error
2575 or else
2576 Nkind (Aggr_High) = N_Error
2577 then
2578 return False;
2579 end if;
2581 -- No others clause present
2583 else
2584 -- Special processing if others allowed and not present. This
2585 -- means that the bounds of the aggregate come from the index
2586 -- constraint (and the length must match).
2588 if Others_Allowed then
2589 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2591 -- Abandon processing if either bound is already signalled
2592 -- as an error (stop junk cascaded messages and blow ups).
2594 if Nkind (Aggr_Low) = N_Error
2595 or else
2596 Nkind (Aggr_High) = N_Error
2597 then
2598 return False;
2599 end if;
2601 -- If others allowed, and no others present, then the array
2602 -- should cover all index values. If it does not, we will
2603 -- get a length check warning, but there is two cases where
2604 -- an additional warning is useful:
2606 -- If we have no positional components, and the length is
2607 -- wrong (which we can tell by others being allowed with
2608 -- missing components), and the index type is an enumeration
2609 -- type, then issue appropriate warnings about these missing
2610 -- components. They are only warnings, since the aggregate
2611 -- is fine, it's just the wrong length. We skip this check
2612 -- for standard character types (since there are no literals
2613 -- and it is too much trouble to concoct them), and also if
2614 -- any of the bounds have values that are not known at
2615 -- compile time.
2617 -- Another case warranting a warning is when the length
2618 -- is right, but as above we have an index type that is
2619 -- an enumeration, and the bounds do not match. This is a
2620 -- case where dubious sliding is allowed and we generate a
2621 -- warning that the bounds do not match.
2623 if No (Expressions (N))
2624 and then Nkind (Index) = N_Range
2625 and then Is_Enumeration_Type (Etype (Index))
2626 and then not Is_Standard_Character_Type (Etype (Index))
2627 and then Compile_Time_Known_Value (Aggr_Low)
2628 and then Compile_Time_Known_Value (Aggr_High)
2629 and then Compile_Time_Known_Value (Choices_Low)
2630 and then Compile_Time_Known_Value (Choices_High)
2631 then
2632 -- If any of the expressions or range bounds in choices
2633 -- have semantic errors, then do not attempt further
2634 -- resolution, to prevent cascaded errors.
2636 if Errors_Posted_On_Choices then
2637 return Failure;
2638 end if;
2640 declare
2641 ALo : constant Node_Id := Expr_Value_E (Aggr_Low);
2642 AHi : constant Node_Id := Expr_Value_E (Aggr_High);
2643 CLo : constant Node_Id := Expr_Value_E (Choices_Low);
2644 CHi : constant Node_Id := Expr_Value_E (Choices_High);
2646 Ent : Entity_Id;
2648 begin
2649 -- Warning case 1, missing values at start/end. Only
2650 -- do the check if the number of entries is too small.
2652 if (Enumeration_Pos (CHi) - Enumeration_Pos (CLo))
2654 (Enumeration_Pos (AHi) - Enumeration_Pos (ALo))
2655 then
2656 Error_Msg_N
2657 ("missing index value(s) in array aggregate??",
2660 -- Output missing value(s) at start
2662 if Chars (ALo) /= Chars (CLo) then
2663 Ent := Prev (CLo);
2665 if Chars (ALo) = Chars (Ent) then
2666 Error_Msg_Name_1 := Chars (ALo);
2667 Error_Msg_N ("\ %??", N);
2668 else
2669 Error_Msg_Name_1 := Chars (ALo);
2670 Error_Msg_Name_2 := Chars (Ent);
2671 Error_Msg_N ("\ % .. %??", N);
2672 end if;
2673 end if;
2675 -- Output missing value(s) at end
2677 if Chars (AHi) /= Chars (CHi) then
2678 Ent := Next (CHi);
2680 if Chars (AHi) = Chars (Ent) then
2681 Error_Msg_Name_1 := Chars (Ent);
2682 Error_Msg_N ("\ %??", N);
2683 else
2684 Error_Msg_Name_1 := Chars (Ent);
2685 Error_Msg_Name_2 := Chars (AHi);
2686 Error_Msg_N ("\ % .. %??", N);
2687 end if;
2688 end if;
2690 -- Warning case 2, dubious sliding. The First_Subtype
2691 -- test distinguishes between a constrained type where
2692 -- sliding is not allowed (so we will get a warning
2693 -- later that Constraint_Error will be raised), and
2694 -- the unconstrained case where sliding is permitted.
2696 elsif (Enumeration_Pos (CHi) - Enumeration_Pos (CLo))
2698 (Enumeration_Pos (AHi) - Enumeration_Pos (ALo))
2699 and then Chars (ALo) /= Chars (CLo)
2700 and then
2701 not Is_Constrained (First_Subtype (Etype (N)))
2702 then
2703 Error_Msg_N
2704 ("bounds of aggregate do not match target??", N);
2705 end if;
2706 end;
2707 end if;
2708 end if;
2710 -- If no others, aggregate bounds come from aggregate
2712 Aggr_Low := Choices_Low;
2713 Aggr_High := Choices_High;
2714 end if;
2715 end Step_2;
2717 -- STEP 3: Process positional components
2719 else
2720 -- STEP 3 (A): Process positional elements
2722 Expr := First (Expressions (N));
2723 Nb_Elements := Uint_0;
2724 while Present (Expr) loop
2725 Nb_Elements := Nb_Elements + 1;
2727 -- Ada 2005 (AI-231)
2729 if Ada_Version >= Ada_2005 and then Known_Null (Expr) then
2730 Check_Can_Never_Be_Null (Etype (N), Expr);
2731 end if;
2733 if not Resolve_Aggr_Expr (Expr, Single_Elmt => True) then
2734 return Failure;
2735 end if;
2737 -- Check incorrect use of dynamically tagged expression
2739 if Is_Tagged_Type (Etype (Expr)) then
2740 Check_Dynamically_Tagged_Expression
2741 (Expr => Expr,
2742 Typ => Component_Type (Etype (N)),
2743 Related_Nod => N);
2744 end if;
2746 Next (Expr);
2747 end loop;
2749 if Others_Present then
2750 Assoc := Last (Component_Associations (N));
2752 -- Ada 2005 (AI-231)
2754 if Ada_Version >= Ada_2005 and then Known_Null (Assoc) then
2755 Check_Can_Never_Be_Null (Etype (N), Expression (Assoc));
2756 end if;
2758 -- Ada 2005 (AI-287): In case of default initialized component,
2759 -- we delay the resolution to the expansion phase.
2761 if Box_Present (Assoc) then
2763 -- Ada 2005 (AI-287): In case of default initialization of a
2764 -- component the expander will generate calls to the
2765 -- corresponding initialization subprogram. We need to call
2766 -- Resolve_Aggr_Expr to check the rules about
2767 -- dimensionality.
2769 if not Resolve_Aggr_Expr (Assoc, Single_Elmt => False) then
2770 return Failure;
2771 end if;
2773 elsif not Resolve_Aggr_Expr (Expression (Assoc),
2774 Single_Elmt => False)
2775 then
2776 return Failure;
2778 -- Check incorrect use of dynamically tagged expression. The
2779 -- expression of the others choice has not been resolved yet.
2780 -- In order to diagnose the semantic error we create a duplicate
2781 -- tree to analyze it and perform the check.
2783 elsif Nkind (Assoc) /= N_Iterated_Component_Association then
2784 declare
2785 Save_Analysis : constant Boolean := Full_Analysis;
2786 Expr : constant Node_Id :=
2787 New_Copy_Tree (Expression (Assoc));
2789 begin
2790 Expander_Mode_Save_And_Set (False);
2791 Full_Analysis := False;
2792 Analyze (Expr);
2793 Full_Analysis := Save_Analysis;
2794 Expander_Mode_Restore;
2796 if Is_Tagged_Type (Etype (Expr)) then
2797 Check_Dynamically_Tagged_Expression
2798 (Expr => Expr,
2799 Typ => Component_Type (Etype (N)),
2800 Related_Nod => N);
2801 end if;
2802 end;
2803 end if;
2804 end if;
2806 -- STEP 3 (B): Compute the aggregate bounds
2808 if Others_Present then
2809 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2811 else
2812 if Others_Allowed then
2813 Get_Index_Bounds (Index_Constr, Aggr_Low, Discard);
2814 else
2815 Aggr_Low := Index_Typ_Low;
2816 end if;
2818 Aggr_High := Add (Nb_Elements - 1, To => Aggr_Low);
2819 Check_Bound (Index_Base_High, Aggr_High);
2820 end if;
2821 end if;
2823 -- STEP 4: Perform static aggregate checks and save the bounds
2825 -- Check (A)
2827 Check_Bounds (Index_Typ_Low, Index_Typ_High, Aggr_Low, Aggr_High);
2828 Check_Bounds (Index_Base_Low, Index_Base_High, Aggr_Low, Aggr_High);
2830 -- Check (B)
2832 if Others_Present and then Nb_Discrete_Choices > 0 then
2833 Check_Bounds (Aggr_Low, Aggr_High, Choices_Low, Choices_High);
2834 Check_Bounds (Index_Typ_Low, Index_Typ_High,
2835 Choices_Low, Choices_High);
2836 Check_Bounds (Index_Base_Low, Index_Base_High,
2837 Choices_Low, Choices_High);
2839 -- Check (C)
2841 elsif Others_Present and then Nb_Elements > 0 then
2842 Check_Length (Aggr_Low, Aggr_High, Nb_Elements);
2843 Check_Length (Index_Typ_Low, Index_Typ_High, Nb_Elements);
2844 Check_Length (Index_Base_Low, Index_Base_High, Nb_Elements);
2845 end if;
2847 if Raises_Constraint_Error (Aggr_Low)
2848 or else Raises_Constraint_Error (Aggr_High)
2849 then
2850 Set_Raises_Constraint_Error (N);
2851 end if;
2853 Aggr_Low := Duplicate_Subexpr (Aggr_Low);
2855 -- Do not duplicate Aggr_High if Aggr_High = Aggr_Low + Nb_Elements
2856 -- since the addition node returned by Add is not yet analyzed. Attach
2857 -- to tree and analyze first. Reset analyzed flag to ensure it will get
2858 -- analyzed when it is a literal bound whose type must be properly set.
2860 if Others_Present or else Nb_Discrete_Choices > 0 then
2861 Aggr_High := Duplicate_Subexpr (Aggr_High);
2863 if Etype (Aggr_High) = Universal_Integer then
2864 Set_Analyzed (Aggr_High, False);
2865 end if;
2866 end if;
2868 -- If the aggregate already has bounds attached to it, it means this is
2869 -- a positional aggregate created as an optimization by
2870 -- Exp_Aggr.Convert_To_Positional, so we don't want to change those
2871 -- bounds.
2873 if Present (Aggregate_Bounds (N))
2874 and then not Others_Allowed
2875 and then not Comes_From_Source (N)
2876 then
2877 Aggr_Low := Low_Bound (Aggregate_Bounds (N));
2878 Aggr_High := High_Bound (Aggregate_Bounds (N));
2879 end if;
2881 Set_Aggregate_Bounds
2882 (N, Make_Range (Loc, Low_Bound => Aggr_Low, High_Bound => Aggr_High));
2884 -- The bounds may contain expressions that must be inserted upwards.
2885 -- Attach them fully to the tree. After analysis, remove side effects
2886 -- from upper bound, if still needed.
2888 Set_Parent (Aggregate_Bounds (N), N);
2889 Analyze_And_Resolve (Aggregate_Bounds (N), Index_Typ);
2890 Check_Unset_Reference (Aggregate_Bounds (N));
2892 if not Others_Present and then Nb_Discrete_Choices = 0 then
2893 Set_High_Bound
2894 (Aggregate_Bounds (N),
2895 Duplicate_Subexpr (High_Bound (Aggregate_Bounds (N))));
2896 end if;
2898 -- Check the dimensions of each component in the array aggregate
2900 Analyze_Dimension_Array_Aggregate (N, Component_Typ);
2902 return Success;
2903 end Resolve_Array_Aggregate;
2905 ---------------------------------
2906 -- Resolve_Container_Aggregate --
2907 ---------------------------------
2909 procedure Resolve_Container_Aggregate (N : Node_Id; Typ : Entity_Id) is
2910 procedure Resolve_Iterated_Association
2911 (Comp : Node_Id;
2912 Key_Type : Entity_Id;
2913 Elmt_Type : Entity_Id);
2914 -- Resolve choices and expression in an iterated component association
2915 -- or an iterated element association, which has a key_expression.
2916 -- This is similar but not identical to the handling of this construct
2917 -- in an array aggregate.
2918 -- For a named container, the type of each choice must be compatible
2919 -- with the key type. For a positional container, the choice must be
2920 -- a subtype indication or an iterator specification that determines
2921 -- an element type.
2923 Asp : constant Node_Id := Find_Value_Of_Aspect (Typ, Aspect_Aggregate);
2925 Empty_Subp : Node_Id := Empty;
2926 Add_Named_Subp : Node_Id := Empty;
2927 Add_Unnamed_Subp : Node_Id := Empty;
2928 New_Indexed_Subp : Node_Id := Empty;
2929 Assign_Indexed_Subp : Node_Id := Empty;
2931 ----------------------------------
2932 -- Resolve_Iterated_Association --
2933 ----------------------------------
2935 procedure Resolve_Iterated_Association
2936 (Comp : Node_Id;
2937 Key_Type : Entity_Id;
2938 Elmt_Type : Entity_Id)
2940 Loc : constant Source_Ptr := Sloc (N);
2941 Choice : Node_Id;
2942 Copy : Node_Id;
2943 Ent : Entity_Id;
2944 Expr : Node_Id;
2945 Key_Expr : Node_Id;
2946 Id : Entity_Id;
2947 Id_Name : Name_Id;
2948 Typ : Entity_Id := Empty;
2950 begin
2951 Error_Msg_Ada_2022_Feature ("iterated component", Loc);
2953 -- If this is an Iterated_Element_Association then either a
2954 -- an Iterator_Specification or a Loop_Parameter specification
2955 -- is present. In both cases a Key_Expression is present.
2957 if Nkind (Comp) = N_Iterated_Element_Association then
2959 -- Create a temporary scope to avoid some modifications from
2960 -- escaping the Analyze call below. The original Tree will be
2961 -- reanalyzed later.
2963 Ent := New_Internal_Entity
2964 (E_Loop, Current_Scope, Sloc (Comp), 'L');
2965 Set_Etype (Ent, Standard_Void_Type);
2966 Set_Parent (Ent, Parent (Comp));
2967 Push_Scope (Ent);
2969 if Present (Loop_Parameter_Specification (Comp)) then
2970 Copy := Copy_Separate_Tree (Comp);
2972 Analyze
2973 (Loop_Parameter_Specification (Copy));
2975 Id_Name := Chars (Defining_Identifier
2976 (Loop_Parameter_Specification (Comp)));
2977 else
2978 Copy := Copy_Separate_Tree (Iterator_Specification (Comp));
2979 Analyze (Copy);
2981 Id_Name := Chars (Defining_Identifier
2982 (Iterator_Specification (Comp)));
2983 end if;
2985 -- Key expression must have the type of the key. We analyze
2986 -- a copy of the original expression, because it will be
2987 -- reanalyzed and copied as needed during expansion of the
2988 -- corresponding loop.
2990 Key_Expr := Key_Expression (Comp);
2991 Analyze_And_Resolve (New_Copy_Tree (Key_Expr), Key_Type);
2992 End_Scope;
2994 elsif Present (Iterator_Specification (Comp)) then
2995 Copy := Copy_Separate_Tree (Iterator_Specification (Comp));
2996 Id_Name := Chars (Defining_Identifier (Comp));
2998 Analyze (Copy);
2999 Typ := Etype (Defining_Identifier (Copy));
3001 else
3002 Choice := First (Discrete_Choices (Comp));
3004 while Present (Choice) loop
3005 Analyze (Choice);
3007 -- Choice can be a subtype name, a range, or an expression
3009 if Is_Entity_Name (Choice)
3010 and then Is_Type (Entity (Choice))
3011 and then Base_Type (Entity (Choice)) = Base_Type (Key_Type)
3012 then
3013 null;
3015 elsif Present (Key_Type) then
3016 Analyze_And_Resolve (Choice, Key_Type);
3018 else
3019 Typ := Etype (Choice); -- assume unique for now
3020 end if;
3022 Next (Choice);
3023 end loop;
3025 Id_Name := Chars (Defining_Identifier (Comp));
3026 end if;
3028 -- Create a scope in which to introduce an index, which is usually
3029 -- visible in the expression for the component, and needed for its
3030 -- analysis.
3032 Id := Make_Defining_Identifier (Sloc (Comp), Id_Name);
3033 Ent := New_Internal_Entity (E_Loop,
3034 Current_Scope, Sloc (Comp), 'L');
3035 Set_Etype (Ent, Standard_Void_Type);
3036 Set_Parent (Ent, Parent (Comp));
3037 Push_Scope (Ent);
3039 -- Insert and decorate the loop variable in the current scope.
3040 -- The expression has to be analyzed once the loop variable is
3041 -- directly visible. Mark the variable as referenced to prevent
3042 -- spurious warnings, given that subsequent uses of its name in the
3043 -- expression will reference the internal (synonym) loop variable.
3045 Enter_Name (Id);
3047 if No (Key_Type) then
3048 pragma Assert (Present (Typ));
3049 Set_Etype (Id, Typ);
3050 else
3051 Set_Etype (Id, Key_Type);
3052 end if;
3054 Mutate_Ekind (Id, E_Variable);
3055 Set_Scope (Id, Ent);
3056 Set_Referenced (Id);
3058 -- Analyze a copy of the expression, to verify legality. We use
3059 -- a copy because the expression will be analyzed anew when the
3060 -- enclosing aggregate is expanded, and the construct is rewritten
3061 -- as a loop with a new index variable.
3063 Expr := New_Copy_Tree (Expression (Comp));
3064 Preanalyze_And_Resolve (Expr, Elmt_Type);
3065 End_Scope;
3067 end Resolve_Iterated_Association;
3069 -- Start of processing for Resolve_Container_Aggregate
3071 begin
3072 pragma Assert (Nkind (Asp) = N_Aggregate);
3074 Set_Etype (N, Typ);
3075 Parse_Aspect_Aggregate (Asp,
3076 Empty_Subp, Add_Named_Subp, Add_Unnamed_Subp,
3077 New_Indexed_Subp, Assign_Indexed_Subp);
3079 if Present (Add_Unnamed_Subp)
3080 and then No (New_Indexed_Subp)
3081 then
3082 declare
3083 Elmt_Type : constant Entity_Id :=
3084 Etype (Next_Formal
3085 (First_Formal (Entity (Add_Unnamed_Subp))));
3086 Comp : Node_Id;
3088 begin
3089 if Present (Expressions (N)) then
3090 -- positional aggregate
3092 Comp := First (Expressions (N));
3093 while Present (Comp) loop
3094 Analyze_And_Resolve (Comp, Elmt_Type);
3095 Next (Comp);
3096 end loop;
3097 end if;
3099 -- Empty aggregate, to be replaced by Empty during
3100 -- expansion, or iterated component association.
3102 if Present (Component_Associations (N)) then
3103 declare
3104 Comp : Node_Id := First (Component_Associations (N));
3105 begin
3106 while Present (Comp) loop
3107 if Nkind (Comp) /=
3108 N_Iterated_Component_Association
3109 then
3110 Error_Msg_N ("illegal component association "
3111 & "for unnamed container aggregate", Comp);
3112 return;
3113 else
3114 Resolve_Iterated_Association
3115 (Comp, Empty, Elmt_Type);
3116 end if;
3118 Next (Comp);
3119 end loop;
3120 end;
3121 end if;
3122 end;
3124 elsif Present (Add_Named_Subp) then
3125 declare
3126 -- Retrieves types of container, key, and element from the
3127 -- specified insertion procedure.
3129 Container : constant Entity_Id :=
3130 First_Formal (Entity (Add_Named_Subp));
3131 Key_Type : constant Entity_Id := Etype (Next_Formal (Container));
3132 Elmt_Type : constant Entity_Id :=
3133 Etype (Next_Formal (Next_Formal (Container)));
3134 Comp : Node_Id;
3135 Choice : Node_Id;
3137 begin
3138 Comp := First (Component_Associations (N));
3139 while Present (Comp) loop
3140 if Nkind (Comp) = N_Component_Association then
3141 Choice := First (Choices (Comp));
3143 while Present (Choice) loop
3144 Analyze_And_Resolve (Choice, Key_Type);
3145 if not Is_Static_Expression (Choice) then
3146 Error_Msg_N ("choice must be static", Choice);
3147 end if;
3149 Next (Choice);
3150 end loop;
3152 Analyze_And_Resolve (Expression (Comp), Elmt_Type);
3154 elsif Nkind (Comp) in
3155 N_Iterated_Component_Association |
3156 N_Iterated_Element_Association
3157 then
3158 Resolve_Iterated_Association
3159 (Comp, Key_Type, Elmt_Type);
3160 end if;
3162 Next (Comp);
3163 end loop;
3164 end;
3166 else
3167 -- Indexed Aggregate. Positional or indexed component
3168 -- can be present, but not both. Choices must be static
3169 -- values or ranges with static bounds.
3171 declare
3172 Container : constant Entity_Id :=
3173 First_Formal (Entity (Assign_Indexed_Subp));
3174 Index_Type : constant Entity_Id := Etype (Next_Formal (Container));
3175 Comp_Type : constant Entity_Id :=
3176 Etype (Next_Formal (Next_Formal (Container)));
3177 Comp : Node_Id;
3178 Choice : Node_Id;
3179 Num_Choices : Nat := 0;
3181 Hi_Val : Uint;
3182 Lo_Val : Uint;
3183 begin
3184 if Present (Expressions (N)) then
3185 Comp := First (Expressions (N));
3186 while Present (Comp) loop
3187 Analyze_And_Resolve (Comp, Comp_Type);
3188 Next (Comp);
3189 end loop;
3190 end if;
3192 if Present (Component_Associations (N))
3193 and then not Is_Empty_List (Component_Associations (N))
3194 then
3195 if Present (Expressions (N))
3196 and then not Is_Empty_List (Expressions (N))
3197 then
3198 Error_Msg_N ("container aggregate cannot be "
3199 & "both positional and named", N);
3200 return;
3201 end if;
3203 Comp := First (Component_Associations (N));
3205 while Present (Comp) loop
3206 if Nkind (Comp) = N_Component_Association then
3207 Choice := First (Choices (Comp));
3209 while Present (Choice) loop
3210 Analyze_And_Resolve (Choice, Index_Type);
3211 Num_Choices := Num_Choices + 1;
3212 Next (Choice);
3213 end loop;
3215 Analyze_And_Resolve (Expression (Comp), Comp_Type);
3217 elsif Nkind (Comp) in
3218 N_Iterated_Component_Association |
3219 N_Iterated_Element_Association
3220 then
3221 Resolve_Iterated_Association
3222 (Comp, Index_Type, Comp_Type);
3223 Num_Choices := Num_Choices + 1;
3224 end if;
3226 Next (Comp);
3227 end loop;
3229 -- The component associations in an indexed aggregate
3230 -- must denote a contiguous set of static values. We
3231 -- build a table of values/ranges and sort it, as is done
3232 -- elsewhere for case statements and array aggregates.
3233 -- If the aggregate has a single iterated association it
3234 -- is allowed to be nonstatic and there is nothing to check.
3236 if Num_Choices > 1 then
3237 declare
3238 Table : Case_Table_Type (1 .. Num_Choices);
3239 No_Choice : Pos := 1;
3240 Lo, Hi : Node_Id;
3242 -- Traverse aggregate to determine size of needed table.
3243 -- Verify that bounds are static and that loops have no
3244 -- filters or key expressions.
3246 begin
3247 Comp := First (Component_Associations (N));
3248 while Present (Comp) loop
3249 if Nkind (Comp) = N_Iterated_Element_Association then
3250 if Present
3251 (Loop_Parameter_Specification (Comp))
3252 then
3253 if Present (Iterator_Filter
3254 (Loop_Parameter_Specification (Comp)))
3255 then
3256 Error_Msg_N
3257 ("iterator filter not allowed " &
3258 "in indexed aggregate", Comp);
3259 return;
3261 elsif Present (Key_Expression
3262 (Loop_Parameter_Specification (Comp)))
3263 then
3264 Error_Msg_N
3265 ("key expression not allowed " &
3266 "in indexed aggregate", Comp);
3267 return;
3268 end if;
3269 end if;
3270 else
3271 Choice := First (Choices (Comp));
3273 while Present (Choice) loop
3274 Get_Index_Bounds (Choice, Lo, Hi);
3275 Table (No_Choice).Choice := Choice;
3276 Table (No_Choice).Lo := Lo;
3277 Table (No_Choice).Hi := Hi;
3279 -- Verify staticness of value or range
3281 if not Is_Static_Expression (Lo)
3282 or else not Is_Static_Expression (Hi)
3283 then
3284 Error_Msg_N
3285 ("nonstatic expression for index " &
3286 "for indexed aggregate", Choice);
3287 return;
3288 end if;
3290 No_Choice := No_Choice + 1;
3291 Next (Choice);
3292 end loop;
3293 end if;
3295 Next (Comp);
3296 end loop;
3298 Sort_Case_Table (Table);
3300 for J in 1 .. Num_Choices - 1 loop
3301 Hi_Val := Expr_Value (Table (J).Hi);
3302 Lo_Val := Expr_Value (Table (J + 1).Lo);
3304 if Lo_Val = Hi_Val then
3305 Error_Msg_N
3306 ("duplicate index in indexed aggregate",
3307 Table (J + 1).Choice);
3308 exit;
3310 elsif Lo_Val < Hi_Val then
3311 Error_Msg_N
3312 ("overlapping indices in indexed aggregate",
3313 Table (J + 1).Choice);
3314 exit;
3316 elsif Lo_Val > Hi_Val + 1 then
3317 Error_Msg_N
3318 ("missing index values", Table (J + 1).Choice);
3319 exit;
3320 end if;
3321 end loop;
3322 end;
3323 end if;
3324 end if;
3325 end;
3326 end if;
3327 end Resolve_Container_Aggregate;
3329 -----------------------------
3330 -- Resolve_Delta_Aggregate --
3331 -----------------------------
3333 procedure Resolve_Delta_Aggregate (N : Node_Id; Typ : Entity_Id) is
3334 Base : constant Node_Id := Expression (N);
3336 begin
3337 Error_Msg_Ada_2022_Feature ("delta aggregate", Sloc (N));
3339 if not Is_Composite_Type (Typ) then
3340 Error_Msg_N ("not a composite type", N);
3341 end if;
3343 Analyze_And_Resolve (Base, Typ);
3345 if Is_Array_Type (Typ) then
3346 Resolve_Delta_Array_Aggregate (N, Typ);
3347 else
3349 -- Delta aggregates for record types must use parentheses,
3350 -- not square brackets.
3352 if Is_Homogeneous_Aggregate (N) then
3353 Error_Msg_N
3354 ("delta aggregates for record types must use (), not '[']", N);
3355 end if;
3357 Resolve_Delta_Record_Aggregate (N, Typ);
3358 end if;
3360 Set_Etype (N, Typ);
3361 end Resolve_Delta_Aggregate;
3363 -----------------------------------
3364 -- Resolve_Delta_Array_Aggregate --
3365 -----------------------------------
3367 procedure Resolve_Delta_Array_Aggregate (N : Node_Id; Typ : Entity_Id) is
3368 Deltas : constant List_Id := Component_Associations (N);
3369 Index_Type : constant Entity_Id := Etype (First_Index (Typ));
3371 Assoc : Node_Id;
3372 Choice : Node_Id;
3373 Expr : Node_Id;
3375 begin
3376 Assoc := First (Deltas);
3377 while Present (Assoc) loop
3378 if Nkind (Assoc) = N_Iterated_Component_Association then
3379 Choice := First (Choice_List (Assoc));
3380 while Present (Choice) loop
3381 if Nkind (Choice) = N_Others_Choice then
3382 Error_Msg_N
3383 ("OTHERS not allowed in delta aggregate", Choice);
3385 elsif Nkind (Choice) = N_Subtype_Indication then
3386 Resolve_Discrete_Subtype_Indication
3387 (Choice, Base_Type (Index_Type));
3389 else
3390 Analyze_And_Resolve (Choice, Index_Type);
3391 end if;
3393 Next (Choice);
3394 end loop;
3396 declare
3397 Id : constant Entity_Id := Defining_Identifier (Assoc);
3398 Ent : constant Entity_Id :=
3399 New_Internal_Entity
3400 (E_Loop, Current_Scope, Sloc (Assoc), 'L');
3402 begin
3403 Set_Etype (Ent, Standard_Void_Type);
3404 Set_Parent (Ent, Assoc);
3405 Push_Scope (Ent);
3407 if No (Scope (Id)) then
3408 Set_Etype (Id, Index_Type);
3409 Mutate_Ekind (Id, E_Variable);
3410 Set_Scope (Id, Ent);
3411 end if;
3412 Enter_Name (Id);
3414 -- Resolve a copy of the expression, after setting
3415 -- its parent properly to preserve its context.
3417 Expr := New_Copy_Tree (Expression (Assoc));
3418 Set_Parent (Expr, Assoc);
3419 Analyze_And_Resolve (Expr, Component_Type (Typ));
3420 End_Scope;
3421 end;
3423 else
3424 Choice := First (Choice_List (Assoc));
3425 while Present (Choice) loop
3426 Analyze (Choice);
3428 if Nkind (Choice) = N_Others_Choice then
3429 Error_Msg_N
3430 ("OTHERS not allowed in delta aggregate", Choice);
3432 elsif Is_Entity_Name (Choice)
3433 and then Is_Type (Entity (Choice))
3434 then
3435 -- Choice covers a range of values
3437 if Base_Type (Entity (Choice)) /=
3438 Base_Type (Index_Type)
3439 then
3440 Error_Msg_NE
3441 ("choice does not match index type of &",
3442 Choice, Typ);
3443 end if;
3445 elsif Nkind (Choice) = N_Subtype_Indication then
3446 Resolve_Discrete_Subtype_Indication
3447 (Choice, Base_Type (Index_Type));
3449 else
3450 Resolve (Choice, Index_Type);
3451 end if;
3453 Next (Choice);
3454 end loop;
3456 Analyze_And_Resolve (Expression (Assoc), Component_Type (Typ));
3457 end if;
3459 Next (Assoc);
3460 end loop;
3461 end Resolve_Delta_Array_Aggregate;
3463 ------------------------------------
3464 -- Resolve_Delta_Record_Aggregate --
3465 ------------------------------------
3467 procedure Resolve_Delta_Record_Aggregate (N : Node_Id; Typ : Entity_Id) is
3469 -- Variables used to verify that discriminant-dependent components
3470 -- appear in the same variant.
3472 Comp_Ref : Entity_Id := Empty; -- init to avoid warning
3473 Variant : Node_Id;
3475 procedure Check_Variant (Id : Entity_Id);
3476 -- If a given component of the delta aggregate appears in a variant
3477 -- part, verify that it is within the same variant as that of previous
3478 -- specified variant components of the delta.
3480 function Get_Component (Nam : Node_Id) return Entity_Id;
3481 -- Locate component with a given name and return it. If none found then
3482 -- report error and return Empty.
3484 function Nested_In (V1 : Node_Id; V2 : Node_Id) return Boolean;
3485 -- Determine whether variant V1 is within variant V2
3487 function Variant_Depth (N : Node_Id) return Natural;
3488 -- Determine the distance of a variant to the enclosing type declaration
3490 --------------------
3491 -- Check_Variant --
3492 --------------------
3494 procedure Check_Variant (Id : Entity_Id) is
3495 Comp : Entity_Id;
3496 Comp_Variant : Node_Id;
3498 begin
3499 if not Has_Discriminants (Typ) then
3500 return;
3501 end if;
3503 Comp := First_Entity (Typ);
3504 while Present (Comp) loop
3505 exit when Chars (Comp) = Chars (Id);
3506 Next_Component (Comp);
3507 end loop;
3509 -- Find the variant, if any, whose component list includes the
3510 -- component declaration.
3512 Comp_Variant := Parent (Parent (List_Containing (Parent (Comp))));
3513 if Nkind (Comp_Variant) = N_Variant then
3514 if No (Variant) then
3515 Variant := Comp_Variant;
3516 Comp_Ref := Comp;
3518 elsif Variant /= Comp_Variant then
3519 declare
3520 D1 : constant Integer := Variant_Depth (Variant);
3521 D2 : constant Integer := Variant_Depth (Comp_Variant);
3523 begin
3524 if D1 = D2
3525 or else
3526 (D1 > D2 and then not Nested_In (Variant, Comp_Variant))
3527 or else
3528 (D2 > D1 and then not Nested_In (Comp_Variant, Variant))
3529 then
3530 pragma Assert (Present (Comp_Ref));
3531 Error_Msg_Node_2 := Comp_Ref;
3532 Error_Msg_NE
3533 ("& and & appear in different variants", Id, Comp);
3535 -- Otherwise retain the deeper variant for subsequent tests
3537 elsif D2 > D1 then
3538 Variant := Comp_Variant;
3539 end if;
3540 end;
3541 end if;
3542 end if;
3543 end Check_Variant;
3545 -------------------
3546 -- Get_Component --
3547 -------------------
3549 function Get_Component (Nam : Node_Id) return Entity_Id is
3550 Comp : Entity_Id;
3552 begin
3553 Comp := First_Entity (Typ);
3554 while Present (Comp) loop
3555 if Chars (Comp) = Chars (Nam) then
3556 if Ekind (Comp) = E_Discriminant then
3557 Error_Msg_N ("delta cannot apply to discriminant", Nam);
3558 end if;
3560 return Comp;
3561 end if;
3563 Next_Entity (Comp);
3564 end loop;
3566 Error_Msg_NE ("type& has no component with this name", Nam, Typ);
3567 return Empty;
3568 end Get_Component;
3570 ---------------
3571 -- Nested_In --
3572 ---------------
3574 function Nested_In (V1, V2 : Node_Id) return Boolean is
3575 Par : Node_Id;
3577 begin
3578 Par := Parent (V1);
3579 while Nkind (Par) /= N_Full_Type_Declaration loop
3580 if Par = V2 then
3581 return True;
3582 end if;
3584 Par := Parent (Par);
3585 end loop;
3587 return False;
3588 end Nested_In;
3590 -------------------
3591 -- Variant_Depth --
3592 -------------------
3594 function Variant_Depth (N : Node_Id) return Natural is
3595 Depth : Natural;
3596 Par : Node_Id;
3598 begin
3599 Depth := 0;
3600 Par := Parent (N);
3601 while Nkind (Par) /= N_Full_Type_Declaration loop
3602 Depth := Depth + 1;
3603 Par := Parent (Par);
3604 end loop;
3606 return Depth;
3607 end Variant_Depth;
3609 -- Local variables
3611 Deltas : constant List_Id := Component_Associations (N);
3613 Assoc : Node_Id;
3614 Choice : Node_Id;
3615 Comp : Entity_Id;
3616 Comp_Type : Entity_Id := Empty; -- init to avoid warning
3618 -- Start of processing for Resolve_Delta_Record_Aggregate
3620 begin
3621 Variant := Empty;
3623 Assoc := First (Deltas);
3624 while Present (Assoc) loop
3625 Choice := First (Choice_List (Assoc));
3626 while Present (Choice) loop
3627 Comp := Get_Component (Choice);
3629 if Present (Comp) then
3630 Check_Variant (Choice);
3632 Comp_Type := Etype (Comp);
3634 -- Decorate the component reference by setting its entity and
3635 -- type, as otherwise backends like GNATprove would have to
3636 -- rediscover this information by themselves.
3638 Set_Entity (Choice, Comp);
3639 Set_Etype (Choice, Comp_Type);
3640 else
3641 Comp_Type := Any_Type;
3642 end if;
3644 Next (Choice);
3645 end loop;
3647 pragma Assert (Present (Comp_Type));
3649 -- A record_component_association in record_delta_aggregate shall not
3650 -- use the box compound delimiter <> rather than an expression; see
3651 -- RM 4.3.1(17.3/5).
3653 pragma Assert (Present (Expression (Assoc)) xor Box_Present (Assoc));
3655 if Box_Present (Assoc) then
3656 Error_Msg_N
3657 ("'<'> in record delta aggregate is not allowed", Assoc);
3658 else
3659 Analyze_And_Resolve (Expression (Assoc), Comp_Type);
3660 end if;
3661 Next (Assoc);
3662 end loop;
3663 end Resolve_Delta_Record_Aggregate;
3665 ---------------------------------
3666 -- Resolve_Extension_Aggregate --
3667 ---------------------------------
3669 -- There are two cases to consider:
3671 -- a) If the ancestor part is a type mark, the components needed are the
3672 -- difference between the components of the expected type and the
3673 -- components of the given type mark.
3675 -- b) If the ancestor part is an expression, it must be unambiguous, and
3676 -- once we have its type we can also compute the needed components as in
3677 -- the previous case. In both cases, if the ancestor type is not the
3678 -- immediate ancestor, we have to build this ancestor recursively.
3680 -- In both cases, discriminants of the ancestor type do not play a role in
3681 -- the resolution of the needed components, because inherited discriminants
3682 -- cannot be used in a type extension. As a result we can compute
3683 -- independently the list of components of the ancestor type and of the
3684 -- expected type.
3686 procedure Resolve_Extension_Aggregate (N : Node_Id; Typ : Entity_Id) is
3687 A : constant Node_Id := Ancestor_Part (N);
3688 A_Type : Entity_Id;
3689 I : Interp_Index;
3690 It : Interp;
3692 function Valid_Limited_Ancestor (Anc : Node_Id) return Boolean;
3693 -- If the type is limited, verify that the ancestor part is a legal
3694 -- expression (aggregate or function call, including 'Input)) that does
3695 -- not require a copy, as specified in 7.5(2).
3697 function Valid_Ancestor_Type return Boolean;
3698 -- Verify that the type of the ancestor part is a non-private ancestor
3699 -- of the expected type, which must be a type extension.
3701 procedure Transform_BIP_Assignment (Typ : Entity_Id);
3702 -- For an extension aggregate whose ancestor part is a build-in-place
3703 -- call returning a nonlimited type, this is used to transform the
3704 -- assignment to the ancestor part to use a temp.
3706 ----------------------------
3707 -- Valid_Limited_Ancestor --
3708 ----------------------------
3710 function Valid_Limited_Ancestor (Anc : Node_Id) return Boolean is
3711 begin
3712 if Is_Entity_Name (Anc) and then Is_Type (Entity (Anc)) then
3713 return True;
3715 -- The ancestor must be a call or an aggregate, but a call may
3716 -- have been expanded into a temporary, so check original node.
3718 elsif Nkind (Anc) in N_Aggregate
3719 | N_Extension_Aggregate
3720 | N_Function_Call
3721 then
3722 return True;
3724 elsif Nkind (Original_Node (Anc)) = N_Function_Call then
3725 return True;
3727 elsif Nkind (Anc) = N_Attribute_Reference
3728 and then Attribute_Name (Anc) = Name_Input
3729 then
3730 return True;
3732 elsif Nkind (Anc) = N_Qualified_Expression then
3733 return Valid_Limited_Ancestor (Expression (Anc));
3735 elsif Nkind (Anc) = N_Raise_Expression then
3736 return True;
3738 else
3739 return False;
3740 end if;
3741 end Valid_Limited_Ancestor;
3743 -------------------------
3744 -- Valid_Ancestor_Type --
3745 -------------------------
3747 function Valid_Ancestor_Type return Boolean is
3748 Imm_Type : Entity_Id;
3750 begin
3751 Imm_Type := Base_Type (Typ);
3752 while Is_Derived_Type (Imm_Type) loop
3753 if Etype (Imm_Type) = Base_Type (A_Type) then
3754 return True;
3756 -- The base type of the parent type may appear as a private
3757 -- extension if it is declared as such in a parent unit of the
3758 -- current one. For consistency of the subsequent analysis use
3759 -- the partial view for the ancestor part.
3761 elsif Is_Private_Type (Etype (Imm_Type))
3762 and then Present (Full_View (Etype (Imm_Type)))
3763 and then Base_Type (A_Type) = Full_View (Etype (Imm_Type))
3764 then
3765 A_Type := Etype (Imm_Type);
3766 return True;
3768 -- The parent type may be a private extension. The aggregate is
3769 -- legal if the type of the aggregate is an extension of it that
3770 -- is not a private extension.
3772 elsif Is_Private_Type (A_Type)
3773 and then not Is_Private_Type (Imm_Type)
3774 and then Present (Full_View (A_Type))
3775 and then Base_Type (Full_View (A_Type)) = Etype (Imm_Type)
3776 then
3777 return True;
3779 -- The parent type may be a raise expression (which is legal in
3780 -- any expression context).
3782 elsif A_Type = Raise_Type then
3783 A_Type := Etype (Imm_Type);
3784 return True;
3786 else
3787 Imm_Type := Etype (Base_Type (Imm_Type));
3788 end if;
3789 end loop;
3791 -- If previous loop did not find a proper ancestor, report error
3793 Error_Msg_NE ("expect ancestor type of &", A, Typ);
3794 return False;
3795 end Valid_Ancestor_Type;
3797 ------------------------------
3798 -- Transform_BIP_Assignment --
3799 ------------------------------
3801 procedure Transform_BIP_Assignment (Typ : Entity_Id) is
3802 Loc : constant Source_Ptr := Sloc (N);
3803 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'Y', A);
3804 Obj_Decl : constant Node_Id :=
3805 Make_Object_Declaration (Loc,
3806 Defining_Identifier => Def_Id,
3807 Constant_Present => True,
3808 Object_Definition => New_Occurrence_Of (Typ, Loc),
3809 Expression => A,
3810 Has_Init_Expression => True);
3811 begin
3812 Set_Etype (Def_Id, Typ);
3813 Set_Ancestor_Part (N, New_Occurrence_Of (Def_Id, Loc));
3814 Insert_Action (N, Obj_Decl);
3815 end Transform_BIP_Assignment;
3817 -- Start of processing for Resolve_Extension_Aggregate
3819 begin
3820 -- Analyze the ancestor part and account for the case where it is a
3821 -- parameterless function call.
3823 Analyze (A);
3824 Check_Parameterless_Call (A);
3826 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
3828 -- AI05-0115: If the ancestor part is a subtype mark, the ancestor
3829 -- must not have unknown discriminants. To catch cases where the
3830 -- aggregate occurs at a place where the full view of the ancestor
3831 -- type is visible and doesn't have unknown discriminants, but the
3832 -- aggregate type was derived from a partial view that has unknown
3833 -- discriminants, we check whether the aggregate type has unknown
3834 -- discriminants (unknown discriminants were inherited), along
3835 -- with checking that the partial view of the ancestor has unknown
3836 -- discriminants. (It might be sufficient to replace the entire
3837 -- condition with Has_Unknown_Discriminants (Typ), but that might
3838 -- miss some cases, not clear, and causes error changes in some tests
3839 -- such as class-wide cases, that aren't clearly improvements. ???)
3841 if Has_Unknown_Discriminants (Entity (A))
3842 or else (Has_Unknown_Discriminants (Typ)
3843 and then Partial_View_Has_Unknown_Discr (Entity (A)))
3844 then
3845 Error_Msg_NE
3846 ("aggregate not available for type& whose ancestor "
3847 & "has unknown discriminants", N, Typ);
3848 end if;
3849 end if;
3851 if not Is_Tagged_Type (Typ) then
3852 Error_Msg_N ("type of extension aggregate must be tagged", N);
3853 return;
3855 elsif Is_Limited_Type (Typ) then
3857 -- Ada 2005 (AI-287): Limited aggregates are allowed
3859 if Ada_Version < Ada_2005 then
3860 Error_Msg_N ("aggregate type cannot be limited", N);
3861 Explain_Limited_Type (Typ, N);
3862 return;
3864 elsif Valid_Limited_Ancestor (A) then
3865 null;
3867 else
3868 Error_Msg_N
3869 ("limited ancestor part must be aggregate or function call", A);
3870 end if;
3872 elsif Is_Class_Wide_Type (Typ) then
3873 Error_Msg_N ("aggregate cannot be of a class-wide type", N);
3874 return;
3875 end if;
3877 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
3878 A_Type := Get_Full_View (Entity (A));
3880 if Valid_Ancestor_Type then
3881 Set_Entity (A, A_Type);
3882 Set_Etype (A, A_Type);
3884 Validate_Ancestor_Part (N);
3885 Resolve_Record_Aggregate (N, Typ);
3886 end if;
3888 elsif Nkind (A) /= N_Aggregate then
3889 if Is_Overloaded (A) then
3890 A_Type := Any_Type;
3892 Get_First_Interp (A, I, It);
3893 while Present (It.Typ) loop
3895 -- Consider limited interpretations if Ada 2005 or higher
3897 if Is_Tagged_Type (It.Typ)
3898 and then (Ada_Version >= Ada_2005
3899 or else not Is_Limited_Type (It.Typ))
3900 then
3901 if A_Type /= Any_Type then
3902 Error_Msg_N ("cannot resolve expression", A);
3903 return;
3904 else
3905 A_Type := It.Typ;
3906 end if;
3907 end if;
3909 Get_Next_Interp (I, It);
3910 end loop;
3912 if A_Type = Any_Type then
3913 if Ada_Version >= Ada_2005 then
3914 Error_Msg_N
3915 ("ancestor part must be of a tagged type", A);
3916 else
3917 Error_Msg_N
3918 ("ancestor part must be of a nonlimited tagged type", A);
3919 end if;
3921 return;
3922 end if;
3924 else
3925 A_Type := Etype (A);
3926 end if;
3928 if Valid_Ancestor_Type then
3929 Resolve (A, A_Type);
3930 Check_Unset_Reference (A);
3931 Check_Non_Static_Context (A);
3933 -- The aggregate is illegal if the ancestor expression is a call
3934 -- to a function with a limited unconstrained result, unless the
3935 -- type of the aggregate is a null extension. This restriction
3936 -- was added in AI05-67 to simplify implementation.
3938 if Nkind (A) = N_Function_Call
3939 and then Is_Limited_Type (A_Type)
3940 and then not Is_Null_Extension (Typ)
3941 and then not Is_Constrained (A_Type)
3942 then
3943 Error_Msg_N
3944 ("type of limited ancestor part must be constrained", A);
3946 -- Reject the use of CPP constructors that leave objects partially
3947 -- initialized. For example:
3949 -- type CPP_Root is tagged limited record ...
3950 -- pragma Import (CPP, CPP_Root);
3952 -- type CPP_DT is new CPP_Root and Iface ...
3953 -- pragma Import (CPP, CPP_DT);
3955 -- type Ada_DT is new CPP_DT with ...
3957 -- Obj : Ada_DT := Ada_DT'(New_CPP_Root with others => <>);
3959 -- Using the constructor of CPP_Root the slots of the dispatch
3960 -- table of CPP_DT cannot be set, and the secondary tag of
3961 -- CPP_DT is unknown.
3963 elsif Nkind (A) = N_Function_Call
3964 and then Is_CPP_Constructor_Call (A)
3965 and then Enclosing_CPP_Parent (Typ) /= A_Type
3966 then
3967 Error_Msg_NE
3968 ("??must use 'C'P'P constructor for type &", A,
3969 Enclosing_CPP_Parent (Typ));
3971 -- The following call is not needed if the previous warning
3972 -- is promoted to an error.
3974 Resolve_Record_Aggregate (N, Typ);
3976 elsif Is_Class_Wide_Type (Etype (A))
3977 and then Nkind (Original_Node (A)) = N_Function_Call
3978 then
3979 -- If the ancestor part is a dispatching call, it appears
3980 -- statically to be a legal ancestor, but it yields any member
3981 -- of the class, and it is not possible to determine whether
3982 -- it is an ancestor of the extension aggregate (much less
3983 -- which ancestor). It is not possible to determine the
3984 -- components of the extension part.
3986 -- This check implements AI-306, which in fact was motivated by
3987 -- an AdaCore query to the ARG after this test was added.
3989 Error_Msg_N ("ancestor part must be statically tagged", A);
3990 else
3991 -- We are using the build-in-place protocol, but we can't build
3992 -- in place, because we need to call the function before
3993 -- allocating the aggregate. Could do better for null
3994 -- extensions, and maybe for nondiscriminated types.
3995 -- This is wrong for limited, but those were wrong already.
3997 if not Is_Limited_View (A_Type)
3998 and then Is_Build_In_Place_Function_Call (A)
3999 then
4000 Transform_BIP_Assignment (A_Type);
4001 end if;
4003 Resolve_Record_Aggregate (N, Typ);
4004 end if;
4005 end if;
4007 else
4008 Error_Msg_N ("no unique type for this aggregate", A);
4009 end if;
4011 Check_Function_Writable_Actuals (N);
4012 end Resolve_Extension_Aggregate;
4014 ----------------------------------
4015 -- Resolve_Null_Array_Aggregate --
4016 ----------------------------------
4018 function Resolve_Null_Array_Aggregate (N : Node_Id) return Boolean is
4019 -- Never returns False, but declared as a function to match
4020 -- other Resolve_Mumble functions.
4022 Loc : constant Source_Ptr := Sloc (N);
4023 Typ : constant Entity_Id := Etype (N);
4025 Check : Node_Id;
4026 Decl : Node_Id;
4027 Index : Node_Id;
4028 Lo, Hi : Node_Id;
4029 Constr : constant List_Id := New_List;
4030 Subt : constant Entity_Id := Make_Temporary (Loc, 'S');
4032 begin
4033 -- Create a constrained subtype with null dimensions
4035 Index := First_Index (Typ);
4036 while Present (Index) loop
4037 Get_Index_Bounds (Index, L => Lo, H => Hi);
4039 -- The upper bound is the predecessor of the lower bound
4041 Hi := Make_Attribute_Reference
4042 (Loc,
4043 Prefix => New_Occurrence_Of (Etype (Index), Loc),
4044 Attribute_Name => Name_Pred,
4045 Expressions => New_List (New_Copy_Tree (Lo)));
4047 -- Check that high bound (i.e., low bound predecessor) exists.
4048 -- Fail if low bound is low bound of base subtype (in all cases,
4049 -- including modular).
4051 Check :=
4052 Make_If_Statement (Loc,
4053 Condition =>
4054 Make_Op_Le (Loc, New_Copy_Tree (Lo), New_Copy_Tree (Hi)),
4055 Then_Statements =>
4056 New_List (Make_Raise_Constraint_Error
4057 (Loc, Reason => CE_Range_Check_Failed)));
4059 Insert_Action (N, Check);
4061 Append (Make_Range (Loc, Lo, Hi), Constr);
4063 Index := Next_Index (Index);
4064 end loop;
4066 Decl := Make_Subtype_Declaration (Loc,
4067 Defining_Identifier => Subt,
4068 Subtype_Indication =>
4069 Make_Subtype_Indication (Loc,
4070 Subtype_Mark =>
4071 New_Occurrence_Of (Base_Type (Typ), Loc),
4072 Constraint =>
4073 Make_Index_Or_Discriminant_Constraint (Loc, Constr)));
4075 Insert_Action (N, Decl);
4076 Set_Is_Internal (Subt);
4077 Analyze (Decl);
4078 Set_Etype (N, Subt);
4079 Set_Compile_Time_Known_Aggregate (N);
4080 Set_Aggregate_Bounds (N, New_Copy_Tree (First_Index (Etype (N))));
4082 return True;
4083 end Resolve_Null_Array_Aggregate;
4085 ------------------------------
4086 -- Resolve_Record_Aggregate --
4087 ------------------------------
4089 procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id) is
4090 New_Assoc_List : constant List_Id := New_List;
4091 -- New_Assoc_List is the newly built list of N_Component_Association
4092 -- nodes.
4094 Others_Etype : Entity_Id := Empty;
4095 -- This variable is used to save the Etype of the last record component
4096 -- that takes its value from the others choice. Its purpose is:
4098 -- (a) make sure the others choice is useful
4100 -- (b) make sure the type of all the components whose value is
4101 -- subsumed by the others choice are the same.
4103 -- This variable is updated as a side effect of function Get_Value.
4105 Box_Node : Node_Id := Empty;
4106 Is_Box_Present : Boolean := False;
4107 Is_Box_Init_By_Default : Boolean := False;
4108 Others_Box : Natural := 0;
4109 -- Ada 2005 (AI-287): Variables used in case of default initialization
4110 -- to provide a functionality similar to Others_Etype. Box_Present
4111 -- indicates that the component takes its default initialization;
4112 -- Others_Box counts the number of components of the current aggregate
4113 -- (which may be a sub-aggregate of a larger one) that are default-
4114 -- initialized. A value of One indicates that an others_box is present.
4115 -- Any larger value indicates that the others_box is not redundant.
4116 -- These variables, similar to Others_Etype, are also updated as a side
4117 -- effect of function Get_Value. Box_Node is used to place a warning on
4118 -- a redundant others_box.
4120 procedure Add_Association
4121 (Component : Entity_Id;
4122 Expr : Node_Id;
4123 Assoc_List : List_Id;
4124 Is_Box_Present : Boolean := False);
4125 -- Builds a new N_Component_Association node which associates Component
4126 -- to expression Expr and adds it to the association list being built,
4127 -- either New_Assoc_List, or the association being built for an inner
4128 -- aggregate.
4130 procedure Add_Discriminant_Values
4131 (New_Aggr : Node_Id;
4132 Assoc_List : List_Id);
4133 -- The constraint to a component may be given by a discriminant of the
4134 -- enclosing type, in which case we have to retrieve its value, which is
4135 -- part of the enclosing aggregate. Assoc_List provides the discriminant
4136 -- associations of the current type or of some enclosing record.
4138 function Discriminant_Present (Input_Discr : Entity_Id) return Boolean;
4139 -- If aggregate N is a regular aggregate this routine will return True.
4140 -- Otherwise, if N is an extension aggregate, then Input_Discr denotes
4141 -- a discriminant whose value may already have been specified by N's
4142 -- ancestor part. This routine checks whether this is indeed the case
4143 -- and if so returns False, signaling that no value for Input_Discr
4144 -- should appear in N's aggregate part. Also, in this case, the routine
4145 -- appends to New_Assoc_List the discriminant value specified in the
4146 -- ancestor part.
4148 -- If the aggregate is in a context with expansion delayed, it will be
4149 -- reanalyzed. The inherited discriminant values must not be reinserted
4150 -- in the component list to prevent spurious errors, but they must be
4151 -- present on first analysis to build the proper subtype indications.
4152 -- The flag Inherited_Discriminant is used to prevent the re-insertion.
4154 function Find_Private_Ancestor (Typ : Entity_Id) return Entity_Id;
4155 -- AI05-0115: Find earlier ancestor in the derivation chain that is
4156 -- derived from private view Typ. Whether the aggregate is legal depends
4157 -- on the current visibility of the type as well as that of the parent
4158 -- of the ancestor.
4160 function Get_Value
4161 (Compon : Entity_Id;
4162 From : List_Id;
4163 Consider_Others_Choice : Boolean := False) return Node_Id;
4164 -- Given a record component stored in parameter Compon, this function
4165 -- returns its value as it appears in the list From, which is a list
4166 -- of N_Component_Association nodes.
4168 -- If no component association has a choice for the searched component,
4169 -- the value provided by the others choice is returned, if there is one,
4170 -- and Consider_Others_Choice is set to true. Otherwise Empty is
4171 -- returned. If there is more than one component association giving a
4172 -- value for the searched record component, an error message is emitted
4173 -- and the first found value is returned.
4175 -- If Consider_Others_Choice is set and the returned expression comes
4176 -- from the others choice, then Others_Etype is set as a side effect.
4177 -- An error message is emitted if the components taking their value from
4178 -- the others choice do not have same type.
4180 procedure Propagate_Discriminants
4181 (Aggr : Node_Id;
4182 Assoc_List : List_Id);
4183 -- Nested components may themselves be discriminated types constrained
4184 -- by outer discriminants, whose values must be captured before the
4185 -- aggregate is expanded into assignments.
4187 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Entity_Id);
4188 -- Analyzes and resolves expression Expr against the Etype of the
4189 -- Component. This routine also applies all appropriate checks to Expr.
4190 -- It finally saves a Expr in the newly created association list that
4191 -- will be attached to the final record aggregate. Note that if the
4192 -- Parent pointer of Expr is not set then Expr was produced with a
4193 -- New_Copy_Tree or some such.
4195 procedure Rewrite_Range (Root_Type : Entity_Id; Rge : Node_Id);
4196 -- Rewrite a range node Rge when its bounds refer to non-stored
4197 -- discriminants from Root_Type, to replace them with the stored
4198 -- discriminant values. This is required in GNATprove mode, and is
4199 -- adopted in all modes to avoid special-casing GNATprove mode.
4201 ---------------------
4202 -- Add_Association --
4203 ---------------------
4205 procedure Add_Association
4206 (Component : Entity_Id;
4207 Expr : Node_Id;
4208 Assoc_List : List_Id;
4209 Is_Box_Present : Boolean := False)
4211 Choice_List : constant List_Id := New_List;
4212 Loc : Source_Ptr;
4214 begin
4215 -- If this is a box association the expression is missing, so use the
4216 -- Sloc of the aggregate itself for the new association.
4218 pragma Assert (Present (Expr) xor Is_Box_Present);
4220 if Present (Expr) then
4221 Loc := Sloc (Expr);
4222 else
4223 Loc := Sloc (N);
4224 end if;
4226 Append_To (Choice_List, New_Occurrence_Of (Component, Loc));
4228 Append_To (Assoc_List,
4229 Make_Component_Association (Loc,
4230 Choices => Choice_List,
4231 Expression => Expr,
4232 Box_Present => Is_Box_Present));
4234 -- If this association has a box for a component that is initialized
4235 -- by default, then set flag on the new association to indicate that
4236 -- the original association was for such a box-initialized component.
4238 if Is_Box_Init_By_Default then
4239 Set_Was_Default_Init_Box_Association (Last (Assoc_List));
4240 end if;
4241 end Add_Association;
4243 -----------------------------
4244 -- Add_Discriminant_Values --
4245 -----------------------------
4247 procedure Add_Discriminant_Values
4248 (New_Aggr : Node_Id;
4249 Assoc_List : List_Id)
4251 Assoc : Node_Id;
4252 Discr : Entity_Id;
4253 Discr_Elmt : Elmt_Id;
4254 Discr_Val : Node_Id;
4255 Val : Entity_Id;
4257 begin
4258 Discr := First_Discriminant (Etype (New_Aggr));
4259 Discr_Elmt := First_Elmt (Discriminant_Constraint (Etype (New_Aggr)));
4260 while Present (Discr_Elmt) loop
4261 Discr_Val := Node (Discr_Elmt);
4263 -- If the constraint is given by a discriminant then it is a
4264 -- discriminant of an enclosing record, and its value has already
4265 -- been placed in the association list.
4267 if Is_Entity_Name (Discr_Val)
4268 and then Ekind (Entity (Discr_Val)) = E_Discriminant
4269 then
4270 Val := Entity (Discr_Val);
4272 Assoc := First (Assoc_List);
4273 while Present (Assoc) loop
4274 if Present (Entity (First (Choices (Assoc))))
4275 and then Entity (First (Choices (Assoc))) = Val
4276 then
4277 Discr_Val := Expression (Assoc);
4278 exit;
4279 end if;
4281 Next (Assoc);
4282 end loop;
4283 end if;
4285 Add_Association
4286 (Discr, New_Copy_Tree (Discr_Val),
4287 Component_Associations (New_Aggr));
4289 -- If the discriminant constraint is a current instance, mark the
4290 -- current aggregate so that the self-reference can be expanded
4291 -- later. The constraint may refer to the subtype of aggregate, so
4292 -- use base type for comparison.
4294 if Nkind (Discr_Val) = N_Attribute_Reference
4295 and then Is_Entity_Name (Prefix (Discr_Val))
4296 and then Is_Type (Entity (Prefix (Discr_Val)))
4297 and then Base_Type (Etype (N)) = Entity (Prefix (Discr_Val))
4298 then
4299 Set_Has_Self_Reference (N);
4300 end if;
4302 Next_Elmt (Discr_Elmt);
4303 Next_Discriminant (Discr);
4304 end loop;
4305 end Add_Discriminant_Values;
4307 --------------------------
4308 -- Discriminant_Present --
4309 --------------------------
4311 function Discriminant_Present (Input_Discr : Entity_Id) return Boolean is
4312 Regular_Aggr : constant Boolean := Nkind (N) /= N_Extension_Aggregate;
4314 Ancestor_Is_Subtyp : Boolean;
4316 Loc : Source_Ptr;
4318 Ancestor : Node_Id;
4319 Ancestor_Typ : Entity_Id;
4320 Comp_Assoc : Node_Id;
4321 Discr : Entity_Id;
4322 Discr_Expr : Node_Id;
4323 Discr_Val : Elmt_Id := No_Elmt;
4324 Orig_Discr : Entity_Id;
4326 begin
4327 if Regular_Aggr then
4328 return True;
4329 end if;
4331 -- Check whether inherited discriminant values have already been
4332 -- inserted in the aggregate. This will be the case if we are
4333 -- re-analyzing an aggregate whose expansion was delayed.
4335 if Present (Component_Associations (N)) then
4336 Comp_Assoc := First (Component_Associations (N));
4337 while Present (Comp_Assoc) loop
4338 if Inherited_Discriminant (Comp_Assoc) then
4339 return True;
4340 end if;
4342 Next (Comp_Assoc);
4343 end loop;
4344 end if;
4346 Ancestor := Ancestor_Part (N);
4347 Ancestor_Typ := Etype (Ancestor);
4348 Loc := Sloc (Ancestor);
4350 -- For a private type with unknown discriminants, use the underlying
4351 -- record view if it is available.
4353 if Has_Unknown_Discriminants (Ancestor_Typ)
4354 and then Present (Full_View (Ancestor_Typ))
4355 and then Present (Underlying_Record_View (Full_View (Ancestor_Typ)))
4356 then
4357 Ancestor_Typ := Underlying_Record_View (Full_View (Ancestor_Typ));
4358 end if;
4360 Ancestor_Is_Subtyp :=
4361 Is_Entity_Name (Ancestor) and then Is_Type (Entity (Ancestor));
4363 -- If the ancestor part has no discriminants clearly N's aggregate
4364 -- part must provide a value for Discr.
4366 if not Has_Discriminants (Ancestor_Typ) then
4367 return True;
4369 -- If the ancestor part is an unconstrained subtype mark then the
4370 -- Discr must be present in N's aggregate part.
4372 elsif Ancestor_Is_Subtyp
4373 and then not Is_Constrained (Entity (Ancestor))
4374 then
4375 return True;
4376 end if;
4378 -- Now look to see if Discr was specified in the ancestor part
4380 if Ancestor_Is_Subtyp then
4381 Discr_Val :=
4382 First_Elmt (Discriminant_Constraint (Entity (Ancestor)));
4383 end if;
4385 Orig_Discr := Original_Record_Component (Input_Discr);
4387 Discr := First_Discriminant (Ancestor_Typ);
4388 while Present (Discr) loop
4390 -- If Ancestor has already specified Disc value then insert its
4391 -- value in the final aggregate.
4393 if Original_Record_Component (Discr) = Orig_Discr then
4394 if Ancestor_Is_Subtyp then
4395 Discr_Expr := New_Copy_Tree (Node (Discr_Val));
4396 else
4397 Discr_Expr :=
4398 Make_Selected_Component (Loc,
4399 Prefix => Duplicate_Subexpr (Ancestor),
4400 Selector_Name => New_Occurrence_Of (Input_Discr, Loc));
4401 end if;
4403 Resolve_Aggr_Expr (Discr_Expr, Input_Discr);
4404 Set_Inherited_Discriminant (Last (New_Assoc_List));
4405 return False;
4406 end if;
4408 Next_Discriminant (Discr);
4410 if Ancestor_Is_Subtyp then
4411 Next_Elmt (Discr_Val);
4412 end if;
4413 end loop;
4415 return True;
4416 end Discriminant_Present;
4418 ---------------------------
4419 -- Find_Private_Ancestor --
4420 ---------------------------
4422 function Find_Private_Ancestor (Typ : Entity_Id) return Entity_Id is
4423 Par : Entity_Id;
4425 begin
4426 Par := Typ;
4427 loop
4428 if Has_Private_Ancestor (Par)
4429 and then not Has_Private_Ancestor (Etype (Base_Type (Par)))
4430 then
4431 return Par;
4433 elsif not Is_Derived_Type (Par) then
4434 return Empty;
4436 else
4437 Par := Etype (Base_Type (Par));
4438 end if;
4439 end loop;
4440 end Find_Private_Ancestor;
4442 ---------------
4443 -- Get_Value --
4444 ---------------
4446 function Get_Value
4447 (Compon : Entity_Id;
4448 From : List_Id;
4449 Consider_Others_Choice : Boolean := False) return Node_Id
4451 Typ : constant Entity_Id := Etype (Compon);
4452 Assoc : Node_Id;
4453 Expr : Node_Id := Empty;
4454 Selector_Name : Node_Id;
4456 begin
4457 Is_Box_Present := False;
4458 Is_Box_Init_By_Default := False;
4460 if No (From) then
4461 return Empty;
4462 end if;
4464 Assoc := First (From);
4465 while Present (Assoc) loop
4466 Selector_Name := First (Choices (Assoc));
4467 while Present (Selector_Name) loop
4468 if Nkind (Selector_Name) = N_Others_Choice then
4469 if Consider_Others_Choice and then No (Expr) then
4471 -- We need to duplicate the expression for each
4472 -- successive component covered by the others choice.
4473 -- This is redundant if the others_choice covers only
4474 -- one component (small optimization possible???), but
4475 -- indispensable otherwise, because each one must be
4476 -- expanded individually to preserve side effects.
4478 -- Ada 2005 (AI-287): In case of default initialization
4479 -- of components, we duplicate the corresponding default
4480 -- expression (from the record type declaration). The
4481 -- copy must carry the sloc of the association (not the
4482 -- original expression) to prevent spurious elaboration
4483 -- checks when the default includes function calls.
4485 if Box_Present (Assoc) then
4486 Others_Box := Others_Box + 1;
4487 Is_Box_Present := True;
4489 if Expander_Active then
4490 return
4491 New_Copy_Tree_And_Copy_Dimensions
4492 (Expression (Parent (Compon)),
4493 New_Sloc => Sloc (Assoc));
4494 else
4495 return Expression (Parent (Compon));
4496 end if;
4498 else
4499 if Present (Others_Etype)
4500 and then Base_Type (Others_Etype) /= Base_Type (Typ)
4501 then
4502 -- If the components are of an anonymous access
4503 -- type they are distinct, but this is legal in
4504 -- Ada 2012 as long as designated types match.
4506 if (Ekind (Typ) = E_Anonymous_Access_Type
4507 or else Ekind (Typ) =
4508 E_Anonymous_Access_Subprogram_Type)
4509 and then Designated_Type (Typ) =
4510 Designated_Type (Others_Etype)
4511 then
4512 null;
4513 else
4514 Error_Msg_N
4515 ("components in OTHERS choice must have same "
4516 & "type", Selector_Name);
4517 end if;
4518 end if;
4520 Others_Etype := Typ;
4522 -- Copy the expression so that it is resolved
4523 -- independently for each component, This is needed
4524 -- for accessibility checks on components of anonymous
4525 -- access types, even in compile_only mode.
4527 if not Inside_A_Generic then
4528 return
4529 New_Copy_Tree_And_Copy_Dimensions
4530 (Expression (Assoc));
4531 else
4532 return Expression (Assoc);
4533 end if;
4534 end if;
4535 end if;
4537 elsif Chars (Compon) = Chars (Selector_Name) then
4538 if No (Expr) then
4540 -- Ada 2005 (AI-231)
4542 if Ada_Version >= Ada_2005
4543 and then Known_Null (Expression (Assoc))
4544 then
4545 Check_Can_Never_Be_Null (Compon, Expression (Assoc));
4546 end if;
4548 -- We need to duplicate the expression when several
4549 -- components are grouped together with a "|" choice.
4550 -- For instance "filed1 | filed2 => Expr"
4552 -- Ada 2005 (AI-287)
4554 if Box_Present (Assoc) then
4555 Is_Box_Present := True;
4557 -- Duplicate the default expression of the component
4558 -- from the record type declaration, so a new copy
4559 -- can be attached to the association.
4561 -- Note that we always copy the default expression,
4562 -- even when the association has a single choice, in
4563 -- order to create a proper association for the
4564 -- expanded aggregate.
4566 -- Component may have no default, in which case the
4567 -- expression is empty and the component is default-
4568 -- initialized, but an association for the component
4569 -- exists, and it is not covered by an others clause.
4571 -- Scalar and private types have no initialization
4572 -- procedure, so they remain uninitialized. If the
4573 -- target of the aggregate is a constant this
4574 -- deserves a warning.
4576 if No (Expression (Parent (Compon)))
4577 and then not Has_Non_Null_Base_Init_Proc (Typ)
4578 and then not Has_Aspect (Typ, Aspect_Default_Value)
4579 and then not Is_Concurrent_Type (Typ)
4580 and then Nkind (Parent (N)) = N_Object_Declaration
4581 and then Constant_Present (Parent (N))
4582 then
4583 Error_Msg_Node_2 := Typ;
4584 Error_Msg_NE
4585 ("component&? of type& is uninitialized",
4586 Assoc, Selector_Name);
4588 -- An additional reminder if the component type
4589 -- is a generic formal.
4591 if Is_Generic_Type (Base_Type (Typ)) then
4592 Error_Msg_NE
4593 ("\instance should provide actual type with "
4594 & "initialization for&", Assoc, Typ);
4595 end if;
4596 end if;
4598 return
4599 New_Copy_Tree_And_Copy_Dimensions
4600 (Expression (Parent (Compon)));
4602 else
4603 if Present (Next (Selector_Name)) then
4604 Expr := New_Copy_Tree_And_Copy_Dimensions
4605 (Expression (Assoc));
4606 else
4607 Expr := Expression (Assoc);
4608 end if;
4609 end if;
4611 Generate_Reference (Compon, Selector_Name, 'm');
4613 else
4614 Error_Msg_NE
4615 ("more than one value supplied for &",
4616 Selector_Name, Compon);
4618 end if;
4619 end if;
4621 Next (Selector_Name);
4622 end loop;
4624 Next (Assoc);
4625 end loop;
4627 return Expr;
4628 end Get_Value;
4630 -----------------------------
4631 -- Propagate_Discriminants --
4632 -----------------------------
4634 procedure Propagate_Discriminants
4635 (Aggr : Node_Id;
4636 Assoc_List : List_Id)
4638 Loc : constant Source_Ptr := Sloc (N);
4640 procedure Process_Component (Comp : Entity_Id);
4641 -- Add one component with a box association to the inner aggregate,
4642 -- and recurse if component is itself composite.
4644 -----------------------
4645 -- Process_Component --
4646 -----------------------
4648 procedure Process_Component (Comp : Entity_Id) is
4649 T : constant Entity_Id := Etype (Comp);
4650 New_Aggr : Node_Id;
4652 begin
4653 if Is_Record_Type (T) and then Has_Discriminants (T) then
4654 New_Aggr := Make_Aggregate (Loc, No_List, New_List);
4655 Set_Etype (New_Aggr, T);
4657 Add_Association
4658 (Comp, New_Aggr, Component_Associations (Aggr));
4660 -- Collect discriminant values and recurse
4662 Add_Discriminant_Values (New_Aggr, Assoc_List);
4663 Propagate_Discriminants (New_Aggr, Assoc_List);
4665 Build_Constrained_Itype
4666 (New_Aggr, T, Component_Associations (New_Aggr));
4667 else
4668 Add_Association
4669 (Comp, Empty, Component_Associations (Aggr),
4670 Is_Box_Present => True);
4671 end if;
4672 end Process_Component;
4674 -- Local variables
4676 Aggr_Type : constant Entity_Id := Base_Type (Etype (Aggr));
4677 Components : constant Elist_Id := New_Elmt_List;
4678 Def_Node : constant Node_Id :=
4679 Type_Definition (Declaration_Node (Aggr_Type));
4681 Comp : Node_Id;
4682 Comp_Elmt : Elmt_Id;
4683 Errors : Boolean;
4685 -- Start of processing for Propagate_Discriminants
4687 begin
4688 -- The component type may be a variant type. Collect the components
4689 -- that are ruled by the known values of the discriminants. Their
4690 -- values have already been inserted into the component list of the
4691 -- current aggregate.
4693 if Nkind (Def_Node) = N_Record_Definition
4694 and then Present (Component_List (Def_Node))
4695 and then Present (Variant_Part (Component_List (Def_Node)))
4696 then
4697 Gather_Components (Aggr_Type,
4698 Component_List (Def_Node),
4699 Governed_By => Component_Associations (Aggr),
4700 Into => Components,
4701 Report_Errors => Errors);
4703 Comp_Elmt := First_Elmt (Components);
4704 while Present (Comp_Elmt) loop
4705 if Ekind (Node (Comp_Elmt)) /= E_Discriminant then
4706 Process_Component (Node (Comp_Elmt));
4707 end if;
4709 Next_Elmt (Comp_Elmt);
4710 end loop;
4712 -- No variant part, iterate over all components
4714 else
4715 Comp := First_Component (Etype (Aggr));
4716 while Present (Comp) loop
4717 Process_Component (Comp);
4718 Next_Component (Comp);
4719 end loop;
4720 end if;
4721 end Propagate_Discriminants;
4723 -----------------------
4724 -- Resolve_Aggr_Expr --
4725 -----------------------
4727 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Entity_Id) is
4728 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean;
4729 -- If the expression is an aggregate (possibly qualified) then its
4730 -- expansion is delayed until the enclosing aggregate is expanded
4731 -- into assignments. In that case, do not generate checks on the
4732 -- expression, because they will be generated later, and will other-
4733 -- wise force a copy (to remove side effects) that would leave a
4734 -- dynamic-sized aggregate in the code, something that gigi cannot
4735 -- handle.
4737 ---------------------------
4738 -- Has_Expansion_Delayed --
4739 ---------------------------
4741 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean is
4742 begin
4743 return
4744 (Nkind (Expr) in N_Aggregate | N_Extension_Aggregate
4745 and then Present (Etype (Expr))
4746 and then Is_Record_Type (Etype (Expr))
4747 and then Expansion_Delayed (Expr))
4748 or else
4749 (Nkind (Expr) = N_Qualified_Expression
4750 and then Has_Expansion_Delayed (Expression (Expr)));
4751 end Has_Expansion_Delayed;
4753 -- Local variables
4755 Expr_Type : Entity_Id := Empty;
4756 New_C : Entity_Id := Component;
4757 New_Expr : Node_Id;
4759 Relocate : Boolean;
4760 -- Set to True if the resolved Expr node needs to be relocated when
4761 -- attached to the newly created association list. This node need not
4762 -- be relocated if its parent pointer is not set. In fact in this
4763 -- case Expr is the output of a New_Copy_Tree call. If Relocate is
4764 -- True then we have analyzed the expression node in the original
4765 -- aggregate and hence it needs to be relocated when moved over to
4766 -- the new association list.
4768 -- Start of processing for Resolve_Aggr_Expr
4770 begin
4771 -- If the type of the component is elementary or the type of the
4772 -- aggregate does not contain discriminants, use the type of the
4773 -- component to resolve Expr.
4775 if Is_Elementary_Type (Etype (Component))
4776 or else not Has_Discriminants (Etype (N))
4777 then
4778 Expr_Type := Etype (Component);
4780 -- Otherwise we have to pick up the new type of the component from
4781 -- the new constrained subtype of the aggregate. In fact components
4782 -- which are of a composite type might be constrained by a
4783 -- discriminant, and we want to resolve Expr against the subtype were
4784 -- all discriminant occurrences are replaced with their actual value.
4786 else
4787 New_C := First_Component (Etype (N));
4788 while Present (New_C) loop
4789 if Chars (New_C) = Chars (Component) then
4790 Expr_Type := Etype (New_C);
4791 exit;
4792 end if;
4794 Next_Component (New_C);
4795 end loop;
4797 pragma Assert (Present (Expr_Type));
4799 -- For each range in an array type where a discriminant has been
4800 -- replaced with the constraint, check that this range is within
4801 -- the range of the base type. This checks is done in the init
4802 -- proc for regular objects, but has to be done here for
4803 -- aggregates since no init proc is called for them.
4805 if Is_Array_Type (Expr_Type) then
4806 declare
4807 Index : Node_Id;
4808 -- Range of the current constrained index in the array
4810 Orig_Index : Node_Id := First_Index (Etype (Component));
4811 -- Range corresponding to the range Index above in the
4812 -- original unconstrained record type. The bounds of this
4813 -- range may be governed by discriminants.
4815 Unconstr_Index : Node_Id := First_Index (Etype (Expr_Type));
4816 -- Range corresponding to the range Index above for the
4817 -- unconstrained array type. This range is needed to apply
4818 -- range checks.
4820 begin
4821 Index := First_Index (Expr_Type);
4822 while Present (Index) loop
4823 if Depends_On_Discriminant (Orig_Index) then
4824 Apply_Range_Check (Index, Etype (Unconstr_Index));
4825 end if;
4827 Next_Index (Index);
4828 Next_Index (Orig_Index);
4829 Next_Index (Unconstr_Index);
4830 end loop;
4831 end;
4832 end if;
4833 end if;
4835 -- If the Parent pointer of Expr is not set, Expr is an expression
4836 -- duplicated by New_Tree_Copy (this happens for record aggregates
4837 -- that look like (Field1 | Filed2 => Expr) or (others => Expr)).
4838 -- Such a duplicated expression must be attached to the tree
4839 -- before analysis and resolution to enforce the rule that a tree
4840 -- fragment should never be analyzed or resolved unless it is
4841 -- attached to the current compilation unit.
4843 if No (Parent (Expr)) then
4844 Set_Parent (Expr, N);
4845 Relocate := False;
4846 else
4847 Relocate := True;
4848 end if;
4850 Analyze_And_Resolve (Expr, Expr_Type);
4851 Check_Expr_OK_In_Limited_Aggregate (Expr);
4852 Check_Non_Static_Context (Expr);
4853 Check_Unset_Reference (Expr);
4855 -- Check wrong use of class-wide types
4857 if Is_Class_Wide_Type (Etype (Expr)) then
4858 Error_Msg_N ("dynamically tagged expression not allowed", Expr);
4859 end if;
4861 if not Has_Expansion_Delayed (Expr) then
4862 Aggregate_Constraint_Checks (Expr, Expr_Type);
4863 end if;
4865 -- If an aggregate component has a type with predicates, an explicit
4866 -- predicate check must be applied, as for an assignment statement,
4867 -- because the aggregate might not be expanded into individual
4868 -- component assignments.
4870 if Has_Predicates (Expr_Type)
4871 and then Analyzed (Expr)
4872 then
4873 Apply_Predicate_Check (Expr, Expr_Type);
4874 end if;
4876 if Raises_Constraint_Error (Expr) then
4877 Set_Raises_Constraint_Error (N);
4878 end if;
4880 -- If the expression has been marked as requiring a range check, then
4881 -- generate it here. It's a bit odd to be generating such checks in
4882 -- the analyzer, but harmless since Generate_Range_Check does nothing
4883 -- (other than making sure Do_Range_Check is set) if the expander is
4884 -- not active.
4886 if Do_Range_Check (Expr) then
4887 Generate_Range_Check (Expr, Expr_Type, CE_Range_Check_Failed);
4888 end if;
4890 -- Add association Component => Expr if the caller requests it
4892 if Relocate then
4893 New_Expr := Relocate_Node (Expr);
4895 -- Since New_Expr is not gonna be analyzed later on, we need to
4896 -- propagate here the dimensions form Expr to New_Expr.
4898 Copy_Dimensions (Expr, New_Expr);
4900 else
4901 New_Expr := Expr;
4902 end if;
4904 Add_Association (New_C, New_Expr, New_Assoc_List);
4905 end Resolve_Aggr_Expr;
4907 -------------------
4908 -- Rewrite_Range --
4909 -------------------
4911 procedure Rewrite_Range (Root_Type : Entity_Id; Rge : Node_Id) is
4912 procedure Rewrite_Bound
4913 (Bound : Node_Id;
4914 Disc : Entity_Id;
4915 Expr_Disc : Node_Id);
4916 -- Rewrite a bound of the range Bound, when it is equal to the
4917 -- non-stored discriminant Disc, into the stored discriminant
4918 -- value Expr_Disc.
4920 -------------------
4921 -- Rewrite_Bound --
4922 -------------------
4924 procedure Rewrite_Bound
4925 (Bound : Node_Id;
4926 Disc : Entity_Id;
4927 Expr_Disc : Node_Id)
4929 begin
4930 if Nkind (Bound) /= N_Identifier then
4931 return;
4932 end if;
4934 -- We expect either the discriminant or the discriminal
4936 if Entity (Bound) = Disc
4937 or else (Ekind (Entity (Bound)) = E_In_Parameter
4938 and then Discriminal_Link (Entity (Bound)) = Disc)
4939 then
4940 Rewrite (Bound, New_Copy_Tree (Expr_Disc));
4941 end if;
4942 end Rewrite_Bound;
4944 -- Local variables
4946 Low, High : Node_Id;
4947 Disc : Entity_Id;
4948 Expr_Disc : Elmt_Id;
4950 -- Start of processing for Rewrite_Range
4952 begin
4953 if Has_Discriminants (Root_Type) and then Nkind (Rge) = N_Range then
4954 Low := Low_Bound (Rge);
4955 High := High_Bound (Rge);
4957 Disc := First_Discriminant (Root_Type);
4958 Expr_Disc := First_Elmt (Stored_Constraint (Etype (N)));
4959 while Present (Disc) loop
4960 Rewrite_Bound (Low, Disc, Node (Expr_Disc));
4961 Rewrite_Bound (High, Disc, Node (Expr_Disc));
4962 Next_Discriminant (Disc);
4963 Next_Elmt (Expr_Disc);
4964 end loop;
4965 end if;
4966 end Rewrite_Range;
4968 -- Local variables
4970 Components : constant Elist_Id := New_Elmt_List;
4971 -- Components is the list of the record components whose value must be
4972 -- provided in the aggregate. This list does include discriminants.
4974 Component : Entity_Id;
4975 Component_Elmt : Elmt_Id;
4976 Expr : Node_Id;
4977 Positional_Expr : Node_Id;
4979 -- Start of processing for Resolve_Record_Aggregate
4981 begin
4982 -- A record aggregate is restricted in SPARK:
4984 -- Each named association can have only a single choice.
4985 -- OTHERS cannot be used.
4986 -- Positional and named associations cannot be mixed.
4988 if Present (Component_Associations (N))
4989 and then Present (First (Component_Associations (N)))
4990 then
4991 declare
4992 Assoc : Node_Id;
4994 begin
4995 Assoc := First (Component_Associations (N));
4996 while Present (Assoc) loop
4997 if Nkind (Assoc) = N_Iterated_Component_Association then
4998 Error_Msg_N
4999 ("iterated component association can only appear in an "
5000 & "array aggregate", N);
5001 raise Unrecoverable_Error;
5002 end if;
5004 Next (Assoc);
5005 end loop;
5006 end;
5007 end if;
5009 -- We may end up calling Duplicate_Subexpr on expressions that are
5010 -- attached to New_Assoc_List. For this reason we need to attach it
5011 -- to the tree by setting its parent pointer to N. This parent point
5012 -- will change in STEP 8 below.
5014 Set_Parent (New_Assoc_List, N);
5016 -- STEP 1: abstract type and null record verification
5018 if Is_Abstract_Type (Typ) then
5019 Error_Msg_N ("type of aggregate cannot be abstract", N);
5020 end if;
5022 if No (First_Entity (Typ)) and then Null_Record_Present (N) then
5023 Set_Etype (N, Typ);
5024 return;
5026 elsif Present (First_Entity (Typ))
5027 and then Null_Record_Present (N)
5028 and then not Is_Tagged_Type (Typ)
5029 then
5030 Error_Msg_N ("record aggregate cannot be null", N);
5031 return;
5033 -- If the type has no components, then the aggregate should either
5034 -- have "null record", or in Ada 2005 it could instead have a single
5035 -- component association given by "others => <>". For Ada 95 we flag an
5036 -- error at this point, but for Ada 2005 we proceed with checking the
5037 -- associations below, which will catch the case where it's not an
5038 -- aggregate with "others => <>". Note that the legality of a <>
5039 -- aggregate for a null record type was established by AI05-016.
5041 elsif No (First_Entity (Typ))
5042 and then Ada_Version < Ada_2005
5043 then
5044 Error_Msg_N ("record aggregate must be null", N);
5045 return;
5046 end if;
5048 -- A record aggregate can only use parentheses
5050 if Nkind (N) = N_Aggregate
5051 and then Is_Homogeneous_Aggregate (N)
5052 then
5053 Error_Msg_N ("record aggregate must use (), not '[']", N);
5054 return;
5055 end if;
5057 -- STEP 2: Verify aggregate structure
5059 Step_2 : declare
5060 Assoc : Node_Id;
5061 Bad_Aggregate : Boolean := False;
5062 Selector_Name : Node_Id;
5064 begin
5065 if Present (Component_Associations (N)) then
5066 Assoc := First (Component_Associations (N));
5067 else
5068 Assoc := Empty;
5069 end if;
5071 while Present (Assoc) loop
5072 Selector_Name := First (Choices (Assoc));
5073 while Present (Selector_Name) loop
5074 if Nkind (Selector_Name) = N_Identifier then
5075 null;
5077 elsif Nkind (Selector_Name) = N_Others_Choice then
5078 if Selector_Name /= First (Choices (Assoc))
5079 or else Present (Next (Selector_Name))
5080 then
5081 Error_Msg_N
5082 ("OTHERS must appear alone in a choice list",
5083 Selector_Name);
5084 return;
5086 elsif Present (Next (Assoc)) then
5087 Error_Msg_N
5088 ("OTHERS must appear last in an aggregate",
5089 Selector_Name);
5090 return;
5092 -- (Ada 2005): If this is an association with a box,
5093 -- indicate that the association need not represent
5094 -- any component.
5096 elsif Box_Present (Assoc) then
5097 Others_Box := 1;
5098 Box_Node := Assoc;
5099 end if;
5101 else
5102 Error_Msg_N
5103 ("selector name should be identifier or OTHERS",
5104 Selector_Name);
5105 Bad_Aggregate := True;
5106 end if;
5108 Next (Selector_Name);
5109 end loop;
5111 Next (Assoc);
5112 end loop;
5114 if Bad_Aggregate then
5115 return;
5116 end if;
5117 end Step_2;
5119 -- STEP 3: Find discriminant Values
5121 Step_3 : declare
5122 Discrim : Entity_Id;
5123 Missing_Discriminants : Boolean := False;
5125 begin
5126 if Present (Expressions (N)) then
5127 Positional_Expr := First (Expressions (N));
5128 else
5129 Positional_Expr := Empty;
5130 end if;
5132 -- AI05-0115: if the ancestor part is a subtype mark, the ancestor
5133 -- must not have unknown discriminants.
5134 -- ??? We are not checking any subtype mark here and this code is not
5135 -- exercised by any test, so it's likely wrong (in particular
5136 -- we should not use Root_Type here but the subtype mark, if any),
5137 -- and possibly not needed.
5139 if Is_Derived_Type (Typ)
5140 and then Has_Unknown_Discriminants (Root_Type (Typ))
5141 and then Nkind (N) /= N_Extension_Aggregate
5142 then
5143 Error_Msg_NE
5144 ("aggregate not available for type& whose ancestor "
5145 & "has unknown discriminants", N, Typ);
5146 end if;
5148 if Has_Unknown_Discriminants (Typ)
5149 and then Present (Underlying_Record_View (Typ))
5150 then
5151 Discrim := First_Discriminant (Underlying_Record_View (Typ));
5152 elsif Has_Discriminants (Typ) then
5153 Discrim := First_Discriminant (Typ);
5154 else
5155 Discrim := Empty;
5156 end if;
5158 -- First find the discriminant values in the positional components
5160 while Present (Discrim) and then Present (Positional_Expr) loop
5161 if Discriminant_Present (Discrim) then
5162 Resolve_Aggr_Expr (Positional_Expr, Discrim);
5164 -- Ada 2005 (AI-231)
5166 if Ada_Version >= Ada_2005
5167 and then Known_Null (Positional_Expr)
5168 then
5169 Check_Can_Never_Be_Null (Discrim, Positional_Expr);
5170 end if;
5172 Next (Positional_Expr);
5173 end if;
5175 if Present (Get_Value (Discrim, Component_Associations (N))) then
5176 Error_Msg_NE
5177 ("more than one value supplied for discriminant&",
5178 N, Discrim);
5179 end if;
5181 Next_Discriminant (Discrim);
5182 end loop;
5184 -- Find remaining discriminant values if any among named components
5186 while Present (Discrim) loop
5187 Expr := Get_Value (Discrim, Component_Associations (N), True);
5189 if not Discriminant_Present (Discrim) then
5190 if Present (Expr) then
5191 Error_Msg_NE
5192 ("more than one value supplied for discriminant &",
5193 N, Discrim);
5194 end if;
5196 elsif No (Expr) then
5197 Error_Msg_NE
5198 ("no value supplied for discriminant &", N, Discrim);
5199 Missing_Discriminants := True;
5201 else
5202 Resolve_Aggr_Expr (Expr, Discrim);
5203 end if;
5205 Next_Discriminant (Discrim);
5206 end loop;
5208 if Missing_Discriminants then
5209 return;
5210 end if;
5212 -- At this point and until the beginning of STEP 6, New_Assoc_List
5213 -- contains only the discriminants and their values.
5215 end Step_3;
5217 -- STEP 4: Set the Etype of the record aggregate
5219 if Has_Discriminants (Typ)
5220 or else (Has_Unknown_Discriminants (Typ)
5221 and then Present (Underlying_Record_View (Typ)))
5222 then
5223 Build_Constrained_Itype (N, Typ, New_Assoc_List);
5224 else
5225 Set_Etype (N, Typ);
5226 end if;
5228 -- STEP 5: Get remaining components according to discriminant values
5230 Step_5 : declare
5231 Dnode : Node_Id;
5232 Errors_Found : Boolean := False;
5233 Record_Def : Node_Id;
5234 Parent_Typ : Entity_Id;
5235 Parent_Typ_List : Elist_Id;
5236 Parent_Elmt : Elmt_Id;
5237 Root_Typ : Entity_Id;
5239 begin
5240 if Is_Derived_Type (Typ) and then Is_Tagged_Type (Typ) then
5241 Parent_Typ_List := New_Elmt_List;
5243 -- If this is an extension aggregate, the component list must
5244 -- include all components that are not in the given ancestor type.
5245 -- Otherwise, the component list must include components of all
5246 -- ancestors, starting with the root.
5248 if Nkind (N) = N_Extension_Aggregate then
5249 Root_Typ := Base_Type (Etype (Ancestor_Part (N)));
5251 else
5252 -- AI05-0115: check legality of aggregate for type with a
5253 -- private ancestor.
5255 Root_Typ := Root_Type (Typ);
5256 if Has_Private_Ancestor (Typ) then
5257 declare
5258 Ancestor : constant Entity_Id :=
5259 Find_Private_Ancestor (Typ);
5260 Ancestor_Unit : constant Entity_Id :=
5261 Cunit_Entity
5262 (Get_Source_Unit (Ancestor));
5263 Parent_Unit : constant Entity_Id :=
5264 Cunit_Entity (Get_Source_Unit
5265 (Base_Type (Etype (Ancestor))));
5266 begin
5267 -- Check whether we are in a scope that has full view
5268 -- over the private ancestor and its parent. This can
5269 -- only happen if the derivation takes place in a child
5270 -- unit of the unit that declares the parent, and we are
5271 -- in the private part or body of that child unit, else
5272 -- the aggregate is illegal.
5274 if Is_Child_Unit (Ancestor_Unit)
5275 and then Scope (Ancestor_Unit) = Parent_Unit
5276 and then In_Open_Scopes (Scope (Ancestor))
5277 and then
5278 (In_Private_Part (Scope (Ancestor))
5279 or else In_Package_Body (Scope (Ancestor)))
5280 then
5281 null;
5283 else
5284 Error_Msg_NE
5285 ("type of aggregate has private ancestor&!",
5286 N, Root_Typ);
5287 Error_Msg_N ("must use extension aggregate!", N);
5288 return;
5289 end if;
5290 end;
5291 end if;
5293 Dnode := Declaration_Node (Base_Type (Root_Typ));
5295 -- If we don't get a full declaration, then we have some error
5296 -- which will get signalled later so skip this part. Otherwise
5297 -- gather components of root that apply to the aggregate type.
5298 -- We use the base type in case there is an applicable stored
5299 -- constraint that renames the discriminants of the root.
5301 if Nkind (Dnode) = N_Full_Type_Declaration then
5302 Record_Def := Type_Definition (Dnode);
5303 Gather_Components
5304 (Base_Type (Typ),
5305 Component_List (Record_Def),
5306 Governed_By => New_Assoc_List,
5307 Into => Components,
5308 Report_Errors => Errors_Found);
5310 if Errors_Found then
5311 Error_Msg_N
5312 ("discriminant controlling variant part is not static",
5314 return;
5315 end if;
5316 end if;
5317 end if;
5319 Parent_Typ := Base_Type (Typ);
5320 while Parent_Typ /= Root_Typ loop
5321 Prepend_Elmt (Parent_Typ, To => Parent_Typ_List);
5322 Parent_Typ := Etype (Parent_Typ);
5324 -- Check whether a private parent requires the use of
5325 -- an extension aggregate. This test does not apply in
5326 -- an instantiation: if the generic unit is legal so is
5327 -- the instance.
5329 if Nkind (Parent (Base_Type (Parent_Typ))) =
5330 N_Private_Type_Declaration
5331 or else Nkind (Parent (Base_Type (Parent_Typ))) =
5332 N_Private_Extension_Declaration
5333 then
5334 if Nkind (N) /= N_Extension_Aggregate
5335 and then not In_Instance
5336 then
5337 Error_Msg_NE
5338 ("type of aggregate has private ancestor&!",
5339 N, Parent_Typ);
5340 Error_Msg_N ("must use extension aggregate!", N);
5341 return;
5343 elsif Parent_Typ /= Root_Typ then
5344 Error_Msg_NE
5345 ("ancestor part of aggregate must be private type&",
5346 Ancestor_Part (N), Parent_Typ);
5347 return;
5348 end if;
5350 -- The current view of ancestor part may be a private type,
5351 -- while the context type is always non-private.
5353 elsif Is_Private_Type (Root_Typ)
5354 and then Present (Full_View (Root_Typ))
5355 and then Nkind (N) = N_Extension_Aggregate
5356 then
5357 exit when Base_Type (Full_View (Root_Typ)) = Parent_Typ;
5358 end if;
5359 end loop;
5361 -- Now collect components from all other ancestors, beginning
5362 -- with the current type. If the type has unknown discriminants
5363 -- use the component list of the Underlying_Record_View, which
5364 -- needs to be used for the subsequent expansion of the aggregate
5365 -- into assignments.
5367 Parent_Elmt := First_Elmt (Parent_Typ_List);
5368 while Present (Parent_Elmt) loop
5369 Parent_Typ := Node (Parent_Elmt);
5371 if Has_Unknown_Discriminants (Parent_Typ)
5372 and then Present (Underlying_Record_View (Typ))
5373 then
5374 Parent_Typ := Underlying_Record_View (Parent_Typ);
5375 end if;
5377 Record_Def := Type_Definition (Parent (Base_Type (Parent_Typ)));
5378 Gather_Components (Empty,
5379 Component_List (Record_Extension_Part (Record_Def)),
5380 Governed_By => New_Assoc_List,
5381 Into => Components,
5382 Report_Errors => Errors_Found);
5384 Next_Elmt (Parent_Elmt);
5385 end loop;
5387 -- Typ is not a derived tagged type
5389 else
5390 Record_Def := Type_Definition (Parent (Base_Type (Typ)));
5392 if Null_Present (Record_Def) then
5393 null;
5395 elsif not Has_Unknown_Discriminants (Typ) then
5396 Gather_Components
5397 (Base_Type (Typ),
5398 Component_List (Record_Def),
5399 Governed_By => New_Assoc_List,
5400 Into => Components,
5401 Report_Errors => Errors_Found);
5403 else
5404 Gather_Components
5405 (Base_Type (Underlying_Record_View (Typ)),
5406 Component_List (Record_Def),
5407 Governed_By => New_Assoc_List,
5408 Into => Components,
5409 Report_Errors => Errors_Found);
5410 end if;
5411 end if;
5413 if Errors_Found then
5414 return;
5415 end if;
5416 end Step_5;
5418 -- STEP 6: Find component Values
5420 Component := Empty;
5421 Component_Elmt := First_Elmt (Components);
5423 -- First scan the remaining positional associations in the aggregate.
5424 -- Remember that at this point Positional_Expr contains the current
5425 -- positional association if any is left after looking for discriminant
5426 -- values in step 3.
5428 while Present (Positional_Expr) and then Present (Component_Elmt) loop
5429 Component := Node (Component_Elmt);
5430 Resolve_Aggr_Expr (Positional_Expr, Component);
5432 -- Ada 2005 (AI-231)
5434 if Ada_Version >= Ada_2005 and then Known_Null (Positional_Expr) then
5435 Check_Can_Never_Be_Null (Component, Positional_Expr);
5436 end if;
5438 if Present (Get_Value (Component, Component_Associations (N))) then
5439 Error_Msg_NE
5440 ("more than one value supplied for component &", N, Component);
5441 end if;
5443 Next (Positional_Expr);
5444 Next_Elmt (Component_Elmt);
5445 end loop;
5447 if Present (Positional_Expr) then
5448 Error_Msg_N
5449 ("too many components for record aggregate", Positional_Expr);
5450 end if;
5452 -- Now scan for the named arguments of the aggregate
5454 while Present (Component_Elmt) loop
5455 Component := Node (Component_Elmt);
5456 Expr := Get_Value (Component, Component_Associations (N), True);
5458 -- Note: The previous call to Get_Value sets the value of the
5459 -- variable Is_Box_Present.
5461 -- Ada 2005 (AI-287): Handle components with default initialization.
5462 -- Note: This feature was originally added to Ada 2005 for limited
5463 -- but it was finally allowed with any type.
5465 if Is_Box_Present then
5466 Check_Box_Component : declare
5467 Ctyp : constant Entity_Id := Etype (Component);
5469 begin
5470 -- Initially assume that the box is for a default-initialized
5471 -- component and reset to False in cases where that's not true.
5473 Is_Box_Init_By_Default := True;
5475 -- If there is a default expression for the aggregate, copy
5476 -- it into a new association. This copy must modify the scopes
5477 -- of internal types that may be attached to the expression
5478 -- (e.g. index subtypes of arrays) because in general the type
5479 -- declaration and the aggregate appear in different scopes,
5480 -- and the backend requires the scope of the type to match the
5481 -- point at which it is elaborated.
5483 -- If the component has an initialization procedure (IP) we
5484 -- pass the component to the expander, which will generate
5485 -- the call to such IP.
5487 -- If the component has discriminants, their values must
5488 -- be taken from their subtype. This is indispensable for
5489 -- constraints that are given by the current instance of an
5490 -- enclosing type, to allow the expansion of the aggregate to
5491 -- replace the reference to the current instance by the target
5492 -- object of the aggregate.
5494 if Is_Case_Choice_Pattern (N) then
5496 -- Do not transform box component values in a case-choice
5497 -- aggregate.
5499 Add_Association
5500 (Component => Component,
5501 Expr => Empty,
5502 Assoc_List => New_Assoc_List,
5503 Is_Box_Present => True);
5505 elsif Present (Parent (Component))
5506 and then Nkind (Parent (Component)) = N_Component_Declaration
5507 and then Present (Expression (Parent (Component)))
5508 then
5509 -- If component declaration has an initialization expression
5510 -- then this is not a case of default initialization.
5512 Is_Box_Init_By_Default := False;
5514 Expr :=
5515 New_Copy_Tree_And_Copy_Dimensions
5516 (Expression (Parent (Component)),
5517 New_Scope => Current_Scope,
5518 New_Sloc => Sloc (N));
5520 -- As the type of the copied default expression may refer
5521 -- to discriminants of the record type declaration, these
5522 -- non-stored discriminants need to be rewritten into stored
5523 -- discriminant values for the aggregate. This is required
5524 -- in GNATprove mode, and is adopted in all modes to avoid
5525 -- special-casing GNATprove mode.
5527 if Is_Array_Type (Etype (Expr)) then
5528 declare
5529 Rec_Typ : constant Entity_Id := Scope (Component);
5530 -- Root record type whose discriminants may be used as
5531 -- bounds in range nodes.
5533 Assoc : Node_Id;
5534 Choice : Node_Id;
5535 Index : Node_Id;
5537 begin
5538 -- Rewrite the range nodes occurring in the indexes
5539 -- and their types.
5541 Index := First_Index (Etype (Expr));
5542 while Present (Index) loop
5543 Rewrite_Range (Rec_Typ, Index);
5544 Rewrite_Range
5545 (Rec_Typ, Scalar_Range (Etype (Index)));
5547 Next_Index (Index);
5548 end loop;
5550 -- Rewrite the range nodes occurring as aggregate
5551 -- bounds and component associations.
5553 if Nkind (Expr) = N_Aggregate then
5554 if Present (Aggregate_Bounds (Expr)) then
5555 Rewrite_Range (Rec_Typ, Aggregate_Bounds (Expr));
5556 end if;
5558 if Present (Component_Associations (Expr)) then
5559 Assoc := First (Component_Associations (Expr));
5560 while Present (Assoc) loop
5561 Choice := First (Choices (Assoc));
5562 while Present (Choice) loop
5563 Rewrite_Range (Rec_Typ, Choice);
5565 Next (Choice);
5566 end loop;
5568 Next (Assoc);
5569 end loop;
5570 end if;
5571 end if;
5572 end;
5573 end if;
5575 Add_Association
5576 (Component => Component,
5577 Expr => Expr,
5578 Assoc_List => New_Assoc_List);
5579 Set_Has_Self_Reference (N);
5581 elsif Needs_Simple_Initialization (Ctyp) then
5582 Add_Association
5583 (Component => Component,
5584 Expr => Empty,
5585 Assoc_List => New_Assoc_List,
5586 Is_Box_Present => True);
5588 elsif Has_Non_Null_Base_Init_Proc (Ctyp)
5589 or else not Expander_Active
5590 then
5591 if Is_Record_Type (Ctyp)
5592 and then Has_Discriminants (Ctyp)
5593 and then not Is_Private_Type (Ctyp)
5594 then
5595 -- We build a partially initialized aggregate with the
5596 -- values of the discriminants and box initialization
5597 -- for the rest, if other components are present.
5599 -- The type of the aggregate is the known subtype of
5600 -- the component. The capture of discriminants must be
5601 -- recursive because subcomponents may be constrained
5602 -- (transitively) by discriminants of enclosing types.
5603 -- For a private type with discriminants, a call to the
5604 -- initialization procedure will be generated, and no
5605 -- subaggregate is needed.
5607 Capture_Discriminants : declare
5608 Loc : constant Source_Ptr := Sloc (N);
5609 Expr : Node_Id;
5611 begin
5612 Expr := Make_Aggregate (Loc, No_List, New_List);
5613 Set_Etype (Expr, Ctyp);
5615 -- If the enclosing type has discriminants, they have
5616 -- been collected in the aggregate earlier, and they
5617 -- may appear as constraints of subcomponents.
5619 -- Similarly if this component has discriminants, they
5620 -- might in turn be propagated to their components.
5622 if Has_Discriminants (Typ) then
5623 Add_Discriminant_Values (Expr, New_Assoc_List);
5624 Propagate_Discriminants (Expr, New_Assoc_List);
5626 elsif Has_Discriminants (Ctyp) then
5627 Add_Discriminant_Values
5628 (Expr, Component_Associations (Expr));
5629 Propagate_Discriminants
5630 (Expr, Component_Associations (Expr));
5632 Build_Constrained_Itype
5633 (Expr, Ctyp, Component_Associations (Expr));
5635 else
5636 declare
5637 Comp : Entity_Id;
5639 begin
5640 -- If the type has additional components, create
5641 -- an OTHERS box association for them.
5643 Comp := First_Component (Ctyp);
5644 while Present (Comp) loop
5645 if Ekind (Comp) = E_Component then
5646 if not Is_Record_Type (Etype (Comp)) then
5647 Append_To
5648 (Component_Associations (Expr),
5649 Make_Component_Association (Loc,
5650 Choices =>
5651 New_List (
5652 Make_Others_Choice (Loc)),
5653 Expression => Empty,
5654 Box_Present => True));
5655 end if;
5657 exit;
5658 end if;
5660 Next_Component (Comp);
5661 end loop;
5662 end;
5663 end if;
5665 Add_Association
5666 (Component => Component,
5667 Expr => Expr,
5668 Assoc_List => New_Assoc_List);
5669 end Capture_Discriminants;
5671 -- Otherwise the component type is not a record, or it has
5672 -- not discriminants, or it is private.
5674 else
5675 Add_Association
5676 (Component => Component,
5677 Expr => Empty,
5678 Assoc_List => New_Assoc_List,
5679 Is_Box_Present => True);
5680 end if;
5682 -- Otherwise we only need to resolve the expression if the
5683 -- component has partially initialized values (required to
5684 -- expand the corresponding assignments and run-time checks).
5686 elsif Present (Expr)
5687 and then Is_Partially_Initialized_Type (Ctyp)
5688 then
5689 Resolve_Aggr_Expr (Expr, Component);
5690 end if;
5691 end Check_Box_Component;
5693 elsif No (Expr) then
5695 -- Ignore hidden components associated with the position of the
5696 -- interface tags: these are initialized dynamically.
5698 if not Present (Related_Type (Component)) then
5699 Error_Msg_NE
5700 ("no value supplied for component &!", N, Component);
5701 end if;
5703 else
5704 Resolve_Aggr_Expr (Expr, Component);
5705 end if;
5707 Next_Elmt (Component_Elmt);
5708 end loop;
5710 -- STEP 7: check for invalid components + check type in choice list
5712 Step_7 : declare
5713 Assoc : Node_Id;
5714 New_Assoc : Node_Id;
5716 Selectr : Node_Id;
5717 -- Selector name
5719 Typech : Entity_Id;
5720 -- Type of first component in choice list
5722 begin
5723 if Present (Component_Associations (N)) then
5724 Assoc := First (Component_Associations (N));
5725 else
5726 Assoc := Empty;
5727 end if;
5729 Verification : while Present (Assoc) loop
5730 Selectr := First (Choices (Assoc));
5731 Typech := Empty;
5733 if Nkind (Selectr) = N_Others_Choice then
5735 -- Ada 2005 (AI-287): others choice may have expression or box
5737 if No (Others_Etype) and then Others_Box = 0 then
5738 Error_Msg_N
5739 ("OTHERS must represent at least one component", Selectr);
5741 elsif Others_Box = 1 and then Warn_On_Redundant_Constructs then
5742 Error_Msg_N ("OTHERS choice is redundant?", Box_Node);
5743 Error_Msg_N
5744 ("\previous choices cover all components?", Box_Node);
5745 end if;
5747 exit Verification;
5748 end if;
5750 while Present (Selectr) loop
5751 New_Assoc := First (New_Assoc_List);
5752 while Present (New_Assoc) loop
5753 Component := First (Choices (New_Assoc));
5755 if Chars (Selectr) = Chars (Component) then
5756 if Style_Check then
5757 Check_Identifier (Selectr, Entity (Component));
5758 end if;
5760 exit;
5761 end if;
5763 Next (New_Assoc);
5764 end loop;
5766 -- If no association, this is not a legal component of the type
5767 -- in question, unless its association is provided with a box.
5769 if No (New_Assoc) then
5770 if Box_Present (Parent (Selectr)) then
5772 -- This may still be a bogus component with a box. Scan
5773 -- list of components to verify that a component with
5774 -- that name exists.
5776 declare
5777 C : Entity_Id;
5779 begin
5780 C := First_Component (Typ);
5781 while Present (C) loop
5782 if Chars (C) = Chars (Selectr) then
5784 -- If the context is an extension aggregate,
5785 -- the component must not be inherited from
5786 -- the ancestor part of the aggregate.
5788 if Nkind (N) /= N_Extension_Aggregate
5789 or else
5790 Scope (Original_Record_Component (C)) /=
5791 Etype (Ancestor_Part (N))
5792 then
5793 exit;
5794 end if;
5795 end if;
5797 Next_Component (C);
5798 end loop;
5800 if No (C) then
5801 Error_Msg_Node_2 := Typ;
5802 Error_Msg_N ("& is not a component of}", Selectr);
5803 end if;
5804 end;
5806 elsif Chars (Selectr) /= Name_uTag
5807 and then Chars (Selectr) /= Name_uParent
5808 then
5809 if not Has_Discriminants (Typ) then
5810 Error_Msg_Node_2 := Typ;
5811 Error_Msg_N ("& is not a component of}", Selectr);
5812 else
5813 Error_Msg_N
5814 ("& is not a component of the aggregate subtype",
5815 Selectr);
5816 end if;
5818 Check_Misspelled_Component (Components, Selectr);
5819 end if;
5821 elsif No (Typech) then
5822 Typech := Base_Type (Etype (Component));
5824 -- AI05-0199: In Ada 2012, several components of anonymous
5825 -- access types can appear in a choice list, as long as the
5826 -- designated types match.
5828 elsif Typech /= Base_Type (Etype (Component)) then
5829 if Ada_Version >= Ada_2012
5830 and then Ekind (Typech) = E_Anonymous_Access_Type
5831 and then
5832 Ekind (Etype (Component)) = E_Anonymous_Access_Type
5833 and then Base_Type (Designated_Type (Typech)) =
5834 Base_Type (Designated_Type (Etype (Component)))
5835 and then
5836 Subtypes_Statically_Match (Typech, (Etype (Component)))
5837 then
5838 null;
5840 elsif not Box_Present (Parent (Selectr)) then
5841 Error_Msg_N
5842 ("components in choice list must have same type",
5843 Selectr);
5844 end if;
5845 end if;
5847 Next (Selectr);
5848 end loop;
5850 Next (Assoc);
5851 end loop Verification;
5852 end Step_7;
5854 -- STEP 8: replace the original aggregate
5856 Step_8 : declare
5857 New_Aggregate : constant Node_Id := New_Copy (N);
5859 begin
5860 Set_Expressions (New_Aggregate, No_List);
5861 Set_Etype (New_Aggregate, Etype (N));
5862 Set_Component_Associations (New_Aggregate, New_Assoc_List);
5863 Set_Check_Actuals (New_Aggregate, Check_Actuals (N));
5865 Rewrite (N, New_Aggregate);
5866 end Step_8;
5868 -- Check the dimensions of the components in the record aggregate
5870 Analyze_Dimension_Extension_Or_Record_Aggregate (N);
5871 end Resolve_Record_Aggregate;
5873 -----------------------------
5874 -- Check_Can_Never_Be_Null --
5875 -----------------------------
5877 procedure Check_Can_Never_Be_Null (Typ : Entity_Id; Expr : Node_Id) is
5878 Comp_Typ : Entity_Id;
5880 begin
5881 pragma Assert
5882 (Ada_Version >= Ada_2005
5883 and then Present (Expr)
5884 and then Known_Null (Expr));
5886 case Ekind (Typ) is
5887 when E_Array_Type =>
5888 Comp_Typ := Component_Type (Typ);
5890 when E_Component
5891 | E_Discriminant
5893 Comp_Typ := Etype (Typ);
5895 when others =>
5896 return;
5897 end case;
5899 if Can_Never_Be_Null (Comp_Typ) then
5901 -- Here we know we have a constraint error. Note that we do not use
5902 -- Apply_Compile_Time_Constraint_Error here to the Expr, which might
5903 -- seem the more natural approach. That's because in some cases the
5904 -- components are rewritten, and the replacement would be missed.
5905 -- We do not mark the whole aggregate as raising a constraint error,
5906 -- because the association may be a null array range.
5908 Error_Msg_N
5909 ("(Ada 2005) NULL not allowed in null-excluding component??", Expr);
5910 Error_Msg_N
5911 ("\Constraint_Error will be raised at run time??", Expr);
5913 Rewrite (Expr,
5914 Make_Raise_Constraint_Error
5915 (Sloc (Expr), Reason => CE_Access_Check_Failed));
5916 Set_Etype (Expr, Comp_Typ);
5917 Set_Analyzed (Expr);
5918 end if;
5919 end Check_Can_Never_Be_Null;
5921 ---------------------
5922 -- Sort_Case_Table --
5923 ---------------------
5925 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is
5926 U : constant Int := Case_Table'Last;
5927 K : Int;
5928 J : Int;
5929 T : Case_Bounds;
5931 begin
5932 K := 1;
5933 while K < U loop
5934 T := Case_Table (K + 1);
5936 J := K + 1;
5937 while J > 1
5938 and then Expr_Value (Case_Table (J - 1).Lo) > Expr_Value (T.Lo)
5939 loop
5940 Case_Table (J) := Case_Table (J - 1);
5941 J := J - 1;
5942 end loop;
5944 Case_Table (J) := T;
5945 K := K + 1;
5946 end loop;
5947 end Sort_Case_Table;
5949 end Sem_Aggr;