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