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