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