[Ada] AI12-0339: Empty function for Aggregate aspect of Ada containers
[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 Id :=
1644 Make_Defining_Identifier (Loc,
1645 Chars => Chars (Defining_Identifier (N)));
1646
1647 -- Insert and decorate the index variable in the current scope.
1648 -- The expression has to be analyzed once the index variable is
1649 -- directly visible. Mark the variable as referenced to prevent
1650 -- spurious warnings, given that subsequent uses of its name in the
1651 -- expression will reference the internal (synonym) loop variable.
1652
1653 Enter_Name (Id);
1654 Set_Etype (Id, Index_Typ);
1655 Set_Ekind (Id, E_Variable);
1656 Set_Scope (Id, Ent);
1657 Set_Referenced (Id);
1658
1659 -- Analyze a copy of the expression, to verify legality. We use
1660 -- a copy because the expression will be analyzed anew when the
1661 -- enclosing aggregate is expanded, and the construct is rewritten
1662 -- as a loop with a new index variable.
1663
1664 Expr := New_Copy_Tree (Expression (N));
1665 Dummy := Resolve_Aggr_Expr (Expr, False);
1666
1667 -- An iterated_component_association may appear in a nested
1668 -- aggregate for a multidimensional structure: preserve the bounds
1669 -- computed for the expression, as well as the anonymous array
1670 -- type generated for it; both are needed during array expansion.
1671 -- This does not work for more than two levels of nesting. ???
1672
1673 if Nkind (Expr) = N_Aggregate then
1674 Set_Aggregate_Bounds (Expression (N), Aggregate_Bounds (Expr));
1675 Set_Etype (Expression (N), Etype (Expr));
1676 end if;
1677
1678 End_Scope;
1679 end Resolve_Iterated_Component_Association;
1680
1681 -- Local variables
1682
1683 Assoc : Node_Id;
1684 Choice : Node_Id;
1685 Expr : Node_Id;
1686 Discard : Node_Id;
1687
1688 Aggr_Low : Node_Id := Empty;
1689 Aggr_High : Node_Id := Empty;
1690 -- The actual low and high bounds of this sub-aggregate
1691
1692 Case_Table_Size : Nat;
1693 -- Contains the size of the case table needed to sort aggregate choices
1694
1695 Choices_Low : Node_Id := Empty;
1696 Choices_High : Node_Id := Empty;
1697 -- The lowest and highest discrete choices values for a named aggregate
1698
1699 Delete_Choice : Boolean;
1700 -- Used when replacing a subtype choice with predicate by a list
1701
1702 Nb_Elements : Uint := Uint_0;
1703 -- The number of elements in a positional aggregate
1704
1705 Nb_Discrete_Choices : Nat := 0;
1706 -- The overall number of discrete choices (not counting others choice)
1707
1708 -- Start of processing for Resolve_Array_Aggregate
1709
1710 begin
1711 -- Ignore junk empty aggregate resulting from parser error
1712
1713 if No (Expressions (N))
1714 and then No (Component_Associations (N))
1715 and then not Null_Record_Present (N)
1716 then
1717 return False;
1718 end if;
1719
1720 -- STEP 1: make sure the aggregate is correctly formatted
1721
1722 if Present (Component_Associations (N)) then
1723 Assoc := First (Component_Associations (N));
1724 while Present (Assoc) loop
1725 if Nkind (Assoc) = N_Iterated_Component_Association then
1726 Resolve_Iterated_Component_Association (Assoc, Index_Typ);
1727 end if;
1728
1729 Choice := First (Choice_List (Assoc));
1730 Delete_Choice := False;
1731 while Present (Choice) loop
1732 if Nkind (Choice) = N_Others_Choice then
1733 Others_Present := True;
1734
1735 if Choice /= First (Choice_List (Assoc))
1736 or else Present (Next (Choice))
1737 then
1738 Error_Msg_N
1739 ("OTHERS must appear alone in a choice list", Choice);
1740 return Failure;
1741 end if;
1742
1743 if Present (Next (Assoc)) then
1744 Error_Msg_N
1745 ("OTHERS must appear last in an aggregate", Choice);
1746 return Failure;
1747 end if;
1748
1749 if Ada_Version = Ada_83
1750 and then Assoc /= First (Component_Associations (N))
1751 and then Nkind (Parent (N)) in
1752 N_Assignment_Statement | N_Object_Declaration
1753 then
1754 Error_Msg_N
1755 ("(Ada 83) illegal context for OTHERS choice", N);
1756 end if;
1757
1758 elsif Is_Entity_Name (Choice) then
1759 Analyze (Choice);
1760
1761 declare
1762 E : constant Entity_Id := Entity (Choice);
1763 New_Cs : List_Id;
1764 P : Node_Id;
1765 C : Node_Id;
1766
1767 begin
1768 if Is_Type (E) and then Has_Predicates (E) then
1769 Freeze_Before (N, E);
1770
1771 if Has_Dynamic_Predicate_Aspect (E) then
1772 Error_Msg_NE
1773 ("subtype& has dynamic predicate, not allowed "
1774 & "in aggregate choice", Choice, E);
1775
1776 elsif not Is_OK_Static_Subtype (E) then
1777 Error_Msg_NE
1778 ("non-static subtype& has predicate, not allowed "
1779 & "in aggregate choice", Choice, E);
1780 end if;
1781
1782 -- If the subtype has a static predicate, replace the
1783 -- original choice with the list of individual values
1784 -- covered by the predicate.
1785 -- This should be deferred to expansion time ???
1786
1787 if Present (Static_Discrete_Predicate (E)) then
1788 Delete_Choice := True;
1789
1790 New_Cs := New_List;
1791 P := First (Static_Discrete_Predicate (E));
1792 while Present (P) loop
1793 C := New_Copy (P);
1794 Set_Sloc (C, Sloc (Choice));
1795 Append_To (New_Cs, C);
1796 Next (P);
1797 end loop;
1798
1799 Insert_List_After (Choice, New_Cs);
1800 end if;
1801 end if;
1802 end;
1803 end if;
1804
1805 Nb_Choices := Nb_Choices + 1;
1806
1807 declare
1808 C : constant Node_Id := Choice;
1809
1810 begin
1811 Next (Choice);
1812
1813 if Delete_Choice then
1814 Remove (C);
1815 Nb_Choices := Nb_Choices - 1;
1816 Delete_Choice := False;
1817 end if;
1818 end;
1819 end loop;
1820
1821 Next (Assoc);
1822 end loop;
1823 end if;
1824
1825 -- At this point we know that the others choice, if present, is by
1826 -- itself and appears last in the aggregate. Check if we have mixed
1827 -- positional and discrete associations (other than the others choice).
1828
1829 if Present (Expressions (N))
1830 and then (Nb_Choices > 1
1831 or else (Nb_Choices = 1 and then not Others_Present))
1832 then
1833 Error_Msg_N
1834 ("named association cannot follow positional association",
1835 First (Choice_List (First (Component_Associations (N)))));
1836 return Failure;
1837 end if;
1838
1839 -- Test for the validity of an others choice if present
1840
1841 if Others_Present and then not Others_Allowed then
1842 Error_Msg_N
1843 ("OTHERS choice not allowed here",
1844 First (Choices (First (Component_Associations (N)))));
1845 return Failure;
1846 end if;
1847
1848 -- Protect against cascaded errors
1849
1850 if Etype (Index_Typ) = Any_Type then
1851 return Failure;
1852 end if;
1853
1854 -- STEP 2: Process named components
1855
1856 if No (Expressions (N)) then
1857 if Others_Present then
1858 Case_Table_Size := Nb_Choices - 1;
1859 else
1860 Case_Table_Size := Nb_Choices;
1861 end if;
1862
1863 Step_2 : declare
1864 function Empty_Range (A : Node_Id) return Boolean;
1865 -- If an association covers an empty range, some warnings on the
1866 -- expression of the association can be disabled.
1867
1868 -----------------
1869 -- Empty_Range --
1870 -----------------
1871
1872 function Empty_Range (A : Node_Id) return Boolean is
1873 R : constant Node_Id := First (Choices (A));
1874 begin
1875 return No (Next (R))
1876 and then Nkind (R) = N_Range
1877 and then Compile_Time_Compare
1878 (Low_Bound (R), High_Bound (R), False) = GT;
1879 end Empty_Range;
1880
1881 -- Local variables
1882
1883 Low : Node_Id;
1884 High : Node_Id;
1885 -- Denote the lowest and highest values in an aggregate choice
1886
1887 S_Low : Node_Id := Empty;
1888 S_High : Node_Id := Empty;
1889 -- if a choice in an aggregate is a subtype indication these
1890 -- denote the lowest and highest values of the subtype
1891
1892 Table : Case_Table_Type (1 .. Case_Table_Size);
1893 -- Used to sort all the different choice values
1894
1895 Single_Choice : Boolean;
1896 -- Set to true every time there is a single discrete choice in a
1897 -- discrete association
1898
1899 Prev_Nb_Discrete_Choices : Nat;
1900 -- Used to keep track of the number of discrete choices in the
1901 -- current association.
1902
1903 Errors_Posted_On_Choices : Boolean := False;
1904 -- Keeps track of whether any choices have semantic errors
1905
1906 -- Start of processing for Step_2
1907
1908 begin
1909 -- STEP 2 (A): Check discrete choices validity
1910
1911 Assoc := First (Component_Associations (N));
1912 while Present (Assoc) loop
1913 Prev_Nb_Discrete_Choices := Nb_Discrete_Choices;
1914 Choice := First (Choice_List (Assoc));
1915
1916 loop
1917 Analyze (Choice);
1918
1919 if Nkind (Choice) = N_Others_Choice then
1920 Single_Choice := False;
1921 exit;
1922
1923 -- Test for subtype mark without constraint
1924
1925 elsif Is_Entity_Name (Choice) and then
1926 Is_Type (Entity (Choice))
1927 then
1928 if Base_Type (Entity (Choice)) /= Index_Base then
1929 Error_Msg_N
1930 ("invalid subtype mark in aggregate choice",
1931 Choice);
1932 return Failure;
1933 end if;
1934
1935 -- Case of subtype indication
1936
1937 elsif Nkind (Choice) = N_Subtype_Indication then
1938 Resolve_Discrete_Subtype_Indication (Choice, Index_Base);
1939
1940 if Has_Dynamic_Predicate_Aspect
1941 (Entity (Subtype_Mark (Choice)))
1942 then
1943 Error_Msg_NE
1944 ("subtype& has dynamic predicate, "
1945 & "not allowed in aggregate choice",
1946 Choice, Entity (Subtype_Mark (Choice)));
1947 end if;
1948
1949 -- Does the subtype indication evaluation raise CE?
1950
1951 Get_Index_Bounds (Subtype_Mark (Choice), S_Low, S_High);
1952 Get_Index_Bounds (Choice, Low, High);
1953 Check_Bounds (S_Low, S_High, Low, High);
1954
1955 -- Case of range or expression
1956
1957 else
1958 Resolve (Choice, Index_Base);
1959 Check_Unset_Reference (Choice);
1960 Check_Non_Static_Context (Choice);
1961
1962 -- If semantic errors were posted on the choice, then
1963 -- record that for possible early return from later
1964 -- processing (see handling of enumeration choices).
1965
1966 if Error_Posted (Choice) then
1967 Errors_Posted_On_Choices := True;
1968 end if;
1969
1970 -- Do not range check a choice. This check is redundant
1971 -- since this test is already done when we check that the
1972 -- bounds of the array aggregate are within range.
1973
1974 Set_Do_Range_Check (Choice, False);
1975 end if;
1976
1977 -- If we could not resolve the discrete choice stop here
1978
1979 if Etype (Choice) = Any_Type then
1980 return Failure;
1981
1982 -- If the discrete choice raises CE get its original bounds
1983
1984 elsif Nkind (Choice) = N_Raise_Constraint_Error then
1985 Set_Raises_Constraint_Error (N);
1986 Get_Index_Bounds (Original_Node (Choice), Low, High);
1987
1988 -- Otherwise get its bounds as usual
1989
1990 else
1991 Get_Index_Bounds (Choice, Low, High);
1992 end if;
1993
1994 if (Dynamic_Or_Null_Range (Low, High)
1995 or else (Nkind (Choice) = N_Subtype_Indication
1996 and then
1997 Dynamic_Or_Null_Range (S_Low, S_High)))
1998 and then Nb_Choices /= 1
1999 then
2000 Error_Msg_N
2001 ("dynamic or empty choice in aggregate "
2002 & "must be the only choice", Choice);
2003 return Failure;
2004 end if;
2005
2006 if not (All_Composite_Constraints_Static (Low)
2007 and then All_Composite_Constraints_Static (High)
2008 and then All_Composite_Constraints_Static (S_Low)
2009 and then All_Composite_Constraints_Static (S_High))
2010 then
2011 Check_Restriction (No_Dynamic_Sized_Objects, Choice);
2012 end if;
2013
2014 Nb_Discrete_Choices := Nb_Discrete_Choices + 1;
2015 Table (Nb_Discrete_Choices).Lo := Low;
2016 Table (Nb_Discrete_Choices).Hi := High;
2017 Table (Nb_Discrete_Choices).Choice := Choice;
2018
2019 Next (Choice);
2020
2021 if No (Choice) then
2022
2023 -- Check if we have a single discrete choice and whether
2024 -- this discrete choice specifies a single value.
2025
2026 Single_Choice :=
2027 (Nb_Discrete_Choices = Prev_Nb_Discrete_Choices + 1)
2028 and then (Low = High);
2029
2030 exit;
2031 end if;
2032 end loop;
2033
2034 -- Ada 2005 (AI-231)
2035
2036 if Ada_Version >= Ada_2005
2037 and then Known_Null (Expression (Assoc))
2038 and then not Empty_Range (Assoc)
2039 then
2040 Check_Can_Never_Be_Null (Etype (N), Expression (Assoc));
2041 end if;
2042
2043 -- Ada 2005 (AI-287): In case of default initialized component
2044 -- we delay the resolution to the expansion phase.
2045
2046 if Box_Present (Assoc) then
2047
2048 -- Ada 2005 (AI-287): In case of default initialization of a
2049 -- component the expander will generate calls to the
2050 -- corresponding initialization subprogram. We need to call
2051 -- Resolve_Aggr_Expr to check the rules about
2052 -- dimensionality.
2053
2054 if not Resolve_Aggr_Expr
2055 (Assoc, Single_Elmt => Single_Choice)
2056 then
2057 return Failure;
2058 end if;
2059
2060 elsif Nkind (Assoc) = N_Iterated_Component_Association then
2061 null; -- handled above, in a loop context.
2062
2063 elsif not Resolve_Aggr_Expr
2064 (Expression (Assoc), Single_Elmt => Single_Choice)
2065 then
2066 return Failure;
2067
2068 -- Check incorrect use of dynamically tagged expression
2069
2070 -- We differentiate here two cases because the expression may
2071 -- not be decorated. For example, the analysis and resolution
2072 -- of the expression associated with the others choice will be
2073 -- done later with the full aggregate. In such case we
2074 -- duplicate the expression tree to analyze the copy and
2075 -- perform the required check.
2076
2077 elsif not Present (Etype (Expression (Assoc))) then
2078 declare
2079 Save_Analysis : constant Boolean := Full_Analysis;
2080 Expr : constant Node_Id :=
2081 New_Copy_Tree (Expression (Assoc));
2082
2083 begin
2084 Expander_Mode_Save_And_Set (False);
2085 Full_Analysis := False;
2086
2087 -- Analyze the expression, making sure it is properly
2088 -- attached to the tree before we do the analysis.
2089
2090 Set_Parent (Expr, Parent (Expression (Assoc)));
2091 Analyze (Expr);
2092
2093 -- Compute its dimensions now, rather than at the end of
2094 -- resolution, because in the case of multidimensional
2095 -- aggregates subsequent expansion may lead to spurious
2096 -- errors.
2097
2098 Check_Expression_Dimensions (Expr, Component_Typ);
2099
2100 -- If the expression is a literal, propagate this info
2101 -- to the expression in the association, to enable some
2102 -- optimizations downstream.
2103
2104 if Is_Entity_Name (Expr)
2105 and then Present (Entity (Expr))
2106 and then Ekind (Entity (Expr)) = E_Enumeration_Literal
2107 then
2108 Analyze_And_Resolve
2109 (Expression (Assoc), Component_Typ);
2110 end if;
2111
2112 Full_Analysis := Save_Analysis;
2113 Expander_Mode_Restore;
2114
2115 if Is_Tagged_Type (Etype (Expr)) then
2116 Check_Dynamically_Tagged_Expression
2117 (Expr => Expr,
2118 Typ => Component_Type (Etype (N)),
2119 Related_Nod => N);
2120 end if;
2121 end;
2122
2123 elsif Is_Tagged_Type (Etype (Expression (Assoc))) then
2124 Check_Dynamically_Tagged_Expression
2125 (Expr => Expression (Assoc),
2126 Typ => Component_Type (Etype (N)),
2127 Related_Nod => N);
2128 end if;
2129
2130 Next (Assoc);
2131 end loop;
2132
2133 -- If aggregate contains more than one choice then these must be
2134 -- static. Check for duplicate and missing values.
2135
2136 -- Note: there is duplicated code here wrt Check_Choice_Set in
2137 -- the body of Sem_Case, and it is possible we could just reuse
2138 -- that procedure. To be checked ???
2139
2140 if Nb_Discrete_Choices > 1 then
2141 Check_Choices : declare
2142 Choice : Node_Id;
2143 -- Location of choice for messages
2144
2145 Hi_Val : Uint;
2146 Lo_Val : Uint;
2147 -- High end of one range and Low end of the next. Should be
2148 -- contiguous if there is no hole in the list of values.
2149
2150 Lo_Dup : Uint;
2151 Hi_Dup : Uint;
2152 -- End points of duplicated range
2153
2154 Missing_Or_Duplicates : Boolean := False;
2155 -- Set True if missing or duplicate choices found
2156
2157 procedure Output_Bad_Choices (Lo, Hi : Uint; C : Node_Id);
2158 -- Output continuation message with a representation of the
2159 -- bounds (just Lo if Lo = Hi, else Lo .. Hi). C is the
2160 -- choice node where the message is to be posted.
2161
2162 ------------------------
2163 -- Output_Bad_Choices --
2164 ------------------------
2165
2166 procedure Output_Bad_Choices (Lo, Hi : Uint; C : Node_Id) is
2167 begin
2168 -- Enumeration type case
2169
2170 if Is_Enumeration_Type (Index_Typ) then
2171 Error_Msg_Name_1 :=
2172 Chars (Get_Enum_Lit_From_Pos (Index_Typ, Lo, Loc));
2173 Error_Msg_Name_2 :=
2174 Chars (Get_Enum_Lit_From_Pos (Index_Typ, Hi, Loc));
2175
2176 if Lo = Hi then
2177 Error_Msg_N ("\\ %!", C);
2178 else
2179 Error_Msg_N ("\\ % .. %!", C);
2180 end if;
2181
2182 -- Integer types case
2183
2184 else
2185 Error_Msg_Uint_1 := Lo;
2186 Error_Msg_Uint_2 := Hi;
2187
2188 if Lo = Hi then
2189 Error_Msg_N ("\\ ^!", C);
2190 else
2191 Error_Msg_N ("\\ ^ .. ^!", C);
2192 end if;
2193 end if;
2194 end Output_Bad_Choices;
2195
2196 -- Start of processing for Check_Choices
2197
2198 begin
2199 Sort_Case_Table (Table);
2200
2201 -- First we do a quick linear loop to find out if we have
2202 -- any duplicates or missing entries (usually we have a
2203 -- legal aggregate, so this will get us out quickly).
2204
2205 for J in 1 .. Nb_Discrete_Choices - 1 loop
2206 Hi_Val := Expr_Value (Table (J).Hi);
2207 Lo_Val := Expr_Value (Table (J + 1).Lo);
2208
2209 if Lo_Val <= Hi_Val
2210 or else (Lo_Val > Hi_Val + 1
2211 and then not Others_Present)
2212 then
2213 Missing_Or_Duplicates := True;
2214 exit;
2215 end if;
2216 end loop;
2217
2218 -- If we have missing or duplicate entries, first fill in
2219 -- the Highest entries to make life easier in the following
2220 -- loops to detect bad entries.
2221
2222 if Missing_Or_Duplicates then
2223 Table (1).Highest := Expr_Value (Table (1).Hi);
2224
2225 for J in 2 .. Nb_Discrete_Choices loop
2226 Table (J).Highest :=
2227 UI_Max
2228 (Table (J - 1).Highest, Expr_Value (Table (J).Hi));
2229 end loop;
2230
2231 -- Loop through table entries to find duplicate indexes
2232
2233 for J in 2 .. Nb_Discrete_Choices loop
2234 Lo_Val := Expr_Value (Table (J).Lo);
2235 Hi_Val := Expr_Value (Table (J).Hi);
2236
2237 -- Case where we have duplicates (the lower bound of
2238 -- this choice is less than or equal to the highest
2239 -- high bound found so far).
2240
2241 if Lo_Val <= Table (J - 1).Highest then
2242
2243 -- We move backwards looking for duplicates. We can
2244 -- abandon this loop as soon as we reach a choice
2245 -- highest value that is less than Lo_Val.
2246
2247 for K in reverse 1 .. J - 1 loop
2248 exit when Table (K).Highest < Lo_Val;
2249
2250 -- Here we may have duplicates between entries
2251 -- for K and J. Get range of duplicates.
2252
2253 Lo_Dup :=
2254 UI_Max (Lo_Val, Expr_Value (Table (K).Lo));
2255 Hi_Dup :=
2256 UI_Min (Hi_Val, Expr_Value (Table (K).Hi));
2257
2258 -- Nothing to do if duplicate range is null
2259
2260 if Lo_Dup > Hi_Dup then
2261 null;
2262
2263 -- Otherwise place proper message
2264
2265 else
2266 -- We place message on later choice, with a
2267 -- line reference to the earlier choice.
2268
2269 if Sloc (Table (J).Choice) <
2270 Sloc (Table (K).Choice)
2271 then
2272 Choice := Table (K).Choice;
2273 Error_Msg_Sloc := Sloc (Table (J).Choice);
2274 else
2275 Choice := Table (J).Choice;
2276 Error_Msg_Sloc := Sloc (Table (K).Choice);
2277 end if;
2278
2279 if Lo_Dup = Hi_Dup then
2280 Error_Msg_N
2281 ("index value in array aggregate "
2282 & "duplicates the one given#!", Choice);
2283 else
2284 Error_Msg_N
2285 ("index values in array aggregate "
2286 & "duplicate those given#!", Choice);
2287 end if;
2288
2289 Output_Bad_Choices (Lo_Dup, Hi_Dup, Choice);
2290 end if;
2291 end loop;
2292 end if;
2293 end loop;
2294
2295 -- Loop through entries in table to find missing indexes.
2296 -- Not needed if others, since missing impossible.
2297
2298 if not Others_Present then
2299 for J in 2 .. Nb_Discrete_Choices loop
2300 Lo_Val := Expr_Value (Table (J).Lo);
2301 Hi_Val := Table (J - 1).Highest;
2302
2303 if Lo_Val > Hi_Val + 1 then
2304
2305 declare
2306 Error_Node : Node_Id;
2307
2308 begin
2309 -- If the choice is the bound of a range in
2310 -- a subtype indication, it is not in the
2311 -- source lists for the aggregate itself, so
2312 -- post the error on the aggregate. Otherwise
2313 -- post it on choice itself.
2314
2315 Choice := Table (J).Choice;
2316
2317 if Is_List_Member (Choice) then
2318 Error_Node := Choice;
2319 else
2320 Error_Node := N;
2321 end if;
2322
2323 if Hi_Val + 1 = Lo_Val - 1 then
2324 Error_Msg_N
2325 ("missing index value "
2326 & "in array aggregate!", Error_Node);
2327 else
2328 Error_Msg_N
2329 ("missing index values "
2330 & "in array aggregate!", Error_Node);
2331 end if;
2332
2333 Output_Bad_Choices
2334 (Hi_Val + 1, Lo_Val - 1, Error_Node);
2335 end;
2336 end if;
2337 end loop;
2338 end if;
2339
2340 -- If either missing or duplicate values, return failure
2341
2342 Set_Etype (N, Any_Composite);
2343 return Failure;
2344 end if;
2345 end Check_Choices;
2346 end if;
2347
2348 -- STEP 2 (B): Compute aggregate bounds and min/max choices values
2349
2350 if Nb_Discrete_Choices > 0 then
2351 Choices_Low := Table (1).Lo;
2352 Choices_High := Table (Nb_Discrete_Choices).Hi;
2353 end if;
2354
2355 -- If Others is present, then bounds of aggregate come from the
2356 -- index constraint (not the choices in the aggregate itself).
2357
2358 if Others_Present then
2359 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2360
2361 -- Abandon processing if either bound is already signalled as
2362 -- an error (prevents junk cascaded messages and blow ups).
2363
2364 if Nkind (Aggr_Low) = N_Error
2365 or else
2366 Nkind (Aggr_High) = N_Error
2367 then
2368 return False;
2369 end if;
2370
2371 -- No others clause present
2372
2373 else
2374 -- Special processing if others allowed and not present. This
2375 -- means that the bounds of the aggregate come from the index
2376 -- constraint (and the length must match).
2377
2378 if Others_Allowed then
2379 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2380
2381 -- Abandon processing if either bound is already signalled
2382 -- as an error (stop junk cascaded messages and blow ups).
2383
2384 if Nkind (Aggr_Low) = N_Error
2385 or else
2386 Nkind (Aggr_High) = N_Error
2387 then
2388 return False;
2389 end if;
2390
2391 -- If others allowed, and no others present, then the array
2392 -- should cover all index values. If it does not, we will
2393 -- get a length check warning, but there is two cases where
2394 -- an additional warning is useful:
2395
2396 -- If we have no positional components, and the length is
2397 -- wrong (which we can tell by others being allowed with
2398 -- missing components), and the index type is an enumeration
2399 -- type, then issue appropriate warnings about these missing
2400 -- components. They are only warnings, since the aggregate
2401 -- is fine, it's just the wrong length. We skip this check
2402 -- for standard character types (since there are no literals
2403 -- and it is too much trouble to concoct them), and also if
2404 -- any of the bounds have values that are not known at
2405 -- compile time.
2406
2407 -- Another case warranting a warning is when the length
2408 -- is right, but as above we have an index type that is
2409 -- an enumeration, and the bounds do not match. This is a
2410 -- case where dubious sliding is allowed and we generate a
2411 -- warning that the bounds do not match.
2412
2413 if No (Expressions (N))
2414 and then Nkind (Index) = N_Range
2415 and then Is_Enumeration_Type (Etype (Index))
2416 and then not Is_Standard_Character_Type (Etype (Index))
2417 and then Compile_Time_Known_Value (Aggr_Low)
2418 and then Compile_Time_Known_Value (Aggr_High)
2419 and then Compile_Time_Known_Value (Choices_Low)
2420 and then Compile_Time_Known_Value (Choices_High)
2421 then
2422 -- If any of the expressions or range bounds in choices
2423 -- have semantic errors, then do not attempt further
2424 -- resolution, to prevent cascaded errors.
2425
2426 if Errors_Posted_On_Choices then
2427 return Failure;
2428 end if;
2429
2430 declare
2431 ALo : constant Node_Id := Expr_Value_E (Aggr_Low);
2432 AHi : constant Node_Id := Expr_Value_E (Aggr_High);
2433 CLo : constant Node_Id := Expr_Value_E (Choices_Low);
2434 CHi : constant Node_Id := Expr_Value_E (Choices_High);
2435
2436 Ent : Entity_Id;
2437
2438 begin
2439 -- Warning case 1, missing values at start/end. Only
2440 -- do the check if the number of entries is too small.
2441
2442 if (Enumeration_Pos (CHi) - Enumeration_Pos (CLo))
2443 <
2444 (Enumeration_Pos (AHi) - Enumeration_Pos (ALo))
2445 then
2446 Error_Msg_N
2447 ("missing index value(s) in array aggregate??",
2448 N);
2449
2450 -- Output missing value(s) at start
2451
2452 if Chars (ALo) /= Chars (CLo) then
2453 Ent := Prev (CLo);
2454
2455 if Chars (ALo) = Chars (Ent) then
2456 Error_Msg_Name_1 := Chars (ALo);
2457 Error_Msg_N ("\ %??", N);
2458 else
2459 Error_Msg_Name_1 := Chars (ALo);
2460 Error_Msg_Name_2 := Chars (Ent);
2461 Error_Msg_N ("\ % .. %??", N);
2462 end if;
2463 end if;
2464
2465 -- Output missing value(s) at end
2466
2467 if Chars (AHi) /= Chars (CHi) then
2468 Ent := Next (CHi);
2469
2470 if Chars (AHi) = Chars (Ent) then
2471 Error_Msg_Name_1 := Chars (Ent);
2472 Error_Msg_N ("\ %??", N);
2473 else
2474 Error_Msg_Name_1 := Chars (Ent);
2475 Error_Msg_Name_2 := Chars (AHi);
2476 Error_Msg_N ("\ % .. %??", N);
2477 end if;
2478 end if;
2479
2480 -- Warning case 2, dubious sliding. The First_Subtype
2481 -- test distinguishes between a constrained type where
2482 -- sliding is not allowed (so we will get a warning
2483 -- later that Constraint_Error will be raised), and
2484 -- the unconstrained case where sliding is permitted.
2485
2486 elsif (Enumeration_Pos (CHi) - Enumeration_Pos (CLo))
2487 =
2488 (Enumeration_Pos (AHi) - Enumeration_Pos (ALo))
2489 and then Chars (ALo) /= Chars (CLo)
2490 and then
2491 not Is_Constrained (First_Subtype (Etype (N)))
2492 then
2493 Error_Msg_N
2494 ("bounds of aggregate do not match target??", N);
2495 end if;
2496 end;
2497 end if;
2498 end if;
2499
2500 -- If no others, aggregate bounds come from aggregate
2501
2502 Aggr_Low := Choices_Low;
2503 Aggr_High := Choices_High;
2504 end if;
2505 end Step_2;
2506
2507 -- STEP 3: Process positional components
2508
2509 else
2510 -- STEP 3 (A): Process positional elements
2511
2512 Expr := First (Expressions (N));
2513 Nb_Elements := Uint_0;
2514 while Present (Expr) loop
2515 Nb_Elements := Nb_Elements + 1;
2516
2517 -- Ada 2005 (AI-231)
2518
2519 if Ada_Version >= Ada_2005 and then Known_Null (Expr) then
2520 Check_Can_Never_Be_Null (Etype (N), Expr);
2521 end if;
2522
2523 if not Resolve_Aggr_Expr (Expr, Single_Elmt => True) then
2524 return Failure;
2525 end if;
2526
2527 -- Check incorrect use of dynamically tagged expression
2528
2529 if Is_Tagged_Type (Etype (Expr)) then
2530 Check_Dynamically_Tagged_Expression
2531 (Expr => Expr,
2532 Typ => Component_Type (Etype (N)),
2533 Related_Nod => N);
2534 end if;
2535
2536 Next (Expr);
2537 end loop;
2538
2539 if Others_Present then
2540 Assoc := Last (Component_Associations (N));
2541
2542 -- Ada 2005 (AI-231)
2543
2544 if Ada_Version >= Ada_2005 and then Known_Null (Assoc) then
2545 Check_Can_Never_Be_Null (Etype (N), Expression (Assoc));
2546 end if;
2547
2548 -- Ada 2005 (AI-287): In case of default initialized component,
2549 -- we delay the resolution to the expansion phase.
2550
2551 if Box_Present (Assoc) then
2552
2553 -- Ada 2005 (AI-287): In case of default initialization of a
2554 -- component the expander will generate calls to the
2555 -- corresponding initialization subprogram. We need to call
2556 -- Resolve_Aggr_Expr to check the rules about
2557 -- dimensionality.
2558
2559 if not Resolve_Aggr_Expr (Assoc, Single_Elmt => False) then
2560 return Failure;
2561 end if;
2562
2563 elsif not Resolve_Aggr_Expr (Expression (Assoc),
2564 Single_Elmt => False)
2565 then
2566 return Failure;
2567
2568 -- Check incorrect use of dynamically tagged expression. The
2569 -- expression of the others choice has not been resolved yet.
2570 -- In order to diagnose the semantic error we create a duplicate
2571 -- tree to analyze it and perform the check.
2572
2573 else
2574 declare
2575 Save_Analysis : constant Boolean := Full_Analysis;
2576 Expr : constant Node_Id :=
2577 New_Copy_Tree (Expression (Assoc));
2578
2579 begin
2580 Expander_Mode_Save_And_Set (False);
2581 Full_Analysis := False;
2582 Analyze (Expr);
2583 Full_Analysis := Save_Analysis;
2584 Expander_Mode_Restore;
2585
2586 if Is_Tagged_Type (Etype (Expr)) then
2587 Check_Dynamically_Tagged_Expression
2588 (Expr => Expr,
2589 Typ => Component_Type (Etype (N)),
2590 Related_Nod => N);
2591 end if;
2592 end;
2593 end if;
2594 end if;
2595
2596 -- STEP 3 (B): Compute the aggregate bounds
2597
2598 if Others_Present then
2599 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2600
2601 else
2602 if Others_Allowed then
2603 Get_Index_Bounds (Index_Constr, Aggr_Low, Discard);
2604 else
2605 Aggr_Low := Index_Typ_Low;
2606 end if;
2607
2608 Aggr_High := Add (Nb_Elements - 1, To => Aggr_Low);
2609 Check_Bound (Index_Base_High, Aggr_High);
2610 end if;
2611 end if;
2612
2613 -- STEP 4: Perform static aggregate checks and save the bounds
2614
2615 -- Check (A)
2616
2617 Check_Bounds (Index_Typ_Low, Index_Typ_High, Aggr_Low, Aggr_High);
2618 Check_Bounds (Index_Base_Low, Index_Base_High, Aggr_Low, Aggr_High);
2619
2620 -- Check (B)
2621
2622 if Others_Present and then Nb_Discrete_Choices > 0 then
2623 Check_Bounds (Aggr_Low, Aggr_High, Choices_Low, Choices_High);
2624 Check_Bounds (Index_Typ_Low, Index_Typ_High,
2625 Choices_Low, Choices_High);
2626 Check_Bounds (Index_Base_Low, Index_Base_High,
2627 Choices_Low, Choices_High);
2628
2629 -- Check (C)
2630
2631 elsif Others_Present and then Nb_Elements > 0 then
2632 Check_Length (Aggr_Low, Aggr_High, Nb_Elements);
2633 Check_Length (Index_Typ_Low, Index_Typ_High, Nb_Elements);
2634 Check_Length (Index_Base_Low, Index_Base_High, Nb_Elements);
2635 end if;
2636
2637 if Raises_Constraint_Error (Aggr_Low)
2638 or else Raises_Constraint_Error (Aggr_High)
2639 then
2640 Set_Raises_Constraint_Error (N);
2641 end if;
2642
2643 Aggr_Low := Duplicate_Subexpr (Aggr_Low);
2644
2645 -- Do not duplicate Aggr_High if Aggr_High = Aggr_Low + Nb_Elements
2646 -- since the addition node returned by Add is not yet analyzed. Attach
2647 -- to tree and analyze first. Reset analyzed flag to ensure it will get
2648 -- analyzed when it is a literal bound whose type must be properly set.
2649
2650 if Others_Present or else Nb_Discrete_Choices > 0 then
2651 Aggr_High := Duplicate_Subexpr (Aggr_High);
2652
2653 if Etype (Aggr_High) = Universal_Integer then
2654 Set_Analyzed (Aggr_High, False);
2655 end if;
2656 end if;
2657
2658 -- If the aggregate already has bounds attached to it, it means this is
2659 -- a positional aggregate created as an optimization by
2660 -- Exp_Aggr.Convert_To_Positional, so we don't want to change those
2661 -- bounds.
2662
2663 if Present (Aggregate_Bounds (N)) and then not Others_Allowed then
2664 Aggr_Low := Low_Bound (Aggregate_Bounds (N));
2665 Aggr_High := High_Bound (Aggregate_Bounds (N));
2666 end if;
2667
2668 Set_Aggregate_Bounds
2669 (N, Make_Range (Loc, Low_Bound => Aggr_Low, High_Bound => Aggr_High));
2670
2671 -- The bounds may contain expressions that must be inserted upwards.
2672 -- Attach them fully to the tree. After analysis, remove side effects
2673 -- from upper bound, if still needed.
2674
2675 Set_Parent (Aggregate_Bounds (N), N);
2676 Analyze_And_Resolve (Aggregate_Bounds (N), Index_Typ);
2677 Check_Unset_Reference (Aggregate_Bounds (N));
2678
2679 if not Others_Present and then Nb_Discrete_Choices = 0 then
2680 Set_High_Bound
2681 (Aggregate_Bounds (N),
2682 Duplicate_Subexpr (High_Bound (Aggregate_Bounds (N))));
2683 end if;
2684
2685 -- Check the dimensions of each component in the array aggregate
2686
2687 Analyze_Dimension_Array_Aggregate (N, Component_Typ);
2688
2689 return Success;
2690 end Resolve_Array_Aggregate;
2691
2692 ---------------------------------
2693 -- Resolve_Container_Aggregate --
2694 ---------------------------------
2695
2696 procedure Resolve_Container_Aggregate (N : Node_Id; Typ : Entity_Id) is
2697 procedure Resolve_Iterated_Association
2698 (Comp : Node_Id;
2699 Key_Type : Entity_Id;
2700 Elmt_Type : Entity_Id);
2701 -- Resolve choices and expression in an iterated component association
2702 -- or an iterated element association, which has a key_expression.
2703 -- This is similar but not identical to the handling of this construct
2704 -- in an array aggregate.
2705 -- For a named container, the type of each choice must be compatible
2706 -- with the key type. For a positional container, the choice must be
2707 -- a subtype indication or an iterator specification that determines
2708 -- an element type.
2709
2710 Asp : constant Node_Id := Find_Value_Of_Aspect (Typ, Aspect_Aggregate);
2711
2712 Empty_Subp : Node_Id := Empty;
2713 Add_Named_Subp : Node_Id := Empty;
2714 Add_Unnamed_Subp : Node_Id := Empty;
2715 New_Indexed_Subp : Node_Id := Empty;
2716 Assign_Indexed_Subp : Node_Id := Empty;
2717
2718 ----------------------------------
2719 -- Resolve_Iterated_Association --
2720 ----------------------------------
2721
2722 procedure Resolve_Iterated_Association
2723 (Comp : Node_Id;
2724 Key_Type : Entity_Id;
2725 Elmt_Type : Entity_Id)
2726 is
2727 Choice : Node_Id;
2728 Ent : Entity_Id;
2729 Expr : Node_Id;
2730 Key_Expr : Node_Id;
2731 Id : Entity_Id;
2732 Id_Name : Name_Id;
2733 Iter : Node_Id;
2734 Typ : Entity_Id := Empty;
2735
2736 begin
2737 -- If this is an Iterated_Element_Association then either a
2738 -- an Iterator_Specification or a Loop_Parameter specification
2739 -- is present. In both cases a Key_Expression is present.
2740
2741 if Nkind (Comp) = N_Iterated_Element_Association then
2742 if Present (Loop_Parameter_Specification (Comp)) then
2743 Analyze_Loop_Parameter_Specification
2744 (Loop_Parameter_Specification (Comp));
2745 Id_Name := Chars (Defining_Identifier
2746 (Loop_Parameter_Specification (Comp)));
2747 else
2748 Iter := Copy_Separate_Tree (Iterator_Specification (Comp));
2749 Analyze (Iter);
2750 Typ := Etype (Defining_Identifier (Iter));
2751 Id_Name := Chars (Defining_Identifier
2752 (Iterator_Specification (Comp)));
2753 end if;
2754
2755 -- Key expression must have the type of the key. We analyze
2756 -- a copy of the original expression, because it will be
2757 -- reanalyzed and copied as needed during expansion of the
2758 -- corresponding loop.
2759
2760 Key_Expr := Key_Expression (Comp);
2761 Analyze_And_Resolve (New_Copy_Tree (Key_Expr), Key_Type);
2762
2763 elsif Present (Iterator_Specification (Comp)) then
2764 Iter := Copy_Separate_Tree (Iterator_Specification (Comp));
2765 Id_Name := Chars (Defining_Identifier (Comp));
2766 Analyze (Iter);
2767 Typ := Etype (Defining_Identifier (Iter));
2768
2769 else
2770 Choice := First (Discrete_Choices (Comp));
2771
2772 while Present (Choice) loop
2773 Analyze (Choice);
2774
2775 -- Choice can be a subtype name, a range, or an expression
2776
2777 if Is_Entity_Name (Choice)
2778 and then Is_Type (Entity (Choice))
2779 and then Base_Type (Entity (Choice)) = Base_Type (Key_Type)
2780 then
2781 null;
2782
2783 elsif Present (Key_Type) then
2784 Analyze_And_Resolve (Choice, Key_Type);
2785
2786 else
2787 Typ := Etype (Choice); -- assume unique for now
2788 end if;
2789
2790 Next (Choice);
2791 end loop;
2792
2793 Id_Name := Chars (Defining_Identifier (Comp));
2794 end if;
2795
2796 -- Create a scope in which to introduce an index, which is usually
2797 -- visible in the expression for the component, and needed for its
2798 -- analysis.
2799
2800 Id := Make_Defining_Identifier (Sloc (Comp), Id_Name);
2801 Ent := New_Internal_Entity (E_Loop, Current_Scope, Sloc (Comp), 'L');
2802 Set_Etype (Ent, Standard_Void_Type);
2803 Set_Parent (Ent, Parent (Comp));
2804 Push_Scope (Ent);
2805
2806 -- Insert and decorate the loop variable in the current scope.
2807 -- The expression has to be analyzed once the loop variable is
2808 -- directly visible. Mark the variable as referenced to prevent
2809 -- spurious warnings, given that subsequent uses of its name in the
2810 -- expression will reference the internal (synonym) loop variable.
2811
2812 Enter_Name (Id);
2813
2814 if No (Key_Type) then
2815 pragma Assert (Present (Typ));
2816 Set_Etype (Id, Typ);
2817 else
2818 Set_Etype (Id, Key_Type);
2819 end if;
2820
2821 Set_Ekind (Id, E_Variable);
2822 Set_Scope (Id, Ent);
2823 Set_Referenced (Id);
2824
2825 -- Analyze a copy of the expression, to verify legality. We use
2826 -- a copy because the expression will be analyzed anew when the
2827 -- enclosing aggregate is expanded, and the construct is rewritten
2828 -- as a loop with a new index variable.
2829
2830 Expr := New_Copy_Tree (Expression (Comp));
2831 Preanalyze_And_Resolve (Expr, Elmt_Type);
2832 End_Scope;
2833
2834 end Resolve_Iterated_Association;
2835
2836 begin
2837 pragma Assert (Nkind (Asp) = N_Aggregate);
2838
2839 Set_Etype (N, Typ);
2840 Parse_Aspect_Aggregate (Asp,
2841 Empty_Subp, Add_Named_Subp, Add_Unnamed_Subp,
2842 New_Indexed_Subp, Assign_Indexed_Subp);
2843
2844 if Present (Add_Unnamed_Subp)
2845 and then No (New_Indexed_Subp)
2846 then
2847 declare
2848 Elmt_Type : constant Entity_Id :=
2849 Etype (Next_Formal
2850 (First_Formal (Entity (Add_Unnamed_Subp))));
2851 Comp : Node_Id;
2852
2853 begin
2854 if Present (Expressions (N)) then
2855 -- positional aggregate
2856
2857 Comp := First (Expressions (N));
2858 while Present (Comp) loop
2859 Analyze_And_Resolve (Comp, Elmt_Type);
2860 Next (Comp);
2861 end loop;
2862 end if;
2863
2864 -- Empty aggregate, to be replaced by Empty during
2865 -- expansion, or iterated component association.
2866
2867 if Present (Component_Associations (N)) then
2868 declare
2869 Comp : Node_Id := First (Component_Associations (N));
2870 begin
2871 while Present (Comp) loop
2872 if Nkind (Comp) /=
2873 N_Iterated_Component_Association
2874 then
2875 Error_Msg_N ("illegal component association "
2876 & "for unnamed container aggregate", Comp);
2877 return;
2878 else
2879 Resolve_Iterated_Association
2880 (Comp, Empty, Elmt_Type);
2881 end if;
2882
2883 Next (Comp);
2884 end loop;
2885 end;
2886 end if;
2887 end;
2888
2889 elsif Present (Add_Named_Subp) then
2890 declare
2891 -- Retrieves types of container, key, and element from the
2892 -- specified insertion procedure.
2893
2894 Container : constant Entity_Id :=
2895 First_Formal (Entity (Add_Named_Subp));
2896 Key_Type : constant Entity_Id := Etype (Next_Formal (Container));
2897 Elmt_Type : constant Entity_Id :=
2898 Etype (Next_Formal (Next_Formal (Container)));
2899 Comp : Node_Id;
2900 Choice : Node_Id;
2901
2902 begin
2903 Comp := First (Component_Associations (N));
2904 while Present (Comp) loop
2905 if Nkind (Comp) = N_Component_Association then
2906 Choice := First (Choices (Comp));
2907
2908 while Present (Choice) loop
2909 Analyze_And_Resolve (Choice, Key_Type);
2910 if not Is_Static_Expression (Choice) then
2911 Error_Msg_N ("Choice must be static", Choice);
2912 end if;
2913
2914 Next (Choice);
2915 end loop;
2916
2917 Analyze_And_Resolve (Expression (Comp), Elmt_Type);
2918
2919 elsif Nkind (Comp) in
2920 N_Iterated_Component_Association |
2921 N_Iterated_Element_Association
2922 then
2923 Resolve_Iterated_Association
2924 (Comp, Key_Type, Elmt_Type);
2925 end if;
2926
2927 Next (Comp);
2928 end loop;
2929 end;
2930
2931 else
2932 -- Indexed Aggregate. Both positional and indexed component
2933 -- can be present. Choices must be static values or ranges
2934 -- with static bounds.
2935
2936 declare
2937 Container : constant Entity_Id :=
2938 First_Formal (Entity (Assign_Indexed_Subp));
2939 Index_Type : constant Entity_Id := Etype (Next_Formal (Container));
2940 Comp_Type : constant Entity_Id :=
2941 Etype (Next_Formal (Next_Formal (Container)));
2942 Comp : Node_Id;
2943 Choice : Node_Id;
2944
2945 begin
2946 if Present (Expressions (N)) then
2947 Comp := First (Expressions (N));
2948 while Present (Comp) loop
2949 Analyze_And_Resolve (Comp, Comp_Type);
2950 Next (Comp);
2951 end loop;
2952 end if;
2953
2954 if Present (Component_Associations (N)) then
2955 Comp := First (Expressions (N));
2956
2957 while Present (Comp) loop
2958 if Nkind (Comp) = N_Component_Association then
2959 Choice := First (Choices (Comp));
2960
2961 while Present (Choice) loop
2962 Analyze_And_Resolve (Choice, Index_Type);
2963 Next (Choice);
2964 end loop;
2965
2966 Analyze_And_Resolve (Expression (Comp), Comp_Type);
2967
2968 elsif Nkind (Comp) in
2969 N_Iterated_Component_Association |
2970 N_Iterated_Element_Association
2971 then
2972 Resolve_Iterated_Association
2973 (Comp, Index_Type, Comp_Type);
2974 end if;
2975
2976 Next (Comp);
2977 end loop;
2978 end if;
2979 end;
2980 end if;
2981 end Resolve_Container_Aggregate;
2982
2983 -----------------------------
2984 -- Resolve_Delta_Aggregate --
2985 -----------------------------
2986
2987 procedure Resolve_Delta_Aggregate (N : Node_Id; Typ : Entity_Id) is
2988 Base : constant Node_Id := Expression (N);
2989
2990 begin
2991 if Ada_Version < Ada_2020 then
2992 Error_Msg_N ("delta_aggregate is an Ada 202x feature", N);
2993 Error_Msg_N ("\compile with -gnat2020", N);
2994 end if;
2995
2996 if not Is_Composite_Type (Typ) then
2997 Error_Msg_N ("not a composite type", N);
2998 end if;
2999
3000 Analyze_And_Resolve (Base, Typ);
3001
3002 if Is_Array_Type (Typ) then
3003 Resolve_Delta_Array_Aggregate (N, Typ);
3004 else
3005 Resolve_Delta_Record_Aggregate (N, Typ);
3006 end if;
3007
3008 Set_Etype (N, Typ);
3009 end Resolve_Delta_Aggregate;
3010
3011 -----------------------------------
3012 -- Resolve_Delta_Array_Aggregate --
3013 -----------------------------------
3014
3015 procedure Resolve_Delta_Array_Aggregate (N : Node_Id; Typ : Entity_Id) is
3016 Deltas : constant List_Id := Component_Associations (N);
3017 Index_Type : constant Entity_Id := Etype (First_Index (Typ));
3018
3019 Assoc : Node_Id;
3020 Choice : Node_Id;
3021
3022 begin
3023 Assoc := First (Deltas);
3024 while Present (Assoc) loop
3025 if Nkind (Assoc) = N_Iterated_Component_Association then
3026 Choice := First (Choice_List (Assoc));
3027 while Present (Choice) loop
3028 if Nkind (Choice) = N_Others_Choice then
3029 Error_Msg_N
3030 ("others not allowed in delta aggregate", Choice);
3031
3032 else
3033 Analyze_And_Resolve (Choice, Index_Type);
3034 end if;
3035
3036 Next (Choice);
3037 end loop;
3038
3039 declare
3040 Id : constant Entity_Id := Defining_Identifier (Assoc);
3041 Ent : constant Entity_Id :=
3042 New_Internal_Entity
3043 (E_Loop, Current_Scope, Sloc (Assoc), 'L');
3044
3045 begin
3046 Set_Etype (Ent, Standard_Void_Type);
3047 Set_Parent (Ent, Assoc);
3048
3049 if No (Scope (Id)) then
3050 Enter_Name (Id);
3051 Set_Etype (Id, Index_Type);
3052 Set_Ekind (Id, E_Variable);
3053 Set_Scope (Id, Ent);
3054 end if;
3055
3056 Push_Scope (Ent);
3057 Analyze_And_Resolve
3058 (New_Copy_Tree (Expression (Assoc)), Component_Type (Typ));
3059 End_Scope;
3060 end;
3061
3062 else
3063 Choice := First (Choice_List (Assoc));
3064 while Present (Choice) loop
3065 if Nkind (Choice) = N_Others_Choice then
3066 Error_Msg_N
3067 ("others not allowed in delta aggregate", Choice);
3068
3069 else
3070 Analyze (Choice);
3071
3072 if Is_Entity_Name (Choice)
3073 and then Is_Type (Entity (Choice))
3074 then
3075 -- Choice covers a range of values
3076
3077 if Base_Type (Entity (Choice)) /=
3078 Base_Type (Index_Type)
3079 then
3080 Error_Msg_NE
3081 ("choice does mat match index type of",
3082 Choice, Typ);
3083 end if;
3084 else
3085 Resolve (Choice, Index_Type);
3086 end if;
3087 end if;
3088
3089 Next (Choice);
3090 end loop;
3091
3092 Analyze_And_Resolve (Expression (Assoc), Component_Type (Typ));
3093 end if;
3094
3095 Next (Assoc);
3096 end loop;
3097 end Resolve_Delta_Array_Aggregate;
3098
3099 ------------------------------------
3100 -- Resolve_Delta_Record_Aggregate --
3101 ------------------------------------
3102
3103 procedure Resolve_Delta_Record_Aggregate (N : Node_Id; Typ : Entity_Id) is
3104
3105 -- Variables used to verify that discriminant-dependent components
3106 -- appear in the same variant.
3107
3108 Comp_Ref : Entity_Id := Empty; -- init to avoid warning
3109 Variant : Node_Id;
3110
3111 procedure Check_Variant (Id : Entity_Id);
3112 -- If a given component of the delta aggregate appears in a variant
3113 -- part, verify that it is within the same variant as that of previous
3114 -- specified variant components of the delta.
3115
3116 function Get_Component (Nam : Node_Id) return Entity_Id;
3117 -- Locate component with a given name and return it. If none found then
3118 -- report error and return Empty.
3119
3120 function Nested_In (V1 : Node_Id; V2 : Node_Id) return Boolean;
3121 -- Determine whether variant V1 is within variant V2
3122
3123 function Variant_Depth (N : Node_Id) return Integer;
3124 -- Determine the distance of a variant to the enclosing type
3125 -- declaration.
3126
3127 --------------------
3128 -- Check_Variant --
3129 --------------------
3130
3131 procedure Check_Variant (Id : Entity_Id) is
3132 Comp : Entity_Id;
3133 Comp_Variant : Node_Id;
3134
3135 begin
3136 if not Has_Discriminants (Typ) then
3137 return;
3138 end if;
3139
3140 Comp := First_Entity (Typ);
3141 while Present (Comp) loop
3142 exit when Chars (Comp) = Chars (Id);
3143 Next_Component (Comp);
3144 end loop;
3145
3146 -- Find the variant, if any, whose component list includes the
3147 -- component declaration.
3148
3149 Comp_Variant := Parent (Parent (List_Containing (Parent (Comp))));
3150 if Nkind (Comp_Variant) = N_Variant then
3151 if No (Variant) then
3152 Variant := Comp_Variant;
3153 Comp_Ref := Comp;
3154
3155 elsif Variant /= Comp_Variant then
3156 declare
3157 D1 : constant Integer := Variant_Depth (Variant);
3158 D2 : constant Integer := Variant_Depth (Comp_Variant);
3159
3160 begin
3161 if D1 = D2
3162 or else
3163 (D1 > D2 and then not Nested_In (Variant, Comp_Variant))
3164 or else
3165 (D2 > D1 and then not Nested_In (Comp_Variant, Variant))
3166 then
3167 pragma Assert (Present (Comp_Ref));
3168 Error_Msg_Node_2 := Comp_Ref;
3169 Error_Msg_NE
3170 ("& and & appear in different variants", Id, Comp);
3171
3172 -- Otherwise retain the deeper variant for subsequent tests
3173
3174 elsif D2 > D1 then
3175 Variant := Comp_Variant;
3176 end if;
3177 end;
3178 end if;
3179 end if;
3180 end Check_Variant;
3181
3182 -------------------
3183 -- Get_Component --
3184 -------------------
3185
3186 function Get_Component (Nam : Node_Id) return Entity_Id is
3187 Comp : Entity_Id;
3188
3189 begin
3190 Comp := First_Entity (Typ);
3191 while Present (Comp) loop
3192 if Chars (Comp) = Chars (Nam) then
3193 if Ekind (Comp) = E_Discriminant then
3194 Error_Msg_N ("delta cannot apply to discriminant", Nam);
3195 end if;
3196
3197 return Comp;
3198 end if;
3199
3200 Next_Entity (Comp);
3201 end loop;
3202
3203 Error_Msg_NE ("type& has no component with this name", Nam, Typ);
3204 return Empty;
3205 end Get_Component;
3206
3207 ---------------
3208 -- Nested_In --
3209 ---------------
3210
3211 function Nested_In (V1, V2 : Node_Id) return Boolean is
3212 Par : Node_Id;
3213
3214 begin
3215 Par := Parent (V1);
3216 while Nkind (Par) /= N_Full_Type_Declaration loop
3217 if Par = V2 then
3218 return True;
3219 end if;
3220
3221 Par := Parent (Par);
3222 end loop;
3223
3224 return False;
3225 end Nested_In;
3226
3227 -------------------
3228 -- Variant_Depth --
3229 -------------------
3230
3231 function Variant_Depth (N : Node_Id) return Integer is
3232 Depth : Integer;
3233 Par : Node_Id;
3234
3235 begin
3236 Depth := 0;
3237 Par := Parent (N);
3238 while Nkind (Par) /= N_Full_Type_Declaration loop
3239 Depth := Depth + 1;
3240 Par := Parent (Par);
3241 end loop;
3242
3243 return Depth;
3244 end Variant_Depth;
3245
3246 -- Local variables
3247
3248 Deltas : constant List_Id := Component_Associations (N);
3249
3250 Assoc : Node_Id;
3251 Choice : Node_Id;
3252 Comp : Entity_Id;
3253 Comp_Type : Entity_Id := Empty; -- init to avoid warning
3254
3255 -- Start of processing for Resolve_Delta_Record_Aggregate
3256
3257 begin
3258 Variant := Empty;
3259
3260 Assoc := First (Deltas);
3261 while Present (Assoc) loop
3262 Choice := First (Choice_List (Assoc));
3263 while Present (Choice) loop
3264 Comp := Get_Component (Choice);
3265
3266 if Present (Comp) then
3267 Check_Variant (Choice);
3268
3269 Comp_Type := Etype (Comp);
3270
3271 -- Decorate the component reference by setting its entity and
3272 -- type, as otherwise backends like GNATprove would have to
3273 -- rediscover this information by themselves.
3274
3275 Set_Entity (Choice, Comp);
3276 Set_Etype (Choice, Comp_Type);
3277 else
3278 Comp_Type := Any_Type;
3279 end if;
3280
3281 Next (Choice);
3282 end loop;
3283
3284 pragma Assert (Present (Comp_Type));
3285 Analyze_And_Resolve (Expression (Assoc), Comp_Type);
3286 Next (Assoc);
3287 end loop;
3288 end Resolve_Delta_Record_Aggregate;
3289
3290 ---------------------------------
3291 -- Resolve_Extension_Aggregate --
3292 ---------------------------------
3293
3294 -- There are two cases to consider:
3295
3296 -- a) If the ancestor part is a type mark, the components needed are the
3297 -- difference between the components of the expected type and the
3298 -- components of the given type mark.
3299
3300 -- b) If the ancestor part is an expression, it must be unambiguous, and
3301 -- once we have its type we can also compute the needed components as in
3302 -- the previous case. In both cases, if the ancestor type is not the
3303 -- immediate ancestor, we have to build this ancestor recursively.
3304
3305 -- In both cases, discriminants of the ancestor type do not play a role in
3306 -- the resolution of the needed components, because inherited discriminants
3307 -- cannot be used in a type extension. As a result we can compute
3308 -- independently the list of components of the ancestor type and of the
3309 -- expected type.
3310
3311 procedure Resolve_Extension_Aggregate (N : Node_Id; Typ : Entity_Id) is
3312 A : constant Node_Id := Ancestor_Part (N);
3313 A_Type : Entity_Id;
3314 I : Interp_Index;
3315 It : Interp;
3316
3317 function Valid_Limited_Ancestor (Anc : Node_Id) return Boolean;
3318 -- If the type is limited, verify that the ancestor part is a legal
3319 -- expression (aggregate or function call, including 'Input)) that does
3320 -- not require a copy, as specified in 7.5(2).
3321
3322 function Valid_Ancestor_Type return Boolean;
3323 -- Verify that the type of the ancestor part is a non-private ancestor
3324 -- of the expected type, which must be a type extension.
3325
3326 procedure Transform_BIP_Assignment (Typ : Entity_Id);
3327 -- For an extension aggregate whose ancestor part is a build-in-place
3328 -- call returning a nonlimited type, this is used to transform the
3329 -- assignment to the ancestor part to use a temp.
3330
3331 ----------------------------
3332 -- Valid_Limited_Ancestor --
3333 ----------------------------
3334
3335 function Valid_Limited_Ancestor (Anc : Node_Id) return Boolean is
3336 begin
3337 if Is_Entity_Name (Anc) and then Is_Type (Entity (Anc)) then
3338 return True;
3339
3340 -- The ancestor must be a call or an aggregate, but a call may
3341 -- have been expanded into a temporary, so check original node.
3342
3343 elsif Nkind (Anc) in N_Aggregate
3344 | N_Extension_Aggregate
3345 | N_Function_Call
3346 then
3347 return True;
3348
3349 elsif Nkind (Original_Node (Anc)) = N_Function_Call then
3350 return True;
3351
3352 elsif Nkind (Anc) = N_Attribute_Reference
3353 and then Attribute_Name (Anc) = Name_Input
3354 then
3355 return True;
3356
3357 elsif Nkind (Anc) = N_Qualified_Expression then
3358 return Valid_Limited_Ancestor (Expression (Anc));
3359
3360 elsif Nkind (Anc) = N_Raise_Expression then
3361 return True;
3362
3363 else
3364 return False;
3365 end if;
3366 end Valid_Limited_Ancestor;
3367
3368 -------------------------
3369 -- Valid_Ancestor_Type --
3370 -------------------------
3371
3372 function Valid_Ancestor_Type return Boolean is
3373 Imm_Type : Entity_Id;
3374
3375 begin
3376 Imm_Type := Base_Type (Typ);
3377 while Is_Derived_Type (Imm_Type) loop
3378 if Etype (Imm_Type) = Base_Type (A_Type) then
3379 return True;
3380
3381 -- The base type of the parent type may appear as a private
3382 -- extension if it is declared as such in a parent unit of the
3383 -- current one. For consistency of the subsequent analysis use
3384 -- the partial view for the ancestor part.
3385
3386 elsif Is_Private_Type (Etype (Imm_Type))
3387 and then Present (Full_View (Etype (Imm_Type)))
3388 and then Base_Type (A_Type) = Full_View (Etype (Imm_Type))
3389 then
3390 A_Type := Etype (Imm_Type);
3391 return True;
3392
3393 -- The parent type may be a private extension. The aggregate is
3394 -- legal if the type of the aggregate is an extension of it that
3395 -- is not a private extension.
3396
3397 elsif Is_Private_Type (A_Type)
3398 and then not Is_Private_Type (Imm_Type)
3399 and then Present (Full_View (A_Type))
3400 and then Base_Type (Full_View (A_Type)) = Etype (Imm_Type)
3401 then
3402 return True;
3403
3404 -- The parent type may be a raise expression (which is legal in
3405 -- any expression context).
3406
3407 elsif A_Type = Raise_Type then
3408 A_Type := Etype (Imm_Type);
3409 return True;
3410
3411 else
3412 Imm_Type := Etype (Base_Type (Imm_Type));
3413 end if;
3414 end loop;
3415
3416 -- If previous loop did not find a proper ancestor, report error
3417
3418 Error_Msg_NE ("expect ancestor type of &", A, Typ);
3419 return False;
3420 end Valid_Ancestor_Type;
3421
3422 ------------------------------
3423 -- Transform_BIP_Assignment --
3424 ------------------------------
3425
3426 procedure Transform_BIP_Assignment (Typ : Entity_Id) is
3427 Loc : constant Source_Ptr := Sloc (N);
3428 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'Y', A);
3429 Obj_Decl : constant Node_Id :=
3430 Make_Object_Declaration (Loc,
3431 Defining_Identifier => Def_Id,
3432 Constant_Present => True,
3433 Object_Definition => New_Occurrence_Of (Typ, Loc),
3434 Expression => A,
3435 Has_Init_Expression => True);
3436 begin
3437 Set_Etype (Def_Id, Typ);
3438 Set_Ancestor_Part (N, New_Occurrence_Of (Def_Id, Loc));
3439 Insert_Action (N, Obj_Decl);
3440 end Transform_BIP_Assignment;
3441
3442 -- Start of processing for Resolve_Extension_Aggregate
3443
3444 begin
3445 -- Analyze the ancestor part and account for the case where it is a
3446 -- parameterless function call.
3447
3448 Analyze (A);
3449 Check_Parameterless_Call (A);
3450
3451 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
3452
3453 -- AI05-0115: if the ancestor part is a subtype mark, the ancestor
3454 -- must not have unknown discriminants.
3455
3456 if Has_Unknown_Discriminants (Entity (A)) then
3457 Error_Msg_NE
3458 ("aggregate not available for type& whose ancestor "
3459 & "has unknown discriminants", N, Typ);
3460 end if;
3461 end if;
3462
3463 if not Is_Tagged_Type (Typ) then
3464 Error_Msg_N ("type of extension aggregate must be tagged", N);
3465 return;
3466
3467 elsif Is_Limited_Type (Typ) then
3468
3469 -- Ada 2005 (AI-287): Limited aggregates are allowed
3470
3471 if Ada_Version < Ada_2005 then
3472 Error_Msg_N ("aggregate type cannot be limited", N);
3473 Explain_Limited_Type (Typ, N);
3474 return;
3475
3476 elsif Valid_Limited_Ancestor (A) then
3477 null;
3478
3479 else
3480 Error_Msg_N
3481 ("limited ancestor part must be aggregate or function call", A);
3482 end if;
3483
3484 elsif Is_Class_Wide_Type (Typ) then
3485 Error_Msg_N ("aggregate cannot be of a class-wide type", N);
3486 return;
3487 end if;
3488
3489 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
3490 A_Type := Get_Full_View (Entity (A));
3491
3492 if Valid_Ancestor_Type then
3493 Set_Entity (A, A_Type);
3494 Set_Etype (A, A_Type);
3495
3496 Validate_Ancestor_Part (N);
3497 Resolve_Record_Aggregate (N, Typ);
3498 end if;
3499
3500 elsif Nkind (A) /= N_Aggregate then
3501 if Is_Overloaded (A) then
3502 A_Type := Any_Type;
3503
3504 Get_First_Interp (A, I, It);
3505 while Present (It.Typ) loop
3506
3507 -- Consider limited interpretations if Ada 2005 or higher
3508
3509 if Is_Tagged_Type (It.Typ)
3510 and then (Ada_Version >= Ada_2005
3511 or else not Is_Limited_Type (It.Typ))
3512 then
3513 if A_Type /= Any_Type then
3514 Error_Msg_N ("cannot resolve expression", A);
3515 return;
3516 else
3517 A_Type := It.Typ;
3518 end if;
3519 end if;
3520
3521 Get_Next_Interp (I, It);
3522 end loop;
3523
3524 if A_Type = Any_Type then
3525 if Ada_Version >= Ada_2005 then
3526 Error_Msg_N
3527 ("ancestor part must be of a tagged type", A);
3528 else
3529 Error_Msg_N
3530 ("ancestor part must be of a nonlimited tagged type", A);
3531 end if;
3532
3533 return;
3534 end if;
3535
3536 else
3537 A_Type := Etype (A);
3538 end if;
3539
3540 if Valid_Ancestor_Type then
3541 Resolve (A, A_Type);
3542 Check_Unset_Reference (A);
3543 Check_Non_Static_Context (A);
3544
3545 -- The aggregate is illegal if the ancestor expression is a call
3546 -- to a function with a limited unconstrained result, unless the
3547 -- type of the aggregate is a null extension. This restriction
3548 -- was added in AI05-67 to simplify implementation.
3549
3550 if Nkind (A) = N_Function_Call
3551 and then Is_Limited_Type (A_Type)
3552 and then not Is_Null_Extension (Typ)
3553 and then not Is_Constrained (A_Type)
3554 then
3555 Error_Msg_N
3556 ("type of limited ancestor part must be constrained", A);
3557
3558 -- Reject the use of CPP constructors that leave objects partially
3559 -- initialized. For example:
3560
3561 -- type CPP_Root is tagged limited record ...
3562 -- pragma Import (CPP, CPP_Root);
3563
3564 -- type CPP_DT is new CPP_Root and Iface ...
3565 -- pragma Import (CPP, CPP_DT);
3566
3567 -- type Ada_DT is new CPP_DT with ...
3568
3569 -- Obj : Ada_DT := Ada_DT'(New_CPP_Root with others => <>);
3570
3571 -- Using the constructor of CPP_Root the slots of the dispatch
3572 -- table of CPP_DT cannot be set, and the secondary tag of
3573 -- CPP_DT is unknown.
3574
3575 elsif Nkind (A) = N_Function_Call
3576 and then Is_CPP_Constructor_Call (A)
3577 and then Enclosing_CPP_Parent (Typ) /= A_Type
3578 then
3579 Error_Msg_NE
3580 ("??must use 'C'P'P constructor for type &", A,
3581 Enclosing_CPP_Parent (Typ));
3582
3583 -- The following call is not needed if the previous warning
3584 -- is promoted to an error.
3585
3586 Resolve_Record_Aggregate (N, Typ);
3587
3588 elsif Is_Class_Wide_Type (Etype (A))
3589 and then Nkind (Original_Node (A)) = N_Function_Call
3590 then
3591 -- If the ancestor part is a dispatching call, it appears
3592 -- statically to be a legal ancestor, but it yields any member
3593 -- of the class, and it is not possible to determine whether
3594 -- it is an ancestor of the extension aggregate (much less
3595 -- which ancestor). It is not possible to determine the
3596 -- components of the extension part.
3597
3598 -- This check implements AI-306, which in fact was motivated by
3599 -- an AdaCore query to the ARG after this test was added.
3600
3601 Error_Msg_N ("ancestor part must be statically tagged", A);
3602 else
3603 -- We are using the build-in-place protocol, but we can't build
3604 -- in place, because we need to call the function before
3605 -- allocating the aggregate. Could do better for null
3606 -- extensions, and maybe for nondiscriminated types.
3607 -- This is wrong for limited, but those were wrong already.
3608
3609 if not Is_Limited_View (A_Type)
3610 and then Is_Build_In_Place_Function_Call (A)
3611 then
3612 Transform_BIP_Assignment (A_Type);
3613 end if;
3614
3615 Resolve_Record_Aggregate (N, Typ);
3616 end if;
3617 end if;
3618
3619 else
3620 Error_Msg_N ("no unique type for this aggregate", A);
3621 end if;
3622
3623 Check_Function_Writable_Actuals (N);
3624 end Resolve_Extension_Aggregate;
3625
3626 ------------------------------
3627 -- Resolve_Record_Aggregate --
3628 ------------------------------
3629
3630 procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id) is
3631 New_Assoc_List : constant List_Id := New_List;
3632 -- New_Assoc_List is the newly built list of N_Component_Association
3633 -- nodes.
3634
3635 Others_Etype : Entity_Id := Empty;
3636 -- This variable is used to save the Etype of the last record component
3637 -- that takes its value from the others choice. Its purpose is:
3638 --
3639 -- (a) make sure the others choice is useful
3640 --
3641 -- (b) make sure the type of all the components whose value is
3642 -- subsumed by the others choice are the same.
3643 --
3644 -- This variable is updated as a side effect of function Get_Value.
3645
3646 Box_Node : Node_Id := Empty;
3647 Is_Box_Present : Boolean := False;
3648 Others_Box : Natural := 0;
3649 -- Ada 2005 (AI-287): Variables used in case of default initialization
3650 -- to provide a functionality similar to Others_Etype. Box_Present
3651 -- indicates that the component takes its default initialization;
3652 -- Others_Box counts the number of components of the current aggregate
3653 -- (which may be a sub-aggregate of a larger one) that are default-
3654 -- initialized. A value of One indicates that an others_box is present.
3655 -- Any larger value indicates that the others_box is not redundant.
3656 -- These variables, similar to Others_Etype, are also updated as a side
3657 -- effect of function Get_Value. Box_Node is used to place a warning on
3658 -- a redundant others_box.
3659
3660 procedure Add_Association
3661 (Component : Entity_Id;
3662 Expr : Node_Id;
3663 Assoc_List : List_Id;
3664 Is_Box_Present : Boolean := False);
3665 -- Builds a new N_Component_Association node which associates Component
3666 -- to expression Expr and adds it to the association list being built,
3667 -- either New_Assoc_List, or the association being built for an inner
3668 -- aggregate.
3669
3670 procedure Add_Discriminant_Values
3671 (New_Aggr : Node_Id;
3672 Assoc_List : List_Id);
3673 -- The constraint to a component may be given by a discriminant of the
3674 -- enclosing type, in which case we have to retrieve its value, which is
3675 -- part of the enclosing aggregate. Assoc_List provides the discriminant
3676 -- associations of the current type or of some enclosing record.
3677
3678 function Discriminant_Present (Input_Discr : Entity_Id) return Boolean;
3679 -- If aggregate N is a regular aggregate this routine will return True.
3680 -- Otherwise, if N is an extension aggregate, then Input_Discr denotes
3681 -- a discriminant whose value may already have been specified by N's
3682 -- ancestor part. This routine checks whether this is indeed the case
3683 -- and if so returns False, signaling that no value for Input_Discr
3684 -- should appear in N's aggregate part. Also, in this case, the routine
3685 -- appends to New_Assoc_List the discriminant value specified in the
3686 -- ancestor part.
3687 --
3688 -- If the aggregate is in a context with expansion delayed, it will be
3689 -- reanalyzed. The inherited discriminant values must not be reinserted
3690 -- in the component list to prevent spurious errors, but they must be
3691 -- present on first analysis to build the proper subtype indications.
3692 -- The flag Inherited_Discriminant is used to prevent the re-insertion.
3693
3694 function Find_Private_Ancestor (Typ : Entity_Id) return Entity_Id;
3695 -- AI05-0115: Find earlier ancestor in the derivation chain that is
3696 -- derived from private view Typ. Whether the aggregate is legal depends
3697 -- on the current visibility of the type as well as that of the parent
3698 -- of the ancestor.
3699
3700 function Get_Value
3701 (Compon : Entity_Id;
3702 From : List_Id;
3703 Consider_Others_Choice : Boolean := False) return Node_Id;
3704 -- Given a record component stored in parameter Compon, this function
3705 -- returns its value as it appears in the list From, which is a list
3706 -- of N_Component_Association nodes.
3707 --
3708 -- If no component association has a choice for the searched component,
3709 -- the value provided by the others choice is returned, if there is one,
3710 -- and Consider_Others_Choice is set to true. Otherwise Empty is
3711 -- returned. If there is more than one component association giving a
3712 -- value for the searched record component, an error message is emitted
3713 -- and the first found value is returned.
3714 --
3715 -- If Consider_Others_Choice is set and the returned expression comes
3716 -- from the others choice, then Others_Etype is set as a side effect.
3717 -- An error message is emitted if the components taking their value from
3718 -- the others choice do not have same type.
3719
3720 procedure Propagate_Discriminants
3721 (Aggr : Node_Id;
3722 Assoc_List : List_Id);
3723 -- Nested components may themselves be discriminated types constrained
3724 -- by outer discriminants, whose values must be captured before the
3725 -- aggregate is expanded into assignments.
3726
3727 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Entity_Id);
3728 -- Analyzes and resolves expression Expr against the Etype of the
3729 -- Component. This routine also applies all appropriate checks to Expr.
3730 -- It finally saves a Expr in the newly created association list that
3731 -- will be attached to the final record aggregate. Note that if the
3732 -- Parent pointer of Expr is not set then Expr was produced with a
3733 -- New_Copy_Tree or some such.
3734
3735 procedure Rewrite_Range (Root_Type : Entity_Id; Rge : Node_Id);
3736 -- Rewrite a range node Rge when its bounds refer to non-stored
3737 -- discriminants from Root_Type, to replace them with the stored
3738 -- discriminant values. This is required in GNATprove mode, and is
3739 -- adopted in all modes to avoid special-casing GNATprove mode.
3740
3741 ---------------------
3742 -- Add_Association --
3743 ---------------------
3744
3745 procedure Add_Association
3746 (Component : Entity_Id;
3747 Expr : Node_Id;
3748 Assoc_List : List_Id;
3749 Is_Box_Present : Boolean := False)
3750 is
3751 Choice_List : constant List_Id := New_List;
3752 Loc : Source_Ptr;
3753
3754 begin
3755 -- If this is a box association the expression is missing, so use the
3756 -- Sloc of the aggregate itself for the new association.
3757
3758 pragma Assert (Present (Expr) xor Is_Box_Present);
3759
3760 if Present (Expr) then
3761 Loc := Sloc (Expr);
3762 else
3763 Loc := Sloc (N);
3764 end if;
3765
3766 Append_To (Choice_List, New_Occurrence_Of (Component, Loc));
3767
3768 Append_To (Assoc_List,
3769 Make_Component_Association (Loc,
3770 Choices => Choice_List,
3771 Expression => Expr,
3772 Box_Present => Is_Box_Present));
3773 end Add_Association;
3774
3775 -----------------------------
3776 -- Add_Discriminant_Values --
3777 -----------------------------
3778
3779 procedure Add_Discriminant_Values
3780 (New_Aggr : Node_Id;
3781 Assoc_List : List_Id)
3782 is
3783 Assoc : Node_Id;
3784 Discr : Entity_Id;
3785 Discr_Elmt : Elmt_Id;
3786 Discr_Val : Node_Id;
3787 Val : Entity_Id;
3788
3789 begin
3790 Discr := First_Discriminant (Etype (New_Aggr));
3791 Discr_Elmt := First_Elmt (Discriminant_Constraint (Etype (New_Aggr)));
3792 while Present (Discr_Elmt) loop
3793 Discr_Val := Node (Discr_Elmt);
3794
3795 -- If the constraint is given by a discriminant then it is a
3796 -- discriminant of an enclosing record, and its value has already
3797 -- been placed in the association list.
3798
3799 if Is_Entity_Name (Discr_Val)
3800 and then Ekind (Entity (Discr_Val)) = E_Discriminant
3801 then
3802 Val := Entity (Discr_Val);
3803
3804 Assoc := First (Assoc_List);
3805 while Present (Assoc) loop
3806 if Present (Entity (First (Choices (Assoc))))
3807 and then Entity (First (Choices (Assoc))) = Val
3808 then
3809 Discr_Val := Expression (Assoc);
3810 exit;
3811 end if;
3812
3813 Next (Assoc);
3814 end loop;
3815 end if;
3816
3817 Add_Association
3818 (Discr, New_Copy_Tree (Discr_Val),
3819 Component_Associations (New_Aggr));
3820
3821 -- If the discriminant constraint is a current instance, mark the
3822 -- current aggregate so that the self-reference can be expanded
3823 -- later. The constraint may refer to the subtype of aggregate, so
3824 -- use base type for comparison.
3825
3826 if Nkind (Discr_Val) = N_Attribute_Reference
3827 and then Is_Entity_Name (Prefix (Discr_Val))
3828 and then Is_Type (Entity (Prefix (Discr_Val)))
3829 and then Base_Type (Etype (N)) = Entity (Prefix (Discr_Val))
3830 then
3831 Set_Has_Self_Reference (N);
3832 end if;
3833
3834 Next_Elmt (Discr_Elmt);
3835 Next_Discriminant (Discr);
3836 end loop;
3837 end Add_Discriminant_Values;
3838
3839 --------------------------
3840 -- Discriminant_Present --
3841 --------------------------
3842
3843 function Discriminant_Present (Input_Discr : Entity_Id) return Boolean is
3844 Regular_Aggr : constant Boolean := Nkind (N) /= N_Extension_Aggregate;
3845
3846 Ancestor_Is_Subtyp : Boolean;
3847
3848 Loc : Source_Ptr;
3849
3850 Ancestor : Node_Id;
3851 Ancestor_Typ : Entity_Id;
3852 Comp_Assoc : Node_Id;
3853 Discr : Entity_Id;
3854 Discr_Expr : Node_Id;
3855 Discr_Val : Elmt_Id := No_Elmt;
3856 Orig_Discr : Entity_Id;
3857
3858 begin
3859 if Regular_Aggr then
3860 return True;
3861 end if;
3862
3863 -- Check whether inherited discriminant values have already been
3864 -- inserted in the aggregate. This will be the case if we are
3865 -- re-analyzing an aggregate whose expansion was delayed.
3866
3867 if Present (Component_Associations (N)) then
3868 Comp_Assoc := First (Component_Associations (N));
3869 while Present (Comp_Assoc) loop
3870 if Inherited_Discriminant (Comp_Assoc) then
3871 return True;
3872 end if;
3873
3874 Next (Comp_Assoc);
3875 end loop;
3876 end if;
3877
3878 Ancestor := Ancestor_Part (N);
3879 Ancestor_Typ := Etype (Ancestor);
3880 Loc := Sloc (Ancestor);
3881
3882 -- For a private type with unknown discriminants, use the underlying
3883 -- record view if it is available.
3884
3885 if Has_Unknown_Discriminants (Ancestor_Typ)
3886 and then Present (Full_View (Ancestor_Typ))
3887 and then Present (Underlying_Record_View (Full_View (Ancestor_Typ)))
3888 then
3889 Ancestor_Typ := Underlying_Record_View (Full_View (Ancestor_Typ));
3890 end if;
3891
3892 Ancestor_Is_Subtyp :=
3893 Is_Entity_Name (Ancestor) and then Is_Type (Entity (Ancestor));
3894
3895 -- If the ancestor part has no discriminants clearly N's aggregate
3896 -- part must provide a value for Discr.
3897
3898 if not Has_Discriminants (Ancestor_Typ) then
3899 return True;
3900
3901 -- If the ancestor part is an unconstrained subtype mark then the
3902 -- Discr must be present in N's aggregate part.
3903
3904 elsif Ancestor_Is_Subtyp
3905 and then not Is_Constrained (Entity (Ancestor))
3906 then
3907 return True;
3908 end if;
3909
3910 -- Now look to see if Discr was specified in the ancestor part
3911
3912 if Ancestor_Is_Subtyp then
3913 Discr_Val :=
3914 First_Elmt (Discriminant_Constraint (Entity (Ancestor)));
3915 end if;
3916
3917 Orig_Discr := Original_Record_Component (Input_Discr);
3918
3919 Discr := First_Discriminant (Ancestor_Typ);
3920 while Present (Discr) loop
3921
3922 -- If Ancestor has already specified Disc value then insert its
3923 -- value in the final aggregate.
3924
3925 if Original_Record_Component (Discr) = Orig_Discr then
3926 if Ancestor_Is_Subtyp then
3927 Discr_Expr := New_Copy_Tree (Node (Discr_Val));
3928 else
3929 Discr_Expr :=
3930 Make_Selected_Component (Loc,
3931 Prefix => Duplicate_Subexpr (Ancestor),
3932 Selector_Name => New_Occurrence_Of (Input_Discr, Loc));
3933 end if;
3934
3935 Resolve_Aggr_Expr (Discr_Expr, Input_Discr);
3936 Set_Inherited_Discriminant (Last (New_Assoc_List));
3937 return False;
3938 end if;
3939
3940 Next_Discriminant (Discr);
3941
3942 if Ancestor_Is_Subtyp then
3943 Next_Elmt (Discr_Val);
3944 end if;
3945 end loop;
3946
3947 return True;
3948 end Discriminant_Present;
3949
3950 ---------------------------
3951 -- Find_Private_Ancestor --
3952 ---------------------------
3953
3954 function Find_Private_Ancestor (Typ : Entity_Id) return Entity_Id is
3955 Par : Entity_Id;
3956
3957 begin
3958 Par := Typ;
3959 loop
3960 if Has_Private_Ancestor (Par)
3961 and then not Has_Private_Ancestor (Etype (Base_Type (Par)))
3962 then
3963 return Par;
3964
3965 elsif not Is_Derived_Type (Par) then
3966 return Empty;
3967
3968 else
3969 Par := Etype (Base_Type (Par));
3970 end if;
3971 end loop;
3972 end Find_Private_Ancestor;
3973
3974 ---------------
3975 -- Get_Value --
3976 ---------------
3977
3978 function Get_Value
3979 (Compon : Entity_Id;
3980 From : List_Id;
3981 Consider_Others_Choice : Boolean := False) return Node_Id
3982 is
3983 Typ : constant Entity_Id := Etype (Compon);
3984 Assoc : Node_Id;
3985 Expr : Node_Id := Empty;
3986 Selector_Name : Node_Id;
3987
3988 begin
3989 Is_Box_Present := False;
3990
3991 if No (From) then
3992 return Empty;
3993 end if;
3994
3995 Assoc := First (From);
3996 while Present (Assoc) loop
3997 Selector_Name := First (Choices (Assoc));
3998 while Present (Selector_Name) loop
3999 if Nkind (Selector_Name) = N_Others_Choice then
4000 if Consider_Others_Choice and then No (Expr) then
4001
4002 -- We need to duplicate the expression for each
4003 -- successive component covered by the others choice.
4004 -- This is redundant if the others_choice covers only
4005 -- one component (small optimization possible???), but
4006 -- indispensable otherwise, because each one must be
4007 -- expanded individually to preserve side effects.
4008
4009 -- Ada 2005 (AI-287): In case of default initialization
4010 -- of components, we duplicate the corresponding default
4011 -- expression (from the record type declaration). The
4012 -- copy must carry the sloc of the association (not the
4013 -- original expression) to prevent spurious elaboration
4014 -- checks when the default includes function calls.
4015
4016 if Box_Present (Assoc) then
4017 Others_Box := Others_Box + 1;
4018 Is_Box_Present := True;
4019
4020 if Expander_Active then
4021 return
4022 New_Copy_Tree_And_Copy_Dimensions
4023 (Expression (Parent (Compon)),
4024 New_Sloc => Sloc (Assoc));
4025 else
4026 return Expression (Parent (Compon));
4027 end if;
4028
4029 else
4030 if Present (Others_Etype)
4031 and then Base_Type (Others_Etype) /= Base_Type (Typ)
4032 then
4033 -- If the components are of an anonymous access
4034 -- type they are distinct, but this is legal in
4035 -- Ada 2012 as long as designated types match.
4036
4037 if (Ekind (Typ) = E_Anonymous_Access_Type
4038 or else Ekind (Typ) =
4039 E_Anonymous_Access_Subprogram_Type)
4040 and then Designated_Type (Typ) =
4041 Designated_Type (Others_Etype)
4042 then
4043 null;
4044 else
4045 Error_Msg_N
4046 ("components in OTHERS choice must have same "
4047 & "type", Selector_Name);
4048 end if;
4049 end if;
4050
4051 Others_Etype := Typ;
4052
4053 -- Copy the expression so that it is resolved
4054 -- independently for each component, This is needed
4055 -- for accessibility checks on components of anonymous
4056 -- access types, even in compile_only mode.
4057
4058 if not Inside_A_Generic then
4059 return
4060 New_Copy_Tree_And_Copy_Dimensions
4061 (Expression (Assoc));
4062 else
4063 return Expression (Assoc);
4064 end if;
4065 end if;
4066 end if;
4067
4068 elsif Chars (Compon) = Chars (Selector_Name) then
4069 if No (Expr) then
4070
4071 -- Ada 2005 (AI-231)
4072
4073 if Ada_Version >= Ada_2005
4074 and then Known_Null (Expression (Assoc))
4075 then
4076 Check_Can_Never_Be_Null (Compon, Expression (Assoc));
4077 end if;
4078
4079 -- We need to duplicate the expression when several
4080 -- components are grouped together with a "|" choice.
4081 -- For instance "filed1 | filed2 => Expr"
4082
4083 -- Ada 2005 (AI-287)
4084
4085 if Box_Present (Assoc) then
4086 Is_Box_Present := True;
4087
4088 -- Duplicate the default expression of the component
4089 -- from the record type declaration, so a new copy
4090 -- can be attached to the association.
4091
4092 -- Note that we always copy the default expression,
4093 -- even when the association has a single choice, in
4094 -- order to create a proper association for the
4095 -- expanded aggregate.
4096
4097 -- Component may have no default, in which case the
4098 -- expression is empty and the component is default-
4099 -- initialized, but an association for the component
4100 -- exists, and it is not covered by an others clause.
4101
4102 -- Scalar and private types have no initialization
4103 -- procedure, so they remain uninitialized. If the
4104 -- target of the aggregate is a constant this
4105 -- deserves a warning.
4106
4107 if No (Expression (Parent (Compon)))
4108 and then not Has_Non_Null_Base_Init_Proc (Typ)
4109 and then not Has_Aspect (Typ, Aspect_Default_Value)
4110 and then not Is_Concurrent_Type (Typ)
4111 and then Nkind (Parent (N)) = N_Object_Declaration
4112 and then Constant_Present (Parent (N))
4113 then
4114 Error_Msg_Node_2 := Typ;
4115 Error_Msg_NE
4116 ("component&? of type& is uninitialized",
4117 Assoc, Selector_Name);
4118
4119 -- An additional reminder if the component type
4120 -- is a generic formal.
4121
4122 if Is_Generic_Type (Base_Type (Typ)) then
4123 Error_Msg_NE
4124 ("\instance should provide actual type with "
4125 & "initialization for&", Assoc, Typ);
4126 end if;
4127 end if;
4128
4129 return
4130 New_Copy_Tree_And_Copy_Dimensions
4131 (Expression (Parent (Compon)));
4132
4133 else
4134 if Present (Next (Selector_Name)) then
4135 Expr := New_Copy_Tree_And_Copy_Dimensions
4136 (Expression (Assoc));
4137 else
4138 Expr := Expression (Assoc);
4139 end if;
4140 end if;
4141
4142 Generate_Reference (Compon, Selector_Name, 'm');
4143
4144 else
4145 Error_Msg_NE
4146 ("more than one value supplied for &",
4147 Selector_Name, Compon);
4148
4149 end if;
4150 end if;
4151
4152 Next (Selector_Name);
4153 end loop;
4154
4155 Next (Assoc);
4156 end loop;
4157
4158 return Expr;
4159 end Get_Value;
4160
4161 -----------------------------
4162 -- Propagate_Discriminants --
4163 -----------------------------
4164
4165 procedure Propagate_Discriminants
4166 (Aggr : Node_Id;
4167 Assoc_List : List_Id)
4168 is
4169 Loc : constant Source_Ptr := Sloc (N);
4170
4171 procedure Process_Component (Comp : Entity_Id);
4172 -- Add one component with a box association to the inner aggregate,
4173 -- and recurse if component is itself composite.
4174
4175 -----------------------
4176 -- Process_Component --
4177 -----------------------
4178
4179 procedure Process_Component (Comp : Entity_Id) is
4180 T : constant Entity_Id := Etype (Comp);
4181 New_Aggr : Node_Id;
4182
4183 begin
4184 if Is_Record_Type (T) and then Has_Discriminants (T) then
4185 New_Aggr := Make_Aggregate (Loc, No_List, New_List);
4186 Set_Etype (New_Aggr, T);
4187
4188 Add_Association
4189 (Comp, New_Aggr, Component_Associations (Aggr));
4190
4191 -- Collect discriminant values and recurse
4192
4193 Add_Discriminant_Values (New_Aggr, Assoc_List);
4194 Propagate_Discriminants (New_Aggr, Assoc_List);
4195
4196 Build_Constrained_Itype
4197 (New_Aggr, T, Component_Associations (New_Aggr));
4198 else
4199 Add_Association
4200 (Comp, Empty, Component_Associations (Aggr),
4201 Is_Box_Present => True);
4202 end if;
4203 end Process_Component;
4204
4205 -- Local variables
4206
4207 Aggr_Type : constant Entity_Id := Base_Type (Etype (Aggr));
4208 Components : constant Elist_Id := New_Elmt_List;
4209 Def_Node : constant Node_Id :=
4210 Type_Definition (Declaration_Node (Aggr_Type));
4211
4212 Comp : Node_Id;
4213 Comp_Elmt : Elmt_Id;
4214 Errors : Boolean;
4215
4216 -- Start of processing for Propagate_Discriminants
4217
4218 begin
4219 -- The component type may be a variant type. Collect the components
4220 -- that are ruled by the known values of the discriminants. Their
4221 -- values have already been inserted into the component list of the
4222 -- current aggregate.
4223
4224 if Nkind (Def_Node) = N_Record_Definition
4225 and then Present (Component_List (Def_Node))
4226 and then Present (Variant_Part (Component_List (Def_Node)))
4227 then
4228 Gather_Components (Aggr_Type,
4229 Component_List (Def_Node),
4230 Governed_By => Component_Associations (Aggr),
4231 Into => Components,
4232 Report_Errors => Errors);
4233
4234 Comp_Elmt := First_Elmt (Components);
4235 while Present (Comp_Elmt) loop
4236 if Ekind (Node (Comp_Elmt)) /= E_Discriminant then
4237 Process_Component (Node (Comp_Elmt));
4238 end if;
4239
4240 Next_Elmt (Comp_Elmt);
4241 end loop;
4242
4243 -- No variant part, iterate over all components
4244
4245 else
4246 Comp := First_Component (Etype (Aggr));
4247 while Present (Comp) loop
4248 Process_Component (Comp);
4249 Next_Component (Comp);
4250 end loop;
4251 end if;
4252 end Propagate_Discriminants;
4253
4254 -----------------------
4255 -- Resolve_Aggr_Expr --
4256 -----------------------
4257
4258 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Entity_Id) is
4259 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean;
4260 -- If the expression is an aggregate (possibly qualified) then its
4261 -- expansion is delayed until the enclosing aggregate is expanded
4262 -- into assignments. In that case, do not generate checks on the
4263 -- expression, because they will be generated later, and will other-
4264 -- wise force a copy (to remove side effects) that would leave a
4265 -- dynamic-sized aggregate in the code, something that gigi cannot
4266 -- handle.
4267
4268 ---------------------------
4269 -- Has_Expansion_Delayed --
4270 ---------------------------
4271
4272 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean is
4273 begin
4274 return
4275 (Nkind (Expr) in N_Aggregate | N_Extension_Aggregate
4276 and then Present (Etype (Expr))
4277 and then Is_Record_Type (Etype (Expr))
4278 and then Expansion_Delayed (Expr))
4279 or else
4280 (Nkind (Expr) = N_Qualified_Expression
4281 and then Has_Expansion_Delayed (Expression (Expr)));
4282 end Has_Expansion_Delayed;
4283
4284 -- Local variables
4285
4286 Expr_Type : Entity_Id := Empty;
4287 New_C : Entity_Id := Component;
4288 New_Expr : Node_Id;
4289
4290 Relocate : Boolean;
4291 -- Set to True if the resolved Expr node needs to be relocated when
4292 -- attached to the newly created association list. This node need not
4293 -- be relocated if its parent pointer is not set. In fact in this
4294 -- case Expr is the output of a New_Copy_Tree call. If Relocate is
4295 -- True then we have analyzed the expression node in the original
4296 -- aggregate and hence it needs to be relocated when moved over to
4297 -- the new association list.
4298
4299 -- Start of processing for Resolve_Aggr_Expr
4300
4301 begin
4302 -- If the type of the component is elementary or the type of the
4303 -- aggregate does not contain discriminants, use the type of the
4304 -- component to resolve Expr.
4305
4306 if Is_Elementary_Type (Etype (Component))
4307 or else not Has_Discriminants (Etype (N))
4308 then
4309 Expr_Type := Etype (Component);
4310
4311 -- Otherwise we have to pick up the new type of the component from
4312 -- the new constrained subtype of the aggregate. In fact components
4313 -- which are of a composite type might be constrained by a
4314 -- discriminant, and we want to resolve Expr against the subtype were
4315 -- all discriminant occurrences are replaced with their actual value.
4316
4317 else
4318 New_C := First_Component (Etype (N));
4319 while Present (New_C) loop
4320 if Chars (New_C) = Chars (Component) then
4321 Expr_Type := Etype (New_C);
4322 exit;
4323 end if;
4324
4325 Next_Component (New_C);
4326 end loop;
4327
4328 pragma Assert (Present (Expr_Type));
4329
4330 -- For each range in an array type where a discriminant has been
4331 -- replaced with the constraint, check that this range is within
4332 -- the range of the base type. This checks is done in the init
4333 -- proc for regular objects, but has to be done here for
4334 -- aggregates since no init proc is called for them.
4335
4336 if Is_Array_Type (Expr_Type) then
4337 declare
4338 Index : Node_Id;
4339 -- Range of the current constrained index in the array
4340
4341 Orig_Index : Node_Id := First_Index (Etype (Component));
4342 -- Range corresponding to the range Index above in the
4343 -- original unconstrained record type. The bounds of this
4344 -- range may be governed by discriminants.
4345
4346 Unconstr_Index : Node_Id := First_Index (Etype (Expr_Type));
4347 -- Range corresponding to the range Index above for the
4348 -- unconstrained array type. This range is needed to apply
4349 -- range checks.
4350
4351 begin
4352 Index := First_Index (Expr_Type);
4353 while Present (Index) loop
4354 if Depends_On_Discriminant (Orig_Index) then
4355 Apply_Range_Check (Index, Etype (Unconstr_Index));
4356 end if;
4357
4358 Next_Index (Index);
4359 Next_Index (Orig_Index);
4360 Next_Index (Unconstr_Index);
4361 end loop;
4362 end;
4363 end if;
4364 end if;
4365
4366 -- If the Parent pointer of Expr is not set, Expr is an expression
4367 -- duplicated by New_Tree_Copy (this happens for record aggregates
4368 -- that look like (Field1 | Filed2 => Expr) or (others => Expr)).
4369 -- Such a duplicated expression must be attached to the tree
4370 -- before analysis and resolution to enforce the rule that a tree
4371 -- fragment should never be analyzed or resolved unless it is
4372 -- attached to the current compilation unit.
4373
4374 if No (Parent (Expr)) then
4375 Set_Parent (Expr, N);
4376 Relocate := False;
4377 else
4378 Relocate := True;
4379 end if;
4380
4381 Analyze_And_Resolve (Expr, Expr_Type);
4382 Check_Expr_OK_In_Limited_Aggregate (Expr);
4383 Check_Non_Static_Context (Expr);
4384 Check_Unset_Reference (Expr);
4385
4386 -- Check wrong use of class-wide types
4387
4388 if Is_Class_Wide_Type (Etype (Expr)) then
4389 Error_Msg_N ("dynamically tagged expression not allowed", Expr);
4390 end if;
4391
4392 if not Has_Expansion_Delayed (Expr) then
4393 Aggregate_Constraint_Checks (Expr, Expr_Type);
4394 end if;
4395
4396 -- If an aggregate component has a type with predicates, an explicit
4397 -- predicate check must be applied, as for an assignment statement,
4398 -- because the aggregate might not be expanded into individual
4399 -- component assignments.
4400
4401 if Has_Predicates (Expr_Type)
4402 and then Analyzed (Expr)
4403 then
4404 Apply_Predicate_Check (Expr, Expr_Type);
4405 end if;
4406
4407 if Raises_Constraint_Error (Expr) then
4408 Set_Raises_Constraint_Error (N);
4409 end if;
4410
4411 -- If the expression has been marked as requiring a range check, then
4412 -- generate it here. It's a bit odd to be generating such checks in
4413 -- the analyzer, but harmless since Generate_Range_Check does nothing
4414 -- (other than making sure Do_Range_Check is set) if the expander is
4415 -- not active.
4416
4417 if Do_Range_Check (Expr) then
4418 Generate_Range_Check (Expr, Expr_Type, CE_Range_Check_Failed);
4419 end if;
4420
4421 -- Add association Component => Expr if the caller requests it
4422
4423 if Relocate then
4424 New_Expr := Relocate_Node (Expr);
4425
4426 -- Since New_Expr is not gonna be analyzed later on, we need to
4427 -- propagate here the dimensions form Expr to New_Expr.
4428
4429 Copy_Dimensions (Expr, New_Expr);
4430
4431 else
4432 New_Expr := Expr;
4433 end if;
4434
4435 Add_Association (New_C, New_Expr, New_Assoc_List);
4436 end Resolve_Aggr_Expr;
4437
4438 -------------------
4439 -- Rewrite_Range --
4440 -------------------
4441
4442 procedure Rewrite_Range (Root_Type : Entity_Id; Rge : Node_Id) is
4443 procedure Rewrite_Bound
4444 (Bound : Node_Id;
4445 Disc : Entity_Id;
4446 Expr_Disc : Node_Id);
4447 -- Rewrite a bound of the range Bound, when it is equal to the
4448 -- non-stored discriminant Disc, into the stored discriminant
4449 -- value Expr_Disc.
4450
4451 -------------------
4452 -- Rewrite_Bound --
4453 -------------------
4454
4455 procedure Rewrite_Bound
4456 (Bound : Node_Id;
4457 Disc : Entity_Id;
4458 Expr_Disc : Node_Id)
4459 is
4460 begin
4461 if Nkind (Bound) /= N_Identifier then
4462 return;
4463 end if;
4464
4465 -- We expect either the discriminant or the discriminal
4466
4467 if Entity (Bound) = Disc
4468 or else (Ekind (Entity (Bound)) = E_In_Parameter
4469 and then Discriminal_Link (Entity (Bound)) = Disc)
4470 then
4471 Rewrite (Bound, New_Copy_Tree (Expr_Disc));
4472 end if;
4473 end Rewrite_Bound;
4474
4475 -- Local variables
4476
4477 Low, High : Node_Id;
4478 Disc : Entity_Id;
4479 Expr_Disc : Elmt_Id;
4480
4481 -- Start of processing for Rewrite_Range
4482
4483 begin
4484 if Has_Discriminants (Root_Type) and then Nkind (Rge) = N_Range then
4485 Low := Low_Bound (Rge);
4486 High := High_Bound (Rge);
4487
4488 Disc := First_Discriminant (Root_Type);
4489 Expr_Disc := First_Elmt (Stored_Constraint (Etype (N)));
4490 while Present (Disc) loop
4491 Rewrite_Bound (Low, Disc, Node (Expr_Disc));
4492 Rewrite_Bound (High, Disc, Node (Expr_Disc));
4493 Next_Discriminant (Disc);
4494 Next_Elmt (Expr_Disc);
4495 end loop;
4496 end if;
4497 end Rewrite_Range;
4498
4499 -- Local variables
4500
4501 Components : constant Elist_Id := New_Elmt_List;
4502 -- Components is the list of the record components whose value must be
4503 -- provided in the aggregate. This list does include discriminants.
4504
4505 Component : Entity_Id;
4506 Component_Elmt : Elmt_Id;
4507 Expr : Node_Id;
4508 Positional_Expr : Node_Id;
4509
4510 -- Start of processing for Resolve_Record_Aggregate
4511
4512 begin
4513 -- A record aggregate is restricted in SPARK:
4514
4515 -- Each named association can have only a single choice.
4516 -- OTHERS cannot be used.
4517 -- Positional and named associations cannot be mixed.
4518
4519 if Present (Component_Associations (N))
4520 and then Present (First (Component_Associations (N)))
4521 then
4522 declare
4523 Assoc : Node_Id;
4524
4525 begin
4526 Assoc := First (Component_Associations (N));
4527 while Present (Assoc) loop
4528 if Nkind (Assoc) = N_Iterated_Component_Association then
4529 Error_Msg_N
4530 ("iterated component association can only appear in an "
4531 & "array aggregate", N);
4532 raise Unrecoverable_Error;
4533 end if;
4534
4535 Next (Assoc);
4536 end loop;
4537 end;
4538 end if;
4539
4540 -- We may end up calling Duplicate_Subexpr on expressions that are
4541 -- attached to New_Assoc_List. For this reason we need to attach it
4542 -- to the tree by setting its parent pointer to N. This parent point
4543 -- will change in STEP 8 below.
4544
4545 Set_Parent (New_Assoc_List, N);
4546
4547 -- STEP 1: abstract type and null record verification
4548
4549 if Is_Abstract_Type (Typ) then
4550 Error_Msg_N ("type of aggregate cannot be abstract", N);
4551 end if;
4552
4553 if No (First_Entity (Typ)) and then Null_Record_Present (N) then
4554 Set_Etype (N, Typ);
4555 return;
4556
4557 elsif Present (First_Entity (Typ))
4558 and then Null_Record_Present (N)
4559 and then not Is_Tagged_Type (Typ)
4560 then
4561 Error_Msg_N ("record aggregate cannot be null", N);
4562 return;
4563
4564 -- If the type has no components, then the aggregate should either
4565 -- have "null record", or in Ada 2005 it could instead have a single
4566 -- component association given by "others => <>". For Ada 95 we flag an
4567 -- error at this point, but for Ada 2005 we proceed with checking the
4568 -- associations below, which will catch the case where it's not an
4569 -- aggregate with "others => <>". Note that the legality of a <>
4570 -- aggregate for a null record type was established by AI05-016.
4571
4572 elsif No (First_Entity (Typ))
4573 and then Ada_Version < Ada_2005
4574 then
4575 Error_Msg_N ("record aggregate must be null", N);
4576 return;
4577 end if;
4578
4579 -- STEP 2: Verify aggregate structure
4580
4581 Step_2 : declare
4582 Assoc : Node_Id;
4583 Bad_Aggregate : Boolean := False;
4584 Selector_Name : Node_Id;
4585
4586 begin
4587 if Present (Component_Associations (N)) then
4588 Assoc := First (Component_Associations (N));
4589 else
4590 Assoc := Empty;
4591 end if;
4592
4593 while Present (Assoc) loop
4594 Selector_Name := First (Choices (Assoc));
4595 while Present (Selector_Name) loop
4596 if Nkind (Selector_Name) = N_Identifier then
4597 null;
4598
4599 elsif Nkind (Selector_Name) = N_Others_Choice then
4600 if Selector_Name /= First (Choices (Assoc))
4601 or else Present (Next (Selector_Name))
4602 then
4603 Error_Msg_N
4604 ("OTHERS must appear alone in a choice list",
4605 Selector_Name);
4606 return;
4607
4608 elsif Present (Next (Assoc)) then
4609 Error_Msg_N
4610 ("OTHERS must appear last in an aggregate",
4611 Selector_Name);
4612 return;
4613
4614 -- (Ada 2005): If this is an association with a box,
4615 -- indicate that the association need not represent
4616 -- any component.
4617
4618 elsif Box_Present (Assoc) then
4619 Others_Box := 1;
4620 Box_Node := Assoc;
4621 end if;
4622
4623 else
4624 Error_Msg_N
4625 ("selector name should be identifier or OTHERS",
4626 Selector_Name);
4627 Bad_Aggregate := True;
4628 end if;
4629
4630 Next (Selector_Name);
4631 end loop;
4632
4633 Next (Assoc);
4634 end loop;
4635
4636 if Bad_Aggregate then
4637 return;
4638 end if;
4639 end Step_2;
4640
4641 -- STEP 3: Find discriminant Values
4642
4643 Step_3 : declare
4644 Discrim : Entity_Id;
4645 Missing_Discriminants : Boolean := False;
4646
4647 begin
4648 if Present (Expressions (N)) then
4649 Positional_Expr := First (Expressions (N));
4650 else
4651 Positional_Expr := Empty;
4652 end if;
4653
4654 -- AI05-0115: if the ancestor part is a subtype mark, the ancestor
4655 -- must not have unknown discriminants.
4656 -- ??? We are not checking any subtype mark here and this code is not
4657 -- exercised by any test, so it's likely wrong (in particular
4658 -- we should not use Root_Type here but the subtype mark, if any),
4659 -- and possibly not needed.
4660
4661 if Is_Derived_Type (Typ)
4662 and then Has_Unknown_Discriminants (Root_Type (Typ))
4663 and then Nkind (N) /= N_Extension_Aggregate
4664 then
4665 Error_Msg_NE
4666 ("aggregate not available for type& whose ancestor "
4667 & "has unknown discriminants ", N, Typ);
4668 end if;
4669
4670 if Has_Unknown_Discriminants (Typ)
4671 and then Present (Underlying_Record_View (Typ))
4672 then
4673 Discrim := First_Discriminant (Underlying_Record_View (Typ));
4674 elsif Has_Discriminants (Typ) then
4675 Discrim := First_Discriminant (Typ);
4676 else
4677 Discrim := Empty;
4678 end if;
4679
4680 -- First find the discriminant values in the positional components
4681
4682 while Present (Discrim) and then Present (Positional_Expr) loop
4683 if Discriminant_Present (Discrim) then
4684 Resolve_Aggr_Expr (Positional_Expr, Discrim);
4685
4686 -- Ada 2005 (AI-231)
4687
4688 if Ada_Version >= Ada_2005
4689 and then Known_Null (Positional_Expr)
4690 then
4691 Check_Can_Never_Be_Null (Discrim, Positional_Expr);
4692 end if;
4693
4694 Next (Positional_Expr);
4695 end if;
4696
4697 if Present (Get_Value (Discrim, Component_Associations (N))) then
4698 Error_Msg_NE
4699 ("more than one value supplied for discriminant&",
4700 N, Discrim);
4701 end if;
4702
4703 Next_Discriminant (Discrim);
4704 end loop;
4705
4706 -- Find remaining discriminant values if any among named components
4707
4708 while Present (Discrim) loop
4709 Expr := Get_Value (Discrim, Component_Associations (N), True);
4710
4711 if not Discriminant_Present (Discrim) then
4712 if Present (Expr) then
4713 Error_Msg_NE
4714 ("more than one value supplied for discriminant &",
4715 N, Discrim);
4716 end if;
4717
4718 elsif No (Expr) then
4719 Error_Msg_NE
4720 ("no value supplied for discriminant &", N, Discrim);
4721 Missing_Discriminants := True;
4722
4723 else
4724 Resolve_Aggr_Expr (Expr, Discrim);
4725 end if;
4726
4727 Next_Discriminant (Discrim);
4728 end loop;
4729
4730 if Missing_Discriminants then
4731 return;
4732 end if;
4733
4734 -- At this point and until the beginning of STEP 6, New_Assoc_List
4735 -- contains only the discriminants and their values.
4736
4737 end Step_3;
4738
4739 -- STEP 4: Set the Etype of the record aggregate
4740
4741 if Has_Discriminants (Typ)
4742 or else (Has_Unknown_Discriminants (Typ)
4743 and then Present (Underlying_Record_View (Typ)))
4744 then
4745 Build_Constrained_Itype (N, Typ, New_Assoc_List);
4746 else
4747 Set_Etype (N, Typ);
4748 end if;
4749
4750 -- STEP 5: Get remaining components according to discriminant values
4751
4752 Step_5 : declare
4753 Dnode : Node_Id;
4754 Errors_Found : Boolean := False;
4755 Record_Def : Node_Id;
4756 Parent_Typ : Entity_Id;
4757 Parent_Typ_List : Elist_Id;
4758 Parent_Elmt : Elmt_Id;
4759 Root_Typ : Entity_Id;
4760
4761 begin
4762 if Is_Derived_Type (Typ) and then Is_Tagged_Type (Typ) then
4763 Parent_Typ_List := New_Elmt_List;
4764
4765 -- If this is an extension aggregate, the component list must
4766 -- include all components that are not in the given ancestor type.
4767 -- Otherwise, the component list must include components of all
4768 -- ancestors, starting with the root.
4769
4770 if Nkind (N) = N_Extension_Aggregate then
4771 Root_Typ := Base_Type (Etype (Ancestor_Part (N)));
4772
4773 else
4774 -- AI05-0115: check legality of aggregate for type with a
4775 -- private ancestor.
4776
4777 Root_Typ := Root_Type (Typ);
4778 if Has_Private_Ancestor (Typ) then
4779 declare
4780 Ancestor : constant Entity_Id :=
4781 Find_Private_Ancestor (Typ);
4782 Ancestor_Unit : constant Entity_Id :=
4783 Cunit_Entity
4784 (Get_Source_Unit (Ancestor));
4785 Parent_Unit : constant Entity_Id :=
4786 Cunit_Entity (Get_Source_Unit
4787 (Base_Type (Etype (Ancestor))));
4788 begin
4789 -- Check whether we are in a scope that has full view
4790 -- over the private ancestor and its parent. This can
4791 -- only happen if the derivation takes place in a child
4792 -- unit of the unit that declares the parent, and we are
4793 -- in the private part or body of that child unit, else
4794 -- the aggregate is illegal.
4795
4796 if Is_Child_Unit (Ancestor_Unit)
4797 and then Scope (Ancestor_Unit) = Parent_Unit
4798 and then In_Open_Scopes (Scope (Ancestor))
4799 and then
4800 (In_Private_Part (Scope (Ancestor))
4801 or else In_Package_Body (Scope (Ancestor)))
4802 then
4803 null;
4804
4805 else
4806 Error_Msg_NE
4807 ("type of aggregate has private ancestor&!",
4808 N, Root_Typ);
4809 Error_Msg_N ("must use extension aggregate!", N);
4810 return;
4811 end if;
4812 end;
4813 end if;
4814
4815 Dnode := Declaration_Node (Base_Type (Root_Typ));
4816
4817 -- If we don't get a full declaration, then we have some error
4818 -- which will get signalled later so skip this part. Otherwise
4819 -- gather components of root that apply to the aggregate type.
4820 -- We use the base type in case there is an applicable stored
4821 -- constraint that renames the discriminants of the root.
4822
4823 if Nkind (Dnode) = N_Full_Type_Declaration then
4824 Record_Def := Type_Definition (Dnode);
4825 Gather_Components
4826 (Base_Type (Typ),
4827 Component_List (Record_Def),
4828 Governed_By => New_Assoc_List,
4829 Into => Components,
4830 Report_Errors => Errors_Found);
4831
4832 if Errors_Found then
4833 Error_Msg_N
4834 ("discriminant controlling variant part is not static",
4835 N);
4836 return;
4837 end if;
4838 end if;
4839 end if;
4840
4841 Parent_Typ := Base_Type (Typ);
4842 while Parent_Typ /= Root_Typ loop
4843 Prepend_Elmt (Parent_Typ, To => Parent_Typ_List);
4844 Parent_Typ := Etype (Parent_Typ);
4845
4846 if Nkind (Parent (Base_Type (Parent_Typ))) =
4847 N_Private_Type_Declaration
4848 or else Nkind (Parent (Base_Type (Parent_Typ))) =
4849 N_Private_Extension_Declaration
4850 then
4851 if Nkind (N) /= N_Extension_Aggregate then
4852 Error_Msg_NE
4853 ("type of aggregate has private ancestor&!",
4854 N, Parent_Typ);
4855 Error_Msg_N ("must use extension aggregate!", N);
4856 return;
4857
4858 elsif Parent_Typ /= Root_Typ then
4859 Error_Msg_NE
4860 ("ancestor part of aggregate must be private type&",
4861 Ancestor_Part (N), Parent_Typ);
4862 return;
4863 end if;
4864
4865 -- The current view of ancestor part may be a private type,
4866 -- while the context type is always non-private.
4867
4868 elsif Is_Private_Type (Root_Typ)
4869 and then Present (Full_View (Root_Typ))
4870 and then Nkind (N) = N_Extension_Aggregate
4871 then
4872 exit when Base_Type (Full_View (Root_Typ)) = Parent_Typ;
4873 end if;
4874 end loop;
4875
4876 -- Now collect components from all other ancestors, beginning
4877 -- with the current type. If the type has unknown discriminants
4878 -- use the component list of the Underlying_Record_View, which
4879 -- needs to be used for the subsequent expansion of the aggregate
4880 -- into assignments.
4881
4882 Parent_Elmt := First_Elmt (Parent_Typ_List);
4883 while Present (Parent_Elmt) loop
4884 Parent_Typ := Node (Parent_Elmt);
4885
4886 if Has_Unknown_Discriminants (Parent_Typ)
4887 and then Present (Underlying_Record_View (Typ))
4888 then
4889 Parent_Typ := Underlying_Record_View (Parent_Typ);
4890 end if;
4891
4892 Record_Def := Type_Definition (Parent (Base_Type (Parent_Typ)));
4893 Gather_Components (Empty,
4894 Component_List (Record_Extension_Part (Record_Def)),
4895 Governed_By => New_Assoc_List,
4896 Into => Components,
4897 Report_Errors => Errors_Found);
4898
4899 Next_Elmt (Parent_Elmt);
4900 end loop;
4901
4902 -- Typ is not a derived tagged type
4903
4904 else
4905 Record_Def := Type_Definition (Parent (Base_Type (Typ)));
4906
4907 if Null_Present (Record_Def) then
4908 null;
4909
4910 elsif not Has_Unknown_Discriminants (Typ) then
4911 Gather_Components
4912 (Base_Type (Typ),
4913 Component_List (Record_Def),
4914 Governed_By => New_Assoc_List,
4915 Into => Components,
4916 Report_Errors => Errors_Found);
4917
4918 else
4919 Gather_Components
4920 (Base_Type (Underlying_Record_View (Typ)),
4921 Component_List (Record_Def),
4922 Governed_By => New_Assoc_List,
4923 Into => Components,
4924 Report_Errors => Errors_Found);
4925 end if;
4926 end if;
4927
4928 if Errors_Found then
4929 return;
4930 end if;
4931 end Step_5;
4932
4933 -- STEP 6: Find component Values
4934
4935 Component := Empty;
4936 Component_Elmt := First_Elmt (Components);
4937
4938 -- First scan the remaining positional associations in the aggregate.
4939 -- Remember that at this point Positional_Expr contains the current
4940 -- positional association if any is left after looking for discriminant
4941 -- values in step 3.
4942
4943 while Present (Positional_Expr) and then Present (Component_Elmt) loop
4944 Component := Node (Component_Elmt);
4945 Resolve_Aggr_Expr (Positional_Expr, Component);
4946
4947 -- Ada 2005 (AI-231)
4948
4949 if Ada_Version >= Ada_2005 and then Known_Null (Positional_Expr) then
4950 Check_Can_Never_Be_Null (Component, Positional_Expr);
4951 end if;
4952
4953 if Present (Get_Value (Component, Component_Associations (N))) then
4954 Error_Msg_NE
4955 ("more than one value supplied for Component &", N, Component);
4956 end if;
4957
4958 Next (Positional_Expr);
4959 Next_Elmt (Component_Elmt);
4960 end loop;
4961
4962 if Present (Positional_Expr) then
4963 Error_Msg_N
4964 ("too many components for record aggregate", Positional_Expr);
4965 end if;
4966
4967 -- Now scan for the named arguments of the aggregate
4968
4969 while Present (Component_Elmt) loop
4970 Component := Node (Component_Elmt);
4971 Expr := Get_Value (Component, Component_Associations (N), True);
4972
4973 -- Note: The previous call to Get_Value sets the value of the
4974 -- variable Is_Box_Present.
4975
4976 -- Ada 2005 (AI-287): Handle components with default initialization.
4977 -- Note: This feature was originally added to Ada 2005 for limited
4978 -- but it was finally allowed with any type.
4979
4980 if Is_Box_Present then
4981 Check_Box_Component : declare
4982 Ctyp : constant Entity_Id := Etype (Component);
4983
4984 begin
4985 -- If there is a default expression for the aggregate, copy
4986 -- it into a new association. This copy must modify the scopes
4987 -- of internal types that may be attached to the expression
4988 -- (e.g. index subtypes of arrays) because in general the type
4989 -- declaration and the aggregate appear in different scopes,
4990 -- and the backend requires the scope of the type to match the
4991 -- point at which it is elaborated.
4992
4993 -- If the component has an initialization procedure (IP) we
4994 -- pass the component to the expander, which will generate
4995 -- the call to such IP.
4996
4997 -- If the component has discriminants, their values must
4998 -- be taken from their subtype. This is indispensable for
4999 -- constraints that are given by the current instance of an
5000 -- enclosing type, to allow the expansion of the aggregate to
5001 -- replace the reference to the current instance by the target
5002 -- object of the aggregate.
5003
5004 if Present (Parent (Component))
5005 and then Nkind (Parent (Component)) = N_Component_Declaration
5006 and then Present (Expression (Parent (Component)))
5007 then
5008 Expr :=
5009 New_Copy_Tree_And_Copy_Dimensions
5010 (Expression (Parent (Component)),
5011 New_Scope => Current_Scope,
5012 New_Sloc => Sloc (N));
5013
5014 -- As the type of the copied default expression may refer
5015 -- to discriminants of the record type declaration, these
5016 -- non-stored discriminants need to be rewritten into stored
5017 -- discriminant values for the aggregate. This is required
5018 -- in GNATprove mode, and is adopted in all modes to avoid
5019 -- special-casing GNATprove mode.
5020
5021 if Is_Array_Type (Etype (Expr)) then
5022 declare
5023 Rec_Typ : constant Entity_Id := Scope (Component);
5024 -- Root record type whose discriminants may be used as
5025 -- bounds in range nodes.
5026
5027 Assoc : Node_Id;
5028 Choice : Node_Id;
5029 Index : Node_Id;
5030
5031 begin
5032 -- Rewrite the range nodes occurring in the indexes
5033 -- and their types.
5034
5035 Index := First_Index (Etype (Expr));
5036 while Present (Index) loop
5037 Rewrite_Range (Rec_Typ, Index);
5038 Rewrite_Range
5039 (Rec_Typ, Scalar_Range (Etype (Index)));
5040
5041 Next_Index (Index);
5042 end loop;
5043
5044 -- Rewrite the range nodes occurring as aggregate
5045 -- bounds and component associations.
5046
5047 if Nkind (Expr) = N_Aggregate then
5048 if Present (Aggregate_Bounds (Expr)) then
5049 Rewrite_Range (Rec_Typ, Aggregate_Bounds (Expr));
5050 end if;
5051
5052 if Present (Component_Associations (Expr)) then
5053 Assoc := First (Component_Associations (Expr));
5054 while Present (Assoc) loop
5055 Choice := First (Choices (Assoc));
5056 while Present (Choice) loop
5057 Rewrite_Range (Rec_Typ, Choice);
5058
5059 Next (Choice);
5060 end loop;
5061
5062 Next (Assoc);
5063 end loop;
5064 end if;
5065 end if;
5066 end;
5067 end if;
5068
5069 Add_Association
5070 (Component => Component,
5071 Expr => Expr,
5072 Assoc_List => New_Assoc_List);
5073 Set_Has_Self_Reference (N);
5074
5075 -- A box-defaulted access component gets the value null. Also
5076 -- included are components of private types whose underlying
5077 -- type is an access type. In either case set the type of the
5078 -- literal, for subsequent use in semantic checks.
5079
5080 elsif Present (Underlying_Type (Ctyp))
5081 and then Is_Access_Type (Underlying_Type (Ctyp))
5082 then
5083 -- If the component's type is private with an access type as
5084 -- its underlying type then we have to create an unchecked
5085 -- conversion to satisfy type checking.
5086
5087 if Is_Private_Type (Ctyp) then
5088 declare
5089 Qual_Null : constant Node_Id :=
5090 Make_Qualified_Expression (Sloc (N),
5091 Subtype_Mark =>
5092 New_Occurrence_Of
5093 (Underlying_Type (Ctyp), Sloc (N)),
5094 Expression => Make_Null (Sloc (N)));
5095
5096 Convert_Null : constant Node_Id :=
5097 Unchecked_Convert_To
5098 (Ctyp, Qual_Null);
5099
5100 begin
5101 Analyze_And_Resolve (Convert_Null, Ctyp);
5102 Add_Association
5103 (Component => Component,
5104 Expr => Convert_Null,
5105 Assoc_List => New_Assoc_List);
5106 end;
5107
5108 -- Otherwise the component type is non-private
5109
5110 else
5111 Expr := Make_Null (Sloc (N));
5112 Set_Etype (Expr, Ctyp);
5113
5114 Add_Association
5115 (Component => Component,
5116 Expr => Expr,
5117 Assoc_List => New_Assoc_List);
5118 end if;
5119
5120 -- Ada 2012: If component is scalar with default value, use it
5121 -- by converting it to Ctyp, so that subtype constraints are
5122 -- checked.
5123
5124 elsif Is_Scalar_Type (Ctyp)
5125 and then Has_Default_Aspect (Ctyp)
5126 then
5127 declare
5128 Conv : constant Node_Id :=
5129 Convert_To
5130 (Typ => Ctyp,
5131 Expr =>
5132 New_Copy_Tree
5133 (Default_Aspect_Value
5134 (First_Subtype (Underlying_Type (Ctyp)))));
5135
5136 begin
5137 Analyze_And_Resolve (Conv, Ctyp);
5138 Add_Association
5139 (Component => Component,
5140 Expr => Conv,
5141 Assoc_List => New_Assoc_List);
5142 end;
5143
5144 elsif Has_Non_Null_Base_Init_Proc (Ctyp)
5145 or else not Expander_Active
5146 then
5147 if Is_Record_Type (Ctyp)
5148 and then Has_Discriminants (Ctyp)
5149 and then not Is_Private_Type (Ctyp)
5150 then
5151 -- We build a partially initialized aggregate with the
5152 -- values of the discriminants and box initialization
5153 -- for the rest, if other components are present.
5154
5155 -- The type of the aggregate is the known subtype of
5156 -- the component. The capture of discriminants must be
5157 -- recursive because subcomponents may be constrained
5158 -- (transitively) by discriminants of enclosing types.
5159 -- For a private type with discriminants, a call to the
5160 -- initialization procedure will be generated, and no
5161 -- subaggregate is needed.
5162
5163 Capture_Discriminants : declare
5164 Loc : constant Source_Ptr := Sloc (N);
5165 Expr : Node_Id;
5166
5167 begin
5168 Expr := Make_Aggregate (Loc, No_List, New_List);
5169 Set_Etype (Expr, Ctyp);
5170
5171 -- If the enclosing type has discriminants, they have
5172 -- been collected in the aggregate earlier, and they
5173 -- may appear as constraints of subcomponents.
5174
5175 -- Similarly if this component has discriminants, they
5176 -- might in turn be propagated to their components.
5177
5178 if Has_Discriminants (Typ) then
5179 Add_Discriminant_Values (Expr, New_Assoc_List);
5180 Propagate_Discriminants (Expr, New_Assoc_List);
5181
5182 elsif Has_Discriminants (Ctyp) then
5183 Add_Discriminant_Values
5184 (Expr, Component_Associations (Expr));
5185 Propagate_Discriminants
5186 (Expr, Component_Associations (Expr));
5187
5188 Build_Constrained_Itype
5189 (Expr, Ctyp, Component_Associations (Expr));
5190
5191 else
5192 declare
5193 Comp : Entity_Id;
5194
5195 begin
5196 -- If the type has additional components, create
5197 -- an OTHERS box association for them.
5198
5199 Comp := First_Component (Ctyp);
5200 while Present (Comp) loop
5201 if Ekind (Comp) = E_Component then
5202 if not Is_Record_Type (Etype (Comp)) then
5203 Append_To
5204 (Component_Associations (Expr),
5205 Make_Component_Association (Loc,
5206 Choices =>
5207 New_List (
5208 Make_Others_Choice (Loc)),
5209 Expression => Empty,
5210 Box_Present => True));
5211 end if;
5212
5213 exit;
5214 end if;
5215
5216 Next_Component (Comp);
5217 end loop;
5218 end;
5219 end if;
5220
5221 Add_Association
5222 (Component => Component,
5223 Expr => Expr,
5224 Assoc_List => New_Assoc_List);
5225 end Capture_Discriminants;
5226
5227 -- Otherwise the component type is not a record, or it has
5228 -- not discriminants, or it is private.
5229
5230 else
5231 Add_Association
5232 (Component => Component,
5233 Expr => Empty,
5234 Assoc_List => New_Assoc_List,
5235 Is_Box_Present => True);
5236 end if;
5237
5238 -- Otherwise we only need to resolve the expression if the
5239 -- component has partially initialized values (required to
5240 -- expand the corresponding assignments and run-time checks).
5241
5242 elsif Present (Expr)
5243 and then Is_Partially_Initialized_Type (Ctyp)
5244 then
5245 Resolve_Aggr_Expr (Expr, Component);
5246 end if;
5247 end Check_Box_Component;
5248
5249 elsif No (Expr) then
5250
5251 -- Ignore hidden components associated with the position of the
5252 -- interface tags: these are initialized dynamically.
5253
5254 if not Present (Related_Type (Component)) then
5255 Error_Msg_NE
5256 ("no value supplied for component &!", N, Component);
5257 end if;
5258
5259 else
5260 Resolve_Aggr_Expr (Expr, Component);
5261 end if;
5262
5263 Next_Elmt (Component_Elmt);
5264 end loop;
5265
5266 -- STEP 7: check for invalid components + check type in choice list
5267
5268 Step_7 : declare
5269 Assoc : Node_Id;
5270 New_Assoc : Node_Id;
5271
5272 Selectr : Node_Id;
5273 -- Selector name
5274
5275 Typech : Entity_Id;
5276 -- Type of first component in choice list
5277
5278 begin
5279 if Present (Component_Associations (N)) then
5280 Assoc := First (Component_Associations (N));
5281 else
5282 Assoc := Empty;
5283 end if;
5284
5285 Verification : while Present (Assoc) loop
5286 Selectr := First (Choices (Assoc));
5287 Typech := Empty;
5288
5289 if Nkind (Selectr) = N_Others_Choice then
5290
5291 -- Ada 2005 (AI-287): others choice may have expression or box
5292
5293 if No (Others_Etype) and then Others_Box = 0 then
5294 Error_Msg_N
5295 ("OTHERS must represent at least one component", Selectr);
5296
5297 elsif Others_Box = 1 and then Warn_On_Redundant_Constructs then
5298 Error_Msg_N ("others choice is redundant?", Box_Node);
5299 Error_Msg_N
5300 ("\previous choices cover all components?", Box_Node);
5301 end if;
5302
5303 exit Verification;
5304 end if;
5305
5306 while Present (Selectr) loop
5307 New_Assoc := First (New_Assoc_List);
5308 while Present (New_Assoc) loop
5309 Component := First (Choices (New_Assoc));
5310
5311 if Chars (Selectr) = Chars (Component) then
5312 if Style_Check then
5313 Check_Identifier (Selectr, Entity (Component));
5314 end if;
5315
5316 exit;
5317 end if;
5318
5319 Next (New_Assoc);
5320 end loop;
5321
5322 -- If no association, this is not a legal component of the type
5323 -- in question, unless its association is provided with a box.
5324
5325 if No (New_Assoc) then
5326 if Box_Present (Parent (Selectr)) then
5327
5328 -- This may still be a bogus component with a box. Scan
5329 -- list of components to verify that a component with
5330 -- that name exists.
5331
5332 declare
5333 C : Entity_Id;
5334
5335 begin
5336 C := First_Component (Typ);
5337 while Present (C) loop
5338 if Chars (C) = Chars (Selectr) then
5339
5340 -- If the context is an extension aggregate,
5341 -- the component must not be inherited from
5342 -- the ancestor part of the aggregate.
5343
5344 if Nkind (N) /= N_Extension_Aggregate
5345 or else
5346 Scope (Original_Record_Component (C)) /=
5347 Etype (Ancestor_Part (N))
5348 then
5349 exit;
5350 end if;
5351 end if;
5352
5353 Next_Component (C);
5354 end loop;
5355
5356 if No (C) then
5357 Error_Msg_Node_2 := Typ;
5358 Error_Msg_N ("& is not a component of}", Selectr);
5359 end if;
5360 end;
5361
5362 elsif Chars (Selectr) /= Name_uTag
5363 and then Chars (Selectr) /= Name_uParent
5364 then
5365 if not Has_Discriminants (Typ) then
5366 Error_Msg_Node_2 := Typ;
5367 Error_Msg_N ("& is not a component of}", Selectr);
5368 else
5369 Error_Msg_N
5370 ("& is not a component of the aggregate subtype",
5371 Selectr);
5372 end if;
5373
5374 Check_Misspelled_Component (Components, Selectr);
5375 end if;
5376
5377 elsif No (Typech) then
5378 Typech := Base_Type (Etype (Component));
5379
5380 -- AI05-0199: In Ada 2012, several components of anonymous
5381 -- access types can appear in a choice list, as long as the
5382 -- designated types match.
5383
5384 elsif Typech /= Base_Type (Etype (Component)) then
5385 if Ada_Version >= Ada_2012
5386 and then Ekind (Typech) = E_Anonymous_Access_Type
5387 and then
5388 Ekind (Etype (Component)) = E_Anonymous_Access_Type
5389 and then Base_Type (Designated_Type (Typech)) =
5390 Base_Type (Designated_Type (Etype (Component)))
5391 and then
5392 Subtypes_Statically_Match (Typech, (Etype (Component)))
5393 then
5394 null;
5395
5396 elsif not Box_Present (Parent (Selectr)) then
5397 Error_Msg_N
5398 ("components in choice list must have same type",
5399 Selectr);
5400 end if;
5401 end if;
5402
5403 Next (Selectr);
5404 end loop;
5405
5406 Next (Assoc);
5407 end loop Verification;
5408 end Step_7;
5409
5410 -- STEP 8: replace the original aggregate
5411
5412 Step_8 : declare
5413 New_Aggregate : constant Node_Id := New_Copy (N);
5414
5415 begin
5416 Set_Expressions (New_Aggregate, No_List);
5417 Set_Etype (New_Aggregate, Etype (N));
5418 Set_Component_Associations (New_Aggregate, New_Assoc_List);
5419 Set_Check_Actuals (New_Aggregate, Check_Actuals (N));
5420
5421 Rewrite (N, New_Aggregate);
5422 end Step_8;
5423
5424 -- Check the dimensions of the components in the record aggregate
5425
5426 Analyze_Dimension_Extension_Or_Record_Aggregate (N);
5427 end Resolve_Record_Aggregate;
5428
5429 -----------------------------
5430 -- Check_Can_Never_Be_Null --
5431 -----------------------------
5432
5433 procedure Check_Can_Never_Be_Null (Typ : Entity_Id; Expr : Node_Id) is
5434 Comp_Typ : Entity_Id;
5435
5436 begin
5437 pragma Assert
5438 (Ada_Version >= Ada_2005
5439 and then Present (Expr)
5440 and then Known_Null (Expr));
5441
5442 case Ekind (Typ) is
5443 when E_Array_Type =>
5444 Comp_Typ := Component_Type (Typ);
5445
5446 when E_Component
5447 | E_Discriminant
5448 =>
5449 Comp_Typ := Etype (Typ);
5450
5451 when others =>
5452 return;
5453 end case;
5454
5455 if Can_Never_Be_Null (Comp_Typ) then
5456
5457 -- Here we know we have a constraint error. Note that we do not use
5458 -- Apply_Compile_Time_Constraint_Error here to the Expr, which might
5459 -- seem the more natural approach. That's because in some cases the
5460 -- components are rewritten, and the replacement would be missed.
5461 -- We do not mark the whole aggregate as raising a constraint error,
5462 -- because the association may be a null array range.
5463
5464 Error_Msg_N
5465 ("(Ada 2005) null not allowed in null-excluding component??", Expr);
5466 Error_Msg_N
5467 ("\Constraint_Error will be raised at run time??", Expr);
5468
5469 Rewrite (Expr,
5470 Make_Raise_Constraint_Error
5471 (Sloc (Expr), Reason => CE_Access_Check_Failed));
5472 Set_Etype (Expr, Comp_Typ);
5473 Set_Analyzed (Expr);
5474 end if;
5475 end Check_Can_Never_Be_Null;
5476
5477 ---------------------
5478 -- Sort_Case_Table --
5479 ---------------------
5480
5481 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is
5482 U : constant Int := Case_Table'Last;
5483 K : Int;
5484 J : Int;
5485 T : Case_Bounds;
5486
5487 begin
5488 K := 1;
5489 while K < U loop
5490 T := Case_Table (K + 1);
5491
5492 J := K + 1;
5493 while J > 1
5494 and then Expr_Value (Case_Table (J - 1).Lo) > Expr_Value (T.Lo)
5495 loop
5496 Case_Table (J) := Case_Table (J - 1);
5497 J := J - 1;
5498 end loop;
5499
5500 Case_Table (J) := T;
5501 K := K + 1;
5502 end loop;
5503 end Sort_Case_Table;
5504
5505 end Sem_Aggr;