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