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