2018-05-21 Steven G. Kargl <kargl@gcc.gnu.org>
[official-gcc.git] / gcc / ada / sem_aggr.adb
bloba03494ed6fbed4a8343195b4cf5daeb538eab6a4
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-2018, 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 Loc : constant Source_Ptr := Sloc (N);
1662 Choice : Node_Id;
1663 Dummy : Boolean;
1664 Ent : Entity_Id;
1665 Expr : Node_Id;
1666 Id : Entity_Id;
1668 begin
1669 Choice := First (Discrete_Choices (N));
1671 while Present (Choice) loop
1672 if Nkind (Choice) = N_Others_Choice then
1673 Others_Present := True;
1675 else
1676 Analyze (Choice);
1678 -- Choice can be a subtype name, a range, or an expression
1680 if Is_Entity_Name (Choice)
1681 and then Is_Type (Entity (Choice))
1682 and then Base_Type (Entity (Choice)) = Base_Type (Index_Typ)
1683 then
1684 null;
1686 else
1687 Analyze_And_Resolve (Choice, Index_Typ);
1688 end if;
1689 end if;
1691 Next (Choice);
1692 end loop;
1694 -- Create a scope in which to introduce an index, which is usually
1695 -- visible in the expression for the component, and needed for its
1696 -- analysis.
1698 Ent := New_Internal_Entity (E_Loop, Current_Scope, Loc, 'L');
1699 Set_Etype (Ent, Standard_Void_Type);
1700 Set_Parent (Ent, Parent (N));
1701 Push_Scope (Ent);
1702 Id :=
1703 Make_Defining_Identifier (Loc,
1704 Chars => Chars (Defining_Identifier (N)));
1706 -- Insert and decorate the index variable in the current scope.
1707 -- The expression has to be analyzed once the index variable is
1708 -- directly visible. Mark the variable as referenced to prevent
1709 -- spurious warnings, given that subsequent uses of its name in the
1710 -- expression will reference the internal (synonym) loop variable.
1712 Enter_Name (Id);
1713 Set_Etype (Id, Index_Typ);
1714 Set_Ekind (Id, E_Variable);
1715 Set_Scope (Id, Ent);
1716 Set_Referenced (Id);
1718 -- Analyze a copy of the expression, to verify legality. We use
1719 -- a copy because the expression will be analyzed anew when the
1720 -- enclosing aggregate is expanded, and the construct is rewritten
1721 -- as a loop with a new index variable.
1723 Expr := New_Copy_Tree (Expression (N));
1724 Dummy := Resolve_Aggr_Expr (Expr, False);
1726 -- An iterated_component_association may appear in a nested
1727 -- aggregate for a multidimensional structure: preserve the bounds
1728 -- computed for the expression, as well as the anonymous array
1729 -- type generated for it; both are needed during array expansion.
1730 -- This does not work for more than two levels of nesting. ???
1732 if Nkind (Expr) = N_Aggregate then
1733 Set_Aggregate_Bounds (Expression (N), Aggregate_Bounds (Expr));
1734 Set_Etype (Expression (N), Etype (Expr));
1735 end if;
1737 End_Scope;
1738 end Resolve_Iterated_Component_Association;
1740 -- Local variables
1742 Assoc : Node_Id;
1743 Choice : Node_Id;
1744 Expr : Node_Id;
1745 Discard : Node_Id;
1747 Aggr_Low : Node_Id := Empty;
1748 Aggr_High : Node_Id := Empty;
1749 -- The actual low and high bounds of this sub-aggregate
1751 Case_Table_Size : Nat;
1752 -- Contains the size of the case table needed to sort aggregate choices
1754 Choices_Low : Node_Id := Empty;
1755 Choices_High : Node_Id := Empty;
1756 -- The lowest and highest discrete choices values for a named aggregate
1758 Delete_Choice : Boolean;
1759 -- Used when replacing a subtype choice with predicate by a list
1761 Nb_Elements : Uint := Uint_0;
1762 -- The number of elements in a positional aggregate
1764 Nb_Discrete_Choices : Nat := 0;
1765 -- The overall number of discrete choices (not counting others choice)
1767 -- Start of processing for Resolve_Array_Aggregate
1769 begin
1770 -- Ignore junk empty aggregate resulting from parser error
1772 if No (Expressions (N))
1773 and then No (Component_Associations (N))
1774 and then not Null_Record_Present (N)
1775 then
1776 return False;
1777 end if;
1779 -- STEP 1: make sure the aggregate is correctly formatted
1781 if Present (Component_Associations (N)) then
1782 Assoc := First (Component_Associations (N));
1783 while Present (Assoc) loop
1784 if Nkind (Assoc) = N_Iterated_Component_Association then
1785 Resolve_Iterated_Component_Association (Assoc, Index_Typ);
1786 end if;
1788 Choice := First (Choice_List (Assoc));
1789 Delete_Choice := False;
1790 while Present (Choice) loop
1791 if Nkind (Choice) = N_Others_Choice then
1792 Others_Present := True;
1794 if Choice /= First (Choice_List (Assoc))
1795 or else Present (Next (Choice))
1796 then
1797 Error_Msg_N
1798 ("OTHERS must appear alone in a choice list", Choice);
1799 return Failure;
1800 end if;
1802 if Present (Next (Assoc)) then
1803 Error_Msg_N
1804 ("OTHERS must appear last in an aggregate", Choice);
1805 return Failure;
1806 end if;
1808 if Ada_Version = Ada_83
1809 and then Assoc /= First (Component_Associations (N))
1810 and then Nkind_In (Parent (N), N_Assignment_Statement,
1811 N_Object_Declaration)
1812 then
1813 Error_Msg_N
1814 ("(Ada 83) illegal context for OTHERS choice", N);
1815 end if;
1817 elsif Is_Entity_Name (Choice) then
1818 Analyze (Choice);
1820 declare
1821 E : constant Entity_Id := Entity (Choice);
1822 New_Cs : List_Id;
1823 P : Node_Id;
1824 C : Node_Id;
1826 begin
1827 if Is_Type (E) and then Has_Predicates (E) then
1828 Freeze_Before (N, E);
1830 if Has_Dynamic_Predicate_Aspect (E) then
1831 Error_Msg_NE
1832 ("subtype& has dynamic predicate, not allowed "
1833 & "in aggregate choice", Choice, E);
1835 elsif not Is_OK_Static_Subtype (E) then
1836 Error_Msg_NE
1837 ("non-static subtype& has predicate, not allowed "
1838 & "in aggregate choice", Choice, E);
1839 end if;
1841 -- If the subtype has a static predicate, replace the
1842 -- original choice with the list of individual values
1843 -- covered by the predicate. Do not perform this
1844 -- transformation if we need to preserve the source
1845 -- for ASIS use.
1846 -- This should be deferred to expansion time ???
1848 if Present (Static_Discrete_Predicate (E))
1849 and then not ASIS_Mode
1850 then
1851 Delete_Choice := True;
1853 New_Cs := New_List;
1854 P := First (Static_Discrete_Predicate (E));
1855 while Present (P) loop
1856 C := New_Copy (P);
1857 Set_Sloc (C, Sloc (Choice));
1858 Append_To (New_Cs, C);
1859 Next (P);
1860 end loop;
1862 Insert_List_After (Choice, New_Cs);
1863 end if;
1864 end if;
1865 end;
1866 end if;
1868 Nb_Choices := Nb_Choices + 1;
1870 declare
1871 C : constant Node_Id := Choice;
1873 begin
1874 Next (Choice);
1876 if Delete_Choice then
1877 Remove (C);
1878 Nb_Choices := Nb_Choices - 1;
1879 Delete_Choice := False;
1880 end if;
1881 end;
1882 end loop;
1884 Next (Assoc);
1885 end loop;
1886 end if;
1888 -- At this point we know that the others choice, if present, is by
1889 -- itself and appears last in the aggregate. Check if we have mixed
1890 -- positional and discrete associations (other than the others choice).
1892 if Present (Expressions (N))
1893 and then (Nb_Choices > 1
1894 or else (Nb_Choices = 1 and then not Others_Present))
1895 then
1896 Error_Msg_N
1897 ("named association cannot follow positional association",
1898 First (Choice_List (First (Component_Associations (N)))));
1899 return Failure;
1900 end if;
1902 -- Test for the validity of an others choice if present
1904 if Others_Present and then not Others_Allowed then
1905 Error_Msg_N
1906 ("OTHERS choice not allowed here",
1907 First (Choices (First (Component_Associations (N)))));
1908 return Failure;
1909 end if;
1911 -- Protect against cascaded errors
1913 if Etype (Index_Typ) = Any_Type then
1914 return Failure;
1915 end if;
1917 -- STEP 2: Process named components
1919 if No (Expressions (N)) then
1920 if Others_Present then
1921 Case_Table_Size := Nb_Choices - 1;
1922 else
1923 Case_Table_Size := Nb_Choices;
1924 end if;
1926 Step_2 : declare
1927 function Empty_Range (A : Node_Id) return Boolean;
1928 -- If an association covers an empty range, some warnings on the
1929 -- expression of the association can be disabled.
1931 -----------------
1932 -- Empty_Range --
1933 -----------------
1935 function Empty_Range (A : Node_Id) return Boolean is
1936 R : constant Node_Id := First (Choices (A));
1937 begin
1938 return No (Next (R))
1939 and then Nkind (R) = N_Range
1940 and then Compile_Time_Compare
1941 (Low_Bound (R), High_Bound (R), False) = GT;
1942 end Empty_Range;
1944 -- Local variables
1946 Low : Node_Id;
1947 High : Node_Id;
1948 -- Denote the lowest and highest values in an aggregate choice
1950 S_Low : Node_Id := Empty;
1951 S_High : Node_Id := Empty;
1952 -- if a choice in an aggregate is a subtype indication these
1953 -- denote the lowest and highest values of the subtype
1955 Table : Case_Table_Type (0 .. Case_Table_Size);
1956 -- Used to sort all the different choice values. Entry zero is
1957 -- reserved for sorting purposes.
1959 Single_Choice : Boolean;
1960 -- Set to true every time there is a single discrete choice in a
1961 -- discrete association
1963 Prev_Nb_Discrete_Choices : Nat;
1964 -- Used to keep track of the number of discrete choices in the
1965 -- current association.
1967 Errors_Posted_On_Choices : Boolean := False;
1968 -- Keeps track of whether any choices have semantic errors
1970 -- Start of processing for Step_2
1972 begin
1973 -- STEP 2 (A): Check discrete choices validity
1975 Assoc := First (Component_Associations (N));
1976 while Present (Assoc) loop
1977 Prev_Nb_Discrete_Choices := Nb_Discrete_Choices;
1978 Choice := First (Choice_List (Assoc));
1980 loop
1981 Analyze (Choice);
1983 if Nkind (Choice) = N_Others_Choice then
1984 Single_Choice := False;
1985 exit;
1987 -- Test for subtype mark without constraint
1989 elsif Is_Entity_Name (Choice) and then
1990 Is_Type (Entity (Choice))
1991 then
1992 if Base_Type (Entity (Choice)) /= Index_Base then
1993 Error_Msg_N
1994 ("invalid subtype mark in aggregate choice",
1995 Choice);
1996 return Failure;
1997 end if;
1999 -- Case of subtype indication
2001 elsif Nkind (Choice) = N_Subtype_Indication then
2002 Resolve_Discrete_Subtype_Indication (Choice, Index_Base);
2004 if Has_Dynamic_Predicate_Aspect
2005 (Entity (Subtype_Mark (Choice)))
2006 then
2007 Error_Msg_NE
2008 ("subtype& has dynamic predicate, "
2009 & "not allowed in aggregate choice",
2010 Choice, Entity (Subtype_Mark (Choice)));
2011 end if;
2013 -- Does the subtype indication evaluation raise CE?
2015 Get_Index_Bounds (Subtype_Mark (Choice), S_Low, S_High);
2016 Get_Index_Bounds (Choice, Low, High);
2017 Check_Bounds (S_Low, S_High, Low, High);
2019 -- Case of range or expression
2021 else
2022 Resolve (Choice, Index_Base);
2023 Check_Unset_Reference (Choice);
2024 Check_Non_Static_Context (Choice);
2026 -- If semantic errors were posted on the choice, then
2027 -- record that for possible early return from later
2028 -- processing (see handling of enumeration choices).
2030 if Error_Posted (Choice) then
2031 Errors_Posted_On_Choices := True;
2032 end if;
2034 -- Do not range check a choice. This check is redundant
2035 -- since this test is already done when we check that the
2036 -- bounds of the array aggregate are within range.
2038 Set_Do_Range_Check (Choice, False);
2040 -- In SPARK, the choice must be static
2042 if not (Is_OK_Static_Expression (Choice)
2043 or else (Nkind (Choice) = N_Range
2044 and then Is_OK_Static_Range (Choice)))
2045 then
2046 Check_SPARK_05_Restriction
2047 ("choice should be static", Choice);
2048 end if;
2049 end if;
2051 -- If we could not resolve the discrete choice stop here
2053 if Etype (Choice) = Any_Type then
2054 return Failure;
2056 -- If the discrete choice raises CE get its original bounds
2058 elsif Nkind (Choice) = N_Raise_Constraint_Error then
2059 Set_Raises_Constraint_Error (N);
2060 Get_Index_Bounds (Original_Node (Choice), Low, High);
2062 -- Otherwise get its bounds as usual
2064 else
2065 Get_Index_Bounds (Choice, Low, High);
2066 end if;
2068 if (Dynamic_Or_Null_Range (Low, High)
2069 or else (Nkind (Choice) = N_Subtype_Indication
2070 and then
2071 Dynamic_Or_Null_Range (S_Low, S_High)))
2072 and then Nb_Choices /= 1
2073 then
2074 Error_Msg_N
2075 ("dynamic or empty choice in aggregate "
2076 & "must be the only choice", Choice);
2077 return Failure;
2078 end if;
2080 if not (All_Composite_Constraints_Static (Low)
2081 and then All_Composite_Constraints_Static (High)
2082 and then All_Composite_Constraints_Static (S_Low)
2083 and then All_Composite_Constraints_Static (S_High))
2084 then
2085 Check_Restriction (No_Dynamic_Sized_Objects, Choice);
2086 end if;
2088 Nb_Discrete_Choices := Nb_Discrete_Choices + 1;
2089 Table (Nb_Discrete_Choices).Lo := Low;
2090 Table (Nb_Discrete_Choices).Hi := High;
2091 Table (Nb_Discrete_Choices).Choice := Choice;
2093 Next (Choice);
2095 if No (Choice) then
2097 -- Check if we have a single discrete choice and whether
2098 -- this discrete choice specifies a single value.
2100 Single_Choice :=
2101 (Nb_Discrete_Choices = Prev_Nb_Discrete_Choices + 1)
2102 and then (Low = High);
2104 exit;
2105 end if;
2106 end loop;
2108 -- Ada 2005 (AI-231)
2110 if Ada_Version >= Ada_2005
2111 and then Known_Null (Expression (Assoc))
2112 and then not Empty_Range (Assoc)
2113 then
2114 Check_Can_Never_Be_Null (Etype (N), Expression (Assoc));
2115 end if;
2117 -- Ada 2005 (AI-287): In case of default initialized component
2118 -- we delay the resolution to the expansion phase.
2120 if Box_Present (Assoc) then
2122 -- Ada 2005 (AI-287): In case of default initialization of a
2123 -- component the expander will generate calls to the
2124 -- corresponding initialization subprogram. We need to call
2125 -- Resolve_Aggr_Expr to check the rules about
2126 -- dimensionality.
2128 if not Resolve_Aggr_Expr
2129 (Assoc, Single_Elmt => Single_Choice)
2130 then
2131 return Failure;
2132 end if;
2134 elsif Nkind (Assoc) = N_Iterated_Component_Association then
2135 null; -- handled above, in a loop context.
2137 elsif not Resolve_Aggr_Expr
2138 (Expression (Assoc), Single_Elmt => Single_Choice)
2139 then
2140 return Failure;
2142 -- Check incorrect use of dynamically tagged expression
2144 -- We differentiate here two cases because the expression may
2145 -- not be decorated. For example, the analysis and resolution
2146 -- of the expression associated with the others choice will be
2147 -- done later with the full aggregate. In such case we
2148 -- duplicate the expression tree to analyze the copy and
2149 -- perform the required check.
2151 elsif not Present (Etype (Expression (Assoc))) then
2152 declare
2153 Save_Analysis : constant Boolean := Full_Analysis;
2154 Expr : constant Node_Id :=
2155 New_Copy_Tree (Expression (Assoc));
2157 begin
2158 Expander_Mode_Save_And_Set (False);
2159 Full_Analysis := False;
2161 -- Analyze the expression, making sure it is properly
2162 -- attached to the tree before we do the analysis.
2164 Set_Parent (Expr, Parent (Expression (Assoc)));
2165 Analyze (Expr);
2167 -- Compute its dimensions now, rather than at the end of
2168 -- resolution, because in the case of multidimensional
2169 -- aggregates subsequent expansion may lead to spurious
2170 -- errors.
2172 Check_Expression_Dimensions (Expr, Component_Typ);
2174 -- If the expression is a literal, propagate this info
2175 -- to the expression in the association, to enable some
2176 -- optimizations downstream.
2178 if Is_Entity_Name (Expr)
2179 and then Present (Entity (Expr))
2180 and then Ekind (Entity (Expr)) = E_Enumeration_Literal
2181 then
2182 Analyze_And_Resolve
2183 (Expression (Assoc), Component_Typ);
2184 end if;
2186 Full_Analysis := Save_Analysis;
2187 Expander_Mode_Restore;
2189 if Is_Tagged_Type (Etype (Expr)) then
2190 Check_Dynamically_Tagged_Expression
2191 (Expr => Expr,
2192 Typ => Component_Type (Etype (N)),
2193 Related_Nod => N);
2194 end if;
2195 end;
2197 elsif Is_Tagged_Type (Etype (Expression (Assoc))) then
2198 Check_Dynamically_Tagged_Expression
2199 (Expr => Expression (Assoc),
2200 Typ => Component_Type (Etype (N)),
2201 Related_Nod => N);
2202 end if;
2204 Next (Assoc);
2205 end loop;
2207 -- If aggregate contains more than one choice then these must be
2208 -- static. Check for duplicate and missing values.
2210 -- Note: there is duplicated code here wrt Check_Choice_Set in
2211 -- the body of Sem_Case, and it is possible we could just reuse
2212 -- that procedure. To be checked ???
2214 if Nb_Discrete_Choices > 1 then
2215 Check_Choices : declare
2216 Choice : Node_Id;
2217 -- Location of choice for messages
2219 Hi_Val : Uint;
2220 Lo_Val : Uint;
2221 -- High end of one range and Low end of the next. Should be
2222 -- contiguous if there is no hole in the list of values.
2224 Lo_Dup : Uint;
2225 Hi_Dup : Uint;
2226 -- End points of duplicated range
2228 Missing_Or_Duplicates : Boolean := False;
2229 -- Set True if missing or duplicate choices found
2231 procedure Output_Bad_Choices (Lo, Hi : Uint; C : Node_Id);
2232 -- Output continuation message with a representation of the
2233 -- bounds (just Lo if Lo = Hi, else Lo .. Hi). C is the
2234 -- choice node where the message is to be posted.
2236 ------------------------
2237 -- Output_Bad_Choices --
2238 ------------------------
2240 procedure Output_Bad_Choices (Lo, Hi : Uint; C : Node_Id) is
2241 begin
2242 -- Enumeration type case
2244 if Is_Enumeration_Type (Index_Typ) then
2245 Error_Msg_Name_1 :=
2246 Chars (Get_Enum_Lit_From_Pos (Index_Typ, Lo, Loc));
2247 Error_Msg_Name_2 :=
2248 Chars (Get_Enum_Lit_From_Pos (Index_Typ, Hi, Loc));
2250 if Lo = Hi then
2251 Error_Msg_N ("\\ %!", C);
2252 else
2253 Error_Msg_N ("\\ % .. %!", C);
2254 end if;
2256 -- Integer types case
2258 else
2259 Error_Msg_Uint_1 := Lo;
2260 Error_Msg_Uint_2 := Hi;
2262 if Lo = Hi then
2263 Error_Msg_N ("\\ ^!", C);
2264 else
2265 Error_Msg_N ("\\ ^ .. ^!", C);
2266 end if;
2267 end if;
2268 end Output_Bad_Choices;
2270 -- Start of processing for Check_Choices
2272 begin
2273 Sort_Case_Table (Table);
2275 -- First we do a quick linear loop to find out if we have
2276 -- any duplicates or missing entries (usually we have a
2277 -- legal aggregate, so this will get us out quickly).
2279 for J in 1 .. Nb_Discrete_Choices - 1 loop
2280 Hi_Val := Expr_Value (Table (J).Hi);
2281 Lo_Val := Expr_Value (Table (J + 1).Lo);
2283 if Lo_Val <= Hi_Val
2284 or else (Lo_Val > Hi_Val + 1
2285 and then not Others_Present)
2286 then
2287 Missing_Or_Duplicates := True;
2288 exit;
2289 end if;
2290 end loop;
2292 -- If we have missing or duplicate entries, first fill in
2293 -- the Highest entries to make life easier in the following
2294 -- loops to detect bad entries.
2296 if Missing_Or_Duplicates then
2297 Table (1).Highest := Expr_Value (Table (1).Hi);
2299 for J in 2 .. Nb_Discrete_Choices loop
2300 Table (J).Highest :=
2301 UI_Max
2302 (Table (J - 1).Highest, Expr_Value (Table (J).Hi));
2303 end loop;
2305 -- Loop through table entries to find duplicate indexes
2307 for J in 2 .. Nb_Discrete_Choices loop
2308 Lo_Val := Expr_Value (Table (J).Lo);
2309 Hi_Val := Expr_Value (Table (J).Hi);
2311 -- Case where we have duplicates (the lower bound of
2312 -- this choice is less than or equal to the highest
2313 -- high bound found so far).
2315 if Lo_Val <= Table (J - 1).Highest then
2317 -- We move backwards looking for duplicates. We can
2318 -- abandon this loop as soon as we reach a choice
2319 -- highest value that is less than Lo_Val.
2321 for K in reverse 1 .. J - 1 loop
2322 exit when Table (K).Highest < Lo_Val;
2324 -- Here we may have duplicates between entries
2325 -- for K and J. Get range of duplicates.
2327 Lo_Dup :=
2328 UI_Max (Lo_Val, Expr_Value (Table (K).Lo));
2329 Hi_Dup :=
2330 UI_Min (Hi_Val, Expr_Value (Table (K).Hi));
2332 -- Nothing to do if duplicate range is null
2334 if Lo_Dup > Hi_Dup then
2335 null;
2337 -- Otherwise place proper message. Because
2338 -- of the missing expansion of subtypes with
2339 -- predicates in ASIS mode, do not report
2340 -- spurious overlap errors.
2342 elsif ASIS_Mode
2343 and then
2344 ((Is_Type (Entity (Table (J).Choice))
2345 and then Has_Predicates
2346 (Entity (Table (J).Choice)))
2347 or else
2348 (Is_Type (Entity (Table (K).Choice))
2349 and then Has_Predicates
2350 (Entity (Table (K).Choice))))
2351 then
2352 null;
2354 else
2355 -- We place message on later choice, with a
2356 -- line reference to the earlier choice.
2358 if Sloc (Table (J).Choice) <
2359 Sloc (Table (K).Choice)
2360 then
2361 Choice := Table (K).Choice;
2362 Error_Msg_Sloc := Sloc (Table (J).Choice);
2363 else
2364 Choice := Table (J).Choice;
2365 Error_Msg_Sloc := Sloc (Table (K).Choice);
2366 end if;
2368 if Lo_Dup = Hi_Dup then
2369 Error_Msg_N
2370 ("index value in array aggregate "
2371 & "duplicates the one given#!", Choice);
2372 else
2373 Error_Msg_N
2374 ("index values in array aggregate "
2375 & "duplicate those given#!", Choice);
2376 end if;
2378 Output_Bad_Choices (Lo_Dup, Hi_Dup, Choice);
2379 end if;
2380 end loop;
2381 end if;
2382 end loop;
2384 -- Loop through entries in table to find missing indexes.
2385 -- Not needed if others, since missing impossible.
2387 if not Others_Present then
2388 for J in 2 .. Nb_Discrete_Choices loop
2389 Lo_Val := Expr_Value (Table (J).Lo);
2390 Hi_Val := Table (J - 1).Highest;
2392 if Lo_Val > Hi_Val + 1 then
2394 declare
2395 Error_Node : Node_Id;
2397 begin
2398 -- If the choice is the bound of a range in
2399 -- a subtype indication, it is not in the
2400 -- source lists for the aggregate itself, so
2401 -- post the error on the aggregate. Otherwise
2402 -- post it on choice itself.
2404 Choice := Table (J).Choice;
2406 if Is_List_Member (Choice) then
2407 Error_Node := Choice;
2408 else
2409 Error_Node := N;
2410 end if;
2412 if Hi_Val + 1 = Lo_Val - 1 then
2413 Error_Msg_N
2414 ("missing index value "
2415 & "in array aggregate!", Error_Node);
2416 else
2417 Error_Msg_N
2418 ("missing index values "
2419 & "in array aggregate!", Error_Node);
2420 end if;
2422 Output_Bad_Choices
2423 (Hi_Val + 1, Lo_Val - 1, Error_Node);
2424 end;
2425 end if;
2426 end loop;
2427 end if;
2429 -- If either missing or duplicate values, return failure
2431 Set_Etype (N, Any_Composite);
2432 return Failure;
2433 end if;
2434 end Check_Choices;
2435 end if;
2437 -- STEP 2 (B): Compute aggregate bounds and min/max choices values
2439 if Nb_Discrete_Choices > 0 then
2440 Choices_Low := Table (1).Lo;
2441 Choices_High := Table (Nb_Discrete_Choices).Hi;
2442 end if;
2444 -- If Others is present, then bounds of aggregate come from the
2445 -- index constraint (not the choices in the aggregate itself).
2447 if Others_Present then
2448 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2450 -- Abandon processing if either bound is already signalled as
2451 -- an error (prevents junk cascaded messages and blow ups).
2453 if Nkind (Aggr_Low) = N_Error
2454 or else
2455 Nkind (Aggr_High) = N_Error
2456 then
2457 return False;
2458 end if;
2460 -- No others clause present
2462 else
2463 -- Special processing if others allowed and not present. This
2464 -- means that the bounds of the aggregate come from the index
2465 -- constraint (and the length must match).
2467 if Others_Allowed then
2468 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2470 -- Abandon processing if either bound is already signalled
2471 -- as an error (stop junk cascaded messages and blow ups).
2473 if Nkind (Aggr_Low) = N_Error
2474 or else
2475 Nkind (Aggr_High) = N_Error
2476 then
2477 return False;
2478 end if;
2480 -- If others allowed, and no others present, then the array
2481 -- should cover all index values. If it does not, we will
2482 -- get a length check warning, but there is two cases where
2483 -- an additional warning is useful:
2485 -- If we have no positional components, and the length is
2486 -- wrong (which we can tell by others being allowed with
2487 -- missing components), and the index type is an enumeration
2488 -- type, then issue appropriate warnings about these missing
2489 -- components. They are only warnings, since the aggregate
2490 -- is fine, it's just the wrong length. We skip this check
2491 -- for standard character types (since there are no literals
2492 -- and it is too much trouble to concoct them), and also if
2493 -- any of the bounds have values that are not known at
2494 -- compile time.
2496 -- Another case warranting a warning is when the length
2497 -- is right, but as above we have an index type that is
2498 -- an enumeration, and the bounds do not match. This is a
2499 -- case where dubious sliding is allowed and we generate a
2500 -- warning that the bounds do not match.
2502 if No (Expressions (N))
2503 and then Nkind (Index) = N_Range
2504 and then Is_Enumeration_Type (Etype (Index))
2505 and then not Is_Standard_Character_Type (Etype (Index))
2506 and then Compile_Time_Known_Value (Aggr_Low)
2507 and then Compile_Time_Known_Value (Aggr_High)
2508 and then Compile_Time_Known_Value (Choices_Low)
2509 and then Compile_Time_Known_Value (Choices_High)
2510 then
2511 -- If any of the expressions or range bounds in choices
2512 -- have semantic errors, then do not attempt further
2513 -- resolution, to prevent cascaded errors.
2515 if Errors_Posted_On_Choices then
2516 return Failure;
2517 end if;
2519 declare
2520 ALo : constant Node_Id := Expr_Value_E (Aggr_Low);
2521 AHi : constant Node_Id := Expr_Value_E (Aggr_High);
2522 CLo : constant Node_Id := Expr_Value_E (Choices_Low);
2523 CHi : constant Node_Id := Expr_Value_E (Choices_High);
2525 Ent : Entity_Id;
2527 begin
2528 -- Warning case 1, missing values at start/end. Only
2529 -- do the check if the number of entries is too small.
2531 if (Enumeration_Pos (CHi) - Enumeration_Pos (CLo))
2533 (Enumeration_Pos (AHi) - Enumeration_Pos (ALo))
2534 then
2535 Error_Msg_N
2536 ("missing index value(s) in array aggregate??",
2539 -- Output missing value(s) at start
2541 if Chars (ALo) /= Chars (CLo) then
2542 Ent := Prev (CLo);
2544 if Chars (ALo) = Chars (Ent) then
2545 Error_Msg_Name_1 := Chars (ALo);
2546 Error_Msg_N ("\ %??", N);
2547 else
2548 Error_Msg_Name_1 := Chars (ALo);
2549 Error_Msg_Name_2 := Chars (Ent);
2550 Error_Msg_N ("\ % .. %??", N);
2551 end if;
2552 end if;
2554 -- Output missing value(s) at end
2556 if Chars (AHi) /= Chars (CHi) then
2557 Ent := Next (CHi);
2559 if Chars (AHi) = Chars (Ent) then
2560 Error_Msg_Name_1 := Chars (Ent);
2561 Error_Msg_N ("\ %??", N);
2562 else
2563 Error_Msg_Name_1 := Chars (Ent);
2564 Error_Msg_Name_2 := Chars (AHi);
2565 Error_Msg_N ("\ % .. %??", N);
2566 end if;
2567 end if;
2569 -- Warning case 2, dubious sliding. The First_Subtype
2570 -- test distinguishes between a constrained type where
2571 -- sliding is not allowed (so we will get a warning
2572 -- later that Constraint_Error will be raised), and
2573 -- the unconstrained case where sliding is permitted.
2575 elsif (Enumeration_Pos (CHi) - Enumeration_Pos (CLo))
2577 (Enumeration_Pos (AHi) - Enumeration_Pos (ALo))
2578 and then Chars (ALo) /= Chars (CLo)
2579 and then
2580 not Is_Constrained (First_Subtype (Etype (N)))
2581 then
2582 Error_Msg_N
2583 ("bounds of aggregate do not match target??", N);
2584 end if;
2585 end;
2586 end if;
2587 end if;
2589 -- If no others, aggregate bounds come from aggregate
2591 Aggr_Low := Choices_Low;
2592 Aggr_High := Choices_High;
2593 end if;
2594 end Step_2;
2596 -- STEP 3: Process positional components
2598 else
2599 -- STEP 3 (A): Process positional elements
2601 Expr := First (Expressions (N));
2602 Nb_Elements := Uint_0;
2603 while Present (Expr) loop
2604 Nb_Elements := Nb_Elements + 1;
2606 -- Ada 2005 (AI-231)
2608 if Ada_Version >= Ada_2005 and then Known_Null (Expr) then
2609 Check_Can_Never_Be_Null (Etype (N), Expr);
2610 end if;
2612 if not Resolve_Aggr_Expr (Expr, Single_Elmt => True) then
2613 return Failure;
2614 end if;
2616 -- Check incorrect use of dynamically tagged expression
2618 if Is_Tagged_Type (Etype (Expr)) then
2619 Check_Dynamically_Tagged_Expression
2620 (Expr => Expr,
2621 Typ => Component_Type (Etype (N)),
2622 Related_Nod => N);
2623 end if;
2625 Next (Expr);
2626 end loop;
2628 if Others_Present then
2629 Assoc := Last (Component_Associations (N));
2631 -- Ada 2005 (AI-231)
2633 if Ada_Version >= Ada_2005 and then Known_Null (Assoc) then
2634 Check_Can_Never_Be_Null (Etype (N), Expression (Assoc));
2635 end if;
2637 -- Ada 2005 (AI-287): In case of default initialized component,
2638 -- we delay the resolution to the expansion phase.
2640 if Box_Present (Assoc) then
2642 -- Ada 2005 (AI-287): In case of default initialization of a
2643 -- component the expander will generate calls to the
2644 -- corresponding initialization subprogram. We need to call
2645 -- Resolve_Aggr_Expr to check the rules about
2646 -- dimensionality.
2648 if not Resolve_Aggr_Expr (Assoc, Single_Elmt => False) then
2649 return Failure;
2650 end if;
2652 elsif not Resolve_Aggr_Expr (Expression (Assoc),
2653 Single_Elmt => False)
2654 then
2655 return Failure;
2657 -- Check incorrect use of dynamically tagged expression. The
2658 -- expression of the others choice has not been resolved yet.
2659 -- In order to diagnose the semantic error we create a duplicate
2660 -- tree to analyze it and perform the check.
2662 else
2663 declare
2664 Save_Analysis : constant Boolean := Full_Analysis;
2665 Expr : constant Node_Id :=
2666 New_Copy_Tree (Expression (Assoc));
2668 begin
2669 Expander_Mode_Save_And_Set (False);
2670 Full_Analysis := False;
2671 Analyze (Expr);
2672 Full_Analysis := Save_Analysis;
2673 Expander_Mode_Restore;
2675 if Is_Tagged_Type (Etype (Expr)) then
2676 Check_Dynamically_Tagged_Expression
2677 (Expr => Expr,
2678 Typ => Component_Type (Etype (N)),
2679 Related_Nod => N);
2680 end if;
2681 end;
2682 end if;
2683 end if;
2685 -- STEP 3 (B): Compute the aggregate bounds
2687 if Others_Present then
2688 Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High);
2690 else
2691 if Others_Allowed then
2692 Get_Index_Bounds (Index_Constr, Aggr_Low, Discard);
2693 else
2694 Aggr_Low := Index_Typ_Low;
2695 end if;
2697 Aggr_High := Add (Nb_Elements - 1, To => Aggr_Low);
2698 Check_Bound (Index_Base_High, Aggr_High);
2699 end if;
2700 end if;
2702 -- STEP 4: Perform static aggregate checks and save the bounds
2704 -- Check (A)
2706 Check_Bounds (Index_Typ_Low, Index_Typ_High, Aggr_Low, Aggr_High);
2707 Check_Bounds (Index_Base_Low, Index_Base_High, Aggr_Low, Aggr_High);
2709 -- Check (B)
2711 if Others_Present and then Nb_Discrete_Choices > 0 then
2712 Check_Bounds (Aggr_Low, Aggr_High, Choices_Low, Choices_High);
2713 Check_Bounds (Index_Typ_Low, Index_Typ_High,
2714 Choices_Low, Choices_High);
2715 Check_Bounds (Index_Base_Low, Index_Base_High,
2716 Choices_Low, Choices_High);
2718 -- Check (C)
2720 elsif Others_Present and then Nb_Elements > 0 then
2721 Check_Length (Aggr_Low, Aggr_High, Nb_Elements);
2722 Check_Length (Index_Typ_Low, Index_Typ_High, Nb_Elements);
2723 Check_Length (Index_Base_Low, Index_Base_High, Nb_Elements);
2724 end if;
2726 if Raises_Constraint_Error (Aggr_Low)
2727 or else Raises_Constraint_Error (Aggr_High)
2728 then
2729 Set_Raises_Constraint_Error (N);
2730 end if;
2732 Aggr_Low := Duplicate_Subexpr (Aggr_Low);
2734 -- Do not duplicate Aggr_High if Aggr_High = Aggr_Low + Nb_Elements
2735 -- since the addition node returned by Add is not yet analyzed. Attach
2736 -- to tree and analyze first. Reset analyzed flag to ensure it will get
2737 -- analyzed when it is a literal bound whose type must be properly set.
2739 if Others_Present or else Nb_Discrete_Choices > 0 then
2740 Aggr_High := Duplicate_Subexpr (Aggr_High);
2742 if Etype (Aggr_High) = Universal_Integer then
2743 Set_Analyzed (Aggr_High, False);
2744 end if;
2745 end if;
2747 -- If the aggregate already has bounds attached to it, it means this is
2748 -- a positional aggregate created as an optimization by
2749 -- Exp_Aggr.Convert_To_Positional, so we don't want to change those
2750 -- bounds.
2752 if Present (Aggregate_Bounds (N)) and then not Others_Allowed then
2753 Aggr_Low := Low_Bound (Aggregate_Bounds (N));
2754 Aggr_High := High_Bound (Aggregate_Bounds (N));
2755 end if;
2757 Set_Aggregate_Bounds
2758 (N, Make_Range (Loc, Low_Bound => Aggr_Low, High_Bound => Aggr_High));
2760 -- The bounds may contain expressions that must be inserted upwards.
2761 -- Attach them fully to the tree. After analysis, remove side effects
2762 -- from upper bound, if still needed.
2764 Set_Parent (Aggregate_Bounds (N), N);
2765 Analyze_And_Resolve (Aggregate_Bounds (N), Index_Typ);
2766 Check_Unset_Reference (Aggregate_Bounds (N));
2768 if not Others_Present and then Nb_Discrete_Choices = 0 then
2769 Set_High_Bound
2770 (Aggregate_Bounds (N),
2771 Duplicate_Subexpr (High_Bound (Aggregate_Bounds (N))));
2772 end if;
2774 -- Check the dimensions of each component in the array aggregate
2776 Analyze_Dimension_Array_Aggregate (N, Component_Typ);
2778 return Success;
2779 end Resolve_Array_Aggregate;
2781 -----------------------------
2782 -- Resolve_Delta_Aggregate --
2783 -----------------------------
2785 procedure Resolve_Delta_Aggregate (N : Node_Id; Typ : Entity_Id) is
2786 Base : constant Node_Id := Expression (N);
2788 begin
2789 if not Is_Composite_Type (Typ) then
2790 Error_Msg_N ("not a composite type", N);
2791 end if;
2793 Analyze_And_Resolve (Base, Typ);
2795 if Is_Array_Type (Typ) then
2796 Resolve_Delta_Array_Aggregate (N, Typ);
2797 else
2798 Resolve_Delta_Record_Aggregate (N, Typ);
2799 end if;
2801 Set_Etype (N, Typ);
2802 end Resolve_Delta_Aggregate;
2804 -----------------------------------
2805 -- Resolve_Delta_Array_Aggregate --
2806 -----------------------------------
2808 procedure Resolve_Delta_Array_Aggregate (N : Node_Id; Typ : Entity_Id) is
2809 Deltas : constant List_Id := Component_Associations (N);
2811 Assoc : Node_Id;
2812 Choice : Node_Id;
2813 Index_Type : Entity_Id;
2815 begin
2816 Index_Type := Etype (First_Index (Typ));
2818 Assoc := First (Deltas);
2819 while Present (Assoc) loop
2820 if Nkind (Assoc) = N_Iterated_Component_Association then
2821 Choice := First (Choice_List (Assoc));
2822 while Present (Choice) loop
2823 if Nkind (Choice) = N_Others_Choice then
2824 Error_Msg_N
2825 ("others not allowed in delta aggregate", Choice);
2827 else
2828 Analyze_And_Resolve (Choice, Index_Type);
2829 end if;
2831 Next (Choice);
2832 end loop;
2834 declare
2835 Id : constant Entity_Id := Defining_Identifier (Assoc);
2836 Ent : constant Entity_Id :=
2837 New_Internal_Entity
2838 (E_Loop, Current_Scope, Sloc (Assoc), 'L');
2840 begin
2841 Set_Etype (Ent, Standard_Void_Type);
2842 Set_Parent (Ent, Assoc);
2844 if No (Scope (Id)) then
2845 Enter_Name (Id);
2846 Set_Etype (Id, Index_Type);
2847 Set_Ekind (Id, E_Variable);
2848 Set_Scope (Id, Ent);
2849 end if;
2851 Push_Scope (Ent);
2852 Analyze_And_Resolve
2853 (New_Copy_Tree (Expression (Assoc)), Component_Type (Typ));
2854 End_Scope;
2855 end;
2857 else
2858 Choice := First (Choice_List (Assoc));
2859 while Present (Choice) loop
2860 if Nkind (Choice) = N_Others_Choice then
2861 Error_Msg_N
2862 ("others not allowed in delta aggregate", Choice);
2864 else
2865 Analyze (Choice);
2867 if Is_Entity_Name (Choice)
2868 and then Is_Type (Entity (Choice))
2869 then
2870 -- Choice covers a range of values
2872 if Base_Type (Entity (Choice)) /=
2873 Base_Type (Index_Type)
2874 then
2875 Error_Msg_NE
2876 ("choice does mat match index type of",
2877 Choice, Typ);
2878 end if;
2879 else
2880 Resolve (Choice, Index_Type);
2881 end if;
2882 end if;
2884 Next (Choice);
2885 end loop;
2887 Analyze_And_Resolve (Expression (Assoc), Component_Type (Typ));
2888 end if;
2890 Next (Assoc);
2891 end loop;
2892 end Resolve_Delta_Array_Aggregate;
2894 ------------------------------------
2895 -- Resolve_Delta_Record_Aggregate --
2896 ------------------------------------
2898 procedure Resolve_Delta_Record_Aggregate (N : Node_Id; Typ : Entity_Id) is
2900 -- Variables used to verify that discriminant-dependent components
2901 -- appear in the same variant.
2903 Comp_Ref : Entity_Id := Empty; -- init to avoid warning
2904 Variant : Node_Id;
2906 procedure Check_Variant (Id : Entity_Id);
2907 -- If a given component of the delta aggregate appears in a variant
2908 -- part, verify that it is within the same variant as that of previous
2909 -- specified variant components of the delta.
2911 function Get_Component_Type (Nam : Node_Id) return Entity_Id;
2912 -- Locate component with a given name and return its type. If none found
2913 -- report error.
2915 function Nested_In (V1 : Node_Id; V2 : Node_Id) return Boolean;
2916 -- Determine whether variant V1 is within variant V2
2918 function Variant_Depth (N : Node_Id) return Integer;
2919 -- Determine the distance of a variant to the enclosing type
2920 -- declaration.
2922 --------------------
2923 -- Check_Variant --
2924 --------------------
2926 procedure Check_Variant (Id : Entity_Id) is
2927 Comp : Entity_Id;
2928 Comp_Variant : Node_Id;
2930 begin
2931 if not Has_Discriminants (Typ) then
2932 return;
2933 end if;
2935 Comp := First_Entity (Typ);
2936 while Present (Comp) loop
2937 exit when Chars (Comp) = Chars (Id);
2938 Next_Component (Comp);
2939 end loop;
2941 -- Find the variant, if any, whose component list includes the
2942 -- component declaration.
2944 Comp_Variant := Parent (Parent (List_Containing (Parent (Comp))));
2945 if Nkind (Comp_Variant) = N_Variant then
2946 if No (Variant) then
2947 Variant := Comp_Variant;
2948 Comp_Ref := Comp;
2950 elsif Variant /= Comp_Variant then
2951 declare
2952 D1 : constant Integer := Variant_Depth (Variant);
2953 D2 : constant Integer := Variant_Depth (Comp_Variant);
2955 begin
2956 if D1 = D2
2957 or else
2958 (D1 > D2 and then not Nested_In (Variant, Comp_Variant))
2959 or else
2960 (D2 > D1 and then not Nested_In (Comp_Variant, Variant))
2961 then
2962 pragma Assert (Present (Comp_Ref));
2963 Error_Msg_Node_2 := Comp_Ref;
2964 Error_Msg_NE
2965 ("& and & appear in different variants", Id, Comp);
2967 -- Otherwise retain the deeper variant for subsequent tests
2969 elsif D2 > D1 then
2970 Variant := Comp_Variant;
2971 end if;
2972 end;
2973 end if;
2974 end if;
2975 end Check_Variant;
2977 ------------------------
2978 -- Get_Component_Type --
2979 ------------------------
2981 function Get_Component_Type (Nam : Node_Id) return Entity_Id is
2982 Comp : Entity_Id;
2984 begin
2985 Comp := First_Entity (Typ);
2986 while Present (Comp) loop
2987 if Chars (Comp) = Chars (Nam) then
2988 if Ekind (Comp) = E_Discriminant then
2989 Error_Msg_N ("delta cannot apply to discriminant", Nam);
2990 end if;
2992 return Etype (Comp);
2993 end if;
2995 Comp := Next_Entity (Comp);
2996 end loop;
2998 Error_Msg_NE ("type& has no component with this name", Nam, Typ);
2999 return Any_Type;
3000 end Get_Component_Type;
3002 ---------------
3003 -- Nested_In --
3004 ---------------
3006 function Nested_In (V1, V2 : Node_Id) return Boolean is
3007 Par : Node_Id;
3009 begin
3010 Par := Parent (V1);
3011 while Nkind (Par) /= N_Full_Type_Declaration loop
3012 if Par = V2 then
3013 return True;
3014 end if;
3016 Par := Parent (Par);
3017 end loop;
3019 return False;
3020 end Nested_In;
3022 -------------------
3023 -- Variant_Depth --
3024 -------------------
3026 function Variant_Depth (N : Node_Id) return Integer is
3027 Depth : Integer;
3028 Par : Node_Id;
3030 begin
3031 Depth := 0;
3032 Par := Parent (N);
3033 while Nkind (Par) /= N_Full_Type_Declaration loop
3034 Depth := Depth + 1;
3035 Par := Parent (Par);
3036 end loop;
3038 return Depth;
3039 end Variant_Depth;
3041 -- Local variables
3043 Deltas : constant List_Id := Component_Associations (N);
3045 Assoc : Node_Id;
3046 Choice : Node_Id;
3047 Comp_Type : Entity_Id := Empty; -- init to avoid warning
3049 -- Start of processing for Resolve_Delta_Record_Aggregate
3051 begin
3052 Variant := Empty;
3054 Assoc := First (Deltas);
3055 while Present (Assoc) loop
3056 Choice := First (Choice_List (Assoc));
3057 while Present (Choice) loop
3058 Comp_Type := Get_Component_Type (Choice);
3060 if Comp_Type /= Any_Type then
3061 Check_Variant (Choice);
3062 end if;
3064 Next (Choice);
3065 end loop;
3067 pragma Assert (Present (Comp_Type));
3068 Analyze_And_Resolve (Expression (Assoc), Comp_Type);
3069 Next (Assoc);
3070 end loop;
3071 end Resolve_Delta_Record_Aggregate;
3073 ---------------------------------
3074 -- Resolve_Extension_Aggregate --
3075 ---------------------------------
3077 -- There are two cases to consider:
3079 -- a) If the ancestor part is a type mark, the components needed are the
3080 -- difference between the components of the expected type and the
3081 -- components of the given type mark.
3083 -- b) If the ancestor part is an expression, it must be unambiguous, and
3084 -- once we have its type we can also compute the needed components as in
3085 -- the previous case. In both cases, if the ancestor type is not the
3086 -- immediate ancestor, we have to build this ancestor recursively.
3088 -- In both cases, discriminants of the ancestor type do not play a role in
3089 -- the resolution of the needed components, because inherited discriminants
3090 -- cannot be used in a type extension. As a result we can compute
3091 -- independently the list of components of the ancestor type and of the
3092 -- expected type.
3094 procedure Resolve_Extension_Aggregate (N : Node_Id; Typ : Entity_Id) is
3095 A : constant Node_Id := Ancestor_Part (N);
3096 A_Type : Entity_Id;
3097 I : Interp_Index;
3098 It : Interp;
3100 function Valid_Limited_Ancestor (Anc : Node_Id) return Boolean;
3101 -- If the type is limited, verify that the ancestor part is a legal
3102 -- expression (aggregate or function call, including 'Input)) that does
3103 -- not require a copy, as specified in 7.5(2).
3105 function Valid_Ancestor_Type return Boolean;
3106 -- Verify that the type of the ancestor part is a non-private ancestor
3107 -- of the expected type, which must be a type extension.
3109 procedure Transform_BIP_Assignment (Typ : Entity_Id);
3110 -- For an extension aggregate whose ancestor part is a build-in-place
3111 -- call returning a nonlimited type, this is used to transform the
3112 -- assignment to the ancestor part to use a temp.
3114 ----------------------------
3115 -- Valid_Limited_Ancestor --
3116 ----------------------------
3118 function Valid_Limited_Ancestor (Anc : Node_Id) return Boolean is
3119 begin
3120 if Is_Entity_Name (Anc) and then Is_Type (Entity (Anc)) then
3121 return True;
3123 -- The ancestor must be a call or an aggregate, but a call may
3124 -- have been expanded into a temporary, so check original node.
3126 elsif Nkind_In (Anc, N_Aggregate,
3127 N_Extension_Aggregate,
3128 N_Function_Call)
3129 then
3130 return True;
3132 elsif Nkind (Original_Node (Anc)) = N_Function_Call then
3133 return True;
3135 elsif Nkind (Anc) = N_Attribute_Reference
3136 and then Attribute_Name (Anc) = Name_Input
3137 then
3138 return True;
3140 elsif Nkind (Anc) = N_Qualified_Expression then
3141 return Valid_Limited_Ancestor (Expression (Anc));
3143 else
3144 return False;
3145 end if;
3146 end Valid_Limited_Ancestor;
3148 -------------------------
3149 -- Valid_Ancestor_Type --
3150 -------------------------
3152 function Valid_Ancestor_Type return Boolean is
3153 Imm_Type : Entity_Id;
3155 begin
3156 Imm_Type := Base_Type (Typ);
3157 while Is_Derived_Type (Imm_Type) loop
3158 if Etype (Imm_Type) = Base_Type (A_Type) then
3159 return True;
3161 -- The base type of the parent type may appear as a private
3162 -- extension if it is declared as such in a parent unit of the
3163 -- current one. For consistency of the subsequent analysis use
3164 -- the partial view for the ancestor part.
3166 elsif Is_Private_Type (Etype (Imm_Type))
3167 and then Present (Full_View (Etype (Imm_Type)))
3168 and then Base_Type (A_Type) = Full_View (Etype (Imm_Type))
3169 then
3170 A_Type := Etype (Imm_Type);
3171 return True;
3173 -- The parent type may be a private extension. The aggregate is
3174 -- legal if the type of the aggregate is an extension of it that
3175 -- is not a private extension.
3177 elsif Is_Private_Type (A_Type)
3178 and then not Is_Private_Type (Imm_Type)
3179 and then Present (Full_View (A_Type))
3180 and then Base_Type (Full_View (A_Type)) = Etype (Imm_Type)
3181 then
3182 return True;
3184 else
3185 Imm_Type := Etype (Base_Type (Imm_Type));
3186 end if;
3187 end loop;
3189 -- If previous loop did not find a proper ancestor, report error
3191 Error_Msg_NE ("expect ancestor type of &", A, Typ);
3192 return False;
3193 end Valid_Ancestor_Type;
3195 ------------------------------
3196 -- Transform_BIP_Assignment --
3197 ------------------------------
3199 procedure Transform_BIP_Assignment (Typ : Entity_Id) is
3200 Loc : constant Source_Ptr := Sloc (N);
3201 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'Y', A);
3202 Obj_Decl : constant Node_Id :=
3203 Make_Object_Declaration (Loc,
3204 Defining_Identifier => Def_Id,
3205 Constant_Present => True,
3206 Object_Definition => New_Occurrence_Of (Typ, Loc),
3207 Expression => A,
3208 Has_Init_Expression => True);
3209 begin
3210 Set_Etype (Def_Id, Typ);
3211 Set_Ancestor_Part (N, New_Occurrence_Of (Def_Id, Loc));
3212 Insert_Action (N, Obj_Decl);
3213 end Transform_BIP_Assignment;
3215 -- Start of processing for Resolve_Extension_Aggregate
3217 begin
3218 -- Analyze the ancestor part and account for the case where it is a
3219 -- parameterless function call.
3221 Analyze (A);
3222 Check_Parameterless_Call (A);
3224 -- In SPARK, the ancestor part cannot be a type mark
3226 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
3227 Check_SPARK_05_Restriction ("ancestor part cannot be a type mark", A);
3229 -- AI05-0115: if the ancestor part is a subtype mark, the ancestor
3230 -- must not have unknown discriminants.
3232 if Has_Unknown_Discriminants (Root_Type (Typ)) then
3233 Error_Msg_NE
3234 ("aggregate not available for type& whose ancestor "
3235 & "has unknown discriminants", N, Typ);
3236 end if;
3237 end if;
3239 if not Is_Tagged_Type (Typ) then
3240 Error_Msg_N ("type of extension aggregate must be tagged", N);
3241 return;
3243 elsif Is_Limited_Type (Typ) then
3245 -- Ada 2005 (AI-287): Limited aggregates are allowed
3247 if Ada_Version < Ada_2005 then
3248 Error_Msg_N ("aggregate type cannot be limited", N);
3249 Explain_Limited_Type (Typ, N);
3250 return;
3252 elsif Valid_Limited_Ancestor (A) then
3253 null;
3255 else
3256 Error_Msg_N
3257 ("limited ancestor part must be aggregate or function call", A);
3258 end if;
3260 elsif Is_Class_Wide_Type (Typ) then
3261 Error_Msg_N ("aggregate cannot be of a class-wide type", N);
3262 return;
3263 end if;
3265 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
3266 A_Type := Get_Full_View (Entity (A));
3268 if Valid_Ancestor_Type then
3269 Set_Entity (A, A_Type);
3270 Set_Etype (A, A_Type);
3272 Validate_Ancestor_Part (N);
3273 Resolve_Record_Aggregate (N, Typ);
3274 end if;
3276 elsif Nkind (A) /= N_Aggregate then
3277 if Is_Overloaded (A) then
3278 A_Type := Any_Type;
3280 Get_First_Interp (A, I, It);
3281 while Present (It.Typ) loop
3283 -- Consider limited interpretations if Ada 2005 or higher
3285 if Is_Tagged_Type (It.Typ)
3286 and then (Ada_Version >= Ada_2005
3287 or else not Is_Limited_Type (It.Typ))
3288 then
3289 if A_Type /= Any_Type then
3290 Error_Msg_N ("cannot resolve expression", A);
3291 return;
3292 else
3293 A_Type := It.Typ;
3294 end if;
3295 end if;
3297 Get_Next_Interp (I, It);
3298 end loop;
3300 if A_Type = Any_Type then
3301 if Ada_Version >= Ada_2005 then
3302 Error_Msg_N
3303 ("ancestor part must be of a tagged type", A);
3304 else
3305 Error_Msg_N
3306 ("ancestor part must be of a nonlimited tagged type", A);
3307 end if;
3309 return;
3310 end if;
3312 else
3313 A_Type := Etype (A);
3314 end if;
3316 if Valid_Ancestor_Type then
3317 Resolve (A, A_Type);
3318 Check_Unset_Reference (A);
3319 Check_Non_Static_Context (A);
3321 -- The aggregate is illegal if the ancestor expression is a call
3322 -- to a function with a limited unconstrained result, unless the
3323 -- type of the aggregate is a null extension. This restriction
3324 -- was added in AI05-67 to simplify implementation.
3326 if Nkind (A) = N_Function_Call
3327 and then Is_Limited_Type (A_Type)
3328 and then not Is_Null_Extension (Typ)
3329 and then not Is_Constrained (A_Type)
3330 then
3331 Error_Msg_N
3332 ("type of limited ancestor part must be constrained", A);
3334 -- Reject the use of CPP constructors that leave objects partially
3335 -- initialized. For example:
3337 -- type CPP_Root is tagged limited record ...
3338 -- pragma Import (CPP, CPP_Root);
3340 -- type CPP_DT is new CPP_Root and Iface ...
3341 -- pragma Import (CPP, CPP_DT);
3343 -- type Ada_DT is new CPP_DT with ...
3345 -- Obj : Ada_DT := Ada_DT'(New_CPP_Root with others => <>);
3347 -- Using the constructor of CPP_Root the slots of the dispatch
3348 -- table of CPP_DT cannot be set, and the secondary tag of
3349 -- CPP_DT is unknown.
3351 elsif Nkind (A) = N_Function_Call
3352 and then Is_CPP_Constructor_Call (A)
3353 and then Enclosing_CPP_Parent (Typ) /= A_Type
3354 then
3355 Error_Msg_NE
3356 ("??must use 'C'P'P constructor for type &", A,
3357 Enclosing_CPP_Parent (Typ));
3359 -- The following call is not needed if the previous warning
3360 -- is promoted to an error.
3362 Resolve_Record_Aggregate (N, Typ);
3364 elsif Is_Class_Wide_Type (Etype (A))
3365 and then Nkind (Original_Node (A)) = N_Function_Call
3366 then
3367 -- If the ancestor part is a dispatching call, it appears
3368 -- statically to be a legal ancestor, but it yields any member
3369 -- of the class, and it is not possible to determine whether
3370 -- it is an ancestor of the extension aggregate (much less
3371 -- which ancestor). It is not possible to determine the
3372 -- components of the extension part.
3374 -- This check implements AI-306, which in fact was motivated by
3375 -- an AdaCore query to the ARG after this test was added.
3377 Error_Msg_N ("ancestor part must be statically tagged", A);
3378 else
3379 -- We are using the build-in-place protocol, but we can't build
3380 -- in place, because we need to call the function before
3381 -- allocating the aggregate. Could do better for null
3382 -- extensions, and maybe for nondiscriminated types.
3383 -- This is wrong for limited, but those were wrong already.
3385 if not Is_Limited_View (A_Type)
3386 and then Is_Build_In_Place_Function_Call (A)
3387 then
3388 Transform_BIP_Assignment (A_Type);
3389 end if;
3391 Resolve_Record_Aggregate (N, Typ);
3392 end if;
3393 end if;
3395 else
3396 Error_Msg_N ("no unique type for this aggregate", A);
3397 end if;
3399 Check_Function_Writable_Actuals (N);
3400 end Resolve_Extension_Aggregate;
3402 ------------------------------
3403 -- Resolve_Record_Aggregate --
3404 ------------------------------
3406 procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id) is
3407 New_Assoc_List : constant List_Id := New_List;
3408 -- New_Assoc_List is the newly built list of N_Component_Association
3409 -- nodes.
3411 Others_Etype : Entity_Id := Empty;
3412 -- This variable is used to save the Etype of the last record component
3413 -- that takes its value from the others choice. Its purpose is:
3415 -- (a) make sure the others choice is useful
3417 -- (b) make sure the type of all the components whose value is
3418 -- subsumed by the others choice are the same.
3420 -- This variable is updated as a side effect of function Get_Value.
3422 Box_Node : Node_Id := Empty;
3423 Is_Box_Present : Boolean := False;
3424 Others_Box : Integer := 0;
3425 -- Ada 2005 (AI-287): Variables used in case of default initialization
3426 -- to provide a functionality similar to Others_Etype. Box_Present
3427 -- indicates that the component takes its default initialization;
3428 -- Others_Box counts the number of components of the current aggregate
3429 -- (which may be a sub-aggregate of a larger one) that are default-
3430 -- initialized. A value of One indicates that an others_box is present.
3431 -- Any larger value indicates that the others_box is not redundant.
3432 -- These variables, similar to Others_Etype, are also updated as a side
3433 -- effect of function Get_Value. Box_Node is used to place a warning on
3434 -- a redundant others_box.
3436 procedure Add_Association
3437 (Component : Entity_Id;
3438 Expr : Node_Id;
3439 Assoc_List : List_Id;
3440 Is_Box_Present : Boolean := False);
3441 -- Builds a new N_Component_Association node which associates Component
3442 -- to expression Expr and adds it to the association list being built,
3443 -- either New_Assoc_List, or the association being built for an inner
3444 -- aggregate.
3446 procedure Add_Discriminant_Values
3447 (New_Aggr : Node_Id;
3448 Assoc_List : List_Id);
3449 -- The constraint to a component may be given by a discriminant of the
3450 -- enclosing type, in which case we have to retrieve its value, which is
3451 -- part of the enclosing aggregate. Assoc_List provides the discriminant
3452 -- associations of the current type or of some enclosing record.
3454 function Discriminant_Present (Input_Discr : Entity_Id) return Boolean;
3455 -- If aggregate N is a regular aggregate this routine will return True.
3456 -- Otherwise, if N is an extension aggregate, then Input_Discr denotes
3457 -- a discriminant whose value may already have been specified by N's
3458 -- ancestor part. This routine checks whether this is indeed the case
3459 -- and if so returns False, signaling that no value for Input_Discr
3460 -- should appear in N's aggregate part. Also, in this case, the routine
3461 -- appends to New_Assoc_List the discriminant value specified in the
3462 -- ancestor part.
3464 -- If the aggregate is in a context with expansion delayed, it will be
3465 -- reanalyzed. The inherited discriminant values must not be reinserted
3466 -- in the component list to prevent spurious errors, but they must be
3467 -- present on first analysis to build the proper subtype indications.
3468 -- The flag Inherited_Discriminant is used to prevent the re-insertion.
3470 function Find_Private_Ancestor (Typ : Entity_Id) return Entity_Id;
3471 -- AI05-0115: Find earlier ancestor in the derivation chain that is
3472 -- derived from private view Typ. Whether the aggregate is legal depends
3473 -- on the current visibility of the type as well as that of the parent
3474 -- of the ancestor.
3476 function Get_Value
3477 (Compon : Node_Id;
3478 From : List_Id;
3479 Consider_Others_Choice : Boolean := False) return Node_Id;
3480 -- Given a record component stored in parameter Compon, this function
3481 -- returns its value as it appears in the list From, which is a list
3482 -- of N_Component_Association nodes.
3484 -- If no component association has a choice for the searched component,
3485 -- the value provided by the others choice is returned, if there is one,
3486 -- and Consider_Others_Choice is set to true. Otherwise Empty is
3487 -- returned. If there is more than one component association giving a
3488 -- value for the searched record component, an error message is emitted
3489 -- and the first found value is returned.
3491 -- If Consider_Others_Choice is set and the returned expression comes
3492 -- from the others choice, then Others_Etype is set as a side effect.
3493 -- An error message is emitted if the components taking their value from
3494 -- the others choice do not have same type.
3496 procedure Propagate_Discriminants
3497 (Aggr : Node_Id;
3498 Assoc_List : List_Id);
3499 -- Nested components may themselves be discriminated types constrained
3500 -- by outer discriminants, whose values must be captured before the
3501 -- aggregate is expanded into assignments.
3503 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Entity_Id);
3504 -- Analyzes and resolves expression Expr against the Etype of the
3505 -- Component. This routine also applies all appropriate checks to Expr.
3506 -- It finally saves a Expr in the newly created association list that
3507 -- will be attached to the final record aggregate. Note that if the
3508 -- Parent pointer of Expr is not set then Expr was produced with a
3509 -- New_Copy_Tree or some such.
3511 procedure Rewrite_Range (Root_Type : Entity_Id; Rge : Node_Id);
3512 -- Rewrite a range node Rge when its bounds refer to non-stored
3513 -- discriminants from Root_Type, to replace them with the stored
3514 -- discriminant values. This is required in GNATprove mode, and is
3515 -- adopted in all modes to avoid special-casing GNATprove mode.
3517 ---------------------
3518 -- Add_Association --
3519 ---------------------
3521 procedure Add_Association
3522 (Component : Entity_Id;
3523 Expr : Node_Id;
3524 Assoc_List : List_Id;
3525 Is_Box_Present : Boolean := False)
3527 Choice_List : constant List_Id := New_List;
3528 Loc : Source_Ptr;
3530 begin
3531 -- If this is a box association the expression is missing, so use the
3532 -- Sloc of the aggregate itself for the new association.
3534 if Present (Expr) then
3535 Loc := Sloc (Expr);
3536 else
3537 Loc := Sloc (N);
3538 end if;
3540 Append_To (Choice_List, New_Occurrence_Of (Component, Loc));
3542 Append_To (Assoc_List,
3543 Make_Component_Association (Loc,
3544 Choices => Choice_List,
3545 Expression => Expr,
3546 Box_Present => Is_Box_Present));
3547 end Add_Association;
3549 -----------------------------
3550 -- Add_Discriminant_Values --
3551 -----------------------------
3553 procedure Add_Discriminant_Values
3554 (New_Aggr : Node_Id;
3555 Assoc_List : List_Id)
3557 Assoc : Node_Id;
3558 Discr : Entity_Id;
3559 Discr_Elmt : Elmt_Id;
3560 Discr_Val : Node_Id;
3561 Val : Entity_Id;
3563 begin
3564 Discr := First_Discriminant (Etype (New_Aggr));
3565 Discr_Elmt := First_Elmt (Discriminant_Constraint (Etype (New_Aggr)));
3566 while Present (Discr_Elmt) loop
3567 Discr_Val := Node (Discr_Elmt);
3569 -- If the constraint is given by a discriminant then it is a
3570 -- discriminant of an enclosing record, and its value has already
3571 -- been placed in the association list.
3573 if Is_Entity_Name (Discr_Val)
3574 and then Ekind (Entity (Discr_Val)) = E_Discriminant
3575 then
3576 Val := Entity (Discr_Val);
3578 Assoc := First (Assoc_List);
3579 while Present (Assoc) loop
3580 if Present (Entity (First (Choices (Assoc))))
3581 and then Entity (First (Choices (Assoc))) = Val
3582 then
3583 Discr_Val := Expression (Assoc);
3584 exit;
3585 end if;
3587 Next (Assoc);
3588 end loop;
3589 end if;
3591 Add_Association
3592 (Discr, New_Copy_Tree (Discr_Val),
3593 Component_Associations (New_Aggr));
3595 -- If the discriminant constraint is a current instance, mark the
3596 -- current aggregate so that the self-reference can be expanded
3597 -- later. The constraint may refer to the subtype of aggregate, so
3598 -- use base type for comparison.
3600 if Nkind (Discr_Val) = N_Attribute_Reference
3601 and then Is_Entity_Name (Prefix (Discr_Val))
3602 and then Is_Type (Entity (Prefix (Discr_Val)))
3603 and then Base_Type (Etype (N)) = Entity (Prefix (Discr_Val))
3604 then
3605 Set_Has_Self_Reference (N);
3606 end if;
3608 Next_Elmt (Discr_Elmt);
3609 Next_Discriminant (Discr);
3610 end loop;
3611 end Add_Discriminant_Values;
3613 --------------------------
3614 -- Discriminant_Present --
3615 --------------------------
3617 function Discriminant_Present (Input_Discr : Entity_Id) return Boolean is
3618 Regular_Aggr : constant Boolean := Nkind (N) /= N_Extension_Aggregate;
3620 Ancestor_Is_Subtyp : Boolean;
3622 Loc : Source_Ptr;
3624 Ancestor : Node_Id;
3625 Ancestor_Typ : Entity_Id;
3626 Comp_Assoc : Node_Id;
3627 Discr : Entity_Id;
3628 Discr_Expr : Node_Id;
3629 Discr_Val : Elmt_Id := No_Elmt;
3630 Orig_Discr : Entity_Id;
3632 begin
3633 if Regular_Aggr then
3634 return True;
3635 end if;
3637 -- Check whether inherited discriminant values have already been
3638 -- inserted in the aggregate. This will be the case if we are
3639 -- re-analyzing an aggregate whose expansion was delayed.
3641 if Present (Component_Associations (N)) then
3642 Comp_Assoc := First (Component_Associations (N));
3643 while Present (Comp_Assoc) loop
3644 if Inherited_Discriminant (Comp_Assoc) then
3645 return True;
3646 end if;
3648 Next (Comp_Assoc);
3649 end loop;
3650 end if;
3652 Ancestor := Ancestor_Part (N);
3653 Ancestor_Typ := Etype (Ancestor);
3654 Loc := Sloc (Ancestor);
3656 -- For a private type with unknown discriminants, use the underlying
3657 -- record view if it is available.
3659 if Has_Unknown_Discriminants (Ancestor_Typ)
3660 and then Present (Full_View (Ancestor_Typ))
3661 and then Present (Underlying_Record_View (Full_View (Ancestor_Typ)))
3662 then
3663 Ancestor_Typ := Underlying_Record_View (Full_View (Ancestor_Typ));
3664 end if;
3666 Ancestor_Is_Subtyp :=
3667 Is_Entity_Name (Ancestor) and then Is_Type (Entity (Ancestor));
3669 -- If the ancestor part has no discriminants clearly N's aggregate
3670 -- part must provide a value for Discr.
3672 if not Has_Discriminants (Ancestor_Typ) then
3673 return True;
3675 -- If the ancestor part is an unconstrained subtype mark then the
3676 -- Discr must be present in N's aggregate part.
3678 elsif Ancestor_Is_Subtyp
3679 and then not Is_Constrained (Entity (Ancestor))
3680 then
3681 return True;
3682 end if;
3684 -- Now look to see if Discr was specified in the ancestor part
3686 if Ancestor_Is_Subtyp then
3687 Discr_Val :=
3688 First_Elmt (Discriminant_Constraint (Entity (Ancestor)));
3689 end if;
3691 Orig_Discr := Original_Record_Component (Input_Discr);
3693 Discr := First_Discriminant (Ancestor_Typ);
3694 while Present (Discr) loop
3696 -- If Ancestor has already specified Disc value then insert its
3697 -- value in the final aggregate.
3699 if Original_Record_Component (Discr) = Orig_Discr then
3700 if Ancestor_Is_Subtyp then
3701 Discr_Expr := New_Copy_Tree (Node (Discr_Val));
3702 else
3703 Discr_Expr :=
3704 Make_Selected_Component (Loc,
3705 Prefix => Duplicate_Subexpr (Ancestor),
3706 Selector_Name => New_Occurrence_Of (Input_Discr, Loc));
3707 end if;
3709 Resolve_Aggr_Expr (Discr_Expr, Input_Discr);
3710 Set_Inherited_Discriminant (Last (New_Assoc_List));
3711 return False;
3712 end if;
3714 Next_Discriminant (Discr);
3716 if Ancestor_Is_Subtyp then
3717 Next_Elmt (Discr_Val);
3718 end if;
3719 end loop;
3721 return True;
3722 end Discriminant_Present;
3724 ---------------------------
3725 -- Find_Private_Ancestor --
3726 ---------------------------
3728 function Find_Private_Ancestor (Typ : Entity_Id) return Entity_Id is
3729 Par : Entity_Id;
3731 begin
3732 Par := Typ;
3733 loop
3734 if Has_Private_Ancestor (Par)
3735 and then not Has_Private_Ancestor (Etype (Base_Type (Par)))
3736 then
3737 return Par;
3739 elsif not Is_Derived_Type (Par) then
3740 return Empty;
3742 else
3743 Par := Etype (Base_Type (Par));
3744 end if;
3745 end loop;
3746 end Find_Private_Ancestor;
3748 ---------------
3749 -- Get_Value --
3750 ---------------
3752 function Get_Value
3753 (Compon : Node_Id;
3754 From : List_Id;
3755 Consider_Others_Choice : Boolean := False) return Node_Id
3757 Typ : constant Entity_Id := Etype (Compon);
3758 Assoc : Node_Id;
3759 Expr : Node_Id := Empty;
3760 Selector_Name : Node_Id;
3762 begin
3763 Is_Box_Present := False;
3765 if No (From) then
3766 return Empty;
3767 end if;
3769 Assoc := First (From);
3770 while Present (Assoc) loop
3771 Selector_Name := First (Choices (Assoc));
3772 while Present (Selector_Name) loop
3773 if Nkind (Selector_Name) = N_Others_Choice then
3774 if Consider_Others_Choice and then No (Expr) then
3776 -- We need to duplicate the expression for each
3777 -- successive component covered by the others choice.
3778 -- This is redundant if the others_choice covers only
3779 -- one component (small optimization possible???), but
3780 -- indispensable otherwise, because each one must be
3781 -- expanded individually to preserve side effects.
3783 -- Ada 2005 (AI-287): In case of default initialization
3784 -- of components, we duplicate the corresponding default
3785 -- expression (from the record type declaration). The
3786 -- copy must carry the sloc of the association (not the
3787 -- original expression) to prevent spurious elaboration
3788 -- checks when the default includes function calls.
3790 if Box_Present (Assoc) then
3791 Others_Box := Others_Box + 1;
3792 Is_Box_Present := True;
3794 if Expander_Active then
3795 return
3796 New_Copy_Tree_And_Copy_Dimensions
3797 (Expression (Parent (Compon)),
3798 New_Sloc => Sloc (Assoc));
3799 else
3800 return Expression (Parent (Compon));
3801 end if;
3803 else
3804 if Present (Others_Etype)
3805 and then Base_Type (Others_Etype) /= Base_Type (Typ)
3806 then
3807 -- If the components are of an anonymous access
3808 -- type they are distinct, but this is legal in
3809 -- Ada 2012 as long as designated types match.
3811 if (Ekind (Typ) = E_Anonymous_Access_Type
3812 or else Ekind (Typ) =
3813 E_Anonymous_Access_Subprogram_Type)
3814 and then Designated_Type (Typ) =
3815 Designated_Type (Others_Etype)
3816 then
3817 null;
3818 else
3819 Error_Msg_N
3820 ("components in OTHERS choice must have same "
3821 & "type", Selector_Name);
3822 end if;
3823 end if;
3825 Others_Etype := Typ;
3827 -- Copy the expression so that it is resolved
3828 -- independently for each component, This is needed
3829 -- for accessibility checks on compoents of anonymous
3830 -- access types, even in compile_only mode.
3832 if not Inside_A_Generic then
3834 -- In ASIS mode, preanalyze the expression in an
3835 -- others association before making copies for
3836 -- separate resolution and accessibility checks.
3837 -- This ensures that the type of the expression is
3838 -- available to ASIS in all cases, in particular if
3839 -- the expression is itself an aggregate.
3841 if ASIS_Mode then
3842 Preanalyze_And_Resolve (Expression (Assoc), Typ);
3843 end if;
3845 return
3846 New_Copy_Tree_And_Copy_Dimensions
3847 (Expression (Assoc));
3849 else
3850 return Expression (Assoc);
3851 end if;
3852 end if;
3853 end if;
3855 elsif Chars (Compon) = Chars (Selector_Name) then
3856 if No (Expr) then
3858 -- Ada 2005 (AI-231)
3860 if Ada_Version >= Ada_2005
3861 and then Known_Null (Expression (Assoc))
3862 then
3863 Check_Can_Never_Be_Null (Compon, Expression (Assoc));
3864 end if;
3866 -- We need to duplicate the expression when several
3867 -- components are grouped together with a "|" choice.
3868 -- For instance "filed1 | filed2 => Expr"
3870 -- Ada 2005 (AI-287)
3872 if Box_Present (Assoc) then
3873 Is_Box_Present := True;
3875 -- Duplicate the default expression of the component
3876 -- from the record type declaration, so a new copy
3877 -- can be attached to the association.
3879 -- Note that we always copy the default expression,
3880 -- even when the association has a single choice, in
3881 -- order to create a proper association for the
3882 -- expanded aggregate.
3884 -- Component may have no default, in which case the
3885 -- expression is empty and the component is default-
3886 -- initialized, but an association for the component
3887 -- exists, and it is not covered by an others clause.
3889 -- Scalar and private types have no initialization
3890 -- procedure, so they remain uninitialized. If the
3891 -- target of the aggregate is a constant this
3892 -- deserves a warning.
3894 if No (Expression (Parent (Compon)))
3895 and then not Has_Non_Null_Base_Init_Proc (Typ)
3896 and then not Has_Aspect (Typ, Aspect_Default_Value)
3897 and then not Is_Concurrent_Type (Typ)
3898 and then Nkind (Parent (N)) = N_Object_Declaration
3899 and then Constant_Present (Parent (N))
3900 then
3901 Error_Msg_Node_2 := Typ;
3902 Error_Msg_NE
3903 ("component&? of type& is uninitialized",
3904 Assoc, Selector_Name);
3906 -- An additional reminder if the component type
3907 -- is a generic formal.
3909 if Is_Generic_Type (Base_Type (Typ)) then
3910 Error_Msg_NE
3911 ("\instance should provide actual type with "
3912 & "initialization for&", Assoc, Typ);
3913 end if;
3914 end if;
3916 return
3917 New_Copy_Tree_And_Copy_Dimensions
3918 (Expression (Parent (Compon)));
3920 else
3921 if Present (Next (Selector_Name)) then
3922 Expr := New_Copy_Tree_And_Copy_Dimensions
3923 (Expression (Assoc));
3924 else
3925 Expr := Expression (Assoc);
3926 end if;
3927 end if;
3929 Generate_Reference (Compon, Selector_Name, 'm');
3931 else
3932 Error_Msg_NE
3933 ("more than one value supplied for &",
3934 Selector_Name, Compon);
3936 end if;
3937 end if;
3939 Next (Selector_Name);
3940 end loop;
3942 Next (Assoc);
3943 end loop;
3945 return Expr;
3946 end Get_Value;
3948 -----------------------------
3949 -- Propagate_Discriminants --
3950 -----------------------------
3952 procedure Propagate_Discriminants
3953 (Aggr : Node_Id;
3954 Assoc_List : List_Id)
3956 Loc : constant Source_Ptr := Sloc (N);
3958 Needs_Box : Boolean := False;
3960 procedure Process_Component (Comp : Entity_Id);
3961 -- Add one component with a box association to the inner aggregate,
3962 -- and recurse if component is itself composite.
3964 -----------------------
3965 -- Process_Component --
3966 -----------------------
3968 procedure Process_Component (Comp : Entity_Id) is
3969 T : constant Entity_Id := Etype (Comp);
3970 New_Aggr : Node_Id;
3972 begin
3973 if Is_Record_Type (T) and then Has_Discriminants (T) then
3974 New_Aggr := Make_Aggregate (Loc, New_List, New_List);
3975 Set_Etype (New_Aggr, T);
3977 Add_Association
3978 (Comp, New_Aggr, Component_Associations (Aggr));
3980 -- Collect discriminant values and recurse
3982 Add_Discriminant_Values (New_Aggr, Assoc_List);
3983 Propagate_Discriminants (New_Aggr, Assoc_List);
3985 else
3986 Needs_Box := True;
3987 end if;
3988 end Process_Component;
3990 -- Local variables
3992 Aggr_Type : constant Entity_Id := Base_Type (Etype (Aggr));
3993 Components : constant Elist_Id := New_Elmt_List;
3994 Def_Node : constant Node_Id :=
3995 Type_Definition (Declaration_Node (Aggr_Type));
3997 Comp : Node_Id;
3998 Comp_Elmt : Elmt_Id;
3999 Errors : Boolean;
4001 -- Start of processing for Propagate_Discriminants
4003 begin
4004 -- The component type may be a variant type. Collect the components
4005 -- that are ruled by the known values of the discriminants. Their
4006 -- values have already been inserted into the component list of the
4007 -- current aggregate.
4009 if Nkind (Def_Node) = N_Record_Definition
4010 and then Present (Component_List (Def_Node))
4011 and then Present (Variant_Part (Component_List (Def_Node)))
4012 then
4013 Gather_Components (Aggr_Type,
4014 Component_List (Def_Node),
4015 Governed_By => Component_Associations (Aggr),
4016 Into => Components,
4017 Report_Errors => Errors);
4019 Comp_Elmt := First_Elmt (Components);
4020 while Present (Comp_Elmt) loop
4021 if Ekind (Node (Comp_Elmt)) /= E_Discriminant then
4022 Process_Component (Node (Comp_Elmt));
4023 end if;
4025 Next_Elmt (Comp_Elmt);
4026 end loop;
4028 -- No variant part, iterate over all components
4030 else
4031 Comp := First_Component (Etype (Aggr));
4032 while Present (Comp) loop
4033 Process_Component (Comp);
4034 Next_Component (Comp);
4035 end loop;
4036 end if;
4038 if Needs_Box then
4039 Append_To (Component_Associations (Aggr),
4040 Make_Component_Association (Loc,
4041 Choices => New_List (Make_Others_Choice (Loc)),
4042 Expression => Empty,
4043 Box_Present => True));
4044 end if;
4045 end Propagate_Discriminants;
4047 -----------------------
4048 -- Resolve_Aggr_Expr --
4049 -----------------------
4051 procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Entity_Id) is
4052 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean;
4053 -- If the expression is an aggregate (possibly qualified) then its
4054 -- expansion is delayed until the enclosing aggregate is expanded
4055 -- into assignments. In that case, do not generate checks on the
4056 -- expression, because they will be generated later, and will other-
4057 -- wise force a copy (to remove side effects) that would leave a
4058 -- dynamic-sized aggregate in the code, something that gigi cannot
4059 -- handle.
4061 ---------------------------
4062 -- Has_Expansion_Delayed --
4063 ---------------------------
4065 function Has_Expansion_Delayed (Expr : Node_Id) return Boolean is
4066 begin
4067 return
4068 (Nkind_In (Expr, N_Aggregate, N_Extension_Aggregate)
4069 and then Present (Etype (Expr))
4070 and then Is_Record_Type (Etype (Expr))
4071 and then Expansion_Delayed (Expr))
4072 or else
4073 (Nkind (Expr) = N_Qualified_Expression
4074 and then Has_Expansion_Delayed (Expression (Expr)));
4075 end Has_Expansion_Delayed;
4077 -- Local variables
4079 Expr_Type : Entity_Id := Empty;
4080 New_C : Entity_Id := Component;
4081 New_Expr : Node_Id;
4083 Relocate : Boolean;
4084 -- Set to True if the resolved Expr node needs to be relocated when
4085 -- attached to the newly created association list. This node need not
4086 -- be relocated if its parent pointer is not set. In fact in this
4087 -- case Expr is the output of a New_Copy_Tree call. If Relocate is
4088 -- True then we have analyzed the expression node in the original
4089 -- aggregate and hence it needs to be relocated when moved over to
4090 -- the new association list.
4092 -- Start of processing for Resolve_Aggr_Expr
4094 begin
4095 -- If the type of the component is elementary or the type of the
4096 -- aggregate does not contain discriminants, use the type of the
4097 -- component to resolve Expr.
4099 if Is_Elementary_Type (Etype (Component))
4100 or else not Has_Discriminants (Etype (N))
4101 then
4102 Expr_Type := Etype (Component);
4104 -- Otherwise we have to pick up the new type of the component from
4105 -- the new constrained subtype of the aggregate. In fact components
4106 -- which are of a composite type might be constrained by a
4107 -- discriminant, and we want to resolve Expr against the subtype were
4108 -- all discriminant occurrences are replaced with their actual value.
4110 else
4111 New_C := First_Component (Etype (N));
4112 while Present (New_C) loop
4113 if Chars (New_C) = Chars (Component) then
4114 Expr_Type := Etype (New_C);
4115 exit;
4116 end if;
4118 Next_Component (New_C);
4119 end loop;
4121 pragma Assert (Present (Expr_Type));
4123 -- For each range in an array type where a discriminant has been
4124 -- replaced with the constraint, check that this range is within
4125 -- the range of the base type. This checks is done in the init
4126 -- proc for regular objects, but has to be done here for
4127 -- aggregates since no init proc is called for them.
4129 if Is_Array_Type (Expr_Type) then
4130 declare
4131 Index : Node_Id;
4132 -- Range of the current constrained index in the array
4134 Orig_Index : Node_Id := First_Index (Etype (Component));
4135 -- Range corresponding to the range Index above in the
4136 -- original unconstrained record type. The bounds of this
4137 -- range may be governed by discriminants.
4139 Unconstr_Index : Node_Id := First_Index (Etype (Expr_Type));
4140 -- Range corresponding to the range Index above for the
4141 -- unconstrained array type. This range is needed to apply
4142 -- range checks.
4144 begin
4145 Index := First_Index (Expr_Type);
4146 while Present (Index) loop
4147 if Depends_On_Discriminant (Orig_Index) then
4148 Apply_Range_Check (Index, Etype (Unconstr_Index));
4149 end if;
4151 Next_Index (Index);
4152 Next_Index (Orig_Index);
4153 Next_Index (Unconstr_Index);
4154 end loop;
4155 end;
4156 end if;
4157 end if;
4159 -- If the Parent pointer of Expr is not set, Expr is an expression
4160 -- duplicated by New_Tree_Copy (this happens for record aggregates
4161 -- that look like (Field1 | Filed2 => Expr) or (others => Expr)).
4162 -- Such a duplicated expression must be attached to the tree
4163 -- before analysis and resolution to enforce the rule that a tree
4164 -- fragment should never be analyzed or resolved unless it is
4165 -- attached to the current compilation unit.
4167 if No (Parent (Expr)) then
4168 Set_Parent (Expr, N);
4169 Relocate := False;
4170 else
4171 Relocate := True;
4172 end if;
4174 Analyze_And_Resolve (Expr, Expr_Type);
4175 Check_Expr_OK_In_Limited_Aggregate (Expr);
4176 Check_Non_Static_Context (Expr);
4177 Check_Unset_Reference (Expr);
4179 -- Check wrong use of class-wide types
4181 if Is_Class_Wide_Type (Etype (Expr)) then
4182 Error_Msg_N ("dynamically tagged expression not allowed", Expr);
4183 end if;
4185 if not Has_Expansion_Delayed (Expr) then
4186 Aggregate_Constraint_Checks (Expr, Expr_Type);
4187 end if;
4189 -- If an aggregate component has a type with predicates, an explicit
4190 -- predicate check must be applied, as for an assignment statement,
4191 -- because the aggegate might not be expanded into individual
4192 -- component assignments.
4194 if Present (Predicate_Function (Expr_Type))
4195 and then Analyzed (Expr)
4196 then
4197 Apply_Predicate_Check (Expr, Expr_Type);
4198 end if;
4200 if Raises_Constraint_Error (Expr) then
4201 Set_Raises_Constraint_Error (N);
4202 end if;
4204 -- If the expression has been marked as requiring a range check, then
4205 -- generate it here. It's a bit odd to be generating such checks in
4206 -- the analyzer, but harmless since Generate_Range_Check does nothing
4207 -- (other than making sure Do_Range_Check is set) if the expander is
4208 -- not active.
4210 if Do_Range_Check (Expr) then
4211 Generate_Range_Check (Expr, Expr_Type, CE_Range_Check_Failed);
4212 end if;
4214 -- Add association Component => Expr if the caller requests it
4216 if Relocate then
4217 New_Expr := Relocate_Node (Expr);
4219 -- Since New_Expr is not gonna be analyzed later on, we need to
4220 -- propagate here the dimensions form Expr to New_Expr.
4222 Copy_Dimensions (Expr, New_Expr);
4224 else
4225 New_Expr := Expr;
4226 end if;
4228 Add_Association (New_C, New_Expr, New_Assoc_List);
4229 end Resolve_Aggr_Expr;
4231 -------------------
4232 -- Rewrite_Range --
4233 -------------------
4235 procedure Rewrite_Range (Root_Type : Entity_Id; Rge : Node_Id) is
4236 procedure Rewrite_Bound
4237 (Bound : Node_Id;
4238 Disc : Entity_Id;
4239 Expr_Disc : Node_Id);
4240 -- Rewrite a bound of the range Bound, when it is equal to the
4241 -- non-stored discriminant Disc, into the stored discriminant
4242 -- value Expr_Disc.
4244 -------------------
4245 -- Rewrite_Bound --
4246 -------------------
4248 procedure Rewrite_Bound
4249 (Bound : Node_Id;
4250 Disc : Entity_Id;
4251 Expr_Disc : Node_Id)
4253 begin
4254 if Nkind (Bound) = N_Identifier
4255 and then Entity (Bound) = Disc
4256 then
4257 Rewrite (Bound, New_Copy_Tree (Expr_Disc));
4258 end if;
4259 end Rewrite_Bound;
4261 -- Local variables
4263 Low, High : Node_Id;
4264 Disc : Entity_Id;
4265 Expr_Disc : Elmt_Id;
4267 -- Start of processing for Rewrite_Range
4269 begin
4270 if Has_Discriminants (Root_Type)
4271 and then Nkind (Rge) = N_Range
4272 then
4273 Low := Low_Bound (Rge);
4274 High := High_Bound (Rge);
4276 Disc := First_Discriminant (Root_Type);
4277 Expr_Disc := First_Elmt (Stored_Constraint (Etype (N)));
4278 while Present (Disc) loop
4279 Rewrite_Bound (Low, Disc, Node (Expr_Disc));
4280 Rewrite_Bound (High, Disc, Node (Expr_Disc));
4281 Next_Discriminant (Disc);
4282 Next_Elmt (Expr_Disc);
4283 end loop;
4284 end if;
4285 end Rewrite_Range;
4287 -- Local variables
4289 Components : constant Elist_Id := New_Elmt_List;
4290 -- Components is the list of the record components whose value must be
4291 -- provided in the aggregate. This list does include discriminants.
4293 Component : Entity_Id;
4294 Component_Elmt : Elmt_Id;
4295 Expr : Node_Id;
4296 Positional_Expr : Node_Id;
4298 -- Start of processing for Resolve_Record_Aggregate
4300 begin
4301 -- A record aggregate is restricted in SPARK:
4303 -- Each named association can have only a single choice.
4304 -- OTHERS cannot be used.
4305 -- Positional and named associations cannot be mixed.
4307 if Present (Component_Associations (N))
4308 and then Present (First (Component_Associations (N)))
4309 then
4310 if Present (Expressions (N)) then
4311 Check_SPARK_05_Restriction
4312 ("named association cannot follow positional one",
4313 First (Choices (First (Component_Associations (N)))));
4314 end if;
4316 declare
4317 Assoc : Node_Id;
4319 begin
4320 Assoc := First (Component_Associations (N));
4321 while Present (Assoc) loop
4322 if Nkind (Assoc) = N_Iterated_Component_Association then
4323 Error_Msg_N
4324 ("iterated component association can only appear in an "
4325 & "array aggregate", N);
4326 raise Unrecoverable_Error;
4328 else
4329 if List_Length (Choices (Assoc)) > 1 then
4330 Check_SPARK_05_Restriction
4331 ("component association in record aggregate must "
4332 & "contain a single choice", Assoc);
4333 end if;
4335 if Nkind (First (Choices (Assoc))) = N_Others_Choice then
4336 Check_SPARK_05_Restriction
4337 ("record aggregate cannot contain OTHERS", Assoc);
4338 end if;
4339 end if;
4341 Assoc := Next (Assoc);
4342 end loop;
4343 end;
4344 end if;
4346 -- We may end up calling Duplicate_Subexpr on expressions that are
4347 -- attached to New_Assoc_List. For this reason we need to attach it
4348 -- to the tree by setting its parent pointer to N. This parent point
4349 -- will change in STEP 8 below.
4351 Set_Parent (New_Assoc_List, N);
4353 -- STEP 1: abstract type and null record verification
4355 if Is_Abstract_Type (Typ) then
4356 Error_Msg_N ("type of aggregate cannot be abstract", N);
4357 end if;
4359 if No (First_Entity (Typ)) and then Null_Record_Present (N) then
4360 Set_Etype (N, Typ);
4361 return;
4363 elsif Present (First_Entity (Typ))
4364 and then Null_Record_Present (N)
4365 and then not Is_Tagged_Type (Typ)
4366 then
4367 Error_Msg_N ("record aggregate cannot be null", N);
4368 return;
4370 -- If the type has no components, then the aggregate should either
4371 -- have "null record", or in Ada 2005 it could instead have a single
4372 -- component association given by "others => <>". For Ada 95 we flag an
4373 -- error at this point, but for Ada 2005 we proceed with checking the
4374 -- associations below, which will catch the case where it's not an
4375 -- aggregate with "others => <>". Note that the legality of a <>
4376 -- aggregate for a null record type was established by AI05-016.
4378 elsif No (First_Entity (Typ))
4379 and then Ada_Version < Ada_2005
4380 then
4381 Error_Msg_N ("record aggregate must be null", N);
4382 return;
4383 end if;
4385 -- STEP 2: Verify aggregate structure
4387 Step_2 : declare
4388 Assoc : Node_Id;
4389 Bad_Aggregate : Boolean := False;
4390 Selector_Name : Node_Id;
4392 begin
4393 if Present (Component_Associations (N)) then
4394 Assoc := First (Component_Associations (N));
4395 else
4396 Assoc := Empty;
4397 end if;
4399 while Present (Assoc) loop
4400 Selector_Name := First (Choices (Assoc));
4401 while Present (Selector_Name) loop
4402 if Nkind (Selector_Name) = N_Identifier then
4403 null;
4405 elsif Nkind (Selector_Name) = N_Others_Choice then
4406 if Selector_Name /= First (Choices (Assoc))
4407 or else Present (Next (Selector_Name))
4408 then
4409 Error_Msg_N
4410 ("OTHERS must appear alone in a choice list",
4411 Selector_Name);
4412 return;
4414 elsif Present (Next (Assoc)) then
4415 Error_Msg_N
4416 ("OTHERS must appear last in an aggregate",
4417 Selector_Name);
4418 return;
4420 -- (Ada 2005): If this is an association with a box,
4421 -- indicate that the association need not represent
4422 -- any component.
4424 elsif Box_Present (Assoc) then
4425 Others_Box := 1;
4426 Box_Node := Assoc;
4427 end if;
4429 else
4430 Error_Msg_N
4431 ("selector name should be identifier or OTHERS",
4432 Selector_Name);
4433 Bad_Aggregate := True;
4434 end if;
4436 Next (Selector_Name);
4437 end loop;
4439 Next (Assoc);
4440 end loop;
4442 if Bad_Aggregate then
4443 return;
4444 end if;
4445 end Step_2;
4447 -- STEP 3: Find discriminant Values
4449 Step_3 : declare
4450 Discrim : Entity_Id;
4451 Missing_Discriminants : Boolean := False;
4453 begin
4454 if Present (Expressions (N)) then
4455 Positional_Expr := First (Expressions (N));
4456 else
4457 Positional_Expr := Empty;
4458 end if;
4460 -- AI05-0115: if the ancestor part is a subtype mark, the ancestor
4461 -- must not have unknown discriminants.
4463 if Is_Derived_Type (Typ)
4464 and then Has_Unknown_Discriminants (Root_Type (Typ))
4465 and then Nkind (N) /= N_Extension_Aggregate
4466 then
4467 Error_Msg_NE
4468 ("aggregate not available for type& whose ancestor "
4469 & "has unknown discriminants ", N, Typ);
4470 end if;
4472 if Has_Unknown_Discriminants (Typ)
4473 and then Present (Underlying_Record_View (Typ))
4474 then
4475 Discrim := First_Discriminant (Underlying_Record_View (Typ));
4476 elsif Has_Discriminants (Typ) then
4477 Discrim := First_Discriminant (Typ);
4478 else
4479 Discrim := Empty;
4480 end if;
4482 -- First find the discriminant values in the positional components
4484 while Present (Discrim) and then Present (Positional_Expr) loop
4485 if Discriminant_Present (Discrim) then
4486 Resolve_Aggr_Expr (Positional_Expr, Discrim);
4488 -- Ada 2005 (AI-231)
4490 if Ada_Version >= Ada_2005
4491 and then Known_Null (Positional_Expr)
4492 then
4493 Check_Can_Never_Be_Null (Discrim, Positional_Expr);
4494 end if;
4496 Next (Positional_Expr);
4497 end if;
4499 if Present (Get_Value (Discrim, Component_Associations (N))) then
4500 Error_Msg_NE
4501 ("more than one value supplied for discriminant&",
4502 N, Discrim);
4503 end if;
4505 Next_Discriminant (Discrim);
4506 end loop;
4508 -- Find remaining discriminant values if any among named components
4510 while Present (Discrim) loop
4511 Expr := Get_Value (Discrim, Component_Associations (N), True);
4513 if not Discriminant_Present (Discrim) then
4514 if Present (Expr) then
4515 Error_Msg_NE
4516 ("more than one value supplied for discriminant &",
4517 N, Discrim);
4518 end if;
4520 elsif No (Expr) then
4521 Error_Msg_NE
4522 ("no value supplied for discriminant &", N, Discrim);
4523 Missing_Discriminants := True;
4525 else
4526 Resolve_Aggr_Expr (Expr, Discrim);
4527 end if;
4529 Next_Discriminant (Discrim);
4530 end loop;
4532 if Missing_Discriminants then
4533 return;
4534 end if;
4536 -- At this point and until the beginning of STEP 6, New_Assoc_List
4537 -- contains only the discriminants and their values.
4539 end Step_3;
4541 -- STEP 4: Set the Etype of the record aggregate
4543 -- ??? This code is pretty much a copy of Sem_Ch3.Build_Subtype. That
4544 -- routine should really be exported in sem_util or some such and used
4545 -- in sem_ch3 and here rather than have a copy of the code which is a
4546 -- maintenance nightmare.
4548 -- ??? Performance WARNING. The current implementation creates a new
4549 -- itype for all aggregates whose base type is discriminated. This means
4550 -- that for record aggregates nested inside an array aggregate we will
4551 -- create a new itype for each record aggregate if the array component
4552 -- type has discriminants. For large aggregates this may be a problem.
4553 -- What should be done in this case is to reuse itypes as much as
4554 -- possible.
4556 if Has_Discriminants (Typ)
4557 or else (Has_Unknown_Discriminants (Typ)
4558 and then Present (Underlying_Record_View (Typ)))
4559 then
4560 Build_Constrained_Itype : declare
4561 Constrs : constant List_Id := New_List;
4562 Loc : constant Source_Ptr := Sloc (N);
4563 Def_Id : Entity_Id;
4564 Indic : Node_Id;
4565 New_Assoc : Node_Id;
4566 Subtyp_Decl : Node_Id;
4568 begin
4569 New_Assoc := First (New_Assoc_List);
4570 while Present (New_Assoc) loop
4571 Append_To (Constrs, Duplicate_Subexpr (Expression (New_Assoc)));
4572 Next (New_Assoc);
4573 end loop;
4575 if Has_Unknown_Discriminants (Typ)
4576 and then Present (Underlying_Record_View (Typ))
4577 then
4578 Indic :=
4579 Make_Subtype_Indication (Loc,
4580 Subtype_Mark =>
4581 New_Occurrence_Of (Underlying_Record_View (Typ), Loc),
4582 Constraint =>
4583 Make_Index_Or_Discriminant_Constraint (Loc,
4584 Constraints => Constrs));
4585 else
4586 Indic :=
4587 Make_Subtype_Indication (Loc,
4588 Subtype_Mark =>
4589 New_Occurrence_Of (Base_Type (Typ), Loc),
4590 Constraint =>
4591 Make_Index_Or_Discriminant_Constraint (Loc,
4592 Constraints => Constrs));
4593 end if;
4595 Def_Id := Create_Itype (Ekind (Typ), N);
4597 Subtyp_Decl :=
4598 Make_Subtype_Declaration (Loc,
4599 Defining_Identifier => Def_Id,
4600 Subtype_Indication => Indic);
4601 Set_Parent (Subtyp_Decl, Parent (N));
4603 -- Itypes must be analyzed with checks off (see itypes.ads)
4605 Analyze (Subtyp_Decl, Suppress => All_Checks);
4607 Set_Etype (N, Def_Id);
4608 Check_Static_Discriminated_Subtype
4609 (Def_Id, Expression (First (New_Assoc_List)));
4610 end Build_Constrained_Itype;
4612 else
4613 Set_Etype (N, Typ);
4614 end if;
4616 -- STEP 5: Get remaining components according to discriminant values
4618 Step_5 : declare
4619 Dnode : Node_Id;
4620 Errors_Found : Boolean := False;
4621 Record_Def : Node_Id;
4622 Parent_Typ : Entity_Id;
4623 Parent_Typ_List : Elist_Id;
4624 Parent_Elmt : Elmt_Id;
4625 Root_Typ : Entity_Id;
4627 begin
4628 if Is_Derived_Type (Typ) and then Is_Tagged_Type (Typ) then
4629 Parent_Typ_List := New_Elmt_List;
4631 -- If this is an extension aggregate, the component list must
4632 -- include all components that are not in the given ancestor type.
4633 -- Otherwise, the component list must include components of all
4634 -- ancestors, starting with the root.
4636 if Nkind (N) = N_Extension_Aggregate then
4637 Root_Typ := Base_Type (Etype (Ancestor_Part (N)));
4639 else
4640 -- AI05-0115: check legality of aggregate for type with a
4641 -- private ancestor.
4643 Root_Typ := Root_Type (Typ);
4644 if Has_Private_Ancestor (Typ) then
4645 declare
4646 Ancestor : constant Entity_Id :=
4647 Find_Private_Ancestor (Typ);
4648 Ancestor_Unit : constant Entity_Id :=
4649 Cunit_Entity
4650 (Get_Source_Unit (Ancestor));
4651 Parent_Unit : constant Entity_Id :=
4652 Cunit_Entity (Get_Source_Unit
4653 (Base_Type (Etype (Ancestor))));
4654 begin
4655 -- Check whether we are in a scope that has full view
4656 -- over the private ancestor and its parent. This can
4657 -- only happen if the derivation takes place in a child
4658 -- unit of the unit that declares the parent, and we are
4659 -- in the private part or body of that child unit, else
4660 -- the aggregate is illegal.
4662 if Is_Child_Unit (Ancestor_Unit)
4663 and then Scope (Ancestor_Unit) = Parent_Unit
4664 and then In_Open_Scopes (Scope (Ancestor))
4665 and then
4666 (In_Private_Part (Scope (Ancestor))
4667 or else In_Package_Body (Scope (Ancestor)))
4668 then
4669 null;
4671 else
4672 Error_Msg_NE
4673 ("type of aggregate has private ancestor&!",
4674 N, Root_Typ);
4675 Error_Msg_N ("must use extension aggregate!", N);
4676 return;
4677 end if;
4678 end;
4679 end if;
4681 Dnode := Declaration_Node (Base_Type (Root_Typ));
4683 -- If we don't get a full declaration, then we have some error
4684 -- which will get signalled later so skip this part. Otherwise
4685 -- gather components of root that apply to the aggregate type.
4686 -- We use the base type in case there is an applicable stored
4687 -- constraint that renames the discriminants of the root.
4689 if Nkind (Dnode) = N_Full_Type_Declaration then
4690 Record_Def := Type_Definition (Dnode);
4691 Gather_Components
4692 (Base_Type (Typ),
4693 Component_List (Record_Def),
4694 Governed_By => New_Assoc_List,
4695 Into => Components,
4696 Report_Errors => Errors_Found);
4698 if Errors_Found then
4699 Error_Msg_N
4700 ("discriminant controlling variant part is not static",
4702 return;
4703 end if;
4704 end if;
4705 end if;
4707 Parent_Typ := Base_Type (Typ);
4708 while Parent_Typ /= Root_Typ loop
4709 Prepend_Elmt (Parent_Typ, To => Parent_Typ_List);
4710 Parent_Typ := Etype (Parent_Typ);
4712 if Nkind (Parent (Base_Type (Parent_Typ))) =
4713 N_Private_Type_Declaration
4714 or else Nkind (Parent (Base_Type (Parent_Typ))) =
4715 N_Private_Extension_Declaration
4716 then
4717 if Nkind (N) /= N_Extension_Aggregate then
4718 Error_Msg_NE
4719 ("type of aggregate has private ancestor&!",
4720 N, Parent_Typ);
4721 Error_Msg_N ("must use extension aggregate!", N);
4722 return;
4724 elsif Parent_Typ /= Root_Typ then
4725 Error_Msg_NE
4726 ("ancestor part of aggregate must be private type&",
4727 Ancestor_Part (N), Parent_Typ);
4728 return;
4729 end if;
4731 -- The current view of ancestor part may be a private type,
4732 -- while the context type is always non-private.
4734 elsif Is_Private_Type (Root_Typ)
4735 and then Present (Full_View (Root_Typ))
4736 and then Nkind (N) = N_Extension_Aggregate
4737 then
4738 exit when Base_Type (Full_View (Root_Typ)) = Parent_Typ;
4739 end if;
4740 end loop;
4742 -- Now collect components from all other ancestors, beginning
4743 -- with the current type. If the type has unknown discriminants
4744 -- use the component list of the Underlying_Record_View, which
4745 -- needs to be used for the subsequent expansion of the aggregate
4746 -- into assignments.
4748 Parent_Elmt := First_Elmt (Parent_Typ_List);
4749 while Present (Parent_Elmt) loop
4750 Parent_Typ := Node (Parent_Elmt);
4752 if Has_Unknown_Discriminants (Parent_Typ)
4753 and then Present (Underlying_Record_View (Typ))
4754 then
4755 Parent_Typ := Underlying_Record_View (Parent_Typ);
4756 end if;
4758 Record_Def := Type_Definition (Parent (Base_Type (Parent_Typ)));
4759 Gather_Components (Empty,
4760 Component_List (Record_Extension_Part (Record_Def)),
4761 Governed_By => New_Assoc_List,
4762 Into => Components,
4763 Report_Errors => Errors_Found);
4765 Next_Elmt (Parent_Elmt);
4766 end loop;
4768 -- Typ is not a derived tagged type
4770 else
4771 Record_Def := Type_Definition (Parent (Base_Type (Typ)));
4773 if Null_Present (Record_Def) then
4774 null;
4776 elsif not Has_Unknown_Discriminants (Typ) then
4777 Gather_Components
4778 (Base_Type (Typ),
4779 Component_List (Record_Def),
4780 Governed_By => New_Assoc_List,
4781 Into => Components,
4782 Report_Errors => Errors_Found);
4784 else
4785 Gather_Components
4786 (Base_Type (Underlying_Record_View (Typ)),
4787 Component_List (Record_Def),
4788 Governed_By => New_Assoc_List,
4789 Into => Components,
4790 Report_Errors => Errors_Found);
4791 end if;
4792 end if;
4794 if Errors_Found then
4795 return;
4796 end if;
4797 end Step_5;
4799 -- STEP 6: Find component Values
4801 Component := Empty;
4802 Component_Elmt := First_Elmt (Components);
4804 -- First scan the remaining positional associations in the aggregate.
4805 -- Remember that at this point Positional_Expr contains the current
4806 -- positional association if any is left after looking for discriminant
4807 -- values in step 3.
4809 while Present (Positional_Expr) and then Present (Component_Elmt) loop
4810 Component := Node (Component_Elmt);
4811 Resolve_Aggr_Expr (Positional_Expr, Component);
4813 -- Ada 2005 (AI-231)
4815 if Ada_Version >= Ada_2005 and then Known_Null (Positional_Expr) then
4816 Check_Can_Never_Be_Null (Component, Positional_Expr);
4817 end if;
4819 if Present (Get_Value (Component, Component_Associations (N))) then
4820 Error_Msg_NE
4821 ("more than one value supplied for Component &", N, Component);
4822 end if;
4824 Next (Positional_Expr);
4825 Next_Elmt (Component_Elmt);
4826 end loop;
4828 if Present (Positional_Expr) then
4829 Error_Msg_N
4830 ("too many components for record aggregate", Positional_Expr);
4831 end if;
4833 -- Now scan for the named arguments of the aggregate
4835 while Present (Component_Elmt) loop
4836 Component := Node (Component_Elmt);
4837 Expr := Get_Value (Component, Component_Associations (N), True);
4839 -- Note: The previous call to Get_Value sets the value of the
4840 -- variable Is_Box_Present.
4842 -- Ada 2005 (AI-287): Handle components with default initialization.
4843 -- Note: This feature was originally added to Ada 2005 for limited
4844 -- but it was finally allowed with any type.
4846 if Is_Box_Present then
4847 Check_Box_Component : declare
4848 Ctyp : constant Entity_Id := Etype (Component);
4850 begin
4851 -- If there is a default expression for the aggregate, copy
4852 -- it into a new association. This copy must modify the scopes
4853 -- of internal types that may be attached to the expression
4854 -- (e.g. index subtypes of arrays) because in general the type
4855 -- declaration and the aggregate appear in different scopes,
4856 -- and the backend requires the scope of the type to match the
4857 -- point at which it is elaborated.
4859 -- If the component has an initialization procedure (IP) we
4860 -- pass the component to the expander, which will generate
4861 -- the call to such IP.
4863 -- If the component has discriminants, their values must
4864 -- be taken from their subtype. This is indispensable for
4865 -- constraints that are given by the current instance of an
4866 -- enclosing type, to allow the expansion of the aggregate to
4867 -- replace the reference to the current instance by the target
4868 -- object of the aggregate.
4870 if Present (Parent (Component))
4871 and then Nkind (Parent (Component)) = N_Component_Declaration
4872 and then Present (Expression (Parent (Component)))
4873 then
4874 Expr :=
4875 New_Copy_Tree_And_Copy_Dimensions
4876 (Expression (Parent (Component)),
4877 New_Scope => Current_Scope,
4878 New_Sloc => Sloc (N));
4880 -- As the type of the copied default expression may refer
4881 -- to discriminants of the record type declaration, these
4882 -- non-stored discriminants need to be rewritten into stored
4883 -- discriminant values for the aggregate. This is required
4884 -- in GNATprove mode, and is adopted in all modes to avoid
4885 -- special-casing GNATprove mode.
4887 if Is_Array_Type (Etype (Expr)) then
4888 declare
4889 Rec_Typ : constant Entity_Id := Scope (Component);
4890 -- Root record type whose discriminants may be used as
4891 -- bounds in range nodes.
4893 Index : Node_Id;
4895 begin
4896 -- Rewrite the range nodes occurring in the indexes
4897 -- and their types.
4899 Index := First_Index (Etype (Expr));
4900 while Present (Index) loop
4901 Rewrite_Range (Rec_Typ, Index);
4902 Rewrite_Range
4903 (Rec_Typ, Scalar_Range (Etype (Index)));
4905 Next_Index (Index);
4906 end loop;
4908 -- Rewrite the range nodes occurring as aggregate
4909 -- bounds.
4911 if Nkind (Expr) = N_Aggregate
4912 and then Present (Aggregate_Bounds (Expr))
4913 then
4914 Rewrite_Range (Rec_Typ, Aggregate_Bounds (Expr));
4915 end if;
4916 end;
4917 end if;
4919 Add_Association
4920 (Component => Component,
4921 Expr => Expr,
4922 Assoc_List => New_Assoc_List);
4923 Set_Has_Self_Reference (N);
4925 -- A box-defaulted access component gets the value null. Also
4926 -- included are components of private types whose underlying
4927 -- type is an access type. In either case set the type of the
4928 -- literal, for subsequent use in semantic checks.
4930 elsif Present (Underlying_Type (Ctyp))
4931 and then Is_Access_Type (Underlying_Type (Ctyp))
4932 then
4933 -- If the component's type is private with an access type as
4934 -- its underlying type then we have to create an unchecked
4935 -- conversion to satisfy type checking.
4937 if Is_Private_Type (Ctyp) then
4938 declare
4939 Qual_Null : constant Node_Id :=
4940 Make_Qualified_Expression (Sloc (N),
4941 Subtype_Mark =>
4942 New_Occurrence_Of
4943 (Underlying_Type (Ctyp), Sloc (N)),
4944 Expression => Make_Null (Sloc (N)));
4946 Convert_Null : constant Node_Id :=
4947 Unchecked_Convert_To
4948 (Ctyp, Qual_Null);
4950 begin
4951 Analyze_And_Resolve (Convert_Null, Ctyp);
4952 Add_Association
4953 (Component => Component,
4954 Expr => Convert_Null,
4955 Assoc_List => New_Assoc_List);
4956 end;
4958 -- Otherwise the component type is non-private
4960 else
4961 Expr := Make_Null (Sloc (N));
4962 Set_Etype (Expr, Ctyp);
4964 Add_Association
4965 (Component => Component,
4966 Expr => Expr,
4967 Assoc_List => New_Assoc_List);
4968 end if;
4970 -- Ada 2012: If component is scalar with default value, use it
4972 elsif Is_Scalar_Type (Ctyp)
4973 and then Has_Default_Aspect (Ctyp)
4974 then
4975 Add_Association
4976 (Component => Component,
4977 Expr =>
4978 Default_Aspect_Value
4979 (First_Subtype (Underlying_Type (Ctyp))),
4980 Assoc_List => New_Assoc_List);
4982 elsif Has_Non_Null_Base_Init_Proc (Ctyp)
4983 or else not Expander_Active
4984 then
4985 if Is_Record_Type (Ctyp)
4986 and then Has_Discriminants (Ctyp)
4987 and then not Is_Private_Type (Ctyp)
4988 then
4989 -- We build a partially initialized aggregate with the
4990 -- values of the discriminants and box initialization
4991 -- for the rest, if other components are present.
4993 -- The type of the aggregate is the known subtype of
4994 -- the component. The capture of discriminants must be
4995 -- recursive because subcomponents may be constrained
4996 -- (transitively) by discriminants of enclosing types.
4997 -- For a private type with discriminants, a call to the
4998 -- initialization procedure will be generated, and no
4999 -- subaggregate is needed.
5001 Capture_Discriminants : declare
5002 Loc : constant Source_Ptr := Sloc (N);
5003 Expr : Node_Id;
5005 begin
5006 Expr := Make_Aggregate (Loc, New_List, New_List);
5007 Set_Etype (Expr, Ctyp);
5009 -- If the enclosing type has discriminants, they have
5010 -- been collected in the aggregate earlier, and they
5011 -- may appear as constraints of subcomponents.
5013 -- Similarly if this component has discriminants, they
5014 -- might in turn be propagated to their components.
5016 if Has_Discriminants (Typ) then
5017 Add_Discriminant_Values (Expr, New_Assoc_List);
5018 Propagate_Discriminants (Expr, New_Assoc_List);
5020 elsif Has_Discriminants (Ctyp) then
5021 Add_Discriminant_Values
5022 (Expr, Component_Associations (Expr));
5023 Propagate_Discriminants
5024 (Expr, Component_Associations (Expr));
5026 else
5027 declare
5028 Comp : Entity_Id;
5030 begin
5031 -- If the type has additional components, create
5032 -- an OTHERS box association for them.
5034 Comp := First_Component (Ctyp);
5035 while Present (Comp) loop
5036 if Ekind (Comp) = E_Component then
5037 if not Is_Record_Type (Etype (Comp)) then
5038 Append_To
5039 (Component_Associations (Expr),
5040 Make_Component_Association (Loc,
5041 Choices =>
5042 New_List (
5043 Make_Others_Choice (Loc)),
5044 Expression => Empty,
5045 Box_Present => True));
5046 end if;
5048 exit;
5049 end if;
5051 Next_Component (Comp);
5052 end loop;
5053 end;
5054 end if;
5056 Add_Association
5057 (Component => Component,
5058 Expr => Expr,
5059 Assoc_List => New_Assoc_List);
5060 end Capture_Discriminants;
5062 -- Otherwise the component type is not a record, or it has
5063 -- not discriminants, or it is private.
5065 else
5066 Add_Association
5067 (Component => Component,
5068 Expr => Empty,
5069 Assoc_List => New_Assoc_List,
5070 Is_Box_Present => True);
5071 end if;
5073 -- Otherwise we only need to resolve the expression if the
5074 -- component has partially initialized values (required to
5075 -- expand the corresponding assignments and run-time checks).
5077 elsif Present (Expr)
5078 and then Is_Partially_Initialized_Type (Ctyp)
5079 then
5080 Resolve_Aggr_Expr (Expr, Component);
5081 end if;
5082 end Check_Box_Component;
5084 elsif No (Expr) then
5086 -- Ignore hidden components associated with the position of the
5087 -- interface tags: these are initialized dynamically.
5089 if not Present (Related_Type (Component)) then
5090 Error_Msg_NE
5091 ("no value supplied for component &!", N, Component);
5092 end if;
5094 else
5095 Resolve_Aggr_Expr (Expr, Component);
5096 end if;
5098 Next_Elmt (Component_Elmt);
5099 end loop;
5101 -- STEP 7: check for invalid components + check type in choice list
5103 Step_7 : declare
5104 Assoc : Node_Id;
5105 New_Assoc : Node_Id;
5107 Selectr : Node_Id;
5108 -- Selector name
5110 Typech : Entity_Id;
5111 -- Type of first component in choice list
5113 begin
5114 if Present (Component_Associations (N)) then
5115 Assoc := First (Component_Associations (N));
5116 else
5117 Assoc := Empty;
5118 end if;
5120 Verification : while Present (Assoc) loop
5121 Selectr := First (Choices (Assoc));
5122 Typech := Empty;
5124 if Nkind (Selectr) = N_Others_Choice then
5126 -- Ada 2005 (AI-287): others choice may have expression or box
5128 if No (Others_Etype) and then Others_Box = 0 then
5129 Error_Msg_N
5130 ("OTHERS must represent at least one component", Selectr);
5132 elsif Others_Box = 1 and then Warn_On_Redundant_Constructs then
5133 Error_Msg_N ("others choice is redundant?", Box_Node);
5134 Error_Msg_N
5135 ("\previous choices cover all components?", Box_Node);
5136 end if;
5138 exit Verification;
5139 end if;
5141 while Present (Selectr) loop
5142 New_Assoc := First (New_Assoc_List);
5143 while Present (New_Assoc) loop
5144 Component := First (Choices (New_Assoc));
5146 if Chars (Selectr) = Chars (Component) then
5147 if Style_Check then
5148 Check_Identifier (Selectr, Entity (Component));
5149 end if;
5151 exit;
5152 end if;
5154 Next (New_Assoc);
5155 end loop;
5157 -- If no association, this is not a legal component of the type
5158 -- in question, unless its association is provided with a box.
5160 if No (New_Assoc) then
5161 if Box_Present (Parent (Selectr)) then
5163 -- This may still be a bogus component with a box. Scan
5164 -- list of components to verify that a component with
5165 -- that name exists.
5167 declare
5168 C : Entity_Id;
5170 begin
5171 C := First_Component (Typ);
5172 while Present (C) loop
5173 if Chars (C) = Chars (Selectr) then
5175 -- If the context is an extension aggregate,
5176 -- the component must not be inherited from
5177 -- the ancestor part of the aggregate.
5179 if Nkind (N) /= N_Extension_Aggregate
5180 or else
5181 Scope (Original_Record_Component (C)) /=
5182 Etype (Ancestor_Part (N))
5183 then
5184 exit;
5185 end if;
5186 end if;
5188 Next_Component (C);
5189 end loop;
5191 if No (C) then
5192 Error_Msg_Node_2 := Typ;
5193 Error_Msg_N ("& is not a component of}", Selectr);
5194 end if;
5195 end;
5197 elsif Chars (Selectr) /= Name_uTag
5198 and then Chars (Selectr) /= Name_uParent
5199 then
5200 if not Has_Discriminants (Typ) then
5201 Error_Msg_Node_2 := Typ;
5202 Error_Msg_N ("& is not a component of}", Selectr);
5203 else
5204 Error_Msg_N
5205 ("& is not a component of the aggregate subtype",
5206 Selectr);
5207 end if;
5209 Check_Misspelled_Component (Components, Selectr);
5210 end if;
5212 elsif No (Typech) then
5213 Typech := Base_Type (Etype (Component));
5215 -- AI05-0199: In Ada 2012, several components of anonymous
5216 -- access types can appear in a choice list, as long as the
5217 -- designated types match.
5219 elsif Typech /= Base_Type (Etype (Component)) then
5220 if Ada_Version >= Ada_2012
5221 and then Ekind (Typech) = E_Anonymous_Access_Type
5222 and then
5223 Ekind (Etype (Component)) = E_Anonymous_Access_Type
5224 and then Base_Type (Designated_Type (Typech)) =
5225 Base_Type (Designated_Type (Etype (Component)))
5226 and then
5227 Subtypes_Statically_Match (Typech, (Etype (Component)))
5228 then
5229 null;
5231 elsif not Box_Present (Parent (Selectr)) then
5232 Error_Msg_N
5233 ("components in choice list must have same type",
5234 Selectr);
5235 end if;
5236 end if;
5238 Next (Selectr);
5239 end loop;
5241 Next (Assoc);
5242 end loop Verification;
5243 end Step_7;
5245 -- STEP 8: replace the original aggregate
5247 Step_8 : declare
5248 New_Aggregate : constant Node_Id := New_Copy (N);
5250 begin
5251 Set_Expressions (New_Aggregate, No_List);
5252 Set_Etype (New_Aggregate, Etype (N));
5253 Set_Component_Associations (New_Aggregate, New_Assoc_List);
5254 Set_Check_Actuals (New_Aggregate, Check_Actuals (N));
5256 Rewrite (N, New_Aggregate);
5257 end Step_8;
5259 -- Check the dimensions of the components in the record aggregate
5261 Analyze_Dimension_Extension_Or_Record_Aggregate (N);
5262 end Resolve_Record_Aggregate;
5264 -----------------------------
5265 -- Check_Can_Never_Be_Null --
5266 -----------------------------
5268 procedure Check_Can_Never_Be_Null (Typ : Entity_Id; Expr : Node_Id) is
5269 Comp_Typ : Entity_Id;
5271 begin
5272 pragma Assert
5273 (Ada_Version >= Ada_2005
5274 and then Present (Expr)
5275 and then Known_Null (Expr));
5277 case Ekind (Typ) is
5278 when E_Array_Type =>
5279 Comp_Typ := Component_Type (Typ);
5281 when E_Component
5282 | E_Discriminant
5284 Comp_Typ := Etype (Typ);
5286 when others =>
5287 return;
5288 end case;
5290 if Can_Never_Be_Null (Comp_Typ) then
5292 -- Here we know we have a constraint error. Note that we do not use
5293 -- Apply_Compile_Time_Constraint_Error here to the Expr, which might
5294 -- seem the more natural approach. That's because in some cases the
5295 -- components are rewritten, and the replacement would be missed.
5296 -- We do not mark the whole aggregate as raising a constraint error,
5297 -- because the association may be a null array range.
5299 Error_Msg_N
5300 ("(Ada 2005) null not allowed in null-excluding component??", Expr);
5301 Error_Msg_N
5302 ("\Constraint_Error will be raised at run time??", Expr);
5304 Rewrite (Expr,
5305 Make_Raise_Constraint_Error
5306 (Sloc (Expr), Reason => CE_Access_Check_Failed));
5307 Set_Etype (Expr, Comp_Typ);
5308 Set_Analyzed (Expr);
5309 end if;
5310 end Check_Can_Never_Be_Null;
5312 ---------------------
5313 -- Sort_Case_Table --
5314 ---------------------
5316 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is
5317 U : constant Int := Case_Table'Last;
5318 K : Int;
5319 J : Int;
5320 T : Case_Bounds;
5322 begin
5323 K := 1;
5324 while K < U loop
5325 T := Case_Table (K + 1);
5327 J := K + 1;
5328 while J > 1
5329 and then Expr_Value (Case_Table (J - 1).Lo) > Expr_Value (T.Lo)
5330 loop
5331 Case_Table (J) := Case_Table (J - 1);
5332 J := J - 1;
5333 end loop;
5335 Case_Table (J) := T;
5336 K := K + 1;
5337 end loop;
5338 end Sort_Case_Table;
5340 end Sem_Aggr;