[Ada] Remove SPARK-specific expansion of array aggregates
[gcc.git] / gcc / ada / sem_aggr.adb
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-2020, 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 Einfo; use Einfo;
30 with Elists; use Elists;
31 with Errout; use Errout;
32 with Expander; use Expander;
33 with Exp_Ch6; use Exp_Ch6;
34 with Exp_Tss; use Exp_Tss;
35 with Exp_Util; use Exp_Util;
36 with Freeze; use Freeze;
37 with Itypes; use Itypes;
38 with Lib; use Lib;
39 with Lib.Xref; use Lib.Xref;
40 with Namet; use Namet;
41 with Namet.Sp; use Namet.Sp;
42 with Nmake; use Nmake;
43 with Nlists; use Nlists;
44 with Opt; use Opt;
45 with Restrict; use Restrict;
46 with Rident; use Rident;
47 with Sem; use Sem;
48 with Sem_Aux; use Sem_Aux;
49 with Sem_Cat; use Sem_Cat;
50 with Sem_Ch3; use Sem_Ch3;
51 with Sem_Ch5; use Sem_Ch5;
52 with Sem_Ch8; use Sem_Ch8;
53 with Sem_Ch13; use Sem_Ch13;
54 with Sem_Dim; use Sem_Dim;
55 with Sem_Eval; use Sem_Eval;
56 with Sem_Res; use Sem_Res;
57 with Sem_Util; use Sem_Util;
58 with Sem_Type; use Sem_Type;
59 with Sem_Warn; use Sem_Warn;
60 with Sinfo; use Sinfo;
61 with Snames; use Snames;
62 with Stringt; use Stringt;
63 with Stand; use Stand;
64 with Style; use Style;
65 with Targparm; use Targparm;
66 with Tbuild; use Tbuild;
67 with Ttypes; use Ttypes;
68 with Uintp; use Uintp;
69
70 package body Sem_Aggr is
71
72 type Case_Bounds is record
73 Lo : Node_Id;
74 -- Low bound of choice. Once we sort the Case_Table, then entries
75 -- will be in order of ascending Choice_Lo values.
76
77 Hi : Node_Id;
78 -- High Bound of choice. The sort does not pay any attention to the
79 -- high bound, so choices 1 .. 4 and 1 .. 5 could be in either order.
80
81 Highest : Uint;
82 -- If there are duplicates or missing entries, then in the sorted
83 -- table, this records the highest value among Choice_Hi values
84 -- seen so far, including this entry.
85
86 Choice : Node_Id;
87 -- The node of the choice
88 end record;
89
90 type Case_Table_Type is array (Pos range <>) of Case_Bounds;
91 -- Table type used by Check_Case_Choices procedure
92
93 -----------------------
94 -- Local Subprograms --
95 -----------------------
96
97 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type);
98 -- Sort the Case Table using the Lower Bound of each Choice as the key. A
99 -- simple insertion sort is used since the choices in a case statement will
100 -- usually be in near sorted order.
101
102 procedure Check_Can_Never_Be_Null (Typ : Entity_Id; Expr : Node_Id);
103 -- Ada 2005 (AI-231): Check bad usage of null for a component for which
104 -- null exclusion (NOT NULL) is specified. Typ can be an E_Array_Type for
105 -- the array case (the component type of the array will be used) or an
106 -- E_Component/E_Discriminant entity in the record case, in which case the
107 -- type of the component will be used for the test. If Typ is any other
108 -- kind of entity, the call is ignored. Expr is the component node in the
109 -- aggregate which is known to have a null value. A warning message will be
110 -- issued if the component is null excluding.
111 --
112 -- It would be better to pass the proper type for Typ ???
113
114 procedure Check_Expr_OK_In_Limited_Aggregate (Expr : Node_Id);
115 -- Check that Expr is either not limited or else is one of the cases of
116 -- expressions allowed for a limited component association (namely, an
117 -- aggregate, function call, or <> notation). Report error for violations.
118 -- Expression is also OK in an instance or inlining context, because we
119 -- have already preanalyzed and it is known to be type correct.
120
121 ------------------------------------------------------
122 -- Subprograms used for RECORD AGGREGATE Processing --
123 ------------------------------------------------------
124
125 procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id);
126 -- This procedure performs all the semantic checks required for record
127 -- aggregates. Note that for aggregates analysis and resolution go
128 -- hand in hand. Aggregate analysis has been delayed up to here and
129 -- it is done while resolving the aggregate.
130 --
131 -- N is the N_Aggregate node.
132 -- Typ is the record type for the aggregate resolution
133 --
134 -- While performing the semantic checks, this procedure builds a new
135 -- Component_Association_List where each record field appears alone in a
136 -- Component_Choice_List along with its corresponding expression. The
137 -- record fields in the Component_Association_List appear in the same order
138 -- in which they appear in the record type Typ.
139 --
140 -- Once this new Component_Association_List is built and all the semantic
141 -- checks performed, the original aggregate subtree is replaced with the
142 -- new named record aggregate just built. This new record aggregate has no
143 -- positional associations, so its Expressions field is set to No_List.
144 -- Note that subtree substitution is performed with Rewrite so as to be
145 -- able to retrieve the original aggregate.
146 --
147 -- The aggregate subtree manipulation performed by Resolve_Record_Aggregate
148 -- yields the aggregate format expected by Gigi. Typically, this kind of
149 -- tree manipulations are done in the expander. However, because the
150 -- semantic checks that need to be performed on record aggregates really go
151 -- hand in hand with the record aggregate normalization, the aggregate
152 -- subtree transformation is performed during resolution rather than
153 -- expansion. Had we decided otherwise we would have had to duplicate most
154 -- of the code in the expansion procedure Expand_Record_Aggregate. Note,
155 -- however, that all the expansion concerning aggregates for tagged records
156 -- is done in Expand_Record_Aggregate.
157 --
158 -- The algorithm of Resolve_Record_Aggregate proceeds as follows:
159 --
160 -- 1. Make sure that the record type against which the record aggregate
161 -- has to be resolved is not abstract. Furthermore if the type is a
162 -- null aggregate make sure the input aggregate N is also null.
163 --
164 -- 2. Verify that the structure of the aggregate is that of a record
165 -- aggregate. Specifically, look for component associations and ensure
166 -- that each choice list only has identifiers or the N_Others_Choice
167 -- node. Also make sure that if present, the N_Others_Choice occurs
168 -- last and by itself.
169 --
170 -- 3. If Typ contains discriminants, the values for each discriminant is
171 -- looked for. If the record type Typ has variants, we check that the
172 -- expressions corresponding to each discriminant ruling the (possibly
173 -- nested) variant parts of Typ, are static. This allows us to determine
174 -- the variant parts to which the rest of the aggregate must conform.
175 -- The names of discriminants with their values are saved in a new
176 -- association list, New_Assoc_List which is later augmented with the
177 -- names and values of the remaining components in the record type.
178 --
179 -- During this phase we also make sure that every discriminant is
180 -- assigned exactly one value. Note that when several values for a given
181 -- discriminant are found, semantic processing continues looking for
182 -- further errors. In this case it's the first discriminant value found
183 -- which we will be recorded.
184 --
185 -- IMPORTANT NOTE: For derived tagged types this procedure expects
186 -- First_Discriminant and Next_Discriminant to give the correct list
187 -- of discriminants, in the correct order.
188 --
189 -- 4. After all the discriminant values have been gathered, we can set the
190 -- Etype of the record aggregate. If Typ contains no discriminants this
191 -- is straightforward: the Etype of N is just Typ, otherwise a new
192 -- implicit constrained subtype of Typ is built to be the Etype of N.
193 --
194 -- 5. Gather the remaining record components according to the discriminant
195 -- values. This involves recursively traversing the record type
196 -- structure to see what variants are selected by the given discriminant
197 -- values. This processing is a little more convoluted if Typ is a
198 -- derived tagged types since we need to retrieve the record structure
199 -- of all the ancestors of Typ.
200 --
201 -- 6. After gathering the record components we look for their values in the
202 -- record aggregate and emit appropriate error messages should we not
203 -- find such values or should they be duplicated.
204 --
205 -- 7. We then make sure no illegal component names appear in the record
206 -- aggregate and make sure that the type of the record components
207 -- appearing in a same choice list is the same. Finally we ensure that
208 -- the others choice, if present, is used to provide the value of at
209 -- least a record component.
210 --
211 -- 8. The original aggregate node is replaced with the new named aggregate
212 -- built in steps 3 through 6, as explained earlier.
213 --
214 -- Given the complexity of record aggregate resolution, the primary goal of
215 -- this routine is clarity and simplicity rather than execution and storage
216 -- efficiency. If there are only positional components in the aggregate the
217 -- running time is linear. If there are associations the running time is
218 -- still linear as long as the order of the associations is not too far off
219 -- the order of the components in the record type. If this is not the case
220 -- the running time is at worst quadratic in the size of the association
221 -- list.
222
223 procedure Check_Misspelled_Component
224 (Elements : Elist_Id;
225 Component : Node_Id);
226 -- Give possible misspelling diagnostic if Component is likely to be a
227 -- misspelling of one of the components of the Assoc_List. This is called
228 -- by Resolve_Aggr_Expr after producing an invalid component error message.
229
230 -----------------------------------------------------
231 -- Subprograms used for ARRAY AGGREGATE Processing --
232 -----------------------------------------------------
233
234 function Resolve_Array_Aggregate
235 (N : Node_Id;
236 Index : Node_Id;
237 Index_Constr : Node_Id;
238 Component_Typ : Entity_Id;
239 Others_Allowed : Boolean) return Boolean;
240 -- This procedure performs the semantic checks for an array aggregate.
241 -- True is returned if the aggregate resolution succeeds.
242 --
243 -- The procedure works by recursively checking each nested aggregate.
244 -- Specifically, after checking a sub-aggregate nested at the i-th level
245 -- we recursively check all the subaggregates at the i+1-st level (if any).
246 -- Note that for aggregates analysis and resolution go hand in hand.
247 -- Aggregate analysis has been delayed up to here and it is done while
248 -- resolving the aggregate.
249 --
250 -- N is the current N_Aggregate node to be checked.
251 --
252 -- Index is the index node corresponding to the array sub-aggregate that
253 -- we are currently checking (RM 4.3.3 (8)). Its Etype is the
254 -- corresponding index type (or subtype).
255 --
256 -- Index_Constr is the node giving the applicable index constraint if
257 -- any (RM 4.3.3 (10)). It "is a constraint provided by certain
258 -- contexts [...] that can be used to determine the bounds of the array
259 -- value specified by the aggregate". If Others_Allowed below is False
260 -- there is no applicable index constraint and this node is set to Index.
261 --
262 -- Component_Typ is the array component type.
263 --
264 -- Others_Allowed indicates whether an others choice is allowed
265 -- in the context where the top-level aggregate appeared.
266 --
267 -- The algorithm of Resolve_Array_Aggregate proceeds as follows:
268 --
269 -- 1. Make sure that the others choice, if present, is by itself and
270 -- appears last in the sub-aggregate. Check that we do not have
271 -- positional and named components in the array sub-aggregate (unless
272 -- the named association is an others choice). Finally if an others
273 -- choice is present, make sure it is allowed in the aggregate context.
274 --
275 -- 2. If the array sub-aggregate contains discrete_choices:
276 --
277 -- (A) Verify their validity. Specifically verify that:
278 --
279 -- (a) If a null range is present it must be the only possible
280 -- choice in the array aggregate.
281 --
282 -- (b) Ditto for a non static range.
283 --
284 -- (c) Ditto for a non static expression.
285 --
286 -- In addition this step analyzes and resolves each discrete_choice,
287 -- making sure that its type is the type of the corresponding Index.
288 -- If we are not at the lowest array aggregate level (in the case of
289 -- multi-dimensional aggregates) then invoke Resolve_Array_Aggregate
290 -- recursively on each component expression. Otherwise, resolve the
291 -- bottom level component expressions against the expected component
292 -- type ONLY IF the component corresponds to a single discrete choice
293 -- which is not an others choice (to see why read the DELAYED
294 -- COMPONENT RESOLUTION below).
295 --
296 -- (B) Determine the bounds of the sub-aggregate and lowest and
297 -- highest choice values.
298 --
299 -- 3. For positional aggregates:
300 --
301 -- (A) Loop over the component expressions either recursively invoking
302 -- Resolve_Array_Aggregate on each of these for multi-dimensional
303 -- array aggregates or resolving the bottom level component
304 -- expressions against the expected component type.
305 --
306 -- (B) Determine the bounds of the positional sub-aggregates.
307 --
308 -- 4. Try to determine statically whether the evaluation of the array
309 -- sub-aggregate raises Constraint_Error. If yes emit proper
310 -- warnings. The precise checks are the following:
311 --
312 -- (A) Check that the index range defined by aggregate bounds is
313 -- compatible with corresponding index subtype.
314 -- We also check against the base type. In fact it could be that
315 -- Low/High bounds of the base type are static whereas those of
316 -- the index subtype are not. Thus if we can statically catch
317 -- a problem with respect to the base type we are guaranteed
318 -- that the same problem will arise with the index subtype
319 --
320 -- (B) If we are dealing with a named aggregate containing an others
321 -- choice and at least one discrete choice then make sure the range
322 -- specified by the discrete choices does not overflow the
323 -- aggregate bounds. We also check against the index type and base
324 -- type bounds for the same reasons given in (A).
325 --
326 -- (C) If we are dealing with a positional aggregate with an others
327 -- choice make sure the number of positional elements specified
328 -- does not overflow the aggregate bounds. We also check against
329 -- the index type and base type bounds as mentioned in (A).
330 --
331 -- Finally construct an N_Range node giving the sub-aggregate bounds.
332 -- Set the Aggregate_Bounds field of the sub-aggregate to be this
333 -- N_Range. The routine Array_Aggr_Subtype below uses such N_Ranges
334 -- to build the appropriate aggregate subtype. Aggregate_Bounds
335 -- information is needed during expansion.
336 --
337 -- DELAYED COMPONENT RESOLUTION: The resolution of bottom level component
338 -- expressions in an array aggregate may call Duplicate_Subexpr or some
339 -- other routine that inserts code just outside the outermost aggregate.
340 -- If the array aggregate contains discrete choices or an others choice,
341 -- this may be wrong. Consider for instance the following example.
342 --
343 -- type Rec is record
344 -- V : Integer := 0;
345 -- end record;
346 --
347 -- type Acc_Rec is access Rec;
348 -- Arr : array (1..3) of Acc_Rec := (1 .. 3 => new Rec);
349 --
350 -- Then the transformation of "new Rec" that occurs during resolution
351 -- entails the following code modifications
352 --
353 -- P7b : constant Acc_Rec := new Rec;
354 -- RecIP (P7b.all);
355 -- Arr : array (1..3) of Acc_Rec := (1 .. 3 => P7b);
356 --
357 -- This code transformation is clearly wrong, since we need to call
358 -- "new Rec" for each of the 3 array elements. To avoid this problem we
359 -- delay resolution of the components of non positional array aggregates
360 -- to the expansion phase. As an optimization, if the discrete choice
361 -- specifies a single value we do not delay resolution.
362
363 function Array_Aggr_Subtype (N : Node_Id; Typ : Node_Id) return Entity_Id;
364 -- This routine returns the type or subtype of an array aggregate.
365 --
366 -- N is the array aggregate node whose type we return.
367 --
368 -- Typ is the context type in which N occurs.
369 --
370 -- This routine creates an implicit array subtype whose bounds are
371 -- those defined by the aggregate. When this routine is invoked
372 -- Resolve_Array_Aggregate has already processed aggregate N. Thus the
373 -- Aggregate_Bounds of each sub-aggregate, is an N_Range node giving the
374 -- sub-aggregate bounds. When building the aggregate itype, this function
375 -- traverses the array aggregate N collecting such Aggregate_Bounds and
376 -- constructs the proper array aggregate itype.
377 --
378 -- Note that in the case of multidimensional aggregates each inner
379 -- sub-aggregate corresponding to a given array dimension, may provide a
380 -- different bounds. If it is possible to determine statically that
381 -- some sub-aggregates corresponding to the same index do not have the
382 -- same bounds, then a warning is emitted. If such check is not possible
383 -- statically (because some sub-aggregate bounds are dynamic expressions)
384 -- then this job is left to the expander. In all cases the particular
385 -- bounds that this function will chose for a given dimension is the first
386 -- N_Range node for a sub-aggregate corresponding to that dimension.
387 --
388 -- Note that the Raises_Constraint_Error flag of an array aggregate
389 -- whose evaluation is determined to raise CE by Resolve_Array_Aggregate,
390 -- is set in Resolve_Array_Aggregate but the aggregate is not
391 -- immediately replaced with a raise CE. In fact, Array_Aggr_Subtype must
392 -- first construct the proper itype for the aggregate (Gigi needs
393 -- this). After constructing the proper itype we will eventually replace
394 -- the top-level aggregate with a raise CE (done in Resolve_Aggregate).
395 -- Of course in cases such as:
396 --
397 -- type Arr is array (integer range <>) of Integer;
398 -- A : Arr := (positive range -1 .. 2 => 0);
399 --
400 -- The bounds of the aggregate itype are cooked up to look reasonable
401 -- (in this particular case the bounds will be 1 .. 2).
402
403 procedure Make_String_Into_Aggregate (N : Node_Id);
404 -- A string literal can appear in a context in which a one dimensional
405 -- array of characters is expected. This procedure simply rewrites the
406 -- string as an aggregate, prior to resolution.
407
408 ---------------------------------
409 -- Delta aggregate processing --
410 ---------------------------------
411
412 procedure Resolve_Delta_Array_Aggregate (N : Node_Id; Typ : Entity_Id);
413 procedure Resolve_Delta_Record_Aggregate (N : Node_Id; Typ : Entity_Id);
414
415 ------------------------
416 -- Array_Aggr_Subtype --
417 ------------------------
418
419 function Array_Aggr_Subtype
420 (N : Node_Id;
421 Typ : Entity_Id) return Entity_Id
422 is
423 Aggr_Dimension : constant Pos := Number_Dimensions (Typ);
424 -- Number of aggregate index dimensions
425
426 Aggr_Range : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
427 -- Constrained N_Range of each index dimension in our aggregate itype
428
429 Aggr_Low : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
430 Aggr_High : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty);
431 -- Low and High bounds for each index dimension in our aggregate itype
432
433 Is_Fully_Positional : Boolean := True;
434
435 procedure Collect_Aggr_Bounds (N : Node_Id; Dim : Pos);
436 -- N is an array (sub-)aggregate. Dim is the dimension corresponding
437 -- to (sub-)aggregate N. This procedure collects and removes the side
438 -- effects of the constrained N_Range nodes corresponding to each index
439 -- dimension of our aggregate itype. These N_Range nodes are collected
440 -- in Aggr_Range above.
441 --
442 -- Likewise collect in Aggr_Low & Aggr_High above the low and high
443 -- bounds of each index dimension. If, when collecting, two bounds
444 -- corresponding to the same dimension are static and found to differ,
445 -- then emit a warning, and mark N as raising Constraint_Error.
446
447 -------------------------
448 -- Collect_Aggr_Bounds --
449 -------------------------
450
451 procedure Collect_Aggr_Bounds (N : Node_Id; Dim : Pos) is
452 This_Range : constant Node_Id := Aggregate_Bounds (N);
453 -- The aggregate range node of this specific sub-aggregate
454
455 This_Low : constant Node_Id := Low_Bound (Aggregate_Bounds (N));
456 This_High : constant Node_Id := High_Bound (Aggregate_Bounds (N));
457 -- The aggregate bounds of this specific sub-aggregate
458
459 Assoc : Node_Id;
460 Expr : Node_Id;
461
462 begin
463 Remove_Side_Effects (This_Low, Variable_Ref => True);
464 Remove_Side_Effects (This_High, Variable_Ref => True);
465
466 -- Collect the first N_Range for a given dimension that you find.
467 -- For a given dimension they must be all equal anyway.
468
469 if No (Aggr_Range (Dim)) then
470 Aggr_Low (Dim) := This_Low;
471 Aggr_High (Dim) := This_High;
472 Aggr_Range (Dim) := This_Range;
473
474 else
475 if Compile_Time_Known_Value (This_Low) then
476 if not Compile_Time_Known_Value (Aggr_Low (Dim)) then
477 Aggr_Low (Dim) := This_Low;
478
479 elsif Expr_Value (This_Low) /= Expr_Value (Aggr_Low (Dim)) then
480 Set_Raises_Constraint_Error (N);
481 Error_Msg_Warn := SPARK_Mode /= On;
482 Error_Msg_N ("sub-aggregate low bound mismatch<<", N);
483 Error_Msg_N ("\Constraint_Error [<<", N);
484 end if;
485 end if;
486
487 if Compile_Time_Known_Value (This_High) then
488 if not Compile_Time_Known_Value (Aggr_High (Dim)) then
489 Aggr_High (Dim) := This_High;
490
491 elsif
492 Expr_Value (This_High) /= Expr_Value (Aggr_High (Dim))
493 then
494 Set_Raises_Constraint_Error (N);
495 Error_Msg_Warn := SPARK_Mode /= On;
496 Error_Msg_N ("sub-aggregate high bound mismatch<<", N);
497 Error_Msg_N ("\Constraint_Error [<<", N);
498 end if;
499 end if;
500 end if;
501
502 if Dim < Aggr_Dimension then
503
504 -- Process positional components
505
506 if Present (Expressions (N)) then
507 Expr := First (Expressions (N));
508 while Present (Expr) loop
509 Collect_Aggr_Bounds (Expr, Dim + 1);
510 Next (Expr);
511 end loop;
512 end if;
513
514 -- Process component associations
515
516 if Present (Component_Associations (N)) then
517 Is_Fully_Positional := False;
518
519 Assoc := First (Component_Associations (N));
520 while Present (Assoc) loop
521 Expr := Expression (Assoc);
522 Collect_Aggr_Bounds (Expr, Dim + 1);
523 Next (Assoc);
524 end loop;
525 end if;
526 end if;
527 end Collect_Aggr_Bounds;
528
529 -- Array_Aggr_Subtype variables
530
531 Itype : Entity_Id;
532 -- The final itype of the overall aggregate
533
534 Index_Constraints : constant List_Id := New_List;
535 -- The list of index constraints of the aggregate itype
536
537 -- Start of processing for Array_Aggr_Subtype
538
539 begin
540 -- Make sure that the list of index constraints is properly attached to
541 -- the tree, and then collect the aggregate bounds.
542
543 Set_Parent (Index_Constraints, N);
544 Collect_Aggr_Bounds (N, 1);
545
546 -- Build the list of constrained indexes of our aggregate itype
547
548 for J in 1 .. Aggr_Dimension loop
549 Create_Index : declare
550 Index_Base : constant Entity_Id :=
551 Base_Type (Etype (Aggr_Range (J)));
552 Index_Typ : Entity_Id;
553
554 begin
555 -- Construct the Index subtype, and associate it with the range
556 -- construct that generates it.
557
558 Index_Typ :=
559 Create_Itype (Subtype_Kind (Ekind (Index_Base)), Aggr_Range (J));
560
561 Set_Etype (Index_Typ, Index_Base);
562
563 if Is_Character_Type (Index_Base) then
564 Set_Is_Character_Type (Index_Typ);
565 end if;
566
567 Set_Size_Info (Index_Typ, (Index_Base));
568 Set_RM_Size (Index_Typ, RM_Size (Index_Base));
569 Set_First_Rep_Item (Index_Typ, First_Rep_Item (Index_Base));
570 Set_Scalar_Range (Index_Typ, Aggr_Range (J));
571
572 if Is_Discrete_Or_Fixed_Point_Type (Index_Typ) then
573 Set_RM_Size (Index_Typ, UI_From_Int (Minimum_Size (Index_Typ)));
574 end if;
575
576 Set_Etype (Aggr_Range (J), Index_Typ);
577
578 Append (Aggr_Range (J), To => Index_Constraints);
579 end Create_Index;
580 end loop;
581
582 -- Now build the Itype
583
584 Itype := Create_Itype (E_Array_Subtype, N);
585
586 Set_First_Rep_Item (Itype, First_Rep_Item (Typ));
587 Set_Convention (Itype, Convention (Typ));
588 Set_Depends_On_Private (Itype, Has_Private_Component (Typ));
589 Set_Etype (Itype, Base_Type (Typ));
590 Set_Has_Alignment_Clause (Itype, Has_Alignment_Clause (Typ));
591 Set_Is_Aliased (Itype, Is_Aliased (Typ));
592 Set_Is_Independent (Itype, Is_Independent (Typ));
593 Set_Depends_On_Private (Itype, Depends_On_Private (Typ));
594
595 Copy_Suppress_Status (Index_Check, Typ, Itype);
596 Copy_Suppress_Status (Length_Check, Typ, Itype);
597
598 Set_First_Index (Itype, First (Index_Constraints));
599 Set_Is_Constrained (Itype, True);
600 Set_Is_Internal (Itype, True);
601
602 if Has_Predicates (Typ) then
603 Set_Has_Predicates (Itype);
604
605 -- If the base type has a predicate, capture the predicated parent
606 -- or the existing predicate function for SPARK use.
607
608 if Present (Predicate_Function (Typ)) then
609 Set_Predicate_Function (Itype, Predicate_Function (Typ));
610
611 elsif Is_Itype (Typ) then
612 Set_Predicated_Parent (Itype, Predicated_Parent (Typ));
613
614 else
615 Set_Predicated_Parent (Itype, Typ);
616 end if;
617 end if;
618
619 -- A simple optimization: purely positional aggregates of static
620 -- components should be passed to gigi unexpanded whenever possible, and
621 -- regardless of the staticness of the bounds themselves. Subsequent
622 -- checks in exp_aggr verify that type is not packed, etc.
623
624 Set_Size_Known_At_Compile_Time
625 (Itype,
626 Is_Fully_Positional
627 and then Comes_From_Source (N)
628 and then Size_Known_At_Compile_Time (Component_Type (Typ)));
629
630 -- We always need a freeze node for a packed array subtype, so that we
631 -- can build the Packed_Array_Impl_Type corresponding to the subtype. If
632 -- expansion is disabled, the packed array subtype is not built, and we
633 -- must not generate a freeze node for the type, or else it will appear
634 -- incomplete to gigi.
635
636 if Is_Packed (Itype)
637 and then not In_Spec_Expression
638 and then Expander_Active
639 then
640 Freeze_Itype (Itype, N);
641 end if;
642
643 return Itype;
644 end Array_Aggr_Subtype;
645
646 --------------------------------
647 -- Check_Misspelled_Component --
648 --------------------------------
649
650 procedure Check_Misspelled_Component
651 (Elements : Elist_Id;
652 Component : Node_Id)
653 is
654 Max_Suggestions : constant := 2;
655
656 Nr_Of_Suggestions : Natural := 0;
657 Suggestion_1 : Entity_Id := Empty;
658 Suggestion_2 : Entity_Id := Empty;
659 Component_Elmt : Elmt_Id;
660
661 begin
662 -- All the components of List are matched against Component and a count
663 -- is maintained of possible misspellings. When at the end of the
664 -- analysis there are one or two (not more) possible misspellings,
665 -- these misspellings will be suggested as possible corrections.
666
667 Component_Elmt := First_Elmt (Elements);
668 while Nr_Of_Suggestions <= Max_Suggestions
669 and then Present (Component_Elmt)
670 loop
671 if Is_Bad_Spelling_Of
672 (Chars (Node (Component_Elmt)),
673 Chars (Component))
674 then
675 Nr_Of_Suggestions := Nr_Of_Suggestions + 1;
676
677 case Nr_Of_Suggestions is
678 when 1 => Suggestion_1 := Node (Component_Elmt);
679 when 2 => Suggestion_2 := Node (Component_Elmt);
680 when others => null;
681 end case;
682 end if;
683
684 Next_Elmt (Component_Elmt);
685 end loop;
686
687 -- Report at most two suggestions
688
689 if Nr_Of_Suggestions = 1 then
690 Error_Msg_NE -- CODEFIX
691 ("\possible misspelling of&", Component, Suggestion_1);
692
693 elsif Nr_Of_Suggestions = 2 then
694 Error_Msg_Node_2 := Suggestion_2;
695 Error_Msg_NE -- CODEFIX
696 ("\possible misspelling of& or&", Component, Suggestion_1);
697 end if;
698 end Check_Misspelled_Component;
699
700 ----------------------------------------
701 -- Check_Expr_OK_In_Limited_Aggregate --
702 ----------------------------------------
703
704 procedure Check_Expr_OK_In_Limited_Aggregate (Expr : Node_Id) is
705 begin
706 if Is_Limited_Type (Etype (Expr))
707 and then Comes_From_Source (Expr)
708 then
709 if In_Instance_Body or else In_Inlined_Body then
710 null;
711
712 elsif not OK_For_Limited_Init (Etype (Expr), Expr) then
713 Error_Msg_N
714 ("initialization not allowed for limited types", Expr);
715 Explain_Limited_Type (Etype (Expr), Expr);
716 end if;
717 end if;
718 end Check_Expr_OK_In_Limited_Aggregate;
719
720 -------------------------
721 -- Is_Others_Aggregate --
722 -------------------------
723
724 function Is_Others_Aggregate (Aggr : Node_Id) return Boolean is
725 Assoc : constant List_Id := Component_Associations (Aggr);
726
727 begin
728 return No (Expressions (Aggr))
729 and then Nkind (First (Choice_List (First (Assoc)))) = N_Others_Choice;
730 end Is_Others_Aggregate;
731
732 -------------------------
733 -- Is_Single_Aggregate --
734 -------------------------
735
736 function Is_Single_Aggregate (Aggr : Node_Id) return Boolean is
737 Assoc : constant List_Id := Component_Associations (Aggr);
738
739 begin
740 return No (Expressions (Aggr))
741 and then No (Next (First (Assoc)))
742 and then No (Next (First (Choice_List (First (Assoc)))));
743 end Is_Single_Aggregate;
744
745 --------------------------------
746 -- Make_String_Into_Aggregate --
747 --------------------------------
748
749 procedure Make_String_Into_Aggregate (N : Node_Id) is
750 Exprs : constant List_Id := New_List;
751 Loc : constant Source_Ptr := Sloc (N);
752 Str : constant String_Id := Strval (N);
753 Strlen : constant Nat := String_Length (Str);
754 C : Char_Code;
755 C_Node : Node_Id;
756 New_N : Node_Id;
757 P : Source_Ptr;
758
759 begin
760 P := Loc + 1;
761 for J in 1 .. Strlen loop
762 C := Get_String_Char (Str, J);
763 Set_Character_Literal_Name (C);
764
765 C_Node :=
766 Make_Character_Literal (P,
767 Chars => Name_Find,
768 Char_Literal_Value => UI_From_CC (C));
769 Set_Etype (C_Node, Any_Character);
770 Append_To (Exprs, C_Node);
771
772 P := P + 1;
773 -- Something special for wide strings???
774 end loop;
775
776 New_N := Make_Aggregate (Loc, Expressions => Exprs);
777 Set_Analyzed (New_N);
778 Set_Etype (New_N, Any_Composite);
779
780 Rewrite (N, New_N);
781 end Make_String_Into_Aggregate;
782
783 -----------------------
784 -- Resolve_Aggregate --
785 -----------------------
786
787 procedure Resolve_Aggregate (N : Node_Id; Typ : Entity_Id) is
788 Loc : constant Source_Ptr := Sloc (N);
789
790 Aggr_Subtyp : Entity_Id;
791 -- The actual aggregate subtype. This is not necessarily the same as Typ
792 -- which is the subtype of the context in which the aggregate was found.
793
794 Others_Box : Boolean := False;
795 -- Set to True if N represents a simple aggregate with only
796 -- (others => <>), not nested as part of another aggregate.
797
798 function Within_Aggregate (N : Node_Id) return Boolean;
799 -- Return True if N is part of an N_Aggregate
800
801 ----------------------
802 -- Within_Aggregate --
803 ----------------------
804
805 function Within_Aggregate (N : Node_Id) return Boolean is
806 P : Node_Id := Parent (N);
807 begin
808 while Present (P) loop
809 if Nkind (P) = N_Aggregate then
810 return True;
811 end if;
812
813 P := Parent (P);
814 end loop;
815
816 return False;
817 end Within_Aggregate;
818
819 -- Start of processing for Resolve_Aggregate
820
821 begin
822 -- Ignore junk empty aggregate resulting from parser error
823
824 if No (Expressions (N))
825 and then No (Component_Associations (N))
826 and then not Null_Record_Present (N)
827 then
828 return;
829 end if;
830
831 -- If the aggregate has box-initialized components, its type must be
832 -- frozen so that initialization procedures can properly be called
833 -- in the resolution that follows. The replacement of boxes with
834 -- initialization calls is properly an expansion activity but it must
835 -- be done during resolution.
836
837 if Expander_Active
838 and then Present (Component_Associations (N))
839 then
840 declare
841 Comp : Node_Id;
842 First_Comp : Boolean := True;
843
844 begin
845 Comp := First (Component_Associations (N));
846 while Present (Comp) loop
847 if Box_Present (Comp) then
848 if First_Comp
849 and then No (Expressions (N))
850 and then Nkind (First (Choices (Comp))) = N_Others_Choice
851 and then not Within_Aggregate (N)
852 then
853 Others_Box := True;
854 end if;
855
856 Insert_Actions (N, Freeze_Entity (Typ, N));
857 exit;
858 end if;
859
860 First_Comp := False;
861 Next (Comp);
862 end loop;
863 end;
864 end if;
865
866 -- Check for aggregates not allowed in configurable run-time mode.
867 -- We allow all cases of aggregates that do not come from source, since
868 -- these are all assumed to be small (e.g. bounds of a string literal).
869 -- We also allow aggregates of types we know to be small.
870
871 if not Support_Aggregates_On_Target
872 and then Comes_From_Source (N)
873 and then (not Known_Static_Esize (Typ)
874 or else Esize (Typ) > System_Max_Integer_Size)
875 then
876 Error_Msg_CRT ("aggregate", N);
877 end if;
878
879 -- Ada 2005 (AI-287): Limited aggregates allowed
880
881 -- In an instance, ignore aggregate subcomponents tnat may be limited,
882 -- because they originate in view conflicts. If the original aggregate
883 -- is legal and the actuals are legal, the aggregate itself is legal.
884
885 if Is_Limited_Type (Typ)
886 and then Ada_Version < Ada_2005
887 and then not In_Instance
888 then
889 Error_Msg_N ("aggregate type cannot be limited", N);
890 Explain_Limited_Type (Typ, N);
891
892 elsif Is_Class_Wide_Type (Typ) then
893 Error_Msg_N ("type of aggregate cannot be class-wide", N);
894
895 elsif Typ = Any_String
896 or else Typ = Any_Composite
897 then
898 Error_Msg_N ("no unique type for aggregate", N);
899 Set_Etype (N, Any_Composite);
900
901 elsif Is_Array_Type (Typ) and then Null_Record_Present (N) then
902 Error_Msg_N ("null record forbidden in array aggregate", N);
903
904 elsif Present (Find_Aspect (Typ, Aspect_Aggregate))
905 and then Ekind (Typ) /= E_Record_Type
906 and then Ada_Version >= Ada_2020
907 then
908 Resolve_Container_Aggregate (N, Typ);
909
910 elsif Is_Record_Type (Typ) then
911 Resolve_Record_Aggregate (N, Typ);
912
913 elsif Is_Array_Type (Typ) then
914
915 -- First a special test, for the case of a positional aggregate of
916 -- characters which can be replaced by a string literal.
917
918 -- Do not perform this transformation if this was a string literal
919 -- to start with, whose components needed constraint checks, or if
920 -- the component type is non-static, because it will require those
921 -- checks and be transformed back into an aggregate. If the index
922 -- type is not Integer the aggregate may represent a user-defined
923 -- string type but the context might need the original type so we
924 -- do not perform the transformation at this point.
925
926 if Number_Dimensions (Typ) = 1
927 and then Is_Standard_Character_Type (Component_Type (Typ))
928 and then No (Component_Associations (N))
929 and then not Is_Limited_Composite (Typ)
930 and then not Is_Private_Composite (Typ)
931 and then not Is_Bit_Packed_Array (Typ)
932 and then Nkind (Original_Node (Parent (N))) /= N_String_Literal
933 and then Is_OK_Static_Subtype (Component_Type (Typ))
934 and then Base_Type (Etype (First_Index (Typ))) =
935 Base_Type (Standard_Integer)
936 then
937 declare
938 Expr : Node_Id;
939
940 begin
941 Expr := First (Expressions (N));
942 while Present (Expr) loop
943 exit when Nkind (Expr) /= N_Character_Literal;
944 Next (Expr);
945 end loop;
946
947 if No (Expr) then
948 Start_String;
949
950 Expr := First (Expressions (N));
951 while Present (Expr) loop
952 Store_String_Char (UI_To_CC (Char_Literal_Value (Expr)));
953 Next (Expr);
954 end loop;
955
956 Rewrite (N, Make_String_Literal (Loc, End_String));
957
958 Analyze_And_Resolve (N, Typ);
959 return;
960 end if;
961 end;
962 end if;
963
964 -- Here if we have a real aggregate to deal with
965
966 Array_Aggregate : declare
967 Aggr_Resolved : Boolean;
968
969 Aggr_Typ : constant Entity_Id := Etype (Typ);
970 -- This is the unconstrained array type, which is the type against
971 -- which the aggregate is to be resolved. Typ itself is the array
972 -- type of the context which may not be the same subtype as the
973 -- subtype for the final aggregate.
974
975 begin
976 -- In the following we determine whether an OTHERS choice is
977 -- allowed inside the array aggregate. The test checks the context
978 -- in which the array aggregate occurs. If the context does not
979 -- permit it, or the aggregate type is unconstrained, an OTHERS
980 -- choice is not allowed (except that it is always allowed on the
981 -- right-hand side of an assignment statement; in this case the
982 -- constrainedness of the type doesn't matter, because an array
983 -- object is always constrained).
984
985 -- If expansion is disabled (generic context, or semantics-only
986 -- mode) actual subtypes cannot be constructed, and the type of an
987 -- object may be its unconstrained nominal type. However, if the
988 -- context is an assignment statement, OTHERS is allowed, because
989 -- the target of the assignment will have a constrained subtype
990 -- when fully compiled. Ditto if the context is an initialization
991 -- procedure where a component may have a predicate function that
992 -- carries the base type.
993
994 -- Note that there is no node for Explicit_Actual_Parameter.
995 -- To test for this context we therefore have to test for node
996 -- N_Parameter_Association which itself appears only if there is a
997 -- formal parameter. Consequently we also need to test for
998 -- N_Procedure_Call_Statement or N_Function_Call.
999
1000 -- The context may be an N_Reference node, created by expansion.
1001 -- Legality of the others clause was established in the source,
1002 -- so the context is legal.
1003
1004 Set_Etype (N, Aggr_Typ); -- May be overridden later on
1005
1006 if Nkind (Parent (N)) = N_Assignment_Statement
1007 or else Inside_Init_Proc
1008 or else (Is_Constrained (Typ)
1009 and then Nkind (Parent (N)) in
1010 N_Parameter_Association
1011 | N_Function_Call
1012 | N_Procedure_Call_Statement
1013 | N_Generic_Association
1014 | N_Formal_Object_Declaration
1015 | N_Simple_Return_Statement
1016 | N_Object_Declaration
1017 | N_Component_Declaration
1018 | N_Parameter_Specification
1019 | N_Qualified_Expression
1020 | N_Reference
1021 | N_Aggregate
1022 | N_Extension_Aggregate
1023 | N_Component_Association
1024 | N_Case_Expression_Alternative
1025 | N_If_Expression
1026 | N_Expression_With_Actions)
1027 then
1028 Aggr_Resolved :=
1029 Resolve_Array_Aggregate
1030 (N,
1031 Index => First_Index (Aggr_Typ),
1032 Index_Constr => First_Index (Typ),
1033 Component_Typ => Component_Type (Typ),
1034 Others_Allowed => True);
1035 else
1036 Aggr_Resolved :=
1037 Resolve_Array_Aggregate
1038 (N,
1039 Index => First_Index (Aggr_Typ),
1040 Index_Constr => First_Index (Aggr_Typ),
1041 Component_Typ => Component_Type (Typ),
1042 Others_Allowed => False);
1043 end if;
1044
1045 if not Aggr_Resolved then
1046
1047 -- A parenthesized expression may have been intended as an
1048 -- aggregate, leading to a type error when analyzing the
1049 -- component. This can also happen for a nested component
1050 -- (see Analyze_Aggr_Expr).
1051
1052 if Paren_Count (N) > 0 then
1053 Error_Msg_N
1054 ("positional aggregate cannot have one component", N);
1055 end if;
1056
1057 Aggr_Subtyp := Any_Composite;
1058
1059 else
1060 Aggr_Subtyp := Array_Aggr_Subtype (N, Typ);
1061 end if;
1062
1063 Set_Etype (N, Aggr_Subtyp);
1064 end Array_Aggregate;
1065
1066 elsif Is_Private_Type (Typ)
1067 and then Present (Full_View (Typ))
1068 and then (In_Inlined_Body or In_Instance_Body)
1069 and then Is_Composite_Type (Full_View (Typ))
1070 then
1071 Resolve (N, Full_View (Typ));
1072
1073 else
1074 Error_Msg_N ("illegal context for aggregate", N);
1075 end if;
1076
1077 -- If we can determine statically that the evaluation of the aggregate
1078 -- raises Constraint_Error, then replace the aggregate with an
1079 -- N_Raise_Constraint_Error node, but set the Etype to the right
1080 -- aggregate subtype. Gigi needs this.
1081
1082 if Raises_Constraint_Error (N) then
1083 Aggr_Subtyp := Etype (N);
1084 Rewrite (N,
1085 Make_Raise_Constraint_Error (Loc, Reason => CE_Range_Check_Failed));
1086 Set_Raises_Constraint_Error (N);
1087 Set_Etype (N, Aggr_Subtyp);
1088 Set_Analyzed (N);
1089 end if;
1090
1091 if Warn_On_No_Value_Assigned
1092 and then Others_Box
1093 and then not Is_Fully_Initialized_Type (Etype (N))
1094 then
1095 Error_Msg_N ("?v?aggregate not fully initialized", N);
1096 end if;
1097
1098 Check_Function_Writable_Actuals (N);
1099 end Resolve_Aggregate;
1100
1101 -----------------------------
1102 -- Resolve_Array_Aggregate --
1103 -----------------------------
1104
1105 function Resolve_Array_Aggregate
1106 (N : Node_Id;
1107 Index : Node_Id;
1108 Index_Constr : Node_Id;
1109 Component_Typ : Entity_Id;
1110 Others_Allowed : Boolean) return Boolean
1111 is
1112 Loc : constant Source_Ptr := Sloc (N);
1113
1114 Failure : constant Boolean := False;
1115 Success : constant Boolean := True;
1116
1117 Index_Typ : constant Entity_Id := Etype (Index);
1118 Index_Typ_Low : constant Node_Id := Type_Low_Bound (Index_Typ);
1119 Index_Typ_High : constant Node_Id := Type_High_Bound (Index_Typ);
1120 -- The type of the index corresponding to the array sub-aggregate along
1121 -- with its low and upper bounds.
1122
1123 Index_Base : constant Entity_Id := Base_Type (Index_Typ);
1124 Index_Base_Low : constant Node_Id := Type_Low_Bound (Index_Base);
1125 Index_Base_High : constant Node_Id := Type_High_Bound (Index_Base);
1126 -- Ditto for the base type
1127
1128 Others_Present : Boolean := False;
1129
1130 Nb_Choices : Nat := 0;
1131 -- Contains the overall number of named choices in this sub-aggregate
1132
1133 function Add (Val : Uint; To : Node_Id) return Node_Id;
1134 -- Creates a new expression node where Val is added to expression To.
1135 -- Tries to constant fold whenever possible. To must be an already
1136 -- analyzed expression.
1137
1138 procedure Check_Bound (BH : Node_Id; AH : in out Node_Id);
1139 -- Checks that AH (the upper bound of an array aggregate) is less than
1140 -- or equal to BH (the upper bound of the index base type). If the check
1141 -- fails, a warning is emitted, the Raises_Constraint_Error flag of N is
1142 -- set, and AH is replaced with a duplicate of BH.
1143
1144 procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id);
1145 -- Checks that range AL .. AH is compatible with range L .. H. Emits a
1146 -- warning if not and sets the Raises_Constraint_Error flag in N.
1147
1148 procedure Check_Length (L, H : Node_Id; Len : Uint);
1149 -- Checks that range L .. H contains at least Len elements. Emits a
1150 -- warning if not and sets the Raises_Constraint_Error flag in N.
1151
1152 function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean;
1153 -- Returns True if range L .. H is dynamic or null
1154
1155 procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean);
1156 -- Given expression node From, this routine sets OK to False if it
1157 -- cannot statically evaluate From. Otherwise it stores this static
1158 -- value into Value.
1159
1160 function Resolve_Aggr_Expr
1161 (Expr : Node_Id;
1162 Single_Elmt : Boolean) return Boolean;
1163 -- Resolves aggregate expression Expr. Returns False if resolution
1164 -- fails. If Single_Elmt is set to False, the expression Expr may be
1165 -- used to initialize several array aggregate elements (this can happen
1166 -- for discrete choices such as "L .. H => Expr" or the OTHERS choice).
1167 -- In this event we do not resolve Expr unless expansion is disabled.
1168 -- To know why, see the DELAYED COMPONENT RESOLUTION note above.
1169 --
1170 -- NOTE: In the case of "... => <>", we pass the in the
1171 -- N_Component_Association node as Expr, since there is no Expression in
1172 -- that case, and we need a Sloc for the error message.
1173
1174 procedure Resolve_Iterated_Component_Association
1175 (N : Node_Id;
1176 Index_Typ : Entity_Id);
1177 -- For AI12-061
1178
1179 ---------
1180 -- Add --
1181 ---------
1182
1183 function Add (Val : Uint; To : Node_Id) return Node_Id is
1184 Expr_Pos : Node_Id;
1185 Expr : Node_Id;
1186 To_Pos : Node_Id;
1187
1188 begin
1189 if Raises_Constraint_Error (To) then
1190 return To;
1191 end if;
1192
1193 -- First test if we can do constant folding
1194
1195 if Compile_Time_Known_Value (To)
1196 or else Nkind (To) = N_Integer_Literal
1197 then
1198 Expr_Pos := Make_Integer_Literal (Loc, Expr_Value (To) + Val);
1199 Set_Is_Static_Expression (Expr_Pos);
1200 Set_Etype (Expr_Pos, Etype (To));
1201 Set_Analyzed (Expr_Pos, Analyzed (To));
1202
1203 if not Is_Enumeration_Type (Index_Typ) then
1204 Expr := Expr_Pos;
1205
1206 -- If we are dealing with enumeration return
1207 -- Index_Typ'Val (Expr_Pos)
1208
1209 else
1210 Expr :=
1211 Make_Attribute_Reference
1212 (Loc,
1213 Prefix => New_Occurrence_Of (Index_Typ, Loc),
1214 Attribute_Name => Name_Val,
1215 Expressions => New_List (Expr_Pos));
1216 end if;
1217
1218 return Expr;
1219 end if;
1220
1221 -- If we are here no constant folding possible
1222
1223 if not Is_Enumeration_Type (Index_Base) then
1224 Expr :=
1225 Make_Op_Add (Loc,
1226 Left_Opnd => Duplicate_Subexpr (To),
1227 Right_Opnd => Make_Integer_Literal (Loc, Val));
1228
1229 -- If we are dealing with enumeration return
1230 -- Index_Typ'Val (Index_Typ'Pos (To) + Val)
1231
1232 else
1233 To_Pos :=
1234 Make_Attribute_Reference
1235 (Loc,
1236 Prefix => New_Occurrence_Of (Index_Typ, Loc),
1237 Attribute_Name => Name_Pos,
1238 Expressions => New_List (Duplicate_Subexpr (To)));
1239
1240 Expr_Pos :=
1241 Make_Op_Add (Loc,
1242 Left_Opnd => To_Pos,
1243 Right_Opnd => Make_Integer_Literal (Loc, Val));
1244
1245 Expr :=
1246 Make_Attribute_Reference
1247 (Loc,
1248 Prefix => New_Occurrence_Of (Index_Typ, Loc),
1249 Attribute_Name => Name_Val,
1250 Expressions => New_List (Expr_Pos));
1251
1252 -- If the index type has a non standard representation, the
1253 -- attributes 'Val and 'Pos expand into function calls and the
1254 -- resulting expression is considered non-safe for reevaluation
1255 -- by the backend. Relocate it into a constant temporary in order
1256 -- to make it safe for reevaluation.
1257
1258 if Has_Non_Standard_Rep (Etype (N)) then
1259 declare
1260 Def_Id : Entity_Id;
1261
1262 begin
1263 Def_Id := Make_Temporary (Loc, 'R', Expr);
1264 Set_Etype (Def_Id, Index_Typ);
1265 Insert_Action (N,
1266 Make_Object_Declaration (Loc,
1267 Defining_Identifier => Def_Id,
1268 Object_Definition =>
1269 New_Occurrence_Of (Index_Typ, Loc),
1270 Constant_Present => True,
1271 Expression => Relocate_Node (Expr)));
1272
1273 Expr := New_Occurrence_Of (Def_Id, Loc);
1274 end;
1275 end if;
1276 end if;
1277
1278 return Expr;
1279 end Add;
1280
1281 -----------------
1282 -- Check_Bound --
1283 -----------------
1284
1285 procedure Check_Bound (BH : Node_Id; AH : in out Node_Id) is
1286 Val_BH : Uint;
1287 Val_AH : Uint;
1288
1289 OK_BH : Boolean;
1290 OK_AH : Boolean;
1291
1292 begin
1293 Get (Value => Val_BH, From => BH, OK => OK_BH);
1294 Get (Value => Val_AH, From => AH, OK => OK_AH);
1295
1296 if OK_BH and then OK_AH and then Val_BH < Val_AH then
1297 Set_Raises_Constraint_Error (N);
1298 Error_Msg_Warn := SPARK_Mode /= On;
1299 Error_Msg_N ("upper bound out of range<<", AH);
1300 Error_Msg_N ("\Constraint_Error [<<", AH);
1301
1302 -- You need to set AH to BH or else in the case of enumerations
1303 -- indexes we will not be able to resolve the aggregate bounds.
1304
1305 AH := Duplicate_Subexpr (BH);
1306 end if;
1307 end Check_Bound;
1308
1309 ------------------
1310 -- Check_Bounds --
1311 ------------------
1312
1313 procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id) is
1314 Val_L : Uint;
1315 Val_H : Uint;
1316 Val_AL : Uint;
1317 Val_AH : Uint;
1318
1319 OK_L : Boolean;
1320 OK_H : Boolean;
1321
1322 OK_AL : Boolean;
1323 OK_AH : Boolean;
1324 pragma Warnings (Off, OK_AL);
1325 pragma Warnings (Off, OK_AH);
1326
1327 begin
1328 if Raises_Constraint_Error (N)
1329 or else Dynamic_Or_Null_Range (AL, AH)
1330 then
1331 return;
1332 end if;
1333
1334 Get (Value => Val_L, From => L, OK => OK_L);
1335 Get (Value => Val_H, From => H, OK => OK_H);
1336
1337 Get (Value => Val_AL, From => AL, OK => OK_AL);
1338 Get (Value => Val_AH, From => AH, OK => OK_AH);
1339
1340 if OK_L and then Val_L > Val_AL then
1341 Set_Raises_Constraint_Error (N);
1342 Error_Msg_Warn := SPARK_Mode /= On;
1343 Error_Msg_N ("lower bound of aggregate out of range<<", N);
1344 Error_Msg_N ("\Constraint_Error [<<", N);
1345 end if;
1346
1347 if OK_H and then Val_H < Val_AH then
1348 Set_Raises_Constraint_Error (N);
1349 Error_Msg_Warn := SPARK_Mode /= On;
1350 Error_Msg_N ("upper bound of aggregate out of range<<", N);
1351 Error_Msg_N ("\Constraint_Error [<<", N);
1352 end if;
1353 end Check_Bounds;
1354
1355 ------------------
1356 -- Check_Length --
1357 ------------------
1358
1359 procedure Check_Length (L, H : Node_Id; Len : Uint) is
1360 Val_L : Uint;
1361 Val_H : Uint;
1362
1363 OK_L : Boolean;
1364 OK_H : Boolean;
1365
1366 Range_Len : Uint;
1367
1368 begin
1369 if Raises_Constraint_Error (N) then
1370 return;
1371 end if;
1372
1373 Get (Value => Val_L, From => L, OK => OK_L);
1374 Get (Value => Val_H, From => H, OK => OK_H);
1375
1376 if not OK_L or else not OK_H then
1377 return;
1378 end if;
1379
1380 -- If null range length is zero
1381
1382 if Val_L > Val_H then
1383 Range_Len := Uint_0;
1384 else
1385 Range_Len := Val_H - Val_L + 1;
1386 end if;
1387
1388 if Range_Len < Len then
1389 Set_Raises_Constraint_Error (N);
1390 Error_Msg_Warn := SPARK_Mode /= On;
1391 Error_Msg_N ("too many elements<<", N);
1392 Error_Msg_N ("\Constraint_Error [<<", N);
1393 end if;
1394 end Check_Length;
1395
1396 ---------------------------
1397 -- Dynamic_Or_Null_Range --
1398 ---------------------------
1399
1400 function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean is
1401 Val_L : Uint;
1402 Val_H : Uint;
1403
1404 OK_L : Boolean;
1405 OK_H : Boolean;
1406
1407 begin
1408 Get (Value => Val_L, From => L, OK => OK_L);
1409 Get (Value => Val_H, From => H, OK => OK_H);
1410
1411 return not OK_L or else not OK_H
1412 or else not Is_OK_Static_Expression (L)
1413 or else not Is_OK_Static_Expression (H)
1414 or else Val_L > Val_H;
1415 end Dynamic_Or_Null_Range;
1416
1417 ---------
1418 -- Get --
1419 ---------
1420
1421 procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean) is
1422 begin
1423 OK := True;
1424
1425 if Compile_Time_Known_Value (From) then
1426 Value := Expr_Value (From);
1427
1428 -- If expression From is something like Some_Type'Val (10) then
1429 -- Value = 10.
1430
1431 elsif Nkind (From) = N_Attribute_Reference
1432 and then Attribute_Name (From) = Name_Val
1433 and then Compile_Time_Known_Value (First (Expressions (From)))
1434 then
1435 Value := Expr_Value (First (Expressions (From)));
1436 else
1437 Value := Uint_0;
1438 OK := False;
1439 end if;
1440 end Get;
1441
1442 -----------------------
1443 -- Resolve_Aggr_Expr --
1444 -----------------------
1445
1446 function Resolve_Aggr_Expr
1447 (Expr : Node_Id;
1448 Single_Elmt : Boolean) return Boolean
1449 is
1450 Nxt_Ind : constant Node_Id := Next_Index (Index);
1451 Nxt_Ind_Constr : constant Node_Id := Next_Index (Index_Constr);
1452 -- Index is the current index corresponding to the expression
1453
1454 Resolution_OK : Boolean := True;
1455 -- Set to False if resolution of the expression failed
1456
1457 begin
1458 -- Defend against previous errors
1459
1460 if Nkind (Expr) = N_Error
1461 or else Error_Posted (Expr)
1462 then
1463 return True;
1464 end if;
1465
1466 -- If the array type against which we are resolving the aggregate
1467 -- has several dimensions, the expressions nested inside the
1468 -- aggregate must be further aggregates (or strings).
1469
1470 if Present (Nxt_Ind) then
1471 if Nkind (Expr) /= N_Aggregate then
1472
1473 -- A string literal can appear where a one-dimensional array
1474 -- of characters is expected. If the literal looks like an
1475 -- operator, it is still an operator symbol, which will be
1476 -- transformed into a string when analyzed.
1477
1478 if Is_Character_Type (Component_Typ)
1479 and then No (Next_Index (Nxt_Ind))
1480 and then Nkind (Expr) in N_String_Literal | N_Operator_Symbol
1481 then
1482 -- A string literal used in a multidimensional array
1483 -- aggregate in place of the final one-dimensional
1484 -- aggregate must not be enclosed in parentheses.
1485
1486 if Paren_Count (Expr) /= 0 then
1487 Error_Msg_N ("no parenthesis allowed here", Expr);
1488 end if;
1489
1490 Make_String_Into_Aggregate (Expr);
1491
1492 else
1493 Error_Msg_N ("nested array aggregate expected", Expr);
1494
1495 -- If the expression is parenthesized, this may be
1496 -- a missing component association for a 1-aggregate.
1497
1498 if Paren_Count (Expr) > 0 then
1499 Error_Msg_N
1500 ("\if single-component aggregate is intended, "
1501 & "write e.g. (1 ='> ...)", Expr);
1502 end if;
1503
1504 return Failure;
1505 end if;
1506 end if;
1507
1508 -- If it's "... => <>", nothing to resolve
1509
1510 if Nkind (Expr) = N_Component_Association then
1511 pragma Assert (Box_Present (Expr));
1512 return Success;
1513 end if;
1514
1515 -- Ada 2005 (AI-231): Propagate the type to the nested aggregate.
1516 -- Required to check the null-exclusion attribute (if present).
1517 -- This value may be overridden later on.
1518
1519 Set_Etype (Expr, Etype (N));
1520
1521 Resolution_OK := Resolve_Array_Aggregate
1522 (Expr, Nxt_Ind, Nxt_Ind_Constr, Component_Typ, Others_Allowed);
1523
1524 else
1525 -- If it's "... => <>", nothing to resolve
1526
1527 if Nkind (Expr) = N_Component_Association then
1528 pragma Assert (Box_Present (Expr));
1529 return Success;
1530 end if;
1531
1532 -- Do not resolve the expressions of discrete or others choices
1533 -- unless the expression covers a single component, or the
1534 -- expander is inactive.
1535
1536 -- In SPARK mode, expressions that can perform side effects will
1537 -- be recognized by the gnat2why back-end, and the whole
1538 -- subprogram will be ignored. So semantic analysis can be
1539 -- performed safely.
1540
1541 if Single_Elmt
1542 or else not Expander_Active
1543 or else In_Spec_Expression
1544 then
1545 Analyze_And_Resolve (Expr, Component_Typ);
1546 Check_Expr_OK_In_Limited_Aggregate (Expr);
1547 Check_Non_Static_Context (Expr);
1548 Aggregate_Constraint_Checks (Expr, Component_Typ);
1549 Check_Unset_Reference (Expr);
1550 end if;
1551 end if;
1552
1553 -- If an aggregate component has a type with predicates, an explicit
1554 -- predicate check must be applied, as for an assignment statement,
1555 -- because the aggregate might not be expanded into individual
1556 -- component assignments. If the expression covers several components
1557 -- the analysis and the predicate check take place later.
1558
1559 if Has_Predicates (Component_Typ)
1560 and then Analyzed (Expr)
1561 then
1562 Apply_Predicate_Check (Expr, Component_Typ);
1563 end if;
1564
1565 if Raises_Constraint_Error (Expr)
1566 and then Nkind (Parent (Expr)) /= N_Component_Association
1567 then
1568 Set_Raises_Constraint_Error (N);
1569 end if;
1570
1571 -- If the expression has been marked as requiring a range check,
1572 -- then generate it here. It's a bit odd to be generating such
1573 -- checks in the analyzer, but harmless since Generate_Range_Check
1574 -- does nothing (other than making sure Do_Range_Check is set) if
1575 -- the expander is not active.
1576
1577 if Do_Range_Check (Expr) then
1578 Generate_Range_Check (Expr, Component_Typ, CE_Range_Check_Failed);
1579 end if;
1580
1581 return Resolution_OK;
1582 end Resolve_Aggr_Expr;
1583
1584 --------------------------------------------
1585 -- Resolve_Iterated_Component_Association --
1586 --------------------------------------------
1587
1588 procedure Resolve_Iterated_Component_Association
1589 (N : Node_Id;
1590 Index_Typ : Entity_Id)
1591 is
1592 Loc : constant Source_Ptr := Sloc (N);
1593 Id : constant Entity_Id := Defining_Identifier (N);
1594
1595 -----------------------
1596 -- Remove_References --
1597 -----------------------
1598
1599 function Remove_Ref (N : Node_Id) return Traverse_Result;
1600 -- Remove references to the entity Id after analysis, so it can be
1601 -- properly reanalyzed after construct is expanded into a loop.
1602
1603 function Remove_Ref (N : Node_Id) return Traverse_Result is
1604 begin
1605 if Nkind (N) = N_Identifier
1606 and then Present (Entity (N))
1607 and then Entity (N) = Id
1608 then
1609 Set_Entity (N, Empty);
1610 Set_Etype (N, Empty);
1611 end if;
1612 Set_Analyzed (N, False);
1613 return OK;
1614 end Remove_Ref;
1615
1616 procedure Remove_References is new Traverse_Proc (Remove_Ref);
1617
1618 -- Local variables
1619
1620 Choice : Node_Id;
1621 Dummy : Boolean;
1622 Ent : Entity_Id;
1623 Expr : Node_Id;
1624
1625 -- Start of processing for Resolve_Iterated_Component_Association
1626
1627 begin
1628 -- An element iterator specification cannot appear in
1629 -- an array aggregate because it does not provide index
1630 -- values for the association. This must be a semantic
1631 -- check because the parser cannot tell whether this is
1632 -- an array aggregate or a container aggregate.
1633
1634 if Present (Iterator_Specification (N)) then
1635 Error_Msg_N ("container element Iterator cannot appear "
1636 & "in an array aggregate", N);
1637 return;
1638 end if;
1639
1640 Choice := First (Discrete_Choices (N));
1641
1642 while Present (Choice) loop
1643 if Nkind (Choice) = N_Others_Choice then
1644 Others_Present := True;
1645
1646 else
1647 Analyze (Choice);
1648
1649 -- Choice can be a subtype name, a range, or an expression
1650
1651 if Is_Entity_Name (Choice)
1652 and then Is_Type (Entity (Choice))
1653 and then Base_Type (Entity (Choice)) = Base_Type (Index_Typ)
1654 then
1655 null;
1656
1657 else
1658 Analyze_And_Resolve (Choice, Index_Typ);
1659 end if;
1660 end if;
1661
1662 Next (Choice);
1663 end loop;
1664
1665 -- Create a scope in which to introduce an index, which is usually
1666 -- visible in the expression for the component, and needed for its
1667 -- analysis.
1668
1669 Ent := New_Internal_Entity (E_Loop, Current_Scope, Loc, 'L');
1670 Set_Etype (Ent, Standard_Void_Type);
1671 Set_Parent (Ent, Parent (N));
1672 Push_Scope (Ent);
1673
1674 -- Insert and decorate the index variable in the current scope.
1675 -- The expression has to be analyzed once the index variable is
1676 -- directly visible.
1677
1678 Enter_Name (Id);
1679 Set_Etype (Id, Index_Typ);
1680 Set_Ekind (Id, E_Variable);
1681 Set_Scope (Id, Ent);
1682
1683 -- Analyze expression without expansion, to verify legality.
1684 -- When generating code, we then remove references to the index
1685 -- variable, because the expression will be analyzed anew after
1686 -- rewritting as a loop with a new index variable; when not
1687 -- generating code we leave the analyzed expression as it is.
1688
1689 Expr := Expression (N);
1690
1691 Expander_Mode_Save_And_Set (False);
1692 Dummy := Resolve_Aggr_Expr (Expr, Single_Elmt => False);
1693 Expander_Mode_Restore;
1694
1695 if Operating_Mode /= Check_Semantics then
1696 Remove_References (Expr);
1697 end if;
1698
1699 -- An iterated_component_association may appear in a nested
1700 -- aggregate for a multidimensional structure: preserve the bounds
1701 -- computed for the expression, as well as the anonymous array
1702 -- type generated for it; both are needed during array expansion.
1703
1704 if Nkind (Expr) = N_Aggregate then
1705 Set_Aggregate_Bounds (Expression (N), Aggregate_Bounds (Expr));
1706 Set_Etype (Expression (N), Etype (Expr));
1707 end if;
1708
1709 End_Scope;
1710 end Resolve_Iterated_Component_Association;
1711
1712 -- Local variables
1713
1714 Assoc : Node_Id;
1715 Choice : Node_Id;
1716 Expr : Node_Id;
1717 Discard : Node_Id;
1718
1719 Aggr_Low : Node_Id := Empty;
1720 Aggr_High : Node_Id := Empty;
1721 -- The actual low and high bounds of this sub-aggregate
1722
1723 Case_Table_Size : Nat;
1724 -- Contains the size of the case table needed to sort aggregate choices
1725
1726 Choices_Low : Node_Id := Empty;
1727 Choices_High : Node_Id := Empty;
1728 -- The lowest and highest discrete choices values for a named aggregate
1729
1730 Delete_Choice : Boolean;
1731 -- Used when replacing a subtype choice with predicate by a list
1732
1733 Nb_Elements : Uint := Uint_0;
1734 -- The number of elements in a positional aggregate
1735
1736 Nb_Discrete_Choices : Nat := 0;
1737 -- The overall number of discrete choices (not counting others choice)
1738
1739 -- Start of processing for Resolve_Array_Aggregate
1740
1741 begin
1742 -- Ignore junk empty aggregate resulting from parser error
1743
1744 if No (Expressions (N))
1745 and then No (Component_Associations (N))
1746 and then not Null_Record_Present (N)
1747 then
1748 return False;
1749 end if;
1750
1751 -- STEP 1: make sure the aggregate is correctly formatted
1752
1753 if Present (Component_Associations (N)) then
1754 Assoc := First (Component_Associations (N));
1755 while Present (Assoc) loop
1756 if Nkind (Assoc) = N_Iterated_Component_Association then
1757 Resolve_Iterated_Component_Association (Assoc, Index_Typ);
1758 end if;
1759
1760 Choice := First (Choice_List (Assoc));
1761 Delete_Choice := False;
1762 while Present (Choice) loop
1763 if Nkind (Choice) = N_Others_Choice then
1764 Others_Present := True;
1765
1766 if Choice /= First (Choice_List (Assoc))
1767 or else Present (Next (Choice))
1768 then
1769 Error_Msg_N
1770 ("OTHERS must appear alone in a choice list", Choice);
1771 return Failure;
1772 end if;
1773
1774 if Present (Next (Assoc)) then
1775 Error_Msg_N
1776 ("OTHERS must appear last in an aggregate", Choice);
1777 return Failure;
1778 end if;
1779
1780 if Ada_Version = Ada_83
1781 and then Assoc /= First (Component_Associations (N))
1782 and then Nkind (Parent (N)) in
1783 N_Assignment_Statement | N_Object_Declaration
1784 then
1785 Error_Msg_N
1786 ("(Ada 83) illegal context for OTHERS choice", N);
1787 end if;
1788
1789 elsif Is_Entity_Name (Choice) then
1790 Analyze (Choice);
1791
1792 declare
1793 E : constant Entity_Id := Entity (Choice);
1794 New_Cs : List_Id;
1795 P : Node_Id;
1796 C : Node_Id;
1797
1798 begin
1799 if Is_Type (E) and then Has_Predicates (E) then
1800 Freeze_Before (N, E);
1801
1802 if Has_Dynamic_Predicate_Aspect (E) then
1803 Error_Msg_NE
1804 ("subtype& has dynamic predicate, not allowed "
1805 & "in aggregate choice", Choice, E);
1806
1807 elsif not Is_OK_Static_Subtype (E) then
1808 Error_Msg_NE
1809 ("non-static subtype& has predicate, not allowed "
1810 & "in aggregate choice", Choice, E);
1811 end if;
1812
1813 -- If the subtype has a static predicate, replace the
1814 -- original choice with the list of individual values
1815 -- covered by the predicate.
1816 -- This should be deferred to expansion time ???
1817
1818 if Present (Static_Discrete_Predicate (E)) then
1819 Delete_Choice := True;
1820
1821 New_Cs := New_List;
1822 P := First (Static_Discrete_Predicate (E));
1823 while Present (P) loop
1824 C := New_Copy (P);
1825 Set_Sloc (C, Sloc (Choice));
1826 Append_To (New_Cs, C);
1827 Next (P);
1828 end loop;
1829
1830 Insert_List_After (Choice, New_Cs);
1831 end if;
1832 end if;
1833 end;
1834 end if;
1835
1836 Nb_Choices := Nb_Choices + 1;
1837
1838 declare
1839 C : constant Node_Id := Choice;
1840
1841 begin
1842 Next (Choice);
1843
1844 if Delete_Choice then
1845 Remove (C);
1846 Nb_Choices := Nb_Choices - 1;
1847 Delete_Choice := False;
1848 end if;
1849 end;
1850 end loop;
1851
1852 Next (Assoc);
1853 end loop;
1854 end if;
1855
1856 -- At this point we know that the others choice, if present, is by
1857 -- itself and appears last in the aggregate. Check if we have mixed
1858 -- positional and discrete associations (other than the others choice).
1859
1860 if Present (Expressions (N))
1861 and then (Nb_Choices > 1
1862 or else (Nb_Choices = 1 and then not Others_Present))
1863 then
1864 Error_Msg_N
1865 ("named association cannot follow positional association",
1866 First (Choice_List (First (Component_Associations (N)))));
1867 return Failure;
1868 end if;
1869
1870 -- Test for the validity of an others choice if present
1871
1872 if Others_Present and then not Others_Allowed then
1873 Error_Msg_N
1874 ("OTHERS choice not allowed here",
1875 First (Choice_List (First (Component_Associations (N)))));
1876 return Failure;
1877 end if;
1878
1879 -- Protect against cascaded errors
1880
1881 if Etype (Index_Typ) = Any_Type then
1882 return Failure;
1883 end if;
1884
1885 -- STEP 2: Process named components
1886
1887 if No (Expressions (N)) then
1888 if Others_Present then
1889 Case_Table_Size := Nb_Choices - 1;
1890 else
1891 Case_Table_Size := Nb_Choices;
1892 end if;
1893
1894 Step_2 : declare
1895 function Empty_Range (A : Node_Id) return Boolean;
1896 -- If an association covers an empty range, some warnings on the
1897 -- expression of the association can be disabled.
1898
1899 -----------------
1900 -- Empty_Range --
1901 -----------------
1902
1903 function Empty_Range (A : Node_Id) return Boolean is
1904 R : constant Node_Id := First (Choices (A));
1905 begin
1906 return No (Next (R))
1907 and then Nkind (R) = N_Range
1908 and then Compile_Time_Compare
1909 (Low_Bound (R), High_Bound (R), False) = GT;
1910 end Empty_Range;
1911
1912 -- Local variables
1913
1914 Low : Node_Id;
1915 High : Node_Id;
1916 -- Denote the lowest and highest values in an aggregate choice
1917
1918 S_Low : Node_Id := Empty;
1919 S_High : Node_Id := Empty;
1920 -- if a choice in an aggregate is a subtype indication these
1921 -- denote the lowest and highest values of the subtype
1922
1923 Table : Case_Table_Type (1 .. Case_Table_Size);
1924 -- Used to sort all the different choice values
1925
1926 Single_Choice : Boolean;
1927 -- Set to true every time there is a single discrete choice in a
1928 -- discrete association
1929
1930 Prev_Nb_Discrete_Choices : Nat;
1931 -- Used to keep track of the number of discrete choices in the
1932 -- current association.
1933
1934 Errors_Posted_On_Choices : Boolean := False;
1935 -- Keeps track of whether any choices have semantic errors
1936
1937 -- Start of processing for Step_2
1938
1939 begin
1940 -- STEP 2 (A): Check discrete choices validity
1941
1942 Assoc := First (Component_Associations (N));
1943 while Present (Assoc) loop
1944 Prev_Nb_Discrete_Choices := Nb_Discrete_Choices;
1945 Choice := First (Choice_List (Assoc));
1946
1947 loop
1948 Analyze (Choice);
1949
1950 if Nkind (Choice) = N_Others_Choice then
1951 Single_Choice := False;
1952 exit;
1953
1954 -- Test for subtype mark without constraint
1955
1956 elsif Is_Entity_Name (Choice) and then
1957 Is_Type (Entity (Choice))
1958 then
1959 if Base_Type (Entity (Choice)) /= Index_Base then
1960 Error_Msg_N
1961 ("invalid subtype mark in aggregate choice",
1962 Choice);
1963 return Failure;
1964 end if;
1965
1966 -- Case of subtype indication
1967
1968 elsif Nkind (Choice) = N_Subtype_Indication then
1969 Resolve_Discrete_Subtype_Indication (Choice, Index_Base);
1970
1971 if Has_Dynamic_Predicate_Aspect
1972 (Entity (Subtype_Mark (Choice)))
1973 then
1974 Error_Msg_NE
1975 ("subtype& has dynamic predicate, "
1976 & "not allowed in aggregate choice",
1977 Choice, Entity (Subtype_Mark (Choice)));
1978 end if;
1979
1980 -- Does the subtype indication evaluation raise CE?
1981
1982 Get_Index_Bounds (Subtype_Mark (Choice), S_Low, S_High);
1983 Get_Index_Bounds (Choice, Low, High);
1984 Check_Bounds (S_Low, S_High, Low, High);
1985
1986 -- Case of range or expression
1987
1988 else
1989 Resolve (Choice, Index_Base);
1990 Check_Unset_Reference (Choice);
1991 Check_Non_Static_Context (Choice);
1992
1993 -- If semantic errors were posted on the choice, then
1994 -- record that for possible early return from later
1995 -- processing (see handling of enumeration choices).
1996
1997 if Error_Posted (Choice) then
1998 Errors_Posted_On_Choices := True;
1999 end if;
2000
2001 -- Do not range check a choice. This check is redundant
2002 -- since this test is already done when we check that the
2003 -- bounds of the array aggregate are within range.
2004
2005 Set_Do_Range_Check (Choice, False);
2006 end if;
2007
2008 -- If we could not resolve the discrete choice stop here
2009
2010 if Etype (Choice) = Any_Type then
2011 return Failure;
2012
2013 -- If the discrete choice raises CE get its original bounds
2014
2015 elsif Nkind (Choice) = N_Raise_Constraint_Error then
2016 Set_Raises_Constraint_Error (N);
2017 Get_Index_Bounds (Original_Node (Choice), Low, High);
2018
2019 -- Otherwise get its bounds as usual
2020
2021 else
2022 Get_Index_Bounds (Choice, Low, High);
2023 end if;
2024
2025 if (Dynamic_Or_Null_Range (Low, High)
2026 or else (Nkind (Choice) = N_Subtype_Indication
2027 and then
2028 Dynamic_Or_Null_Range (S_Low, S_High)))
2029 and then Nb_Choices /= 1
2030 then
2031 Error_Msg_N
2032 ("dynamic or empty choice in aggregate "
2033 & "must be the only choice", Choice);
2034 return Failure;
2035 end if;
2036
2037 if not (All_Composite_Constraints_Static (Low)
2038 and then All_Composite_Constraints_Static (High)
2039 and then All_Composite_Constraints_Static (S_Low)
2040 and then All_Composite_Constraints_Static (S_High))
2041 then
2042 Check_Restriction (No_Dynamic_Sized_Objects, Choice);
2043 end if;
2044
2045 Nb_Discrete_Choices := Nb_Discrete_Choices + 1;
2046 Table (Nb_Discrete_Choices).Lo := Low;
2047 Table (Nb_Discrete_Choices).Hi := High;
2048 Table (Nb_Discrete_Choices).Choice := Choice;
2049
2050 Next (Choice);
2051
2052 if No (Choice) then
2053
2054 -- Check if we have a single discrete choice and whether
2055 -- this discrete choice specifies a single value.
2056
2057 Single_Choice :=
2058 (Nb_Discrete_Choices = Prev_Nb_Discrete_Choices + 1)
2059 and then (Low = High);
2060
2061 exit;
2062 end if;
2063 end loop;
2064
2065 -- Ada 2005 (AI-231)
2066
2067 if Ada_Version >= Ada_2005
2068 and then Known_Null (Expression (Assoc))
2069 and then not Empty_Range (Assoc)
2070 then
2071 Check_Can_Never_Be_Null (Etype (N), Expression (Assoc));
2072 end if;
2073
2074 -- Ada 2005 (AI-287): In case of default initialized component
2075 -- we delay the resolution to the expansion phase.
2076
2077 if Box_Present (Assoc) then
2078
2079 -- Ada 2005 (AI-287): In case of default initialization of a
2080 -- component the expander will generate calls to the
2081 -- corresponding initialization subprogram. We need to call
2082 -- Resolve_Aggr_Expr to check the rules about
2083 -- dimensionality.
2084
2085 if not Resolve_Aggr_Expr
2086 (Assoc, Single_Elmt => Single_Choice)
2087 then
2088 return Failure;
2089 end if;
2090
2091 -- ??? Checks for dynamically tagged expressions below will
2092 -- be only applied to iterated_component_association after
2093 -- expansion; in particular, errors might not be reported when
2094 -- -gnatc switch is used.
2095
2096 elsif Nkind (Assoc) = N_Iterated_Component_Association then
2097 null; -- handled above, in a loop context
2098
2099 elsif not Resolve_Aggr_Expr
2100 (Expression (Assoc), Single_Elmt => Single_Choice)
2101 then
2102 return Failure;
2103
2104 -- Check incorrect use of dynamically tagged expression
2105
2106 -- We differentiate here two cases because the expression may
2107 -- not be decorated. For example, the analysis and resolution
2108 -- of the expression associated with the others choice will be
2109 -- done later with the full aggregate. In such case we
2110 -- duplicate the expression tree to analyze the copy and
2111 -- perform the required check.
2112
2113 elsif not Present (Etype (Expression (Assoc))) then
2114 declare
2115 Save_Analysis : constant Boolean := Full_Analysis;
2116 Expr : constant Node_Id :=
2117 New_Copy_Tree (Expression (Assoc));
2118
2119 begin
2120 Expander_Mode_Save_And_Set (False);
2121 Full_Analysis := False;
2122
2123 -- Analyze the expression, making sure it is properly
2124 -- attached to the tree before we do the analysis.
2125
2126 Set_Parent (Expr, Parent (Expression (Assoc)));
2127 Analyze (Expr);
2128
2129 -- Compute its dimensions now, rather than at the end of
2130 -- resolution, because in the case of multidimensional
2131 -- aggregates subsequent expansion may lead to spurious
2132 -- errors.
2133
2134 Check_Expression_Dimensions (Expr, Component_Typ);
2135
2136 -- If the expression is a literal, propagate this info
2137 -- to the expression in the association, to enable some
2138 -- optimizations downstream.
2139
2140 if Is_Entity_Name (Expr)
2141 and then Present (Entity (Expr))
2142 and then Ekind (Entity (Expr)) = E_Enumeration_Literal
2143 then
2144 Analyze_And_Resolve
2145 (Expression (Assoc), Component_Typ);
2146 end if;
2147
2148 Full_Analysis := Save_Analysis;
2149 Expander_Mode_Restore;
2150
2151 if Is_Tagged_Type (Etype (Expr)) then
2152 Check_Dynamically_Tagged_Expression
2153 (Expr => Expr,
2154 Typ => Component_Type (Etype (N)),
2155 Related_Nod => N);
2156 end if;
2157 end;
2158
2159 elsif Is_Tagged_Type (Etype (Expression (Assoc))) then
2160 Check_Dynamically_Tagged_Expression
2161 (Expr => Expression (Assoc),
2162 Typ => Component_Type (Etype (N)),
2163 Related_Nod => N);
2164 end if;
2165
2166 Next (Assoc);
2167 end loop;
2168
2169 -- If aggregate contains more than one choice then these must be
2170 -- static. Check for duplicate and missing values.
2171
2172 -- Note: there is duplicated code here wrt Check_Choice_Set in
2173 -- the body of Sem_Case, and it is possible we could just reuse
2174 -- that procedure. To be checked ???
2175
2176 if Nb_Discrete_Choices > 1 then
2177 Check_Choices : declare
2178 Choice : Node_Id;
2179 -- Location of choice for messages
2180
2181 Hi_Val : Uint;
2182 Lo_Val : Uint;
2183 -- High end of one range and Low end of the next. Should be
2184 -- contiguous if there is no hole in the list of values.
2185
2186 Lo_Dup : Uint;
2187 Hi_Dup : Uint;
2188 -- End points of duplicated range
2189
2190 Missing_Or_Duplicates : Boolean := False;
2191 -- Set True if missing or duplicate choices found
2192
2193 procedure Output_Bad_Choices (Lo, Hi : Uint; C : Node_Id);
2194 -- Output continuation message with a representation of the
2195 -- bounds (just Lo if Lo = Hi, else Lo .. Hi). C is the
2196 -- choice node where the message is to be posted.
2197
2198 ------------------------
2199 -- Output_Bad_Choices --
2200 ------------------------
2201
2202 procedure Output_Bad_Choices (Lo, Hi : Uint; C : Node_Id) is
2203 begin
2204 -- Enumeration type case
2205
2206 if Is_Enumeration_Type (Index_Typ) then
2207 Error_Msg_Name_1 :=
2208 Chars (Get_Enum_Lit_From_Pos (Index_Typ, Lo, Loc));
2209 Error_Msg_Name_2 :=
2210 Chars (Get_Enum_Lit_From_Pos (Index_Typ, Hi, Loc));
2211
2212 if Lo = Hi then
2213 Error_Msg_N ("\\ %!", C);
2214 else
2215 Error_Msg_N ("\\ % .. %!", C);
2216 end if;
2217
2218 -- Integer types case
2219
2220 else
2221 Error_Msg_Uint_1 := Lo;
2222 Error_Msg_Uint_2 := Hi;
2223
2224 if Lo = Hi then
2225 Error_Msg_N ("\\ ^!", C);
2226 else
2227 Error_Msg_N ("\\ ^ .. ^!", C);
2228 end if;
2229 end if;
2230 end Output_Bad_Choices;
2231
2232 -- Start of processing for Check_Choices
2233
2234 begin
2235 Sort_Case_Table (Table);
2236
2237 -- First we do a quick linear loop to find out if we have
2238 -- any duplicates or missing entries (usually we have a
2239 -- legal aggregate, so this will get us out quickly).
2240
2241 for J in 1 .. Nb_Discrete_Choices - 1 loop
2242 Hi_Val := Expr_Value (Table (J).Hi);
2243 Lo_Val := Expr_Value (Table (J + 1).Lo);
2244
2245 if Lo_Val <= Hi_Val
2246 or else (Lo_Val > Hi_Val + 1
2247 and then not Others_Present)
2248 then
2249 Missing_Or_Duplicates := True;
2250 exit;
2251 end if;
2252 end loop;
2253
2254 -- If we have missing or duplicate entries, first fill in
2255 -- the Highest entries to make life easier in the following
2256 -- loops to detect bad entries.
2257
2258 if Missing_Or_Duplicates then
2259 Table (1).Highest := Expr_Value (Table (1).Hi);
2260
2261 for J in 2 .. Nb_Discrete_Choices loop
2262 Table (J).Highest :=
2263 UI_Max
2264 (Table (J - 1).Highest, Expr_Value (Table (J).Hi));
2265 end loop;
2266
2267 -- Loop through table entries to find duplicate indexes
2268
2269 for J in 2 .. Nb_Discrete_Choices loop
2270 Lo_Val := Expr_Value (Table (J).Lo);
2271 Hi_Val := Expr_Value (Table (J).Hi);
2272
2273 -- Case where we have duplicates (the lower bound of
2274 -- this choice is less than or equal to the highest
2275 -- high bound found so far).
2276
2277 if Lo_Val <= Table (J - 1).Highest then
2278
2279 -- We move backwards looking for duplicates. We can
2280 -- abandon this loop as soon as we reach a choice
2281 -- highest value that is less than Lo_Val.
2282
2283 for K in reverse 1 .. J - 1 loop
2284 exit when Table (K).Highest < Lo_Val;
2285
2286 -- Here we may have duplicates between entries
2287 -- for K and J. Get range of duplicates.
2288
2289 Lo_Dup :=
2290 UI_Max (Lo_Val, Expr_Value (Table (K).Lo));
2291 Hi_Dup :=
2292 UI_Min (Hi_Val, Expr_Value (Table (K).Hi));
2293
2294 -- Nothing to do if duplicate range is null
2295
2296 if Lo_Dup > Hi_Dup then
2297 null;
2298
2299 -- Otherwise place proper message
2300
2301 else
2302 -- We place message on later choice, with a
2303 -- line reference to the earlier choice.
2304
2305 if Sloc (Table (J).Choice) <
2306 Sloc (Table (K).Choice)
2307 then
2308 Choice := Table (K).Choice;
2309 Error_Msg_Sloc := Sloc (Table (J).Choice);
2310 else
2311 Choice := Table (J).Choice;
2312 Error_Msg_Sloc := Sloc (Table (K).Choice);
2313 end if;
2314
2315 if Lo_Dup = Hi_Dup then
2316 Error_Msg_N
2317 ("index value in array aggregate "
2318 & "duplicates the one given#!", Choice);
2319 else
2320 Error_Msg_N
2321 ("index values in array aggregate "
2322 & "duplicate those given#!", Choice);
2323 end if;
2324
2325 Output_Bad_Choices (Lo_Dup, Hi_Dup, Choice);
2326 end if;
2327 end loop;
2328 end if;
2329 end loop;
2330
2331 -- Loop through entries in table to find missing indexes.
2332 -- Not needed if others, since missing impossible.
2333
2334 if not Others_Present then
2335 for J in 2 .. Nb_Discrete_Choices loop
2336 Lo_Val := Expr_Value (Table (J).Lo);
2337 Hi_Val := Table (J - 1).Highest;
2338
2339 if Lo_Val > Hi_Val + 1 then
2340
2341 declare
2342 Error_Node : Node_Id;
2343
2344 begin
2345 -- If the choice is the bound of a range in
2346 -- a subtype indication, it is not in the
2347 -- source lists for the aggregate itself, so
2348 -- post the error on the aggregate. Otherwise
2349 -- post it on choice itself.
2350
2351 Choice := Table (J).Choice;
2352
2353 if Is_List_Member (Choice) then
2354 Error_Node := Choice;
2355 else
2356 Error_Node := N;
2357 end if;
2358
2359 if Hi_Val + 1 = Lo_Val - 1 then
2360 Error_Msg_N
2361 ("missing index value "
2362 & "in array aggregate!", Error_Node);
2363 else
2364 Error_Msg_N
2365 ("missing index values "
2366 & "in array aggregate!", Error_Node);
2367 end if;
2368
2369 Output_Bad_Choices
2370 (Hi_Val + 1, Lo_Val - 1, Error_Node);
2371 end;
2372 end if;
2373 end loop;
2374 end if;
2375
2376 -- If either missing or duplicate values, return failure
2377
2378 Set_Etype (N, Any_Composite);
2379 return Failure;
2380 end if;
2381 end Check_Choices;
2382 end if;
2383
2384 -- STEP 2 (B): Compute aggregate bounds and min/max choices values
2385
2386 if Nb_Discrete_Choices > 0 then
2387 Choices_Low := Table (1).Lo;
2388 Choices_High := Table (Nb_Discrete_Choices).Hi;
2389 end if;
2390
2391 -- If Others is present, then bounds of aggregate come from the
2392 -- index constraint (not the choices in the aggregate itself).
2393
2394 if Others_Present then
2395 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2396
2397 -- Abandon processing if either bound is already signalled as
2398 -- an error (prevents junk cascaded messages and blow ups).
2399
2400 if Nkind (Aggr_Low) = N_Error
2401 or else
2402 Nkind (Aggr_High) = N_Error
2403 then
2404 return False;
2405 end if;
2406
2407 -- No others clause present
2408
2409 else
2410 -- Special processing if others allowed and not present. This
2411 -- means that the bounds of the aggregate come from the index
2412 -- constraint (and the length must match).
2413
2414 if Others_Allowed then
2415 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2416
2417 -- Abandon processing if either bound is already signalled
2418 -- as an error (stop junk cascaded messages and blow ups).
2419
2420 if Nkind (Aggr_Low) = N_Error
2421 or else
2422 Nkind (Aggr_High) = N_Error
2423 then
2424 return False;
2425 end if;
2426
2427 -- If others allowed, and no others present, then the array
2428 -- should cover all index values. If it does not, we will
2429 -- get a length check warning, but there is two cases where
2430 -- an additional warning is useful:
2431
2432 -- If we have no positional components, and the length is
2433 -- wrong (which we can tell by others being allowed with
2434 -- missing components), and the index type is an enumeration
2435 -- type, then issue appropriate warnings about these missing
2436 -- components. They are only warnings, since the aggregate
2437 -- is fine, it's just the wrong length. We skip this check
2438 -- for standard character types (since there are no literals
2439 -- and it is too much trouble to concoct them), and also if
2440 -- any of the bounds have values that are not known at
2441 -- compile time.
2442
2443 -- Another case warranting a warning is when the length
2444 -- is right, but as above we have an index type that is
2445 -- an enumeration, and the bounds do not match. This is a
2446 -- case where dubious sliding is allowed and we generate a
2447 -- warning that the bounds do not match.
2448
2449 if No (Expressions (N))
2450 and then Nkind (Index) = N_Range
2451 and then Is_Enumeration_Type (Etype (Index))
2452 and then not Is_Standard_Character_Type (Etype (Index))
2453 and then Compile_Time_Known_Value (Aggr_Low)
2454 and then Compile_Time_Known_Value (Aggr_High)
2455 and then Compile_Time_Known_Value (Choices_Low)
2456 and then Compile_Time_Known_Value (Choices_High)
2457 then
2458 -- If any of the expressions or range bounds in choices
2459 -- have semantic errors, then do not attempt further
2460 -- resolution, to prevent cascaded errors.
2461
2462 if Errors_Posted_On_Choices then
2463 return Failure;
2464 end if;
2465
2466 declare
2467 ALo : constant Node_Id := Expr_Value_E (Aggr_Low);
2468 AHi : constant Node_Id := Expr_Value_E (Aggr_High);
2469 CLo : constant Node_Id := Expr_Value_E (Choices_Low);
2470 CHi : constant Node_Id := Expr_Value_E (Choices_High);
2471
2472 Ent : Entity_Id;
2473
2474 begin
2475 -- Warning case 1, missing values at start/end. Only
2476 -- do the check if the number of entries is too small.
2477
2478 if (Enumeration_Pos (CHi) - Enumeration_Pos (CLo))
2479 <
2480 (Enumeration_Pos (AHi) - Enumeration_Pos (ALo))
2481 then
2482 Error_Msg_N
2483 ("missing index value(s) in array aggregate??",
2484 N);
2485
2486 -- Output missing value(s) at start
2487
2488 if Chars (ALo) /= Chars (CLo) then
2489 Ent := Prev (CLo);
2490
2491 if Chars (ALo) = Chars (Ent) then
2492 Error_Msg_Name_1 := Chars (ALo);
2493 Error_Msg_N ("\ %??", N);
2494 else
2495 Error_Msg_Name_1 := Chars (ALo);
2496 Error_Msg_Name_2 := Chars (Ent);
2497 Error_Msg_N ("\ % .. %??", N);
2498 end if;
2499 end if;
2500
2501 -- Output missing value(s) at end
2502
2503 if Chars (AHi) /= Chars (CHi) then
2504 Ent := Next (CHi);
2505
2506 if Chars (AHi) = Chars (Ent) then
2507 Error_Msg_Name_1 := Chars (Ent);
2508 Error_Msg_N ("\ %??", N);
2509 else
2510 Error_Msg_Name_1 := Chars (Ent);
2511 Error_Msg_Name_2 := Chars (AHi);
2512 Error_Msg_N ("\ % .. %??", N);
2513 end if;
2514 end if;
2515
2516 -- Warning case 2, dubious sliding. The First_Subtype
2517 -- test distinguishes between a constrained type where
2518 -- sliding is not allowed (so we will get a warning
2519 -- later that Constraint_Error will be raised), and
2520 -- the unconstrained case where sliding is permitted.
2521
2522 elsif (Enumeration_Pos (CHi) - Enumeration_Pos (CLo))
2523 =
2524 (Enumeration_Pos (AHi) - Enumeration_Pos (ALo))
2525 and then Chars (ALo) /= Chars (CLo)
2526 and then
2527 not Is_Constrained (First_Subtype (Etype (N)))
2528 then
2529 Error_Msg_N
2530 ("bounds of aggregate do not match target??", N);
2531 end if;
2532 end;
2533 end if;
2534 end if;
2535
2536 -- If no others, aggregate bounds come from aggregate
2537
2538 Aggr_Low := Choices_Low;
2539 Aggr_High := Choices_High;
2540 end if;
2541 end Step_2;
2542
2543 -- STEP 3: Process positional components
2544
2545 else
2546 -- STEP 3 (A): Process positional elements
2547
2548 Expr := First (Expressions (N));
2549 Nb_Elements := Uint_0;
2550 while Present (Expr) loop
2551 Nb_Elements := Nb_Elements + 1;
2552
2553 -- Ada 2005 (AI-231)
2554
2555 if Ada_Version >= Ada_2005 and then Known_Null (Expr) then
2556 Check_Can_Never_Be_Null (Etype (N), Expr);
2557 end if;
2558
2559 if not Resolve_Aggr_Expr (Expr, Single_Elmt => True) then
2560 return Failure;
2561 end if;
2562
2563 -- Check incorrect use of dynamically tagged expression
2564
2565 if Is_Tagged_Type (Etype (Expr)) then
2566 Check_Dynamically_Tagged_Expression
2567 (Expr => Expr,
2568 Typ => Component_Type (Etype (N)),
2569 Related_Nod => N);
2570 end if;
2571
2572 Next (Expr);
2573 end loop;
2574
2575 if Others_Present then
2576 Assoc := Last (Component_Associations (N));
2577
2578 -- Ada 2005 (AI-231)
2579
2580 if Ada_Version >= Ada_2005 and then Known_Null (Assoc) then
2581 Check_Can_Never_Be_Null (Etype (N), Expression (Assoc));
2582 end if;
2583
2584 -- Ada 2005 (AI-287): In case of default initialized component,
2585 -- we delay the resolution to the expansion phase.
2586
2587 if Box_Present (Assoc) then
2588
2589 -- Ada 2005 (AI-287): In case of default initialization of a
2590 -- component the expander will generate calls to the
2591 -- corresponding initialization subprogram. We need to call
2592 -- Resolve_Aggr_Expr to check the rules about
2593 -- dimensionality.
2594
2595 if not Resolve_Aggr_Expr (Assoc, Single_Elmt => False) then
2596 return Failure;
2597 end if;
2598
2599 elsif not Resolve_Aggr_Expr (Expression (Assoc),
2600 Single_Elmt => False)
2601 then
2602 return Failure;
2603
2604 -- Check incorrect use of dynamically tagged expression. The
2605 -- expression of the others choice has not been resolved yet.
2606 -- In order to diagnose the semantic error we create a duplicate
2607 -- tree to analyze it and perform the check.
2608
2609 elsif Nkind (Assoc) /= N_Iterated_Component_Association then
2610 declare
2611 Save_Analysis : constant Boolean := Full_Analysis;
2612 Expr : constant Node_Id :=
2613 New_Copy_Tree (Expression (Assoc));
2614
2615 begin
2616 Expander_Mode_Save_And_Set (False);
2617 Full_Analysis := False;
2618 Analyze (Expr);
2619 Full_Analysis := Save_Analysis;
2620 Expander_Mode_Restore;
2621
2622 if Is_Tagged_Type (Etype (Expr)) then
2623 Check_Dynamically_Tagged_Expression
2624 (Expr => Expr,
2625 Typ => Component_Type (Etype (N)),
2626 Related_Nod => N);
2627 end if;
2628 end;
2629 end if;
2630 end if;
2631
2632 -- STEP 3 (B): Compute the aggregate bounds
2633
2634 if Others_Present then
2635 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2636
2637 else
2638 if Others_Allowed then
2639 Get_Index_Bounds (Index_Constr, Aggr_Low, Discard);
2640 else
2641 Aggr_Low := Index_Typ_Low;
2642 end if;
2643
2644 Aggr_High := Add (Nb_Elements - 1, To => Aggr_Low);
2645 Check_Bound (Index_Base_High, Aggr_High);
2646 end if;
2647 end if;
2648
2649 -- STEP 4: Perform static aggregate checks and save the bounds
2650
2651 -- Check (A)
2652
2653 Check_Bounds (Index_Typ_Low, Index_Typ_High, Aggr_Low, Aggr_High);
2654 Check_Bounds (Index_Base_Low, Index_Base_High, Aggr_Low, Aggr_High);
2655
2656 -- Check (B)
2657
2658 if Others_Present and then Nb_Discrete_Choices > 0 then
2659 Check_Bounds (Aggr_Low, Aggr_High, Choices_Low, Choices_High);
2660 Check_Bounds (Index_Typ_Low, Index_Typ_High,
2661 Choices_Low, Choices_High);
2662 Check_Bounds (Index_Base_Low, Index_Base_High,
2663 Choices_Low, Choices_High);
2664
2665 -- Check (C)
2666
2667 elsif Others_Present and then Nb_Elements > 0 then
2668 Check_Length (Aggr_Low, Aggr_High, Nb_Elements);
2669 Check_Length (Index_Typ_Low, Index_Typ_High, Nb_Elements);
2670 Check_Length (Index_Base_Low, Index_Base_High, Nb_Elements);
2671 end if;
2672
2673 if Raises_Constraint_Error (Aggr_Low)
2674 or else Raises_Constraint_Error (Aggr_High)
2675 then
2676 Set_Raises_Constraint_Error (N);
2677 end if;
2678
2679 Aggr_Low := Duplicate_Subexpr (Aggr_Low);
2680
2681 -- Do not duplicate Aggr_High if Aggr_High = Aggr_Low + Nb_Elements
2682 -- since the addition node returned by Add is not yet analyzed. Attach
2683 -- to tree and analyze first. Reset analyzed flag to ensure it will get
2684 -- analyzed when it is a literal bound whose type must be properly set.
2685
2686 if Others_Present or else Nb_Discrete_Choices > 0 then
2687 Aggr_High := Duplicate_Subexpr (Aggr_High);
2688
2689 if Etype (Aggr_High) = Universal_Integer then
2690 Set_Analyzed (Aggr_High, False);
2691 end if;
2692 end if;
2693
2694 -- If the aggregate already has bounds attached to it, it means this is
2695 -- a positional aggregate created as an optimization by
2696 -- Exp_Aggr.Convert_To_Positional, so we don't want to change those
2697 -- bounds.
2698
2699 if Present (Aggregate_Bounds (N)) and then not Others_Allowed then
2700 Aggr_Low := Low_Bound (Aggregate_Bounds (N));
2701 Aggr_High := High_Bound (Aggregate_Bounds (N));
2702 end if;
2703
2704 Set_Aggregate_Bounds
2705 (N, Make_Range (Loc, Low_Bound => Aggr_Low, High_Bound => Aggr_High));
2706
2707 -- The bounds may contain expressions that must be inserted upwards.
2708 -- Attach them fully to the tree. After analysis, remove side effects
2709 -- from upper bound, if still needed.
2710
2711 Set_Parent (Aggregate_Bounds (N), N);
2712 Analyze_And_Resolve (Aggregate_Bounds (N), Index_Typ);
2713 Check_Unset_Reference (Aggregate_Bounds (N));
2714
2715 if not Others_Present and then Nb_Discrete_Choices = 0 then
2716 Set_High_Bound
2717 (Aggregate_Bounds (N),
2718 Duplicate_Subexpr (High_Bound (Aggregate_Bounds (N))));
2719 end if;
2720
2721 -- Check the dimensions of each component in the array aggregate
2722
2723 Analyze_Dimension_Array_Aggregate (N, Component_Typ);
2724
2725 return Success;
2726 end Resolve_Array_Aggregate;
2727
2728 ---------------------------------
2729 -- Resolve_Container_Aggregate --
2730 ---------------------------------
2731
2732 procedure Resolve_Container_Aggregate (N : Node_Id; Typ : Entity_Id) is
2733 procedure Resolve_Iterated_Association
2734 (Comp : Node_Id;
2735 Key_Type : Entity_Id;
2736 Elmt_Type : Entity_Id);
2737 -- Resolve choices and expression in an iterated component association
2738 -- or an iterated element association, which has a key_expression.
2739 -- This is similar but not identical to the handling of this construct
2740 -- in an array aggregate.
2741 -- For a named container, the type of each choice must be compatible
2742 -- with the key type. For a positional container, the choice must be
2743 -- a subtype indication or an iterator specification that determines
2744 -- an element type.
2745
2746 Asp : constant Node_Id := Find_Value_Of_Aspect (Typ, Aspect_Aggregate);
2747
2748 Empty_Subp : Node_Id := Empty;
2749 Add_Named_Subp : Node_Id := Empty;
2750 Add_Unnamed_Subp : Node_Id := Empty;
2751 New_Indexed_Subp : Node_Id := Empty;
2752 Assign_Indexed_Subp : Node_Id := Empty;
2753
2754 ----------------------------------
2755 -- Resolve_Iterated_Association --
2756 ----------------------------------
2757
2758 procedure Resolve_Iterated_Association
2759 (Comp : Node_Id;
2760 Key_Type : Entity_Id;
2761 Elmt_Type : Entity_Id)
2762 is
2763 Choice : Node_Id;
2764 Ent : Entity_Id;
2765 Expr : Node_Id;
2766 Key_Expr : Node_Id;
2767 Id : Entity_Id;
2768 Id_Name : Name_Id;
2769 Iter : Node_Id;
2770 Typ : Entity_Id := Empty;
2771
2772 begin
2773 -- If this is an Iterated_Element_Association then either a
2774 -- an Iterator_Specification or a Loop_Parameter specification
2775 -- is present. In both cases a Key_Expression is present.
2776
2777 if Nkind (Comp) = N_Iterated_Element_Association then
2778 if Present (Loop_Parameter_Specification (Comp)) then
2779 Analyze_Loop_Parameter_Specification
2780 (Loop_Parameter_Specification (Comp));
2781 Id_Name := Chars (Defining_Identifier
2782 (Loop_Parameter_Specification (Comp)));
2783 else
2784 Iter := Copy_Separate_Tree (Iterator_Specification (Comp));
2785 Analyze (Iter);
2786 Typ := Etype (Defining_Identifier (Iter));
2787 Id_Name := Chars (Defining_Identifier
2788 (Iterator_Specification (Comp)));
2789 end if;
2790
2791 -- Key expression must have the type of the key. We analyze
2792 -- a copy of the original expression, because it will be
2793 -- reanalyzed and copied as needed during expansion of the
2794 -- corresponding loop.
2795
2796 Key_Expr := Key_Expression (Comp);
2797 Analyze_And_Resolve (New_Copy_Tree (Key_Expr), Key_Type);
2798
2799 elsif Present (Iterator_Specification (Comp)) then
2800 Iter := Copy_Separate_Tree (Iterator_Specification (Comp));
2801 Id_Name := Chars (Defining_Identifier (Comp));
2802 Analyze (Iter);
2803 Typ := Etype (Defining_Identifier (Iter));
2804
2805 else
2806 Choice := First (Discrete_Choices (Comp));
2807
2808 while Present (Choice) loop
2809 Analyze (Choice);
2810
2811 -- Choice can be a subtype name, a range, or an expression
2812
2813 if Is_Entity_Name (Choice)
2814 and then Is_Type (Entity (Choice))
2815 and then Base_Type (Entity (Choice)) = Base_Type (Key_Type)
2816 then
2817 null;
2818
2819 elsif Present (Key_Type) then
2820 Analyze_And_Resolve (Choice, Key_Type);
2821
2822 else
2823 Typ := Etype (Choice); -- assume unique for now
2824 end if;
2825
2826 Next (Choice);
2827 end loop;
2828
2829 Id_Name := Chars (Defining_Identifier (Comp));
2830 end if;
2831
2832 -- Create a scope in which to introduce an index, which is usually
2833 -- visible in the expression for the component, and needed for its
2834 -- analysis.
2835
2836 Id := Make_Defining_Identifier (Sloc (Comp), Id_Name);
2837 Ent := New_Internal_Entity (E_Loop, Current_Scope, Sloc (Comp), 'L');
2838 Set_Etype (Ent, Standard_Void_Type);
2839 Set_Parent (Ent, Parent (Comp));
2840 Push_Scope (Ent);
2841
2842 -- Insert and decorate the loop variable in the current scope.
2843 -- The expression has to be analyzed once the loop variable is
2844 -- directly visible. Mark the variable as referenced to prevent
2845 -- spurious warnings, given that subsequent uses of its name in the
2846 -- expression will reference the internal (synonym) loop variable.
2847
2848 Enter_Name (Id);
2849
2850 if No (Key_Type) then
2851 pragma Assert (Present (Typ));
2852 Set_Etype (Id, Typ);
2853 else
2854 Set_Etype (Id, Key_Type);
2855 end if;
2856
2857 Set_Ekind (Id, E_Variable);
2858 Set_Scope (Id, Ent);
2859 Set_Referenced (Id);
2860
2861 -- Analyze a copy of the expression, to verify legality. We use
2862 -- a copy because the expression will be analyzed anew when the
2863 -- enclosing aggregate is expanded, and the construct is rewritten
2864 -- as a loop with a new index variable.
2865
2866 Expr := New_Copy_Tree (Expression (Comp));
2867 Preanalyze_And_Resolve (Expr, Elmt_Type);
2868 End_Scope;
2869
2870 end Resolve_Iterated_Association;
2871
2872 begin
2873 pragma Assert (Nkind (Asp) = N_Aggregate);
2874
2875 Set_Etype (N, Typ);
2876 Parse_Aspect_Aggregate (Asp,
2877 Empty_Subp, Add_Named_Subp, Add_Unnamed_Subp,
2878 New_Indexed_Subp, Assign_Indexed_Subp);
2879
2880 if Present (Add_Unnamed_Subp)
2881 and then No (New_Indexed_Subp)
2882 then
2883 declare
2884 Elmt_Type : constant Entity_Id :=
2885 Etype (Next_Formal
2886 (First_Formal (Entity (Add_Unnamed_Subp))));
2887 Comp : Node_Id;
2888
2889 begin
2890 if Present (Expressions (N)) then
2891 -- positional aggregate
2892
2893 Comp := First (Expressions (N));
2894 while Present (Comp) loop
2895 Analyze_And_Resolve (Comp, Elmt_Type);
2896 Next (Comp);
2897 end loop;
2898 end if;
2899
2900 -- Empty aggregate, to be replaced by Empty during
2901 -- expansion, or iterated component association.
2902
2903 if Present (Component_Associations (N)) then
2904 declare
2905 Comp : Node_Id := First (Component_Associations (N));
2906 begin
2907 while Present (Comp) loop
2908 if Nkind (Comp) /=
2909 N_Iterated_Component_Association
2910 then
2911 Error_Msg_N ("illegal component association "
2912 & "for unnamed container aggregate", Comp);
2913 return;
2914 else
2915 Resolve_Iterated_Association
2916 (Comp, Empty, Elmt_Type);
2917 end if;
2918
2919 Next (Comp);
2920 end loop;
2921 end;
2922 end if;
2923 end;
2924
2925 elsif Present (Add_Named_Subp) then
2926 declare
2927 -- Retrieves types of container, key, and element from the
2928 -- specified insertion procedure.
2929
2930 Container : constant Entity_Id :=
2931 First_Formal (Entity (Add_Named_Subp));
2932 Key_Type : constant Entity_Id := Etype (Next_Formal (Container));
2933 Elmt_Type : constant Entity_Id :=
2934 Etype (Next_Formal (Next_Formal (Container)));
2935 Comp : Node_Id;
2936 Choice : Node_Id;
2937
2938 begin
2939 Comp := First (Component_Associations (N));
2940 while Present (Comp) loop
2941 if Nkind (Comp) = N_Component_Association then
2942 Choice := First (Choices (Comp));
2943
2944 while Present (Choice) loop
2945 Analyze_And_Resolve (Choice, Key_Type);
2946 if not Is_Static_Expression (Choice) then
2947 Error_Msg_N ("Choice must be static", Choice);
2948 end if;
2949
2950 Next (Choice);
2951 end loop;
2952
2953 Analyze_And_Resolve (Expression (Comp), Elmt_Type);
2954
2955 elsif Nkind (Comp) in
2956 N_Iterated_Component_Association |
2957 N_Iterated_Element_Association
2958 then
2959 Resolve_Iterated_Association
2960 (Comp, Key_Type, Elmt_Type);
2961 end if;
2962
2963 Next (Comp);
2964 end loop;
2965 end;
2966
2967 else
2968 -- Indexed Aggregate. Positional or indexed component
2969 -- can be present, but not both. Choices must be static
2970 -- values or ranges with static bounds.
2971
2972 declare
2973 Container : constant Entity_Id :=
2974 First_Formal (Entity (Assign_Indexed_Subp));
2975 Index_Type : constant Entity_Id := Etype (Next_Formal (Container));
2976 Comp_Type : constant Entity_Id :=
2977 Etype (Next_Formal (Next_Formal (Container)));
2978 Comp : Node_Id;
2979 Choice : Node_Id;
2980
2981 begin
2982 if Present (Expressions (N)) then
2983 Comp := First (Expressions (N));
2984 while Present (Comp) loop
2985 Analyze_And_Resolve (Comp, Comp_Type);
2986 Next (Comp);
2987 end loop;
2988 end if;
2989
2990 if Present (Component_Associations (N)) then
2991 if Present (Expressions (N)) then
2992 Error_Msg_N ("Container aggregate cannot be "
2993 & "both positional and named", N);
2994 return;
2995 end if;
2996
2997 Comp := First (Expressions (N));
2998
2999 while Present (Comp) loop
3000 if Nkind (Comp) = N_Component_Association then
3001 Choice := First (Choices (Comp));
3002
3003 while Present (Choice) loop
3004 Analyze_And_Resolve (Choice, Index_Type);
3005 Next (Choice);
3006 end loop;
3007
3008 Analyze_And_Resolve (Expression (Comp), Comp_Type);
3009
3010 elsif Nkind (Comp) in
3011 N_Iterated_Component_Association |
3012 N_Iterated_Element_Association
3013 then
3014 Resolve_Iterated_Association
3015 (Comp, Index_Type, Comp_Type);
3016 end if;
3017
3018 Next (Comp);
3019 end loop;
3020 end if;
3021 end;
3022 end if;
3023 end Resolve_Container_Aggregate;
3024
3025 -----------------------------
3026 -- Resolve_Delta_Aggregate --
3027 -----------------------------
3028
3029 procedure Resolve_Delta_Aggregate (N : Node_Id; Typ : Entity_Id) is
3030 Base : constant Node_Id := Expression (N);
3031
3032 begin
3033 if Ada_Version < Ada_2020 then
3034 Error_Msg_N ("delta_aggregate is an Ada 202x feature", N);
3035 Error_Msg_N ("\compile with -gnat2020", N);
3036 end if;
3037
3038 if not Is_Composite_Type (Typ) then
3039 Error_Msg_N ("not a composite type", N);
3040 end if;
3041
3042 Analyze_And_Resolve (Base, Typ);
3043
3044 if Is_Array_Type (Typ) then
3045 Resolve_Delta_Array_Aggregate (N, Typ);
3046 else
3047 Resolve_Delta_Record_Aggregate (N, Typ);
3048 end if;
3049
3050 Set_Etype (N, Typ);
3051 end Resolve_Delta_Aggregate;
3052
3053 -----------------------------------
3054 -- Resolve_Delta_Array_Aggregate --
3055 -----------------------------------
3056
3057 procedure Resolve_Delta_Array_Aggregate (N : Node_Id; Typ : Entity_Id) is
3058 Deltas : constant List_Id := Component_Associations (N);
3059 Index_Type : constant Entity_Id := Etype (First_Index (Typ));
3060
3061 Assoc : Node_Id;
3062 Choice : Node_Id;
3063 Expr : Node_Id;
3064
3065 begin
3066 Assoc := First (Deltas);
3067 while Present (Assoc) loop
3068 if Nkind (Assoc) = N_Iterated_Component_Association then
3069 Choice := First (Choice_List (Assoc));
3070 while Present (Choice) loop
3071 if Nkind (Choice) = N_Others_Choice then
3072 Error_Msg_N
3073 ("others not allowed in delta aggregate", Choice);
3074
3075 else
3076 Analyze_And_Resolve (Choice, Index_Type);
3077 end if;
3078
3079 Next (Choice);
3080 end loop;
3081
3082 declare
3083 Id : constant Entity_Id := Defining_Identifier (Assoc);
3084 Ent : constant Entity_Id :=
3085 New_Internal_Entity
3086 (E_Loop, Current_Scope, Sloc (Assoc), 'L');
3087
3088 begin
3089 Set_Etype (Ent, Standard_Void_Type);
3090 Set_Parent (Ent, Assoc);
3091 Push_Scope (Ent);
3092
3093 if No (Scope (Id)) then
3094 Set_Etype (Id, Index_Type);
3095 Set_Ekind (Id, E_Variable);
3096 Set_Scope (Id, Ent);
3097 end if;
3098 Enter_Name (Id);
3099
3100 -- Resolve a copy of the expression, after setting
3101 -- its parent properly to preserve its context.
3102
3103 Expr := New_Copy_Tree (Expression (Assoc));
3104 Set_Parent (Expr, Assoc);
3105 Analyze_And_Resolve (Expr, Component_Type (Typ));
3106 End_Scope;
3107 end;
3108
3109 else
3110 Choice := First (Choice_List (Assoc));
3111 while Present (Choice) loop
3112 if Nkind (Choice) = N_Others_Choice then
3113 Error_Msg_N
3114 ("others not allowed in delta aggregate", Choice);
3115
3116 else
3117 Analyze (Choice);
3118
3119 if Is_Entity_Name (Choice)
3120 and then Is_Type (Entity (Choice))
3121 then
3122 -- Choice covers a range of values
3123
3124 if Base_Type (Entity (Choice)) /=
3125 Base_Type (Index_Type)
3126 then
3127 Error_Msg_NE
3128 ("choice does not match index type of &",
3129 Choice, Typ);
3130 end if;
3131 else
3132 Resolve (Choice, Index_Type);
3133 end if;
3134 end if;
3135
3136 Next (Choice);
3137 end loop;
3138
3139 Analyze_And_Resolve (Expression (Assoc), Component_Type (Typ));
3140 end if;
3141
3142 Next (Assoc);
3143 end loop;
3144 end Resolve_Delta_Array_Aggregate;
3145
3146 ------------------------------------
3147 -- Resolve_Delta_Record_Aggregate --
3148 ------------------------------------
3149
3150 procedure Resolve_Delta_Record_Aggregate (N : Node_Id; Typ : Entity_Id) is
3151
3152 -- Variables used to verify that discriminant-dependent components
3153 -- appear in the same variant.
3154
3155 Comp_Ref : Entity_Id := Empty; -- init to avoid warning
3156 Variant : Node_Id;
3157
3158 procedure Check_Variant (Id : Entity_Id);
3159 -- If a given component of the delta aggregate appears in a variant
3160 -- part, verify that it is within the same variant as that of previous
3161 -- specified variant components of the delta.
3162
3163 function Get_Component (Nam : Node_Id) return Entity_Id;
3164 -- Locate component with a given name and return it. If none found then
3165 -- report error and return Empty.
3166
3167 function Nested_In (V1 : Node_Id; V2 : Node_Id) return Boolean;
3168 -- Determine whether variant V1 is within variant V2
3169
3170 function Variant_Depth (N : Node_Id) return Integer;
3171 -- Determine the distance of a variant to the enclosing type
3172 -- declaration.
3173
3174 --------------------
3175 -- Check_Variant --
3176 --------------------
3177
3178 procedure Check_Variant (Id : Entity_Id) is
3179 Comp : Entity_Id;
3180 Comp_Variant : Node_Id;
3181
3182 begin
3183 if not Has_Discriminants (Typ) then
3184 return;
3185 end if;
3186
3187 Comp := First_Entity (Typ);
3188 while Present (Comp) loop
3189 exit when Chars (Comp) = Chars (Id);
3190 Next_Component (Comp);
3191 end loop;
3192
3193 -- Find the variant, if any, whose component list includes the
3194 -- component declaration.
3195
3196 Comp_Variant := Parent (Parent (List_Containing (Parent (Comp))));
3197 if Nkind (Comp_Variant) = N_Variant then
3198 if No (Variant) then
3199 Variant := Comp_Variant;
3200 Comp_Ref := Comp;
3201
3202 elsif Variant /= Comp_Variant then
3203 declare
3204 D1 : constant Integer := Variant_Depth (Variant);
3205 D2 : constant Integer := Variant_Depth (Comp_Variant);
3206
3207 begin
3208 if D1 = D2
3209 or else
3210 (D1 > D2 and then not Nested_In (Variant, Comp_Variant))
3211 or else
3212 (D2 > D1 and then not Nested_In (Comp_Variant, Variant))
3213 then
3214 pragma Assert (Present (Comp_Ref));
3215 Error_Msg_Node_2 := Comp_Ref;
3216 Error_Msg_NE
3217 ("& and & appear in different variants", Id, Comp);
3218
3219 -- Otherwise retain the deeper variant for subsequent tests
3220
3221 elsif D2 > D1 then
3222 Variant := Comp_Variant;
3223 end if;
3224 end;
3225 end if;
3226 end if;
3227 end Check_Variant;
3228
3229 -------------------
3230 -- Get_Component --
3231 -------------------
3232
3233 function Get_Component (Nam : Node_Id) return Entity_Id is
3234 Comp : Entity_Id;
3235
3236 begin
3237 Comp := First_Entity (Typ);
3238 while Present (Comp) loop
3239 if Chars (Comp) = Chars (Nam) then
3240 if Ekind (Comp) = E_Discriminant then
3241 Error_Msg_N ("delta cannot apply to discriminant", Nam);
3242 end if;
3243
3244 return Comp;
3245 end if;
3246
3247 Next_Entity (Comp);
3248 end loop;
3249
3250 Error_Msg_NE ("type& has no component with this name", Nam, Typ);
3251 return Empty;
3252 end Get_Component;
3253
3254 ---------------
3255 -- Nested_In --
3256 ---------------
3257
3258 function Nested_In (V1, V2 : Node_Id) return Boolean is
3259 Par : Node_Id;
3260
3261 begin
3262 Par := Parent (V1);
3263 while Nkind (Par) /= N_Full_Type_Declaration loop
3264 if Par = V2 then
3265 return True;
3266 end if;
3267
3268 Par := Parent (Par);
3269 end loop;
3270
3271 return False;
3272 end Nested_In;
3273
3274 -------------------
3275 -- Variant_Depth --
3276 -------------------
3277
3278 function Variant_Depth (N : Node_Id) return Integer is
3279 Depth : Integer;
3280 Par : Node_Id;
3281
3282 begin
3283 Depth := 0;
3284 Par := Parent (N);
3285 while Nkind (Par) /= N_Full_Type_Declaration loop
3286 Depth := Depth + 1;
3287 Par := Parent (Par);
3288 end loop;
3289
3290 return Depth;
3291 end Variant_Depth;
3292
3293 -- Local variables
3294
3295 Deltas : constant List_Id := Component_Associations (N);
3296
3297 Assoc : Node_Id;
3298 Choice : Node_Id;
3299 Comp : Entity_Id;
3300 Comp_Type : Entity_Id := Empty; -- init to avoid warning
3301
3302 -- Start of processing for Resolve_Delta_Record_Aggregate
3303
3304 begin
3305 Variant := Empty;
3306
3307 Assoc := First (Deltas);
3308 while Present (Assoc) loop
3309 Choice := First (Choice_List (Assoc));
3310 while Present (Choice) loop
3311 Comp := Get_Component (Choice);
3312
3313 if Present (Comp) then
3314 Check_Variant (Choice);
3315
3316 Comp_Type := Etype (Comp);
3317
3318 -- Decorate the component reference by setting its entity and
3319 -- type, as otherwise backends like GNATprove would have to
3320 -- rediscover this information by themselves.
3321
3322 Set_Entity (Choice, Comp);
3323 Set_Etype (Choice, Comp_Type);
3324 else
3325 Comp_Type := Any_Type;
3326 end if;
3327
3328 Next (Choice);
3329 end loop;
3330
3331 pragma Assert (Present (Comp_Type));
3332 Analyze_And_Resolve (Expression (Assoc), Comp_Type);
3333 Next (Assoc);
3334 end loop;
3335 end Resolve_Delta_Record_Aggregate;
3336
3337 ---------------------------------
3338 -- Resolve_Extension_Aggregate --
3339 ---------------------------------
3340
3341 -- There are two cases to consider:
3342
3343 -- a) If the ancestor part is a type mark, the components needed are the
3344 -- difference between the components of the expected type and the
3345 -- components of the given type mark.
3346
3347 -- b) If the ancestor part is an expression, it must be unambiguous, and
3348 -- once we have its type we can also compute the needed components as in
3349 -- the previous case. In both cases, if the ancestor type is not the
3350 -- immediate ancestor, we have to build this ancestor recursively.
3351
3352 -- In both cases, discriminants of the ancestor type do not play a role in
3353 -- the resolution of the needed components, because inherited discriminants
3354 -- cannot be used in a type extension. As a result we can compute
3355 -- independently the list of components of the ancestor type and of the
3356 -- expected type.
3357
3358 procedure Resolve_Extension_Aggregate (N : Node_Id; Typ : Entity_Id) is
3359 A : constant Node_Id := Ancestor_Part (N);
3360 A_Type : Entity_Id;
3361 I : Interp_Index;
3362 It : Interp;
3363
3364 function Valid_Limited_Ancestor (Anc : Node_Id) return Boolean;
3365 -- If the type is limited, verify that the ancestor part is a legal
3366 -- expression (aggregate or function call, including 'Input)) that does
3367 -- not require a copy, as specified in 7.5(2).
3368
3369 function Valid_Ancestor_Type return Boolean;
3370 -- Verify that the type of the ancestor part is a non-private ancestor
3371 -- of the expected type, which must be a type extension.
3372
3373 procedure Transform_BIP_Assignment (Typ : Entity_Id);
3374 -- For an extension aggregate whose ancestor part is a build-in-place
3375 -- call returning a nonlimited type, this is used to transform the
3376 -- assignment to the ancestor part to use a temp.
3377
3378 ----------------------------
3379 -- Valid_Limited_Ancestor --
3380 ----------------------------
3381
3382 function Valid_Limited_Ancestor (Anc : Node_Id) return Boolean is
3383 begin
3384 if Is_Entity_Name (Anc) and then Is_Type (Entity (Anc)) then
3385 return True;
3386
3387 -- The ancestor must be a call or an aggregate, but a call may
3388 -- have been expanded into a temporary, so check original node.
3389
3390 elsif Nkind (Anc) in N_Aggregate
3391 | N_Extension_Aggregate
3392 | N_Function_Call
3393 then
3394 return True;
3395
3396 elsif Nkind (Original_Node (Anc)) = N_Function_Call then
3397 return True;
3398
3399 elsif Nkind (Anc) = N_Attribute_Reference
3400 and then Attribute_Name (Anc) = Name_Input
3401 then
3402 return True;
3403
3404 elsif Nkind (Anc) = N_Qualified_Expression then
3405 return Valid_Limited_Ancestor (Expression (Anc));
3406
3407 elsif Nkind (Anc) = N_Raise_Expression then
3408 return True;
3409
3410 else
3411 return False;
3412 end if;
3413 end Valid_Limited_Ancestor;
3414
3415 -------------------------
3416 -- Valid_Ancestor_Type --
3417 -------------------------
3418
3419 function Valid_Ancestor_Type return Boolean is
3420 Imm_Type : Entity_Id;
3421
3422 begin
3423 Imm_Type := Base_Type (Typ);
3424 while Is_Derived_Type (Imm_Type) loop
3425 if Etype (Imm_Type) = Base_Type (A_Type) then
3426 return True;
3427
3428 -- The base type of the parent type may appear as a private
3429 -- extension if it is declared as such in a parent unit of the
3430 -- current one. For consistency of the subsequent analysis use
3431 -- the partial view for the ancestor part.
3432
3433 elsif Is_Private_Type (Etype (Imm_Type))
3434 and then Present (Full_View (Etype (Imm_Type)))
3435 and then Base_Type (A_Type) = Full_View (Etype (Imm_Type))
3436 then
3437 A_Type := Etype (Imm_Type);
3438 return True;
3439
3440 -- The parent type may be a private extension. The aggregate is
3441 -- legal if the type of the aggregate is an extension of it that
3442 -- is not a private extension.
3443
3444 elsif Is_Private_Type (A_Type)
3445 and then not Is_Private_Type (Imm_Type)
3446 and then Present (Full_View (A_Type))
3447 and then Base_Type (Full_View (A_Type)) = Etype (Imm_Type)
3448 then
3449 return True;
3450
3451 -- The parent type may be a raise expression (which is legal in
3452 -- any expression context).
3453
3454 elsif A_Type = Raise_Type then
3455 A_Type := Etype (Imm_Type);
3456 return True;
3457
3458 else
3459 Imm_Type := Etype (Base_Type (Imm_Type));
3460 end if;
3461 end loop;
3462
3463 -- If previous loop did not find a proper ancestor, report error
3464
3465 Error_Msg_NE ("expect ancestor type of &", A, Typ);
3466 return False;
3467 end Valid_Ancestor_Type;
3468
3469 ------------------------------
3470 -- Transform_BIP_Assignment --
3471 ------------------------------
3472
3473 procedure Transform_BIP_Assignment (Typ : Entity_Id) is
3474 Loc : constant Source_Ptr := Sloc (N);
3475 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'Y', A);
3476 Obj_Decl : constant Node_Id :=
3477 Make_Object_Declaration (Loc,
3478 Defining_Identifier => Def_Id,
3479 Constant_Present => True,
3480 Object_Definition => New_Occurrence_Of (Typ, Loc),
3481 Expression => A,
3482 Has_Init_Expression => True);
3483 begin
3484 Set_Etype (Def_Id, Typ);
3485 Set_Ancestor_Part (N, New_Occurrence_Of (Def_Id, Loc));
3486 Insert_Action (N, Obj_Decl);
3487 end Transform_BIP_Assignment;
3488
3489 -- Start of processing for Resolve_Extension_Aggregate
3490
3491 begin
3492 -- Analyze the ancestor part and account for the case where it is a
3493 -- parameterless function call.
3494
3495 Analyze (A);
3496 Check_Parameterless_Call (A);
3497
3498 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
3499
3500 -- AI05-0115: If the ancestor part is a subtype mark, the ancestor
3501 -- must not have unknown discriminants. To catch cases where the
3502 -- aggregate occurs at a place where the full view of the ancestor
3503 -- type is visible and doesn't have unknown discriminants, but the
3504 -- aggregate type was derived from a partial view that has unknown
3505 -- discriminants, we check whether the aggregate type has unknown
3506 -- discriminants (unknown discriminants were inherited), along
3507 -- with checking that the partial view of the ancestor has unknown
3508 -- discriminants. (It might be sufficient to replace the entire
3509 -- condition with Has_Unknown_Discriminants (Typ), but that might
3510 -- miss some cases, not clear, and causes error changes in some tests
3511 -- such as class-wide cases, that aren't clearly improvements. ???)
3512
3513 if Has_Unknown_Discriminants (Entity (A))
3514 or else (Has_Unknown_Discriminants (Typ)
3515 and then Partial_View_Has_Unknown_Discr (Entity (A)))
3516 then
3517 Error_Msg_NE
3518 ("aggregate not available for type& whose ancestor "
3519 & "has unknown discriminants", N, Typ);
3520 end if;
3521 end if;
3522
3523 if not Is_Tagged_Type (Typ) then
3524 Error_Msg_N ("type of extension aggregate must be tagged", N);
3525 return;
3526
3527 elsif Is_Limited_Type (Typ) then
3528
3529 -- Ada 2005 (AI-287): Limited aggregates are allowed
3530
3531 if Ada_Version < Ada_2005 then
3532 Error_Msg_N ("aggregate type cannot be limited", N);
3533 Explain_Limited_Type (Typ, N);
3534 return;
3535
3536 elsif Valid_Limited_Ancestor (A) then
3537 null;
3538
3539 else
3540 Error_Msg_N
3541 ("limited ancestor part must be aggregate or function call", A);
3542 end if;
3543
3544 elsif Is_Class_Wide_Type (Typ) then
3545 Error_Msg_N ("aggregate cannot be of a class-wide type", N);
3546 return;
3547 end if;
3548
3549 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
3550 A_Type := Get_Full_View (Entity (A));
3551
3552 if Valid_Ancestor_Type then
3553 Set_Entity (A, A_Type);
3554 Set_Etype (A, A_Type);
3555
3556 Validate_Ancestor_Part (N);
3557 Resolve_Record_Aggregate (N, Typ);
3558 end if;
3559
3560 elsif Nkind (A) /= N_Aggregate then
3561 if Is_Overloaded (A) then
3562 A_Type := Any_Type;
3563
3564 Get_First_Interp (A, I, It);
3565 while Present (It.Typ) loop
3566
3567 -- Consider limited interpretations if Ada 2005 or higher
3568
3569 if Is_Tagged_Type (It.Typ)
3570 and then (Ada_Version >= Ada_2005
3571 or else not Is_Limited_Type (It.Typ))
3572 then
3573 if A_Type /= Any_Type then
3574 Error_Msg_N ("cannot resolve expression", A);
3575 return;
3576 else
3577 A_Type := It.Typ;
3578 end if;
3579 end if;
3580
3581 Get_Next_Interp (I, It);
3582 end loop;
3583
3584 if A_Type = Any_Type then
3585 if Ada_Version >= Ada_2005 then
3586 Error_Msg_N
3587 ("ancestor part must be of a tagged type", A);
3588 else
3589 Error_Msg_N
3590 ("ancestor part must be of a nonlimited tagged type", A);
3591 end if;
3592
3593 return;
3594 end if;
3595
3596 else
3597 A_Type := Etype (A);
3598 end if;
3599
3600 if Valid_Ancestor_Type then
3601 Resolve (A, A_Type);
3602 Check_Unset_Reference (A);
3603 Check_Non_Static_Context (A);
3604
3605 -- The aggregate is illegal if the ancestor expression is a call
3606 -- to a function with a limited unconstrained result, unless the
3607 -- type of the aggregate is a null extension. This restriction
3608 -- was added in AI05-67 to simplify implementation.
3609
3610 if Nkind (A) = N_Function_Call
3611 and then Is_Limited_Type (A_Type)
3612 and then not Is_Null_Extension (Typ)
3613 and then not Is_Constrained (A_Type)
3614 then
3615 Error_Msg_N
3616 ("type of limited ancestor part must be constrained", A);
3617
3618 -- Reject the use of CPP constructors that leave objects partially
3619 -- initialized. For example:
3620
3621 -- type CPP_Root is tagged limited record ...
3622 -- pragma Import (CPP, CPP_Root);
3623
3624 -- type CPP_DT is new CPP_Root and Iface ...
3625 -- pragma Import (CPP, CPP_DT);
3626
3627 -- type Ada_DT is new CPP_DT with ...
3628
3629 -- Obj : Ada_DT := Ada_DT'(New_CPP_Root with others => <>);
3630
3631 -- Using the constructor of CPP_Root the slots of the dispatch
3632 -- table of CPP_DT cannot be set, and the secondary tag of
3633 -- CPP_DT is unknown.
3634
3635 elsif Nkind (A) = N_Function_Call
3636 and then Is_CPP_Constructor_Call (A)
3637 and then Enclosing_CPP_Parent (Typ) /= A_Type
3638 then
3639 Error_Msg_NE
3640 ("??must use 'C'P'P constructor for type &", A,
3641 Enclosing_CPP_Parent (Typ));
3642
3643 -- The following call is not needed if the previous warning
3644 -- is promoted to an error.
3645
3646 Resolve_Record_Aggregate (N, Typ);
3647
3648 elsif Is_Class_Wide_Type (Etype (A))
3649 and then Nkind (Original_Node (A)) = N_Function_Call
3650 then
3651 -- If the ancestor part is a dispatching call, it appears
3652 -- statically to be a legal ancestor, but it yields any member
3653 -- of the class, and it is not possible to determine whether
3654 -- it is an ancestor of the extension aggregate (much less
3655 -- which ancestor). It is not possible to determine the
3656 -- components of the extension part.
3657
3658 -- This check implements AI-306, which in fact was motivated by
3659 -- an AdaCore query to the ARG after this test was added.
3660
3661 Error_Msg_N ("ancestor part must be statically tagged", A);
3662 else
3663 -- We are using the build-in-place protocol, but we can't build
3664 -- in place, because we need to call the function before
3665 -- allocating the aggregate. Could do better for null
3666 -- extensions, and maybe for nondiscriminated types.
3667 -- This is wrong for limited, but those were wrong already.
3668
3669 if not Is_Limited_View (A_Type)
3670 and then Is_Build_In_Place_Function_Call (A)
3671 then
3672 Transform_BIP_Assignment (A_Type);
3673 end if;
3674
3675 Resolve_Record_Aggregate (N, Typ);
3676 end if;
3677 end if;
3678
3679 else
3680 Error_Msg_N ("no unique type for this aggregate", A);
3681 end if;
3682
3683 Check_Function_Writable_Actuals (N);
3684 end Resolve_Extension_Aggregate;
3685
3686 ------------------------------
3687 -- Resolve_Record_Aggregate --
3688 ------------------------------
3689
3690 procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id) is
3691 New_Assoc_List : constant List_Id := New_List;
3692 -- New_Assoc_List is the newly built list of N_Component_Association
3693 -- nodes.
3694
3695 Others_Etype : Entity_Id := Empty;
3696 -- This variable is used to save the Etype of the last record component
3697 -- that takes its value from the others choice. Its purpose is:
3698 --
3699 -- (a) make sure the others choice is useful
3700 --
3701 -- (b) make sure the type of all the components whose value is
3702 -- subsumed by the others choice are the same.
3703 --
3704 -- This variable is updated as a side effect of function Get_Value.
3705
3706 Box_Node : Node_Id := Empty;
3707 Is_Box_Present : Boolean := False;
3708 Others_Box : Natural := 0;
3709 -- Ada 2005 (AI-287): Variables used in case of default initialization
3710 -- to provide a functionality similar to Others_Etype. Box_Present
3711 -- indicates that the component takes its default initialization;
3712 -- Others_Box counts the number of components of the current aggregate
3713 -- (which may be a sub-aggregate of a larger one) that are default-
3714 -- initialized. A value of One indicates that an others_box is present.
3715 -- Any larger value indicates that the others_box is not redundant.
3716 -- These variables, similar to Others_Etype, are also updated as a side
3717 -- effect of function Get_Value. Box_Node is used to place a warning on
3718 -- a redundant others_box.
3719
3720 procedure Add_Association
3721 (Component : Entity_Id;
3722 Expr : Node_Id;
3723 Assoc_List : List_Id;
3724 Is_Box_Present : Boolean := False);
3725 -- Builds a new N_Component_Association node which associates Component
3726 -- to expression Expr and adds it to the association list being built,
3727 -- either New_Assoc_List, or the association being built for an inner
3728 -- aggregate.
3729
3730 procedure Add_Discriminant_Values
3731 (New_Aggr : Node_Id;
3732 Assoc_List : List_Id);
3733 -- The constraint to a component may be given by a discriminant of the
3734 -- enclosing type, in which case we have to retrieve its value, which is
3735 -- part of the enclosing aggregate. Assoc_List provides the discriminant
3736 -- associations of the current type or of some enclosing record.
3737
3738 function Discriminant_Present (Input_Discr : Entity_Id) return Boolean;
3739 -- If aggregate N is a regular aggregate this routine will return True.
3740 -- Otherwise, if N is an extension aggregate, then Input_Discr denotes
3741 -- a discriminant whose value may already have been specified by N's
3742 -- ancestor part. This routine checks whether this is indeed the case
3743 -- and if so returns False, signaling that no value for Input_Discr
3744 -- should appear in N's aggregate part. Also, in this case, the routine
3745 -- appends to New_Assoc_List the discriminant value specified in the
3746 -- ancestor part.
3747 --
3748 -- If the aggregate is in a context with expansion delayed, it will be
3749 -- reanalyzed. The inherited discriminant values must not be reinserted
3750 -- in the component list to prevent spurious errors, but they must be
3751 -- present on first analysis to build the proper subtype indications.
3752 -- The flag Inherited_Discriminant is used to prevent the re-insertion.
3753
3754 function Find_Private_Ancestor (Typ : Entity_Id) return Entity_Id;
3755 -- AI05-0115: Find earlier ancestor in the derivation chain that is
3756 -- derived from private view Typ. Whether the aggregate is legal depends
3757 -- on the current visibility of the type as well as that of the parent
3758 -- of the ancestor.
3759
3760 function Get_Value
3761 (Compon : Entity_Id;
3762 From : List_Id;
3763 Consider_Others_Choice : Boolean := False) return Node_Id;
3764 -- Given a record component stored in parameter Compon, this function
3765 -- returns its value as it appears in the list From, which is a list
3766 -- of N_Component_Association nodes.
3767 --
3768 -- If no component association has a choice for the searched component,
3769 -- the value provided by the others choice is returned, if there is one,
3770 -- and Consider_Others_Choice is set to true. Otherwise Empty is
3771 -- returned. If there is more than one component association giving a
3772 -- value for the searched record component, an error message is emitted
3773 -- and the first found value is returned.
3774 --
3775 -- If Consider_Others_Choice is set and the returned expression comes
3776 -- from the others choice, then Others_Etype is set as a side effect.
3777 -- An error message is emitted if the components taking their value from
3778 -- the others choice do not have same type.
3779
3780 procedure Propagate_Discriminants
3781 (Aggr : Node_Id;
3782 Assoc_List : List_Id);
3783 -- Nested components may themselves be discriminated types constrained
3784 -- by outer discriminants, whose values must be captured before the
3785 -- aggregate is expanded into assignments.
3786
3787 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Entity_Id);
3788 -- Analyzes and resolves expression Expr against the Etype of the
3789 -- Component. This routine also applies all appropriate checks to Expr.
3790 -- It finally saves a Expr in the newly created association list that
3791 -- will be attached to the final record aggregate. Note that if the
3792 -- Parent pointer of Expr is not set then Expr was produced with a
3793 -- New_Copy_Tree or some such.
3794
3795 procedure Rewrite_Range (Root_Type : Entity_Id; Rge : Node_Id);
3796 -- Rewrite a range node Rge when its bounds refer to non-stored
3797 -- discriminants from Root_Type, to replace them with the stored
3798 -- discriminant values. This is required in GNATprove mode, and is
3799 -- adopted in all modes to avoid special-casing GNATprove mode.
3800
3801 ---------------------
3802 -- Add_Association --
3803 ---------------------
3804
3805 procedure Add_Association
3806 (Component : Entity_Id;
3807 Expr : Node_Id;
3808 Assoc_List : List_Id;
3809 Is_Box_Present : Boolean := False)
3810 is
3811 Choice_List : constant List_Id := New_List;
3812 Loc : Source_Ptr;
3813
3814 begin
3815 -- If this is a box association the expression is missing, so use the
3816 -- Sloc of the aggregate itself for the new association.
3817
3818 pragma Assert (Present (Expr) xor Is_Box_Present);
3819
3820 if Present (Expr) then
3821 Loc := Sloc (Expr);
3822 else
3823 Loc := Sloc (N);
3824 end if;
3825
3826 Append_To (Choice_List, New_Occurrence_Of (Component, Loc));
3827
3828 Append_To (Assoc_List,
3829 Make_Component_Association (Loc,
3830 Choices => Choice_List,
3831 Expression => Expr,
3832 Box_Present => Is_Box_Present));
3833 end Add_Association;
3834
3835 -----------------------------
3836 -- Add_Discriminant_Values --
3837 -----------------------------
3838
3839 procedure Add_Discriminant_Values
3840 (New_Aggr : Node_Id;
3841 Assoc_List : List_Id)
3842 is
3843 Assoc : Node_Id;
3844 Discr : Entity_Id;
3845 Discr_Elmt : Elmt_Id;
3846 Discr_Val : Node_Id;
3847 Val : Entity_Id;
3848
3849 begin
3850 Discr := First_Discriminant (Etype (New_Aggr));
3851 Discr_Elmt := First_Elmt (Discriminant_Constraint (Etype (New_Aggr)));
3852 while Present (Discr_Elmt) loop
3853 Discr_Val := Node (Discr_Elmt);
3854
3855 -- If the constraint is given by a discriminant then it is a
3856 -- discriminant of an enclosing record, and its value has already
3857 -- been placed in the association list.
3858
3859 if Is_Entity_Name (Discr_Val)
3860 and then Ekind (Entity (Discr_Val)) = E_Discriminant
3861 then
3862 Val := Entity (Discr_Val);
3863
3864 Assoc := First (Assoc_List);
3865 while Present (Assoc) loop
3866 if Present (Entity (First (Choices (Assoc))))
3867 and then Entity (First (Choices (Assoc))) = Val
3868 then
3869 Discr_Val := Expression (Assoc);
3870 exit;
3871 end if;
3872
3873 Next (Assoc);
3874 end loop;
3875 end if;
3876
3877 Add_Association
3878 (Discr, New_Copy_Tree (Discr_Val),
3879 Component_Associations (New_Aggr));
3880
3881 -- If the discriminant constraint is a current instance, mark the
3882 -- current aggregate so that the self-reference can be expanded
3883 -- later. The constraint may refer to the subtype of aggregate, so
3884 -- use base type for comparison.
3885
3886 if Nkind (Discr_Val) = N_Attribute_Reference
3887 and then Is_Entity_Name (Prefix (Discr_Val))
3888 and then Is_Type (Entity (Prefix (Discr_Val)))
3889 and then Base_Type (Etype (N)) = Entity (Prefix (Discr_Val))
3890 then
3891 Set_Has_Self_Reference (N);
3892 end if;
3893
3894 Next_Elmt (Discr_Elmt);
3895 Next_Discriminant (Discr);
3896 end loop;
3897 end Add_Discriminant_Values;
3898
3899 --------------------------
3900 -- Discriminant_Present --
3901 --------------------------
3902
3903 function Discriminant_Present (Input_Discr : Entity_Id) return Boolean is
3904 Regular_Aggr : constant Boolean := Nkind (N) /= N_Extension_Aggregate;
3905
3906 Ancestor_Is_Subtyp : Boolean;
3907
3908 Loc : Source_Ptr;
3909
3910 Ancestor : Node_Id;
3911 Ancestor_Typ : Entity_Id;
3912 Comp_Assoc : Node_Id;
3913 Discr : Entity_Id;
3914 Discr_Expr : Node_Id;
3915 Discr_Val : Elmt_Id := No_Elmt;
3916 Orig_Discr : Entity_Id;
3917
3918 begin
3919 if Regular_Aggr then
3920 return True;
3921 end if;
3922
3923 -- Check whether inherited discriminant values have already been
3924 -- inserted in the aggregate. This will be the case if we are
3925 -- re-analyzing an aggregate whose expansion was delayed.
3926
3927 if Present (Component_Associations (N)) then
3928 Comp_Assoc := First (Component_Associations (N));
3929 while Present (Comp_Assoc) loop
3930 if Inherited_Discriminant (Comp_Assoc) then
3931 return True;
3932 end if;
3933
3934 Next (Comp_Assoc);
3935 end loop;
3936 end if;
3937
3938 Ancestor := Ancestor_Part (N);
3939 Ancestor_Typ := Etype (Ancestor);
3940 Loc := Sloc (Ancestor);
3941
3942 -- For a private type with unknown discriminants, use the underlying
3943 -- record view if it is available.
3944
3945 if Has_Unknown_Discriminants (Ancestor_Typ)
3946 and then Present (Full_View (Ancestor_Typ))
3947 and then Present (Underlying_Record_View (Full_View (Ancestor_Typ)))
3948 then
3949 Ancestor_Typ := Underlying_Record_View (Full_View (Ancestor_Typ));
3950 end if;
3951
3952 Ancestor_Is_Subtyp :=
3953 Is_Entity_Name (Ancestor) and then Is_Type (Entity (Ancestor));
3954
3955 -- If the ancestor part has no discriminants clearly N's aggregate
3956 -- part must provide a value for Discr.
3957
3958 if not Has_Discriminants (Ancestor_Typ) then
3959 return True;
3960
3961 -- If the ancestor part is an unconstrained subtype mark then the
3962 -- Discr must be present in N's aggregate part.
3963
3964 elsif Ancestor_Is_Subtyp
3965 and then not Is_Constrained (Entity (Ancestor))
3966 then
3967 return True;
3968 end if;
3969
3970 -- Now look to see if Discr was specified in the ancestor part
3971
3972 if Ancestor_Is_Subtyp then
3973 Discr_Val :=
3974 First_Elmt (Discriminant_Constraint (Entity (Ancestor)));
3975 end if;
3976
3977 Orig_Discr := Original_Record_Component (Input_Discr);
3978
3979 Discr := First_Discriminant (Ancestor_Typ);
3980 while Present (Discr) loop
3981
3982 -- If Ancestor has already specified Disc value then insert its
3983 -- value in the final aggregate.
3984
3985 if Original_Record_Component (Discr) = Orig_Discr then
3986 if Ancestor_Is_Subtyp then
3987 Discr_Expr := New_Copy_Tree (Node (Discr_Val));
3988 else
3989 Discr_Expr :=
3990 Make_Selected_Component (Loc,
3991 Prefix => Duplicate_Subexpr (Ancestor),
3992 Selector_Name => New_Occurrence_Of (Input_Discr, Loc));
3993 end if;
3994
3995 Resolve_Aggr_Expr (Discr_Expr, Input_Discr);
3996 Set_Inherited_Discriminant (Last (New_Assoc_List));
3997 return False;
3998 end if;
3999
4000 Next_Discriminant (Discr);
4001
4002 if Ancestor_Is_Subtyp then
4003 Next_Elmt (Discr_Val);
4004 end if;
4005 end loop;
4006
4007 return True;
4008 end Discriminant_Present;
4009
4010 ---------------------------
4011 -- Find_Private_Ancestor --
4012 ---------------------------
4013
4014 function Find_Private_Ancestor (Typ : Entity_Id) return Entity_Id is
4015 Par : Entity_Id;
4016
4017 begin
4018 Par := Typ;
4019 loop
4020 if Has_Private_Ancestor (Par)
4021 and then not Has_Private_Ancestor (Etype (Base_Type (Par)))
4022 then
4023 return Par;
4024
4025 elsif not Is_Derived_Type (Par) then
4026 return Empty;
4027
4028 else
4029 Par := Etype (Base_Type (Par));
4030 end if;
4031 end loop;
4032 end Find_Private_Ancestor;
4033
4034 ---------------
4035 -- Get_Value --
4036 ---------------
4037
4038 function Get_Value
4039 (Compon : Entity_Id;
4040 From : List_Id;
4041 Consider_Others_Choice : Boolean := False) return Node_Id
4042 is
4043 Typ : constant Entity_Id := Etype (Compon);
4044 Assoc : Node_Id;
4045 Expr : Node_Id := Empty;
4046 Selector_Name : Node_Id;
4047
4048 begin
4049 Is_Box_Present := False;
4050
4051 if No (From) then
4052 return Empty;
4053 end if;
4054
4055 Assoc := First (From);
4056 while Present (Assoc) loop
4057 Selector_Name := First (Choices (Assoc));
4058 while Present (Selector_Name) loop
4059 if Nkind (Selector_Name) = N_Others_Choice then
4060 if Consider_Others_Choice and then No (Expr) then
4061
4062 -- We need to duplicate the expression for each
4063 -- successive component covered by the others choice.
4064 -- This is redundant if the others_choice covers only
4065 -- one component (small optimization possible???), but
4066 -- indispensable otherwise, because each one must be
4067 -- expanded individually to preserve side effects.
4068
4069 -- Ada 2005 (AI-287): In case of default initialization
4070 -- of components, we duplicate the corresponding default
4071 -- expression (from the record type declaration). The
4072 -- copy must carry the sloc of the association (not the
4073 -- original expression) to prevent spurious elaboration
4074 -- checks when the default includes function calls.
4075
4076 if Box_Present (Assoc) then
4077 Others_Box := Others_Box + 1;
4078 Is_Box_Present := True;
4079
4080 if Expander_Active then
4081 return
4082 New_Copy_Tree_And_Copy_Dimensions
4083 (Expression (Parent (Compon)),
4084 New_Sloc => Sloc (Assoc));
4085 else
4086 return Expression (Parent (Compon));
4087 end if;
4088
4089 else
4090 if Present (Others_Etype)
4091 and then Base_Type (Others_Etype) /= Base_Type (Typ)
4092 then
4093 -- If the components are of an anonymous access
4094 -- type they are distinct, but this is legal in
4095 -- Ada 2012 as long as designated types match.
4096
4097 if (Ekind (Typ) = E_Anonymous_Access_Type
4098 or else Ekind (Typ) =
4099 E_Anonymous_Access_Subprogram_Type)
4100 and then Designated_Type (Typ) =
4101 Designated_Type (Others_Etype)
4102 then
4103 null;
4104 else
4105 Error_Msg_N
4106 ("components in OTHERS choice must have same "
4107 & "type", Selector_Name);
4108 end if;
4109 end if;
4110
4111 Others_Etype := Typ;
4112
4113 -- Copy the expression so that it is resolved
4114 -- independently for each component, This is needed
4115 -- for accessibility checks on components of anonymous
4116 -- access types, even in compile_only mode.
4117
4118 if not Inside_A_Generic then
4119 return
4120 New_Copy_Tree_And_Copy_Dimensions
4121 (Expression (Assoc));
4122 else
4123 return Expression (Assoc);
4124 end if;
4125 end if;
4126 end if;
4127
4128 elsif Chars (Compon) = Chars (Selector_Name) then
4129 if No (Expr) then
4130
4131 -- Ada 2005 (AI-231)
4132
4133 if Ada_Version >= Ada_2005
4134 and then Known_Null (Expression (Assoc))
4135 then
4136 Check_Can_Never_Be_Null (Compon, Expression (Assoc));
4137 end if;
4138
4139 -- We need to duplicate the expression when several
4140 -- components are grouped together with a "|" choice.
4141 -- For instance "filed1 | filed2 => Expr"
4142
4143 -- Ada 2005 (AI-287)
4144
4145 if Box_Present (Assoc) then
4146 Is_Box_Present := True;
4147
4148 -- Duplicate the default expression of the component
4149 -- from the record type declaration, so a new copy
4150 -- can be attached to the association.
4151
4152 -- Note that we always copy the default expression,
4153 -- even when the association has a single choice, in
4154 -- order to create a proper association for the
4155 -- expanded aggregate.
4156
4157 -- Component may have no default, in which case the
4158 -- expression is empty and the component is default-
4159 -- initialized, but an association for the component
4160 -- exists, and it is not covered by an others clause.
4161
4162 -- Scalar and private types have no initialization
4163 -- procedure, so they remain uninitialized. If the
4164 -- target of the aggregate is a constant this
4165 -- deserves a warning.
4166
4167 if No (Expression (Parent (Compon)))
4168 and then not Has_Non_Null_Base_Init_Proc (Typ)
4169 and then not Has_Aspect (Typ, Aspect_Default_Value)
4170 and then not Is_Concurrent_Type (Typ)
4171 and then Nkind (Parent (N)) = N_Object_Declaration
4172 and then Constant_Present (Parent (N))
4173 then
4174 Error_Msg_Node_2 := Typ;
4175 Error_Msg_NE
4176 ("component&? of type& is uninitialized",
4177 Assoc, Selector_Name);
4178
4179 -- An additional reminder if the component type
4180 -- is a generic formal.
4181
4182 if Is_Generic_Type (Base_Type (Typ)) then
4183 Error_Msg_NE
4184 ("\instance should provide actual type with "
4185 & "initialization for&", Assoc, Typ);
4186 end if;
4187 end if;
4188
4189 return
4190 New_Copy_Tree_And_Copy_Dimensions
4191 (Expression (Parent (Compon)));
4192
4193 else
4194 if Present (Next (Selector_Name)) then
4195 Expr := New_Copy_Tree_And_Copy_Dimensions
4196 (Expression (Assoc));
4197 else
4198 Expr := Expression (Assoc);
4199 end if;
4200 end if;
4201
4202 Generate_Reference (Compon, Selector_Name, 'm');
4203
4204 else
4205 Error_Msg_NE
4206 ("more than one value supplied for &",
4207 Selector_Name, Compon);
4208
4209 end if;
4210 end if;
4211
4212 Next (Selector_Name);
4213 end loop;
4214
4215 Next (Assoc);
4216 end loop;
4217
4218 return Expr;
4219 end Get_Value;
4220
4221 -----------------------------
4222 -- Propagate_Discriminants --
4223 -----------------------------
4224
4225 procedure Propagate_Discriminants
4226 (Aggr : Node_Id;
4227 Assoc_List : List_Id)
4228 is
4229 Loc : constant Source_Ptr := Sloc (N);
4230
4231 procedure Process_Component (Comp : Entity_Id);
4232 -- Add one component with a box association to the inner aggregate,
4233 -- and recurse if component is itself composite.
4234
4235 -----------------------
4236 -- Process_Component --
4237 -----------------------
4238
4239 procedure Process_Component (Comp : Entity_Id) is
4240 T : constant Entity_Id := Etype (Comp);
4241 New_Aggr : Node_Id;
4242
4243 begin
4244 if Is_Record_Type (T) and then Has_Discriminants (T) then
4245 New_Aggr := Make_Aggregate (Loc, No_List, New_List);
4246 Set_Etype (New_Aggr, T);
4247
4248 Add_Association
4249 (Comp, New_Aggr, Component_Associations (Aggr));
4250
4251 -- Collect discriminant values and recurse
4252
4253 Add_Discriminant_Values (New_Aggr, Assoc_List);
4254 Propagate_Discriminants (New_Aggr, Assoc_List);
4255
4256 Build_Constrained_Itype
4257 (New_Aggr, T, Component_Associations (New_Aggr));
4258 else
4259 Add_Association
4260 (Comp, Empty, Component_Associations (Aggr),
4261 Is_Box_Present => True);
4262 end if;
4263 end Process_Component;
4264
4265 -- Local variables
4266
4267 Aggr_Type : constant Entity_Id := Base_Type (Etype (Aggr));
4268 Components : constant Elist_Id := New_Elmt_List;
4269 Def_Node : constant Node_Id :=
4270 Type_Definition (Declaration_Node (Aggr_Type));
4271
4272 Comp : Node_Id;
4273 Comp_Elmt : Elmt_Id;
4274 Errors : Boolean;
4275
4276 -- Start of processing for Propagate_Discriminants
4277
4278 begin
4279 -- The component type may be a variant type. Collect the components
4280 -- that are ruled by the known values of the discriminants. Their
4281 -- values have already been inserted into the component list of the
4282 -- current aggregate.
4283
4284 if Nkind (Def_Node) = N_Record_Definition
4285 and then Present (Component_List (Def_Node))
4286 and then Present (Variant_Part (Component_List (Def_Node)))
4287 then
4288 Gather_Components (Aggr_Type,
4289 Component_List (Def_Node),
4290 Governed_By => Component_Associations (Aggr),
4291 Into => Components,
4292 Report_Errors => Errors);
4293
4294 Comp_Elmt := First_Elmt (Components);
4295 while Present (Comp_Elmt) loop
4296 if Ekind (Node (Comp_Elmt)) /= E_Discriminant then
4297 Process_Component (Node (Comp_Elmt));
4298 end if;
4299
4300 Next_Elmt (Comp_Elmt);
4301 end loop;
4302
4303 -- No variant part, iterate over all components
4304
4305 else
4306 Comp := First_Component (Etype (Aggr));
4307 while Present (Comp) loop
4308 Process_Component (Comp);
4309 Next_Component (Comp);
4310 end loop;
4311 end if;
4312 end Propagate_Discriminants;
4313
4314 -----------------------
4315 -- Resolve_Aggr_Expr --
4316 -----------------------
4317
4318 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Entity_Id) is
4319 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean;
4320 -- If the expression is an aggregate (possibly qualified) then its
4321 -- expansion is delayed until the enclosing aggregate is expanded
4322 -- into assignments. In that case, do not generate checks on the
4323 -- expression, because they will be generated later, and will other-
4324 -- wise force a copy (to remove side effects) that would leave a
4325 -- dynamic-sized aggregate in the code, something that gigi cannot
4326 -- handle.
4327
4328 ---------------------------
4329 -- Has_Expansion_Delayed --
4330 ---------------------------
4331
4332 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean is
4333 begin
4334 return
4335 (Nkind (Expr) in N_Aggregate | N_Extension_Aggregate
4336 and then Present (Etype (Expr))
4337 and then Is_Record_Type (Etype (Expr))
4338 and then Expansion_Delayed (Expr))
4339 or else
4340 (Nkind (Expr) = N_Qualified_Expression
4341 and then Has_Expansion_Delayed (Expression (Expr)));
4342 end Has_Expansion_Delayed;
4343
4344 -- Local variables
4345
4346 Expr_Type : Entity_Id := Empty;
4347 New_C : Entity_Id := Component;
4348 New_Expr : Node_Id;
4349
4350 Relocate : Boolean;
4351 -- Set to True if the resolved Expr node needs to be relocated when
4352 -- attached to the newly created association list. This node need not
4353 -- be relocated if its parent pointer is not set. In fact in this
4354 -- case Expr is the output of a New_Copy_Tree call. If Relocate is
4355 -- True then we have analyzed the expression node in the original
4356 -- aggregate and hence it needs to be relocated when moved over to
4357 -- the new association list.
4358
4359 -- Start of processing for Resolve_Aggr_Expr
4360
4361 begin
4362 -- If the type of the component is elementary or the type of the
4363 -- aggregate does not contain discriminants, use the type of the
4364 -- component to resolve Expr.
4365
4366 if Is_Elementary_Type (Etype (Component))
4367 or else not Has_Discriminants (Etype (N))
4368 then
4369 Expr_Type := Etype (Component);
4370
4371 -- Otherwise we have to pick up the new type of the component from
4372 -- the new constrained subtype of the aggregate. In fact components
4373 -- which are of a composite type might be constrained by a
4374 -- discriminant, and we want to resolve Expr against the subtype were
4375 -- all discriminant occurrences are replaced with their actual value.
4376
4377 else
4378 New_C := First_Component (Etype (N));
4379 while Present (New_C) loop
4380 if Chars (New_C) = Chars (Component) then
4381 Expr_Type := Etype (New_C);
4382 exit;
4383 end if;
4384
4385 Next_Component (New_C);
4386 end loop;
4387
4388 pragma Assert (Present (Expr_Type));
4389
4390 -- For each range in an array type where a discriminant has been
4391 -- replaced with the constraint, check that this range is within
4392 -- the range of the base type. This checks is done in the init
4393 -- proc for regular objects, but has to be done here for
4394 -- aggregates since no init proc is called for them.
4395
4396 if Is_Array_Type (Expr_Type) then
4397 declare
4398 Index : Node_Id;
4399 -- Range of the current constrained index in the array
4400
4401 Orig_Index : Node_Id := First_Index (Etype (Component));
4402 -- Range corresponding to the range Index above in the
4403 -- original unconstrained record type. The bounds of this
4404 -- range may be governed by discriminants.
4405
4406 Unconstr_Index : Node_Id := First_Index (Etype (Expr_Type));
4407 -- Range corresponding to the range Index above for the
4408 -- unconstrained array type. This range is needed to apply
4409 -- range checks.
4410
4411 begin
4412 Index := First_Index (Expr_Type);
4413 while Present (Index) loop
4414 if Depends_On_Discriminant (Orig_Index) then
4415 Apply_Range_Check (Index, Etype (Unconstr_Index));
4416 end if;
4417
4418 Next_Index (Index);
4419 Next_Index (Orig_Index);
4420 Next_Index (Unconstr_Index);
4421 end loop;
4422 end;
4423 end if;
4424 end if;
4425
4426 -- If the Parent pointer of Expr is not set, Expr is an expression
4427 -- duplicated by New_Tree_Copy (this happens for record aggregates
4428 -- that look like (Field1 | Filed2 => Expr) or (others => Expr)).
4429 -- Such a duplicated expression must be attached to the tree
4430 -- before analysis and resolution to enforce the rule that a tree
4431 -- fragment should never be analyzed or resolved unless it is
4432 -- attached to the current compilation unit.
4433
4434 if No (Parent (Expr)) then
4435 Set_Parent (Expr, N);
4436 Relocate := False;
4437 else
4438 Relocate := True;
4439 end if;
4440
4441 Analyze_And_Resolve (Expr, Expr_Type);
4442 Check_Expr_OK_In_Limited_Aggregate (Expr);
4443 Check_Non_Static_Context (Expr);
4444 Check_Unset_Reference (Expr);
4445
4446 -- Check wrong use of class-wide types
4447
4448 if Is_Class_Wide_Type (Etype (Expr)) then
4449 Error_Msg_N ("dynamically tagged expression not allowed", Expr);
4450 end if;
4451
4452 if not Has_Expansion_Delayed (Expr) then
4453 Aggregate_Constraint_Checks (Expr, Expr_Type);
4454 end if;
4455
4456 -- If an aggregate component has a type with predicates, an explicit
4457 -- predicate check must be applied, as for an assignment statement,
4458 -- because the aggregate might not be expanded into individual
4459 -- component assignments.
4460
4461 if Has_Predicates (Expr_Type)
4462 and then Analyzed (Expr)
4463 then
4464 Apply_Predicate_Check (Expr, Expr_Type);
4465 end if;
4466
4467 if Raises_Constraint_Error (Expr) then
4468 Set_Raises_Constraint_Error (N);
4469 end if;
4470
4471 -- If the expression has been marked as requiring a range check, then
4472 -- generate it here. It's a bit odd to be generating such checks in
4473 -- the analyzer, but harmless since Generate_Range_Check does nothing
4474 -- (other than making sure Do_Range_Check is set) if the expander is
4475 -- not active.
4476
4477 if Do_Range_Check (Expr) then
4478 Generate_Range_Check (Expr, Expr_Type, CE_Range_Check_Failed);
4479 end if;
4480
4481 -- Add association Component => Expr if the caller requests it
4482
4483 if Relocate then
4484 New_Expr := Relocate_Node (Expr);
4485
4486 -- Since New_Expr is not gonna be analyzed later on, we need to
4487 -- propagate here the dimensions form Expr to New_Expr.
4488
4489 Copy_Dimensions (Expr, New_Expr);
4490
4491 else
4492 New_Expr := Expr;
4493 end if;
4494
4495 Add_Association (New_C, New_Expr, New_Assoc_List);
4496 end Resolve_Aggr_Expr;
4497
4498 -------------------
4499 -- Rewrite_Range --
4500 -------------------
4501
4502 procedure Rewrite_Range (Root_Type : Entity_Id; Rge : Node_Id) is
4503 procedure Rewrite_Bound
4504 (Bound : Node_Id;
4505 Disc : Entity_Id;
4506 Expr_Disc : Node_Id);
4507 -- Rewrite a bound of the range Bound, when it is equal to the
4508 -- non-stored discriminant Disc, into the stored discriminant
4509 -- value Expr_Disc.
4510
4511 -------------------
4512 -- Rewrite_Bound --
4513 -------------------
4514
4515 procedure Rewrite_Bound
4516 (Bound : Node_Id;
4517 Disc : Entity_Id;
4518 Expr_Disc : Node_Id)
4519 is
4520 begin
4521 if Nkind (Bound) /= N_Identifier then
4522 return;
4523 end if;
4524
4525 -- We expect either the discriminant or the discriminal
4526
4527 if Entity (Bound) = Disc
4528 or else (Ekind (Entity (Bound)) = E_In_Parameter
4529 and then Discriminal_Link (Entity (Bound)) = Disc)
4530 then
4531 Rewrite (Bound, New_Copy_Tree (Expr_Disc));
4532 end if;
4533 end Rewrite_Bound;
4534
4535 -- Local variables
4536
4537 Low, High : Node_Id;
4538 Disc : Entity_Id;
4539 Expr_Disc : Elmt_Id;
4540
4541 -- Start of processing for Rewrite_Range
4542
4543 begin
4544 if Has_Discriminants (Root_Type) and then Nkind (Rge) = N_Range then
4545 Low := Low_Bound (Rge);
4546 High := High_Bound (Rge);
4547
4548 Disc := First_Discriminant (Root_Type);
4549 Expr_Disc := First_Elmt (Stored_Constraint (Etype (N)));
4550 while Present (Disc) loop
4551 Rewrite_Bound (Low, Disc, Node (Expr_Disc));
4552 Rewrite_Bound (High, Disc, Node (Expr_Disc));
4553 Next_Discriminant (Disc);
4554 Next_Elmt (Expr_Disc);
4555 end loop;
4556 end if;
4557 end Rewrite_Range;
4558
4559 -- Local variables
4560
4561 Components : constant Elist_Id := New_Elmt_List;
4562 -- Components is the list of the record components whose value must be
4563 -- provided in the aggregate. This list does include discriminants.
4564
4565 Component : Entity_Id;
4566 Component_Elmt : Elmt_Id;
4567 Expr : Node_Id;
4568 Positional_Expr : Node_Id;
4569
4570 -- Start of processing for Resolve_Record_Aggregate
4571
4572 begin
4573 -- A record aggregate is restricted in SPARK:
4574
4575 -- Each named association can have only a single choice.
4576 -- OTHERS cannot be used.
4577 -- Positional and named associations cannot be mixed.
4578
4579 if Present (Component_Associations (N))
4580 and then Present (First (Component_Associations (N)))
4581 then
4582 declare
4583 Assoc : Node_Id;
4584
4585 begin
4586 Assoc := First (Component_Associations (N));
4587 while Present (Assoc) loop
4588 if Nkind (Assoc) = N_Iterated_Component_Association then
4589 Error_Msg_N
4590 ("iterated component association can only appear in an "
4591 & "array aggregate", N);
4592 raise Unrecoverable_Error;
4593 end if;
4594
4595 Next (Assoc);
4596 end loop;
4597 end;
4598 end if;
4599
4600 -- We may end up calling Duplicate_Subexpr on expressions that are
4601 -- attached to New_Assoc_List. For this reason we need to attach it
4602 -- to the tree by setting its parent pointer to N. This parent point
4603 -- will change in STEP 8 below.
4604
4605 Set_Parent (New_Assoc_List, N);
4606
4607 -- STEP 1: abstract type and null record verification
4608
4609 if Is_Abstract_Type (Typ) then
4610 Error_Msg_N ("type of aggregate cannot be abstract", N);
4611 end if;
4612
4613 if No (First_Entity (Typ)) and then Null_Record_Present (N) then
4614 Set_Etype (N, Typ);
4615 return;
4616
4617 elsif Present (First_Entity (Typ))
4618 and then Null_Record_Present (N)
4619 and then not Is_Tagged_Type (Typ)
4620 then
4621 Error_Msg_N ("record aggregate cannot be null", N);
4622 return;
4623
4624 -- If the type has no components, then the aggregate should either
4625 -- have "null record", or in Ada 2005 it could instead have a single
4626 -- component association given by "others => <>". For Ada 95 we flag an
4627 -- error at this point, but for Ada 2005 we proceed with checking the
4628 -- associations below, which will catch the case where it's not an
4629 -- aggregate with "others => <>". Note that the legality of a <>
4630 -- aggregate for a null record type was established by AI05-016.
4631
4632 elsif No (First_Entity (Typ))
4633 and then Ada_Version < Ada_2005
4634 then
4635 Error_Msg_N ("record aggregate must be null", N);
4636 return;
4637 end if;
4638
4639 -- STEP 2: Verify aggregate structure
4640
4641 Step_2 : declare
4642 Assoc : Node_Id;
4643 Bad_Aggregate : Boolean := False;
4644 Selector_Name : Node_Id;
4645
4646 begin
4647 if Present (Component_Associations (N)) then
4648 Assoc := First (Component_Associations (N));
4649 else
4650 Assoc := Empty;
4651 end if;
4652
4653 while Present (Assoc) loop
4654 Selector_Name := First (Choices (Assoc));
4655 while Present (Selector_Name) loop
4656 if Nkind (Selector_Name) = N_Identifier then
4657 null;
4658
4659 elsif Nkind (Selector_Name) = N_Others_Choice then
4660 if Selector_Name /= First (Choices (Assoc))
4661 or else Present (Next (Selector_Name))
4662 then
4663 Error_Msg_N
4664 ("OTHERS must appear alone in a choice list",
4665 Selector_Name);
4666 return;
4667
4668 elsif Present (Next (Assoc)) then
4669 Error_Msg_N
4670 ("OTHERS must appear last in an aggregate",
4671 Selector_Name);
4672 return;
4673
4674 -- (Ada 2005): If this is an association with a box,
4675 -- indicate that the association need not represent
4676 -- any component.
4677
4678 elsif Box_Present (Assoc) then
4679 Others_Box := 1;
4680 Box_Node := Assoc;
4681 end if;
4682
4683 else
4684 Error_Msg_N
4685 ("selector name should be identifier or OTHERS",
4686 Selector_Name);
4687 Bad_Aggregate := True;
4688 end if;
4689
4690 Next (Selector_Name);
4691 end loop;
4692
4693 Next (Assoc);
4694 end loop;
4695
4696 if Bad_Aggregate then
4697 return;
4698 end if;
4699 end Step_2;
4700
4701 -- STEP 3: Find discriminant Values
4702
4703 Step_3 : declare
4704 Discrim : Entity_Id;
4705 Missing_Discriminants : Boolean := False;
4706
4707 begin
4708 if Present (Expressions (N)) then
4709 Positional_Expr := First (Expressions (N));
4710 else
4711 Positional_Expr := Empty;
4712 end if;
4713
4714 -- AI05-0115: if the ancestor part is a subtype mark, the ancestor
4715 -- must not have unknown discriminants.
4716 -- ??? We are not checking any subtype mark here and this code is not
4717 -- exercised by any test, so it's likely wrong (in particular
4718 -- we should not use Root_Type here but the subtype mark, if any),
4719 -- and possibly not needed.
4720
4721 if Is_Derived_Type (Typ)
4722 and then Has_Unknown_Discriminants (Root_Type (Typ))
4723 and then Nkind (N) /= N_Extension_Aggregate
4724 then
4725 Error_Msg_NE
4726 ("aggregate not available for type& whose ancestor "
4727 & "has unknown discriminants ", N, Typ);
4728 end if;
4729
4730 if Has_Unknown_Discriminants (Typ)
4731 and then Present (Underlying_Record_View (Typ))
4732 then
4733 Discrim := First_Discriminant (Underlying_Record_View (Typ));
4734 elsif Has_Discriminants (Typ) then
4735 Discrim := First_Discriminant (Typ);
4736 else
4737 Discrim := Empty;
4738 end if;
4739
4740 -- First find the discriminant values in the positional components
4741
4742 while Present (Discrim) and then Present (Positional_Expr) loop
4743 if Discriminant_Present (Discrim) then
4744 Resolve_Aggr_Expr (Positional_Expr, Discrim);
4745
4746 -- Ada 2005 (AI-231)
4747
4748 if Ada_Version >= Ada_2005
4749 and then Known_Null (Positional_Expr)
4750 then
4751 Check_Can_Never_Be_Null (Discrim, Positional_Expr);
4752 end if;
4753
4754 Next (Positional_Expr);
4755 end if;
4756
4757 if Present (Get_Value (Discrim, Component_Associations (N))) then
4758 Error_Msg_NE
4759 ("more than one value supplied for discriminant&",
4760 N, Discrim);
4761 end if;
4762
4763 Next_Discriminant (Discrim);
4764 end loop;
4765
4766 -- Find remaining discriminant values if any among named components
4767
4768 while Present (Discrim) loop
4769 Expr := Get_Value (Discrim, Component_Associations (N), True);
4770
4771 if not Discriminant_Present (Discrim) then
4772 if Present (Expr) then
4773 Error_Msg_NE
4774 ("more than one value supplied for discriminant &",
4775 N, Discrim);
4776 end if;
4777
4778 elsif No (Expr) then
4779 Error_Msg_NE
4780 ("no value supplied for discriminant &", N, Discrim);
4781 Missing_Discriminants := True;
4782
4783 else
4784 Resolve_Aggr_Expr (Expr, Discrim);
4785 end if;
4786
4787 Next_Discriminant (Discrim);
4788 end loop;
4789
4790 if Missing_Discriminants then
4791 return;
4792 end if;
4793
4794 -- At this point and until the beginning of STEP 6, New_Assoc_List
4795 -- contains only the discriminants and their values.
4796
4797 end Step_3;
4798
4799 -- STEP 4: Set the Etype of the record aggregate
4800
4801 if Has_Discriminants (Typ)
4802 or else (Has_Unknown_Discriminants (Typ)
4803 and then Present (Underlying_Record_View (Typ)))
4804 then
4805 Build_Constrained_Itype (N, Typ, New_Assoc_List);
4806 else
4807 Set_Etype (N, Typ);
4808 end if;
4809
4810 -- STEP 5: Get remaining components according to discriminant values
4811
4812 Step_5 : declare
4813 Dnode : Node_Id;
4814 Errors_Found : Boolean := False;
4815 Record_Def : Node_Id;
4816 Parent_Typ : Entity_Id;
4817 Parent_Typ_List : Elist_Id;
4818 Parent_Elmt : Elmt_Id;
4819 Root_Typ : Entity_Id;
4820
4821 begin
4822 if Is_Derived_Type (Typ) and then Is_Tagged_Type (Typ) then
4823 Parent_Typ_List := New_Elmt_List;
4824
4825 -- If this is an extension aggregate, the component list must
4826 -- include all components that are not in the given ancestor type.
4827 -- Otherwise, the component list must include components of all
4828 -- ancestors, starting with the root.
4829
4830 if Nkind (N) = N_Extension_Aggregate then
4831 Root_Typ := Base_Type (Etype (Ancestor_Part (N)));
4832
4833 else
4834 -- AI05-0115: check legality of aggregate for type with a
4835 -- private ancestor.
4836
4837 Root_Typ := Root_Type (Typ);
4838 if Has_Private_Ancestor (Typ) then
4839 declare
4840 Ancestor : constant Entity_Id :=
4841 Find_Private_Ancestor (Typ);
4842 Ancestor_Unit : constant Entity_Id :=
4843 Cunit_Entity
4844 (Get_Source_Unit (Ancestor));
4845 Parent_Unit : constant Entity_Id :=
4846 Cunit_Entity (Get_Source_Unit
4847 (Base_Type (Etype (Ancestor))));
4848 begin
4849 -- Check whether we are in a scope that has full view
4850 -- over the private ancestor and its parent. This can
4851 -- only happen if the derivation takes place in a child
4852 -- unit of the unit that declares the parent, and we are
4853 -- in the private part or body of that child unit, else
4854 -- the aggregate is illegal.
4855
4856 if Is_Child_Unit (Ancestor_Unit)
4857 and then Scope (Ancestor_Unit) = Parent_Unit
4858 and then In_Open_Scopes (Scope (Ancestor))
4859 and then
4860 (In_Private_Part (Scope (Ancestor))
4861 or else In_Package_Body (Scope (Ancestor)))
4862 then
4863 null;
4864
4865 else
4866 Error_Msg_NE
4867 ("type of aggregate has private ancestor&!",
4868 N, Root_Typ);
4869 Error_Msg_N ("must use extension aggregate!", N);
4870 return;
4871 end if;
4872 end;
4873 end if;
4874
4875 Dnode := Declaration_Node (Base_Type (Root_Typ));
4876
4877 -- If we don't get a full declaration, then we have some error
4878 -- which will get signalled later so skip this part. Otherwise
4879 -- gather components of root that apply to the aggregate type.
4880 -- We use the base type in case there is an applicable stored
4881 -- constraint that renames the discriminants of the root.
4882
4883 if Nkind (Dnode) = N_Full_Type_Declaration then
4884 Record_Def := Type_Definition (Dnode);
4885 Gather_Components
4886 (Base_Type (Typ),
4887 Component_List (Record_Def),
4888 Governed_By => New_Assoc_List,
4889 Into => Components,
4890 Report_Errors => Errors_Found);
4891
4892 if Errors_Found then
4893 Error_Msg_N
4894 ("discriminant controlling variant part is not static",
4895 N);
4896 return;
4897 end if;
4898 end if;
4899 end if;
4900
4901 Parent_Typ := Base_Type (Typ);
4902 while Parent_Typ /= Root_Typ loop
4903 Prepend_Elmt (Parent_Typ, To => Parent_Typ_List);
4904 Parent_Typ := Etype (Parent_Typ);
4905
4906 if Nkind (Parent (Base_Type (Parent_Typ))) =
4907 N_Private_Type_Declaration
4908 or else Nkind (Parent (Base_Type (Parent_Typ))) =
4909 N_Private_Extension_Declaration
4910 then
4911 if Nkind (N) /= N_Extension_Aggregate then
4912 Error_Msg_NE
4913 ("type of aggregate has private ancestor&!",
4914 N, Parent_Typ);
4915 Error_Msg_N ("must use extension aggregate!", N);
4916 return;
4917
4918 elsif Parent_Typ /= Root_Typ then
4919 Error_Msg_NE
4920 ("ancestor part of aggregate must be private type&",
4921 Ancestor_Part (N), Parent_Typ);
4922 return;
4923 end if;
4924
4925 -- The current view of ancestor part may be a private type,
4926 -- while the context type is always non-private.
4927
4928 elsif Is_Private_Type (Root_Typ)
4929 and then Present (Full_View (Root_Typ))
4930 and then Nkind (N) = N_Extension_Aggregate
4931 then
4932 exit when Base_Type (Full_View (Root_Typ)) = Parent_Typ;
4933 end if;
4934 end loop;
4935
4936 -- Now collect components from all other ancestors, beginning
4937 -- with the current type. If the type has unknown discriminants
4938 -- use the component list of the Underlying_Record_View, which
4939 -- needs to be used for the subsequent expansion of the aggregate
4940 -- into assignments.
4941
4942 Parent_Elmt := First_Elmt (Parent_Typ_List);
4943 while Present (Parent_Elmt) loop
4944 Parent_Typ := Node (Parent_Elmt);
4945
4946 if Has_Unknown_Discriminants (Parent_Typ)
4947 and then Present (Underlying_Record_View (Typ))
4948 then
4949 Parent_Typ := Underlying_Record_View (Parent_Typ);
4950 end if;
4951
4952 Record_Def := Type_Definition (Parent (Base_Type (Parent_Typ)));
4953 Gather_Components (Empty,
4954 Component_List (Record_Extension_Part (Record_Def)),
4955 Governed_By => New_Assoc_List,
4956 Into => Components,
4957 Report_Errors => Errors_Found);
4958
4959 Next_Elmt (Parent_Elmt);
4960 end loop;
4961
4962 -- Typ is not a derived tagged type
4963
4964 else
4965 Record_Def := Type_Definition (Parent (Base_Type (Typ)));
4966
4967 if Null_Present (Record_Def) then
4968 null;
4969
4970 elsif not Has_Unknown_Discriminants (Typ) then
4971 Gather_Components
4972 (Base_Type (Typ),
4973 Component_List (Record_Def),
4974 Governed_By => New_Assoc_List,
4975 Into => Components,
4976 Report_Errors => Errors_Found);
4977
4978 else
4979 Gather_Components
4980 (Base_Type (Underlying_Record_View (Typ)),
4981 Component_List (Record_Def),
4982 Governed_By => New_Assoc_List,
4983 Into => Components,
4984 Report_Errors => Errors_Found);
4985 end if;
4986 end if;
4987
4988 if Errors_Found then
4989 return;
4990 end if;
4991 end Step_5;
4992
4993 -- STEP 6: Find component Values
4994
4995 Component := Empty;
4996 Component_Elmt := First_Elmt (Components);
4997
4998 -- First scan the remaining positional associations in the aggregate.
4999 -- Remember that at this point Positional_Expr contains the current
5000 -- positional association if any is left after looking for discriminant
5001 -- values in step 3.
5002
5003 while Present (Positional_Expr) and then Present (Component_Elmt) loop
5004 Component := Node (Component_Elmt);
5005 Resolve_Aggr_Expr (Positional_Expr, Component);
5006
5007 -- Ada 2005 (AI-231)
5008
5009 if Ada_Version >= Ada_2005 and then Known_Null (Positional_Expr) then
5010 Check_Can_Never_Be_Null (Component, Positional_Expr);
5011 end if;
5012
5013 if Present (Get_Value (Component, Component_Associations (N))) then
5014 Error_Msg_NE
5015 ("more than one value supplied for Component &", N, Component);
5016 end if;
5017
5018 Next (Positional_Expr);
5019 Next_Elmt (Component_Elmt);
5020 end loop;
5021
5022 if Present (Positional_Expr) then
5023 Error_Msg_N
5024 ("too many components for record aggregate", Positional_Expr);
5025 end if;
5026
5027 -- Now scan for the named arguments of the aggregate
5028
5029 while Present (Component_Elmt) loop
5030 Component := Node (Component_Elmt);
5031 Expr := Get_Value (Component, Component_Associations (N), True);
5032
5033 -- Note: The previous call to Get_Value sets the value of the
5034 -- variable Is_Box_Present.
5035
5036 -- Ada 2005 (AI-287): Handle components with default initialization.
5037 -- Note: This feature was originally added to Ada 2005 for limited
5038 -- but it was finally allowed with any type.
5039
5040 if Is_Box_Present then
5041 Check_Box_Component : declare
5042 Ctyp : constant Entity_Id := Etype (Component);
5043
5044 begin
5045 -- If there is a default expression for the aggregate, copy
5046 -- it into a new association. This copy must modify the scopes
5047 -- of internal types that may be attached to the expression
5048 -- (e.g. index subtypes of arrays) because in general the type
5049 -- declaration and the aggregate appear in different scopes,
5050 -- and the backend requires the scope of the type to match the
5051 -- point at which it is elaborated.
5052
5053 -- If the component has an initialization procedure (IP) we
5054 -- pass the component to the expander, which will generate
5055 -- the call to such IP.
5056
5057 -- If the component has discriminants, their values must
5058 -- be taken from their subtype. This is indispensable for
5059 -- constraints that are given by the current instance of an
5060 -- enclosing type, to allow the expansion of the aggregate to
5061 -- replace the reference to the current instance by the target
5062 -- object of the aggregate.
5063
5064 if Present (Parent (Component))
5065 and then Nkind (Parent (Component)) = N_Component_Declaration
5066 and then Present (Expression (Parent (Component)))
5067 then
5068 Expr :=
5069 New_Copy_Tree_And_Copy_Dimensions
5070 (Expression (Parent (Component)),
5071 New_Scope => Current_Scope,
5072 New_Sloc => Sloc (N));
5073
5074 -- As the type of the copied default expression may refer
5075 -- to discriminants of the record type declaration, these
5076 -- non-stored discriminants need to be rewritten into stored
5077 -- discriminant values for the aggregate. This is required
5078 -- in GNATprove mode, and is adopted in all modes to avoid
5079 -- special-casing GNATprove mode.
5080
5081 if Is_Array_Type (Etype (Expr)) then
5082 declare
5083 Rec_Typ : constant Entity_Id := Scope (Component);
5084 -- Root record type whose discriminants may be used as
5085 -- bounds in range nodes.
5086
5087 Assoc : Node_Id;
5088 Choice : Node_Id;
5089 Index : Node_Id;
5090
5091 begin
5092 -- Rewrite the range nodes occurring in the indexes
5093 -- and their types.
5094
5095 Index := First_Index (Etype (Expr));
5096 while Present (Index) loop
5097 Rewrite_Range (Rec_Typ, Index);
5098 Rewrite_Range
5099 (Rec_Typ, Scalar_Range (Etype (Index)));
5100
5101 Next_Index (Index);
5102 end loop;
5103
5104 -- Rewrite the range nodes occurring as aggregate
5105 -- bounds and component associations.
5106
5107 if Nkind (Expr) = N_Aggregate then
5108 if Present (Aggregate_Bounds (Expr)) then
5109 Rewrite_Range (Rec_Typ, Aggregate_Bounds (Expr));
5110 end if;
5111
5112 if Present (Component_Associations (Expr)) then
5113 Assoc := First (Component_Associations (Expr));
5114 while Present (Assoc) loop
5115 Choice := First (Choices (Assoc));
5116 while Present (Choice) loop
5117 Rewrite_Range (Rec_Typ, Choice);
5118
5119 Next (Choice);
5120 end loop;
5121
5122 Next (Assoc);
5123 end loop;
5124 end if;
5125 end if;
5126 end;
5127 end if;
5128
5129 Add_Association
5130 (Component => Component,
5131 Expr => Expr,
5132 Assoc_List => New_Assoc_List);
5133 Set_Has_Self_Reference (N);
5134
5135 -- A box-defaulted access component gets the value null. Also
5136 -- included are components of private types whose underlying
5137 -- type is an access type. In either case set the type of the
5138 -- literal, for subsequent use in semantic checks.
5139
5140 elsif Present (Underlying_Type (Ctyp))
5141 and then Is_Access_Type (Underlying_Type (Ctyp))
5142 then
5143 -- If the component's type is private with an access type as
5144 -- its underlying type then we have to create an unchecked
5145 -- conversion to satisfy type checking.
5146
5147 if Is_Private_Type (Ctyp) then
5148 declare
5149 Qual_Null : constant Node_Id :=
5150 Make_Qualified_Expression (Sloc (N),
5151 Subtype_Mark =>
5152 New_Occurrence_Of
5153 (Underlying_Type (Ctyp), Sloc (N)),
5154 Expression => Make_Null (Sloc (N)));
5155
5156 Convert_Null : constant Node_Id :=
5157 Unchecked_Convert_To
5158 (Ctyp, Qual_Null);
5159
5160 begin
5161 Analyze_And_Resolve (Convert_Null, Ctyp);
5162 Add_Association
5163 (Component => Component,
5164 Expr => Convert_Null,
5165 Assoc_List => New_Assoc_List);
5166 end;
5167
5168 -- Otherwise the component type is non-private
5169
5170 else
5171 Expr := Make_Null (Sloc (N));
5172 Set_Etype (Expr, Ctyp);
5173
5174 Add_Association
5175 (Component => Component,
5176 Expr => Expr,
5177 Assoc_List => New_Assoc_List);
5178 end if;
5179
5180 -- Ada 2012: If component is scalar with default value, use it
5181 -- by converting it to Ctyp, so that subtype constraints are
5182 -- checked.
5183
5184 elsif Is_Scalar_Type (Ctyp)
5185 and then Has_Default_Aspect (Ctyp)
5186 then
5187 declare
5188 Conv : constant Node_Id :=
5189 Convert_To
5190 (Typ => Ctyp,
5191 Expr =>
5192 New_Copy_Tree
5193 (Default_Aspect_Value
5194 (First_Subtype (Underlying_Type (Ctyp)))));
5195
5196 begin
5197 Analyze_And_Resolve (Conv, Ctyp);
5198 Add_Association
5199 (Component => Component,
5200 Expr => Conv,
5201 Assoc_List => New_Assoc_List);
5202 end;
5203
5204 elsif Has_Non_Null_Base_Init_Proc (Ctyp)
5205 or else not Expander_Active
5206 then
5207 if Is_Record_Type (Ctyp)
5208 and then Has_Discriminants (Ctyp)
5209 and then not Is_Private_Type (Ctyp)
5210 then
5211 -- We build a partially initialized aggregate with the
5212 -- values of the discriminants and box initialization
5213 -- for the rest, if other components are present.
5214
5215 -- The type of the aggregate is the known subtype of
5216 -- the component. The capture of discriminants must be
5217 -- recursive because subcomponents may be constrained
5218 -- (transitively) by discriminants of enclosing types.
5219 -- For a private type with discriminants, a call to the
5220 -- initialization procedure will be generated, and no
5221 -- subaggregate is needed.
5222
5223 Capture_Discriminants : declare
5224 Loc : constant Source_Ptr := Sloc (N);
5225 Expr : Node_Id;
5226
5227 begin
5228 Expr := Make_Aggregate (Loc, No_List, New_List);
5229 Set_Etype (Expr, Ctyp);
5230
5231 -- If the enclosing type has discriminants, they have
5232 -- been collected in the aggregate earlier, and they
5233 -- may appear as constraints of subcomponents.
5234
5235 -- Similarly if this component has discriminants, they
5236 -- might in turn be propagated to their components.
5237
5238 if Has_Discriminants (Typ) then
5239 Add_Discriminant_Values (Expr, New_Assoc_List);
5240 Propagate_Discriminants (Expr, New_Assoc_List);
5241
5242 elsif Has_Discriminants (Ctyp) then
5243 Add_Discriminant_Values
5244 (Expr, Component_Associations (Expr));
5245 Propagate_Discriminants
5246 (Expr, Component_Associations (Expr));
5247
5248 Build_Constrained_Itype
5249 (Expr, Ctyp, Component_Associations (Expr));
5250
5251 else
5252 declare
5253 Comp : Entity_Id;
5254
5255 begin
5256 -- If the type has additional components, create
5257 -- an OTHERS box association for them.
5258
5259 Comp := First_Component (Ctyp);
5260 while Present (Comp) loop
5261 if Ekind (Comp) = E_Component then
5262 if not Is_Record_Type (Etype (Comp)) then
5263 Append_To
5264 (Component_Associations (Expr),
5265 Make_Component_Association (Loc,
5266 Choices =>
5267 New_List (
5268 Make_Others_Choice (Loc)),
5269 Expression => Empty,
5270 Box_Present => True));
5271 end if;
5272
5273 exit;
5274 end if;
5275
5276 Next_Component (Comp);
5277 end loop;
5278 end;
5279 end if;
5280
5281 Add_Association
5282 (Component => Component,
5283 Expr => Expr,
5284 Assoc_List => New_Assoc_List);
5285 end Capture_Discriminants;
5286
5287 -- Otherwise the component type is not a record, or it has
5288 -- not discriminants, or it is private.
5289
5290 else
5291 Add_Association
5292 (Component => Component,
5293 Expr => Empty,
5294 Assoc_List => New_Assoc_List,
5295 Is_Box_Present => True);
5296 end if;
5297
5298 -- Otherwise we only need to resolve the expression if the
5299 -- component has partially initialized values (required to
5300 -- expand the corresponding assignments and run-time checks).
5301
5302 elsif Present (Expr)
5303 and then Is_Partially_Initialized_Type (Ctyp)
5304 then
5305 Resolve_Aggr_Expr (Expr, Component);
5306 end if;
5307 end Check_Box_Component;
5308
5309 elsif No (Expr) then
5310
5311 -- Ignore hidden components associated with the position of the
5312 -- interface tags: these are initialized dynamically.
5313
5314 if not Present (Related_Type (Component)) then
5315 Error_Msg_NE
5316 ("no value supplied for component &!", N, Component);
5317 end if;
5318
5319 else
5320 Resolve_Aggr_Expr (Expr, Component);
5321 end if;
5322
5323 Next_Elmt (Component_Elmt);
5324 end loop;
5325
5326 -- STEP 7: check for invalid components + check type in choice list
5327
5328 Step_7 : declare
5329 Assoc : Node_Id;
5330 New_Assoc : Node_Id;
5331
5332 Selectr : Node_Id;
5333 -- Selector name
5334
5335 Typech : Entity_Id;
5336 -- Type of first component in choice list
5337
5338 begin
5339 if Present (Component_Associations (N)) then
5340 Assoc := First (Component_Associations (N));
5341 else
5342 Assoc := Empty;
5343 end if;
5344
5345 Verification : while Present (Assoc) loop
5346 Selectr := First (Choices (Assoc));
5347 Typech := Empty;
5348
5349 if Nkind (Selectr) = N_Others_Choice then
5350
5351 -- Ada 2005 (AI-287): others choice may have expression or box
5352
5353 if No (Others_Etype) and then Others_Box = 0 then
5354 Error_Msg_N
5355 ("OTHERS must represent at least one component", Selectr);
5356
5357 elsif Others_Box = 1 and then Warn_On_Redundant_Constructs then
5358 Error_Msg_N ("others choice is redundant?", Box_Node);
5359 Error_Msg_N
5360 ("\previous choices cover all components?", Box_Node);
5361 end if;
5362
5363 exit Verification;
5364 end if;
5365
5366 while Present (Selectr) loop
5367 New_Assoc := First (New_Assoc_List);
5368 while Present (New_Assoc) loop
5369 Component := First (Choices (New_Assoc));
5370
5371 if Chars (Selectr) = Chars (Component) then
5372 if Style_Check then
5373 Check_Identifier (Selectr, Entity (Component));
5374 end if;
5375
5376 exit;
5377 end if;
5378
5379 Next (New_Assoc);
5380 end loop;
5381
5382 -- If no association, this is not a legal component of the type
5383 -- in question, unless its association is provided with a box.
5384
5385 if No (New_Assoc) then
5386 if Box_Present (Parent (Selectr)) then
5387
5388 -- This may still be a bogus component with a box. Scan
5389 -- list of components to verify that a component with
5390 -- that name exists.
5391
5392 declare
5393 C : Entity_Id;
5394
5395 begin
5396 C := First_Component (Typ);
5397 while Present (C) loop
5398 if Chars (C) = Chars (Selectr) then
5399
5400 -- If the context is an extension aggregate,
5401 -- the component must not be inherited from
5402 -- the ancestor part of the aggregate.
5403
5404 if Nkind (N) /= N_Extension_Aggregate
5405 or else
5406 Scope (Original_Record_Component (C)) /=
5407 Etype (Ancestor_Part (N))
5408 then
5409 exit;
5410 end if;
5411 end if;
5412
5413 Next_Component (C);
5414 end loop;
5415
5416 if No (C) then
5417 Error_Msg_Node_2 := Typ;
5418 Error_Msg_N ("& is not a component of}", Selectr);
5419 end if;
5420 end;
5421
5422 elsif Chars (Selectr) /= Name_uTag
5423 and then Chars (Selectr) /= Name_uParent
5424 then
5425 if not Has_Discriminants (Typ) then
5426 Error_Msg_Node_2 := Typ;
5427 Error_Msg_N ("& is not a component of}", Selectr);
5428 else
5429 Error_Msg_N
5430 ("& is not a component of the aggregate subtype",
5431 Selectr);
5432 end if;
5433
5434 Check_Misspelled_Component (Components, Selectr);
5435 end if;
5436
5437 elsif No (Typech) then
5438 Typech := Base_Type (Etype (Component));
5439
5440 -- AI05-0199: In Ada 2012, several components of anonymous
5441 -- access types can appear in a choice list, as long as the
5442 -- designated types match.
5443
5444 elsif Typech /= Base_Type (Etype (Component)) then
5445 if Ada_Version >= Ada_2012
5446 and then Ekind (Typech) = E_Anonymous_Access_Type
5447 and then
5448 Ekind (Etype (Component)) = E_Anonymous_Access_Type
5449 and then Base_Type (Designated_Type (Typech)) =
5450 Base_Type (Designated_Type (Etype (Component)))
5451 and then
5452 Subtypes_Statically_Match (Typech, (Etype (Component)))
5453 then
5454 null;
5455
5456 elsif not Box_Present (Parent (Selectr)) then
5457 Error_Msg_N
5458 ("components in choice list must have same type",
5459 Selectr);
5460 end if;
5461 end if;
5462
5463 Next (Selectr);
5464 end loop;
5465
5466 Next (Assoc);
5467 end loop Verification;
5468 end Step_7;
5469
5470 -- STEP 8: replace the original aggregate
5471
5472 Step_8 : declare
5473 New_Aggregate : constant Node_Id := New_Copy (N);
5474
5475 begin
5476 Set_Expressions (New_Aggregate, No_List);
5477 Set_Etype (New_Aggregate, Etype (N));
5478 Set_Component_Associations (New_Aggregate, New_Assoc_List);
5479 Set_Check_Actuals (New_Aggregate, Check_Actuals (N));
5480
5481 Rewrite (N, New_Aggregate);
5482 end Step_8;
5483
5484 -- Check the dimensions of the components in the record aggregate
5485
5486 Analyze_Dimension_Extension_Or_Record_Aggregate (N);
5487 end Resolve_Record_Aggregate;
5488
5489 -----------------------------
5490 -- Check_Can_Never_Be_Null --
5491 -----------------------------
5492
5493 procedure Check_Can_Never_Be_Null (Typ : Entity_Id; Expr : Node_Id) is
5494 Comp_Typ : Entity_Id;
5495
5496 begin
5497 pragma Assert
5498 (Ada_Version >= Ada_2005
5499 and then Present (Expr)
5500 and then Known_Null (Expr));
5501
5502 case Ekind (Typ) is
5503 when E_Array_Type =>
5504 Comp_Typ := Component_Type (Typ);
5505
5506 when E_Component
5507 | E_Discriminant
5508 =>
5509 Comp_Typ := Etype (Typ);
5510
5511 when others =>
5512 return;
5513 end case;
5514
5515 if Can_Never_Be_Null (Comp_Typ) then
5516
5517 -- Here we know we have a constraint error. Note that we do not use
5518 -- Apply_Compile_Time_Constraint_Error here to the Expr, which might
5519 -- seem the more natural approach. That's because in some cases the
5520 -- components are rewritten, and the replacement would be missed.
5521 -- We do not mark the whole aggregate as raising a constraint error,
5522 -- because the association may be a null array range.
5523
5524 Error_Msg_N
5525 ("(Ada 2005) null not allowed in null-excluding component??", Expr);
5526 Error_Msg_N
5527 ("\Constraint_Error will be raised at run time??", Expr);
5528
5529 Rewrite (Expr,
5530 Make_Raise_Constraint_Error
5531 (Sloc (Expr), Reason => CE_Access_Check_Failed));
5532 Set_Etype (Expr, Comp_Typ);
5533 Set_Analyzed (Expr);
5534 end if;
5535 end Check_Can_Never_Be_Null;
5536
5537 ---------------------
5538 -- Sort_Case_Table --
5539 ---------------------
5540
5541 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is
5542 U : constant Int := Case_Table'Last;
5543 K : Int;
5544 J : Int;
5545 T : Case_Bounds;
5546
5547 begin
5548 K := 1;
5549 while K < U loop
5550 T := Case_Table (K + 1);
5551
5552 J := K + 1;
5553 while J > 1
5554 and then Expr_Value (Case_Table (J - 1).Lo) > Expr_Value (T.Lo)
5555 loop
5556 Case_Table (J) := Case_Table (J - 1);
5557 J := J - 1;
5558 end loop;
5559
5560 Case_Table (J) := T;
5561 K := K + 1;
5562 end loop;
5563 end Sort_Case_Table;
5564
5565 end Sem_Aggr;