Debian Codegraph @

gcc/ada/sem_aggr.adb

raw
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-2025, 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 ------------------------------------------------------------------------------
25
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Debug; use Debug;
30 with Diagnostics.Constructors; use Diagnostics.Constructors;
31 with Einfo; use Einfo;
32 with Einfo.Utils; use Einfo.Utils;
33 with Elists; use Elists;
34 with Errout; use Errout;
35 with Expander; use Expander;
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 Mutably_Tagged; use Mutably_Tagged;
43 with Namet; use Namet;
44 with Namet.Sp; use Namet.Sp;
45 with Nmake; use Nmake;
46 with Nlists; use Nlists;
47 with Opt; use Opt;
48 with Restrict; use Restrict;
49 with Rident; use Rident;
50 with Sem; use Sem;
51 with Sem_Aux; use Sem_Aux;
52 with Sem_Case; use Sem_Case;
53 with Sem_Cat; use Sem_Cat;
54 with Sem_Ch3; use Sem_Ch3;
55 with Sem_Ch8; use Sem_Ch8;
56 with Sem_Ch13; use Sem_Ch13;
57 with Sem_Dim; use Sem_Dim;
58 with Sem_Eval; use Sem_Eval;
59 with Sem_Res; use Sem_Res;
60 with Sem_Util; use Sem_Util;
61 with Sem_Type; use Sem_Type;
62 with Sem_Warn; use Sem_Warn;
63 with Sinfo; use Sinfo;
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;
73 with Warnsw; use Warnsw;
74
75 package body Sem_Aggr is
76
77 type Case_Bounds is record
78 Lo : Node_Id;
79 -- Low bound of choice. Once we sort the Case_Table, then entries
80 -- will be in order of ascending Choice_Lo values.
81
82 Hi : Node_Id;
83 -- High Bound of choice. The sort does not pay any attention to the
84 -- high bound, so choices 1 .. 4 and 1 .. 5 could be in either order.
85
86 Highest : Uint;
87 -- If there are duplicates or missing entries, then in the sorted
88 -- table, this records the highest value among Choice_Hi values
89 -- seen so far, including this entry.
90
91 Choice : Node_Id;
92 -- The node of the choice
93 end record;
94
95 type Case_Table_Type is array (Pos range <>) of Case_Bounds;
96 -- Table type used by Check_Case_Choices procedure
97
98 -----------------------
99 -- Local Subprograms --
100 -----------------------
101
102 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type);
103 -- Sort the Case Table using the Lower Bound of each Choice as the key. A
104 -- simple insertion sort is used since the choices in a case statement will
105 -- usually be in near sorted order.
106
107 function Cannot_Compute_High_Bound (Index : Entity_Id) return Boolean;
108 -- Determines if the type of the given array aggregate index is a modular
109 -- type or an enumeration type that will raise CE at runtime when computing
110 -- the high bound of a null aggregate.
111
112 procedure Check_Can_Never_Be_Null (Typ : Entity_Id; Expr : Node_Id);
113 -- Ada 2005 (AI-231): Check bad usage of null for a component for which
114 -- null exclusion (NOT NULL) is specified. Typ can be an E_Array_Type for
115 -- the array case (the component type of the array will be used) or an
116 -- E_Component/E_Discriminant entity in the record case, in which case the
117 -- type of the component will be used for the test. If Typ is any other
118 -- kind of entity, the call is ignored. Expr is the component node in the
119 -- aggregate which is known to have a null value. A warning message will be
120 -- issued if the component is null excluding.
121 --
122 -- It would be better to pass the proper type for Typ ???
123
124 procedure Check_Expr_OK_In_Limited_Aggregate (Expr : Node_Id);
125 -- Check that Expr is either not limited or else is one of the cases of
126 -- expressions allowed for a limited component association (namely, an
127 -- aggregate, function call, or <> notation). Report error for violations.
128 -- Expression is also OK in an instance or inlining context, because we
129 -- have already preanalyzed and it is known to be type correct.
130
131 procedure Report_Null_Array_Constraint_Error
132 (N : Node_Id;
133 Index_Typ : Entity_Id);
134 -- N is a null array aggregate indexed by the given enumeration type or
135 -- modular type. Report a warning notifying that CE will be raised at
136 -- runtime. Under SPARK mode an error is reported instead of a warning.
137
138 ------------------------------------------------------
139 -- Subprograms used for RECORD AGGREGATE Processing --
140 ------------------------------------------------------
141
142 procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id);
143 -- This procedure performs all the semantic checks required for record
144 -- aggregates. Note that for aggregates analysis and resolution go
145 -- hand in hand. Aggregate analysis has been delayed up to here and
146 -- it is done while resolving the aggregate.
147 --
148 -- N is the N_Aggregate node.
149 -- Typ is the record type for the aggregate resolution
150 --
151 -- While performing the semantic checks, this procedure builds a new
152 -- Component_Association_List where each record field appears alone in a
153 -- Component_Choice_List along with its corresponding expression. The
154 -- record fields in the Component_Association_List appear in the same order
155 -- in which they appear in the record type Typ.
156 --
157 -- Once this new Component_Association_List is built and all the semantic
158 -- checks performed, the original aggregate subtree is replaced with the
159 -- new named record aggregate just built. This new record aggregate has no
160 -- positional associations, so its Expressions field is set to No_List.
161 -- Note that subtree substitution is performed with Rewrite so as to be
162 -- able to retrieve the original aggregate.
163 --
164 -- The aggregate subtree manipulation performed by Resolve_Record_Aggregate
165 -- yields the aggregate format expected by Gigi. Typically, this kind of
166 -- tree manipulations are done in the expander. However, because the
167 -- semantic checks that need to be performed on record aggregates really go
168 -- hand in hand with the record aggregate normalization, the aggregate
169 -- subtree transformation is performed during resolution rather than
170 -- expansion. Had we decided otherwise we would have had to duplicate most
171 -- of the code in the expansion procedure Expand_Record_Aggregate. Note,
172 -- however, that all the expansion concerning aggregates for tagged records
173 -- is done in Expand_Record_Aggregate.
174 --
175 -- The algorithm of Resolve_Record_Aggregate proceeds as follows:
176 --
177 -- 1. Make sure that the record type against which the record aggregate
178 -- has to be resolved is not abstract. Furthermore if the type is a
179 -- null aggregate make sure the input aggregate N is also null.
180 --
181 -- 2. Verify that the structure of the aggregate is that of a record
182 -- aggregate. Specifically, look for component associations and ensure
183 -- that each choice list only has identifiers or the N_Others_Choice
184 -- node. Also make sure that if present, the N_Others_Choice occurs
185 -- last and by itself.
186 --
187 -- 3. If Typ contains discriminants, the values for each discriminant is
188 -- looked for. If the record type Typ has variants, we check that the
189 -- expressions corresponding to each discriminant ruling the (possibly
190 -- nested) variant parts of Typ, are static. This allows us to determine
191 -- the variant parts to which the rest of the aggregate must conform.
192 -- The names of discriminants with their values are saved in a new
193 -- association list, New_Assoc_List which is later augmented with the
194 -- names and values of the remaining components in the record type.
195 --
196 -- During this phase we also make sure that every discriminant is
197 -- assigned exactly one value. Note that when several values for a given
198 -- discriminant are found, semantic processing continues looking for
199 -- further errors. In this case it's the first discriminant value found
200 -- which we will be recorded.
201 --
202 -- IMPORTANT NOTE: For derived tagged types this procedure expects
203 -- First_Discriminant and Next_Discriminant to give the correct list
204 -- of discriminants, in the correct order.
205 --
206 -- 4. After all the discriminant values have been gathered, we can set the
207 -- Etype of the record aggregate. If Typ contains no discriminants this
208 -- is straightforward: the Etype of N is just Typ, otherwise a new
209 -- implicit constrained subtype of Typ is built to be the Etype of N.
210 --
211 -- 5. Gather the remaining record components according to the discriminant
212 -- values. This involves recursively traversing the record type
213 -- structure to see what variants are selected by the given discriminant
214 -- values. This processing is a little more convoluted if Typ is a
215 -- derived tagged types since we need to retrieve the record structure
216 -- of all the ancestors of Typ.
217 --
218 -- 6. After gathering the record components we look for their values in the
219 -- record aggregate and emit appropriate error messages should we not
220 -- find such values or should they be duplicated.
221 --
222 -- 7. We then make sure no illegal component names appear in the record
223 -- aggregate and make sure that the type of the record components
224 -- appearing in a same choice list is the same. Finally we ensure that
225 -- the others choice, if present, is used to provide the value of at
226 -- least a record component.
227 --
228 -- 8. The original aggregate node is replaced with the new named aggregate
229 -- built in steps 3 through 6, as explained earlier.
230 --
231 -- Given the complexity of record aggregate resolution, the primary goal of
232 -- this routine is clarity and simplicity rather than execution and storage
233 -- efficiency. If there are only positional components in the aggregate the
234 -- running time is linear. If there are associations the running time is
235 -- still linear as long as the order of the associations is not too far off
236 -- the order of the components in the record type. If this is not the case
237 -- the running time is at worst quadratic in the size of the association
238 -- list.
239
240 procedure Check_Misspelled_Component
241 (Elements : Elist_Id;
242 Component : Node_Id);
243 -- Give possible misspelling diagnostic if Component is likely to be a
244 -- misspelling of one of the components of the Assoc_List. This is called
245 -- by Resolve_Aggr_Expr after producing an invalid component error message.
246
247 -----------------------------------------------------
248 -- Subprograms used for ARRAY AGGREGATE Processing --
249 -----------------------------------------------------
250
251 function Resolve_Array_Aggregate
252 (N : Node_Id;
253 Index : Node_Id;
254 Index_Constr : Node_Id;
255 Component_Typ : Entity_Id;
256 Iterated : Boolean;
257 Others_Allowed : Boolean) return Boolean;
258 -- This procedure performs the semantic checks for an array aggregate.
259 -- True is returned if the aggregate resolution succeeds.
260 --
261 -- The procedure works by recursively checking each nested aggregate.
262 -- Specifically, after checking a sub-aggregate nested at the i-th level
263 -- we recursively check all the subaggregates at the i+1-st level (if any).
264 -- Note that aggregates analysis and resolution go hand in hand.
265 -- Aggregate analysis has been delayed up to here and it is done while
266 -- resolving the aggregate.
267 --
268 -- N is the current N_Aggregate node to be checked.
269 --
270 -- Index is the index node corresponding to the array sub-aggregate that
271 -- we are currently checking (RM 4.3.3 (8)). Its Etype is the
272 -- corresponding index type (or subtype).
273 --
274 -- Index_Constr is the node giving the applicable index constraint if
275 -- any (RM 4.3.3 (10)). It "is a constraint provided by certain
276 -- contexts [...] that can be used to determine the bounds of the array
277 -- value specified by the aggregate". If Others_Allowed below is False
278 -- there is no applicable index constraint and this node is set to Index.
279 --
280 -- Component_Typ is the array component type.
281 --
282 -- Iterated indicates whether the aggregate appears in the context of an
283 -- iterated association for a parent aggregate.
284 --
285 -- Others_Allowed indicates whether an others choice is allowed
286 -- in the context where the top-level aggregate appeared.
287 --
288 -- The algorithm of Resolve_Array_Aggregate proceeds as follows:
289 --
290 -- 1. Make sure that the others choice, if present, is by itself and
291 -- appears last in the sub-aggregate. Check that we do not have
292 -- positional and named components in the array sub-aggregate (unless
293 -- the named association is an others choice). Finally if an others
294 -- choice is present, make sure it is allowed in the aggregate context.
295 --
296 -- 2. If the array sub-aggregate contains discrete_choices:
297 --
298 -- (A) Verify their validity. Specifically verify that:
299 --
300 -- (a) If a null range is present it must be the only possible
301 -- choice in the array aggregate.
302 --
303 -- (b) Ditto for a non static range.
304 --
305 -- (c) Ditto for a non static expression.
306 --
307 -- In addition this step analyzes and resolves each discrete_choice,
308 -- making sure that its type is the type of the corresponding Index.
309 -- If we are not at the lowest array aggregate level (in the case of
310 -- multidimensional aggregates) then invoke Resolve_Array_Aggregate
311 -- recursively on each component expression. Otherwise, resolve the
312 -- bottom level component expressions against the expected component
313 -- type ONLY IF the component corresponds to a single discrete choice
314 -- which is not an others choice (to see why read the DELAYED
315 -- COMPONENT RESOLUTION below).
316 --
317 -- (B) Determine the bounds of the sub-aggregate and lowest and
318 -- highest choice values.
319 --
320 -- 3. For positional aggregates:
321 --
322 -- (A) Loop over the component expressions either recursively invoking
323 -- Resolve_Array_Aggregate on each of these for multidimensional
324 -- array aggregates or resolving the bottom level component
325 -- expressions against the expected component type.
326 --
327 -- (B) Determine the bounds of the positional sub-aggregates.
328 --
329 -- 4. Try to determine statically whether the evaluation of the array
330 -- sub-aggregate raises Constraint_Error. If yes emit proper
331 -- warnings. The precise checks are the following:
332 --
333 -- (A) Check that the index range defined by aggregate bounds is
334 -- compatible with corresponding index subtype.
335 -- We also check against the base type. In fact it could be that
336 -- Low/High bounds of the base type are static whereas those of
337 -- the index subtype are not. Thus if we can statically catch
338 -- a problem with respect to the base type we are guaranteed
339 -- that the same problem will arise with the index subtype
340 --
341 -- (B) If we are dealing with a named aggregate containing an others
342 -- choice and at least one discrete choice then make sure the range
343 -- specified by the discrete choices does not overflow the
344 -- aggregate bounds. We also check against the index type and base
345 -- type bounds for the same reasons given in (A).
346 --
347 -- (C) If we are dealing with a positional aggregate with an others
348 -- choice make sure the number of positional elements specified
349 -- does not overflow the aggregate bounds. We also check against
350 -- the index type and base type bounds as mentioned in (A).
351 --
352 -- Finally construct an N_Range node giving the sub-aggregate bounds.
353 -- Set the Aggregate_Bounds field of the sub-aggregate to be this
354 -- N_Range. The routine Array_Aggr_Subtype below uses such N_Ranges
355 -- to build the appropriate aggregate subtype. Aggregate_Bounds
356 -- information is needed during expansion.
357 --
358 -- DELAYED COMPONENT RESOLUTION: The resolution of bottom level component
359 -- expressions in an array aggregate may call Duplicate_Subexpr or some
360 -- other routine that inserts code just outside the outermost aggregate.
361 -- If the array aggregate contains discrete choices or an others choice,
362 -- this may be wrong. Consider for instance the following example.
363 --
364 -- type Rec is record
365 -- V : Integer := 0;
366 -- end record;
367 --
368 -- type Acc_Rec is access Rec;
369 -- Arr : array (1..3) of Acc_Rec := (1 .. 3 => new Rec);
370 --
371 -- Then the transformation of "new Rec" that occurs during resolution
372 -- entails the following code modifications
373 --
374 -- P7b : constant Acc_Rec := new Rec;
375 -- RecIP (P7b.all);
376 -- Arr : array (1..3) of Acc_Rec := (1 .. 3 => P7b);
377 --
378 -- This code transformation is clearly wrong, since we need to call
379 -- "new Rec" for each of the 3 array elements. To avoid this problem we
380 -- delay resolution of the components of non positional array aggregates
381 -- to the expansion phase. As an optimization, if the discrete choice
382 -- specifies a single value we do not delay resolution.
383
384 function Array_Aggr_Subtype (N : Node_Id; Typ : Entity_Id) return Entity_Id;
385 -- This routine returns the type or subtype of an array aggregate.
386 --
387 -- N is the array aggregate node whose type we return.
388 --
389 -- Typ is the context type in which N occurs.
390 --
391 -- This routine creates an implicit array subtype whose bounds are
392 -- those defined by the aggregate. When this routine is invoked
393 -- Resolve_Array_Aggregate has already processed aggregate N. Thus the
394 -- Aggregate_Bounds of each sub-aggregate, is an N_Range node giving the
395 -- sub-aggregate bounds. When building the aggregate itype, this function
396 -- traverses the array aggregate N collecting such Aggregate_Bounds and
397 -- constructs the proper array aggregate itype.
398 --
399 -- Note that in the case of multidimensional aggregates each inner
400 -- sub-aggregate corresponding to a given array dimension, may provide a
401 -- different bounds. If it is possible to determine statically that
402 -- some sub-aggregates corresponding to the same index do not have the
403 -- same bounds, then a warning is emitted. If such check is not possible
404 -- statically (because some sub-aggregate bounds are dynamic expressions)
405 -- then this job is left to the expander. In all cases the particular
406 -- bounds that this function will chose for a given dimension is the first
407 -- N_Range node for a sub-aggregate corresponding to that dimension.
408 --
409 -- Note that the Raises_Constraint_Error flag of an array aggregate
410 -- whose evaluation is determined to raise CE by Resolve_Array_Aggregate,
411 -- is set in Resolve_Array_Aggregate but the aggregate is not
412 -- immediately replaced with a raise CE. In fact, Array_Aggr_Subtype must
413 -- first construct the proper itype for the aggregate (Gigi needs
414 -- this). After constructing the proper itype we will eventually replace
415 -- the top-level aggregate with a raise CE (done in Resolve_Aggregate).
416 -- Of course in cases such as:
417 --
418 -- type Arr is array (integer range <>) of Integer;
419 -- A : Arr := (positive range -1 .. 2 => 0);
420 --
421 -- The bounds of the aggregate itype are cooked up to look reasonable
422 -- (in this particular case the bounds will be 1 .. 2).
423
424 procedure Make_String_Into_Aggregate (N : Node_Id);
425 -- A string literal can appear in a context in which a one dimensional
426 -- array of characters is expected. This procedure simply rewrites the
427 -- string as an aggregate, prior to resolution.
428
429 function Resolve_Null_Array_Aggregate (N : Node_Id) return Boolean;
430 -- The recursive method used to construct an aggregate's bounds in
431 -- Resolve_Array_Aggregate cannot work for null array aggregates. This
432 -- function constructs an appropriate list of ranges and stores its first
433 -- element in Aggregate_Bounds (N).
434
435 ---------------------------------
436 -- Delta aggregate processing --
437 ---------------------------------
438
439 procedure Resolve_Delta_Array_Aggregate (N : Node_Id; Typ : Entity_Id);
440 procedure Resolve_Delta_Record_Aggregate (N : Node_Id; Typ : Entity_Id);
441 procedure Resolve_Deep_Delta_Assoc (N : Node_Id; Typ : Entity_Id);
442 -- Resolve the names/expressions in a component association for
443 -- a deep delta aggregate. Typ is the type of the enclosing object.
444
445 ------------------------
446 -- Array_Aggr_Subtype --
447 ------------------------
448
449 function Array_Aggr_Subtype
450 (N : Node_Id;
451 Typ : Entity_Id) return Entity_Id
452 is
453 Aggr_Dimension : constant Pos := Number_Dimensions (Typ);
454 -- Number of aggregate index dimensions
455
456 Aggr_Range : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
457 -- Constrained N_Range of each index dimension in our aggregate itype
458
459 Aggr_Low : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
460 Aggr_High : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
461 -- Low and High bounds for each index dimension in our aggregate itype
462
463 Is_Fully_Positional : Boolean := True;
464
465 procedure Collect_Aggr_Bounds (N : Node_Id; Dim : Pos);
466 -- N is an array (sub-)aggregate. Dim is the dimension corresponding
467 -- to (sub-)aggregate N. This procedure collects and removes the side
468 -- effects of the constrained N_Range nodes corresponding to each index
469 -- dimension of our aggregate itype. These N_Range nodes are collected
470 -- in Aggr_Range above.
471 --
472 -- Likewise collect in Aggr_Low & Aggr_High above the low and high
473 -- bounds of each index dimension. If, when collecting, two bounds
474 -- corresponding to the same dimension are static and found to differ,
475 -- then emit a warning, and mark N as raising Constraint_Error.
476
477 procedure Retrieve_Aggregate_Bounds (This_Range : Node_Id);
478 -- In some cases, an appropriate list of aggregate bounds has been
479 -- created during resolution. Populate Aggr_Range with that list, and
480 -- remove the elements from the list so they can be added to another
481 -- list later.
482
483 -------------------------
484 -- Collect_Aggr_Bounds --
485 -------------------------
486
487 procedure Collect_Aggr_Bounds (N : Node_Id; Dim : Pos) is
488 This_Range : constant Node_Id := Aggregate_Bounds (N);
489 -- The aggregate range node of this specific sub-aggregate
490
491 This_Low : constant Node_Id := Low_Bound (This_Range);
492 This_High : constant Node_Id := High_Bound (This_Range);
493 -- The aggregate bounds of this specific sub-aggregate
494
495 Assoc : Node_Id;
496 Expr : Node_Id;
497
498 begin
499 Remove_Side_Effects (This_Low, Variable_Ref => True);
500 Remove_Side_Effects (This_High, Variable_Ref => True);
501
502 -- Collect the first N_Range for a given dimension that you find.
503 -- For a given dimension they must be all equal anyway.
504
505 if No (Aggr_Range (Dim)) then
506 Aggr_Low (Dim) := This_Low;
507 Aggr_High (Dim) := This_High;
508 Aggr_Range (Dim) := This_Range;
509
510 else
511 if Compile_Time_Known_Value (This_Low) then
512 if not Compile_Time_Known_Value (Aggr_Low (Dim)) then
513 Aggr_Low (Dim) := This_Low;
514
515 elsif Expr_Value (This_Low) /= Expr_Value (Aggr_Low (Dim)) then
516 Set_Raises_Constraint_Error (N);
517 Error_Msg_Warn := SPARK_Mode /= On;
518 Error_Msg_N ("sub-aggregate low bound mismatch<<", N);
519 Error_Msg_N ("\Constraint_Error [<<", N);
520 end if;
521 end if;
522
523 if Compile_Time_Known_Value (This_High) then
524 if not Compile_Time_Known_Value (Aggr_High (Dim)) then
525 Aggr_High (Dim) := This_High;
526
527 elsif
528 Expr_Value (This_High) /= Expr_Value (Aggr_High (Dim))
529 then
530 Set_Raises_Constraint_Error (N);
531 Error_Msg_Warn := SPARK_Mode /= On;
532 Error_Msg_N ("sub-aggregate high bound mismatch<<", N);
533 Error_Msg_N ("\Constraint_Error [<<", N);
534 end if;
535 end if;
536 end if;
537
538 if Dim < Aggr_Dimension then
539
540 if not Is_Null_Aggregate (N) then
541
542 -- Process positional components
543
544 if Present (Expressions (N)) then
545 Expr := First (Expressions (N));
546 while Present (Expr) loop
547 Collect_Aggr_Bounds (Expr, Dim + 1);
548 Next (Expr);
549 end loop;
550 end if;
551
552 -- Process component associations
553
554 if Present (Component_Associations (N)) then
555 Is_Fully_Positional := False;
556
557 Assoc := First (Component_Associations (N));
558 while Present (Assoc) loop
559 Expr := Expression (Assoc);
560 Collect_Aggr_Bounds (Expr, Dim + 1);
561
562 -- Propagate the error; it is not done in other cases to
563 -- avoid replacing this aggregate by a CE node (required
564 -- to report complementary warnings when the expression
565 -- is resolved).
566
567 if Is_Null_Aggregate (Expr)
568 and then Raises_Constraint_Error (Expr)
569 then
570 Set_Raises_Constraint_Error (N);
571 end if;
572
573 Next (Assoc);
574 end loop;
575 end if;
576
577 -- For null aggregates, build the bounds of their inner dimensions
578 -- since they are required for building the aggregate itype.
579
580 else
581 declare
582 Loc : constant Source_Ptr := Sloc (N);
583 Typ : constant Entity_Id := Etype (N);
584 Index : Node_Id;
585 Index_Typ : Entity_Id;
586 Lo, Hi : Node_Id;
587 Null_Range : Node_Id;
588 Num_Dim : Pos := 1;
589
590 begin
591 -- Move the index to the first dimension implicitly included
592 -- in this null aggregate.
593
594 Index := First_Index (Typ);
595 while Num_Dim <= Dim loop
596 Next_Index (Index);
597 Num_Dim := Num_Dim + 1;
598 end loop;
599
600 while Present (Index) loop
601 Get_Index_Bounds (Index, L => Lo, H => Hi);
602 Index_Typ := Etype (Index);
603
604 if Cannot_Compute_High_Bound (Index) then
605 -- To avoid reporting spurious errors we use the upper
606 -- bound as the higger bound of this index; this value
607 -- will not be used to generate code because this
608 -- aggregate will be replaced by a raise CE node.
609
610 Hi := New_Copy_Tree (Lo);
611
612 if not Raises_Constraint_Error (N) then
613 Report_Null_Array_Constraint_Error (N, Index_Typ);
614 Set_Raises_Constraint_Error (N);
615 end if;
616
617 else
618 -- The upper bound is the predecessor of the lower
619 -- bound.
620
621 Hi := Make_Attribute_Reference (Loc,
622 Prefix => New_Occurrence_Of (Index_Typ, Loc),
623 Attribute_Name => Name_Pred,
624 Expressions => New_List (New_Copy_Tree (Lo)));
625 end if;
626
627 Null_Range := Make_Range (Loc, New_Copy_Tree (Lo), Hi);
628 Analyze_And_Resolve (Null_Range, Index_Typ);
629
630 Aggr_Low (Num_Dim) := Low_Bound (Null_Range);
631 Aggr_High (Num_Dim) := High_Bound (Null_Range);
632 Aggr_Range (Num_Dim) := Null_Range;
633
634 Num_Dim := Num_Dim + 1;
635 Next_Index (Index);
636 end loop;
637
638 pragma Assert (Num_Dim = Aggr_Dimension + 1);
639 end;
640 end if;
641 end if;
642 end Collect_Aggr_Bounds;
643
644 -------------------------------
645 -- Retrieve_Aggregate_Bounds --
646 -------------------------------
647
648 procedure Retrieve_Aggregate_Bounds (This_Range : Node_Id) is
649 R : Node_Id := This_Range;
650 begin
651 for J in 1 .. Aggr_Dimension loop
652 Aggr_Range (J) := R;
653 Next_Index (R);
654
655 -- Remove bounds from the list, so they can be reattached as
656 -- the First_Index/Next_Index again.
657
658 Remove (Aggr_Range (J));
659 end loop;
660 end Retrieve_Aggregate_Bounds;
661
662 -- Array_Aggr_Subtype variables
663
664 Itype : Entity_Id;
665 -- The final itype of the overall aggregate
666
667 Index_Constraints : constant List_Id := New_List;
668 -- The list of index constraints of the aggregate itype
669
670 -- Start of processing for Array_Aggr_Subtype
671
672 begin
673 -- Make sure that the list of index constraints is properly attached to
674 -- the tree, and then collect the aggregate bounds.
675
676 -- If no aggregate bounds have been set, this is an aggregate with
677 -- iterator specifications and a dynamic size to be determined by
678 -- first pass of expanded code.
679
680 if No (Aggregate_Bounds (N)) then
681 return Typ;
682 end if;
683
684 Set_Parent (Index_Constraints, N);
685
686 if Is_Rewrite_Substitution (N)
687 and then Present (Component_Associations (Original_Node (N)))
688 then
689 Retrieve_Aggregate_Bounds (First_Index (Etype (Original_Node (N))));
690
691 -- When resolving a null aggregate we created a list of aggregate bounds
692 -- for the consecutive dimensions. The bounds for the first dimension
693 -- are attached as the Aggregate_Bounds of the aggregate node.
694
695 elsif Is_Null_Aggregate (N) then
696 Retrieve_Aggregate_Bounds (Aggregate_Bounds (N));
697 else
698 Collect_Aggr_Bounds (N, 1);
699 end if;
700
701 -- Build the list of constrained indexes of our aggregate itype
702
703 for J in 1 .. Aggr_Dimension loop
704 Create_Index : declare
705 Index_Base : constant Entity_Id :=
706 Base_Type (Etype (Aggr_Range (J)));
707 Index_Typ : Entity_Id;
708
709 begin
710 -- Construct the Index subtype, and associate it with the range
711 -- construct that generates it.
712
713 Index_Typ :=
714 Create_Itype (Subtype_Kind (Ekind (Index_Base)), Aggr_Range (J));
715
716 Set_Etype (Index_Typ, Index_Base);
717
718 if Is_Character_Type (Index_Base) then
719 Set_Is_Character_Type (Index_Typ);
720 end if;
721
722 Set_Size_Info (Index_Typ, (Index_Base));
723 Set_RM_Size (Index_Typ, RM_Size (Index_Base));
724 Set_First_Rep_Item (Index_Typ, First_Rep_Item (Index_Base));
725 Set_Scalar_Range (Index_Typ, Aggr_Range (J));
726
727 if Is_Discrete_Or_Fixed_Point_Type (Index_Typ) then
728 Set_RM_Size (Index_Typ, UI_From_Int (Minimum_Size (Index_Typ)));
729 end if;
730
731 Set_Etype (Aggr_Range (J), Index_Typ);
732
733 Append (Aggr_Range (J), To => Index_Constraints);
734 end Create_Index;
735 end loop;
736
737 -- Now build the Itype
738
739 Itype := Create_Itype (E_Array_Subtype, N);
740
741 Set_First_Rep_Item (Itype, First_Rep_Item (Typ));
742 Set_Convention (Itype, Convention (Typ));
743 Set_Depends_On_Private (Itype, Has_Private_Component (Typ));
744 Set_Etype (Itype, Base_Type (Typ));
745 Set_Has_Alignment_Clause (Itype, Has_Alignment_Clause (Typ));
746 Set_Is_Aliased (Itype, Is_Aliased (Typ));
747 Set_Is_Independent (Itype, Is_Independent (Typ));
748 Set_Depends_On_Private (Itype, Depends_On_Private (Typ));
749
750 Copy_Suppress_Status (Index_Check, Typ, Itype);
751 Copy_Suppress_Status (Length_Check, Typ, Itype);
752
753 Set_First_Index (Itype, First (Index_Constraints));
754 Set_Is_Constrained (Itype, True);
755 Set_Is_Internal (Itype, True);
756
757 if Has_Predicates (Typ) then
758 Set_Has_Predicates (Itype);
759
760 -- If the base type has a predicate, capture the predicated parent
761 -- or the existing predicate function for SPARK use.
762
763 if Present (Predicate_Function (Typ)) then
764 Set_Predicate_Function (Itype, Predicate_Function (Typ));
765
766 elsif Is_Itype (Typ) then
767 Set_Predicated_Parent (Itype, Predicated_Parent (Typ));
768
769 else
770 Set_Predicated_Parent (Itype, Typ);
771 end if;
772 end if;
773
774 -- A simple optimization: purely positional aggregates of static
775 -- components should be passed to gigi unexpanded whenever possible, and
776 -- regardless of the staticness of the bounds themselves. Subsequent
777 -- checks in exp_aggr verify that type is not packed, etc.
778
779 Set_Size_Known_At_Compile_Time
780 (Itype,
781 Is_Fully_Positional
782 and then Comes_From_Source (N)
783 and then Size_Known_At_Compile_Time (Component_Type (Typ)));
784
785 -- We always need a freeze node for a packed array subtype, so that we
786 -- can build the Packed_Array_Impl_Type corresponding to the subtype. If
787 -- expansion is disabled, the packed array subtype is not built, and we
788 -- must not generate a freeze node for the type, or else it will appear
789 -- incomplete to gigi.
790
791 if Is_Packed (Itype)
792 and then not In_Spec_Expression
793 and then Expander_Active
794 then
795 Freeze_Itype (Itype, N);
796 end if;
797
798 return Itype;
799 end Array_Aggr_Subtype;
800
801 -------------------------------
802 -- Cannot_Compute_High_Bound --
803 -------------------------------
804
805 function Cannot_Compute_High_Bound (Index : Entity_Id) return Boolean is
806 Index_Type : constant Entity_Id := Etype (Index);
807 Lo, Hi : Node_Id;
808
809 begin
810 if not Is_Modular_Integer_Type (Index_Type)
811 and then not Is_Enumeration_Type (Index_Type)
812 then
813 return False;
814
815 elsif Index_Type = Base_Type (Index_Type) then
816 return True;
817
818 else
819 Get_Index_Bounds (Index, L => Lo, H => Hi);
820
821 if Compile_Time_Known_Value (Lo) then
822 if Is_Enumeration_Type (Index_Type)
823 and then not Is_Character_Type (Index_Type)
824 then
825 return Enumeration_Pos (Entity (Lo))
826 = Enumeration_Pos (First_Literal (Base_Type (Index_Type)));
827 else
828 return Expr_Value (Lo) = Uint_0;
829 end if;
830 end if;
831 end if;
832
833 return False;
834 end Cannot_Compute_High_Bound;
835
836 --------------------------------
837 -- Check_Misspelled_Component --
838 --------------------------------
839
840 procedure Check_Misspelled_Component
841 (Elements : Elist_Id;
842 Component : Node_Id)
843 is
844 Max_Suggestions : constant := 2;
845
846 Nr_Of_Suggestions : Natural := 0;
847 Suggestion_1 : Entity_Id := Empty;
848 Suggestion_2 : Entity_Id := Empty;
849 Component_Elmt : Elmt_Id;
850
851 begin
852 -- All the components of List are matched against Component and a count
853 -- is maintained of possible misspellings. When at the end of the
854 -- analysis there are one or two (not more) possible misspellings,
855 -- these misspellings will be suggested as possible corrections.
856
857 Component_Elmt := First_Elmt (Elements);
858 while Nr_Of_Suggestions <= Max_Suggestions
859 and then Present (Component_Elmt)
860 loop
861 if Is_Bad_Spelling_Of
862 (Chars (Node (Component_Elmt)),
863 Chars (Component))
864 then
865 Nr_Of_Suggestions := Nr_Of_Suggestions + 1;
866
867 case Nr_Of_Suggestions is
868 when 1 => Suggestion_1 := Node (Component_Elmt);
869 when 2 => Suggestion_2 := Node (Component_Elmt);
870 when others => null;
871 end case;
872 end if;
873
874 Next_Elmt (Component_Elmt);
875 end loop;
876
877 -- Report at most two suggestions
878
879 if Nr_Of_Suggestions = 1 then
880 Error_Msg_NE -- CODEFIX
881 ("\possible misspelling of&", Component, Suggestion_1);
882
883 elsif Nr_Of_Suggestions = 2 then
884 Error_Msg_Node_2 := Suggestion_2;
885 Error_Msg_NE -- CODEFIX
886 ("\possible misspelling of& or&", Component, Suggestion_1);
887 end if;
888 end Check_Misspelled_Component;
889
890 ----------------------------------------
891 -- Check_Expr_OK_In_Limited_Aggregate --
892 ----------------------------------------
893
894 procedure Check_Expr_OK_In_Limited_Aggregate (Expr : Node_Id) is
895 begin
896 if Is_Limited_Type (Etype (Expr))
897 and then Comes_From_Source (Expr)
898 then
899 if In_Instance_Body or else In_Inlined_Body then
900 null;
901
902 elsif not OK_For_Limited_Init (Etype (Expr), Expr) then
903 Error_Msg_N
904 ("initialization not allowed for limited types", Expr);
905 Explain_Limited_Type (Etype (Expr), Expr);
906 end if;
907 end if;
908 end Check_Expr_OK_In_Limited_Aggregate;
909
910 --------------------
911 -- Is_Deep_Choice --
912 --------------------
913
914 function Is_Deep_Choice
915 (Choice : Node_Id;
916 Aggr_Type : Type_Kind_Id) return Boolean
917 is
918 Pref : Node_Id := Choice;
919 begin
920 while not Is_Root_Prefix_Of_Deep_Choice (Pref) loop
921 Pref := Prefix (Pref);
922 end loop;
923
924 if Is_Array_Type (Aggr_Type) then
925 return Paren_Count (Pref) > 0
926 and then Pref /= Choice;
927 else
928 return Pref /= Choice;
929 end if;
930 end Is_Deep_Choice;
931
932 --------------------------
933 -- Is_Indexed_Aggregate --
934 --------------------------
935
936 function Is_Indexed_Aggregate
937 (N : N_Aggregate_Id;
938 Add_Unnamed : Node_Id;
939 New_Indexed : Node_Id) return Boolean
940 is
941 begin
942 if Present (New_Indexed)
943 and then not Is_Null_Aggregate (N)
944 then
945 if No (Add_Unnamed) then
946 return True;
947
948 else
949 declare
950 Comp_Assns : constant List_Id := Component_Associations (N);
951 Comp_Assn : Node_Id;
952
953 begin
954 if not Is_Empty_List (Comp_Assns) then
955
956 -- It suffices to look at the first association to determine
957 -- whether the aggregate is an indexed aggregate.
958
959 Comp_Assn := First (Comp_Assns);
960
961 -- Test for the component association being either:
962 --
963 -- 1) an N_Component_Association node, in which case there
964 -- is a list of choices (the "key choices");
965 --
966 -- or else:
967 --
968 -- 2) an N_Iterated_Component_Association node that has
969 -- a Defining_Identifier, in which case it has
970 -- Discrete_Choices that effectively make it
971 -- equivalent to a Loop_Parameter_Specification;
972 --
973 -- or else:
974 --
975 -- 3) an N_Iterated_Element_Association node with
976 -- a Loop_Parameter_Specification with a discrete
977 -- subtype or range.
978 --
979 -- This basically corresponds to the definition of indexed
980 -- aggregates (in RM22 4.3.5(25/5)), but the GNAT tree
981 -- representation doesn't always directly match the RM
982 -- syntax for various reasons.
983
984 if Nkind (Comp_Assn) = N_Component_Association
985 or else
986 (Nkind (Comp_Assn) = N_Iterated_Component_Association
987 and then Present (Defining_Identifier (Comp_Assn)))
988 then
989 return True;
990
991 -- In the case of an iterated_element_association with a
992 -- loop_parameter_specification, we have to look deeper to
993 -- confirm that it is not actually an iterator_specification
994 -- masquerading as a loop_parameter_specification. Those can
995 -- share syntax (for example, having the iterator of form
996 -- "for C in <function-call>") and a rewrite into an
997 -- iterator_specification can happen later.
998
999 elsif Nkind (Comp_Assn) = N_Iterated_Element_Association
1000 and then Present (Loop_Parameter_Specification (Comp_Assn))
1001 then
1002 declare
1003 Loop_Parm_Spec : constant Node_Id :=
1004 Loop_Parameter_Specification (Comp_Assn);
1005 Discr_Subt_Defn : constant Node_Id :=
1006 Discrete_Subtype_Definition (Loop_Parm_Spec);
1007 begin
1008 if Nkind (Discr_Subt_Defn) = N_Range
1009 or else
1010 Nkind (Discr_Subt_Defn) = N_Subtype_Indication
1011 or else
1012 (Is_Entity_Name (Discr_Subt_Defn)
1013 and then
1014 Is_Type (Entity (Discr_Subt_Defn)))
1015 then
1016 return True;
1017 end if;
1018 end;
1019 end if;
1020 end if;
1021 end;
1022 end if;
1023 end if;
1024
1025 return False;
1026 end Is_Indexed_Aggregate;
1027
1028 -------------------------
1029 -- Is_Others_Aggregate --
1030 -------------------------
1031
1032 function Is_Others_Aggregate (Aggr : Node_Id) return Boolean is
1033 Assoc : constant List_Id := Component_Associations (Aggr);
1034
1035 begin
1036 return No (Expressions (Aggr))
1037 and then Nkind (First (Choice_List (First (Assoc)))) = N_Others_Choice;
1038 end Is_Others_Aggregate;
1039
1040 -----------------------------------
1041 -- Is_Root_Prefix_Of_Deep_Choice --
1042 -----------------------------------
1043
1044 function Is_Root_Prefix_Of_Deep_Choice (Pref : Node_Id) return Boolean is
1045 begin
1046 return Paren_Count (Pref) > 0
1047 or else Nkind (Pref) not in N_Indexed_Component
1048 | N_Selected_Component;
1049 end Is_Root_Prefix_Of_Deep_Choice;
1050
1051 -------------------------
1052 -- Is_Single_Aggregate --
1053 -------------------------
1054
1055 function Is_Single_Aggregate (Aggr : Node_Id) return Boolean is
1056 Assoc : constant List_Id := Component_Associations (Aggr);
1057
1058 begin
1059 return No (Expressions (Aggr))
1060 and then No (Next (First (Assoc)))
1061 and then No (Next (First (Choice_List (First (Assoc)))));
1062 end Is_Single_Aggregate;
1063
1064 -----------------------
1065 -- Is_Null_Aggregate --
1066 -----------------------
1067
1068 function Is_Null_Aggregate (N : Node_Id) return Boolean is
1069 begin
1070 return Nkind (N) = N_Aggregate
1071 and then Is_Homogeneous_Aggregate (N)
1072 and then Is_Empty_List (Expressions (N))
1073 and then Is_Empty_List (Component_Associations (N));
1074 end Is_Null_Aggregate;
1075
1076 ----------------------------------------
1077 -- Is_Null_Array_Aggregate_High_Bound --
1078 ----------------------------------------
1079
1080 function Is_Null_Array_Aggregate_High_Bound (N : Node_Id) return Boolean is
1081 Original_N : constant Node_Id := Original_Node (N);
1082 begin
1083 return Ada_Version >= Ada_2022
1084 and then not Comes_From_Source (Original_N)
1085 and then Nkind (Original_N) = N_Attribute_Reference
1086 and then
1087 Get_Attribute_Id (Attribute_Name (Original_N)) = Attribute_Pred
1088 and then Nkind (Parent (N)) in N_Range | N_Op_Le
1089 and then not Comes_From_Source (Parent (N));
1090 end Is_Null_Array_Aggregate_High_Bound;
1091
1092 --------------------------------
1093 -- Make_String_Into_Aggregate --
1094 --------------------------------
1095
1096 procedure Make_String_Into_Aggregate (N : Node_Id) is
1097 Exprs : constant List_Id := New_List;
1098 Loc : constant Source_Ptr := Sloc (N);
1099 Str : constant String_Id := Strval (N);
1100 Strlen : constant Nat := String_Length (Str);
1101 C : Char_Code;
1102 C_Node : Node_Id;
1103 New_N : Node_Id;
1104 P : Source_Ptr;
1105
1106 begin
1107 P := Loc + 1;
1108 for J in 1 .. Strlen loop
1109 C := Get_String_Char (Str, J);
1110 Set_Character_Literal_Name (C);
1111
1112 C_Node :=
1113 Make_Character_Literal (P,
1114 Chars => Name_Find,
1115 Char_Literal_Value => UI_From_CC (C));
1116 Set_Etype (C_Node, Any_Character);
1117 Append_To (Exprs, C_Node);
1118
1119 P := P + 1;
1120 -- Something special for wide strings???
1121 end loop;
1122
1123 New_N := Make_Aggregate (Loc, Expressions => Exprs);
1124 Set_Analyzed (New_N);
1125 Set_Etype (New_N, Any_Composite);
1126
1127 Rewrite (N, New_N);
1128 end Make_String_Into_Aggregate;
1129
1130 ----------------------------------------
1131 -- Report_Null_Array_Constraint_Error --
1132 ----------------------------------------
1133
1134 procedure Report_Null_Array_Constraint_Error
1135 (N : Node_Id;
1136 Index_Typ : Entity_Id) is
1137 begin
1138 Error_Msg_Warn := SPARK_Mode /= On;
1139
1140 if Is_Modular_Integer_Type (Index_Typ) then
1141 Error_Msg_N
1142 ("null array aggregate indexed by a modular type<<", N);
1143 else
1144 Error_Msg_N
1145 ("null array aggregate indexed by an enumeration type<<", N);
1146 end if;
1147
1148 Error_Msg_N ("\Constraint_Error [<<", N);
1149 end Report_Null_Array_Constraint_Error;
1150
1151 -----------------------
1152 -- Resolve_Aggregate --
1153 -----------------------
1154
1155 procedure Resolve_Aggregate (N : Node_Id; Typ : Entity_Id) is
1156 Loc : constant Source_Ptr := Sloc (N);
1157
1158 Aggr_Subtyp : Entity_Id;
1159 -- The actual aggregate subtype. This is not necessarily the same as Typ
1160 -- which is the subtype of the context in which the aggregate was found.
1161
1162 Others_Box : Boolean := False;
1163 -- Set to True if N represents a simple aggregate with only
1164 -- (others => <>), not nested as part of another aggregate.
1165
1166 function Is_Full_Access_Aggregate (N : Node_Id) return Boolean;
1167 -- If a full access object is initialized with an aggregate or is
1168 -- assigned an aggregate, we have to prevent a piecemeal access or
1169 -- assignment to the object, even if the aggregate is to be expanded.
1170 -- We create a temporary for the aggregate, and assign the temporary
1171 -- instead, so that the back end can generate an atomic move for it.
1172 -- This is only done in the context of an object declaration or an
1173 -- assignment. Function is a noop and returns false in other contexts.
1174
1175 function Within_Aggregate (N : Node_Id) return Boolean;
1176 -- Return True if N is part of an N_Aggregate
1177
1178 ------------------------------
1179 -- Is_Full_Access_Aggregate --
1180 ------------------------------
1181
1182 function Is_Full_Access_Aggregate (N : Node_Id) return Boolean is
1183 Loc : constant Source_Ptr := Sloc (N);
1184
1185 New_N : Node_Id;
1186 Par : Node_Id;
1187 Temp : Entity_Id;
1188 Typ : Entity_Id;
1189
1190 begin
1191 Par := Parent (N);
1192
1193 -- Aggregate may be qualified, so find outer context
1194
1195 if Nkind (Par) = N_Qualified_Expression then
1196 Par := Parent (Par);
1197 end if;
1198
1199 if not Comes_From_Source (Par) then
1200 return False;
1201 end if;
1202
1203 case Nkind (Par) is
1204 when N_Assignment_Statement =>
1205 Typ := Etype (Name (Par));
1206
1207 if not Is_Full_Access (Typ)
1208 and then not Is_Full_Access_Object (Name (Par))
1209 then
1210 return False;
1211 end if;
1212
1213 when N_Object_Declaration =>
1214 Typ := Etype (Defining_Identifier (Par));
1215
1216 if not Is_Full_Access (Typ)
1217 and then not Is_Full_Access (Defining_Identifier (Par))
1218 then
1219 return False;
1220 end if;
1221
1222 when others =>
1223 return False;
1224 end case;
1225
1226 Temp := Make_Temporary (Loc, 'T', N);
1227 New_N :=
1228 Make_Object_Declaration (Loc,
1229 Defining_Identifier => Temp,
1230 Constant_Present => True,
1231 Object_Definition => New_Occurrence_Of (Typ, Loc),
1232 Expression => Relocate_Node (N));
1233 Insert_Action (Par, New_N);
1234
1235 Rewrite (N, New_Occurrence_Of (Temp, Loc));
1236 Analyze_And_Resolve (N, Typ);
1237
1238 return True;
1239 end Is_Full_Access_Aggregate;
1240
1241 ----------------------
1242 -- Within_Aggregate --
1243 ----------------------
1244
1245 function Within_Aggregate (N : Node_Id) return Boolean is
1246 P : Node_Id := Parent (N);
1247 begin
1248 while Present (P) loop
1249 if Nkind (P) = N_Aggregate then
1250 return True;
1251 end if;
1252
1253 P := Parent (P);
1254 end loop;
1255
1256 return False;
1257 end Within_Aggregate;
1258
1259 -- Start of processing for Resolve_Aggregate
1260
1261 begin
1262 -- Ignore junk empty aggregate resulting from parser error
1263
1264 if No (Expressions (N))
1265 and then No (Component_Associations (N))
1266 and then not Null_Record_Present (N)
1267 then
1268 return;
1269
1270 -- If the aggregate is assigned to a full access variable, we have
1271 -- to prevent a piecemeal assignment even if the aggregate is to be
1272 -- expanded. We create a temporary for the aggregate, and assign the
1273 -- temporary instead, so that the back end can generate an atomic move
1274 -- for it. This is properly an expansion activity but it must be done
1275 -- before resolution because aggregate resolution cannot be done twice.
1276
1277 elsif Expander_Active and then Is_Full_Access_Aggregate (N) then
1278 return;
1279 end if;
1280
1281 -- If the aggregate has box-initialized components, its type must be
1282 -- frozen so that initialization procedures can properly be called
1283 -- in the resolution that follows. The replacement of boxes with
1284 -- initialization calls is properly an expansion activity but it must
1285 -- be done during resolution.
1286
1287 if Expander_Active
1288 and then Present (Component_Associations (N))
1289 then
1290 declare
1291 Comp : Node_Id;
1292 First_Comp : Boolean := True;
1293
1294 begin
1295 Comp := First (Component_Associations (N));
1296 while Present (Comp) loop
1297 if Box_Present (Comp) then
1298 if First_Comp
1299 and then No (Expressions (N))
1300 and then Nkind (First (Choices (Comp))) = N_Others_Choice
1301 and then not Within_Aggregate (N)
1302 then
1303 Others_Box := True;
1304 end if;
1305
1306 Insert_Actions (N, Freeze_Entity (Typ, N));
1307 exit;
1308 end if;
1309
1310 First_Comp := False;
1311 Next (Comp);
1312 end loop;
1313 end;
1314 end if;
1315
1316 -- Check for aggregates not allowed in configurable run-time mode.
1317 -- We allow all cases of aggregates that do not come from source, since
1318 -- these are all assumed to be small (e.g. bounds of a string literal).
1319 -- We also allow aggregates of types we know to be small.
1320
1321 if not Support_Aggregates_On_Target
1322 and then Comes_From_Source (N)
1323 and then (not Known_Static_Esize (Typ)
1324 or else Esize (Typ) > System_Max_Integer_Size)
1325 then
1326 Error_Msg_CRT ("aggregate", N);
1327 end if;
1328
1329 -- Ada 2005 (AI-287): Limited aggregates allowed
1330
1331 -- In an instance, ignore aggregate subcomponents that may be limited,
1332 -- because they originate in view conflicts. If the original aggregate
1333 -- is legal and the actuals are legal, the aggregate itself is legal.
1334
1335 if Is_Limited_Type (Typ)
1336 and then Ada_Version < Ada_2005
1337 and then not In_Instance
1338 then
1339 Error_Msg_N ("aggregate type cannot be limited", N);
1340 Explain_Limited_Type (Typ, N);
1341
1342 elsif Is_Class_Wide_Type (Typ) then
1343 Error_Msg_N ("type of aggregate cannot be class-wide", N);
1344
1345 elsif Typ = Any_String
1346 or else Typ = Any_Composite
1347 then
1348 Error_Msg_N ("no unique type for aggregate", N);
1349 Set_Etype (N, Any_Composite);
1350
1351 elsif Is_Array_Type (Typ) and then Null_Record_Present (N) then
1352 Error_Msg_N ("null record forbidden in array aggregate", N);
1353
1354 elsif Is_Record_Type (Typ)
1355 and then not Is_Homogeneous_Aggregate (N)
1356 then
1357 Resolve_Record_Aggregate (N, Typ);
1358
1359 elsif Has_Aspect (Typ, Aspect_Aggregate)
1360 and then Ada_Version >= Ada_2022
1361 then
1362 -- Check for Ada 2022 and () aggregate.
1363
1364 if not Is_Homogeneous_Aggregate (N) then
1365 Error_Msg_N ("container aggregate must use '['], not ()", N);
1366 end if;
1367
1368 Resolve_Container_Aggregate (N, Typ);
1369
1370 -- Check for an attempt to use "[]" for an aggregate of a record type
1371 -- after handling the case where the type has an Aggregate aspect,
1372 -- because the aspect can be specified for record types, but if it
1373 -- wasn't specified, then this is an error.
1374
1375 elsif Is_Record_Type (Typ) and then Is_Homogeneous_Aggregate (N) then
1376 Error_Msg_N ("record aggregate must use (), not '[']", N);
1377
1378 elsif Is_Array_Type (Typ) then
1379
1380 -- First a special test, for the case of a positional aggregate of
1381 -- characters which can be replaced by a string literal.
1382
1383 -- Do not perform this transformation if this was a string literal
1384 -- to start with, whose components needed constraint checks, or if
1385 -- the component type is nonstatic or has predicates, because it will
1386 -- require those checks and be transformed back into an aggregate.
1387 -- If the index type is not Integer, then the aggregate may represent
1388 -- a user-defined string type but the context might need the original
1389 -- type, so we do not perform the transformation at this point.
1390
1391 if Number_Dimensions (Typ) = 1
1392 and then Is_Standard_Character_Type (Component_Type (Typ))
1393 and then No (Component_Associations (N))
1394 and then not Is_Limited_Composite (Typ)
1395 and then not Is_Private_Composite (Typ)
1396 and then not Is_Bit_Packed_Array (Typ)
1397 and then Nkind (Original_Node (Parent (N))) /= N_String_Literal
1398 and then Is_OK_Static_Subtype (Component_Type (Typ))
1399 and then not Has_Predicates (Component_Type (Typ))
1400 and then Base_Type (Etype (First_Index (Typ))) =
1401 Base_Type (Standard_Integer)
1402 and then not Has_Static_Empty_Array_Bounds (Typ)
1403 then
1404 declare
1405 Expr : Node_Id;
1406
1407 begin
1408 Expr := First (Expressions (N));
1409 while Present (Expr) loop
1410 exit when Nkind (Expr) /= N_Character_Literal;
1411 Next (Expr);
1412 end loop;
1413
1414 if No (Expr) then
1415 Start_String;
1416
1417 Expr := First (Expressions (N));
1418 while Present (Expr) loop
1419 Store_String_Char (UI_To_CC (Char_Literal_Value (Expr)));
1420 Next (Expr);
1421 end loop;
1422
1423 Rewrite (N, Make_String_Literal (Loc, End_String));
1424
1425 Analyze_And_Resolve (N, Typ);
1426 return;
1427 end if;
1428 end;
1429 end if;
1430
1431 -- Here if we have a real aggregate to deal with
1432
1433 Array_Aggregate : declare
1434 Aggr_Resolved : Boolean;
1435 Aggr_Typ : constant Entity_Id := Etype (Typ);
1436 -- This is the unconstrained array type, which is the type against
1437 -- which the aggregate is to be resolved. Typ itself is the array
1438 -- type of the context which may not be the same subtype as the
1439 -- subtype for the final aggregate.
1440
1441 Is_Null_Aggr : constant Boolean := Is_Null_Aggregate (N);
1442
1443 begin
1444 -- In the following we determine whether an OTHERS choice is
1445 -- allowed inside the array aggregate. The test checks the context
1446 -- in which the array aggregate occurs. If the context does not
1447 -- permit it, or the aggregate type is unconstrained, an OTHERS
1448 -- choice is not allowed (except that it is always allowed on the
1449 -- right-hand side of an assignment statement; in this case the
1450 -- constrainedness of the type doesn't matter, because an array
1451 -- object is always constrained).
1452
1453 -- If expansion is disabled (generic context, or semantics-only
1454 -- mode) actual subtypes cannot be constructed, and the type of an
1455 -- object may be its unconstrained nominal type. However, if the
1456 -- context is an assignment statement, OTHERS is allowed, because
1457 -- the target of the assignment will have a constrained subtype
1458 -- when fully compiled. Ditto if the context is an initialization
1459 -- procedure where a component may have a predicate function that
1460 -- carries the base type.
1461
1462 -- Note that there is no node for Explicit_Actual_Parameter.
1463 -- To test for this context we therefore have to test for node
1464 -- N_Parameter_Association which itself appears only if there is a
1465 -- formal parameter. Consequently we also need to test for
1466 -- N_Procedure_Call_Statement or N_Function_Call.
1467
1468 -- The context may be an N_Reference node, created by expansion.
1469 -- Legality of the others clause was established in the source,
1470 -- so the context is legal.
1471
1472 Set_Etype (N, Aggr_Typ); -- May be overridden later on
1473
1474 if Is_Null_Aggr then
1475 Set_Etype (N, Typ);
1476 Aggr_Resolved := Resolve_Null_Array_Aggregate (N);
1477
1478 elsif Nkind (Parent (N)) = N_Assignment_Statement
1479 or else Inside_Init_Proc
1480 or else (Is_Constrained (Typ)
1481 and then Nkind (Parent (N)) in
1482 N_Parameter_Association
1483 | N_Function_Call
1484 | N_Procedure_Call_Statement
1485 | N_Generic_Association
1486 | N_Formal_Object_Declaration
1487 | N_Simple_Return_Statement
1488 | N_Object_Declaration
1489 | N_Component_Declaration
1490 | N_Parameter_Specification
1491 | N_Qualified_Expression
1492 | N_Unchecked_Type_Conversion
1493 | N_Reference
1494 | N_Aggregate
1495 | N_Extension_Aggregate
1496 | N_Component_Association
1497 | N_Case_Expression_Alternative
1498 | N_If_Expression
1499 | N_Expression_With_Actions)
1500 then
1501 Aggr_Resolved :=
1502 Resolve_Array_Aggregate
1503 (N,
1504 Index => First_Index (Aggr_Typ),
1505 Index_Constr => First_Index (Typ),
1506 Component_Typ => Component_Type (Typ),
1507 Iterated => False,
1508 Others_Allowed => True);
1509 else
1510 Aggr_Resolved :=
1511 Resolve_Array_Aggregate
1512 (N,
1513 Index => First_Index (Aggr_Typ),
1514 Index_Constr => First_Index (Aggr_Typ),
1515 Component_Typ => Component_Type (Typ),
1516 Iterated => False,
1517 Others_Allowed => False);
1518 end if;
1519
1520 if not Aggr_Resolved then
1521
1522 -- A parenthesized expression may have been intended as an
1523 -- aggregate, leading to a type error when analyzing the
1524 -- component. This can also happen for a nested component
1525 -- (see Analyze_Aggr_Expr).
1526
1527 if Paren_Count (N) > 0 then
1528 Error_Msg_N
1529 ("positional aggregate cannot have one component", N);
1530 end if;
1531
1532 Aggr_Subtyp := Any_Composite;
1533
1534 else
1535 Aggr_Subtyp := Array_Aggr_Subtype (N, Typ);
1536 end if;
1537
1538 Set_Etype (N, Aggr_Subtyp);
1539 end Array_Aggregate;
1540
1541 elsif Is_Private_Type (Typ)
1542 and then Present (Full_View (Typ))
1543 and then (In_Inlined_Body or In_Instance_Body)
1544 and then Is_Composite_Type (Full_View (Typ))
1545 then
1546 Resolve (N, Full_View (Typ));
1547
1548 else
1549 Error_Msg_N ("illegal context for aggregate", N);
1550 end if;
1551
1552 -- If we can determine statically that the evaluation of the aggregate
1553 -- raises Constraint_Error, then replace the aggregate with an
1554 -- N_Raise_Constraint_Error node, but set the Etype to the right
1555 -- aggregate subtype. Gigi needs this.
1556
1557 if Raises_Constraint_Error (N) then
1558 Aggr_Subtyp := Etype (N);
1559 Rewrite (N,
1560 Make_Raise_Constraint_Error (Loc, Reason => CE_Range_Check_Failed));
1561 Set_Raises_Constraint_Error (N);
1562 Set_Etype (N, Aggr_Subtyp);
1563 Set_Analyzed (N);
1564 end if;
1565
1566 if Warn_On_No_Value_Assigned
1567 and then Others_Box
1568 and then not Is_Fully_Initialized_Type (Etype (N))
1569 then
1570 Error_Msg_N ("?v?aggregate not fully initialized", N);
1571 end if;
1572
1573 Check_Function_Writable_Actuals (N);
1574 end Resolve_Aggregate;
1575
1576 -----------------------------
1577 -- Resolve_Array_Aggregate --
1578 -----------------------------
1579
1580 function Resolve_Array_Aggregate
1581 (N : Node_Id;
1582 Index : Node_Id;
1583 Index_Constr : Node_Id;
1584 Component_Typ : Entity_Id;
1585 Iterated : Boolean;
1586 Others_Allowed : Boolean) return Boolean
1587 is
1588 Loc : constant Source_Ptr := Sloc (N);
1589
1590 Failure : constant Boolean := False;
1591 Success : constant Boolean := True;
1592
1593 Ctyp : constant Entity_Id :=
1594 Get_Corresponding_Mutably_Tagged_Type_If_Present (Component_Typ);
1595
1596 Index_Typ : constant Entity_Id := Etype (Index);
1597 Index_Typ_Low : constant Node_Id := Type_Low_Bound (Index_Typ);
1598 Index_Typ_High : constant Node_Id := Type_High_Bound (Index_Typ);
1599 -- The type of the index corresponding to the array sub-aggregate along
1600 -- with its low and upper bounds.
1601
1602 Index_Base : constant Entity_Id := Base_Type (Index_Typ);
1603 Index_Base_Low : constant Node_Id := Type_Low_Bound (Index_Base);
1604 Index_Base_High : constant Node_Id := Type_High_Bound (Index_Base);
1605 -- Ditto for the base type
1606
1607 Others_N : Node_Id := Empty;
1608
1609 Nb_Choices : Nat := 0;
1610 -- Contains the overall number of named choices in this sub-aggregate
1611
1612 Saved_SED : constant Nat := Serious_Errors_Detected;
1613
1614 function Add (Val : Uint; To : Node_Id) return Node_Id;
1615 -- Creates a new expression node where Val is added to expression To.
1616 -- Tries to constant fold whenever possible. To must be an already
1617 -- analyzed expression.
1618
1619 procedure Check_Bound (BH : Node_Id; AH : in out Node_Id);
1620 -- Checks that AH (the upper bound of an array aggregate) is less than
1621 -- or equal to BH (the upper bound of the index base type). If the check
1622 -- fails, a warning is emitted, the Raises_Constraint_Error flag of N is
1623 -- set, and AH is replaced with a duplicate of BH.
1624
1625 procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id);
1626 -- Checks that range AL .. AH is compatible with range L .. H. Emits a
1627 -- warning if not and sets the Raises_Constraint_Error flag in N.
1628
1629 procedure Check_Length (L, H : Node_Id; Len : Uint);
1630 -- Checks that range L .. H contains at least Len elements. Emits a
1631 -- warning if not and sets the Raises_Constraint_Error flag in N.
1632
1633 function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean;
1634 -- Returns True if range L .. H is dynamic or null
1635
1636 procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean);
1637 -- Given expression node From, this routine sets OK to False if it
1638 -- cannot statically evaluate From. Otherwise it stores this static
1639 -- value into Value.
1640
1641 function Has_Null_Aggregate_Raising_Constraint_Error
1642 (Expr : Node_Id) return Boolean;
1643 -- Determines if the given expression has some null aggregate that will
1644 -- cause raising CE at runtime.
1645
1646 function Resolve_Aggr_Expr
1647 (Expr : Node_Id;
1648 Iterated_Elmt : Boolean := False;
1649 Single_Elmt : Boolean := True) return Boolean;
1650 -- Resolves aggregate expression Expr. Returns False if resolution
1651 -- fails. If Single_Elmt is set to False, the expression Expr may be
1652 -- used to initialize several array aggregate elements (this can happen
1653 -- for discrete choices such as "L .. H => Expr" or the OTHERS choice).
1654 -- In this event we do not resolve Expr unless expansion is disabled.
1655 -- To know why, see the DELAYED COMPONENT RESOLUTION note above.
1656
1657 function Resolve_Iterated_Component_Association
1658 (N : Node_Id;
1659 Index_Typ : Entity_Id) return Boolean;
1660 -- For AI12-061: resolves iterated component association N of Index_Typ.
1661 -- Returns False if resolution fails.
1662
1663 function Subtract (Val : Uint; To : Node_Id) return Node_Id;
1664 -- Creates a new expression node where Val is subtracted to expression
1665 -- To. Tries to constant fold whenever possible. To must be an already
1666 -- analyzed expression.
1667
1668 procedure Warn_On_Null_Component_Association (Expr : Node_Id);
1669 -- Expr is either a conditional expression or a case expression of an
1670 -- iterated component association initializing the aggregate N with
1671 -- components that can never be null. Report warning on associations
1672 -- that may initialize some component with a null value.
1673
1674 ---------
1675 -- Add --
1676 ---------
1677
1678 function Add (Val : Uint; To : Node_Id) return Node_Id is
1679 Expr_Pos : Node_Id;
1680 Expr : Node_Id;
1681 To_Pos : Node_Id;
1682
1683 begin
1684 if Raises_Constraint_Error (To) then
1685 return To;
1686 end if;
1687
1688 -- First test if we can do constant folding
1689
1690 if Compile_Time_Known_Value (To)
1691 or else Nkind (To) = N_Integer_Literal
1692 then
1693 Expr_Pos := Make_Integer_Literal (Loc, Expr_Value (To) + Val);
1694 Set_Is_Static_Expression (Expr_Pos);
1695 Set_Etype (Expr_Pos, Etype (To));
1696 Set_Analyzed (Expr_Pos, Analyzed (To));
1697
1698 if not Is_Enumeration_Type (Index_Typ) then
1699 Expr := Expr_Pos;
1700
1701 -- If we are dealing with enumeration return
1702 -- Index_Typ'Val (Expr_Pos)
1703
1704 else
1705 Expr :=
1706 Make_Attribute_Reference
1707 (Loc,
1708 Prefix => New_Occurrence_Of (Index_Typ, Loc),
1709 Attribute_Name => Name_Val,
1710 Expressions => New_List (Expr_Pos));
1711 end if;
1712
1713 return Expr;
1714 end if;
1715
1716 -- If we are here no constant folding possible
1717
1718 if not Is_Enumeration_Type (Index_Base) then
1719 Expr :=
1720 Make_Op_Add (Loc,
1721 Left_Opnd => Duplicate_Subexpr (To),
1722 Right_Opnd => Make_Integer_Literal (Loc, Val));
1723
1724 -- If we are dealing with enumeration return
1725 -- Index_Typ'Val (Index_Typ'Pos (To) + Val)
1726
1727 else
1728 To_Pos :=
1729 Make_Attribute_Reference
1730 (Loc,
1731 Prefix => New_Occurrence_Of (Index_Typ, Loc),
1732 Attribute_Name => Name_Pos,
1733 Expressions => New_List (Duplicate_Subexpr (To)));
1734
1735 Expr_Pos :=
1736 Make_Op_Add (Loc,
1737 Left_Opnd => To_Pos,
1738 Right_Opnd => Make_Integer_Literal (Loc, Val));
1739
1740 Expr :=
1741 Make_Attribute_Reference
1742 (Loc,
1743 Prefix => New_Occurrence_Of (Index_Typ, Loc),
1744 Attribute_Name => Name_Val,
1745 Expressions => New_List (Expr_Pos));
1746
1747 -- If the index type has a non standard representation, the
1748 -- attributes 'Val and 'Pos expand into function calls and the
1749 -- resulting expression is considered non-safe for reevaluation
1750 -- by the backend. Relocate it into a constant temporary in order
1751 -- to make it safe for reevaluation.
1752
1753 if Has_Non_Standard_Rep (Etype (N)) then
1754 declare
1755 Def_Id : Entity_Id;
1756
1757 begin
1758 Def_Id := Make_Temporary (Loc, 'R', Expr);
1759 Set_Etype (Def_Id, Index_Typ);
1760 Insert_Action (N,
1761 Make_Object_Declaration (Loc,
1762 Defining_Identifier => Def_Id,
1763 Object_Definition =>
1764 New_Occurrence_Of (Index_Typ, Loc),
1765 Constant_Present => True,
1766 Expression => Relocate_Node (Expr)));
1767
1768 Expr := New_Occurrence_Of (Def_Id, Loc);
1769 end;
1770 end if;
1771 end if;
1772
1773 return Expr;
1774 end Add;
1775
1776 -----------------
1777 -- Check_Bound --
1778 -----------------
1779
1780 procedure Check_Bound (BH : Node_Id; AH : in out Node_Id) is
1781 Val_BH : Uint;
1782 Val_AH : Uint;
1783
1784 OK_BH : Boolean;
1785 OK_AH : Boolean;
1786
1787 begin
1788 Get (Value => Val_BH, From => BH, OK => OK_BH);
1789 Get (Value => Val_AH, From => AH, OK => OK_AH);
1790
1791 if OK_BH and then OK_AH and then Val_BH < Val_AH then
1792 Set_Raises_Constraint_Error (N);
1793 Error_Msg_Warn := SPARK_Mode /= On;
1794 Error_Msg_N ("upper bound out of range<<", AH);
1795 Error_Msg_N ("\Constraint_Error [<<", AH);
1796
1797 -- You need to set AH to BH or else in the case of enumerations
1798 -- indexes we will not be able to resolve the aggregate bounds.
1799
1800 AH := Duplicate_Subexpr (BH);
1801 end if;
1802 end Check_Bound;
1803
1804 ------------------
1805 -- Check_Bounds --
1806 ------------------
1807
1808 procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id) is
1809 Val_L : Uint;
1810 Val_H : Uint;
1811 Val_AL : Uint;
1812 Val_AH : Uint;
1813
1814 OK_L : Boolean;
1815 OK_H : Boolean;
1816
1817 OK_AL : Boolean;
1818 OK_AH : Boolean;
1819 pragma Warnings (Off, OK_AL);
1820 pragma Warnings (Off, OK_AH);
1821
1822 begin
1823 if Raises_Constraint_Error (N)
1824 or else Dynamic_Or_Null_Range (AL, AH)
1825 then
1826 return;
1827 end if;
1828
1829 Get (Value => Val_L, From => L, OK => OK_L);
1830 Get (Value => Val_H, From => H, OK => OK_H);
1831
1832 Get (Value => Val_AL, From => AL, OK => OK_AL);
1833 Get (Value => Val_AH, From => AH, OK => OK_AH);
1834
1835 if OK_L and then Val_L > Val_AL then
1836 Set_Raises_Constraint_Error (N);
1837 Error_Msg_Warn := SPARK_Mode /= On;
1838 Error_Msg_N ("lower bound of aggregate out of range<<", N);
1839 Error_Msg_N ("\Constraint_Error [<<", N);
1840 end if;
1841
1842 if OK_H and then Val_H < Val_AH then
1843 Set_Raises_Constraint_Error (N);
1844 Error_Msg_Warn := SPARK_Mode /= On;
1845 Error_Msg_N ("upper bound of aggregate out of range<<", N);
1846 Error_Msg_N ("\Constraint_Error [<<", N);
1847 end if;
1848 end Check_Bounds;
1849
1850 ------------------
1851 -- Check_Length --
1852 ------------------
1853
1854 procedure Check_Length (L, H : Node_Id; Len : Uint) is
1855 Val_L : Uint;
1856 Val_H : Uint;
1857
1858 OK_L : Boolean;
1859 OK_H : Boolean;
1860
1861 Range_Len : Uint;
1862
1863 begin
1864 if Raises_Constraint_Error (N) then
1865 return;
1866 end if;
1867
1868 Get (Value => Val_L, From => L, OK => OK_L);
1869 Get (Value => Val_H, From => H, OK => OK_H);
1870
1871 if not OK_L or else not OK_H then
1872 return;
1873 end if;
1874
1875 -- If null range length is zero
1876
1877 if Val_L > Val_H then
1878 Range_Len := Uint_0;
1879 else
1880 Range_Len := Val_H - Val_L + 1;
1881 end if;
1882
1883 if Range_Len < Len then
1884 Set_Raises_Constraint_Error (N);
1885 Error_Msg_Warn := SPARK_Mode /= On;
1886 Error_Msg_N ("too many elements<<", N);
1887 Error_Msg_N ("\Constraint_Error [<<", N);
1888 end if;
1889 end Check_Length;
1890
1891 ---------------------------
1892 -- Dynamic_Or_Null_Range --
1893 ---------------------------
1894
1895 function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean is
1896 Val_L : Uint;
1897 Val_H : Uint;
1898
1899 OK_L : Boolean;
1900 OK_H : Boolean;
1901
1902 begin
1903 Get (Value => Val_L, From => L, OK => OK_L);
1904 Get (Value => Val_H, From => H, OK => OK_H);
1905
1906 return not OK_L or else not OK_H
1907 or else not Is_OK_Static_Expression (L)
1908 or else not Is_OK_Static_Expression (H)
1909 or else Val_L > Val_H;
1910 end Dynamic_Or_Null_Range;
1911
1912 ---------
1913 -- Get --
1914 ---------
1915
1916 procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean) is
1917 begin
1918 OK := True;
1919
1920 if Compile_Time_Known_Value (From) then
1921 Value := Expr_Value (From);
1922
1923 -- If expression From is something like Some_Type'Val (10) then
1924 -- Value = 10.
1925
1926 elsif Nkind (From) = N_Attribute_Reference
1927 and then Attribute_Name (From) = Name_Val
1928 and then Compile_Time_Known_Value (First (Expressions (From)))
1929 then
1930 Value := Expr_Value (First (Expressions (From)));
1931 else
1932 Value := Uint_0;
1933 OK := False;
1934 end if;
1935 end Get;
1936
1937 -------------------------------------------------
1938 -- Has_Null_Aggregate_Raising_Constraint_Error --
1939 -------------------------------------------------
1940
1941 function Has_Null_Aggregate_Raising_Constraint_Error
1942 (Expr : Node_Id) return Boolean
1943 is
1944 function Process (N : Node_Id) return Traverse_Result;
1945 -- Process one node in search for generic formal type
1946
1947 -------------
1948 -- Process --
1949 -------------
1950
1951 function Process (N : Node_Id) return Traverse_Result is
1952 begin
1953 if Nkind (N) = N_Aggregate
1954 and then Is_Null_Aggregate (N)
1955 and then Raises_Constraint_Error (N)
1956 then
1957 return Abandon;
1958 end if;
1959
1960 return OK;
1961 end Process;
1962
1963 function Traverse is new Traverse_Func (Process);
1964 -- Traverse tree to look for null aggregates that will raise CE
1965
1966 -- Start of processing for Has_Null_Aggregate_Raising_Constraint_Error
1967
1968 begin
1969 return Traverse (Expr) = Abandon;
1970 end Has_Null_Aggregate_Raising_Constraint_Error;
1971
1972 -----------------------
1973 -- Resolve_Aggr_Expr --
1974 -----------------------
1975
1976 function Resolve_Aggr_Expr
1977 (Expr : Node_Id;
1978 Iterated_Elmt : Boolean := False;
1979 Single_Elmt : Boolean := True) return Boolean
1980 is
1981 Iterated_Expr : constant Boolean := Iterated_Elmt or else Iterated;
1982 -- True if the Expr is in an iteration context, possibly nested
1983
1984 Nxt_Ind : constant Node_Id := Next_Index (Index);
1985 Nxt_Ind_Constr : constant Node_Id := Next_Index (Index_Constr);
1986 -- Index is the current index corresponding to the expression
1987
1988 Resolution_OK : Boolean := True;
1989 -- Set to False if resolution of the expression failed
1990
1991 begin
1992 -- Defend against previous errors
1993
1994 if Nkind (Expr) = N_Error
1995 or else Error_Posted (Expr)
1996 then
1997 return True;
1998 end if;
1999
2000 -- If the array type against which we are resolving the aggregate
2001 -- has several dimensions, the expressions nested inside the
2002 -- aggregate must be further aggregates (or strings).
2003
2004 if Present (Nxt_Ind) then
2005 if Nkind (Expr) /= N_Aggregate then
2006
2007 -- A string literal can appear where a one-dimensional array
2008 -- of characters is expected. If the literal looks like an
2009 -- operator, it is still an operator symbol, which will be
2010 -- transformed into a string when analyzed.
2011
2012 if Is_Character_Type (Ctyp)
2013 and then No (Next_Index (Nxt_Ind))
2014 and then Nkind (Expr) in N_String_Literal | N_Operator_Symbol
2015 then
2016 -- A string literal used in a multidimensional array
2017 -- aggregate in place of the final one-dimensional
2018 -- aggregate must not be enclosed in parentheses.
2019
2020 if Paren_Count (Expr) /= 0 then
2021 Error_Msg_N ("no parenthesis allowed here", Expr);
2022 end if;
2023
2024 Make_String_Into_Aggregate (Expr);
2025
2026 else
2027 Error_Msg_N ("nested array aggregate expected", Expr);
2028
2029 -- If the expression is parenthesized, this may be
2030 -- a missing component association for a 1-aggregate.
2031
2032 if Paren_Count (Expr) > 0 then
2033 Error_Msg_N
2034 ("\if single-component aggregate is intended, "
2035 & "write e.g. (1 ='> ...)", Expr);
2036 end if;
2037
2038 return Failure;
2039 end if;
2040 end if;
2041
2042 -- Ada 2005 (AI-231): Propagate the type to the nested aggregate.
2043 -- Required to check the null-exclusion attribute (if present).
2044 -- This value may be overridden later on.
2045
2046 Set_Etype (Expr, Etype (N));
2047
2048 Resolution_OK :=
2049 Resolve_Array_Aggregate
2050 (Expr, Nxt_Ind, Nxt_Ind_Constr, Ctyp,
2051 Iterated => Iterated_Expr, Others_Allowed => Others_Allowed);
2052
2053 if Resolution_OK = Failure then
2054 return Failure;
2055 end if;
2056
2057 else
2058 -- In an iterated context, preanalyze a copy of the expression to
2059 -- verify legality. We use a copy because the expression will be
2060 -- analyzed anew when the enclosing aggregate is expanded and the
2061 -- construct is rewritten as a loop with a new iteration variable.
2062 -- This does not apply to SPARK mode, where expansion is skipped.
2063
2064 -- If the parent is a component association, we also temporarily
2065 -- point its Expression field to the copy, because analysis may
2066 -- expect this invariant to hold.
2067
2068 if Iterated_Expr and then not GNATprove_Mode then
2069 declare
2070 In_Assoc : constant Boolean :=
2071 Nkind (Parent (Expr)) in N_Component_Association
2072 | N_Iterated_Component_Association;
2073 New_Expr : constant Node_Id := Copy_Separate_Tree (Expr);
2074
2075 begin
2076 Set_Parent (New_Expr, Parent (Expr));
2077 if In_Assoc then
2078 Set_Expression (Parent (Expr), New_Expr);
2079 end if;
2080
2081 Preanalyze_And_Resolve (New_Expr, Ctyp);
2082 Check_Expr_OK_In_Limited_Aggregate (New_Expr);
2083 Check_Expression_Dimensions (New_Expr, Ctyp);
2084
2085 if In_Assoc then
2086 Set_Expression (Parent (Expr), Expr);
2087 end if;
2088 end;
2089
2090 -- If the expander is active and the choice may cover multiple
2091 -- components, then we cannot expand (see the spec of Sem), so
2092 -- we preanalyze the expression.
2093
2094 elsif Expander_Active and then not Single_Elmt then
2095 Preanalyze_And_Resolve (Expr, Ctyp);
2096 Check_Expr_OK_In_Limited_Aggregate (Expr);
2097 Check_Expression_Dimensions (Expr, Ctyp);
2098
2099 -- The range given by the choice may be empty, in which case we
2100 -- do not want spurious warnings about CE raised at run time.
2101
2102 Remove_Warning_Messages (Expr);
2103
2104 -- Otherwise, we perform a full analysis of the expression
2105
2106 else
2107 Analyze_And_Resolve (Expr, Ctyp);
2108 Check_Expr_OK_In_Limited_Aggregate (Expr);
2109 Check_Expression_Dimensions (Expr, Ctyp);
2110 Check_Non_Static_Context (Expr);
2111 Check_Unset_Reference (Expr);
2112 Aggregate_Constraint_Checks (Expr, Ctyp);
2113 end if;
2114 end if;
2115
2116 -- If an aggregate component has a type with predicates, an explicit
2117 -- predicate check must be applied, as for an assignment statement,
2118 -- because the aggregate might not be expanded into individual
2119 -- component assignments. If the expression covers several components
2120 -- the analysis and the predicate check take place later.
2121
2122 if Has_Predicates (Ctyp)
2123 and then Analyzed (Expr)
2124 then
2125 Apply_Predicate_Check (Expr, Ctyp);
2126 end if;
2127
2128 if Raises_Constraint_Error (Expr)
2129 and then (Nkind (Parent (Expr)) /= N_Component_Association
2130 or else Is_Null_Aggregate (Expr))
2131 then
2132 Set_Raises_Constraint_Error (N);
2133 end if;
2134
2135 -- If the expression has been marked as requiring a range check,
2136 -- then generate it here. It's a bit odd to be generating such
2137 -- checks in the analyzer, but harmless since Generate_Range_Check
2138 -- does nothing (other than making sure Do_Range_Check is set) if
2139 -- the expander is not active.
2140
2141 if Do_Range_Check (Expr) then
2142 Generate_Range_Check (Expr, Ctyp, CE_Range_Check_Failed);
2143 end if;
2144
2145 return Resolution_OK;
2146 end Resolve_Aggr_Expr;
2147
2148 --------------------------------------------
2149 -- Resolve_Iterated_Component_Association --
2150 --------------------------------------------
2151
2152 function Resolve_Iterated_Component_Association
2153 (N : Node_Id;
2154 Index_Typ : Entity_Id) return Boolean
2155 is
2156 Loc : constant Source_Ptr := Sloc (N);
2157 Id : constant Entity_Id := Defining_Identifier (N);
2158 Expr : constant Node_Id := Expression (N);
2159
2160 -- Local variables
2161
2162 Choice : Node_Id;
2163 Resolution_OK : Boolean;
2164 Scop : Entity_Id;
2165
2166 -- Start of processing for Resolve_Iterated_Component_Association
2167
2168 begin
2169 Error_Msg_Ada_2022_Feature ("iterated component", Loc);
2170
2171 -- Create a scope in which to introduce an index, to make it visible
2172 -- for the analysis of component expression.
2173
2174 Scop := New_Internal_Entity (E_Loop, Current_Scope, Loc, 'L');
2175 Set_Etype (Scop, Standard_Void_Type);
2176 Set_Parent (Scop, Parent (N));
2177 Push_Scope (Scop);
2178
2179 -- If there is iterator specification, then its preanalysis will make
2180 -- the index visible.
2181
2182 if Present (Iterator_Specification (N)) then
2183 Preanalyze (Iterator_Specification (N));
2184
2185 -- Otherwise, analyze discrete choices and make the index visible
2186
2187 else
2188 -- Insert index name into current scope but don't decorate it yet,
2189 -- so that a premature usage of this name in discrete choices will
2190 -- be nicely diagnosed.
2191
2192 Enter_Name (Id);
2193
2194 Choice := First (Discrete_Choices (N));
2195
2196 while Present (Choice) loop
2197 if Nkind (Choice) = N_Others_Choice then
2198 Others_N := Choice;
2199
2200 else
2201 Analyze (Choice);
2202
2203 -- Choice can be a subtype name, a range, or an expression
2204
2205 if Is_Entity_Name (Choice)
2206 and then Is_Type (Entity (Choice))
2207 and then
2208 Base_Type (Entity (Choice)) = Base_Type (Index_Typ)
2209 then
2210 null;
2211
2212 else
2213 Analyze_And_Resolve (Choice, Index_Typ);
2214 end if;
2215 end if;
2216
2217 Next (Choice);
2218 end loop;
2219
2220 -- Decorate the index variable
2221
2222 Set_Etype (Id, Index_Typ);
2223 Mutate_Ekind (Id, E_Variable);
2224 Set_Is_Not_Self_Hidden (Id);
2225 Set_Scope (Id, Scop);
2226 end if;
2227
2228 -- Analyze expression without expansion, to verify legality
2229
2230 Resolution_OK := Resolve_Aggr_Expr (Expr, Iterated_Elmt => True);
2231
2232 End_Scope;
2233
2234 return Resolution_OK;
2235 end Resolve_Iterated_Component_Association;
2236
2237 --------------
2238 -- Subtract --
2239 --------------
2240
2241 function Subtract (Val : Uint; To : Node_Id) return Node_Id is
2242 Expr_Pos : Node_Id;
2243 Expr : Node_Id;
2244 To_Pos : Node_Id;
2245
2246 begin
2247 if Raises_Constraint_Error (To) then
2248 return To;
2249 end if;
2250
2251 -- First test if we can do constant folding
2252
2253 if Compile_Time_Known_Value (To)
2254 or else Nkind (To) = N_Integer_Literal
2255 then
2256 Expr_Pos := Make_Integer_Literal (Loc, Expr_Value (To) - Val);
2257 Set_Is_Static_Expression (Expr_Pos);
2258 Set_Etype (Expr_Pos, Etype (To));
2259 Set_Analyzed (Expr_Pos, Analyzed (To));
2260
2261 if not Is_Enumeration_Type (Index_Typ) then
2262 Expr := Expr_Pos;
2263
2264 -- If we are dealing with enumeration return
2265 -- Index_Typ'Val (Expr_Pos)
2266
2267 else
2268 Expr :=
2269 Make_Attribute_Reference
2270 (Loc,
2271 Prefix => New_Occurrence_Of (Index_Typ, Loc),
2272 Attribute_Name => Name_Val,
2273 Expressions => New_List (Expr_Pos));
2274 end if;
2275
2276 return Expr;
2277 end if;
2278
2279 -- If we are here no constant folding possible
2280
2281 if not Is_Enumeration_Type (Index_Base) then
2282 Expr :=
2283 Make_Op_Subtract (Loc,
2284 Left_Opnd => Duplicate_Subexpr (To),
2285 Right_Opnd => Make_Integer_Literal (Loc, Val));
2286
2287 -- If we are dealing with enumeration return
2288 -- Index_Typ'Val (Index_Typ'Pos (To) - Val)
2289
2290 else
2291 To_Pos :=
2292 Make_Attribute_Reference
2293 (Loc,
2294 Prefix => New_Occurrence_Of (Index_Typ, Loc),
2295 Attribute_Name => Name_Pos,
2296 Expressions => New_List (Duplicate_Subexpr (To)));
2297
2298 Expr_Pos :=
2299 Make_Op_Subtract (Loc,
2300 Left_Opnd => To_Pos,
2301 Right_Opnd => Make_Integer_Literal (Loc, Val));
2302
2303 Expr :=
2304 Make_Attribute_Reference
2305 (Loc,
2306 Prefix => New_Occurrence_Of (Index_Typ, Loc),
2307 Attribute_Name => Name_Val,
2308 Expressions => New_List (Expr_Pos));
2309
2310 -- If the index type has a non standard representation, the
2311 -- attributes 'Val and 'Pos expand into function calls and the
2312 -- resulting expression is considered non-safe for reevaluation
2313 -- by the backend. Relocate it into a constant temporary in order
2314 -- to make it safe for reevaluation.
2315
2316 if Has_Non_Standard_Rep (Etype (N)) then
2317 declare
2318 Def_Id : Entity_Id;
2319
2320 begin
2321 Def_Id := Make_Temporary (Loc, 'R', Expr);
2322 Set_Etype (Def_Id, Index_Typ);
2323 Insert_Action (N,
2324 Make_Object_Declaration (Loc,
2325 Defining_Identifier => Def_Id,
2326 Object_Definition =>
2327 New_Occurrence_Of (Index_Typ, Loc),
2328 Constant_Present => True,
2329 Expression => Relocate_Node (Expr)));
2330
2331 Expr := New_Occurrence_Of (Def_Id, Loc);
2332 end;
2333 end if;
2334 end if;
2335
2336 return Expr;
2337 end Subtract;
2338
2339 ----------------------------------------
2340 -- Warn_On_Null_Component_Association --
2341 ----------------------------------------
2342
2343 procedure Warn_On_Null_Component_Association (Expr : Node_Id) is
2344 procedure Check_Case_Expr (N : Node_Id);
2345 -- Check if a case expression may initialize some component with a
2346 -- null value.
2347
2348 procedure Check_Cond_Expr (N : Node_Id);
2349 -- Check if a conditional expression may initialize some component
2350 -- with a null value.
2351
2352 procedure Check_Expr (Expr : Node_Id);
2353 -- Check if an expression may initialize some component with a
2354 -- null value.
2355
2356 procedure Warn_On_Null_Expression_And_Rewrite (Null_Expr : Node_Id);
2357 -- Report warning on known null expression and replace the expression
2358 -- by a raise constraint error node.
2359
2360 ---------------------
2361 -- Check_Case_Expr --
2362 ---------------------
2363
2364 procedure Check_Case_Expr (N : Node_Id) is
2365 Alt_Node : Node_Id := First (Alternatives (N));
2366
2367 begin
2368 while Present (Alt_Node) loop
2369 Check_Expr (Expression (Alt_Node));
2370 Next (Alt_Node);
2371 end loop;
2372 end Check_Case_Expr;
2373
2374 ---------------------
2375 -- Check_Cond_Expr --
2376 ---------------------
2377
2378 procedure Check_Cond_Expr (N : Node_Id) is
2379 If_Expr : Node_Id := N;
2380 Then_Expr : Node_Id;
2381 Else_Expr : Node_Id;
2382
2383 begin
2384 Then_Expr := Next (First (Expressions (If_Expr)));
2385 Else_Expr := Next (Then_Expr);
2386
2387 Check_Expr (Then_Expr);
2388
2389 -- Process elsif parts (if any)
2390
2391 while Nkind (Else_Expr) = N_If_Expression loop
2392 If_Expr := Else_Expr;
2393 Then_Expr := Next (First (Expressions (If_Expr)));
2394 Else_Expr := Next (Then_Expr);
2395
2396 Check_Expr (Then_Expr);
2397 end loop;
2398
2399 if Known_Null (Else_Expr) then
2400 Warn_On_Null_Expression_And_Rewrite (Else_Expr);
2401 end if;
2402 end Check_Cond_Expr;
2403
2404 ----------------
2405 -- Check_Expr --
2406 ----------------
2407
2408 procedure Check_Expr (Expr : Node_Id) is
2409 begin
2410 if Known_Null (Expr) then
2411 Warn_On_Null_Expression_And_Rewrite (Expr);
2412
2413 elsif Nkind (Expr) = N_If_Expression then
2414 Check_Cond_Expr (Expr);
2415
2416 elsif Nkind (Expr) = N_Case_Expression then
2417 Check_Case_Expr (Expr);
2418 end if;
2419 end Check_Expr;
2420
2421 -----------------------------------------
2422 -- Warn_On_Null_Expression_And_Rewrite --
2423 -----------------------------------------
2424
2425 procedure Warn_On_Null_Expression_And_Rewrite (Null_Expr : Node_Id) is
2426 begin
2427 Error_Msg_N
2428 ("(Ada 2005) NULL not allowed in null-excluding component??",
2429 Null_Expr);
2430 Error_Msg_N
2431 ("\Constraint_Error might be raised at run time??", Null_Expr);
2432
2433 -- We cannot use Apply_Compile_Time_Constraint_Error because in
2434 -- some cases the components are rewritten and the runtime error
2435 -- would be missed.
2436
2437 Rewrite (Null_Expr,
2438 Make_Raise_Constraint_Error (Sloc (Null_Expr),
2439 Reason => CE_Access_Check_Failed));
2440
2441 Set_Etype (Null_Expr, Ctyp);
2442 Set_Analyzed (Null_Expr);
2443 end Warn_On_Null_Expression_And_Rewrite;
2444
2445 -- Start of processing for Warn_On_Null_Component_Association
2446
2447 begin
2448 pragma Assert (Can_Never_Be_Null (Ctyp));
2449
2450 case Nkind (Expr) is
2451 when N_If_Expression =>
2452 Check_Cond_Expr (Expr);
2453
2454 when N_Case_Expression =>
2455 Check_Case_Expr (Expr);
2456
2457 when others =>
2458 pragma Assert (False);
2459 null;
2460 end case;
2461 end Warn_On_Null_Component_Association;
2462
2463 -- Local variables
2464
2465 Assoc : Node_Id;
2466 Choice : Node_Id;
2467 Expr : Node_Id;
2468 Discard : Node_Id;
2469
2470 Aggr_Low : Node_Id := Empty;
2471 Aggr_High : Node_Id := Empty;
2472 -- The actual low and high bounds of this sub-aggregate
2473
2474 Case_Table_Size : Nat;
2475 -- Contains the size of the case table needed to sort aggregate choices
2476
2477 Choices_Low : Node_Id := Empty;
2478 Choices_High : Node_Id := Empty;
2479 -- The lowest and highest discrete choices values for a named aggregate
2480
2481 Delete_Choice : Boolean;
2482 -- Used when replacing a subtype choice with predicate by a list
2483
2484 Has_Iterator_Specifications : Boolean := False;
2485 -- Flag to indicate that all named associations are iterated component
2486 -- associations with iterator specifications, in which case the
2487 -- expansion will create two loops: one to evaluate the size and one
2488 -- to generate the elements (4.3.3 (20.2/5)).
2489
2490 Nb_Elements : Uint := Uint_0;
2491 -- The number of elements in a positional aggregate
2492
2493 Nb_Discrete_Choices : Nat := 0;
2494 -- The overall number of discrete choices (not counting others choice)
2495
2496 -- Start of processing for Resolve_Array_Aggregate
2497
2498 begin
2499 -- Ignore junk empty aggregate resulting from parser error
2500
2501 if No (Expressions (N))
2502 and then No (Component_Associations (N))
2503 and then not Null_Record_Present (N)
2504 then
2505 return Failure;
2506 end if;
2507
2508 -- Disable the warning for GNAT Mode to allow for easier transition.
2509
2510 -- We don't warn about obsolescent usage of parentheses in generic
2511 -- instances for two reasons:
2512 --
2513 -- 1. An equivalent warning has been emitted in the corresponding
2514 -- definition.
2515 -- 2. In cases where a generic definition specifies a version older than
2516 -- Ada 2022 through a pragma and rightfully uses parentheses for
2517 -- an array aggregate, an incorrect warning would be raised in
2518 -- instances of that generic that are in Ada 2022 or later if we
2519 -- didn't filter out the instance case.
2520
2521 if Ada_Version_Explicit >= Ada_2022
2522 and then Warn_On_Obsolescent_Feature
2523 and then not GNAT_Mode
2524 and then not Is_Homogeneous_Aggregate (N)
2525 and then Is_Parenthesis_Aggregate (N)
2526 and then Nkind (Parent (N)) /= N_Qualified_Expression
2527 and then Comes_From_Source (N)
2528 and then not In_Instance
2529 then
2530 Error_Msg_N
2531 ("?j?array aggregate using () is an" &
2532 " obsolescent syntax, use '['] instead", N);
2533 end if;
2534
2535 -- STEP 1: make sure the aggregate is correctly formatted
2536
2537 if Is_Null_Aggregate (N) then
2538 null;
2539
2540 elsif Present (Component_Associations (N)) then
2541 Assoc := First (Component_Associations (N));
2542
2543 -- Loop over associations to identify any iterated associations that
2544 -- need to be converted from the form with a Defining_Identifer and
2545 -- Discrete_Choices list to the form with an Iterator_Specification.
2546
2547 if Nkind (Assoc) = N_Iterated_Component_Association then
2548 while Present (Assoc) loop
2549 if Nkind (Assoc) = N_Iterated_Component_Association
2550 and then No (Iterator_Specification (Assoc))
2551 then
2552 declare
2553 Choice : constant Node_Id :=
2554 First (Discrete_Choices (Assoc));
2555 Copy : Node_Id;
2556 begin
2557
2558 -- A copy of Choice is made before it's analyzed,
2559 -- to preserve prefixed calls in their original form,
2560 -- because otherwise the analysis of Choice can transform
2561 -- such calls to normal form, and the later analysis of
2562 -- the iterator_specification created below may trigger
2563 -- an error on the call (in the case where the function
2564 -- is not directly visible).
2565
2566 Copy := Copy_Separate_Tree (Choice);
2567
2568 -- This is an association with a Defining_Identifier and
2569 -- Discrete_Choice_List, but if the latter has a single
2570 -- choice denoting an object (including a function call)
2571 -- of an iterator type, then it's a stand-in for an
2572 -- Iterator_Specification, and so we transform the
2573 -- association accordingly.
2574
2575 if No (Next (Choice)) then
2576 Analyze (Choice);
2577
2578 if Is_Object_Reference (Choice)
2579 and then Is_Iterator (Etype (Choice))
2580 then
2581 Set_Iterator_Specification
2582 (Assoc,
2583 Make_Iterator_Specification (Sloc (N),
2584 Defining_Identifier =>
2585 Relocate_Node (Defining_Identifier (Assoc)),
2586 Name => Copy,
2587 Reverse_Present => Reverse_Present (Assoc),
2588 Iterator_Filter => Empty,
2589 Subtype_Indication => Empty));
2590
2591 Set_Defining_Identifier (Assoc, Empty);
2592 Set_Discrete_Choices (Assoc, No_List);
2593 end if;
2594 end if;
2595 end;
2596 end if;
2597
2598 Next (Assoc);
2599 end loop;
2600 end if;
2601
2602 -- Verify that all or none of the component associations
2603 -- include an iterator specification.
2604
2605 Assoc := First (Component_Associations (N));
2606 if Nkind (Assoc) = N_Iterated_Component_Association
2607 and then Present (Iterator_Specification (Assoc))
2608 then
2609 if Number_Dimensions (Etype (N)) /= 1 then
2610 Error_Msg_N ("iterated_component_association with an" &
2611 " iterator_specification not allowed for" &
2612 " multidimensional array aggregate",
2613 Assoc);
2614 return Failure;
2615 end if;
2616
2617 -- All other component associations must have an iterator spec.
2618
2619 Next (Assoc);
2620 while Present (Assoc) loop
2621 if Nkind (Assoc) /= N_Iterated_Component_Association
2622 or else No (Iterator_Specification (Assoc))
2623 then
2624 Error_Msg_N ("mixed iterated component association"
2625 & " (RM 4.3.3 (17.1/5))",
2626 Assoc);
2627 return Failure;
2628 end if;
2629
2630 Next (Assoc);
2631 end loop;
2632
2633 Has_Iterator_Specifications := True;
2634
2635 else
2636 -- or none of them do.
2637
2638 Next (Assoc);
2639 while Present (Assoc) loop
2640 if Nkind (Assoc) = N_Iterated_Component_Association
2641 and then Present (Iterator_Specification (Assoc))
2642 then
2643 Error_Msg_N ("mixed iterated component association"
2644 & " (RM 4.3.3 (17.1/5))",
2645 Assoc);
2646 return Failure;
2647 end if;
2648
2649 Next (Assoc);
2650 end loop;
2651
2652 end if;
2653
2654 Assoc := First (Component_Associations (N));
2655 while Present (Assoc) loop
2656 if Nkind (Assoc) = N_Iterated_Component_Association then
2657 if not Resolve_Iterated_Component_Association
2658 (Assoc, Index_Typ)
2659 then
2660 return Failure;
2661 end if;
2662
2663 elsif Nkind (Assoc) /= N_Component_Association then
2664 Error_Msg_N
2665 ("invalid component association for aggregate", Assoc);
2666 return Failure;
2667 end if;
2668
2669 Choice := First (Choice_List (Assoc));
2670 Delete_Choice := False;
2671 while Present (Choice) loop
2672 if Nkind (Choice) = N_Others_Choice then
2673 Others_N := Choice;
2674
2675 if Choice /= First (Choice_List (Assoc))
2676 or else Present (Next (Choice))
2677 then
2678 Error_Msg_N
2679 ("OTHERS must appear alone in a choice list", Choice);
2680 return Failure;
2681 end if;
2682
2683 if Present (Next (Assoc)) then
2684 Error_Msg_N
2685 ("OTHERS must appear last in an aggregate", Choice);
2686 return Failure;
2687 end if;
2688
2689 if Ada_Version = Ada_83
2690 and then Assoc /= First (Component_Associations (N))
2691 and then Nkind (Parent (N)) in
2692 N_Assignment_Statement | N_Object_Declaration
2693 then
2694 Error_Msg_N
2695 ("(Ada 83) illegal context for OTHERS choice", N);
2696 end if;
2697
2698 elsif Is_Entity_Name (Choice) then
2699 Analyze (Choice);
2700
2701 declare
2702 E : constant Entity_Id := Entity (Choice);
2703 New_Cs : List_Id;
2704 P : Node_Id;
2705 C : Node_Id;
2706
2707 begin
2708 if Is_Type (E) and then Has_Predicates (E) then
2709 Freeze_Before (N, E);
2710
2711 if Has_Dynamic_Predicate_Aspect (E)
2712 or else Has_Ghost_Predicate_Aspect (E)
2713 then
2714 Error_Msg_NE
2715 ("subtype& has non-static predicate, not allowed "
2716 & "in aggregate choice", Choice, E);
2717
2718 elsif not Is_OK_Static_Subtype (E) then
2719 Error_Msg_NE
2720 ("non-static subtype& has predicate, not allowed "
2721 & "in aggregate choice", Choice, E);
2722 end if;
2723
2724 -- If the subtype has a static predicate, replace the
2725 -- original choice with the list of individual values
2726 -- covered by the predicate.
2727 -- This should be deferred to expansion time ???
2728
2729 if Present (Static_Discrete_Predicate (E)) then
2730 Delete_Choice := True;
2731
2732 New_Cs := New_List;
2733 P := First (Static_Discrete_Predicate (E));
2734 while Present (P) loop
2735 C := New_Copy (P);
2736 Set_Sloc (C, Sloc (Choice));
2737 Append_To (New_Cs, C);
2738 Next (P);
2739 end loop;
2740
2741 Insert_List_After (Choice, New_Cs);
2742 end if;
2743 end if;
2744 end;
2745 end if;
2746
2747 Nb_Choices := Nb_Choices + 1;
2748
2749 declare
2750 C : constant Node_Id := Choice;
2751
2752 begin
2753 Next (Choice);
2754
2755 if Delete_Choice then
2756 Remove (C);
2757 Nb_Choices := Nb_Choices - 1;
2758 Delete_Choice := False;
2759 end if;
2760 end;
2761 end loop;
2762
2763 Next (Assoc);
2764 end loop;
2765 end if;
2766
2767 -- At this point we know that the others choice, if present, is by
2768 -- itself and appears last in the aggregate. Check if we have mixed
2769 -- positional and discrete associations (other than the others choice).
2770
2771 if Present (Expressions (N))
2772 and then (Nb_Choices > 1
2773 or else (Nb_Choices = 1 and then No (Others_N)))
2774 then
2775 Error_Msg_N
2776 ("cannot mix named and positional associations in array aggregate",
2777 First (Choice_List (First (Component_Associations (N)))));
2778 return Failure;
2779 end if;
2780
2781 -- Test for the validity of an others choice if present
2782
2783 if Present (Others_N) and then not Others_Allowed then
2784 Error_Msg_N ("OTHERS choice not allowed here", Others_N);
2785 Error_Msg_N ("\qualify the aggregate with a constrained subtype "
2786 & "to provide bounds for it", Others_N);
2787 return Failure;
2788 end if;
2789
2790 -- Protect against cascaded errors
2791
2792 if Etype (Index_Typ) = Any_Type then
2793 return Failure;
2794 end if;
2795
2796 -- STEP 2: Process named components
2797
2798 if No (Expressions (N)) then
2799 if Present (Others_N) then
2800 Case_Table_Size := Nb_Choices - 1;
2801 else
2802 Case_Table_Size := Nb_Choices;
2803 end if;
2804
2805 Step_2 : declare
2806 function Empty_Range (A : Node_Id) return Boolean;
2807 -- If an association covers an empty range, some warnings on the
2808 -- expression of the association can be disabled.
2809
2810 -----------------
2811 -- Empty_Range --
2812 -----------------
2813
2814 function Empty_Range (A : Node_Id) return Boolean is
2815 R : constant Node_Id := First (Choice_List (A));
2816
2817 begin
2818 return No (Next (R))
2819 and then Nkind (R) = N_Range
2820 and then Compile_Time_Compare
2821 (Low_Bound (R), High_Bound (R), False) = GT;
2822 end Empty_Range;
2823
2824 -- Local variables
2825
2826 Low : Node_Id;
2827 High : Node_Id;
2828 -- Denote the lowest and highest values in an aggregate choice
2829
2830 S_Low : Node_Id := Empty;
2831 S_High : Node_Id := Empty;
2832 -- if a choice in an aggregate is a subtype indication these
2833 -- denote the lowest and highest values of the subtype
2834
2835 Table : Case_Table_Type (1 .. Case_Table_Size);
2836 -- Used to sort all the different choice values
2837
2838 Single_Choice : Boolean;
2839 -- Set to true every time there is a single discrete choice in a
2840 -- discrete association
2841
2842 Prev_Nb_Discrete_Choices : Nat;
2843 -- Used to keep track of the number of discrete choices in the
2844 -- current association.
2845
2846 Errors_Posted_On_Choices : Boolean := False;
2847 -- Keeps track of whether any choices have semantic errors
2848
2849 -- Start of processing for Step_2
2850
2851 begin
2852 -- STEP 2 (A): Check discrete choices validity
2853 -- No need if this is an element iteration.
2854
2855 Assoc := First (Component_Associations (N));
2856 while Present (Assoc)
2857 and then Present (Choice_List (Assoc))
2858 loop
2859 Prev_Nb_Discrete_Choices := Nb_Discrete_Choices;
2860 Choice := First (Choice_List (Assoc));
2861
2862 loop
2863 Analyze (Choice);
2864
2865 if Nkind (Choice) = N_Others_Choice then
2866 Single_Choice := False;
2867 exit;
2868
2869 -- Test for subtype mark without constraint
2870
2871 elsif Is_Entity_Name (Choice) and then
2872 Is_Type (Entity (Choice))
2873 then
2874 if Base_Type (Entity (Choice)) /= Index_Base then
2875 Error_Msg_N
2876 ("invalid subtype mark in aggregate choice",
2877 Choice);
2878 return Failure;
2879 end if;
2880
2881 -- Case of subtype indication
2882
2883 elsif Nkind (Choice) = N_Subtype_Indication then
2884 Resolve_Discrete_Subtype_Indication (Choice, Index_Base);
2885
2886 if Has_Dynamic_Predicate_Aspect
2887 (Entity (Subtype_Mark (Choice)))
2888 or else Has_Ghost_Predicate_Aspect
2889 (Entity (Subtype_Mark (Choice)))
2890 then
2891 Error_Msg_NE
2892 ("subtype& has non-static predicate, "
2893 & "not allowed in aggregate choice",
2894 Choice, Entity (Subtype_Mark (Choice)));
2895 end if;
2896
2897 -- Does the subtype indication evaluation raise CE?
2898
2899 Get_Index_Bounds (Subtype_Mark (Choice), S_Low, S_High);
2900 Get_Index_Bounds (Choice, Low, High);
2901 Check_Bounds (S_Low, S_High, Low, High);
2902
2903 -- Case of range or expression
2904
2905 else
2906 Resolve (Choice, Index_Base);
2907 Check_Unset_Reference (Choice);
2908 Check_Non_Static_Context (Choice);
2909
2910 -- If semantic errors were posted on the choice, then
2911 -- record that for possible early return from later
2912 -- processing (see handling of enumeration choices).
2913
2914 if Error_Posted (Choice) then
2915 Errors_Posted_On_Choices := True;
2916 end if;
2917
2918 -- Do not range check a choice. This check is redundant
2919 -- since this test is already done when we check that the
2920 -- bounds of the array aggregate are within range.
2921
2922 Set_Do_Range_Check (Choice, False);
2923 end if;
2924
2925 -- If we could not resolve the discrete choice stop here
2926
2927 if Etype (Choice) = Any_Type then
2928 return Failure;
2929
2930 -- If the discrete choice raises CE get its original bounds
2931
2932 elsif Nkind (Choice) = N_Raise_Constraint_Error then
2933 Set_Raises_Constraint_Error (N);
2934 Get_Index_Bounds (Original_Node (Choice), Low, High);
2935
2936 -- Otherwise get its bounds as usual
2937
2938 else
2939 Get_Index_Bounds (Choice, Low, High);
2940 end if;
2941
2942 if Dynamic_Or_Null_Range (Low, High)
2943 or else (Nkind (Choice) = N_Subtype_Indication
2944 and then Dynamic_Or_Null_Range (S_Low, S_High))
2945 then
2946 if Nb_Choices /= 1 then
2947 Error_Msg_N
2948 ("dynamic or empty choice in aggregate "
2949 & "must be the only choice", Choice);
2950 return Failure;
2951 elsif Number_Dimensions (Etype (N)) > 1 then
2952 declare
2953 function Check_Bound_Subexpression
2954 (Exp : Node_Id) return Traverse_Result;
2955 -- A bound expression for a subaggregate of an
2956 -- array aggregate is not permitted to reference
2957 -- a loop iteration variable defined in an earlier
2958 -- dimension of the same enclosing aggregate, as
2959 -- in (for X in 1 .. 3 => (1 .. X + 2 => ...)) .
2960 -- Always returns OK.
2961
2962 --------------------------------
2963 -- Check_Bound_Subexpression --
2964 --------------------------------
2965
2966 function Check_Bound_Subexpression
2967 (Exp : Node_Id) return Traverse_Result
2968 is
2969 Scope_Parent : Node_Id;
2970 begin
2971 if Nkind (Exp) /= N_Identifier
2972 or else No (Entity (Exp))
2973 or else No (Scope (Entity (Exp)))
2974 or else Ekind (Scope (Entity (Exp))) /= E_Loop
2975 then
2976 return OK;
2977 end if;
2978
2979 Scope_Parent := Parent (Scope (Entity (Exp)));
2980
2981 if Nkind (Scope_Parent) = N_Aggregate
2982
2983 -- We want to know whether the aggregate
2984 -- where this loop var is defined is
2985 -- "the same" aggregate as N, where "the
2986 -- same" means looking through subaggregates.
2987 -- To do this, we compare Etypes of the two.
2988 --
2989 -- ??? There may be very obscure cases
2990 -- involving allocators where this is too
2991 -- strict and will generate a spurious error.
2992
2993 and then Etype (Scope_Parent) = Etype (N)
2994 then
2995 Error_Msg_N ("bound expression for a "
2996 & "subaggregate of an array aggregate must "
2997 & "not refer to an index parameter of an "
2998 & "earlier dimension", Exp);
2999 end if;
3000
3001 return OK;
3002 end Check_Bound_Subexpression;
3003
3004 procedure Check_Bound_Expression is new
3005 Traverse_Proc (Check_Bound_Subexpression);
3006 begin
3007 Check_Bound_Expression (Low);
3008 Check_Bound_Expression (High);
3009 end;
3010 end if;
3011 end if;
3012
3013 if not (All_Composite_Constraints_Static (Low)
3014 and then All_Composite_Constraints_Static (High)
3015 and then All_Composite_Constraints_Static (S_Low)
3016 and then All_Composite_Constraints_Static (S_High))
3017 then
3018 Check_Restriction (No_Dynamic_Sized_Objects, Choice);
3019 end if;
3020
3021 Nb_Discrete_Choices := Nb_Discrete_Choices + 1;
3022 Table (Nb_Discrete_Choices).Lo := Low;
3023 Table (Nb_Discrete_Choices).Hi := High;
3024 Table (Nb_Discrete_Choices).Choice := Choice;
3025
3026 Next (Choice);
3027
3028 if No (Choice) then
3029
3030 -- Check if we have a single discrete choice and whether
3031 -- this discrete choice specifies a single value.
3032
3033 Single_Choice :=
3034 Nb_Discrete_Choices = Prev_Nb_Discrete_Choices + 1
3035 and then Low = High;
3036
3037 exit;
3038 end if;
3039 end loop;
3040
3041 -- Ada 2005 (AI-231)
3042
3043 if Ada_Version >= Ada_2005
3044 and then not Empty_Range (Assoc)
3045 then
3046 if Known_Null (Expression (Assoc)) then
3047 Check_Can_Never_Be_Null (Etype (N), Expression (Assoc));
3048
3049 -- Report warning on iterated component association that may
3050 -- initialize some component of an array of null-excluding
3051 -- access type components with a null value. For example:
3052
3053 -- type AList is array (...) of not null access Integer;
3054 -- L : AList :=
3055 -- [for J in A'Range =>
3056 -- (if Func (J) = 0 then A(J)'Access else Null)];
3057
3058 elsif Ada_Version >= Ada_2022
3059 and then Can_Never_Be_Null (Ctyp)
3060 and then Nkind (Assoc) = N_Iterated_Component_Association
3061 and then Nkind (Expression (Assoc)) in N_If_Expression
3062 | N_Case_Expression
3063 then
3064 Warn_On_Null_Component_Association (Expression (Assoc));
3065 end if;
3066 end if;
3067
3068 -- Ada 2005 (AI-287): In case of default initialized component
3069 -- we delay the resolution to the expansion phase.
3070
3071 if Box_Present (Assoc) then
3072
3073 -- Ada 2005 (AI-287): In case of default initialization of
3074 -- a component, the expander will generate calls to the
3075 -- corresponding initialization subprogram. Check that we
3076 -- have a single dimension.
3077
3078 if Present (Next_Index (Index)) then
3079 Error_Msg_N ("nested array aggregate expected", Assoc);
3080 return Failure;
3081 end if;
3082
3083 -- ??? Checks for dynamically tagged expressions below will
3084 -- be only applied to iterated_component_association after
3085 -- expansion; in particular, errors might not be reported when
3086 -- -gnatc switch is used.
3087
3088 elsif Nkind (Assoc) = N_Iterated_Component_Association then
3089 null; -- handled above, in a loop context
3090
3091 elsif not Resolve_Aggr_Expr
3092 (Expression (Assoc), Single_Elmt => Single_Choice)
3093 then
3094 return Failure;
3095
3096 -- Check incorrect use of dynamically tagged expression
3097
3098 -- We differentiate here two cases because the expression may
3099 -- not be decorated. For example, the analysis and resolution
3100 -- of the expression associated with the others choice will be
3101 -- done later with the full aggregate. In such case we
3102 -- duplicate the expression tree to analyze the copy and
3103 -- perform the required check.
3104
3105 elsif No (Etype (Expression (Assoc))) then
3106 declare
3107 Expr : constant Node_Id :=
3108 New_Copy_Tree (Expression (Assoc));
3109
3110 begin
3111 -- Preanalyze the expression, making sure it is properly
3112 -- attached to the tree before we do the analysis.
3113
3114 Set_Parent (Expr, Parent (Expression (Assoc)));
3115 Preanalyze (Expr);
3116
3117 -- If the expression is a literal, propagate this info
3118 -- to the expression in the association, to enable some
3119 -- optimizations downstream.
3120
3121 if Is_Entity_Name (Expr)
3122 and then Present (Entity (Expr))
3123 and then Ekind (Entity (Expr)) = E_Enumeration_Literal
3124 then
3125 Preanalyze_And_Resolve (Expression (Assoc), Ctyp);
3126 end if;
3127
3128 -- Skip tagged checking for mutably tagged CW equivalent
3129 -- types.
3130
3131 if Is_Tagged_Type (Etype (Expr))
3132 and then Is_Class_Wide_Equivalent_Type (Ctyp)
3133 then
3134 null;
3135
3136 -- Otherwise perform the dynamic tag check
3137
3138 elsif Is_Tagged_Type (Etype (Expr)) then
3139 Check_Dynamically_Tagged_Expression
3140 (Expr => Expr,
3141 Typ => Ctyp,
3142 Related_Nod => N);
3143 end if;
3144 end;
3145
3146 elsif Is_Tagged_Type (Etype (Expression (Assoc))) then
3147 Check_Dynamically_Tagged_Expression
3148 (Expr => Expression (Assoc),
3149 Typ => Ctyp,
3150 Related_Nod => N);
3151 end if;
3152
3153 -- Propagate the attribute Raises_CE when it was reported on a
3154 -- null aggregate. This will cause replacing the aggregate by a
3155 -- raise CE node; it is not done in other cases to avoid such
3156 -- replacement and report complementary warnings when the
3157 -- expression is resolved.
3158
3159 if Present (Expression (Assoc))
3160 and then Has_Null_Aggregate_Raising_Constraint_Error
3161 (Expression (Assoc))
3162 then
3163 Set_Raises_Constraint_Error (N);
3164 end if;
3165
3166 Next (Assoc);
3167 end loop;
3168
3169 -- If aggregate contains more than one choice then these must be
3170 -- static. Check for duplicate and missing values.
3171
3172 -- Note: there is duplicated code here wrt Check_Choice_Set in
3173 -- the body of Sem_Case, and it is possible we could just reuse
3174 -- that procedure. To be checked ???
3175
3176 if Nb_Discrete_Choices > 1 then
3177 Check_Choices : declare
3178 Choice : Node_Id;
3179 -- Location of choice for messages
3180
3181 Hi_Val : Uint;
3182 Lo_Val : Uint;
3183 -- High end of one range and Low end of the next. Should be
3184 -- contiguous if there is no hole in the list of values.
3185
3186 Lo_Dup : Uint;
3187 Hi_Dup : Uint;
3188 -- End points of duplicated range
3189
3190 Missing_Or_Duplicates : Boolean := False;
3191 -- Set True if missing or duplicate choices found
3192
3193 procedure Output_Bad_Choices (Lo, Hi : Uint; C : Node_Id);
3194 -- Output continuation message with a representation of the
3195 -- bounds (just Lo if Lo = Hi, else Lo .. Hi). C is the
3196 -- choice node where the message is to be posted.
3197
3198 ------------------------
3199 -- Output_Bad_Choices --
3200 ------------------------
3201
3202 procedure Output_Bad_Choices (Lo, Hi : Uint; C : Node_Id) is
3203 begin
3204 -- Enumeration type case
3205
3206 if Is_Enumeration_Type (Index_Typ) then
3207 Error_Msg_Name_1 :=
3208 Chars (Get_Enum_Lit_From_Pos (Index_Typ, Lo, Loc));
3209 Error_Msg_Name_2 :=
3210 Chars (Get_Enum_Lit_From_Pos (Index_Typ, Hi, Loc));
3211
3212 if Lo = Hi then
3213 Error_Msg_N ("\\ %!", C);
3214 else
3215 Error_Msg_N ("\\ % .. %!", C);
3216 end if;
3217
3218 -- Integer types case
3219
3220 else
3221 Error_Msg_Uint_1 := Lo;
3222 Error_Msg_Uint_2 := Hi;
3223
3224 if Lo = Hi then
3225 Error_Msg_N ("\\ ^!", C);
3226 else
3227 Error_Msg_N ("\\ ^ .. ^!", C);
3228 end if;
3229 end if;
3230 end Output_Bad_Choices;
3231
3232 -- Start of processing for Check_Choices
3233
3234 begin
3235 Sort_Case_Table (Table);
3236
3237 -- First we do a quick linear loop to find out if we have
3238 -- any duplicates or missing entries (usually we have a
3239 -- legal aggregate, so this will get us out quickly).
3240
3241 for J in 1 .. Nb_Discrete_Choices - 1 loop
3242 Hi_Val := Expr_Value (Table (J).Hi);
3243 Lo_Val := Expr_Value (Table (J + 1).Lo);
3244
3245 if Lo_Val <= Hi_Val
3246 or else (Lo_Val > Hi_Val + 1
3247 and then No (Others_N))
3248 then
3249 Missing_Or_Duplicates := True;
3250 exit;
3251 end if;
3252 end loop;
3253
3254 -- If we have missing or duplicate entries, first fill in
3255 -- the Highest entries to make life easier in the following
3256 -- loops to detect bad entries.
3257
3258 if Missing_Or_Duplicates then
3259 Table (1).Highest := Expr_Value (Table (1).Hi);
3260
3261 for J in 2 .. Nb_Discrete_Choices loop
3262 Table (J).Highest :=
3263 UI_Max
3264 (Table (J - 1).Highest, Expr_Value (Table (J).Hi));
3265 end loop;
3266
3267 -- Loop through table entries to find duplicate indexes
3268
3269 for J in 2 .. Nb_Discrete_Choices loop
3270 Lo_Val := Expr_Value (Table (J).Lo);
3271 Hi_Val := Expr_Value (Table (J).Hi);
3272
3273 -- Case where we have duplicates (the lower bound of
3274 -- this choice is less than or equal to the highest
3275 -- high bound found so far).
3276
3277 if Lo_Val <= Table (J - 1).Highest then
3278
3279 -- We move backwards looking for duplicates. We can
3280 -- abandon this loop as soon as we reach a choice
3281 -- highest value that is less than Lo_Val.
3282
3283 for K in reverse 1 .. J - 1 loop
3284 exit when Table (K).Highest < Lo_Val;
3285
3286 -- Here we may have duplicates between entries
3287 -- for K and J. Get range of duplicates.
3288
3289 Lo_Dup :=
3290 UI_Max (Lo_Val, Expr_Value (Table (K).Lo));
3291 Hi_Dup :=
3292 UI_Min (Hi_Val, Expr_Value (Table (K).Hi));
3293
3294 -- Nothing to do if duplicate range is null
3295
3296 if Lo_Dup > Hi_Dup then
3297 null;
3298
3299 -- Otherwise place proper message
3300
3301 else
3302 -- We place message on later choice, with a
3303 -- line reference to the earlier choice.
3304
3305 if Sloc (Table (J).Choice) <
3306 Sloc (Table (K).Choice)
3307 then
3308 Choice := Table (K).Choice;
3309 Error_Msg_Sloc := Sloc (Table (J).Choice);
3310 else
3311 Choice := Table (J).Choice;
3312 Error_Msg_Sloc := Sloc (Table (K).Choice);
3313 end if;
3314
3315 if Lo_Dup = Hi_Dup then
3316 Error_Msg_N
3317 ("index value in array aggregate "
3318 & "duplicates the one given#!", Choice);
3319 else
3320 Error_Msg_N
3321 ("index values in array aggregate "
3322 & "duplicate those given#!", Choice);
3323 end if;
3324
3325 Output_Bad_Choices (Lo_Dup, Hi_Dup, Choice);
3326 end if;
3327 end loop;
3328 end if;
3329 end loop;
3330
3331 -- Loop through entries in table to find missing indexes.
3332 -- Not needed if others, since missing impossible.
3333
3334 if No (Others_N) then
3335 for J in 2 .. Nb_Discrete_Choices loop
3336 Lo_Val := Expr_Value (Table (J).Lo);
3337 Hi_Val := Table (J - 1).Highest;
3338
3339 if Lo_Val > Hi_Val + 1 then
3340
3341 declare
3342 Error_Node : Node_Id;
3343
3344 begin
3345 -- If the choice is the bound of a range in
3346 -- a subtype indication, it is not in the
3347 -- source lists for the aggregate itself, so
3348 -- post the error on the aggregate. Otherwise
3349 -- post it on choice itself.
3350
3351 Choice := Table (J).Choice;
3352
3353 if Is_List_Member (Choice) then
3354 Error_Node := Choice;
3355 else
3356 Error_Node := N;
3357 end if;
3358
3359 if Hi_Val + 1 = Lo_Val - 1 then
3360 Error_Msg_N
3361 ("missing index value "
3362 & "in array aggregate!", Error_Node);
3363 else
3364 Error_Msg_N
3365 ("missing index values "
3366 & "in array aggregate!", Error_Node);
3367 end if;
3368
3369 Output_Bad_Choices
3370 (Hi_Val + 1, Lo_Val - 1, Error_Node);
3371 end;
3372 end if;
3373 end loop;
3374 end if;
3375
3376 -- If either missing or duplicate values, return failure
3377
3378 Set_Etype (N, Any_Composite);
3379 return Failure;
3380 end if;
3381 end Check_Choices;
3382 end if;
3383
3384 if Has_Iterator_Specifications then
3385 -- Bounds will be determined dynamically.
3386
3387 return Success;
3388 end if;
3389
3390 -- STEP 2 (B): Compute aggregate bounds and min/max choices values
3391
3392 if Nb_Discrete_Choices > 0 then
3393 Choices_Low := Table (1).Lo;
3394 Choices_High := Table (Nb_Discrete_Choices).Hi;
3395 end if;
3396
3397 -- If Others is present, then bounds of aggregate come from the
3398 -- index constraint (not the choices in the aggregate itself).
3399
3400 if Present (Others_N) then
3401 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
3402
3403 -- Abandon processing if either bound is already signalled as
3404 -- an error (prevents junk cascaded messages and blow ups).
3405
3406 if Nkind (Aggr_Low) = N_Error
3407 or else
3408 Nkind (Aggr_High) = N_Error
3409 then
3410 return False;
3411 end if;
3412
3413 -- No others clause present
3414
3415 else
3416 -- Special processing if others allowed and not present. In
3417 -- this case, the bounds of the aggregate come from the
3418 -- choices (RM 4.3.3 (27)).
3419
3420 if Others_Allowed then
3421 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
3422
3423 -- Abandon processing if either bound is already signalled
3424 -- as an error (stop junk cascaded messages and blow ups).
3425
3426 if Nkind (Aggr_Low) = N_Error
3427 or else
3428 Nkind (Aggr_High) = N_Error
3429 then
3430 return False;
3431 end if;
3432
3433 -- If there is an applicable index constraint and others is
3434 -- not present, then sliding is allowed and only a length
3435 -- check will be performed. However, additional warnings are
3436 -- useful if the index type is an enumeration type, as
3437 -- sliding is dubious in this case. We emit two kinds of
3438 -- warnings:
3439 --
3440 -- 1. If the length is wrong then there are missing
3441 -- components; we issue appropriate warnings about
3442 -- these missing components. They are only warnings,
3443 -- since the aggregate is fine, it's just the wrong
3444 -- length. We skip this check for standard character
3445 -- types (since there are no literals and it is too
3446 -- much trouble to concoct them), and also if any of
3447 -- the bounds have values that are not known at compile
3448 -- time.
3449 --
3450 -- 2. If the length is right but the bounds do not match,
3451 -- we issue a warning, as we consider sliding dubious
3452 -- when the index type is an enumeration type.
3453
3454 if Nkind (Index) = N_Range
3455 and then Is_Enumeration_Type (Etype (Index))
3456 and then not Is_Standard_Character_Type (Etype (Index))
3457 and then Compile_Time_Known_Value (Aggr_Low)
3458 and then Compile_Time_Known_Value (Aggr_High)
3459 and then Compile_Time_Known_Value (Choices_Low)
3460 and then Compile_Time_Known_Value (Choices_High)
3461 then
3462 -- If any of the expressions or range bounds in choices
3463 -- have semantic errors, then do not attempt further
3464 -- resolution, to prevent cascaded errors.
3465
3466 if Errors_Posted_On_Choices then
3467 return Failure;
3468 end if;
3469
3470 declare
3471 ALo : constant Node_Id := Expr_Value_E (Aggr_Low);
3472 AHi : constant Node_Id := Expr_Value_E (Aggr_High);
3473 CLo : constant Node_Id := Expr_Value_E (Choices_Low);
3474 CHi : constant Node_Id := Expr_Value_E (Choices_High);
3475
3476 Ent : Entity_Id;
3477
3478 begin
3479 -- Warning case 1, missing values at start/end. Only
3480 -- do the check if the number of entries is too small.
3481
3482 if (Enumeration_Pos (CHi) - Enumeration_Pos (CLo))
3483 <
3484 (Enumeration_Pos (AHi) - Enumeration_Pos (ALo))
3485 then
3486 Error_Msg_N
3487 ("missing index value(s) in array aggregate??",
3488 N);
3489
3490 -- Output missing value(s) at start
3491
3492 if Chars (ALo) /= Chars (CLo) then
3493 Ent := Prev (CLo);
3494
3495 if Chars (ALo) = Chars (Ent) then
3496 Error_Msg_Name_1 := Chars (ALo);
3497 Error_Msg_N ("\ %??", N);
3498 else
3499 Error_Msg_Name_1 := Chars (ALo);
3500 Error_Msg_Name_2 := Chars (Ent);
3501 Error_Msg_N ("\ % .. %??", N);
3502 end if;
3503 end if;
3504
3505 -- Output missing value(s) at end
3506
3507 if Chars (AHi) /= Chars (CHi) then
3508 Ent := Next (CHi);
3509
3510 if Chars (AHi) = Chars (Ent) then
3511 Error_Msg_Name_1 := Chars (Ent);
3512 Error_Msg_N ("\ %??", N);
3513 else
3514 Error_Msg_Name_1 := Chars (Ent);
3515 Error_Msg_Name_2 := Chars (AHi);
3516 Error_Msg_N ("\ % .. %??", N);
3517 end if;
3518 end if;
3519
3520 -- Warning case 2, dubious sliding. The First_Subtype
3521 -- test distinguishes between a constrained type where
3522 -- sliding is not allowed (so we will get a warning
3523 -- later that Constraint_Error will be raised), and
3524 -- the unconstrained case where sliding is permitted.
3525
3526 elsif (Enumeration_Pos (CHi) - Enumeration_Pos (CLo))
3527 =
3528 (Enumeration_Pos (AHi) - Enumeration_Pos (ALo))
3529 and then Chars (ALo) /= Chars (CLo)
3530 and then
3531 not Is_Constrained (First_Subtype (Etype (N)))
3532 then
3533 Error_Msg_N
3534 ("bounds of aggregate do not match target??", N);
3535 end if;
3536 end;
3537 end if;
3538 end if;
3539
3540 -- If no others, aggregate bounds come from aggregate
3541
3542 Aggr_Low := Choices_Low;
3543 Aggr_High := Choices_High;
3544 end if;
3545 end Step_2;
3546
3547 -- STEP 3: Process positional components
3548
3549 else
3550 -- STEP 3 (A): Process positional elements
3551
3552 Expr := First (Expressions (N));
3553 Nb_Elements := Uint_0;
3554 while Present (Expr) loop
3555 Nb_Elements := Nb_Elements + 1;
3556
3557 -- Ada 2005 (AI-231)
3558
3559 if Ada_Version >= Ada_2005 and then Known_Null (Expr) then
3560 Check_Can_Never_Be_Null (Etype (N), Expr);
3561 end if;
3562
3563 if not Resolve_Aggr_Expr (Expr) then
3564 return Failure;
3565 end if;
3566
3567 -- Check incorrect use of dynamically tagged expression
3568
3569 if not Iterated then
3570 if Is_Tagged_Type (Etype (Expr)) then
3571 Check_Dynamically_Tagged_Expression
3572 (Expr => Expr,
3573 Typ => Ctyp,
3574 Related_Nod => N);
3575 end if;
3576 end if;
3577
3578 Next (Expr);
3579 end loop;
3580
3581 if Present (Others_N) then
3582 Assoc := Last (Component_Associations (N));
3583
3584 -- Ada 2005 (AI-231)
3585
3586 if Ada_Version >= Ada_2005 and then Known_Null (Assoc) then
3587 Check_Can_Never_Be_Null (Etype (N), Expression (Assoc));
3588 end if;
3589
3590 -- Ada 2005 (AI-287): In case of default initialized component,
3591 -- we delay the resolution to the expansion phase.
3592
3593 if Box_Present (Assoc) then
3594
3595 -- Ada 2005 (AI-287): In case of default initialization of
3596 -- a component, the expander will generate calls to the
3597 -- corresponding initialization subprogram. Check that we
3598 -- have a single dimension.
3599
3600 if Present (Next_Index (Index)) then
3601 Error_Msg_N ("nested array aggregate expected", Assoc);
3602 return Failure;
3603 end if;
3604
3605 -- ??? Checks for dynamically tagged expressions below will
3606 -- be only applied to iterated_component_association after
3607 -- expansion; in particular, errors might not be reported when
3608 -- -gnatc switch is used.
3609
3610 elsif Nkind (Assoc) = N_Iterated_Component_Association then
3611 null; -- handled above, in a loop context
3612
3613 elsif not Resolve_Aggr_Expr (Expression (Assoc),
3614 Single_Elmt => False)
3615 then
3616 return Failure;
3617
3618 -- Check incorrect use of dynamically tagged expression. The
3619 -- expression of the others choice has not been resolved yet.
3620 -- In order to diagnose the semantic error we create a duplicate
3621 -- tree to analyze it and perform the check.
3622
3623 else
3624 declare
3625 Save_Analysis : constant Boolean := Full_Analysis;
3626 Expr : constant Node_Id :=
3627 New_Copy_Tree (Expression (Assoc));
3628
3629 begin
3630 Expander_Mode_Save_And_Set (False);
3631 Full_Analysis := False;
3632 Analyze (Expr);
3633 Full_Analysis := Save_Analysis;
3634 Expander_Mode_Restore;
3635
3636 if Is_Tagged_Type (Etype (Expr)) then
3637 Check_Dynamically_Tagged_Expression
3638 (Expr => Expr,
3639 Typ => Ctyp,
3640 Related_Nod => N);
3641 end if;
3642 end;
3643 end if;
3644 end if;
3645
3646 -- STEP 3 (B): Compute the aggregate bounds
3647
3648 if Present (Others_N) then
3649 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
3650
3651 else
3652 if Others_Allowed then
3653 Get_Index_Bounds (Index_Constr, Aggr_Low, Discard);
3654 else
3655 Aggr_Low := Index_Typ_Low;
3656 end if;
3657
3658 -- Report a warning when the index type of a null array aggregate
3659 -- is a modular type or an enumeration type, and we know that
3660 -- we will not be able to compute its high bound at runtime
3661 -- (AI22-0100-2).
3662
3663 if Nb_Elements = Uint_0
3664 and then Cannot_Compute_High_Bound (Index_Constr)
3665 then
3666 -- Use the low bound value for the high-bound value to avoid
3667 -- reporting spurious errors; this value will not be used at
3668 -- runtime because this aggregate will be replaced by a raise
3669 -- CE node.
3670
3671 Aggr_High := Aggr_Low;
3672
3673 Report_Null_Array_Constraint_Error (N, Index_Typ);
3674 Set_Raises_Constraint_Error (N);
3675
3676 elsif Nb_Elements = Uint_0 then
3677 Aggr_High := Subtract (Uint_1, To => Aggr_Low);
3678 Check_Bound (Index_Base_High, Aggr_High);
3679
3680 else
3681 Aggr_High := Add (Nb_Elements - 1, To => Aggr_Low);
3682 Check_Bound (Index_Base_High, Aggr_High);
3683 end if;
3684 end if;
3685 end if;
3686
3687 -- STEP 4: Perform static aggregate checks and save the bounds
3688
3689 -- Check (A)
3690
3691 Check_Bounds (Index_Typ_Low, Index_Typ_High, Aggr_Low, Aggr_High);
3692 Check_Bounds (Index_Base_Low, Index_Base_High, Aggr_Low, Aggr_High);
3693
3694 -- Check (B)
3695
3696 if Present (Others_N) and then Nb_Discrete_Choices > 0 then
3697 Check_Bounds (Aggr_Low, Aggr_High, Choices_Low, Choices_High);
3698 Check_Bounds (Index_Typ_Low, Index_Typ_High,
3699 Choices_Low, Choices_High);
3700 Check_Bounds (Index_Base_Low, Index_Base_High,
3701 Choices_Low, Choices_High);
3702
3703 -- Check (C)
3704
3705 elsif Present (Others_N) and then Nb_Elements > 0 then
3706 Check_Length (Aggr_Low, Aggr_High, Nb_Elements);
3707 Check_Length (Index_Typ_Low, Index_Typ_High, Nb_Elements);
3708 Check_Length (Index_Base_Low, Index_Base_High, Nb_Elements);
3709 end if;
3710
3711 if Raises_Constraint_Error (Aggr_Low)
3712 or else Raises_Constraint_Error (Aggr_High)
3713 then
3714 Set_Raises_Constraint_Error (N);
3715 end if;
3716
3717 Aggr_Low := Duplicate_Subexpr (Aggr_Low);
3718
3719 -- Do not duplicate Aggr_High if Aggr_High = Aggr_Low + Nb_Elements
3720 -- since the addition node returned by Add is not yet analyzed. Attach
3721 -- to tree and analyze first. Reset analyzed flag to ensure it will get
3722 -- analyzed when it is a literal bound whose type must be properly set.
3723
3724 if Present (Others_N) or else Nb_Discrete_Choices > 0 then
3725 Aggr_High := Duplicate_Subexpr (Aggr_High);
3726
3727 if Etype (Aggr_High) = Universal_Integer then
3728 Set_Analyzed (Aggr_High, False);
3729 end if;
3730 end if;
3731
3732 -- If the aggregate already has bounds attached to it, it means this is
3733 -- a positional aggregate created as an optimization by
3734 -- Exp_Aggr.Convert_To_Positional, so we don't want to change those
3735 -- bounds, unless they depend on discriminants. If they do, we have to
3736 -- perform analysis in the current context.
3737
3738 if Present (Aggregate_Bounds (N))
3739 and then No (Others_N)
3740 and then not Depends_On_Discriminant (Aggregate_Bounds (N))
3741 and then not Comes_From_Source (N)
3742 then
3743 Aggr_Low := Low_Bound (Aggregate_Bounds (N));
3744 Aggr_High := High_Bound (Aggregate_Bounds (N));
3745 end if;
3746
3747 Set_Aggregate_Bounds
3748 (N, Make_Range (Loc, Low_Bound => Aggr_Low, High_Bound => Aggr_High));
3749
3750 -- The bounds may contain expressions that must be inserted upwards.
3751 -- Attach them fully to the tree. After analysis, remove side effects
3752 -- from upper bound, if still needed.
3753
3754 Set_Parent (Aggregate_Bounds (N), N);
3755 Analyze_And_Resolve (Aggregate_Bounds (N), Index_Typ);
3756 Check_Unset_Reference (Aggregate_Bounds (N));
3757
3758 if No (Others_N) and then Nb_Discrete_Choices = 0 then
3759 Set_High_Bound
3760 (Aggregate_Bounds (N),
3761 Duplicate_Subexpr (High_Bound (Aggregate_Bounds (N))));
3762 end if;
3763
3764 -- Check the dimensions of each component in the array aggregate
3765
3766 Analyze_Dimension_Array_Aggregate (N, Ctyp);
3767
3768 if Serious_Errors_Detected /= Saved_SED then
3769 return Failure;
3770 end if;
3771
3772 return Success;
3773 end Resolve_Array_Aggregate;
3774
3775 ---------------------------------
3776 -- Resolve_Container_Aggregate --
3777 ---------------------------------
3778
3779 procedure Resolve_Container_Aggregate (N : Node_Id; Typ : Entity_Id) is
3780 procedure Resolve_Iterated_Association
3781 (Comp : Node_Id;
3782 Key_Type : Entity_Id;
3783 Elmt_Type : Entity_Id);
3784 -- Resolve choices and expression in an iterated component association
3785 -- or an iterated element association, which has a key_expression.
3786 -- This is similar but not identical to the handling of this construct
3787 -- in an array aggregate.
3788 -- For a named container, the type of each choice must be compatible
3789 -- with the key type. For a positional container, the choice must be
3790 -- a subtype indication or an iterator specification that determines
3791 -- an element type.
3792
3793 Asp : constant Node_Id := Find_Value_Of_Aspect (Typ, Aspect_Aggregate);
3794
3795 Empty_Subp : Node_Id := Empty;
3796 Add_Named_Subp : Node_Id := Empty;
3797 Add_Unnamed_Subp : Node_Id := Empty;
3798 New_Indexed_Subp : Node_Id := Empty;
3799 Assign_Indexed_Subp : Node_Id := Empty;
3800
3801 ----------------------------------
3802 -- Resolve_Iterated_Association --
3803 ----------------------------------
3804
3805 procedure Resolve_Iterated_Association
3806 (Comp : Node_Id;
3807 Key_Type : Entity_Id;
3808 Elmt_Type : Entity_Id)
3809 is
3810 Loc : constant Source_Ptr := Sloc (N);
3811
3812 Choice : Node_Id;
3813 Copy : Node_Id;
3814 Ent : Entity_Id;
3815 Expr : Node_Id;
3816 Key_Expr : Node_Id := Empty;
3817 Id : Entity_Id;
3818 Id_Name : Name_Id;
3819 Typ : Entity_Id := Empty;
3820 Loop_Param_Id : Entity_Id := Empty;
3821
3822 begin
3823 Error_Msg_Ada_2022_Feature ("iterated component", Loc);
3824
3825 -- If this is an Iterated_Element_Association then either a
3826 -- an Iterator_Specification or a Loop_Parameter specification
3827 -- is present.
3828
3829 if Nkind (Comp) = N_Iterated_Element_Association then
3830 -- Create a temporary scope to avoid some modifications from
3831 -- escaping the Preanalyze call below. The original tree will
3832 -- be reanalyzed later.
3833
3834 Ent := New_Internal_Entity
3835 (E_Loop, Current_Scope, Sloc (Comp), 'L');
3836 Set_Etype (Ent, Standard_Void_Type);
3837 Set_Parent (Ent, Parent (Comp));
3838 Push_Scope (Ent);
3839
3840 if Present (Loop_Parameter_Specification (Comp)) then
3841 Copy := Copy_Separate_Tree (Comp);
3842 Set_Parent (Copy, Parent (Comp));
3843
3844 Preanalyze (Loop_Parameter_Specification (Copy));
3845
3846 if Present (Iterator_Specification (Copy)) then
3847 Loop_Param_Id :=
3848 Defining_Identifier (Iterator_Specification (Copy));
3849 else
3850 Loop_Param_Id :=
3851 Defining_Identifier (Loop_Parameter_Specification (Copy));
3852 end if;
3853
3854 Id_Name := Chars (Loop_Param_Id);
3855
3856 else
3857 Copy := Copy_Separate_Tree (Iterator_Specification (Comp));
3858
3859 Preanalyze (Copy);
3860
3861 Loop_Param_Id := Defining_Identifier (Copy);
3862
3863 Id_Name := Chars (Loop_Param_Id);
3864 end if;
3865
3866 -- Key expression must have the type of the key. We preanalyze
3867 -- a copy of the original expression, because it will be
3868 -- reanalyzed and copied as needed during expansion of the
3869 -- corresponding loop.
3870
3871 Key_Expr := Key_Expression (Comp);
3872 if Present (Key_Expr) then
3873 if No (Add_Named_Subp) then
3874 Error_Msg_N
3875 ("iterated_element_association with key_expression only "
3876 & "allowed for container type with Add_Named operation "
3877 & "(RM22 4.3.5(24))",
3878 Comp);
3879 else
3880 Preanalyze_And_Resolve
3881 (Copy_Separate_Tree (Key_Expr), Key_Type);
3882 end if;
3883 end if;
3884
3885 End_Scope;
3886
3887 Typ := Etype (Loop_Param_Id);
3888
3889 elsif Present (Iterator_Specification (Comp)) then
3890 -- Create a temporary scope to avoid some modifications from
3891 -- escaping the Preanalyze call below. The original tree will
3892 -- be reanalyzed later.
3893
3894 Ent := New_Internal_Entity
3895 (E_Loop, Current_Scope, Sloc (Comp), 'L');
3896 Set_Etype (Ent, Standard_Void_Type);
3897 Set_Parent (Ent, Parent (Comp));
3898 Push_Scope (Ent);
3899
3900 Copy := Copy_Separate_Tree (Iterator_Specification (Comp));
3901
3902 Loop_Param_Id :=
3903 Defining_Identifier (Iterator_Specification (Comp));
3904
3905 Id_Name := Chars (Loop_Param_Id);
3906
3907 Preanalyze (Copy);
3908
3909 End_Scope;
3910
3911 Typ := Etype (Defining_Identifier (Copy));
3912
3913 else
3914 Choice := First (Discrete_Choices (Comp));
3915
3916 -- A copy of Choice is made before it's analyzed, to preserve
3917 -- prefixed calls in their original form, because otherwise the
3918 -- analysis of Choice can transform such calls to normal form,
3919 -- and the later analysis of an iterator_specification created
3920 -- below in the case of a function-call choice may trigger an
3921 -- error on the call (in the case where the function is not
3922 -- directly visible).
3923
3924 Copy := Copy_Separate_Tree (Choice);
3925
3926 -- This is an N_Component_Association with a Defining_Identifier
3927 -- and Discrete_Choice_List, but the latter can only have a single
3928 -- choice, as it's a stand-in for a Loop_Parameter_Specification
3929 -- (or possibly even an Iterator_Specification, see below).
3930
3931 pragma Assert (No (Next (Choice)));
3932
3933 Analyze (Choice);
3934
3935 -- Choice can be a subtype name, a range, or an expression
3936
3937 if Is_Entity_Name (Choice)
3938 and then Is_Type (Entity (Choice))
3939 and then Present (Key_Type)
3940 and then Base_Type (Entity (Choice)) = Base_Type (Key_Type)
3941 then
3942 Typ := Entity (Choice);
3943
3944 elsif Is_Object_Reference (Choice) then
3945 declare
3946 I_Spec : constant Node_Id :=
3947 Make_Iterator_Specification (Sloc (N),
3948 Defining_Identifier =>
3949 Relocate_Node (Defining_Identifier (Comp)),
3950 Name => Copy,
3951 Reverse_Present => Reverse_Present (Comp),
3952 Iterator_Filter => Empty,
3953 Subtype_Indication => Empty);
3954
3955 begin
3956 -- Recurse to expand association as iterator_spec
3957
3958 Set_Iterator_Specification (Comp, I_Spec);
3959 Set_Defining_Identifier (Comp, Empty);
3960 Resolve_Iterated_Association (Comp, Key_Type, Elmt_Type);
3961 return;
3962 end;
3963
3964 elsif Present (Key_Type) then
3965 Analyze_And_Resolve (Choice, Key_Type);
3966 Typ := Key_Type;
3967
3968 else
3969 Typ := Etype (Choice); -- assume unique for now
3970 end if;
3971
3972 Loop_Param_Id := Defining_Identifier (Comp);
3973
3974 Id_Name := Chars (Loop_Param_Id);
3975 end if;
3976
3977 -- Create a scope in which to introduce an index, which is usually
3978 -- visible in the expression for the component, and needed for its
3979 -- analysis.
3980
3981 Id := Make_Defining_Identifier (Sloc (Comp), Id_Name);
3982 Ent := New_Internal_Entity (E_Loop, Current_Scope, Sloc (Comp), 'L');
3983 Set_Etype (Ent, Standard_Void_Type);
3984 Set_Parent (Ent, Parent (Comp));
3985 Push_Scope (Ent);
3986
3987 -- Insert and decorate the loop variable in the current scope.
3988 -- The expression has to be analyzed once the loop variable is
3989 -- directly visible. Mark the variable as referenced to prevent
3990 -- spurious warnings, given that subsequent uses of its name in the
3991 -- expression will reference the internal (synonym) loop variable.
3992
3993 Enter_Name (Id);
3994
3995 pragma Assert (Present (Typ));
3996 Set_Etype (Id, Typ);
3997
3998 Mutate_Ekind (Id, E_Variable);
3999 Set_Is_Not_Self_Hidden (Id);
4000 Set_Scope (Id, Ent);
4001 Set_Referenced (Id);
4002
4003 -- Check for violation of 4.3.5(27/5)
4004
4005 if No (Key_Expr)
4006 and then Present (Key_Type)
4007 and then
4008 (Is_Indexed_Aggregate (N, Add_Unnamed_Subp, New_Indexed_Subp)
4009 or else Present (Add_Named_Subp))
4010 and then Base_Type (Key_Type) /= Base_Type (Typ)
4011 then
4012 Error_Msg_Node_2 := Key_Type;
4013 Error_Msg_NE
4014 ("loop parameter type & must be same as key type & " &
4015 "(RM22 4.3.5(27))", Loop_Param_Id, Typ);
4016 end if;
4017
4018 -- Analyze a copy of the expression, to verify legality. We use
4019 -- a copy because the expression will be analyzed anew when the
4020 -- enclosing aggregate is expanded, and the construct is rewritten
4021 -- as a loop with a new index variable.
4022
4023 Expr := Copy_Separate_Tree (Expression (Comp));
4024 Set_Parent (Expr, Comp);
4025 Preanalyze_And_Resolve (Expr, Elmt_Type);
4026 End_Scope;
4027 end Resolve_Iterated_Association;
4028
4029 -- Start of processing for Resolve_Container_Aggregate
4030
4031 begin
4032 pragma Assert (Nkind (Asp) = N_Aggregate);
4033
4034 Set_Etype (N, Typ);
4035 Parse_Aspect_Aggregate (Asp,
4036 Empty_Subp, Add_Named_Subp, Add_Unnamed_Subp,
4037 New_Indexed_Subp, Assign_Indexed_Subp);
4038
4039 if Present (First (Expressions (N)))
4040 and then Present (First (Component_Associations (N)))
4041 then
4042 if Debug_Flag_Underscore_DD then
4043 Record_Mixed_Container_Aggregate_Error
4044 (Aggr => N,
4045 Pos_Elem => First (Expressions (N)),
4046 Named_Elem => First (Component_Associations (N)));
4047 else
4048 Error_Msg_N
4049 ("container aggregate cannot be both positional and named", N);
4050 end if;
4051 return;
4052 end if;
4053
4054 if Present (Add_Unnamed_Subp)
4055 and then No (New_Indexed_Subp)
4056 and then Present (Entity (Add_Unnamed_Subp))
4057 and then Entity (Add_Unnamed_Subp) /= Any_Id
4058 then
4059 declare
4060 Elmt_Type : constant Entity_Id :=
4061 Etype (Next_Formal
4062 (First_Formal (Entity (Add_Unnamed_Subp))));
4063 Comp : Node_Id;
4064
4065 begin
4066 if Present (Expressions (N)) then
4067 -- positional aggregate
4068
4069 Comp := First (Expressions (N));
4070 while Present (Comp) loop
4071 Analyze_And_Resolve (Comp, Elmt_Type);
4072 Next (Comp);
4073 end loop;
4074 end if;
4075
4076 -- Empty aggregate, to be replaced by Empty during
4077 -- expansion, or iterated component association.
4078
4079 if Present (Component_Associations (N)) then
4080 declare
4081 Comp : Node_Id := First (Component_Associations (N));
4082 begin
4083 while Present (Comp) loop
4084 if Nkind (Comp) in N_Iterated_Component_Association
4085 | N_Iterated_Element_Association
4086 then
4087 Resolve_Iterated_Association (Comp, Empty, Elmt_Type);
4088 else
4089 Error_Msg_N ("illegal component association "
4090 & "for unnamed container aggregate", Comp);
4091 return;
4092 end if;
4093
4094 Next (Comp);
4095 end loop;
4096 end;
4097 end if;
4098 end;
4099
4100 elsif Present (Add_Named_Subp)
4101 and then Present (Entity (Add_Named_Subp))
4102 and then Entity (Add_Named_Subp) /= Any_Id
4103 then
4104 declare
4105 -- Retrieves types of container, key, and element from the
4106 -- specified insertion procedure.
4107
4108 Container : constant Entity_Id :=
4109 First_Formal (Entity (Add_Named_Subp));
4110 Key_Type : constant Entity_Id := Etype (Next_Formal (Container));
4111 Elmt_Type : constant Entity_Id :=
4112 Etype (Next_Formal (Next_Formal (Container)));
4113
4114 Comp_Assocs : constant List_Id := Component_Associations (N);
4115 Comp : Node_Id;
4116 Choice : Node_Id;
4117
4118 begin
4119 -- In the Add_Named case, the aggregate must consist of named
4120 -- associations (Add_Unnnamed is not allowed), so we issue an
4121 -- error if there are positional associations.
4122
4123 if No (Comp_Assocs)
4124 and then Present (Expressions (N))
4125 then
4126 Error_Msg_N ("container aggregate must be "
4127 & "named, not positional", N);
4128 return;
4129 end if;
4130
4131 Comp := First (Comp_Assocs);
4132 while Present (Comp) loop
4133 if Nkind (Comp) = N_Component_Association then
4134 Choice := First (Choices (Comp));
4135
4136 while Present (Choice) loop
4137 Analyze_And_Resolve (Choice, Key_Type);
4138 Next (Choice);
4139 end loop;
4140
4141 Analyze_And_Resolve (Expression (Comp), Elmt_Type);
4142
4143 elsif Nkind (Comp) in N_Iterated_Component_Association
4144 | N_Iterated_Element_Association
4145 then
4146 Resolve_Iterated_Association (Comp, Key_Type, Elmt_Type);
4147 end if;
4148
4149 Next (Comp);
4150 end loop;
4151 end;
4152
4153 elsif Present (Assign_Indexed_Subp)
4154 and then Present (Entity (Assign_Indexed_Subp))
4155 and then Entity (Assign_Indexed_Subp) /= Any_Id
4156 then
4157 -- Indexed Aggregate. Positional or indexed component
4158 -- can be present, but not both. Choices must be static
4159 -- values or ranges with static bounds.
4160
4161 declare
4162 Container : constant Entity_Id :=
4163 First_Formal (Entity (Assign_Indexed_Subp));
4164 Index_Type : constant Entity_Id := Etype (Next_Formal (Container));
4165 Comp_Type : constant Entity_Id :=
4166 Etype (Next_Formal (Next_Formal (Container)));
4167 Comp : Node_Id;
4168 Choice : Node_Id;
4169 Num_Choices : Nat := 0;
4170
4171 Hi_Val : Uint;
4172 Lo_Val : Uint;
4173 begin
4174 if Present (Expressions (N)) then
4175 Comp := First (Expressions (N));
4176 while Present (Comp) loop
4177 Analyze_And_Resolve (Comp, Comp_Type);
4178 Next (Comp);
4179 end loop;
4180 end if;
4181
4182 if Present (Component_Associations (N))
4183 and then not Is_Empty_List (Component_Associations (N))
4184 then
4185 Comp := First (Component_Associations (N));
4186
4187 while Present (Comp) loop
4188 if Nkind (Comp) = N_Component_Association then
4189 Choice := First (Choices (Comp));
4190
4191 while Present (Choice) loop
4192 Analyze_And_Resolve (Choice, Index_Type);
4193 Num_Choices := Num_Choices + 1;
4194 Next (Choice);
4195 end loop;
4196
4197 if not Box_Present (Comp) then
4198 Analyze_And_Resolve (Expression (Comp), Comp_Type);
4199 end if;
4200
4201 elsif Nkind (Comp) in N_Iterated_Component_Association
4202 | N_Iterated_Element_Association
4203 then
4204 Resolve_Iterated_Association
4205 (Comp, Index_Type, Comp_Type);
4206
4207 -- Check the legality rule of RM22 4.3.5(28/5). Note that
4208 -- Is_Indexed_Aggregate can change its status (to False)
4209 -- as a result of calling Resolve_Iterated_Association,
4210 -- due to possible expansion of iterator_specifications
4211 -- there.
4212
4213 if Is_Indexed_Aggregate
4214 (N, Add_Unnamed_Subp, New_Indexed_Subp)
4215 then
4216 if Nkind (Comp) = N_Iterated_Element_Association then
4217 if Present (Loop_Parameter_Specification (Comp))
4218 then
4219 if Present (Iterator_Filter
4220 (Loop_Parameter_Specification (Comp)))
4221 then
4222 Error_Msg_N
4223 ("iterator filter not allowed " &
4224 "in indexed aggregate (RM22 4.3.5(28))",
4225 Iterator_Filter
4226 (Loop_Parameter_Specification (Comp)));
4227 return;
4228
4229 elsif Present (Key_Expression (Comp)) then
4230 Error_Msg_N
4231 ("key expression not allowed " &
4232 "in indexed aggregate (RM22 4.3.5(28))",
4233 Key_Expression (Comp));
4234 return;
4235 end if;
4236
4237 elsif Present (Iterator_Specification (Comp)) then
4238 Error_Msg_N
4239 ("iterator specification not allowed " &
4240 "in indexed aggregate (RM22 4.3.5(28))",
4241 Iterator_Specification (Comp));
4242 return;
4243 end if;
4244
4245 elsif Nkind (Comp) = N_Iterated_Component_Association
4246 and then Present (Iterator_Specification (Comp))
4247 then
4248 Error_Msg_N
4249 ("iterator specification not allowed " &
4250 "in indexed aggregate (RM22 4.3.5(28))",
4251 Iterator_Specification (Comp));
4252 return;
4253 end if;
4254 end if;
4255
4256 Num_Choices := Num_Choices + 1;
4257 end if;
4258
4259 Next (Comp);
4260 end loop;
4261
4262 -- The component associations in an indexed aggregate
4263 -- must denote a contiguous set of static values. We
4264 -- build a table of values/ranges and sort it, as is done
4265 -- elsewhere for case statements and array aggregates.
4266 -- If the aggregate has a single iterated association it
4267 -- is allowed to be nonstatic and there is nothing to check.
4268
4269 if Num_Choices > 1 then
4270 declare
4271 Table : Case_Table_Type (1 .. Num_Choices);
4272 No_Choice : Pos := 1;
4273 Lo, Hi : Node_Id;
4274
4275 -- Traverse aggregate to determine size of needed table.
4276 -- Verify that bounds are static and that loops have no
4277 -- filters or key expressions.
4278
4279 begin
4280 Comp := First (Component_Associations (N));
4281 while Present (Comp) loop
4282
4283 -- If Nkind is N_Iterated_Component_Association,
4284 -- this corresponds to an iterator_specification
4285 -- with a loop_parameter_specification, and we
4286 -- have to pick up Discrete_Choices. In this case
4287 -- there will be just one "choice", which will
4288 -- typically be a range.
4289
4290 if Nkind (Comp) = N_Iterated_Component_Association
4291 then
4292 Choice := First (Discrete_Choices (Comp));
4293
4294 -- Case where there's a list of choices
4295
4296 else
4297 Choice := First (Choices (Comp));
4298 end if;
4299
4300 while Present (Choice) loop
4301 Get_Index_Bounds (Choice, Lo, Hi);
4302 Table (No_Choice).Choice := Choice;
4303 Table (No_Choice).Lo := Lo;
4304 Table (No_Choice).Hi := Hi;
4305
4306 -- Verify staticness of value or range
4307
4308 if not Is_Static_Expression (Lo)
4309 or else not Is_Static_Expression (Hi)
4310 then
4311 Error_Msg_N
4312 ("nonstatic expression for index " &
4313 "for indexed aggregate", Choice);
4314 return;
4315 end if;
4316
4317 No_Choice := No_Choice + 1;
4318 Next (Choice);
4319 end loop;
4320
4321 Next (Comp);
4322 end loop;
4323
4324 Sort_Case_Table (Table);
4325
4326 for J in 1 .. Num_Choices - 1 loop
4327 Hi_Val := Expr_Value (Table (J).Hi);
4328 Lo_Val := Expr_Value (Table (J + 1).Lo);
4329
4330 if Lo_Val = Hi_Val then
4331 Error_Msg_N
4332 ("duplicate index in indexed aggregate",
4333 Table (J + 1).Choice);
4334 exit;
4335
4336 elsif Lo_Val < Hi_Val then
4337 Error_Msg_N
4338 ("overlapping indices in indexed aggregate",
4339 Table (J + 1).Choice);
4340 exit;
4341
4342 elsif Lo_Val > Hi_Val + 1 then
4343 Error_Msg_N
4344 ("missing index values", Table (J + 1).Choice);
4345 exit;
4346 end if;
4347 end loop;
4348 end;
4349 end if;
4350 end if;
4351 end;
4352 end if;
4353 end Resolve_Container_Aggregate;
4354
4355 -----------------------------
4356 -- Resolve_Delta_Aggregate --
4357 -----------------------------
4358
4359 procedure Resolve_Delta_Aggregate (N : Node_Id; Typ : Entity_Id) is
4360 Base : constant Node_Id := Expression (N);
4361
4362 begin
4363 Error_Msg_Ada_2022_Feature ("delta aggregate", Sloc (N));
4364
4365 if not Is_Composite_Type (Typ) then
4366 Error_Msg_N ("not a composite type", N);
4367 end if;
4368
4369 Analyze_And_Resolve (Base, Typ);
4370
4371 if Is_Array_Type (Typ) then
4372 -- For an array_delta_aggregate, the base_expression and each
4373 -- expression in every array_component_association shall be of a
4374 -- nonlimited type; RM 4.3.4(13/5). However, to prevent repeated
4375 -- errors we only check the base expression and not array component
4376 -- associations.
4377
4378 if Is_Limited_Type (Etype (Base)) then
4379 Error_Msg_N
4380 ("array delta aggregate shall be of a nonlimited type", Base);
4381 Explain_Limited_Type (Etype (Base), Base);
4382 end if;
4383
4384 Resolve_Delta_Array_Aggregate (N, Typ);
4385 else
4386
4387 -- Delta aggregates for record types must use parentheses,
4388 -- not square brackets.
4389
4390 if Is_Homogeneous_Aggregate (N) then
4391 Error_Msg_N
4392 ("delta aggregates for record types must use (), not '[']", N);
4393 end if;
4394
4395 -- The base_expression of a record_delta_aggregate can be of a
4396 -- limited type only if it is newly constructed; RM 7.5(2.1/5).
4397
4398 Check_Expr_OK_In_Limited_Aggregate (Base);
4399
4400 Resolve_Delta_Record_Aggregate (N, Typ);
4401 end if;
4402
4403 declare
4404 Assoc : Node_Id;
4405 Choice : Node_Id;
4406
4407 procedure Check_For_Bad_Dd_Component_Choice (Choice : Node_Id);
4408 -- Enforce the GNAT RM rule that a deep delta aggregate choice
4409 -- cannot name a discriminant-dependent component if the
4410 -- immediately enclosing object's subtype is unconstrained and the
4411 -- prefix of the component includes at least one array indexing.
4412 -- [Note: The motivation for this rule is unclear. The GNAT RM
4413 -- gives a rationale for this particular rule, but it still
4414 -- seems dubious.]
4415
4416 ---------------------------------------
4417 -- Check_For_Bad_Dd_Component_Choice --
4418 ---------------------------------------
4419
4420 procedure Check_For_Bad_Dd_Component_Choice (Choice : Node_Id) is
4421 Pref : Node_Id := Choice;
4422 Dd_Comp_Name : Node_Id := Empty;
4423 begin
4424 loop
4425 case Nkind (Pref) is
4426 when N_Selected_Component =>
4427 declare
4428 Comp : constant Entity_Id
4429 := Entity (Selector_Name (Pref));
4430
4431 Enclosing_Type : Entity_Id := Etype (Prefix (Pref));
4432 begin
4433 if Is_Declared_Within_Variant (Comp)
4434 or else Has_Discriminant_Dependent_Constraint (Comp)
4435 then
4436 if not Has_Discriminants (Enclosing_Type) then
4437 -- a deep delta array aggregate choice like
4438 -- (Index_Value).Record_Component => ...
4439 Enclosing_Type := Component_Type (Etype (N));
4440 end if;
4441
4442 if not Is_Constrained (Enclosing_Type) then
4443 Dd_Comp_Name := Selector_Name (Pref);
4444 end if;
4445 end if;
4446 end;
4447
4448 when N_Indexed_Component =>
4449 exit when Present (Dd_Comp_Name);
4450
4451 when N_Identifier =>
4452 return;
4453
4454 when others =>
4455 exit;
4456 end case;
4457 Pref := Prefix (Pref);
4458 end loop;
4459
4460 if Present (Dd_Comp_Name) then
4461 -- It would be difficult to explain the whole rule briefly,
4462 -- so we just say "illegal".
4463
4464 Error_Msg_N
4465 ("illegal discriminant-dependent component &" &
4466 " in deep delta aggregate choice", Dd_Comp_Name);
4467 end if;
4468 end Check_For_Bad_Dd_Component_Choice;
4469
4470 begin
4471 Assoc := First (Component_Associations (N));
4472 while Present (Assoc) loop
4473 Choice := First (Choice_List (Assoc));
4474 while Present (Choice) loop
4475 Check_For_Bad_Dd_Component_Choice (Choice);
4476 Next (Choice);
4477 end loop;
4478 Next (Assoc);
4479 end loop;
4480 end;
4481
4482 Set_Etype (N, Typ);
4483 end Resolve_Delta_Aggregate;
4484
4485 -----------------------------------
4486 -- Resolve_Delta_Array_Aggregate --
4487 -----------------------------------
4488
4489 procedure Resolve_Delta_Array_Aggregate (N : Node_Id; Typ : Entity_Id) is
4490 Deltas : constant List_Id := Component_Associations (N);
4491 Index_Type : constant Entity_Id := Etype (First_Index (Typ));
4492
4493 Assoc : Node_Id;
4494 Choice : Node_Id;
4495 Expr : Node_Id;
4496
4497 Deep_Choice_Seen : Boolean := False;
4498
4499 begin
4500 Assoc := First (Deltas);
4501 while Present (Assoc) loop
4502 if Nkind (Assoc) = N_Iterated_Component_Association then
4503 Choice := First (Choice_List (Assoc));
4504 while Present (Choice) loop
4505 if Nkind (Choice) = N_Others_Choice then
4506 Error_Msg_N
4507 ("OTHERS not allowed in delta aggregate", Choice);
4508
4509 elsif Nkind (Choice) = N_Subtype_Indication then
4510 Resolve_Discrete_Subtype_Indication
4511 (Choice, Base_Type (Index_Type));
4512
4513 else
4514 Analyze_And_Resolve (Choice, Index_Type);
4515 end if;
4516
4517 Next (Choice);
4518 end loop;
4519
4520 declare
4521 Id : constant Entity_Id := Defining_Identifier (Assoc);
4522 Ent : constant Entity_Id :=
4523 New_Internal_Entity
4524 (E_Loop, Current_Scope, Sloc (Assoc), 'L');
4525
4526 begin
4527 Set_Etype (Ent, Standard_Void_Type);
4528 Set_Parent (Ent, Assoc);
4529 Push_Scope (Ent);
4530
4531 if No (Scope (Id)) then
4532 Set_Etype (Id, Index_Type);
4533 Mutate_Ekind (Id, E_Variable);
4534 Set_Is_Not_Self_Hidden (Id);
4535 Set_Scope (Id, Ent);
4536 end if;
4537
4538 Enter_Name (Id);
4539
4540 -- Analyze a copy of the expression, to verify legality. We use
4541 -- a copy because the expression will be analyzed anew when the
4542 -- enclosing aggregate is expanded, and the construct is
4543 -- rewritten as a loop with a new index variable.
4544
4545 Expr := Copy_Separate_Tree (Expression (Assoc));
4546 Set_Parent (Expr, Assoc);
4547 Preanalyze_And_Resolve (Expr, Component_Type (Typ));
4548 End_Scope;
4549 end;
4550
4551 else
4552 Choice := First (Choice_List (Assoc));
4553 while Present (Choice) loop
4554 if Is_Deep_Choice (Choice, Typ) then
4555 pragma Assert (All_Extensions_Allowed);
4556 Deep_Choice_Seen := True;
4557
4558 -- a deep delta aggregate
4559 Resolve_Deep_Delta_Assoc (Assoc, Typ);
4560 else
4561 Analyze (Choice);
4562
4563 if Nkind (Choice) = N_Others_Choice then
4564 Error_Msg_N
4565 ("OTHERS not allowed in delta aggregate", Choice);
4566
4567 elsif Is_Entity_Name (Choice)
4568 and then Is_Type (Entity (Choice))
4569 then
4570 -- Choice covers a range of values
4571
4572 if Base_Type (Entity (Choice)) /=
4573 Base_Type (Index_Type)
4574 then
4575 Error_Msg_NE
4576 ("choice does not match index type of &",
4577 Choice, Typ);
4578 end if;
4579
4580 elsif Nkind (Choice) = N_Subtype_Indication then
4581 Resolve_Discrete_Subtype_Indication
4582 (Choice, Base_Type (Index_Type));
4583
4584 else
4585 Resolve (Choice, Index_Type);
4586 end if;
4587 end if;
4588
4589 Next (Choice);
4590 end loop;
4591
4592 -- For an array_delta_aggregate, the array_component_association
4593 -- shall not use the box symbol <>; RM 4.3.4(11/5).
4594
4595 pragma Assert
4596 (Box_Present (Assoc) xor Present (Expression (Assoc)));
4597
4598 if Box_Present (Assoc) then
4599 Error_Msg_N
4600 ("'<'> in array delta aggregate is not allowed", Assoc);
4601 elsif not Deep_Choice_Seen then
4602 Analyze_And_Resolve (Expression (Assoc), Component_Type (Typ));
4603 end if;
4604 end if;
4605
4606 Next (Assoc);
4607 end loop;
4608 end Resolve_Delta_Array_Aggregate;
4609
4610 ------------------------------------
4611 -- Resolve_Delta_Record_Aggregate --
4612 ------------------------------------
4613
4614 procedure Resolve_Delta_Record_Aggregate (N : Node_Id; Typ : Entity_Id) is
4615
4616 -- Variables used to verify that discriminant-dependent components
4617 -- appear in the same variant.
4618
4619 Comp_Ref : Entity_Id := Empty; -- init to avoid warning
4620 Variant : Node_Id;
4621
4622 procedure Check_Variant (Id : Node_Id);
4623 -- If a given component of the delta aggregate appears in a variant
4624 -- part, verify that it is within the same variant as that of previous
4625 -- specified variant components of the delta.
4626
4627 function Get_Component_Type
4628 (Selector : Node_Id; Enclosing_Type : Entity_Id) return Entity_Id;
4629 -- Locate component with a given name and return its type.
4630 -- If none found then report error and return Empty.
4631
4632 function Nested_In (V1 : Node_Id; V2 : Node_Id) return Boolean;
4633 -- Determine whether variant V1 is within variant V2
4634
4635 function Variant_Depth (N : Node_Id) return Natural;
4636 -- Determine the distance of a variant to the enclosing type declaration
4637
4638 --------------------
4639 -- Check_Variant --
4640 --------------------
4641
4642 procedure Check_Variant (Id : Node_Id) is
4643 Comp : Entity_Id;
4644 Comp_Variant : Node_Id;
4645
4646 begin
4647 if not Has_Discriminants (Typ) then
4648 return;
4649 end if;
4650
4651 Comp := First_Entity (Typ);
4652 while Present (Comp) loop
4653 exit when Chars (Comp) = Chars (Id);
4654 Next_Component (Comp);
4655 end loop;
4656
4657 -- Find the variant, if any, whose component list includes the
4658 -- component declaration.
4659
4660 Comp_Variant := Parent (Parent (List_Containing (Parent (Comp))));
4661 if Nkind (Comp_Variant) = N_Variant then
4662 if No (Variant) then
4663 Variant := Comp_Variant;
4664 Comp_Ref := Comp;
4665
4666 elsif Variant /= Comp_Variant then
4667 declare
4668 D1 : constant Integer := Variant_Depth (Variant);
4669 D2 : constant Integer := Variant_Depth (Comp_Variant);
4670
4671 begin
4672 if D1 = D2
4673 or else
4674 (D1 > D2 and then not Nested_In (Variant, Comp_Variant))
4675 or else
4676 (D2 > D1 and then not Nested_In (Comp_Variant, Variant))
4677 then
4678 pragma Assert (Present (Comp_Ref));
4679 Error_Msg_Node_2 := Comp_Ref;
4680 Error_Msg_NE
4681 ("& and & appear in different variants", Id, Comp);
4682
4683 -- Otherwise retain the deeper variant for subsequent tests
4684
4685 elsif D2 > D1 then
4686 Variant := Comp_Variant;
4687 end if;
4688 end;
4689 end if;
4690 end if;
4691 end Check_Variant;
4692
4693 ------------------------
4694 -- Get_Component_Type --
4695 ------------------------
4696
4697 function Get_Component_Type
4698 (Selector : Node_Id; Enclosing_Type : Entity_Id) return Entity_Id
4699 is
4700 Comp : Entity_Id;
4701 begin
4702 case Nkind (Selector) is
4703 when N_Selected_Component | N_Indexed_Component =>
4704 -- a deep delta aggregate choice
4705
4706 declare
4707 Prefix_Type : constant Entity_Id :=
4708 Get_Component_Type (Prefix (Selector), Enclosing_Type);
4709 begin
4710 if No (Prefix_Type) then
4711 pragma Assert (Serious_Errors_Detected > 0);
4712 return Empty;
4713 end if;
4714
4715 -- Set the type of the prefix for GNATprove
4716
4717 Set_Etype (Prefix (Selector), Prefix_Type);
4718
4719 if Nkind (Selector) = N_Selected_Component then
4720 return Get_Component_Type
4721 (Selector_Name (Selector),
4722 Enclosing_Type => Prefix_Type);
4723 elsif not Is_Array_Type (Prefix_Type) then
4724 Error_Msg_NE
4725 ("type& is not an array type",
4726 Selector, Prefix_Type);
4727 elsif Number_Dimensions (Prefix_Type) /= 1 then
4728 Error_Msg_NE
4729 ("array type& not one-dimensional",
4730 Selector, Prefix_Type);
4731 elsif List_Length (Expressions (Selector)) /= 1 then
4732 Error_Msg_NE
4733 ("wrong number of indices for array type&",
4734 Selector, Prefix_Type);
4735 else
4736 Analyze_And_Resolve
4737 (First (Expressions (Selector)),
4738 Etype (First_Index (Prefix_Type)));
4739 return Component_Type (Prefix_Type);
4740 end if;
4741 end;
4742
4743 when others =>
4744 null;
4745 end case;
4746
4747 Comp := First_Entity (Enclosing_Type);
4748 while Present (Comp) loop
4749 if Chars (Comp) = Chars (Selector) then
4750 if Ekind (Comp) = E_Discriminant then
4751 Error_Msg_N ("delta cannot apply to discriminant", Selector);
4752 end if;
4753
4754 Set_Entity (Selector, Comp);
4755 Set_Etype (Selector, Etype (Comp));
4756
4757 return Etype (Comp);
4758 end if;
4759
4760 Next_Entity (Comp);
4761 end loop;
4762
4763 Error_Msg_NE
4764 ("type& has no component with this name", Selector, Enclosing_Type);
4765 return Empty;
4766 end Get_Component_Type;
4767
4768 ---------------
4769 -- Nested_In --
4770 ---------------
4771
4772 function Nested_In (V1, V2 : Node_Id) return Boolean is
4773 Par : Node_Id;
4774
4775 begin
4776 Par := Parent (V1);
4777 while Nkind (Par) /= N_Full_Type_Declaration loop
4778 if Par = V2 then
4779 return True;
4780 end if;
4781
4782 Par := Parent (Par);
4783 end loop;
4784
4785 return False;
4786 end Nested_In;
4787
4788 -------------------
4789 -- Variant_Depth --
4790 -------------------
4791
4792 function Variant_Depth (N : Node_Id) return Natural is
4793 Depth : Natural;
4794 Par : Node_Id;
4795
4796 begin
4797 Depth := 0;
4798 Par := Parent (N);
4799 while Nkind (Par) /= N_Full_Type_Declaration loop
4800 Depth := Depth + 1;
4801 Par := Parent (Par);
4802 end loop;
4803
4804 return Depth;
4805 end Variant_Depth;
4806
4807 -- Local variables
4808
4809 Deltas : constant List_Id := Component_Associations (N);
4810
4811 Assoc : Node_Id;
4812 Choice : Node_Id;
4813 Comp_Type : Entity_Id := Empty; -- init to avoid warning
4814 Deep_Choice : Boolean;
4815 Choice_Count : Natural := 0;
4816
4817 -- Start of processing for Resolve_Delta_Record_Aggregate
4818
4819 begin
4820 Variant := Empty;
4821
4822 Assoc := First (Deltas);
4823 while Present (Assoc) loop
4824 Choice := First (Choice_List (Assoc));
4825 while Present (Choice) loop
4826 Choice_Count := Choice_Count + 1;
4827
4828 Deep_Choice := Nkind (Choice) /= N_Identifier;
4829 if Deep_Choice then
4830 Error_Msg_GNAT_Extension
4831 ("deep delta aggregate", Sloc (Choice));
4832 end if;
4833
4834 Comp_Type := Get_Component_Type
4835 (Selector => Choice, Enclosing_Type => Typ);
4836
4837 -- Set the type of the choice for GNATprove
4838
4839 if Deep_Choice then
4840 Set_Etype (Choice, Comp_Type);
4841 end if;
4842
4843 if Present (Comp_Type) then
4844 if not Deep_Choice then
4845 -- ??? Not clear yet how RM 4.3.1(17.7) applies to a
4846 -- deep delta aggregate.
4847 Check_Variant (Choice);
4848 end if;
4849 else
4850 Comp_Type := Any_Type;
4851 end if;
4852
4853 Next (Choice);
4854 end loop;
4855
4856 pragma Assert (Present (Comp_Type));
4857
4858 -- A record_component_association in record_delta_aggregate shall not
4859 -- use the box compound delimiter <> rather than an expression; see
4860 -- RM 4.3.1(17.3/5).
4861
4862 pragma Assert (Present (Expression (Assoc)) xor Box_Present (Assoc));
4863
4864 if Box_Present (Assoc) then
4865 Error_Msg_N
4866 ("'<'> in record delta aggregate is not allowed", Assoc);
4867 else
4868 Analyze_And_Resolve (Expression (Assoc), Comp_Type);
4869
4870 -- The expression must not be of a limited type; RM 4.3.1(17.4/5)
4871
4872 if Is_Limited_Type (Etype (Expression (Assoc))) then
4873 Error_Msg_N
4874 ("expression of a limited type in record delta aggregate " &
4875 "is not allowed",
4876 Expression (Assoc));
4877 end if;
4878 end if;
4879
4880 Next (Assoc);
4881 end loop;
4882
4883 declare
4884 type Choice_Info is record
4885 Choice : Node_Id;
4886 Depth : Natural; -- 0 indicates non-record selector
4887 end record;
4888
4889 Info : array (1 .. Choice_Count) of Choice_Info;
4890 Current_Index : Natural := 0;
4891
4892 function Choice_Depth (Choice : Node_Id) return Natural;
4893 -- Given a choice in record delta aggregate, return 1 for
4894 -- "Abc", 3 for "Aa.Bb.Cc", and 0 if anything other than
4895 -- record component selectors are involved.
4896
4897 procedure Check_For_Bad_Overlap (Info1, Info2 : Choice_Info);
4898 -- If the two choices overlap illegally, then generate an error
4899 -- message. If deep delta aggregates are not enabled, then choices
4900 -- should be N_Identifier nodes and depths should each be 1.
4901
4902 ------------------
4903 -- Choice_Depth --
4904 ------------------
4905
4906 function Choice_Depth (Choice : Node_Id) return Natural is
4907 Prefix_Depth : Natural;
4908 begin
4909 case Nkind (Choice) is
4910 when N_Identifier =>
4911 return 1;
4912 when N_Selected_Component =>
4913 Prefix_Depth := Choice_Depth (Prefix (Choice));
4914 if Prefix_Depth = 0 then
4915 return 0;
4916 else
4917 return Prefix_Depth + 1;
4918 end if;
4919 when others =>
4920 return 0;
4921 end case;
4922 end Choice_Depth;
4923
4924 ---------------------------
4925 -- Check_For_Bad_Overlap --
4926 ---------------------------
4927
4928 procedure Check_For_Bad_Overlap (Info1, Info2 : Choice_Info) is
4929 Choice1, Choice2 : Node_Id;
4930 begin
4931 if Info1.Depth = 0 or Info2.Depth = 0 then
4932 -- We're not interested in cases involving array indexing
4933 return;
4934 end if;
4935 if Info1.Depth > Info2.Depth then
4936 -- Normalize
4937 Check_For_Bad_Overlap (Info1 => Info2, Info2 => Info1);
4938 return;
4939 end if;
4940 pragma Assert (Info1.Depth <= Info2.Depth);
4941 Choice1 := Info1.Choice;
4942 Choice2 := Info2.Choice;
4943
4944 -- Adjust deeper choice to match depth of the other choice
4945 for Count in 1 .. Info2.Depth - Info1.Depth loop
4946 pragma Assert (Nkind (Choice2) = N_Selected_Component);
4947 Choice2 := Prefix (Choice2);
4948 end loop;
4949
4950 -- Traverse the two choices; return if Entity mismatch found.
4951 loop
4952 pragma Assert (Nkind (Choice1) = Nkind (Choice2));
4953 if Nkind (Choice1) = N_Identifier then
4954 exit when Entity (Choice1) = Entity (Choice2);
4955 return; -- no overlap if entities differ
4956 end if;
4957 if Entity (Selector_Name (Choice1)) /=
4958 Entity (Selector_Name (Choice2))
4959 then
4960 return; -- no overlap if selected entities differ
4961 end if;
4962 Choice1 := Prefix (Choice1);
4963 Choice2 := Prefix (Choice2);
4964 end loop;
4965
4966 -- Illegal overlap detected
4967 Error_Msg_Sloc := Sloc (Info2.Choice);
4968 Error_Msg_NE
4969 ("record delta aggregate choice overlaps with choice & #",
4970 Info1.Choice, Info2.Choice);
4971 end Check_For_Bad_Overlap;
4972
4973 begin
4974 Assoc := First (Deltas);
4975 while Present (Assoc) loop
4976 Choice := First (Choice_List (Assoc));
4977 while Present (Choice) loop
4978 Current_Index := Current_Index + 1;
4979 Info (Current_Index) := (Choice => Choice,
4980 Depth => Choice_Depth (Choice));
4981
4982 -- Check against previous Info elements
4983 for Prev_Index in 1 .. Current_Index - 1 loop
4984 Check_For_Bad_Overlap
4985 (Info (Prev_Index), Info (Current_Index));
4986 end loop;
4987
4988 Next (Choice);
4989 end loop;
4990 Next (Assoc);
4991 end loop;
4992 end;
4993 end Resolve_Delta_Record_Aggregate;
4994
4995 ------------------------------
4996 -- Resolve_Deep_Delta_Assoc --
4997 ------------------------------
4998
4999 procedure Resolve_Deep_Delta_Assoc (N : Node_Id; Typ : Entity_Id) is
5000 Choice : constant Node_Id := First (Choice_List (N));
5001 Enclosing_Type : Entity_Id := Typ;
5002
5003 procedure Resolve_Choice_Prefix
5004 (Choice_Prefix : Node_Id; Enclosing_Type : in out Entity_Id);
5005 -- Recursively analyze selectors. Enclosing_Type is set to
5006 -- type of the last component.
5007
5008 ---------------------------
5009 -- Resolve_Choice_Prefix --
5010 ---------------------------
5011
5012 procedure Resolve_Choice_Prefix
5013 (Choice_Prefix : Node_Id; Enclosing_Type : in out Entity_Id)
5014 is
5015 Selector : Node_Id := Choice_Prefix;
5016 begin
5017 if not Is_Root_Prefix_Of_Deep_Choice (Choice_Prefix) then
5018 Resolve_Choice_Prefix (Prefix (Choice_Prefix), Enclosing_Type);
5019
5020 if Nkind (Choice_Prefix) = N_Selected_Component then
5021 Selector := Selector_Name (Choice_Prefix);
5022 else
5023 pragma Assert (Nkind (Choice_Prefix) = N_Indexed_Component);
5024 Selector := First (Expressions (Choice_Prefix));
5025 end if;
5026 end if;
5027
5028 if Is_Array_Type (Enclosing_Type) then
5029 Analyze_And_Resolve (Selector,
5030 Etype (First_Index (Enclosing_Type)));
5031 Enclosing_Type := Component_Type (Enclosing_Type);
5032 else
5033 declare
5034 Comp : Entity_Id := First_Entity (Enclosing_Type);
5035 Found : Boolean := False;
5036 begin
5037 while Present (Comp) and not Found loop
5038 if Chars (Comp) = Chars (Selector) then
5039 if Ekind (Comp) = E_Discriminant then
5040 Error_Msg_N ("delta cannot apply to discriminant",
5041 Selector);
5042 end if;
5043 Found := True;
5044 Set_Entity (Selector, Comp);
5045 Set_Etype (Selector, Etype (Comp));
5046 Set_Analyzed (Selector);
5047 Enclosing_Type := Etype (Comp);
5048 else
5049 Next_Entity (Comp);
5050 end if;
5051 end loop;
5052 if not Found then
5053 Error_Msg_NE
5054 ("type& has no component with this name",
5055 Selector, Enclosing_Type);
5056 end if;
5057 end;
5058 end if;
5059
5060 -- Set the type of the prefix for GNATprove, except for the root
5061 -- prefix, whose type is already the expected one for a record
5062 -- delta aggregate, or the type of the array index for an
5063 -- array delta aggregate (the only case here really since
5064 -- Resolve_Deep_Delta_Assoc is only called for array delta
5065 -- aggregates).
5066
5067 if Selector /= Choice_Prefix then
5068 Set_Etype (Choice_Prefix, Enclosing_Type);
5069 end if;
5070 end Resolve_Choice_Prefix;
5071 begin
5072 declare
5073 Unimplemented : exception; -- TEMPORARY
5074 begin
5075 if Present (Next (Choice)) then
5076 raise Unimplemented;
5077 end if;
5078 end;
5079
5080 Resolve_Choice_Prefix (Choice, Enclosing_Type);
5081 Analyze_And_Resolve (Expression (N), Enclosing_Type);
5082 end Resolve_Deep_Delta_Assoc;
5083
5084 ---------------------------------
5085 -- Resolve_Extension_Aggregate --
5086 ---------------------------------
5087
5088 -- There are two cases to consider:
5089
5090 -- a) If the ancestor part is a type mark, the components needed are the
5091 -- difference between the components of the expected type and the
5092 -- components of the given type mark.
5093
5094 -- b) If the ancestor part is an expression, it must be unambiguous, and
5095 -- once we have its type we can also compute the needed components as in
5096 -- the previous case. In both cases, if the ancestor type is not the
5097 -- immediate ancestor, we have to build this ancestor recursively.
5098
5099 -- In both cases, discriminants of the ancestor type do not play a role in
5100 -- the resolution of the needed components, because inherited discriminants
5101 -- cannot be used in a type extension. As a result we can compute
5102 -- independently the list of components of the ancestor type and of the
5103 -- expected type.
5104
5105 procedure Resolve_Extension_Aggregate (N : Node_Id; Typ : Entity_Id) is
5106 A : constant Node_Id := Ancestor_Part (N);
5107 A_Type : Entity_Id;
5108 I : Interp_Index;
5109 It : Interp;
5110
5111 function Valid_Limited_Ancestor (Anc : Node_Id) return Boolean;
5112 -- If the type is limited, verify that the ancestor part is a legal
5113 -- expression (aggregate or function call, including 'Input)) that does
5114 -- not require a copy, as specified in 7.5(2).
5115
5116 function Valid_Ancestor_Type return Boolean;
5117 -- Verify that the type of the ancestor part is a non-private ancestor
5118 -- of the expected type, which must be a type extension.
5119
5120 ----------------------------
5121 -- Valid_Limited_Ancestor --
5122 ----------------------------
5123
5124 function Valid_Limited_Ancestor (Anc : Node_Id) return Boolean is
5125 begin
5126 if Is_Entity_Name (Anc) and then Is_Type (Entity (Anc)) then
5127 return True;
5128
5129 -- The ancestor must be a call or an aggregate, but a call may
5130 -- have been expanded into a temporary, so check original node.
5131
5132 elsif Nkind (Anc) in N_Aggregate
5133 | N_Extension_Aggregate
5134 | N_Function_Call
5135 then
5136 return True;
5137
5138 elsif Nkind (Original_Node (Anc)) = N_Function_Call then
5139 return True;
5140
5141 elsif Nkind (Anc) = N_Attribute_Reference
5142 and then Attribute_Name (Anc) = Name_Input
5143 then
5144 return True;
5145
5146 elsif Nkind (Anc) = N_Qualified_Expression then
5147 return Valid_Limited_Ancestor (Expression (Anc));
5148
5149 elsif Nkind (Anc) = N_Raise_Expression then
5150 return True;
5151
5152 else
5153 return False;
5154 end if;
5155 end Valid_Limited_Ancestor;
5156
5157 -------------------------
5158 -- Valid_Ancestor_Type --
5159 -------------------------
5160
5161 function Valid_Ancestor_Type return Boolean is
5162 Imm_Type : Entity_Id;
5163
5164 begin
5165 Imm_Type := Base_Type (Typ);
5166 while Is_Derived_Type (Imm_Type) loop
5167 if Etype (Imm_Type) = Base_Type (A_Type) then
5168 return True;
5169
5170 -- The base type of the parent type may appear as a private
5171 -- extension if it is declared as such in a parent unit of the
5172 -- current one. For consistency of the subsequent analysis use
5173 -- the partial view for the ancestor part.
5174
5175 elsif Is_Private_Type (Etype (Imm_Type))
5176 and then Present (Full_View (Etype (Imm_Type)))
5177 and then Base_Type (A_Type) = Full_View (Etype (Imm_Type))
5178 then
5179 A_Type := Etype (Imm_Type);
5180 return True;
5181
5182 -- The parent type may be a private extension. The aggregate is
5183 -- legal if the type of the aggregate is an extension of it that
5184 -- is not a private extension.
5185
5186 elsif Is_Private_Type (A_Type)
5187 and then not Is_Private_Type (Imm_Type)
5188 and then Present (Full_View (A_Type))
5189 and then Base_Type (Full_View (A_Type)) = Etype (Imm_Type)
5190 then
5191 return True;
5192
5193 -- The parent type may be a raise expression (which is legal in
5194 -- any expression context).
5195
5196 elsif A_Type = Raise_Type then
5197 A_Type := Etype (Imm_Type);
5198 return True;
5199
5200 else
5201 Imm_Type := Etype (Base_Type (Imm_Type));
5202 end if;
5203 end loop;
5204
5205 -- If previous loop did not find a proper ancestor, report error
5206
5207 Error_Msg_NE ("expect ancestor type of &", A, Typ);
5208 return False;
5209 end Valid_Ancestor_Type;
5210
5211 -- Start of processing for Resolve_Extension_Aggregate
5212
5213 begin
5214 -- Analyze the ancestor part and account for the case where it is a
5215 -- parameterless function call.
5216
5217 Analyze (A);
5218 Check_Parameterless_Call (A);
5219
5220 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
5221
5222 -- AI05-0115: If the ancestor part is a subtype mark, the ancestor
5223 -- must not have unknown discriminants. To catch cases where the
5224 -- aggregate occurs at a place where the full view of the ancestor
5225 -- type is visible and doesn't have unknown discriminants, but the
5226 -- aggregate type was derived from a partial view that has unknown
5227 -- discriminants, we check whether the aggregate type has unknown
5228 -- discriminants (unknown discriminants were inherited), along
5229 -- with checking that the partial view of the ancestor has unknown
5230 -- discriminants. (It might be sufficient to replace the entire
5231 -- condition with Has_Unknown_Discriminants (Typ), but that might
5232 -- miss some cases, not clear, and causes error changes in some tests
5233 -- such as class-wide cases, that aren't clearly improvements. ???)
5234
5235 if Has_Unknown_Discriminants (Entity (A))
5236 or else (Has_Unknown_Discriminants (Typ)
5237 and then Partial_View_Has_Unknown_Discr (Entity (A)))
5238 then
5239 Error_Msg_NE
5240 ("aggregate not available for type& whose ancestor "
5241 & "has unknown discriminants", N, Typ);
5242 end if;
5243 end if;
5244
5245 if not Is_Tagged_Type (Typ) then
5246 Error_Msg_N ("type of extension aggregate must be tagged", N);
5247 return;
5248
5249 elsif Is_Limited_Type (Typ) then
5250
5251 -- Ada 2005 (AI-287): Limited aggregates are allowed
5252
5253 if Ada_Version < Ada_2005 then
5254 Error_Msg_N ("aggregate type cannot be limited", N);
5255 Explain_Limited_Type (Typ, N);
5256 return;
5257
5258 elsif Valid_Limited_Ancestor (A) then
5259 null;
5260
5261 else
5262 Error_Msg_N
5263 ("limited ancestor part must be aggregate or function call", A);
5264 end if;
5265
5266 elsif Is_Class_Wide_Type (Typ) then
5267 Error_Msg_N ("aggregate cannot be of a class-wide type", N);
5268 return;
5269 end if;
5270
5271 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
5272 A_Type := Get_Full_View (Entity (A));
5273
5274 if Valid_Ancestor_Type then
5275 Set_Entity (A, A_Type);
5276 Set_Etype (A, A_Type);
5277
5278 Validate_Ancestor_Part (N);
5279 Resolve_Record_Aggregate (N, Typ);
5280 end if;
5281
5282 elsif Nkind (A) /= N_Aggregate then
5283 if Is_Overloaded (A) then
5284 A_Type := Any_Type;
5285
5286 Get_First_Interp (A, I, It);
5287 while Present (It.Typ) loop
5288
5289 -- Consider limited interpretations if Ada 2005 or higher
5290
5291 if Is_Tagged_Type (It.Typ)
5292 and then (Ada_Version >= Ada_2005
5293 or else not Is_Limited_Type (It.Typ))
5294 then
5295 if A_Type /= Any_Type then
5296 Error_Msg_N ("cannot resolve expression", A);
5297 return;
5298 else
5299 A_Type := It.Typ;
5300 end if;
5301 end if;
5302
5303 Get_Next_Interp (I, It);
5304 end loop;
5305
5306 if A_Type = Any_Type then
5307 if Ada_Version >= Ada_2005 then
5308 Error_Msg_N
5309 ("ancestor part must be of a tagged type", A);
5310 else
5311 Error_Msg_N
5312 ("ancestor part must be of a nonlimited tagged type", A);
5313 end if;
5314
5315 return;
5316 end if;
5317
5318 else
5319 A_Type := Etype (A);
5320 end if;
5321
5322 if Valid_Ancestor_Type then
5323 Resolve (A, A_Type);
5324 Check_Unset_Reference (A);
5325 Check_Non_Static_Context (A);
5326
5327 -- The aggregate is illegal if the ancestor expression is a call
5328 -- to a function with a limited unconstrained result, unless the
5329 -- type of the aggregate is a null extension. This restriction
5330 -- was added in AI05-67 to simplify implementation.
5331
5332 if Nkind (A) = N_Function_Call
5333 and then Is_Limited_Type (A_Type)
5334 and then not Is_Null_Extension (Typ)
5335 and then not Is_Constrained (A_Type)
5336 then
5337 Error_Msg_N
5338 ("type of limited ancestor part must be constrained", A);
5339
5340 -- Reject the use of CPP constructors that leave objects partially
5341 -- initialized. For example:
5342
5343 -- type CPP_Root is tagged limited record ...
5344 -- pragma Import (CPP, CPP_Root);
5345
5346 -- type CPP_DT is new CPP_Root and Iface ...
5347 -- pragma Import (CPP, CPP_DT);
5348
5349 -- type Ada_DT is new CPP_DT with ...
5350
5351 -- Obj : Ada_DT := Ada_DT'(New_CPP_Root with others => <>);
5352
5353 -- Using the constructor of CPP_Root the slots of the dispatch
5354 -- table of CPP_DT cannot be set, and the secondary tag of
5355 -- CPP_DT is unknown.
5356
5357 elsif Nkind (A) = N_Function_Call
5358 and then Is_CPP_Constructor_Call (A)
5359 and then Enclosing_CPP_Parent (Typ) /= A_Type
5360 then
5361 Error_Msg_NE
5362 ("??must use 'C'P'P constructor for type &", A,
5363 Enclosing_CPP_Parent (Typ));
5364
5365 -- The following call is not needed if the previous warning
5366 -- is promoted to an error.
5367
5368 Resolve_Record_Aggregate (N, Typ);
5369
5370 elsif Is_Class_Wide_Type (Etype (A))
5371 and then Nkind (Original_Node (A)) = N_Function_Call
5372 then
5373 -- If the ancestor part is a dispatching call, it appears
5374 -- statically to be a legal ancestor, but it yields any member
5375 -- of the class, and it is not possible to determine whether
5376 -- it is an ancestor of the extension aggregate (much less
5377 -- which ancestor). It is not possible to determine the
5378 -- components of the extension part.
5379
5380 -- This check implements AI-306, which in fact was motivated by
5381 -- an AdaCore query to the ARG after this test was added.
5382
5383 Error_Msg_N ("ancestor part must be statically tagged", A);
5384
5385 else
5386 Resolve_Record_Aggregate (N, Typ);
5387 end if;
5388 end if;
5389
5390 else
5391 Error_Msg_N ("no unique type for this aggregate", A);
5392 end if;
5393
5394 Check_Function_Writable_Actuals (N);
5395 end Resolve_Extension_Aggregate;
5396
5397 ----------------------------------
5398 -- Resolve_Null_Array_Aggregate --
5399 ----------------------------------
5400
5401 function Resolve_Null_Array_Aggregate (N : Node_Id) return Boolean is
5402 -- Never returns False, but declared as a function to match
5403 -- other Resolve_Mumble functions.
5404
5405 Loc : constant Source_Ptr := Sloc (N);
5406 Typ : constant Entity_Id := Etype (N);
5407
5408 Constr : constant List_Id := New_List;
5409 Index : Node_Id;
5410 Index_Typ : Node_Id;
5411 Known_Bounds : Boolean := True;
5412 Lo, Hi : Node_Id;
5413
5414 begin
5415 -- Attach the list of constraints at the location of the aggregate, so
5416 -- the individual constraints can be analyzed.
5417
5418 Set_Parent (Constr, N);
5419
5420 -- Populate the list with null ranges. The relevant RM clauses are
5421 -- RM 4.3.3 (26.1) and RM 4.3.3 (26).
5422
5423 Index := First_Index (Typ);
5424 while Present (Index) loop
5425 Get_Index_Bounds (Index, L => Lo, H => Hi);
5426 Index_Typ := Etype (Index);
5427
5428 Known_Bounds := Known_Bounds
5429 and Compile_Time_Known_Value (Lo)
5430 and Compile_Time_Known_Value (Hi);
5431
5432 if Cannot_Compute_High_Bound (Index) then
5433 -- The upper bound is the higger bound to avoid reporting
5434 -- spurious errors; this value will not be used at runtime
5435 -- because this aggregate will be replaced by a raise CE node,
5436 -- or the index type is formal of a generic unit.
5437
5438 Hi := New_Copy_Tree (Lo);
5439
5440 Report_Null_Array_Constraint_Error (N, Index_Typ);
5441 Set_Raises_Constraint_Error (N);
5442
5443 else
5444 -- The upper bound is the predecessor of the lower bound
5445
5446 Hi := Make_Attribute_Reference (Loc,
5447 Prefix => New_Occurrence_Of (Etype (Index), Loc),
5448 Attribute_Name => Name_Pred,
5449 Expressions => New_List (New_Copy_Tree (Lo)));
5450 end if;
5451
5452 Append (Make_Range (Loc, New_Copy_Tree (Lo), Hi), Constr);
5453 Analyze_And_Resolve (Last (Constr), Etype (Index));
5454
5455 Next_Index (Index);
5456 end loop;
5457
5458 Set_Compile_Time_Known_Aggregate (N, Known_Bounds);
5459 Set_Aggregate_Bounds (N, First (Constr));
5460
5461 return True;
5462 end Resolve_Null_Array_Aggregate;
5463
5464 ------------------------------
5465 -- Resolve_Record_Aggregate --
5466 ------------------------------
5467
5468 procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id) is
5469 New_Assoc_List : constant List_Id := New_List;
5470 -- New_Assoc_List is the newly built list of N_Component_Association
5471 -- nodes.
5472
5473 Others_Etype : Entity_Id := Empty;
5474 -- This variable is used to save the Etype of the last record component
5475 -- that takes its value from the others choice. Its purpose is:
5476 --
5477 -- (a) make sure the others choice is useful
5478 --
5479 -- (b) make sure the type of all the components whose value is
5480 -- subsumed by the others choice are the same.
5481 --
5482 -- This variable is updated as a side effect of function Get_Value.
5483
5484 Box_Node : Node_Id := Empty;
5485 Is_Box_Present : Boolean := False;
5486 Is_Box_Init_By_Default : Boolean := False;
5487 Others_Box : Natural := 0;
5488 -- Ada 2005 (AI-287): Variables used in case of default initialization
5489 -- to provide a functionality similar to Others_Etype. Box_Present
5490 -- indicates that the component takes its default initialization;
5491 -- Others_Box counts the number of components of the current aggregate
5492 -- (which may be a sub-aggregate of a larger one) that are default-
5493 -- initialized. A value of One indicates that an others_box is present.
5494 -- Any larger value indicates that the others_box is not redundant.
5495 -- These variables, similar to Others_Etype, are also updated as a side
5496 -- effect of function Get_Value. Box_Node is used to place a warning on
5497 -- a redundant others_box.
5498
5499 procedure Add_Association
5500 (Component : Entity_Id;
5501 Expr : Node_Id;
5502 Assoc_List : List_Id;
5503 Is_Box_Present : Boolean := False);
5504 -- Builds a new N_Component_Association node which associates Component
5505 -- to expression Expr and adds it to the association list being built,
5506 -- either New_Assoc_List, or the association being built for an inner
5507 -- aggregate.
5508
5509 function Discriminant_Present (Input_Discr : Entity_Id) return Boolean;
5510 -- If aggregate N is a regular aggregate this routine will return True.
5511 -- Otherwise, if N is an extension aggregate, then Input_Discr denotes
5512 -- a discriminant whose value may already have been specified by N's
5513 -- ancestor part. This routine checks whether this is indeed the case
5514 -- and if so returns False, signaling that no value for Input_Discr
5515 -- should appear in N's aggregate part. Also, in this case, the routine
5516 -- appends to New_Assoc_List the discriminant value specified in the
5517 -- ancestor part.
5518 --
5519 -- If the aggregate is in a context with expansion delayed, it will be
5520 -- reanalyzed. The inherited discriminant values must not be reinserted
5521 -- in the component list to prevent spurious errors, but they must be
5522 -- present on first analysis to build the proper subtype indications.
5523 -- The flag Inherited_Discriminant is used to prevent the re-insertion.
5524
5525 function Find_Private_Ancestor (Typ : Entity_Id) return Entity_Id;
5526 -- AI05-0115: Find earlier ancestor in the derivation chain that is
5527 -- derived from private view Typ. Whether the aggregate is legal depends
5528 -- on the current visibility of the type as well as that of the parent
5529 -- of the ancestor.
5530
5531 function Get_Value
5532 (Compon : Entity_Id;
5533 From : List_Id;
5534 Consider_Others_Choice : Boolean := False) return Node_Id;
5535 -- Given a record component stored in parameter Compon, this function
5536 -- returns its value as it appears in the list From, which is a list
5537 -- of N_Component_Association nodes.
5538 --
5539 -- If no component association has a choice for the searched component,
5540 -- the value provided by the others choice is returned, if there is one,
5541 -- and Consider_Others_Choice is set to true. Otherwise Empty is
5542 -- returned. If there is more than one component association giving a
5543 -- value for the searched record component, an error message is emitted
5544 -- and the first found value is returned.
5545 --
5546 -- If Consider_Others_Choice is set and the returned expression comes
5547 -- from the others choice, then Others_Etype is set as a side effect.
5548 -- An error message is emitted if the components taking their value from
5549 -- the others choice do not have same type.
5550
5551 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Entity_Id);
5552 -- Analyzes and resolves expression Expr against the Etype of the
5553 -- Component. This routine also applies all appropriate checks to Expr.
5554 -- It finally saves a Expr in the newly created association list that
5555 -- will be attached to the final record aggregate. Note that if the
5556 -- Parent pointer of Expr is not set then Expr was produced with a
5557 -- New_Copy_Tree or some such.
5558
5559 procedure Rewrite_Range (Root_Type : Entity_Id; Rge : Node_Id);
5560 -- Rewrite a range node Rge when its bounds refer to non-stored
5561 -- discriminants from Root_Type, to replace them with the stored
5562 -- discriminant values. This is required in GNATprove mode, and is
5563 -- adopted in all modes to avoid special-casing GNATprove mode.
5564
5565 ---------------------
5566 -- Add_Association --
5567 ---------------------
5568
5569 procedure Add_Association
5570 (Component : Entity_Id;
5571 Expr : Node_Id;
5572 Assoc_List : List_Id;
5573 Is_Box_Present : Boolean := False)
5574 is
5575 Choice_List : constant List_Id := New_List;
5576 Loc : Source_Ptr;
5577
5578 begin
5579 -- If this is a box association the expression is missing, so use the
5580 -- Sloc of the aggregate itself for the new association.
5581
5582 pragma Assert (Present (Expr) xor Is_Box_Present);
5583
5584 if Present (Expr) then
5585 Loc := Sloc (Expr);
5586 else
5587 Loc := Sloc (N);
5588 end if;
5589
5590 Append_To (Choice_List, New_Occurrence_Of (Component, Loc));
5591
5592 Append_To (Assoc_List,
5593 Make_Component_Association (Loc,
5594 Choices => Choice_List,
5595 Expression => Expr,
5596 Box_Present => Is_Box_Present));
5597
5598 -- If this association has a box for a component that is initialized
5599 -- by default, then set flag on the new association to indicate that
5600 -- the original association was for such a box-initialized component.
5601
5602 if Is_Box_Init_By_Default then
5603 Set_Was_Default_Init_Box_Association (Last (Assoc_List));
5604 end if;
5605 end Add_Association;
5606
5607 --------------------------
5608 -- Discriminant_Present --
5609 --------------------------
5610
5611 function Discriminant_Present (Input_Discr : Entity_Id) return Boolean is
5612 Regular_Aggr : constant Boolean := Nkind (N) /= N_Extension_Aggregate;
5613
5614 Ancestor_Is_Subtyp : Boolean;
5615
5616 Loc : Source_Ptr;
5617
5618 Ancestor : Node_Id;
5619 Ancestor_Typ : Entity_Id;
5620 Comp_Assoc : Node_Id;
5621 Discr : Entity_Id;
5622 Discr_Expr : Node_Id;
5623 Discr_Val : Elmt_Id := No_Elmt;
5624 Orig_Discr : Entity_Id;
5625
5626 begin
5627 if Regular_Aggr then
5628 return True;
5629 end if;
5630
5631 -- Check whether inherited discriminant values have already been
5632 -- inserted in the aggregate. This will be the case if we are
5633 -- re-analyzing an aggregate whose expansion was delayed.
5634
5635 if Present (Component_Associations (N)) then
5636 Comp_Assoc := First (Component_Associations (N));
5637 while Present (Comp_Assoc) loop
5638 if Inherited_Discriminant (Comp_Assoc) then
5639 return True;
5640 end if;
5641
5642 Next (Comp_Assoc);
5643 end loop;
5644 end if;
5645
5646 Ancestor := Ancestor_Part (N);
5647 Ancestor_Typ := Etype (Ancestor);
5648 Loc := Sloc (Ancestor);
5649
5650 -- For a private type with unknown discriminants, use the underlying
5651 -- record view if it is available.
5652
5653 if Has_Unknown_Discriminants (Ancestor_Typ)
5654 and then Present (Full_View (Ancestor_Typ))
5655 and then Present (Underlying_Record_View (Full_View (Ancestor_Typ)))
5656 then
5657 Ancestor_Typ := Underlying_Record_View (Full_View (Ancestor_Typ));
5658 end if;
5659
5660 Ancestor_Is_Subtyp :=
5661 Is_Entity_Name (Ancestor) and then Is_Type (Entity (Ancestor));
5662
5663 -- If the ancestor part has no discriminants clearly N's aggregate
5664 -- part must provide a value for Discr.
5665
5666 if not Has_Discriminants (Ancestor_Typ) then
5667 return True;
5668
5669 -- If the ancestor part is an unconstrained subtype mark then the
5670 -- Discr must be present in N's aggregate part.
5671
5672 elsif Ancestor_Is_Subtyp
5673 and then not Is_Constrained (Entity (Ancestor))
5674 then
5675 return True;
5676 end if;
5677
5678 -- Now look to see if Discr was specified in the ancestor part
5679
5680 if Ancestor_Is_Subtyp then
5681 Discr_Val :=
5682 First_Elmt (Discriminant_Constraint (Entity (Ancestor)));
5683 end if;
5684
5685 Orig_Discr := Original_Record_Component (Input_Discr);
5686
5687 Discr := First_Discriminant (Ancestor_Typ);
5688 while Present (Discr) loop
5689
5690 -- If Ancestor has already specified Disc value then insert its
5691 -- value in the final aggregate.
5692
5693 if Original_Record_Component (Discr) = Orig_Discr then
5694 if Ancestor_Is_Subtyp then
5695 Discr_Expr := New_Copy_Tree (Node (Discr_Val));
5696 else
5697 Discr_Expr :=
5698 Make_Selected_Component (Loc,
5699 Prefix => Duplicate_Subexpr (Ancestor),
5700 Selector_Name => New_Occurrence_Of (Input_Discr, Loc));
5701 end if;
5702
5703 Resolve_Aggr_Expr (Discr_Expr, Input_Discr);
5704 Set_Inherited_Discriminant (Last (New_Assoc_List));
5705 return False;
5706 end if;
5707
5708 Next_Discriminant (Discr);
5709
5710 if Ancestor_Is_Subtyp then
5711 Next_Elmt (Discr_Val);
5712 end if;
5713 end loop;
5714
5715 return True;
5716 end Discriminant_Present;
5717
5718 ---------------------------
5719 -- Find_Private_Ancestor --
5720 ---------------------------
5721
5722 function Find_Private_Ancestor (Typ : Entity_Id) return Entity_Id is
5723 Par : Entity_Id;
5724
5725 begin
5726 Par := Typ;
5727 loop
5728 if Has_Private_Ancestor (Par)
5729 and then not Has_Private_Ancestor (Etype (Base_Type (Par)))
5730 then
5731 return Par;
5732
5733 elsif not Is_Derived_Type (Par) then
5734 return Empty;
5735
5736 else
5737 Par := Etype (Base_Type (Par));
5738 end if;
5739 end loop;
5740 end Find_Private_Ancestor;
5741
5742 ---------------
5743 -- Get_Value --
5744 ---------------
5745
5746 function Get_Value
5747 (Compon : Entity_Id;
5748 From : List_Id;
5749 Consider_Others_Choice : Boolean := False) return Node_Id
5750 is
5751 Typ : constant Entity_Id := Etype (Compon);
5752 Assoc : Node_Id;
5753 Expr : Node_Id := Empty;
5754 Selector_Name : Node_Id;
5755
5756 begin
5757 Is_Box_Present := False;
5758 Is_Box_Init_By_Default := False;
5759
5760 if No (From) then
5761 return Empty;
5762 end if;
5763
5764 Assoc := First (From);
5765 while Present (Assoc) loop
5766 Selector_Name := First (Choices (Assoc));
5767 while Present (Selector_Name) loop
5768 if Nkind (Selector_Name) = N_Others_Choice then
5769 if Consider_Others_Choice and then No (Expr) then
5770
5771 -- We need to duplicate the expression for each
5772 -- successive component covered by the others choice.
5773 -- This is redundant if the others_choice covers only
5774 -- one component (small optimization possible???), but
5775 -- indispensable otherwise, because each one must be
5776 -- expanded individually to preserve side effects.
5777
5778 -- Ada 2005 (AI-287): In case of default initialization
5779 -- of components, we duplicate the corresponding default
5780 -- expression (from the record type declaration). The
5781 -- copy must carry the sloc of the association (not the
5782 -- original expression) to prevent spurious elaboration
5783 -- checks when the default includes function calls.
5784
5785 if Box_Present (Assoc) then
5786 Others_Box := Others_Box + 1;
5787 Is_Box_Present := True;
5788
5789 if Expander_Active then
5790 return
5791 New_Copy_Tree_And_Copy_Dimensions
5792 (Expression (Parent (Compon)),
5793 New_Sloc => Sloc (Assoc));
5794 else
5795 return Expression (Parent (Compon));
5796 end if;
5797
5798 else
5799 if Present (Others_Etype)
5800 and then Base_Type (Others_Etype) /= Base_Type (Typ)
5801 then
5802 -- If the components are of an anonymous access
5803 -- type they are distinct, but this is legal in
5804 -- Ada 2012 as long as designated types match.
5805
5806 if (Ekind (Typ) = E_Anonymous_Access_Type
5807 or else Ekind (Typ) =
5808 E_Anonymous_Access_Subprogram_Type)
5809 and then Designated_Type (Typ) =
5810 Designated_Type (Others_Etype)
5811 then
5812 null;
5813 else
5814 Error_Msg_N
5815 ("components in OTHERS choice must have same "
5816 & "type", Selector_Name);
5817 end if;
5818 end if;
5819
5820 Others_Etype := Typ;
5821
5822 -- Copy the expression so that it is resolved
5823 -- independently for each component, This is needed
5824 -- for accessibility checks on components of anonymous
5825 -- access types, even in compile_only mode.
5826
5827 if not Inside_A_Generic then
5828 return
5829 New_Copy_Tree_And_Copy_Dimensions
5830 (Expression (Assoc));
5831 else
5832 return Expression (Assoc);
5833 end if;
5834 end if;
5835 end if;
5836
5837 elsif Chars (Compon) = Chars (Selector_Name) then
5838 if No (Expr) then
5839
5840 -- Ada 2005 (AI-231)
5841
5842 if Ada_Version >= Ada_2005
5843 and then Known_Null (Expression (Assoc))
5844 then
5845 Check_Can_Never_Be_Null (Compon, Expression (Assoc));
5846 end if;
5847
5848 -- We need to duplicate the expression when several
5849 -- components are grouped together with a "|" choice.
5850 -- For instance "filed1 | filed2 => Expr"
5851
5852 -- Ada 2005 (AI-287)
5853
5854 if Box_Present (Assoc) then
5855 Is_Box_Present := True;
5856
5857 -- Duplicate the default expression of the component
5858 -- from the record type declaration, so a new copy
5859 -- can be attached to the association.
5860
5861 -- Note that we always copy the default expression,
5862 -- even when the association has a single choice, in
5863 -- order to create a proper association for the
5864 -- expanded aggregate.
5865
5866 -- Component may have no default, in which case the
5867 -- expression is empty and the component is default-
5868 -- initialized, but an association for the component
5869 -- exists, and it is not covered by an others clause.
5870
5871 -- Scalar and private types have no initialization
5872 -- procedure, so they remain uninitialized. If the
5873 -- target of the aggregate is a constant this
5874 -- deserves a warning.
5875
5876 if No (Expression (Parent (Compon)))
5877 and then not Has_Non_Null_Base_Init_Proc (Typ)
5878 and then not Has_Aspect (Typ, Aspect_Default_Value)
5879 and then not Is_Concurrent_Type (Typ)
5880 and then Nkind (Parent (N)) = N_Object_Declaration
5881 and then Constant_Present (Parent (N))
5882 then
5883 Error_Msg_Node_2 := Typ;
5884 Error_Msg_NE
5885 ("??component& of type& is uninitialized",
5886 Assoc, Selector_Name);
5887
5888 -- An additional reminder if the component type
5889 -- is a generic formal.
5890
5891 if Is_Generic_Type (Base_Type (Typ)) then
5892 Error_Msg_NE
5893 ("\instance should provide actual type with "
5894 & "initialization for&", Assoc, Typ);
5895 end if;
5896 end if;
5897
5898 return
5899 New_Copy_Tree_And_Copy_Dimensions
5900 (Expression (Parent (Compon)));
5901
5902 else
5903 if Present (Next (Selector_Name)) then
5904 Expr := New_Copy_Tree_And_Copy_Dimensions
5905 (Expression (Assoc));
5906 else
5907 Expr := Expression (Assoc);
5908 end if;
5909 end if;
5910
5911 Generate_Reference (Compon, Selector_Name, 'm');
5912
5913 else
5914 Error_Msg_NE
5915 ("more than one value supplied for &",
5916 Selector_Name, Compon);
5917
5918 end if;
5919 end if;
5920
5921 Next (Selector_Name);
5922 end loop;
5923
5924 Next (Assoc);
5925 end loop;
5926
5927 return Expr;
5928 end Get_Value;
5929
5930 -----------------------
5931 -- Resolve_Aggr_Expr --
5932 -----------------------
5933
5934 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Entity_Id) is
5935 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean;
5936 -- If the expression is an aggregate (possibly qualified) then its
5937 -- expansion is delayed until the enclosing aggregate is expanded
5938 -- into assignments. In that case, do not generate checks on the
5939 -- expression, because they will be generated later, and will other-
5940 -- wise force a copy (to remove side effects) that would leave a
5941 -- dynamic-sized aggregate in the code, something that gigi cannot
5942 -- handle.
5943
5944 ---------------------------
5945 -- Has_Expansion_Delayed --
5946 ---------------------------
5947
5948 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean is
5949 begin
5950 return
5951 (Nkind (Expr) in N_Aggregate | N_Extension_Aggregate
5952 and then Present (Etype (Expr))
5953 and then Is_Record_Type (Etype (Expr))
5954 and then Expansion_Delayed (Expr))
5955 or else
5956 (Nkind (Expr) = N_Qualified_Expression
5957 and then Has_Expansion_Delayed (Expression (Expr)));
5958 end Has_Expansion_Delayed;
5959
5960 -- Local variables
5961
5962 Expr_Type : Entity_Id := Empty;
5963 New_C : Entity_Id := Component;
5964 New_Expr : Node_Id;
5965
5966 Relocate : Boolean;
5967 -- Set to True if the resolved Expr node needs to be relocated when
5968 -- attached to the newly created association list. This node need not
5969 -- be relocated if its parent pointer is not set. In fact in this
5970 -- case Expr is the output of a New_Copy_Tree call. If Relocate is
5971 -- True then we have analyzed the expression node in the original
5972 -- aggregate and hence it needs to be relocated when moved over to
5973 -- the new association list.
5974
5975 -- Start of processing for Resolve_Aggr_Expr
5976
5977 begin
5978 -- If the type of the component is elementary or the type of the
5979 -- aggregate does not contain discriminants, use the type of the
5980 -- component to resolve Expr.
5981
5982 if Is_Elementary_Type (Etype (Component))
5983 or else not Has_Discriminants (Etype (N))
5984 then
5985 Expr_Type := Etype (Component);
5986
5987 -- Otherwise we have to pick up the new type of the component from
5988 -- the new constrained subtype of the aggregate. In fact components
5989 -- which are of a composite type might be constrained by a
5990 -- discriminant, and we want to resolve Expr against the subtype were
5991 -- all discriminant occurrences are replaced with their actual value.
5992
5993 else
5994 New_C := First_Component (Etype (N));
5995 while Present (New_C) loop
5996 if Chars (New_C) = Chars (Component) then
5997 Expr_Type := Etype (New_C);
5998 exit;
5999 end if;
6000
6001 Next_Component (New_C);
6002 end loop;
6003
6004 pragma Assert (Present (Expr_Type));
6005
6006 -- For each range in an array type where a discriminant has been
6007 -- replaced with the constraint, check that this range is within
6008 -- the range of the base type. This checks is done in the init
6009 -- proc for regular objects, but has to be done here for
6010 -- aggregates since no init proc is called for them.
6011
6012 if Is_Array_Type (Expr_Type) then
6013 declare
6014 Index : Node_Id;
6015 -- Range of the current constrained index in the array
6016
6017 Orig_Index : Node_Id := First_Index (Etype (Component));
6018 -- Range corresponding to the range Index above in the
6019 -- original unconstrained record type. The bounds of this
6020 -- range may be governed by discriminants.
6021
6022 Unconstr_Index : Node_Id := First_Index (Etype (Expr_Type));
6023 -- Range corresponding to the range Index above for the
6024 -- unconstrained array type. This range is needed to apply
6025 -- range checks.
6026
6027 begin
6028 Index := First_Index (Expr_Type);
6029 while Present (Index) loop
6030 if Depends_On_Discriminant (Orig_Index) then
6031 Apply_Range_Check (Index, Etype (Unconstr_Index));
6032 end if;
6033
6034 Next_Index (Index);
6035 Next_Index (Orig_Index);
6036 Next_Index (Unconstr_Index);
6037 end loop;
6038 end;
6039 end if;
6040 end if;
6041
6042 -- If the Parent pointer of Expr is not set, Expr is an expression
6043 -- duplicated by New_Tree_Copy (this happens for record aggregates
6044 -- that look like (Field1 | Filed2 => Expr) or (others => Expr)).
6045 -- Such a duplicated expression must be attached to the tree
6046 -- before analysis and resolution to enforce the rule that a tree
6047 -- fragment should never be analyzed or resolved unless it is
6048 -- attached to the current compilation unit.
6049
6050 if No (Parent (Expr)) then
6051 Set_Parent (Expr, N);
6052 Relocate := False;
6053 else
6054 Relocate := True;
6055 end if;
6056
6057 -- Obtain the corresponding mutably tagged types if we are looking
6058 -- at a special internally generated class-wide equivalent type.
6059
6060 Expr_Type :=
6061 Get_Corresponding_Mutably_Tagged_Type_If_Present (Expr_Type);
6062
6063 Analyze_And_Resolve (Expr, Expr_Type);
6064 Check_Expr_OK_In_Limited_Aggregate (Expr);
6065 Check_Non_Static_Context (Expr);
6066 Check_Unset_Reference (Expr);
6067
6068 -- Check wrong use of class-wide types
6069
6070 if Is_Class_Wide_Type (Etype (Expr))
6071 and then not Is_Mutably_Tagged_Type (Expr_Type)
6072 then
6073 Error_Msg_N ("dynamically tagged expression not allowed", Expr);
6074 end if;
6075
6076 if not Has_Expansion_Delayed (Expr) then
6077 Aggregate_Constraint_Checks (Expr, Expr_Type);
6078 end if;
6079
6080 -- If an aggregate component has a type with predicates, an explicit
6081 -- predicate check must be applied, as for an assignment statement,
6082 -- because the aggregate might not be expanded into individual
6083 -- component assignments.
6084
6085 if Has_Predicates (Expr_Type)
6086 and then Analyzed (Expr)
6087 then
6088 Apply_Predicate_Check (Expr, Expr_Type);
6089 end if;
6090
6091 if Raises_Constraint_Error (Expr) then
6092 Set_Raises_Constraint_Error (N);
6093 end if;
6094
6095 -- If the expression has been marked as requiring a range check, then
6096 -- generate it here. It's a bit odd to be generating such checks in
6097 -- the analyzer, but harmless since Generate_Range_Check does nothing
6098 -- (other than making sure Do_Range_Check is set) if the expander is
6099 -- not active.
6100
6101 if Do_Range_Check (Expr) then
6102 Generate_Range_Check (Expr, Expr_Type, CE_Range_Check_Failed);
6103 end if;
6104
6105 -- Add association Component => Expr if the caller requests it
6106
6107 if Relocate then
6108 New_Expr := Relocate_Node (Expr);
6109
6110 -- Since New_Expr is not gonna be analyzed later on, we need to
6111 -- propagate here the dimensions form Expr to New_Expr.
6112
6113 Copy_Dimensions (Expr, New_Expr);
6114
6115 else
6116 New_Expr := Expr;
6117 end if;
6118
6119 Add_Association (New_C, New_Expr, New_Assoc_List);
6120 end Resolve_Aggr_Expr;
6121
6122 -------------------
6123 -- Rewrite_Range --
6124 -------------------
6125
6126 procedure Rewrite_Range (Root_Type : Entity_Id; Rge : Node_Id) is
6127 procedure Rewrite_Bound
6128 (Bound : Node_Id;
6129 Disc : Entity_Id;
6130 Expr_Disc : Node_Id);
6131 -- Rewrite a bound of the range Bound, when it is equal to the
6132 -- non-stored discriminant Disc, into the stored discriminant
6133 -- value Expr_Disc.
6134
6135 -------------------
6136 -- Rewrite_Bound --
6137 -------------------
6138
6139 procedure Rewrite_Bound
6140 (Bound : Node_Id;
6141 Disc : Entity_Id;
6142 Expr_Disc : Node_Id)
6143 is
6144 begin
6145 if Nkind (Bound) /= N_Identifier then
6146 return;
6147 end if;
6148
6149 -- We expect either the discriminant or the discriminal
6150
6151 if Entity (Bound) = Disc
6152 or else (Ekind (Entity (Bound)) = E_In_Parameter
6153 and then Discriminal_Link (Entity (Bound)) = Disc)
6154 then
6155 Rewrite (Bound, New_Copy_Tree (Expr_Disc));
6156 end if;
6157 end Rewrite_Bound;
6158
6159 -- Local variables
6160
6161 Low, High : Node_Id;
6162 Disc : Entity_Id;
6163 Expr_Disc : Elmt_Id;
6164
6165 -- Start of processing for Rewrite_Range
6166
6167 begin
6168 if Has_Discriminants (Root_Type) and then Nkind (Rge) = N_Range then
6169 Low := Low_Bound (Rge);
6170 High := High_Bound (Rge);
6171
6172 Disc := First_Discriminant (Root_Type);
6173 Expr_Disc := First_Elmt (Stored_Constraint (Etype (N)));
6174 while Present (Disc) loop
6175 Rewrite_Bound (Low, Disc, Node (Expr_Disc));
6176 Rewrite_Bound (High, Disc, Node (Expr_Disc));
6177 Next_Discriminant (Disc);
6178 Next_Elmt (Expr_Disc);
6179 end loop;
6180 end if;
6181 end Rewrite_Range;
6182
6183 -- Local variables
6184
6185 Components : constant Elist_Id := New_Elmt_List;
6186 -- Components is the list of the record components whose value must be
6187 -- provided in the aggregate. This list does include discriminants.
6188
6189 Component : Entity_Id;
6190 Component_Elmt : Elmt_Id;
6191 Expr : Node_Id;
6192 Positional_Expr : Node_Id;
6193
6194 -- Start of processing for Resolve_Record_Aggregate
6195
6196 begin
6197 -- A record aggregate is restricted in SPARK:
6198
6199 -- Each named association can have only a single choice.
6200 -- OTHERS cannot be used.
6201 -- Positional and named associations cannot be mixed.
6202
6203 if Present (Component_Associations (N)) then
6204 declare
6205 Assoc : Node_Id;
6206
6207 begin
6208 Assoc := First (Component_Associations (N));
6209 while Present (Assoc) loop
6210 if Nkind (Assoc) = N_Iterated_Component_Association then
6211 Error_Msg_N
6212 ("iterated component association can only appear in an "
6213 & "array aggregate", N);
6214 raise Unrecoverable_Error;
6215 end if;
6216
6217 Next (Assoc);
6218 end loop;
6219 end;
6220 end if;
6221
6222 -- We may end up calling Duplicate_Subexpr on expressions that are
6223 -- attached to New_Assoc_List. For this reason we need to attach it
6224 -- to the tree by setting its parent pointer to N. This parent point
6225 -- will change in STEP 8 below.
6226
6227 Set_Parent (New_Assoc_List, N);
6228
6229 -- STEP 1: abstract type and null record verification
6230
6231 if Is_Abstract_Type (Typ) then
6232 Error_Msg_N ("type of aggregate cannot be abstract", N);
6233 end if;
6234
6235 if No (First_Entity (Typ)) and then Null_Record_Present (N) then
6236 Set_Etype (N, Typ);
6237 return;
6238
6239 elsif Present (First_Entity (Typ))
6240 and then Null_Record_Present (N)
6241 and then not Is_Tagged_Type (Typ)
6242 then
6243 Error_Msg_N ("record aggregate cannot be null", N);
6244 return;
6245
6246 -- If the type has no components, then the aggregate should either
6247 -- have "null record", or in Ada 2005 it could instead have a single
6248 -- component association given by "others => <>". For Ada 95 we flag an
6249 -- error at this point, but for Ada 2005 we proceed with checking the
6250 -- associations below, which will catch the case where it's not an
6251 -- aggregate with "others => <>". Note that the legality of a <>
6252 -- aggregate for a null record type was established by AI05-016.
6253
6254 elsif No (First_Entity (Typ))
6255 and then Ada_Version < Ada_2005
6256 then
6257 Error_Msg_N ("record aggregate must be null", N);
6258 return;
6259 end if;
6260
6261 -- STEP 2: Verify aggregate structure
6262
6263 Step_2 : declare
6264 Assoc : Node_Id;
6265 Bad_Aggregate : Boolean := False;
6266 Selector_Name : Node_Id;
6267
6268 begin
6269 if Present (Component_Associations (N)) then
6270 Assoc := First (Component_Associations (N));
6271 else
6272 Assoc := Empty;
6273 end if;
6274
6275 while Present (Assoc) loop
6276 Selector_Name := First (Choices (Assoc));
6277 while Present (Selector_Name) loop
6278 if Nkind (Selector_Name) = N_Identifier then
6279 null;
6280
6281 elsif Nkind (Selector_Name) = N_Others_Choice then
6282 if Selector_Name /= First (Choices (Assoc))
6283 or else Present (Next (Selector_Name))
6284 then
6285 Error_Msg_N
6286 ("OTHERS must appear alone in a choice list",
6287 Selector_Name);
6288 return;
6289
6290 elsif Present (Next (Assoc)) then
6291 Error_Msg_N
6292 ("OTHERS must appear last in an aggregate",
6293 Selector_Name);
6294 return;
6295
6296 -- (Ada 2005): If this is an association with a box,
6297 -- indicate that the association need not represent
6298 -- any component.
6299
6300 elsif Box_Present (Assoc) then
6301 Others_Box := 1;
6302 Box_Node := Assoc;
6303 end if;
6304
6305 else
6306 Error_Msg_N
6307 ("selector name should be identifier or OTHERS",
6308 Selector_Name);
6309 Bad_Aggregate := True;
6310 end if;
6311
6312 Next (Selector_Name);
6313 end loop;
6314
6315 Next (Assoc);
6316 end loop;
6317
6318 if Bad_Aggregate then
6319 return;
6320 end if;
6321 end Step_2;
6322
6323 -- STEP 3: Find discriminant Values
6324
6325 Step_3 : declare
6326 Discrim : Entity_Id;
6327 Missing_Discriminants : Boolean := False;
6328
6329 begin
6330 if Present (Expressions (N)) then
6331 Positional_Expr := First (Expressions (N));
6332 else
6333 Positional_Expr := Empty;
6334 end if;
6335
6336 -- AI05-0115: if the ancestor part is a subtype mark, the ancestor
6337 -- must not have unknown discriminants.
6338 -- ??? We are not checking any subtype mark here and this code is not
6339 -- exercised by any test, so it's likely wrong (in particular
6340 -- we should not use Root_Type here but the subtype mark, if any),
6341 -- and possibly not needed.
6342
6343 if Is_Derived_Type (Typ)
6344 and then Has_Unknown_Discriminants (Root_Type (Typ))
6345 and then Nkind (N) /= N_Extension_Aggregate
6346 then
6347 Error_Msg_NE
6348 ("aggregate not available for type& whose ancestor "
6349 & "has unknown discriminants", N, Typ);
6350 end if;
6351
6352 if Has_Unknown_Discriminants (Typ)
6353 and then Present (Underlying_Record_View (Typ))
6354 then
6355 Discrim := First_Discriminant (Underlying_Record_View (Typ));
6356 elsif Has_Discriminants (Typ) then
6357 Discrim := First_Discriminant (Typ);
6358 else
6359 Discrim := Empty;
6360 end if;
6361
6362 -- First find the discriminant values in the positional components
6363
6364 while Present (Discrim) and then Present (Positional_Expr) loop
6365 if Discriminant_Present (Discrim) then
6366 Resolve_Aggr_Expr (Positional_Expr, Discrim);
6367
6368 -- Ada 2005 (AI-231)
6369
6370 if Ada_Version >= Ada_2005
6371 and then Known_Null (Positional_Expr)
6372 then
6373 Check_Can_Never_Be_Null (Discrim, Positional_Expr);
6374 end if;
6375
6376 Next (Positional_Expr);
6377 end if;
6378
6379 if Present (Get_Value (Discrim, Component_Associations (N))) then
6380 Error_Msg_NE
6381 ("more than one value supplied for discriminant&",
6382 N, Discrim);
6383 end if;
6384
6385 Next_Discriminant (Discrim);
6386 end loop;
6387
6388 -- Find remaining discriminant values if any among named components
6389
6390 while Present (Discrim) loop
6391 Expr := Get_Value (Discrim, Component_Associations (N), True);
6392
6393 if not Discriminant_Present (Discrim) then
6394 if Present (Expr) then
6395 Error_Msg_NE
6396 ("more than one value supplied for discriminant &",
6397 N, Discrim);
6398 end if;
6399
6400 elsif No (Expr) then
6401 Error_Msg_NE
6402 ("no value supplied for discriminant &", N, Discrim);
6403 Missing_Discriminants := True;
6404
6405 else
6406 Resolve_Aggr_Expr (Expr, Discrim);
6407 end if;
6408
6409 Next_Discriminant (Discrim);
6410 end loop;
6411
6412 if Missing_Discriminants then
6413 return;
6414 end if;
6415
6416 -- At this point and until the beginning of STEP 6, New_Assoc_List
6417 -- contains only the discriminants and their values.
6418
6419 end Step_3;
6420
6421 -- STEP 4: Set the Etype of the record aggregate
6422
6423 if Has_Discriminants (Typ)
6424 or else (Has_Unknown_Discriminants (Typ)
6425 and then Present (Underlying_Record_View (Typ)))
6426 then
6427 Build_Constrained_Itype (N, Typ, New_Assoc_List);
6428 else
6429 Set_Etype (N, Typ);
6430 end if;
6431
6432 -- STEP 5: Get remaining components according to discriminant values
6433
6434 Step_5 : declare
6435 Dnode : Node_Id;
6436 Errors_Found : Boolean := False;
6437 Record_Def : Node_Id;
6438 Parent_Typ : Entity_Id;
6439 Parent_Typ_List : Elist_Id;
6440 Parent_Elmt : Elmt_Id;
6441 Root_Typ : Entity_Id;
6442
6443 begin
6444 if Is_Derived_Type (Typ) and then Is_Tagged_Type (Typ) then
6445 Parent_Typ_List := New_Elmt_List;
6446
6447 -- If this is an extension aggregate, the component list must
6448 -- include all components that are not in the given ancestor type.
6449 -- Otherwise, the component list must include components of all
6450 -- ancestors, starting with the root.
6451
6452 if Nkind (N) = N_Extension_Aggregate then
6453 Root_Typ := Base_Type (Etype (Ancestor_Part (N)));
6454
6455 else
6456 -- AI05-0115: check legality of aggregate for type with a
6457 -- private ancestor.
6458
6459 Root_Typ := Root_Type (Typ);
6460 if Has_Private_Ancestor (Typ) then
6461 declare
6462 Ancestor : constant Entity_Id :=
6463 Find_Private_Ancestor (Typ);
6464 Ancestor_Unit : constant Entity_Id :=
6465 Cunit_Entity
6466 (Get_Source_Unit (Ancestor));
6467 Parent_Unit : constant Entity_Id :=
6468 Cunit_Entity (Get_Source_Unit
6469 (Base_Type (Etype (Ancestor))));
6470 begin
6471 -- Check whether we are in a scope that has full view
6472 -- over the private ancestor and its parent. This can
6473 -- only happen if the derivation takes place in a child
6474 -- unit of the unit that declares the parent, and we are
6475 -- in the private part or body of that child unit, else
6476 -- the aggregate is illegal.
6477
6478 if Is_Child_Unit (Ancestor_Unit)
6479 and then Scope (Ancestor_Unit) = Parent_Unit
6480 and then In_Open_Scopes (Scope (Ancestor))
6481 and then
6482 (In_Private_Part (Scope (Ancestor))
6483 or else In_Package_Body (Scope (Ancestor)))
6484 then
6485 null;
6486
6487 else
6488 Error_Msg_NE
6489 ("type of aggregate has private ancestor&!",
6490 N, Root_Typ);
6491 Error_Msg_N ("must use extension aggregate!", N);
6492 return;
6493 end if;
6494 end;
6495 end if;
6496
6497 Dnode := Declaration_Node (Base_Type (Root_Typ));
6498
6499 -- If we don't get a full declaration, then we have some error
6500 -- which will get signalled later so skip this part. Otherwise
6501 -- gather components of root that apply to the aggregate type.
6502 -- We use the base type in case there is an applicable stored
6503 -- constraint that renames the discriminants of the root.
6504
6505 if Nkind (Dnode) = N_Full_Type_Declaration then
6506 Record_Def := Type_Definition (Dnode);
6507 Gather_Components
6508 (Base_Type (Typ),
6509 Component_List (Record_Def),
6510 Governed_By => New_Assoc_List,
6511 Into => Components,
6512 Report_Errors => Errors_Found);
6513
6514 if Errors_Found then
6515 Error_Msg_N
6516 ("discriminant controlling variant part is not static",
6517 N);
6518 return;
6519 end if;
6520 end if;
6521 end if;
6522
6523 Parent_Typ := Base_Type (Typ);
6524 while Parent_Typ /= Root_Typ loop
6525 Prepend_Elmt (Parent_Typ, To => Parent_Typ_List);
6526 Parent_Typ := Etype (Parent_Typ);
6527
6528 -- Check whether a private parent requires the use of
6529 -- an extension aggregate.
6530
6531 if Nkind (Parent (Base_Type (Parent_Typ))) =
6532 N_Private_Type_Declaration
6533 or else Nkind (Parent (Base_Type (Parent_Typ))) =
6534 N_Private_Extension_Declaration
6535 then
6536 if Nkind (N) /= N_Extension_Aggregate then
6537 Error_Msg_NE
6538 ("type of aggregate has private ancestor&!",
6539 N, Parent_Typ);
6540 Error_Msg_N ("must use extension aggregate!", N);
6541 return;
6542
6543 elsif Parent_Typ /= Root_Typ then
6544 Error_Msg_NE
6545 ("ancestor part of aggregate must be private type&",
6546 Ancestor_Part (N), Parent_Typ);
6547 return;
6548 end if;
6549
6550 -- The current view of ancestor part may be a private type,
6551 -- while the context type is always non-private.
6552
6553 elsif Is_Private_Type (Root_Typ)
6554 and then Present (Full_View (Root_Typ))
6555 and then Nkind (N) = N_Extension_Aggregate
6556 then
6557 exit when Base_Type (Full_View (Root_Typ)) = Parent_Typ;
6558 end if;
6559 end loop;
6560
6561 -- Now collect components from all other ancestors, beginning
6562 -- with the current type. If the type has unknown discriminants
6563 -- use the component list of the Underlying_Record_View, which
6564 -- needs to be used for the subsequent expansion of the aggregate
6565 -- into assignments.
6566
6567 Parent_Elmt := First_Elmt (Parent_Typ_List);
6568 while Present (Parent_Elmt) loop
6569 Parent_Typ := Node (Parent_Elmt);
6570
6571 if Has_Unknown_Discriminants (Parent_Typ)
6572 and then Present (Underlying_Record_View (Typ))
6573 then
6574 Parent_Typ := Underlying_Record_View (Parent_Typ);
6575 end if;
6576
6577 Record_Def := Type_Definition (Parent (Base_Type (Parent_Typ)));
6578 Gather_Components (Parent_Typ,
6579 Component_List (Record_Extension_Part (Record_Def)),
6580 Governed_By => New_Assoc_List,
6581 Into => Components,
6582 Report_Errors => Errors_Found);
6583
6584 Next_Elmt (Parent_Elmt);
6585 end loop;
6586
6587 -- Typ is not a derived tagged type
6588
6589 elsif Nkind (Parent (Base_Type (Typ))) = N_Full_Type_Declaration then
6590 Record_Def := Type_Definition (Parent (Base_Type (Typ)));
6591
6592 if Null_Present (Record_Def) then
6593 null;
6594
6595 elsif not Has_Unknown_Discriminants (Typ) then
6596 Gather_Components
6597 (Base_Type (Typ),
6598 Component_List (Record_Def),
6599 Governed_By => New_Assoc_List,
6600 Into => Components,
6601 Report_Errors => Errors_Found);
6602
6603 else
6604 Gather_Components
6605 (Base_Type (Underlying_Record_View (Typ)),
6606 Component_List (Record_Def),
6607 Governed_By => New_Assoc_List,
6608 Into => Components,
6609 Report_Errors => Errors_Found);
6610 end if;
6611 end if;
6612
6613 if Errors_Found then
6614 return;
6615 end if;
6616 end Step_5;
6617
6618 -- STEP 6: Find component Values
6619
6620 Component_Elmt := First_Elmt (Components);
6621
6622 -- First scan the remaining positional associations in the aggregate.
6623 -- Remember that at this point Positional_Expr contains the current
6624 -- positional association if any is left after looking for discriminant
6625 -- values in step 3.
6626
6627 while Present (Positional_Expr) and then Present (Component_Elmt) loop
6628 Component := Node (Component_Elmt);
6629 Resolve_Aggr_Expr (Positional_Expr, Component);
6630
6631 -- Ada 2005 (AI-231)
6632
6633 if Ada_Version >= Ada_2005 and then Known_Null (Positional_Expr) then
6634 Check_Can_Never_Be_Null (Component, Positional_Expr);
6635 end if;
6636
6637 if Present (Get_Value (Component, Component_Associations (N))) then
6638 Error_Msg_NE
6639 ("more than one value supplied for component &", N, Component);
6640 end if;
6641
6642 Next (Positional_Expr);
6643 Next_Elmt (Component_Elmt);
6644 end loop;
6645
6646 if Present (Positional_Expr) then
6647 Error_Msg_N
6648 ("too many components for record aggregate", Positional_Expr);
6649 end if;
6650
6651 -- Now scan for the named arguments of the aggregate
6652
6653 while Present (Component_Elmt) loop
6654 Component := Node (Component_Elmt);
6655 Expr := Get_Value (Component, Component_Associations (N), True);
6656
6657 -- Note: The previous call to Get_Value sets the value of the
6658 -- variable Is_Box_Present.
6659
6660 -- Ada 2005 (AI-287): Handle components with default initialization.
6661 -- Note: This feature was originally added to Ada 2005 for limited
6662 -- but it was finally allowed with any type.
6663
6664 if Is_Box_Present then
6665 Check_Box_Component : declare
6666 Ctyp : constant Entity_Id := Etype (Component);
6667
6668 begin
6669 -- Initially assume that the box is for a default-initialized
6670 -- component and reset to False in cases where that's not true.
6671
6672 Is_Box_Init_By_Default := True;
6673
6674 -- If there is a default expression for the aggregate, copy
6675 -- it into a new association. This copy must modify the scopes
6676 -- of internal types that may be attached to the expression
6677 -- (e.g. index subtypes of arrays) because in general the type
6678 -- declaration and the aggregate appear in different scopes,
6679 -- and the backend requires the scope of the type to match the
6680 -- point at which it is elaborated.
6681
6682 -- If the component has an initialization procedure (IP) we
6683 -- pass the component to the expander, which will generate
6684 -- the call to such IP.
6685
6686 -- If the component has discriminants, their values must
6687 -- be taken from their subtype. This is indispensable for
6688 -- constraints that are given by the current instance of an
6689 -- enclosing type, to allow the expansion of the aggregate to
6690 -- replace the reference to the current instance by the target
6691 -- object of the aggregate.
6692
6693 if Is_Case_Choice_Pattern (N) then
6694
6695 -- Do not transform box component values in a case-choice
6696 -- aggregate.
6697
6698 Add_Association
6699 (Component => Component,
6700 Expr => Empty,
6701 Assoc_List => New_Assoc_List,
6702 Is_Box_Present => True);
6703
6704 elsif Present (Parent (Component))
6705 and then Nkind (Parent (Component)) = N_Component_Declaration
6706 and then Present (Expression (Parent (Component)))
6707 then
6708 -- If component declaration has an initialization expression
6709 -- then this is not a case of default initialization.
6710
6711 Is_Box_Init_By_Default := False;
6712
6713 Expr :=
6714 New_Copy_Tree_And_Copy_Dimensions
6715 (Expression (Parent (Component)),
6716 New_Scope => Current_Scope,
6717 New_Sloc => Sloc (N));
6718
6719 -- As the type of the copied default expression may refer
6720 -- to discriminants of the record type declaration, these
6721 -- non-stored discriminants need to be rewritten into stored
6722 -- discriminant values for the aggregate. This is required
6723 -- in GNATprove mode, and is adopted in all modes to avoid
6724 -- special-casing GNATprove mode.
6725
6726 if Is_Array_Type (Etype (Expr)) then
6727 declare
6728 Rec_Typ : constant Entity_Id := Scope (Component);
6729 -- Root record type whose discriminants may be used as
6730 -- bounds in range nodes.
6731
6732 Assoc : Node_Id;
6733 Choice : Node_Id;
6734 Index : Node_Id;
6735
6736 begin
6737 -- Rewrite the range nodes occurring in the indexes
6738 -- and their types.
6739
6740 Index := First_Index (Etype (Expr));
6741 while Present (Index) loop
6742 Rewrite_Range (Rec_Typ, Index);
6743 Rewrite_Range
6744 (Rec_Typ, Scalar_Range (Etype (Index)));
6745
6746 Next_Index (Index);
6747 end loop;
6748
6749 -- Rewrite the range nodes occurring as aggregate
6750 -- bounds and component associations.
6751
6752 if Nkind (Expr) = N_Aggregate then
6753 if Present (Aggregate_Bounds (Expr)) then
6754 Rewrite_Range (Rec_Typ, Aggregate_Bounds (Expr));
6755 end if;
6756
6757 if Present (Component_Associations (Expr)) then
6758 Assoc := First (Component_Associations (Expr));
6759 while Present (Assoc) loop
6760 Choice := First (Choices (Assoc));
6761 while Present (Choice) loop
6762 Rewrite_Range (Rec_Typ, Choice);
6763
6764 Next (Choice);
6765 end loop;
6766
6767 Next (Assoc);
6768 end loop;
6769 end if;
6770 end if;
6771 end;
6772 end if;
6773
6774 Add_Association
6775 (Component => Component,
6776 Expr => Expr,
6777 Assoc_List => New_Assoc_List);
6778 Set_Has_Self_Reference (N);
6779
6780 elsif Needs_Simple_Initialization (Ctyp)
6781 or else Has_Non_Null_Base_Init_Proc (Ctyp)
6782 or else not Expander_Active
6783 then
6784 Add_Association
6785 (Component => Component,
6786 Expr => Empty,
6787 Assoc_List => New_Assoc_List,
6788 Is_Box_Present => True);
6789
6790 -- Otherwise we only need to resolve the expression if the
6791 -- component has partially initialized values (required to
6792 -- expand the corresponding assignments and run-time checks).
6793
6794 elsif Present (Expr)
6795 and then Is_Partially_Initialized_Type (Ctyp)
6796 then
6797 Resolve_Aggr_Expr (Expr, Component);
6798 end if;
6799 end Check_Box_Component;
6800
6801 elsif No (Expr) then
6802
6803 -- Ignore hidden components associated with the position of the
6804 -- interface tags: these are initialized dynamically.
6805
6806 if No (Related_Type (Component)) then
6807 Error_Msg_NE
6808 ("no value supplied for component &!", N, Component);
6809 end if;
6810
6811 else
6812 Resolve_Aggr_Expr (Expr, Component);
6813 end if;
6814
6815 Next_Elmt (Component_Elmt);
6816 end loop;
6817
6818 -- STEP 7: check for invalid components + check type in choice list
6819
6820 Step_7 : declare
6821 Assoc : Node_Id;
6822 New_Assoc : Node_Id;
6823
6824 Selectr : Node_Id;
6825 -- Selector name
6826
6827 Typech : Entity_Id;
6828 -- Type of first component in choice list
6829
6830 begin
6831 if Present (Component_Associations (N)) then
6832 Assoc := First (Component_Associations (N));
6833 else
6834 Assoc := Empty;
6835 end if;
6836
6837 Verification : while Present (Assoc) loop
6838 Selectr := First (Choices (Assoc));
6839 Typech := Empty;
6840
6841 if Nkind (Selectr) = N_Others_Choice then
6842
6843 -- Ada 2005 (AI-287): others choice may have expression or box
6844
6845 if No (Others_Etype) and then Others_Box = 0 then
6846 Error_Msg_N
6847 ("OTHERS must represent at least one component", Selectr);
6848
6849 elsif Others_Box = 1 and then Warn_On_Redundant_Constructs then
6850 Error_Msg_N ("OTHERS choice is redundant?r?", Box_Node);
6851 Error_Msg_N
6852 ("\previous choices cover all components?r?", Box_Node);
6853 end if;
6854
6855 exit Verification;
6856 end if;
6857
6858 while Present (Selectr) loop
6859 Component := Empty;
6860 New_Assoc := First (New_Assoc_List);
6861 while Present (New_Assoc) loop
6862 Component := First (Choices (New_Assoc));
6863
6864 if Chars (Selectr) = Chars (Component) then
6865 if Style_Check then
6866 Check_Identifier (Selectr, Entity (Component));
6867 end if;
6868
6869 exit;
6870 end if;
6871
6872 Next (New_Assoc);
6873 end loop;
6874
6875 -- If we found an association, then this is a legal component
6876 -- of the type in question.
6877
6878 pragma Assert (if Present (New_Assoc) then Present (Component));
6879
6880 -- If no association, this is not a legal component of the type
6881 -- in question, unless its association is provided with a box.
6882
6883 if No (New_Assoc) then
6884 if Box_Present (Parent (Selectr)) then
6885
6886 -- This may still be a bogus component with a box. Scan
6887 -- list of components to verify that a component with
6888 -- that name exists.
6889
6890 declare
6891 C : Entity_Id;
6892
6893 begin
6894 C := First_Component (Typ);
6895 while Present (C) loop
6896 if Chars (C) = Chars (Selectr) then
6897
6898 -- If the context is an extension aggregate,
6899 -- the component must not be inherited from
6900 -- the ancestor part of the aggregate.
6901
6902 if Nkind (N) /= N_Extension_Aggregate
6903 or else
6904 Scope (Original_Record_Component (C)) /=
6905 Etype (Ancestor_Part (N))
6906 then
6907 exit;
6908 end if;
6909 end if;
6910
6911 Next_Component (C);
6912 end loop;
6913
6914 if No (C) then
6915 Error_Msg_Node_2 := Typ;
6916 Error_Msg_N ("& is not a component of}", Selectr);
6917 end if;
6918 end;
6919
6920 elsif Chars (Selectr) /= Name_uTag
6921 and then Chars (Selectr) /= Name_uParent
6922 then
6923 if not Has_Discriminants (Typ) then
6924 Error_Msg_Node_2 := Typ;
6925 Error_Msg_N ("& is not a component of}", Selectr);
6926 else
6927 Error_Msg_N
6928 ("& is not a component of the aggregate subtype",
6929 Selectr);
6930 end if;
6931
6932 Check_Misspelled_Component (Components, Selectr);
6933 end if;
6934
6935 elsif No (Typech) then
6936 Typech := Base_Type (Etype (Component));
6937
6938 -- AI05-0199: In Ada 2012, several components of anonymous
6939 -- access types can appear in a choice list, as long as the
6940 -- designated types match.
6941
6942 elsif Typech /= Base_Type (Etype (Component)) then
6943 if Ada_Version >= Ada_2012
6944 and then Ekind (Typech) = E_Anonymous_Access_Type
6945 and then
6946 Ekind (Etype (Component)) = E_Anonymous_Access_Type
6947 and then Base_Type (Designated_Type (Typech)) =
6948 Base_Type (Designated_Type (Etype (Component)))
6949 and then
6950 Subtypes_Statically_Match (Typech, (Etype (Component)))
6951 then
6952 null;
6953
6954 elsif not Box_Present (Parent (Selectr)) then
6955 Error_Msg_N
6956 ("components in choice list must have same type",
6957 Selectr);
6958 end if;
6959 end if;
6960
6961 Next (Selectr);
6962 end loop;
6963
6964 Next (Assoc);
6965 end loop Verification;
6966 end Step_7;
6967
6968 -- STEP 8: replace the original aggregate
6969
6970 Step_8 : declare
6971 New_Aggregate : constant Node_Id := New_Copy (N);
6972
6973 begin
6974 Set_Expressions (New_Aggregate, No_List);
6975 Set_Etype (New_Aggregate, Etype (N));
6976 Set_Component_Associations (New_Aggregate, New_Assoc_List);
6977 Set_Check_Actuals (New_Aggregate, Check_Actuals (N));
6978
6979 Rewrite (N, New_Aggregate);
6980 end Step_8;
6981
6982 -- Check the dimensions of the components in the record aggregate
6983
6984 Analyze_Dimension_Extension_Or_Record_Aggregate (N);
6985 end Resolve_Record_Aggregate;
6986
6987 -----------------------------
6988 -- Check_Can_Never_Be_Null --
6989 -----------------------------
6990
6991 procedure Check_Can_Never_Be_Null (Typ : Entity_Id; Expr : Node_Id) is
6992 Comp_Typ : Entity_Id;
6993
6994 begin
6995 pragma Assert
6996 (Ada_Version >= Ada_2005
6997 and then Present (Expr)
6998 and then Known_Null (Expr));
6999
7000 case Ekind (Typ) is
7001 when E_Array_Type =>
7002 Comp_Typ := Component_Type (Typ);
7003
7004 when E_Component
7005 | E_Discriminant
7006 =>
7007 Comp_Typ := Etype (Typ);
7008
7009 when others =>
7010 return;
7011 end case;
7012
7013 if Can_Never_Be_Null (Comp_Typ) then
7014
7015 -- Here we know we have a constraint error. Note that we do not use
7016 -- Apply_Compile_Time_Constraint_Error here to the Expr, which might
7017 -- seem the more natural approach. That's because in some cases the
7018 -- components are rewritten, and the replacement would be missed.
7019 -- We do not mark the whole aggregate as raising a constraint error,
7020 -- because the association may be a null array range.
7021
7022 Error_Msg_N
7023 ("(Ada 2005) NULL not allowed in null-excluding component??", Expr);
7024 Error_Msg_N
7025 ("\Constraint_Error will be raised at run time??", Expr);
7026
7027 Rewrite (Expr,
7028 Make_Raise_Constraint_Error
7029 (Sloc (Expr), Reason => CE_Access_Check_Failed));
7030 Set_Etype (Expr, Comp_Typ);
7031 Set_Analyzed (Expr);
7032 end if;
7033 end Check_Can_Never_Be_Null;
7034
7035 ---------------------
7036 -- Sort_Case_Table --
7037 ---------------------
7038
7039 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is
7040 U : constant Int := Case_Table'Last;
7041 K : Int;
7042 J : Int;
7043 T : Case_Bounds;
7044
7045 begin
7046 K := 1;
7047 while K < U loop
7048 T := Case_Table (K + 1);
7049
7050 J := K + 1;
7051 while J > 1
7052 and then Expr_Value (Case_Table (J - 1).Lo) > Expr_Value (T.Lo)
7053 loop
7054 Case_Table (J) := Case_Table (J - 1);
7055 J := J - 1;
7056 end loop;
7057
7058 Case_Table (J) := T;
7059 K := K + 1;
7060 end loop;
7061 end Sort_Case_Table;
7062
7063 end Sem_Aggr;