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