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