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