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