[Ada] Multidimensional arrays with Iterated_Component_Associations
[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 the expression without expansion, to verify legality.
1684 -- After analysis we remove references to the index variable because
1685 -- the expression will be analyzed anew when the enclosing aggregate
1686 -- is expanded, and the construct is rewritten as a loop with a new
1687 -- index variable.
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 Remove_References (Expr);
1695
1696 -- An iterated_component_association may appear in a nested
1697 -- aggregate for a multidimensional structure: preserve the bounds
1698 -- computed for the expression, as well as the anonymous array
1699 -- type generated for it; both are needed during array expansion.
1700
1701 if Nkind (Expr) = N_Aggregate then
1702 Set_Aggregate_Bounds (Expression (N), Aggregate_Bounds (Expr));
1703 Set_Etype (Expression (N), Etype (Expr));
1704 end if;
1705
1706 End_Scope;
1707 end Resolve_Iterated_Component_Association;
1708
1709 -- Local variables
1710
1711 Assoc : Node_Id;
1712 Choice : Node_Id;
1713 Expr : Node_Id;
1714 Discard : Node_Id;
1715
1716 Aggr_Low : Node_Id := Empty;
1717 Aggr_High : Node_Id := Empty;
1718 -- The actual low and high bounds of this sub-aggregate
1719
1720 Case_Table_Size : Nat;
1721 -- Contains the size of the case table needed to sort aggregate choices
1722
1723 Choices_Low : Node_Id := Empty;
1724 Choices_High : Node_Id := Empty;
1725 -- The lowest and highest discrete choices values for a named aggregate
1726
1727 Delete_Choice : Boolean;
1728 -- Used when replacing a subtype choice with predicate by a list
1729
1730 Nb_Elements : Uint := Uint_0;
1731 -- The number of elements in a positional aggregate
1732
1733 Nb_Discrete_Choices : Nat := 0;
1734 -- The overall number of discrete choices (not counting others choice)
1735
1736 -- Start of processing for Resolve_Array_Aggregate
1737
1738 begin
1739 -- Ignore junk empty aggregate resulting from parser error
1740
1741 if No (Expressions (N))
1742 and then No (Component_Associations (N))
1743 and then not Null_Record_Present (N)
1744 then
1745 return False;
1746 end if;
1747
1748 -- STEP 1: make sure the aggregate is correctly formatted
1749
1750 if Present (Component_Associations (N)) then
1751 Assoc := First (Component_Associations (N));
1752 while Present (Assoc) loop
1753 if Nkind (Assoc) = N_Iterated_Component_Association then
1754 Resolve_Iterated_Component_Association (Assoc, Index_Typ);
1755 end if;
1756
1757 Choice := First (Choice_List (Assoc));
1758 Delete_Choice := False;
1759 while Present (Choice) loop
1760 if Nkind (Choice) = N_Others_Choice then
1761 Others_Present := True;
1762
1763 if Choice /= First (Choice_List (Assoc))
1764 or else Present (Next (Choice))
1765 then
1766 Error_Msg_N
1767 ("OTHERS must appear alone in a choice list", Choice);
1768 return Failure;
1769 end if;
1770
1771 if Present (Next (Assoc)) then
1772 Error_Msg_N
1773 ("OTHERS must appear last in an aggregate", Choice);
1774 return Failure;
1775 end if;
1776
1777 if Ada_Version = Ada_83
1778 and then Assoc /= First (Component_Associations (N))
1779 and then Nkind (Parent (N)) in
1780 N_Assignment_Statement | N_Object_Declaration
1781 then
1782 Error_Msg_N
1783 ("(Ada 83) illegal context for OTHERS choice", N);
1784 end if;
1785
1786 elsif Is_Entity_Name (Choice) then
1787 Analyze (Choice);
1788
1789 declare
1790 E : constant Entity_Id := Entity (Choice);
1791 New_Cs : List_Id;
1792 P : Node_Id;
1793 C : Node_Id;
1794
1795 begin
1796 if Is_Type (E) and then Has_Predicates (E) then
1797 Freeze_Before (N, E);
1798
1799 if Has_Dynamic_Predicate_Aspect (E) then
1800 Error_Msg_NE
1801 ("subtype& has dynamic predicate, not allowed "
1802 & "in aggregate choice", Choice, E);
1803
1804 elsif not Is_OK_Static_Subtype (E) then
1805 Error_Msg_NE
1806 ("non-static subtype& has predicate, not allowed "
1807 & "in aggregate choice", Choice, E);
1808 end if;
1809
1810 -- If the subtype has a static predicate, replace the
1811 -- original choice with the list of individual values
1812 -- covered by the predicate.
1813 -- This should be deferred to expansion time ???
1814
1815 if Present (Static_Discrete_Predicate (E)) then
1816 Delete_Choice := True;
1817
1818 New_Cs := New_List;
1819 P := First (Static_Discrete_Predicate (E));
1820 while Present (P) loop
1821 C := New_Copy (P);
1822 Set_Sloc (C, Sloc (Choice));
1823 Append_To (New_Cs, C);
1824 Next (P);
1825 end loop;
1826
1827 Insert_List_After (Choice, New_Cs);
1828 end if;
1829 end if;
1830 end;
1831 end if;
1832
1833 Nb_Choices := Nb_Choices + 1;
1834
1835 declare
1836 C : constant Node_Id := Choice;
1837
1838 begin
1839 Next (Choice);
1840
1841 if Delete_Choice then
1842 Remove (C);
1843 Nb_Choices := Nb_Choices - 1;
1844 Delete_Choice := False;
1845 end if;
1846 end;
1847 end loop;
1848
1849 Next (Assoc);
1850 end loop;
1851 end if;
1852
1853 -- At this point we know that the others choice, if present, is by
1854 -- itself and appears last in the aggregate. Check if we have mixed
1855 -- positional and discrete associations (other than the others choice).
1856
1857 if Present (Expressions (N))
1858 and then (Nb_Choices > 1
1859 or else (Nb_Choices = 1 and then not Others_Present))
1860 then
1861 Error_Msg_N
1862 ("named association cannot follow positional association",
1863 First (Choice_List (First (Component_Associations (N)))));
1864 return Failure;
1865 end if;
1866
1867 -- Test for the validity of an others choice if present
1868
1869 if Others_Present and then not Others_Allowed then
1870 Error_Msg_N
1871 ("OTHERS choice not allowed here",
1872 First (Choice_List (First (Component_Associations (N)))));
1873 return Failure;
1874 end if;
1875
1876 -- Protect against cascaded errors
1877
1878 if Etype (Index_Typ) = Any_Type then
1879 return Failure;
1880 end if;
1881
1882 -- STEP 2: Process named components
1883
1884 if No (Expressions (N)) then
1885 if Others_Present then
1886 Case_Table_Size := Nb_Choices - 1;
1887 else
1888 Case_Table_Size := Nb_Choices;
1889 end if;
1890
1891 Step_2 : declare
1892 function Empty_Range (A : Node_Id) return Boolean;
1893 -- If an association covers an empty range, some warnings on the
1894 -- expression of the association can be disabled.
1895
1896 -----------------
1897 -- Empty_Range --
1898 -----------------
1899
1900 function Empty_Range (A : Node_Id) return Boolean is
1901 R : constant Node_Id := First (Choices (A));
1902 begin
1903 return No (Next (R))
1904 and then Nkind (R) = N_Range
1905 and then Compile_Time_Compare
1906 (Low_Bound (R), High_Bound (R), False) = GT;
1907 end Empty_Range;
1908
1909 -- Local variables
1910
1911 Low : Node_Id;
1912 High : Node_Id;
1913 -- Denote the lowest and highest values in an aggregate choice
1914
1915 S_Low : Node_Id := Empty;
1916 S_High : Node_Id := Empty;
1917 -- if a choice in an aggregate is a subtype indication these
1918 -- denote the lowest and highest values of the subtype
1919
1920 Table : Case_Table_Type (1 .. Case_Table_Size);
1921 -- Used to sort all the different choice values
1922
1923 Single_Choice : Boolean;
1924 -- Set to true every time there is a single discrete choice in a
1925 -- discrete association
1926
1927 Prev_Nb_Discrete_Choices : Nat;
1928 -- Used to keep track of the number of discrete choices in the
1929 -- current association.
1930
1931 Errors_Posted_On_Choices : Boolean := False;
1932 -- Keeps track of whether any choices have semantic errors
1933
1934 -- Start of processing for Step_2
1935
1936 begin
1937 -- STEP 2 (A): Check discrete choices validity
1938
1939 Assoc := First (Component_Associations (N));
1940 while Present (Assoc) loop
1941 Prev_Nb_Discrete_Choices := Nb_Discrete_Choices;
1942 Choice := First (Choice_List (Assoc));
1943
1944 loop
1945 Analyze (Choice);
1946
1947 if Nkind (Choice) = N_Others_Choice then
1948 Single_Choice := False;
1949 exit;
1950
1951 -- Test for subtype mark without constraint
1952
1953 elsif Is_Entity_Name (Choice) and then
1954 Is_Type (Entity (Choice))
1955 then
1956 if Base_Type (Entity (Choice)) /= Index_Base then
1957 Error_Msg_N
1958 ("invalid subtype mark in aggregate choice",
1959 Choice);
1960 return Failure;
1961 end if;
1962
1963 -- Case of subtype indication
1964
1965 elsif Nkind (Choice) = N_Subtype_Indication then
1966 Resolve_Discrete_Subtype_Indication (Choice, Index_Base);
1967
1968 if Has_Dynamic_Predicate_Aspect
1969 (Entity (Subtype_Mark (Choice)))
1970 then
1971 Error_Msg_NE
1972 ("subtype& has dynamic predicate, "
1973 & "not allowed in aggregate choice",
1974 Choice, Entity (Subtype_Mark (Choice)));
1975 end if;
1976
1977 -- Does the subtype indication evaluation raise CE?
1978
1979 Get_Index_Bounds (Subtype_Mark (Choice), S_Low, S_High);
1980 Get_Index_Bounds (Choice, Low, High);
1981 Check_Bounds (S_Low, S_High, Low, High);
1982
1983 -- Case of range or expression
1984
1985 else
1986 Resolve (Choice, Index_Base);
1987 Check_Unset_Reference (Choice);
1988 Check_Non_Static_Context (Choice);
1989
1990 -- If semantic errors were posted on the choice, then
1991 -- record that for possible early return from later
1992 -- processing (see handling of enumeration choices).
1993
1994 if Error_Posted (Choice) then
1995 Errors_Posted_On_Choices := True;
1996 end if;
1997
1998 -- Do not range check a choice. This check is redundant
1999 -- since this test is already done when we check that the
2000 -- bounds of the array aggregate are within range.
2001
2002 Set_Do_Range_Check (Choice, False);
2003 end if;
2004
2005 -- If we could not resolve the discrete choice stop here
2006
2007 if Etype (Choice) = Any_Type then
2008 return Failure;
2009
2010 -- If the discrete choice raises CE get its original bounds
2011
2012 elsif Nkind (Choice) = N_Raise_Constraint_Error then
2013 Set_Raises_Constraint_Error (N);
2014 Get_Index_Bounds (Original_Node (Choice), Low, High);
2015
2016 -- Otherwise get its bounds as usual
2017
2018 else
2019 Get_Index_Bounds (Choice, Low, High);
2020 end if;
2021
2022 if (Dynamic_Or_Null_Range (Low, High)
2023 or else (Nkind (Choice) = N_Subtype_Indication
2024 and then
2025 Dynamic_Or_Null_Range (S_Low, S_High)))
2026 and then Nb_Choices /= 1
2027 then
2028 Error_Msg_N
2029 ("dynamic or empty choice in aggregate "
2030 & "must be the only choice", Choice);
2031 return Failure;
2032 end if;
2033
2034 if not (All_Composite_Constraints_Static (Low)
2035 and then All_Composite_Constraints_Static (High)
2036 and then All_Composite_Constraints_Static (S_Low)
2037 and then All_Composite_Constraints_Static (S_High))
2038 then
2039 Check_Restriction (No_Dynamic_Sized_Objects, Choice);
2040 end if;
2041
2042 Nb_Discrete_Choices := Nb_Discrete_Choices + 1;
2043 Table (Nb_Discrete_Choices).Lo := Low;
2044 Table (Nb_Discrete_Choices).Hi := High;
2045 Table (Nb_Discrete_Choices).Choice := Choice;
2046
2047 Next (Choice);
2048
2049 if No (Choice) then
2050
2051 -- Check if we have a single discrete choice and whether
2052 -- this discrete choice specifies a single value.
2053
2054 Single_Choice :=
2055 (Nb_Discrete_Choices = Prev_Nb_Discrete_Choices + 1)
2056 and then (Low = High);
2057
2058 exit;
2059 end if;
2060 end loop;
2061
2062 -- Ada 2005 (AI-231)
2063
2064 if Ada_Version >= Ada_2005
2065 and then Known_Null (Expression (Assoc))
2066 and then not Empty_Range (Assoc)
2067 then
2068 Check_Can_Never_Be_Null (Etype (N), Expression (Assoc));
2069 end if;
2070
2071 -- Ada 2005 (AI-287): In case of default initialized component
2072 -- we delay the resolution to the expansion phase.
2073
2074 if Box_Present (Assoc) then
2075
2076 -- Ada 2005 (AI-287): In case of default initialization of a
2077 -- component the expander will generate calls to the
2078 -- corresponding initialization subprogram. We need to call
2079 -- Resolve_Aggr_Expr to check the rules about
2080 -- dimensionality.
2081
2082 if not Resolve_Aggr_Expr
2083 (Assoc, Single_Elmt => Single_Choice)
2084 then
2085 return Failure;
2086 end if;
2087
2088 -- ??? Checks for dynamically tagged expressions below will
2089 -- be only applied to iterated_component_association after
2090 -- expansion; in particular, errors might not be reported when
2091 -- -gnatc switch is used.
2092
2093 elsif Nkind (Assoc) = N_Iterated_Component_Association then
2094 null; -- handled above, in a loop context
2095
2096 elsif not Resolve_Aggr_Expr
2097 (Expression (Assoc), Single_Elmt => Single_Choice)
2098 then
2099 return Failure;
2100
2101 -- Check incorrect use of dynamically tagged expression
2102
2103 -- We differentiate here two cases because the expression may
2104 -- not be decorated. For example, the analysis and resolution
2105 -- of the expression associated with the others choice will be
2106 -- done later with the full aggregate. In such case we
2107 -- duplicate the expression tree to analyze the copy and
2108 -- perform the required check.
2109
2110 elsif not Present (Etype (Expression (Assoc))) then
2111 declare
2112 Save_Analysis : constant Boolean := Full_Analysis;
2113 Expr : constant Node_Id :=
2114 New_Copy_Tree (Expression (Assoc));
2115
2116 begin
2117 Expander_Mode_Save_And_Set (False);
2118 Full_Analysis := False;
2119
2120 -- Analyze the expression, making sure it is properly
2121 -- attached to the tree before we do the analysis.
2122
2123 Set_Parent (Expr, Parent (Expression (Assoc)));
2124 Analyze (Expr);
2125
2126 -- Compute its dimensions now, rather than at the end of
2127 -- resolution, because in the case of multidimensional
2128 -- aggregates subsequent expansion may lead to spurious
2129 -- errors.
2130
2131 Check_Expression_Dimensions (Expr, Component_Typ);
2132
2133 -- If the expression is a literal, propagate this info
2134 -- to the expression in the association, to enable some
2135 -- optimizations downstream.
2136
2137 if Is_Entity_Name (Expr)
2138 and then Present (Entity (Expr))
2139 and then Ekind (Entity (Expr)) = E_Enumeration_Literal
2140 then
2141 Analyze_And_Resolve
2142 (Expression (Assoc), Component_Typ);
2143 end if;
2144
2145 Full_Analysis := Save_Analysis;
2146 Expander_Mode_Restore;
2147
2148 if Is_Tagged_Type (Etype (Expr)) then
2149 Check_Dynamically_Tagged_Expression
2150 (Expr => Expr,
2151 Typ => Component_Type (Etype (N)),
2152 Related_Nod => N);
2153 end if;
2154 end;
2155
2156 elsif Is_Tagged_Type (Etype (Expression (Assoc))) then
2157 Check_Dynamically_Tagged_Expression
2158 (Expr => Expression (Assoc),
2159 Typ => Component_Type (Etype (N)),
2160 Related_Nod => N);
2161 end if;
2162
2163 Next (Assoc);
2164 end loop;
2165
2166 -- If aggregate contains more than one choice then these must be
2167 -- static. Check for duplicate and missing values.
2168
2169 -- Note: there is duplicated code here wrt Check_Choice_Set in
2170 -- the body of Sem_Case, and it is possible we could just reuse
2171 -- that procedure. To be checked ???
2172
2173 if Nb_Discrete_Choices > 1 then
2174 Check_Choices : declare
2175 Choice : Node_Id;
2176 -- Location of choice for messages
2177
2178 Hi_Val : Uint;
2179 Lo_Val : Uint;
2180 -- High end of one range and Low end of the next. Should be
2181 -- contiguous if there is no hole in the list of values.
2182
2183 Lo_Dup : Uint;
2184 Hi_Dup : Uint;
2185 -- End points of duplicated range
2186
2187 Missing_Or_Duplicates : Boolean := False;
2188 -- Set True if missing or duplicate choices found
2189
2190 procedure Output_Bad_Choices (Lo, Hi : Uint; C : Node_Id);
2191 -- Output continuation message with a representation of the
2192 -- bounds (just Lo if Lo = Hi, else Lo .. Hi). C is the
2193 -- choice node where the message is to be posted.
2194
2195 ------------------------
2196 -- Output_Bad_Choices --
2197 ------------------------
2198
2199 procedure Output_Bad_Choices (Lo, Hi : Uint; C : Node_Id) is
2200 begin
2201 -- Enumeration type case
2202
2203 if Is_Enumeration_Type (Index_Typ) then
2204 Error_Msg_Name_1 :=
2205 Chars (Get_Enum_Lit_From_Pos (Index_Typ, Lo, Loc));
2206 Error_Msg_Name_2 :=
2207 Chars (Get_Enum_Lit_From_Pos (Index_Typ, Hi, Loc));
2208
2209 if Lo = Hi then
2210 Error_Msg_N ("\\ %!", C);
2211 else
2212 Error_Msg_N ("\\ % .. %!", C);
2213 end if;
2214
2215 -- Integer types case
2216
2217 else
2218 Error_Msg_Uint_1 := Lo;
2219 Error_Msg_Uint_2 := Hi;
2220
2221 if Lo = Hi then
2222 Error_Msg_N ("\\ ^!", C);
2223 else
2224 Error_Msg_N ("\\ ^ .. ^!", C);
2225 end if;
2226 end if;
2227 end Output_Bad_Choices;
2228
2229 -- Start of processing for Check_Choices
2230
2231 begin
2232 Sort_Case_Table (Table);
2233
2234 -- First we do a quick linear loop to find out if we have
2235 -- any duplicates or missing entries (usually we have a
2236 -- legal aggregate, so this will get us out quickly).
2237
2238 for J in 1 .. Nb_Discrete_Choices - 1 loop
2239 Hi_Val := Expr_Value (Table (J).Hi);
2240 Lo_Val := Expr_Value (Table (J + 1).Lo);
2241
2242 if Lo_Val <= Hi_Val
2243 or else (Lo_Val > Hi_Val + 1
2244 and then not Others_Present)
2245 then
2246 Missing_Or_Duplicates := True;
2247 exit;
2248 end if;
2249 end loop;
2250
2251 -- If we have missing or duplicate entries, first fill in
2252 -- the Highest entries to make life easier in the following
2253 -- loops to detect bad entries.
2254
2255 if Missing_Or_Duplicates then
2256 Table (1).Highest := Expr_Value (Table (1).Hi);
2257
2258 for J in 2 .. Nb_Discrete_Choices loop
2259 Table (J).Highest :=
2260 UI_Max
2261 (Table (J - 1).Highest, Expr_Value (Table (J).Hi));
2262 end loop;
2263
2264 -- Loop through table entries to find duplicate indexes
2265
2266 for J in 2 .. Nb_Discrete_Choices loop
2267 Lo_Val := Expr_Value (Table (J).Lo);
2268 Hi_Val := Expr_Value (Table (J).Hi);
2269
2270 -- Case where we have duplicates (the lower bound of
2271 -- this choice is less than or equal to the highest
2272 -- high bound found so far).
2273
2274 if Lo_Val <= Table (J - 1).Highest then
2275
2276 -- We move backwards looking for duplicates. We can
2277 -- abandon this loop as soon as we reach a choice
2278 -- highest value that is less than Lo_Val.
2279
2280 for K in reverse 1 .. J - 1 loop
2281 exit when Table (K).Highest < Lo_Val;
2282
2283 -- Here we may have duplicates between entries
2284 -- for K and J. Get range of duplicates.
2285
2286 Lo_Dup :=
2287 UI_Max (Lo_Val, Expr_Value (Table (K).Lo));
2288 Hi_Dup :=
2289 UI_Min (Hi_Val, Expr_Value (Table (K).Hi));
2290
2291 -- Nothing to do if duplicate range is null
2292
2293 if Lo_Dup > Hi_Dup then
2294 null;
2295
2296 -- Otherwise place proper message
2297
2298 else
2299 -- We place message on later choice, with a
2300 -- line reference to the earlier choice.
2301
2302 if Sloc (Table (J).Choice) <
2303 Sloc (Table (K).Choice)
2304 then
2305 Choice := Table (K).Choice;
2306 Error_Msg_Sloc := Sloc (Table (J).Choice);
2307 else
2308 Choice := Table (J).Choice;
2309 Error_Msg_Sloc := Sloc (Table (K).Choice);
2310 end if;
2311
2312 if Lo_Dup = Hi_Dup then
2313 Error_Msg_N
2314 ("index value in array aggregate "
2315 & "duplicates the one given#!", Choice);
2316 else
2317 Error_Msg_N
2318 ("index values in array aggregate "
2319 & "duplicate those given#!", Choice);
2320 end if;
2321
2322 Output_Bad_Choices (Lo_Dup, Hi_Dup, Choice);
2323 end if;
2324 end loop;
2325 end if;
2326 end loop;
2327
2328 -- Loop through entries in table to find missing indexes.
2329 -- Not needed if others, since missing impossible.
2330
2331 if not Others_Present then
2332 for J in 2 .. Nb_Discrete_Choices loop
2333 Lo_Val := Expr_Value (Table (J).Lo);
2334 Hi_Val := Table (J - 1).Highest;
2335
2336 if Lo_Val > Hi_Val + 1 then
2337
2338 declare
2339 Error_Node : Node_Id;
2340
2341 begin
2342 -- If the choice is the bound of a range in
2343 -- a subtype indication, it is not in the
2344 -- source lists for the aggregate itself, so
2345 -- post the error on the aggregate. Otherwise
2346 -- post it on choice itself.
2347
2348 Choice := Table (J).Choice;
2349
2350 if Is_List_Member (Choice) then
2351 Error_Node := Choice;
2352 else
2353 Error_Node := N;
2354 end if;
2355
2356 if Hi_Val + 1 = Lo_Val - 1 then
2357 Error_Msg_N
2358 ("missing index value "
2359 & "in array aggregate!", Error_Node);
2360 else
2361 Error_Msg_N
2362 ("missing index values "
2363 & "in array aggregate!", Error_Node);
2364 end if;
2365
2366 Output_Bad_Choices
2367 (Hi_Val + 1, Lo_Val - 1, Error_Node);
2368 end;
2369 end if;
2370 end loop;
2371 end if;
2372
2373 -- If either missing or duplicate values, return failure
2374
2375 Set_Etype (N, Any_Composite);
2376 return Failure;
2377 end if;
2378 end Check_Choices;
2379 end if;
2380
2381 -- STEP 2 (B): Compute aggregate bounds and min/max choices values
2382
2383 if Nb_Discrete_Choices > 0 then
2384 Choices_Low := Table (1).Lo;
2385 Choices_High := Table (Nb_Discrete_Choices).Hi;
2386 end if;
2387
2388 -- If Others is present, then bounds of aggregate come from the
2389 -- index constraint (not the choices in the aggregate itself).
2390
2391 if Others_Present then
2392 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2393
2394 -- Abandon processing if either bound is already signalled as
2395 -- an error (prevents junk cascaded messages and blow ups).
2396
2397 if Nkind (Aggr_Low) = N_Error
2398 or else
2399 Nkind (Aggr_High) = N_Error
2400 then
2401 return False;
2402 end if;
2403
2404 -- No others clause present
2405
2406 else
2407 -- Special processing if others allowed and not present. This
2408 -- means that the bounds of the aggregate come from the index
2409 -- constraint (and the length must match).
2410
2411 if Others_Allowed then
2412 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2413
2414 -- Abandon processing if either bound is already signalled
2415 -- as an error (stop junk cascaded messages and blow ups).
2416
2417 if Nkind (Aggr_Low) = N_Error
2418 or else
2419 Nkind (Aggr_High) = N_Error
2420 then
2421 return False;
2422 end if;
2423
2424 -- If others allowed, and no others present, then the array
2425 -- should cover all index values. If it does not, we will
2426 -- get a length check warning, but there is two cases where
2427 -- an additional warning is useful:
2428
2429 -- If we have no positional components, and the length is
2430 -- wrong (which we can tell by others being allowed with
2431 -- missing components), and the index type is an enumeration
2432 -- type, then issue appropriate warnings about these missing
2433 -- components. They are only warnings, since the aggregate
2434 -- is fine, it's just the wrong length. We skip this check
2435 -- for standard character types (since there are no literals
2436 -- and it is too much trouble to concoct them), and also if
2437 -- any of the bounds have values that are not known at
2438 -- compile time.
2439
2440 -- Another case warranting a warning is when the length
2441 -- is right, but as above we have an index type that is
2442 -- an enumeration, and the bounds do not match. This is a
2443 -- case where dubious sliding is allowed and we generate a
2444 -- warning that the bounds do not match.
2445
2446 if No (Expressions (N))
2447 and then Nkind (Index) = N_Range
2448 and then Is_Enumeration_Type (Etype (Index))
2449 and then not Is_Standard_Character_Type (Etype (Index))
2450 and then Compile_Time_Known_Value (Aggr_Low)
2451 and then Compile_Time_Known_Value (Aggr_High)
2452 and then Compile_Time_Known_Value (Choices_Low)
2453 and then Compile_Time_Known_Value (Choices_High)
2454 then
2455 -- If any of the expressions or range bounds in choices
2456 -- have semantic errors, then do not attempt further
2457 -- resolution, to prevent cascaded errors.
2458
2459 if Errors_Posted_On_Choices then
2460 return Failure;
2461 end if;
2462
2463 declare
2464 ALo : constant Node_Id := Expr_Value_E (Aggr_Low);
2465 AHi : constant Node_Id := Expr_Value_E (Aggr_High);
2466 CLo : constant Node_Id := Expr_Value_E (Choices_Low);
2467 CHi : constant Node_Id := Expr_Value_E (Choices_High);
2468
2469 Ent : Entity_Id;
2470
2471 begin
2472 -- Warning case 1, missing values at start/end. Only
2473 -- do the check if the number of entries is too small.
2474
2475 if (Enumeration_Pos (CHi) - Enumeration_Pos (CLo))
2476 <
2477 (Enumeration_Pos (AHi) - Enumeration_Pos (ALo))
2478 then
2479 Error_Msg_N
2480 ("missing index value(s) in array aggregate??",
2481 N);
2482
2483 -- Output missing value(s) at start
2484
2485 if Chars (ALo) /= Chars (CLo) then
2486 Ent := Prev (CLo);
2487
2488 if Chars (ALo) = Chars (Ent) then
2489 Error_Msg_Name_1 := Chars (ALo);
2490 Error_Msg_N ("\ %??", N);
2491 else
2492 Error_Msg_Name_1 := Chars (ALo);
2493 Error_Msg_Name_2 := Chars (Ent);
2494 Error_Msg_N ("\ % .. %??", N);
2495 end if;
2496 end if;
2497
2498 -- Output missing value(s) at end
2499
2500 if Chars (AHi) /= Chars (CHi) then
2501 Ent := Next (CHi);
2502
2503 if Chars (AHi) = Chars (Ent) then
2504 Error_Msg_Name_1 := Chars (Ent);
2505 Error_Msg_N ("\ %??", N);
2506 else
2507 Error_Msg_Name_1 := Chars (Ent);
2508 Error_Msg_Name_2 := Chars (AHi);
2509 Error_Msg_N ("\ % .. %??", N);
2510 end if;
2511 end if;
2512
2513 -- Warning case 2, dubious sliding. The First_Subtype
2514 -- test distinguishes between a constrained type where
2515 -- sliding is not allowed (so we will get a warning
2516 -- later that Constraint_Error will be raised), and
2517 -- the unconstrained case where sliding is permitted.
2518
2519 elsif (Enumeration_Pos (CHi) - Enumeration_Pos (CLo))
2520 =
2521 (Enumeration_Pos (AHi) - Enumeration_Pos (ALo))
2522 and then Chars (ALo) /= Chars (CLo)
2523 and then
2524 not Is_Constrained (First_Subtype (Etype (N)))
2525 then
2526 Error_Msg_N
2527 ("bounds of aggregate do not match target??", N);
2528 end if;
2529 end;
2530 end if;
2531 end if;
2532
2533 -- If no others, aggregate bounds come from aggregate
2534
2535 Aggr_Low := Choices_Low;
2536 Aggr_High := Choices_High;
2537 end if;
2538 end Step_2;
2539
2540 -- STEP 3: Process positional components
2541
2542 else
2543 -- STEP 3 (A): Process positional elements
2544
2545 Expr := First (Expressions (N));
2546 Nb_Elements := Uint_0;
2547 while Present (Expr) loop
2548 Nb_Elements := Nb_Elements + 1;
2549
2550 -- Ada 2005 (AI-231)
2551
2552 if Ada_Version >= Ada_2005 and then Known_Null (Expr) then
2553 Check_Can_Never_Be_Null (Etype (N), Expr);
2554 end if;
2555
2556 if not Resolve_Aggr_Expr (Expr, Single_Elmt => True) then
2557 return Failure;
2558 end if;
2559
2560 -- Check incorrect use of dynamically tagged expression
2561
2562 if Is_Tagged_Type (Etype (Expr)) then
2563 Check_Dynamically_Tagged_Expression
2564 (Expr => Expr,
2565 Typ => Component_Type (Etype (N)),
2566 Related_Nod => N);
2567 end if;
2568
2569 Next (Expr);
2570 end loop;
2571
2572 if Others_Present then
2573 Assoc := Last (Component_Associations (N));
2574
2575 -- Ada 2005 (AI-231)
2576
2577 if Ada_Version >= Ada_2005 and then Known_Null (Assoc) then
2578 Check_Can_Never_Be_Null (Etype (N), Expression (Assoc));
2579 end if;
2580
2581 -- Ada 2005 (AI-287): In case of default initialized component,
2582 -- we delay the resolution to the expansion phase.
2583
2584 if Box_Present (Assoc) then
2585
2586 -- Ada 2005 (AI-287): In case of default initialization of a
2587 -- component the expander will generate calls to the
2588 -- corresponding initialization subprogram. We need to call
2589 -- Resolve_Aggr_Expr to check the rules about
2590 -- dimensionality.
2591
2592 if not Resolve_Aggr_Expr (Assoc, Single_Elmt => False) then
2593 return Failure;
2594 end if;
2595
2596 elsif not Resolve_Aggr_Expr (Expression (Assoc),
2597 Single_Elmt => False)
2598 then
2599 return Failure;
2600
2601 -- Check incorrect use of dynamically tagged expression. The
2602 -- expression of the others choice has not been resolved yet.
2603 -- In order to diagnose the semantic error we create a duplicate
2604 -- tree to analyze it and perform the check.
2605
2606 elsif Nkind (Assoc) /= N_Iterated_Component_Association then
2607 declare
2608 Save_Analysis : constant Boolean := Full_Analysis;
2609 Expr : constant Node_Id :=
2610 New_Copy_Tree (Expression (Assoc));
2611
2612 begin
2613 Expander_Mode_Save_And_Set (False);
2614 Full_Analysis := False;
2615 Analyze (Expr);
2616 Full_Analysis := Save_Analysis;
2617 Expander_Mode_Restore;
2618
2619 if Is_Tagged_Type (Etype (Expr)) then
2620 Check_Dynamically_Tagged_Expression
2621 (Expr => Expr,
2622 Typ => Component_Type (Etype (N)),
2623 Related_Nod => N);
2624 end if;
2625 end;
2626 end if;
2627 end if;
2628
2629 -- STEP 3 (B): Compute the aggregate bounds
2630
2631 if Others_Present then
2632 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2633
2634 else
2635 if Others_Allowed then
2636 Get_Index_Bounds (Index_Constr, Aggr_Low, Discard);
2637 else
2638 Aggr_Low := Index_Typ_Low;
2639 end if;
2640
2641 Aggr_High := Add (Nb_Elements - 1, To => Aggr_Low);
2642 Check_Bound (Index_Base_High, Aggr_High);
2643 end if;
2644 end if;
2645
2646 -- STEP 4: Perform static aggregate checks and save the bounds
2647
2648 -- Check (A)
2649
2650 Check_Bounds (Index_Typ_Low, Index_Typ_High, Aggr_Low, Aggr_High);
2651 Check_Bounds (Index_Base_Low, Index_Base_High, Aggr_Low, Aggr_High);
2652
2653 -- Check (B)
2654
2655 if Others_Present and then Nb_Discrete_Choices > 0 then
2656 Check_Bounds (Aggr_Low, Aggr_High, Choices_Low, Choices_High);
2657 Check_Bounds (Index_Typ_Low, Index_Typ_High,
2658 Choices_Low, Choices_High);
2659 Check_Bounds (Index_Base_Low, Index_Base_High,
2660 Choices_Low, Choices_High);
2661
2662 -- Check (C)
2663
2664 elsif Others_Present and then Nb_Elements > 0 then
2665 Check_Length (Aggr_Low, Aggr_High, Nb_Elements);
2666 Check_Length (Index_Typ_Low, Index_Typ_High, Nb_Elements);
2667 Check_Length (Index_Base_Low, Index_Base_High, Nb_Elements);
2668 end if;
2669
2670 if Raises_Constraint_Error (Aggr_Low)
2671 or else Raises_Constraint_Error (Aggr_High)
2672 then
2673 Set_Raises_Constraint_Error (N);
2674 end if;
2675
2676 Aggr_Low := Duplicate_Subexpr (Aggr_Low);
2677
2678 -- Do not duplicate Aggr_High if Aggr_High = Aggr_Low + Nb_Elements
2679 -- since the addition node returned by Add is not yet analyzed. Attach
2680 -- to tree and analyze first. Reset analyzed flag to ensure it will get
2681 -- analyzed when it is a literal bound whose type must be properly set.
2682
2683 if Others_Present or else Nb_Discrete_Choices > 0 then
2684 Aggr_High := Duplicate_Subexpr (Aggr_High);
2685
2686 if Etype (Aggr_High) = Universal_Integer then
2687 Set_Analyzed (Aggr_High, False);
2688 end if;
2689 end if;
2690
2691 -- If the aggregate already has bounds attached to it, it means this is
2692 -- a positional aggregate created as an optimization by
2693 -- Exp_Aggr.Convert_To_Positional, so we don't want to change those
2694 -- bounds.
2695
2696 if Present (Aggregate_Bounds (N)) and then not Others_Allowed then
2697 Aggr_Low := Low_Bound (Aggregate_Bounds (N));
2698 Aggr_High := High_Bound (Aggregate_Bounds (N));
2699 end if;
2700
2701 Set_Aggregate_Bounds
2702 (N, Make_Range (Loc, Low_Bound => Aggr_Low, High_Bound => Aggr_High));
2703
2704 -- The bounds may contain expressions that must be inserted upwards.
2705 -- Attach them fully to the tree. After analysis, remove side effects
2706 -- from upper bound, if still needed.
2707
2708 Set_Parent (Aggregate_Bounds (N), N);
2709 Analyze_And_Resolve (Aggregate_Bounds (N), Index_Typ);
2710 Check_Unset_Reference (Aggregate_Bounds (N));
2711
2712 if not Others_Present and then Nb_Discrete_Choices = 0 then
2713 Set_High_Bound
2714 (Aggregate_Bounds (N),
2715 Duplicate_Subexpr (High_Bound (Aggregate_Bounds (N))));
2716 end if;
2717
2718 -- Check the dimensions of each component in the array aggregate
2719
2720 Analyze_Dimension_Array_Aggregate (N, Component_Typ);
2721
2722 return Success;
2723 end Resolve_Array_Aggregate;
2724
2725 ---------------------------------
2726 -- Resolve_Container_Aggregate --
2727 ---------------------------------
2728
2729 procedure Resolve_Container_Aggregate (N : Node_Id; Typ : Entity_Id) is
2730 procedure Resolve_Iterated_Association
2731 (Comp : Node_Id;
2732 Key_Type : Entity_Id;
2733 Elmt_Type : Entity_Id);
2734 -- Resolve choices and expression in an iterated component association
2735 -- or an iterated element association, which has a key_expression.
2736 -- This is similar but not identical to the handling of this construct
2737 -- in an array aggregate.
2738 -- For a named container, the type of each choice must be compatible
2739 -- with the key type. For a positional container, the choice must be
2740 -- a subtype indication or an iterator specification that determines
2741 -- an element type.
2742
2743 Asp : constant Node_Id := Find_Value_Of_Aspect (Typ, Aspect_Aggregate);
2744
2745 Empty_Subp : Node_Id := Empty;
2746 Add_Named_Subp : Node_Id := Empty;
2747 Add_Unnamed_Subp : Node_Id := Empty;
2748 New_Indexed_Subp : Node_Id := Empty;
2749 Assign_Indexed_Subp : Node_Id := Empty;
2750
2751 ----------------------------------
2752 -- Resolve_Iterated_Association --
2753 ----------------------------------
2754
2755 procedure Resolve_Iterated_Association
2756 (Comp : Node_Id;
2757 Key_Type : Entity_Id;
2758 Elmt_Type : Entity_Id)
2759 is
2760 Choice : Node_Id;
2761 Ent : Entity_Id;
2762 Expr : Node_Id;
2763 Key_Expr : Node_Id;
2764 Id : Entity_Id;
2765 Id_Name : Name_Id;
2766 Iter : Node_Id;
2767 Typ : Entity_Id := Empty;
2768
2769 begin
2770 -- If this is an Iterated_Element_Association then either a
2771 -- an Iterator_Specification or a Loop_Parameter specification
2772 -- is present. In both cases a Key_Expression is present.
2773
2774 if Nkind (Comp) = N_Iterated_Element_Association then
2775 if Present (Loop_Parameter_Specification (Comp)) then
2776 Analyze_Loop_Parameter_Specification
2777 (Loop_Parameter_Specification (Comp));
2778 Id_Name := Chars (Defining_Identifier
2779 (Loop_Parameter_Specification (Comp)));
2780 else
2781 Iter := Copy_Separate_Tree (Iterator_Specification (Comp));
2782 Analyze (Iter);
2783 Typ := Etype (Defining_Identifier (Iter));
2784 Id_Name := Chars (Defining_Identifier
2785 (Iterator_Specification (Comp)));
2786 end if;
2787
2788 -- Key expression must have the type of the key. We analyze
2789 -- a copy of the original expression, because it will be
2790 -- reanalyzed and copied as needed during expansion of the
2791 -- corresponding loop.
2792
2793 Key_Expr := Key_Expression (Comp);
2794 Analyze_And_Resolve (New_Copy_Tree (Key_Expr), Key_Type);
2795
2796 elsif Present (Iterator_Specification (Comp)) then
2797 Iter := Copy_Separate_Tree (Iterator_Specification (Comp));
2798 Id_Name := Chars (Defining_Identifier (Comp));
2799 Analyze (Iter);
2800 Typ := Etype (Defining_Identifier (Iter));
2801
2802 else
2803 Choice := First (Discrete_Choices (Comp));
2804
2805 while Present (Choice) loop
2806 Analyze (Choice);
2807
2808 -- Choice can be a subtype name, a range, or an expression
2809
2810 if Is_Entity_Name (Choice)
2811 and then Is_Type (Entity (Choice))
2812 and then Base_Type (Entity (Choice)) = Base_Type (Key_Type)
2813 then
2814 null;
2815
2816 elsif Present (Key_Type) then
2817 Analyze_And_Resolve (Choice, Key_Type);
2818
2819 else
2820 Typ := Etype (Choice); -- assume unique for now
2821 end if;
2822
2823 Next (Choice);
2824 end loop;
2825
2826 Id_Name := Chars (Defining_Identifier (Comp));
2827 end if;
2828
2829 -- Create a scope in which to introduce an index, which is usually
2830 -- visible in the expression for the component, and needed for its
2831 -- analysis.
2832
2833 Id := Make_Defining_Identifier (Sloc (Comp), Id_Name);
2834 Ent := New_Internal_Entity (E_Loop, Current_Scope, Sloc (Comp), 'L');
2835 Set_Etype (Ent, Standard_Void_Type);
2836 Set_Parent (Ent, Parent (Comp));
2837 Push_Scope (Ent);
2838
2839 -- Insert and decorate the loop variable in the current scope.
2840 -- The expression has to be analyzed once the loop variable is
2841 -- directly visible. Mark the variable as referenced to prevent
2842 -- spurious warnings, given that subsequent uses of its name in the
2843 -- expression will reference the internal (synonym) loop variable.
2844
2845 Enter_Name (Id);
2846
2847 if No (Key_Type) then
2848 pragma Assert (Present (Typ));
2849 Set_Etype (Id, Typ);
2850 else
2851 Set_Etype (Id, Key_Type);
2852 end if;
2853
2854 Set_Ekind (Id, E_Variable);
2855 Set_Scope (Id, Ent);
2856 Set_Referenced (Id);
2857
2858 -- Analyze a copy of the expression, to verify legality. We use
2859 -- a copy because the expression will be analyzed anew when the
2860 -- enclosing aggregate is expanded, and the construct is rewritten
2861 -- as a loop with a new index variable.
2862
2863 Expr := New_Copy_Tree (Expression (Comp));
2864 Preanalyze_And_Resolve (Expr, Elmt_Type);
2865 End_Scope;
2866
2867 end Resolve_Iterated_Association;
2868
2869 begin
2870 pragma Assert (Nkind (Asp) = N_Aggregate);
2871
2872 Set_Etype (N, Typ);
2873 Parse_Aspect_Aggregate (Asp,
2874 Empty_Subp, Add_Named_Subp, Add_Unnamed_Subp,
2875 New_Indexed_Subp, Assign_Indexed_Subp);
2876
2877 if Present (Add_Unnamed_Subp)
2878 and then No (New_Indexed_Subp)
2879 then
2880 declare
2881 Elmt_Type : constant Entity_Id :=
2882 Etype (Next_Formal
2883 (First_Formal (Entity (Add_Unnamed_Subp))));
2884 Comp : Node_Id;
2885
2886 begin
2887 if Present (Expressions (N)) then
2888 -- positional aggregate
2889
2890 Comp := First (Expressions (N));
2891 while Present (Comp) loop
2892 Analyze_And_Resolve (Comp, Elmt_Type);
2893 Next (Comp);
2894 end loop;
2895 end if;
2896
2897 -- Empty aggregate, to be replaced by Empty during
2898 -- expansion, or iterated component association.
2899
2900 if Present (Component_Associations (N)) then
2901 declare
2902 Comp : Node_Id := First (Component_Associations (N));
2903 begin
2904 while Present (Comp) loop
2905 if Nkind (Comp) /=
2906 N_Iterated_Component_Association
2907 then
2908 Error_Msg_N ("illegal component association "
2909 & "for unnamed container aggregate", Comp);
2910 return;
2911 else
2912 Resolve_Iterated_Association
2913 (Comp, Empty, Elmt_Type);
2914 end if;
2915
2916 Next (Comp);
2917 end loop;
2918 end;
2919 end if;
2920 end;
2921
2922 elsif Present (Add_Named_Subp) then
2923 declare
2924 -- Retrieves types of container, key, and element from the
2925 -- specified insertion procedure.
2926
2927 Container : constant Entity_Id :=
2928 First_Formal (Entity (Add_Named_Subp));
2929 Key_Type : constant Entity_Id := Etype (Next_Formal (Container));
2930 Elmt_Type : constant Entity_Id :=
2931 Etype (Next_Formal (Next_Formal (Container)));
2932 Comp : Node_Id;
2933 Choice : Node_Id;
2934
2935 begin
2936 Comp := First (Component_Associations (N));
2937 while Present (Comp) loop
2938 if Nkind (Comp) = N_Component_Association then
2939 Choice := First (Choices (Comp));
2940
2941 while Present (Choice) loop
2942 Analyze_And_Resolve (Choice, Key_Type);
2943 if not Is_Static_Expression (Choice) then
2944 Error_Msg_N ("Choice must be static", Choice);
2945 end if;
2946
2947 Next (Choice);
2948 end loop;
2949
2950 Analyze_And_Resolve (Expression (Comp), Elmt_Type);
2951
2952 elsif Nkind (Comp) in
2953 N_Iterated_Component_Association |
2954 N_Iterated_Element_Association
2955 then
2956 Resolve_Iterated_Association
2957 (Comp, Key_Type, Elmt_Type);
2958 end if;
2959
2960 Next (Comp);
2961 end loop;
2962 end;
2963
2964 else
2965 -- Indexed Aggregate. Positional or indexed component
2966 -- can be present, but not both. Choices must be static
2967 -- values or ranges with static bounds.
2968
2969 declare
2970 Container : constant Entity_Id :=
2971 First_Formal (Entity (Assign_Indexed_Subp));
2972 Index_Type : constant Entity_Id := Etype (Next_Formal (Container));
2973 Comp_Type : constant Entity_Id :=
2974 Etype (Next_Formal (Next_Formal (Container)));
2975 Comp : Node_Id;
2976 Choice : Node_Id;
2977
2978 begin
2979 if Present (Expressions (N)) then
2980 Comp := First (Expressions (N));
2981 while Present (Comp) loop
2982 Analyze_And_Resolve (Comp, Comp_Type);
2983 Next (Comp);
2984 end loop;
2985 end if;
2986
2987 if Present (Component_Associations (N)) then
2988 if Present (Expressions (N)) then
2989 Error_Msg_N ("Container aggregate cannot be "
2990 & "both positional and named", N);
2991 return;
2992 end if;
2993
2994 Comp := First (Expressions (N));
2995
2996 while Present (Comp) loop
2997 if Nkind (Comp) = N_Component_Association then
2998 Choice := First (Choices (Comp));
2999
3000 while Present (Choice) loop
3001 Analyze_And_Resolve (Choice, Index_Type);
3002 Next (Choice);
3003 end loop;
3004
3005 Analyze_And_Resolve (Expression (Comp), Comp_Type);
3006
3007 elsif Nkind (Comp) in
3008 N_Iterated_Component_Association |
3009 N_Iterated_Element_Association
3010 then
3011 Resolve_Iterated_Association
3012 (Comp, Index_Type, Comp_Type);
3013 end if;
3014
3015 Next (Comp);
3016 end loop;
3017 end if;
3018 end;
3019 end if;
3020 end Resolve_Container_Aggregate;
3021
3022 -----------------------------
3023 -- Resolve_Delta_Aggregate --
3024 -----------------------------
3025
3026 procedure Resolve_Delta_Aggregate (N : Node_Id; Typ : Entity_Id) is
3027 Base : constant Node_Id := Expression (N);
3028
3029 begin
3030 if Ada_Version < Ada_2020 then
3031 Error_Msg_N ("delta_aggregate is an Ada 202x feature", N);
3032 Error_Msg_N ("\compile with -gnat2020", N);
3033 end if;
3034
3035 if not Is_Composite_Type (Typ) then
3036 Error_Msg_N ("not a composite type", N);
3037 end if;
3038
3039 Analyze_And_Resolve (Base, Typ);
3040
3041 if Is_Array_Type (Typ) then
3042 Resolve_Delta_Array_Aggregate (N, Typ);
3043 else
3044 Resolve_Delta_Record_Aggregate (N, Typ);
3045 end if;
3046
3047 Set_Etype (N, Typ);
3048 end Resolve_Delta_Aggregate;
3049
3050 -----------------------------------
3051 -- Resolve_Delta_Array_Aggregate --
3052 -----------------------------------
3053
3054 procedure Resolve_Delta_Array_Aggregate (N : Node_Id; Typ : Entity_Id) is
3055 Deltas : constant List_Id := Component_Associations (N);
3056 Index_Type : constant Entity_Id := Etype (First_Index (Typ));
3057
3058 Assoc : Node_Id;
3059 Choice : Node_Id;
3060 Expr : Node_Id;
3061
3062 begin
3063 Assoc := First (Deltas);
3064 while Present (Assoc) loop
3065 if Nkind (Assoc) = N_Iterated_Component_Association then
3066 Choice := First (Choice_List (Assoc));
3067 while Present (Choice) loop
3068 if Nkind (Choice) = N_Others_Choice then
3069 Error_Msg_N
3070 ("others not allowed in delta aggregate", Choice);
3071
3072 else
3073 Analyze_And_Resolve (Choice, Index_Type);
3074 end if;
3075
3076 Next (Choice);
3077 end loop;
3078
3079 declare
3080 Id : constant Entity_Id := Defining_Identifier (Assoc);
3081 Ent : constant Entity_Id :=
3082 New_Internal_Entity
3083 (E_Loop, Current_Scope, Sloc (Assoc), 'L');
3084
3085 begin
3086 Set_Etype (Ent, Standard_Void_Type);
3087 Set_Parent (Ent, Assoc);
3088 Push_Scope (Ent);
3089
3090 if No (Scope (Id)) then
3091 Set_Etype (Id, Index_Type);
3092 Set_Ekind (Id, E_Variable);
3093 Set_Scope (Id, Ent);
3094 end if;
3095 Enter_Name (Id);
3096
3097 -- Resolve a copy of the expression, after setting
3098 -- its parent properly to preserve its context.
3099
3100 Expr := New_Copy_Tree (Expression (Assoc));
3101 Set_Parent (Expr, Assoc);
3102 Analyze_And_Resolve (Expr, Component_Type (Typ));
3103 End_Scope;
3104 end;
3105
3106 else
3107 Choice := First (Choice_List (Assoc));
3108 while Present (Choice) loop
3109 if Nkind (Choice) = N_Others_Choice then
3110 Error_Msg_N
3111 ("others not allowed in delta aggregate", Choice);
3112
3113 else
3114 Analyze (Choice);
3115
3116 if Is_Entity_Name (Choice)
3117 and then Is_Type (Entity (Choice))
3118 then
3119 -- Choice covers a range of values
3120
3121 if Base_Type (Entity (Choice)) /=
3122 Base_Type (Index_Type)
3123 then
3124 Error_Msg_NE
3125 ("choice does not match index type of &",
3126 Choice, Typ);
3127 end if;
3128 else
3129 Resolve (Choice, Index_Type);
3130 end if;
3131 end if;
3132
3133 Next (Choice);
3134 end loop;
3135
3136 Analyze_And_Resolve (Expression (Assoc), Component_Type (Typ));
3137 end if;
3138
3139 Next (Assoc);
3140 end loop;
3141 end Resolve_Delta_Array_Aggregate;
3142
3143 ------------------------------------
3144 -- Resolve_Delta_Record_Aggregate --
3145 ------------------------------------
3146
3147 procedure Resolve_Delta_Record_Aggregate (N : Node_Id; Typ : Entity_Id) is
3148
3149 -- Variables used to verify that discriminant-dependent components
3150 -- appear in the same variant.
3151
3152 Comp_Ref : Entity_Id := Empty; -- init to avoid warning
3153 Variant : Node_Id;
3154
3155 procedure Check_Variant (Id : Entity_Id);
3156 -- If a given component of the delta aggregate appears in a variant
3157 -- part, verify that it is within the same variant as that of previous
3158 -- specified variant components of the delta.
3159
3160 function Get_Component (Nam : Node_Id) return Entity_Id;
3161 -- Locate component with a given name and return it. If none found then
3162 -- report error and return Empty.
3163
3164 function Nested_In (V1 : Node_Id; V2 : Node_Id) return Boolean;
3165 -- Determine whether variant V1 is within variant V2
3166
3167 function Variant_Depth (N : Node_Id) return Integer;
3168 -- Determine the distance of a variant to the enclosing type
3169 -- declaration.
3170
3171 --------------------
3172 -- Check_Variant --
3173 --------------------
3174
3175 procedure Check_Variant (Id : Entity_Id) is
3176 Comp : Entity_Id;
3177 Comp_Variant : Node_Id;
3178
3179 begin
3180 if not Has_Discriminants (Typ) then
3181 return;
3182 end if;
3183
3184 Comp := First_Entity (Typ);
3185 while Present (Comp) loop
3186 exit when Chars (Comp) = Chars (Id);
3187 Next_Component (Comp);
3188 end loop;
3189
3190 -- Find the variant, if any, whose component list includes the
3191 -- component declaration.
3192
3193 Comp_Variant := Parent (Parent (List_Containing (Parent (Comp))));
3194 if Nkind (Comp_Variant) = N_Variant then
3195 if No (Variant) then
3196 Variant := Comp_Variant;
3197 Comp_Ref := Comp;
3198
3199 elsif Variant /= Comp_Variant then
3200 declare
3201 D1 : constant Integer := Variant_Depth (Variant);
3202 D2 : constant Integer := Variant_Depth (Comp_Variant);
3203
3204 begin
3205 if D1 = D2
3206 or else
3207 (D1 > D2 and then not Nested_In (Variant, Comp_Variant))
3208 or else
3209 (D2 > D1 and then not Nested_In (Comp_Variant, Variant))
3210 then
3211 pragma Assert (Present (Comp_Ref));
3212 Error_Msg_Node_2 := Comp_Ref;
3213 Error_Msg_NE
3214 ("& and & appear in different variants", Id, Comp);
3215
3216 -- Otherwise retain the deeper variant for subsequent tests
3217
3218 elsif D2 > D1 then
3219 Variant := Comp_Variant;
3220 end if;
3221 end;
3222 end if;
3223 end if;
3224 end Check_Variant;
3225
3226 -------------------
3227 -- Get_Component --
3228 -------------------
3229
3230 function Get_Component (Nam : Node_Id) return Entity_Id is
3231 Comp : Entity_Id;
3232
3233 begin
3234 Comp := First_Entity (Typ);
3235 while Present (Comp) loop
3236 if Chars (Comp) = Chars (Nam) then
3237 if Ekind (Comp) = E_Discriminant then
3238 Error_Msg_N ("delta cannot apply to discriminant", Nam);
3239 end if;
3240
3241 return Comp;
3242 end if;
3243
3244 Next_Entity (Comp);
3245 end loop;
3246
3247 Error_Msg_NE ("type& has no component with this name", Nam, Typ);
3248 return Empty;
3249 end Get_Component;
3250
3251 ---------------
3252 -- Nested_In --
3253 ---------------
3254
3255 function Nested_In (V1, V2 : Node_Id) return Boolean is
3256 Par : Node_Id;
3257
3258 begin
3259 Par := Parent (V1);
3260 while Nkind (Par) /= N_Full_Type_Declaration loop
3261 if Par = V2 then
3262 return True;
3263 end if;
3264
3265 Par := Parent (Par);
3266 end loop;
3267
3268 return False;
3269 end Nested_In;
3270
3271 -------------------
3272 -- Variant_Depth --
3273 -------------------
3274
3275 function Variant_Depth (N : Node_Id) return Integer is
3276 Depth : Integer;
3277 Par : Node_Id;
3278
3279 begin
3280 Depth := 0;
3281 Par := Parent (N);
3282 while Nkind (Par) /= N_Full_Type_Declaration loop
3283 Depth := Depth + 1;
3284 Par := Parent (Par);
3285 end loop;
3286
3287 return Depth;
3288 end Variant_Depth;
3289
3290 -- Local variables
3291
3292 Deltas : constant List_Id := Component_Associations (N);
3293
3294 Assoc : Node_Id;
3295 Choice : Node_Id;
3296 Comp : Entity_Id;
3297 Comp_Type : Entity_Id := Empty; -- init to avoid warning
3298
3299 -- Start of processing for Resolve_Delta_Record_Aggregate
3300
3301 begin
3302 Variant := Empty;
3303
3304 Assoc := First (Deltas);
3305 while Present (Assoc) loop
3306 Choice := First (Choice_List (Assoc));
3307 while Present (Choice) loop
3308 Comp := Get_Component (Choice);
3309
3310 if Present (Comp) then
3311 Check_Variant (Choice);
3312
3313 Comp_Type := Etype (Comp);
3314
3315 -- Decorate the component reference by setting its entity and
3316 -- type, as otherwise backends like GNATprove would have to
3317 -- rediscover this information by themselves.
3318
3319 Set_Entity (Choice, Comp);
3320 Set_Etype (Choice, Comp_Type);
3321 else
3322 Comp_Type := Any_Type;
3323 end if;
3324
3325 Next (Choice);
3326 end loop;
3327
3328 pragma Assert (Present (Comp_Type));
3329 Analyze_And_Resolve (Expression (Assoc), Comp_Type);
3330 Next (Assoc);
3331 end loop;
3332 end Resolve_Delta_Record_Aggregate;
3333
3334 ---------------------------------
3335 -- Resolve_Extension_Aggregate --
3336 ---------------------------------
3337
3338 -- There are two cases to consider:
3339
3340 -- a) If the ancestor part is a type mark, the components needed are the
3341 -- difference between the components of the expected type and the
3342 -- components of the given type mark.
3343
3344 -- b) If the ancestor part is an expression, it must be unambiguous, and
3345 -- once we have its type we can also compute the needed components as in
3346 -- the previous case. In both cases, if the ancestor type is not the
3347 -- immediate ancestor, we have to build this ancestor recursively.
3348
3349 -- In both cases, discriminants of the ancestor type do not play a role in
3350 -- the resolution of the needed components, because inherited discriminants
3351 -- cannot be used in a type extension. As a result we can compute
3352 -- independently the list of components of the ancestor type and of the
3353 -- expected type.
3354
3355 procedure Resolve_Extension_Aggregate (N : Node_Id; Typ : Entity_Id) is
3356 A : constant Node_Id := Ancestor_Part (N);
3357 A_Type : Entity_Id;
3358 I : Interp_Index;
3359 It : Interp;
3360
3361 function Valid_Limited_Ancestor (Anc : Node_Id) return Boolean;
3362 -- If the type is limited, verify that the ancestor part is a legal
3363 -- expression (aggregate or function call, including 'Input)) that does
3364 -- not require a copy, as specified in 7.5(2).
3365
3366 function Valid_Ancestor_Type return Boolean;
3367 -- Verify that the type of the ancestor part is a non-private ancestor
3368 -- of the expected type, which must be a type extension.
3369
3370 procedure Transform_BIP_Assignment (Typ : Entity_Id);
3371 -- For an extension aggregate whose ancestor part is a build-in-place
3372 -- call returning a nonlimited type, this is used to transform the
3373 -- assignment to the ancestor part to use a temp.
3374
3375 ----------------------------
3376 -- Valid_Limited_Ancestor --
3377 ----------------------------
3378
3379 function Valid_Limited_Ancestor (Anc : Node_Id) return Boolean is
3380 begin
3381 if Is_Entity_Name (Anc) and then Is_Type (Entity (Anc)) then
3382 return True;
3383
3384 -- The ancestor must be a call or an aggregate, but a call may
3385 -- have been expanded into a temporary, so check original node.
3386
3387 elsif Nkind (Anc) in N_Aggregate
3388 | N_Extension_Aggregate
3389 | N_Function_Call
3390 then
3391 return True;
3392
3393 elsif Nkind (Original_Node (Anc)) = N_Function_Call then
3394 return True;
3395
3396 elsif Nkind (Anc) = N_Attribute_Reference
3397 and then Attribute_Name (Anc) = Name_Input
3398 then
3399 return True;
3400
3401 elsif Nkind (Anc) = N_Qualified_Expression then
3402 return Valid_Limited_Ancestor (Expression (Anc));
3403
3404 elsif Nkind (Anc) = N_Raise_Expression then
3405 return True;
3406
3407 else
3408 return False;
3409 end if;
3410 end Valid_Limited_Ancestor;
3411
3412 -------------------------
3413 -- Valid_Ancestor_Type --
3414 -------------------------
3415
3416 function Valid_Ancestor_Type return Boolean is
3417 Imm_Type : Entity_Id;
3418
3419 begin
3420 Imm_Type := Base_Type (Typ);
3421 while Is_Derived_Type (Imm_Type) loop
3422 if Etype (Imm_Type) = Base_Type (A_Type) then
3423 return True;
3424
3425 -- The base type of the parent type may appear as a private
3426 -- extension if it is declared as such in a parent unit of the
3427 -- current one. For consistency of the subsequent analysis use
3428 -- the partial view for the ancestor part.
3429
3430 elsif Is_Private_Type (Etype (Imm_Type))
3431 and then Present (Full_View (Etype (Imm_Type)))
3432 and then Base_Type (A_Type) = Full_View (Etype (Imm_Type))
3433 then
3434 A_Type := Etype (Imm_Type);
3435 return True;
3436
3437 -- The parent type may be a private extension. The aggregate is
3438 -- legal if the type of the aggregate is an extension of it that
3439 -- is not a private extension.
3440
3441 elsif Is_Private_Type (A_Type)
3442 and then not Is_Private_Type (Imm_Type)
3443 and then Present (Full_View (A_Type))
3444 and then Base_Type (Full_View (A_Type)) = Etype (Imm_Type)
3445 then
3446 return True;
3447
3448 -- The parent type may be a raise expression (which is legal in
3449 -- any expression context).
3450
3451 elsif A_Type = Raise_Type then
3452 A_Type := Etype (Imm_Type);
3453 return True;
3454
3455 else
3456 Imm_Type := Etype (Base_Type (Imm_Type));
3457 end if;
3458 end loop;
3459
3460 -- If previous loop did not find a proper ancestor, report error
3461
3462 Error_Msg_NE ("expect ancestor type of &", A, Typ);
3463 return False;
3464 end Valid_Ancestor_Type;
3465
3466 ------------------------------
3467 -- Transform_BIP_Assignment --
3468 ------------------------------
3469
3470 procedure Transform_BIP_Assignment (Typ : Entity_Id) is
3471 Loc : constant Source_Ptr := Sloc (N);
3472 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'Y', A);
3473 Obj_Decl : constant Node_Id :=
3474 Make_Object_Declaration (Loc,
3475 Defining_Identifier => Def_Id,
3476 Constant_Present => True,
3477 Object_Definition => New_Occurrence_Of (Typ, Loc),
3478 Expression => A,
3479 Has_Init_Expression => True);
3480 begin
3481 Set_Etype (Def_Id, Typ);
3482 Set_Ancestor_Part (N, New_Occurrence_Of (Def_Id, Loc));
3483 Insert_Action (N, Obj_Decl);
3484 end Transform_BIP_Assignment;
3485
3486 -- Start of processing for Resolve_Extension_Aggregate
3487
3488 begin
3489 -- Analyze the ancestor part and account for the case where it is a
3490 -- parameterless function call.
3491
3492 Analyze (A);
3493 Check_Parameterless_Call (A);
3494
3495 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
3496
3497 -- AI05-0115: If the ancestor part is a subtype mark, the ancestor
3498 -- must not have unknown discriminants. To catch cases where the
3499 -- aggregate occurs at a place where the full view of the ancestor
3500 -- type is visible and doesn't have unknown discriminants, but the
3501 -- aggregate type was derived from a partial view that has unknown
3502 -- discriminants, we check whether the aggregate type has unknown
3503 -- discriminants (unknown discriminants were inherited), along
3504 -- with checking that the partial view of the ancestor has unknown
3505 -- discriminants. (It might be sufficient to replace the entire
3506 -- condition with Has_Unknown_Discriminants (Typ), but that might
3507 -- miss some cases, not clear, and causes error changes in some tests
3508 -- such as class-wide cases, that aren't clearly improvements. ???)
3509
3510 if Has_Unknown_Discriminants (Entity (A))
3511 or else (Has_Unknown_Discriminants (Typ)
3512 and then Partial_View_Has_Unknown_Discr (Entity (A)))
3513 then
3514 Error_Msg_NE
3515 ("aggregate not available for type& whose ancestor "
3516 & "has unknown discriminants", N, Typ);
3517 end if;
3518 end if;
3519
3520 if not Is_Tagged_Type (Typ) then
3521 Error_Msg_N ("type of extension aggregate must be tagged", N);
3522 return;
3523
3524 elsif Is_Limited_Type (Typ) then
3525
3526 -- Ada 2005 (AI-287): Limited aggregates are allowed
3527
3528 if Ada_Version < Ada_2005 then
3529 Error_Msg_N ("aggregate type cannot be limited", N);
3530 Explain_Limited_Type (Typ, N);
3531 return;
3532
3533 elsif Valid_Limited_Ancestor (A) then
3534 null;
3535
3536 else
3537 Error_Msg_N
3538 ("limited ancestor part must be aggregate or function call", A);
3539 end if;
3540
3541 elsif Is_Class_Wide_Type (Typ) then
3542 Error_Msg_N ("aggregate cannot be of a class-wide type", N);
3543 return;
3544 end if;
3545
3546 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
3547 A_Type := Get_Full_View (Entity (A));
3548
3549 if Valid_Ancestor_Type then
3550 Set_Entity (A, A_Type);
3551 Set_Etype (A, A_Type);
3552
3553 Validate_Ancestor_Part (N);
3554 Resolve_Record_Aggregate (N, Typ);
3555 end if;
3556
3557 elsif Nkind (A) /= N_Aggregate then
3558 if Is_Overloaded (A) then
3559 A_Type := Any_Type;
3560
3561 Get_First_Interp (A, I, It);
3562 while Present (It.Typ) loop
3563
3564 -- Consider limited interpretations if Ada 2005 or higher
3565
3566 if Is_Tagged_Type (It.Typ)
3567 and then (Ada_Version >= Ada_2005
3568 or else not Is_Limited_Type (It.Typ))
3569 then
3570 if A_Type /= Any_Type then
3571 Error_Msg_N ("cannot resolve expression", A);
3572 return;
3573 else
3574 A_Type := It.Typ;
3575 end if;
3576 end if;
3577
3578 Get_Next_Interp (I, It);
3579 end loop;
3580
3581 if A_Type = Any_Type then
3582 if Ada_Version >= Ada_2005 then
3583 Error_Msg_N
3584 ("ancestor part must be of a tagged type", A);
3585 else
3586 Error_Msg_N
3587 ("ancestor part must be of a nonlimited tagged type", A);
3588 end if;
3589
3590 return;
3591 end if;
3592
3593 else
3594 A_Type := Etype (A);
3595 end if;
3596
3597 if Valid_Ancestor_Type then
3598 Resolve (A, A_Type);
3599 Check_Unset_Reference (A);
3600 Check_Non_Static_Context (A);
3601
3602 -- The aggregate is illegal if the ancestor expression is a call
3603 -- to a function with a limited unconstrained result, unless the
3604 -- type of the aggregate is a null extension. This restriction
3605 -- was added in AI05-67 to simplify implementation.
3606
3607 if Nkind (A) = N_Function_Call
3608 and then Is_Limited_Type (A_Type)
3609 and then not Is_Null_Extension (Typ)
3610 and then not Is_Constrained (A_Type)
3611 then
3612 Error_Msg_N
3613 ("type of limited ancestor part must be constrained", A);
3614
3615 -- Reject the use of CPP constructors that leave objects partially
3616 -- initialized. For example:
3617
3618 -- type CPP_Root is tagged limited record ...
3619 -- pragma Import (CPP, CPP_Root);
3620
3621 -- type CPP_DT is new CPP_Root and Iface ...
3622 -- pragma Import (CPP, CPP_DT);
3623
3624 -- type Ada_DT is new CPP_DT with ...
3625
3626 -- Obj : Ada_DT := Ada_DT'(New_CPP_Root with others => <>);
3627
3628 -- Using the constructor of CPP_Root the slots of the dispatch
3629 -- table of CPP_DT cannot be set, and the secondary tag of
3630 -- CPP_DT is unknown.
3631
3632 elsif Nkind (A) = N_Function_Call
3633 and then Is_CPP_Constructor_Call (A)
3634 and then Enclosing_CPP_Parent (Typ) /= A_Type
3635 then
3636 Error_Msg_NE
3637 ("??must use 'C'P'P constructor for type &", A,
3638 Enclosing_CPP_Parent (Typ));
3639
3640 -- The following call is not needed if the previous warning
3641 -- is promoted to an error.
3642
3643 Resolve_Record_Aggregate (N, Typ);
3644
3645 elsif Is_Class_Wide_Type (Etype (A))
3646 and then Nkind (Original_Node (A)) = N_Function_Call
3647 then
3648 -- If the ancestor part is a dispatching call, it appears
3649 -- statically to be a legal ancestor, but it yields any member
3650 -- of the class, and it is not possible to determine whether
3651 -- it is an ancestor of the extension aggregate (much less
3652 -- which ancestor). It is not possible to determine the
3653 -- components of the extension part.
3654
3655 -- This check implements AI-306, which in fact was motivated by
3656 -- an AdaCore query to the ARG after this test was added.
3657
3658 Error_Msg_N ("ancestor part must be statically tagged", A);
3659 else
3660 -- We are using the build-in-place protocol, but we can't build
3661 -- in place, because we need to call the function before
3662 -- allocating the aggregate. Could do better for null
3663 -- extensions, and maybe for nondiscriminated types.
3664 -- This is wrong for limited, but those were wrong already.
3665
3666 if not Is_Limited_View (A_Type)
3667 and then Is_Build_In_Place_Function_Call (A)
3668 then
3669 Transform_BIP_Assignment (A_Type);
3670 end if;
3671
3672 Resolve_Record_Aggregate (N, Typ);
3673 end if;
3674 end if;
3675
3676 else
3677 Error_Msg_N ("no unique type for this aggregate", A);
3678 end if;
3679
3680 Check_Function_Writable_Actuals (N);
3681 end Resolve_Extension_Aggregate;
3682
3683 ------------------------------
3684 -- Resolve_Record_Aggregate --
3685 ------------------------------
3686
3687 procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id) is
3688 New_Assoc_List : constant List_Id := New_List;
3689 -- New_Assoc_List is the newly built list of N_Component_Association
3690 -- nodes.
3691
3692 Others_Etype : Entity_Id := Empty;
3693 -- This variable is used to save the Etype of the last record component
3694 -- that takes its value from the others choice. Its purpose is:
3695 --
3696 -- (a) make sure the others choice is useful
3697 --
3698 -- (b) make sure the type of all the components whose value is
3699 -- subsumed by the others choice are the same.
3700 --
3701 -- This variable is updated as a side effect of function Get_Value.
3702
3703 Box_Node : Node_Id := Empty;
3704 Is_Box_Present : Boolean := False;
3705 Others_Box : Natural := 0;
3706 -- Ada 2005 (AI-287): Variables used in case of default initialization
3707 -- to provide a functionality similar to Others_Etype. Box_Present
3708 -- indicates that the component takes its default initialization;
3709 -- Others_Box counts the number of components of the current aggregate
3710 -- (which may be a sub-aggregate of a larger one) that are default-
3711 -- initialized. A value of One indicates that an others_box is present.
3712 -- Any larger value indicates that the others_box is not redundant.
3713 -- These variables, similar to Others_Etype, are also updated as a side
3714 -- effect of function Get_Value. Box_Node is used to place a warning on
3715 -- a redundant others_box.
3716
3717 procedure Add_Association
3718 (Component : Entity_Id;
3719 Expr : Node_Id;
3720 Assoc_List : List_Id;
3721 Is_Box_Present : Boolean := False);
3722 -- Builds a new N_Component_Association node which associates Component
3723 -- to expression Expr and adds it to the association list being built,
3724 -- either New_Assoc_List, or the association being built for an inner
3725 -- aggregate.
3726
3727 procedure Add_Discriminant_Values
3728 (New_Aggr : Node_Id;
3729 Assoc_List : List_Id);
3730 -- The constraint to a component may be given by a discriminant of the
3731 -- enclosing type, in which case we have to retrieve its value, which is
3732 -- part of the enclosing aggregate. Assoc_List provides the discriminant
3733 -- associations of the current type or of some enclosing record.
3734
3735 function Discriminant_Present (Input_Discr : Entity_Id) return Boolean;
3736 -- If aggregate N is a regular aggregate this routine will return True.
3737 -- Otherwise, if N is an extension aggregate, then Input_Discr denotes
3738 -- a discriminant whose value may already have been specified by N's
3739 -- ancestor part. This routine checks whether this is indeed the case
3740 -- and if so returns False, signaling that no value for Input_Discr
3741 -- should appear in N's aggregate part. Also, in this case, the routine
3742 -- appends to New_Assoc_List the discriminant value specified in the
3743 -- ancestor part.
3744 --
3745 -- If the aggregate is in a context with expansion delayed, it will be
3746 -- reanalyzed. The inherited discriminant values must not be reinserted
3747 -- in the component list to prevent spurious errors, but they must be
3748 -- present on first analysis to build the proper subtype indications.
3749 -- The flag Inherited_Discriminant is used to prevent the re-insertion.
3750
3751 function Find_Private_Ancestor (Typ : Entity_Id) return Entity_Id;
3752 -- AI05-0115: Find earlier ancestor in the derivation chain that is
3753 -- derived from private view Typ. Whether the aggregate is legal depends
3754 -- on the current visibility of the type as well as that of the parent
3755 -- of the ancestor.
3756
3757 function Get_Value
3758 (Compon : Entity_Id;
3759 From : List_Id;
3760 Consider_Others_Choice : Boolean := False) return Node_Id;
3761 -- Given a record component stored in parameter Compon, this function
3762 -- returns its value as it appears in the list From, which is a list
3763 -- of N_Component_Association nodes.
3764 --
3765 -- If no component association has a choice for the searched component,
3766 -- the value provided by the others choice is returned, if there is one,
3767 -- and Consider_Others_Choice is set to true. Otherwise Empty is
3768 -- returned. If there is more than one component association giving a
3769 -- value for the searched record component, an error message is emitted
3770 -- and the first found value is returned.
3771 --
3772 -- If Consider_Others_Choice is set and the returned expression comes
3773 -- from the others choice, then Others_Etype is set as a side effect.
3774 -- An error message is emitted if the components taking their value from
3775 -- the others choice do not have same type.
3776
3777 procedure Propagate_Discriminants
3778 (Aggr : Node_Id;
3779 Assoc_List : List_Id);
3780 -- Nested components may themselves be discriminated types constrained
3781 -- by outer discriminants, whose values must be captured before the
3782 -- aggregate is expanded into assignments.
3783
3784 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Entity_Id);
3785 -- Analyzes and resolves expression Expr against the Etype of the
3786 -- Component. This routine also applies all appropriate checks to Expr.
3787 -- It finally saves a Expr in the newly created association list that
3788 -- will be attached to the final record aggregate. Note that if the
3789 -- Parent pointer of Expr is not set then Expr was produced with a
3790 -- New_Copy_Tree or some such.
3791
3792 procedure Rewrite_Range (Root_Type : Entity_Id; Rge : Node_Id);
3793 -- Rewrite a range node Rge when its bounds refer to non-stored
3794 -- discriminants from Root_Type, to replace them with the stored
3795 -- discriminant values. This is required in GNATprove mode, and is
3796 -- adopted in all modes to avoid special-casing GNATprove mode.
3797
3798 ---------------------
3799 -- Add_Association --
3800 ---------------------
3801
3802 procedure Add_Association
3803 (Component : Entity_Id;
3804 Expr : Node_Id;
3805 Assoc_List : List_Id;
3806 Is_Box_Present : Boolean := False)
3807 is
3808 Choice_List : constant List_Id := New_List;
3809 Loc : Source_Ptr;
3810
3811 begin
3812 -- If this is a box association the expression is missing, so use the
3813 -- Sloc of the aggregate itself for the new association.
3814
3815 pragma Assert (Present (Expr) xor Is_Box_Present);
3816
3817 if Present (Expr) then
3818 Loc := Sloc (Expr);
3819 else
3820 Loc := Sloc (N);
3821 end if;
3822
3823 Append_To (Choice_List, New_Occurrence_Of (Component, Loc));
3824
3825 Append_To (Assoc_List,
3826 Make_Component_Association (Loc,
3827 Choices => Choice_List,
3828 Expression => Expr,
3829 Box_Present => Is_Box_Present));
3830 end Add_Association;
3831
3832 -----------------------------
3833 -- Add_Discriminant_Values --
3834 -----------------------------
3835
3836 procedure Add_Discriminant_Values
3837 (New_Aggr : Node_Id;
3838 Assoc_List : List_Id)
3839 is
3840 Assoc : Node_Id;
3841 Discr : Entity_Id;
3842 Discr_Elmt : Elmt_Id;
3843 Discr_Val : Node_Id;
3844 Val : Entity_Id;
3845
3846 begin
3847 Discr := First_Discriminant (Etype (New_Aggr));
3848 Discr_Elmt := First_Elmt (Discriminant_Constraint (Etype (New_Aggr)));
3849 while Present (Discr_Elmt) loop
3850 Discr_Val := Node (Discr_Elmt);
3851
3852 -- If the constraint is given by a discriminant then it is a
3853 -- discriminant of an enclosing record, and its value has already
3854 -- been placed in the association list.
3855
3856 if Is_Entity_Name (Discr_Val)
3857 and then Ekind (Entity (Discr_Val)) = E_Discriminant
3858 then
3859 Val := Entity (Discr_Val);
3860
3861 Assoc := First (Assoc_List);
3862 while Present (Assoc) loop
3863 if Present (Entity (First (Choices (Assoc))))
3864 and then Entity (First (Choices (Assoc))) = Val
3865 then
3866 Discr_Val := Expression (Assoc);
3867 exit;
3868 end if;
3869
3870 Next (Assoc);
3871 end loop;
3872 end if;
3873
3874 Add_Association
3875 (Discr, New_Copy_Tree (Discr_Val),
3876 Component_Associations (New_Aggr));
3877
3878 -- If the discriminant constraint is a current instance, mark the
3879 -- current aggregate so that the self-reference can be expanded
3880 -- later. The constraint may refer to the subtype of aggregate, so
3881 -- use base type for comparison.
3882
3883 if Nkind (Discr_Val) = N_Attribute_Reference
3884 and then Is_Entity_Name (Prefix (Discr_Val))
3885 and then Is_Type (Entity (Prefix (Discr_Val)))
3886 and then Base_Type (Etype (N)) = Entity (Prefix (Discr_Val))
3887 then
3888 Set_Has_Self_Reference (N);
3889 end if;
3890
3891 Next_Elmt (Discr_Elmt);
3892 Next_Discriminant (Discr);
3893 end loop;
3894 end Add_Discriminant_Values;
3895
3896 --------------------------
3897 -- Discriminant_Present --
3898 --------------------------
3899
3900 function Discriminant_Present (Input_Discr : Entity_Id) return Boolean is
3901 Regular_Aggr : constant Boolean := Nkind (N) /= N_Extension_Aggregate;
3902
3903 Ancestor_Is_Subtyp : Boolean;
3904
3905 Loc : Source_Ptr;
3906
3907 Ancestor : Node_Id;
3908 Ancestor_Typ : Entity_Id;
3909 Comp_Assoc : Node_Id;
3910 Discr : Entity_Id;
3911 Discr_Expr : Node_Id;
3912 Discr_Val : Elmt_Id := No_Elmt;
3913 Orig_Discr : Entity_Id;
3914
3915 begin
3916 if Regular_Aggr then
3917 return True;
3918 end if;
3919
3920 -- Check whether inherited discriminant values have already been
3921 -- inserted in the aggregate. This will be the case if we are
3922 -- re-analyzing an aggregate whose expansion was delayed.
3923
3924 if Present (Component_Associations (N)) then
3925 Comp_Assoc := First (Component_Associations (N));
3926 while Present (Comp_Assoc) loop
3927 if Inherited_Discriminant (Comp_Assoc) then
3928 return True;
3929 end if;
3930
3931 Next (Comp_Assoc);
3932 end loop;
3933 end if;
3934
3935 Ancestor := Ancestor_Part (N);
3936 Ancestor_Typ := Etype (Ancestor);
3937 Loc := Sloc (Ancestor);
3938
3939 -- For a private type with unknown discriminants, use the underlying
3940 -- record view if it is available.
3941
3942 if Has_Unknown_Discriminants (Ancestor_Typ)
3943 and then Present (Full_View (Ancestor_Typ))
3944 and then Present (Underlying_Record_View (Full_View (Ancestor_Typ)))
3945 then
3946 Ancestor_Typ := Underlying_Record_View (Full_View (Ancestor_Typ));
3947 end if;
3948
3949 Ancestor_Is_Subtyp :=
3950 Is_Entity_Name (Ancestor) and then Is_Type (Entity (Ancestor));
3951
3952 -- If the ancestor part has no discriminants clearly N's aggregate
3953 -- part must provide a value for Discr.
3954
3955 if not Has_Discriminants (Ancestor_Typ) then
3956 return True;
3957
3958 -- If the ancestor part is an unconstrained subtype mark then the
3959 -- Discr must be present in N's aggregate part.
3960
3961 elsif Ancestor_Is_Subtyp
3962 and then not Is_Constrained (Entity (Ancestor))
3963 then
3964 return True;
3965 end if;
3966
3967 -- Now look to see if Discr was specified in the ancestor part
3968
3969 if Ancestor_Is_Subtyp then
3970 Discr_Val :=
3971 First_Elmt (Discriminant_Constraint (Entity (Ancestor)));
3972 end if;
3973
3974 Orig_Discr := Original_Record_Component (Input_Discr);
3975
3976 Discr := First_Discriminant (Ancestor_Typ);
3977 while Present (Discr) loop
3978
3979 -- If Ancestor has already specified Disc value then insert its
3980 -- value in the final aggregate.
3981
3982 if Original_Record_Component (Discr) = Orig_Discr then
3983 if Ancestor_Is_Subtyp then
3984 Discr_Expr := New_Copy_Tree (Node (Discr_Val));
3985 else
3986 Discr_Expr :=
3987 Make_Selected_Component (Loc,
3988 Prefix => Duplicate_Subexpr (Ancestor),
3989 Selector_Name => New_Occurrence_Of (Input_Discr, Loc));
3990 end if;
3991
3992 Resolve_Aggr_Expr (Discr_Expr, Input_Discr);
3993 Set_Inherited_Discriminant (Last (New_Assoc_List));
3994 return False;
3995 end if;
3996
3997 Next_Discriminant (Discr);
3998
3999 if Ancestor_Is_Subtyp then
4000 Next_Elmt (Discr_Val);
4001 end if;
4002 end loop;
4003
4004 return True;
4005 end Discriminant_Present;
4006
4007 ---------------------------
4008 -- Find_Private_Ancestor --
4009 ---------------------------
4010
4011 function Find_Private_Ancestor (Typ : Entity_Id) return Entity_Id is
4012 Par : Entity_Id;
4013
4014 begin
4015 Par := Typ;
4016 loop
4017 if Has_Private_Ancestor (Par)
4018 and then not Has_Private_Ancestor (Etype (Base_Type (Par)))
4019 then
4020 return Par;
4021
4022 elsif not Is_Derived_Type (Par) then
4023 return Empty;
4024
4025 else
4026 Par := Etype (Base_Type (Par));
4027 end if;
4028 end loop;
4029 end Find_Private_Ancestor;
4030
4031 ---------------
4032 -- Get_Value --
4033 ---------------
4034
4035 function Get_Value
4036 (Compon : Entity_Id;
4037 From : List_Id;
4038 Consider_Others_Choice : Boolean := False) return Node_Id
4039 is
4040 Typ : constant Entity_Id := Etype (Compon);
4041 Assoc : Node_Id;
4042 Expr : Node_Id := Empty;
4043 Selector_Name : Node_Id;
4044
4045 begin
4046 Is_Box_Present := False;
4047
4048 if No (From) then
4049 return Empty;
4050 end if;
4051
4052 Assoc := First (From);
4053 while Present (Assoc) loop
4054 Selector_Name := First (Choices (Assoc));
4055 while Present (Selector_Name) loop
4056 if Nkind (Selector_Name) = N_Others_Choice then
4057 if Consider_Others_Choice and then No (Expr) then
4058
4059 -- We need to duplicate the expression for each
4060 -- successive component covered by the others choice.
4061 -- This is redundant if the others_choice covers only
4062 -- one component (small optimization possible???), but
4063 -- indispensable otherwise, because each one must be
4064 -- expanded individually to preserve side effects.
4065
4066 -- Ada 2005 (AI-287): In case of default initialization
4067 -- of components, we duplicate the corresponding default
4068 -- expression (from the record type declaration). The
4069 -- copy must carry the sloc of the association (not the
4070 -- original expression) to prevent spurious elaboration
4071 -- checks when the default includes function calls.
4072
4073 if Box_Present (Assoc) then
4074 Others_Box := Others_Box + 1;
4075 Is_Box_Present := True;
4076
4077 if Expander_Active then
4078 return
4079 New_Copy_Tree_And_Copy_Dimensions
4080 (Expression (Parent (Compon)),
4081 New_Sloc => Sloc (Assoc));
4082 else
4083 return Expression (Parent (Compon));
4084 end if;
4085
4086 else
4087 if Present (Others_Etype)
4088 and then Base_Type (Others_Etype) /= Base_Type (Typ)
4089 then
4090 -- If the components are of an anonymous access
4091 -- type they are distinct, but this is legal in
4092 -- Ada 2012 as long as designated types match.
4093
4094 if (Ekind (Typ) = E_Anonymous_Access_Type
4095 or else Ekind (Typ) =
4096 E_Anonymous_Access_Subprogram_Type)
4097 and then Designated_Type (Typ) =
4098 Designated_Type (Others_Etype)
4099 then
4100 null;
4101 else
4102 Error_Msg_N
4103 ("components in OTHERS choice must have same "
4104 & "type", Selector_Name);
4105 end if;
4106 end if;
4107
4108 Others_Etype := Typ;
4109
4110 -- Copy the expression so that it is resolved
4111 -- independently for each component, This is needed
4112 -- for accessibility checks on components of anonymous
4113 -- access types, even in compile_only mode.
4114
4115 if not Inside_A_Generic then
4116 return
4117 New_Copy_Tree_And_Copy_Dimensions
4118 (Expression (Assoc));
4119 else
4120 return Expression (Assoc);
4121 end if;
4122 end if;
4123 end if;
4124
4125 elsif Chars (Compon) = Chars (Selector_Name) then
4126 if No (Expr) then
4127
4128 -- Ada 2005 (AI-231)
4129
4130 if Ada_Version >= Ada_2005
4131 and then Known_Null (Expression (Assoc))
4132 then
4133 Check_Can_Never_Be_Null (Compon, Expression (Assoc));
4134 end if;
4135
4136 -- We need to duplicate the expression when several
4137 -- components are grouped together with a "|" choice.
4138 -- For instance "filed1 | filed2 => Expr"
4139
4140 -- Ada 2005 (AI-287)
4141
4142 if Box_Present (Assoc) then
4143 Is_Box_Present := True;
4144
4145 -- Duplicate the default expression of the component
4146 -- from the record type declaration, so a new copy
4147 -- can be attached to the association.
4148
4149 -- Note that we always copy the default expression,
4150 -- even when the association has a single choice, in
4151 -- order to create a proper association for the
4152 -- expanded aggregate.
4153
4154 -- Component may have no default, in which case the
4155 -- expression is empty and the component is default-
4156 -- initialized, but an association for the component
4157 -- exists, and it is not covered by an others clause.
4158
4159 -- Scalar and private types have no initialization
4160 -- procedure, so they remain uninitialized. If the
4161 -- target of the aggregate is a constant this
4162 -- deserves a warning.
4163
4164 if No (Expression (Parent (Compon)))
4165 and then not Has_Non_Null_Base_Init_Proc (Typ)
4166 and then not Has_Aspect (Typ, Aspect_Default_Value)
4167 and then not Is_Concurrent_Type (Typ)
4168 and then Nkind (Parent (N)) = N_Object_Declaration
4169 and then Constant_Present (Parent (N))
4170 then
4171 Error_Msg_Node_2 := Typ;
4172 Error_Msg_NE
4173 ("component&? of type& is uninitialized",
4174 Assoc, Selector_Name);
4175
4176 -- An additional reminder if the component type
4177 -- is a generic formal.
4178
4179 if Is_Generic_Type (Base_Type (Typ)) then
4180 Error_Msg_NE
4181 ("\instance should provide actual type with "
4182 & "initialization for&", Assoc, Typ);
4183 end if;
4184 end if;
4185
4186 return
4187 New_Copy_Tree_And_Copy_Dimensions
4188 (Expression (Parent (Compon)));
4189
4190 else
4191 if Present (Next (Selector_Name)) then
4192 Expr := New_Copy_Tree_And_Copy_Dimensions
4193 (Expression (Assoc));
4194 else
4195 Expr := Expression (Assoc);
4196 end if;
4197 end if;
4198
4199 Generate_Reference (Compon, Selector_Name, 'm');
4200
4201 else
4202 Error_Msg_NE
4203 ("more than one value supplied for &",
4204 Selector_Name, Compon);
4205
4206 end if;
4207 end if;
4208
4209 Next (Selector_Name);
4210 end loop;
4211
4212 Next (Assoc);
4213 end loop;
4214
4215 return Expr;
4216 end Get_Value;
4217
4218 -----------------------------
4219 -- Propagate_Discriminants --
4220 -----------------------------
4221
4222 procedure Propagate_Discriminants
4223 (Aggr : Node_Id;
4224 Assoc_List : List_Id)
4225 is
4226 Loc : constant Source_Ptr := Sloc (N);
4227
4228 procedure Process_Component (Comp : Entity_Id);
4229 -- Add one component with a box association to the inner aggregate,
4230 -- and recurse if component is itself composite.
4231
4232 -----------------------
4233 -- Process_Component --
4234 -----------------------
4235
4236 procedure Process_Component (Comp : Entity_Id) is
4237 T : constant Entity_Id := Etype (Comp);
4238 New_Aggr : Node_Id;
4239
4240 begin
4241 if Is_Record_Type (T) and then Has_Discriminants (T) then
4242 New_Aggr := Make_Aggregate (Loc, No_List, New_List);
4243 Set_Etype (New_Aggr, T);
4244
4245 Add_Association
4246 (Comp, New_Aggr, Component_Associations (Aggr));
4247
4248 -- Collect discriminant values and recurse
4249
4250 Add_Discriminant_Values (New_Aggr, Assoc_List);
4251 Propagate_Discriminants (New_Aggr, Assoc_List);
4252
4253 Build_Constrained_Itype
4254 (New_Aggr, T, Component_Associations (New_Aggr));
4255 else
4256 Add_Association
4257 (Comp, Empty, Component_Associations (Aggr),
4258 Is_Box_Present => True);
4259 end if;
4260 end Process_Component;
4261
4262 -- Local variables
4263
4264 Aggr_Type : constant Entity_Id := Base_Type (Etype (Aggr));
4265 Components : constant Elist_Id := New_Elmt_List;
4266 Def_Node : constant Node_Id :=
4267 Type_Definition (Declaration_Node (Aggr_Type));
4268
4269 Comp : Node_Id;
4270 Comp_Elmt : Elmt_Id;
4271 Errors : Boolean;
4272
4273 -- Start of processing for Propagate_Discriminants
4274
4275 begin
4276 -- The component type may be a variant type. Collect the components
4277 -- that are ruled by the known values of the discriminants. Their
4278 -- values have already been inserted into the component list of the
4279 -- current aggregate.
4280
4281 if Nkind (Def_Node) = N_Record_Definition
4282 and then Present (Component_List (Def_Node))
4283 and then Present (Variant_Part (Component_List (Def_Node)))
4284 then
4285 Gather_Components (Aggr_Type,
4286 Component_List (Def_Node),
4287 Governed_By => Component_Associations (Aggr),
4288 Into => Components,
4289 Report_Errors => Errors);
4290
4291 Comp_Elmt := First_Elmt (Components);
4292 while Present (Comp_Elmt) loop
4293 if Ekind (Node (Comp_Elmt)) /= E_Discriminant then
4294 Process_Component (Node (Comp_Elmt));
4295 end if;
4296
4297 Next_Elmt (Comp_Elmt);
4298 end loop;
4299
4300 -- No variant part, iterate over all components
4301
4302 else
4303 Comp := First_Component (Etype (Aggr));
4304 while Present (Comp) loop
4305 Process_Component (Comp);
4306 Next_Component (Comp);
4307 end loop;
4308 end if;
4309 end Propagate_Discriminants;
4310
4311 -----------------------
4312 -- Resolve_Aggr_Expr --
4313 -----------------------
4314
4315 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Entity_Id) is
4316 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean;
4317 -- If the expression is an aggregate (possibly qualified) then its
4318 -- expansion is delayed until the enclosing aggregate is expanded
4319 -- into assignments. In that case, do not generate checks on the
4320 -- expression, because they will be generated later, and will other-
4321 -- wise force a copy (to remove side effects) that would leave a
4322 -- dynamic-sized aggregate in the code, something that gigi cannot
4323 -- handle.
4324
4325 ---------------------------
4326 -- Has_Expansion_Delayed --
4327 ---------------------------
4328
4329 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean is
4330 begin
4331 return
4332 (Nkind (Expr) in N_Aggregate | N_Extension_Aggregate
4333 and then Present (Etype (Expr))
4334 and then Is_Record_Type (Etype (Expr))
4335 and then Expansion_Delayed (Expr))
4336 or else
4337 (Nkind (Expr) = N_Qualified_Expression
4338 and then Has_Expansion_Delayed (Expression (Expr)));
4339 end Has_Expansion_Delayed;
4340
4341 -- Local variables
4342
4343 Expr_Type : Entity_Id := Empty;
4344 New_C : Entity_Id := Component;
4345 New_Expr : Node_Id;
4346
4347 Relocate : Boolean;
4348 -- Set to True if the resolved Expr node needs to be relocated when
4349 -- attached to the newly created association list. This node need not
4350 -- be relocated if its parent pointer is not set. In fact in this
4351 -- case Expr is the output of a New_Copy_Tree call. If Relocate is
4352 -- True then we have analyzed the expression node in the original
4353 -- aggregate and hence it needs to be relocated when moved over to
4354 -- the new association list.
4355
4356 -- Start of processing for Resolve_Aggr_Expr
4357
4358 begin
4359 -- If the type of the component is elementary or the type of the
4360 -- aggregate does not contain discriminants, use the type of the
4361 -- component to resolve Expr.
4362
4363 if Is_Elementary_Type (Etype (Component))
4364 or else not Has_Discriminants (Etype (N))
4365 then
4366 Expr_Type := Etype (Component);
4367
4368 -- Otherwise we have to pick up the new type of the component from
4369 -- the new constrained subtype of the aggregate. In fact components
4370 -- which are of a composite type might be constrained by a
4371 -- discriminant, and we want to resolve Expr against the subtype were
4372 -- all discriminant occurrences are replaced with their actual value.
4373
4374 else
4375 New_C := First_Component (Etype (N));
4376 while Present (New_C) loop
4377 if Chars (New_C) = Chars (Component) then
4378 Expr_Type := Etype (New_C);
4379 exit;
4380 end if;
4381
4382 Next_Component (New_C);
4383 end loop;
4384
4385 pragma Assert (Present (Expr_Type));
4386
4387 -- For each range in an array type where a discriminant has been
4388 -- replaced with the constraint, check that this range is within
4389 -- the range of the base type. This checks is done in the init
4390 -- proc for regular objects, but has to be done here for
4391 -- aggregates since no init proc is called for them.
4392
4393 if Is_Array_Type (Expr_Type) then
4394 declare
4395 Index : Node_Id;
4396 -- Range of the current constrained index in the array
4397
4398 Orig_Index : Node_Id := First_Index (Etype (Component));
4399 -- Range corresponding to the range Index above in the
4400 -- original unconstrained record type. The bounds of this
4401 -- range may be governed by discriminants.
4402
4403 Unconstr_Index : Node_Id := First_Index (Etype (Expr_Type));
4404 -- Range corresponding to the range Index above for the
4405 -- unconstrained array type. This range is needed to apply
4406 -- range checks.
4407
4408 begin
4409 Index := First_Index (Expr_Type);
4410 while Present (Index) loop
4411 if Depends_On_Discriminant (Orig_Index) then
4412 Apply_Range_Check (Index, Etype (Unconstr_Index));
4413 end if;
4414
4415 Next_Index (Index);
4416 Next_Index (Orig_Index);
4417 Next_Index (Unconstr_Index);
4418 end loop;
4419 end;
4420 end if;
4421 end if;
4422
4423 -- If the Parent pointer of Expr is not set, Expr is an expression
4424 -- duplicated by New_Tree_Copy (this happens for record aggregates
4425 -- that look like (Field1 | Filed2 => Expr) or (others => Expr)).
4426 -- Such a duplicated expression must be attached to the tree
4427 -- before analysis and resolution to enforce the rule that a tree
4428 -- fragment should never be analyzed or resolved unless it is
4429 -- attached to the current compilation unit.
4430
4431 if No (Parent (Expr)) then
4432 Set_Parent (Expr, N);
4433 Relocate := False;
4434 else
4435 Relocate := True;
4436 end if;
4437
4438 Analyze_And_Resolve (Expr, Expr_Type);
4439 Check_Expr_OK_In_Limited_Aggregate (Expr);
4440 Check_Non_Static_Context (Expr);
4441 Check_Unset_Reference (Expr);
4442
4443 -- Check wrong use of class-wide types
4444
4445 if Is_Class_Wide_Type (Etype (Expr)) then
4446 Error_Msg_N ("dynamically tagged expression not allowed", Expr);
4447 end if;
4448
4449 if not Has_Expansion_Delayed (Expr) then
4450 Aggregate_Constraint_Checks (Expr, Expr_Type);
4451 end if;
4452
4453 -- If an aggregate component has a type with predicates, an explicit
4454 -- predicate check must be applied, as for an assignment statement,
4455 -- because the aggregate might not be expanded into individual
4456 -- component assignments.
4457
4458 if Has_Predicates (Expr_Type)
4459 and then Analyzed (Expr)
4460 then
4461 Apply_Predicate_Check (Expr, Expr_Type);
4462 end if;
4463
4464 if Raises_Constraint_Error (Expr) then
4465 Set_Raises_Constraint_Error (N);
4466 end if;
4467
4468 -- If the expression has been marked as requiring a range check, then
4469 -- generate it here. It's a bit odd to be generating such checks in
4470 -- the analyzer, but harmless since Generate_Range_Check does nothing
4471 -- (other than making sure Do_Range_Check is set) if the expander is
4472 -- not active.
4473
4474 if Do_Range_Check (Expr) then
4475 Generate_Range_Check (Expr, Expr_Type, CE_Range_Check_Failed);
4476 end if;
4477
4478 -- Add association Component => Expr if the caller requests it
4479
4480 if Relocate then
4481 New_Expr := Relocate_Node (Expr);
4482
4483 -- Since New_Expr is not gonna be analyzed later on, we need to
4484 -- propagate here the dimensions form Expr to New_Expr.
4485
4486 Copy_Dimensions (Expr, New_Expr);
4487
4488 else
4489 New_Expr := Expr;
4490 end if;
4491
4492 Add_Association (New_C, New_Expr, New_Assoc_List);
4493 end Resolve_Aggr_Expr;
4494
4495 -------------------
4496 -- Rewrite_Range --
4497 -------------------
4498
4499 procedure Rewrite_Range (Root_Type : Entity_Id; Rge : Node_Id) is
4500 procedure Rewrite_Bound
4501 (Bound : Node_Id;
4502 Disc : Entity_Id;
4503 Expr_Disc : Node_Id);
4504 -- Rewrite a bound of the range Bound, when it is equal to the
4505 -- non-stored discriminant Disc, into the stored discriminant
4506 -- value Expr_Disc.
4507
4508 -------------------
4509 -- Rewrite_Bound --
4510 -------------------
4511
4512 procedure Rewrite_Bound
4513 (Bound : Node_Id;
4514 Disc : Entity_Id;
4515 Expr_Disc : Node_Id)
4516 is
4517 begin
4518 if Nkind (Bound) /= N_Identifier then
4519 return;
4520 end if;
4521
4522 -- We expect either the discriminant or the discriminal
4523
4524 if Entity (Bound) = Disc
4525 or else (Ekind (Entity (Bound)) = E_In_Parameter
4526 and then Discriminal_Link (Entity (Bound)) = Disc)
4527 then
4528 Rewrite (Bound, New_Copy_Tree (Expr_Disc));
4529 end if;
4530 end Rewrite_Bound;
4531
4532 -- Local variables
4533
4534 Low, High : Node_Id;
4535 Disc : Entity_Id;
4536 Expr_Disc : Elmt_Id;
4537
4538 -- Start of processing for Rewrite_Range
4539
4540 begin
4541 if Has_Discriminants (Root_Type) and then Nkind (Rge) = N_Range then
4542 Low := Low_Bound (Rge);
4543 High := High_Bound (Rge);
4544
4545 Disc := First_Discriminant (Root_Type);
4546 Expr_Disc := First_Elmt (Stored_Constraint (Etype (N)));
4547 while Present (Disc) loop
4548 Rewrite_Bound (Low, Disc, Node (Expr_Disc));
4549 Rewrite_Bound (High, Disc, Node (Expr_Disc));
4550 Next_Discriminant (Disc);
4551 Next_Elmt (Expr_Disc);
4552 end loop;
4553 end if;
4554 end Rewrite_Range;
4555
4556 -- Local variables
4557
4558 Components : constant Elist_Id := New_Elmt_List;
4559 -- Components is the list of the record components whose value must be
4560 -- provided in the aggregate. This list does include discriminants.
4561
4562 Component : Entity_Id;
4563 Component_Elmt : Elmt_Id;
4564 Expr : Node_Id;
4565 Positional_Expr : Node_Id;
4566
4567 -- Start of processing for Resolve_Record_Aggregate
4568
4569 begin
4570 -- A record aggregate is restricted in SPARK:
4571
4572 -- Each named association can have only a single choice.
4573 -- OTHERS cannot be used.
4574 -- Positional and named associations cannot be mixed.
4575
4576 if Present (Component_Associations (N))
4577 and then Present (First (Component_Associations (N)))
4578 then
4579 declare
4580 Assoc : Node_Id;
4581
4582 begin
4583 Assoc := First (Component_Associations (N));
4584 while Present (Assoc) loop
4585 if Nkind (Assoc) = N_Iterated_Component_Association then
4586 Error_Msg_N
4587 ("iterated component association can only appear in an "
4588 & "array aggregate", N);
4589 raise Unrecoverable_Error;
4590 end if;
4591
4592 Next (Assoc);
4593 end loop;
4594 end;
4595 end if;
4596
4597 -- We may end up calling Duplicate_Subexpr on expressions that are
4598 -- attached to New_Assoc_List. For this reason we need to attach it
4599 -- to the tree by setting its parent pointer to N. This parent point
4600 -- will change in STEP 8 below.
4601
4602 Set_Parent (New_Assoc_List, N);
4603
4604 -- STEP 1: abstract type and null record verification
4605
4606 if Is_Abstract_Type (Typ) then
4607 Error_Msg_N ("type of aggregate cannot be abstract", N);
4608 end if;
4609
4610 if No (First_Entity (Typ)) and then Null_Record_Present (N) then
4611 Set_Etype (N, Typ);
4612 return;
4613
4614 elsif Present (First_Entity (Typ))
4615 and then Null_Record_Present (N)
4616 and then not Is_Tagged_Type (Typ)
4617 then
4618 Error_Msg_N ("record aggregate cannot be null", N);
4619 return;
4620
4621 -- If the type has no components, then the aggregate should either
4622 -- have "null record", or in Ada 2005 it could instead have a single
4623 -- component association given by "others => <>". For Ada 95 we flag an
4624 -- error at this point, but for Ada 2005 we proceed with checking the
4625 -- associations below, which will catch the case where it's not an
4626 -- aggregate with "others => <>". Note that the legality of a <>
4627 -- aggregate for a null record type was established by AI05-016.
4628
4629 elsif No (First_Entity (Typ))
4630 and then Ada_Version < Ada_2005
4631 then
4632 Error_Msg_N ("record aggregate must be null", N);
4633 return;
4634 end if;
4635
4636 -- STEP 2: Verify aggregate structure
4637
4638 Step_2 : declare
4639 Assoc : Node_Id;
4640 Bad_Aggregate : Boolean := False;
4641 Selector_Name : Node_Id;
4642
4643 begin
4644 if Present (Component_Associations (N)) then
4645 Assoc := First (Component_Associations (N));
4646 else
4647 Assoc := Empty;
4648 end if;
4649
4650 while Present (Assoc) loop
4651 Selector_Name := First (Choices (Assoc));
4652 while Present (Selector_Name) loop
4653 if Nkind (Selector_Name) = N_Identifier then
4654 null;
4655
4656 elsif Nkind (Selector_Name) = N_Others_Choice then
4657 if Selector_Name /= First (Choices (Assoc))
4658 or else Present (Next (Selector_Name))
4659 then
4660 Error_Msg_N
4661 ("OTHERS must appear alone in a choice list",
4662 Selector_Name);
4663 return;
4664
4665 elsif Present (Next (Assoc)) then
4666 Error_Msg_N
4667 ("OTHERS must appear last in an aggregate",
4668 Selector_Name);
4669 return;
4670
4671 -- (Ada 2005): If this is an association with a box,
4672 -- indicate that the association need not represent
4673 -- any component.
4674
4675 elsif Box_Present (Assoc) then
4676 Others_Box := 1;
4677 Box_Node := Assoc;
4678 end if;
4679
4680 else
4681 Error_Msg_N
4682 ("selector name should be identifier or OTHERS",
4683 Selector_Name);
4684 Bad_Aggregate := True;
4685 end if;
4686
4687 Next (Selector_Name);
4688 end loop;
4689
4690 Next (Assoc);
4691 end loop;
4692
4693 if Bad_Aggregate then
4694 return;
4695 end if;
4696 end Step_2;
4697
4698 -- STEP 3: Find discriminant Values
4699
4700 Step_3 : declare
4701 Discrim : Entity_Id;
4702 Missing_Discriminants : Boolean := False;
4703
4704 begin
4705 if Present (Expressions (N)) then
4706 Positional_Expr := First (Expressions (N));
4707 else
4708 Positional_Expr := Empty;
4709 end if;
4710
4711 -- AI05-0115: if the ancestor part is a subtype mark, the ancestor
4712 -- must not have unknown discriminants.
4713 -- ??? We are not checking any subtype mark here and this code is not
4714 -- exercised by any test, so it's likely wrong (in particular
4715 -- we should not use Root_Type here but the subtype mark, if any),
4716 -- and possibly not needed.
4717
4718 if Is_Derived_Type (Typ)
4719 and then Has_Unknown_Discriminants (Root_Type (Typ))
4720 and then Nkind (N) /= N_Extension_Aggregate
4721 then
4722 Error_Msg_NE
4723 ("aggregate not available for type& whose ancestor "
4724 & "has unknown discriminants ", N, Typ);
4725 end if;
4726
4727 if Has_Unknown_Discriminants (Typ)
4728 and then Present (Underlying_Record_View (Typ))
4729 then
4730 Discrim := First_Discriminant (Underlying_Record_View (Typ));
4731 elsif Has_Discriminants (Typ) then
4732 Discrim := First_Discriminant (Typ);
4733 else
4734 Discrim := Empty;
4735 end if;
4736
4737 -- First find the discriminant values in the positional components
4738
4739 while Present (Discrim) and then Present (Positional_Expr) loop
4740 if Discriminant_Present (Discrim) then
4741 Resolve_Aggr_Expr (Positional_Expr, Discrim);
4742
4743 -- Ada 2005 (AI-231)
4744
4745 if Ada_Version >= Ada_2005
4746 and then Known_Null (Positional_Expr)
4747 then
4748 Check_Can_Never_Be_Null (Discrim, Positional_Expr);
4749 end if;
4750
4751 Next (Positional_Expr);
4752 end if;
4753
4754 if Present (Get_Value (Discrim, Component_Associations (N))) then
4755 Error_Msg_NE
4756 ("more than one value supplied for discriminant&",
4757 N, Discrim);
4758 end if;
4759
4760 Next_Discriminant (Discrim);
4761 end loop;
4762
4763 -- Find remaining discriminant values if any among named components
4764
4765 while Present (Discrim) loop
4766 Expr := Get_Value (Discrim, Component_Associations (N), True);
4767
4768 if not Discriminant_Present (Discrim) then
4769 if Present (Expr) then
4770 Error_Msg_NE
4771 ("more than one value supplied for discriminant &",
4772 N, Discrim);
4773 end if;
4774
4775 elsif No (Expr) then
4776 Error_Msg_NE
4777 ("no value supplied for discriminant &", N, Discrim);
4778 Missing_Discriminants := True;
4779
4780 else
4781 Resolve_Aggr_Expr (Expr, Discrim);
4782 end if;
4783
4784 Next_Discriminant (Discrim);
4785 end loop;
4786
4787 if Missing_Discriminants then
4788 return;
4789 end if;
4790
4791 -- At this point and until the beginning of STEP 6, New_Assoc_List
4792 -- contains only the discriminants and their values.
4793
4794 end Step_3;
4795
4796 -- STEP 4: Set the Etype of the record aggregate
4797
4798 if Has_Discriminants (Typ)
4799 or else (Has_Unknown_Discriminants (Typ)
4800 and then Present (Underlying_Record_View (Typ)))
4801 then
4802 Build_Constrained_Itype (N, Typ, New_Assoc_List);
4803 else
4804 Set_Etype (N, Typ);
4805 end if;
4806
4807 -- STEP 5: Get remaining components according to discriminant values
4808
4809 Step_5 : declare
4810 Dnode : Node_Id;
4811 Errors_Found : Boolean := False;
4812 Record_Def : Node_Id;
4813 Parent_Typ : Entity_Id;
4814 Parent_Typ_List : Elist_Id;
4815 Parent_Elmt : Elmt_Id;
4816 Root_Typ : Entity_Id;
4817
4818 begin
4819 if Is_Derived_Type (Typ) and then Is_Tagged_Type (Typ) then
4820 Parent_Typ_List := New_Elmt_List;
4821
4822 -- If this is an extension aggregate, the component list must
4823 -- include all components that are not in the given ancestor type.
4824 -- Otherwise, the component list must include components of all
4825 -- ancestors, starting with the root.
4826
4827 if Nkind (N) = N_Extension_Aggregate then
4828 Root_Typ := Base_Type (Etype (Ancestor_Part (N)));
4829
4830 else
4831 -- AI05-0115: check legality of aggregate for type with a
4832 -- private ancestor.
4833
4834 Root_Typ := Root_Type (Typ);
4835 if Has_Private_Ancestor (Typ) then
4836 declare
4837 Ancestor : constant Entity_Id :=
4838 Find_Private_Ancestor (Typ);
4839 Ancestor_Unit : constant Entity_Id :=
4840 Cunit_Entity
4841 (Get_Source_Unit (Ancestor));
4842 Parent_Unit : constant Entity_Id :=
4843 Cunit_Entity (Get_Source_Unit
4844 (Base_Type (Etype (Ancestor))));
4845 begin
4846 -- Check whether we are in a scope that has full view
4847 -- over the private ancestor and its parent. This can
4848 -- only happen if the derivation takes place in a child
4849 -- unit of the unit that declares the parent, and we are
4850 -- in the private part or body of that child unit, else
4851 -- the aggregate is illegal.
4852
4853 if Is_Child_Unit (Ancestor_Unit)
4854 and then Scope (Ancestor_Unit) = Parent_Unit
4855 and then In_Open_Scopes (Scope (Ancestor))
4856 and then
4857 (In_Private_Part (Scope (Ancestor))
4858 or else In_Package_Body (Scope (Ancestor)))
4859 then
4860 null;
4861
4862 else
4863 Error_Msg_NE
4864 ("type of aggregate has private ancestor&!",
4865 N, Root_Typ);
4866 Error_Msg_N ("must use extension aggregate!", N);
4867 return;
4868 end if;
4869 end;
4870 end if;
4871
4872 Dnode := Declaration_Node (Base_Type (Root_Typ));
4873
4874 -- If we don't get a full declaration, then we have some error
4875 -- which will get signalled later so skip this part. Otherwise
4876 -- gather components of root that apply to the aggregate type.
4877 -- We use the base type in case there is an applicable stored
4878 -- constraint that renames the discriminants of the root.
4879
4880 if Nkind (Dnode) = N_Full_Type_Declaration then
4881 Record_Def := Type_Definition (Dnode);
4882 Gather_Components
4883 (Base_Type (Typ),
4884 Component_List (Record_Def),
4885 Governed_By => New_Assoc_List,
4886 Into => Components,
4887 Report_Errors => Errors_Found);
4888
4889 if Errors_Found then
4890 Error_Msg_N
4891 ("discriminant controlling variant part is not static",
4892 N);
4893 return;
4894 end if;
4895 end if;
4896 end if;
4897
4898 Parent_Typ := Base_Type (Typ);
4899 while Parent_Typ /= Root_Typ loop
4900 Prepend_Elmt (Parent_Typ, To => Parent_Typ_List);
4901 Parent_Typ := Etype (Parent_Typ);
4902
4903 if Nkind (Parent (Base_Type (Parent_Typ))) =
4904 N_Private_Type_Declaration
4905 or else Nkind (Parent (Base_Type (Parent_Typ))) =
4906 N_Private_Extension_Declaration
4907 then
4908 if Nkind (N) /= N_Extension_Aggregate then
4909 Error_Msg_NE
4910 ("type of aggregate has private ancestor&!",
4911 N, Parent_Typ);
4912 Error_Msg_N ("must use extension aggregate!", N);
4913 return;
4914
4915 elsif Parent_Typ /= Root_Typ then
4916 Error_Msg_NE
4917 ("ancestor part of aggregate must be private type&",
4918 Ancestor_Part (N), Parent_Typ);
4919 return;
4920 end if;
4921
4922 -- The current view of ancestor part may be a private type,
4923 -- while the context type is always non-private.
4924
4925 elsif Is_Private_Type (Root_Typ)
4926 and then Present (Full_View (Root_Typ))
4927 and then Nkind (N) = N_Extension_Aggregate
4928 then
4929 exit when Base_Type (Full_View (Root_Typ)) = Parent_Typ;
4930 end if;
4931 end loop;
4932
4933 -- Now collect components from all other ancestors, beginning
4934 -- with the current type. If the type has unknown discriminants
4935 -- use the component list of the Underlying_Record_View, which
4936 -- needs to be used for the subsequent expansion of the aggregate
4937 -- into assignments.
4938
4939 Parent_Elmt := First_Elmt (Parent_Typ_List);
4940 while Present (Parent_Elmt) loop
4941 Parent_Typ := Node (Parent_Elmt);
4942
4943 if Has_Unknown_Discriminants (Parent_Typ)
4944 and then Present (Underlying_Record_View (Typ))
4945 then
4946 Parent_Typ := Underlying_Record_View (Parent_Typ);
4947 end if;
4948
4949 Record_Def := Type_Definition (Parent (Base_Type (Parent_Typ)));
4950 Gather_Components (Empty,
4951 Component_List (Record_Extension_Part (Record_Def)),
4952 Governed_By => New_Assoc_List,
4953 Into => Components,
4954 Report_Errors => Errors_Found);
4955
4956 Next_Elmt (Parent_Elmt);
4957 end loop;
4958
4959 -- Typ is not a derived tagged type
4960
4961 else
4962 Record_Def := Type_Definition (Parent (Base_Type (Typ)));
4963
4964 if Null_Present (Record_Def) then
4965 null;
4966
4967 elsif not Has_Unknown_Discriminants (Typ) then
4968 Gather_Components
4969 (Base_Type (Typ),
4970 Component_List (Record_Def),
4971 Governed_By => New_Assoc_List,
4972 Into => Components,
4973 Report_Errors => Errors_Found);
4974
4975 else
4976 Gather_Components
4977 (Base_Type (Underlying_Record_View (Typ)),
4978 Component_List (Record_Def),
4979 Governed_By => New_Assoc_List,
4980 Into => Components,
4981 Report_Errors => Errors_Found);
4982 end if;
4983 end if;
4984
4985 if Errors_Found then
4986 return;
4987 end if;
4988 end Step_5;
4989
4990 -- STEP 6: Find component Values
4991
4992 Component := Empty;
4993 Component_Elmt := First_Elmt (Components);
4994
4995 -- First scan the remaining positional associations in the aggregate.
4996 -- Remember that at this point Positional_Expr contains the current
4997 -- positional association if any is left after looking for discriminant
4998 -- values in step 3.
4999
5000 while Present (Positional_Expr) and then Present (Component_Elmt) loop
5001 Component := Node (Component_Elmt);
5002 Resolve_Aggr_Expr (Positional_Expr, Component);
5003
5004 -- Ada 2005 (AI-231)
5005
5006 if Ada_Version >= Ada_2005 and then Known_Null (Positional_Expr) then
5007 Check_Can_Never_Be_Null (Component, Positional_Expr);
5008 end if;
5009
5010 if Present (Get_Value (Component, Component_Associations (N))) then
5011 Error_Msg_NE
5012 ("more than one value supplied for Component &", N, Component);
5013 end if;
5014
5015 Next (Positional_Expr);
5016 Next_Elmt (Component_Elmt);
5017 end loop;
5018
5019 if Present (Positional_Expr) then
5020 Error_Msg_N
5021 ("too many components for record aggregate", Positional_Expr);
5022 end if;
5023
5024 -- Now scan for the named arguments of the aggregate
5025
5026 while Present (Component_Elmt) loop
5027 Component := Node (Component_Elmt);
5028 Expr := Get_Value (Component, Component_Associations (N), True);
5029
5030 -- Note: The previous call to Get_Value sets the value of the
5031 -- variable Is_Box_Present.
5032
5033 -- Ada 2005 (AI-287): Handle components with default initialization.
5034 -- Note: This feature was originally added to Ada 2005 for limited
5035 -- but it was finally allowed with any type.
5036
5037 if Is_Box_Present then
5038 Check_Box_Component : declare
5039 Ctyp : constant Entity_Id := Etype (Component);
5040
5041 begin
5042 -- If there is a default expression for the aggregate, copy
5043 -- it into a new association. This copy must modify the scopes
5044 -- of internal types that may be attached to the expression
5045 -- (e.g. index subtypes of arrays) because in general the type
5046 -- declaration and the aggregate appear in different scopes,
5047 -- and the backend requires the scope of the type to match the
5048 -- point at which it is elaborated.
5049
5050 -- If the component has an initialization procedure (IP) we
5051 -- pass the component to the expander, which will generate
5052 -- the call to such IP.
5053
5054 -- If the component has discriminants, their values must
5055 -- be taken from their subtype. This is indispensable for
5056 -- constraints that are given by the current instance of an
5057 -- enclosing type, to allow the expansion of the aggregate to
5058 -- replace the reference to the current instance by the target
5059 -- object of the aggregate.
5060
5061 if Present (Parent (Component))
5062 and then Nkind (Parent (Component)) = N_Component_Declaration
5063 and then Present (Expression (Parent (Component)))
5064 then
5065 Expr :=
5066 New_Copy_Tree_And_Copy_Dimensions
5067 (Expression (Parent (Component)),
5068 New_Scope => Current_Scope,
5069 New_Sloc => Sloc (N));
5070
5071 -- As the type of the copied default expression may refer
5072 -- to discriminants of the record type declaration, these
5073 -- non-stored discriminants need to be rewritten into stored
5074 -- discriminant values for the aggregate. This is required
5075 -- in GNATprove mode, and is adopted in all modes to avoid
5076 -- special-casing GNATprove mode.
5077
5078 if Is_Array_Type (Etype (Expr)) then
5079 declare
5080 Rec_Typ : constant Entity_Id := Scope (Component);
5081 -- Root record type whose discriminants may be used as
5082 -- bounds in range nodes.
5083
5084 Assoc : Node_Id;
5085 Choice : Node_Id;
5086 Index : Node_Id;
5087
5088 begin
5089 -- Rewrite the range nodes occurring in the indexes
5090 -- and their types.
5091
5092 Index := First_Index (Etype (Expr));
5093 while Present (Index) loop
5094 Rewrite_Range (Rec_Typ, Index);
5095 Rewrite_Range
5096 (Rec_Typ, Scalar_Range (Etype (Index)));
5097
5098 Next_Index (Index);
5099 end loop;
5100
5101 -- Rewrite the range nodes occurring as aggregate
5102 -- bounds and component associations.
5103
5104 if Nkind (Expr) = N_Aggregate then
5105 if Present (Aggregate_Bounds (Expr)) then
5106 Rewrite_Range (Rec_Typ, Aggregate_Bounds (Expr));
5107 end if;
5108
5109 if Present (Component_Associations (Expr)) then
5110 Assoc := First (Component_Associations (Expr));
5111 while Present (Assoc) loop
5112 Choice := First (Choices (Assoc));
5113 while Present (Choice) loop
5114 Rewrite_Range (Rec_Typ, Choice);
5115
5116 Next (Choice);
5117 end loop;
5118
5119 Next (Assoc);
5120 end loop;
5121 end if;
5122 end if;
5123 end;
5124 end if;
5125
5126 Add_Association
5127 (Component => Component,
5128 Expr => Expr,
5129 Assoc_List => New_Assoc_List);
5130 Set_Has_Self_Reference (N);
5131
5132 -- A box-defaulted access component gets the value null. Also
5133 -- included are components of private types whose underlying
5134 -- type is an access type. In either case set the type of the
5135 -- literal, for subsequent use in semantic checks.
5136
5137 elsif Present (Underlying_Type (Ctyp))
5138 and then Is_Access_Type (Underlying_Type (Ctyp))
5139 then
5140 -- If the component's type is private with an access type as
5141 -- its underlying type then we have to create an unchecked
5142 -- conversion to satisfy type checking.
5143
5144 if Is_Private_Type (Ctyp) then
5145 declare
5146 Qual_Null : constant Node_Id :=
5147 Make_Qualified_Expression (Sloc (N),
5148 Subtype_Mark =>
5149 New_Occurrence_Of
5150 (Underlying_Type (Ctyp), Sloc (N)),
5151 Expression => Make_Null (Sloc (N)));
5152
5153 Convert_Null : constant Node_Id :=
5154 Unchecked_Convert_To
5155 (Ctyp, Qual_Null);
5156
5157 begin
5158 Analyze_And_Resolve (Convert_Null, Ctyp);
5159 Add_Association
5160 (Component => Component,
5161 Expr => Convert_Null,
5162 Assoc_List => New_Assoc_List);
5163 end;
5164
5165 -- Otherwise the component type is non-private
5166
5167 else
5168 Expr := Make_Null (Sloc (N));
5169 Set_Etype (Expr, Ctyp);
5170
5171 Add_Association
5172 (Component => Component,
5173 Expr => Expr,
5174 Assoc_List => New_Assoc_List);
5175 end if;
5176
5177 -- Ada 2012: If component is scalar with default value, use it
5178 -- by converting it to Ctyp, so that subtype constraints are
5179 -- checked.
5180
5181 elsif Is_Scalar_Type (Ctyp)
5182 and then Has_Default_Aspect (Ctyp)
5183 then
5184 declare
5185 Conv : constant Node_Id :=
5186 Convert_To
5187 (Typ => Ctyp,
5188 Expr =>
5189 New_Copy_Tree
5190 (Default_Aspect_Value
5191 (First_Subtype (Underlying_Type (Ctyp)))));
5192
5193 begin
5194 Analyze_And_Resolve (Conv, Ctyp);
5195 Add_Association
5196 (Component => Component,
5197 Expr => Conv,
5198 Assoc_List => New_Assoc_List);
5199 end;
5200
5201 elsif Has_Non_Null_Base_Init_Proc (Ctyp)
5202 or else not Expander_Active
5203 then
5204 if Is_Record_Type (Ctyp)
5205 and then Has_Discriminants (Ctyp)
5206 and then not Is_Private_Type (Ctyp)
5207 then
5208 -- We build a partially initialized aggregate with the
5209 -- values of the discriminants and box initialization
5210 -- for the rest, if other components are present.
5211
5212 -- The type of the aggregate is the known subtype of
5213 -- the component. The capture of discriminants must be
5214 -- recursive because subcomponents may be constrained
5215 -- (transitively) by discriminants of enclosing types.
5216 -- For a private type with discriminants, a call to the
5217 -- initialization procedure will be generated, and no
5218 -- subaggregate is needed.
5219
5220 Capture_Discriminants : declare
5221 Loc : constant Source_Ptr := Sloc (N);
5222 Expr : Node_Id;
5223
5224 begin
5225 Expr := Make_Aggregate (Loc, No_List, New_List);
5226 Set_Etype (Expr, Ctyp);
5227
5228 -- If the enclosing type has discriminants, they have
5229 -- been collected in the aggregate earlier, and they
5230 -- may appear as constraints of subcomponents.
5231
5232 -- Similarly if this component has discriminants, they
5233 -- might in turn be propagated to their components.
5234
5235 if Has_Discriminants (Typ) then
5236 Add_Discriminant_Values (Expr, New_Assoc_List);
5237 Propagate_Discriminants (Expr, New_Assoc_List);
5238
5239 elsif Has_Discriminants (Ctyp) then
5240 Add_Discriminant_Values
5241 (Expr, Component_Associations (Expr));
5242 Propagate_Discriminants
5243 (Expr, Component_Associations (Expr));
5244
5245 Build_Constrained_Itype
5246 (Expr, Ctyp, Component_Associations (Expr));
5247
5248 else
5249 declare
5250 Comp : Entity_Id;
5251
5252 begin
5253 -- If the type has additional components, create
5254 -- an OTHERS box association for them.
5255
5256 Comp := First_Component (Ctyp);
5257 while Present (Comp) loop
5258 if Ekind (Comp) = E_Component then
5259 if not Is_Record_Type (Etype (Comp)) then
5260 Append_To
5261 (Component_Associations (Expr),
5262 Make_Component_Association (Loc,
5263 Choices =>
5264 New_List (
5265 Make_Others_Choice (Loc)),
5266 Expression => Empty,
5267 Box_Present => True));
5268 end if;
5269
5270 exit;
5271 end if;
5272
5273 Next_Component (Comp);
5274 end loop;
5275 end;
5276 end if;
5277
5278 Add_Association
5279 (Component => Component,
5280 Expr => Expr,
5281 Assoc_List => New_Assoc_List);
5282 end Capture_Discriminants;
5283
5284 -- Otherwise the component type is not a record, or it has
5285 -- not discriminants, or it is private.
5286
5287 else
5288 Add_Association
5289 (Component => Component,
5290 Expr => Empty,
5291 Assoc_List => New_Assoc_List,
5292 Is_Box_Present => True);
5293 end if;
5294
5295 -- Otherwise we only need to resolve the expression if the
5296 -- component has partially initialized values (required to
5297 -- expand the corresponding assignments and run-time checks).
5298
5299 elsif Present (Expr)
5300 and then Is_Partially_Initialized_Type (Ctyp)
5301 then
5302 Resolve_Aggr_Expr (Expr, Component);
5303 end if;
5304 end Check_Box_Component;
5305
5306 elsif No (Expr) then
5307
5308 -- Ignore hidden components associated with the position of the
5309 -- interface tags: these are initialized dynamically.
5310
5311 if not Present (Related_Type (Component)) then
5312 Error_Msg_NE
5313 ("no value supplied for component &!", N, Component);
5314 end if;
5315
5316 else
5317 Resolve_Aggr_Expr (Expr, Component);
5318 end if;
5319
5320 Next_Elmt (Component_Elmt);
5321 end loop;
5322
5323 -- STEP 7: check for invalid components + check type in choice list
5324
5325 Step_7 : declare
5326 Assoc : Node_Id;
5327 New_Assoc : Node_Id;
5328
5329 Selectr : Node_Id;
5330 -- Selector name
5331
5332 Typech : Entity_Id;
5333 -- Type of first component in choice list
5334
5335 begin
5336 if Present (Component_Associations (N)) then
5337 Assoc := First (Component_Associations (N));
5338 else
5339 Assoc := Empty;
5340 end if;
5341
5342 Verification : while Present (Assoc) loop
5343 Selectr := First (Choices (Assoc));
5344 Typech := Empty;
5345
5346 if Nkind (Selectr) = N_Others_Choice then
5347
5348 -- Ada 2005 (AI-287): others choice may have expression or box
5349
5350 if No (Others_Etype) and then Others_Box = 0 then
5351 Error_Msg_N
5352 ("OTHERS must represent at least one component", Selectr);
5353
5354 elsif Others_Box = 1 and then Warn_On_Redundant_Constructs then
5355 Error_Msg_N ("others choice is redundant?", Box_Node);
5356 Error_Msg_N
5357 ("\previous choices cover all components?", Box_Node);
5358 end if;
5359
5360 exit Verification;
5361 end if;
5362
5363 while Present (Selectr) loop
5364 New_Assoc := First (New_Assoc_List);
5365 while Present (New_Assoc) loop
5366 Component := First (Choices (New_Assoc));
5367
5368 if Chars (Selectr) = Chars (Component) then
5369 if Style_Check then
5370 Check_Identifier (Selectr, Entity (Component));
5371 end if;
5372
5373 exit;
5374 end if;
5375
5376 Next (New_Assoc);
5377 end loop;
5378
5379 -- If no association, this is not a legal component of the type
5380 -- in question, unless its association is provided with a box.
5381
5382 if No (New_Assoc) then
5383 if Box_Present (Parent (Selectr)) then
5384
5385 -- This may still be a bogus component with a box. Scan
5386 -- list of components to verify that a component with
5387 -- that name exists.
5388
5389 declare
5390 C : Entity_Id;
5391
5392 begin
5393 C := First_Component (Typ);
5394 while Present (C) loop
5395 if Chars (C) = Chars (Selectr) then
5396
5397 -- If the context is an extension aggregate,
5398 -- the component must not be inherited from
5399 -- the ancestor part of the aggregate.
5400
5401 if Nkind (N) /= N_Extension_Aggregate
5402 or else
5403 Scope (Original_Record_Component (C)) /=
5404 Etype (Ancestor_Part (N))
5405 then
5406 exit;
5407 end if;
5408 end if;
5409
5410 Next_Component (C);
5411 end loop;
5412
5413 if No (C) then
5414 Error_Msg_Node_2 := Typ;
5415 Error_Msg_N ("& is not a component of}", Selectr);
5416 end if;
5417 end;
5418
5419 elsif Chars (Selectr) /= Name_uTag
5420 and then Chars (Selectr) /= Name_uParent
5421 then
5422 if not Has_Discriminants (Typ) then
5423 Error_Msg_Node_2 := Typ;
5424 Error_Msg_N ("& is not a component of}", Selectr);
5425 else
5426 Error_Msg_N
5427 ("& is not a component of the aggregate subtype",
5428 Selectr);
5429 end if;
5430
5431 Check_Misspelled_Component (Components, Selectr);
5432 end if;
5433
5434 elsif No (Typech) then
5435 Typech := Base_Type (Etype (Component));
5436
5437 -- AI05-0199: In Ada 2012, several components of anonymous
5438 -- access types can appear in a choice list, as long as the
5439 -- designated types match.
5440
5441 elsif Typech /= Base_Type (Etype (Component)) then
5442 if Ada_Version >= Ada_2012
5443 and then Ekind (Typech) = E_Anonymous_Access_Type
5444 and then
5445 Ekind (Etype (Component)) = E_Anonymous_Access_Type
5446 and then Base_Type (Designated_Type (Typech)) =
5447 Base_Type (Designated_Type (Etype (Component)))
5448 and then
5449 Subtypes_Statically_Match (Typech, (Etype (Component)))
5450 then
5451 null;
5452
5453 elsif not Box_Present (Parent (Selectr)) then
5454 Error_Msg_N
5455 ("components in choice list must have same type",
5456 Selectr);
5457 end if;
5458 end if;
5459
5460 Next (Selectr);
5461 end loop;
5462
5463 Next (Assoc);
5464 end loop Verification;
5465 end Step_7;
5466
5467 -- STEP 8: replace the original aggregate
5468
5469 Step_8 : declare
5470 New_Aggregate : constant Node_Id := New_Copy (N);
5471
5472 begin
5473 Set_Expressions (New_Aggregate, No_List);
5474 Set_Etype (New_Aggregate, Etype (N));
5475 Set_Component_Associations (New_Aggregate, New_Assoc_List);
5476 Set_Check_Actuals (New_Aggregate, Check_Actuals (N));
5477
5478 Rewrite (N, New_Aggregate);
5479 end Step_8;
5480
5481 -- Check the dimensions of the components in the record aggregate
5482
5483 Analyze_Dimension_Extension_Or_Record_Aggregate (N);
5484 end Resolve_Record_Aggregate;
5485
5486 -----------------------------
5487 -- Check_Can_Never_Be_Null --
5488 -----------------------------
5489
5490 procedure Check_Can_Never_Be_Null (Typ : Entity_Id; Expr : Node_Id) is
5491 Comp_Typ : Entity_Id;
5492
5493 begin
5494 pragma Assert
5495 (Ada_Version >= Ada_2005
5496 and then Present (Expr)
5497 and then Known_Null (Expr));
5498
5499 case Ekind (Typ) is
5500 when E_Array_Type =>
5501 Comp_Typ := Component_Type (Typ);
5502
5503 when E_Component
5504 | E_Discriminant
5505 =>
5506 Comp_Typ := Etype (Typ);
5507
5508 when others =>
5509 return;
5510 end case;
5511
5512 if Can_Never_Be_Null (Comp_Typ) then
5513
5514 -- Here we know we have a constraint error. Note that we do not use
5515 -- Apply_Compile_Time_Constraint_Error here to the Expr, which might
5516 -- seem the more natural approach. That's because in some cases the
5517 -- components are rewritten, and the replacement would be missed.
5518 -- We do not mark the whole aggregate as raising a constraint error,
5519 -- because the association may be a null array range.
5520
5521 Error_Msg_N
5522 ("(Ada 2005) null not allowed in null-excluding component??", Expr);
5523 Error_Msg_N
5524 ("\Constraint_Error will be raised at run time??", Expr);
5525
5526 Rewrite (Expr,
5527 Make_Raise_Constraint_Error
5528 (Sloc (Expr), Reason => CE_Access_Check_Failed));
5529 Set_Etype (Expr, Comp_Typ);
5530 Set_Analyzed (Expr);
5531 end if;
5532 end Check_Can_Never_Be_Null;
5533
5534 ---------------------
5535 -- Sort_Case_Table --
5536 ---------------------
5537
5538 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is
5539 U : constant Int := Case_Table'Last;
5540 K : Int;
5541 J : Int;
5542 T : Case_Bounds;
5543
5544 begin
5545 K := 1;
5546 while K < U loop
5547 T := Case_Table (K + 1);
5548
5549 J := K + 1;
5550 while J > 1
5551 and then Expr_Value (Case_Table (J - 1).Lo) > Expr_Value (T.Lo)
5552 loop
5553 Case_Table (J) := Case_Table (J - 1);
5554 J := J - 1;
5555 end loop;
5556
5557 Case_Table (J) := T;
5558 K := K + 1;
5559 end loop;
5560 end Sort_Case_Table;
5561
5562 end Sem_Aggr;