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