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